From ba682d8f6abdf80ed762ca2d8089039c7a524eb5 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Thu, 15 Apr 2021 12:41:52 -0400 Subject: [PATCH] Start to work on 5.33 --- ex-5_31-xx.scm | 60 ++++++++++++++++++++++++++++++++++++++++--- misc/ch5-compiler.scm | 2 +- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/ex-5_31-xx.scm b/ex-5_31-xx.scm index 80c7e4e..06160e4 100644 --- a/ex-5_31-xx.scm +++ b/ex-5_31-xx.scm @@ -1,4 +1,5 @@ (load "util.scm") +(load "misc/ch5-compiler.scm") (display "\nex-5.31 - save-and-restore-for-apply\n") @@ -61,10 +62,63 @@ ev-appl-did-operator-no-restore ; before runtime (display "[answered]\n") -(display "\nex-5.33\n") -(display "CONTINUE at 5.5.3\n") +(display "\nex-5.33 - compare-factorial-definitions\n") + +(define (compile-to-file code target linkage file-name) + (define (write-list-to-port xs port) + (if (null? xs) + '() + (begin + (display (car xs) port) + (display "\n" port) + (write-list-to-port (cdr xs) port)))) + (let* ((compile-result (compile code target linkage)) + (assembly-insts (statements compile-result)) + (port (open-output-file file-name))) + (write-list-to-port assembly-insts port) + (display "[") + (display file-name) + (display "]\n") + (close-output-port port))) + +; Uncomment the following lines to write the assembly code for the to methods +; into files. + +;(compile-to-file +; '(define (factorial n) +; (if (= n 1) +; 1 +; (* (factorial (- n 1)) n))) +; 'val 'next "factorial.sicp-asm") +; +;; reset label counter to the same labels for diff +;(define label-counter 0) +; +;(compile-to-file +; '(define (factorial-alt n) +; (if (= n 1) +; 1 +; (* n (factorial-alt (- n 1))))) +; 'val 'next "factorial-alt.sicp-asm") + +; $ diff factorial.sicp-asm factorial-alt.sicp-asm +; 33,36c33,34 +; < (assign val (op lookup-variable-value) (const n) (reg env)) +; < (assign argl (op list) (reg val)) +; < (save argl) +; --- +; > (save env) +; > (assign proc (op lookup-variable-value) (const factorial-alt) (reg env)) +; 63c61,63 +; < (restore argl) +; --- +; > (assign argl (op list) (reg val)) +; > (restore env) +; > (assign val (op lookup-variable-value) (const n) (reg env)) + (display "\nex-5.34\n") -;(display "\nex-5.35\n") + +(display "\nex-5.35\n") diff --git a/misc/ch5-compiler.scm b/misc/ch5-compiler.scm index c295770..c27c010 100644 --- a/misc/ch5-compiler.scm +++ b/misc/ch5-compiler.scm @@ -12,7 +12,7 @@ ;;;;Then you can compile Scheme programs as shown in section 5.5.5 ;;**implementation-dependent loading of syntax procedures -(load "ch5-syntax.scm") ;section 4.1.2 syntax procedures +(load "misc/ch5-syntax.scm") ;section 4.1.2 syntax procedures ;;;SECTION 5.5.1