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