1 ;;;; machine-independent disassembler
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.
12 (in-package "SB!DISASSEM")
14 ;;; types and defaults
16 (def!constant label-column-width
7)
18 (deftype text-width
() '(integer 0 1000))
19 (deftype alignment
() '(integer 0 64))
20 (deftype offset
() '(signed-byte 24))
21 (deftype address
() '(unsigned-byte #.sb
!vm
:n-word-bits
))
22 (deftype disassem-length
() '(unsigned-byte 24))
23 (deftype column
() '(integer 0 1000))
25 (def!constant max-filtered-value-index
32)
26 (deftype filtered-value-index
()
27 `(integer 0 (,max-filtered-value-index
)))
28 (deftype filtered-value-vector
()
29 `(simple-array t
(,max-filtered-value-index
)))
31 ;;;; disassembly parameters
34 (defvar *disassem-insts
* (make-hash-table :test
'eq
))
35 (declaim (type hash-table
*disassem-insts
*))
37 (defvar *disassem-inst-space
* nil
)
39 ;;; minimum alignment of instructions, in bytes
40 (defvar *disassem-inst-alignment-bytes
* sb
!vm
:n-word-bytes
)
41 (declaim (type alignment
*disassem-inst-alignment-bytes
*))
43 ;; How many columns of output to allow for the address preceding each line.
44 ;; If NIL, use the minimum possible width for the disassembly range.
45 ;; If 0, do not print addresses.
46 (defvar *disassem-location-column-width
* nil
)
47 (declaim (type (or null text-width
) *disassem-location-column-width
*))
49 ;;; the width of the column in which instruction-names are printed. A
50 ;;; value of zero gives the effect of not aligning the arguments at
52 (defvar *disassem-opcode-column-width
* 0)
53 (declaim (type text-width
*disassem-opcode-column-width
*))
55 ;;; the width of the column in which instruction-bytes are printed. A
56 ;;; value of zero disables the printing of instruction bytes.
57 (defvar *disassem-inst-column-width
* 16
59 "The width of instruction bytes.")
60 (declaim (type text-width
*disassem-inst-column-width
*))
62 (defvar *disassem-note-column
* (+ 45 *disassem-inst-column-width
*)
64 "The column in which end-of-line comments for notes are started.")
68 ;;;; There's no need for 1000 different versions of a function equivalent
69 ;;;; to (PROGN (PRINT ADDR) (PRINT OPCODE) (PRINT ARG)) so we try to
70 ;;;; coalesce sexprs, since there is no such thing as coalescing compiled code.
71 ;;;; This is not really a "cache" as much as hashtable for coalescing.
73 (defstruct (fun-cache (:copier nil
)
74 (:print-object
(lambda (self stream
)
75 (print-unreadable-object
76 (self stream
:type t
:identity t
)))))
77 (serial-number 0 :type fixnum
)
78 (printers nil
:type list
)
79 (labellers nil
:type list
)
80 (prefilters nil
:type list
))
82 (defvar *disassem-fun-cache
* (make-fun-cache))
83 (declaim (type fun-cache
*disassem-fun-cache
*))
85 ;;;; A DCHUNK contains the bits we look at to decode an
87 ;;;; I tried to keep this abstract so that if using integers > the machine
88 ;;;; word size conses too much, it can be changed to use bit-vectors or
91 ;;;; KLUDGE: It's not clear that using bit-vectors would be any more efficient.
92 ;;;; Perhaps the abstraction could go away. -- WHN 19991124
95 (declaim (inline dchunk-or dchunk-and dchunk-clear dchunk-not
96 dchunk-make-mask dchunk-make-field
102 (def!constant dchunk-bits
#.sb
!vm
:n-word-bits
)
105 `(unsigned-byte ,dchunk-bits
))
106 (deftype dchunk-index
()
107 `(integer 0 ,dchunk-bits
))
109 (def!constant dchunk-zero
0)
110 (def!constant dchunk-one
#.
(1- (expt 2 sb
!vm
:n-word-bits
)))
112 (defun dchunk-extract (from pos
)
113 (declare (type dchunk from
))
114 (the dchunk
(ldb pos
(the dchunk from
))))
116 (defmacro dchunk-copy
(x)
119 (defun dchunk-or (to from
)
120 (declare (type dchunk to from
))
121 (the dchunk
(logior to from
)))
122 (defun dchunk-and (to from
)
123 (declare (type dchunk to from
))
124 (the dchunk
(logand to from
)))
125 (defun dchunk-clear (to from
)
126 (declare (type dchunk to from
))
127 (the dchunk
(logandc2 to from
)))
128 (defun dchunk-not (from)
129 (declare (type dchunk from
))
130 (the dchunk
(logand dchunk-one
(lognot from
))))
132 (defmacro dchunk-andf
(to from
)
133 `(setf ,to
(dchunk-and ,to
,from
)))
134 (defmacro dchunk-orf
(to from
)
135 `(setf ,to
(dchunk-or ,to
,from
)))
136 (defmacro dchunk-clearf
(to from
)
137 `(setf ,to
(dchunk-clear ,to
,from
)))
139 (defun dchunk-make-mask (pos)
140 (the dchunk
(mask-field pos -
1)))
141 (defun dchunk-make-field (pos value
)
142 (the dchunk
(dpb value pos
0)))
144 (defmacro make-dchunk
(value)
145 `(the dchunk
,value
))
147 (defun sap-ref-dchunk (sap byte-offset byte-order
)
148 (declare (type sb
!sys
:system-area-pointer sap
)
149 (type offset byte-offset
)
150 (optimize (speed 3) (safety 0)))
153 (32 (if (eq byte-order
:big-endian
)
154 (+ (ash (sb!sys
:sap-ref-8 sap byte-offset
) 24)
155 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 16)
156 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 8)
157 (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)))
158 (+ (sb!sys
:sap-ref-8 sap byte-offset
)
159 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 8)
160 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 16)
161 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 24))))
162 (64 (if (eq byte-order
:big-endian
)
163 (+ (ash (sb!sys
:sap-ref-8 sap byte-offset
) 56)
164 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 48)
165 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 40)
166 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 32)
167 (ash (sb!sys
:sap-ref-8 sap
(+ 4 byte-offset
)) 24)
168 (ash (sb!sys
:sap-ref-8 sap
(+ 5 byte-offset
)) 16)
169 (ash (sb!sys
:sap-ref-8 sap
(+ 6 byte-offset
)) 8)
170 (sb!sys
:sap-ref-8 sap
(+ 7 byte-offset
)))
171 (+ (sb!sys
:sap-ref-8 sap byte-offset
)
172 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 8)
173 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 16)
174 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 24)
175 (ash (sb!sys
:sap-ref-8 sap
(+ 4 byte-offset
)) 32)
176 (ash (sb!sys
:sap-ref-8 sap
(+ 5 byte-offset
)) 40)
177 (ash (sb!sys
:sap-ref-8 sap
(+ 6 byte-offset
)) 48)
178 (ash (sb!sys
:sap-ref-8 sap
(+ 7 byte-offset
)) 56)))))))
180 (defun dchunk-corrected-extract (from pos unit-bits byte-order
)
181 (declare (type dchunk from
))
182 (if (eq byte-order
:big-endian
)
183 (ldb (byte (byte-size pos
)
184 (+ (byte-position pos
) (- dchunk-bits unit-bits
)))
186 (ldb pos
(the dchunk from
))))
188 (defmacro dchunk-insertf
(place pos value
)
189 `(setf ,place
(the dchunk
(dpb ,value
,pos
(the dchunk
,place
)))))
192 (declare (type dchunk x y
))
194 (defmacro dchunk-zerop
(x)
195 `(dchunk= ,x dchunk-zero
))
197 (defun dchunk-strict-superset-p (sup sub
)
198 (and (zerop (logandc2 sub sup
))
199 (not (zerop (logandc2 sup sub
)))))
201 (defun dchunk-count-bits (x)
202 (declare (type dchunk x
))
205 (defstruct (instruction (:conc-name inst-
)
207 make-instruction
(name
213 labeller prefilter control
))
215 (name nil
:type
(or symbol string
))
216 (format-name nil
:type
(or symbol string
))
218 (mask dchunk-zero
:type dchunk
) ; bits in the inst that are constant
219 (id dchunk-zero
:type dchunk
) ; value of those constant bits
221 (length 0 :type disassem-length
) ; in bytes
223 (print-name nil
:type symbol
)
225 ;; disassembly functions
226 (prefilter nil
:type
(or null function
))
227 (labeller nil
:type
(or null function
))
228 (printer (missing-arg) :type
(or null function
))
229 (control nil
:type
(or null function
))
231 ;; instructions that are the same as this instruction but with more
233 (specializers nil
:type list
))
234 (def!method print-object
((inst instruction
) stream
)
235 (print-unreadable-object (inst stream
:type t
:identity t
)
236 (format stream
"~A(~A)" (inst-name inst
) (inst-format-name inst
))))
238 ;;;; an instruction space holds all known machine instructions in a
239 ;;;; form that can be easily searched
241 (defstruct (inst-space (:conc-name ispace-
)
243 (valid-mask dchunk-zero
:type dchunk
) ; applies to *children*
244 (choices nil
:type list
))
245 (def!method print-object
((ispace inst-space
) stream
)
246 (print-unreadable-object (ispace stream
:type t
:identity t
)))
248 ;;; now that we've defined the structure, we can declaim the type of
250 (declaim (type (or null inst-space
) *disassem-inst-space
*))
252 (defstruct (inst-space-choice (:conc-name ischoice-
)
254 (common-id dchunk-zero
:type dchunk
) ; applies to *parent's* mask
255 (subspace (missing-arg) :type
(or inst-space instruction
)))
257 ;;;; These are the kind of values we can compute for an argument, and
258 ;;;; how to compute them. The :CHECKER functions make sure that a given
259 ;;;; argument is compatible with another argument for a given use.
261 (defvar *arg-form-kinds
* nil
)
263 (defstruct (arg-form-kind (:copier nil
))
264 (names nil
:type list
)
265 (producer (missing-arg) :type function
)
266 (checker (missing-arg) :type function
))
268 (defun arg-form-kind-or-lose (kind)
269 (or (getf *arg-form-kinds
* kind
)
270 (pd-error "unknown arg-form kind ~S" kind
)))
272 (defun find-arg-form-producer (kind)
273 (arg-form-kind-producer (arg-form-kind-or-lose kind
)))
274 (defun find-arg-form-checker (kind)
275 (arg-form-kind-checker (arg-form-kind-or-lose kind
)))
277 (defun canonicalize-arg-form-kind (kind)
278 (car (arg-form-kind-names (arg-form-kind-or-lose kind
))))
280 ;;;; only used during compilation of the instructions for a backend
282 ;;;; FIXME: If only used then, isn't there some way we could do
283 ;;;; EVAL-WHEN tricks to keep this stuff from appearing in the target
286 (defvar *disassem-inst-formats
* (make-hash-table))
287 (defvar *disassem-arg-types
* nil
)
288 (defvar *disassem-fun-cache
* (make-fun-cache))
290 (defstruct (arg (:copier nil
)
292 (:constructor %make-arg
(name &optional position
))
293 (:constructor standard-make-arg
) ; only so #S readmacro works
295 (lambda (self stream
)
298 (print-unreadable-object (self stream
:type t
)
300 "~D:~A ~:[~;+~]~:S~@[=~S~]~@[ filt=~S~]~
301 ~@[ lbl=~S~]~@[ prt=~S~]"
304 (arg-sign-extend-p self
)
309 (arg-printer self
)))))))
310 (name nil
:type symbol
)
311 (fields nil
:type list
)
313 (value nil
:type
(or list integer
))
314 (sign-extend-p nil
:type
(member t nil
))
316 ;; position in a vector of prefiltered values
317 (position 0 :type fixnum
)
324 (defstruct (instruction-format (:conc-name format-
)
325 (:constructor make-inst-format
326 (name length default-printer args
))
329 (args nil
:type list
)
331 (length 0 :type disassem-length
) ; in bytes
333 (default-printer nil
:type list
))
335 ;;; A FUNSTATE holds the state of any arguments used in a disassembly
337 (defstruct (funstate (:conc-name funstate-
)
338 (:constructor %make-funstate
)
340 (args nil
:type list
)
341 (arg-temps nil
:type list
)) ; See below.
343 (defun make-funstate (args)
344 ;; give the args a position
347 (setf (arg-position arg
) i
)
349 (%make-funstate
:args args
))
351 (defun funstate-compatible-p (funstate args
)
352 (every (lambda (this-arg-temps)
353 (let* ((old-arg (car this-arg-temps
))
354 (new-arg (find (arg-name old-arg
) args
:key
#'arg-name
)))
356 (= (arg-position old-arg
) (arg-position new-arg
))
357 (every (lambda (this-kind-temps)
358 (funcall (find-arg-form-checker
359 (car this-kind-temps
))
362 (cdr this-arg-temps
)))))
363 (funstate-arg-temps funstate
)))
365 (defun arg-or-lose (name funstate
)
366 (let ((arg (find name
(funstate-args funstate
) :key
#'arg-name
)))
368 (pd-error "unknown argument ~S" name
))
371 ;;;; Since we can't include some values in compiled output as they are
372 ;;;; (notably functions), we sometimes use a VALSRC structure to keep
373 ;;;; track of the source from which they were derived.
375 (defstruct (valsrc (:constructor %make-valsrc
)
380 (defun make-valsrc (value source
)
381 (cond ((equal value source
)
383 ((and (listp value
) (eq (car value
) 'function
))
386 (%make-valsrc
:value value
:source source
))))
388 ;;; machinery to provide more meaningful error messages during compilation
389 (defvar *current-instruction-flavor
* nil
)
390 (defun pd-error (fmt &rest args
)
391 (if *current-instruction-flavor
*
392 (error "~@<in printer-definition for ~S(~S): ~3I~:_~?~:>"
393 (car *current-instruction-flavor
*)
394 (cdr *current-instruction-flavor
*)
396 (apply #'error fmt args
)))
399 ;;; 1. This should become a utility in SB!INT.
400 ;;; 2. Arrays and structures and maybe other things are
401 ;;; self-evaluating too.
402 (defun self-evaluating-p (x)
410 (defun maybe-quote (evalp form
)
411 (if (or evalp
(self-evaluating-p form
)) form
`',form
))
413 ;;; Detect things that obviously don't need wrapping, like
414 ;;; variable-refs and #'function.
415 (defun doesnt-need-wrapping-p (form)
418 (eq (car form
) 'function
)
419 (symbolp (cadr form
)))))
421 (defun make-wrapper (form arg-name funargs prefix
)
422 (if (and (listp form
)
423 (eq (car form
) 'function
))
425 (let ((wrapper-name (symbolicate prefix
"-" arg-name
"-WRAPPER"))
426 (wrapper-args (make-gensym-list (length funargs
))))
427 (values `#',wrapper-name
428 `(defun ,wrapper-name
,wrapper-args
429 (funcall ,form
,@wrapper-args
))))
431 (let ((wrapper-name (symbolicate "*" prefix
"-" arg-name
"-WRAPPER*")))
432 (values wrapper-name
`(defparameter ,wrapper-name
,form
)))))
434 (defun filter-overrides (overrides evalp
)
435 (mapcar (lambda (override)
436 (list* (car override
) (cadr override
)
437 (munge-fun-refs (cddr override
) evalp
)))
440 (defparameter *arg-fun-params
*
441 '((:printer .
(value stream dstate
))
442 (:use-label .
(value dstate
))
443 (:prefilter .
(value dstate
))))
445 (defun munge-fun-refs (params evalp
&optional wrap-defs-p
(prefix ""))
446 (let ((params (copy-list params
)))
447 (do ((tail params
(cdr tail
))
450 (values params
(nreverse wrapper-defs
)))
451 (let ((fun-arg (assoc (car tail
) *arg-fun-params
*)))
453 (let* ((fun-form (cadr tail
))
454 (quoted-fun-form `',fun-form
))
455 (when (and wrap-defs-p
(not (doesnt-need-wrapping-p fun-form
)))
456 (multiple-value-bind (access-form wrapper-def-form
)
457 (make-wrapper fun-form
(car fun-arg
) (cdr fun-arg
) prefix
)
458 (setf quoted-fun-form
`',access-form
)
459 (push wrapper-def-form wrapper-defs
)))
462 `(make-valsrc ,fun-form
,quoted-fun-form
))
466 (defun gen-args-def-form (overrides format-form
&optional
(evalp t
))
467 (let ((args-var (gensym)))
468 `(let ((,args-var
(copy-list (format-args ,format-form
))))
469 ,@(mapcar (lambda (override)
470 (update-args-form args-var
473 (cons :value
(cdr override
)))
478 (defun gen-printer-def-forms-def-form (base-name
482 (declare (type symbol base-name
))
486 &optional
(printer-form :default
)
487 &key
((:print-name print-name-form
) `',base-name
) control
)
489 (let ((format-var (gensym))
490 (field-defs (filter-overrides field-defs evalp
)))
491 `(let* ((*current-instruction-flavor
* ',(cons base-name format-name
))
492 (,format-var
(format-or-lose ',format-name
))
493 (args ,(gen-args-def-form field-defs format-var evalp
))
494 (funcache *disassem-fun-cache
*))
495 (multiple-value-bind (printer-fun printer-defun
)
496 (find-printer-fun ,(if (eq printer-form
:default
)
497 `(format-default-printer ,format-var
)
498 (maybe-quote evalp printer-form
))
500 (multiple-value-bind (labeller-fun labeller-defun
)
501 (find-labeller-fun args funcache
)
502 (multiple-value-bind (prefilter-fun prefilter-defun
)
503 (find-prefilter-fun args funcache
)
504 (multiple-value-bind (mask id
)
505 (compute-mask-id args
)
507 `(make-instruction ',',base-name
510 ,(format-length ,format-var
)
513 ,(and printer-fun
`#',printer-fun
)
514 ,(and labeller-fun
`#',labeller-fun
)
515 ,(and prefilter-fun
`#',prefilter-fun
)
518 ,@(and printer-defun
(list printer-defun
))
519 ,@(and labeller-defun
(list labeller-defun
))
520 ,@(and prefilter-defun
(list prefilter-defun
))))
523 (defun update-args-form (var name-form descrip-forms evalp
)
526 `(modify-or-add-arg ,name-form
,var
,@descrip-forms
)
527 `(apply #'modify-or-add-arg
,name-form
,var
',descrip-forms
))))
529 (defun format-or-lose (name)
530 (or (gethash name
*disassem-inst-formats
*)
531 (pd-error "unknown instruction format ~S" name
)))
533 ;;; FIXME: needed only at build-the-system time, not in running system
534 ;;; and FIXME: better syntax would allow inheriting the length to avoid
535 ;;; re-stating it needlessly in some derived formats. Perhaps:
536 ;;; (DEFINE-INSTRUCTION-FORMAT NAME (:bits N [more-format-keys]*) &rest fields)
538 (defmacro define-instruction-format
((format-name length-in-bits
539 &key default-printer include
)
542 "DEFINE-INSTRUCTION-FORMAT (Name Length {Format-Key Value}*) Arg-Def*
543 Define an instruction format NAME for the disassembler's use. LENGTH is
544 the length of the format in bits.
545 Possible FORMAT-KEYs:
547 :INCLUDE other-format-name
548 Inherit all arguments and properties of the given format. Any
549 arguments defined in the current format definition will either modify
550 the copy of an existing argument (keeping in the same order with
551 respect to when prefilters are called), if it has the same name as
552 one, or be added to the end.
553 :DEFAULT-PRINTER printer-list
554 Use the given PRINTER-LIST as a format to print any instructions of
555 this format when they don't specify something else.
557 Each ARG-DEF defines one argument in the format, and is of the form
558 (Arg-Name {Arg-Key Value}*)
560 Possible ARG-KEYs (the values are evaluated unless otherwise specified):
562 :FIELDS byte-spec-list
563 The argument takes values from these fields in the instruction. If
564 the list is of length one, then the corresponding value is supplied by
565 itself; otherwise it is a list of the values. The list may be NIL.
567 The same as :FIELDS (list byte-spec).
570 If the argument only has one field, this is the value it should have,
571 otherwise it's a list of the values of the individual fields. This can
572 be overridden in an instruction-definition or a format definition
573 including this one by specifying another, or NIL to indicate that it's
577 If non-NIL, the raw value of this argument is sign-extended,
578 immediately after being extracted from the instruction (before any
579 prefilters are run, for instance). If the argument has multiple
580 fields, they are all sign-extended.
583 Inherit any properties of the given argument type.
586 A function which is called (along with all other prefilters, in the
587 order that their arguments appear in the instruction-format) before
588 any printing is done, to filter the raw value. Any uses of READ-SUFFIX
589 must be done inside a prefilter.
591 :PRINTER function-string-or-vector
592 A function, string, or vector which is used to print this argument.
595 If non-NIL, the value of this argument is used as an address, and if
596 that address occurs inside the disassembled code, it is replaced by a
597 label. If this is a function, it is called to filter the value."
598 (let ((length-var (gensym)) ; are lengths ever non-constant? probably not.
601 (copy-list (format-args (format-or-lose include
)))))
602 added-args readers all-wrapper-defs
)
603 (dolist (arg-spec arg-specs
)
604 (let ((arg-name (car arg-spec
)))
605 (multiple-value-bind (props wrapper-defs
)
606 (munge-fun-refs (cdr arg-spec
) t t
607 (symbolicate format-name
'- arg-name
))
608 (setf all-wrapper-defs
(nconc wrapper-defs all-wrapper-defs
))
609 (let ((reader (getf props
:reader
)))
611 (setq readers
(list* #!-sb-fluid
`(declaim (inline ,reader
))
612 `(defun ,reader
(dchunk dstate
)
613 (declare (ignorable dchunk dstate
))
614 (arg-access-macro ,arg-name
,format-name
617 (remf props
:reader
))) ; ok because MUNGEing copied the plist
618 (let ((cell (member arg-name inherited-args
620 (arg-name (if (listp x
) (second x
) x
))))))
623 ,(+ (length inherited-args
) (length added-args
))
624 ,length-var
',arg-name
,@props
)
626 (props ; do nothing if no alterations
628 `(copy-arg ,(car cell
) ,length-var
,@props
))))))))
631 (eval-when (:compile-toplevel
:execute
)
632 (let ((,length-var
,length-in-bits
))
633 (setf (gethash ',format-name
*disassem-inst-formats
*)
634 (make-inst-format ',format-name
(bits-to-bytes ,length-var
)
635 ,(maybe-quote t default-printer
)
636 (list ,@inherited-args
637 ,@(nreverse added-args
))))))
640 (defun make-arg (number format-length-bits name
&rest properties
)
641 (apply #'modify-arg
(%make-arg name number
) format-length-bits properties
))
643 (defun copy-arg (arg format-length-bits
&rest properties
)
644 (apply #'modify-arg
(copy-structure arg
) format-length-bits properties
))
646 ;;; FIXME: probably needed only at build-the-system time, not in
647 ;;; final target system
648 (defun modify-or-add-arg (arg-name args
&rest properties
)
649 (declare (dynamic-extent properties
))
650 (when (get-properties properties
'(:field
:fields
))
651 (error "~@<in arg ~S: ~3I~:_~
652 can't specify fields except using DEFINE-INSTRUCTION-FORMAT~:>"
654 (let* ((cell (member arg-name args
:key
#'arg-name
))
656 (setf (car cell
) (copy-structure (car cell
)))
657 (let ((arg (%make-arg arg-name
)))
658 (setf args
(nconc args
(list arg
)))
660 (apply #'modify-arg arg nil properties
)
663 (defun modify-arg (arg format-length
664 &key
(value nil value-p
)
666 (prefilter nil prefilter-p
)
667 (printer nil printer-p
)
668 (sign-extend nil sign-extend-p
)
669 (use-label nil use-label-p
)
671 (fields nil fields-p
))
674 (error ":FIELD and :FIELDS are mutually exclusive")
675 (setf fields
(list field
) fields-p t
)))
677 (set-arg-from-type arg type
*disassem-arg-types
*))
679 (setf (arg-value arg
) value
))
681 (setf (arg-prefilter arg
) prefilter
))
683 (setf (arg-sign-extend-p arg
) sign-extend
))
685 (setf (arg-printer arg
) printer
))
687 (setf (arg-use-label arg
) use-label
))
689 (setf (arg-fields arg
)
690 (mapcar (lambda (bytespec)
691 (when (> (+ (byte-position bytespec
) (byte-size bytespec
))
693 (error "~@<in arg ~S: ~3I~:_~
694 The field ~S doesn't fit in an ~
695 instruction-format ~W bits wide.~:>"
696 (arg-name arg
) bytespec format-length
))
697 (correct-dchunk-bytespec-for-endianness
698 bytespec format-length sb
!c
:*backend-byte-order
*))
702 ;; Generate a sexpr to extract ARG-NAME of FORMAT-NAME using CHUNK and DSTATE.
703 ;; The first two arguments to this macro are not runtime-evaluated.
704 (defmacro arg-access-macro
(arg-name format-name chunk dstate
)
705 (let* ((funstate (make-funstate (format-args (format-or-lose format-name
))))
706 (arg (arg-or-lose arg-name funstate
))
707 (arg-val-form (arg-value-form arg funstate
:adjusted
)))
708 `(flet ((local-filtered-value (offset)
709 (declare (type filtered-value-index offset
))
710 (aref (dstate-filtered-values ,dstate
) offset
))
711 (local-extract (bytespec)
712 (dchunk-extract ,chunk bytespec
)))
713 (declare (ignorable #'local-filtered-value
#'local-extract
)
714 (inline local-filtered-value local-extract
))
715 (let* ,(make-arg-temp-bindings funstate
) ,arg-val-form
))))
717 (defun arg-value-form (arg funstate
720 (allow-multiple-p (not (eq kind
:numeric
))))
721 (let ((forms (gen-arg-forms arg kind funstate
)))
722 (when (and (not allow-multiple-p
)
724 (/= (length forms
) 1))
725 (pd-error "~S must not have multiple values." arg
))
726 (maybe-listify forms
)))
728 (defun correct-dchunk-bytespec-for-endianness (bs unit-bits byte-order
)
729 (if (eq byte-order
:big-endian
)
730 (byte (byte-size bs
) (+ (byte-position bs
) (- dchunk-bits unit-bits
)))
733 (defun make-arg-temp-bindings (funstate)
734 ;; (Everything is in reverse order, so we just use PUSH, which
735 ;; results in everything being in the right order at the end.)
736 (let ((bindings nil
))
737 (dolist (ats (funstate-arg-temps funstate
))
738 (dolist (atk (cdr ats
))
739 (cond ((null (cadr atk
)))
741 (push `(,(cadr atk
) ,(cddr atk
)) bindings
))
743 (mapc (lambda (var form
)
744 (push `(,var
,form
) bindings
))
749 (defun gen-arg-forms (arg kind funstate
)
750 (multiple-value-bind (vars forms
)
751 (get-arg-temp arg kind funstate
)
753 (multiple-value-bind (new-forms single-value-p
)
754 (funcall (find-arg-form-producer kind
) arg funstate
)
755 (setq forms new-forms
)
756 (cond ((or single-value-p
(atom forms
))
757 (unless (symbolp forms
)
758 (setq vars
(gensym))))
759 ((every #'symbolp forms
)
760 ;; just use the same as the forms
763 (setq vars
(make-gensym-list (length forms
)))))
764 (set-arg-temps vars forms arg kind funstate
)))
767 (defun maybe-listify (forms)
770 ((/= (length forms
) 1)
775 (defun set-arg-from-type (arg type-name table
)
776 (let ((type-arg (find type-name table
:key
#'arg-name
)))
777 (when (null type-arg
)
778 (pd-error "unknown argument type: ~S" type-name
))
779 (setf (arg-printer arg
) (arg-printer type-arg
))
780 (setf (arg-prefilter arg
) (arg-prefilter type-arg
))
781 (setf (arg-sign-extend-p arg
) (arg-sign-extend-p type-arg
))
782 (setf (arg-use-label arg
) (arg-use-label type-arg
))))
784 (defun get-arg-temp (arg kind funstate
)
785 (let ((this-arg-temps (assoc arg
(funstate-arg-temps funstate
))))
787 (let ((this-kind-temps
788 (assoc (canonicalize-arg-form-kind kind
)
789 (cdr this-arg-temps
))))
790 (values (cadr this-kind-temps
) (cddr this-kind-temps
)))
793 (defun set-arg-temps (vars forms arg kind funstate
)
794 (let ((this-arg-temps
795 (or (assoc arg
(funstate-arg-temps funstate
))
796 (car (push (cons arg nil
) (funstate-arg-temps funstate
)))))
797 (kind (canonicalize-arg-form-kind kind
)))
798 (let ((this-kind-temps
799 (or (assoc kind
(cdr this-arg-temps
))
800 (car (push (cons kind nil
) (cdr this-arg-temps
))))))
801 (setf (cdr this-kind-temps
) (cons vars forms
)))))
803 ;;; DEFINE-ARG-TYPE Name {Key Value}*
805 ;;; Define a disassembler argument type NAME (which can then be referenced in
806 ;;; another argument definition using the :TYPE argument). &KEY args are:
808 ;;; :SIGN-EXTEND boolean
809 ;;; If non-NIL, the raw value of this argument is sign-extended.
811 ;;; :TYPE arg-type-name
812 ;;; Inherit any properties of given arg-type.
814 ;;; :PREFILTER function
815 ;;; A function which is called (along with all other prefilters,
816 ;;; in the order that their arguments appear in the instruction-
817 ;;; format) before any printing is done, to filter the raw value.
818 ;;; Any uses of READ-SUFFIX must be done inside a prefilter.
820 ;;; :PRINTER function-string-or-vector
821 ;;; A function, string, or vector which is used to print an argument of
825 ;;; If non-NIL, the value of an argument of this type is used as
826 ;;; an address, and if that address occurs inside the disassembled
827 ;;; code, it is replaced by a label. If this is a function, it is
828 ;;; called to filter the value.
829 (defmacro define-arg-type
(name &rest args
830 &key sign-extend type prefilter printer use-label
)
831 (declare (ignore sign-extend type prefilter printer use-label
))
832 (multiple-value-bind (args wrapper-defs
)
833 (munge-fun-refs args t t name
)
836 (eval-when (:compile-toplevel
:execute
)
837 (setq *disassem-arg-types
*
838 (delete ',name
*disassem-arg-types
* :key
#'arg-name
))
839 (push (modify-arg (%make-arg
',name
) nil
,@args
) *disassem-arg-types
*))
842 (defmacro def-arg-form-kind
((&rest names
) &rest inits
)
843 `(let ((kind (make-arg-form-kind :names
',names
,@inits
)))
844 ,@(mapcar (lambda (name)
845 `(setf (getf *arg-form-kinds
* ',name
) kind
))
848 (def-arg-form-kind (:raw
)
849 :producer
(lambda (arg funstate
)
850 (declare (ignore funstate
))
851 (mapcar (lambda (bytespec)
852 `(the (unsigned-byte ,(byte-size bytespec
))
853 (local-extract ',bytespec
)))
855 :checker
(lambda (new-arg old-arg
)
856 (equal (arg-fields new-arg
)
857 (arg-fields old-arg
))))
859 (def-arg-form-kind (:sign-extended
:unfiltered
)
860 :producer
(lambda (arg funstate
)
861 (let ((raw-forms (gen-arg-forms arg
:raw funstate
)))
862 (if (and (arg-sign-extend-p arg
) (listp raw-forms
))
863 (mapcar (lambda (form field
)
864 `(the (signed-byte ,(byte-size field
))
866 ,(byte-size field
))))
870 :checker
(lambda (new-arg old-arg
)
871 (equal (arg-sign-extend-p new-arg
)
872 (arg-sign-extend-p old-arg
))))
874 (defun valsrc-equal (f1 f2
)
877 (equal (value-or-source f1
)
878 (value-or-source f2
))))
880 (def-arg-form-kind (:filtering
)
881 :producer
(lambda (arg funstate
)
882 (let ((sign-extended-forms
883 (gen-arg-forms arg
:sign-extended funstate
))
884 (pf (arg-prefilter arg
)))
887 `(local-filter ,(maybe-listify sign-extended-forms
)
890 (values sign-extended-forms nil
))))
891 :checker
(lambda (new-arg old-arg
)
892 (valsrc-equal (arg-prefilter new-arg
) (arg-prefilter old-arg
))))
894 (def-arg-form-kind (:filtered
:unadjusted
)
895 :producer
(lambda (arg funstate
)
896 (let ((pf (arg-prefilter arg
)))
898 (values `(local-filtered-value ,(arg-position arg
)) t
)
899 (gen-arg-forms arg
:sign-extended funstate
))))
900 :checker
(lambda (new-arg old-arg
)
901 (let ((pf1 (arg-prefilter new-arg
))
902 (pf2 (arg-prefilter old-arg
)))
905 (= (arg-position new-arg
)
906 (arg-position old-arg
))))))
908 (def-arg-form-kind (:adjusted
:numeric
:unlabelled
)
909 :producer
(lambda (arg funstate
)
910 (let ((filtered-forms (gen-arg-forms arg
:filtered funstate
))
911 (use-label (arg-use-label arg
)))
912 (if (and use-label
(not (eq use-label t
)))
914 `(adjust-label ,(maybe-listify filtered-forms
)
915 ,(source-form use-label
)))
917 :checker
(lambda (new-arg old-arg
)
918 (valsrc-equal (arg-use-label new-arg
) (arg-use-label old-arg
))))
920 (def-arg-form-kind (:labelled
:final
)
921 :producer
(lambda (arg funstate
)
922 (let ((adjusted-forms
923 (gen-arg-forms arg
:adjusted funstate
))
924 (use-label (arg-use-label arg
)))
926 (let ((form (maybe-listify adjusted-forms
)))
927 (if (and (not (eq use-label t
))
928 (not (atom adjusted-forms
))
929 (/= (length adjusted-forms
) 1))
931 "cannot label a multiple-field argument ~
932 unless using a function: ~S" arg
)
933 `((lookup-label ,form
))))
935 :checker
(lambda (new-arg old-arg
)
936 (let ((lf1 (arg-use-label new-arg
))
937 (lf2 (arg-use-label old-arg
)))
938 (if (null lf1
) (null lf2
) t
))))
940 ;;; This is a bogus kind that's just used to ensure that printers are
942 (def-arg-form-kind (:printed
)
943 :producer
(lambda (&rest noise
)
944 (declare (ignore noise
))
945 (pd-error "bogus! can't use the :printed value of an arg!"))
946 :checker
(lambda (new-arg old-arg
)
947 (valsrc-equal (arg-printer new-arg
) (arg-printer old-arg
))))
949 (defun remember-printer-use (arg funstate
)
950 (set-arg-temps nil nil arg
:printed funstate
))
952 ;;; Returns a version of THING suitable for including in an evaluable
953 ;;; position in some form.
954 (defun source-form (thing)
955 (cond ((valsrc-p thing
)
956 (valsrc-source thing
))
959 "can't dump functions, so function ref form must be quoted: ~S"
961 ((self-evaluating-p thing
)
963 ((eq (car thing
) 'function
)
968 ;;; Return anything but a VALSRC structure.
969 (defun value-or-source (thing)
974 (defstruct (cached-fun (:conc-name cached-fun-
)
976 (funstate nil
:type
(or null funstate
))
977 (constraint nil
:type list
)
978 (name nil
:type
(or null symbol
)))
980 (defun find-cached-fun (cached-funs args constraint
)
981 (dolist (cached-fun cached-funs nil
)
982 (let ((funstate (cached-fun-funstate cached-fun
)))
983 (when (and (equal constraint
(cached-fun-constraint cached-fun
))
985 (funstate-compatible-p funstate args
)))
986 (return cached-fun
)))))
988 (defmacro !with-cached-fun
((name-var
995 (stem (missing-arg)))
996 &body defun-maker-forms
)
997 (let ((cache-var (gensym))
998 (constraint-var (gensym)))
999 `(let* ((,constraint-var
,constraint
)
1000 (,cache-var
(find-cached-fun (,cache-slot
,cache
)
1001 ,args
,constraint-var
)))
1003 (values (cached-fun-name ,cache-var
) nil
))
1008 (write-to-string (incf (fun-cache-serial-number cache
)))))
1009 (,funstate-var
(make-funstate ,args
))
1011 (make-cached-fun :name
,name-var
1012 :funstate
,funstate-var
1013 :constraint
,constraint-var
)))
1016 ,(progn ,@defun-maker-forms
)
1017 (eval-when (:compile-toplevel
:execute
)
1019 (,',cache-slot
',,cache
)))))))))))
1021 (defun find-printer-fun (printer-source args cache
)
1022 (if (null printer-source
)
1024 (let ((printer-source (preprocess-printer printer-source args
)))
1026 (name funstate cache fun-cache-printers args
1027 :constraint printer-source
1028 :stem
"INST-PRINTER-")
1029 (make-printer-defun printer-source funstate name
)))))
1031 (defun make-printer-defun (source funstate fun-name
)
1032 (let ((printer-form (compile-printer-list source funstate
))
1033 (bindings (make-arg-temp-bindings funstate
)))
1034 `(defun ,fun-name
(chunk inst stream dstate
)
1035 (declare (type dchunk chunk
)
1036 (type instruction inst
)
1037 (type stream stream
)
1038 (type disassem-state dstate
))
1039 (macrolet ((local-format-arg (arg fmt
)
1040 `(funcall (formatter ,fmt
) stream
,arg
)))
1041 (flet ((local-tab-to-arg-column ()
1042 (tab (dstate-argument-column dstate
) stream
))
1043 (local-print-name ()
1044 (princ (inst-print-name inst
) stream
))
1045 (local-write-char (ch)
1046 (write-char ch stream
))
1047 (local-princ (thing)
1048 (princ thing stream
))
1049 (local-princ16 (thing)
1050 (princ16 thing stream
))
1051 (local-call-arg-printer (arg printer
)
1052 (funcall printer arg stream dstate
))
1053 (local-call-global-printer (fun)
1054 (funcall fun chunk inst stream dstate
))
1055 (local-filtered-value (offset)
1056 (declare (type filtered-value-index offset
))
1057 (aref (dstate-filtered-values dstate
) offset
))
1058 (local-extract (bytespec)
1059 (dchunk-extract chunk bytespec
))
1061 (or (gethash lab
(dstate-label-hash dstate
))
1063 (adjust-label (val adjust-fun
)
1064 (funcall adjust-fun val dstate
)))
1065 (declare (ignorable #'local-tab-to-arg-column
1067 #'local-princ
#'local-princ16
1069 #'local-call-arg-printer
1070 #'local-call-global-printer
1072 #'local-filtered-value
1073 #'lookup-label
#'adjust-label
)
1074 (inline local-tab-to-arg-column
1075 local-princ local-princ16
1076 local-call-arg-printer local-call-global-printer
1077 local-filtered-value local-extract
1078 lookup-label adjust-label
))
1080 ,@printer-form
))))))
1082 (defun preprocess-test (subj form args
)
1083 (multiple-value-bind (subj test
)
1084 (if (and (consp form
) (symbolp (car form
)) (not (keywordp (car form
))))
1085 (values (car form
) (cdr form
))
1087 (let ((key (if (consp test
) (car test
) test
))
1088 (body (if (consp test
) (cdr test
) nil
)))
1092 ;; If no supplied constant values, just any constant is ok,
1093 ;; just see whether there's some constant value in the arg.
1097 (or (find subj args
:key
#'arg-name
)
1098 (pd-error "unknown argument ~S" subj
)))))
1099 ;; Otherwise, defer to run-time.
1110 (preprocess-test subj sub-test args
))
1114 (defun preprocess-conditionals (printer args
)
1119 (preprocess-conditionals
1120 `(:cond
((:not
,(nth 1 printer
)) ,@(nthcdr 2 printer
)))
1123 (preprocess-conditionals `(:cond
(,(cdr printer
))) args
))
1125 (preprocess-conditionals
1126 `(:cond
(,(nth 1 printer
) ,(nth 2 printer
))
1127 (t ,(nth 3 printer
)))
1135 (let ((filtered-body
1137 (lambda (sub-printer)
1138 (preprocess-conditionals sub-printer args
))
1142 (preprocess-test (find-first-field-name filtered-body
)
1150 (lambda (sub-printer)
1151 (preprocess-conditionals sub-printer args
))
1154 ;;; Return a version of the disassembly-template PRINTER with
1155 ;;; compile-time tests (e.g. :constant without a value), and any
1156 ;;; :CHOOSE operators resolved properly for the args ARGS.
1158 ;;; (:CHOOSE Sub*) simply returns the first Sub in which every field
1159 ;;; reference refers to a valid arg.
1160 (defun preprocess-printer (printer args
)
1161 (preprocess-conditionals (preprocess-chooses printer args
) args
))
1163 ;;; Return the first non-keyword symbol in a depth-first search of TREE.
1164 (defun find-first-field-name (tree)
1167 ((and (symbolp tree
) (not (keywordp tree
)))
1171 ((eq (car tree
) 'quote
)
1174 (or (find-first-field-name (car tree
))
1175 (find-first-field-name (cdr tree
))))))
1177 (defun preprocess-chooses (printer args
)
1178 (cond ((atom printer
)
1180 ((eq (car printer
) :choose
)
1181 (pick-printer-choice (cdr printer
) args
))
1183 (sharing-mapcar (lambda (sub) (preprocess-chooses sub args
))
1186 ;;;; some simple functions that help avoid consing when we're just
1187 ;;;; recursively filtering things that usually don't change
1189 (defun sharing-cons (old-cons car cdr
)
1191 "If CAR is eq to the car of OLD-CONS and CDR is eq to the CDR, return
1192 OLD-CONS, otherwise return (cons CAR CDR)."
1193 (if (and (eq car
(car old-cons
)) (eq cdr
(cdr old-cons
)))
1197 (defun sharing-mapcar (fun list
)
1198 (declare (type function fun
))
1200 "A simple (one list arg) mapcar that avoids consing up a new list
1201 as long as the results of calling FUN on the elements of LIST are
1202 eq to the original."
1205 (funcall fun
(car list
))
1206 (sharing-mapcar fun
(cdr list
)))))
1208 (defun all-arg-refs-relevant-p (printer args
)
1209 (cond ((or (null printer
) (keywordp printer
) (eq printer t
))
1212 (find printer args
:key
#'arg-name
))
1214 (every (lambda (x) (all-arg-refs-relevant-p x args
))
1218 (defun pick-printer-choice (choices args
)
1219 (dolist (choice choices
1220 (pd-error "no suitable choice found in ~S" choices
))
1221 (when (all-arg-refs-relevant-p choice args
)
1224 (defun compile-printer-list (sources funstate
)
1225 (unless (null sources
)
1226 ;; Coalesce adjacent symbols/strings, and convert to strings if possible,
1227 ;; since they require less consing to write.
1228 (do ((el (car sources
) (car sources
))
1229 (names nil
(cons (strip-quote el
) names
)))
1230 ((not (string-or-qsym-p el
))
1232 ;; concatenate adjacent strings and symbols
1234 (apply #'concatenate
1236 (mapcar #'string
(nreverse names
)))))
1237 (push (if (some #'alpha-char-p string
)
1238 `',(make-symbol string
) ; Preserve casifying output.
1242 (cons (compile-printer-body (car sources
) funstate
)
1243 (compile-printer-list (cdr sources
) funstate
))))
1245 (defun compile-printer-body (source funstate
)
1246 (cond ((null source
)
1249 `(local-print-name))
1251 `(local-tab-to-arg-column))
1253 (pd-error "unknown printer element: ~S" source
))
1255 (compile-print source funstate
))
1257 `(local-princ ',source
))
1258 ((eq (car source
) :using
)
1259 (unless (or (stringp (cadr source
))
1260 (and (listp (cadr source
))
1261 (eq (caadr source
) 'function
)))
1262 (pd-error "The first arg to :USING must be a string or #'function."))
1263 (compile-print (caddr source
) funstate
1264 (make-valsrc (eval (cadr source
)) (cadr source
))))
1265 ((eq (car source
) :plus-integer
)
1266 ;; prints the given field proceed with a + or a -
1268 (arg-value-form (arg-or-lose (cadr source
) funstate
)
1273 (local-write-char #\
+))
1274 (local-princ ,form
))))
1275 ((eq (car source
) 'quote
)
1276 `(local-princ ,source
))
1277 ((eq (car source
) 'function
)
1278 `(local-call-global-printer ,source
))
1279 ((eq (car source
) :cond
)
1280 `(cond ,@(mapcar (lambda (clause)
1281 `(,(compile-test (find-first-field-name
1285 ,@(compile-printer-list (cdr clause
)
1288 ;; :IF, :UNLESS, and :WHEN are replaced by :COND during preprocessing
1290 `(progn ,@(compile-printer-list source funstate
)))))
1292 (defun compile-print (arg-name funstate
&optional printer
)
1293 (let* ((arg (arg-or-lose arg-name funstate
))
1294 (printer (or printer
(arg-printer arg
)))
1295 (printer-val (value-or-source printer
))
1296 (printer-src (source-form printer
)))
1297 (remember-printer-use arg funstate
)
1298 (cond ((stringp printer-val
)
1299 `(local-format-arg ,(arg-value-form arg funstate
) ,printer-val
))
1300 ((vectorp printer-val
)
1303 ,(arg-value-form arg funstate
:numeric
))))
1304 ((or (functionp printer-val
)
1305 (and (consp printer-val
) (eq (car printer-val
) 'function
)))
1306 `(local-call-arg-printer ,(arg-value-form arg funstate
)
1308 ((or (null printer-val
) (eq printer-val t
))
1309 `(,(if (arg-use-label arg
) 'local-princ16
'local-princ
)
1310 ,(arg-value-form arg funstate
)))
1312 (pd-error "illegal printer: ~S" printer-src
)))))
1314 (defun string-or-qsym-p (thing)
1317 (eq (car thing
) 'quote
)
1318 (or (stringp (cadr thing
))
1319 (symbolp (cadr thing
))))))
1321 (defun strip-quote (thing)
1322 (if (and (consp thing
) (eq (car thing
) 'quote
))
1326 (defun compare-fields-form (val-form-1 val-form-2
)
1327 (flet ((listify-fields (fields)
1328 (cond ((symbolp fields
) fields
)
1329 ((every #'constantp fields
) `',fields
)
1330 (t `(list ,@fields
)))))
1331 (cond ((or (symbolp val-form-1
) (symbolp val-form-2
))
1332 `(equal ,(listify-fields val-form-1
)
1333 ,(listify-fields val-form-2
)))
1335 `(and ,@(mapcar (lambda (v1 v2
) `(= ,v1
,v2
))
1336 val-form-1 val-form-2
))))))
1338 (defun compile-test (subj test funstate
)
1339 (when (and (consp test
) (symbolp (car test
)) (not (keywordp (car test
))))
1340 (setf subj
(car test
)
1342 (let ((key (if (consp test
) (car test
) test
))
1343 (body (if (consp test
) (cdr test
) nil
)))
1349 (let* ((arg (arg-or-lose subj funstate
))
1350 (fields (arg-fields arg
))
1352 (when (not (= (length fields
) (length consts
)))
1353 (pd-error "The number of constants doesn't match number of ~
1354 fields in: (~S :constant~{ ~S~})"
1356 (compare-fields-form (gen-arg-forms arg
:numeric funstate
)
1359 `(> ,(arg-value-form (arg-or-lose subj funstate
) funstate
:numeric
)
1362 `(< ,(arg-value-form (arg-or-lose subj funstate
) funstate
:numeric
)
1365 (let ((arg1 (arg-or-lose subj funstate
))
1366 (arg2 (arg-or-lose (car body
) funstate
)))
1367 (unless (and (= (length (arg-fields arg1
))
1368 (length (arg-fields arg2
)))
1369 (every (lambda (bs1 bs2
)
1370 (= (byte-size bs1
) (byte-size bs2
)))
1373 (pd-error "can't compare differently sized fields: ~
1374 (~S :same-as ~S)" subj
(car body
)))
1375 (compare-fields-form (gen-arg-forms arg1
:numeric funstate
)
1376 (gen-arg-forms arg2
:numeric funstate
))))
1378 `(or ,@(mapcar (lambda (sub) (compile-test subj sub funstate
))
1381 `(and ,@(mapcar (lambda (sub) (compile-test subj sub funstate
))
1384 `(not ,(compile-test subj
(car body
) funstate
)))
1385 ((and (consp key
) (null body
))
1386 (compile-test subj key funstate
))
1388 (pd-error "bogus test-form: ~S" test
)))))
1390 (defun find-labeller-fun (args cache
)
1391 (let ((labelled-fields
1392 (mapcar #'arg-name
(remove-if-not #'arg-use-label args
))))
1393 (if (null labelled-fields
)
1396 (name funstate cache fun-cache-labellers args
1397 :stem
"INST-LABELLER-"
1398 :constraint labelled-fields
)
1399 (let ((labels-form 'labels
))
1401 (when (arg-use-label arg
)
1403 `(let ((labels ,labels-form
)
1405 ,(arg-value-form arg funstate
:adjusted nil
)))
1406 ;; if labeler didn't return an integer, it isn't a label
1407 (if (or (not (integerp addr
)) (assoc addr labels
))
1409 (cons (cons addr nil
) labels
))))))
1410 `(defun ,name
(chunk labels dstate
)
1411 (declare (type list labels
)
1413 (type disassem-state dstate
))
1414 (flet ((local-filtered-value (offset)
1415 (declare (type filtered-value-index offset
))
1416 (aref (dstate-filtered-values dstate
) offset
))
1417 (local-extract (bytespec)
1418 (dchunk-extract chunk bytespec
))
1419 (adjust-label (val adjust-fun
)
1420 (funcall adjust-fun val dstate
)))
1421 (declare (ignorable #'local-filtered-value
#'local-extract
1423 (inline local-filtered-value local-extract
1425 (let* ,(make-arg-temp-bindings funstate
)
1426 ,labels-form
))))))))
1428 (defun find-prefilter-fun (args cache
)
1429 (let ((filtered-args (mapcar #'arg-name
1430 (remove-if-not #'arg-prefilter args
))))
1431 (if (null filtered-args
)
1434 (name funstate cache fun-cache-prefilters args
1435 :stem
"INST-PREFILTER-"
1436 :constraint filtered-args
)
1439 (let ((pf (arg-prefilter arg
)))
1442 `(setf (local-filtered-value ,(arg-position arg
))
1444 (gen-arg-forms arg
:filtering funstate
)))))
1446 `(defun ,name
(chunk dstate
)
1447 (declare (type dchunk chunk
)
1448 (type disassem-state dstate
))
1449 (flet (((setf local-filtered-value
) (value offset
)
1450 (declare (type filtered-value-index offset
))
1451 (setf (aref (dstate-filtered-values dstate
) offset
)
1453 (local-filter (value filter
)
1454 (funcall filter value dstate
))
1455 (local-extract (bytespec)
1456 (dchunk-extract chunk bytespec
)))
1457 (declare (ignorable #'local-filter
#'local-extract
)
1458 (inline (setf local-filtered-value
)
1459 local-filter local-extract
))
1460 ;; Use them for side effects only.
1461 (let* ,(make-arg-temp-bindings funstate
)
1464 (defun compute-mask-id (args)
1465 (let ((mask dchunk-zero
)
1467 (dolist (arg args
(values mask id
))
1468 (let ((av (arg-value arg
)))
1470 (do ((fields (arg-fields arg
) (cdr fields
))
1471 (values (if (atom av
) (list av
) av
) (cdr values
)))
1473 (let ((field-mask (dchunk-make-mask (car fields
))))
1474 (when (/= (dchunk-and mask field-mask
) dchunk-zero
)
1475 (pd-error "The field ~S in arg ~S overlaps some other field."
1478 (dchunk-insertf id
(car fields
) (car values
))
1479 (dchunk-orf mask field-mask
))))))))
1481 (defun install-inst-flavors (name flavors
)
1482 (setf (gethash name
*disassem-insts
*)
1485 #!-sb-fluid
(declaim (inline bytes-to-bits
))
1486 (declaim (maybe-inline sign-extend aligned-p align tab tab0
))
1488 (defun bytes-to-bits (bytes)
1489 (declare (type disassem-length bytes
))
1490 (* bytes sb
!vm
:n-byte-bits
))
1492 (defun bits-to-bytes (bits)
1493 (declare (type disassem-length bits
))
1494 (multiple-value-bind (bytes rbits
)
1495 (truncate bits sb
!vm
:n-byte-bits
)
1496 (when (not (zerop rbits
))
1497 (error "~W bits is not a byte-multiple." bits
))
1500 (defun sign-extend (int size
)
1501 (declare (type integer int
)
1502 (type (integer 0 128) size
))
1503 (if (logbitp (1- size
) int
)
1504 (dpb int
(byte size
0) -
1)
1507 ;;; Is ADDRESS aligned on a SIZE byte boundary?
1508 (defun aligned-p (address size
)
1509 (declare (type address address
)
1510 (type alignment size
))
1511 (zerop (logand (1- size
) address
)))
1513 ;;; Return ADDRESS aligned *upward* to a SIZE byte boundary.
1514 (defun align (address size
)
1515 (declare (type address address
)
1516 (type alignment size
))
1517 (logandc1 (1- size
) (+ (1- size
) address
)))
1519 (defun tab (column stream
)
1520 (funcall (formatter "~V,1t") stream column
)
1522 (defun tab0 (column stream
)
1523 (funcall (formatter "~V,0t") stream column
)
1526 (defun princ16 (value stream
)
1527 (write value
:stream stream
:radix t
:base
16 :escape nil
))
1529 (defun read-signed-suffix (length dstate
)
1530 (declare (type (member 8 16 32 64) length
)
1531 (type disassem-state dstate
)
1532 (optimize (speed 3) (safety 0)))
1533 (sign-extend (read-suffix length dstate
) length
))
1535 ;;; All state during disassembly. We store some seemingly redundant
1536 ;;; information so that we can allow garbage collect during disassembly and
1537 ;;; not get tripped up by a code block being moved...
1538 (defstruct (disassem-state (:conc-name dstate-
)
1539 (:constructor %make-dstate
)
1541 ;; offset of current pos in segment
1542 (cur-offs 0 :type offset
)
1543 ;; offset of next position
1544 (next-offs 0 :type offset
)
1545 ;; a sap pointing to our segment
1546 (segment-sap nil
:type
(or null sb
!sys
:system-area-pointer
))
1547 ;; the current segment
1548 (segment nil
:type
(or null segment
))
1549 ;; what to align to in most cases
1550 (alignment sb
!vm
:n-word-bytes
:type alignment
)
1551 (byte-order :little-endian
1552 :type
(member :big-endian
:little-endian
))
1553 ;; for user code to hang stuff off of
1554 (properties nil
:type list
)
1555 ;; for user code to hang stuff off of, cleared each time after a
1556 ;; non-prefix instruction is processed
1557 (inst-properties nil
:type list
)
1558 (filtered-values (make-array max-filtered-value-index
)
1559 :type filtered-value-vector
)
1560 ;; used for prettifying printing
1561 (addr-print-len nil
:type
(or null
(integer 0 20)))
1562 (argument-column 0 :type column
)
1563 ;; to make output look nicer
1564 (output-state :beginning
1565 :type
(member :beginning
1569 ;; alist of (address . label-number)
1570 (labels nil
:type list
)
1571 ;; same as LABELS slot data, but in a different form
1572 (label-hash (make-hash-table) :type hash-table
)
1574 (fun-hooks nil
:type list
)
1576 ;; alist of (address . label-number), popped as it's used
1577 (cur-labels nil
:type list
)
1578 ;; OFFS-HOOKs, popped as they're used
1579 (cur-offs-hooks nil
:type list
)
1581 ;; for the current location
1582 (notes nil
:type list
)
1584 ;; currently active source variables
1585 (current-valid-locations nil
:type
(or null
(vector bit
))))
1586 (def!method print-object
((dstate disassem-state
) stream
)
1587 (print-unreadable-object (dstate stream
:type t
)
1590 (dstate-cur-offs dstate
)
1591 (dstate-segment dstate
))))
1593 ;;; Return the absolute address of the current instruction in DSTATE.
1594 (defun dstate-cur-addr (dstate)
1595 (the address
(+ (seg-virtual-location (dstate-segment dstate
))
1596 (dstate-cur-offs dstate
))))
1598 ;;; Return the absolute address of the next instruction in DSTATE.
1599 (defun dstate-next-addr (dstate)
1600 (the address
(+ (seg-virtual-location (dstate-segment dstate
))
1601 (dstate-next-offs dstate
))))
1603 ;;; Get the value of the property called NAME in DSTATE. Also SETF'able.
1605 ;;; KLUDGE: The associated run-time machinery for this is in
1606 ;;; target-disassem.lisp (much later). This is here just to make sure
1607 ;;; it's defined before it's used. -- WHN ca. 19990701
1608 (defmacro dstate-get-prop
(dstate name
)
1609 `(getf (dstate-properties ,dstate
) ,name
))
1611 ;;; Push NAME on the list of instruction properties in DSTATE.
1612 (defun dstate-put-inst-prop (dstate name
)
1613 (push name
(dstate-inst-properties dstate
)))
1615 ;;; Return non-NIL if NAME is on the list of instruction properties in
1617 (defun dstate-get-inst-prop (dstate name
)
1618 (member name
(dstate-inst-properties dstate
) :test
#'eq
))