twobit-smaller.sch 578 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409
  1. ; Complete source for Twobit and Sparc assembler in one file.
  2. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  3. ;
  4. ; See 'twobit-benchmark', at end.
  5. ; Copyright 1998 Lars T Hansen.
  6. ;
  7. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  8. ;
  9. ; Completely fundamental pathname manipulation.
  10. ; This takes zero or more directory components and a file name and
  11. ; constructs a filename relative to the current directory.
  12. (define (make-relative-filename . components)
  13. (define (construct l)
  14. (if (null? (cdr l))
  15. l
  16. (cons (car l)
  17. (cons "/" (construct (cdr l))))))
  18. (if (null? (cdr components))
  19. (car components)
  20. (apply string-append (construct components))))
  21. ; This takes one or more directory components and constructs a
  22. ; directory name with proper termination (a crock -- we can finess
  23. ; this later).
  24. (define (pathname-append . components)
  25. (define (construct l)
  26. (cond ((null? (cdr l))
  27. l)
  28. ((string=? (car l) "")
  29. (construct (cdr l)))
  30. ((char=? #\/ (string-ref (car l) (- (string-length (car l)) 1)))
  31. (cons (car l) (construct (cdr l))))
  32. (else
  33. (cons (car l)
  34. (cons "/" (construct (cdr l)))))))
  35. (let ((n (if (null? (cdr components))
  36. (car components)
  37. (apply string-append (construct components)))))
  38. (if (not (char=? #\/ (string-ref n (- (string-length n) 1))))
  39. (string-append n "/")
  40. n)))
  41. ; eof
  42. ; Copyright 1998 Lars T Hansen.
  43. ;
  44. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  45. ;
  46. ; Nbuild parameters for SPARC Larceny.
  47. (define (make-nbuild-parameter dir source? verbose? hostdir hostname)
  48. (let ((parameters
  49. `((compiler . ,(pathname-append dir "Compiler"))
  50. (util . ,(pathname-append dir "Util"))
  51. (build . ,(pathname-append dir "Rts" "Build"))
  52. (source . ,(pathname-append dir "Lib"))
  53. (common-source . ,(pathname-append dir "Lib" "Common"))
  54. (repl-source . ,(pathname-append dir "Repl"))
  55. (interp-source . ,(pathname-append dir "Eval"))
  56. (machine-source . ,(pathname-append dir "Lib" "Sparc"))
  57. (common-asm . ,(pathname-append dir "Asm" "Common"))
  58. (sparc-asm . ,(pathname-append dir "Asm" "Sparc"))
  59. (target-machine . SPARC)
  60. (endianness . big)
  61. (word-size . 32)
  62. (always-source? . ,source?)
  63. (verbose-load? . ,verbose?)
  64. (compatibility . ,(pathname-append dir "Compat" hostdir))
  65. (host-system . ,hostname)
  66. )))
  67. (lambda (key)
  68. (let ((probe (assq key parameters)))
  69. (if probe
  70. (cdr probe)
  71. #f)))))
  72. (define nbuild-parameter
  73. (make-nbuild-parameter "" #f #f "Larceny" "Larceny"))
  74. ; eof
  75. ; Copyright 1998 Lars T Hansen.
  76. ;
  77. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  78. ;
  79. ; Useful list functions.
  80. ;
  81. ; Notes:
  82. ; * Reduce, reduce-right, fold-right, fold-left are compatible with MIT Scheme.
  83. ; * Make-list is compatible with MIT Scheme and Chez Scheme.
  84. ; * These are not (yet) compatible with Shivers's proposed list functions.
  85. ; * remq, remv, remove, remq!, remv!, remov!, every?, and some? are in the
  86. ; basic library.
  87. ; Destructively remove all associations whose key matches `key' from `alist'.
  88. (define (aremq! key alist)
  89. (cond ((null? alist) alist)
  90. ((eq? key (caar alist))
  91. (aremq! key (cdr alist)))
  92. (else
  93. (set-cdr! alist (aremq! key (cdr alist)))
  94. alist)))
  95. (define (aremv! key alist)
  96. (cond ((null? alist) alist)
  97. ((eqv? key (caar alist))
  98. (aremv! key (cdr alist)))
  99. (else
  100. (set-cdr! alist (aremv! key (cdr alist)))
  101. alist)))
  102. (define (aremove! key alist)
  103. (cond ((null? alist) alist)
  104. ((equal? key (caar alist))
  105. (aremove! key (cdr alist)))
  106. (else
  107. (set-cdr! alist (aremove! key (cdr alist)))
  108. alist)))
  109. ; Return a list of elements of `list' selected by the predicate.
  110. (define (filter select? list)
  111. (cond ((null? list) list)
  112. ((select? (car list))
  113. (cons (car list) (filter select? (cdr list))))
  114. (else
  115. (filter select? (cdr list)))))
  116. ; Return the first element of `list' selected by the predicate.
  117. (define (find selected? list)
  118. (cond ((null? list) #f)
  119. ((selected? (car list)) (car list))
  120. (else (find selected? (cdr list)))))
  121. ; Return a list with all duplicates (according to predicate) removed.
  122. (define (remove-duplicates list same?)
  123. (define (member? x list)
  124. (cond ((null? list) #f)
  125. ((same? x (car list)) #t)
  126. (else (member? x (cdr list)))))
  127. (cond ((null? list) list)
  128. ((member? (car list) (cdr list))
  129. (remove-duplicates (cdr list) same?))
  130. (else
  131. (cons (car list) (remove-duplicates (cdr list) same?)))))
  132. ; Return the least element of `list' according to some total order.
  133. (define (least less? list)
  134. (reduce (lambda (a b) (if (less? a b) a b)) #f list))
  135. ; Return the greatest element of `list' according to some total order.
  136. (define (greatest greater? list)
  137. (reduce (lambda (a b) (if (greater? a b) a b)) #f list))
  138. ; (mappend p l) = (apply append (map p l))
  139. (define (mappend proc l)
  140. (apply append (map proc l)))
  141. ; (make-list n) => (a1 ... an) for some ai
  142. ; (make-list n x) => (a1 ... an) where ai = x
  143. (define (make-list nelem . rest)
  144. (let ((val (if (null? rest) #f (car rest))))
  145. (define (loop n l)
  146. (if (zero? n)
  147. l
  148. (loop (- n 1) (cons val l))))
  149. (loop nelem '())))
  150. ; (reduce p x ()) => x
  151. ; (reduce p x (a)) => a
  152. ; (reduce p x (a b ...)) => (p (p a b) ...))
  153. (define (reduce proc initial l)
  154. (define (loop val l)
  155. (if (null? l)
  156. val
  157. (loop (proc val (car l)) (cdr l))))
  158. (cond ((null? l) initial)
  159. ((null? (cdr l)) (car l))
  160. (else (loop (car l) (cdr l)))))
  161. ; (reduce-right p x ()) => x
  162. ; (reduce-right p x (a)) => a
  163. ; (reduce-right p x (a b ...)) => (p a (p b ...))
  164. (define (reduce-right proc initial l)
  165. (define (loop l)
  166. (if (null? (cdr l))
  167. (car l)
  168. (proc (car l) (loop (cdr l)))))
  169. (cond ((null? l) initial)
  170. ((null? (cdr l)) (car l))
  171. (else (loop l))))
  172. ; (fold-left p x (a b ...)) => (p (p (p x a) b) ...)
  173. (define (fold-left proc initial l)
  174. (if (null? l)
  175. initial
  176. (fold-left proc (proc initial (car l)) (cdr l))))
  177. ; (fold-right p x (a b ...)) => (p a (p b (p ... x)))
  178. (define (fold-right proc initial l)
  179. (if (null? l)
  180. initial
  181. (proc (car l) (fold-right proc initial (cdr l)))))
  182. ; (iota n) => (0 1 2 ... n-1)
  183. (define (iota n)
  184. (let loop ((n (- n 1)) (r '()))
  185. (let ((r (cons n r)))
  186. (if (= n 0)
  187. r
  188. (loop (- n 1) r)))))
  189. ; (list-head (a1 ... an) m) => (a1 ... am) for m <= n
  190. (define (list-head l n)
  191. (if (zero? n)
  192. '()
  193. (cons (car l) (list-head (cdr l) (- n 1)))))
  194. ; eof
  195. ; Copyright 1998 Lars T Hansen.
  196. ;
  197. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  198. ;
  199. ; Larceny -- compatibility library for Twobit running under Larceny.
  200. (define ($$trace x) #t)
  201. (define host-system 'larceny)
  202. ; Temporary?
  203. (define (.check! flag exn . args)
  204. (if (not flag)
  205. (apply error "Runtime check exception: " exn args)))
  206. ; The compatibility library loads Auxlib if compat:initialize is called
  207. ; without arguments. Compat:load will load fasl files when appropriate.
  208. (define (compat:initialize . rest)
  209. (if (null? rest)
  210. (let ((dir (nbuild-parameter 'compatibility)))
  211. (compat:load (string-append dir "compat2.sch"))
  212. (compat:load (string-append dir "../../Auxlib/list.sch"))
  213. (compat:load (string-append dir "../../Auxlib/pp.sch")))))
  214. (define (with-optimization level thunk)
  215. (thunk))
  216. ; Calls thunk1, and if thunk1 causes an error to be signaled, calls thunk2.
  217. (define (call-with-error-control thunk1 thunk2)
  218. (let ((eh (error-handler)))
  219. (error-handler (lambda args
  220. (error-handler eh)
  221. (thunk2)
  222. (apply eh args)))
  223. (thunk1)
  224. (error-handler eh)))
  225. (define (larc-new-extension fn ext)
  226. (let* ((l (string-length fn))
  227. (x (let loop ((i (- l 1)))
  228. (cond ((< i 0) #f)
  229. ((char=? (string-ref fn i) #\.) (+ i 1))
  230. (else (loop (- i 1)))))))
  231. (if (not x)
  232. (string-append fn "." ext)
  233. (string-append (substring fn 0 x) ext))))
  234. (define (compat:load filename)
  235. (define (loadit fn)
  236. (if (nbuild-parameter 'verbose-load?)
  237. (format #t "~a~%" fn))
  238. (load fn))
  239. (if (nbuild-parameter 'always-source?)
  240. (loadit filename)
  241. (let ((fn (larc-new-extension filename "fasl")))
  242. (if (and (file-exists? fn)
  243. (compat:file-newer? fn filename))
  244. (loadit fn)
  245. (loadit filename)))))
  246. (define (compat:file-newer? a b)
  247. (let* ((ta (file-modification-time a))
  248. (tb (file-modification-time b))
  249. (limit (vector-length ta)))
  250. (let loop ((i 0))
  251. (cond ((= i limit)
  252. #f)
  253. ((= (vector-ref ta i) (vector-ref tb i))
  254. (loop (+ i 1)))
  255. (else
  256. (> (vector-ref ta i) (vector-ref tb i)))))))
  257. ; eof
  258. ; Copyright 1998 Lars T Hansen.
  259. ;
  260. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  261. ;
  262. ; Larceny -- second part of compatibility code
  263. ; This file ought to be compiled, but doesn't have to be.
  264. ;
  265. ; 12 April 1999
  266. (define host-system 'larceny) ; Don't remove this!
  267. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  268. ;
  269. ; A well-defined sorting procedure.
  270. (define compat:sort (lambda (list less?) (sort list less?)))
  271. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  272. ;
  273. ; Well-defined character codes.
  274. ; Returns the UCS-2 code for a character.
  275. (define compat:char->integer char->integer)
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;
  278. ; Input and output
  279. (define (write-lop item port)
  280. (lowlevel-write item port)
  281. (newline port)
  282. (newline port))
  283. (define write-fasl-datum lowlevel-write)
  284. ; The power of self-hosting ;-)
  285. (define (misc->bytevector x)
  286. (let ((bv (bytevector-like-copy x)))
  287. (typetag-set! bv $tag.bytevector-typetag)
  288. bv))
  289. (define string->bytevector misc->bytevector)
  290. (define bignum->bytevector misc->bytevector)
  291. (define (flonum->bytevector x)
  292. (clear-first-word (misc->bytevector x)))
  293. (define (compnum->bytevector x)
  294. (clear-first-word (misc->bytevector x)))
  295. ; Clears garbage word of compnum/flonum; makes regression testing much
  296. ; easier.
  297. (define (clear-first-word bv)
  298. (bytevector-like-set! bv 0 0)
  299. (bytevector-like-set! bv 1 0)
  300. (bytevector-like-set! bv 2 0)
  301. (bytevector-like-set! bv 3 0)
  302. bv)
  303. (define (list->bytevector l)
  304. (let ((b (make-bytevector (length l))))
  305. (do ((i 0 (+ i 1))
  306. (l l (cdr l)))
  307. ((null? l) b)
  308. (bytevector-set! b i (car l)))))
  309. (define bytevector-word-ref
  310. (let ((two^8 (expt 2 8))
  311. (two^16 (expt 2 16))
  312. (two^24 (expt 2 24)))
  313. (lambda (bv i)
  314. (+ (* (bytevector-ref bv i) two^24)
  315. (* (bytevector-ref bv (+ i 1)) two^16)
  316. (* (bytevector-ref bv (+ i 2)) two^8)
  317. (bytevector-ref bv (+ i 3))))))
  318. (define (twobit-format fmt . rest)
  319. (let ((out (open-output-string)))
  320. (apply format out fmt rest)
  321. (get-output-string out)))
  322. ; This needs to be a random number in both a weaker and stronger sense
  323. ; than `random': it doesn't need to be a truly random number, so a sequence
  324. ; of calls can return a non-random sequence, but if two processes generate
  325. ; two sequences, then those sequences should not be the same.
  326. ;
  327. ; Gross, huh?
  328. (define (an-arbitrary-number)
  329. (system "echo \\\"`date`\\\" > a-random-number")
  330. (let ((x (string-hash (call-with-input-file "a-random-number" read))))
  331. (delete-file "a-random-number")
  332. x))
  333. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  334. ;
  335. ; Miscellaneous
  336. (define cerror error)
  337. ; eof
  338. ; Copyright 1991 Wiliam Clinger.
  339. ;
  340. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  341. ;
  342. ; Sets represented as lists.
  343. ;
  344. ; 5 April 1999.
  345. (define (empty-set) '())
  346. (define (empty-set? x) (null? x))
  347. (define (make-set x)
  348. (define (loop x y)
  349. (cond ((null? x) y)
  350. ((member (car x) y) (loop (cdr x) y))
  351. (else (loop (cdr x) (cons (car x) y)))))
  352. (loop x '()))
  353. (define (set-equal? x y)
  354. (and (subset? x y) (subset? y x)))
  355. (define (subset? x y)
  356. (every? (lambda (x) (member x y))
  357. x))
  358. ; To get around MacScheme's limit on the number of arguments.
  359. (define apply-union)
  360. (define union
  361. (letrec ((union2
  362. (lambda (x y)
  363. (cond ((null? x) y)
  364. ((member (car x) y)
  365. (union2 (cdr x) y))
  366. (else (union2 (cdr x) (cons (car x) y)))))))
  367. (set! apply-union
  368. (lambda (sets)
  369. (do ((sets sets (cdr sets))
  370. (result '() (union2 (car sets) result)))
  371. ((null? sets)
  372. result))))
  373. (lambda args
  374. (cond ((null? args) '())
  375. ((null? (cdr args)) (car args))
  376. ((null? (cddr args)) (union2 (car args) (cadr args)))
  377. (else (union2 (union2 (car args)
  378. (cadr args))
  379. (apply union (cddr args))))))))
  380. (define intersection
  381. (letrec ((intersection2
  382. (lambda (x y)
  383. (cond ((null? x) '())
  384. ((member (car x) y)
  385. (cons (car x) (intersection2 (cdr x) y)))
  386. (else (intersection2 (cdr x) y))))))
  387. (lambda args
  388. (cond ((null? args) '())
  389. ((null? (cdr args)) (car args))
  390. ((null? (cddr args)) (intersection2 (car args) (cadr args)))
  391. (else (intersection2 (intersection2 (car args)
  392. (cadr args))
  393. (apply intersection (cddr args))))))))
  394. (define (difference x y)
  395. (cond ((null? x) '())
  396. ((member (car x) y)
  397. (difference (cdr x) y))
  398. (else (cons (car x) (difference (cdr x) y)))))
  399. ; Reasonably portable hashing on EQ?, EQV?, EQUAL?.
  400. ; Requires bignums, SYMBOL-HASH.
  401. ;
  402. ; Given any Scheme object, returns a non-negative exact integer
  403. ; less than 2^24.
  404. (define object-hash (lambda (x) 0)) ; hash on EQ?, EQV?
  405. (define equal-hash (lambda (x) 0)) ; hash on EQUAL?
  406. (let ((n 16777216)
  407. (n-1 16777215)
  408. (adj:fixnum 9000000)
  409. (adj:negative 8000000)
  410. (adj:large 7900000)
  411. (adj:ratnum 7800000)
  412. (adj:complex 7700000)
  413. (adj:flonum 7000000)
  414. (adj:compnum 6900000)
  415. (adj:char 6111000)
  416. (adj:string 5022200)
  417. (adj:vector 4003330)
  418. (adj:misc 3000444)
  419. (adj:pair 2555000)
  420. (adj:proc 2321001)
  421. (adj:iport 2321002)
  422. (adj:oport 2321003)
  423. (adj:weird 2321004)
  424. (budget0 32))
  425. (define (combine hash adjustment)
  426. (modulo (+ hash hash hash adjustment) 16777216))
  427. (define (hash-on-equal x budget)
  428. (if (> budget 0)
  429. (cond ((string? x)
  430. (string-hash x))
  431. ((pair? x)
  432. (let ((budget (quotient budget 2)))
  433. (combine (hash-on-equal (car x) budget)
  434. (hash-on-equal (cdr x) budget))))
  435. ((vector? x)
  436. (let ((n (vector-length x))
  437. (budget (quotient budget 4)))
  438. (if (> n 0)
  439. (combine
  440. (combine (hash-on-equal (vector-ref x 0) budget)
  441. (hash-on-equal (vector-ref x (- n 1)) budget))
  442. (hash-on-equal (vector-ref x (quotient n 2))
  443. (+ budget budget)))
  444. adj:vector)))
  445. (else
  446. (object-hash x)))
  447. adj:weird))
  448. (set! object-hash
  449. (lambda (x)
  450. (cond ((symbol? x)
  451. (symbol-hash x))
  452. ((number? x)
  453. (if (exact? x)
  454. (cond ((integer? x)
  455. (cond ((negative? x)
  456. (combine (object-hash (- x)) adj:negative))
  457. ((< x n)
  458. (combine x adj:fixnum))
  459. (else
  460. (combine (modulo x n) adj:large))))
  461. ((rational? x)
  462. (combine (combine (object-hash (numerator x))
  463. adj:ratnum)
  464. (object-hash (denominator x))))
  465. ((real? x)
  466. adj:weird)
  467. ((complex? x)
  468. (combine (combine (object-hash (real-part x))
  469. adj:complex)
  470. (object-hash (imag-part x))))
  471. (else
  472. adj:weird))
  473. (cond (#t
  474. ; We can't really do anything with inexact numbers
  475. ; unless infinities and NaNs behave reasonably.
  476. adj:flonum)
  477. ((rational? x)
  478. (combine
  479. (combine (object-hash
  480. (inexact->exact (numerator x)))
  481. adj:flonum)
  482. (object-hash (inexact->exact (denominator x)))))
  483. ((real? x)
  484. adj:weird)
  485. ((complex? x)
  486. (combine (combine (object-hash (real-part x))
  487. adj:compnum)
  488. (object-hash (imag-part x))))
  489. (else adj:weird))))
  490. ((char? x)
  491. (combine (char->integer x) adj:char))
  492. ((string? x)
  493. (combine (string-length x) adj:string))
  494. ((vector? x)
  495. (combine (vector-length x) adj:vector))
  496. ((eq? x #t)
  497. (combine 1 adj:misc))
  498. ((eq? x #f)
  499. (combine 2 adj:misc))
  500. ((null? x)
  501. (combine 3 adj:misc))
  502. ((pair? x)
  503. adj:pair)
  504. ((procedure? x)
  505. adj:proc)
  506. ((input-port? x)
  507. adj:iport)
  508. ((output-port? x)
  509. adj:oport)
  510. (else
  511. adj:weird))))
  512. (set! equal-hash
  513. (lambda (x)
  514. (hash-on-equal x budget0)))); Hash tables.
  515. ; Requires CALL-WITHOUT-INTERRUPTS.
  516. ; This code should be thread-safe provided VECTOR-REF is atomic.
  517. ;
  518. ; (make-hashtable <hash-function> <bucket-searcher> <size>)
  519. ;
  520. ; Returns a newly allocated mutable hash table
  521. ; using <hash-function> as the hash function
  522. ; and <bucket-searcher>, e.g. ASSQ, ASSV, ASSOC, to search a bucket
  523. ; with <size> buckets at first, expanding the number of buckets as needed.
  524. ; The <hash-function> must accept a key and return a non-negative exact
  525. ; integer.
  526. ;
  527. ; (make-hashtable <hash-function> <bucket-searcher>)
  528. ;
  529. ; Equivalent to (make-hashtable <hash-function> <bucket-searcher> n)
  530. ; for some value of n chosen by the implementation.
  531. ;
  532. ; (make-hashtable <hash-function>)
  533. ;
  534. ; Equivalent to (make-hashtable <hash-function> assv).
  535. ;
  536. ; (make-hashtable)
  537. ;
  538. ; Equivalent to (make-hashtable object-hash assv).
  539. ;
  540. ; (hashtable-contains? <hashtable> <key>)
  541. ;
  542. ; Returns true iff the <hashtable> contains an entry for <key>.
  543. ;
  544. ; (hashtable-fetch <hashtable> <key> <flag>)
  545. ;
  546. ; Returns the value associated with <key> in the <hashtable> if the
  547. ; <hashtable> contains <key>; otherwise returns <flag>.
  548. ;
  549. ; (hashtable-get <hashtable> <key>)
  550. ;
  551. ; Equivalent to (hashtable-fetch <hashtable> <key> #f)
  552. ;
  553. ; (hashtable-put! <hashtable> <key> <value>)
  554. ;
  555. ; Changes the <hashtable> to associate <key> with <value>, replacing
  556. ; any existing association for <key>.
  557. ;
  558. ; (hashtable-remove! <hashtable> <key>)
  559. ;
  560. ; Removes any association for <key> within the <hashtable>.
  561. ;
  562. ; (hashtable-clear! <hashtable>)
  563. ;
  564. ; Removes all associations from the <hashtable>.
  565. ;
  566. ; (hashtable-size <hashtable>)
  567. ;
  568. ; Returns the number of keys contained within the <hashtable>.
  569. ;
  570. ; (hashtable-for-each <procedure> <hashtable>)
  571. ;
  572. ; The <procedure> must accept two arguments, a key and the value
  573. ; associated with that key. Calls the <procedure> once for each
  574. ; key-value association. The order of these calls is indeterminate.
  575. ;
  576. ; (hashtable-map <procedure> <hashtable>)
  577. ;
  578. ; The <procedure> must accept two arguments, a key and the value
  579. ; associated with that key. Calls the <procedure> once for each
  580. ; key-value association, and returns a list of the results. The
  581. ; order of the calls is indeterminate.
  582. ;
  583. ; (hashtable-copy <hashtable>)
  584. ;
  585. ; Returns a copy of the <hashtable>.
  586. ; These global variables are assigned new values later.
  587. (define make-hashtable (lambda args '*))
  588. (define hashtable-contains? (lambda (ht key) #f))
  589. (define hashtable-fetch (lambda (ht key flag) flag))
  590. (define hashtable-get (lambda (ht key) (hashtable-fetch ht key #f)))
  591. (define hashtable-put! (lambda (ht key val) '*))
  592. (define hashtable-remove! (lambda (ht key) '*))
  593. (define hashtable-clear! (lambda (ht) '*))
  594. (define hashtable-size (lambda (ht) 0))
  595. (define hashtable-for-each (lambda (ht proc) '*))
  596. (define hashtable-map (lambda (ht proc) '()))
  597. (define hashtable-copy (lambda (ht) ht))
  598. ; Implementation.
  599. ; A hashtable is represented as a vector of the form
  600. ;
  601. ; #(("HASHTABLE") <count> <hasher> <searcher> <buckets>)
  602. ;
  603. ; where <count> is the number of associations within the hashtable,
  604. ; <hasher> is the hash function, <searcher> is the bucket searcher,
  605. ; and <buckets> is a vector of buckets.
  606. ;
  607. ; The <hasher> and <searcher> fields are constant, but
  608. ; the <count> and <buckets> fields are mutable.
  609. ;
  610. ; For thread-safe operation, the mutators must modify both
  611. ; as an atomic operation. Other operations do not require
  612. ; critical sections provided VECTOR-REF is an atomic operation
  613. ; and the operation does not modify the hashtable, does not
  614. ; reference the <count> field, and fetches the <buckets>
  615. ; field exactly once.
  616. (let ((doc (list "HASHTABLE"))
  617. (count (lambda (ht) (vector-ref ht 1)))
  618. (count! (lambda (ht n) (vector-set! ht 1 n)))
  619. (hasher (lambda (ht) (vector-ref ht 2)))
  620. (searcher (lambda (ht) (vector-ref ht 3)))
  621. (buckets (lambda (ht) (vector-ref ht 4)))
  622. (buckets! (lambda (ht v) (vector-set! ht 4 v)))
  623. (defaultn 10))
  624. (let ((hashtable? (lambda (ht)
  625. (and (vector? ht)
  626. (= 5 (vector-length ht))
  627. (eq? doc (vector-ref ht 0)))))
  628. (hashtable-error (lambda (x)
  629. (display "ERROR: Bad hash table: ")
  630. (newline)
  631. (write x)
  632. (newline))))
  633. ; Internal operations.
  634. (define (make-ht hashfun searcher size)
  635. (vector doc 0 hashfun searcher (make-vector size '())))
  636. ; Substitute x for the first occurrence of y within the list z.
  637. ; y is known to occur within z.
  638. (define (substitute1 x y z)
  639. (cond ((eq? y (car z))
  640. (cons x (cdr z)))
  641. (else
  642. (cons (car z)
  643. (substitute1 x y (cdr z))))))
  644. ; Remove the first occurrence of x from y.
  645. ; x is known to occur within y.
  646. (define (remq1 x y)
  647. (cond ((eq? x (car y))
  648. (cdr y))
  649. (else
  650. (cons (car y)
  651. (remq1 x (cdr y))))))
  652. (define (resize ht0)
  653. (call-without-interrupts
  654. (lambda ()
  655. (let ((ht (make-ht (hasher ht0)
  656. (searcher ht0)
  657. (+ 1 (* 2 (count ht0))))))
  658. (ht-for-each (lambda (key val)
  659. (put! ht key val))
  660. ht0)
  661. (buckets! ht0 (buckets ht))))))
  662. ; Returns the contents of the hashtable as a vector of pairs.
  663. (define (contents ht)
  664. (let* ((v (buckets ht))
  665. (n (vector-length v))
  666. (z (make-vector (count ht) '())))
  667. (define (loop i bucket j)
  668. (if (null? bucket)
  669. (if (= i n)
  670. (if (= j (vector-length z))
  671. z
  672. (begin (display "BUG in hashtable")
  673. (newline)
  674. '#()))
  675. (loop (+ i 1)
  676. (vector-ref v i)
  677. j))
  678. (let ((entry (car bucket)))
  679. (vector-set! z j (cons (car entry) (cdr entry)))
  680. (loop i
  681. (cdr bucket)
  682. (+ j 1)))))
  683. (loop 0 '() 0)))
  684. (define (contains? ht key)
  685. (if (hashtable? ht)
  686. (let* ((v (buckets ht))
  687. (n (vector-length v))
  688. (h (modulo ((hasher ht) key) n))
  689. (b (vector-ref v h)))
  690. (if ((searcher ht) key b)
  691. #t
  692. #f))
  693. (hashtable-error ht)))
  694. (define (fetch ht key flag)
  695. (if (hashtable? ht)
  696. (let* ((v (buckets ht))
  697. (n (vector-length v))
  698. (h (modulo ((hasher ht) key) n))
  699. (b (vector-ref v h))
  700. (probe ((searcher ht) key b)))
  701. (if probe
  702. (cdr probe)
  703. flag))
  704. (hashtable-error ht)))
  705. (define (put! ht key val)
  706. (if (hashtable? ht)
  707. (call-without-interrupts
  708. (lambda ()
  709. (let* ((v (buckets ht))
  710. (n (vector-length v))
  711. (h (modulo ((hasher ht) key) n))
  712. (b (vector-ref v h))
  713. (probe ((searcher ht) key b)))
  714. (if probe
  715. ; Using SET-CDR! on the probe would make it necessary
  716. ; to synchronize the CONTENTS routine.
  717. (vector-set! v h (substitute1 (cons key val) probe b))
  718. (begin (count! ht (+ (count ht) 1))
  719. (vector-set! v h (cons (cons key val) b))
  720. (if (> (count ht) n)
  721. (resize ht)))))
  722. #f))
  723. (hashtable-error ht)))
  724. (define (remove! ht key)
  725. (if (hashtable? ht)
  726. (call-without-interrupts
  727. (lambda ()
  728. (let* ((v (buckets ht))
  729. (n (vector-length v))
  730. (h (modulo ((hasher ht) key) n))
  731. (b (vector-ref v h))
  732. (probe ((searcher ht) key b)))
  733. (if probe
  734. (begin (count! ht (- (count ht) 1))
  735. (vector-set! v h (remq1 probe b))
  736. (if (< (* 2 (+ defaultn (count ht))) n)
  737. (resize ht))))
  738. #f)))
  739. (hashtable-error ht)))
  740. (define (clear! ht)
  741. (if (hashtable? ht)
  742. (call-without-interrupts
  743. (lambda ()
  744. (begin (count! ht 0)
  745. (buckets! ht (make-vector defaultn '()))
  746. #f)))
  747. (hashtable-error ht)))
  748. (define (size ht)
  749. (if (hashtable? ht)
  750. (count ht)
  751. (hashtable-error ht)))
  752. ; This code must be written so that the procedure can modify the
  753. ; hashtable without breaking any invariants.
  754. (define (ht-for-each f ht)
  755. (if (hashtable? ht)
  756. (let* ((v (contents ht))
  757. (n (vector-length v)))
  758. (do ((j 0 (+ j 1)))
  759. ((= j n))
  760. (let ((x (vector-ref v j)))
  761. (f (car x) (cdr x)))))
  762. (hashtable-error ht)))
  763. (define (ht-map f ht)
  764. (if (hashtable? ht)
  765. (let* ((v (contents ht))
  766. (n (vector-length v)))
  767. (do ((j 0 (+ j 1))
  768. (results '() (let ((x (vector-ref v j)))
  769. (cons (f (car x) (cdr x))
  770. results))))
  771. ((= j n)
  772. (reverse results))))
  773. (hashtable-error ht)))
  774. (define (ht-copy ht)
  775. (if (hashtable? ht)
  776. (let* ((newtable (make-hashtable (hasher ht) (searcher ht) 0))
  777. (v (buckets ht))
  778. (n (vector-length v))
  779. (newvector (make-vector n '())))
  780. (count! newtable (count ht))
  781. (buckets! newtable newvector)
  782. (do ((i 0 (+ i 1)))
  783. ((= i n))
  784. (vector-set! newvector i (append (vector-ref v i) '())))
  785. newtable)
  786. (hashtable-error ht)))
  787. ; External entry points.
  788. (set! make-hashtable
  789. (lambda args
  790. (let* ((hashfun (if (null? args) object-hash (car args)))
  791. (searcher (if (or (null? args) (null? (cdr args)))
  792. assv
  793. (cadr args)))
  794. (size (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
  795. defaultn
  796. (caddr args))))
  797. (make-ht hashfun searcher size))))
  798. (set! hashtable-contains? (lambda (ht key) (contains? ht key)))
  799. (set! hashtable-fetch (lambda (ht key flag) (fetch ht key flag)))
  800. (set! hashtable-get (lambda (ht key) (fetch ht key #f)))
  801. (set! hashtable-put! (lambda (ht key val) (put! ht key val)))
  802. (set! hashtable-remove! (lambda (ht key) (remove! ht key)))
  803. (set! hashtable-clear! (lambda (ht) (clear! ht)))
  804. (set! hashtable-size (lambda (ht) (size ht)))
  805. (set! hashtable-for-each (lambda (ht proc) (ht-for-each ht proc)))
  806. (set! hashtable-map (lambda (ht proc) (ht-map ht proc)))
  807. (set! hashtable-copy (lambda (ht) (ht-copy ht)))
  808. #f))
  809. ; Hash trees: a functional data structure analogous to hash tables.
  810. ;
  811. ; (make-hashtree <hash-function> <bucket-searcher>)
  812. ;
  813. ; Returns a newly allocated mutable hash table
  814. ; using <hash-function> as the hash function
  815. ; and <bucket-searcher>, e.g. ASSQ, ASSV, ASSOC, to search a bucket.
  816. ; The <hash-function> must accept a key and return a non-negative exact
  817. ; integer.
  818. ;
  819. ; (make-hashtree <hash-function>)
  820. ;
  821. ; Equivalent to (make-hashtree <hash-function> assv).
  822. ;
  823. ; (make-hashtree)
  824. ;
  825. ; Equivalent to (make-hashtree object-hash assv).
  826. ;
  827. ; (hashtree-contains? <hashtree> <key>)
  828. ;
  829. ; Returns true iff the <hashtree> contains an entry for <key>.
  830. ;
  831. ; (hashtree-fetch <hashtree> <key> <flag>)
  832. ;
  833. ; Returns the value associated with <key> in the <hashtree> if the
  834. ; <hashtree> contains <key>; otherwise returns <flag>.
  835. ;
  836. ; (hashtree-get <hashtree> <key>)
  837. ;
  838. ; Equivalent to (hashtree-fetch <hashtree> <key> #f)
  839. ;
  840. ; (hashtree-put <hashtree> <key> <value>)
  841. ;
  842. ; Returns a new hashtree that is like <hashtree> except that
  843. ; <key> is associated with <value>.
  844. ;
  845. ; (hashtree-remove <hashtree> <key>)
  846. ;
  847. ; Returns a new hashtree that is like <hashtree> except that
  848. ; <key> is not associated with any value.
  849. ;
  850. ; (hashtree-size <hashtree>)
  851. ;
  852. ; Returns the number of keys contained within the <hashtree>.
  853. ;
  854. ; (hashtree-for-each <procedure> <hashtree>)
  855. ;
  856. ; The <procedure> must accept two arguments, a key and the value
  857. ; associated with that key. Calls the <procedure> once for each
  858. ; key-value association. The order of these calls is indeterminate.
  859. ;
  860. ; (hashtree-map <procedure> <hashtree>)
  861. ;
  862. ; The <procedure> must accept two arguments, a key and the value
  863. ; associated with that key. Calls the <procedure> once for each
  864. ; key-value association, and returns a list of the results. The
  865. ; order of the calls is indeterminate.
  866. ; These global variables are assigned new values later.
  867. (define make-hashtree (lambda args '*))
  868. (define hashtree-contains? (lambda (ht key) #f))
  869. (define hashtree-fetch (lambda (ht key flag) flag))
  870. (define hashtree-get (lambda (ht key) (hashtree-fetch ht key #f)))
  871. (define hashtree-put (lambda (ht key val) '*))
  872. (define hashtree-remove (lambda (ht key) '*))
  873. (define hashtree-size (lambda (ht) 0))
  874. (define hashtree-for-each (lambda (ht proc) '*))
  875. (define hashtree-map (lambda (ht proc) '()))
  876. ; Implementation.
  877. ; A hashtree is represented as a vector of the form
  878. ;
  879. ; #(("hashtree") <count> <hasher> <searcher> <buckets>)
  880. ;
  881. ; where <count> is the number of associations within the hashtree,
  882. ; <hasher> is the hash function, <searcher> is the bucket searcher,
  883. ; and <buckets> is generated by the following grammar:
  884. ;
  885. ; <buckets> ::= ()
  886. ; | (<fixnum> <associations> <buckets> <buckets>)
  887. ; <alist> ::= (<associations>)
  888. ; <associations> ::=
  889. ; | <association> <associations>
  890. ; <association> ::= (<key> . <value>)
  891. ;
  892. ; If <buckets> is of the form (n alist buckets1 buckets2),
  893. ; then n is the hash code of all keys in alist, all keys in buckets1
  894. ; have a hash code less than n, and all keys in buckets2 have a hash
  895. ; code greater than n.
  896. (let ((doc (list "hashtree"))
  897. (count (lambda (ht) (vector-ref ht 1)))
  898. (hasher (lambda (ht) (vector-ref ht 2)))
  899. (searcher (lambda (ht) (vector-ref ht 3)))
  900. (buckets (lambda (ht) (vector-ref ht 4)))
  901. (make-empty-buckets (lambda () '()))
  902. (make-buckets
  903. (lambda (h alist buckets1 buckets2)
  904. (list h alist buckets1 buckets2)))
  905. (buckets-empty? (lambda (buckets) (null? buckets)))
  906. (buckets-n (lambda (buckets) (car buckets)))
  907. (buckets-alist (lambda (buckets) (cadr buckets)))
  908. (buckets-left (lambda (buckets) (caddr buckets)))
  909. (buckets-right (lambda (buckets) (cadddr buckets))))
  910. (let ((hashtree? (lambda (ht)
  911. (and (vector? ht)
  912. (= 5 (vector-length ht))
  913. (eq? doc (vector-ref ht 0)))))
  914. (hashtree-error (lambda (x)
  915. (display "ERROR: Bad hash tree: ")
  916. (newline)
  917. (write x)
  918. (newline))))
  919. ; Internal operations.
  920. (define (make-ht count hashfun searcher buckets)
  921. (vector doc count hashfun searcher buckets))
  922. ; Substitute x for the first occurrence of y within the list z.
  923. ; y is known to occur within z.
  924. (define (substitute1 x y z)
  925. (cond ((eq? y (car z))
  926. (cons x (cdr z)))
  927. (else
  928. (cons (car z)
  929. (substitute1 x y (cdr z))))))
  930. ; Remove the first occurrence of x from y.
  931. ; x is known to occur within y.
  932. (define (remq1 x y)
  933. (cond ((eq? x (car y))
  934. (cdr y))
  935. (else
  936. (cons (car y)
  937. (remq1 x (cdr y))))))
  938. ; Returns the contents of the hashtree as a list of pairs.
  939. (define (contents ht)
  940. (let* ((t (buckets ht)))
  941. (define (contents t alist)
  942. (if (buckets-empty? t)
  943. alist
  944. (contents (buckets-left t)
  945. (contents (buckets-right t)
  946. (append-reverse (buckets-alist t)
  947. alist)))))
  948. (define (append-reverse x y)
  949. (if (null? x)
  950. y
  951. (append-reverse (cdr x)
  952. (cons (car x) y))))
  953. ; Creating a new hashtree from a list that is almost sorted
  954. ; in hash code order would create an extremely unbalanced
  955. ; hashtree, so this routine randomizes the order a bit.
  956. (define (randomize1 alist alist1 alist2 alist3)
  957. (if (null? alist)
  958. (randomize-combine alist1 alist2 alist3)
  959. (randomize2 (cdr alist)
  960. (cons (car alist) alist1)
  961. alist2
  962. alist3)))
  963. (define (randomize2 alist alist1 alist2 alist3)
  964. (if (null? alist)
  965. (randomize-combine alist1 alist2 alist3)
  966. (randomize3 (cdr alist)
  967. alist1
  968. (cons (car alist) alist2)
  969. alist3)))
  970. (define (randomize3 alist alist1 alist2 alist3)
  971. (if (null? alist)
  972. (randomize-combine alist1 alist2 alist3)
  973. (randomize1 (cdr alist)
  974. alist1
  975. alist2
  976. (cons (car alist) alist3))))
  977. (define (randomize-combine alist1 alist2 alist3)
  978. (cond ((null? alist2)
  979. alist1)
  980. ((null? alist3)
  981. (append-reverse alist2 alist1))
  982. (else
  983. (append-reverse
  984. (randomize1 alist3 '() '() '())
  985. (append-reverse
  986. (randomize1 alist1 '() '() '())
  987. (randomize1 alist2 '() '() '()))))))
  988. (randomize1 (contents t '()) '() '() '())))
  989. (define (contains? ht key)
  990. (if (hashtree? ht)
  991. (let* ((t (buckets ht))
  992. (h ((hasher ht) key)))
  993. (if ((searcher ht) key (find-bucket t h))
  994. #t
  995. #f))
  996. (hashtree-error ht)))
  997. (define (fetch ht key flag)
  998. (if (hashtree? ht)
  999. (let* ((t (buckets ht))
  1000. (h ((hasher ht) key))
  1001. (probe ((searcher ht) key (find-bucket t h))))
  1002. (if probe
  1003. (cdr probe)
  1004. flag))
  1005. (hashtree-error ht)))
  1006. ; Given a <buckets> t and a hash code h, returns the alist for h.
  1007. (define (find-bucket t h)
  1008. (if (buckets-empty? t)
  1009. '()
  1010. (let ((n (buckets-n t)))
  1011. (cond ((< h n)
  1012. (find-bucket (buckets-left t) h))
  1013. ((< n h)
  1014. (find-bucket (buckets-right t) h))
  1015. (else
  1016. (buckets-alist t))))))
  1017. (define (put ht key val)
  1018. (if (hashtree? ht)
  1019. (let ((t (buckets ht))
  1020. (h ((hasher ht) key))
  1021. (association (cons key val))
  1022. (c (count ht)))
  1023. (define (put t h)
  1024. (if (buckets-empty? t)
  1025. (begin (set! c (+ c 1))
  1026. (make-buckets h (list association) t t))
  1027. (let ((n (buckets-n t))
  1028. (alist (buckets-alist t))
  1029. (left (buckets-left t))
  1030. (right (buckets-right t)))
  1031. (cond ((< h n)
  1032. (make-buckets n
  1033. alist
  1034. (put (buckets-left t) h)
  1035. right))
  1036. ((< n h)
  1037. (make-buckets n
  1038. alist
  1039. left
  1040. (put (buckets-right t) h)))
  1041. (else
  1042. (let ((probe ((searcher ht) key alist)))
  1043. (if probe
  1044. (make-buckets n
  1045. (substitute1 association
  1046. probe
  1047. alist)
  1048. left
  1049. right)
  1050. (begin
  1051. (set! c (+ c 1))
  1052. (make-buckets n
  1053. (cons association alist)
  1054. left
  1055. right)))))))))
  1056. (let ((buckets (put t h)))
  1057. (make-ht c (hasher ht) (searcher ht) buckets)))
  1058. (hashtree-error ht)))
  1059. (define (remove ht key)
  1060. (if (hashtree? ht)
  1061. (let ((t (buckets ht))
  1062. (h ((hasher ht) key))
  1063. (c (count ht)))
  1064. (define (remove t h)
  1065. (if (buckets-empty? t)
  1066. t
  1067. (let ((n (buckets-n t))
  1068. (alist (buckets-alist t))
  1069. (left (buckets-left t))
  1070. (right (buckets-right t)))
  1071. (cond ((< h n)
  1072. (make-buckets n
  1073. alist
  1074. (remove left h)
  1075. right))
  1076. ((< n h)
  1077. (make-buckets n
  1078. alist
  1079. left
  1080. (remove right h)))
  1081. (else
  1082. (let ((probe ((searcher ht) key alist)))
  1083. (if probe
  1084. (begin (set! c (- c 1))
  1085. (make-buckets n
  1086. (remq1 probe alist)
  1087. left
  1088. right))
  1089. t)))))))
  1090. (let ((buckets (remove t h)))
  1091. (make-ht c (hasher ht) (searcher ht) buckets)))
  1092. (hashtree-error ht)))
  1093. (define (size ht)
  1094. (if (hashtree? ht)
  1095. (count ht)
  1096. (hashtree-error ht)))
  1097. (define (ht-for-each f ht)
  1098. (if (hashtree? ht)
  1099. (for-each (lambda (association)
  1100. (f (car association)
  1101. (cdr association)))
  1102. (contents ht))
  1103. (hashtree-error ht)))
  1104. (define (ht-map f ht)
  1105. (if (hashtree? ht)
  1106. (map (lambda (association)
  1107. (f (car association)
  1108. (cdr association)))
  1109. (contents ht))
  1110. (hashtree-error ht)))
  1111. ; External entry points.
  1112. (set! make-hashtree
  1113. (lambda args
  1114. (let* ((hashfun (if (null? args) object-hash (car args)))
  1115. (searcher (if (or (null? args) (null? (cdr args)))
  1116. assv
  1117. (cadr args))))
  1118. (make-ht 0 hashfun searcher (make-empty-buckets)))))
  1119. (set! hashtree-contains? (lambda (ht key) (contains? ht key)))
  1120. (set! hashtree-fetch (lambda (ht key flag) (fetch ht key flag)))
  1121. (set! hashtree-get (lambda (ht key) (fetch ht key #f)))
  1122. (set! hashtree-put (lambda (ht key val) (put ht key val)))
  1123. (set! hashtree-remove (lambda (ht key) (remove ht key)))
  1124. (set! hashtree-size (lambda (ht) (size ht)))
  1125. (set! hashtree-for-each (lambda (ht proc) (ht-for-each ht proc)))
  1126. (set! hashtree-map (lambda (ht proc) (ht-map ht proc)))
  1127. #f))
  1128. ; Copyright 1994 William Clinger
  1129. ;
  1130. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  1131. ;
  1132. ; 24 April 1999
  1133. ;
  1134. ; Compiler switches needed by Twobit.
  1135. (define make-twobit-flag)
  1136. (define display-twobit-flag)
  1137. (define make-twobit-flag
  1138. (lambda (name)
  1139. (define (twobit-warning)
  1140. (display "Error: incorrect arguments to ")
  1141. (write name)
  1142. (newline)
  1143. (reset))
  1144. (define (display-flag state)
  1145. (display (if state " + " " - "))
  1146. (display name)
  1147. (display " is ")
  1148. (display (if state "on" "off"))
  1149. (newline))
  1150. (let ((state #t))
  1151. (lambda args
  1152. (cond ((null? args) state)
  1153. ((and (null? (cdr args))
  1154. (boolean? (car args)))
  1155. (set! state (car args))
  1156. state)
  1157. ((and (null? (cdr args))
  1158. (eq? (car args) 'display))
  1159. (display-flag state))
  1160. (else (twobit-warning)))))))
  1161. (define (display-twobit-flag flag)
  1162. (flag 'display))
  1163. ; Debugging and convenience.
  1164. (define issue-warnings
  1165. (make-twobit-flag 'issue-warnings))
  1166. (define include-source-code
  1167. (make-twobit-flag 'include-source-code))
  1168. (define include-variable-names
  1169. (make-twobit-flag 'include-variable-names))
  1170. (define include-procedure-names
  1171. (make-twobit-flag 'include-procedure-names))
  1172. ; Space efficiency.
  1173. ; This switch isn't fully implemented yet. If it is true, then
  1174. ; Twobit will generate flat closures and will go to some trouble
  1175. ; to zero stale registers and stack slots.
  1176. ; Don't turn this switch off unless space is more important than speed.
  1177. (define avoid-space-leaks
  1178. (make-twobit-flag 'avoid-space-leaks))
  1179. ; Major optimizations.
  1180. (define integrate-usual-procedures
  1181. (make-twobit-flag 'integrate-usual-procedures))
  1182. (define control-optimization
  1183. (make-twobit-flag 'control-optimization))
  1184. (define parallel-assignment-optimization
  1185. (make-twobit-flag 'parallel-assignment-optimization))
  1186. (define lambda-optimization
  1187. (make-twobit-flag 'lambda-optimization))
  1188. (define benchmark-mode
  1189. (make-twobit-flag 'benchmark-mode))
  1190. (define benchmark-block-mode
  1191. (make-twobit-flag 'benchmark-block-mode))
  1192. (define global-optimization
  1193. (make-twobit-flag 'global-optimization))
  1194. (define interprocedural-inlining
  1195. (make-twobit-flag 'interprocedural-inlining))
  1196. (define interprocedural-constant-propagation
  1197. (make-twobit-flag 'interprocedural-constant-propagation))
  1198. (define common-subexpression-elimination
  1199. (make-twobit-flag 'common-subexpression-elimination))
  1200. (define representation-inference
  1201. (make-twobit-flag 'representation-inference))
  1202. (define local-optimization
  1203. (make-twobit-flag 'local-optimization))
  1204. ; For backwards compatibility, until I can change the code.
  1205. (define (ignore-space-leaks . args)
  1206. (if (null? args)
  1207. (not (avoid-space-leaks))
  1208. (avoid-space-leaks (not (car args)))))
  1209. (define lambda-optimizations lambda-optimization)
  1210. (define local-optimizations local-optimization)
  1211. (define (set-compiler-flags! how)
  1212. (case how
  1213. ((no-optimization)
  1214. (set-compiler-flags! 'standard)
  1215. (avoid-space-leaks #t)
  1216. (integrate-usual-procedures #f)
  1217. (control-optimization #f)
  1218. (parallel-assignment-optimization #f)
  1219. (lambda-optimization #f)
  1220. (benchmark-mode #f)
  1221. (benchmark-block-mode #f)
  1222. (global-optimization #f)
  1223. (interprocedural-inlining #f)
  1224. (interprocedural-constant-propagation #f)
  1225. (common-subexpression-elimination #f)
  1226. (representation-inference #f)
  1227. (local-optimization #f))
  1228. ((standard)
  1229. (issue-warnings #t)
  1230. (include-source-code #f)
  1231. (include-procedure-names #t)
  1232. (include-variable-names #t)
  1233. (avoid-space-leaks #f)
  1234. (runtime-safety-checking #t)
  1235. (integrate-usual-procedures #f)
  1236. (control-optimization #t)
  1237. (parallel-assignment-optimization #t)
  1238. (lambda-optimization #t)
  1239. (benchmark-mode #f)
  1240. (benchmark-block-mode #f)
  1241. (global-optimization #t)
  1242. (interprocedural-inlining #t)
  1243. (interprocedural-constant-propagation #t)
  1244. (common-subexpression-elimination #t)
  1245. (representation-inference #t)
  1246. (local-optimization #t))
  1247. ((fast-safe)
  1248. (let ((bbmode (benchmark-block-mode)))
  1249. (set-compiler-flags! 'standard)
  1250. (integrate-usual-procedures #t)
  1251. (benchmark-mode #t)
  1252. (benchmark-block-mode bbmode)))
  1253. ((fast-unsafe)
  1254. (set-compiler-flags! 'fast-safe)
  1255. (runtime-safety-checking #f))
  1256. (else
  1257. (error "set-compiler-flags!: unknown mode " how))))
  1258. (define (display-twobit-flags which)
  1259. (case which
  1260. ((debugging)
  1261. (display-twobit-flag issue-warnings)
  1262. (display-twobit-flag include-procedure-names)
  1263. (display-twobit-flag include-variable-names)
  1264. (display-twobit-flag include-source-code))
  1265. ((safety)
  1266. (display-twobit-flag avoid-space-leaks))
  1267. ((optimization)
  1268. (display-twobit-flag integrate-usual-procedures)
  1269. (display-twobit-flag control-optimization)
  1270. (display-twobit-flag parallel-assignment-optimization)
  1271. (display-twobit-flag lambda-optimization)
  1272. (display-twobit-flag benchmark-mode)
  1273. (display-twobit-flag benchmark-block-mode)
  1274. (display-twobit-flag global-optimization)
  1275. (if (global-optimization)
  1276. (begin (display " ")
  1277. (display-twobit-flag interprocedural-inlining)
  1278. (display " ")
  1279. (display-twobit-flag interprocedural-constant-propagation)
  1280. (display " ")
  1281. (display-twobit-flag common-subexpression-elimination)
  1282. (display " ")
  1283. (display-twobit-flag representation-inference)))
  1284. (display-twobit-flag local-optimization))
  1285. (else
  1286. ; The switch might mean something to the assembler, but not to Twobit
  1287. #t)))
  1288. ; eof
  1289. ; Copyright 1991 William Clinger
  1290. ;
  1291. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  1292. ;
  1293. ; 14 April 1999 / wdc
  1294. ($$trace "pass1.aux")
  1295. ;***************************************************************
  1296. ;
  1297. ; Each definition in this section should be overridden by an assignment
  1298. ; in a target-specific file.
  1299. ;
  1300. ; If a lambda expression has more than @maxargs-with-rest-arg@ required
  1301. ; arguments followed by a rest argument, then the macro expander will
  1302. ; rewrite the lambda expression as a lambda expression with only one
  1303. ; argument (a rest argument) whose body is a LET that binds the arguments
  1304. ; of the original lambda expression.
  1305. (define @maxargs-with-rest-arg@
  1306. 1000000) ; infinity
  1307. (define (prim-entry name) #f) ; no integrable procedures
  1308. (define (prim-arity name) 0) ; all of which take 0 arguments
  1309. (define (prim-opcodename name) name) ; and go by their source names
  1310. ; End of definitions to be overridden by target-specific assignments.
  1311. ;
  1312. ;***************************************************************
  1313. ; Miscellaneous routines.
  1314. (define (m-warn msg . more)
  1315. (if (issue-warnings)
  1316. (begin
  1317. (display "WARNING from macro expander:")
  1318. (newline)
  1319. (display msg)
  1320. (newline)
  1321. (for-each (lambda (x) (write x) (newline))
  1322. more))))
  1323. (define (m-error msg . more)
  1324. (display "ERROR detected during macro expansion:")
  1325. (newline)
  1326. (display msg)
  1327. (newline)
  1328. (for-each (lambda (x) (write x) (newline))
  1329. more)
  1330. (m-quit (make-constant #f)))
  1331. (define (m-bug msg . more)
  1332. (display "BUG in macro expander: ")
  1333. (newline)
  1334. (display msg)
  1335. (newline)
  1336. (for-each (lambda (x) (write x) (newline))
  1337. more)
  1338. (m-quit (make-constant #f)))
  1339. ; Given a <formals>, returns a list of bound variables.
  1340. '
  1341. (define (make-null-terminated x)
  1342. (cond ((null? x) '())
  1343. ((pair? x)
  1344. (cons (car x) (make-null-terminated (cdr x))))
  1345. (else (list x))))
  1346. ; Returns the length of the given list, or -1 if the argument
  1347. ; is not a list. Does not check for circular lists.
  1348. (define (safe-length x)
  1349. (define (loop x n)
  1350. (cond ((null? x) n)
  1351. ((pair? x) (loop (cdr x) (+ n 1)))
  1352. (else -1)))
  1353. (loop x 0))
  1354. ; Given a unary predicate and a list, returns a list of those
  1355. ; elements for which the predicate is true.
  1356. (define (filter1 p x)
  1357. (cond ((null? x) '())
  1358. ((p (car x)) (cons (car x) (filter1 p (cdr x))))
  1359. (else (filter1 p (cdr x)))))
  1360. ; Given a unary predicate and a list, returns #t if the
  1361. ; predicate is true of every element of the list.
  1362. (define (every1? p x)
  1363. (cond ((null? x) #t)
  1364. ((p (car x)) (every1? p (cdr x)))
  1365. (else #f)))
  1366. ; Binary union of two sets represented as lists, using equal?.
  1367. (define (union2 x y)
  1368. (cond ((null? x) y)
  1369. ((member (car x) y)
  1370. (union2 (cdr x) y))
  1371. (else (union2 (cdr x) (cons (car x) y)))))
  1372. ; Given an association list, copies the association pairs.
  1373. (define (copy-alist alist)
  1374. (map (lambda (x) (cons (car x) (cdr x)))
  1375. alist))
  1376. ; Removes a value from a list. May destroy the list.
  1377. '
  1378. (define remq!
  1379. (letrec ((loop (lambda (x y prev)
  1380. (cond ((null? y) #t)
  1381. ((eq? x (car y))
  1382. (set-cdr! prev (cdr y))
  1383. (loop x (cdr prev) prev))
  1384. (else
  1385. (loop x (cdr y) y))))))
  1386. (lambda (x y)
  1387. (cond ((null? y) '())
  1388. ((eq? x (car y))
  1389. (remq! x (cdr y)))
  1390. (else
  1391. (loop x (cdr y) y)
  1392. y)))))
  1393. ; Procedure-specific source code transformations.
  1394. ; The transformer is passed a source code expression and a predicate
  1395. ; and returns one of:
  1396. ;
  1397. ; the original source code expression
  1398. ; a new source code expression to use in place of the original
  1399. ; #f to indicate that the procedure is being called
  1400. ; with an incorrect number of arguments or
  1401. ; with an incorrect operand
  1402. ;
  1403. ; The original source code expression is guaranteed to be a list whose
  1404. ; car is the name associated with the transformer.
  1405. ; The predicate takes an identifier (a symbol) and returns true iff
  1406. ; that identifier is bound to something other than its global binding.
  1407. ;
  1408. ; Since the procedures and their transformations are target-specific,
  1409. ; they are defined in another file, in the Target subdirectory.
  1410. ; FIXME:
  1411. ; I think this is now used in only one place, in simplify-if.
  1412. (define (integrable? name)
  1413. (and (integrate-usual-procedures)
  1414. (prim-entry name)))
  1415. ; MAKE-READABLE strips the referencing information
  1416. ; and replaces (begin I) by I.
  1417. ; If the optional argument is true, then it also reconstructs LET.
  1418. (define (make-readable exp . rest)
  1419. (let ((fancy? (and (not (null? rest))
  1420. (car rest))))
  1421. (define (make-readable exp)
  1422. (case (car exp)
  1423. ((quote) (make-readable-quote exp))
  1424. ((lambda) `(lambda ,(lambda.args exp)
  1425. ,@(map (lambda (def)
  1426. `(define ,(def.lhs def)
  1427. ,(make-readable (def.rhs def))))
  1428. (lambda.defs exp))
  1429. ,(make-readable (lambda.body exp))))
  1430. ((set!) `(set! ,(assignment.lhs exp)
  1431. ,(make-readable (assignment.rhs exp))))
  1432. ((if) `(if ,(make-readable (if.test exp))
  1433. ,(make-readable (if.then exp))
  1434. ,(make-readable (if.else exp))))
  1435. ((begin) (if (variable? exp)
  1436. (variable.name exp)
  1437. `(begin ,@(map make-readable (begin.exprs exp)))))
  1438. (else (make-readable-call exp))))
  1439. (define (make-readable-quote exp)
  1440. (let ((x (constant.value exp)))
  1441. (if (and fancy?
  1442. (or (boolean? x)
  1443. (number? x)
  1444. (char? x)
  1445. (string? x)))
  1446. x
  1447. exp)))
  1448. (define (make-readable-call exp)
  1449. (let ((proc (call.proc exp)))
  1450. (if (and fancy?
  1451. (lambda? proc)
  1452. (list? (lambda.args proc)))
  1453. ;(make-readable-let* exp '() '() '())
  1454. (make-readable-let exp)
  1455. `(,(make-readable (call.proc exp))
  1456. ,@(map make-readable (call.args exp))))))
  1457. (define (make-readable-let exp)
  1458. (let* ((L (call.proc exp))
  1459. (formals (lambda.args L))
  1460. (args (map make-readable (call.args exp)))
  1461. (body (make-readable (lambda.body L))))
  1462. (if (and (null? (lambda.defs L))
  1463. (= (length args) 1)
  1464. (pair? body)
  1465. (or (and (eq? (car body) 'let)
  1466. (= (length (cadr body)) 1))
  1467. (eq? (car body) 'let*)))
  1468. `(let* ((,(car formals) ,(car args))
  1469. ,@(cadr body))
  1470. ,@(cddr body))
  1471. `(let ,(map list
  1472. (lambda.args L)
  1473. args)
  1474. ,@(map (lambda (def)
  1475. `(define ,(def.lhs def)
  1476. ,(make-readable (def.rhs def))))
  1477. (lambda.defs L))
  1478. ,body))))
  1479. (define (make-readable-let* exp vars inits defs)
  1480. (if (and (null? defs)
  1481. (call? exp)
  1482. (lambda? (call.proc exp))
  1483. (= 1 (length (lambda.args (call.proc exp)))))
  1484. (let ((proc (call.proc exp))
  1485. (arg (car (call.args exp))))
  1486. (if (and (call? arg)
  1487. (lambda? (call.proc arg))
  1488. (= 1 (length (lambda.args (call.proc arg))))
  1489. (null? (lambda.defs (call.proc arg))))
  1490. (make-readable-let*
  1491. (make-call proc (list (lambda.body (call.proc arg))))
  1492. (cons (car (lambda.args (call.proc arg))) vars)
  1493. (cons (make-readable (car (call.args arg))) inits)
  1494. '())
  1495. (make-readable-let* (lambda.body proc)
  1496. (cons (car (lambda.args proc)) vars)
  1497. (cons (make-readable (car (call.args exp)))
  1498. inits)
  1499. (map (lambda (def)
  1500. `(define ,(def.lhs def)
  1501. ,(make-readable (def.rhs def))))
  1502. (reverse (lambda.defs proc))))))
  1503. (cond ((or (not (null? vars))
  1504. (not (null? defs)))
  1505. `(let* ,(map list
  1506. (reverse vars)
  1507. (reverse inits))
  1508. ,@defs
  1509. ,(make-readable exp)))
  1510. ((and (call? exp)
  1511. (lambda? (call.proc exp)))
  1512. (let ((proc (call.proc exp)))
  1513. `(let ,(map list
  1514. (lambda.args proc)
  1515. (map make-readable (call.args exp)))
  1516. ,@(map (lambda (def)
  1517. `(define ,(def.lhs def)
  1518. ,(make-readable (def.rhs def))))
  1519. (lambda.defs proc))
  1520. ,(make-readable (lambda.body proc)))))
  1521. (else
  1522. (make-readable exp)))))
  1523. (make-readable exp)))
  1524. ; For testing.
  1525. ; MAKE-UNREADABLE does the reverse.
  1526. ; It assumes there are no internal definitions.
  1527. (define (make-unreadable exp)
  1528. (cond ((symbol? exp) (list 'begin exp))
  1529. ((pair? exp)
  1530. (case (car exp)
  1531. ((quote) exp)
  1532. ((lambda) (list 'lambda
  1533. (cadr exp)
  1534. '(begin)
  1535. (list '() '() '() '())
  1536. (make-unreadable (cons 'begin (cddr exp)))))
  1537. ((set!) (list 'set! (cadr exp) (make-unreadable (caddr exp))))
  1538. ((if) (list 'if
  1539. (make-unreadable (cadr exp))
  1540. (make-unreadable (caddr exp))
  1541. (if (= (length exp) 3)
  1542. '(unspecified)
  1543. (make-unreadable (cadddr exp)))))
  1544. ((begin) (if (= (length exp) 2)
  1545. (make-unreadable (cadr exp))
  1546. (cons 'begin (map make-unreadable (cdr exp)))))
  1547. (else (map make-unreadable exp))))
  1548. (else (list 'quote exp))))
  1549. ; Copyright 1991 William D Clinger.
  1550. ;
  1551. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  1552. ;
  1553. ; 12 April 1999.
  1554. ;
  1555. ; Procedures for fetching and clobbering parts of expressions.
  1556. ($$trace "pass2.aux")
  1557. (define (constant? exp) (eq? (car exp) 'quote))
  1558. (define (variable? exp)
  1559. (and (eq? (car exp) 'begin)
  1560. (null? (cddr exp))))
  1561. (define (lambda? exp) (eq? (car exp) 'lambda))
  1562. (define (call? exp) (pair? (car exp)))
  1563. (define (assignment? exp) (eq? (car exp) 'set!))
  1564. (define (conditional? exp) (eq? (car exp) 'if))
  1565. (define (begin? exp)
  1566. (and (eq? (car exp) 'begin)
  1567. (not (null? (cddr exp)))))
  1568. (define (make-constant value) (list 'quote value))
  1569. (define (make-variable name) (list 'begin name))
  1570. (define (make-lambda formals defs R F G decls doc body)
  1571. (list 'lambda
  1572. formals
  1573. (cons 'begin defs)
  1574. (list 'quote (list R F G decls doc))
  1575. body))
  1576. (define (make-call proc args) (cons proc (append args '())))
  1577. (define (make-assignment lhs rhs) (list 'set! lhs rhs))
  1578. (define (make-conditional e0 e1 e2) (list 'if e0 e1 e2))
  1579. (define (make-begin exprs)
  1580. (if (null? (cdr exprs))
  1581. (car exprs)
  1582. (cons 'begin (append exprs '()))))
  1583. (define (make-definition lhs rhs) (list 'define lhs rhs))
  1584. (define (constant.value exp) (cadr exp))
  1585. (define (variable.name exp) (cadr exp))
  1586. (define (lambda.args exp) (cadr exp))
  1587. (define (lambda.defs exp) (cdr (caddr exp)))
  1588. (define (lambda.R exp) (car (cadr (cadddr exp))))
  1589. (define (lambda.F exp) (cadr (cadr (cadddr exp))))
  1590. (define (lambda.G exp) (caddr (cadr (cadddr exp))))
  1591. (define (lambda.decls exp) (cadddr (cadr (cadddr exp))))
  1592. (define (lambda.doc exp) (car (cddddr (cadr (cadddr exp)))))
  1593. (define (lambda.body exp) (car (cddddr exp)))
  1594. (define (call.proc exp) (car exp))
  1595. (define (call.args exp) (cdr exp))
  1596. (define (assignment.lhs exp) (cadr exp))
  1597. (define (assignment.rhs exp) (caddr exp))
  1598. (define (if.test exp) (cadr exp))
  1599. (define (if.then exp) (caddr exp))
  1600. (define (if.else exp) (cadddr exp))
  1601. (define (begin.exprs exp) (cdr exp))
  1602. (define (def.lhs exp) (cadr exp))
  1603. (define (def.rhs exp) (caddr exp))
  1604. (define (variable-set! exp newexp)
  1605. (set-car! exp (car newexp))
  1606. (set-cdr! exp (append (cdr newexp) '())))
  1607. (define (lambda.args-set! exp args) (set-car! (cdr exp) args))
  1608. (define (lambda.defs-set! exp defs) (set-cdr! (caddr exp) defs))
  1609. (define (lambda.R-set! exp R) (set-car! (cadr (cadddr exp)) R))
  1610. (define (lambda.F-set! exp F) (set-car! (cdr (cadr (cadddr exp))) F))
  1611. (define (lambda.G-set! exp G) (set-car! (cddr (cadr (cadddr exp))) G))
  1612. (define (lambda.decls-set! exp decls) (set-car! (cdddr (cadr (cadddr exp))) decls))
  1613. (define (lambda.doc-set! exp doc) (set-car! (cddddr (cadr (cadddr exp))) doc))
  1614. (define (lambda.body-set! exp exp0) (set-car! (cddddr exp) exp0))
  1615. (define (call.proc-set! exp exp0) (set-car! exp exp0))
  1616. (define (call.args-set! exp exprs) (set-cdr! exp exprs))
  1617. (define (assignment.rhs-set! exp exp0) (set-car! (cddr exp) exp0))
  1618. (define (if.test-set! exp exp0) (set-car! (cdr exp) exp0))
  1619. (define (if.then-set! exp exp0) (set-car! (cddr exp) exp0))
  1620. (define (if.else-set! exp exp0) (set-car! (cdddr exp) exp0))
  1621. (define (begin.exprs-set! exp exprs) (set-cdr! exp exprs))
  1622. (define expression-set! variable-set!) ; used only by pass 3
  1623. ; FIXME: This duplicates information in Lib/procinfo.sch.
  1624. (define (make-doc name arity formals source-code filename filepos)
  1625. (vector name source-code arity filename filepos formals))
  1626. (define (doc.name d) (vector-ref d 0))
  1627. (define (doc.code d) (vector-ref d 1))
  1628. (define (doc.arity d) (vector-ref d 2))
  1629. (define (doc.file d) (vector-ref d 3))
  1630. (define (doc.filepos d) (vector-ref d 4))
  1631. (define (doc.formals d) (vector-ref d 5))
  1632. (define (doc.name-set! d x) (if d (vector-set! d 0 x)))
  1633. (define (doc.code-set! d x) (if d (vector-set! d 1 x)))
  1634. (define (doc.arity-set! d x) (if d (vector-set! d 2 x)))
  1635. (define (doc.file-set! d x) (if d (vector-set! d 3 x)))
  1636. (define (doc.filepos-set! d x) (if d (vector-set! d 4 x)))
  1637. (define (doc.formals-set! d x) (if d (vector-set! d 5 x)))
  1638. (define (doc-copy d) (list->vector (vector->list d)))
  1639. (define (ignored? name) (eq? name name:IGNORED))
  1640. ; Fairly harmless bug: rest arguments aren't getting flagged.
  1641. (define (flag-as-ignored name L)
  1642. (define (loop name formals)
  1643. (cond ((null? formals)
  1644. ;(pass2-error p2error:violation-of-invariant name formals)
  1645. #t)
  1646. ((symbol? formals) #t)
  1647. ((eq? name (car formals))
  1648. (set-car! formals name:IGNORED)
  1649. (if (not (local? (lambda.R L) name:IGNORED))
  1650. (lambda.R-set! L
  1651. (cons (make-R-entry name:IGNORED '() '() '())
  1652. (lambda.R L)))))
  1653. (else (loop name (cdr formals)))))
  1654. (loop name (lambda.args L)))
  1655. (define (make-null-terminated formals)
  1656. (cond ((null? formals) '())
  1657. ((symbol? formals) (list formals))
  1658. (else (cons (car formals)
  1659. (make-null-terminated (cdr formals))))))
  1660. (define (list-head x n)
  1661. (cond ((zero? n) '())
  1662. (else (cons (car x) (list-head (cdr x) (- n 1))))))
  1663. (define (remq x y)
  1664. (cond ((null? y) '())
  1665. ((eq? x (car y)) (remq x (cdr y)))
  1666. (else (cons (car y) (remq x (cdr y))))))
  1667. (define (make-call-to-LIST args)
  1668. (cond ((null? args) (make-constant '()))
  1669. ((null? (cdr args))
  1670. (make-call (make-variable name:CONS)
  1671. (list (car args) (make-constant '()))))
  1672. (else (make-call (make-variable name:LIST) args))))
  1673. (define (pass2-error i . etc)
  1674. (apply cerror (cons (vector-ref pass2-error-messages i) etc)))
  1675. (define pass2-error-messages
  1676. '#("System error: violation of an invariant in pass 2"
  1677. "Wrong number of arguments to known procedure"))
  1678. (define p2error:violation-of-invariant 0)
  1679. (define p2error:wna 1)
  1680. ; Procedures for fetching referencing information from R-tables.
  1681. (define (make-R-entry name refs assigns calls)
  1682. (list name refs assigns calls))
  1683. (define (R-entry.name x) (car x))
  1684. (define (R-entry.references x) (cadr x))
  1685. (define (R-entry.assignments x) (caddr x))
  1686. (define (R-entry.calls x) (cadddr x))
  1687. (define (R-entry.references-set! x refs) (set-car! (cdr x) refs))
  1688. (define (R-entry.assignments-set! x assignments) (set-car! (cddr x) assignments))
  1689. (define (R-entry.calls-set! x calls) (set-car! (cdddr x) calls))
  1690. (define (local? R I)
  1691. (assq I R))
  1692. (define (R-entry R I)
  1693. (assq I R))
  1694. (define (R-lookup R I)
  1695. (or (assq I R)
  1696. (pass2-error p2error:violation-of-invariant R I)))
  1697. (define (references R I)
  1698. (cadr (R-lookup R I)))
  1699. (define (assignments R I)
  1700. (caddr (R-lookup R I)))
  1701. (define (calls R I)
  1702. (cadddr (R-lookup R I)))
  1703. (define (references-set! R I X)
  1704. (set-car! (cdr (R-lookup R I)) X))
  1705. (define (assignments-set! R I X)
  1706. (set-car! (cddr (R-lookup R I)) X))
  1707. (define (calls-set! R I X)
  1708. (set-car! (cdddr (R-lookup R I)) X))
  1709. ; A notepad is a vector of the form #(L0 (L1 ...) (L2 ...) (I ...)),
  1710. ; where the components are:
  1711. ; element 0: a parent lambda expression (or #f if there is no enclosing
  1712. ; parent, or we want to pretend that there isn't).
  1713. ; element 1: a list of lambda expressions that the parent lambda
  1714. ; expression encloses immediately.
  1715. ; element 2: a subset of that list that does not escape.
  1716. ; element 3: a list of free variables.
  1717. (define (make-notepad L)
  1718. (vector L '() '() '()))
  1719. (define (notepad.parent np) (vector-ref np 0))
  1720. (define (notepad.lambdas np) (vector-ref np 1))
  1721. (define (notepad.nonescaping np) (vector-ref np 2))
  1722. (define (notepad.vars np) (vector-ref np 3))
  1723. (define (notepad.lambdas-set! np x) (vector-set! np 1 x))
  1724. (define (notepad.nonescaping-set! np x) (vector-set! np 2 x))
  1725. (define (notepad.vars-set! np x) (vector-set! np 3 x))
  1726. (define (notepad-lambda-add! np L)
  1727. (notepad.lambdas-set! np (cons L (notepad.lambdas np))))
  1728. (define (notepad-nonescaping-add! np L)
  1729. (notepad.nonescaping-set! np (cons L (notepad.nonescaping np))))
  1730. (define (notepad-var-add! np I)
  1731. (let ((vars (notepad.vars np)))
  1732. (if (not (memq I vars))
  1733. (notepad.vars-set! np (cons I vars)))))
  1734. ; Given a notepad, returns the list of variables that are closed
  1735. ; over by some nested lambda expression that escapes.
  1736. (define (notepad-captured-variables np)
  1737. (let ((nonescaping (notepad.nonescaping np)))
  1738. (apply-union
  1739. (map (lambda (L)
  1740. (if (memq L nonescaping)
  1741. (lambda.G L)
  1742. (lambda.F L)))
  1743. (notepad.lambdas np)))))
  1744. ; Given a notepad, returns a list of free variables computed
  1745. ; as the union of the immediate free variables with the free
  1746. ; variables of nested lambda expressions.
  1747. (define (notepad-free-variables np)
  1748. (do ((lambdas (notepad.lambdas np) (cdr lambdas))
  1749. (fv (notepad.vars np)
  1750. (let ((L (car lambdas)))
  1751. (union (difference (lambda.F L)
  1752. (make-null-terminated (lambda.args L)))
  1753. fv))))
  1754. ((null? lambdas) fv)))
  1755. ; Copyright 1992 William Clinger
  1756. ;
  1757. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  1758. ;
  1759. ; 13 December 1998
  1760. ; Implementation-dependent parameters and preferences that determine
  1761. ; how identifiers are represented in the output of the macro expander.
  1762. ;
  1763. ; The basic problem is that there are no reserved words, so the
  1764. ; syntactic keywords of core Scheme that are used to express the
  1765. ; output need to be represented by data that cannot appear in the
  1766. ; input. This file defines those data.
  1767. ($$trace "prefs")
  1768. ; FIXME: The following definitions are currently ignored.
  1769. ; The following definitions assume that identifiers of mixed case
  1770. ; cannot appear in the input.
  1771. (define begin1 (string->symbol "Begin"))
  1772. (define define1 (string->symbol "Define"))
  1773. (define quote1 (string->symbol "Quote"))
  1774. (define lambda1 (string->symbol "Lambda"))
  1775. (define if1 (string->symbol "If"))
  1776. (define set!1 (string->symbol "Set!"))
  1777. ; The following defines an implementation-dependent expression
  1778. ; that evaluates to an undefined (not unspecified!) value, for
  1779. ; use in expanding the (define x) syntax.
  1780. (define undefined1 (list (string->symbol "Undefined")))
  1781. ; End of FIXME.
  1782. ; A variable is renamed by suffixing a vertical bar followed by a unique
  1783. ; integer. In IEEE and R4RS Scheme, a vertical bar cannot appear as part
  1784. ; of an identifier, but presumably this is enforced by the reader and not
  1785. ; by the compiler. Any other character that cannot appear as part of an
  1786. ; identifier may be used instead of the vertical bar.
  1787. (define renaming-prefix-character #\.)
  1788. (define renaming-suffix-character #\|)
  1789. (define renaming-prefix (string renaming-prefix-character))
  1790. (define renaming-suffix (string renaming-suffix-character))
  1791. ; Patches for Twobit. Here temporarily.
  1792. (define (make-toplevel-definition id exp)
  1793. (if (lambda? exp)
  1794. (doc.name-set! (lambda.doc exp) id))
  1795. (make-begin
  1796. (list (make-assignment id exp)
  1797. (make-constant id))))
  1798. (define (make-undefined)
  1799. (make-call (make-variable 'undefined) '()))
  1800. (define (make-unspecified)
  1801. (make-call (make-variable 'unspecified) '()))
  1802. ; Copyright 1992 William Clinger
  1803. ;
  1804. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  1805. ;
  1806. ; 9 December 1998
  1807. ; Syntactic environments.
  1808. ;
  1809. ; A syntactic environment maps identifiers to denotations,
  1810. ; where a denotation is one of
  1811. ;
  1812. ; (special <special>)
  1813. ; (macro <rules> <env>)
  1814. ; (inline <rules> <env>)
  1815. ; (identifier <id> <references> <assignments> <calls>)
  1816. ;
  1817. ; and where <special> is one of
  1818. ;
  1819. ; quote
  1820. ; lambda
  1821. ; if
  1822. ; set!
  1823. ; begin
  1824. ; define
  1825. ; define-syntax
  1826. ; let-syntax
  1827. ; letrec-syntax
  1828. ; syntax-rules
  1829. ;
  1830. ; and where <rules> is a compiled <transformer spec> (see R4RS),
  1831. ; <env> is a syntactic environment, and <id> is an identifier.
  1832. ;
  1833. ; An inline denotation is like a macro denotation, except that it
  1834. ; is not an error when none of the rules match the use. Inline
  1835. ; denotations are created by DEFINE-INLINE.
  1836. ; The standard syntactic environment should not include any
  1837. ; identifier denotations; space leaks will result if it does.
  1838. ($$trace "syntaxenv")
  1839. (define standard-syntactic-environment
  1840. `((quote . (special quote))
  1841. (lambda . (special lambda))
  1842. (if . (special if))
  1843. (set! . (special set!))
  1844. (begin . (special begin))
  1845. (define . (special define))
  1846. (define-inline . (special define-inline))
  1847. (define-syntax . (special define-syntax))
  1848. (let-syntax . (special let-syntax))
  1849. (letrec-syntax . (special letrec-syntax))
  1850. (syntax-rules . (special syntax-rules))
  1851. ))
  1852. ; Unforgeable synonyms for lambda and set!, used to expand definitions.
  1853. (define lambda0 (string->symbol " lambda "))
  1854. (define set!0 (string->symbol " set! "))
  1855. (define (syntactic-copy env)
  1856. (copy-alist env))
  1857. (define (make-basic-syntactic-environment)
  1858. (cons (cons lambda0
  1859. (cdr (assq 'lambda standard-syntactic-environment)))
  1860. (cons (cons set!0
  1861. (cdr (assq 'set! standard-syntactic-environment)))
  1862. (syntactic-copy standard-syntactic-environment))))
  1863. ; The global-syntactic-environment will always be a nonempty
  1864. ; association list since there is no way to remove the entry
  1865. ; for lambda0. That entry is used as a header by destructive
  1866. ; operations.
  1867. (define global-syntactic-environment
  1868. (make-basic-syntactic-environment))
  1869. (define (global-syntactic-environment-set! env)
  1870. (set-cdr! global-syntactic-environment env)
  1871. #t)
  1872. (define (syntactic-bind-globally! id denotation)
  1873. (if (and (identifier-denotation? denotation)
  1874. (eq? id (identifier-name denotation)))
  1875. (letrec ((remove-bindings-for-id
  1876. (lambda (bindings)
  1877. (cond ((null? bindings) '())
  1878. ((eq? (caar bindings) id)
  1879. (remove-bindings-for-id (cdr bindings)))
  1880. (else (cons (car bindings)
  1881. (remove-bindings-for-id (cdr bindings))))))))
  1882. (global-syntactic-environment-set!
  1883. (remove-bindings-for-id (cdr global-syntactic-environment))))
  1884. (let ((x (assq id global-syntactic-environment)))
  1885. (if x
  1886. (begin (set-cdr! x denotation) #t)
  1887. (global-syntactic-environment-set!
  1888. (cons (cons id denotation)
  1889. (cdr global-syntactic-environment)))))))
  1890. (define (syntactic-divert env1 env2)
  1891. (append env2 env1))
  1892. (define (syntactic-extend env ids denotations)
  1893. (syntactic-divert env (map cons ids denotations)))
  1894. (define (syntactic-lookup env id)
  1895. (let ((entry (assq id env)))
  1896. (if entry
  1897. (cdr entry)
  1898. (make-identifier-denotation id))))
  1899. (define (syntactic-assign! env id denotation)
  1900. (let ((entry (assq id env)))
  1901. (if entry
  1902. (set-cdr! entry denotation)
  1903. (m-bug "Bug detected in syntactic-assign!" env id denotation))))
  1904. ; Denotations.
  1905. (define denotation-class car)
  1906. (define (special-denotation? denotation)
  1907. (eq? (denotation-class denotation) 'special))
  1908. (define (macro-denotation? denotation)
  1909. (eq? (denotation-class denotation) 'macro))
  1910. (define (inline-denotation? denotation)
  1911. (eq? (denotation-class denotation) 'inline))
  1912. (define (identifier-denotation? denotation)
  1913. (eq? (denotation-class denotation) 'identifier))
  1914. (define (make-macro-denotation rules env)
  1915. (list 'macro rules env))
  1916. (define (make-inline-denotation id rules env)
  1917. (list 'inline rules env id))
  1918. (define (make-identifier-denotation id)
  1919. (list 'identifier id '() '() '()))
  1920. (define macro-rules cadr)
  1921. (define macro-env caddr)
  1922. (define inline-rules macro-rules)
  1923. (define inline-env macro-env)
  1924. (define inline-name cadddr)
  1925. (define identifier-name cadr)
  1926. (define identifier-R-entry cdr)
  1927. (define (same-denotation? d1 d2)
  1928. (or (eq? d1 d2)
  1929. (and (identifier-denotation? d1)
  1930. (identifier-denotation? d2)
  1931. (eq? (identifier-name d1)
  1932. (identifier-name d2)))))
  1933. (define denotation-of-quote
  1934. (syntactic-lookup standard-syntactic-environment 'quote))
  1935. (define denotation-of-lambda
  1936. (syntactic-lookup standard-syntactic-environment 'lambda))
  1937. (define denotation-of-if
  1938. (syntactic-lookup standard-syntactic-environment 'if))
  1939. (define denotation-of-set!
  1940. (syntactic-lookup standard-syntactic-environment 'set!))
  1941. (define denotation-of-begin
  1942. (syntactic-lookup standard-syntactic-environment 'begin))
  1943. (define denotation-of-define
  1944. (syntactic-lookup standard-syntactic-environment 'define))
  1945. (define denotation-of-define-inline
  1946. (syntactic-lookup standard-syntactic-environment 'define-inline))
  1947. (define denotation-of-define-syntax
  1948. (syntactic-lookup standard-syntactic-environment 'define-syntax))
  1949. (define denotation-of-let-syntax
  1950. (syntactic-lookup standard-syntactic-environment 'let-syntax))
  1951. (define denotation-of-letrec-syntax
  1952. (syntactic-lookup standard-syntactic-environment 'letrec-syntax))
  1953. (define denotation-of-syntax-rules
  1954. (syntactic-lookup standard-syntactic-environment 'syntax-rules))
  1955. (define denotation-of-...
  1956. (syntactic-lookup standard-syntactic-environment '...))
  1957. (define denotation-of-transformer
  1958. (syntactic-lookup standard-syntactic-environment 'transformer))
  1959. ; Given a syntactic environment env to be extended, an alist returned
  1960. ; by rename-vars, and a syntactic environment env2, extends env by
  1961. ; binding the fresh identifiers to the denotations of the original
  1962. ; identifiers in env2.
  1963. (define (syntactic-alias env alist env2)
  1964. (syntactic-divert
  1965. env
  1966. (map (lambda (name-pair)
  1967. (let ((old-name (car name-pair))
  1968. (new-name (cdr name-pair)))
  1969. (cons new-name
  1970. (syntactic-lookup env2 old-name))))
  1971. alist)))
  1972. ; Given a syntactic environment and an alist returned by rename-vars,
  1973. ; extends the environment by binding the old identifiers to the fresh
  1974. ; identifiers.
  1975. ; For Twobit, it also binds the fresh identifiers to their denotations.
  1976. ; This is ok so long as the fresh identifiers are not legal Scheme
  1977. ; identifiers.
  1978. (define (syntactic-rename env alist)
  1979. (if (null? alist)
  1980. env
  1981. (let* ((old (caar alist))
  1982. (new (cdar alist))
  1983. (denotation (make-identifier-denotation new)))
  1984. (syntactic-rename
  1985. (cons (cons old denotation)
  1986. (cons (cons new denotation)
  1987. env))
  1988. (cdr alist)))))
  1989. ; Renaming of variables.
  1990. (define renaming-counter 0)
  1991. (define (make-rename-procedure)
  1992. (set! renaming-counter (+ renaming-counter 1))
  1993. (let ((suffix (string-append renaming-suffix (number->string renaming-counter))))
  1994. (lambda (sym)
  1995. (if (symbol? sym)
  1996. (let ((s (symbol->string sym)))
  1997. (if (and (positive? (string-length s))
  1998. (char=? (string-ref s 0) renaming-prefix-character))
  1999. (string->symbol (string-append s suffix))
  2000. (string->symbol (string-append renaming-prefix s suffix))))
  2001. (m-warn "Illegal use of rename procedure" 'ok:FIXME sym)))))
  2002. ; Given a datum, strips the suffixes from any symbols that appear within
  2003. ; the datum, trying not to copy any more of the datum than necessary.
  2004. (define (m-strip x)
  2005. (define (original-symbol x)
  2006. (define (loop sym s i n)
  2007. (cond ((= i n) sym)
  2008. ((char=? (string-ref s i)
  2009. renaming-suffix-character)
  2010. (string->symbol (substring s 1 i)))
  2011. (else
  2012. (loop sym s (+ i 1) n))))
  2013. (let ((s (symbol->string x)))
  2014. (if (and (positive? (string-length s))
  2015. (char=? (string-ref s 0) renaming-prefix-character))
  2016. (loop x s 0 (string-length s))
  2017. x)))
  2018. (cond ((symbol? x)
  2019. (original-symbol x))
  2020. ((pair? x)
  2021. (let ((a (m-strip (car x)))
  2022. (b (m-strip (cdr x))))
  2023. (if (and (eq? a (car x))
  2024. (eq? b (cdr x)))
  2025. x
  2026. (cons a b))))
  2027. ((vector? x)
  2028. (let* ((v (vector->list x))
  2029. (v2 (map m-strip v)))
  2030. (if (equal? v v2)
  2031. x
  2032. (list->vector v2))))
  2033. (else x)))
  2034. ; Given a list of identifiers, or a formal parameter "list",
  2035. ; returns an alist that associates each identifier with a fresh identifier.
  2036. (define (rename-vars original-vars)
  2037. (let ((rename (make-rename-procedure)))
  2038. (define (loop vars newvars)
  2039. (cond ((null? vars) (reverse newvars))
  2040. ((pair? vars)
  2041. (let ((var (car vars)))
  2042. (if (symbol? var)
  2043. (loop (cdr vars)
  2044. (cons (cons var (rename var))
  2045. newvars))
  2046. (m-error "Illegal variable" var))))
  2047. ((symbol? vars)
  2048. (loop (list vars) newvars))
  2049. (else (m-error "Malformed parameter list" original-vars))))
  2050. (loop original-vars '())))
  2051. ; Given a <formals> and an alist returned by rename-vars that contains
  2052. ; a new name for each formal identifier in <formals>, renames the
  2053. ; formal identifiers.
  2054. (define (rename-formals formals alist)
  2055. (cond ((null? formals) '())
  2056. ((pair? formals)
  2057. (cons (cdr (assq (car formals) alist))
  2058. (rename-formals (cdr formals) alist)))
  2059. (else (cdr (assq formals alist)))))
  2060. ; Copyright 1992 William Clinger
  2061. ;
  2062. ; Permission to copy this software, in whole or in part, to use this
  2063. ; software for any lawful purpose, and to redistribute this software
  2064. ; is granted subject to the restriction that all copies made of this
  2065. ; software must include this copyright notice in full.
  2066. ;
  2067. ; I also request that you send me a copy of any improvements that you
  2068. ; make to this software so that they may be incorporated within it to
  2069. ; the benefit of the Scheme community.
  2070. ;
  2071. ; 23 November 1998
  2072. ; Compiler for a <transformer spec>.
  2073. ;
  2074. ; References:
  2075. ;
  2076. ; The Revised^4 Report on the Algorithmic Language Scheme.
  2077. ; Clinger and Rees [editors]. To appear in Lisp Pointers.
  2078. ; Also available as a technical report from U of Oregon,
  2079. ; MIT AI Lab, and Cornell.
  2080. ;
  2081. ; Macros That Work. Clinger and Rees. POPL '91.
  2082. ;
  2083. ; The input is a <transformer spec> and a syntactic environment.
  2084. ; Syntactic environments are described in another file.
  2085. ;
  2086. ; The supported syntax differs from the R4RS in that vectors are
  2087. ; allowed as patterns and as templates and are not allowed as
  2088. ; pattern or template data.
  2089. ;
  2090. ; <transformer spec> --> (syntax-rules <literals> <rules>)
  2091. ; <rules> --> () | (<rule> . <rules>)
  2092. ; <rule> --> (<pattern> <template>)
  2093. ; <pattern> --> <pattern_var> ; a <symbol> not in <literals>
  2094. ; | <symbol> ; a <symbol> in <literals>
  2095. ; | ()
  2096. ; | (<pattern> . <pattern>)
  2097. ; | (<ellipsis_pattern>)
  2098. ; | #(<pattern>*) ; extends R4RS
  2099. ; | #(<pattern>* <ellipsis_pattern>) ; extends R4RS
  2100. ; | <pattern_datum>
  2101. ; <template> --> <pattern_var>
  2102. ; | <symbol>
  2103. ; | ()
  2104. ; | (<template2> . <template2>)
  2105. ; | #(<template>*) ; extends R4RS
  2106. ; | <pattern_datum>
  2107. ; <template2> --> <template> | <ellipsis_template>
  2108. ; <pattern_datum> --> <string> ; no <vector>
  2109. ; | <character>
  2110. ; | <boolean>
  2111. ; | <number>
  2112. ; <ellipsis_pattern> --> <pattern> ...
  2113. ; <ellipsis_template> --> <template> ...
  2114. ; <pattern_var> --> <symbol> ; not in <literals>
  2115. ; <literals> --> () | (<symbol> . <literals>)
  2116. ;
  2117. ; Definitions.
  2118. ;
  2119. ; scope of an ellipsis
  2120. ;
  2121. ; Within a pattern or template, the scope of an ellipsis
  2122. ; (...) is the pattern or template that appears to its left.
  2123. ;
  2124. ; rank of a pattern variable
  2125. ;
  2126. ; The rank of a pattern variable is the number of ellipses
  2127. ; within whose scope it appears in the pattern.
  2128. ;
  2129. ; rank of a subtemplate
  2130. ;
  2131. ; The rank of a subtemplate is the number of ellipses within
  2132. ; whose scope it appears in the template.
  2133. ;
  2134. ; template rank of an occurrence of a pattern variable
  2135. ;
  2136. ; The template rank of an occurrence of a pattern variable
  2137. ; within a template is the rank of that occurrence, viewed
  2138. ; as a subtemplate.
  2139. ;
  2140. ; variables bound by a pattern
  2141. ;
  2142. ; The variables bound by a pattern are the pattern variables
  2143. ; that appear within it.
  2144. ;
  2145. ; referenced variables of a subtemplate
  2146. ;
  2147. ; The referenced variables of a subtemplate are the pattern
  2148. ; variables that appear within it.
  2149. ;
  2150. ; variables opened by an ellipsis template
  2151. ;
  2152. ; The variables opened by an ellipsis template are the
  2153. ; referenced pattern variables whose rank is greater than
  2154. ; the rank of the ellipsis template.
  2155. ;
  2156. ;
  2157. ; Restrictions.
  2158. ;
  2159. ; No pattern variable appears more than once within a pattern.
  2160. ;
  2161. ; For every occurrence of a pattern variable within a template,
  2162. ; the template rank of the occurrence must be greater than or
  2163. ; equal to the pattern variable's rank.
  2164. ;
  2165. ; Every ellipsis template must open at least one variable.
  2166. ;
  2167. ; For every ellipsis template, the variables opened by an
  2168. ; ellipsis template must all be bound to sequences of the
  2169. ; same length.
  2170. ;
  2171. ;
  2172. ; The compiled form of a <rule> is
  2173. ;
  2174. ; <rule> --> (<pattern> <template> <inserted>)
  2175. ; <pattern> --> <pattern_var>
  2176. ; | <symbol>
  2177. ; | ()
  2178. ; | (<pattern> . <pattern>)
  2179. ; | <ellipsis_pattern>
  2180. ; | #(<pattern>)
  2181. ; | <pattern_datum>
  2182. ; <template> --> <pattern_var>
  2183. ; | <symbol>
  2184. ; | ()
  2185. ; | (<template2> . <template2>)
  2186. ; | #(<pattern>)
  2187. ; | <pattern_datum>
  2188. ; <template2> --> <template> | <ellipsis_template>
  2189. ; <pattern_datum> --> <string>
  2190. ; | <character>
  2191. ; | <boolean>
  2192. ; | <number>
  2193. ; <pattern_var> --> #(<V> <symbol> <rank>)
  2194. ; <ellipsis_pattern> --> #(<E> <pattern> <pattern_vars>)
  2195. ; <ellipsis_template> --> #(<E> <template> <pattern_vars>)
  2196. ; <inserted> --> () | (<symbol> . <inserted>)
  2197. ; <pattern_vars> --> () | (<pattern_var> . <pattern_vars>)
  2198. ; <rank> --> <exact non-negative integer>
  2199. ;
  2200. ; where <V> and <E> are unforgeable values.
  2201. ; The pattern variables associated with an ellipsis pattern
  2202. ; are the variables bound by the pattern, and the pattern
  2203. ; variables associated with an ellipsis template are the
  2204. ; variables opened by the ellipsis template.
  2205. ;
  2206. ;
  2207. ; What's wrong with the above?
  2208. ; If the template contains a big chunk that contains no pattern variables
  2209. ; or inserted identifiers, then the big chunk will be copied unnecessarily.
  2210. ; That shouldn't matter very often.
  2211. ($$trace "syntaxrules")
  2212. (define pattern-variable-flag (list 'v))
  2213. (define ellipsis-pattern-flag (list 'e))
  2214. (define ellipsis-template-flag ellipsis-pattern-flag)
  2215. (define (make-patternvar v rank)
  2216. (vector pattern-variable-flag v rank))
  2217. (define (make-ellipsis-pattern P vars)
  2218. (vector ellipsis-pattern-flag P vars))
  2219. (define (make-ellipsis-template T vars)
  2220. (vector ellipsis-template-flag T vars))
  2221. (define (patternvar? x)
  2222. (and (vector? x)
  2223. (= (vector-length x) 3)
  2224. (eq? (vector-ref x 0) pattern-variable-flag)))
  2225. (define (ellipsis-pattern? x)
  2226. (and (vector? x)
  2227. (= (vector-length x) 3)
  2228. (eq? (vector-ref x 0) ellipsis-pattern-flag)))
  2229. (define (ellipsis-template? x)
  2230. (and (vector? x)
  2231. (= (vector-length x) 3)
  2232. (eq? (vector-ref x 0) ellipsis-template-flag)))
  2233. (define (patternvar-name V) (vector-ref V 1))
  2234. (define (patternvar-rank V) (vector-ref V 2))
  2235. (define (ellipsis-pattern P) (vector-ref P 1))
  2236. (define (ellipsis-pattern-vars P) (vector-ref P 2))
  2237. (define (ellipsis-template T) (vector-ref T 1))
  2238. (define (ellipsis-template-vars T) (vector-ref T 2))
  2239. (define (pattern-variable v vars)
  2240. (cond ((null? vars) #f)
  2241. ((eq? v (patternvar-name (car vars)))
  2242. (car vars))
  2243. (else (pattern-variable v (cdr vars)))))
  2244. ; Given a <transformer spec> and a syntactic environment,
  2245. ; returns a macro denotation.
  2246. ;
  2247. ; A macro denotation is of the form
  2248. ;
  2249. ; (macro (<rule> ...) env)
  2250. ;
  2251. ; where each <rule> has been compiled as described above.
  2252. (define (m-compile-transformer-spec spec env)
  2253. (if (and (> (safe-length spec) 1)
  2254. (eq? (syntactic-lookup env (car spec))
  2255. denotation-of-syntax-rules))
  2256. (let ((literals (cadr spec))
  2257. (rules (cddr spec)))
  2258. (if (or (not (list? literals))
  2259. (not (every1? (lambda (rule)
  2260. (and (= (safe-length rule) 2)
  2261. (pair? (car rule))))
  2262. rules)))
  2263. (m-error "Malformed syntax-rules" spec))
  2264. (list 'macro
  2265. (map (lambda (rule)
  2266. (m-compile-rule rule literals env))
  2267. rules)
  2268. env))
  2269. (m-error "Malformed syntax-rules" spec)))
  2270. (define (m-compile-rule rule literals env)
  2271. (m-compile-pattern (cdr (car rule))
  2272. literals
  2273. env
  2274. (lambda (compiled-rule patternvars)
  2275. ; FIXME
  2276. ; should check uniqueness of pattern variables here
  2277. (cons compiled-rule
  2278. (m-compile-template
  2279. (cadr rule)
  2280. patternvars
  2281. env)))))
  2282. (define (m-compile-pattern P literals env k)
  2283. (define (loop P vars rank k)
  2284. (cond ((symbol? P)
  2285. (if (memq P literals)
  2286. (k P vars)
  2287. (let ((var (make-patternvar P rank)))
  2288. (k var (cons var vars)))))
  2289. ((null? P) (k '() vars))
  2290. ((pair? P)
  2291. (if (and (pair? (cdr P))
  2292. (symbol? (cadr P))
  2293. (same-denotation? (syntactic-lookup env (cadr P))
  2294. denotation-of-...))
  2295. (if (null? (cddr P))
  2296. (loop (car P)
  2297. '()
  2298. (+ rank 1)
  2299. (lambda (P vars1)
  2300. (k (make-ellipsis-pattern P vars1)
  2301. (union2 vars1 vars))))
  2302. (m-error "Malformed pattern" P))
  2303. (loop (car P)
  2304. vars
  2305. rank
  2306. (lambda (P1 vars)
  2307. (loop (cdr P)
  2308. vars
  2309. rank
  2310. (lambda (P2 vars)
  2311. (k (cons P1 P2) vars)))))))
  2312. ((vector? P)
  2313. (loop (vector->list P)
  2314. vars
  2315. rank
  2316. (lambda (P vars)
  2317. (k (vector P) vars))))
  2318. (else (k P vars))))
  2319. (loop P '() 0 k))
  2320. (define (m-compile-template T vars env)
  2321. (define (loop T inserted referenced rank escaped? k)
  2322. (cond ((symbol? T)
  2323. (let ((x (pattern-variable T vars)))
  2324. (if x
  2325. (if (>= rank (patternvar-rank x))
  2326. (k x inserted (cons x referenced))
  2327. (m-error
  2328. "Too few ellipses follow pattern variable in template"
  2329. (patternvar-name x)))
  2330. (k T (cons T inserted) referenced))))
  2331. ((null? T) (k '() inserted referenced))
  2332. ((pair? T)
  2333. (cond ((and (not escaped?)
  2334. (symbol? (car T))
  2335. (same-denotation? (syntactic-lookup env (car T))
  2336. denotation-of-...)
  2337. (pair? (cdr T))
  2338. (null? (cddr T)))
  2339. (loop (cadr T) inserted referenced rank #t k))
  2340. ((and (not escaped?)
  2341. (pair? (cdr T))
  2342. (symbol? (cadr T))
  2343. (same-denotation? (syntactic-lookup env (cadr T))
  2344. denotation-of-...))
  2345. (loop1 T inserted referenced rank escaped? k))
  2346. (else
  2347. (loop (car T)
  2348. inserted
  2349. referenced
  2350. rank
  2351. escaped?
  2352. (lambda (T1 inserted referenced)
  2353. (loop (cdr T)
  2354. inserted
  2355. referenced
  2356. rank
  2357. escaped?
  2358. (lambda (T2 inserted referenced)
  2359. (k (cons T1 T2) inserted referenced))))))))
  2360. ((vector? T)
  2361. (loop (vector->list T)
  2362. inserted
  2363. referenced
  2364. rank
  2365. escaped?
  2366. (lambda (T inserted referenced)
  2367. (k (vector T) inserted referenced))))
  2368. (else (k T inserted referenced))))
  2369. (define (loop1 T inserted referenced rank escaped? k)
  2370. (loop (car T)
  2371. inserted
  2372. '()
  2373. (+ rank 1)
  2374. escaped?
  2375. (lambda (T1 inserted referenced1)
  2376. (loop (cddr T)
  2377. inserted
  2378. (append referenced1 referenced)
  2379. rank
  2380. escaped?
  2381. (lambda (T2 inserted referenced)
  2382. (k (cons (make-ellipsis-template
  2383. T1
  2384. (filter1 (lambda (var)
  2385. (> (patternvar-rank var)
  2386. rank))
  2387. referenced1))
  2388. T2)
  2389. inserted
  2390. referenced))))))
  2391. (loop T
  2392. '()
  2393. '()
  2394. 0
  2395. #f
  2396. (lambda (T inserted referenced)
  2397. (list T inserted))))
  2398. ; The pattern matcher.
  2399. ;
  2400. ; Given an input, a pattern, and two syntactic environments,
  2401. ; returns a pattern variable environment (represented as an alist)
  2402. ; if the input matches the pattern, otherwise returns #f.
  2403. (define empty-pattern-variable-environment
  2404. (list (make-patternvar (string->symbol "") 0)))
  2405. (define (m-match F P env-def env-use)
  2406. (define (match F P answer rank)
  2407. (cond ((null? P)
  2408. (and (null? F) answer))
  2409. ((pair? P)
  2410. (and (pair? F)
  2411. (let ((answer (match (car F) (car P) answer rank)))
  2412. (and answer (match (cdr F) (cdr P) answer rank)))))
  2413. ((symbol? P)
  2414. (and (symbol? F)
  2415. (same-denotation? (syntactic-lookup env-def P)
  2416. (syntactic-lookup env-use F))
  2417. answer))
  2418. ((patternvar? P)
  2419. (cons (cons P F) answer))
  2420. ((ellipsis-pattern? P)
  2421. (match1 F P answer (+ rank 1)))
  2422. ((vector? P)
  2423. (and (vector? F)
  2424. (match (vector->list F) (vector-ref P 0) answer rank)))
  2425. (else (and (equal? F P) answer))))
  2426. (define (match1 F P answer rank)
  2427. (cond ((not (list? F)) #f)
  2428. ((null? F)
  2429. (append (map (lambda (var) (cons var '()))
  2430. (ellipsis-pattern-vars P))
  2431. answer))
  2432. (else
  2433. (let* ((P1 (ellipsis-pattern P))
  2434. (answers (map (lambda (F) (match F P1 answer rank))
  2435. F)))
  2436. (if (every1? (lambda (answer) answer) answers)
  2437. (append (map (lambda (var)
  2438. (cons var
  2439. (map (lambda (answer)
  2440. (cdr (assq var answer)))
  2441. answers)))
  2442. (ellipsis-pattern-vars P))
  2443. answer)
  2444. #f)))))
  2445. (match F P empty-pattern-variable-environment 0))
  2446. (define (m-rewrite T alist)
  2447. (define (rewrite T alist rank)
  2448. (cond ((null? T) '())
  2449. ((pair? T)
  2450. ((if (ellipsis-pattern? (car T))
  2451. append
  2452. cons)
  2453. (rewrite (car T) alist rank)
  2454. (rewrite (cdr T) alist rank)))
  2455. ((symbol? T) (cdr (assq T alist)))
  2456. ((patternvar? T) (cdr (assq T alist)))
  2457. ((ellipsis-template? T)
  2458. (rewrite1 T alist (+ rank 1)))
  2459. ((vector? T)
  2460. (list->vector (rewrite (vector-ref T 0) alist rank)))
  2461. (else T)))
  2462. (define (rewrite1 T alist rank)
  2463. (let* ((T1 (ellipsis-template T))
  2464. (vars (ellipsis-template-vars T))
  2465. (rows (map (lambda (var) (cdr (assq var alist)))
  2466. vars)))
  2467. (map (lambda (alist) (rewrite T1 alist rank))
  2468. (make-columns vars rows alist))))
  2469. (define (make-columns vars rows alist)
  2470. (define (loop rows)
  2471. (if (null? (car rows))
  2472. '()
  2473. (cons (append (map (lambda (var row)
  2474. (cons var (car row)))
  2475. vars
  2476. rows)
  2477. alist)
  2478. (loop (map cdr rows)))))
  2479. (if (or (null? (cdr rows))
  2480. (apply = (map length rows)))
  2481. (loop rows)
  2482. (m-error "Use of macro is not consistent with definition"
  2483. vars
  2484. rows)))
  2485. (rewrite T alist 0))
  2486. ; Given a use of a macro, the syntactic environment of the use,
  2487. ; a continuation that expects a transcribed expression and
  2488. ; a new environment in which to continue expansion, and a boolean
  2489. ; that is true if this transcription is for an inline procedure,
  2490. ; does the right thing.
  2491. (define (m-transcribe0 exp env-use k inline?)
  2492. (let* ((m (syntactic-lookup env-use (car exp)))
  2493. (rules (macro-rules m))
  2494. (env-def (macro-env m))
  2495. (F (cdr exp)))
  2496. (define (loop rules)
  2497. (if (null? rules)
  2498. (if inline?
  2499. (k exp env-use)
  2500. (m-error "Use of macro does not match definition" exp))
  2501. (let* ((rule (car rules))
  2502. (pattern (car rule))
  2503. (alist (m-match F pattern env-def env-use)))
  2504. (if alist
  2505. (let* ((template (cadr rule))
  2506. (inserted (caddr rule))
  2507. (alist2 (rename-vars inserted))
  2508. (newexp (m-rewrite template (append alist2 alist))))
  2509. (k newexp
  2510. (syntactic-alias env-use alist2 env-def)))
  2511. (loop (cdr rules))))))
  2512. (if (procedure? rules)
  2513. (m-transcribe-low-level exp env-use k rules env-def)
  2514. (loop rules))))
  2515. (define (m-transcribe exp env-use k)
  2516. (m-transcribe0 exp env-use k #f))
  2517. (define (m-transcribe-inline exp env-use k)
  2518. (m-transcribe0 exp env-use k #t))
  2519. ; Copyright 1998 William Clinger
  2520. ;
  2521. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  2522. ;
  2523. ; Low-level macro facility based on explicit renaming. See
  2524. ; William D Clinger. Hygienic macros through explicit renaming.
  2525. ; In Lisp Pointers IV(4), 25-28, December 1991.
  2526. ($$trace "lowlevel")
  2527. (define (m-transcribe-low-level exp env-use k transformer env-def)
  2528. (let ((rename0 (make-rename-procedure))
  2529. (renamed '())
  2530. (ok #t))
  2531. (define (lookup sym)
  2532. (let loop ((alist renamed))
  2533. (cond ((null? alist)
  2534. (syntactic-lookup env-use sym))
  2535. ((eq? sym (cdr (car alist)))
  2536. (syntactic-lookup env-def (car (car alist))))
  2537. (else
  2538. (loop (cdr alist))))))
  2539. (let ((rename
  2540. (lambda (sym)
  2541. (if ok
  2542. (let ((probe (assq sym renamed)))
  2543. (if probe
  2544. (cdr probe)
  2545. (let ((sym2 (rename0 sym)))
  2546. (set! renamed (cons (cons sym sym2) renamed))
  2547. sym2)))
  2548. (m-error "Illegal use of a rename procedure" sym))))
  2549. (compare
  2550. (lambda (sym1 sym2)
  2551. (same-denotation? (lookup sym1) (lookup sym2)))))
  2552. (let ((exp2 (transformer exp rename compare)))
  2553. (set! ok #f)
  2554. (k exp2
  2555. (syntactic-alias env-use renamed env-def))))))
  2556. (define identifier? symbol?)
  2557. (define (identifier->symbol id)
  2558. (m-strip id))
  2559. ; Copyright 1992 William Clinger
  2560. ;
  2561. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  2562. ;
  2563. ; 22 April 1999
  2564. ($$trace "expand")
  2565. ; This procedure sets the default scope of global macro definitions.
  2566. (define define-syntax-scope
  2567. (let ((flag 'letrec))
  2568. (lambda args
  2569. (cond ((null? args) flag)
  2570. ((not (null? (cdr args)))
  2571. (apply m-warn
  2572. "Too many arguments passed to define-syntax-scope"
  2573. args))
  2574. ((memq (car args) '(letrec letrec* let*))
  2575. (set! flag (car args)))
  2576. (else (m-warn "Unrecognized argument to define-syntax-scope"
  2577. (car args)))))))
  2578. ; The main entry point.
  2579. ; The outermost lambda allows known procedures to be lifted outside
  2580. ; all local variables.
  2581. (define (macro-expand def-or-exp)
  2582. (call-with-current-continuation
  2583. (lambda (k)
  2584. (set! m-quit k)
  2585. (set! renaming-counter 0)
  2586. (make-call
  2587. (make-lambda '() ; formals
  2588. '() ; definitions
  2589. '() ; R
  2590. '() ; F
  2591. '() ; G
  2592. '() ; declarations
  2593. #f ; documentation
  2594. (desugar-definitions def-or-exp
  2595. global-syntactic-environment
  2596. make-toplevel-definition))
  2597. '()))))
  2598. (define (desugar-definitions exp env make-toplevel-definition)
  2599. (letrec
  2600. ((define-loop
  2601. (lambda (exp rest first env)
  2602. (cond ((and (pair? exp)
  2603. (symbol? (car exp))
  2604. (eq? (syntactic-lookup env (car exp))
  2605. denotation-of-begin)
  2606. (pair? (cdr exp)))
  2607. (define-loop (cadr exp) (append (cddr exp) rest) first env))
  2608. ((and (pair? exp)
  2609. (symbol? (car exp))
  2610. (eq? (syntactic-lookup env (car exp))
  2611. denotation-of-define))
  2612. (let ((exp (desugar-define exp env)))
  2613. (cond ((and (null? first) (null? rest))
  2614. exp)
  2615. ((null? rest)
  2616. (make-begin (reverse (cons exp first))))
  2617. (else (define-loop (car rest)
  2618. (cdr rest)
  2619. (cons exp first)
  2620. env)))))
  2621. ((and (pair? exp)
  2622. (symbol? (car exp))
  2623. (or (eq? (syntactic-lookup env (car exp))
  2624. denotation-of-define-syntax)
  2625. (eq? (syntactic-lookup env (car exp))
  2626. denotation-of-define-inline))
  2627. (null? first))
  2628. (define-syntax-loop exp rest env))
  2629. ((and (pair? exp)
  2630. (symbol? (car exp))
  2631. (macro-denotation? (syntactic-lookup env (car exp))))
  2632. (m-transcribe exp
  2633. env
  2634. (lambda (exp env)
  2635. (define-loop exp rest first env))))
  2636. ((and (null? first) (null? rest))
  2637. (m-expand exp env))
  2638. ((null? rest)
  2639. (make-begin (reverse (cons (m-expand exp env) first))))
  2640. (else (make-begin
  2641. (append (reverse first)
  2642. (map (lambda (exp) (m-expand exp env))
  2643. (cons exp rest))))))))
  2644. (define-syntax-loop
  2645. (lambda (exp rest env)
  2646. (cond ((and (pair? exp)
  2647. (symbol? (car exp))
  2648. (eq? (syntactic-lookup env (car exp))
  2649. denotation-of-begin)
  2650. (pair? (cdr exp)))
  2651. (define-syntax-loop (cadr exp) (append (cddr exp) rest) env))
  2652. ((and (pair? exp)
  2653. (symbol? (car exp))
  2654. (eq? (syntactic-lookup env (car exp))
  2655. denotation-of-define-syntax))
  2656. (if (pair? (cdr exp))
  2657. (redefinition (cadr exp)))
  2658. (if (null? rest)
  2659. (m-define-syntax exp env)
  2660. (begin (m-define-syntax exp env)
  2661. (define-syntax-loop (car rest) (cdr rest) env))))
  2662. ((and (pair? exp)
  2663. (symbol? (car exp))
  2664. (eq? (syntactic-lookup env (car exp))
  2665. denotation-of-define-inline))
  2666. (if (pair? (cdr exp))
  2667. (redefinition (cadr exp)))
  2668. (if (null? rest)
  2669. (m-define-inline exp env)
  2670. (begin (m-define-inline exp env)
  2671. (define-syntax-loop (car rest) (cdr rest) env))))
  2672. ((and (pair? exp)
  2673. (symbol? (car exp))
  2674. (macro-denotation? (syntactic-lookup env (car exp))))
  2675. (m-transcribe exp
  2676. env
  2677. (lambda (exp env)
  2678. (define-syntax-loop exp rest env))))
  2679. ((and (pair? exp)
  2680. (symbol? (car exp))
  2681. (eq? (syntactic-lookup env (car exp))
  2682. denotation-of-define))
  2683. (define-loop exp rest '() env))
  2684. ((null? rest)
  2685. (m-expand exp env))
  2686. (else (make-begin
  2687. (map (lambda (exp) (m-expand exp env))
  2688. (cons exp rest)))))))
  2689. (desugar-define
  2690. (lambda (exp env)
  2691. (cond
  2692. ((null? (cdr exp)) (m-error "Malformed definition" exp))
  2693. ; (define foo) syntax is transformed into (define foo (undefined)).
  2694. ((null? (cddr exp))
  2695. (let ((id (cadr exp)))
  2696. (if (or (null? pass1-block-inlines)
  2697. (not (memq id pass1-block-inlines)))
  2698. (begin
  2699. (redefinition id)
  2700. (syntactic-bind-globally! id (make-identifier-denotation id))))
  2701. (make-toplevel-definition id (make-undefined))))
  2702. ((pair? (cadr exp))
  2703. (desugar-define
  2704. (let* ((def (car exp))
  2705. (pattern (cadr exp))
  2706. (f (car pattern))
  2707. (args (cdr pattern))
  2708. (body (cddr exp)))
  2709. (if (and (symbol? (car (cadr exp)))
  2710. (benchmark-mode)
  2711. (list? (cadr exp)))
  2712. `(,def ,f
  2713. (,lambda0 ,args
  2714. ((,lambda0 (,f)
  2715. (,set!0 ,f (,lambda0 ,args ,@body))
  2716. ,pattern)
  2717. 0)))
  2718. `(,def ,f (,lambda0 ,args ,@body))))
  2719. env))
  2720. ((> (length exp) 3) (m-error "Malformed definition" exp))
  2721. (else (let ((id (cadr exp)))
  2722. (if (or (null? pass1-block-inlines)
  2723. (not (memq id pass1-block-inlines)))
  2724. (begin
  2725. (redefinition id)
  2726. (syntactic-bind-globally! id (make-identifier-denotation id))))
  2727. (make-toplevel-definition id (m-expand (caddr exp) env)))))))
  2728. (redefinition
  2729. (lambda (id)
  2730. (if (symbol? id)
  2731. (if (not (identifier-denotation?
  2732. (syntactic-lookup global-syntactic-environment id)))
  2733. (if (issue-warnings)
  2734. (m-warn "Redefining " id)))
  2735. (m-error "Malformed variable or keyword" id)))))
  2736. ; body of letrec
  2737. (define-loop exp '() '() env)))
  2738. ; Given an expression and a syntactic environment,
  2739. ; returns an expression in core Scheme.
  2740. (define (m-expand exp env)
  2741. (cond ((not (pair? exp))
  2742. (m-atom exp env))
  2743. ((not (symbol? (car exp)))
  2744. (m-application exp env))
  2745. (else
  2746. (let ((keyword (syntactic-lookup env (car exp))))
  2747. (case (denotation-class keyword)
  2748. ((special)
  2749. (cond
  2750. ((eq? keyword denotation-of-quote) (m-quote exp))
  2751. ((eq? keyword denotation-of-lambda) (m-lambda exp env))
  2752. ((eq? keyword denotation-of-if) (m-if exp env))
  2753. ((eq? keyword denotation-of-set!) (m-set exp env))
  2754. ((eq? keyword denotation-of-begin) (m-begin exp env))
  2755. ((eq? keyword denotation-of-let-syntax)
  2756. (m-let-syntax exp env))
  2757. ((eq? keyword denotation-of-letrec-syntax)
  2758. (m-letrec-syntax exp env))
  2759. ((or (eq? keyword denotation-of-define)
  2760. (eq? keyword denotation-of-define-syntax)
  2761. (eq? keyword denotation-of-define-inline))
  2762. (m-error "Definition out of context" exp))
  2763. (else (m-bug "Bug detected in m-expand" exp env))))
  2764. ((macro) (m-macro exp env))
  2765. ((inline) (m-inline exp env))
  2766. ((identifier) (m-application exp env))
  2767. (else (m-bug "Bug detected in m-expand" exp env)))))))
  2768. (define (m-atom exp env)
  2769. (cond ((not (symbol? exp))
  2770. ; Here exp ought to be a boolean, number, character, or string.
  2771. ; I'll warn about other things but treat them as if quoted.
  2772. ;
  2773. ; I'm turning off some of the warnings because notably procedures
  2774. ; and #!unspecified can occur in loaded files and it's a major
  2775. ; pain if a warning is printed for each. --lars
  2776. (if (and (not (boolean? exp))
  2777. (not (number? exp))
  2778. (not (char? exp))
  2779. (not (string? exp))
  2780. (not (procedure? exp))
  2781. (not (eq? exp (unspecified))))
  2782. (m-warn "Malformed constant -- should be quoted" exp))
  2783. (make-constant exp))
  2784. (else (let ((denotation (syntactic-lookup env exp)))
  2785. (case (denotation-class denotation)
  2786. ((special macro)
  2787. (m-warn "Syntactic keyword used as a variable" exp)
  2788. ; Syntactic keywords used as variables are treated as #t.
  2789. (make-constant #t))
  2790. ((inline)
  2791. (make-variable (inline-name denotation)))
  2792. ((identifier)
  2793. (let ((var (make-variable (identifier-name denotation)))
  2794. (R-entry (identifier-R-entry denotation)))
  2795. (R-entry.references-set!
  2796. R-entry
  2797. (cons var (R-entry.references R-entry)))
  2798. var))
  2799. (else (m-bug "Bug detected by m-atom" exp env)))))))
  2800. (define (m-quote exp)
  2801. (if (and (pair? (cdr exp))
  2802. (null? (cddr exp)))
  2803. (make-constant (m-strip (cadr exp)))
  2804. (m-error "Malformed quoted constant" exp)))
  2805. (define (m-lambda exp env)
  2806. (if (> (safe-length exp) 2)
  2807. (let* ((formals (cadr exp))
  2808. (alist (rename-vars formals))
  2809. (env (syntactic-rename env alist))
  2810. (body (cddr exp)))
  2811. (do ((alist alist (cdr alist)))
  2812. ((null? alist))
  2813. (if (assq (caar alist) (cdr alist))
  2814. (m-error "Malformed parameter list" formals)))
  2815. ; To simplify the run-time system, there's a limit on how many
  2816. ; fixed arguments can be followed by a rest argument.
  2817. ; That limit is removed here.
  2818. ; Bug: documentation slot isn't right when this happens.
  2819. ; Bug: this generates extremely inefficient code.
  2820. (if (and (not (list? formals))
  2821. (> (length alist) @maxargs-with-rest-arg@))
  2822. (let ((TEMP (car (rename-vars '(temp)))))
  2823. (m-lambda
  2824. `(,lambda0 ,TEMP
  2825. ((,lambda0 ,(map car alist)
  2826. ,@(cddr exp))
  2827. ,@(do ((actuals '() (cons (list name:CAR path)
  2828. actuals))
  2829. (path TEMP (list name:CDR path))
  2830. (formals formals (cdr formals)))
  2831. ((symbol? formals)
  2832. (append (reverse actuals) (list path))))))
  2833. env))
  2834. (make-lambda (rename-formals formals alist)
  2835. '() ; no definitions yet
  2836. (map (lambda (entry)
  2837. (cdr (syntactic-lookup env (cdr entry))))
  2838. alist) ; R
  2839. '() ; F
  2840. '() ; G
  2841. '() ; decls
  2842. (make-doc #f
  2843. (if (list? formals)
  2844. (length alist)
  2845. (exact->inexact (- (length alist) 1)))
  2846. (if (include-variable-names)
  2847. formals
  2848. #f)
  2849. (if (include-source-code)
  2850. exp
  2851. #f)
  2852. source-file-name
  2853. source-file-position)
  2854. (m-body body env))))
  2855. (m-error "Malformed lambda expression" exp)))
  2856. (define (m-body body env)
  2857. (define (loop body env defs)
  2858. (if (null? body)
  2859. (m-error "Empty body"))
  2860. (let ((exp (car body)))
  2861. (if (and (pair? exp)
  2862. (symbol? (car exp)))
  2863. (let ((denotation (syntactic-lookup env (car exp))))
  2864. (case (denotation-class denotation)
  2865. ((special)
  2866. (cond ((eq? denotation denotation-of-begin)
  2867. (loop (append (cdr exp) (cdr body)) env defs))
  2868. ((eq? denotation denotation-of-define)
  2869. (loop (cdr body) env (cons exp defs)))
  2870. (else (finalize-body body env defs))))
  2871. ((macro)
  2872. (m-transcribe exp
  2873. env
  2874. (lambda (exp env)
  2875. (loop (cons exp (cdr body))
  2876. env
  2877. defs))))
  2878. ((inline identifier)
  2879. (finalize-body body env defs))
  2880. (else (m-bug "Bug detected in m-body" body env))))
  2881. (finalize-body body env defs))))
  2882. (loop body env '()))
  2883. (define (finalize-body body env defs)
  2884. (if (null? defs)
  2885. (let ((body (map (lambda (exp) (m-expand exp env))
  2886. body)))
  2887. (if (null? (cdr body))
  2888. (car body)
  2889. (make-begin body)))
  2890. (let ()
  2891. (define (sort-defs defs)
  2892. (let* ((augmented
  2893. (map (lambda (def)
  2894. (let ((rhs (cadr def)))
  2895. (if (not (pair? rhs))
  2896. (cons 'trivial def)
  2897. (let ((denotation
  2898. (syntactic-lookup env (car rhs))))
  2899. (cond ((eq? denotation
  2900. denotation-of-lambda)
  2901. (cons 'procedure def))
  2902. ((eq? denotation
  2903. denotation-of-quote)
  2904. (cons 'trivial def))
  2905. (else
  2906. (cons 'miscellaneous def)))))))
  2907. defs))
  2908. (sorted (twobit-sort (lambda (x y)
  2909. (or (eq? (car x) 'procedure)
  2910. (eq? (car y) 'miscellaneous)))
  2911. augmented)))
  2912. (map cdr sorted)))
  2913. (define (desugar-definition def)
  2914. (if (> (safe-length def) 2)
  2915. (cond ((pair? (cadr def))
  2916. (desugar-definition
  2917. `(,(car def)
  2918. ,(car (cadr def))
  2919. (,lambda0
  2920. ,(cdr (cadr def))
  2921. ,@(cddr def)))))
  2922. ((and (= (length def) 3)
  2923. (symbol? (cadr def)))
  2924. (cdr def))
  2925. (else (m-error "Malformed definition" def)))
  2926. (m-error "Malformed definition" def)))
  2927. (define (expand-letrec bindings body)
  2928. (make-call
  2929. (m-expand
  2930. `(,lambda0 ,(map car bindings)
  2931. ,@(map (lambda (binding)
  2932. `(,set!0 ,(car binding)
  2933. ,(cadr binding)))
  2934. bindings)
  2935. ,@body)
  2936. env)
  2937. (map (lambda (binding) (make-unspecified)) bindings)))
  2938. (expand-letrec (sort-defs (map desugar-definition
  2939. (reverse defs)))
  2940. body))))
  2941. (define (m-if exp env)
  2942. (let ((n (safe-length exp)))
  2943. (if (or (= n 3) (= n 4))
  2944. (make-conditional (m-expand (cadr exp) env)
  2945. (m-expand (caddr exp) env)
  2946. (if (= n 3)
  2947. (make-unspecified)
  2948. (m-expand (cadddr exp) env)))
  2949. (m-error "Malformed if expression" exp))))
  2950. (define (m-set exp env)
  2951. (if (= (safe-length exp) 3)
  2952. (let ((lhs (m-expand (cadr exp) env))
  2953. (rhs (m-expand (caddr exp) env)))
  2954. (if (variable? lhs)
  2955. (let* ((x (variable.name lhs))
  2956. (assignment (make-assignment x rhs))
  2957. (denotation (syntactic-lookup env x)))
  2958. (if (identifier-denotation? denotation)
  2959. (let ((R-entry (identifier-R-entry denotation)))
  2960. (R-entry.references-set!
  2961. R-entry
  2962. (remq lhs (R-entry.references R-entry)))
  2963. (R-entry.assignments-set!
  2964. R-entry
  2965. (cons assignment (R-entry.assignments R-entry)))))
  2966. (if (and (lambda? rhs)
  2967. (include-procedure-names))
  2968. (let ((doc (lambda.doc rhs)))
  2969. (doc.name-set! doc x)))
  2970. (if pass1-block-compiling?
  2971. (set! pass1-block-assignments
  2972. (cons x pass1-block-assignments)))
  2973. assignment)
  2974. (m-error "Malformed assignment" exp)))
  2975. (m-error "Malformed assignment" exp)))
  2976. (define (m-begin exp env)
  2977. (cond ((> (safe-length exp) 1)
  2978. (make-begin (map (lambda (exp) (m-expand exp env)) (cdr exp))))
  2979. ((= (safe-length exp) 1)
  2980. (m-warn "Non-standard begin expression" exp)
  2981. (make-unspecified))
  2982. (else
  2983. (m-error "Malformed begin expression" exp))))
  2984. (define (m-application exp env)
  2985. (if (> (safe-length exp) 0)
  2986. (let* ((proc (m-expand (car exp) env))
  2987. (args (map (lambda (exp) (m-expand exp env))
  2988. (cdr exp)))
  2989. (call (make-call proc args)))
  2990. (if (variable? proc)
  2991. (let* ((procname (variable.name proc))
  2992. (entry
  2993. (and (not (null? args))
  2994. (constant? (car args))
  2995. (integrate-usual-procedures)
  2996. (every1? constant? args)
  2997. (let ((entry (constant-folding-entry procname)))
  2998. (and entry
  2999. (let ((predicates
  3000. (constant-folding-predicates entry)))
  3001. (and (= (length args)
  3002. (length predicates))
  3003. (let loop ((args args)
  3004. (predicates predicates))
  3005. (cond ((null? args) entry)
  3006. (((car predicates)
  3007. (constant.value (car args)))
  3008. (loop (cdr args)
  3009. (cdr predicates)))
  3010. (else #f))))))))))
  3011. (if entry
  3012. (make-constant (apply (constant-folding-folder entry)
  3013. (map constant.value args)))
  3014. (let ((denotation (syntactic-lookup env procname)))
  3015. (if (identifier-denotation? denotation)
  3016. (let ((R-entry (identifier-R-entry denotation)))
  3017. (R-entry.calls-set!
  3018. R-entry
  3019. (cons call (R-entry.calls R-entry)))))
  3020. call)))
  3021. call))
  3022. (m-error "Malformed application" exp)))
  3023. ; The environment argument should always be global here.
  3024. (define (m-define-inline exp env)
  3025. (cond ((and (= (safe-length exp) 3)
  3026. (symbol? (cadr exp)))
  3027. (let ((name (cadr exp)))
  3028. (m-define-syntax1 name
  3029. (caddr exp)
  3030. env
  3031. (define-syntax-scope))
  3032. (let ((denotation
  3033. (syntactic-lookup global-syntactic-environment name)))
  3034. (syntactic-bind-globally!
  3035. name
  3036. (make-inline-denotation name
  3037. (macro-rules denotation)
  3038. (macro-env denotation))))
  3039. (make-constant name)))
  3040. (else
  3041. (m-error "Malformed define-inline" exp))))
  3042. ; The environment argument should always be global here.
  3043. (define (m-define-syntax exp env)
  3044. (cond ((and (= (safe-length exp) 3)
  3045. (symbol? (cadr exp)))
  3046. (m-define-syntax1 (cadr exp)
  3047. (caddr exp)
  3048. env
  3049. (define-syntax-scope)))
  3050. ((and (= (safe-length exp) 4)
  3051. (symbol? (cadr exp))
  3052. ; FIXME: should use denotations here
  3053. (memq (caddr exp) '(letrec letrec* let*)))
  3054. (m-define-syntax1 (cadr exp)
  3055. (cadddr exp)
  3056. env
  3057. (caddr exp)))
  3058. (else (m-error "Malformed define-syntax" exp))))
  3059. (define (m-define-syntax1 keyword spec env scope)
  3060. (if (and (pair? spec)
  3061. (symbol? (car spec)))
  3062. (let* ((transformer-keyword (car spec))
  3063. (denotation (syntactic-lookup env transformer-keyword)))
  3064. (cond ((eq? denotation denotation-of-syntax-rules)
  3065. (case scope
  3066. ((letrec) (m-define-syntax-letrec keyword spec env))
  3067. ((letrec*) (m-define-syntax-letrec* keyword spec env))
  3068. ((let*) (m-define-syntax-let* keyword spec env))
  3069. (else (m-bug "Weird scope" scope))))
  3070. ((same-denotation? denotation denotation-of-transformer)
  3071. ; FIXME: no error checking here
  3072. (syntactic-bind-globally!
  3073. keyword
  3074. (make-macro-denotation (eval (cadr spec)) env)))
  3075. (else
  3076. (m-error "Malformed syntax transformer" spec))))
  3077. (m-error "Malformed syntax transformer" spec))
  3078. (make-constant keyword))
  3079. (define (m-define-syntax-letrec keyword spec env)
  3080. (syntactic-bind-globally!
  3081. keyword
  3082. (m-compile-transformer-spec spec env)))
  3083. (define (m-define-syntax-letrec* keyword spec env)
  3084. (let* ((env (syntactic-extend (syntactic-copy env)
  3085. (list keyword)
  3086. '((fake denotation))))
  3087. (transformer (m-compile-transformer-spec spec env)))
  3088. (syntactic-assign! env keyword transformer)
  3089. (syntactic-bind-globally! keyword transformer)))
  3090. (define (m-define-syntax-let* keyword spec env)
  3091. (syntactic-bind-globally!
  3092. keyword
  3093. (m-compile-transformer-spec spec (syntactic-copy env))))
  3094. (define (m-let-syntax exp env)
  3095. (if (and (> (safe-length exp) 2)
  3096. (every1? (lambda (binding)
  3097. (and (pair? binding)
  3098. (symbol? (car binding))
  3099. (pair? (cdr binding))
  3100. (null? (cddr binding))))
  3101. (cadr exp)))
  3102. (m-body (cddr exp)
  3103. (syntactic-extend env
  3104. (map car (cadr exp))
  3105. (map (lambda (spec)
  3106. (m-compile-transformer-spec
  3107. spec
  3108. env))
  3109. (map cadr (cadr exp)))))
  3110. (m-error "Malformed let-syntax" exp)))
  3111. (define (m-letrec-syntax exp env)
  3112. (if (and (> (safe-length exp) 2)
  3113. (every1? (lambda (binding)
  3114. (and (pair? binding)
  3115. (symbol? (car binding))
  3116. (pair? (cdr binding))
  3117. (null? (cddr binding))))
  3118. (cadr exp)))
  3119. (let ((env (syntactic-extend env
  3120. (map car (cadr exp))
  3121. (map (lambda (id)
  3122. '(fake denotation))
  3123. (cadr exp)))))
  3124. (for-each (lambda (id spec)
  3125. (syntactic-assign!
  3126. env
  3127. id
  3128. (m-compile-transformer-spec spec env)))
  3129. (map car (cadr exp))
  3130. (map cadr (cadr exp)))
  3131. (m-body (cddr exp) env))
  3132. (m-error "Malformed let-syntax" exp)))
  3133. (define (m-macro exp env)
  3134. (m-transcribe exp
  3135. env
  3136. (lambda (exp env)
  3137. (m-expand exp env))))
  3138. (define (m-inline exp env)
  3139. (if (integrate-usual-procedures)
  3140. (m-transcribe-inline exp
  3141. env
  3142. (lambda (newexp env)
  3143. (if (eq? exp newexp)
  3144. (m-application exp env)
  3145. (m-expand newexp env))))
  3146. (m-application exp env)))
  3147. (define m-quit ; assigned by macro-expand
  3148. (lambda (v) v))
  3149. ; To do:
  3150. ; Clean up alist hacking et cetera.
  3151. ; Declarations.
  3152. ; Integrable procedures.
  3153. ; New semantics for body of LET-SYNTAX and LETREC-SYNTAX.
  3154. ; Copyright 1992 William Clinger
  3155. ;
  3156. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  3157. ;
  3158. ; 5 April 1999.
  3159. ($$trace "usual")
  3160. ; The usual macros, adapted from Jonathan's Version 2 implementation.
  3161. ; DEFINE is handled primitively, since top-level DEFINE has a side
  3162. ; effect on the global syntactic environment, and internal definitions
  3163. ; have to be handled specially anyway.
  3164. ;
  3165. ; Some extensions are noted, as are some optimizations.
  3166. ;
  3167. ; The LETREC* scope rule is used here to protect these macros against
  3168. ; redefinition of LAMBDA etc. The scope rule is changed to LETREC at
  3169. ; the end of this file.
  3170. (define-syntax-scope 'letrec*)
  3171. (for-each (lambda (form)
  3172. (macro-expand form))
  3173. '(
  3174. ; Named LET is defined later, after LETREC has been defined.
  3175. (define-syntax let
  3176. (syntax-rules ()
  3177. ((let ((?name ?val) ...) ?body ?body1 ...)
  3178. ((lambda (?name ...) ?body ?body1 ...) ?val ...))))
  3179. (define-syntax let*
  3180. (syntax-rules ()
  3181. ((let* () ?body ?body1 ...)
  3182. (let () ?body ?body1 ...))
  3183. ((let* ((?name1 ?val1) (?name ?val) ...) ?body ?body1 ...)
  3184. (let ((?name1 ?val1)) (let* ((?name ?val) ...) ?body ?body1 ...)))))
  3185. ; Internal definitions have to be handled specially anyway,
  3186. ; so we might as well rely on them here.
  3187. (define-syntax letrec
  3188. (syntax-rules (lambda quote)
  3189. ((letrec ((?name ?val) ...) ?body ?body2 ...)
  3190. ((lambda ()
  3191. (define ?name ?val) ...
  3192. ?body ?body2 ...)))))
  3193. ; This definition of named LET extends the prior definition of LET.
  3194. ; The first rule is non-circular, thanks to the LET* scope that is
  3195. ; specified for this use of DEFINE-SYNTAX.
  3196. (define-syntax let let*
  3197. (syntax-rules ()
  3198. ((let (?bindings ...) . ?body)
  3199. (let (?bindings ...) . ?body))
  3200. ((let ?tag ((?name ?val) ...) ?body ?body1 ...)
  3201. (let ((?name ?val) ...)
  3202. (letrec ((?tag (lambda (?name ...) ?body ?body1 ...)))
  3203. (?tag ?name ...))))))
  3204. (define-syntax and
  3205. (syntax-rules ()
  3206. ((and) #t)
  3207. ((and ?e) ?e)
  3208. ((and ?e1 ?e2 ?e3 ...)
  3209. (if ?e1 (and ?e2 ?e3 ...) #f))))
  3210. (define-syntax or
  3211. (syntax-rules ()
  3212. ((or) #f)
  3213. ((or ?e) ?e)
  3214. ((or ?e1 ?e2 ?e3 ...)
  3215. (let ((temp ?e1))
  3216. (if temp temp (or ?e2 ?e3 ...))))))
  3217. (define-syntax cond
  3218. (syntax-rules (else =>)
  3219. ((cond (else ?result ?result2 ...))
  3220. (begin ?result ?result2 ...))
  3221. ((cond (?test => ?result))
  3222. (let ((temp ?test))
  3223. (if temp (?result temp))))
  3224. ((cond (?test)) ?test)
  3225. ((cond (?test ?result ?result2 ...))
  3226. (if ?test (begin ?result ?result2 ...)))
  3227. ((cond (?test => ?result) ?clause ?clause2 ...)
  3228. (let ((temp ?test))
  3229. (if temp (?result temp) (cond ?clause ?clause2 ...))))
  3230. ((cond (?test) ?clause ?clause2 ...)
  3231. (or ?test (cond ?clause ?clause2 ...)))
  3232. ((cond (?test ?result ?result2 ...)
  3233. ?clause ?clause2 ...)
  3234. (if ?test
  3235. (begin ?result ?result2 ...)
  3236. (cond ?clause ?clause2 ...)))))
  3237. ; The R4RS says a <step> may be omitted.
  3238. ; That's a good excuse for a macro-defining macro that uses LETREC-SYNTAX
  3239. ; and the ... escape.
  3240. (define-syntax do
  3241. (syntax-rules ()
  3242. ((do (?bindings0 ...) (?test) ?body0 ...)
  3243. (do (?bindings0 ...) (?test (if #f #f)) ?body0 ...))
  3244. ((do (?bindings0 ...) ?clause0 ?body0 ...)
  3245. (letrec-syntax
  3246. ((do-aux
  3247. (... (syntax-rules ()
  3248. ((do-aux () ((?name ?init ?step) ...) ?clause ?body ...)
  3249. (letrec ((loop (lambda (?name ...)
  3250. (cond ?clause
  3251. (else
  3252. (begin #t ?body ...)
  3253. (loop ?step ...))))))
  3254. (loop ?init ...)))
  3255. ((do-aux ((?name ?init ?step) ?todo ...)
  3256. (?bindings ...)
  3257. ?clause
  3258. ?body ...)
  3259. (do-aux (?todo ...)
  3260. (?bindings ... (?name ?init ?step))
  3261. ?clause
  3262. ?body ...))
  3263. ((do-aux ((?name ?init) ?todo ...)
  3264. (?bindings ...)
  3265. ?clause
  3266. ?body ...)
  3267. (do-aux (?todo ...)
  3268. (?bindings ... (?name ?init ?name))
  3269. ?clause
  3270. ?body ...))))))
  3271. (do-aux (?bindings0 ...) () ?clause0 ?body0 ...)))))
  3272. (define-syntax delay
  3273. (syntax-rules ()
  3274. ((delay ?e) (.make-promise (lambda () ?e)))))
  3275. ; Another use of LETREC-SYNTAX and the escape extension.
  3276. (define-syntax case
  3277. (syntax-rules (else)
  3278. ((case ?e1 (else ?body ?body2 ...))
  3279. (begin ?e1 ?body ?body2 ...))
  3280. ((case ?e1 (?z ?body ?body2 ...))
  3281. (if (memv ?e1 '?z) (begin ?body ?body2 ...)))
  3282. ((case ?e1 ?clause1 ?clause2 ?clause3 ...)
  3283. (letrec-syntax
  3284. ((case-aux
  3285. (... (syntax-rules (else)
  3286. ((case-aux ?temp (else ?body ?body2 ...))
  3287. (begin ?body ?body2 ...))
  3288. ((case-aux ?temp ((?z ...) ?body ?body2 ...))
  3289. (if (memv ?temp '(?z ...)) (begin ?body ?body2 ...)))
  3290. ((case-aux ?temp ((?z ...) ?body ?body2 ...) ?c1 ?c2 ...)
  3291. (if (memv ?temp '(?z ...))
  3292. (begin ?body ?body2 ...)
  3293. (case-aux ?temp ?c1 ?c2 ...)))
  3294. ; a popular extension
  3295. ((case-aux ?temp (?z ?body ...) ?c1 ...)
  3296. (case-aux ?temp ((?z) ?body ...) ?c1 ...))))))
  3297. (let ((temp ?e1))
  3298. (case-aux temp ?clause1 ?clause2 ?clause3 ...))))))
  3299. ; A complete implementation of quasiquote, obtained by translating
  3300. ; Jonathan Rees's implementation that was posted to RRRS-AUTHORS
  3301. ; on 22 December 1986.
  3302. ; Unfortunately, the use of LETREC scope means that it is vulnerable
  3303. ; to top-level redefinitions of QUOTE etc. That could be fixed, but
  3304. ; it has hair enough already.
  3305. (begin
  3306. (define-syntax .finalize-quasiquote letrec
  3307. (syntax-rules (quote unquote unquote-splicing)
  3308. ((.finalize-quasiquote quote ?arg ?return)
  3309. (.interpret-continuation ?return (quote ?arg)))
  3310. ((.finalize-quasiquote unquote ?arg ?return)
  3311. (.interpret-continuation ?return ?arg))
  3312. ((.finalize-quasiquote unquote-splicing ?arg ?return)
  3313. (syntax-error ",@ in illegal context" ?arg))
  3314. ((.finalize-quasiquote ?mode ?arg ?return)
  3315. (.interpret-continuation ?return (?mode . ?arg)))))
  3316. ; The first two "arguments" to .descend-quasiquote and to
  3317. ; .descend-quasiquote-pair are always identical.
  3318. (define-syntax .descend-quasiquote letrec
  3319. (syntax-rules (quasiquote unquote unquote-splicing)
  3320. ((.descend-quasiquote `?y ?x ?level ?return)
  3321. (.descend-quasiquote-pair ?x ?x (?level) ?return))
  3322. ((.descend-quasiquote ,?y ?x () ?return)
  3323. (.interpret-continuation ?return unquote ?y))
  3324. ((.descend-quasiquote ,?y ?x (?level) ?return)
  3325. (.descend-quasiquote-pair ?x ?x ?level ?return))
  3326. ((.descend-quasiquote ,@?y ?x () ?return)
  3327. (.interpret-continuation ?return unquote-splicing ?y))
  3328. ((.descend-quasiquote ,@?y ?x (?level) ?return)
  3329. (.descend-quasiquote-pair ?x ?x ?level ?return))
  3330. ((.descend-quasiquote (?y . ?z) ?x ?level ?return)
  3331. (.descend-quasiquote-pair ?x ?x ?level ?return))
  3332. ((.descend-quasiquote #(?y ...) ?x ?level ?return)
  3333. (.descend-quasiquote-vector ?x ?x ?level ?return))
  3334. ((.descend-quasiquote ?y ?x ?level ?return)
  3335. (.interpret-continuation ?return quote ?x))))
  3336. (define-syntax .descend-quasiquote-pair letrec
  3337. (syntax-rules (quote unquote unquote-splicing)
  3338. ((.descend-quasiquote-pair (?carx . ?cdrx) ?x ?level ?return)
  3339. (.descend-quasiquote ?carx ?carx ?level (1 ?cdrx ?x ?level ?return)))))
  3340. (define-syntax .descend-quasiquote-vector letrec
  3341. (syntax-rules (quote)
  3342. ((.descend-quasiquote-vector #(?y ...) ?x ?level ?return)
  3343. (.descend-quasiquote (?y ...) (?y ...) ?level (6 ?x ?return)))))
  3344. ; Representations for continuations used here.
  3345. ; Continuation types 0, 1, 2, and 6 take a mode and an expression.
  3346. ; Continuation types -1, 3, 4, 5, and 7 take just an expression.
  3347. ;
  3348. ; (-1)
  3349. ; means no continuation
  3350. ; (0)
  3351. ; means to call .finalize-quasiquote with no further continuation
  3352. ; (1 ?cdrx ?x ?level ?return)
  3353. ; means a return from the call to .descend-quasiquote from
  3354. ; .descend-quasiquote-pair
  3355. ; (2 ?car-mode ?car-arg ?x ?return)
  3356. ; means a return from the second call to .descend-quasiquote in
  3357. ; in Jonathan's code for .descend-quasiquote-pair
  3358. ; (3 ?car-arg ?return)
  3359. ; means take the result and return an append of ?car-arg with it
  3360. ; (4 ?cdr-mode ?cdr-arg ?return)
  3361. ; means take the result and call .finalize-quasiquote on ?cdr-mode
  3362. ; and ?cdr-arg with a continuation of type 5
  3363. ; (5 ?car-result ?return)
  3364. ; means take the result and return a cons of ?car-result onto it
  3365. ; (6 ?x ?return)
  3366. ; means a return from the call to .descend-quasiquote from
  3367. ; .descend-quasiquote-vector
  3368. ; (7 ?return)
  3369. ; means take the result and return a call of list->vector on it
  3370. (define-syntax .interpret-continuation letrec
  3371. (syntax-rules (quote unquote unquote-splicing)
  3372. ((.interpret-continuation (-1) ?e) ?e)
  3373. ((.interpret-continuation (0) ?mode ?arg)
  3374. (.finalize-quasiquote ?mode ?arg (-1)))
  3375. ((.interpret-continuation (1 ?cdrx ?x ?level ?return) ?car-mode ?car-arg)
  3376. (.descend-quasiquote ?cdrx
  3377. ?cdrx
  3378. ?level
  3379. (2 ?car-mode ?car-arg ?x ?return)))
  3380. ((.interpret-continuation (2 quote ?car-arg ?x ?return) quote ?cdr-arg)
  3381. (.interpret-continuation ?return quote ?x))
  3382. ((.interpret-continuation (2 unquote-splicing ?car-arg ?x ?return) quote ())
  3383. (.interpret-continuation ?return unquote ?car-arg))
  3384. ((.interpret-continuation (2 unquote-splicing ?car-arg ?x ?return)
  3385. ?cdr-mode ?cdr-arg)
  3386. (.finalize-quasiquote ?cdr-mode ?cdr-arg (3 ?car-arg ?return)))
  3387. ((.interpret-continuation (2 ?car-mode ?car-arg ?x ?return)
  3388. ?cdr-mode ?cdr-arg)
  3389. (.finalize-quasiquote ?car-mode ?car-arg (4 ?cdr-mode ?cdr-arg ?return)))
  3390. ((.interpret-continuation (3 ?car-arg ?return) ?e)
  3391. (.interpret-continuation ?return append (?car-arg ?e)))
  3392. ((.interpret-continuation (4 ?cdr-mode ?cdr-arg ?return) ?e1)
  3393. (.finalize-quasiquote ?cdr-mode ?cdr-arg (5 ?e1 ?return)))
  3394. ((.interpret-continuation (5 ?e1 ?return) ?e2)
  3395. (.interpret-continuation ?return .cons (?e1 ?e2)))
  3396. ((.interpret-continuation (6 ?x ?return) quote ?arg)
  3397. (.interpret-continuation ?return quote ?x))
  3398. ((.interpret-continuation (6 ?x ?return) ?mode ?arg)
  3399. (.finalize-quasiquote ?mode ?arg (7 ?return)))
  3400. ((.interpret-continuation (7 ?return) ?e)
  3401. (.interpret-continuation ?return .list->vector (?e)))))
  3402. (define-syntax quasiquote letrec
  3403. (syntax-rules ()
  3404. ((quasiquote ?x)
  3405. (.descend-quasiquote ?x ?x () (0)))))
  3406. )
  3407. (define-syntax let*-syntax
  3408. (syntax-rules ()
  3409. ((let*-syntax () ?body)
  3410. (let-syntax () ?body))
  3411. ((let*-syntax ((?name1 ?val1) (?name ?val) ...) ?body)
  3412. (let-syntax ((?name1 ?val1)) (let*-syntax ((?name ?val) ...) ?body)))))
  3413. ))
  3414. (define-syntax-scope 'letrec)
  3415. (define standard-syntactic-environment
  3416. (syntactic-copy global-syntactic-environment))
  3417. (define (make-standard-syntactic-environment)
  3418. (syntactic-copy standard-syntactic-environment))
  3419. ; Copyright 1998 William Clinger.
  3420. ;
  3421. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  3422. ;
  3423. ; 25 April 1999
  3424. ;
  3425. ; Given an expression in the subset of Scheme used as an intermediate language
  3426. ; by Twobit, returns a newly allocated copy of the expression in which the
  3427. ; local variables have been renamed and the referencing information has been
  3428. ; recomputed.
  3429. (define (copy-exp exp)
  3430. (define special-names (cons name:IGNORED argument-registers))
  3431. (define original-names (make-hashtable symbol-hash assq))
  3432. (define renaming-counter 0)
  3433. (define (rename-vars vars)
  3434. (let ((rename (make-rename-procedure)))
  3435. (map (lambda (var)
  3436. (cond ((memq var special-names)
  3437. var)
  3438. ((hashtable-get original-names var)
  3439. (rename var))
  3440. (else
  3441. (hashtable-put! original-names var #t)
  3442. var)))
  3443. vars)))
  3444. (define (rename-formals formals newnames)
  3445. (cond ((null? formals) '())
  3446. ((symbol? formals) (car newnames))
  3447. ((memq (car formals) special-names)
  3448. (cons (car formals)
  3449. (rename-formals (cdr formals)
  3450. (cdr newnames))))
  3451. (else (cons (car newnames)
  3452. (rename-formals (cdr formals)
  3453. (cdr newnames))))))
  3454. ; Environments that map symbols to arbitrary information.
  3455. ; This data type is mutable, and uses the shallow binding technique.
  3456. (define (make-env) (make-hashtable symbol-hash assq))
  3457. (define (env-bind! env sym info)
  3458. (let ((stack (hashtable-get env sym)))
  3459. (hashtable-put! env sym (cons info stack))))
  3460. (define (env-unbind! env sym)
  3461. (let ((stack (hashtable-get env sym)))
  3462. (hashtable-put! env sym (cdr stack))))
  3463. (define (env-lookup env sym default)
  3464. (let ((stack (hashtable-get env sym)))
  3465. (if stack
  3466. (car stack)
  3467. default)))
  3468. (define (env-bind-multiple! env symbols infos)
  3469. (for-each (lambda (sym info) (env-bind! env sym info))
  3470. symbols
  3471. infos))
  3472. (define (env-unbind-multiple! env symbols)
  3473. (for-each (lambda (sym) (env-unbind! env sym))
  3474. symbols))
  3475. ;
  3476. (define (lexical-lookup R-table name)
  3477. (assq name R-table))
  3478. (define (copy exp env notepad R-table)
  3479. (cond ((constant? exp) exp)
  3480. ((lambda? exp)
  3481. (let* ((bvl (make-null-terminated (lambda.args exp)))
  3482. (newnames (rename-vars bvl))
  3483. (procnames (map def.lhs (lambda.defs exp)))
  3484. (newprocnames (rename-vars procnames))
  3485. (refinfo (map (lambda (var)
  3486. (make-R-entry var '() '() '()))
  3487. (append newnames newprocnames)))
  3488. (newexp
  3489. (make-lambda
  3490. (rename-formals (lambda.args exp) newnames)
  3491. '()
  3492. refinfo
  3493. '()
  3494. '()
  3495. (lambda.decls exp)
  3496. (lambda.doc exp)
  3497. (lambda.body exp))))
  3498. (env-bind-multiple! env procnames newprocnames)
  3499. (env-bind-multiple! env bvl newnames)
  3500. (for-each (lambda (entry)
  3501. (env-bind! R-table (R-entry.name entry) entry))
  3502. refinfo)
  3503. (notepad-lambda-add! notepad newexp)
  3504. (let ((newnotepad (make-notepad notepad)))
  3505. (for-each (lambda (name rhs)
  3506. (lambda.defs-set!
  3507. newexp
  3508. (cons (make-definition
  3509. name
  3510. (copy rhs env newnotepad R-table))
  3511. (lambda.defs newexp))))
  3512. (reverse newprocnames)
  3513. (map def.rhs
  3514. (reverse (lambda.defs exp))))
  3515. (lambda.body-set!
  3516. newexp
  3517. (copy (lambda.body exp) env newnotepad R-table))
  3518. (lambda.F-set! newexp (notepad-free-variables newnotepad))
  3519. (lambda.G-set! newexp (notepad-captured-variables newnotepad)))
  3520. (env-unbind-multiple! env procnames)
  3521. (env-unbind-multiple! env bvl)
  3522. (for-each (lambda (entry)
  3523. (env-unbind! R-table (R-entry.name entry)))
  3524. refinfo)
  3525. newexp))
  3526. ((assignment? exp)
  3527. (let* ((oldname (assignment.lhs exp))
  3528. (name (env-lookup env oldname oldname))
  3529. (varinfo (env-lookup R-table name #f))
  3530. (newexp
  3531. (make-assignment name
  3532. (copy (assignment.rhs exp) env notepad R-table))))
  3533. (notepad-var-add! notepad name)
  3534. (if varinfo
  3535. (R-entry.assignments-set!
  3536. varinfo
  3537. (cons newexp (R-entry.assignments varinfo))))
  3538. newexp))
  3539. ((conditional? exp)
  3540. (make-conditional (copy (if.test exp) env notepad R-table)
  3541. (copy (if.then exp) env notepad R-table)
  3542. (copy (if.else exp) env notepad R-table)))
  3543. ((begin? exp)
  3544. (make-begin (map (lambda (exp) (copy exp env notepad R-table))
  3545. (begin.exprs exp))))
  3546. ((variable? exp)
  3547. (let* ((oldname (variable.name exp))
  3548. (name (env-lookup env oldname oldname))
  3549. (varinfo (env-lookup R-table name #f))
  3550. (newexp (make-variable name)))
  3551. (notepad-var-add! notepad name)
  3552. (if varinfo
  3553. (R-entry.references-set!
  3554. varinfo
  3555. (cons newexp (R-entry.references varinfo))))
  3556. newexp))
  3557. ((call? exp)
  3558. (let ((newexp (make-call (copy (call.proc exp) env notepad R-table)
  3559. (map (lambda (exp)
  3560. (copy exp env notepad R-table))
  3561. (call.args exp)))))
  3562. (if (variable? (call.proc newexp))
  3563. (let ((varinfo
  3564. (env-lookup R-table
  3565. (variable.name
  3566. (call.proc newexp))
  3567. #f)))
  3568. (if varinfo
  3569. (R-entry.calls-set!
  3570. varinfo
  3571. (cons newexp (R-entry.calls varinfo))))))
  3572. (if (lambda? (call.proc newexp))
  3573. (notepad-nonescaping-add! notepad (call.proc newexp)))
  3574. newexp))
  3575. (else ???)))
  3576. (copy exp (make-env) (make-notepad #f) (make-env)))
  3577. ; For debugging.
  3578. ; Given an expression, traverses the expression to confirm
  3579. ; that the referencing invariants are correct.
  3580. (define (check-referencing-invariants exp . flags)
  3581. (let ((check-free-variables? (memq 'free flags))
  3582. (check-referencing? (memq 'reference flags))
  3583. (first-violation? #t))
  3584. ; env is the list of enclosing lambda expressions,
  3585. ; beginning with the innermost.
  3586. (define (check exp env)
  3587. (cond ((constant? exp) (return exp #t))
  3588. ((lambda? exp)
  3589. (let ((env (cons exp env)))
  3590. (return exp
  3591. (and (every? (lambda (exp)
  3592. (check exp env))
  3593. (map def.rhs (lambda.defs exp)))
  3594. (check (lambda.body exp) env)
  3595. (if (and check-free-variables?
  3596. (not (null? env)))
  3597. (subset? (difference
  3598. (lambda.F exp)
  3599. (make-null-terminated
  3600. (lambda.args exp)))
  3601. (lambda.F (car env)))
  3602. #t)
  3603. (if check-referencing?
  3604. (let ((env (cons exp env))
  3605. (R (lambda.R exp)))
  3606. (every? (lambda (formal)
  3607. (or (ignored? formal)
  3608. (R-entry R formal)))
  3609. (make-null-terminated
  3610. (lambda.args exp))))
  3611. #t)))))
  3612. ((variable? exp)
  3613. (return exp
  3614. (and (if (and check-free-variables?
  3615. (not (null? env)))
  3616. (memq (variable.name exp)
  3617. (lambda.F (car env)))
  3618. #t)
  3619. (if check-referencing?
  3620. (let ((Rinfo (lookup env (variable.name exp))))
  3621. (if Rinfo
  3622. (memq exp (R-entry.references Rinfo))
  3623. #t))
  3624. #t))))
  3625. ((assignment? exp)
  3626. (return exp
  3627. (and (check (assignment.rhs exp) env)
  3628. (if (and check-free-variables?
  3629. (not (null? env)))
  3630. (memq (assignment.lhs exp)
  3631. (lambda.F (car env)))
  3632. #t)
  3633. (if check-referencing?
  3634. (let ((Rinfo (lookup env (assignment.lhs exp))))
  3635. (if Rinfo
  3636. (memq exp (R-entry.assignments Rinfo))
  3637. #t))
  3638. #t))))
  3639. ((conditional? exp)
  3640. (return exp
  3641. (and (check (if.test exp) env)
  3642. (check (if.then exp) env)
  3643. (check (if.else exp) env))))
  3644. ((begin? exp)
  3645. (return exp
  3646. (every? (lambda (exp) (check exp env))
  3647. (begin.exprs exp))))
  3648. ((call? exp)
  3649. (return exp
  3650. (and (check (call.proc exp) env)
  3651. (every? (lambda (exp) (check exp env))
  3652. (call.args exp))
  3653. (if (and check-referencing?
  3654. (variable? (call.proc exp)))
  3655. (let ((Rinfo (lookup env
  3656. (variable.name
  3657. (call.proc exp)))))
  3658. (if Rinfo
  3659. (memq exp (R-entry.calls Rinfo))
  3660. #t))
  3661. #t))))
  3662. (else ???)))
  3663. (define (return exp flag)
  3664. (cond (flag
  3665. #t)
  3666. (first-violation?
  3667. (set! first-violation? #f)
  3668. (display "Violation of referencing invariants")
  3669. (newline)
  3670. (pretty-print (make-readable exp))
  3671. #f)
  3672. (else (pretty-print (make-readable exp))
  3673. #f)))
  3674. (define (lookup env I)
  3675. (if (null? env)
  3676. #f
  3677. (let ((Rinfo (R-entry (lambda.R (car env)) I)))
  3678. (or Rinfo
  3679. (lookup (cdr env) I)))))
  3680. (if (null? flags)
  3681. (begin (set! check-free-variables? #t)
  3682. (set! check-referencing? #t)))
  3683. (check exp '())))
  3684. ; Calculating the free variable information for an expression
  3685. ; as output by pass 2. This should be faster than computing both
  3686. ; the free variables and the referencing information.
  3687. (define (compute-free-variables! exp)
  3688. (define empty-set (make-set '()))
  3689. (define (singleton x) (list x))
  3690. (define (union2 x y) (union x y))
  3691. (define (union3 x y z) (union x y z))
  3692. (define (set->list set) set)
  3693. (define (free exp)
  3694. (cond ((constant? exp) empty-set)
  3695. ((lambda? exp)
  3696. (let* ((defs (lambda.defs exp))
  3697. (formals (make-set
  3698. (make-null-terminated (lambda.args exp))))
  3699. (defined (make-set (map def.lhs defs)))
  3700. (Fdefs
  3701. (apply-union
  3702. (map (lambda (def)
  3703. (free (def.rhs def)))
  3704. defs)))
  3705. (Fbody (free (lambda.body exp)))
  3706. (F (union2 Fdefs Fbody)))
  3707. (lambda.F-set! exp (set->list F))
  3708. (lambda.G-set! exp (set->list F))
  3709. (difference F (union2 formals defined))))
  3710. ((assignment? exp)
  3711. (union2 (make-set (list (assignment.lhs exp)))
  3712. (free (assignment.rhs exp))))
  3713. ((conditional? exp)
  3714. (union3 (free (if.test exp))
  3715. (free (if.then exp))
  3716. (free (if.else exp))))
  3717. ((begin? exp)
  3718. (apply-union
  3719. (map (lambda (exp) (free exp))
  3720. (begin.exprs exp))))
  3721. ((variable? exp)
  3722. (singleton (variable.name exp)))
  3723. ((call? exp)
  3724. (union2 (free (call.proc exp))
  3725. (apply-union
  3726. (map (lambda (exp) (free exp))
  3727. (call.args exp)))))
  3728. (else ???)))
  3729. (free exp))
  3730. ; As above, but representing sets as hashtrees.
  3731. ; This is commented out because it is much slower than the implementation
  3732. ; above. Because the set of free variables is represented as a list
  3733. ; within a lambda expression, this implementation must convert the
  3734. ; representation for every lambda expression, which is quite expensive
  3735. ; for A-normal form.
  3736. (begin
  3737. '
  3738. (define (compute-free-variables! exp)
  3739. (define empty-set (make-hashtree symbol-hash assq))
  3740. (define (singleton x)
  3741. (hashtree-put empty-set x #t))
  3742. (define (make-set values)
  3743. (if (null? values)
  3744. empty-set
  3745. (hashtree-put (make-set (cdr values))
  3746. (car values)
  3747. #t)))
  3748. (define (union2 x y)
  3749. (hashtree-for-each (lambda (key val)
  3750. (set! x (hashtree-put x key #t)))
  3751. y)
  3752. x)
  3753. (define (union3 x y z)
  3754. (union2 (union2 x y) z))
  3755. (define (apply-union sets)
  3756. (cond ((null? sets)
  3757. (make-set '()))
  3758. ((null? (cdr sets))
  3759. (car sets))
  3760. (else
  3761. (union2 (car sets)
  3762. (apply-union (cdr sets))))))
  3763. (define (difference x y)
  3764. (hashtree-for-each (lambda (key val)
  3765. (set! x (hashtree-remove x key)))
  3766. y)
  3767. x)
  3768. (define (set->list set)
  3769. (hashtree-map (lambda (sym val) sym) set))
  3770. (define (free exp)
  3771. (cond ((constant? exp) empty-set)
  3772. ((lambda? exp)
  3773. (let* ((defs (lambda.defs exp))
  3774. (formals (make-set
  3775. (make-null-terminated (lambda.args exp))))
  3776. (defined (make-set (map def.lhs defs)))
  3777. (Fdefs
  3778. (apply-union
  3779. (map (lambda (def)
  3780. (free (def.rhs def)))
  3781. defs)))
  3782. (Fbody (free (lambda.body exp)))
  3783. (F (union2 Fdefs Fbody)))
  3784. (lambda.F-set! exp (set->list F))
  3785. (lambda.G-set! exp (set->list F))
  3786. (difference F (union2 formals defined))))
  3787. ((assignment? exp)
  3788. (union2 (make-set (list (assignment.lhs exp)))
  3789. (free (assignment.rhs exp))))
  3790. ((conditional? exp)
  3791. (union3 (free (if.test exp))
  3792. (free (if.then exp))
  3793. (free (if.else exp))))
  3794. ((begin? exp)
  3795. (apply-union
  3796. (map (lambda (exp) (free exp))
  3797. (begin.exprs exp))))
  3798. ((variable? exp)
  3799. (singleton (variable.name exp)))
  3800. ((call? exp)
  3801. (union2 (free (call.proc exp))
  3802. (apply-union
  3803. (map (lambda (exp) (free exp))
  3804. (call.args exp)))))
  3805. (else ???)))
  3806. (hashtree-map (lambda (sym val) sym)
  3807. (free exp)))
  3808. #t); Copyright 1991 William Clinger
  3809. ;
  3810. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  3811. ;
  3812. ; 24 April 1999
  3813. ;
  3814. ; First pass of the Twobit compiler:
  3815. ; macro expansion, syntax checking, alpha conversion,
  3816. ; preliminary annotation.
  3817. ;
  3818. ; The input to this pass is a Scheme definition or expression.
  3819. ; The output is an expression in the subset of Scheme described
  3820. ; by the following grammar, where the output satisfies certain
  3821. ; additional invariants described below.
  3822. ;
  3823. ; "X ..." means zero or more occurrences of X.
  3824. ;
  3825. ; L --> (lambda (I_1 ...)
  3826. ; (begin D ...)
  3827. ; (quote (R F G <decls> <doc>)
  3828. ; E)
  3829. ; | (lambda (I_1 ... . I_rest)
  3830. ; (begin D ...)
  3831. ; (quote (R F <decls> <doc>))
  3832. ; E)
  3833. ; D --> (define I L)
  3834. ; E --> (quote K) ; constants
  3835. ; | (begin I) ; variable references
  3836. ; | L ; lambda expressions
  3837. ; | (E0 E1 ...) ; calls
  3838. ; | (set! I E) ; assignments
  3839. ; | (if E0 E1 E2) ; conditionals
  3840. ; | (begin E0 E1 E2 ...) ; sequential expressions
  3841. ; I --> <identifier>
  3842. ;
  3843. ; R --> ((I <references> <assignments> <calls>) ...)
  3844. ; F --> (I ...)
  3845. ; G --> (I ...)
  3846. ;
  3847. ; Invariants that hold for the output:
  3848. ; * There are no internal definitions.
  3849. ; * No identifier containing an upper case letter is bound anywhere.
  3850. ; (Change the "name:..." variables if upper case is preferred.)
  3851. ; * No identifier is bound in more than one place.
  3852. ; * Each R contains one entry for every identifier bound in the
  3853. ; formal argument list and the internal definition list that
  3854. ; precede it. Each entry contains a list of pointers to all
  3855. ; references to the identifier, a list of pointers to all
  3856. ; assignments to the identifier, and a list of pointers to all
  3857. ; calls to the identifier.
  3858. ; * Except for constants, the expression does not share structure
  3859. ; with the original input or itself, except that the references
  3860. ; and assignments in R are guaranteed to share structure with
  3861. ; the expression. Thus the expression may be side effected, and
  3862. ; side effects to references or assignments obtained through R
  3863. ; are guaranteed to change the references or assignments pointed
  3864. ; to by R.
  3865. ; * F and G are garbage.
  3866. ($$trace "pass1")
  3867. (define source-file-name #f)
  3868. (define source-file-position #f)
  3869. (define pass1-block-compiling? #f)
  3870. (define pass1-block-assignments '())
  3871. (define pass1-block-inlines '())
  3872. (define (pass1 def-or-exp . rest)
  3873. (set! source-file-name #f)
  3874. (set! source-file-position #f)
  3875. (set! pass1-block-compiling? #f)
  3876. (set! pass1-block-assignments '())
  3877. (set! pass1-block-inlines '())
  3878. (if (not (null? rest))
  3879. (begin (set! source-file-name (car rest))
  3880. (if (not (null? (cdr rest)))
  3881. (set! source-file-position (cadr rest)))))
  3882. (set! renaming-counter 0)
  3883. (macro-expand def-or-exp))
  3884. ; Compiles a whole sequence of top-level forms on the assumption
  3885. ; that no variable that is defined by a form in the sequence is
  3886. ; ever defined or assigned outside of the sequence.
  3887. ;
  3888. ; This is a crock in three parts:
  3889. ;
  3890. ; 1. Macro-expand each form and record assignments.
  3891. ; 2. Find the top-level variables that are defined but not
  3892. ; assigned, give them local names, generate a DEFINE-INLINE
  3893. ; for each of the top-level procedures, and macro-expand
  3894. ; each form again.
  3895. ; 3. Wrap the whole mess in an appropriate LET and recompute
  3896. ; the referencing information by copying it.
  3897. ;
  3898. ; Note that macros get expanded twice, and that all DEFINE-SYNTAX
  3899. ; macros are considered local to the forms.
  3900. ; FIXME: Need to turn off warning messages.
  3901. (define (pass1-block forms . rest)
  3902. (define (part1)
  3903. (set! pass1-block-compiling? #t)
  3904. (set! pass1-block-assignments '())
  3905. (set! pass1-block-inlines '())
  3906. (set! renaming-counter 0)
  3907. (let ((env0 (syntactic-copy global-syntactic-environment))
  3908. (bmode (benchmark-mode))
  3909. (wmode (issue-warnings))
  3910. (defined '()))
  3911. (define (make-toplevel-definition id exp)
  3912. (cond ((memq id defined)
  3913. (set! pass1-block-assignments
  3914. (cons id pass1-block-assignments)))
  3915. ((or (constant? exp)
  3916. (and (lambda? exp)
  3917. (list? (lambda.args exp))))
  3918. (set! defined (cons id defined))))
  3919. (make-begin
  3920. (list (make-assignment id exp)
  3921. (make-constant id))))
  3922. (benchmark-mode #f)
  3923. (issue-warnings #f)
  3924. (for-each (lambda (form)
  3925. (desugar-definitions form
  3926. global-syntactic-environment
  3927. make-toplevel-definition))
  3928. forms)
  3929. (set! global-syntactic-environment env0)
  3930. (benchmark-mode bmode)
  3931. (issue-warnings wmode)
  3932. (part2 (filter (lambda (id)
  3933. (not (memq id pass1-block-assignments)))
  3934. (reverse defined)))))
  3935. (define (part2 defined)
  3936. (set! pass1-block-compiling? #f)
  3937. (set! pass1-block-assignments '())
  3938. (set! pass1-block-inlines '())
  3939. (set! renaming-counter 0)
  3940. (let* ((rename (make-rename-procedure))
  3941. (alist (map (lambda (id)
  3942. (cons id (rename id)))
  3943. defined))
  3944. (definitions0 '()) ; for constants
  3945. (definitions1 '())) ; for lambda expressions
  3946. (define (make-toplevel-definition id exp)
  3947. (if (lambda? exp)
  3948. (doc.name-set! (lambda.doc exp) id))
  3949. (let ((probe (assq id alist)))
  3950. (if probe
  3951. (let ((id1 (cdr probe)))
  3952. (cond ((constant? exp)
  3953. (set! definitions0
  3954. (cons (make-assignment id exp)
  3955. definitions0))
  3956. (make-constant id))
  3957. ((lambda? exp)
  3958. (set! definitions1
  3959. (cons (make-assignment id1 exp)
  3960. definitions1))
  3961. (make-assignment
  3962. id
  3963. (make-lambda (lambda.args exp)
  3964. '() ; no definitions
  3965. '() ; R
  3966. '() ; F
  3967. '() ; G
  3968. '() ; decls
  3969. (lambda.doc exp)
  3970. (make-call
  3971. (make-variable id1)
  3972. (map make-variable
  3973. (lambda.args exp))))))
  3974. (else
  3975. (m-error "Inconsistent macro expansion"
  3976. (make-readable exp)))))
  3977. (make-assignment id exp))))
  3978. (let ((env0 (syntactic-copy global-syntactic-environment))
  3979. (bmode (benchmark-mode))
  3980. (wmode (issue-warnings)))
  3981. (issue-warnings #f)
  3982. (for-each (lambda (pair)
  3983. (let ((id0 (car pair))
  3984. (id1 (cdr pair)))
  3985. (syntactic-bind-globally!
  3986. id0
  3987. (make-inline-denotation
  3988. id0
  3989. (lambda (exp rename compare)
  3990. ; Deliberately non-hygienic!
  3991. (cons id1 (cdr exp)))
  3992. global-syntactic-environment))
  3993. (set! pass1-block-inlines
  3994. (cons id0 pass1-block-inlines))))
  3995. alist)
  3996. (benchmark-mode #f)
  3997. (issue-warnings wmode)
  3998. (let ((forms
  3999. (do ((forms forms (cdr forms))
  4000. (newforms '()
  4001. (cons (desugar-definitions
  4002. (car forms)
  4003. global-syntactic-environment
  4004. make-toplevel-definition)
  4005. newforms)))
  4006. ((null? forms)
  4007. (reverse newforms)))))
  4008. (benchmark-mode bmode)
  4009. (set! global-syntactic-environment env0)
  4010. (part3 alist definitions0 definitions1 forms)))))
  4011. (define (part3 alist definitions0 definitions1 forms)
  4012. (set! pass1-block-compiling? #f)
  4013. (set! pass1-block-assignments '())
  4014. (set! pass1-block-inlines '())
  4015. (let* ((constnames0 (map assignment.lhs definitions0))
  4016. (constnames1 (map (lambda (id0)
  4017. (cdr (assq id0 alist)))
  4018. constnames0))
  4019. (procnames1 (map assignment.lhs definitions1)))
  4020. (copy-exp
  4021. (make-call
  4022. (make-lambda
  4023. constnames1
  4024. '() ; no definitions
  4025. '() ; R
  4026. '() ; F
  4027. '() ; G
  4028. '() ; decls
  4029. #f ; doc
  4030. (make-begin
  4031. (list
  4032. (make-begin
  4033. (cons (make-constant #f)
  4034. (reverse
  4035. (map (lambda (id)
  4036. (make-assignment id (make-variable (cdr (assq id alist)))))
  4037. constnames0))))
  4038. (make-call
  4039. (make-lambda
  4040. constnames0
  4041. '() ; no definitions
  4042. '() ; R
  4043. '() ; F
  4044. '() ; G
  4045. '() ; decls
  4046. #f ; doc
  4047. (make-call
  4048. (make-lambda
  4049. (map assignment.lhs definitions1)
  4050. '() ; no definitions
  4051. '() ; R
  4052. '() ; F
  4053. '() ; G
  4054. '() ; decls
  4055. #f ; doc
  4056. (make-begin (cons (make-constant #f)
  4057. (append definitions1 forms))))
  4058. (map (lambda (ignored) (make-unspecified))
  4059. definitions1)))
  4060. (map make-variable constnames1))
  4061. )))
  4062. (map assignment.rhs definitions0)))))
  4063. (set! source-file-name #f)
  4064. (set! source-file-position #f)
  4065. (if (not (null? rest))
  4066. (begin (set! source-file-name (car rest))
  4067. (if (not (null? (cdr rest)))
  4068. (set! source-file-position (cadr rest)))))
  4069. (part1))
  4070. ; Copyright 1999 William D Clinger.
  4071. ;
  4072. ; Permission to copy this software, in whole or in part, to use this
  4073. ; software for any lawful noncommercial purpose, and to redistribute
  4074. ; this software is granted subject to the restriction that all copies
  4075. ; made of this software must include this copyright notice in full.
  4076. ;
  4077. ; I also request that you send me a copy of any improvements that you
  4078. ; make to this software so that they may be incorporated within it to
  4079. ; the benefit of the Scheme community.
  4080. ;
  4081. ; 7 June 1999.
  4082. ;
  4083. ; Support for intraprocedural value numbering:
  4084. ; set of available expressions
  4085. ; miscellaneous
  4086. ;
  4087. ; The set of available expressions is represented as a
  4088. ; mutable abstract data type Available with these operations:
  4089. ;
  4090. ; make-available-table: -> Available
  4091. ; copy-available-table: Available -> Available
  4092. ; available-expression: Available x Expr -> (symbol + {#f})
  4093. ; available-variable: Available x symbol -> Expr
  4094. ; available-extend!: Available x symbol x Expr x Killer ->
  4095. ; available-kill!: Available x Killer ->
  4096. ;
  4097. ; where Expr is of the form
  4098. ;
  4099. ; Expr --> W
  4100. ; | (W_0 W_1 ...)
  4101. ;
  4102. ; W --> (quote K)
  4103. ; | (begin I)
  4104. ;
  4105. ; and Killer is a fixnum, as defined later in this file.
  4106. ;
  4107. ; (make-available-table)
  4108. ; returns an empty table of available expressions.
  4109. ; (copy-available-table available)
  4110. ; copies the given table.
  4111. ; (available-expression available E)
  4112. ; returns the name of E if it is available in the table, else #f.
  4113. ; (available-variable available T)
  4114. ; returns a constant or variable to use in place of T, else #f.
  4115. ; (available-extend! available T E K)
  4116. ; adds the binding (T E) to the table, with Killer K.
  4117. ; If E is a variable and this binding is never killed, then copy
  4118. ; propagation will replace uses of T by uses of E; otherwise
  4119. ; commoning will replace uses of E by uses of T, until the
  4120. ; binding is killed.
  4121. ; (available-kill! available K)
  4122. ; removes all bindings whose Killer intersects K.
  4123. ;
  4124. ; (available-extend! available T E K) is very fast if the previous
  4125. ; operation on the table was (available-expression available E).
  4126. ; Implementation.
  4127. ;
  4128. ; Quick and dirty.
  4129. ; The available expressions are represented as a vector of 2 association
  4130. ; lists. The first list is used for common subexpression elimination,
  4131. ; and the second is used for copy and constant propagation.
  4132. ;
  4133. ; Each element of the first list is a binding of
  4134. ; a symbol T to an expression E, with killer K,
  4135. ; represented by the list (E T K).
  4136. ;
  4137. ; Each element of the second list is a binding of
  4138. ; a symbol T to an expression E, with killer K,
  4139. ; represented by the list (T E K).
  4140. ; The expression E will be a constant or variable.
  4141. (define (make-available-table)
  4142. (vector '() '()))
  4143. (define (copy-available-table available)
  4144. (vector (vector-ref available 0)
  4145. (vector-ref available 1)))
  4146. (define (available-expression available E)
  4147. (let ((binding (assoc E (vector-ref available 0))))
  4148. (if binding
  4149. (cadr binding)
  4150. #f)))
  4151. (define (available-variable available T)
  4152. (let ((binding (assq T (vector-ref available 1))))
  4153. (if binding
  4154. (cadr binding)
  4155. #f)))
  4156. (define (available-extend! available T E K)
  4157. (cond ((constant? E)
  4158. (vector-set! available
  4159. 1
  4160. (cons (list T E K)
  4161. (vector-ref available 1))))
  4162. ((and (variable? E)
  4163. (eq? K available:killer:none))
  4164. (vector-set! available
  4165. 1
  4166. (cons (list T E K)
  4167. (vector-ref available 1))))
  4168. (else
  4169. (vector-set! available
  4170. 0
  4171. (cons (list E T K)
  4172. (vector-ref available 0))))))
  4173. (define (available-kill! available K)
  4174. (vector-set! available
  4175. 0
  4176. (filter (lambda (binding)
  4177. (zero?
  4178. (logand K
  4179. (caddr binding))))
  4180. (vector-ref available 0)))
  4181. (vector-set! available
  4182. 1
  4183. (filter (lambda (binding)
  4184. (zero?
  4185. (logand K
  4186. (caddr binding))))
  4187. (vector-ref available 1))))
  4188. (define (available-intersect! available0 available1 available2)
  4189. (vector-set! available0
  4190. 0
  4191. (intersection (vector-ref available1 0)
  4192. (vector-ref available2 0)))
  4193. (vector-set! available0
  4194. 1
  4195. (intersection (vector-ref available1 1)
  4196. (vector-ref available2 1))))
  4197. ; The Killer concrete data type, represented as a fixnum.
  4198. ;
  4199. ; The set of side effects that can kill an available expression
  4200. ; are a subset of
  4201. ;
  4202. ; assignments to global variables
  4203. ; uses of SET-CAR!
  4204. ; uses of SET-CDR!
  4205. ; uses of STRING-SET!
  4206. ; uses of VECTOR-SET!
  4207. ;
  4208. ; This list is not complete. If we were trying to perform common
  4209. ; subexpression elimination on calls to PEEK-CHAR, for example,
  4210. ; then those calls would be killed by reads.
  4211. (define available:killer:globals 2)
  4212. (define available:killer:car 4)
  4213. (define available:killer:cdr 8)
  4214. (define available:killer:string 16) ; also bytevectors etc
  4215. (define available:killer:vector 32) ; also structures etc
  4216. (define available:killer:cell 64)
  4217. (define available:killer:io 128)
  4218. (define available:killer:none 0) ; none of the above
  4219. (define available:killer:all 1022) ; all of the above
  4220. (define available:killer:immortal 0) ; never killed
  4221. (define available:killer:dead 1023) ; never available
  4222. (define (available:killer-combine k1 k2)
  4223. (logior k1 k2))
  4224. ; Miscellaneous.
  4225. ; A simple lambda expression has no internal definitions at its head
  4226. ; and no declarations aside from A-normal form.
  4227. (define (simple-lambda? L)
  4228. (and (null? (lambda.defs L))
  4229. (every? (lambda (decl)
  4230. (eq? decl A-normal-form-declaration))
  4231. (lambda.decls L))))
  4232. ; A real call is a call whose procedure expression is
  4233. ; neither a lambda expression nor a primop.
  4234. (define (real-call? E)
  4235. (and (call? E)
  4236. (let ((proc (call.proc E)))
  4237. (and (not (lambda? proc))
  4238. (or (not (variable? proc))
  4239. (let ((f (variable.name proc)))
  4240. (or (not (integrate-usual-procedures))
  4241. (not (prim-entry f)))))))))
  4242. (define (prim-call E)
  4243. (and (call? E)
  4244. (let ((proc (call.proc E)))
  4245. (and (variable? proc)
  4246. (integrate-usual-procedures)
  4247. (prim-entry (variable.name proc))))))
  4248. (define (no-side-effects? E)
  4249. (or (constant? E)
  4250. (variable? E)
  4251. (lambda? E)
  4252. (and (conditional? E)
  4253. (no-side-effects? (if.test E))
  4254. (no-side-effects? (if.then E))
  4255. (no-side-effects? (if.else E)))
  4256. (and (call? E)
  4257. (let ((proc (call.proc E)))
  4258. (and (variable? proc)
  4259. (integrate-usual-procedures)
  4260. (let ((entry (prim-entry (variable.name proc))))
  4261. (and entry
  4262. (not (eq? available:killer:dead
  4263. (prim-lives-until entry))))))))))
  4264. ; Given a local variable, the expression within its scope, and
  4265. ; a list of local variables that are known to be used only once,
  4266. ; returns #t if the variable is used only once.
  4267. ;
  4268. ; The purpose of this routine is to recognize temporaries that
  4269. ; may once have had two or more uses because of CSE, but now have
  4270. ; only one use because of further CSE followed by dead code elimination.
  4271. (define (temporary-used-once? T E used-once)
  4272. (cond ((call? E)
  4273. (let ((proc (call.proc E))
  4274. (args (call.args E)))
  4275. (or (and (lambda? proc)
  4276. (not (memq T (lambda.F proc)))
  4277. (and (pair? args)
  4278. (null? (cdr args))
  4279. (temporary-used-once? T (car args) used-once)))
  4280. (do ((exprs (cons proc (call.args E))
  4281. (cdr exprs))
  4282. (n 0
  4283. (let ((exp (car exprs)))
  4284. (cond ((constant? exp)
  4285. n)
  4286. ((variable? exp)
  4287. (if (eq? T (variable.name exp))
  4288. (+ n 1)
  4289. n))
  4290. (else
  4291. ; Terminate the loop and return #f.
  4292. 2)))))
  4293. ((or (null? exprs)
  4294. (> n 1))
  4295. (= n 1))))))
  4296. (else
  4297. (memq T used-once))))
  4298. ; Register bindings.
  4299. (define (make-regbinding lhs rhs use)
  4300. (list lhs rhs use))
  4301. (define (regbinding.lhs x) (car x))
  4302. (define (regbinding.rhs x) (cadr x))
  4303. (define (regbinding.use x) (caddr x))
  4304. ; Given a list of register bindings, an expression E and its free variables F,
  4305. ; returns two values:
  4306. ; E with the register bindings wrapped around it
  4307. ; the free variables of the wrapped expression
  4308. (define (wrap-with-register-bindings regbindings E F)
  4309. (if (null? regbindings)
  4310. (values E F)
  4311. (let* ((regbinding (car regbindings))
  4312. (R (regbinding.lhs regbinding))
  4313. (x (regbinding.rhs regbinding)))
  4314. (wrap-with-register-bindings
  4315. (cdr regbindings)
  4316. (make-call (make-lambda (list R)
  4317. '()
  4318. '()
  4319. F
  4320. F
  4321. (list A-normal-form-declaration)
  4322. #f
  4323. E)
  4324. (list (make-variable x)))
  4325. (union (list x)
  4326. (difference F (list R)))))))
  4327. ; Returns two values:
  4328. ; the subset of regbindings that have x as their right hand side
  4329. ; the rest of regbindings
  4330. (define (register-bindings regbindings x)
  4331. (define (loop regbindings to-x others)
  4332. (cond ((null? regbindings)
  4333. (values to-x others))
  4334. ((eq? x (regbinding.rhs (car regbindings)))
  4335. (loop (cdr regbindings)
  4336. (cons (car regbindings) to-x)
  4337. others))
  4338. (else
  4339. (loop (cdr regbindings)
  4340. to-x
  4341. (cons (car regbindings) others)))))
  4342. (loop regbindings '() '()))
  4343. ; This procedure is called when the compiler can tell that an assertion
  4344. ; is never true.
  4345. (define (declaration-error E)
  4346. (if (issue-warnings)
  4347. (begin (display "WARNING: Assertion is false: ")
  4348. (write (make-readable E #t))
  4349. (newline))))
  4350. ; Representations, which form a subtype hierarchy.
  4351. ;
  4352. ; <rep> ::= <fixnum> | (<fixnum> <datum> ...)
  4353. ;
  4354. ; (<rep> <datum> ...) is a subtype of <rep>, but the non-fixnum
  4355. ; representations are otherwise interpreted by arbitrary code.
  4356. (define *nreps* 0)
  4357. (define *rep-encodings* '())
  4358. (define *rep-decodings* '())
  4359. (define *rep-subtypes* '())
  4360. (define *rep-joins* (make-bytevector 0))
  4361. (define *rep-meets* (make-bytevector 0))
  4362. (define *rep-joins-special* '#())
  4363. (define *rep-meets-special* '#())
  4364. (define (representation-error msg . stuff)
  4365. (apply error
  4366. (if (string? msg)
  4367. (string-append "Bug in flow analysis: " msg)
  4368. msg)
  4369. stuff))
  4370. (define (symbol->rep sym)
  4371. (let ((probe (assq sym *rep-encodings*)))
  4372. (if probe
  4373. (cdr probe)
  4374. (let ((rep *nreps*))
  4375. (set! *nreps* (+ *nreps* 1))
  4376. (if (> *nreps* 255)
  4377. (representation-error "Too many representation types"))
  4378. (set! *rep-encodings*
  4379. (cons (cons sym rep)
  4380. *rep-encodings*))
  4381. (set! *rep-decodings*
  4382. (cons (cons rep sym)
  4383. *rep-decodings*))
  4384. rep))))
  4385. (define (rep->symbol rep)
  4386. (if (pair? rep)
  4387. (cons (rep->symbol (car rep)) (cdr rep))
  4388. (let ((probe (assv rep *rep-decodings*)))
  4389. (if probe
  4390. (cdr probe)
  4391. 'unknown))))
  4392. (define (representation-table table)
  4393. (map (lambda (row)
  4394. (map (lambda (x)
  4395. (if (list? x)
  4396. (map symbol->rep x)
  4397. x))
  4398. row))
  4399. table))
  4400. ; DEFINE-SUBTYPE is how representation types are defined.
  4401. (define (define-subtype sym1 sym2)
  4402. (let* ((rep2 (symbol->rep sym2))
  4403. (rep1 (symbol->rep sym1)))
  4404. (set! *rep-subtypes*
  4405. (cons (cons rep1 rep2)
  4406. *rep-subtypes*))
  4407. sym1))
  4408. ; COMPUTE-TYPE-STRUCTURE! must be called before DEFINE-INTERSECTION.
  4409. (define (define-intersection sym1 sym2 sym3)
  4410. (let ((rep1 (symbol->rep sym1))
  4411. (rep2 (symbol->rep sym2))
  4412. (rep3 (symbol->rep sym3)))
  4413. (representation-aset! *rep-meets* rep1 rep2 rep3)
  4414. (representation-aset! *rep-meets* rep2 rep1 rep3)))
  4415. ;
  4416. (define (representation-aref bv i j)
  4417. (bytevector-ref bv (+ (* *nreps* i) j)))
  4418. (define (representation-aset! bv i j x)
  4419. (bytevector-set! bv (+ (* *nreps* i) j) x))
  4420. (define (compute-unions!)
  4421. ; Always define a bottom element.
  4422. (for-each (lambda (sym)
  4423. (define-subtype 'bottom sym))
  4424. (map car *rep-encodings*))
  4425. (let* ((debugging? #f)
  4426. (n *nreps*)
  4427. (n^2 (* n n))
  4428. (matrix (make-bytevector n^2)))
  4429. ; This code assumes there will always be a top element.
  4430. (define (lub rep1 rep2 subtype?)
  4431. (do ((i 0 (+ i 1))
  4432. (bounds '()
  4433. (if (and (subtype? rep1 i)
  4434. (subtype? rep2 i))
  4435. (cons i bounds)
  4436. bounds)))
  4437. ((= i n)
  4438. (car (twobit-sort subtype? bounds)))))
  4439. (define (join i j)
  4440. (lub i j (lambda (rep1 rep2)
  4441. (= 1 (representation-aref matrix rep1 rep2)))))
  4442. (define (compute-transitive-closure!)
  4443. (let ((changed? #f))
  4444. (define (loop)
  4445. (do ((i 0 (+ i 1)))
  4446. ((= i n))
  4447. (do ((k 0 (+ k 1)))
  4448. ((= k n))
  4449. (do ((j 0 (+ j 1))
  4450. (sum 0
  4451. (logior sum
  4452. (logand
  4453. (representation-aref matrix i j)
  4454. (representation-aref matrix j k)))))
  4455. ((= j n)
  4456. (if (> sum 0)
  4457. (let ((x (representation-aref matrix i k)))
  4458. (if (zero? x)
  4459. (begin
  4460. (set! changed? #t)
  4461. (representation-aset! matrix i k 1)))))))))
  4462. (if changed?
  4463. (begin (set! changed? #f)
  4464. (loop))))
  4465. (loop)))
  4466. (define (compute-joins!)
  4467. (let ((default (lambda (x y)
  4468. (error "Compiler bug: special meet or join" x y))))
  4469. (set! *rep-joins-special* (make-vector n default))
  4470. (set! *rep-meets-special* (make-vector n default)))
  4471. (set! *rep-joins* (make-bytevector n^2))
  4472. (set! *rep-meets* (make-bytevector n^2))
  4473. (do ((i 0 (+ i 1)))
  4474. ((= i n))
  4475. (do ((j 0 (+ j 1)))
  4476. ((= j n))
  4477. (representation-aset! *rep-joins*
  4478. i
  4479. j
  4480. (join i j)))))
  4481. (do ((i 0 (+ i 1)))
  4482. ((= i n))
  4483. (do ((j 0 (+ j 1)))
  4484. ((= j n))
  4485. (representation-aset! matrix i j 0))
  4486. (representation-aset! matrix i i 1))
  4487. (for-each (lambda (subtype)
  4488. (let ((rep1 (car subtype))
  4489. (rep2 (cdr subtype)))
  4490. (representation-aset! matrix rep1 rep2 1)))
  4491. *rep-subtypes*)
  4492. (compute-transitive-closure!)
  4493. (if debugging?
  4494. (do ((i 0 (+ i 1)))
  4495. ((= i n))
  4496. (do ((j 0 (+ j 1)))
  4497. ((= j n))
  4498. (write-char #\space)
  4499. (write (representation-aref matrix i j)))
  4500. (newline)))
  4501. (compute-joins!)
  4502. (set! *rep-subtypes* '())))
  4503. ; Intersections are not dual to unions because a conservative analysis
  4504. ; must always err on the side of the larger subtype.
  4505. ; COMPUTE-UNIONS! must be called before COMPUTE-INTERSECTIONS!.
  4506. (define (compute-intersections!)
  4507. (let ((n *nreps*))
  4508. (define (meet i j)
  4509. (let ((k (representation-union i j)))
  4510. (if (= i k)
  4511. j
  4512. i)))
  4513. (do ((i 0 (+ i 1)))
  4514. ((= i n))
  4515. (do ((j 0 (+ j 1)))
  4516. ((= j n))
  4517. (representation-aset! *rep-meets*
  4518. i
  4519. j
  4520. (meet i j))))))
  4521. (define (compute-type-structure!)
  4522. (compute-unions!)
  4523. (compute-intersections!))
  4524. (define (representation-subtype? rep1 rep2)
  4525. (equal? rep2 (representation-union rep1 rep2)))
  4526. (define (representation-union rep1 rep2)
  4527. (if (fixnum? rep1)
  4528. (if (fixnum? rep2)
  4529. (representation-aref *rep-joins* rep1 rep2)
  4530. (representation-union rep1 (car rep2)))
  4531. (if (fixnum? rep2)
  4532. (representation-union (car rep1) rep2)
  4533. (let ((r1 (car rep1))
  4534. (r2 (car rep2)))
  4535. (if (= r1 r2)
  4536. ((vector-ref *rep-joins-special* r1) rep1 rep2)
  4537. (representation-union r1 r2))))))
  4538. (define (representation-intersection rep1 rep2)
  4539. (if (fixnum? rep1)
  4540. (if (fixnum? rep2)
  4541. (representation-aref *rep-meets* rep1 rep2)
  4542. (representation-intersection rep1 (car rep2)))
  4543. (if (fixnum? rep2)
  4544. (representation-intersection (car rep1) rep2)
  4545. (let ((r1 (car rep1))
  4546. (r2 (car rep2)))
  4547. (if (= r1 r2)
  4548. ((vector-ref *rep-meets-special* r1) rep1 rep2)
  4549. (representation-intersection r1 r2))))))
  4550. ; For debugging.
  4551. (define (display-unions-and-intersections)
  4552. (let* ((column-width 10)
  4553. (columns/row (quotient 80 column-width)))
  4554. (define (display-symbol sym)
  4555. (let* ((s (symbol->string sym))
  4556. (n (string-length s)))
  4557. (if (< n column-width)
  4558. (begin (display s)
  4559. (display (make-string (- column-width n) #\space)))
  4560. (begin (display (substring s 0 (- column-width 1)))
  4561. (write-char #\space)))))
  4562. ; Display columns i to n.
  4563. (define (display-matrix f i n)
  4564. (display (make-string column-width #\space))
  4565. (do ((i i (+ i 1)))
  4566. ((= i n))
  4567. (display-symbol (rep->symbol i)))
  4568. (newline)
  4569. (newline)
  4570. (do ((k 0 (+ k 1)))
  4571. ((= k *nreps*))
  4572. (display-symbol (rep->symbol k))
  4573. (do ((i i (+ i 1)))
  4574. ((= i n))
  4575. (display-symbol (rep->symbol (f k i))))
  4576. (newline))
  4577. (newline)
  4578. (newline))
  4579. (display "Unions:")
  4580. (newline)
  4581. (newline)
  4582. (do ((i 0 (+ i columns/row)))
  4583. ((>= i *nreps*))
  4584. (display-matrix representation-union
  4585. i
  4586. (min *nreps* (+ i columns/row))))
  4587. (display "Intersections:")
  4588. (newline)
  4589. (newline)
  4590. (do ((i 0 (+ i columns/row)))
  4591. ((>= i *nreps*))
  4592. (display-matrix representation-intersection
  4593. i
  4594. (min *nreps* (+ i columns/row))))))
  4595. ; Operations that can be specialized.
  4596. ;
  4597. ; Format: (<name> (<arg-rep> ...) <specific-name>)
  4598. (define (rep-specific? f rs)
  4599. (rep-match f rs rep-specific caddr))
  4600. ; Operations whose result has some specific representation.
  4601. ;
  4602. ; Format: (<name> (<arg-rep> ...) (<result-rep>))
  4603. (define (rep-result? f rs)
  4604. (rep-match f rs rep-result caaddr))
  4605. ; Unary predicates that give information about representation.
  4606. ;
  4607. ; Format: (<name> <rep-if-true> <rep-if-false>)
  4608. (define (rep-if-true f rs)
  4609. (rep-match f rs rep-informing caddr))
  4610. (define (rep-if-false f rs)
  4611. (rep-match f rs rep-informing cadddr))
  4612. ; Given the name of an integrable primitive,
  4613. ; the representations of its arguments,
  4614. ; a representation table, and a selector function
  4615. ; finds the most type-specific row of the table that matches both
  4616. ; the name of the primitive and the representations of its arguments,
  4617. ; and returns the result of applying the selector to that row.
  4618. ; If no row matches, then REP-MATCH returns #f.
  4619. ;
  4620. ; FIXME: This should be more efficient, and should prefer the most
  4621. ; specific matches.
  4622. (define (rep-match f rs table selector)
  4623. (let ((n (length rs)))
  4624. (let loop ((entries table))
  4625. (cond ((null? entries)
  4626. #f)
  4627. ((eq? f (car (car entries)))
  4628. (let ((rs0 (cadr (car entries))))
  4629. (if (and (= n (length rs0))
  4630. (every? (lambda (r1+r2)
  4631. (let ((r1 (car r1+r2))
  4632. (r2 (cdr r1+r2)))
  4633. (representation-subtype? r1 r2)))
  4634. (map cons rs rs0)))
  4635. (selector (car entries))
  4636. (loop (cdr entries)))))
  4637. (else
  4638. (loop (cdr entries)))))))
  4639. ; Abstract interpretation with respect to types and constraints.
  4640. ; Returns a representation type.
  4641. (define (aeval E types constraints)
  4642. (cond ((call? E)
  4643. (let ((proc (call.proc E)))
  4644. (if (variable? proc)
  4645. (let* ((op (variable.name proc))
  4646. (argtypes (map (lambda (E)
  4647. (aeval E types constraints))
  4648. (call.args E)))
  4649. (type (rep-result? op argtypes)))
  4650. (if type
  4651. type
  4652. rep:object))
  4653. rep:object)))
  4654. ((variable? E)
  4655. (representation-typeof (variable.name E) types constraints))
  4656. ((constant? E)
  4657. (representation-of-value (constant.value E)))
  4658. (else
  4659. rep:object)))
  4660. ; If x has representation type t0 in the hash table,
  4661. ; and some further constraints
  4662. ;
  4663. ; x = (op y1 ... yn)
  4664. ; x : t1
  4665. ; ...
  4666. ; x : tk
  4667. ;
  4668. ; then
  4669. ;
  4670. ; typeof (x) = op (typeof (y1), ..., typeof (yn))
  4671. ; & t0 & t1 & ... & tk
  4672. ;
  4673. ; where & means intersection and op is the abstraction of op.
  4674. ;
  4675. ; Also if T : true and T = E then E may give information about
  4676. ; the types of other variables. Similarly for T : false.
  4677. (define (representation-typeof name types constraints)
  4678. (let ((t0 (hashtable-fetch types name rep:object))
  4679. (cs (hashtable-fetch (constraints.table constraints) name '())))
  4680. (define (loop type cs)
  4681. (if (null? cs)
  4682. type
  4683. (let* ((c (car cs))
  4684. (cs (cdr cs))
  4685. (E (constraint.rhs c)))
  4686. (cond ((constant? E)
  4687. (loop (representation-intersection type
  4688. (constant.value E))
  4689. cs))
  4690. ((call? E)
  4691. (loop (representation-intersection
  4692. type (aeval E types constraints))
  4693. cs))
  4694. (else
  4695. (loop type cs))))))
  4696. (loop t0 cs)))
  4697. ; Constraints.
  4698. ;
  4699. ; The constraints used by this analysis consist of type constraints
  4700. ; together with the available expressions used for commoning.
  4701. ;
  4702. ; (T E K) T = E until killed by an effect in K
  4703. ; (T '<rep> K) T : <rep> until killed by an effect in K
  4704. (define (make-constraint T E K)
  4705. (list T E K))
  4706. (define (constraint.lhs c)
  4707. (car c))
  4708. (define (constraint.rhs c)
  4709. (cadr c))
  4710. (define (constraint.killer c)
  4711. (caddr c))
  4712. (define (make-type-constraint T type K)
  4713. (make-constraint T
  4714. (make-constant type)
  4715. K))
  4716. ; If the new constraint is of the form T = E until killed by K,
  4717. ; then there shouldn't be any prior constraints.
  4718. ;
  4719. ; Otherwise the new constraint is of the form T : t until killed by K.
  4720. ; Suppose the prior constraints are
  4721. ; T = E until killed by K
  4722. ; T : t1 until killed by K1
  4723. ; ...
  4724. ; T : tn until killed by Kn
  4725. ;
  4726. ; If there exists i such that ti is a subtype of t and Ki a subset of K,
  4727. ; then the new constraint adds no new information and should be ignored.
  4728. ; Otherwise compute t' = t1 & ... & tn and K' = K1 | ... | Kn, where
  4729. ; & indicates intersection and | indicates union.
  4730. ; If K = K' then add the new constraint T : t' until killed by K;
  4731. ; otherwise add two new constraints:
  4732. ; T : t' until killed by K'
  4733. ; T : t until killed by K
  4734. (define (constraints-add! types constraints new)
  4735. (let* ((debugging? #f)
  4736. (T (constraint.lhs new))
  4737. (E (constraint.rhs new))
  4738. (K (constraint.killer new))
  4739. (cs (constraints-for-variable constraints T)))
  4740. (define (loop type K cs newcs)
  4741. (if (null? cs)
  4742. (cons (make-type-constraint T type K) newcs)
  4743. (let* ((c2 (car cs))
  4744. (cs (cdr cs))
  4745. (E2 (constraint.rhs c2))
  4746. (K2 (constraint.killer c2)))
  4747. (if (constant? E2)
  4748. (let* ((type2 (constant.value E2))
  4749. (type3 (representation-intersection type type2)))
  4750. (cond ((eq? type2 type3)
  4751. (if (= K2 (logand K K2))
  4752. (append newcs cs)
  4753. (loop (representation-intersection type type2)
  4754. (available:killer-combine K K2)
  4755. cs
  4756. (cons c2 newcs))))
  4757. ((representation-subtype? type type3)
  4758. (if (= K (logand K K2))
  4759. (loop type K cs newcs)
  4760. (loop type K cs (cons c2 newcs))))
  4761. (else
  4762. (loop type3
  4763. (available:killer-combine K K2)
  4764. cs
  4765. (cons c2 newcs)))))
  4766. (let* ((op (variable.name (call.proc E2)))
  4767. (args (call.args E2))
  4768. (argtypes (map (lambda (exp)
  4769. (aeval exp types constraints))
  4770. args)))
  4771. (cond ((representation-subtype? type rep:true)
  4772. (let ((reps (rep-if-true op argtypes)))
  4773. (if reps
  4774. (record-new-reps! args argtypes reps K2))))
  4775. ((representation-subtype? type rep:false)
  4776. (let ((reps (rep-if-false op argtypes)))
  4777. (if reps
  4778. (record-new-reps! args argtypes reps K2)))))
  4779. (loop type K cs (cons c2 newcs)))))))
  4780. (define (record-new-reps! args argtypes reps K2)
  4781. (if debugging?
  4782. (begin (write (list (map make-readable args)
  4783. (map rep->symbol argtypes)
  4784. (map rep->symbol reps)))
  4785. (newline)))
  4786. (for-each (lambda (arg type0 type1)
  4787. (if (not (representation-subtype? type0 type1))
  4788. (if (variable? arg)
  4789. (let ((name (variable.name arg)))
  4790. ; FIXME: In this context, a variable
  4791. ; should always be local so the hashtable
  4792. ; operation isn't necessary.
  4793. (if (hashtable-get types name)
  4794. (constraints-add!
  4795. types
  4796. constraints
  4797. (make-type-constraint
  4798. name
  4799. type1
  4800. (available:killer-combine K K2)))
  4801. (cerror
  4802. "Compiler bug: unexpected global: "
  4803. name))))))
  4804. args argtypes reps))
  4805. (if (not (zero? K))
  4806. (constraints-add-killedby! constraints T K))
  4807. (let* ((table (constraints.table constraints))
  4808. (cs (hashtable-fetch table T '())))
  4809. (cond ((constant? E)
  4810. ; It's a type constraint.
  4811. (let ((type (constant.value E)))
  4812. (if debugging?
  4813. (begin (display T)
  4814. (display " : ")
  4815. (display (rep->symbol type))
  4816. (newline)))
  4817. (let ((cs (loop type K cs '())))
  4818. (hashtable-put! table T cs)
  4819. constraints)))
  4820. (else
  4821. (if debugging?
  4822. (begin (display T)
  4823. (display " = ")
  4824. (display (make-readable E #t))
  4825. (newline)))
  4826. (if (not (null? cs))
  4827. (begin
  4828. (display "Compiler bug: ")
  4829. (write T)
  4830. (display " has unexpectedly nonempty constraints")
  4831. (newline)))
  4832. (hashtable-put! table T (list (list T E K)))
  4833. constraints)))))
  4834. ; Sets of constraints.
  4835. ;
  4836. ; The set of constraints is represented as (<hashtable> <killedby>),
  4837. ; where <hashtable> is a hashtable mapping variables to lists of
  4838. ; constraints as above, and <killedby> is a vector mapping basic killers
  4839. ; to lists of variables that need to be examined for constraints that
  4840. ; are killed by that basic killer.
  4841. (define number-of-basic-killers
  4842. (do ((i 0 (+ i 1))
  4843. (k 1 (+ k k)))
  4844. ((> k available:killer:dead)
  4845. i)))
  4846. (define (constraints.table constraints) (car constraints))
  4847. (define (constraints.killed constraints) (cadr constraints))
  4848. (define (make-constraints-table)
  4849. (list (make-hashtable symbol-hash assq)
  4850. (make-vector number-of-basic-killers '())))
  4851. (define (copy-constraints-table constraints)
  4852. (list (hashtable-copy (constraints.table constraints))
  4853. (list->vector (vector->list (constraints.killed constraints)))))
  4854. (define (constraints-for-variable constraints T)
  4855. (hashtable-fetch (constraints.table constraints) T '()))
  4856. (define (constraints-add-killedby! constraints T K0)
  4857. (if (not (zero? K0))
  4858. (let ((v (constraints.killed constraints)))
  4859. (do ((i 0 (+ i 1))
  4860. (k 1 (+ k k)))
  4861. ((= i number-of-basic-killers))
  4862. (if (not (zero? (logand k K0)))
  4863. (vector-set! v i (cons T (vector-ref v i))))))))
  4864. (define (constraints-kill! constraints K)
  4865. (if (not (zero? K))
  4866. (let ((table (constraints.table constraints))
  4867. (killed (constraints.killed constraints)))
  4868. (define (examine! T)
  4869. (let ((cs (filter (lambda (c)
  4870. (zero? (logand (constraint.killer c) K)))
  4871. (hashtable-fetch table T '()))))
  4872. (if (null? cs)
  4873. (hashtable-remove! table T)
  4874. (hashtable-put! table T cs))))
  4875. (do ((i 0 (+ i 1))
  4876. (j 1 (+ j j)))
  4877. ((= i number-of-basic-killers))
  4878. (if (not (zero? (logand j K)))
  4879. (begin (for-each examine! (vector-ref killed i))
  4880. (vector-set! killed i '())))))))
  4881. (define (constraints-intersect! constraints0 constraints1 constraints2)
  4882. (let ((table0 (constraints.table constraints0))
  4883. (table1 (constraints.table constraints1))
  4884. (table2 (constraints.table constraints2)))
  4885. (if (eq? table0 table1)
  4886. ; FIXME: Which is more efficient: to update the killed vector,
  4887. ; or not to update it? Both are safe.
  4888. (hashtable-for-each (lambda (T cs)
  4889. (if (not (null? cs))
  4890. (hashtable-put!
  4891. table0
  4892. T
  4893. (cs-intersect
  4894. (hashtable-fetch table2 T '())
  4895. cs))))
  4896. table1)
  4897. ; This case shouldn't ever happen, so it can be slow.
  4898. (begin
  4899. (constraints-intersect! constraints0 constraints0 constraints1)
  4900. (constraints-intersect! constraints0 constraints0 constraints2)))))
  4901. (define (cs-intersect cs1 cs2)
  4902. (define (loop cs init rep Krep)
  4903. (if (null? cs)
  4904. (values init rep Krep)
  4905. (let* ((c (car cs))
  4906. (cs (cdr cs))
  4907. (E2 (constraint.rhs c))
  4908. (K2 (constraint.killer c)))
  4909. (cond ((constant? E2)
  4910. (loop cs
  4911. init
  4912. (representation-intersection rep (constant.value E2))
  4913. (available:killer-combine Krep K2)))
  4914. ((call? E2)
  4915. (if init
  4916. (begin (display "Compiler bug in cs-intersect")
  4917. (break))
  4918. (loop cs c rep Krep)))
  4919. (else
  4920. (error "Compiler bug in cs-intersect"))))))
  4921. (call-with-values
  4922. (lambda ()
  4923. (loop cs1 #f rep:object available:killer:none))
  4924. (lambda (c1 rep1 Krep1)
  4925. (call-with-values
  4926. (lambda ()
  4927. (loop cs2 #f rep:object available:killer:none))
  4928. (lambda (c2 rep2 Krep2)
  4929. (let ((c (if (equal? c1 c2) c1 #f))
  4930. (rep (representation-union rep1 rep2))
  4931. (Krep (available:killer-combine Krep1 Krep2)))
  4932. (if (eq? rep rep:object)
  4933. (if c (list c) '())
  4934. (let ((T (constraint.lhs (car cs1))))
  4935. (if c
  4936. (list c (make-type-constraint T rep Krep))
  4937. (list (make-type-constraint T rep Krep)))))))))))
  4938. ; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
  4939. (define $gc.ephemeral 0)
  4940. (define $gc.tenuring 1)
  4941. (define $gc.full 2)
  4942. (define $mstat.wallocated-hi 0)
  4943. (define $mstat.wallocated-lo 1)
  4944. (define $mstat.wcollected-hi 2)
  4945. (define $mstat.wcollected-lo 3)
  4946. (define $mstat.wcopied-hi 4)
  4947. (define $mstat.wcopied-lo 5)
  4948. (define $mstat.gctime 6)
  4949. (define $mstat.wlive 7)
  4950. (define $mstat.gc-last-gen 8)
  4951. (define $mstat.gc-last-type 9)
  4952. (define $mstat.generations 10)
  4953. (define $mstat.g-gc-count 0)
  4954. (define $mstat.g-prom-count 1)
  4955. (define $mstat.g-gctime 2)
  4956. (define $mstat.g-wlive 3)
  4957. (define $mstat.g-np-youngp 4)
  4958. (define $mstat.g-np-oldp 5)
  4959. (define $mstat.g-np-j 6)
  4960. (define $mstat.g-np-k 7)
  4961. (define $mstat.g-alloc 8)
  4962. (define $mstat.g-target 9)
  4963. (define $mstat.g-promtime 10)
  4964. (define $mstat.remsets 11)
  4965. (define $mstat.r-apool 0)
  4966. (define $mstat.r-upool 1)
  4967. (define $mstat.r-ahash 2)
  4968. (define $mstat.r-uhash 3)
  4969. (define $mstat.r-hrec-hi 4)
  4970. (define $mstat.r-hrec-lo 5)
  4971. (define $mstat.r-hrem-hi 6)
  4972. (define $mstat.r-hrem-lo 7)
  4973. (define $mstat.r-hscan-hi 8)
  4974. (define $mstat.r-hscan-lo 9)
  4975. (define $mstat.r-wscan-hi 10)
  4976. (define $mstat.r-wscan-lo 11)
  4977. (define $mstat.r-ssbrec-hi 12)
  4978. (define $mstat.r-ssbrec-lo 13)
  4979. (define $mstat.r-np-p 14)
  4980. (define $mstat.fflushed-hi 12)
  4981. (define $mstat.fflushed-lo 13)
  4982. (define $mstat.wflushed-hi 14)
  4983. (define $mstat.wflushed-lo 15)
  4984. (define $mstat.stk-created 16)
  4985. (define $mstat.frestored-hi 17)
  4986. (define $mstat.frestored-lo 18)
  4987. (define $mstat.words-heap 19)
  4988. (define $mstat.words-remset 20)
  4989. (define $mstat.words-rts 21)
  4990. (define $mstat.swb-assign 22)
  4991. (define $mstat.swb-lhs-ok 23)
  4992. (define $mstat.swb-rhs-const 24)
  4993. (define $mstat.swb-not-xgen 25)
  4994. (define $mstat.swb-trans 26)
  4995. (define $mstat.rtime 27)
  4996. (define $mstat.stime 28)
  4997. (define $mstat.utime 29)
  4998. (define $mstat.minfaults 30)
  4999. (define $mstat.majfaults 31)
  5000. (define $mstat.np-remsetp 32)
  5001. (define $mstat.max-heap 33)
  5002. (define $mstat.promtime 34)
  5003. (define $mstat.wmoved-hi 35)
  5004. (define $mstat.wmoved-lo 36)
  5005. (define $mstat.vsize 37)
  5006. (define $g.reg0 12)
  5007. (define $r.reg8 44)
  5008. (define $r.reg9 48)
  5009. (define $r.reg10 52)
  5010. (define $r.reg11 56)
  5011. (define $r.reg12 60)
  5012. (define $r.reg13 64)
  5013. (define $r.reg14 68)
  5014. (define $r.reg15 72)
  5015. (define $r.reg16 76)
  5016. (define $r.reg17 80)
  5017. (define $r.reg18 84)
  5018. (define $r.reg19 88)
  5019. (define $r.reg20 92)
  5020. (define $r.reg21 96)
  5021. (define $r.reg22 100)
  5022. (define $r.reg23 104)
  5023. (define $r.reg24 108)
  5024. (define $r.reg25 112)
  5025. (define $r.reg26 116)
  5026. (define $r.reg27 120)
  5027. (define $r.reg28 124)
  5028. (define $r.reg29 128)
  5029. (define $r.reg30 132)
  5030. (define $r.reg31 136)
  5031. (define $g.stkbot 180)
  5032. (define $g.gccnt 420)
  5033. (define $m.alloc 1024)
  5034. (define $m.alloci 1032)
  5035. (define $m.gc 1040)
  5036. (define $m.addtrans 1048)
  5037. (define $m.stkoflow 1056)
  5038. (define $m.stkuflow 1072)
  5039. (define $m.creg 1080)
  5040. (define $m.creg-set! 1088)
  5041. (define $m.add 1096)
  5042. (define $m.subtract 1104)
  5043. (define $m.multiply 1112)
  5044. (define $m.quotient 1120)
  5045. (define $m.remainder 1128)
  5046. (define $m.divide 1136)
  5047. (define $m.modulo 1144)
  5048. (define $m.negate 1152)
  5049. (define $m.numeq 1160)
  5050. (define $m.numlt 1168)
  5051. (define $m.numle 1176)
  5052. (define $m.numgt 1184)
  5053. (define $m.numge 1192)
  5054. (define $m.zerop 1200)
  5055. (define $m.complexp 1208)
  5056. (define $m.realp 1216)
  5057. (define $m.rationalp 1224)
  5058. (define $m.integerp 1232)
  5059. (define $m.exactp 1240)
  5060. (define $m.inexactp 1248)
  5061. (define $m.exact->inexact 1256)
  5062. (define $m.inexact->exact 1264)
  5063. (define $m.make-rectangular 1272)
  5064. (define $m.real-part 1280)
  5065. (define $m.imag-part 1288)
  5066. (define $m.sqrt 1296)
  5067. (define $m.round 1304)
  5068. (define $m.truncate 1312)
  5069. (define $m.apply 1320)
  5070. (define $m.varargs 1328)
  5071. (define $m.typetag 1336)
  5072. (define $m.typetag-set 1344)
  5073. (define $m.break 1352)
  5074. (define $m.eqv 1360)
  5075. (define $m.partial-list->vector 1368)
  5076. (define $m.timer-exception 1376)
  5077. (define $m.exception 1384)
  5078. (define $m.singlestep 1392)
  5079. (define $m.syscall 1400)
  5080. (define $m.bvlcmp 1408)
  5081. (define $m.enable-interrupts 1416)
  5082. (define $m.disable-interrupts 1424)
  5083. (define $m.alloc-bv 1432)
  5084. (define $m.global-ex 1440)
  5085. (define $m.invoke-ex 1448)
  5086. (define $m.global-invoke-ex 1456)
  5087. (define $m.argc-ex 1464)
  5088. ; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
  5089. (define $r.g0 0)
  5090. (define $r.g1 1)
  5091. (define $r.g2 2)
  5092. (define $r.g3 3)
  5093. (define $r.g4 4)
  5094. (define $r.g5 5)
  5095. (define $r.g6 6)
  5096. (define $r.g7 7)
  5097. (define $r.o0 8)
  5098. (define $r.o1 9)
  5099. (define $r.o2 10)
  5100. (define $r.o3 11)
  5101. (define $r.o4 12)
  5102. (define $r.o5 13)
  5103. (define $r.o6 14)
  5104. (define $r.o7 15)
  5105. (define $r.l0 16)
  5106. (define $r.l1 17)
  5107. (define $r.l2 18)
  5108. (define $r.l3 19)
  5109. (define $r.l4 20)
  5110. (define $r.l5 21)
  5111. (define $r.l6 22)
  5112. (define $r.l7 23)
  5113. (define $r.i0 24)
  5114. (define $r.i1 25)
  5115. (define $r.i2 26)
  5116. (define $r.i3 27)
  5117. (define $r.i4 28)
  5118. (define $r.i5 29)
  5119. (define $r.i6 30)
  5120. (define $r.i7 31)
  5121. (define $r.result $r.o0)
  5122. (define $r.argreg2 $r.o1)
  5123. (define $r.argreg3 $r.o2)
  5124. (define $r.stkp $r.o3)
  5125. (define $r.stklim $r.i0)
  5126. (define $r.tmp1 $r.o4)
  5127. (define $r.tmp2 $r.o5)
  5128. (define $r.tmp0 $r.g1)
  5129. (define $r.e-top $r.i0)
  5130. (define $r.e-limit $r.o3)
  5131. (define $r.timer $r.i4)
  5132. (define $r.millicode $r.i7)
  5133. (define $r.globals $r.i7)
  5134. (define $r.reg0 $r.l0)
  5135. (define $r.reg1 $r.l1)
  5136. (define $r.reg2 $r.l2)
  5137. (define $r.reg3 $r.l3)
  5138. (define $r.reg4 $r.l4)
  5139. (define $r.reg5 $r.l5)
  5140. (define $r.reg6 $r.l6)
  5141. (define $r.reg7 $r.l7)
  5142. ; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
  5143. (define $ex.car 0)
  5144. (define $ex.cdr 1)
  5145. (define $ex.setcar 2)
  5146. (define $ex.setcdr 3)
  5147. (define $ex.add 10)
  5148. (define $ex.sub 11)
  5149. (define $ex.mul 12)
  5150. (define $ex.div 13)
  5151. (define $ex.lessp 14)
  5152. (define $ex.lesseqp 15)
  5153. (define $ex.equalp 16)
  5154. (define $ex.greatereqp 17)
  5155. (define $ex.greaterp 18)
  5156. (define $ex.quotient 19)
  5157. (define $ex.remainder 20)
  5158. (define $ex.modulo 21)
  5159. (define $ex.logior 22)
  5160. (define $ex.logand 23)
  5161. (define $ex.logxor 24)
  5162. (define $ex.lognot 25)
  5163. (define $ex.lsh 26)
  5164. (define $ex.rsha 27)
  5165. (define $ex.rshl 28)
  5166. (define $ex.e2i 29)
  5167. (define $ex.i2e 30)
  5168. (define $ex.exactp 31)
  5169. (define $ex.inexactp 32)
  5170. (define $ex.round 33)
  5171. (define $ex.trunc 34)
  5172. (define $ex.zerop 35)
  5173. (define $ex.neg 36)
  5174. (define $ex.abs 37)
  5175. (define $ex.realpart 38)
  5176. (define $ex.imagpart 39)
  5177. (define $ex.vref 40)
  5178. (define $ex.vset 41)
  5179. (define $ex.vlen 42)
  5180. (define $ex.pref 50)
  5181. (define $ex.pset 51)
  5182. (define $ex.plen 52)
  5183. (define $ex.sref 60)
  5184. (define $ex.sset 61)
  5185. (define $ex.slen 62)
  5186. (define $ex.bvref 70)
  5187. (define $ex.bvset 71)
  5188. (define $ex.bvlen 72)
  5189. (define $ex.bvlref 80)
  5190. (define $ex.bvlset 81)
  5191. (define $ex.bvllen 82)
  5192. (define $ex.vlref 90)
  5193. (define $ex.vlset 91)
  5194. (define $ex.vllen 92)
  5195. (define $ex.typetag 100)
  5196. (define $ex.typetagset 101)
  5197. (define $ex.apply 102)
  5198. (define $ex.argc 103)
  5199. (define $ex.vargc 104)
  5200. (define $ex.nonproc 105)
  5201. (define $ex.undef-global 106)
  5202. (define $ex.dump 107)
  5203. (define $ex.dumpfail 108)
  5204. (define $ex.timer 109)
  5205. (define $ex.unsupported 110)
  5206. (define $ex.int2char 111)
  5207. (define $ex.char2int 112)
  5208. (define $ex.mkbvl 113)
  5209. (define $ex.mkvl 114)
  5210. (define $ex.char<? 115)
  5211. (define $ex.char<=? 116)
  5212. (define $ex.char=? 117)
  5213. (define $ex.char>? 118)
  5214. (define $ex.char>=? 119)
  5215. (define $ex.bvfill 120)
  5216. (define $ex.enable-interrupts 121)
  5217. (define $ex.keyboard-interrupt 122)
  5218. (define $ex.arithmetic-exception 123)
  5219. (define $ex.global-invoke 124)
  5220. (define $ex.fx+ 140)
  5221. (define $ex.fx- 141)
  5222. (define $ex.fx-- 142)
  5223. (define $ex.fx= 143)
  5224. (define $ex.fx< 144)
  5225. (define $ex.fx<= 145)
  5226. (define $ex.fx> 146)
  5227. (define $ex.fx>= 147)
  5228. (define $ex.fxpositive? 148)
  5229. (define $ex.fxnegative? 149)
  5230. (define $ex.fxzero? 150)
  5231. (define $ex.fx* 151)
  5232. ; DO NOT EDIT THIS FILE. Edit the config file and rerun "config".
  5233. (define $tag.tagmask 7)
  5234. (define $tag.pair-tag 1)
  5235. (define $tag.vector-tag 3)
  5236. (define $tag.bytevector-tag 5)
  5237. (define $tag.procedure-tag 7)
  5238. (define $imm.vector-header 162)
  5239. (define $imm.bytevector-header 194)
  5240. (define $imm.procedure-header 254)
  5241. (define $imm.true 6)
  5242. (define $imm.false 2)
  5243. (define $imm.null 10)
  5244. (define $imm.unspecified 278)
  5245. (define $imm.eof 534)
  5246. (define $imm.undefined 790)
  5247. (define $imm.character 38)
  5248. (define $tag.vector-typetag 0)
  5249. (define $tag.rectnum-typetag 4)
  5250. (define $tag.ratnum-typetag 8)
  5251. (define $tag.symbol-typetag 12)
  5252. (define $tag.port-typetag 16)
  5253. (define $tag.structure-typetag 20)
  5254. (define $tag.bytevector-typetag 0)
  5255. (define $tag.string-typetag 4)
  5256. (define $tag.flonum-typetag 8)
  5257. (define $tag.compnum-typetag 12)
  5258. (define $tag.bignum-typetag 16)
  5259. (define $hdr.port 178)
  5260. (define $hdr.struct 182)
  5261. (define $p.codevector -3)
  5262. (define $p.constvector 1)
  5263. (define $p.linkoffset 5)
  5264. (define $p.reg0 5)
  5265. (define $p.codeoffset -1)
  5266. ; Copyright 1991 William Clinger
  5267. ;
  5268. ; Relatively target-independent information for Twobit's backend.
  5269. ;
  5270. ; 24 April 1999 / wdc
  5271. ;
  5272. ; Most of the definitions in this file can be extended or overridden by
  5273. ; target-specific definitions.
  5274. (define twobit-sort
  5275. (lambda (less? list) (compat:sort list less?)))
  5276. (define renaming-prefix ".")
  5277. ; The prefix used for cells introduced by the compiler.
  5278. (define cell-prefix (string-append renaming-prefix "CELL:"))
  5279. ; Names of global procedures that cannot be redefined or assigned
  5280. ; by ordinary code.
  5281. ; The expansion of quasiquote uses .cons and .list directly, so these
  5282. ; should not be changed willy-nilly.
  5283. ; Others may be used directly by a DEFINE-INLINE.
  5284. (define name:CHECK! '.check!)
  5285. (define name:CONS '.cons)
  5286. (define name:LIST '.list)
  5287. (define name:MAKE-CELL '.make-cell)
  5288. (define name:CELL-REF '.cell-ref)
  5289. (define name:CELL-SET! '.cell-set!)
  5290. (define name:IGNORED (string->symbol "IGNORED"))
  5291. (define name:CAR '.car)
  5292. (define name:CDR '.cdr)
  5293. ;(begin (eval `(define ,name:CONS cons))
  5294. ; (eval `(define ,name:LIST list))
  5295. ; (eval `(define ,name:MAKE-CELL list))
  5296. ; (eval `(define ,name:CELL-REF car))
  5297. ; (eval `(define ,name:CELL-SET! set-car!)))
  5298. ; If (INTEGRATE-USUAL-PROCEDURES) is true, then control optimization
  5299. ; recognizes calls to these procedures.
  5300. (define name:NOT 'not)
  5301. (define name:MEMQ 'memq)
  5302. (define name:MEMV 'memv)
  5303. ; If (INTEGRATE-USUAL-PROCEDURES) is true, then control optimization
  5304. ; recognizes calls to these procedures and also creates calls to them.
  5305. (define name:EQ? 'eq?)
  5306. (define name:EQV? 'eqv?)
  5307. ; Control optimization creates calls to these procedures,
  5308. ; which do not need to check their arguments.
  5309. (define name:FIXNUM? 'fixnum?)
  5310. (define name:CHAR? 'char?)
  5311. (define name:SYMBOL? 'symbol?)
  5312. (define name:FX< '<:fix:fix)
  5313. (define name:FX- 'fx-) ; non-checking version
  5314. (define name:CHAR->INTEGER 'char->integer) ; non-checking version
  5315. (define name:VECTOR-REF 'vector-ref:trusted)
  5316. ; Constant folding.
  5317. ; Prototype, will probably change in the future.
  5318. (define (constant-folding-entry name)
  5319. (assq name $usual-constant-folding-procedures$))
  5320. (define constant-folding-predicates cadr)
  5321. (define constant-folding-folder caddr)
  5322. (define $usual-constant-folding-procedures$
  5323. (let ((always? (lambda (x) #t))
  5324. (charcode? (lambda (n)
  5325. (and (number? n)
  5326. (exact? n)
  5327. (<= 0 n)
  5328. (< n 128))))
  5329. (ratnum? (lambda (n)
  5330. (and (number? n)
  5331. (exact? n)
  5332. (rational? n))))
  5333. ; smallint? is defined later.
  5334. (smallint? (lambda (n) (smallint? n))))
  5335. `(
  5336. ; This makes some assumptions about the host system.
  5337. (integer->char (,charcode?) ,integer->char)
  5338. (char->integer (,char?) ,char->integer)
  5339. (zero? (,ratnum?) ,zero?)
  5340. (< (,ratnum? ,ratnum?) ,<)
  5341. (<= (,ratnum? ,ratnum?) ,<=)
  5342. (= (,ratnum? ,ratnum?) ,=)
  5343. (>= (,ratnum? ,ratnum?) ,>=)
  5344. (> (,ratnum? ,ratnum?) ,>)
  5345. (+ (,ratnum? ,ratnum?) ,+)
  5346. (- (,ratnum? ,ratnum?) ,-)
  5347. (* (,ratnum? ,ratnum?) ,*)
  5348. (-- (,ratnum?) ,(lambda (x) (- 0 x)))
  5349. (eq? (,always? ,always?) ,eq?)
  5350. (eqv? (,always? ,always?) ,eqv?)
  5351. (equal? (,always? ,always?) ,equal?)
  5352. (memq (,always? ,list?) ,memq)
  5353. (memv (,always? ,list?) ,memv)
  5354. (member (,always? ,list?) ,member)
  5355. (assq (,always? ,list?) ,assq)
  5356. (assv (,always? ,list?) ,assv)
  5357. (assoc (,always? ,list?) ,assoc)
  5358. (length (,list?) ,length)
  5359. (fixnum? (,smallint?) ,smallint?)
  5360. (=:fix:fix (,smallint? ,smallint?) ,=)
  5361. (<:fix:fix (,smallint? ,smallint?) ,<)
  5362. (<=:fix:fix (,smallint? ,smallint?) ,<=)
  5363. (>:fix:fix (,smallint? ,smallint?) ,>)
  5364. (>=:fix:fix (,smallint? ,smallint?) ,>=)
  5365. )))
  5366. (begin '
  5367. (define (.check! flag exn . args)
  5368. (if (not flag)
  5369. (apply error "Runtime check exception: " exn args)))
  5370. #t)
  5371. ; Order matters. If f and g are both inlined, and the definition of g
  5372. ; uses f, then f should be defined before g.
  5373. (for-each pass1
  5374. `(
  5375. (define-inline car
  5376. (syntax-rules ()
  5377. ((car x0)
  5378. (let ((x x0))
  5379. (.check! (pair? x) ,$ex.car x)
  5380. (car:pair x)))))
  5381. (define-inline cdr
  5382. (syntax-rules ()
  5383. ((car x0)
  5384. (let ((x x0))
  5385. (.check! (pair? x) ,$ex.cdr x)
  5386. (cdr:pair x)))))
  5387. (define-inline vector-length
  5388. (syntax-rules ()
  5389. ((vector-length v0)
  5390. (let ((v v0))
  5391. (.check! (vector? v) ,$ex.vlen v)
  5392. (vector-length:vec v)))))
  5393. (define-inline vector-ref
  5394. (syntax-rules ()
  5395. ((vector-ref v0 i0)
  5396. (let ((v v0)
  5397. (i i0))
  5398. (.check! (fixnum? i) ,$ex.vref v i)
  5399. (.check! (vector? v) ,$ex.vref v i)
  5400. (.check! (<:fix:fix i (vector-length:vec v)) ,$ex.vref v i)
  5401. (.check! (>=:fix:fix i 0) ,$ex.vref v i)
  5402. (vector-ref:trusted v i)))))
  5403. (define-inline vector-set!
  5404. (syntax-rules ()
  5405. ((vector-set! v0 i0 x0)
  5406. (let ((v v0)
  5407. (i i0)
  5408. (x x0))
  5409. (.check! (fixnum? i) ,$ex.vset v i x)
  5410. (.check! (vector? v) ,$ex.vset v i x)
  5411. (.check! (<:fix:fix i (vector-length:vec v)) ,$ex.vset v i x)
  5412. (.check! (>=:fix:fix i 0) ,$ex.vset v i x)
  5413. (vector-set!:trusted v i x)))))
  5414. ; This transformation must make sure the entire list is freshly
  5415. ; allocated when an argument to LIST returns more than once.
  5416. (define-inline list
  5417. (syntax-rules ()
  5418. ((list)
  5419. '())
  5420. ((list ?e)
  5421. (cons ?e '()))
  5422. ((list ?e1 ?e2 ...)
  5423. (let* ((t1 ?e1)
  5424. (t2 (list ?e2 ...)))
  5425. (cons t1 t2)))))
  5426. ; This transformation must make sure the entire list is freshly
  5427. ; allocated when an argument to VECTOR returns more than once.
  5428. (define-inline vector
  5429. (syntax-rules ()
  5430. ((vector)
  5431. '#())
  5432. ((vector ?e)
  5433. (make-vector 1 ?e))
  5434. ((vector ?e1 ?e2 ...)
  5435. (letrec-syntax
  5436. ((vector-aux1
  5437. (... (syntax-rules ()
  5438. ((vector-aux1 () ?n ?exps ?indexes ?temps)
  5439. (vector-aux2 ?n ?exps ?indexes ?temps))
  5440. ((vector-aux1 (?exp1 ?exp2 ...) ?n ?exps ?indexes ?temps)
  5441. (vector-aux1 (?exp2 ...)
  5442. (+ ?n 1)
  5443. (?exp1 . ?exps)
  5444. (?n . ?indexes)
  5445. (t . ?temps))))))
  5446. (vector-aux2
  5447. (... (syntax-rules ()
  5448. ((vector-aux2 ?n (?exp1 ?exp2 ...) (?n1 ?n2 ...) (?t1 ?t2 ...))
  5449. (let* ((?t1 ?exp1)
  5450. (?t2 ?exp2)
  5451. ...
  5452. (v (make-vector ?n ?t1)))
  5453. (vector-set! v ?n2 ?t2)
  5454. ...
  5455. v))))))
  5456. (vector-aux1 (?e1 ?e2 ...) 0 () () ())))))
  5457. (define-inline cadddr
  5458. (syntax-rules ()
  5459. ((cadddr ?e)
  5460. (car (cdr (cdr (cdr ?e)))))))
  5461. (define-inline cddddr
  5462. (syntax-rules ()
  5463. ((cddddr ?e)
  5464. (cdr (cdr (cdr (cdr ?e)))))))
  5465. (define-inline cdddr
  5466. (syntax-rules ()
  5467. ((cdddr ?e)
  5468. (cdr (cdr (cdr ?e))))))
  5469. (define-inline caddr
  5470. (syntax-rules ()
  5471. ((caddr ?e)
  5472. (car (cdr (cdr ?e))))))
  5473. (define-inline cddr
  5474. (syntax-rules ()
  5475. ((cddr ?e)
  5476. (cdr (cdr ?e)))))
  5477. (define-inline cdar
  5478. (syntax-rules ()
  5479. ((cdar ?e)
  5480. (cdr (car ?e)))))
  5481. (define-inline cadr
  5482. (syntax-rules ()
  5483. ((cadr ?e)
  5484. (car (cdr ?e)))))
  5485. (define-inline caar
  5486. (syntax-rules ()
  5487. ((caar ?e)
  5488. (car (car ?e)))))
  5489. (define-inline make-vector
  5490. (syntax-rules ()
  5491. ((make-vector ?n)
  5492. (make-vector ?n '()))))
  5493. (define-inline make-string
  5494. (syntax-rules ()
  5495. ((make-string ?n)
  5496. (make-string ?n #\space))))
  5497. (define-inline =
  5498. (syntax-rules ()
  5499. ((= ?e1 ?e2 ?e3 ?e4 ...)
  5500. (let ((t ?e2))
  5501. (and (= ?e1 t)
  5502. (= t ?e3 ?e4 ...))))))
  5503. (define-inline <
  5504. (syntax-rules ()
  5505. ((< ?e1 ?e2 ?e3 ?e4 ...)
  5506. (let ((t ?e2))
  5507. (and (< ?e1 t)
  5508. (< t ?e3 ?e4 ...))))))
  5509. (define-inline >
  5510. (syntax-rules ()
  5511. ((> ?e1 ?e2 ?e3 ?e4 ...)
  5512. (let ((t ?e2))
  5513. (and (> ?e1 t)
  5514. (> t ?e3 ?e4 ...))))))
  5515. (define-inline <=
  5516. (syntax-rules ()
  5517. ((<= ?e1 ?e2 ?e3 ?e4 ...)
  5518. (let ((t ?e2))
  5519. (and (<= ?e1 t)
  5520. (<= t ?e3 ?e4 ...))))))
  5521. (define-inline >=
  5522. (syntax-rules ()
  5523. ((>= ?e1 ?e2 ?e3 ?e4 ...)
  5524. (let ((t ?e2))
  5525. (and (>= ?e1 t)
  5526. (>= t ?e3 ?e4 ...))))))
  5527. (define-inline +
  5528. (syntax-rules ()
  5529. ((+)
  5530. 0)
  5531. ((+ ?e)
  5532. ?e)
  5533. ((+ ?e1 ?e2 ?e3 ?e4 ...)
  5534. (+ (+ ?e1 ?e2) ?e3 ?e4 ...))))
  5535. (define-inline *
  5536. (syntax-rules ()
  5537. ((*)
  5538. 1)
  5539. ((* ?e)
  5540. ?e)
  5541. ((* ?e1 ?e2 ?e3 ?e4 ...)
  5542. (* (* ?e1 ?e2) ?e3 ?e4 ...))))
  5543. (define-inline -
  5544. (syntax-rules ()
  5545. ((- ?e)
  5546. (- 0 ?e))
  5547. ((- ?e1 ?e2 ?e3 ?e4 ...)
  5548. (- (- ?e1 ?e2) ?e3 ?e4 ...))))
  5549. (define-inline /
  5550. (syntax-rules ()
  5551. ((/ ?e)
  5552. (/ 1 ?e))
  5553. ((/ ?e1 ?e2 ?e3 ?e4 ...)
  5554. (/ (/ ?e1 ?e2) ?e3 ?e4 ...))))
  5555. (define-inline abs
  5556. (syntax-rules ()
  5557. ((abs ?z)
  5558. (let ((temp ?z))
  5559. (if (< temp 0)
  5560. (-- temp)
  5561. temp)))))
  5562. (define-inline negative?
  5563. (syntax-rules ()
  5564. ((negative? ?x)
  5565. (< ?x 0))))
  5566. (define-inline positive?
  5567. (syntax-rules ()
  5568. ((positive? ?x)
  5569. (> ?x 0))))
  5570. (define-inline eqv?
  5571. (transformer
  5572. (lambda (exp rename compare)
  5573. (let ((arg1 (cadr exp))
  5574. (arg2 (caddr exp)))
  5575. (define (constant? exp)
  5576. (or (boolean? exp)
  5577. (char? exp)
  5578. (and (pair? exp)
  5579. (= (length exp) 2)
  5580. (identifier? (car exp))
  5581. (compare (car exp) (rename 'quote))
  5582. (symbol? (cadr exp)))))
  5583. (if (or (constant? arg1)
  5584. (constant? arg2))
  5585. (cons (rename 'eq?) (cdr exp))
  5586. exp)))))
  5587. (define-inline memq
  5588. (syntax-rules (quote)
  5589. ((memq ?expr '(?datum ...))
  5590. (letrec-syntax
  5591. ((memq0
  5592. (... (syntax-rules (quote)
  5593. ((memq0 '?xx '(?d ...))
  5594. (let ((t1 '(?d ...)))
  5595. (memq1 '?xx t1 (?d ...))))
  5596. ((memq0 ?e '(?d ...))
  5597. (let ((t0 ?e)
  5598. (t1 '(?d ...)))
  5599. (memq1 t0 t1 (?d ...)))))))
  5600. (memq1
  5601. (... (syntax-rules ()
  5602. ((memq1 ?t0 ?t1 ())
  5603. #f)
  5604. ((memq1 ?t0 ?t1 (?d1 ?d2 ...))
  5605. (if (eq? ?t0 '?d1)
  5606. ?t1
  5607. (let ((?t1 (cdr ?t1)))
  5608. (memq1 ?t0 ?t1 (?d2 ...)))))))))
  5609. (memq0 ?expr '(?datum ...))))))
  5610. (define-inline memv
  5611. (transformer
  5612. (lambda (exp rename compare)
  5613. (let ((arg1 (cadr exp))
  5614. (arg2 (caddr exp)))
  5615. (if (or (boolean? arg1)
  5616. (fixnum? arg1)
  5617. (char? arg1)
  5618. (and (pair? arg1)
  5619. (= (length arg1) 2)
  5620. (identifier? (car arg1))
  5621. (compare (car arg1) (rename 'quote))
  5622. (symbol? (cadr arg1)))
  5623. (and (pair? arg2)
  5624. (= (length arg2) 2)
  5625. (identifier? (car arg2))
  5626. (compare (car arg2) (rename 'quote))
  5627. (every1? (lambda (x)
  5628. (or (boolean? x)
  5629. (fixnum? x)
  5630. (char? x)
  5631. (symbol? x)))
  5632. (cadr arg2))))
  5633. (cons (rename 'memq) (cdr exp))
  5634. exp)))))
  5635. (define-inline assv
  5636. (transformer
  5637. (lambda (exp rename compare)
  5638. (let ((arg1 (cadr exp))
  5639. (arg2 (caddr exp)))
  5640. (if (or (boolean? arg1)
  5641. (char? arg1)
  5642. (and (pair? arg1)
  5643. (= (length arg1) 2)
  5644. (identifier? (car arg1))
  5645. (compare (car arg1) (rename 'quote))
  5646. (symbol? (cadr arg1)))
  5647. (and (pair? arg2)
  5648. (= (length arg2) 2)
  5649. (identifier? (car arg2))
  5650. (compare (car arg2) (rename 'quote))
  5651. (every1? (lambda (y)
  5652. (and (pair? y)
  5653. (let ((x (car y)))
  5654. (or (boolean? x)
  5655. (char? x)
  5656. (symbol? x)))))
  5657. (cadr arg2))))
  5658. (cons (rename 'assq) (cdr exp))
  5659. exp)))))
  5660. (define-inline map
  5661. (syntax-rules (lambda)
  5662. ((map ?proc ?exp1 ?exp2 ...)
  5663. (letrec-syntax
  5664. ((loop
  5665. (... (syntax-rules (lambda)
  5666. ((loop 1 () (?y1 ?y2 ...) ?f ?exprs)
  5667. (loop 2 (?y1 ?y2 ...) ?f ?exprs))
  5668. ((loop 1 (?a1 ?a2 ...) (?y2 ...) ?f ?exprs)
  5669. (loop 1 (?a2 ...) (y1 ?y2 ...) ?f ?exprs))
  5670. ((loop 2 ?ys (lambda ?formals ?body) ?exprs)
  5671. (loop 3 ?ys (lambda ?formals ?body) ?exprs))
  5672. ((loop 2 ?ys (?f1 . ?f2) ?exprs)
  5673. (let ((f (?f1 . ?f2)))
  5674. (loop 3 ?ys f ?exprs)))
  5675. ; ?f must be a constant or variable.
  5676. ((loop 2 ?ys ?f ?exprs)
  5677. (loop 3 ?ys ?f ?exprs))
  5678. ((loop 3 (?y1 ?y2 ...) ?f (?e1 ?e2 ...))
  5679. (do ((?y1 ?e1 (cdr ?y1))
  5680. (?y2 ?e2 (cdr ?y2))
  5681. ...
  5682. (results '() (cons (?f (car ?y1) (car ?y2) ...)
  5683. results)))
  5684. ((or (null? ?y1) (null? ?y2) ...)
  5685. (reverse results))))))))
  5686. (loop 1 (?exp1 ?exp2 ...) () ?proc (?exp1 ?exp2 ...))))))
  5687. (define-inline for-each
  5688. (syntax-rules (lambda)
  5689. ((for-each ?proc ?exp1 ?exp2 ...)
  5690. (letrec-syntax
  5691. ((loop
  5692. (... (syntax-rules (lambda)
  5693. ((loop 1 () (?y1 ?y2 ...) ?f ?exprs)
  5694. (loop 2 (?y1 ?y2 ...) ?f ?exprs))
  5695. ((loop 1 (?a1 ?a2 ...) (?y2 ...) ?f ?exprs)
  5696. (loop 1 (?a2 ...) (y1 ?y2 ...) ?f ?exprs))
  5697. ((loop 2 ?ys (lambda ?formals ?body) ?exprs)
  5698. (loop 3 ?ys (lambda ?formals ?body) ?exprs))
  5699. ((loop 2 ?ys (?f1 . ?f2) ?exprs)
  5700. (let ((f (?f1 . ?f2)))
  5701. (loop 3 ?ys f ?exprs)))
  5702. ; ?f must be a constant or variable.
  5703. ((loop 2 ?ys ?f ?exprs)
  5704. (loop 3 ?ys ?f ?exprs))
  5705. ((loop 3 (?y1 ?y2 ...) ?f (?e1 ?e2 ...))
  5706. (do ((?y1 ?e1 (cdr ?y1))
  5707. (?y2 ?e2 (cdr ?y2))
  5708. ...)
  5709. ((or (null? ?y1) (null? ?y2) ...)
  5710. (if #f #f))
  5711. (?f (car ?y1) (car ?y2) ...)))))))
  5712. (loop 1 (?exp1 ?exp2 ...) () ?proc (?exp1 ?exp2 ...))))))
  5713. ))
  5714. (define extended-syntactic-environment
  5715. (syntactic-copy global-syntactic-environment))
  5716. (define (make-extended-syntactic-environment)
  5717. (syntactic-copy extended-syntactic-environment))
  5718. ; MacScheme machine assembly instructions.
  5719. (define instruction.op car)
  5720. (define instruction.arg1 cadr)
  5721. (define instruction.arg2 caddr)
  5722. (define instruction.arg3 cadddr)
  5723. ; Opcode table.
  5724. (define *mnemonic-names* '()) ; For readify-lap
  5725. (begin
  5726. '
  5727. (define *last-reserved-mnemonic* 32767) ; For consistency check
  5728. '
  5729. (define make-mnemonic
  5730. (let ((count 0))
  5731. (lambda (name)
  5732. (set! count (+ count 1))
  5733. (if (= count *last-reserved-mnemonic*)
  5734. (error "Error in make-mnemonic: conflict: " name))
  5735. (set! *mnemonic-names* (cons (cons count name) *mnemonic-names*))
  5736. count)))
  5737. '
  5738. (define (reserved-mnemonic name value)
  5739. (if (and (> value 0) (< value *last-reserved-mnemonic*))
  5740. (set! *last-reserved-mnemonic* value))
  5741. (set! *mnemonic-names* (cons (cons value name) *mnemonic-names*))
  5742. value)
  5743. #t)
  5744. (define make-mnemonic
  5745. (let ((count 0))
  5746. (lambda (name)
  5747. (set! count (+ count 1))
  5748. (set! *mnemonic-names* (cons (cons count name) *mnemonic-names*))
  5749. count)))
  5750. (define (reserved-mnemonic name ignored)
  5751. (make-mnemonic name))
  5752. (define $.linearize (reserved-mnemonic '.linearize -1)) ; unused?
  5753. (define $.label (reserved-mnemonic '.label 63))
  5754. (define $.proc (reserved-mnemonic '.proc 62)) ; proc entry point
  5755. (define $.cont (reserved-mnemonic '.cont 61)) ; return point
  5756. (define $.align (reserved-mnemonic '.align 60)) ; align code stream
  5757. (define $.asm (reserved-mnemonic '.asm 59)) ; in-line native code
  5758. (define $.proc-doc ; internal def proc info
  5759. (reserved-mnemonic '.proc-doc 58))
  5760. (define $.end ; end of code vector
  5761. (reserved-mnemonic '.end 57)) ; (asm internal)
  5762. (define $.singlestep ; insert singlestep point
  5763. (reserved-mnemonic '.singlestep 56)) ; (asm internal)
  5764. (define $.entry (reserved-mnemonic '.entry 55)) ; procedure entry point
  5765. ; (asm internal)
  5766. (define $op1 (make-mnemonic 'op1)) ; op prim
  5767. (define $op2 (make-mnemonic 'op2)) ; op2 prim,k
  5768. (define $op3 (make-mnemonic 'op3)) ; op3 prim,k1,k2
  5769. (define $op2imm (make-mnemonic 'op2imm)) ; op2imm prim,x
  5770. (define $const (make-mnemonic 'const)) ; const x
  5771. (define $global (make-mnemonic 'global)) ; global x
  5772. (define $setglbl (make-mnemonic 'setglbl)) ; setglbl x
  5773. (define $lexical (make-mnemonic 'lexical)) ; lexical m,n
  5774. (define $setlex (make-mnemonic 'setlex)) ; setlex m,n
  5775. (define $stack (make-mnemonic 'stack)) ; stack n
  5776. (define $setstk (make-mnemonic 'setstk)) ; setstk n
  5777. (define $load (make-mnemonic 'load)) ; load k,n
  5778. (define $store (make-mnemonic 'store)) ; store k,n
  5779. (define $reg (make-mnemonic 'reg)) ; reg k
  5780. (define $setreg (make-mnemonic 'setreg)) ; setreg k
  5781. (define $movereg (make-mnemonic 'movereg)) ; movereg k1,k2
  5782. (define $lambda (make-mnemonic 'lambda)) ; lambda x,n,doc
  5783. (define $lexes (make-mnemonic 'lexes)) ; lexes n,doc
  5784. (define $args= (make-mnemonic 'args=)) ; args= k
  5785. (define $args>= (make-mnemonic 'args>=)) ; args>= k
  5786. (define $invoke (make-mnemonic 'invoke)) ; invoke k
  5787. (define $save (make-mnemonic 'save)) ; save L,k
  5788. (define $setrtn (make-mnemonic 'setrtn)) ; setrtn L
  5789. (define $restore (make-mnemonic 'restore)) ; restore n ; deprecated
  5790. (define $pop (make-mnemonic 'pop)) ; pop k
  5791. (define $popstk (make-mnemonic 'popstk)) ; popstk ; for students
  5792. (define $return (make-mnemonic 'return)) ; return
  5793. (define $mvrtn (make-mnemonic 'mvrtn)) ; mvrtn ; NYI
  5794. (define $apply (make-mnemonic 'apply)) ; apply
  5795. (define $nop (make-mnemonic 'nop)) ; nop
  5796. (define $jump (make-mnemonic 'jump)) ; jump m,o
  5797. (define $skip (make-mnemonic 'skip)) ; skip L ; forward
  5798. (define $branch (make-mnemonic 'branch)) ; branch L
  5799. (define $branchf (make-mnemonic 'branchf)) ; branchf L
  5800. (define $check (make-mnemonic 'check)) ; check k1,k2,k3,L
  5801. (define $trap (make-mnemonic 'trap)) ; trap k1,k2,k3,exn
  5802. ; A peephole optimizer may define more instructions in some
  5803. ; target-specific file.
  5804. ; eof
  5805. ; Copyright 1991 William Clinger
  5806. ;
  5807. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  5808. ;
  5809. ; Larceny -- target-specific information for Twobit's SPARC backend.
  5810. ;
  5811. ; 11 June 1999 / wdc
  5812. ; The maximum number of fixed arguments that may be followed by a rest
  5813. ; argument. This limitation is removed by the macro expander.
  5814. (define @maxargs-with-rest-arg@ 30)
  5815. ; The number of MacScheme machine registers.
  5816. ; (They do not necessarily correspond to hardware registers.)
  5817. (define *nregs* 32)
  5818. (define *lastreg* (- *nregs* 1))
  5819. (define *fullregs* (quotient *nregs* 2))
  5820. ; The number of argument registers that are represented by hardware
  5821. ; registers.
  5822. (define *nhwregs* 8)
  5823. ; Variable names that indicate register targets.
  5824. (define *regnames*
  5825. (do ((alist '() (cons (cons (string->symbol
  5826. (string-append ".REG" (number->string r)))
  5827. r)
  5828. alist))
  5829. (r (- *nhwregs* 1) (- r 1)))
  5830. ((<= r 0)
  5831. alist)))
  5832. ; A non-inclusive upper bound for the instruction encodings.
  5833. (define *number-of-mnemonics* 72)
  5834. ; Integrable procedures and procedure-specific source code transformations.
  5835. ; Every integrable procedure that takes a varying number of arguments must
  5836. ; supply a transformation procedure to map calls into the fixed arity
  5837. ; required by the MacScheme machine instructions.
  5838. ; The table of integrable procedures.
  5839. ; Each entry is a list of the following items:
  5840. ;
  5841. ; procedure name
  5842. ; arity (or -1 for special primops like .check!)
  5843. ; procedure name to be used by the disassembler
  5844. ; predicate for immediate operands (or #f)
  5845. ; primop code in the MacScheme machine (not used by Larceny)
  5846. ; the effects that kill this primop's result
  5847. ; the effects of this primop that kill available expressions
  5848. (define (prim-entry name)
  5849. (assq name $usual-integrable-procedures$))
  5850. (define prim-arity cadr)
  5851. (define prim-opcodename caddr)
  5852. (define prim-immediate? cadddr)
  5853. (define (prim-primcode entry)
  5854. (car (cddddr entry)))
  5855. ; This predicate returns #t iff its argument will be represented
  5856. ; as a fixnum on the target machine.
  5857. (define smallint?
  5858. (let* ((least (- (expt 2 29)))
  5859. (greatest (- (- least) 1)))
  5860. (lambda (x)
  5861. (and (number? x)
  5862. (exact? x)
  5863. (integer? x)
  5864. (<= least x greatest)))))
  5865. (define (sparc-imm? x)
  5866. (and (fixnum? x)
  5867. (<= -1024 x 1023)))
  5868. (define (sparc-eq-imm? x)
  5869. (or (sparc-imm? x)
  5870. (eq? x #t)
  5871. (eq? x #f)
  5872. (eq? x '())))
  5873. (define (valid-typetag? x)
  5874. (and (fixnum? x)
  5875. (<= 0 x 7)))
  5876. (define (fixnum-primitives) #t)
  5877. (define (flonum-primitives) #t)
  5878. ; The table of primitives has been extended with
  5879. ; kill information used for commoning.
  5880. (define (prim-lives-until entry)
  5881. (list-ref entry 5))
  5882. (define (prim-kills entry)
  5883. (list-ref entry 6))
  5884. (define $usual-integrable-procedures$
  5885. (let ((:globals available:killer:globals)
  5886. (:car available:killer:car)
  5887. (:cdr available:killer:cdr)
  5888. (:string available:killer:string)
  5889. (:vector available:killer:vector)
  5890. (:cell available:killer:cell)
  5891. (:io available:killer:io)
  5892. (:none available:killer:none) ; none of the above
  5893. (:all available:killer:all) ; all of the above
  5894. (:immortal available:killer:immortal) ; never killed
  5895. (:dead available:killer:dead) ; never available
  5896. )
  5897. ; external arity internal immediate ignored killed kills
  5898. ; name name predicate by what
  5899. ; kind of
  5900. ; effect
  5901. `((break 0 break #f 3 ,:dead ,:all)
  5902. (creg 0 creg #f 7 ,:dead ,:all)
  5903. (unspecified 0 unspecified #f -1 ,:dead ,:none)
  5904. (undefined 0 undefined #f 8 ,:dead ,:none)
  5905. (eof-object 0 eof-object #f -1 ,:dead ,:none)
  5906. (enable-interrupts 1 enable-interrupts #f -1 ,:dead ,:all)
  5907. (disable-interrupts 0 disable-interrupts #f -1 ,:dead ,:all)
  5908. (typetag 1 typetag #f #x11 ,:dead ,:none)
  5909. (not 1 not #f #x18 ,:immortal ,:none)
  5910. (null? 1 null? #f #x19 ,:immortal ,:none)
  5911. (pair? 1 pair? #f #x1a ,:immortal ,:none)
  5912. (eof-object? 1 eof-object? #f -1 ,:immortal ,:none)
  5913. (port? 1 port? #f -1 ,:dead ,:none)
  5914. (structure? 1 structure? #f -1 ,:dead ,:none)
  5915. (car 1 car #f #x1b ,:car ,:none)
  5916. (,name:CAR 1 car #f #x1b ,:car ,:none)
  5917. (cdr 1 cdr #f #x1c ,:cdr ,:none)
  5918. (,name:CDR 1 cdr #f #x1c ,:cdr ,:none)
  5919. (symbol? 1 symbol? #f #x1f ,:immortal ,:none)
  5920. (number? 1 complex? #f #x20 ,:immortal ,:none)
  5921. (complex? 1 complex? #f #x20 ,:immortal ,:none)
  5922. (real? 1 rational? #f #x21 ,:immortal ,:none)
  5923. (rational? 1 rational? #f #x21 ,:immortal ,:none)
  5924. (integer? 1 integer? #f #x22 ,:immortal ,:none)
  5925. (fixnum? 1 fixnum? #f #x23 ,:immortal ,:none)
  5926. (flonum? 1 flonum? #f -1 ,:immortal ,:none)
  5927. (compnum? 1 compnum? #f -1 ,:immortal ,:none)
  5928. (exact? 1 exact? #f #x24 ,:immortal ,:none)
  5929. (inexact? 1 inexact? #f #x25 ,:immortal ,:none)
  5930. (exact->inexact 1 exact->inexact #f #x26 ,:immortal ,:none)
  5931. (inexact->exact 1 inexact->exact #f #x27 ,:immortal ,:none)
  5932. (round 1 round #f #x28 ,:immortal ,:none)
  5933. (truncate 1 truncate #f #x29 ,:immortal ,:none)
  5934. (zero? 1 zero? #f #x2c ,:immortal ,:none)
  5935. (-- 1 -- #f #x2d ,:immortal ,:none)
  5936. (lognot 1 lognot #f #x2f ,:immortal ,:none)
  5937. (real-part 1 real-part #f #x3e ,:immortal ,:none)
  5938. (imag-part 1 imag-part #f #x3f ,:immortal ,:none)
  5939. (char? 1 char? #f #x40 ,:immortal ,:none)
  5940. (char->integer 1 char->integer #f #x41 ,:immortal ,:none)
  5941. (integer->char 1 integer->char #f #x42 ,:immortal ,:none)
  5942. (string? 1 string? #f #x50 ,:immortal ,:none)
  5943. (string-length 1 string-length #f #x51 ,:immortal ,:none)
  5944. (vector? 1 vector? #f #x52 ,:immortal ,:none)
  5945. (vector-length 1 vector-length #f #x53 ,:immortal ,:none)
  5946. (bytevector? 1 bytevector? #f #x54 ,:immortal ,:none)
  5947. (bytevector-length 1 bytevector-length #f #x55 ,:immortal ,:none)
  5948. (bytevector-fill! 2 bytevector-fill! #f -1 ,:dead ,:string)
  5949. (make-bytevector 1 make-bytevector #f #x56 ,:dead ,:none)
  5950. (procedure? 1 procedure? #f #x58 ,:immortal ,:none)
  5951. (procedure-length 1 procedure-length #f #x59 ,:dead ,:none)
  5952. (make-procedure 1 make-procedure #f #x5a ,:dead ,:none)
  5953. (creg-set! 1 creg-set! #f #x71 ,:dead ,:none)
  5954. (,name:MAKE-CELL 1 make-cell #f #x7e ,:dead ,:none)
  5955. (,name:CELL-REF 1 cell-ref #f #x7f ,:cell ,:none)
  5956. (,name:CELL-SET! 2 cell-set! #f #xdf ,:dead ,:cell)
  5957. (typetag-set! 2 typetag-set! ,valid-typetag? #xa0 ,:dead ,:all)
  5958. (eq? 2 eq? ,sparc-eq-imm? #xa1 ,:immortal ,:none)
  5959. (eqv? 2 eqv? #f #xa2 ,:immortal ,:none)
  5960. (cons 2 cons #f #xa8 ,:dead ,:none)
  5961. (,name:CONS 2 cons #f #xa8 ,:dead ,:none)
  5962. (set-car! 2 set-car! #f #xa9 ,:dead ,:car)
  5963. (set-cdr! 2 set-cdr! #f #xaa ,:dead ,:cdr)
  5964. (+ 2 + ,sparc-imm? #xb0 ,:immortal ,:none)
  5965. (- 2 - ,sparc-imm? #xb1 ,:immortal ,:none)
  5966. (* 2 * ,sparc-imm? #xb2 ,:immortal ,:none)
  5967. (/ 2 / #f #xb3 ,:immortal ,:none)
  5968. (quotient 2 quotient #f #xb4 ,:immortal ,:none)
  5969. (< 2 < ,sparc-imm? #xb5 ,:immortal ,:none)
  5970. (<= 2 <= ,sparc-imm? #xb6 ,:immortal ,:none)
  5971. (= 2 = ,sparc-imm? #xb7 ,:immortal ,:none)
  5972. (> 2 > ,sparc-imm? #xb8 ,:immortal ,:none)
  5973. (>= 2 >= ,sparc-imm? #xb9 ,:immortal ,:none)
  5974. (logand 2 logand #f #xc0 ,:immortal ,:none)
  5975. (logior 2 logior #f #xc1 ,:immortal ,:none)
  5976. (logxor 2 logxor #f #xc2 ,:immortal ,:none)
  5977. (lsh 2 lsh #f #xc3 ,:immortal ,:none)
  5978. (rsha 2 rsha #f -1 ,:immortal ,:none)
  5979. (rshl 2 rshl #f -1 ,:immortal ,:none)
  5980. (rot 2 rot #f #xc4 ,:immortal ,:none)
  5981. (make-string 2 make-string #f -1 ,:dead ,:none)
  5982. (string-ref 2 string-ref ,sparc-imm? #xd1 ,:string ,:none)
  5983. (string-set! 3 string-set! ,sparc-imm? -1 ,:dead ,:string)
  5984. (make-vector 2 make-vector #f #xd2 ,:dead ,:none)
  5985. (vector-ref 2 vector-ref ,sparc-imm? #xd3 ,:vector ,:none)
  5986. (bytevector-ref 2 bytevector-ref ,sparc-imm? #xd5 ,:string ,:none)
  5987. (procedure-ref 2 procedure-ref #f #xd7 ,:dead ,:none)
  5988. (char<? 2 char<? ,char? #xe0 ,:immortal ,:none)
  5989. (char<=? 2 char<=? ,char? #xe1 ,:immortal ,:none)
  5990. (char=? 2 char=? ,char? #xe2 ,:immortal ,:none)
  5991. (char>? 2 char>? ,char? #xe3 ,:immortal ,:none)
  5992. (char>=? 2 char>=? ,char? #xe4 ,:immortal ,:none)
  5993. (sys$partial-list->vector 2 sys$partial-list->vector #f -1 ,:dead ,:all)
  5994. (vector-set! 3 vector-set! #f #xf1 ,:dead ,:vector)
  5995. (bytevector-set! 3 bytevector-set! #f #xf2 ,:dead ,:string)
  5996. (procedure-set! 3 procedure-set! #f #xf3 ,:dead ,:all)
  5997. (bytevector-like? 1 bytevector-like? #f -1 ,:immortal ,:none)
  5998. (vector-like? 1 vector-like? #f -1 ,:immortal ,:none)
  5999. (bytevector-like-ref 2 bytevector-like-ref #f -1 ,:string ,:none)
  6000. (bytevector-like-set! 3 bytevector-like-set! #f -1 ,:dead ,:string)
  6001. (sys$bvlcmp 2 sys$bvlcmp #f -1 ,:dead ,:all)
  6002. (vector-like-ref 2 vector-like-ref #f -1 ,:vector ,:none)
  6003. (vector-like-set! 3 vector-like-set! #f -1 ,:dead ,:vector)
  6004. (vector-like-length 1 vector-like-length #f -1 ,:immortal ,:none)
  6005. (bytevector-like-length 1 bytevector-like-length #f -1 ,:immortal ,:none)
  6006. (remainder 2 remainder #f -1 ,:immortal ,:none)
  6007. (sys$read-char 1 sys$read-char #f -1 ,:dead ,:io)
  6008. (gc-counter 0 gc-counter #f -1 ,:dead ,:none)
  6009. ,@(if (fixnum-primitives)
  6010. `((most-positive-fixnum
  6011. 0 most-positive-fixnum
  6012. #f -1 ,:immortal ,:none)
  6013. (most-negative-fixnum
  6014. 0 most-negative-fixnum
  6015. #f -1 ,:immortal ,:none)
  6016. (fx+ 2 fx+ ,sparc-imm? -1 ,:immortal ,:none)
  6017. (fx- 2 fx- ,sparc-imm? -1 ,:immortal ,:none)
  6018. (fx-- 1 fx-- #f -1 ,:immortal ,:none)
  6019. (fx* 2 fx* #f -1 ,:immortal ,:none)
  6020. (fx= 2 fx= ,sparc-imm? -1 ,:immortal ,:none)
  6021. (fx< 2 fx< ,sparc-imm? -1 ,:immortal ,:none)
  6022. (fx<= 2 fx<= ,sparc-imm? -1 ,:immortal ,:none)
  6023. (fx> 2 fx> ,sparc-imm? -1 ,:immortal ,:none)
  6024. (fx>= 2 fx>= ,sparc-imm? -1 ,:immortal ,:none)
  6025. (fxzero? 1 fxzero? #f -1 ,:immortal ,:none)
  6026. (fxpositive? 1 fxpositive? #f -1 ,:immortal ,:none)
  6027. (fxnegative? 1 fxnegative? #f -1 ,:immortal ,:none))
  6028. '())
  6029. ,@(if (flonum-primitives)
  6030. `((fl+ 2 + #f -1 ,:immortal ,:none)
  6031. (fl- 2 - #f -1 ,:immortal ,:none)
  6032. (fl-- 1 -- #f -1 ,:immortal ,:none)
  6033. (fl* 2 * #f -1 ,:immortal ,:none)
  6034. (fl= 2 = #f -1 ,:immortal ,:none)
  6035. (fl< 2 < #f -1 ,:immortal ,:none)
  6036. (fl<= 2 <= #f -1 ,:immortal ,:none)
  6037. (fl> 2 > #f -1 ,:immortal ,:none)
  6038. (fl>= 2 >= #f -1 ,:immortal ,:none))
  6039. '())
  6040. ; Added for CSE, representation analysis.
  6041. (,name:CHECK! -1 check! #f -1 ,:dead ,:none)
  6042. (vector-length:vec 1 vector-length:vec #f -1 ,:immortal ,:none)
  6043. (vector-ref:trusted 2 vector-ref:trusted ,sparc-imm? -1 ,:vector ,:none)
  6044. (vector-set!:trusted 3 vector-set!:trusted #f -1 ,:dead ,:vector)
  6045. (car:pair 1 car:pair #f -1 ,:car ,:none)
  6046. (cdr:pair 1 cdr:pair #f -1 ,:cdr ,:none)
  6047. (=:fix:fix 2 =:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
  6048. (<:fix:fix 2 <:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
  6049. (<=:fix:fix 2 <=:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
  6050. (>=:fix:fix 2 >=:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
  6051. (>:fix:fix 2 >:fix:fix ,sparc-imm? -1 ,:immortal ,:none)
  6052. ; Not yet implemented.
  6053. (+:idx:idx 2 +:idx:idx #f -1 ,:immortal ,:none)
  6054. (+:fix:fix 2 +:idx:idx #f -1 ,:immortal ,:none)
  6055. (+:exi:exi 2 +:idx:idx #f -1 ,:immortal ,:none)
  6056. (+:flo:flo 2 +:idx:idx #f -1 ,:immortal ,:none)
  6057. (=:flo:flo 2 =:flo:flo #f -1 ,:immortal ,:none)
  6058. (=:obj:flo 2 =:obj:flo #f -1 ,:immortal ,:none)
  6059. (=:flo:obj 2 =:flo:obj #f -1 ,:immortal ,:none)
  6060. )))
  6061. ; Not used by the Sparc assembler; for information only.
  6062. (define $immediate-primops$
  6063. '((typetag-set! #x80)
  6064. (eq? #x81)
  6065. (+ #x82)
  6066. (- #x83)
  6067. (< #x84)
  6068. (<= #x85)
  6069. (= #x86)
  6070. (> #x87)
  6071. (>= #x88)
  6072. (char<? #x89)
  6073. (char<=? #x8a)
  6074. (char=? #x8b)
  6075. (char>? #x8c)
  6076. (char>=? #x8d)
  6077. (string-ref #x90)
  6078. (vector-ref #x91)
  6079. (bytevector-ref #x92)
  6080. (bytevector-like-ref -1)
  6081. (vector-like-ref -1)
  6082. (fx+ -1)
  6083. (fx- -1)
  6084. (fx-- -1)
  6085. (fx= -1)
  6086. (fx< -1)
  6087. (fx<= -1)
  6088. (fx> -1)
  6089. (fx>= -1)))
  6090. ; Operations introduced by peephole optimizer.
  6091. (define $reg/op1/branchf ; reg/op1/branchf prim,k1,L
  6092. (make-mnemonic 'reg/op1/branchf))
  6093. (define $reg/op2/branchf ; reg/op2/branchf prim,k1,k2,L
  6094. (make-mnemonic 'reg/op2/branchf))
  6095. (define $reg/op2imm/branchf ; reg/op2imm/branchf prim,k1,x,L
  6096. (make-mnemonic 'reg/op2imm/branchf))
  6097. (define $reg/op1/check ; reg/op1/check prim,k1,k2,k3,k4,exn
  6098. (make-mnemonic 'reg/op1/check))
  6099. (define $reg/op2/check ; reg/op2/check prim,k1,k2,k3,k4,k5,exn
  6100. (make-mnemonic 'reg/op2/check))
  6101. (define $reg/op2imm/check ; reg/op2imm/check prim,k1,x,k2,k3,k4,exn
  6102. (make-mnemonic 'reg/op2imm/check))
  6103. (define $reg/op1/setreg ; reg/op1/setreg prim,k1,kr
  6104. (make-mnemonic 'reg/op1/setreg))
  6105. (define $reg/op2/setreg ; reg/op2/setreg prim,k1,k2,kr
  6106. (make-mnemonic 'reg/op2/setreg))
  6107. (define $reg/op2imm/setreg ; reg/op2imm/setreg prim,k1,x,kr
  6108. (make-mnemonic 'reg/op2imm/setreg))
  6109. (define $reg/branchf ; reg/branchf k, L
  6110. (make-mnemonic 'reg/branchf))
  6111. (define $reg/return ; reg/return k
  6112. (make-mnemonic 'reg/return))
  6113. (define $reg/setglbl ; reg/setglbl k,x
  6114. (make-mnemonic 'reg/setglbl))
  6115. (define $reg/op3 ; reg/op3 prim,k1,k2,k3
  6116. (make-mnemonic 'reg/op3))
  6117. (define $const/setreg ; const/setreg const,k
  6118. (make-mnemonic 'const/setreg))
  6119. (define $const/return ; const/return const
  6120. (make-mnemonic 'const/return))
  6121. (define $global/setreg ; global/setreg x,k
  6122. (make-mnemonic 'global/setreg))
  6123. (define $setrtn/branch ; setrtn/branch L,doc
  6124. (make-mnemonic 'setrtn/branch))
  6125. (define $setrtn/invoke ; setrtn/invoke L
  6126. (make-mnemonic 'setrtn/invoke))
  6127. (define $global/invoke ; global/invoke global,n
  6128. (make-mnemonic 'global/invoke))
  6129. ; misc
  6130. (define $cons 'cons)
  6131. (define $car:pair 'car)
  6132. (define $cdr:pair 'cdr)
  6133. ; eof
  6134. ; Target-specific representations.
  6135. ;
  6136. ; A few of these representation types must be specified for every target:
  6137. ; rep:object
  6138. ; rep:procedure
  6139. ; rep:true
  6140. ; rep:false
  6141. ; rep:bottom
  6142. (define-subtype 'true 'object) ; values that count as true
  6143. (define-subtype 'eqtype 'object) ; can use EQ? instead of EQV?
  6144. (define-subtype 'nonpointer 'eqtype) ; can omit write barrier
  6145. (define-subtype 'eqtype1 'eqtype) ; eqtypes excluding #f
  6146. (define-subtype 'boolean 'nonpointer)
  6147. (define-subtype 'truth 'eqtype1) ; { #t }
  6148. (define-subtype 'truth 'boolean)
  6149. (define-subtype 'false 'boolean) ; { #f }
  6150. (define-subtype 'eqtype1 'true)
  6151. (define-subtype 'procedure 'true)
  6152. (define-subtype 'vector 'true)
  6153. (define-subtype 'bytevector 'true)
  6154. (define-subtype 'string 'true)
  6155. (define-subtype 'pair 'true)
  6156. (define-subtype 'emptylist 'eqtype1)
  6157. (define-subtype 'emptylist 'nonpointer)
  6158. (define-subtype 'symbol 'eqtype1)
  6159. (define-subtype 'char 'eqtype1)
  6160. (define-subtype 'char 'nonpointer)
  6161. (define-subtype 'number 'true)
  6162. (define-subtype 'inexact 'number)
  6163. (define-subtype 'flonum 'inexact)
  6164. (define-subtype 'integer 'number)
  6165. (define-subtype 'exact 'number)
  6166. (define-subtype 'exactint 'integer)
  6167. (define-subtype 'exactint 'exact)
  6168. (define-subtype 'fixnum 'exactint)
  6169. (define-subtype '!fixnum 'fixnum) ; 0 <= n
  6170. (define-subtype 'fixnum! 'fixnum) ; n <= largest index
  6171. (define-subtype 'index '!fixnum)
  6172. (define-subtype 'index 'fixnum!)
  6173. (define-subtype 'zero 'index)
  6174. (define-subtype 'fixnum 'eqtype1)
  6175. (define-subtype 'fixnum 'nonpointer)
  6176. (compute-type-structure!)
  6177. ; If the intersection of rep1 and rep2 is known precisely,
  6178. ; but neither is a subtype of the other, then their intersection
  6179. ; should be declared explicitly.
  6180. ; Otherwise a conservative approximation will be used.
  6181. (define-intersection 'true 'eqtype 'eqtype1)
  6182. (define-intersection 'true 'boolean 'truth)
  6183. (define-intersection 'exact 'integer 'exactint)
  6184. (define-intersection '!fixnum 'fixnum! 'index)
  6185. ;(display-unions-and-intersections)
  6186. ; Parameters.
  6187. (define rep:min_fixnum (- (expt 2 29)))
  6188. (define rep:max_fixnum (- (expt 2 29) 1))
  6189. (define rep:max_index (- (expt 2 24) 1))
  6190. ; The representations we'll recognize for now.
  6191. (define rep:object (symbol->rep 'object))
  6192. (define rep:true (symbol->rep 'true))
  6193. (define rep:truth (symbol->rep 'truth))
  6194. (define rep:false (symbol->rep 'false))
  6195. (define rep:boolean (symbol->rep 'boolean))
  6196. (define rep:pair (symbol->rep 'pair))
  6197. (define rep:symbol (symbol->rep 'symbol))
  6198. (define rep:number (symbol->rep 'number))
  6199. (define rep:zero (symbol->rep 'zero))
  6200. (define rep:index (symbol->rep 'index))
  6201. (define rep:fixnum (symbol->rep 'fixnum))
  6202. (define rep:exactint (symbol->rep 'exactint))
  6203. (define rep:flonum (symbol->rep 'flonum))
  6204. (define rep:exact (symbol->rep 'exact))
  6205. (define rep:inexact (symbol->rep 'inexact))
  6206. (define rep:integer (symbol->rep 'integer))
  6207. ;(define rep:real (symbol->rep 'real))
  6208. (define rep:char (symbol->rep 'char))
  6209. (define rep:string (symbol->rep 'string))
  6210. (define rep:vector (symbol->rep 'vector))
  6211. (define rep:procedure (symbol->rep 'procedure))
  6212. (define rep:bottom (symbol->rep 'bottom))
  6213. ; Given the value of a quoted constant, return its representation.
  6214. (define (representation-of-value x)
  6215. (cond ((boolean? x)
  6216. (if x
  6217. rep:truth
  6218. rep:false))
  6219. ((pair? x)
  6220. rep:pair)
  6221. ((symbol? x)
  6222. rep:symbol)
  6223. ((number? x)
  6224. (cond ((and (exact? x)
  6225. (integer? x))
  6226. (cond ((zero? x)
  6227. rep:zero)
  6228. ((<= 0 x rep:max_index)
  6229. rep:index)
  6230. ((<= rep:min_fixnum
  6231. x
  6232. rep:max_fixnum)
  6233. rep:fixnum)
  6234. (else
  6235. rep:exactint)))
  6236. ((and (inexact? x)
  6237. (real? x))
  6238. rep:flonum)
  6239. (else
  6240. ; We're not tracking other numbers yet.
  6241. rep:number)))
  6242. ((char? x)
  6243. rep:char)
  6244. ((string? x)
  6245. rep:string)
  6246. ((vector? x)
  6247. rep:vector)
  6248. ; Everything counts as true except for #f.
  6249. (else
  6250. rep:true)))
  6251. ; Tables that express the representation-specific operations,
  6252. ; and the information about representations that are implied
  6253. ; by certain operations.
  6254. ; FIXME: Currently way incomplete, but good enough for testing.
  6255. (define rep-specific
  6256. (representation-table
  6257. ; When the procedure in the first column is called with
  6258. ; arguments described in the middle column, then the procedure
  6259. ; in the last column can be called instead.
  6260. '(
  6261. ;(+ (index index) +:idx:idx)
  6262. ;(+ (fixnum fixnum) +:fix:fix)
  6263. ;(- (index index) -:idx:idx)
  6264. ;(- (fixnum fixnum) -:fix:fix)
  6265. (= (fixnum fixnum) =:fix:fix)
  6266. (< (fixnum fixnum) <:fix:fix)
  6267. (<= (fixnum fixnum) <=:fix:fix)
  6268. (> (fixnum fixnum) >:fix:fix)
  6269. (>= (fixnum fixnum) >=:fix:fix)
  6270. ;(+ (flonum flonum) +:flo:flo)
  6271. ;(- (flonum flonum) -:flo:flo)
  6272. ;(= (flonum flonum) =:flo:flo)
  6273. ;(< (flonum flonum) <:flo:flo)
  6274. ;(<= (flonum flonum) <=:flo:flo)
  6275. ;(> (flonum flonum) >:flo:flo)
  6276. ;(>= (flonum flonum) >=:flo:flo)
  6277. ;(vector-set!:trusted (vector fixnum nonpointer) vector-set!:trusted:imm)
  6278. )))
  6279. (define rep-result
  6280. (representation-table
  6281. ; When the procedure in the first column is called with
  6282. ; arguments described in the middle column, then the result
  6283. ; is described by the last column.
  6284. '((fixnum? (fixnum) (truth))
  6285. (vector? (vector) (truth))
  6286. (<= (zero !fixnum) (truth))
  6287. (>= (!fixnum zero) (truth))
  6288. (<=:fix:fix (zero !fixnum) (truth))
  6289. (>=:fix:fix (!fixnum zero) (truth))
  6290. (+ (index index) (!fixnum))
  6291. (+ (fixnum fixnum) (exactint))
  6292. (- (index index) (fixnum!))
  6293. (- (fixnum fixnum) (exactint))
  6294. (+ (flonum flonum) (flonum))
  6295. (- (flonum flonum) (flonum))
  6296. ;(+:idx:idx (index index) (!fixnum))
  6297. ;(-:idx:idx (index index) (fixnum!))
  6298. ;(+:fix:fix (index index) (exactint))
  6299. ;(+:fix:fix (fixnum fixnum) (exactint))
  6300. ;(-:idx:idx (index index) (fixnum))
  6301. ;(-:fix:fix (fixnum fixnum) (exactint))
  6302. (make-vector (object object) (vector))
  6303. (vector-length:vec (vector) (index))
  6304. (cons (object object) (pair))
  6305. ; Is it really all that useful to know that the result
  6306. ; of these comparisons is a boolean?
  6307. (= (number number) (boolean))
  6308. (< (number number) (boolean))
  6309. (<= (number number) (boolean))
  6310. (> (number number) (boolean))
  6311. (>= (number number) (boolean))
  6312. (=:fix:fix (fixnum fixnum) (boolean))
  6313. (<:fix:fix (fixnum fixnum) (boolean))
  6314. (<=:fix:fix (fixnum fixnum) (boolean))
  6315. (>:fix:fix (fixnum fixnum) (boolean))
  6316. (>=:fix:fix (fixnum fixnum) (boolean))
  6317. )))
  6318. (define rep-informing
  6319. (representation-table
  6320. ; When the predicate in the first column is called in the test position
  6321. ; of a conditional expression, on arguments described by the second
  6322. ; column, then the arguments are described by the third column if the
  6323. ; predicate returns true, and by the fourth column if the predicate
  6324. ; returns false.
  6325. '(
  6326. (fixnum? (object) (fixnum) (object))
  6327. (flonum? (object) (flonum) (object))
  6328. (vector? (object) (vector) (object))
  6329. (pair? (object) (pair) (object))
  6330. (= (exactint index) (index index) (exactint index))
  6331. (= (index exactint) (index index) (index exactint))
  6332. (= (exactint !fixnum) (!fixnum !fixnum) (exactint !fixnum))
  6333. (= (!fixnum exactint) (!fixnum !fixnum) (!fixnum exactint))
  6334. (= (exactint fixnum!) (fixnum! fixnum!) (exactint fixnum!))
  6335. (= (fixnum! exactint) (fixnum! fixnum!) (fixnum! exactint))
  6336. (< (!fixnum fixnum!) (index index) (!fixnum fixnum!))
  6337. (< (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
  6338. (< (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
  6339. (< (fixnum! !fixnum) (fixnum! !fixnum) (index index))
  6340. (<= (!fixnum fixnum!) (index index) (!fixnum fixnum!))
  6341. (<= (fixnum! !fixnum) (fixnum! !fixnum) (index index))
  6342. (<= (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
  6343. (<= (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
  6344. (> (!fixnum fixnum!) (!fixnum fixnum!) (index index))
  6345. (> (fixnum! !fixnum) (index index) (fixnum! !fixnum))
  6346. (> (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
  6347. (> (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
  6348. (>= (!fixnum fixnum!) (!fixnum fixnum!) (index index))
  6349. (>= (fixnum! !fixnum) (index index) (fixnum! !fixnum))
  6350. (>= (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
  6351. (>= (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
  6352. (=:fix:fix (exactint index) (index index) (exactint index))
  6353. (=:fix:fix (index exactint) (index index) (index exactint))
  6354. (=:fix:fix (exactint !fixnum) (!fixnum !fixnum) (exactint !fixnum))
  6355. (=:fix:fix (!fixnum exactint) (!fixnum !fixnum) (!fixnum exactint))
  6356. (=:fix:fix (exactint fixnum!) (fixnum! fixnum!) (exactint fixnum!))
  6357. (=:fix:fix (fixnum! exactint) (fixnum! fixnum!) (fixnum! exactint))
  6358. (<:fix:fix (!fixnum fixnum!) (index index) (!fixnum fixnum!))
  6359. (<:fix:fix (fixnum! !fixnum) (fixnum! !fixnum) (index index))
  6360. (<:fix:fix (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
  6361. (<:fix:fix (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
  6362. (<=:fix:fix (!fixnum fixnum!) (index index) (!fixnum fixnum!))
  6363. (<=:fix:fix (fixnum! !fixnum) (fixnum! !fixnum) (index index))
  6364. (<=:fix:fix (fixnum fixnum!) (fixnum! fixnum!) (fixnum fixnum!))
  6365. (<=:fix:fix (!fixnum fixnum) (!fixnum !fixnum) (!fixnum fixnum))
  6366. (>:fix:fix (!fixnum fixnum!) (!fixnum fixnum!) (index index))
  6367. (>:fix:fix (fixnum! !fixnum) (index index) (fixnum! !fixnum))
  6368. (>:fix:fix (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
  6369. (>:fix:fix (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
  6370. (>=:fix:fix (!fixnum fixnum!) (!fixnum fixnum!) (index index))
  6371. (>=:fix:fix (fixnum! !fixnum) (index index) (fixnum! !fixnum))
  6372. (>=:fix:fix (fixnum fixnum!) (fixnum fixnum!) (fixnum! fixnum!))
  6373. (>=:fix:fix (!fixnum fixnum) (!fixnum fixnum) (!fixnum !fixnum))
  6374. )))
  6375. ; Copyright 1991 William D Clinger.
  6376. ;
  6377. ; Permission to copy this software, in whole or in part, to use this
  6378. ; software for any lawful noncommercial purpose, and to redistribute
  6379. ; this software is granted subject to the restriction that all copies
  6380. ; made of this software must include this copyright notice in full.
  6381. ;
  6382. ; I also request that you send me a copy of any improvements that you
  6383. ; make to this software so that they may be incorporated within it to
  6384. ; the benefit of the Scheme community.
  6385. ;
  6386. ; 25 April 1999.
  6387. ;
  6388. ; Second pass of the Twobit compiler:
  6389. ; single assignment analysis, local source transformations,
  6390. ; assignment elimination, and lambda lifting.
  6391. ; The code for assignment elimination and lambda lifting
  6392. ; are in a separate file.
  6393. ;
  6394. ; This pass operates as a source-to-source transformation on
  6395. ; expressions written in the subset of Scheme described by the
  6396. ; following grammar, where the input and output expressions
  6397. ; satisfy certain additional invariants described below.
  6398. ;
  6399. ; "X ..." means zero or more occurrences of X.
  6400. ;
  6401. ; L --> (lambda (I_1 ...)
  6402. ; (begin D ...)
  6403. ; (quote (R F G <decls> <doc>)
  6404. ; E)
  6405. ; | (lambda (I_1 ... . I_rest)
  6406. ; (begin D ...)
  6407. ; (quote (R F G <decls> <doc>))
  6408. ; E)
  6409. ; D --> (define I L)
  6410. ; E --> (quote K) ; constants
  6411. ; | (begin I) ; variable references
  6412. ; | L ; lambda expressions
  6413. ; | (E0 E1 ...) ; calls
  6414. ; | (set! I E) ; assignments
  6415. ; | (if E0 E1 E2) ; conditionals
  6416. ; | (begin E0 E1 E2 ...) ; sequential expressions
  6417. ; I --> <identifier>
  6418. ;
  6419. ; R --> ((I <references> <assignments> <calls>) ...)
  6420. ; F --> (I ...)
  6421. ; G --> (I ...)
  6422. ;
  6423. ; Invariants that hold for the input only:
  6424. ; * There are no internal definitions.
  6425. ; * No identifier containing an upper case letter is bound anywhere.
  6426. ; (Change the "name:..." variables if upper case is preferred.)
  6427. ; * No identifier is bound in more than one place.
  6428. ; * Each R contains one entry for every identifier bound in the
  6429. ; formal argument list and the internal definition list that
  6430. ; precede it. Each entry contains a list of pointers to all
  6431. ; references to the identifier, a list of pointers to all
  6432. ; assignments to the identifier, and a list of pointers to all
  6433. ; calls to the identifier.
  6434. ; * Except for constants, the expression does not share structure
  6435. ; with the original input or itself, except that the references
  6436. ; and assignments in R are guaranteed to share structure with
  6437. ; the expression. Thus the expression may be side effected, and
  6438. ; side effects to references or assignments obtained through R
  6439. ; are guaranteed to change the references or assignments pointed
  6440. ; to by R.
  6441. ;
  6442. ; Invariants that hold for the output only:
  6443. ; * There are no assignments except to global variables.
  6444. ; * If I is declared by an internal definition, then the right hand
  6445. ; side of the internal definition is a lambda expression and I
  6446. ; is referenced only in the procedure position of a call.
  6447. ; * Each R contains one entry for every identifier bound in the
  6448. ; formal argument list and the internal definition list that
  6449. ; precede it. Each entry contains a list of pointers to all
  6450. ; references to the identifier, a list of pointers to all
  6451. ; assignments to the identifier, and a list of pointers to all
  6452. ; calls to the identifier.
  6453. ; * For each lambda expression, the associated F is a list of all
  6454. ; the identifiers that occur free in the body of that lambda
  6455. ; expression, and possibly a few extra identifiers that were
  6456. ; once free but have been removed by optimization.
  6457. ; * For each lambda expression, the associated G is a subset of F
  6458. ; that contains every identifier that occurs free within some
  6459. ; inner lambda expression that escapes, and possibly a few that
  6460. ; don't. (Assignment-elimination does not calculate G exactly.)
  6461. ; * Variables named IGNORED are neither referenced nor assigned.
  6462. ; * Except for constants, the expression does not share structure
  6463. ; with the original input or itself, except that the references
  6464. ; and assignments in R are guaranteed to share structure with
  6465. ; the expression. Thus the expression may be side effected, and
  6466. ; side effects to references or assignments obtained through R
  6467. ; are guaranteed to change the references or assignments pointed
  6468. ; to by R.
  6469. (define (pass2 exp)
  6470. (simplify exp (make-notepad #f)))
  6471. ; Given an expression and a "notepad" data structure that conveys
  6472. ; inherited attributes, performs the appropriate optimizations and
  6473. ; destructively modifies the notepad to record various attributes
  6474. ; that it synthesizes while traversing the expression. In particular,
  6475. ; any nested lambda expressions and any variable references will be
  6476. ; noted in the notepad.
  6477. (define (simplify exp notepad)
  6478. (case (car exp)
  6479. ((quote) exp)
  6480. ((lambda) (simplify-lambda exp notepad))
  6481. ((set!) (simplify-assignment exp notepad))
  6482. ((if) (simplify-conditional exp notepad))
  6483. ((begin) (if (variable? exp)
  6484. (begin (notepad-var-add! notepad (variable.name exp))
  6485. exp)
  6486. (simplify-sequential exp notepad)))
  6487. (else (simplify-call exp notepad))))
  6488. ; Most optimization occurs here.
  6489. ; The right hand sides of internal definitions are simplified,
  6490. ; as is the body.
  6491. ; Internal definitions of enclosed lambda expressions may
  6492. ; then be lifted to this one.
  6493. ; Single assignment analysis creates internal definitions.
  6494. ; Single assignment elimination converts single assignments
  6495. ; to bindings where possible, and renames arguments whose value
  6496. ; is ignored.
  6497. ; Assignment elimination then replaces all remaining assigned
  6498. ; variables by heap-allocated cells.
  6499. (define (simplify-lambda exp notepad)
  6500. (notepad-lambda-add! notepad exp)
  6501. (let ((defs (lambda.defs exp))
  6502. (body (lambda.body exp))
  6503. (newnotepad (make-notepad exp)))
  6504. (for-each (lambda (def)
  6505. (simplify-lambda (def.rhs def) newnotepad))
  6506. defs)
  6507. (lambda.body-set! exp (simplify body newnotepad))
  6508. (lambda.F-set! exp (notepad-free-variables newnotepad))
  6509. (lambda.G-set! exp (notepad-captured-variables newnotepad))
  6510. (single-assignment-analysis exp newnotepad)
  6511. (let ((known-lambdas (notepad.nonescaping newnotepad)))
  6512. (for-each (lambda (L)
  6513. (if (memq L known-lambdas)
  6514. (lambda-lifting L exp)
  6515. (lambda-lifting L L)))
  6516. (notepad.lambdas newnotepad))))
  6517. (single-assignment-elimination exp notepad)
  6518. (assignment-elimination exp)
  6519. (if (not (notepad.parent notepad))
  6520. ; This is an outermost lambda expression.
  6521. (lambda-lifting exp exp))
  6522. exp)
  6523. ; SIMPLIFY-ASSIGNMENT performs this transformation:
  6524. ;
  6525. ; (set! I (begin ... E))
  6526. ; -> (begin ... (set! I E))
  6527. (define (simplify-assignment exp notepad)
  6528. (notepad-var-add! notepad (assignment.lhs exp))
  6529. (let ((rhs (simplify (assignment.rhs exp) notepad)))
  6530. (cond ((begin? rhs)
  6531. (let ((exprs (reverse (begin.exprs rhs))))
  6532. (assignment.rhs-set! exp (car exprs))
  6533. (post-simplify-begin
  6534. (make-begin (reverse (cons exp (cdr exprs))))
  6535. notepad)))
  6536. (else (assignment.rhs-set! exp rhs) exp))))
  6537. (define (simplify-sequential exp notepad)
  6538. (let ((exprs (map (lambda (exp) (simplify exp notepad))
  6539. (begin.exprs exp))))
  6540. (begin.exprs-set! exp exprs)
  6541. (post-simplify-begin exp notepad)))
  6542. ; Given (BEGIN E0 E1 E2 ...) where the E_i are simplified expressions,
  6543. ; flattens any nested BEGINs and removes trivial expressions that
  6544. ; don't appear in the last position. The second argument is used only
  6545. ; if a lambda expression is removed.
  6546. ; This procedure is careful to return E instead of (BEGIN E).
  6547. ; Fairly harmless bug: a variable reference removed by this procedure
  6548. ; may remain on the notepad when it shouldn't.
  6549. (define (post-simplify-begin exp notepad)
  6550. (let ((unspecified-expression (make-unspecified)))
  6551. ; (flatten exprs '()) returns the flattened exprs in reverse order.
  6552. (define (flatten exprs flattened)
  6553. (cond ((null? exprs) flattened)
  6554. ((begin? (car exprs))
  6555. (flatten (cdr exprs)
  6556. (flatten (begin.exprs (car exprs)) flattened)))
  6557. (else (flatten (cdr exprs) (cons (car exprs) flattened)))))
  6558. (define (filter exprs filtered)
  6559. (if (null? exprs)
  6560. filtered
  6561. (let ((exp (car exprs)))
  6562. (cond ((constant? exp) (filter (cdr exprs) filtered))
  6563. ((variable? exp) (filter (cdr exprs) filtered))
  6564. ((lambda? exp)
  6565. (notepad.lambdas-set!
  6566. notepad
  6567. (remq exp (notepad.lambdas notepad)))
  6568. (filter (cdr exprs) filtered))
  6569. ((equal? exp unspecified-expression)
  6570. (filter (cdr exprs) filtered))
  6571. (else (filter (cdr exprs) (cons exp filtered)))))))
  6572. (let ((exprs (flatten (begin.exprs exp) '())))
  6573. (begin.exprs-set! exp (filter (cdr exprs) (list (car exprs))))
  6574. (if (null? (cdr (begin.exprs exp)))
  6575. (car (begin.exprs exp))
  6576. exp))))
  6577. ; SIMPLIFY-CALL performs this transformation:
  6578. ;
  6579. ; (... (begin ... E) ...)
  6580. ; -> (begin ... (... E ...))
  6581. ;
  6582. ; It also takes care of LET transformations.
  6583. (define (simplify-call exp notepad)
  6584. (define (loop args newargs exprs)
  6585. (cond ((null? args)
  6586. (finish newargs exprs))
  6587. ((begin? (car args))
  6588. (let ((newexprs (reverse (begin.exprs (car args)))))
  6589. (loop (cdr args)
  6590. (cons (car newexprs) newargs)
  6591. (append (cdr newexprs) exprs))))
  6592. (else (loop (cdr args) (cons (car args) newargs) exprs))))
  6593. (define (finish newargs exprs)
  6594. (call.args-set! exp (reverse newargs))
  6595. (let* ((newexp
  6596. (if (lambda? (call.proc exp))
  6597. (simplify-let exp notepad)
  6598. (begin
  6599. (call.proc-set! exp
  6600. (simplify (call.proc exp) notepad))
  6601. exp)))
  6602. (newexp
  6603. (if (and (call? newexp)
  6604. (variable? (call.proc newexp)))
  6605. (let* ((procname (variable.name (call.proc newexp)))
  6606. (args (call.args newexp))
  6607. (entry
  6608. (and (not (null? args))
  6609. (constant? (car args))
  6610. (integrate-usual-procedures)
  6611. (every? constant? args)
  6612. (let ((entry (constant-folding-entry procname)))
  6613. (and entry
  6614. (let ((predicates
  6615. (constant-folding-predicates entry)))
  6616. (and (= (length args)
  6617. (length predicates))
  6618. (let loop ((args args)
  6619. (predicates predicates))
  6620. (cond ((null? args) entry)
  6621. (((car predicates)
  6622. (constant.value
  6623. (car args)))
  6624. (loop (cdr args)
  6625. (cdr predicates)))
  6626. (else #f))))))))))
  6627. (if entry
  6628. (make-constant (apply (constant-folding-folder entry)
  6629. (map constant.value args)))
  6630. newexp))
  6631. newexp)))
  6632. (cond ((and (call? newexp)
  6633. (begin? (call.proc newexp)))
  6634. (let ((exprs0 (reverse (begin.exprs (call.proc newexp)))))
  6635. (call.proc-set! newexp (car exprs0))
  6636. (post-simplify-begin
  6637. (make-begin (reverse
  6638. (cons newexp
  6639. (append (cdr exprs0) exprs))))
  6640. notepad)))
  6641. ((null? exprs)
  6642. newexp)
  6643. (else
  6644. (post-simplify-begin
  6645. (make-begin (reverse (cons newexp exprs)))
  6646. notepad)))))
  6647. (call.args-set! exp (map (lambda (arg) (simplify arg notepad))
  6648. (call.args exp)))
  6649. (loop (call.args exp) '() '()))
  6650. ; SIMPLIFY-LET performs these transformations:
  6651. ;
  6652. ; ((lambda (I_1 ... I_k . I_rest) ---) E1 ... Ek Ek+1 ...)
  6653. ; -> ((lambda (I_1 ... I_k I_rest) ---) E1 ... Ek (LIST Ek+1 ...))
  6654. ;
  6655. ; ((lambda (I1 I2 ...) (begin D ...) (quote ...) E) L ...)
  6656. ; -> ((lambda (I2 ...) (begin (define I1 L) D ...) (quote ...) E) ...)
  6657. ;
  6658. ; provided I1 is not assigned and each reference to I1 is in call position.
  6659. ;
  6660. ; ((lambda (I1)
  6661. ; (begin)
  6662. ; (quote ((I1 ((begin I1)) () ())))
  6663. ; (begin I1))
  6664. ; E1)
  6665. ;
  6666. ; -> E1
  6667. ;
  6668. ; ((lambda (I1)
  6669. ; (begin)
  6670. ; (quote ((I1 ((begin I1)) () ())))
  6671. ; (if (begin I1) E2 E3))
  6672. ; E1)
  6673. ;
  6674. ; -> (if E1 E2 E3)
  6675. ;
  6676. ; (Together with SIMPLIFY-CONDITIONAL, this cleans up the output of the OR
  6677. ; macro and enables certain control optimizations.)
  6678. ;
  6679. ; ((lambda (I1 I2 ...)
  6680. ; (begin D ...)
  6681. ; (quote (... (I <references> () <calls>) ...) ...)
  6682. ; E)
  6683. ; K ...)
  6684. ; -> ((lambda (I2 ...)
  6685. ; (begin D' ...)
  6686. ; (quote (... ...) ...)
  6687. ; E')
  6688. ; ...)
  6689. ;
  6690. ; where D' ... and E' ... are obtained from D ... and E ...
  6691. ; by replacing all references to I1 by K. This transformation
  6692. ; applies if K is a constant that can be duplicated without changing
  6693. ; its EQV? behavior.
  6694. ;
  6695. ; ((lambda () (begin) (quote ...) E)) -> E
  6696. ;
  6697. ; ((lambda (IGNORED I2 ...) ---) E1 E2 ...)
  6698. ; -> (begin E1 ((lambda (I2 ...) ---) E2 ...))
  6699. ;
  6700. ; (Single assignment analysis, performed by the simplifier for lambda
  6701. ; expressions, detects unused arguments and replaces them in the argument
  6702. ; list by the special identifier IGNORED.)
  6703. (define (simplify-let exp notepad)
  6704. (define proc (call.proc exp))
  6705. ; Loop1 operates before simplification of the lambda body.
  6706. (define (loop1 formals actuals processed-formals processed-actuals)
  6707. (cond ((null? formals)
  6708. (if (not (null? actuals))
  6709. (pass2-error p2error:wna exp))
  6710. (return1 processed-formals processed-actuals))
  6711. ((symbol? formals)
  6712. (return1 (cons formals processed-formals)
  6713. (cons (make-call-to-LIST actuals) processed-actuals)))
  6714. ((null? actuals)
  6715. (pass2-error p2error:wna exp)
  6716. (return1 processed-formals
  6717. processed-actuals))
  6718. ((and (lambda? (car actuals))
  6719. (let ((Rinfo (R-lookup (lambda.R proc) (car formals))))
  6720. (and (null? (R-entry.assignments Rinfo))
  6721. (= (length (R-entry.references Rinfo))
  6722. (length (R-entry.calls Rinfo))))))
  6723. (let ((I (car formals))
  6724. (L (car actuals)))
  6725. (notepad-nonescaping-add! notepad L)
  6726. (lambda.defs-set! proc
  6727. (cons (make-definition I L)
  6728. (lambda.defs proc)))
  6729. (standardize-known-calls L
  6730. (R-entry.calls
  6731. (R-lookup (lambda.R proc) I)))
  6732. (lambda.F-set! proc (union (lambda.F proc)
  6733. (free-variables L)))
  6734. (lambda.G-set! proc (union (lambda.G proc) (lambda.G L))))
  6735. (loop1 (cdr formals)
  6736. (cdr actuals)
  6737. processed-formals
  6738. processed-actuals))
  6739. ((and (constant? (car actuals))
  6740. (let ((x (constant.value (car actuals))))
  6741. (or (boolean? x)
  6742. (number? x)
  6743. (symbol? x)
  6744. (char? x))))
  6745. (let* ((I (car formals))
  6746. (Rinfo (R-lookup (lambda.R proc) I)))
  6747. (if (null? (R-entry.assignments Rinfo))
  6748. (begin
  6749. (for-each (lambda (ref)
  6750. (variable-set! ref (car actuals)))
  6751. (R-entry.references Rinfo))
  6752. (lambda.R-set! proc (remq Rinfo (lambda.R proc)))
  6753. (lambda.F-set! proc (remq I (lambda.F proc)))
  6754. (lambda.G-set! proc (remq I (lambda.G proc)))
  6755. (loop1 (cdr formals)
  6756. (cdr actuals)
  6757. processed-formals
  6758. processed-actuals))
  6759. (loop1 (cdr formals)
  6760. (cdr actuals)
  6761. (cons (car formals) processed-formals)
  6762. (cons (car actuals) processed-actuals)))))
  6763. (else (if (null? actuals)
  6764. (pass2-error p2error:wna exp))
  6765. (loop1 (cdr formals)
  6766. (cdr actuals)
  6767. (cons (car formals) processed-formals)
  6768. (cons (car actuals) processed-actuals)))))
  6769. (define (return1 rev-formals rev-actuals)
  6770. (let ((formals (reverse rev-formals))
  6771. (actuals (reverse rev-actuals)))
  6772. (lambda.args-set! proc formals)
  6773. (if (and (not (null? formals))
  6774. (null? (cdr formals))
  6775. (let* ((x (car formals))
  6776. (R (lambda.R proc))
  6777. (refs (references R x)))
  6778. (and (= 1 (length refs))
  6779. (null? (assignments R x)))))
  6780. (let ((x (car formals))
  6781. (body (lambda.body proc)))
  6782. (cond ((and (variable? body)
  6783. (eq? x (variable.name body)))
  6784. (simplify (car actuals) notepad))
  6785. ((and (conditional? body)
  6786. (let ((B0 (if.test body)))
  6787. (variable? B0)
  6788. (eq? x (variable.name B0))))
  6789. (if.test-set! body (car actuals))
  6790. (simplify body notepad))
  6791. (else
  6792. (return1-finish formals actuals))))
  6793. (return1-finish formals actuals))))
  6794. (define (return1-finish formals actuals)
  6795. (simplify-lambda proc notepad)
  6796. (loop2 formals actuals '() '() '()))
  6797. ; Loop2 operates after simplification of the lambda body.
  6798. (define (loop2 formals actuals processed-formals processed-actuals for-effect)
  6799. (cond ((null? formals)
  6800. (return2 processed-formals processed-actuals for-effect))
  6801. ((ignored? (car formals))
  6802. (loop2 (cdr formals)
  6803. (cdr actuals)
  6804. processed-formals
  6805. processed-actuals
  6806. (cons (car actuals) for-effect)))
  6807. (else (loop2 (cdr formals)
  6808. (cdr actuals)
  6809. (cons (car formals) processed-formals)
  6810. (cons (car actuals) processed-actuals)
  6811. for-effect))))
  6812. (define (return2 rev-formals rev-actuals rev-for-effect)
  6813. (let ((formals (reverse rev-formals))
  6814. (actuals (reverse rev-actuals))
  6815. (for-effect (reverse rev-for-effect)))
  6816. (lambda.args-set! proc formals)
  6817. (call.args-set! exp actuals)
  6818. (let ((exp (if (and (null? actuals)
  6819. (or (null? (lambda.defs proc))
  6820. (and (notepad.parent notepad)
  6821. (POLICY:LIFT? proc
  6822. (notepad.parent notepad)
  6823. (map (lambda (def) '())
  6824. (lambda.defs proc))))))
  6825. (begin (for-each (lambda (I)
  6826. (notepad-var-add! notepad I))
  6827. (lambda.F proc))
  6828. (if (not (null? (lambda.defs proc)))
  6829. (let ((parent (notepad.parent notepad))
  6830. (defs (lambda.defs proc))
  6831. (R (lambda.R proc)))
  6832. (lambda.defs-set!
  6833. parent
  6834. (append defs (lambda.defs parent)))
  6835. (lambda.defs-set! proc '())
  6836. (lambda.R-set!
  6837. parent
  6838. (append (map (lambda (def)
  6839. (R-lookup R (def.lhs def)))
  6840. defs)
  6841. (lambda.R parent)))))
  6842. (lambda.body proc))
  6843. exp)))
  6844. (if (null? for-effect)
  6845. exp
  6846. (post-simplify-begin (make-begin (append for-effect (list exp)))
  6847. notepad)))))
  6848. (notepad-nonescaping-add! notepad proc)
  6849. (loop1 (lambda.args proc) (call.args exp) '() '()))
  6850. ; Single assignment analysis performs the transformation
  6851. ;
  6852. ; (lambda (... I ...)
  6853. ; (begin D ...)
  6854. ; (quote (... (I <references> ((set! I L)) <calls>) ...) ...)
  6855. ; (begin (set! I L) E1 ...))
  6856. ; -> (lambda (... IGNORED ...)
  6857. ; (begin (define I L) D ...)
  6858. ; (quote (... (I <references> () <calls>) ...) ...)
  6859. ; (begin E1 ...))
  6860. ;
  6861. ; For best results, pass 1 should sort internal definitions and LETRECs so
  6862. ; that procedure definitions/bindings come first.
  6863. ;
  6864. ; This procedure operates by side effect.
  6865. (define (single-assignment-analysis L notepad)
  6866. (let ((formals (lambda.args L))
  6867. (defs (lambda.defs L))
  6868. (R (lambda.R L))
  6869. (body (lambda.body L)))
  6870. (define (finish! exprs escapees)
  6871. (begin.exprs-set! body
  6872. (append (reverse escapees)
  6873. exprs))
  6874. (lambda.body-set! L (post-simplify-begin body '())))
  6875. (if (begin? body)
  6876. (let loop ((exprs (begin.exprs body))
  6877. (escapees '()))
  6878. (let ((first (car exprs)))
  6879. (if (and (assignment? first)
  6880. (not (null? (cdr exprs))))
  6881. (let ((I (assignment.lhs first))
  6882. (rhs (assignment.rhs first)))
  6883. (if (and (lambda? rhs)
  6884. (local? R I)
  6885. (= 1 (length (assignments R I))))
  6886. (if (= (length (calls R I))
  6887. (length (references R I)))
  6888. (begin (notepad-nonescaping-add! notepad rhs)
  6889. (flag-as-ignored I L)
  6890. (lambda.defs-set! L
  6891. (cons (make-definition I rhs)
  6892. (lambda.defs L)))
  6893. (assignments-set! R I '())
  6894. (standardize-known-calls
  6895. rhs
  6896. (R-entry.calls (R-lookup R I)))
  6897. (loop (cdr exprs) escapees))
  6898. (loop (cdr exprs)
  6899. (cons (car exprs) escapees)))
  6900. (finish! exprs escapees)))
  6901. (finish! exprs escapees)))))))
  6902. (define (standardize-known-calls L calls)
  6903. (let ((formals (lambda.args L)))
  6904. (cond ((not (list? formals))
  6905. (let* ((newformals (make-null-terminated formals))
  6906. (n (- (length newformals) 1)))
  6907. (lambda.args-set! L newformals)
  6908. (for-each (lambda (call)
  6909. (if (>= (length (call.args call)) n)
  6910. (call.args-set!
  6911. call
  6912. (append (list-head (call.args call) n)
  6913. (list
  6914. (make-call-to-LIST
  6915. (list-tail (call.args call) n)))))
  6916. (pass2-error p2error:wna call)))
  6917. calls)))
  6918. (else (let ((n (length formals)))
  6919. (for-each (lambda (call)
  6920. (if (not (= (length (call.args call)) n))
  6921. (pass2-error p2error:wna call)))
  6922. calls))))))
  6923. ; Copyright 1991 William D Clinger.
  6924. ;
  6925. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  6926. ;
  6927. ; 13 November 1998
  6928. ;
  6929. ; Second pass of the Twobit compiler, part 2:
  6930. ; single assignment elimination, assignment elimination,
  6931. ; and lambda lifting.
  6932. ;
  6933. ; See part 1 for further documentation.
  6934. ; Single assignment elimination performs the transformation
  6935. ;
  6936. ; (lambda (... I1 ... In ...)
  6937. ; (begin D ...)
  6938. ; (begin (set! I1 E1)
  6939. ; ...
  6940. ; (set! In En)
  6941. ; E ...))
  6942. ; -> (lambda (... IGNORED ... IGNORED ...)
  6943. ; (let* ((I1 E1) ... (In En))
  6944. ; (begin D ...)
  6945. ; (begin E ...)))
  6946. ;
  6947. ; provided for each k:
  6948. ;
  6949. ; 1. Ik does not occur in E1, ..., Ek.
  6950. ; 2. Either E1 through Ek contain no procedure calls
  6951. ; or Ik is not referenced by an escaping lambda expression.
  6952. ; 3. Ik is assigned only once.
  6953. ;
  6954. ; I doubt whether the third condition is really necessary, but
  6955. ; dropping it would involve a more complex calculation of the
  6956. ; revised referencing information.
  6957. ;
  6958. ; A more precise description of the transformation:
  6959. ;
  6960. ; (lambda (... I1 ... In ...)
  6961. ; (begin (define F1 L1) ...)
  6962. ; (quote (... (I1 <references> ((set! I1 E1)) <calls>) ...
  6963. ; (In <references> ((set! In En)) <calls>)
  6964. ; (F1 <references> () <calls>) ...) ...)
  6965. ; (begin (set! I1 E1) ... (set! In En) E ...))
  6966. ; -> (lambda (... IGNORED ... IGNORED ...)
  6967. ; (begin)
  6968. ; (quote (...) ...)
  6969. ; ((lambda (I1)
  6970. ; (begin)
  6971. ; (quote ((I1 <references> () <calls>)) ...)
  6972. ; ...
  6973. ; ((lambda (In)
  6974. ; (begin (define F1 L1) ...)
  6975. ; (quote (... (In <references> () <calls>)
  6976. ; (F1 <references> () <calls>) ...) ...)
  6977. ; (begin E ...))
  6978. ; En)
  6979. ; ...)
  6980. ; E1))
  6981. ;
  6982. ; For best results, pass 1 should sort internal definitions and LETRECs
  6983. ; so that procedure definitions/bindings come first, followed by
  6984. ; definitions/bindings whose right hand side contains no calls,
  6985. ; followed by definitions/bindings of variables that do not escape,
  6986. ; followed by all other definitions/bindings.
  6987. ;
  6988. ; Pass 1 can't tell which variables escape, however. Pass 2 can't tell
  6989. ; which variables escape either until all enclosed lambda expressions
  6990. ; have been simplified and the first transformation above has been
  6991. ; performed. That is why single assignment analysis precedes single
  6992. ; assignment elimination. As implemented here, an assignment that does
  6993. ; not satisfy the conditions above will prevent the transformation from
  6994. ; being applied to any subsequent assignments.
  6995. ;
  6996. ; This procedure operates by side effect.
  6997. (define (single-assignment-elimination L notepad)
  6998. (if (begin? (lambda.body L))
  6999. (let* ((formals (make-null-terminated (lambda.args L)))
  7000. (defined (map def.lhs (lambda.defs L)))
  7001. (escaping (intersection formals
  7002. (notepad-captured-variables notepad)))
  7003. (R (lambda.R L)))
  7004. ; Given:
  7005. ; exprs that remain in the body;
  7006. ; assigns that will be replaced by let* variables;
  7007. ; call-has-occurred?, a boolean;
  7008. ; free variables of the assigns;
  7009. ; Performs the transformation described above.
  7010. (define (loop exprs assigns call-has-occurred? free)
  7011. (cond ((null? (cdr exprs))
  7012. (return exprs assigns))
  7013. ((assignment? (car exprs))
  7014. (let ((I1 (assignment.lhs (car exprs)))
  7015. (E1 (assignment.rhs (car exprs))))
  7016. (if (and (memq I1 formals)
  7017. (= (length (assignments R I1)) 1)
  7018. (not (and call-has-occurred?
  7019. (memq I1 escaping))))
  7020. (let* ((free-in-E1 (free-variables E1))
  7021. (newfree (union free-in-E1 free)))
  7022. (if (or (memq I1 newfree)
  7023. (not
  7024. (empty-set?
  7025. (intersection free-in-E1 defined))))
  7026. (return exprs assigns)
  7027. (loop (cdr exprs)
  7028. (cons (car exprs) assigns)
  7029. (or call-has-occurred?
  7030. (might-return-twice? E1))
  7031. newfree)))
  7032. (return exprs assigns))))
  7033. (else (return exprs assigns))))
  7034. (define (return exprs assigns)
  7035. (if (not (null? assigns))
  7036. (let ((I (assignment.lhs (car assigns)))
  7037. (E (assignment.rhs (car assigns)))
  7038. (defs (lambda.defs L))
  7039. (F (lambda.F L))
  7040. (G (lambda.G L)))
  7041. (flag-as-ignored I L)
  7042. (assignments-set! R I '())
  7043. (let ((L2 (make-lambda (list I)
  7044. defs
  7045. (cons (R-entry R I)
  7046. (map (lambda (def)
  7047. (R-entry R (def.lhs def)))
  7048. defs))
  7049. F
  7050. G
  7051. (lambda.decls L)
  7052. (lambda.doc L)
  7053. (make-begin exprs))))
  7054. (lambda.defs-set! L '())
  7055. (for-each (lambda (entry)
  7056. (lambda.R-set! L (remq entry R)))
  7057. (lambda.R L2))
  7058. (return-loop (cdr assigns) (make-call L2 (list E)))))))
  7059. (define (return-loop assigns body)
  7060. (if (null? assigns)
  7061. (let ((L3 (call.proc body)))
  7062. (lambda.body-set! L body)
  7063. (lambda-lifting L3 L))
  7064. (let* ((I (assignment.lhs (car assigns)))
  7065. (E (assignment.rhs (car assigns)))
  7066. (L3 (call.proc body))
  7067. (F (remq I (lambda.F L3)))
  7068. (G (remq I (lambda.G L3))))
  7069. (flag-as-ignored I L)
  7070. (assignments-set! R I '())
  7071. (let ((L2 (make-lambda (list I)
  7072. '()
  7073. (list (R-entry R I))
  7074. F
  7075. G
  7076. (lambda.decls L)
  7077. (lambda.doc L)
  7078. body)))
  7079. (lambda.R-set! L (remq (R-entry R I) R))
  7080. (lambda-lifting L3 L2)
  7081. (return-loop (cdr assigns) (make-call L2 (list E)))))))
  7082. (loop (begin.exprs (lambda.body L)) '() #f '())))
  7083. L)
  7084. ; Temporary definitions.
  7085. (define (free-variables exp)
  7086. (case (car exp)
  7087. ((quote) '())
  7088. ((lambda) (difference (lambda.F exp)
  7089. (make-null-terminated (lambda.args exp))))
  7090. ((set!) (union (list (assignment.lhs exp))
  7091. (free-variables (assignment.rhs exp))))
  7092. ((if) (union (free-variables (if.test exp))
  7093. (free-variables (if.then exp))
  7094. (free-variables (if.else exp))))
  7095. ((begin) (if (variable? exp)
  7096. (list (variable.name exp))
  7097. (apply union (map free-variables (begin.exprs exp)))))
  7098. (else (apply union (map free-variables exp)))))
  7099. (define (might-return-twice? exp)
  7100. (case (car exp)
  7101. ((quote) #f)
  7102. ((lambda) #f)
  7103. ((set!) (might-return-twice? (assignment.rhs exp)))
  7104. ((if) (or (might-return-twice? (if.test exp))
  7105. (might-return-twice? (if.then exp))
  7106. (might-return-twice? (if.else exp))))
  7107. ((begin) (if (variable? exp)
  7108. #f
  7109. (some? might-return-twice? (begin.exprs exp))))
  7110. (else #t)))
  7111. ; Assignment elimination replaces variables that appear on the left
  7112. ; hand side of an assignment by data structures. This is necessary
  7113. ; to avoid some nasty complications with lambda lifting.
  7114. ;
  7115. ; This procedure operates by side effect.
  7116. (define (assignment-elimination L)
  7117. (let ((R (lambda.R L)))
  7118. ; Given a list of entries, return those for assigned variables.
  7119. (define (loop entries assigned)
  7120. (cond ((null? entries)
  7121. (if (not (null? assigned))
  7122. (eliminate assigned)))
  7123. ((not (null? (R-entry.assignments (car entries))))
  7124. (loop (cdr entries) (cons (car entries) assigned)))
  7125. ((null? (R-entry.references (car entries)))
  7126. (flag-as-ignored (R-entry.name (car entries)) L)
  7127. (loop (cdr entries) assigned))
  7128. (else (loop (cdr entries) assigned))))
  7129. ; Given a list of entries for assigned variables I1 ...,
  7130. ; remove the assignments by replacing the body by a LET of the form
  7131. ; ((LAMBDA (V1 ...) ...) (MAKE-CELL I1) ...), by replacing references
  7132. ; by calls to CELL-REF, and by replacing assignments by calls to
  7133. ; CELL-SET!.
  7134. (define (eliminate assigned)
  7135. (let* ((oldnames (map R-entry.name assigned))
  7136. (newnames (map generate-new-name oldnames)))
  7137. (let ((augmented-entries (map list newnames assigned))
  7138. (renaming-alist (map cons oldnames newnames))
  7139. (defs (lambda.defs L)))
  7140. (for-each cellify! augmented-entries)
  7141. (for-each (lambda (def)
  7142. (do ((free (lambda.F (def.rhs def)) (cdr free)))
  7143. ((null? free))
  7144. (let ((z (assq (car free) renaming-alist)))
  7145. (if z
  7146. (set-car! free (cdr z))))))
  7147. defs)
  7148. (let ((newbody
  7149. (make-call
  7150. (make-lambda (map car augmented-entries)
  7151. defs
  7152. (union (map (lambda (def)
  7153. (R-entry R (def.lhs def)))
  7154. defs)
  7155. (map new-reference-info augmented-entries))
  7156. (union (list name:CELL-REF name:CELL-SET!)
  7157. newnames
  7158. (difference (lambda.F L) oldnames))
  7159. (union (list name:CELL-REF name:CELL-SET!)
  7160. newnames
  7161. (difference (lambda.G L) oldnames))
  7162. (lambda.decls L)
  7163. (lambda.doc L)
  7164. (lambda.body L))
  7165. (map (lambda (name)
  7166. (make-call (make-variable name:MAKE-CELL)
  7167. (list (make-variable name))))
  7168. (map R-entry.name assigned)))))
  7169. (lambda.F-set! L (union (list name:MAKE-CELL name:CELL-REF name:CELL-SET!)
  7170. (difference (lambda.F L)
  7171. (map def.lhs (lambda.defs L)))))
  7172. (lambda.defs-set! L '())
  7173. (for-each update-old-reference-info!
  7174. (map (lambda (arg)
  7175. (car (call.args arg)))
  7176. (call.args newbody)))
  7177. (lambda.body-set! L newbody)
  7178. (lambda-lifting (call.proc newbody) L)))))
  7179. (define (generate-new-name name)
  7180. (string->symbol (string-append cell-prefix (symbol->string name))))
  7181. ; In addition to replacing references and assignments involving the
  7182. ; old variable by calls to CELL-REF and CELL-SET! on the new, CELLIFY!
  7183. ; uses the old entry to collect the referencing information for the
  7184. ; new variable.
  7185. (define (cellify! augmented-entry)
  7186. (let ((newname (car augmented-entry))
  7187. (entry (cadr augmented-entry)))
  7188. (do ((refs (R-entry.references entry)
  7189. (cdr refs)))
  7190. ((null? refs))
  7191. (let* ((reference (car refs))
  7192. (newref (make-variable newname)))
  7193. (set-car! reference (make-variable name:CELL-REF))
  7194. (set-car! (cdr reference) newref)
  7195. (set-car! refs newref)))
  7196. (do ((assigns (R-entry.assignments entry)
  7197. (cdr assigns)))
  7198. ((null? assigns))
  7199. (let* ((assignment (car assigns))
  7200. (newref (make-variable newname)))
  7201. (set-car! assignment (make-variable name:CELL-SET!))
  7202. (set-car! (cdr assignment) newref)
  7203. (R-entry.references-set! entry
  7204. (cons newref
  7205. (R-entry.references entry)))))
  7206. (R-entry.assignments-set! entry '())))
  7207. ; This procedure creates a brand new entry for a new variable, extracting
  7208. ; the references stored in the old entry by CELLIFY!.
  7209. (define (new-reference-info augmented-entry)
  7210. (make-R-entry (car augmented-entry)
  7211. (R-entry.references (cadr augmented-entry))
  7212. '()
  7213. '()))
  7214. ; This procedure updates the old entry to reflect the fact that it is
  7215. ; now referenced once and never assigned.
  7216. (define (update-old-reference-info! ref)
  7217. (references-set! R (variable.name ref) (list ref))
  7218. (assignments-set! R (variable.name ref) '())
  7219. (calls-set! R (variable.name ref) '()))
  7220. (loop R '())))
  7221. ; Lambda lifting raises internal definitions to outer scopes to avoid
  7222. ; having to choose between creating a closure or losing tail recursion.
  7223. ; If L is not #f, then L2 is a lambda expression nested within L.
  7224. ; Any internal definitions that occur within L2 may be lifted to L
  7225. ; by adding extra arguments to the defined procedure and to all calls to it.
  7226. ; Lambda lifting is not a clear win, because the extra arguments could
  7227. ; easily become more expensive than creating a closure and referring
  7228. ; to the non-local arguments through the closure. The heuristics used
  7229. ; to decide whether to lift a group of internal definitions are isolated
  7230. ; within the POLICY:LIFT? procedure.
  7231. ; L2 can be the same as L, so the order of side effects is critical.
  7232. (define (lambda-lifting L2 L)
  7233. ; The call to sort is optional. It gets the added arguments into
  7234. ; the same order they appear in the formals list, which is an
  7235. ; advantage for register targeting.
  7236. (define (lift L2 L args-to-add)
  7237. (let ((formals (make-null-terminated (lambda.args L2))))
  7238. (do ((defs (lambda.defs L2) (cdr defs))
  7239. (args-to-add args-to-add (cdr args-to-add)))
  7240. ((null? defs))
  7241. (let* ((def (car defs))
  7242. (entry (R-lookup (lambda.R L2) (def.lhs def)))
  7243. (calls (R-entry.calls entry))
  7244. (added (twobit-sort (lambda (x y)
  7245. (let ((xx (memq x formals))
  7246. (yy (memq y formals)))
  7247. (if (and xx yy)
  7248. (> (length xx) (length yy))
  7249. #t)))
  7250. (car args-to-add)))
  7251. (L3 (def.rhs def)))
  7252. ; The flow equation guarantees that these added arguments
  7253. ; will occur free by the time this round of lifting is done.
  7254. (lambda.F-set! L3 (union added (lambda.F L3)))
  7255. (lambda.args-set! L3 (append added (lambda.args L3)))
  7256. (for-each (lambda (call)
  7257. (let ((newargs (map make-variable added)))
  7258. ; The referencing information is made obsolete here!
  7259. (call.args-set! call
  7260. (append newargs (call.args call)))))
  7261. calls)
  7262. (lambda.R-set! L2 (remq entry (lambda.R L2)))
  7263. (lambda.R-set! L (cons entry (lambda.R L)))
  7264. ))
  7265. (if (not (eq? L2 L))
  7266. (begin
  7267. (lambda.defs-set! L (append (lambda.defs L2) (lambda.defs L)))
  7268. (lambda.defs-set! L2 '())))))
  7269. (if L
  7270. (if (not (null? (lambda.defs L2)))
  7271. (let ((args-to-add (compute-added-arguments
  7272. (lambda.defs L2)
  7273. (make-null-terminated (lambda.args L2)))))
  7274. (if (POLICY:LIFT? L2 L args-to-add)
  7275. (lift L2 L args-to-add))))))
  7276. ; Given a list of definitions ((define f1 ...) ...) and a set of formals
  7277. ; N over which the definitions may be lifted, returns a list of the
  7278. ; subsets of N that need to be added to each procedure definition
  7279. ; as new arguments.
  7280. ;
  7281. ; Algorithm: Let F_i be the variables that occur free in the body of
  7282. ; the lambda expression associated with f_i. Construct the call graph.
  7283. ; Solve the flow equations
  7284. ;
  7285. ; A_i = (F_i /\ N) \/ (\/ {A_j | A_i calls A_j})
  7286. ;
  7287. ; where /\ is intersection and \/ is union.
  7288. (define (compute-added-arguments defs formals)
  7289. (let ((procs (map def.lhs defs))
  7290. (freevars (map lambda.F (map def.rhs defs))))
  7291. (let ((callgraph (map (lambda (names)
  7292. (map (lambda (name)
  7293. (position name procs))
  7294. (intersection names procs)))
  7295. freevars))
  7296. (added_0 (map (lambda (names)
  7297. (intersection names formals))
  7298. freevars)))
  7299. (vector->list
  7300. (compute-fixedpoint
  7301. (make-vector (length procs) '())
  7302. (list->vector (map (lambda (term0 indexes)
  7303. (lambda (approximations)
  7304. (union term0
  7305. (apply union
  7306. (map (lambda (i)
  7307. (vector-ref approximations i))
  7308. indexes)))))
  7309. added_0
  7310. callgraph))
  7311. set-equal?)))))
  7312. (define (position x l)
  7313. (cond ((eq? x (car l)) 0)
  7314. (else (+ 1 (position x (cdr l))))))
  7315. ; Given a vector of starting approximations,
  7316. ; a vector of functions that compute a next approximation
  7317. ; as a function of the vector of approximations,
  7318. ; and an equality predicate,
  7319. ; returns a vector of fixed points.
  7320. (define (compute-fixedpoint v functions equiv?)
  7321. (define (loop i flag)
  7322. (if (negative? i)
  7323. (if flag
  7324. (loop (- (vector-length v) 1) #f)
  7325. v)
  7326. (let ((next_i ((vector-ref functions i) v)))
  7327. (if (equiv? next_i (vector-ref v i))
  7328. (loop (- i 1) flag)
  7329. (begin (vector-set! v i next_i)
  7330. (loop (- i 1) #t))))))
  7331. (loop (- (vector-length v) 1) #f))
  7332. ; Given a lambda expression L2, its parent lambda expression
  7333. ; L (which may be the same as L2, or #f), and a list of the
  7334. ; lists of arguments that would need to be added to known
  7335. ; local procedures, returns #t iff lambda lifting should be done.
  7336. ;
  7337. ; Here are some heuristics:
  7338. ;
  7339. ; Don't lift if it means adding too many arguments.
  7340. ; Don't lift large groups of definitions.
  7341. ; In questionable cases it is better to lift to an outer
  7342. ; lambda expression that already contains internal
  7343. ; definitions than to one that doesn't.
  7344. ; It is better not to lift if the body contains a lambda
  7345. ; expression that has to be closed anyway.
  7346. (define (POLICY:LIFT? L2 L args-to-add)
  7347. (and (lambda-optimizations)
  7348. (not (lambda? (lambda.body L2)))
  7349. (every? (lambda (addlist)
  7350. (< (length addlist) 6))
  7351. args-to-add)))
  7352. ; Copyright 1991 William D Clinger (for SIMPLIFY-CONDITIONAL)
  7353. ; Copyright 1999 William D Clinger (for everything else)
  7354. ;
  7355. ; Permission to copy this software, in whole or in part, to use this
  7356. ; software for any lawful noncommercial purpose, and to redistribute
  7357. ; this software is granted subject to the restriction that all copies
  7358. ; made of this software must include this copyright notice in full.
  7359. ;
  7360. ; I also request that you send me a copy of any improvements that you
  7361. ; make to this software so that they may be incorporated within it to
  7362. ; the benefit of the Scheme community.
  7363. ;
  7364. ; 11 April 1999.
  7365. ;
  7366. ; Some source transformations on IF expressions:
  7367. ;
  7368. ; (if '#f E1 E2) E2
  7369. ; (if 'K E1 E2) E1 K != #f
  7370. ; (if (if B0 '#f '#f) E1 E2) (begin B0 E2)
  7371. ; (if (if B0 '#f 'K ) E1 E2) (if B0 E2 E1) K != #f
  7372. ; (if (if B0 'K '#f) E1 E2) (if B0 E1 E2) K != #f
  7373. ; (if (if B0 'K1 'K2) E1 E2) (begin B0 E1) K1, K2 != #f
  7374. ; (if (if B0 (if B1 #t #f) B2) E1 E2) (if (if B0 B1 B2) E1 E2)
  7375. ; (if (if B0 B1 (if B2 #t #f)) E1 E2) (if (if B0 B1 B2) E1 E2)
  7376. ; (if (if X X B0 ) E1 E2) (if (if X #t B0) E1 E2) X a variable
  7377. ; (if (if X B0 X ) E1 E2) (if (if X B0 #f) E1 E2) X a variable
  7378. ; (if ((lambda (X) (if ((lambda (X)
  7379. ; (if X X B2)) B0) (if X #t (if B2 #t #f))) B0)
  7380. ; E1 E2) E1 E2)
  7381. ; (if (begin ... B0) E1 E2) (begin ... (if B0 E1 E2))
  7382. ; (if (not E0) E1 E2) (if E0 E2 E1) not is integrable
  7383. ;
  7384. ; FIXME: Three of the transformations above are intended to clean up
  7385. ; the output of the OR macro. It isn't yet clear how well this works.
  7386. (define (simplify-conditional exp notepad)
  7387. (define (coercion-to-boolean? exp)
  7388. (and (conditional? exp)
  7389. (let ((E1 (if.then exp))
  7390. (E2 (if.else exp)))
  7391. (and (constant? E1)
  7392. (eq? #t (constant.value E1))
  7393. (constant? E2)
  7394. (eq? #f (constant.value E2))))))
  7395. (if (not (control-optimization))
  7396. (begin (if.test-set! exp (simplify (if.test exp) notepad))
  7397. (if.then-set! exp (simplify (if.then exp) notepad))
  7398. (if.else-set! exp (simplify (if.else exp) notepad))
  7399. exp)
  7400. (let* ((test (if.test exp)))
  7401. (if (and (call? test)
  7402. (lambda? (call.proc test))
  7403. (let* ((L (call.proc test))
  7404. (body (lambda.body L)))
  7405. (and (conditional? body)
  7406. (let ((R (lambda.R L))
  7407. (B0 (if.test body))
  7408. (B1 (if.then body)))
  7409. (and (variable? B0)
  7410. (variable? B1)
  7411. (let ((x (variable.name B0)))
  7412. (and (eq? x (variable.name B1))
  7413. (local? R x)
  7414. (= 1 (length R))
  7415. (= 1 (length (call.args test))))))))))
  7416. (let* ((L (call.proc test))
  7417. (R (lambda.R L))
  7418. (body (lambda.body L))
  7419. (ref (if.then body))
  7420. (x (variable.name ref))
  7421. (entry (R-entry R x)))
  7422. (if.then-set! body (make-constant #t))
  7423. (if.else-set! body
  7424. (make-conditional (if.else body)
  7425. (make-constant #t)
  7426. (make-constant #f)))
  7427. (R-entry.references-set! entry
  7428. (remq ref
  7429. (R-entry.references entry)))
  7430. (simplify-conditional exp notepad))
  7431. (let loop ((test (simplify (if.test exp) notepad)))
  7432. (if.test-set! exp test)
  7433. (cond ((constant? test)
  7434. (simplify (if (constant.value test)
  7435. (if.then exp)
  7436. (if.else exp))
  7437. notepad))
  7438. ((and (conditional? test)
  7439. (constant? (if.then test))
  7440. (constant? (if.else test)))
  7441. (cond ((and (constant.value (if.then test))
  7442. (constant.value (if.else test)))
  7443. (post-simplify-begin
  7444. (make-begin (list (if.test test)
  7445. (simplify (if.then exp)
  7446. notepad)))
  7447. notepad))
  7448. ((and (not (constant.value (if.then test)))
  7449. (not (constant.value (if.else test))))
  7450. (post-simplify-begin
  7451. (make-begin (list (if.test test)
  7452. (simplify (if.else exp)
  7453. notepad)))
  7454. notepad))
  7455. (else (if (not (constant.value (if.then test)))
  7456. (let ((temp (if.then exp)))
  7457. (if.then-set! exp (if.else exp))
  7458. (if.else-set! exp temp)))
  7459. (if.test-set! exp (if.test test))
  7460. (loop (if.test exp)))))
  7461. ((and (conditional? test)
  7462. (or (coercion-to-boolean? (if.then test))
  7463. (coercion-to-boolean? (if.else test))))
  7464. (if (coercion-to-boolean? (if.then test))
  7465. (if.then-set! test (if.test (if.then test)))
  7466. (if.else-set! test (if.test (if.else test))))
  7467. (loop test))
  7468. ((and (conditional? test)
  7469. (variable? (if.test test))
  7470. (let ((x (variable.name (if.test test))))
  7471. (or (and (variable? (if.then test))
  7472. (eq? x (variable.name (if.then test)))
  7473. 1)
  7474. (and (variable? (if.else test))
  7475. (eq? x (variable.name (if.else test)))
  7476. 2))))
  7477. =>
  7478. (lambda (n)
  7479. (case n
  7480. ((1) (if.then-set! test (make-constant #t)))
  7481. ((2) (if.else-set! test (make-constant #f))))
  7482. (loop test)))
  7483. ((begin? test)
  7484. (let ((exprs (reverse (begin.exprs test))))
  7485. (if.test-set! exp (car exprs))
  7486. (post-simplify-begin
  7487. (make-begin (reverse (cons (loop (car exprs))
  7488. (cdr exprs))))
  7489. notepad)))
  7490. ((and (call? test)
  7491. (variable? (call.proc test))
  7492. (eq? (variable.name (call.proc test)) name:NOT)
  7493. (integrable? name:NOT)
  7494. (integrate-usual-procedures)
  7495. (= (length (call.args test)) 1))
  7496. (let ((temp (if.then exp)))
  7497. (if.then-set! exp (if.else exp))
  7498. (if.else-set! exp temp))
  7499. (loop (car (call.args test))))
  7500. (else
  7501. (simplify-case exp notepad))))))))
  7502. ; Given a conditional expression whose test has been simplified,
  7503. ; simplifies the then and else parts while applying optimizations
  7504. ; for CASE expressions.
  7505. ; Precondition: (control-optimization) is true.
  7506. (define (simplify-case exp notepad)
  7507. (let ((E0 (if.test exp)))
  7508. (if (and (call? E0)
  7509. (variable? (call.proc E0))
  7510. (let ((name (variable.name (call.proc E0))))
  7511. ; FIXME: Should ensure that the name is integrable,
  7512. ; but MEMQ and MEMV probably aren't according to the
  7513. ; INTEGRABLE? predicate.
  7514. (or (eq? name name:EQ?)
  7515. (eq? name name:EQV?)
  7516. (eq? name name:MEMQ)
  7517. (eq? name name:MEMV)))
  7518. (integrate-usual-procedures)
  7519. (= (length (call.args E0)) 2)
  7520. (variable? (car (call.args E0)))
  7521. (constant? (cadr (call.args E0))))
  7522. (simplify-case-clauses (variable.name (car (call.args E0)))
  7523. exp
  7524. notepad)
  7525. (begin (if.then-set! exp (simplify (if.then exp) notepad))
  7526. (if.else-set! exp (simplify (if.else exp) notepad))
  7527. exp))))
  7528. ; Code generation for case expressions.
  7529. ;
  7530. ; A case expression turns into a conditional expression
  7531. ; of the form
  7532. ;
  7533. ; CASE{I} ::= E | (if (PRED I K) E CASE{I})
  7534. ; PRED ::= memv | memq | eqv? | eq?
  7535. ;
  7536. ; The memq and eq? predicates are used when the constant
  7537. ; is a (list of) boolean, fixnum, char, empty list, or symbol.
  7538. ; The constants will almost always be of these types.
  7539. ;
  7540. ; The first step is to remove duplicated constants and to
  7541. ; collect all the case clauses, sorting them into the following
  7542. ; categories based on their simplified list of constants:
  7543. ; constants are fixnums
  7544. ; constants are characters
  7545. ; constants are symbols
  7546. ; constants are of mixed or other type
  7547. ; After duplicated constants have been removed, the predicates
  7548. ; for these clauses can be tested in any order.
  7549. ; Given the name of an arbitrary variable, an expression that
  7550. ; has not yet been simplified or can safely be simplified again,
  7551. ; and a notepad, returns the expression after simplification.
  7552. ; If the expression is equivalent to a case expression that dispatches
  7553. ; on the given variable, then case-optimization will be applied.
  7554. (define (simplify-case-clauses var0 E notepad)
  7555. (define notepad2 (make-notepad (notepad.parent notepad)))
  7556. (define (collect-clauses E fix chr sym other constants)
  7557. (if (not (conditional? E))
  7558. (analyze (simplify E notepad2)
  7559. fix chr sym other constants)
  7560. (let ((test (simplify (if.test E) notepad2))
  7561. (code (simplify (if.then E) notepad2)))
  7562. (if.test-set! E test)
  7563. (if.then-set! E code)
  7564. (if (not (call? test))
  7565. (finish E fix chr sym other constants)
  7566. (let ((proc (call.proc test))
  7567. (args (call.args test)))
  7568. (if (not (and (variable? proc)
  7569. (let ((name (variable.name proc)))
  7570. ; FIXME: See note above.
  7571. (or (eq? name name:EQ?)
  7572. (eq? name name:EQV?)
  7573. (eq? name name:MEMQ)
  7574. (eq? name name:MEMV)))
  7575. (= (length args) 2)
  7576. (variable? (car args))
  7577. (eq? (variable.name (car args)) var0)
  7578. (constant? (cadr args))))
  7579. (finish E fix chr sym other constants)
  7580. (let ((pred (variable.name proc))
  7581. (datum (constant.value (cadr args))))
  7582. ; FIXME
  7583. (if (or (and (or (eq? pred name:MEMV)
  7584. (eq? pred name:MEMQ))
  7585. (not (list? datum)))
  7586. (and (eq? pred name:EQ?)
  7587. (not (eqv-is-ok? datum)))
  7588. (and (eq? pred name:MEMQ)
  7589. (not (every? (lambda (datum)
  7590. (eqv-is-ok? datum))
  7591. datum))))
  7592. (finish E fix chr sym other constants)
  7593. (call-with-values
  7594. (lambda ()
  7595. (remove-duplicates (if (or (eq? pred name:EQV?)
  7596. (eq? pred name:EQ?))
  7597. (list datum)
  7598. datum)
  7599. constants))
  7600. (lambda (data constants)
  7601. (let ((clause (list data code))
  7602. (E2 (if.else E)))
  7603. (cond ((every? smallint? data)
  7604. (collect-clauses E2
  7605. (cons clause fix)
  7606. chr
  7607. sym
  7608. other
  7609. constants))
  7610. ((every? char? data)
  7611. (collect-clauses E2
  7612. fix
  7613. (cons clause chr)
  7614. sym
  7615. other
  7616. constants))
  7617. ((every? symbol? data)
  7618. (collect-clauses E2
  7619. fix
  7620. chr
  7621. (cons clause sym)
  7622. other
  7623. constants))
  7624. (else
  7625. (collect-clauses E2
  7626. fix
  7627. chr
  7628. sym
  7629. (cons clause other)
  7630. constants))))))))))))))
  7631. (define (remove-duplicates data set)
  7632. (let loop ((originals data)
  7633. (data '())
  7634. (set set))
  7635. (if (null? originals)
  7636. (values data set)
  7637. (let ((x (car originals))
  7638. (originals (cdr originals)))
  7639. (if (memv x set)
  7640. (loop originals data set)
  7641. (loop originals (cons x data) (cons x set)))))))
  7642. (define (finish E fix chr sym other constants)
  7643. (if.else-set! E (simplify (if.else E) notepad2))
  7644. (analyze E fix chr sym other constants))
  7645. (define (analyze default fix chr sym other constants)
  7646. (notepad-var-add! notepad2 var0)
  7647. (for-each (lambda (L)
  7648. (notepad-lambda-add! notepad L))
  7649. (notepad.lambdas notepad2))
  7650. (for-each (lambda (L)
  7651. (notepad-nonescaping-add! notepad L))
  7652. (notepad.nonescaping notepad2))
  7653. (for-each (lambda (var)
  7654. (notepad-var-add! notepad var))
  7655. (append (list name:FIXNUM?
  7656. name:CHAR?
  7657. name:SYMBOL?
  7658. name:FX<
  7659. name:FX-
  7660. name:CHAR->INTEGER
  7661. name:VECTOR-REF)
  7662. (notepad.vars notepad2)))
  7663. (analyze-clauses (notepad.vars notepad2)
  7664. var0
  7665. default
  7666. (reverse fix)
  7667. (reverse chr)
  7668. (reverse sym)
  7669. (reverse other)
  7670. constants))
  7671. (collect-clauses E '() '() '() '() '()))
  7672. ; Returns true if EQ? and EQV? behave the same on x.
  7673. (define (eqv-is-ok? x)
  7674. (or (smallint? x)
  7675. (char? x)
  7676. (symbol? x)
  7677. (boolean? x)))
  7678. ; Returns true if EQ? and EQV? behave the same on x.
  7679. (define (eq-is-ok? x)
  7680. (eqv-is-ok? x))
  7681. ; Any case expression that dispatches on a variable var0 and whose
  7682. ; constants are disjoint can be compiled as
  7683. ;
  7684. ; (let ((n (cond ((eq? var0 'K1) ...) ; miscellaneous constants
  7685. ; ...
  7686. ; ((fixnum? var0)
  7687. ; <dispatch-on-fixnum>)
  7688. ; ((char? var0)
  7689. ; <dispatch-on-char>)
  7690. ; ((symbol? var0)
  7691. ; <dispatch-on-symbols>)
  7692. ; (else 0))))
  7693. ; <dispatch-on-case-number>)
  7694. ;
  7695. ; where the <dispatch-on-case-number> uses binary search within
  7696. ; the interval [0, p+1), where p is the number of non-default cases.
  7697. ;
  7698. ; On the SPARC, sequential search is faster if there are fewer than
  7699. ; 8 constants, and sequential search uses less than half the space
  7700. ; if there are fewer than 10 constants. Most target machines should
  7701. ; similar, so I'm hard-wiring this constant.
  7702. ; FIXME: The hardwired constant is annoying.
  7703. (define (analyze-clauses F var0 default fix chr sym other constants)
  7704. (cond ((or (and (null? fix)
  7705. (null? chr))
  7706. (< (length constants) 12))
  7707. (implement-clauses-by-sequential-search var0
  7708. default
  7709. (append fix chr sym other)))
  7710. (else
  7711. (implement-clauses F var0 default fix chr sym other constants))))
  7712. ; Implements the general technique described above.
  7713. (define (implement-clauses F var0 default fix chr sym other constants)
  7714. (let* ((name:n ((make-rename-procedure) 'n))
  7715. ; Referencing information is destroyed by pass 2.
  7716. (entry (make-R-entry name:n '() '() '()))
  7717. (F (union (make-set (list name:n)) F))
  7718. (L (make-lambda
  7719. (list name:n)
  7720. '()
  7721. '() ; entry
  7722. F
  7723. '()
  7724. '()
  7725. #f
  7726. (implement-case-dispatch
  7727. name:n
  7728. (cons default
  7729. (map cadr
  7730. ; The order here must match the order
  7731. ; used by IMPLEMENT-DISPATCH.
  7732. (append other fix chr sym)))))))
  7733. (make-call L
  7734. (list (implement-dispatch 0
  7735. var0
  7736. (map car other)
  7737. (map car fix)
  7738. (map car chr)
  7739. (map car sym))))))
  7740. (define (implement-case-dispatch var0 exprs)
  7741. (implement-intervals var0
  7742. (map (lambda (n code)
  7743. (list n (+ n 1) code))
  7744. (iota (length exprs))
  7745. exprs)))
  7746. ; Given the number of prior clauses,
  7747. ; the variable on which to dispatch,
  7748. ; a list of constant lists for mixed or miscellaneous clauses,
  7749. ; a list of constant lists for the fixnum clauses,
  7750. ; a list of constant lists for the character clauses, and
  7751. ; a list of constant lists for the symbol clauses,
  7752. ; returns code that computes the index of the selected clause.
  7753. ; The mixed/miscellaneous clauses must be tested first because
  7754. ; Twobit's SMALLINT? predicate might not be true of all fixnums
  7755. ; on the target machine, which means that Twobit might classify
  7756. ; some fixnums as miscellaneous.
  7757. (define (implement-dispatch prior var0 other fix chr sym)
  7758. (cond ((not (null? other))
  7759. (implement-dispatch-other
  7760. (implement-dispatch (+ prior (length other))
  7761. var0 fix chr sym '())
  7762. prior var other))
  7763. ((not (null? fix))
  7764. (make-conditional (make-call (make-variable name:FIXNUM?)
  7765. (list (make-variable var0)))
  7766. (implement-dispatch-fixnum prior var0 fix)
  7767. (implement-dispatch (+ prior (length fix))
  7768. var0 '() chr sym other)))
  7769. ((not (null? chr))
  7770. (make-conditional (make-call (make-variable name:CHAR?)
  7771. (list (make-variable var0)))
  7772. (implement-dispatch-char prior var0 chr)
  7773. (implement-dispatch (+ prior (length chr))
  7774. var0 fix '() sym other)))
  7775. ((not (null? sym))
  7776. (make-conditional (make-call (make-variable name:SYMBOL?)
  7777. (list (make-variable var0)))
  7778. (implement-dispatch-symbol prior var0 sym)
  7779. (implement-dispatch (+ prior (length sym))
  7780. var0 fix chr '() other)))
  7781. (else
  7782. (make-constant 0))))
  7783. ; The value of var0 will be known to be a fixnum.
  7784. ; Can use table lookup, binary search, or sequential search.
  7785. ; FIXME: Never uses sequential search, which is best when
  7786. ; there are only a few constants, with gaps between them.
  7787. (define (implement-dispatch-fixnum prior var0 lists)
  7788. (define (calculate-intervals n lists)
  7789. (define (loop n lists intervals)
  7790. (if (null? lists)
  7791. (twobit-sort (lambda (interval1 interval2)
  7792. (< (car interval1) (car interval2)))
  7793. intervals)
  7794. (let ((constants (twobit-sort < (car lists))))
  7795. (loop (+ n 1)
  7796. (cdr lists)
  7797. (append (extract-intervals n constants)
  7798. intervals)))))
  7799. (loop n lists '()))
  7800. (define (extract-intervals n constants)
  7801. (if (null? constants)
  7802. '()
  7803. (let ((k0 (car constants)))
  7804. (do ((constants (cdr constants) (cdr constants))
  7805. (k1 (+ k0 1) (+ k1 1)))
  7806. ((or (null? constants)
  7807. (not (= k1 (car constants))))
  7808. (cons (list k0 k1 (make-constant n))
  7809. (extract-intervals n constants)))))))
  7810. (define (complete-intervals intervals)
  7811. (cond ((null? intervals)
  7812. intervals)
  7813. ((null? (cdr intervals))
  7814. intervals)
  7815. (else
  7816. (let* ((i1 (car intervals))
  7817. (i2 (cadr intervals))
  7818. (end1 (cadr i1))
  7819. (start2 (car i2))
  7820. (intervals (complete-intervals (cdr intervals))))
  7821. (if (= end1 start2)
  7822. (cons i1 intervals)
  7823. (cons i1
  7824. (cons (list end1 start2 (make-constant 0))
  7825. intervals)))))))
  7826. (let* ((intervals (complete-intervals
  7827. (calculate-intervals (+ prior 1) lists)))
  7828. (lo (car (car intervals)))
  7829. (hi (car (car (reverse intervals))))
  7830. (p (length intervals)))
  7831. (make-conditional
  7832. (make-call (make-variable name:FX<)
  7833. (list (make-variable var0)
  7834. (make-constant lo)))
  7835. (make-constant 0)
  7836. (make-conditional
  7837. (make-call (make-variable name:FX<)
  7838. (list (make-variable var0)
  7839. (make-constant (+ hi 1))))
  7840. ; The static cost of table lookup is about hi - lo words.
  7841. ; The static cost of binary search is about 5 SPARC instructions
  7842. ; per interval.
  7843. (if (< (- hi lo) (* 5 p))
  7844. (implement-table-lookup var0 (+ prior 1) lists lo hi)
  7845. (implement-intervals var0 intervals))
  7846. (make-constant 0)))))
  7847. (define (implement-dispatch-char prior var0 lists)
  7848. (let* ((lists (map (lambda (constants)
  7849. (map compat:char->integer constants))
  7850. lists))
  7851. (name:n ((make-rename-procedure) 'n))
  7852. ; Referencing information is destroyed by pass 2.
  7853. ;(entry (make-R-entry name:n '() '() '()))
  7854. (F (list name:n name:EQ? name:FX< name:FX- name:VECTOR-REF))
  7855. (L (make-lambda
  7856. (list name:n)
  7857. '()
  7858. '() ; entry
  7859. F
  7860. '()
  7861. '()
  7862. #f
  7863. (implement-dispatch-fixnum prior name:n lists))))
  7864. (make-call L
  7865. (make-call (make-variable name:CHAR->INTEGER)
  7866. (list (make-variable var0))))))
  7867. (define (implement-dispatch-symbol prior var0 lists)
  7868. (implement-dispatch-other (make-constant 0) prior var0 lists))
  7869. (define (implement-dispatch-other default prior var0 lists)
  7870. (if (null? lists)
  7871. default
  7872. (let* ((constants (car lists))
  7873. (lists (cdr lists))
  7874. (n (+ prior 1)))
  7875. (make-conditional (make-call-to-memv var0 constants)
  7876. (make-constant n)
  7877. (implement-dispatch-other default n var0 lists)))))
  7878. (define (make-call-to-memv var0 constants)
  7879. (cond ((null? constants)
  7880. (make-constant #f))
  7881. ((null? (cdr constants))
  7882. (make-call-to-eqv var0 (car constants)))
  7883. (else
  7884. (make-conditional (make-call-to-eqv var0 (car constants))
  7885. (make-constant #t)
  7886. (make-call-to-memv var0 (cdr constants))))))
  7887. (define (make-call-to-eqv var0 constant)
  7888. (make-call (make-variable
  7889. (if (eq-is-ok? constant)
  7890. name:EQ?
  7891. name:EQV?))
  7892. (list (make-variable var0)
  7893. (make-constant constant))))
  7894. ; Given a variable whose value is known to be a fixnum,
  7895. ; the clause index for the first fixnum clause,
  7896. ; an ordered list of lists of constants for fixnum-only clauses,
  7897. ; and the least and greatest constants in those lists,
  7898. ; returns code for a table lookup.
  7899. (define (implement-table-lookup var0 index lists lo hi)
  7900. (let ((v (make-vector (+ 1 (- hi lo)) 0)))
  7901. (do ((index index (+ index 1))
  7902. (lists lists (cdr lists)))
  7903. ((null? lists))
  7904. (for-each (lambda (k)
  7905. (vector-set! v (- k lo) index))
  7906. (car lists)))
  7907. (make-call (make-variable name:VECTOR-REF)
  7908. (list (make-constant v)
  7909. (make-call (make-variable name:FX-)
  7910. (list (make-variable var0)
  7911. (make-constant lo)))))))
  7912. ; Given a variable whose value is known to lie within the
  7913. ; half-open interval [m0, mk), and an ordered complete
  7914. ; list of intervals of the form
  7915. ;
  7916. ; ((m0 m1 code0)
  7917. ; (m1 m2 code1)
  7918. ; ...
  7919. ; (m{k-1} mk code{k-1})
  7920. ; )
  7921. ;
  7922. ; returns an expression that finds the unique i such that
  7923. ; var0 lies within [mi, m{i+1}), and then executes code{i}.
  7924. (define (implement-intervals var0 intervals)
  7925. (if (null? (cdr intervals))
  7926. (caddr (car intervals))
  7927. (let ((n (quotient (length intervals) 2)))
  7928. (do ((n n (- n 1))
  7929. (intervals1 '() (cons (car intervals2) intervals1))
  7930. (intervals2 intervals (cdr intervals2)))
  7931. ((zero? n)
  7932. (let ((intervals1 (reverse intervals1))
  7933. (m (car (car intervals2))))
  7934. (make-conditional (make-call (make-variable name:FX<)
  7935. (list
  7936. (make-variable var0)
  7937. (make-constant m)))
  7938. (implement-intervals var0 intervals1)
  7939. (implement-intervals var0 intervals2))))))))
  7940. ; The brute force approach.
  7941. ; Given the variable on which the dispatch is being performed, and
  7942. ; actual (simplified) code for the default clause and
  7943. ; for all other clauses,
  7944. ; returns code to perform the dispatch by sequential search.
  7945. (define *memq-threshold* 20)
  7946. (define *memv-threshold* 4)
  7947. (define (implement-clauses-by-sequential-search var0 default clauses)
  7948. (if (null? clauses)
  7949. default
  7950. (let* ((case1 (car clauses))
  7951. (clauses (cdr clauses))
  7952. (constants1 (car case1))
  7953. (code1 (cadr case1)))
  7954. (make-conditional (make-call-to-memv var0 constants1)
  7955. code1
  7956. (implement-clauses-by-sequential-search
  7957. var0 default clauses)))))
  7958. ; Copyright 1999 William D Clinger.
  7959. ;
  7960. ; Permission to copy this software, in whole or in part, to use this
  7961. ; software for any lawful noncommercial purpose, and to redistribute
  7962. ; this software is granted subject to the restriction that all copies
  7963. ; made of this software must include this copyright notice in full.
  7964. ;
  7965. ; I also request that you send me a copy of any improvements that you
  7966. ; make to this software so that they may be incorporated within it to
  7967. ; the benefit of the Scheme community.
  7968. ;
  7969. ; 13 April 1999.
  7970. ;
  7971. ; The tail and non-tail call graphs of known and unknown procedures.
  7972. ;
  7973. ; Given an expression E returned by pass 2 of Twobit,
  7974. ; returns a list of the following form:
  7975. ;
  7976. ; ((#t L () <tailcalls> <nontailcalls> <size> #f)
  7977. ; (<name> L <vars> <tailcalls> <nontailcalls> <size> #f)
  7978. ; ...)
  7979. ;
  7980. ; where
  7981. ;
  7982. ; Each L is a lambda expression that occurs within E
  7983. ; as either an escaping lambda expression or as a known
  7984. ; procedure. If L is a known procedure, then <name> is
  7985. ; its name; otherwise <name> is #f.
  7986. ;
  7987. ; <vars> is a list of the non-global variables within whose
  7988. ; scope L occurs.
  7989. ;
  7990. ; <tailcalls> is a complete list of names of known local procedures
  7991. ; that L calls tail-recursively, disregarding calls from other known
  7992. ; procedures or escaping lambda expressions that occur within L.
  7993. ;
  7994. ; <nontailcalls> is a complete list of names of known local procedures
  7995. ; that L calls non-tail-recursively, disregarding calls from other
  7996. ; known procedures or escaping lambda expressions that occur within L.
  7997. ;
  7998. ; <size> is a measure of the size of L, including known procedures
  7999. ; and escaping lambda expressions that occur within L.
  8000. (define (callgraphnode.name x) (car x))
  8001. (define (callgraphnode.code x) (cadr x))
  8002. (define (callgraphnode.vars x) (caddr x))
  8003. (define (callgraphnode.tailcalls x) (cadddr x))
  8004. (define (callgraphnode.nontailcalls x) (car (cddddr x)))
  8005. (define (callgraphnode.size x) (cadr (cddddr x)))
  8006. (define (callgraphnode.info x) (caddr (cddddr x)))
  8007. (define (callgraphnode.size! x v) (set-car! (cdr (cddddr x)) v) #f)
  8008. (define (callgraphnode.info! x v) (set-car! (cddr (cddddr x)) v) #f)
  8009. (define (callgraph exp)
  8010. ; Returns (union (list x) z).
  8011. (define (adjoin x z)
  8012. (if (memq x z)
  8013. z
  8014. (cons x z)))
  8015. (let ((result '()))
  8016. ; Given a <name> as described above, a lambda expression, a list
  8017. ; of variables that are in scope, and a list of names of known
  8018. ; local procedure that are in scope, computes an entry for L and
  8019. ; entries for any nested known procedures or escaping lambda
  8020. ; expressions, and adds them to the result.
  8021. (define (add-vertex! name L vars known)
  8022. (let ((tailcalls '())
  8023. (nontailcalls '())
  8024. (size 0))
  8025. ; Given an expression, a list of variables that are in scope,
  8026. ; a list of names of known local procedures that are in scope,
  8027. ; and a boolean indicating whether the expression occurs in a
  8028. ; tail context, adds any tail or non-tail calls to known
  8029. ; procedures that occur within the expression to the list
  8030. ; variables declared above.
  8031. (define (graph! exp vars known tail?)
  8032. (set! size (+ size 1))
  8033. (case (car exp)
  8034. ((quote) #f)
  8035. ((lambda) (add-vertex! #f exp vars known)
  8036. (set! size
  8037. (+ size
  8038. (callgraphnode.size (car result)))))
  8039. ((set!) (graph! (assignment.rhs exp) vars known #f))
  8040. ((if) (graph! (if.test exp) vars known #f)
  8041. (graph! (if.then exp) vars known tail?)
  8042. (graph! (if.else exp) vars known tail?))
  8043. ((begin) (if (not (variable? exp))
  8044. (do ((exprs (begin.exprs exp) (cdr exprs)))
  8045. ((null? (cdr exprs))
  8046. (graph! (car exprs) vars known tail?))
  8047. (graph! (car exprs) vars known #f))))
  8048. (else (let ((proc (call.proc exp)))
  8049. (cond ((variable? proc)
  8050. (let ((name (variable.name proc)))
  8051. (if (memq name known)
  8052. (if tail?
  8053. (set! tailcalls
  8054. (adjoin name tailcalls))
  8055. (set! nontailcalls
  8056. (adjoin name nontailcalls))))))
  8057. ((lambda? proc)
  8058. (graph-lambda! proc vars known tail?))
  8059. (else
  8060. (graph! proc vars known #f)))
  8061. (for-each (lambda (exp)
  8062. (graph! exp vars known #f))
  8063. (call.args exp))))))
  8064. (define (graph-lambda! L vars known tail?)
  8065. (let* ((defs (lambda.defs L))
  8066. (newknown (map def.lhs defs))
  8067. (vars (append newknown
  8068. (make-null-terminated
  8069. (lambda.args L))
  8070. vars))
  8071. (known (append newknown known)))
  8072. (for-each (lambda (def)
  8073. (add-vertex! (def.lhs def)
  8074. (def.rhs def)
  8075. vars
  8076. known)
  8077. (set! size
  8078. (+ size
  8079. (callgraphnode.size (car result)))))
  8080. defs)
  8081. (graph! (lambda.body L) vars known tail?)))
  8082. (graph-lambda! L vars known #t)
  8083. (set! result
  8084. (cons (list name L vars tailcalls nontailcalls size #f)
  8085. result))))
  8086. (add-vertex! #t
  8087. (make-lambda '() '() '() '() '() '() '() exp)
  8088. '()
  8089. '())
  8090. result))
  8091. ; Displays the callgraph, for debugging.
  8092. (define (view-callgraph g)
  8093. (for-each (lambda (entry)
  8094. (let ((name (callgraphnode.name entry))
  8095. (exp (callgraphnode.code entry))
  8096. (vars (callgraphnode.vars entry))
  8097. (tail (callgraphnode.tailcalls entry))
  8098. (nt (callgraphnode.nontailcalls entry))
  8099. (size (callgraphnode.size entry)))
  8100. (cond ((symbol? name)
  8101. (write name))
  8102. (name
  8103. (display "TOP LEVEL EXPRESSION"))
  8104. (else
  8105. (display "ESCAPING LAMBDA EXPRESSION")))
  8106. (display ":")
  8107. (newline)
  8108. (display "Size: ")
  8109. (write size)
  8110. (newline)
  8111. ;(newline)
  8112. ;(display "Variables in scope: ")
  8113. ;(write vars)
  8114. ;(newline)
  8115. (display "Tail calls: ")
  8116. (write tail)
  8117. (newline)
  8118. (display "Non-tail calls: ")
  8119. (write nt)
  8120. (newline)
  8121. ;(newline)
  8122. ;(pretty-print (make-readable exp))
  8123. ;(newline)
  8124. ;(newline)
  8125. (newline)))
  8126. g))
  8127. ; Copyright 1999 William D Clinger.
  8128. ;
  8129. ; Permission to copy this software, in whole or in part, to use this
  8130. ; software for any lawful noncommercial purpose, and to redistribute
  8131. ; this software is granted subject to the restriction that all copies
  8132. ; made of this software must include this copyright notice in full.
  8133. ;
  8134. ; I also request that you send me a copy of any improvements that you
  8135. ; make to this software so that they may be incorporated within it to
  8136. ; the benefit of the Scheme community.
  8137. ;
  8138. ; 14 April 1999.
  8139. ;
  8140. ; Inlining of known local procedures.
  8141. ;
  8142. ; First find the known and escaping procedures and compute the call graph.
  8143. ;
  8144. ; If a known local procedure is not called at all, then delete its code.
  8145. ;
  8146. ; If a known local procedure is called exactly once,
  8147. ; then inline its code at the call site and delete the
  8148. ; known local procedure. Change the size of the code
  8149. ; at the call site by adding the size of the inlined code.
  8150. ;
  8151. ; Divide the remaining known and escaping procedures into categories:
  8152. ; 1. makes no calls to known local procedures
  8153. ; 2. known procedures that call known procedures;
  8154. ; within this category, try to sort so that procedures do not
  8155. ; call procedures that come later in the sequence; or sort by
  8156. ; number of calls and/or size
  8157. ; 3. escaping procedures that call known procedures
  8158. ;
  8159. ; Approve each procedure in category 1 for inlining if its code size
  8160. ; is less than some threshold.
  8161. ;
  8162. ; For each procedure in categories 2 and 3, traverse its code, inlining
  8163. ; where it seems like a good idea. The compiler should be more aggressive
  8164. ; about inlining non-tail calls than tail calls because:
  8165. ;
  8166. ; Inlining a non-tail call can eliminate a stack frame
  8167. ; or expose the inlined code to loop optimizations.
  8168. ;
  8169. ; The main reason for inlining a tail call is to enable
  8170. ; intraprocedural optimizations or to unroll a loop.
  8171. ;
  8172. ; After inlining has been performed on a known local procedure,
  8173. ; then approve it for inlining if its size is less than some threshold.
  8174. ;
  8175. ; FIXME:
  8176. ; This strategy avoids infinite unrolling, but it also avoids finite
  8177. ; unrolling of loops.
  8178. ; Parameters to control inlining.
  8179. ; These can be tuned later.
  8180. (define *tail-threshold* 10)
  8181. (define *nontail-threshold* 20)
  8182. (define *multiplier* 300)
  8183. ; Given a callgraph, performs inlining of known local procedures
  8184. ; by side effect. The original expression must then be copied to
  8185. ; reinstate Twobit's invariants.
  8186. ; FIXME: This code doesn't yet do the right thing with known local
  8187. ; procedures that aren't called or are called in exactly one place.
  8188. (define (inline-using-callgraph! g)
  8189. (let ((known (make-hashtable))
  8190. (category2 '())
  8191. (category3 '()))
  8192. (for-each (lambda (node)
  8193. (let ((name (callgraphnode.name node))
  8194. (tcalls (callgraphnode.tailcalls node))
  8195. (ncalls (callgraphnode.nontailcalls node)))
  8196. (if (symbol? name)
  8197. (hashtable-put! known name node))
  8198. (if (and (null? tcalls)
  8199. (null? ncalls))
  8200. (if (< (callgraphnode.size node)
  8201. *nontail-threshold*)
  8202. (callgraphnode.info! node #t))
  8203. (if (symbol? name)
  8204. (set! category2 (cons node category2))
  8205. (set! category3 (cons node category3))))))
  8206. g)
  8207. (set! category2 (twobit-sort (lambda (x y)
  8208. (< (callgraphnode.size x)
  8209. (callgraphnode.size y)))
  8210. category2))
  8211. (for-each (lambda (node)
  8212. (inline-node! node known))
  8213. category2)
  8214. (for-each (lambda (node)
  8215. (inline-node! node known))
  8216. category3)
  8217. ; FIXME:
  8218. ; Inlining destroys the callgraph, so maybe this cleanup is useless.
  8219. (hashtable-for-each (lambda (name node) (callgraphnode.info! node #f))
  8220. known)))
  8221. ; Given a node of the callgraph and a hash table of nodes for
  8222. ; known local procedures, performs inlining by side effect.
  8223. (define (inline-node! node known)
  8224. (let* ((debugging? #f)
  8225. (name (callgraphnode.name node))
  8226. (exp (callgraphnode.code node))
  8227. (size0 (callgraphnode.size node))
  8228. (budget (quotient (* (- *multiplier* 100) size0) 100))
  8229. (tail-threshold *tail-threshold*)
  8230. (nontail-threshold *nontail-threshold*))
  8231. ; Given an expression,
  8232. ; a boolean indicating whether the expression is in a tail context,
  8233. ; a list of procedures that should not be inlined,
  8234. ; and a size budget,
  8235. ; performs inlining by side effect and returns the unused budget.
  8236. (define (inline exp tail? budget)
  8237. (if (positive? budget)
  8238. (case (car exp)
  8239. ((quote lambda)
  8240. budget)
  8241. ((set!)
  8242. (inline (assignment.rhs exp) #f budget))
  8243. ((if)
  8244. (let* ((budget (inline (if.test exp) #f budget))
  8245. (budget (inline (if.then exp) tail? budget))
  8246. (budget (inline (if.else exp) tail? budget)))
  8247. budget))
  8248. ((begin)
  8249. (if (variable? exp)
  8250. budget
  8251. (do ((exprs (begin.exprs exp) (cdr exprs))
  8252. (budget budget
  8253. (inline (car exprs) #f budget)))
  8254. ((null? (cdr exprs))
  8255. (inline (car exprs) tail? budget)))))
  8256. (else
  8257. (let ((budget (do ((exprs (call.args exp) (cdr exprs))
  8258. (budget budget
  8259. (inline (car exprs) #f budget)))
  8260. ((null? exprs)
  8261. budget))))
  8262. (let ((proc (call.proc exp)))
  8263. (cond ((variable? proc)
  8264. (let* ((procname (variable.name proc))
  8265. (procnode (hashtable-get known procname)))
  8266. (if procnode
  8267. (let ((size (callgraphnode.size procnode))
  8268. (info (callgraphnode.info procnode)))
  8269. (if (and info
  8270. (<= size budget)
  8271. (<= size
  8272. (if tail?
  8273. tail-threshold
  8274. nontail-threshold)))
  8275. (begin
  8276. (if debugging?
  8277. (begin
  8278. (display " Inlining ")
  8279. (write (variable.name proc))
  8280. (newline)))
  8281. (call.proc-set!
  8282. exp
  8283. (copy-exp
  8284. (callgraphnode.code procnode)))
  8285. (callgraphnode.size!
  8286. node
  8287. (+ (callgraphnode.size node) size))
  8288. (- budget size))
  8289. (begin
  8290. (if (and #f debugging?)
  8291. (begin
  8292. (display " Declining to inline ")
  8293. (write (variable.name proc))
  8294. (newline)))
  8295. budget)))
  8296. budget)))
  8297. ((lambda? proc)
  8298. (inline (lambda.body proc) tail? budget))
  8299. (else
  8300. (inline proc #f budget)))))))
  8301. -1))
  8302. (if (and #f debugging?)
  8303. (begin
  8304. (display "Processing ")
  8305. (write name)
  8306. (newline)))
  8307. (let ((budget (inline (if (lambda? exp)
  8308. (lambda.body exp)
  8309. exp)
  8310. #t
  8311. budget)))
  8312. (if (and (negative? budget)
  8313. debugging?)
  8314. ; This shouldn't happen very often.
  8315. (begin (display "Ran out of inlining budget for ")
  8316. (write (callgraphnode.name node))
  8317. (newline)))
  8318. (if (<= (callgraphnode.size node) nontail-threshold)
  8319. (callgraphnode.info! node #t))
  8320. #f)))
  8321. ; For testing.
  8322. (define (test-inlining test0)
  8323. (begin (define exp0 (begin (display "Compiling...")
  8324. (newline)
  8325. (pass2 (pass1 test0))))
  8326. (define g0 (begin (display "Computing call graph...")
  8327. (newline)
  8328. (callgraph exp0))))
  8329. (display "Inlining...")
  8330. (newline)
  8331. (inline-using-callgraph! g0)
  8332. (pretty-print (make-readable (copy-exp exp0))))
  8333. ; Copyright 1999 William D Clinger.
  8334. ;
  8335. ; Permission to copy this software, in whole or in part, to use this
  8336. ; software for any lawful noncommercial purpose, and to redistribute
  8337. ; this software is granted subject to the restriction that all copies
  8338. ; made of this software must include this copyright notice in full.
  8339. ;
  8340. ; I also request that you send me a copy of any improvements that you
  8341. ; make to this software so that they may be incorporated within it to
  8342. ; the benefit of the Scheme community.
  8343. ;
  8344. ; 14 April 1999.
  8345. ;
  8346. ; Interprocedural constant propagation and folding.
  8347. ;
  8348. ; Constant propagation must converge before constant folding can be
  8349. ; performed. Constant folding creates more constants that can be
  8350. ; propagated, so these two optimizations must be iterated, but it
  8351. ; is safe to stop at any time.
  8352. ;
  8353. ; Abstract interpretation for constant folding.
  8354. ;
  8355. ; The abstract values are
  8356. ; bottom (represented here by #f)
  8357. ; constants (represented by quoted literals)
  8358. ; top (represented here by #t)
  8359. ;
  8360. ; Let [[ E ]] be the abstract interpretation of E over that domain
  8361. ; of abstract values, with respect to some arbitrary set of abstract
  8362. ; values for local variables.
  8363. ;
  8364. ; If a is a global variable or a formal parameter of an escaping
  8365. ; lambda expression, then [[ a ]] = #t.
  8366. ;
  8367. ; If x is the ith formal parameter of a known local procedure f,
  8368. ; then [[ x ]] = \join_{(f E1 ... En)} [[ Ei ]].
  8369. ;
  8370. ; [[ K ]] = K
  8371. ; [[ L ]] = #t
  8372. ; [[ (begin E1 ... En) ]] = [[ En ]]
  8373. ; [[ (set! I E) ]] = #f
  8374. ;
  8375. ; If [[ E0 ]] = #t, then [[ (if E0 E1 E2) ]] = [[ E1 ]] \join [[ E2 ]]
  8376. ; else if [[ E0 ]] = K, then [[ (if E0 E1 E2) ]] = [[ E1 ]]
  8377. ; or [[ (if E0 E1 E2) ]] = [[ E2 ]]
  8378. ; depending upon K
  8379. ; else [[ (if E0 E1 E2) ]] = #f
  8380. ;
  8381. ; If f is a known local procedure with body E,
  8382. ; then [[ (f E1 ... En) ]] = [[ E ]]
  8383. ;
  8384. ; If g is a foldable integrable procedure, then:
  8385. ; if there is some i for which [[ Ei ]] = #t,
  8386. ; then [[ (g E1 ... En) ]] = #t
  8387. ; else if [[ E1 ]] = K1, ..., [[ En ]] = Kn,
  8388. ; then [[ (g E1 ... En) ]] = (g K1 ... Kn)
  8389. ; else [[ (g E1 ... En) ]] = #f
  8390. ;
  8391. ; Symbolic representations of abstract values.
  8392. ; (Can be thought of as mappings from abstract environments to
  8393. ; abstract values.)
  8394. ;
  8395. ; <symbolic> ::= #t | ( <expressions> )
  8396. ; <expressions> ::= <empty> | <expression> <expressions>
  8397. ; Parameter to limit constant propagation and folding.
  8398. ; This parameter can be tuned later.
  8399. (define *constant-propagation-limit* 5)
  8400. ; Given an expression as output by pass 2, performs constant
  8401. ; propagation and folding.
  8402. (define (constant-propagation exp)
  8403. (define (constant-propagation exp i)
  8404. (if (< i *constant-propagation-limit*)
  8405. (begin
  8406. ;(display "Performing constant propagation and folding...")
  8407. ;(newline)
  8408. (let* ((g (callgraph exp))
  8409. (L (callgraphnode.code (car g)))
  8410. (variables (constant-propagation-using-callgraph g))
  8411. (changed? (constant-folding! L variables)))
  8412. (if changed?
  8413. (constant-propagation (lambda.body L) (+ i 1))
  8414. (lambda.body L))))))
  8415. (constant-propagation exp 0))
  8416. ; Given a callgraph, returns a hashtable of abstract values for
  8417. ; all local variables.
  8418. (define (constant-propagation-using-callgraph g)
  8419. (let ((debugging? #f)
  8420. (folding? (integrate-usual-procedures))
  8421. (known (make-hashtable))
  8422. (variables (make-hashtable))
  8423. (counter 0))
  8424. ; Computes joins of abstract values.
  8425. (define (join x y)
  8426. (cond ((boolean? x)
  8427. (if x #t y))
  8428. ((boolean? y)
  8429. (join y x))
  8430. ((equal? x y)
  8431. x)
  8432. (else #t)))
  8433. ; Given a <symbolic> and a vector of abstract values,
  8434. ; evaluates the <symbolic> and returns its abstract value.
  8435. (define (aeval rep env)
  8436. (cond ((eq? rep #t)
  8437. #t)
  8438. ((null? rep)
  8439. #f)
  8440. ((null? (cdr rep))
  8441. (aeval1 (car rep) env))
  8442. (else
  8443. (join (aeval1 (car rep) env)
  8444. (aeval (cdr rep) env)))))
  8445. (define (aeval1 exp env)
  8446. (case (car exp)
  8447. ((quote)
  8448. exp)
  8449. ((lambda)
  8450. #t)
  8451. ((set!)
  8452. #f)
  8453. ((begin)
  8454. (if (variable? exp)
  8455. (let* ((name (variable.name exp))
  8456. (i (hashtable-get variables name)))
  8457. (if i
  8458. (vector-ref env i)
  8459. #t))
  8460. (aeval1-error)))
  8461. ((if)
  8462. (let* ((val0 (aeval1 (if.test exp) env))
  8463. (val1 (aeval1 (if.then exp) env))
  8464. (val2 (aeval1 (if.else exp) env)))
  8465. (cond ((eq? val0 #t)
  8466. (join val1 val2))
  8467. ((pair? val0)
  8468. (if (constant.value val0)
  8469. val1
  8470. val2))
  8471. (else
  8472. #f))))
  8473. (else
  8474. (do ((exprs (reverse (call.args exp)) (cdr exprs))
  8475. (vals '() (cons (aeval1 (car exprs) env) vals)))
  8476. ((null? exprs)
  8477. (let ((proc (call.proc exp)))
  8478. (cond ((variable? proc)
  8479. (let* ((procname (variable.name proc))
  8480. (procnode (hashtable-get known procname))
  8481. (entry (if folding?
  8482. (constant-folding-entry procname)
  8483. #f)))
  8484. (cond (procnode
  8485. (vector-ref env
  8486. (hashtable-get variables
  8487. procname)))
  8488. (entry
  8489. ; FIXME: No constant folding
  8490. #t)
  8491. (else (aeval1-error)))))
  8492. (else
  8493. (aeval1-error)))))))))
  8494. (define (aeval1-error)
  8495. (error "Compiler bug: constant propagation (aeval1)"))
  8496. ; Combines two <symbolic>s.
  8497. (define (combine-symbolic rep1 rep2)
  8498. (cond ((eq? rep1 #t) #t)
  8499. ((eq? rep2 #t) #t)
  8500. (else
  8501. (append rep1 rep2))))
  8502. ; Given an expression, returns a <symbolic> that represents
  8503. ; a list of expressions whose abstract values can be joined
  8504. ; to obtain the abstract value of the given expression.
  8505. ; As a side effect, enters local variables into variables.
  8506. (define (collect! exp)
  8507. (case (car exp)
  8508. ((quote)
  8509. (list exp))
  8510. ((lambda)
  8511. #t)
  8512. ((set!)
  8513. (collect! (assignment.rhs exp))
  8514. '())
  8515. ((begin)
  8516. (if (variable? exp)
  8517. (list exp)
  8518. (do ((exprs (begin.exprs exp) (cdr exprs)))
  8519. ((null? (cdr exprs))
  8520. (collect! (car exprs)))
  8521. (collect! (car exprs)))))
  8522. ((if)
  8523. (collect! (if.test exp))
  8524. (collect! (if.then exp))
  8525. (collect! (if.else exp))
  8526. #t)
  8527. (else
  8528. (do ((exprs (reverse (call.args exp)) (cdr exprs))
  8529. (reps '() (cons (collect! (car exprs)) reps)))
  8530. ((null? exprs)
  8531. (let ((proc (call.proc exp)))
  8532. (define (put-args! args reps)
  8533. (cond ((pair? args)
  8534. (let ((v (car args))
  8535. (rep (car reps)))
  8536. (hashtable-put! variables v rep)
  8537. (put-args! (cdr args) (cdr reps))))
  8538. ((symbol? args)
  8539. (hashtable-put! variables args #t))
  8540. (else #f)))
  8541. (cond ((variable? proc)
  8542. (let* ((procname (variable.name proc))
  8543. (procnode (hashtable-get known procname))
  8544. (entry (if folding?
  8545. (constant-folding-entry procname)
  8546. #f)))
  8547. (cond (procnode
  8548. (for-each (lambda (v rep)
  8549. (hashtable-put!
  8550. variables
  8551. v
  8552. (combine-symbolic
  8553. rep (hashtable-get variables v))))
  8554. (lambda.args
  8555. (callgraphnode.code procnode))
  8556. reps)
  8557. (list (make-variable procname)))
  8558. (entry
  8559. ; FIXME: No constant folding
  8560. #t)
  8561. (else #t))))
  8562. ((lambda? proc)
  8563. (put-args! (lambda.args proc) reps)
  8564. (collect! (lambda.body proc)))
  8565. (else
  8566. (collect! proc)
  8567. #t))))))))
  8568. (for-each (lambda (node)
  8569. (let* ((name (callgraphnode.name node))
  8570. (code (callgraphnode.code node))
  8571. (known? (symbol? name))
  8572. (rep (if known? '() #t)))
  8573. (if known?
  8574. (hashtable-put! known name node))
  8575. (if (lambda? code)
  8576. (for-each (lambda (var)
  8577. (hashtable-put! variables var rep))
  8578. (make-null-terminated (lambda.args code))))))
  8579. g)
  8580. (for-each (lambda (node)
  8581. (let ((name (callgraphnode.name node))
  8582. (code (callgraphnode.code node)))
  8583. (cond ((symbol? name)
  8584. (hashtable-put! variables
  8585. name
  8586. (collect! (lambda.body code))))
  8587. (else
  8588. (collect! (lambda.body code))))))
  8589. g)
  8590. (if (and #f debugging?)
  8591. (begin
  8592. (hashtable-for-each (lambda (v rep)
  8593. (write v)
  8594. (display ": ")
  8595. (write rep)
  8596. (newline))
  8597. variables)
  8598. (display "----------------------------------------")
  8599. (newline)))
  8600. ;(trace aeval aeval1)
  8601. (let* ((n (hashtable-size variables))
  8602. (vars (hashtable-map (lambda (v rep) v) variables))
  8603. (reps (map (lambda (v) (hashtable-get variables v)) vars))
  8604. (init (make-vector n #f))
  8605. (next (make-vector n)))
  8606. (do ((i 0 (+ i 1))
  8607. (vars vars (cdr vars))
  8608. (reps reps (cdr reps)))
  8609. ((= i n))
  8610. (hashtable-put! variables (car vars) i)
  8611. (vector-set! next
  8612. i
  8613. (let ((rep (car reps)))
  8614. (lambda (env)
  8615. (aeval rep env)))))
  8616. (compute-fixedpoint init next equal?)
  8617. (for-each (lambda (v)
  8618. (let* ((i (hashtable-get variables v))
  8619. (aval (vector-ref init i)))
  8620. (hashtable-put! variables v aval)
  8621. (if (and debugging?
  8622. (not (eq? aval #t)))
  8623. (begin (write v)
  8624. (display ": ")
  8625. (write aval)
  8626. (newline)))))
  8627. vars)
  8628. variables)))
  8629. ; Given a lambda expression, performs constant propagation, folding,
  8630. ; and simplifications by side effect, using the abstract values in the
  8631. ; hash table of variables.
  8632. ; Returns #t if any new constants were created by constant folding,
  8633. ; otherwise returns #f.
  8634. (define (constant-folding! L variables)
  8635. (let ((debugging? #f)
  8636. (msg1 " Propagating constant value for ")
  8637. (msg2 " Folding: ")
  8638. (msg3 " ==> ")
  8639. (folding? (integrate-usual-procedures))
  8640. (changed? #f))
  8641. ; Given a known lambda expression L, its original formal parameters,
  8642. ; and a list of all calls to L, deletes arguments that are now
  8643. ; ignored because of constant propagation.
  8644. (define (delete-ignored-args! L formals0 calls)
  8645. (let ((formals1 (lambda.args L)))
  8646. (for-each (lambda (call)
  8647. (do ((formals0 formals0 (cdr formals0))
  8648. (formals1 formals1 (cdr formals1))
  8649. (args (call.args call)
  8650. (cdr args))
  8651. (newargs '()
  8652. (if (and (eq? (car formals1) name:IGNORED)
  8653. (pair?
  8654. (hashtable-get variables
  8655. (car formals0))))
  8656. newargs
  8657. (cons (car args) newargs))))
  8658. ((null? formals0)
  8659. (call.args-set! call (reverse newargs)))))
  8660. calls)
  8661. (do ((formals0 formals0 (cdr formals0))
  8662. (formals1 formals1 (cdr formals1))
  8663. (formals2 '()
  8664. (if (and (not (eq? (car formals0)
  8665. (car formals1)))
  8666. (eq? (car formals1) name:IGNORED)
  8667. (pair?
  8668. (hashtable-get variables
  8669. (car formals0))))
  8670. formals2
  8671. (cons (car formals1) formals2))))
  8672. ((null? formals0)
  8673. (lambda.args-set! L (reverse formals2))))))
  8674. (define (fold! exp)
  8675. (case (car exp)
  8676. ((quote) exp)
  8677. ((lambda)
  8678. (let ((Rinfo (lambda.R exp))
  8679. (known (map def.lhs (lambda.defs exp))))
  8680. (for-each (lambda (entry)
  8681. (let* ((v (R-entry.name entry))
  8682. (aval (hashtable-fetch variables v #t)))
  8683. (if (and (pair? aval)
  8684. (not (memq v known)))
  8685. (let ((x (constant.value aval)))
  8686. (if (or (boolean? x)
  8687. (null? x)
  8688. (symbol? x)
  8689. (number? x)
  8690. (char? x)
  8691. (and (vector? x)
  8692. (zero? (vector-length x))))
  8693. (let ((refs (R-entry.references entry)))
  8694. (for-each (lambda (ref)
  8695. (variable-set! ref aval))
  8696. refs)
  8697. ; Do not try to use Rinfo in place of
  8698. ; (lambda.R exp) below!
  8699. (lambda.R-set!
  8700. exp
  8701. (remq entry (lambda.R exp)))
  8702. (flag-as-ignored v exp)
  8703. (if debugging?
  8704. (begin (display msg1)
  8705. (write v)
  8706. (display ": ")
  8707. (write aval)
  8708. (newline)))))))))
  8709. Rinfo)
  8710. (for-each (lambda (def)
  8711. (let* ((name (def.lhs def))
  8712. (rhs (def.rhs def))
  8713. (entry (R-lookup Rinfo name))
  8714. (calls (R-entry.calls entry)))
  8715. (if (null? calls)
  8716. (begin (lambda.defs-set!
  8717. exp
  8718. (remq def (lambda.defs exp)))
  8719. ; Do not try to use Rinfo in place of
  8720. ; (lambda.R exp) below!
  8721. (lambda.R-set!
  8722. exp
  8723. (remq entry (lambda.R exp))))
  8724. (let* ((formals0 (append (lambda.args rhs) '()))
  8725. (L (fold! rhs))
  8726. (formals1 (lambda.args L)))
  8727. (if (not (equal? formals0 formals1))
  8728. (delete-ignored-args! L formals0 calls))))))
  8729. (lambda.defs exp))
  8730. (lambda.body-set!
  8731. exp
  8732. (fold! (lambda.body exp)))
  8733. exp))
  8734. ((set!)
  8735. (assignment.rhs-set! exp (fold! (assignment.rhs exp)))
  8736. exp)
  8737. ((begin)
  8738. (if (variable? exp)
  8739. exp
  8740. (post-simplify-begin (make-begin (map fold! (begin.exprs exp)))
  8741. (make-notepad #f))))
  8742. ((if)
  8743. (let ((exp0 (fold! (if.test exp)))
  8744. (exp1 (fold! (if.then exp)))
  8745. (exp2 (fold! (if.else exp))))
  8746. (if (constant? exp0)
  8747. (let ((newexp (if (constant.value exp0)
  8748. exp1
  8749. exp2)))
  8750. (if debugging?
  8751. (begin (display msg2)
  8752. (write (make-readable exp))
  8753. (display msg3)
  8754. (write (make-readable newexp))
  8755. (newline)))
  8756. (set! changed? #t)
  8757. newexp)
  8758. (make-conditional exp0 exp1 exp2))))
  8759. (else
  8760. (let ((args (map fold! (call.args exp)))
  8761. (proc (fold! (call.proc exp))))
  8762. (cond ((and folding?
  8763. (variable? proc)
  8764. (every? constant? args)
  8765. (let ((entry
  8766. (constant-folding-entry (variable.name proc))))
  8767. (and entry
  8768. (let ((preds
  8769. (constant-folding-predicates entry)))
  8770. (and (= (length args) (length preds))
  8771. (every?
  8772. (lambda (x) x)
  8773. (map (lambda (f v) (f v))
  8774. (constant-folding-predicates entry)
  8775. (map constant.value args))))))))
  8776. (set! changed? #t)
  8777. (let ((result
  8778. (make-constant
  8779. (apply (constant-folding-folder
  8780. (constant-folding-entry
  8781. (variable.name proc)))
  8782. (map constant.value args)))))
  8783. (if debugging?
  8784. (begin (display msg2)
  8785. (write (make-readable (make-call proc args)))
  8786. (display msg3)
  8787. (write result)
  8788. (newline)))
  8789. result))
  8790. ((and (lambda? proc)
  8791. (list? (lambda.args proc)))
  8792. ; FIXME: Folding should be done even if there is
  8793. ; a rest argument.
  8794. (let loop ((formals (reverse (lambda.args proc)))
  8795. (actuals (reverse args))
  8796. (processed-formals '())
  8797. (processed-actuals '())
  8798. (for-effect '()))
  8799. (cond ((null? formals)
  8800. (lambda.args-set! proc processed-formals)
  8801. (call.args-set! exp processed-actuals)
  8802. (let ((call (if (and (null? processed-formals)
  8803. (null? (lambda.defs proc)))
  8804. (lambda.body proc)
  8805. exp)))
  8806. (if (null? for-effect)
  8807. call
  8808. (post-simplify-begin
  8809. (make-begin
  8810. (reverse (cons call for-effect)))
  8811. (make-notepad #f)))))
  8812. ((ignored? (car formals))
  8813. (loop (cdr formals)
  8814. (cdr actuals)
  8815. processed-formals
  8816. processed-actuals
  8817. (cons (car actuals) for-effect)))
  8818. (else
  8819. (loop (cdr formals)
  8820. (cdr actuals)
  8821. (cons (car formals) processed-formals)
  8822. (cons (car actuals) processed-actuals)
  8823. for-effect)))))
  8824. (else
  8825. (call.proc-set! exp proc)
  8826. (call.args-set! exp args)
  8827. exp))))))
  8828. (fold! L)
  8829. changed?))
  8830. ; Copyright 1998 William D Clinger.
  8831. ;
  8832. ; Permission to copy this software, in whole or in part, to use this
  8833. ; software for any lawful noncommercial purpose, and to redistribute
  8834. ; this software is granted subject to the restriction that all copies
  8835. ; made of this software must include this copyright notice in full.
  8836. ;
  8837. ; I also request that you send me a copy of any improvements that you
  8838. ; make to this software so that they may be incorporated within it to
  8839. ; the benefit of the Scheme community.
  8840. ;
  8841. ; 7 June 1999.
  8842. ;
  8843. ; Conversion to A-normal form, with heuristics for
  8844. ; choosing a good order of evaluation.
  8845. ;
  8846. ; This pass operates as a source-to-source transformation on
  8847. ; expressions written in the subset of Scheme described by the
  8848. ; following grammar, where the input and output expressions
  8849. ; satisfy certain additional invariants described below.
  8850. ;
  8851. ; "X ..." means zero or more occurrences of X.
  8852. ;
  8853. ; L --> (lambda (I_1 ...)
  8854. ; (begin D ...)
  8855. ; (quote (R F G <decls> <doc>)
  8856. ; E)
  8857. ; | (lambda (I_1 ... . I_rest)
  8858. ; (begin D ...)
  8859. ; (quote (R F G <decls> <doc>))
  8860. ; E)
  8861. ; D --> (define I L)
  8862. ; E --> (quote K) ; constants
  8863. ; | (begin I) ; variable references
  8864. ; | L ; lambda expressions
  8865. ; | (E0 E1 ...) ; calls
  8866. ; | (set! I E) ; assignments
  8867. ; | (if E0 E1 E2) ; conditionals
  8868. ; | (begin E0 E1 E2 ...) ; sequential expressions
  8869. ; I --> <identifier>
  8870. ;
  8871. ; R --> ((I <references> <assignments> <calls>) ...)
  8872. ; F --> (I ...)
  8873. ; G --> (I ...)
  8874. ;
  8875. ; Invariants that hold for the input only:
  8876. ; * There are no assignments except to global variables.
  8877. ; * If I is declared by an internal definition, then the right hand
  8878. ; side of the internal definition is a lambda expression and I
  8879. ; is referenced only in the procedure position of a call.
  8880. ; * For each lambda expression, the associated F is a list of all
  8881. ; the identifiers that occur free in the body of that lambda
  8882. ; expression, and possibly a few extra identifiers that were
  8883. ; once free but have been removed by optimization.
  8884. ; * For each lambda expression, the associated G is a subset of F
  8885. ; that contains every identifier that occurs free within some
  8886. ; inner lambda expression that escapes, and possibly a few that
  8887. ; don't. (Assignment-elimination does not calculate G exactly.)
  8888. ; * Variables named IGNORED are neither referenced nor assigned.
  8889. ;
  8890. ; Invariants that hold for the output only:
  8891. ; * There are no assignments except to global variables.
  8892. ; * If I is declared by an internal definition, then the right hand
  8893. ; side of the internal definition is a lambda expression and I
  8894. ; is referenced only in the procedure position of a call.
  8895. ; * R, F, and G are garbage.
  8896. ; * There are no sequential expressions.
  8897. ; * The output is an expression E with syntax
  8898. ;
  8899. ; E --> A
  8900. ; | (L)
  8901. ; | (L A)
  8902. ;
  8903. ; A --> W
  8904. ; | L
  8905. ; | (W_0 W_1 ...)
  8906. ; | (set! I W)
  8907. ; | (if W E1 E2)
  8908. ;
  8909. ; W --> (quote K)
  8910. ; | (begin I)
  8911. ;
  8912. ; In other words:
  8913. ; An expression is a LET* such that the rhs of every binding is
  8914. ; a conditional with the test already evaluated, or
  8915. ; an expression that can be evaluated in one step
  8916. ; (treating function calls as a single step)
  8917. ;
  8918. ; A-normal form corresponds to the control flow graph for a lambda
  8919. ; expression.
  8920. ; Algorithm: repeated use of these rules:
  8921. ;
  8922. ; (E0 E1 ...) ((lambda (T0 T1 ...) (T0 T1 ...))
  8923. ; E0 E1 ...)
  8924. ; (set! I E) ((lambda (T) (set! I T)) E)
  8925. ; (if E0 E1 E2) ((lambda (T) (if T E1 E2)) E0)
  8926. ; (begin E0 E1 E2 ...) ((lambda (T) (begin E1 E2 ...)) E0)
  8927. ;
  8928. ; ((lambda (I1 I2 I3 ...) E) ((lambda (I1)
  8929. ; E1 E2 E3) ((lambda (I2 I3 ...) E)
  8930. ; E2 E3))
  8931. ; E1)
  8932. ;
  8933. ; ((lambda (I2) E) ((lambda (I1)
  8934. ; ((lambda (I1) E2) ((lambda (I2) E)
  8935. ; E1)) E2)
  8936. ; E1)
  8937. ;
  8938. ; In other words:
  8939. ; Introduce a temporary name for every expression except:
  8940. ; tail expressions
  8941. ; the alternatives of a non-tail conditional
  8942. ; Convert every LET into a LET*.
  8943. ; Get rid of LET* on the right hand side of a binding.
  8944. ; Given an expression E in the representation output by pass 2,
  8945. ; returns an A-normal form for E in that representation.
  8946. ; Except for quoted values, the A-normal form does not share
  8947. ; mutable structure with the original expression E.
  8948. ;
  8949. ; KNOWN BUG:
  8950. ;
  8951. ; If you call A-normal on a form that has already been converted
  8952. ; to A-normal form, then the same temporaries will be generated
  8953. ; twice. An optional argument lets you specify a different prefix
  8954. ; for temporaries the second time around. Example:
  8955. ;
  8956. ; (A-normal-form (A-normal-form E ".T")
  8957. ; ".U")
  8958. ; This is the declaration that is used to indicate A-normal form.
  8959. (define A-normal-form-declaration (list 'anf))
  8960. (define (A-normal-form E . rest)
  8961. (define (A-normal-form E)
  8962. (anf-make-let* (anf E '() '())))
  8963. ; New temporaries.
  8964. (define temp-counter 0)
  8965. (define temp-prefix
  8966. (if (or (null? rest)
  8967. (not (string? (car rest))))
  8968. (string-append renaming-prefix "T")
  8969. (car rest)))
  8970. (define (newtemp)
  8971. (set! temp-counter (+ temp-counter 1))
  8972. (string->symbol
  8973. (string-append temp-prefix
  8974. (number->string temp-counter))))
  8975. ; Given an expression E as output by pass 2,
  8976. ; a list of surrounding LET* bindings,
  8977. ; and an ordered list of likely register variables,
  8978. ; return a non-empty list of LET* bindings
  8979. ; whose first binding associates a dummy variable
  8980. ; with an A-expression giving the value for E.
  8981. (define (anf E bindings regvars)
  8982. (case (car E)
  8983. ((quote) (anf-bind-dummy E bindings))
  8984. ((begin) (if (variable? E)
  8985. (anf-bind-dummy E bindings)
  8986. (anf-sequential E bindings regvars)))
  8987. ((lambda) (anf-lambda E bindings regvars))
  8988. ((set!) (anf-assignment E bindings regvars))
  8989. ((if) (anf-conditional E bindings regvars))
  8990. (else (anf-call E bindings regvars))))
  8991. (define anf:dummy (string->symbol "RESULT"))
  8992. (define (anf-bind-dummy E bindings)
  8993. (cons (list anf:dummy E)
  8994. bindings))
  8995. ; Unlike anf-bind-dummy, anf-bind-name and anf-bind convert
  8996. ; their expression argument to A-normal form.
  8997. ; Don't change anf-bind to call anf-bind-name, because that
  8998. ; would name the temporaries in an aesthetically bad order.
  8999. (define (anf-bind-name name E bindings regvars)
  9000. (let ((bindings (anf E bindings regvars)))
  9001. (cons (list name (cadr (car bindings)))
  9002. (cdr bindings))))
  9003. (define (anf-bind E bindings regvars)
  9004. (let ((bindings (anf E bindings regvars)))
  9005. (cons (list (newtemp) (cadr (car bindings)))
  9006. (cdr bindings))))
  9007. (define (anf-result bindings)
  9008. (make-variable (car (car bindings))))
  9009. (define (anf-make-let* bindings)
  9010. (define (loop bindings body)
  9011. (if (null? bindings)
  9012. body
  9013. (let ((T1 (car (car bindings)))
  9014. (E1 (cadr (car bindings))))
  9015. (loop (cdr bindings)
  9016. (make-call (make-lambda (list T1)
  9017. '()
  9018. '()
  9019. '()
  9020. '()
  9021. (list A-normal-form-declaration)
  9022. '()
  9023. body)
  9024. (list E1))))))
  9025. (loop (cdr bindings)
  9026. (cadr (car bindings))))
  9027. (define (anf-sequential E bindings regvars)
  9028. (do ((bindings bindings
  9029. (anf-bind (car exprs) bindings regvars))
  9030. (exprs (begin.exprs E)
  9031. (cdr exprs)))
  9032. ((null? (cdr exprs))
  9033. (anf (car exprs) bindings regvars))))
  9034. ; Heuristic: the formal parameters of an escaping lambda or
  9035. ; known local procedure are kept in REG1, REG2, et cetera.
  9036. (define (anf-lambda L bindings regvars)
  9037. (anf-bind-dummy
  9038. (make-lambda (lambda.args L)
  9039. (map (lambda (def)
  9040. (make-definition
  9041. (def.lhs def)
  9042. (A-normal-form (def.rhs def))))
  9043. (lambda.defs L))
  9044. '()
  9045. '()
  9046. '()
  9047. (cons A-normal-form-declaration
  9048. (lambda.decls L))
  9049. (lambda.doc L)
  9050. (anf-make-let*
  9051. (anf (lambda.body L)
  9052. '()
  9053. (make-null-terminated (lambda.args L)))))
  9054. bindings))
  9055. (define (anf-assignment E bindings regvars)
  9056. (let ((I (assignment.lhs E))
  9057. (E1 (assignment.rhs E)))
  9058. (if (variable? E1)
  9059. (anf-bind-dummy E bindings)
  9060. (let* ((bindings (anf-bind E1 bindings regvars))
  9061. (T1 (anf-result bindings)))
  9062. (anf-bind-dummy (make-assignment I T1) bindings)))))
  9063. (define (anf-conditional E bindings regvars)
  9064. (let ((E0 (if.test E))
  9065. (E1 (if.then E))
  9066. (E2 (if.else E)))
  9067. (if (variable? E0)
  9068. (let ((E1 (anf-make-let* (anf E1 '() regvars)))
  9069. (E2 (anf-make-let* (anf E2 '() regvars))))
  9070. (anf-bind-dummy
  9071. (make-conditional E0 E1 E2)
  9072. bindings))
  9073. (let* ((bindings (anf-bind E0 bindings regvars))
  9074. (E1 (anf-make-let* (anf E1 '() regvars)))
  9075. (E2 (anf-make-let* (anf E2 '() regvars))))
  9076. (anf-bind-dummy
  9077. (make-conditional (anf-result bindings) E1 E2)
  9078. bindings)))))
  9079. (define (anf-call E bindings regvars)
  9080. (let* ((proc (call.proc E))
  9081. (args (call.args E)))
  9082. ; Evaluates the exprs and returns both a list of bindings and
  9083. ; a list of the temporaries that name the results of the exprs.
  9084. ; If rename-always? is true, then temporaries are generated even
  9085. ; for constants and temporaries.
  9086. (define (loop exprs bindings names rename-always?)
  9087. (if (null? exprs)
  9088. (values bindings (reverse names))
  9089. (let ((E (car exprs)))
  9090. (if (or rename-always?
  9091. (not (or (constant? E)
  9092. (variable? E))))
  9093. (let* ((bindings
  9094. (anf-bind (car exprs) bindings regvars)))
  9095. (loop (cdr exprs)
  9096. bindings
  9097. (cons (anf-result bindings) names)
  9098. rename-always?))
  9099. (loop (cdr exprs)
  9100. bindings
  9101. (cons E names)
  9102. rename-always?)))))
  9103. ; Evaluates the exprs, binding them to the vars, and returns
  9104. ; a list of bindings.
  9105. ;
  9106. ; Although LET variables are likely to be kept in registers,
  9107. ; trying to guess which register will be allocated is likely
  9108. ; to do more harm than good.
  9109. (define (let-loop exprs bindings regvars vars)
  9110. (if (null? exprs)
  9111. (if (null? (lambda.defs proc))
  9112. (anf (lambda.body proc)
  9113. bindings
  9114. regvars)
  9115. (let ((bindings
  9116. (anf-bind
  9117. (make-lambda '()
  9118. (lambda.defs proc)
  9119. '()
  9120. '()
  9121. '()
  9122. (cons A-normal-form-declaration
  9123. (lambda.decls proc))
  9124. (lambda.doc proc)
  9125. (lambda.body proc))
  9126. bindings
  9127. '())))
  9128. (anf-bind-dummy
  9129. (make-call (anf-result bindings) '())
  9130. bindings)))
  9131. (let-loop (cdr exprs)
  9132. (anf-bind-name (car vars)
  9133. (car exprs)
  9134. bindings
  9135. regvars)
  9136. regvars
  9137. (cdr vars))))
  9138. (cond ((lambda? proc)
  9139. (let ((formals (lambda.args proc)))
  9140. (if (list? formals)
  9141. (let* ((pi (anf-order-of-evaluation args regvars #f))
  9142. (exprs (permute args pi))
  9143. (names (permute (lambda.args proc) pi)))
  9144. (let-loop (reverse exprs) bindings regvars (reverse names)))
  9145. (anf-call (normalize-let E) bindings regvars))))
  9146. ((not (variable? proc))
  9147. (let ((pi (anf-order-of-evaluation args regvars #f)))
  9148. (call-with-values
  9149. (lambda () (loop (permute args pi) bindings '() #t))
  9150. (lambda (bindings names)
  9151. (let ((bindings (anf-bind proc bindings regvars)))
  9152. (anf-bind-dummy
  9153. (make-call (anf-result bindings)
  9154. (unpermute names pi))
  9155. bindings))))))
  9156. ((and (integrate-usual-procedures)
  9157. (prim-entry (variable.name proc)))
  9158. (let ((pi (anf-order-of-evaluation args regvars #t)))
  9159. (call-with-values
  9160. (lambda () (loop (permute args pi) bindings '() #t))
  9161. (lambda (bindings names)
  9162. (anf-bind-dummy
  9163. (make-call proc (unpermute names pi))
  9164. bindings)))))
  9165. ((memq (variable.name proc) regvars)
  9166. (let* ((exprs (cons proc args))
  9167. (pi (anf-order-of-evaluation
  9168. exprs
  9169. (cons name:IGNORED regvars)
  9170. #f)))
  9171. (call-with-values
  9172. (lambda () (loop (permute exprs pi) bindings '() #t))
  9173. (lambda (bindings names)
  9174. (let ((names (unpermute names pi)))
  9175. (anf-bind-dummy
  9176. (make-call (car names) (cdr names))
  9177. bindings))))))
  9178. (else
  9179. (let ((pi (anf-order-of-evaluation args regvars #f)))
  9180. (call-with-values
  9181. (lambda () (loop (permute args pi) bindings '() #t))
  9182. (lambda (bindings names)
  9183. (anf-bind-dummy
  9184. (make-call proc (unpermute names pi))
  9185. bindings))))))))
  9186. ; Given a list of expressions, a list of likely register contents,
  9187. ; and a switch telling whether these are arguments for a primop
  9188. ; or something else (such as the arguments for a real call),
  9189. ; try to choose a good order in which to evaluate the expressions.
  9190. ;
  9191. ; Heuristic: If none of the expressions is a call to a non-primop,
  9192. ; then parallel assignment optimization gives a good order if the
  9193. ; regvars are right, and should do no worse than a random order if
  9194. ; the regvars are wrong.
  9195. ;
  9196. ; Heuristic: If the expressions are arguments to a primop, and
  9197. ; none are a call to a non-primop, then the register contents
  9198. ; are irrelevant, and the first argument should be evaluated last.
  9199. ;
  9200. ; Heuristic: If one or more of the expressions is a call to a
  9201. ; non-primop, then the following should be a good order:
  9202. ;
  9203. ; expressions that are neither a constant, variable, or a call
  9204. ; calls to non-primops
  9205. ; constants and variables
  9206. (define (anf-order-of-evaluation exprs regvars for-primop?)
  9207. (define (ordering targets exprs alist)
  9208. (let ((para
  9209. (parallel-assignment targets alist exprs)))
  9210. (or para
  9211. ; Evaluate left to right until a parallel assignment is found.
  9212. (cons (car targets)
  9213. (ordering (cdr targets)
  9214. (cdr exprs)
  9215. alist)))))
  9216. (if (parallel-assignment-optimization)
  9217. (cond ((null? exprs) '())
  9218. ((null? (cdr exprs)) '(0))
  9219. (else
  9220. (let* ((contains-call? #f)
  9221. (vexprs (list->vector exprs))
  9222. (vindexes (list->vector
  9223. (iota (vector-length vexprs))))
  9224. (contains-call? #f)
  9225. (categories
  9226. (list->vector
  9227. (map (lambda (E)
  9228. (cond ((constant? E)
  9229. 2)
  9230. ((variable? E)
  9231. 2)
  9232. ((complicated? E)
  9233. (set! contains-call? #t)
  9234. 1)
  9235. (else
  9236. 0)))
  9237. exprs))))
  9238. (cond (contains-call?
  9239. (twobit-sort (lambda (i j)
  9240. (< (vector-ref categories i)
  9241. (vector-ref categories j)))
  9242. (iota (length exprs))))
  9243. (for-primop?
  9244. (reverse (iota (length exprs))))
  9245. (else
  9246. (let ((targets (iota (length exprs))))
  9247. (define (pairup regvars targets)
  9248. (if (or (null? targets)
  9249. (null? regvars))
  9250. '()
  9251. (cons (cons (car regvars)
  9252. (car targets))
  9253. (pairup (cdr regvars)
  9254. (cdr targets)))))
  9255. (ordering targets
  9256. exprs
  9257. (pairup regvars targets))))))))
  9258. (iota (length exprs))))
  9259. (define (permute things pi)
  9260. (let ((v (list->vector things)))
  9261. (map (lambda (i) (vector-ref v i))
  9262. pi)))
  9263. (define (unpermute things pi)
  9264. (let* ((v0 (list->vector things))
  9265. (v1 (make-vector (vector-length v0))))
  9266. (do ((pi pi (cdr pi))
  9267. (k 0 (+ k 1)))
  9268. ((null? pi)
  9269. (vector->list v1))
  9270. (vector-set! v1 (car pi) (vector-ref v0 k)))))
  9271. ; Given a call whose procedure is a lambda expression that has
  9272. ; a rest argument, return a genuine let expression.
  9273. (define (normalize-let-error exp)
  9274. (if (issue-warnings)
  9275. (begin (display "WARNING from compiler: ")
  9276. (display "Wrong number of arguments ")
  9277. (display "to lambda expression")
  9278. (newline)
  9279. (pretty-print (make-readable exp) #t)
  9280. (newline))))
  9281. (define (normalize-let exp)
  9282. (let* ((L (call.proc exp)))
  9283. (let loop ((formals (lambda.args L))
  9284. (args (call.args exp))
  9285. (newformals '())
  9286. (newargs '()))
  9287. (cond ((null? formals)
  9288. (if (null? args)
  9289. (begin (lambda.args-set! L (reverse newformals))
  9290. (call.args-set! exp (reverse newargs)))
  9291. (begin (normalize-let-error exp)
  9292. (loop (list (newtemp))
  9293. args
  9294. newformals
  9295. newargs))))
  9296. ((pair? formals)
  9297. (if (pair? args)
  9298. (loop (cdr formals)
  9299. (cdr args)
  9300. (cons (car formals) newformals)
  9301. (cons (car args) newargs))
  9302. (begin (normalize-let-error exp)
  9303. (loop formals
  9304. (cons (make-constant 0)
  9305. args)
  9306. newformals
  9307. newargs))))
  9308. (else
  9309. (loop (list formals)
  9310. (list (make-call-to-list args))
  9311. newformals
  9312. newargs))))))
  9313. ; For heuristic use only.
  9314. ; An expression is complicated unless it can probably be evaluated
  9315. ; without saving and restoring any registers, even if it occurs in
  9316. ; a non-tail position.
  9317. (define (complicated? exp)
  9318. ; Let's not spend all day on this.
  9319. (let ((budget 10))
  9320. (define (complicated? exp)
  9321. (set! budget (- budget 1))
  9322. (if (zero? budget)
  9323. #t
  9324. (case (car exp)
  9325. ((quote) #f)
  9326. ((lambda) #f)
  9327. ((set!) (complicated? (assignment.rhs exp)))
  9328. ((if) (or (complicated? (if.test exp))
  9329. (complicated? (if.then exp))
  9330. (complicated? (if.else exp))))
  9331. ((begin) (if (variable? exp)
  9332. #f
  9333. (some? complicated?
  9334. (begin.exprs exp))))
  9335. (else (let ((proc (call.proc exp)))
  9336. (if (and (variable? proc)
  9337. (integrate-usual-procedures)
  9338. (prim-entry (variable.name proc)))
  9339. (some? complicated?
  9340. (call.args exp))
  9341. #t))))))
  9342. (complicated? exp)))
  9343. (A-normal-form E))
  9344. (define (post-simplify-anf L0 T1 E0 E1 free regbindings L2)
  9345. (define (return-normally)
  9346. (values (make-call L0 (list E1))
  9347. free
  9348. regbindings))
  9349. (return-normally))
  9350. ; Copyright 1999 William D Clinger.
  9351. ;
  9352. ; Permission to copy this software, in whole or in part, to use this
  9353. ; software for any lawful noncommercial purpose, and to redistribute
  9354. ; this software is granted subject to the restriction that all copies
  9355. ; made of this software must include this copyright notice in full.
  9356. ;
  9357. ; I also request that you send me a copy of any improvements that you
  9358. ; make to this software so that they may be incorporated within it to
  9359. ; the benefit of the Scheme community.
  9360. ;
  9361. ; 7 June 1999.
  9362. ;
  9363. ; Intraprocedural common subexpression elimination, constant propagation,
  9364. ; copy propagation, dead code elimination, and register targeting.
  9365. ;
  9366. ; (intraprocedural-commoning E 'commoning)
  9367. ;
  9368. ; Given an A-normal form E (alpha-converted, with correct free
  9369. ; variables and referencing information), returns an optimized
  9370. ; A-normal form with correct free variables but incorrect referencing
  9371. ; information.
  9372. ;
  9373. ; (intraprocedural-commoning E 'target-registers)
  9374. ;
  9375. ; Given an A-normal form E (alpha-converted, with correct free
  9376. ; variables and referencing information), returns an A-normal form
  9377. ; with correct free variables but incorrect referencing information,
  9378. ; and in which MacScheme machine register names are used as temporary
  9379. ; variables. The result is alpha-converted except for register names.
  9380. ;
  9381. ; (intraprocedural-commoning E 'commoning 'target-registers)
  9382. ; (intraprocedural-commoning E)
  9383. ;
  9384. ; Given an A-normal form as described above, returns an optimized
  9385. ; form in which register names are used as temporary variables.
  9386. ; Semantics of .check!:
  9387. ;
  9388. ; (.check! b exn x ...) faults with code exn and arguments x ...
  9389. ; if b is #f.
  9390. ; The list of argument registers.
  9391. ; This can't go in pass3commoning.aux.sch because that file must be
  9392. ; loaded before the target-specific file that defines *nregs*.
  9393. (define argument-registers
  9394. (do ((n (- *nregs* 2) (- n 1))
  9395. (regs '()
  9396. (cons (string->symbol
  9397. (string-append ".REG" (number->string n)))
  9398. regs)))
  9399. ((zero? n)
  9400. regs)))
  9401. (define (intraprocedural-commoning E . flags)
  9402. (define target-registers? (or (null? flags) (memq 'target-registers flags)))
  9403. (define commoning? (or (null? flags) (memq 'commoning flags)))
  9404. (define debugging? #f)
  9405. (call-with-current-continuation
  9406. (lambda (return)
  9407. (define (error . stuff)
  9408. (display "Bug detected during intraprocedural optimization")
  9409. (newline)
  9410. (for-each (lambda (s)
  9411. (display s) (newline))
  9412. stuff)
  9413. (return (make-constant #f)))
  9414. ; Given an expression, an environment, the available expressions,
  9415. ; and an ordered list of likely register variables (used heuristically),
  9416. ; returns the transformed expression and its set of free variables.
  9417. (define (scan-body E env available regvars)
  9418. ; The local variables are those that are bound by a LET within
  9419. ; this procedure. The formals of a lambda expression and the
  9420. ; known local procedures are counted as non-global, not local,
  9421. ; because there is no let-binding for a formal that can be
  9422. ; renamed during register targeting.
  9423. ; For each local variable, we keep track of how many times it
  9424. ; is referenced. This information is not accurate until we
  9425. ; are backing out of the recursion, and does not have to be.
  9426. (define local-variables (make-hashtable symbol-hash assq))
  9427. (define (local-variable? sym)
  9428. (hashtable-get local-variables sym))
  9429. (define (local-variable-not-used? sym)
  9430. (= 0 (hashtable-fetch local-variables sym -1)))
  9431. (define (local-variable-used-once? sym)
  9432. (= 1 (hashtable-fetch local-variables sym 0)))
  9433. (define (record-local-variable! sym)
  9434. (hashtable-put! local-variables sym 0))
  9435. (define (used-local-variable! sym)
  9436. (adjust-local-variable! sym 1))
  9437. (define (adjust-local-variable! sym n)
  9438. (let ((m (hashtable-get local-variables sym)))
  9439. (if debugging?
  9440. (if (and m (> m 0))
  9441. (begin (write (list sym (+ m n)))
  9442. (newline))))
  9443. (if m
  9444. (hashtable-put! local-variables
  9445. sym
  9446. (+ m n)))))
  9447. (define (closed-over-local-variable! sym)
  9448. ; Set its reference count to infinity so it won't be optimized away.
  9449. ; FIXME: One million isn't infinity.
  9450. (hashtable-put! local-variables sym 1000000))
  9451. (define (used-variable! sym)
  9452. (used-local-variable! sym))
  9453. (define (abandon-expression! E)
  9454. (cond ((variable? E)
  9455. (adjust-local-variable! (variable.name E) -1))
  9456. ((conditional? E)
  9457. (abandon-expression! (if.test E))
  9458. (abandon-expression! (if.then E))
  9459. (abandon-expression! (if.else E)))
  9460. ((call? E)
  9461. (for-each (lambda (exp)
  9462. (if (variable? exp)
  9463. (let ((name (variable.name exp)))
  9464. (if (local-variable? name)
  9465. (adjust-local-variable! name -1)))))
  9466. (cons (call.proc E)
  9467. (call.args E))))))
  9468. ; Environments are represented as hashtrees.
  9469. (define (make-empty-environment)
  9470. (make-hashtree symbol-hash assq))
  9471. (define (environment-extend env sym)
  9472. (hashtree-put env sym #t))
  9473. (define (environment-extend* env symbols)
  9474. (if (null? symbols)
  9475. env
  9476. (environment-extend* (hashtree-put env (car symbols) #t)
  9477. (cdr symbols))))
  9478. (define (environment-lookup env sym)
  9479. (hashtree-get env sym))
  9480. (define (global? x)
  9481. (cond ((local-variable? x)
  9482. #f)
  9483. ((environment-lookup env x)
  9484. #f)
  9485. (else
  9486. #t)))
  9487. ;
  9488. (define (available-add! available T E)
  9489. (cond ((constant? E)
  9490. (available-extend! available T E available:killer:immortal))
  9491. ((variable? E)
  9492. (available-extend! available
  9493. T
  9494. E
  9495. (if (global? (variable.name E))
  9496. available:killer:globals
  9497. available:killer:immortal)))
  9498. (else
  9499. (let ((entry (prim-call E)))
  9500. (if entry
  9501. (let ((killer (prim-lives-until entry)))
  9502. (if (not (eq? killer available:killer:dead))
  9503. (do ((args (call.args E) (cdr args))
  9504. (k killer
  9505. (let ((arg (car args)))
  9506. (if (and (variable? arg)
  9507. (global? (variable.name arg)))
  9508. available:killer:globals
  9509. k))))
  9510. ((null? args)
  9511. (available-extend!
  9512. available
  9513. T
  9514. E
  9515. (logior killer k)))))))))))
  9516. ; Given an expression E,
  9517. ; an environment containing all variables that are in scope,
  9518. ; and a table of available expressions,
  9519. ; returns multiple values:
  9520. ; the transformed E
  9521. ; the free variables of E
  9522. ; the register bindings to be inserted; each binding has the form
  9523. ; (R x (begin R)), where (begin R) is a reference to R.
  9524. ;
  9525. ; Side effects E.
  9526. (define (scan E env available)
  9527. (if (not (call? E))
  9528. (scan-rhs E env available)
  9529. (let ((proc (call.proc E)))
  9530. (if (not (lambda? proc))
  9531. (scan-rhs E env available)
  9532. (let ((vars (lambda.args proc)))
  9533. (cond ((null? vars)
  9534. (scan-let0 E env available))
  9535. ((null? (cdr vars))
  9536. (scan-binding E env available))
  9537. (else
  9538. (error (make-readable E)))))))))
  9539. ; E has the form of (let ((T1 E1)) E0).
  9540. (define (scan-binding E env available)
  9541. (let* ((L (call.proc E))
  9542. (T1 (car (lambda.args L)))
  9543. (E1 (car (call.args E)))
  9544. (E0 (lambda.body L)))
  9545. (record-local-variable! T1)
  9546. (call-with-values
  9547. (lambda () (scan-rhs E1 env available))
  9548. (lambda (E1 F1 regbindings1)
  9549. (available-add! available T1 E1)
  9550. (let* ((env (let ((formals
  9551. (make-null-terminated (lambda.args L))))
  9552. (environment-extend*
  9553. (environment-extend* env formals)
  9554. (map def.lhs (lambda.defs L)))))
  9555. (Fdefs (scan-defs L env available)))
  9556. (call-with-values
  9557. (lambda () (scan E0 env available))
  9558. (lambda (E0 F0 regbindings0)
  9559. (lambda.body-set! L E0)
  9560. (if target-registers?
  9561. (scan-binding-phase2
  9562. L T1 E0 E1 F0 F1 Fdefs regbindings0 regbindings1)
  9563. (scan-binding-phase3
  9564. L E0 E1 (union F0 Fdefs)
  9565. F1 regbindings0 regbindings1)))))))))
  9566. ; Given the lambda expression for a let expression that binds
  9567. ; a single variable T1, the transformed body E0 and right hand side E1,
  9568. ; their sets of free variables F0 and F1, the set of free variables
  9569. ; for the internal definitions of L, and the sets of register
  9570. ; bindings that need to be wrapped around E0 and E1, returns the
  9571. ; transformed let expression, its free variables, and register
  9572. ; bindings.
  9573. ;
  9574. ; This phase is concerned exclusively with register bindings,
  9575. ; and is bypassed unless the target-registers flag is specified.
  9576. (define (scan-binding-phase2
  9577. L T1 E0 E1 F0 F1 Fdefs regbindings0 regbindings1)
  9578. ; T1 can't be a register because we haven't
  9579. ; yet inserted register bindings that high up.
  9580. ; Classify the register bindings that need to wrapped around E0:
  9581. ; 1. those that have T1 as their rhs
  9582. ; 2. those whose lhs is a register that is likely to hold
  9583. ; a variable that occurs free in E1
  9584. ; 3. all others
  9585. (define (phase2a)
  9586. (do ((rvars regvars (cdr rvars))
  9587. (regs argument-registers (cdr regs))
  9588. (regs1 '() (if (memq (car rvars) F1)
  9589. (cons (car regs) regs1)
  9590. regs1)))
  9591. ((or (null? rvars)
  9592. (null? regs))
  9593. ; regs1 is the set of registers that are live for E1
  9594. (let loop ((regbindings regbindings0)
  9595. (rb1 '())
  9596. (rb2 '())
  9597. (rb3 '()))
  9598. (if (null? regbindings)
  9599. (phase2b rb1 rb2 rb3)
  9600. (let* ((binding (car regbindings))
  9601. (regbindings (cdr regbindings))
  9602. (lhs (regbinding.lhs binding))
  9603. (rhs (regbinding.rhs binding)))
  9604. (cond ((eq? rhs T1)
  9605. (loop regbindings
  9606. (cons binding rb1)
  9607. rb2
  9608. rb3))
  9609. ((memq lhs regs1)
  9610. (loop regbindings
  9611. rb1
  9612. (cons binding rb2)
  9613. rb3))
  9614. (else
  9615. (loop regbindings
  9616. rb1
  9617. rb2
  9618. (cons binding rb3))))))))))
  9619. ; Determine which categories of register bindings should be
  9620. ; wrapped around E0.
  9621. ; Always wrap the register bindings in category 2.
  9622. ; If E1 is a conditional or a real call, then wrap category 3.
  9623. ; If T1 might be used more than once, then wrap category 1.
  9624. (define (phase2b rb1 rb2 rb3)
  9625. (if (or (conditional? E1)
  9626. (real-call? E1))
  9627. (phase2c (append rb2 rb3) rb1 '())
  9628. (phase2c rb2 rb1 rb3)))
  9629. (define (phase2c towrap rb1 regbindings0)
  9630. (cond ((and (not (null? rb1))
  9631. (local-variable-used-once? T1))
  9632. (phase2d towrap rb1 regbindings0))
  9633. (else
  9634. (phase2e (append rb1 towrap) regbindings0))))
  9635. ; T1 is used only once, and there is a register binding (R T1).
  9636. ; Change T1 to R.
  9637. (define (phase2d towrap regbindings-T1 regbindings0)
  9638. (if (not (null? (cdr regbindings-T1)))
  9639. (error "incorrect number of uses" T1))
  9640. (let* ((regbinding (car regbindings-T1))
  9641. (R (regbinding.lhs regbinding)))
  9642. (lambda.args-set! L (list R))
  9643. (phase2e towrap regbindings0)))
  9644. ; Wrap the selected register bindings around E0.
  9645. (define (phase2e towrap regbindings0)
  9646. (call-with-values
  9647. (lambda ()
  9648. (wrap-with-register-bindings towrap E0 F0))
  9649. (lambda (E0 F0)
  9650. (let ((F (union Fdefs F0)))
  9651. (scan-binding-phase3
  9652. L E0 E1 F F1 regbindings0 regbindings1)))))
  9653. (phase2a))
  9654. ; This phase, with arguments as above, constructs the result.
  9655. (define (scan-binding-phase3 L E0 E1 F F1 regbindings0 regbindings1)
  9656. (let* ((args (lambda.args L))
  9657. (T1 (car args))
  9658. (free (union F1 (difference F args)))
  9659. (simple-let? (simple-lambda? L))
  9660. (regbindings
  9661. ; At least one of regbindings0 and regbindings1
  9662. ; is the empty list.
  9663. (cond ((null? regbindings0)
  9664. regbindings1)
  9665. ((null? regbindings1)
  9666. regbindings0)
  9667. (else
  9668. (error 'scan-binding 'regbindings)))))
  9669. (lambda.body-set! L E0)
  9670. (lambda.F-set! L F)
  9671. (lambda.G-set! L F)
  9672. (cond ((and simple-let?
  9673. (not (memq T1 F))
  9674. (no-side-effects? E1))
  9675. (abandon-expression! E1)
  9676. (values E0 F regbindings0))
  9677. ((and target-registers?
  9678. simple-let?
  9679. (local-variable-used-once? T1))
  9680. (post-simplify-anf L T1 E0 E1 free regbindings #f))
  9681. (else
  9682. (values (make-call L (list E1))
  9683. free
  9684. regbindings)))))
  9685. (define (scan-let0 E env available)
  9686. (let ((L (call.proc E)))
  9687. (if (simple-lambda? L)
  9688. (scan (lambda.body L) env available)
  9689. (let ((T1 (make-variable name:IGNORED)))
  9690. (lambda.args-set! L (list T1))
  9691. (call-with-values
  9692. (lambda () (scan (make-call L (list (make-constant 0)))
  9693. env
  9694. available))
  9695. (lambda (E F regbindings)
  9696. (lambda.args-set! L '())
  9697. (values (make-call L '())
  9698. F
  9699. regbindings)))))))
  9700. ; Optimizes the internal definitions of L and returns their
  9701. ; free variables.
  9702. (define (scan-defs L env available)
  9703. (let loop ((defs (lambda.defs L))
  9704. (newdefs '())
  9705. (Fdefs '()))
  9706. (if (null? defs)
  9707. (begin (lambda.defs-set! L (reverse newdefs))
  9708. Fdefs)
  9709. (let ((def (car defs)))
  9710. (call-with-values
  9711. (lambda ()
  9712. (let* ((Ldef (def.rhs def))
  9713. (Lformals (make-null-terminated (lambda.args Ldef)))
  9714. (Lenv (environment-extend*
  9715. (environment-extend* env Lformals)
  9716. (map def.lhs (lambda.defs Ldef)))))
  9717. (scan Ldef Lenv available)))
  9718. (lambda (rhs Frhs empty)
  9719. (if (not (null? empty))
  9720. (error 'scan-binding 'def))
  9721. (loop (cdr defs)
  9722. (cons (make-definition (def.lhs def) rhs)
  9723. newdefs)
  9724. (union Frhs Fdefs))))))))
  9725. ; Given the right-hand side of a let-binding, an environment,
  9726. ; and a table of available expressions, returns the transformed
  9727. ; expression, its free variables, and the register bindings that
  9728. ; need to be wrapped around it.
  9729. (define (scan-rhs E env available)
  9730. (cond
  9731. ((constant? E)
  9732. (values E (empty-set) '()))
  9733. ((variable? E)
  9734. (let* ((name (variable.name E))
  9735. (Enew (and commoning?
  9736. (if (global? name)
  9737. (let ((T (available-expression
  9738. available E)))
  9739. (if T
  9740. (make-variable T)
  9741. #f))
  9742. (available-variable available name)))))
  9743. (if Enew
  9744. (scan-rhs Enew env available)
  9745. (begin (used-variable! name)
  9746. (values E (list name) '())))))
  9747. ((lambda? E)
  9748. (let* ((formals (make-null-terminated (lambda.args E)))
  9749. (env (environment-extend*
  9750. (environment-extend* env formals)
  9751. (map def.lhs (lambda.defs E))))
  9752. (Fdefs (scan-defs E env available)))
  9753. (call-with-values
  9754. (lambda ()
  9755. (let ((available (copy-available-table available)))
  9756. (available-kill! available available:killer:all)
  9757. (scan-body (lambda.body E)
  9758. env
  9759. available
  9760. formals)))
  9761. (lambda (E0 F0 regbindings0)
  9762. (call-with-values
  9763. (lambda ()
  9764. (wrap-with-register-bindings regbindings0 E0 F0))
  9765. (lambda (E0 F0)
  9766. (lambda.body-set! E E0)
  9767. (let ((F (union Fdefs F0)))
  9768. (for-each (lambda (x)
  9769. (closed-over-local-variable! x))
  9770. F)
  9771. (lambda.F-set! E F)
  9772. (lambda.G-set! E F)
  9773. (values E
  9774. (difference F
  9775. (make-null-terminated
  9776. (lambda.args E)))
  9777. '()))))))))
  9778. ((conditional? E)
  9779. (let ((E0 (if.test E))
  9780. (E1 (if.then E))
  9781. (E2 (if.else E)))
  9782. (if (constant? E0)
  9783. ; FIXME: E1 and E2 might not be a legal rhs,
  9784. ; so we can't just return the simplified E1 or E2.
  9785. (let ((E1 (if (constant.value E0) E1 E2)))
  9786. (call-with-values
  9787. (lambda () (scan E1 env available))
  9788. (lambda (E1 F1 regbindings1)
  9789. (cond ((or (not (call? E1))
  9790. (not (lambda? (call.proc E1))))
  9791. (values E1 F1 regbindings1))
  9792. (else
  9793. ; FIXME: Must return a valid rhs.
  9794. (values (make-conditional
  9795. (make-constant #t)
  9796. E1
  9797. (make-constant 0))
  9798. F1
  9799. regbindings1))))))
  9800. (call-with-values
  9801. (lambda () (scan E0 env available))
  9802. (lambda (E0 F0 regbindings0)
  9803. (if (not (null? regbindings0))
  9804. (error 'scan-rhs 'if))
  9805. (if (not (eq? E0 (if.test E)))
  9806. (scan-rhs (make-conditional E0 E1 E2)
  9807. env available)
  9808. (let ((available1
  9809. (copy-available-table available))
  9810. (available2
  9811. (copy-available-table available)))
  9812. (if (variable? E0)
  9813. (let ((T0 (variable.name E0)))
  9814. (available-add!
  9815. available2 T0 (make-constant #f)))
  9816. (error (make-readable E #t)))
  9817. (call-with-values
  9818. (lambda () (scan E1 env available1))
  9819. (lambda (E1 F1 regbindings1)
  9820. (call-with-values
  9821. (lambda ()
  9822. (wrap-with-register-bindings
  9823. regbindings1 E1 F1))
  9824. (lambda (E1 F1)
  9825. (call-with-values
  9826. (lambda () (scan E2 env available2))
  9827. (lambda (E2 F2 regbindings2)
  9828. (call-with-values
  9829. (lambda ()
  9830. (wrap-with-register-bindings
  9831. regbindings2 E2 F2))
  9832. (lambda (E2 F2)
  9833. (let ((E (make-conditional
  9834. E0 E1 E2))
  9835. (F (union F0 F1 F2)))
  9836. (available-intersect!
  9837. available
  9838. available1
  9839. available2)
  9840. (values E F '())))))))))))))))))
  9841. ((assignment? E)
  9842. (call-with-values
  9843. (lambda () (scan-rhs (assignment.rhs E) env available))
  9844. (lambda (E1 F1 regbindings1)
  9845. (if (not (null? regbindings1))
  9846. (error 'scan-rhs 'set!))
  9847. (available-kill! available available:killer:globals)
  9848. (values (make-assignment (assignment.lhs E) E1)
  9849. (union (list (assignment.lhs E)) F1)
  9850. '()))))
  9851. ((begin? E)
  9852. ; Shouldn't occur in A-normal form.
  9853. (error 'scan-rhs 'begin))
  9854. ((real-call? E)
  9855. (let* ((E0 (call.proc E))
  9856. (args (call.args E))
  9857. (regcontents (append regvars
  9858. (map (lambda (x) #f) args))))
  9859. (let loop ((args args)
  9860. (regs argument-registers)
  9861. (regcontents regcontents)
  9862. (newargs '())
  9863. (regbindings '())
  9864. (F (if (variable? E0)
  9865. (let ((f (variable.name E0)))
  9866. (used-variable! f)
  9867. (list f))
  9868. (empty-set))))
  9869. (cond ((null? args)
  9870. (available-kill! available available:killer:all)
  9871. (values (make-call E0 (reverse newargs))
  9872. F
  9873. regbindings))
  9874. ((null? regs)
  9875. (let ((arg (car args)))
  9876. (loop (cdr args)
  9877. '()
  9878. (cdr regcontents)
  9879. (cons arg newargs)
  9880. regbindings
  9881. (if (variable? arg)
  9882. (let ((name (variable.name arg)))
  9883. (used-variable! name)
  9884. (union (list name) F))
  9885. F))))
  9886. ((and commoning?
  9887. (variable? (car args))
  9888. (available-variable
  9889. available
  9890. (variable.name (car args))))
  9891. (let* ((name (variable.name (car args)))
  9892. (Enew (available-variable available name)))
  9893. (loop (cons Enew (cdr args))
  9894. regs regcontents newargs regbindings F)))
  9895. ((and target-registers?
  9896. (variable? (car args))
  9897. (let ((x (variable.name (car args))))
  9898. ; We haven't yet recorded this use.
  9899. (or (local-variable-not-used? x)
  9900. (and (memq x regvars)
  9901. (not (eq? x (car regcontents)))))))
  9902. (let* ((x (variable.name (car args)))
  9903. (R (car regs))
  9904. (newarg (make-variable R)))
  9905. (used-variable! x)
  9906. (loop (cdr args)
  9907. (cdr regs)
  9908. (cdr regcontents)
  9909. (cons newarg newargs)
  9910. (cons (make-regbinding R x newarg)
  9911. regbindings)
  9912. (union (list R) F))))
  9913. (else
  9914. (let ((E1 (car args)))
  9915. (loop (cdr args)
  9916. (cdr regs)
  9917. (cdr regcontents)
  9918. (cons E1 newargs)
  9919. regbindings
  9920. (if (variable? E1)
  9921. (let ((name (variable.name E1)))
  9922. (used-variable! name)
  9923. (union (list name) F))
  9924. F))))))))
  9925. ((call? E)
  9926. ; Must be a call to a primop.
  9927. (let* ((E0 (call.proc E))
  9928. (f0 (variable.name E0)))
  9929. (let loop ((args (call.args E))
  9930. (newargs '())
  9931. (F (list f0)))
  9932. (cond ((null? args)
  9933. (let* ((E (make-call E0 (reverse newargs)))
  9934. (T (and commoning?
  9935. (available-expression
  9936. available E))))
  9937. (if T
  9938. (begin (abandon-expression! E)
  9939. (scan-rhs (make-variable T) env available))
  9940. (begin
  9941. (available-kill!
  9942. available
  9943. (prim-kills (prim-entry f0)))
  9944. (cond ((eq? f0 name:check!)
  9945. (let ((x (car (call.args E))))
  9946. (cond ((not (runtime-safety-checking))
  9947. (abandon-expression! E)
  9948. ;(values x '() '())
  9949. (scan-rhs x env available))
  9950. ((variable? x)
  9951. (available-add!
  9952. available
  9953. (variable.name x)
  9954. (make-constant #t))
  9955. (values E F '()))
  9956. ((constant.value x)
  9957. (abandon-expression! E)
  9958. (values x '() '()))
  9959. (else
  9960. (declaration-error E)
  9961. (values E F '())))))
  9962. (else
  9963. (values E F '())))))))
  9964. ((variable? (car args))
  9965. (let* ((E1 (car args))
  9966. (x (variable.name E1))
  9967. (Enew
  9968. (and commoning?
  9969. (available-variable available x))))
  9970. (if Enew
  9971. ; All of the arguments are constants or
  9972. ; variables, so if the variable is replaced
  9973. ; here it will be replaced throughout the call.
  9974. (loop (cons Enew (cdr args))
  9975. newargs
  9976. (remq x F))
  9977. (begin
  9978. (used-variable! x)
  9979. (loop (cdr args)
  9980. (cons (car args) newargs)
  9981. (union (list x) F))))))
  9982. (else
  9983. (loop (cdr args)
  9984. (cons (car args) newargs)
  9985. F))))))
  9986. (else
  9987. (error 'scan-rhs (make-readable E)))))
  9988. (call-with-values
  9989. (lambda () (scan E env available))
  9990. (lambda (E F regbindings)
  9991. (call-with-values
  9992. (lambda () (wrap-with-register-bindings regbindings E F))
  9993. (lambda (E F)
  9994. (values E F '()))))))
  9995. (call-with-values
  9996. (lambda ()
  9997. (scan-body E
  9998. (make-hashtree symbol-hash assq)
  9999. (make-available-table)
  10000. '()))
  10001. (lambda (E F regbindings)
  10002. (if (not (null? regbindings))
  10003. (error 'scan-body))
  10004. E)))))
  10005. ; Copyright 1999 William D Clinger.
  10006. ;
  10007. ; Permission to copy this software, in whole or in part, to use this
  10008. ; software for any lawful noncommercial purpose, and to redistribute
  10009. ; this software is granted subject to the restriction that all copies
  10010. ; made of this software must include this copyright notice in full.
  10011. ;
  10012. ; I also request that you send me a copy of any improvements that you
  10013. ; make to this software so that they may be incorporated within it to
  10014. ; the benefit of the Scheme community.
  10015. ;
  10016. ; 16 June 1999.
  10017. ;
  10018. ; Intraprocedural representation inference.
  10019. (define (representation-analysis exp)
  10020. (let* ((debugging? #f)
  10021. (integrate-usual? (integrate-usual-procedures))
  10022. (known (make-hashtable symbol-hash assq))
  10023. (types (make-hashtable symbol-hash assq))
  10024. (g (callgraph exp))
  10025. (schedule (list (callgraphnode.code (car g))))
  10026. (changed? #f)
  10027. (mutate? #f))
  10028. ; known is a hashtable that maps the name of a known local procedure
  10029. ; to a list of the form (tv1 ... tvN), where tv1, ..., tvN
  10030. ; are type variables that stand for the representation types of its
  10031. ; arguments. The type variable that stands for the representation
  10032. ; type of the result of the procedure has the same name as the
  10033. ; procedure itself.
  10034. ; types is a hashtable that maps local variables and the names
  10035. ; of known local procedures to an approximation of their
  10036. ; representation type.
  10037. ; For a known local procedure, the representation type is for the
  10038. ; result of the procedure, not the procedure itself.
  10039. ; schedule is a stack of work that needs to be done.
  10040. ; Each entry in the stack is either an escaping lambda expression
  10041. ; or the name of a known local procedure.
  10042. (define (schedule! job)
  10043. (if (not (memq job schedule))
  10044. (begin (set! schedule (cons job schedule))
  10045. (if (not (symbol? job))
  10046. (callgraphnode.info! (lookup-node job) #t)))))
  10047. ; Schedules a known local procedure.
  10048. (define (schedule-known-procedure! name)
  10049. ; Mark every known procedure that can actually be called.
  10050. (callgraphnode.info! (assq name g) #t)
  10051. (schedule! name))
  10052. ; Schedule all code that calls the given known local procedure.
  10053. (define (schedule-callers! name)
  10054. (for-each (lambda (node)
  10055. (if (and (callgraphnode.info node)
  10056. (or (memq name (callgraphnode.tailcalls node))
  10057. (memq name (callgraphnode.nontailcalls node))))
  10058. (let ((caller (callgraphnode.name node)))
  10059. (if caller
  10060. (schedule! caller)
  10061. (schedule! (callgraphnode.code node))))))
  10062. g))
  10063. ; Schedules local procedures of a lambda expression.
  10064. (define (schedule-local-procedures! L)
  10065. (for-each (lambda (def)
  10066. (let ((name (def.lhs def)))
  10067. (if (known-procedure-is-callable? name)
  10068. (schedule! name))))
  10069. (lambda.defs L)))
  10070. ; Returns true iff the given known procedure is known to be callable.
  10071. (define (known-procedure-is-callable? name)
  10072. (callgraphnode.info (assq name g)))
  10073. ; Sets CHANGED? to #t and returns #t if the type variable's
  10074. ; approximation has changed; otherwise returns #f.
  10075. (define (update-typevar! tv type)
  10076. (let* ((type0 (hashtable-get types tv))
  10077. (type0 (or type0
  10078. (begin (hashtable-put! types tv rep:bottom)
  10079. rep:bottom)))
  10080. (type1 (representation-union type0 type)))
  10081. (if (eq? type0 type1)
  10082. #f
  10083. (begin (hashtable-put! types tv type1)
  10084. (set! changed? #t)
  10085. (if (and debugging? mutate?)
  10086. (begin (display "******** Changing type of ")
  10087. (display tv)
  10088. (display " from ")
  10089. (display (rep->symbol type0))
  10090. (display " to ")
  10091. (display (rep->symbol type1))
  10092. (newline)))
  10093. #t))))
  10094. ; GIven the name of a known local procedure, returns its code.
  10095. (define (lookup-code name)
  10096. (callgraphnode.code (assq name g)))
  10097. ; Given a lambda expression, either escaping or the code for
  10098. ; a known local procedure, returns its node in the call graph.
  10099. (define (lookup-node L)
  10100. (let loop ((g g))
  10101. (cond ((null? g)
  10102. (error "Unknown lambda expression" (make-readable L #t)))
  10103. ((eq? L (callgraphnode.code (car g)))
  10104. (car g))
  10105. (else
  10106. (loop (cdr g))))))
  10107. ; Given: a type variable, expression, and a set of constraints.
  10108. ; Side effects:
  10109. ; Update the representation types of all variables that are
  10110. ; bound within the expression.
  10111. ; Update the representation types of all arguments to known
  10112. ; local procedures that are called within the expression.
  10113. ; If the representation type of an argument to a known local
  10114. ; procedure changes, then schedule that procedure's code
  10115. ; for analysis.
  10116. ; Update the constraint set to reflect the constraints that
  10117. ; hold following execution of the expression.
  10118. ; If mutate? is true, then transform the expression to rely
  10119. ; on the representation types that have been inferred.
  10120. ; Return: type of the expression under the current assumptions
  10121. ; and constraints.
  10122. (define (analyze exp constraints)
  10123. (if (and #f debugging?)
  10124. (begin (display "Analyzing: ")
  10125. (newline)
  10126. (pretty-print (make-readable exp #t))
  10127. (newline)))
  10128. (case (car exp)
  10129. ((quote)
  10130. (representation-of-value (constant.value exp)))
  10131. ((begin)
  10132. (let* ((name (variable.name exp)))
  10133. (representation-typeof name types constraints)))
  10134. ((lambda)
  10135. (schedule! exp)
  10136. rep:procedure)
  10137. ((set!)
  10138. (analyze (assignment.rhs exp) constraints)
  10139. (constraints-kill! constraints available:killer:globals)
  10140. rep:object)
  10141. ((if)
  10142. (let* ((E0 (if.test exp))
  10143. (E1 (if.then exp))
  10144. (E2 (if.else exp))
  10145. (type0 (analyze E0 constraints)))
  10146. (if mutate?
  10147. (cond ((representation-subtype? type0 rep:true)
  10148. (if.test-set! exp (make-constant #t)))
  10149. ((representation-subtype? type0 rep:false)
  10150. (if.test-set! exp (make-constant #f)))))
  10151. (cond ((representation-subtype? type0 rep:true)
  10152. (analyze E1 constraints))
  10153. ((representation-subtype? type0 rep:false)
  10154. (analyze E2 constraints))
  10155. ((variable? E0)
  10156. (let* ((T0 (variable.name E0))
  10157. (ignored (analyze E0 constraints))
  10158. (constraints1 (copy-constraints-table constraints))
  10159. (constraints2 (copy-constraints-table constraints)))
  10160. (constraints-add! types
  10161. constraints1
  10162. (make-type-constraint
  10163. T0 rep:true available:killer:immortal))
  10164. (constraints-add! types
  10165. constraints2
  10166. (make-type-constraint
  10167. T0 rep:false available:killer:immortal))
  10168. (let* ((type1 (analyze E1 constraints1))
  10169. (type2 (analyze E2 constraints2))
  10170. (type (representation-union type1 type2)))
  10171. (constraints-intersect! constraints
  10172. constraints1
  10173. constraints2)
  10174. type)))
  10175. (else
  10176. (representation-error "Bad ANF" (make-readable exp #t))))))
  10177. (else
  10178. (let ((proc (call.proc exp))
  10179. (args (call.args exp)))
  10180. (cond ((lambda? proc)
  10181. (cond ((null? args)
  10182. (analyze-let0 exp constraints))
  10183. ((null? (cdr args))
  10184. (analyze-let1 exp constraints))
  10185. (else
  10186. (error "Compiler bug: pass3rep"))))
  10187. ((variable? proc)
  10188. (let* ((procname (variable.name proc)))
  10189. (cond ((hashtable-get known procname)
  10190. =>
  10191. (lambda (vars)
  10192. (analyze-known-call exp constraints vars)))
  10193. (integrate-usual?
  10194. (let ((entry (prim-entry procname)))
  10195. (if entry
  10196. (analyze-primop-call exp constraints entry)
  10197. (analyze-unknown-call exp constraints))))
  10198. (else
  10199. (analyze-unknown-call exp constraints)))))
  10200. (else
  10201. (analyze-unknown-call exp constraints)))))))
  10202. (define (analyze-let0 exp constraints)
  10203. (let ((proc (call.proc exp)))
  10204. (schedule-local-procedures! proc)
  10205. (if (null? (lambda.args proc))
  10206. (analyze (lambda.body exp) constraints)
  10207. (analyze-unknown-call exp constraints))))
  10208. (define (analyze-let1 exp constraints)
  10209. (let* ((proc (call.proc exp))
  10210. (vars (lambda.args proc)))
  10211. (schedule-local-procedures! proc)
  10212. (if (and (pair? vars)
  10213. (null? (cdr vars)))
  10214. (let* ((T1 (car vars))
  10215. (E1 (car (call.args exp))))
  10216. (if (and integrate-usual? (call? E1))
  10217. (let ((proc (call.proc E1))
  10218. (args (call.args E1)))
  10219. (if (variable? proc)
  10220. (let* ((op (variable.name proc))
  10221. (entry (prim-entry op))
  10222. (K1 (if entry
  10223. (prim-lives-until entry)
  10224. available:killer:dead)))
  10225. (if (not (= K1 available:killer:dead))
  10226. ; Must copy the call to avoid problems
  10227. ; with side effects when mutate? is true.
  10228. (constraints-add!
  10229. types
  10230. constraints
  10231. (make-constraint T1
  10232. (make-call proc args)
  10233. K1)))))))
  10234. (update-typevar! T1 (analyze E1 constraints))
  10235. (analyze (lambda.body proc) constraints))
  10236. (analyze-unknown-call exp constraints))))
  10237. (define (analyze-primop-call exp constraints entry)
  10238. (let* ((op (prim-opcodename entry))
  10239. (args (call.args exp))
  10240. (argtypes (map (lambda (arg) (analyze arg constraints))
  10241. args))
  10242. (type (rep-result? op argtypes)))
  10243. (constraints-kill! constraints (prim-kills entry))
  10244. (cond ((and (eq? op 'check!)
  10245. (variable? (car args)))
  10246. (let ((varname (variable.name (car args))))
  10247. (if (and mutate?
  10248. (representation-subtype? (car argtypes) rep:true))
  10249. (call.args-set! exp
  10250. (cons (make-constant #t) (cdr args))))
  10251. (constraints-add! types
  10252. constraints
  10253. (make-type-constraint
  10254. varname
  10255. rep:true
  10256. available:killer:immortal))))
  10257. ((and mutate? (rep-specific? op argtypes))
  10258. =>
  10259. (lambda (newop)
  10260. (call.proc-set! exp (make-variable newop)))))
  10261. (or type rep:object)))
  10262. (define (analyze-known-call exp constraints vars)
  10263. (let* ((procname (variable.name (call.proc exp)))
  10264. (args (call.args exp))
  10265. (argtypes (map (lambda (arg) (analyze arg constraints))
  10266. args)))
  10267. (if (not (known-procedure-is-callable? procname))
  10268. (schedule-known-procedure! procname))
  10269. (for-each (lambda (var type)
  10270. (if (update-typevar! var type)
  10271. (schedule-known-procedure! procname)))
  10272. vars
  10273. argtypes)
  10274. ; FIXME: We aren't analyzing the effects of known local procedures.
  10275. (constraints-kill! constraints available:killer:all)
  10276. (hashtable-get types procname)))
  10277. (define (analyze-unknown-call exp constraints)
  10278. (analyze (call.proc exp) constraints)
  10279. (for-each (lambda (arg) (analyze arg constraints))
  10280. (call.args exp))
  10281. (constraints-kill! constraints available:killer:all)
  10282. rep:object)
  10283. (define (analyze-known-local-procedure name)
  10284. (if debugging?
  10285. (begin (display "Analyzing ")
  10286. (display name)
  10287. (newline)))
  10288. (let ((L (lookup-code name))
  10289. (constraints (make-constraints-table)))
  10290. (schedule-local-procedures! L)
  10291. (let ((type (analyze (lambda.body L) constraints)))
  10292. (if (update-typevar! name type)
  10293. (schedule-callers! name))
  10294. type)))
  10295. (define (analyze-unknown-lambda L)
  10296. (if debugging?
  10297. (begin (display "Analyzing escaping lambda expression")
  10298. (newline)))
  10299. (schedule-local-procedures! L)
  10300. (let ((vars (make-null-terminated (lambda.args L))))
  10301. (for-each (lambda (var)
  10302. (hashtable-put! types var rep:object))
  10303. vars)
  10304. (analyze (lambda.body L)
  10305. (make-constraints-table))))
  10306. ; For debugging.
  10307. (define (display-types)
  10308. (hashtable-for-each (lambda (f vars)
  10309. (write f)
  10310. (display " : returns ")
  10311. (write (rep->symbol (hashtable-get types f)))
  10312. (newline)
  10313. (for-each (lambda (x)
  10314. (display " ")
  10315. (write x)
  10316. (display ": ")
  10317. (write (rep->symbol
  10318. (hashtable-get types x)))
  10319. (newline))
  10320. vars))
  10321. known))
  10322. (define (display-all-types)
  10323. (let* ((vars (hashtable-map (lambda (x type) x) types))
  10324. (vars (twobit-sort (lambda (var1 var2)
  10325. (string<=? (symbol->string var1)
  10326. (symbol->string var2)))
  10327. vars)))
  10328. (for-each (lambda (x)
  10329. (write x)
  10330. (display ": ")
  10331. (write (rep->symbol
  10332. (hashtable-get types x)))
  10333. (newline))
  10334. vars)))
  10335. '
  10336. (if debugging?
  10337. (begin (pretty-print (make-readable (car schedule) #t))
  10338. (newline)))
  10339. (if debugging?
  10340. (view-callgraph g))
  10341. (for-each (lambda (node)
  10342. (let* ((name (callgraphnode.name node))
  10343. (code (callgraphnode.code node))
  10344. (vars (make-null-terminated (lambda.args code)))
  10345. (known? (symbol? name))
  10346. (rep (if known? rep:bottom rep:object)))
  10347. (callgraphnode.info! node #f)
  10348. (if known?
  10349. (begin (hashtable-put! known name vars)
  10350. (hashtable-put! types name rep)))
  10351. (for-each (lambda (var)
  10352. (hashtable-put! types var rep))
  10353. vars)))
  10354. g)
  10355. (let loop ()
  10356. (cond ((not (null? schedule))
  10357. (let ((job (car schedule)))
  10358. (set! schedule (cdr schedule))
  10359. (if (symbol? job)
  10360. (analyze-known-local-procedure job)
  10361. (analyze-unknown-lambda job))
  10362. (loop)))
  10363. (changed?
  10364. (set! changed? #f)
  10365. (set! schedule (list (callgraphnode.code (car g))))
  10366. (if debugging?
  10367. (begin (display-all-types) (newline)))
  10368. (loop))))
  10369. (if debugging?
  10370. (display-types))
  10371. (set! mutate? #t)
  10372. ; We don't want to analyze known procedures that are never called.
  10373. (set! schedule
  10374. (cons (callgraphnode.code (car g))
  10375. (map callgraphnode.name
  10376. (filter (lambda (node)
  10377. (let* ((name (callgraphnode.name node))
  10378. (known? (symbol? name))
  10379. (marked?
  10380. (known-procedure-is-callable? name)))
  10381. (callgraphnode.info! node #f)
  10382. (and known? marked?)))
  10383. g))))
  10384. (let loop ()
  10385. (if (not (null? schedule))
  10386. (let ((job (car schedule)))
  10387. (set! schedule (cdr schedule))
  10388. (if (symbol? job)
  10389. (analyze-known-local-procedure job)
  10390. (analyze-unknown-lambda job))
  10391. (loop))))
  10392. (if changed?
  10393. (error "Compiler bug in representation inference"))
  10394. (if debugging?
  10395. (pretty-print (make-readable (callgraphnode.code (car g)) #t)))
  10396. exp))
  10397. ; Copyright 1999 William D Clinger.
  10398. ;
  10399. ; Permission to copy this software, in whole or in part, to use this
  10400. ; software for any lawful noncommercial purpose, and to redistribute
  10401. ; this software is granted subject to the restriction that all copies
  10402. ; made of this software must include this copyright notice in full.
  10403. ;
  10404. ; I also request that you send me a copy of any improvements that you
  10405. ; make to this software so that they may be incorporated within it to
  10406. ; the benefit of the Scheme community.
  10407. ;
  10408. ; 11 June 1999.
  10409. ;
  10410. ; The third "pass" of the Twobit compiler actually consists of several
  10411. ; passes, which are related by the common theme of flow analysis:
  10412. ; interprocedural inlining of known local procedures
  10413. ; interprocedural constant propagation and folding
  10414. ; intraprocedural commoning, copy propagation, and dead code elimination
  10415. ; representation inference (not yet implemented)
  10416. ; register targeting
  10417. ;
  10418. ; This pass operates as source-to-source transformations on
  10419. ; expressions written in the subset of Scheme described by the
  10420. ; following grammar:
  10421. ;
  10422. ; "X ..." means zero or more occurrences of X.
  10423. ;
  10424. ; L --> (lambda (I_1 ...)
  10425. ; (begin D ...)
  10426. ; (quote (R F G <decls> <doc>)
  10427. ; E)
  10428. ; | (lambda (I_1 ... . I_rest)
  10429. ; (begin D ...)
  10430. ; (quote (R F G <decls> <doc>))
  10431. ; E)
  10432. ; D --> (define I L)
  10433. ; E --> (quote K) ; constants
  10434. ; | (begin I) ; variable references
  10435. ; | L ; lambda expressions
  10436. ; | (E0 E1 ...) ; calls
  10437. ; | (set! I E) ; assignments
  10438. ; | (if E0 E1 E2) ; conditionals
  10439. ; | (begin E0 E1 E2 ...) ; sequential expressions
  10440. ; I --> <identifier>
  10441. ;
  10442. ; R --> ((I <references> <assignments> <calls>) ...)
  10443. ; F --> (I ...)
  10444. ; G --> (I ...)
  10445. ;
  10446. ; Invariants that hold for the input only:
  10447. ; * There are no assignments except to global variables.
  10448. ; * If I is declared by an internal definition, then the right hand
  10449. ; side of the internal definition is a lambda expression and I
  10450. ; is referenced only in the procedure position of a call.
  10451. ; * R, F, and G are garbage.
  10452. ; * Variables named IGNORED are neither referenced nor assigned.
  10453. ; * The expression does not share structure with the original input,
  10454. ; but might share structure with itself.
  10455. ;
  10456. ; Invariants that hold for the output only:
  10457. ; * There are no assignments except to global variables.
  10458. ; * If I is declared by an internal definition, then the right hand
  10459. ; side of the internal definition is a lambda expression and I
  10460. ; is referenced only in the procedure position of a call.
  10461. ; * R is garbage.
  10462. ; * For each lambda expression, the associated F is a list of all
  10463. ; the identifiers that occur free in the body of that lambda
  10464. ; expression, and possibly a few extra identifiers that were
  10465. ; once free but have been removed by optimization.
  10466. ; * If a lambda expression is declared to be in A-normal form (see
  10467. ; pass3anormal.sch), then it really is in A-normal form.
  10468. ;
  10469. ; The phases of pass 3 interact with the referencing information R
  10470. ; and the free variables F as follows:
  10471. ;
  10472. ; Inlining ignores R, ignores F, destroys R, destroys F.
  10473. ; Constant propagation uses R, ignores F, preserves R, preserves F.
  10474. ; Conversion to ANF ignores R, ignores F, destroys R, destroys F.
  10475. ; Commoning ignores R, ignores F, destroys R, computes F.
  10476. ; Register targeting ignores R, ignores F, destroys R, computes F.
  10477. (define (pass3 exp)
  10478. (define (phase1 exp)
  10479. (if (interprocedural-inlining)
  10480. (let ((g (callgraph exp)))
  10481. (inline-using-callgraph! g)
  10482. exp)
  10483. exp))
  10484. (define (phase2 exp)
  10485. (if (interprocedural-constant-propagation)
  10486. (constant-propagation (copy-exp exp))
  10487. exp))
  10488. (define (phase3 exp)
  10489. (if (common-subexpression-elimination)
  10490. (let* ((exp (if (interprocedural-constant-propagation)
  10491. exp
  10492. ; alpha-conversion
  10493. (copy-exp exp)))
  10494. (exp (a-normal-form exp)))
  10495. (if (representation-inference)
  10496. (intraprocedural-commoning exp 'commoning)
  10497. (intraprocedural-commoning exp)))
  10498. exp))
  10499. (define (phase4 exp)
  10500. (if (representation-inference)
  10501. (let ((exp (cond ((common-subexpression-elimination)
  10502. exp)
  10503. ((interprocedural-constant-propagation)
  10504. (a-normal-form exp))
  10505. (else
  10506. ; alpha-conversion
  10507. (a-normal-form (copy-exp exp))))))
  10508. (intraprocedural-commoning
  10509. (representation-analysis exp)))
  10510. exp))
  10511. (define (finish exp)
  10512. (if (and (not (interprocedural-constant-propagation))
  10513. (not (common-subexpression-elimination)))
  10514. (begin (compute-free-variables! exp)
  10515. exp)
  10516. ;(make-begin (list (make-constant 'anf) exp))))
  10517. exp))
  10518. (define (verify exp)
  10519. (check-referencing-invariants exp 'free)
  10520. exp)
  10521. (if (global-optimization)
  10522. (verify (finish (phase4 (phase3 (phase2 (phase1 exp))))))
  10523. (begin (compute-free-variables! exp)
  10524. (verify exp))))
  10525. ; Copyright 1991 Lightship Software, Incorporated.
  10526. ;
  10527. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  10528. ;
  10529. ; 4 June 1999
  10530. ; Implements the following abstract data types.
  10531. ;
  10532. ; labels
  10533. ; (init-labels)
  10534. ; (make-label)
  10535. ; cg-label-counter
  10536. ;
  10537. ; assembly streams
  10538. ; (make-assembly-stream)
  10539. ; (assembly-stream-code as)
  10540. ; (gen! as . instruction)
  10541. ; (gen-instruction! as instruction)
  10542. ; (gen-save! as frame)
  10543. ; (gen-restore! as frame)
  10544. ; (gen-pop! as frame)
  10545. ; (gen-setstk! as frame v)
  10546. ; (gen-store! as frame r v)
  10547. ; (gen-load! as frame r v)
  10548. ; (gen-stack! as frame v)
  10549. ;
  10550. ; temporaries
  10551. ; (init-temps)
  10552. ; (newtemp)
  10553. ; (newtemps)
  10554. ; newtemp-counter
  10555. ;
  10556. ; register environments
  10557. ; (cgreg-initial)
  10558. ; (cgreg-copy regs)
  10559. ; (cgreg-tos regs)
  10560. ; (cgreg-liveregs regs)
  10561. ; (cgreg-live regs r)
  10562. ; (cgreg-vars regs)
  10563. ; (cgreg-bind! regs r v)
  10564. ; (cgreg-bindregs! regs vars)
  10565. ; (cgreg-rename! regs alist)
  10566. ; (cgreg-release! regs r)
  10567. ; (cgreg-clear! regs)
  10568. ; (cgreg-lookup regs var)
  10569. ; (cgreg-lookup-reg regs r)
  10570. ; (cgreg-join! regs1 regs2)
  10571. ;
  10572. ; stack frame environments
  10573. ; (cgframe-initial)
  10574. ; (cgframe-size-cell frame)
  10575. ; (cgframe-size frame)
  10576. ; (cgframe-copy frame)
  10577. ; (cgframe-join! frame1 frame2)
  10578. ; (cgframe-update-stale! frame)
  10579. ; (cgframe-used! frame)
  10580. ; (cgframe-bind! frame n v instruction)
  10581. ; (cgframe-touch! frame v)
  10582. ; (cgframe-rename! frame alist)
  10583. ; (cgframe-release! frame v)
  10584. ; (cgframe-lookup frame v)
  10585. ; (cgframe-spilled? frame v)
  10586. ;
  10587. ; environments
  10588. ; (entry.name entry)
  10589. ; (entry.kind entry)
  10590. ; (entry.rib entry)
  10591. ; (entry.offset entry)
  10592. ; (entry.label entry)
  10593. ; (entry.regnum entry)
  10594. ; (entry.arity entry)
  10595. ; (entry.op entry)
  10596. ; (entry.imm entry)
  10597. ; (cgenv-initial)
  10598. ; (cgenv-lookup env id)
  10599. ; (cgenv-extend env vars procs)
  10600. ; (cgenv-bindprocs env procs)
  10601. ; (var-lookup var regs frame env)
  10602. ; Labels.
  10603. (define (init-labels)
  10604. (set! cg-label-counter 1000))
  10605. (define (make-label)
  10606. (set! cg-label-counter (+ cg-label-counter 1))
  10607. cg-label-counter)
  10608. (define cg-label-counter 1000)
  10609. ; an assembly stream into which instructions should be emitted
  10610. ; an expression
  10611. ; the desired target register ('result, a register number, or '#f)
  10612. ; a register environment [cgreg]
  10613. ; a stack-frame environment [cgframe]
  10614. ; contains size of frame, current top of frame
  10615. ; a compile-time environment [cgenv]
  10616. ; a flag indicating whether the expression is in tail position
  10617. ; Assembly streams, into which instructions are emitted by side effect.
  10618. ; Represented as a list of two things:
  10619. ;
  10620. ; Assembly code, represented as a pair whose car is a nonempty list
  10621. ; whose cdr is a possibly empty list of MacScheme machine assembly
  10622. ; instructions, and whose cdr is the last pair of the car.
  10623. ;
  10624. ; Any Scheme object that the code generator wants to associate with
  10625. ; this code.
  10626. (define (make-assembly-stream)
  10627. (let ((code (list (list 0))))
  10628. (set-cdr! code (car code))
  10629. (list code #f)))
  10630. (define (assembly-stream-code output)
  10631. (if (local-optimizations)
  10632. (filter-basic-blocks (cdar (car output)))
  10633. (cdar (car output))))
  10634. (define (assembly-stream-info output)
  10635. (cadr output))
  10636. (define (assembly-stream-info! output x)
  10637. (set-car! (cdr output) x)
  10638. #f)
  10639. (define (gen-instruction! output instruction)
  10640. (let ((pair (list instruction))
  10641. (code (car output)))
  10642. (set-cdr! (cdr code) pair)
  10643. (set-cdr! code pair)
  10644. output))
  10645. ;
  10646. (define (gen! output . instruction)
  10647. (gen-instruction! output instruction))
  10648. (define (gen-save! output frame t0)
  10649. (let ((size (cgframe-size-cell frame)))
  10650. (gen-instruction! output (cons $save size))
  10651. (gen-store! output frame 0 t0)
  10652. (cgframe:stale-set! frame '())))
  10653. (define (gen-restore! output frame)
  10654. (let ((size (cgframe-size-cell frame)))
  10655. (gen-instruction! output (cons $restore size))))
  10656. (define (gen-pop! output frame)
  10657. (let ((size (cgframe-size-cell frame)))
  10658. (gen-instruction! output (cons $pop size))))
  10659. (define (gen-setstk! output frame tempname)
  10660. (let ((instruction (list $nop $setstk -1)))
  10661. (cgframe-bind! frame tempname instruction)
  10662. (gen-instruction! output instruction)))
  10663. (define (gen-store! output frame r tempname)
  10664. (let ((instruction (list $nop $store r -1)))
  10665. (cgframe-bind! frame tempname instruction)
  10666. (gen-instruction! output instruction)))
  10667. (define (gen-load! output frame r tempname)
  10668. (cgframe-touch! frame tempname)
  10669. (let ((n (entry.slotnum (cgframe-lookup frame tempname))))
  10670. (gen! output $load r n)))
  10671. (define (gen-stack! output frame tempname)
  10672. (cgframe-touch! frame tempname)
  10673. (let ((n (entry.slotnum (cgframe-lookup frame tempname))))
  10674. (gen! output $stack n)))
  10675. ; Returns a temporary name.
  10676. ; Temporaries are compared using EQ?, so the use of small
  10677. ; exact integers as temporary names is implementation-dependent.
  10678. (define (init-temps)
  10679. (set! newtemp-counter 5000))
  10680. (define (newtemp)
  10681. (set! newtemp-counter
  10682. (+ newtemp-counter 1))
  10683. newtemp-counter)
  10684. (define newtemp-counter 5000)
  10685. (define (newtemps n)
  10686. (if (zero? n)
  10687. '()
  10688. (cons (newtemp)
  10689. (newtemps (- n 1)))))
  10690. ; New representation of
  10691. ; Register environments.
  10692. ; Represented as a list of three items:
  10693. ; an exact integer, one more than the highest index of a live register
  10694. ; a mutable vector with *nregs* elements of the form
  10695. ; #f (the register is dead)
  10696. ; #t (the register is live)
  10697. ; v (the register contains variable v)
  10698. ; t (the register contains temporary variable t)
  10699. ; a mutable vector of booleans: true if the register might be stale
  10700. (define (cgreg-makeregs n v1 v2) (list n v1 v2))
  10701. (define (cgreg-liveregs regs)
  10702. (car regs))
  10703. (define (cgreg-contents regs)
  10704. (cadr regs))
  10705. (define (cgreg-stale regs)
  10706. (caddr regs))
  10707. (define (cgreg-liveregs-set! regs n)
  10708. (set-car! regs n)
  10709. regs)
  10710. (define (cgreg-initial)
  10711. (let ((v1 (make-vector *nregs* #f))
  10712. (v2 (make-vector *nregs* #f)))
  10713. (cgreg-makeregs 0 v1 v2)))
  10714. (define (cgreg-copy regs)
  10715. (let* ((newregs (cgreg-initial))
  10716. (v1a (cgreg-contents regs))
  10717. (v2a (cgreg-stale regs))
  10718. (v1 (cgreg-contents newregs))
  10719. (v2 (cgreg-stale newregs))
  10720. (n (vector-length v1a)))
  10721. (cgreg-liveregs-set! newregs (cgreg-liveregs regs))
  10722. (do ((i 0 (+ i 1)))
  10723. ((= i n)
  10724. newregs)
  10725. (vector-set! v1 i (vector-ref v1a i))
  10726. (vector-set! v2 i (vector-ref v2a i)))))
  10727. (define (cgreg-tos regs)
  10728. (- (cgreg-liveregs regs) 1))
  10729. (define (cgreg-live regs r)
  10730. (if (eq? r 'result)
  10731. (cgreg-tos regs)
  10732. (max r (cgreg-tos regs))))
  10733. (define (cgreg-vars regs)
  10734. (let ((m (cgreg-liveregs regs))
  10735. (v (cgreg-contents regs)))
  10736. (do ((i (- m 1) (- i 1))
  10737. (vars '()
  10738. (cons (vector-ref v i)
  10739. vars)))
  10740. ((< i 0)
  10741. vars))))
  10742. (define (cgreg-bind! regs r t)
  10743. (let ((m (cgreg-liveregs regs))
  10744. (v (cgreg-contents regs)))
  10745. (vector-set! v r t)
  10746. (if (>= r m)
  10747. (cgreg-liveregs-set! regs (+ r 1)))))
  10748. (define (cgreg-bindregs! regs vars)
  10749. (do ((m (cgreg-liveregs regs) (+ m 1))
  10750. (v (cgreg-contents regs))
  10751. (vars vars (cdr vars)))
  10752. ((null? vars)
  10753. (cgreg-liveregs-set! regs m)
  10754. regs)
  10755. (vector-set! v m (car vars))))
  10756. (define (cgreg-rename! regs alist)
  10757. (do ((i (- (cgreg-liveregs regs) 1) (- i 1))
  10758. (v (cgreg-contents regs)))
  10759. ((negative? i))
  10760. (let ((var (vector-ref v i)))
  10761. (if var
  10762. (let ((probe (assv var alist)))
  10763. (if probe
  10764. (vector-set! v i (cdr probe))))))))
  10765. (define (cgreg-release! regs r)
  10766. (let ((m (cgreg-liveregs regs))
  10767. (v (cgreg-contents regs)))
  10768. (vector-set! v r #f)
  10769. (vector-set! (cgreg-stale regs) r #t)
  10770. (if (= r (- m 1))
  10771. (do ((m r (- m 1)))
  10772. ((or (negative? m)
  10773. (vector-ref v m))
  10774. (cgreg-liveregs-set! regs (+ m 1)))))))
  10775. (define (cgreg-release-except! regs vars)
  10776. (do ((i (- (cgreg-liveregs regs) 1) (- i 1))
  10777. (v (cgreg-contents regs)))
  10778. ((negative? i))
  10779. (let ((var (vector-ref v i)))
  10780. (if (and var (not (memq var vars)))
  10781. (cgreg-release! regs i)))))
  10782. (define (cgreg-clear! regs)
  10783. (let ((m (cgreg-liveregs regs))
  10784. (v1 (cgreg-contents regs))
  10785. (v2 (cgreg-stale regs)))
  10786. (do ((r 0 (+ r 1)))
  10787. ((= r m)
  10788. (cgreg-liveregs-set! regs 0))
  10789. (vector-set! v1 r #f)
  10790. (vector-set! v2 r #t))))
  10791. (define (cgreg-lookup regs var)
  10792. (let ((m (cgreg-liveregs regs))
  10793. (v (cgreg-contents regs)))
  10794. (define (loop i)
  10795. (cond ((< i 0)
  10796. #f)
  10797. ((eq? var (vector-ref v i))
  10798. (list var 'register i '(object)))
  10799. (else
  10800. (loop (- i 1)))))
  10801. (loop (- m 1))))
  10802. (define (cgreg-lookup-reg regs r)
  10803. (let ((m (cgreg-liveregs regs))
  10804. (v (cgreg-contents regs)))
  10805. (if (<= m r)
  10806. #f
  10807. (vector-ref v r))))
  10808. (define (cgreg-join! regs1 regs2)
  10809. (let ((m1 (cgreg-liveregs regs1))
  10810. (m2 (cgreg-liveregs regs2))
  10811. (v1 (cgreg-contents regs1))
  10812. (v2 (cgreg-contents regs2))
  10813. (stale1 (cgreg-stale regs1)))
  10814. (do ((i (- (max m1 m2) 1) (- i 1)))
  10815. ((< i 0)
  10816. (cgreg-liveregs-set! regs1 (min m1 m2)))
  10817. (let ((x1 (vector-ref v1 i))
  10818. (x2 (vector-ref v2 i)))
  10819. (cond ((eq? x1 x2)
  10820. #t)
  10821. ((not x1)
  10822. (if x2
  10823. (vector-set! stale1 i #t)))
  10824. (else
  10825. (vector-set! v1 i #f)
  10826. (vector-set! stale1 i #t)))))))
  10827. ; New representation of
  10828. ; Stack-frame environments.
  10829. ; Represented as a three-element list.
  10830. ;
  10831. ; Its car is a list whose car is a list of slot entries, each
  10832. ; of the form
  10833. ; (v n instruction stale)
  10834. ; where
  10835. ; v is the name of a variable or temporary,
  10836. ; n is #f or a slot number,
  10837. ; instruction is a possibly phantom store or setstk instruction
  10838. ; that stores v into slot n, and
  10839. ; stale is a list of stale slot entries, each of the form
  10840. ; (#t . n)
  10841. ; or (#f . -1)
  10842. ; where slot n had been allocated, initialized, and released
  10843. ; before the store or setstk instruction was generated.
  10844. ; Slot entries are updated by side effect.
  10845. ;
  10846. ; Its cadr is the list of currently stale slots.
  10847. ;
  10848. ; Its caddr is a list of variables that are free in the continuation,
  10849. ; or #f if that information is unknown.
  10850. ; This information allows a direct-style code generator to know when
  10851. ; a slot becomes stale.
  10852. ;
  10853. ; Its cadddr is the size of the stack frame, which can be
  10854. ; increased but not decreased. The cdddr of the stack frame
  10855. ; environment is shared with the save instruction that
  10856. ; created the frame. What a horrible crock!
  10857. ; This stuff is private to the implementation of stack-frame
  10858. ; environments.
  10859. (define cgframe:slots car)
  10860. (define cgframe:stale cadr)
  10861. (define cgframe:livevars caddr)
  10862. (define cgframe:slot.name car)
  10863. (define cgframe:slot.offset cadr)
  10864. (define cgframe:slot.instruction caddr)
  10865. (define cgframe:slot.stale cadddr)
  10866. (define cgframe:slots-set! set-car!)
  10867. (define (cgframe:stale-set! frame stale)
  10868. (set-car! (cdr frame) stale))
  10869. (define (cgframe:livevars-set! frame vars)
  10870. (set-car! (cddr frame) vars))
  10871. (define cgframe:slot.name-set! set-car!)
  10872. (define (cgframe:slot.offset-set! entry n)
  10873. (let ((instruction (caddr entry)))
  10874. (if (or (not (eq? #f (cadr entry)))
  10875. (not (eq? $nop (car instruction))))
  10876. (error "Compiler bug: cgframe" entry)
  10877. (begin
  10878. (set-car! (cdr entry) n)
  10879. (set-car! instruction (cadr instruction))
  10880. (set-cdr! instruction (cddr instruction))
  10881. (if (eq? $setstk (car instruction))
  10882. (set-car! (cdr instruction) n)
  10883. (set-car! (cddr instruction) n))))))
  10884. ; Reserves a slot offset that was unused where the instruction
  10885. ; of the slot entry was generated, and returns that offset.
  10886. (define (cgframe:unused-slot frame entry)
  10887. (let* ((stale (cgframe:slot.stale entry))
  10888. (probe (assq #t stale)))
  10889. (if probe
  10890. (let ((n (cdr probe)))
  10891. (if (zero? n)
  10892. (cgframe-used! frame))
  10893. (set-car! probe #f)
  10894. n)
  10895. (let* ((cell (cgframe-size-cell frame))
  10896. (n (+ 1 (car cell))))
  10897. (set-car! cell n)
  10898. (if (zero? n)
  10899. (cgframe:unused-slot frame entry)
  10900. n)))))
  10901. ; Public entry points.
  10902. ; The runtime system requires slot 0 of a frame to contain
  10903. ; a closure whose code pointer contains the return address
  10904. ; of the frame.
  10905. ; To prevent slot 0 from being used for some other purpose,
  10906. ; we rely on a complex trick: Slot 0 is initially stale.
  10907. ; Gen-save! generates a store instruction for register 0,
  10908. ; with slot 0 as the only stale slot for that instruction;
  10909. ; then gen-save! clears the frame's set of stale slots, which
  10910. ; prevents other store instructions from using slot 0.
  10911. (define (cgframe-initial)
  10912. (list '()
  10913. (list (cons #t 0))
  10914. '#f
  10915. -1))
  10916. (define cgframe-livevars cgframe:livevars)
  10917. (define cgframe-livevars-set! cgframe:livevars-set!)
  10918. (define (cgframe-size-cell frame)
  10919. (cdddr frame))
  10920. (define (cgframe-size frame)
  10921. (car (cgframe-size-cell frame)))
  10922. (define (cgframe-used! frame)
  10923. (if (negative? (cgframe-size frame))
  10924. (set-car! (cgframe-size-cell frame) 0)))
  10925. ; Called only by gen-store!, gen-setstk!
  10926. (define (cgframe-bind! frame var instruction)
  10927. (cgframe:slots-set! frame
  10928. (cons (list var #f instruction (cgframe:stale frame))
  10929. (cgframe:slots frame))))
  10930. ; Called only by gen-load!, gen-stack!
  10931. (define (cgframe-touch! frame var)
  10932. (let ((entry (assq var (cgframe:slots frame))))
  10933. (if entry
  10934. (let ((n (cgframe:slot.offset entry)))
  10935. (if (eq? #f n)
  10936. (let ((n (cgframe:unused-slot frame entry)))
  10937. (cgframe:slot.offset-set! entry n))))
  10938. (error "Compiler bug: cgframe-touch!" frame var))))
  10939. (define (cgframe-rename! frame alist)
  10940. (for-each (lambda (entry)
  10941. (let ((probe (assq (cgframe:slot.name entry) alist)))
  10942. (if probe
  10943. (cgframe:slot.name-set! entry (cdr probe)))))
  10944. (cgframe:slots frame)))
  10945. (define (cgframe-release! frame var)
  10946. (let* ((slots (cgframe:slots frame))
  10947. (entry (assq var slots)))
  10948. (if entry
  10949. (begin (cgframe:slots-set! frame (remq entry slots))
  10950. (let ((n (cgframe:slot.offset entry)))
  10951. (if (and (not (eq? #f n))
  10952. (not (zero? n)))
  10953. (cgframe:stale-set!
  10954. frame
  10955. (cons (cons #t n)
  10956. (cgframe:stale frame)))))))))
  10957. (define (cgframe-release-except! frame vars)
  10958. (let loop ((slots (reverse (cgframe:slots frame)))
  10959. (newslots '())
  10960. (stale (cgframe:stale frame)))
  10961. (if (null? slots)
  10962. (begin (cgframe:slots-set! frame newslots)
  10963. (cgframe:stale-set! frame stale))
  10964. (let ((slot (car slots)))
  10965. (if (memq (cgframe:slot.name slot) vars)
  10966. (loop (cdr slots)
  10967. (cons slot newslots)
  10968. stale)
  10969. (let ((n (cgframe:slot.offset slot)))
  10970. (cond ((eq? n #f)
  10971. (loop (cdr slots)
  10972. newslots
  10973. stale))
  10974. ((zero? n)
  10975. (loop (cdr slots)
  10976. (cons slot newslots)
  10977. stale))
  10978. (else
  10979. (loop (cdr slots)
  10980. newslots
  10981. (cons (cons #t n) stale))))))))))
  10982. (define (cgframe-lookup frame var)
  10983. (let ((entry (assq var (cgframe:slots frame))))
  10984. (if entry
  10985. (let ((n (cgframe:slot.offset entry)))
  10986. (if (eq? #f n)
  10987. (cgframe-touch! frame var))
  10988. (list var 'frame (cgframe:slot.offset entry) '(object)))
  10989. #f)))
  10990. (define (cgframe-spilled? frame var)
  10991. (let ((entry (assq var (cgframe:slots frame))))
  10992. (if entry
  10993. (let ((n (cgframe:slot.offset entry)))
  10994. (not (eq? #f n)))
  10995. #f)))
  10996. ; For a conditional expression, the then and else parts must be
  10997. ; evaluated using separate copies of the frame environment,
  10998. ; and those copies must be resolved at the join point. The
  10999. ; nature of the resolution depends upon whether the conditional
  11000. ; expression is in a tail position.
  11001. ;
  11002. ; Critical invariant:
  11003. ; Any store instructions that are generated within either arm of the
  11004. ; conditional involve variables and temporaries that are local to the
  11005. ; conditional.
  11006. ;
  11007. ; If the conditional expression is in a tail position, then a slot
  11008. ; that is stale after the test can be allocated independently by the
  11009. ; two arms of the conditional. If the conditional expression is in a
  11010. ; non-tail position, then the slot can be allocated independently
  11011. ; provided it is not a candidate destination for any previous emitted
  11012. ; store instruction.
  11013. (define (cgframe-copy frame)
  11014. (cons (car frame)
  11015. (cons (cadr frame)
  11016. (cons (caddr frame)
  11017. (cdddr frame)))))
  11018. (define (cgframe-update-stale! frame)
  11019. (let* ((n (cgframe-size frame))
  11020. (v (make-vector (+ 1 n) #t))
  11021. (stale (cgframe:stale frame)))
  11022. (for-each (lambda (x)
  11023. (if (car x)
  11024. (let ((i (cdr x)))
  11025. (if (<= i n)
  11026. (vector-set! v i #f)))))
  11027. stale)
  11028. (for-each (lambda (slot)
  11029. (let ((offset (cgframe:slot.offset slot)))
  11030. (if offset
  11031. (vector-set! v offset #f)
  11032. (for-each (lambda (stale)
  11033. (if (car stale)
  11034. (let ((i (cdr stale)))
  11035. (if (< i n)
  11036. (vector-set! v i #f)))))
  11037. (cgframe:slot.stale slot)))))
  11038. (cgframe:slots frame))
  11039. (do ((i n (- i 1))
  11040. (stale (filter car stale)
  11041. (if (vector-ref v i)
  11042. (cons (cons #t i) stale)
  11043. stale)))
  11044. ((<= i 0)
  11045. (cgframe:stale-set! frame stale)))))
  11046. (define (cgframe-join! frame1 frame2)
  11047. (let* ((slots1 (cgframe:slots frame1))
  11048. (slots2 (cgframe:slots frame2))
  11049. (slots (intersection slots1 slots2))
  11050. (deadslots (append (difference slots1 slots)
  11051. (difference slots2 slots)))
  11052. (deadoffsets (make-set
  11053. (filter (lambda (x) (not (eq? x #f)))
  11054. (map cgframe:slot.offset deadslots))))
  11055. (stale1 (cgframe:stale frame1))
  11056. (stale2 (cgframe:stale frame2))
  11057. (stale (intersection stale1 stale2))
  11058. (stale (append (map (lambda (n) (cons #t n))
  11059. deadoffsets)
  11060. stale)))
  11061. (cgframe:slots-set! frame1 slots)
  11062. (cgframe:stale-set! frame1 stale)))
  11063. ; Environments.
  11064. ;
  11065. ; Each identifier has one of the following kinds of entry.
  11066. ;
  11067. ; (<name> register <number> (object))
  11068. ; (<name> frame <slot> (object))
  11069. ; (<name> lexical <rib> <offset> (object))
  11070. ; (<name> procedure <rib> <label> (object))
  11071. ; (<name> integrable <arity> <op> <imm> (object))
  11072. ; (<name> global (object))
  11073. ;
  11074. ; Implementation.
  11075. ;
  11076. ; An environment is represented as a list of the form
  11077. ;
  11078. ; ((<entry> ...) ; lexical rib
  11079. ; ...)
  11080. ;
  11081. ; where each <entry> has one of the forms
  11082. ;
  11083. ; (<name> lexical <offset> (object))
  11084. ; (<name> procedure <rib> <label> (object))
  11085. ; (<name> integrable <arity> <op> <imm> (object))
  11086. (define entry.name car)
  11087. (define entry.kind cadr)
  11088. (define entry.rib caddr)
  11089. (define entry.offset cadddr)
  11090. (define entry.label cadddr)
  11091. (define entry.regnum caddr)
  11092. (define entry.slotnum caddr)
  11093. (define entry.arity caddr)
  11094. (define entry.op cadddr)
  11095. (define (entry.imm entry) (car (cddddr entry)))
  11096. (define (cgenv-initial integrable)
  11097. (list (map (lambda (x)
  11098. (list (car x)
  11099. 'integrable
  11100. (cadr x)
  11101. (caddr x)
  11102. (cadddr x)
  11103. '(object)))
  11104. integrable)))
  11105. (define (cgenv-lookup env id)
  11106. (define (loop ribs m)
  11107. (if (null? ribs)
  11108. (cons id '(global (object)))
  11109. (let ((x (assq id (car ribs))))
  11110. (if x
  11111. (case (cadr x)
  11112. ((lexical)
  11113. (cons id
  11114. (cons (cadr x)
  11115. (cons m (cddr x)))))
  11116. ((procedure)
  11117. (cons id
  11118. (cons (cadr x)
  11119. (cons m (cddr x)))))
  11120. ((integrable)
  11121. (if (integrate-usual-procedures)
  11122. x
  11123. (loop '() m)))
  11124. (else ???))
  11125. (loop (cdr ribs) (+ m 1))))))
  11126. (loop env 0))
  11127. (define (cgenv-extend env vars procs)
  11128. (cons (do ((n 0 (+ n 1))
  11129. (vars vars (cdr vars))
  11130. (rib (map (lambda (id)
  11131. (list id 'procedure (make-label) '(object)))
  11132. procs)
  11133. (cons (list (car vars) 'lexical n '(object)) rib)))
  11134. ((null? vars) rib))
  11135. env))
  11136. (define (cgenv-bindprocs env procs)
  11137. (cons (append (map (lambda (id)
  11138. (list id 'procedure (make-label) '(object)))
  11139. procs)
  11140. (car env))
  11141. (cdr env)))
  11142. (define (var-lookup var regs frame env)
  11143. (or (cgreg-lookup regs var)
  11144. (cgframe-lookup frame var)
  11145. (cgenv-lookup env var)))
  11146. ; Compositions.
  11147. (define compile
  11148. (lambda (x)
  11149. (pass4 (pass3 (pass2 (pass1 x))) $usual-integrable-procedures$)))
  11150. (define compile-block
  11151. (lambda (x)
  11152. (pass4 (pass3 (pass2 (pass1-block x))) $usual-integrable-procedures$)))
  11153. ; For testing.
  11154. (define foo
  11155. (lambda (x)
  11156. (pretty-print (compile x))))
  11157. ; Find the smallest number of registers such that
  11158. ; adding more registers does not affect the code
  11159. ; generated for x (from 4 to 32 registers).
  11160. (define (minregs x)
  11161. (define (defregs R)
  11162. (set! *nregs* R)
  11163. (set! *lastreg* (- *nregs* 1))
  11164. (set! *fullregs* (quotient *nregs* 2)))
  11165. (defregs 32)
  11166. (let ((code (assemble (compile x))))
  11167. (define (binary-search m1 m2)
  11168. (if (= (+ m1 1) m2)
  11169. m2
  11170. (let ((midpt (quotient (+ m1 m2) 2)))
  11171. (defregs midpt)
  11172. (if (equal? code (assemble (compile x)))
  11173. (binary-search m1 midpt)
  11174. (binary-search midpt m2)))))
  11175. (defregs 4)
  11176. (let ((newcode (assemble (compile x))))
  11177. (if (equal? code newcode)
  11178. 4
  11179. (binary-search 4 32)))))
  11180. ; Minimums:
  11181. ; browse 10
  11182. ; triangle 5
  11183. ; traverse 10
  11184. ; destruct 6
  11185. ; puzzle 8,8,10,7
  11186. ; tak 6
  11187. ; fft 28 (changing the named lets to macros didn't matter)
  11188. ; Copyright 1991 William Clinger
  11189. ;
  11190. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  11191. ;
  11192. ; 7 June 1999.
  11193. ;
  11194. ; Fourth pass of the Twobit compiler:
  11195. ; code generation for the MacScheme machine.
  11196. ;
  11197. ; This pass operates on input expressions described by the
  11198. ; following grammar and the invariants that follow it.
  11199. ;
  11200. ; "X ..." means zero or more occurrences of X.
  11201. ;
  11202. ; L --> (lambda (I_1 ...)
  11203. ; (begin D ...)
  11204. ; (quote (R F G <decls> <doc>)
  11205. ; E)
  11206. ; | (lambda (I_1 ... . I_rest)
  11207. ; (begin D ...)
  11208. ; (quote (R F G <decls> <doc>))
  11209. ; E)
  11210. ; D --> (define I L)
  11211. ; E --> (quote K) ; constants
  11212. ; | (begin I) ; variable references
  11213. ; | L ; lambda expressions
  11214. ; | (E0 E1 ...) ; calls
  11215. ; | (set! I E) ; assignments
  11216. ; | (if E0 E1 E2) ; conditionals
  11217. ; | (begin E0 E1 E2 ...) ; sequential expressions
  11218. ; I --> <identifier>
  11219. ;
  11220. ; R --> ((I <references> <assignments> <calls>) ...)
  11221. ; F --> (I ...)
  11222. ; G --> (I ...)
  11223. ;
  11224. ; Invariants that hold for the input
  11225. ; * There are no assignments except to global variables.
  11226. ; * If I is declared by an internal definition, then the right hand
  11227. ; side of the internal definition is a lambda expression and I
  11228. ; is referenced only in the procedure position of a call.
  11229. ; * Every procedure defined by an internal definition takes a
  11230. ; fixed number of arguments.
  11231. ; * Every call to a procedure defined by an internal definition
  11232. ; passes the correct number of arguments.
  11233. ; * For each lambda expression, the associated F is a list of all
  11234. ; the identifiers that occur free in the body of that lambda
  11235. ; expression, and possibly a few extra identifiers that were
  11236. ; once free but have been removed by optimization.
  11237. ; * For each lambda expression, the associated G is a subset of F
  11238. ; that contains every identifier that occurs free within some
  11239. ; inner lambda expression that escapes, and possibly a few that
  11240. ; don't. (Assignment-elimination does not calculate G exactly.)
  11241. ; * Variables named IGNORED are neither referenced nor assigned.
  11242. ; * Any lambda expression that is declared to be in A-normal form
  11243. ; really is in A-normal form.
  11244. ;
  11245. ;
  11246. ; Stack frames are created by "save" instructions.
  11247. ; A save instruction is generated
  11248. ;
  11249. ; * at the beginning of each lambda body
  11250. ; * at the beginning of the code for each arm of a conditional,
  11251. ; provided:
  11252. ; the conditional is in a tail position
  11253. ; the frames that were allocated by the save instructions
  11254. ; that dominate the arms of the conditional have not been
  11255. ; used (those save instructions will be eliminated during
  11256. ; assembly)
  11257. ;
  11258. ; The operand of a save instruction, and of its matching pop instructions,
  11259. ; increases automatically as frame slots are allocated.
  11260. ;
  11261. ; The code generated to return from a procedure is
  11262. ;
  11263. ; pop n
  11264. ; return
  11265. ;
  11266. ; The code generated for a tail call is
  11267. ;
  11268. ; pop n
  11269. ; invoke ...
  11270. ;
  11271. ; Invariant: When the code generator reserves an argument register
  11272. ; to hold a value, that value is named, and is stored into the current
  11273. ; stack frame. These store instructions are eliminated during assembly
  11274. ; unless there is a matching load instruction. If all of the instructions
  11275. ; that store into a stack frame are eliminated, then the stack frame
  11276. ; itself is eliminated.
  11277. ; Exception: An argument register may be used without naming or storing
  11278. ; its value provided the register is not in use and no expressions are
  11279. ; evaluated while it contains the unnamed and unstored value.
  11280. (define (pass4 exp integrable)
  11281. (init-labels)
  11282. (init-temps)
  11283. (let ((output (make-assembly-stream))
  11284. (frame (cgframe-initial))
  11285. (regs (cgreg-initial))
  11286. (t0 (newtemp)))
  11287. (assembly-stream-info! output (make-hashtable equal-hash assoc))
  11288. (cgreg-bind! regs 0 t0)
  11289. (gen-save! output frame t0)
  11290. (cg0 output
  11291. exp
  11292. 'result
  11293. regs
  11294. frame
  11295. (cgenv-initial integrable)
  11296. #t)
  11297. (pass4-code output)))
  11298. (define (pass4-code output)
  11299. (hashtable-for-each (lambda (situation label)
  11300. (cg-trap output situation label))
  11301. (assembly-stream-info output))
  11302. (assembly-stream-code output))
  11303. ; Given:
  11304. ; an assembly stream into which instructions should be emitted
  11305. ; an expression
  11306. ; the target register
  11307. ; ('result, a register number, or '#f; tail position implies 'result)
  11308. ; a register environment [cgreg]
  11309. ; a stack-frame environment [cgframe]
  11310. ; a compile-time environment [cgenv]
  11311. ; a flag indicating whether the expression is in tail position
  11312. ; Returns:
  11313. ; the target register ('result or a register number)
  11314. ; Side effects:
  11315. ; may change the register and stack-frame environments
  11316. ; may increase the size of the stack frame, which changes previously
  11317. ; emitted instructions
  11318. ; writes instructions to the assembly stream
  11319. (define (cg0 output exp target regs frame env tail?)
  11320. (case (car exp)
  11321. ((quote) (gen! output $const (constant.value exp))
  11322. (if tail?
  11323. (begin (gen-pop! output frame)
  11324. (gen! output $return)
  11325. 'result)
  11326. (cg-move output frame regs 'result target)))
  11327. ((lambda) (cg-lambda output exp regs frame env)
  11328. (if tail?
  11329. (begin (gen-pop! output frame)
  11330. (gen! output $return)
  11331. 'result)
  11332. (cg-move output frame regs 'result target)))
  11333. ((set!) (cg0 output (assignment.rhs exp) 'result regs frame env #f)
  11334. (cg-assignment-result output exp target regs frame env tail?))
  11335. ((if) (cg-if output exp target regs frame env tail?))
  11336. ((begin) (if (variable? exp)
  11337. (cg-variable output exp target regs frame env tail?)
  11338. (cg-sequential output exp target regs frame env tail?)))
  11339. (else (cg-call output exp target regs frame env tail?))))
  11340. ; Lambda expressions that evaluate to closures.
  11341. ; This is hard because the MacScheme machine's lambda instruction
  11342. ; closes over the values that are in argument registers 0 through r
  11343. ; (where r can be larger than *nregs*).
  11344. ; The set of free variables is calculated and then sorted to minimize
  11345. ; register shuffling.
  11346. ;
  11347. ; Returns: nothing.
  11348. (define (cg-lambda output exp regs frame env)
  11349. (let* ((args (lambda.args exp))
  11350. (vars (make-null-terminated args))
  11351. (free (difference (lambda.F exp) vars))
  11352. (free (cg-sort-vars free regs frame env))
  11353. (newenv (cgenv-extend env (cons #t free) '()))
  11354. (newoutput (make-assembly-stream)))
  11355. (assembly-stream-info! newoutput (make-hashtable equal-hash assoc))
  11356. (gen! newoutput $.proc)
  11357. (if (list? args)
  11358. (gen! newoutput $args= (length args))
  11359. (gen! newoutput $args>= (- (length vars) 1)))
  11360. (cg-known-lambda newoutput exp newenv)
  11361. (cg-eval-vars output free regs frame env)
  11362. ; FIXME
  11363. '
  11364. (if (not (ignore-space-leaks))
  11365. ; FIXME: Is this the right constant?
  11366. (begin (gen! output $const #f)
  11367. (gen! output $setreg 0)))
  11368. (gen! output
  11369. $lambda
  11370. (pass4-code newoutput)
  11371. (length free)
  11372. (lambda.doc exp))
  11373. ; FIXME
  11374. '
  11375. (if (not (ignore-space-leaks))
  11376. ; FIXME: This load forces a stack frame to be allocated.
  11377. (gen-load! output frame 0 (cgreg-lookup-reg regs 0)))))
  11378. ; Given a list of free variables, filters out the ones that
  11379. ; need to be copied into a closure, and sorts them into an order
  11380. ; that reduces register shuffling. Returns a sorted version of
  11381. ; the list in which the first element (element 0) should go
  11382. ; into register 1, the second into register 2, and so on.
  11383. (define (cg-sort-vars free regs frame env)
  11384. (let* ((free (filter (lambda (var)
  11385. (case (entry.kind
  11386. (var-lookup var regs frame env))
  11387. ((register frame)
  11388. #t)
  11389. ((lexical)
  11390. (not (ignore-space-leaks)))
  11391. (else #f)))
  11392. free))
  11393. (n (length free))
  11394. (m (min n (- *nregs* 1)))
  11395. (vec (make-vector m #f)))
  11396. (define (loop1 free free-notregister)
  11397. (if (null? free)
  11398. (loop2 0 free-notregister)
  11399. (let* ((var (car free))
  11400. (entry (cgreg-lookup regs var)))
  11401. (if entry
  11402. (let ((r (entry.regnum entry)))
  11403. (if (<= r n)
  11404. (begin (vector-set! vec (- r 1) var)
  11405. (loop1 (cdr free)
  11406. free-notregister))
  11407. (loop1 (cdr free)
  11408. (cons var free-notregister))))
  11409. (loop1 (cdr free)
  11410. (cons var free-notregister))))))
  11411. (define (loop2 i free)
  11412. (cond ((null? free)
  11413. (vector->list vec))
  11414. ((= i m)
  11415. (append (vector->list vec) free))
  11416. ((vector-ref vec i)
  11417. (loop2 (+ i 1) free))
  11418. (else
  11419. (vector-set! vec i (car free))
  11420. (loop2 (+ i 1) (cdr free)))))
  11421. (loop1 free '())))
  11422. ; Fetches the given list of free variables into the corresponding
  11423. ; registers in preparation for a $lambda or $lexes instruction.
  11424. (define (cg-eval-vars output free regs frame env)
  11425. (let ((n (length free))
  11426. (R-1 (- *nregs* 1)))
  11427. (if (>= n R-1)
  11428. (begin (gen! output $const '())
  11429. (gen! output $setreg R-1)
  11430. (cgreg-release! regs R-1)))
  11431. (do ((r n (- r 1))
  11432. (vars (reverse free) (cdr vars)))
  11433. ((zero? r))
  11434. (let* ((v (car vars))
  11435. (entry (var-lookup v regs frame env)))
  11436. (case (entry.kind entry)
  11437. ((register)
  11438. (let ((r1 (entry.regnum entry)))
  11439. (if (not (eqv? r r1))
  11440. (if (< r R-1)
  11441. (begin (gen! output $movereg r1 r)
  11442. (cgreg-bind! regs r v))
  11443. (gen! output $reg r1 v)))))
  11444. ((frame)
  11445. (if (< r R-1)
  11446. (begin (gen-load! output frame r v)
  11447. (cgreg-bind! regs r v))
  11448. (gen-stack! output frame v)))
  11449. ((lexical)
  11450. (gen! output $lexical
  11451. (entry.rib entry)
  11452. (entry.offset entry)
  11453. v)
  11454. (if (< r R-1)
  11455. (begin (gen! output $setreg r)
  11456. (cgreg-bind! regs r v)
  11457. (gen-store! output frame r v))))
  11458. (else
  11459. (error "Bug in cg-close-lambda")))
  11460. (if (>= r R-1)
  11461. (begin (gen! output $op2 $cons R-1)
  11462. (gen! output $setreg R-1)))))))
  11463. ; Lambda expressions that appear on the rhs of a definition are
  11464. ; compiled here. They don't need an args= instruction at their head.
  11465. ;
  11466. ; Returns: nothing.
  11467. (define (cg-known-lambda output exp env)
  11468. (let* ((vars (make-null-terminated (lambda.args exp)))
  11469. (regs (cgreg-initial))
  11470. (frame (cgframe-initial))
  11471. (t0 (newtemp)))
  11472. (if (member A-normal-form-declaration (lambda.decls exp))
  11473. (cgframe-livevars-set! frame '()))
  11474. (cgreg-bind! regs 0 t0)
  11475. (gen-save! output frame t0)
  11476. (do ((r 1 (+ r 1))
  11477. (vars vars (cdr vars)))
  11478. ((or (null? vars)
  11479. (= r *lastreg*))
  11480. (if (not (null? vars))
  11481. (begin (gen! output $movereg *lastreg* 1)
  11482. (cgreg-release! regs 1)
  11483. (do ((vars vars (cdr vars)))
  11484. ((null? vars))
  11485. (gen! output $reg 1)
  11486. (gen! output $op1 $car:pair)
  11487. (gen-setstk! output frame (car vars))
  11488. (gen! output $reg 1)
  11489. (gen! output $op1 $cdr:pair)
  11490. (gen! output $setreg 1)))))
  11491. (cgreg-bind! regs r (car vars))
  11492. (gen-store! output frame r (car vars)))
  11493. (cg-body output
  11494. exp
  11495. 'result
  11496. regs
  11497. frame
  11498. env
  11499. #t)))
  11500. ; Compiles a let or lambda body.
  11501. ; The arguments of the lambda expression L are already in
  11502. ; registers or the stack frame, as specified by regs and frame.
  11503. ;
  11504. ; The problem here is that the free variables of an internal
  11505. ; definition must be in a heap-allocated environment, so any
  11506. ; such variables in registers must be copied to the heap.
  11507. ;
  11508. ; Returns: destination register.
  11509. (define (cg-body output L target regs frame env tail?)
  11510. (let* ((exp (lambda.body L))
  11511. (defs (lambda.defs L))
  11512. (free (apply-union
  11513. (map (lambda (def)
  11514. (let ((L (def.rhs def)))
  11515. (difference (lambda.F L)
  11516. (lambda.args L))))
  11517. defs))))
  11518. (cond ((or (null? defs) (constant? exp) (variable? exp))
  11519. (cg0 output exp target regs frame env tail?))
  11520. ((lambda? exp)
  11521. (let* ((free (cg-sort-vars
  11522. (union free
  11523. (difference
  11524. (lambda.F exp)
  11525. (make-null-terminated (lambda.args exp))))
  11526. regs frame env))
  11527. (newenv1 (cgenv-extend env
  11528. (cons #t free)
  11529. (map def.lhs defs)))
  11530. (args (lambda.args exp))
  11531. (vars (make-null-terminated args))
  11532. (newoutput (make-assembly-stream)))
  11533. (assembly-stream-info! newoutput (make-hashtable equal-hash assoc))
  11534. (gen! newoutput $.proc)
  11535. (if (list? args)
  11536. (gen! newoutput $args= (length args))
  11537. (gen! newoutput $args>= (- (length vars) 1)))
  11538. (cg-known-lambda newoutput exp newenv1)
  11539. (cg-defs newoutput defs newenv1)
  11540. (cg-eval-vars output free regs frame env)
  11541. (gen! output
  11542. $lambda
  11543. (pass4-code newoutput)
  11544. (length free)
  11545. (lambda.doc exp))
  11546. (if tail?
  11547. (begin (gen-pop! output frame)
  11548. (gen! output $return)
  11549. 'result)
  11550. (cg-move output frame regs 'result target))))
  11551. ((every? (lambda (def)
  11552. (every? (lambda (v)
  11553. (case (entry.kind
  11554. (var-lookup v regs frame env))
  11555. ((register frame) #f)
  11556. (else #t)))
  11557. (let ((Ldef (def.rhs def)))
  11558. (difference (lambda.F Ldef)
  11559. (lambda.args Ldef)))))
  11560. defs)
  11561. (let* ((newenv (cgenv-bindprocs env (map def.lhs defs)))
  11562. (L (make-label))
  11563. (r (cg0 output exp target regs frame newenv tail?)))
  11564. (if (not tail?)
  11565. (gen! output $skip L (cgreg-live regs r)))
  11566. (cg-defs output defs newenv)
  11567. (if (not tail?)
  11568. (gen! output $.label L))
  11569. r))
  11570. (else
  11571. (let ((free (cg-sort-vars free regs frame env)))
  11572. (cg-eval-vars output free regs frame env)
  11573. ; FIXME: Have to restore it too!
  11574. '
  11575. (if (not (ignore-space-leaks))
  11576. ; FIXME: Is this constant the right one?
  11577. (begin (gen! output $const #f)
  11578. (gen! output $setreg 0)))
  11579. (let ((t0 (cgreg-lookup-reg regs 0))
  11580. (t1 (newtemp))
  11581. (newenv (cgenv-extend env
  11582. (cons #t free)
  11583. (map def.lhs defs)))
  11584. (L (make-label)))
  11585. (gen! output $lexes (length free) free)
  11586. (gen! output $setreg 0)
  11587. (cgreg-bind! regs 0 t1)
  11588. (if tail?
  11589. (begin (cgframe-release! frame t0)
  11590. (gen-store! output frame 0 t1)
  11591. (cg0 output exp 'result regs frame newenv #t)
  11592. (cg-defs output defs newenv)
  11593. 'result)
  11594. (begin (gen-store! output frame 0 t1)
  11595. (cg0 output exp 'result regs frame newenv #f)
  11596. (gen! output $skip L (cgreg-tos regs))
  11597. (cg-defs output defs newenv)
  11598. (gen! output $.label L)
  11599. (gen-load! output frame 0 t0)
  11600. (cgreg-bind! regs 0 t0)
  11601. (cgframe-release! frame t1)
  11602. (cg-move output frame regs 'result target)))))))))
  11603. (define (cg-defs output defs env)
  11604. (for-each (lambda (def)
  11605. (gen! output $.align 4)
  11606. (gen! output $.label
  11607. (entry.label
  11608. (cgenv-lookup env (def.lhs def))))
  11609. (gen! output $.proc)
  11610. (gen! output $.proc-doc (lambda.doc (def.rhs def)))
  11611. (cg-known-lambda output
  11612. (def.rhs def)
  11613. env))
  11614. defs))
  11615. ; The right hand side has already been evaluated into the result register.
  11616. (define (cg-assignment-result output exp target regs frame env tail?)
  11617. (gen! output $setglbl (assignment.lhs exp))
  11618. (if tail?
  11619. (begin (gen-pop! output frame)
  11620. (gen! output $return)
  11621. 'result)
  11622. (cg-move output frame regs 'result target)))
  11623. (define (cg-if output exp target regs frame env tail?)
  11624. ; The test can be a constant, because it is awkward
  11625. ; to remove constant tests from an A-normal form.
  11626. (if (constant? (if.test exp))
  11627. (cg0 output
  11628. (if (constant.value (if.test exp))
  11629. (if.then exp)
  11630. (if.else exp))
  11631. target regs frame env tail?)
  11632. (begin
  11633. (cg0 output (if.test exp) 'result regs frame env #f)
  11634. (cg-if-result output exp target regs frame env tail?))))
  11635. ; The test expression has already been evaluated into the result register.
  11636. (define (cg-if-result output exp target regs frame env tail?)
  11637. (let ((L1 (make-label))
  11638. (L2 (make-label)))
  11639. (gen! output $branchf L1 (cgreg-tos regs))
  11640. (let* ((regs2 (cgreg-copy regs))
  11641. (frame1 (if (and tail?
  11642. (negative? (cgframe-size frame)))
  11643. (cgframe-initial)
  11644. frame))
  11645. (frame2 (if (eq? frame frame1)
  11646. (cgframe-copy frame1)
  11647. (cgframe-initial)))
  11648. (t0 (cgreg-lookup-reg regs 0)))
  11649. (if (not (eq? frame frame1))
  11650. (let ((live (cgframe-livevars frame)))
  11651. (cgframe-livevars-set! frame1 live)
  11652. (cgframe-livevars-set! frame2 live)
  11653. (gen-save! output frame1 t0)
  11654. (cg-saveregs output regs frame1)))
  11655. (let ((r (cg0 output (if.then exp) target regs frame1 env tail?)))
  11656. (if (not tail?)
  11657. (gen! output $skip L2 (cgreg-live regs r)))
  11658. (gen! output $.label L1)
  11659. (if (not (eq? frame frame1))
  11660. (begin (gen-save! output frame2 t0)
  11661. (cg-saveregs output regs2 frame2))
  11662. (cgframe-update-stale! frame2))
  11663. (cg0 output (if.else exp) r regs2 frame2 env tail?)
  11664. (if (not tail?)
  11665. (begin (gen! output $.label L2)
  11666. (cgreg-join! regs regs2)
  11667. (cgframe-join! frame1 frame2)))
  11668. (if (and (not target)
  11669. (not (eq? r 'result))
  11670. (not (cgreg-lookup-reg regs r)))
  11671. (cg-move output frame regs r 'result)
  11672. r)))))
  11673. (define (cg-variable output exp target regs frame env tail?)
  11674. (define (return id)
  11675. (if tail?
  11676. (begin (gen-pop! output frame)
  11677. (gen! output $return)
  11678. 'result)
  11679. (if (and target
  11680. (not (eq? 'result target)))
  11681. (begin (gen! output $setreg target)
  11682. (cgreg-bind! regs target id)
  11683. (gen-store! output frame target id)
  11684. target)
  11685. 'result)))
  11686. ; Same as return, but doesn't emit a store instruction.
  11687. (define (return-nostore id)
  11688. (if tail?
  11689. (begin (gen-pop! output frame)
  11690. (gen! output $return)
  11691. 'result)
  11692. (if (and target
  11693. (not (eq? 'result target)))
  11694. (begin (gen! output $setreg target)
  11695. (cgreg-bind! regs target id)
  11696. target)
  11697. 'result)))
  11698. (let* ((id (variable.name exp))
  11699. (entry (var-lookup id regs frame env)))
  11700. (case (entry.kind entry)
  11701. ((global integrable)
  11702. (gen! output $global id)
  11703. (return (newtemp)))
  11704. ((lexical)
  11705. (let ((m (entry.rib entry))
  11706. (n (entry.offset entry)))
  11707. (gen! output $lexical m n id)
  11708. (if (or (zero? m)
  11709. (negative? (cgframe-size frame)))
  11710. (return-nostore id)
  11711. (return id))))
  11712. ((procedure) (error "Bug in cg-variable" exp))
  11713. ((register)
  11714. (let ((r (entry.regnum entry)))
  11715. (if (or tail?
  11716. (and target (not (eqv? target r))))
  11717. (begin (gen! output $reg (entry.regnum entry) id)
  11718. (return-nostore id))
  11719. r)))
  11720. ((frame)
  11721. (cond ((eq? target 'result)
  11722. (gen-stack! output frame id)
  11723. (return id))
  11724. (target
  11725. ; Must be non-tail.
  11726. (gen-load! output frame target id)
  11727. (cgreg-bind! regs target id)
  11728. target)
  11729. (else
  11730. ; Must be non-tail.
  11731. (let ((r (choose-register regs frame)))
  11732. (gen-load! output frame r id)
  11733. (cgreg-bind! regs r id)
  11734. r))))
  11735. (else (error "Bug in cg-variable" exp)))))
  11736. (define (cg-sequential output exp target regs frame env tail?)
  11737. (cg-sequential-loop output (begin.exprs exp) target regs frame env tail?))
  11738. (define (cg-sequential-loop output exprs target regs frame env tail?)
  11739. (cond ((null? exprs)
  11740. (gen! output $const unspecified)
  11741. (if tail?
  11742. (begin (gen-pop! output frame)
  11743. (gen! output $return)
  11744. 'result)
  11745. (cg-move output frame regs 'result target)))
  11746. ((null? (cdr exprs))
  11747. (cg0 output (car exprs) target regs frame env tail?))
  11748. (else (cg0 output (car exprs) #f regs frame env #f)
  11749. (cg-sequential-loop output
  11750. (cdr exprs)
  11751. target regs frame env tail?))))
  11752. (define (cg-saveregs output regs frame)
  11753. (do ((i 1 (+ i 1))
  11754. (vars (cdr (cgreg-vars regs)) (cdr vars)))
  11755. ((null? vars))
  11756. (let ((t (car vars)))
  11757. (if t
  11758. (gen-store! output frame i t)))))
  11759. (define (cg-move output frame regs src dst)
  11760. (define (bind dst)
  11761. (let ((temp (newtemp)))
  11762. (cgreg-bind! regs dst temp)
  11763. (gen-store! output frame dst temp)
  11764. dst))
  11765. (cond ((not dst)
  11766. src)
  11767. ((eqv? src dst)
  11768. dst)
  11769. ((eq? dst 'result)
  11770. (gen! output $reg src)
  11771. dst)
  11772. ((eq? src 'result)
  11773. (gen! output $setreg dst)
  11774. (bind dst))
  11775. ((and (not (zero? src))
  11776. (not (zero? dst)))
  11777. (gen! output $movereg src dst)
  11778. (bind dst))
  11779. (else
  11780. (gen! output $reg src)
  11781. (gen! output $setreg dst)
  11782. (bind dst))))
  11783. ; On-the-fly register allocator.
  11784. ; Tries to allocate:
  11785. ; a hardware register that isn't being used
  11786. ; a hardware register whose contents have already been spilled
  11787. ; a software register that isn't being used, unless a stack
  11788. ; frame has already been created, in which case it is better to use
  11789. ; a hardware register that is in use and hasn't yet been spilled
  11790. ;
  11791. ; All else equal, it is better to allocate a higher-numbered register
  11792. ; because the lower-numbered registers are targets when arguments
  11793. ; are being evaluated.
  11794. ;
  11795. ; Invariant: Every register that is returned by this allocator
  11796. ; is either not in use or has been spilled.
  11797. (define (choose-register regs frame)
  11798. (car (choose-registers regs frame 1)))
  11799. (define (choose-registers regs frame n)
  11800. ; Find unused hardware registers.
  11801. (define (loop1 i n good)
  11802. (cond ((zero? n)
  11803. good)
  11804. ((zero? i)
  11805. (if (negative? (cgframe-size frame))
  11806. (hardcase)
  11807. (loop2 (- *nhwregs* 1) n good)))
  11808. (else
  11809. (if (cgreg-lookup-reg regs i)
  11810. (loop1 (- i 1) n good)
  11811. (loop1 (- i 1)
  11812. (- n 1)
  11813. (cons i good))))))
  11814. ; Find already spilled hardware registers.
  11815. (define (loop2 i n good)
  11816. (cond ((zero? n)
  11817. good)
  11818. ((zero? i)
  11819. (hardcase))
  11820. (else
  11821. (let ((t (cgreg-lookup-reg regs i)))
  11822. (if (and t (cgframe-spilled? frame t))
  11823. (loop2 (- i 1)
  11824. (- n 1)
  11825. (cons i good))
  11826. (loop2 (- i 1) n good))))))
  11827. ; This is ridiculous.
  11828. ; Fortunately the correctness of the compiler is independent
  11829. ; of the predicate used for this sort.
  11830. (define (hardcase)
  11831. (let* ((frame-exists? (not (negative? (cgframe-size frame))))
  11832. (stufftosort
  11833. (map (lambda (r)
  11834. (let* ((t (cgreg-lookup-reg regs r))
  11835. (spilled?
  11836. (and t
  11837. (cgframe-spilled? frame t))))
  11838. (list r t spilled?)))
  11839. (cdr (iota *nregs*))))
  11840. (registers
  11841. (twobit-sort
  11842. (lambda (x1 x2)
  11843. (let ((r1 (car x1))
  11844. (r2 (car x2))
  11845. (t1 (cadr x1))
  11846. (t2 (cadr x2)))
  11847. (cond ((< r1 *nhwregs*)
  11848. (cond ((not t1) #t)
  11849. ((< r2 *nhwregs*)
  11850. (cond ((not t2) #f)
  11851. ((caddr x1) #t)
  11852. ((caddr x2) #f)
  11853. (else #t)))
  11854. (frame-exists? #t)
  11855. (t2 #t)
  11856. (else #f)))
  11857. ((< r2 *nhwregs*)
  11858. (cond (frame-exists? #f)
  11859. (t1 #f)
  11860. (t2 #t)
  11861. (else #f)))
  11862. (t1
  11863. (if (and (caddr x1)
  11864. t2
  11865. (not (caddr x2)))
  11866. #t
  11867. #f))
  11868. (else #t))))
  11869. stufftosort)))
  11870. ; FIXME: What was this for?
  11871. '
  11872. (for-each (lambda (register)
  11873. (let ((t (cadr register))
  11874. (spilled? (caddr register)))
  11875. (if (and t (not spilled?))
  11876. (cgframe-touch! frame t))))
  11877. registers)
  11878. (do ((sorted (map car registers) (cdr sorted))
  11879. (rs '() (cons (car sorted) rs))
  11880. (n n (- n 1)))
  11881. ((zero? n)
  11882. (reverse rs)))))
  11883. (if (< n *nregs*)
  11884. (loop1 (- *nhwregs* 1) n '())
  11885. (error (string-append "Compiler bug: can't allocate "
  11886. (number->string n)
  11887. " registers on this target."))))
  11888. ; Copyright 1991 William Clinger
  11889. ;
  11890. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  11891. ;
  11892. ; 21 May 1999.
  11893. ; Procedure calls.
  11894. (define (cg-call output exp target regs frame env tail?)
  11895. (let ((proc (call.proc exp)))
  11896. (cond ((and (lambda? proc)
  11897. (list? (lambda.args proc)))
  11898. (cg-let output exp target regs frame env tail?))
  11899. ((not (variable? proc))
  11900. (cg-unknown-call output exp target regs frame env tail?))
  11901. (else (let ((entry
  11902. (var-lookup (variable.name proc) regs frame env)))
  11903. (case (entry.kind entry)
  11904. ((global lexical frame register)
  11905. (cg-unknown-call output
  11906. exp
  11907. target regs frame env tail?))
  11908. ((integrable)
  11909. (cg-integrable-call output
  11910. exp
  11911. target regs frame env tail?))
  11912. ((procedure)
  11913. (cg-known-call output
  11914. exp
  11915. target regs frame env tail?))
  11916. (else (error "Bug in cg-call" exp))))))))
  11917. (define (cg-unknown-call output exp target regs frame env tail?)
  11918. (let* ((proc (call.proc exp))
  11919. (args (call.args exp))
  11920. (n (length args))
  11921. (L (make-label)))
  11922. (cond ((>= (+ n 1) *lastreg*)
  11923. (cg-big-call output exp target regs frame env tail?))
  11924. (else
  11925. (let ((r0 (cgreg-lookup-reg regs 0)))
  11926. (if (variable? proc)
  11927. (let ((entry (cgreg-lookup regs (variable.name proc))))
  11928. (if (and entry
  11929. (<= (entry.regnum entry) n))
  11930. (begin (cg-arguments output
  11931. (iota1 (+ n 1))
  11932. (append args (list proc))
  11933. regs frame env)
  11934. (gen! output $reg (+ n 1)))
  11935. (begin (cg-arguments output
  11936. (iota1 n)
  11937. args
  11938. regs frame env)
  11939. (cg0 output proc 'result regs frame env #f)))
  11940. (if tail?
  11941. (gen-pop! output frame)
  11942. (begin (cgframe-used! frame)
  11943. (gen! output $setrtn L)))
  11944. (gen! output $invoke n))
  11945. (begin (cg-arguments output
  11946. (iota1 (+ n 1))
  11947. (append args (list proc))
  11948. regs frame env)
  11949. (gen! output $reg (+ n 1))
  11950. (if tail?
  11951. (gen-pop! output frame)
  11952. (begin (cgframe-used! frame)
  11953. (gen! output $setrtn L)))
  11954. (gen! output $invoke n)))
  11955. (if tail?
  11956. 'result
  11957. (begin (gen! output $.align 4)
  11958. (gen! output $.label L)
  11959. (gen! output $.cont)
  11960. (cgreg-clear! regs)
  11961. (cgreg-bind! regs 0 r0)
  11962. (gen-load! output frame 0 r0)
  11963. (cg-move output frame regs 'result target))))))))
  11964. (define (cg-known-call output exp target regs frame env tail?)
  11965. (let* ((args (call.args exp))
  11966. (n (length args))
  11967. (L (make-label)))
  11968. (cond ((>= (+ n 1) *lastreg*)
  11969. (cg-big-call output exp target regs frame env tail?))
  11970. (else
  11971. (let ((r0 (cgreg-lookup-reg regs 0)))
  11972. (cg-arguments output (iota1 n) args regs frame env)
  11973. (if tail?
  11974. (gen-pop! output frame)
  11975. (begin (cgframe-used! frame)
  11976. (gen! output $setrtn L)))
  11977. (let* ((entry (cgenv-lookup env (variable.name (call.proc exp))))
  11978. (label (entry.label entry))
  11979. (m (entry.rib entry)))
  11980. (if (zero? m)
  11981. (gen! output $branch label n)
  11982. (gen! output $jump m label n)))
  11983. (if tail?
  11984. 'result
  11985. (begin (gen! output $.align 4)
  11986. (gen! output $.label L)
  11987. (gen! output $.cont)
  11988. (cgreg-clear! regs)
  11989. (cgreg-bind! regs 0 r0)
  11990. (gen-load! output frame 0 r0)
  11991. (cg-move output frame regs 'result target))))))))
  11992. ; Any call can be compiled as follows, even if there are no free registers.
  11993. ;
  11994. ; Let T0, T1, ..., Tn be newly allocated stack temporaries.
  11995. ;
  11996. ; <arg0>
  11997. ; setstk T0
  11998. ; <arg1> -|
  11999. ; setstk T1 |
  12000. ; ... |- evaluate args into stack frame
  12001. ; <argn> |
  12002. ; setstk Tn -|
  12003. ; const ()
  12004. ; setreg R-1
  12005. ; stack Tn -|
  12006. ; op2 cons,R-1 |
  12007. ; setreg R-1 |
  12008. ; ... |- cons up overflow args
  12009. ; stack T_{R-1} |
  12010. ; op2 cons,R-1 |
  12011. ; setreg R-1 -|
  12012. ; stack T_{R-2} -|
  12013. ; setreg R-2 |
  12014. ; ... |- pop remaining args into registers
  12015. ; stack T1 |
  12016. ; setreg 1 -|
  12017. ; stack T0
  12018. ; invoke n
  12019. (define (cg-big-call output exp target regs frame env tail?)
  12020. (let* ((proc (call.proc exp))
  12021. (args (call.args exp))
  12022. (n (length args))
  12023. (argslots (newtemps n))
  12024. (procslot (newtemp))
  12025. (r0 (cgreg-lookup-reg regs 0))
  12026. (R-1 (- *nregs* 1))
  12027. (entry (if (variable? proc)
  12028. (let ((entry
  12029. (var-lookup (variable.name proc)
  12030. regs frame env)))
  12031. (if (eq? (entry.kind entry) 'procedure)
  12032. entry
  12033. #f))
  12034. #f))
  12035. (L (make-label)))
  12036. (if (not entry)
  12037. (begin
  12038. (cg0 output proc 'result regs frame env #f)
  12039. (gen-setstk! output frame procslot)))
  12040. (for-each (lambda (arg argslot)
  12041. (cg0 output arg 'result regs frame env #f)
  12042. (gen-setstk! output frame argslot))
  12043. args
  12044. argslots)
  12045. (cgreg-clear! regs)
  12046. (gen! output $const '())
  12047. (gen! output $setreg R-1)
  12048. (do ((i n (- i 1))
  12049. (slots (reverse argslots) (cdr slots)))
  12050. ((zero? i))
  12051. (if (< i R-1)
  12052. (gen-load! output frame i (car slots))
  12053. (begin (gen-stack! output frame (car slots))
  12054. (gen! output $op2 $cons R-1)
  12055. (gen! output $setreg R-1))))
  12056. (if (not entry)
  12057. (gen-stack! output frame procslot))
  12058. (if tail?
  12059. (gen-pop! output frame)
  12060. (begin (cgframe-used! frame)
  12061. (gen! output $setrtn L)))
  12062. (if entry
  12063. (let ((label (entry.label entry))
  12064. (m (entry.rib entry)))
  12065. (if (zero? m)
  12066. (gen! output $branch label n)
  12067. (gen! output $jump m label n)))
  12068. (gen! output $invoke n))
  12069. (if tail?
  12070. 'result
  12071. (begin (gen! output $.align 4)
  12072. (gen! output $.label L)
  12073. (gen! output $.cont)
  12074. (cgreg-clear! regs) ; redundant, see above
  12075. (cgreg-bind! regs 0 r0)
  12076. (gen-load! output frame 0 r0)
  12077. (cg-move output frame regs 'result target)))))
  12078. (define (cg-integrable-call output exp target regs frame env tail?)
  12079. (let ((args (call.args exp))
  12080. (entry (var-lookup (variable.name (call.proc exp)) regs frame env)))
  12081. (if (= (entry.arity entry) (length args))
  12082. (begin (case (entry.arity entry)
  12083. ((0) (gen! output $op1 (entry.op entry)))
  12084. ((1) (cg0 output (car args) 'result regs frame env #f)
  12085. (gen! output $op1 (entry.op entry)))
  12086. ((2) (cg-integrable-call2 output
  12087. entry
  12088. args
  12089. regs frame env))
  12090. ((3) (cg-integrable-call3 output
  12091. entry
  12092. args
  12093. regs frame env))
  12094. (else (error "Bug detected by cg-integrable-call"
  12095. (make-readable exp))))
  12096. (if tail?
  12097. (begin (gen-pop! output frame)
  12098. (gen! output $return)
  12099. 'result)
  12100. (cg-move output frame regs 'result target)))
  12101. (if (negative? (entry.arity entry))
  12102. (cg-special output exp target regs frame env tail?)
  12103. (error "Wrong number of arguments to integrable procedure"
  12104. (make-readable exp))))))
  12105. (define (cg-integrable-call2 output entry args regs frame env)
  12106. (let ((op (entry.op entry)))
  12107. (if (and (entry.imm entry)
  12108. (constant? (cadr args))
  12109. ((entry.imm entry) (constant.value (cadr args))))
  12110. (begin (cg0 output (car args) 'result regs frame env #f)
  12111. (gen! output $op2imm
  12112. op
  12113. (constant.value (cadr args))))
  12114. (let* ((reg2 (cg0 output (cadr args) #f regs frame env #f))
  12115. (r2 (choose-register regs frame))
  12116. (t2 (if (eq? reg2 'result)
  12117. (let ((t2 (newtemp)))
  12118. (gen! output $setreg r2)
  12119. (cgreg-bind! regs r2 t2)
  12120. (gen-store! output frame r2 t2)
  12121. t2)
  12122. (cgreg-lookup-reg regs reg2))))
  12123. (cg0 output (car args) 'result regs frame env #f)
  12124. (let* ((r2 (or (let ((entry (cgreg-lookup regs t2)))
  12125. (if entry
  12126. (entry.regnum entry)
  12127. #f))
  12128. (let ((r2 (choose-register regs frame)))
  12129. (cgreg-bind! regs r2 t2)
  12130. (gen-load! output frame r2 t2)
  12131. r2))))
  12132. (gen! output $op2 (entry.op entry) r2)
  12133. (if (eq? reg2 'result)
  12134. (begin (cgreg-release! regs r2)
  12135. (cgframe-release! frame t2)))))))
  12136. 'result)
  12137. (define (cg-integrable-call3 output entry args regs frame env)
  12138. (let* ((reg2 (cg0 output (cadr args) #f regs frame env #f))
  12139. (r2 (choose-register regs frame))
  12140. (t2 (if (eq? reg2 'result)
  12141. (let ((t2 (newtemp)))
  12142. (gen! output $setreg r2)
  12143. (cgreg-bind! regs r2 t2)
  12144. (gen-store! output frame r2 t2)
  12145. t2)
  12146. (cgreg-lookup-reg regs reg2)))
  12147. (reg3 (cg0 output (caddr args) #f regs frame env #f))
  12148. (spillregs (choose-registers regs frame 2))
  12149. (t3 (if (eq? reg3 'result)
  12150. (let ((t3 (newtemp))
  12151. (r3 (if (eq? t2 (cgreg-lookup-reg
  12152. regs (car spillregs)))
  12153. (cadr spillregs)
  12154. (car spillregs))))
  12155. (gen! output $setreg r3)
  12156. (cgreg-bind! regs r3 t3)
  12157. (gen-store! output frame r3 t3)
  12158. t3)
  12159. (cgreg-lookup-reg regs reg3))))
  12160. (cg0 output (car args) 'result regs frame env #f)
  12161. (let* ((spillregs (choose-registers regs frame 2))
  12162. (r2 (or (let ((entry (cgreg-lookup regs t2)))
  12163. (if entry
  12164. (entry.regnum entry)
  12165. #f))
  12166. (let ((r2 (car spillregs)))
  12167. (cgreg-bind! regs r2 t2)
  12168. (gen-load! output frame r2 t2)
  12169. r2)))
  12170. (r3 (or (let ((entry (cgreg-lookup regs t3)))
  12171. (if entry
  12172. (entry.regnum entry)
  12173. #f))
  12174. (let ((r3 (if (eq? r2 (car spillregs))
  12175. (cadr spillregs)
  12176. (car spillregs))))
  12177. (cgreg-bind! regs r3 t3)
  12178. (gen-load! output frame r3 t3)
  12179. r3))))
  12180. (gen! output $op3 (entry.op entry) r2 r3)
  12181. (if (eq? reg2 'result)
  12182. (begin (cgreg-release! regs r2)
  12183. (cgframe-release! frame t2)))
  12184. (if (eq? reg3 'result)
  12185. (begin (cgreg-release! regs r3)
  12186. (cgframe-release! frame t3)))))
  12187. 'result)
  12188. ; Given a short list of expressions that can be evaluated in any order,
  12189. ; evaluates the first into the result register and the others into any
  12190. ; register, and returns an ordered list of the registers that contain
  12191. ; the arguments that follow the first.
  12192. ; The number of expressions must be less than the number of argument
  12193. ; registers.
  12194. (define (cg-primop-args output args regs frame env)
  12195. ; Given a list of expressions to evaluate, a list of variables
  12196. ; and temporary names for arguments that have already been
  12197. ; evaluated, in reverse order, and a mask of booleans that
  12198. ; indicate which temporaries should be released before returning,
  12199. ; returns the correct result.
  12200. (define (eval-loop args temps mask)
  12201. (if (null? args)
  12202. (eval-first-into-result temps mask)
  12203. (let ((reg (cg0 output (car args) #f regs frame env #f)))
  12204. (if (eq? reg 'result)
  12205. (let* ((r (choose-register regs frame))
  12206. (t (newtemp)))
  12207. (gen! output $setreg r)
  12208. (cgreg-bind! regs r t)
  12209. (gen-store! output frame r t)
  12210. (eval-loop (cdr args)
  12211. (cons t temps)
  12212. (cons #t mask)))
  12213. (eval-loop (cdr args)
  12214. (cons (cgreg-lookup-reg regs reg) temps)
  12215. (cons #f mask))))))
  12216. (define (eval-first-into-result temps mask)
  12217. (cg0 output (car args) 'result regs frame env #f)
  12218. (finish-loop (choose-registers regs frame (length temps))
  12219. temps
  12220. mask
  12221. '()))
  12222. ; Given a sufficient number of disjoint registers, a list of
  12223. ; variable and temporary names that may need to be loaded into
  12224. ; registers, a mask of booleans that indicates which temporaries
  12225. ; should be released, and a list of registers in forward order,
  12226. ; returns the correct result.
  12227. (define (finish-loop disjoint temps mask registers)
  12228. (if (null? temps)
  12229. registers
  12230. (let* ((t (car temps))
  12231. (entry (cgreg-lookup regs t)))
  12232. (if entry
  12233. (let ((r (entry.regnum entry)))
  12234. (if (car mask)
  12235. (begin (cgreg-release! regs r)
  12236. (cgframe-release! frame t)))
  12237. (finish-loop disjoint
  12238. (cdr temps)
  12239. (cdr mask)
  12240. (cons r registers)))
  12241. (let ((r (car disjoint)))
  12242. (if (memv r registers)
  12243. (finish-loop (cdr disjoint) temps mask registers)
  12244. (begin (gen-load! output frame r t)
  12245. (cgreg-bind! regs r t)
  12246. (if (car mask)
  12247. (begin (cgreg-release! regs r)
  12248. (cgframe-release! frame t)))
  12249. (finish-loop disjoint
  12250. (cdr temps)
  12251. (cdr mask)
  12252. (cons r registers)))))))))
  12253. (if (< (length args) *nregs*)
  12254. (eval-loop (cdr args) '() '())
  12255. (error "Bug detected by cg-primop-args" args)))
  12256. ; Parallel assignment.
  12257. ; Given a list of target registers, a list of expressions, and a
  12258. ; compile-time environment, generates code to evaluate the expressions
  12259. ; into the registers.
  12260. ;
  12261. ; Argument evaluation proceeds as follows:
  12262. ;
  12263. ; 1. Evaluate all but one of the complicated arguments.
  12264. ; 2. Evaluate remaining arguments.
  12265. ; 3. Load spilled arguments from stack.
  12266. (define (cg-arguments output targets args regs frame env)
  12267. ; Sorts the args and their targets into complicated and
  12268. ; uncomplicated args and targets.
  12269. ; Then it calls evalargs.
  12270. (define (sortargs targets args targets1 args1 targets2 args2)
  12271. (if (null? args)
  12272. (evalargs targets1 args1 targets2 args2)
  12273. (let ((target (car targets))
  12274. (arg (car args))
  12275. (targets (cdr targets))
  12276. (args (cdr args)))
  12277. (if (complicated? arg env)
  12278. (sortargs targets
  12279. args
  12280. (cons target targets1)
  12281. (cons arg args1)
  12282. targets2
  12283. args2)
  12284. (sortargs targets
  12285. args
  12286. targets1
  12287. args1
  12288. (cons target targets2)
  12289. (cons arg args2))))))
  12290. ; Given the complicated args1 and their targets1,
  12291. ; and the uncomplicated args2 and their targets2,
  12292. ; evaluates all the arguments into their target registers.
  12293. (define (evalargs targets1 args1 targets2 args2)
  12294. (let* ((temps1 (newtemps (length targets1)))
  12295. (temps2 (newtemps (length targets2))))
  12296. (if (not (null? args1))
  12297. (for-each (lambda (arg temp)
  12298. (cg0 output arg 'result regs frame env #f)
  12299. (gen-setstk! output frame temp))
  12300. (cdr args1)
  12301. (cdr temps1)))
  12302. (if (not (null? args1))
  12303. (evalargs0 (cons (car targets1) targets2)
  12304. (cons (car args1) args2)
  12305. (cons (car temps1) temps2))
  12306. (evalargs0 targets2 args2 temps2))
  12307. (for-each (lambda (r t)
  12308. (let ((temp (cgreg-lookup-reg regs r)))
  12309. (if (not (eq? temp t))
  12310. (let ((entry (var-lookup t regs frame env)))
  12311. (case (entry.kind entry)
  12312. ((register)
  12313. (gen! output $movereg (entry.regnum entry) r))
  12314. ((frame)
  12315. (gen-load! output frame r t)))
  12316. (cgreg-bind! regs r t)))
  12317. (cgframe-release! frame t)))
  12318. (append targets1 targets2)
  12319. (append temps1 temps2))))
  12320. (define (evalargs0 targets args temps)
  12321. (if (not (null? targets))
  12322. (let ((para (let* ((regvars (map (lambda (reg)
  12323. (cgreg-lookup-reg regs reg))
  12324. targets)))
  12325. (parallel-assignment targets
  12326. (map cons regvars targets)
  12327. args))))
  12328. (if para
  12329. (let ((targets para)
  12330. (args (cg-permute args targets para))
  12331. (temps (cg-permute temps targets para)))
  12332. (for-each (lambda (arg r t)
  12333. (cg0 output arg r regs frame env #f)
  12334. (cgreg-bind! regs r t)
  12335. (gen-store! output frame r t))
  12336. args
  12337. para
  12338. temps))
  12339. (let ((r (choose-register regs frame))
  12340. (t (car temps)))
  12341. (cg0 output (car args) r regs frame env #f)
  12342. (cgreg-bind! regs r t)
  12343. (gen-store! output frame r t)
  12344. (evalargs0 (cdr targets)
  12345. (cdr args)
  12346. (cdr temps)))))))
  12347. (if (parallel-assignment-optimization)
  12348. (sortargs (reverse targets) (reverse args) '() '() '() '())
  12349. (cg-evalargs output targets args regs frame env)))
  12350. ; Left-to-right evaluation of arguments directly into targets.
  12351. (define (cg-evalargs output targets args regs frame env)
  12352. (let ((temps (newtemps (length targets))))
  12353. (for-each (lambda (arg r t)
  12354. (cg0 output arg r regs frame env #f)
  12355. (cgreg-bind! regs r t)
  12356. (gen-store! output frame r t))
  12357. args
  12358. targets
  12359. temps)
  12360. (for-each (lambda (r t)
  12361. (let ((temp (cgreg-lookup-reg regs r)))
  12362. (if (not (eq? temp t))
  12363. (begin (gen-load! output frame r t)
  12364. (cgreg-bind! regs r t)))
  12365. (cgframe-release! frame t)))
  12366. targets
  12367. temps)))
  12368. ; For heuristic use only.
  12369. ; An expression is complicated unless it can probably be evaluated
  12370. ; without saving and restoring any registers, even if it occurs in
  12371. ; a non-tail position.
  12372. (define (complicated? exp env)
  12373. (case (car exp)
  12374. ((quote) #f)
  12375. ((lambda) #t)
  12376. ((set!) (complicated? (assignment.rhs exp) env))
  12377. ((if) (or (complicated? (if.test exp) env)
  12378. (complicated? (if.then exp) env)
  12379. (complicated? (if.else exp) env)))
  12380. ((begin) (if (variable? exp)
  12381. #f
  12382. (some? (lambda (exp)
  12383. (complicated? exp env))
  12384. (begin.exprs exp))))
  12385. (else (let ((proc (call.proc exp)))
  12386. (if (and (variable? proc)
  12387. (let ((entry
  12388. (cgenv-lookup env (variable.name proc))))
  12389. (eq? (entry.kind entry) 'integrable)))
  12390. (some? (lambda (exp)
  12391. (complicated? exp env))
  12392. (call.args exp))
  12393. #t)))))
  12394. ; Returns a permutation of the src list, permuted the same way the
  12395. ; key list was permuted to obtain newkey.
  12396. (define (cg-permute src key newkey)
  12397. (let ((alist (map cons key (iota (length key)))))
  12398. (do ((newkey newkey (cdr newkey))
  12399. (dest '()
  12400. (cons (list-ref src (cdr (assq (car newkey) alist)))
  12401. dest)))
  12402. ((null? newkey) (reverse dest)))))
  12403. ; Given a list of register numbers,
  12404. ; an association list with entries of the form (name . regnum) giving
  12405. ; the variable names by which those registers are known in code,
  12406. ; and a list of expressions giving new values for those registers,
  12407. ; returns an ordering of the register assignments that implements a
  12408. ; parallel assignment if one can be found, otherwise returns #f.
  12409. (define parallel-assignment
  12410. (lambda (regnums alist exps)
  12411. (if (null? regnums)
  12412. #t
  12413. (let ((x (toposort (dependency-graph regnums alist exps))))
  12414. (if x (reverse x) #f)))))
  12415. (define dependency-graph
  12416. (lambda (regnums alist exps)
  12417. (let ((names (map car alist)))
  12418. (do ((regnums regnums (cdr regnums))
  12419. (exps exps (cdr exps))
  12420. (l '() (cons (cons (car regnums)
  12421. (map (lambda (var) (cdr (assq var alist)))
  12422. (intersection (freevariables (car exps))
  12423. names)))
  12424. l)))
  12425. ((null? regnums) l)))))
  12426. ; Given a nonempty graph represented as a list of the form
  12427. ; ((node1 . <list of nodes that node1 is less than or equal to>)
  12428. ; (node2 . <list of nodes that node2 is less than or equal to>)
  12429. ; ...)
  12430. ; returns a topological sort of the nodes if one can be found,
  12431. ; otherwise returns #f.
  12432. (define toposort
  12433. (lambda (graph)
  12434. (cond ((null? (cdr graph)) (list (caar graph)))
  12435. (else (toposort2 graph '())))))
  12436. (define toposort2
  12437. (lambda (totry tried)
  12438. (cond ((null? totry) #f)
  12439. ((or (null? (cdr (car totry)))
  12440. (and (null? (cddr (car totry)))
  12441. (eq? (cadr (car totry))
  12442. (car (car totry)))))
  12443. (if (and (null? (cdr totry)) (null? tried))
  12444. (list (caar totry))
  12445. (let* ((node (caar totry))
  12446. (x (toposort2 (map (lambda (y)
  12447. (cons (car y) (remove node (cdr y))))
  12448. (append (cdr totry) tried))
  12449. '())))
  12450. (if x
  12451. (cons node x)
  12452. #f))))
  12453. (else (toposort2 (cdr totry) (cons (car totry) tried))))))
  12454. (define iota (lambda (n) (iota2 n '())))
  12455. (define iota1 (lambda (n) (cdr (iota2 (+ n 1) '()))))
  12456. (define iota2
  12457. (lambda (n l)
  12458. (if (zero? n)
  12459. l
  12460. (let ((n (- n 1)))
  12461. (iota2 n (cons n l))))))
  12462. (define (freevariables exp)
  12463. (freevars2 exp '()))
  12464. (define (freevars2 exp env)
  12465. (cond ((symbol? exp)
  12466. (if (memq exp env) '() (list exp)))
  12467. ((not (pair? exp)) '())
  12468. (else (let ((keyword (car exp)))
  12469. (cond ((eq? keyword 'quote) '())
  12470. ((eq? keyword 'lambda)
  12471. (let ((env (append (make-null-terminated (cadr exp))
  12472. env)))
  12473. (apply-union
  12474. (map (lambda (x) (freevars2 x env))
  12475. (cddr exp)))))
  12476. ((memq keyword '(if set! begin))
  12477. (apply-union
  12478. (map (lambda (x) (freevars2 x env))
  12479. (cdr exp))))
  12480. (else (apply-union
  12481. (map (lambda (x) (freevars2 x env))
  12482. exp))))))))
  12483. ; Copyright 1991 William Clinger (cg-let and cg-let-body)
  12484. ; Copyright 1999 William Clinger (everything else)
  12485. ;
  12486. ; 10 June 1999.
  12487. ; Generates code for a let expression.
  12488. (define (cg-let output exp target regs frame env tail?)
  12489. (let* ((proc (call.proc exp))
  12490. (vars (lambda.args proc))
  12491. (n (length vars))
  12492. (free (lambda.F proc))
  12493. (live (cgframe-livevars frame)))
  12494. (if (and (null? (lambda.defs proc))
  12495. (= n 1))
  12496. (cg-let1 output exp target regs frame env tail?)
  12497. (let* ((args (call.args exp))
  12498. (temps (newtemps n))
  12499. (alist (map cons temps vars)))
  12500. (for-each (lambda (arg t)
  12501. (let ((r (choose-register regs frame)))
  12502. (cg0 output arg r regs frame env #f)
  12503. (cgreg-bind! regs r t)
  12504. (gen-store! output frame r t)))
  12505. args
  12506. temps)
  12507. (cgreg-rename! regs alist)
  12508. (cgframe-rename! frame alist)
  12509. (cg-let-release! free live regs frame tail?)
  12510. (cg-let-body output proc target regs frame env tail?)))))
  12511. ; Given the free variables of a let body, and the variables that are
  12512. ; live after the let expression, and the usual regs, frame, and tail?
  12513. ; arguments, releases any registers and frame slots that don't need
  12514. ; to be preserved across the body of the let.
  12515. (define (cg-let-release! free live regs frame tail?)
  12516. ; The tail case is easy because there are no live temporaries,
  12517. ; and there are no free variables in the context.
  12518. ; The non-tail case assumes A-normal form.
  12519. (cond (tail?
  12520. (let ((keepers (cons (cgreg-lookup-reg regs 0) free)))
  12521. (cgreg-release-except! regs keepers)
  12522. (cgframe-release-except! frame keepers)))
  12523. (live
  12524. (let ((keepers (cons (cgreg-lookup-reg regs 0)
  12525. (union live free))))
  12526. (cgreg-release-except! regs keepers)
  12527. (cgframe-release-except! frame keepers)))))
  12528. ; Generates code for the body of a let.
  12529. (define (cg-let-body output L target regs frame env tail?)
  12530. (let ((vars (lambda.args L))
  12531. (free (lambda.F L))
  12532. (live (cgframe-livevars frame)))
  12533. (let ((r (cg-body output L target regs frame env tail?)))
  12534. (for-each (lambda (v)
  12535. (let ((entry (cgreg-lookup regs v)))
  12536. (if entry
  12537. (cgreg-release! regs (entry.regnum entry)))
  12538. (cgframe-release! frame v)))
  12539. vars)
  12540. (if (and (not target)
  12541. (not (eq? r 'result))
  12542. (not (cgreg-lookup-reg regs r)))
  12543. (cg-move output frame regs r 'result)
  12544. r))))
  12545. ; Generates code for a let expression that binds exactly one variable
  12546. ; and has no internal definitions. These let expressions are very
  12547. ; common in A-normal form, and there are many special cases with
  12548. ; respect to register allocation and order of evaluation.
  12549. (define (cg-let1 output exp target regs frame env tail?)
  12550. (let* ((proc (call.proc exp))
  12551. (v (car (lambda.args proc)))
  12552. (arg (car (call.args exp)))
  12553. (free (lambda.F proc))
  12554. (live (cgframe-livevars frame))
  12555. (body (lambda.body proc)))
  12556. (define (evaluate-into-register r)
  12557. (cg0 output arg r regs frame env #f)
  12558. (cgreg-bind! regs r v)
  12559. (gen-store! output frame r v)
  12560. r)
  12561. (define (release-registers!)
  12562. (cgframe-livevars-set! frame live)
  12563. (cg-let-release! free live regs frame tail?))
  12564. (define (finish)
  12565. (release-registers!)
  12566. (cg-let-body output proc target regs frame env tail?))
  12567. (if live
  12568. (cgframe-livevars-set! frame (union live free)))
  12569. (cond ((assq v *regnames*)
  12570. (evaluate-into-register (cdr (assq v *regnames*)))
  12571. (finish))
  12572. ((not (memq v free))
  12573. (cg0 output arg #f regs frame env #f)
  12574. (finish))
  12575. (live
  12576. (cg0 output arg 'result regs frame env #f)
  12577. (release-registers!)
  12578. (cg-let1-result output exp target regs frame env tail?))
  12579. (else
  12580. (evaluate-into-register (choose-register regs frame))
  12581. (finish)))))
  12582. ; Given a let expression that binds one variable whose value has already
  12583. ; been evaluated into the result register, generates code for the rest
  12584. ; of the let expression.
  12585. ; The main difficulty is an unfortunate interaction between A-normal
  12586. ; form and the MacScheme machine architecture: We don't want to move
  12587. ; a value from the result register into a general register if it has
  12588. ; only one use and can remain in the result register until that use.
  12589. (define (cg-let1-result output exp target regs frame env tail?)
  12590. (let* ((proc (call.proc exp))
  12591. (v (car (lambda.args proc)))
  12592. (free (lambda.F proc))
  12593. (live (cgframe-livevars frame))
  12594. (body (lambda.body proc))
  12595. (pattern (cg-let-used-once v body)))
  12596. (define (move-to-register r)
  12597. (gen! output $setreg r)
  12598. (cgreg-bind! regs r v)
  12599. (gen-store! output frame r v)
  12600. r)
  12601. (define (release-registers!)
  12602. (cgframe-livevars-set! frame live)
  12603. (cg-let-release! free live regs frame tail?))
  12604. ; FIXME: The live variables must be correct in the frame.
  12605. (case pattern
  12606. ((if)
  12607. (cg-if-result output body target regs frame env tail?))
  12608. ((let-if)
  12609. (if live
  12610. (cgframe-livevars-set! frame (union live free)))
  12611. (cg-if-result output
  12612. (car (call.args body))
  12613. 'result regs frame env #f)
  12614. (release-registers!)
  12615. (cg-let1-result output body target regs frame env tail?))
  12616. ((set!)
  12617. (cg-assignment-result output
  12618. body target regs frame env tail?))
  12619. ((let-set!)
  12620. (cg-assignment-result output
  12621. (car (call.args body))
  12622. 'result regs frame env #f)
  12623. (cg-let1-result output body target regs frame env tail?))
  12624. ((primop)
  12625. (cg-primop-result output body target regs frame env tail?))
  12626. ((let-primop)
  12627. (cg-primop-result output
  12628. (car (call.args body))
  12629. 'result regs frame env #f)
  12630. (cg-let1-result output body target regs frame env tail?))
  12631. ; FIXME
  12632. ((_called)
  12633. (cg-call-result output body target regs frame env tail?))
  12634. ; FIXME
  12635. ((_let-called)
  12636. (cg-call-result output
  12637. (car (call.args body))
  12638. 'result regs frame env #f)
  12639. (cg-let1-result output body target regs frame env tail?))
  12640. (else
  12641. ; FIXME: The first case was handled by cg-let1.
  12642. (cond ((assq v *regnames*)
  12643. (move-to-register (cdr (assq v *regnames*))))
  12644. ((memq v free)
  12645. (move-to-register (choose-register regs frame))))
  12646. (cg-let-body output proc target regs frame env tail?)))))
  12647. ; Given a call to a primop whose first argument has already been
  12648. ; evaluated into the result register and whose remaining arguments
  12649. ; consist of constants and variable references, generates code for
  12650. ; the call.
  12651. (define (cg-primop-result output exp target regs frame env tail?)
  12652. (let ((args (call.args exp))
  12653. (entry (var-lookup (variable.name (call.proc exp)) regs frame env)))
  12654. (if (= (entry.arity entry) (length args))
  12655. (begin (case (entry.arity entry)
  12656. ((0) (gen! output $op1 (entry.op entry)))
  12657. ((1) (gen! output $op1 (entry.op entry)))
  12658. ((2) (cg-primop2-result! output entry args regs frame env))
  12659. ((3) (let ((rs (cg-result-args output args regs frame env)))
  12660. (gen! output
  12661. $op3 (entry.op entry) (car rs) (cadr rs))))
  12662. (else (error "Bug detected by cg-primop-result"
  12663. (make-readable exp))))
  12664. (if tail?
  12665. (begin (gen-pop! output frame)
  12666. (gen! output $return)
  12667. 'result)
  12668. (cg-move output frame regs 'result target)))
  12669. (if (negative? (entry.arity entry))
  12670. (cg-special-result output exp target regs frame env tail?)
  12671. (error "Wrong number of arguments to integrable procedure"
  12672. (make-readable exp))))))
  12673. (define (cg-primop2-result! output entry args regs frame env)
  12674. (let ((op (entry.op entry))
  12675. (arg2 (cadr args)))
  12676. (if (and (constant? arg2)
  12677. (entry.imm entry)
  12678. ((entry.imm entry) (constant.value arg2)))
  12679. (gen! output $op2imm op (constant.value arg2))
  12680. (let ((rs (cg-result-args output args regs frame env)))
  12681. (gen! output $op2 op (car rs))))))
  12682. ; Given a short list of constants and variable references to be evaluated
  12683. ; into arbitrary general registers, evaluates them into registers without
  12684. ; disturbing the result register and returns a list of the registers into
  12685. ; which they are evaluated. Before returning, any registers that were
  12686. ; allocated by this routine are released.
  12687. (define (cg-result-args output args regs frame env)
  12688. ; Given a list of unevaluated arguments,
  12689. ; a longer list of disjoint general registers,
  12690. ; the register that holds the first evaluated argument,
  12691. ; a list of registers in reverse order that hold other arguments,
  12692. ; and a list of registers to be released afterwards,
  12693. ; generates code to evaluate the arguments,
  12694. ; deallocates any registers that were evaluated to hold the arguments,
  12695. ; and returns the list of registers that contain the arguments.
  12696. (define (loop args registers rr rs temps)
  12697. (if (null? args)
  12698. (begin (if (not (eq? rr 'result))
  12699. (gen! output $reg rr))
  12700. (for-each (lambda (r) (cgreg-release! regs r))
  12701. temps)
  12702. (reverse rs))
  12703. (let ((arg (car args)))
  12704. (cond ((constant? arg)
  12705. (let ((r (car registers)))
  12706. (gen! output $const/setreg (constant.value arg) r)
  12707. (cgreg-bind! regs r #t)
  12708. (loop (cdr args)
  12709. (cdr registers)
  12710. rr
  12711. (cons r rs)
  12712. (cons r temps))))
  12713. ((variable? arg)
  12714. (let* ((id (variable.name arg))
  12715. (entry (var-lookup id regs frame env)))
  12716. (case (entry.kind entry)
  12717. ((global integrable)
  12718. (if (eq? rr 'result)
  12719. (save-result! args registers rr rs temps)
  12720. (let ((r (car registers)))
  12721. (gen! output $global id)
  12722. (gen! output $setreg r)
  12723. (cgreg-bind! regs r id)
  12724. (loop (cdr args)
  12725. (cdr registers)
  12726. rr
  12727. (cons r rs)
  12728. (cons r temps)))))
  12729. ((lexical)
  12730. (if (eq? rr 'result)
  12731. (save-result! args registers rr rs temps)
  12732. (let ((m (entry.rib entry))
  12733. (n (entry.offset entry))
  12734. (r (car registers)))
  12735. (gen! output $lexical m n id)
  12736. (gen! output $setreg r)
  12737. (cgreg-bind! regs r id)
  12738. (loop (cdr args)
  12739. (cdr registers)
  12740. rr
  12741. (cons r rs)
  12742. (cons r temps)))))
  12743. ((procedure) (error "Bug in cg-variable" arg))
  12744. ((register)
  12745. (let ((r (entry.regnum entry)))
  12746. (loop (cdr args)
  12747. registers
  12748. rr
  12749. (cons r rs)
  12750. temps)))
  12751. ((frame)
  12752. (let ((r (car registers)))
  12753. (gen-load! output frame r id)
  12754. (cgreg-bind! regs r id)
  12755. (loop (cdr args)
  12756. (cdr registers)
  12757. rr
  12758. (cons r rs)
  12759. (cons r temps))))
  12760. (else (error "Bug in cg-result-args" arg)))))
  12761. (else
  12762. (error "Bug in cg-result-args"))))))
  12763. (define (save-result! args registers rr rs temps)
  12764. (let ((r (car registers)))
  12765. (gen! output $setreg r)
  12766. (loop args
  12767. (cdr registers)
  12768. r
  12769. rs
  12770. temps)))
  12771. (loop (cdr args)
  12772. (choose-registers regs frame (length args))
  12773. 'result '() '()))
  12774. ; Given a local variable T1 and an expression in A-normal form,
  12775. ; cg-let-used-once returns a symbol if the local variable is used
  12776. ; exactly once in the expression and the expression matches one of
  12777. ; the patterns below. Otherwise returns #f. The symbol that is
  12778. ; returned is the name of the pattern that is matched.
  12779. ;
  12780. ; pattern symbol returned
  12781. ;
  12782. ; (if T1 ... ...) if
  12783. ;
  12784. ; (<primop> T1 ...) primop
  12785. ;
  12786. ; (T1 ...) called
  12787. ;
  12788. ; (set! ... T1) set!
  12789. ;
  12790. ; (let ((T2 (if T1 ... ...))) let-if
  12791. ; E3)
  12792. ;
  12793. ; (let ((T2 (<primop> T1 ...))) let-primop
  12794. ; E3)
  12795. ;
  12796. ; (let ((T2 (T1 ...))) let-called
  12797. ; E3)
  12798. ;
  12799. ; (let ((T2 (set! ... T1))) let-set!
  12800. ; E3)
  12801. ;
  12802. ; This implementation sometimes returns #f incorrectly, but it always
  12803. ; returns an answer in constant time (assuming A-normal form).
  12804. (define (cg-let-used-once T1 exp)
  12805. (define budget 20)
  12806. (define (cg-let-used-once T1 exp)
  12807. (define (used? T1 exp)
  12808. (set! budget (- budget 1))
  12809. (cond ((negative? budget) #t)
  12810. ((constant? exp) #f)
  12811. ((variable? exp)
  12812. (eq? T1 (variable.name exp)))
  12813. ((lambda? exp)
  12814. (memq T1 (lambda.F exp)))
  12815. ((assignment? exp)
  12816. (used? T1 (assignment.rhs exp)))
  12817. ((call? exp)
  12818. (or (used? T1 (call.proc exp))
  12819. (used-in-args? T1 (call.args exp))))
  12820. ((conditional? exp)
  12821. (or (used? T1 (if.test exp))
  12822. (used? T1 (if.then exp))
  12823. (used? T1 (if.else exp))))
  12824. (else #t)))
  12825. (define (used-in-args? T1 args)
  12826. (if (null? args)
  12827. #f
  12828. (or (used? T1 (car args))
  12829. (used-in-args? T1 (cdr args)))))
  12830. (set! budget (- budget 1))
  12831. (cond ((negative? budget) #f)
  12832. ((call? exp)
  12833. (let ((proc (call.proc exp))
  12834. (args (call.args exp)))
  12835. (cond ((variable? proc)
  12836. (let ((f (variable.name proc)))
  12837. (cond ((eq? f T1)
  12838. (and (not (used-in-args? T1 args))
  12839. 'called))
  12840. ((and (integrable? f)
  12841. (not (null? args))
  12842. (variable? (car args))
  12843. (eq? T1 (variable.name (car args))))
  12844. (and (not (used-in-args? T1 (cdr args)))
  12845. 'primop))
  12846. (else #f))))
  12847. ((lambda? proc)
  12848. (and (not (memq T1 (lambda.F proc)))
  12849. (not (null? args))
  12850. (null? (cdr args))
  12851. (case (cg-let-used-once T1 (car args))
  12852. ((if) 'let-if)
  12853. ((primop) 'let-primop)
  12854. ((called) 'let-called)
  12855. ((set!) 'let-set!)
  12856. (else #f))))
  12857. (else #f))))
  12858. ((conditional? exp)
  12859. (let ((E0 (if.test exp)))
  12860. (and (variable? E0)
  12861. (eq? T1 (variable.name E0))
  12862. (not (used? T1 (if.then exp)))
  12863. (not (used? T1 (if.else exp)))
  12864. 'if)))
  12865. ((assignment? exp)
  12866. (let ((rhs (assignment.rhs exp)))
  12867. (and (variable? rhs)
  12868. (eq? T1 (variable.name rhs))
  12869. 'set!)))
  12870. (else #f)))
  12871. (cg-let-used-once T1 exp))
  12872. ; Given the name of a let-body pattern, an expression that matches that
  12873. ; pattern, and an expression to be substituted for the let variable,
  12874. ; returns the transformed expression.
  12875. ; FIXME: No longer used.
  12876. (define (cg-let-transform pattern exp E1)
  12877. (case pattern
  12878. ((if)
  12879. (make-conditional E1 (if.then exp) (if.else exp)))
  12880. ((primop)
  12881. (make-call (call.proc exp)
  12882. (cons E1 (cdr (call.args exp)))))
  12883. ((called)
  12884. (make-call E1 (call.args exp)))
  12885. ((set!)
  12886. (make-assignment (assignment.lhs exp) E1))
  12887. ((let-if let-primop let-called let-set!)
  12888. (make-call (call.proc exp)
  12889. (list (cg-let-transform (case pattern
  12890. ((let-if) 'if)
  12891. ((let-primop) 'primop)
  12892. ((let-called) 'called)
  12893. ((let-set!) 'set!))
  12894. (car (call.args exp))
  12895. E1))))
  12896. (else
  12897. (error "Unrecognized pattern in cg-let-transform" pattern)))); Copyright 1999 William Clinger
  12898. ;
  12899. ; Code for special primitives, used to generate runtime safety checks,
  12900. ; efficient code for call-with-values, and other weird things.
  12901. ;
  12902. ; 4 June 1999.
  12903. (define (cg-special output exp target regs frame env tail?)
  12904. (let ((name (variable.name (call.proc exp))))
  12905. (cond ((eq? name name:CHECK!)
  12906. (if (runtime-safety-checking)
  12907. (cg-check output exp target regs frame env tail?)))
  12908. (else
  12909. (error "Compiler bug: cg-special" (make-readable exp))))))
  12910. (define (cg-special-result output exp target regs frame env tail?)
  12911. (let ((name (variable.name (call.proc exp))))
  12912. (cond ((eq? name name:CHECK!)
  12913. (if (runtime-safety-checking)
  12914. (cg-check-result output exp target regs frame env tail?)))
  12915. (else
  12916. (error "Compiler bug: cg-special" (make-readable exp))))))
  12917. (define (cg-check output exp target regs frame env tail?)
  12918. (cg0 output (car (call.args exp)) 'result regs frame env #f)
  12919. (cg-check-result output exp target regs frame env tail?))
  12920. (define (cg-check-result output exp target regs frame env tail?)
  12921. (let* ((args (call.args exp))
  12922. (nargs (length args))
  12923. (valexps (cddr args)))
  12924. (if (and (<= 2 nargs 5)
  12925. (constant? (cadr args))
  12926. (every? (lambda (exp)
  12927. (or (constant? exp)
  12928. (variable? exp)))
  12929. valexps))
  12930. (let* ((exn (constant.value (cadr args)))
  12931. (vars (filter variable? valexps))
  12932. (rs (cg-result-args output
  12933. (cons (car args) vars)
  12934. regs frame env)))
  12935. ; Construct the trap situation:
  12936. ; the exception number followed by an ordered list of
  12937. ; register numbers and constant expressions.
  12938. (let loop ((registers rs)
  12939. (exps valexps)
  12940. (operands '()))
  12941. (cond ((null? exps)
  12942. (let* ((situation (cons exn (reverse operands)))
  12943. (ht (assembly-stream-info output))
  12944. (L1 (or (hashtable-get ht situation)
  12945. (let ((L1 (make-label)))
  12946. (hashtable-put! ht situation L1)
  12947. L1))))
  12948. (define (translate r)
  12949. (if (number? r) r 0))
  12950. (case (length operands)
  12951. ((0) (gen! output $check 0 0 0 L1))
  12952. ((1) (gen! output $check
  12953. (translate (car operands))
  12954. 0 0 L1))
  12955. ((2) (gen! output $check
  12956. (translate (car operands))
  12957. (translate (cadr operands))
  12958. 0 L1))
  12959. ((3) (gen! output $check
  12960. (translate (car operands))
  12961. (translate (cadr operands))
  12962. (translate (caddr operands))
  12963. L1)))))
  12964. ((constant? (car exps))
  12965. (loop registers
  12966. (cdr exps)
  12967. (cons (car exps) operands)))
  12968. (else
  12969. (loop (cdr registers)
  12970. (cdr exps)
  12971. (cons (car registers) operands))))))
  12972. (error "Compiler bug: runtime check" (make-readable exp)))))
  12973. ; Given an assembly stream and the description of a trap as recorded
  12974. ; by cg-check above, generates a non-continuable trap at that label for
  12975. ; that trap, passing the operands to the exception handler.
  12976. (define (cg-trap output situation L1)
  12977. (let* ((exn (car situation))
  12978. (operands (cdr situation)))
  12979. (gen! output $.label L1)
  12980. (let ((liveregs (filter number? operands)))
  12981. (define (loop operands registers r)
  12982. (cond ((null? operands)
  12983. (case (length registers)
  12984. ((0) (gen! output $trap 0 0 0 exn))
  12985. ((1) (gen! output $trap (car registers) 0 0 exn))
  12986. ((2) (gen! output $trap
  12987. (car registers)
  12988. (cadr registers)
  12989. 0
  12990. exn))
  12991. ((3) (gen! output $trap
  12992. (car registers)
  12993. (cadr registers)
  12994. (caddr registers)
  12995. exn))
  12996. (else "Compiler bug: trap")))
  12997. ((number? (car operands))
  12998. (loop (cdr operands)
  12999. (cons (car operands) registers)
  13000. r))
  13001. ((memv r liveregs)
  13002. (loop operands registers (+ r 1)))
  13003. (else
  13004. (gen! output $const (constant.value (car operands)))
  13005. (gen! output $setreg r)
  13006. (loop (cdr operands)
  13007. (cons r registers)
  13008. (+ r 1)))))
  13009. (loop (reverse operands) '() 1))))
  13010. ; Given a short list of expressions that can be evaluated in any order,
  13011. ; evaluates the first into the result register and the others into any
  13012. ; register, and returns an ordered list of the registers that contain
  13013. ; the arguments that follow the first.
  13014. ; The number of expressions must be less than the number of argument
  13015. ; registers.
  13016. ; FIXME: No longer used.
  13017. (define (cg-check-args output args regs frame env)
  13018. ; Given a list of expressions to evaluate, a list of variables
  13019. ; and temporary names for arguments that have already been
  13020. ; evaluated, in reverse order, and a mask of booleans that
  13021. ; indicate which temporaries should be released before returning,
  13022. ; returns the correct result.
  13023. (define (eval-loop args temps mask)
  13024. (if (null? args)
  13025. (eval-first-into-result temps mask)
  13026. (let ((reg (cg0 output (car args) #f regs frame env #f)))
  13027. (if (eq? reg 'result)
  13028. (let* ((r (choose-register regs frame))
  13029. (t (newtemp)))
  13030. (gen! output $setreg r)
  13031. (cgreg-bind! regs r t)
  13032. (gen-store! output frame r t)
  13033. (eval-loop (cdr args)
  13034. (cons t temps)
  13035. (cons #t mask)))
  13036. (eval-loop (cdr args)
  13037. (cons (cgreg-lookup-reg regs reg) temps)
  13038. (cons #f mask))))))
  13039. (define (eval-first-into-result temps mask)
  13040. (cg0 output (car args) 'result regs frame env #f)
  13041. (finish-loop (choose-registers regs frame (length temps))
  13042. temps
  13043. mask
  13044. '()))
  13045. ; Given a sufficient number of disjoint registers, a list of
  13046. ; variable and temporary names that may need to be loaded into
  13047. ; registers, a mask of booleans that indicates which temporaries
  13048. ; should be released, and a list of registers in forward order,
  13049. ; returns the correct result.
  13050. (define (finish-loop disjoint temps mask registers)
  13051. (if (null? temps)
  13052. registers
  13053. (let* ((t (car temps))
  13054. (entry (cgreg-lookup regs t)))
  13055. (if entry
  13056. (let ((r (entry.regnum entry)))
  13057. (if (car mask)
  13058. (begin (cgreg-release! regs r)
  13059. (cgframe-release! frame t)))
  13060. (finish-loop disjoint
  13061. (cdr temps)
  13062. (cdr mask)
  13063. (cons r registers)))
  13064. (let ((r (car disjoint)))
  13065. (if (memv r registers)
  13066. (finish-loop (cdr disjoint) temps mask registers)
  13067. (begin (gen-load! output frame r t)
  13068. (cgreg-bind! regs r t)
  13069. (if (car mask)
  13070. (begin (cgreg-release! regs r)
  13071. (cgframe-release! frame t)))
  13072. (finish-loop disjoint
  13073. (cdr temps)
  13074. (cdr mask)
  13075. (cons r registers)))))))))
  13076. (if (< (length args) *nregs*)
  13077. (eval-loop (cdr args) '() '())
  13078. (error "Bug detected by cg-primop-args" args)))
  13079. ; Copyright 1998 William Clinger.
  13080. ;
  13081. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  13082. ;
  13083. ; 5 June 1999.
  13084. ;
  13085. ; Local optimizations for MacScheme machine assembly code.
  13086. ;
  13087. ; Branch tensioning.
  13088. ; Suppress nop instructions.
  13089. ; Suppress save, restore, and pop instructions whose operand is -1.
  13090. ; Suppress redundant stores.
  13091. ; Suppress definitions (primarily loads) of dead registers.
  13092. ;
  13093. ; Note: Twobit never generates a locally redundant load or store,
  13094. ; so this code must be tested with a different code generator.
  13095. ;
  13096. ; To perform these optimizations, the basic block must be traversed
  13097. ; both forwards and backwards.
  13098. ; The forward traversal keeps track of registers that were defined
  13099. ; by a load.
  13100. ; The backward traversal keeps track of live registers.
  13101. (define filter-basic-blocks
  13102. (let* ((suppression-message
  13103. "Local optimization detected a useless instruction.")
  13104. ; Each instruction is mapping to an encoding of the actions
  13105. ; to be performed when it is encountered during the forward
  13106. ; or backward traversal.
  13107. (forward:normal 0)
  13108. (forward:nop 1)
  13109. (forward:ends-block 2)
  13110. (forward:interesting 3)
  13111. (forward:kills-all-registers 4)
  13112. (forward:nop-if-arg1-is-negative 5)
  13113. (backward:normal 0)
  13114. (backward:ends-block 1)
  13115. (backward:begins-block 2)
  13116. (backward:uses-arg1 4)
  13117. (backward:uses-arg2 8)
  13118. (backward:uses-arg3 16)
  13119. (backward:kills-arg1 32)
  13120. (backward:kills-arg2 64)
  13121. (backward:uses-many 128)
  13122. ; largest mnemonic + 1
  13123. (dispatch-table-size *number-of-mnemonics*)
  13124. ; Dispatch table for the forwards traversal.
  13125. (forward-table (make-bytevector dispatch-table-size))
  13126. ; Dispatch table for the backwards traversal.
  13127. (backward-table (make-bytevector dispatch-table-size)))
  13128. (do ((i 0 (+ i 1)))
  13129. ((= i dispatch-table-size))
  13130. (bytevector-set! forward-table i forward:normal)
  13131. (bytevector-set! backward-table i backward:normal))
  13132. (bytevector-set! forward-table $nop forward:nop)
  13133. (bytevector-set! forward-table $invoke forward:ends-block)
  13134. (bytevector-set! forward-table $return forward:ends-block)
  13135. (bytevector-set! forward-table $skip forward:ends-block)
  13136. (bytevector-set! forward-table $branch forward:ends-block)
  13137. (bytevector-set! forward-table $branchf forward:ends-block)
  13138. (bytevector-set! forward-table $jump forward:ends-block)
  13139. (bytevector-set! forward-table $.align forward:ends-block)
  13140. (bytevector-set! forward-table $.proc forward:ends-block)
  13141. (bytevector-set! forward-table $.cont forward:ends-block)
  13142. (bytevector-set! forward-table $.label forward:ends-block)
  13143. (bytevector-set! forward-table $store forward:interesting)
  13144. (bytevector-set! forward-table $load forward:interesting)
  13145. (bytevector-set! forward-table $setstk forward:interesting)
  13146. (bytevector-set! forward-table $setreg forward:interesting)
  13147. (bytevector-set! forward-table $movereg forward:interesting)
  13148. (bytevector-set! forward-table $const/setreg
  13149. forward:interesting)
  13150. (bytevector-set! forward-table $args>= forward:kills-all-registers)
  13151. (bytevector-set! forward-table $popstk forward:kills-all-registers)
  13152. ; These instructions also kill all registers.
  13153. (bytevector-set! forward-table $save forward:nop-if-arg1-is-negative)
  13154. (bytevector-set! forward-table $restore forward:nop-if-arg1-is-negative)
  13155. (bytevector-set! forward-table $pop forward:nop-if-arg1-is-negative)
  13156. (bytevector-set! backward-table $invoke backward:ends-block)
  13157. (bytevector-set! backward-table $return backward:ends-block)
  13158. (bytevector-set! backward-table $skip backward:ends-block)
  13159. (bytevector-set! backward-table $branch backward:ends-block)
  13160. (bytevector-set! backward-table $branchf backward:ends-block)
  13161. (bytevector-set! backward-table $jump backward:begins-block) ; [sic]
  13162. (bytevector-set! backward-table $.align backward:begins-block)
  13163. (bytevector-set! backward-table $.proc backward:begins-block)
  13164. (bytevector-set! backward-table $.cont backward:begins-block)
  13165. (bytevector-set! backward-table $.label backward:begins-block)
  13166. (bytevector-set! backward-table $op2 backward:uses-arg2)
  13167. (bytevector-set! backward-table $op3 (logior backward:uses-arg2
  13168. backward:uses-arg3))
  13169. (bytevector-set! backward-table $check (logior
  13170. backward:uses-arg1
  13171. (logior backward:uses-arg2
  13172. backward:uses-arg3)))
  13173. (bytevector-set! backward-table $trap (logior
  13174. backward:uses-arg1
  13175. (logior backward:uses-arg2
  13176. backward:uses-arg3)))
  13177. (bytevector-set! backward-table $store backward:uses-arg1)
  13178. (bytevector-set! backward-table $reg backward:uses-arg1)
  13179. (bytevector-set! backward-table $load backward:kills-arg1)
  13180. (bytevector-set! backward-table $setreg backward:kills-arg1)
  13181. (bytevector-set! backward-table $movereg (logior backward:uses-arg1
  13182. backward:kills-arg2))
  13183. (bytevector-set! backward-table $const/setreg
  13184. backward:kills-arg2)
  13185. (bytevector-set! backward-table $lambda backward:uses-many)
  13186. (bytevector-set! backward-table $lexes backward:uses-many)
  13187. (bytevector-set! backward-table $args>= backward:uses-many)
  13188. (lambda (instructions)
  13189. (let* ((*nregs* *nregs*) ; locals might be faster than globals
  13190. ; During the forwards traversal:
  13191. ; (vector-ref registers i) = #f
  13192. ; means the content of register i is unknown
  13193. ; (vector-ref registers i) = j
  13194. ; means register was defined by load i,j
  13195. ;
  13196. ; During the backwards traversal:
  13197. ; (vector-ref registers i) = #f means register i is dead
  13198. ; (vector-ref registers i) = #t means register i is live
  13199. (registers (make-vector *nregs* #f))
  13200. ; During the forwards traversal, the label of a block that
  13201. ; falls through into another block or consists of a skip
  13202. ; to another block is mapped to another label.
  13203. ; This mapping is implemented by a hash table.
  13204. ; Before the backwards traversal, the transitive closure
  13205. ; is computed. The graph has no cycles, and the maximum
  13206. ; out-degree is 1, so this is easy.
  13207. (label-table (make-hashtable (lambda (n) n) assv)))
  13208. (define (compute-transitive-closure!)
  13209. (define (lookup x)
  13210. (let ((y (hashtable-get label-table x)))
  13211. (if y
  13212. (lookup y)
  13213. x)))
  13214. (hashtable-for-each (lambda (x y)
  13215. (hashtable-put! label-table x (lookup y)))
  13216. label-table))
  13217. ; Don't use this procedure until the preceding procedure
  13218. ; has been called.
  13219. (define (lookup-label x)
  13220. (hashtable-fetch label-table x x))
  13221. (define (vector-fill! v x)
  13222. (subvector-fill! v 0 (vector-length v) x))
  13223. (define (subvector-fill! v i j x)
  13224. (if (< i j)
  13225. (begin (vector-set! v i x)
  13226. (subvector-fill! v (+ i 1) j x))))
  13227. (define (kill-stack! j)
  13228. (do ((i 0 (+ i 1)))
  13229. ((= i *nregs*))
  13230. (let ((x (vector-ref registers i)))
  13231. (if (and x (= x j))
  13232. (vector-set! registers i #f)))))
  13233. ; Dispatch procedure for the forwards traversal.
  13234. (define (forwards instructions filtered)
  13235. (if (null? instructions)
  13236. (begin (vector-fill! registers #f)
  13237. (vector-set! registers 0 #t)
  13238. (compute-transitive-closure!)
  13239. (backwards0 filtered '()))
  13240. (let* ((instruction (car instructions))
  13241. (instructions (cdr instructions))
  13242. (op (instruction.op instruction))
  13243. (flags (bytevector-ref forward-table op)))
  13244. (cond ((eqv? flags forward:normal)
  13245. (forwards instructions (cons instruction filtered)))
  13246. ((eqv? flags forward:nop)
  13247. (forwards instructions filtered))
  13248. ((eqv? flags forward:nop-if-arg1-is-negative)
  13249. (if (negative? (instruction.arg1 instruction))
  13250. (forwards instructions filtered)
  13251. (begin (vector-fill! registers #f)
  13252. (forwards instructions
  13253. (cons instruction filtered)))))
  13254. ((eqv? flags forward:kills-all-registers)
  13255. (vector-fill! registers #f)
  13256. (forwards instructions
  13257. (cons instruction filtered)))
  13258. ((eqv? flags forward:ends-block)
  13259. (vector-fill! registers #f)
  13260. (if (eqv? op $.label)
  13261. (forwards-label instruction
  13262. instructions
  13263. filtered)
  13264. (forwards instructions
  13265. (cons instruction filtered))))
  13266. ((eqv? flags forward:interesting)
  13267. (cond ((eqv? op $setreg)
  13268. (vector-set! registers
  13269. (instruction.arg1 instruction)
  13270. #f)
  13271. (forwards instructions
  13272. (cons instruction filtered)))
  13273. ((eqv? op $const/setreg)
  13274. (vector-set! registers
  13275. (instruction.arg2 instruction)
  13276. #f)
  13277. (forwards instructions
  13278. (cons instruction filtered)))
  13279. ((eqv? op $movereg)
  13280. (vector-set! registers
  13281. (instruction.arg2 instruction)
  13282. #f)
  13283. (forwards instructions
  13284. (cons instruction filtered)))
  13285. ((eqv? op $setstk)
  13286. (kill-stack! (instruction.arg1 instruction))
  13287. (forwards instructions
  13288. (cons instruction filtered)))
  13289. ((eqv? op $load)
  13290. (let ((i (instruction.arg1 instruction))
  13291. (j (instruction.arg2 instruction)))
  13292. (if (eqv? (vector-ref registers i) j)
  13293. ; Suppress redundant load.
  13294. ; Should never happen with Twobit.
  13295. (suppress-forwards instruction
  13296. instructions
  13297. filtered)
  13298. (begin (vector-set! registers i j)
  13299. (forwards instructions
  13300. (cons instruction
  13301. filtered))))))
  13302. ((eqv? op $store)
  13303. (let ((i (instruction.arg1 instruction))
  13304. (j (instruction.arg2 instruction)))
  13305. (if (eqv? (vector-ref registers i) j)
  13306. ; Suppress redundant store.
  13307. ; Should never happen with Twobit.
  13308. (suppress-forwards instruction
  13309. instructions
  13310. filtered)
  13311. (begin (kill-stack! j)
  13312. (forwards instructions
  13313. (cons instruction
  13314. filtered))))))
  13315. (else
  13316. (local-optimization-error op))))
  13317. (else
  13318. (local-optimization-error op))))))
  13319. ; Enters labels into a table for branch tensioning.
  13320. (define (forwards-label instruction1 instructions filtered)
  13321. (let ((label1 (instruction.arg1 instruction1)))
  13322. (if (null? instructions)
  13323. ; This is ok provided the label is unreachable.
  13324. (forwards instructions (cdr filtered))
  13325. (let loop ((instructions instructions)
  13326. (filtered (cons instruction1 filtered)))
  13327. (let* ((instruction (car instructions))
  13328. (op (instruction.op instruction))
  13329. (flags (bytevector-ref forward-table op)))
  13330. (cond ((eqv? flags forward:nop)
  13331. (loop (cdr instructions) filtered))
  13332. ((and (eqv? flags forward:nop-if-arg1-is-negative)
  13333. (negative? (instruction.arg1 instruction)))
  13334. (loop (cdr instructions) filtered))
  13335. ((eqv? op $.label)
  13336. (let ((label2 (instruction.arg1 instruction)))
  13337. (hashtable-put! label-table label1 label2)
  13338. (forwards-label instruction
  13339. (cdr instructions)
  13340. (cdr filtered))))
  13341. ((eqv? op $skip)
  13342. (let ((label2 (instruction.arg1 instruction)))
  13343. (hashtable-put! label-table label1 label2)
  13344. ; We can't get rid of the skip instruction
  13345. ; because control might fall into this block,
  13346. ; but we can get rid of the label.
  13347. (forwards instructions (cdr filtered))))
  13348. (else
  13349. (forwards instructions filtered))))))))
  13350. ; Dispatch procedure for the backwards traversal.
  13351. (define (backwards instructions filtered)
  13352. (if (null? instructions)
  13353. filtered
  13354. (let* ((instruction (car instructions))
  13355. (instructions (cdr instructions))
  13356. (op (instruction.op instruction))
  13357. (flags (bytevector-ref backward-table op)))
  13358. (cond ((eqv? flags backward:normal)
  13359. (backwards instructions (cons instruction filtered)))
  13360. ((eqv? flags backward:ends-block)
  13361. (backwards0 (cons instruction instructions)
  13362. filtered))
  13363. ((eqv? flags backward:begins-block)
  13364. (backwards0 instructions
  13365. (cons instruction filtered)))
  13366. ((eqv? flags backward:uses-many)
  13367. (cond ((or (eqv? op $lambda)
  13368. (eqv? op $lexes))
  13369. (let ((live
  13370. (if (eqv? op $lexes)
  13371. (instruction.arg1 instruction)
  13372. (instruction.arg2 instruction))))
  13373. (subvector-fill! registers
  13374. 0
  13375. (min *nregs* (+ 1 live))
  13376. #t)
  13377. (backwards instructions
  13378. (cons instruction filtered))))
  13379. ((eqv? op $args>=)
  13380. (vector-fill! registers #t)
  13381. (backwards instructions
  13382. (cons instruction filtered)))
  13383. (else
  13384. (local-optimization-error op))))
  13385. ((and (eqv? (logand flags backward:kills-arg1)
  13386. backward:kills-arg1)
  13387. (not (vector-ref registers
  13388. (instruction.arg1 instruction))))
  13389. ; Suppress initialization of dead register.
  13390. (suppress-backwards instruction
  13391. instructions
  13392. filtered))
  13393. ((and (eqv? (logand flags backward:kills-arg2)
  13394. backward:kills-arg2)
  13395. (not (vector-ref registers
  13396. (instruction.arg2 instruction))))
  13397. ; Suppress initialization of dead register.
  13398. (suppress-backwards instruction
  13399. instructions
  13400. filtered))
  13401. ((and (eqv? op $movereg)
  13402. (= (instruction.arg1 instruction)
  13403. (instruction.arg2 instruction)))
  13404. (backwards instructions filtered))
  13405. (else
  13406. (let ((filtered (cons instruction filtered)))
  13407. (if (eqv? (logand flags backward:kills-arg1)
  13408. backward:kills-arg1)
  13409. (vector-set! registers
  13410. (instruction.arg1 instruction)
  13411. #f))
  13412. (if (eqv? (logand flags backward:kills-arg2)
  13413. backward:kills-arg2)
  13414. (vector-set! registers
  13415. (instruction.arg2 instruction)
  13416. #f))
  13417. (if (eqv? (logand flags backward:uses-arg1)
  13418. backward:uses-arg1)
  13419. (vector-set! registers
  13420. (instruction.arg1 instruction)
  13421. #t))
  13422. (if (eqv? (logand flags backward:uses-arg2)
  13423. backward:uses-arg2)
  13424. (vector-set! registers
  13425. (instruction.arg2 instruction)
  13426. #t))
  13427. (if (eqv? (logand flags backward:uses-arg3)
  13428. backward:uses-arg3)
  13429. (vector-set! registers
  13430. (instruction.arg3 instruction)
  13431. #t))
  13432. (backwards instructions filtered)))))))
  13433. ; Given a list of instructions in reverse order, whose first
  13434. ; element is the last instruction of a basic block,
  13435. ; and a filtered list of instructions in forward order,
  13436. ; returns a filtered list of instructions in the correct order.
  13437. (define (backwards0 instructions filtered)
  13438. (if (null? instructions)
  13439. filtered
  13440. (let* ((instruction (car instructions))
  13441. (mnemonic (instruction.op instruction)))
  13442. (cond ((or (eqv? mnemonic $.label)
  13443. (eqv? mnemonic $.proc)
  13444. (eqv? mnemonic $.cont)
  13445. (eqv? mnemonic $.align))
  13446. (backwards0 (cdr instructions)
  13447. (cons instruction filtered)))
  13448. ; all registers are dead at a $return
  13449. ((eqv? mnemonic $return)
  13450. (vector-fill! registers #f)
  13451. (vector-set! registers 0 #t)
  13452. (backwards (cdr instructions)
  13453. (cons instruction filtered)))
  13454. ; all but the argument registers are dead at an $invoke
  13455. ((eqv? mnemonic $invoke)
  13456. (let ((n+1 (min *nregs*
  13457. (+ (instruction.arg1 instruction) 1))))
  13458. (subvector-fill! registers 0 n+1 #t)
  13459. (subvector-fill! registers n+1 *nregs* #f)
  13460. (backwards (cdr instructions)
  13461. (cons instruction filtered))))
  13462. ; the compiler says which registers are live at the
  13463. ; target of $skip, $branch, $branchf, or $jump
  13464. ((or (eqv? mnemonic $skip)
  13465. (eqv? mnemonic $branch))
  13466. (let* ((live (instruction.arg2 instruction))
  13467. (n+1 (min *nregs* (+ live 1))))
  13468. (subvector-fill! registers 0 n+1 #t)
  13469. (subvector-fill! registers n+1 *nregs* #f)
  13470. (let ((instruction
  13471. ; FIXME
  13472. (list mnemonic
  13473. (lookup-label
  13474. (instruction.arg1 instruction))
  13475. live)))
  13476. (backwards (cdr instructions)
  13477. (cons instruction filtered)))))
  13478. ((eqv? mnemonic $jump)
  13479. (let ((n+1 (min *nregs*
  13480. (+ (instruction.arg3 instruction) 1))))
  13481. (subvector-fill! registers 0 n+1 #t)
  13482. (subvector-fill! registers n+1 *nregs* #f)
  13483. (backwards (cdr instructions)
  13484. (cons instruction filtered))))
  13485. ; the live registers at the target of a $branchf must be
  13486. ; combined with the live registers at the $branchf
  13487. ((eqv? mnemonic $branchf)
  13488. (let* ((live (instruction.arg2 instruction))
  13489. (n+1 (min *nregs* (+ live 1))))
  13490. (subvector-fill! registers 0 n+1 #t)
  13491. (let ((instruction
  13492. ; FIXME
  13493. (list mnemonic
  13494. (lookup-label
  13495. (instruction.arg1 instruction))
  13496. live)))
  13497. (backwards (cdr instructions)
  13498. (cons instruction filtered)))))
  13499. (else (backwards instructions filtered))))))
  13500. (define (suppress-forwards instruction instructions filtered)
  13501. (if (issue-warnings)
  13502. '(begin (display suppression-message)
  13503. (newline)))
  13504. (forwards instructions filtered))
  13505. (define (suppress-backwards instruction instructions filtered)
  13506. (if (issue-warnings)
  13507. '(begin (display suppression-message)
  13508. (newline)))
  13509. (backwards instructions filtered))
  13510. (define (local-optimization-error op)
  13511. (error "Compiler bug: local optimization" op))
  13512. (vector-fill! registers #f)
  13513. (forwards instructions '())))))
  13514. ; Copyright 1998 Lars T Hansen.
  13515. ;
  13516. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  13517. ;
  13518. ; 28 April 1999
  13519. ;
  13520. ; compile313 -- compilation parameters and driver procedures.
  13521. ; File types -- these may differ between operating systems.
  13522. (define *scheme-file-types* '(".sch" ".scm"))
  13523. (define *lap-file-type* ".lap")
  13524. (define *mal-file-type* ".mal")
  13525. (define *lop-file-type* ".lop")
  13526. (define *fasl-file-type* ".fasl")
  13527. ; Compile and assemble a scheme source file and produce a fastload file.
  13528. (define (compile-file infilename . rest)
  13529. (define (doit)
  13530. (let ((outfilename
  13531. (if (not (null? rest))
  13532. (car rest)
  13533. (rewrite-file-type infilename
  13534. *scheme-file-types*
  13535. *fasl-file-type*)))
  13536. (user
  13537. (assembly-user-data)))
  13538. (if (and (not (integrate-usual-procedures))
  13539. (issue-warnings))
  13540. (begin
  13541. (display "WARNING from compiler: ")
  13542. (display "integrate-usual-procedures is turned off")
  13543. (newline)
  13544. (display "Performance is likely to be poor.")
  13545. (newline)))
  13546. (if (benchmark-block-mode)
  13547. (process-file-block infilename
  13548. outfilename
  13549. dump-fasl-segment-to-port
  13550. (lambda (forms)
  13551. (assemble (compile-block forms) user)))
  13552. (process-file infilename
  13553. outfilename
  13554. dump-fasl-segment-to-port
  13555. (lambda (expr)
  13556. (assemble (compile expr) user))))
  13557. (unspecified)))
  13558. (if (eq? (nbuild-parameter 'target-machine) 'standard-c)
  13559. (error "Compile-file not supported on this target architecture.")
  13560. (doit)))
  13561. ; Assemble a MAL or LOP file and produce a FASL file.
  13562. (define (assemble-file infilename . rest)
  13563. (define (doit)
  13564. (let ((outfilename
  13565. (if (not (null? rest))
  13566. (car rest)
  13567. (rewrite-file-type infilename
  13568. (list *lap-file-type* *mal-file-type*)
  13569. *fasl-file-type*)))
  13570. (malfile?
  13571. (file-type=? infilename *mal-file-type*))
  13572. (user
  13573. (assembly-user-data)))
  13574. (process-file infilename
  13575. outfilename
  13576. dump-fasl-segment-to-port
  13577. (lambda (x) (assemble (if malfile? (eval x) x) user)))
  13578. (unspecified)))
  13579. (if (eq? (nbuild-parameter 'target-machine) 'standard-c)
  13580. (error "Assemble-file not supported on this target architecture.")
  13581. (doit)))
  13582. ; Compile and assemble a single expression; return the LOP segment.
  13583. (define compile-expression
  13584. (let ()
  13585. (define (compile-expression expr env)
  13586. (let ((syntax-env
  13587. (case (environment-tag env)
  13588. ((0 1) (make-standard-syntactic-environment))
  13589. ((2) global-syntactic-environment)
  13590. (else
  13591. (error "Invalid environment for compile-expression: " env)
  13592. #t))))
  13593. (let ((current-env global-syntactic-environment))
  13594. (dynamic-wind
  13595. (lambda ()
  13596. (set! global-syntactic-environment syntax-env))
  13597. (lambda ()
  13598. (assemble (compile expr)))
  13599. (lambda ()
  13600. (set! global-syntactic-environment current-env))))))
  13601. compile-expression))
  13602. (define macro-expand-expression
  13603. (let ()
  13604. (define (macro-expand-expression expr env)
  13605. (let ((syntax-env
  13606. (case (environment-tag env)
  13607. ((0 1) (make-standard-syntactic-environment))
  13608. ((2) global-syntactic-environment)
  13609. (else
  13610. (error "Invalid environment for compile-expression: " env)
  13611. #t))))
  13612. (let ((current-env global-syntactic-environment))
  13613. (dynamic-wind
  13614. (lambda ()
  13615. (set! global-syntactic-environment syntax-env))
  13616. (lambda ()
  13617. (make-readable
  13618. (macro-expand expr)))
  13619. (lambda ()
  13620. (set! global-syntactic-environment current-env))))))
  13621. macro-expand-expression))
  13622. ; Compile a scheme source file to a LAP file.
  13623. (define (compile313 infilename . rest)
  13624. (let ((outfilename
  13625. (if (not (null? rest))
  13626. (car rest)
  13627. (rewrite-file-type infilename
  13628. *scheme-file-types*
  13629. *lap-file-type*)))
  13630. (write-lap
  13631. (lambda (item port)
  13632. (write item port)
  13633. (newline port)
  13634. (newline port))))
  13635. (if (benchmark-block-mode)
  13636. (process-file-block infilename outfilename write-lap compile-block)
  13637. (process-file infilename outfilename write-lap compile))
  13638. (unspecified)))
  13639. ; Assemble a LAP or MAL file to a LOP file.
  13640. (define (assemble313 file . rest)
  13641. (let ((outputfile
  13642. (if (not (null? rest))
  13643. (car rest)
  13644. (rewrite-file-type file
  13645. (list *lap-file-type* *mal-file-type*)
  13646. *lop-file-type*)))
  13647. (malfile?
  13648. (file-type=? file *mal-file-type*))
  13649. (user
  13650. (assembly-user-data)))
  13651. (process-file file
  13652. outputfile
  13653. write-lop
  13654. (lambda (x) (assemble (if malfile? (eval x) x) user)))
  13655. (unspecified)))
  13656. ; Compile and assemble a Scheme source file to a LOP file.
  13657. (define (compile-and-assemble313 input-file . rest)
  13658. (let ((output-file
  13659. (if (not (null? rest))
  13660. (car rest)
  13661. (rewrite-file-type input-file
  13662. *scheme-file-types*
  13663. *lop-file-type*)))
  13664. (user
  13665. (assembly-user-data)))
  13666. (if (benchmark-block-mode)
  13667. (process-file-block input-file
  13668. output-file
  13669. write-lop
  13670. (lambda (x) (assemble (compile-block x) user)))
  13671. (process-file input-file
  13672. output-file
  13673. write-lop
  13674. (lambda (x) (assemble (compile x) user))))
  13675. (unspecified)))
  13676. ; Convert a LOP file to a FASL file.
  13677. (define (make-fasl infilename . rest)
  13678. (define (doit)
  13679. (let ((outfilename
  13680. (if (not (null? rest))
  13681. (car rest)
  13682. (rewrite-file-type infilename
  13683. *lop-file-type*
  13684. *fasl-file-type*))))
  13685. (process-file infilename
  13686. outfilename
  13687. dump-fasl-segment-to-port
  13688. (lambda (x) x))
  13689. (unspecified)))
  13690. (if (eq? (nbuild-parameter 'target-machine) 'standard-c)
  13691. (error "Make-fasl not supported on this target architecture.")
  13692. (doit)))
  13693. ; Disassemble a procedure's code vector.
  13694. (define (disassemble item . rest)
  13695. (let ((output-port (if (null? rest)
  13696. (current-output-port)
  13697. (car rest))))
  13698. (disassemble-item item #f output-port)
  13699. (unspecified)))
  13700. ; The item can be either a procedure or a pair (assumed to be a segment).
  13701. (define (disassemble-item item segment-no port)
  13702. (define (print . rest)
  13703. (for-each (lambda (x) (display x port)) rest)
  13704. (newline port))
  13705. (define (print-constvector cv)
  13706. (do ((i 0 (+ i 1)))
  13707. ((= i (vector-length cv)))
  13708. (print "------------------------------------------")
  13709. (print "Constant vector element # " i)
  13710. (case (car (vector-ref cv i))
  13711. ((codevector)
  13712. (print "Code vector")
  13713. (print-instructions (disassemble-codevector
  13714. (cadr (vector-ref cv i)))
  13715. port))
  13716. ((constantvector)
  13717. (print "Constant vector")
  13718. (print-constvector (cadr (vector-ref cv i))))
  13719. ((global)
  13720. (print "Global: " (cadr (vector-ref cv i))))
  13721. ((data)
  13722. (print "Data: " (cadr (vector-ref cv i)))))))
  13723. (define (print-segment segment)
  13724. (print "Segment # " segment-no)
  13725. (print-instructions (disassemble-codevector (car segment)) port)
  13726. (print-constvector (cdr segment))
  13727. (print "========================================"))
  13728. (cond ((procedure? item)
  13729. (print-instructions (disassemble-codevector (procedure-ref item 0))
  13730. port))
  13731. ((and (pair? item)
  13732. (bytevector? (car item))
  13733. (vector? (cdr item)))
  13734. (print-segment item))
  13735. (else
  13736. (error "disassemble-item: " item " is not disassemblable."))))
  13737. ; Disassemble a ".lop" or ".fasl" file; dump output to screen or
  13738. ; other (optional) file.
  13739. (define (disassemble-file file . rest)
  13740. (define (doit input-port output-port)
  13741. (display "; From " output-port)
  13742. (display file output-port)
  13743. (newline output-port)
  13744. (do ((segment-no 0 (+ segment-no 1))
  13745. (segment (read input-port) (read input-port)))
  13746. ((eof-object? segment))
  13747. (disassemble-item segment segment-no output-port)))
  13748. ; disassemble313
  13749. (call-with-input-file
  13750. file
  13751. (lambda (input-port)
  13752. (if (null? rest)
  13753. (doit input-port (current-output-port))
  13754. (begin
  13755. (delete-file (car rest))
  13756. (call-with-output-file
  13757. (car rest)
  13758. (lambda (output-port) (doit input-port output-port)))))))
  13759. (unspecified))
  13760. ; Display and manipulate the compiler switches.
  13761. (define (compiler-switches . rest)
  13762. (define (slow-code)
  13763. (set-compiler-flags! 'no-optimization)
  13764. (set-assembler-flags! 'no-optimization))
  13765. (define (standard-code)
  13766. (set-compiler-flags! 'standard)
  13767. (set-assembler-flags! 'standard))
  13768. (define (fast-safe-code)
  13769. (set-compiler-flags! 'fast-safe)
  13770. (set-assembler-flags! 'fast-safe))
  13771. (define (fast-unsafe-code)
  13772. (set-compiler-flags! 'fast-unsafe)
  13773. (set-assembler-flags! 'fast-unsafe))
  13774. (cond ((null? rest)
  13775. (display "Debugging:")
  13776. (newline)
  13777. (display-twobit-flags 'debugging)
  13778. (display-assembler-flags 'debugging)
  13779. (newline)
  13780. (display "Safety:")
  13781. (newline)
  13782. (display-twobit-flags 'safety)
  13783. (display-assembler-flags 'safety)
  13784. (newline)
  13785. (display "Speed:")
  13786. (newline)
  13787. (display-twobit-flags 'optimization)
  13788. (display-assembler-flags 'optimization)
  13789. (if #f #f))
  13790. ((null? (cdr rest))
  13791. (case (car rest)
  13792. ((0 slow) (slow-code))
  13793. ((1 standard) (standard-code))
  13794. ((2 fast-safe) (fast-safe-code))
  13795. ((3 fast-unsafe) (fast-unsafe-code))
  13796. ((default
  13797. factory-settings) (fast-safe-code)
  13798. (include-source-code #t)
  13799. (benchmark-mode #f)
  13800. (benchmark-block-mode #f)
  13801. (common-subexpression-elimination #f)
  13802. (representation-inference #f))
  13803. (else
  13804. (error "Unrecognized flag " (car rest) " to compiler-switches.")))
  13805. (unspecified))
  13806. (else
  13807. (error "Too many arguments to compiler-switches."))))
  13808. ; Read and process one file, producing another.
  13809. ; Preserves the global syntactic environment.
  13810. (define (process-file infilename outfilename writer processer)
  13811. (define (doit)
  13812. (delete-file outfilename)
  13813. (call-with-output-file
  13814. outfilename
  13815. (lambda (outport)
  13816. (call-with-input-file
  13817. infilename
  13818. (lambda (inport)
  13819. (let loop ((x (read inport)))
  13820. (if (eof-object? x)
  13821. #t
  13822. (begin (writer (processer x) outport)
  13823. (loop (read inport))))))))))
  13824. (let ((current-syntactic-environment
  13825. (syntactic-copy global-syntactic-environment)))
  13826. (dynamic-wind
  13827. (lambda () #t)
  13828. (lambda () (doit))
  13829. (lambda ()
  13830. (set! global-syntactic-environment
  13831. current-syntactic-environment)))))
  13832. ; Same as above, but passes a list of the entire file's contents
  13833. ; to the processer.
  13834. ; FIXME: Both versions of PROCESS-FILE always delete the output file.
  13835. ; Shouldn't it be left alone if the input file can't be opened?
  13836. (define (process-file-block infilename outfilename writer processer)
  13837. (define (doit)
  13838. (delete-file outfilename)
  13839. (call-with-output-file
  13840. outfilename
  13841. (lambda (outport)
  13842. (call-with-input-file
  13843. infilename
  13844. (lambda (inport)
  13845. (do ((x (read inport) (read inport))
  13846. (forms '() (cons x forms)))
  13847. ((eof-object? x)
  13848. (writer (processer (reverse forms)) outport))))))))
  13849. (let ((current-syntactic-environment
  13850. (syntactic-copy global-syntactic-environment)))
  13851. (dynamic-wind
  13852. (lambda () #t)
  13853. (lambda () (doit))
  13854. (lambda ()
  13855. (set! global-syntactic-environment
  13856. current-syntactic-environment)))))
  13857. ; Given a file name with some type, produce another with some other type.
  13858. (define (rewrite-file-type filename matches new)
  13859. (if (not (pair? matches))
  13860. (rewrite-file-type filename (list matches) new)
  13861. (let ((j (string-length filename)))
  13862. (let loop ((m matches))
  13863. (cond ((null? m)
  13864. (string-append filename new))
  13865. (else
  13866. (let* ((n (car m))
  13867. (l (string-length n)))
  13868. (if (file-type=? filename n)
  13869. (string-append (substring filename 0 (- j l)) new)
  13870. (loop (cdr m))))))))))
  13871. (define (file-type=? file-name type-name)
  13872. (let ((fl (string-length file-name))
  13873. (tl (string-length type-name)))
  13874. (and (>= fl tl)
  13875. (string-ci=? type-name
  13876. (substring file-name (- fl tl) fl)))))
  13877. ; eof
  13878. ; Copyright 1998 William Clinger.
  13879. ;
  13880. ; $Id: twobit-smaller.sch,v 1.1 1999/09/09 22:13:42 lth Exp $
  13881. ;
  13882. ; Procedures that make .LAP structures human-readable
  13883. (define (readify-lap code)
  13884. (map (lambda (x)
  13885. (let ((iname (cdr (assv (car x) *mnemonic-names*))))
  13886. (if (not (= (car x) $lambda))
  13887. (cons iname (cdr x))
  13888. (list iname (readify-lap (cadr x)) (caddr x)))))
  13889. code))
  13890. (define (readify-file f . o)
  13891. (define (doit)
  13892. (let ((i (open-input-file f)))
  13893. (let loop ((x (read i)))
  13894. (if (not (eof-object? x))
  13895. (begin (pretty-print (readify-lap x))
  13896. (loop (read i)))))))
  13897. (if (null? o)
  13898. (doit)
  13899. (begin (delete-file (car o))
  13900. (with-output-to-file (car o) doit))))
  13901. ; eof
  13902. ; ----------------------------------------------------------------------
  13903. (define (twobit-benchmark . rest)
  13904. (let ((k (if (null? rest) 1 (car rest))))
  13905. (compiler-switches 'fast-safe)
  13906. (benchmark-block-mode #t)
  13907. (run-benchmark
  13908. "twobit"
  13909. k
  13910. (lambda () (compile-file "twobit-input.sch"))
  13911. (lambda (result)
  13912. #t))))
  13913. ; eof