1 ;;;; the instruction set definition for the PPC
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 ;;; needs a little more work in the assembler, to realise that the
15 ;;; delays requested here are not mandatory, so that the assembler
16 ;;; shouldn't fill gaps with NOPs but with real instructions. -- CSR,
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20 (setf sb!assem:*assem-scheduler-p* t)
21 (setf sb!assem:*assem-max-locations* 70))
23 ;;;; Constants, types, conversion functions, some disassembler stuff.
25 (defun reg-tn-encoding (tn)
26 (declare (type tn tn))
31 (if (eq (sb-name (sc-sb (tn-sc tn))) 'registers)
33 (error "~S isn't a register." tn)))))
35 (defun fp-reg-tn-encoding (tn)
36 (declare (type tn tn))
37 (unless (eq (sb-name (sc-sb (tn-sc tn))) 'float-registers)
38 (error "~S isn't a floating-point register." tn))
41 ;(sb!disassem:set-disassem-params :instruction-alignment 32)
43 (defvar *disassem-use-lisp-reg-names* t)
45 (!def-vm-support-routine location-number (loc)
52 (ecase (sb-name (sc-sb (tn-sc loc)))
54 ;; Can happen if $ZERO or $NULL are passed in.
57 (unless (zerop (tn-offset loc))
60 (+ (tn-offset loc) 32))))
70 (defparameter reg-symbols
73 (cond ((null name) nil)
74 (t (make-symbol (concatenate 'string "$" name)))))
77 (defun maybe-add-notes (regno dstate)
78 (let* ((inst (sb!disassem::sap-ref-int
79 (sb!disassem::dstate-segment-sap dstate)
80 (sb!disassem::dstate-cur-offs dstate)
82 (sb!disassem::dstate-byte-order dstate)))
83 (op (ldb (byte 6 26) inst)))
87 (when (= regno (ldb (byte 5 16) inst)) ; only for the second
88 (case (ldb (byte 5 16) inst)
91 (sb!disassem:note-code-constant (ldb (byte 16 0) inst) dstate)))))
94 (when (= regno null-offset)
95 (sb!disassem:maybe-note-nil-indexed-object
96 (ldb (byte 16 0) inst) dstate))))))
98 (sb!disassem:define-arg-type reg
100 (lambda (value stream dstate)
101 (declare (type stream stream) (fixnum value))
102 (let ((regname (aref reg-symbols value)))
103 (princ regname stream)
104 (sb!disassem:maybe-note-associated-storage-ref
105 value 'registers regname dstate)
106 (maybe-add-notes value dstate))))
108 (defparameter float-reg-symbols
110 (loop for n from 0 to 31 collect (make-symbol (format nil "$F~d" n)))
113 (sb!disassem:define-arg-type fp-reg
114 :printer #'(lambda (value stream dstate)
115 (declare (type stream stream) (fixnum value))
116 (let ((regname (aref float-reg-symbols value)))
117 (princ regname stream)
118 (sb!disassem:maybe-note-associated-storage-ref
124 (eval-when (:compile-toplevel :load-toplevel :execute)
125 (defparameter bo-kind-names
126 #(:bo-dnzf :bo-dnzfp :bo-dzf :bo-dzfp :bo-f :bo-fp nil nil
127 :bo-dnzt :bo-dnztp :bo-dzt :bo-dztp :bo-t :bo-tp nil nil
128 :bo-dnz :bo-dnzp :bo-dz :bo-dzp :bo-u nil nil nil
129 nil nil nil nil nil nil nil nil)))
131 (sb!disassem:define-arg-type bo-field
132 :printer #'(lambda (value stream dstate)
133 (declare (ignore dstate)
136 (princ (svref bo-kind-names value) stream)))
138 (eval-when (:compile-toplevel :load-toplevel :execute)
139 (defun valid-bo-encoding (enc)
140 (or (if (integerp enc)
141 (and (= enc (logand #x1f enc))
142 (not (null (svref bo-kind-names enc)))
144 (and enc (position enc bo-kind-names)))
145 (error "Invalid BO field spec: ~s" enc)))
149 (defparameter cr-bit-names #(:lt :gt :eq :so))
150 (defparameter cr-bit-inverse-names #(:ge :le :ne :ns))
152 (defparameter cr-field-names #(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))
154 (defun valid-cr-bit-encoding (enc &optional error-p)
155 (or (if (integerp enc)
156 (and (= enc (logand 3 enc))
158 (position enc cr-bit-names)
159 (if error-p (error "Invalid condition bit specifier : ~s" enc))))
161 (defun valid-cr-field-encoding (enc)
162 (let* ((field (if (integerp enc)
163 (and (= enc (logand #x7 enc)))
164 (position enc cr-field-names))))
167 (error "Invalid condition register field specifier : ~s" enc))))
169 (defun valid-bi-encoding (enc)
173 (and (= enc (logand 31 enc)) enc)
174 (position enc cr-bit-names))
175 (+ (valid-cr-field-encoding (car enc))
176 (valid-cr-bit-encoding (cadr enc))))
177 (error "Invalid BI field spec : ~s" enc)))
179 (sb!disassem:define-arg-type bi-field
180 :printer #'(lambda (value stream dstate)
181 (declare (ignore dstate)
183 (type (unsigned-byte 5) value))
184 (let* ((bitname (svref cr-bit-names (logand 3 value)))
185 (crfield (ash value -2)))
186 (declare (type (unsigned-byte 3) crfield))
188 (princ bitname stream)
189 (princ (list (svref cr-field-names crfield) bitname) stream)))))
191 (sb!disassem:define-arg-type crf
192 :printer #'(lambda (value stream dstate)
193 (declare (ignore dstate)
195 (type (unsigned-byte 3) value))
196 (princ (svref cr-field-names value) stream)))
198 (sb!disassem:define-arg-type relative-label
200 :use-label #'(lambda (value dstate)
201 (declare (type (signed-byte 14) value)
202 (type sb!disassem:disassem-state dstate))
203 (+ (ash value 2) (sb!disassem:dstate-cur-addr dstate))))
205 (eval-when (:compile-toplevel :load-toplevel :execute)
206 (defparameter trap-values-alist '((:t . 31) (:lt . 16) (:le . 20) (:eq . 4) (:lng . 6)
207 (:ge .12) (:ne . 24) (:ng . 20) (:llt . 2) (:f . 0)
208 (:lle . 6) (:lge . 5) (:lgt . 1) (:lnl . 5))))
211 (defun valid-tcond-encoding (enc)
212 (or (and (if (integerp enc) (= (logand 31 enc) enc)) enc)
213 (cdr (assoc enc trap-values-alist))
214 (error "Unknown trap condition: ~s" enc)))
216 (sb!disassem:define-arg-type to-field
218 :printer #'(lambda (value stream dstate)
219 (declare (ignore dstate)
222 (princ (or (car (rassoc value trap-values-alist))
226 (defun snarf-error-junk (sap offset &optional length-only)
227 (let* ((length (sb!sys:sap-ref-8 sap offset))
228 (vector (make-array length :element-type '(unsigned-byte 8))))
229 (declare (type sb!sys:system-area-pointer sap)
230 (type (unsigned-byte 8) length)
231 (type (simple-array (unsigned-byte 8) (*)) vector))
233 (values 0 (1+ length) nil nil))
235 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
237 (collect ((sc-offsets)
239 (lengths 1) ; the length byte
241 (error-number (sb!c:read-var-integer vector index)))
244 (when (>= index length)
246 (let ((old-index index))
247 (sc-offsets (sb!c:read-var-integer vector index))
248 (lengths (- index old-index))))
254 (defun emit-conditional-branch (segment bo bi target &optional aa-p lk-p)
255 (declare (type boolean aa-p lk-p))
256 (let* ((bo (valid-bo-encoding bo))
257 (bi (valid-bi-encoding bi))
258 (aa-bit (if aa-p 1 0))
259 (lk-bit (if lk-p 1 0)))
260 (if aa-p ; Not bloody likely, bwth.
261 (emit-b-form-inst segment 16 bo bi target aa-bit lk-bit)
262 ;; the target may be >32k away, in which case we have to invert the
263 ;; test and do an absolute branch
265 ;; We emit either 4 or 8 bytes, so I think we declare this as
266 ;; preserving 4 byte alignment. If this gives us no joy, we can
267 ;; stick a nop in the long branch and then we will be
268 ;; preserving 8 byte alignment
269 segment 8 2 ; 2^2 is 4 byte alignment. I think
270 #'(lambda (segment posn magic-value)
271 (let ((delta (ash (- (label-position target posn magic-value) posn)
273 (when (typep delta '(signed-byte 14))
274 (emit-back-patch segment 4
275 #'(lambda (segment posn)
278 (ash (- (label-position target) posn) -2)
281 #'(lambda (segment posn)
282 (declare (ignore posn))
283 (let ((bo (logxor 8 bo))) ;; invert the test
284 (emit-b-form-inst segment 16 bo bi
285 2 ; skip over next instruction
287 (emit-back-patch segment 4
288 #'(lambda (segment posn)
289 (declare (ignore posn))
290 (emit-i-form-branch segment target lk-p)))))
295 ; non-absolute I-form: B, BL.
296 (defun emit-i-form-branch (segment target &optional lk-p)
297 (let* ((lk-bit (if lk-p 1 0)))
300 (note-fixup segment :b target)
301 (emit-i-form-inst segment 18 0 0 lk-bit))
303 (emit-back-patch segment 4
304 #'(lambda (segment posn)
308 (ash (- (label-position target) posn) -2)
312 (eval-when (:compile-toplevel :execute :load-toplevel)
313 (defparameter *spr-numbers-alist* '((:xer 1) (:lr 8) (:ctr 9))))
315 (sb!disassem:define-arg-type spr
316 :printer #'(lambda (value stream dstate)
317 (declare (ignore dstate)
318 (type (unsigned-byte 10) value))
319 (let* ((name (car (rassoc value *spr-numbers-alist*))))
322 (princ value stream)))))
324 (eval-when (:compile-toplevel :load-toplevel :execute)
325 (defparameter jump-printer
326 #'(lambda (value stream dstate)
327 (let ((addr (ash value 2)))
328 (sb!disassem:maybe-note-assembler-routine addr t dstate)
329 (write addr :base 16 :radix t :stream stream)))))
333 ;;;; dissassem:define-instruction-formats
335 (eval-when (:compile-toplevel :execute)
336 (defmacro ppc-byte (startbit &optional (endbit startbit))
337 (unless (and (typep startbit '(unsigned-byte 32))
338 (typep endbit '(unsigned-byte 32))
339 (>= endbit startbit))
341 ``(byte ,(1+ ,(- endbit startbit)) ,(- 31 ,endbit)))
343 (defparameter *ppc-field-specs-alist*
344 `((aa :field ,(ppc-byte 30))
345 (ba :field ,(ppc-byte 11 15) :type 'bi-field)
346 (bb :field ,(ppc-byte 16 20) :type 'bi-field)
347 (bd :field ,(ppc-byte 16 29) :type 'relative-label)
348 (bf :field ,(ppc-byte 6 8) :type 'crf)
349 (bfa :field ,(ppc-byte 11 13) :type 'crf)
350 (bi :field ,(ppc-byte 11 15) :type 'bi-field)
351 (bo :field ,(ppc-byte 6 10) :type 'bo-field)
352 (bt :field ,(ppc-byte 6 10) :type 'bi-field)
353 (d :field ,(ppc-byte 16 31) :sign-extend t)
354 (flm :field ,(ppc-byte 7 14) :sign-extend nil)
355 (fra :field ,(ppc-byte 11 15) :type 'fp-reg)
356 (frb :field ,(ppc-byte 16 20) :type 'fp-reg)
357 (frc :field ,(ppc-byte 21 25) :type 'fp-reg)
358 (frs :field ,(ppc-byte 6 10) :type 'fp-reg)
359 (frt :field ,(ppc-byte 6 10) :type 'fp-reg)
360 (fxm :field ,(ppc-byte 12 19) :sign-extend nil)
361 (l :field ,(ppc-byte 10) :sign-extend nil)
362 (li :field ,(ppc-byte 6 29) :sign-extend t :type 'relative-label)
363 (li-abs :field ,(ppc-byte 6 29) :sign-extend t :printer jump-printer)
364 (lk :field ,(ppc-byte 31))
365 (mb :field ,(ppc-byte 21 25) :sign-extend nil)
366 (me :field ,(ppc-byte 26 30) :sign-extend nil)
367 (nb :field ,(ppc-byte 16 20) :sign-extend nil)
368 (oe :field ,(ppc-byte 21))
369 (ra :field ,(ppc-byte 11 15) :type 'reg)
370 (rb :field ,(ppc-byte 16 20) :type 'reg)
371 (rc :field ,(ppc-byte 31))
372 (rs :field ,(ppc-byte 6 10) :type 'reg)
373 (rt :field ,(ppc-byte 6 10) :type 'reg)
374 (sh :field ,(ppc-byte 16 20) :sign-extend nil)
375 (si :field ,(ppc-byte 16 31) :sign-extend t)
376 (spr :field ,(ppc-byte 11 20) :type 'spr)
377 (to :field ,(ppc-byte 6 10) :type 'to-field)
378 (u :field ,(ppc-byte 16 19) :sign-extend nil)
379 (ui :field ,(ppc-byte 16 31) :sign-extend nil)
380 (xo21-30 :field ,(ppc-byte 21 30) :sign-extend nil)
381 (xo22-30 :field ,(ppc-byte 22 30) :sign-extend nil)
382 (xo26-30 :field ,(ppc-byte 26 30) :sign-extend nil)))
386 (sb!disassem:define-instruction-format (instr 32)
387 (op :field (byte 6 26))
388 (other :field (byte 26 0)))
390 (sb!disassem:define-instruction-format (xinstr 32 :default-printer '(:name :tab data))
391 (op-to-a :field (byte 16 16))
392 (data :field (byte 16 0)))
394 (sb!disassem:define-instruction-format (sc 32 :default-printer '(:name :tab rest))
395 (op :field (byte 6 26))
396 (rest :field (byte 26 0) :value 2))
400 (macrolet ((def-ppc-iformat ((name &optional default-printer) &rest specs)
401 (flet ((specname-field (specname)
402 (or (assoc specname *ppc-field-specs-alist*)
403 (error "Unknown ppc instruction field spec ~s" specname))))
404 (labels ((spec-field (spec)
406 (specname-field spec)
408 (cdr (specname-field (cadr spec)))))))
409 (collect ((field (list '(op :field (byte 6 26)))))
411 (field (spec-field spec)))
412 `(sb!disassem:define-instruction-format (,name 32 ,@(if default-printer `(:default-printer ,default-printer)))
415 (def-ppc-iformat (i '(:name :tab li))
418 (def-ppc-iformat (i-abs '(:name :tab li-abs))
421 (def-ppc-iformat (b '(:name :tab bo "," bi "," bd))
424 (def-ppc-iformat (d '(:name :tab rt "," d "(" ra ")"))
427 (def-ppc-iformat (d-si '(:name :tab rt "," ra "," si ))
430 (def-ppc-iformat (d-rs '(:name :tab rs "," d "(" ra ")"))
433 (def-ppc-iformat (d-rs-ui '(:name :tab ra "," rs "," ui))
436 (def-ppc-iformat (d-crf-si)
439 (def-ppc-iformat (d-crf-ui)
442 (def-ppc-iformat (d-to '(:name :tab to "," ra "," si))
445 (def-ppc-iformat (d-frt '(:name :tab frt "," d "(" ra ")"))
448 (def-ppc-iformat (d-frs '(:name :tab frs "," d "(" ra ")"))
453 ;;; There are around ... oh, 28 or so ... variants on the "X" format.
454 ;;; Some of them are only used by one instruction; some are used by dozens.
455 ;;; Some aren't used by instructions that we generate ...
457 (def-ppc-iformat (x '(:name :tab rt "," ra "," rb))
458 rt ra rb (xo xo21-30))
460 (def-ppc-iformat (x-1 '(:name :tab rt "," ra "," nb))
461 rt ra nb (xo xo21-30))
463 (def-ppc-iformat (x-4 '(:name :tab rt))
466 (def-ppc-iformat (x-5 '(:name :tab ra "," rs "," rb))
467 rs ra rb (xo xo21-30) rc)
469 (def-ppc-iformat (x-7 '(:name :tab ra "," rs "," rb))
470 rs ra rb (xo xo21-30))
472 (def-ppc-iformat (x-8 '(:name :tab ra "," rs "," nb))
473 rs ra nb (xo xo21-30))
475 (def-ppc-iformat (x-9 '(:name :tab ra "," rs "," sh))
476 rs ra sh (xo xo21-30) rc)
478 (def-ppc-iformat (x-10 '(:name :tab ra "," rs))
479 rs ra (xo xo21-30) rc)
481 (def-ppc-iformat (x-14 '(:name :tab bf "," l "," ra "," rb))
482 bf l ra rb (xo xo21-30))
484 (def-ppc-iformat (x-15 '(:name :tab bf "," l "," fra "," frb))
485 bf l fra frb (xo xo21-30))
487 (def-ppc-iformat (x-18 '(:name :tab bf))
490 (def-ppc-iformat (x-19 '(:name :tab to "," ra "," rb))
491 to ra rb (xo xo21-30))
493 (def-ppc-iformat (x-20 '(:name :tab frt "," ra "," rb))
494 frt ra rb (xo xo21-30))
496 (def-ppc-iformat (x-21 '(:name :tab frt "," rb))
497 frt rb (xo xo21-30) rc)
499 (def-ppc-iformat (x-22 '(:name :tab frt))
502 (def-ppc-iformat (x-23 '(:name :tab ra "," frs "," rb))
503 frs ra rb (xo xo21-30))
505 (def-ppc-iformat (x-24 '(:name :tab bt))
508 (def-ppc-iformat (x-25 '(:name :tab ra "," rb))
511 (def-ppc-iformat (x-26 '(:name :tab rb))
514 (def-ppc-iformat (x-27 '(:name))
520 (def-ppc-iformat (xl '(:name :tab bt "," ba "," bb))
521 bt ba bb (xo xo21-30))
523 (def-ppc-iformat (xl-bo-bi '(:name :tab bo "," bi))
524 bo bi (xo xo21-30) lk)
526 (def-ppc-iformat (xl-cr '(:name :tab bf "," bfa))
529 (def-ppc-iformat (xl-xo '(:name))
535 (def-ppc-iformat (xfx)
538 (def-ppc-iformat (xfx-fxm '(:name :tab fxm "," rs))
541 (def-ppc-iformat (xfl '(:name :tab flm "," frb))
542 flm frb (xo xo21-30) rc)
547 (def-ppc-iformat (xo '(:name :tab rt "," ra "," rb))
548 rt ra rb oe (xo xo22-30) rc)
550 (def-ppc-iformat (xo-oe '(:name :tab rt "," ra "," rb))
551 rt ra rb (xo xo22-30) rc)
553 (def-ppc-iformat (xo-a '(:name :tab rt "," ra))
554 rt ra oe (xo xo22-30) rc)
559 (def-ppc-iformat (a '(:name :tab frt "," fra "," frb "," frc))
560 frt fra frb frc (xo xo26-30) rc)
562 (def-ppc-iformat (a-tab '(:name :tab frt "," fra "," frb))
563 frt fra frb (xo xo26-30) rc)
565 (def-ppc-iformat (a-tac '(:name :tab frt "," fra "," frc))
566 frt fra frc (xo xo26-30) rc)
568 (def-ppc-iformat (a-tbc '(:name :tab frt "," frb "," frc))
569 frt frb frc (xo xo26-30) rc)
572 (def-ppc-iformat (m '(:name :tab ra "," rs "," rb "," mb "," me))
575 (def-ppc-iformat (m-sh '(:name :tab ra "," rs "," sh "," mb "," me))
581 ;;;; Primitive emitters.
584 (define-bitfield-emitter emit-word 32
587 (define-bitfield-emitter emit-short 16
590 (define-bitfield-emitter emit-i-form-inst 32
591 (byte 6 26) (byte 24 2) (byte 1 1) (byte 1 0))
593 (define-bitfield-emitter emit-b-form-inst 32
594 (byte 6 26) (byte 5 21) (byte 5 16) (byte 14 2) (byte 1 1) (byte 1 0))
596 (define-bitfield-emitter emit-sc-form-inst 32
597 (byte 6 26) (byte 26 0))
599 (define-bitfield-emitter emit-d-form-inst 32
600 (byte 6 26) (byte 5 21) (byte 5 16) (byte 16 0))
602 ; Also used for XL-form. What's the difference ?
603 (define-bitfield-emitter emit-x-form-inst 32
604 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 10 1) (byte 1 0))
606 (define-bitfield-emitter emit-xfx-form-inst 32
607 (byte 6 26) (byte 5 21) (byte 10 11) (byte 10 1) (byte 1 0))
609 (define-bitfield-emitter emit-xfl-form-inst 32
610 (byte 6 26) (byte 10 16) (byte 5 11) (byte 10 1) (byte 1 0))
613 (define-bitfield-emitter emit-xo-form-inst 32
614 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 1 10) (byte 9 1) (byte 1 0))
616 (define-bitfield-emitter emit-a-form-inst 32
617 (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 5 6) (byte 5 1) (byte 1 0))
622 (defun unimp-control (chunk inst stream dstate)
623 (declare (ignore inst))
624 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
625 (case (xinstr-data chunk dstate)
628 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
631 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
632 (#.object-not-list-trap
633 (nt "Object not list trap"))
635 (nt "Breakpoint trap"))
636 (#.pending-interrupt-trap
637 (nt "Pending interrupt trap"))
640 (#.fun-end-breakpoint-trap
641 (nt "Function end breakpoint trap"))
642 (#.object-not-instance-trap
643 (nt "Object not instance trap"))
646 (eval-when (:compile-toplevel :execute)
648 (defun classify-dependencies (deplist)
649 (collect ((reads) (writes))
650 (dolist (dep deplist)
653 (writes (writes dep))))
654 (values (reads) (writes)))))
656 (macrolet ((define-xo-instruction
657 (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
658 `(define-instruction ,name (segment rt ra rb)
659 (:printer xo ((op ,op ) (xo ,xo) (oe ,(if oe-p 1 0)) (rc ,(if rc-p 1 0))))
660 (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer)))
661 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if (or oe-p always-writes-xer) '((writes :xer))) )
665 (emit-xo-form-inst segment ,op
672 (define-xo-oe-instruction
673 (name op xo rc-p always-reads-xer always-writes-xer cost)
674 `(define-instruction ,name (segment rt ra rb)
675 (:printer xo-oe ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
676 (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer)))
677 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))))
681 (emit-xo-form-inst segment ,op
688 (define-4-xo-instructions
689 (base op xo &key always-reads-xer always-writes-xer (cost 1))
691 (define-xo-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
692 (define-xo-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
693 (define-xo-instruction ,(symbolicate base "O") ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
694 (define-xo-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
696 (define-2-xo-oe-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
698 (define-xo-oe-instruction ,base ,op ,xo nil ,always-reads-xer ,always-writes-xer ,cost)
699 (define-xo-oe-instruction ,(symbolicate base ".") ,op ,xo t ,always-reads-xer ,always-writes-xer ,cost)))
701 (define-xo-a-instruction (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
702 `(define-instruction ,name (segment rt ra)
703 (:printer xo-a ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0)) (oe ,(if oe-p 1 0))))
704 (:dependencies (reads ra) ,@(if always-reads-xer '((reads :xer)))
705 (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))) )
709 (emit-xo-form-inst segment ,op
717 (define-4-xo-a-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
719 (define-xo-a-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
720 (define-xo-a-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
721 (define-xo-a-instruction ,(symbolicate base "O") ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
722 (define-xo-a-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
724 (define-x-instruction (name op xo &key (cost 2) other-dependencies)
725 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
726 `(define-instruction ,name (segment rt ra rb)
727 (:printer x ((op ,op) (xo ,xo)))
730 (:dependencies (reads ra) (reads rb) (reads :memory) ,@other-reads
731 (writes rt) ,@other-writes)
733 (emit-x-form-inst segment ,op
740 (define-x-20-instruction (name op xo &key (cost 2) other-dependencies)
741 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
742 `(define-instruction ,name (segment frt ra rb)
743 (:printer x-20 ((op ,op) (xo ,xo)))
746 (:dependencies (reads ra) (reads rb) ,@other-reads
747 (writes frt) ,@other-writes)
749 (emit-x-form-inst segment ,op
750 (fp-reg-tn-encoding frt)
756 (define-x-5-instruction (name op xo rc-p &key (cost 1) other-dependencies)
757 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
758 `(define-instruction ,name (segment ra rs rb)
759 (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
762 (:dependencies (reads rb) (reads rs) ,@other-reads
763 (writes ra) ,@other-writes)
765 (emit-x-form-inst segment ,op
773 (define-x-5-st-instruction (name op xo rc-p &key (cost 1) other-dependencies)
774 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
775 `(define-instruction ,name (segment rs ra rb)
776 (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
779 (:dependencies (reads ra) (reads rb) (reads rs) ,@other-reads
780 (writes :memory :partially t) ,@other-writes)
782 (emit-x-form-inst segment ,op
789 (define-x-23-st-instruction (name op xo &key (cost 1) other-dependencies)
790 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
791 `(define-instruction ,name (segment frs ra rb)
792 (:printer x-23 ((op ,op) (xo ,xo)))
795 (:dependencies (reads ra) (reads rb) (reads frs) ,@other-reads
796 (writes :memory :partially t) ,@other-writes)
798 (emit-x-form-inst segment ,op
799 (fp-reg-tn-encoding frs)
805 (define-x-10-instruction (name op xo rc-p &key (cost 1) other-dependencies)
806 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
807 `(define-instruction ,name (segment ra rs)
808 (:printer x-10 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
811 (:dependencies (reads rs) ,@other-reads
812 (writes ra) ,@other-writes)
814 (emit-x-form-inst segment ,op
821 (define-2-x-5-instructions (name op xo &key (cost 1) other-dependencies)
823 (define-x-5-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
824 (define-x-5-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
825 :other-dependencies ,other-dependencies)))
827 (define-2-x-10-instructions (name op xo &key (cost 1) other-dependencies)
829 (define-x-10-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
830 (define-x-10-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
831 :other-dependencies ,other-dependencies)))
834 (define-x-21-instruction (name op xo rc-p &key (cost 4) other-dependencies)
835 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
836 `(define-instruction ,name (segment frt frb)
837 (:printer x-21 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
840 (:dependencies (reads frb) ,@other-reads
841 (writes frt) ,@other-writes)
843 (emit-x-form-inst segment ,op
844 (fp-reg-tn-encoding frt)
846 (fp-reg-tn-encoding frb)
850 (define-2-x-21-instructions (name op xo &key (cost 4) other-dependencies)
852 (define-x-21-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
853 (define-x-21-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost
854 :other-dependencies ,other-dependencies)))
857 (define-d-si-instruction (name op &key (fixup nil) (cost 1) other-dependencies)
858 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
859 `(define-instruction ,name (segment rt ra si)
860 (:declare (type (or ,@(when fixup '(fixup))
861 (unsigned-byte 16) (signed-byte 16))
863 (:printer d-si ((op ,op)))
866 (:dependencies (reads ra) ,@other-reads
867 (writes rt) ,@other-writes)
869 (when (typep si 'fixup)
871 ((:ha :l) (note-fixup segment ,fixup si)))
873 (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
875 (define-d-rs-ui-instruction (name op &key (cost 1) other-dependencies)
876 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
877 `(define-instruction ,name (segment ra rs ui)
878 (:declare (type (unsigned-byte 16) ui))
879 (:printer d-rs-ui ((op ,op)))
882 (:dependencies (reads rs) ,@other-reads
883 (writes ra) ,@other-writes)
885 (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) ui)))))
887 (define-d-instruction (name op &key (cost 2) other-dependencies pinned)
888 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
889 `(define-instruction ,name (segment rt ra si)
890 (:declare (type (signed-byte 16) si))
891 (:printer d ((op ,op)))
894 ,@(when pinned '(:pinned))
895 (:dependencies (reads ra) (reads :memory) ,@other-reads
896 (writes rt) ,@other-writes)
898 (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
900 (define-d-frt-instruction (name op &key (cost 3) other-dependencies)
901 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
902 `(define-instruction ,name (segment frt ra si)
903 (:declare (type (signed-byte 16) si))
904 (:printer d-frt ((op ,op)))
907 (:dependencies (reads ra) (reads :memory) ,@other-reads
908 (writes frt) ,@other-writes)
910 (emit-d-form-inst segment ,op (fp-reg-tn-encoding frt) (reg-tn-encoding ra) si)))))
912 (define-d-rs-instruction (name op &key (cost 1) other-dependencies pinned)
913 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
914 `(define-instruction ,name (segment rs ra si)
915 (:declare (type (signed-byte 16) si))
916 (:printer d-rs ((op ,op)))
919 ,@(when pinned '(:pinned))
920 (:dependencies (reads rs) (reads ra) ,@other-reads
921 (writes :memory :partially t) ,@other-writes)
923 (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) si)))))
925 (define-d-frs-instruction (name op &key (cost 1) other-dependencies)
926 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
927 `(define-instruction ,name (segment frs ra si)
928 (:declare (type (signed-byte 16) si))
929 (:printer d-frs ((op ,op)))
932 (:dependencies (reads frs) (reads ra) ,@other-reads
933 (writes :memory :partially t) ,@other-writes)
935 (emit-d-form-inst segment ,op (fp-reg-tn-encoding frs) (reg-tn-encoding ra) si)))))
937 (define-a-instruction (name op xo rc &key (cost 1) other-dependencies)
938 `(define-instruction ,name (segment frt fra frb frc)
939 (:printer a ((op ,op) (xo ,xo) (rc ,rc)))
942 (:dependencies (writes frt) (reads fra) (reads frb) (reads frc) ,@other-dependencies)
944 (emit-a-form-inst segment
946 (fp-reg-tn-encoding frt)
947 (fp-reg-tn-encoding fra)
948 (fp-reg-tn-encoding frb)
949 (fp-reg-tn-encoding frb)
953 (define-2-a-instructions (name op xo &key (cost 1) other-dependencies)
955 (define-a-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
956 (define-a-instruction ,(symbolicate name ".")
957 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
959 (define-a-tab-instruction (name op xo rc &key (cost 1) other-dependencies)
960 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
961 `(define-instruction ,name (segment frt fra frb)
962 (:printer a-tab ((op ,op) (xo ,xo) (rc ,rc)))
965 (:dependencies (reads fra) (reads frb) ,@other-reads
966 (writes frt) ,@other-writes)
968 (emit-a-form-inst segment
970 (fp-reg-tn-encoding frt)
971 (fp-reg-tn-encoding fra)
972 (fp-reg-tn-encoding frb)
977 (define-2-a-tab-instructions (name op xo &key (cost 1) other-dependencies)
979 (define-a-tab-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
980 (define-a-tab-instruction ,(symbolicate name ".")
981 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
983 (define-a-tac-instruction (name op xo rc &key (cost 1) other-dependencies)
984 (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
985 `(define-instruction ,name (segment frt fra frb)
986 (:printer a-tac ((op ,op) (xo ,xo) (rc ,rc)))
989 (:dependencies (reads fra) (reads frb) ,@other-reads
990 (writes frt) ,@other-writes)
992 (emit-a-form-inst segment
994 (fp-reg-tn-encoding frt)
995 (fp-reg-tn-encoding fra)
997 (fp-reg-tn-encoding frb)
1001 (define-2-a-tac-instructions (name op xo &key (cost 1) other-dependencies)
1003 (define-a-tac-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
1004 (define-a-tac-instruction ,(symbolicate name ".")
1005 ,op ,xo 1 :cost ,cost :other-dependencies ,other-dependencies)))
1007 (define-crbit-instruction (name op xo)
1008 `(define-instruction ,name (segment dbit abit bbit)
1009 (:printer xl ((op ,op ) (xo ,xo)))
1012 (:dependencies (reads :ccr) (writes :ccr))
1013 (:emitter (emit-x-form-inst segment 19
1014 (valid-bi-encoding dbit)
1015 (valid-bi-encoding abit)
1016 (valid-bi-encoding bbit)
1020 ;;; The instructions, in numerical order
1022 (define-instruction unimp (segment data)
1023 (:declare (type (signed-byte 16) data))
1024 (:printer xinstr ((op-to-a #.(logior (ash 3 10) (ash 6 5) 0)))
1025 :default :control #'unimp-control)
1028 (:emitter (emit-d-form-inst segment 3 6 0 data)))
1030 (define-instruction twi (segment tcond ra si)
1031 (:printer d-to ((op 3)))
1034 (:emitter (emit-d-form-inst segment 3 (valid-tcond-encoding tcond) (reg-tn-encoding ra) si)))
1036 (define-d-si-instruction mulli 7 :cost 5)
1037 (define-d-si-instruction subfic 8)
1039 (define-instruction cmplwi (segment crf ra &optional (ui nil ui-p))
1040 (:printer d-crf-ui ((op 10) (l 0)) '(:name :tab bf "," ra "," ui))
1041 (:dependencies (if ui-p (reads ra) (reads crf)) (writes :ccr))
1045 (setq ui ra ra crf crf :cr0))
1046 (emit-d-form-inst segment
1048 (valid-cr-field-encoding crf)
1049 (reg-tn-encoding ra)
1052 (define-instruction cmpwi (segment crf ra &optional (si nil si-p))
1053 (:printer d-crf-si ((op 11) (l 0)) '(:name :tab bf "," ra "," si))
1054 (:dependencies (if si-p (reads ra) (reads crf)) (writes :ccr))
1058 (setq si ra ra crf crf :cr0))
1059 (emit-d-form-inst segment
1061 (valid-cr-field-encoding crf)
1062 (reg-tn-encoding ra)
1065 (define-d-si-instruction addic 12 :other-dependencies ((writes :xer)))
1066 (define-d-si-instruction addic. 13 :other-dependencies ((writes :xer) (writes :ccr)))
1068 (define-d-si-instruction addi 14 :fixup :l)
1069 (define-d-si-instruction addis 15 :fixup :ha)
1071 ;; There's no real support here for branch options that decrement
1072 ;; and test the CTR :
1073 ;; (a) the instruction scheduler doesn't know that anything's happening
1075 ;; (b) Lisp may have to assume that the CTR always has a lisp
1076 ;; object/locative in it.
1078 (define-instruction bc (segment bo bi target)
1079 (:declare (type label target))
1080 (:printer b ((op 16) (aa 0) (lk 0)))
1081 (:attributes branch)
1083 (:dependencies (reads :ccr))
1085 (emit-conditional-branch segment bo bi target)))
1087 (define-instruction bcl (segment bo bi target)
1088 (:declare (type label target))
1089 (:printer b ((op 16) (aa 0) (lk 1)))
1090 (:attributes branch)
1092 (:dependencies (reads :ccr))
1094 (emit-conditional-branch segment bo bi target nil t)))
1096 (define-instruction bca (segment bo bi target)
1097 (:declare (type label target))
1098 (:printer b ((op 16) (aa 1) (lk 0)))
1099 (:attributes branch)
1101 (:dependencies (reads :ccr))
1103 (emit-conditional-branch segment bo bi target t)))
1105 (define-instruction bcla (segment bo bi target)
1106 (:declare (type label target))
1107 (:printer b ((op 16) (aa 1) (lk 1)))
1108 (:attributes branch)
1110 (:dependencies (reads :ccr))
1112 (emit-conditional-branch segment bo bi target t t)))
1114 ;;; There may (or may not) be a good reason to use this in preference
1115 ;;; to "b[la] target". I can't think of a -bad- reason ...
1117 (define-instruction bu (segment target)
1118 (:declare (type label target))
1119 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (aa 0) (lk 0))
1121 (:attributes branch)
1124 (emit-conditional-branch segment #.(valid-bo-encoding :bo-u) 0 target nil nil)))
1127 (define-instruction bt (segment bi target)
1128 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-t)) (aa 0) (lk 0))
1129 '(:name :tab bi "," bd))
1130 (:attributes branch)
1133 (emit-conditional-branch segment #.(valid-bo-encoding :bo-t) bi target nil nil)))
1135 (define-instruction bf (segment bi target)
1136 (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-f)) (aa 0) (lk 0))
1137 '(:name :tab bi "," bd))
1138 (:attributes branch)
1141 (emit-conditional-branch segment #.(valid-bo-encoding :bo-f) bi target nil nil)))
1143 (define-instruction b? (segment cr-field-name cr-name &optional (target nil target-p))
1144 (:attributes branch)
1148 (setq target cr-name cr-name cr-field-name cr-field-name :cr0))
1149 (let* ((+cond (position cr-name cr-bit-names))
1150 (-cond (position cr-name cr-bit-inverse-names))
1154 (error "Unknown branch condition ~s" cr-name))))
1155 (cr-form (list cr-field-name (if +cond cr-name (svref cr-bit-names -cond)))))
1156 (emit-conditional-branch segment b0 cr-form target))))
1158 (define-instruction sc (segment)
1159 (:printer sc ((op 17)))
1160 (:attributes branch)
1163 (:emitter (emit-sc-form-inst segment 17 2)))
1165 (define-instruction b (segment target)
1166 (:printer i ((op 18) (aa 0) (lk 0)))
1167 (:attributes branch)
1170 (emit-i-form-branch segment target nil)))
1172 (define-instruction ba (segment target)
1173 (:printer i-abs ((op 18) (aa 1) (lk 0)))
1174 (:attributes branch)
1177 (when (typep target 'fixup)
1178 (note-fixup segment :ba target)
1180 (emit-i-form-inst segment 18 (ash target -2) 1 0)))
1183 (define-instruction bl (segment target)
1184 (:printer i ((op 18) (aa 0) (lk 1)))
1185 (:attributes branch)
1188 (emit-i-form-branch segment target t)))
1190 (define-instruction bla (segment target)
1191 (:printer i-abs ((op 18) (aa 1) (lk 1)))
1192 (:attributes branch)
1195 (when (typep target 'fixup)
1196 (note-fixup segment :ba target)
1198 (emit-i-form-inst segment 18 (ash target -2) 1 1)))
1200 (define-instruction blr (segment)
1201 (:printer xl-bo-bi ((op 19) (xo 16) (bo #.(valid-bo-encoding :bo-u))(bi 0) (lk 0)) '(:name))
1202 (:attributes branch)
1204 (:dependencies (reads :ccr) (reads :ctr))
1206 (emit-x-form-inst segment 19 (valid-bo-encoding :bo-u) 0 0 16 0)))
1208 (define-instruction bclr (segment bo bi)
1209 (:printer xl-bo-bi ((op 19) (xo 16)))
1210 (:attributes branch)
1212 (:dependencies (reads :ccr) (reads :lr))
1214 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 16 0)))
1216 (define-instruction bclrl (segment bo bi)
1217 (:printer xl-bo-bi ((op 19) (xo 16) (lk 1)))
1218 (:attributes branch)
1220 (:dependencies (reads :ccr) (reads :lr))
1222 (emit-x-form-inst segment 19 (valid-bo-encoding bo)
1223 (valid-bi-encoding bi) 0 16 1)))
1225 (define-crbit-instruction crnor 19 33)
1226 (define-crbit-instruction crandc 19 129)
1227 (define-instruction isync (segment)
1228 (:printer xl-xo ((op 19) (xo 150)))
1231 (:emitter (emit-x-form-inst segment 19 0 0 0 150 0)))
1233 (define-crbit-instruction crxor 19 193)
1234 (define-crbit-instruction crnand 19 225)
1235 (define-crbit-instruction crand 19 257)
1236 (define-crbit-instruction creqv 19 289)
1237 (define-crbit-instruction crorc 19 417)
1238 (define-crbit-instruction cror 19 449)
1240 (define-instruction bcctr (segment bo bi)
1241 (:printer xl-bo-bi ((op 19) (xo 528)))
1242 (:attributes branch)
1244 (:dependencies (reads :ccr) (reads :ctr))
1246 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 0)))
1248 (define-instruction bcctrl (segment bo bi)
1249 (:printer xl-bo-bi ((op 19) (xo 528) (lk 1)))
1250 (:attributes branch)
1252 (:dependencies (reads :ccr) (reads :ctr) (writes :lr))
1254 (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 1)))
1256 (define-instruction bctr (segment)
1257 (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 0)) '(:name))
1258 (:attributes branch)
1260 (:dependencies (reads :ccr) (reads :ctr))
1262 (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0 528 0)))
1264 (define-instruction bctrl (segment)
1265 (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 1)) '(:name))
1266 (:attributes branch)
1268 (:dependencies (reads :ccr) (reads :ctr))
1270 (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0 528 1)))
1272 (define-instruction rlwimi (segment ra rs sh mb me)
1273 (:printer m-sh ((op 20) (rc 0)))
1274 (:dependencies (reads rs) (writes ra))
1277 (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1279 (define-instruction rlwimi. (segment ra rs sh mb me)
1280 (:printer m-sh ((op 20) (rc 1)))
1281 (:dependencies (reads rs) (writes ra) (writes :ccr))
1284 (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1286 (define-instruction rlwinm (segment ra rs sh mb me)
1287 (:printer m-sh ((op 21) (rc 0)))
1289 (:dependencies (reads rs) (writes ra))
1291 (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1293 (define-instruction rlwinm. (segment ra rs sh mb me)
1294 (:printer m-sh ((op 21) (rc 1)))
1296 (:dependencies (reads rs) (writes ra) (writes :ccr))
1298 (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1300 (define-instruction rlwnm (segment ra rs rb mb me)
1301 (:printer m ((op 23) (rc 0) (rb nil :type 'reg)))
1303 (:dependencies (reads rs) (writes ra) (reads rb))
1305 (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 0)))
1307 (define-instruction rlwnm. (segment ra rs rb mb me)
1308 (:printer m ((op 23) (rc 1) (rb nil :type 'reg)))
1310 (:dependencies (reads rs) (reads rb) (writes ra) (writes :ccr))
1312 (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 1)))
1315 (define-d-rs-ui-instruction ori 24)
1317 (define-instruction nop (segment)
1318 (:printer d-rs-ui ((op 24) (rs 0) (ra 0) (ui 0)) '(:name))
1322 (emit-d-form-inst segment 24 0 0 0)))
1324 (define-d-rs-ui-instruction oris 25)
1325 (define-d-rs-ui-instruction xori 26)
1326 (define-d-rs-ui-instruction xoris 27)
1327 (define-d-rs-ui-instruction andi. 28 :other-dependencies ((writes :ccr)))
1328 (define-d-rs-ui-instruction andis. 29 :other-dependencies ((writes :ccr)))
1330 (define-instruction cmpw (segment crf ra &optional (rb nil rb-p))
1331 (:printer x-14 ((op 31) (xo 0) (l 0)) '(:name :tab bf "," ra "," rb))
1333 (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1336 (setq rb ra ra crf crf :cr0))
1337 (emit-x-form-inst segment
1339 (valid-cr-field-encoding crf)
1340 (reg-tn-encoding ra)
1341 (reg-tn-encoding rb)
1345 (define-instruction tw (segment tcond ra rb)
1346 (:printer x-19 ((op 31) (xo 4)))
1347 (:attributes branch)
1350 (:emitter (emit-x-form-inst segment 31 (valid-tcond-encoding tcond) (reg-tn-encoding ra) (reg-tn-encoding rb) 4 0)))
1352 (define-4-xo-instructions subfc 31 8 :always-writes-xer t)
1353 (define-4-xo-instructions addc 31 10 :always-writes-xer t)
1354 (define-2-xo-oe-instructions mulhwu 31 11 :cost 5)
1356 (define-instruction mfcr (segment rd)
1357 (:printer x-4 ((op 31) (xo 19)))
1359 (:dependencies (reads :ccr) (writes rd))
1360 (:emitter (emit-x-form-inst segment 31 (reg-tn-encoding rd) 0 0 19 0)))
1362 (define-x-instruction lwarx 31 20)
1363 (define-x-instruction lwzx 31 23)
1364 (define-2-x-5-instructions slw 31 24)
1365 (define-2-x-10-instructions cntlzw 31 26)
1366 (define-2-x-5-instructions and 31 28)
1368 (define-instruction cmplw (segment crf ra &optional (rb nil rb-p))
1369 (:printer x-14 ((op 31) (xo 32) (l 0)) '(:name :tab bf "," ra "," rb))
1371 (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1374 (setq rb ra ra crf crf :cr0))
1375 (emit-x-form-inst segment
1377 (valid-cr-field-encoding crf)
1378 (reg-tn-encoding ra)
1379 (reg-tn-encoding rb)
1384 (define-4-xo-instructions subf 31 40)
1386 (define-x-instruction lwzux 31 55 :other-dependencies ((writes rt)))
1387 (define-2-x-5-instructions andc 31 60)
1388 (define-2-xo-oe-instructions mulhw 31 75 :cost 5)
1390 (define-x-instruction lbzx 31 87)
1391 (define-4-xo-a-instructions neg 31 104)
1392 (define-x-instruction lbzux 31 119 :other-dependencies ((writes rt)))
1393 (define-2-x-5-instructions nor 31 124)
1394 (define-4-xo-instructions subfe 31 136 :always-reads-xer t :always-writes-xer t)
1396 (define-instruction-macro sube (rt ra rb)
1397 `(inst subfe ,rt ,rb ,ra))
1399 (define-instruction-macro sube. (rt ra rb)
1400 `(inst subfe. ,rt ,rb ,ra))
1402 (define-instruction-macro subeo (rt ra rb)
1403 `(inst subfeo ,rt ,rb ,ra))
1405 (define-instruction-macro subeo. (rt ra rb)
1406 `(inst subfeo ,rt ,rb ,ra))
1408 (define-4-xo-instructions adde 31 138 :always-reads-xer t :always-writes-xer t)
1410 (define-instruction mtcrf (segment mask rt)
1411 (:printer xfx-fxm ((op 31) (xo 144)))
1413 (:dependencies (reads rt) (writes :ccr))
1414 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash mask 1) 144 0)))
1416 (define-x-5-st-instruction stwcx. 31 150 t :other-dependencies ((writes :ccr)))
1417 (define-x-5-st-instruction stwx 31 151 nil)
1418 (define-x-5-st-instruction stwux 31 183 nil :other-dependencies ((writes ra)))
1419 (define-4-xo-a-instructions subfze 31 200 :always-reads-xer t :always-writes-xer t)
1420 (define-4-xo-a-instructions addze 31 202 :always-reads-xer t :always-writes-xer t)
1421 (define-x-5-st-instruction stbx 31 215 nil)
1422 (define-4-xo-a-instructions subfme 31 232 :always-reads-xer t :always-writes-xer t)
1423 (define-4-xo-a-instructions addme 31 234 :always-reads-xer t :always-writes-xer t)
1424 (define-4-xo-instructions mullw 31 235 :cost 5)
1425 (define-x-5-st-instruction stbux 31 247 nil :other-dependencies ((writes ra)))
1426 (define-4-xo-instructions add 31 266)
1427 (define-x-instruction lhzx 31 279)
1428 (define-2-x-5-instructions eqv 31 284)
1429 (define-x-instruction lhzux 31 311 :other-dependencies ((writes ra)))
1430 (define-2-x-5-instructions xor 31 316)
1432 (define-instruction mfmq (segment rt)
1433 (:printer xfx ((op 31) (xo 339) (spr 0)) '(:name :tab rt))
1435 (:dependencies (reads :xer) (writes rt))
1436 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 339 0)))
1438 (define-instruction mfxer (segment rt)
1439 (:printer xfx ((op 31) (xo 339) (spr 1)) '(:name :tab rt))
1441 (:dependencies (reads :xer) (writes rt))
1442 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 339 0)))
1444 (define-instruction mflr (segment rt)
1445 (:printer xfx ((op 31) (xo 339) (spr 8)) '(:name :tab rt))
1447 (:dependencies (reads :lr) (writes rt))
1448 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 339 0)))
1450 (define-instruction mfctr (segment rt)
1451 (:printer xfx ((op 31) (xo 339) (spr 9)) '(:name :tab rt))
1453 (:dependencies (reads rt) (reads :ctr))
1454 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 339 0)))
1457 (define-x-instruction lhax 31 343)
1458 (define-x-instruction lhaux 31 375 :other-dependencies ((writes ra)))
1459 (define-x-5-st-instruction sthx 31 407 nil)
1460 (define-2-x-5-instructions orc 31 412)
1461 (define-x-5-st-instruction sthux 31 439 nil :other-dependencies ((writes ra)))
1463 (define-instruction or (segment ra rs rb)
1464 (:printer x-5 ((op 31) (xo 444) (rc 0)) '((:cond
1465 ((rs :same-as rb) 'mr)
1469 (:unless (:same-as rs) "," rb)))
1472 (:dependencies (reads rb) (reads rs) (writes ra))
1474 (emit-x-form-inst segment
1476 (reg-tn-encoding rs)
1477 (reg-tn-encoding ra)
1478 (reg-tn-encoding rb)
1482 (define-instruction or. (segment ra rs rb)
1483 (:printer x-5 ((op 31) (xo 444) (rc 1)) '((:cond
1484 ((rs :same-as rb) 'mr.)
1488 (:unless (:same-as rs) "," rb)))
1491 (:dependencies (reads rb) (reads rs) (writes ra) (writes :ccr))
1493 (emit-x-form-inst segment
1495 (reg-tn-encoding rs)
1496 (reg-tn-encoding ra)
1497 (reg-tn-encoding rb)
1501 (define-instruction-macro mr (ra rs)
1502 `(inst or ,ra ,rs ,rs))
1504 (define-instruction-macro mr. (ra rs)
1505 `(inst or. ,ra ,rs ,rs))
1507 (define-4-xo-instructions divwu 31 459 :cost 36)
1509 ; This is a 601-specific instruction class.
1510 (define-4-xo-instructions div 31 331 :cost 36)
1512 ; This is a 601-specific instruction.
1513 (define-instruction mtmq (segment rt)
1514 (:printer xfx ((op 31) (xo 467) (spr (ash 0 5))) '(:name :tab rt))
1516 (:dependencies (reads rt) (writes :xer))
1517 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 467 0)))
1519 (define-instruction mtxer (segment rt)
1520 (:printer xfx ((op 31) (xo 467) (spr (ash 1 5))) '(:name :tab rt))
1522 (:dependencies (reads rt) (writes :xer))
1523 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 467 0)))
1525 (define-instruction mtlr (segment rt)
1526 (:printer xfx ((op 31) (xo 467) (spr (ash 8 5))) '(:name :tab rt))
1528 (:dependencies (reads rt) (writes :lr))
1529 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 467 0)))
1531 (define-instruction mtctr (segment rt)
1532 (:printer xfx ((op 31) (xo 467) (spr (ash 9 5))) '(:name :tab rt))
1534 (:dependencies (reads rt) (writes :ctr))
1535 (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 467 0)))
1538 (define-2-x-5-instructions nand 31 476)
1539 (define-4-xo-instructions divw 31 491 :cost 36)
1540 (define-instruction mcrxr (segment crf)
1541 (:printer x-18 ((op 31) (xo 512)))
1543 (:dependencies (reads :xer) (writes :ccr) (writes :xer))
1544 (:emitter (emit-x-form-inst segment 31 (valid-cr-field-encoding crf) 0 0 512 0)))
1546 (define-instruction lswx (segment rs ra rb)
1547 (:printer x ((op 31) (xo 533) (rc 0)))
1551 (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) 533 0)))
1552 (define-x-instruction lwbrx 31 534)
1553 (define-x-20-instruction lfsx 31 535)
1554 (define-2-x-5-instructions srw 31 536)
1555 (define-x-20-instruction lfsux 31 567 :other-dependencies ((writes ra)))
1557 (define-instruction lswi (segment rt ra rb)
1558 (:printer x-1 ((op 31) (xo 597) (rc 0)))
1562 (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rt) (reg-tn-encoding ra) rb 597 0)))
1564 (define-instruction sync (segment)
1565 (:printer x-27 ((op 31) (xo 598)))
1568 (:emitter (emit-x-form-inst segment 31 0 0 0 598 0)))
1569 (define-x-20-instruction lfdx 31 599)
1570 (define-x-20-instruction lfdux 31 631 :other-dependencies ((writes ra)))
1571 (define-instruction stswx (segment rs ra rb)
1572 (:printer x-5 ((op 31) (xo 661)))
1576 (:emitter (emit-x-form-inst sb!assem:segment 31
1577 (reg-tn-encoding rs)
1578 (reg-tn-encoding ra)
1579 (reg-tn-encoding rb)
1582 (define-x-5-st-instruction stwbrx 31 662 nil)
1583 (define-x-23-st-instruction stfsx 31 663)
1584 (define-x-23-st-instruction stfsux 31 695 :other-dependencies ((writes ra)))
1585 (define-instruction stswi (segment rs ra nb)
1586 (:printer x-8 ((op 31) (xo 725)))
1590 (emit-x-form-inst segment 31
1591 (reg-tn-encoding rs)
1592 (reg-tn-encoding ra)
1597 (define-x-23-st-instruction stfdx 31 727)
1598 (define-x-23-st-instruction stfdux 31 759 :other-dependencies ((writes ra)))
1599 (define-x-instruction lhbrx 31 790)
1600 (define-2-x-5-instructions sraw 31 792)
1602 (define-instruction srawi (segment ra rs rb)
1603 (:printer x-9 ((op 31) (xo 824) (rc 0)))
1606 (:dependencies (reads rs) (writes ra))
1608 (emit-x-form-inst segment 31
1609 (reg-tn-encoding rs)
1610 (reg-tn-encoding ra)
1615 (define-instruction srawi. (segment ra rs rb)
1616 (:printer x-9 ((op 31) (xo 824) (rc 1)))
1619 (:dependencies (reads rs) (writes ra) (writes :ccr))
1621 (emit-x-form-inst segment 31
1622 (reg-tn-encoding rs)
1623 (reg-tn-encoding ra)
1628 (define-instruction eieio (segment)
1629 (:printer x-27 ((op 31) (xo 854)))
1632 (:emitter (emit-x-form-inst segment 31 0 0 0 854 0)))
1634 (define-x-5-st-instruction sthbrx 31 918 nil)
1636 (define-2-x-10-instructions extsb 31 954)
1637 (define-2-x-10-instructions extsh 31 922)
1640 (define-instruction lwz (segment rt ra si)
1641 (:declare (type (or fixup (signed-byte 16)) si))
1642 (:printer d ((op 32)))
1645 (:dependencies (reads ra) (writes rt) (reads :memory))
1647 (when (typep si 'fixup)
1648 (note-fixup segment :l si)
1650 (emit-d-form-inst segment 32 (reg-tn-encoding rt) (reg-tn-encoding ra) si)))
1652 (define-d-instruction lwzu 33 :other-dependencies ((writes ra)))
1653 (define-d-instruction lbz 34)
1654 (define-d-instruction lbzu 35 :other-dependencies ((writes ra)))
1655 (define-d-rs-instruction stw 36)
1656 (define-d-rs-instruction stwu 37 :other-dependencies ((writes ra)))
1657 (define-d-rs-instruction stb 38)
1658 (define-d-rs-instruction stbu 39 :other-dependencies ((writes ra)))
1659 (define-d-instruction lhz 40)
1660 (define-d-instruction lhzu 41 :other-dependencies ((writes ra)))
1661 (define-d-instruction lha 42)
1662 (define-d-instruction lhau 43 :other-dependencies ((writes ra)))
1663 (define-d-rs-instruction sth 44)
1664 (define-d-rs-instruction sthu 45 :other-dependencies ((writes ra)))
1665 (define-d-instruction lmw 46 :pinned t)
1666 (define-d-rs-instruction stmw 47 :pinned t)
1667 (define-d-frt-instruction lfs 48)
1668 (define-d-frt-instruction lfsu 49 :other-dependencies ((writes ra)))
1669 (define-d-frt-instruction lfd 50)
1670 (define-d-frt-instruction lfdu 51 :other-dependencies ((writes ra)))
1671 (define-d-frs-instruction stfs 52)
1672 (define-d-frs-instruction stfsu 53 :other-dependencies ((writes ra)))
1673 (define-d-frs-instruction stfd 54)
1674 (define-d-frs-instruction stfdu 55 :other-dependencies ((writes ra)))
1676 (define-2-a-tab-instructions fdivs 59 18 :cost 17)
1677 (define-2-a-tab-instructions fsubs 59 20)
1678 (define-2-a-tab-instructions fadds 59 21)
1679 (define-2-a-tac-instructions fmuls 59 25)
1680 (define-2-a-instructions fmsubs 59 28 :cost 4)
1681 (define-2-a-instructions fmadds 59 29 :cost 4)
1682 (define-2-a-instructions fnmsubs 59 30 :cost 4)
1683 (define-2-a-instructions fnmadds 59 31 :cost 4)
1685 (define-instruction fcmpu (segment crfd fra frb)
1686 (:printer x-15 ((op 63) (xo 0)))
1687 (:dependencies (reads fra) (reads frb) (reads :fpscr)
1688 (writes :fpscr) (writes :ccr))
1691 (:emitter (emit-x-form-inst segment
1693 (valid-cr-field-encoding crfd)
1694 (fp-reg-tn-encoding fra)
1695 (fp-reg-tn-encoding frb)
1700 (define-2-x-21-instructions frsp 63 12)
1701 (define-2-x-21-instructions fctiw 63 14)
1702 (define-2-x-21-instructions fctiwz 63 15)
1704 (define-2-a-tab-instructions fdiv 63 18 :cost 31)
1705 (define-2-a-tab-instructions fsub 63 20)
1706 (define-2-a-tab-instructions fadd 63 21)
1707 (define-2-a-tac-instructions fmul 63 25 :cost 5)
1708 (define-2-a-instructions fmsub 63 28 :cost 5)
1709 (define-2-a-instructions fmadd 63 29 :cost 5)
1710 (define-2-a-instructions fnmsub 63 30 :cost 5)
1711 (define-2-a-instructions fnmadd 63 31 :cost 5)
1713 (define-instruction fcmpo (segment crfd fra frb)
1714 (:printer x-15 ((op 63) (xo 32)))
1715 (:dependencies (reads fra) (reads frb) (reads :fpscr)
1716 (writes :fpscr) (writes :ccr))
1719 (:emitter (emit-x-form-inst segment
1721 (valid-cr-field-encoding crfd)
1722 (fp-reg-tn-encoding fra)
1723 (fp-reg-tn-encoding frb)
1727 (define-2-x-21-instructions fneg 63 40)
1729 (define-2-x-21-instructions fmr 63 72)
1730 (define-2-x-21-instructions fnabs 63 136)
1731 (define-2-x-21-instructions fabs 63 264)
1733 (define-instruction mffs (segment frd)
1734 (:printer x-22 ((op 63) (xo 583) (rc 0)))
1736 (:dependencies (reads :fpscr) (writes frd))
1737 (:emitter (emit-x-form-inst segment
1739 (fp-reg-tn-encoding frd)
1745 (define-instruction mffs. (segment frd)
1746 (:printer x-22 ((op 63) (xo 583) (rc 1)))
1748 (:dependencies (reads :fpscr) (writes frd) (writes :ccr))
1749 (:emitter (emit-x-form-inst segment
1751 (fp-reg-tn-encoding frd)
1757 (define-instruction mtfsf (segment mask rb)
1758 (:printer xfl ((op 63) (xo 711) (rc 0)))
1759 (:dependencies (reads rb) (writes :fpscr))
1761 (:emitter (emit-xfl-form-inst segment 63 (ash mask 1) (fp-reg-tn-encoding rb) 711 0)))
1763 (define-instruction mtfsf. (segment mask rb)
1764 (:printer xfl ((op 63) (xo 711) (rc 1)))
1766 (:dependencies (reads rb) (writes :ccr) (writes :fpscr))
1767 (:emitter (emit-xfl-form-inst segment 63 (ash mask 1) (fp-reg-tn-encoding rb) 711 1)))
1772 ;;; Here in the future, macros are our friends.
1774 (define-instruction-macro subis (rt ra simm)
1775 `(inst addis ,rt ,ra (- ,simm)))
1777 (define-instruction-macro sub (rt rb ra)
1778 `(inst subf ,rt ,ra ,rb))
1779 (define-instruction-macro sub. (rt rb ra)
1780 `(inst subf. ,rt ,ra ,rb))
1781 (define-instruction-macro subo (rt rb ra)
1782 `(inst subfo ,rt ,ra ,rb))
1783 (define-instruction-macro subo. (rt rb ra)
1784 `(inst subfo. ,rt ,ra ,rb))
1787 (define-instruction-macro subic (rt ra simm)
1788 `(inst addic ,rt ,ra (- ,simm)))
1791 (define-instruction-macro subic. (rt ra simm)
1792 `(inst addic. ,rt ,ra (- ,simm)))
1796 (define-instruction-macro subc (rt rb ra)
1797 `(inst subfc ,rt ,ra ,rb))
1798 (define-instruction-macro subc. (rt rb ra)
1799 `(inst subfc. ,rt ,ra ,rb))
1800 (define-instruction-macro subco (rt rb ra)
1801 `(inst subfco ,rt ,ra ,rb))
1802 (define-instruction-macro subco. (rt rb ra)
1803 `(inst subfco. ,rt ,ra ,rb))
1805 (define-instruction-macro subi (rt ra simm)
1806 `(inst addi ,rt ,ra (- ,simm)))
1808 (define-instruction-macro li (rt val)
1809 `(inst addi ,rt zero-tn ,val))
1811 (define-instruction-macro lis (rt val)
1812 `(inst addis ,rt zero-tn ,val))
1815 (define-instruction-macro not (ra rs)
1816 `(inst nor ,ra ,rs ,rs))
1818 (define-instruction-macro not. (ra rs)
1819 `(inst nor. ,ra ,rs ,rs))
1822 (!def-vm-support-routine emit-nop (segment)
1823 (emit-word segment #x60000000))
1825 (define-instruction-macro extlwi (ra rs n b)
1826 `(inst rlwinm ,ra ,rs ,b 0 (1- ,n)))
1828 (define-instruction-macro extlwi. (ra rs n b)
1829 `(inst rlwinm. ,ra ,rs ,b 0 (1- ,n)))
1831 (define-instruction-macro extrwi (ra rs n b)
1832 `(inst rlwinm ,ra ,rs (mod (+ ,b ,n) 32) (- 32 ,n) 31))
1834 (define-instruction-macro extrwi. (ra rs n b)
1835 `(inst rlwinm. ,ra ,rs (mod (+ ,b ,n) 32) (- 32 ,n) 31))
1837 (define-instruction-macro srwi (ra rs n)
1838 `(inst rlwinm ,ra ,rs (- 32 ,n) ,n 31))
1840 (define-instruction-macro srwi. (ra rs n)
1841 `(inst rlwinm. ,ra ,rs (- 32 ,n) ,n 31))
1843 (define-instruction-macro clrrwi (ra rs n)
1844 `(inst rlwinm ,ra ,rs 0 0 (- 31 ,n)))
1846 (define-instruction-macro clrrwi. (ra rs n)
1847 `(inst rlwinm. ,ra ,rs 0 0 (- 31 ,n)))
1849 (define-instruction-macro inslw (ra rs n b)
1850 `(inst rlwimi ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1852 (define-instruction-macro inslw. (ra rs n b)
1853 `(inst rlwimi. ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1855 (define-instruction-macro rotlw (ra rs rb)
1856 `(inst rlwnm ,ra ,rs ,rb 0 31))
1858 (define-instruction-macro rotlw. (ra rs rb)
1859 `(inst rlwnm. ,ra ,rs ,rb 0 31))
1861 (define-instruction-macro rotlwi (ra rs n)
1862 `(inst rlwinm ,ra ,rs ,n 0 31))
1864 (define-instruction-macro rotrwi (ra rs n)
1865 `(inst rlwinm ,ra ,rs (- 32 ,n) 0 31))
1867 (define-instruction-macro slwi (ra rs n)
1868 `(inst rlwinm ,ra ,rs ,n 0 (- 31 ,n)))
1870 (define-instruction-macro slwi. (ra rs n)
1871 `(inst rlwinm. ,ra ,rs ,n 0 (- 31 ,n))))
1878 ((define-conditional-branches (name bo-name)
1879 (let* ((bo-enc (valid-bo-encoding bo-name)))
1881 (define-instruction-macro ,(symbolicate name "A") (bi target)
1882 ``(inst bca ,,,bo-enc ,,bi ,,target))
1883 (define-instruction-macro ,(symbolicate name "L") (bi target)
1884 ``(inst bcl ,,,bo-enc ,,bi ,,target))
1885 (define-instruction-macro ,(symbolicate name "LA") (bi target)
1886 ``(inst bcla ,,,bo-enc ,,bi ,,target))
1887 (define-instruction-macro ,(symbolicate name "CTR") (bi target)
1888 ``(inst bcctr ,,,bo-enc ,,bi ,,target))
1889 (define-instruction-macro ,(symbolicate name "CTRL") (bi target)
1890 ``(inst bcctrl ,,,bo-enc ,,bi ,,target))
1891 (define-instruction-macro ,(symbolicate name "LR") (bi target)
1892 ``(inst bclr ,,,bo-enc ,,bi ,,target))
1893 (define-instruction-macro ,(symbolicate name "LRL") (bi target)
1894 ``(inst bclrl ,,,bo-enc ,,bi ,,target))))))
1895 (define-conditional-branches bt :bo-t)
1896 (define-conditional-branches bf :bo-f))
1900 ((define-positive-conditional-branches (name cr-bit-name)
1902 (define-instruction-macro ,name (crf &optional (target nil target-p))
1904 (setq target crf crf :cr0))
1905 `(inst bt `(,,crf ,,,cr-bit-name) ,target))
1907 (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1908 ``(inst bta (,,cr-field ,,,cr-bit-name) ,,target))
1909 (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1910 ``(inst btl (,,cr-field ,,,cr-bit-name) ,,target))
1911 (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1912 ``(inst btla (,,cr-field ,,,cr-bit-name) ,,target))
1913 (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1914 ``(inst btctr (,,cr-field ,,,cr-bit-name) ,,target))
1915 (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1916 ``(inst btctrl (,,cr-field ,,,cr-bit-name) ,,target))
1917 (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1918 ``(inst btlr (,,cr-field ,,,cr-bit-name) ,,target))
1919 (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1920 ``(inst btlrl (,,cr-field ,,,cr-bit-name) ,,target))
1923 (define-positive-conditional-branches beq :eq)
1924 (define-positive-conditional-branches blt :lt)
1925 (define-positive-conditional-branches bgt :gt)
1926 (define-positive-conditional-branches bso :so)
1927 (define-positive-conditional-branches bun :so))
1931 ((define-negative-conditional-branches (name cr-bit-name)
1933 (define-instruction-macro ,name (crf &optional (target nil target-p))
1935 (setq target crf crf :cr0))
1936 `(inst bf `(,,crf ,,,cr-bit-name) ,target))
1938 (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1939 ``(inst bfa (,,cr-field ,,,cr-bit-name) ,,target))
1940 (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1941 ``(inst bfl (,,cr-field ,,,cr-bit-name) ,,target))
1942 (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1943 ``(inst bfla (,,cr-field ,,,cr-bit-name) ,,target))
1944 (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1945 ``(inst bfctr (,,cr-field ,,,cr-bit-name) ,,target))
1946 (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1947 ``(inst bfctrl (,,cr-field ,,,cr-bit-name) ,,target))
1948 (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1949 ``(inst bflr (,,cr-field ,,,cr-bit-name) ,,target))
1950 (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1951 ``(inst bflrl (,,cr-field ,,,cr-bit-name) ,,target))
1954 (define-negative-conditional-branches bne :eq)
1955 (define-negative-conditional-branches bnl :lt)
1956 (define-negative-conditional-branches bge :lt)
1957 (define-negative-conditional-branches bng :gt)
1958 (define-negative-conditional-branches ble :gt)
1959 (define-negative-conditional-branches bns :so)
1960 (define-negative-conditional-branches bnu :so))
1964 (define-instruction-macro j (func-tn offset)
1966 (inst addi lip-tn ,func-tn ,offset)
1972 (define-instruction-macro bua (target)
1973 `(inst bca :bo-u 0 ,target))
1975 (define-instruction-macro bul (target)
1976 `(inst bcl :bo-u 0 ,target))
1978 (define-instruction-macro bula (target)
1979 `(inst bcla :bo-u 0 ,target))
1982 (define-instruction-macro blrl ()
1983 `(inst bclrl :bo-u 0))
1986 ;;; Some more macros
1988 (defun %lr (reg value)
1991 (inst li reg value))
1993 (inst ori reg zero-tn value))
1994 ((or (signed-byte 32) (unsigned-byte 32))
1995 (let* ((high-half (ldb (byte 16 16) value))
1996 (low-half (ldb (byte 16 0) value)))
1997 (declare (type (unsigned-byte 16) high-half low-half))
1998 (cond ((and (logbitp 15 low-half) (= high-half #xffff))
1999 (inst li reg (dpb low-half (byte 16 0) -1)))
2000 ((and (not (logbitp 15 low-half)) (zerop high-half))
2001 (inst li reg low-half))
2003 (inst lis reg (if (logbitp 15 high-half)
2004 (dpb high-half (byte 16 0) -1)
2006 (unless (zerop low-half)
2007 (inst ori reg reg low-half))))))
2009 (inst lis reg value)
2010 (inst addi reg reg value))))
2012 (define-instruction-macro lr (reg value)
2017 ;;;; Instructions for dumping data and header objects.
2019 (define-instruction word (segment word)
2020 (:declare (type (or (unsigned-byte 32) (signed-byte 32)) word))
2024 (emit-word segment word)))
2026 (define-instruction short (segment short)
2027 (:declare (type (or (unsigned-byte 16) (signed-byte 16)) short))
2031 (emit-short segment short)))
2033 (define-instruction byte (segment byte)
2034 (:declare (type (or (unsigned-byte 8) (signed-byte 8)) byte))
2038 (emit-byte segment byte)))
2040 (define-bitfield-emitter emit-header-object 32
2041 (byte 24 8) (byte 8 0))
2043 (defun emit-header-data (segment type)
2046 #'(lambda (segment posn)
2049 (ash (+ posn (component-header-length))
2050 (- n-widetag-bits word-shift)))))))
2052 (define-instruction simple-fun-header-word (segment)
2056 (emit-header-data segment simple-fun-header-widetag)))
2058 (define-instruction lra-header-word (segment)
2062 (emit-header-data segment return-pc-header-widetag)))
2065 ;;;; Instructions for converting between code objects, functions, and lras.
2066 (defun emit-compute-inst (segment vop dst src label temp calc)
2068 ;; We emit either 12 or 4 bytes, so we maintain 8 byte alignments.
2070 #'(lambda (segment posn delta-if-after)
2071 (let ((delta (funcall calc label posn delta-if-after)))
2072 (when (<= (- (ash 1 15)) delta (1- (ash 1 15)))
2073 (emit-back-patch segment 4
2074 #'(lambda (segment posn)
2075 (assemble (segment vop)
2077 (funcall calc label posn 0)))))
2079 #'(lambda (segment posn)
2080 (let ((delta (funcall calc label posn 0)))
2081 (assemble (segment vop)
2082 (inst lis temp (ldb (byte 16 16) delta))
2083 (inst ori temp temp (ldb (byte 16 0) delta))
2084 (inst add dst src temp))))))
2086 ;; this function is misnamed. should be compute-code-from-lip,
2087 ;; if the use in xep-allocate-frame is typical
2088 ;; (someone says code = fn - header - label-offset + other-pointer-tag)
2089 (define-instruction compute-code-from-fn (segment dst src label temp)
2090 (:declare (type tn dst src temp) (type label label))
2091 (:attributes variable-length)
2092 (:dependencies (reads src) (writes dst) (writes temp))
2096 (emit-compute-inst segment vop dst src label temp
2097 #'(lambda (label posn delta-if-after)
2098 (- other-pointer-lowtag
2099 ;;function-pointer-type
2100 (label-position label posn delta-if-after)
2101 (component-header-length))))))
2103 ;; code = lra - other-pointer-tag - header - label-offset + other-pointer-tag
2104 (define-instruction compute-code-from-lra (segment dst src label temp)
2105 (:declare (type tn dst src temp) (type label label))
2106 (:attributes variable-length)
2107 (:dependencies (reads src) (writes dst) (writes temp))
2111 (emit-compute-inst segment vop dst src label temp
2112 #'(lambda (label posn delta-if-after)
2113 (- (+ (label-position label posn delta-if-after)
2114 (component-header-length)))))))
2116 ;; lra = code + other-pointer-tag + header + label-offset - other-pointer-tag
2117 (define-instruction compute-lra-from-code (segment dst src label temp)
2118 (:declare (type tn dst src temp) (type label label))
2119 (:attributes variable-length)
2120 (:dependencies (reads src) (writes dst) (writes temp))
2124 (emit-compute-inst segment vop dst src label temp
2125 #'(lambda (label posn delta-if-after)
2126 (+ (label-position label posn delta-if-after)
2127 (component-header-length))))))