source to source compilation via submodules
play

Source-to-Source Compilation via Submodules ELS, 910 May 2016 Hasu, - PowerPoint PPT Presentation

Source-to-Source Compilation via Submodules ELS, 910 May 2016 Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules Tero Hasu 1 Matthew Flatt 2 1 BLDL and University of Bergen 2 PLT and University of Utah Racket specificity


  1. Source-to-Source Compilation via Submodules ELS, 9–10 May 2016 Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules Tero Hasu 1 Matthew Flatt 2 1 BLDL and University of Bergen 2 PLT and University of Utah

  2. Racket specificity warning Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ module-body-transforming macros ▶ #%module-begin ▶ complete sub-form expansion ▶ local-expand ▶ submodules ▶ module , module* , module+

  3. one language environment to rule all targets Racket VM (define-values (_sum-2) (#%closed sum-220 (lambda (arg0-785) 'sum-2 .... C++ MGL_API_FUNC int sum_2( List<int> const& lst ) { List<int> t; return is_empty(lst) ? 0 : .... Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules

  4. motivation for source-to-source compilation Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ deploy via a platform-supported language ▶ perhaps even readable language ▶ easier debugging, safer adoption ▶ e.g.: Linj, mbeddr, STELLA, PureScript ▶ use one language to abstract over multiple others ▶ e.g.: Haxe, Oxygene, STELLA

  5. motivation for Racket hosting of languages definition generating, macro generating, in macro implementations, … Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ stay in Racket’s language environment ▶ reusing its tools ▶ make your language self-extensible ▶ macros: lexically scoped, top-level and local, in modules,

  6. “mouldable” programming http://mouldable.org/ language support: Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ compile-time “concept” implementation composition ▶ compile-time reasoning about properties and behavior ▶ compile-time program self-transformations ▶ for added convenience and syntactic flexibility

  7. self-extensible languages #lang magnolisp (define-syntax-rule (if-not c t e) (if c e t)) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ construction with: Lisps, Sugar*, …? ▶ with most “language workbenches”, not so much

  8. Magnolisp (g)) Hasu, Flatt (BLDL, PLT) } return x; MGL_FUNC int f1_g( int const& x ) { } return f1_g(x); MGL_API_FUNC int f1( int const& x ) { MGL_PROTO int f1_g( int const& x ); (define (g) x) ^(-> int int)) #:: (export (define (f1 x) #:: (foreign)) (typedef int #lang magnolisp Source-to-Source Compilation via Submodules ▶ Rackety syntax ▶ statically typed, with inference à la Hindley-Milner ▶ not “functional”—no function values ▶ runs in Racket, or compiles to C++

  9. running code within Racket #lang racket "Hello World!" Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ #lang line declares the language of a module macroexpand Racket-based core Racket language compile run Racket VM bytecode

  10. defining languages in Racket #lang racket (module reader syntax/module-reader my-lang/main) (provide (all-from-out racket)) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ a #lang is implemented as a module ▶ exports variables, macros, core forms ▶ specifies a reader to turn text into syntax objects e.g., a my-lang just like racket :

  11. getting known syntax for compilation constructions Hasu, Flatt (BLDL, PLT) interpreting Source-to-Source Compilation via Submodules ▶ by read ing and expand ing ▶ by parsing bytecode macroexpand C Racket core Racket mzc compile macroexpand run Racket core Racket Racket VM bytecode Whalesong compile run Racket VM bytecode JavaScript ▶ by evaluating code as AST ▶ by treating code as data, and ▶ e.g., C-Mera ▶ e.g., SC

  12. or: implement a language that exports syntax Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules lis t a.rkt #l a ng m a gnolisp DefV ar ( r equi r e "num- t ypes .rkt " ) def-ls t (define (in t -id x) .... annos I d La mbd a #:: ([ t ype (-> in t in t )] expo rt ) run x) .... .... in t -id .... .... .... macroexpand a.rkt (co r e) translate (module a m a gnolisp/m a in (#%module-begin a. cpp (module m a gnolisp-s2s ra c k e t /b a se #include " a. hpp" (#%module-begin .... MG L _AP I _FUNC in t in t _id(in t cons t & x) { (define-v a lues (def-ls t ) r e t u r n x; (#% a pp lis t (#% a pp DefV ar .... ) .... )) } .... )) .... a. hpp (#% r equi r e "num- t ypes .rkt " ) #include " a _config . hpp" (define-v a lues (in t -id ) .... ))) MG L _AP I _PROTO in t in t _id(in t cons t & x);

  13. language getting its own syntax (provide (rename-out [module-begin #%module-begin])) (define-syntax module-begin (do-some-processing-of stx))) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ( λ (stx)

  14. language getting its own core syntax (define-syntax (module-begin stx) (syntax-case stx () [(_ . forms) (let ([ast (local-expand #'(#%module-begin . forms) 'module-begin '())]) (do-some-processing-of ast))])) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules

  15. language exporting its own core syntax (provide def-lst ....)) Hasu, Flatt (BLDL, PLT) have #%module-begin insert a “submodule” Source-to-Source Compilation via Submodules (require magnolisp/ir-ast) (define def-lst (list (DefVar ....) ....)) .... (module magnolisp-s2s racket/base (module a magnolisp/main (#%module-begin (module magnolisp-s2s racket/base (#%module-begin .... (define-values (def-lst ) separately (#%app list (#%app DefVar ....) ....)) loadable ....)) .... (#%require "num-types.rkt" ) (define-values (int-id) ....)))

  16. Hasu, Flatt (BLDL, PLT) just a curiosity? Source-to-Source Compilation via Submodules ▶ separate compilation ▶ macroexpand and byte-compile only out-of-date modules ▶ #lang itself is in control ▶ decides which compilers it supports ▶ can, e.g., specify options for compilation

  17. getting the most out of Racket infrastructure for the hosted language, give: Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ Racket-compatible name resolution ▶ S-expression syntax

  18. non-Racket core syntax e.g., use a variable binding to identify core-language forms (if #f (#%plain-app #%magnolisp (quote auto)) #f) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ Racket expects only known core forms and bound variable uses (auto) �→ (CORE 'auto) �→

  19. source-to-source compiler implementation Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules mglc (CLI tool) invokes invokes middle end back end middle-end back-end API API invokes outputs invokes invokes inputOf C++ module analyses & IR back-end loader optimizations driver evaluates invokes invokes invokes generates generates pretty submod translator sectioner a.cpp a.hpp printer ▶ Illusyn: term-rewriting strategy combination à la Stratego ▶ another alternative for Racket: Nanopass Framework

  20. Magnolisp-based language: Erda C++ #lang erda/cxx (require "arith.rkt") (define (factorial x) #:: (export ^(->Result Int Int)) #:alert ([bad-arg pre-when (< x 0)]) (cond [(= x 0) 1] [else (* x (factorial (- x 1)))])) 5 ;; => (Good 5) (factorial 5) ;; => (Good 120) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules (factorial -5) ;; => (Bad bad − arg)

  21. source locations define-values: function return type does not match body expression; at (source): (f x) at (syntax): #<syntax:error-3.rkt:9:2 (#%app f x)> in (source): (define (g x) #:: (^(-> Int Long) export) (f x)) in (syntax): #<syntax:error-3.rkt:7:0 (define-values (g) (let-value...> declared return type: Long actual return type: Int Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules

  22. other compile-time mechanisms based on macros (require (for-syntax "config.rkt")) (static-cond [qt? (define-mapped-type String #:mapped-to QString [string-index #:mapped-to QString-indexOf] ....)] [cxx? (define-mapped-type String #:mapped-to std::wstring [string-index #:mapped-to std::wstring-find] ....)]) Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules ▶ conditional compilation ▶ “mapped types”

  23. synopsis approach Have macros encode foreign core language in terms of Racket’s. Implement a #%module-begin to expand and process a module body, and embed an AST-containing submodule for an external compiler. achieves Languages (i.e., #lang definitions) getting to decide which compilers they target. Separate macroexpansion and byte compilation. software and documentation raco pkg install magnolisp contact tero@ii.uib.no mflatt@cs.utah.edu Hasu, Flatt (BLDL, PLT) Source-to-Source Compilation via Submodules

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend