1 ; CPU description file generator for the GNU assembler testsuite.
2 ; Copyright (C) 2000, 2001, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; This is invoked to build allinsn.exp and a script to run to
7 ; generate allinsn.s and allinsn.d.
9 ; Specify which application.
10 (set! APPLICATION 'GAS-TEST)
12 ; Called before/after the .cpu file has been read.
14 (define (gas-test-init!) (opcodes-init!))
15 (define (gas-test-finish!) (opcodes-finish!))
17 ; Called after .cpu file has been read and global error checks are done.
18 ; We use the `tmp' member to record the syntax split up into its components.
20 (define (gas-test-analyze!)
23 (elm-xset! insn 'tmp (syntax-break-out (insn-syntax insn))))
24 (non-multi-insns (current-insn-list)))
28 ; Methods to compute test data.
29 ; The result is a list of strings to be inserted in the assembler
30 ; in the operand's position.
32 ; For a general assembler operand, just turn the value into a string.
36 (map number->string ops))
39 ; For a keyword operand, choose the appropriate keyword.
43 (let* ((test-cases (elm-get self 'values))
44 (prefix (elm-get self 'prefix)))
47 (if (and (not (string=? prefix ""))
48 (eq? (string-ref prefix 0) #\$))
51 (car (list-ref test-cases n))))
56 <hw-address> 'test-data
58 (let* ((test-cases '("foodata" "4" "footext" "-4"))
60 (selection (map (lambda (z) (random (length test-cases))) (iota n))))
61 (map (lambda (n) (list-ref test-cases n)) selection)))
65 <hw-iaddress> 'test-data
67 (let* ((test-cases '("footext" "4" "foodata" "-4"))
69 (selection (map (lambda (z) (random (length test-cases))) (iota n))))
70 (map (lambda (n) (list-ref test-cases n)) selection)))
73 (method-make-forward! <hw-register> 'indices '(test-data))
74 (method-make-forward! <hw-immediate> 'values '(test-data))
76 ; Test data for a field is chosen firstly out of some bit patterns,
77 ; then randomly. It is then interpreted based on whether there
82 (let* ((bf-len (ifld-length self))
83 (field-max (inexact->exact (round (expt 2 bf-len))))
84 (highbit (quotient field-max 2))
85 (values (map (lambda (n)
92 (else (random field-max))))
94 (decode (ifld-decode self)))
96 ; FIXME: need to run the decoder.
99 (case (mode:class (ifld-mode self))
100 ((INT) (map (lambda (n) (if (>= n highbit) (- n field-max) n))
103 (else (error "unsupported mode class"
104 (mode:class (ifld-mode self))))))))
108 <hw-index> 'test-data
110 (case (hw-index:type self)
111 ((ifield operand) (send (hw-index:value self) 'test-data n))
112 ((constant) (hw-index:value self))
119 (send (op:type self) 'test-data (send (op:index self) 'test-data n)))
122 ; Given an operand, return a set of N test data.
123 ; e.g. For a keyword operand, return a random subset.
124 ; For a number, return N numbers.
126 (define (operand-test-data op n)
127 (send op 'test-data n)
130 ; Given the broken out assembler syntax string, return the list of operand
133 (define (extract-operands syntax-list)
134 (let loop ((result nil) (l syntax-list))
135 (cond ((null? l) (reverse! result))
136 ((object? (car l)) (loop (cons (car l) result) (cdr l)))
137 (else (loop result (cdr l)))))
140 ; Collate a list of operands into a test test.
141 ; Input is a list of operand lists. Returns a collated set of test
142 ; inputs. For example:
143 ; ((r0 r1 r2) (r3 r4 r5) (2 3 8)) => ((r0 r3 2) (r1 r4 3) (r2 r5 8))
145 (define (-collate-test-set L)
146 (if (=? (length (car L)) 0)
149 (-collate-test-set (map cdr L))))
152 ; Given a list of operands for an instruction, return the test set
153 ; (all possible combinations).
154 ; N is the number of testcases for each operand.
155 ; The result has N to-the-power (length OP-LIST) elements.
157 (define (build-test-set op-list n)
158 (let ((test-data (map (lambda (op) (operand-test-data op n)) op-list))
159 (len (length op-list)))
160 (cond ((=? len 0) (list (list)))
161 (else (-collate-test-set test-data))))
164 ; Given an assembler expression and a set of operands build a testcase.
165 ; TEST-DATA is a list of strings, one element per operand.
167 (define (build-asm-testcase syntax-list test-data)
168 (let loop ((result nil) (sl syntax-list) (td test-data))
169 ;(display (list result sl td "\n"))
172 (apply string-append (reverse result))
175 (loop (cons (car sl) result) (cdr sl) td))
176 (else (loop (cons (car td) result) (cdr sl) (cdr td)))))
179 ; Generate the testsuite for INSN.
180 ; FIXME: make the number of cases an argument to this application.
182 (define (gen-gas-test insn)
183 (logit 2 "Generating gas test data for " (obj:name insn) " ...\n")
186 "\t.global " (gen-sym insn) "\n"
188 (let* ((syntax-list (insn-tmp insn))
189 (op-list (extract-operands syntax-list))
190 (test-set (build-test-set op-list 8)))
191 (string-map (lambda (test-data)
192 (build-asm-testcase syntax-list test-data))
197 ; Generate the shell script that builds the .d file.
198 ; .d files contain the objdump result that is used to see whether the
200 ; We do this by running gas and objdump.
201 ; Obviously this isn't quite right - bugs in gas or
202 ; objdump - the things we're testing - will cause an incorrect testsuite to
203 ; be built and thus the bugs will be missed. It is *not* intended that this
204 ; be run immediately before running the testsuite! Rather, this is run to
205 ; generate the testsuite which is then inspected for accuracy and checked
206 ; into CVS. As bugs in the testsuite are found they are corrected by hand.
207 ; Or if they're due to bugs in the generator the generator can be rerun and
208 ; the output diff'd to ensure no errors have crept back in.
209 ; The point of doing things this way is TO SAVE A HELL OF A LOT OF TYPING!
210 ; Clearly some hand generated testcases will also be needed, but this
211 ; provides a good test for each instruction.
213 (define (cgen-build.sh)
214 (logit 1 "Generating gas-build.sh ...\n")
218 # Generate test result data for " (current-arch-name) " GAS testing.
219 # This script is machine generated.
220 # It is intended to be run in the testsuite source directory.
222 # Syntax: build.sh /path/to/build/gas
225 if [ ! -x ../gas/as-new ] ; then
226 echo \"Usage: $0 [/path/to/gas/build]\"
234 if [ ! -x $BUILD/as-new ] ; then
235 echo \"$BUILD is not a gas build directory\"
239 # Put results here, so we preserve the existing set for comparison.
246 $BUILD/as-new ${1}.s -o a.out
247 echo \"#as:\" >${1}.d
248 echo \"#objdump: -dr\" >>${1}.d
249 echo \"#name: $1\" >>${1}.d
250 $BUILD/../binutils/objdump -dr a.out | \
251 sed -e 's/(/\\\\(/g' \
254 -e 's/\\[/\\\\\\[/g' \
255 -e 's/\\]/\\\\\\]/g' \
258 -e 's/[*]/\\\\*/g' | \
259 sed -e 's/^.*file format.*$/.*: +file format .*/' \
264 # Now come all the testcases.
265 cat > allinsn.s <<EOF
270 (string-map (lambda (insn)
272 (non-multi-insns (current-insn-list)))
275 "# Finally, generate the .d file.\n"
280 ; Generate the dejagnu allinsn.exp file that drives the tests.
282 (define (cgen-allinsn.exp)
283 (logit 1 "Generating allinsn.exp ...\n")
286 # " (string-upcase (current-arch-name)) " assembler testsuite. -*- Tcl -*-
288 if [istarget " (current-arch-name) "*-*-*] {
289 run_dump_test \"allinsn\"