0.6.12.46:
[sbcl.git] / src / code / byte-interp.lisp
1 ;;;; the byte code interpreter
2
3 (in-package "SB!C")
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 ;;; We need at least this level of DEBUGness in order for the local
15 ;;; declaration in WITH-DEBUGGER-INFO to take effect.
16 ;;;
17 ;;; FIXME: This will cause source code location information to be
18 ;;; compiled into the executable, which will probably cause problems
19 ;;; for users running without the sources and/or without the
20 ;;; build-the-system readtable.
21 (declaim (optimize (debug 2)))
22 \f
23 ;;; Return a function type approximating the type of a byte-compiled
24 ;;; function. We really only capture the arg signature.
25 (defun byte-function-type (x)
26   (specifier-type
27    (etypecase x
28      (simple-byte-function
29       `(function ,(make-list (simple-byte-function-num-args x)
30                              :initial-element t)
31                  *))
32      (hairy-byte-function
33       (collect ((res))
34         (let ((min (hairy-byte-function-min-args x))
35               (max (hairy-byte-function-max-args x)))
36           (dotimes (i min) (res t))
37           (when (> max min)
38             (res '&optional)
39             (dotimes (i (- max min))
40               (res t))))
41         (when (hairy-byte-function-rest-arg-p x)
42           (res '&rest t))
43         (ecase (hairy-byte-function-keywords-p x)
44           ((t :allow-others)
45            (res '&key)
46            (dolist (key (hairy-byte-function-keywords x))
47                    (res `(,(car key) t)))
48            (when (eql (hairy-byte-function-keywords-p x) :allow-others)
49              (res '&allow-other-keys)))
50           ((nil)))
51         `(function ,(res) *))))))
52 \f
53 ;;;; the evaluation stack
54
55 ;;; the interpreter's evaluation stack
56 (defvar *eval-stack* (make-array 100)) ; will grow as needed
57 ;;; FIXME: This seems to be used by the ordinary (non-byte) interpreter
58 ;;; too, judging from a crash I had when I removed byte-interp.lisp from
59 ;;; the cold build sequence. It would probably be clearer to pull the
60 ;;; shared interpreter machinery out of the byte interpreter and ordinary
61 ;;; interpreter files and put them into their own file shared-interp.lisp
62 ;;; or something.
63
64 ;;; the index of the next free element of the interpreter's evaluation stack
65 (defvar *eval-stack-top* 0)
66
67 #!-sb-fluid (declaim (inline eval-stack-ref))
68 (defun eval-stack-ref (offset)
69   (declare (type stack-pointer offset))
70   (svref sb!eval::*eval-stack* offset))
71
72 #!-sb-fluid (declaim (inline (setf eval-stack-ref)))
73 (defun (setf eval-stack-ref) (new-value offset)
74   (declare (type stack-pointer offset))
75   (setf (svref sb!eval::*eval-stack* offset) new-value))
76
77 (defun push-eval-stack (value)
78   (let ((len (length (the simple-vector sb!eval::*eval-stack*)))
79         (sp *eval-stack-top*))
80     (when (= len sp)
81       (let ((new-stack (make-array (ash len 1))))
82         (replace new-stack sb!eval::*eval-stack* :end1 len :end2 len)
83         (setf sb!eval::*eval-stack* new-stack)))
84     (setf *eval-stack-top* (1+ sp))
85     (setf (eval-stack-ref sp) value)))
86
87 (defun allocate-eval-stack (amount)
88   (let* ((len (length (the simple-vector sb!eval::*eval-stack*)))
89          (sp *eval-stack-top*)
90          (new-sp (+ sp amount)))
91     (declare (type index sp new-sp))
92     (when (>= new-sp len)
93       (let ((new-stack (make-array (ash new-sp 1))))
94         (replace new-stack sb!eval::*eval-stack* :end1 len :end2 len)
95         (setf sb!eval::*eval-stack* new-stack)))
96     (setf *eval-stack-top* new-sp)
97     (let ((stack sb!eval::*eval-stack*))
98       (do ((i sp (1+ i))) ; FIXME: DOTIMES? or just :INITIAL-ELEMENT in MAKE-ARRAY?
99           ((= i new-sp))
100         (setf (svref stack i) '#:uninitialized))))
101   (values))
102
103 (defun pop-eval-stack ()
104   (let* ((new-sp (1- *eval-stack-top*))
105          (value (eval-stack-ref new-sp)))
106     (setf *eval-stack-top* new-sp)
107     value))
108
109 (defmacro multiple-value-pop-eval-stack ((&rest vars) &body body)
110   #+nil (declare (optimize (inhibit-warnings 3)))
111   (let ((num-vars (length vars))
112         (index -1)
113         (new-sp-var (gensym "NEW-SP-"))
114         (decls nil))
115     (loop
116       (unless (and (consp body) (consp (car body)) (eq (caar body) 'declare))
117         (return))
118       (push (pop body) decls))
119     `(let ((,new-sp-var (- *eval-stack-top* ,num-vars)))
120        (declare (type stack-pointer ,new-sp-var))
121        (let ,(mapcar #'(lambda (var)
122                          `(,var (eval-stack-ref
123                                  (+ ,new-sp-var ,(incf index)))))
124                      vars)
125          ,@(nreverse decls)
126          (setf *eval-stack-top* ,new-sp-var)
127          ,@body))))
128
129 (defun eval-stack-copy (dest src count)
130   (declare (type stack-pointer dest src count))
131   (let ((stack *eval-stack*))
132     (if (< dest src)
133         (dotimes (i count)
134           (setf (svref stack dest) (svref stack src))
135           (incf dest)
136           (incf src))
137         (do ((si (1- (+ src count))
138                  (1- si))
139              (di (1- (+ dest count))
140                  (1- di)))
141             ((< si src))
142           (declare (fixnum si di))
143           (setf (svref stack di) (svref stack si)))))
144   (values))
145 \f
146 ;;;; component access magic
147
148 #!-sb-fluid (declaim (inline component-ref))
149 (defun component-ref (component pc)
150   (declare (type code-component component)
151            (type pc pc))
152   (sap-ref-8 (code-instructions component) pc))
153
154 #!-sb-fluid (declaim (inline (setf component-ref)))
155 (defun (setf component-ref) (value component pc)
156   (declare (type (unsigned-byte 8) value)
157            (type code-component component)
158            (type pc pc))
159   (setf (sap-ref-8 (code-instructions component) pc) value))
160
161 #!-sb-fluid (declaim (inline component-ref-signed))
162 (defun component-ref-signed (component pc)
163   (let ((byte (component-ref component pc)))
164     (if (logbitp 7 byte)
165         (logior (ash -1 8) byte)
166         byte)))
167
168 #!-sb-fluid (declaim (inline component-ref-24))
169 (defun component-ref-24 (component pc)
170   (logior (ash (component-ref component pc) 16)
171           (ash (component-ref component (1+ pc)) 8)
172           (component-ref component (+ pc 2))))
173 \f
174 ;;;; debugging support
175
176 ;;; This macro binds three magic variables. When the debugger notices that
177 ;;; these three variables are bound, it makes a byte-code frame out of the
178 ;;; supplied information instead of a compiled frame. We set each var in
179 ;;; addition to binding it so the compiler doens't optimize away the binding.
180 (defmacro with-debugger-info ((component pc fp) &body body)
181   `(let ((%byte-interp-component ,component)
182          (%byte-interp-pc ,pc)
183          (%byte-interp-fp ,fp))
184      ;; FIXME: This will cause source code location information to be compiled
185      ;; into the executable, which will probably cause problems for users
186      ;; running without the sources and/or without the build-the-system
187      ;; readtable.
188      (declare (optimize (debug 3)))
189      (setf %byte-interp-component %byte-interp-component)
190      (setf %byte-interp-pc %byte-interp-pc)
191      (setf %byte-interp-fp %byte-interp-fp)
192      ,@body))
193
194 (defun byte-install-breakpoint (component pc)
195   (declare (type code-component component)
196            (type pc pc)
197            (values (unsigned-byte 8)))
198   (let ((orig (component-ref component pc)))
199     (setf (component-ref component pc)
200           #.(logior byte-xop
201                     (xop-index-or-lose 'breakpoint)))
202     orig))
203
204 (defun byte-remove-breakpoint (component pc orig)
205   (declare (type code-component component)
206            (type pc pc)
207            (type (unsigned-byte 8) orig)
208            (values (unsigned-byte 8)))
209   (setf (component-ref component pc) orig))
210
211 (defun byte-skip-breakpoint (component pc fp orig)
212   (declare (type code-component component)
213            (type pc pc)
214            (type stack-pointer fp)
215            (type (unsigned-byte 8) orig))
216   (byte-interpret-byte component fp pc orig))
217 \f
218 ;;;; system constants
219
220 ;;; a table mapping system constant indices to run-time values. We don't
221 ;;; reference the compiler variable at load time, since the interpreter is
222 ;;; loaded first.
223 (defparameter *system-constants*
224   (let ((res (make-array 256)))
225     (dolist (x '#.(collect ((res))
226                     (dohash (key value *system-constant-codes*)
227                       (res (cons key value)))
228                     (res)))
229       (let ((key (car x))
230             (value (cdr x)))
231         (setf (svref res value)
232               (if (and (consp key) (eq (car key) '%fdefinition-marker%))
233                   (fdefinition-object (cdr key) t)
234                   key))))
235     res))
236 \f
237 ;;;; byte compiled function constructors/extractors
238
239 (defun initialize-byte-compiled-function (xep)
240   (declare (type byte-function xep))
241   (push xep (code-header-ref (byte-function-component xep)
242                              sb!vm:code-trace-table-offset-slot))
243   (setf (funcallable-instance-function xep)
244         #'(instance-lambda (&more context count)
245             (let ((old-sp *eval-stack-top*))
246               (declare (type stack-pointer old-sp))
247               (dotimes (i count)
248                 (push-eval-stack (%more-arg context i)))
249               (invoke-xep nil 0 old-sp 0 count xep))))
250   xep)
251
252 (defun make-byte-compiled-closure (xep closure-vars)
253   (declare (type byte-function xep)
254            (type simple-vector closure-vars))
255   (let ((res (make-byte-closure xep closure-vars)))
256     (setf (funcallable-instance-function res)
257           #'(instance-lambda (&more context count)
258               (let ((old-sp *eval-stack-top*))
259                 (declare (type stack-pointer old-sp))
260                 (dotimes (i count)
261                   (push-eval-stack (%more-arg context i)))
262                 (invoke-xep nil 0 old-sp 0 count
263                             (byte-closure-function res)
264                             (byte-closure-data res)))))
265     res))
266 \f
267 ;;;; INLINEs
268
269 ;;; (The idea here seems to be to make sure it's at least 100,
270 ;;; in order to be able to compile the 32+ inline functions
271 ;;; in EXPAND-INTO-INLINES as intended. -- WHN 19991206)
272 (eval-when (:compile-toplevel :execute)
273   (setq sb!ext:*inline-expansion-limit* 100))
274
275 ;;; FIXME: This doesn't seem to be needed in the target Lisp, only
276 ;;; at build-the-system time.
277 ;;;
278 ;;; KLUDGE: This expands into code like
279 ;;; (IF (ZEROP (LOGAND BYTE 16))
280 ;;;     (IF (ZEROP (LOGAND BYTE 8))
281 ;;;      (IF (ZEROP (LOGAND BYTE 4))
282 ;;;          (IF (ZEROP (LOGAND BYTE 2))
283 ;;;              (IF (ZEROP (LOGAND BYTE 1))
284 ;;;                  (ERROR "Unknown inline function, id=~D" 0)
285 ;;;                  (ERROR "Unknown inline function, id=~D" 1))
286 ;;;              (IF (ZEROP (LOGAND BYTE 1))
287 ;;;                  (ERROR "Unknown inline function, id=~D" 2)
288 ;;;                  (ERROR "Unknown inline function, id=~D" 3)))
289 ;;;          (IF (ZEROP (LOGAND BYTE 2))
290 ;;;      ..) ..) ..)
291 ;;; That's probably more efficient than doing a function call (even a
292 ;;; local function call) for every byte interpreted, but I doubt it's
293 ;;; as fast as doing a jump through a table of sixteen addresses.
294 ;;; Perhaps it would be good to recode this as a straightforward
295 ;;; CASE statement and redirect the cleverness previously devoted to
296 ;;; this code to an optimizer for CASE which is smart enough to
297 ;;; implement suitable code as jump tables.
298 (defmacro expand-into-inlines ()
299   #+nil (declare (optimize (inhibit-warnings 3)))
300   (named-let build-dispatch ((bit 4)
301                              (base 0))
302     (if (minusp bit)
303         (let ((info (svref *inline-functions* base)))
304           (if info
305               (let* ((spec (type-specifier
306                             (inline-function-info-type info)))
307                      (arg-types (second spec))
308                      (result-type (third spec))
309                      (args (make-gensym-list (length arg-types)))
310                      (func
311                       `(the ,result-type
312                             (,(inline-function-info-interpreter-function info)
313                              ,@args))))
314                 `(multiple-value-pop-eval-stack ,args
315                    (declare ,@(mapcar #'(lambda (type var)
316                                           `(type ,type ,var))
317                                       arg-types args))
318                    ,(if (and (consp result-type)
319                              (eq (car result-type) 'values))
320                         (let ((results (make-gensym-list
321                                         (length (cdr result-type)))))
322                           `(multiple-value-bind ,results ,func
323                              ,@(mapcar #'(lambda (res)
324                                            `(push-eval-stack ,res))
325                                        results)))
326                         `(push-eval-stack ,func))))
327               `(error "unknown inline function, id=~D" ,base)))
328         `(if (zerop (logand byte ,(ash 1 bit)))
329              ,(build-dispatch (1- bit) base)
330              ,(build-dispatch (1- bit) (+ base (ash 1 bit)))))))
331
332 #!-sb-fluid (declaim (inline value-cell-setf))
333 (defun value-cell-setf (value cell)
334   (value-cell-set cell value)
335   value)
336
337 #!-sb-fluid (declaim (inline setf-symbol-value))
338 (defun setf-symbol-value (value symbol)
339   (setf (symbol-value symbol) value))
340
341 #!-sb-fluid (declaim (inline %setf-instance-ref))
342 (defun %setf-instance-ref (new-value instance index)
343   (setf (%instance-ref instance index) new-value))
344
345 (eval-when (:compile-toplevel)
346
347 (sb!xc:defmacro %byte-symbol-value (x)
348   `(let ((x ,x))
349      (unless (boundp x)
350        (with-debugger-info (component pc fp)
351          (error "unbound variable: ~S" x)))
352      (symbol-value x)))
353
354 (sb!xc:defmacro %byte-car (x)
355   `(let ((x ,x))
356      (unless (listp x)
357        (with-debugger-info (component pc fp)
358          (error 'simple-type-error :item x :expected-type 'list
359                 :format-control "non-list argument to CAR: ~S"
360                 :format-arguments (list x))))
361      (car x)))
362
363 (sb!xc:defmacro %byte-cdr (x)
364   `(let ((x ,x))
365      (unless (listp x)
366        (with-debugger-info (component pc fp)
367          (error 'simple-type-error :item x :expected-type 'list
368                 :format-control "non-list argument to CDR: ~S"
369                 :format-arguments (list x))))
370      (cdr x)))
371
372 ) ; EVAL-WHEN
373
374 #!-sb-fluid (declaim (inline %byte-special-bind))
375 (defun %byte-special-bind (value symbol)
376   (sb!sys:%primitive bind value symbol)
377   (values))
378
379 #!-sb-fluid (declaim (inline %byte-special-unbind))
380 (defun %byte-special-unbind ()
381   (sb!sys:%primitive unbind)
382   (values))
383 \f
384 ;;;; two-arg function stubs
385 ;;;;
386 ;;;; We have two-arg versions of some n-ary functions that are normally
387 ;;;; open-coded.
388
389 (defun two-arg-char= (x y) (char= x y))
390 (defun two-arg-char< (x y) (char< x y))
391 (defun two-arg-char> (x y) (char> x y))
392 (defun two-arg-char-equal (x y) (char-equal x y))
393 (defun two-arg-char-lessp (x y) (char-lessp x y))
394 (defun two-arg-char-greaterp (x y) (char-greaterp x y))
395 (defun two-arg-string= (x y) (string= x y))
396 (defun two-arg-string< (x y) (string= x y))
397 (defun two-arg-string> (x y) (string= x y))
398 \f
399 ;;;; miscellaneous primitive stubs
400
401 (macrolet ((frob (name &optional (args '(x)))
402              `(defun ,name ,args (,name ,@args))))
403   (frob %CODE-CODE-SIZE)
404   (frob %CODE-DEBUG-INFO)
405   (frob %CODE-ENTRY-POINTS)
406   (frob %FUNCALLABLE-INSTANCE-FUNCTION)
407   (frob %FUNCALLABLE-INSTANCE-LAYOUT)
408   (frob %FUNCALLABLE-INSTANCE-LEXENV)
409   (frob %FUNCTION-NEXT)
410   (frob %FUNCTION-SELF)
411   (frob %SET-FUNCALLABLE-INSTANCE-FUNCTION (fin new-val)))
412 \f
413 ;;;; funny functions
414
415 ;;; (used both by the byte interpreter and by the IR1 interpreter)
416 (defun %progv (vars vals fun)
417   (progv vars vals
418     (funcall fun)))
419 \f
420 ;;;; XOPs
421
422 ;;; Extension operations (XOPs) are various magic things that the byte
423 ;;; interpreter needs to do, but can't be represented as a function call.
424 ;;; When the byte interpreter encounters an XOP in the byte stream, it
425 ;;; tail-calls the corresponding XOP routine extracted from *byte-xops*.
426 ;;; The XOP routine can do whatever it wants, probably re-invoking the
427 ;;; byte interpreter.
428
429 ;;; Fetch an 8/24 bit operand out of the code stream.
430 (eval-when (:compile-toplevel :execute)
431   (sb!xc:defmacro with-extended-operand ((component pc operand new-pc)
432                                          &body body)
433     (once-only ((n-component component)
434                 (n-pc pc))
435       `(multiple-value-bind (,operand ,new-pc)
436            (let ((,operand (component-ref ,n-component ,n-pc)))
437              (if (= ,operand #xff)
438                  (values (component-ref-24 ,n-component (1+ ,n-pc))
439                          (+ ,n-pc 4))
440                  (values ,operand (1+ ,n-pc))))
441          (declare (type index ,operand ,new-pc))
442          ,@body))))
443
444 ;;; If a real XOP hasn't been defined, this gets invoked and signals an
445 ;;; error. This shouldn't happen in normal operation.
446 (defun undefined-xop (component old-pc pc fp)
447   (declare (ignore component old-pc pc fp))
448   (error "undefined XOP"))
449
450 ;;; a simple vector of the XOP functions
451 (declaim (type (simple-vector 256) *byte-xops*))
452 (defvar *byte-xops*
453   (make-array 256 :initial-element #'undefined-xop))
454
455 ;;; Define a XOP function and install it in *BYTE-XOPS*.
456 (eval-when (:compile-toplevel :execute)
457   (sb!xc:defmacro define-xop (name lambda-list &body body)
458     (let ((defun-name (symbolicate "BYTE-" name "-XOP")))
459       `(progn
460          (defun ,defun-name ,lambda-list
461            ,@body)
462          (setf (aref *byte-xops* ,(xop-index-or-lose name)) #',defun-name)
463          ',defun-name))))
464
465 ;;; This is spliced in by the debugger in order to implement breakpoints.
466 (define-xop breakpoint (component old-pc pc fp)
467   (declare (type code-component component)
468            (type pc old-pc)
469            (ignore pc)
470            (type stack-pointer fp))
471   ;; Invoke the debugger.
472   (with-debugger-info (component old-pc fp)
473     (sb!di::handle-breakpoint component old-pc fp))
474   ;; Retry the breakpoint XOP in case it was replaced with the original
475   ;; displaced byte-code.
476   (byte-interpret component old-pc fp))
477
478 ;;; This just duplicates whatever is on the top of the stack.
479 (define-xop dup (component old-pc pc fp)
480   (declare (type code-component component)
481            (ignore old-pc)
482            (type pc pc)
483            (type stack-pointer fp))
484   (let ((value (eval-stack-ref (1- *eval-stack-top*))))
485     (push-eval-stack value))
486   (byte-interpret component pc fp))
487
488 (define-xop make-closure (component old-pc pc fp)
489   (declare (type code-component component)
490            (ignore old-pc)
491            (type pc pc)
492            (type stack-pointer fp))
493   (let* ((num-closure-vars (pop-eval-stack))
494          (closure-vars (make-array num-closure-vars)))
495     (declare (type index num-closure-vars)
496              (type simple-vector closure-vars))
497     (named-let frob ((index (1- num-closure-vars)))
498       (unless (minusp index)
499         (setf (svref closure-vars index) (pop-eval-stack))
500         (frob (1- index))))
501     (push-eval-stack (make-byte-compiled-closure (pop-eval-stack)
502                                                  closure-vars)))
503   (byte-interpret component pc fp))
504
505 (define-xop merge-unknown-values (component old-pc pc fp)
506   (declare (type code-component component)
507            (ignore old-pc)
508            (type pc pc)
509            (type stack-pointer fp))
510   (labels ((grovel (remaining-blocks block-count-ptr)
511              (declare (type index remaining-blocks)
512                       (type stack-pointer block-count-ptr))
513              (declare (values index stack-pointer))
514              (let ((block-count (eval-stack-ref block-count-ptr)))
515                (declare (type index block-count))
516                (if (= remaining-blocks 1)
517                    (values block-count block-count-ptr)
518                    (let ((src (- block-count-ptr block-count)))
519                      (declare (type index src))
520                      (multiple-value-bind (values-above dst)
521                          (grovel (1- remaining-blocks) (1- src))
522                        (eval-stack-copy dst src block-count)
523                        (values (+ values-above block-count)
524                                (+ dst block-count))))))))
525     (multiple-value-bind (total-count end-ptr)
526         (grovel (pop-eval-stack) (1- *eval-stack-top*))
527       (setf (eval-stack-ref end-ptr) total-count)
528       (setf *eval-stack-top* (1+ end-ptr))))
529   (byte-interpret component pc fp))
530
531 (define-xop default-unknown-values (component old-pc pc fp)
532   (declare (type code-component component)
533            (ignore old-pc)
534            (type pc pc)
535            (type stack-pointer fp))
536   (let* ((desired (pop-eval-stack))
537          (supplied (pop-eval-stack))
538          (delta (- desired supplied)))
539     (declare (type index desired supplied)
540              (type fixnum delta))
541     (cond ((minusp delta)
542            (incf *eval-stack-top* delta))
543           ((plusp delta)
544            (dotimes (i delta)
545              (push-eval-stack nil)))))
546   (byte-interpret component pc fp))
547
548 ;;; %THROW is compiled down into this xop. The stack contains the tag, the
549 ;;; values, and then a count of the values. We special case various small
550 ;;; numbers of values to keep from consing if we can help it.
551 ;;;
552 ;;; Basically, we just extract the values and the tag and then do a throw.
553 ;;; The native compiler will convert this throw into whatever is necessary
554 ;;; to throw, so we don't have to duplicate all that cruft.
555 (define-xop throw (component old-pc pc fp)
556   (declare (type code-component component)
557            (type pc old-pc)
558            (ignore pc)
559            (type stack-pointer fp))
560   (let ((num-results (pop-eval-stack)))
561     (declare (type index num-results))
562     (case num-results
563       (0
564        (let ((tag (pop-eval-stack)))
565          (with-debugger-info (component old-pc fp)
566            (throw tag (values)))))
567       (1
568        (multiple-value-pop-eval-stack
569            (tag result)
570          (with-debugger-info (component old-pc fp)
571            (throw tag result))))
572       (2
573        (multiple-value-pop-eval-stack
574            (tag result0 result1)
575          (with-debugger-info (component old-pc fp)
576            (throw tag (values result0 result1)))))
577       (t
578        (let ((results nil))
579          (dotimes (i num-results)
580            (push (pop-eval-stack) results))
581          (let ((tag (pop-eval-stack)))
582            (with-debugger-info (component old-pc fp)
583              (throw tag (values-list results)))))))))
584
585 ;;; This is used for both CATCHes and BLOCKs that are closed over. We
586 ;;; establish a catcher for the supplied tag (from the stack top), and
587 ;;; recursivly enter the byte interpreter. If the byte interpreter exits,
588 ;;; it must have been because of a BREAKUP (see below), so we branch (by
589 ;;; tail-calling the byte interpreter) to the pc returned by BREAKUP.
590 ;;; If we are thrown to, then we branch to the address encoded in the 3 bytes
591 ;;; following the catch XOP.
592 (define-xop catch (component old-pc pc fp)
593   (declare (type code-component component)
594            (ignore old-pc)
595            (type pc pc)
596            (type stack-pointer fp))
597   (let ((new-pc (block nil
598                   (let ((results
599                          (multiple-value-list
600                           (catch (pop-eval-stack)
601                             (return (byte-interpret component (+ pc 3) fp))))))
602                     (let ((num-results 0))
603                       (declare (type index num-results))
604                       (dolist (result results)
605                         (push-eval-stack result)
606                         (incf num-results))
607                       (push-eval-stack num-results))
608                     (component-ref-24 component pc)))))
609     (byte-interpret component new-pc fp)))
610
611 ;;; Blow out of the dynamically nested CATCH or TAGBODY. We just return the
612 ;;; pc following the BREAKUP XOP and the drop-through code in CATCH or
613 ;;; TAGBODY will do the correct thing.
614 (define-xop breakup (component old-pc pc fp)
615   (declare (ignore component old-pc fp)
616            (type pc pc))
617   pc)
618
619 ;;; This is exactly like THROW, except that the tag is the last thing on
620 ;;; the stack instead of the first. This is used for RETURN-FROM (hence the
621 ;;; name).
622 (define-xop return-from (component old-pc pc fp)
623   (declare (type code-component component)
624            (type pc old-pc)
625            (ignore pc)
626            (type stack-pointer fp))
627   (let ((tag (pop-eval-stack))
628         (num-results (pop-eval-stack)))
629     (declare (type index num-results))
630     (case num-results
631       (0
632        (with-debugger-info (component old-pc fp)
633          (throw tag (values))))
634       (1
635        (let ((value (pop-eval-stack)))
636          (with-debugger-info (component old-pc fp)
637            (throw tag value))))
638       (2
639        (multiple-value-pop-eval-stack
640            (result0 result1)
641          (with-debugger-info (component old-pc fp)
642            (throw tag (values result0 result1)))))
643       (t
644        (let ((results nil))
645          (dotimes (i num-results)
646            (push (pop-eval-stack) results))
647          (with-debugger-info (component old-pc fp)
648            (throw tag (values-list results))))))))
649
650 ;;; Similar to CATCH, except for TAGBODY. One significant difference is that
651 ;;; when thrown to, we don't want to leave the dynamic extent of the tagbody
652 ;;; so we loop around and re-enter the catcher. We keep looping until BREAKUP
653 ;;; is used to blow out. When that happens, we just branch to the pc supplied
654 ;;; by BREAKUP.
655 (define-xop tagbody (component old-pc pc fp)
656   (declare (type code-component component)
657            (ignore old-pc)
658            (type pc pc)
659            (type stack-pointer fp))
660   (let* ((tag (pop-eval-stack))
661          (new-pc (block nil
662                    (loop
663                      (setf pc
664                            (catch tag
665                              (return (byte-interpret component pc fp))))))))
666     (byte-interpret component new-pc fp)))
667
668 ;;; Yup, you guessed it. This XOP implements GO. There are no values to
669 ;;; pass, so we don't have to mess with them, and multiple exits can all be
670 ;;; using the same tag so we have to pass the pc we want to go to.
671 (define-xop go (component old-pc pc fp)
672   (declare (type code-component component)
673            (type pc old-pc pc)
674            (type stack-pointer fp))
675   (let ((tag (pop-eval-stack))
676         (new-pc (component-ref-24 component pc)))
677     (with-debugger-info (component old-pc fp)
678       (throw tag new-pc))))
679
680 ;;; UNWIND-PROTECTs are handled significantly different in the byte
681 ;;; compiler and the native compiler. Basically, we just use the
682 ;;; native compiler's UNWIND-PROTECT, and let it worry about
683 ;;; continuing the unwind.
684 (define-xop unwind-protect (component old-pc pc fp)
685   (declare (type code-component component)
686            (ignore old-pc)
687            (type pc pc)
688            (type stack-pointer fp))
689   (let ((new-pc nil))
690     (unwind-protect
691         (setf new-pc (byte-interpret component (+ pc 3) fp))
692       (unless new-pc
693         ;; The cleanup function expects 3 values to be one the stack, so
694         ;; we have to put something there.
695         (push-eval-stack nil)
696         (push-eval-stack nil)
697         (push-eval-stack nil)
698         ;; Now run the cleanup code.
699         (byte-interpret component (component-ref-24 component pc) fp)))
700     (byte-interpret component new-pc fp)))
701
702 (define-xop fdefn-function-or-lose (component old-pc pc fp)
703   (let* ((fdefn (pop-eval-stack))
704          (fun (fdefn-function fdefn)))
705     (declare (type fdefn fdefn))
706     (cond (fun
707            (push-eval-stack fun)
708            (byte-interpret component pc fp))
709           (t
710            (with-debugger-info (component old-pc fp)
711              (error 'undefined-function :name (fdefn-name fdefn)))))))
712
713 ;;; This is used to insert placeholder arguments for unused arguments
714 ;;; to local calls.
715 (define-xop push-n-under (component old-pc pc fp)
716   (declare (ignore old-pc))
717   (with-extended-operand (component pc howmany new-pc)
718     (let ((val (pop-eval-stack)))
719       (allocate-eval-stack howmany)
720       (push-eval-stack val))
721     (byte-interpret component new-pc fp)))
722 \f
723 ;;;; type checking
724
725 ;;; These two hashtables map between type specifiers and type
726 ;;; predicate functions that test those types. They are initialized
727 ;;; according to the standard type predicates of the target system.
728 (defvar *byte-type-predicates* (make-hash-table :test 'equal))
729 (defvar *byte-predicate-types* (make-hash-table :test 'eq))
730
731 (loop for (type predicate) in
732           '#.(loop for (type . predicate) in
733                    *backend-type-predicates*
734                collect `(,(type-specifier type) ,predicate))
735       do
736   (let ((fun (fdefinition predicate)))
737     (setf (gethash type *byte-type-predicates*) fun)
738     (setf (gethash fun *byte-predicate-types*) type)))
739
740 ;;; This is called by the loader to convert a type specifier into a
741 ;;; type predicate (as used by the TYPE-CHECK XOP.) If it is a
742 ;;; structure type with a predicate or has a predefined predicate,
743 ;;; then return the predicate function, otherwise return the CTYPE
744 ;;; structure for the type.
745 (defun load-type-predicate (desc)
746   (or (gethash desc *byte-type-predicates*)
747       (let ((type (specifier-type desc)))
748         (if (typep type 'structure-class)
749             (let ((info (layout-info (class-layout type))))
750               (if (and info (eq (dd-type info) 'structure))
751                   (let ((pred (dd-predicate info)))
752                     (if (and pred (fboundp pred))
753                         (fdefinition pred)
754                         type))
755                   type))
756             type))))
757
758 ;;; Check the type of the value on the top of the stack. The type is
759 ;;; designated by an entry in the constants. If the value is a
760 ;;; function, then it is called as a type predicate. Otherwise, the
761 ;;; value is a CTYPE object, and we call %TYPEP on it.
762 (define-xop type-check (component old-pc pc fp)
763   (declare (type code-component component)
764            (type pc old-pc pc)
765            (type stack-pointer fp))
766   (with-extended-operand (component pc operand new-pc)
767     (let ((value (eval-stack-ref (1- *eval-stack-top*)))
768           (type (code-header-ref component
769                                  (+ operand sb!vm:code-constants-offset))))
770       (unless (if (functionp type)
771                   (funcall type value)
772                   (%typep value type))
773         (with-debugger-info (component old-pc fp)
774           (error 'type-error
775                  :datum value
776                  :expected-type (if (functionp type)
777                                     (gethash type *byte-predicate-types*)
778                                     (type-specifier type))))))
779
780     (byte-interpret component new-pc fp)))
781 \f
782 ;;;; the actual byte-interpreter
783
784 ;;; The various operations are encoded as follows.
785 ;;;
786 ;;; 0000xxxx push-local op
787 ;;; 0001xxxx push-arg op   [push-local, but negative]
788 ;;; 0010xxxx push-constant op
789 ;;; 0011xxxx push-system-constant op
790 ;;; 0100xxxx push-int op
791 ;;; 0101xxxx push-neg-int op
792 ;;; 0110xxxx pop-local op
793 ;;; 0111xxxx pop-n op
794 ;;; 1000nxxx call op
795 ;;; 1001nxxx tail-call op
796 ;;; 1010nxxx multiple-call op
797 ;;; 10110xxx local-call
798 ;;; 10111xxx local-tail-call
799 ;;; 11000xxx local-multiple-call
800 ;;; 11001xxx return
801 ;;; 1101000r branch
802 ;;; 1101001r if-true
803 ;;; 1101010r if-false
804 ;;; 1101011r if-eq
805 ;;; 11011xxx Xop
806 ;;; 11100000
807 ;;;    to    various inline functions.
808 ;;; 11111111
809 ;;;
810 ;;; This encoding is rather hard wired into BYTE-INTERPRET due to the
811 ;;; binary dispatch tree.
812
813 (defvar *byte-trace* nil)
814
815 ;;; the main entry point to the byte interpreter
816 (defun byte-interpret (component pc fp)
817   (declare (type code-component component)
818            (type pc pc)
819            (type stack-pointer fp))
820   (byte-interpret-byte component pc fp (component-ref component pc)))
821
822 ;;; This is separated from BYTE-INTERPRET in order to let us continue
823 ;;; from a breakpoint without having to replace the breakpoint with
824 ;;; the original instruction and arrange to somehow put the breakpoint
825 ;;; back after executing the instruction. We just leave the breakpoint
826 ;;; there, and call this function with the byte that the breakpoint
827 ;;; displaced.
828 (defun byte-interpret-byte (component pc fp byte)
829   (declare (type code-component component)
830            (type pc pc)
831            (type stack-pointer fp)
832            (type (unsigned-byte 8) byte))
833   (locally
834     #+nil (declare (optimize (inhibit-warnings 3)))
835     (when *byte-trace*
836       (let ((*byte-trace* nil))
837         (format *trace-output*
838                 "pc=~D, fp=~D, sp=~D, byte=#b~,'0X, frame:~%    ~S~%"
839                 pc fp *eval-stack-top* byte
840                 (subseq sb!eval::*eval-stack* fp *eval-stack-top*)))))
841   (if (zerop (logand byte #x80))
842       ;; Some stack operation. No matter what, we need the operand,
843       ;; so compute it.
844       (multiple-value-bind (operand new-pc)
845           (let ((operand (logand byte #xf)))
846             (if (= operand #xf)
847                 (let ((operand (component-ref component (1+ pc))))
848                   (if (= operand #xff)
849                       (values (component-ref-24 component (+ pc 2))
850                               (+ pc 5))
851                       (values operand (+ pc 2))))
852                 (values operand (1+ pc))))
853         (if (zerop (logand byte #x40))
854             (push-eval-stack (if (zerop (logand byte #x20))
855                                  (if (zerop (logand byte #x10))
856                                      (eval-stack-ref (+ fp operand))
857                                      (eval-stack-ref (- fp operand 5)))
858                                  (if (zerop (logand byte #x10))
859                                      (code-header-ref
860                                       component
861                                       (+ operand sb!vm:code-constants-offset))
862                                      (svref *system-constants* operand))))
863             (if (zerop (logand byte #x20))
864                 (push-eval-stack (if (zerop (logand byte #x10))
865                                      operand
866                                      (- (1+ operand))))
867                 (if (zerop (logand byte #x10))
868                     (setf (eval-stack-ref (+ fp operand)) (pop-eval-stack))
869                     (if (zerop operand)
870                         (let ((operand (pop-eval-stack)))
871                           (declare (type index operand))
872                           (decf *eval-stack-top* operand))
873                         (decf *eval-stack-top* operand)))))
874         (byte-interpret component new-pc fp))
875       (if (zerop (logand byte #x40))
876           ;; Some kind of call.
877           (let ((args (let ((args (logand byte #x07)))
878                         (if (= args #x07)
879                             (pop-eval-stack)
880                             args))))
881             (if (zerop (logand byte #x20))
882                 (let ((named (not (zerop (logand byte #x08)))))
883                   (if (zerop (logand byte #x10))
884                       ;; Call for single value.
885                       (do-call component pc (1+ pc) fp args named)
886                       ;; Tail call.
887                       (do-tail-call component pc fp args named)))
888                 (if (zerop (logand byte #x10))
889                     ;; Call for multiple-values.
890                     (do-call component pc (- (1+ pc)) fp args
891                              (not (zerop (logand byte #x08))))
892                     (if (zerop (logand byte #x08))
893                         ;; Local call
894                         (do-local-call component pc (+ pc 4) fp args)
895                         ;; Local tail-call
896                         (do-tail-local-call component pc fp args)))))
897           (if (zerop (logand byte #x20))
898               ;; local-multiple-call, Return, branch, or Xop.
899               (if (zerop (logand byte #x10))
900                   ;; local-multiple-call or return.
901                   (if (zerop (logand byte #x08))
902                       ;; Local-multiple-call.
903                       (do-local-call component pc (- (+ pc 4)) fp
904                                      (let ((args (logand byte #x07)))
905                                        (if (= args #x07)
906                                            (pop-eval-stack)
907                                            args)))
908                       ;; Return.
909                       (let ((num-results
910                              (let ((num-results (logand byte #x7)))
911                                (if (= num-results 7)
912                                    (pop-eval-stack)
913                                    num-results))))
914                         (do-return fp num-results)))
915                   ;; Branch or Xop.
916                   (if (zerop (logand byte #x08))
917                       ;; Branch.
918                       (if (if (zerop (logand byte #x04))
919                               (if (zerop (logand byte #x02))
920                                   t
921                                   (pop-eval-stack))
922                               (if (zerop (logand byte #x02))
923                                   (not (pop-eval-stack))
924                                   (multiple-value-pop-eval-stack
925                                    (val1 val2)
926                                    (eq val1 val2))))
927                           ;; Branch taken.
928                           (byte-interpret
929                            component
930                            (if (zerop (logand byte #x01))
931                                (component-ref-24 component (1+ pc))
932                                (+ pc 2
933                                   (component-ref-signed component (1+ pc))))
934                            fp)
935                           ;; Branch not taken.
936                           (byte-interpret component
937                                           (if (zerop (logand byte #x01))
938                                               (+ pc 4)
939                                               (+ pc 2))
940                                           fp))
941                       ;; Xop.
942                       (multiple-value-bind (sub-code new-pc)
943                           (let ((operand (logand byte #x7)))
944                             (if (= operand #x7)
945                                 (values (component-ref component (+ pc 1))
946                                         (+ pc 2))
947                                 (values operand (1+ pc))))
948                         (funcall (the function (svref *byte-xops* sub-code))
949                                  component pc new-pc fp))))
950               ;; some miscellaneous inline function
951               (progn
952                 (expand-into-inlines)
953                 (byte-interpret component (1+ pc) fp))))))
954
955 (defun do-local-call (component pc old-pc old-fp num-args)
956   (declare (type pc pc)
957            (type return-pc old-pc)
958            (type stack-pointer old-fp)
959            (type (integer 0 #.call-arguments-limit) num-args))
960   (invoke-local-entry-point component (component-ref-24 component (1+ pc))
961                             component old-pc
962                             (- *eval-stack-top* num-args)
963                             old-fp))
964
965 (defun do-tail-local-call (component pc fp num-args)
966   (declare (type code-component component) (type pc pc)
967            (type stack-pointer fp)
968            (type index num-args))
969   (let ((old-fp (eval-stack-ref (- fp 1)))
970         (old-sp (eval-stack-ref (- fp 2)))
971         (old-pc (eval-stack-ref (- fp 3)))
972         (old-component (eval-stack-ref (- fp 4)))
973         (start-of-args (- *eval-stack-top* num-args)))
974     (eval-stack-copy old-sp start-of-args num-args)
975     (setf *eval-stack-top* (+ old-sp num-args))
976     (invoke-local-entry-point component (component-ref-24 component (1+ pc))
977                               old-component old-pc old-sp old-fp)))
978
979 (defun invoke-local-entry-point (component target old-component old-pc old-sp
980                                            old-fp &optional closure-vars)
981   (declare (type pc target)
982            (type return-pc old-pc)
983            (type stack-pointer old-sp old-fp)
984            (type (or null simple-vector) closure-vars))
985   (when closure-vars
986     (named-let more ((index (1- (length closure-vars))))
987       (unless (minusp index)
988         (push-eval-stack (svref closure-vars index))
989         (more (1- index)))))
990   (push-eval-stack old-component)
991   (push-eval-stack old-pc)
992   (push-eval-stack old-sp)
993   (push-eval-stack old-fp)
994   (multiple-value-bind (stack-frame-size entry-pc)
995       (let ((byte (component-ref component target)))
996         (if (= byte 255)
997             (values (component-ref-24 component (1+ target)) (+ target 4))
998             (values (* byte 2) (1+ target))))
999     (declare (type pc entry-pc))
1000     (let ((fp *eval-stack-top*))
1001       (allocate-eval-stack stack-frame-size)
1002       (byte-interpret component entry-pc fp))))
1003
1004 ;;; Call a function with some arguments popped off of the interpreter
1005 ;;; stack, and restore the SP to the specified value.
1006 (defun byte-apply (function num-args restore-sp)
1007   (declare (type function function) (type index num-args))
1008   (let ((start (- *eval-stack-top* num-args)))
1009     (declare (type stack-pointer start))
1010     (macrolet ((frob ()
1011                  `(case num-args
1012                     ,@(loop for n below 8
1013                         collect `(,n (call-1 ,n)))
1014                     (t
1015                      (let ((args ())
1016                            (end (+ start num-args)))
1017                        (declare (type stack-pointer end))
1018                        (do ((i (1- end) (1- i)))
1019                            ((< i start))
1020                          (declare (fixnum i))
1021                          (push (eval-stack-ref i) args))
1022                        (setf *eval-stack-top* restore-sp)
1023                        (apply function args)))))
1024                (call-1 (n)
1025                  (collect ((binds)
1026                            (args))
1027                    (dotimes (i n)
1028                      (let ((dum (gensym)))
1029                        (binds `(,dum (eval-stack-ref (+ start ,i))))
1030                        (args dum)))
1031                    `(let ,(binds)
1032                       (setf *eval-stack-top* restore-sp)
1033                       (funcall function ,@(args))))))
1034       (frob))))
1035
1036 ;;; Note: negative RET-PC is a convention for "we need multiple return
1037 ;;; values".
1038 (defun do-call (old-component call-pc ret-pc old-fp num-args named)
1039   (declare (type code-component old-component)
1040            (type pc call-pc)
1041            (type return-pc ret-pc)
1042            (type stack-pointer old-fp)
1043            (type (integer 0 #.call-arguments-limit) num-args)
1044            (type (member t nil) named))
1045   (let* ((old-sp (- *eval-stack-top* num-args 1))
1046          (fun-or-fdefn (eval-stack-ref old-sp))
1047          (function (if named
1048                        (or (fdefn-function fun-or-fdefn)
1049                            (with-debugger-info (old-component call-pc old-fp)
1050                              (error 'undefined-function
1051                                     :name (fdefn-name fun-or-fdefn))))
1052                        fun-or-fdefn)))
1053     (declare (type stack-pointer old-sp)
1054              (type (or function fdefn) fun-or-fdefn)
1055              (type function function))
1056     (typecase function
1057       (byte-function
1058        (invoke-xep old-component ret-pc old-sp old-fp num-args function))
1059       (byte-closure
1060        (invoke-xep old-component ret-pc old-sp old-fp num-args
1061                    (byte-closure-function function)
1062                    (byte-closure-data function)))
1063       (t
1064        (cond ((minusp ret-pc)
1065               (let* ((ret-pc (- ret-pc))
1066                      (results
1067                       (multiple-value-list
1068                        (with-debugger-info
1069                            (old-component ret-pc old-fp)
1070                          (byte-apply function num-args old-sp)))))
1071                 (dolist (result results)
1072                   (push-eval-stack result))
1073                 (push-eval-stack (length results))
1074                 (byte-interpret old-component ret-pc old-fp)))
1075              (t
1076               (push-eval-stack
1077                (with-debugger-info
1078                    (old-component ret-pc old-fp)
1079                  (byte-apply function num-args old-sp)))
1080               (byte-interpret old-component ret-pc old-fp)))))))
1081
1082 (defun do-tail-call (component pc fp num-args named)
1083   (declare (type code-component component)
1084            (type pc pc)
1085            (type stack-pointer fp)
1086            (type (integer 0 #.call-arguments-limit) num-args)
1087            (type (member t nil) named))
1088   (let* ((start-of-args (- *eval-stack-top* num-args))
1089          (fun-or-fdefn (eval-stack-ref (1- start-of-args)))
1090          (function (if named
1091                        (or (fdefn-function fun-or-fdefn)
1092                            (with-debugger-info (component pc fp)
1093                              (error 'undefined-function
1094                                     :name (fdefn-name fun-or-fdefn))))
1095                        fun-or-fdefn))
1096          (old-fp (eval-stack-ref (- fp 1)))
1097          (old-sp (eval-stack-ref (- fp 2)))
1098          (old-pc (eval-stack-ref (- fp 3)))
1099          (old-component (eval-stack-ref (- fp 4))))
1100     (declare (type stack-pointer old-fp old-sp start-of-args)
1101              (type return-pc old-pc)
1102              (type (or fdefn function) fun-or-fdefn)
1103              (type function function))
1104     (typecase function
1105       (byte-function
1106        (eval-stack-copy old-sp start-of-args num-args)
1107        (setf *eval-stack-top* (+ old-sp num-args))
1108        (invoke-xep old-component old-pc old-sp old-fp num-args function))
1109       (byte-closure
1110        (eval-stack-copy old-sp start-of-args num-args)
1111        (setf *eval-stack-top* (+ old-sp num-args))
1112        (invoke-xep old-component old-pc old-sp old-fp num-args
1113                    (byte-closure-function function)
1114                    (byte-closure-data function)))
1115       (t
1116        ;; We are tail-calling native code.
1117        (cond ((null old-component)
1118               ;; We were called by native code.
1119               (byte-apply function num-args old-sp))
1120              ((minusp old-pc)
1121               ;; We were called for multiple values. So return multiple
1122               ;; values.
1123               (let* ((old-pc (- old-pc))
1124                      (results
1125                       (multiple-value-list
1126                        (with-debugger-info
1127                         (old-component old-pc old-fp)
1128                         (byte-apply function num-args old-sp)))))
1129                 (dolist (result results)
1130                   (push-eval-stack result))
1131                 (push-eval-stack (length results))
1132                 (byte-interpret old-component old-pc old-fp)))
1133              (t
1134               ;; We were called for one value. So return one value.
1135               (push-eval-stack
1136                (with-debugger-info
1137                    (old-component old-pc old-fp)
1138                  (byte-apply function num-args old-sp)))
1139               (byte-interpret old-component old-pc old-fp)))))))
1140
1141 (defvar *byte-trace-calls* nil)
1142
1143 (defun invoke-xep (old-component ret-pc old-sp old-fp num-args xep
1144                                  &optional closure-vars)
1145   (declare (type (or null code-component) old-component)
1146            (type index num-args)
1147            (type return-pc ret-pc)
1148            (type stack-pointer old-sp old-fp)
1149            (type byte-function xep)
1150            (type (or null simple-vector) closure-vars))
1151   ;; FIXME: Perhaps BYTE-TRACE-CALLS stuff should be conditional on SB-SHOW.
1152   (when *byte-trace-calls*
1153     (let ((*byte-trace-calls* nil)
1154           (*byte-trace* nil)
1155           (*print-level* sb!debug:*debug-print-level*)
1156           (*print-length* sb!debug:*debug-print-length*)
1157           (sp *eval-stack-top*))
1158       (format *trace-output*
1159               "~&INVOKE-XEP: ocode= ~S[~D]~%  ~
1160                osp= ~D, ofp= ~D, nargs= ~D, SP= ~D:~%  ~
1161                Fun= ~S ~@[~S~]~%  Args= ~S~%"
1162               old-component ret-pc old-sp old-fp num-args sp
1163               xep closure-vars (subseq *eval-stack* (- sp num-args) sp))
1164       (force-output *trace-output*)))
1165
1166   (let ((entry-point
1167          (cond
1168           ((typep xep 'simple-byte-function)
1169            (unless (eql (simple-byte-function-num-args xep) num-args)
1170              (with-debugger-info (old-component ret-pc old-fp)
1171                (error "wrong number of arguments")))
1172            (simple-byte-function-entry-point xep))
1173           (t
1174            (let ((min (hairy-byte-function-min-args xep))
1175                  (max (hairy-byte-function-max-args xep)))
1176              (cond
1177               ((< num-args min)
1178                (with-debugger-info (old-component ret-pc old-fp)
1179                  (error "not enough arguments")))
1180               ((<= num-args max)
1181                (nth (- num-args min) (hairy-byte-function-entry-points xep)))
1182               ((null (hairy-byte-function-more-args-entry-point xep))
1183                (with-debugger-info (old-component ret-pc old-fp)
1184                  (error "too many arguments")))
1185               (t
1186                (let* ((more-args-supplied (- num-args max))
1187                       (sp *eval-stack-top*)
1188                       (more-args-start (- sp more-args-supplied))
1189                       (restp (hairy-byte-function-rest-arg-p xep))
1190                       (rest (and restp
1191                                  (do ((index (1- sp) (1- index))
1192                                       (result nil
1193                                               (cons (eval-stack-ref index)
1194                                                     result)))
1195                                      ((< index more-args-start) result)
1196                                    (declare (fixnum index))))))
1197                  (declare (type index more-args-supplied)
1198                           (type stack-pointer more-args-start))
1199                  (cond
1200                   ((not (hairy-byte-function-keywords-p xep))
1201                    (aver restp)
1202                    (setf *eval-stack-top* (1+ more-args-start))
1203                    (setf (eval-stack-ref more-args-start) rest))
1204                   (t
1205                    (unless (evenp more-args-supplied)
1206                      (with-debugger-info (old-component ret-pc old-fp)
1207                        (error "odd number of &KEY arguments")))
1208                    ;; If there are &KEY args, then we need to leave
1209                    ;; the defaulted and supplied-p values where the
1210                    ;; more args currently are. There might be more or
1211                    ;; fewer. And also, we need to flatten the parsed
1212                    ;; args with the defaults before we scan the
1213                    ;; keywords. So we copy all the &MORE args to a
1214                    ;; temporary area at the end of the stack.
1215                    (let* ((num-more-args
1216                            (hairy-byte-function-num-more-args xep))
1217                           (new-sp (+ more-args-start num-more-args))
1218                           (temp (max sp new-sp))
1219                           (temp-sp (+ temp more-args-supplied))
1220                           (keywords (hairy-byte-function-keywords xep)))
1221                      (declare (type index temp)
1222                               (type stack-pointer new-sp temp-sp))
1223                      (allocate-eval-stack (- temp-sp sp))
1224                      (eval-stack-copy temp more-args-start more-args-supplied)
1225                      (when restp
1226                        (setf (eval-stack-ref more-args-start) rest)
1227                        (incf more-args-start))
1228                      (let ((index more-args-start))
1229                        (dolist (keyword keywords)
1230                          (setf (eval-stack-ref index) (cadr keyword))
1231                          (incf index)
1232                          (when (caddr keyword)
1233                            (setf (eval-stack-ref index) nil)
1234                            (incf index))))
1235                      (let ((index temp-sp)
1236                            (allow (eq (hairy-byte-function-keywords-p xep)
1237                                       :allow-others))
1238                            (bogus-key nil)
1239                            (bogus-key-p nil))
1240                        (declare (type fixnum index))
1241                        (loop
1242                          (decf index 2)
1243                          (when (< index temp)
1244                            (return))
1245                          (let ((key (eval-stack-ref index))
1246                                (value (eval-stack-ref (1+ index))))
1247                            (if (eq key :allow-other-keys)
1248                                (setf allow value)
1249                                (let ((target more-args-start))
1250                                  (declare (type stack-pointer target))
1251                                  (dolist (keyword keywords
1252                                                   (setf bogus-key key
1253                                                         bogus-key-p t))
1254                                    (cond ((eq (car keyword) key)
1255                                           (setf (eval-stack-ref target) value)
1256                                           (when (caddr keyword)
1257                                             (setf (eval-stack-ref (1+ target))
1258                                                   t))
1259                                           (return))
1260                                          ((caddr keyword)
1261                                           (incf target 2))
1262                                          (t
1263                                           (incf target))))))))
1264                        (when (and bogus-key-p (not allow))
1265                          (with-debugger-info (old-component ret-pc old-fp)
1266                            (error "unknown keyword: ~S" bogus-key))))
1267                      (setf *eval-stack-top* new-sp)))))
1268                (hairy-byte-function-more-args-entry-point xep))))))))
1269     (declare (type pc entry-point))
1270     (invoke-local-entry-point (byte-function-component xep) entry-point
1271                               old-component ret-pc old-sp old-fp
1272                               closure-vars)))
1273
1274 (defun do-return (fp num-results)
1275   (declare (type stack-pointer fp) (type index num-results))
1276   (let ((old-component (eval-stack-ref (- fp 4))))
1277     (typecase old-component
1278       (code-component
1279        ;; returning to more byte-interpreted code
1280        (do-local-return old-component fp num-results))
1281       (null
1282        ;; returning to native code
1283        (let ((old-sp (eval-stack-ref (- fp 2))))
1284          (case num-results
1285            (0
1286             (setf *eval-stack-top* old-sp)
1287             (values))
1288            (1
1289             (let ((result (pop-eval-stack)))
1290               (setf *eval-stack-top* old-sp)
1291               result))
1292            (t
1293             (let ((results nil))
1294               (dotimes (i num-results)
1295                 (push (pop-eval-stack) results))
1296               (setf *eval-stack-top* old-sp)
1297               (values-list results))))))
1298       (t
1299        ;; ### function end breakpoint?
1300        (error "Function-end breakpoints are not supported.")))))
1301
1302 (defun do-local-return (old-component fp num-results)
1303   (declare (type stack-pointer fp) (type index num-results))
1304   (let ((old-fp (eval-stack-ref (- fp 1)))
1305         (old-sp (eval-stack-ref (- fp 2)))
1306         (old-pc (eval-stack-ref (- fp 3))))
1307     (declare (type (signed-byte 25) old-pc))
1308     (if (plusp old-pc)
1309         ;; wants single value
1310         (let ((result (if (zerop num-results)
1311                           nil
1312                           (eval-stack-ref (- *eval-stack-top*
1313                                              num-results)))))
1314           (setf *eval-stack-top* old-sp)
1315           (push-eval-stack result)
1316           (byte-interpret old-component old-pc old-fp))
1317         ;; wants multiple values
1318         (progn
1319           (eval-stack-copy old-sp
1320                            (- *eval-stack-top* num-results)
1321                            num-results)
1322           (setf *eval-stack-top* (+ old-sp num-results))
1323           (push-eval-stack num-results)
1324           (byte-interpret old-component (- old-pc) old-fp)))))
1325