96 lines
2.9 KiB
Scheme
96 lines
2.9 KiB
Scheme
(load "shared/util")
|
|
(load "shared/sicp-load-eceval-compiler")
|
|
(load "shared/sicp-compiler")
|
|
|
|
(display "\nex-5.45 - factorial-stack-usage-comparison\n")
|
|
|
|
;(compile-and-go
|
|
; '(begin
|
|
; (define (factorial n)
|
|
; (if (= n 1)
|
|
; 1
|
|
; (* (factorial (- n 1)) n)))
|
|
; (factorial 10)))
|
|
|
|
; a.
|
|
|
|
; | (factorial 10) | pushes | depth |
|
|
; | -------------- | ------ | ------ |
|
|
; | machine | 18 | 18 |
|
|
; | compiled | 56 | 29 |
|
|
; | evaluator | 310 | 35 |
|
|
|
|
; | (factorial 20) | pushes | depth |
|
|
; | -------------- | ------ | ------ |
|
|
; | machine | 38 | 38 |
|
|
; | compiled | 116 | 59 |
|
|
; | evaluator | 630 | 65 |
|
|
|
|
; pushes(compiled/machine) = 3
|
|
; depth(compiled/machine) = 1.5
|
|
|
|
; pushes(evaluator/compiled) = 5.5
|
|
; depth(evaluator/compiled) = 1.2
|
|
|
|
; b. Use special handling for primitive procedures from exercise 5.38.
|
|
|
|
(display "[done]\n")
|
|
|
|
(display "\nex-5.46 - fibo-stack-usage-comparison\n")
|
|
|
|
;(compile-and-go
|
|
; '(begin
|
|
; (define (fib n)
|
|
; (if (< n 2)
|
|
; n
|
|
; (+ (fib (- n 1)) (fib (- n 2)))))
|
|
; (fib 10)))
|
|
|
|
; | (fib 10) | pushes | depth |
|
|
; | -------------- | ------ | ------ |
|
|
; | machine | 264 | 18 |
|
|
; | compiled | 882 | 29 |
|
|
; | evaluator | 4950 | 53 |
|
|
|
|
; | (fib 20) | pushes | depth |
|
|
; | -------------- | ------ | ------ |
|
|
; | machine | 32835 | 38 |
|
|
; | compiled | 109452 | 59 |
|
|
; | evaluator | 612942 | 103 |
|
|
|
|
(display "[done]\n")
|
|
|
|
(display "\nex-5.47\n")
|
|
|
|
; Exercise 5.47. This section described how to modify the explicit-control
|
|
; evaluator so that interpreted code can call compiled procedures. Show how to
|
|
; modify the compiler so that compiled procedures can call not only primitive
|
|
; procedures and compiled procedures, but interpreted procedures as well. This
|
|
; requires modifying compile-procedure-call to handle the case of compound
|
|
; (interpreted) procedures. Be sure to handle all the same target and linkage
|
|
; combinations as in compile-proc-appl. To do the actual procedure application,
|
|
; the code needs to jump to the evaluator's compound-apply entry point. This
|
|
; label cannot be directly referenced in object code (since the assembler
|
|
; requires that all labels referenced by the code it is assembling be defined
|
|
; there), so we will add a register called compapp to the evaluator machine to
|
|
; hold this entry point, and add an instruction to initialize it:
|
|
|
|
; (assign compapp (label compound-apply))
|
|
; (branch (label external-entry)) ; branches if flag is set
|
|
;read-eval-print-loop
|
|
; ...
|
|
|
|
; To test your code, start by defining a procedure f that calls a procedure g.
|
|
; Use compile-and-go to compile the definition of f and start the evaluator.
|
|
; Now, typing at the evaluator, define g and try to call f.
|
|
|
|
|
|
(compile-and-go
|
|
'(define (f n)
|
|
(g n)))
|
|
|
|
(display "\nex-5.48\n")
|
|
|
|
; (display "\nex-5.49\n")
|
|
|