A Survey on Multi-Stage Programming Languages

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ASurveyonMulti-StageProgrammingLanguagesZheYangJanuary16,19991IntroductionAtraditionalsoftwaresystemconsistsofmodulesofxedprogramcode;be-tweenthesemodulescomputeddataarepassed.Butitisalsonotuncommontoseeanotherformofsoftwareorganization,wheresomepartofthesoftwaregeneratescode(insteadofdata)tobeexecutedatalaterstage.Thiskindofprogrammingparadigmissometimesreferredtoasmeta-programming,becausecertainprogrammingtasksinvolvemanipulationofprogramcode.Itisalsocalledmulti-stageprogramming,becausethereisgenerallyacleardivisioninthecomputationstages,wheretheresultsfromearlierstagescanbethecodeforlaterstages.Examplesofmulti-stageprogrammingincludetranslatorssuchascom-pilers,run-timecodegeneration,andadvancedsyntaxabstractionmecha-nism(alsocalledmacrosystems)suchasthatofScheme’s.Compiler.Acompilerusuallyinvolvestwostages:compiletimeistherststage,whentargetcodeisgenerated;thegeneratedtargetcodeisexecutedatthesecondstage,runtime.Otherexamplesoftranslatorsrangefromparsergenerators,whichtranslategrammarlestoprogramcodeinmoreconventionallanguages,toembeddedSQLprocessors,whichtranslateSQLstatementsembeddedinaprogramwritteninsomehostlanguagetocodefragmentsofthishostlanguage.Mostofthesecanbethoughtofascompilersofsomespecicationlanguages.Run-timecodegeneration.Insteadofsupplyingxedargumentstothesameroutineagainandagain,generatingspecializedprogramcodeatrun-timetobereusedseveraltimesoftenoersasignicantimprove-mentintimeeciency.1TakethestandardexampleoftheUnixleaccessfunctions[25]:whenaleisopenedforaccess,variousmodes(read/read-write,text/binary,rewrite/append,etc.)aresuppliedandxed;later,whenthefunctionreadorthefunctionwriteisinvoked,thereneedstobealengthychecktoensurethatnoneoftheseaccessmodesareviolated.Thisoftenaddsasubstantialoverheadtotheessentiallycheapcostofreadandwrite,especiallywhenmostleaccesseshappeninthebuers,andnotthroughsecondarystorages.Bygeneratingspecializedcodeforthereadandwritefunctions,thesecheckscanbeeliminated.Advancedsyntax-abstractionmechanism.Somelanguages,suchasScheme,allowuserstodenesyntaxabstractions,whichcomputesourcecodefromcodethatconformstonewlyintroducedsyntacticforms.Withthismechanism,itisveryeasytoimplementdomain-speciclanguages[16]directlyinthislanguage;OlinShivers’sScsh(SchemeShell)system[28]givesoneexampleofsuchembeddedim-plementationofadomain-speciclanguage.Itisnothardtoseesomeoftheadvantagesoeredbymulti-stagepro-gramming:itdeliversgenerality(e.g.,acompilercandealwithvarioussourceprogramslikeaninterpreter)alongwitheciency(thecompiledre-sultofacompilerrunsmuchfasterthanrunningthesourceprogramusinganinterpreter);ithelpsdetecterrorsatearlystages(compilersfacilitatestaticdebugging,byperformingvariousstaticcheckssuchastypechecking);itmayaddexpressivenesstothelanguage|thecorrespondingfunctionformofsyntaxabstractionsoftenhaveanintriguingdependencybetweenthetypesanddataofitsarguments.Centraltotheseargumentsistheobser-vationthataresultfromarunofanearlystagemightbereusedseveraltimesinalaterstage,andtheamortizedcostofgeneratingspecializedcodeismuchsmallerthanthatoftherepeatedcomputation.Thesameargumentwasusedtodemonstratetheadvantageofpartialevaluation[15].Apartialevaluatortakesaprogramandsomeofitsin-putsandgeneratesaspecializedversionoftheprogram(calledtheresidualprogram),which,takingtheremaininginputs,givesthenalresult.Com-putationsthatdependsonlyontheknowninputsareperformedatthespecializationprocess,leavingaresidualprogramthatisoftenmuchmoreecientthantheoriginalone.Infact,partialevaluationiscloselyrelatedtomulti-stageprogramming.Thespecializationandtheexecutingoftheresidualprogramaretwostagesinthesenseofmulti-stageprogramming.Manyinstancesofthemulti-stageprogrammingparadigm,suchascompilersandrun-timecodegeneration,are2alsothestandardapplicationsofpartialevaluation.Ontheotherhand,thestudyofmulti-stageprogramming,especiallyitslanguagesupport,emergedfromthestudyofpartialevaluation,whenpeopleconsideredproblemssuchasthecorrectnessandtheusercontrollabilityofthespecializationprocess.Theparadigmofmulti-stageprogrammingisbackedupbythedesignphilosophyofmoderndigitalcomputers,whereprogramcodecanbema-nipulatedjustasusualdata.However,thereisstillverylittlehigh-levelprogramminglanguagesupportformulti-stageprogramming;bysuch\lan-guagesupportwemeanmechanismsthatmakeiteasytoconstructprogramcode,toguaranteecertainpropertiesofthegeneratedcodesuchasitstypecorrectness,andeventoexecutesuchprogramcode(i.e.,movetothenextstageofcomputation)inthesamerun.Multi-stageprogramminglanguageshavebeendevelopedinrecentyearstoprovideagoodbasisforthemulti-stageprogrammingparadigm.Theselanguagesallowuserstowritecodeconstructionsdirectlyintheformofsourcecode,insteadofnumerousdataconstructorsforaspecic\codetype;specialstagingannotationsareusedtoallowuserstospecifythesep-arationofcodeindierentstages.Thetypesystemsoftheselanguagesguaranteethatthegeneratedcodeiswell-formed,therebyguaranteeingthattheentireexecutionwillnot\gowrong.Thisisofparticularimportanceforapplicationssuchasrun-timecodegeneration,whereverifyingthe

1 / 35
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功