Next: , Up: compiler lifting   [Index]


17.20.1 Introduction to clambda lifting

The purpose of this compiler pass is to partition the input expression into: the code that will be compiled to code objects implementing closure objects; code that will be compiled to a code object implementing the main body of the expression. As example, the library:

(library (clambda-lifting-demo-0)
  (export a b c d)
  (import (rnrs))
  (define (a) '1)
  (define (b) '2)
  (define (c) '3)
  (define d 4))

is expanded into:

(library-letrec*
    ((a a.loc (lambda () '1))
     (b b.loc (lambda () '2))
     (c c.loc (lambda () '3))
     (d d.loc '4))
  (quote #!void))

before this compiler pass it is transformed into:

(fix ((a_0 (closure-maker (lambda () (constant 1)) no-freevars))
      (b_0 (closure-maker (lambda () (constant 2)) no-freevars))
      (c_0 (closure-maker (lambda () (constant 3)) no-freevars)))
  (seq
    (funcall (primref $set-symbol-value/proc!)
             (constant a.loc) a_0)
    (funcall (primref $init-symbol-value!)
             (constant b.loc) b_0)
    (funcall (primref $init-symbol-value!)
             (constant c.loc) c_0)
    (bind ((d_0 (constant 4)))
      (seq
        (funcall (primref $init-symbol-value!)
                 (constant d.loc) d_0)
        (constant #!void)))))

and after this compiler pass it will become:

(codes
  ((lambda (label: asmlabel:c:clambda) () (constant 3))
   (lambda (label: asmlabel:b:clambda) () (constant 2))
   (lambda (label: asmlabel:a:clambda) () (constant 1)))
  (seq
    (funcall (primref $set-symbol-value/proc!)
      (constant a.loc)
      (closure-maker (code-loc asmlabel:a:clambda) no-freevars))
    (funcall (primref $init-symbol-value!)
      (constant b.loc)
      (closure-maker (code-loc asmlabel:b:clambda) no-freevars))
    (funcall (primref $init-symbol-value!)
      (constant c.loc)
      (closure-maker (code-loc asmlabel:c:clambda) no-freevars))
    (bind ((d_0 (constant 4)))
      (seq
        (funcall (primref $init-symbol-value!)
          (constant d.loc) d_0)
        (constant #!void)))))

We see the result of the transformation is a struct of type codes in which the clambda forms have been extracted and separated; in addition, the top level fix struct has been removed and references to the fix bindings have been replaced by closure-maker forms, this happens because all the functions are combinators.

The closure-maker form:

(closure-maker (code-loc asmlabel:a:clambda) no-freevars)

will be compiled to code that returns a closure object implementing the function a, with assembly entry point label asmlabel:a:clambda.


Next: , Up: compiler lifting   [Index]