From 867e456de112ee717f430b1a36ac686497fa043d Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 27 Apr 2021 12:47:43 -0400 Subject: [PATCH] Answer 5.46 --- .gitignore | 1 + ex-5_45-xx.scm | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e363ff5..73dbccb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.ss#* .#*.ss +test.scm *.scm~ *.scm#* .#*.scm diff --git a/ex-5_45-xx.scm b/ex-5_45-xx.scm index 6ab39b5..c2508b2 100644 --- a/ex-5_45-xx.scm +++ b/ex-5_45-xx.scm @@ -2,7 +2,6 @@ (load "shared/sicp-load-eceval-compiler") (load "shared/sicp-compiler") - (display "\nex-5.45 - factorial-stack-usage-comparison\n") ;(compile-and-go @@ -35,9 +34,62 @@ ; b. Use special handling for primitive procedures from exercise 5.38. -(display "[answered]\n") +(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") +