2 ; Copyright (C) 2000 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; The `<ifield>' class.
7 ; (pronounced "I-field")
9 ; These describe raw data, little semantic content is attributed to them.
10 ; The goal being to avoid interfering with future applications.
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)
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.
22 ; ??? Split into two? One for definition, and one for value.
28 ; The mode the raw value is to be interpreted in.
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.
38 ; Argument to :follows, as an object.
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.
53 ; Value of field, if there is one.
54 ; Possible types are: integer, <operand>, ???
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))
66 ; ??? `value' is treated specially, needed anymore?
68 (define-getters <ifield> ifld (mode encode decode follows))
70 (define-setters <ifield> ifld (follows))
73 (define -ifld-bitrange (elm-make-getter <ifield> 'bitrange))
75 (define (ifld-word-offset f) (bitrange-word-offset (-ifld-bitrange f)))
76 (define (ifld-word-length f) (bitrange-word-length (-ifld-bitrange f)))
78 ; Return the mode of the value passed to the encode rtl.
79 ; This is the mode of the result of the decode rtl.
81 (define (ifld-encode-mode f)
83 ; cadr/cadr gets WI in ((value pc) (sra WI ...))
84 (mode:lookup (cadr (cadr (ifld-decode f))))
88 ; Return the mode of the value passed to the decode rtl.
89 ; This is the mode of the field.
91 (define (ifld-decode-mode f) (ifld-mode f))
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
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
121 (- (bitrange-start bitrange) (- recorded-word-len
123 (bitrange-start bitrange)))
127 (+ (bitrange-start bitrange) (- wanted-word-len
129 (bitrange-start bitrange))))))
132 (define (ifld-start ifld word-len)
133 (send ifld 'field-start word-len)
136 (method-make-virtual!
137 <ifield> 'field-length
139 (bitrange-length (elm-get self 'bitrange)))
142 (define (ifld-length f) (send f 'field-length))
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.
148 <ifield> 'get-field-value
150 (elm-get self 'value))
152 (define (ifld-get-value self)
153 (send self 'get-field-value)
156 <ifield> 'set-field-value!
157 (lambda (self new-val)
158 (elm-set! self 'value new-val))
160 (define (ifld-set-value! self new-val)
161 (send self 'set-field-value! new-val)
164 ; Return a boolean indicating if X is an <ifield>.
166 (define (ifield? x) (class-instance? <ifield> x))
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.
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.
178 (obj:name value) ; send's message 'get-name to fetch object's `name'
179 "#")) ; # -> "it's a number"
182 ; Generate the name of the enum for instruction field ifld.
183 ; If PREFIX? is present and #f, the @ARCH@_ prefix is omitted.
185 (define (ifld-enum ifld . prefix?)
186 (string-upcase (string-append (if (or (null? prefix?) (car prefix?))
192 ; Return a boolean indicating if ifield F is an opcode field
193 ; (has a constant value).
195 (define (ifld-constant? f)
196 (number? (ifld-get-value f))
197 ; (and (number? (ifld-get-value f))
198 ; (if option:reserved-as-opcode?
200 ; (not (has-attr? f 'RESERVED))))
203 ; Return a boolean indicating if ifield F is an operand.
204 ; FIXME: Should check for operand? or some such.
206 (define (ifld-operand? f) (not (number? (ifld-get-value f))))
208 ; Return known value table for rtx-simplify of <ifield> list ifld-list.
210 (define (ifld-known-values ifld-list)
211 (let ((constant-iflds (find ifld-constant? (collect ifld-base-ifields ifld-list))))
214 (rtx-make-const 'INT (ifld-get-value f))))
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).
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
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)))
249 ((not (eq? lsb0? (bitrange-lsb0? container)))
250 (error "field-mask: different lsb0? values"))
251 ((not (= word-length container-word-length))
253 ; container occurs after?
254 ((<= (+ word-offset word-length) container-word-offset)
256 ; container occurs before?
257 ((>= word-offset (+ container-word-offset container-word-length))
260 (word-mask start length word-length lsb0? #f))))))
263 (define (ifld-mask ifld base-len container)
264 (send ifld 'field-mask base-len container)
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).
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)
283 (bitrange-lsb0? bitrange) #f
287 ; FIXME: confusion with ifld-get-value.
288 (define (ifld-value f base-len value)
289 (send f 'field-value base-len value)
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.
298 <ifield> 'needed-iflds
303 (define (ifld-needed-iflds f)
304 (send f 'needed-iflds)
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.
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)
324 (define (ifld-extract ifld value insn)
325 (send ifld 'field-extract value insn)
328 ; Return a boolean indicating if bit 0 is the least significant bit.
331 <ifield> 'field-lsb0?
333 (bitrange-lsb0? (-ifld-bitrange self)))
336 (define (ifld-lsb0? f) (send f 'field-lsb0?))
338 ; Return the minimum value of a field.
343 (case (mode:class (ifld-mode self))
344 ((INT) (- (integer-expt 2 (- (ifld-length self) 1))))
346 (else (error "unsupported mode class" (mode:class (ifld-mode self))))))
349 ; Return the maximum value of a field.
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))))))
360 ; Create a copy of field F with value VALUE.
361 ; VALUE is either ... ???
363 (define (ifld-new-value f value)
364 (let ((new-f (object-copy-top f)))
365 (ifld-set-value! new-f value)
369 ; Change the offset of the word containing an ifield to {word-offset}.
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)
379 (define (ifld-set-word-offset! f word-offset)
380 (send f 'set-word-offset! word-offset)
383 ; Return a copy of F with new {word-offset}.
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)
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.
397 (let ((br (-ifld-bitrange f)))
398 (bitrange-next-word br)))
401 (define (ifld-next-word f) (send f 'next-word))
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.
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))
412 ((= (bitrange-word-offset br1) (bitrange-word-offset br2))
414 (assert (eq? (bitrange-lsb0? br1) (bitrange-lsb0? br2)))
415 (assert (= (bitrange-word-length br1) (bitrange-word-length br1)))
417 (if (bitrange-lsb0? br1)
418 (> (bitrange-start br1) (bitrange-start br2))
419 (< (bitrange-start br1) (bitrange-start br2)))))
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).
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,
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.
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'.
452 ; FIXME: More error checking.
454 (define (-ifield-parse errtxt name comment attrs
455 word-offset word-length start flength follows
457 (logit 2 "Processing ifield " name " ...\n")
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))))
463 ; No longer ensure only one isa specified.
464 ;(if (!= (length isas) 1)
465 ; (parse-error errtxt "can only specify 1 isa" attrs))
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"))
471 (if (keep-isa-atlist? atlist #f)
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))))
479 (start (parse-number errtxt start '(0 . 127)))
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))
487 ; Calculate the <bitrange> object.
488 ; FIXME: word-offset/word-length computation needs work.
489 ; Move positional info to format?
494 word-offset start flength word-length lsb0?)
496 (let* ((default-insn-word-bitsize
497 (isa-default-insn-word-bitsize isa))
501 default-insn-word-bitsize)))
502 (start (remainder start default-insn-word-bitsize)))
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))
522 (parse-comment comment errtxt)
526 (-ifld-parse-encode errtxt encode)
527 (-ifld-parse-decode errtxt decode))))
529 (ifld-set-follows! result follows-obj))
534 (logit 2 "Ignoring " name ".\n")
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.
544 (define (-ifield-read errtxt . arg-list)
545 (let (; Current ifield elements:
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-".
562 ; Loop over each element in ARG-LIST, recording what's found.
563 (let loop ((arg-list arg-list))
566 (let ((arg (car arg-list))
567 (elm-name (caar arg-list)))
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)))))
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))
588 (if (and (pair? decode)
589 (eq? (car decode) #f))
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
599 ; Parse a `follows' spec.
601 (define (-ifld-parse-follows errtxt follows)
603 (let ((follows-obj (current-op-lookup follows)))
604 (if (not follows-obj)
605 (parse-error errtxt "unknown operand to follow" follows))
610 ; Do common parts of <ifield> encode/decode processing.
612 (define (-ifld-parse-encode-decode errtxt which value)
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))))
621 (string-append "bad ifield " which " spec")
623 (if (or (not (> (length (cadr value)) 2))
624 (not (mode:lookup (cadr (cadr value)))))
626 (string-append which " expression must have a mode")
631 ; Parse an <ifield> encode spec.
633 (define (-ifld-parse-encode errtxt encode)
634 (-ifld-parse-encode-decode errtxt "encode" encode)
637 ; Parse an <ifield> decode spec.
639 (define (-ifld-parse-decode errtxt decode)
640 (-ifld-parse-encode-decode errtxt "decode" decode)
643 ; Define an instruction field object, name/value pair list version.
645 (define define-ifield
647 (let ((f (apply -ifield-read (cons "define-ifield" arg-list))))
649 (current-ifld-add! f))
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.
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)))
662 (current-ifld-add! f))
666 (define (-ifield-add-commands!)
667 (reader-add-command! 'define-ifield
669 Define an instruction field, name/value pair list version.
671 nil 'arg-list define-ifield)
672 (reader-add-command! 'define-full-ifield
674 Define an instruction field, all arguments specified.
676 nil '(name comment attrs start length mode encode decode)
678 (reader-add-command! 'define-multi-ifield
680 Define an instruction multi-field, name/value pair list version.
682 nil 'arg-list define-multi-ifield)
683 (reader-add-command! 'define-full-multi-ifield
685 Define an instruction multi-field, all arguments specified.
687 nil '(name comment attrs mode subflds insert extract)
688 define-full-multi-ifield)
693 ; Instruction fields consisting of multiple parts.
695 (define <multi-ifield>
696 (class-make '<multi-ifield>
699 ; List of <ifield> objects.
701 ; rtl to set SUBFIELDS from self
703 ; rtl to set self from SUBFIELDS
711 (define-getters <multi-ifield> multi-ifld
712 (subfields insert extract)
715 ; Return a boolean indicating if X is an <ifield>.
717 (define (multi-ifield? x) (class-instance? <multi-ifield> x))
719 (define (non-multi-ifields ifld-list)
720 (find (lambda (ifld) (not (multi-ifield? ifld))) ifld-list)
723 (define (non-derived-ifields ifld-list)
724 (find (lambda (ifld) (not (derived-ifield? ifld))) ifld-list)
728 ; Return the starting bit number of the first field.
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))))
736 ; Return the total length.
738 (method-make-virtual!
739 <multi-ifield> 'field-length
741 (apply + (map ifld-length (elm-get self 'subfields))))
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.
748 <multi-ifield> 'next-word
750 (apply max (map (lambda (f)
751 (bitrange-next-word (-ifld-bitrange f)))
752 (multi-ifld-subfields self))))
755 ; Return mask of field in bitrange CONTAINER.
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))))
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.
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))))
773 ; Return a list of ifields required to compute the field's value.
776 <multi-ifield> 'needed-iflds
778 (cons self (elm-get self 'subfields)))
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.
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))
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))
801 (plus-scan (cdr lengths)
802 (+ current (car lengths))))))))
805 (plus-scan (map ifld-length subflds) 0))))))
808 ; Return a boolean indicating if bit 0 is the least significant bit.
811 <multi-ifield> 'field-lsb0?
813 (ifld-lsb0? (car (elm-get self 'subfields))))
816 ; Multi-ifield parsing.
818 ; Subroutine of -multi-ifield-parse to build the default insert expression.
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)
827 subfields lengths shifts)))
828 ; Now set each ifield with their respective values.
829 (apply rtx-make (cons 'sequence
831 (map (lambda (f expr)
832 (rtx-make-set f expr))
833 subfields exprs))))))
836 ; Subroutine of -multi-ifield-parse to build the default extract expression.
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)
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))))
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).
857 (define (-multi-ifield-parse errtxt name comment attrs mode subfields insert extract encode decode)
858 (logit 2 "Processing multi-ifield element " name " ...\n")
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))))
864 ; No longer ensure only one isa specified.
865 ; (if (!= (length isas) 1)
866 ; (parse-error errtxt "can only specify 1 isa" attrs))
868 (if (keep-isa-atlist? atlist #f)
870 (let ((result (new <multi-ifield>))
871 (subfields (map (lambda (subfld)
872 (let ((f (current-ifld-lookup subfld)))
874 (parse-error errtxt "unknown ifield" subfld))
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))
887 (elm-xset! result 'insert insert)
888 (elm-xset! result 'insert
889 (-multi-ifield-make-default-insert name subfields)))
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)
896 ; else don't keep isa
900 ; Read an instruction multi-ifield.
902 (define (-multi-ifield-read errtxt . arg-list)
903 (let (; Current multi-ifield elements:
914 ; Loop over each element in ARG-LIST, recording what's found.
915 (let loop ((arg-list arg-list))
918 (let ((arg (car arg-list))
919 (elm-name (caar arg-list)))
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)
937 ; Define an instruction multi-field object, name/value pair list version.
939 (define define-multi-ifield
941 (let ((f (apply -multi-ifield-read (cons "define-multi-ifield" arg-list))))
943 (current-ifld-add! f))
947 ; Define an instruction multi-field object, all arguments specified.
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)
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.
962 ; Derived ifields are (currently) always machine generated from other
963 ; elements of the description file so there is no reader support.
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.
969 (define <derived-ifield>
970 (class-make '<derived-ifield>
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.
979 ; List of ifields that make up this ifield.
987 <derived-ifield> 'needed-iflds
989 (find (lambda (ifld) (not (ifld-constant? ifld)))
990 (elm-get self 'subfields)))
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)
1009 (define-getters <derived-ifield> derived-ifield (owner subfields))
1011 (define-setters <derived-ifield> derived-ifield (owner subfields))
1013 (define (derived-ifield? x) (class-instance? <derived-ifield> x))
1015 ; Return a boolean indicating if F is a derived ifield with a derived operand
1017 ; ??? The former might imply the latter so some simplification may be possible.
1019 (define (ifld-derived-operand? f)
1020 (and (derived-ifield? f)
1021 (derived-operand? (ifld-get-value f)))
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.
1028 <derived-ifield> 'next-word
1030 (apply max (map (lambda (f)
1031 (bitrange-next-word (-ifld-bitrange f)))
1032 (derived-ifield-subfields self))))
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)))
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.
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
1058 (define sort-ifield-list
1059 (if (and (defined? 'cgh-qsort) (defined? 'cgh-qsort-int-cmp))
1060 (lambda (fld-list up?)
1064 (cgh-qsort-int-cmp (ifld-start a #f)
1067 (- (cgh-qsort-int-cmp (ifld-start a #f)
1068 (ifld-start b #f)))))))
1069 (lambda (fld-list up?)
1072 (lambda (a b) (< (ifld-start a #f)
1074 (lambda (a b) (> (ifld-start a #f)
1075 (ifld-start b #f)))))))
1078 ; Return a boolean indicating if field F extends beyond the base insn.
1080 (define (ifld-beyond-base? f base-bitsize total-bitsize)
1082 ;(< base-bitsize (+ (ifld-start f total-bitsize) (ifld-length f)))
1083 (> (ifld-word-offset f) 0)
1086 ; Return the mode of the decoded value of <ifield> F.
1087 ; ??? This is made easy because we require the decode expression to have
1090 (define (ifld-decode-mode f)
1091 (if (not (elm-bound? f 'decode))
1093 (let ((d (ifld-decode f)))
1095 (mode:lookup (cadr (cadr d)))
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))
1106 (else (error "unsupported mode class" (mode:class (ifld-mode f)))))
1109 ; Builtin fields, attributes, init/fini support.
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).
1115 (define (ifld-nil? f)
1116 (eq? (obj:name f) 'f-nil)
1119 ; The f-anyof field is a placeholder when building "anyof" operands.
1122 (define (ifld-anyof? f)
1123 (eq? (obj:name f) 'f-anyof)
1126 ; Return a boolean indicating if F is an anyof ifield with an anyof operand
1128 ; ??? The former implies the latter so some simplification is possible.
1130 (define (ifld-anyof-operand? f)
1131 (and (ifld-anyof? f)
1132 (anyof-operand? (ifld-get-value f)))
1135 ; Called before loading the .cpu file to initialize.
1137 (define (ifield-init!)
1138 (-ifield-add-commands!)
1143 ; Called before loading the .cpu file to create any builtins.
1145 (define (ifield-builtin!)
1146 ; Standard ifield attributes.
1147 ; ??? Some of these can be combined into one, booleans are easier to
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.
1159 (define-attr '(for ifield operand) '(type boolean) '(name SIGNED)
1160 '(comment "value is unsigned"))
1161 ; Also (defined elsewhere): VIRTUAL
1163 (set! f-nil (make <ifield> 'f-nil "empty ifield"
1166 (make <bitrange> 0 0 0 0 #f)
1167 #f #f)) ; encode/decode
1168 (current-ifld-add! f-nil)
1170 (set! f-anyof (make <ifield> 'f-anyof "placeholder for anyof operands"
1173 (make <bitrange> 0 0 0 0 #f)
1174 #f #f)) ; encode/decode
1175 (current-ifld-add! f-anyof)
1180 ; Called after the .cpu file has been read in.
1182 (define (ifield-finish!)