Building Code with Dynamic Staging

When creating a new domain-specific language (DSL) it is common to embed it as a part of a flexible host language, rather than creating it entirely from scratch. The semantics of an embedded DSL (EDSL) is either given directly as a set of functions (shallow embedding), or an AST is constructed that is later processed (deep embedding). Typically, the deep embedding is used when the EDSL specifies domain-specific optimizations (DSO) in a form of AST transformations. In this paper we show that deep embedding is not necessary to specify most optimizations. We define language semantics as action functions that are executed during parsing. These actions build incrementally a new, arbitrary complex program function. The EDSL designer is able to specify many aspects of the semantics as a runnable code, such as variable scoping rules, custom type checking, arbitrary control flow structures, or DSO. A sufficiently powerful staging mechanism helps assembling the code from different actions, as well as evaluate the semantics in arbitrarily many stages. In the end, we obtain code that is as efficient as one written by hand. We never create any object representation of the code. No external traversing algorithm is used to process the code. All program fragments are functions with their entire semantics embedded within the function bodies. This approach allows reusing the code between EDSL and the host language, as well as combining actions of many different EDSLs.

[1]  Simon L. Peyton Jones,et al.  Scripting COM components in Haskell , 1998, Proceedings. Fifth International Conference on Software Reuse (Cat. No.98TB100203).

[2]  Martin Odersky,et al.  Spiral in scala: towards the systematic construction of generators for performance libraries , 2014, GPCE '13.

[3]  Sam Tobin-Hochstadt,et al.  Languages as libraries , 2011, PLDI '11.

[4]  J. Drife,et al.  The third edition , 2014 .

[5]  Erik Meijer Server side web scripting in Haskell , 2000, J. Funct. Program..

[6]  Murray Hill,et al.  Yacc: Yet Another Compiler-Compiler , 1978 .

[7]  Daan Leijen,et al.  Domain specific embedded compilers , 1999, DSL '99.

[8]  Ronald L. Rivest,et al.  Introduction to Algorithms, third edition , 2009 .

[9]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997, PEPM.

[10]  Jacques Carette,et al.  Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages , 2007, Journal of Functional Programming.

[11]  Philipp Slusallek,et al.  Specialization through dynamic staging , 2015 .

[12]  Stanley B. Lippman C++ gems , 1996 .

[13]  John C. Reynolds User-defined types and procedural data structures as complementary approaches to data abstraction , 1994 .

[14]  Walid Taha,et al.  An Idealized MetaML: Simpler, and More Expressive , 1999, ESOP.

[15]  Stephan Herhut,et al.  Auxiliary computations : a framework for a step-wise, non-disruptive introduction of static guarantees to untyped programs using partial evaluation techniques , 2010 .

[16]  Martin Odersky,et al.  Yin-yang: concealing the deep embedding of DSLs , 2014, GPCE 2014.

[17]  Friedrich L. Bauer,et al.  Revised report on the algorithm language ALGOL 60 , 1963, CACM.

[18]  Emir Pasalic,et al.  DSL implementation using staging and monads , 1999, DSL '99.

[19]  Alan Bawden,et al.  Quasiquotation in Lisp , 1999, PEPM.

[20]  Daniel J. Rosenkrantz,et al.  Attributed Translations , 1974, J. Comput. Syst. Sci..

[21]  Kunle Olukotun,et al.  Language virtualization for heterogeneous parallel computing , 2010, OOPSLA.

[22]  Dawson R. Engler,et al.  C: a language for high-level, efficient, and machine-independent dynamic code generation , 1995, POPL '96.

[23]  Philipp Slusallek,et al.  Shallow embedding of DSLs via online partial evaluation , 2016 .

[24]  Mary Sheeran,et al.  Lava: hardware design in Haskell , 1998, ICFP '98.

[25]  Christian Hofer,et al.  Modular domain-specific language components in scala , 2010, GPCE '10.

[26]  Martin Odersky,et al.  Lightweight modular staging: a pragmatic approach to runtime code generation and compiled DSLs , 2010, GPCE '10.

[27]  Paul Hudak,et al.  An Experiment in Software Prototyping Productivity , 1994 .

[28]  Kunle Olukotun,et al.  Building-Blocks for Performance Oriented DSLs , 2011, DSL.

[29]  Krzysztof Czarnecki,et al.  DSL Implementation in MetaOCaml, Template Haskell, and C++ , 2003, Domain-Specific Program Generation.

[30]  Walid Taha,et al.  MetaML and multi-stage programming with explicit annotations , 2000, Theor. Comput. Sci..

[31]  Andrew W. Appel,et al.  Compiling with Continuations , 1991 .

[32]  Peter Thiemann,et al.  An embedded domain-specific language for type-safe server-side web scripting , 2005, TOIT.

[33]  Vikram S. Adve,et al.  LLVM: a compilation framework for lifelong program analysis & transformation , 2004, International Symposium on Code Generation and Optimization, 2004. CGO 2004..