| 1 |
;;; one possible implementation strategy for procedures is via closure |
| 2 |
;;; conversion. |
| 3 |
|
| 4 |
;;; Lambda does many things at the same time: |
| 5 |
;;; 1) It creates a procedure object (ie. one that passes procedure?) |
| 6 |
;;; 2) It contains both code (what to do when applied) and data (what |
| 7 |
;;; variables it references. |
| 8 |
;;; 3) The procedure object, in addition to passing procedure?, can be |
| 9 |
;;; applied to arguments. |
| 10 |
|
| 11 |
;;; First step: separate code from data: |
| 12 |
;;; convert every program containing lambda to a program containing |
| 13 |
;;; codes and closures: |
| 14 |
;;; (let ([f (lambda () 12)]) (procedure? f)) |
| 15 |
;;; => |
| 16 |
;;; (codes ([f-code (code () () 12)]) |
| 17 |
;;; (let ([f (closure f-code)]) |
| 18 |
;;; (procedure? f))) |
| 19 |
;;; |
| 20 |
;;; The codes binds code names to code points. Every code |
| 21 |
;;; is of the form (code (formals ...) (free-vars ...) body) |
| 22 |
;;; |
| 23 |
;;; sexpr |
| 24 |
;;; => recordize |
| 25 |
;;; recognize lambda forms and applications |
| 26 |
;;; => |
| 27 |
;;; (let ([y 12]) |
| 28 |
;;; (let ([f (lambda (x) (fx+ y x))]) |
| 29 |
;;; (fx+ (f 10) (f 0)))) |
| 30 |
;;; => convert closures |
| 31 |
;;; (let ([y 12]) |
| 32 |
;;; (let ([f (closure (code (x) (y) (fx+ x y)) y)]) |
| 33 |
;;; (fx+ (call f 10) (call f 0)) |
| 34 |
;;; => lift codes |
| 35 |
;;; (codes ([code0 (code (x) (y) (fx+ x y))]) |
| 36 |
;;; (let ([y 12]) |
| 37 |
;;; (let ([f (closure code0 y)]) |
| 38 |
;;; (fx+ (call f 10) (call f 0))))) |
| 39 |
;;; => code generation |
| 40 |
;;; 1) codes form generates unique-labels for every code and |
| 41 |
;;; binds the names of the code to these labels. |
| 42 |
;;; 2) Every code object has a list of formals and a list of free vars. |
| 43 |
;;; The formals are at stack locations -4(%esp), -8(%esp), -12(%esp), ... |
| 44 |
;;; The free vars are at -2(%edi), 2(%edi), 6(%edi), 10(%edi) ... |
| 45 |
;;; These are inserted in the environment and then the body of the code |
| 46 |
;;; is generated. |
| 47 |
;;; 3) A (closure code-name free-vars ...) is generated the same way a |
| 48 |
;;; (vector val* ...) is generated: First, the code-label and the free |
| 49 |
;;; variables are placed at 0(%ebp), 4(%ebp), 8(%ebp), etc.. |
| 50 |
;;; A closure pointer is placed in %eax, and %ebp is incremented to the |
| 51 |
;;; next boundary. |
| 52 |
;;; 4) A (call f arg* ...) does the following: |
| 53 |
;;; a) evaluates the args and places them at contiguous stack locations |
| 54 |
;;; si-8(%esp), si-12(%esp), ... (leaving room for two values). |
| 55 |
;;; b) The value of the current closure pointer, %edi, is saved on the |
| 56 |
;;; stack at si(%esp). |
| 57 |
;;; c) The closure pointer of the callee is loaded in %edi. |
| 58 |
;;; d) The value of %esp is adjusted by si |
| 59 |
;;; e) An indirect call to -6(%edi) is issued. |
| 60 |
;;; f) After return, the value of %esp is adjusted back by -si |
| 61 |
;;; g) The value of the closure pointer is restored. |
| 62 |
;;; The returned value is still in %eax. |
| 63 |
|
| 64 |
(add-tests-with-string-output "procedure?" |
| 65 |
[(procedure? (lambda (x) x)) => "#t\n"] |
| 66 |
[(let ([f (lambda (x) x)]) (procedure? f)) => "#t\n"] |
| 67 |
[(procedure? (make-vector 0)) => "#f\n"] |
| 68 |
[(procedure? (make-string 0)) => "#f\n"] |
| 69 |
[(procedure? (cons 1 2)) => "#f\n"] |
| 70 |
[(procedure? #\S) => "#f\n"] |
| 71 |
[(procedure? ()) => "#f\n"] |
| 72 |
[(procedure? #t) => "#f\n"] |
| 73 |
[(procedure? #f) => "#f\n"] |
| 74 |
[(string? (lambda (x) x)) => "#f\n"] |
| 75 |
[(vector? (lambda (x) x)) => "#f\n"] |
| 76 |
[(boolean? (lambda (x) x)) => "#f\n"] |
| 77 |
[(null? (lambda (x) x)) => "#f\n"] |
| 78 |
[(not (lambda (x) x)) => "#f\n"] |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
(add-tests-with-string-output "applying thunks" |
| 83 |
[(let ([f (lambda () 12)]) (f)) => "12\n"] |
| 84 |
[(let ([f (lambda () (fx+ 12 13))]) (f)) => "25\n"] |
| 85 |
[(let ([f (lambda () 13)]) (fx+ (f) (f))) => "26\n"] |
| 86 |
[(let ([f (lambda () |
| 87 |
(let ([g (lambda () (fx+ 2 3))]) |
| 88 |
(fx* (g) (g))))]) |
| 89 |
(fx+ (f) (f))) => "50\n"] |
| 90 |
[(let ([f (lambda () |
| 91 |
(let ([f (lambda () (fx+ 2 3))]) |
| 92 |
(fx* (f) (f))))]) |
| 93 |
(fx+ (f) (f))) => "50\n"] |
| 94 |
[(let ([f (if (boolean? (lambda () 12)) |
| 95 |
(lambda () 13) |
| 96 |
(lambda () 14))]) |
| 97 |
(f)) => "14\n"] |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
(add-tests-with-string-output "parameter passing" |
| 102 |
[(let ([f (lambda (x) x)]) (f 12)) => "12\n"] |
| 103 |
[(let ([f (lambda (x y) (fx+ x y))]) (f 12 13)) => "25\n"] |
| 104 |
[(let ([f (lambda (x) |
| 105 |
(let ([g (lambda (x y) (fx+ x y))]) |
| 106 |
(g x 100)))]) |
| 107 |
(f 1000)) => "1100\n"] |
| 108 |
[(let ([f (lambda (g) (g 2 13))]) |
| 109 |
(f (lambda (n m) (fx* n m)))) => "26\n"] |
| 110 |
[(let ([f (lambda (g) (fx+ (g 10) (g 100)))]) |
| 111 |
(f (lambda (x) (fx* x x)))) => "10100\n"] |
| 112 |
[(let ([f (lambda (f n m) |
| 113 |
(if (fxzero? n) |
| 114 |
m |
| 115 |
(f f (fxsub1 n) (fx* n m))))]) |
| 116 |
(f f 5 1)) => "120\n"] |
| 117 |
[(let ([f (lambda (f n) |
| 118 |
(if (fxzero? n) |
| 119 |
1 |
| 120 |
(fx* n (f f (fxsub1 n)))))]) |
| 121 |
(f f 5)) => "120\n"] |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
(add-tests-with-string-output "closures" |
| 126 |
[(let ([n 12]) |
| 127 |
(let ([f (lambda () n)]) |
| 128 |
(f))) => "12\n"] |
| 129 |
[(let ([n 12]) |
| 130 |
(let ([f (lambda (m) (fx+ n m))]) |
| 131 |
(f 100))) => "112\n"] |
| 132 |
[(let ([f (lambda (f n m) |
| 133 |
(if (fxzero? n) |
| 134 |
m |
| 135 |
(f (fxsub1 n) (fx* n m))))]) |
| 136 |
(let ([g (lambda (g n m) (f (lambda (n m) (g g n m)) n m))]) |
| 137 |
(g g 5 1))) => "120\n"] |
| 138 |
[(let ([f (lambda (f n) |
| 139 |
(if (fxzero? n) |
| 140 |
1 |
| 141 |
(fx* n (f (fxsub1 n)))))]) |
| 142 |
(let ([g (lambda (g n) (f (lambda (n) (g g n)) n))]) |
| 143 |
(g g 5))) => "120\n"] |
| 144 |
) |