1 ;;;; the byte code interpreter
5 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;; We need at least this level of DEBUGness in order for the local
15 ;;; declaration in WITH-DEBUGGER-INFO to take effect.
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)))
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)
29 `(function ,(make-list (simple-byte-function-num-args x)
34 (let ((min (hairy-byte-function-min-args x))
35 (max (hairy-byte-function-max-args x)))
36 (dotimes (i min) (res 't))
39 (dotimes (i (- max min))
41 (when (hairy-byte-function-rest-arg-p x)
43 (ecase (hairy-byte-function-keywords-p x)
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)))
51 `(function ,(res) *))))))
53 ;;;; the evaluation stack
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
64 ;;; the index of the next free element of the interpreter's evaluation stack
65 (defvar *eval-stack-top* 0)
67 (defmacro current-stack-pointer () '*eval-stack-top*)
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))
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))
79 (defun push-eval-stack (value)
80 (let ((len (length (the simple-vector sb!eval::*eval-stack*)))
81 (sp (current-stack-pointer)))
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)))
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))
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?
102 (setf (svref stack i) '#:uninitialized))))
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)
111 (defmacro multiple-value-pop-eval-stack ((&rest vars) &body body)
112 #+nil (declare (optimize (inhibit-warnings 3)))
113 (let ((num-vars (length vars))
115 (new-sp-var (gensym "NEW-SP-"))
118 (unless (and (consp body) (consp (car body)) (eq (caar body) 'declare))
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)))))
128 (setf (current-stack-pointer) ,new-sp-var)
131 (defun stack-copy (dest src count)
132 (declare (type stack-pointer dest src count))
133 (let ((stack *eval-stack*))
136 (setf (svref stack dest) (svref stack src))
139 (do ((si (1- (+ src count))
141 (di (1- (+ dest count))
144 (declare (fixnum si di))
145 (setf (svref stack di) (svref stack si)))))
148 ;;;; component access magic
150 #!-sb-fluid (declaim (inline component-ref))
151 (defun component-ref (component pc)
152 (declare (type code-component component)
154 (sap-ref-8 (code-instructions component) pc))
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)
161 (setf (sap-ref-8 (code-instructions component) pc) value))
163 #!-sb-fluid (declaim (inline component-ref-signed))
164 (defun component-ref-signed (component pc)
165 (let ((byte (component-ref component pc)))
167 (logior (ash -1 8) byte)
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))))
176 ;;;; debugging support
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
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)
196 (defun byte-install-breakpoint (component pc)
197 (declare (type code-component component)
199 (values (unsigned-byte 8)))
200 (let ((orig (component-ref component pc)))
201 (setf (component-ref component pc)
203 (xop-index-or-lose 'breakpoint)))
206 (defun byte-remove-breakpoint (component pc orig)
207 (declare (type code-component component)
209 (type (unsigned-byte 8) orig)
210 (values (unsigned-byte 8)))
211 (setf (component-ref component pc) orig))
213 (defun byte-skip-breakpoint (component pc fp orig)
214 (declare (type code-component component)
216 (type stack-pointer fp)
217 (type (unsigned-byte 8) orig))
218 (byte-interpret-byte component fp pc orig))
220 ;;;; system constants
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
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)))
233 (setf (svref res value)
234 (if (and (consp key) (eq (car key) '%fdefinition-marker%))
235 (sb!impl::fdefinition-object (cdr key) t)
239 ;;;; byte compiled function constructors/extractors
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))
250 (push-eval-stack (%more-arg context i)))
251 (invoke-xep nil 0 old-sp 0 count xep))))
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))
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)))))
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))
277 ;;; FIXME: This doesn't seem to be needed in the target Lisp, only
278 ;;; at build-the-system time.
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))
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
306 (let ((info (svref *inline-functions* base)))
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)))
315 (,(inline-function-info-interpreter-function info)
317 `(multiple-value-pop-eval-stack ,args
318 (declare ,@(mapcar #'(lambda (type var)
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))
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)))))))
335 #!-sb-fluid (declaim (inline value-cell-setf))
336 (defun value-cell-setf (value cell)
337 (value-cell-set cell value)
340 #!-sb-fluid (declaim (inline setf-symbol-value))
341 (defun setf-symbol-value (value symbol)
342 (setf (symbol-value symbol) value))
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))
348 (eval-when (:compile-toplevel)
350 (sb!xc:defmacro %byte-symbol-value (x)
353 (with-debugger-info (component pc fp)
354 (error "unbound variable: ~S" x)))
357 (sb!xc:defmacro %byte-car (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))))
366 (sb!xc:defmacro %byte-cdr (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))))
377 #!-sb-fluid (declaim (inline %byte-special-bind))
378 (defun %byte-special-bind (value symbol)
379 (sb!sys:%primitive bind value symbol)
382 #!-sb-fluid (declaim (inline %byte-special-unbind))
383 (defun %byte-special-unbind ()
384 (sb!sys:%primitive unbind)
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.
393 ;;;; two-arg function stubs
395 ;;;; We have two-arg versions of some n-ary functions that are normally
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))
408 ;;;; miscellaneous primitive stubs
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)))
424 ;;; (used both by the byte interpreter and by the IR1 interpreter)
425 (defun %progv (vars vals fun)
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.
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)
442 (once-only ((n-component component)
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))
449 (values ,operand (1+ ,n-pc))))
450 (declare (type index ,operand ,new-pc))
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"))
459 ;;; a simple vector of the XOP functions
460 (declaim (type (simple-vector 256) *byte-xops*))
462 (make-array 256 :initial-element #'undefined-xop))
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")))
469 (defun ,defun-name ,lambda-list
471 (setf (aref *byte-xops* ,(xop-index-or-lose name)) #',defun-name)
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)
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))
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)
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))
497 (define-xop make-closure (component old-pc pc fp)
498 (declare (type code-component component)
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))
510 (push-eval-stack (make-byte-compiled-closure (pop-eval-stack)
512 (byte-interpret component pc fp))
514 (define-xop merge-unknown-values (component old-pc pc fp)
515 (declare (type code-component component)
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))
540 (define-xop default-unknown-values (component old-pc pc fp)
541 (declare (type code-component component)
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)
550 (cond ((minusp delta)
551 (incf (current-stack-pointer) delta))
554 (push-eval-stack nil)))))
555 (byte-interpret component pc fp))
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.
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)
568 (type stack-pointer fp))
569 (let ((num-results (pop-eval-stack)))
570 (declare (type index num-results))
573 (let ((tag (pop-eval-stack)))
574 (with-debugger-info (component old-pc fp)
575 (throw tag (values)))))
577 (multiple-value-pop-eval-stack
579 (with-debugger-info (component old-pc fp)
580 (throw tag result))))
582 (multiple-value-pop-eval-stack
583 (tag result0 result1)
584 (with-debugger-info (component old-pc fp)
585 (throw tag (values result0 result1)))))
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)))))))))
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)
605 (type stack-pointer fp))
606 (let ((new-pc (block nil
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)
616 (push-eval-stack num-results))
617 (component-ref-24 component pc)))))
618 (byte-interpret component new-pc fp)))
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)
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
631 (define-xop return-from (component old-pc pc fp)
632 (declare (type code-component component)
635 (type stack-pointer fp))
636 (let ((tag (pop-eval-stack))
637 (num-results (pop-eval-stack)))
638 (declare (type index num-results))
641 (with-debugger-info (component old-pc fp)
642 (throw tag (values))))
644 (let ((value (pop-eval-stack)))
645 (with-debugger-info (component old-pc fp)
648 (multiple-value-pop-eval-stack
650 (with-debugger-info (component old-pc fp)
651 (throw tag (values result0 result1)))))
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))))))))
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
664 (define-xop tagbody (component old-pc pc fp)
665 (declare (type code-component component)
668 (type stack-pointer fp))
669 (let* ((tag (pop-eval-stack))
674 (return (byte-interpret component pc fp))))))))
675 (byte-interpret component new-pc fp)))
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)
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))))
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)
697 (type stack-pointer fp))
700 (setf new-pc (byte-interpret component (+ pc 3) fp))
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)))
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))
716 (push-eval-stack fun)
717 (byte-interpret component pc fp))
719 (with-debugger-info (component old-pc fp)
720 (error 'undefined-function :name (fdefn-name fdefn)))))))
722 ;;; This is used to insert placeholder arguments for unused arguments
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)))
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))
740 (loop for (type predicate) in
741 '#.(loop for (type . predicate) in
742 *backend-type-predicates*
743 collect `(,(type-specifier type) ,predicate))
745 (let ((fun (fdefinition predicate)))
746 (setf (gethash type *byte-type-predicates*) fun)
747 (setf (gethash fun *byte-predicate-types*) type)))
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))
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)
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)
782 (with-debugger-info (component old-pc fp)
785 :expected-type (if (functionp type)
786 (gethash type *byte-predicate-types*)
787 (type-specifier type))))))
789 (byte-interpret component new-pc fp)))
791 ;;;; the byte-interpreter
793 ;;; The various operations are encoded as follows.
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
804 ;;; 1001nxxx tail-call op
805 ;;; 1010nxxx multiple-call op
806 ;;; 10110xxx local-call
807 ;;; 10111xxx local-tail-call
808 ;;; 11000xxx local-multiple-call
812 ;;; 1101010r if-false
816 ;;; to various inline functions.
819 ;;; This encoding is rather hard wired into BYTE-INTERPRET due to the
820 ;;; binary dispatch tree.
822 (defvar *byte-trace* nil)
824 ;;; the main entry point to the byte interpreter
825 (defun byte-interpret (component pc fp)
826 (declare (type code-component component)
828 (type stack-pointer fp))
829 (byte-interpret-byte component pc fp (component-ref component pc)))
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
837 (defun byte-interpret-byte (component pc fp byte)
838 (declare (type code-component component)
840 (type stack-pointer fp)
841 (type (unsigned-byte 8) byte))
843 #+nil (declare (optimize (inhibit-warnings 3)))
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,
853 (multiple-value-bind (operand new-pc)
854 (let ((operand (logand byte #xf)))
856 (let ((operand (component-ref component (1+ pc))))
858 (values (component-ref-24 component (+ pc 2))
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))
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))
876 (if (zerop (logand byte #x10))
877 (setf (eval-stack-ref (+ fp operand)) (pop-eval-stack))
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)))
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)
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))
903 (do-local-call component pc (+ pc 4) fp args)
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)))
919 (let ((num-results (logand byte #x7)))
920 (if (= num-results 7)
923 (do-return fp num-results)))
925 (if (zerop (logand byte #x08))
927 (if (if (zerop (logand byte #x04))
928 (if (zerop (logand byte #x02))
931 (if (zerop (logand byte #x02))
932 (not (pop-eval-stack))
933 (multiple-value-pop-eval-stack
939 (if (zerop (logand byte #x01))
940 (component-ref-24 component (1+ pc))
942 (component-ref-signed component (1+ pc))))
945 (byte-interpret component
946 (if (zerop (logand byte #x01))
951 (multiple-value-bind (sub-code new-pc)
952 (let ((operand (logand byte #x7)))
954 (values (component-ref component (+ pc 1))
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
961 (expand-into-inlines)
962 (byte-interpret component (1+ pc) fp))))))
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))
971 (- (current-stack-pointer) num-args)
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)))
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))
995 (iterate more ((index (1- (length closure-vars))))
996 (unless (minusp index)
997 (push-eval-stack (svref closure-vars 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)))
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))))
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))
1021 ,@(loop for n below 8
1022 collect `(,n (call-1 ,n)))
1025 (end (+ start num-args)))
1026 (declare (type stack-pointer end))
1027 (do ((i (1- end) (1- i)))
1029 (declare (fixnum i))
1030 (push (eval-stack-ref i) args))
1031 (setf (current-stack-pointer) restore-sp)
1032 (apply function args)))))
1037 (let ((dum (gensym)))
1038 (binds `(,dum (eval-stack-ref (+ start ,i))))
1041 (setf (current-stack-pointer) restore-sp)
1042 (funcall function ,@(args))))))
1045 (defun do-call (old-component call-pc ret-pc old-fp num-args named)
1046 (declare (type code-component old-component)
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))
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))))
1060 (declare (type stack-pointer old-sp)
1061 (type (or function fdefn) fun-or-fdefn)
1062 (type function function))
1065 (invoke-xep old-component ret-pc old-sp old-fp num-args function))
1067 (invoke-xep old-component ret-pc old-sp old-fp num-args
1068 (byte-closure-function function)
1069 (byte-closure-data function)))
1071 (cond ((minusp ret-pc)
1072 (let* ((ret-pc (- ret-pc))
1074 (multiple-value-list
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)))
1085 (old-component ret-pc old-fp)
1086 (byte-apply function num-args old-sp)))
1087 (byte-interpret old-component ret-pc old-fp)))))))
1089 (defun do-tail-call (component pc fp num-args named)
1090 (declare (type code-component component)
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)))
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))))
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))
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))
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)))
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))
1128 ;; We were called for multiple values. So return multiple
1130 (let* ((old-pc (- old-pc))
1132 (multiple-value-list
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)))
1141 ;; We were called for one value. So return one value.
1144 (old-component old-pc old-fp)
1145 (byte-apply function num-args old-sp)))
1146 (byte-interpret old-component old-pc old-fp)))))))
1148 (defvar *byte-trace-calls* nil)
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)
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*)))
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))
1181 (let ((min (hairy-byte-function-min-args xep))
1182 (max (hairy-byte-function-max-args xep)))
1185 (with-debugger-info (old-component ret-pc old-fp)
1186 (error "not enough arguments")))
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")))
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))
1198 (do ((index (1- sp) (1- index))
1200 (cons (eval-stack-ref index)
1202 ((< index more-args-start) result)
1203 (declare (fixnum index))))))
1204 (declare (type index more-args-supplied)
1205 (type stack-pointer more-args-start))
1207 ((not (hairy-byte-function-keywords-p xep))
1209 (setf (current-stack-pointer) (1+ more-args-start))
1210 (setf (eval-stack-ref more-args-start) rest))
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)
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))
1238 (when (caddr keyword)
1239 (setf (eval-stack-ref index) nil)
1241 (let ((index temp-sp)
1242 (allow (eq (hairy-byte-function-keywords-p xep)
1246 (declare (type fixnum index))
1249 (when (< index temp)
1251 (let ((key (eval-stack-ref index))
1252 (value (eval-stack-ref (1+ index))))
1253 (if (eq key :allow-other-keys)
1255 (let ((target more-args-start))
1256 (declare (type stack-pointer target))
1257 (dolist (keyword keywords
1260 (cond ((eq (car keyword) key)
1261 (setf (eval-stack-ref target) value)
1262 (when (caddr keyword)
1263 (setf (eval-stack-ref (1+ target))
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
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
1285 ;; returning to more byte-interpreted code
1286 (do-local-return old-component fp num-results))
1288 ;; returning to native code
1289 (let ((old-sp (eval-stack-ref (- fp 2))))
1292 (setf (current-stack-pointer) old-sp)
1295 (let ((result (pop-eval-stack)))
1296 (setf (current-stack-pointer) old-sp)
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))))))
1305 ;; ### function end breakpoint?
1306 (error "Function-end breakpoints are not supported.")))))
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))
1315 ;; wants single value
1316 (let ((result (if (zerop num-results)
1318 (eval-stack-ref (- (current-stack-pointer)
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
1325 (stack-copy old-sp (- (current-stack-pointer) 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)))))