From ce6ed4603b94b34fe5ac911110e513a6d6dd3bd5 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 19 Apr 2021 14:10:37 -0400 Subject: [PATCH] Implement 5.38a --- ex-5_31-xx.scm | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/ex-5_31-xx.scm b/ex-5_31-xx.scm index acad072..2a51196 100644 --- a/ex-5_31-xx.scm +++ b/ex-5_31-xx.scm @@ -391,11 +391,6 @@ ev-appl-did-operator-no-restore (display "\nex-5.38 - optimize-procedure-application\n") -; The expression (+ a 1) might be compiled into something as simple as: - -; (assign val (op lookup-variable-value) (const a) (reg env)) -; (assign val (op +) (reg val) (const 1)) - ; a. The open-coded primitives, unlike the special forms, all need their ; operands evaluated. Write a code generator spread-arguments for use by all ; the open-coding code generators. Spread-arguments should take an operand list @@ -403,12 +398,35 @@ ev-appl-did-operator-no-restore ; Note that an operand may contain a call to an open-coded primitive, so ; argument registers will have to be preserved during operand evaluation. -(define (spread-arguments operand-codes) - (assert (length operand-codes) 2) - (make-instruction-sequence - '() - '() - '((assign )))) +(define (spread-arguments operand-list) + (define (compile-operands operand-list operand-number) + (cond + ((null? operand-list) (empty-instruction-sequence)) + ((and (= operand-number 1)) + (preserving + '(arg1) + (compile (car operand-list) 'arg1 'next) + (compile-operands (cdr operand-list) (+ operand-number 1)))) + ((and (= operand-number 2)) + (preserving + '(arg1 arg2) + (compile (car operand-list) 'arg2 'next) + (compile-operands (cdr operand-list) (+ operand-number 1)))) + (else + (error "Only two arg registers supported -- SPREAD-ARGUMENTS")))) + + (let ((operand-codes (compile-operands operand-list 1))) + operand-codes)) + +(define (display-lines xs) + (define (display-line x) + (display x) (newline)) + (map display-line xs)) + +(display-lines (third (spread-arguments '(1 1)))) +(newline) + +(display "b. CONTINUE HERE\n") ; b. For each of the primitive procedures =, *, -, and +, write a code ; generator that takes a combination with that operator, together with a target