1 ;;; the instruction set definition for MIPS
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 (setf *assem-scheduler-p* t)
15 (setf *assem-max-locations* 68)
17 ;;;; Constants, types, conversion functions, some disassembler stuff.
19 (defun reg-tn-encoding (tn)
20 (declare (type tn tn))
25 (if (eq (sb-name (sc-sb (tn-sc tn))) 'registers)
27 (error "~S isn't a register." tn)))))
29 (defun fp-reg-tn-encoding (tn)
30 (declare (type tn tn))
31 (unless (eq (sb-name (sc-sb (tn-sc tn))) 'float-registers)
32 (error "~S isn't a floating-point register." tn))
35 ;;;(sb!disassem:set-disassem-params :instruction-alignment 32)
37 (defvar *disassem-use-lisp-reg-names* t)
39 (!def-vm-support-routine location-number (loc)
46 (ecase (sb-name (sc-sb (tn-sc loc)))
48 ;; Can happen if $ZERO or $NULL are passed in.
51 (unless (zerop (tn-offset loc))
54 (+ (tn-offset loc) 32))))
64 (defparameter reg-symbols
67 (cond ((null name) nil)
68 (t (make-symbol (concatenate 'string "$" name)))))
71 (sb!disassem:define-arg-type reg
72 :printer #'(lambda (value stream dstate)
73 (declare (stream stream) (fixnum value))
74 (let ((regname (aref reg-symbols value)))
75 (princ regname stream)
76 (sb!disassem:maybe-note-associated-storage-ref
82 (defparameter float-reg-symbols
84 (loop for n from 0 to 31 collect (make-symbol (format nil "$F~d" n)))
87 (sb!disassem:define-arg-type fp-reg
88 :printer #'(lambda (value stream dstate)
89 (declare (stream stream) (fixnum value))
90 (let ((regname (aref float-reg-symbols value)))
91 (princ regname stream)
92 (sb!disassem:maybe-note-associated-storage-ref
98 (sb!disassem:define-arg-type control-reg
101 (sb!disassem:define-arg-type relative-label
103 :use-label #'(lambda (value dstate)
104 (declare (type (signed-byte 16) value)
105 (type sb!disassem:disassem-state dstate))
106 (+ (ash (1+ value) 2) (sb!disassem:dstate-cur-addr dstate))))
108 (deftype float-format ()
109 '(member :s :single :d :double :w :word))
111 (defun float-format-value (format)
117 (sb!disassem:define-arg-type float-format
118 :printer #'(lambda (value stream dstate)
119 (declare (ignore dstate)
129 (defconstant-eqx compare-kinds
130 '(:f :un :eq :ueq :olt :ult :ole :ule :sf :ngle :seq :ngl :lt :nge :le :ngt)
133 (defconstant-eqx compare-kinds-vec
134 (apply #'vector compare-kinds)
137 (deftype compare-kind ()
138 `(member ,@compare-kinds))
140 (defun compare-kind (kind)
141 (or (position kind compare-kinds)
142 (error "Unknown floating point compare kind: ~S~%Must be one of: ~S"
146 (sb!disassem:define-arg-type compare-kind
147 :printer compare-kinds-vec)
149 (defconstant-eqx float-operations '(+ - * /) #'equalp)
151 (deftype float-operation ()
152 `(member ,@float-operations))
154 (defconstant-eqx float-operation-names
155 ;; this gets used for output only
159 (defun float-operation (op)
160 (or (position op float-operations)
161 (error "Unknown floating point operation: ~S~%Must be one of: ~S"
165 (sb!disassem:define-arg-type float-operation
166 :printer float-operation-names)
170 ;;;; Constants used by instruction emitters.
172 (defconstant special-op #b000000)
173 (defconstant bcond-op #b000001)
174 (defconstant cop0-op #b010000)
175 (defconstant cop1-op #b010001)
176 (defconstant cop2-op #b010010)
177 (defconstant cop3-op #b010011)
181 ;;;; dissassem:define-instruction-formats
183 (defconstant-eqx immed-printer
184 '(:name :tab rt (:unless (:same-as rt) ", " rs) ", " immediate)
187 ;;; for things that use rt=0 as a nop
188 (defconstant-eqx immed-zero-printer
189 '(:name :tab rt (:unless (:constant 0) ", " rs) ", " immediate)
192 (sb!disassem:define-instruction-format
193 (immediate 32 :default-printer immed-printer)
194 (op :field (byte 6 26))
195 (rs :field (byte 5 21) :type 'reg)
196 (rt :field (byte 5 16) :type 'reg)
197 (immediate :field (byte 16 0) :sign-extend t))
199 (eval-when (:compile-toplevel :load-toplevel :execute)
200 (defparameter jump-printer
201 #'(lambda (value stream dstate)
202 (let ((addr (ash value 2)))
203 (sb!disassem:maybe-note-assembler-routine addr t dstate)
204 (write addr :base 16 :radix t :stream stream)))))
206 (sb!disassem:define-instruction-format
207 (jump 32 :default-printer '(:name :tab target))
208 (op :field (byte 6 26))
209 (target :field (byte 26 0) :printer jump-printer))
211 (defconstant-eqx reg-printer
212 '(:name :tab rd (:unless (:same-as rd) ", " rs) ", " rt)
215 (sb!disassem:define-instruction-format
216 (register 32 :default-printer reg-printer)
217 (op :field (byte 6 26))
218 (rs :field (byte 5 21) :type 'reg)
219 (rt :field (byte 5 16) :type 'reg)
220 (rd :field (byte 5 11) :type 'reg)
221 (shamt :field (byte 5 6) :value 0)
222 (funct :field (byte 6 0)))
224 (sb!disassem:define-instruction-format
225 (break 32 :default-printer
226 '(:name :tab code (:unless (:constant 0) subcode)))
227 (op :field (byte 6 26) :value special-op)
228 (code :field (byte 10 16))
229 (subcode :field (byte 10 6) :value 0)
230 (funct :field (byte 6 0) :value #b001101))
232 (sb!disassem:define-instruction-format
233 (coproc-branch 32 :default-printer '(:name :tab offset))
234 (op :field (byte 6 26))
235 (funct :field (byte 10 16))
236 (offset :field (byte 16 0)))
238 (defconstant-eqx float-fmt-printer
239 '((:unless :constant funct)
240 (:choose (:unless :constant sub-funct) nil)
244 (defconstant-eqx float-printer
245 `(:name ,@float-fmt-printer
248 (:unless (:same-as fd) ", " fs)
252 (sb!disassem:define-instruction-format
253 (float 32 :default-printer float-printer)
254 (op :field (byte 6 26) :value cop1-op)
255 (filler :field (byte 1 25) :value 1)
256 (format :field (byte 4 21) :type 'float-format)
257 (ft :field (byte 5 16) :value 0)
258 (fs :field (byte 5 11) :type 'fp-reg)
259 (fd :field (byte 5 6) :type 'fp-reg)
260 (funct :field (byte 6 0)))
262 (sb!disassem:define-instruction-format
263 (float-aux 32 :default-printer float-printer)
264 (op :field (byte 6 26) :value cop1-op)
265 (filler-1 :field (byte 1 25) :value 1)
266 (format :field (byte 4 21) :type 'float-format)
267 (ft :field (byte 5 16) :type 'fp-reg)
268 (fs :field (byte 5 11) :type 'fp-reg)
269 (fd :field (byte 5 6) :type 'fp-reg)
270 (funct :field (byte 2 4))
271 (sub-funct :field (byte 4 0)))
273 (sb!disassem:define-instruction-format
277 '('f funct "." format
280 (:unless (:same-as fd) ", " fs)
282 (funct :field (byte 2 0) :type 'float-operation)
283 (funct-filler :field (byte 4 2) :value 0)
284 (ft :value nil :type 'fp-reg))
287 ;;;; Primitive emitters.
289 (define-bitfield-emitter emit-word 32
292 (define-bitfield-emitter emit-short 16
295 (define-bitfield-emitter emit-immediate-inst 32
296 (byte 6 26) (byte 5 21) (byte 5 16) (byte 16 0))
298 (define-bitfield-emitter emit-jump-inst 32
299 (byte 6 26) (byte 26 0))
301 (define-bitfield-emitter emit-register-inst 32
302 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 5 6) (byte 6 0))
304 (define-bitfield-emitter emit-break-inst 32
305 (byte 6 26) (byte 10 16) (byte 10 6) (byte 6 0))
307 (define-bitfield-emitter emit-float-inst 32
308 (byte 6 26) (byte 1 25) (byte 4 21) (byte 5 16)
309 (byte 5 11) (byte 5 6) (byte 6 0))
313 ;;;; Math instructions.
315 (defun emit-math-inst (segment dst src1 src2 reg-opcode immed-opcode
316 &optional allow-fixups)
322 (emit-register-inst segment special-op (reg-tn-encoding src1)
323 (reg-tn-encoding src2) (reg-tn-encoding dst)
326 (emit-immediate-inst segment immed-opcode (reg-tn-encoding src1)
327 (reg-tn-encoding dst) src2))
330 (error "Fixups aren't allowed."))
331 (note-fixup segment :addi src2)
332 (emit-immediate-inst segment immed-opcode (reg-tn-encoding src1)
333 (reg-tn-encoding dst) 0))))
335 (define-instruction add (segment dst src1 &optional src2)
336 (:declare (type tn dst)
337 (type (or tn (signed-byte 16) null) src1 src2))
338 (:printer register ((op special-op) (funct #b100000)))
339 (:printer immediate ((op #b001000)))
340 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
343 (emit-math-inst segment dst src1 src2 #b100000 #b001000)))
345 (define-instruction addu (segment dst src1 &optional src2)
346 (:declare (type tn dst)
347 (type (or tn (signed-byte 16) fixup null) src1 src2))
348 (:printer register ((op special-op) (funct #b100001)))
349 (:printer immediate ((op #b001001)))
350 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
353 (emit-math-inst segment dst src1 src2 #b100001 #b001001 t)))
355 (define-instruction sub (segment dst src1 &optional src2)
358 (type (or tn (integer #.(- 1 (ash 1 15)) #.(ash 1 15)) null) src1 src2))
359 (:printer register ((op special-op) (funct #b100010)))
360 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
366 (emit-math-inst segment dst src1
367 (if (integerp src2) (- src2) src2)
370 (define-instruction subu (segment dst src1 &optional src2)
374 (or tn (integer #.(- 1 (ash 1 15)) #.(ash 1 15)) fixup null) src1 src2))
375 (:printer register ((op special-op) (funct #b100011)))
376 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
382 (emit-math-inst segment dst src1
383 (if (integerp src2) (- src2) src2)
384 #b100011 #b001001 t)))
386 (define-instruction and (segment dst src1 &optional src2)
387 (:declare (type tn dst)
388 (type (or tn (unsigned-byte 16) null) src1 src2))
389 (:printer register ((op special-op) (funct #b100100)))
390 (:printer immediate ((op #b001100) (immediate nil :sign-extend nil)))
391 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
394 (emit-math-inst segment dst src1 src2 #b100100 #b001100)))
396 (define-instruction or (segment dst src1 &optional src2)
397 (:declare (type tn dst)
398 (type (or tn (unsigned-byte 16) null) src1 src2))
399 (:printer register ((op special-op) (funct #b100101)))
400 (:printer immediate ((op #b001101)))
401 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
404 (emit-math-inst segment dst src1 src2 #b100101 #b001101)))
406 (define-instruction xor (segment dst src1 &optional src2)
407 (:declare (type tn dst)
408 (type (or tn (unsigned-byte 16) null) src1 src2))
409 (:printer register ((op special-op) (funct #b100110)))
410 (:printer immediate ((op #b001110)))
411 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
414 (emit-math-inst segment dst src1 src2 #b100110 #b001110)))
416 (define-instruction nor (segment dst src1 &optional src2)
417 (:declare (type tn dst src1) (type (or tn null) src2))
418 (:printer register ((op special-op) (funct #b100111)))
419 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
422 (emit-math-inst segment dst src1 src2 #b100111 #b000000)))
424 (define-instruction slt (segment dst src1 &optional src2)
425 (:declare (type tn dst)
426 (type (or tn (signed-byte 16) null) src1 src2))
427 (:printer register ((op special-op) (funct #b101010)))
428 (:printer immediate ((op #b001010)))
429 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
432 (emit-math-inst segment dst src1 src2 #b101010 #b001010)))
434 (define-instruction sltu (segment dst src1 &optional src2)
435 (:declare (type tn dst)
436 (type (or tn (signed-byte 16) null) src1 src2))
437 (:printer register ((op special-op) (funct #b101011)))
438 (:printer immediate ((op #b001011)))
439 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
442 (emit-math-inst segment dst src1 src2 #b101011 #b001011)))
444 (defconstant-eqx divmul-printer '(:name :tab rs ", " rt) #'equalp)
446 (define-instruction div (segment src1 src2)
447 (:declare (type tn src1 src2))
448 (:printer register ((op special-op) (rd 0) (funct #b011010)) divmul-printer)
449 (:dependencies (reads src1) (reads src2) (writes :hi-reg) (writes :low-reg))
452 (emit-register-inst segment special-op (reg-tn-encoding src1)
453 (reg-tn-encoding src2) 0 0 #b011010)))
455 (define-instruction divu (segment src1 src2)
456 (:declare (type tn src1 src2))
457 (:printer register ((op special-op) (rd 0) (funct #b011011))
459 (:dependencies (reads src1) (reads src2) (writes :hi-reg) (writes :low-reg))
462 (emit-register-inst segment special-op (reg-tn-encoding src1)
463 (reg-tn-encoding src2) 0 0 #b011011)))
465 (define-instruction mult (segment src1 src2)
466 (:declare (type tn src1 src2))
467 (:printer register ((op special-op) (rd 0) (funct #b011000)) divmul-printer)
468 (:dependencies (reads src1) (reads src2) (writes :hi-reg) (writes :low-reg))
471 (emit-register-inst segment special-op (reg-tn-encoding src1)
472 (reg-tn-encoding src2) 0 0 #b011000)))
474 (define-instruction multu (segment src1 src2)
475 (:declare (type tn src1 src2))
476 (:printer register ((op special-op) (rd 0) (funct #b011001)))
477 (:dependencies (reads src1) (reads src2) (writes :hi-reg) (writes :low-reg))
480 (emit-register-inst segment special-op (reg-tn-encoding src1)
481 (reg-tn-encoding src2) 0 0 #b011001)))
483 (defun emit-shift-inst (segment opcode dst src1 src2)
489 (emit-register-inst segment special-op (reg-tn-encoding src2)
490 (reg-tn-encoding src1) (reg-tn-encoding dst)
491 0 (logior #b000100 opcode)))
493 (emit-register-inst segment special-op 0 (reg-tn-encoding src1)
494 (reg-tn-encoding dst) src2 opcode))))
496 (defconstant-eqx shift-printer
499 (:unless (:same-as rd) ", " rt)
500 ", " (:cond ((rs :constant 0) shamt)
504 (define-instruction sll (segment dst src1 &optional src2)
505 (:declare (type tn dst)
506 (type (or tn (unsigned-byte 5) null) src1 src2))
507 (:printer register ((op special-op) (rs 0) (shamt nil) (funct #b000000))
509 (:printer register ((op special-op) (funct #b000100)) shift-printer)
510 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
513 (emit-shift-inst segment #b00 dst src1 src2)))
515 (define-instruction sra (segment dst src1 &optional src2)
516 (:declare (type tn dst)
517 (type (or tn (unsigned-byte 5) null) src1 src2))
518 (:printer register ((op special-op) (rs 0) (shamt nil) (funct #b000011))
520 (:printer register ((op special-op) (funct #b000111)) shift-printer)
521 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
524 (emit-shift-inst segment #b11 dst src1 src2)))
526 (define-instruction srl (segment dst src1 &optional src2)
527 (:declare (type tn dst)
528 (type (or tn (unsigned-byte 5) null) src1 src2))
529 (:printer register ((op special-op) (rs 0) (shamt nil) (funct #b000010))
531 (:printer register ((op special-op) (funct #b000110)) shift-printer)
532 (:dependencies (reads src1) (if src2 (reads src2) (reads dst)) (writes dst))
535 (emit-shift-inst segment #b10 dst src1 src2)))
538 ;;;; Floating point math.
540 (define-instruction float-op (segment operation format dst src1 src2)
541 (:declare (type float-operation operation)
542 (type float-format format)
543 (type tn dst src1 src2))
544 (:printer float-op ())
545 (:dependencies (reads src1) (reads src2) (writes dst))
548 (emit-float-inst segment cop1-op 1 (float-format-value format)
549 (fp-reg-tn-encoding src2) (fp-reg-tn-encoding src1)
550 (fp-reg-tn-encoding dst) (float-operation operation))))
552 (defconstant-eqx float-unop-printer
553 `(:name ,@float-fmt-printer :tab fd (:unless (:same-as fd) ", " fs))
556 (define-instruction fabs (segment format dst &optional (src dst))
557 (:declare (type float-format format) (type tn dst src))
558 (:printer float ((funct #b000101)) float-unop-printer)
559 (:dependencies (reads src) (writes dst))
562 (emit-float-inst segment cop1-op 1 (float-format-value format)
563 0 (fp-reg-tn-encoding src) (fp-reg-tn-encoding dst)
566 (define-instruction fneg (segment format dst &optional (src dst))
567 (:declare (type float-format format) (type tn dst src))
568 (:printer float ((funct #b000111)) float-unop-printer)
569 (:dependencies (reads src) (writes dst))
572 (emit-float-inst segment cop1-op 1 (float-format-value format)
573 0 (fp-reg-tn-encoding src) (fp-reg-tn-encoding dst)
576 (define-instruction fcvt (segment format1 format2 dst src)
577 (:declare (type float-format format1 format2) (type tn dst src))
578 (:printer float-aux ((funct #b10) (sub-funct nil :type 'float-format))
579 `(:name "." sub-funct "." format :tab fd ", " fs))
580 (:dependencies (reads src) (writes dst))
583 (emit-float-inst segment cop1-op 1 (float-format-value format2) 0
584 (fp-reg-tn-encoding src) (fp-reg-tn-encoding dst)
585 (logior #b100000 (float-format-value format1)))))
587 (define-instruction fcmp (segment operation format fs ft)
588 (:declare (type compare-kind operation)
589 (type float-format format)
591 (:printer float-aux ((fd 0) (funct #b11) (sub-funct nil :type 'compare-kind))
592 `(:name "-" sub-funct "." format :tab fs ", " ft))
593 (:dependencies (reads fs) (reads ft) (writes :float-status))
596 (emit-float-inst segment cop1-op 1 (float-format-value format)
597 (fp-reg-tn-encoding ft) (fp-reg-tn-encoding fs) 0
598 (logior #b110000 (compare-kind operation)))))
601 ;;;; Branch/Jump instructions.
603 (defun emit-relative-branch (segment opcode r1 r2 target)
604 (emit-back-patch segment 4
605 #'(lambda (segment posn)
606 (emit-immediate-inst segment
610 (reg-tn-encoding r1))
613 (reg-tn-encoding r2))
614 (ash (- (label-position target)
618 (define-instruction b (segment target)
619 (:declare (type label target))
620 (:printer immediate ((op #b000100) (rs 0) (rt 0)
621 (immediate nil :type 'relative-label))
622 '(:name :tab immediate))
626 (emit-relative-branch segment #b000100 0 0 target)))
628 (define-instruction bal (segment target)
629 (:declare (type label target))
630 (:printer immediate ((op bcond-op) (rs 0) (rt #b01001)
631 (immediate nil :type 'relative-label))
632 '(:name :tab immediate))
636 (emit-relative-branch segment bcond-op 0 #b10001 target)))
639 (define-instruction beq (segment r1 r2-or-target &optional target)
640 (:declare (type tn r1)
641 (type (or tn fixnum label) r2-or-target)
642 (type (or label null) target))
643 (:printer immediate ((op #b000100) (immediate nil :type 'relative-label)))
645 (:dependencies (reads r1) (reads r2-or-target))
649 (setf target r2-or-target)
650 (setf r2-or-target 0))
651 (emit-relative-branch segment #b000100 r1 r2-or-target target)))
653 (define-instruction bne (segment r1 r2-or-target &optional target)
654 (:declare (type tn r1)
655 (type (or tn fixnum label) r2-or-target)
656 (type (or label null) target))
657 (:printer immediate ((op #b000101) (immediate nil :type 'relative-label)))
659 (:dependencies (reads r1) (reads r2-or-target))
663 (setf target r2-or-target)
664 (setf r2-or-target 0))
665 (emit-relative-branch segment #b000101 r1 r2-or-target target)))
667 (defconstant-eqx cond-branch-printer
668 '(:name :tab rs ", " immediate)
671 (define-instruction blez (segment reg target)
672 (:declare (type label target) (type tn reg))
674 immediate ((op #b000110) (rt 0) (immediate nil :type 'relative-label))
677 (:dependencies (reads reg))
680 (emit-relative-branch segment #b000110 reg 0 target)))
682 (define-instruction bgtz (segment reg target)
683 (:declare (type label target) (type tn reg))
685 immediate ((op #b000111) (rt 0) (immediate nil :type 'relative-label))
688 (:dependencies (reads reg))
691 (emit-relative-branch segment #b000111 reg 0 target)))
693 (define-instruction bltz (segment reg target)
694 (:declare (type label target) (type tn reg))
696 immediate ((op bcond-op) (rt 0) (immediate nil :type 'relative-label))
699 (:dependencies (reads reg))
702 (emit-relative-branch segment bcond-op reg #b00000 target)))
704 (define-instruction bgez (segment reg target)
705 (:declare (type label target) (type tn reg))
707 immediate ((op bcond-op) (rt 1) (immediate nil :type 'relative-label))
710 (:dependencies (reads reg))
713 (emit-relative-branch segment bcond-op reg #b00001 target)))
715 (define-instruction bltzal (segment reg target)
716 (:declare (type label target) (type tn reg))
718 immediate ((op bcond-op) (rt #b01000) (immediate nil :type 'relative-label))
721 (:dependencies (reads reg) (writes :r31))
724 (emit-relative-branch segment bcond-op reg #b10000 target)))
726 (define-instruction bgezal (segment reg target)
727 (:declare (type label target) (type tn reg))
729 immediate ((op bcond-op) (rt #b01001) (immediate nil :type 'relative-label))
733 (:dependencies (reads reg) (writes :r31))
735 (emit-relative-branch segment bcond-op reg #b10001 target)))
737 (defconstant-eqx j-printer
738 '(:name :tab (:choose rs target))
741 (define-instruction j (segment target)
742 (:declare (type (or tn fixup) target))
743 (:printer register ((op special-op) (rt 0) (rd 0) (funct #b001000))
745 (:printer jump ((op #b000010)) j-printer)
747 (:dependencies (reads target))
752 (emit-register-inst segment special-op (reg-tn-encoding target)
755 (note-fixup segment :jump target)
756 (emit-jump-inst segment #b000010 0)))))
758 (define-instruction jal (segment reg-or-target &optional target)
759 (:declare (type (or null tn fixup) target)
760 (type (or tn fixup (integer -16 31)) reg-or-target))
761 (:printer register ((op special-op) (rt 0) (funct #b001001)) j-printer)
762 (:printer jump ((op #b000011)) j-printer)
764 (:dependencies (if target (writes reg-or-target) (writes :r31)))
768 (setf target reg-or-target)
769 (setf reg-or-target 31))
772 (emit-register-inst segment special-op (reg-tn-encoding target) 0
773 reg-or-target 0 #b001001))
775 (note-fixup segment :jump target)
776 (emit-jump-inst segment #b000011 0)))))
778 (define-instruction bc1f (segment target)
779 (:declare (type label target))
780 (:printer coproc-branch ((op cop1-op) (funct #x100)
781 (offset nil :type 'relative-label)))
783 (:dependencies (reads :float-status))
786 (emit-relative-branch segment cop1-op #b01000 #b00000 target)))
788 (define-instruction bc1t (segment target)
789 (:declare (type label target))
790 (:printer coproc-branch ((op cop1-op) (funct #x101)
791 (offset nil :type 'relative-label)))
793 (:dependencies (reads :float-status))
796 (emit-relative-branch segment cop1-op #b01000 #b00001 target)))
800 ;;;; Random movement instructions.
802 (define-instruction lui (segment reg value)
803 (:declare (type tn reg)
804 (type (or fixup (signed-byte 16) (unsigned-byte 16)) value))
805 (:printer immediate ((op #b001111)
806 (immediate nil :sign-extend nil :printer "#x~4,'0X")))
807 (:dependencies (writes reg))
810 (when (fixup-p value)
811 (note-fixup segment :lui value)
813 (emit-immediate-inst segment #b001111 0 (reg-tn-encoding reg) value)))
815 (defconstant-eqx mvsreg-printer '(:name :tab rd)
818 (define-instruction mfhi (segment reg)
819 (:declare (type tn reg))
820 (:printer register ((op special-op) (rs 0) (rt 0) (funct #b010000))
822 (:dependencies (reads :hi-reg) (writes reg))
825 (emit-register-inst segment special-op 0 0 (reg-tn-encoding reg) 0
828 (define-instruction mthi (segment reg)
829 (:declare (type tn reg))
830 (:printer register ((op special-op) (rs 0) (rt 0) (funct #b010001))
832 (:dependencies (reads reg) (writes :hi-reg))
835 (emit-register-inst segment special-op 0 0 (reg-tn-encoding reg) 0
838 (define-instruction mflo (segment reg)
839 (:declare (type tn reg))
840 (:printer register ((op special-op) (rs 0) (rt 0) (funct #b010010))
842 (:dependencies (reads :low-reg) (writes reg))
845 (emit-register-inst segment special-op 0 0 (reg-tn-encoding reg) 0
848 (define-instruction mtlo (segment reg)
849 (:declare (type tn reg))
850 (:printer register ((op special-op) (rs 0) (rt 0) (funct #b010011))
852 (:dependencies (reads reg) (writes :low-reg))
855 (emit-register-inst segment special-op 0 0 (reg-tn-encoding reg) 0
858 (define-instruction move (segment dst src)
859 (:declare (type tn dst src))
860 (:printer register ((op special-op) (rt 0) (funct #b100001))
861 '(:name :tab rd ", " rs))
862 (:attributes flushable)
863 (:dependencies (reads src) (writes dst))
866 (emit-register-inst segment special-op (reg-tn-encoding src) 0
867 (reg-tn-encoding dst) 0 #b100001)))
869 (define-instruction fmove (segment format dst src)
870 (:declare (type float-format format) (type tn dst src))
871 (:printer float ((funct #b000110)) '(:name "." format :tab fd ", " fs))
872 (:attributes flushable)
873 (:dependencies (reads src) (writes dst))
876 (emit-float-inst segment cop1-op 1 (float-format-value format) 0
877 (fp-reg-tn-encoding src) (fp-reg-tn-encoding dst)
880 (defun %li (reg value)
883 (inst or reg zero-tn value))
885 (inst addu reg zero-tn value))
886 ((or (signed-byte 32) (unsigned-byte 32))
887 (inst lui reg (ldb (byte 16 16) value))
888 (inst or reg (ldb (byte 16 0) value)))
891 (inst addu reg value))))
893 (define-instruction-macro li (reg value)
896 (defconstant-eqx sub-op-printer '(:name :tab rd ", " rt) #'equalp)
898 (define-instruction mtc1 (segment to from)
899 (:declare (type tn to from))
900 (:printer register ((op cop1-op) (rs #b00100) (funct 0)) sub-op-printer)
901 (:dependencies (reads from) (writes to))
904 (emit-register-inst segment cop1-op #b00100 (reg-tn-encoding from)
905 (fp-reg-tn-encoding to) 0 0)))
907 (define-instruction mtc1-odd (segment to from)
908 (:declare (type tn to from))
909 (:dependencies (reads from) (writes to))
912 (emit-register-inst segment cop1-op #b00100 (reg-tn-encoding from)
913 (1+ (fp-reg-tn-encoding to)) 0 0)))
915 (define-instruction mfc1 (segment to from)
916 (:declare (type tn to from))
917 (:printer register ((op cop1-op) (rs 0) (rd nil :type 'fp-reg) (funct 0))
919 (:dependencies (reads from) (writes to))
922 (emit-register-inst segment cop1-op #b00000 (reg-tn-encoding to)
923 (fp-reg-tn-encoding from) 0 0)))
925 (define-instruction mfc1-odd (segment to from)
926 (:declare (type tn to from))
927 (:dependencies (reads from) (writes to))
930 (emit-register-inst segment cop1-op #b00000 (reg-tn-encoding to)
931 (1+ (fp-reg-tn-encoding from)) 0 0)))
933 (define-instruction mfc1-odd2 (segment to from)
934 (:declare (type tn to from))
935 (:dependencies (reads from) (writes to))
938 (emit-register-inst segment cop1-op #b00000 (1+ (reg-tn-encoding to))
939 (fp-reg-tn-encoding from) 0 0)))
941 (define-instruction mfc1-odd3 (segment to from)
942 (:declare (type tn to from))
943 (:dependencies (reads from) (writes to))
946 (emit-register-inst segment cop1-op #b00000 (1+ (reg-tn-encoding to))
947 (1+ (fp-reg-tn-encoding from)) 0 0)))
949 (define-instruction cfc1 (segment reg cr)
950 (:declare (type tn reg) (type (unsigned-byte 5) cr))
951 (:printer register ((op cop1-op) (rs #b00010) (rd nil :type 'control-reg)
952 (funct 0)) sub-op-printer)
953 (:dependencies (reads :ctrl-stat-reg) (writes reg))
956 (emit-register-inst segment cop1-op #b00010 (reg-tn-encoding reg)
959 (define-instruction ctc1 (segment reg cr)
960 (:declare (type tn reg) (type (unsigned-byte 5) cr))
961 (:printer register ((op cop1-op) (rs #b00110) (rd nil :type 'control-reg)
962 (funct 0)) sub-op-printer)
963 (:dependencies (reads reg) (writes :ctrl-stat-reg))
966 (emit-register-inst segment cop1-op #b00110 (reg-tn-encoding reg)
971 ;;;; Random system hackery and other noise
973 (define-instruction-macro entry-point ()
977 (define-bitfield-emitter emit-break-inst 32
978 (byte 6 26) (byte 10 16) (byte 10 6) (byte 6 0))
980 (defun snarf-error-junk (sap offset &optional length-only)
981 (let* ((length (sb!sys:sap-ref-8 sap offset))
982 (vector (make-array length :element-type '(unsigned-byte 8))))
983 (declare (type sb!sys:system-area-pointer sap)
984 (type (unsigned-byte 8) length)
985 (type (simple-array (unsigned-byte 8) (*)) vector))
987 (values 0 (1+ length) nil nil))
989 (sb!kernel:copy-from-system-area sap (* n-byte-bits (1+ offset))
990 vector (* n-word-bits
992 (* length n-byte-bits))
993 (collect ((sc-offsets)
995 (lengths 1) ; the length byte
997 (error-number (sb!c:read-var-integer vector index)))
1000 (when (>= index length)
1002 (let ((old-index index))
1003 (sc-offsets (sb!c:read-var-integer vector index))
1004 (lengths (- index old-index))))
1005 (values error-number
1010 (defmacro break-cases (breaknum &body cases)
1011 (let ((bn-temp (gensym)))
1012 (collect ((clauses))
1013 (dolist (case cases)
1014 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
1015 `(let ((,bn-temp ,breaknum))
1016 (cond ,@(clauses))))))
1018 (defun break-control (chunk inst stream dstate)
1019 (declare (ignore inst))
1020 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
1021 (case (break-code chunk dstate)
1024 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
1027 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
1029 (nt "Breakpoint trap"))
1030 (#.pending-interrupt-trap
1031 (nt "Pending interrupt trap"))
1034 (#.fun-end-breakpoint-trap
1035 (nt "Function end breakpoint trap"))
1038 (define-instruction break (segment code &optional (subcode 0))
1039 (:declare (type (unsigned-byte 10) code subcode))
1040 (:printer break ((op special-op) (funct #b001101))
1041 '(:name :tab code (:unless (:constant 0) subcode))
1042 :control #'break-control )
1047 (emit-break-inst segment special-op code subcode #b001101)))
1049 (define-instruction syscall (segment)
1050 (:printer register ((op special-op) (rd 0) (rt 0) (rs 0) (funct #b001100))
1055 (emit-register-inst segment special-op 0 0 0 0 #b001100)))
1057 (define-instruction nop (segment)
1058 (:printer register ((op 0) (rd 0) (rd 0) (rs 0) (funct 0)) '(:name))
1059 (:attributes flushable)
1062 (emit-word segment 0)))
1064 (!def-vm-support-routine emit-nop (segment)
1065 (emit-word segment 0))
1067 (define-instruction word (segment word)
1068 (:declare (type (or (unsigned-byte 32) (signed-byte 32)) word))
1073 (emit-word segment word)))
1075 (define-instruction short (segment short)
1076 (:declare (type (or (unsigned-byte 16) (signed-byte 16)) short))
1081 (emit-short segment short)))
1083 (define-instruction byte (segment byte)
1084 (:declare (type (or (unsigned-byte 8) (signed-byte 8)) byte))
1089 (emit-byte segment byte)))
1092 (defun emit-header-data (segment type)
1095 #'(lambda (segment posn)
1098 (ash (+ posn (component-header-length))
1099 (- n-widetag-bits word-shift)))))))
1101 (define-instruction fun-header-word (segment)
1106 (emit-header-data segment simple-fun-header-widetag)))
1108 (define-instruction lra-header-word (segment)
1113 (emit-header-data segment return-pc-header-widetag)))
1116 (defun emit-compute-inst (segment vop dst src label temp calc)
1118 ;; We emit either 12 or 4 bytes, so we maintain 8 byte alignments.
1120 #'(lambda (segment posn delta-if-after)
1121 (let ((delta (funcall calc label posn delta-if-after)))
1122 (when (<= (- (ash 1 15)) delta (1- (ash 1 15)))
1123 (emit-back-patch segment 4
1124 #'(lambda (segment posn)
1125 (assemble (segment vop)
1127 (funcall calc label posn 0)))))
1129 #'(lambda (segment posn)
1130 (let ((delta (funcall calc label posn 0)))
1131 (assemble (segment vop)
1132 (inst lui temp (ldb (byte 16 16) delta))
1133 (inst or temp (ldb (byte 16 0) delta))
1134 (inst addu dst src temp))))))
1136 ;; code = fn - header - label-offset + other-pointer-tag
1137 (define-instruction compute-code-from-fn (segment dst src label temp)
1138 (:declare (type tn dst src temp) (type label label))
1139 (:attributes variable-length)
1140 (:dependencies (reads src) (writes dst) (writes temp))
1144 (emit-compute-inst segment vop dst src label temp
1145 #'(lambda (label posn delta-if-after)
1146 (- other-pointer-lowtag
1147 (label-position label posn delta-if-after)
1148 (component-header-length))))))
1150 ;; code = lra - other-pointer-tag - header - label-offset + other-pointer-tag
1151 ;; = lra - (header + label-offset)
1152 (define-instruction compute-code-from-lra (segment dst src label temp)
1153 (:declare (type tn dst src temp) (type label label))
1154 (:attributes variable-length)
1155 (:dependencies (reads src) (writes dst) (writes temp))
1159 (emit-compute-inst segment vop dst src label temp
1160 #'(lambda (label posn delta-if-after)
1161 (- (+ (label-position label posn delta-if-after)
1162 (component-header-length)))))))
1164 ;; lra = code + other-pointer-tag + header + label-offset - other-pointer-tag
1165 (define-instruction compute-lra-from-code (segment dst src label temp)
1166 (:declare (type tn dst src temp) (type label label))
1167 (:attributes variable-length)
1168 (:dependencies (reads src) (writes dst) (writes temp))
1172 (emit-compute-inst segment vop dst src label temp
1173 #'(lambda (label posn delta-if-after)
1174 (+ (label-position label posn delta-if-after)
1175 (component-header-length))))))
1178 ;;;; Loads and Stores
1180 (defun emit-load/store-inst (segment opcode reg base index
1181 &optional (oddhack 0))
1182 (when (fixup-p index)
1183 (note-fixup segment :addi index)
1185 (emit-immediate-inst segment opcode (reg-tn-encoding reg)
1186 (+ (reg-tn-encoding base) oddhack) index))
1188 (defconstant-eqx load-store-printer
1192 (:unless (:constant 0) "[" immediate "]"))
1195 (define-instruction lb (segment reg base &optional (index 0))
1196 (:declare (type tn reg base)
1197 (type (or (signed-byte 16) fixup) index))
1198 (:printer immediate ((op #b100000)) load-store-printer)
1199 (:dependencies (reads base) (reads :memory) (writes reg))
1202 (emit-load/store-inst segment #b100000 base reg index)))
1204 (define-instruction lh (segment reg base &optional (index 0))
1205 (:declare (type tn reg base)
1206 (type (or (signed-byte 16) fixup) index))
1207 (:printer immediate ((op #b100001)) load-store-printer)
1208 (:dependencies (reads base) (reads :memory) (writes reg))
1211 (emit-load/store-inst segment #b100001 base reg index)))
1213 (define-instruction lwl (segment reg base &optional (index 0))
1214 (:declare (type tn reg base)
1215 (type (or (signed-byte 16) fixup) index))
1216 (:printer immediate ((op #b100010)) load-store-printer)
1217 (:dependencies (reads base) (reads :memory) (writes reg))
1220 (emit-load/store-inst segment #b100010 base reg index)))
1222 (define-instruction lw (segment reg base &optional (index 0))
1223 (:declare (type tn reg base)
1224 (type (or (signed-byte 16) fixup) index))
1225 (:printer immediate ((op #b100011)) load-store-printer)
1226 (:dependencies (reads base) (reads :memory) (writes reg))
1229 (emit-load/store-inst segment #b100011 base reg index)))
1231 ;; next is just for ease of coding double-in-int c-call convention
1232 (define-instruction lw-odd (segment reg base &optional (index 0))
1233 (:declare (type tn reg base)
1234 (type (or (signed-byte 16) fixup) index))
1235 (:dependencies (reads base) (reads :memory) (writes reg))
1238 (emit-load/store-inst segment #b100011 base reg index 1)))
1240 (define-instruction lbu (segment reg base &optional (index 0))
1241 (:declare (type tn reg base)
1242 (type (or (signed-byte 16) fixup) index))
1243 (:printer immediate ((op #b100100)) load-store-printer)
1244 (:dependencies (reads base) (reads :memory) (writes reg))
1247 (emit-load/store-inst segment #b100100 base reg index)))
1249 (define-instruction lhu (segment reg base &optional (index 0))
1250 (:declare (type tn reg base)
1251 (type (or (signed-byte 16) fixup) index))
1252 (:printer immediate ((op #b100101)) load-store-printer)
1253 (:dependencies (reads base) (reads :memory) (writes reg))
1256 (emit-load/store-inst segment #b100101 base reg index)))
1258 (define-instruction lwr (segment reg base &optional (index 0))
1259 (:declare (type tn reg base)
1260 (type (or (signed-byte 16) fixup) index))
1261 (:printer immediate ((op #b100110)) load-store-printer)
1262 (:dependencies (reads base) (reads :memory) (writes reg))
1265 (emit-load/store-inst segment #b100110 base reg index)))
1267 (define-instruction sb (segment reg base &optional (index 0))
1268 (:declare (type tn reg base)
1269 (type (or (signed-byte 16) fixup) index))
1270 (:printer immediate ((op #b101000)) load-store-printer)
1271 (:dependencies (reads base) (reads reg) (writes :memory))
1274 (emit-load/store-inst segment #b101000 base reg index)))
1276 (define-instruction sh (segment reg base &optional (index 0))
1277 (:declare (type tn reg base)
1278 (type (or (signed-byte 16) fixup) index))
1279 (:printer immediate ((op #b101001)) load-store-printer)
1280 (:dependencies (reads base) (reads reg) (writes :memory))
1283 (emit-load/store-inst segment #b101001 base reg index)))
1285 (define-instruction swl (segment reg base &optional (index 0))
1286 (:declare (type tn reg base)
1287 (type (or (signed-byte 16) fixup) index))
1288 (:printer immediate ((op #b101010)) load-store-printer)
1289 (:dependencies (reads base) (reads reg) (writes :memory))
1292 (emit-load/store-inst segment #b101010 base reg index)))
1294 (define-instruction sw (segment reg base &optional (index 0))
1295 (:declare (type tn reg base)
1296 (type (or (signed-byte 16) fixup) index))
1297 (:printer immediate ((op #b101011)) load-store-printer)
1298 (:dependencies (reads base) (reads reg) (writes :memory))
1301 (emit-load/store-inst segment #b101011 base reg index)))
1303 (define-instruction swr (segment reg base &optional (index 0))
1304 (:declare (type tn reg base)
1305 (type (or (signed-byte 16) fixup) index))
1306 (:printer immediate ((op #b101110)) load-store-printer)
1307 (:dependencies (reads base) (reads reg) (writes :memory))
1310 (emit-load/store-inst segment #b101110 base reg index)))
1313 (defun emit-fp-load/store-inst (segment opcode reg odd base index)
1314 (when (fixup-p index)
1315 (note-fixup segment :addi index)
1317 (emit-immediate-inst segment opcode (reg-tn-encoding base)
1318 (+ (fp-reg-tn-encoding reg) odd) index))
1320 (define-instruction lwc1 (segment reg base &optional (index 0))
1321 (:declare (type tn reg base)
1322 (type (or (signed-byte 16) fixup) index))
1323 (:printer immediate ((op #b110001) (rt nil :type 'fp-reg)) load-store-printer)
1324 (:dependencies (reads base) (reads :memory) (writes reg))
1327 (emit-fp-load/store-inst segment #b110001 reg 0 base index)))
1329 (define-instruction lwc1-odd (segment reg base &optional (index 0))
1330 (:declare (type tn reg base)
1331 (type (or (signed-byte 16) fixup) index))
1332 (:dependencies (reads base) (reads :memory) (writes reg))
1335 (emit-fp-load/store-inst segment #b110001 reg 1 base index)))
1337 (define-instruction swc1 (segment reg base &optional (index 0))
1338 (:declare (type tn reg base)
1339 (type (or (signed-byte 16) fixup) index))
1340 (:printer immediate ((op #b111001) (rt nil :type 'fp-reg)) load-store-printer)
1341 (:dependencies (reads base) (reads reg) (writes :memory))
1344 (emit-fp-load/store-inst segment #b111001 reg 0 base index)))
1346 (define-instruction swc1-odd (segment reg base &optional (index 0))
1347 (:declare (type tn reg base)
1348 (type (or (signed-byte 16) fixup) index))
1349 (:dependencies (reads base) (reads reg) (writes :memory))
1352 (emit-fp-load/store-inst segment #b111001 reg 1 base index)))