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 #!-sb-fluid (declaim (inline eval-stack-ref))
68 (defun eval-stack-ref (offset)
69 (declare (type stack-pointer offset))
70 (svref sb!eval::*eval-stack* offset))
72 #!-sb-fluid (declaim (inline (setf eval-stack-ref)))
73 (defun (setf eval-stack-ref) (new-value offset)
74 (declare (type stack-pointer offset))
75 (setf (svref sb!eval::*eval-stack* offset) new-value))
77 (defun push-eval-stack (value)
78 (let ((len (length (the simple-vector sb!eval::*eval-stack*)))
79 (sp *eval-stack-top*))
81 (let ((new-stack (make-array (ash len 1))))
82 (replace new-stack sb!eval::*eval-stack* :end1 len :end2 len)
83 (setf sb!eval::*eval-stack* new-stack)))
84 (setf *eval-stack-top* (1+ sp))
85 (setf (eval-stack-ref sp) value)))
87 (defun allocate-eval-stack (amount)
88 (let* ((len (length (the simple-vector sb!eval::*eval-stack*)))
90 (new-sp (+ sp amount)))
91 (declare (type index sp new-sp))
93 (let ((new-stack (make-array (ash new-sp 1))))
94 (replace new-stack sb!eval::*eval-stack* :end1 len :end2 len)
95 (setf sb!eval::*eval-stack* new-stack)))
96 (setf *eval-stack-top* new-sp)
97 (let ((stack sb!eval::*eval-stack*))
98 (do ((i sp (1+ i))) ; FIXME: DOTIMES? or just :INITIAL-ELEMENT in MAKE-ARRAY?
100 (setf (svref stack i) '#:uninitialized))))
103 (defun pop-eval-stack ()
104 (let* ((new-sp (1- *eval-stack-top*))
105 (value (eval-stack-ref new-sp)))
106 (setf *eval-stack-top* new-sp)
109 (defmacro multiple-value-pop-eval-stack ((&rest vars) &body body)
110 #+nil (declare (optimize (inhibit-warnings 3)))
111 (let ((num-vars (length vars))
113 (new-sp-var (gensym "NEW-SP-"))
116 (unless (and (consp body) (consp (car body)) (eq (caar body) 'declare))
118 (push (pop body) decls))
119 `(let ((,new-sp-var (- *eval-stack-top* ,num-vars)))
120 (declare (type stack-pointer ,new-sp-var))
121 (let ,(mapcar #'(lambda (var)
122 `(,var (eval-stack-ref
123 (+ ,new-sp-var ,(incf index)))))
126 (setf *eval-stack-top* ,new-sp-var)
129 (defun eval-stack-copy (dest src count)
130 (declare (type stack-pointer dest src count))
131 (let ((stack *eval-stack*))
134 (setf (svref stack dest) (svref stack src))
137 (do ((si (1- (+ src count))
139 (di (1- (+ dest count))
142 (declare (fixnum si di))
143 (setf (svref stack di) (svref stack si)))))
146 ;;;; component access magic
148 #!-sb-fluid (declaim (inline component-ref))
149 (defun component-ref (component pc)
150 (declare (type code-component component)
152 (sap-ref-8 (code-instructions component) pc))
154 #!-sb-fluid (declaim (inline (setf component-ref)))
155 (defun (setf component-ref) (value component pc)
156 (declare (type (unsigned-byte 8) value)
157 (type code-component component)
159 (setf (sap-ref-8 (code-instructions component) pc) value))
161 #!-sb-fluid (declaim (inline component-ref-signed))
162 (defun component-ref-signed (component pc)
163 (let ((byte (component-ref component pc)))
165 (logior (ash -1 8) byte)
168 #!-sb-fluid (declaim (inline component-ref-24))
169 (defun component-ref-24 (component pc)
170 (logior (ash (component-ref component pc) 16)
171 (ash (component-ref component (1+ pc)) 8)
172 (component-ref component (+ pc 2))))
174 ;;;; debugging support
176 ;;; This macro binds three magic variables. When the debugger notices that
177 ;;; these three variables are bound, it makes a byte-code frame out of the
178 ;;; supplied information instead of a compiled frame. We set each var in
179 ;;; addition to binding it so the compiler doens't optimize away the binding.
180 (defmacro with-debugger-info ((component pc fp) &body body)
181 `(let ((%byte-interp-component ,component)
182 (%byte-interp-pc ,pc)
183 (%byte-interp-fp ,fp))
184 ;; FIXME: This will cause source code location information to be compiled
185 ;; into the executable, which will probably cause problems for users
186 ;; running without the sources and/or without the build-the-system
188 (declare (optimize (debug 3)))
189 (setf %byte-interp-component %byte-interp-component)
190 (setf %byte-interp-pc %byte-interp-pc)
191 (setf %byte-interp-fp %byte-interp-fp)
194 (defun byte-install-breakpoint (component pc)
195 (declare (type code-component component)
197 (values (unsigned-byte 8)))
198 (let ((orig (component-ref component pc)))
199 (setf (component-ref component pc)
201 (xop-index-or-lose 'breakpoint)))
204 (defun byte-remove-breakpoint (component pc orig)
205 (declare (type code-component component)
207 (type (unsigned-byte 8) orig)
208 (values (unsigned-byte 8)))
209 (setf (component-ref component pc) orig))
211 (defun byte-skip-breakpoint (component pc fp orig)
212 (declare (type code-component component)
214 (type stack-pointer fp)
215 (type (unsigned-byte 8) orig))
216 (byte-interpret-byte component fp pc orig))
218 ;;;; system constants
220 ;;; a table mapping system constant indices to run-time values. We don't
221 ;;; reference the compiler variable at load time, since the interpreter is
223 (defparameter *system-constants*
224 (let ((res (make-array 256)))
225 (dolist (x '#.(collect ((res))
226 (dohash (key value *system-constant-codes*)
227 (res (cons key value)))
231 (setf (svref res value)
232 (if (and (consp key) (eq (car key) '%fdefinition-marker%))
233 (fdefinition-object (cdr key) t)
237 ;;;; byte compiled function constructors/extractors
239 (defun initialize-byte-compiled-function (xep)
240 (declare (type byte-function xep))
241 (push xep (code-header-ref (byte-function-component xep)
242 sb!vm:code-trace-table-offset-slot))
243 (setf (funcallable-instance-function xep)
244 #'(instance-lambda (&more context count)
245 (let ((old-sp *eval-stack-top*))
246 (declare (type stack-pointer old-sp))
248 (push-eval-stack (%more-arg context i)))
249 (invoke-xep nil 0 old-sp 0 count xep))))
252 (defun make-byte-compiled-closure (xep closure-vars)
253 (declare (type byte-function xep)
254 (type simple-vector closure-vars))
255 (let ((res (make-byte-closure xep closure-vars)))
256 (setf (funcallable-instance-function res)
257 #'(instance-lambda (&more context count)
258 (let ((old-sp *eval-stack-top*))
259 (declare (type stack-pointer old-sp))
261 (push-eval-stack (%more-arg context i)))
262 (invoke-xep nil 0 old-sp 0 count
263 (byte-closure-function res)
264 (byte-closure-data res)))))
269 ;;; (The idea here seems to be to make sure it's at least 100,
270 ;;; in order to be able to compile the 32+ inline functions
271 ;;; in EXPAND-INTO-INLINES as intended. -- WHN 19991206)
272 (eval-when (:compile-toplevel :execute)
273 (setq sb!ext:*inline-expansion-limit* 100))
275 ;;; FIXME: This doesn't seem to be needed in the target Lisp, only
276 ;;; at build-the-system time.
278 ;;; KLUDGE: This expands into code like
279 ;;; (IF (ZEROP (LOGAND BYTE 16))
280 ;;; (IF (ZEROP (LOGAND BYTE 8))
281 ;;; (IF (ZEROP (LOGAND BYTE 4))
282 ;;; (IF (ZEROP (LOGAND BYTE 2))
283 ;;; (IF (ZEROP (LOGAND BYTE 1))
284 ;;; (ERROR "Unknown inline function, id=~D" 0)
285 ;;; (ERROR "Unknown inline function, id=~D" 1))
286 ;;; (IF (ZEROP (LOGAND BYTE 1))
287 ;;; (ERROR "Unknown inline function, id=~D" 2)
288 ;;; (ERROR "Unknown inline function, id=~D" 3)))
289 ;;; (IF (ZEROP (LOGAND BYTE 2))
291 ;;; That's probably more efficient than doing a function call (even a
292 ;;; local function call) for every byte interpreted, but I doubt it's
293 ;;; as fast as doing a jump through a table of sixteen addresses.
294 ;;; Perhaps it would be good to recode this as a straightforward
295 ;;; CASE statement and redirect the cleverness previously devoted to
296 ;;; this code to an optimizer for CASE which is smart enough to
297 ;;; implement suitable code as jump tables.
298 (defmacro expand-into-inlines ()
299 #+nil (declare (optimize (inhibit-warnings 3)))
300 (named-let build-dispatch ((bit 4)
303 (let ((info (svref *inline-functions* base)))
305 (let* ((spec (type-specifier
306 (inline-function-info-type info)))
307 (arg-types (second spec))
308 (result-type (third spec))
309 (args (make-gensym-list (length arg-types)))
312 (,(inline-function-info-interpreter-function info)
314 `(multiple-value-pop-eval-stack ,args
315 (declare ,@(mapcar #'(lambda (type var)
318 ,(if (and (consp result-type)
319 (eq (car result-type) 'values))
320 (let ((results (make-gensym-list
321 (length (cdr result-type)))))
322 `(multiple-value-bind ,results ,func
323 ,@(mapcar #'(lambda (res)
324 `(push-eval-stack ,res))
326 `(push-eval-stack ,func))))
327 `(error "unknown inline function, id=~D" ,base)))
328 `(if (zerop (logand byte ,(ash 1 bit)))
329 ,(build-dispatch (1- bit) base)
330 ,(build-dispatch (1- bit) (+ base (ash 1 bit)))))))
332 #!-sb-fluid (declaim (inline value-cell-setf))
333 (defun value-cell-setf (value cell)
334 (value-cell-set cell value)
337 #!-sb-fluid (declaim (inline setf-symbol-value))
338 (defun setf-symbol-value (value symbol)
339 (setf (symbol-value symbol) value))
341 #!-sb-fluid (declaim (inline %setf-instance-ref))
342 (defun %setf-instance-ref (new-value instance index)
343 (setf (%instance-ref instance index) new-value))
345 (eval-when (:compile-toplevel)
347 (sb!xc:defmacro %byte-symbol-value (x)
350 (with-debugger-info (component pc fp)
351 (error "unbound variable: ~S" x)))
354 (sb!xc:defmacro %byte-car (x)
357 (with-debugger-info (component pc fp)
358 (error 'simple-type-error :item x :expected-type 'list
359 :format-control "non-list argument to CAR: ~S"
360 :format-arguments (list x))))
363 (sb!xc:defmacro %byte-cdr (x)
366 (with-debugger-info (component pc fp)
367 (error 'simple-type-error :item x :expected-type 'list
368 :format-control "non-list argument to CDR: ~S"
369 :format-arguments (list x))))
374 #!-sb-fluid (declaim (inline %byte-special-bind))
375 (defun %byte-special-bind (value symbol)
376 (sb!sys:%primitive bind value symbol)
379 #!-sb-fluid (declaim (inline %byte-special-unbind))
380 (defun %byte-special-unbind ()
381 (sb!sys:%primitive unbind)
384 ;;;; two-arg function stubs
386 ;;;; We have two-arg versions of some n-ary functions that are normally
389 (defun two-arg-char= (x y) (char= x y))
390 (defun two-arg-char< (x y) (char< x y))
391 (defun two-arg-char> (x y) (char> x y))
392 (defun two-arg-char-equal (x y) (char-equal x y))
393 (defun two-arg-char-lessp (x y) (char-lessp x y))
394 (defun two-arg-char-greaterp (x y) (char-greaterp x y))
395 (defun two-arg-string= (x y) (string= x y))
396 (defun two-arg-string< (x y) (string= x y))
397 (defun two-arg-string> (x y) (string= x y))
399 ;;;; miscellaneous primitive stubs
401 (macrolet ((frob (name &optional (args '(x)))
402 `(defun ,name ,args (,name ,@args))))
403 (frob %CODE-CODE-SIZE)
404 (frob %CODE-DEBUG-INFO)
405 (frob %CODE-ENTRY-POINTS)
406 (frob %FUNCALLABLE-INSTANCE-FUNCTION)
407 (frob %FUNCALLABLE-INSTANCE-LAYOUT)
408 (frob %FUNCALLABLE-INSTANCE-LEXENV)
409 (frob %FUNCTION-NEXT)
410 (frob %FUNCTION-SELF)
411 (frob %SET-FUNCALLABLE-INSTANCE-FUNCTION (fin new-val)))
415 ;;; (used both by the byte interpreter and by the IR1 interpreter)
416 (defun %progv (vars vals fun)
422 ;;; Extension operations (XOPs) are various magic things that the byte
423 ;;; interpreter needs to do, but can't be represented as a function call.
424 ;;; When the byte interpreter encounters an XOP in the byte stream, it
425 ;;; tail-calls the corresponding XOP routine extracted from *byte-xops*.
426 ;;; The XOP routine can do whatever it wants, probably re-invoking the
427 ;;; byte interpreter.
429 ;;; Fetch an 8/24 bit operand out of the code stream.
430 (eval-when (:compile-toplevel :execute)
431 (sb!xc:defmacro with-extended-operand ((component pc operand new-pc)
433 (once-only ((n-component component)
435 `(multiple-value-bind (,operand ,new-pc)
436 (let ((,operand (component-ref ,n-component ,n-pc)))
437 (if (= ,operand #xff)
438 (values (component-ref-24 ,n-component (1+ ,n-pc))
440 (values ,operand (1+ ,n-pc))))
441 (declare (type index ,operand ,new-pc))
444 ;;; If a real XOP hasn't been defined, this gets invoked and signals an
445 ;;; error. This shouldn't happen in normal operation.
446 (defun undefined-xop (component old-pc pc fp)
447 (declare (ignore component old-pc pc fp))
448 (error "undefined XOP"))
450 ;;; a simple vector of the XOP functions
451 (declaim (type (simple-vector 256) *byte-xops*))
453 (make-array 256 :initial-element #'undefined-xop))
455 ;;; Define a XOP function and install it in *BYTE-XOPS*.
456 (eval-when (:compile-toplevel :execute)
457 (sb!xc:defmacro define-xop (name lambda-list &body body)
458 (let ((defun-name (symbolicate "BYTE-" name "-XOP")))
460 (defun ,defun-name ,lambda-list
462 (setf (aref *byte-xops* ,(xop-index-or-lose name)) #',defun-name)
465 ;;; This is spliced in by the debugger in order to implement breakpoints.
466 (define-xop breakpoint (component old-pc pc fp)
467 (declare (type code-component component)
470 (type stack-pointer fp))
471 ;; Invoke the debugger.
472 (with-debugger-info (component old-pc fp)
473 (sb!di::handle-breakpoint component old-pc fp))
474 ;; Retry the breakpoint XOP in case it was replaced with the original
475 ;; displaced byte-code.
476 (byte-interpret component old-pc fp))
478 ;;; This just duplicates whatever is on the top of the stack.
479 (define-xop dup (component old-pc pc fp)
480 (declare (type code-component component)
483 (type stack-pointer fp))
484 (let ((value (eval-stack-ref (1- *eval-stack-top*))))
485 (push-eval-stack value))
486 (byte-interpret component pc fp))
488 (define-xop make-closure (component old-pc pc fp)
489 (declare (type code-component component)
492 (type stack-pointer fp))
493 (let* ((num-closure-vars (pop-eval-stack))
494 (closure-vars (make-array num-closure-vars)))
495 (declare (type index num-closure-vars)
496 (type simple-vector closure-vars))
497 (named-let frob ((index (1- num-closure-vars)))
498 (unless (minusp index)
499 (setf (svref closure-vars index) (pop-eval-stack))
501 (push-eval-stack (make-byte-compiled-closure (pop-eval-stack)
503 (byte-interpret component pc fp))
505 (define-xop merge-unknown-values (component old-pc pc fp)
506 (declare (type code-component component)
509 (type stack-pointer fp))
510 (labels ((grovel (remaining-blocks block-count-ptr)
511 (declare (type index remaining-blocks)
512 (type stack-pointer block-count-ptr))
513 (declare (values index stack-pointer))
514 (let ((block-count (eval-stack-ref block-count-ptr)))
515 (declare (type index block-count))
516 (if (= remaining-blocks 1)
517 (values block-count block-count-ptr)
518 (let ((src (- block-count-ptr block-count)))
519 (declare (type index src))
520 (multiple-value-bind (values-above dst)
521 (grovel (1- remaining-blocks) (1- src))
522 (eval-stack-copy dst src block-count)
523 (values (+ values-above block-count)
524 (+ dst block-count))))))))
525 (multiple-value-bind (total-count end-ptr)
526 (grovel (pop-eval-stack) (1- *eval-stack-top*))
527 (setf (eval-stack-ref end-ptr) total-count)
528 (setf *eval-stack-top* (1+ end-ptr))))
529 (byte-interpret component pc fp))
531 (define-xop default-unknown-values (component old-pc pc fp)
532 (declare (type code-component component)
535 (type stack-pointer fp))
536 (let* ((desired (pop-eval-stack))
537 (supplied (pop-eval-stack))
538 (delta (- desired supplied)))
539 (declare (type index desired supplied)
541 (cond ((minusp delta)
542 (incf *eval-stack-top* delta))
545 (push-eval-stack nil)))))
546 (byte-interpret component pc fp))
548 ;;; %THROW is compiled down into this xop. The stack contains the tag, the
549 ;;; values, and then a count of the values. We special case various small
550 ;;; numbers of values to keep from consing if we can help it.
552 ;;; Basically, we just extract the values and the tag and then do a throw.
553 ;;; The native compiler will convert this throw into whatever is necessary
554 ;;; to throw, so we don't have to duplicate all that cruft.
555 (define-xop throw (component old-pc pc fp)
556 (declare (type code-component component)
559 (type stack-pointer fp))
560 (let ((num-results (pop-eval-stack)))
561 (declare (type index num-results))
564 (let ((tag (pop-eval-stack)))
565 (with-debugger-info (component old-pc fp)
566 (throw tag (values)))))
568 (multiple-value-pop-eval-stack
570 (with-debugger-info (component old-pc fp)
571 (throw tag result))))
573 (multiple-value-pop-eval-stack
574 (tag result0 result1)
575 (with-debugger-info (component old-pc fp)
576 (throw tag (values result0 result1)))))
579 (dotimes (i num-results)
580 (push (pop-eval-stack) results))
581 (let ((tag (pop-eval-stack)))
582 (with-debugger-info (component old-pc fp)
583 (throw tag (values-list results)))))))))
585 ;;; This is used for both CATCHes and BLOCKs that are closed over. We
586 ;;; establish a catcher for the supplied tag (from the stack top), and
587 ;;; recursivly enter the byte interpreter. If the byte interpreter exits,
588 ;;; it must have been because of a BREAKUP (see below), so we branch (by
589 ;;; tail-calling the byte interpreter) to the pc returned by BREAKUP.
590 ;;; If we are thrown to, then we branch to the address encoded in the 3 bytes
591 ;;; following the catch XOP.
592 (define-xop catch (component old-pc pc fp)
593 (declare (type code-component component)
596 (type stack-pointer fp))
597 (let ((new-pc (block nil
600 (catch (pop-eval-stack)
601 (return (byte-interpret component (+ pc 3) fp))))))
602 (let ((num-results 0))
603 (declare (type index num-results))
604 (dolist (result results)
605 (push-eval-stack result)
607 (push-eval-stack num-results))
608 (component-ref-24 component pc)))))
609 (byte-interpret component new-pc fp)))
611 ;;; Blow out of the dynamically nested CATCH or TAGBODY. We just return the
612 ;;; pc following the BREAKUP XOP and the drop-through code in CATCH or
613 ;;; TAGBODY will do the correct thing.
614 (define-xop breakup (component old-pc pc fp)
615 (declare (ignore component old-pc fp)
619 ;;; This is exactly like THROW, except that the tag is the last thing on
620 ;;; the stack instead of the first. This is used for RETURN-FROM (hence the
622 (define-xop return-from (component old-pc pc fp)
623 (declare (type code-component component)
626 (type stack-pointer fp))
627 (let ((tag (pop-eval-stack))
628 (num-results (pop-eval-stack)))
629 (declare (type index num-results))
632 (with-debugger-info (component old-pc fp)
633 (throw tag (values))))
635 (let ((value (pop-eval-stack)))
636 (with-debugger-info (component old-pc fp)
639 (multiple-value-pop-eval-stack
641 (with-debugger-info (component old-pc fp)
642 (throw tag (values result0 result1)))))
645 (dotimes (i num-results)
646 (push (pop-eval-stack) results))
647 (with-debugger-info (component old-pc fp)
648 (throw tag (values-list results))))))))
650 ;;; Similar to CATCH, except for TAGBODY. One significant difference is that
651 ;;; when thrown to, we don't want to leave the dynamic extent of the tagbody
652 ;;; so we loop around and re-enter the catcher. We keep looping until BREAKUP
653 ;;; is used to blow out. When that happens, we just branch to the pc supplied
655 (define-xop tagbody (component old-pc pc fp)
656 (declare (type code-component component)
659 (type stack-pointer fp))
660 (let* ((tag (pop-eval-stack))
665 (return (byte-interpret component pc fp))))))))
666 (byte-interpret component new-pc fp)))
668 ;;; Yup, you guessed it. This XOP implements GO. There are no values to
669 ;;; pass, so we don't have to mess with them, and multiple exits can all be
670 ;;; using the same tag so we have to pass the pc we want to go to.
671 (define-xop go (component old-pc pc fp)
672 (declare (type code-component component)
674 (type stack-pointer fp))
675 (let ((tag (pop-eval-stack))
676 (new-pc (component-ref-24 component pc)))
677 (with-debugger-info (component old-pc fp)
678 (throw tag new-pc))))
680 ;;; UNWIND-PROTECTs are handled significantly different in the byte
681 ;;; compiler and the native compiler. Basically, we just use the
682 ;;; native compiler's UNWIND-PROTECT, and let it worry about
683 ;;; continuing the unwind.
684 (define-xop unwind-protect (component old-pc pc fp)
685 (declare (type code-component component)
688 (type stack-pointer fp))
691 (setf new-pc (byte-interpret component (+ pc 3) fp))
693 ;; The cleanup function expects 3 values to be one the stack, so
694 ;; we have to put something there.
695 (push-eval-stack nil)
696 (push-eval-stack nil)
697 (push-eval-stack nil)
698 ;; Now run the cleanup code.
699 (byte-interpret component (component-ref-24 component pc) fp)))
700 (byte-interpret component new-pc fp)))
702 (define-xop fdefn-function-or-lose (component old-pc pc fp)
703 (let* ((fdefn (pop-eval-stack))
704 (fun (fdefn-function fdefn)))
705 (declare (type fdefn fdefn))
707 (push-eval-stack fun)
708 (byte-interpret component pc fp))
710 (with-debugger-info (component old-pc fp)
711 (error 'undefined-function :name (fdefn-name fdefn)))))))
713 ;;; This is used to insert placeholder arguments for unused arguments
715 (define-xop push-n-under (component old-pc pc fp)
716 (declare (ignore old-pc))
717 (with-extended-operand (component pc howmany new-pc)
718 (let ((val (pop-eval-stack)))
719 (allocate-eval-stack howmany)
720 (push-eval-stack val))
721 (byte-interpret component new-pc fp)))
725 ;;; These two hashtables map between type specifiers and type
726 ;;; predicate functions that test those types. They are initialized
727 ;;; according to the standard type predicates of the target system.
728 (defvar *byte-type-predicates* (make-hash-table :test 'equal))
729 (defvar *byte-predicate-types* (make-hash-table :test 'eq))
731 (loop for (type predicate) in
732 '#.(loop for (type . predicate) in
733 *backend-type-predicates*
734 collect `(,(type-specifier type) ,predicate))
736 (let ((fun (fdefinition predicate)))
737 (setf (gethash type *byte-type-predicates*) fun)
738 (setf (gethash fun *byte-predicate-types*) type)))
740 ;;; This is called by the loader to convert a type specifier into a
741 ;;; type predicate (as used by the TYPE-CHECK XOP.) If it is a
742 ;;; structure type with a predicate or has a predefined predicate,
743 ;;; then return the predicate function, otherwise return the CTYPE
744 ;;; structure for the type.
745 (defun load-type-predicate (desc)
746 (or (gethash desc *byte-type-predicates*)
747 (let ((type (specifier-type desc)))
748 (if (typep type 'structure-class)
749 (let ((info (layout-info (class-layout type))))
750 (if (and info (eq (dd-type info) 'structure))
751 (let ((pred (dd-predicate info)))
752 (if (and pred (fboundp pred))
758 ;;; Check the type of the value on the top of the stack. The type is
759 ;;; designated by an entry in the constants. If the value is a
760 ;;; function, then it is called as a type predicate. Otherwise, the
761 ;;; value is a CTYPE object, and we call %TYPEP on it.
762 (define-xop type-check (component old-pc pc fp)
763 (declare (type code-component component)
765 (type stack-pointer fp))
766 (with-extended-operand (component pc operand new-pc)
767 (let ((value (eval-stack-ref (1- *eval-stack-top*)))
768 (type (code-header-ref component
769 (+ operand sb!vm:code-constants-offset))))
770 (unless (if (functionp type)
773 (with-debugger-info (component old-pc fp)
776 :expected-type (if (functionp type)
777 (gethash type *byte-predicate-types*)
778 (type-specifier type))))))
780 (byte-interpret component new-pc fp)))
782 ;;;; the actual byte-interpreter
784 ;;; The various operations are encoded as follows.
786 ;;; 0000xxxx push-local op
787 ;;; 0001xxxx push-arg op [push-local, but negative]
788 ;;; 0010xxxx push-constant op
789 ;;; 0011xxxx push-system-constant op
790 ;;; 0100xxxx push-int op
791 ;;; 0101xxxx push-neg-int op
792 ;;; 0110xxxx pop-local op
793 ;;; 0111xxxx pop-n op
795 ;;; 1001nxxx tail-call op
796 ;;; 1010nxxx multiple-call op
797 ;;; 10110xxx local-call
798 ;;; 10111xxx local-tail-call
799 ;;; 11000xxx local-multiple-call
803 ;;; 1101010r if-false
807 ;;; to various inline functions.
810 ;;; This encoding is rather hard wired into BYTE-INTERPRET due to the
811 ;;; binary dispatch tree.
813 (defvar *byte-trace* nil)
815 ;;; the main entry point to the byte interpreter
816 (defun byte-interpret (component pc fp)
817 (declare (type code-component component)
819 (type stack-pointer fp))
820 (byte-interpret-byte component pc fp (component-ref component pc)))
822 ;;; This is separated from BYTE-INTERPRET in order to let us continue
823 ;;; from a breakpoint without having to replace the breakpoint with
824 ;;; the original instruction and arrange to somehow put the breakpoint
825 ;;; back after executing the instruction. We just leave the breakpoint
826 ;;; there, and call this function with the byte that the breakpoint
828 (defun byte-interpret-byte (component pc fp byte)
829 (declare (type code-component component)
831 (type stack-pointer fp)
832 (type (unsigned-byte 8) byte))
834 #+nil (declare (optimize (inhibit-warnings 3)))
836 (let ((*byte-trace* nil))
837 (format *trace-output*
838 "pc=~D, fp=~D, sp=~D, byte=#b~,'0X, frame:~% ~S~%"
839 pc fp *eval-stack-top* byte
840 (subseq sb!eval::*eval-stack* fp *eval-stack-top*)))))
841 (if (zerop (logand byte #x80))
842 ;; Some stack operation. No matter what, we need the operand,
844 (multiple-value-bind (operand new-pc)
845 (let ((operand (logand byte #xf)))
847 (let ((operand (component-ref component (1+ pc))))
849 (values (component-ref-24 component (+ pc 2))
851 (values operand (+ pc 2))))
852 (values operand (1+ pc))))
853 (if (zerop (logand byte #x40))
854 (push-eval-stack (if (zerop (logand byte #x20))
855 (if (zerop (logand byte #x10))
856 (eval-stack-ref (+ fp operand))
857 (eval-stack-ref (- fp operand 5)))
858 (if (zerop (logand byte #x10))
861 (+ operand sb!vm:code-constants-offset))
862 (svref *system-constants* operand))))
863 (if (zerop (logand byte #x20))
864 (push-eval-stack (if (zerop (logand byte #x10))
867 (if (zerop (logand byte #x10))
868 (setf (eval-stack-ref (+ fp operand)) (pop-eval-stack))
870 (let ((operand (pop-eval-stack)))
871 (declare (type index operand))
872 (decf *eval-stack-top* operand))
873 (decf *eval-stack-top* operand)))))
874 (byte-interpret component new-pc fp))
875 (if (zerop (logand byte #x40))
876 ;; Some kind of call.
877 (let ((args (let ((args (logand byte #x07)))
881 (if (zerop (logand byte #x20))
882 (let ((named (not (zerop (logand byte #x08)))))
883 (if (zerop (logand byte #x10))
884 ;; Call for single value.
885 (do-call component pc (1+ pc) fp args named)
887 (do-tail-call component pc fp args named)))
888 (if (zerop (logand byte #x10))
889 ;; Call for multiple-values.
890 (do-call component pc (- (1+ pc)) fp args
891 (not (zerop (logand byte #x08))))
892 (if (zerop (logand byte #x08))
894 (do-local-call component pc (+ pc 4) fp args)
896 (do-tail-local-call component pc fp args)))))
897 (if (zerop (logand byte #x20))
898 ;; local-multiple-call, Return, branch, or Xop.
899 (if (zerop (logand byte #x10))
900 ;; local-multiple-call or return.
901 (if (zerop (logand byte #x08))
902 ;; Local-multiple-call.
903 (do-local-call component pc (- (+ pc 4)) fp
904 (let ((args (logand byte #x07)))
910 (let ((num-results (logand byte #x7)))
911 (if (= num-results 7)
914 (do-return fp num-results)))
916 (if (zerop (logand byte #x08))
918 (if (if (zerop (logand byte #x04))
919 (if (zerop (logand byte #x02))
922 (if (zerop (logand byte #x02))
923 (not (pop-eval-stack))
924 (multiple-value-pop-eval-stack
930 (if (zerop (logand byte #x01))
931 (component-ref-24 component (1+ pc))
933 (component-ref-signed component (1+ pc))))
936 (byte-interpret component
937 (if (zerop (logand byte #x01))
942 (multiple-value-bind (sub-code new-pc)
943 (let ((operand (logand byte #x7)))
945 (values (component-ref component (+ pc 1))
947 (values operand (1+ pc))))
948 (funcall (the function (svref *byte-xops* sub-code))
949 component pc new-pc fp))))
950 ;; some miscellaneous inline function
952 (expand-into-inlines)
953 (byte-interpret component (1+ pc) fp))))))
955 (defun do-local-call (component pc old-pc old-fp num-args)
956 (declare (type pc pc)
957 (type return-pc old-pc)
958 (type stack-pointer old-fp)
959 (type (integer 0 #.call-arguments-limit) num-args))
960 (invoke-local-entry-point component (component-ref-24 component (1+ pc))
962 (- *eval-stack-top* num-args)
965 (defun do-tail-local-call (component pc fp num-args)
966 (declare (type code-component component) (type pc pc)
967 (type stack-pointer fp)
968 (type index num-args))
969 (let ((old-fp (eval-stack-ref (- fp 1)))
970 (old-sp (eval-stack-ref (- fp 2)))
971 (old-pc (eval-stack-ref (- fp 3)))
972 (old-component (eval-stack-ref (- fp 4)))
973 (start-of-args (- *eval-stack-top* num-args)))
974 (eval-stack-copy old-sp start-of-args num-args)
975 (setf *eval-stack-top* (+ old-sp num-args))
976 (invoke-local-entry-point component (component-ref-24 component (1+ pc))
977 old-component old-pc old-sp old-fp)))
979 (defun invoke-local-entry-point (component target old-component old-pc old-sp
980 old-fp &optional closure-vars)
981 (declare (type pc target)
982 (type return-pc old-pc)
983 (type stack-pointer old-sp old-fp)
984 (type (or null simple-vector) closure-vars))
986 (named-let more ((index (1- (length closure-vars))))
987 (unless (minusp index)
988 (push-eval-stack (svref closure-vars index))
990 (push-eval-stack old-component)
991 (push-eval-stack old-pc)
992 (push-eval-stack old-sp)
993 (push-eval-stack old-fp)
994 (multiple-value-bind (stack-frame-size entry-pc)
995 (let ((byte (component-ref component target)))
997 (values (component-ref-24 component (1+ target)) (+ target 4))
998 (values (* byte 2) (1+ target))))
999 (declare (type pc entry-pc))
1000 (let ((fp *eval-stack-top*))
1001 (allocate-eval-stack stack-frame-size)
1002 (byte-interpret component entry-pc fp))))
1004 ;;; Call a function with some arguments popped off of the interpreter
1005 ;;; stack, and restore the SP to the specified value.
1006 (defun byte-apply (function num-args restore-sp)
1007 (declare (type function function) (type index num-args))
1008 (let ((start (- *eval-stack-top* num-args)))
1009 (declare (type stack-pointer start))
1012 ,@(loop for n below 8
1013 collect `(,n (call-1 ,n)))
1016 (end (+ start num-args)))
1017 (declare (type stack-pointer end))
1018 (do ((i (1- end) (1- i)))
1020 (declare (fixnum i))
1021 (push (eval-stack-ref i) args))
1022 (setf *eval-stack-top* restore-sp)
1023 (apply function args)))))
1028 (let ((dum (gensym)))
1029 (binds `(,dum (eval-stack-ref (+ start ,i))))
1032 (setf *eval-stack-top* restore-sp)
1033 (funcall function ,@(args))))))
1036 ;;; Note: negative RET-PC is a convention for "we need multiple return
1038 (defun do-call (old-component call-pc ret-pc old-fp num-args named)
1039 (declare (type code-component old-component)
1041 (type return-pc ret-pc)
1042 (type stack-pointer old-fp)
1043 (type (integer 0 #.call-arguments-limit) num-args)
1044 (type (member t nil) named))
1045 (let* ((old-sp (- *eval-stack-top* num-args 1))
1046 (fun-or-fdefn (eval-stack-ref old-sp))
1048 (or (fdefn-function fun-or-fdefn)
1049 (with-debugger-info (old-component call-pc old-fp)
1050 (error 'undefined-function
1051 :name (fdefn-name fun-or-fdefn))))
1053 (declare (type stack-pointer old-sp)
1054 (type (or function fdefn) fun-or-fdefn)
1055 (type function function))
1058 (invoke-xep old-component ret-pc old-sp old-fp num-args function))
1060 (invoke-xep old-component ret-pc old-sp old-fp num-args
1061 (byte-closure-function function)
1062 (byte-closure-data function)))
1064 (cond ((minusp ret-pc)
1065 (let* ((ret-pc (- ret-pc))
1067 (multiple-value-list
1069 (old-component ret-pc old-fp)
1070 (byte-apply function num-args old-sp)))))
1071 (dolist (result results)
1072 (push-eval-stack result))
1073 (push-eval-stack (length results))
1074 (byte-interpret old-component ret-pc old-fp)))
1078 (old-component ret-pc old-fp)
1079 (byte-apply function num-args old-sp)))
1080 (byte-interpret old-component ret-pc old-fp)))))))
1082 (defun do-tail-call (component pc fp num-args named)
1083 (declare (type code-component component)
1085 (type stack-pointer fp)
1086 (type (integer 0 #.call-arguments-limit) num-args)
1087 (type (member t nil) named))
1088 (let* ((start-of-args (- *eval-stack-top* num-args))
1089 (fun-or-fdefn (eval-stack-ref (1- start-of-args)))
1091 (or (fdefn-function fun-or-fdefn)
1092 (with-debugger-info (component pc fp)
1093 (error 'undefined-function
1094 :name (fdefn-name fun-or-fdefn))))
1096 (old-fp (eval-stack-ref (- fp 1)))
1097 (old-sp (eval-stack-ref (- fp 2)))
1098 (old-pc (eval-stack-ref (- fp 3)))
1099 (old-component (eval-stack-ref (- fp 4))))
1100 (declare (type stack-pointer old-fp old-sp start-of-args)
1101 (type return-pc old-pc)
1102 (type (or fdefn function) fun-or-fdefn)
1103 (type function function))
1106 (eval-stack-copy old-sp start-of-args num-args)
1107 (setf *eval-stack-top* (+ old-sp num-args))
1108 (invoke-xep old-component old-pc old-sp old-fp num-args function))
1110 (eval-stack-copy old-sp start-of-args num-args)
1111 (setf *eval-stack-top* (+ old-sp num-args))
1112 (invoke-xep old-component old-pc old-sp old-fp num-args
1113 (byte-closure-function function)
1114 (byte-closure-data function)))
1116 ;; We are tail-calling native code.
1117 (cond ((null old-component)
1118 ;; We were called by native code.
1119 (byte-apply function num-args old-sp))
1121 ;; We were called for multiple values. So return multiple
1123 (let* ((old-pc (- old-pc))
1125 (multiple-value-list
1127 (old-component old-pc old-fp)
1128 (byte-apply function num-args old-sp)))))
1129 (dolist (result results)
1130 (push-eval-stack result))
1131 (push-eval-stack (length results))
1132 (byte-interpret old-component old-pc old-fp)))
1134 ;; We were called for one value. So return one value.
1137 (old-component old-pc old-fp)
1138 (byte-apply function num-args old-sp)))
1139 (byte-interpret old-component old-pc old-fp)))))))
1141 (defvar *byte-trace-calls* nil)
1143 (defun invoke-xep (old-component ret-pc old-sp old-fp num-args xep
1144 &optional closure-vars)
1145 (declare (type (or null code-component) old-component)
1146 (type index num-args)
1147 (type return-pc ret-pc)
1148 (type stack-pointer old-sp old-fp)
1149 (type byte-function xep)
1150 (type (or null simple-vector) closure-vars))
1151 ;; FIXME: Perhaps BYTE-TRACE-CALLS stuff should be conditional on SB-SHOW.
1152 (when *byte-trace-calls*
1153 (let ((*byte-trace-calls* nil)
1155 (*print-level* sb!debug:*debug-print-level*)
1156 (*print-length* sb!debug:*debug-print-length*)
1157 (sp *eval-stack-top*))
1158 (format *trace-output*
1159 "~&INVOKE-XEP: ocode= ~S[~D]~% ~
1160 osp= ~D, ofp= ~D, nargs= ~D, SP= ~D:~% ~
1161 Fun= ~S ~@[~S~]~% Args= ~S~%"
1162 old-component ret-pc old-sp old-fp num-args sp
1163 xep closure-vars (subseq *eval-stack* (- sp num-args) sp))
1164 (force-output *trace-output*)))
1168 ((typep xep 'simple-byte-function)
1169 (unless (eql (simple-byte-function-num-args xep) num-args)
1170 (with-debugger-info (old-component ret-pc old-fp)
1171 (error "wrong number of arguments")))
1172 (simple-byte-function-entry-point xep))
1174 (let ((min (hairy-byte-function-min-args xep))
1175 (max (hairy-byte-function-max-args xep)))
1178 (with-debugger-info (old-component ret-pc old-fp)
1179 (error "not enough arguments")))
1181 (nth (- num-args min) (hairy-byte-function-entry-points xep)))
1182 ((null (hairy-byte-function-more-args-entry-point xep))
1183 (with-debugger-info (old-component ret-pc old-fp)
1184 (error "too many arguments")))
1186 (let* ((more-args-supplied (- num-args max))
1187 (sp *eval-stack-top*)
1188 (more-args-start (- sp more-args-supplied))
1189 (restp (hairy-byte-function-rest-arg-p xep))
1191 (do ((index (1- sp) (1- index))
1193 (cons (eval-stack-ref index)
1195 ((< index more-args-start) result)
1196 (declare (fixnum index))))))
1197 (declare (type index more-args-supplied)
1198 (type stack-pointer more-args-start))
1200 ((not (hairy-byte-function-keywords-p xep))
1202 (setf *eval-stack-top* (1+ more-args-start))
1203 (setf (eval-stack-ref more-args-start) rest))
1205 (unless (evenp more-args-supplied)
1206 (with-debugger-info (old-component ret-pc old-fp)
1207 (error "odd number of &KEY arguments")))
1208 ;; If there are &KEY args, then we need to leave
1209 ;; the defaulted and supplied-p values where the
1210 ;; more args currently are. There might be more or
1211 ;; fewer. And also, we need to flatten the parsed
1212 ;; args with the defaults before we scan the
1213 ;; keywords. So we copy all the &MORE args to a
1214 ;; temporary area at the end of the stack.
1215 (let* ((num-more-args
1216 (hairy-byte-function-num-more-args xep))
1217 (new-sp (+ more-args-start num-more-args))
1218 (temp (max sp new-sp))
1219 (temp-sp (+ temp more-args-supplied))
1220 (keywords (hairy-byte-function-keywords xep)))
1221 (declare (type index temp)
1222 (type stack-pointer new-sp temp-sp))
1223 (allocate-eval-stack (- temp-sp sp))
1224 (eval-stack-copy temp more-args-start more-args-supplied)
1226 (setf (eval-stack-ref more-args-start) rest)
1227 (incf more-args-start))
1228 (let ((index more-args-start))
1229 (dolist (keyword keywords)
1230 (setf (eval-stack-ref index) (cadr keyword))
1232 (when (caddr keyword)
1233 (setf (eval-stack-ref index) nil)
1235 (let ((index temp-sp)
1236 (allow (eq (hairy-byte-function-keywords-p xep)
1240 (declare (type fixnum index))
1243 (when (< index temp)
1245 (let ((key (eval-stack-ref index))
1246 (value (eval-stack-ref (1+ index))))
1247 (if (eq key :allow-other-keys)
1249 (let ((target more-args-start))
1250 (declare (type stack-pointer target))
1251 (dolist (keyword keywords
1254 (cond ((eq (car keyword) key)
1255 (setf (eval-stack-ref target) value)
1256 (when (caddr keyword)
1257 (setf (eval-stack-ref (1+ target))
1263 (incf target))))))))
1264 (when (and bogus-key-p (not allow))
1265 (with-debugger-info (old-component ret-pc old-fp)
1266 (error "unknown keyword: ~S" bogus-key))))
1267 (setf *eval-stack-top* new-sp)))))
1268 (hairy-byte-function-more-args-entry-point xep))))))))
1269 (declare (type pc entry-point))
1270 (invoke-local-entry-point (byte-function-component xep) entry-point
1271 old-component ret-pc old-sp old-fp
1274 (defun do-return (fp num-results)
1275 (declare (type stack-pointer fp) (type index num-results))
1276 (let ((old-component (eval-stack-ref (- fp 4))))
1277 (typecase old-component
1279 ;; returning to more byte-interpreted code
1280 (do-local-return old-component fp num-results))
1282 ;; returning to native code
1283 (let ((old-sp (eval-stack-ref (- fp 2))))
1286 (setf *eval-stack-top* old-sp)
1289 (let ((result (pop-eval-stack)))
1290 (setf *eval-stack-top* old-sp)
1293 (let ((results nil))
1294 (dotimes (i num-results)
1295 (push (pop-eval-stack) results))
1296 (setf *eval-stack-top* old-sp)
1297 (values-list results))))))
1299 ;; ### function end breakpoint?
1300 (error "Function-end breakpoints are not supported.")))))
1302 (defun do-local-return (old-component fp num-results)
1303 (declare (type stack-pointer fp) (type index num-results))
1304 (let ((old-fp (eval-stack-ref (- fp 1)))
1305 (old-sp (eval-stack-ref (- fp 2)))
1306 (old-pc (eval-stack-ref (- fp 3))))
1307 (declare (type (signed-byte 25) old-pc))
1309 ;; wants single value
1310 (let ((result (if (zerop num-results)
1312 (eval-stack-ref (- *eval-stack-top*
1314 (setf *eval-stack-top* old-sp)
1315 (push-eval-stack result)
1316 (byte-interpret old-component old-pc old-fp))
1317 ;; wants multiple values
1319 (eval-stack-copy old-sp
1320 (- *eval-stack-top* num-results)
1322 (setf *eval-stack-top* (+ old-sp num-results))
1323 (push-eval-stack num-results)
1324 (byte-interpret old-component (- old-pc) old-fp)))))