0.8.2.7:
[sbcl.git] / src / compiler / ppc / insts.lisp
1 ;;;; the instruction set definition for the PPC
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13
14 ;(def-assembler-params
15 ;    :scheduler-p nil ; t when we trust the scheduler not to "fill delay slots"
16 ;  :max-locations 70)
17 \f
18 ;;;; Constants, types, conversion functions, some disassembler stuff.
19
20 (defun reg-tn-encoding (tn)
21   (declare (type tn tn))
22   (sc-case tn
23     (zero zero-offset)
24     (null null-offset)
25     (t
26      (if (eq (sb-name (sc-sb (tn-sc tn))) 'registers)
27          (tn-offset tn)
28          (error "~S isn't a register." tn)))))
29
30 (defun fp-reg-tn-encoding (tn)
31   (declare (type tn tn))
32   (unless (eq (sb-name (sc-sb (tn-sc tn))) 'float-registers)
33     (error "~S isn't a floating-point register." tn))
34   (tn-offset tn))
35
36 ;(sb!disassem:set-disassem-params :instruction-alignment 32)
37
38 (defvar *disassem-use-lisp-reg-names* t)
39
40 (!def-vm-support-routine location-number (loc)
41   (etypecase loc
42     (null)
43     (number)
44     (label)
45     (fixup)
46     (tn
47      (ecase (sb-name (sc-sb (tn-sc loc)))
48        (immediate-constant
49         ;; Can happen if $ZERO or $NULL are passed in.
50         nil)
51        (registers
52         (unless (zerop (tn-offset loc))
53           (tn-offset loc)))
54        (float-registers
55         (+ (tn-offset loc) 32))))
56     (symbol
57      (ecase loc
58        (:memory 0)
59        (:ccr 64)
60        (:xer 65)
61        (:lr 66)
62        (:ctr 67)
63        (:fpscr 68)))))
64
65 (defparameter reg-symbols
66   (map 'vector
67        #'(lambda (name)
68            (cond ((null name) nil)
69                  (t (make-symbol (concatenate 'string "$" name)))))
70        *register-names*))
71
72 (defun maybe-add-notes (regno dstate)
73   (let* ((inst (sb!disassem::sap-ref-int
74                 (sb!disassem::dstate-segment-sap dstate)
75                 (sb!disassem::dstate-cur-offs dstate)
76                 n-word-bytes
77                 (sb!disassem::dstate-byte-order dstate)))
78          (op (ldb (byte 6 26) inst)))
79     (case op
80       ;; lwz
81       (32
82        (when (= regno (ldb (byte 5 16) inst)) ; only for the second 
83          (case (ldb (byte 5 16) inst)
84            ;; reg_CODE
85            (19
86             (sb!disassem:note-code-constant (ldb (byte 16 0) inst) dstate)))))
87       ;; addi
88       (14
89        (when (= regno null-offset)
90          (sb!disassem:maybe-note-nil-indexed-object
91           (ldb (byte 16 0) inst) dstate))))))
92
93 (sb!disassem:define-arg-type reg
94   :printer 
95   (lambda (value stream dstate)
96     (declare (type stream stream) (fixnum value))
97     (let ((regname (aref reg-symbols value)))
98       (princ regname stream)
99       (sb!disassem:maybe-note-associated-storage-ref
100        value 'registers regname dstate)
101       (maybe-add-notes value dstate))))
102
103 (defparameter float-reg-symbols
104   #.(coerce 
105      (loop for n from 0 to 31 collect (make-symbol (format nil "$F~d" n)))
106      'vector))
107
108 (sb!disassem:define-arg-type fp-reg
109   :printer #'(lambda (value stream dstate)
110                (declare (type stream stream) (fixnum value))
111                (let ((regname (aref float-reg-symbols value)))
112                  (princ regname stream)
113                  (sb!disassem:maybe-note-associated-storage-ref
114                   value
115                   'float-registers
116                   regname
117                   dstate))))
118
119 (eval-when (:compile-toplevel :load-toplevel :execute)
120   (defparameter bo-kind-names
121     #(:bo-dnzf :bo-dnzfp :bo-dzf :bo-dzfp :bo-f :bo-fp nil nil
122       :bo-dnzt :bo-dnztp :bo-dzt :bo-dztp :bo-t :bo-tp nil nil
123       :bo-dnz :bo-dnzp :bo-dz :bo-dzp :bo-u nil nil nil
124       nil nil nil nil nil nil nil nil)))
125
126 (sb!disassem:define-arg-type bo-field
127   :printer #'(lambda (value stream dstate)
128                (declare (ignore dstate)
129                         (type stream stream)
130                         (type fixnum value))
131                (princ (svref bo-kind-names value) stream)))
132
133 (eval-when (:compile-toplevel :load-toplevel :execute)
134 (defun valid-bo-encoding (enc)
135   (or (if (integerp enc)
136         (and (= enc (logand #x1f enc))
137              (not (null (svref bo-kind-names enc)))
138              enc)
139         (and enc (position enc bo-kind-names)))
140       (error "Invalid BO field spec: ~s" enc)))
141 )
142
143
144 (defparameter cr-bit-names #(:lt :gt :eq :so))
145 (defparameter cr-bit-inverse-names #(:ge :le :ne :ns))
146
147 (defparameter cr-field-names #(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))
148
149 (defun valid-cr-bit-encoding (enc &optional error-p)
150   (or (if (integerp enc)
151         (and (= enc (logand 3 enc))
152              enc))
153       (position enc cr-bit-names)
154       (if error-p (error "Invalid condition bit specifier : ~s" enc))))
155
156 (defun valid-cr-field-encoding (enc)
157   (let* ((field (if (integerp enc) 
158                   (and (= enc (logand #x7 enc)))
159                   (position enc cr-field-names))))
160     (if field
161       (ash field 2)
162       (error "Invalid condition register field specifier : ~s" enc))))
163                 
164 (defun valid-bi-encoding (enc)
165   (or
166    (if (atom enc) 
167      (if (integerp enc) 
168        (and (= enc (logand 31 enc)) enc)
169        (position enc cr-bit-names))
170      (+ (valid-cr-field-encoding (car enc))
171         (valid-cr-bit-encoding (cadr enc))))
172    (error "Invalid BI field spec : ~s" enc)))
173
174 (sb!disassem:define-arg-type bi-field
175   :printer #'(lambda (value stream dstate)
176                (declare (ignore dstate)
177                         (type stream stream)
178                         (type (unsigned-byte 5) value))
179                (let* ((bitname (svref cr-bit-names (logand 3 value)))
180                       (crfield (ash value -2)))
181                  (declare (type (unsigned-byte 3) crfield))
182                  (if (= crfield 0)
183                    (princ bitname stream)
184                    (princ (list (svref cr-field-names crfield) bitname) stream)))))
185
186 (sb!disassem:define-arg-type crf
187   :printer #'(lambda (value stream dstate)
188                (declare (ignore dstate)
189                         (type stream stream)
190                         (type (unsigned-byte 3) value))
191                (princ (svref cr-field-names value) stream)))
192
193 (sb!disassem:define-arg-type relative-label
194   :sign-extend t
195   :use-label #'(lambda (value dstate)
196                  (declare (type (signed-byte 14) value)
197                           (type sb!disassem:disassem-state dstate))
198                  (+ (ash value 2) (sb!disassem:dstate-cur-addr dstate))))
199
200 (eval-when (:compile-toplevel :load-toplevel :execute)
201   (defparameter trap-values-alist '((:t . 31) (:lt . 16) (:le . 20) (:eq . 4) (:lng . 6)
202                                    (:ge .12) (:ne . 24) (:ng . 20) (:llt . 2) (:f . 0)
203                                    (:lle . 6) (:lge . 5) (:lgt . 1) (:lnl . 5))))
204                                    
205     
206 (defun valid-tcond-encoding (enc)
207   (or (and (if (integerp enc) (= (logand 31 enc) enc)) enc)
208       (cdr (assoc enc trap-values-alist))
209       (error "Unknown trap condition: ~s" enc)))
210         
211 (sb!disassem:define-arg-type to-field
212   :sign-extend nil
213   :printer #'(lambda (value stream dstate)
214                (declare (ignore dstate)
215                         (type stream stream)
216                         (type fixnum value))
217                (princ (or (car (rassoc value trap-values-alist))
218                           value) 
219                       stream)))
220
221 (defun snarf-error-junk (sap offset &optional length-only)
222   (let* ((length (sb!sys:sap-ref-8 sap offset))
223          (vector (make-array length :element-type '(unsigned-byte 8))))
224     (declare (type sb!sys:system-area-pointer sap)
225              (type (unsigned-byte 8) length)
226              (type (simple-array (unsigned-byte 8) (*)) vector))
227     (cond (length-only
228            (values 0 (1+ length) nil nil))
229           (t
230            (sb!kernel:copy-from-system-area sap (* sb!vm:n-byte-bits (1+ offset))
231                                          vector (* sb!vm:n-word-bits
232                                                    sb!vm:vector-data-offset)
233                                          (* length sb!vm:n-byte-bits))
234            (collect ((sc-offsets)
235                      (lengths))
236              (lengths 1)                ; the length byte
237              (let* ((index 0)
238                     (error-number (sb!c:read-var-integer vector index)))
239                (lengths index)
240                (loop
241                  (when (>= index length)
242                    (return))
243                  (let ((old-index index))
244                    (sc-offsets (sb!c:read-var-integer vector index))
245                    (lengths (- index old-index))))
246                (values error-number
247                        (1+ length)
248                        (sc-offsets)
249                        (lengths))))))))
250
251 (defun emit-conditional-branch (segment bo bi target &optional aa-p lk-p)
252   (declare (type boolean aa-p lk-p))
253   (let* ((bo (valid-bo-encoding bo))
254          (bi (valid-bi-encoding bi))
255          (aa-bit (if aa-p 1 0))
256          (lk-bit (if lk-p 1 0)))
257     (if aa-p                            ; Not bloody likely, bwth.
258       (emit-b-form-inst segment 16 bo bi target aa-bit lk-bit)
259       ;; the target may be >32k away, in which case we have to invert the
260       ;; test and do an absolute branch
261       (emit-chooser
262        ;; We emit either 4 or 8 bytes, so I think we declare this as
263        ;; preserving 4 byte alignment.  If this gives us no joy, we can
264        ;; stick a nop in the long branch and then we will be
265        ;; preserving 8 byte alignment
266        segment 8 2 ; 2^2 is 4 byte alignment.  I think
267        #'(lambda (segment posn magic-value)
268            (let ((delta (ash (- (label-position target posn magic-value) posn)
269                              -2)))
270              (when (typep delta '(signed-byte 14))
271                (emit-back-patch segment 4
272                                 #'(lambda (segment posn)
273                                     (emit-b-form-inst 
274                                      segment 16 bo bi
275                                      (ash (- (label-position target) posn) -2)
276                                      aa-bit lk-bit)))
277                t)))
278        #'(lambda (segment posn)
279            (let ((bo (logxor 8 bo))) ;; invert the test
280              (emit-b-form-inst segment 16 bo bi
281                                2 ; skip over next instruction
282                                0 0)
283              (emit-back-patch segment 4
284                               #'(lambda (segment posn)
285                                   (emit-i-form-branch segment target lk-p)))))
286        ))))
287              
288
289
290 ; non-absolute I-form: B, BL.
291 (defun emit-i-form-branch (segment target &optional lk-p)
292   (let* ((lk-bit (if lk-p 1 0)))
293     (etypecase target
294       (fixup
295        (note-fixup segment :b target)
296        (emit-i-form-inst segment 18 0 0 lk-bit))
297       (label
298        (emit-back-patch segment 4
299                         #'(lambda (segment posn)
300                             (emit-i-form-inst 
301                              segment
302                              18
303                              (ash (- (label-position target) posn) -2)
304                              0
305                              lk-bit)))))))
306
307 (eval-when (:compile-toplevel :execute :load-toplevel)
308 (defparameter *spr-numbers-alist* '((:xer 1) (:lr 8) (:ctr 9))))
309
310 (sb!disassem:define-arg-type spr
311   :printer #'(lambda (value stream dstate)
312                (declare (ignore dstate)
313                         (type (unsigned-byte 10) value))
314                (let* ((name (car (rassoc value *spr-numbers-alist*))))
315                    (if name
316                      (princ name stream)
317                      (princ value stream)))))
318
319 (eval-when (:compile-toplevel :load-toplevel :execute)
320   (defparameter jump-printer
321     #'(lambda (value stream dstate)
322         (let ((addr (ash value 2)))
323           (sb!disassem:maybe-note-assembler-routine addr t dstate)
324           (write addr :base 16 :radix t :stream stream)))))
325
326
327 \f
328 ;;;; dissassem:define-instruction-formats
329
330 (eval-when (:compile-toplevel :execute)
331   (defmacro ppc-byte (startbit &optional (endbit startbit))
332     (unless (and (typep startbit '(unsigned-byte 32))
333                  (typep endbit '(unsigned-byte 32))
334                  (>= endbit startbit))
335       (error "Bad bits."))
336     ``(byte ,(1+ ,(- endbit startbit)) ,(- 31 ,endbit)))
337
338   (defparameter *ppc-field-specs-alist*
339     `((aa :field ,(ppc-byte 30))
340       (ba :field ,(ppc-byte 11 15) :type 'bi-field)
341       (bb :field ,(ppc-byte 16 20) :type 'bi-field)
342       (bd :field ,(ppc-byte 16 29) :type 'relative-label)
343       (bf :field ,(ppc-byte 6 8) :type 'crf)
344       (bfa :field ,(ppc-byte 11 13) :type 'crf)
345       (bi :field ,(ppc-byte 11 15) :type 'bi-field)
346       (bo :field ,(ppc-byte 6 10) :type 'bo-field)
347       (bt :field ,(ppc-byte 6 10) :type 'bi-field)
348       (d :field ,(ppc-byte 16 31) :sign-extend t)
349       (flm :field ,(ppc-byte 7 14) :sign-extend nil)
350       (fra :field ,(ppc-byte 11 15) :type 'fp-reg)
351       (frb :field ,(ppc-byte 16 20) :type 'fp-reg)
352       (frc :field ,(ppc-byte 21 25) :type 'fp-reg)
353       (frs :field ,(ppc-byte 6 10) :type 'fp-reg)
354       (frt :field ,(ppc-byte 6 10) :type 'fp-reg)
355       (fxm :field ,(ppc-byte 12 19) :sign-extend nil)
356       (l :field ,(ppc-byte 10) :sign-extend nil)
357       (li :field ,(ppc-byte 6 29) :sign-extend t :type 'relative-label)
358       (li-abs :field ,(ppc-byte 6 29) :sign-extend t :printer jump-printer)
359       (lk :field ,(ppc-byte 31))
360       (mb :field ,(ppc-byte 21 25) :sign-extend nil)
361       (me :field ,(ppc-byte 26 30) :sign-extend nil)
362       (nb :field ,(ppc-byte 16 20) :sign-extend nil)
363       (oe :field ,(ppc-byte 21))
364       (ra :field ,(ppc-byte 11 15) :type 'reg)
365       (rb :field ,(ppc-byte 16 20) :type 'reg)
366       (rc :field ,(ppc-byte 31))
367       (rs :field ,(ppc-byte 6 10) :type 'reg)
368       (rt :field ,(ppc-byte 6 10) :type 'reg)
369       (sh :field ,(ppc-byte 16 20) :sign-extend nil)
370       (si :field ,(ppc-byte 16 31) :sign-extend t)
371       (spr :field ,(ppc-byte 11 20) :type 'spr)
372       (to :field ,(ppc-byte 6 10) :type 'to-field)
373       (u :field ,(ppc-byte 16 19) :sign-extend nil)
374       (ui :field ,(ppc-byte 16 31) :sign-extend nil)
375       (xo21-30 :field ,(ppc-byte 21 30) :sign-extend nil)
376       (xo22-30 :field ,(ppc-byte 22 30) :sign-extend nil)
377       (xo26-30 :field ,(ppc-byte 26 30) :sign-extend nil)))
378
379
380   
381 (sb!disassem:define-instruction-format (instr 32)
382   (op :field (byte 6 26))
383   (other :field (byte 26 0)))
384
385 (sb!disassem:define-instruction-format (xinstr 32 :default-printer '(:name :tab data))
386   (op-to-a :field (byte 16 16))
387   (data :field (byte 16 0)))
388
389 (sb!disassem:define-instruction-format (sc 32 :default-printer '(:name :tab rest))
390   (op :field (byte 6 26))
391   (rest :field (byte 26 0) :value 2))
392
393
394
395 (macrolet ((def-ppc-iformat ((name &optional default-printer) &rest specs)
396                (flet ((specname-field (specname) 
397                         (or (assoc specname *ppc-field-specs-alist*)
398                             (error "Unknown ppc instruction field spec ~s" specname))))
399                  (labels ((spec-field (spec)
400                             (if (atom spec)
401                                 (specname-field spec)
402                                 (cons (car spec)
403                                       (cdr (specname-field (cadr spec)))))))
404                    (collect ((field (list '(op :field (byte 6 26)))))
405                             (dolist (spec specs) 
406                               (field (spec-field spec)))
407                             `(sb!disassem:define-instruction-format (,name 32 ,@(if default-printer `(:default-printer ,default-printer)))
408                               ,@(field)))))))
409
410 (def-ppc-iformat (i '(:name :tab li)) 
411   li aa lk)
412
413 (def-ppc-iformat (i-abs '(:name :tab li-abs)) 
414   li-abs aa lk)
415
416 (def-ppc-iformat (b '(:name :tab bo "," bi "," bd)) 
417   bo bi bd aa lk)
418
419 (def-ppc-iformat (d '(:name :tab rt "," d "(" ra ")"))
420   rt ra d)
421
422 (def-ppc-iformat (d-si '(:name :tab rt "," ra "," si ))
423   rt ra si)
424
425 (def-ppc-iformat (d-rs '(:name :tab rs "," d "(" ra ")"))
426   rs ra d)
427
428 (def-ppc-iformat (d-rs-ui '(:name :tab ra "," rs "," ui))
429   rs ra ui)
430
431 (def-ppc-iformat (d-crf-si)
432   bf l ra si)
433
434 (def-ppc-iformat (d-crf-ui)
435   bf l ra ui)
436
437 (def-ppc-iformat (d-to '(:name :tab to "," ra "," si))
438   to ra rb si)
439
440 (def-ppc-iformat (d-frt '(:name :tab frt "," d "(" ra ")"))
441   frt ra d)
442
443 (def-ppc-iformat (d-frs '(:name :tab frs "," d "(" ra ")"))
444   frs ra d)
445             
446
447 \f
448 ;;; There are around ... oh, 28 or so ... variants on the "X" format.
449 ;;;  Some of them are only used by one instruction; some are used by dozens.
450 ;;;  Some aren't used by instructions that we generate ...
451
452 (def-ppc-iformat (x '(:name :tab rt "," ra "," rb))
453   rt ra rb (xo xo21-30))
454
455 (def-ppc-iformat (x-1 '(:name :tab rt "," ra "," nb))
456   rt ra nb (xo xo21-30))
457
458 (def-ppc-iformat (x-4 '(:name :tab rt))
459   rt (xo xo21-30))
460
461 (def-ppc-iformat (x-5 '(:name :tab ra "," rs "," rb))
462   rs ra rb (xo xo21-30) rc)
463
464 (def-ppc-iformat (x-7 '(:name :tab ra "," rs "," rb))
465   rs ra rb (xo xo21-30))
466
467 (def-ppc-iformat (x-8 '(:name :tab ra "," rs "," nb))
468   rs ra nb (xo xo21-30))
469
470 (def-ppc-iformat (x-9 '(:name :tab ra "," rs "," sh))
471   rs ra sh (xo xo21-30) rc)
472
473 (def-ppc-iformat (x-10 '(:name :tab ra "," rs))
474   rs ra (xo xo21-30) rc)
475
476 (def-ppc-iformat (x-14 '(:name :tab bf "," l "," ra "," rb))
477   bf l ra rb (xo xo21-30))
478
479 (def-ppc-iformat (x-15 '(:name :tab bf "," l "," fra "," frb))
480   bf l fra frb (xo xo21-30))
481
482 (def-ppc-iformat (x-18 '(:name :tab bf))
483   bf (xo xo21-30))
484
485 (def-ppc-iformat (x-19 '(:name :tab to "," ra "," rb))
486   to ra rb (xo xo21-30))
487
488 (def-ppc-iformat (x-20 '(:name :tab frt "," ra "," rb))
489   frt ra rb (xo xo21-30))
490
491 (def-ppc-iformat (x-21 '(:name :tab frt "," rb))
492   frt rb (xo xo21-30) rc)
493
494 (def-ppc-iformat (x-22 '(:name :tab frt))
495   frt (xo xo21-30) rc)
496
497 (def-ppc-iformat (x-23 '(:name :tab ra "," frs "," rb))
498   frs ra rb (xo xo21-30))
499
500 (def-ppc-iformat (x-24 '(:name :tab bt))
501   bt (xo xo21-30) rc)
502
503 (def-ppc-iformat (x-25 '(:name :tab ra "," rb))
504   ra rb (xo xo21-30))
505
506 (def-ppc-iformat (x-26 '(:name :tab rb))
507   rb (xo xo21-30))
508
509 (def-ppc-iformat (x-27 '(:name))
510   (xo xo21-30))
511
512 \f
513 ;;;;
514
515 (def-ppc-iformat (xl '(:name :tab bt "," ba "," bb))
516   bt ba bb (xo xo21-30))
517
518 (def-ppc-iformat (xl-bo-bi '(:name :tab bo "," bi))
519   bo bi (xo xo21-30) lk)
520
521 (def-ppc-iformat (xl-cr '(:name :tab bf "," bfa))
522   bf bfa (xo xo21-30))
523
524 (def-ppc-iformat (xl-xo '(:name))
525   (xo xo21-30))
526
527 \f
528 ;;;;
529
530 (def-ppc-iformat (xfx)
531   rt spr (xo xo21-30))
532
533 (def-ppc-iformat (xfx-fxm '(:name :tab fxm "," rs))
534   rs fxm (xo xo21-30))
535
536 (def-ppc-iformat (xfl '(:name :tab flm "," frb))
537   flm frb (xo xo21-30) rc)
538
539 \f
540 ;;;
541
542 (def-ppc-iformat (xo '(:name :tab rt "," ra "," rb))
543   rt ra rb oe (xo xo22-30) rc)
544
545 (def-ppc-iformat (xo-oe '(:name :tab rt "," ra "," rb))
546   rt ra rb (xo xo22-30) rc)
547
548 (def-ppc-iformat (xo-a '(:name :tab rt "," ra))
549   rt ra oe (xo xo22-30) rc)
550
551 \f
552 ;;;
553
554 (def-ppc-iformat (a '(:name :tab frt "," fra "," frb "," frc))
555   frt fra frb frc (xo xo26-30) rc)
556
557 (def-ppc-iformat (a-tab '(:name :tab frt "," fra "," frb))
558   frt fra frb (xo xo26-30) rc)
559
560 (def-ppc-iformat (a-tac '(:name :tab frt "," fra "," frc))
561   frt fra frc (xo xo26-30) rc)
562
563 (def-ppc-iformat (a-tbc '(:name :tab frt "," frb "," frc))
564   frt frb frc (xo xo26-30) rc)
565 \f
566
567 (def-ppc-iformat (m '(:name :tab ra "," rs "," rb "," mb "," me))
568   rs ra rb mb me rc)
569
570 (def-ppc-iformat (m-sh '(:name :tab ra "," rs "," sh "," mb "," me))
571   rs ra sh mb me rc)))
572
573
574
575 \f
576 ;;;; Primitive emitters.
577
578
579 (define-bitfield-emitter emit-word 32
580   (byte 32 0))
581
582 (define-bitfield-emitter emit-short 16
583   (byte 16 0))
584
585 (define-bitfield-emitter emit-i-form-inst 32
586   (byte 6 26) (byte 24 2) (byte 1 1) (byte 1 0))
587
588 (define-bitfield-emitter emit-b-form-inst 32
589   (byte 6 26) (byte 5 21) (byte 5 16) (byte 14 2) (byte 1 1) (byte 1 0))
590
591 (define-bitfield-emitter emit-sc-form-inst 32
592   (byte 6 26) (byte 26 0))
593
594 (define-bitfield-emitter emit-d-form-inst 32
595   (byte 6 26) (byte 5 21) (byte 5 16) (byte 16 0))
596
597 ; Also used for XL-form.  What's the difference ?
598 (define-bitfield-emitter emit-x-form-inst 32
599   (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 10 1) (byte 1 0))
600
601 (define-bitfield-emitter emit-xfx-form-inst 32
602   (byte 6 26) (byte 5 21) (byte 10 11) (byte 10 1) (byte 1 0))
603
604 (define-bitfield-emitter emit-xfl-form-inst 32
605   (byte 6 26) (byte 10  16) (byte 5 11) (byte 10 1) (byte 1 0))
606
607 ; XS is 64-bit only
608 (define-bitfield-emitter emit-xo-form-inst 32
609   (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 1 10) (byte 9 1) (byte 1 0))
610
611 (define-bitfield-emitter emit-a-form-inst 32
612   (byte 6 26) (byte 5 21) (byte 5 16) (byte 5 11) (byte 5 6) (byte 5 1) (byte 1 0))
613
614
615 \f
616
617 (defun unimp-control (chunk inst stream dstate)
618   (declare (ignore inst))
619   (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
620     (case (xinstr-data chunk dstate)
621       (#.sb!vm:error-trap
622        (nt "Error trap")
623        (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
624       (#.sb!vm:cerror-trap
625        (nt "Cerror trap")
626        (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
627       (#.sb!vm:object-not-list-trap
628        (nt "Object not list trap"))
629       (#.sb!vm:breakpoint-trap
630        (nt "Breakpoint trap"))
631       (#.sb!vm:pending-interrupt-trap
632        (nt "Pending interrupt trap"))
633       (#.sb!vm:halt-trap
634        (nt "Halt trap"))
635       (#.sb!vm:fun-end-breakpoint-trap
636        (nt "Function end breakpoint trap"))
637       (#.sb!vm:object-not-instance-trap
638        (nt "Object not instance trap"))
639     )))
640
641 (eval-when (:compile-toplevel :execute)
642
643 (defun classify-dependencies (deplist)
644   (collect ((reads) (writes))
645     (dolist (dep deplist)
646       (ecase (car dep)
647         (reads (reads dep))
648         (writes (writes dep))))
649     (values (reads) (writes)))))
650
651 (macrolet ((define-xo-instruction
652                (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
653                `(define-instruction ,name (segment rt ra rb)
654                  (:printer xo ((op ,op ) (xo ,xo) (oe ,(if oe-p 1 0)) (rc ,(if rc-p 1 0))))
655                  (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer))) 
656                   (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if (or oe-p always-writes-xer) '((writes :xer))) )
657                  (:cost ,cost)
658                  (:delay ,cost)
659                  (:emitter
660                   (emit-xo-form-inst segment ,op
661                    (reg-tn-encoding rt) 
662                    (reg-tn-encoding ra) 
663                    (reg-tn-encoding rb)
664                    ,(if oe-p 1 0)
665                    ,xo
666                    ,(if rc-p 1 0)))))
667            (define-xo-oe-instruction
668                (name op xo rc-p always-reads-xer always-writes-xer cost)
669                `(define-instruction ,name (segment rt ra rb)
670                  (:printer xo-oe ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
671                  (:dependencies (reads ra) (reads rb) ,@(if always-reads-xer '((reads :xer))) 
672                   (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))))
673                  (:cost ,cost)
674                  (:delay ,cost)
675                  (:emitter
676                   (emit-xo-form-inst segment ,op
677                    (reg-tn-encoding rt) 
678                    (reg-tn-encoding ra) 
679                    (reg-tn-encoding rb)
680                    0
681                    ,xo
682                    (if ,rc-p 1 0)))))
683            (define-4-xo-instructions
684                (base op xo &key always-reads-xer always-writes-xer (cost 1))
685                `(progn
686                  (define-xo-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
687                  (define-xo-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
688                  (define-xo-instruction ,(symbolicate base "O") ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
689                  (define-xo-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
690
691            (define-2-xo-oe-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
692                `(progn
693                  (define-xo-oe-instruction ,base ,op ,xo nil ,always-reads-xer ,always-writes-xer ,cost)
694                  (define-xo-oe-instruction ,(symbolicate base ".") ,op ,xo t ,always-reads-xer ,always-writes-xer ,cost)))
695            
696            (define-xo-a-instruction (name op xo oe-p rc-p always-reads-xer always-writes-xer cost)
697                `(define-instruction ,name (segment rt ra)
698                  (:printer xo-a ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0)) (oe ,(if oe-p 1 0))))
699                  (:dependencies (reads ra) ,@(if always-reads-xer '((reads :xer)))
700                   (writes rt) ,@(if rc-p '((writes :ccr))) ,@(if always-writes-xer '((writes :xer))) )
701                  (:cost ,cost)
702                  (:delay ,cost)
703                  (:emitter
704                   (emit-xo-form-inst segment ,op
705                    (reg-tn-encoding rt) 
706                    (reg-tn-encoding ra) 
707                    0
708                    (if ,oe-p 1 0)
709                    ,xo
710                    (if ,rc-p 1 0)))))
711            
712            (define-4-xo-a-instructions (base op xo &key always-reads-xer always-writes-xer (cost 1))
713                `(progn
714                  (define-xo-a-instruction ,base ,op ,xo nil nil ,always-reads-xer ,always-writes-xer ,cost)
715                  (define-xo-a-instruction ,(symbolicate base ".") ,op ,xo nil t ,always-reads-xer ,always-writes-xer ,cost)
716                  (define-xo-a-instruction ,(symbolicate base "O")  ,op ,xo t nil ,always-reads-xer ,always-writes-xer ,cost)
717                  (define-xo-a-instruction ,(symbolicate base "O.") ,op ,xo t t ,always-reads-xer ,always-writes-xer ,cost)))
718            
719            (define-x-instruction (name op xo &key (cost 2) other-dependencies)
720                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
721                  `(define-instruction ,name (segment rt ra rb)
722                    (:printer x ((op ,op) (xo ,xo)))
723                    (:delay ,cost)
724                    (:cost ,cost)
725                    (:dependencies (reads ra) (reads rb) ,@ other-reads 
726                     (writes rt) ,@other-writes)
727                    (:emitter
728                     (emit-x-form-inst segment ,op 
729                      (reg-tn-encoding rt) 
730                      (reg-tn-encoding ra)
731                      (reg-tn-encoding rb)
732                      ,xo
733                      0)))))
734
735            (define-x-20-instruction (name op xo &key (cost 2) other-dependencies)
736                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
737                  `(define-instruction ,name (segment frt ra rb)
738                    (:printer x-20 ((op ,op) (xo ,xo)))
739                    (:delay ,cost)
740                    (:cost ,cost)
741                    (:dependencies (reads ra) (reads rb) ,@other-reads 
742                     (writes frt) ,@other-writes)
743                    (:emitter
744                     (emit-x-form-inst segment ,op 
745                      (fp-reg-tn-encoding frt) 
746                      (reg-tn-encoding ra)
747                      (reg-tn-encoding rb)
748                      ,xo
749                      0)))))
750            
751            (define-x-5-instruction (name op xo rc-p &key (cost 1) other-dependencies)
752                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
753                  `(define-instruction ,name (segment ra rs rb)
754                    (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
755                    (:delay ,cost)
756                    (:cost ,cost)
757                    (:dependencies (reads rb) (reads rs) ,@other-reads 
758                     (writes ra) ,@other-writes)
759                    (:emitter
760                     (emit-x-form-inst segment ,op 
761                      (reg-tn-encoding rs) 
762                      (reg-tn-encoding ra)
763                      (reg-tn-encoding rb)
764                      ,xo
765                      ,(if rc-p 1 0))))))
766
767
768            (define-x-5-st-instruction (name op xo rc-p &key (cost 1) other-dependencies)
769                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
770                  `(define-instruction ,name (segment rs ra rb)
771                    (:printer x-5 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
772                    (:delay ,cost)
773                    (:cost ,cost)
774                    (:dependencies (reads ra) (reads rb) (reads rs) ,@other-reads 
775                     ,@other-writes)
776                    (:emitter
777                     (emit-x-form-inst segment ,op 
778                      (reg-tn-encoding rs) 
779                      (reg-tn-encoding ra)
780                      (reg-tn-encoding rb)
781                      ,xo
782                      ,(if rc-p 1 0))))))
783            
784            (define-x-23-st-instruction (name op xo &key (cost 1) other-dependencies)
785                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
786                  `(define-instruction ,name (segment frs ra rb)
787                    (:printer x-23 ((op ,op) (xo ,xo)))
788                    (:delay ,cost)
789                    (:cost ,cost)
790                    (:dependencies (reads ra) (reads rb) (reads frs) ,@other-reads 
791                     ,@other-writes)
792                    (:emitter
793                     (emit-x-form-inst segment ,op 
794                      (fp-reg-tn-encoding frs) 
795                      (reg-tn-encoding ra)
796                      (reg-tn-encoding rb)
797                      ,xo
798                      0)))))
799
800            (define-x-10-instruction (name op xo rc-p &key (cost 1) other-dependencies)
801                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
802                  `(define-instruction ,name (segment ra rs)
803                    (:printer x-10 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
804                    (:delay ,cost)
805                    (:cost ,cost)
806                    (:dependencies (reads rs) ,@other-reads 
807                     (writes ra) ,@other-writes)
808                    (:emitter
809                     (emit-x-form-inst segment ,op 
810                      (reg-tn-encoding rs) 
811                      (reg-tn-encoding ra)
812                      0
813                      ,xo
814                      ,(if rc-p 1 0))))))
815
816            (define-2-x-5-instructions (name op xo &key (cost 1) other-dependencies)
817                `(progn
818                  (define-x-5-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
819                  (define-x-5-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost 
820                                          :other-dependencies ,other-dependencies)))
821            
822            (define-2-x-10-instructions (name op xo &key (cost 1) other-dependencies)
823                `(progn
824                  (define-x-10-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
825                  (define-x-10-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost 
826                                           :other-dependencies ,other-dependencies)))
827            
828            
829            (define-x-21-instruction (name op xo rc-p &key (cost 4) other-dependencies)
830                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
831                  `(define-instruction ,name (segment frt frb)
832                    (:printer x-21 ((op ,op) (xo ,xo) (rc ,(if rc-p 1 0))))
833                    (:cost ,cost)
834                    (:delay ,cost)
835                    (:dependencies (reads frb) ,@other-reads 
836                     (writes frt) ,@other-writes)
837                    (:emitter
838                     (emit-x-form-inst segment ,op
839                      (fp-reg-tn-encoding frt)
840                      0
841                      (fp-reg-tn-encoding frb)
842                      ,xo
843                      ,(if rc-p 1 0))))))
844            
845            (define-2-x-21-instructions (name op xo &key (cost 4) other-dependencies)
846                `(progn
847                  (define-x-21-instruction ,name ,op ,xo nil :cost ,cost :other-dependencies ,other-dependencies)
848                  (define-x-21-instruction ,(symbolicate name ".") ,op ,xo t :cost ,cost 
849                                           :other-dependencies ,other-dependencies)))
850            
851
852            (define-d-si-instruction (name op &key (fixup nil) (cost 1) other-dependencies)
853                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
854                  `(define-instruction ,name (segment rt ra si)
855                    (:declare (type (signed-byte 16)))
856                    (:printer d-si ((op ,op)))
857                    (:delay ,cost)
858                    (:cost ,cost)
859                    (:dependencies (reads ra) ,@other-reads 
860                     (writes rt) ,@other-writes)
861                    (:emitter
862                     (when (typep si 'fixup)
863                       (ecase ,fixup
864                         ((:ha :l) (note-fixup segment ,fixup si)))
865                       (setq si 0))      
866                     (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
867            
868            (define-d-rs-ui-instruction (name op &key (cost 1) other-dependencies)
869                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
870                  `(define-instruction ,name (segment ra rs ui)
871                    (:declare (type (unsigned-byte 16) ui))
872                    (:printer d-rs-ui ((op ,op)))
873                    (:cost ,cost)
874                    (:delay ,cost)
875                    (:dependencies (reads rs) ,@other-reads 
876                     (writes ra) ,@other-writes)
877                    (:emitter
878                     (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) ui)))))
879            
880            (define-d-instruction (name op &key (cost 2) other-dependencies pinned)
881                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
882                  `(define-instruction ,name (segment rt ra si)
883                    (:declare (type (signed-byte 16) si))
884                    (:printer d ((op ,op)))
885                    (:delay ,cost)
886                    (:cost ,cost)
887                    ,@(when pinned '(:pinned))
888                    (:dependencies (reads ra) ,@other-reads 
889                     (writes rt) ,@other-writes)
890                    (:emitter
891                     (emit-d-form-inst segment ,op (reg-tn-encoding rt) (reg-tn-encoding ra) si)))))
892            
893            (define-d-frt-instruction (name op &key (cost 3) other-dependencies)
894                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
895                  `(define-instruction ,name (segment frt ra si)
896                    (:declare (type (signed-byte 16) si))
897                    (:printer d-frt ((op ,op)))
898                    (:delay ,cost)
899                    (:cost ,cost)
900                    (:dependencies (reads ra) ,@other-reads 
901                     (writes frt) ,@other-writes)
902                    (:emitter
903                     (emit-d-form-inst segment ,op (fp-reg-tn-encoding frt) (reg-tn-encoding ra) si)))))
904
905            (define-d-rs-instruction (name op &key (cost 1) other-dependencies pinned)
906                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
907                  `(define-instruction ,name (segment rs ra si)
908                    (:declare (type (signed-byte 16) si))
909                    (:printer d-rs ((op ,op)))
910                    (:delay ,cost)
911                    (:cost ,cost)
912                    ,@(when pinned '(:pinned))
913                    (:dependencies (reads rs) (reads ra) ,@other-reads 
914                     (writes :memory :partially t) ,@other-writes)
915                    (:emitter
916                     (emit-d-form-inst segment ,op (reg-tn-encoding rs) (reg-tn-encoding ra) si)))))
917
918            (define-d-frs-instruction (name op &key (cost 1) other-dependencies)
919                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
920                  `(define-instruction ,name (segment frs ra si)
921                    (:declare (type (signed-byte 16) si))
922                    (:printer d-frs ((op ,op)))
923                    (:delay ,cost)
924                    (:cost ,cost)
925                    (:dependencies (reads frs) (reads ra) ,@other-reads 
926                     (writes :memory :partially t) ,@other-writes)
927                    (:emitter
928                     (emit-d-form-inst segment ,op (fp-reg-tn-encoding frs) (reg-tn-encoding ra) si)))))
929
930            (define-a-instruction (name op xo rc &key (cost 1) other-dependencies)
931                `(define-instruction ,name (segment frt fra frb frc)
932                  (:printer a ((op ,op) (xo ,xo) (rc ,rc)))
933                  (:cost ,cost)
934                  (:delay ,cost)
935                  (:dependencies (writes frt) (reads fra) (reads frb) (reads frc) ,@other-dependencies)
936                  (:emitter
937                   (emit-a-form-inst segment 
938                    ,op 
939                    (fp-reg-tn-encoding frt) 
940                    (fp-reg-tn-encoding fra) 
941                    (fp-reg-tn-encoding frb)
942                    (fp-reg-tn-encoding frb)
943                    ,xo
944                    ,rc))))
945            
946            (define-2-a-instructions (name op xo &key (cost 1) other-dependencies)
947                `(progn
948                  (define-a-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
949                  (define-a-instruction ,(symbolicate name ".")
950                      ,op ,xo 1  :cost ,cost :other-dependencies ,other-dependencies)))
951            
952            (define-a-tab-instruction (name op xo rc &key (cost 1) other-dependencies)
953                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
954                  `(define-instruction ,name (segment frt fra frb)
955                    (:printer a-tab ((op ,op) (xo ,xo) (rc ,rc)))
956                    (:cost ,cost)
957                    (:delay 1)
958                    (:dependencies (reads fra) (reads frb) ,@other-reads
959                     (writes frt) ,@other-writes)
960                    (:emitter
961                     (emit-a-form-inst segment 
962                      ,op 
963                      (fp-reg-tn-encoding frt) 
964                      (fp-reg-tn-encoding fra) 
965                      (fp-reg-tn-encoding frb)
966                      0
967                      ,xo
968                      ,rc)))))
969            
970            (define-2-a-tab-instructions (name op xo &key (cost 1) other-dependencies)
971                `(progn
972                  (define-a-tab-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
973                  (define-a-tab-instruction ,(symbolicate name ".")
974                      ,op ,xo 1  :cost ,cost :other-dependencies ,other-dependencies)))
975            
976            (define-a-tac-instruction (name op xo rc &key (cost 1) other-dependencies)
977                (multiple-value-bind (other-reads other-writes) (classify-dependencies other-dependencies)
978                  `(define-instruction ,name (segment frt fra frc)
979                    (:printer a-tac ((op ,op) (xo ,xo) (rc ,rc)))
980                    (:cost ,cost)
981                    (:delay 1)
982                    (:dependencies (reads fra) (reads frb) ,@other-reads
983                     (writes frt) ,@other-writes)
984                    (:emitter
985                     (emit-a-form-inst segment 
986                      ,op 
987                      (fp-reg-tn-encoding frt) 
988                      (fp-reg-tn-encoding fra) 
989                      0
990                      (fp-reg-tn-encoding frc)
991                      ,xo
992                      ,rc)))))
993            
994            (define-2-a-tac-instructions (name op xo &key (cost 1) other-dependencies)
995                `(progn
996                  (define-a-tac-instruction ,name ,op ,xo 0 :cost ,cost :other-dependencies ,other-dependencies)
997                  (define-a-tac-instruction ,(symbolicate name ".")
998                      ,op ,xo 1  :cost ,cost :other-dependencies ,other-dependencies)))
999            
1000            (define-crbit-instruction (name op xo)
1001                `(define-instruction ,name (segment dbit abit bbit)
1002                  (:printer xl ((op ,op ) (xo ,xo)))
1003                  (:delay 1)
1004                  (:cost 1)
1005                  (:dependencies (reads :ccr) (writes :ccr))
1006                  (:emitter (emit-x-form-inst segment 19
1007                             (valid-bi-encoding dbit)
1008                             (valid-bi-encoding abit)
1009                             (valid-bi-encoding bbit)
1010                             ,xo
1011                             0)))))
1012   
1013    ;;; The instructions, in numerical order
1014
1015   (define-instruction unimp (segment data)
1016     (:declare (type (signed-byte 16) data))
1017     (:printer xinstr ((op-to-a #.(logior (ash 3 10) (ash 6 5) 0)))
1018               :default :control #'unimp-control)
1019     :pinned
1020     (:delay 0)
1021     (:emitter (emit-d-form-inst segment 3 6 0 data)))
1022
1023   (define-instruction twi (segment tcond ra si)
1024     (:printer d-to ((op 3)))
1025     (:delay 1)
1026     :pinned
1027     (:emitter (emit-d-form-inst segment 3 (valid-tcond-encoding tcond) (reg-tn-encoding ra) si)))
1028   
1029   (define-d-si-instruction mulli 7 :cost 5)
1030   (define-d-si-instruction subfic 8)
1031   
1032   (define-instruction cmplwi (segment crf ra &optional (ui nil ui-p))
1033     (:printer d-crf-ui ((op 10) (l 0)) '(:name :tab bf "," ra "," ui))
1034     (:dependencies (if ui-p (reads ra) (reads crf)) (writes :ccr))
1035     (:delay 1)
1036     (:emitter 
1037      (unless ui-p
1038        (setq ui ra ra crf crf :cr0))
1039      (emit-d-form-inst segment 
1040                        10
1041                        (valid-cr-field-encoding crf) 
1042                        (reg-tn-encoding ra)
1043                        ui)))
1044   
1045   (define-instruction cmpwi (segment crf ra  &optional (si nil si-p))
1046     (:printer d-crf-si ((op 11) (l 0)) '(:name :tab bf "," ra "," si))
1047     (:dependencies (if si-p (reads ra) (reads crf)) (writes :ccr))
1048     (:delay 1)
1049     (:emitter 
1050      (unless si-p
1051        (setq si ra ra crf crf :cr0))
1052      (emit-d-form-inst segment 
1053                        11
1054                        (valid-cr-field-encoding crf) 
1055                        (reg-tn-encoding ra)
1056                        si)))
1057   
1058   (define-d-si-instruction addic 12 :other-dependencies ((writes :xer)))
1059   (define-d-si-instruction addic. 13 :other-dependencies ((writes :xer) (writes :ccr)))
1060   
1061   (define-d-si-instruction addi 14 :fixup :l)
1062   (define-d-si-instruction addis 15 :fixup :ha)
1063   
1064   ;; There's no real support here for branch options that decrement
1065   ;; and test the CTR :
1066   ;; (a) the instruction scheduler doesn't know that anything's happening 
1067   ;;    to the CTR
1068   ;; (b) Lisp may have to assume that the CTR always has a lisp 
1069   ;;    object/locative in it.
1070   
1071   (define-instruction bc (segment bo bi target)
1072     (:declare (type label target))
1073     (:printer b ((op 16) (aa 0) (lk 0)))
1074     (:delay 1)
1075     (:dependencies (reads :ccr))
1076     (:emitter
1077      (emit-conditional-branch segment bo bi target)))
1078   
1079   (define-instruction bcl (segment bo bi target)
1080     (:declare (type label target))
1081     (:printer b ((op 16) (aa 0) (lk 1)))
1082     (:delay 1)
1083     (:dependencies (reads :ccr))
1084     (:emitter
1085      (emit-conditional-branch segment bo bi target nil t)))
1086   
1087   (define-instruction bca (segment bo bi target)
1088     (:declare (type label target))
1089     (:printer b ((op 16) (aa 1) (lk 0)))
1090     (:delay 1)
1091     (:dependencies (reads :ccr))
1092     (:emitter
1093      (emit-conditional-branch segment bo bi target t)))
1094   
1095   (define-instruction bcla (segment bo bi target)
1096     (:declare (type label target))
1097     (:printer b ((op 16) (aa 1) (lk 1)))
1098     (:delay 1)
1099     (:dependencies (reads :ccr))
1100     (:emitter
1101      (emit-conditional-branch segment bo bi target t t)))
1102   
1103 ;;; There may (or may not) be a good reason to use this in preference to "b[la] target".
1104 ;;; I can't think of a -bad- reason ...
1105   
1106   (define-instruction bu (segment target)
1107     (:declare (type label target))
1108     (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (aa 0) (lk 0)) 
1109               '(:name :tab bd))
1110     (:delay 1)
1111     (:emitter
1112      (emit-conditional-branch segment #.(valid-bo-encoding :bo-u) 0 target nil nil)))
1113   
1114   
1115   (define-instruction bt (segment bi  target)
1116     (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-t)) (aa 0) (lk 0))
1117               '(:name :tab bi "," bd))
1118     (:delay 1)
1119     (:emitter
1120      (emit-conditional-branch segment #.(valid-bo-encoding :bo-t) bi target nil nil)))
1121   
1122   (define-instruction bf (segment bi  target)
1123     (:printer b ((op 16) (bo #.(valid-bo-encoding :bo-f)) (aa 0) (lk 0))
1124               '(:name :tab bi "," bd))
1125     (:delay 1)
1126     (:emitter
1127      (emit-conditional-branch segment #.(valid-bo-encoding :bo-f) bi target nil nil)))
1128   
1129   (define-instruction b? (segment cr-field-name cr-name  &optional (target nil target-p))
1130     (:delay 1)
1131     (:emitter 
1132      (unless target-p
1133        (setq target cr-name cr-name cr-field-name cr-field-name :cr0))
1134      (let*  ((+cond (position cr-name cr-bit-names))
1135              (-cond (position cr-name cr-bit-inverse-names))
1136              (b0 (if +cond :bo-t 
1137                      (if -cond 
1138                          :bo-f
1139                          (error "Unknown branch condition ~s" cr-name))))
1140              (cr-form (list cr-field-name (if +cond cr-name (svref cr-bit-names -cond)))))
1141        (emit-conditional-branch segment b0 cr-form target))))
1142   
1143   (define-instruction sc (segment)
1144     (:printer sc ((op 17)))
1145     (:delay 1)
1146     :pinned
1147     (:emitter (emit-sc-form-inst segment 17 2)))
1148
1149   (define-instruction b (segment target)
1150     (:printer i ((op 18) (aa 0) (lk 0)))
1151     (:delay 1)
1152     (:emitter
1153      (emit-i-form-branch segment target nil)))
1154   
1155   (define-instruction ba (segment target)
1156     (:printer i-abs ((op 18) (aa 1) (lk 0)))
1157     (:delay 1)
1158     (:emitter
1159      (when (typep target 'fixup)
1160        (note-fixup segment :ba target)
1161        (setq target 0))
1162      (emit-i-form-inst segment 18 (ash target -2) 1 0)))
1163   
1164   
1165   (define-instruction bl (segment target)
1166     (:printer i ((op 18) (aa 0) (lk 1)))
1167     (:delay 1)
1168     (:emitter
1169      (emit-i-form-branch segment target t)))
1170   
1171   (define-instruction bla (segment target)
1172     (:printer i-abs ((op 18) (aa 1) (lk 1)))
1173     (:delay 1)
1174     (:emitter
1175      (when (typep target 'fixup)
1176        (note-fixup segment :ba target)
1177        (setq target 0))
1178      (emit-i-form-inst segment 18 (ash target -2) 1 1)))
1179   
1180   (define-instruction blr (segment)
1181     (:printer xl-bo-bi ((op 19) (xo 16) (bo #.(valid-bo-encoding :bo-u))(bi 0) (lk 0))  '(:name))
1182     (:delay 1)
1183     (:dependencies (reads :ccr) (reads :ctr))
1184     (:emitter
1185      (emit-x-form-inst segment 19 (valid-bo-encoding :bo-u) 0 0 16 0)))
1186   
1187   (define-instruction bclr (segment bo bi)
1188     (:printer xl-bo-bi ((op 19) (xo 16)))
1189     (:delay 1)
1190     (:dependencies (reads :ccr) (reads :lr))
1191     (:emitter
1192      (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 16 0)))
1193   
1194   (define-instruction bclrl (segment bo bi)
1195     (:printer xl-bo-bi ((op 19) (xo 16) (lk 1)))
1196     (:delay 1)
1197     (:dependencies (reads :ccr) (reads :lr))
1198     (:emitter
1199      (emit-x-form-inst segment 19 (valid-bo-encoding bo)
1200                        (valid-bi-encoding bi) 0 16 1)))
1201   
1202   (define-crbit-instruction crnor 19 33)
1203   (define-crbit-instruction crandc 19 129)
1204   (define-instruction isync (segment)
1205     (:printer xl-xo ((op 19) (xo 150)))
1206     (:delay 1)
1207     :pinned
1208     (:emitter (emit-x-form-inst segment 19 0 0 0 150 0)))
1209   
1210   (define-crbit-instruction crxor 19 193)
1211   (define-crbit-instruction crnand 19 225)
1212   (define-crbit-instruction crand 19 257)
1213   (define-crbit-instruction creqv 19 289)
1214   (define-crbit-instruction crorc 19 417)
1215   (define-crbit-instruction cror 19 449)
1216   
1217   (define-instruction bcctr (segment bo bi)
1218     (:printer xl-bo-bi ((op 19) (xo 528)))
1219     (:delay 1)
1220     (:dependencies (reads :ccr) (reads :ctr))
1221     (:emitter
1222      (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 0)))
1223   
1224   (define-instruction bcctrl (segment bo bi)
1225     (:printer xl-bo-bi ((op 19) (xo 528) (lk 1)))
1226     (:delay 1)
1227     (:dependencies (reads :ccr) (reads :ctr) (writes :lr))
1228     (:emitter
1229      (emit-x-form-inst segment 19 (valid-bo-encoding bo) (valid-bi-encoding bi) 0 528 1)))
1230   
1231   (define-instruction bctr (segment)
1232     (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 0))  '(:name))
1233     (:delay 1)
1234     (:dependencies (reads :ccr) (reads :ctr))
1235     (:emitter
1236      (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0  528 0)))
1237   
1238   (define-instruction bctrl (segment)
1239     (:printer xl-bo-bi ((op 19) (xo 528) (bo #.(valid-bo-encoding :bo-u)) (bi 0) (lk 1))  '(:name))
1240     (:delay 1)
1241     (:dependencies (reads :ccr) (reads :ctr))
1242     (:emitter
1243      (emit-x-form-inst segment 19 #.(valid-bo-encoding :bo-u) 0 0  528 1)))
1244   
1245   (define-instruction rlwimi (segment ra rs sh mb me)
1246     (:printer m-sh ((op 20) (rc 0)))
1247     (:dependencies (reads rs) (writes ra))
1248     (:delay 1)
1249     (:emitter
1250      (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1251   
1252   (define-instruction rlwimi. (segment ra rs sh mb me)
1253     (:printer m-sh ((op 20) (rc 1)))
1254     (:dependencies (reads rs) (writes ra) (writes :ccr))
1255     (:delay 1)
1256     (:emitter
1257      (emit-a-form-inst segment 20 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1258   
1259   (define-instruction rlwinm (segment ra rs sh mb me)
1260     (:printer m-sh ((op 21) (rc 0)))
1261     (:delay 1)
1262     (:dependencies (reads rs) (writes ra))
1263     (:emitter
1264      (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 0)))
1265   
1266   (define-instruction rlwinm. (segment ra rs sh mb me)
1267     (:printer m-sh ((op 21) (rc 1)))
1268     (:delay 1)
1269     (:dependencies (reads rs) (writes ra) (writes :ccr))
1270     (:emitter
1271      (emit-a-form-inst segment 21 (reg-tn-encoding rs) (reg-tn-encoding ra) sh mb me 1)))
1272
1273   (define-instruction rlwnm (segment ra rs rb mb me)
1274     (:printer m ((op 23) (rc 0) (rb nil :type 'reg)))
1275     (:delay 1)
1276     (:dependencies (reads rs) (writes ra) (reads rb))
1277     (:emitter
1278      (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 0)))
1279   
1280   (define-instruction rlwnm. (segment ra rs rb mb me)
1281     (:printer m ((op 23) (rc 1) (rb nil :type 'reg)))
1282     (:delay 1)
1283     (:dependencies (reads rs) (reads rb) (writes ra) (writes :ccr))
1284     (:emitter
1285      (emit-a-form-inst segment 23 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) mb me 1)))
1286   
1287   
1288   (define-d-rs-ui-instruction ori 24)
1289   
1290   (define-instruction nop (segment)
1291     (:printer d-rs-ui ((op 24) (rs 0) (ra 0) (ui 0)) '(:name))
1292     (:cost 1)
1293     (:delay 1)
1294     (:emitter
1295      (emit-d-form-inst segment 24 0 0 0)))
1296   
1297   (define-d-rs-ui-instruction oris 25)
1298   (define-d-rs-ui-instruction xori 26)
1299   (define-d-rs-ui-instruction xoris 27)
1300   (define-d-rs-ui-instruction andi. 28 :other-dependencies ((writes :ccr)))
1301   (define-d-rs-ui-instruction andis. 29 :other-dependencies ((writes :ccr)))
1302   
1303   (define-instruction cmpw (segment crf ra  &optional (rb nil rb-p))
1304     (:printer x-14 ((op 31) (xo 0) (l 0)) '(:name :tab bf "," ra "," rb))
1305     (:delay 1)
1306     (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1307     (:emitter 
1308      (unless rb-p
1309        (setq rb ra ra crf crf :cr0))
1310      (emit-x-form-inst segment 
1311                        31
1312                        (valid-cr-field-encoding crf) 
1313                        (reg-tn-encoding ra)
1314                        (reg-tn-encoding rb)
1315                        0
1316                        0)))
1317   
1318   (define-instruction tw (segment tcond ra rb)
1319     (:printer x-19 ((op 31) (xo 4)))
1320     (:delay 1)
1321     :pinned
1322     (:emitter (emit-x-form-inst segment 31 (valid-tcond-encoding tcond) (reg-tn-encoding ra) (reg-tn-encoding rb) 4 0)))
1323   
1324   (define-4-xo-instructions subfc 31 8 :always-writes-xer t)
1325   (define-4-xo-instructions addc 31 10 :always-writes-xer t)
1326   (define-2-xo-oe-instructions mulhwu 31 11 :cost 5)
1327   
1328   (define-instruction mfcr (segment rd)
1329     (:printer x-4 ((op 31) (xo 19)))
1330     (:delay 1)
1331     (:dependencies (reads :ccr) (writes rd))
1332     (:emitter (emit-x-form-inst segment 31 (reg-tn-encoding rd) 0 0 19 0)))
1333   
1334   (define-x-instruction lwarx 31 20)
1335   (define-x-instruction lwzx 31 23)
1336   (define-2-x-5-instructions slw 31 24)
1337   (define-2-x-10-instructions cntlzw 31 26)
1338   (define-2-x-5-instructions and 31 28)
1339   
1340   (define-instruction cmplw (segment crf ra  &optional (rb nil rb-p))
1341     (:printer x-14 ((op 31) (xo 32) (l 0)) '(:name :tab bf "," ra "," rb))
1342     (:delay 1)
1343     (:dependencies (reads ra) (if rb-p (reads rb) (reads crf)) (reads :xer) (writes :ccr))
1344     (:emitter 
1345      (unless rb-p
1346        (setq rb ra ra crf crf :cr0))
1347      (emit-x-form-inst segment 
1348                        31
1349                        (valid-cr-field-encoding crf) 
1350                        (reg-tn-encoding ra)
1351                        (reg-tn-encoding rb)
1352                        32
1353                        0)))
1354   
1355   
1356   (define-4-xo-instructions subf 31 40)
1357                                         ; dcbst
1358   (define-x-instruction lwzux 31 55 :other-dependencies ((writes rt)))
1359   (define-2-x-5-instructions andc 31 60)
1360   (define-2-xo-oe-instructions mulhw 31 75 :cost 5)
1361   
1362   (define-x-instruction lbzx 31 87)
1363   (define-4-xo-a-instructions neg 31 104)
1364   (define-x-instruction lbzux 31 119 :other-dependencies ((writes rt)))
1365   (define-2-x-5-instructions nor 31 124)
1366   (define-4-xo-instructions subfe 31 136 :always-reads-xer t :always-writes-xer t)
1367   
1368   (define-instruction-macro sube (rt ra rb)
1369     `(inst subfe ,rt ,rb ,ra))
1370   
1371   (define-instruction-macro sube. (rt ra rb)
1372     `(inst subfe. ,rt ,rb ,ra))
1373   
1374   (define-instruction-macro subeo (rt ra rb)
1375     `(inst subfeo ,rt ,rb ,ra))
1376   
1377   (define-instruction-macro subeo. (rt ra rb)
1378     `(inst subfeo ,rt ,rb ,ra))
1379   
1380   (define-4-xo-instructions adde 31 138 :always-reads-xer t :always-writes-xer t)
1381   
1382   (define-instruction mtcrf (segment mask rt)
1383     (:printer xfx-fxm ((op 31) (xo 144)))
1384     (:delay 1)
1385     (:dependencies (reads rt) (writes :ccr))
1386     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash mask 1) 144 0)))
1387   
1388   (define-x-5-st-instruction stwcx. 31 150 t :other-dependencies ((writes :ccr)))
1389   (define-x-5-st-instruction stwx 31 151 nil)
1390   (define-x-5-st-instruction stwux 31 183 nil :other-dependencies ((writes ra)))
1391   (define-4-xo-a-instructions subfze 31 200 :always-reads-xer t :always-writes-xer t)
1392   (define-4-xo-a-instructions addze 31 202 :always-reads-xer t :always-writes-xer t)
1393   (define-x-5-st-instruction stbx 31 215 nil)
1394   (define-4-xo-a-instructions subfme 31 232 :always-reads-xer t :always-writes-xer t)
1395   (define-4-xo-a-instructions addme 31 234 :always-reads-xer t :always-writes-xer t)
1396   (define-4-xo-instructions mullw 31 235 :cost 5)
1397   (define-x-5-st-instruction stbux 31 247 nil :other-dependencies ((writes ra)))
1398   (define-4-xo-instructions add 31 266)
1399   (define-x-instruction lhzx 31 279)
1400   (define-2-x-5-instructions eqv 31 284)
1401   (define-x-instruction lhzux 31 311 :other-dependencies ((writes ra)))
1402   (define-2-x-5-instructions xor 31 316)
1403   
1404   (define-instruction mfmq (segment rt)
1405     (:printer xfx ((op 31) (xo 339) (spr 0)) '(:name :tab rt))
1406     (:delay 1)
1407     (:dependencies (reads :xer) (writes rt))
1408     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 339 0)))
1409   
1410   (define-instruction mfxer (segment rt)
1411     (:printer xfx ((op 31) (xo 339) (spr 1)) '(:name :tab rt))
1412     (:delay 1)
1413     (:dependencies (reads :xer) (writes rt))
1414     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 339 0)))
1415   
1416   (define-instruction mflr (segment rt)
1417     (:printer xfx ((op 31) (xo 339) (spr 8)) '(:name :tab rt))
1418     (:delay 1)
1419     (:dependencies (reads :lr) (writes rt))
1420     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 339 0)))
1421   
1422   (define-instruction mfctr (segment rt)
1423     (:printer xfx ((op 31) (xo 339) (spr 9)) '(:name :tab rt))
1424     (:delay 1)
1425     (:dependencies (reads rt) (reads :ctr))
1426     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 339 0)))
1427   
1428   
1429   (define-x-instruction lhax 31 343)
1430   (define-x-instruction lhaux 31 375 :other-dependencies ((writes ra)))
1431   (define-x-5-st-instruction sthx 31 407 nil)
1432   (define-2-x-5-instructions orc 31 412)
1433   (define-x-5-st-instruction sthux 31 439 nil :other-dependencies ((writes ra)))
1434   
1435   (define-instruction or (segment ra rs rb)
1436     (:printer x-5 ((op 31) (xo 444) (rc 0)) '((:cond
1437                                                 ((rs :same-as rb) 'mr)
1438                                                 (t :name))
1439                                               :tab
1440                                               ra "," rs
1441                                               (:unless (:same-as rs) "," rb)))
1442     (:delay 1)
1443     (:cost 1)
1444     (:dependencies (reads rb) (reads rs) (writes ra))
1445     (:emitter
1446      (emit-x-form-inst segment
1447                        31
1448                        (reg-tn-encoding rs) 
1449                        (reg-tn-encoding ra)
1450                        (reg-tn-encoding rb)
1451                        444
1452                        0)))
1453   
1454   (define-instruction or. (segment ra rs rb)
1455     (:printer x-5 ((op 31) (xo 444) (rc 1)) '((:cond
1456                                                 ((rs :same-as rb) 'mr.)
1457                                                 (t :name))
1458                                               :tab
1459                                               ra "," rs
1460                                               (:unless (:same-as rs) "," rb)))
1461     (:delay 1)
1462     (:cost 1)
1463     (:dependencies (reads rb) (reads rs) (writes ra))
1464     (:emitter
1465      (emit-x-form-inst segment
1466                        31
1467                        (reg-tn-encoding rs) 
1468                        (reg-tn-encoding ra)
1469                        (reg-tn-encoding rb)
1470                        444
1471                        1)))
1472   
1473   (define-instruction-macro mr (ra rs)
1474     `(inst or ,ra ,rs ,rs))
1475   
1476   (define-instruction-macro mr. (ra rs)
1477     `(inst or. ,ra ,rs ,rs))
1478   
1479   (define-4-xo-instructions divwu 31 459 :cost 36)
1480   
1481                                         ; This is a 601-specific instruction class.
1482   (define-4-xo-instructions div 31 331 :cost 36)
1483   
1484                                         ; This is a 601-specific instruction.
1485   (define-instruction mtmq (segment rt)
1486     (:printer xfx ((op 31) (xo 467) (spr (ash 0 5))) '(:name :tab rt))
1487     (:delay 1)
1488     (:dependencies (reads rt) (writes :xer))
1489     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 0 5) 467 0)))
1490   
1491   (define-instruction mtxer (segment rt)
1492     (:printer xfx ((op 31) (xo 467) (spr (ash 1 5))) '(:name :tab rt))
1493     (:delay 1)
1494     (:dependencies (reads rt) (writes :xer))
1495     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 1 5) 467 0)))
1496   
1497   (define-instruction mtlr (segment rt)
1498     (:printer xfx ((op 31) (xo 467) (spr (ash 8 5))) '(:name :tab rt))
1499     (:delay 1)
1500     (:dependencies (reads rt) (writes :lr))
1501     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 8 5) 467 0)))
1502   
1503   (define-instruction mtctr (segment rt)
1504     (:printer xfx ((op 31) (xo 467) (spr (ash 9 5))) '(:name :tab rt))
1505     (:delay 1)
1506     (:dependencies (reads rt) (writes :ctr))
1507     (:emitter (emit-xfx-form-inst segment 31 (reg-tn-encoding rt) (ash 9 5) 467 0)))
1508   
1509   
1510   (define-2-x-5-instructions nand 31 476)
1511   (define-4-xo-instructions divw 31 491 :cost 36)
1512   (define-instruction mcrxr (segment crf)
1513     (:printer x-18 ((op 31) (xo 512)))
1514     (:delay 1)
1515     (:dependencies (reads :xer) (writes :ccr) (writes :xer))
1516     (:emitter (emit-x-form-inst segment 31 (valid-cr-field-encoding crf) 0 0 512 0)))
1517   
1518   (define-instruction lswx (segment rs ra rb) 
1519     (:printer x ((op 31) (xo 533) (rc 0)))
1520     (:delay 1)
1521     :pinned
1522     (:cost 8) 
1523     (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rs) (reg-tn-encoding ra) (reg-tn-encoding rb) 533 0)))
1524   (define-x-instruction lwbrx 31 534)
1525   (define-x-20-instruction lfsx 31 535)
1526   (define-2-x-5-instructions srw 31 536)
1527   (define-x-20-instruction lfsux 31 567 :other-dependencies ((writes ra)))
1528   
1529   (define-instruction lswi (segment rt ra rb) 
1530     (:printer x-1 ((op 31) (xo 597) (rc 0)))
1531     :pinned
1532     (:delay 8)
1533     (:cost 8) 
1534     (:emitter (emit-x-form-inst sb!assem:segment 31 (reg-tn-encoding rt) (reg-tn-encoding ra) rb 597 0)))
1535   
1536   (define-instruction sync (segment)
1537     (:printer x-27 ((op 31) (xo 598)))
1538     (:delay 1)
1539     :pinned
1540     (:emitter (emit-x-form-inst segment 31 0 0 0 598 0)))
1541   (define-x-20-instruction lfdx 31 599)
1542   (define-x-20-instruction lfdux 31 631 :other-dependencies ((writes ra)))
1543   (define-instruction stswx (segment rs ra rb) 
1544     (:printer x-5 ((op 31) (xo 661)))
1545     :pinned
1546     (:cost 8) 
1547     (:delay 1)
1548     (:emitter (emit-x-form-inst sb!assem:segment 31 
1549                                 (reg-tn-encoding rs) 
1550                                 (reg-tn-encoding ra) 
1551                                 (reg-tn-encoding rb) 
1552                                 661 
1553                                 0)))
1554   (define-x-5-st-instruction stwbrx 31 662 nil)
1555   (define-x-23-st-instruction stfsx 31 663)
1556   (define-x-23-st-instruction stfsux 31 695 :other-dependencies ((writes ra)))
1557   (define-instruction stswi (segment rs ra nb)
1558     (:printer x-8 ((op 31) (xo 725)))
1559     :pinned
1560     (:delay 1)
1561     (:emitter
1562      (emit-x-form-inst segment 31
1563                        (reg-tn-encoding rs) 
1564                        (reg-tn-encoding ra)
1565                        nb
1566                        725
1567                        0)))
1568   
1569   (define-x-23-st-instruction stfdx 31 727)
1570   (define-x-23-st-instruction stfdux 31 759 :other-dependencies ((writes ra)))
1571   (define-x-instruction lhbrx 31 790)
1572   (define-2-x-5-instructions sraw 31 792)
1573   
1574   (define-instruction srawi (segment ra rs rb)
1575     (:printer x-9 ((op 31) (xo 824) (rc 0)))
1576     (:cost 1)
1577     (:delay 1)
1578     (:dependencies (reads rs) (writes ra))
1579     (:emitter
1580      (emit-x-form-inst segment 31
1581                        (reg-tn-encoding rs) 
1582                        (reg-tn-encoding ra)
1583                        rb
1584                        824
1585                        0)))
1586   
1587   (define-instruction srawi. (segment ra rs rb)
1588     (:printer x-9 ((op 31) (xo 824) (rc 1)))
1589     (:cost 1)
1590     (:delay 1)
1591     (:dependencies (reads rs) (writes ra))
1592     (:emitter
1593      (emit-x-form-inst segment 31
1594                        (reg-tn-encoding rs) 
1595                        (reg-tn-encoding ra)
1596                        rb
1597                        824
1598                         1)))
1599   
1600   (define-instruction eieio (segment)
1601     (:printer x-27 ((op 31) (xo 854)))
1602     :pinned
1603     (:delay 1)
1604     (:emitter (emit-x-form-inst segment 31 0 0 0 854 0)))
1605   
1606   (define-x-5-st-instruction sthbrx 31 918 nil)
1607   
1608   (define-2-x-10-instructions extsb 31 954)
1609   (define-2-x-10-instructions extsh 31 922)
1610                                         ; Whew.
1611   
1612   (define-instruction lwz (segment rt ra si)
1613     (:declare (type (or fixup (signed-byte 16)) si))
1614     (:printer d ((op 32)))
1615     (:delay 2)
1616     (:cost 2)
1617     (:dependencies (reads ra) (writes rt))
1618     (:emitter
1619      (when (typep si 'fixup)
1620        (note-fixup segment :l si)
1621        (setq si 0))
1622      (emit-d-form-inst segment 32 (reg-tn-encoding rt) (reg-tn-encoding ra) si)))
1623   
1624   (define-d-instruction lwzu 33 :other-dependencies ((writes ra)))
1625   (define-d-instruction lbz 34)
1626   (define-d-instruction lbzu 35 :other-dependencies ((writes ra)))
1627   (define-d-rs-instruction stw 36)
1628   (define-d-rs-instruction stwu 37 :other-dependencies ((writes ra)))
1629   (define-d-rs-instruction stb 38)
1630   (define-d-rs-instruction stbu 39 :other-dependencies ((writes ra)))
1631   (define-d-instruction lhz 40)
1632   (define-d-instruction lhzu 41 :other-dependencies ((writes ra)))
1633   (define-d-instruction lha 42)
1634   (define-d-instruction lhau 43 :other-dependencies ((writes ra)))
1635   (define-d-rs-instruction sth 44)
1636   (define-d-rs-instruction sthu 45 :other-dependencies ((writes ra)))
1637   (define-d-instruction lmw 46 :pinned t)
1638   (define-d-rs-instruction stmw 47 :pinned t)
1639   (define-d-frt-instruction lfs 48)
1640   (define-d-frt-instruction lfsu 49 :other-dependencies ((writes ra)))
1641   (define-d-frt-instruction lfd 50)
1642   (define-d-frt-instruction lfdu 51 :other-dependencies ((writes ra)))
1643   (define-d-frs-instruction stfs 52)
1644   (define-d-frs-instruction stfsu 53 :other-dependencies ((writes ra)))
1645   (define-d-frs-instruction stfd 54)
1646   (define-d-frs-instruction stfdu 55 :other-dependencies ((writes ra)))
1647   
1648   (define-2-a-tab-instructions fdivs 59 18 :cost 17)
1649   (define-2-a-tab-instructions fsubs 59 20)
1650   (define-2-a-tab-instructions fadds 59 21)
1651   (define-2-a-tac-instructions fmuls 59 25)
1652   (define-2-a-instructions fmsubs 59 28 :cost 4)
1653   (define-2-a-instructions fmadds 59 29 :cost 4)
1654   (define-2-a-instructions fnmsubs 59 30 :cost 4)
1655   (define-2-a-instructions fnmadds 59 31 :cost 4)
1656
1657   (define-instruction fcmpu (segment crfd fra frb)
1658     (:printer x-15 ((op 63) (xo 0)))
1659     (:dependencies (reads fra) (reads frb) (reads :fpscr) 
1660                    (writes :fpscr) (writes :ccr))
1661     (:cost 4)
1662     (:delay 4)
1663     (:emitter (emit-x-form-inst segment 
1664                                 63 
1665                                 (valid-cr-field-encoding crfd)
1666                                 (fp-reg-tn-encoding fra) 
1667                                 (fp-reg-tn-encoding frb)
1668                                 0
1669                                 0)))
1670   
1671   
1672   (define-2-x-21-instructions frsp 63 12)
1673   (define-2-x-21-instructions fctiw 63 14)
1674   (define-2-x-21-instructions fctiwz 63 15)
1675   
1676   (define-2-a-tab-instructions fdiv 63 18 :cost 31)
1677   (define-2-a-tab-instructions fsub 63 20)
1678   (define-2-a-tab-instructions fadd 63 21)
1679   (define-2-a-tac-instructions fmul 63 25 :cost 5)
1680   (define-2-a-instructions fmsub 63 28 :cost 5)
1681   (define-2-a-instructions fmadd 63 29 :cost 5)
1682   (define-2-a-instructions fnmsub 63 30 :cost 5)
1683   (define-2-a-instructions fnmadd 63 31 :cost 5)
1684   
1685   (define-instruction fcmpo (segment crfd fra frb)
1686     (:printer x-15 ((op 63) (xo 32)))
1687     (:dependencies (reads fra) (reads frb) (reads :fpscr) 
1688                    (writes :fpscr) (writes :ccr))
1689     (:cost 4)
1690     (:delay 1)
1691     (:emitter (emit-x-form-inst segment 
1692                                 63 
1693                                 (valid-cr-field-encoding crfd)
1694                                 (fp-reg-tn-encoding fra) 
1695                                 (fp-reg-tn-encoding frb)
1696                                 32
1697                               0)))
1698   
1699   (define-2-x-21-instructions fneg 63 40)
1700   
1701   (define-2-x-21-instructions fmr 63 72)
1702   (define-2-x-21-instructions fnabs 63 136)
1703   (define-2-x-21-instructions fabs 63 264)
1704   
1705   (define-instruction mffs (segment frd)
1706   (:printer x-22 ((op 63)  (xo 583) (rc 0)))
1707   (:delay 1)
1708   (:dependencies (reads :fpscr) (writes frd))
1709   (:emitter (emit-x-form-inst segment 
1710                           63 
1711                           (fp-reg-tn-encoding frd)
1712                           0 
1713                           0
1714                           583
1715                           0)))
1716
1717   (define-instruction mffs. (segment frd)
1718   (:printer x-22 ((op 63)  (xo 583) (rc 1)))
1719   (:delay 1)
1720   (:dependencies (reads :fpscr) (writes frd))
1721   (:emitter (emit-x-form-inst segment 
1722                           63 
1723                           (fp-reg-tn-encoding frd)
1724                           0 
1725                           0
1726                           583
1727                           1)))
1728
1729   (define-instruction mtfsf (segment mask rb)
1730   (:printer xfl ((op 63) (xo 711) (rc 0)))
1731   (:dependencies (reads rb) (writes :fpscr))
1732   (:delay 1)
1733   (:emitter (emit-xfl-form-inst segment 63  (ash mask 1) (fp-reg-tn-encoding rb) 711 0)))
1734
1735   (define-instruction mtfsf. (segment mask rb)
1736   (:printer xfl ((op 63) (xo 711) (rc 1)))
1737   (:delay 1)
1738   (:dependencies (reads rb) (writes :ccr) (writes :fpscr))
1739   (:emitter (emit-xfl-form-inst segment 63  (ash mask 1) (fp-reg-tn-encoding rb) 711 1)))
1740
1741
1742
1743 \f
1744 ;;; Here in the future, macros are our friends.
1745
1746   (define-instruction-macro subis (rt ra simm)
1747     `(inst addis ,rt ,ra (- ,simm)))
1748   
1749   (define-instruction-macro sub (rt rb ra)
1750     `(inst subf ,rt ,ra ,rb))
1751   (define-instruction-macro sub. (rt rb ra)
1752     `(inst subf. ,rt ,ra ,rb))
1753   (define-instruction-macro subo (rt rb ra)
1754     `(inst subfo ,rt ,ra ,rb))
1755   (define-instruction-macro subo. (rt rb ra)
1756     `(inst subfo. ,rt ,ra ,rb))
1757
1758
1759   (define-instruction-macro subic (rt ra simm)
1760     `(inst addic ,rt ,ra (- ,simm)))
1761   
1762
1763   (define-instruction-macro subic. (rt ra simm)
1764     `(inst addic. ,rt ,ra (- ,simm)))
1765   
1766   
1767   
1768   (define-instruction-macro subc (rt rb ra)
1769     `(inst subfc ,rt ,ra ,rb))
1770   (define-instruction-macro subc. (rt rb ra)
1771     `(inst subfc. ,rt ,ra ,rb))
1772   (define-instruction-macro subco (rt rb ra)
1773     `(inst subfco ,rt ,ra ,rb))
1774   (define-instruction-macro subco. (rt rb ra)
1775     `(inst subfco. ,rt ,ra ,rb))
1776   
1777   (define-instruction-macro subi (rt ra simm)
1778     `(inst addi ,rt ,ra (- ,simm)))
1779   
1780   (define-instruction-macro li (rt val)
1781     `(inst addi ,rt zero-tn ,val))
1782   
1783   (define-instruction-macro lis (rt val)
1784     `(inst addis ,rt zero-tn ,val))
1785   
1786   
1787   (define-instruction-macro not (ra rs)
1788     `(inst nor ,ra ,rs ,rs))
1789   
1790   (define-instruction-macro not. (ra rs)
1791     `(inst nor. ,ra ,rs ,rs))
1792   
1793   
1794   (!def-vm-support-routine emit-nop (segment)
1795                            (emit-word segment #x60000000))
1796   
1797   (define-instruction-macro extlwi (ra rs n b)
1798     `(inst rlwinm ,ra ,rs ,b 0 (1- ,n)))
1799   
1800   (define-instruction-macro extlwi. (ra rs n b)
1801     `(inst rlwinm. ,ra ,rs ,b 0 (1- ,n)))
1802   
1803   (define-instruction-macro srwi (ra rs n)
1804     `(inst rlwinm ,ra ,rs (- 32 ,n) ,n 31))
1805   
1806   (define-instruction-macro srwi. (ra rs n)
1807     `(inst rlwinm. ,ra ,rs (- 32 ,n) ,n 31))
1808   
1809   (define-instruction-macro clrrwi (ra rs n)
1810     `(inst rlwinm ,ra ,rs 0 0 (- 31 ,n)))
1811   
1812   (define-instruction-macro clrrwi. (ra rs n)
1813     `(inst rlwinm. ,ra ,rs 0 0 (- 31 ,n)))
1814   
1815   (define-instruction-macro inslw (ra rs n b)
1816     `(inst rlwimi ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1817   
1818   (define-instruction-macro inslw. (ra rs n b)
1819     `(inst rlwimi. ,ra ,rs (- 32 ,b) ,b (+ ,b (1- ,n))))
1820   
1821   (define-instruction-macro rotlw (ra rs rb)
1822     `(inst rlwnm ,ra ,rs ,rb 0 31))
1823   
1824   (define-instruction-macro rotlw. (ra rs rb)
1825     `(inst rlwnm. ,ra ,rs ,rb 0 31))
1826   
1827   (define-instruction-macro rotlwi (ra rs n)
1828     `(inst rlwinm ,ra ,rs ,n 0 31))
1829
1830   (define-instruction-macro rotrwi (ra rs n)
1831     `(inst rlwinm ,ra ,rs (- 32 ,n) 0 31))
1832
1833   (define-instruction-macro slwi (ra rs n)
1834     `(inst rlwinm ,ra ,rs ,n 0 (- 31 ,n)))
1835
1836   (define-instruction-macro slwi. (ra rs n)
1837     `(inst rlwinm. ,ra ,rs ,n 0 (- 31 ,n))))
1838   
1839
1840
1841
1842 #|
1843 (macrolet 
1844   ((define-conditional-branches (name bo-name)
1845      (let* ((bo-enc (valid-bo-encoding bo-name)))
1846        `(progn
1847           (define-instruction-macro ,(symbolicate name "A") (bi target)
1848             ``(inst bca ,,,bo-enc ,,bi ,,target))
1849           (define-instruction-macro ,(symbolicate name "L") (bi target)
1850             ``(inst bcl ,,,bo-enc ,,bi ,,target))
1851           (define-instruction-macro ,(symbolicate name "LA") (bi target)
1852             ``(inst bcla ,,,bo-enc ,,bi ,,target))
1853           (define-instruction-macro ,(symbolicate name "CTR") (bi target)
1854             ``(inst bcctr ,,,bo-enc ,,bi ,,target))
1855           (define-instruction-macro ,(symbolicate name "CTRL") (bi target)
1856             ``(inst bcctrl ,,,bo-enc ,,bi ,,target))
1857           (define-instruction-macro ,(symbolicate name "LR") (bi target)
1858             ``(inst bclr ,,,bo-enc ,,bi ,,target))
1859           (define-instruction-macro ,(symbolicate name "LRL") (bi target)
1860             ``(inst bclrl ,,,bo-enc ,,bi ,,target))))))
1861   (define-conditional-branches bt :bo-t)
1862   (define-conditional-branches bf :bo-f))
1863 |#
1864
1865 (macrolet 
1866   ((define-positive-conditional-branches (name cr-bit-name)
1867      `(progn
1868         (define-instruction-macro ,name (crf &optional (target nil target-p))
1869           (unless target-p
1870             (setq target crf crf :cr0))
1871           `(inst bt `(,,crf ,,,cr-bit-name) ,target))
1872 #|
1873         (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1874           ``(inst bta (,,cr-field ,,,cr-bit-name) ,,target))
1875         (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1876           ``(inst btl (,,cr-field ,,,cr-bit-name) ,,target))
1877         (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1878           ``(inst btla (,,cr-field ,,,cr-bit-name) ,,target))
1879         (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1880           ``(inst btctr (,,cr-field ,,,cr-bit-name) ,,target))
1881         (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1882           ``(inst btctrl (,,cr-field ,,,cr-bit-name) ,,target))
1883         (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1884           ``(inst btlr (,,cr-field ,,,cr-bit-name) ,,target))
1885         (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1886           ``(inst btlrl (,,cr-field ,,,cr-bit-name) ,,target))
1887 |#
1888         )))
1889   (define-positive-conditional-branches beq :eq)
1890   (define-positive-conditional-branches blt :lt)
1891   (define-positive-conditional-branches bgt :gt)
1892   (define-positive-conditional-branches bso :so)
1893   (define-positive-conditional-branches bun :so))
1894
1895
1896 (macrolet 
1897   ((define-negative-conditional-branches (name cr-bit-name)
1898      `(progn
1899         (define-instruction-macro ,name (crf &optional (target nil target-p))
1900           (unless target-p
1901             (setq target crf crf :cr0))
1902           `(inst bf `(,,crf ,,,cr-bit-name) ,target))
1903 #|
1904         (define-instruction-macro ,(symbolicate name "A") (target &optional (cr-field :cr0))
1905           ``(inst bfa (,,cr-field ,,,cr-bit-name) ,,target))
1906         (define-instruction-macro ,(symbolicate name "L") (target &optional (cr-field :cr0))
1907           ``(inst bfl (,,cr-field ,,,cr-bit-name) ,,target))
1908         (define-instruction-macro ,(symbolicate name "LA") (target &optional (cr-field :cr0))
1909           ``(inst bfla (,,cr-field ,,,cr-bit-name) ,,target))
1910         (define-instruction-macro ,(symbolicate name "CTR") (target &optional (cr-field :cr0))
1911           ``(inst bfctr (,,cr-field ,,,cr-bit-name) ,,target))
1912         (define-instruction-macro ,(symbolicate name "CTRL") (target &optional (cr-field :cr0))
1913           ``(inst bfctrl (,,cr-field ,,,cr-bit-name) ,,target))
1914         (define-instruction-macro ,(symbolicate name "LR") (target &optional (cr-field :cr0))
1915           ``(inst bflr (,,cr-field ,,,cr-bit-name) ,,target))
1916         (define-instruction-macro ,(symbolicate name "LRL") (target &optional (cr-field :cr0))
1917           ``(inst bflrl (,,cr-field ,,,cr-bit-name) ,,target))
1918 |#
1919 )))
1920   (define-negative-conditional-branches bne :eq)
1921   (define-negative-conditional-branches bnl :lt)
1922   (define-negative-conditional-branches bge :lt)
1923   (define-negative-conditional-branches bng :gt)
1924   (define-negative-conditional-branches ble :gt)
1925   (define-negative-conditional-branches bns :so)
1926   (define-negative-conditional-branches bnu :so))
1927
1928
1929
1930 (define-instruction-macro j (func-tn offset)
1931   `(progn
1932     (inst addi lip-tn ,func-tn ,offset)
1933     (inst mtctr lip-tn)
1934     (inst bctr)))
1935
1936
1937 #|
1938 (define-instruction-macro bua (target)
1939   `(inst bca :bo-u 0 ,target))
1940
1941 (define-instruction-macro bul (target)
1942   `(inst bcl :bo-u 0 ,target))
1943
1944 (define-instruction-macro bula (target)
1945   `(inst bcla :bo-u 0 ,target))
1946
1947
1948 (define-instruction-macro blrl ()
1949   `(inst bclrl :bo-u 0))
1950
1951
1952
1953 |#
1954
1955
1956
1957
1958 \f
1959 ;;; Some more macros 
1960
1961 (defun %lr (reg value)
1962   (etypecase value
1963     ((signed-byte 16)
1964      (inst li reg value))
1965     ((unsigned-byte 16)
1966      (inst ori reg zero-tn value))
1967     ((or (signed-byte 32) (unsigned-byte 32))
1968      (let* ((high-half (ldb (byte 16 16) value))
1969             (low-half (ldb (byte 16 0) value)))
1970        (declare (type (unsigned-byte 16) high-half low-half))
1971        (cond ((if (logbitp 15 low-half) (= high-half #xffff) (zerop high-half))
1972               (inst li reg low-half))
1973              (t
1974               (inst lis reg high-half)
1975               (unless (zerop low-half)
1976                 (inst ori reg reg low-half))))))
1977     (fixup
1978      (inst lis reg value)
1979      (inst addi reg reg value))))
1980
1981 (define-instruction-macro lr (reg value)
1982   `(%lr ,reg ,value))
1983      
1984
1985 \f
1986 ;;;; Instructions for dumping data and header objects.
1987
1988 (define-instruction word (segment word)
1989   (:declare (type (or (unsigned-byte 32) (signed-byte 32)) word))
1990   :pinned
1991   (:delay 0)
1992   (:emitter
1993    (emit-word segment word)))
1994
1995 (define-instruction short (segment short)
1996   (:declare (type (or (unsigned-byte 16) (signed-byte 16)) short))
1997   :pinned
1998   (:delay 0)
1999   (:emitter
2000    (emit-short segment short)))
2001
2002 (define-instruction byte (segment byte)
2003   (:declare (type (or (unsigned-byte 8) (signed-byte 8)) byte))
2004   :pinned
2005   (:delay 0)
2006   (:emitter
2007    (emit-byte segment byte)))
2008
2009 (define-bitfield-emitter emit-header-object 32
2010   (byte 24 8) (byte 8 0))
2011
2012 (defun emit-header-data (segment type)
2013   (emit-back-patch
2014    segment 4
2015    #'(lambda (segment posn)
2016        (emit-word segment
2017                   (logior type
2018                           (ash (+ posn (component-header-length))
2019                                (- n-widetag-bits word-shift)))))))
2020
2021 (define-instruction simple-fun-header-word (segment)
2022   :pinned
2023   (:delay 0)
2024   (:emitter
2025    (emit-header-data segment simple-fun-header-widetag)))
2026
2027 (define-instruction lra-header-word (segment)
2028   :pinned
2029   (:delay 0)
2030   (:emitter
2031    (emit-header-data segment return-pc-header-widetag)))
2032
2033 \f
2034 ;;;; Instructions for converting between code objects, functions, and lras.
2035 (defun emit-compute-inst (segment vop dst src label temp calc)
2036   (emit-chooser
2037    ;; We emit either 12 or 4 bytes, so we maintain 8 byte alignments.
2038    segment 12 3
2039    #'(lambda (segment posn delta-if-after)
2040        (let ((delta (funcall calc label posn delta-if-after)))
2041          (when (<= (- (ash 1 15)) delta (1- (ash 1 15)))
2042            (emit-back-patch segment 4
2043                             #'(lambda (segment posn)
2044                                 (assemble (segment vop)
2045                                           (inst addi dst src
2046                                                 (funcall calc label posn 0)))))
2047            t)))
2048    #'(lambda (segment posn)
2049        (let ((delta (funcall calc label posn 0)))
2050          (assemble (segment vop)
2051                    (inst lis temp (ldb (byte 16 16) delta))
2052                    (inst ori temp temp (ldb (byte 16 0) delta))
2053                    (inst add dst src temp))))))
2054
2055 ;; this function is misnamed.  should be compute-code-from-lip,
2056 ;; if the use in xep-allocate-frame is typical
2057 ;; (someone says code = fn - header - label-offset + other-pointer-tag)
2058 (define-instruction compute-code-from-fn (segment dst src label temp)
2059   (:declare (type tn dst src temp) (type label label))
2060   (:attributes variable-length)
2061   (:dependencies (reads src) (writes dst) (writes temp))
2062   (:delay 0)
2063   (:vop-var vop)
2064   (:emitter
2065    (emit-compute-inst segment vop dst src label temp
2066                       #'(lambda (label posn delta-if-after)
2067                           (- other-pointer-lowtag
2068                              ;;function-pointer-type
2069                              (label-position label posn delta-if-after)
2070                              (component-header-length))))))
2071
2072 ;; code = lra - other-pointer-tag - header - label-offset + other-pointer-tag
2073 (define-instruction compute-code-from-lra (segment dst src label temp)
2074   (:declare (type tn dst src temp) (type label label))
2075   (:attributes variable-length)
2076   (:dependencies (reads src) (writes dst) (writes temp))
2077   (:delay 0)
2078   (:vop-var vop)
2079   (:emitter
2080    (emit-compute-inst segment vop dst src label temp
2081                       #'(lambda (label posn delta-if-after)
2082                           (- (+ (label-position label posn delta-if-after)
2083                                 (component-header-length)))))))
2084
2085 ;; lra = code + other-pointer-tag + header + label-offset - other-pointer-tag
2086 (define-instruction compute-lra-from-code (segment dst src label temp)
2087   (:declare (type tn dst src temp) (type label label))
2088   (:attributes variable-length)
2089   (:dependencies (reads src) (writes dst) (writes temp))
2090   (:delay 0)
2091   (:vop-var vop)
2092   (:emitter
2093    (emit-compute-inst segment vop dst src label temp
2094                       #'(lambda (label posn delta-if-after)
2095                           (+ (label-position label posn delta-if-after)
2096                              (component-header-length))))))