FoxIntermediateBackend.Mod 558 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. NonPointer = -1; (* special pointer values *)
  53. NoType = 0; (* special type info values *)
  54. LhsIsPointer = 0; (* for the operator kind *)
  55. RhsIsPointer = 1;
  56. (* priority values, lower means higher priority *)
  57. EntryPriority=-4;
  58. FirstPriority=-3;
  59. InitPriority=-2;
  60. ExitPriority=-1;
  61. BasePointerTypeSize = 5;
  62. BaseArrayTypeSize = BasePointerTypeSize + 3;
  63. LengthOffset = BasePointerTypeSize + 0;
  64. DataOffset = BasePointerTypeSize + 1;
  65. DescriptorOffset = BasePointerTypeSize + 2;
  66. BaseRecordTypeSize = BasePointerTypeSize + 2;
  67. ActionOffset = BasePointerTypeSize + 0;
  68. MonitorOffset = BasePointerTypeSize + 1;
  69. BaseObjectTypeSize = BaseRecordTypeSize;
  70. ActionTypeSize = 3;
  71. MonitorTypeSize = 7;
  72. ProcessorOffset = BaseObjectTypeSize + 1;
  73. StackLimitOffset* = BaseObjectTypeSize + 3;
  74. QuantumOffset = BaseObjectTypeSize + 4;
  75. (* flags for optimizations with small matricies and vectors (Alexey Morozov) *)
  76. SmallMatrixFlag = 3; (* flag for identification of a small matrix *)
  77. SmallVectorFlag = 3; (* flag for identification of a small vector *)
  78. Size2Flag = 4; (* size = 2 *)
  79. Size3Flag = 5; (* size = 3 *)
  80. Size4Flag = 6; (* size = 4 *)
  81. Size5Flag = 7; (* size = 5 *)
  82. Size6Flag = 8; (* size = 6 *)
  83. Size7Flag = 9; (* size = 7 *)
  84. Size8Flag = 10; (* size = 8 *)
  85. ReflectionSupport = TRUE;
  86. (* Solution for identifying procedure descriptors on the stack and for being able to differentiate "old school" stack frames from the underlying operating system stack frames:
  87. push a procedure desriptor plus one to where the BP pointer would be located. The misalignment of the procedure descriptor makes it possible to identify that it is not
  88. a base pointer but a procedure descriptor. The base pointer itself is in such cases located at BP + address size.
  89. *)
  90. TYPE
  91. Position=SyntaxTree.Position;
  92. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  93. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  94. Operand = RECORD
  95. mode: SHORTINT;
  96. op: IntermediateCode.Operand;
  97. tag: IntermediateCode.Operand;
  98. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  99. dimOffset: LONGINT;
  100. END;
  101. Fixup= POINTER TO RECORD
  102. pc: LONGINT;
  103. nextFixup: Fixup;
  104. END;
  105. WriteBackCall = POINTER TO RECORD
  106. call: SyntaxTree.ProcedureCallDesignator;
  107. next: WriteBackCall;
  108. END;
  109. Label= OBJECT
  110. VAR
  111. fixups: Fixup;
  112. section: IntermediateCode.Section;
  113. pc: LONGINT;
  114. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  115. BEGIN
  116. SELF.section := section; pc := -1;
  117. END InitLabel;
  118. PROCEDURE Resolve(pc: LONGINT);
  119. VAR at: LONGINT;
  120. BEGIN
  121. SELF.pc := pc;
  122. WHILE(fixups # NIL) DO
  123. at := fixups.pc;
  124. section.PatchAddress(at,pc);
  125. fixups := fixups.nextFixup;
  126. END;
  127. END Resolve;
  128. PROCEDURE AddFixup(at: LONGINT);
  129. VAR fixup: Fixup;
  130. BEGIN
  131. ASSERT(pc=-1);
  132. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  133. END AddFixup;
  134. END Label;
  135. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  136. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  137. VAR
  138. backend: IntermediateBackend;
  139. implementationVisitor: ImplementationVisitor;
  140. meta: MetaDataGenerator;
  141. system: Global.System;
  142. currentScope: SyntaxTree.Scope;
  143. module: Sections.Module;
  144. moduleSelf: SyntaxTree.Variable;
  145. dump: BOOLEAN;
  146. forceModuleBody: BOOLEAN;
  147. addressType: IntermediateCode.Type;
  148. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  149. BEGIN
  150. currentScope := NIL; module := NIL; moduleSelf := NIL;
  151. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  152. SELF.dump := dump;
  153. SELF.backend := backend;
  154. SELF.forceModuleBody := forceModuleBody;
  155. addressType := IntermediateCode.GetType(system,system.addressType)
  156. END Init;
  157. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  158. BEGIN
  159. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  160. END Error;
  161. PROCEDURE Type(x: SyntaxTree.Type);
  162. BEGIN
  163. x.Accept(SELF);
  164. END Type;
  165. (** types **)
  166. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  167. BEGIN (* no code emission *) END VisitBasicType;
  168. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  169. BEGIN (* no code emission *) END VisitCharacterType;
  170. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  171. BEGIN (* no code emission *) END VisitIntegerType;
  172. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  173. BEGIN (* no code emission *) END VisitFloatType;
  174. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  175. BEGIN (* no code emission *) END VisitComplexType;
  176. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  177. VAR type: SyntaxTree.Type;
  178. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  179. type := x.resolved;
  180. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  181. meta.CheckTypeDeclaration(type);
  182. END;
  183. END VisitQualifiedType;
  184. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  185. BEGIN (* no code emission *) END VisitStringType;
  186. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  187. BEGIN (* no code emission *)
  188. END VisitArrayRangeType;
  189. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  190. BEGIN (* no code emission *) END VisitArrayType;
  191. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  192. BEGIN (* no code emission *) END VisitPortType;
  193. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  194. BEGIN
  195. meta.CheckTypeDeclaration(x);
  196. END VisitMathArrayType;
  197. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  198. BEGIN
  199. meta.CheckTypeDeclaration(x);
  200. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  201. END VisitPointerType;
  202. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  203. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  204. BEGIN (* no code emission *)
  205. meta.CheckTypeDeclaration(x);
  206. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  207. IF x.pointerType.typeDeclaration # NIL THEN
  208. td := x.pointerType.typeDeclaration
  209. ELSE
  210. td := x.typeDeclaration
  211. END;
  212. Global.GetSymbolName(td,name);
  213. (* code section for object *)
  214. END;
  215. Scope(x.recordScope);
  216. END VisitRecordType;
  217. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  218. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  219. BEGIN
  220. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  221. WHILE (this # NIL) & (this.identifier# id) DO
  222. this := this.nextModifier;
  223. END;
  224. RETURN this # NIL
  225. END HasFlag;
  226. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  227. VAR port,adr: LONGINT; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  228. BEGIN
  229. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  230. capabilities := {};
  231. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  232. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  233. backend.SetCapabilities(capabilities);
  234. IF ~implementationVisitor.checker.SkipImplementation(x) THEN
  235. Scope(x.cellScope);
  236. END;
  237. END VisitCellType;
  238. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  239. BEGIN (* no code emission *) END VisitProcedureType;
  240. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  241. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  242. END VisitEnumerationType;
  243. (* symbols *)
  244. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  245. BEGIN
  246. Procedure(x);
  247. END VisitProcedure;
  248. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  249. BEGIN
  250. Procedure(x);
  251. END VisitOperator;
  252. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  253. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, dim, i: LONGINT;
  254. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  255. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  256. BEGIN
  257. type := type.resolved;
  258. IF type IS SyntaxTree.RecordType THEN
  259. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  260. ELSIF (type IS SyntaxTree.ArrayType) THEN
  261. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  262. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  263. END;
  264. ELSIF type IS SyntaxTree.MathArrayType THEN
  265. WITH type: SyntaxTree.MathArrayType DO
  266. IF type.form = SyntaxTree.Open THEN
  267. RETURN TRUE
  268. ELSIF type.form = SyntaxTree.Static THEN
  269. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  270. END;
  271. END;
  272. END;
  273. RETURN FALSE
  274. END TypeNeedsInitialization;
  275. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  276. VAR variable: SyntaxTree.Variable;
  277. BEGIN
  278. variable := scope.firstVariable;
  279. WHILE variable # NIL DO
  280. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  281. IF variable.initializer # NIL THEN RETURN TRUE END;
  282. variable := variable.nextVariable;
  283. END;
  284. RETURN FALSE
  285. END ScopeNeedsInitialization;
  286. PROCEDURE SingleInitialize(CONST op: IntermediateCode.Operand; offset: LONGINT);
  287. BEGIN
  288. size := offset - lastUpdated;
  289. IF size > 0 THEN
  290. irv.Emit(Reserve(x.position,size));
  291. END;
  292. irv.Emit(Data(x.position, op));
  293. lastUpdated := offset + ToMemoryUnits(system, op.type.sizeInBits);
  294. END SingleInitialize;
  295. PROCEDURE Initialize(type: SyntaxTree.Type; initializer: SyntaxTree.Expression; offset: LONGINT);
  296. VAR op: Operand; baseType: SyntaxTree.Type; variable: SyntaxTree.Variable; i: LONGINT; size: SIZE;
  297. BEGIN
  298. IF type = NIL THEN RETURN ELSE type := type.resolved END;
  299. WITH type: SyntaxTree.RecordType DO
  300. baseType := type.baseType;
  301. IF baseType # NIL THEN
  302. baseType := baseType.resolved;
  303. IF baseType IS SyntaxTree.PointerType THEN
  304. baseType := baseType(SyntaxTree.PointerType).pointerBase
  305. END;
  306. Initialize(baseType,NIL, offset);
  307. END;
  308. variable := type.recordScope.firstVariable;
  309. WHILE variable # NIL DO
  310. Initialize(variable.type, variable.initializer, offset+ToMemoryUnits(system,variable.offsetInBits));
  311. variable := variable.nextVariable
  312. END;
  313. | type: SyntaxTree.ArrayType DO
  314. IF type.form = SyntaxTree.Static THEN
  315. baseType := type.arrayBase;
  316. IF TypeNeedsInitialization(baseType) THEN
  317. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  318. FOR i := 0 TO type.staticLength-1 DO
  319. Initialize(baseType,NIL,offset+i*size);
  320. END;
  321. END;
  322. END;
  323. | type: SyntaxTree.MathArrayType DO
  324. IF type.form = SyntaxTree.Open THEN
  325. dim := DynamicDim(type);
  326. baseType := SemanticChecker.ArrayBase(type,dim);
  327. imm := IntermediateCode.Immediate(addressType,dim);
  328. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathDimOffset);
  329. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  330. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  331. SingleInitialize(imm, offset + ToMemoryUnits(system, addressType.sizeInBits)* MathElementSizeOffset);
  332. (* flags remain empty (=0) for open array *)
  333. ELSIF type.form = SyntaxTree.Static THEN
  334. baseType := type.arrayBase;
  335. IF TypeNeedsInitialization(baseType) THEN
  336. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  337. ASSERT(type.staticLength < 1024*1024*1024);
  338. FOR i := 0 TO type.staticLength-1 DO
  339. Initialize(baseType,NIL,offset+i*size);
  340. END;
  341. END;
  342. END;
  343. ELSE
  344. IF initializer # NIL THEN
  345. implementationVisitor.Evaluate(initializer, op);
  346. SingleInitialize(op.op, offset);
  347. END;
  348. END;
  349. END Initialize;
  350. BEGIN
  351. IF x.externalName # NIL THEN RETURN END;
  352. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  353. (* code section for variable *)
  354. Global.GetSymbolSegmentedName(x,name);
  355. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  356. irv.SetExported(IsExported(x));
  357. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  358. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  359. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  360. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  361. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  362. FOR i := 0 TO DynamicDim(x.type)-1 DO
  363. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  364. END;
  365. ELSE
  366. lastUpdated:= 0;
  367. IF implementationVisitor.newObjectFile & ((x.initializer # NIL) OR TypeNeedsInitialization(x.type)) THEN
  368. Initialize(x.type, x.initializer, 0);
  369. END;
  370. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  371. IF size > 0 THEN
  372. irv.Emit(Reserve(x.position,size));
  373. END;
  374. IF ~x.fixed THEN
  375. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  376. ELSE
  377. align := x.alignment;
  378. END;
  379. irv.SetPositionOrAlignment(x.fixed, align);
  380. meta.CheckTypeDeclaration(x.type);
  381. END;
  382. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  383. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  384. END;
  385. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  386. END VisitVariable;
  387. PROCEDURE VisitProperty(x: SyntaxTree.Property);
  388. BEGIN
  389. VisitVariable(x)
  390. END VisitProperty;
  391. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  392. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align, i, dim: LONGINT;
  393. size: LONGINT; lastUpdated: LONGINT; imm: IntermediateCode.Operand;
  394. BEGIN
  395. ASSERT(currentScope IS SyntaxTree.CellScope);
  396. Global.GetSymbolSegmentedName(x,name);
  397. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  398. irv.SetExported(IsExported(x));
  399. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  400. IF (currentScope IS SyntaxTree.CellScope) & IsSemiDynamicArray(x.type) THEN
  401. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  402. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  403. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  404. FOR i := 0 TO DynamicDim(x.type)-1 DO
  405. irv.Emit(Reserve(x.position, ToMemoryUnits(system, system.addressSize)));
  406. END;
  407. ELSE
  408. lastUpdated:= 0;
  409. size := ToMemoryUnits(system,system.SizeOf(x.type)) - lastUpdated;
  410. IF size > 0 THEN
  411. irv.Emit(Reserve(x.position,size));
  412. END;
  413. IF ~x.fixed THEN
  414. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  415. ELSE
  416. align := x.alignment;
  417. END;
  418. irv.SetPositionOrAlignment(x.fixed, align);
  419. meta.CheckTypeDeclaration(x.type);
  420. END;
  421. END VisitParameter;
  422. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  423. BEGIN
  424. Type(x.declaredType); (* => code in objects *)
  425. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  426. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  427. END;
  428. END VisitTypeDeclaration;
  429. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  430. BEGIN
  431. IF (SyntaxTree.Public * x.access # {}) THEN
  432. implementationVisitor.VisitConstant(x);
  433. END;
  434. END VisitConstant;
  435. PROCEDURE Scope(x: SyntaxTree.Scope);
  436. VAR procedure: SyntaxTree.Procedure;
  437. constant: SyntaxTree.Constant;
  438. variable: SyntaxTree.Variable;
  439. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  440. cell: SyntaxTree.CellType;
  441. parameter: SyntaxTree.Parameter;
  442. property: SyntaxTree.Property;
  443. BEGIN
  444. prevScope := currentScope;
  445. currentScope := x;
  446. (* constants treated in implementation visitor *)
  447. WITH x: SyntaxTree.CellScope DO
  448. cell := x.ownerCell;
  449. parameter := cell.firstParameter;
  450. WHILE parameter # NIL DO
  451. VisitParameter(parameter);
  452. parameter := parameter.nextParameter;
  453. END;
  454. property := cell.firstProperty;
  455. WHILE property # NIL DO
  456. VisitProperty(property);
  457. property := property.nextProperty;
  458. END;
  459. ELSE
  460. END;
  461. typeDeclaration := x.firstTypeDeclaration;
  462. WHILE typeDeclaration # NIL DO
  463. VisitTypeDeclaration(typeDeclaration);
  464. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  465. END;
  466. variable := x.firstVariable;
  467. WHILE variable # NIL DO
  468. VisitVariable(variable);
  469. variable := variable.nextVariable;
  470. END;
  471. procedure := x.firstProcedure;
  472. WHILE procedure # NIL DO
  473. VisitProcedure(procedure);
  474. procedure := procedure.nextProcedure;
  475. END;
  476. constant := x.firstConstant;
  477. WHILE constant # NIL DO
  478. VisitConstant(constant);
  479. constant := constant.nextConstant;
  480. END;
  481. currentScope := prevScope;
  482. END Scope;
  483. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  484. VAR parameter: SyntaxTree.Parameter;
  485. BEGIN
  486. parameter := first;
  487. WHILE parameter # NIL DO
  488. VisitParameter(parameter);
  489. parameter := parameter.nextParameter;
  490. END;
  491. END Parameters;
  492. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  493. VAR scope: SyntaxTree.ProcedureScope;
  494. prevScope: SyntaxTree.Scope;
  495. inline, finalizer: BOOLEAN;
  496. procedureType: SyntaxTree.ProcedureType;
  497. pc: LONGINT;
  498. stackSize: LONGINT;
  499. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  500. null,size,src,dest,fp,res: IntermediateCode.Operand;
  501. cc: LONGINT;
  502. cellType: SyntaxTree.CellType;
  503. registerNumber: LONGINT;
  504. registerParameter: Backend.Registers;
  505. registerParameters: LONGINT;
  506. registerClass: IntermediateCode.RegisterClass;
  507. type: IntermediateCode.Type;
  508. formalParameter: SyntaxTree.Parameter;
  509. recordType: SyntaxTree.RecordType;
  510. isModuleBody: BOOLEAN;
  511. parametersSize: LONGINT;
  512. PROCEDURE Signature;
  513. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  514. BEGIN
  515. procedureType := x.type(SyntaxTree.ProcedureType);
  516. returnType := procedureType.returnType;
  517. IF returnType # NIL THEN
  518. meta.CheckTypeDeclaration(returnType)
  519. END;
  520. parameter := procedureType.firstParameter;
  521. WHILE parameter # NIL DO
  522. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  523. parameter := parameter.nextParameter;
  524. END;
  525. END Signature;
  526. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  527. VAR result: BOOLEAN;
  528. BEGIN
  529. result := FALSE;
  530. IF x = SyntaxTree.invalidExpression THEN
  531. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  532. result := TRUE;
  533. value := x.resolved(SyntaxTree.IntegerValue).value;
  534. ELSE
  535. Error(x.position,"expression is not an integer constant");
  536. END;
  537. RETURN result;
  538. END CheckIntegerValue;
  539. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  540. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  541. BEGIN
  542. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  543. WHILE (this # NIL) & (this.identifier # id) DO
  544. this := this.nextModifier;
  545. END;
  546. IF this # NIL THEN
  547. IF this.expression = NIL THEN
  548. Error(this.position,"expected expression value");
  549. ELSIF CheckIntegerValue(this.expression,value) THEN
  550. END;
  551. RETURN TRUE
  552. ELSE RETURN FALSE
  553. END;
  554. END HasValue;
  555. CONST DefaultDataMemorySize=512;
  556. BEGIN
  557. IF x.externalName # NIL THEN RETURN END;
  558. (*
  559. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  560. *)
  561. (* code section for this procedure *)
  562. scope := x.procedureScope;
  563. prevScope := currentScope;
  564. currentScope := scope;
  565. procedureType := x.type(SyntaxTree.ProcedureType);
  566. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  567. implementationVisitor.temporaries.Init;
  568. implementationVisitor.usedRegisters := NIL;
  569. implementationVisitor.registerUsageCount.Init;
  570. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  571. IF (scope.body # NIL) & (x.isInline) THEN
  572. inline := TRUE;
  573. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  574. ir.SetExported(IsExported(x));
  575. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  576. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  577. IF backend.cellsAreObjects THEN
  578. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  579. ir.SetExported(IsExported(x));
  580. ELSE
  581. RETURN; (* cellnet cannot be compiled for final static hardware *)
  582. END;
  583. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  584. inline := FALSE;
  585. AddBodyCallStub(x);
  586. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  587. ir.SetExported(IsExported(x));
  588. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  589. inline := FALSE;
  590. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  591. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := DefaultDataMemorySize END;
  592. AddBodyCallStub(x);
  593. AddStackAllocation(x,stackSize);
  594. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  595. ir.SetExported(IsExported(x));
  596. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  597. inline := FALSE;
  598. Parameters(procedureType.firstParameter);
  599. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  600. ir.SetExported(IsExported(x));
  601. ELSE
  602. inline := FALSE;
  603. IF x.isEntry OR x.isExit THEN
  604. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  605. ir.SetExported(TRUE);
  606. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  607. ELSE
  608. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  609. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  610. END;
  611. END;
  612. cc := procedureType.callingConvention;
  613. IF cc = SyntaxTree.WinAPICallingConvention THEN
  614. parametersSize := ProcedureParametersSize(backend.system,x);
  615. ELSE
  616. parametersSize := 0;
  617. END;
  618. IF scope.body # NIL THEN
  619. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  620. registerNumber := 0;
  621. IF ~inline THEN
  622. IF scope.lastVariable = NIL THEN
  623. stackSize := 0
  624. ELSE
  625. stackSize := scope.lastVariable.offsetInBits;
  626. IF stackSize <0 THEN stackSize := -stackSize END;
  627. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  628. END;
  629. (*
  630. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  631. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  632. *)
  633. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  634. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  635. END;
  636. pc := ir.pc-1;
  637. (*
  638. ir.Emit(Nop(position)); (* placeholder for fill *)
  639. *)
  640. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  641. registerParameter := backend.GetParameterRegisters(procedureType.callingConvention);
  642. IF registerParameter = NIL THEN registerParameters := 0
  643. ELSE registerParameters := LEN(registerParameter)
  644. END;
  645. formalParameter := procedureType.lastParameter;
  646. WHILE (formalParameter # NIL) & (registerNumber < registerParameters) DO
  647. IF ~PassInRegister(formalParameter) THEN
  648. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  649. ELSE
  650. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, registerParameter[registerNumber]);
  651. type := GetType(system, formalParameter.type);
  652. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  653. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  654. ir.Emit(Mov(Basic.invalidPosition,dest, src));
  655. implementationVisitor.ReleaseIntermediateOperand(src);
  656. INC(registerNumber);
  657. formalParameter := formalParameter.prevParameter;
  658. END;
  659. END;
  660. END;
  661. END;
  662. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  663. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  664. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  665. IF scope.lastVariable # NIL THEN
  666. stackSize := scope.lastVariable.offsetInBits;
  667. IF stackSize <0 THEN stackSize := -stackSize END;
  668. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  669. END;
  670. END;
  671. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  672. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  673. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  674. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  675. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  676. ir.EmitAt(pc,Push(size));
  677. implementationVisitor.StaticCallOperand(result,procedure);
  678. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  679. END;
  680. *)
  681. END;
  682. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  683. IF stackSize > 0 THEN
  684. IF (stackSize MOD system.addressSize = 0) THEN
  685. null := IntermediateCode.Immediate(addressType,0);
  686. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  687. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  688. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  689. ELSE
  690. null := IntermediateCode.Immediate(int8,0);
  691. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  692. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  693. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  694. END;
  695. (*! should potentially be called by backend -- enter might initialize
  696. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  697. *)
  698. END;
  699. IF procedureType.callingConvention = SyntaxTree.WinAPICallingConvention THEN
  700. parametersSize := ProcedureParametersSize(backend.system,x);
  701. ELSE
  702. parametersSize := 0;
  703. END;
  704. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  705. finalizer := FALSE;
  706. IF backend.cooperative & x.isFinalizer THEN
  707. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  708. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  709. GetRecordTypeName(recordType,name);
  710. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  711. END;
  712. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  713. IF backend.cooperative THEN
  714. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  715. IF implementationVisitor.profile & ~isModuleBody THEN
  716. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  717. END;
  718. END;
  719. implementationVisitor.EmitLeave(ir, x.position,x,cc);
  720. IF finalizer THEN
  721. IF backend.hasLinkRegister THEN
  722. ir.Emit(Pop(Basic.invalidPosition, implementationVisitor.lr));
  723. END;
  724. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  725. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  726. ir.Emit(Br(x.position,dest));
  727. ELSE
  728. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  729. END;
  730. ELSE
  731. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  732. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  733. END;
  734. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  735. IF backend.cooperative THEN
  736. IF HasPointers (scope) THEN
  737. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  738. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  739. ir.Emit(Result(x.position, res));
  740. ir.Emit(Push(x.position, res));
  741. implementationVisitor.ResetVariables(scope);
  742. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  743. ir.Emit(Pop(x.position, res));
  744. ir.Emit(Return(x.position, res));
  745. ELSE
  746. implementationVisitor.ResetVariables(scope);
  747. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  748. END;
  749. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  750. IF ~SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  751. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  752. ir.Emit(Result(x.position, res));
  753. ir.Emit(Push(x.position, res));
  754. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  755. ir.Emit(Pop(x.position, res));
  756. ir.Emit(Return(x.position, res));
  757. ELSE
  758. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  759. END;
  760. END;
  761. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  762. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  763. ELSE
  764. ir.Emit(Nop(x.position));
  765. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  766. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  767. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  768. END;
  769. END;
  770. END
  771. END;
  772. ELSE (* force body for procedures *)
  773. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  774. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  775. (*IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;*)
  776. implementationVisitor.EmitLeave(ir,x.position,x,cc);
  777. ir.Emit(Exit(x.position,procedureType.pcOffset,cc, parametersSize));
  778. END;
  779. Scope(scope);
  780. Signature;
  781. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  782. currentScope := prevScope;
  783. END Procedure;
  784. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  785. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  786. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  787. BEGIN
  788. ASSERT (bodyProcedure # NIL);
  789. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  790. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  791. procedure.SetScope(bodyProcedure.scope);
  792. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,bodyProcedure.scope));
  793. procedure.SetAccess(SyntaxTree.Hidden);
  794. Global.GetSymbolSegmentedName (procedure,name);
  795. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  796. ir.SetExported(TRUE);
  797. ir.SetPriority(InitPriority);
  798. Global.GetSymbolSegmentedName (bodyProcedure,name);
  799. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  800. implementationVisitor.currentScope := module.module.moduleScope;
  801. implementationVisitor.section := ir;
  802. implementationVisitor.PushSelfPointer();
  803. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  804. ELSIF backend.preregisterStatic THEN
  805. implementationVisitor.currentScope := module.module.moduleScope;
  806. implementationVisitor.section := ir;
  807. implementationVisitor.PushSelfPointer();
  808. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  809. ELSE
  810. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  811. ir.Emit(Call(bodyProcedure.position,op, 0));
  812. END;
  813. END AddBodyCallStub;
  814. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  815. VAR name: Basic.SegmentedName;
  816. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  817. BEGIN
  818. Global.GetSymbolSegmentedName (symbol,name);
  819. Basic.RemoveSuffix(name);
  820. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  821. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  822. ir.SetExported(TRUE);
  823. ir.SetPriority(FirstPriority);
  824. IntermediateCode.InitImmediate(op,addressType,initStack);
  825. ir.Emit(Mov(Basic.invalidPosition,implementationVisitor.sp,op));
  826. END AddStackAllocation;
  827. (** entry function to visit a complete module *)
  828. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  829. VAR
  830. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  831. hasDynamicOperatorDeclarations: BOOLEAN;
  832. operator: SyntaxTree.Operator;
  833. import: SyntaxTree.Import;
  834. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  835. BEGIN
  836. type := type.resolved;
  837. IF type IS SyntaxTree.RecordType THEN
  838. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  839. ELSIF (type IS SyntaxTree.ArrayType) THEN
  840. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  841. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  842. END;
  843. ELSIF type IS SyntaxTree.MathArrayType THEN
  844. WITH type: SyntaxTree.MathArrayType DO
  845. IF type.form = SyntaxTree.Open THEN
  846. RETURN TRUE
  847. ELSIF type.form = SyntaxTree.Static THEN
  848. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  849. END;
  850. END;
  851. END;
  852. RETURN FALSE
  853. END TypeNeedsInitialization;
  854. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  855. VAR variable: SyntaxTree.Variable;
  856. BEGIN
  857. variable := scope.firstVariable;
  858. WHILE variable # NIL DO
  859. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  860. IF variable.initializer # NIL THEN RETURN TRUE END;
  861. variable := variable.nextVariable;
  862. END;
  863. RETURN FALSE
  864. END ScopeNeedsInitialization;
  865. BEGIN
  866. ASSERT(x # NIL); ASSERT(module # NIL);
  867. SELF.module := module;
  868. (* add import names to the generated Sections.Module *)
  869. import := x.moduleScope.firstImport;
  870. WHILE import # NIL DO
  871. import.module.GetName(idstr);
  872. module.imports.AddName(idstr);
  873. import := import.nextImport
  874. END;
  875. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  876. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  877. moduleSelf := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@moduleSelf"));
  878. moduleSelf.SetType(system.anyType);
  879. moduleSelf.SetScope(x.moduleScope);
  880. moduleSelf.SetUntraced(TRUE);
  881. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  882. ir.SetExported(TRUE);
  883. IntermediateCode.InitImmediate(op,addressType,0);
  884. ir.Emit(Data(Basic.invalidPosition,op));
  885. END;
  886. implementationVisitor.module := module;
  887. implementationVisitor.moduleScope := x.moduleScope;
  888. implementationVisitor.moduleSelf := moduleSelf;
  889. implementationVisitor.canBeLoaded := TRUE;
  890. meta.SetModule(module);
  891. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  892. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  893. END;
  894. IF backend.profile THEN
  895. EnsureBodyProcedure(x.moduleScope);
  896. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  897. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  898. implementationVisitor.profileId.Emit(Reserve(Basic.invalidPosition,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  899. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  900. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  901. implementationVisitor.EmitEnter(implementationVisitor.profileInit,Basic.invalidPosition,NIL,0,0,0);
  902. Global.GetModuleName(module.module,idstr);
  903. implementationVisitor.ProfilerAddModule(idstr);
  904. implementationVisitor.numberProcedures := 0;
  905. END;
  906. implementationVisitor.profile := backend.profile;
  907. (* check if there is at least one dynamic operator locally defined *)
  908. hasDynamicOperatorDeclarations := FALSE;
  909. operator := x.moduleScope.firstOperator;
  910. WHILE operator # NIL DO
  911. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  912. operator := operator.nextOperator
  913. END;
  914. (* add operator initialization code section *)
  915. IF hasDynamicOperatorDeclarations THEN
  916. EnsureBodyProcedure(x.moduleScope);
  917. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  918. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  919. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0,0,0);
  920. END;
  921. Scope(x.moduleScope);
  922. IF hasDynamicOperatorDeclarations THEN
  923. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,Basic.invalidPosition,NIL,0);
  924. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(Basic.invalidPosition,0,0,0));
  925. END;
  926. IF backend.profile THEN
  927. implementationVisitor.ProfilerPatchInit;
  928. END;
  929. END Module;
  930. END DeclarationVisitor;
  931. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  932. RegisterUsageCount*=OBJECT
  933. VAR used: UsedArray; count: LONGINT;
  934. PROCEDURE &Init;
  935. VAR i: LONGINT;
  936. BEGIN
  937. count := 0;
  938. IF used = NIL THEN NEW(used,64); END;
  939. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  940. END Init;
  941. PROCEDURE Grow;
  942. VAR new: UsedArray; size,i: LONGINT;
  943. BEGIN
  944. size := LEN(used)*2;
  945. NEW(new,size);
  946. FOR i := 0 TO LEN(used)-1 DO
  947. new[i].count := used[i].count;
  948. new[i].type := used[i].type;
  949. new[i].map := used[i].map
  950. END;
  951. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  952. used := new
  953. END Grow;
  954. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  955. BEGIN
  956. INC(count);
  957. IF count = LEN(used) THEN Grow END;
  958. used[count].type := type;
  959. used[count].class := class;
  960. used[count].map := count;
  961. RETURN count;
  962. END Next;
  963. PROCEDURE IncUse(register: LONGINT);
  964. BEGIN
  965. INC(used[register].count);
  966. (*
  967. IF (register = 1) & (count > 30) THEN
  968. D.TraceBack;
  969. END;
  970. *)
  971. END IncUse;
  972. PROCEDURE DecUse(register: LONGINT);
  973. BEGIN
  974. DEC(used[register].count);
  975. END DecUse;
  976. PROCEDURE Map(register: LONGINT): LONGINT;
  977. VAR map : LONGINT;
  978. BEGIN
  979. IF register > 0 THEN
  980. map := used[register].map;
  981. WHILE register # map DO register := map; map := used[register].map END;
  982. END;
  983. RETURN register
  984. END Map;
  985. PROCEDURE Use(register: LONGINT): LONGINT;
  986. BEGIN
  987. IF register< LEN(used) THEN
  988. RETURN used[register].count
  989. ELSE
  990. RETURN 0
  991. END
  992. END Use;
  993. END RegisterUsageCount;
  994. RegisterEntry = POINTER TO RECORD
  995. prev,next: RegisterEntry;
  996. register: LONGINT;
  997. registerClass: IntermediateCode.RegisterClass;
  998. type: IntermediateCode.Type;
  999. END;
  1000. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  1001. Variables = OBJECT (Basic.List)
  1002. VAR
  1003. inUse: VariableUse;
  1004. registerIndex: LONGINT;
  1005. PROCEDURE & Init;
  1006. VAR i: LONGINT;
  1007. BEGIN
  1008. InitList(16);
  1009. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  1010. registerIndex := 1024;
  1011. END Init;
  1012. PROCEDURE GetUsage(VAR use: VariableUse);
  1013. BEGIN
  1014. use := inUse;
  1015. END GetUsage;
  1016. PROCEDURE SetUsage(CONST use: VariableUse);
  1017. BEGIN
  1018. inUse := use;
  1019. END SetUsage;
  1020. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  1021. VAR any: ANY;
  1022. BEGIN
  1023. any := Get(i);;
  1024. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  1025. END GetVariable;
  1026. PROCEDURE Occupy(pos: LONGINT);
  1027. BEGIN
  1028. INCL(inUse[pos DIV 32], pos MOD 32);
  1029. END Occupy;
  1030. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  1031. BEGIN
  1032. Occupy(Length());
  1033. Add(v);
  1034. END AddVariable;
  1035. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  1036. VAR var : SyntaxTree.Variable;
  1037. BEGIN
  1038. FOR pos := 0 TO Length()-1 DO
  1039. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  1040. var := GetVariable(pos);
  1041. IF type.SameType(var.type) THEN
  1042. Occupy(pos); RETURN var
  1043. END;
  1044. END;
  1045. END;
  1046. pos := Length();
  1047. RETURN NIL
  1048. END GetFreeVariable;
  1049. END Variables;
  1050. SymbolMap = POINTER TO RECORD
  1051. this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap;
  1052. isAddress: BOOLEAN;
  1053. END;
  1054. SymbolMapper = OBJECT
  1055. VAR
  1056. first: SymbolMap;
  1057. PROCEDURE & Init;
  1058. BEGIN
  1059. first := NIL;
  1060. END Init;
  1061. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; isAddress: BOOLEAN);
  1062. VAR new: SymbolMap;
  1063. BEGIN
  1064. NEW(new); new.this := this; new.to := to; new.tag := tag; new.isAddress := isAddress;
  1065. new.next := first; first := new;
  1066. END Add;
  1067. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  1068. VAR s: SymbolMap;
  1069. BEGIN
  1070. s := first;
  1071. WHILE (s # NIL) & (s.this # this) DO
  1072. s := s.next
  1073. END;
  1074. RETURN s
  1075. END Get;
  1076. END SymbolMapper;
  1077. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  1078. VAR
  1079. system: Global.System;
  1080. section: IntermediateCode.Section;
  1081. module: Sections.Module;
  1082. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  1083. awaitProcCounter, labelId, constId, caseId: LONGINT;
  1084. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  1085. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  1086. checker: SemanticChecker.Checker;
  1087. backend: IntermediateBackend;
  1088. meta: MetaDataGenerator;
  1089. position: Position;
  1090. moduleSelf: SyntaxTree.Variable;
  1091. (* variables for hand over of variables / temporary state *)
  1092. currentScope: SyntaxTree.Scope;
  1093. constantDeclaration : SyntaxTree.Symbol;
  1094. result: Operand; (* result of the most recent expression / statement *)
  1095. destination: IntermediateCode.Operand;
  1096. arrayDestinationTag: IntermediateCode.Operand;
  1097. arrayDestinationDimension:LONGINT;
  1098. currentLoop: Label; (* variable to hand over loop exit jump list *)
  1099. conditional: BOOLEAN;
  1100. trueLabel, falseLabel, exitLabel: Label;
  1101. locked: BOOLEAN;
  1102. (*
  1103. usedRegisters: Registers;
  1104. *)
  1105. registerUsageCount: RegisterUsageCount;
  1106. usedRegisters: RegisterEntry;
  1107. (* useful operands and types *)
  1108. nil,one,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  1109. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  1110. commentPrintout: Printout.Printer;
  1111. dump: Streams.Writer;
  1112. tagsAvailable : BOOLEAN;
  1113. supportedInstruction: SupportedInstructionProcedure;
  1114. supportedImmediate: SupportedImmediateProcedure;
  1115. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  1116. emitLabels: BOOLEAN;
  1117. runtimeModuleName : SyntaxTree.IdentifierString;
  1118. newObjectFile: BOOLEAN;
  1119. indexCounter: LONGINT;
  1120. profile: BOOLEAN;
  1121. profileId, profileInit: IntermediateCode.Section;
  1122. profileInitPatchPosition: LONGINT;
  1123. numberProcedures: LONGINT;
  1124. procedureResultDesignator : SyntaxTree.Designator;
  1125. operatorInitializationCodeSection: IntermediateCode.Section;
  1126. fingerPrinter: FingerPrinter.FingerPrinter;
  1127. temporaries: Variables;
  1128. canBeLoaded : BOOLEAN;
  1129. currentIsInline: BOOLEAN;
  1130. currentMapper: SymbolMapper;
  1131. currentInlineExit: Label;
  1132. moduleBodySection: IntermediateCode.Section;
  1133. NeedDescriptor : BOOLEAN;
  1134. cooperativeSwitches: BOOLEAN;
  1135. lastSwitchPC: LONGINT;
  1136. isUnchecked: BOOLEAN;
  1137. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1138. newObjectFile: BOOLEAN);
  1139. BEGIN
  1140. SELF.system := system;
  1141. SELF.runtimeModuleName := runtime;
  1142. currentScope := NIL;
  1143. hiddenPointerType := NIL;
  1144. delegatePointerType := NIL;
  1145. awaitProcCounter := 0;
  1146. labelId := 0; constId := 0; labelId := 0;
  1147. SELF.checker := checker;
  1148. SELF.backend := backend;
  1149. position := Basic.invalidPosition;
  1150. conditional := FALSE;
  1151. locked := FALSE;
  1152. InitOperand(result,ModeUndefined);
  1153. addressType := IntermediateCode.GetType(system,system.addressType);
  1154. setType := IntermediateCode.GetType(system,system.setType);
  1155. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1156. byteType := IntermediateCode.GetType(system, system.byteType);
  1157. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1158. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1159. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1160. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1161. nil := IntermediateCode.Immediate(addressType,0);
  1162. one := IntermediateCode.Immediate(addressType,1);
  1163. IntermediateCode.InitOperand(destination);
  1164. tagsAvailable := TRUE;
  1165. supportedInstruction := supportedInstructionProcedure;
  1166. supportedImmediate := supportedImmediateProcedure;
  1167. inData := FALSE;
  1168. SELF.emitLabels := emitLabels;
  1169. IntermediateCode.InitOperand(arrayDestinationTag);
  1170. bool := IntermediateCode.GetType(system,system.booleanType);
  1171. IntermediateCode.InitImmediate(false,bool,0);
  1172. IntermediateCode.InitImmediate(true,bool,1);
  1173. SELF.newObjectFile := newObjectFile;
  1174. indexCounter := 0;
  1175. NEW(registerUsageCount);
  1176. usedRegisters := NIL;
  1177. procedureResultDesignator := NIL;
  1178. NEW(fingerPrinter);
  1179. NEW(temporaries);
  1180. currentIsInline := FALSE;
  1181. NeedDescriptor := FALSE;
  1182. isUnchecked := backend.noRuntimeChecks;
  1183. END Init;
  1184. TYPE Context = RECORD
  1185. section: IntermediateCode.Section;
  1186. registerUsageCount: RegisterUsageCount;
  1187. usedRegisters: RegisterEntry;
  1188. END;
  1189. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1190. VAR context: Context;
  1191. BEGIN
  1192. context.section := section;
  1193. context.registerUsageCount := registerUsageCount;
  1194. context.usedRegisters := usedRegisters;
  1195. section := new;
  1196. NEW(registerUsageCount);
  1197. usedRegisters := NIL;
  1198. RETURN context;
  1199. END SwitchContext;
  1200. PROCEDURE ReturnToContext(context: Context);
  1201. BEGIN
  1202. section := context.section;
  1203. registerUsageCount := context.registerUsageCount;
  1204. usedRegisters := context.usedRegisters;
  1205. END ReturnToContext;
  1206. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1207. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1208. BEGIN
  1209. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1210. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1211. END;
  1212. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1213. section.SetExported(IsExported(syntaxTreeSymbol));
  1214. RETURN section
  1215. END NewSection;
  1216. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1217. VAR new: LONGINT;
  1218. BEGIN
  1219. new := registerUsageCount.Next(type,class);
  1220. UseRegister(new);
  1221. RETURN new
  1222. END AcquireRegister;
  1223. PROCEDURE GetFingerprintString(symbol: SyntaxTree.Symbol; VAR string: ARRAY OF CHAR);
  1224. VAR
  1225. fingerPrint: SyntaxTree.FingerPrint;
  1226. fingerPrintString: ARRAY 32 OF CHAR;
  1227. BEGIN
  1228. fingerPrint := fingerPrinter.SymbolFP(symbol);
  1229. string := "[";
  1230. Strings.IntToHexStr(fingerPrint.shallow, 8, fingerPrintString);
  1231. Strings.Append(string, fingerPrintString);
  1232. Strings.Append(string, "]");
  1233. END GetFingerprintString;
  1234. (** get the name for the code section that represens a certain symbol
  1235. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1236. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1237. VAR string: ARRAY 32 OF CHAR;
  1238. BEGIN
  1239. Global.GetSymbolSegmentedName(symbol, name);
  1240. (* if the symbol is an operator, then append the fingerprint to the name *)
  1241. IF symbol IS SyntaxTree.Operator THEN
  1242. GetFingerprintString(symbol, string);
  1243. Basic.AppendToSegmentedName(name,string);
  1244. END
  1245. END GetCodeSectionNameForSymbol;
  1246. (** get the name for the code section that represens a certain symbol
  1247. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1248. PROCEDURE GetCodeSectionNameForSymbolInScope(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; VAR name: ARRAY OF CHAR);
  1249. VAR string: ARRAY 32 OF CHAR;
  1250. BEGIN
  1251. Global.GetSymbolNameInScope(symbol, scope, name);
  1252. (* if the symbol is an operator, then append the fingerprint to the name *)
  1253. IF symbol IS SyntaxTree.Operator THEN
  1254. GetFingerprintString(symbol, string);
  1255. Strings.Append(name, string);
  1256. END
  1257. END GetCodeSectionNameForSymbolInScope;
  1258. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1259. BEGIN
  1260. IF dump # NIL THEN
  1261. dump.String("enter "); dump.String(s); dump.Ln;
  1262. END;
  1263. END TraceEnter;
  1264. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1265. BEGIN
  1266. IF dump # NIL THEN
  1267. dump.String("exit "); dump.String(s); dump.Ln;
  1268. END;
  1269. END TraceExit;
  1270. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1271. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1272. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1273. VAR i: LONGINT;
  1274. BEGIN
  1275. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1276. IF op.rule # NIL THEN
  1277. FOR i := 0 TO LEN(op.rule)-1 DO
  1278. CheckRegister(op.rule[i])
  1279. END;
  1280. END;
  1281. END CheckRegister;
  1282. BEGIN
  1283. CheckRegister(instruction.op1);
  1284. CheckRegister(instruction.op2);
  1285. CheckRegister(instruction.op3);
  1286. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1287. ELSE section.Emit(instruction);
  1288. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1289. END;
  1290. END Emit;
  1291. PROCEDURE EmitTrap (position: Position; trapNo: LONGINT);
  1292. BEGIN
  1293. IF backend.cooperative THEN
  1294. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1295. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1296. ELSE
  1297. Emit(Trap(position,trapNo));
  1298. END;
  1299. END EmitTrap;
  1300. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: Position; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1301. VAR name: Basic.SegmentedName;
  1302. VAR op1, op2, reg: IntermediateCode.Operand;
  1303. VAR call, nocall: Label;
  1304. VAR parametersSize: LONGINT;
  1305. VAR prevSection: IntermediateCode.Section;
  1306. VAR prevDump: Streams.Writer;
  1307. VAR body: SyntaxTree.Body;
  1308. VAR procedureType: SyntaxTree.ProcedureType;
  1309. BEGIN
  1310. IF procedure # NIL THEN
  1311. procedureType := procedure.type(SyntaxTree.ProcedureType);
  1312. ELSE procedureType := NIL;
  1313. END;
  1314. ASSERT((procedure = NIL) OR ~procedureType.noPAF);
  1315. prevSection := SELF.section;
  1316. SELF.section := section;
  1317. prevDump := dump;
  1318. dump := section.comments;
  1319. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1320. IF backend.hasLinkRegister THEN
  1321. Emit(Push(Basic.invalidPosition, lr));
  1322. END;
  1323. Emit(Push(Basic.invalidPosition,fp));
  1324. END;
  1325. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1326. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1327. GetCodeSectionNameForSymbol(procedure, name);
  1328. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1329. ELSE
  1330. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1331. END;
  1332. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1333. Emit(Push(Basic.invalidPosition,op1));
  1334. Emit(Mov(Basic.invalidPosition,fp, sp));
  1335. body := procedure.procedureScope.body;
  1336. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1337. NEW(call, section);
  1338. NEW(nocall, section);
  1339. reg := NewRegisterOperand(addressType);
  1340. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1341. Emit(Sub(Basic.invalidPosition,reg, sp, op1));
  1342. BrltL(call, sp, reg);
  1343. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1344. BrgeL(nocall, sp, op2);
  1345. call.Resolve(section.pc);
  1346. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1347. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1348. Emit(Push(Basic.invalidPosition, op2));
  1349. Emit(Push(Basic.invalidPosition, reg));
  1350. ReleaseIntermediateOperand(reg);
  1351. CallThis(position, "Activities","ExpandStack",2);
  1352. Emit(Result(Basic.invalidPosition, sp));
  1353. nocall.Resolve(section.pc);
  1354. END;
  1355. ELSIF callconv # SyntaxTree.InterruptCallingConvention THEN
  1356. IF procedure # NIL THEN
  1357. body := procedure.procedureScope.body;
  1358. ELSE
  1359. body := NIL;
  1360. END;
  1361. IF backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1362. Emit(Push(Basic.invalidPosition, one)) ;
  1363. procedureType.SetParametersOffset(1);
  1364. ASSERT(system.GenerateParameterOffsets(procedure, procedure.level > 0));
  1365. END;
  1366. Emit(Mov(Basic.invalidPosition, fp, sp));
  1367. END;
  1368. Emit(Enter(Basic.invalidPosition, callconv, varSize));
  1369. SELF.section := prevSection;
  1370. dump := prevDump;
  1371. END EmitEnter;
  1372. PROCEDURE Enter(position: Position; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1373. VAR op1,op2: IntermediateCode.Operand;
  1374. VAR instruction: IntermediateCode.Instruction;
  1375. BEGIN
  1376. IntermediateCode.InitNumber(op1,callconv);
  1377. IntermediateCode.InitNumber(op2,varSize);
  1378. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1379. RETURN instruction
  1380. END Enter;
  1381. PROCEDURE Leave(position: Position; callconv: LONGINT): IntermediateCode.Instruction;
  1382. VAR op1: IntermediateCode.Operand;
  1383. VAR instruction: IntermediateCode.Instruction;
  1384. BEGIN
  1385. IntermediateCode.InitNumber(op1,callconv);
  1386. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1387. RETURN instruction
  1388. END Leave;
  1389. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: Basic.Position; procedure: SyntaxTree.Procedure; callconv: LONGINT);
  1390. VAR prevSection: IntermediateCode.Section;
  1391. VAR op2, size: IntermediateCode.Operand;
  1392. VAR body: SyntaxTree.Body;
  1393. BEGIN
  1394. prevSection := SELF.section;
  1395. SELF.section := section;
  1396. Emit(Leave(position, callconv));
  1397. IF procedure # NIL THEN
  1398. body := procedure.procedureScope.body;
  1399. ELSE
  1400. body := NIL;
  1401. END;
  1402. IF callconv # SyntaxTree.InterruptCallingConvention THEN
  1403. IF backend.cooperative OR backend.preciseGC & (body # NIL) & (body.code = NIL) THEN
  1404. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1405. Emit(Add(position, sp, fp, op2));
  1406. ELSE
  1407. Emit(Mov(position, sp, fp));
  1408. END;
  1409. Emit(Pop(position, fp));
  1410. END;
  1411. SELF.section := prevSection;
  1412. END EmitLeave;
  1413. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1414. VAR m: SymbolMap;
  1415. BEGIN
  1416. position := x.position;
  1417. IF currentIsInline THEN
  1418. m := currentMapper.Get(x);
  1419. IF m # NIL THEN
  1420. (*
  1421. Printout.Info("mapping from", x);
  1422. Printout.Info("mapping to ", m.to);
  1423. *)
  1424. m.to.Accept(SELF);
  1425. op := result;
  1426. IF m.tag # NIL THEN
  1427. ReleaseIntermediateOperand(result.tag);
  1428. m.tag.Accept(SELF);
  1429. op.tag := result.op;
  1430. ReleaseIntermediateOperand(result.tag);
  1431. END;
  1432. RETURN
  1433. END;
  1434. END;
  1435. x.Accept(SELF);
  1436. op := result;
  1437. END Symbol;
  1438. PROCEDURE Expression(x: SyntaxTree.Expression);
  1439. BEGIN
  1440. position := x.position;
  1441. constantDeclaration := NIL;
  1442. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1443. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1444. END;
  1445. IF x.resolved # NIL THEN
  1446. x.resolved.Accept(SELF)
  1447. ELSE
  1448. x.Accept(SELF)
  1449. END;
  1450. (* check this, was commented out in ActiveCells3 *)
  1451. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1452. Error(x.position, "unsupported modifier");
  1453. END;
  1454. END Expression;
  1455. (*
  1456. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1457. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1458. BEGIN
  1459. temporaries.GetUsage(current);
  1460. FOR i := 0 TO LEN(current)-1 DO
  1461. set := current[i] - previous[i];
  1462. IF set # {} THEN
  1463. FOR j := 0 TO MAX(SET)-1 DO
  1464. IF j IN set THEN
  1465. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1466. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1467. Symbol(variable, op);
  1468. MakeMemory(tmp,op.op,addressType,0);
  1469. ReleaseOperand(op);
  1470. Emit(Mov(position,tmp, nil ) );
  1471. ReleaseIntermediateOperand(tmp);
  1472. END;
  1473. END;
  1474. END;
  1475. END;
  1476. END;
  1477. END ResetUsedTemporaries;
  1478. *)
  1479. PROCEDURE Statement(x: SyntaxTree.Statement);
  1480. VAR use: VariableUse;
  1481. BEGIN
  1482. temporaries.GetUsage(use);
  1483. position := x.position;
  1484. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1485. IF commentPrintout # NIL THEN
  1486. commentPrintout.Statement(x);
  1487. dump.Ln;
  1488. (*dump.Update;*)
  1489. END;
  1490. x.Accept(SELF);
  1491. (*
  1492. CheckRegistersFree();
  1493. *)
  1494. (*ResetUsedTemporaries(use);*)
  1495. temporaries.SetUsage(use);
  1496. END Statement;
  1497. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1498. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1499. *)
  1500. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1501. BEGIN
  1502. ASSERT(op.mode # IntermediateCode.Undefined);
  1503. IF op.mode = IntermediateCode.ModeMemory THEN
  1504. ReuseCopy(res,op);
  1505. ELSE
  1506. res := op;
  1507. UseIntermediateOperand(res);
  1508. END;
  1509. IntermediateCode.AddOffset(res,offset);
  1510. IntermediateCode.MakeMemory(res,type);
  1511. END MakeMemory;
  1512. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1513. VAR mem: IntermediateCode.Operand;
  1514. BEGIN
  1515. MakeMemory(mem,res,type,offset);
  1516. ReleaseIntermediateOperand(res);
  1517. res := mem;
  1518. END ToMemory;
  1519. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1520. VAR mem: IntermediateCode.Operand;
  1521. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1522. componentType: SyntaxTree.Type;
  1523. BEGIN
  1524. type := type.resolved;
  1525. IF operand.mode = ModeReference THEN
  1526. IF type IS SyntaxTree.RangeType THEN
  1527. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1528. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1529. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1530. ReleaseOperand(operand);
  1531. operand.op := firstOp;
  1532. operand.tag := lastOp;
  1533. operand.extra := stepOp;
  1534. ELSIF type IS SyntaxTree.ComplexType THEN
  1535. componentType := type(SyntaxTree.ComplexType).componentType;
  1536. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1537. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1538. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1539. ReleaseOperand(operand);
  1540. operand.op := firstOp;
  1541. operand.tag := lastOp
  1542. ELSE
  1543. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1544. ReleaseIntermediateOperand(operand.op);
  1545. operand.op := mem;
  1546. END;
  1547. operand.mode := ModeValue;
  1548. END;
  1549. ASSERT(operand.mode = ModeValue);
  1550. END LoadValue;
  1551. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1552. VAR prevConditional: BOOLEAN;
  1553. BEGIN
  1554. prevConditional := conditional;
  1555. conditional := FALSE;
  1556. InitOperand(result, ModeUndefined);
  1557. Expression(x);
  1558. op := result;
  1559. LoadValue(op,x.type.resolved);
  1560. conditional := prevConditional;
  1561. END Evaluate;
  1562. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1563. VAR prevConditional: BOOLEAN;
  1564. BEGIN
  1565. prevConditional := conditional;
  1566. conditional := FALSE;
  1567. InitOperand(result,ModeUndefined);
  1568. Expression(x);
  1569. op := result;
  1570. (*
  1571. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1572. *)
  1573. conditional := prevConditional;
  1574. END Designate;
  1575. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1576. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1577. BEGIN
  1578. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1579. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1580. conditional := TRUE;
  1581. trueLabel := trueL; falseLabel := falseL;
  1582. Expression(x);
  1583. trueL := trueLabel; falseL := falseLabel;
  1584. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1585. END Condition;
  1586. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1587. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1588. BEGIN
  1589. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1590. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1591. RETURN op
  1592. END NewRegisterOperand;
  1593. PROCEDURE UnuseRegister(register: LONGINT);
  1594. BEGIN
  1595. IF (register > 0) THEN
  1596. register := registerUsageCount.Map(register);
  1597. registerUsageCount.DecUse(register);
  1598. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1599. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1600. END;
  1601. IF registerUsageCount.Use(register)=0 THEN
  1602. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1603. Warning(position, "register cannot be removed");
  1604. END;
  1605. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1606. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1607. END;
  1608. ELSIF registerUsageCount.Use(register)<0 THEN
  1609. Warning(position, "register removed too often");
  1610. IF dump # NIL THEN
  1611. dump.String("register removed too often"); dump.Ln; dump.Update;
  1612. END;
  1613. END;
  1614. END;
  1615. END UnuseRegister;
  1616. PROCEDURE UseRegister(register: LONGINT);
  1617. BEGIN
  1618. IF (register > 0) THEN
  1619. register := registerUsageCount.Map(register);
  1620. registerUsageCount.IncUse(register);
  1621. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1622. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1623. END;
  1624. IF registerUsageCount.Use(register)=1 THEN
  1625. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1626. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1627. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1628. END;
  1629. END;
  1630. END;
  1631. END UseRegister;
  1632. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1633. BEGIN
  1634. UnuseRegister(op.register)
  1635. END ReleaseIntermediateOperand;
  1636. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1637. BEGIN
  1638. UseRegister(op.register)
  1639. END UseIntermediateOperand;
  1640. PROCEDURE ReleaseOperand(CONST op: Operand);
  1641. BEGIN
  1642. UnuseRegister(op.op.register);
  1643. UnuseRegister(op.tag.register);
  1644. UnuseRegister(op.extra.register);
  1645. END ReleaseOperand;
  1646. (* save registers marked in array "markedRegisters" to the stack
  1647. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1648. *)
  1649. PROCEDURE SaveRegisters();
  1650. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1651. BEGIN
  1652. entry := usedRegisters;
  1653. WHILE entry # NIL DO
  1654. type := registerUsageCount.used[entry.register].type;
  1655. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1656. Emit(Push(position,op));
  1657. entry := entry.next;
  1658. END;
  1659. END SaveRegisters;
  1660. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1661. BEGIN
  1662. saved := usedRegisters;
  1663. usedRegisters := NIL;
  1664. END ReleaseUsedRegisters;
  1665. (** remove parameter registers from used queue *)
  1666. PROCEDURE ReleaseParameterRegisters;
  1667. VAR entry,prev,next: RegisterEntry;
  1668. BEGIN
  1669. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1670. WHILE entry # NIL DO
  1671. next := entry.next;
  1672. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1673. registerUsageCount.DecUse(entry.register);
  1674. ASSERT(registerUsageCount.Use(entry.register)=0);
  1675. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1676. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1677. END;
  1678. ELSIF prev = NIL THEN
  1679. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1680. ELSE
  1681. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1682. END;
  1683. entry := next;
  1684. END;
  1685. END ReleaseParameterRegisters;
  1686. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1687. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1688. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1689. BEGIN
  1690. entry := saved;
  1691. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1692. entry := prev;
  1693. WHILE entry # NIL DO
  1694. prev := entry.prev;
  1695. type := entry.type;
  1696. class := entry.registerClass;
  1697. IntermediateCode.InitRegister(op,type,class,entry.register);
  1698. (*
  1699. new := registerUsageCount.Next(type,class);
  1700. registerUsageCount.Remap(entry.register,new);
  1701. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1702. dump.String("remap register "); dump.Int(entry.register,1);
  1703. dump.String("to "); dump.Int(new,1);
  1704. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1705. END;
  1706. entry.register := new;
  1707. *)
  1708. Emit(Pop(position,op));
  1709. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1710. entry := prev;
  1711. END;
  1712. (*
  1713. usedRegisters := saved;
  1714. *)
  1715. END RestoreRegisters;
  1716. PROCEDURE CheckRegistersFree;
  1717. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1718. BEGIN
  1719. IF usedRegisters # NIL THEN
  1720. r := usedRegisters;
  1721. WHILE r # NIL DO
  1722. warning := "register ";
  1723. Strings.AppendInt(warning, r.register);
  1724. Strings.Append(warning, " not released.");
  1725. Warning(position,warning);
  1726. r := r .next;
  1727. END;
  1728. END;
  1729. FOR i := 0 TO registerUsageCount.count-1 DO
  1730. IF registerUsageCount.used[i].count < 0 THEN
  1731. warning := "register ";
  1732. Strings.AppendInt(warning, i);
  1733. Strings.Append(warning, " unused too often.");
  1734. Warning(position,warning);
  1735. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1736. warning := "register ";
  1737. Strings.AppendInt(warning, i);
  1738. Strings.Append(warning, " not unused often enough.");
  1739. Warning(position,warning);
  1740. END;
  1741. END;
  1742. END CheckRegistersFree;
  1743. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1744. Otherwise allocate a new register.
  1745. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1746. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1747. BEGIN
  1748. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1749. UseIntermediateOperand(result);
  1750. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1751. UseIntermediateOperand(result);
  1752. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1753. END;
  1754. END Reuse2;
  1755. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1756. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1757. If not then allocate a new register.
  1758. Does NOT necessarily keep the content of src1 or src2 in result!
  1759. *)
  1760. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1761. BEGIN
  1762. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1763. UseIntermediateOperand(result);
  1764. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1765. UseIntermediateOperand(result);
  1766. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1767. result := alternative; alternative := emptyOperand;
  1768. UseIntermediateOperand(result);
  1769. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1770. END;
  1771. END Reuse2a;
  1772. (* like reuse2 but only one source *)
  1773. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1774. BEGIN
  1775. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1776. UseIntermediateOperand(result);
  1777. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1778. END;
  1779. END Reuse1;
  1780. (* like reuse2a but only one source *)
  1781. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1782. BEGIN
  1783. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1784. UseIntermediateOperand(result);
  1785. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1786. UseIntermediateOperand(result);
  1787. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1788. END;
  1789. END Reuse1a;
  1790. (* like reuse1 but guarantees that content of src1 is in result *)
  1791. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1792. BEGIN
  1793. IF ReusableRegister(src1) THEN
  1794. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1795. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1796. UseIntermediateOperand(result);
  1797. ELSE
  1798. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1799. Emit(Mov(position,result,src1));
  1800. END
  1801. END ReuseCopy;
  1802. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1803. BEGIN
  1804. IF ReusableRegister(src) THEN
  1805. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1806. ELSE
  1807. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1808. Emit(Mov(position,result,src));
  1809. ReleaseIntermediateOperand(src);
  1810. END
  1811. END TransferToRegister;
  1812. (** labels and branches **)
  1813. PROCEDURE NewLabel(): Label;
  1814. VAR label: Label;
  1815. BEGIN
  1816. NEW(label,section); RETURN label;
  1817. END NewLabel;
  1818. PROCEDURE SetLabel(label: Label);
  1819. BEGIN label.Resolve(section.pc);
  1820. END SetLabel;
  1821. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1822. BEGIN
  1823. ASSERT(label # NIL);
  1824. IF label.pc < 0 THEN (* label not yet set *)
  1825. label.AddFixup(section.pc);
  1826. END;
  1827. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1828. END LabelOperand;
  1829. PROCEDURE BrL(label: Label);
  1830. BEGIN
  1831. Emit(Br(position,LabelOperand(label)));
  1832. END BrL;
  1833. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1834. BEGIN
  1835. Emit(Brge(position,LabelOperand(label),left,right));
  1836. END BrgeL;
  1837. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1838. BEGIN
  1839. Emit(Brlt(position,LabelOperand(label),left,right));
  1840. END BrltL;
  1841. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1842. BEGIN
  1843. Emit(Breq(position,LabelOperand(label),left,right));
  1844. END BreqL;
  1845. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1846. BEGIN
  1847. Emit(Brne(position,LabelOperand(label),left,right));
  1848. END BrneL;
  1849. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1850. VAR new: IntermediateCode.Operand;
  1851. BEGIN
  1852. IF Trace THEN TraceEnter("Convert") END;
  1853. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1854. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1855. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1856. & (operand.offset = 0)
  1857. THEN
  1858. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1859. Emit(Conv(position,new,operand));
  1860. ELSE
  1861. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1862. Emit(Conv(position,new,operand));
  1863. ReleaseIntermediateOperand(operand);
  1864. END;
  1865. operand := new;
  1866. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1867. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1868. ELSE
  1869. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1870. Emit(Conv(position,new,operand));
  1871. ReleaseIntermediateOperand(operand);
  1872. operand := new;
  1873. END;
  1874. IF Trace THEN TraceExit("Convert") END;
  1875. END Convert;
  1876. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1877. VAR exit: Label;
  1878. BEGIN
  1879. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1880. exit := NewLabel();
  1881. br(exit,left,right);
  1882. EmitTrap(position,trapNo);
  1883. SetLabel(exit);
  1884. END TrapC;
  1885. (** expressions *)
  1886. (** emit necessary runtime check for set elements **)
  1887. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1888. VAR max: IntermediateCode.Operand;
  1889. BEGIN
  1890. IF isUnchecked THEN RETURN END;
  1891. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1892. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1893. TrapC(BrgeL, max, o, SetElementTrap);
  1894. END;
  1895. END CheckSetElement;
  1896. (** the set that a range represents **)
  1897. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1898. VAR
  1899. operand: Operand;
  1900. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1901. BEGIN
  1902. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1903. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1904. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1905. one := IntermediateCode.Immediate(setType, 1);
  1906. Evaluate(x, operand);
  1907. Convert(operand.op, setType);
  1908. Convert(operand.tag, setType);
  1909. CheckSetElement(operand.op);
  1910. CheckSetElement(operand.tag);
  1911. (* create mask for lower bound
  1912. i.e. shift 11111111 to the left by the value of the lower bound
  1913. *)
  1914. Reuse1(temp, operand.op);
  1915. Emit(Shl(position,temp, allBits, operand.op));
  1916. ReleaseIntermediateOperand(operand.op);
  1917. operand.op := temp;
  1918. (* create mask for upper bound
  1919. i.e. shift 11111111 to the right by the difference between the
  1920. upper bound and the maximum number of set elements
  1921. *)
  1922. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1923. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1924. Reuse1(temp, operand.tag);
  1925. ELSE
  1926. Reuse1(temp, operand.tag);
  1927. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1928. Emit(Sub(position,temp, size, operand.tag));
  1929. END;
  1930. Emit(Shr(position,temp, allBits, operand.tag));
  1931. ReleaseIntermediateOperand(operand.tag);
  1932. operand.tag := temp;
  1933. Reuse2(resultingSet, operand.op, operand.tag);
  1934. (* intersect the two masks *)
  1935. Emit(And(position,resultingSet, operand.op, operand.tag));
  1936. ReleaseOperand(operand);
  1937. RETURN resultingSet
  1938. END SetFromRange;
  1939. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1940. VAR
  1941. res, operand: Operand;
  1942. temp, one, noBits, dest: IntermediateCode.Operand;
  1943. expression: SyntaxTree.Expression;
  1944. i: LONGINT;
  1945. BEGIN
  1946. IF Trace THEN TraceEnter("VisitSet") END;
  1947. dest := destination;
  1948. destination := emptyOperand;
  1949. noBits := IntermediateCode.Immediate(setType, 0);
  1950. one := IntermediateCode.Immediate(setType, 1);
  1951. (* start off with the empty set *)
  1952. InitOperand(res, ModeValue);
  1953. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1954. Emit(Mov(position,res.op, noBits));
  1955. FOR i := 0 TO x.elements.Length() - 1 DO
  1956. expression := x.elements.GetExpression(i);
  1957. IF expression IS SyntaxTree.RangeExpression THEN
  1958. (* range of set elements *)
  1959. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1960. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1961. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1962. ReleaseIntermediateOperand(temp)
  1963. ELSE
  1964. (* singelton element *)
  1965. Evaluate(expression, operand);
  1966. Convert(operand.op, setType);
  1967. CheckSetElement(operand.op);
  1968. (* create subset containing single element *)
  1969. Reuse1(temp, operand.op);
  1970. Emit(Shl(position,temp, one, operand.op));
  1971. ReleaseOperand(operand);
  1972. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1973. ReleaseIntermediateOperand(temp);
  1974. END
  1975. END;
  1976. result := res;
  1977. destination := dest;
  1978. IF Trace THEN TraceExit("VisitSet") END;
  1979. END VisitSet;
  1980. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1981. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1982. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1983. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1984. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1985. element: SyntaxTree.IntegerValue;
  1986. BEGIN
  1987. numberElements := x.elements.Length();
  1988. expression := index.parameters.GetExpression(dim);
  1989. element := expression(SyntaxTree.IntegerValue);
  1990. FOR i := 0 TO numberElements-1 DO
  1991. expression := x.elements.GetExpression(i);
  1992. element.SetValue(i);
  1993. IF expression IS SyntaxTree.MathArrayExpression THEN
  1994. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1995. ELSE
  1996. Assign(index,expression);
  1997. END;
  1998. END;
  1999. END RecursiveAssignment;
  2000. BEGIN
  2001. variable := GetTemporaryVariable(x.type, FALSE);
  2002. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  2003. designator.SetType(variable.type);
  2004. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  2005. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  2006. FOR i := 0 TO dim-1 DO
  2007. element := SyntaxTree.NewIntegerValue(x.position,0);
  2008. element.SetType(system.longintType);
  2009. index.parameters.AddExpression(element);
  2010. END;
  2011. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  2012. RecursiveAssignment(x,0);
  2013. Expression(designator);
  2014. END VisitMathArrayExpression;
  2015. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  2016. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  2017. BEGIN
  2018. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  2019. dest := destination; destination := emptyOperand;
  2020. IF x.operator = Scanner.Not THEN
  2021. IF conditional THEN
  2022. Condition(x.left,falseLabel,trueLabel)
  2023. ELSE
  2024. Evaluate(x.left,operand);
  2025. InitOperand(result,ModeValue);
  2026. Reuse1a(result.op,operand.op,dest);
  2027. Emit(Xor(position,result.op,operand.op,true));
  2028. ReleaseOperand(operand);
  2029. END;
  2030. ELSIF x.operator = Scanner.Minus THEN
  2031. Evaluate(x.left,operand);
  2032. InitOperand(result,ModeValue);
  2033. Reuse1a(result.op,operand.op,dest);
  2034. type := x.left.type.resolved;
  2035. IF type IS SyntaxTree.SetType THEN
  2036. Emit(Not(position,result.op,operand.op));
  2037. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2038. Reuse1(result.tag,operand.tag);
  2039. Emit(Neg(position,result.op,operand.op)); (* real part *)
  2040. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  2041. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  2042. Emit(Neg(position,result.op,operand.op));
  2043. ELSE HALT(200)
  2044. END;
  2045. ReleaseOperand(operand);
  2046. ELSIF x.operator = Scanner.Address THEN
  2047. Designate(x.left,operand);
  2048. operand.mode := ModeValue;
  2049. t0 := x.left.type.resolved;
  2050. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  2051. ReleaseIntermediateOperand(operand.op);
  2052. operand.op := operand.tag;
  2053. IntermediateCode.InitOperand(operand.tag);
  2054. END;
  2055. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  2056. result := operand;
  2057. ELSE HALT(100)
  2058. END;
  2059. destination := dest;
  2060. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  2061. END VisitUnaryExpression;
  2062. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  2063. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  2064. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  2065. BEGIN
  2066. type := type.resolved;
  2067. originalType := type;
  2068. IF type IS SyntaxTree.PointerType THEN
  2069. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  2070. END;
  2071. IF type IS SyntaxTree.ObjectType THEN
  2072. BrL(trueL);
  2073. ELSE
  2074. ASSERT(type IS SyntaxTree.RecordType);
  2075. (*
  2076. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  2077. *)
  2078. ReuseCopy(left,tag);
  2079. right := TypeDescriptorAdr(type);
  2080. IF ~newObjectFile THEN
  2081. IntermediateCode.MakeMemory(right,addressType);
  2082. END;
  2083. IF backend.cooperative THEN
  2084. repeatL := NewLabel();
  2085. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  2086. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  2087. END;
  2088. SetLabel(repeatL);
  2089. BreqL(trueL,left,right);
  2090. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  2091. BrneL(repeatL,left,nil);
  2092. ELSIF meta.simple THEN
  2093. level := type(SyntaxTree.RecordType).Level();
  2094. (* get type desc tag of level relative to base tag *)
  2095. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  2096. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2097. IntermediateCode.MakeMemory(left,addressType);
  2098. BreqL(trueL,left,right);
  2099. ELSE
  2100. level := type(SyntaxTree.RecordType).Level();
  2101. (* get type desc tag of level relative to base tag *)
  2102. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  2103. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  2104. IntermediateCode.MakeMemory(left,addressType);
  2105. BreqL(trueL,left,right);
  2106. END;
  2107. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  2108. BrL(falseL);
  2109. END;
  2110. END TypeTest;
  2111. PROCEDURE Error(position: Position; CONST s: ARRAY OF CHAR);
  2112. BEGIN
  2113. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  2114. IF dump # NIL THEN
  2115. dump.String(s); dump.Ln;
  2116. END;
  2117. END Error;
  2118. PROCEDURE Warning(position: Position; CONST s: ARRAY OF CHAR);
  2119. BEGIN
  2120. Basic.Warning(backend.diagnostics, module.module.sourceName,position, s);
  2121. IF dump # NIL THEN
  2122. dump.String(s); dump.Ln; dump.Update;
  2123. END;
  2124. END Warning;
  2125. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  2126. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2127. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  2128. operand:Operand;
  2129. BEGIN
  2130. previousSection := section;
  2131. Global.GetModuleName(mod,name);
  2132. Strings.Append(name,".@Trace");
  2133. Basic.ToSegmentedName(name, pooledName);
  2134. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  2135. IF dump # NIL THEN dump := section.comments END;
  2136. IF backend.hasLinkRegister THEN
  2137. Emit(Push(Basic.invalidPosition, lr));
  2138. END;
  2139. variable := mod.moduleScope.firstVariable;
  2140. WHILE variable # NIL DO
  2141. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  2142. Symbol(variable, operand);
  2143. register := operand.op;
  2144. CallTraceMethod(register, variable.type);
  2145. ReleaseIntermediateOperand(register);
  2146. END;
  2147. variable := variable.nextVariable;
  2148. END;
  2149. IF backend.hasLinkRegister THEN
  2150. Emit(Pop(Basic.invalidPosition, lr));
  2151. END;
  2152. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  2153. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  2154. Emit(Br(position,op));
  2155. INC(statCoopTraceModule, section.pc);
  2156. section := previousSection;
  2157. IF dump # NIL THEN dump := section.comments END;
  2158. END CreateTraceModuleMethod;
  2159. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  2160. BEGIN
  2161. Emit (Push(position, dst));
  2162. Emit (Push(position, src));
  2163. CallThis(position,"GarbageCollector","Assign", 2);
  2164. END CallAssignPointer;
  2165. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  2166. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  2167. BEGIN
  2168. IF SemanticChecker.IsPointerType (type) THEN
  2169. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2170. ELSIF type.IsRecordType() THEN
  2171. Emit (Push(position,dst));
  2172. Emit (Push(position,src));
  2173. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2174. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2175. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2176. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2177. ELSIF IsStaticArray(type) THEN
  2178. size := StaticArrayNumElements(type);
  2179. base := StaticArrayBaseType(type);
  2180. IF base.IsRecordType() THEN
  2181. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2182. Emit (Push(position, dst));
  2183. Emit (Push(position, src));
  2184. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2186. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2187. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2188. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2189. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2190. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2191. Emit (Push(position, dst));
  2192. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2193. Emit (Push(position, src));
  2194. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2195. ELSE
  2196. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2197. Emit (Push(position, dst));
  2198. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2199. Emit (Push(position, src));
  2200. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2201. ASSERT(StaticArrayBaseType(type).IsPointer());
  2202. END;
  2203. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2204. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2205. Emit (Push(position, dst));
  2206. Emit (Push(position, src));
  2207. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2208. ELSE HALT(100); (* missing ? *)
  2209. END;
  2210. END CallAssignMethod;
  2211. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2212. VAR name: Basic.SegmentedName;
  2213. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2214. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2215. context: Context;
  2216. BEGIN
  2217. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2218. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2219. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2220. GetRecordTypeName (recordType,name);
  2221. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2222. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. IF backend.hasLinkRegister THEN
  2225. Emit(Push(Basic.invalidPosition, lr));
  2226. END;
  2227. variable := recordType.recordScope.firstVariable;
  2228. WHILE variable # NIL DO
  2229. IF variable.NeedsTrace() THEN
  2230. dst := NewRegisterOperand (addressType);
  2231. src := NewRegisterOperand (addressType);
  2232. Emit (Mov(position, dst, parameter1));
  2233. Emit (Mov(position, src, parameter2));
  2234. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2235. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2236. CallAssignMethod(dst, src, variable.type);
  2237. ReleaseIntermediateOperand(src);
  2238. ReleaseIntermediateOperand(dst);
  2239. END;
  2240. variable := variable.nextVariable;
  2241. END;
  2242. recordBase := recordType.GetBaseRecord();
  2243. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2244. IF backend.hasLinkRegister THEN
  2245. Emit(Pop(Basic.invalidPosition, lr));
  2246. END;
  2247. GetRecordTypeName (recordBase,name);
  2248. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2249. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2250. Emit(Br(position,op));
  2251. ELSE
  2252. Emit(Exit(position,0,0, 0));
  2253. END;
  2254. IF ~recordType.isObject THEN
  2255. GetRecordTypeName (recordType,name);
  2256. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2257. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2258. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2259. NEW(registerUsageCount);
  2260. usedRegisters := NIL;
  2261. dst := NewRegisterOperand (addressType);
  2262. src := NewRegisterOperand (addressType);
  2263. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2264. Emit(Mov(position, dst, parameter1));
  2265. Emit(Mov(position, src, parameter2));
  2266. label := NewLabel();
  2267. SetLabel(label);
  2268. Emit(Push(position, dst));
  2269. Emit(Push(position, src));
  2270. GetRecordTypeName (recordType,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Call(position, op, 0));
  2274. Emit(Pop(position, src));
  2275. Emit(Pop(position, dst));
  2276. Emit(Add(position, dst, dst, ofs));
  2277. Emit(Add(position, src, src, ofs));
  2278. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2279. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2280. Emit(Exit(position,0,0, 0));
  2281. END;
  2282. INC(statCoopAssignProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateAssignProcedure;
  2286. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2287. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2288. BEGIN
  2289. IF IsUnsafePointer (type) THEN
  2290. skip := NewLabel();
  2291. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2292. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2293. BreqL (skip, op, nil);
  2294. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2295. SetLabel (skip);
  2296. ELSIF SemanticChecker.IsPointerType (type) THEN
  2297. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2298. CallThis(position,"GarbageCollector","Mark", 1);
  2299. ELSIF type.IsRecordType() THEN
  2300. Emit (Push(position,register));
  2301. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2302. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2303. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2304. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2305. ELSIF IsStaticArray(type) THEN
  2306. size := StaticArrayNumElements(type);
  2307. base := StaticArrayBaseType(type);
  2308. IF base.IsRecordType() THEN
  2309. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2310. Emit (Push(position, register));
  2311. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2312. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2316. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2317. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2318. Emit (Push(position, register));
  2319. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2320. ELSE
  2321. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2322. Emit (Push(position, register));
  2323. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2324. ASSERT(base.IsPointer());
  2325. END;
  2326. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2327. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2328. CallThis(position,"GarbageCollector","Mark", 1);
  2329. ELSE HALT(100); (* missing ? *)
  2330. END;
  2331. END CallTraceMethod;
  2332. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2333. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2334. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2335. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2336. BEGIN
  2337. previousSection := section;
  2338. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2339. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2340. GetRecordTypeName (recordType,name);
  2341. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2342. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2343. IF dump # NIL THEN dump := section.comments END;
  2344. IF backend.hasLinkRegister THEN
  2345. Emit(Push(Basic.invalidPosition, lr));
  2346. END;
  2347. variable := recordType.recordScope.firstVariable;
  2348. WHILE variable # NIL DO
  2349. IF variable.NeedsTrace() THEN
  2350. register := NewRegisterOperand (addressType);
  2351. Emit (Mov(position,register,parameter1));
  2352. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2353. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2354. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2355. END;
  2356. CallTraceMethod(register, variable.type);
  2357. ReleaseIntermediateOperand(register);
  2358. END;
  2359. variable := variable.nextVariable;
  2360. END;
  2361. recordBase := recordType.GetBaseRecord();
  2362. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2363. recordBase := recordBase.GetBaseRecord();
  2364. END;
  2365. IF recordBase # NIL THEN
  2366. IF backend.hasLinkRegister THEN
  2367. Emit(Pop(Basic.invalidPosition, lr));
  2368. END;
  2369. GetRecordTypeName (recordBase,name);
  2370. IF HasExplicitTraceMethod (recordBase) THEN
  2371. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2372. ELSE
  2373. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2374. END;
  2375. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2376. Emit(Br(position,op));
  2377. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2378. Emit(Exit(position,0,0,0));
  2379. ELSE
  2380. IF backend.hasLinkRegister THEN
  2381. Emit(Pop(Basic.invalidPosition, lr));
  2382. END;
  2383. IF recordType.isObject THEN
  2384. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2385. ELSE
  2386. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2387. END;
  2388. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2389. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2390. Emit(Br(position,op));
  2391. END;
  2392. IF ~recordType.isObject THEN
  2393. GetRecordTypeName (recordType,name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2395. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2396. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2397. NEW(registerUsageCount);
  2398. usedRegisters := NIL;
  2399. IF dump # NIL THEN dump := section.comments END;
  2400. register := NewRegisterOperand (addressType);
  2401. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2402. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2403. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2404. END;
  2405. Emit (Push(position,register));
  2406. GetRecordTypeName (recordType,name);
  2407. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2408. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2409. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2410. ReleaseIntermediateOperand(register);
  2411. Emit(Exit(position,0,0,0));
  2412. GetRecordTypeName (recordType,name);
  2413. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2414. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2415. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2416. NEW(registerUsageCount);
  2417. usedRegisters := NIL;
  2418. register := NewRegisterOperand (addressType);
  2419. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2420. Emit(Mov(position, register, parameter1));
  2421. label := NewLabel();
  2422. SetLabel(label);
  2423. Emit(Push(position, register));
  2424. GetRecordTypeName (recordType,name);
  2425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2426. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2427. Emit(Call(position, op, 0));
  2428. Emit(Pop(position, register));
  2429. Emit(Add(position, register, register, ofs));
  2430. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2431. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2432. Emit(Exit(position,0,0,0));
  2433. END;
  2434. INC(statCoopTraceMethod, section.pc);
  2435. ReturnToContext(context);
  2436. IF dump # NIL THEN dump := section.comments END;
  2437. END CreateTraceMethod;
  2438. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2439. VAR name: Basic.SegmentedName;
  2440. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2441. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2442. context: Context;
  2443. BEGIN
  2444. IF recordType.isObject THEN RETURN END;
  2445. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2446. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2447. GetRecordTypeName (recordType,name);
  2448. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2449. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2450. IF dump # NIL THEN dump := section.comments END;
  2451. IF backend.hasLinkRegister THEN
  2452. Emit(Push(Basic.invalidPosition, lr));
  2453. END;
  2454. variable := recordType.recordScope.firstVariable;
  2455. WHILE variable # NIL DO
  2456. IF variable.NeedsTrace() THEN
  2457. dst := NewRegisterOperand (addressType);
  2458. Emit (Mov(position, dst, parameter1));
  2459. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2460. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2461. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2462. END;
  2463. CallResetProcedure(dst, nil, variable.type);
  2464. ReleaseIntermediateOperand(dst);
  2465. END;
  2466. variable := variable.nextVariable;
  2467. END;
  2468. recordBase := recordType.GetBaseRecord();
  2469. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2470. IF backend.hasLinkRegister THEN
  2471. Emit(Pop(Basic.invalidPosition, lr));
  2472. END;
  2473. GetRecordTypeName (recordBase,name);
  2474. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2475. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2476. Emit(Br(position,op));
  2477. ELSE
  2478. Emit(Exit(position,0,0, 0));
  2479. END;
  2480. GetRecordTypeName (recordType,name);
  2481. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2482. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2483. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL);
  2484. NEW(registerUsageCount);
  2485. usedRegisters := NIL;
  2486. dst := NewRegisterOperand (addressType);
  2487. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2488. Emit(Mov(position, dst, parameter1));
  2489. label := NewLabel();
  2490. SetLabel(label);
  2491. Emit(Push(position, dst));
  2492. GetRecordTypeName (recordType,name);
  2493. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2494. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2495. Emit(Call(position, op, 0));
  2496. Emit(Pop(position, dst));
  2497. Emit(Add(position, dst, dst, ofs));
  2498. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2499. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2500. Emit(Exit(position,0,0, 0));
  2501. INC(statCoopResetProcedure, section.pc);
  2502. ReturnToContext(context);
  2503. IF dump # NIL THEN dump := section.comments END;
  2504. END CreateResetProcedure;
  2505. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2506. VAR name: Basic.SegmentedName; context: Context;
  2507. BEGIN
  2508. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2509. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2510. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump # NIL));
  2511. IF dump # NIL THEN dump := section.comments END;
  2512. IF backend.hasLinkRegister THEN
  2513. Emit(Push(Basic.invalidPosition, lr));
  2514. END;
  2515. Emit(Push(position,fp));
  2516. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2517. ResetVariables(scope);
  2518. Emit(Pop(position,fp));
  2519. Emit(Exit(position,0,0, 0));
  2520. ReturnToContext(context);
  2521. IF dump # NIL THEN dump := section.comments END;
  2522. END CreateResetMethod;
  2523. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2524. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2525. BEGIN
  2526. IF SemanticChecker.IsPointerType (type) THEN
  2527. Emit (Push(position, dest));
  2528. CallThis(position,"GarbageCollector","Reset", 1);
  2529. ELSIF type.IsRecordType() THEN
  2530. Emit (Push(position, dest));
  2531. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2532. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2533. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2534. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2535. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2536. size := GetArrayLength(type, tag);
  2537. base := ArrayBaseType(type);
  2538. IF base.IsRecordType() THEN
  2539. Emit (Push(position, size));
  2540. Emit (Push(position, dest));
  2541. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2542. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2543. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2544. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2545. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2546. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2547. Emit (Push(position, size));
  2548. Emit (Push(position, dest));
  2549. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2550. ELSE
  2551. Emit (Push(position, size));
  2552. Emit (Push(position, dest));
  2553. CallThis(position,"GarbageCollector","ResetArray", 2);
  2554. ASSERT(ArrayBaseType(type).IsPointer());
  2555. END;
  2556. ReleaseIntermediateOperand(size);
  2557. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2558. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2559. Emit (Push(position, dest));
  2560. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2561. ELSE HALT(100); (* missing ? *)
  2562. END;
  2563. END CallResetProcedure;
  2564. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2565. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2566. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2567. VAR operand: Operand;
  2568. BEGIN
  2569. Symbol (symbol, operand);
  2570. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2571. ReleaseOperand(operand);
  2572. END Reset;
  2573. BEGIN
  2574. previousScope := currentScope;
  2575. currentScope := scope;
  2576. pc := section.pc;
  2577. variable := scope.firstVariable;
  2578. WHILE variable # NIL DO
  2579. IF variable.NeedsTrace() THEN
  2580. Reset (variable);
  2581. END;
  2582. variable := variable.nextVariable;
  2583. END;
  2584. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2585. WHILE parameter # NIL DO
  2586. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2587. Reset (parameter);
  2588. END;
  2589. parameter := parameter.nextParameter;
  2590. END;
  2591. INC(statCoopResetVariables, section.pc - pc);
  2592. currentScope := previousScope;
  2593. END ResetVariables;
  2594. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2595. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2596. VAR op: IntermediateCode.Operand; context: Context;
  2597. BEGIN
  2598. previousSection := section;
  2599. GetCodeSectionNameForSymbol(procedure, name);
  2600. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2601. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,dump # NIL));
  2602. IF dump # NIL THEN dump := section.comments END;
  2603. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2604. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2605. Emit(Data(position,op));
  2606. Emit(Data(position,nil));
  2607. IF HasPointers (procedure.procedureScope) THEN
  2608. GetCodeSectionNameForSymbol(procedure, name);
  2609. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2610. ELSE
  2611. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2612. END;
  2613. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2614. Emit(Data(position,op));
  2615. ReturnToContext(context);
  2616. IF dump # NIL THEN dump := section.comments END;
  2617. END CreateProcedureDescriptor;
  2618. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2619. VAR import: SyntaxTree.Import;
  2620. s: Basic.MessageString;
  2621. selfName: SyntaxTree.IdentifierString;
  2622. BEGIN
  2623. moduleScope.ownerModule.GetName(selfName);
  2624. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2625. module := moduleScope.ownerModule
  2626. ELSE
  2627. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2628. IF import = NIL THEN
  2629. import := SyntaxTree.NewImport(Basic.invalidPosition,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),FALSE);
  2630. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2631. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2632. s := "Module ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s," cannot be imported.");
  2635. IF force THEN
  2636. Error(position,s);
  2637. ELSIF canBeLoaded THEN
  2638. Strings.Append(s, "=> no dynamic linking.");
  2639. Warning(position, s);
  2640. canBeLoaded := FALSE;
  2641. END;
  2642. RETURN FALSE
  2643. ELSE
  2644. SELF.module.imports.AddName(moduleName)
  2645. END;
  2646. ELSIF import.module = NIL THEN (* already tried *)
  2647. RETURN FALSE
  2648. END;
  2649. module := import.module;
  2650. END;
  2651. RETURN TRUE
  2652. END AddImport;
  2653. (* needed for old binary object file format*)
  2654. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2655. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2656. BEGIN
  2657. IF AddImport(moduleName,module,TRUE) THEN
  2658. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2659. IF procedure = NIL THEN
  2660. s := "Instruction not supported on target, emulation procedure ";
  2661. Strings.Append(s,moduleName);
  2662. Strings.Append(s,".");
  2663. Strings.Append(s,procedureName);
  2664. Strings.Append(s," not present");
  2665. Error(position,s);
  2666. ELSE
  2667. StaticCallOperand(r,procedure);
  2668. ReleaseOperand(r);
  2669. fp := GetFingerprint(procedure);
  2670. END;
  2671. END;
  2672. END EnsureSymbol;
  2673. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2674. VAR exit: Label; trueL,falseL: Label;
  2675. BEGIN
  2676. trueL := NewLabel();
  2677. falseL := NewLabel();
  2678. exit := NewLabel();
  2679. Condition(x,trueL,falseL);
  2680. InitOperand(result,ModeValue);
  2681. SetLabel(trueL);
  2682. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2683. Emit(Mov(position,result.op,true));
  2684. BrL(exit);
  2685. SetLabel(falseL);
  2686. Emit(MovReplace(position,result.op,false));
  2687. SetLabel(exit);
  2688. END ConditionToValue;
  2689. PROCEDURE ValueToCondition(VAR op: Operand);
  2690. BEGIN
  2691. LoadValue(op,system.booleanType);
  2692. BrneL(trueLabel,op.op, false);
  2693. ReleaseOperand(op);
  2694. BrL(falseLabel);
  2695. END ValueToCondition;
  2696. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2697. VAR size: LONGINT;
  2698. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2699. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2700. BEGIN
  2701. ASSERT(type.form = SyntaxTree.Open);
  2702. baseType := type.arrayBase.resolved;
  2703. IF IsOpenArray(baseType) THEN
  2704. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2705. ELSE
  2706. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2707. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2708. END;
  2709. len := tag;
  2710. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2711. IntermediateCode.MakeMemory(len,addressType);
  2712. UseIntermediateOperand(len);
  2713. Reuse2(res,sizeOperand,len);
  2714. Emit(Mul(position,res,sizeOperand,len));
  2715. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2716. RETURN res
  2717. END GetArraySize;
  2718. BEGIN
  2719. type := type.resolved;
  2720. IF IsOpenArray(type) THEN
  2721. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2722. RETURN tag
  2723. ELSE
  2724. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2725. END;
  2726. ELSE
  2727. size := ToMemoryUnits(system,system.SizeOf(type));
  2728. RETURN IntermediateCode.Immediate(addressType,size)
  2729. END;
  2730. END GetDynamicSize;
  2731. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2732. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2733. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2734. BEGIN
  2735. ASSERT(type.form = SyntaxTree.Open);
  2736. baseType := type.arrayBase.resolved;
  2737. IF IsOpenArray(baseType) THEN
  2738. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2739. ELSE
  2740. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2741. END;
  2742. len := tag;
  2743. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2744. IntermediateCode.MakeMemory(len,addressType);
  2745. UseIntermediateOperand(len);
  2746. Reuse2(res,sizeOperand,len);
  2747. Emit(Mul(position,res,sizeOperand,len));
  2748. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2749. RETURN res
  2750. END GetLength;
  2751. BEGIN
  2752. type := type.resolved;
  2753. IF IsOpenArray(type) THEN
  2754. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2755. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2756. ELSIF type IS SyntaxTree.StringType THEN
  2757. RETURN tag;
  2758. ELSE
  2759. RETURN IntermediateCode.Immediate(addressType,StaticArrayNumElements(type))
  2760. END;
  2761. END GetArrayLength;
  2762. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2763. VAR result: IntermediateCode.Operand;
  2764. BEGIN
  2765. IF backend.cooperative THEN
  2766. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2767. ELSE
  2768. MakeMemory(result, tag, addressType, 0);
  2769. END;
  2770. RETURN result
  2771. END GetSizeFromTag;
  2772. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2773. VAR parameter: SyntaxTree.Parameter;
  2774. BEGIN
  2775. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2776. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2777. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2778. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2779. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2780. END;
  2781. RETURN GetDynamicSize(e.type, tag);
  2782. END GetArrayOfBytesSize;
  2783. (*
  2784. to find imported symbol. not needed ?
  2785. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2786. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2787. BEGIN
  2788. IF AddImport(moduleName,importedModule,FALSE) THEN
  2789. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2790. RETURN symbol
  2791. ELSE
  2792. RETURN NIL
  2793. END
  2794. END SymbolByName;
  2795. *)
  2796. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2797. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2798. BEGIN
  2799. IF AddImport(moduleName,runtimeModule,force) THEN
  2800. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2801. IF procedure = NIL THEN
  2802. s := "Procedure ";
  2803. Strings.Append(s,moduleName);
  2804. Strings.Append(s,".");
  2805. Strings.Append(s,procedureName);
  2806. Strings.Append(s," not present");
  2807. Error(position,s);
  2808. RETURN FALSE
  2809. ELSE
  2810. RETURN TRUE
  2811. END;
  2812. ELSE RETURN FALSE
  2813. END;
  2814. END GetRuntimeProcedure;
  2815. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2816. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2817. s: Basic.MessageString;
  2818. BEGIN
  2819. Basic.InitSegmentedName(name);
  2820. name[0] := Basic.MakeString(moduleName);
  2821. name[1] := Basic.MakeString(typeName);
  2822. name[2] := -1;
  2823. IF AddImport(moduleName,importedModule, FALSE) THEN
  2824. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2825. IF symbol = NIL THEN
  2826. s := "type ";
  2827. Strings.Append(s,moduleName);
  2828. Strings.Append(s,".");
  2829. Strings.Append(s,typeName);
  2830. Strings.Append(s," not present");
  2831. Error(position,s);
  2832. END;
  2833. ELSE symbol := NIL;
  2834. END;
  2835. IF importedModule = moduleScope.ownerModule THEN
  2836. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2837. ELSE
  2838. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2839. END;
  2840. RETURN symbol
  2841. END GetTypeDescriptor;
  2842. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2843. PROCEDURE CallThisChecked(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2844. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2845. pooledName: Basic.SegmentedName; size: LONGINT;
  2846. BEGIN
  2847. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2848. StaticCallOperand(result,procedure);
  2849. IF numberParameters < 0 THEN
  2850. size := ProcedureParametersSize(system,procedure);
  2851. ELSE
  2852. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2853. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2854. Error(position,"runtime call parameter count mismatch");
  2855. END;
  2856. END;
  2857. Emit(Call(position, result.op, size));
  2858. ReleaseOperand(result);
  2859. ELSE (* only static linking possible *)
  2860. ASSERT(numberParameters >= 0);
  2861. Basic.InitSegmentedName(pooledName);
  2862. pooledName[0] := Basic.MakeString(moduleName);
  2863. pooledName[1] := Basic.MakeString(procedureName);
  2864. pooledName[2] := -1;
  2865. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2866. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2867. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2868. END;
  2869. END CallThisChecked;
  2870. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2871. PROCEDURE CallThis(position: Position; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2872. BEGIN
  2873. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2874. END CallThis;
  2875. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2876. VAR
  2877. left,right: Operand;
  2878. leftSize, rightSize: IntermediateCode.Operand;
  2879. saved: RegisterEntry;
  2880. reg: IntermediateCode.Operand;
  2881. procedureName: SyntaxTree.IdentifierString;
  2882. BEGIN
  2883. procedureName := "CompareString";
  2884. SaveRegisters();ReleaseUsedRegisters(saved);
  2885. Designate(leftExpression,left);
  2886. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2887. Emit(Push(position,leftSize));
  2888. ReleaseIntermediateOperand(leftSize);
  2889. Emit(Push(position,left.op));
  2890. ReleaseOperand(left);
  2891. Designate(rightExpression,right);
  2892. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2893. Emit(Push(position,rightSize));
  2894. ReleaseIntermediateOperand(rightSize);
  2895. Emit(Push(position,right.op));
  2896. ReleaseOperand(right);
  2897. IF backend.cooperative THEN
  2898. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2899. ELSE
  2900. CallThis(position,runtimeModuleName,procedureName, 4);
  2901. END;
  2902. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Result(position,reg));
  2904. (*
  2905. AcquireThisRegister(int8,IntermediateCode.Result);
  2906. *)
  2907. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2908. (*
  2909. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2910. *)
  2911. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2912. ReleaseIntermediateOperand(reg);
  2913. BrL(falseLabel);
  2914. END CompareString;
  2915. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2916. VAR
  2917. left,right: Operand;
  2918. leftSize, rightSize: IntermediateCode.Operand;
  2919. saved: RegisterEntry;
  2920. procedureName: SyntaxTree.IdentifierString;
  2921. BEGIN
  2922. procedureName := "CopyString";
  2923. SaveRegisters();ReleaseUsedRegisters(saved);
  2924. Designate(leftExpression,left);
  2925. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2926. Emit(Push(position,leftSize));
  2927. ReleaseIntermediateOperand(leftSize);
  2928. Emit(Push(position,left.op));
  2929. ReleaseOperand(left);
  2930. Designate(rightExpression,right);
  2931. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2932. Emit(Push(position,rightSize));
  2933. ReleaseIntermediateOperand(rightSize);
  2934. Emit(Push(position,right.op));
  2935. ReleaseOperand(right);
  2936. IF backend.cooperative THEN
  2937. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2938. ELSE
  2939. CallThis(position,runtimeModuleName,procedureName,4);
  2940. END;
  2941. RestoreRegisters(saved);
  2942. END CopyString;
  2943. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2944. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2945. leftType,rightType: SyntaxTree.Type;
  2946. leftExpression,rightExpression : SyntaxTree.Expression;
  2947. componentType: IntermediateCode.Type;
  2948. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2949. size: LONGINT;
  2950. BEGIN
  2951. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2952. dest := destination; destination := emptyOperand;
  2953. leftType := x.left.type.resolved;
  2954. rightType := x.right.type.resolved;
  2955. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2956. CASE x.operator OF
  2957. Scanner.Or:
  2958. (* shortcut evaluation of left OR right *)
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSE
  2961. next := NewLabel();
  2962. Condition(x.left,trueLabel,next);
  2963. SetLabel(next);
  2964. Condition(x.right,trueLabel,falseLabel);
  2965. END;
  2966. |Scanner.And:
  2967. (* shortcut evaluation of left & right *)
  2968. IF ~conditional THEN ConditionToValue(x);
  2969. ELSE
  2970. next := NewLabel();
  2971. Condition(x.left,next,falseLabel);
  2972. SetLabel(next);
  2973. Condition(x.right,trueLabel,falseLabel);
  2974. END;
  2975. |Scanner.Is:
  2976. IF ~conditional THEN ConditionToValue(x);
  2977. ELSE
  2978. (* get type desc tag *)
  2979. IF IsPointerToRecord(leftType,recordType) THEN
  2980. Evaluate(x.left,left);
  2981. Dereference(left,recordType,IsUnsafePointer(leftType))
  2982. ELSE
  2983. Designate(x.left,left);
  2984. END;
  2985. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2986. ReleaseOperand(left);
  2987. END;
  2988. |Scanner.Plus:
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. IF leftType IS SyntaxTree.SetType THEN
  2992. InitOperand(result,ModeValue);
  2993. Reuse2a(result.op,left.op,right.op,dest);
  2994. Emit(Or(position,result.op,left.op,right.op));
  2995. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2996. InitOperand(result,ModeValue);
  2997. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2998. Reuse2(result.tag,left.tag,right.tag);
  2999. Emit(Add(position,result.op,left.op,right.op));
  3000. Emit(Add(position,result.tag,left.tag,right.tag))
  3001. ELSE
  3002. InitOperand(result,ModeValue);
  3003. (*! IF SemanticChecker.IsIntegerType(leftType) THEN
  3004. AddInt(result.op, left.op, right.op) ;
  3005. ELSE
  3006. *)
  3007. Reuse2a(result.op,left.op,right.op,dest);
  3008. Emit(Add(position,result.op,left.op,right.op));
  3009. (*
  3010. END;
  3011. *)
  3012. END;
  3013. ReleaseOperand(left); ReleaseOperand(right);
  3014. |Scanner.Minus:
  3015. Evaluate(x.left,left);
  3016. Evaluate(x.right,right);
  3017. IF leftType IS SyntaxTree.SetType THEN
  3018. InitOperand(result,ModeValue);
  3019. Reuse1(result.op,right.op);
  3020. Emit(Not(position,result.op,right.op));
  3021. ReleaseOperand(right);
  3022. Emit(And(position,result.op,result.op,left.op));
  3023. ReleaseOperand(left);
  3024. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3025. InitOperand(result,ModeValue);
  3026. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  3027. Reuse2(result.tag,left.tag,right.tag);
  3028. Emit(Sub(position,result.op,left.op,right.op));
  3029. Emit(Sub(position,result.tag,left.tag,right.tag));
  3030. ReleaseOperand(left); ReleaseOperand(right)
  3031. ELSE
  3032. InitOperand(result,ModeValue);
  3033. Reuse2a(result.op,left.op,right.op,dest);
  3034. Emit(Sub(position,result.op,left.op,right.op));
  3035. ReleaseOperand(left); ReleaseOperand(right);
  3036. END;
  3037. |Scanner.Times:
  3038. Evaluate(x.left,left);
  3039. Evaluate(x.right,right);
  3040. IF leftType IS SyntaxTree.SetType THEN
  3041. InitOperand(result,ModeValue);
  3042. Reuse2a(result.op,left.op,right.op,dest);
  3043. Emit(And(position,result.op,left.op,right.op));
  3044. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3045. InitOperand(result, ModeValue);
  3046. componentType := left.op.type;
  3047. (* TODO: review this *)
  3048. (*
  3049. result.op = left.op * right.op - left.tag * right.tag
  3050. result.tag = left.tag * right.op + left.op * right.tag
  3051. *)
  3052. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3053. Emit(Mul(position,result.op, left.op, right.op));
  3054. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3055. Emit(Mul(position,tempReg, left.tag, right.tag));
  3056. Emit(Sub(position,result.op, result.op, tempReg));
  3057. Reuse2(result.tag, left.tag, right.op);
  3058. Emit(Mul(position,result.tag, left.tag, right.op));
  3059. Emit(Mul(position,tempReg, left.op, right.tag));
  3060. Emit(Add(position,result.tag, result.tag, tempReg));
  3061. ReleaseIntermediateOperand(tempReg)
  3062. ELSE
  3063. InitOperand(result,ModeValue);
  3064. Reuse2a(result.op,left.op,right.op,dest);
  3065. Emit(Mul(position,result.op,left.op,right.op));
  3066. END;
  3067. ReleaseOperand(left); ReleaseOperand(right);
  3068. |Scanner.Div:
  3069. Evaluate(x.left,left);
  3070. Evaluate(x.right,right);
  3071. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3072. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3073. IF ~isUnchecked THEN
  3074. exit := NewLabel();
  3075. BrltL(exit,zero,right.op);
  3076. EmitTrap(position,NegativeDivisorTrap);
  3077. SetLabel(exit);
  3078. END;
  3079. END;
  3080. InitOperand(result,ModeValue);
  3081. Reuse2a(result.op,left.op,right.op,dest);
  3082. Emit(Div(position,result.op,left.op,right.op));
  3083. ReleaseOperand(left); ReleaseOperand(right);
  3084. |Scanner.Mod:
  3085. Evaluate(x.left,left);
  3086. Evaluate(x.right,right);
  3087. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  3088. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  3089. IF ~isUnchecked THEN
  3090. exit := NewLabel();
  3091. BrltL(exit,zero,right.op);
  3092. EmitTrap(position,NegativeDivisorTrap);
  3093. SetLabel(exit);
  3094. END;
  3095. END;
  3096. InitOperand(result,ModeValue);
  3097. Reuse2a(result.op,left.op,right.op,dest);
  3098. Emit(Mod(position,result.op,left.op,right.op));
  3099. ReleaseOperand(left); ReleaseOperand(right);
  3100. |Scanner.Slash:
  3101. Evaluate(x.left,left);
  3102. Evaluate(x.right,right);
  3103. IF leftType IS SyntaxTree.SetType THEN
  3104. InitOperand(result,ModeValue);
  3105. Reuse2a(result.op,left.op,right.op,dest);
  3106. Emit(Xor(position,result.op,left.op,right.op));
  3107. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3108. InitOperand(result,ModeValue);
  3109. componentType := left.op.type;
  3110. (* review this *)
  3111. (*
  3112. divisor = right.op * right.op + right.tag * right.tag
  3113. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  3114. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  3115. *)
  3116. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3117. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3118. Emit(Mul(position,tempReg, right.op, right.op));
  3119. Emit(Mul(position,tempReg2, right.tag, right.tag));
  3120. Emit(Add(position,tempReg, tempReg, tempReg2));
  3121. result.op := tempReg2;
  3122. Emit(Mul(position,result.op, left.op, right.op));
  3123. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  3124. Emit(Mul(position,tempReg2, left.tag, right.tag));
  3125. Emit(Add(position,result.op, result.op, tempReg2));
  3126. Emit(Div(position,result.op, result.op, tempReg));
  3127. Reuse2(result.tag, left.tag, right.op);
  3128. Emit(Mul(position,result.tag, left.tag, right.op));
  3129. Emit(Mul(position,tempReg2, left.op, right.tag));
  3130. Emit(Sub(position,result.tag, result.tag, tempReg2));
  3131. Emit(Div(position,result.tag, result.tag, tempReg));
  3132. ReleaseIntermediateOperand(tempReg);
  3133. ReleaseIntermediateOperand(tempReg2)
  3134. ELSE
  3135. InitOperand(result,ModeValue);
  3136. Reuse2a(result.op,left.op,right.op,dest);
  3137. Emit(Div(position,result.op,left.op,right.op));
  3138. END;
  3139. ReleaseOperand(left); ReleaseOperand(right);
  3140. |Scanner.Equal:
  3141. IF ~conditional THEN ConditionToValue(x);
  3142. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3143. CompareString(BreqL,x.left,x.right);
  3144. ELSE
  3145. Evaluate(x.left,left);
  3146. Evaluate(x.right,right);
  3147. IF leftType IS SyntaxTree.RangeType THEN
  3148. ASSERT(rightType IS SyntaxTree.RangeType);
  3149. BrneL(falseLabel, left.op, right.op); (* first *)
  3150. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3151. BrneL(falseLabel, left.extra, right.extra); (* step *)
  3152. ReleaseOperand(left); ReleaseOperand(right);
  3153. BrL(trueLabel)
  3154. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3155. BrneL(falseLabel, left.op, right.op); (* first *)
  3156. BrneL(falseLabel, left.tag, right.tag); (* last *)
  3157. ReleaseOperand(left); ReleaseOperand(right);
  3158. BrL(trueLabel)
  3159. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3160. (* TODO: review this *)
  3161. BrneL(falseLabel, left.op, right.op); (* real part *)
  3162. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  3163. ReleaseOperand(left); ReleaseOperand(right);
  3164. BrL(trueLabel)
  3165. ELSE
  3166. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3167. ReleaseOperand(left); ReleaseOperand(right);
  3168. BrL(trueLabel);
  3169. END;
  3170. END;
  3171. |Scanner.LessEqual:
  3172. IF ~conditional THEN ConditionToValue(x);
  3173. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3174. CompareString(BrgeL,x.right,x.left);
  3175. ELSE
  3176. Evaluate(x.left,left);
  3177. Evaluate(x.right,right);
  3178. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  3179. Reuse1(temp.op,right.op);
  3180. Emit(And(position,temp.op,left.op,right.op));
  3181. ReleaseOperand(right);
  3182. BreqL(trueLabel,temp.op,left.op);
  3183. BrL(falseLabel);
  3184. ReleaseOperand(temp);ReleaseOperand(left);
  3185. ELSE
  3186. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3187. ReleaseOperand(left); ReleaseOperand(right);
  3188. BrL(trueLabel);
  3189. END;
  3190. END;
  3191. |Scanner.Less:
  3192. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3193. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.LessEqual);
  3194. leftExpression.SetType(system.booleanType);
  3195. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3196. rightExpression.SetType(system.booleanType);
  3197. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3198. leftExpression.SetType(system.booleanType);
  3199. Expression(leftExpression);
  3200. ELSIF ~conditional THEN ConditionToValue(x);
  3201. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3202. CompareString(BrltL,x.left,x.right);
  3203. ELSE
  3204. Evaluate(x.left,left);
  3205. Evaluate(x.right,right);
  3206. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3207. ReleaseOperand(left); ReleaseOperand(right);
  3208. BrL(trueLabel);
  3209. END;
  3210. |Scanner.Greater:
  3211. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3212. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.GreaterEqual);
  3213. leftExpression.SetType(system.booleanType);
  3214. rightExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.left,x.right,Scanner.Unequal);
  3215. rightExpression.SetType(system.booleanType);
  3216. leftExpression := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,leftExpression,rightExpression,Scanner.And);
  3217. leftExpression.SetType(system.booleanType);
  3218. Expression(leftExpression);
  3219. ELSIF ~conditional THEN ConditionToValue(x);
  3220. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3221. CompareString(BrltL,x.right,x.left);
  3222. ELSE
  3223. Evaluate(x.left,left);
  3224. Evaluate(x.right,right);
  3225. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3226. ReleaseOperand(left); ReleaseOperand(right);
  3227. BrL(trueLabel);
  3228. END;
  3229. |Scanner.GreaterEqual:
  3230. IF ~conditional THEN ConditionToValue(x);
  3231. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3232. CompareString(BrgeL,x.left,x.right);
  3233. ELSE
  3234. Evaluate(x.left,left);
  3235. Evaluate(x.right,right);
  3236. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3237. Reuse1(temp.op,left.op);
  3238. Emit(And(position,temp.op,left.op,right.op));
  3239. ReleaseOperand(left);
  3240. BreqL(trueLabel, temp.op,right.op);
  3241. ReleaseOperand(temp); ReleaseOperand(right);
  3242. BrL(falseLabel);
  3243. ELSE
  3244. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3245. ReleaseOperand(left); ReleaseOperand(right);
  3246. BrL(trueLabel);
  3247. END;
  3248. END;
  3249. |Scanner.Unequal:
  3250. IF ~conditional THEN ConditionToValue(x);
  3251. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3252. CompareString(BrneL,x.left,x.right);
  3253. ELSE
  3254. Evaluate(x.left,left);
  3255. Evaluate(x.right,right);
  3256. IF leftType IS SyntaxTree.RangeType THEN
  3257. ASSERT(rightType IS SyntaxTree.RangeType);
  3258. BrneL(trueLabel, left.op, right.op); (* first *)
  3259. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3260. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3261. ReleaseOperand(left); ReleaseOperand(right);
  3262. BrL(falseLabel)
  3263. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3264. BrneL(trueLabel, left.op, right.op); (* first *)
  3265. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3266. ReleaseOperand(left); ReleaseOperand(right);
  3267. BrL(falseLabel)
  3268. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3269. (* TODO: review this *)
  3270. BrneL(trueLabel, left.op, right.op); (* real part *)
  3271. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3272. ReleaseOperand(left); ReleaseOperand(right);
  3273. BrL(falseLabel)
  3274. ELSE
  3275. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3276. ReleaseOperand(left); ReleaseOperand(right);
  3277. BrL(trueLabel);
  3278. END;
  3279. END;
  3280. |Scanner.In:
  3281. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3282. Evaluate(x.left,left);
  3283. Evaluate(x.right,right);
  3284. Convert(left.op,setType);
  3285. ReuseCopy(temp.op,right.op);
  3286. Emit(Shr(position,temp.op,temp.op,left.op));
  3287. ReleaseOperand(right); ReleaseOperand(left);
  3288. IntermediateCode.InitImmediate(one,setType,1);
  3289. Emit(And(position,temp.op,temp.op,one));
  3290. IF conditional THEN
  3291. IntermediateCode.InitImmediate(zero,setType,0);
  3292. BrneL(trueLabel,temp.op,zero);
  3293. ReleaseOperand(temp);
  3294. BrL(falseLabel);
  3295. ELSE
  3296. Convert(temp.op,bool);
  3297. result.mode := ModeValue;
  3298. result.op := temp.op;
  3299. result.tag := nil; (* may be left over from calls to evaluate *)
  3300. END;
  3301. ELSE
  3302. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3303. IF x.operator = Scanner.Questionmarks THEN
  3304. leftExpression := x.left;
  3305. rightExpression := x.right;
  3306. ELSE
  3307. leftExpression := x.right;
  3308. rightExpression := x.left;
  3309. END;
  3310. Evaluate(leftExpression, left);
  3311. Emit(Push(position,left.op));
  3312. ReleaseOperand(left);
  3313. Designate(rightExpression, right);
  3314. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3315. IF ~backend.cellsAreObjects THEN
  3316. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3317. END;
  3318. Emit(Push(position,right.op));
  3319. ReleaseOperand(right);
  3320. IF backend.cellsAreObjects THEN
  3321. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3322. ELSE
  3323. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3324. END;
  3325. InitOperand(result, ModeValue);
  3326. result.op := NewRegisterOperand(bool);
  3327. Emit(Result(position,result.op));
  3328. IF conditional THEN
  3329. IntermediateCode.InitImmediate(zero,setType,0);
  3330. BrneL(trueLabel,result.op,zero);
  3331. ReleaseOperand(result);
  3332. BrL(falseLabel);
  3333. END;
  3334. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3335. leftExpression := x.left;
  3336. rightExpression := x.right;
  3337. Evaluate(leftExpression, left);
  3338. Emit(Push(position,left.op));
  3339. ReleaseOperand(left);
  3340. Evaluate(rightExpression, right);
  3341. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3342. IF ~backend.cellsAreObjects THEN
  3343. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3344. END;
  3345. Emit(Push(position,right.op));
  3346. ReleaseOperand(right);
  3347. IF backend.cellsAreObjects THEN
  3348. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3349. ELSE
  3350. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3351. END;
  3352. InitOperand(result, ModeValue);
  3353. result.op := NewRegisterOperand(bool);
  3354. Emit(Result(position,result.op));
  3355. IF conditional THEN
  3356. IntermediateCode.InitImmediate(zero,setType,0);
  3357. BrneL(trueLabel,result.op,zero);
  3358. ReleaseOperand(result);
  3359. BrL(falseLabel);
  3360. END;
  3361. ELSE
  3362. HALT(100);
  3363. END;
  3364. END;
  3365. destination := dest;
  3366. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3367. END VisitBinaryExpression;
  3368. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3369. VAR localResult, operand: Operand;
  3370. BEGIN
  3371. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3372. InitOperand(localResult, ModeValue);
  3373. ASSERT(x.first # NIL);
  3374. Evaluate(x.first, operand);
  3375. localResult.op := operand.op;
  3376. ReleaseOperand(operand);
  3377. UseIntermediateOperand(localResult.op);
  3378. ASSERT(x.last # NIL);
  3379. Evaluate(x.last, operand);
  3380. localResult.tag := operand.op;
  3381. ReleaseOperand(operand);
  3382. UseIntermediateOperand(localResult.tag);
  3383. IF x.step # NIL THEN
  3384. Evaluate(x.step, operand);
  3385. localResult.extra := operand.op;
  3386. ReleaseOperand(operand);
  3387. UseIntermediateOperand(localResult.extra);
  3388. END;
  3389. result := localResult;
  3390. IF Trace THEN TraceExit("VisitRangeExpression") END
  3391. END VisitRangeExpression;
  3392. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3393. BEGIN
  3394. HALT(100); (* should never be evaluated *)
  3395. END VisitTensorRangeExpression;
  3396. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3397. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3398. BEGIN
  3399. IF Trace THEN TraceEnter("VisitConversion") END;
  3400. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3401. dest := destination; destination := emptyOperand;
  3402. Evaluate(x.expression,old);
  3403. InitOperand(result,ModeValue);
  3404. result.op := old.op;
  3405. ASSERT(result.op.mode # 0);
  3406. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3407. (* convert TO a complex number *)
  3408. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3409. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3410. ASSERT(result.op.mode # 0);
  3411. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3412. (* convert FROM a complex number TO a complex number*)
  3413. result.tag := old.tag;
  3414. ASSERT(result.tag.mode # 0);
  3415. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3416. ASSERT(result.tag.mode # 0)
  3417. ELSE
  3418. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3419. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3420. END
  3421. ELSE
  3422. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3423. ASSERT(result.op.mode # 0);
  3424. result.tag := old.tag; (*! probably never used *)
  3425. END;
  3426. destination := dest;
  3427. IF Trace THEN TraceExit("VisitConversion") END;
  3428. END VisitConversion;
  3429. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3430. BEGIN
  3431. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3432. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3433. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3434. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3435. END VisitTypeDeclaration;
  3436. (** designators (expressions) *)
  3437. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3438. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3439. BEGIN
  3440. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3441. IF x.left # NIL THEN Expression(x.left) END;
  3442. Symbol(x.symbol,result);
  3443. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3444. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3445. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3446. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3447. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3448. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3449. END;
  3450. END;
  3451. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3452. END VisitSymbolDesignator;
  3453. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3454. BEGIN
  3455. IF isUnchecked THEN RETURN END;
  3456. IF tagsAvailable THEN
  3457. TrapC(BrltL,index,length,IndexCheckTrap);
  3458. END;
  3459. END BoundCheck;
  3460. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3461. VAR d: IntermediateCode.Operand;
  3462. BEGIN
  3463. IF isUnchecked THEN RETURN END;
  3464. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3465. TrapC(op,dim,d,ArraySizeTrap);
  3466. ReleaseIntermediateOperand(d);
  3467. END DimensionCheck;
  3468. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3469. VAR
  3470. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3471. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3472. expression: SyntaxTree.Expression;
  3473. resultingType, leftType, baseType: SyntaxTree.Type;
  3474. skipLabel1: Label;
  3475. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3476. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3477. variableOp: Operand;
  3478. variable: SyntaxTree.Variable;
  3479. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3480. VAR expression: SyntaxTree.Expression;
  3481. BEGIN
  3482. (* count the number of indices, ranges and tensorRanges in the index list *)
  3483. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3484. FOR i := 0 TO parameters.Length() - 1 DO
  3485. expression := parameters.GetExpression(i);
  3486. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3487. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3488. ELSE INC(indexCount)
  3489. END
  3490. END;
  3491. END CountIndices;
  3492. BEGIN
  3493. ASSERT(tagsAvailable);
  3494. resultingType := x.type.resolved; (* resulting type *)
  3495. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3496. InitOperand(localResult, ModeReference);
  3497. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3498. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3499. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3500. (* a globally defined array destination tag is available -> use and invalidate it*)
  3501. localResult.tag := arrayDestinationTag;
  3502. IntermediateCode.InitOperand(arrayDestinationTag)
  3503. ELSE
  3504. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3505. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3506. Symbol(variable, variableOp);
  3507. ReuseCopy(localResult.tag, variableOp.op);
  3508. ReleaseOperand(variableOp);
  3509. END
  3510. END;
  3511. indexListSize := x.parameters.Length();
  3512. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3513. ASSERT(tensorRangeCount <= 1);
  3514. (* designate the array to be indexed over, perform tensor range check if known *)
  3515. Designate(x.left, array);
  3516. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3517. Dereference(array, leftType,FALSE);
  3518. IF tensorRangeCount=0 THEN
  3519. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3520. END
  3521. END;
  3522. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3523. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3524. srcDimOffset := -indexListSize;
  3525. destDimOffset := -rangeCount
  3526. ELSE
  3527. srcDimOffset := 0;
  3528. destDimOffset := 0
  3529. END;
  3530. indexDim := 0;
  3531. (* use address of source array as basis *)
  3532. (*
  3533. ReuseCopy(localResult.op, array.op);
  3534. *)
  3535. localResult.op := array.op;
  3536. UseIntermediateOperand(localResult.op);
  3537. (* go through the index list *)
  3538. FOR i := 0 TO indexListSize - 1 DO
  3539. expression := x.parameters.GetExpression(i);
  3540. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3541. (* nothing to do *)
  3542. ELSE
  3543. (* determine which dimension of source array is currently looked at *)
  3544. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3545. (* get the memory operand pointing to array descriptor dimension *)
  3546. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3547. (* make a reusable register from it *)
  3548. ReuseCopy(srcDim, tmp);
  3549. ReleaseIntermediateOperand(tmp);
  3550. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3551. ELSE
  3552. srcDim := IntermediateCode.Immediate(int32, i)
  3553. END;
  3554. (* get length and increment of source array for current dimension *)
  3555. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3556. Convert(sourceLength.op, sizeType);
  3557. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3558. Convert(sourceIncrement.op, sizeType);
  3559. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3560. ReleaseIntermediateOperand(srcDim);
  3561. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3562. (* SINGLE INDEX *)
  3563. Evaluate(expression, index);
  3564. ReleaseIntermediateOperand(index.tag);
  3565. index.tag := emptyOperand;
  3566. Convert(index.op, sizeType);
  3567. (* lower bound check *)
  3568. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3569. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3570. ELSIF isUnchecked THEN (* do nothing *)
  3571. ELSE
  3572. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3573. END;
  3574. (* upper bound check *)
  3575. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3576. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3577. ELSIF isUnchecked THEN (* do nothing *)
  3578. ELSE
  3579. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3580. END;
  3581. ReleaseOperand(sourceLength);
  3582. Convert(index.op, addressType);
  3583. summand := index.op;
  3584. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3585. (* RANGE OF INDICES *)
  3586. Evaluate(expression, range);
  3587. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3588. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3589. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3590. ReleaseOperand(range);
  3591. Convert(firstIndex, sizeType);
  3592. Convert(lastIndex, sizeType);
  3593. Convert(stepSize, sizeType);
  3594. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3595. if it is 'MAX(LONGINT)' *)
  3596. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3597. TransferToRegister(lastIndex, lastIndex);
  3598. skipLabel1 := NewLabel();
  3599. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3600. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3601. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3602. SetLabel(skipLabel1)
  3603. END;
  3604. (* check if step size is valid *)
  3605. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3606. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3607. ELSIF isUnchecked THEN (* do nothing *)
  3608. ELSE
  3609. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3610. END;
  3611. (* check lower bound check *)
  3612. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3613. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3614. ELSIF isUnchecked THEN (* do nothing *)
  3615. ELSE
  3616. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3617. END;
  3618. (* check upper bound check *)
  3619. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3620. (* statically open range: nothing to do *)
  3621. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3622. ASSERT(staticLastIndex < staticSourceLength)
  3623. ELSIF isUnchecked THEN (* do nothing *)
  3624. ELSE
  3625. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3626. END;
  3627. (* determine length of target array for current dimension *)
  3628. (* 1. incorporate last index: *)
  3629. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3630. (* last index is static *)
  3631. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3632. targetLength := sourceLength.op
  3633. ELSE
  3634. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3635. END;
  3636. UseIntermediateOperand(targetLength);
  3637. ELSE
  3638. (* targetLength := lastIndex + 1
  3639. Reuse1(targetLength, lastIndex);
  3640. *)
  3641. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3642. END;
  3643. ReleaseOperand(sourceLength);
  3644. ReleaseIntermediateOperand(lastIndex);
  3645. (* 2. incorporate first index: *)
  3646. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3647. (* first index and current target length are static *)
  3648. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3649. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3650. (* first index = 0: nothing to do *)
  3651. ELSE
  3652. (* targetLength := targetLength - firstIndex *)
  3653. TransferToRegister(targetLength, targetLength);
  3654. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3655. END;
  3656. (* clip negative lengths to 0 *)
  3657. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3658. IF staticTargetLength < 0 THEN
  3659. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3660. END
  3661. ELSE
  3662. skipLabel1 := NewLabel();
  3663. TransferToRegister(targetLength, targetLength);
  3664. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3665. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3666. SetLabel(skipLabel1)
  3667. END;
  3668. (* 3. incorporate index step size: *)
  3669. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3670. (*step size and current target length are static *)
  3671. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3672. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3673. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3674. (* step size = 1: nothing to do *)
  3675. ELSE
  3676. (* emit code for this:
  3677. targetLength := (targetLength-1) DIV stepSize +1;
  3678. *)
  3679. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3680. DivInt(targetLength, targetLength, stepSize);
  3681. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3682. END;
  3683. (* determine increment of target array for current dimension *)
  3684. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3685. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3686. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3687. (* step size = 1 *)
  3688. targetIncrement := sourceIncrement.op;
  3689. UseIntermediateOperand(targetIncrement)
  3690. ELSE
  3691. (* targetIncrement := sourceIncrement * stepSize *)
  3692. Reuse1(targetIncrement, stepSize);
  3693. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3694. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3695. END;
  3696. ReleaseIntermediateOperand(stepSize);
  3697. (* write length and increment of target array to descriptor *)
  3698. IF destDimOffset < 0 THEN
  3699. (* determine which dimension of target array is currently looked at *)
  3700. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3701. TransferToRegister(destDim, tmp);
  3702. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3703. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3704. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3705. ReleaseIntermediateOperand(destDim)
  3706. ELSE
  3707. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3708. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3709. END;
  3710. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3711. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3712. INC(indexDim);
  3713. Convert(firstIndex, addressType);
  3714. TransferToRegister(summand, firstIndex);
  3715. ELSE HALT(100);
  3716. END;
  3717. (*
  3718. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3719. *)
  3720. Convert(sourceIncrement.op, addressType);
  3721. Convert(summand, addressType);
  3722. MulInt(summand, summand, sourceIncrement.op);
  3723. ReleaseIntermediateOperand(sourceIncrement.op);
  3724. AddInt(localResult.op, localResult.op, summand);
  3725. ReleaseIntermediateOperand(summand);
  3726. END
  3727. END;
  3728. result := localResult;
  3729. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3730. ReleaseIntermediateOperand(result.tag);
  3731. result.tag := TypeDescriptorAdr(resultingType);
  3732. IF ~newObjectFile THEN
  3733. IntermediateCode.MakeMemory(result.tag,addressType);
  3734. END;
  3735. ELSIF IsDelegate(resultingType) THEN
  3736. ReleaseIntermediateOperand(result.tag);
  3737. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3738. UseIntermediateOperand(result.tag);
  3739. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3740. ReleaseIntermediateOperand(result.tag);
  3741. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3742. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3743. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3744. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3745. (* finalize target array descriptor *)
  3746. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3747. (* write lengths and increments of target array for remaining dimensions *)
  3748. i := indexListSize;
  3749. WHILE indexDim < targetArrayDimensionality DO
  3750. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3751. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3752. ReleaseOperand(sourceLength);
  3753. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3754. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3755. ReleaseOperand(sourceIncrement);
  3756. INC(i); INC(indexDim);
  3757. END;
  3758. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3759. tmp := nil;
  3760. ELSE
  3761. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3762. END;
  3763. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3764. ReleaseIntermediateOperand(tmp);
  3765. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3766. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3767. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3768. ELSE
  3769. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3770. END;
  3771. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3772. ReleaseIntermediateOperand(tmp);
  3773. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3774. (* write dimensionality *)
  3775. IF targetArrayDimensionality # 0 THEN
  3776. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3777. END;
  3778. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3779. END;
  3780. ReleaseOperand(array);
  3781. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3782. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3783. END;
  3784. END MathIndexDesignator;
  3785. (* get the length of an array , trying to make use of static information *)
  3786. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3787. VAR res: IntermediateCode.Operand; size: LONGINT;
  3788. BEGIN
  3789. type := type.resolved;
  3790. IF type IS SyntaxTree.ArrayType THEN
  3791. WITH type: SyntaxTree.ArrayType DO
  3792. IF type.form = SyntaxTree.Static THEN
  3793. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3794. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3795. Evaluate(type.length, op);
  3796. ReleaseIntermediateOperand(op.tag);
  3797. RETURN op.op;*)
  3798. ELSE
  3799. res := tag;
  3800. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3801. IntermediateCode.MakeMemory(res,addressType);
  3802. UseIntermediateOperand(res);
  3803. RETURN res
  3804. END
  3805. END;
  3806. ELSE
  3807. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3808. RETURN IntermediateCode.Immediate(addressType,size);
  3809. END;
  3810. END ArrayLength;
  3811. PROCEDURE CopyInt(VAR res: IntermediateCode.Operand; x: IntermediateCode.Operand);
  3812. BEGIN
  3813. IF IsImmediate(x) THEN
  3814. IntermediateCode.InitImmediate(res,x.type,x.intValue);
  3815. ELSE
  3816. IF ~ReusableRegister(res) THEN
  3817. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3818. ELSE
  3819. UseIntermediateOperand(res);
  3820. END;
  3821. Emit(Mov(position,res,x))
  3822. END;
  3823. END CopyInt;
  3824. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3825. BEGIN
  3826. ReleaseIntermediateOperand(res);
  3827. IF IsImmediate(x) & IsImmediate(y) THEN
  3828. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3829. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3830. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3831. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3832. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3833. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3834. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3835. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3836. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3837. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3838. UseIntermediateOperand(res);
  3839. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3840. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3841. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3842. UseIntermediateOperand(res);
  3843. ELSE
  3844. IF ~ReusableRegister(res) THEN
  3845. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3846. ELSE
  3847. UseIntermediateOperand(res);
  3848. END;
  3849. IF IsImmediate(x) & (x.intValue = 0) THEN
  3850. Emit(Mov(position,res,y))
  3851. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3852. Emit(Mov(position,res,x))
  3853. ELSE
  3854. Emit(Add(position,res, x, y));
  3855. END;
  3856. END;
  3857. END AddInt;
  3858. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3859. BEGIN
  3860. ReleaseIntermediateOperand(res);
  3861. IF IsImmediate(x) & IsImmediate(y) THEN
  3862. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3863. ELSE
  3864. IF ~ReusableRegister(res) THEN
  3865. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3866. ELSE UseIntermediateOperand(res);
  3867. END;
  3868. IF IsImmediate(x) & (x.intValue = 1) THEN
  3869. Emit(Mov(position,res,y))
  3870. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3871. Emit(Mov(position,res,x))
  3872. ELSE
  3873. Emit(Mul(position,res, x, y));
  3874. END;
  3875. END;
  3876. END MulInt;
  3877. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3878. BEGIN
  3879. ReleaseIntermediateOperand(res);
  3880. IF IsImmediate(x) & IsImmediate(y) THEN
  3881. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3882. ELSE
  3883. IF ~ReusableRegister(res) THEN
  3884. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3885. ELSE UseIntermediateOperand(res);
  3886. END;
  3887. IF IsImmediate(x) & (x.intValue = 1) THEN
  3888. Emit(Mov(position,res,y))
  3889. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3890. Emit(Mov(position,res,x))
  3891. ELSE
  3892. Emit(Div(position,res, x, y));
  3893. END;
  3894. END;
  3895. END DivInt;
  3896. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3897. VAR length,res: IntermediateCode.Operand; type,ttype: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3898. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3899. BEGIN
  3900. type := x.left.type.resolved;
  3901. IF type IS SyntaxTree.StringType THEN
  3902. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  3903. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3904. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, type(SyntaxTree.StringType).length));
  3905. type := atype;
  3906. x.left.SetType(type);
  3907. END;
  3908. IntermediateCode.InitImmediate(res,addressType,0);
  3909. maxDim := x.parameters.Length()-1;
  3910. (*
  3911. computation rule:
  3912. a: ARRAY X,Y,Z OF Element with size S
  3913. a[i,j,k] -->
  3914. ( ( ( ( i ) * Y + j ) * Z) + k) * S
  3915. *)
  3916. FOR i := 0 TO maxDim DO
  3917. e := x.parameters.GetExpression(i);
  3918. Evaluate(e,index);
  3919. Convert(index.op,addressType);
  3920. AddInt(res, res, index.op);
  3921. IF i = 0 THEN
  3922. (*
  3923. ReuseCopy(res, index.op);
  3924. *)
  3925. Designate(x.left,array);
  3926. type := x.left.type.resolved;
  3927. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3928. Dereference(array, type, FALSE);
  3929. END;
  3930. (*
  3931. ELSE AddInt(res, res, index.op);
  3932. *)
  3933. END;
  3934. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3935. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3936. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3937. BoundCheck(index.op, length);
  3938. END;
  3939. ReleaseIntermediateOperand(length);
  3940. END;
  3941. ReleaseOperand(index);
  3942. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3943. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3944. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3945. MulInt(res,res,length);
  3946. END;
  3947. ReleaseIntermediateOperand(length);
  3948. END;
  3949. (* remaining open dimensions -- compute address *)
  3950. i := maxDim+1;
  3951. IF type IS SyntaxTree.ArrayType THEN
  3952. ttype := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3953. WHILE (ttype IS SyntaxTree.ArrayType) & (ttype(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  3954. length := ArrayLength(ttype,array.dimOffset+i-1,array.tag);
  3955. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3956. MulInt(res,res,length);
  3957. END;
  3958. ReleaseIntermediateOperand(length);
  3959. INC(i);
  3960. ttype := ttype(SyntaxTree.ArrayType).arrayBase.resolved;
  3961. END;
  3962. END;
  3963. IF (type IS SyntaxTree.ArrayType) THEN
  3964. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3965. size := StaticSize(system, type);
  3966. IF size # 1 THEN
  3967. length := IntermediateCode.Immediate(addressType,size);
  3968. MulInt(res,res,length);
  3969. END;
  3970. ELSE
  3971. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3972. IF size # 1 THEN
  3973. length := IntermediateCode.Immediate(addressType,size);
  3974. MulInt(res,res,length);
  3975. END;
  3976. END;
  3977. END;
  3978. AddInt(res,res,array.op);
  3979. InitOperand(result,ModeReference);
  3980. result.op := res;
  3981. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3982. ReleaseIntermediateOperand(result.tag);
  3983. result.tag := TypeDescriptorAdr(type);
  3984. IF ~newObjectFile THEN
  3985. IntermediateCode.MakeMemory(result.tag,addressType);
  3986. END
  3987. ELSIF IsDelegate(type) THEN
  3988. ReleaseIntermediateOperand(result.tag);
  3989. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3990. UseIntermediateOperand(result.tag);
  3991. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3992. ReleaseIntermediateOperand(result.tag);
  3993. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3994. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3995. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3996. END;
  3997. ReleaseOperand(array);
  3998. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3999. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  4000. END;
  4001. END IndexDesignator;
  4002. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  4003. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  4004. BEGIN
  4005. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  4006. dest := destination; destination := emptyOperand;
  4007. type := x.left.type.resolved;
  4008. IF type IS SyntaxTree.MathArrayType THEN
  4009. MathIndexDesignator(x);
  4010. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  4011. IndexDesignator(x);
  4012. END;
  4013. destination := dest;
  4014. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  4015. END VisitIndexDesignator;
  4016. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  4017. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  4018. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  4019. procedure: SyntaxTree.Procedure;
  4020. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  4021. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  4022. BEGIN
  4023. variable := GetTemporaryVariable(expression.left.type, FALSE);
  4024. parameters := expression.parameters;
  4025. moduleName := "FoxArrayBase";
  4026. procedureName := "CopyDescriptor";
  4027. IF AddImport(moduleName,arrayBase,TRUE) THEN
  4028. SaveRegisters();ReleaseUsedRegisters(saved);
  4029. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  4030. IF procedure = NIL THEN
  4031. s := "procedure ";
  4032. Strings.Append(s,moduleName);
  4033. Strings.Append(s,".");
  4034. Strings.Append(s,procedureName);
  4035. Strings.Append(s," not present");
  4036. Error(position,s);
  4037. ELSE
  4038. (* push address of temporary variable *)
  4039. Symbol(variable,destOperand);
  4040. Emit(Push(position,destOperand.op));
  4041. ReleaseOperand(destOperand);
  4042. (* push src *)
  4043. Evaluate(expression.left,srcOperand);
  4044. (*
  4045. Dereference(srcOperand,expression.type.resolved);
  4046. Emit(Push(position,srcOperand.tag));
  4047. *)
  4048. Emit(Push(position,srcOperand.op));
  4049. ReleaseOperand(srcOperand);
  4050. tensorFound := FALSE;
  4051. FOR i := 0 TO parameters.Length()-1 DO
  4052. e := parameters.GetExpression(i);
  4053. IF e IS SyntaxTree.TensorRangeExpression THEN
  4054. tensorFound := TRUE;
  4055. ELSIF e IS SyntaxTree.RangeExpression THEN
  4056. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  4057. ELSE
  4058. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  4059. END;
  4060. END;
  4061. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  4062. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  4063. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  4064. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  4065. StaticCallOperand(procOp,procedure);
  4066. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  4067. ReleaseOperand(procOp);
  4068. END;
  4069. RestoreRegisters(saved);
  4070. END;
  4071. RETURN variable
  4072. END PrepareTensorDescriptor;
  4073. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  4074. VAR
  4075. type, descriptorType, baseType: SyntaxTree.Type;
  4076. operand, tmpOperand, variableOp, variable2Op: Operand;
  4077. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  4078. variable, variable2: SyntaxTree.Variable;
  4079. dim, i, size: LONGINT;
  4080. (* TODO: needed? *)
  4081. oldArrayDestinationTag: IntermediateCode.Operand;
  4082. oldArrayDestinationDimension: LONGINT;
  4083. position: Position;
  4084. saved: RegisterEntry;
  4085. arrayFlags: SET;
  4086. m, n: LONGINT;
  4087. PROCEDURE Pass(op: IntermediateCode.Operand);
  4088. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  4089. BEGIN
  4090. IF numberRegister >= 0 THEN
  4091. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  4092. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  4093. Emit(Mov(position,parameterRegister, op));
  4094. ELSE
  4095. Emit(Push(position,op))
  4096. END
  4097. END Pass;
  4098. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  4099. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  4100. BEGIN
  4101. formalType := formalType.resolved; actualType := actualType.resolved;
  4102. IF IsOpenArray(formalType)THEN
  4103. IF actualType IS SyntaxTree.StringType THEN
  4104. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  4105. RETURN;
  4106. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  4107. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  4108. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  4109. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  4110. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  4111. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4112. ELSE
  4113. tmp := baseReg;
  4114. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  4115. IntermediateCode.MakeMemory(tmp,addressType);
  4116. Pass((tmp));
  4117. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  4118. END;
  4119. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  4120. END;
  4121. END PushArrayLens;
  4122. PROCEDURE SetSmallArraySizeFlag(VAR flags: SET; size: LONGINT);
  4123. BEGIN
  4124. CASE size OF
  4125. |2: INCL(flags,Size2Flag);
  4126. |3: INCL(flags,Size3Flag);
  4127. |4: INCL(flags,Size4Flag);
  4128. |5: INCL(flags,Size5Flag);
  4129. |6: INCL(flags,Size6Flag);
  4130. |7: INCL(flags,Size7Flag);
  4131. |8: INCL(flags,Size8Flag);
  4132. END;
  4133. END SetSmallArraySizeFlag;
  4134. BEGIN
  4135. IF Trace THEN TraceEnter("PushParameter") END;
  4136. position := expression.position;
  4137. IF expression.resolved # NIL THEN expression := expression.resolved END;
  4138. type := expression.type.resolved;
  4139. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  4140. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  4141. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  4142. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  4143. );
  4144. (* TODO: needed? *)
  4145. oldArrayDestinationTag := arrayDestinationTag;
  4146. oldArrayDestinationDimension := arrayDestinationDimension;
  4147. IF IsArrayOfSystemByte(parameter.type) THEN
  4148. IF SemanticChecker.HasAddress(expression) OR (callingConvention = SyntaxTree.WinAPICallingConvention) & (expression IS SyntaxTree.NilValue) THEN
  4149. Designate(expression,operand);
  4150. ELSE
  4151. Evaluate(expression, tmpOperand);
  4152. variable := GetTemporaryVariable(expression.type, FALSE);
  4153. Symbol(variable, operand);
  4154. MakeMemory(tmp,operand.op,tmpOperand.op.type,0);
  4155. Emit(Mov(position,tmp, tmpOperand.op));
  4156. ReleaseOperand(tmpOperand);
  4157. END;
  4158. tmp := GetArrayOfBytesSize(expression,operand.tag);
  4159. ReleaseIntermediateOperand(operand.tag);
  4160. operand.tag := tmp;
  4161. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4162. Pass((operand.tag));
  4163. END;
  4164. Pass((operand.op));
  4165. ELSIF IsOpenArray(parameter.type) THEN
  4166. Designate(expression,operand);
  4167. baseReg := operand.tag;
  4168. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4169. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4170. END;
  4171. Pass((operand.op)); (* address of the array *)
  4172. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  4173. (* case 1 *)
  4174. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  4175. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  4176. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4177. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  4178. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4179. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4180. arrayDestinationTag := sp;
  4181. (* case 1b *)
  4182. IF expression IS SyntaxTree.IndexDesignator THEN
  4183. (*
  4184. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4185. descriptorType := GetMathArrayDescriptorType(dim);
  4186. variable := GetTemporaryVariable(descriptorType,expression.position);
  4187. Symbol(variable,variableOp);
  4188. arrayDestinationTag := variableOp.op;
  4189. *)
  4190. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4191. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4192. arrayDestinationDimension := dim;
  4193. Designate(expression,operand);
  4194. (* case 1a *)
  4195. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4196. Designate(expression,operand);
  4197. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  4198. i := 0;
  4199. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4200. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4201. INC(i);
  4202. END;
  4203. type := expression.type.resolved;
  4204. WHILE (i<dim) DO (* remaining static dimensions *)
  4205. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4206. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4207. ReleaseOperand(tmpOperand);
  4208. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4209. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4210. ReleaseOperand(tmpOperand);
  4211. INC(i);
  4212. END;
  4213. dimOp := IntermediateCode.Immediate(addressType,dim);
  4214. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4215. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4216. (* case 1d *)
  4217. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4218. Designate(expression,operand);
  4219. Dereference(operand,type.resolved,FALSE);
  4220. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4221. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  4222. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4223. (* case 1f *)
  4224. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4225. Designate(expression,operand);
  4226. FOR i := 0 TO dim-1 DO
  4227. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4228. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4229. ReleaseOperand(tmpOperand);
  4230. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4231. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4232. ReleaseOperand(tmpOperand);
  4233. END;
  4234. (*******
  4235. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4236. *)
  4237. arrayFlags := {StaticFlag};
  4238. IF dim = 1 THEN
  4239. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4240. ReleaseOperand(tmpOperand);
  4241. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4242. m := LONGINT(tmpOperand.op.intValue);
  4243. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4244. ELSIF dim = 2 THEN
  4245. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4246. ReleaseOperand(tmpOperand);
  4247. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4248. m := LONGINT(tmpOperand.op.intValue);
  4249. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4250. ReleaseOperand(tmpOperand);
  4251. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4252. n := LONGINT(tmpOperand.op.intValue);
  4253. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4254. INCL(arrayFlags,SmallMatrixFlag);
  4255. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4256. END;
  4257. END;
  4258. (*******)
  4259. dimOp := IntermediateCode.Immediate(addressType,dim);
  4260. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4261. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4262. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4263. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4264. baseType := SemanticChecker.ArrayBase(type,dim);
  4265. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4266. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4267. ELSE HALT(100);
  4268. END;
  4269. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4270. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4271. (* case 2b *)
  4272. IF expression IS SyntaxTree.IndexDesignator THEN
  4273. descriptorType := GetMathArrayDescriptorType(dim);
  4274. variable := GetTemporaryVariable(descriptorType, FALSE);
  4275. Symbol(variable,variableOp);
  4276. arrayDestinationTag := variableOp.op;
  4277. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4278. arrayDestinationDimension := dim;
  4279. NeedDescriptor := TRUE;
  4280. Designate(expression,operand);
  4281. Pass((operand.tag));
  4282. NeedDescriptor := FALSE;
  4283. (* case 2a *)
  4284. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4285. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4286. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4287. INC(i);
  4288. END;
  4289. IF i = dim THEN
  4290. Designate(expression,operand);
  4291. Pass((operand.tag));
  4292. ELSE (* open-static *)
  4293. type := expression.type.resolved;
  4294. descriptorType := GetMathArrayDescriptorType(dim);
  4295. variable := GetTemporaryVariable(descriptorType, FALSE);
  4296. Symbol(variable,variableOp);
  4297. arrayDestinationTag := variableOp.op;
  4298. Designate(expression,operand);
  4299. FOR i := 0 TO dim-1 DO
  4300. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4301. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4302. ReleaseOperand(tmpOperand);
  4303. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4304. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4305. ReleaseOperand(tmpOperand);
  4306. END;
  4307. dimOp := IntermediateCode.Immediate(addressType,dim);
  4308. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4309. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4310. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4311. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4312. baseType := SemanticChecker.ArrayBase(type,dim);
  4313. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4314. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4315. Pass((arrayDestinationTag));
  4316. END;
  4317. (* case 2d *)
  4318. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4319. Designate(expression,operand);
  4320. Dereference(operand,type.resolved,FALSE);
  4321. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4322. Pass((operand.tag));
  4323. (* case 2f *)
  4324. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4325. descriptorType := GetMathArrayDescriptorType(dim);
  4326. variable := GetTemporaryVariable(descriptorType, FALSE);
  4327. Symbol(variable,variableOp);
  4328. arrayDestinationTag := variableOp.op;
  4329. Designate(expression,operand);
  4330. FOR i := 0 TO dim-1 DO
  4331. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4332. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4333. ReleaseOperand(tmpOperand);
  4334. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4335. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4336. ReleaseOperand(tmpOperand);
  4337. END;
  4338. (*
  4339. identify the cases of small vector and matrices, used for optimizations in FoxArrayBase module (Alexey Morozov)
  4340. *)
  4341. arrayFlags := {StaticFlag};
  4342. IF dim = 1 THEN
  4343. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4344. ReleaseOperand(tmpOperand);
  4345. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4346. m := LONGINT(tmpOperand.op.intValue);
  4347. IF (m >= 2) & (m <= 8) THEN INCL(arrayFlags,SmallVectorFlag); SetSmallArraySizeFlag(arrayFlags,m); END;
  4348. ELSIF dim = 2 THEN
  4349. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,0,FALSE,tmpOperand);
  4350. ReleaseOperand(tmpOperand);
  4351. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4352. m := LONGINT(tmpOperand.op.intValue);
  4353. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,1,FALSE,tmpOperand);
  4354. ReleaseOperand(tmpOperand);
  4355. ASSERT(tmpOperand.op.mode = IntermediateCode.ModeImmediate);
  4356. n := LONGINT(tmpOperand.op.intValue);
  4357. IF (m >= 2) & (m <= 8) & (n >= 2) & (n <= 8) THEN
  4358. INCL(arrayFlags,SmallMatrixFlag);
  4359. IF m = n THEN SetSmallArraySizeFlag(arrayFlags,m); END;
  4360. END;
  4361. END;
  4362. (*******)
  4363. dimOp := IntermediateCode.Immediate(addressType,dim);
  4364. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4365. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4366. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4367. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,arrayFlags)),MathFlagsOffset);
  4368. baseType := SemanticChecker.ArrayBase(type,dim);
  4369. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4370. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4371. Pass((arrayDestinationTag));
  4372. ELSE HALT(100);
  4373. END;
  4374. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4375. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4376. (* case 3b *)
  4377. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4378. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4379. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4380. Symbol(variable,variableOp);
  4381. LoadValue(variableOp,system.addressType);
  4382. ELSE
  4383. descriptorType := GetMathArrayDescriptorType(dim);
  4384. variable := GetTemporaryVariable(descriptorType, FALSE);
  4385. Symbol(variable,variableOp);
  4386. END;
  4387. arrayDestinationTag := variableOp.op;
  4388. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4389. arrayDestinationDimension := 0;
  4390. Designate(expression,operand);
  4391. Pass((operand.tag));
  4392. (* case 3a *)
  4393. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4394. i := 0;
  4395. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4396. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4397. INC(i);
  4398. END;
  4399. IF i = dim THEN
  4400. Designate(expression,operand);
  4401. Pass((operand.tag));
  4402. ELSE (* open-static *)
  4403. type := expression.type.resolved;
  4404. descriptorType := GetMathArrayDescriptorType(dim);
  4405. variable := GetTemporaryVariable(descriptorType, FALSE);
  4406. Symbol(variable,variableOp);
  4407. arrayDestinationTag := variableOp.op;
  4408. Designate(expression,operand);
  4409. FOR i := 0 TO dim-1 DO
  4410. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4411. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4412. ReleaseOperand(tmpOperand);
  4413. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4414. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4415. ReleaseOperand(tmpOperand);
  4416. END;
  4417. dimOp := IntermediateCode.Immediate(addressType,dim);
  4418. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4419. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4420. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4421. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4422. baseType := SemanticChecker.ArrayBase(type,dim);
  4423. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4424. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4425. Pass((arrayDestinationTag));
  4426. END;
  4427. (* case 3d *)
  4428. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4429. Designate(expression,operand);
  4430. Dereference(operand,type.resolved,FALSE);
  4431. (*
  4432. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4433. *)
  4434. Pass((operand.tag));
  4435. (* case 3f *)
  4436. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4437. descriptorType := GetMathArrayDescriptorType(dim);
  4438. variable := GetTemporaryVariable(descriptorType, FALSE);
  4439. Symbol(variable,variableOp);
  4440. arrayDestinationTag := variableOp.op;
  4441. Designate(expression,operand);
  4442. IF operand.op.type.length >1 THEN (* vector register *)
  4443. variable2 := GetTemporaryVariable(type, FALSE);
  4444. Symbol(variable2, variable2Op);
  4445. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4446. Emit(Mov(position,tmp, operand.op));
  4447. ReleaseOperand(operand);
  4448. Symbol(variable2, operand);
  4449. END;
  4450. FOR i := 0 TO dim-1 DO
  4451. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4452. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4453. ReleaseOperand(tmpOperand);
  4454. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4455. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4456. ReleaseOperand(tmpOperand);
  4457. END;
  4458. dimOp := IntermediateCode.Immediate(addressType,dim);
  4459. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4460. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4461. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4462. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4463. baseType := SemanticChecker.ArrayBase(type,dim);
  4464. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4465. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4466. Pass((arrayDestinationTag));
  4467. ELSE HALT(100);
  4468. END;
  4469. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4470. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4471. (* case 4b *)
  4472. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4473. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4474. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4475. Symbol(variable,variableOp);
  4476. LoadValue(variableOp,system.addressType);
  4477. ELSE
  4478. descriptorType := GetMathArrayDescriptorType(dim);
  4479. variable := GetTemporaryVariable(descriptorType, FALSE);
  4480. Symbol(variable,variableOp);
  4481. END;
  4482. arrayDestinationTag := variableOp.op;
  4483. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4484. arrayDestinationDimension := 0;
  4485. Designate(expression,operand);
  4486. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4487. Symbol(variable,variableOp);
  4488. ELSE
  4489. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4490. Symbol(variable,variableOp);
  4491. MakeMemory(tmp,variableOp.op,addressType,0);
  4492. Emit(Mov(position,tmp,operand.tag));
  4493. ReleaseIntermediateOperand(tmp);
  4494. END;
  4495. Pass((variableOp.op));
  4496. ReleaseOperand(variableOp);
  4497. (* case 4a *)
  4498. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4499. i := 0;
  4500. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4501. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4502. INC(i);
  4503. END;
  4504. IF i = dim THEN
  4505. Designate(expression,operand);
  4506. arrayDestinationTag := operand.tag;
  4507. ELSE (* open-static *)
  4508. type := expression.type.resolved;
  4509. descriptorType := GetMathArrayDescriptorType(dim);
  4510. variable := GetTemporaryVariable(descriptorType, FALSE);
  4511. Symbol(variable,variableOp);
  4512. arrayDestinationTag := variableOp.op;
  4513. Designate(expression,operand);
  4514. FOR i := 0 TO dim-1 DO
  4515. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4516. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4517. ReleaseOperand(tmpOperand);
  4518. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4519. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4520. ReleaseOperand(tmpOperand);
  4521. END;
  4522. dimOp := IntermediateCode.Immediate(addressType,dim);
  4523. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4524. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4525. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4526. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4527. baseType := SemanticChecker.ArrayBase(type,dim);
  4528. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4529. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4530. END;
  4531. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4532. Symbol(variable,variableOp);
  4533. MakeMemory(tmp,variableOp.op,addressType,0);
  4534. Emit(Mov(position,tmp,arrayDestinationTag));
  4535. ReleaseIntermediateOperand(tmp);
  4536. Pass((variableOp.op));
  4537. ReleaseOperand(variableOp);
  4538. (* case 4d *)
  4539. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4540. Designate(expression,operand);
  4541. (*
  4542. Dereference(operand,type.resolved,FALSE);
  4543. *)
  4544. (*
  4545. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4546. *)
  4547. Pass((operand.op));
  4548. (* case 4f *)
  4549. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4550. descriptorType := GetMathArrayDescriptorType(dim);
  4551. variable := GetTemporaryVariable(descriptorType, FALSE);
  4552. Symbol(variable,variableOp);
  4553. arrayDestinationTag := variableOp.op;
  4554. Designate(expression,operand);
  4555. FOR i := 0 TO dim-1 DO
  4556. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4557. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4558. ReleaseOperand(tmpOperand);
  4559. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4560. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4561. ReleaseOperand(tmpOperand);
  4562. END;
  4563. dimOp := IntermediateCode.Immediate(addressType,dim);
  4564. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4565. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4566. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4567. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4568. baseType := SemanticChecker.ArrayBase(type,dim);
  4569. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4570. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4571. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4572. Symbol(variable,variableOp);
  4573. MakeMemory(tmp,variableOp.op,addressType,0);
  4574. Emit(Mov(position,tmp,arrayDestinationTag));
  4575. ReleaseIntermediateOperand(tmp);
  4576. Pass((variableOp.op));
  4577. ReleaseOperand(variableOp);
  4578. ELSE HALT(100);
  4579. END;
  4580. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4581. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4582. Designate(expression,operand);
  4583. IF operand.op.type.length > 1 THEN
  4584. Emit(Push(position, operand.op));
  4585. ELSE
  4586. size := system.SizeOf(type);
  4587. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4588. size := ToMemoryUnits(system,size);
  4589. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4590. arrayDestinationTag := sp;
  4591. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4592. END;
  4593. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4594. Designate(expression,operand);
  4595. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4596. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4597. Symbol(variable,variableOp);
  4598. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4599. Emit(Mov(position,tmp,operand.op));
  4600. Emit(Push(position,variableOp.op));
  4601. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4602. Pass((operand.op));
  4603. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4604. END;
  4605. ELSE HALT(200)
  4606. END;
  4607. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4608. IF parameter.kind = SyntaxTree.VarParameter THEN
  4609. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4610. Designate(expression, operand);
  4611. Pass((operand.op));
  4612. ELSE
  4613. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4614. Evaluate(expression, operand);
  4615. Pass((operand.extra)); (* step *)
  4616. Pass((operand.tag)); (* last *)
  4617. Pass((operand.op)) (* first *)
  4618. END
  4619. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4620. IF parameter.kind = SyntaxTree.VarParameter THEN
  4621. Designate(expression, operand);
  4622. Pass((operand.op));
  4623. ELSE
  4624. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4625. Evaluate(expression, operand);
  4626. Pass((operand.tag)); (* real part *)
  4627. Pass((operand.op)) (* imaginary part *)
  4628. END
  4629. ELSE
  4630. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4631. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4632. Designate(expression,operand);
  4633. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4634. (* stack allocation *)
  4635. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4636. (*! parameter alignment to be discussed ... *)
  4637. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4638. size := type(SyntaxTree.StringType).length;
  4639. END;
  4640. IF backend.cooperative & parameter.NeedsTrace() THEN
  4641. dst := NewRegisterOperand (addressType);
  4642. Emit(Mov(position,dst, sp));
  4643. IntermediateCode.InitImmediate(null, byteType, 0);
  4644. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4645. ReleaseIntermediateOperand(dst);
  4646. SaveRegisters();ReleaseUsedRegisters(saved);
  4647. (* register dst has been freed before SaveRegisters already *)
  4648. CallAssignMethod(dst, operand.op, parameter.type);
  4649. RestoreRegisters(saved);
  4650. END;
  4651. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4652. ELSIF IsOpenArray(parameter.type) THEN
  4653. Designate(expression,operand);
  4654. baseReg := operand.tag;
  4655. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4656. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4657. END;
  4658. Pass((operand.op)); (* address of the array *)
  4659. ELSIF IsDelegate(parameter.type) THEN
  4660. Evaluate(expression,operand);
  4661. IF backend.cooperative & parameter.NeedsTrace() THEN
  4662. Emit(Push(position, nil));
  4663. dst := NewRegisterOperand (addressType);
  4664. Emit(Mov(position,dst, sp));
  4665. ReleaseIntermediateOperand(dst);
  4666. SaveRegisters();ReleaseUsedRegisters(saved);
  4667. Emit(Push(position, dst));
  4668. (* register dst has been freed before SaveRegisters already *)
  4669. Emit(Push(position, operand.tag));
  4670. CallThis(position,"GarbageCollector","Assign",2);
  4671. RestoreRegisters(saved);
  4672. ELSE
  4673. Pass((operand.tag));
  4674. END;
  4675. Pass((operand.op));
  4676. ELSE
  4677. Evaluate(expression,operand);
  4678. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4679. Emit(Push(position, nil));
  4680. dst := NewRegisterOperand (addressType);
  4681. Emit(Mov(position,dst, sp));
  4682. ReleaseIntermediateOperand(dst);
  4683. SaveRegisters();ReleaseUsedRegisters(saved);
  4684. Emit(Push(position, dst));
  4685. (* register dst has been freed before SaveRegisters already *)
  4686. Emit(Push(position, operand.op));
  4687. CallThis(position,"GarbageCollector","Assign",2);
  4688. RestoreRegisters(saved);
  4689. ELSE
  4690. Pass((operand.op));
  4691. END;
  4692. END;
  4693. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4694. Evaluate(expression,operand);
  4695. Pass((operand.op));
  4696. ELSE (* var parameter *)
  4697. Designate(expression,operand);
  4698. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4699. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4700. Pass((operand.tag));
  4701. END;
  4702. END;
  4703. Pass((operand.op));
  4704. END;
  4705. END;
  4706. (* TODO: needed? *)
  4707. arrayDestinationTag := oldArrayDestinationTag;
  4708. arrayDestinationDimension := oldArrayDestinationDimension;
  4709. IF needsParameterBackup THEN
  4710. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4711. ReuseCopy(parameterBackup, operand.op)
  4712. END;
  4713. ReleaseOperand(operand);
  4714. IF Trace THEN TraceExit("PushParameter") END;
  4715. END PushParameter;
  4716. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4717. VAR prevConditional: BOOLEAN;
  4718. BEGIN
  4719. prevConditional := conditional;
  4720. conditional := FALSE;
  4721. IF (x.result # NIL) & ( x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4722. Expression(x.result); (* use register *)
  4723. END;
  4724. Statement(x.statement);
  4725. conditional := prevConditional;
  4726. IF x.result # NIL THEN Expression(x.result) END;
  4727. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4728. ReleaseIntermediateOperand(result.op);
  4729. END;
  4730. END VisitStatementDesignator;
  4731. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4732. VAR
  4733. procedure: SyntaxTree.Procedure;
  4734. procedureType: SyntaxTree.ProcedureType;
  4735. wasInline: BOOLEAN;
  4736. actualParameters: SyntaxTree.ExpressionList;
  4737. formalParameter: SyntaxTree.Parameter;
  4738. actualParameter: SyntaxTree.Expression;
  4739. i: LONGINT;
  4740. localVariable: SyntaxTree.Variable;
  4741. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4742. src, dest: Operand;
  4743. prevInlineExit : Label;
  4744. prevMapper: SymbolMapper;
  4745. tooComplex: BOOLEAN;
  4746. resultDesignator: SyntaxTree.Expression;
  4747. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4748. BEGIN
  4749. IF e = NIL THEN RETURN TRUE
  4750. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4751. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4752. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4753. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4754. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4755. ELSE RETURN FALSE
  4756. END;
  4757. END SimpleExpression;
  4758. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4759. BEGIN
  4760. RETURN checker.CanPassInRegister(type)
  4761. END FitsInRegister;
  4762. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4763. VAR
  4764. variable: SyntaxTree.Variable;
  4765. variableDesignator: SyntaxTree.Designator;
  4766. BEGIN
  4767. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4768. variableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4769. variableDesignator.SetType(type);
  4770. RETURN variableDesignator
  4771. END GetTemp;
  4772. BEGIN
  4773. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4774. wasInline := currentIsInline;
  4775. prevInlineExit := currentInlineExit;
  4776. prevMapper := currentMapper;
  4777. currentInlineExit := NewLabel();
  4778. tooComplex := FALSE;
  4779. NEW(currentMapper);
  4780. currentIsInline := TRUE;
  4781. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4782. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4783. formalParameter := procedureType.firstParameter;
  4784. actualParameters := x.parameters;
  4785. i := 0;
  4786. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4787. actualParameter := actualParameters.GetExpression(i);
  4788. IF actualParameter.resolved # NIL THEN
  4789. actualParameter := actualParameter.resolved
  4790. END;
  4791. (*
  4792. if expression is simple and can be passed immediately
  4793. or if type fits in register then we can proceed
  4794. otherwise we escape to ordinary procedure call.
  4795. *)
  4796. (* cases where the expression can be mapped identically *)
  4797. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4798. currentMapper.Add(formalParameter, actualParameter, NIL,FALSE);
  4799. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4800. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4801. (*
  4802. Assign(variableDesignator, actualParameter);
  4803. *)
  4804. Evaluate(actualParameter, src);
  4805. Designate(variableDesignator, dest);
  4806. Emit(Mov(x.position, dest.op, src.op));
  4807. ReleaseOperand(dest);
  4808. ReleaseOperand(src);
  4809. (* the src operand should now have been completely released ! *)
  4810. currentMapper.Add(formalParameter, variableDesignator, NIL, FALSE);
  4811. ELSE tooComplex := TRUE
  4812. END;
  4813. (*
  4814. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4815. currentMapper.Add(formalParameter, actualParameter, NIL);
  4816. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4817. variableDesignator := GetTemp(system.addressType, FALSE);
  4818. Designate(actualParameter, src);
  4819. Designate(variableDesignator, dest);
  4820. IntermediateCode.MakeMemory(dest.op,addressType);
  4821. Emit(Mov(x.position, dest.op, src.op));
  4822. ReleaseOperand(dest);
  4823. IF src.tag.mode # IntermediateCode.Undefined THEN
  4824. tagDesignator := GetTemp(system.addressType, FALSE);
  4825. Designate(tagDesignator, dest);
  4826. IntermediateCode.MakeMemory(dest.op,addressType);
  4827. Emit(Mov(x.position, dest.op, src.op));
  4828. END;
  4829. ReleaseOperand(dest); ReleaseOperand(src);
  4830. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4831. END;
  4832. *)
  4833. formalParameter := formalParameter.nextParameter;
  4834. INC(i);
  4835. END;
  4836. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4837. IF resultDesignator # NIL THEN
  4838. returnDesignator := resultDesignator
  4839. ELSE
  4840. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4841. END;
  4842. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  4843. END;
  4844. localVariable := procedure.procedureScope.firstVariable;
  4845. WHILE ~tooComplex & (localVariable # NIL) DO
  4846. variableDesignator := GetTemp(localVariable.type, FALSE);
  4847. currentMapper.Add(localVariable, variableDesignator, NIL, FALSE);
  4848. localVariable := localVariable.nextVariable;
  4849. END;
  4850. IF ~tooComplex THEN
  4851. VisitStatementBlock(procedure.procedureScope.body);
  4852. SetLabel(currentInlineExit);
  4853. IF procedureType.returnType # NIL THEN
  4854. Designate(returnDesignator, result);
  4855. IF conditional THEN
  4856. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4857. ValueToCondition(result)
  4858. END;
  4859. END;
  4860. END;
  4861. currentMapper := prevMapper;
  4862. currentInlineExit := prevInlineExit;
  4863. currentIsInline := wasInline;
  4864. RETURN ~tooComplex
  4865. END InlineProcedureCall;
  4866. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4867. VAR
  4868. parameters: SyntaxTree.ExpressionList;
  4869. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4870. designator: SyntaxTree.Designator;
  4871. procedureType: SyntaxTree.ProcedureType;
  4872. formalParameter: SyntaxTree.Parameter;
  4873. operand, returnValue: Operand;
  4874. reg, size, mask, dest: IntermediateCode.Operand;
  4875. saved: RegisterEntry;
  4876. symbol: SyntaxTree.Symbol;
  4877. variable: SyntaxTree.Variable;
  4878. i, parametersSize, returnTypeSize : LONGINT;
  4879. structuredReturnType: BOOLEAN;
  4880. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4881. tempVariableDesignator: SyntaxTree.Designator;
  4882. gap, alignment: LONGINT; (*fld*)
  4883. (* TODO: remove unnecessary backup variables *)
  4884. oldResult: Operand;
  4885. oldCurrentScope: SyntaxTree.Scope;
  4886. oldArrayDestinationTag: IntermediateCode.Operand;
  4887. oldArrayDestinationDimension: LONGINT;
  4888. oldConstantDeclaration: SyntaxTree.Symbol;
  4889. oldDestination: IntermediateCode.Operand;
  4890. oldCurrentLoop: Label;
  4891. oldConditional: BOOLEAN;
  4892. oldTrueLabel, oldFalseLabel: Label;
  4893. oldLocked: BOOLEAN;
  4894. usedRegisters,oldUsedRegisters: RegisterEntry;
  4895. return: IntermediateCode.Operand;
  4896. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4897. arg: IntermediateCode.Operand;
  4898. dummy: IntermediateCode.Operand;
  4899. recordType: SyntaxTree.RecordType;
  4900. operatorSelectionProcedureOperand: Operand;
  4901. operatorSelectionProcedure: SyntaxTree.Procedure;
  4902. fingerPrint: SyntaxTree.FingerPrint;
  4903. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4904. identifierNumber: LONGINT;
  4905. parameterRegister: Backend.Registers;
  4906. parameterRegisters: LONGINT;
  4907. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4908. procedure: SyntaxTree.Procedure;
  4909. PROCEDURE BackupGlobalState;
  4910. BEGIN
  4911. oldResult := result;
  4912. oldCurrentScope := currentScope;
  4913. oldArrayDestinationTag := arrayDestinationTag;
  4914. oldArrayDestinationDimension := arrayDestinationDimension;
  4915. oldConstantDeclaration := constantDeclaration;
  4916. oldDestination := destination;
  4917. oldCurrentLoop := currentLoop;
  4918. oldConditional := conditional;
  4919. oldTrueLabel := trueLabel;
  4920. oldFalseLabel := falseLabel;
  4921. oldLocked := locked;
  4922. oldUsedRegisters := usedRegisters
  4923. END BackupGlobalState;
  4924. PROCEDURE RestoreGlobalState;
  4925. BEGIN
  4926. result := oldResult;
  4927. currentScope := oldCurrentScope;
  4928. arrayDestinationTag := oldArrayDestinationTag;
  4929. arrayDestinationDimension := oldArrayDestinationDimension;
  4930. constantDeclaration := oldConstantDeclaration;
  4931. destination := oldDestination;
  4932. currentLoop := oldCurrentLoop;
  4933. conditional := oldConditional;
  4934. trueLabel := oldTrueLabel;
  4935. falseLabel := oldFalseLabel;
  4936. locked := oldLocked;
  4937. usedRegisters := oldUsedRegisters
  4938. END RestoreGlobalState;
  4939. (** do preparations before parameter push for array-structured object types (ASOTs):
  4940. if ASOT is passed as VAR parameter:
  4941. - allocate temporary variable of math array type
  4942. - copy contents of ASOT to be passed to temporary variable
  4943. - use temporary variable as the actual parameter instead
  4944. - create and store a write-back call in a list (an index operator call that writes the contents of the temp. variable back into the ASOT)
  4945. **)
  4946. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4947. VAR
  4948. expression: SyntaxTree.Expression;
  4949. BEGIN
  4950. IF actualParameter IS SyntaxTree.Designator THEN
  4951. designator := actualParameter(SyntaxTree.Designator);
  4952. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4953. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4954. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4955. ASSERT(checker # NIL);
  4956. checker.SetCurrentScope(currentScope);
  4957. (* allocate temporary variable *)
  4958. ASSERT(actualParameter.type # NIL);
  4959. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4960. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4961. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4962. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4963. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4964. ASSERT(tempVariableDesignator.type # NIL);
  4965. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4966. (* copy math array stored in actual parameter to temporary variable *)
  4967. BackupGlobalState;
  4968. AssignMathArray(tempVariableDesignator, actualParameter);
  4969. RestoreGlobalState;
  4970. (* use temporary variable as actual parameter instead of the original one *)
  4971. actualParameter := tempVariableDesignator;
  4972. (* create write-back call and store it in linked list *)
  4973. (* create new list entry *)
  4974. IF firstWriteBackCall = NIL THEN
  4975. NEW(firstWriteBackCall);
  4976. currentWriteBackCall := firstWriteBackCall
  4977. ELSE
  4978. ASSERT(currentWriteBackCall # NIL);
  4979. NEW(currentWriteBackCall.next);
  4980. currentWriteBackCall := currentWriteBackCall.next
  4981. END;
  4982. expression := checker.NewIndexOperatorCall(Basic.invalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4983. ASSERT(expression.type = NIL);
  4984. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4985. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4986. (* prepare writeback for any other "normal" indexer *)
  4987. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4988. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, variable);
  4989. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4990. Assign(tempVariableDesignator, actualParameter);
  4991. actualParameter := tempVariableDesignator;
  4992. IF firstWriteBackCall = NIL THEN
  4993. NEW(firstWriteBackCall);
  4994. currentWriteBackCall := firstWriteBackCall
  4995. ELSE
  4996. ASSERT(currentWriteBackCall # NIL);
  4997. NEW(currentWriteBackCall.next);
  4998. currentWriteBackCall := currentWriteBackCall.next
  4999. END;
  5000. expression := checker.NewObjectOperatorCall(Basic.invalidPosition, designator.relatedAsot, 0, designator.relatedIndexList, tempVariableDesignator);
  5001. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  5002. END
  5003. END
  5004. END PrepareParameter;
  5005. BEGIN
  5006. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  5007. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  5008. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  5009. IF procedure.isOberonInline (* & (procedure.scope.ownerModule = module.module) *) THEN
  5010. IF InlineProcedureCall(x) THEN
  5011. RETURN
  5012. ELSE
  5013. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  5014. END
  5015. END;
  5016. END;
  5017. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  5018. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  5019. dest := destination; destination := emptyOperand;
  5020. SaveRegisters();ReleaseUsedRegisters(saved);
  5021. parameters := x.parameters;
  5022. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  5023. (* an operator is called *)
  5024. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5025. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  5026. (* check if a dynamic operator call should be performed *)
  5027. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic;
  5028. ELSE
  5029. isCallOfDynamicOperator := FALSE
  5030. END;
  5031. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5032. Emit(Push(position, ap));
  5033. END;
  5034. alignment := procedureType.stackAlignment;
  5035. IF alignment > 1 THEN
  5036. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5037. Emit(Mov(position,reg, sp));
  5038. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  5039. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  5040. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  5041. Emit(And(position,sp, sp, mask));
  5042. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  5043. Emit(Push(position,reg));
  5044. (*
  5045. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  5046. Emit(Mov(position,mem,reg));
  5047. *)
  5048. ReleaseIntermediateOperand(reg);
  5049. END;
  5050. IF SysvABI(procedureType.callingConvention) & (system.addressSize = 32) THEN
  5051. (* align stack to 16-byte boundary *)
  5052. IntermediateCode.InitImmediate(mask,addressType,-16);
  5053. Emit(And(position,sp, sp, mask));
  5054. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  5055. IF gap # 0 THEN
  5056. IntermediateCode.InitImmediate(size,addressType,gap);
  5057. Emit(Sub(position,sp,sp,size))
  5058. END;
  5059. END;
  5060. IF x.left IS SyntaxTree.SupercallDesignator THEN
  5061. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5062. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  5063. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  5064. ELSE
  5065. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  5066. END;
  5067. Evaluate(x.left, operand);
  5068. IF symbol IS SyntaxTree.Procedure THEN
  5069. IF (procedureType.selfParameter # NIL) THEN
  5070. Emit(Push(position,operand.tag));
  5071. ELSIF x.left IS SyntaxTree.SupercallDesignator THEN
  5072. Emit(Push(position,operand.tag));
  5073. ELSIF (procedureType.isDelegate) THEN
  5074. Emit(Push(position,operand.tag));
  5075. END;
  5076. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5077. IF (procedureType.selfParameter # NIL) THEN
  5078. Emit(Push(position,operand.tag));
  5079. ELSIF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  5080. Emit(Push(position,operand.tag));
  5081. END;
  5082. ELSE HALT(200);
  5083. END;
  5084. ReleaseIntermediateOperand(operand.tag);
  5085. operand.tag := emptyOperand;
  5086. (* determine if a structured return type is needed *)
  5087. structuredReturnType := SemanticChecker.StructuredReturnType(procedureType);
  5088. IF structuredReturnType THEN
  5089. IF resultDesignator # NIL THEN
  5090. d := resultDesignator;
  5091. ELSE
  5092. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  5093. variable.SetUntraced(procedureType.hasUntracedReturn);
  5094. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,variable);
  5095. d.SetType(variable.type);
  5096. END;
  5097. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  5098. Designate(d,returnValue);
  5099. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  5100. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  5101. Emit(Push(position,size));
  5102. Emit(Push(position,returnValue.op));
  5103. ReleaseOperand(returnValue);
  5104. ELSE
  5105. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  5106. END;
  5107. END;
  5108. firstWriteBackCall := NIL; (* reset write-back call list *)
  5109. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  5110. parameterRegister := backend.GetParameterRegisters(procedureType.callingConvention);
  5111. IF parameterRegister = NIL THEN parameterRegisters := 0
  5112. ELSE parameterRegisters := LEN(parameterRegister)
  5113. END;
  5114. passByRegister := parameterRegisters > 0;
  5115. registerNumber := 0;
  5116. formalParameter := procedureType.lastParameter;
  5117. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  5118. actualParameter := parameters.GetExpression(i);
  5119. PrepareParameter(actualParameter, formalParameter);
  5120. IF passByRegister & (i < parameterRegisters) THEN
  5121. IF ~PassInRegister(formalParameter) THEN
  5122. Error(actualParameter.position,"cannot be passed by register")
  5123. ELSE
  5124. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,parameterRegister[i]);
  5125. END;
  5126. INC(registerNumber);
  5127. ELSE
  5128. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5129. END;
  5130. formalParameter := formalParameter.prevParameter;
  5131. END;
  5132. IF passByRegister & (registerNumber > 0) & ~SysvABI(procedureType.callingConvention) THEN
  5133. stackSize := ToMemoryUnits(system,parameterRegisters*addressType.sizeInBits);
  5134. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5135. END;
  5136. ELSE
  5137. hasDynamicOperands := FALSE;
  5138. formalParameter := procedureType.firstParameter;
  5139. FOR i := 0 TO parameters.Length() - 1 DO
  5140. actualParameter := parameters.GetExpression(i);
  5141. IF formalParameter # NIL THEN (* TENTATIVE *)
  5142. PrepareParameter(actualParameter, formalParameter);
  5143. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  5144. ASSERT(i < 2);
  5145. hasDynamicOperands := TRUE;
  5146. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  5147. ELSE
  5148. IF passByRegister & (registerNumber > 0) THEN
  5149. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  5150. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  5151. END;
  5152. passByRegister := FALSE;
  5153. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  5154. END;
  5155. formalParameter := formalParameter.nextParameter;
  5156. END;
  5157. END;
  5158. END;
  5159. IF symbol IS SyntaxTree.Procedure THEN
  5160. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  5161. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  5162. Emit(Push(position,reg));
  5163. ReleaseIntermediateOperand(reg);
  5164. END;
  5165. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  5166. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  5167. parametersSize := ParametersSize(system,procedureType,FALSE);
  5168. END;
  5169. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  5170. (*
  5171. dynamic operator overloading:
  5172. first push parameters, regularly:
  5173. [self]
  5174. par1
  5175. par2
  5176. then push parameters for GetOperator
  5177. identifier
  5178. ptr1
  5179. tag
  5180. ptr2
  5181. tag
  5182. call GetOperatorRuntimeProc
  5183. call Operator
  5184. *)
  5185. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  5186. (* push ID *)
  5187. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5188. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  5189. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  5190. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  5191. formalParameter := procedureType.firstParameter;
  5192. FOR i := 0 TO parameters.Length() - 1 DO
  5193. IF formalParameter.access # SyntaxTree.Hidden THEN
  5194. ASSERT(i < 2);
  5195. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  5196. (* push pointer *)
  5197. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5198. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  5199. (* add dereference *)
  5200. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5201. (*! better: do refer to stack above than using parameter backups !!*)
  5202. ReleaseIntermediateOperand(parameterBackups[i]);
  5203. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  5204. END;
  5205. Emit(Push(position,parameterBackups[i]));
  5206. ReleaseIntermediateOperand(parameterBackups[i]);
  5207. (* push typetag *)
  5208. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5209. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5210. arg := TypeDescriptorAdr(recordType);
  5211. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5212. Emit(Push(position,arg));
  5213. ELSE
  5214. (* push 'NonPointer' *)
  5215. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5216. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5217. (* push fingerprint *)
  5218. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5219. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  5220. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  5221. END
  5222. END;
  5223. formalParameter := formalParameter.nextParameter
  5224. END;
  5225. (* for unary operators: complete the information for the second parameter *)
  5226. IF procedureType.numberParameters < 2 THEN
  5227. (* push 'NonPointer' *)
  5228. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5229. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  5230. (* push 'NoType' *)
  5231. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5232. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  5233. END;
  5234. (* call operator selection procedure *)
  5235. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  5236. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  5237. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  5238. ReleaseOperand(operatorSelectionProcedureOperand);
  5239. (* use the address that the operator selection procedure returned as the target address of the call *)
  5240. InitOperand(operand, ModeValue);
  5241. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5242. Emit(Result(position,operand.op))
  5243. END
  5244. END;
  5245. ReleaseParameterRegisters();
  5246. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR SysvABI(procedureType.callingConvention) THEN
  5247. Emit(Call(position,operand.op,0));
  5248. ELSE
  5249. Emit(Call(position,operand.op,parametersSize));
  5250. END;
  5251. ReleaseOperand(operand);
  5252. IF procedureType.noReturn THEN
  5253. EmitTrap(position,NoReturnTrap);
  5254. END;
  5255. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  5256. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  5257. Emit(Result(position,return));
  5258. END;
  5259. (* === return parameter space === *)
  5260. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) & passByRegister THEN
  5261. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5262. IF parametersSize < 32 THEN
  5263. (* allocated space for all parameter registers *)
  5264. parametersSize := 32
  5265. END;
  5266. size := IntermediateCode.Immediate(addressType,parametersSize);
  5267. Emit(Add(position,sp,sp,size))
  5268. END;
  5269. IF SysvABI(procedureType.callingConvention) THEN
  5270. IF passByRegister THEN
  5271. IF parameters.Length() > parameterRegisters THEN
  5272. parametersSize := ToMemoryUnits(system,(parameters.Length()-parameterRegisters)*addressType.sizeInBits)
  5273. ELSE
  5274. parametersSize := 0
  5275. END;
  5276. ELSE
  5277. parametersSize := ToMemoryUnits(system,parameters.Length()*addressType.sizeInBits);
  5278. INC( parametersSize, (-parametersSize) MOD 16 )
  5279. END;
  5280. IF parametersSize > 0 THEN
  5281. size := IntermediateCode.Immediate(addressType,parametersSize);
  5282. Emit(Add(position,sp,sp,size))
  5283. END;
  5284. END;
  5285. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  5286. IF structuredReturnType THEN
  5287. (* stack pointer rewinding done by callee
  5288. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  5289. Emit(Add(position,sp,sp,size));
  5290. *)
  5291. RestoreRegisters(saved);
  5292. InitOperand(result,ModeReference);
  5293. Symbol(variable,result);
  5294. ELSE
  5295. RestoreRegisters(saved);
  5296. InitOperand(result,ModeValue);
  5297. result.op := return;
  5298. END;
  5299. END;
  5300. IF alignment > 1 THEN
  5301. Emit(Pop(position,sp));
  5302. END;
  5303. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  5304. Emit(Pop(position, ap));
  5305. END;
  5306. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5307. ValueToCondition(result);
  5308. END;
  5309. destination := dest;
  5310. (* perform all write-back calls in the list *)
  5311. BackupGlobalState;
  5312. currentWriteBackCall := firstWriteBackCall;
  5313. WHILE currentWriteBackCall # NIL DO
  5314. VisitProcedureCallDesignator(currentWriteBackCall.call);
  5315. currentWriteBackCall := currentWriteBackCall.next
  5316. END;
  5317. RestoreGlobalState;
  5318. (* TENATIVE *)
  5319. (*
  5320. IF isOperatorCall THEN
  5321. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  5322. END;
  5323. *)
  5324. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5325. END VisitProcedureCallDesignator;
  5326. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5327. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5328. td: SyntaxTree.Symbol;
  5329. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5330. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5331. BEGIN
  5332. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5333. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5334. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5335. variable.SetType(system.anyType);
  5336. scope.AddVariable(variable);
  5337. hiddenPointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5338. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5339. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5340. typeDeclaration.SetScope(module.module.moduleScope);
  5341. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5342. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5343. END;
  5344. RETURN hiddenPointerType;
  5345. END GetHiddenPointerType;
  5346. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5347. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5348. BEGIN
  5349. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5350. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5351. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Procedure"));
  5352. variable.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,NIL));
  5353. scope.AddVariable(variable);
  5354. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Any"));
  5355. variable.SetType(system.anyType);
  5356. scope.AddVariable(variable);
  5357. delegatePointerType := SyntaxTree.NewRecordType(Basic.invalidPosition,NIL,scope);
  5358. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier("@Delegate"));
  5359. typeDeclaration.SetDeclaredType(delegatePointerType);
  5360. typeDeclaration.SetScope(module.module.moduleScope);
  5361. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5362. delegatePointerType.SetState(SyntaxTree.Resolved);
  5363. END;
  5364. RETURN delegatePointerType
  5365. END GetDelegateType;
  5366. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5367. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5368. such as in VAR a: RECORD ... END;
  5369. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5370. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5371. *)
  5372. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5373. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5374. BEGIN (* no code emission *)
  5375. source := NIL;
  5376. x := x.resolved;
  5377. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5378. x := GetHiddenPointerType();
  5379. ELSIF IsDelegate(x) THEN
  5380. x := GetDelegateType();
  5381. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5382. ELSE HALT(200);
  5383. END;
  5384. td := x.typeDeclaration;
  5385. IF td = NIL THEN
  5386. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5387. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5388. ASSERT(td # NIL);
  5389. END;
  5390. IF newObjectFile THEN
  5391. GetCodeSectionNameForSymbol(td,name);
  5392. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5393. meta.CheckTypeDeclaration(x);
  5394. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5395. ELSE
  5396. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5397. END;
  5398. IF backend.cooperative OR meta.simple THEN
  5399. offset := 0;
  5400. ELSE
  5401. IF x IS SyntaxTree.CellType THEN
  5402. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5403. IF baseRecord = NIL THEN
  5404. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5405. ELSE
  5406. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5407. END;
  5408. ELSE
  5409. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5410. END;
  5411. END;
  5412. ELSE
  5413. offset := 0;
  5414. source := module.allSections.FindBySymbol(td); (*TODO*)
  5415. IF source = NIL THEN
  5416. null := 0;
  5417. GetCodeSectionNameForSymbol(td,name);
  5418. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  5419. Basic.SuffixSegmentedName (name, module.module.name);
  5420. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5421. IntermediateCode.InitImmediate(op,addressType,0);
  5422. source(IntermediateCode.Section).Emit(Data(position,op));
  5423. source.SetReferenced(FALSE)
  5424. ELSE
  5425. name := source.name;
  5426. END;
  5427. END;
  5428. RETURN td
  5429. END GetBackendType;
  5430. BEGIN
  5431. (*td := t.typeDeclaration;*)
  5432. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5433. (*
  5434. IF t IS SyntaxTree.PointerType THEN
  5435. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5436. ELSE
  5437. source := GetBackendType(t);
  5438. END;
  5439. *)
  5440. IF newObjectFile THEN
  5441. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5442. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5443. IntermediateCode.SetOffset(res,offset);
  5444. ELSE
  5445. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5446. END;
  5447. (*
  5448. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5449. make memory should be used when tag is used, not earlier
  5450. *)
  5451. RETURN res
  5452. END TypeDescriptorAdr;
  5453. (*
  5454. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5455. VAR result: IntermediateCode.Operand;
  5456. BEGIN
  5457. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5458. operand.tag := TypeDescriptorAdr(operand.type);
  5459. IntermediateCode.MakeMemory(operand.tag,addressType);
  5460. UseIntermediateOperand(operand.tag);
  5461. END;
  5462. END MakeTypeTag;
  5463. *)
  5464. PROCEDURE ProfilerInit;
  5465. VAR reg: IntermediateCode.Operand;
  5466. BEGIN
  5467. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5468. Emit(Call(position,reg,0));
  5469. END ProfilerInit;
  5470. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5471. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5472. BEGIN
  5473. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5474. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5475. THEN
  5476. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5477. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5478. Emit(Push(position,reg));
  5479. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5480. Emit(Push(position,reg));
  5481. StaticCallOperand(result,procedure);
  5482. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5483. ReleaseOperand(result);
  5484. END;
  5485. END ProfilerEnterExit;
  5486. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5487. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5488. BEGIN
  5489. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5490. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5491. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5492. profileInit.Emit(Push(position,reg));
  5493. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5494. profileInit.Emit(Push(position,reg));
  5495. NEW(string, LEN(name)); COPY(name, string^);
  5496. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5497. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5498. sv.SetType(type);
  5499. Designate(sv,result);
  5500. profileInit.Emit(Push(position,result.tag));
  5501. profileInit.Emit(Push(position,result.op));
  5502. StaticCallOperand(result,procedure);
  5503. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5504. ReleaseOperand(result);
  5505. END;
  5506. END ProfilerAddProcedure;
  5507. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5508. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5509. BEGIN
  5510. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5511. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5512. profileInit.Emit(Push(position,reg));
  5513. profileInitPatchPosition := profileInit.pc;
  5514. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5515. NEW(string, LEN(name)); COPY(name, string^);
  5516. sv := SyntaxTree.NewStringValue(Basic.invalidPosition, string);
  5517. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(name));
  5518. sv.SetType(type);
  5519. Designate(sv,result);
  5520. profileInit.Emit(Push(position,result.tag));
  5521. profileInit.Emit(Push(position,result.op));
  5522. StaticCallOperand(result,procedure);
  5523. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5524. ReleaseOperand(result);
  5525. END;
  5526. END ProfilerAddModule;
  5527. PROCEDURE ProfilerPatchInit;
  5528. VAR reg: IntermediateCode.Operand;
  5529. BEGIN
  5530. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5531. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5532. EmitLeave(profileInit,position,NIL,0);
  5533. profileInit.Emit(Exit(position,0,0,0));
  5534. END ProfilerPatchInit;
  5535. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5536. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5537. VAR
  5538. id: LONGINT;
  5539. leftType, rightType: SyntaxTree.Type;
  5540. procedureType: SyntaxTree.ProcedureType;
  5541. runtimeProcedure: SyntaxTree.Procedure;
  5542. runtimeProcedureOperand, operatorOperand: Operand;
  5543. kind: SET;
  5544. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5545. VAR
  5546. arg: IntermediateCode.Operand;
  5547. recordType: SyntaxTree.RecordType;
  5548. fingerPrint: SyntaxTree.FingerPrint;
  5549. BEGIN
  5550. IF type = NIL THEN
  5551. (* no type: push 'NoType' *)
  5552. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5553. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5554. ELSIF IsStrictlyPointerToRecord(type) THEN
  5555. (* pointer to record type: push typetag *)
  5556. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5557. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5558. arg := TypeDescriptorAdr(recordType);
  5559. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5560. ELSE
  5561. (* non-pointer to record type: push fingerprint *)
  5562. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5563. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5564. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5565. END;
  5566. operatorInitializationCodeSection.Emit(Push(position,arg))
  5567. END PushTypeInfo;
  5568. BEGIN
  5569. ASSERT(operatorInitializationCodeSection # NIL);
  5570. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5571. procedureType := operator.type(SyntaxTree.ProcedureType);
  5572. (* determine types *)
  5573. leftType := procedureType.firstParameter.type;
  5574. IF procedureType.numberParameters = 2 THEN
  5575. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5576. rightType := procedureType.firstParameter.nextParameter.type;
  5577. ELSE
  5578. rightType := NIL
  5579. END;
  5580. (* determine operator kind *)
  5581. IF IsStrictlyPointerToRecord(leftType) THEN
  5582. kind := {LhsIsPointer}
  5583. ELSE
  5584. kind := {}
  5585. END;
  5586. IF IsStrictlyPointerToRecord(rightType) THEN
  5587. kind := kind + {RhsIsPointer}
  5588. END;
  5589. IF kind # {} THEN (* TODO: to be removed later on *)
  5590. (* at least one of the types is a pointer to record *)
  5591. (* emit a code that registers this specific operator in the runtime *)
  5592. dump := operatorInitializationCodeSection.comments;
  5593. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5594. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5595. (* push ID *)
  5596. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5597. id := Global.GetSymbol(module.module.case, operator.name);
  5598. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5599. (* push kind *)
  5600. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5601. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5602. (* push type infos *)
  5603. PushTypeInfo(leftType);
  5604. PushTypeInfo(rightType);
  5605. (* push operator address *)
  5606. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5607. StaticCallOperand(operatorOperand, operator);
  5608. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5609. ReleaseOperand(operatorOperand);
  5610. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5611. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5612. ReleaseOperand(runtimeProcedureOperand)
  5613. END
  5614. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5615. END
  5616. END RegisterDynamicOperator;
  5617. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: Position);
  5618. VAR
  5619. traceModule: SyntaxTree.Module;
  5620. procedure: SyntaxTree.Procedure;
  5621. procedureVariable: SyntaxTree.Variable;
  5622. s,msg: Basic.MessageString;
  5623. res: Operand;
  5624. i: LONGINT;
  5625. sv: SyntaxTree.StringValue;
  5626. type: SyntaxTree.Type;
  5627. recordType: SyntaxTree.RecordType;
  5628. printout: Printout.Printer;
  5629. stringWriter: Streams.StringWriter;
  5630. expression: SyntaxTree.Expression;
  5631. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5632. BEGIN
  5633. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5634. IF procedure = NIL THEN
  5635. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5636. END;
  5637. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5638. s := "procedure ";
  5639. Strings.Append(s,backend.traceModuleName);
  5640. Strings.Append(s,".");
  5641. Strings.Append(s,procedureName);
  5642. Strings.Append(s," not present");
  5643. Error(position,s);
  5644. RETURN FALSE
  5645. ELSE
  5646. RETURN TRUE
  5647. END;
  5648. END GetProcedure;
  5649. PROCEDURE CallProcedure;
  5650. VAR size: LONGINT;
  5651. BEGIN
  5652. IF procedure # NIL THEN
  5653. StaticCallOperand(result,procedure);
  5654. size := ProcedureParametersSize(system,procedure);
  5655. ELSE
  5656. Symbol(procedureVariable, result);
  5657. LoadValue(result, procedureVariable.type.resolved);
  5658. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5659. END;
  5660. Emit(Call(position,result.op,size));
  5661. END CallProcedure;
  5662. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5663. VAR res: Operand; string: SyntaxTree.String;
  5664. BEGIN
  5665. IF GetProcedure("String") THEN
  5666. NEW(string, LEN(s)); COPY(s, string^);
  5667. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  5668. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  5669. sv.SetType(type);
  5670. Designate(sv,res);
  5671. Emit(Push(position,res.tag));
  5672. Emit(Push(position,res.op));
  5673. ReleaseOperand(res);
  5674. CallProcedure;
  5675. END;
  5676. END String;
  5677. PROCEDURE Integer(op: IntermediateCode.Operand);
  5678. BEGIN
  5679. IF GetProcedure("Int") THEN
  5680. Emit(Push(position,op));
  5681. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5682. CallProcedure;
  5683. END;
  5684. END Integer;
  5685. PROCEDURE Float(op: IntermediateCode.Operand);
  5686. BEGIN
  5687. IF GetProcedure("HIntHex") THEN
  5688. Emit(Push(position,op));
  5689. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5690. CallProcedure;
  5691. END;
  5692. END Float;
  5693. PROCEDURE Set(op: IntermediateCode.Operand);
  5694. BEGIN
  5695. IF GetProcedure("Set") THEN
  5696. Emit(Push(position,op));
  5697. (*
  5698. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5699. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5700. *)
  5701. CallProcedure;
  5702. END;
  5703. END Set;
  5704. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5705. BEGIN
  5706. IF GetProcedure("Boolean") THEN
  5707. Emit(Push(position,op));
  5708. CallProcedure;
  5709. END;
  5710. END Boolean;
  5711. PROCEDURE Char(op: IntermediateCode.Operand);
  5712. BEGIN
  5713. IF GetProcedure("Char") THEN
  5714. Emit(Push(position,op));
  5715. CallProcedure;
  5716. END;
  5717. END Char;
  5718. PROCEDURE Address(op: IntermediateCode.Operand);
  5719. BEGIN
  5720. IF GetProcedure("Address") THEN
  5721. Emit(Push(position,op));
  5722. CallProcedure;
  5723. END;
  5724. END Address;
  5725. PROCEDURE StringOperand(op: Operand; type: SyntaxTree.Type);
  5726. VAR len: IntermediateCode.Operand;
  5727. BEGIN
  5728. IF GetProcedure("String") THEN
  5729. len := GetArrayLength(type, op.tag);
  5730. Emit(Push(position,len));
  5731. ReleaseIntermediateOperand(len);
  5732. Emit(Push(position,op.op));
  5733. CallProcedure;
  5734. END;
  5735. END StringOperand;
  5736. PROCEDURE Ln;
  5737. BEGIN
  5738. IF GetProcedure("Ln") THEN
  5739. CallProcedure;
  5740. END;
  5741. END Ln;
  5742. BEGIN
  5743. IF backend.traceModuleName = "" THEN RETURN END;
  5744. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5745. IF GetProcedure("Enter") THEN
  5746. CallProcedure
  5747. END;
  5748. NEW(stringWriter,LEN(s));
  5749. FOR i := 0 TO x.Length()-1 DO
  5750. msg := "";
  5751. expression := x.GetExpression(i);
  5752. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5753. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5754. ELSE
  5755. Global.GetModuleName(module.module, s);
  5756. END;
  5757. IF i = 0 THEN
  5758. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos.start,1);
  5759. stringWriter.String(":");
  5760. END;
  5761. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5762. IF ~(expression IS SyntaxTree.StringValue) THEN
  5763. printout.Expression(expression);
  5764. stringWriter.Get(s);
  5765. Strings.Append(msg,s);
  5766. Strings.Append(msg,"= ");
  5767. ELSE stringWriter.Get(s); (* remove from string writer *)
  5768. Strings.Append(msg, s);
  5769. END;
  5770. String(msg);
  5771. IF SemanticChecker.IsStringType(expression.type) THEN
  5772. Designate(expression,res);
  5773. StringOperand(res, expression.type);
  5774. ELSE
  5775. Evaluate(expression,res);
  5776. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5777. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5778. Convert(res.op,int64);
  5779. END;
  5780. Integer(res.op);
  5781. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5782. Boolean(res.op);
  5783. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5784. Set(res.op);
  5785. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5786. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5787. Convert(res.op,float64);
  5788. END;
  5789. Float(res.op);
  5790. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5791. Char(res.op);
  5792. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5793. Address(res.op);String("H");
  5794. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5795. Address(res.op);String("H");
  5796. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5797. Address(res.op);String("H");
  5798. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5799. Address(res.op);String("H");
  5800. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5801. String("NIL");
  5802. ELSE HALT(200);
  5803. END;
  5804. END;
  5805. ReleaseOperand(res);
  5806. String("; ");
  5807. END;
  5808. IF GetProcedure("Exit") THEN
  5809. CallProcedure
  5810. ELSE
  5811. Ln;
  5812. END;
  5813. END;
  5814. END SystemTrace;
  5815. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5816. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5817. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5818. BEGIN
  5819. type := type.resolved;
  5820. IF type IS SyntaxTree.RecordType THEN
  5821. WITH type: SyntaxTree.RecordType DO
  5822. baseType := type.baseType;
  5823. IF baseType # NIL THEN
  5824. baseType := baseType.resolved;
  5825. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5826. InitFields(baseType,adr,offset);
  5827. END;
  5828. variable := type.recordScope.firstVariable;
  5829. WHILE variable # NIL DO
  5830. IF variable.initializer # NIL THEN
  5831. Evaluate(variable.initializer,initializerOp);
  5832. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5833. Emit(Mov(position,mem,initializerOp.op));
  5834. ReleaseOperand(initializerOp);
  5835. ReleaseIntermediateOperand(mem);
  5836. END;
  5837. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5838. variable := variable.nextVariable
  5839. END;
  5840. END;
  5841. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5842. WITH type: SyntaxTree.ArrayType DO
  5843. IF type.form = SyntaxTree.Static THEN
  5844. baseType := type.arrayBase;
  5845. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5846. FOR i := 0 TO type.staticLength-1 DO
  5847. InitFields(baseType,adr,offset+i*size);
  5848. END;
  5849. END;
  5850. END;
  5851. ELSIF type IS SyntaxTree.MathArrayType THEN
  5852. WITH type: SyntaxTree.MathArrayType DO
  5853. IF type.form = SyntaxTree.Open THEN
  5854. dim := DynamicDim(type);
  5855. imm := IntermediateCode.Immediate(addressType,dim);
  5856. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5857. baseType := SemanticChecker.ArrayBase(type,dim);
  5858. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5859. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5860. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5861. ReleaseIntermediateOperand(imm);
  5862. (* flags remain empty (=0) for open array *)
  5863. ELSIF type.form = SyntaxTree.Static THEN
  5864. baseType := type.arrayBase;
  5865. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5866. ASSERT(type.staticLength < 1024*1024*1024);
  5867. FOR i := 0 TO type.staticLength-1 DO
  5868. InitFields(baseType,adr,offset+i*size);
  5869. END;
  5870. END;
  5871. END;
  5872. END;
  5873. END InitFields;
  5874. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5875. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5876. BEGIN
  5877. type := variable.type.resolved;
  5878. IF (type IS SyntaxTree.MathArrayType) THEN
  5879. WITH type: SyntaxTree.MathArrayType DO
  5880. IF type.form = SyntaxTree.Open THEN
  5881. Symbol(variable,operand);
  5882. InitFields(type, operand.tag,0);
  5883. ELSIF type.form = SyntaxTree.Tensor THEN
  5884. Symbol(variable, operand);
  5885. MakeMemory(tmp,operand.op,addressType,0);
  5886. ReleaseOperand(operand);
  5887. Emit(Mov(position,tmp, nil ) );
  5888. ReleaseIntermediateOperand(tmp);
  5889. END;
  5890. END;
  5891. ELSE
  5892. Symbol(variable,operand);
  5893. IF variable.initializer # NIL THEN
  5894. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5895. reference.SetType(variable.type.resolved);
  5896. reference.SetAssignable(TRUE);
  5897. Assign(reference,variable.initializer);
  5898. END;
  5899. InitFields(type, operand.op,0);
  5900. ReleaseOperand(operand);
  5901. END;
  5902. END InitVariable;
  5903. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5904. VAR end: Label;
  5905. BEGIN
  5906. IF type.form = SyntaxTree.Tensor THEN
  5907. InitOperand(result,ModeValue);
  5908. ReuseCopy(result.op,base);
  5909. end := NewLabel();
  5910. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5911. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5912. SetLabel(end);
  5913. Convert(result.op,int32);
  5914. ELSE
  5915. InitOperand(result,ModeValue);
  5916. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5917. END
  5918. END MathArrayDim;
  5919. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5920. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5921. BEGIN
  5922. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5923. MakeMemory(mem,base,addressType,offset);
  5924. Emit(Mov(position,mem,value));
  5925. ReleaseIntermediateOperand(mem);
  5926. END PutMathArrayField;
  5927. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5928. VAR mem: IntermediateCode.Operand;
  5929. BEGIN
  5930. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5931. MakeMemory(mem,base,addressType,offset);
  5932. Emit(Mov(position,mem,value));
  5933. ReleaseIntermediateOperand(mem);
  5934. END PutMathArrayFieldOffset;
  5935. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5936. BEGIN
  5937. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5938. MakeMemory(value,base,addressType,offset);
  5939. END GetMathArrayField;
  5940. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5941. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5942. BEGIN
  5943. IF incr THEN
  5944. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5945. ELSE
  5946. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5947. END;
  5948. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5949. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5950. ELSE
  5951. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5952. Emit(Mov(position,reg,dim));
  5953. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5954. Emit(Add(position,reg,reg,base));
  5955. MakeMemory(mem, reg, addressType, offset);
  5956. ReleaseIntermediateOperand(reg);
  5957. Emit(Mov(position,mem,value));
  5958. ReleaseIntermediateOperand(mem);
  5959. END;
  5960. END PutMathArrayLenOrIncr;
  5961. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5962. BEGIN
  5963. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5964. END PutMathArrayLength;
  5965. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5966. BEGIN
  5967. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5968. END PutMathArrayIncrement;
  5969. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5970. BEGIN
  5971. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5972. END GetMathArrayIncrement;
  5973. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5974. BEGIN
  5975. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5976. END GetMathArrayLength;
  5977. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5978. VAR dimOp: IntermediateCode.Operand;
  5979. BEGIN
  5980. dimOp := IntermediateCode.Immediate(int32, dim);
  5981. GetMathArrayLength(type, operand, dimOp, check, result);
  5982. END GetMathArrayLengthAt;
  5983. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5984. VAR dimOp: IntermediateCode.Operand;
  5985. BEGIN
  5986. dimOp := IntermediateCode.Immediate(int32, dim);
  5987. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5988. END GetMathArrayIncrementAt;
  5989. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5990. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5991. offset: LONGINT;
  5992. BEGIN
  5993. IF increment THEN
  5994. offset := MathIncrOffset;
  5995. ELSE
  5996. offset := MathLenOffset;
  5997. END;
  5998. INC(offset,operand.dimOffset*2);
  5999. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  6000. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  6001. END;
  6002. (* static dimension *)
  6003. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6004. IF check & (type.form = SyntaxTree.Tensor) THEN
  6005. DimensionCheck(operand.tag,dim,BrltL);
  6006. END;
  6007. val := SHORT(dim.intValue);
  6008. IF type.form # SyntaxTree.Tensor THEN
  6009. t := SemanticChecker.ArrayBase(type,val);
  6010. type := t.resolved(SyntaxTree.MathArrayType);
  6011. IF type.form = SyntaxTree.Static THEN
  6012. IF increment THEN
  6013. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  6014. ELSE
  6015. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6016. END;
  6017. InitOperand(result,ModeValue);
  6018. result.op := res;
  6019. RETURN;
  6020. END;
  6021. END;
  6022. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  6023. MakeMemory(res,operand.tag,addressType,offset);
  6024. (*
  6025. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6026. *)
  6027. InitOperand(result,ModeValue);
  6028. result.op := res;
  6029. ELSE
  6030. Convert(dim,addressType);
  6031. IF check THEN
  6032. IF type.form = SyntaxTree.Tensor THEN
  6033. DimensionCheck(operand.tag,dim,BrltL);
  6034. ELSIF isUnchecked THEN (* do nothing *)
  6035. ELSE
  6036. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6037. END;
  6038. END;
  6039. end := NewLabel(); next := NIL;
  6040. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6041. Emit(Mov(position,res,dim));
  6042. Convert(res,int32);
  6043. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6044. WHILE t IS SyntaxTree.MathArrayType DO
  6045. type := t(SyntaxTree.MathArrayType);
  6046. IF type.form = SyntaxTree.Static THEN
  6047. imm := IntermediateCode.Immediate(int32,val);
  6048. next := NewLabel();
  6049. BrneL(next,imm,res);
  6050. IF increment THEN
  6051. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  6052. ELSE
  6053. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6054. END;
  6055. Emit(MovReplace(position,res,imm));
  6056. BrL(end);
  6057. ELSE hasDynamicPart := TRUE;
  6058. END;
  6059. t := type.arrayBase.resolved;
  6060. val := val + 1;
  6061. IF next # NIL THEN SetLabel(next) END;
  6062. END;
  6063. IF hasDynamicPart THEN
  6064. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6065. Emit(Mov(position,res2,dim));
  6066. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  6067. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6068. Emit(Add(position,res2,res2,imm));
  6069. Emit(Add(position,res2,res2,operand.tag));
  6070. IntermediateCode.MakeMemory(res2,int32);
  6071. Emit(MovReplace(position,res,res2));
  6072. ReleaseIntermediateOperand(res2);
  6073. END;
  6074. SetLabel(end);
  6075. Convert(res,int32);
  6076. InitOperand(result,ModeValue);
  6077. result.op := res;
  6078. END;
  6079. END MathArrayLenOrIncr;
  6080. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  6081. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  6082. offset: LONGINT;
  6083. BEGIN
  6084. offset := operand.dimOffset+DynamicDim(type)-1;
  6085. IF dim.mode = IntermediateCode.ModeImmediate THEN
  6086. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  6087. val := SHORT(dim.intValue);
  6088. t := SemanticChecker.ArrayBase(type,val);
  6089. type := t.resolved(SyntaxTree.ArrayType);
  6090. IF type.form = SyntaxTree.Static THEN
  6091. res := IntermediateCode.Immediate(addressType,type.staticLength);
  6092. ELSE
  6093. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  6094. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  6095. END;
  6096. UseIntermediateOperand(res);
  6097. InitOperand(result,ModeValue);
  6098. result.op := res;
  6099. ELSE
  6100. Convert(dim,addressType);
  6101. IF ~isUnchecked THEN
  6102. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  6103. END;
  6104. end := NewLabel(); next := NIL;
  6105. (* ReuseCopy(dim,res); *)
  6106. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  6107. Emit(Mov(position,res,dim));
  6108. Convert(res,int32);
  6109. Convert(res,int32);
  6110. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  6111. WHILE t IS SyntaxTree.ArrayType DO
  6112. type := t(SyntaxTree.ArrayType);
  6113. IF type.form = SyntaxTree.Static THEN
  6114. imm := IntermediateCode.Immediate(int32,val);
  6115. next := NewLabel();
  6116. BrneL(next,imm,res);
  6117. imm := IntermediateCode.Immediate(int32,type.staticLength);
  6118. Emit(MovReplace(position,res,imm));
  6119. BrL(end);
  6120. ELSE hasDynamicPart := TRUE;
  6121. END;
  6122. t := type.arrayBase.resolved;
  6123. val := val + 1;
  6124. IF next # NIL THEN SetLabel(next) END;
  6125. END;
  6126. IF hasDynamicPart THEN
  6127. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  6128. Convert(res2,addressType);
  6129. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  6130. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  6131. Emit(Sub(position,res2,imm,res2));
  6132. Emit(Add(position,res2,res2,operand.tag));
  6133. IntermediateCode.MakeMemory(res2,int32);
  6134. Emit(MovReplace(position,res,res2));
  6135. ReleaseIntermediateOperand(res2);
  6136. END;
  6137. SetLabel(end);
  6138. Convert(res,int32);
  6139. InitOperand(result,ModeValue);
  6140. result.op := res;
  6141. END;
  6142. END ArrayLen;
  6143. (**
  6144. create a temporary variable in current scope
  6145. **)
  6146. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  6147. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  6148. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  6149. BEGIN
  6150. IF ~register THEN
  6151. variable := temporaries.GetFreeVariable(type, index);
  6152. ELSE
  6153. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  6154. END;
  6155. scope := currentScope;
  6156. IF variable = NIL THEN
  6157. COPY("@hiddenIRVar",string);
  6158. Basic.AppendNumber(string,index);
  6159. name := SyntaxTree.NewIdentifier(string);
  6160. variable := SyntaxTree.NewVariable(Basic.invalidPosition,name);
  6161. variable.SetType(type);
  6162. variable.SetAccess(SyntaxTree.Hidden);
  6163. IF ~register THEN
  6164. temporaries.AddVariable(variable);
  6165. IF scope.lastVariable # NIL THEN
  6166. offset := scope.lastVariable.offsetInBits;
  6167. ELSE
  6168. offset := 0;
  6169. END;
  6170. DEC(offset,system.SizeOf(variable.type));
  6171. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  6172. variable(SyntaxTree.Variable).SetOffset(offset);
  6173. scope.AddVariable(variable(SyntaxTree.Variable));
  6174. scope.EnterSymbol(variable, duplicate);
  6175. ASSERT(~duplicate);
  6176. InitVariable(variable(SyntaxTree.Variable));
  6177. ELSE
  6178. variable.SetUseRegister(TRUE);
  6179. variable(SyntaxTree.Variable).SetOffset(0);
  6180. END;
  6181. ELSE
  6182. InitVariable(variable(SyntaxTree.Variable));
  6183. (*
  6184. ASSERT(variable.type.resolved = type.resolved)
  6185. *)
  6186. END;
  6187. RETURN variable(SyntaxTree.Variable)
  6188. END GetTemporaryVariable;
  6189. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  6190. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  6191. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  6192. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  6193. i: LONGINT; duplicate: BOOLEAN;
  6194. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  6195. VAR variable: SyntaxTree.Variable;
  6196. BEGIN
  6197. variable := SyntaxTree.NewVariable(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6198. variable.SetType(type);
  6199. recordScope.AddVariable(variable);
  6200. END AddVariable;
  6201. BEGIN
  6202. name := "@ArrayDescriptor";
  6203. Basic.AppendNumber(name,dimensions);
  6204. identifier := SyntaxTree.NewIdentifier(name);
  6205. parentScope := module.module.moduleScope;
  6206. symbol := parentScope.FindSymbol(identifier);
  6207. IF symbol # NIL THEN
  6208. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  6209. type := typeDeclaration.declaredType;
  6210. ELSE
  6211. typeDeclaration := SyntaxTree.NewTypeDeclaration(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  6212. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  6213. recordScope := SyntaxTree.NewRecordScope(parentScope);
  6214. recordType := SyntaxTree.NewRecordType( Basic.invalidPosition, parentScope, recordScope);
  6215. recordType.SetTypeDeclaration(typeDeclaration);
  6216. recordType.SetState(SyntaxTree.Resolved);
  6217. typeDeclaration.SetDeclaredType(recordType);
  6218. AddVariable("@ptr",system.anyType);
  6219. AddVariable("@adr",system.addressType);
  6220. AddVariable("@flags",system.addressType);
  6221. AddVariable("@dim",system.addressType);
  6222. AddVariable("@elementSize",system.addressType);
  6223. FOR i := 0 TO dimensions-1 DO
  6224. name := "@len";
  6225. Basic.AppendNumber(name,i);
  6226. AddVariable(name,system.addressType);
  6227. name := "@incr";
  6228. Basic.AppendNumber(name,i);
  6229. AddVariable(name,system.addressType);
  6230. END;
  6231. parentScope.AddTypeDeclaration(typeDeclaration);
  6232. parentScope.EnterSymbol(typeDeclaration,duplicate);
  6233. ASSERT(~duplicate);
  6234. type := recordType;
  6235. END;
  6236. RETURN type
  6237. END GetMathArrayDescriptorType;
  6238. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  6239. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  6240. BEGIN
  6241. type := GetMathArrayDescriptorType(dimensions);
  6242. Emit(Push(position,op.op));
  6243. (* push type descriptor *)
  6244. reg := TypeDescriptorAdr(type);
  6245. IF ~newObjectFile THEN
  6246. IntermediateCode.MakeMemory(reg,addressType);
  6247. END;
  6248. Emit(Push(position,reg));
  6249. ReleaseIntermediateOperand(reg);
  6250. (* push realtime flag: false by default *)
  6251. Emit(Push(position,false));
  6252. CallThis(position,"Heaps","NewRec",3);
  6253. END NewMathArrayDescriptor;
  6254. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  6255. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  6256. BEGIN
  6257. NEW(string, LEN(s)); COPY(s, string^);
  6258. sv := SyntaxTree.NewStringValue(Basic.invalidPosition,string);
  6259. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,Strings.Length(s));
  6260. sv.SetType(type);
  6261. Designate(sv,res);
  6262. Emit(Push(position,res.tag));
  6263. Emit(Push(position,res.op));
  6264. ReleaseOperand(res);
  6265. END PushConstString;
  6266. PROCEDURE PushConstBoolean(b: BOOLEAN);
  6267. BEGIN
  6268. IF b THEN
  6269. Emit(Push(Basic.invalidPosition, true));
  6270. ELSE
  6271. Emit(Push(Basic.invalidPosition, false));
  6272. END;
  6273. END PushConstBoolean;
  6274. PROCEDURE PushConstSet(v: SET);
  6275. VAR value: SyntaxTree.Value; op: Operand;
  6276. BEGIN
  6277. value := SyntaxTree.NewSetValue(Basic.invalidPosition, v);
  6278. value.SetType(system.setType);
  6279. Evaluate(value, op);
  6280. Emit(Push(Basic.invalidPosition, op.op));
  6281. ReleaseOperand(op);
  6282. END PushConstSet;
  6283. PROCEDURE PushConstInteger(v: LONGINT);
  6284. VAR value: SyntaxTree.Value; op: Operand;
  6285. BEGIN
  6286. value := SyntaxTree.NewIntegerValue(Basic.invalidPosition, v);
  6287. value.SetType(system.longintType);
  6288. Evaluate(value, op);
  6289. Emit(Push(Basic.invalidPosition, op.op));
  6290. ReleaseOperand(op);
  6291. END PushConstInteger;
  6292. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  6293. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  6294. section: IntermediateCode.Section;
  6295. BEGIN
  6296. procedureScope := SyntaxTree.NewProcedureScope(scope);
  6297. Global.GetSymbolSegmentedName(symbol, name);
  6298. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  6299. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition, SyntaxTree.NewIdentifier(""), procedureScope);
  6300. procedure.SetScope(moduleScope);
  6301. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,scope));
  6302. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  6303. procedure.SetAccess(SyntaxTree.Hidden);
  6304. currentScope := procedureScope;
  6305. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  6306. EmitEnter(section, Basic.invalidPosition,procedure,0,0,0);
  6307. RETURN section;
  6308. END OpenInitializer;
  6309. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  6310. BEGIN
  6311. EmitLeave(section, Basic.invalidPosition, NIL, 0 );
  6312. Emit(Exit(Basic.invalidPosition,0 ,0, 0));
  6313. section := prev;
  6314. END CloseInitializer;
  6315. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  6316. VAR name: SyntaxTree.IdentifierString;
  6317. variable: SyntaxTree.Variable;
  6318. parameter: SyntaxTree.Parameter;
  6319. type: SyntaxTree.Type;
  6320. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  6321. BEGIN
  6322. InitOperand(op,ModeReference);
  6323. op.op := fp;
  6324. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  6325. Dereference(op, x, FALSE);
  6326. result := op;
  6327. Symbol(symbol, op);
  6328. END Field;
  6329. PROCEDURE Direction(direction: LONGINT): SET;
  6330. BEGIN
  6331. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6332. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6333. ELSE HALT(100);
  6334. END;
  6335. END Direction;
  6336. PROCEDURE AddPortProperty(port: SyntaxTree.Parameter; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6337. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6338. BEGIN
  6339. Field(port, op);
  6340. ToMemory(op.op,addressType,0);
  6341. Emit(Push(Basic.invalidPosition, op.op));
  6342. ReleaseOperand(op);
  6343. Basic.GetString(modifier.identifier, name);
  6344. PushConstString(name);
  6345. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6346. ASSERT(SemanticChecker.IsStringType(value.type));
  6347. Designate(value, op);
  6348. Emit(Push(modifier.position, op.tag));
  6349. Emit(Push(modifier.position, op.op));
  6350. ReleaseOperand(op);
  6351. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6352. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6353. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6354. Evaluate(value, op);
  6355. Emit(Push(modifier.position, op.op));
  6356. ReleaseOperand(op);
  6357. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6358. ELSE
  6359. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6360. END;
  6361. END AddPortProperty;
  6362. PROCEDURE AddPortProperties(parameter: SyntaxTree.Parameter);
  6363. VAR modifier: SyntaxTree.Modifier;
  6364. BEGIN
  6365. modifier := parameter.modifiers;
  6366. WHILE modifier # NIL DO
  6367. AddPortProperty(parameter,modifier, modifier.expression);
  6368. modifier := modifier.nextModifier;
  6369. END;
  6370. END AddPortProperties;
  6371. PROCEDURE Parameter(name: ARRAY OF CHAR; parameter: SyntaxTree.Parameter);
  6372. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6373. size, reg: IntermediateCode.Operand; dim, len: LONGINT;
  6374. PROCEDURE PushLens(type: SyntaxTree.Type);
  6375. BEGIN
  6376. IF IsSemiDynamicArray(type) THEN
  6377. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6378. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6379. Emit(Push(Basic.invalidPosition, op.op));
  6380. ReleaseOperand(op);
  6381. INC(dim);
  6382. ELSIF IsStaticArray(type) THEN
  6383. len := len * type(SyntaxTree.ArrayType).staticLength;
  6384. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6385. INC(dim);
  6386. ELSE
  6387. baseType := type;
  6388. END;
  6389. END PushLens;
  6390. BEGIN
  6391. (* cell *)
  6392. IF parameter.type IS SyntaxTree.ArrayType THEN
  6393. type := parameter.type;
  6394. dim := 0;
  6395. len := 1;
  6396. PushLens(type);
  6397. portType := baseType.resolved(SyntaxTree.PortType);
  6398. ELSE
  6399. portType := parameter.type(SyntaxTree.PortType);
  6400. END;
  6401. PushSelfPointer();
  6402. (* port / array of ports *)
  6403. IF IsStaticArray(type) THEN
  6404. PushConstInteger(len);
  6405. END;
  6406. Field(parameter, op);
  6407. (*left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6408. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(x);
  6409. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, parameter); d.SetType(parameter.type);
  6410. Designate(d, op);*)
  6411. Emit(Push(Basic.invalidPosition, op.op));
  6412. ReleaseOperand(op);
  6413. (* name *)
  6414. PushConstString(name);
  6415. (* inout *)
  6416. PushConstSet(Direction(portType.direction));
  6417. (* width *)
  6418. PushConstInteger(portType.sizeInBits);
  6419. IF parameter.type IS SyntaxTree.PortType THEN
  6420. CallThis(parameter.position,"ActiveCellsRuntime","AddPort",6);
  6421. AddPortProperties(parameter);
  6422. ELSIF IsStaticArray(type)THEN
  6423. CallThis(parameter.position,"ActiveCellsRuntime","AddStaticPortArray",7);
  6424. ELSIF IsSemiDynamicArray(type) THEN
  6425. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6426. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6427. Emit(Add(position,reg, sp, size));
  6428. (* dim *)
  6429. PushConstInteger(dim);
  6430. (* len array *)
  6431. Emit(Push(position, reg));
  6432. ReleaseIntermediateOperand(reg);
  6433. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6434. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6435. Emit(Add(position, sp,sp, size));
  6436. ELSE
  6437. HALT(100);
  6438. END;
  6439. END Parameter;
  6440. BEGIN
  6441. IF backend.cellsAreObjects THEN
  6442. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  6443. AddPorts(cell, x.baseType.resolved(SyntaxTree.CellType));
  6444. END;
  6445. parameter := x.firstParameter;
  6446. WHILE (parameter # NIL) DO
  6447. type := parameter.type.resolved;
  6448. WHILE (type IS SyntaxTree.ArrayType) DO
  6449. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6450. END;
  6451. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6452. Global.GetSymbolNameInScope(parameter,x.cellScope,name);
  6453. Parameter(name,parameter);
  6454. END;
  6455. parameter := parameter.nextParameter;
  6456. END;
  6457. ELSE HALT(200)
  6458. END;
  6459. END AddPorts;
  6460. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6461. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6462. BEGIN
  6463. Symbol(cell,op);
  6464. ToMemory(op.op,addressType,0);
  6465. Emit(Push(position,op.op));
  6466. ReleaseOperand(op);
  6467. property.GetName(name);
  6468. (* does not work when inheritance is used:
  6469. Global.GetSymbolNameInScope(property, cellType.cellScope , name);
  6470. *)
  6471. PushConstString(name);
  6472. IF (value # NIL) THEN
  6473. ASSERT(
  6474. SemanticChecker.IsStringType(property.type)
  6475. OR (property.type.resolved IS SyntaxTree.IntegerType)
  6476. OR (property.type.resolved IS SyntaxTree.FloatType)
  6477. OR (property.type.resolved IS SyntaxTree.BooleanType)
  6478. OR (property.type.resolved IS SyntaxTree.SetType)
  6479. );
  6480. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,left,cell); left.SetType(system.anyType);
  6481. left := SyntaxTree.NewDereferenceDesignator(Basic.invalidPosition, left); left.SetType(cellType);
  6482. d := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, left, property); d.SetType(property.type);
  6483. Designate(d, op);
  6484. IF SemanticChecker.IsStringType(property.type) THEN
  6485. Emit(Push(Basic.invalidPosition, op.tag))
  6486. END;
  6487. Emit(Push(Basic.invalidPosition, op.op));
  6488. ReleaseOperand(op);
  6489. END;
  6490. IF value = NIL THEN
  6491. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6492. ELSIF SemanticChecker.IsStringType(property.type) THEN
  6493. ASSERT(SemanticChecker.IsStringType(value.type));
  6494. Designate(value, op);
  6495. PushString(op, value.type);
  6496. ReleaseOperand(op);
  6497. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6498. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6499. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6500. Evaluate(value, op);
  6501. Emit(Push(property.position, op.op));
  6502. ReleaseOperand(op);
  6503. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6504. ELSIF (property.type.resolved IS SyntaxTree.BooleanType) THEN
  6505. ASSERT(value.type.resolved IS SyntaxTree.BooleanType);
  6506. Evaluate(value, op);
  6507. Emit(Push(property.position, op.op));
  6508. ReleaseOperand(op);
  6509. CallThis(position,"ActiveCellsRuntime","AddBooleanProperty",5);
  6510. ELSIF (property.type.resolved IS SyntaxTree.FloatType) THEN
  6511. ASSERT((value.type.resolved IS SyntaxTree.FloatType) & (value.type.resolved(SyntaxTree.FloatType).sizeInBits =64));
  6512. Evaluate(value, op);
  6513. Emit(Push(property.position, op.op));
  6514. ReleaseOperand(op);
  6515. CallThis(position,"ActiveCellsRuntime","AddRealProperty",-1); (* must import *)
  6516. ELSIF (property.type.resolved IS SyntaxTree.SetType) THEN
  6517. ASSERT((value.type.resolved IS SyntaxTree.SetType));
  6518. Evaluate(value, op);
  6519. Emit(Push(property.position, op.op));
  6520. ReleaseOperand(op);
  6521. CallThis(position,"ActiveCellsRuntime","AddSetProperty",-1); (* must import *)
  6522. ELSE
  6523. HALT(200);
  6524. END;
  6525. END AddProperty;
  6526. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6527. VAR symbol: SyntaxTree.Symbol;
  6528. BEGIN
  6529. WHILE modifier # NIL DO
  6530. symbol := cellType.FindProperty(modifier.identifier);
  6531. ASSERT ((symbol # NIL) & (symbol IS SyntaxTree.Property));
  6532. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6533. modifier := modifier.nextModifier;
  6534. END;
  6535. END AddModifiers;
  6536. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6537. VAR last: SyntaxTree.Modifier;
  6538. BEGIN
  6539. IF to = NIL THEN
  6540. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6541. ELSE
  6542. last := to;
  6543. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6544. last := last.nextModifier;
  6545. END;
  6546. IF last.identifier # this.identifier THEN
  6547. ASSERT(last.nextModifier = NIL);
  6548. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6549. END;
  6550. END;
  6551. END AppendModifier;
  6552. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6553. BEGIN
  6554. WHILE this # NIL DO
  6555. AppendModifier(to, this);
  6556. this := this.nextModifier;
  6557. END;
  6558. END AppendModifiers;
  6559. PROCEDURE AppendCellTypeModifiers(VAR to: SyntaxTree.Modifier; c: SyntaxTree.CellType);
  6560. VAR base: SyntaxTree.Type;
  6561. BEGIN
  6562. AppendModifiers(to, c.modifiers);
  6563. base := c.GetBaseValueType();
  6564. IF (base # NIL) & (base IS SyntaxTree.CellType) THEN
  6565. AppendCellTypeModifiers(to, base(SyntaxTree.CellType))
  6566. END;
  6567. END AppendCellTypeModifiers;
  6568. PROCEDURE AddPortProperty(modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6569. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6570. BEGIN
  6571. Basic.GetString(modifier.identifier, name);
  6572. PushConstString(name);
  6573. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6574. ASSERT(SemanticChecker.IsStringType(value.type));
  6575. Designate(value, op);
  6576. PushString(op, value.type);
  6577. ReleaseOperand(op);
  6578. CallThisChecked(position,"ActiveCellsRuntime","AddPortStringProperty",4,FALSE);
  6579. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6580. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6581. Evaluate(value, op);
  6582. Emit(Push(modifier.position, op.op));
  6583. ReleaseOperand(op);
  6584. CallThisChecked(position,"ActiveCellsRuntime","AddPortIntegerProperty",3,FALSE);
  6585. ELSE
  6586. CallThisChecked(position,"ActiveCellsRuntime","AddPortFlagProperty",2,FALSE);
  6587. END;
  6588. END AddPortProperty;
  6589. PROCEDURE AddPortProperties(modifier: SyntaxTree.Modifier);
  6590. BEGIN
  6591. WHILE modifier # NIL DO
  6592. AddPortProperty(modifier, modifier.expression);
  6593. modifier := modifier.nextModifier;
  6594. END;
  6595. END AddPortProperties;
  6596. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6597. VAR op: Operand;
  6598. BEGIN
  6599. Evaluate(p, op);
  6600. Emit(Push(p.position, op.op));
  6601. ReleaseOperand(op);
  6602. IF p IS SyntaxTree.Designator THEN
  6603. AddPortProperties(p(SyntaxTree.Designator).modifiers)
  6604. END;
  6605. END PushPort;
  6606. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6607. VAR tmp: IntermediateCode.Operand;
  6608. BEGIN
  6609. actualType := actualType.resolved;
  6610. IF actualType IS SyntaxTree.StringType THEN
  6611. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6612. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6613. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6614. ELSE
  6615. tmp := op.tag;
  6616. IntermediateCode.MakeMemory(tmp,addressType);
  6617. Emit(Push(position,tmp));
  6618. END;
  6619. Emit(Push(position,op.op))
  6620. END PushString;
  6621. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6622. VAR
  6623. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6624. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6625. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6626. tmp:IntermediateCode.Operand;
  6627. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6628. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6629. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6630. dest: IntermediateCode.Operand;
  6631. staticLength: LONGINT; itype: IntermediateCode.Type;
  6632. convert,isTensor: BOOLEAN;
  6633. recordType: SyntaxTree.RecordType;
  6634. baseType: SyntaxTree.Type;
  6635. flags: SET;
  6636. left: SyntaxTree.Expression;
  6637. call: SyntaxTree.Designator;
  6638. procedure: SyntaxTree.Procedure;
  6639. temporaryVariable: SyntaxTree.Variable;
  6640. dummy: IntermediateCode.Operand;
  6641. customBuiltin: SyntaxTree.CustomBuiltin;
  6642. isVarPar: ARRAY 3 OF BOOLEAN;
  6643. callsection: Sections.Section;
  6644. segmentedName: Basic.SegmentedName;
  6645. needsTrace: BOOLEAN;
  6646. n: ARRAY 256 OF CHAR;
  6647. modifier: SyntaxTree.Modifier;
  6648. previous, init: IntermediateCode.Section;
  6649. prevScope: SyntaxTree.Scope;
  6650. firstPar: LONGINT;
  6651. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6652. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6653. priority: IntermediateCode.Operand;
  6654. op,callop: Operand;
  6655. BEGIN
  6656. IF type = NIL THEN RETURN END;
  6657. type := type.resolved;
  6658. IF type IS SyntaxTree.PointerType THEN
  6659. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6660. END;
  6661. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6662. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6663. recordScope := type(SyntaxTree.RecordType).recordScope;
  6664. IF recordScope.bodyProcedure # NIL THEN
  6665. procedure := recordScope.bodyProcedure;
  6666. body := procedure.procedureScope.body;
  6667. Emit(Push(position,self));
  6668. IF body.isActive THEN
  6669. StaticCallOperand(callop,procedure);
  6670. Emit(Push(position,callop.op));
  6671. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6672. Convert(priority,sizeType);
  6673. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6674. END;
  6675. Emit(Push(position,priority));
  6676. ReleaseIntermediateOperand(priority);
  6677. IF backend.cooperative THEN
  6678. Emit(Push(position,self));
  6679. CallThis(position,"Activities","Create",3)
  6680. ELSE
  6681. flags := 0;
  6682. IF body.isSafe THEN
  6683. flags := 1;
  6684. END;
  6685. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6686. Emit(Push(position,self));
  6687. CallThis(position,"Objects","CreateProcess",4)
  6688. END;
  6689. ELSE
  6690. Emit(Push(position,self));
  6691. StaticCallOperand(callop,procedure);
  6692. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6693. END;
  6694. Emit(Pop(position,self));
  6695. END;
  6696. END CallBodies;
  6697. PROCEDURE PushTD(type: SyntaxTree.Type);
  6698. VAR op: IntermediateCode.Operand;
  6699. BEGIN
  6700. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6701. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6702. ELSE
  6703. IF type.resolved IS SyntaxTree.PointerType THEN
  6704. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6705. END;
  6706. op := TypeDescriptorAdr(type.resolved);
  6707. IF ~newObjectFile THEN
  6708. IntermediateCode.MakeMemory(op,addressType);
  6709. END;
  6710. Emit(Push(position,op));
  6711. END
  6712. END PushTD;
  6713. BEGIN
  6714. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6715. dest := destination; destination := emptyOperand;
  6716. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6717. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6718. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6719. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6720. CASE x.id OF
  6721. (* ---- COPY ----- *)
  6722. |Global.Copy:
  6723. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6724. (* ---- EXCL, INCL----- *)
  6725. |Global.Excl,Global.Incl:
  6726. Evaluate(p0,s0);
  6727. Evaluate(p1,s1);
  6728. Convert(s1.op,setType);
  6729. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6730. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6731. END;
  6732. ReuseCopy(res,s0.op);
  6733. ReleaseOperand(s0);
  6734. Reuse1(tmp,s1.op);
  6735. ReleaseOperand(s1);
  6736. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6737. IF x.id = Global.Excl THEN
  6738. Emit(Not(position,tmp,tmp));
  6739. Emit(And(position,res,res,tmp));
  6740. ELSE
  6741. Emit(Or(position,res,res,tmp));
  6742. END;
  6743. ReleaseIntermediateOperand(tmp);
  6744. Designate(p0,s0);
  6745. ToMemory(s0.op,setType,0);
  6746. Emit(Mov(position,s0.op,res));
  6747. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6748. (* ---- DISPOSE ----- *)
  6749. |Global.Dispose:
  6750. Designate(p0,s0);
  6751. Emit(Push(position,s0.op));
  6752. ReleaseOperand(s0);
  6753. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6754. (* ---- GETPROCEDURE ----- *)
  6755. |Global.GetProcedure:
  6756. Designate(p0,s0);
  6757. PushString(s0,p0.type);
  6758. Designate(p1,s1);
  6759. PushString(s1,p1.type);
  6760. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6761. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6762. ELSE PushTD(procedureType.firstParameter.type)
  6763. END;
  6764. PushTD(procedureType.returnType);
  6765. Designate(p2,s2);
  6766. Emit(Push(position,s2.op));
  6767. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6768. CallThis(position,"Modules","GetProcedure", 7);
  6769. (* ---- ASH, LSH, ROT ----- *)
  6770. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6771. Evaluate(p0,s0);
  6772. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6773. (* make unsigned arguments in order to produced a logical shift *)
  6774. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6775. convert:= TRUE;
  6776. itype := s0.op.type;
  6777. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6778. Convert(s0.op,itype);
  6779. ELSE
  6780. convert := FALSE;
  6781. END;
  6782. END;
  6783. Evaluate(p1,s1);
  6784. IF IsIntegerConstant(p1,hint) THEN
  6785. ReuseCopy(reg,s0.op);
  6786. IF hint > 0 THEN
  6787. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6788. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6789. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6790. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6791. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6792. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6793. END;
  6794. ELSIF hint < 0 THEN
  6795. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6796. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6797. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6798. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6799. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6800. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6801. END;
  6802. END;
  6803. ReleaseOperand(s0); ReleaseOperand(s1);
  6804. ELSE
  6805. exit := NewLabel();
  6806. end := NewLabel();
  6807. ReuseCopy(reg,s0.op);
  6808. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6809. Reuse1(tmp,s1.op);
  6810. Emit(Neg(position,tmp,s1.op));
  6811. Convert(tmp,s1.op.type);
  6812. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6813. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6814. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6815. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6816. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6817. END;
  6818. ReleaseIntermediateOperand(tmp);
  6819. BrL(end);
  6820. SetLabel(exit);
  6821. ReuseCopy(tmp,s1.op);
  6822. Convert(tmp,s1.op.type);
  6823. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6824. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6825. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6826. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6827. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6828. END;
  6829. ReleaseIntermediateOperand(tmp);
  6830. SetLabel(end);
  6831. ReleaseOperand(s0); ReleaseOperand(s1);
  6832. END;
  6833. InitOperand(result,ModeValue);
  6834. IF convert THEN
  6835. itype := reg.type;
  6836. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6837. Convert(reg,itype);
  6838. END;
  6839. result.op := reg;
  6840. (* ---- CAP ----- *)
  6841. |Global.Cap:
  6842. Evaluate(p0,result);
  6843. ReuseCopy(reg,result.op);
  6844. ReleaseIntermediateOperand(result.op);
  6845. ignore := NewLabel();
  6846. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6847. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6848. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6849. SetLabel(ignore);
  6850. result.op := reg;
  6851. (* ---- CHR ----- *)
  6852. |Global.Chr, Global.Chr32:
  6853. Evaluate(p0,result);
  6854. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6855. |Global.Entier, Global.EntierH:
  6856. Evaluate(p0,result);
  6857. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6858. (* ---- MIN and MAX ----- *)
  6859. |Global.Max,Global.Min:
  6860. Evaluate(p0,s0);
  6861. Evaluate(p1,s1);
  6862. Reuse2(res,s0.op,s1.op);
  6863. else := NewLabel();
  6864. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6865. ELSE BrltL(else,s1.op,s0.op) END;
  6866. Emit(Mov(position,res,s0.op));
  6867. ReleaseOperand(s0);
  6868. end := NewLabel();
  6869. BrL(end);
  6870. SetLabel(else);
  6871. Emit(MovReplace(position,res,s1.op));
  6872. SetLabel(end);
  6873. ReleaseOperand(s1);
  6874. InitOperand(result,ModeValue);
  6875. result.op := res;
  6876. (* ---- ODD ----- *)
  6877. |Global.Odd:
  6878. IF ~conditional THEN
  6879. ConditionToValue(x)
  6880. ELSE
  6881. Evaluate(p0,result);
  6882. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6883. Reuse1(res,result.op);
  6884. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6885. ReleaseIntermediateOperand(result.op);
  6886. result.op := res;
  6887. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6888. ReleaseOperand(result);
  6889. BrL(falseLabel);
  6890. END;
  6891. (* ---- ORD ----- *)
  6892. |Global.Ord, Global.Ord32:
  6893. Evaluate(p0,result);
  6894. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6895. (* ---- SHORT, LONG ----- *)
  6896. |Global.Short, Global.Long:
  6897. Evaluate(p0,result);
  6898. IF x.type IS SyntaxTree.ComplexType THEN
  6899. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6900. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6901. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6902. ELSE
  6903. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6904. END
  6905. (* ---- HALT, SYSTEM.HALT----- *)
  6906. |Global.Halt, Global.systemHalt:
  6907. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6908. EmitTrap (position, val);
  6909. (* ---- ASSERT ----- *)
  6910. |Global.Assert:
  6911. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6912. trueL := NewLabel();
  6913. falseL := NewLabel();
  6914. Condition(p0,trueL,falseL);
  6915. IF p1 = NIL THEN val := AssertTrap
  6916. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6917. END;
  6918. SetLabel(falseL);
  6919. EmitTrap(position,val);
  6920. SetLabel(trueL);
  6921. END;
  6922. (*
  6923. Emit(TrapC(result.op,val);
  6924. *)
  6925. (* ---- INC, DEC----- *)
  6926. |Global.Inc,Global.Dec:
  6927. Expression(p0); adr := result.op;
  6928. LoadValue(result,p0.type); l := result;
  6929. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6930. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6931. END;
  6932. IF x.id = Global.Inc THEN
  6933. Emit(Add(position,l.op,l.op,r.op));
  6934. ELSE
  6935. Emit(Sub(position,l.op,l.op,r.op));
  6936. END;
  6937. ReleaseOperand(l); ReleaseOperand(r);
  6938. (* ---- LEN ----- *)
  6939. |Global.Len: (* dynamic length, static length done by checker *)
  6940. Designate(p0,operand);
  6941. IF p1 = NIL THEN
  6942. InitOperand(l,ModeValue);
  6943. l.op := IntermediateCode.Immediate(int32,0);
  6944. ELSE
  6945. Evaluate(p1,l);
  6946. END;
  6947. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6948. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6949. Dereference(operand, p0.type.resolved, FALSE);
  6950. END;
  6951. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6952. ReleaseOperand(operand); ReleaseOperand(l);
  6953. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6954. ASSERT(p1 # NIL);
  6955. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6956. Dereference(operand,p0.type.resolved,FALSE);
  6957. END;
  6958. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6959. ReleaseOperand(operand); ReleaseOperand(l);
  6960. ELSE HALT(100);
  6961. END;
  6962. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6963. (* ---- FIRST ---- *)
  6964. |Global.First:
  6965. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6966. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6967. ELSE
  6968. Designate(p0, result)
  6969. END
  6970. (* ---- LAST ---- *)
  6971. |Global.Last:
  6972. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6973. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6974. ELSE
  6975. Designate(p0, result);
  6976. (* make sure result.op is a register *)
  6977. tmp := result.op;
  6978. ReuseCopy(result.op, result.op);
  6979. ReleaseIntermediateOperand(tmp);
  6980. (* add offset to result.op *)
  6981. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6982. END
  6983. (* ---- STEP ---- *)
  6984. |Global.Step:
  6985. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6986. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6987. ELSE
  6988. Designate(p0, result);
  6989. (* make sure result.op is a register *)
  6990. tmp := result.op;
  6991. ReuseCopy(result.op, result.op);
  6992. ReleaseIntermediateOperand(tmp);
  6993. (* add offset to result.op *)
  6994. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6995. END
  6996. (* ---- RE ---- *)
  6997. |Global.Re:
  6998. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6999. Designate(p0, result)
  7000. ELSE
  7001. Evaluate(p0, result)
  7002. END
  7003. (* ---- IM ---- *)
  7004. |Global.Im:
  7005. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  7006. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  7007. Designate(p0, result);
  7008. (* make sure result.op is a register *)
  7009. tmp := result.op;
  7010. ReuseCopy(result.op, result.op);
  7011. ReleaseIntermediateOperand(tmp);
  7012. (* add offset to result.op *)
  7013. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  7014. (* ---- ABS ----- *)
  7015. |Global.Abs:
  7016. Evaluate(p0,operand);
  7017. type := p0.type.resolved;
  7018. InitOperand(result,ModeValue);
  7019. Reuse1a(result.op,operand.op,dest);
  7020. Emit(Abs(position,result.op,operand.op));
  7021. ReleaseOperand(operand);
  7022. (* ---- WAIT ----- *)
  7023. |Global.Wait:
  7024. Evaluate(p0,operand);
  7025. Emit(Push(position,operand.op));
  7026. ReleaseOperand(operand);
  7027. CallThis(position,"Activities","Wait", 1);
  7028. (* ---- NEW ----- *)
  7029. |Global.New:
  7030. (*! the following code is only correct for "standard" Oberon calling convention *)
  7031. IF x.type # NIL THEN
  7032. type := x.type.resolved;
  7033. firstPar := 0;
  7034. ELSE
  7035. type := p0.type.resolved;
  7036. firstPar := 1;
  7037. END;
  7038. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  7039. THEN
  7040. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  7041. IF backend.cooperative THEN
  7042. size := ToMemoryUnits(system,system.SizeOf(recordType));
  7043. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7044. IF recordType.isObject THEN
  7045. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7046. IF recordType.IsActive() THEN
  7047. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7048. END;
  7049. IF recordType.IsProtected() THEN
  7050. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  7051. END;
  7052. ELSE
  7053. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7054. END;
  7055. END;
  7056. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  7057. CallThis(position,"Runtime","New", 1);
  7058. pointer := NewRegisterOperand(IntermediateCode.GetType(system, type));
  7059. Emit(Result(position, pointer));
  7060. exit := NewLabel();
  7061. BreqL(exit,pointer,nil);
  7062. IF ~type(SyntaxTree.PointerType).isPlain THEN
  7063. GetRecordTypeName (recordType,name);
  7064. IF ~recordType.isObject THEN
  7065. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  7066. END;
  7067. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  7068. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  7069. IF recordType.isObject THEN
  7070. IF recordType.IsProtected() THEN
  7071. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7072. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7073. END;
  7074. IF recordType.IsActive() THEN
  7075. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7076. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  7077. END;
  7078. END;
  7079. END;
  7080. (* initialize fields *)
  7081. IF type(SyntaxTree.PointerType).isPlain THEN
  7082. size := 0;
  7083. ELSIF recordType.isObject THEN
  7084. size := BaseObjectTypeSize;
  7085. ELSE
  7086. size := BaseRecordTypeSize;
  7087. END;
  7088. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  7089. (* call initializer *)
  7090. constructor := GetConstructor(recordType);
  7091. IF constructor # NIL THEN
  7092. (*! should be unified with ProcedureCallDesignator *)
  7093. Emit(Push(position,pointer));
  7094. ReleaseIntermediateOperand(pointer);
  7095. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7096. FOR i := firstPar TO x.parameters.Length()-1 DO
  7097. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7098. formalParameter := formalParameter.nextParameter;
  7099. END;
  7100. (* static call of the constructor *)
  7101. GetCodeSectionNameForSymbol(constructor,name);
  7102. ASSERT(~constructor.isInline);
  7103. IF constructor.scope.ownerModule # module.module THEN
  7104. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7105. ELSE
  7106. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7107. END;
  7108. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  7109. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7110. Emit(Pop(position,pointer));
  7111. END;
  7112. (* call bodies *)
  7113. CallBodies(pointer,type);
  7114. SetLabel(exit);
  7115. needsTrace := p0.NeedsTrace();
  7116. IF needsTrace THEN ModifyAssignments(true) END;
  7117. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  7118. Emit(Push(position, pointer));
  7119. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7120. Emit(Pop(position, pointer));
  7121. END;
  7122. Designate(p0,l);
  7123. IF needsTrace THEN
  7124. CallAssignPointer(l.op, pointer);
  7125. ELSE
  7126. ToMemory(l.op,addressType,0);
  7127. Emit(Mov(position,l.op,pointer));
  7128. END;
  7129. ReleaseIntermediateOperand(pointer);
  7130. ReleaseOperand(l);
  7131. IF needsTrace THEN ModifyAssignments(false) END;
  7132. ELSE (* not cooperative backend *)
  7133. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7134. IF temporaryVariable # NIL THEN
  7135. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7136. ELSE
  7137. Designate(p0,l);
  7138. END;
  7139. (* l.op contains address of pointer to record *)
  7140. Emit(Push(position,l.op)); (* address for use after syscall *)
  7141. Emit(Push(position,l.op));
  7142. ReleaseOperand(l);
  7143. (* push type descriptor *)
  7144. reg := TypeDescriptorAdr(recordType);
  7145. IF ~newObjectFile THEN
  7146. IntermediateCode.MakeMemory(reg,addressType);
  7147. END;
  7148. Emit(Push(position,reg));
  7149. ReleaseIntermediateOperand(reg);
  7150. (* push realtime flag *)
  7151. IF type.resolved.isRealtime THEN Emit(Push(position,true));
  7152. ELSE Emit(Push(position,false));
  7153. END;
  7154. CallThis(position,"Heaps","NewRec", 3);
  7155. (* check allocation success, if not successful then do not call initializers and bodies *)
  7156. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7157. Emit(Pop(position,pointer));
  7158. MakeMemory(reg,pointer,addressType,0);
  7159. ReleaseIntermediateOperand(pointer);
  7160. pointer := reg;
  7161. exit := NewLabel();
  7162. BreqL(exit,pointer,nil);
  7163. Emit(Push(position,pointer));
  7164. (* initialize fields *)
  7165. InitFields(recordType, pointer,0);
  7166. (* call initializer *)
  7167. constructor := GetConstructor(recordType);
  7168. IF constructor # NIL THEN
  7169. (*! should be unified with ProcedureCallDesignator *)
  7170. Emit(Push(position,pointer));
  7171. ReleaseIntermediateOperand(pointer);
  7172. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7173. FOR i := firstPar TO x.parameters.Length()-1 DO
  7174. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7175. formalParameter := formalParameter.nextParameter;
  7176. END;
  7177. (* static call of the constructor *)
  7178. GetCodeSectionNameForSymbol(constructor,name);
  7179. ASSERT(~constructor.isInline);
  7180. IF constructor.scope.ownerModule # module.module THEN
  7181. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7182. ELSE
  7183. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  7184. END;
  7185. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7186. ELSE
  7187. ReleaseIntermediateOperand(pointer);
  7188. END;
  7189. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7190. Emit(Pop(position,pointer));
  7191. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7192. Designate(p0,l);
  7193. ToMemory(l.op,addressType,0);
  7194. Emit(Mov(position,l.op,pointer));
  7195. ReleaseOperand(l);
  7196. result.tag := emptyOperand;
  7197. ELSIF (x.type # NIL) THEN
  7198. result := l; (* temporary variable is the result of NEW Type() *)
  7199. END;
  7200. (* call bodies *)
  7201. CallBodies(pointer,type);
  7202. ReleaseIntermediateOperand(pointer);
  7203. IF (temporaryVariable # NIL) & (x.type = NIL) THEN
  7204. end := NewLabel();
  7205. BrL(end);
  7206. SetLabel(exit);
  7207. Designate(p0,l);
  7208. ToMemory(l.op,addressType,0);
  7209. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7210. ReleaseOperand(l);
  7211. SetLabel(end);
  7212. ELSE
  7213. SetLabel(exit);
  7214. END;
  7215. END;
  7216. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  7217. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  7218. dim := 0;
  7219. IntermediateCode.InitOperand(reg);
  7220. IF p1 # NIL THEN
  7221. FOR i := firstPar TO x.parameters.Length()-1 DO
  7222. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7223. parameter := x.parameters.GetExpression(i);
  7224. Evaluate(parameter,r);
  7225. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7226. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7227. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7228. END;
  7229. Emit(Push(position,r.op));
  7230. IF i=1 THEN
  7231. CopyInt(reg,r.op);
  7232. ELSE
  7233. MulInt(reg, reg, r.op);
  7234. END;
  7235. ReleaseOperand(r);
  7236. INC(dim);
  7237. END;
  7238. Convert(reg,addressType);
  7239. ELSE
  7240. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7241. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  7242. END;
  7243. openDim := dim;
  7244. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  7245. IF backend.cooperative THEN
  7246. size := ToMemoryUnits(system,system.SizeOf(type));
  7247. WHILE type IS SyntaxTree.ArrayType DO
  7248. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7249. END;
  7250. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  7251. IF (size # 1) THEN
  7252. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7253. END;
  7254. Emit(Push(position,reg));
  7255. size := ToMemoryUnits(system,system.SizeOf(type));
  7256. IF (size # 1) THEN
  7257. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7258. END;
  7259. AddInt(reg, reg, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize)));
  7260. (*Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));*)
  7261. Emit(Push(position,reg));
  7262. ReleaseIntermediateOperand(reg);
  7263. CallThis(position,"Runtime","New", 1);
  7264. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7265. Emit(Result(position, pointer));
  7266. exit := NewLabel();
  7267. else := NewLabel();
  7268. BreqL(else,pointer,nil);
  7269. IF ~type.hasPointers THEN
  7270. Basic.ToSegmentedName ("BaseTypes.Array",name);
  7271. ELSIF type IS SyntaxTree.RecordType THEN
  7272. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  7273. ELSIF type IS SyntaxTree.ProcedureType THEN
  7274. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  7275. ELSE
  7276. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  7277. END;
  7278. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  7279. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  7280. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DataOffset * system.addressSize)),IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  7281. IF type IS SyntaxTree.RecordType THEN
  7282. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  7283. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  7284. ELSE
  7285. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  7286. END;
  7287. i := openDim;
  7288. WHILE i > 0 DO
  7289. DEC (i);
  7290. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  7291. END;
  7292. needsTrace := p0.NeedsTrace();
  7293. IF needsTrace THEN ModifyAssignments(true) END;
  7294. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  7295. Emit(Push(position, pointer));
  7296. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  7297. Emit(Pop(position, pointer));
  7298. END;
  7299. Designate(p0,l);
  7300. IF needsTrace THEN
  7301. CallAssignPointer(l.op, pointer);
  7302. ModifyAssignments(false);
  7303. ELSE
  7304. ToMemory(l.op,addressType,0);
  7305. Emit(Mov(position,l.op,pointer));
  7306. END;
  7307. ReleaseIntermediateOperand(pointer);
  7308. ReleaseOperand(l);
  7309. BrL(exit);
  7310. SetLabel(else);
  7311. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  7312. Designate(p0,l);
  7313. IF needsTrace THEN
  7314. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  7315. ELSE
  7316. ToMemory(l.op,addressType,0);
  7317. Emit(Mov(position,l.op,pointer));
  7318. END;
  7319. ReleaseOperand(l);
  7320. SetLabel(exit);
  7321. ELSE
  7322. (*! the following code is only correct for "standard" Oberon calling convention *)
  7323. IF SemanticChecker.ContainsPointer(type) THEN
  7324. IF type IS SyntaxTree.ArrayType THEN
  7325. staticLength := 1;
  7326. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  7327. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  7328. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  7329. END;
  7330. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7331. MulInt(reg,reg,tmp);
  7332. END;
  7333. Designate(p0,l);
  7334. IF openDim > 0 THEN
  7335. Emit(Push(position,l.op)); (* address for use after syscall *)
  7336. END;
  7337. Emit(Push(position,l.op)); (* address *)
  7338. ReleaseOperand(l);
  7339. tmp := TypeDescriptorAdr(type);
  7340. IF ~newObjectFile THEN
  7341. IntermediateCode.MakeMemory(tmp,addressType);
  7342. END;
  7343. Emit(Push(position,tmp)); (* type descriptor *)
  7344. ReleaseIntermediateOperand(tmp);
  7345. Emit(Push(position,reg)); (* number Elements *)
  7346. ReleaseIntermediateOperand(reg);
  7347. tmp := IntermediateCode.Immediate(addressType,dim);
  7348. Emit(Push(position,tmp)); (* dimensions *)
  7349. (* push realtime flag *)
  7350. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7351. ELSE Emit(Push(position,false));
  7352. END;
  7353. CallThis(position,"Heaps","NewArr",5)
  7354. ELSE
  7355. size := ToMemoryUnits(system,system.SizeOf(type));
  7356. IF (size # 1) THEN
  7357. MulInt(reg, reg, IntermediateCode.Immediate(addressType,size));
  7358. (*
  7359. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7360. *)
  7361. END;
  7362. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7363. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7364. AddInt(reg, reg, tmp);
  7365. (*
  7366. Emit(Add(position,reg,reg,tmp));
  7367. *)
  7368. Designate(p0,l);
  7369. IF openDim >0 THEN
  7370. Emit(Push(position,l.op)); (* address for use after syscall *)
  7371. END;
  7372. Emit(Push(position,l.op)); (* address for syscall *)
  7373. ReleaseOperand(l); (* pointer address *)
  7374. Emit(Push(position,reg)); (* size *)
  7375. ReleaseIntermediateOperand(reg);
  7376. (* push realtime flag *)
  7377. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7378. ELSE Emit(Push(position,false));
  7379. END;
  7380. CallThis(position,"Heaps","NewSys", 3)
  7381. END;
  7382. IF openDim > 0 THEN
  7383. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7384. Emit(Pop(position,adr));
  7385. ToMemory(adr,addressType,0);
  7386. ReuseCopy(tmp,adr);
  7387. ReleaseIntermediateOperand(adr);
  7388. adr := tmp;
  7389. else := NewLabel();
  7390. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7391. i := openDim-1;
  7392. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7393. WHILE (i >= 0) DO
  7394. Emit(Pop(position,reg));
  7395. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7396. Emit(Mov(position,res,reg));
  7397. DEC(i);
  7398. END;
  7399. ReleaseIntermediateOperand(adr);
  7400. ReleaseIntermediateOperand(reg);
  7401. exit := NewLabel();
  7402. BrL(exit);
  7403. SetLabel(else);
  7404. (* else part: array could not be allocated *)
  7405. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7406. Emit(Add(position,sp,sp,tmp));
  7407. SetLabel(exit);
  7408. END;
  7409. END;
  7410. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7411. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7412. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7413. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7414. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7415. left.SetType(procedure.type);
  7416. formalParameter := procedureType.firstParameter;
  7417. (* push array to allocate *)
  7418. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7419. formalParameter :=formalParameter.nextParameter;
  7420. (* push length array *)
  7421. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7422. (* push size *)
  7423. type := t0;
  7424. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7425. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7426. END;
  7427. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7428. Emit(Push(position,tmp));
  7429. (* *)
  7430. IF SemanticChecker.ContainsPointer(type) THEN
  7431. tmp := TypeDescriptorAdr(type);
  7432. IF ~newObjectFile THEN
  7433. IntermediateCode.MakeMemory(tmp,addressType);
  7434. END;
  7435. ELSE
  7436. tmp := IntermediateCode.Immediate(addressType, 0);
  7437. END;
  7438. Emit(Push(position,tmp)); (* type descriptor *)
  7439. StaticCallOperand(result,procedure);
  7440. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7441. ReleaseOperand(result);
  7442. END;
  7443. (*
  7444. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7445. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7446. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7447. *)
  7448. ELSE
  7449. dim := 0;
  7450. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7451. (* generate geometry descriptor *)
  7452. Designate(p0,l);
  7453. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7454. ReleaseOperand(l);
  7455. isTensor := TRUE;
  7456. ELSE
  7457. isTensor := FALSE;
  7458. END;
  7459. FOR i := firstPar TO x.parameters.Length()-1 DO
  7460. IF ~isTensor THEN
  7461. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7462. END;
  7463. parameter := x.parameters.GetExpression(i);
  7464. Evaluate(parameter,r);
  7465. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7466. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7467. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7468. END;
  7469. Emit(Push(position,r.op));
  7470. IF i=1 THEN
  7471. CopyInt(reg, r.op);
  7472. ELSE
  7473. MulInt(reg, reg, r.op);
  7474. END;
  7475. ReleaseOperand(r);
  7476. INC(dim);
  7477. END;
  7478. Convert(reg,addressType);
  7479. openDim := dim;
  7480. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7481. (*! the following code is only correct for "standard" Oberon calling convention *)
  7482. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7483. t := type;
  7484. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7485. staticLength := 1;
  7486. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7487. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7488. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7489. END;
  7490. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7491. MulInt(reg,reg,tmp);
  7492. END;
  7493. Designate(p0,l);
  7494. IF isTensor THEN
  7495. Dereference(l,type,FALSE);
  7496. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7497. END;
  7498. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7499. Emit(Push(position,l.tag)); (* address *)
  7500. ReleaseOperand(l);
  7501. tmp := TypeDescriptorAdr(t);
  7502. IF ~newObjectFile THEN
  7503. IntermediateCode.MakeMemory(tmp,addressType);
  7504. END;
  7505. Emit(Push(position,tmp)); (* type descriptor *)
  7506. ReleaseIntermediateOperand(tmp);
  7507. Emit(Push(position,reg)); (* number Elements *)
  7508. ReleaseIntermediateOperand(reg);
  7509. tmp := IntermediateCode.Immediate(addressType,0);
  7510. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7511. (* push realtime flag: false by default *)
  7512. Emit(Push(position,false));
  7513. CallThis(position,"Heaps","NewArr",5);
  7514. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7515. Emit(Pop(position,adr));
  7516. GetMathArrayField(tmp,adr,MathPtrOffset);
  7517. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7518. AddInt(reg, tmp, IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset)));
  7519. PutMathArrayField(adr,reg,MathAdrOffset);
  7520. ReleaseIntermediateOperand(tmp);
  7521. ReleaseIntermediateOperand(reg);
  7522. ELSE
  7523. IF isTensor THEN
  7524. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7525. ELSE
  7526. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7527. END;
  7528. IF (size # 1) THEN
  7529. MulInt(reg,reg,IntermediateCode.Immediate(addressType,size)); (*! optimize the multiplication of immediate operands *)
  7530. END;
  7531. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7532. AddInt(reg,reg,tmp);
  7533. Designate(p0,l);
  7534. IF isTensor THEN
  7535. Dereference(l,type,FALSE);
  7536. END;
  7537. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7538. Emit(Push(position,l.tag)); (* address for syscall *)
  7539. ReleaseOperand(l); (* pointer address *)
  7540. Emit(Push(position,reg)); (* size *)
  7541. ReleaseIntermediateOperand(reg);
  7542. (* push realtime flag: false by default *)
  7543. Emit(Push(position,false));
  7544. CallThis(position,"Heaps","NewSys",3);
  7545. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7546. Emit(Pop(position,adr));
  7547. GetMathArrayField(tmp,adr,MathPtrOffset);
  7548. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7549. AddInt(reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset)));
  7550. PutMathArrayField(adr,reg,MathAdrOffset);
  7551. ReleaseIntermediateOperand(tmp);
  7552. ReleaseIntermediateOperand(reg);
  7553. END;
  7554. flags := {};
  7555. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7556. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7557. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7558. PutMathArrayField(adr,tmp,MathDimOffset);
  7559. else := NewLabel();
  7560. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7561. i := openDim-1;
  7562. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7563. IF isTensor THEN
  7564. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7565. ELSE
  7566. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7567. END;
  7568. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7569. WHILE (i >= 0) DO
  7570. Emit(Pop(position,reg));
  7571. PutMathArrayLength(adr,reg,i);
  7572. PutMathArrayIncrement(adr,tmp,i);
  7573. IF i > 0 THEN
  7574. IF i=openDim-1 THEN
  7575. CopyInt(tmp,tmp);
  7576. END;
  7577. MulInt(tmp,tmp,reg);
  7578. END;
  7579. DEC(i);
  7580. END;
  7581. ReleaseIntermediateOperand(adr);
  7582. ReleaseIntermediateOperand(reg);
  7583. ReleaseIntermediateOperand(tmp);
  7584. exit := NewLabel();
  7585. BrL(exit);
  7586. SetLabel(else);
  7587. (* else part: array could not be allocated *)
  7588. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7589. Emit(Add(position,sp,sp,tmp));
  7590. SetLabel(exit);
  7591. END;
  7592. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7593. THEN
  7594. IF ~backend.cellsAreObjects THEN RETURN END;
  7595. IF InCellScope(currentScope) THEN
  7596. PushSelfPointer()
  7597. ELSE
  7598. Emit(Push(position, nil));
  7599. END;
  7600. (* push temp address *)
  7601. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7602. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7603. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7604. (* l.op contains address of pointer to record *)
  7605. Emit(Push(position,l.op)); (* address for use after syscall *)
  7606. ReleaseOperand(l);
  7607. (* push type descriptor *)
  7608. reg := TypeDescriptorAdr(baseType);
  7609. IF ~newObjectFile THEN
  7610. IntermediateCode.MakeMemory(reg,addressType);
  7611. END;
  7612. Emit(Push(position,reg));
  7613. ReleaseIntermediateOperand(reg);
  7614. (* push name *)
  7615. (*Global.GetSymbolName(p0, n);*)
  7616. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7617. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7618. ELSE
  7619. Global.GetModuleName(module.module, n);
  7620. END;
  7621. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position.start);
  7622. (*type.typeDeclaration.GetName(n);*)
  7623. PushConstString(n);
  7624. (* push cellnet boolean *)
  7625. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7626. (* push engine boolean *)
  7627. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7628. (* allocate *)
  7629. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7630. (* add capabilities *)
  7631. modifier := p0(SyntaxTree.Designator).modifiers;
  7632. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7633. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7634. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7635. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7636. END;
  7637. AppendCellTypeModifiers(modifier, baseType(SyntaxTree.CellType));
  7638. (*
  7639. modifier := baseType(SyntaxTree.CellType).modifiers;
  7640. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7641. modifier := p0(SyntaxTree.Designator).modifiers;
  7642. *)
  7643. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7644. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7645. (* l.op contains address of pointer to record *)
  7646. ToMemory(l.op,addressType,0);
  7647. (* l.op contains value of pointer to record *)
  7648. Emit(Push(position,l.op)); (* address for use after syscall *)
  7649. ReleaseOperand(l);
  7650. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7651. prevScope := currentScope;
  7652. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7653. previous := section;
  7654. section := init;
  7655. (* add ports *)
  7656. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7657. CloseInitializer(previous);
  7658. currentScope := prevScope;
  7659. Symbol(temporaryVariable,l);
  7660. ToMemory(l.op,addressType,0);
  7661. Emit(Push(position,l.op));
  7662. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7663. (*
  7664. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7665. IF constructor # NIL THEN
  7666. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7667. FOR i := 1 TO x.parameters.Length()-1 DO
  7668. p := x.parameters.GetExpression(i);
  7669. Global.GetSymbolName(parameter,name);
  7670. Evaluate(p, value);
  7671. ASSERT(value.type # NIL);
  7672. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7673. par := instance.AddParameter(name);
  7674. par.SetInteger(value.integer);
  7675. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7676. par := instance.AddParameter(name);
  7677. par.SetBoolean(value.boolean);
  7678. ELSE Error(x.position,NotYetImplemented)
  7679. END;
  7680. parameter := parameter.nextParameter
  7681. END;
  7682. END;
  7683. *)
  7684. (* call initializer *)
  7685. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7686. IF constructor # NIL THEN
  7687. (*! should be unified with ProcedureCallDesignator *)
  7688. IF backend.cellsAreObjects THEN
  7689. Symbol(temporaryVariable,l);
  7690. ToMemory(l.op,addressType,0);
  7691. Emit(Push(position,l.op));
  7692. ReleaseOperand(l);
  7693. END;
  7694. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7695. FOR i := firstPar TO x.parameters.Length()-1 DO
  7696. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7697. formalParameter := formalParameter.nextParameter;
  7698. END;
  7699. (* static call of the constructor *)
  7700. Global.GetSymbolSegmentedName(constructor,name);
  7701. ASSERT(~constructor.isInline);
  7702. IF constructor.scope.ownerModule # module.module THEN
  7703. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7704. ELSE
  7705. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7706. END;
  7707. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7708. (*ELSE
  7709. ReleaseIntermediateOperand(pointer);*)
  7710. END;
  7711. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7712. ToMemory(l.op, addressType, 0);
  7713. Designate(p0,s0);
  7714. ToMemory(s0.op,addressType,0);
  7715. Emit(Mov(position,s0.op,l.op));
  7716. ReleaseOperand(l);
  7717. ReleaseOperand(s0);
  7718. result.tag := emptyOperand;
  7719. (* start *)
  7720. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7721. (* push cell *)
  7722. Symbol(temporaryVariable, l);
  7723. ToMemory(l.op,addressType,0);
  7724. Emit(Push(Basic.invalidPosition,l.op));
  7725. (* push delegate *)
  7726. Emit(Push(Basic.invalidPosition,l.op));
  7727. ReleaseOperand(l);
  7728. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7729. Emit(Push(position, s1.op));
  7730. ReleaseOperand(s1);
  7731. CallThis(position,"ActiveCellsRuntime","Start",3);
  7732. END;
  7733. (*IF temporaryVariable # NIL THEN
  7734. end := NewLabel();
  7735. BrL(end);
  7736. SetLabel(exit);
  7737. Designate(p0,l);
  7738. ToMemory(l.op,addressType,0);
  7739. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7740. ReleaseOperand(l);
  7741. SetLabel(end);
  7742. ELSE
  7743. SetLabel(exit);
  7744. END;
  7745. *)
  7746. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7747. ELSE (* no pointer to record, no pointer to array *)
  7748. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7749. (* ignore new statement *)
  7750. Warning(p0.position, "cannot run on final hardware");
  7751. ELSE
  7752. HALT(200);
  7753. END;
  7754. END;
  7755. (* ---- ADDRESSOF----- *)
  7756. |Global.systemAdr:
  7757. Designate(p0,s0);
  7758. s0.mode := ModeValue;
  7759. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7760. ReleaseIntermediateOperand(s0.op);
  7761. s0.op := s0.tag;
  7762. IntermediateCode.InitOperand(s0.tag);
  7763. END;
  7764. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7765. result := s0;
  7766. (* ---- BIT ----- *)
  7767. |Global.systemBit:
  7768. Evaluate(p0,s0);
  7769. ToMemory(s0.op,addressType,0);
  7770. ReuseCopy(res,s0.op);
  7771. ReleaseOperand(s0);
  7772. Evaluate(p1,s1);
  7773. Emit(Ror(position,res,res,s1.op));
  7774. ReleaseOperand(s1);
  7775. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7776. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7777. IF ~conditional THEN
  7778. InitOperand(result,ModeValue); result.op := res;
  7779. ELSE
  7780. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7781. BrL(falseLabel);
  7782. ReleaseIntermediateOperand(res);
  7783. END;
  7784. (* --- MSK ----*)
  7785. |Global.systemMsk:
  7786. Evaluate(p0,s0);
  7787. Evaluate(p1,s1);
  7788. ReuseCopy(res,s0.op);
  7789. ReleaseOperand(s0);
  7790. Emit(And(position,res,res,s1.op));
  7791. ReleaseOperand(s1);
  7792. InitOperand(result,ModeValue);
  7793. result.op := res;
  7794. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7795. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7796. Evaluate(p0,s0);
  7797. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7798. ReleaseOperand(s0);
  7799. InitOperand(result,ModeValue);
  7800. result.op := res;
  7801. (* ---- SYSTEM.GetStackPointer ----- *)
  7802. |Global.systemGetStackPointer:
  7803. InitOperand(result,ModeValue);
  7804. result.op := sp;
  7805. (* ---- SYSTEM.GetFramePointer ----- *)
  7806. |Global.systemGetFramePointer:
  7807. InitOperand(result,ModeValue);
  7808. result.op := fp;
  7809. (* ---- SYSTEM.GetActivity ----- *)
  7810. |Global.systemGetActivity:
  7811. ASSERT(backend.cooperative);
  7812. InitOperand(result,ModeValue);
  7813. result.op := ap;
  7814. (* ---- SYSTEM.SetStackPointer ----- *)
  7815. |Global.systemSetStackPointer:
  7816. Evaluate(p0,s0); (* *)
  7817. Emit(Mov(position,sp,s0.op));
  7818. ReleaseOperand(s0);
  7819. (* ---- SYSTEM.SetFramePointer ----- *)
  7820. |Global.systemSetFramePointer:
  7821. Evaluate(p0,s0); (* *)
  7822. Emit(Mov(position,fp,s0.op));
  7823. ReleaseOperand(s0);
  7824. (* ---- SYSTEM.Activity ----- *)
  7825. |Global.systemSetActivity:
  7826. ASSERT(backend.cooperative);
  7827. Evaluate(p0,s0); (* *)
  7828. Emit(Mov(position,ap,s0.op));
  7829. ReleaseOperand(s0);
  7830. (* ---- SYSTEM.VAL ----- *)
  7831. |Global.systemVal:
  7832. Expression(p1);
  7833. s1 := result;
  7834. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7835. IF s1.mode = ModeReference THEN
  7836. (* nothing to be done if not record type, just take over new type *)
  7837. IF (type IS SyntaxTree.RecordType) THEN
  7838. ReleaseIntermediateOperand(s1.tag);
  7839. s1.tag := TypeDescriptorAdr(type);
  7840. IF ~newObjectFile THEN
  7841. IntermediateCode.MakeMemory(s1.tag,addressType);
  7842. END;
  7843. UseIntermediateOperand(s1.tag);
  7844. END;
  7845. result := s1;
  7846. ELSE (* copy over result to different type, may not use convert *)
  7847. itype := IntermediateCode.GetType(system,type);
  7848. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7849. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7850. Emit(Mov(position,s0.op,s1.op));
  7851. ReleaseOperand(s1);
  7852. InitOperand(result,ModeValue);
  7853. result.op := s0.op;
  7854. ELSE (* different size, must convert *)
  7855. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7856. Convert(s1.op, IntermediateCode.GetType(system,type));
  7857. result := s1;
  7858. END;
  7859. END;
  7860. (* ---- SYSTEM.GET ----- *)
  7861. |Global.systemGet:
  7862. Evaluate(p0,s0); (* adr *)
  7863. Designate(p1,s1); (* variable *)
  7864. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7865. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7866. Emit(Mov(position,s1.op,s0.op));
  7867. ReleaseOperand(s1);
  7868. ReleaseOperand(s0);
  7869. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7870. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7871. Evaluate(p0,s0); (* *)
  7872. Evaluate(p1,s1); (* variable *)
  7873. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7874. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7875. (* real part *)
  7876. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7877. Emit(Mov(position,res, s1.op));
  7878. ReleaseIntermediateOperand(res);
  7879. (* imaginary part *)
  7880. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7881. Emit(Mov(position,res, s1.tag));
  7882. ReleaseIntermediateOperand(res);
  7883. ReleaseOperand(s1);
  7884. ReleaseOperand(s0);
  7885. ELSE
  7886. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7887. ReleaseOperand(s0);
  7888. Emit(Mov(position,res,s1.op));
  7889. ReleaseIntermediateOperand(res);
  7890. ReleaseOperand(s1);
  7891. END;
  7892. (* ---- SYSTEM.MOVE ----- *)
  7893. |Global.systemMove:
  7894. Evaluate(p0,s0);
  7895. Evaluate(p1,s1);
  7896. Evaluate(p2,s2);
  7897. Emit(Copy(position,s1.op,s0.op,s2.op));
  7898. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7899. (* ---- SYSTEM.NEW ----- *)
  7900. |Global.systemNew:
  7901. Designate(p0,s0);
  7902. Emit(Push(position,s0.op));
  7903. ReleaseOperand(s0);
  7904. Evaluate(p1,s1);
  7905. Emit(Push(position,s1.op));
  7906. ReleaseOperand(s1);
  7907. (* push realtime flag: false by default *)
  7908. Emit(Push(position,false));
  7909. CallThis(position,"Heaps","NewSys",3);
  7910. (* ---- SYSTEM.CALL ----- *)
  7911. |Global.systemRef:
  7912. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7913. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7914. s0.mode := ModeValue;
  7915. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7916. result := s0
  7917. (* ---- INCR ----- *)
  7918. |Global.Incr:
  7919. Designate(p0,operand);
  7920. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7921. Dereference(operand,p0.type.resolved,FALSE);
  7922. END;
  7923. ASSERT(p1 # NIL);
  7924. Evaluate(p1,l);
  7925. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7926. ReleaseOperand(operand); ReleaseOperand(l);
  7927. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7928. (* ---- SUM ----- *)
  7929. |Global.Sum: HALT(200);
  7930. (* ---- ALL ----- *)
  7931. |Global.All: HALT(200);
  7932. (* ---- CAS ----- *)
  7933. |Global.Cas:
  7934. needsTrace := p0.NeedsTrace();
  7935. IF needsTrace THEN ModifyAssignments(true) END;
  7936. Designate(p0,s0);
  7937. Evaluate(p1,s1);
  7938. Evaluate(p2,s2);
  7939. IF needsTrace THEN
  7940. Emit(Push(position, s0.op));
  7941. Emit(Push(position, s1.op));
  7942. Emit(Push(position, s2.op));
  7943. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7944. ELSE
  7945. Emit(Cas(position,s0.op,s1.op,s2.op));
  7946. END;
  7947. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7948. IF needsTrace THEN ModifyAssignments(false) END;
  7949. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7950. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7951. IF conditional THEN
  7952. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7953. BrL(falseLabel);
  7954. ReleaseIntermediateOperand(res);
  7955. END;
  7956. (* ---- DIM ----- *)
  7957. |Global.Dim:
  7958. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7959. Designate(p0,s0);
  7960. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7961. Dereference(s0,p0.type.resolved,FALSE);
  7962. END;
  7963. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7964. ReleaseOperand(s0);
  7965. (* ---- RESHAPE ----- *)
  7966. |Global.Reshape:
  7967. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7968. left := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,procedure);
  7969. left.SetType(procedure.type);
  7970. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7971. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7972. END;
  7973. (* ---- SYSTEM.TYPECODE ----- *)
  7974. |Global.systemTypeCode:
  7975. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7976. IF type.resolved IS SyntaxTree.PointerType THEN
  7977. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7978. END;
  7979. result.op := TypeDescriptorAdr(type);
  7980. IF ~newObjectFile THEN
  7981. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7982. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7983. END;
  7984. result.mode := ModeValue;
  7985. (* ---- SYSTEM.TRACE ----- *)
  7986. |Global.systemTrace:
  7987. SystemTrace(x.parameters, x.position);
  7988. (* ----- CONNECT ------*)
  7989. |Global.Connect:
  7990. IF backend.cellsAreObjects THEN
  7991. PushPort(p0);
  7992. PushPort(p1);
  7993. IF p2 # NIL THEN
  7994. Evaluate(p2, s2);
  7995. Emit(Push(p2.position, s2.op));
  7996. ReleaseOperand(s2);
  7997. ELSE
  7998. Emit(Push(Basic.invalidPosition, IntermediateCode.Immediate(int32, -1)));
  7999. END;
  8000. CallThis(position,"ActiveCellsRuntime","Connect",3);
  8001. ELSE
  8002. Warning(x.position, "cannot run on final hardware");
  8003. END;
  8004. (* ----- DELEGATE ------*)
  8005. |Global.Delegate:
  8006. IF backend.cellsAreObjects THEN
  8007. PushPort(p0);
  8008. PushPort(p1);
  8009. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  8010. ELSE
  8011. Warning(x.position, "cannot run on final hardware");
  8012. END;
  8013. (* ----- SEND ------*)
  8014. |Global.Send:
  8015. Evaluate(p0,s0);
  8016. Evaluate(p1,s1);
  8017. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8018. Emit(Push(position,s0.op));
  8019. Emit(Push(position,s1.op));
  8020. (*
  8021. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8022. *)
  8023. IF ~backend.cellsAreObjects THEN
  8024. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  8025. END;
  8026. ReleaseOperand(s0);
  8027. ReleaseOperand(s1);
  8028. IF backend.cellsAreObjects THEN
  8029. CallThis(position,"ActiveCellsRuntime","Send",2);
  8030. ELSE
  8031. CallThis(position,ChannelModuleName,"Send",2);
  8032. END;
  8033. (* ----- RECEIVE ------*)
  8034. |Global.Receive:
  8035. Evaluate(p0,s0);
  8036. Emit(Push(position,s0.op));
  8037. Designate(p1,s1);
  8038. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8039. Emit(Push(position,s1.op));
  8040. IF p2 # NIL THEN
  8041. Designate(p2,s2);
  8042. Emit(Push(position,s2.op));
  8043. END;
  8044. (*
  8045. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8046. *)
  8047. IF ~backend.cellsAreObjects THEN
  8048. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  8049. END;
  8050. ReleaseOperand(s0);
  8051. ReleaseOperand(s1);
  8052. ReleaseOperand(s2);
  8053. IF backend.cellsAreObjects THEN
  8054. IF p2 = NIL THEN
  8055. CallThis(position,"ActiveCellsRuntime","Receive",2)
  8056. ELSE
  8057. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  8058. END;
  8059. ELSE
  8060. IF p2 = NIL THEN
  8061. CallThis(position,ChannelModuleName,"Receive",2)
  8062. ELSE
  8063. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  8064. END;
  8065. END;
  8066. | Global.systemSpecial:
  8067. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  8068. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  8069. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  8070. (* determine if parameters are of the VAR kind *)
  8071. ASSERT(x.parameters.Length() <= 3);
  8072. formalParameter := procedureType.firstParameter;
  8073. FOR i := 0 TO x.parameters.Length() - 1 DO
  8074. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  8075. formalParameter := formalParameter.nextParameter
  8076. END;
  8077. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  8078. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  8079. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  8080. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  8081. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  8082. IF procedureType.returnType # NIL THEN
  8083. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  8084. Emit(Result(position, res));
  8085. (*InitOperand(result,ModeValue);
  8086. result.op := res;
  8087. *)
  8088. IF ~conditional THEN
  8089. InitOperand(result,ModeValue); result.op := res;
  8090. ELSE
  8091. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  8092. BrL(falseLabel);
  8093. ReleaseIntermediateOperand(res);
  8094. END;
  8095. END
  8096. ELSE (* function not yet implemented *)
  8097. Error(position,"not yet implemented");
  8098. END;
  8099. destination := dest;
  8100. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  8101. END VisitBuiltinCallDesignator;
  8102. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  8103. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  8104. BEGIN
  8105. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  8106. dest := destination; destination := emptyOperand;
  8107. Expression(x.left);
  8108. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  8109. ELSIF isUnchecked THEN (* no check *)
  8110. ELSE
  8111. trueL := NewLabel();
  8112. falseL := NewLabel();
  8113. IF IsPointerToRecord(x.left.type,recordType) THEN
  8114. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  8115. Emit(Mov(position,tag, result.op));
  8116. IF result.mode # ModeValue THEN
  8117. ptr := tag;
  8118. IntermediateCode.MakeMemory(ptr,addressType);
  8119. Emit(Mov(position,tag, ptr));
  8120. END;
  8121. IF ~backend.cooperative THEN
  8122. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8123. END;
  8124. IntermediateCode.MakeMemory(tag,addressType);
  8125. ELSE
  8126. tag := result.tag;
  8127. UseIntermediateOperand(tag);
  8128. END;
  8129. TypeTest(tag,x.type,trueL,falseL);
  8130. ReleaseIntermediateOperand(tag);
  8131. SetLabel(falseL);
  8132. EmitTrap(position,TypeCheckTrap);
  8133. SetLabel(trueL);
  8134. END;
  8135. destination := dest;
  8136. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  8137. END VisitTypeGuardDesignator;
  8138. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  8139. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  8140. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  8141. VAR label: Label; pc: LONGINT;
  8142. BEGIN
  8143. IF backend.cooperative & ~isUnchecked THEN
  8144. pc := section.pc;
  8145. label := NewLabel();
  8146. BrneL(label, operand.op, nil);
  8147. EmitTrap(position, NilPointerTrap);
  8148. SetLabel(label);
  8149. INC(statCoopNilCheck, section.pc - pc);
  8150. END;
  8151. END NilCheck;
  8152. BEGIN
  8153. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  8154. ReuseCopy(dereferenced,operand.op);
  8155. ReleaseOperand(operand);
  8156. operand.mode := ModeReference;
  8157. operand.op := dereferenced;
  8158. operand.tag := dereferenced;
  8159. UseIntermediateOperand(operand.tag);
  8160. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  8161. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  8162. ReleaseIntermediateOperand(operand.tag);
  8163. operand.tag := TypeDescriptorAdr(type);
  8164. IF ~newObjectFile THEN
  8165. IntermediateCode.MakeMemory(operand.tag,addressType);
  8166. END;
  8167. ELSE
  8168. IF ~backend.cooperative THEN
  8169. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  8170. END;
  8171. IntermediateCode.MakeMemory(operand.tag,addressType);
  8172. END;
  8173. NilCheck(operand.op);
  8174. ELSIF type IS SyntaxTree.ArrayType THEN
  8175. IF isUnsafe THEN
  8176. NilCheck(operand.op);
  8177. ReleaseIntermediateOperand(operand.tag);
  8178. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  8179. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8180. ELSE
  8181. operand.tag := emptyOperand;
  8182. END;
  8183. ELSE
  8184. NilCheck(operand.op);
  8185. IF backend.cooperative THEN
  8186. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  8187. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8188. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  8189. ELSE
  8190. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  8191. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  8192. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  8193. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  8194. END;
  8195. END;
  8196. ELSIF type IS SyntaxTree.MathArrayType THEN
  8197. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8198. IntermediateCode.MakeMemory(operand.op,addressType);
  8199. ELSE HALT(100);
  8200. END;
  8201. END Dereference;
  8202. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  8203. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  8204. BEGIN
  8205. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  8206. dest := destination; destination := emptyOperand;
  8207. Evaluate(x.left,d);
  8208. type := x.type.resolved;
  8209. prevIsUnchecked := isUnchecked;
  8210. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  8211. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  8212. END;
  8213. Dereference(d,type,IsUnsafePointer(x.left.type));
  8214. isUnchecked := prevIsUnchecked;
  8215. result := d;
  8216. IF backend.cooperative & (x.left.type.resolved IS SyntaxTree.PointerType) & ~x.left.type.resolved(SyntaxTree.PointerType).isPlain & ~x.left.type.resolved(SyntaxTree.PointerType).isUnsafe THEN
  8217. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  8218. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8219. END;
  8220. END;
  8221. destination := dest;
  8222. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  8223. END VisitDereferenceDesignator;
  8224. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  8225. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  8226. BEGIN
  8227. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  8228. dest := destination; destination := emptyOperand;
  8229. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  8230. tag := result.op;
  8231. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  8232. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  8233. StaticCallOperand(result,procedure.super);
  8234. ReleaseIntermediateOperand(result.tag);
  8235. UseIntermediateOperand(tag); (* necessary ? *)
  8236. result.tag := tag;
  8237. destination := dest;
  8238. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  8239. END VisitSupercallDesignator;
  8240. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  8241. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  8242. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  8243. name: Basic.SegmentedName;
  8244. procedure: SyntaxTree.Procedure;
  8245. procedureType: SyntaxTree.ProcedureType;
  8246. BEGIN
  8247. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  8248. dest := destination; destination := emptyOperand;
  8249. scope := currentScope;
  8250. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  8251. scope := scope.outerScope;
  8252. END;
  8253. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  8254. IF newObjectFile THEN
  8255. moduleSection := meta.ModuleSection();
  8256. IF backend.cooperative THEN
  8257. moduleOffset := 0;
  8258. ELSE
  8259. moduleOffset := moduleSection.pc;
  8260. END;
  8261. result.mode := ModeValue;
  8262. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  8263. ELSE
  8264. Symbol(moduleSelf,result);
  8265. IntermediateCode.MakeMemory(result.op,addressType);
  8266. END
  8267. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8268. result.mode := ModeValue;
  8269. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  8270. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  8271. ELSE
  8272. GetBaseRegister(basereg,currentScope,scope);
  8273. InitOperand(result,ModeReference);
  8274. result.op := basereg;
  8275. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  8276. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8277. parametersSize := ProcedureParametersSize(system,procedure);
  8278. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  8279. IF backend.cooperative THEN
  8280. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  8281. END;
  8282. (* tag must be loaded when dereferencing SELF pointer *)
  8283. END;
  8284. destination := dest;
  8285. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  8286. END VisitSelfDesignator;
  8287. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  8288. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; map: SymbolMap;
  8289. BEGIN
  8290. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  8291. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  8292. procedureType := procedure.type(SyntaxTree.ProcedureType);
  8293. parameter := procedureType.returnParameter;
  8294. IF currentIsInline THEN
  8295. map := currentMapper.Get(NIL);
  8296. IF map # NIL THEN
  8297. Designate(map.to, result);
  8298. ELSE
  8299. HALT(200);
  8300. END;
  8301. RETURN;
  8302. END;
  8303. VisitParameter(parameter);
  8304. IF Trace THEN TraceExit("VisitResultDesignator") END;
  8305. END VisitResultDesignator;
  8306. (** values *)
  8307. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  8308. BEGIN
  8309. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  8310. IF conditional THEN
  8311. IF x.value THEN BrL(trueLabel)
  8312. ELSE BrL(falseLabel)
  8313. END;
  8314. ELSE
  8315. InitOperand(result,ModeValue);
  8316. IF x.value THEN result.op := true ELSE result.op := false END;
  8317. END;
  8318. END VisitBooleanValue;
  8319. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  8320. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  8321. BEGIN
  8322. Global.GetModuleSegmentedName(module.module, name);
  8323. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  8324. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL, dump # NIL);
  8325. RETURN section
  8326. END GetDataSection;
  8327. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  8328. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  8329. BEGIN
  8330. type := vop.type;
  8331. data := GetDataSection();
  8332. pc := EnterImmediate(data,vop);
  8333. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  8334. IntermediateCode.MakeMemory(vop, type);
  8335. END GetImmediateMem;
  8336. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  8337. BEGIN
  8338. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  8339. InitOperand(result,ModeValue);
  8340. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  8341. IF ~supportedImmediate(result.op) &~inData THEN
  8342. GetImmediateMem(result.op)
  8343. END;
  8344. END VisitIntegerValue;
  8345. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  8346. BEGIN
  8347. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  8348. InitOperand(result,ModeValue);
  8349. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  8350. END VisitCharacterValue;
  8351. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  8352. BEGIN
  8353. IF Trace THEN TraceEnter("VisitSetValue") END;
  8354. InitOperand(result,ModeValue);
  8355. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  8356. END VisitSetValue;
  8357. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  8358. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  8359. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  8360. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  8361. BEGIN
  8362. numberElements := x.elements.Length();
  8363. FOR i := 0 TO numberElements-1 DO
  8364. expression := x.elements.GetExpression(i);
  8365. IF expression IS SyntaxTree.MathArrayExpression THEN
  8366. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  8367. ELSE
  8368. inData := TRUE;
  8369. Evaluate(expression,op);
  8370. irv.Emit(Data(position,op.op));
  8371. inData := FALSE;
  8372. ReleaseOperand(op);
  8373. END;
  8374. END;
  8375. END RecursiveData;
  8376. BEGIN
  8377. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8378. IF ~TryConstantDeclaration() THEN
  8379. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8380. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8381. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8382. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8383. ELSE
  8384. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8385. END;
  8386. RecursiveData(x.array);
  8387. InitOperand(result,ModeReference);
  8388. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8389. END
  8390. END VisitMathArrayValue;
  8391. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8392. VAR constant: Sections.Section;
  8393. BEGIN
  8394. IF constantDeclaration = NIL THEN
  8395. RETURN FALSE
  8396. ELSE
  8397. (* Is a constant in this module: did we generate it already? *)
  8398. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8399. IF constant # NIL THEN
  8400. InitOperand(result,ModeReference);
  8401. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8402. RETURN TRUE;
  8403. END;
  8404. END;
  8405. RETURN FALSE
  8406. END TryConstantDeclaration;
  8407. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8408. BEGIN
  8409. constantDeclaration := x;
  8410. x.value.resolved.Accept(SELF);
  8411. END VisitConstant;
  8412. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8413. BEGIN
  8414. IF Trace THEN TraceEnter("VisitRealValue") END;
  8415. InitOperand(result,ModeValue);
  8416. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8417. END VisitRealValue;
  8418. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8419. VAR
  8420. componentType: SyntaxTree.Type;
  8421. BEGIN
  8422. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8423. ASSERT(x.type IS SyntaxTree.ComplexType);
  8424. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8425. InitOperand(result,ModeValue);
  8426. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8427. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8428. END VisitComplexValue;
  8429. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8430. VAR i: LONGINT; name: Basic.SegmentedName;
  8431. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8432. BEGIN
  8433. IF Trace THEN TraceEnter("VisitStringValue") END;
  8434. IF ~TryConstantDeclaration() THEN
  8435. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8436. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8437. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8438. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8439. ELSE
  8440. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8441. END;
  8442. FOR i := 0 TO x.length-1 DO
  8443. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8444. irv.Emit(Data(position,op));
  8445. END;
  8446. InitOperand(result,ModeReference);
  8447. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8448. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8449. END
  8450. END VisitStringValue;
  8451. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8452. BEGIN
  8453. IF Trace THEN TraceEnter("VisitNilValue") END;
  8454. InitOperand(result,ModeValue);
  8455. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8456. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8457. END VisitNilValue;
  8458. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8459. BEGIN
  8460. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8461. InitOperand(result,ModeValue);
  8462. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8463. END VisitEnumerationValue;
  8464. (** symbols *)
  8465. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8466. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8467. END VisitImport;
  8468. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8469. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8470. BEGIN
  8471. IF scope # baseScope THEN
  8472. (* left := [fp+8] *)
  8473. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8474. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8475. ReuseCopy(left,right);
  8476. ReleaseIntermediateOperand(right);
  8477. scope := scope.outerScope; DEC(level);
  8478. (* { left := [left+8] } *)
  8479. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8480. IF backend.cooperative OR backend.preciseGC THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8481. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8482. Emit(Mov(position,left,right));
  8483. scope := scope.outerScope; DEC(level);
  8484. END;
  8485. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8486. result := left;
  8487. ELSE
  8488. result := fp;
  8489. END;
  8490. END GetBaseRegister;
  8491. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8492. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8493. BEGIN
  8494. IF Trace THEN TraceEnter("VisitVariable"); END;
  8495. type := x.type.resolved;
  8496. IF (x.useRegister) THEN
  8497. InitOperand(result, ModeValue);
  8498. IF x.registerNumber < 0 THEN
  8499. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8500. reg := x.registerNumber;
  8501. ELSE
  8502. reg := registerUsageCount.Map(x.registerNumber);
  8503. UseRegister(reg);
  8504. END;
  8505. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8506. ELSIF x.externalName # NIL THEN
  8507. InitOperand(result,ModeReference);
  8508. Basic.ToSegmentedName(x.externalName^, name);
  8509. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8510. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8511. InitOperand(result,ModeReference);
  8512. GetBaseRegister(result.op,currentScope,x.scope);
  8513. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8514. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8515. InitOperand(result,ModeReference);
  8516. GetCodeSectionNameForSymbol(x,name);
  8517. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8518. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8519. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8520. InitOperand(result,ModeReference);
  8521. GetCodeSectionNameForSymbol(x,name);
  8522. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8523. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8524. ELSE (* field, left designator must have been emitted *)
  8525. ASSERT(result.mode = ModeReference);
  8526. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8527. ReuseCopy(temp,result.op);
  8528. ReleaseIntermediateOperand(result.op);
  8529. result.op := temp;
  8530. END;
  8531. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8532. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8533. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8534. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8535. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8536. END;
  8537. END;
  8538. END;
  8539. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8540. ValueToCondition(result);
  8541. ELSIF type IS SyntaxTree.ProcedureType THEN
  8542. ReleaseIntermediateOperand(result.tag);
  8543. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8544. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8545. UseIntermediateOperand(result.tag);
  8546. ELSE
  8547. result.tag := nil; (* nil *)
  8548. END;
  8549. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8550. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8551. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8552. ReleaseIntermediateOperand(result.tag);
  8553. Global.GetSymbolSegmentedName(x,name);
  8554. Basic.SuffixSegmentedName (name, Basic.MakeString ("@len"));
  8555. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8556. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8557. ELSE
  8558. END;
  8559. ELSE
  8560. ReleaseIntermediateOperand(result.tag);
  8561. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8562. END;
  8563. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8564. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8565. ReleaseIntermediateOperand(result.tag);
  8566. result.tag := result.op;
  8567. UseIntermediateOperand(result.tag);
  8568. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8569. IntermediateCode.MakeMemory(result.op,addressType);
  8570. END;
  8571. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8572. ReleaseIntermediateOperand(result.tag);
  8573. result.tag := TypeDescriptorAdr(type);
  8574. IF ~newObjectFile THEN
  8575. IntermediateCode.MakeMemory(result.tag,addressType);
  8576. END;
  8577. UseIntermediateOperand(result.tag);
  8578. (* tag for pointer type computed not here but during dereferencing *)
  8579. END;
  8580. IF Trace THEN TraceExit("VisitVariable") END;
  8581. END VisitVariable;
  8582. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8583. BEGIN
  8584. VisitVariable(property);
  8585. END VisitProperty;
  8586. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8587. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8588. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT; temp: IntermediateCode.Operand;
  8589. BEGIN
  8590. type := x.type.resolved;
  8591. IF Trace THEN TraceEnter("VisitParameter") END;
  8592. IF x.ownerType IS SyntaxTree.CellType THEN
  8593. ptype := x.type.resolved;
  8594. IF backend.cellsAreObjects THEN
  8595. ASSERT(result.mode = ModeReference);
  8596. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8597. ReuseCopy(temp,result.op);
  8598. ReleaseIntermediateOperand(result.op);
  8599. result.op := temp;
  8600. END;
  8601. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8602. RETURN;
  8603. ELSE
  8604. InitOperand(result,ModeReference);
  8605. GetCodeSectionNameForSymbol(x,name);
  8606. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8607. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8608. RETURN;
  8609. END;
  8610. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8611. InitOperand(result,ModeReference);
  8612. GetCodeSectionNameForSymbol(x,name);
  8613. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8614. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8615. RETURN
  8616. ELSE
  8617. GetBaseRegister(basereg,currentScope,x.scope);
  8618. InitOperand(result,ModeReference);
  8619. result.op := basereg;
  8620. END;
  8621. IF IsOpenArray(type) THEN
  8622. result.tag := basereg;
  8623. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8624. IntermediateCode.MakeMemory(result.op,addressType);
  8625. IF Global.IsOberonProcedure(x.ownerType) THEN
  8626. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8627. UseIntermediateOperand(result.tag);
  8628. ELSE
  8629. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8630. END;
  8631. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8632. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8633. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8634. ELSIF IsStaticArray(type) THEN
  8635. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8636. IntermediateCode.MakeMemory(result.op,addressType);
  8637. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8638. ELSIF type IS SyntaxTree.MathArrayType THEN
  8639. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8640. WITH type: SyntaxTree.MathArrayType DO
  8641. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8642. IF type.form = SyntaxTree.Tensor THEN
  8643. ELSIF type.form = SyntaxTree.Open THEN
  8644. result.tag := result.op;
  8645. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8646. IntermediateCode.MakeMemory(result.op,addressType);
  8647. UseIntermediateOperand(result.tag);
  8648. ELSIF type.form = SyntaxTree.Static THEN
  8649. IF x.kind = SyntaxTree.ConstParameter THEN
  8650. IntermediateCode.MakeMemory(result.op,addressType);
  8651. END;
  8652. ELSE HALT(100)
  8653. END;
  8654. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8655. IF type.form = SyntaxTree.Tensor THEN
  8656. ToMemory(result.op,addressType,0);
  8657. ELSIF type.form = SyntaxTree.Open THEN
  8658. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8659. ReuseCopy(result.tag, mem);
  8660. ReleaseIntermediateOperand(mem);
  8661. ReleaseIntermediateOperand(result.op);
  8662. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8663. ELSIF type.form = SyntaxTree.Static THEN
  8664. IntermediateCode.MakeMemory(result.op,addressType);
  8665. ELSE HALT(100)
  8666. END;
  8667. ELSE HALT(100)
  8668. END;
  8669. END;
  8670. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8671. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8672. IntermediateCode.MakeMemory(result.op,addressType);
  8673. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8674. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8675. END;
  8676. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8677. ValueToCondition(result);
  8678. ELSIF type IS SyntaxTree.ProcedureType THEN
  8679. ReleaseIntermediateOperand(result.tag);
  8680. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8681. IF x.kind = SyntaxTree.VarParameter THEN
  8682. ReuseCopy(result.tag,result.op);
  8683. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8684. IntermediateCode.MakeMemory(result.tag,addressType);
  8685. ELSE
  8686. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8687. UseIntermediateOperand(result.tag);
  8688. END;
  8689. ELSE
  8690. result.tag := nil;
  8691. END;
  8692. (* tag for pointer type computed not here but during dereferencing *)
  8693. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) & ~(x.selfParameter) THEN
  8694. ReleaseIntermediateOperand(result.tag);
  8695. result.tag := basereg;
  8696. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8697. IntermediateCode.MakeMemory(result.tag,addressType);
  8698. UseIntermediateOperand(result.tag);
  8699. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & ((x.kind = SyntaxTree.ValueParameter) OR x.selfParameter) THEN
  8700. ReleaseIntermediateOperand(result.tag);
  8701. result.tag := TypeDescriptorAdr(type);
  8702. IF ~newObjectFile THEN
  8703. IntermediateCode.MakeMemory(result.tag,addressType);
  8704. END;
  8705. UseIntermediateOperand(result.tag);
  8706. END;
  8707. IF Trace THEN TraceExit("VisitParameter") END;
  8708. END VisitParameter;
  8709. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8710. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8711. BEGIN
  8712. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8713. (* left.p: left already emitted *)
  8714. tag := result.op; (* value of pointer to left *)
  8715. (* get type desc *)
  8716. tmp := result.tag;
  8717. IntermediateCode.MakeMemory(tmp,addressType);
  8718. (* get method adr *)
  8719. Reuse1(reg,tmp);
  8720. ReleaseIntermediateOperand(tmp);
  8721. IF backend.cooperative THEN
  8722. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8723. WHILE recordType.baseType # NIL DO
  8724. recordType := recordType.GetBaseRecord ();
  8725. END;
  8726. GetRecordTypeName (recordType,name);
  8727. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8728. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8729. offset := 0;
  8730. ELSE
  8731. offset := 2;
  8732. END;
  8733. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8734. ELSE
  8735. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8736. END;
  8737. InitOperand(operand,ModeReference);
  8738. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8739. operand.op := reg;
  8740. operand.tag := tag;
  8741. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8742. END DynamicCallOperand;
  8743. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8744. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8745. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8746. BEGIN
  8747. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8748. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8749. tag := operand.op;
  8750. ReleaseIntermediateOperand(operand.tag);
  8751. ELSE tag := nil
  8752. END;
  8753. IF x.isInline THEN
  8754. sectionType := Sections.InlineCodeSection;
  8755. ELSE
  8756. sectionType := Sections.CodeSection;
  8757. END;
  8758. IF x.externalName # NIL THEN
  8759. Basic.ToSegmentedName(x.externalName^, name);
  8760. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8761. ELSE
  8762. GetCodeSectionNameForSymbol(x, name);
  8763. IF (x.scope.ownerModule = module.module) THEN
  8764. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8765. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8766. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8767. IF source.pc = 0 THEN (* no code yet *)
  8768. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8769. END;
  8770. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8771. bits := x.procedureScope.body.code.inlineCode;
  8772. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8773. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8774. binary.CopyBits(bits, 0, bits.GetSize());
  8775. source.SetResolved(binary);
  8776. ELSE
  8777. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8778. END;
  8779. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8780. END;
  8781. InitOperand(operand,ModeValue);
  8782. operand.op := reg;
  8783. operand.tag := tag;
  8784. IF Trace THEN TraceExit("StaticCallOperand") END;
  8785. END StaticCallOperand;
  8786. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8787. (* handle expressions of the form designator.procedure or procedure *)
  8788. BEGIN
  8789. IF Trace THEN TraceEnter("VisitProcedure") END;
  8790. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) & ~(result.tag.mode = IntermediateCode.ModeImmediate) THEN
  8791. DynamicCallOperand(result,x);
  8792. ELSIF x.isInline THEN
  8793. StaticCallOperand(result,x);
  8794. ELSE
  8795. StaticCallOperand(result,x);
  8796. END;
  8797. IF Trace THEN TraceExit("VisitProcedure") END;
  8798. END VisitProcedure;
  8799. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8800. BEGIN
  8801. VisitProcedure(x);
  8802. END VisitOperator;
  8803. (** statements *)
  8804. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8805. BEGIN
  8806. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8807. Expression(x.call);
  8808. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8809. ReleaseOperand(result)
  8810. END;
  8811. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8812. END VisitProcedureCallStatement;
  8813. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8814. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8815. leftBase, rightBase: SyntaxTree.Type;
  8816. procedureName,s: SyntaxTree.IdentifierString;
  8817. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8818. size: LONGINT; rightKind: LONGINT;
  8819. dummy: IntermediateCode.Operand;
  8820. CONST moduleName = "FoxArrayBase";
  8821. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8822. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8823. BEGIN
  8824. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8825. IF base IS SyntaxTree.MathArrayType THEN
  8826. base := OpenArray(base(SyntaxTree.MathArrayType));
  8827. END;
  8828. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8829. result.SetArrayBase(base);
  8830. RETURN result
  8831. END OpenArray;
  8832. BEGIN
  8833. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8834. SaveRegisters();ReleaseUsedRegisters(saved);
  8835. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8836. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8837. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8838. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8839. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8840. IF leftType.form = SyntaxTree.Tensor THEN
  8841. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8842. ELSIF leftType.form = SyntaxTree.Open THEN
  8843. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8844. ELSIF leftType.form = SyntaxTree.Static THEN
  8845. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8846. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8847. END;
  8848. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8849. IF procedure = NIL THEN
  8850. s := "Instruction not supported on target, emulation procedure ";
  8851. Strings.Append(s,moduleName);
  8852. Strings.Append(s,".");
  8853. Strings.Append(s,procedureName);
  8854. Strings.Append(s," not present");
  8855. Error(position,s);
  8856. ELSE
  8857. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8858. parameter.SetType(leftType);
  8859. parameter.SetAccess(SyntaxTree.Internal);
  8860. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8861. parameter.SetKind(rightKind);
  8862. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8863. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8864. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8865. StaticCallOperand(result,procedure);
  8866. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8867. ReleaseOperand(result);
  8868. END;
  8869. RestoreRegisters(saved);
  8870. END;
  8871. END AssignMathArray;
  8872. VAR modifyAssignmentCounter := 0: LONGINT;
  8873. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8874. VAR processor,mem,dst: IntermediateCode.Operand;
  8875. BEGIN
  8876. IF value.intValue = true.intValue THEN
  8877. INC(modifyAssignmentCounter);
  8878. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8879. modifyAssignmentsPC := section.pc;
  8880. ELSE
  8881. DEC(modifyAssignmentCounter);
  8882. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8883. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8884. END;
  8885. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8886. dst := NewRegisterOperand (addressType);
  8887. Emit(Mov(position,dst, processor));
  8888. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8889. Emit(Mov(position,mem, value));
  8890. ReleaseIntermediateOperand(dst);
  8891. END ModifyAssignments;
  8892. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8893. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8894. BEGIN
  8895. type := left.type.resolved;
  8896. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8897. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8898. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8899. IF procedureType.returnParameter = parameter THEN
  8900. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8901. END;
  8902. END;
  8903. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8904. END CopySize;
  8905. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8906. VAR
  8907. leftO, rightO: Operand;
  8908. mem, sizeOp: IntermediateCode.Operand;
  8909. leftType, rightType, componentType: SyntaxTree.Type;
  8910. size: LONGINT;
  8911. parameters: SyntaxTree.ExpressionList;
  8912. procedure: SyntaxTree.Procedure;
  8913. call: SyntaxTree.ProcedureCallDesignator;
  8914. designator: SyntaxTree.Designator;
  8915. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8916. VAR procedureType: SyntaxTree.ProcedureType;
  8917. BEGIN
  8918. IF SemanticChecker.ReturnedAsParameter(right.type) THEN
  8919. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8920. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8921. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8922. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8923. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8924. IF right.id = Global.Reshape THEN RETURN TRUE
  8925. END;
  8926. END;
  8927. END;
  8928. END;
  8929. RETURN FALSE
  8930. END CanPassAsResultParameter;
  8931. BEGIN
  8932. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8933. leftType := left.type.resolved; rightType:= right.type.resolved;
  8934. IF backend.cooperative & left.NeedsTrace() THEN
  8935. ModifyAssignments(true);
  8936. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8937. Designate(right, rightO);
  8938. Designate(left, leftO);
  8939. ASSERT(leftO.mode = ModeReference);
  8940. TransferToRegister(leftO.op, leftO.op);
  8941. TransferToRegister(rightO.op, rightO.op);
  8942. Emit(Push(position, leftO.op));
  8943. Emit(Push(position, rightO.op));
  8944. CallAssignMethod(leftO.op, rightO.op, left.type);
  8945. Emit(Pop(position, rightO.op));
  8946. Emit(Pop(position, leftO.op));
  8947. sizeOp := CopySize(left);
  8948. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8949. ReleaseOperand(leftO);
  8950. ReleaseOperand(rightO);
  8951. ELSE
  8952. Evaluate(right,rightO);
  8953. Designate(left,leftO);
  8954. ASSERT(leftO.mode = ModeReference);
  8955. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8956. (* copy procedure address first *)
  8957. MakeMemory(mem,leftO.op,addressType,0);
  8958. Emit(Mov(position,mem,rightO.op));
  8959. ReleaseIntermediateOperand(mem);
  8960. (* copy pointer address *)
  8961. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8962. CallAssignPointer(leftO.tag, rightO.tag);
  8963. ELSE
  8964. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8965. CallAssignPointer(leftO.op, rightO.op);
  8966. END;
  8967. ReleaseOperand(leftO);
  8968. ReleaseOperand(rightO);
  8969. END;
  8970. ModifyAssignments(false);
  8971. RETURN;
  8972. END;
  8973. IF CanPassAsResultParameter(right) THEN
  8974. procedureResultDesignator := left(SyntaxTree.Designator);
  8975. Expression(right);
  8976. procedureResultDesignator := NIL;
  8977. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8978. (* left <-- ALIAS OF right: zerocopy *)
  8979. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8980. designator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, procedure);
  8981. designator.SetType(procedure.type);
  8982. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8983. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8984. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8985. END;
  8986. ELSIF leftType IS SyntaxTree.RangeType THEN
  8987. (* LHS is of array range type *)
  8988. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8989. Evaluate(right, rightO);
  8990. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8991. (* first *)
  8992. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8993. Emit(Mov(position,mem, rightO.op));
  8994. ReleaseIntermediateOperand(mem);
  8995. (* last *)
  8996. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8997. Emit(Mov(position,mem, rightO.tag));
  8998. ReleaseIntermediateOperand(mem);
  8999. (* step *)
  9000. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9001. Emit(Mov(position,mem, rightO.extra));
  9002. ReleaseIntermediateOperand(mem);
  9003. ReleaseOperand(rightO);
  9004. ReleaseOperand(leftO)
  9005. ELSIF leftType IS SyntaxTree.ComplexType THEN
  9006. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  9007. Evaluate(right, rightO);
  9008. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  9009. componentType := leftType(SyntaxTree.ComplexType).componentType;
  9010. (* real part *)
  9011. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  9012. Emit(Mov(position,mem, rightO.op));
  9013. ReleaseIntermediateOperand(mem);
  9014. (* imaginary part *)
  9015. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9016. Emit(Mov(position,mem, rightO.tag));
  9017. ReleaseIntermediateOperand(mem);
  9018. ReleaseOperand(rightO);
  9019. ReleaseOperand(leftO)
  9020. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  9021. OR (leftType IS SyntaxTree.PortType) THEN
  9022. (* rightO := leftO;*)
  9023. Evaluate(right,rightO);
  9024. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  9025. Designate(left,leftO);
  9026. IF leftO.mode = ModeReference THEN
  9027. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  9028. destination := mem;
  9029. ELSE
  9030. destination := leftO.op;
  9031. END;
  9032. ReleaseOperand(leftO);
  9033. IF destination.mode # IntermediateCode.Undefined THEN
  9034. Emit(Mov(position,destination,rightO.op));
  9035. END;
  9036. ReleaseOperand(rightO);
  9037. ReleaseIntermediateOperand(mem);
  9038. IntermediateCode.InitOperand(destination);
  9039. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  9040. Evaluate(right,rightO);
  9041. Designate(left,leftO);
  9042. MakeMemory(mem,leftO.op,addressType,0);
  9043. Emit(Mov(position,mem,rightO.op));
  9044. ReleaseIntermediateOperand(mem);
  9045. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  9046. (* delegate *)
  9047. (*
  9048. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  9049. *)
  9050. Emit(Mov(position,leftO.tag,rightO.tag));
  9051. END;
  9052. ReleaseOperand(leftO);
  9053. ReleaseOperand(rightO);
  9054. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  9055. Designate(right,rightO);
  9056. Designate(left,leftO);
  9057. sizeOp := CopySize(left);
  9058. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  9059. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9060. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  9061. IF (rightType IS SyntaxTree.StringType) THEN
  9062. CopyString(left,right);
  9063. 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
  9064. Designate(right,rightO);
  9065. Designate(left,leftO);
  9066. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9067. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9068. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9069. ELSE
  9070. HALT(201)
  9071. END;
  9072. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  9073. IF (leftType(SyntaxTree.MathArrayType).staticLength # 0) & (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0) THEN
  9074. Designate(right,rightO);
  9075. Designate(left,leftO);
  9076. size := ToMemoryUnits(system,system.SizeOf(rightType));
  9077. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  9078. ReleaseOperand(leftO); ReleaseOperand(rightO);
  9079. END;
  9080. AssignMathArray(left,right);
  9081. ELSE
  9082. HALT(200);
  9083. END;
  9084. END Assign;
  9085. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  9086. BEGIN
  9087. IF Trace THEN TraceEnter("VisitAssignment") END;
  9088. Assign(x.left,x.right);
  9089. IF Trace THEN TraceExit("VisitAssignment") END;
  9090. END VisitAssignment;
  9091. PROCEDURE EmitCooperativeSwitch;
  9092. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  9093. BEGIN
  9094. ASSERT (cooperativeSwitches);
  9095. pc := section.pc;
  9096. IF lastSwitchPC = section.pc THEN RETURN END;
  9097. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  9098. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  9099. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  9100. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  9101. INC(statCoopSwitch, section.pc - pc);
  9102. END EmitCooperativeSwitch;
  9103. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  9104. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  9105. BEGIN
  9106. p0 := communication.left; p1 := communication.right;
  9107. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  9108. Evaluate(p0,s0);
  9109. Evaluate(p1,s1);
  9110. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9111. Emit(Push(position,s0.op));
  9112. Emit(Push(position,s1.op));
  9113. (*
  9114. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9115. *)
  9116. IF ~backend.cellsAreObjects THEN
  9117. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  9118. END;
  9119. ReleaseOperand(s0);
  9120. ReleaseOperand(s1);
  9121. IF backend.cellsAreObjects THEN
  9122. CallThis(position,"ActiveCellsRuntime","Send",2);
  9123. ELSE
  9124. CallThis(position,ChannelModuleName,"Send",2);
  9125. END;
  9126. (* ----- RECEIVE ------*)
  9127. ELSE
  9128. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  9129. tmp := p0; p0 := p1; p1 := tmp;
  9130. END;
  9131. Evaluate(p0,s0);
  9132. Emit(Push(position,s0.op));
  9133. Designate(p1,s1);
  9134. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  9135. Emit(Push(position,s1.op));
  9136. (*
  9137. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  9138. *)
  9139. IF ~backend.cellsAreObjects THEN
  9140. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  9141. END;
  9142. ReleaseOperand(s0);
  9143. ReleaseOperand(s1);
  9144. IF backend.cellsAreObjects THEN
  9145. CallThis(position,"ActiveCellsRuntime","Receive",2);
  9146. ELSE
  9147. CallThis(position,ChannelModuleName,"Receive",2)
  9148. END;
  9149. END;
  9150. END VisitCommunicationStatement;
  9151. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  9152. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  9153. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  9154. VAR true, false: Label; condition, value: BOOLEAN;
  9155. BEGIN
  9156. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  9157. IF condition THEN
  9158. true := NewLabel();
  9159. false := NewLabel();
  9160. Condition(if.condition,true,false);
  9161. SetLabel(true);
  9162. StatementSequence(if.statements);
  9163. BrL(end);
  9164. SetLabel(false);
  9165. ELSE
  9166. IF value THEN (* always true *)
  9167. escape := TRUE;
  9168. StatementSequence(if.statements);
  9169. (* no branch necessary -- rest skipped *)
  9170. END;
  9171. END;
  9172. END IfPart;
  9173. BEGIN
  9174. IF Trace THEN TraceEnter("VisitIfStatement") END;
  9175. end := NewLabel();
  9176. elsifs := x.ElsifParts();
  9177. IfPart(x.ifPart);
  9178. FOR i := 0 TO elsifs-1 DO
  9179. IF ~escape THEN
  9180. elsif := x.GetElsifPart(i);
  9181. IfPart(elsif);
  9182. END;
  9183. END;
  9184. IF (x.elsePart # NIL) & ~escape THEN
  9185. StatementSequence(x.elsePart);
  9186. END;
  9187. SetLabel(end);
  9188. IF Trace THEN TraceExit("VisitIfStatement") END;
  9189. END VisitIfStatement;
  9190. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  9191. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  9192. BEGIN
  9193. (*IF x.variable.type.resolved = x.type.resolved THEN
  9194. (* always true, do nothing *)
  9195. ELSE*)
  9196. Designate(x.variable,res);
  9197. IF IsPointerToRecord(x.variable.type,recordType) THEN
  9198. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  9199. END;
  9200. trueL := NewLabel();
  9201. TypeTest(res.tag,x.type,trueL,falseL);
  9202. ReleaseOperand(res);
  9203. SetLabel(trueL);
  9204. StatementSequence(x.statements);
  9205. BrL(endL);
  9206. END WithPart;
  9207. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  9208. VAR endL,falseL: Label;i: LONGINT;
  9209. BEGIN
  9210. IF Trace THEN TraceEnter("VisitWithStatement") END;
  9211. endL := NewLabel();
  9212. FOR i := 0 TO x.WithParts()-1 DO
  9213. falseL := NewLabel();
  9214. WithPart(x.GetWithPart(i),falseL,endL);
  9215. SetLabel(falseL);
  9216. END;
  9217. IF x.elsePart = NIL THEN
  9218. IF ~isUnchecked THEN
  9219. EmitTrap(position,WithTrap);
  9220. END;
  9221. ELSE
  9222. StatementSequence(x.elsePart)
  9223. END;
  9224. SetLabel(endL);
  9225. IF Trace THEN TraceExit("VisitWithStatement") END;
  9226. END VisitWithStatement;
  9227. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  9228. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  9229. out,else: Label; label: Label;
  9230. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  9231. symbol: SyntaxTree.Symbol;
  9232. BEGIN
  9233. (*! split case statement into if-elsif statements for large case label lists *)
  9234. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  9235. size := x.max-x.min+1;
  9236. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  9237. END;
  9238. Evaluate(x.variable,var);
  9239. ReuseCopy(tmp,var.op);
  9240. ReleaseIntermediateOperand(var.op);
  9241. var.op := tmp;
  9242. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  9243. Convert(var.op,addressType);
  9244. size := x.max-x.min+1;
  9245. else := NewLabel();
  9246. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  9247. (*
  9248. UniqueId(name,module.module,"case",caseId);
  9249. *)
  9250. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  9251. Global.GetModuleSegmentedName(module.module, name);
  9252. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  9253. symbol := SyntaxTree.NewSymbol(name[1]);
  9254. symbol.SetScope(moduleScope);
  9255. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  9256. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  9257. section.isCaseTable := TRUE;
  9258. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  9259. ReuseCopy(res,var.op);
  9260. ReleaseOperand(var);
  9261. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  9262. Emit(Add(position,res,res,jmp));
  9263. IntermediateCode.MakeMemory(res,addressType);
  9264. Emit(Br(position,res));
  9265. ReleaseIntermediateOperand(res);
  9266. out := NewLabel();
  9267. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  9268. part := x.GetCasePart(i);
  9269. constant := part.firstConstant;
  9270. label := NewLabel();
  9271. SetLabel(label);
  9272. WHILE(constant # NIL) DO (* case labels for this case part *)
  9273. FOR j := constant.min TO constant.max DO
  9274. fixups[j-x.min] := label;
  9275. END;
  9276. constant := constant.next;
  9277. END;
  9278. StatementSequence(part.statements);
  9279. BrL(out);
  9280. END;
  9281. SetLabel(else);
  9282. FOR i := 0 TO size-1 DO
  9283. IF fixups[i] = NIL THEN
  9284. fixups[i] := else;
  9285. END;
  9286. END;
  9287. IF x.elsePart # NIL THEN
  9288. StatementSequence(x.elsePart);
  9289. ELSIF ~isUnchecked THEN
  9290. EmitTrap(position,CaseTrap);
  9291. END;
  9292. SetLabel(out);
  9293. FOR i := 0 TO size-1 DO
  9294. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  9295. section.Emit(Data(position,op));
  9296. END;
  9297. IF Trace THEN TraceExit("VisitCaseStatement") END;
  9298. END VisitCaseStatement;
  9299. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  9300. VAR start: Label; true,false: Label;
  9301. BEGIN
  9302. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  9303. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9304. start := NewLabel();
  9305. true := NewLabel();
  9306. false := NewLabel();
  9307. SetLabel(start);
  9308. Condition(x.condition,true,false);
  9309. SetLabel(true);
  9310. StatementSequence(x.statements);
  9311. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9312. BrL(start);
  9313. SetLabel(false);
  9314. IF Trace THEN TraceExit("VisitWhileStatement") END;
  9315. END VisitWhileStatement;
  9316. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  9317. VAR false,true: Label;
  9318. BEGIN
  9319. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  9320. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9321. true := NewLabel();
  9322. false := NewLabel();
  9323. SetLabel(false);
  9324. StatementSequence(x.statements);
  9325. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9326. Condition(x.condition,true,false);
  9327. SetLabel(true);
  9328. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  9329. END VisitRepeatStatement;
  9330. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  9331. VAR
  9332. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  9333. temporaryVariable: SyntaxTree.Variable;
  9334. temporaryVariableDesignator : SyntaxTree.Designator;
  9335. BEGIN
  9336. IF Trace THEN TraceEnter("VisitForStatement") END;
  9337. true := NewLabel();
  9338. false := NewLabel();
  9339. start := NewLabel();
  9340. Assign(x.variable,x.from);
  9341. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  9342. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition, NIL, temporaryVariable);
  9343. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  9344. Assign(temporaryVariableDesignator,x.to);
  9345. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  9346. IF by > 0 THEN
  9347. cmp := Scanner.LessEqual
  9348. ELSE
  9349. cmp := Scanner.GreaterEqual
  9350. END;
  9351. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,temporaryVariableDesignator,cmp);
  9352. binary.SetType(system.booleanType);
  9353. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9354. SetLabel(start);
  9355. Condition(binary,true,false);
  9356. SetLabel(true);
  9357. StatementSequence(x.statements);
  9358. binary := SyntaxTree.NewBinaryExpression(Basic.invalidPosition,x.variable,x.by,Scanner.Plus);
  9359. binary.SetType(x.variable.type);
  9360. Assign(x.variable,binary);
  9361. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9362. BrL(start);
  9363. SetLabel(false);
  9364. IF Trace THEN TraceExit("VisitForStatement") END;
  9365. END VisitForStatement;
  9366. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  9367. VAR prevLoop: Label;
  9368. BEGIN
  9369. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  9370. prevLoop := currentLoop;
  9371. currentLoop := NewLabel();
  9372. StatementSequence(x.statements);
  9373. SetLabel(currentLoop);
  9374. currentLoop := prevLoop;
  9375. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9376. END VisitExitableBlock;
  9377. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9378. VAR prevLoop,start: Label;
  9379. BEGIN
  9380. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9381. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9382. start := NewLabel();
  9383. prevLoop := currentLoop;
  9384. SetLabel(start);
  9385. currentLoop := NewLabel();
  9386. StatementSequence(x.statements);
  9387. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9388. BrL(start);
  9389. SetLabel(currentLoop);
  9390. currentLoop := prevLoop;
  9391. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9392. END VisitLoopStatement;
  9393. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9394. VAR outer: SyntaxTree.Statement;
  9395. BEGIN
  9396. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9397. IF locked THEN (* r if we jump out of an exclusive block *)
  9398. outer := x.outer;
  9399. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9400. outer := outer.outer;
  9401. END;
  9402. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9403. Lock(FALSE);
  9404. END;
  9405. END;
  9406. BrL(currentLoop);
  9407. IF Trace THEN TraceExit("VisitExitStatement") END;
  9408. END VisitExitStatement;
  9409. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9410. VAR
  9411. expression, parameterDesignator: SyntaxTree.Expression;
  9412. type, componentType: SyntaxTree.Type;
  9413. res, right: Operand;
  9414. left, mem, reg: IntermediateCode.Operand;
  9415. parameter: SyntaxTree.Parameter;
  9416. procedure: SyntaxTree.Procedure;
  9417. procedureType: SyntaxTree.ProcedureType;
  9418. returnTypeOffset: LONGINT;
  9419. delegate: BOOLEAN;
  9420. map: SymbolMap;
  9421. cc, parametersSize: LONGINT;
  9422. BEGIN
  9423. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9424. expression := x.returnValue;
  9425. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9426. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9427. IF currentIsInline THEN
  9428. IF expression # NIL THEN
  9429. map := currentMapper.Get(NIL);
  9430. IF map # NIL THEN
  9431. Assign(map.to, expression);
  9432. ELSE
  9433. Evaluate(expression,res);
  9434. Emit(Return(position,res.op));
  9435. ReleaseOperand(res);
  9436. END;
  9437. END;
  9438. BrL(currentInlineExit);
  9439. RETURN;
  9440. END;
  9441. IF expression # NIL THEN
  9442. type := expression.type.resolved;
  9443. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9444. IF locked THEN Lock(FALSE) END;
  9445. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9446. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9447. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9448. (* return without structured return parameter *)
  9449. Evaluate(expression,res);
  9450. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9451. IF locked OR profile THEN
  9452. Emit(Push(position,res.op));
  9453. IF delegate THEN HALT(200) END;
  9454. ReleaseOperand(res);
  9455. IF locked THEN Lock(FALSE) END;
  9456. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9457. reg := NewRegisterOperand(res.op.type);
  9458. Emit(Pop(position,reg));
  9459. Emit(Return(position,reg));
  9460. ReleaseIntermediateOperand(reg);
  9461. ELSE
  9462. Emit(Return(position,res.op));
  9463. ReleaseOperand(res);
  9464. END;
  9465. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9466. THEN
  9467. (* return using structured return parameter *)
  9468. 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)
  9469. OR SemanticChecker.IsPointerType(type));
  9470. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9471. parameter :=procedureType.returnParameter;
  9472. ASSERT(parameter # NIL);
  9473. returnTypeOffset := parameter.offsetInBits;
  9474. (*
  9475. IF parameter# NIL THEN
  9476. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9477. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9478. ELSE
  9479. returnTypeOffset := system.offsetFirstParameter
  9480. END;
  9481. *)
  9482. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9483. IF type IS SyntaxTree.RangeType THEN
  9484. (* array range type *)
  9485. Evaluate(expression, right);
  9486. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9487. Emit(Mov(position,mem, right.op)); (* first *)
  9488. ReleaseIntermediateOperand(mem);
  9489. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9490. Emit(Mov(position,mem, right.tag)); (* last *)
  9491. ReleaseIntermediateOperand(mem);
  9492. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9493. Emit(Mov(position,mem, right.extra)); (* step *)
  9494. ReleaseIntermediateOperand(mem);
  9495. ReleaseOperand(right);
  9496. ELSIF type IS SyntaxTree.ComplexType THEN
  9497. Evaluate(expression, right);
  9498. componentType := type(SyntaxTree.ComplexType).componentType;
  9499. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9500. Emit(Mov(position,mem, right.op)); (* real part *)
  9501. ReleaseIntermediateOperand(mem);
  9502. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9503. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9504. ReleaseIntermediateOperand(mem);
  9505. ReleaseOperand(right);
  9506. ELSE (* covers cases: pointer / record / array *)
  9507. parameter := procedureType.returnParameter;
  9508. checker.SetCurrentScope(currentScope);
  9509. ASSERT(parameter # NIL);
  9510. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9511. Assign(parameterDesignator,expression);
  9512. END;
  9513. ReleaseIntermediateOperand(left);
  9514. IF locked THEN Lock(FALSE) END;
  9515. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9516. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9517. parameter := procedureType.returnParameter;
  9518. checker.SetCurrentScope(currentScope);
  9519. IF parameter = NIL THEN
  9520. Error(procedure.position, "structured return of parameter of procedure not found");
  9521. ELSE
  9522. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9523. Assign(parameterDesignator,expression);
  9524. END;
  9525. IF locked THEN Lock(FALSE) END;
  9526. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9527. ELSE
  9528. HALT(200);
  9529. END;
  9530. ELSE
  9531. IF locked THEN Lock(FALSE) END;
  9532. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9533. END;
  9534. IF backend.cooperative THEN
  9535. BrL(exitLabel);
  9536. ELSE
  9537. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9538. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9539. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9540. ELSE
  9541. parametersSize := 0;
  9542. END;
  9543. EmitLeave(section, position,procedure, procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9544. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention, parametersSize));
  9545. END;
  9546. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9547. END VisitReturnStatement;
  9548. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9549. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9550. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9551. statements: SyntaxTree.StatementSequence;
  9552. name, suffix: SyntaxTree.IdentifierString;
  9553. BEGIN
  9554. Strings.IntToStr(awaitProcCounter,suffix);
  9555. Strings.Concat("@AwaitProcedure",suffix,name);
  9556. identifier := SyntaxTree.NewIdentifier(name);
  9557. INC(awaitProcCounter);
  9558. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9559. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9560. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9561. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9562. procedure.SetAccess(SyntaxTree.Hidden);
  9563. procedure.SetScope(currentScope);
  9564. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9565. procedureType.SetReturnType(system.booleanType);
  9566. procedure.SetType(procedureType);
  9567. body := SyntaxTree.NewBody(x.position,procedureScope);
  9568. procedureScope.SetBody(body);
  9569. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9570. returnStatement.SetReturnValue(x.condition);
  9571. statements := SyntaxTree.NewStatementSequence();
  9572. statements.AddStatement(returnStatement);
  9573. body.SetStatementSequence(statements);
  9574. currentScope.AddProcedure(procedure);
  9575. RETURN procedure
  9576. END MakeAwaitProcedure;
  9577. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9578. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9579. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9580. BEGIN
  9581. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9582. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9583. IF backend.cooperative THEN
  9584. start := NewLabel();
  9585. true := NewLabel();
  9586. false := NewLabel();
  9587. SetLabel(start);
  9588. Condition(x.condition,true,false);
  9589. SetLabel(false);
  9590. PushSelfPointer();
  9591. CallThis(position,"ExclusiveBlocks","Await",1);
  9592. BrL(start);
  9593. SetLabel(true);
  9594. PushSelfPointer();
  9595. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9596. ELSE
  9597. proc := MakeAwaitProcedure(x);
  9598. Emit(Push(position,fp));
  9599. GetCodeSectionNameForSymbol(proc,name);
  9600. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9601. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9602. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9603. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9604. Emit(Result(position,res));
  9605. (*
  9606. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9607. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9608. *)
  9609. InitOperand(result,ModeValue);
  9610. result.op := res;
  9611. label := NewLabel();
  9612. BreqL(label, result.op, SELF.true);
  9613. ReleaseOperand(result);
  9614. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9615. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9616. Emit(Push(position,res));
  9617. Emit(Push(position,fp));
  9618. PushSelfPointer();
  9619. Emit(Push(position,nil));
  9620. CallThis(position,"Objects","Await",4);
  9621. SetLabel(label);
  9622. END;
  9623. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9624. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9625. END VisitAwaitStatement;
  9626. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9627. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9628. BEGIN
  9629. FOR i := 0 TO x.Length() - 1 DO
  9630. statement := x.GetStatement( i );
  9631. Statement(statement);
  9632. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9633. END;
  9634. END StatementSequence;
  9635. PROCEDURE PushSelfPointer;
  9636. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT; procedure: SyntaxTree.Procedure;
  9637. procedureType: SyntaxTree.ProcedureType;
  9638. BEGIN
  9639. scope := currentScope;
  9640. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9641. scope := scope.outerScope;
  9642. END;
  9643. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9644. IF ~newObjectFile THEN
  9645. Symbol(moduleSelf,op);
  9646. IntermediateCode.MakeMemory(op.op,addressType);
  9647. ELSE
  9648. moduleSection := meta.ModuleSection();
  9649. IF backend.cooperative THEN
  9650. moduleOffset := 0;
  9651. ELSE
  9652. moduleOffset := moduleSection.pc;
  9653. END;
  9654. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9655. END;
  9656. ELSE
  9657. GetBaseRegister(op.op,currentScope,scope);
  9658. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9659. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9660. parametersSize := ProcedureParametersSize(system,procedure);
  9661. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)*(procedureType.parametersOffset+1)+parametersSize);
  9662. IF backend.cooperative THEN
  9663. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9664. END;
  9665. IntermediateCode.MakeMemory(op.op,addressType);
  9666. END;
  9667. Emit(Push(position,op.op));
  9668. ReleaseOperand(op);
  9669. END PushSelfPointer;
  9670. PROCEDURE Lock(lock: BOOLEAN);
  9671. BEGIN
  9672. IF Trace THEN TraceEnter("Lock") END;
  9673. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9674. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9675. ASSERT(modifyAssignmentCounter = 0);
  9676. IF dump # NIL THEN
  9677. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9678. dump.Ln;dump.Update;
  9679. END;
  9680. PushSelfPointer;
  9681. IF backend.cooperative THEN
  9682. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9683. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9684. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9685. END;
  9686. ELSE
  9687. Emit(Push(position,true));
  9688. IF lock THEN CallThis(position,"Objects","Lock",2)
  9689. ELSE CallThis(position,"Objects","Unlock",2);
  9690. END;
  9691. END;
  9692. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9693. IF Trace THEN TraceExit("Lock") END;
  9694. END Lock;
  9695. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9696. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9697. BEGIN
  9698. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9699. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9700. previouslyUnchecked := isUnchecked;
  9701. isUnchecked := isUnchecked OR x.isUnchecked;
  9702. previouslyCooperativeSwitches := cooperativeSwitches;
  9703. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9704. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9705. IF x.statements # NIL THEN
  9706. StatementSequence(x.statements);
  9707. END;
  9708. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9709. isUnchecked := previouslyUnchecked;
  9710. cooperativeSwitches := previouslyCooperativeSwitches;
  9711. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9712. END VisitStatementBlock;
  9713. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9714. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9715. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9716. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9717. procedure: SyntaxTree.Procedure;
  9718. map: SymbolMap;
  9719. cc, parametersSize: LONGINT;
  9720. BEGIN
  9721. scope := currentScope;
  9722. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9723. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9724. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9725. return := emptyOperand;
  9726. IF Trace THEN TraceEnter("VisitCode") END;
  9727. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9728. NEW(in, x.inRules.Length());
  9729. FOR i := 0 TO LEN(in)-1 DO
  9730. statement := x.inRules.GetStatement(i);
  9731. WITH statement: SyntaxTree.Assignment DO
  9732. Evaluate(statement.right, operand);
  9733. result := operand.op;
  9734. NEW(str, 64);
  9735. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9736. in[i] := result; IntermediateCode.SetString(in[i], str);
  9737. ReleaseIntermediateOperand(operand.tag);
  9738. END;
  9739. END;
  9740. ELSE in := NIL
  9741. END;
  9742. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9743. NEW(out, x.outRules.Length());
  9744. FOR i := 0 TO LEN(out)-1 DO
  9745. statement := x.outRules.GetStatement(i);
  9746. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9747. WITH statement: SyntaxTree.Assignment DO
  9748. Designate(statement.left, operand);
  9749. MakeMemory(result, operand.op, IntermediateCode.GetType(system,statement.left.type) , 0);
  9750. NEW(str, 64);
  9751. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9752. out[i] := result; IntermediateCode.SetString(out[i], str);
  9753. ReleaseOperand(operand); (* implicit increase of use of operand.op in MakeMemory *)
  9754. |statement: SyntaxTree.ReturnStatement DO
  9755. NEW(str, 64);
  9756. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9757. IF currentIsInline THEN
  9758. map := currentMapper.Get(NIL);
  9759. Designate(map.to, operand);
  9760. IF map.isAddress THEN
  9761. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9762. ELSE
  9763. result := operand.op;
  9764. END;
  9765. (*! only if it does not fit into register
  9766. MakeMemory(result, operand.op, IntermediateCode.GetType(system,map.to.type) , 0);
  9767. *)
  9768. (*Evaluate(map.to, operand);*)
  9769. out[i] := result;
  9770. ELSE
  9771. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9772. END;
  9773. IntermediateCode.SetString(out[i], str);
  9774. ReleaseIntermediateOperand(operand.tag);
  9775. return := out[i];
  9776. ELSE
  9777. END;
  9778. END;
  9779. ELSE out := NIL
  9780. END;
  9781. Emit(Asm(x.position,x.sourceCode, in, out));
  9782. IF in # NIL THEN
  9783. FOR i := 0 TO LEN(in)-1 DO
  9784. ReleaseIntermediateOperand(in[i]);
  9785. END;
  9786. END;
  9787. IF out # NIL THEN
  9788. FOR i := 0 TO LEN(out)-1 DO
  9789. WITH statement: SyntaxTree.Assignment DO
  9790. ReleaseIntermediateOperand(out[i]);
  9791. |statement: SyntaxTree.ReturnStatement DO
  9792. (* release happens below *)
  9793. ELSE
  9794. END;
  9795. statement := x.outRules.GetStatement(i);
  9796. END;
  9797. END;
  9798. IF return.mode # IntermediateCode.Undefined THEN
  9799. IF currentIsInline THEN
  9800. ELSIF SemanticChecker.ReturnedAsParameter(procedureType.returnType) THEN
  9801. Symbol(procedureType.returnParameter, par);
  9802. MakeMemory(mem, par.op, return.type, 0);
  9803. ReleaseOperand(par);
  9804. Emit(Mov(position, mem, return));
  9805. ReleaseIntermediateOperand(mem);
  9806. ELSE
  9807. Emit(Return(position,return));
  9808. END;
  9809. ReleaseIntermediateOperand(return);
  9810. IF currentIsInline THEN RETURN END;
  9811. cc := procedureType(SyntaxTree.ProcedureType).callingConvention;
  9812. IF cc = SyntaxTree.WinAPICallingConvention THEN
  9813. parametersSize := ProcedureParametersSize(backend.system,procedure);
  9814. ELSE
  9815. parametersSize := 0;
  9816. END;
  9817. EmitLeave(section, position,NIL, cc);
  9818. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,cc, parametersSize));
  9819. END;
  9820. IF Trace THEN TraceExit("VisitCode") END;
  9821. END VisitCode;
  9822. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9823. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9824. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9825. const, call: IntermediateCode.Operand;
  9826. parameterDesignator: SyntaxTree.Expression;
  9827. saved: RegisterEntry;
  9828. name: Basic.SegmentedName;
  9829. BEGIN
  9830. IF Trace THEN TraceEnter("ParameterCopies") END;
  9831. parameter := x.firstParameter;
  9832. WHILE parameter # NIL DO
  9833. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9834. type := parameter.type.resolved;
  9835. IF IsOpenArray(type) THEN
  9836. VisitParameter(parameter);
  9837. op := result;
  9838. IF backend.cooperative & parameter.NeedsTrace() THEN
  9839. length := GetArrayLength(type, op.tag);
  9840. size := NewRegisterOperand(addressType);
  9841. base := ArrayBaseType(type);
  9842. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9843. Emit(Mul(position, size, length, const));
  9844. dst := NewRegisterOperand (addressType);
  9845. Emit(Mov(position, dst, size));
  9846. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9847. Emit(Sub(position,dst,sp,dst));
  9848. Emit(And(position,dst,dst,const));
  9849. Emit(Mov(position,sp,dst));
  9850. par := fp;
  9851. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9852. IntermediateCode.InitImmediate(null, byteType, 0);
  9853. Emit(Fill(position, dst, size, null));
  9854. ReleaseIntermediateOperand(dst);
  9855. ReleaseIntermediateOperand(length);
  9856. SaveRegisters();ReleaseUsedRegisters(saved);
  9857. (* register dst has been freed before SaveRegisters already *)
  9858. base := ArrayBaseType(type);
  9859. (* assign method of open array *)
  9860. IF base.IsRecordType() THEN
  9861. Emit (Push(position, length));
  9862. Emit (Push(position, dst));
  9863. Emit (Push(position, op.op));
  9864. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9865. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9866. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9867. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9868. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9869. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9870. Emit (Push(position,length));
  9871. Emit (Push(position, dst));
  9872. Emit (Push(position, length));
  9873. Emit (Push(position, op.op));
  9874. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9875. ELSE
  9876. Emit (Push(position, length));
  9877. Emit (Push(position, dst));
  9878. Emit (Push(position, length));
  9879. Emit (Push(position, op.op));
  9880. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9881. ASSERT(ArrayBaseType(type).IsPointer());
  9882. END;
  9883. RestoreRegisters(saved);
  9884. ELSE
  9885. temp := GetDynamicSize(type,op.tag);
  9886. ReuseCopy(size,temp);
  9887. ReleaseIntermediateOperand(temp);
  9888. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9889. Emit(Sub(position,size,sp,size));
  9890. Emit(And(position,size,size,const));
  9891. Emit(Mov(position,sp,size));
  9892. par := fp;
  9893. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9894. ReleaseIntermediateOperand(size);
  9895. size := GetDynamicSize(type,op.tag);
  9896. END;
  9897. Emit(Copy(position,sp,op.op,size));
  9898. ReleaseIntermediateOperand(size);
  9899. ReleaseOperand(op);
  9900. IntermediateCode.MakeMemory(par,addressType);
  9901. Emit(Mov(position,par,sp));
  9902. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9903. checker.SetCurrentScope(currentScope);
  9904. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9905. Assign(parameterDesignator,parameterDesignator);
  9906. END;
  9907. END;
  9908. parameter := parameter.nextParameter;
  9909. END;
  9910. IF Trace THEN TraceExit("ParameterCopies") END;
  9911. END ParameterCopies;
  9912. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9913. VAR x: SyntaxTree.Variable;
  9914. BEGIN
  9915. x := scope.firstVariable;
  9916. WHILE x # NIL DO
  9917. InitVariable(x);
  9918. x := x.nextVariable;
  9919. END;
  9920. END InitVariables;
  9921. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9922. BEGIN
  9923. IF (symbol # NIL) THEN
  9924. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9925. ELSE
  9926. RETURN 0
  9927. END;
  9928. END GetFingerprint;
  9929. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9930. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9931. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9932. saved: RegisterEntry; left, right: IntermediateCode.Operand;
  9933. name: Basic.SegmentedName;
  9934. offset: LONGINT;
  9935. BEGIN
  9936. IF Trace THEN TraceEnter("Body") END;
  9937. ReleaseUsedRegisters(saved); (* just in case ... *)
  9938. section := ir;
  9939. exitLabel := NewLabel ();
  9940. IF moduleBody THEN moduleBodySection := section END;
  9941. IF ir.comments # NIL THEN
  9942. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9943. commentPrintout.SingleStatement(TRUE);
  9944. dump := ir.comments;
  9945. ELSE
  9946. commentPrintout := NIL;
  9947. dump := NIL;
  9948. END;
  9949. prevScope := currentScope;
  9950. currentScope := scope;
  9951. lastSwitchPC := 0;
  9952. cooperativeSwitches := backend.cooperative;
  9953. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9954. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9955. IF x # NIL THEN
  9956. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9957. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9958. IF moduleBody THEN
  9959. ProfilerInit();
  9960. ELSE
  9961. Basic.SegmentedNameToString(ir.name, string);
  9962. ProfilerAddProcedure(numberProcedures,string);
  9963. ProfilerEnterExit(numberProcedures,TRUE);
  9964. END;
  9965. END;
  9966. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9967. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9968. END;
  9969. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9970. IF moduleBody & ~newObjectFile THEN
  9971. InitVariables(moduleScope)
  9972. END;
  9973. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9974. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9975. IF procedure = cellScope.bodyProcedure THEN
  9976. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9977. StaticCallOperand(op, cellScope.constructor);
  9978. Emit(Call(position,op.op,0));
  9979. END;
  9980. END;
  9981. END;
  9982. ParameterCopies(procedureType);
  9983. InitVariables(scope);
  9984. IF backend.preciseGC & (x.code = NIL) & (~procedureType.noPAF) & ~procedure.isEntry & ~procedure.isExit THEN
  9985. GetCodeSectionNameForSymbol(procedure, name);
  9986. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  9987. IntermediateCode.InitAddress(right, addressType, name, 0, 0);
  9988. offset := ToMemoryUnits(module.system,meta.RecordBaseOffset*module.system.addressSize)+1;
  9989. IntermediateCode.SetOffset(right,offset); (* tag *)
  9990. IntermediateCode.InitMemory(left,addressType,fp,0);
  9991. Emit(Mov(position, left, right));
  9992. END;
  9993. IF x.code = NIL THEN
  9994. VisitStatementBlock(x);
  9995. ELSE
  9996. VisitCode(x.code)
  9997. END;
  9998. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9999. ir.SetFinally(ir.pc);
  10000. StatementSequence(x.finally)
  10001. END;
  10002. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  10003. IF ~backend.cooperative THEN
  10004. ProfilerEnterExit(numberProcedures,FALSE);
  10005. END;
  10006. INC(numberProcedures);
  10007. END;
  10008. END;
  10009. IF backend.cooperative THEN
  10010. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  10011. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  10012. END;
  10013. IF x # NIL THEN
  10014. SELF.position := x.position;
  10015. END;
  10016. CheckRegistersFree();
  10017. ASSERT(modifyAssignmentCounter = 0);
  10018. currentScope := prevScope;
  10019. IF Trace THEN TraceExit("Body") END;
  10020. END Body;
  10021. END ImplementationVisitor;
  10022. MetaDataGenerator=OBJECT
  10023. VAR
  10024. implementationVisitor: ImplementationVisitor;
  10025. declarationVisitor: DeclarationVisitor;
  10026. module: Sections.Module;
  10027. moduleName: ARRAY 128 OF CHAR;
  10028. moduleNamePool: Basic.HashTableInt;
  10029. moduleNamePoolSection: IntermediateCode.Section;
  10030. modulePointerSection: IntermediateCode.Section;
  10031. modulePointerSizePC: LONGINT;
  10032. modulePointerSectionOffset: LONGINT;
  10033. modulePointers: LONGINT;
  10034. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  10035. RecordBaseOffset: LONGINT;
  10036. MethodTableOffset: LONGINT; (* method table offset from zero *)
  10037. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  10038. TypeTags: LONGINT; (* type extension level support *)
  10039. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  10040. patchInfoPC: LONGINT;
  10041. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  10042. BEGIN
  10043. IF implementationVisitor.backend.cooperative THEN
  10044. TypeTags := MAX(LONGINT);
  10045. BaseTypesTableOffset := 0;
  10046. MethodTableOffset := 2;
  10047. TypeRecordBaseOffset := 0;
  10048. RecordBaseOffset := 0;
  10049. ELSIF simple THEN
  10050. TypeTags := 3; (* only 3 extensions allowed *)
  10051. BaseTypesTableOffset := 1;
  10052. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  10053. TypeRecordBaseOffset := 0;
  10054. RecordBaseOffset := 1;
  10055. ELSE
  10056. TypeTags := 16;
  10057. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  10058. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  10059. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  10060. RecordBaseOffset := 8;
  10061. END;
  10062. SELF.simple := simple;
  10063. SELF.implementationVisitor := implementationVisitor;
  10064. SELF.declarationVisitor := declarationVisitor;
  10065. implementationVisitor.meta := SELF;
  10066. declarationVisitor.meta := SELF;
  10067. END InitMetaDataGenerator;
  10068. PROCEDURE SetModule(module: Sections.Module);
  10069. VAR namePoolOffset, offset: LONGINT; name: Basic.SegmentedName;
  10070. BEGIN
  10071. SELF.module := module;
  10072. Global.GetModuleName(module.module,moduleName);
  10073. IF ReflectionSupport & implementationVisitor.newObjectFile & ~simple & ~implementationVisitor.backend.cooperative THEN
  10074. NEW(moduleNamePool, 32);
  10075. (*! require GC protection *)
  10076. modulePointerSection := Block("Heaps","ArrayBlockDesc",".@ModulePointerArray", modulePointerSectionOffset);
  10077. name := "Heaps.AnyPtr";
  10078. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10079. (* set base pointer *)
  10080. NamedSymbolAt(modulePointerSection, modulePointerSectionOffset -1, name, NIL, 0, offset);
  10081. ArrayBlock(modulePointerSection, modulePointerSizePC, "", TRUE);
  10082. modulePointers := 0;
  10083. moduleNamePoolSection := Block("Heaps","SystemBlockDesc",".@ModuleNamePool", namePoolOffset);
  10084. AddPointer(moduleNamePoolSection, namePoolOffset);
  10085. END;
  10086. END SetModule;
  10087. PROCEDURE AddPointer(section: IntermediateCode.Section; offset: LONGINT);
  10088. BEGIN
  10089. IF ~implementationVisitor.backend.cooperative THEN
  10090. NamedSymbol(modulePointerSection, section.name, NIL, 0, offset);
  10091. INC(modulePointers);
  10092. (* optimization hint: this can be done once at the end but for consistency of the first tests we keep it like this *)
  10093. PatchSize(modulePointerSection, modulePointerSizePC, modulePointers);
  10094. END;
  10095. END AddPointer;
  10096. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  10097. BEGIN
  10098. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  10099. END GetTypeRecordBaseOffset;
  10100. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10101. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  10102. BEGIN
  10103. INC(dataAdrOffset,6);
  10104. Info(section,"headerAdr");
  10105. Address(section,0);
  10106. Info(section,"typeDesc");
  10107. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  10108. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  10109. NamedSymbol(section, name, symbol, 0, offset);
  10110. Info(section,"mark: LONGINT;");
  10111. Longint(section,-1);
  10112. IF module.system.addressType.sizeInBits = 64 THEN Longint(section, 0);END;
  10113. Info(section,"dataAdr-: ADDRESS");
  10114. Symbol(section,section, dataAdrOffset,0);
  10115. Info(section,"size-: SIZE");
  10116. Address(section,0);
  10117. Info(section,"nextRealtime: HeapBlock;");
  10118. Address(section,0);
  10119. END HeapBlock;
  10120. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  10121. VAR i: LONGINT;
  10122. BEGIN
  10123. INC(dataAdrOffset,14); (*! change this when changing data structure below *)
  10124. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  10125. Info(section,"count*: LONGINT");
  10126. Longint(section,0);
  10127. Info(section,"locked*: BOOLEAN");
  10128. Longint(section,0);
  10129. Info(section,"awaitingLock*: ProcessQueue");
  10130. Address(section,0);
  10131. Address(section,0);
  10132. Info(section,"awaitingCond*: ProcessQueue");
  10133. Address(section,0);
  10134. Address(section,0);
  10135. Info(section,"lockedBy*: ANY");
  10136. Address(section,0);
  10137. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  10138. Longint(section,1);
  10139. FOR i := 2 TO 6 DO
  10140. Longint(section,0);
  10141. END;
  10142. Info(section,"lock*: ANY");
  10143. Address(section,0);
  10144. END ProtectedHeapBlock;
  10145. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  10146. BEGIN
  10147. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  10148. END Info;
  10149. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  10150. VAR op: IntermediateCode.Operand;
  10151. BEGIN
  10152. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10153. section.Emit(Data(Basic.invalidPosition,op));
  10154. END Address;
  10155. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  10156. VAR op: IntermediateCode.Operand;
  10157. BEGIN
  10158. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10159. section.Emit(Data(Basic.invalidPosition,op));
  10160. END Size;
  10161. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  10162. VAR op: IntermediateCode.Operand;
  10163. BEGIN
  10164. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  10165. section.Emit(Data(Basic.invalidPosition,op));
  10166. END Set;
  10167. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  10168. VAR op: IntermediateCode.Operand;
  10169. BEGIN
  10170. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10171. section.Emit(Data(Basic.invalidPosition,op));
  10172. END Longint;
  10173. PROCEDURE PatchAddress(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10174. VAR op,noOperand: IntermediateCode.Operand;
  10175. BEGIN
  10176. IntermediateCode.InitOperand(noOperand);
  10177. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  10178. section.PatchOperands(pc,op,noOperand,noOperand);
  10179. END PatchAddress;
  10180. PROCEDURE PatchSize(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10181. VAR op,noOperand: IntermediateCode.Operand;
  10182. BEGIN
  10183. IntermediateCode.InitOperand(noOperand);
  10184. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  10185. section.PatchOperands(pc,op,noOperand,noOperand);
  10186. END PatchSize;
  10187. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  10188. VAR op,noOperand: IntermediateCode.Operand;
  10189. BEGIN
  10190. IntermediateCode.InitOperand(noOperand);
  10191. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  10192. section.PatchOperands(pc,op,noOperand,noOperand);
  10193. END PatchLongint;
  10194. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10195. VAR op, noOperand: IntermediateCode.Operand;
  10196. BEGIN
  10197. IntermediateCode.InitOperand(noOperand);
  10198. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10199. section.PatchOperands(pc,op,noOperand,noOperand);
  10200. END PatchSymbol;
  10201. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  10202. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  10203. BEGIN
  10204. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  10205. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  10206. section.Emit(Data(Basic.invalidPosition,op));
  10207. END Boolean;
  10208. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  10209. VAR op: IntermediateCode.Operand;
  10210. BEGIN
  10211. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  10212. section.Emit(Data(Basic.invalidPosition,op));
  10213. END Char;
  10214. PROCEDURE Integer(section: IntermediateCode.Section; int: LONGINT);
  10215. VAR op: IntermediateCode.Operand;
  10216. BEGIN
  10217. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.integerType),int);
  10218. section.Emit(Data(Basic.invalidPosition,op));
  10219. END Integer;
  10220. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  10221. VAR i: LONGINT;
  10222. BEGIN
  10223. Info(section,str);
  10224. i := 0;
  10225. WHILE(str[i] # 0X) DO
  10226. Char(section,str[i]);
  10227. INC(i);
  10228. END;
  10229. Char(section,0X);
  10230. END String;
  10231. PROCEDURE String0(section: IntermediateCode.Section; str: StringPool.Index);
  10232. VAR s: Basic.SectionName;
  10233. BEGIN
  10234. StringPool.GetString(str, s);
  10235. String(section, s);
  10236. END String0;
  10237. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10238. VAR op: IntermediateCode.Operand;
  10239. BEGIN
  10240. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10241. IntermediateCode.SetOffset(op,realOffset);
  10242. section.Emit(Data(Basic.invalidPosition,op));
  10243. END NamedSymbol;
  10244. PROCEDURE NamedSymbolAt(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  10245. VAR op: IntermediateCode.Operand;
  10246. BEGIN
  10247. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  10248. IntermediateCode.SetOffset(op,realOffset);
  10249. section.EmitAt(pc, Data(Basic.invalidPosition,op));
  10250. END NamedSymbolAt;
  10251. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  10252. BEGIN
  10253. IF symbol= NIL THEN
  10254. Address( section, realOffset);
  10255. ASSERT(virtualOffset = 0);
  10256. ELSE
  10257. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  10258. END;
  10259. END Symbol;
  10260. (* OutPointers delivers
  10261. {pointerOffset}
  10262. *)
  10263. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  10264. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type; property: SyntaxTree.Property; parameter: SyntaxTree.Parameter;
  10265. BEGIN
  10266. type := type.resolved;
  10267. IF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  10268. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10269. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10270. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  10271. Symbol(section, symbol, 0, offset); INC(numberPointers);
  10272. ELSIF type IS SyntaxTree.PointerType THEN
  10273. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  10274. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  10275. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  10276. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  10277. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  10278. ELSIF (type IS SyntaxTree.RecordType) THEN
  10279. (* never treat a record like a pointer, even if the pointer field is set! *)
  10280. WITH type: SyntaxTree.RecordType DO
  10281. base := type.GetBaseRecord();
  10282. IF base # NIL THEN
  10283. Pointers(offset,symbol,section, base,numberPointers);
  10284. END;
  10285. variable := type.recordScope.firstVariable;
  10286. WHILE(variable # NIL) DO
  10287. IF ~(variable.untraced) THEN
  10288. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10289. END;
  10290. variable := variable.nextVariable;
  10291. END;
  10292. END;
  10293. ELSIF (type IS SyntaxTree.CellType) THEN
  10294. WITH type: SyntaxTree.CellType DO
  10295. base := type.GetBaseRecord();
  10296. IF base # NIL THEN
  10297. Pointers(offset,symbol,section, base,numberPointers);
  10298. END;
  10299. variable := type.cellScope.firstVariable;
  10300. WHILE(variable # NIL) DO
  10301. IF ~(variable.untraced) THEN
  10302. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  10303. END;
  10304. variable := variable.nextVariable;
  10305. END;
  10306. property := type.firstProperty;
  10307. WHILE(property # NIL) DO
  10308. IF ~(property.untraced) THEN
  10309. Pointers(offset+ToMemoryUnits(module.system,property.offsetInBits), symbol, section, property.type,numberPointers);
  10310. END;
  10311. property := property.nextProperty;
  10312. END;
  10313. parameter := type.firstParameter;
  10314. WHILE(parameter # NIL) DO
  10315. IF ~(parameter.untraced) THEN
  10316. Pointers(offset+ToMemoryUnits(module.system,parameter.offsetInBits), symbol, section, parameter.type,numberPointers);
  10317. END;
  10318. parameter := parameter.nextParameter;
  10319. END;
  10320. END;
  10321. ELSIF (type IS SyntaxTree.ArrayType) THEN
  10322. WITH type: SyntaxTree.ArrayType DO
  10323. IF type.form= SyntaxTree.Static THEN
  10324. n := type.staticLength;
  10325. base := type.arrayBase.resolved;
  10326. WHILE(base IS SyntaxTree.ArrayType) DO
  10327. type := base(SyntaxTree.ArrayType);
  10328. n := n* type.staticLength;
  10329. base := type.arrayBase.resolved;
  10330. END;
  10331. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10332. IF SemanticChecker.ContainsPointer(base) THEN
  10333. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10334. FOR i := 0 TO n-1 DO
  10335. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10336. END;
  10337. END;
  10338. ELSE
  10339. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  10340. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10341. END;
  10342. END;
  10343. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  10344. WITH type: SyntaxTree.MathArrayType DO
  10345. IF type.form = SyntaxTree.Static THEN
  10346. n := type.staticLength;
  10347. base := type.arrayBase.resolved;
  10348. WHILE(base IS SyntaxTree.MathArrayType) DO
  10349. type := base(SyntaxTree.MathArrayType);
  10350. n := n* type.staticLength;
  10351. base := type.arrayBase.resolved;
  10352. END;
  10353. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  10354. IF SemanticChecker.ContainsPointer(base) THEN
  10355. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  10356. FOR i := 0 TO n-1 DO
  10357. Pointers(offset+i*size, symbol, section, base,numberPointers);
  10358. END;
  10359. END;
  10360. ELSE
  10361. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  10362. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  10363. END
  10364. END;
  10365. (* ELSE no pointers in type *)
  10366. END;
  10367. END Pointers;
  10368. PROCEDURE EnterDynamicName(source: IntermediateCode.Section; CONST name: ARRAY OF CHAR; index: LONGINT; pool: Basic.HashTableInt): LONGINT;
  10369. VAR position,i: LONGINT; ch: CHAR;
  10370. BEGIN
  10371. IF pool.Has(index) THEN
  10372. RETURN pool.GetInt(index)
  10373. ELSE
  10374. position := source.pc;
  10375. pool.PutInt(index, position);
  10376. Info(source, name);
  10377. i := 0;
  10378. REPEAT
  10379. ch := name[i]; INC(i);
  10380. Char( source, ch);
  10381. UNTIL ch = 0X;
  10382. END;
  10383. RETURN position;
  10384. END EnterDynamicName;
  10385. PROCEDURE DynamicName(source: IntermediateCode.Section; index: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  10386. VAR name: Basic.SectionName; position: LONGINT;
  10387. BEGIN
  10388. IF pool.Has(index) THEN
  10389. RETURN pool.GetInt(index)
  10390. ELSE
  10391. StringPool.GetString(index, name);
  10392. position := EnterDynamicName(source,name,index, pool);
  10393. END;
  10394. RETURN position;
  10395. END DynamicName;
  10396. PROCEDURE NamedBlock(CONST mName, typeName: ARRAY OF CHAR; name: Basic.SegmentedName; VAR offset: LONGINT): IntermediateCode.Section;
  10397. VAR section: IntermediateCode.Section;
  10398. BEGIN
  10399. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL, declarationVisitor.dump);
  10400. IF implementationVisitor.backend.cooperative THEN
  10401. Info(section, "TypeDescriptor");
  10402. Basic.ToSegmentedName("BaseTypes.Array", name);
  10403. NamedSymbol(section, name,NIL, 0, 0);
  10404. BasePointer(section);
  10405. offset := 0;
  10406. ELSE
  10407. HeapBlock(mName,typeName,section,2);
  10408. Info(section, "HeapBlock");
  10409. (*
  10410. Symbol(section,section,2,0);
  10411. *)
  10412. Address(section,0); (* empty such that GC does not go on traversing *)
  10413. Info(section, "TypeDescriptor");
  10414. Address(section,0);
  10415. offset := section.pc;
  10416. END;
  10417. RETURN section
  10418. END NamedBlock;
  10419. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  10420. VAR name: ARRAY 128 OF CHAR; pooledName: Basic.SegmentedName;
  10421. BEGIN
  10422. COPY(moduleName,name);
  10423. Strings.Append(name,suffix);
  10424. Basic.ToSegmentedName(name, pooledName);
  10425. RETURN NamedBlock(mName, typeName, pooledName, offset);
  10426. END Block;
  10427. PROCEDURE ArrayBlock(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR; hasPointer: BOOLEAN);
  10428. VAR name: Basic.SegmentedName;
  10429. BEGIN
  10430. Info(source,"ArrayHeader");
  10431. IF implementationVisitor.backend.cooperative THEN
  10432. sizePC := source.pc;
  10433. Address(source,0);
  10434. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  10435. IF baseType # "" THEN
  10436. Basic.ToSegmentedName(baseType, name);
  10437. NamedSymbol(source, name,NIL, 0, 0);
  10438. ELSE
  10439. Address(source,0);
  10440. END;
  10441. Address(source,0);
  10442. ELSE
  10443. Address(source,0);
  10444. Address(source,0);
  10445. (* first pointer for GC *)
  10446. IF hasPointer THEN
  10447. (* points to first element in the array, this is NOT the base type descriptor *)
  10448. NamedSymbol(source,source.name, NIL,source.pc+2,0);
  10449. ELSE
  10450. Address(source,0);
  10451. END;
  10452. sizePC := source.pc;
  10453. Address(source,0);
  10454. Info(source,"array data");
  10455. END;
  10456. END ArrayBlock;
  10457. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  10458. BEGIN
  10459. IF implementationVisitor.backend.cooperative THEN
  10460. PatchSize(section, pc, size);
  10461. PatchSize(section, pc + 3, size);
  10462. ELSE
  10463. PatchSize(section, pc-3, size); (* actually only for arrays with pointers, but does not harm... *)
  10464. PatchSize(section, pc, size);
  10465. END;
  10466. END PatchArray;
  10467. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  10468. VAR
  10469. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  10470. sectionArray: POINTER TO ARRAY OF Sections.Section;
  10471. poolMap: Basic.HashTableInt;
  10472. namePool: IntermediateCode.Section;
  10473. namePoolOffset: LONGINT;
  10474. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  10475. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  10476. BEGIN
  10477. Basic.SegmentedNameToString(s1.name,n1);
  10478. Basic.SegmentedNameToString(s2.name,n2);
  10479. index := 0;
  10480. ch1 := n1[index];
  10481. ch2 := n2[index];
  10482. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  10483. INC(index);
  10484. ch1 := n1[index];
  10485. ch2 := n2[index];
  10486. END;
  10487. RETURN ch1 < ch2;
  10488. END Compare;
  10489. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  10490. VAR
  10491. i, j: LONGINT;
  10492. x, t: Sections.Section;
  10493. BEGIN
  10494. IF lo < hi THEN
  10495. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  10496. WHILE i <= j DO
  10497. WHILE Compare(list[i], x) DO INC(i) END;
  10498. WHILE Compare(x, list[j]) DO DEC(j) END;
  10499. IF i <= j THEN
  10500. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  10501. INC(i); DEC(j)
  10502. END
  10503. END;
  10504. IF lo < j THEN QuickSort(list, lo, j) END;
  10505. IF i < hi THEN QuickSort(list, i, hi) END
  10506. END;
  10507. END QuickSort;
  10508. (*
  10509. ExportDesc* = RECORD
  10510. fp*: ADDRESS;
  10511. name* {UNTRACED}: DynamicName;
  10512. adr*: ADDRESS;
  10513. exports*: LONGINT;
  10514. dsc* {UNTRACED}: ExportArray
  10515. END;
  10516. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10517. *)
  10518. PROCEDURE ExportDesc2(
  10519. source: IntermediateCode.Section;
  10520. namePool: IntermediateCode.Section;
  10521. fingerPrinter: FingerPrinter.FingerPrinter;
  10522. symbol: Sections.Section;
  10523. name: StringPool.Index;
  10524. VAR patchAdr: LONGINT
  10525. ): BOOLEAN;
  10526. VAR fingerPrint: SyntaxTree.FingerPrint;
  10527. BEGIN
  10528. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10529. & (symbol.type # Sections.InlineCodeSection)
  10530. THEN
  10531. *)
  10532. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10533. & (symbol.type # Sections.InlineCodeSection))
  10534. THEN
  10535. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10536. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10537. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10538. Longint(source,fingerPrint.shallow);
  10539. ELSE
  10540. Longint(source, 0);
  10541. END;
  10542. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0);END;
  10543. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10544. Symbol(source, symbol,0,0);
  10545. patchAdr := source.pc;
  10546. Longint(source, 0);
  10547. IF module.system.addressType.sizeInBits = 64 THEN Longint(source, 0); END;
  10548. Address(source,0);
  10549. END;
  10550. RETURN TRUE
  10551. ELSE
  10552. RETURN FALSE
  10553. END;
  10554. END ExportDesc2;
  10555. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10556. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10557. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10558. sym: Sections.Section; offset: LONGINT; symbol: Sections.Section;
  10559. nextPatch: LONGINT;
  10560. TYPE
  10561. Scope = RECORD
  10562. elements: LONGINT;
  10563. gelements: LONGINT;
  10564. section: IntermediateCode.Section;
  10565. patchAdr: LONGINT;
  10566. arraySizePC: LONGINT;
  10567. beginPC: LONGINT; (* current scope start pc *)
  10568. END;
  10569. BEGIN
  10570. Basic.InitSegmentedName(prev);
  10571. olevel := -1;
  10572. scopes[0].section := source;
  10573. scopes[0].arraySizePC := MIN(LONGINT);
  10574. FOR s := 0 TO LEN(sections)-1 DO
  10575. symbol := sections[s];
  10576. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10577. this := sections[s].name;
  10578. level := 0;
  10579. WHILE (this[level] > 0) DO
  10580. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10581. INC(level);
  10582. END;
  10583. WHILE level < olevel DO
  10584. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10585. IF olevel > 0 THEN
  10586. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10587. nextPatch := scopes[olevel-1].patchAdr+1;
  10588. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10589. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10590. END;
  10591. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10592. DEC(olevel);
  10593. END;
  10594. IF (this[level] > 0) THEN
  10595. IF level > olevel THEN
  10596. (*TRACE("opening",level); *)
  10597. IF scopes[level].section = NIL THEN
  10598. arrayName := ".@ExportArray";
  10599. Strings.AppendInt(arrayName, level);
  10600. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,offset);
  10601. AddPointer(scopes[level].section,offset);
  10602. ArrayBlock(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc", FALSE);
  10603. END;
  10604. scopes[level].beginPC := scopes[level].section.pc;
  10605. olevel := level;
  10606. scopes[olevel].elements := 0;
  10607. END;
  10608. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10609. sym := sections[s];
  10610. ELSE
  10611. sym := NIL;
  10612. END;
  10613. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10614. THEN
  10615. INC(scopes[olevel].elements);
  10616. END;
  10617. (* enter string in scope *)
  10618. INC(level);
  10619. END;
  10620. END;
  10621. Basic.SegmentedNameToString(this, name);
  10622. prev := this;
  10623. END;
  10624. END;
  10625. WHILE 0 <= olevel DO
  10626. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10627. IF olevel > 0 THEN
  10628. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10629. nextPatch := scopes[olevel-1].patchAdr+1;
  10630. IF module.system.addressType.sizeInBits = 64 THEN INC(nextPatch) END;
  10631. PatchSymbol(scopes[olevel-1].section,nextPatch, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10632. END;
  10633. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10634. DEC(olevel);
  10635. END;
  10636. level := 0;
  10637. WHILE (level < LEN(scopes)) DO
  10638. IF (scopes[level].section # NIL) & (scopes[level].arraySizePC # MIN(LONGINT)) THEN
  10639. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10640. END;
  10641. INC(level);
  10642. END;
  10643. END Export;
  10644. BEGIN
  10645. NEW(fingerPrinter);
  10646. NEW(poolMap, 64);
  10647. (* 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 *)
  10648. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10649. NEW(sectionArray, module.allSections.Length());
  10650. FOR i := 0 TO module.allSections.Length() - 1 DO
  10651. section := module.allSections.GetSection(i);
  10652. sectionArray[i] := section;
  10653. END;
  10654. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10655. Export(sectionArray^);
  10656. END ExportDesc;
  10657. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10658. VAR
  10659. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10660. BEGIN
  10661. Info(source, "exception table offsets array descriptor");
  10662. size := 0;
  10663. ArrayBlock(source,sizePC,"Modules.ExceptionTableEntry", FALSE);
  10664. Info(source, "exception table content");
  10665. FOR i := 0 TO module.allSections.Length() - 1 DO
  10666. p := module.allSections.GetSection(i);
  10667. IF p.type = Sections.CodeSection THEN
  10668. finallyPC := p(IntermediateCode.Section).finally;
  10669. IF finallyPC>=0 THEN
  10670. Symbol( source, p, 0,0);
  10671. Symbol( source, p, finallyPC, 0);
  10672. Symbol( source, p, finallyPC,0);
  10673. INC(size);
  10674. END;
  10675. END
  10676. END;
  10677. PatchArray(source,sizePC,size);
  10678. END ExceptionArray;
  10679. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10680. VAR i: LONGINT; ch: CHAR;
  10681. BEGIN
  10682. i := 0;
  10683. REPEAT
  10684. ch := name[i]; INC(i);
  10685. Char( section, ch);
  10686. UNTIL ch = 0X;
  10687. WHILE i < 32 DO
  10688. Char( section, 0X); INC(i);
  10689. END;
  10690. END Name;
  10691. PROCEDURE References(section: IntermediateCode.Section);
  10692. CONST
  10693. sfTypeNone = 0X;
  10694. sfTypeCHAR = 01X;
  10695. sfTypeCHAR8 = 02X;
  10696. sfTypeCHAR16 = 03X;
  10697. sfTypeCHAR32 = 04X;
  10698. sfTypeRANGE = 05X;
  10699. sfTypeSHORTINT = 06X;
  10700. sfTypeINTEGER = 07X;
  10701. sfTypeLONGINT = 08X;
  10702. sfTypeHUGEINT = 09X;
  10703. sfTypeWORD = 0AX;
  10704. sfTypeLONGWORD = 0BX;
  10705. sfTypeSIGNED8 = 0CX;
  10706. sfTypeSIGNED16 = 0DX;
  10707. sfTypeSIGNED32 = 0EX;
  10708. sfTypeSIGNED64 = 0FX;
  10709. sfTypeUNSIGNED8 = 10X;
  10710. sfTypeUNSIGNED16 = 11X;
  10711. sfTypeUNSIGNED32 = 12X;
  10712. sfTypeUNSIGNED64 = 13X;
  10713. sfTypeREAL = 14X;
  10714. sfTypeLONGREAL = 15X;
  10715. sfTypeCOMPLEX = 16X;
  10716. sfTypeLONGCOMPLEX = 17X;
  10717. sfTypeBOOLEAN = 18X;
  10718. sfTypeSET = 19X;
  10719. sfTypeANY = 1AX;
  10720. sfTypeOBJECT = 1BX;
  10721. sfTypeBYTE = 1CX;
  10722. sfTypeADDRESS = 1DX;
  10723. sfTypeSIZE = 1EX;
  10724. sfTypeIndirect = 1FX;
  10725. sfTypeRecord = 20X;
  10726. sfTypePointerToRecord = 21X;
  10727. sfTypePointerToArray = 22X;
  10728. sfTypeOpenArray = 23X;
  10729. sfTypeStaticArray = 24X;
  10730. sfTypeDynamicArray = 25X;
  10731. sfTypeMathStaticArray = 26X;
  10732. sfTypeMathOpenArray = 27X;
  10733. sfTypeMathTensor = 28X;
  10734. sfTypeDelegate = 29X;
  10735. sfTypeENUM = 2AX;
  10736. sfTypeCELL = 2BX;
  10737. sfTypePORT = 2CX;
  10738. sfIN = 0X;
  10739. sfOUT = 1X;
  10740. flagDelegate = 0;
  10741. flagConstructor = 1;
  10742. (* variable / parameter addressing modes *)
  10743. sfAbsolute = 0X; (* global vars *)
  10744. sfRelative = 1X; (* variables, value parameters *)
  10745. sfIndirect = 2X; (* var parameters *)
  10746. sfScopeBegin = 0F0X;
  10747. sfScopeEnd = 0F1X;
  10748. sfProcedure = 0F2X;
  10749. sfVariable = 0F3X;
  10750. sfTypeDeclaration = 0F4X;
  10751. sfModule = 0FFX;
  10752. RefInfo = TRUE;
  10753. VAR
  10754. s: Sections.Section; sizePC, i, startPC, lastOffset: LONGINT;
  10755. indirectTypes: Basic.HashTable;
  10756. PROCEDURE CurrentIndex(): SIZE;
  10757. VAR i: LONGINT;
  10758. BEGIN
  10759. FOR i := startPC TO section.pc -1 DO
  10760. ASSERT (section.instructions[i].opcode = IntermediateCode.data);
  10761. INC(lastOffset, ToMemoryUnits(module.system, section.instructions[i].op1.type.sizeInBits));
  10762. END;
  10763. startPC := section.pc;
  10764. RETURN lastOffset;
  10765. END CurrentIndex;
  10766. (*
  10767. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  10768. Module = sfModule prevSymbol:SIZE name:String Scope.
  10769. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  10770. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfIndirect offset:SIZE | sfRelative offset:SIZE) Type.
  10771. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  10772. Type =
  10773. sfTypePointerToRecord
  10774. | sfTypePointerToArray Type
  10775. | sfTypeOpenArray Type
  10776. | sfTypeDynamicArray Type
  10777. | sfTypeStaticArray length:SIZE Type
  10778. | sfTypeMathOpenArray Type
  10779. | sfTypeMathStaticArray length:SIZE Type
  10780. | sfTypeMathTensor Type
  10781. | sfTypeRecord tdAdr:ADDRESS
  10782. | sfTypeDelegate {Parameter} return:Type
  10783. | sfTypePort (sfIN | sfOUT)
  10784. | sfTypeBOOLEAN
  10785. | sfTypeCHAR | sfTypeCHAR8 | sfTypeCHAR16 | sfTypeCHAR32
  10786. | sfTypeSHORTINT | sfTypeINTEGER | sfTypeLONGINT | sfTypeHUGEINT
  10787. | sfTypeSIGNED8 | sfTypeSIGNED16 | sfTypeSIGNED32 | sfTypeSIGNED64
  10788. | sfTypeUNSIGNED8 | sfTypeUNSIGNED16 | sfTypeUNSIGNED32 | sfTypeUNSIGNED64
  10789. | sfTypeWORD | sfTypeLONGWORD
  10790. | sfTypeREAL | sfTypeLONGREAL
  10791. | sfTypeCOMPLEX | sfTypeLONGCOMPLEX
  10792. | sfTypeSET | sfTypeANY | sfTypeOBJECT | sfTypeBYTE | sfTypeADDRESS | sfTypeSIZE
  10793. | sfTypeIndirect offset:SIZE.
  10794. *)
  10795. PROCEDURE Indirect(type: SyntaxTree.Type): BOOLEAN;
  10796. VAR offset: SIZE;
  10797. BEGIN
  10798. IF indirectTypes.Has(type) THEN
  10799. offset := indirectTypes.GetInt(type);
  10800. Char(section, sfTypeIndirect);
  10801. Size(section, offset);
  10802. RETURN TRUE;
  10803. ELSE
  10804. indirectTypes.PutInt(type, CurrentIndex());
  10805. RETURN FALSE;
  10806. END;
  10807. END Indirect;
  10808. PROCEDURE NType(type: SyntaxTree.Type);
  10809. VAR size: SIZE; td: SyntaxTree.TypeDeclaration; tir: Sections.Section;
  10810. segmentedName: Basic.SegmentedName; offset: SIZE; parameter: SyntaxTree.Parameter;
  10811. BEGIN
  10812. IF type = NIL THEN
  10813. Char(section, sfTypeNone)
  10814. ELSE
  10815. type := type.resolved;
  10816. size := type.sizeInBits;
  10817. WITH type:SyntaxTree.PointerType DO
  10818. IF type.pointerBase.resolved IS SyntaxTree.RecordType THEN
  10819. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10820. Char(section, sfTypePointerToRecord);
  10821. (*! do we ever need the pointer base? NType(type.pointerBase);*)
  10822. ELSE
  10823. IF RefInfo THEN Info(section,"PointerToArray") END;
  10824. Char(section, sfTypePointerToArray);
  10825. NType(type.pointerBase);
  10826. END;
  10827. | type: SyntaxTree.ArrayType DO
  10828. IF ~Indirect(type) THEN
  10829. IF type.form = SyntaxTree.Open THEN
  10830. IF RefInfo THEN Info(section,"OpenArray") END;
  10831. Char(section, sfTypeOpenArray);
  10832. ELSIF type.form = SyntaxTree.SemiDynamic THEN
  10833. IF RefInfo THEN Info(section,"DynamicArray") END;
  10834. Char(section, sfTypeDynamicArray);
  10835. ELSIF type.form = SyntaxTree.Static THEN
  10836. IF RefInfo THEN Info(section,"StaticArray") END;
  10837. Char(section, sfTypeStaticArray);
  10838. Size(section, type.staticLength);
  10839. ELSE
  10840. HALT(100);
  10841. END;
  10842. NType(type.arrayBase);
  10843. END;
  10844. | type: SyntaxTree.MathArrayType DO
  10845. IF ~Indirect(type) THEN
  10846. IF type.form = SyntaxTree.Open THEN
  10847. IF RefInfo THEN Info(section,"MathOpenArray") END;
  10848. Char(section, sfTypeMathOpenArray);
  10849. ELSIF type.form = SyntaxTree.Static THEN
  10850. IF RefInfo THEN Info(section,"MathStaticArray") END;
  10851. Char(section, sfTypeMathStaticArray);
  10852. Size(section, type.staticLength);
  10853. ELSIF type.form = SyntaxTree.Tensor THEN
  10854. IF RefInfo THEN Info(section,"MathTensor") END;
  10855. Char(section, sfTypeMathTensor);
  10856. ELSE
  10857. HALT(100);
  10858. END;
  10859. NType(type.arrayBase);
  10860. END;
  10861. | type: SyntaxTree.RecordType DO
  10862. IF ~Indirect(type) THEN
  10863. IF type.pointerType # NIL (* OBJECT *) THEN
  10864. IF RefInfo THEN Info(section,"PointerToRecord") END;
  10865. Char(section, sfTypePointerToRecord)
  10866. ELSE
  10867. IF RefInfo THEN Info(section,"Record") END;
  10868. Char(section, sfTypeRecord);
  10869. td := type.typeDeclaration;
  10870. IF RefInfo THEN Info(section,"TD") END;
  10871. IF (td # NIL) THEN
  10872. Global.GetSymbolSegmentedName(td,segmentedName);
  10873. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10874. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10875. ELSE
  10876. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10877. END;
  10878. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10879. Symbol(section, tir, 0, offset);
  10880. ELSE
  10881. Address(section, 0);
  10882. END;
  10883. END;
  10884. END;
  10885. | type: SyntaxTree.CellType DO
  10886. IF ~Indirect(type) THEN
  10887. IF RefInfo THEN Info(section,"Record") END;
  10888. Char(section, sfTypeRecord);
  10889. td := type.typeDeclaration;
  10890. IF RefInfo THEN Info(section,"TD") END;
  10891. IF (td # NIL) THEN
  10892. Global.GetSymbolSegmentedName(td,segmentedName);
  10893. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10894. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10895. ELSE
  10896. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10897. END;
  10898. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  10899. Symbol(section, tir, 0, offset);
  10900. ELSE
  10901. Address(section, 0);
  10902. END;
  10903. END;
  10904. | type: SyntaxTree.PortType DO
  10905. Char(section, sfTypePORT);
  10906. IF type.direction = SyntaxTree.OutPort THEN
  10907. Char(section, sfOUT)
  10908. ELSE
  10909. Char(section, sfIN)
  10910. END;
  10911. | type: SyntaxTree.ProcedureType DO
  10912. IF ~Indirect(type) THEN
  10913. Char(section, sfTypeDelegate);
  10914. parameter := type.firstParameter;
  10915. WHILE(parameter # NIL) DO
  10916. NParameter(parameter, -1);
  10917. parameter := parameter.nextParameter;
  10918. END;
  10919. NType(type.returnType);
  10920. END;
  10921. | type:SyntaxTree.EnumerationType DO
  10922. Char(section, sfTypeENUM);
  10923. | type: SyntaxTree.BasicType DO
  10924. WITH type: SyntaxTree.BooleanType DO
  10925. IF RefInfo THEN Info(section,"Boolean") END;
  10926. Char(section, sfTypeBOOLEAN);
  10927. | type: SyntaxTree.CharacterType DO
  10928. IF type = module.system.characterType THEN
  10929. IF RefInfo THEN Info(section,"CHAR") END;
  10930. Char(section, sfTypeCHAR);
  10931. ELSIF (type = module.system.characterType8) OR (type.sizeInBits= 8) THEN
  10932. IF RefInfo THEN Info(section,"CHAR8") END;
  10933. Char(section, sfTypeCHAR8)
  10934. ELSIF (type = module.system.characterType16) OR (type.sizeInBits= 16) THEN
  10935. IF RefInfo THEN Info(section,"CHAR16") END;
  10936. Char(section, sfTypeCHAR16);
  10937. ELSIF (type = module.system.characterType32) OR (type.sizeInBits = 32) THEN
  10938. IF RefInfo THEN Info(section,"CHAR32") END;
  10939. Char(section, sfTypeCHAR32);
  10940. ELSE
  10941. HALT(100);
  10942. END;
  10943. |type: SyntaxTree.IntegerType DO
  10944. IF type(SyntaxTree.IntegerType).signed THEN
  10945. IF (type = module.system.shortintType) THEN
  10946. IF RefInfo THEN Info(section,"SHORTINT") END;
  10947. Char(section, sfTypeSHORTINT)
  10948. ELSIF (type = module.system.integerType) THEN
  10949. IF RefInfo THEN Info(section,"INTEGER") END;
  10950. Char(section, sfTypeINTEGER)
  10951. ELSIF (type = module.system.longintType) THEN
  10952. IF RefInfo THEN Info(section,"LONGINT") END;
  10953. Char(section, sfTypeLONGINT)
  10954. ELSIF (type = module.system.hugeintType) THEN
  10955. IF RefInfo THEN Info(section,"HUGEINT") END;
  10956. Char(section, sfTypeHUGEINT)
  10957. ELSIF (type = module.system.wordType) THEN
  10958. IF RefInfo THEN Info(section,"WORD") END;
  10959. Char(section, sfTypeWORD)
  10960. ELSIF (type = module.system.longWordType) THEN
  10961. IF RefInfo THEN Info(section,"LONGWORD") END;
  10962. Char(section, sfTypeLONGWORD);
  10963. ELSIF (type = Global.Integer8) OR (type.sizeInBits = 8 ) THEN
  10964. IF RefInfo THEN Info(section,"SIGNED8") END;
  10965. Char(section, sfTypeSIGNED8)
  10966. ELSIF (type = Global.Integer16) OR (type.sizeInBits = 16 ) THEN
  10967. IF RefInfo THEN Info(section,"SIGNED16") END;
  10968. Char(section, sfTypeSIGNED16)
  10969. ELSIF (type = Global.Integer32) OR (type.sizeInBits = 32 ) THEN
  10970. IF RefInfo THEN Info(section,"SIGNED32") END;
  10971. Char(section, sfTypeSIGNED32)
  10972. ELSIF (type = Global.Integer64) OR (type.sizeInBits = 64 ) THEN
  10973. IF RefInfo THEN Info(section,"SIGNED64") END;
  10974. Char(section, sfTypeSIGNED64)
  10975. ELSE
  10976. HALT(100);
  10977. END
  10978. ELSE (* unsigned *)
  10979. IF (type = Global.Unsigned8) OR (type.sizeInBits = 8 ) THEN
  10980. IF RefInfo THEN Info(section,"UNSIGNED8") END;
  10981. Char(section, sfTypeUNSIGNED8)
  10982. ELSIF (type = Global.Unsigned16) OR (type.sizeInBits = 16 ) THEN
  10983. IF RefInfo THEN Info(section,"UNSIGNED16") END;
  10984. Char(section, sfTypeUNSIGNED16)
  10985. ELSIF (type = Global.Unsigned32) OR (type.sizeInBits = 32 ) THEN
  10986. IF RefInfo THEN Info(section,"UNSIGNED32") END;
  10987. Char(section, sfTypeUNSIGNED32)
  10988. ELSIF (type = Global.Unsigned64) OR (type.sizeInBits = 64 ) THEN
  10989. IF RefInfo THEN Info(section,"UNSIGNED64") END;
  10990. Char(section, sfTypeUNSIGNED64)
  10991. ELSE
  10992. HALT(100)
  10993. END
  10994. END;
  10995. | type: SyntaxTree.FloatType DO
  10996. IF (type = module.system.realType) OR (type.sizeInBits = 32) THEN
  10997. IF RefInfo THEN Info(section,"REAL") END;
  10998. Char(section, sfTypeREAL);
  10999. ELSIF (type = module.system.longrealType) OR (type.sizeInBits = 64) THEN
  11000. IF RefInfo THEN Info(section,"LONGREAL") END;
  11001. Char(section, sfTypeLONGREAL);
  11002. ELSE
  11003. HALT(100);
  11004. END;
  11005. |type: SyntaxTree.ComplexType DO
  11006. IF (type = module.system.complexType) OR (type.sizeInBits = 64) THEN
  11007. IF RefInfo THEN Info(section,"COMPLEX") END;
  11008. Char(section, sfTypeCOMPLEX);
  11009. ELSIF (type = module.system.longcomplexType) OR (type.sizeInBits = 12) THEN
  11010. IF RefInfo THEN Info(section,"LONGCOMPLEX") END;
  11011. Char(section, sfTypeLONGCOMPLEX);
  11012. ELSE
  11013. HALT(100);
  11014. END;
  11015. |type:SyntaxTree.SetType DO
  11016. IF RefInfo THEN Info(section,"SET") END;
  11017. Char(section, sfTypeSET);
  11018. |type:SyntaxTree.AnyType DO
  11019. IF RefInfo THEN Info(section,"ANY") END;
  11020. Char(section, sfTypeANY);
  11021. |type:SyntaxTree.ObjectType DO
  11022. IF RefInfo THEN Info(section,"OBJECT") END;
  11023. Char(section, sfTypeOBJECT);
  11024. |type:SyntaxTree.ByteType DO
  11025. IF RefInfo THEN Info(section,"BYTE") END;
  11026. Char(section, sfTypeBYTE);
  11027. |type:SyntaxTree.RangeType DO
  11028. IF RefInfo THEN Info(section,"RANGE") END;
  11029. Char(section, sfTypeRANGE)
  11030. |type:SyntaxTree.AddressType DO
  11031. IF RefInfo THEN Info(section,"ADDRESS") END;
  11032. Char(section, sfTypeADDRESS)
  11033. |type:SyntaxTree.SizeType DO
  11034. IF RefInfo THEN Info(section,"SIZE") END;
  11035. Char(section, sfTypeSIZE)
  11036. ELSE
  11037. HALT(100)
  11038. END;
  11039. ELSE HALT(101);
  11040. END;
  11041. END;
  11042. END NType;
  11043. (*
  11044. Parameter = sfVariable prevSymbol:SIZE name:STRING (sfIndirec|sfRelative) offset:SIZE Type.
  11045. *)
  11046. PROCEDURE NParameter(parameter: SyntaxTree.Parameter; procOffset: LONGINT);
  11047. VAR pos: LONGINT; type: SyntaxTree.Type;
  11048. BEGIN
  11049. IF RefInfo THEN Info(section, "Parameter") END;
  11050. Char(section, sfVariable);
  11051. Size(section, procOffset);
  11052. String0(section, parameter.name);
  11053. type := parameter.type.resolved;
  11054. IF parameter.kind = SyntaxTree.VarParameter THEN
  11055. IF IsOpenArray(type) THEN Char(section, sfRelative)
  11056. ELSE Char(section, sfIndirect)
  11057. END;
  11058. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11059. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(type) THEN
  11060. Char(section, sfIndirect);
  11061. ELSE
  11062. Char(section, sfRelative);
  11063. END;
  11064. ELSE
  11065. Char(section, sfRelative);
  11066. END;
  11067. Size(section, ToMemoryUnits(module.system,parameter.offsetInBits));
  11068. NType(parameter.type);
  11069. END NParameter;
  11070. (*
  11071. Procedure = sfProcedure prevSymbol:SIZE name:STRING from:ADDRESS to:ADDRESS {Parameter} returnType:Type Scope.
  11072. *)
  11073. PROCEDURE NProcedure(procedure: SyntaxTree.Procedure; scopeOffset: LONGINT);
  11074. VAR s: Sections.Section; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter; pos: LONGINT;
  11075. name: Basic.SegmentedName; flags: SET;
  11076. BEGIN
  11077. IF RefInfo THEN Info(section, "Procedure") END;
  11078. pos := CurrentIndex();
  11079. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  11080. Char(section, sfProcedure);
  11081. Size(section, scopeOffset);
  11082. String0(section,procedure.name);
  11083. s := module.allSections.FindBySymbol(procedure);
  11084. Symbol(section,s,0,0); (* start *)
  11085. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  11086. flags := {};
  11087. IF procedureType.isDelegate THEN
  11088. INCL(flags, flagDelegate);
  11089. END;
  11090. IF procedure.isConstructor THEN
  11091. INCL(flags, flagConstructor);
  11092. END;
  11093. Set(section, flags);
  11094. Global.GetSymbolSegmentedName(procedure,name);
  11095. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  11096. IF RefInfo THEN Info(section, "Parameters") END;
  11097. parameter := procedureType.firstParameter;
  11098. WHILE(parameter # NIL) DO
  11099. NParameter(parameter, pos);
  11100. parameter := parameter.nextParameter;
  11101. END;
  11102. IF RefInfo THEN Info(section, "ReturnType") END;
  11103. NType(procedureType.returnType);
  11104. NScope(procedure.procedureScope, pos);
  11105. END NProcedure;
  11106. (*
  11107. Variable = sfVariable prevSymbol:SIZE name:STRING (sfAbsolute address:ADDRESS| sfRelative offset:SIZE) Type.
  11108. *)
  11109. PROCEDURE NVariable(variable: SyntaxTree.Variable; scopeOffset: LONGINT);
  11110. VAR s: Sections.Section; sn: Basic.SegmentedName; pos: LONGINT;
  11111. BEGIN
  11112. IF RefInfo THEN Info(section, "Variable") END;
  11113. pos := CurrentIndex();
  11114. Char(section, sfVariable);
  11115. Size(section, scopeOffset);
  11116. String0(section, variable.name);
  11117. IF (variable.scope # NIL) & (variable.scope IS SyntaxTree.ModuleScope) THEN
  11118. Char(section, sfAbsolute);
  11119. implementationVisitor.GetCodeSectionNameForSymbol(variable, sn);
  11120. NamedSymbol(section, sn,variable, 0,0);
  11121. ELSE
  11122. Char(section, sfRelative);
  11123. Size(section, ToMemoryUnits(module.system,variable.offsetInBits));
  11124. END;
  11125. NType(variable.type);
  11126. s := module.allSections.FindBySymbol(variable);
  11127. END NVariable;
  11128. (*
  11129. TypeDeclaration = sfTypeDeclaration prevSymbol:SIZE name:STRING td:ADDRESS Scope.
  11130. *)
  11131. PROCEDURE NTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration; scopeOffset: LONGINT);
  11132. VAR declared: SyntaxTree.Type; s: Sections.Section; offset: LONGINT; name: Basic.SegmentedName; pos: LONGINT;
  11133. BEGIN
  11134. IF typeDeclaration = NIL THEN RETURN END;
  11135. pos := CurrentIndex();
  11136. s := module.allSections.FindBySymbol(typeDeclaration);
  11137. IF s = NIL THEN RETURN END; (*! duplicate, what to do? *)
  11138. IF RefInfo THEN Info(section, "TypeDeclaration") END;
  11139. Char(section, sfTypeDeclaration);
  11140. Size(section, scopeOffset);
  11141. String0(section, typeDeclaration.name);
  11142. declared := typeDeclaration.declaredType.resolved;
  11143. IF (declared IS SyntaxTree.PointerType) THEN
  11144. declared := declared(SyntaxTree.PointerType).pointerBase.resolved;
  11145. END;
  11146. WITH declared: SyntaxTree.RecordType DO
  11147. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(declared.recordScope.numberMethods)*module.system.addressSize);
  11148. Symbol(section, s, 0, offset);
  11149. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11150. Basic.AppendToSegmentedName(name,".@Info");
  11151. s := module.allSections.FindByName(name);
  11152. IF s # NIL THEN (* does not work for coop *)
  11153. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11154. NScope(declared.recordScope, pos);
  11155. END;
  11156. |declared: SyntaxTree.CellType DO
  11157. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(0)*module.system.addressSize);
  11158. Symbol(section, s, 0, offset);
  11159. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11160. Basic.AppendToSegmentedName(name,".@Info");
  11161. s := module.allSections.FindByName(name);
  11162. IF s # NIL THEN
  11163. PatchSize(s(IntermediateCode.Section), patchInfoPC, pos);
  11164. NScope(declared.cellScope, pos);
  11165. END;
  11166. ELSE
  11167. Address(section, 0);
  11168. END;
  11169. END NTypeDeclaration;
  11170. PROCEDURE NModule(module: SyntaxTree.Module; prevSymbol: LONGINT);
  11171. VAR pos: LONGINT;
  11172. BEGIN
  11173. pos := CurrentIndex();
  11174. Char(section,sfModule);
  11175. Size(section, prevSymbol);
  11176. String0(section, module.name);
  11177. NScope(module.moduleScope, pos);
  11178. END NModule;
  11179. (*
  11180. Scope = sfScopeBegin {Variable} {Procedure} {TypeDeclaration} sfScopeEnd.
  11181. *)
  11182. PROCEDURE NScope(scope: SyntaxTree.Scope; prevSymbol: LONGINT);
  11183. VAR bodyProcedure, procedure: SyntaxTree.Procedure; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration; pos: LONGINT;
  11184. BEGIN
  11185. IF scope = NIL THEN RETURN END;
  11186. IF RefInfo THEN Info(section, "Scope") END;
  11187. Char(section, sfScopeBegin);
  11188. variable := scope.firstVariable;
  11189. WHILE (variable # NIL) DO
  11190. NVariable(variable, prevSymbol);
  11191. variable := variable.nextVariable;
  11192. END;
  11193. WITH scope: SyntaxTree.ModuleScope DO
  11194. bodyProcedure := scope.bodyProcedure;
  11195. |scope: SyntaxTree.RecordScope DO
  11196. bodyProcedure := scope.bodyProcedure;
  11197. ELSE
  11198. bodyProcedure := NIL;
  11199. END;
  11200. IF bodyProcedure # NIL THEN
  11201. NProcedure(bodyProcedure, prevSymbol)
  11202. END;
  11203. procedure := scope.firstProcedure;
  11204. WHILE procedure # NIL DO
  11205. IF (procedure # bodyProcedure) & ~procedure.isInline THEN NProcedure(procedure, prevSymbol) END;
  11206. procedure := procedure.nextProcedure;
  11207. END;
  11208. typeDeclaration := scope.firstTypeDeclaration;
  11209. WHILE typeDeclaration # NIL DO
  11210. NTypeDeclaration(typeDeclaration, prevSymbol);
  11211. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  11212. END;
  11213. Char(section, sfScopeEnd); (* scope ends *)
  11214. END NScope;
  11215. BEGIN
  11216. NEW(indirectTypes, 32);
  11217. ArrayBlock(section,sizePC,"", FALSE);
  11218. IF ~implementationVisitor.backend.cooperative THEN
  11219. startPC := section.pc;
  11220. NModule(module.module, -1);
  11221. PatchArray(section,sizePC,CurrentIndex());
  11222. END;
  11223. END References;
  11224. (*
  11225. Command* = RECORD
  11226. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  11227. name*: Name; (* name of the procedure *)
  11228. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  11229. entryAdr* : ADDRESS; (* entry address of procedure *)
  11230. END;
  11231. *)
  11232. PROCEDURE CommandArray(source: IntermediateCode.Section);
  11233. VAR
  11234. p: Sections.Section; sizePC, numberCommands: LONGINT;
  11235. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  11236. name: SyntaxTree.IdentifierString; numberParameters, i: LONGINT;
  11237. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  11238. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  11239. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  11240. BEGIN
  11241. RETURN
  11242. (type = NIL) OR
  11243. (type.resolved IS SyntaxTree.RecordType) OR
  11244. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  11245. (type.resolved IS SyntaxTree.AnyType);
  11246. END TypeAllowed;
  11247. BEGIN
  11248. numberParameters := procedureType.numberParameters;
  11249. RETURN
  11250. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  11251. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  11252. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  11253. END GetProcedureAllowed;
  11254. PROCEDURE WriteType(type : SyntaxTree.Type);
  11255. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  11256. name: Basic.SegmentedName; offset: LONGINT;
  11257. BEGIN
  11258. IF type = NIL THEN
  11259. Address(source,0);
  11260. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  11261. Address(source,1);
  11262. ELSE
  11263. type := type.resolved;
  11264. IF type IS SyntaxTree.PointerType THEN
  11265. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  11266. END;
  11267. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  11268. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  11269. name[0] := typeDeclaration.name; name[1] := -1;
  11270. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  11271. ASSERT(section # NIL);
  11272. ELSE
  11273. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11274. (* TODO *)
  11275. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  11276. END;
  11277. IF implementationVisitor.backend.cooperative THEN
  11278. offset := 0;
  11279. ELSE
  11280. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  11281. END;
  11282. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  11283. END;
  11284. END WriteType;
  11285. BEGIN
  11286. Info(source, "command array descriptor");
  11287. ArrayBlock(source,sizePC,"Modules.Command", FALSE);
  11288. numberCommands := 0;
  11289. Info(source, "command array content");
  11290. FOR i := 0 TO module.allSections.Length() - 1 DO
  11291. p := module.allSections.GetSection(i);
  11292. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  11293. procedure := p.symbol(SyntaxTree.Procedure);
  11294. procedureType := procedure.type(SyntaxTree.ProcedureType);
  11295. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  11296. procedure.GetName(name);
  11297. Name(source,name);
  11298. numberParameters := procedureType.numberParameters;
  11299. (* offset of type of first parameter *)
  11300. IF (numberParameters = 0 ) THEN WriteType(NIL)
  11301. ELSE WriteType(procedureType.firstParameter.type)
  11302. END;
  11303. (* offset of type of return parameter *)
  11304. WriteType(procedureType.returnType);
  11305. (* command name *)
  11306. (* command code offset *)
  11307. Symbol(source,p,0,0);
  11308. INC(numberCommands);
  11309. IF Trace THEN
  11310. D.Ln;
  11311. END;
  11312. END;
  11313. END
  11314. END;
  11315. PatchArray(source,sizePC,numberCommands);
  11316. END CommandArray;
  11317. (* to prevent from double import of different module aliases *)
  11318. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  11319. VAR i: SyntaxTree.Import;
  11320. BEGIN
  11321. i := module.module.moduleScope.firstImport;
  11322. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  11323. i := i.nextImport;
  11324. END;
  11325. RETURN i = import
  11326. END IsFirstDirectOccurence;
  11327. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  11328. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  11329. BEGIN
  11330. (* strictly speaking this needs to be a pointer array but by the construction of module loading, this references are not required *)
  11331. ArrayBlock(source,pc,"", FALSE);
  11332. Info(source, "import module array data");
  11333. IF implementationVisitor.backend.cooperative THEN
  11334. offset := 0;
  11335. ELSE
  11336. IF module.system.addressType.sizeInBits = 64 THEN
  11337. offset := ToMemoryUnits(module.system, 18* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11338. ELSE
  11339. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  11340. END;
  11341. END;
  11342. import := module.module.moduleScope.firstImport;
  11343. numberImports := 0;
  11344. WHILE import # NIL DO
  11345. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  11346. Global.GetModuleSegmentedName(import.module,name);
  11347. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  11348. NamedSymbol(source, name, NIL, 0, offset);
  11349. INC(numberImports);
  11350. END;
  11351. import := import.nextImport
  11352. END;
  11353. PatchArray(source,pc,numberImports);
  11354. END ImportsArray;
  11355. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  11356. VAR
  11357. p: Sections.Section; sizePC, size, i: LONGINT;
  11358. BEGIN
  11359. Info(source, "Type info section");
  11360. size := 0;
  11361. ArrayBlock(source,sizePC,"Modules.TypeDesc", FALSE);
  11362. FOR i := 0 TO module.allSections.Length() - 1 DO
  11363. p := module.allSections.GetSection(i);
  11364. WITH p: IntermediateCode.Section DO
  11365. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  11366. Symbol(source,p,0,0);
  11367. INC(size);
  11368. END;
  11369. END
  11370. END;
  11371. PatchArray(source,sizePC,size);
  11372. END TypeInfoSection;
  11373. (*
  11374. ProcTableEntry* = RECORD
  11375. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  11376. noPtr*: LONGINT;
  11377. END;
  11378. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  11379. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  11380. *)
  11381. PROCEDURE ProcedureDescriptor(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11382. VAR
  11383. numberPointers: SIZE;
  11384. procedure: SyntaxTree.Procedure;
  11385. BEGIN
  11386. Info(section,"pcFrom");
  11387. Symbol(section,procedureSection,0,0);
  11388. Info(section,"pcTo");
  11389. Symbol(section, procedureSection, procedureSection.pc, 0);
  11390. Info(section,"pointer to offsets array");
  11391. Symbol(section, section,section.pc+1,0);
  11392. Info(section,"offsets array");
  11393. procedure := procedureSection.symbol(SyntaxTree.Procedure);
  11394. PointerArray(section, procedure.procedureScope, numberPointers);
  11395. END ProcedureDescriptor;
  11396. PROCEDURE ProcedureDescriptorPointer(section: IntermediateCode.Section; procedureSection: IntermediateCode.Section);
  11397. VAR dest: IntermediateCode.Section; name: Basic.SegmentedName; offset: LONGINT;
  11398. BEGIN
  11399. name := procedureSection.name;
  11400. Basic.SuffixSegmentedName(name, Basic.MakeString("@Descriptor"));
  11401. dest := NamedBlock("Heaps","SystemBlock",name,offset);
  11402. ProcedureDescriptor(dest, procedureSection);
  11403. Symbol(section, dest, offset, 0);
  11404. END ProcedureDescriptorPointer;
  11405. PROCEDURE ProcedureDescriptorArray(section: IntermediateCode.Section; VAR numberProcs: LONGINT);
  11406. VAR sizePC, i: LONGINT; destination: Sections.Section;
  11407. BEGIN
  11408. ArrayBlock(section, sizePC,"Modules.ProcedureDescPointer",FALSE);
  11409. numberProcs := 0;
  11410. FOR i := 0 TO module.allSections.Length() - 1 DO
  11411. destination := module.allSections.GetSection(i);
  11412. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  11413. ProcedureDescriptorPointer(section, destination(IntermediateCode.Section));
  11414. INC(numberProcs);
  11415. END
  11416. END;
  11417. PatchArray(section, sizePC, numberProcs);
  11418. END ProcedureDescriptorArray;
  11419. (*
  11420. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  11421. VAR
  11422. next*: Module; (** once a module is published, all fields are read-only *)
  11423. name*: Name;
  11424. init, published: BOOLEAN;
  11425. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  11426. sb*: ADDRESS; <- should be zero as the static base in generic object file is indeed 0 !
  11427. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  11428. command*: POINTER TO ARRAY OF Command;
  11429. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  11430. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  11431. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  11432. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  11433. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  11434. data*, code*: Bytes;
  11435. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  11436. export*: ExportDesc;
  11437. term*: TerminationHandler;
  11438. exTable*: ExceptionTable;
  11439. noProcs*: LONGINT;
  11440. firstProc*: ADDRESS; <- done by loader
  11441. maxPtrs*: LONGINT;
  11442. crc*: LONGINT;
  11443. *)
  11444. PROCEDURE BasePointer (section: IntermediateCode.Section);
  11445. BEGIN
  11446. Info(section, "cycle");
  11447. Size(section,0);
  11448. Info(section, "references");
  11449. Size(section,0);
  11450. Info(section, "nextMarked");
  11451. Address(section,0);
  11452. Info(section, "nextWatched");
  11453. Address(section,0);
  11454. END BasePointer;
  11455. PROCEDURE BaseObject (section: IntermediateCode.Section);
  11456. BEGIN
  11457. BasePointer(section);
  11458. Info(section, "action");
  11459. Address(section,0);
  11460. Info(section, "monitor");
  11461. Address(section,0);
  11462. END BaseObject;
  11463. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  11464. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  11465. pooledName: Basic.SegmentedName;
  11466. symbol: SyntaxTree.Symbol;
  11467. BEGIN
  11468. Global.GetModuleName(module.module,name);
  11469. Strings.Append(name,".@Module.@Descriptor");
  11470. Basic.ToSegmentedName(name, pooledName);
  11471. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11472. Symbol(section,descriptorSection,0,0);
  11473. Info(descriptorSection, "descriptor");
  11474. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11475. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  11476. Address(descriptorSection,0);
  11477. Global.GetModuleName(module.module,name);
  11478. Strings.Append(name,".@Trace");
  11479. Basic.ToSegmentedName(name, pooledName);
  11480. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11481. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  11482. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  11483. END ModuleDescriptor;
  11484. PROCEDURE ModuleSection(): IntermediateCode.Section;
  11485. VAR name: ARRAY 128 OF CHAR;
  11486. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  11487. symbol: SyntaxTree.Symbol;
  11488. BEGIN
  11489. Global.GetModuleName(module.module,name);
  11490. Strings.Append(name,".@Module");
  11491. Basic.ToSegmentedName(name, pooledName);
  11492. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,declarationVisitor.dump);
  11493. moduleSection.SetExported(TRUE);
  11494. IF moduleSection.pc = 0 THEN
  11495. IF implementationVisitor.backend.cooperative THEN
  11496. Info(moduleSection, "descriptor");
  11497. ModuleDescriptor(moduleSection);
  11498. BaseObject(moduleSection);
  11499. implementationVisitor.CreateTraceModuleMethod(module.module);
  11500. ELSE
  11501. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  11502. Info(moduleSection, "HeapBlock");
  11503. Symbol(moduleSection,moduleSection,2,0);
  11504. Info(moduleSection, "TypeDescriptor");
  11505. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  11506. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  11507. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  11508. END;
  11509. END;
  11510. RETURN moduleSection;
  11511. END ModuleSection;
  11512. PROCEDURE NewModuleInfo();
  11513. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11514. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11515. sectionName: Basic.SectionName;
  11516. CONST MPO=-40000000H;
  11517. BEGIN
  11518. (*
  11519. TypeDesc* = POINTER TO RECORD
  11520. descSize: LONGINT;
  11521. sentinel: LONGINT; (* = MPO-4 *)
  11522. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11523. flags*: SET;
  11524. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11525. name*: Name;
  11526. END;
  11527. *)
  11528. Global.GetSymbolSegmentedName(module.module,name);
  11529. Basic.AppendToSegmentedName(name,".@Info");
  11530. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11531. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11532. Address(source,MPO-4);
  11533. Info(source, "type tag pointer");
  11534. Address( source,0);
  11535. Info(source, "type flags");
  11536. flags := {};
  11537. Set( source, flags);
  11538. Info(source, "pointer to module");
  11539. moduleSection := ModuleSection();
  11540. Symbol( source, moduleSection, moduleSection.pc,0);
  11541. Info(source, "type name");
  11542. i := 0;
  11543. sectionName := "@Self";
  11544. (*
  11545. Global.GetSymbolSegmentedName(td,name);
  11546. Basic.SegmentedNameToString(name, sectionName);
  11547. *)
  11548. Name(source,sectionName);
  11549. source.SetReferenced(FALSE);
  11550. patchInfoPC := source.pc;
  11551. Size(source, 0);
  11552. END NewModuleInfo;
  11553. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  11554. VAR
  11555. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  11556. typeInfoSection, procTableSection, referenceSection : IntermediateCode.Section;
  11557. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  11558. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, maxPointers, numberProcs,temp,
  11559. referenceSectionOffset : LONGINT;
  11560. name: Basic.SegmentedName; offset: LONGINT;
  11561. BEGIN
  11562. NewModuleInfo();
  11563. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  11564. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  11565. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  11566. ImportsArray(importSection);
  11567. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  11568. CommandArray(commandsSection);
  11569. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  11570. ExceptionArray(exceptionSection);
  11571. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  11572. AddPointer(typeInfoSection, typeInfoSectionOffset);
  11573. TypeInfoSection(typeInfoSection);
  11574. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  11575. References(referenceSection);
  11576. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  11577. IF ~implementationVisitor.backend.cooperative THEN
  11578. ProcedureDescriptorArray(procTableSection, numberProcs);
  11579. name := "Heaps.AnyPtr";
  11580. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  11581. (* set base pointer *)
  11582. NamedSymbolAt(procTableSection, procTableSectionOffset -1 , name, NIL, 0, offset);
  11583. END;
  11584. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  11585. ArrayBlock(emptyArraySection,temp,"", FALSE);
  11586. moduleSection := ModuleSection();
  11587. Info(moduleSection, "nextRoot*: RootObject");
  11588. Address(moduleSection,0);
  11589. Info(moduleSection, "next*: Module");
  11590. Address(moduleSection,0);
  11591. Info(moduleSection, "name*: Name");
  11592. Name(moduleSection,moduleName);
  11593. Info(moduleSection, "init, published: BOOLEAN");
  11594. Boolean(moduleSection,FALSE);
  11595. Boolean(moduleSection,FALSE);
  11596. Info(moduleSection,"filler"); (*! introduce alignment! *)
  11597. Boolean(moduleSection,FALSE);
  11598. Boolean(moduleSection,FALSE);
  11599. Info(moduleSection, "refcnt*: LONGINT");
  11600. Longint(moduleSection,0);
  11601. Info(moduleSection, "sb*: ADDRESS");
  11602. Address(moduleSection,0);
  11603. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  11604. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11605. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  11606. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  11607. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  11608. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  11609. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  11610. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  11611. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  11612. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  11613. Info(moduleSection, "procTable*: ProcTable");
  11614. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  11615. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  11616. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11617. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11618. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  11619. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  11620. Info(moduleSection, "export*: ExportDesc");
  11621. ExportDesc(moduleSection);
  11622. Info(moduleSection, "term*: TerminationHandler");
  11623. Address(moduleSection,0);
  11624. Info(moduleSection, "exTable*: ExceptionTable");
  11625. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  11626. IF module.system.addressType.sizeInBits = 64 THEN Longint(moduleSection, 0); END; (* padding -> alignment *)
  11627. Info(moduleSection,"internal: POINTER TO ARRAY OF Pointer");
  11628. Symbol(moduleSection, modulePointerSection, modulePointerSectionOffset, 0);
  11629. Info(moduleSection, "crc*: LONGINT");
  11630. Longint(moduleSection, 0); (*! must be implemented *)
  11631. Info(moduleSection, "body*: ADDRESS");
  11632. Symbol(moduleSection, bodyProc, 0,0);
  11633. IF implementationVisitor.backend.cooperative THEN
  11634. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  11635. Info(moduleSection, "monitor.owner");
  11636. Address(moduleSection,0);
  11637. Info(moduleSection, "monitor.nestingLevel");
  11638. Address(moduleSection,0);
  11639. Info(moduleSection, "monitor.blockedQueue");
  11640. Address(moduleSection,0); Address(moduleSection,0);
  11641. Info(moduleSection, "monitor.waitingQueue");
  11642. Address(moduleSection,0); Address(moduleSection,0);
  11643. Info(moduleSection, "monitor.waitingSentinel");
  11644. Address(moduleSection,0);
  11645. END;
  11646. END Module;
  11647. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  11648. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section; parameter: SyntaxTree.Parameter; parametersSize: SIZE;
  11649. BEGIN
  11650. ArrayBlock(source,pc,"",FALSE);
  11651. Info(source, "pointer offsets array data");
  11652. IF scope IS SyntaxTree.RecordScope THEN
  11653. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  11654. ELSIF scope IS SyntaxTree.CellScope THEN
  11655. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  11656. ELSIF scope IS SyntaxTree.ModuleScope THEN
  11657. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  11658. WHILE variable # NIL DO
  11659. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11660. symbol := module.allSections.FindBySymbol(variable);
  11661. ASSERT(symbol # NIL);
  11662. Pointers(0,symbol, source,variable.type,numberPointers);
  11663. END;
  11664. variable := variable.nextVariable;
  11665. END;
  11666. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  11667. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11668. WHILE parameter # NIL DO
  11669. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11670. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, source, parameter.type, numberPointers);
  11671. END;
  11672. parameter := parameter.nextParameter;
  11673. END;
  11674. IF scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).isDelegate THEN
  11675. parametersSize := ProcedureParametersSize(module.system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  11676. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11677. IF implementationVisitor.backend.preciseGC THEN
  11678. INC(parametersSize,ToMemoryUnits(module.system,module.system.addressSize));
  11679. END;
  11680. Symbol(source, NIL, 0, parametersSize); INC(numberPointers);
  11681. END;
  11682. variable := scope(SyntaxTree.ProcedureScope).firstVariable;
  11683. WHILE(variable # NIL) DO
  11684. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  11685. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, source, variable.type, numberPointers);
  11686. END;
  11687. variable := variable.nextVariable
  11688. END;
  11689. END;
  11690. PatchArray(source,pc,numberPointers);
  11691. END PointerArray;
  11692. PROCEDURE SymbolSection(symbol: SyntaxTree.Symbol; CONST suffix: ARRAY OF CHAR; VAR pc: LONGINT): IntermediateCode.Section;
  11693. VAR
  11694. name: Basic.SegmentedName;
  11695. section: IntermediateCode.Section;
  11696. BEGIN
  11697. ASSERT(implementationVisitor.newObjectFile);
  11698. Global.GetSymbolSegmentedName(symbol,name);
  11699. Basic.AppendToSegmentedName(name,suffix);
  11700. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,declarationVisitor.dump);
  11701. HeapBlock("Heaps","SystemBlockDesc", section, 2);
  11702. Info(section, "HeapBlock");
  11703. Address(section,0); (* empty such that GC does not go on traversing *)
  11704. Info(section, suffix);
  11705. Address(section,0);
  11706. pc := section.pc;
  11707. RETURN section;
  11708. END SymbolSection;
  11709. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  11710. VAR recordType: SyntaxTree.RecordType;
  11711. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  11712. section: Sections.Section; cellType: SyntaxTree.CellType;
  11713. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  11714. VAR name: Basic.SegmentedName;source: IntermediateCode.Section;
  11715. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  11716. sectionName: Basic.SectionName;
  11717. CONST MPO=-40000000H;
  11718. BEGIN
  11719. (*
  11720. TypeDesc* = POINTER TO RECORD
  11721. descSize: LONGINT;
  11722. sentinel: LONGINT; (* = MPO-4 *)
  11723. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  11724. flags*: SET;
  11725. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  11726. name*: Name;
  11727. END;
  11728. *)
  11729. (* source := module.sections.FindByName(...) *)
  11730. Global.GetSymbolSegmentedName(td,name);
  11731. Basic.AppendToSegmentedName(name,".@Info");
  11732. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11733. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  11734. Address(source,MPO-4);
  11735. Info(source, "type tag pointer");
  11736. Symbol( source, tag, offset, 0);
  11737. Info(source, "type flags");
  11738. flags := {};
  11739. IF isProtected THEN INCL(flags,31) END;
  11740. Set( source, flags);
  11741. Info(source, "pointer to module");
  11742. moduleSection := ModuleSection();
  11743. Symbol( source, moduleSection, moduleSection.pc,0);
  11744. Info(source, "type name");
  11745. i := 0;
  11746. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  11747. (*
  11748. Global.GetSymbolSegmentedName(td,name);
  11749. Basic.SegmentedNameToString(name, sectionName);
  11750. *)
  11751. Name(source,sectionName);
  11752. source.SetReferenced(FALSE);
  11753. Size(source, 0);
  11754. RETURN source;
  11755. END NewTypeDescriptorInfo;
  11756. PROCEDURE NewTypeDescriptor;
  11757. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11758. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11759. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11760. numberPointers: LONGINT; padding, i: LONGINT;
  11761. CONST MPO=-40000000H;
  11762. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11763. VAR i: LONGINT;
  11764. PROCEDURE Td(record: SyntaxTree.RecordType);
  11765. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11766. BEGIN
  11767. IF record # NIL THEN
  11768. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11769. baseTD := record.typeDeclaration;
  11770. Global.GetSymbolSegmentedName(baseTD,name);
  11771. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11772. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11773. ELSE
  11774. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11775. END;
  11776. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11777. Symbol(source, tir, 0, offset);
  11778. IF reverse THEN Td(record.GetBaseRecord()) END;
  11779. END;
  11780. END Td;
  11781. BEGIN
  11782. Info(source, "tag table");
  11783. baseRecord := recordType;
  11784. i := 0;
  11785. WHILE baseRecord # NIL DO
  11786. INC(i);
  11787. baseRecord := baseRecord.GetBaseRecord();
  11788. END;
  11789. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11790. IF ~reverse THEN Td(recordType) END;
  11791. WHILE i < size DO
  11792. Address(source,0);
  11793. INC(i);
  11794. END;
  11795. IF reverse THEN Td(recordType) END;
  11796. END TdTable;
  11797. PROCEDURE MethodTable(reverse: BOOLEAN);
  11798. VAR i,methods: LONGINT;
  11799. BEGIN
  11800. Info(source, "method table");
  11801. IF recordType # NIL THEN
  11802. methods := recordType.recordScope.numberMethods;
  11803. IF reverse THEN
  11804. FOR i := methods-1 TO 0 BY -1 DO
  11805. procedure := recordType.recordScope.FindMethod(i);
  11806. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11807. NamedSymbol(source, name,procedure, 0,0);
  11808. END;
  11809. ELSE
  11810. FOR i := 0 TO methods-1 DO
  11811. procedure := recordType.recordScope.FindMethod(i);
  11812. implementationVisitor.GetCodeSectionNameForSymbol(procedure, name);
  11813. NamedSymbol(source, name,procedure, 0,0);
  11814. END;
  11815. END;
  11816. END;
  11817. END MethodTable;
  11818. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11819. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11820. BEGIN
  11821. Info(source, "method table");
  11822. baseRecord := recordType;
  11823. WHILE baseRecord.baseType # NIL DO
  11824. baseRecord := baseRecord.GetBaseRecord ();
  11825. END;
  11826. GetRecordTypeName (baseRecord, name);
  11827. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11828. IF name = stackFrame THEN
  11829. start := 0;
  11830. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11831. baseRecord := recordType;
  11832. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11833. baseRecord := baseRecord.GetBaseRecord ();
  11834. END;
  11835. IF baseRecord # NIL THEN
  11836. GetRecordTypeName (baseRecord, name);
  11837. IF pointer & ~baseRecord.isObject THEN
  11838. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11839. END;
  11840. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11841. ELSIF recordType.isObject THEN
  11842. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11843. ELSIF pointer THEN
  11844. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11845. ELSE
  11846. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11847. END;
  11848. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11849. Symbol(source, tir, 0, 0);
  11850. start := 0;
  11851. baseRecord := recordType;
  11852. WHILE (baseRecord # NIL) DO
  11853. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11854. baseRecord := baseRecord.GetBaseRecord ();
  11855. END;
  11856. ELSE
  11857. (* explicit trace method: *)
  11858. procedure := recordType.recordScope.FindMethod(0);
  11859. IF ~procedure.isFinalizer THEN
  11860. Global.GetSymbolSegmentedName(procedure, name);
  11861. NamedSymbol(source, name,procedure, 0,0);
  11862. END;
  11863. start := 1;
  11864. END;
  11865. IF (name # stackFrame) & recordType.isObject THEN
  11866. baseRecord := recordType;
  11867. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11868. baseRecord := baseRecord.GetBaseRecord ();
  11869. END;
  11870. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11871. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11872. ELSE
  11873. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11874. END;
  11875. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11876. Symbol(source, tir, 0, 0);
  11877. END;
  11878. methods := recordType.recordScope.numberMethods;
  11879. FOR i := start TO methods-1 DO
  11880. procedure := recordType.recordScope.FindMethod(i);
  11881. IF ~procedure.isFinalizer THEN
  11882. Global.GetSymbolSegmentedName(procedure, name);
  11883. NamedSymbol(source, name,procedure, 0,0);
  11884. END;
  11885. END;
  11886. END CooperativeMethodTable;
  11887. BEGIN
  11888. Global.GetSymbolSegmentedName(td,name);
  11889. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11890. source.SetExported(IsExported(td));
  11891. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11892. IF implementationVisitor.backend.cooperative THEN
  11893. base := NIL;
  11894. baseRecord := recordType.GetBaseRecord();
  11895. IF baseRecord # NIL THEN
  11896. baseTD := baseRecord.typeDeclaration;
  11897. END;
  11898. IF ~recordType.isObject THEN
  11899. Info(source, "parent");
  11900. IF baseRecord # NIL THEN
  11901. Global.GetSymbolSegmentedName(baseTD,name);
  11902. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11903. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11904. ELSE
  11905. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11906. END;
  11907. Symbol(source, tir, 0, 0);
  11908. ELSE
  11909. Address(source,0);
  11910. END;
  11911. Info(source, "record size");
  11912. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11913. source.SetReferenced(FALSE);
  11914. CooperativeMethodTable(FALSE);
  11915. base := source;
  11916. Global.GetSymbolSegmentedName(td,name);
  11917. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11918. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11919. source.SetExported(IsExported(td));
  11920. source.SetReferenced(FALSE);
  11921. END;
  11922. Info(source, "parent");
  11923. IF baseRecord # NIL THEN
  11924. Global.GetSymbolSegmentedName(baseTD,name);
  11925. sym := baseTD;
  11926. IF ~recordType.isObject THEN
  11927. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11928. sym := NIL;
  11929. END;
  11930. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11931. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11932. ELSE
  11933. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11934. END;
  11935. Symbol(source, tir, 0, 0);
  11936. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11937. Address(source,0);
  11938. ELSE
  11939. IF recordType.isObject THEN
  11940. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11941. ELSE
  11942. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11943. END;
  11944. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11945. Symbol(source, tir, 0, 0);
  11946. END;
  11947. Info(source, "base record descriptor");
  11948. Symbol(source, base, 0, 0);
  11949. CooperativeMethodTable(TRUE);
  11950. source.SetReferenced(FALSE);
  11951. IF recordType.hasPointers THEN
  11952. IF ~HasExplicitTraceMethod (recordType) THEN
  11953. implementationVisitor.CreateTraceMethod(recordType);
  11954. END;
  11955. implementationVisitor.CreateResetProcedure(recordType);
  11956. implementationVisitor.CreateAssignProcedure(recordType);
  11957. END;
  11958. ELSIF ~simple THEN
  11959. (*
  11960. MethodEnd = MPO
  11961. ---
  11962. methods (# methods)
  11963. ---
  11964. tags (16)
  11965. ---
  11966. TypeDesc = TypeInfoAdr
  11967. ---
  11968. td adr ---> rec size
  11969. ----
  11970. pointer offsets
  11971. ----
  11972. (padding)
  11973. -----
  11974. empty [2 addresses aligned]
  11975. empty
  11976. empty
  11977. numPtrs
  11978. ---
  11979. pointer offsets
  11980. ---
  11981. *)
  11982. Info(source, "MethodEnd = MPO");
  11983. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11984. source(IntermediateCode.Section).Emit(Data(Basic.invalidPosition,op));
  11985. MethodTable(TRUE);
  11986. TdTable(TypeTags, TRUE);
  11987. Info(source, "type descriptor info pointer");
  11988. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11989. IF (cellType # NIL) THEN
  11990. IF cellType.sizeInBits < 0 THEN
  11991. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11992. END;
  11993. Info(source, "cell size");
  11994. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11995. ELSE
  11996. Info(source, "record size");
  11997. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11998. END;
  11999. Info(source, "pointer offsets pointer");
  12000. padding := 1- source.pc MOD 2;
  12001. Symbol(source, source, source.pc+1+padding,0);
  12002. IF padding >0 THEN
  12003. Info(source, "padding");
  12004. FOR i := 1 TO padding DO Address(source,0) END;
  12005. END;
  12006. IF cellType # NIL THEN
  12007. PointerArray(source, cellType.cellScope, numberPointers);
  12008. ELSE
  12009. PointerArray(source, recordType.recordScope, numberPointers);
  12010. END;
  12011. ELSE
  12012. (*
  12013. simple:
  12014. td adr --> size
  12015. tag(1)
  12016. tag(2)
  12017. tag(3)
  12018. methods ->
  12019. *)
  12020. Info(source, "record size");
  12021. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  12022. TdTable(TypeTags, FALSE);
  12023. MethodTable(FALSE);
  12024. source.SetReferenced(FALSE);
  12025. END;
  12026. END NewTypeDescriptor;
  12027. BEGIN
  12028. x := x.resolved;
  12029. IF (x IS SyntaxTree.PointerType) THEN
  12030. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  12031. END;
  12032. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  12033. recordType := x(SyntaxTree.RecordType);
  12034. td := x.typeDeclaration;
  12035. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12036. ASSERT(td # NIL);
  12037. section := module.allSections.FindBySymbol(td); (* TODO *)
  12038. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12039. IF implementationVisitor.newObjectFile THEN
  12040. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12041. NewTypeDescriptor
  12042. END;
  12043. ELSE
  12044. (* data section in intermediate code *)
  12045. Global.GetSymbolSegmentedName(td,name);
  12046. Basic.SuffixSegmentedName (name, Basic.MakeString("@"));
  12047. Basic.SuffixSegmentedName (name, module.module.name);
  12048. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  12049. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  12050. tir.Emit(Data(Basic.invalidPosition,op));
  12051. END;
  12052. END;
  12053. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  12054. cellType := x(SyntaxTree.CellType);
  12055. td := x.typeDeclaration;
  12056. section := module.allSections.FindBySymbol(td); (* TODO *)
  12057. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  12058. IF implementationVisitor.newObjectFile THEN
  12059. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  12060. NewTypeDescriptor
  12061. END;
  12062. END;
  12063. END;
  12064. END
  12065. END CheckTypeDeclaration
  12066. END MetaDataGenerator;
  12067. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  12068. VAR
  12069. trace-: BOOLEAN;
  12070. traceString-: SyntaxTree.IdentifierString;
  12071. traceModuleName-: SyntaxTree.IdentifierString;
  12072. newObjectFile-: BOOLEAN;
  12073. profile-: BOOLEAN;
  12074. noRuntimeChecks: BOOLEAN;
  12075. simpleMetaData-: BOOLEAN;
  12076. noAsserts: BOOLEAN;
  12077. optimize-: BOOLEAN;
  12078. cooperative-: BOOLEAN;
  12079. preregisterStatic-: BOOLEAN;
  12080. dump-: Basic.Writer;
  12081. cellsAreObjects: BOOLEAN;
  12082. preciseGC: BOOLEAN;
  12083. PROCEDURE &InitIntermediateBackend*;
  12084. BEGIN
  12085. simpleMetaData := FALSE;
  12086. newObjectFile := FALSE;
  12087. InitBackend;
  12088. SetRuntimeModuleName(DefaultRuntimeModuleName);
  12089. SetTraceModuleName(DefaultTraceModuleName);
  12090. END InitIntermediateBackend;
  12091. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  12092. VAR
  12093. declarationVisitor: DeclarationVisitor;
  12094. implementationVisitor: ImplementationVisitor;
  12095. module: Sections.Module;
  12096. name, platformName: SyntaxTree.IdentifierString;
  12097. meta: MetaDataGenerator;
  12098. BEGIN
  12099. ResetError;
  12100. Global.GetSymbolName(x,name);
  12101. NEW(module,x,system); (* backend structures *)
  12102. Global.GetModuleName(x, name);
  12103. module.SetModuleName(name);
  12104. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  12105. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  12106. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  12107. declarationVisitor.Module(x,module);
  12108. IF newObjectFile & ~meta.simple THEN
  12109. meta.Module(implementationVisitor.moduleBodySection);
  12110. END;
  12111. GetDescription(platformName);
  12112. module.SetPlatformName(platformName);
  12113. RETURN module
  12114. END GenerateIntermediate;
  12115. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  12116. BEGIN RETURN TRUE
  12117. END SupportedImmediate;
  12118. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  12119. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  12120. END ProcessSyntaxTreeModule;
  12121. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  12122. VAR
  12123. result: Sections.Module;
  12124. traceName: Basic.MessageString;
  12125. BEGIN
  12126. ASSERT(intermediateCodeModule IS Sections.Module);
  12127. result := intermediateCodeModule(Sections.Module);
  12128. IF trace THEN
  12129. traceName := "intermediate code trace: ";
  12130. Strings.Append(traceName,traceString);
  12131. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  12132. IF (traceString="") OR (traceString="*") THEN
  12133. result.Dump(dump);
  12134. dump.Update
  12135. ELSE
  12136. Sections.DumpFiltered(dump, result, traceString);
  12137. END
  12138. END;
  12139. RETURN result
  12140. END ProcessIntermediateCodeModule;
  12141. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  12142. BEGIN instructionSet := "Intermediate";
  12143. END GetDescription;
  12144. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  12145. BEGIN
  12146. SELF.newObjectFile := newObjectFile;
  12147. SELF.simpleMetaData := simpleMetaData;
  12148. END SetNewObjectFile;
  12149. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  12150. BEGIN COPY(name, traceModuleName)
  12151. END SetTraceModuleName;
  12152. PROCEDURE DefineOptions(options: Options.Options);
  12153. BEGIN
  12154. DefineOptions^(options);
  12155. options.Add(0X,"trace",Options.String);
  12156. options.Add(0X,"runtime",Options.String);
  12157. options.Add(0X,"newObjectFile",Options.Flag);
  12158. options.Add(0X,"traceModule",Options.String);
  12159. options.Add(0X,"profile",Options.Flag);
  12160. options.Add(0X,"noRuntimeChecks",Options.Flag);
  12161. options.Add(0X,"noAsserts",Options.Flag);
  12162. options.Add(0X,"metaData",Options.String);
  12163. options.Add('o',"optimize", Options.Flag);
  12164. options.Add(0X,"preregisterStatic", Options.Flag);
  12165. options.Add(0X,"cellsAreObjects", Options.Flag);
  12166. options.Add(0X,"preciseGC", Options.Flag);
  12167. END DefineOptions;
  12168. PROCEDURE GetOptions(options: Options.Options);
  12169. VAR name,string: SyntaxTree.IdentifierString;
  12170. BEGIN
  12171. GetOptions^(options);
  12172. trace := options.GetString("trace",traceString);
  12173. profile := options.GetFlag("profile");
  12174. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  12175. noAsserts := options.GetFlag("noAsserts");
  12176. cooperative := options.GetFlag("cooperative");
  12177. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  12178. IF cooperative THEN
  12179. SetRuntimeModuleName("CPU") END
  12180. END;
  12181. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  12182. simpleMetaData := TRUE
  12183. END;
  12184. IF options.GetString("metaData",string) THEN
  12185. IF string = "simple" THEN simpleMetaData := TRUE
  12186. ELSIF string ="full" THEN simpleMetaData := FALSE
  12187. END;
  12188. END;
  12189. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  12190. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  12191. optimize := options.GetFlag("optimize");
  12192. preregisterStatic := options.GetFlag("preregisterStatic");
  12193. cellsAreObjects := options.GetFlag("cellsAreObjects");
  12194. preciseGC := options.GetFlag("preciseGC");
  12195. END GetOptions;
  12196. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  12197. BEGIN RETURN SymbolFileFormat.Get()
  12198. END DefaultSymbolFileFormat;
  12199. END IntermediateBackend;
  12200. (* ----------------------------------- register allocation ------------------------------------- *)
  12201. (* register mapping scheme
  12202. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  12203. spill offset
  12204. part(n) --> ticket <--> physical register
  12205. spill offset
  12206. *)
  12207. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  12208. emptyOperand: IntermediateCode.Operand;
  12209. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  12210. statCoopResetVariables: LONGINT;
  12211. statCoopModifyAssignments: LONGINT;
  12212. modifyAssignmentsPC : LONGINT;
  12213. statCoopNilCheck: LONGINT;
  12214. statCoopSwitch: LONGINT;
  12215. statCoopAssignProcedure: LONGINT;
  12216. statCoopTraceMethod: LONGINT;
  12217. statCoopResetProcedure: LONGINT;
  12218. statCoopTraceModule: LONGINT;
  12219. PROCEDURE ResetStatistics*;
  12220. BEGIN
  12221. statCoopResetVariables := 0;
  12222. statCoopModifyAssignments := 0;
  12223. statCoopNilCheck:= 0;
  12224. statCoopSwitch:= 0;
  12225. statCoopAssignProcedure:= 0;
  12226. statCoopTraceMethod:= 0;
  12227. statCoopResetProcedure:= 0;
  12228. statCoopTraceModule:= 0;
  12229. END ResetStatistics;
  12230. PROCEDURE Statistics*;
  12231. BEGIN
  12232. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  12233. TRACE(statCoopNilCheck, statCoopSwitch);
  12234. TRACE(statCoopAssignProcedure,
  12235. statCoopTraceMethod,
  12236. statCoopResetProcedure,
  12237. statCoopTraceModule)
  12238. END Statistics;
  12239. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  12240. VAR h: LONGINT;
  12241. BEGIN
  12242. WHILE b # 0 DO
  12243. h := a MOD b;
  12244. a := b;
  12245. b := h;
  12246. END;
  12247. RETURN a
  12248. END GCD;
  12249. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  12250. BEGIN
  12251. RETURN a*b DIV GCD(a,b)
  12252. END SCM;
  12253. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  12254. BEGIN
  12255. (*TRACE(a,b);*)
  12256. IF a = 0 THEN RETURN b
  12257. ELSIF b = 0 THEN RETURN a
  12258. ELSE RETURN SCM(a,b)
  12259. END;
  12260. END CommonAlignment;
  12261. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  12262. BEGIN
  12263. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  12264. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  12265. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & SysvABIorWINAPI(parameter.ownerType(SyntaxTree.ProcedureType).callingConvention)
  12266. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  12267. 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)
  12268. END
  12269. END PassBySingleReference;
  12270. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  12271. BEGIN
  12272. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  12273. END PassInRegister;
  12274. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  12275. VAR new: RegisterEntry;
  12276. BEGIN
  12277. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  12278. IF queue = NIL THEN
  12279. queue := new
  12280. ELSE
  12281. new.next := queue;
  12282. IF queue#NIL THEN queue.prev := new END;
  12283. queue := new
  12284. END;
  12285. END AddRegisterEntry;
  12286. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  12287. VAR this: RegisterEntry;
  12288. BEGIN
  12289. this := queue;
  12290. WHILE (this # NIL) & (this.register # register) DO
  12291. this := this.next;
  12292. END;
  12293. IF this = NIL THEN
  12294. RETURN FALSE
  12295. END;
  12296. ASSERT(this # NIL);
  12297. IF this = queue THEN queue := queue.next END;
  12298. IF this.prev # NIL THEN this.prev.next := this.next END;
  12299. IF this.next # NIL THEN this.next.prev := this.prev END;
  12300. RETURN TRUE
  12301. END RemoveRegisterEntry;
  12302. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  12303. BEGIN ASSERT(cond);
  12304. END Assert;
  12305. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  12306. BEGIN
  12307. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  12308. END ReusableRegister;
  12309. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  12310. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  12311. BEGIN
  12312. procedure := moduleScope.bodyProcedure;
  12313. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  12314. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  12315. procedure := SyntaxTree.NewProcedure(Basic.invalidPosition,Global.ModuleBodyName, procedureScope);
  12316. procedure.SetScope(moduleScope);
  12317. procedure.SetType(SyntaxTree.NewProcedureType(Basic.invalidPosition,moduleScope));
  12318. procedure.SetAccess(SyntaxTree.Hidden);
  12319. moduleScope.SetBodyProcedure(procedure);
  12320. moduleScope.AddProcedure(procedure);
  12321. procedureScope.SetBody(SyntaxTree.NewBody(Basic.invalidPosition,procedureScope)); (* empty body *)
  12322. END;
  12323. END EnsureBodyProcedure;
  12324. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  12325. VAR import: SyntaxTree.Import;
  12326. selfName: SyntaxTree.IdentifierString;
  12327. module: SyntaxTree.Module;
  12328. BEGIN
  12329. scope.ownerModule.GetName(selfName);
  12330. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  12331. module := scope.ownerModule
  12332. ELSE
  12333. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  12334. IF import = NIL THEN
  12335. RETURN NIL
  12336. ELSIF import.module = NIL THEN
  12337. RETURN NIL
  12338. ELSE module := import.module
  12339. END;
  12340. END;
  12341. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  12342. END GetSymbol;
  12343. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  12344. BEGIN
  12345. op.mode := mode;
  12346. IntermediateCode.InitOperand(op.op);
  12347. IntermediateCode.InitOperand(op.tag);
  12348. IntermediateCode.InitOperand(op.extra);
  12349. op.dimOffset := 0;
  12350. END InitOperand;
  12351. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  12352. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  12353. BEGIN RETURN IntermediateCode.GetType(system, type)
  12354. END GetType;
  12355. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  12356. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  12357. BEGIN
  12358. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  12359. (*
  12360. UniqueId(name,module,name,adr);
  12361. *)
  12362. constant := SyntaxTree.NewConstant(Basic.invalidPosition,SyntaxTree.NewIdentifier(name));
  12363. constant.SetValue(value);
  12364. constant.SetAccess(SyntaxTree.Hidden);
  12365. module.moduleScope.AddConstant(constant);
  12366. constant.SetScope(module.moduleScope);
  12367. RETURN constant
  12368. END BuildConstant;
  12369. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12370. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  12371. BEGIN
  12372. variable := scope.firstVariable;
  12373. WHILE variable # NIL DO
  12374. IF variable.NeedsTrace() THEN
  12375. RETURN TRUE;
  12376. END;
  12377. variable := variable.nextVariable;
  12378. END;
  12379. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12380. WHILE parameter # NIL DO
  12381. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  12382. RETURN TRUE;
  12383. END;
  12384. parameter := parameter.nextParameter;
  12385. END;
  12386. RETURN FALSE;
  12387. END HasPointers;
  12388. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  12389. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  12390. END IsVariableParameter;
  12391. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  12392. VAR parameter: SyntaxTree.Parameter;
  12393. BEGIN
  12394. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  12395. WHILE parameter # NIL DO
  12396. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  12397. IF parameter.movable THEN RETURN TRUE END;
  12398. parameter := parameter.nextParameter;
  12399. END;
  12400. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  12401. END HasVariableParameters;
  12402. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  12403. BEGIN
  12404. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  12405. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  12406. END HasExplicitTraceMethod;
  12407. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  12408. BEGIN
  12409. IF expression.resolved # NIL THEN expression := expression.resolved END;
  12410. IF (expression IS SyntaxTree.IntegerValue) THEN
  12411. val := expression(SyntaxTree.IntegerValue).value;
  12412. RETURN TRUE
  12413. ELSE
  12414. RETURN FALSE
  12415. END;
  12416. END IsIntegerConstant;
  12417. PROCEDURE PowerOf2*(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  12418. BEGIN
  12419. IF val <= 0 THEN RETURN FALSE END;
  12420. exp := 0;
  12421. WHILE ~ODD(val) DO
  12422. val := val DIV 2;
  12423. INC(exp)
  12424. END;
  12425. RETURN val = 1
  12426. END PowerOf2;
  12427. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  12428. VAR procedure: SyntaxTree.Procedure;
  12429. BEGIN
  12430. procedure := record.recordScope.constructor;
  12431. IF procedure = NIL THEN
  12432. record := record.GetBaseRecord();
  12433. IF record # NIL THEN
  12434. procedure := GetConstructor(record)
  12435. END;
  12436. END;
  12437. RETURN procedure;
  12438. END GetConstructor;
  12439. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  12440. BEGIN
  12441. value := SHORT(op.intValue);
  12442. RETURN op.mode = IntermediateCode.ModeImmediate;
  12443. END IsIntegerImmediate;
  12444. (** whether a type strictily is a pointer to record or object type
  12445. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  12446. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  12447. BEGIN
  12448. IF type = NIL THEN
  12449. RETURN FALSE
  12450. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  12451. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  12452. ELSE
  12453. RETURN FALSE
  12454. END
  12455. END IsStrictlyPointerToRecord;
  12456. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  12457. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  12458. END IsUnsafePointer;
  12459. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  12460. BEGIN type := type.resolved;
  12461. IF type IS SyntaxTree.PointerType THEN
  12462. type := type(SyntaxTree.PointerType).pointerBase;
  12463. type := type.resolved;
  12464. IF type IS SyntaxTree.RecordType THEN
  12465. recordType := type(SyntaxTree.RecordType);
  12466. RETURN TRUE
  12467. ELSE
  12468. RETURN FALSE
  12469. END
  12470. ELSIF type IS SyntaxTree.RecordType THEN
  12471. recordType := type(SyntaxTree.RecordType);
  12472. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  12473. ELSIF type IS SyntaxTree.ObjectType THEN
  12474. RETURN TRUE
  12475. ELSIF type IS SyntaxTree.AnyType THEN
  12476. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  12477. ELSE
  12478. RETURN FALSE
  12479. END;
  12480. END IsPointerToRecord;
  12481. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  12482. BEGIN
  12483. type := type.resolved;
  12484. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  12485. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  12486. END IsArrayOfSystemByte;
  12487. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  12488. BEGIN
  12489. IF type = NIL THEN RETURN FALSE END;
  12490. type := type.resolved;
  12491. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  12492. END IsOpenArray;
  12493. PROCEDURE IsSemiDynamicArray(type: SyntaxTree.Type): BOOLEAN;
  12494. BEGIN
  12495. IF type = NIL THEN RETURN FALSE END;
  12496. type := type.resolved;
  12497. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic);
  12498. END IsSemiDynamicArray;
  12499. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  12500. BEGIN
  12501. IF type = NIL THEN RETURN FALSE END;
  12502. type := type.resolved;
  12503. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  12504. END IsStaticArray;
  12505. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  12506. VAR size: LONGINT;
  12507. BEGIN
  12508. size := 1;
  12509. WHILE (IsStaticArray(type)) DO
  12510. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  12511. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12512. END;
  12513. RETURN size;
  12514. END StaticArrayNumElements;
  12515. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12516. BEGIN
  12517. WHILE (IsStaticArray(type)) DO
  12518. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12519. END;
  12520. RETURN type;
  12521. END StaticArrayBaseType;
  12522. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  12523. BEGIN
  12524. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  12525. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  12526. END;
  12527. RETURN type;
  12528. END ArrayBaseType;
  12529. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  12530. BEGIN
  12531. IF type = NIL THEN RETURN FALSE END;
  12532. type := type.resolved;
  12533. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  12534. END IsDelegate;
  12535. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  12536. VAR i: LONGINT;
  12537. BEGIN
  12538. i := 0; type := type.resolved;
  12539. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12540. INC(i);
  12541. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  12542. END;
  12543. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12544. INC(i);
  12545. type := type(SyntaxTree.MathArrayType).arrayBase;
  12546. IF type # NIL THEN type := type.resolved END;
  12547. END;
  12548. RETURN i
  12549. END DynamicDim;
  12550. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  12551. BEGIN
  12552. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  12553. type := type(SyntaxTree.ArrayType).arrayBase;
  12554. END;
  12555. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  12556. type := type(SyntaxTree.MathArrayType).arrayBase;
  12557. END;
  12558. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  12559. END StaticSize;
  12560. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  12561. BEGIN
  12562. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  12563. END IsImmediate;
  12564. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  12565. BEGIN
  12566. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  12567. END IsAddress;
  12568. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  12569. BEGIN
  12570. RETURN (x.mode = IntermediateCode.ModeRegister);
  12571. END IsRegister;
  12572. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  12573. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  12574. BEGIN
  12575. typeDeclaration := recordType.typeDeclaration;
  12576. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  12577. Global.GetSymbolSegmentedName(typeDeclaration,name);
  12578. END GetRecordTypeName;
  12579. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  12580. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  12581. BEGIN
  12582. parSize := 0;
  12583. IF SemanticChecker.StructuredReturnType(procedureType) THEN
  12584. parameter := procedureType.returnParameter;
  12585. INC(parSize,system.SizeOfParameter(parameter));
  12586. parSize := parSize + (-parSize) MOD system.addressSize;
  12587. END;
  12588. parameter :=procedureType.lastParameter;
  12589. WHILE (parameter # NIL) DO
  12590. INC(parSize,system.SizeOfParameter(parameter));
  12591. parSize := parSize + (-parSize) MOD system.addressSize;
  12592. parameter := parameter.prevParameter;
  12593. END;
  12594. IF procedureType.selfParameter # NIL THEN
  12595. parameter := procedureType.selfParameter;
  12596. INC(parSize,system.SizeOfParameter(parameter));
  12597. parSize := parSize + (-parSize) MOD system.addressSize;
  12598. ELSIF procedureType.isDelegate THEN INC(parSize,system.addressSize)
  12599. END; (* method => self pointer *)
  12600. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  12601. RETURN ToMemoryUnits(system,parSize)
  12602. END ParametersSize;
  12603. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  12604. BEGIN
  12605. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  12606. END IsNested;
  12607. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  12608. BEGIN
  12609. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  12610. scope := scope.outerScope;
  12611. END;
  12612. RETURN scope # NIL;
  12613. END InCellScope;
  12614. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  12615. BEGIN
  12616. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  12617. RETURN 0
  12618. ELSE
  12619. *)
  12620. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  12621. (*END;*)
  12622. END ProcedureParametersSize;
  12623. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  12624. VAR dataUnit: LONGINT;
  12625. BEGIN dataUnit := system.dataUnit;
  12626. ASSERT(size MOD system.dataUnit = 0);
  12627. RETURN size DIV system.dataUnit
  12628. END ToMemoryUnits;
  12629. PROCEDURE Get*(): Backend.Backend;
  12630. VAR backend: IntermediateBackend;
  12631. BEGIN NEW(backend); RETURN backend
  12632. END Get;
  12633. PROCEDURE Nop(position: Basic.Position):IntermediateCode.Instruction;
  12634. VAR instruction: IntermediateCode.Instruction;
  12635. BEGIN
  12636. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  12637. RETURN instruction
  12638. END Nop;
  12639. PROCEDURE Mov(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12640. VAR instruction: IntermediateCode.Instruction;
  12641. BEGIN
  12642. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  12643. RETURN instruction
  12644. END Mov;
  12645. (* like Mov but ensures that no new register will be allocated for dest *)
  12646. PROCEDURE MovReplace(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12647. VAR instruction: IntermediateCode.Instruction;
  12648. BEGIN
  12649. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  12650. RETURN instruction
  12651. END MovReplace;
  12652. PROCEDURE Conv(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12653. VAR instruction: IntermediateCode.Instruction;
  12654. BEGIN
  12655. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  12656. RETURN instruction
  12657. END Conv;
  12658. PROCEDURE SysvABI( cc: LONGINT ): BOOLEAN;
  12659. BEGIN
  12660. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention})
  12661. END SysvABI;
  12662. PROCEDURE SysvABIorWINAPI( cc: LONGINT ): BOOLEAN;
  12663. BEGIN
  12664. RETURN (cc IN {SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention, SyntaxTree.WinAPICallingConvention})
  12665. END SysvABIorWINAPI;
  12666. PROCEDURE Call*(position: Basic.Position;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  12667. VAR instruction: IntermediateCode.Instruction;
  12668. BEGIN
  12669. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  12670. RETURN instruction
  12671. END Call;
  12672. PROCEDURE Exit(position: Basic.Position;pcOffset: LONGINT; callingConvention, unwind: LONGINT): IntermediateCode.Instruction;
  12673. VAR op1, op2, op3: IntermediateCode.Operand;
  12674. VAR instruction: IntermediateCode.Instruction;
  12675. BEGIN
  12676. IntermediateCode.InitNumber(op1,pcOffset);
  12677. IntermediateCode.InitNumber(op2,callingConvention);
  12678. IntermediateCode.InitNumber(op3,unwind);
  12679. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,op3);
  12680. RETURN instruction
  12681. END Exit;
  12682. PROCEDURE Return(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12683. VAR instruction: IntermediateCode.Instruction;
  12684. BEGIN
  12685. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  12686. RETURN instruction
  12687. END Return;
  12688. PROCEDURE Result*(position: Basic.Position;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  12689. VAR instruction: IntermediateCode.Instruction;
  12690. BEGIN
  12691. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  12692. RETURN instruction
  12693. END Result;
  12694. PROCEDURE Trap(position: Basic.Position;nr: LONGINT): IntermediateCode.Instruction;
  12695. VAR op1: IntermediateCode.Operand;
  12696. VAR instruction: IntermediateCode.Instruction;
  12697. BEGIN
  12698. IntermediateCode.InitNumber(op1,nr);
  12699. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  12700. RETURN instruction
  12701. END Trap;
  12702. PROCEDURE Br(position: Basic.Position;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  12703. VAR instruction: IntermediateCode.Instruction;
  12704. BEGIN
  12705. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  12706. RETURN instruction
  12707. END Br;
  12708. PROCEDURE Breq(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12709. VAR instruction: IntermediateCode.Instruction;
  12710. BEGIN
  12711. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  12712. RETURN instruction
  12713. END Breq;
  12714. PROCEDURE Brne(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12715. VAR instruction: IntermediateCode.Instruction;
  12716. BEGIN
  12717. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  12718. RETURN instruction
  12719. END Brne;
  12720. PROCEDURE Brge(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12721. VAR instruction: IntermediateCode.Instruction;
  12722. BEGIN
  12723. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  12724. RETURN instruction
  12725. END Brge;
  12726. PROCEDURE Brlt(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12727. VAR instruction: IntermediateCode.Instruction;
  12728. BEGIN
  12729. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  12730. RETURN instruction
  12731. END Brlt;
  12732. PROCEDURE Pop*(position: Basic.Position;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  12733. VAR instruction: IntermediateCode.Instruction;
  12734. BEGIN
  12735. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  12736. RETURN instruction
  12737. END Pop;
  12738. PROCEDURE Push*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12739. VAR instruction: IntermediateCode.Instruction;
  12740. BEGIN
  12741. ASSERT(op.mode # IntermediateCode.Undefined);
  12742. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  12743. RETURN instruction
  12744. END Push;
  12745. PROCEDURE Neg(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12746. VAR instruction: IntermediateCode.Instruction;
  12747. BEGIN
  12748. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  12749. RETURN instruction
  12750. END Neg;
  12751. PROCEDURE Not(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12752. VAR instruction: IntermediateCode.Instruction;
  12753. BEGIN
  12754. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  12755. RETURN instruction
  12756. END Not;
  12757. PROCEDURE Abs(position: Basic.Position;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  12758. VAR instruction: IntermediateCode.Instruction;
  12759. BEGIN
  12760. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  12761. RETURN instruction
  12762. END Abs;
  12763. PROCEDURE Mul(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12764. VAR instruction: IntermediateCode.Instruction;
  12765. BEGIN
  12766. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12767. ASSERT(~IsImmediate(instruction.op1));
  12768. RETURN instruction
  12769. END Mul;
  12770. PROCEDURE Div(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12771. VAR instruction: IntermediateCode.Instruction;
  12772. BEGIN
  12773. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12774. RETURN instruction
  12775. END Div;
  12776. PROCEDURE Mod(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12777. VAR instruction: IntermediateCode.Instruction;
  12778. BEGIN
  12779. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12780. RETURN instruction
  12781. END Mod;
  12782. PROCEDURE Sub(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12783. VAR instruction: IntermediateCode.Instruction;
  12784. BEGIN
  12785. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12786. RETURN instruction
  12787. END Sub;
  12788. PROCEDURE Add(position: Basic.Position;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12789. VAR instruction: IntermediateCode.Instruction;
  12790. BEGIN
  12791. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12792. RETURN instruction
  12793. END Add;
  12794. PROCEDURE And(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12795. VAR instruction: IntermediateCode.Instruction;
  12796. BEGIN
  12797. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12798. RETURN instruction
  12799. END And;
  12800. PROCEDURE Or(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12801. VAR instruction: IntermediateCode.Instruction;
  12802. BEGIN
  12803. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12804. RETURN instruction
  12805. END Or;
  12806. PROCEDURE Xor(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12807. VAR instruction: IntermediateCode.Instruction;
  12808. BEGIN
  12809. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12810. RETURN instruction
  12811. END Xor;
  12812. PROCEDURE Shl(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12813. VAR instruction: IntermediateCode.Instruction;
  12814. BEGIN
  12815. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12816. RETURN instruction
  12817. END Shl;
  12818. PROCEDURE Shr(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12819. VAR instruction: IntermediateCode.Instruction;
  12820. BEGIN
  12821. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12822. RETURN instruction
  12823. END Shr;
  12824. PROCEDURE Rol(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12825. VAR instruction: IntermediateCode.Instruction;
  12826. BEGIN
  12827. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12828. RETURN instruction
  12829. END Rol;
  12830. PROCEDURE Ror(position: Basic.Position;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12831. VAR instruction: IntermediateCode.Instruction;
  12832. BEGIN
  12833. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12834. RETURN instruction
  12835. END Ror;
  12836. PROCEDURE Cas(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12837. VAR instruction: IntermediateCode.Instruction;
  12838. BEGIN
  12839. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12840. RETURN instruction
  12841. END Cas;
  12842. PROCEDURE Copy(position: Basic.Position;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12843. VAR instruction: IntermediateCode.Instruction;
  12844. BEGIN
  12845. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12846. RETURN instruction
  12847. END Copy;
  12848. PROCEDURE Fill(position: Basic.Position;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12849. VAR instruction: IntermediateCode.Instruction;
  12850. BEGIN
  12851. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12852. RETURN instruction
  12853. END Fill;
  12854. PROCEDURE Asm(position: Basic.Position;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12855. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12856. BEGIN
  12857. string := IntermediateCode.String(s);
  12858. (*IntermediateCode.SetIntValue(string,position); (* for error reporting *)*)
  12859. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12860. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12861. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12862. RETURN instruction
  12863. END Asm;
  12864. PROCEDURE Data*(position: Basic.Position;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12865. VAR instruction: IntermediateCode.Instruction;
  12866. BEGIN
  12867. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12868. RETURN instruction
  12869. END Data;
  12870. PROCEDURE SpecialInstruction(position: Basic.Position;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12871. VAR instruction: IntermediateCode.Instruction;
  12872. BEGIN
  12873. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12874. IntermediateCode.SetSubType(instruction, subtype);
  12875. RETURN instruction
  12876. END SpecialInstruction;
  12877. PROCEDURE Reserve(position: Basic.Position;units: LONGINT): IntermediateCode.Instruction;
  12878. VAR op1: IntermediateCode.Operand;
  12879. VAR instruction: IntermediateCode.Instruction;
  12880. BEGIN
  12881. (*! generate a warning if size exceeds a certain limit *)
  12882. (*
  12883. ASSERT(bytes < 1000000); (* sanity check *)
  12884. *)
  12885. ASSERT(0 <= units); (* sanity check *)
  12886. IntermediateCode.InitNumber(op1,units);
  12887. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12888. RETURN instruction
  12889. END Reserve;
  12890. PROCEDURE LabelInstruction(position: Basic.Position): IntermediateCode.Instruction;
  12891. VAR op1: IntermediateCode.Operand;
  12892. VAR instruction: IntermediateCode.Instruction;
  12893. BEGIN
  12894. IntermediateCode.InitNumber(op1,position.start);
  12895. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12896. RETURN instruction
  12897. END LabelInstruction;
  12898. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12899. VAR pc: LONGINT;
  12900. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12901. BEGIN
  12902. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12903. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12904. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12905. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12906. IF instr.op1.type.form = IntermediateCode.Float THEN
  12907. RETURN instr.op1.floatValue = op.floatValue
  12908. ELSE
  12909. RETURN instr.op1.intValue = op.intValue
  12910. END;
  12911. END ProvidesValue;
  12912. BEGIN
  12913. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12914. pc := 0;
  12915. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12916. INC(pc);
  12917. END;
  12918. IF pc = data.pc THEN
  12919. data.Emit(Data(Basic.invalidPosition,vop));
  12920. END;
  12921. RETURN pc
  12922. END EnterImmediate;
  12923. PROCEDURE Init;
  12924. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12925. BEGIN
  12926. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12927. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12928. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12929. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12930. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12931. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12932. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12933. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12934. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12935. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12936. IntermediateCode.InitOperand(emptyOperand);
  12937. FOR i := 0 TO NumberSystemCalls-1 DO
  12938. name := "@SystemCall";
  12939. Basic.AppendNumber(name,i);
  12940. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12941. END;
  12942. END Init;
  12943. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12944. BEGIN
  12945. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12946. END IsExported;
  12947. BEGIN
  12948. Init;
  12949. END FoxIntermediateBackend.
  12950. Compiler.Compile FoxIntermediateBackend.Mod ~
  12951. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12952. # 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 ~
  12953. # Release.Build --path="/temp/obg/" WinAosNewObjectFile ~
  12954. # 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 ~
  12955. FSTools.CloseFiles A2Z.exe ~
  12956. SystemTools.FreeDownTo FoxIntermediateBackend ~
  12957. Compiler.Compile -p=Win32G --destPath=/temp/obg/ --traceModule=Trace
  12958. Runtime.Mod Trace.Mod Generic.Win32.Kernel32.Mod Win32.Machine.Mod Heaps.Mod
  12959. Generic.Modules.Mod Win32.Objects.Mod Win32.Kernel.Mod KernelLog.Mod Plugins.Mod Streams.Mod Pipes.Mod
  12960. Commands.Mod I386.Reals.Mod Generic.Reflection.Mod TrapWriters.Mod CRC.Mod SystemVersion.Mod
  12961. Win32.Traps.Mod Locks.Mod Win32.Clock.Mod Disks.Mod Files.Mod Dates.Mod Strings.Mod UTF8Strings.Mod
  12962. FileTrapWriter.Mod Caches.Mod DiskVolumes.Mod OldDiskVolumes.Mod RAMVolumes.Mod DiskFS.Mod OldDiskFS.Mod
  12963. OberonFS.Mod FATVolumes.Mod FATFiles.Mod ISO9660Volumes.Mod ISO9660Files.Mod Win32.User32.Mod
  12964. Win32.WinTrace.Mod Win32.ODBC.Mod Win32.Shell32.Mod Win32.SQL.Mod Win32.WinFS.Mod
  12965. RelativeFileSystem.Mod BitSets.Mod Diagnostics.Mod StringPool.Mod ObjectFile.Mod
  12966. GenericLinker.Mod GenericLoader.Mod BootConsole.Mod