A Bootstrapping Infrastructure to Build and Extend Pharo-Like Languages Guillermo Polito, Stéphane Ducasse, Luc Fabresse, Noury Bouraqadi
Total Page:16
File Type:pdf, Size:1020Kb
A Bootstrapping Infrastructure to Build and Extend Pharo-Like Languages Guillermo Polito, Stéphane Ducasse, Luc Fabresse, Noury Bouraqadi To cite this version: Guillermo Polito, Stéphane Ducasse, Luc Fabresse, Noury Bouraqadi. A Bootstrapping Infrastruc- ture to Build and Extend Pharo-Like Languages. Onward!, Jun 2015, Pittsburg, United States. 10.1145/2814228.2814236. hal-01185812 HAL Id: hal-01185812 https://hal.inria.fr/hal-01185812 Submitted on 21 Aug 2015 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Copyright A Bootstrapping Infrastructure to Build and Extend Pharo-Like Languages G. Polito S. Ducasse L.Fabresse N.Bouraqadi RMoD Project-Team, Inria Lille–Nord Europe, France CAR Team, Institut Mines-Telecom, Mines Douai, France Abstract Acknowledgments Bootstrapping is well known in the context of compilers, We thank the European Smalltalk User Group for their sup- where a bootstrapped compiler can compile its own source port (www.esug.org). code. Bootstrapping is a beneficial engineering practice be- cause it raises the level of abstraction of a program making 1. Introduction it easier to understand, optimize, evolve, etc. Bootstrapping A language initialization, or bootstrap, is the process where a reflective object-oriented language is however more chal- the initial elements of the language are set up. In high-level lenging, as we need also to initialize the runtime of the lan- languages, such as Ruby or Smalltalk, this bootstrap re- guage with its initial objects and classes besides writing its quires also the creation of initial objects and classes such compiler. as the Object base class and the Boolean true and false ob- In this paper, we present a novel bootstrapping infrastruc- jects (cf. Section 2). The language bootstrap usually fixes ture for Pharo-like languages that allows us to easily extend the semantics of the language, for safety reasons or language and modify such languages. Our bootstrapping process relies design. It is however desirable to have easy access to the on a first-class runtime. A first-class runtime is a meta-object language to modify it and extend it. For example, studying that represents a program’s runtime and provides a MOP to Pharo [BDN+09], a smalltalk-inspired language and plat- easily load code into it and manipulate its objects. It decou- form, we identified a need for better support in the introduc- ples the virtual machine (VM) and language concerns by in- tion of new features such as traits [SDNB03] and first-class troducing a clear VM-language interface. Using this process, instance variables [VBLN11]. we show how we succeeded to bootstrap a Smalltalk-based Changing an existing language is indeed challeng- language named Candle and then extend it with traits in less ing without the proper knowledge or infrastructure. On than 250 lines of high-level Smalltalk code. We also show one hand, languages whose language bootstrap is VM- how we can bootstrap with minimal effort two other lan- based (i.e., defined inside virtual machine (VM) routines) guages (Pharo and MetaTalk) with similar execution seman- fix several language features and prevent us to change it tics but different object models. without changing the VM. In addition, such routines may Categories and Subject Descriptors D.3.2 [Programming mix VM and language concerns, making the code harder Languages]: Language Classifications—Extensible Lan- to understand and change. On the other hand, reflective guages languages [Smi84] such as Lisp or Smalltalk provide the means to modify the language elements at runtime. How- General Terms Languages, Object-Oriented Program- ever, as these languages contain circular definitions [CKL96] ming we need to stage these changes to avoid metastability prob- Keywords Bootstrapping, OOP, Traits, Pharo, Meta- lems [KdRB91] i.e., a change in the language may introduce programming a meta-call recursion and turn the system unusable. This paper takes a high-level low-level programming ap- proach [FBC+09] and revisits an already well-known tech- nique: an object-oriented reflective language bootstrap, as it is known in the context of compilers (i.e., a circular boot- strap, where a compiler can compile itself). We believe this concept has not been fully explored for object-oriented re- flective languages, where the necessary infrastructure is still ad hoc and the impact it has on the development process of language engineers is usually unknown. In this context we DEFINITION 1 (Programming Language). A programming pose the following research question: What is the infrastruc- language L is a combination of syntax, semantics and prag- ture required to execute a circular bootstrap of an object- matics, a compiler and a runtime. oriented reflective language? The syntax is the set of rules that restricts the legal content In this paper we propose a circular bootstrap infrastruc- of a program’s source code. The semantics are the meaning ture that allows us to build an object-oriented reflective lan- of that content (e.g., how a method invocation will be exe- guage such as Pharo using itself (cf. Section 4). Our solution cuted at run-time). The pragmatics are the range of visible overcomes the metastability issues in an elegant manner by effects and values that are possible during a program execu- transparently breaking the circular definition of the language tion (e.g., the range of available integer values). The com- during its definition: the language bootstrap is described in piler is a program that translates a program’s source code itself using the full power of the language but executed by written in our language to a machine executable run-time a specialized interpreter that manipulates the language ele- representation of that program (cf. Figure 1). The runtime is ments as behavior-less data structures. It also avoids to du- the software that support the execution of a program at run- plicate code in an external VM-based bootstrap by reusing time, providing the builtin structures and functions available the means to create the language’s structures already avail- in the language (e.g., the initial objects of the language) and able in reflective languages. services such as memory management and cross-cutting op- Once bootstrapped we can easily modify the language timizations. Notice that the language syntax, semantics and to change critical parts of the language runtime and extend pragmatics are embedded within the compiler and the run- the language model adding e.g., traits [SDNB03] without time. VM changes and avoiding the staging required by circular definitions of the language (cf. Section 5). A first-class <<complies to>> runtime in our infrastructure provides a high-level API to manipulate the low-level wirings of the language (cf. Section Program's Source Code 6). This allows us to easily extend our bootstrap and thus, our language. Additionally it makes a clear distinction between VM and language concerns. This allows us to modify the Syntax + Semantics + language without changing the VM. Pragmatics We implemented our bootstrapping infrastructure on top of the Pharo language (cf. Section 7). Finally, we evaluate Compiler it by conducting and measuring different experiencies with languages with similar execution as Pharo (cf. Section 8). First, we show how we can bootstrap a Smalltalk implemen- Runtime tation named Candle and extend it with traits; then we boot- strapped from scratch MetaTalk, a Smalltalk implementation Programming Language with mirror based reflection, and the Pharo language that in- Executable Program cludes extensions such as traits and first class instance vari- <<uses>> ables. These languages can then run on a stock VM without Figure 1. How a language is composed. modifications. The co-evolution of VM code and language will be addressed in future work. Based on this definition, we can define bootstrapping as follows: DEFINITION 2 (Language Definition). A language defini- tion DL of a programming language L is a textual repre- 2. Bootstrapping Definitions sentation all elements of L. That is, it is the source code used Bootstrapping is known in many different contexts with to create an executable runtime and compiler for L. different meanings (e.g., a machine’s startup process, the DEFINITION 3 (Programming Language Bootstrap Process). process to build a more complex system from a simpler A bootstrap process of a programming language L is a one). Particularly in programming languages we can see it process that produces a programming language L from a is broadly used with two different meanings: the startup of language definition D. That is, it produces the set of its the language runtime elements (e.g., as in Java’s bootstrap syntax, semantics and pragmatics, its compiler and runtime. class loader [LB98]) and the self-compilation also known as a compiler’s bootstrap. To avoid confusions in terminol- DEFINITION 4 (Circular Bootstrap Process). A circular ogy, this section states the definitions we use in this paper. bootstrap process (or self-bootstrap process) of a program- Piumarta et al. [Piu06] define a programming language as ming language L is a bootstrap process of L where its follows: definition DL is expressed in L. Example: The process that creates a C compiler from duces its syntax, semantics, pragmatics, compiler, runtime assembly source code is the bootstrap of this C compiler. or a combination of them, but not all of them. The process that creates a C compiler from C source code Example: Just by themselves, a self-compiling Smalltalk of the compiler is a circular bootstrap of the C compiler.