OSDN Git Service

From 2001-03-01 Tom Rix <trix@redhat.com>:
[pf3gnuchains/pf3gnuchains3x.git] / cgen / ifield.scm
1 ; Instruction fields.
2 ; Copyright (C) 2000 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
5
6 ; The `<ifield>' class.
7 ; (pronounced "I-field")
8 ;
9 ; These describe raw data, little semantic content is attributed to them.
10 ; The goal being to avoid interfering with future applications.
11 ;
12 ; FIXME: Move start, word-offset, word-length into the instruction format?
13 ; - would require proper ordering of fields in insns, but that's ok.
14 ;   (??? though the sparc64 description shows a case where its useful to
15 ;   not have to worry about instruction ordering - different versions of an
16 ;   insn take different fields and these fields are passed via a macro)
17 ;
18 ; ??? One could treat all ifields as being unsigned.  They could be thought of
19 ; as indices into a table of values, be they signed, unsigned, floating point,
20 ; whatever.  Just an idea.
21 ;
22 ; ??? Split into two?  One for definition, and one for value.
23
24 (define <ifield>
25   (class-make '<ifield>
26               '(<ident>)
27               '(
28                 ; The mode the raw value is to be interpreted in.
29                 mode
30
31                 ; A <bitrange> object.
32                 ; This contains the field's offset, start, length, word-length,
33                 ; and orientation (msb==0, lsb==0).  The orientation is
34                 ; recorded to keep the <bitrange> object self-contained.
35                 ; Endianness is not recorded.
36                 bitrange
37
38                 ; Argument to :follows, as an object.
39                 ; FIXME: wip
40                 (follows . #f)
41
42                 ; ENCODE/DECODE operate on the raw value, absent of any context
43                 ; save `pc' and mode of field.
44                 ; If #f, no special processing is required.
45                 ; ??? It's not clear where the best place to process fields is.
46                 ; An earlier version had insert/extract fields in operands to
47                 ; handle more complicated cases.  Following the goal of
48                 ; incremental complication, the special handling for m32r's
49                 ; f-disp8 field is handled entirely here, rather than partially
50                 ; here and partially in the operand.
51                 encode decode
52
53                 ; Value of field, if there is one.
54                 ; Possible types are: integer, <operand>, ???
55                 value
56                 )
57               nil)
58 )
59
60 ; {value},{follows} are missing on purpose
61 ; {value} is handled specially.
62 ; {follows} is rarely used
63 (method-make-make! <ifield> '(name comment attrs mode bitrange encode decode))
64
65 ; Accessor fns
66 ; ??? `value' is treated specially, needed anymore?
67
68 (define-getters <ifield> ifld (mode encode decode follows))
69
70 (define-setters <ifield> ifld (follows))
71
72 ; internal fn
73 (define -ifld-bitrange (elm-make-getter <ifield> 'bitrange))
74
75 (define (ifld-word-offset f) (bitrange-word-offset (-ifld-bitrange f)))
76 (define (ifld-word-length f) (bitrange-word-length (-ifld-bitrange f)))
77
78 ; Return the mode of the value passed to the encode rtl.
79 ; This is the mode of the result of the decode rtl.
80
81 (define (ifld-encode-mode f)
82   (if (ifld-decode f)
83       ; cadr/cadr gets WI in ((value pc) (sra WI ...))
84       (mode:lookup (cadr (cadr (ifld-decode f))))
85       (ifld-mode f))
86 )
87
88 ; Return the mode of the value passed to the decode rtl.
89 ; This is the mode of the field.
90
91 (define (ifld-decode-mode f) (ifld-mode f))
92
93 ; Return start of ifield.
94 ; WORD-LEN is the length of the word in which to compute the value or
95 ; #f meaning to use the default length (recorded with the bitrange).
96 ; WORD-LEN is present for architectures like the m32r where there are insns
97 ; smaller than the base insn size (LIW).
98 ; ??? Not sure it'll be applicable to other LIW architectures.  The m32r is
99 ; rather easy as the insns are 16 and 32 bits.
100 ; ??? Another way to do this would be to either set the base-insn-size for
101 ; the m32r to be 16 bits, or to add a new field to hold the insn-word-size
102 ; and set it to 16 for the m32r.  The problem here is that there is no
103 ; canonicalization that works regardless of whether a "word" is shortened
104 ; or lengthened.
105
106 (method-make-virtual!
107  <ifield> 'field-start
108  (lambda (self word-len)
109    (let* ((bitrange (-ifld-bitrange self))
110           (lsb0? (bitrange-lsb0? bitrange))
111           (recorded-word-len (bitrange-word-length bitrange))
112           (wanted-word-len (or word-len recorded-word-len)))
113      ; Note that this is only intended for situations like the m32r.
114      ; If it doesn't work elsewhere, it may be that you need to
115      ; do things different (use two fields instead of one).
116      (cond ((= wanted-word-len recorded-word-len)
117             (bitrange-start bitrange))
118            ((< wanted-word-len recorded-word-len)
119             ; smaller word wanted
120             (if lsb0?
121                 (- (bitrange-start bitrange) (- recorded-word-len
122                                                 wanted-word-len))
123                 (bitrange-start bitrange)))
124            (else
125             ; larger word wanted
126             (if lsb0?
127                 (+ (bitrange-start bitrange) (- wanted-word-len
128                                                 recorded-word-len))
129                 (bitrange-start bitrange))))))
130 )
131
132 (define (ifld-start ifld word-len)
133   (send ifld 'field-start word-len)
134 )
135
136 (method-make-virtual!
137  <ifield> 'field-length
138  (lambda (self)
139    (bitrange-length (elm-get self 'bitrange)))
140 )
141
142 (define (ifld-length f) (send f 'field-length))
143
144 ; FIXME: It might make things more "readable" if enum values were preserved in
145 ; their symbolic form and the get-field-value method did the lookup.
146
147 (method-make!
148  <ifield> 'get-field-value
149  (lambda (self)
150    (elm-get self 'value))
151 )
152 (define (ifld-get-value self)
153   (send self 'get-field-value)
154 )
155 (method-make!
156  <ifield> 'set-field-value!
157  (lambda (self new-val)
158    (elm-set! self 'value new-val))
159 )
160 (define (ifld-set-value! self new-val)
161   (send self 'set-field-value! new-val)
162 )
163
164 ; Return a boolean indicating if X is an <ifield>.
165
166 (define (ifield? x) (class-instance? <ifield> x))
167
168 ; Return ilk of field.
169 ; ("ilk" sounds klunky but "type" is too ambiguous.  Here "ilk" means
170 ; the kind of the hardware element, enum, etc.)
171 ; The result is a character string naming the field type.
172
173 (define (ifld-ilk fld)
174   (let ((value (elm-xget fld 'value)))
175     ; ??? One could require that the `value' field always be an object.
176     ; I can't get too worked up over it yet.
177     (if (object? value)
178         (obj:name value) ; send's message 'get-name to fetch object's `name'
179         "#")) ; # -> "it's a number"
180 )
181
182 ; Generate the name of the enum for instruction field ifld.
183 ; If PREFIX? is present and #f, the @ARCH@_ prefix is omitted.
184
185 (define (ifld-enum ifld . prefix?)
186   (string-upcase (string-append (if (or (null? prefix?) (car prefix?))
187                                     "@ARCH@_"
188                                     "")
189                                 (gen-sym ifld)))
190 )
191
192 ; Return a boolean indicating if ifield F is an opcode field
193 ; (has a constant value).
194
195 (define (ifld-constant? f)
196   (number? (ifld-get-value f))
197 ;  (and (number? (ifld-get-value f))
198 ;       (if option:reserved-as-opcode?
199 ;          #t
200 ;          (not (has-attr? f 'RESERVED))))
201 )
202
203 ; Return a boolean indicating if ifield F is an operand.
204 ; FIXME: Should check for operand? or some such.
205
206 (define (ifld-operand? f) (not (number? (ifld-get-value f))))
207
208 ; Return known value table for rtx-simplify of <ifield> list ifld-list.
209
210 (define (ifld-known-values ifld-list)
211   (let ((constant-iflds (find ifld-constant? (collect ifld-base-ifields ifld-list))))
212     (map (lambda (f)
213            (cons (obj:name f)
214                  (rtx-make-const 'INT (ifld-get-value f))))
215          constant-iflds))
216 )
217
218 ; Return mask to use for a field in <bitrange> CONTAINER.
219 ; If the bitrange is outside the range of the field, return 0.
220 ; If CONTAINER is #f, use the recorded bitrange.
221 ; BASE-LEN, if non-#f, overrides the base insn length of the insn.
222 ; BASE-LEN is present for architectures like the m32r where there are insns
223 ; smaller than the base insn size (LIW).
224 ;
225 ; Simplifying restrictions [to be relaxed as necessary]:
226 ; - the field must either be totally contained within CONTAINER or totally
227 ;   outside it, partial overlaps aren't handled
228 ; - CONTAINER must be an integral number of bytes, beginning on a
229 ;   byte boundary [simplifies things]
230 ; - both SELF's bitrange and CONTAINER must have the same word length
231 ; - LSB0? of SELF's bitrange and CONTAINER must be the same
232
233 (method-make!
234  <ifield> 'field-mask
235  (lambda (self base-len container)
236    (let* ((container (or container (-ifld-bitrange self)))
237           (bitrange (-ifld-bitrange self))
238           (recorded-word-length (bitrange-word-length bitrange))
239           (word-offset (bitrange-word-offset bitrange)))
240      (let ((lsb0? (bitrange-lsb0? bitrange))
241            (start (bitrange-start bitrange))
242            (length (bitrange-length bitrange))
243            (word-length (or (and (= word-offset 0) base-len)
244                             recorded-word-length))
245            (container-word-offset (bitrange-word-offset container))
246            (container-word-length (bitrange-word-length container)))
247        (cond
248         ; must be same lsb0
249         ((not (eq? lsb0? (bitrange-lsb0? container)))
250          (error "field-mask: different lsb0? values"))
251         ((not (= word-length container-word-length))
252          0)
253         ; container occurs after?
254         ((<= (+ word-offset word-length) container-word-offset)
255          0)
256         ; container occurs before?
257         ((>= word-offset (+ container-word-offset container-word-length))
258          0)
259         (else
260          (word-mask start length word-length lsb0? #f))))))
261 )
262
263 (define (ifld-mask ifld base-len container)
264   (send ifld 'field-mask base-len container)
265 )
266
267 ; Return VALUE inserted into the field's position.
268 ; BASE-LEN, if non-#f, overrides the base insn length of the insn.
269 ; BASE-LEN is present for architectures like the m32r where there are insns
270 ; smaller than the base insn size (LIW).
271
272 (method-make!
273  <ifield> 'field-value
274  (lambda (self base-len value)
275    (let* ((bitrange (-ifld-bitrange self))
276           (recorded-word-length (bitrange-word-length bitrange))
277           (word-offset (bitrange-word-offset bitrange))
278           (word-length (or (and (= word-offset 0) base-len)
279                            recorded-word-length)))
280      (word-value (ifld-start self base-len)
281                  (bitrange-length bitrange)
282                  word-length
283                  (bitrange-lsb0? bitrange) #f
284                  value)))
285 )
286
287 ; FIXME: confusion with ifld-get-value.
288 (define (ifld-value f base-len value)
289   (send f 'field-value base-len value)
290 )
291
292 ; Return a list of ifields required to compute <ifield> F's value.
293 ; Normally this is just F itself.  For multi-ifields it will be more.
294 ; ??? It can also be more if F's value is derived from other fields but
295 ; that isn't supported yet.
296
297 (method-make!
298  <ifield> 'needed-iflds
299  (lambda (self)
300    (list self))
301 )
302
303 (define (ifld-needed-iflds f)
304   (send f 'needed-iflds)
305 )
306
307 ; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
308 ; VALUE is the entire insn's value if it fits in a word, or is a list
309 ; of values, one per word (not implemented, sigh).
310 ; ??? The instruction's format should specify where the word boundaries are.
311
312 (method-make!
313  <ifield> 'field-extract
314  (lambda (self insn value)
315    (let ((base-len (insn-base-mask-length insn)))
316      (word-extract (ifld-start self base-len)
317                    (ifld-length self)
318                    base-len
319                    (ifld-lsb0? self)
320                    #f ; start is msb
321                    value)))
322 )
323
324 (define (ifld-extract ifld value insn)
325   (send ifld 'field-extract value insn)
326 )
327
328 ; Return a boolean indicating if bit 0 is the least significant bit.
329
330 (method-make!
331  <ifield> 'field-lsb0?
332  (lambda (self)
333    (bitrange-lsb0? (-ifld-bitrange self)))
334 )
335
336 (define (ifld-lsb0? f) (send f 'field-lsb0?))
337
338 ; Return the minimum value of a field.
339
340 (method-make!
341  <ifield> 'min-value
342  (lambda (self)
343   (case (mode:class (ifld-mode self))
344     ((INT) (- (integer-expt 2 (- (ifld-length self) 1))))
345     ((UINT) 0)
346     (else (error "unsupported mode class" (mode:class (ifld-mode self))))))
347 )
348
349 ; Return the maximum value of a field.
350
351 (method-make!
352  <ifield> 'max-value
353  (lambda (self)
354   (case (mode:class (ifld-mode self))
355     ((INT) (- (integer-expt 2 (- (ifld-length self) 1)) 1))
356     ((UINT) (- (integer-expt 2 (ifld-length self)) 1))
357     (else (error "unsupported mode class" (mode:class (ifld-mode self))))))
358 )
359
360 ; Create a copy of field F with value VALUE.
361 ; VALUE is either ... ???
362
363 (define (ifld-new-value f value)
364   (let ((new-f (object-copy-top f)))
365     (ifld-set-value! new-f value)
366     new-f)
367 )
368
369 ; Change the offset of the word containing an ifield to {word-offset}.
370
371 (method-make!
372  <ifield> 'set-word-offset!
373  (lambda (self word-offset)
374    (let ((bitrange (object-copy-top (-ifld-bitrange self))))
375      (bitrange-set-word-offset! bitrange word-offset)
376      (elm-set! self 'bitrange bitrange)
377      *UNSPECIFIED*))
378 )
379 (define (ifld-set-word-offset! f word-offset)
380   (send f 'set-word-offset! word-offset)
381 )
382
383 ; Return a copy of F with new {word-offset}.
384
385 (define (ifld-new-word-offset f word-offset)
386   (let ((new-f (object-copy-top f)))
387     (ifld-set-word-offset! new-f word-offset)
388     new-f)
389 )
390
391 ; Return the bit offset of the word after the word <ifield> F is in.
392 ; What a `word' here is defined by F in its bitrange.
393
394 (method-make!
395  <ifield> 'next-word
396  (lambda (self)
397   (let ((br (-ifld-bitrange f)))
398     (bitrange-next-word br)))
399 )
400
401 (define (ifld-next-word f) (send f 'next-word))
402
403 ; Return a boolean indicating if <ifield> F1 precedes <ifield> F2.
404 ; FIXME: Move into a method as different subclasses will need
405 ; different handling.
406
407 (define (ifld-precedes? f1 f2)
408   (let ((br1 (-ifld-bitrange f1))
409         (br2 (-ifld-bitrange f2)))
410     (cond ((< (bitrange-word-offset br1) (bitrange-word-offset br2))
411            #t)
412           ((= (bitrange-word-offset br1) (bitrange-word-offset br2))
413            (begin
414              (assert (eq? (bitrange-lsb0? br1) (bitrange-lsb0? br2)))
415              (assert (= (bitrange-word-length br1) (bitrange-word-length br1)))
416              ; ??? revisit
417              (if (bitrange-lsb0? br1)
418                  (> (bitrange-start br1) (bitrange-start br2))
419                  (< (bitrange-start br1) (bitrange-start br2)))))
420           (else
421            #f)))
422 )
423 \f
424 ; Parse an ifield definition.
425 ; This is the main routine for building an ifield object from a
426 ; description in the .cpu file.
427 ; All arguments are in raw (non-evaluated) form.
428 ; The result is the parsed object or #f if object isn't for selected mach(s).
429 ;
430 ; Two forms of specification are supported, loosely defined as the RISC way
431 ; and the CISC way.  The reason for the distinction is to simplify ifield
432 ; specification of RISC-like cpus.
433 ; Note that VLIW's are another way.  These are handled like the RISC way, with
434 ; the possible addition of instruction framing (which is, surprise surprise,
435 ; wip).
436 ;
437 ; RISC:
438 ; WORD-OFFSET and WORD-LENGTH are #f.  Insns are assumed to be N copies of
439 ; (isa-default-insn-word-bitsize).  WORD-OFFSET is computed from START.
440 ; START is the offset in bits from the start of the insn.
441 ; FLENGTH is the length of the field in bits.
442 ;
443 ; CISC:
444 ; WORD-OFFSET is the offset in bits from the start to the first byte of the
445 ; word containing the ifield.
446 ; WORD-LENGTH is the length in bits of the word containing the ifield.
447 ; START is the starting bit number in the word.  Bit numbering is taken from
448 ; (current-arch-insn-lsb0?).
449 ; FLENGTH is the length in bits of the ifield.  It is named that way to avoid
450 ; collision with the proc named `length'.
451 ;
452 ; FIXME: More error checking.
453
454 (define (-ifield-parse errtxt name comment attrs
455                        word-offset word-length start flength follows
456                        mode encode decode)
457   (logit 2 "Processing ifield " name " ...\n")
458
459   (let* ((name (parse-name name errtxt))
460          (atlist (atlist-parse attrs "cgen_ifld" errtxt))
461          (isas (bitset-attr->list (atlist-attr-value atlist 'ISA #f))))
462
463     ; No longer ensure only one isa specified.
464     ;(if (!= (length isas) 1)
465     ;   (parse-error errtxt "can only specify 1 isa" attrs))
466
467     (if (not (eq? (->bool word-offset)
468                   (->bool word-length)))
469         (parse-error errtxt "either both or neither of word-offset,word-length can be specified"))
470
471     (if (keep-isa-atlist? atlist #f)
472
473         (let ((isa (current-isa-lookup (car isas)))
474               (word-offset (and word-offset
475                                 (parse-number errtxt word-offset '(0 . 256))))
476               (word-length (and word-length
477                                 (parse-number errtxt word-length '(0 . 128))))
478               ; ??? 0.127 for now
479               (start (parse-number errtxt start '(0 . 127)))
480               ; ??? 0.127 for now
481               (flength (parse-number errtxt flength '(0 . 127)))
482               (lsb0? (current-arch-insn-lsb0?))
483               (mode-obj (parse-mode-name mode errtxt))
484               (follows-obj (-ifld-parse-follows errtxt follows))
485               )
486
487           ; Calculate the <bitrange> object.
488           ; FIXME: word-offset/word-length computation needs work.
489           ; Move positional info to format?
490           (let ((bitrange
491                  (if word-offset
492                      ; CISC
493                      (make <bitrange>
494                        word-offset start flength word-length lsb0?)
495                      ; RISC
496                      (let* ((default-insn-word-bitsize
497                               (isa-default-insn-word-bitsize isa))
498                             (word-offset
499                              (- start
500                                 (remainder start
501                                            default-insn-word-bitsize)))
502                             (start (remainder start default-insn-word-bitsize)))
503                        (make <bitrange>
504                          word-offset
505                          start
506                          flength
507                          (if lsb0?
508                              (* (quotient (+ start 1
509                                              (- default-insn-word-bitsize 1))
510                                           default-insn-word-bitsize)
511                                 default-insn-word-bitsize)
512                              (* (quotient (+ start flength
513                                              (- default-insn-word-bitsize 1))
514                                           default-insn-word-bitsize)
515                                 default-insn-word-bitsize))
516                          lsb0?))))
517                  )
518
519             (let ((result
520                    (make <ifield>
521                          name
522                          (parse-comment comment errtxt)
523                          atlist
524                          mode-obj
525                          bitrange
526                          (-ifld-parse-encode errtxt encode)
527                          (-ifld-parse-decode errtxt decode))))
528               (if follows-obj
529                   (ifld-set-follows! result follows-obj))
530               result)))
531
532         ; Else ignore entry.
533         (begin
534           (logit 2 "Ignoring " name ".\n")
535           #f)))
536 )
537
538 ; Read an instruction field description.
539 ; This is the main routine for analyzing instruction fields in the .cpu file.
540 ; ERRTXT is prepended to error messages to provide context.
541 ; ARG-LIST is an associative list of field name and field value.
542 ; -ifield-parse is invoked to create the <ifield> object.
543
544 (define (-ifield-read errtxt . arg-list)
545   (let (; Current ifield elements:
546         (name nil)
547         (comment "")
548         (attrs nil)
549         (word-offset #f)
550         (word-length #f)
551         (start 0)
552         ; FIXME: Hobbit computes the wrong symbol for `length'
553         ; in the `case' expression below because there is a local var
554         ; of the same name ("__1" gets appended to the symbol name).
555         ; As a workaround we name it "length-".
556         (length- 0)
557         (follows #f)
558         (mode 'UINT)
559         (encode #f)
560         (decode #f)
561         )
562     ; Loop over each element in ARG-LIST, recording what's found.
563     (let loop ((arg-list arg-list))
564       (if (null? arg-list)
565           nil
566           (let ((arg (car arg-list))
567                 (elm-name (caar arg-list)))
568             (case elm-name
569               ((name) (set! name (cadr arg)))
570               ((comment) (set! comment (cadr arg)))
571               ((attrs) (set! attrs (cdr arg)))
572               ((mode) (set! mode (cadr arg)))
573               ((word-offset) (set! word-offset (cadr arg)))
574               ((word-length) (set! word-length (cadr arg)))
575               ((start) (set! start (cadr arg)))
576               ((length) (set! length- (cadr arg)))
577               ((follows) (set! follows (cadr arg)))
578               ((encode) (set! encode (cdr arg)))
579               ((decode) (set! decode (cdr arg)))
580               (else (parse-error errtxt "invalid ifield arg" arg)))
581             (loop (cdr arg-list)))))
582
583     ; See if encode/decode were specified as "unspecified".
584     ; This happens with shorthand macros.
585     (if (and (pair? encode)
586              (eq? (car encode) #f))
587         (set! encode #f))
588     (if (and (pair? decode)
589              (eq? (car decode) #f))
590         (set! decode #f))
591
592     ; Now that we've identified the elements, build the object.
593     (-ifield-parse errtxt name comment attrs
594                    word-offset word-length start length- follows
595                    mode encode decode)
596     )
597 )
598
599 ; Parse a `follows' spec.
600
601 (define (-ifld-parse-follows errtxt follows)
602   (if follows
603       (let ((follows-obj (current-op-lookup follows)))
604         (if (not follows-obj)
605             (parse-error errtxt "unknown operand to follow" follows))
606         follows-obj)
607       #f)
608 )
609
610 ; Do common parts of <ifield> encode/decode processing.
611
612 (define (-ifld-parse-encode-decode errtxt which value)
613   (if value
614       (begin
615         (if (or (not (list? value))
616                 (not (= (length value) 2))
617                 (not (list? (car value)))
618                 (not (= (length (car value)) 2))
619                 (not (list? (cadr value))))
620             (parse-error errtxt
621                          (string-append "bad ifield " which " spec")
622                          value))
623         (if (or (not (> (length (cadr value)) 2))
624                 (not (mode:lookup (cadr (cadr value)))))
625             (parse-error errtxt
626                          (string-append which " expression must have a mode")
627                          value))))
628   value
629 )
630
631 ; Parse an <ifield> encode spec.
632
633 (define (-ifld-parse-encode errtxt encode)
634   (-ifld-parse-encode-decode errtxt "encode" encode)
635 )
636
637 ; Parse an <ifield> decode spec.
638
639 (define (-ifld-parse-decode errtxt decode)
640   (-ifld-parse-encode-decode errtxt "decode" decode)
641 )
642
643 ; Define an instruction field object, name/value pair list version.
644
645 (define define-ifield
646   (lambda arg-list
647     (let ((f (apply -ifield-read (cons "define-ifield" arg-list))))
648       (if f
649           (current-ifld-add! f))
650       f))
651 )
652
653 ; Define an instruction field object, all arguments specified.
654 ; ??? Leave out word-offset,word-length,follows for now (RISC version).
655 ; Not sure whether to add another function or leave CISC cpu's to define
656 ; a shorthand macro if they want.
657
658 (define (define-full-ifield name comment attrs start length mode encode decode)
659   (let ((f (-ifield-parse "define-full-ifield" name comment attrs
660                           #f #f start length #f mode encode decode)))
661     (if f
662         (current-ifld-add! f))
663     f)
664 )
665
666 (define (-ifield-add-commands!)
667   (reader-add-command! 'define-ifield
668                        "\
669 Define an instruction field, name/value pair list version.
670 "
671                        nil 'arg-list define-ifield)
672   (reader-add-command! 'define-full-ifield
673                        "\
674 Define an instruction field, all arguments specified.
675 "
676                        nil '(name comment attrs start length mode encode decode)
677                        define-full-ifield)
678   (reader-add-command! 'define-multi-ifield
679                        "\
680 Define an instruction multi-field, name/value pair list version.
681 "
682                        nil 'arg-list define-multi-ifield)
683   (reader-add-command! 'define-full-multi-ifield
684                        "\
685 Define an instruction multi-field, all arguments specified.
686 "
687                        nil '(name comment attrs mode subflds insert extract)
688                        define-full-multi-ifield)
689
690   *UNSPECIFIED*
691 )
692 \f
693 ; Instruction fields consisting of multiple parts.
694
695 (define <multi-ifield>
696   (class-make '<multi-ifield>
697               '(<ifield>)
698               '(
699                 ; List of <ifield> objects.
700                 subfields
701                 ; rtl to set SUBFIELDS from self
702                 insert
703                 ; rtl to set self from SUBFIELDS
704                 extract
705                 )
706               nil)
707 )
708
709 ; Accessors
710
711 (define-getters <multi-ifield> multi-ifld
712   (subfields insert extract)
713 )
714
715 ; Return a boolean indicating if X is an <ifield>.
716
717 (define (multi-ifield? x) (class-instance? <multi-ifield> x))
718
719 (define (non-multi-ifields ifld-list)
720   (find (lambda (ifld) (not (multi-ifield? ifld))) ifld-list)
721 )
722
723 (define (non-derived-ifields ifld-list)
724   (find (lambda (ifld) (not (derived-ifield? ifld))) ifld-list)
725 )
726
727
728 ; Return the starting bit number of the first field.
729
730 (method-make-virtual!
731  <multi-ifield> 'field-start
732  (lambda (self word-len)
733    (apply min (map (lambda (f) (ifld-start f #f)) (elm-get self 'subfields))))
734 )
735
736 ; Return the total length.
737
738 (method-make-virtual!
739  <multi-ifield> 'field-length
740  (lambda (self)
741    (apply + (map ifld-length (elm-get self 'subfields))))
742 )
743
744 ; Return the bit offset of the word after the last word SELF is in.
745 ; What a `word' here is defined by subfields in their bitranges.
746
747 (method-make!
748  <multi-ifield> 'next-word
749  (lambda (self)
750    (apply max (map (lambda (f)
751                      (bitrange-next-word (-ifld-bitrange f)))
752                    (multi-ifld-subfields self))))
753 )
754
755 ; Return mask of field in bitrange CONTAINER.
756
757 (method-make!
758  <multi-ifield> 'field-mask
759  (lambda (self base-len container)
760    (apply + (map (lambda (f) (ifld-mask f base-len container)) (elm-get self 'subfields))))
761 )
762
763 ; Return VALUE inserted into the field's position.
764 ; The value is spread out over the various subfields in sorted order.
765 ; We assume the subfields have been sorted by starting bit position.
766
767 (method-make!
768  <multi-ifield> 'field-value
769  (lambda (self base-len value)
770    (apply + (map (lambda (f) (ifld-value f base-len value)) (elm-get self 'subfields))))
771 )
772
773 ; Return a list of ifields required to compute the field's value.
774
775 (method-make!
776  <multi-ifield> 'needed-iflds
777  (lambda (self)
778    (cons self (elm-get self 'subfields)))
779 )
780
781 ; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
782 ; VALUE is the entire insn's value if it fits in a word, or is a list
783 ; of values, one per word (not implemented, sigh).
784 ; ??? The instruction's format should specify where the word boundaries are.
785
786 (method-make!
787  <multi-ifield> 'field-extract
788  (lambda (self insn value)
789    (let* ((subflds (sort-ifield-list (elm-get self 'subfields)
790                                      (not (ifld-lsb0? self))))
791           (subvals (map (lambda (subfld)
792                           (ifld-extract subfld insn value))
793                         subflds))
794          )
795      ; We have each subfield's value, now concatenate them.
796      (letrec ((plus-scan (lambda (lengths current)
797                            ; do the -1 drop here as it's easier
798                            (if (null? (cdr lengths))
799                                nil
800                                (cons current
801                                      (plus-scan (cdr lengths)
802                                                 (+ current (car lengths))))))))
803        (apply + (map logsll
804                      subvals
805                      (plus-scan (map ifld-length subflds) 0))))))
806 )
807
808 ; Return a boolean indicating if bit 0 is the least significant bit.
809
810 (method-make!
811  <multi-ifield> 'field-lsb0?
812  (lambda (self)
813    (ifld-lsb0? (car (elm-get self 'subfields))))
814 )
815 \f
816 ; Multi-ifield parsing.
817
818 ; Subroutine of -multi-ifield-parse to build the default insert expression.
819
820 (define (-multi-ifield-make-default-insert container-name subfields)
821   (let* ((lengths (map ifld-length subfields))
822          (shifts (cons 0 (list-tail-drop 1 (plus-scan (cons 0 lengths))))))
823     ; Build RTL expression to shift and mask each ifield into right spot.
824     (let ((exprs (map (lambda (f length shift)
825                         (rtx-make 'and (rtx-make 'srl container-name shift)
826                                   (mask length)))
827                       subfields lengths shifts)))
828       ; Now set each ifield with their respective values.
829       (apply rtx-make (cons 'sequence
830                             (cons nil
831                                   (map (lambda (f expr)
832                                          (rtx-make-set f expr))
833                                        subfields exprs))))))
834 )
835
836 ; Subroutine of -multi-ifield-parse to build the default extract expression.
837
838 (define (-multi-ifield-make-default-extract container-name subfields)
839   (let* ((lengths (map ifld-length subfields))
840          (shifts (cons 0 (list-tail-drop 1 (plus-scan (cons 0 lengths))))))
841     ; Build RTL expression to shift and mask each ifield into right spot.
842     (let ((exprs (map (lambda (f length shift)
843                         (rtx-make 'sll (rtx-make 'and (obj:name f)
844                                                  (mask length))
845                                   shift))
846                       subfields lengths shifts)))
847       ; Now set {container-name} with all the values or'd together.
848       (rtx-make-set container-name
849                     (rtx-combine 'or exprs))))
850 )
851
852 ; Parse a multi-ifield spec.
853 ; This is the main routine for building the object from the .cpu file.
854 ; All arguments are in raw (non-evaluated) form.
855 ; The result is the parsed object or #f if object isn't for selected mach(s).
856
857 (define (-multi-ifield-parse errtxt name comment attrs mode subfields insert extract encode decode)
858   (logit 2 "Processing multi-ifield element " name " ...\n")
859
860   (let* ((name (parse-name name errtxt))
861          (atlist (atlist-parse attrs "cgen_ifld" errtxt))
862          (isas (bitset-attr->list (atlist-attr-value atlist 'ISA #f))))
863     
864     ; No longer ensure only one isa specified.
865     ; (if (!= (length isas) 1) 
866     ;     (parse-error errtxt "can only specify 1 isa" attrs))
867     
868     (if (keep-isa-atlist? atlist #f)
869         (begin
870           (let ((result (new <multi-ifield>))
871                 (subfields (map (lambda (subfld)
872                                   (let ((f (current-ifld-lookup subfld)))
873                                     (if (not f)
874                                         (parse-error errtxt "unknown ifield" subfld))
875                                     f))
876                                 subfields)))
877             
878             (elm-xset! result 'name name)
879             (elm-xset! result 'comment (parse-comment comment errtxt))
880                                         ; multi-ifields are always VIRTUAL
881             (elm-xset! result 'attrs
882                        (atlist-parse (cons 'VIRTUAL attrs) "multi-ifield" errtxt))
883             (elm-xset! result 'mode (parse-mode-name mode errtxt))
884             (elm-xset! result 'encode (-ifld-parse-encode errtxt encode))
885             (elm-xset! result 'decode (-ifld-parse-encode errtxt decode))
886             (if insert
887                 (elm-xset! result 'insert insert)
888                 (elm-xset! result 'insert
889                            (-multi-ifield-make-default-insert name subfields)))
890             (if extract
891                 (elm-xset! result 'extract extract)
892                 (elm-xset! result 'extract
893                            (-multi-ifield-make-default-extract name subfields)))
894             (elm-xset! result 'subfields subfields)
895             result))
896         ; else don't keep isa
897         #f))
898 )
899
900 ; Read an instruction multi-ifield.
901
902 (define (-multi-ifield-read errtxt . arg-list)
903   (let (; Current multi-ifield elements:
904         (name nil)
905         (comment "")
906         (attrs nil)
907         (mode 'UINT)
908         (subflds nil)
909         (insert #f)
910         (extract #f)
911         (encode #f)
912         (decode #f)
913         )
914     ; Loop over each element in ARG-LIST, recording what's found.
915     (let loop ((arg-list arg-list))
916       (if (null? arg-list)
917           nil
918           (let ((arg (car arg-list))
919                 (elm-name (caar arg-list)))
920             (case elm-name
921               ((name) (set! name (cadr arg)))
922               ((comment) (set! comment (cadr arg)))
923               ((attrs) (set! attrs (cdr arg)))
924               ((mode) (set! mode (cadr arg)))
925               ((subfields) (set! subflds (cdr arg)))
926               ((insert) (set! insert (cadr arg)))
927               ((extract) (set! extract (cadr arg)))
928               ((encode) (set! encode (cdr arg)))
929               ((decode) (set! decode (cdr arg)))
930               (else (parse-error errtxt "invalid ifield arg" arg)))
931             (loop (cdr arg-list)))))
932     ; Now that we've identified the elements, build the object.
933     (-multi-ifield-parse errtxt name comment attrs mode subflds insert extract encode decode)
934     )
935 )
936
937 ; Define an instruction multi-field object, name/value pair list version.
938
939 (define define-multi-ifield
940   (lambda arg-list
941     (let ((f (apply -multi-ifield-read (cons "define-multi-ifield" arg-list))))
942       (if f
943           (current-ifld-add! f))
944       f))
945 )
946
947 ; Define an instruction multi-field object, all arguments specified.
948
949 (define (define-full-multi-ifield name comment attrs mode subflds insert extract)
950   (let ((f (-multi-ifield-parse "define-full-multi-ifield" name comment attrs
951                                 mode subflds insert extract #f #f)))
952     (current-ifld-add! f)
953     f)
954 )
955 \f
956 ; Derived ifields (ifields based on one or more other ifields).
957 ; These support the complicated requirements of CISC instructions
958 ; where one "ifield" is actually a placeholder for an addressing mode
959 ; which can consist of several ifields.
960 ; These are also intended to support other complex ifield usage.
961 ;
962 ; Derived ifields are (currently) always machine generated from other
963 ; elements of the description file so there is no reader support.
964 ;
965 ; ??? experimental and wip!
966 ; ??? These are kind of like multi-ifields but I don't want to disturb them
967 ; while this is still experimental.
968
969 (define <derived-ifield>
970   (class-make '<derived-ifield>
971               '(<ifield>)
972               '(
973                 ; Operand that uses this ifield.
974                 ; Unlike other ifields, derived ifields have a one-to-one
975                 ; correspondence with the operand that uses them.
976                 ; ??? Not true in -anyof-merge-subchoices.
977                 owner
978
979                 ; List of ifields that make up this ifield.
980                 subfields
981                 )
982               nil)
983 )
984
985
986 (method-make!
987  <derived-ifield> 'needed-iflds
988  (lambda (self)
989    (find (lambda (ifld) (not (ifld-constant? ifld)))
990          (elm-get self 'subfields)))
991 )
992
993
994 (method-make!
995  <derived-ifield> 'make!
996  (lambda (self name comment attrs owner subfields)
997    (elm-set! self 'name name)
998    (elm-set! self 'comment comment)
999    (elm-set! self 'attrs attrs)
1000    (elm-set! self 'mode UINT)
1001    (elm-set! self 'bitrange (make <bitrange> 0 0 0 0 #f))
1002    (elm-set! self 'owner owner)
1003    (elm-set! self 'subfields subfields)
1004    self)
1005 )
1006
1007 ; Accessors.
1008
1009 (define-getters <derived-ifield> derived-ifield (owner subfields))
1010
1011 (define-setters <derived-ifield> derived-ifield (owner subfields))
1012
1013 (define (derived-ifield? x) (class-instance? <derived-ifield> x))
1014
1015 ; Return a boolean indicating if F is a derived ifield with a derived operand
1016 ; for a value.
1017 ; ??? The former might imply the latter so some simplification may be possible.
1018
1019 (define (ifld-derived-operand? f)
1020   (and (derived-ifield? f)
1021        (derived-operand? (ifld-get-value f)))
1022 )
1023
1024 ; Return the bit offset of the word after the last word SELF is in.
1025 ; What a `word' here is defined by subfields in their bitranges.
1026
1027 (method-make!
1028  <derived-ifield> 'next-word
1029  (lambda (self)
1030    (apply max (map (lambda (f)
1031                      (bitrange-next-word (-ifld-bitrange f)))
1032                    (derived-ifield-subfields self))))
1033 )
1034
1035
1036 ; Traverse the ifield to collect all base (non-derived) ifields used in it.
1037 (define (ifld-base-ifields ifld)
1038   (cond ((derived-ifield? ifld) (collect (lambda (subfield) (ifld-base-ifields subfield))
1039                                          (derived-ifield-subfields ifld)))
1040         ; ((multi-ifield? ifld) (collect (lambda (subfield) (ifld-base-ifields subfield))
1041         ;                              (multi-ifld-subfields ifld)))
1042         (else (list ifld)))
1043 )
1044
1045
1046 \f
1047 ; Misc. utilities.
1048
1049 ; Sort a list of fields (sorted by the starting bit number).
1050 ; This must be carefully defined to pass through Hobbit.
1051 ; (define foo (if x bar baz)) is ok.
1052 ; (if x (define foo bar) (define foo baz)) is not ok.
1053 ;
1054 ; ??? Usually there aren't that many fields and the range of values is fixed,
1055 ; so I think this needn't use a general purpose sort routine (should it become
1056 ; an issue).
1057
1058 (define sort-ifield-list
1059   (if (and (defined? 'cgh-qsort) (defined? 'cgh-qsort-int-cmp))
1060       (lambda (fld-list up?)
1061         (cgh-qsort fld-list
1062                    (if up?
1063                        (lambda (a b)
1064                          (cgh-qsort-int-cmp (ifld-start a #f)
1065                                             (ifld-start b #f)))
1066                        (lambda (a b)
1067                          (- (cgh-qsort-int-cmp (ifld-start a #f)
1068                                                (ifld-start b #f)))))))
1069       (lambda (fld-list up?)
1070         (sort fld-list
1071               (if up?
1072                   (lambda (a b) (< (ifld-start a #f)
1073                                    (ifld-start b #f)))
1074                   (lambda (a b) (> (ifld-start a #f)
1075                                    (ifld-start b #f)))))))
1076 )
1077
1078 ; Return a boolean indicating if field F extends beyond the base insn.
1079
1080 (define (ifld-beyond-base? f base-bitsize total-bitsize)
1081   ; old way
1082   ;(< base-bitsize (+ (ifld-start f total-bitsize) (ifld-length f)))
1083   (> (ifld-word-offset f) 0)
1084 )
1085
1086 ; Return the mode of the decoded value of <ifield> F.
1087 ; ??? This is made easy because we require the decode expression to have
1088 ; an explicit mode.
1089
1090 (define (ifld-decode-mode f)
1091   (if (not (elm-bound? f 'decode))
1092       (ifld-mode f)
1093       (let ((d (ifld-decode f)))
1094         (if d
1095             (mode:lookup (cadr (cadr d)))
1096             (ifld-mode f))))
1097 )
1098
1099 ; Return <hardware> object to use to hold value of <ifield> F.
1100 ; i.e. one of h-uint, h-sint.
1101 ; NB: Should be defined in terms of `hardware-for-mode'.
1102 (define (ifld-hw-type f)
1103   (case (mode:class (ifld-mode f))
1104     ((INT) h-sint)
1105     ((UINT) h-uint)
1106     (else (error "unsupported mode class" (mode:class (ifld-mode f)))))
1107 )
1108 \f
1109 ; Builtin fields, attributes, init/fini support.
1110
1111 ; The f-nil field is a placeholder when building operands out of hardware
1112 ; elements that aren't indexed by an instruction field (scalars).
1113 (define f-nil #f)
1114
1115 (define (ifld-nil? f)
1116   (eq? (obj:name f) 'f-nil)
1117 )
1118
1119 ; The f-anyof field is a placeholder when building "anyof" operands.
1120 (define f-anyof #f)
1121
1122 (define (ifld-anyof? f)
1123   (eq? (obj:name f) 'f-anyof)
1124 )
1125
1126 ; Return a boolean indicating if F is an anyof ifield with an anyof operand
1127 ; for a value.
1128 ; ??? The former implies the latter so some simplification is possible.
1129
1130 (define (ifld-anyof-operand? f)
1131   (and (ifld-anyof? f)
1132        (anyof-operand? (ifld-get-value f)))
1133 )
1134
1135 ; Called before loading the .cpu file to initialize.
1136
1137 (define (ifield-init!)
1138   (-ifield-add-commands!)
1139
1140   *UNSPECIFIED*
1141 )
1142
1143 ; Called before loading the .cpu file to create any builtins.
1144
1145 (define (ifield-builtin!)
1146   ; Standard ifield attributes.
1147   ; ??? Some of these can be combined into one, booleans are easier to
1148   ; work with.
1149   (define-attr '(for ifield operand) '(type boolean) '(name PCREL-ADDR)
1150     '(comment "pc relative address"))
1151   (define-attr '(for ifield operand) '(type boolean) '(name ABS-ADDR)
1152     '(comment "absolute address"))
1153   (define-attr '(for ifield) '(type boolean) '(name RESERVED)
1154     '(comment "field is reserved"))
1155   (define-attr '(for ifield operand) '(type boolean) '(name SIGN-OPT)
1156     '(comment "value is signed or unsigned"))
1157   ; ??? This is an internal attribute for implementation purposes only.
1158   ; To be revisited.
1159   (define-attr '(for ifield operand) '(type boolean) '(name SIGNED)
1160     '(comment "value is unsigned"))
1161   ; Also (defined elsewhere): VIRTUAL
1162
1163   (set! f-nil (make <ifield> 'f-nil "empty ifield"
1164                     atlist-empty
1165                     UINT
1166                     (make <bitrange> 0 0 0 0 #f)
1167                     #f #f)) ; encode/decode
1168   (current-ifld-add! f-nil)
1169
1170   (set! f-anyof (make <ifield> 'f-anyof "placeholder for anyof operands"
1171                     atlist-empty
1172                     UINT
1173                     (make <bitrange> 0 0 0 0 #f)
1174                     #f #f)) ; encode/decode
1175   (current-ifld-add! f-anyof)
1176
1177   *UNSPECIFIED*
1178 )
1179
1180 ; Called after the .cpu file has been read in.
1181
1182 (define (ifield-finish!)
1183   *UNSPECIFIED*
1184 )