
Typ ed Closure Conversion y Yasuhiko Minamide Greg Morrisett Rob ert Harp er Research Institute for Mathematical Sciences Scho ol of Computer Science Scho ol of Computer Science Kyoto University Carnegie Mellon University Carnegie Mellon University Kyoto Japan Pittsburgh PA Pittsburgh PA nankurimskyotouacjp jgmorriscscmuedu rwhcscmuedu data Functions with free variables are replaced by co de ab Abstract stracted on an extra environment parameter Free variables Closureconversion is a program transformation used by in the b o dy of the function are replaced by references to the compilers to separate co de from data Previous accounts environment The abstracted co de is partially applied to an of closure conversion use only untyped target languages Re explicitly constructed environmentproviding the bindings cent studies show that translating to typed target languages for these variables This partial application of the co de to is a useful metho dology for building compilers b ecause a its environment is in fact susp ended until the function is ac compiler can use the typ es to implement ecient data rep tually applied to its argument the susp ended application resentations calling conventions and tagfree garbage col called a closure is a data structure consisting of a piece of lection Furthermore typ ebased translations facilitate se pure co de and a representation of its environment curity and debugging through automatic typ e checking as A critical decision in closure conversion is the choice well as correctness arguments through the metho d of logical of representation for the environment whether to use a relations at FAMlike linked CAMlikeorhybrid represen ta We present closure conversion as a typ edirected and tion The choice of representation is inuenced byade typ epreserving translation for b oth the simplytyp ed and sire to minimize closure creation time the space consumed the p olymorphic calculus Our translations are based on by the environment and the time to access a variable in a simple closures as ob jects principle higherorder func the environment An imp ortant prop erty of closure tions are viewed as ob jects consisting of a single metho d conversion is that the representation of the environmentis the co de and a single instance variable the environment private to the closure This aords considerable exibility In the simplytyp ed case the PierceTurner mo del of ob in the representation of environments and is thus exploited ject typing where ob jects are packages of existential typ e to go o d advantage by Shao and App el and Wand and suces In the p olymorphic case more careful tracking of Steckler typ e sharing is required We exploit a variant of the Harp er Previous accounts consider closure conversion as a trans Lillibri dge translucenttyp e formalism to characterize the formation to untyped terms even if the source language is typ es of p olymorphi c closures typ ed This is adequate for compilers that make little or no use of typ es in the back end or at run time However when compiling typ ed languages it is often Intro duction advantageous to propagate typ e information through each stage of the compiler and to make use of typ es at link or Closureconversion is a program even run time For example Leroys representation analy transformation that achieves a separation b etween co de and sis uses typ es to determine pro cedure calling conven This researchwas sp onsored in part by the Advanced Research tions and Ohoris record compilation uses a representa Pro jects Agency CSTO under the title The Fox Pro ject Advanced tion of typ es at run time to access comp onents of a record Languages for Systems Software ARPA Order No C issued Compilation strategies for p olymorphic languages suchas by ESCENS under Contract No FC and in part those prop osed by Morrison et al and Harp er and Mor by the National Science Foundation under Grant No CCR and in part by the Isaac Newton Institute for Mathematical Sciences risett rely on analyzing typ es at run time to supp ort Cambridge England The views and conclusions contained in this unboxed representations and nonparametric op erators in do cument are those of the authors and should not b e interpreted as cluding printing and structural equalityTagfree garbage representing ocial p olicies either expressed or implied of ARPA collection for b oth monomorphic and p olymor or the US Government Any opinions ndings and conclusions or recommendations expressed in this material are those of the au phic programming languages relies on analyzing typ es at thors and do not necessarily reect the views of the National Science run time to determine the size and la yout of ob jects in the Foundation heap To supp ort any of these implementation strategies y This researchwas p erformed while the rst author was visiting it is necessary to propagate typ e information through clo the Fox Pro ject at Carnegie Mellon University sure conversion and into the generated co de The purp ose of this pap er is to demonstrate how this can b e done in b oth a simplytyp ed and a p olymorphic setting We present closure conversion as an example of a type directed and typepreserving translation In general such the companion technical rep ort translations transform b oth a term and its typ e p ossibly relying on typ e information to guide the translation Thus Closure conversion is discussed in descriptions of various each stage of the compiler can b e viewed as a typ epreserving functional language compilers It is sim translation b etween typ ed intermediate languages Exam ilar to lifting in that it eliminates free variables in ples of such translations havebeengiven byLeroy the b o dies of abstractions but diers by making the rep Ohori Harp er and Lillibri dge and Harp er and Mor resentation of the environment explicit as a data structure risett In contrasttotyp efree compilation strategies Making the environment explicit is imp ortant b ecause it ex these translations make essential use of typ e information poses environment construction and variable lo okup to an during translation to increase the time or space eciency of optimizer Furthermore Shao and App el show that not all programs In addition to the practical advantages of this ap environment representations are safe for space and proach typ edirected translation also facilitates the work of thus cho osing a go o d environment representation is an im the compiler writer In particular the typing prop erties of p ortant part of compilation Wand and Steckler have the intermediate co de may b e exploited to give clear concise wo optimizations of the basic closure conversion consider t pro ofs of compiler correctness through the metho d of logical strategy called selective and lightweight closure conversion relations Furthermore the intermediate and provide a correctness pro of for each of these in an un co de of the compiler can b e mechanicall y typ edchecked typ ed setting Hannan recasts Wands work into a typ ed an imp ortant debugging to ol for the compiler writer Fi setting and provides correctness pro ofs for one of Wands nallycheckable typ ed intermediate languages are a promis optimizations Hannans translation like ours is given as a ing technique for ensuring safety prop erties of programs in deductive system but he do es not consider the imp ortant a distributed environment issue of environment representation preferring an abstract account instead nor do es he consider the typing prop erties We describ e closure conversion for the simplytyp ed of the closureconverted co de Finally neither Wand nor calculus and the predicative fragment of the p olymorphic Hannan consider closure conversion under a typ epassing calculus In eachcasewe present closure conversion in interpretation of p olymorphism two stages The rst stage called abstract closureconver yp edirected translation to an intermediate lan sionisa t The remainder of this pap er is organized as follows In guage with a primitive notion of closures We describ e the Section we giveanoverview of closure conversion and translation as a deductive system where the choice of en the typing issues involved for the simplytyp ed calculus vironment representations may b e indep endently made for In Section weprovide the details of our typ epreserving each closure We argue that various representations consid transform for the simplytyp ed case In Section we givean ered in the literature such as the FAM or CAM as overview of closure conversion and the typing issues involved well as hybrid representations can all b e explained in for the predicative fragment of the p olymorphic calculus this uniform framework We establish the correctness of the The formal development of this conversion is given in Section translation once for all environment representations The second stage called closurerepresentationisan other typ edirected translation where closures are imple mented in terms of generic typ ed calculus primitives The Overview of SimplyTyp ed Closure Con main idea is to represent closures as objects consisting of a single metho d the co de and a single instance variable version the environment We show that in the simplytyp ed case The main ideas of closure conversion are illustrated by con Pierce and Turners typ e discipli ne for ob jectoriented pro sidering the following ML program gramming may b e used to characterize the typ es of clo sures In particular we use existential typ e abstraction to let val x ensure the privacy of environment
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages13 Page
-
File Size-