Implement 5.38a

This commit is contained in:
2021-04-19 14:10:37 -04:00
parent 1bf5859f17
commit ce6ed4603b

View File

@@ -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