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 ;;;; FIXME: Is it important to cache these? For performance? Or why?
69 ;;;; If performance: *Really*? How fast does disassembly need to be??
70 ;;;; So: Could we just punt this?
72 (defstruct (fun-cache (:copier nil
)
73 (:print-object
(lambda (self stream
)
74 (print-unreadable-object
75 (self stream
:type t
:identity t
)))))
76 (serial-number 0 :type fixnum
)
77 (printers nil
:type list
)
78 (labellers nil
:type list
)
79 (prefilters nil
:type list
))
81 (defvar *disassem-fun-cache
* (make-fun-cache))
82 (declaim (type fun-cache
*disassem-fun-cache
*))
84 ;;;; A DCHUNK contains the bits we look at to decode an
86 ;;;; I tried to keep this abstract so that if using integers > the machine
87 ;;;; word size conses too much, it can be changed to use bit-vectors or
90 ;;;; KLUDGE: It's not clear that using bit-vectors would be any more efficient.
91 ;;;; Perhaps the abstraction could go away. -- WHN 19991124
94 (declaim (inline dchunk-or dchunk-and dchunk-clear dchunk-not
95 dchunk-make-mask dchunk-make-field
101 (def!constant dchunk-bits
#.sb
!vm
:n-word-bits
)
104 `(unsigned-byte ,dchunk-bits
))
105 (deftype dchunk-index
()
106 `(integer 0 ,dchunk-bits
))
108 (def!constant dchunk-zero
0)
109 (def!constant dchunk-one
#.
(1- (expt 2 sb
!vm
:n-word-bits
)))
111 (defun dchunk-extract (from pos
)
112 (declare (type dchunk from
))
113 (the dchunk
(ldb pos
(the dchunk from
))))
115 (defmacro dchunk-copy
(x)
118 (defun dchunk-or (to from
)
119 (declare (type dchunk to from
))
120 (the dchunk
(logior to from
)))
121 (defun dchunk-and (to from
)
122 (declare (type dchunk to from
))
123 (the dchunk
(logand to from
)))
124 (defun dchunk-clear (to from
)
125 (declare (type dchunk to from
))
126 (the dchunk
(logandc2 to from
)))
127 (defun dchunk-not (from)
128 (declare (type dchunk from
))
129 (the dchunk
(logand dchunk-one
(lognot from
))))
131 (defmacro dchunk-andf
(to from
)
132 `(setf ,to
(dchunk-and ,to
,from
)))
133 (defmacro dchunk-orf
(to from
)
134 `(setf ,to
(dchunk-or ,to
,from
)))
135 (defmacro dchunk-clearf
(to from
)
136 `(setf ,to
(dchunk-clear ,to
,from
)))
138 (defun dchunk-make-mask (pos)
139 (the dchunk
(mask-field pos -
1)))
140 (defun dchunk-make-field (pos value
)
141 (the dchunk
(dpb value pos
0)))
143 (defmacro make-dchunk
(value)
144 `(the dchunk
,value
))
146 (defun sap-ref-dchunk (sap byte-offset byte-order
)
147 (declare (type sb
!sys
:system-area-pointer sap
)
148 (type offset byte-offset
)
149 (optimize (speed 3) (safety 0)))
152 (32 (if (eq byte-order
:big-endian
)
153 (+ (ash (sb!sys
:sap-ref-8 sap byte-offset
) 24)
154 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 16)
155 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 8)
156 (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)))
157 (+ (sb!sys
:sap-ref-8 sap byte-offset
)
158 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 8)
159 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 16)
160 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 24))))
161 (64 (if (eq byte-order
:big-endian
)
162 (+ (ash (sb!sys
:sap-ref-8 sap byte-offset
) 56)
163 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 48)
164 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 40)
165 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 32)
166 (ash (sb!sys
:sap-ref-8 sap
(+ 4 byte-offset
)) 24)
167 (ash (sb!sys
:sap-ref-8 sap
(+ 5 byte-offset
)) 16)
168 (ash (sb!sys
:sap-ref-8 sap
(+ 6 byte-offset
)) 8)
169 (sb!sys
:sap-ref-8 sap
(+ 7 byte-offset
)))
170 (+ (sb!sys
:sap-ref-8 sap byte-offset
)
171 (ash (sb!sys
:sap-ref-8 sap
(+ 1 byte-offset
)) 8)
172 (ash (sb!sys
:sap-ref-8 sap
(+ 2 byte-offset
)) 16)
173 (ash (sb!sys
:sap-ref-8 sap
(+ 3 byte-offset
)) 24)
174 (ash (sb!sys
:sap-ref-8 sap
(+ 4 byte-offset
)) 32)
175 (ash (sb!sys
:sap-ref-8 sap
(+ 5 byte-offset
)) 40)
176 (ash (sb!sys
:sap-ref-8 sap
(+ 6 byte-offset
)) 48)
177 (ash (sb!sys
:sap-ref-8 sap
(+ 7 byte-offset
)) 56)))))))
179 (defun dchunk-corrected-extract (from pos unit-bits byte-order
)
180 (declare (type dchunk from
))
181 (if (eq byte-order
:big-endian
)
182 (ldb (byte (byte-size pos
)
183 (+ (byte-position pos
) (- dchunk-bits unit-bits
)))
185 (ldb pos
(the dchunk from
))))
187 (defmacro dchunk-insertf
(place pos value
)
188 `(setf ,place
(the dchunk
(dpb ,value
,pos
(the dchunk
,place
)))))
191 (declare (type dchunk x y
))
193 (defmacro dchunk-zerop
(x)
194 `(dchunk= ,x dchunk-zero
))
196 (defun dchunk-strict-superset-p (sup sub
)
197 (and (zerop (logandc2 sub sup
))
198 (not (zerop (logandc2 sup sub
)))))
200 (defun dchunk-count-bits (x)
201 (declare (type dchunk x
))
204 (defstruct (instruction (:conc-name inst-
)
206 make-instruction
(name
212 labeller prefilter control
))
214 (name nil
:type
(or symbol string
))
215 (format-name nil
:type
(or symbol string
))
217 (mask dchunk-zero
:type dchunk
) ; bits in the inst that are constant
218 (id dchunk-zero
:type dchunk
) ; value of those constant bits
220 (length 0 :type disassem-length
) ; in bytes
222 (print-name nil
:type symbol
)
224 ;; disassembly functions
225 (prefilter nil
:type
(or null function
))
226 (labeller nil
:type
(or null function
))
227 (printer (missing-arg) :type
(or null function
))
228 (control nil
:type
(or null function
))
230 ;; instructions that are the same as this instruction but with more
232 (specializers nil
:type list
))
233 (def!method print-object
((inst instruction
) stream
)
234 (print-unreadable-object (inst stream
:type t
:identity t
)
235 (format stream
"~A(~A)" (inst-name inst
) (inst-format-name inst
))))
237 ;;;; an instruction space holds all known machine instructions in a
238 ;;;; form that can be easily searched
240 (defstruct (inst-space (:conc-name ispace-
)
242 (valid-mask dchunk-zero
:type dchunk
) ; applies to *children*
243 (choices nil
:type list
))
244 (def!method print-object
((ispace inst-space
) stream
)
245 (print-unreadable-object (ispace stream
:type t
:identity t
)))
247 ;;; now that we've defined the structure, we can declaim the type of
249 (declaim (type (or null inst-space
) *disassem-inst-space
*))
251 (defstruct (inst-space-choice (:conc-name ischoice-
)
253 (common-id dchunk-zero
:type dchunk
) ; applies to *parent's* mask
254 (subspace (missing-arg) :type
(or inst-space instruction
)))
256 ;;;; These are the kind of values we can compute for an argument, and
257 ;;;; how to compute them. The :CHECKER functions make sure that a given
258 ;;;; argument is compatible with another argument for a given use.
260 (defvar *arg-form-kinds
* nil
)
262 (defstruct (arg-form-kind (:copier nil
))
263 (names nil
:type list
)
264 (producer (missing-arg) :type function
)
265 (checker (missing-arg) :type function
))
267 (defun arg-form-kind-or-lose (kind)
268 (or (getf *arg-form-kinds
* kind
)
269 (pd-error "unknown arg-form kind ~S" kind
)))
271 (defun find-arg-form-producer (kind)
272 (arg-form-kind-producer (arg-form-kind-or-lose kind
)))
273 (defun find-arg-form-checker (kind)
274 (arg-form-kind-checker (arg-form-kind-or-lose kind
)))
276 (defun canonicalize-arg-form-kind (kind)
277 (car (arg-form-kind-names (arg-form-kind-or-lose kind
))))
279 ;;;; only used during compilation of the instructions for a backend
281 ;;;; FIXME: If only used then, isn't there some way we could do
282 ;;;; EVAL-WHEN tricks to keep this stuff from appearing in the target
285 (defvar *disassem-inst-formats
* (make-hash-table))
286 (defvar *disassem-arg-types
* nil
)
287 (defvar *disassem-fun-cache
* (make-fun-cache))
289 (defstruct (arg (:copier nil
)
291 (:constructor %make-arg
(name &optional position
))
292 (:constructor standard-make-arg
) ; only so #S readmacro works
294 (lambda (self stream
)
297 (print-unreadable-object (self stream
:type t
)
299 "~D:~A ~:[~;+~]~:S~@[=~S~]~@[ filt=~S~]~
300 ~@[ lbl=~S~]~@[ prt=~S~]"
303 (arg-sign-extend-p self
)
308 (arg-printer self
)))))))
309 (name nil
:type symbol
)
310 (fields nil
:type list
)
312 (value nil
:type
(or list integer
))
313 (sign-extend-p nil
:type
(member t nil
))
315 ;; position in a vector of prefiltered values
316 (position 0 :type fixnum
)
323 (defstruct (instruction-format (:conc-name format-
)
324 (:constructor make-inst-format
325 (name length default-printer args
))
328 (args nil
:type list
)
330 (length 0 :type disassem-length
) ; in bytes
332 (default-printer nil
:type list
))
334 ;;; A FUNSTATE holds the state of any arguments used in a disassembly
336 (defstruct (funstate (:conc-name funstate-
)
337 (:constructor %make-funstate
)
339 (args nil
:type list
)
340 (arg-temps nil
:type list
)) ; See below.
342 (defun make-funstate (args)
343 ;; give the args a position
346 (setf (arg-position arg
) i
)
348 (%make-funstate
:args args
))
350 (defun funstate-compatible-p (funstate args
)
351 (every (lambda (this-arg-temps)
352 (let* ((old-arg (car this-arg-temps
))
353 (new-arg (find (arg-name old-arg
) args
:key
#'arg-name
)))
355 (= (arg-position old-arg
) (arg-position new-arg
))
356 (every (lambda (this-kind-temps)
357 (funcall (find-arg-form-checker
358 (car this-kind-temps
))
361 (cdr this-arg-temps
)))))
362 (funstate-arg-temps funstate
)))
364 (defun arg-or-lose (name funstate
)
365 (let ((arg (find name
(funstate-args funstate
) :key
#'arg-name
)))
367 (pd-error "unknown argument ~S" name
))
370 ;;;; Since we can't include some values in compiled output as they are
371 ;;;; (notably functions), we sometimes use a VALSRC structure to keep
372 ;;;; track of the source from which they were derived.
374 (defstruct (valsrc (:constructor %make-valsrc
)
379 (defun make-valsrc (value source
)
380 (cond ((equal value source
)
382 ((and (listp value
) (eq (car value
) 'function
))
385 (%make-valsrc
:value value
:source source
))))
387 ;;; machinery to provide more meaningful error messages during compilation
388 (defvar *current-instruction-flavor
* nil
)
389 (defun pd-error (fmt &rest args
)
390 (if *current-instruction-flavor
*
391 (error "~@<in printer-definition for ~S(~S): ~3I~:_~?~:>"
392 (car *current-instruction-flavor
*)
393 (cdr *current-instruction-flavor
*)
395 (apply #'error fmt args
)))
398 ;;; 1. This should become a utility in SB!INT.
399 ;;; 2. Arrays and structures and maybe other things are
400 ;;; self-evaluating too.
401 (defun self-evaluating-p (x)
409 (defun maybe-quote (evalp form
)
410 (if (or evalp
(self-evaluating-p form
)) form
`',form
))
412 ;;; Detect things that obviously don't need wrapping, like
413 ;;; variable-refs and #'function.
414 (defun doesnt-need-wrapping-p (form)
417 (eq (car form
) 'function
)
418 (symbolp (cadr form
)))))
420 (defun make-wrapper (form arg-name funargs prefix
)
421 (if (and (listp form
)
422 (eq (car form
) 'function
))
424 (let ((wrapper-name (symbolicate prefix
"-" arg-name
"-WRAPPER"))
425 (wrapper-args (make-gensym-list (length funargs
))))
426 (values `#',wrapper-name
427 `(defun ,wrapper-name
,wrapper-args
428 (funcall ,form
,@wrapper-args
))))
430 (let ((wrapper-name (symbolicate "*" prefix
"-" arg-name
"-WRAPPER*")))
431 (values wrapper-name
`(defparameter ,wrapper-name
,form
)))))
433 (defun filter-overrides (overrides evalp
)
434 (mapcar (lambda (override)
435 (list* (car override
) (cadr override
)
436 (munge-fun-refs (cddr override
) evalp
)))
439 (defparameter *arg-fun-params
*
440 '((:printer .
(value stream dstate
))
441 (:use-label .
(value dstate
))
442 (:prefilter .
(value dstate
))))
444 (defun munge-fun-refs (params evalp
&optional wrap-defs-p
(prefix ""))
445 (let ((params (copy-list params
)))
446 (do ((tail params
(cdr tail
))
449 (values params
(nreverse wrapper-defs
)))
450 (let ((fun-arg (assoc (car tail
) *arg-fun-params
*)))
452 (let* ((fun-form (cadr tail
))
453 (quoted-fun-form `',fun-form
))
454 (when (and wrap-defs-p
(not (doesnt-need-wrapping-p fun-form
)))
455 (multiple-value-bind (access-form wrapper-def-form
)
456 (make-wrapper fun-form
(car fun-arg
) (cdr fun-arg
) prefix
)
457 (setf quoted-fun-form
`',access-form
)
458 (push wrapper-def-form wrapper-defs
)))
461 `(make-valsrc ,fun-form
,quoted-fun-form
))
465 (defun gen-args-def-form (overrides format-form
&optional
(evalp t
))
466 (let ((args-var (gensym)))
467 `(let ((,args-var
(copy-list (format-args ,format-form
))))
468 ,@(mapcar (lambda (override)
469 (update-args-form args-var
472 (cons :value
(cdr override
)))
477 (defun gen-printer-def-forms-def-form (base-name
481 (declare (type symbol base-name
))
485 &optional
(printer-form :default
)
486 &key
((:print-name print-name-form
) `',base-name
) control
)
488 (let ((format-var (gensym))
489 (field-defs (filter-overrides field-defs evalp
)))
490 `(let* ((*current-instruction-flavor
* ',(cons base-name format-name
))
491 (,format-var
(format-or-lose ',format-name
))
492 (args ,(gen-args-def-form field-defs format-var evalp
))
493 (funcache *disassem-fun-cache
*))
494 (multiple-value-bind (printer-fun printer-defun
)
495 (find-printer-fun ,(if (eq printer-form
:default
)
496 `(format-default-printer ,format-var
)
497 (maybe-quote evalp printer-form
))
499 (multiple-value-bind (labeller-fun labeller-defun
)
500 (find-labeller-fun args funcache
)
501 (multiple-value-bind (prefilter-fun prefilter-defun
)
502 (find-prefilter-fun args funcache
)
503 (multiple-value-bind (mask id
)
504 (compute-mask-id args
)
506 `(make-instruction ',',base-name
509 ,(format-length ,format-var
)
512 ,(and printer-fun
`#',printer-fun
)
513 ,(and labeller-fun
`#',labeller-fun
)
514 ,(and prefilter-fun
`#',prefilter-fun
)
517 ,@(and printer-defun
(list printer-defun
))
518 ,@(and labeller-defun
(list labeller-defun
))
519 ,@(and prefilter-defun
(list prefilter-defun
))))
522 (defun update-args-form (var name-form descrip-forms evalp
)
525 `(modify-or-add-arg ,name-form
,var
,@descrip-forms
)
526 `(apply #'modify-or-add-arg
,name-form
,var
',descrip-forms
))))
528 (defun format-or-lose (name)
529 (or (gethash name
*disassem-inst-formats
*)
530 (pd-error "unknown instruction format ~S" name
)))
532 ;;; FIXME: needed only at build-the-system time, not in running system
533 ;;; and FIXME: better syntax would allow inheriting the length to avoid
534 ;;; re-stating it needlessly in some derived formats. Perhaps:
535 ;;; (DEFINE-INSTRUCTION-FORMAT NAME (:bits N [more-format-keys]*) &rest fields)
537 (defmacro define-instruction-format
((format-name length-in-bits
538 &key default-printer include
)
541 "DEFINE-INSTRUCTION-FORMAT (Name Length {Format-Key Value}*) Arg-Def*
542 Define an instruction format NAME for the disassembler's use. LENGTH is
543 the length of the format in bits.
544 Possible FORMAT-KEYs:
546 :INCLUDE other-format-name
547 Inherit all arguments and properties of the given format. Any
548 arguments defined in the current format definition will either modify
549 the copy of an existing argument (keeping in the same order with
550 respect to when prefilters are called), if it has the same name as
551 one, or be added to the end.
552 :DEFAULT-PRINTER printer-list
553 Use the given PRINTER-LIST as a format to print any instructions of
554 this format when they don't specify something else.
556 Each ARG-DEF defines one argument in the format, and is of the form
557 (Arg-Name {Arg-Key Value}*)
559 Possible ARG-KEYs (the values are evaluated unless otherwise specified):
561 :FIELDS byte-spec-list
562 The argument takes values from these fields in the instruction. If
563 the list is of length one, then the corresponding value is supplied by
564 itself; otherwise it is a list of the values. The list may be NIL.
566 The same as :FIELDS (list byte-spec).
569 If the argument only has one field, this is the value it should have,
570 otherwise it's a list of the values of the individual fields. This can
571 be overridden in an instruction-definition or a format definition
572 including this one by specifying another, or NIL to indicate that it's
576 If non-NIL, the raw value of this argument is sign-extended,
577 immediately after being extracted from the instruction (before any
578 prefilters are run, for instance). If the argument has multiple
579 fields, they are all sign-extended.
582 Inherit any properties of the given argument type.
585 A function which is called (along with all other prefilters, in the
586 order that their arguments appear in the instruction-format) before
587 any printing is done, to filter the raw value. Any uses of READ-SUFFIX
588 must be done inside a prefilter.
590 :PRINTER function-string-or-vector
591 A function, string, or vector which is used to print this argument.
594 If non-NIL, the value of this argument is used as an address, and if
595 that address occurs inside the disassembled code, it is replaced by a
596 label. If this is a function, it is called to filter the value."
597 (let ((length-var (gensym)) ; are lengths ever non-constant? probably not.
600 (copy-list (format-args (format-or-lose include
)))))
601 added-args readers all-wrapper-defs
)
602 (dolist (arg-spec arg-specs
)
603 (let ((arg-name (car arg-spec
)))
604 (multiple-value-bind (props wrapper-defs
)
605 (munge-fun-refs (cdr arg-spec
) t t
606 (symbolicate format-name
'- arg-name
))
607 (setf all-wrapper-defs
(nconc wrapper-defs all-wrapper-defs
))
608 (let ((reader (getf props
:reader
)))
610 (setq readers
(list* #!-sb-fluid
`(declaim (inline ,reader
))
611 `(defun ,reader
(dchunk dstate
)
612 (declare (ignorable dchunk dstate
))
613 (arg-access-macro ,arg-name
,format-name
616 (remf props
:reader
))) ; ok because MUNGEing copied the plist
617 (let ((cell (member arg-name inherited-args
619 (arg-name (if (listp x
) (second x
) x
))))))
622 ,(+ (length inherited-args
) (length added-args
))
623 ,length-var
',arg-name
,@props
)
625 (props ; do nothing if no alterations
627 `(copy-arg ,(car cell
) ,length-var
,@props
))))))))
630 (eval-when (:compile-toplevel
:execute
)
631 (let ((,length-var
,length-in-bits
))
632 (setf (gethash ',format-name
*disassem-inst-formats
*)
633 (make-inst-format ',format-name
(bits-to-bytes ,length-var
)
634 ,(maybe-quote t default-printer
)
635 (list ,@inherited-args
636 ,@(nreverse added-args
))))))
639 (defun make-arg (number format-length-bits name
&rest properties
)
640 (apply #'modify-arg
(%make-arg name number
) format-length-bits properties
))
642 (defun copy-arg (arg format-length-bits
&rest properties
)
643 (apply #'modify-arg
(copy-structure arg
) format-length-bits properties
))
645 ;;; FIXME: probably needed only at build-the-system time, not in
646 ;;; final target system
647 (defun modify-or-add-arg (arg-name args
&rest properties
)
648 (declare (dynamic-extent properties
))
649 (when (get-properties properties
'(:field
:fields
))
650 (error "~@<in arg ~S: ~3I~:_~
651 can't specify fields except using DEFINE-INSTRUCTION-FORMAT~:>"
653 (let* ((cell (member arg-name args
:key
#'arg-name
))
655 (setf (car cell
) (copy-structure (car cell
)))
656 (let ((arg (%make-arg arg-name
)))
657 (setf args
(nconc args
(list arg
)))
659 (apply #'modify-arg arg nil properties
)
662 (defun modify-arg (arg format-length
663 &key
(value nil value-p
)
665 (prefilter nil prefilter-p
)
666 (printer nil printer-p
)
667 (sign-extend nil sign-extend-p
)
668 (use-label nil use-label-p
)
670 (fields nil fields-p
))
673 (error ":FIELD and :FIELDS are mutually exclusive")
674 (setf fields
(list field
) fields-p t
)))
676 (set-arg-from-type arg type
*disassem-arg-types
*))
678 (setf (arg-value arg
) value
))
680 (setf (arg-prefilter arg
) prefilter
))
682 (setf (arg-sign-extend-p arg
) sign-extend
))
684 (setf (arg-printer arg
) printer
))
686 (setf (arg-use-label arg
) use-label
))
688 (setf (arg-fields arg
)
689 (mapcar (lambda (bytespec)
690 (when (> (+ (byte-position bytespec
) (byte-size bytespec
))
692 (error "~@<in arg ~S: ~3I~:_~
693 The field ~S doesn't fit in an ~
694 instruction-format ~W bits wide.~:>"
695 (arg-name arg
) bytespec format-length
))
696 (correct-dchunk-bytespec-for-endianness
697 bytespec format-length sb
!c
:*backend-byte-order
*))
701 ;; Generate a sexpr to extract ARG-NAME of FORMAT-NAME using CHUNK and DSTATE.
702 ;; The first two arguments to this macro are not runtime-evaluated.
703 (defmacro arg-access-macro
(arg-name format-name chunk dstate
)
704 (let* ((funstate (make-funstate (format-args (format-or-lose format-name
))))
705 (arg (arg-or-lose arg-name funstate
))
706 (arg-val-form (arg-value-form arg funstate
:adjusted
)))
707 `(flet ((local-filtered-value (offset)
708 (declare (type filtered-value-index offset
))
709 (aref (dstate-filtered-values ,dstate
) offset
))
710 (local-extract (bytespec)
711 (dchunk-extract ,chunk bytespec
)))
712 (declare (ignorable #'local-filtered-value
#'local-extract
)
713 (inline local-filtered-value local-extract
))
714 (let* ,(make-arg-temp-bindings funstate
) ,arg-val-form
))))
716 (defun arg-value-form (arg funstate
719 (allow-multiple-p (not (eq kind
:numeric
))))
720 (let ((forms (gen-arg-forms arg kind funstate
)))
721 (when (and (not allow-multiple-p
)
723 (/= (length forms
) 1))
724 (pd-error "~S must not have multiple values." arg
))
725 (maybe-listify forms
)))
727 (defun correct-dchunk-bytespec-for-endianness (bs unit-bits byte-order
)
728 (if (eq byte-order
:big-endian
)
729 (byte (byte-size bs
) (+ (byte-position bs
) (- dchunk-bits unit-bits
)))
732 (defun make-arg-temp-bindings (funstate)
733 ;; (Everything is in reverse order, so we just use PUSH, which
734 ;; results in everything being in the right order at the end.)
735 (let ((bindings nil
))
736 (dolist (ats (funstate-arg-temps funstate
))
737 (dolist (atk (cdr ats
))
738 (cond ((null (cadr atk
)))
740 (push `(,(cadr atk
) ,(cddr atk
)) bindings
))
742 (mapc (lambda (var form
)
743 (push `(,var
,form
) bindings
))
748 (defun gen-arg-forms (arg kind funstate
)
749 (multiple-value-bind (vars forms
)
750 (get-arg-temp arg kind funstate
)
752 (multiple-value-bind (new-forms single-value-p
)
753 (funcall (find-arg-form-producer kind
) arg funstate
)
754 (setq forms new-forms
)
755 (cond ((or single-value-p
(atom forms
))
756 (unless (symbolp forms
)
757 (setq vars
(gensym))))
758 ((every #'symbolp forms
)
759 ;; just use the same as the forms
762 (setq vars
(make-gensym-list (length forms
)))))
763 (set-arg-temps vars forms arg kind funstate
)))
766 (defun maybe-listify (forms)
769 ((/= (length forms
) 1)
774 (defun set-arg-from-type (arg type-name table
)
775 (let ((type-arg (find type-name table
:key
#'arg-name
)))
776 (when (null type-arg
)
777 (pd-error "unknown argument type: ~S" type-name
))
778 (setf (arg-printer arg
) (arg-printer type-arg
))
779 (setf (arg-prefilter arg
) (arg-prefilter type-arg
))
780 (setf (arg-sign-extend-p arg
) (arg-sign-extend-p type-arg
))
781 (setf (arg-use-label arg
) (arg-use-label type-arg
))))
783 (defun get-arg-temp (arg kind funstate
)
784 (let ((this-arg-temps (assoc arg
(funstate-arg-temps funstate
))))
786 (let ((this-kind-temps
787 (assoc (canonicalize-arg-form-kind kind
)
788 (cdr this-arg-temps
))))
789 (values (cadr this-kind-temps
) (cddr this-kind-temps
)))
792 (defun set-arg-temps (vars forms arg kind funstate
)
793 (let ((this-arg-temps
794 (or (assoc arg
(funstate-arg-temps funstate
))
795 (car (push (cons arg nil
) (funstate-arg-temps funstate
)))))
796 (kind (canonicalize-arg-form-kind kind
)))
797 (let ((this-kind-temps
798 (or (assoc kind
(cdr this-arg-temps
))
799 (car (push (cons kind nil
) (cdr this-arg-temps
))))))
800 (setf (cdr this-kind-temps
) (cons vars forms
)))))
802 ;;; DEFINE-ARG-TYPE Name {Key Value}*
804 ;;; Define a disassembler argument type NAME (which can then be referenced in
805 ;;; another argument definition using the :TYPE argument). &KEY args are:
807 ;;; :SIGN-EXTEND boolean
808 ;;; If non-NIL, the raw value of this argument is sign-extended.
810 ;;; :TYPE arg-type-name
811 ;;; Inherit any properties of given arg-type.
813 ;;; :PREFILTER function
814 ;;; A function which is called (along with all other prefilters,
815 ;;; in the order that their arguments appear in the instruction-
816 ;;; format) before any printing is done, to filter the raw value.
817 ;;; Any uses of READ-SUFFIX must be done inside a prefilter.
819 ;;; :PRINTER function-string-or-vector
820 ;;; A function, string, or vector which is used to print an argument of
824 ;;; If non-NIL, the value of an argument of this type is used as
825 ;;; an address, and if that address occurs inside the disassembled
826 ;;; code, it is replaced by a label. If this is a function, it is
827 ;;; called to filter the value.
828 (defmacro define-arg-type
(name &rest args
829 &key sign-extend type prefilter printer use-label
)
830 (declare (ignore sign-extend type prefilter printer use-label
))
831 (multiple-value-bind (args wrapper-defs
)
832 (munge-fun-refs args t t name
)
835 (eval-when (:compile-toplevel
:execute
)
836 (setq *disassem-arg-types
*
837 (delete ',name
*disassem-arg-types
* :key
#'arg-name
))
838 (push (modify-arg (%make-arg
',name
) nil
,@args
) *disassem-arg-types
*))
841 (defmacro def-arg-form-kind
((&rest names
) &rest inits
)
842 `(let ((kind (make-arg-form-kind :names
',names
,@inits
)))
843 ,@(mapcar (lambda (name)
844 `(setf (getf *arg-form-kinds
* ',name
) kind
))
847 (def-arg-form-kind (:raw
)
848 :producer
(lambda (arg funstate
)
849 (declare (ignore funstate
))
850 (mapcar (lambda (bytespec)
851 `(the (unsigned-byte ,(byte-size bytespec
))
852 (local-extract ',bytespec
)))
854 :checker
(lambda (new-arg old-arg
)
855 (equal (arg-fields new-arg
)
856 (arg-fields old-arg
))))
858 (def-arg-form-kind (:sign-extended
:unfiltered
)
859 :producer
(lambda (arg funstate
)
860 (let ((raw-forms (gen-arg-forms arg
:raw funstate
)))
861 (if (and (arg-sign-extend-p arg
) (listp raw-forms
))
862 (mapcar (lambda (form field
)
863 `(the (signed-byte ,(byte-size field
))
865 ,(byte-size field
))))
869 :checker
(lambda (new-arg old-arg
)
870 (equal (arg-sign-extend-p new-arg
)
871 (arg-sign-extend-p old-arg
))))
873 (defun valsrc-equal (f1 f2
)
876 (equal (value-or-source f1
)
877 (value-or-source f2
))))
879 (def-arg-form-kind (:filtering
)
880 :producer
(lambda (arg funstate
)
881 (let ((sign-extended-forms
882 (gen-arg-forms arg
:sign-extended funstate
))
883 (pf (arg-prefilter arg
)))
886 `(local-filter ,(maybe-listify sign-extended-forms
)
889 (values sign-extended-forms nil
))))
890 :checker
(lambda (new-arg old-arg
)
891 (valsrc-equal (arg-prefilter new-arg
) (arg-prefilter old-arg
))))
893 (def-arg-form-kind (:filtered
:unadjusted
)
894 :producer
(lambda (arg funstate
)
895 (let ((pf (arg-prefilter arg
)))
897 (values `(local-filtered-value ,(arg-position arg
)) t
)
898 (gen-arg-forms arg
:sign-extended funstate
))))
899 :checker
(lambda (new-arg old-arg
)
900 (let ((pf1 (arg-prefilter new-arg
))
901 (pf2 (arg-prefilter old-arg
)))
904 (= (arg-position new-arg
)
905 (arg-position old-arg
))))))
907 (def-arg-form-kind (:adjusted
:numeric
:unlabelled
)
908 :producer
(lambda (arg funstate
)
909 (let ((filtered-forms (gen-arg-forms arg
:filtered funstate
))
910 (use-label (arg-use-label arg
)))
911 (if (and use-label
(not (eq use-label t
)))
913 `(adjust-label ,(maybe-listify filtered-forms
)
914 ,(source-form use-label
)))
916 :checker
(lambda (new-arg old-arg
)
917 (valsrc-equal (arg-use-label new-arg
) (arg-use-label old-arg
))))
919 (def-arg-form-kind (:labelled
:final
)
920 :producer
(lambda (arg funstate
)
921 (let ((adjusted-forms
922 (gen-arg-forms arg
:adjusted funstate
))
923 (use-label (arg-use-label arg
)))
925 (let ((form (maybe-listify adjusted-forms
)))
926 (if (and (not (eq use-label t
))
927 (not (atom adjusted-forms
))
928 (/= (length adjusted-forms
) 1))
930 "cannot label a multiple-field argument ~
931 unless using a function: ~S" arg
)
932 `((lookup-label ,form
))))
934 :checker
(lambda (new-arg old-arg
)
935 (let ((lf1 (arg-use-label new-arg
))
936 (lf2 (arg-use-label old-arg
)))
937 (if (null lf1
) (null lf2
) t
))))
939 ;;; This is a bogus kind that's just used to ensure that printers are
941 (def-arg-form-kind (:printed
)
942 :producer
(lambda (&rest noise
)
943 (declare (ignore noise
))
944 (pd-error "bogus! can't use the :printed value of an arg!"))
945 :checker
(lambda (new-arg old-arg
)
946 (valsrc-equal (arg-printer new-arg
) (arg-printer old-arg
))))
948 (defun remember-printer-use (arg funstate
)
949 (set-arg-temps nil nil arg
:printed funstate
))
951 ;;; Returns a version of THING suitable for including in an evaluable
952 ;;; position in some form.
953 (defun source-form (thing)
954 (cond ((valsrc-p thing
)
955 (valsrc-source thing
))
958 "can't dump functions, so function ref form must be quoted: ~S"
960 ((self-evaluating-p thing
)
962 ((eq (car thing
) 'function
)
967 ;;; Return anything but a VALSRC structure.
968 (defun value-or-source (thing)
973 (defstruct (cached-fun (:conc-name cached-fun-
)
975 (funstate nil
:type
(or null funstate
))
976 (constraint nil
:type list
)
977 (name nil
:type
(or null symbol
)))
979 (defun find-cached-fun (cached-funs args constraint
)
980 (dolist (cached-fun cached-funs nil
)
981 (let ((funstate (cached-fun-funstate cached-fun
)))
982 (when (and (equal constraint
(cached-fun-constraint cached-fun
))
984 (funstate-compatible-p funstate args
)))
985 (return cached-fun
)))))
987 (defmacro !with-cached-fun
((name-var
994 (stem (missing-arg)))
995 &body defun-maker-forms
)
996 (let ((cache-var (gensym))
997 (constraint-var (gensym)))
998 `(let* ((,constraint-var
,constraint
)
999 (,cache-var
(find-cached-fun (,cache-slot
,cache
)
1000 ,args
,constraint-var
)))
1002 (values (cached-fun-name ,cache-var
) nil
))
1007 (write-to-string (incf (fun-cache-serial-number cache
)))))
1008 (,funstate-var
(make-funstate ,args
))
1010 (make-cached-fun :name
,name-var
1011 :funstate
,funstate-var
1012 :constraint
,constraint-var
)))
1015 ,(progn ,@defun-maker-forms
)
1016 (eval-when (:compile-toplevel
:execute
)
1018 (,',cache-slot
',,cache
)))))))))))
1020 (defun find-printer-fun (printer-source args cache
)
1021 (if (null printer-source
)
1023 (let ((printer-source (preprocess-printer printer-source args
)))
1025 (name funstate cache fun-cache-printers args
1026 :constraint printer-source
1027 :stem
"INST-PRINTER-")
1028 (make-printer-defun printer-source funstate name
)))))
1030 (defun make-printer-defun (source funstate fun-name
)
1031 (let ((printer-form (compile-printer-list source funstate
))
1032 (bindings (make-arg-temp-bindings funstate
)))
1033 `(defun ,fun-name
(chunk inst stream dstate
)
1034 (declare (type dchunk chunk
)
1035 (type instruction inst
)
1036 (type stream stream
)
1037 (type disassem-state dstate
))
1038 (macrolet ((local-format-arg (arg fmt
)
1039 `(funcall (formatter ,fmt
) stream
,arg
)))
1040 (flet ((local-tab-to-arg-column ()
1041 (tab (dstate-argument-column dstate
) stream
))
1042 (local-print-name ()
1043 (princ (inst-print-name inst
) stream
))
1044 (local-write-char (ch)
1045 (write-char ch stream
))
1046 (local-princ (thing)
1047 (princ thing stream
))
1048 (local-princ16 (thing)
1049 (princ16 thing stream
))
1050 (local-call-arg-printer (arg printer
)
1051 (funcall printer arg stream dstate
))
1052 (local-call-global-printer (fun)
1053 (funcall fun chunk inst stream dstate
))
1054 (local-filtered-value (offset)
1055 (declare (type filtered-value-index offset
))
1056 (aref (dstate-filtered-values dstate
) offset
))
1057 (local-extract (bytespec)
1058 (dchunk-extract chunk bytespec
))
1060 (or (gethash lab
(dstate-label-hash dstate
))
1062 (adjust-label (val adjust-fun
)
1063 (funcall adjust-fun val dstate
)))
1064 (declare (ignorable #'local-tab-to-arg-column
1066 #'local-princ
#'local-princ16
1068 #'local-call-arg-printer
1069 #'local-call-global-printer
1071 #'local-filtered-value
1072 #'lookup-label
#'adjust-label
)
1073 (inline local-tab-to-arg-column
1074 local-princ local-princ16
1075 local-call-arg-printer local-call-global-printer
1076 local-filtered-value local-extract
1077 lookup-label adjust-label
))
1079 ,@printer-form
))))))
1081 (defun preprocess-test (subj form args
)
1082 (multiple-value-bind (subj test
)
1083 (if (and (consp form
) (symbolp (car form
)) (not (keywordp (car form
))))
1084 (values (car form
) (cdr form
))
1086 (let ((key (if (consp test
) (car test
) test
))
1087 (body (if (consp test
) (cdr test
) nil
)))
1091 ;; If no supplied constant values, just any constant is ok,
1092 ;; just see whether there's some constant value in the arg.
1096 (or (find subj args
:key
#'arg-name
)
1097 (pd-error "unknown argument ~S" subj
)))))
1098 ;; Otherwise, defer to run-time.
1109 (preprocess-test subj sub-test args
))
1113 (defun preprocess-conditionals (printer args
)
1118 (preprocess-conditionals
1119 `(:cond
((:not
,(nth 1 printer
)) ,@(nthcdr 2 printer
)))
1122 (preprocess-conditionals `(:cond
(,(cdr printer
))) args
))
1124 (preprocess-conditionals
1125 `(:cond
(,(nth 1 printer
) ,(nth 2 printer
))
1126 (t ,(nth 3 printer
)))
1134 (let ((filtered-body
1136 (lambda (sub-printer)
1137 (preprocess-conditionals sub-printer args
))
1141 (preprocess-test (find-first-field-name filtered-body
)
1149 (lambda (sub-printer)
1150 (preprocess-conditionals sub-printer args
))
1153 ;;; Return a version of the disassembly-template PRINTER with
1154 ;;; compile-time tests (e.g. :constant without a value), and any
1155 ;;; :CHOOSE operators resolved properly for the args ARGS.
1157 ;;; (:CHOOSE Sub*) simply returns the first Sub in which every field
1158 ;;; reference refers to a valid arg.
1159 (defun preprocess-printer (printer args
)
1160 (preprocess-conditionals (preprocess-chooses printer args
) args
))
1162 ;;; Return the first non-keyword symbol in a depth-first search of TREE.
1163 (defun find-first-field-name (tree)
1166 ((and (symbolp tree
) (not (keywordp tree
)))
1170 ((eq (car tree
) 'quote
)
1173 (or (find-first-field-name (car tree
))
1174 (find-first-field-name (cdr tree
))))))
1176 (defun preprocess-chooses (printer args
)
1177 (cond ((atom printer
)
1179 ((eq (car printer
) :choose
)
1180 (pick-printer-choice (cdr printer
) args
))
1182 (sharing-mapcar (lambda (sub) (preprocess-chooses sub args
))
1185 ;;;; some simple functions that help avoid consing when we're just
1186 ;;;; recursively filtering things that usually don't change
1188 (defun sharing-cons (old-cons car cdr
)
1190 "If CAR is eq to the car of OLD-CONS and CDR is eq to the CDR, return
1191 OLD-CONS, otherwise return (cons CAR CDR)."
1192 (if (and (eq car
(car old-cons
)) (eq cdr
(cdr old-cons
)))
1196 (defun sharing-mapcar (fun list
)
1197 (declare (type function fun
))
1199 "A simple (one list arg) mapcar that avoids consing up a new list
1200 as long as the results of calling FUN on the elements of LIST are
1201 eq to the original."
1204 (funcall fun
(car list
))
1205 (sharing-mapcar fun
(cdr list
)))))
1207 (defun all-arg-refs-relevant-p (printer args
)
1208 (cond ((or (null printer
) (keywordp printer
) (eq printer t
))
1211 (find printer args
:key
#'arg-name
))
1213 (every (lambda (x) (all-arg-refs-relevant-p x args
))
1217 (defun pick-printer-choice (choices args
)
1218 (dolist (choice choices
1219 (pd-error "no suitable choice found in ~S" choices
))
1220 (when (all-arg-refs-relevant-p choice args
)
1223 (defun compile-printer-list (sources funstate
)
1224 (unless (null sources
)
1225 ;; Coalesce adjacent symbols/strings, and convert to strings if possible,
1226 ;; since they require less consing to write.
1227 (do ((el (car sources
) (car sources
))
1228 (names nil
(cons (strip-quote el
) names
)))
1229 ((not (string-or-qsym-p el
))
1231 ;; concatenate adjacent strings and symbols
1233 (apply #'concatenate
1235 (mapcar #'string
(nreverse names
)))))
1236 (push (if (some #'alpha-char-p string
)
1237 `',(make-symbol string
) ; Preserve casifying output.
1241 (cons (compile-printer-body (car sources
) funstate
)
1242 (compile-printer-list (cdr sources
) funstate
))))
1244 (defun compile-printer-body (source funstate
)
1245 (cond ((null source
)
1248 `(local-print-name))
1250 `(local-tab-to-arg-column))
1252 (pd-error "unknown printer element: ~S" source
))
1254 (compile-print source funstate
))
1256 `(local-princ ',source
))
1257 ((eq (car source
) :using
)
1258 (unless (or (stringp (cadr source
))
1259 (and (listp (cadr source
))
1260 (eq (caadr source
) 'function
)))
1261 (pd-error "The first arg to :USING must be a string or #'function."))
1262 (compile-print (caddr source
) funstate
1263 (make-valsrc (eval (cadr source
)) (cadr source
))))
1264 ((eq (car source
) :plus-integer
)
1265 ;; prints the given field proceed with a + or a -
1267 (arg-value-form (arg-or-lose (cadr source
) funstate
)
1272 (local-write-char #\
+))
1273 (local-princ ,form
))))
1274 ((eq (car source
) 'quote
)
1275 `(local-princ ,source
))
1276 ((eq (car source
) 'function
)
1277 `(local-call-global-printer ,source
))
1278 ((eq (car source
) :cond
)
1279 `(cond ,@(mapcar (lambda (clause)
1280 `(,(compile-test (find-first-field-name
1284 ,@(compile-printer-list (cdr clause
)
1287 ;; :IF, :UNLESS, and :WHEN are replaced by :COND during preprocessing
1289 `(progn ,@(compile-printer-list source funstate
)))))
1291 (defun compile-print (arg-name funstate
&optional printer
)
1292 (let* ((arg (arg-or-lose arg-name funstate
))
1293 (printer (or printer
(arg-printer arg
)))
1294 (printer-val (value-or-source printer
))
1295 (printer-src (source-form printer
)))
1296 (remember-printer-use arg funstate
)
1297 (cond ((stringp printer-val
)
1298 `(local-format-arg ,(arg-value-form arg funstate
) ,printer-val
))
1299 ((vectorp printer-val
)
1302 ,(arg-value-form arg funstate
:numeric
))))
1303 ((or (functionp printer-val
)
1304 (and (consp printer-val
) (eq (car printer-val
) 'function
)))
1305 `(local-call-arg-printer ,(arg-value-form arg funstate
)
1307 ((or (null printer-val
) (eq printer-val t
))
1308 `(,(if (arg-use-label arg
) 'local-princ16
'local-princ
)
1309 ,(arg-value-form arg funstate
)))
1311 (pd-error "illegal printer: ~S" printer-src
)))))
1313 (defun string-or-qsym-p (thing)
1316 (eq (car thing
) 'quote
)
1317 (or (stringp (cadr thing
))
1318 (symbolp (cadr thing
))))))
1320 (defun strip-quote (thing)
1321 (if (and (consp thing
) (eq (car thing
) 'quote
))
1325 (defun compare-fields-form (val-form-1 val-form-2
)
1326 (flet ((listify-fields (fields)
1327 (cond ((symbolp fields
) fields
)
1328 ((every #'constantp fields
) `',fields
)
1329 (t `(list ,@fields
)))))
1330 (cond ((or (symbolp val-form-1
) (symbolp val-form-2
))
1331 `(equal ,(listify-fields val-form-1
)
1332 ,(listify-fields val-form-2
)))
1334 `(and ,@(mapcar (lambda (v1 v2
) `(= ,v1
,v2
))
1335 val-form-1 val-form-2
))))))
1337 (defun compile-test (subj test funstate
)
1338 (when (and (consp test
) (symbolp (car test
)) (not (keywordp (car test
))))
1339 (setf subj
(car test
)
1341 (let ((key (if (consp test
) (car test
) test
))
1342 (body (if (consp test
) (cdr test
) nil
)))
1348 (let* ((arg (arg-or-lose subj funstate
))
1349 (fields (arg-fields arg
))
1351 (when (not (= (length fields
) (length consts
)))
1352 (pd-error "The number of constants doesn't match number of ~
1353 fields in: (~S :constant~{ ~S~})"
1355 (compare-fields-form (gen-arg-forms arg
:numeric funstate
)
1358 `(> ,(arg-value-form (arg-or-lose subj funstate
) funstate
:numeric
)
1361 `(< ,(arg-value-form (arg-or-lose subj funstate
) funstate
:numeric
)
1364 (let ((arg1 (arg-or-lose subj funstate
))
1365 (arg2 (arg-or-lose (car body
) funstate
)))
1366 (unless (and (= (length (arg-fields arg1
))
1367 (length (arg-fields arg2
)))
1368 (every (lambda (bs1 bs2
)
1369 (= (byte-size bs1
) (byte-size bs2
)))
1372 (pd-error "can't compare differently sized fields: ~
1373 (~S :same-as ~S)" subj
(car body
)))
1374 (compare-fields-form (gen-arg-forms arg1
:numeric funstate
)
1375 (gen-arg-forms arg2
:numeric funstate
))))
1377 `(or ,@(mapcar (lambda (sub) (compile-test subj sub funstate
))
1380 `(and ,@(mapcar (lambda (sub) (compile-test subj sub funstate
))
1383 `(not ,(compile-test subj
(car body
) funstate
)))
1384 ((and (consp key
) (null body
))
1385 (compile-test subj key funstate
))
1387 (pd-error "bogus test-form: ~S" test
)))))
1389 (defun find-labeller-fun (args cache
)
1390 (let ((labelled-fields
1391 (mapcar #'arg-name
(remove-if-not #'arg-use-label args
))))
1392 (if (null labelled-fields
)
1395 (name funstate cache fun-cache-labellers args
1396 :stem
"INST-LABELLER-"
1397 :constraint labelled-fields
)
1398 (let ((labels-form 'labels
))
1400 (when (arg-use-label arg
)
1402 `(let ((labels ,labels-form
)
1404 ,(arg-value-form arg funstate
:adjusted nil
)))
1405 ;; if labeler didn't return an integer, it isn't a label
1406 (if (or (not (integerp addr
)) (assoc addr labels
))
1408 (cons (cons addr nil
) labels
))))))
1409 `(defun ,name
(chunk labels dstate
)
1410 (declare (type list labels
)
1412 (type disassem-state dstate
))
1413 (flet ((local-filtered-value (offset)
1414 (declare (type filtered-value-index offset
))
1415 (aref (dstate-filtered-values dstate
) offset
))
1416 (local-extract (bytespec)
1417 (dchunk-extract chunk bytespec
))
1418 (adjust-label (val adjust-fun
)
1419 (funcall adjust-fun val dstate
)))
1420 (declare (ignorable #'local-filtered-value
#'local-extract
1422 (inline local-filtered-value local-extract
1424 (let* ,(make-arg-temp-bindings funstate
)
1425 ,labels-form
))))))))
1427 (defun find-prefilter-fun (args cache
)
1428 (let ((filtered-args (mapcar #'arg-name
1429 (remove-if-not #'arg-prefilter args
))))
1430 (if (null filtered-args
)
1433 (name funstate cache fun-cache-prefilters args
1434 :stem
"INST-PREFILTER-"
1435 :constraint filtered-args
)
1438 (let ((pf (arg-prefilter arg
)))
1441 `(setf (local-filtered-value ,(arg-position arg
))
1443 (gen-arg-forms arg
:filtering funstate
)))))
1445 `(defun ,name
(chunk dstate
)
1446 (declare (type dchunk chunk
)
1447 (type disassem-state dstate
))
1448 (flet (((setf local-filtered-value
) (value offset
)
1449 (declare (type filtered-value-index offset
))
1450 (setf (aref (dstate-filtered-values dstate
) offset
)
1452 (local-filter (value filter
)
1453 (funcall filter value dstate
))
1454 (local-extract (bytespec)
1455 (dchunk-extract chunk bytespec
)))
1456 (declare (ignorable #'local-filter
#'local-extract
)
1457 (inline (setf local-filtered-value
)
1458 local-filter local-extract
))
1459 ;; Use them for side effects only.
1460 (let* ,(make-arg-temp-bindings funstate
)
1463 (defun compute-mask-id (args)
1464 (let ((mask dchunk-zero
)
1466 (dolist (arg args
(values mask id
))
1467 (let ((av (arg-value arg
)))
1469 (do ((fields (arg-fields arg
) (cdr fields
))
1470 (values (if (atom av
) (list av
) av
) (cdr values
)))
1472 (let ((field-mask (dchunk-make-mask (car fields
))))
1473 (when (/= (dchunk-and mask field-mask
) dchunk-zero
)
1474 (pd-error "The field ~S in arg ~S overlaps some other field."
1477 (dchunk-insertf id
(car fields
) (car values
))
1478 (dchunk-orf mask field-mask
))))))))
1480 (defun install-inst-flavors (name flavors
)
1481 (setf (gethash name
*disassem-insts
*)
1484 #!-sb-fluid
(declaim (inline bytes-to-bits
))
1485 (declaim (maybe-inline sign-extend aligned-p align tab tab0
))
1487 (defun bytes-to-bits (bytes)
1488 (declare (type disassem-length bytes
))
1489 (* bytes sb
!vm
:n-byte-bits
))
1491 (defun bits-to-bytes (bits)
1492 (declare (type disassem-length bits
))
1493 (multiple-value-bind (bytes rbits
)
1494 (truncate bits sb
!vm
:n-byte-bits
)
1495 (when (not (zerop rbits
))
1496 (error "~W bits is not a byte-multiple." bits
))
1499 (defun sign-extend (int size
)
1500 (declare (type integer int
)
1501 (type (integer 0 128) size
))
1502 (if (logbitp (1- size
) int
)
1503 (dpb int
(byte size
0) -
1)
1506 ;;; Is ADDRESS aligned on a SIZE byte boundary?
1507 (defun aligned-p (address size
)
1508 (declare (type address address
)
1509 (type alignment size
))
1510 (zerop (logand (1- size
) address
)))
1512 ;;; Return ADDRESS aligned *upward* to a SIZE byte boundary.
1513 (defun align (address size
)
1514 (declare (type address address
)
1515 (type alignment size
))
1516 (logandc1 (1- size
) (+ (1- size
) address
)))
1518 (defun tab (column stream
)
1519 (funcall (formatter "~V,1t") stream column
)
1521 (defun tab0 (column stream
)
1522 (funcall (formatter "~V,0t") stream column
)
1525 (defun princ16 (value stream
)
1526 (write value
:stream stream
:radix t
:base
16 :escape nil
))
1528 (defun read-signed-suffix (length dstate
)
1529 (declare (type (member 8 16 32 64) length
)
1530 (type disassem-state dstate
)
1531 (optimize (speed 3) (safety 0)))
1532 (sign-extend (read-suffix length dstate
) length
))
1534 ;;; All state during disassembly. We store some seemingly redundant
1535 ;;; information so that we can allow garbage collect during disassembly and
1536 ;;; not get tripped up by a code block being moved...
1537 (defstruct (disassem-state (:conc-name dstate-
)
1538 (:constructor %make-dstate
)
1540 ;; offset of current pos in segment
1541 (cur-offs 0 :type offset
)
1542 ;; offset of next position
1543 (next-offs 0 :type offset
)
1544 ;; a sap pointing to our segment
1545 (segment-sap nil
:type
(or null sb
!sys
:system-area-pointer
))
1546 ;; the current segment
1547 (segment nil
:type
(or null segment
))
1548 ;; what to align to in most cases
1549 (alignment sb
!vm
:n-word-bytes
:type alignment
)
1550 (byte-order :little-endian
1551 :type
(member :big-endian
:little-endian
))
1552 ;; for user code to hang stuff off of
1553 (properties nil
:type list
)
1554 ;; for user code to hang stuff off of, cleared each time after a
1555 ;; non-prefix instruction is processed
1556 (inst-properties nil
:type list
)
1557 (filtered-values (make-array max-filtered-value-index
)
1558 :type filtered-value-vector
)
1559 ;; used for prettifying printing
1560 (addr-print-len nil
:type
(or null
(integer 0 20)))
1561 (argument-column 0 :type column
)
1562 ;; to make output look nicer
1563 (output-state :beginning
1564 :type
(member :beginning
1568 ;; alist of (address . label-number)
1569 (labels nil
:type list
)
1570 ;; same as LABELS slot data, but in a different form
1571 (label-hash (make-hash-table) :type hash-table
)
1573 (fun-hooks nil
:type list
)
1575 ;; alist of (address . label-number), popped as it's used
1576 (cur-labels nil
:type list
)
1577 ;; OFFS-HOOKs, popped as they're used
1578 (cur-offs-hooks nil
:type list
)
1580 ;; for the current location
1581 (notes nil
:type list
)
1583 ;; currently active source variables
1584 (current-valid-locations nil
:type
(or null
(vector bit
))))
1585 (def!method print-object
((dstate disassem-state
) stream
)
1586 (print-unreadable-object (dstate stream
:type t
)
1589 (dstate-cur-offs dstate
)
1590 (dstate-segment dstate
))))
1592 ;;; Return the absolute address of the current instruction in DSTATE.
1593 (defun dstate-cur-addr (dstate)
1594 (the address
(+ (seg-virtual-location (dstate-segment dstate
))
1595 (dstate-cur-offs dstate
))))
1597 ;;; Return the absolute address of the next instruction in DSTATE.
1598 (defun dstate-next-addr (dstate)
1599 (the address
(+ (seg-virtual-location (dstate-segment dstate
))
1600 (dstate-next-offs dstate
))))
1602 ;;; Get the value of the property called NAME in DSTATE. Also SETF'able.
1604 ;;; KLUDGE: The associated run-time machinery for this is in
1605 ;;; target-disassem.lisp (much later). This is here just to make sure
1606 ;;; it's defined before it's used. -- WHN ca. 19990701
1607 (defmacro dstate-get-prop
(dstate name
)
1608 `(getf (dstate-properties ,dstate
) ,name
))
1610 ;;; Push NAME on the list of instruction properties in DSTATE.
1611 (defun dstate-put-inst-prop (dstate name
)
1612 (push name
(dstate-inst-properties dstate
)))
1614 ;;; Return non-NIL if NAME is on the list of instruction properties in
1616 (defun dstate-get-inst-prop (dstate name
)
1617 (member name
(dstate-inst-properties dstate
) :test
#'eq
))