disassem.lisp 131 KB
Newer Older
1
;;; -*- Package: DISASSEM -*-
ram's avatar
ram committed
2
3
;;;
;;; **********************************************************************
ram's avatar
ram committed
4
;;; This code was written as part of the CMU Common Lisp project at
ram's avatar
ram committed
5
6
7
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
8
  "$Header: src/compiler/disassem.lisp $")
ram's avatar
ram committed
9
10
11
12
13
14
15
16
;;;
;;; **********************************************************************
;;;
;;; Machine independent disassembler for CMU Common Lisp
;;;
;;; Written by Miles Bader <miles@cogsci.ed.ac.uk>
;;;

17
(in-package :disassem)
18
(intl:textdomain "cmucl")
hallgren's avatar
hallgren committed
19
20

(use-package :extensions)
ram's avatar
ram committed
21

22
23
(export '(;; for defining the instruction set
	  set-disassem-params
hallgren's avatar
hallgren committed
24
25
26
27
28
29
30
31
	  define-argument-type
	  define-instruction-format
	  install-inst-flavors

	  ;; macroexpanders
	  gen-preamble-form gen-clear-info-form
	  gen-arg-type-def-form gen-format-def-form
	  gen-printer-def-forms-def-form
32
33
34

	  ;; main user entry-points
	  disassemble
ram's avatar
ram committed
35
	  disassemble-memory
36
37
	  disassemble-function
	  disassemble-code-component
38
	  disassemble-assem-segment
39
40
41
42
43

	  ;; some variables to set
	  *opcode-column-width*
	  *note-column*

44
	  ;; slightly lower level entry-points
hallgren's avatar
hallgren committed
45
46
47
48
49
50
51
52
53
54
	  make-dstate
	  get-function-segments get-code-segments
	  label-segments disassemble-segments disassemble-segment
	  map-segment-instructions
	  segment-overflow
	  set-location-printing-range
	  add-offs-hook add-offs-note-hook add-offs-comment-hook
	  *default-dstate-hooks*
	  make-segment make-code-segment make-vector-segment make-memory-segment
	  make-offs-hook
55
56

	  ;; segment type
hallgren's avatar
hallgren committed
57
	  segment seg-sap-maker seg-length seg-virtual-location
58
59
60
61
62
63
64

	  ;; decoding a bit-pattern
	  sap-ref-dchunk
	  get-inst-space
	  find-inst

	  ;; getting at the dstate (usually from mach-dep code)
hallgren's avatar
hallgren committed
65
66
	  disassem-state dstate-cur-offs dstate-next-offs
	  dstate-segment dstate-segment-sap
ram's avatar
ram committed
67
	  dstate-get-prop
hallgren's avatar
hallgren committed
68
	  dstate-cur-addr dstate-next-addr
69

70
	  ;; random types
71
	  dchunk params instruction
72

73
	  ;; 
hallgren's avatar
hallgren committed
74
	  read-suffix read-signed-suffix
ram's avatar
ram committed
75
	  sign-extend
76

hallgren's avatar
hallgren committed
77
78
79
	  ;; useful for printers
	  princ16

80
	  ;; making handy margin notes
ram's avatar
ram committed
81
82
83
84
85
	  note
	  note-code-constant
	  maybe-note-nil-indexed-symbol-slot-ref
	  maybe-note-nil-indexed-object
	  maybe-note-assembler-routine
86
	  maybe-note-static-function
87
88
	  maybe-note-single-storage-ref
	  maybe-note-associated-storage-ref
ram's avatar
ram committed
89
	  handle-break-args
90
91
92
93
94
95
96

	  ;; taking over and printing...
	  print-notes-and-newline
	  print-current-address
	  print-bytes print-words
	  prin1-short
	  prin1-quoted-short
ram's avatar
ram committed
97
98
99
100
	  ))

;;; ----------------------------------------------------------------

101
(defvar *opcode-column-width* nil
102
  "The width of the column in which instruction-names are printed.
103
104
  NIL means use the default.  A value of zero gives the effect of not
  aligning the arguments at all.")
ram's avatar
ram committed
105
(defvar *note-column* 45
106
  "The column in which end-of-line comments for notes are started.")
ram's avatar
ram committed
107

108
(defconstant default-opcode-column-width 6)
hallgren's avatar
hallgren committed
109
(defconstant default-location-column-width 8)
110
(defconstant label-column-width 7)
111

hallgren's avatar
hallgren committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(deftype address () '(unsigned-byte 32))
(deftype alignment () '(integer 0 64))
(deftype offset () '(signed-byte 24))
(deftype length () '(unsigned-byte 24))

(deftype column () '(integer 0 1000))
(deftype text-width () '(integer 0 1000))

(defconstant max-filtered-value-index 32)
(deftype filtered-value-index ()
  `(integer 0 ,max-filtered-value-index))
(deftype filtered-value-vector ()
  `(simple-array t (,max-filtered-value-index)))

126
127
128
#+sparc
(declaim (special vm::*note-sethi-inst* vm::*pseudo-atomic-set*))

ram's avatar
ram committed
129
130
;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
131
(defmacro set-disassem-params (&rest args)
132
  "Specify global disassembler params for C:*TARGET-BACKEND*.
hallgren's avatar
hallgren committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  Keyword arguments include:
      
  :INSTRUCTION-ALIGNMENT number
      Minimum alignment of instructions, in bits.
      
  :ADDRESS-SIZE number
      Size of a machine address, in bits.
      
  :OPCODE-COLUMN-WIDTH
      Width of the column used for printing the opcode portion of the
      instruction, or NIL to use the default."
  (gen-preamble-form args))

(defmacro define-argument-type (name &rest args)
147
  "DEFINE-ARGUMENT-TYPE Name {Key Value}*
hallgren's avatar
hallgren committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  Define a disassembler argument type NAME (which can then be referenced in
  another argument definition using the :TYPE keyword argument).  Keyword
  arguments are:

  :SIGN-EXTEND boolean
      If non-NIL, the raw value of this argument is sign-extended.

  :TYPE arg-type-name
      Inherit any properties of given argument-type.

  :PREFILTER function
      A function which is called (along with all other prefilters, in the
      order that their arguments appear in the instruction- format) before
      any printing is done, to filter the raw value.  Any uses of READ-SUFFIX
      must be done inside a prefilter.
      
  :PRINTER function-string-or-vector
      A function, string, or vector which is used to print an argument of
      this type.
      
  :USE-LABEL 
      If non-NIL, the value of an argument of this type is used as an
      address, and if that address occurs inside the disassembled code, it is
      replaced by a label.  If this is a function, it is called to filter the
      value."
  (gen-arg-type-def-form name args))

(defmacro define-instruction-format (header &rest fields)
176
  "DEFINE-INSTRUCTION-FORMAT (Name Length {Format-Key Value}*) Arg-Def*
hallgren's avatar
hallgren committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  Define an instruction format NAME for the disassembler's use.  LENGTH is
  the length of the format in bits.
  Possible FORMAT-KEYs:

  :INCLUDE other-format-name
      Inherit all arguments and properties of the given format.  Any
      arguments defined in the current format definition will either modify
      the copy of an existing argument (keeping in the same order with
      respect to when pre-filter's are called), if it has the same name as
      one, or be added to the end.
  :DEFAULT-PRINTER printer-list
      Use the given PRINTER-LIST as a format to print any instructions of
      this format when they don't specify something else.

  Each ARG-DEF defines one argument in the format, and is of the form
    (Arg-Name {Arg-Key Value}*)

  Possible ARG-KEYs (the values are evaulated unless otherwise specified):
  
  :FIELDS byte-spec-list
      The argument takes values from these fields in the instruction.  If
      the list is of length one, then the corresponding value is supplied by
      itself; otherwise it is a list of the values.  The list may be NIL.
  :FIELD byte-spec
      The same as :FIELDS (list byte-spec).

  :VALUE value
      If the argument only has one field, this is the value it should have,
      otherwise it's a list of the values of the individual fields.  This can
      be overridden in an instruction-definition or a format definition
      including this one by specifying another, or NIL to indicate that it's
      variable.

  :SIGN-EXTEND boolean
      If non-NIL, the raw value of this argument is sign-extended,
      immediately after being extracted from the instruction (before any
      prefilters are run, for instance).  If the argument has multiple
      fields, they are all sign-extended.

  :TYPE arg-type-name
      Inherit any properties of the given argument-type.

  :PREFILTER function
      A function which is called (along with all other prefilters, in the
      order that their arguments appear in the instruction-format) before
      any printing is done, to filter the raw value.  Any uses of READ-SUFFIX
      must be done inside a prefilter.

  :PRINTER function-string-or-vector
      A function, string, or vector which is used to print this argument.
      
  :USE-LABEL 
      If non-NIL, the value of this argument is used as an address, and if
      that address occurs inside the disassembled code, it is replaced by a
      label.  If this is a function, it is called to filter the value."
  (gen-format-def-form header fields))
ram's avatar
ram committed
233

hallgren's avatar
hallgren committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
;;; ----------------------------------------------------------------

(declaim (inline bytes-to-bits)
	 (maybe-inline sign-extend aligned-p align tab tab0))

(defun bytes-to-bits (bytes)
  (declare (type length bytes))
  (* bytes vm:byte-bits))

(defun bits-to-bytes (bits)
  (declare (type length bits))
  (multiple-value-bind (bytes rbits)
      (truncate bits vm:byte-bits)
    (when (not (zerop rbits))
248
      (error (intl:gettext "~d bits is not a byte-multiple") bits))
hallgren's avatar
hallgren committed
249
250
251
252
253
254
255
256
257
258
    bytes))

(defun sign-extend (int size)
  (declare (type integer int)
	   (type (integer 0 128) size))
  (if (logbitp (1- size) int)
      (dpb int (byte size 0) -1)
      int))

(defun aligned-p (address size)
259
  "Returns non-NIL if ADDRESS is aligned on a SIZE byte boundary."
hallgren's avatar
hallgren committed
260
261
262
263
264
  (declare (type address address)
	   (type alignment size))
  (zerop (logand (1- size) address)))

(defun align (address size)
265
  "Return ADDRESS aligned *upward* to a SIZE byte boundary."
hallgren's avatar
hallgren committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  (declare (type address address)
	   (type alignment size))
  (logandc1 (1- size) (+ (1- size) address)))

(defun tab (column stream)
  (funcall (formatter "~v,1t") stream column)
  nil)
(defun tab0 (column stream)
  (funcall (formatter "~v,0t") stream column)
  nil)

(defun princ16 (value stream)
  (write value :stream stream :radix t :base 16 :escape nil))

;;; ----------------------------------------------------------------

(defun self-evaluating-p (x)
  (typecase x
    (null t)
    (keyword t)
    (symbol (eq x t))
    (cons nil)
    (t t)))
ram's avatar
ram committed
289

hallgren's avatar
hallgren committed
290
291
292
293
294
;;; ----------------------------------------------------------------
;;; Some simple functions that help avoid consing when we're just
;;; recursively filtering things that usually don't change.

(defun sharing-cons (old-cons car cdr)
295
  "If CAR is eq to the car of OLD-CONS and CDR is eq to the CDR, return
hallgren's avatar
hallgren committed
296
297
298
299
300
301
  OLD-CONS, otherwise return (cons CAR CDR)."
  (if (and (eq car (car old-cons)) (eq cdr (cdr old-cons)))
      old-cons
      (cons car cdr)))

(defun sharing-mapcar (fun list)
302
  "A simple (one list arg) mapcar that avoids consing up a new list
hallgren's avatar
hallgren committed
303
304
305
306
307
308
309
  as long as the results of calling FUN on the elements of LIST are
  eq to the original."
  (and list
       (sharing-cons list
		     (funcall fun (car list))
		     (sharing-mapcar fun (cdr list)))))

ram's avatar
ram committed
310
;;; ----------------------------------------------------------------
311
312
;;; A Dchunk contains the bits we look at to decode an
;;; instruction.
ram's avatar
ram committed
313
314
315
316
;;; I tried to keep this abstract so that if using integers > the machine
;;; word size conses too much, it can be changed to use bit-vectors or
;;; something.

hallgren's avatar
hallgren committed
317
318
319
320
321
322
323
(declaim (inline dchunk-or dchunk-and dchunk-clear dchunk-not
		 dchunk-make-mask dchunk-make-field
		 sap-ref-dchunk
		 dchunk-extract
		 dchunk=
		 dchunk-count-bits))

ram's avatar
ram committed
324
325
326
327
(defconstant dchunk-bits 32)

(deftype dchunk ()
  `(unsigned-byte ,dchunk-bits))
hallgren's avatar
hallgren committed
328
329
(deftype dchunk-index ()
  `(integer 0 ,dchunk-bits))
ram's avatar
ram committed
330
331
332
333
334

(defconstant dchunk-zero 0)
(defconstant dchunk-one #xFFFFFFFF)

(defmacro dchunk-copy (x)
hallgren's avatar
hallgren committed
335
  `(the dchunk ,x))
ram's avatar
ram committed
336

337
338
339
340
341
342
343
344
345
346
347
(defun dchunk-or (to from)
  (declare (type dchunk to from))
  (the dchunk (logior to from)))
(defun dchunk-and (to from)
  (declare (type dchunk to from))
  (the dchunk (logand to from)))
(defun dchunk-clear (to from)
  (declare (type dchunk to from))
  (the dchunk (logandc2 to from)))
(defun dchunk-not (from)
  (declare (type dchunk from))
hallgren's avatar
hallgren committed
348
  (the dchunk (logand dchunk-one (lognot from))))
ram's avatar
ram committed
349
350

(defmacro dchunk-andf (to from)
351
  `(setf ,to (dchunk-and ,to ,from)))
ram's avatar
ram committed
352
(defmacro dchunk-orf (to from)
353
  `(setf ,to (dchunk-or ,to ,from)))
ram's avatar
ram committed
354
(defmacro dchunk-clearf (to from)
355
  `(setf ,to (dchunk-clear ,to ,from)))
ram's avatar
ram committed
356

357
358
359
360
(defun dchunk-make-mask (pos)
  (the dchunk (mask-field pos -1)))
(defun dchunk-make-field (pos value)
  (the dchunk (dpb value pos 0)))
ram's avatar
ram committed
361
362

(defmacro make-dchunk (value)
hallgren's avatar
hallgren committed
363
  `(the dchunk ,value))
ram's avatar
ram committed
364
365
366

(defun sap-ref-dchunk (sap byte-offset byte-order)
  (declare (type system:system-area-pointer sap)
hallgren's avatar
hallgren committed
367
368
369
370
371
372
373
374
375
376
377
378
	   (type offset byte-offset)
	   (optimize (speed 3) (safety 0)))
  (the dchunk
       (if (eq byte-order :big-endian)
	   (+ (ash (system:sap-ref-8 sap byte-offset) 24)
	      (ash (system:sap-ref-8 sap (+ 1 byte-offset)) 16)
	      (ash (system:sap-ref-8 sap (+ 2 byte-offset)) 8)
	      (system:sap-ref-8 sap (+ 3 byte-offset)))
	   (+ (system:sap-ref-8 sap byte-offset)
	      (ash (system:sap-ref-8 sap (+ 1 byte-offset)) 8)
	      (ash (system:sap-ref-8 sap (+ 2 byte-offset)) 16)
	      (ash (system:sap-ref-8 sap (+ 3 byte-offset)) 24)))))
ram's avatar
ram committed
379
380
381
382
383
384

(defun correct-dchunk-bytespec-for-endianness (bs unit-bits byte-order)
  (if (eq byte-order :big-endian)
      (byte (byte-size bs) (+ (byte-position bs) (- dchunk-bits unit-bits)))
      bs))

385
386
(defun dchunk-extract (from pos)
  (declare (type dchunk from))
hallgren's avatar
hallgren committed
387
  (the dchunk (ldb pos (the dchunk from))))
ram's avatar
ram committed
388

389
390
391
(defun dchunk-corrected-extract (from pos unit-bits byte-order)
  (declare (type dchunk from))
  (if (eq byte-order :big-endian)
hallgren's avatar
hallgren committed
392
393
      (ldb (byte (byte-size pos)
		 (+ (byte-position pos) (- dchunk-bits unit-bits)))
394
395
396
	   (the dchunk from))
      (ldb pos (the dchunk from))))

ram's avatar
ram committed
397
(defmacro dchunk-insertf (place pos value)
398
  `(setf ,place (the dchunk (dpb ,value ,pos (the dchunk,place)))))
ram's avatar
ram committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

(defun dchunk= (x y)
  (declare (type dchunk x y))
  (= x y))
(defmacro dchunk-zerop (x)
  `(dchunk= ,x dchunk-zero))

(defun dchunk-strict-superset-p (sup sub)
  (and (zerop (logandc2 sub sup))
       (not (zerop (logandc2 sup sub)))))

(defun dchunk-count-bits (x)
  (declare (type dchunk x))
  (logcount x))

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
416
417
418
419
420
421
422
423
(defstruct (params (:print-function %print-params))
  (instructions (make-hash-table :test #'eq) :type hash-table)
  (inst-space nil :type (or null inst-space))
  (instruction-alignment vm:word-bytes :type alignment)
  (location-column-width default-location-column-width :type text-width)
  (opcode-column-width default-opcode-column-width :type (or null text-width))
  (backend (required-argument) :type c::backend) ; for convenience
  )
424

hallgren's avatar
hallgren committed
425
426
427
428
429
(defun %print-params (params stream level)
  (declare (ignore level))
  (print-unreadable-object (params stream :type t)
    (when (params-backend params)
      (prin1 (c:backend-name (params-backend params)) stream))))
430
431

;;; ----------------------------------------------------------------
hallgren's avatar
hallgren committed
432
;;; Only used during compilation of the instructions for a backend
433

hallgren's avatar
hallgren committed
434
435
436
(defstruct (argument (:conc-name arg-))
  (name nil :type symbol)
  (fields nil :type list)
ram's avatar
ram committed
437

hallgren's avatar
hallgren committed
438
439
  (value nil :type (or list integer))
  (sign-extend-p nil :type (member t nil))
ram's avatar
ram committed
440

hallgren's avatar
hallgren committed
441
442
  ;; position in a vector of prefiltered values
  (position 0 :type fixnum)
ram's avatar
ram committed
443

hallgren's avatar
hallgren committed
444
445
446
447
448
  ;; functions to use
  (printer nil)
  (prefilter nil)
  (use-label nil)
  )
ram's avatar
ram committed
449

hallgren's avatar
hallgren committed
450
451
452
(defstruct (instruction-format (:conc-name format-))
  (name nil)
  (args nil :type list)
ram's avatar
ram committed
453

hallgren's avatar
hallgren committed
454
  (length 0 :type length)		; in bytes
ram's avatar
ram committed
455

hallgren's avatar
hallgren committed
456
  (default-printer nil :type list)
ram's avatar
ram committed
457
458
  )

hallgren's avatar
hallgren committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
;;; ----------------------------------------------------------------
;;; 

(defstruct (instruction (:conc-name inst-)
			(:print-function %print-instruction)
			(:constructor
			 make-instruction (name
					   format-name
					   print-name
					   length
					   mask id
					   printer
					   labeller prefilter control)))
  (name nil :type (or symbol string))
  (format-name nil :type (or symbol string))

  (mask dchunk-zero :type dchunk)	; bits in the inst that are constant
  (id dchunk-zero :type dchunk)		; value of those constant bits

  (length 0 :type length)		; in bytes

  (print-name nil :type symbol)

  ;; disassembly functions
  (prefilter nil :type (or null function))
  (labeller nil :type (or null function))
  (printer (required-argument) :type (or null function))
  (control nil :type (or null function))

  ;; instructions that are the same as this instruction but with more
  ;; constraints
  (specializers nil :type list)
ram's avatar
ram committed
491
492
  )

hallgren's avatar
hallgren committed
493
494
495
496
(defun %print-instruction (inst stream depth)
  (declare (ignore depth))
  (print-unreadable-object (inst stream :type t :identity t)
    (format stream "~a(~a)" (inst-name inst) (inst-format-name inst))))
ram's avatar
ram committed
497
498

;;; ----------------------------------------------------------------
hallgren's avatar
hallgren committed
499
;;; provide more meaningful error messages during compilation
ram's avatar
ram committed
500

hallgren's avatar
hallgren committed
501
(defvar *current-instruction-flavor* nil)
ram's avatar
ram committed
502

hallgren's avatar
hallgren committed
503
504
505
506
507
508
509
(defun pd-error (fmt &rest args)
  (if *current-instruction-flavor*
      (error "~@<In printer-definition for ~s(~s):  ~3i~:_~?~:>"
	     (car *current-instruction-flavor*)
	     (cdr *current-instruction-flavor*)
	     fmt args)
      (apply #'error fmt args)))
ram's avatar
ram committed
510

hallgren's avatar
hallgren committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
;;; ----------------------------------------------------------------
;;; Since we can't include some values in compiled output as they are
;;; (notably functions), we sometimes use a valsrc structure to keep track of
;;; the source from which they were derived.

(defstruct (valsrc (:constructor %make-valsrc))
  (value nil)
  (source nil))

;;; Returns a version of THING suitable for including in an evaluable
;;; position in some form.
(defun source-form (thing)
  (cond ((valsrc-p thing)
	 (valsrc-source thing))
	((functionp thing)
	 (pd-error
527
	  (intl:gettext "Can't dump functions, so function ref form must be quoted: ~s")
hallgren's avatar
hallgren committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
	  thing))
	((self-evaluating-p thing)
	 thing)
	((eq (car thing) 'function)
	 thing)
	(t
	 `',thing)))

;;; Returns anything but a valsrc structure.
(defun value-or-source (thing)
  (if (valsrc-p thing)
      (valsrc-value thing)
      thing))

(defun make-valsrc (value source)
  (cond ((equal value source)
	 source)
	((and (listp value) (eq (car value) 'function))
	 value)
	(t
	 (%make-valsrc :value value :source source))))
ram's avatar
ram committed
549
550

;;; ----------------------------------------------------------------
hallgren's avatar
hallgren committed
551
552
;;; A funstate holds the state of any arguments used in a disassembly
;;; function.
ram's avatar
ram committed
553

hallgren's avatar
hallgren committed
554
555
556
557
(defstruct (funstate (:conc-name funstate-) (:constructor %make-funstate))
  (args nil :type list)
  (arg-temps nil :type list)		; see below
  )
ram's avatar
ram committed
558

hallgren's avatar
hallgren committed
559
560
561
562
563
564
565
566
567
568
569
(defun make-funstate (args)
  ;; give the args a position
  (let ((i 0))
    (dolist (arg args)
      (setf (arg-position arg) i)
      (incf i)))
  (%make-funstate :args args))

(defun arg-or-lose (name funstate)
  (let ((arg (find name (funstate-args funstate) :key #'arg-name)))
    (when (null arg)
570
      (pd-error (intl:gettext "Unknown argument ~s") name))
hallgren's avatar
hallgren committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
    arg))

(defun get-arg-temp (arg kind funstate)
  (let ((this-arg-temps (assoc arg (funstate-arg-temps funstate))))
    (if this-arg-temps
	(let ((this-kind-temps
	       (assoc (canonicalize-arg-form-kind kind)
		      (cdr this-arg-temps))))
	  (values (cadr this-kind-temps) (cddr this-kind-temps)))
	(values nil nil))))

(defun make-arg-temp-bindings (funstate)
  ;; Everything is in reverse order, so we just use push, which results in
  ;; everything being in the right order at the end.
  (let ((bindings nil))
    (dolist (ats (funstate-arg-temps funstate))
      (dolist (atk (cdr ats))
	(cond ((null (cadr atk)))
	      ((atom (cadr atk))
	       (push `(,(cadr atk) ,(cddr atk)) bindings))
	      (t
	       (mapc #'(lambda (var form)
			 (push `(,var ,form) bindings))
		     (cadr atk)
		     (cddr atk))))))
    bindings))

(defun set-arg-temps (vars forms arg kind funstate)
  (let ((this-arg-temps
	 (or (assoc arg (funstate-arg-temps funstate))
	     (car (push (cons arg nil) (funstate-arg-temps funstate)))))
	(kind (canonicalize-arg-form-kind kind)))
    (let ((this-kind-temps
	   (or (assoc kind (cdr this-arg-temps))
	       (car (push (cons kind nil) (cdr this-arg-temps))))))
      (setf (cdr this-kind-temps) (cons vars forms)))))

(defun gen-arg-forms (arg kind funstate)
  (multiple-value-bind (vars forms)
      (get-arg-temp arg kind funstate)
    (when (null forms)
      (multiple-value-bind (new-forms single-value-p)
	  (funcall (find-arg-form-producer kind) arg funstate)
	(setq forms new-forms)
	(cond ((or single-value-p (atom forms))
	       (unless (symbolp forms)
		 (setq vars (gensym))))
	      ((every #'symbolp forms)
	       ;; just use the same as the forms
	       (setq vars nil))
	      (t
	       (setq vars nil)
	       (dotimes (i (length forms))
		 (push (gensym) vars))))
	(set-arg-temps vars forms arg kind funstate)))
    (or vars forms)))

(defun funstate-compatible-p (funstate args)
  (every #'(lambda (this-arg-temps)
	     (let* ((old-arg (car this-arg-temps))
		    (new-arg (find (arg-name old-arg) args :key #'arg-name)))
	       (and new-arg
		    (every #'(lambda (this-kind-temps)
			       (funcall (find-arg-form-checker
					 (car this-kind-temps))
					new-arg
					old-arg))
			   (cdr this-arg-temps)))))
	 (funstate-arg-temps funstate)))

(defun maybe-listify (forms)
  (cond ((atom forms)
	 forms)
	((/= (length forms) 1)
	 `(list ,@forms))
	(t
	 (car forms))))

(defun arg-value-form (arg funstate
		       &optional
		       (kind :final)
		       (allow-multiple-p (not (eq kind :numeric))))
  (let ((forms (gen-arg-forms arg kind funstate)))
    (when (and (not allow-multiple-p)
	       (listp forms)
	       (/= (length forms) 1))
657
      (pd-error (intl:gettext "~s must not have multiple values") arg))
hallgren's avatar
hallgren committed
658
    (maybe-listify forms)))
ram's avatar
ram committed
659

hallgren's avatar
hallgren committed
660
661
662
663
;;; ----------------------------------------------------------------
;;; These are the kind of values we can compute for an argument, and
;;; how to compute them.  The :checker functions make sure that a given
;;; argument is compatible with another argument for a given use.
ram's avatar
ram committed
664

hallgren's avatar
hallgren committed
665
(defvar *arg-form-kinds* nil)
ram's avatar
ram committed
666

hallgren's avatar
hallgren committed
667
668
669
670
(defstruct arg-form-kind
  (names nil :type list)
  (producer (required-argument) :type function)
  (checker (required-argument) :type function)
ram's avatar
ram committed
671
672
  )

hallgren's avatar
hallgren committed
673
674
(defun arg-form-kind-or-lose (kind)
  (or (getf *arg-form-kinds* kind)
675
      (pd-error (intl:gettext "Unknown arg-form kind ~s") kind)))
hallgren's avatar
hallgren committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716

(defun find-arg-form-producer (kind)
  (arg-form-kind-producer (arg-form-kind-or-lose kind)))
(defun find-arg-form-checker (kind)
  (arg-form-kind-checker (arg-form-kind-or-lose kind)))

(defun canonicalize-arg-form-kind (kind)
  (car (arg-form-kind-names (arg-form-kind-or-lose kind))))

(defmacro def-arg-form-kind ((&rest names) &rest inits)
  `(let ((kind (make-arg-form-kind :names ',names ,@inits)))
     ,@(mapcar #'(lambda (name)
		   `(setf (getf *arg-form-kinds* ',name) kind))
	       names)))

(def-arg-form-kind (:raw)
  :producer #'(lambda (arg funstate)
		(declare (ignore funstate))
		(mapcar #'(lambda (bytespec)
			    `(the (unsigned-byte ,(byte-size bytespec))
				  (local-extract ',bytespec)))
			(arg-fields arg)))
  :checker #'(lambda (new-arg old-arg)
	       (equal (arg-fields new-arg)
		      (arg-fields old-arg))))

(def-arg-form-kind (:sign-extended :unfiltered)
  :producer #'(lambda (arg funstate)
		(let ((raw-forms (gen-arg-forms arg :raw funstate)))
		  (if (and (arg-sign-extend-p arg) (listp raw-forms))
		      (mapcar #'(lambda (form field)
				  `(the (signed-byte ,(byte-size field))
					(sign-extend ,form
						     ,(byte-size field))))
			      raw-forms
			      (arg-fields arg))
		      raw-forms)))
  :checker #'(lambda (new-arg old-arg)
	       (equal (arg-sign-extend-p new-arg)
		      (arg-sign-extend-p old-arg))))

hallgren's avatar
hallgren committed
717
718
719
720
721
722
(defun valsrc-equal (f1 f2)
  (if (null f1)
      (null f2)
      (equal (value-or-source f1)
	     (value-or-source f2))))

hallgren's avatar
hallgren committed
723
724
725
726
727
728
729
730
731
732
733
734
(def-arg-form-kind (:filtering)
  :producer #'(lambda (arg funstate)
		(let ((sign-extended-forms
		       (gen-arg-forms arg :sign-extended funstate))
		      (pf (arg-prefilter arg)))
		  (if pf
		      (values
		       `(local-filter ,(maybe-listify sign-extended-forms)
				      ,(source-form pf))
		       t)
		      (values sign-extended-forms nil))))
  :checker #'(lambda (new-arg old-arg)
hallgren's avatar
hallgren committed
735
	       (valsrc-equal (arg-prefilter new-arg) (arg-prefilter old-arg))))
hallgren's avatar
hallgren committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760

(def-arg-form-kind (:filtered :unadjusted)
  :producer #'(lambda (arg funstate)
		(let ((pf (arg-prefilter arg)))
		  (if pf
		      (values `(local-filtered-value ,(arg-position arg)) t)
		      (gen-arg-forms arg :sign-extended funstate))))
  :checker #'(lambda (new-arg old-arg)
	       (let ((pf1 (arg-prefilter new-arg))
		     (pf2 (arg-prefilter old-arg)))
		 (if (null pf1)
		     (null pf2)
		     (= (arg-position new-arg)
			(arg-position old-arg))))))

(def-arg-form-kind (:adjusted :numeric :unlabelled)
  :producer #'(lambda (arg funstate)
		(let ((filtered-forms (gen-arg-forms arg :filtered funstate))
		      (use-label (arg-use-label arg)))
		  (if (and use-label (not (eq use-label t)))
		      (list
		       `(adjust-label ,(maybe-listify filtered-forms)
				      ,(source-form use-label)))
		      filtered-forms)))
  :checker #'(lambda (new-arg old-arg)
hallgren's avatar
hallgren committed
761
	       (valsrc-equal (arg-use-label new-arg) (arg-use-label old-arg))))
hallgren's avatar
hallgren committed
762
763
764
765
766
767
768
769
770
771
772
773

(def-arg-form-kind (:labelled :final)
  :producer #'(lambda (arg funstate)
		(let ((adjusted-forms
		       (gen-arg-forms arg :adjusted funstate))
		      (use-label (arg-use-label arg)))
		  (if use-label
		      (let ((form (maybe-listify adjusted-forms)))
			(if (and (not (eq use-label t))
				 (not (atom adjusted-forms))
				 (/= (Length adjusted-forms) 1))
			    (pd-error
774
775
			     (intl:gettext "Cannot label a multiple-field argument ~
			      unless using a function: ~s") arg)
hallgren's avatar
hallgren committed
776
777
778
779
780
781
			    `((lookup-label ,form))))
		      adjusted-forms)))
  :checker #'(lambda (new-arg old-arg)
	       (let ((lf1 (arg-use-label new-arg))
		     (lf2 (arg-use-label old-arg)))
		 (if (null lf1) (null lf2) t))))
ram's avatar
ram committed
782

hallgren's avatar
hallgren committed
783
784
785
786
787
;;; This is a bogus kind that's just used to ensure that printers are
;;; compatible...
(def-arg-form-kind (:printed)
  :producer #'(lambda (&rest noise)
		(declare (ignore noise))
788
		(pd-error (intl:gettext "Bogus!  Can't use the :printed value of an arg!")))
hallgren's avatar
hallgren committed
789
790
791
792
793
794
  :checker #'(lambda (new-arg old-arg)
	       (valsrc-equal (arg-printer new-arg) (arg-printer old-arg))))

(defun remember-printer-use (arg funstate)
  (set-arg-temps nil nil arg :printed funstate))

795
796
;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
797
798
799
800
801
802
803
804
(defun compare-fields-form (val-form-1 val-form-2)
  (flet ((listify-fields (fields)
	   (cond ((symbolp fields) fields)
		 ((every #'constantp fields) `',fields)
		 (t `(list ,@fields)))))
    (cond ((or (symbolp val-form-1) (symbolp val-form-2))
	   `(equal ,(listify-fields val-form-1)
		   ,(listify-fields val-form-2)))
805
	  (t
hallgren's avatar
hallgren committed
806
807
808
809
810
811
812
813
814
815
	   `(and ,@(mapcar #'(lambda (v1 v2) `(= ,v1 ,v2))
			   val-form-1 val-form-2))))))

(defun compile-test (subj test funstate)
  (when (and (consp test) (symbolp (car test)) (not (keywordp (car test))))
    (setf subj (car test)
	  test (cdr test)))
  (let ((key (if (consp test) (car test) test))
	(body (if (consp test) (cdr test) nil)))
    (cond ((null key)
816
	   nil)
hallgren's avatar
hallgren committed
817
818
819
820
821
822
823
	  ((eq key t)
	   t)
	  ((eq key :constant)
	   (let* ((arg (arg-or-lose subj funstate))
		  (fields (arg-fields arg))
		  (consts body))
	     (when (not (= (length fields) (length consts)))
824
825
	       (pd-error (intl:gettext "number of constants doesn't match number of fields ~
			  in: (~s :constant~{ ~s~})")
hallgren's avatar
hallgren committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
			 subj body))
	     (compare-fields-form (gen-arg-forms arg :numeric funstate)
				  consts)))
	  ((eq key :positive)
	   `(> ,(arg-value-form (arg-or-lose subj funstate) funstate :numeric)
	       0))
	  ((eq key :negative)
	   `(< ,(arg-value-form (arg-or-lose subj funstate) funstate :numeric)
	       0))
	  ((eq key :same-as)
	   (let ((arg1 (arg-or-lose subj funstate))
		 (arg2 (arg-or-lose (car body) funstate)))
	     (unless (and (= (length (arg-fields arg1))
			     (length (arg-fields arg2)))
			  (every #'(lambda (bs1 bs2)
				     (= (byte-size bs1) (byte-size bs2)))
				 (arg-fields arg1)
				 (arg-fields arg2)))
844
845
	       (pd-error (intl:gettext "Can't compare differently sized fields: ~
		          (~s :same-as ~s)") subj (car body)))
hallgren's avatar
hallgren committed
846
847
848
849
850
851
852
853
854
855
856
857
858
	     (compare-fields-form (gen-arg-forms arg1 :numeric funstate)
				  (gen-arg-forms arg2 :numeric funstate))))
	  ((eq key :or)
	   `(or ,@(mapcar #'(lambda (sub) (compile-test subj sub funstate))
			  body)))
	  ((eq key :and)
	   `(and ,@(mapcar #'(lambda (sub) (compile-test subj sub funstate))
			   body)))
	  ((eq key :not)
	   `(not ,(compile-test subj (car body) funstate)))
	  ((and (consp key) (null body))
	   (compile-test subj key funstate))
	  (t
859
	   (pd-error (intl:gettext "Bogus test-form: ~s") test)))))
hallgren's avatar
hallgren committed
860
861
862

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
863
(defun find-first-field-name (tree)
864
  "Returns the first non-keyword symbol in a depth-first search of TREE."
hallgren's avatar
hallgren committed
865
866
867
868
869
870
  (cond ((null tree)
	 nil)
	((and (symbolp tree) (not (keywordp tree)))
	 tree)
	((atom tree)
	 nil)
hallgren's avatar
hallgren committed
871
872
	((eq (car tree) 'quote)
	 nil)
hallgren's avatar
hallgren committed
873
	(t
hallgren's avatar
hallgren committed
874
875
	 (or (find-first-field-name (car tree))
	     (find-first-field-name (cdr tree))))))
hallgren's avatar
hallgren committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914

(defun string-or-qsym-p (thing)
  (or (stringp thing)
      (and (consp thing)
	   (eq (car thing) 'quote)
	   (or (stringp (cadr thing))
	       (symbolp (cadr thing))))))

(defun strip-quote (thing)
  (if (and (consp thing) (eq (car thing) 'quote))
      (cadr thing)
      thing))

(defun compile-printer-list (sources funstate)
  (unless (null sources)
    ;; Coalesce adjacent symbols/strings, and convert to strings if possible,
    ;; since they require less consing to write.
    (do ((el (car sources) (car sources))
	 (names nil (cons (strip-quote el) names)))
	((not (string-or-qsym-p el))
	 (when names
	   ;; concatenate adjacent strings and symbols
	   (let ((string
		  (apply #'concatenate
			 'string
			 (mapcar #'string (nreverse names)))))
	     (push (if (some #'alpha-char-p string)
		       `',(make-symbol string) ; preserve casifying output
		       string)
		   sources))))
      (pop sources))
    (cons (compile-printer-body (car sources) funstate)
	  (compile-printer-list (cdr sources) funstate))))

(defun compile-print (arg-name funstate &optional printer)
  (let* ((arg (arg-or-lose arg-name funstate))
	 (printer (or printer (arg-printer arg)))
	 (printer-val (value-or-source printer))
	 (printer-src (source-form printer)))
hallgren's avatar
hallgren committed
915
    (remember-printer-use arg funstate)
hallgren's avatar
hallgren committed
916
917
918
919
920
921
922
923
924
925
926
927
928
    (cond ((stringp printer-val)
	   `(local-format-arg ,(arg-value-form arg funstate) ,printer-val))
	  ((vectorp printer-val)
	   `(local-princ
	     (aref ,printer-src
		   ,(arg-value-form arg funstate :numeric))))
	  ((or (functionp printer-val)
	       (and (consp printer-val) (eq (car printer-val) 'function)))
	   `(local-call-arg-printer ,(arg-value-form arg funstate)
				    ,printer-src))
	  ((or (null printer-val) (eq printer-val t))
	   `(,(if (arg-use-label arg) 'local-princ16 'local-princ)
	     ,(arg-value-form arg funstate)))
929
	  (t
930
	   (pd-error (intl:gettext "Illegal printer: ~s") printer-src)))))
hallgren's avatar
hallgren committed
931
932
933
934
935
936
937
938
939

(defun compile-printer-body (source funstate)
  (cond ((null source)
	 nil)
	((eq source :name)
	 `(local-print-name))
	((eq source :tab)
	 `(local-tab-to-arg-column))
	((keywordp source)
940
	 (pd-error (intl:gettext "Unknown printer element: ~s") source))
hallgren's avatar
hallgren committed
941
942
943
944
945
946
947
948
	((symbolp source)
	 (compile-print source funstate))
	((atom source)
	 `(local-princ ',source))
	((eq (car source) :using)
	 (unless (or (stringp (cadr source))
		     (and (listp (cadr source))
			  (eq (caadr source) 'function)))
949
	   (pd-error (intl:gettext "First arg to :USING must be a string or #'function")))
hallgren's avatar
hallgren committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
	 (compile-print (caddr source) funstate
			(cons (eval (cadr source)) (cadr source))))
	((eq (car source) :plus-integer)
	 ;; prints the given field proceed with a + or a -
	 (let ((form
		(arg-value-form (arg-or-lose (cadr source) funstate)
				funstate
				:numeric)))
	   `(progn
	      (when (>= ,form 0)
		(local-write-char #\+))
	      (local-princ ,form))))
	((eq (car source) 'quote)
	 `(local-princ ,source))
	((eq (car source) 'function)
	 `(local-call-global-printer ,source))
	((eq (car source) :cond)
	 `(cond ,@(mapcar #'(lambda (clause)
hallgren's avatar
hallgren committed
968
			      `(,(compile-test (find-first-field-name
hallgren's avatar
hallgren committed
969
970
971
972
973
974
						(cdr clause))
					       (car clause)
					       funstate)
				,@(compile-printer-list (cdr clause)
							funstate)))
			  (cdr source))))
hallgren's avatar
hallgren committed
975
	;; :if, :unless, and :when are replaced by :cond during preprocessing
hallgren's avatar
hallgren committed
976
977
978
979
	(t
	 `(progn ,@(compile-printer-list source funstate)))))

;;; ----------------------------------------------------------------
ram's avatar
ram committed
980
;;; Note that these things are compiled byte compiled to save space.
hallgren's avatar
hallgren committed
981
982
983
984
985
986
987
988
989

(defun make-printer-defun (source funstate function-name)
  (let ((printer-form (compile-printer-list source funstate))
	(bindings (make-arg-temp-bindings funstate)))
    `(defun ,function-name (chunk inst stream dstate)
       (declare (type dchunk chunk)
		(type instruction inst)
		(type stream stream)
		(type disassem-state dstate)
ram's avatar
ram committed
990
		#+small (optimize (speed 0) (safety 0) (debug 0)))
ram's avatar
ram committed
991

hallgren's avatar
hallgren committed
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
       (macrolet ((local-format-arg (arg fmt)
		    `(funcall (formatter ,fmt) stream ,arg)))
	 (flet ((local-tab-to-arg-column ()
		  (tab (dstate-argument-column dstate) stream))
		(local-print-name ()
		  (princ (inst-print-name inst) stream))
		(local-write-char (ch)
		  (write-char ch stream))
		(local-princ (thing)
		  (princ thing stream))
		(local-princ16 (thing)
		  (princ16 thing stream))
		(local-call-arg-printer (arg printer)
		  (funcall printer arg stream dstate))
		(local-call-global-printer (fun)
		  (funcall fun chunk inst stream dstate))
		(local-filtered-value (offset)
		  (declare (type filtered-value-index offset))
		  (aref (dstate-filtered-values dstate) offset))
		(local-extract (bytespec)
		  (dchunk-extract chunk bytespec))
		(lookup-label (lab)
		  (or (gethash lab (dstate-label-hash dstate))
		      lab))
		(adjust-label (val adjust-fun)
		  (funcall adjust-fun val dstate)))
	   (declare (ignorable #'local-tab-to-arg-column
			       #'local-print-name
			       #'local-princ #'local-princ16
			       #'local-write-char
			       #'local-call-arg-printer
			       #'local-call-global-printer
			       #'local-extract
			       #'local-filtered-value
			       #'lookup-label #'adjust-label)
		    (inline local-tab-to-arg-column
			    local-princ local-princ16
			    local-call-arg-printer local-call-global-printer
			    local-filtered-value local-extract
			    lookup-label adjust-label))
	   (let* ,bindings
	     ,@printer-form))))))
1034

ram's avatar
ram committed
1035
1036
;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1037
(defun all-arg-refs-relevent-p (printer args)
ram's avatar
ram committed
1038
1039
1040
  (cond ((or (null printer) (keywordp printer) (eq printer t))
	 t)
	((symbolp printer)
hallgren's avatar
hallgren committed
1041
	 (find printer args :key #'arg-name))
ram's avatar
ram committed
1042
	((listp printer)
hallgren's avatar
hallgren committed
1043
	 (every #'(lambda (x) (all-arg-refs-relevent-p x args))
1044
		printer))
hallgren's avatar
hallgren committed
1045
	(t t)))
ram's avatar
ram committed
1046

hallgren's avatar
hallgren committed
1047
(defun pick-printer-choice (choices args)
ram's avatar
ram committed
1048
  (dolist (choice choices
1049
	   (pd-error (intl:gettext "No suitable choice found in ~s") choices))
hallgren's avatar
hallgren committed
1050
    (when (all-arg-refs-relevent-p choice args)
ram's avatar
ram committed
1051
1052
      (return choice))))

hallgren's avatar
hallgren committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
(defun preprocess-chooses (printer args)
  (cond ((atom printer)
	 printer)
	((eq (car printer) :choose)
	 (pick-printer-choice (cdr printer) args))
	(t
	 (sharing-mapcar #'(lambda (sub) (preprocess-chooses sub args))
			 printer))))

;;; ----------------------------------------------------------------

(defun preprocess-test (subj form args)
  (multiple-value-bind (subj test)
      (if (and (consp form) (symbolp (car form)) (not (keywordp (car form))))
	  (values (car form) (cdr form))
	  (values subj form))
    (let ((key (if (consp test) (car test) test))
	  (body (if (consp test) (cdr test) nil)))
      (case key
	(:constant
	 (if (null body)
	     ;; if no supplied constant values, just any constant is ok, just
	     ;; see if there's some constant value in the arg.
	     (not
	      (null
	       (arg-value
		(or (find subj args :key #'arg-name)
1080
		    (pd-error (intl:gettext "Unknown argument ~s") subj)))))
hallgren's avatar
hallgren committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
	     ;; otherwise, defer to run-time
	     form))
	((:or :and :not)
	 (sharing-cons
	  form
	  subj
	  (sharing-cons
	   test
	   key
	   (sharing-mapcar
	    #'(lambda (sub-test)
		(preprocess-test subj sub-test args))
	    body))))
	(t form)))))

(defun preprocess-conditionals (printer args)
  (if (atom printer)
      printer
      (case (car printer)
	(:unless
	 (preprocess-conditionals
	  `(:cond ((:not ,(nth 1 printer)) ,@(nthcdr 2 printer)))
	  args))
	(:when
	 (preprocess-conditionals `(:cond (,(cdr printer))) args))
	(:if
	 (preprocess-conditionals
	  `(:cond (,(nth 1 printer) ,(nth 2 printer))
		  (t ,(nth 3 printer)))
	  args))
	(:cond
	 (sharing-cons
	  printer
	  :cond
	  (sharing-mapcar
	   #'(lambda (clause)
	       (let ((filtered-body
		      (sharing-mapcar
		       #'(lambda (sub-printer)
			   (preprocess-conditionals sub-printer args))
		       (cdr clause))))
		 (sharing-cons
		  clause
		  (preprocess-test (find-first-field-name filtered-body)
				   (car clause)
				   args)
		  filtered-body)))
	   (cdr printer))))
	(quote printer)
	(t
	 (sharing-mapcar
	  #'(lambda (sub-printer)
	      (preprocess-conditionals sub-printer args))
	  printer)))))

hallgren's avatar
hallgren committed
1136
(defun preprocess-printer (printer args)
1137
  "Returns a version of the disassembly-template PRINTER with compile-time
hallgren's avatar
hallgren committed
1138
1139
1140
1141
  tests (e.g. :constant without a value), and any :CHOOSE operators resolved
  properly for the args ARGS.  (:CHOOSE Sub*) simply returns the first Sub in
  which every field reference refers to a valid arg."
  (preprocess-conditionals (preprocess-chooses printer args) args))
ram's avatar
ram committed
1142
1143
1144

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
(defstruct (cached-function (:conc-name cached-fun-))
  (funstate nil :type (or null funstate))
  (constraint nil :type list)
  (name nil :type (or null symbol)))

(defun find-cached-function (cached-funs args constraint)
  (dolist (cached-fun cached-funs nil)
    (let ((funstate (cached-fun-funstate cached-fun)))
      (when (and (equal constraint (cached-fun-constraint cached-fun))
		 (or (null funstate)
		     (funstate-compatible-p funstate args)))
	(return cached-fun)))))

1158
(defmacro with-cached-function ((name-var funstate-var cache cache-slot
hallgren's avatar
hallgren committed
1159
1160
1161
1162
1163
					  args &key constraint prefix)
				&body defun-maker-forms)
  (let ((cache-var (gensym))
	(constraint-var (gensym)))
    `(let* ((,constraint-var ,constraint)
1164
1165
	    (,cache-var (find-cached-function (,cache-slot ,cache)
					      ,args ,constraint-var)))
hallgren's avatar
hallgren committed
1166
1167
       (cond (,cache-var
	      #+nil
1168
	      (Format t (intl:gettext "~&; Using cached function ~s~%")
hallgren's avatar
hallgren committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
		      (cached-fun-name ,cache-var))
	      (values (cached-fun-name ,cache-var) nil))
	     (t
	      (let* ((,name-var (gensym ,prefix))
		     (,funstate-var (make-funstate ,args))
		     (,cache-var
		      (make-cached-function :name ,name-var
					    :funstate ,funstate-var
					    :constraint ,constraint-var)))
		#+nil
1179
		(format t (intl:gettext "~&; Making new function ~s~%")
hallgren's avatar
hallgren committed
1180
			(cached-fun-name ,cache-var))
1181
1182
1183
1184
1185
1186
		(values ,name-var
			`(progn
			   ,(progn ,@defun-maker-forms)
			   (eval-when (compile eval)
			     (push ,,cache-var
				   (,',cache-slot ',,cache)))))))))))
ram's avatar
ram committed
1187
1188
1189

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
(defstruct function-cache
  (printers nil :type list)
  (labellers nil :type list)
  (prefilters nil :type list))

(defun find-printer-fun (printer-source args cache)
  (if (null printer-source)
      (values nil nil)
      (let ((printer-source (preprocess-printer printer-source args)))
	(with-cached-function
1200
1201
1202
	    (name funstate cache function-cache-printers args
		  :constraint printer-source
		  :prefix "PRINTER")
hallgren's avatar
hallgren committed
1203
1204
1205
1206
1207
1208
1209
1210
	  (make-printer-defun printer-source funstate name)))))

(defun find-labeller-fun (args cache)
  (let ((labelled-fields
	 (mapcar #'arg-name (remove-if-not #'arg-use-label args))))
    (if (null labelled-fields)
	(values nil nil)
	(with-cached-function
1211
	    (name funstate cache function-cache-labellers args
hallgren's avatar
hallgren committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
	     :prefix "LABELLER"
	     :constraint labelled-fields)
	  (let ((labels-form 'labels))
	    (dolist (arg args)
	      (when (arg-use-label arg)
		(setf labels-form
		      `(let ((labels ,labels-form)
			     (addr
			      ,(arg-value-form arg funstate :adjusted nil)))
			 (if (assoc addr labels :test #'eq)
			     labels
			     (cons (cons addr nil) labels))))))
	    `(defun ,name (chunk labels dstate)
	       (declare (type list labels)
			(type dchunk chunk)
			(type disassem-state dstate)
ram's avatar
ram committed
1228
			#+small
ram's avatar
ram committed
1229
			(optimize (speed 0) (safety 0) (debug 0)))
hallgren's avatar
hallgren committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
	       (flet ((local-filtered-value (offset)
			(declare (type filtered-value-index offset))
			(aref (dstate-filtered-values dstate) offset))
		      (local-extract (bytespec)
			(dchunk-extract chunk bytespec))
		      (adjust-label (val adjust-fun)
			(funcall adjust-fun val dstate)))
		 (declare (ignorable #'local-filtered-value #'local-extract
				     #'adjust-label)
			  (inline local-filtered-value local-extract
				  adjust-label))
		 (let* ,(make-arg-temp-bindings funstate)
		   ,labels-form))))))))

(defun find-prefilter-fun (args cache)
  (let ((filtered-args
	 (mapcar #'arg-name (remove-if-not #'arg-prefilter args))))
    (if (null filtered-args)
	(values nil nil)
	(with-cached-function
1250
	    (name funstate cache function-cache-prefilters args
hallgren's avatar
hallgren committed
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
	     :prefix "PREFILTER"
	     :constraint filtered-args)
	  (collect ((forms))
	    (dolist (arg args)
	      (let ((pf (arg-prefilter arg)))
		(when pf
		  (forms
		   `(setf (local-filtered-value ,(arg-position arg))
			  ,(maybe-listify
			    (gen-arg-forms arg :filtering funstate)))))
		))
	    `(defun ,name (chunk dstate)
	       (declare (type dchunk chunk)
			(type disassem-state dstate)
ram's avatar
ram committed
1265
1266
			#+small
			(optimize (speed 0) (safety 0) (debug 0)))
hallgren's avatar
hallgren committed
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
		 (flet (((setf local-filtered-value) (value offset)
			  (declare (type filtered-value-index offset))
			  (setf (aref (dstate-filtered-values dstate) offset)
				value))
			(local-filter (value filter)
			  (funcall filter value dstate))
			(local-extract (bytespec)
			  (dchunk-extract chunk bytespec)))
		   (declare (ignorable #'local-filter #'local-extract)
			    (inline (setf local-filtered-value)
				    local-filter local-extract))
		   ;; use them for side-effects only
		   (let* ,(make-arg-temp-bindings funstate)
		     ,@(forms)))))))))
ram's avatar
ram committed
1281
1282
1283

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1284
1285
1286
(defun set-arg-from-type (arg type-name table)
  (let ((type-arg (find type-name table :key #'arg-name)))
    (when (null type-arg)
1287
      (pd-error (intl:gettext "Unknown argument type: ~s") type-name))
hallgren's avatar
hallgren committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
    (setf (arg-printer arg) (arg-printer type-arg))
    (setf (arg-prefilter arg) (arg-prefilter type-arg))
    (setf (arg-sign-extend-p arg) (arg-sign-extend-p type-arg))
    (setf (arg-use-label arg) (arg-use-label type-arg))))

(defun modify-or-add-arg (arg-name
			  args
			  type-table
			  &key
			  (value nil value-p)
			  (type nil type-p)
			  (prefilter nil prefilter-p)
			  (printer nil printer-p)
			  (sign-extend nil sign-extend-p)
			  (use-label nil use-label-p)
			  (field nil field-p)
			  (fields nil fields-p)
			  format-length)
  (let* ((arg-pos (position arg-name args :key #'arg-name))
	 (arg
	  (if (null arg-pos)
	      (let ((arg (make-argument :name arg-name)))
		(if (null args)
		    (setf args (list arg))
		    (push arg (cdr (last args))))
		arg)
	      (setf (nth arg-pos args) (copy-argument (nth arg-pos args))))))
    (when (and field-p (not fields-p))
      (setf fields (list field))
      (setf fields-p t))
    (when type-p
      (set-arg-from-type arg type type-table))
    (when value-p
      (setf (arg-value arg) value))
    (when prefilter-p
      (setf (arg-prefilter arg) prefilter))
    (when sign-extend-p
      (setf (arg-sign-extend-p arg) sign-extend))
    (when printer-p
      (setf (arg-printer arg) printer))
    (when use-label-p
      (setf (arg-use-label arg) use-label))
    (when fields-p
      (when (null format-length)
	(error
1333
1334
	 (intl:gettext "~@<In arg ~s:  ~3i~:_~
          Can't specify fields except using DEFINE-INSTRUCTION-FORMAT.~:>")
hallgren's avatar
hallgren committed
1335
1336
1337
1338
1339
1340
	 arg-name))
      (setf (arg-fields arg)
	    (mapcar #'(lambda (bytespec)
			(when (> (+ (byte-position bytespec)
				    (byte-size bytespec))
				 format-length)
1341
			  (error (intl:gettext "~@<In arg ~s:  ~3i~:_~
hallgren's avatar
hallgren committed
1342
				     Field ~s doesn't fit in an ~
1343
				     instruction-format ~d bits wide.~:>")
hallgren's avatar
hallgren committed
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
				 arg-name
				 bytespec
				 format-length))
			(correct-dchunk-bytespec-for-endianness
			 bytespec
			 format-length
			 (c:backend-byte-order c:*target-backend*)))
		    fields))
      )
    args))
ram's avatar
ram committed
1354

hallgren's avatar
hallgren committed
1355
1356
;;; ----------------------------------------------------------------
;;; Compile time info that we stash in the package of the machine backend
1357

hallgren's avatar
hallgren committed
1358
1359
1360
1361
1362
1363
(defun format-table-name ()
  (intern "*DISASSEMBLER-INSTRUCTION-FORMATS*"))
(defun arg-type-table-name ()
  (intern "*DISASSEMBLER-ARG-TYPES*"))
(defun function-cache-name ()
  (intern "*DISASSEMBLER-CACHED-FUNCTIONS*"))
ram's avatar
ram committed
1364
1365
1366

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
(defparameter *arg-function-params*
  '((:printer . (value stream dstate))
    (:use-label . (value dstate))
    (:prefilter . (value dstate))))

;;; detect things that obviously don't need wrapping, like variable-refs &
;;; #'function
(defun doesnt-need-wrapping-p (form)
  (or (symbolp form)
      (and (listp form)
	   (eq (car form) 'function)
	   (symbolp (cadr form)))))

(defun make-wrapper (form arg-name funargs prefix)
  (if (and (listp form)
	   (eq (car form) 'function))
      ;; a function def
      (let ((wrapper-name (symbolicate prefix "-" arg-name "-WRAPPER"))
	    (wrapper-args nil))
	(dotimes (i (length funargs))
	  (push (gensym) wrapper-args))
	(values `#',wrapper-name
		`(defun ,wrapper-name ,wrapper-args
		   (funcall ,form ,@wrapper-args))))
      ;; something else
      (let ((wrapper-name (symbolicate "*" prefix "-" arg-name "-WRAPPER*")))
	(values wrapper-name `(defparameter ,wrapper-name ,form)))))

(defun munge-fun-refs (params evalp &optional wrap-defs-p (prefix ""))
  (let ((params (copy-list params)))
    (do ((tail params (cdr tail))
	 (wrapper-defs nil))
	((null tail)
	 (values params (nreverse wrapper-defs)))
      (let ((fun-arg (assoc (car tail) *arg-function-params*)))
	(when fun-arg
	  (let* ((fun-form (cadr tail))
		 (quoted-fun-form `',fun-form))
	    (when (and wrap-defs-p (not (doesnt-need-wrapping-p fun-form)))
	      (multiple-value-bind (access-form wrapper-def-form)
		  (make-wrapper fun-form (car fun-arg) (cdr fun-arg) prefix)
		(setf quoted-fun-form `',access-form)
		(push wrapper-def-form wrapper-defs)))
	    (if evalp
		(setf (cadr tail)
		      `(make-valsrc ,fun-form ,quoted-fun-form))
		(setf (cadr tail)
		      fun-form))))))))
ram's avatar
ram committed
1415

hallgren's avatar
hallgren committed
1416
;;; ----------------------------------------------------------------
ram's avatar
ram committed
1417

hallgren's avatar
hallgren committed
1418
(defun gen-preamble-form (args)
1419
  "Generate a form to specify global disassembler params.  See the
hallgren's avatar
hallgren committed
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
  documentation for SET-DISASSEM-PARAMS for more info."
  (destructuring-bind
	(&key instruction-alignment
	      address-size
	      (opcode-column-width nil opcode-column-width-p))
      args
    `(progn
       (eval-when (compile eval)
	 ;; these are not in the params because they only exist at compile time
	 (defparameter ,(format-table-name) (make-hash-table))
	 (defparameter ,(arg-type-table-name) nil)
	 (defparameter ,(function-cache-name) (make-function-cache)))
       (let ((params
	      (or (c:backend-disassem-params c:*target-backend*)
		  (setf (c:backend-disassem-params c:*target-backend*)
			(make-params :backend c::*target-backend*)))))
	 (declare (ignorable params))
	 ,(when instruction-alignment
	    `(setf (params-instruction-alignment params)
		   (bits-to-bytes ,instruction-alignment)))
	 ,(when address-size
	    `(setf (params-location-column-width params)
		   (* 2 ,address-size)))
	 ,(when opcode-column-width-p
	    `(setf (params-opcode-column-width params) ,opcode-column-width))
	 'disassem-params))))

(defun gen-clear-info-form ()
  `(eval-when (compile eval)
     (setf ,(format-table-name) nil)
     (setf ,(arg-type-table-name) nil)))

(defun update-args-form (var name-form descrip-forms evalp
			     &optional format-length-form)
  `(setf ,var
	 ,(if evalp
	      `(modify-or-add-arg ,name-form
				  ,var
				  ,(arg-type-table-name)
				  ,@(and format-length-form
					 `(:format-length
					    ,format-length-form))
				  ,@descrip-forms)
	      `(apply #'modify-or-add-arg
		      ,name-form
		      ,var
		      ,(arg-type-table-name);
		      ,@(and format-length-form
			     `(:format-length ,format-length-form))
		      ',descrip-forms))))

(defun gen-arg-type-def-form (name args &optional (evalp t))
1472
  "Generate a form to define a disassembler argument type.  See
hallgren's avatar
hallgren committed
1473
1474
1475
1476
1477
1478
  DEFINE-ARGUMENT-TYPE for more info."
  (multiple-value-bind (args wrapper-defs)
      (munge-fun-refs args evalp t name)
    `(progn
       ,@wrapper-defs
       (eval-when (compile eval)
hallgren's avatar
hallgren committed
1479
1480
	 ,(update-args-form (arg-type-table-name) `',name args evalp))
       ',name)))
hallgren's avatar
hallgren committed
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507

(defun maybe-quote (evalp form)
  (if (or evalp (self-evaluating-p form)) form `',form))

(defun maybe-quote-list (evalp list)
  (if evalp
      list
      (mapcar #'(lambda (el) (maybe-quote nil el)) list)))

(defun gen-arg-access-macro-def-form (arg args format-name)
  (let* ((funstate (make-funstate args))
	 (arg-val-form (arg-value-form arg funstate :adjusted))
	 (bindings (make-arg-temp-bindings funstate)))
    `(defmacro ,(symbolicate format-name "-" (arg-name arg)) (chunk dstate)
       `(let ((chunk ,chunk) (dstate ,dstate))
	  (declare (ignorable chunk dstate))
	  (flet ((local-filtered-value (offset)
		   (declare (type filtered-value-index offset))
		   (aref (dstate-filtered-values dstate) offset))
		 (local-extract (bytespec)
		   (dchunk-extract chunk bytespec)))
	    (declare (ignorable #'local-filtered-value #'local-extract)
		     (inline local-filtered-value local-extract))
	    (let* ,',bindings
	      ,',arg-val-form))))))

(defun gen-format-def-form (header descrips &optional (evalp t))
1508
  "Generate a form to define an instruction format.  See
hallgren's avatar
hallgren committed
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
  DEFINE-INSTRUCTION-FORMAT for more info."
  (when (atom header)
    (setf header (list header)))
  (destructuring-bind (name length &key default-printer include)
      header
    (let ((args-var (gensym))
	  (length-var (gensym))
	  (all-wrapper-defs nil)
	  (arg-count 0))
      (collect ((arg-def-forms))
	(dolist (descrip descrips)
	  (let ((name (pop descrip))) 
	    (multiple-value-bind (descrip wrapper-defs)
		(munge-fun-refs
		 descrip evalp t (format nil "~:@(~a~)-~d" name arg-count))
	      (arg-def-forms
	       (update-args-form args-var `',name descrip evalp length-var))
	      (setf all-wrapper-defs
		    (nconc wrapper-defs all-wrapper-defs)))
	    (incf arg-count)))
	`(progn
	   ,@all-wrapper-defs
	   (eval-when (compile eval)
	     (let ((,length-var ,length)
		   (,args-var
		    ,(and include
			  `(copy-list
			    (format-args
			     (format-or-lose ,include
					     ,(format-table-name)))))))
	       ,@(arg-def-forms)
	       (setf (gethash ',name ,(format-table-name))
		     (make-instruction-format
		      :name ',name
		      :length (bits-to-bytes ,length-var)
		      :default-printer ,(maybe-quote evalp default-printer)
		      :args ,args-var))
	       (eval
		`(progn
		   ,@(mapcar #'(lambda (arg)
				 (when (arg-fields arg)
				   (gen-arg-access-macro-def-form
				    arg ,args-var ',name)))
			     ,args-var)))
	       )))))))
ram's avatar
ram committed
1554
1555
1556

;;; ----------------------------------------------------------------

hallgren's avatar
hallgren committed
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
(defun compute-mask-id (args)
  (let ((mask dchunk-zero)
	(id dchunk-zero))
    (dolist (arg args (values mask id))
      (let ((av (arg-value arg)))
	(when av
	  (do ((fields (arg-fields arg) (cdr fields))
	       (values (if (atom av) (list av) av) (cdr values)))
	      ((null fields))
	    (let ((field-mask (dchunk-make-mask (car fields))))
	      (when (/= (dchunk-and mask field-mask) dchunk-zero)
1568
		(pd-error (intl:gettext "Field ~s in arg ~s overlaps some other field")
hallgren's avatar
hallgren committed
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
			  (car fields)
			  (arg-name arg)))
	      (dchunk-insertf id (car fields) (car values))
	      (dchunk-orf mask field-mask))))))))

(defun install-inst-flavors (name flavors)
  (setf (gethash name
		 (params-instructions
		  (c:backend-disassem-params c:*target-backend*)))
	flavors))
			  
(defun format-or-lose (name table)
  (or (gethash name table)
1582
      (pd-error (intl:gettext "Unknown instruction format ~s") name)))
hallgren's avatar
hallgren committed
1583
1584
1585