Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Growing an Abstract Grammar: Teaching Language ...

Growing an Abstract Grammar: Teaching Language Language Engineering

Avatar for ICOOOLPS Workshop

ICOOOLPS Workshop

July 18, 2016
Tweet

More Decks by ICOOOLPS Workshop

Other Decks in Education

Transcript

  1. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 Growing an Abstract Grammar Teaching Language Engineering Theo D'Hondt Software Languages Lab Vrije Universiteit Brussel soft.vub.ac.be [email protected] Growing an abstract grammar: teaching language engineering
  2. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 8 Teaching language engineering http://soft.vub.ac.be/PLE
  3. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 9 Teaching language engineering http://soft.vub.ac.be/PLE •No nested scope •No garbage collection •Static & weak typing •No proper tail calls
  4. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 10 Teaching language engineering http://soft.vub.ac.be/PLE •No nested scope •No garbage collection •Static & weak typing •No proper tail calls version 6: partial evaluation version 7: iterative constructs version 8: lexical addressing version 9: garbage collection version 10: proper tail recursion version 11: internal caches version 12: optimizing C
  5. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 11 SLIP (begin (define meta-level-eval eval) (define eval ()) (define (loop output environment) (define rollback environment) (define (evaluate expression) (define (abort message qualifier) (display message) (loop qualifier rollback)) (define (bind-variable variable value) (define binding (cons variable value)) (set! environment (cons binding environment))) (define (bind-parameters parameters arguments) (if (symbol? parameters) (bind-variable parameters arguments) (if (pair? parameters) (begin (define variable (car parameters)) (define value (car arguments )) (bind-variable variable value) (bind-parameters (cdr parameters) (cdr arguments)))))) (define (evaluate-sequence expressions) (define head (car expressions)) (define tail (cdr expressions)) (define value (evaluate head)) (if (null? tail) value (evaluate-sequence tail))) (define (build-closure parameters expressions) (define lexical-environment environment) (define (closure . arguments) (define dynamic-environment environment) (set! environment lexical-environment) (bind-parameters parameters arguments) (define value (evaluate-sequence expressions)) (set! environment dynamic-environment) value) closure) (define (evaluate-application operator) (lambda operands (apply (evaluate operator) (map evaluate operands)))) (define (evaluate-begin . expressions) (evaluate-sequence expressions)) (define (evaluate-define pattern . expressions) (if (symbol? pattern) (begin (define value (evaluate (car expressions))) (define binding (cons pattern value)) (set! environment (cons binding environment)) value) (begin (define binding (cons (car pattern) ())) (set! environment (cons binding environment)) (define closure (build-closure (cdr pattern) expressions)) (set-cdr! binding closure) closure))) (define (evaluate-if predicate consequent . alternate) (if (evaluate predicate) (evaluate consequent) (if (null? alternate) () (evaluate (car alternate))))) (define (evaluate-lambda parameters . expressions) (build-closure parameters expressions)) (define (evaluate-quote expression) expression) (define (evaluate-set! variable expression) (define value (evaluate expression)) (define binding (assoc variable environment)) (if (pair? binding) (set-cdr! binding value) (abort "inaccessible variable: " variable))) (define (evaluate-variable variable) (define binding (assoc variable environment)) (if (pair? binding) (cdr binding) (meta-level-eval variable))) (define (evaluate-while predicate body) (define (iterate value) (if (evaluate predicate) (iterate (evaluate body)) value)) (iterate ())) (if (symbol? expression) (evaluate-variable expression) (if (pair? expression) (begin (define operator (car expression)) (define operands (cdr expression)) (apply (if (eq? operator 'begin) evaluate-begin (if (eq? operator 'define) evaluate-define (if (eq? operator 'if) evaluate-if (if (eq? operator 'lambda) evaluate-lambda (if (eq? operator 'quote) evaluate-quote (if (eq? operator 'set!) evaluate-set! (if (eq? operator 'while) evaluate-while (evaluate-application operator)))))))) operands)) expression))) (display output) (newline) (display ">") (set! eval evaluate) (loop (evaluate (read)) environment)) (loop "Meta-Circular Slip" ())) 100 loc
  6. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 define (evaluate-define pattern . expressions) (lambda (continue environment) (define (continue-after-expression value environment-after-expression) (define binding (cons pattern value)) (continue value (cons binding environment-after-expression))) (if (symbol? pattern) (evaluate (car expressions) continue-after-expression environment) (let* ((binding (cons (car pattern) '())) (environment (cons binding environment)) (procedure (make-procedure (cdr pattern) expressions environment))) (set-cdr! binding procedure) (continue procedure environment))))) (define (evaluate-if predicate consequent . alternative) (lambda (continue environment) (define (continue-after-predicate boolean environment-after-predicate) (if (eq? boolean #f) (if (null? alternative) (continue '() environment-after-predicate) (evaluate (car alternative) continue environment-after-predicate)) (evaluate consequent continue environment-after-predicate))) (evaluate predicate continue-after-predicate environment))) ... (define (evaluate expression continue environment) (cond ((symbol? expression) (evaluate-variable expression continue environment)) ((pair? expression) (let ((operator (car expression)) (operands (cdr expression))) ((apply (case operator ((begin) evaluate-begin ) ((define) evaluate-define) ((if) evaluate-if ) ((lambda) evaluate-lambda) ((quote) evaluate-quote ) ((set!) evaluate-set! ) ((while) evaluate-while ) (else (evaluate-application operator))) operands) continue environment))) (else (continue expression environment)))) (display output) (newline) (display ">>>") (evaluate (read) loop environment)) (loop "cpSlip version 0" '())) (begin (define meta-level-eval eval) (define (loop output environment) (define rollback environment) (define (error message qualifier) (display message) (loop qualifier rollback)) (define (wrap-native-procedure native-procedure) (lambda (arguments continue environment) (define native-value (apply native-procedure arguments)) (continue native-value environment))) (define (bind-variable variable value environment) (define binding (cons variable value)) (cons binding environment)) (define (bind-parameters parameters arguments environment) (if (symbol? parameters) (bind-variable parameters arguments environment) (if (pair? parameters) (let* ((variable (car parameters)) (value (car arguments )) (environment (bind-variable variable value environment))) (bind-parameters (cdr parameters) (cdr arguments) environment)) environment))) (define (evaluate-sequence expressions continue environment) (define head (car expressions)) (define tail (cdr expressions)) (define (continue-with-sequence value environment) (evaluate-sequence tail continue environment)) (if (null? tail) (evaluate head continue environment) (evaluate head continue-with-sequence environment))) (define (make-procedure parameters expressions environment) (lambda (arguments continue dynamic-environment) (define (continue-after-sequence value environment-after-sequence) (continue value dynamic-environment)) (define lexical-environment (bind-parameters parameters arguments environment)) (evaluate-sequence expressions continue-after-sequence lexical-environment))) (define (evaluate-application operator) (lambda operands (lambda (continue environment) (define (continue-after-operator procedure environment-after-operator) (define (evaluate-operands operands arguments environment) (define (continue-with-operands value environment-with-operands) (evaluate-operands (cdr operands) (cons value arguments) environment-with-operands)) (if (null? operands) (procedure (reverse arguments) continue environment) (evaluate (car operands) continue-with-operands environment))) (evaluate-operands operands '() environment-after-operator)) (evaluate operator continue-after-operator environment)))) (define (evaluate-begin . expressions) (lambda (continue environment) (evaluate-sequence expressions continue environment))) 12 cpSLIP 150 loc
  7. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 static NIL_type evaluate_expression(EXP_type Expression, CNT_type Continuation, ENV_type Environment) { TAG_type tag; tag = Tag_of(Expression); switch (tag) { case PAI_tag: evaluate_form(Expression, Continuation, Environment); case SYM_tag: evaluate_symbol(Expression, Continuation, Environment); case CHA_tag: case FLS_tag: case NUL_tag: case NBR_tag: case REA_tag: case STR_tag: case TRU_tag: case VEC_tag: evaluate_value(Expression, Continuation, Environment); case CFN_tag: case CNT_tag: case ENV_tag: case NAT_tag: case PRC_tag: case USP_tag: Main_Error_Tag(ILT_error_string, tag); } Main_Error_Tag(IXT_error_string, tag); } 13 1500 loc cSLIP
  8. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 static NIL_type evaluate_expression(EXP_type Expression, CNT_type Continuation, ENV_type Environment) { TAG_type tag; tag = Tag_of(Expression); switch (tag) { case PAI_tag: evaluate_form(Expression, Continuation, Environment); case SYM_tag: evaluate_symbol(Expression, Continuation, Environment); case CHA_tag: case FLS_tag: case NUL_tag: case NBR_tag: case REA_tag: case STR_tag: case TRU_tag: case VEC_tag: evaluate_value(Expression, Continuation, Environment); case CFN_tag: case CNT_tag: case ENV_tag: case NAT_tag: case PRC_tag: case USP_tag: Main_Error_Tag(ILT_error_string, tag); } Main_Error_Tag(IXT_error_string, tag); } 14 1500 loc cSLIP λ lifting
  9. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 15 Abstract grammar In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language [Wikipedia] read eval print ‑ ‑ ‑ ‑ text s-expr s-expr text
  10. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 16 Abstract grammar (cont'd) Abstract grammar formalizes the interface to a programming language interpreter; it structures the language's semantics K ∈ Con constants I ∈ Ide variables ε ∈ E expressions ε → K | I | (ε0 ε*) | (define I ε) | (define (I0 I*) ε+) | (define (I0 I+ . I) ε+) | (define (I0 . I) ε+) | (if ε0 ε1 ) | (if ε0 ε1 ε2 ) | (lambda (I*) ε+) | (lambda (I+ . I) ε+) | (lambda I ε+) | (set! I ε) ℇ⟦(E0 E*)⟧ = λκ.ℇ⟦E0⟧ (λε.ε ∈ F → ℇ*⟦E*⟧ (λε*.applicate ε ε* κ) ⟨⟩ , wrong “procedure expected”) ℇ⟦(begin E+)⟧ = λκ.send ℇ+⟦ E+⟧ κ ℇ⟦K⟧ = send K⟦K⟧ κ ℇ⟦(define I E)⟧ = λκ.def I ∅ (λε∅.ℇ⟦E⟧ (λε.set I ε κ)) ℇ⟦(define (I0 I*) E+)⟧ = λκ.def I0 ∅ (λε∅.ℇ⟦(lambda (I*) E+)⟧ (λφ.set I0 φ κ)) ℇ⟦(define (I0 I+ . I) E+)⟧ = λκ.def I0 ∅ (λε∅.ℇ⟦(lambda (I+ . I) E+)⟧ (λφ.set I0 φ κ)) ℇ⟦(define (I0 . I) E+)⟧ = λκ.def I0 ∅ (λε∅.ℇ⟦(lambda I E+)⟧ (λφ.set I0 φ κ)) ℇ⟦(if E0 E1)⟧ = λκ.ℇ⟦E0⟧ (λε.λρ.ε = false → (send ∅ κ)ρ , (ℇ⟦E1⟧ (λε.λρ∅.(send ε κ)ρ))ρ) ℇ⟦(if E0 E1 E2)⟧ = λκ.ℇ⟦E0⟧ (λε.λρ.((ε = false → ℇ⟦E2⟧ , ℇ⟦E1⟧) (λε.λρ∅.(send ε κ)ρ))ρ) ℇ⟦(lambda (I*) E+)⟧ = λI*E+.λκ.λρ.(send (λε*κcall.λρcall.#I* = #ε* → (bind I* ε* (λε∅.body E+ κcall ρcall))ρ , wrong “non-matching argument list”) κ)ρ ℇ⟦(lambda (I+ . I) E+)⟧ = λI*IE+.λκ.λρ.(send (λε*κcall.λρcall.#I* <= #ε* → (bind I* ε* (λε*'.def I ε*' (λε∅.body E+ κcall ρcall)))ρ , wrong “non-matching argument list”) κ)ρ ℇ⟦(lambda I E+)⟧ = λIE+κ.λρ.(send (λε*κcall.λρcall.def I ε* (λε∅.(body E+ κcall)ρcall)ρ) κ)ρ ℇ⟦(quote E)⟧ = λκ.send K⟦E⟧ κ ℇ⟦(set! I E)⟧ = λκ.ℇ⟦E⟧ (λε.set I ε κ) ℇ⟦I⟧ = λκ.get I κ ℇ+⟦E+⟧ = λκ.ℇ⟦E+↓1⟧ (λε.#E+ = 1 → (send ε κ) , ℇ+⟦E+†1⟧ κ) ℇ*⟦E*⟧ = λκl.#E* = 0 → send l κ , ℇ⟦E* ↓ 1⟧ (λε.ℇ*⟦E*†1⟧ κ (l § ⟨ε⟩))
  11. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 17 Abstract grammar w/o sexpr boolean ::= « TRU » boolean ::= « FLS » character ::= « CHR , native_character » integer ::= « INT , native_integer » real ::= « REA , native_real » string ::= « STR , native_string » null ::= « NUL » symbol ::= « SYM , native_string » procedure ::= « PRC , parameter_vector , body_vector , environment » procedure ::= « PRV , parameter_vector , body_vector , environment » native ::= « NAT , native_function » pair ::= « PAI , car_expression , cdr_expression » vector ::= « VEC , expression* » variable ::= « VAR , symbol » application ::= « APL , operator_expression , operand_vector » begin_form ::= « BEG , expression+ » define_form ::= « DFV , symbol , define_expression » define_form ::= « DFF , symbol , parameter_vector , body_vector » define_form ::= « DFZ , symbol , parameter_vector , body_vector » if_form ::= « IFF , predicate_expression , then_expression , else_expression » if_form ::= « IFZ , predicate_expression , then_expression » lambda_form ::= « LMB , parameter_vector , body_vector » lambda_form ::= « LMV , parameter_vector , body_vector » set_form ::= « SET , symbol , set_expression » s-expr ::= null | atom | symbol | list null ::= () atom ::= number | " string " symbol ::= string list ::= ( s-expr+ [ . s-expr ] ) a-expr ::= tag native | tag a-expr* s-expression abstract expression SLIP abstract grammar
  12. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 18 Partial evaluation read eval print ‑ ‑ ‑ text s-expr s-expr text p-eval ‑ ‑ a-expr
  13. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 19 Example: lexical addressing boolean ::= « TRU » boolean ::= « FLS » character ::= « CHR , native_character » integer ::= « INT , native_integer » real ::= « REA , native_real » string ::= « STR , native_string » null ::= « NUL » symbol ::= « SYM , native_string » procedure ::= « PRC , size , frame_size , body_vector , environment_vector » procedure ::= « PRV , size , frame_size , body_vector , environment_vector » native ::= « NAT , native_function » pair ::= « PAI , car_expression , cdr_expression » vector ::= « VEC , expression* » variable ::= « VLC , offset » variable ::= « VGL , scope , offset» application ::= « APL , operator_expression , operand_vector » begin_form ::= « BEG , begin_vector » define_form ::= « DFV , offset , define_expression » define_form ::= « DFF , offset , size , frame_size , body_vector » define_form ::= « DFZ , offset , size , frame_size , body_vector » if_form ::= « IFF , predicate_expression , then_expression , else_expression » if_form ::= « IFZ , predicate_expression , then_expression » lambda_form ::= « LMB , size , frame_size , body_vector » lambda_form ::= « LMV , size , frame_size , body_vector » set_form ::= « SLC , offset , set_expression » set_form ::= « SGL , scope , offset , set_expression » SLIP abstract grammar —— natives —— 2 1 (p) ( ... ) (q r) ( ... ) 0 1 1 2 3 4 2 1 3 (s.t) ( ... ) 2 1 2 3 4 '(2 3 4) 1 5 1 2 3 3 (begin (define x 1) (define y 2) (define (f p) (+ p x)) (define (g q r) (define a 3) (define b 4) (define (h s . t) (define c 5) (f (+ c (car t)))) (h q r a b)) (g x y))
  14. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 20 Example: proper tail calls (define (unify-match p1 p2 frame) (cond ((eq? frame 'failed) 'failed) ((equal? p1 p2) frame) ((var? p1) (extend-if-possible p1 p2 frame)) ((var? p2) (extend-if-possible p2 p1 frame)) ((and (pair? p1) (pair? p2)) (unify-match (cdr p1) (cdr p2) (unify-match (car p1) (car p2) frame))) (else 'failed))) (define (extend-if-possible var val frame) (let ((binding (binding-in-frame var frame))) (cond (binding (unify-match (binding-value binding) val frame)) ((var? val) (let ((binding (binding-in-frame val frame))) (if binding (unify-match var (binding-value binding) frame) (extend var val frame)))) ((depends-on? val var frame) 'failed) (else (extend var val frame)))))
  15. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 21 Example: proper tail calls (define (unify-match p1 p2 frame) (cond ((eq? frame 'failed) 'failed) ((equal? p1 p2) frame) ((var? p1) (extend-if-possible p1 p2 frame)) ((var? p2) (extend-if-possible p2 p1 frame)) ((and (pair? p1) (pair? p2)) (unify-match (cdr p1) (cdr p2) (unify-match (car p1) (car p2) frame))) (else 'failed))) (define (extend-if-possible var val frame) (let ((binding (binding-in-frame var frame))) (cond (binding (unify-match (binding-value binding) val frame)) ((var? val) (let ((binding (binding-in-frame val frame))) (if binding (unify-match var (binding-value binding) frame) (extend var val frame)))) ((depends-on? val var frame) 'failed) (else (extend var val frame)))))
  16. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 22 (define (unify-match p1 p2 frame) (cond ((eq? frame 'failed) 'failed) ((equal? p1 p2) frame) ((var? p1) (extend-if-possible p1 p2 frame)) ((var? p2) (extend-if-possible p2 p1 frame)) ((and (pair? p1) (pair? p2)) (unify-match (cdr p1) (cdr p2) (unify-match (car p1) (car p2) frame))) (else 'failed))) (define (extend-if-possible var val frame) (let ((binding (binding-in-frame var frame))) (cond (binding (unify-match (binding-value binding) val frame)) ((var? val) (let ((binding (binding-in-frame val frame))) (if binding (unify-match var (binding-value binding) frame) (extend var val frame)))) ((depends-on? val var frame) 'failed) (else (extend var val frame))))) boolean ::= « TRU » boolean ::= « FLS » character ::= « CHR , native_character » integer ::= « INT , native_integer » real ::= « REA , native_real » string ::= « STR , native_string » null ::= « NUL » symbol ::= « SYM , native_string » procedure ::= « PRC , parameter_vector , body_vector , environment » procedure ::= « PRV , parameter_vector , body_vector , environment » native ::= « NAT , native_function » pair ::= « PAI , car_expression , cdr_expression » vector ::= « VEC , expression* » variable ::= « VAR , symbol » application ::= « APL , operator_expression , operand_vector » application ::= « APL* , operator_expression , operand_vector » begin_form ::= « BEG , expression+ » define_form ::= « DFV , symbol , define_expression » define_form ::= « DFF , symbol , parameter_vector , body_vector » define_form ::= « DFZ , symbol , parameter_vector , body_vector » if_form ::= « IFF , predicate_expression , then_expression , else_expression » if_form ::= « IFF* , predicate_expression , then_expression , else_expression » if_form ::= « IFF , predicate_expression , then_expression , else_expression » if_form ::= « IFF* , predicate_expression , then_expression , else_expression » lambda_form ::= « LMB , parameter_vector , body_vector » lambda_form ::= « LMV , parameter_vector , body_vector » set_form ::= « SET , symbol , set_expression » SLIP abstract grammar Example: proper tail calls
  17. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 23 Abstract grammar 2.0 s-expr values frames forms spec forms
  18. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 24 Abstract grammar 2.0 s-expr values frames forms spec forms
  19. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 25 Universal memory model tag size pointer “chunk” ➜ ➜ ➜ ➜ “cell” ➜ ➜ ➜ ➜
  20. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 26 Universal memory model bsy pointer tag size ptr raw immediate number immediate value 1 0 0 null true false unspecified
  21. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 P C 㱺 C A ◦ A ◦ A A A A P P C 㱺 P C A ◦ A ◦ P 㱺 C C A A A A A A P 27 Schorr-Waite [1967] C P C 㱺 A A P An efficient machine-independent procedure for garbage collection in various list structures H. Schorr, W. M. Waite CACM Volume 10 Issue 8, Aug. 1967 Pages 501-506 An efficient, incremental, automatic garbage collector L. Peter Deutsch, Daniel G. Bobrow CACM Volume 19 Issue 9, Sept. 1976 Pages 522-526
  22. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 28 Morris [1978] 0 1 6 2 3 1 12 6 1 2 6 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 free root 15 1 0 14 9 2 2 1 2 13 1 8 3 7 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 free root 15 1 0 1 2 2 3 0 6 2 1 2 2 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 free root 15 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 free root 9 0 1 2 3 0 6 6 2 2 0 6 SWEEP MARK COMPACT A time- and space-efficient garbage compaction algorithm F. Lockwood Morris CACM Volume 21 Issue 8, Aug. 1978 Pages 662-66
  23. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 C C H H 㱺 29 Mark-and-sweep GC C 㱺 C A A C 㱺 H H C C 㱺 C H H
  24. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 C C H H 㱺 30 Mark-and-sweep GC C 㱺 C A A C 㱺 H H C C 㱺 C H H Schorr-Waite
  25. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 C C H H 㱺 31 Mark-and-sweep GC C 㱺 C A A C 㱺 H H C C 㱺 C H H Schorr-Waite Morris
  26. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 static VOI_type traverse_and_mark(void) { CEL_type bits; PTR_type current, pointer; UNS_type size; for (current = Head_pointer;;) { bits = current->cel; switch (get_last_2_bits(bits)) { case bp_bits: pointer = current->ptr; if ((pointer < Head_pointer) || (pointer > Tail_pointer)) { current--; continue; } bits = pointer->cel; switch (get_last_3_bits(bits)) { case rbp_bits: pointer->cel = make_busy(current); current->cel = bits; size = get_size(bits); if (size) current = pointer + size; else current--; continue; case rBp_bits: case RBp_bits: pointer = make_free(bits); bits = pointer->cel; case Rbp_bits: pointer->cel = make_busy(current); current->cel = bits; current--; } continue; case Bp_bits: current = make_free(bits); if (current == Head_pointer) return; case bP_bits: case BP_bits: current--; }}} 32 traverse-and-mark
  27. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 33 Some impressions 400000000 2 3,26 200000000 6 3,15 100000000 12 3,08 50000000 28 3,04 25000000 56 3,11 12500000 118 3,31 6250000 264 3,83 3125000 682 5,38 #cells #gc time Start Slip [400000000] >(load "CODE/mceval.slip") Meta-Circular Slip level 1>>(load "CODE/mceval.slip") Meta-Circular Slip level 2>>(load "CODE/mceval.slip") Meta-Circular Slip level 3>>(load "CODE/mceval.slip") Meta-Circular Slip level 4>>(+ 1 2) 3 level 4>>(exit) GC count = 2 elapsed = 3.262436 Stop Slip
  28. The 11th Implementation, Compilation, Optimization of Object-Oriented Languages, Programs and

    Systems Workshop Rome, July 18th 2016 34 SLIP in asm.js A Performant Scheme Interpreter in asm.js. / Van Es, Noah; Nicolay, Jens; Stiévenart, Quentin; D'Hondt, Theo; De Roover, Coen. Proceedings of the 31st ACM Symposium on Applied Computing, Programming Languages Track (SAC 2016). ACM, 2016. p. 1944-1951. Frequently calling in and out of asm.js modules compiled ahead-of-time causes a major overhead in terms of performance. Integrating asm.js into an existing JavaScript application is therefore only beneficial if all computation can reside in a single asm.js module A macro preprocessor is necessary to alleviate the challenges in readability, maintainability and performance when writing asm.js by hand Using asm.js to improve the efficiency of web applications comes down to a tradeoff between development effort and performance At nearly native performance, writing asm.js by hand appears to be the best solution to get the most out of an application that can run on a billion-user VM This paper presents the implementation of an efficient interpreter for a Scheme-like language using manually written asm.js code. The asm.js specification defines an optimizable subset of JavaScript which has already served well as a compilation target for web applications where performance is critical. However, its usage as a human-writable language that can be integrated into existing projects to improve performance has remained largely unexplored. We therefore apply this strategy to optimize the implementation of an interpreter. We also discuss the feasibility of this approach, as writing asm.js by hand is generally not its recommended use-case. We therefore present a macro system to solve the challenges we encounter. The resulting interpreter is compared to the original C implementation and its compiled equivalent in asm.js. This way, we evaluate whether manual integration with asm.js provides the necessary performance to bring larger applications and runtimes to the web.