2 ;;; Written by William Lott
8 ; :scheduler-p nil ; t when we trust the scheduler not to "fill delay slots"
13 ;;;; Constants, types, conversion functions, some disassembler stuff.
15 (defun reg-tn-encoding (tn)
16 (declare (type tn tn))
21 (if (eq (sb-name (sc-sb (tn-sc tn))) 'registers)
23 (error "~S isn't a register." tn)))))
25 (defun fp-reg-tn-encoding (tn)
26 (declare (type tn tn))
27 (unless (eq (sb-name (sc-sb (tn-sc tn))) 'float-registers)
28 (error "~S isn't a floating-point register." tn))
31 ;(sb!disassem:set-disassem-params :instruction-alignment 32)
33 (defvar *disassem-use-lisp-reg-names* t)
35 (!def-vm-support-routine location-number (loc)
42 (ecase (sb-name (sc-sb (tn-sc loc)))
44 ;; Can happen if $ZERO or $NULL are passed in.
47 (unless (zerop (tn-offset loc))
50 (+ (tn-offset loc) 32))))
60 (defparameter reg-symbols
63 (cond ((null name) nil)
64 (t (make-symbol (concatenate 'string "$" name)))))
67 (sb!disassem:define-arg-type reg
68 :printer #'(lambda (value stream dstate)
69 (declare (type stream stream) (fixnum value))
70 (let ((regname (aref reg-symbols value)))
71 (princ regname stream)
72 (sb!disassem:maybe-note-associated-storage-ref
78 (defparameter float-reg-symbols
80 (loop for n from 0 to 31 collect (make-symbol (format nil "$F~d" n)))
83 (sb!disassem:define-arg-type fp-reg
84 :printer #'(lambda (value stream dstate)
85 (declare (type stream stream) (fixnum value))
86 (let ((regname (aref float-reg-symbols value)))
87 (princ regname stream)
88 (sb!disassem:maybe-note-associated-storage-ref
94 (eval-when (:compile-toplevel :load-toplevel :execute)
95 (defparameter bo-kind-names
96 #(:bo-dnzf :bo-dnzfp :bo-dzf :bo-dzfp :bo-f :bo-fp nil nil
97 :bo-dnzt :bo-dnztp :bo-dzt :bo-dztp :bo-t :bo-tp nil nil
98 :bo-dnz :bo-dnzp :bo-dz :bo-dzp :bo-u nil nil nil
99 nil nil nil nil nil nil nil nil)))
101 (sb!disassem:define-arg-type bo-field
102 :printer #'(lambda (value stream dstate)
103 (declare (ignore dstate)
106 (princ (svref bo-kind-names value) stream)))
108 (eval-when (:compile-toplevel :load-toplevel :execute)
109 (defun valid-bo-encoding (enc)
110 (or (if (integerp enc)
111 (and (= enc (logand #x1f enc))
112 (not (null (svref bo-kind-names enc)))
114 (and enc (position enc bo-kind-names)))
115 (error "Invalid BO field spec: ~s" enc)))
119 (defparameter cr-bit-names #(:lt :gt :eq :so))
120 (defparameter cr-bit-inverse-names #(:ge :le :ne :ns))
122 (defparameter cr-field-names #(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))
124 (defun valid-cr-bit-encoding (enc &optional error-p)
125 (or (if (integerp enc)
126 (and (= enc (logand 3 enc))
128 (position enc cr-bit-names)
129 (if error-p (error "Invalid condition bit specifier : ~s" enc))))
131 (defun valid-cr-field-encoding (enc)
132 (let* ((field (if (integerp enc)
133 (and (= enc (logand #x7 enc)))
134 (position enc cr-field-names))))
137 (error "Invalid condition register field specifier : ~s" enc))))
139 (defun valid-bi-encoding (enc)
143 (and (= enc (logand 31 enc)) enc)
144 (position enc cr-bit-names))
145 (+ (valid-cr-field-encoding (car enc))
146 (valid-cr-bit-encoding (cadr enc))))
147 (error "Invalid BI field spec : ~s" enc)))
149 (sb!disassem:define-arg-type bi-field
150 :printer #'(lambda (value stream dstate)
151 (declare (ignore dstate)
153 (type (unsigned-byte 5) value))
154 (let* ((bitname (svref cr-bit-names (logand 3 value)))
155 (crfield (ash value -2)))
156 (declare (type (unsigned-byte 3) crfield))
158 (princ bitname stream)
159 (princ (list (svref cr-field-names crfield) bitname) stream)))))
161 (sb!disassem:define-arg-type crf
162 :printer #'(lambda (value stream dstate)
163 (declare (ignore dstate)
165 (type (unsigned-byte 3) value))
166 (princ (svref cr-field-names value) stream)))
168 (sb!disassem:define-arg-type relative-label
170 :use-label #'(lambda (value dstate)
171 (declare (type (signed-byte 14) value)
172 (type sb!disassem:disassem-state dstate))
173 (+ (ash value 2) (sb!disassem:dstate-cur-addr dstate))))
175 (eval-when (:compile-toplevel :load-toplevel :execute)
176 (defparameter trap-values-alist '((:t . 31) (:lt . 16) (:le . 20) (:eq . 4) (:lng . 6)
177 (:ge .12) (:ne . 24) (:ng . 20) (:llt . 2) (:f . 0)
178 (:lle . 6) (:lge . 5) (:lgt . 1) (:lnl . 5))))
181 (defun valid-tcond-encoding (enc)
182 (or (and (if (integerp enc) (= (logand 31 enc) enc)) enc)
183 (cdr (assoc enc trap-values-alist))
184 (error "Unknown trap condition: ~s" enc)))
186 (sb!disassem:define-arg-type to-field
188 :printer #'(lambda (value stream dstate)
189 (declare (ignore dstate)
192 (princ (or (car (rassoc value trap-values-alist))
196 (defun snarf-error-junk (sap offset &optional length-only)
197 (let* ((length (sb!sys:sap-ref-8 sap offset))
198 (vector (make-array length :element-type '(unsigned-byte 8))))
199 (declare (type sb!sys:system-area-pointer sap)
200 (type (unsigned-byte 8) length)
201 (type (simple-array (unsigned-byte 8) (*)) vector))
203 (values 0 (1+ length) nil nil))
205 (sb!kernel:copy-from-system-area sap (* sb!vm:n-byte-bits (1+ offset))
206 vector (* sb!vm:n-word-bits
207 sb!vm:vector-data-offset)
208 (* length sb!vm:n-byte-bits))
209 (collect ((sc-offsets)
211 (lengths 1) ; the length byte
213 (error-number (sb!c:read-var-integer vector index)))
216 (when (>= index length)
218 (let ((old-index index))
219 (sc-offsets (sb!c:read-var-integer vector index))
220 (lengths (- index old-index))))
226 (defun emit-conditional-branch (segment bo bi target &optional aa-p lk-p)
227 (declare (type boolean aa-p lk-p))
228 (let* ((bo (valid-bo-encoding bo))
229 (bi (valid-bi-encoding bi))
230 (aa-bit (if aa-p 1 0))
231 (lk-bit (if lk-p 1 0)))
232 (if aa-p ; Not bloody likely, bwth.
233 (emit-b-form-inst segment 16 bo bi target aa-bit lk-bit)
234 ;; the target may be >32k away, in which case we have to invert the
235 ;; test and do an absolute branch
237 ;; We emit either 4 or 8 bytes, so I think we declare this as
238 ;; preserving 4 byte alignment. If this gives us no joy, we can
239 ;; stick a nop in the long branch and then we will be
240 ;; preserving 8 byte alignment
241 segment 8 2 ; 2^2 is 4 byte alignment. I think
242 #'(lambda (segment posn magic-value)
243 (let ((delta (ash (- (label-position target posn magic-value) posn)
245 (when (typep delta '(signed-byte 14))
246 (emit-back-patch segment 4
247 #'(lambda (segment posn)
250 (ash (- (label-position target) posn) -2)
253 #'(lambda (segment posn)
254 (let ((bo (logxor 8 bo))) ;; invert the test
255 (emit-b-form-inst segment 16 bo bi
256 2 ; skip over next instruction
258 (emit-back-patch segment 4
259 #'(lambda (segment posn)
260 (emit-i-form-branch segment target lk-p)))))
265 ; non-absolute I-form: B, BL.
266 (defun emit-i-form-branch (segment target &optional lk-p)
267 (let* ((lk-bit (if lk-p 1 0)))
270 (note-fixup segment :b target)
271 (emit-i-form-inst segment 18 0 0 lk-bit))
273 (emit-back-patch segment 4
274 #'(lambda (segment posn)
278 (ash (- (label-position target) posn) -2)
282 (eval-when (:compile-toplevel :execute :load-toplevel)
283 (defparameter *spr-numbers-alist* '((:xer 1) (:lr 8) (:ctr 9))))
285 (sb!disassem:define-arg-type spr
286 :printer #'(lambda (value stream dstate)
287 (declare (ignore dstate)
288 (type (unsigned-byte 10) value))
289 (let* ((name (car (rassoc value *spr-numbers-alist*))))
292 (princ value stream)))))
294 (eval-when (:compile-toplevel :load-toplevel :execute)
295 (defparameter jump-printer
296 #'(lambda (value stream dstate)
297 (let ((addr (ash value 2)))
298 (sb!disassem:maybe-note-assembler-routine addr t dstate)
299 (write addr :base 16 :radix t :stream stream)))))
303 ;;;; dissassem:define-instruction-formats
305 (eval-when (:compile-toplevel :execute)
306 (defmacro ppc-byte (startbit &optional (endbit startbit))
307 (unless (and (typep startbit '(unsigned-byte 32))
308 (typep endbit '(unsigned-byte 32))
309 (>= endbit startbit))
311 ``(byte ,(1+ ,(- endbit startbit)) ,(- 31 ,endbit)))
313 (defparameter *ppc-field-specs-alist*
314 `((aa :field ,(ppc-byte 30))
315 (ba :field ,(ppc-byte 11 15) :type 'bi-field)
316 (bb :field ,(ppc-byte 16 20) :type 'bi-field)
317 (bd :field ,(ppc-byte 16 29) :type 'relative-label)
318 (bf :field ,(ppc-byte 6 8) :type 'crf)
319 (bfa :field ,(ppc-byte 11 13) :type 'crf)
320 (bi :field ,(ppc-byte 11 15) :type 'bi-field)
321 (bo :field ,(ppc-byte 6 10) :type 'bo-field)
322 (bt :field ,(ppc-byte 6 10) :type 'bi-field)
323 (d :field ,(ppc-byte 16 31) :sign-extend t)
324 (flm :field ,(ppc-byte 7 14) :sign-extend nil)
325 (fra :field ,(ppc-byte 11 15) :type 'fp-reg)
326 (frb :field ,(ppc-byte 16 20) :type 'fp-reg)
327 (frc :field ,(ppc-byte 21 25) :type 'fp-reg)
328 (frs :field ,(ppc-byte 6 10) :type 'fp-reg)
329 (frt :field ,(ppc-byte 6 10) :type 'fp-reg)
330 (fxm :field ,(ppc-byte 12 19) :sign-extend nil)
331 (l :field ,(ppc-byte 10) :sign-extend nil)
332 (li :field ,(ppc-byte 6 29) :sign-extend t :type 'relative-label)
333 (li-abs :field ,(ppc-byte 6 29) :sign-extend t :printer jump-printer)
334 (lk :field ,(ppc-byte 31))
335 (mb :field ,(ppc-byte 21 25) :sign-extend nil)
336 (me :field ,(ppc-byte 26 30) :sign-extend nil)
337 (nb :field ,(ppc-byte 16 20) :sign-extend nil)
338 (oe :field ,(ppc-byte 21))
339 (ra :field ,(ppc-byte 11 15) :type 'reg)
340 (rb :field ,(ppc-byte 16 20) :type 'reg)
341 (rc :field ,(ppc-byte 31))
342 (rs :field ,(ppc-byte 6 10) :type 'reg)
343 (rt :field ,(ppc-byte 6 10) :type 'reg)
344 (sh :field ,(ppc-byte 16 20) :sign-extend nil)
345 (si :field ,(ppc-byte 16 31) :sign-extend t)
346 (spr :field ,(ppc-byte 11 20) :type 'spr)
347 (to :field ,(ppc-byte 6 10) :type 'to-field)
348 (u :field ,(ppc-byte 16 19) :sign-extend nil)
349 (ui :field ,(ppc-byte 16 31) :sign-extend nil)
350 (xo21-30 :field ,(ppc-byte 21 30) :sign-extend nil)
351 (xo22-30 :field ,(ppc-byte 22 30) :sign-extend nil)
352 (xo26-30 :field ,(ppc-byte 26 30) :sign-extend nil)))
356 (sb!disassem:define-instruction-format (instr 32)
357 (op :field (byte 6 26))
358 (other :field (byte 26 0)))
360 (sb!disassem:define-instruction-format (xinstr 32 :default-printer '(:name :tab data))
361 (op-to-a :field (byte 16 16))
362 (data :field (byte 16 0)))
364 (sb!disassem:define-instruction-format (sc 32 :default-printer '(:name :tab rest))
365 (op :field (byte 6 26))
366 (rest :field (byte 26 0) :value 2))
370 (macrolet ((def-ppc-iformat ((name &optional default-printer) &rest specs)
371 (flet ((specname-field (specname)
372 (or (assoc specname *ppc-field-specs-alist*)
373 (error "Unknown ppc instruction field spec ~s" specname))))
374 (labels ((spec-field (spec)
376 (specname-field spec)
378 (cdr (specname-field (cadr spec)))))))
379 (collect ((field (list '(op :field (byte 6 26)))))
381 (field (spec-field spec)))
382 `(sb!disassem:define-instruction-format (,name 32 ,@(if default-printer `(:default-printer ,default-printer)))
385 (def-ppc-iformat (i '(:name :tab li))
388 (def-ppc-iformat (i-abs '(:name :tab li-abs))
391 (def-ppc-iformat (b '(:name :tab bo "," bi "," bd))
394 (def-ppc-iformat (d '(:name :tab rt "," d "(" ra ")"))
397 (def-ppc-iformat (d-si '(:name :tab rt "," ra "," si ))
400 (def-ppc-iformat (d-rs '(:name :tab rs "," d "(" ra ")"))
403 (def-ppc-iformat (d-rs-ui '(:name :tab ra "," rs "," ui))
406 (def-ppc-iformat (d-crf-si)
409 (def-ppc-iformat (d-crf-ui)
412 (def-ppc-iformat (d-to '(:name :tab to "," ra "," si))
415 (def-ppc-iformat (d-frt '(:name :tab frt "," d "(" ra ")"))
418 (def-ppc-iformat (d-frs '(:name :tab frs "," d "(" ra ")"))
423 ;;; There are around ... oh, 28 or so ... variants on the "X" format.
424 ;;; Some of them are only used by one instruction; some are used by dozens.
425 ;;; Some aren't used by instructions that we generate ...
427 (def-ppc-iformat (x '(:name :tab rt "," ra "," rb))
428 rt ra rb (xo xo21-30))
430 (def-ppc-iformat (x-1 '(:name :tab rt "," ra "," nb))
431 rt ra nb (xo xo21-30))
433 (def-ppc-iformat (x-4 '(:name :tab rt))
436 (def-ppc-iformat (x-5 '(:name :tab ra "," rs "," rb))
437 rs ra rb (xo xo21-30) rc)
439 (def-ppc-iformat (x-7 '(:name :tab ra "," rs "," rb))
440 rs ra rb (xo xo21-30))
442 (def-ppc-iformat (x-8 '(:name :tab ra "," rs "," nb))
443 rs ra nb (xo xo21-30))
445 (def-ppc-iformat (x-9 '(:name :tab ra "," rs "," sh))
446 rs ra sh (xo xo21-30) rc)
448 (def-ppc-iformat (x-10 '(:name :tab ra "," rs))
449 rs ra (xo xo21-30) rc)
451 (def-ppc-iformat (x-14 '(:name :tab bf "," l "," ra "," rb))
452 bf l ra rb (xo xo21-30))
454 (def-ppc-iformat (x-15 '(:name :tab bf "," l "," fra "," frb))
455 bf l fra frb (xo xo21-30))
457 (def-ppc-iformat (x-18 '(:name :tab bf))
460 (def-ppc-iformat (x-19 '(:name :tab to "," ra "," rb))
461 to ra rb (xo xo21-30))
463 (def-ppc-iformat (x-20 '(:name :tab frt "," ra "," rb))
464 frt ra rb (xo xo21-30))
466 (def-ppc-iformat (x-21 '(:name :tab frt "," rb))
467 frt rb (xo xo21-30) rc)
469 (def-ppc-iformat (x-22 '(:name :tab frt))
472 (def-ppc-iformat (x-23 '(:name :tab ra "," frs "," rb))
473 frs ra rb (xo xo21-30))
475 (def-ppc-iformat (x-24 '(:name :tab bt))
478 (def-ppc-iformat (x-25 '(:name :tab ra "," rb))
481 (def-ppc-iformat (x-26 '(:name :tab rb))
484 (def-ppc-iformat (x-27 '(:name))
490 (def-ppc-iformat (xl '(:name :tab bt "," ba "," bb))
491 bt ba bb (xo xo21-30))
493 (def-ppc-iformat (xl-bo-bi '(:name :tab bo "," bi))
494 bo bi (xo xo21-30) lk)
496 (def-ppc-iformat (xl-cr '(:name :tab bf "," bfa))
499 (def-ppc-iformat (xl-xo '(:name))
505 (def-ppc-iformat (xfx)
508 (def-ppc-iformat (xfx-fxm '(:name :tab fxm "," rs))
511 (def-ppc-iformat (xfl '(:name :tab flm "," frb))
512 flm frb (xo xo21-30) rc)
517 (def-ppc-iformat (xo '(:name :tab rt "," ra "," rb))
518 rt ra rb oe (xo xo22-30) rc)
520 (def-ppc-iformat (xo-oe '(:name :tab rt "," ra "," rb))
521 rt ra rb (xo xo22-30) rc)
523 (def-ppc-iformat (xo-a '(:name :tab rt "," ra))
524 rt ra oe (xo xo22-30) rc)
529 (def-ppc-iformat (a '(:name :tab frt "," fra "," frb "," frc))
530 frt fra frb frc (xo xo26-30) rc)
532 (def-ppc-iformat (a-tab '(:name :tab frt "," fra "," frb))
533 frt fra frb (xo xo26-30) rc)
535 (def-ppc-iformat (a-tac '(:name :tab frt "," fra "," frc))
536 frt fra frc (xo xo26-30) rc)
538 (def-ppc-iformat (a-tbc '(:name :tab frt "," frb "," frc))
539 frt frb frc (xo xo26-30) rc)
542 (def-ppc-iformat (m '(:name :tab ra "," rs "," rb "," mb "," me))
545 (def-ppc-iformat (m-sh '(:name :tab ra "," rs "," sh "," mb "," me))
551 ;;;; Primitive emitters.
554 (define-bitfield-emitter emit-word 32
557 (define-bitfield-emitter emit-short 16
560 (define-bitfield-emitter emit-i-form-inst 32
561 (byte 6 26) (byte 24 2) (byte 1 1) (byte 1 0))
563 (define-bitfield-emitter emit-b-form-inst 32
564 (byte 6 26) (byte 5 21) (byte 5 16) (byte 14 2) (byte 1 1) (byte 1 0))
566 (define-bitfield-emitter emit-sc-form-inst 32
567 (byte 6 26) (byte 26 0))
569 (define-bitfield-emitter emit-d-form-inst 32
570 (byte 6 26) (byte 5 21) (byte 5 16) (byte 16 0))
572 ; Also used for XL-form. What's the difference ?
573 (define-bitfield-emitter emit-x-form-inst 32
574 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 10 1) (byte 1 0))
576 (define-bitfield-emitter emit-xfx-form-inst 32
577 (byte 6 26) (byte 5 21) (byte 10 11) (byte 10 1) (byte 1 0))
579 (define-bitfield-emitter emit-xfl-form-inst 32
580 (byte 6 26) (byte 10 16) (byte 5 11) (byte 10 1) (byte 1 0))
583 (define-bitfield-emitter emit-xo-form-inst 32
584 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 1 10) (byte 9 1) (byte 1 0))
586 (define-bitfield-emitter emit-a-form-inst 32
587 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 5 6) (byte 5 1) (byte 1 0))
592 (defun unimp-control (chunk inst stream dstate)
593 (declare (ignore inst))
594 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
595 (case (xinstr-data chunk dstate)
598 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
601 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
602 (#.sb!vm:object-not-list-trap
603 (nt "Object not list trap"))
604 (#.sb!vm:breakpoint-trap
605 (nt "Breakpoint trap"))
606 (#.sb!vm:pending-interrupt-trap
607 (nt "Pending interrupt trap"))
610 (#.sb!vm:fun-end-breakpoint-trap
611 (nt "Function end breakpoint trap"))
612 (#.sb!vm:object-not-instance-trap
613 (nt "Object not instance trap"))
616 (eval-when (:compile-toplevel :execute)
618 (defun classify-dependencies (deplist)
619 (collect ((reads) (writes))
620 (dolist (dep deplist)
623 (writes (writes dep))))
624 (values (reads) (writes)))))
626 (macrolet ((define-xo-instruction
627 (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
628 `(define-instruction ,name (segment rt ra rb)
629 (:printer xo ((op ,op ) (xo ,xo) (oe ,(if oe-p 1 0)) (rc ,(if rc-p 1 0))))
630 (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer)))
631 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if (or oe-p always-writes-xer) '((writes :xer))) )
635 (emit-xo-form-inst segment ,op
642 (define-xo-oe-instruction
643 (name op xo rc-p always-reads-xer always-writes-xer cost)
644 `(define-instruction ,name (segment rt ra rb)
645 (:printer xo-oe ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
646 (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer)))
647 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))))
651 (emit-xo-form-inst segment ,op
658 (define-4-xo-instructions
659 (base op xo &key always-reads-xer always-writes-xer (cost 1))
661 (define-xo-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
662 (define-xo-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
663 (define-xo-instruction ,(symbolicate base "O") ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
664 (define-xo-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
666 (define-2-xo-oe-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
668 (define-xo-oe-instruction ,base ,op ,xo nil ,always-reads-xer ,always-writes-xer ,cost)
669 (define-xo-oe-instruction ,(symbolicate base ".") ,op ,xo t ,always-reads-xer ,always-writes-xer ,cost)))
671 (define-xo-a-instruction (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
672 `(define-instruction ,name (segment rt ra)
673 (:printer xo-a ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0)) (oe ,(if oe-p 1 0))))
674 (:dependencies (reads ra) ,@(if always-reads-xer '((reads :xer)))
675 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))) )
679 (emit-xo-form-inst segment ,op
687 (define-4-xo-a-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
689 (define-xo-a-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
690 (define-xo-a-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
691 (define-xo-a-instruction ,(symbolicate base "O") ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
692 (define-xo-a-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
694 (define-x-instruction (name op xo &key (cost 2) other-dependencies)
695 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
696 `(define-instruction ,name (segment rt ra rb)
697 (:printer x ((op ,op) (xo ,xo)))
700 (:dependencies (reads ra) (reads rb) ,@ other-reads
701 (writes rt) ,@other-writes)
703 (emit-x-form-inst segment ,op
710 (define-x-20-instruction (name op xo &key (cost 2) other-dependencies)
711 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
712 `(define-instruction ,name (segment frt ra rb)
713 (:printer x-20 ((op ,op) (xo ,xo)))
716 (:dependencies (reads ra) (reads rb) ,@other-reads
717 (writes frt) ,@other-writes)
719 (emit-x-form-inst segment ,op
720 (fp-reg-tn-encoding frt)
726 (define-x-5-instruction (name op xo rc-p &key (cost 1) other-dependencies)
727 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
728 `(define-instruction ,name (segment ra rs rb)
729 (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
732 (:dependencies (reads rb) (reads rs) ,@other-reads
733 (writes ra) ,@other-writes)
735 (emit-x-form-inst segment ,op
743 (define-x-5-st-instruction (name op xo rc-p &key (cost 1) other-dependencies)
744 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
745 `(define-instruction ,name (segment rs ra rb)
746 (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
749 (:dependencies (reads ra) (reads rb) (reads rs) ,@other-reads
752 (emit-x-form-inst segment ,op
759 (define-x-23-st-instruction (name op xo &key (cost 1) other-dependencies)
760 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
761 `(define-instruction ,name (segment frs ra rb)
762 (:printer x-23 ((op ,op) (xo ,xo)))
765 (:dependencies (reads ra) (reads rb) (reads frs) ,@other-reads
768 (emit-x-form-inst segment ,op
769 (fp-reg-tn-encoding frs)
775 (define-x-10-instruction (name op xo rc-p &key (cost 1) other-dependencies)
776 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
777 `(define-instruction ,name (segment ra rs)
778 (:printer x-10 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
781 (:dependencies (reads rs) ,@other-reads
782 (writes ra) ,@other-writes)
784 (emit-x-form-inst segment ,op
791 (define-2-x-5-instructions (name op xo &key (cost 1) other-dependencies)
793 (define-x-5-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
794 (define-x-5-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
795 :other-dependencies ,other-dependencies)))
797 (define-2-x-10-instructions (name op xo &key (cost 1) other-dependencies)
799 (define-x-10-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
800 (define-x-10-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
801 :other-dependencies ,other-dependencies)))
804 (define-x-21-instruction (name op xo rc-p &key (cost 4) other-dependencies)
805 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
806 `(define-instruction ,name (segment frt frb)
807 (:printer x-21 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
810 (:dependencies (reads frb) ,@other-reads
811 (writes frt) ,@other-writes)
813 (emit-x-form-inst segment ,op
814 (fp-reg-tn-encoding frt)
816 (fp-reg-tn-encoding frb)
820 (define-2-x-21-instructions (name op xo &key (cost 4) other-dependencies)
822 (define-x-21-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
823 (define-x-21-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
824 :other-dependencies ,other-dependencies)))
827 (define-d-si-instruction (name op &key (fixup nil) (cost 1) other-dependencies)
828 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
829 `(define-instruction ,name (segment rt ra si)
830 (:declare (type (signed-byte 16)))
831 (:printer d-si ((op ,op)))
834 (:dependencies (reads ra) ,@other-reads
835 (writes rt) ,@other-writes)
837 (when (typep si 'fixup)
839 ((:ha :l) (note-fixup segment ,fixup si)))
841 (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
843 (define-d-rs-ui-instruction (name op &key (cost 1) other-dependencies)
844 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
845 `(define-instruction ,name (segment ra rs ui)
846 (:declare (type (unsigned-byte 16) ui))
847 (:printer d-rs-ui ((op ,op)))
850 (:dependencies (reads rs) ,@other-reads
851 (writes ra) ,@other-writes)
853 (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) ui)))))
855 (define-d-instruction (name op &key (cost 2) other-dependencies pinned)
856 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
857 `(define-instruction ,name (segment rt ra si)
858 (:declare (type (signed-byte 16) si))
859 (:printer d ((op ,op)))
862 ,@(when pinned '(:pinned))
863 (:dependencies (reads ra) ,@other-reads
864 (writes rt) ,@other-writes)
866 (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
868 (define-d-frt-instruction (name op &key (cost 3) other-dependencies)
869 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
870 `(define-instruction ,name (segment frt ra si)
871 (:declare (type (signed-byte 16) si))
872 (:printer d-frt ((op ,op)))
875 (:dependencies (reads ra) ,@other-reads
876 (writes frt) ,@other-writes)
878 (emit-d-form-inst segment ,op (fp-reg-tn-encoding frt) (reg-tn-encoding ra) si)))))
880 (define-d-rs-instruction (name op &key (cost 1) other-dependencies pinned)
881 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
882 `(define-instruction ,name (segment rs ra si)
883 (:declare (type (signed-byte 16) si))
884 (:printer d-rs ((op ,op)))
887 ,@(when pinned '(:pinned))
888 (:dependencies (reads rs) (reads ra) ,@other-reads
889 (writes :memory :partially t) ,@other-writes)
891 (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) si)))))
893 (define-d-frs-instruction (name op &key (cost 1) other-dependencies)
894 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
895 `(define-instruction ,name (segment frs ra si)
896 (:declare (type (signed-byte 16) si))
897 (:printer d-frs ((op ,op)))
900 (:dependencies (reads frs) (reads ra) ,@other-reads
901 (writes :memory :partially t) ,@other-writes)
903 (emit-d-form-inst segment ,op (fp-reg-tn-encoding frs) (reg-tn-encoding ra) si)))))
905 (define-a-instruction (name op xo rc &key (cost 1) other-dependencies)
906 `(define-instruction ,name (segment frt fra frb frc)
907 (:printer a ((op ,op) (xo ,xo) (rc ,rc)))
910 (:dependencies (writes frt) (reads fra) (reads frb) (reads frc) ,@other-dependencies)
912 (emit-a-form-inst segment
914 (fp-reg-tn-encoding frt)
915 (fp-reg-tn-encoding fra)
916 (fp-reg-tn-encoding frb)
917 (fp-reg-tn-encoding frb)
921 (define-2-a-instructions (name op xo &key (cost 1) other-dependencies)
923 (define-a-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
924 (define-a-instruction ,(symbolicate name ".")
925 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
927 (define-a-tab-instruction (name op xo rc &key (cost 1) other-dependencies)
928 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
929 `(define-instruction ,name (segment frt fra frb)
930 (:printer a-tab ((op ,op) (xo ,xo) (rc ,rc)))
933 (:dependencies (reads fra) (reads frb) ,@other-reads
934 (writes frt) ,@other-writes)
936 (emit-a-form-inst segment
938 (fp-reg-tn-encoding frt)
939 (fp-reg-tn-encoding fra)
940 (fp-reg-tn-encoding frb)
945 (define-2-a-tab-instructions (name op xo &key (cost 1) other-dependencies)
947 (define-a-tab-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
948 (define-a-tab-instruction ,(symbolicate name ".")
949 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
951 (define-a-tac-instruction (name op xo rc &key (cost 1) other-dependencies)
952 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
953 `(define-instruction ,name (segment frt fra frc)
954 (:printer a-tac ((op ,op) (xo ,xo) (rc ,rc)))
957 (:dependencies (reads fra) (reads frb) ,@other-reads
958 (writes frt) ,@other-writes)
960 (emit-a-form-inst segment
962 (fp-reg-tn-encoding frt)
963 (fp-reg-tn-encoding fra)
965 (fp-reg-tn-encoding frc)
969 (define-2-a-tac-instructions (name op xo &key (cost 1) other-dependencies)
971 (define-a-tac-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
972 (define-a-tac-instruction ,(symbolicate name ".")
973 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
975 (define-crbit-instruction (name op xo)
976 `(define-instruction ,name (segment dbit abit bbit)
977 (:printer xl ((op ,op ) (xo ,xo)))
980 (:dependencies (reads :ccr) (writes :ccr))
981 (:emitter (emit-x-form-inst segment 19
982 (valid-bi-encoding dbit)
983 (valid-bi-encoding abit)
984 (valid-bi-encoding bbit)
988 ;;; The instructions, in numerical order
990 (define-instruction unimp (segment data)
991 (:declare (type (signed-byte 16) data))
992 (:printer xinstr ((op-to-a #.(logior (ash 3 10) (ash 6 5) 0)))
993 :default :control #'unimp-control)
996 (:emitter (emit-d-form-inst segment 3 6 0 data)))
998 (define-instruction twi (segment tcond ra si)
999 (:printer d-to ((op 3)))
1002 (:emitter (emit-d-form-inst segment 3 (valid-tcond-encoding tcond) (reg-tn-encoding ra) si)))
1004 (define-d-si-instruction mulli 7 :cost 5)
1005 (define-d-si-instruction subfic 8)
1007 (define-instruction cmplwi (segment crf ra &optional (ui nil ui-p))
1008 (:printer d-crf-ui ((op 10) (l 0)) '(:name :tab bf "," ra "," ui))
1009 (:dependencies (if ui-p (reads ra) (reads crf)) (writes :ccr))
1013 (setq ui ra ra crf crf :cr0))
1014 (emit-d-form-inst segment
1016 (valid-cr-field-encoding crf)
1017 (reg-tn-encoding ra)
1020 (define-instruction cmpwi (segment crf ra &optional (si nil si-p))
1021 (:printer d-crf-si ((op 11) (l 0)) '(:name :tab bf "," ra "," si))
1022 (:dependencies (if si-p (reads ra) (reads crf)) (writes :ccr))
1026 (setq si ra ra crf crf :cr0))
1027 (emit-d-form-inst segment
1029 (valid-cr-field-encoding crf)
1030 (reg-tn-encoding ra)
1033 (define-d-si-instruction addic 12 :other-dependencies ((writes :xer)))
1034 (define-d-si-instruction addic. 13 :other-dependencies ((writes :xer) (writes :ccr)))
1036 (define-d-si-instruction addi 14 :fixup :l)
1037 (define-d-si-instruction addis 15 :fixup :ha)
1039 ;; There's no real support here for branch options that decrement
1040 ;; and test the CTR :
1041 ;; (a) the instruction scheduler doesn't know that anything's happening
1043 ;; (b) Lisp may have to assume that the CTR always has a lisp
1044 ;; object/locative in it.
1046 (define-instruction bc (segment bo bi target)
1047 (:declare (type label target))
1048 (:printer b ((op 16) (aa 0) (lk 0)))
1050 (:dependencies (reads :ccr))
1052 (emit-conditional-branch segment bo bi target)))
1054 (define-instruction bcl (segment bo bi target)
1055 (:declare (type label target))
1056 (:printer b ((op 16) (aa 0) (lk 1)))
1058 (:dependencies (reads :ccr))
1060 (emit-conditional-branch segment bo bi target nil t)))
1062 (define-instruction bca (segment bo bi target)
1063 (:declare (type label target))
1064 (:printer b ((op 16) (aa 1) (lk 0)))
1066 (:dependencies (reads :ccr))
1068 (emit-conditional-branch segment bo bi target t)))
1070 (define-instruction bcla (segment bo bi target)
1071 (:declare (type label target))
1072 (:printer b ((op 16) (aa 1) (lk 1)))
1074 (:dependencies (reads :ccr))
1076 (emit-conditional-branch segment bo bi target t t)))
1078 ;;; There may (or may not) be a good reason to use this in preference to "b[la] target".
1079 ;;; I can't think of a -bad- reason ...
1081 (define-instruction bu (segment target)
1082 (:declare (type label target))
1083 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (aa 0) (lk 0))
1087 (emit-conditional-branch segment #.(valid-bo-encoding :bo-u) 0 target nil nil)))
1090 (define-instruction bt (segment bi target)
1091 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-t)) (aa 0) (lk 0))
1092 '(:name :tab bi "," bd))
1095 (emit-conditional-branch segment #.(valid-bo-encoding :bo-t) bi target nil nil)))
1097 (define-instruction bf (segment bi target)
1098 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-f)) (aa 0) (lk 0))
1099 '(:name :tab bi "," bd))
1102 (emit-conditional-branch segment #.(valid-bo-encoding :bo-f) bi target nil nil)))
1104 (define-instruction b? (segment cr-field-name cr-name &optional (target nil target-p))
1108 (setq target cr-name cr-name cr-field-name cr-field-name :cr0))
1109 (let* ((+cond (position cr-name cr-bit-names))
1110 (-cond (position cr-name cr-bit-inverse-names))
1114 (error "Unknown branch condition ~s" cr-name))))
1115 (cr-form (list cr-field-name (if +cond cr-name (svref cr-bit-names -cond)))))
1116 (emit-conditional-branch segment b0 cr-form target))))
1118 (define-instruction sc (segment)
1119 (:printer sc ((op 17)))
1122 (:emitter (emit-sc-form-inst segment 17 2)))
1124 (define-instruction b (segment target)
1125 (:printer i ((op 18) (aa 0) (lk 0)))
1128 (emit-i-form-branch segment target nil)))
1130 (define-instruction ba (segment target)
1131 (:printer i-abs ((op 18) (aa 1) (lk 0)))
1134 (when (typep target 'fixup)
1135 (note-fixup segment :ba target)
1137 (emit-i-form-inst segment 18 (ash target -2) 1 0)))
1140 (define-instruction bl (segment target)
1141 (:printer i ((op 18) (aa 0) (lk 1)))
1144 (emit-i-form-branch segment target t)))
1146 (define-instruction bla (segment target)
1147 (:printer i-abs ((op 18) (aa 1) (lk 1)))
1150 (when (typep target 'fixup)
1151 (note-fixup segment :ba target)
1153 (emit-i-form-inst segment 18 (ash target -2) 1 1)))
1155 (define-instruction blr (segment)
1156 (:printer xl-bo-bi ((op 19) (xo 16) (bo #.(valid-bo-encoding :bo-u))(bi 0) (lk 0)) '(:name))
1158 (:dependencies (reads :ccr) (reads :ctr))
1160 (emit-x-form-inst segment 19 (valid-bo-encoding :bo-u) 0 0 16 0)))
1162 (define-instruction bclr (segment bo bi)
1163 (:printer xl-bo-bi ((op 19) (xo 16)))
1165 (:dependencies (reads :ccr) (reads :lr))
1167 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 16 0)))
1169 (define-instruction bclrl (segment bo bi)
1170 (:printer xl-bo-bi ((op 19) (xo 16) (lk 1)))
1172 (:dependencies (reads :ccr) (reads :lr))
1174 (emit-x-form-inst segment 19 (valid-bo-encoding bo)
1175 (valid-bi-encoding bi) 0 16 1)))
1177 (define-crbit-instruction crnor 19 33)
1178 (define-crbit-instruction crandc 19 129)
1179 (define-instruction isync (segment)
1180 (:printer xl-xo ((op 19) (xo 150)))
1183 (:emitter (emit-x-form-inst segment 19 0 0 0 150 0)))
1185 (define-crbit-instruction crxor 19 193)
1186 (define-crbit-instruction crnand 19 225)
1187 (define-crbit-instruction crand 19 257)
1188 (define-crbit-instruction creqv 19 289)
1189 (define-crbit-instruction crorc 19 417)
1190 (define-crbit-instruction cror 19 449)
1192 (define-instruction bcctr (segment bo bi)
1193 (:printer xl-bo-bi ((op 19) (xo 528)))
1195 (:dependencies (reads :ccr) (reads :ctr))
1197 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 0)))
1199 (define-instruction bcctrl (segment bo bi)
1200 (:printer xl-bo-bi ((op 19) (xo 528) (lk 1)))
1202 (:dependencies (reads :ccr) (reads :ctr) (writes :lr))
1204 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 1)))
1206 (define-instruction bctr (segment)
1207 (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 0)) '(:name))
1209 (:dependencies (reads :ccr) (reads :ctr))
1211 (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0 528 0)))
1213 (define-instruction bctrl (segment)
1214 (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 1)) '(:name))
1216 (:dependencies (reads :ccr) (reads :ctr))
1218 (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0 528 1)))
1220 (define-instruction rlwimi (segment ra rs sh mb me)
1221 (:printer m-sh ((op 20) (rc 0)))
1222 (:dependencies (reads rs) (writes ra))
1225 (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1227 (define-instruction rlwimi. (segment ra rs sh mb me)
1228 (:printer m-sh ((op 20) (rc 1)))
1229 (:dependencies (reads rs) (writes ra) (writes :ccr))
1232 (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1234 (define-instruction rlwinm (segment ra rs sh mb me)
1235 (:printer m-sh ((op 21) (rc 0)))
1237 (:dependencies (reads rs) (writes ra))
1239 (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1241 (define-instruction rlwinm. (segment ra rs sh mb me)
1242 (:printer m-sh ((op 21) (rc 1)))
1244 (:dependencies (reads rs) (writes ra) (writes :ccr))
1246 (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1248 (define-instruction rlwnm (segment ra rs rb mb me)
1249 (:printer m ((op 23) (rc 0) (rb nil :type 'reg)))
1251 (:dependencies (reads rs) (writes ra) (reads rb))
1253 (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 0)))
1255 (define-instruction rlwnm. (segment ra rs rb mb me)
1256 (:printer m ((op 23) (rc 1) (rb nil :type 'reg)))
1258 (:dependencies (reads rs) (reads rb) (writes ra) (writes :ccr))
1260 (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 1)))
1263 (define-d-rs-ui-instruction ori 24)
1265 (define-instruction nop (segment)
1266 (:printer d-rs-ui ((op 24) (rs 0) (ra 0) (ui 0)) '(:name))
1270 (emit-d-form-inst segment 24 0 0 0)))
1272 (define-d-rs-ui-instruction oris 25)
1273 (define-d-rs-ui-instruction xori 26)
1274 (define-d-rs-ui-instruction xoris 27)
1275 (define-d-rs-ui-instruction andi. 28 :other-dependencies ((writes :ccr)))
1276 (define-d-rs-ui-instruction andis. 29 :other-dependencies ((writes :ccr)))
1278 (define-instruction cmpw (segment crf ra &optional (rb nil rb-p))
1279 (:printer x-14 ((op 31) (xo 0) (l 0)) '(:name :tab bf "," ra "," rb))
1281 (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1284 (setq rb ra ra crf crf :cr0))
1285 (emit-x-form-inst segment
1287 (valid-cr-field-encoding crf)
1288 (reg-tn-encoding ra)
1289 (reg-tn-encoding rb)
1293 (define-instruction tw (segment tcond ra rb)
1294 (:printer x-19 ((op 31) (xo 4)))
1297 (:emitter (emit-x-form-inst segment 31 (valid-tcond-encoding tcond) (reg-tn-encoding ra) (reg-tn-encoding rb) 4 0)))
1299 (define-4-xo-instructions subfc 31 8 :always-writes-xer t)
1300 (define-4-xo-instructions addc 31 10 :always-writes-xer t)
1301 (define-2-xo-oe-instructions mulhwu 31 11 :cost 5)
1303 (define-instruction mfcr (segment rd)
1304 (:printer x-4 ((op 31) (xo 19)))
1306 (:dependencies (reads :ccr) (writes rd))
1307 (:emitter (emit-x-form-inst segment 31 (reg-tn-encoding rd) 0 0 19 0)))
1309 (define-x-instruction lwarx 31 20)
1310 (define-x-instruction lwzx 31 23)
1311 (define-2-x-5-instructions slw 31 24)
1312 (define-2-x-10-instructions cntlzw 31 26)
1313 (define-2-x-5-instructions and 31 28)
1315 (define-instruction cmplw (segment crf ra &optional (rb nil rb-p))
1316 (:printer x-14 ((op 31) (xo 32) (l 0)) '(:name :tab bf "," ra "," rb))
1318 (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1321 (setq rb ra ra crf crf :cr0))
1322 (emit-x-form-inst segment
1324 (valid-cr-field-encoding crf)
1325 (reg-tn-encoding ra)
1326 (reg-tn-encoding rb)
1331 (define-4-xo-instructions subf 31 40)
1333 (define-x-instruction lwzux 31 55 :other-dependencies ((writes rt)))
1334 (define-2-x-5-instructions andc 31 60)
1335 (define-2-xo-oe-instructions mulhw 31 75 :cost 5)
1337 (define-x-instruction lbzx 31 87)
1338 (define-4-xo-a-instructions neg 31 104)
1339 (define-x-instruction lbzux 31 119 :other-dependencies ((writes rt)))
1340 (define-2-x-5-instructions nor 31 124)
1341 (define-4-xo-instructions subfe 31 136 :always-reads-xer t :always-writes-xer t)
1343 (define-instruction-macro sube (rt ra rb)
1344 `(inst subfe ,rt ,rb ,ra))
1346 (define-instruction-macro sube. (rt ra rb)
1347 `(inst subfe. ,rt ,rb ,ra))
1349 (define-instruction-macro subeo (rt ra rb)
1350 `(inst subfeo ,rt ,rb ,ra))
1352 (define-instruction-macro subeo. (rt ra rb)
1353 `(inst subfeo ,rt ,rb ,ra))
1355 (define-4-xo-instructions adde 31 138 :always-reads-xer t :always-writes-xer t)
1357 (define-instruction mtcrf (segment mask rt)
1358 (:printer xfx-fxm ((op 31) (xo 144)))
1360 (:dependencies (reads rt) (writes :ccr))
1361 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash mask 1) 144 0)))
1363 (define-x-5-st-instruction stwcx. 31 150 t :other-dependencies ((writes :ccr)))
1364 (define-x-5-st-instruction stwx 31 151 nil)
1365 (define-x-5-st-instruction stwux 31 183 nil :other-dependencies ((writes ra)))
1366 (define-4-xo-a-instructions subfze 31 200 :always-reads-xer t :always-writes-xer t)
1367 (define-4-xo-a-instructions addze 31 202 :always-reads-xer t :always-writes-xer t)
1368 (define-x-5-st-instruction stbx 31 215 nil)
1369 (define-4-xo-a-instructions subfme 31 232 :always-reads-xer t :always-writes-xer t)
1370 (define-4-xo-a-instructions addme 31 234 :always-reads-xer t :always-writes-xer t)
1371 (define-4-xo-instructions mullw 31 235 :cost 5)
1372 (define-x-5-st-instruction stbux 31 247 nil :other-dependencies ((writes ra)))
1373 (define-4-xo-instructions add 31 266)
1374 (define-x-instruction lhzx 31 279)
1375 (define-2-x-5-instructions eqv 31 284)
1376 (define-x-instruction lhzux 31 311 :other-dependencies ((writes ra)))
1377 (define-2-x-5-instructions xor 31 316)
1379 (define-instruction mfmq (segment rt)
1380 (:printer xfx ((op 31) (xo 339) (spr 0)) '(:name :tab rt))
1382 (:dependencies (reads :xer) (writes rt))
1383 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 339 0)))
1385 (define-instruction mfxer (segment rt)
1386 (:printer xfx ((op 31) (xo 339) (spr 1)) '(:name :tab rt))
1388 (:dependencies (reads :xer) (writes rt))
1389 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 339 0)))
1391 (define-instruction mflr (segment rt)
1392 (:printer xfx ((op 31) (xo 339) (spr 8)) '(:name :tab rt))
1394 (:dependencies (reads :lr) (writes rt))
1395 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 339 0)))
1397 (define-instruction mfctr (segment rt)
1398 (:printer xfx ((op 31) (xo 339) (spr 9)) '(:name :tab rt))
1400 (:dependencies (reads rt) (reads :ctr))
1401 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 339 0)))
1404 (define-x-instruction lhax 31 343)
1405 (define-x-instruction lhaux 31 375 :other-dependencies ((writes ra)))
1406 (define-x-5-st-instruction sthx 31 407 nil)
1407 (define-2-x-5-instructions orc 31 412)
1408 (define-x-5-st-instruction sthux 31 439 nil :other-dependencies ((writes ra)))
1410 (define-instruction or (segment ra rs rb)
1411 (:printer x-5 ((op 31) (xo 444) (rc 0)) '((:cond
1412 ((rs :same-as rb) 'mr)
1416 (:unless (:same-as rs) "," rb)))
1419 (:dependencies (reads rb) (reads rs) (writes ra))
1421 (emit-x-form-inst segment
1423 (reg-tn-encoding rs)
1424 (reg-tn-encoding ra)
1425 (reg-tn-encoding rb)
1429 (define-instruction or. (segment ra rs rb)
1430 (:printer x-5 ((op 31) (xo 444) (rc 1)) '((:cond
1431 ((rs :same-as rb) 'mr.)
1435 (:unless (:same-as rs) "," rb)))
1438 (:dependencies (reads rb) (reads rs) (writes ra))
1440 (emit-x-form-inst segment
1442 (reg-tn-encoding rs)
1443 (reg-tn-encoding ra)
1444 (reg-tn-encoding rb)
1448 (define-instruction-macro mr (ra rs)
1449 `(inst or ,ra ,rs ,rs))
1451 (define-instruction-macro mr. (ra rs)
1452 `(inst or. ,ra ,rs ,rs))
1454 (define-4-xo-instructions divwu 31 459 :cost 36)
1456 ; This is a 601-specific instruction class.
1457 (define-4-xo-instructions div 31 331 :cost 36)
1459 ; This is a 601-specific instruction.
1460 (define-instruction mtmq (segment rt)
1461 (:printer xfx ((op 31) (xo 467) (spr (ash 0 5))) '(:name :tab rt))
1463 (:dependencies (reads rt) (writes :xer))
1464 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 467 0)))
1466 (define-instruction mtxer (segment rt)
1467 (:printer xfx ((op 31) (xo 467) (spr (ash 1 5))) '(:name :tab rt))
1469 (:dependencies (reads rt) (writes :xer))
1470 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 467 0)))
1472 (define-instruction mtlr (segment rt)
1473 (:printer xfx ((op 31) (xo 467) (spr (ash 8 5))) '(:name :tab rt))
1475 (:dependencies (reads rt) (writes :lr))
1476 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 467 0)))
1478 (define-instruction mtctr (segment rt)
1479 (:printer xfx ((op 31) (xo 467) (spr (ash 9 5))) '(:name :tab rt))
1481 (:dependencies (reads rt) (writes :ctr))
1482 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 467 0)))
1485 (define-2-x-5-instructions nand 31 476)
1486 (define-4-xo-instructions divw 31 491 :cost 36)
1487 (define-instruction mcrxr (segment crf)
1488 (:printer x-18 ((op 31) (xo 512)))
1490 (:dependencies (reads :xer) (writes :ccr) (writes :xer))
1491 (:emitter (emit-x-form-inst segment 31 (valid-cr-field-encoding crf) 0 0 512 0)))
1493 (define-instruction lswx (segment rs ra rb)
1494 (:printer x ((op 31) (xo 533) (rc 0)))
1498 (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) 533 0)))
1499 (define-x-instruction lwbrx 31 534)
1500 (define-x-20-instruction lfsx 31 535)
1501 (define-2-x-5-instructions srw 31 536)
1502 (define-x-20-instruction lfsux 31 567 :other-dependencies ((writes ra)))
1504 (define-instruction lswi (segment rt ra rb)
1505 (:printer x-1 ((op 31) (xo 597) (rc 0)))
1509 (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rt) (reg-tn-encoding ra) rb 597 0)))
1511 (define-instruction sync (segment)
1512 (:printer x-27 ((op 31) (xo 598)))
1515 (:emitter (emit-x-form-inst segment 31 0 0 0 598 0)))
1516 (define-x-20-instruction lfdx 31 599)
1517 (define-x-20-instruction lfdux 31 631 :other-dependencies ((writes ra)))
1518 (define-instruction stswx (segment rs ra rb)
1519 (:printer x-5 ((op 31) (xo 661)))
1523 (:emitter (emit-x-form-inst sb!assem:segment 31
1524 (reg-tn-encoding rs)
1525 (reg-tn-encoding ra)
1526 (reg-tn-encoding rb)
1529 (define-x-5-st-instruction stwbrx 31 662 nil)
1530 (define-x-23-st-instruction stfsx 31 663)
1531 (define-x-23-st-instruction stfsux 31 695 :other-dependencies ((writes ra)))
1532 (define-instruction stswi (segment rs ra nb)
1533 (:printer x-8 ((op 31) (xo 725)))
1537 (emit-x-form-inst segment 31
1538 (reg-tn-encoding rs)
1539 (reg-tn-encoding ra)
1544 (define-x-23-st-instruction stfdx 31 727)
1545 (define-x-23-st-instruction stfdux 31 759 :other-dependencies ((writes ra)))
1546 (define-x-instruction lhbrx 31 790)
1547 (define-2-x-5-instructions sraw 31 792)
1549 (define-instruction srawi (segment ra rs rb)
1550 (:printer x-9 ((op 31) (xo 824) (rc 0)))
1553 (:dependencies (reads rs) (writes ra))
1555 (emit-x-form-inst segment 31
1556 (reg-tn-encoding rs)
1557 (reg-tn-encoding ra)
1562 (define-instruction srawi. (segment ra rs rb)
1563 (:printer x-9 ((op 31) (xo 824) (rc 1)))
1566 (:dependencies (reads rs) (writes ra))
1568 (emit-x-form-inst segment 31
1569 (reg-tn-encoding rs)
1570 (reg-tn-encoding ra)
1575 (define-instruction eieio (segment)
1576 (:printer x-27 ((op 31) (xo 854)))
1579 (:emitter (emit-x-form-inst segment 31 0 0 0 854 0)))
1581 (define-x-5-st-instruction sthbrx 31 918 nil)
1583 (define-2-x-10-instructions extsb 31 954)
1584 (define-2-x-10-instructions extsh 31 922)
1587 (define-instruction lwz (segment rt ra si)
1588 (:declare (type (or fixup (signed-byte 16)) si))
1589 (:printer d ((op 32)))
1592 (:dependencies (reads ra) (writes rt))
1594 (when (typep si 'fixup)
1595 (note-fixup segment :l si)
1597 (emit-d-form-inst segment 32 (reg-tn-encoding rt) (reg-tn-encoding ra) si)))
1599 (define-d-instruction lwzu 33 :other-dependencies ((writes ra)))
1600 (define-d-instruction lbz 34)
1601 (define-d-instruction lbzu 35 :other-dependencies ((writes ra)))
1602 (define-d-rs-instruction stw 36)
1603 (define-d-rs-instruction stwu 37 :other-dependencies ((writes ra)))
1604 (define-d-rs-instruction stb 38)
1605 (define-d-rs-instruction stbu 39 :other-dependencies ((writes ra)))
1606 (define-d-instruction lhz 40)
1607 (define-d-instruction lhzu 41 :other-dependencies ((writes ra)))
1608 (define-d-instruction lha 42)
1609 (define-d-instruction lhau 43 :other-dependencies ((writes ra)))
1610 (define-d-rs-instruction sth 44)
1611 (define-d-rs-instruction sthu 45 :other-dependencies ((writes ra)))
1612 (define-d-instruction lmw 46 :pinned t)
1613 (define-d-rs-instruction stmw 47 :pinned t)
1614 (define-d-frt-instruction lfs 48)
1615 (define-d-frt-instruction lfsu 49 :other-dependencies ((writes ra)))
1616 (define-d-frt-instruction lfd 50)
1617 (define-d-frt-instruction lfdu 51 :other-dependencies ((writes ra)))
1618 (define-d-frs-instruction stfs 52)
1619 (define-d-frs-instruction stfsu 53 :other-dependencies ((writes ra)))
1620 (define-d-frs-instruction stfd 54)
1621 (define-d-frs-instruction stfdu 55 :other-dependencies ((writes ra)))
1623 (define-2-a-tab-instructions fdivs 59 18 :cost 17)
1624 (define-2-a-tab-instructions fsubs 59 20)
1625 (define-2-a-tab-instructions fadds 59 21)
1626 (define-2-a-tac-instructions fmuls 59 25)
1627 (define-2-a-instructions fmsubs 59 28 :cost 4)
1628 (define-2-a-instructions fmadds 59 29 :cost 4)
1629 (define-2-a-instructions fnmsubs 59 30 :cost 4)
1630 (define-2-a-instructions fnmadds 59 31 :cost 4)
1632 (define-instruction fcmpu (segment crfd fra frb)
1633 (:printer x-15 ((op 63) (xo 0)))
1634 (:dependencies (reads fra) (reads frb) (reads :fpscr)
1635 (writes :fpscr) (writes :ccr))
1638 (:emitter (emit-x-form-inst segment
1640 (valid-cr-field-encoding crfd)
1641 (fp-reg-tn-encoding fra)
1642 (fp-reg-tn-encoding frb)
1647 (define-2-x-21-instructions frsp 63 12)
1648 (define-2-x-21-instructions fctiw 63 14)
1649 (define-2-x-21-instructions fctiwz 63 15)
1651 (define-2-a-tab-instructions fdiv 63 18 :cost 31)
1652 (define-2-a-tab-instructions fsub 63 20)
1653 (define-2-a-tab-instructions fadd 63 21)
1654 (define-2-a-tac-instructions fmul 63 25 :cost 5)
1655 (define-2-a-instructions fmsub 63 28 :cost 5)
1656 (define-2-a-instructions fmadd 63 29 :cost 5)
1657 (define-2-a-instructions fnmsub 63 30 :cost 5)
1658 (define-2-a-instructions fnmadd 63 31 :cost 5)
1660 (define-instruction fcmpo (segment crfd fra frb)
1661 (:printer x-15 ((op 63) (xo 32)))
1662 (:dependencies (reads fra) (reads frb) (reads :fpscr)
1663 (writes :fpscr) (writes :ccr))
1666 (:emitter (emit-x-form-inst segment
1668 (valid-cr-field-encoding crfd)
1669 (fp-reg-tn-encoding fra)
1670 (fp-reg-tn-encoding frb)
1674 (define-2-x-21-instructions fneg 63 40)
1676 (define-2-x-21-instructions fmr 63 72)
1677 (define-2-x-21-instructions fnabs 63 136)
1678 (define-2-x-21-instructions fabs 63 264)
1680 (define-instruction mffs (segment frd)
1681 (:printer x-22 ((op 63) (xo 583) (rc 0)))
1683 (:dependencies (reads :fpscr) (writes frd))
1684 (:emitter (emit-x-form-inst segment
1686 (fp-reg-tn-encoding frd)
1692 (define-instruction mffs. (segment frd)
1693 (:printer x-22 ((op 63) (xo 583) (rc 1)))
1695 (:dependencies (reads :fpscr) (writes frd))
1696 (:emitter (emit-x-form-inst segment
1698 (fp-reg-tn-encoding frd)
1704 (define-instruction mtfsf (segment mask rb)
1705 (:printer xfl ((op 63) (xo 711) (rc 0)))
1706 (:dependencies (reads rb) (writes :fpscr))
1708 (:emitter (emit-xfl-form-inst segment 63 (ash mask 1) (fp-reg-tn-encoding rb) 711 0)))
1710 (define-instruction mtfsf. (segment mask rb)
1711 (:printer xfl ((op 63) (xo 711) (rc 1)))
1713 (:dependencies (reads rb) (writes :ccr) (writes :fpscr))
1714 (:emitter (emit-xfl-form-inst segment 63 (ash mask 1) (fp-reg-tn-encoding rb) 711 1)))
1719 ;;; Here in the future, macros are our friends.
1721 (define-instruction-macro subis (rt ra simm)
1722 `(inst addis ,rt ,ra (- ,simm)))
1724 (define-instruction-macro sub (rt rb ra)
1725 `(inst subf ,rt ,ra ,rb))
1726 (define-instruction-macro sub. (rt rb ra)
1727 `(inst subf. ,rt ,ra ,rb))
1728 (define-instruction-macro subo (rt rb ra)
1729 `(inst subfo ,rt ,ra ,rb))
1730 (define-instruction-macro subo. (rt rb ra)
1731 `(inst subfo. ,rt ,ra ,rb))
1734 (define-instruction-macro subic (rt ra simm)
1735 `(inst addic ,rt ,ra (- ,simm)))
1738 (define-instruction-macro subic. (rt ra simm)
1739 `(inst addic. ,rt ,ra (- ,simm)))
1743 (define-instruction-macro subc (rt rb ra)
1744 `(inst subfc ,rt ,ra ,rb))
1745 (define-instruction-macro subc. (rt rb ra)
1746 `(inst subfc. ,rt ,ra ,rb))
1747 (define-instruction-macro subco (rt rb ra)
1748 `(inst subfco ,rt ,ra ,rb))
1749 (define-instruction-macro subco. (rt rb ra)
1750 `(inst subfco. ,rt ,ra ,rb))
1752 (define-instruction-macro subi (rt ra simm)
1753 `(inst addi ,rt ,ra (- ,simm)))
1755 (define-instruction-macro li (rt val)
1756 `(inst addi ,rt zero-tn ,val))
1758 (define-instruction-macro lis (rt val)
1759 `(inst addis ,rt zero-tn ,val))
1762 (define-instruction-macro not (ra rs)
1763 `(inst nor ,ra ,rs ,rs))
1765 (define-instruction-macro not. (ra rs)
1766 `(inst nor. ,ra ,rs ,rs))
1769 (!def-vm-support-routine emit-nop (segment)
1770 (emit-word segment #x60000000))
1772 (define-instruction-macro extlwi (ra rs n b)
1773 `(inst rlwinm ,ra ,rs ,b 0 (1- ,n)))
1775 (define-instruction-macro extlwi. (ra rs n b)
1776 `(inst rlwinm. ,ra ,rs ,b 0 (1- ,n)))
1778 (define-instruction-macro srwi (ra rs n)
1779 `(inst rlwinm ,ra ,rs (- 32 ,n) ,n 31))
1781 (define-instruction-macro srwi. (ra rs n)
1782 `(inst rlwinm. ,ra ,rs (- 32 ,n) ,n 31))
1784 (define-instruction-macro clrrwi (ra rs n)
1785 `(inst rlwinm ,ra ,rs 0 0 (- 31 ,n)))
1787 (define-instruction-macro clrrwi. (ra rs n)
1788 `(inst rlwinm. ,ra ,rs 0 0 (- 31 ,n)))
1790 (define-instruction-macro inslw (ra rs n b)
1791 `(inst rlwimi ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1793 (define-instruction-macro inslw. (ra rs n b)
1794 `(inst rlwimi. ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1796 (define-instruction-macro rotlw (ra rs rb)
1797 `(inst rlwnm ,ra ,rs ,rb 0 31))
1799 (define-instruction-macro rotlw. (ra rs rb)
1800 `(inst rlwnm. ,ra ,rs ,rb 0 31))
1802 (define-instruction-macro slwi (ra rs n)
1803 `(inst rlwinm ,ra ,rs ,n 0 (- 31 ,n)))
1805 (define-instruction-macro slwi. (ra rs n)
1806 `(inst rlwinm. ,ra ,rs ,n 0 (- 31 ,n))))
1813 ((define-conditional-branches (name bo-name)
1814 (let* ((bo-enc (valid-bo-encoding bo-name)))
1816 (define-instruction-macro ,(symbolicate name "A") (bi target)
1817 ``(inst bca ,,,bo-enc ,,bi ,,target))
1818 (define-instruction-macro ,(symbolicate name "L") (bi target)
1819 ``(inst bcl ,,,bo-enc ,,bi ,,target))
1820 (define-instruction-macro ,(symbolicate name "LA") (bi target)
1821 ``(inst bcla ,,,bo-enc ,,bi ,,target))
1822 (define-instruction-macro ,(symbolicate name "CTR") (bi target)
1823 ``(inst bcctr ,,,bo-enc ,,bi ,,target))
1824 (define-instruction-macro ,(symbolicate name "CTRL") (bi target)
1825 ``(inst bcctrl ,,,bo-enc ,,bi ,,target))
1826 (define-instruction-macro ,(symbolicate name "LR") (bi target)
1827 ``(inst bclr ,,,bo-enc ,,bi ,,target))
1828 (define-instruction-macro ,(symbolicate name "LRL") (bi target)
1829 ``(inst bclrl ,,,bo-enc ,,bi ,,target))))))
1830 (define-conditional-branches bt :bo-t)
1831 (define-conditional-branches bf :bo-f))
1835 ((define-positive-conditional-branches (name cr-bit-name)
1837 (define-instruction-macro ,name (crf &optional (target nil target-p))
1839 (setq target crf crf :cr0))
1840 `(inst bt `(,,crf ,,,cr-bit-name) ,target))
1842 (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1843 ``(inst bta (,,cr-field ,,,cr-bit-name) ,,target))
1844 (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1845 ``(inst btl (,,cr-field ,,,cr-bit-name) ,,target))
1846 (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1847 ``(inst btla (,,cr-field ,,,cr-bit-name) ,,target))
1848 (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1849 ``(inst btctr (,,cr-field ,,,cr-bit-name) ,,target))
1850 (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1851 ``(inst btctrl (,,cr-field ,,,cr-bit-name) ,,target))
1852 (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1853 ``(inst btlr (,,cr-field ,,,cr-bit-name) ,,target))
1854 (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1855 ``(inst btlrl (,,cr-field ,,,cr-bit-name) ,,target))
1858 (define-positive-conditional-branches beq :eq)
1859 (define-positive-conditional-branches blt :lt)
1860 (define-positive-conditional-branches bgt :gt)
1861 (define-positive-conditional-branches bso :so)
1862 (define-positive-conditional-branches bun :so))
1866 ((define-negative-conditional-branches (name cr-bit-name)
1868 (define-instruction-macro ,name (crf &optional (target nil target-p))
1870 (setq target crf crf :cr0))
1871 `(inst bf `(,,crf ,,,cr-bit-name) ,target))
1873 (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1874 ``(inst bfa (,,cr-field ,,,cr-bit-name) ,,target))
1875 (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1876 ``(inst bfl (,,cr-field ,,,cr-bit-name) ,,target))
1877 (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1878 ``(inst bfla (,,cr-field ,,,cr-bit-name) ,,target))
1879 (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1880 ``(inst bfctr (,,cr-field ,,,cr-bit-name) ,,target))
1881 (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1882 ``(inst bfctrl (,,cr-field ,,,cr-bit-name) ,,target))
1883 (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1884 ``(inst bflr (,,cr-field ,,,cr-bit-name) ,,target))
1885 (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1886 ``(inst bflrl (,,cr-field ,,,cr-bit-name) ,,target))
1889 (define-negative-conditional-branches bne :eq)
1890 (define-negative-conditional-branches bnl :lt)
1891 (define-negative-conditional-branches bge :lt)
1892 (define-negative-conditional-branches bng :gt)
1893 (define-negative-conditional-branches ble :gt)
1894 (define-negative-conditional-branches bns :so)
1895 (define-negative-conditional-branches bnu :so))
1899 (define-instruction-macro j (func-tn offset)
1901 (inst addi lip-tn ,func-tn ,offset)
1907 (define-instruction-macro bua (target)
1908 `(inst bca :bo-u 0 ,target))
1910 (define-instruction-macro bul (target)
1911 `(inst bcl :bo-u 0 ,target))
1913 (define-instruction-macro bula (target)
1914 `(inst bcla :bo-u 0 ,target))
1917 (define-instruction-macro blrl ()
1918 `(inst bclrl :bo-u 0))
1928 ;;; Some more macros
1930 (defun %lr (reg value)
1933 (inst li reg value))
1935 (inst ori reg zero-tn value))
1936 ((or (signed-byte 32) (unsigned-byte 32))
1937 (let* ((high-half (ldb (byte 16 16) value))
1938 (low-half (ldb (byte 16 0) value)))
1939 (declare (type (unsigned-byte 16) high-half low-half))
1940 (cond ((if (logbitp 15 low-half) (= high-half #xffff) (zerop high-half))
1941 (inst li reg low-half))
1943 (inst lis reg high-half)
1944 (unless (zerop low-half)
1945 (inst ori reg reg low-half))))))
1947 (inst lis reg value)
1948 (inst addi reg reg value))))
1950 (define-instruction-macro lr (reg value)
1955 ;;;; Instructions for dumping data and header objects.
1957 (define-instruction word (segment word)
1958 (:declare (type (or (unsigned-byte 32) (signed-byte 32)) word))
1962 (emit-word segment word)))
1964 (define-instruction short (segment short)
1965 (:declare (type (or (unsigned-byte 16) (signed-byte 16)) short))
1969 (emit-short segment short)))
1971 (define-instruction byte (segment byte)
1972 (:declare (type (or (unsigned-byte 8) (signed-byte 8)) byte))
1976 (emit-byte segment byte)))
1978 (define-bitfield-emitter emit-header-object 32
1979 (byte 24 8) (byte 8 0))
1981 (defun emit-header-data (segment type)
1984 #'(lambda (segment posn)
1987 (ash (+ posn (component-header-length))
1988 (- n-widetag-bits word-shift)))))))
1990 (define-instruction simple-fun-header-word (segment)
1994 (emit-header-data segment simple-fun-header-widetag)))
1996 (define-instruction lra-header-word (segment)
2000 (emit-header-data segment return-pc-header-widetag)))
2003 ;;;; Instructions for converting between code objects, functions, and lras.
2004 (defun emit-compute-inst (segment vop dst src label temp calc)
2006 ;; We emit either 12 or 4 bytes, so we maintain 8 byte alignments.
2008 #'(lambda (segment posn delta-if-after)
2009 (let ((delta (funcall calc label posn delta-if-after)))
2010 (when (<= (- (ash 1 15)) delta (1- (ash 1 15)))
2011 (emit-back-patch segment 4
2012 #'(lambda (segment posn)
2013 (assemble (segment vop)
2015 (funcall calc label posn 0)))))
2017 #'(lambda (segment posn)
2018 (let ((delta (funcall calc label posn 0)))
2019 (assemble (segment vop)
2020 (inst lis temp (ldb (byte 16 16) delta))
2021 (inst ori temp temp (ldb (byte 16 0) delta))
2022 (inst add dst src temp))))))
2024 ;; this function is misnamed. should be compute-code-from-lip,
2025 ;; if the use in xep-allocate-frame is typical
2026 ;; (someone says code = fn - header - label-offset + other-pointer-tag)
2027 (define-instruction compute-code-from-fn (segment dst src label temp)
2028 (:declare (type tn dst src temp) (type label label))
2029 (:attributes variable-length)
2030 (:dependencies (reads src) (writes dst) (writes temp))
2034 (emit-compute-inst segment vop dst src label temp
2035 #'(lambda (label posn delta-if-after)
2036 (- other-pointer-lowtag
2037 ;;function-pointer-type
2038 (label-position label posn delta-if-after)
2039 (component-header-length))))))
2041 ;; code = lra - other-pointer-tag - header - label-offset + other-pointer-tag
2042 (define-instruction compute-code-from-lra (segment dst src label temp)
2043 (:declare (type tn dst src temp) (type label label))
2044 (:attributes variable-length)
2045 (:dependencies (reads src) (writes dst) (writes temp))
2049 (emit-compute-inst segment vop dst src label temp
2050 #'(lambda (label posn delta-if-after)
2051 (- (+ (label-position label posn delta-if-after)
2052 (component-header-length)))))))
2054 ;; lra = code + other-pointer-tag + header + label-offset - other-pointer-tag
2055 (define-instruction compute-lra-from-code (segment dst src label temp)
2056 (:declare (type tn dst src temp) (type label label))
2057 (:attributes variable-length)
2058 (:dependencies (reads src) (writes dst) (writes temp))
2062 (emit-compute-inst segment vop dst src label temp
2063 #'(lambda (label posn delta-if-after)
2064 (+ (label-position label posn delta-if-after)
2065 (component-header-length))))))