-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgmr-tools.scm
183 lines (166 loc) · 5.2 KB
/
pgmr-tools.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
176
177
178
179
180
181
182
183
; Programmer development tools.
; Copyright (C) 2000, 2009 Red Hat, Inc.
; This file is part of CGEN.
; See file COPYING.CGEN for details.
;
; This file contains a collection of programmer debugging tools.
; They're mainly intended for using cgen to debug other things,
; but any kind of debugging tool can go here.
; All routines require the application independent part of cgen to be loaded
; and the .cpu file to be loaded. They do not require any particular
; application though (opcodes, simulator, etc.). If they do, that's a bug.
; It may be that the appication has a generally useful routine that should
; live elsewhere, but that's it.
;
; These tools don't have to be particularily efficient (within reason).
; It's more important that they be simple and clear.
;
; Some tools require ifmt-compute! to be run.
; They will run it if necessary.
;
; Table of contents:
;
; pgmr-pretty-print-insn-format
; cgen debugging tool, pretty prints the iformat of an <insn> object
;
; pgmr-pretty-print-insn-value
; break out an instruction's value into its component fields
;
; pgmr-lookup-insn
; given a random bit pattern for an instruction, lookup the insn and return
; its <insn> object
; Pretty print the instruction's opcode value, for debugging.
; INSN is an <insn> object.
(define (pgmr-pretty-print-insn-format insn)
(define (to-width width n-str)
(string-take-with-filler (- width)
n-str
#\0))
(define (dump-insn-mask mask insn-length)
(string-append "0x" (to-width (quotient insn-length 4)
(number->string mask 16))
", "
(string-map
(lambda (n)
(string-append " " (to-width 4 (number->string n 2))))
(reverse
(split-bits (make-list (quotient insn-length 4) 4)
mask)))))
; Print VALUE with digits not in MASK printed as "X".
(define (dump-insn-value value mask insn-length)
(string-append "0x" (to-width (quotient insn-length 4)
(number->string value 16))
", "
(string-map
(lambda (n mask)
(string-append
" "
(list->string
(map (lambda (char in-mask?)
(if in-mask? char #\X))
(string->list (to-width 4 (number->string n 2)))
(bits->bools mask 4)))))
(reverse
(split-bits (make-list (quotient insn-length 4) 4)
value))
(reverse
(split-bits (make-list (quotient insn-length 4) 4)
mask)))))
(define (dump-ifield f)
(string-append " Name: "
(obj:name f)
", "
"Start: "
(number->string
(+ (bitrange-word-offset (-ifld-bitrange f))
(bitrange-start (-ifld-bitrange f))))
", "
"Length: "
(number->string (ifld-length f))
"\n"))
(let* ((iflds (sort-ifield-list (insn-iflds insn)
(not (current-arch-insn-lsb0?))))
(mask (compute-insn-base-mask iflds))
(mask-length (compute-insn-base-mask-length iflds)))
(display
(string-append
"Instruction: " (obj:name insn)
"\n"
"Syntax: "
(insn-syntax insn)
"\n"
"Fields:\n"
(string-map dump-ifield iflds)
"Instruction length (computed from ifield list): "
(number->string (apply + (map ifld-length iflds)))
"\n"
"Mask: "
(dump-insn-mask mask mask-length)
"\n"
"Value: "
(let ((value (apply +
(map (lambda (fld)
(ifld-value fld mask-length
(ifld-get-value fld)))
(find ifld-constant? (ifields-base-ifields (insn-iflds insn)))))))
(dump-insn-value value mask mask-length))
; TODO: Print value spaced according to fields.
"\n"
)))
)
; Pretty print an instruction's value.
(define (pgmr-pretty-print-insn-value insn value)
(define (dump-ifield ifld value name-width)
(string-append
(string-take name-width (obj:str-name ifld))
": "
(number->string value)
", 0x"
(number->hex value)
"\n"))
(let ((ifld-values (map (lambda (ifld)
(ifld-extract ifld insn value))
(insn-iflds insn)))
(max-name-length (apply max
(map string-length
(map obj:name
(insn-iflds insn)))))
)
(display
(string-append
"Instruction: " (obj:name insn)
"\n"
"Fields:\n"
(string-map (lambda (ifld value)
(dump-ifield ifld value max-name-length))
(insn-iflds insn)
ifld-values)
)))
)
; Return the <insn> object matching VALUE.
; VALUE is either a single number of size base-insn-bitsize,
; or a list of numbers for variable length ISAs.
; LENGTH is the total length of VALUE in bits.
(define (pgmr-lookup-insn length value)
(arch-analyze-insns! CURRENT-ARCH
#t ; include aliases
#f) ; don't need to analyze semantics
; Return a boolean indicating if BASE matches the base part of <insn> INSN.
(define (match-base base insn)
(let ((mask (compute-insn-base-mask (insn-iflds insn)))
(ivalue (insn-value insn)))
; return (value & mask) == ivalue
(= (logand base mask) ivalue)))
(define (match-rest value insn)
#t)
(let ((base (if (list? value) (car value) value)))
(let loop ((insns (current-insn-list)))
(if (null? insns)
#f
(let ((insn (car insns)))
(if (and (= length (insn-length insn))
(match-base base insn)
(match-rest value insn))
insn
(loop (cdr insns)))))))
)