2 ; Copyright (C) 2000, 2002, 2009 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.
29 ; This is a <mode> object.
32 ; A <bitrange> object.
33 ; This contains the field's offset, start, length, word-length,
34 ; and orientation (msb==0, lsb==0). The orientation is
35 ; recorded to keep the <bitrange> object self-contained.
36 ; Endianness is not recorded.
39 ; Argument to :follows, as an object.
43 ; ENCODE/DECODE operate on the raw value, absent of any context
44 ; save `pc' and mode of field.
45 ; If #f, no special processing is required.
46 ; ??? It's not clear where the best place to process fields is.
47 ; An earlier version had insert/extract fields in operands to
48 ; handle more complicated cases. Following the goal of
49 ; incremental complication, the special handling for m32r's
50 ; f-disp8 field is handled entirely here, rather than partially
51 ; here and partially in the operand.
54 ; Value of field, if there is one, or #f.
55 ; Possible types are: integer, <operand>, ???
61 ; {ordinal} is missing on purpose, it's handled at a higher level.
62 ; {value},{follows} are missing on purpose.
63 ; {value} is handled specially.
64 ; {follows} is rarely used.
65 (method-make-make! <ifield>
66 '(location name comment attrs mode bitrange encode decode))
69 ; ??? `value' is treated specially, needed anymore?
71 (define-getters <ifield> ifld (mode encode decode follows))
73 (define-setters <ifield> ifld (follows))
76 (define /ifld-bitrange (elm-make-getter <ifield> 'bitrange))
78 (define (ifld-word-offset f) (bitrange-word-offset (/ifld-bitrange f)))
79 (define (ifld-word-length f) (bitrange-word-length (/ifld-bitrange f)))
81 ; Return the mode of the decoded value of <ifield> F.
82 ; ??? This is made easy because we require the decode expression to have
85 (define (ifld-decode-mode f)
86 (assert (elm-bound? f 'decode))
87 (let ((d (ifld-decode f)))
89 ;; FIXME: Does this work with canonicalized rtl?
90 (mode:lookup (cadr (cadr d)))
94 ; Return start of ifield.
99 (bitrange-start (/ifld-bitrange self)))
102 (define (ifld-start ifld)
103 (send ifld 'field-start)
107 <ifield> 'field-length
109 (bitrange-length (elm-get self 'bitrange)))
112 (define (ifld-length f) (send f 'field-length))
114 ; FIXME: It might make things more "readable" if enum values were preserved in
115 ; their symbolic form and the get-field-value method did the lookup.
118 <ifield> 'get-field-value
120 (elm-get self 'value))
122 (define (ifld-get-value self)
123 (send self 'get-field-value)
126 <ifield> 'set-field-value!
127 (lambda (self new-val)
128 (elm-set! self 'value new-val))
130 (define (ifld-set-value! self new-val)
131 (send self 'set-field-value! new-val)
134 ; Return a boolean indicating if X is an <ifield>.
136 (define (ifield? x) (class-instance? <ifield> x))
138 ; Return ilk of field as a string.
139 ; ("ilk" sounds klunky but "type" is too ambiguous. Here "ilk" means
140 ; the kind of the hardware element, enum, etc.)
141 ; The result is a character string naming the field type.
143 (define (ifld-ilk fld)
144 (let ((value (elm-xget fld 'value)))
145 ; ??? One could require that the `value' field always be an object.
146 ; I can't get too worked up over it yet.
148 (symbol->string (obj:name value)) ; send 'get-name to fetch the name
149 "#")) ; # -> "it's a number"
152 ; Generate the name of the enum for instruction field ifld.
153 ; If PREFIX? is present and #f, the @ARCH@_ prefix is omitted.
155 (define (ifld-enum ifld . prefix?)
156 (string-upcase (string-append (if (or (null? prefix?) (car prefix?))
162 ; Return a boolean indicating if ifield F is an opcode field
163 ; (has a constant value).
165 (define (ifld-constant? f)
166 (number? (ifld-get-value f))
167 ; (and (number? (ifld-get-value f))
168 ; (if option:reserved-as-opcode?
170 ; (not (has-attr? f 'RESERVED))))
173 ; Return a boolean indicating if ifield F is signed.
175 (define (ifld-signed? f)
176 (eq? (mode:class (ifld-mode f)) 'INT)
179 ; Return a boolean indicating if ifield F is an operand.
180 ; FIXME: Should check for operand? or some such.
182 (define (ifld-operand? f) (not (number? (ifld-get-value f))))
184 ; Return known value table for rtx-simplify of <ifield> list ifld-list.
186 (define (ifld-known-values ifld-list)
187 (let ((constant-iflds (find ifld-constant? (ifields-base-ifields ifld-list))))
190 (rtx-make-const 'INT (ifld-get-value f))))
194 ; Return mask to use for a field in <bitrange> CONTAINER.
195 ; If the bitrange is outside the range of the field, return 0.
196 ; If CONTAINER is #f, use the recorded bitrange.
197 ; BASE-LEN, if non-#f, overrides the base insn length of the insn.
198 ; BASE-LEN is present for architectures like the m32r where there are insns
199 ; smaller than the base insn size (LIW).
201 ; Simplifying restrictions [to be relaxed as necessary]:
202 ; - the field must either be totally contained within CONTAINER or totally
203 ; outside it, partial overlaps aren't handled
204 ; - CONTAINER must be an integral number of bytes, beginning on a
205 ; byte boundary [simplifies things]
206 ; - both SELF's bitrange and CONTAINER must have the same word length
207 ; - LSB0? of SELF's bitrange and CONTAINER must be the same
211 (lambda (self base-len container)
212 (let* ((container (or container (/ifld-bitrange self)))
213 (bitrange (/ifld-bitrange self))
214 (recorded-word-length (bitrange-word-length bitrange))
215 (word-offset (bitrange-word-offset bitrange)))
216 (let ((lsb0? (bitrange-lsb0? bitrange))
217 (start (bitrange-start bitrange))
218 (length (bitrange-length bitrange))
219 (word-length (or (and (= word-offset 0) base-len)
220 recorded-word-length))
221 (container-word-offset (bitrange-word-offset container))
222 (container-word-length (bitrange-word-length container)))
225 ((not (eq? lsb0? (bitrange-lsb0? container)))
226 (error "field-mask: different lsb0? values"))
227 ((not (= word-length container-word-length))
229 ; container occurs after?
230 ((<= (+ word-offset word-length) container-word-offset)
232 ; container occurs before?
233 ((>= word-offset (+ container-word-offset container-word-length))
236 (word-mask start length word-length lsb0? #f))))))
239 (define (ifld-mask ifld base-len container)
240 (send ifld 'field-mask base-len container)
243 ; Return VALUE inserted into the field's position.
244 ; BASE-LEN, if non-#f, overrides the base insn length of the insn.
245 ; BASE-LEN is present for architectures like the m32r where there are insns
246 ; smaller than the base insn size (LIW).
249 <ifield> 'field-value
250 (lambda (self base-len value)
251 (let* ((bitrange (/ifld-bitrange self))
252 (recorded-word-length (bitrange-word-length bitrange))
253 (word-offset (bitrange-word-offset bitrange))
254 (word-length (or (and (= word-offset 0) base-len)
255 recorded-word-length)))
256 (word-value (ifld-start self)
257 (bitrange-length bitrange)
259 (bitrange-lsb0? bitrange) #f
263 ; FIXME: confusion with ifld-get-value.
264 (define (ifld-value f base-len value)
265 (send f 'field-value base-len value)
268 ; Return a list of ifields required to compute <ifield> F's value.
269 ; Normally this is just F itself. For multi-ifields it will be more.
270 ; ??? It can also be more if F's value is derived from other fields but
271 ; that isn't supported yet.
274 <ifield> 'needed-iflds
279 (define (ifld-needed-iflds f)
280 (send f 'needed-iflds)
283 ; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
284 ; VALUE is the entire insn's value if it fits in a word, or is a list
285 ; of values, one per word (not implemented, sigh).
286 ; ??? The instruction's format should specify where the word boundaries are.
289 <ifield> 'field-extract
290 (lambda (self insn value)
291 (let ((base-len (insn-base-mask-length insn)))
292 (word-extract (ifld-start self)
300 (define (ifld-extract ifld value insn)
301 (send ifld 'field-extract value insn)
304 ; Return a boolean indicating if bit 0 is the least significant bit.
307 <ifield> 'field-lsb0?
309 (bitrange-lsb0? (/ifld-bitrange self)))
312 (define (ifld-lsb0? f) (send f 'field-lsb0?))
314 ; Return the minimum value of a field.
319 (case (mode:class (ifld-mode self))
320 ((INT) (- (integer-expt 2 (- (ifld-length self) 1))))
322 (else (error "unsupported mode class" (mode:class (ifld-mode self))))))
325 ; Return the maximum value of a field.
330 (case (mode:class (ifld-mode self))
331 ((INT) (- (integer-expt 2 (- (ifld-length self) 1)) 1))
332 ((UINT) (- (integer-expt 2 (ifld-length self)) 1))
333 (else (error "unsupported mode class" (mode:class (ifld-mode self))))))
336 ; Create a copy of field F with value VALUE.
337 ; VALUE is either ... ???
339 (define (ifld-new-value f value)
340 (let ((new-f (object-copy-top f)))
341 (ifld-set-value! new-f value)
345 ; Change the offset of the word containing an ifield to {word-offset}.
348 <ifield> 'set-word-offset!
349 (lambda (self word-offset)
350 (let ((bitrange (object-copy-top (/ifld-bitrange self))))
351 (bitrange-set-word-offset! bitrange word-offset)
352 (elm-set! self 'bitrange bitrange)
355 (define (ifld-set-word-offset! f word-offset)
356 (send f 'set-word-offset! word-offset)
359 ; Return a copy of F with new {word-offset}.
361 (define (ifld-new-word-offset f word-offset)
362 (let ((new-f (object-copy-top f)))
363 (ifld-set-word-offset! new-f word-offset)
367 ; Return the bit offset of the word after the word <ifield> F is in.
368 ; What a `word' here is defined by F in its bitrange.
373 (let ((br (/ifld-bitrange f)))
374 (bitrange-next-word br)))
377 (define (ifld-next-word f) (send f 'next-word))
379 ; Return a boolean indicating if <ifield> F1 precedes <ifield> F2.
380 ; FIXME: Move into a method as different subclasses will need
381 ; different handling.
383 (define (ifld-precedes? f1 f2)
384 (let ((br1 (/ifld-bitrange f1))
385 (br2 (/ifld-bitrange f2)))
386 (cond ((< (bitrange-word-offset br1) (bitrange-word-offset br2))
388 ((= (bitrange-word-offset br1) (bitrange-word-offset br2))
390 (assert (eq? (bitrange-lsb0? br1) (bitrange-lsb0? br2)))
391 (assert (= (bitrange-word-length br1) (bitrange-word-length br1)))
393 (if (bitrange-lsb0? br1)
394 (> (bitrange-start br1) (bitrange-start br2))
395 (< (bitrange-start br1) (bitrange-start br2)))))
400 ;; Pretty print an ifield, typically for error messages.
403 <ifield> 'pretty-print
405 (string-append "(" (obj:str-name self)
406 " " (number->string (ifld-start self))
407 " " (number->string (ifld-length self))
411 (define (ifld-pretty-print f)
412 (send f 'pretty-print)
415 ; Parse an ifield definition.
416 ; This is the main routine for building an ifield object from a
417 ; description in the .cpu file.
418 ; All arguments are in raw (non-evaluated) form.
419 ; The result is the parsed object or #f if object isn't for selected mach(s).
421 ; Two forms of specification are supported, loosely defined as the RISC way
422 ; and the CISC way. The reason for the distinction is to simplify ifield
423 ; specification of RISC-like cpus.
424 ; Note that VLIW's are another way. These are handled like the RISC way, with
425 ; the possible addition of instruction framing (which is, surprise surprise,
429 ; WORD-OFFSET and WORD-LENGTH are #f. Insns are assumed to be N copies of
430 ; (isa-default-insn-word-bitsize). WORD-OFFSET is computed from START.
431 ; START is the offset in bits from the start of the insn.
432 ; FLENGTH is the length of the field in bits.
435 ; WORD-OFFSET is the offset in bits from the start to the first byte of the
436 ; word containing the ifield.
437 ; WORD-LENGTH is the length in bits of the word containing the ifield.
438 ; START is the starting bit number in the word. Bit numbering is taken from
439 ; (current-arch-insn-lsb0?).
440 ; FLENGTH is the length in bits of the ifield. It is named that way to avoid
441 ; collision with the proc named `length'.
443 ; FIXME: More error checking.
445 (define (/ifield-parse context name comment attrs
446 word-offset word-length start flength follows
448 (logit 2 "Processing ifield " name " ...\n")
450 ;; Pick out name first to augment the error context.
451 (let* ((name (parse-name context name))
452 (context (context-append-name context name))
453 (atlist (atlist-parse context attrs "cgen_ifld"))
454 (isas (atlist-attr-value atlist 'ISA #f)))
456 ; No longer ensure only one isa specified.
457 ;(if (!= (length isas) 1)
458 ; (parse-error context "can only specify 1 isa" attrs))
460 (if (not (eq? (->bool word-offset)
461 (->bool word-length)))
462 (parse-error context "either both or neither of word-offset,word-length can be specified"))
464 (if (keep-isa-atlist? atlist #f)
466 (let ((isa (current-isa-lookup (car isas)))
467 (word-offset (and word-offset
468 (parse-number context word-offset '(0 . 256))))
469 (word-length (and word-length
470 (parse-number context word-length '(0 . 128))))
472 (start (parse-number context start '(0 . 127)))
474 (flength (parse-number context flength '(0 . 127)))
475 (lsb0? (current-arch-insn-lsb0?))
476 (mode-obj (parse-mode-name context mode))
477 (follows-obj (/ifld-parse-follows context follows isas))
480 ; Calculate the <bitrange> object.
481 ; ??? Move positional info to format?
485 ; CISC-like. Easy. Everything must be specified.
487 word-offset start flength word-length lsb0?)
489 ; RISC-like. Hard. Have to make best choice of start,
490 ; flength. This doesn't have to be perfect, just easily
491 ; explainable. Cases this doesn't handle can explicitly
492 ; specify word-offset,word-length.
493 ; One can certainly argue the choice of the term
494 ; "RISC-like" is inaccurate. Perhaps.
495 (let* ((diwb (isa-default-insn-word-bitsize isa))
496 (word-offset (/get-ifld-word-offset start flength diwb lsb0?))
497 (word-length (/get-ifld-word-length start flength diwb lsb0?))
498 (start (- start word-offset))
510 (context-location context)
512 (parse-comment context comment)
516 (/ifld-parse-encode context encode)
517 (/ifld-parse-decode context decode))))
519 (ifld-set-follows! result follows-obj))
524 (logit 2 "Ignoring " name ".\n")
528 ; Subroutine of /ifield-parse to simplify it.
529 ; Given START,FLENGTH, return the "best" choice for the offset to the word
530 ; containing the ifield.
531 ; This is easy to visualize, hard to put into words.
532 ; Imagine several words of size DIWB laid out from the start of the insn.
533 ; On top of that lay the ifield.
534 ; Now pick the minimal set of words that are required to contain the ifield.
535 ; That's what we want.
536 ; No claim is made that this is always the correct choice for any
537 ; particular architecture. For those where this isn't correct, the ifield
538 ; must be fully specified (i.e. word-offset,word-length explicitly specified).
540 (define (/get-ifld-word-offset start flength diwb lsb0?)
542 ; Convert to non-lsb0 case, then it's easy.
543 ; NOTE: The conversion is seemingly wrong because `start' is misnamed.
545 (set! start (+ (- start flength) 1)))
546 (- start (remainder start diwb))
549 ; Subroutine of /ifield-parse to simplify it.
550 ; Given START,FLENGTH, return the "best" choice for the length of the word
551 ; containing the ifield.
552 ; DIWB = default insn word bitsize
553 ; See -get-ifld-word-offset for more info.
555 (define (/get-ifld-word-length start flength diwb lsb0?)
557 ; Convert to non-lsb0 case, then it's easy.
558 ; NOTE: The conversion is seemingly wrong because `start' is misnamed.
560 (set! start (+ (- start flength) 1)))
561 (* (quotient (+ (remainder start diwb) flength (- diwb 1))
566 ; Read an instruction field description.
567 ; This is the main routine for analyzing instruction fields in the .cpu file.
568 ; CONTEXT is a <context> object for error messages.
569 ; ARG-LIST is an associative list of field name and field value.
570 ; /ifield-parse is invoked to create the <ifield> object.
572 (define (/ifield-read context . arg-list)
580 ; FIXME: Hobbit computes the wrong symbol for `length'
581 ; in the `case' expression below because there is a local var
582 ; of the same name ("__1" gets appended to the symbol name).
583 ; As a workaround we name it "length-".
591 ; Loop over each element in ARG-LIST, recording what's found.
592 (let loop ((arg-list arg-list))
595 (let ((arg (car arg-list))
596 (elm-name (caar arg-list)))
598 ((name) (set! name (cadr arg)))
599 ((comment) (set! comment (cadr arg)))
600 ((attrs) (set! attrs (cdr arg)))
601 ((mode) (set! mode (cadr arg)))
602 ((word-offset) (set! word-offset (cadr arg)))
603 ((word-length) (set! word-length (cadr arg)))
604 ((start) (set! start (cadr arg)))
605 ((length) (set! length- (cadr arg)))
606 ((follows) (set! follows (cadr arg)))
607 ((encode) (set! encode (cdr arg)))
608 ((decode) (set! decode (cdr arg)))
609 (else (parse-error context "invalid ifield arg" arg)))
610 (loop (cdr arg-list)))))
612 ; See if encode/decode were specified as "unspecified".
613 ; This happens with shorthand macros.
614 (if (and (pair? encode)
615 (eq? (car encode) #f))
617 (if (and (pair? decode)
618 (eq? (car decode) #f))
621 ; Now that we've identified the elements, build the object.
622 (/ifield-parse context name comment attrs
623 word-offset word-length start length- follows
627 ; Parse a `follows' spec.
629 (define (/ifld-parse-follows context follows isas)
631 (let ((follows-obj (current-op-lookup follows isas)))
632 (if (not follows-obj)
633 (parse-error context "unknown operand to follow" follows))
638 ; Do common parts of <ifield> encode/decode processing.
640 (define (/ifld-parse-encode-decode context which value)
643 (if (or (not (list? value))
644 (not (= (length value) 2))
645 (not (list? (car value)))
646 (not (= (length (car value)) 2))
647 (not (list? (cadr value))))
649 (string-append "bad ifield " which " spec")
651 (if (or (not (> (length (cadr value)) 2))
652 (not (mode:lookup (cadr (cadr value)))))
654 (string-append which " expression must have a mode")
659 ; Parse an <ifield> encode spec.
661 (define (/ifld-parse-encode context encode)
662 (/ifld-parse-encode-decode context "encode" encode)
665 ; Parse an <ifield> decode spec.
667 (define (/ifld-parse-decode context decode)
668 (/ifld-parse-encode-decode context "decode" decode)
671 ; Define an instruction field object, name/value pair list version.
673 (define define-ifield
675 (let ((f (apply /ifield-read (cons (make-current-context "define-ifield")
678 (current-ifld-add! f))
682 ; Define an instruction field object, all arguments specified.
683 ; ??? Leave out word-offset,word-length,follows for now (RISC version).
684 ; FIXME: Eventually this should be fixed to take *all* arguments.
686 (define (define-full-ifield name comment attrs start length mode encode decode)
687 (let ((f (/ifield-parse (make-current-context "define-full-ifield")
689 #f #f start length #f mode encode decode)))
691 (current-ifld-add! f))
695 (define (/ifield-add-commands!)
696 (reader-add-command! 'define-ifield
698 Define an instruction field, name/value pair list version.
700 nil 'arg-list define-ifield)
701 (reader-add-command! 'define-full-ifield
703 Define an instruction field, all arguments specified.
705 nil '(name comment attrs start length mode encode decode)
707 (reader-add-command! 'define-multi-ifield
709 Define an instruction multi-field, name/value pair list version.
711 nil 'arg-list define-multi-ifield)
712 (reader-add-command! 'define-full-multi-ifield
714 Define an instruction multi-field, all arguments specified.
716 nil '(name comment attrs mode subflds insert extract)
717 define-full-multi-ifield)
722 ; Instruction fields consisting of multiple parts.
724 (define <multi-ifield>
725 (class-make '<multi-ifield>
728 ; List of <ifield> objects.
730 ; rtl to set SUBFIELDS from self
732 ; rtl to set self from SUBFIELDS
738 (method-make-make! <multi-ifield> '(name comment attrs
739 mode bitrange encode decode
740 subfields insert extract))
744 (define-getters <multi-ifield> multi-ifld
745 (subfields insert extract)
748 ; Return a boolean indicating if X is an <ifield>.
750 (define (multi-ifield? x) (class-instance? <multi-ifield> x))
752 (define (non-multi-ifields ifld-list)
753 (find (lambda (ifld) (not (multi-ifield? ifld))) ifld-list)
756 (define (non-derived-ifields ifld-list)
757 (find (lambda (ifld) (not (derived-ifield? ifld))) ifld-list)
760 ; Return the starting bit number of the first field.
763 <multi-ifield> 'field-start
765 (apply min (map (lambda (f) (ifld-start f)) (elm-get self 'subfields))))
768 ; Return the total length.
771 <multi-ifield> 'field-length
773 (apply + (map ifld-length (elm-get self 'subfields))))
776 ; Return the bit offset of the word after the last word SELF is in.
777 ; What a `word' here is defined by subfields in their bitranges.
780 <multi-ifield> 'next-word
782 (apply max (map (lambda (f)
783 (bitrange-next-word (/ifld-bitrange f)))
784 (multi-ifld-subfields self))))
787 ; Return mask of field in bitrange CONTAINER.
790 <multi-ifield> 'field-mask
791 (lambda (self base-len container)
792 (apply + (map (lambda (f) (ifld-mask f base-len container)) (elm-get self 'subfields))))
795 ; Return VALUE inserted into the field's position.
796 ; The value is spread out over the various subfields in sorted order.
797 ; We assume the subfields have been sorted by starting bit position.
800 <multi-ifield> 'field-value
801 (lambda (self base-len value)
802 (apply + (map (lambda (f) (ifld-value f base-len value)) (elm-get self 'subfields))))
805 ; Return a list of ifields required to compute the field's value.
808 <multi-ifield> 'needed-iflds
810 (cons self (elm-get self 'subfields)))
813 ; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
814 ; VALUE is the entire insn's value if it fits in a word, or is a list
815 ; of values, one per word (not implemented, sigh).
816 ; ??? The instruction's format should specify where the word boundaries are.
819 <multi-ifield> 'field-extract
820 (lambda (self insn value)
821 (let* ((subflds (sort-ifield-list (elm-get self 'subfields)
822 (not (ifld-lsb0? self))))
823 (subvals (map (lambda (subfld)
824 (ifld-extract subfld insn value))
827 ; We have each subfield's value, now concatenate them.
828 (letrec ((plus-scan (lambda (lengths current)
829 ; do the -1 drop here as it's easier
830 (if (null? (cdr lengths))
833 (plus-scan (cdr lengths)
834 (+ current (car lengths))))))))
837 (plus-scan (map ifld-length subflds) 0))))))
840 ; Return a boolean indicating if bit 0 is the least significant bit.
843 <multi-ifield> 'field-lsb0?
845 (ifld-lsb0? (car (elm-get self 'subfields))))
848 ;; Pretty print a multi-ifield, typically for error messages.
851 <multi-ifield> 'pretty-print
853 (string-append "(" (obj:str-name self)
854 (string-map (lambda (f)
855 (string-append " " (ifld-pretty-print f)))
856 (elm-get self 'subfields))
860 ; Multi-ifield parsing.
862 ; Subroutine of /multi-ifield-parse to build the default insert expression.
864 (define (/multi-ifield-make-default-insert container-name subfields)
865 (let* ((lengths (map ifld-length subfields))
866 (shifts (list-tail-drop 1 (plus-scan (cons 0 lengths)))))
867 ; Build RTL expression to shift and mask each ifield into right spot.
868 (let ((exprs (map (lambda (f length shift)
869 (rtx-make 'and (rtx-make 'srl container-name shift)
871 subfields lengths shifts)))
872 ; Now set each ifield with their respective values.
873 (apply rtx-make (cons 'sequence
875 (map (lambda (f expr)
876 (rtx-make-set f expr))
877 subfields exprs))))))
880 ; Subroutine of /multi-ifield-parse to build the default extract expression.
882 (define (/multi-ifield-make-default-extract container-name subfields)
883 (let* ((lengths (map ifld-length subfields))
884 (shifts (list-tail-drop 1 (plus-scan (cons 0 lengths)))))
885 ; Build RTL expression to shift and mask each ifield into right spot.
886 (let ((exprs (map (lambda (f length shift)
887 (rtx-make 'sll (rtx-make 'and (obj:name f)
890 subfields lengths shifts)))
891 ; Now set {container-name} with all the values or'd together.
892 (rtx-make-set container-name
893 (rtx-combine 'or exprs))))
896 ; Parse a multi-ifield spec.
897 ; This is the main routine for building the object from the .cpu file.
898 ; All arguments are in raw (non-evaluated) form.
899 ; The result is the parsed object or #f if object isn't for selected mach(s).
901 (define (/multi-ifield-parse context name comment attrs mode
902 subfields insert extract encode decode)
903 (logit 2 "Processing multi-ifield element " name " ...\n")
905 (if (null? subfields)
906 (parse-error context "empty subfield list" subfields))
908 ;; Pick out name first to augment the error context.
909 (let* ((name (parse-name context name))
910 (context (context-append-name context name))
911 (atlist (atlist-parse context attrs "cgen_ifld"))
912 (isas (atlist-attr-value atlist 'ISA #f)))
914 ; No longer ensure only one isa specified.
915 ; (if (!= (length isas) 1)
916 ; (parse-error context "can only specify 1 isa" attrs))
918 (if (keep-isa-atlist? atlist #f)
921 (let ((result (new <multi-ifield>))
922 (subfields (map (lambda (subfld)
923 (let ((f (current-ifld-lookup subfld)))
925 (parse-error context "unknown ifield"
930 (elm-xset! result 'name name)
931 (elm-xset! result 'comment (parse-comment context comment))
932 (elm-xset! result 'attrs
933 ;; multi-ifields are always VIRTUAL
934 (atlist-parse context (cons 'VIRTUAL attrs)
936 (elm-xset! result 'mode (parse-mode-name context mode))
937 (elm-xset! result 'encode (/ifld-parse-encode context encode))
938 (elm-xset! result 'decode (/ifld-parse-encode context decode))
939 (elm-xset! result 'bitrange "multi-ifields don't have bitranges") ;; FIXME
941 (elm-xset! result 'insert insert)
942 (elm-xset! result 'insert
943 (/multi-ifield-make-default-insert name subfields)))
945 (elm-xset! result 'extract extract)
946 (elm-xset! result 'extract
947 (/multi-ifield-make-default-extract name subfields)))
948 (elm-xset! result 'subfields subfields)
951 ; else don't keep isa
955 ; Read an instruction multi-ifield.
956 ; This is the main routine for analyzing multi-ifields in the .cpu file.
957 ; CONTEXT is a <context> object for error messages.
958 ; ARG-LIST is an associative list of field name and field value.
959 ; /multi-ifield-parse is invoked to create the `multi-ifield' object.
961 (define (/multi-ifield-read context . arg-list)
974 ; Loop over each element in ARG-LIST, recording what's found.
975 (let loop ((arg-list arg-list))
978 (let ((arg (car arg-list))
979 (elm-name (caar arg-list)))
981 ((name) (set! name (cadr arg)))
982 ((comment) (set! comment (cadr arg)))
983 ((attrs) (set! attrs (cdr arg)))
984 ((mode) (set! mode (cadr arg)))
985 ((subfields) (set! subflds (cdr arg)))
986 ((insert) (set! insert (cadr arg)))
987 ((extract) (set! extract (cadr arg)))
988 ((encode) (set! encode (cdr arg)))
989 ((decode) (set! decode (cdr arg)))
990 (else (parse-error context "invalid ifield arg" arg)))
991 (loop (cdr arg-list)))))
993 ; Now that we've identified the elements, build the object.
994 (/multi-ifield-parse context name comment attrs mode subflds
995 insert extract encode decode))
998 ; Define an instruction multi-field object, name/value pair list version.
1000 (define define-multi-ifield
1002 (let ((f (apply /multi-ifield-read (cons (make-current-context "define-multi-ifield")
1005 (current-ifld-add! f))
1009 ; Define an instruction multi-field object, all arguments specified.
1010 ; FIXME: encode/decode arguments are missing.
1012 (define (define-full-multi-ifield name comment attrs mode subflds insert extract)
1013 (let ((f (/multi-ifield-parse (make-current-context "define-full-multi-ifield")
1015 mode subflds insert extract #f #f)))
1016 (current-ifld-add! f)
1020 ; Derived ifields (ifields based on one or more other ifields).
1021 ; These support the complicated requirements of CISC instructions
1022 ; where one "ifield" is actually a placeholder for an addressing mode
1023 ; which can consist of several ifields.
1024 ; These are also intended to support other complex ifield usage.
1026 ; Derived ifields are (currently) always machine generated from other
1027 ; elements of the description file so there is no reader support.
1029 ; ??? experimental and wip!
1030 ; ??? These are kind of like multi-ifields but I don't want to disturb them
1031 ; while this is still experimental.
1033 (define <derived-ifield>
1034 (class-make '<derived-ifield>
1037 ; Operand that uses this ifield.
1038 ; Unlike other ifields, derived ifields have a one-to-one
1039 ; correspondence with the operand that uses them.
1040 ; ??? Not true in -anyof-merge-subchoices.
1043 ; List of ifields that make up this ifield.
1050 <derived-ifield> 'needed-iflds
1052 (find (lambda (ifld) (not (ifld-constant? ifld)))
1053 (elm-get self 'subfields)))
1057 <derived-ifield> 'make!
1058 (lambda (self name comment attrs owner subfields)
1059 (elm-set! self 'name name)
1060 (elm-set! self 'comment comment)
1061 (elm-set! self 'attrs attrs)
1062 (elm-set! self 'mode UINT)
1063 (elm-set! self 'bitrange (make <bitrange> 0 0 0 0 #f))
1064 (elm-set! self 'encode #f)
1065 (elm-set! self 'decode #f)
1066 (elm-set! self 'owner owner)
1067 (elm-set! self 'subfields subfields)
1073 (define-getters <derived-ifield> derived-ifield (owner subfields))
1075 (define-setters <derived-ifield> derived-ifield (owner subfields))
1077 (define (derived-ifield? x) (class-instance? <derived-ifield> x))
1079 ; Return a boolean indicating if F is a derived ifield with a derived operand
1081 ; ??? The former might imply the latter so some simplification may be possible.
1083 (define (ifld-derived-operand? f)
1084 (and (derived-ifield? f)
1085 (derived-operand? (ifld-get-value f)))
1088 ; Return the bit offset of the word after the last word SELF is in.
1089 ; What a `word' here is defined by subfields in their bitranges.
1092 <derived-ifield> 'next-word
1094 (apply max (map (lambda (f)
1095 (bitrange-next-word (/ifld-bitrange f)))
1096 (derived-ifield-subfields self))))
1099 ; Return a list of all base (non-derived) ifields in IFLD.
1100 ; NOTE: multi-ifields are *not* reduced to their sub-ifields.
1102 (define (ifld-base-ifields ifld)
1103 (cond ((derived-ifield? ifld) (ifields-base-ifields (derived-ifield-subfields ifld)))
1104 ;;((multi-ifield? ifld) (ifields-base-ifields (multi-ifld-subfields ifld)))
1108 ; Collect all base (non-derived) ifields in IFLD-LIST.
1109 ; NOTE: multi-ifields are *not* reduced to their sub-ifields.
1111 (define (ifields-base-ifields ifld-list)
1112 (collect ifld-base-ifields ifld-list)
1115 ; Return a list of all simple ifields in IFLD.
1116 ; NOTE: multi-ifields *are* reduced to their sub-ifields.
1118 (define (ifld-simple-ifields ifld)
1119 (cond ((derived-ifield? ifld) (ifields-simple-ifields (derived-ifield-subfields ifld)))
1120 ((multi-ifield? ifld) (ifields-simple-ifields (multi-ifld-subfields ifld)))
1124 ; Collect all simple ifields in IFLD-LIST.
1125 ; NOTE: multi-ifields *are* reduced to their sub-ifields.
1127 (define (ifields-simple-ifields ifld-list)
1128 (collect ifld-simple-ifields ifld-list)
1133 ; Sort a list of fields (sorted by the starting bit number).
1134 ; This must be carefully defined to pass through Hobbit.
1135 ; (define foo (if x bar baz)) is ok.
1136 ; (if x (define foo bar) (define foo baz)) is not ok.
1138 ; ??? Usually there aren't that many fields and the range of values is fixed,
1139 ; so I think this needn't use a general purpose sort routine (should it become
1142 (define sort-ifield-list
1143 (if (and (defined? 'cgh-qsort) (defined? 'cgh-qsort-int-cmp))
1144 (lambda (fld-list up?)
1148 (cgh-qsort-int-cmp (ifld-start a)
1151 (- (cgh-qsort-int-cmp (ifld-start a)
1152 (ifld-start b)))))))
1153 (lambda (fld-list up?)
1156 (lambda (a b) (< (ifld-start a)
1158 (lambda (a b) (> (ifld-start a)
1159 (ifld-start b)))))))
1162 ; Return a boolean indicating if field F extends beyond the base insn.
1164 (define (ifld-beyond-base? f)
1165 (> (ifld-word-offset f) 0)
1168 ; Return <hardware> object to use to hold value of <ifield> F.
1169 ; i.e. one of h-uint, h-sint.
1170 ; NB: Should be defined in terms of `hardware-for-mode'.
1171 (define (ifld-hw-type f)
1172 (case (mode:class (ifld-mode f))
1175 (else (error "unsupported mode class" (mode:class (ifld-mode f)))))
1178 ; Builtin fields, attributes, init/fini support.
1180 ; The f-nil field is a placeholder when building operands out of hardware
1181 ; elements that aren't indexed by an instruction field (scalars).
1184 (define (ifld-nil? f)
1185 (eq? (obj:name f) 'f-nil)
1188 ; The f-anyof field is a placeholder when building "anyof" operands.
1191 (define (ifld-anyof? f)
1192 (eq? (obj:name f) 'f-anyof)
1195 ; Return a boolean indicating if F is an anyof ifield with an anyof operand
1197 ; ??? The former implies the latter so some simplification is possible.
1199 (define (ifld-anyof-operand? f)
1200 (and (ifld-anyof? f)
1201 (anyof-operand? (ifld-get-value f)))
1204 ; Called before loading the .cpu file to initialize.
1206 (define (ifield-init!)
1207 (/ifield-add-commands!)
1212 ; Called before loading the .cpu file to create any builtins.
1214 (define (ifield-builtin!)
1215 ; Standard ifield attributes.
1216 ; ??? Some of these can be combined into one, booleans are easier to
1218 (define-attr '(for ifield operand) '(type boolean) '(name PCREL-ADDR)
1219 '(comment "pc relative address"))
1220 (define-attr '(for ifield operand) '(type boolean) '(name ABS-ADDR)
1221 '(comment "absolute address"))
1222 (define-attr '(for ifield) '(type boolean) '(name RESERVED)
1223 '(comment "field is reserved"))
1224 (define-attr '(for ifield operand) '(type boolean) '(name SIGN-OPT)
1225 '(comment "value is signed or unsigned"))
1226 ; ??? This is an internal attribute for implementation purposes only.
1228 (define-attr '(for ifield operand) '(type boolean) '(name SIGNED)
1229 '(comment "value is unsigned"))
1230 ; Also (defined elsewhere): VIRTUAL
1232 (set! f-nil (make <ifield> (builtin-location)
1233 'f-nil "empty ifield"
1234 (atlist-cons (all-isas-attr) nil)
1236 (make <bitrange> 0 0 0 0 #f)
1237 #f #f)) ; encode/decode
1238 (current-ifld-add! f-nil)
1240 (set! f-anyof (make <ifield> (builtin-location)
1241 'f-anyof "placeholder for anyof operands"
1242 (atlist-cons (all-isas-attr) nil)
1244 (make <bitrange> 0 0 0 0 #f)
1245 #f #f)) ; encode/decode
1246 (current-ifld-add! f-anyof)
1251 ; Called after the .cpu file has been read in.
1253 (define (ifield-finish!)