123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- (define (compile-forms forms name package-key)
- (with-package-key package-key
- (lambda ()
- (if (null? forms)
- (segment->template (sequentially
- (lambda-protocol 0 #t #f #f)
- (deliver-value (instruction (enum op unspecific))
- (return-cont #f)))
- (make-frame #f name 0 #f #f #f))
- (compile-forms-loop (reverse forms)
- name
- #f)))))
- (define (compile-forms-loop forms name next)
- (if (null? forms)
- next
- (compile-forms-loop (cdr forms)
- name
- (compile-form (car forms) name next))))
-
- (define (compile-form form name next)
- (let ((frame (make-frame #f name 0 #t #f #f)))
- (segment->template
- (sequentially
- (lambda-protocol 0 #t #f #f)
- (let ((node (flatten-form (force-node form))))
- (cond ((define-node? node)
- (sequentially
- (compile-definition node frame an-ignore-values-cont)
- (if next
- (call-template-inst next #f 0 1 frame)
- (instruction (enum op values) 0 0))))
- (next
- (sequentially
- (compile-expression node 1 frame an-ignore-values-cont)
- (call-template-inst next #f 0 1 frame)))
- (else
- (compile-expression node 1 frame (return-cont #f))))))
- frame)))
- (define (call-template-inst template label nargs depth frame)
- (let ((offset (template-offset frame depth))
- (index (literal->index frame template)))
- (using-optional-label (enum op call-template)
- label
- (high-byte offset)
- (low-byte offset)
- (high-byte index)
- (low-byte index)
- nargs)))
- (define (template-call template depth frame cont)
- (receive (before depth label after)
- (push-continuation depth frame cont #f)
- (sequentially before
- (call-template-inst template label 0 depth frame)
- after)))
- (define (compile-definition node frame cont)
- (let* ((form (node-form node))
- (name (cadr form)))
- (sequentially (stack-indirect-instruction
- (template-offset frame 1)
- (binding->index frame
- (node-ref name 'binding)
- (node-form name)
- #f))
- (begin (depth-check! frame 2)
- (instruction (enum op push)))
- (compile-expression (caddr form)
- 2
- frame
- (named-cont (node-form name)))
- (deliver-value
- (instruction (enum op stored-object-set!)
- (enum stob location)
- location-contents-offset
- 0)
- cont))))
- (define location-contents-offset
- (cond ((assq 'location stob-data)
- => (lambda (stuff)
- (let loop ((slots (cdddr stuff)) (i 0))
- (if (eq? (caar slots) 'contents)
- i
- (loop (cdr slots) (+ i 1))))))
- (else
- (assertion-violation 'location-contents-offset
- "can't find location data in STOB-DATA"))))
- (define (make-startup-procedure inits resumer)
- (let* ((nargs 8)
- (frame (make-frame #f
- #f
- nargs
- #t
- #f
- #f)))
- (append-templates inits
- nargs
- frame
- (sequentially
- (template-call resumer
- (+ nargs 1)
- frame
- (fall-through-cont #f #f))
- (instruction (enum op pop-n) 0 1)
- (instruction (enum op tail-call) nargs 0 0)))))
- (define (append-templates templates nargs frame final)
- (segment->template
- (sequentially
- (lambda-protocol nargs #t #f #f)
- (reduce (lambda (template seg)
- (sequentially
- (template-call template
- (+ nargs 1)
- frame
- an-ignore-values-cont)
- seg))
- final
- templates))
- frame))
- (define an-ignore-values-cont (ignore-values-cont #f #f))
|