/[cmucl]/src/code/pmax-disassem.lisp
ViewVC logotype

Contents of /src/code/pmax-disassem.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Sun Nov 18 09:00:24 1990 UTC (23 years, 5 months ago) by wlott
Branch: MAIN
Changes since 1.15: +2 -3 lines
Took out a reference to register-name-style, 'cause it doesn't exist
any more.
1 ;;; -*- Mode: Lisp; Package: MIPS -*-
2 ;;;
3 ;;; $Header: /tiger/var/lib/cvsroots/cmucl/src/code/pmax-disassem.lisp,v 1.16 1990/11/18 09:00:24 wlott Exp $
4 ;;;
5 ;;; A simple dissambler for the MIPS R2000.
6 ;;;
7 ;;; Written by Christopher Hoover.
8 ;;;
9
10 (in-package "MIPS" :use '("LISP"))
11
12 (export '(register-name disassemble-code-vector))
13
14
15 ;;;; Instruction Layout
16
17 ;;;
18 ;;; Each instrunction on the MIPS R2000 consists of a single word (32
19 ;;; bits) aligned on a single word boundaray. There are three
20 ;;; instrunction formats:
21 ;;;
22 ;;; I-Type (Immediate)
23 ;;;
24 ;;; 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
25 ;;; 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
26 ;;; ---------------------------------------------------------------
27 ;;; [ op ] [ rs ] [ rt ] [ immediate ]
28 ;;; ---------------------------------------------------------------
29 ;;;
30 ;;;
31 ;;; J-Type (Jump)
32 ;;;
33 ;;; 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
34 ;;; 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
35 ;;; ---------------------------------------------------------------
36 ;;; [ op ] [ target ]
37 ;;; ---------------------------------------------------------------
38 ;;;
39 ;;;
40 ;;; R-Type (Register)
41 ;;;
42 ;;; 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
43 ;;; 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
44 ;;; ---------------------------------------------------------------
45 ;;; [ op ] [ rs ] [ rt ] [ rd ] [ shmat ] [ funct ]
46 ;;; ---------------------------------------------------------------
47 ;;;
48 ;;; These instructions fall into 5 categories: Load/Store,
49 ;;; Computational, Jump/Branch, Coprocessor, and Special.
50 ;;;
51
52
53 ;;;; Register Names
54
55 (defun register-name (register-number)
56 (unless (<= 0 register-number 31)
57 (error "Illegal register number!"))
58 (svref *register-names* register-number))
59
60
61
62 ;;;; Instruction Type Definition
63
64 ;;;
65 ;;; These instruction types correspond to the various ways the decoded
66 ;;; instructions are printed which in turn corresponds somewhat to the
67 ;;; way the instructions are decoded.
68 ;;;
69
70 (defvar *mips-instruction-types* (make-hash-table :test #'eq))
71
72 (defmacro def-mips-instruction-type (types &body body)
73 `(let ((closure #'(lambda (name word stream) ,@body)))
74 (dolist (type ',types)
75 (when (gethash type *mips-instruction-types*)
76 (warn "Instruction type ~S being redefined" type))
77 (setf (gethash type *mips-instruction-types*) closure))))
78
79 (defun mips-instruction-type-p (type)
80 (not (not (gethash type *mips-instruction-types*))))
81
82
83 ;;;; Instruction Types
84
85 ;;;
86 ;;; Used later for relative branches.
87 (defvar *current-instruction-number* 0)
88
89 (def-mips-instruction-type (:ls-type)
90 (let ((rs (ldb (byte 5 21) word))
91 (rt (ldb (byte 5 16) word))
92 (immed (signed-ldb (byte 16 0) word)))
93 (format stream "~16,8T~A~8,8T~A, #x~X(~A)~%"
94 name (register-name rt) immed (register-name rs))))
95
96 (def-mips-instruction-type (:si-type)
97 (let ((rs (ldb (byte 5 21) word))
98 (rt (ldb (byte 5 16) word))
99 (immed (signed-ldb (byte 16 0) word)))
100 (cond ((and (zerop rs) (or (string= name "ADDI") (string= name "ADDIU")))
101 (format stream "~16,8TLOADI~8,8T~A, #x~X~%"
102 (register-name rt) immed))
103 ((and (= rs null-offset) (string= name "ADDI"))
104 ;; Major hack ...
105 (format stream "~16,8T~A~8,8T~A, ~A, #x~X~48,8T; ~S~%"
106 name (register-name rt) (register-name rs) immed
107 (vm:offset-static-symbol immed)))
108 (t
109 (format stream "~16,8T~A~8,8T~A, ~A, #x~X~%"
110 name (register-name rt) (register-name rs) immed)))))
111
112 (def-mips-instruction-type (:ui-type)
113 (let ((rs (ldb (byte 5 21) word))
114 (rt (ldb (byte 5 16) word))
115 (immed (ldb (byte 16 0) word)))
116 (cond ((and (zerop rs) (or (string= name "ORI") (string= name "XORI")))
117 (format stream "~16,8TLOADI~8,8T~A, #x~X~%"
118 (register-name rt) immed))
119 (t
120 (format stream "~16,8T~A~8,8T~A, ~A, #x~X~%"
121 name (register-name rt) (register-name rs) immed)))))
122
123 (def-mips-instruction-type (:lui-type)
124 (let ((rt (ldb (byte 5 16) word))
125 (immed (ldb (byte 16 0) word)))
126 (format stream "~16,8T~A~8,8T~A, #x~X~%" name (register-name rt) immed)))
127
128 (def-mips-instruction-type (:j-type)
129 (let ((target (ldb (byte 26 0) word)))
130 (format stream "~16,8T~A~8,8Ttarget = ~D~%" name target)))
131
132 (def-mips-instruction-type (:jr-type)
133 (let ((rs (ldb (byte 5 21) word)))
134 (format stream "~16,8T~A~8,8T~A~%" name (register-name rs))))
135
136 (def-mips-instruction-type (:jalr-type)
137 (let ((rs (ldb (byte 5 21) word))
138 (rd (ldb (byte 5 11) word)))
139 (format stream "~16,8T~A~8,8T~A, ~A~%" name
140 (register-name rd) (register-name rs))))
141
142 (defun branch-target (offset)
143 (+ *current-instruction-number* offset 1))
144
145 (def-mips-instruction-type (:branch-type)
146 (let ((rs (ldb (byte 5 21) word))
147 (offset (signed-ldb (byte 16 0) word)))
148 (format stream "~16,8T~A~8,8T~A, ~D~%" name
149 (register-name rs) (branch-target offset))))
150
151 (def-mips-instruction-type (:branch2-type)
152 (let* ((rs (ldb (byte 5 21) word))
153 (rt (ldb (byte 5 16) word))
154 (offset (signed-ldb (byte 16 0) word))
155 (target (branch-target offset)))
156 (cond ((and (zerop rs) (zerop rt) (string= name "BEQ"))
157 (format stream "~16,8TB~8,8T~D~%" target))
158 (t
159 (format stream "~16,8T~A~8,8T~A, ~A, ~D~%" name
160 (register-name rs) (register-name rt) target)))))
161
162 (def-mips-instruction-type (:r3-type)
163 (let ((rs (ldb (byte 5 21) word))
164 (rt (ldb (byte 5 16) word))
165 (rd (ldb (byte 5 11) word)))
166 (cond ((zerop rd)
167 ;; Hack for NOP
168 (format stream "~16,8TNOP~%"))
169 ((and (zerop rt) (or (string= name "OR") (string= name "ADDU")))
170 ;; Hack for MOVE
171 (format stream "~16,8TMOVE~8,8T~A, ~A~%"
172 (register-name rd) (register-name rs)))
173 (t
174 (format stream "~16,8T~A~8,8T~A, ~A, ~A~%"
175 name (register-name rd) (register-name rs)
176 (register-name rt))))))
177
178 (def-mips-instruction-type (:mf-type)
179 (let ((rd (ldb (byte 5 11) word)))
180 (format stream "~16,8T~A~8,8T~A~%" name (register-name rd))))
181
182 (def-mips-instruction-type (:mt-type)
183 (let ((rs (ldb (byte 5 21) word)))
184 (format stream "~16,8T~A~8,8T~A~%" name (register-name rs))))
185
186 (def-mips-instruction-type (:mult-type)
187 (let ((rs (ldb (byte 5 21) word))
188 (rt (ldb (byte 5 16) word)))
189 (format stream "~16,8T~A~8,8T~A, ~A~%" name
190 (register-name rs) (register-name rt))))
191
192 (def-mips-instruction-type (:shift-type)
193 (let ((rt (ldb (byte 5 16) word))
194 (rd (ldb (byte 5 11) word))
195 (shamt (ldb (byte 5 6) word)))
196 ;; Hack for NOP
197 (cond ((= word 0)
198 (format stream "~16,8TNOP~%"))
199 (t
200 (format stream "~16,8T~A~8,8T~A, ~A, #x~X~%"
201 name (register-name rd) (register-name rt) shamt)))))
202
203 (def-mips-instruction-type (:shiftv-type)
204 (let ((rs (ldb (byte 5 21) word))
205 (rt (ldb (byte 5 16) word))
206 (rd (ldb (byte 5 11) word)))
207 (format stream "~16,8T~A~8,8T~A, ~A, ~A~%"
208 name (register-name rd) (register-name rt) (register-name rs))))
209
210 (def-mips-instruction-type (:break-type)
211 (let ((code (ldb (byte 10 16) word))) ; The entire field is (byte 20 6)
212 (format stream "~16,8T~A~8,8T#x~X~%" name code)))
213
214 (def-mips-instruction-type (:syscall-type)
215 (declare (ignore word))
216 (format stream "~16,8T~A~%" name))
217
218 (def-mips-instruction-type (:cop0-type :cop1-type :cop2-type :cop3-type)
219 (format stream "~16,8T~A~8,8T(#x~X)~%" name word))
220
221
222 ;;;; Instruction Definition
223
224 (defstruct (mips-instruction
225 (:constructor make-mips-instruction (name type))
226 (:print-function %print-mips-instruction))
227 (name "" :type simple-string)
228 type)
229
230 (defun %print-mips-instruction (instr stream depth)
231 (declare (ignore depth))
232 (format stream "#<MIPS instruction ~A>" (mips-instruction-name instr)))
233
234
235 (defconstant mips-instruction-bits 6)
236
237 (defvar *mips-instructions*
238 (make-array (ash 1 mips-instruction-bits) :initial-element nil))
239 (proclaim '(type simple-vector *mips-instructions*))
240
241 (defmacro def-mips-instr (name op-code type)
242 `(let ((name ,name)
243 (type ,type))
244 (unless (mips-instruction-type-p type)
245 (warn "~S is an unknown instruction type" type))
246 (setf (svref *mips-instructions* ,op-code)
247 (make-mips-instruction name ,type))
248 name))
249
250
251 (defconstant mips-special-instruction-bits 6)
252
253 (defvar *mips-special-instructions*
254 (make-array (ash 1 mips-special-instruction-bits) :initial-element nil))
255 (proclaim '(type simple-vector *mips-special-instructions*))
256
257 (defmacro def-mips-special-instr (name op-code type)
258 `(let ((name ,name)
259 (type ,type))
260 (unless (mips-instruction-type-p type)
261 (warn "~S is an unknown instruction type" type))
262 (setf (svref *mips-special-instructions* ,op-code)
263 (make-mips-instruction name ,type))
264 name))
265
266
267 (defconstant mips-bcond-instruction-bits 6)
268
269 (defvar *mips-bcond-instructions*
270 (make-array (ash 1 mips-bcond-instruction-bits) :initial-element nil))
271 (proclaim '(type simple-vector *mips-bcond-instructions*))
272
273 (defmacro def-mips-bcond-instr (name op-code type)
274 `(let ((name ,name)
275 (type ,type))
276 (unless (mips-instruction-type-p type)
277 (warn "~S is an unknown instruction type" type))
278 (setf (svref *mips-bcond-instructions* ,op-code)
279 (make-mips-instruction name ,type))
280 name))
281
282
283 ;;;; Normal Opcodes
284
285 (def-mips-instr "J" #b000010 :j-type)
286 (def-mips-instr "JAL" #b000011 :j-type)
287 (def-mips-instr "BEQ" #b000100 :branch2-type)
288 (def-mips-instr "BNE" #b000101 :branch2-type)
289 (def-mips-instr "BLEZ" #b000110 :branch-type)
290 (def-mips-instr "BGTZ" #b000111 :branch-type)
291
292 (def-mips-instr "ADDI" #b001000 :si-type)
293 (def-mips-instr "ADDIU" #b001001 :si-type)
294 (def-mips-instr "SLTI" #b001010 :si-type)
295 (def-mips-instr "SLTIU" #b001011 :si-type)
296 (def-mips-instr "ANDI" #b001100 :ui-type)
297 (def-mips-instr "ORI" #b001101 :ui-type)
298 (def-mips-instr "XORI" #b001110 :ui-type)
299 (def-mips-instr "LUI" #b001111 :lui-type)
300
301 (def-mips-instr "COP0" #b010000 :cop0-type)
302 (def-mips-instr "COP1" #b010001 :cop1-type)
303 (def-mips-instr "COP2" #b010010 :cop2-type)
304 (def-mips-instr "COP3" #b010011 :cop3-type)
305
306 (def-mips-instr "LB" #b100000 :ls-type)
307 (def-mips-instr "LH" #b100001 :ls-type)
308 (def-mips-instr "LWL" #b100010 :ls-type)
309 (def-mips-instr "LW" #b100011 :ls-type)
310 (def-mips-instr "LBU" #b100100 :ls-type)
311 (def-mips-instr "LHU" #b100101 :ls-type)
312 (def-mips-instr "LWR" #b100110 :ls-type)
313
314 (def-mips-instr "SB" #b101000 :ls-type)
315 (def-mips-instr "SH" #b101001 :ls-type)
316 (def-mips-instr "SWL" #b101010 :ls-type)
317 (def-mips-instr "SW" #b101011 :ls-type)
318 (def-mips-instr "SWR" #b101110 :ls-type)
319
320 (def-mips-instr "LWC0" #b110000 :cop0-type)
321 (def-mips-instr "LWC1" #b110001 :cop1-type)
322 (def-mips-instr "LWC2" #b110010 :cop2-type)
323 (def-mips-instr "LWC3" #b110011 :cop3-type)
324
325 (def-mips-instr "SWC0" #b111000 :cop0-type)
326 (def-mips-instr "SWC1" #b111001 :cop1-type)
327 (def-mips-instr "SWC2" #b111010 :cop2-type)
328 (def-mips-instr "SWC3" #b111011 :cop3-type)
329
330
331 ;;;; SPECIAL Opcodes
332
333 (defconstant special-op #b000000)
334
335 (def-mips-special-instr "SLL" #b000000 :shift-type)
336 (def-mips-special-instr "SRL" #b000010 :shift-type)
337 (def-mips-special-instr "SRA" #b000011 :shift-type)
338 (def-mips-special-instr "SLLV" #b000100 :shiftv-type)
339 (def-mips-special-instr "SRLV" #b000110 :shiftv-type)
340 (def-mips-special-instr "SRAV" #b000111 :shiftv-type)
341
342 (def-mips-special-instr "JR" #b001000 :jr-type)
343 (def-mips-special-instr "JALR" #b001001 :jalr-type)
344 (def-mips-special-instr "SYSCALL" #b001100 :syscall-type)
345 (def-mips-special-instr "BREAK" #b001101 :break-type)
346
347 (def-mips-special-instr "MFHI" #b010000 :mf-type)
348 (def-mips-special-instr "MTHI" #b010001 :mt-type)
349 (def-mips-special-instr "MFLO" #b010010 :mf-type)
350 (def-mips-special-instr "MTLO" #b010011 :mt-type)
351
352 (def-mips-special-instr "MULT" #b011000 :mult-type)
353 (def-mips-special-instr "MULTU" #b011001 :mult-type)
354 (def-mips-special-instr "DIV" #b011010 :mult-type)
355 (def-mips-special-instr "DIVU" #b011011 :mult-type)
356
357 (def-mips-special-instr "ADD" #b100000 :r3-type)
358 (def-mips-special-instr "ADDU" #b100001 :r3-type)
359 (def-mips-special-instr "SUB" #b100010 :r3-type)
360 (def-mips-special-instr "SUBU" #b100011 :r3-type)
361 (def-mips-special-instr "AND" #b100100 :r3-type)
362 (def-mips-special-instr "OR" #b100101 :r3-type)
363 (def-mips-special-instr "XOR" #b100110 :r3-type)
364 (def-mips-special-instr "NOR" #b100111 :r3-type)
365
366 (def-mips-special-instr "SLT" #b101010 :r3-type)
367 (def-mips-special-instr "SLTU" #b101011 :r3-type)
368
369
370 ;;;; BCOND Opcodes
371
372 (defconstant bcond-op #b000001)
373
374 (def-mips-bcond-instr "BLTZ" #b00000 :branch-type)
375 (def-mips-bcond-instr "BLTZAL" #b00001 :branch-type)
376
377 (def-mips-bcond-instr "BLTZAL" #b10000 :branch-type)
378 (def-mips-bcond-instr "BGEZAL" #b10001 :branch-type)
379
380
381 ;;;; Signed-Ldb
382
383 (defun signed-ldb (byte-spec integer)
384 (let ((unsigned (ldb byte-spec integer))
385 (length (byte-size byte-spec)))
386 (if (logbitp (1- length) unsigned)
387 (- unsigned (ash 1 length))
388 unsigned)))
389
390
391 ;;;; Instruction Decoding
392
393 (defun mips-instruction (word)
394 (let* ((opcode (ldb (byte 6 26) word)))
395 (cond ((= opcode special-op)
396 (let ((function (ldb (byte 6 0) word)))
397 (svref *mips-special-instructions* function)))
398 ((= opcode bcond-op)
399 (let ((cond (ldb (byte 5 16) word)))
400 (svref *mips-bcond-instructions* cond)))
401 (t
402 (svref *mips-instructions* opcode)))))
403
404
405 ;;;; Disassemble-Instruction
406
407 (defun disassemble-instruction (word &optional (stream t))
408 (let* ((instr (mips-instruction word)))
409 (cond (instr
410 (let* ((instr-name (mips-instruction-name instr))
411 (instr-type (mips-instruction-type instr))
412 (closure (gethash instr-type *mips-instruction-types*)))
413 (cond (closure
414 (funcall closure instr-name word stream))
415 (t
416 (format stream "UNKNOWN TYPE (~A/~S/#x~X)~%"
417 instr-name instr-type word)))
418 (values instr-name instr-type)))
419 (t
420 (format stream "~16,8TDATA~8,8T#x~X~%" word)
421 (return-from disassemble-instruction (values nil nil))))))
422
423
424
425 ;;; Dissassemble-Code-Vector
426
427 (defconstant delay-slot-instruction-types
428 '(:j-type :jr-type :jalr-type :branch-type :branch2-type))
429
430 (defun disassemble-code-vector (code-vector length &optional (stream t))
431 (do ((i 0 (+ i 4))
432 (*current-instruction-number* 0 (1+ *current-instruction-number*))
433 (instruction-in-delay-slot-p nil))
434 ((>= i length))
435 (unless instruction-in-delay-slot-p
436 (format stream "~6D:" *current-instruction-number*))
437 (multiple-value-bind
438 (name type)
439 (disassemble-instruction (logior (aref code-vector i)
440 (ash (aref code-vector (+ i 1)) 8)
441 (ash (aref code-vector (+ i 2)) 16)
442 (ash (aref code-vector (+ i 3)) 24))
443 stream)
444 (declare (ignore name))
445 (cond ((member type delay-slot-instruction-types :test #'eq)
446 (setf instruction-in-delay-slot-p t))
447 (t
448 (setf instruction-in-delay-slot-p nil))))))
449
450
451
452 ;;;; Disassemble-code-sap
453
454 (defun disassemble-code-sap (sap length &optional (stream t))
455 (do ((*current-instruction-number* 0 (1+ *current-instruction-number*))
456 (instruction-in-delay-slot-p nil))
457 ((>= *current-instruction-number* length))
458 (unless instruction-in-delay-slot-p
459 (format stream "~6D:" *current-instruction-number*))
460 (multiple-value-bind
461 (name type)
462 (disassemble-instruction (system:sap-ref-32
463 sap
464 *current-instruction-number*)
465 stream)
466 (declare (ignore name))
467 (cond ((member type delay-slot-instruction-types :test #'eq)
468 (setf instruction-in-delay-slot-p t))
469 (t
470 (setf instruction-in-delay-slot-p nil))))))
471
472
473 ;;;; Disassemble
474
475 (defun compile-function-lambda-expr (function)
476 (multiple-value-bind
477 (lambda closurep name)
478 (function-lambda-expression function)
479 (declare (ignore name))
480 (when closurep
481 (error "Cannot compile lexical closure."))
482 (compile nil lambda)))
483
484 (defun disassemble (object &optional (stream *standard-output*))
485 (let* ((function (cond ((or (symbolp object)
486 (and (listp object)
487 (eq (car object) 'lisp:setf)))
488 (let ((temp (fdefinition object)))
489 (if (eval:interpreted-function-p temp)
490 (compile-function-lambda-expr temp)
491 temp)))
492 ((eval:interpreted-function-p object)
493 (compile-function-lambda-expr object))
494 ((functionp object)
495 object)
496 ((and (listp object)
497 (eq (car object) 'lisp::lambda))
498 (compile nil object))
499 (t
500 (error "Invalid argument to disassemble - ~S"
501 object))))
502 (self (system:%primitive function-self function))
503 (code (di::function-code-header self)))
504 (disassemble-code-sap (truly-the system:system-area-pointer
505 (system:%primitive code-instructions
506 code))
507 (system:%primitive code-code-size code)
508 stream)))

  ViewVC Help
Powered by ViewVC 1.1.5