2 ; Copyright (C) 2000, 2003, 2005, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
6 ; There are 5 kinds of attributes: boolean, integer, enum, bitset, and string.
7 ; Boolean attributes are really enum attributes with two possible values,
8 ; but they occur frequently enough that they are special cased.
9 ; String attributes are intentionally not documented in the manual as
10 ; being supported - they're still a bit of work-in-progress.
12 ; All objects that use attributes must have two methods:
13 ; - 'get-atlist - returns the object's attr-list
14 ; - 'set-atlist! - set the object's attr-list
16 ; In .cpu files, attribute lists are associative lists of (NAME VALUE).
17 ; Boolean attributes are specified as (NAME #t) or (NAME #f),
18 ; but for convenience ATTR and !ATTR are also supported.
19 ; integer/enum attrs are specified as (ATTR value).
20 ; string attrs are specified as (ATTR "value").
21 ; Bitset attrs are specified as (ATTR val1 val2 val3), each value must be
22 ; a valid Scheme symbol (stick with valid C symbols + "-" and you'll be fine).
23 ; For backwards compatibility (ATTR val1,val2,val3) and
24 ; (ATTR "val1,val2,val3") are also supported for bitset values.
25 ; val1,val2,val3 is not portable (e.g. mzscheme will reject it).
26 ; In all cases the value needn't be constant, and can be an expression,
27 ; though expressions are currently only supported for META-attributes
28 ; (attributes that don't appear in any generated code).
31 ; (FOO1 !FOO2 (BAR 3) (FOO3 X) (MACH sparc sparclite))
33 ; ??? Implementation of expressions is being postponed as long
34 ; as possible, avoiding adding complications for complication's sake, and
35 ; because I'm not completely sure how I want to do them.
36 ; The syntax for an expression value is (ATTR (rtx-func ...)).
38 ; ??? May wish to allow a bitset attribute like (ATTR val1 !val2), where `!'
39 ; means to turn off that particular bit (or bits if val2 refers to several).
41 ; ??? May wish to allow specifying enum attributes by only having to
42 ; specify the value (move names into "enum space" or some such).
44 ; An attr-list (or "atlist") is a collection of attributes.
45 ; Attributes are stored as an associative list.
46 ; There is possible confusion between "alist" (associative-list) and
47 ; "atlist" (attribute-list) but in practice I haven't had a problem.
48 ; ??? May wish to change this to a list of objects, as the alist doesn't carry
49 ; enough info. However the alist is simple and fast.
51 (define <attr-list> (class-make '<attr-list> nil '(prefix attrs) nil))
53 (define atlist-prefix (elm-make-getter <attr-list> 'prefix))
54 (define atlist-attrs (elm-make-getter <attr-list> 'attrs))
56 (define (atlist? x) (class-instance? <attr-list> x))
58 ; An empty attribute-list.
60 (define atlist-empty (make <attr-list> "" nil))
62 ; The attribute baseclass.
63 ; The attributes of <ident> are the set of attributes for this attribute
64 ; [meaning attributes themselves can have attributes].
65 ; [Ya, that's clumsily written. I left it that way for fun.]
66 ; An odd notion that is of some use. It's current raison d'etre is to
67 ; support sanitization of attributes [which is implemented with the
68 ; `sanitize' attribute].
71 (class-make '<attribute>
74 ; List of object types this attribute is for.
75 ; Possible element values are:
76 ; attr, enum, cpu, mach, model, ifield, hardware, operand,
78 ; A value of #f means the attribute is for everything.
86 (define atlist-for (elm-make-getter <attribute> 'for))
88 ; A class for each type of attribute.
90 ; `values' exists for boolean-attribute to simplify the code, it's ignored.
91 ; Ditto for `default'. The default for boolean-attribute is always #f.
93 (define <boolean-attribute>
94 (class-make '<boolean-attribute>
100 ; VALUES is ignored for string-attribute.
102 (define <string-attribute>
103 (class-make '<string-attribute>
109 ; For bitset attributes VALUES is a list of
110 ; (symbol bit-number-or-#f attr-list comment-or-#f),
112 ; If bit-number is #f (unspecified), cgen will choose.
113 ; Int's are used to record the bitset in the generated code so there's a limit
114 ; of 32 elements, though there's nothing inherent in the description language
115 ; that precludes removing the limit.
116 ; NOTE: While one might want to record each element as an object, there's
117 ; currently no need for the added complexity.
119 (define <bitset-attribute>
120 (class-make '<bitset-attribute>
126 ; For integer attributes VALUES is a list of (int),
127 ; one for each possible value,
128 ; or the empty list of all values are permissible.
129 ; Note that each element is itself a list. This is for consistency.
131 (define <integer-attribute>
132 (class-make '<integer-attribute>
138 ; For enum attributes VALUES is a list of
139 ; (symbol enum-value-or-#f attr-list comment-or-#f),
140 ; one for each possible.
141 ; If enum-value is #f (unspecified) cgen will apply the standard rule for
142 ; assigning enum values.
143 ; NOTE: While one might want to record each element as an object, there's
144 ; currently no need for the added complexity.
146 (define <enum-attribute>
147 (class-make '<enum-attribute>
153 ; Return a boolean indicating if X is a <boolean-attribute> object.
155 (define (bool-attr? x) (class-instance? <boolean-attribute> x))
157 ; Return a symbol indicating the kind of attribute ATTR is.
158 ; The result is one of boolean,integer,enum,bitset or string.
160 (define (attr-kind attr)
161 (case (object-class-name attr)
162 ((<boolean-attribute>) 'boolean)
163 ((<string-attribute>) 'string)
164 ((<integer-attribute>) 'integer)
165 ((<enum-attribute>) 'enum)
166 ((<bitset-attribute>) 'bitset)
167 (else (error "attr-kind: internal error, not an attribute class"
168 (object-class-name attr))))
173 (define (attr-default attr) (elm-xget attr 'default))
174 (define (attr-values attr) (elm-xget attr 'values))
176 ; Create an attribute.
177 ; Attributes are stored in attribute lists using the actual value
178 ; rather than an object containing the value, so we only have to cons
179 ; NAME and VALUE rather than building some object. This is for simplicity
180 ; and speed. We try to incrementally complicate things, only as necessary.
182 ; VALUE must be #f or #t.
184 (define (bool-attr-make name value) (cons name value))
186 ; VALUES must be a list of symbols.
187 ; E.g., (val1 val2) not val1,val2.
189 (define (bitset-attr-make name values) (cons name values))
191 ; VALUE must be a number (or maybe a symbol).
193 (define (int-attr-make name value) (cons name value))
195 ; VALUE must be a symbol.
197 (define (enum-attr-make name value) (cons name value))
199 ;; Return a procedure to parse an attribute.
200 ;; RIGHT-TYPE? is a procedure that verifies the value is the right type.
201 ;; MESSAGE is printed if there is an error.
202 ;; The result of the parsed attribute is (name . value).
204 (define (/parse-simple-attribute right-type? message)
205 (lambda (self context val)
206 (if (and (not (null? val))
207 (right-type? (car val))
209 (cons (obj:name self) (car val))
210 (parse-error context message (cons (obj:name self) val))))
213 ; A boolean attribute's value is either #t or #f.
216 <boolean-attribute> 'parse-value
217 (/parse-simple-attribute boolean? "boolean attribute not one of #f/#t")
221 <string-attribute> 'parse-value
222 (/parse-simple-attribute string? "invalid argument to string attribute"))
224 ; A bitset attribute's value is a list of symbols.
225 ; For backwards compatibility (ATTR val1,val2,val3) and
226 ; (ATTR "val1,val2,val3") are also supported for bitset values.
227 ; val1,val2,val3 is not portable (e.g. mzscheme will reject it).
229 ; We don't validate the values. In the case of the MACH attribute,
230 ; there's no current mechanism to create it after all define-mach's have
232 ; ??? Need to decide whether all define-mach's must appear before any
233 ; define-insn's. It would be nice to be able to spread an architecture's
234 ; description over several .cpu files.
235 ; ??? On the other hand, all machs are specified in define-arch.
236 ; Perhaps creation of builtins could be defered until then.
239 <bitset-attribute> 'parse-value
240 (lambda (self context val)
241 (let ((value (if (and (= (length val) 1)
242 (or (symbol? (car val)) (string? (car val))))
243 (map string->symbol (string-cut (->string (car val)) #\,))
245 (message "improper bitset attribute"))
246 ;; NOTE: An empty list is ok.
247 (if (all-true? (map symbol? value))
248 (cons (obj:name self) value)
249 (parse-error context message (cons (obj:name self) val)))))
252 ; An integer attribute's value is a number
253 ; (or maybe a symbol representing that value).
256 <integer-attribute> 'parse-value
257 (/parse-simple-attribute (lambda (x) (or (number? x) (symbol? x)))
258 "improper integer attribute")
261 ; An enum attribute's value is a symbol representing that value.
264 <enum-attribute> 'parse-value
265 (/parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
266 "improper enum attribute")
269 ; Parse a boolean attribute's value definition.
272 <boolean-attribute> 'parse-value-def
273 (lambda (self context values)
274 (if (equal? values '(#f #t))
276 (parse-error context "boolean value list must be (#f #t)" values)))
279 ; Ignore values for strings.
280 ; They're not supported and /attr-read catches this.
283 <string-attribute> 'parse-value-def
284 (lambda (self context values) #f)
287 ; Parse a bitset attribute's value definition.
290 <bitset-attribute> 'parse-value-def
291 (lambda (self context values)
292 ;; parse-enum-vals works well enough
293 (parse-enum-vals context "" values))
296 ; Parse an integer attribute's value definition.
297 ; VALUES may be #f which means any value is ok.
298 ; A fixed set of VALUES is work-in-progress.
301 <integer-attribute> 'parse-value-def
302 (lambda (self context values)
304 (for-each (lambda (val)
305 ;; A list entry is for providing a sanitization key.
306 (if (or (not (list? val))
307 (not (number? (car val))))
309 "invalid element in integer attribute list"
315 ; Parse an enum attribute's value definition.
316 ; See parse-enum-vals for more info.
319 <enum-attribute> 'parse-value-def
320 (lambda (self context values)
321 (parse-enum-vals context "" values))
324 ; Make an attribute list object from a list of name/value pairs.
326 (define (atlist-make prefix . attrs) (make <attr-list> prefix attrs))
328 ; Parse an attribute definition.
329 ; This is the main routine for building an attribute object from a
330 ; description in the .cpu file.
331 ; All arguments are in raw (non-evaluated) form.
332 ; TYPE-CLASS is the class of the object to create.
333 ; i.e. one of <{boolean,bitset,integer,enum,string}-attribute>.
334 ; For enum attributes, if DEFAULT is #f use the first value.
335 ; For all other attribute kinds, we use what /attr-read gives us.
336 ; ??? Allowable values for integer attributes is wip,
337 ; for now it is the portable set of integers (int32_t).
339 (define (/attr-parse context type-class name comment attrs for default values)
340 (logit 2 "Processing attribute " name " ...\n")
342 ;; Pick out name first to augment the error context.
343 (let* ((name (parse-name context name))
344 (context (context-append-name context name))
345 (result (new type-class))
346 (parsed-values (send result 'parse-value-def context values)))
348 (elm-xset! result 'name name)
349 (elm-xset! result 'comment (parse-comment context comment))
350 (elm-xset! result 'attrs (atlist-parse context attrs ""))
351 (elm-xset! result 'for for)
354 ;; FIXME: Clean up with /attr-read.
355 (case (class-name type-class)
356 ((<boolean-attribute>)
357 ;; ??? docs say default must be #f, but we want to allow an rtx to
358 ;; specify the default.
359 (if (and (not (memq default '(#f #t)))
360 (not (/attr-val-is-rtx? default)))
361 (parse-error context "invalid default" default))
362 (elm-xset! result 'default default))
363 ((<string-attribute>)
364 (let ((default (or default "")))
365 (if (and (not (string? default))
366 (not (/attr-val-is-rtx? default)))
367 (parse-error context "invalid default" default))
368 (elm-xset! result 'default default)))
369 ((<integer-attribute>)
370 (let ((default (if default default (if (null? values) 0 (car values)))))
371 (if (and (not (integer? default))
372 (not (/attr-val-is-rtx? default)))
373 (parse-error context "invalid default" default))
374 (elm-xset! result 'default default)))
376 (let ((default (if default default (caar parsed-values))))
377 (if (and (not (assq default parsed-values))
378 (not (/attr-val-is-rtx? default)))
379 (parse-error context "invalid default" default))
380 (elm-xset! result 'default default)))
381 ((<bitset-attribute>)
382 ;; bitset attributes must specify a default, /attr-read catches this
384 ;; It's also /attr-read's job to ensure it is a list.
385 (assert (list? default))
386 (let ((default default))
387 ;; NOTE: We don't allow an rtx for bitset attributes,
388 ;; the rtl language currently doesn't support them.
389 (if (/attr-val-is-rtx? default)
390 (parse-error context "invalid default, rtx not supported for bitset" default))
391 (if (not (all-true? (map (lambda (v) (assq v parsed-values))
393 (parse-error context "invalid default" default))
394 (elm-xset! result 'default default))))
396 (elm-xset! result 'values parsed-values)
401 ; Read an attribute description
402 ; This is the main routine for analyzing attributes in the .cpu file.
403 ; CONTEXT is a <context> object for error messages.
404 ; ARG-LIST is an associative list of field name and field value.
405 ; /attr-parse is invoked to create the attribute object.
407 (define (/attr-read context . arg-list)
409 (type 'not-set) ;; attribute type
410 (type-class 'not-set) ;; attribute class
414 (for #f) ;; assume for everything
415 (default #f) ;; #f indicates "not set"
416 (values #f) ;; #f indicates "not set"
419 ;; Loop over each element in ARG-LIST, recording what's found.
420 (let loop ((arg-list arg-list))
423 (let ((arg (car arg-list))
424 (elm-name (caar arg-list)))
427 (set! type-class (case (cadr arg)
428 ((boolean) <boolean-attribute>)
429 ((string) <string-attribute>)
430 ((bitset) <bitset-attribute>)
431 ((integer) <integer-attribute>)
432 ((enum) <enum-attribute>)
435 "invalid attribute type"
437 (set! type (cadr arg)))
438 ((name) (set! name (cadr arg)))
439 ((comment) (set! comment (cadr arg)))
440 ((attrs) (set! attrs (cdr arg)))
441 ((for) (set! for (cdr arg)))
442 ((default) (set! default (cdr arg)))
443 ((values) (set! values (cdr arg)))
444 (else (parse-error context "invalid attribute arg" arg)))
445 (loop (cdr arg-list)))))
447 ;; Must have type now.
448 (if (eq? type-class 'not-set)
449 (parse-error context "type not specified") arg-list)
451 ;; For scalar attributes, fix up the default.
452 (if (and default (memq type '(boolean string integer enum)))
454 (if (!= (length default) 1)
455 (parse-error context "invalid default" default))
456 ;; Don't change rtx values.
457 (if (not (pair? (car default)))
458 (set! default (car default)))))
460 ;; Establish proper defaults now that we know the type.
461 ;; FIXME: Clean up with /attr-parse.
465 (set! default #f)) ;; really a nop, but for consistency
467 (set! values '(#f #t))))
470 (parse-error context "bitset attribute default not specified"
473 (parse-error context "bitset attribute values not specified"
479 (set! values #f))) ;; really a nop, but for consistency
481 ;; There are some existing cases where no default is specified,
482 ;; expecting that the first value is the default.
483 ;; (if (eq? default #f)
484 ;; (parse-error context "enum attribute default not specified"
487 (parse-error context "enum attribute values not specified"
492 (if (not (eq? values #f))
493 (parse-error context "string attribute values specified"
497 ;; Now that we've identified the elements, build the object.
498 (/attr-parse context type-class name comment attrs for default values))
501 ; Main routines for defining attributes in .cpu files.
505 (let ((a (apply /attr-read (cons (make-current-context "define-attr")
507 (current-attr-add! a)
513 ; Lookup ATTR-NAME in ATTR-LIST.
514 ; The result is the object or #f if not found.
516 (define (attr-lookup attr-name attr-list)
517 (object-assq attr-name attr-list)
520 ; Return a boolean indicating if boolean attribute ATTR is "true" in
521 ; attribute alist ALIST.
522 ; Note that if the attribute isn't present, it is defined to be #f.
525 <attr-list> 'has-attr?
527 (let ((a (assq attr (elm-get self 'attrs))))
529 ((boolean? (cdr a)) (cdr a))
530 (else (error "Not a boolean attribute:" attr)))))
533 (define (atlist-has-attr? atlist attr)
534 (send atlist 'has-attr? attr)
537 ; Return a boolean indicating if attribute ATTR is present in
538 ; attribute alist ALIST.
541 <attr-list> 'attr-present?
543 (->bool (assq attr (elm-get self 'attrs))))
546 (define (atlist-attr-present? atlist attr)
547 (send atlist 'attr-present? attr)
550 ;; Return #t if attribute value VAL is an rtx expression.
551 ;; RTXs in attributes are recorded as a list of one element
553 ;; I.e., ((rtx foo bar)).
555 (define (/attr-val-is-rtx? val)
558 (pair? (car val))) ;; pair? -> cheap non-null-list?
561 ; Expand attribute value ATVAL, which is an rtx expression.
562 ; OWNER is the containing object or #f if there is none.
563 ; OWNER is needed if an attribute is defined in terms of other attributes.
564 ; OWNER is also needed to get the ISA(s) in which to evaluate the expression.
565 ; If it's #f obviously ATVAL can't be defined in terms of others,
566 ; or refer to operands that require an ISA to disambiguate.
568 (define (/attr-eval atval owner)
569 (let* ((atval-expr (car atval))
570 (expr (rtx-simplify #f owner
571 (rtx-canonicalize #f 'DFLT
572 (and owner (obj-isa-list owner))
575 (value (rtx-value expr owner)))
576 (cond ((symbol? value) value)
577 ((number? value) value)
578 (error "/attr-eval: internal error, unsupported result:" value)))
581 ; Return value of ATTR in attribute alist ALIST.
582 ; If not present, return the default value.
583 ; If ATTR is an unknown attribute, return #f.
584 ; OWNER is the containing object or #f if there is none.
586 (define (attr-value alist attr owner)
587 (let ((a (assq-ref alist attr)))
589 (if (/attr-val-is-rtx? a)
592 (attr-lookup-default attr owner)))
595 ; Return the value of ATTR in ATLIST.
596 ; If not present, return the default value.
597 ; If ATTR is an unknown attribute, return #f.
598 ; OWNER is the containing object or #f if there is none.
600 (define (atlist-attr-value atlist attr owner)
601 (attr-value (atlist-attrs atlist) attr owner)
604 ; Same as atlist-attr-value but return nil if attribute not present.
606 (define (atlist-attr-value-no-default atlist attr owner)
607 (let ((a (assq-ref (atlist-attrs atlist) attr)))
609 (if (/attr-val-is-rtx? a)
615 ; Return the default for attribute A.
617 ; If A is unknown return #f.
618 ; This means the caller can't distinguish booleans from unknowns,
619 ; but the caller is left to deal with that.
621 ; OWNER is the containing object or #f if there is none.
623 (define (attr-lookup-default a owner)
624 (let ((at (current-attr-lookup a)))
627 #f ;; FIXME: should fetch default from the attribute
628 (let ((deflt (attr-default at)))
630 (if (/attr-val-is-rtx? deflt)
631 (/attr-eval deflt owner)
633 ;; If no default was provided, use the first value.
634 ;; FIXME: This shouldn't happen. /attr-parse should DTRT.
635 (caar (attr-values at)))))
639 ; Return a boolean indicating if X is present in BITSET.
640 ; Bitset values are recorded as (val1 val2 ...).
642 (define (bitset-attr-member? x bitset)
643 (->bool (memq x bitset))
646 ; Routines for accessing attributes in objects.
648 ; Get/set attributes of OBJ.
649 ; OBJ is any object which supports the get-atlist message.
651 (define (obj-atlist obj)
652 (let ((result (send obj 'get-atlist)))
653 ; As a speed up, we allow objects to specify an empty attribute list
654 ; with #f or (), rather than creating an attr-list object.
655 ; ??? There is atlist-empty now which should be used directly.
656 (if (or (null? result) (not result))
661 (define (obj-set-atlist! obj attrs) (send obj 'set-atlist! attrs))
663 ; Add attribute ATTR to OBJ.
664 ; The attribute is prepended to the front so it overrides any existing
667 (define (obj-cons-attr! obj attr)
668 (obj-set-atlist! obj (atlist-cons attr (obj-atlist obj)))
671 ; Add attribute list ATLIST to OBJ.
672 ; Attributes in ATLIST override existing values, so ATLIST is "prepended".
674 (define (obj-prepend-atlist! obj atlist)
675 ; Must have same prefix.
676 (assert (equal? (atlist-prefix (obj-atlist obj))
677 (atlist-prefix atlist)))
678 (obj-set-atlist! obj (atlist-append atlist (obj-atlist obj)))
681 ; Return boolean of whether OBJ has boolean attribute ATTR or not.
682 ; OBJ is any object that supports attributes.
684 (define (obj-has-attr? obj attr)
685 (atlist-has-attr? (obj-atlist obj) attr)
688 ; FIXME: for backward compatibility. Delete in time.
689 (define has-attr? obj-has-attr?)
691 ; Return a boolean indicating if attribute ATTR is present in OBJ.
693 (define (obj-attr-present? obj attr)
694 (atlist-attr-present? (obj-atlist obj) attr)
697 ; Return value of attribute ATTR in OBJ.
698 ; If the attribute isn't present, the default is returned.
699 ; If ATTR is an unknown attribute, return #f.
700 ; OBJ is any object that supports the get-atlist method.
702 (define (obj-attr-value obj attr)
703 (let ((atlist (obj-atlist obj)))
704 (atlist-attr-value atlist attr obj))
707 ; Return boolean of whether OBJ has attribute ATTR value VALUE or not.
708 ; OBJ is any object that supports attributes.
709 ; NOTE: The default value of the attribute IS considered.
711 (define (obj-has-attr-value? obj attr value)
712 (let ((a (obj-attr-value obj attr)))
716 ; Return boolean of whether OBJ explicitly has attribute ATTR value VALUE
718 ; OBJ is any object that supports attributes.
719 ; NOTE: The default value of the attribute IS NOT considered.
721 (define (obj-has-attr-value-no-default? obj attr value)
722 (let* ((atlist (obj-atlist obj))
723 (objs-value (atlist-attr-value-no-default atlist attr obj)))
724 (and (not (null? objs-value)) (eq? value objs-value)))
729 ; Generate a list representing a bit mask of the indices of 'values'
730 ; within 'all-values'. Each element in the resulting list represents a byte.
731 ; Both bits and bytes are indexed from left to right starting at 0
732 ; with 8 bits in a byte.
734 (define (charmask-bytes values all-values vec-length)
735 (logit 3 "charmask-bytes for " values " " all-values "\n")
736 (let ((result (make-vector vec-length 0))
737 (indices (map (lambda (name)
738 (list-ref (map cadr all-values)
739 (element-lookup-index name (map car all-values) 0)))
741 (logit 3 "indices: " indices "\n")
742 (for-each (lambda (x)
743 (let* ((byteno (quotient x 8))
744 (bitno (- 7 (remainder x 8)))
745 (byteval (logior (vector-ref result byteno)
747 (vector-set! result byteno byteval)))
749 (logit 3 "result: " (vector->list result) "\n")
750 (vector->list result))
753 ; Convert a bitset value into a bit string based on the
754 ; index of each member in values.
755 ; VALUE is a list of symbols in the bitset.
756 ; VALUES is the values member of the attribute's definition.
758 (define (/bitset-attr->charmask value values)
759 (let* ((values-names (map car values))
760 (values-values (map cadr values))
761 (vec-length (+ 1 (quotient (apply max values-values) 8))))
762 (string-append "{ " (number->string vec-length) ", \""
763 (string-map (lambda (x)
764 (string-append "\\x" (number->hex x)))
765 (charmask-bytes value values vec-length))
769 ; Return the enum of ATTR-NAME for type TYPE.
770 ; TYPE is one of 'ifld, 'hw, 'operand, 'insn.
772 (define (gen-attr-enum type attr-name)
773 (string-upcase (string-append "CGEN_" type "_" (gen-sym attr-name)))
776 ; Return a list of enum value definitions for gen-enum-decl.
777 ; Attributes numbers are organized as follows: booleans are numbered 0-31.
778 ; The range is because that's what fits in a portable int. Unused numbers
779 ; are left unused. Non-booleans are numbered starting at 32.
780 ; An alternative is start numbering the booleans at 32. The generated code
781 ; is simpler with the current way (the "- 32" to get back the bit number or
782 ; array index number occurs less often).
784 ; Three special values are created:
785 ; END-BOOLS - mark end of boolean attributes
786 ; END-NBOOLS - mark end of non-boolean attributes
787 ; START-NBOOLS - marks the start of the non-boolean attributes
788 ; (needed in case first non-bool is sanytized out).
790 ; ATTR-OBJ-LIST is a list of <attribute> objects (always subclassed of course).
792 (define (attr-list-enum-list attr-obj-list)
793 (let ((sorted-attrs (/attr-sort (attr-remove-meta-attrs attr-obj-list))))
794 (assert (<= (length (car sorted-attrs)) 32))
796 (map (lambda (bool-attr)
797 (list (obj:name bool-attr) '-
798 (atlist-attrs (obj-atlist bool-attr))))
801 (list '(START-NBOOLS 31))
802 (map (lambda (nbool-attr)
803 (list (obj:name nbool-attr) '-
804 (atlist-attrs (obj-atlist nbool-attr))))
810 ; Sort an alist of attributes so non-boolean attributes are at the front.
811 ; This is used to sort a particular object's attributes.
812 ; This is required by the C support code (cgen.h:CGEN_ATTR_VALUE).
813 ; Boolean attributes appear as (NAME . #t/#f), non-boolean ones appear as
814 ; (NAME . VALUE). Attributes of the same type are sorted by name.
816 (define (/attr-sort-alist alist)
819 ;(display (list a b "\n"))
820 (cond ((and (boolean? (cdr a)) (boolean? (cdr b)))
821 (string<? (symbol->string (car a)) (symbol->string (car b))))
822 ((boolean? (cdr a)) #f) ; we know b is non-bool here
823 ((boolean? (cdr b)) #t) ; we know a is non-bool here
824 (else (string<? (symbol->string (car a))
825 (symbol->string (car b)))))))
828 ; Sort ATTR-LIST into two lists: bools and non-bools.
829 ; The car of the result is the bools, the cdr is the non-bools.
830 ; Attributes requiring a fixed index have the INDEX attribute,
831 ; and used for the few special attributes that are refered to by
832 ; architecture independent code.
833 ; For each of non-bools and bools, put attributes with the INDEX attribute
834 ; first. This is used to sort a list of attributes for output (e.g. define
837 ; FIXME: Record index number with the INDEX attribute and sort on it.
838 ; At present it's just a boolean.
840 (define (/attr-sort attr-list)
841 (let loop ((fixed-non-bools nil)
842 (non-fixed-non-bools nil)
844 (non-fixed-bools nil)
845 (attr-list attr-list))
846 (cond ((null? attr-list)
847 (cons (append! (reverse! fixed-bools)
848 (reverse! non-fixed-bools))
849 (append! (reverse! fixed-non-bools)
850 (reverse! non-fixed-non-bools))))
851 ((bool-attr? (car attr-list))
852 (if (obj-has-attr? (car attr-list) 'INDEX)
853 (loop fixed-non-bools non-fixed-non-bools
854 (cons (car attr-list) fixed-bools) non-fixed-bools
856 (loop fixed-non-bools non-fixed-non-bools
857 fixed-bools (cons (car attr-list) non-fixed-bools)
860 (if (obj-has-attr? (car attr-list) 'INDEX)
861 (loop (cons (car attr-list) fixed-non-bools) non-fixed-non-bools
862 fixed-bools non-fixed-bools
864 (loop fixed-non-bools (cons (car attr-list) non-fixed-non-bools)
865 fixed-bools non-fixed-bools
869 ; Return number of non-bools in attributes ATLIST.
871 (define (attr-count-non-bools atlist)
872 (count-true (map (lambda (a) (not (bool-attr? a)))
876 ; Given an alist of attributes, return the non-bools.
878 (define (attr-non-bool-attrs alist)
879 (let loop ((result nil) (alist alist))
880 (cond ((null? alist) (reverse! result))
881 ((boolean? (cdar alist)) (loop result (cdr alist)))
882 (else (loop (cons (car alist) result) (cdr alist)))))
885 ; Given an alist of attributes, return the bools.
887 (define (attr-bool-attrs alist)
888 (let loop ((result nil) (alist alist))
889 (cond ((null? alist) (reverse! result))
890 ((boolean? (cdar alist))
891 (loop (cons (car alist) result) (cdr alist)))
892 (else (loop result (cdr alist)))))
895 ; Parse an attribute spec.
896 ; CONTEXT is a <context> object or #f if there is none.
897 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
898 ; The result is the attribute alist.
900 (define (attr-parse context attrs)
901 (logit 4 (list 'attr-parse context attrs) "\n")
902 (if (not (list? attrs))
903 (parse-error context "improper attribute list" attrs))
905 (for-each (lambda (elm)
908 (if (char=? (string-ref (symbol->string elm) 0) #\!)
909 (set! alist (acons (string->symbol (string-drop1 (symbol->string elm))) #f alist))
910 (set! alist (acons elm #t alist)))
911 (if (not (current-attr-lookup (caar alist)))
912 (parse-error context "unknown attribute" (caar alist))))
913 ((and (list? elm) (pair? elm) (symbol? (car elm)))
914 (let ((a (current-attr-lookup (car elm))))
916 (parse-error context "unknown attribute" elm))
917 (set! alist (cons (send a 'parse-value
920 (else (parse-error context "improper attribute" elm))))
925 ; Parse an object attribute spec.
926 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
927 ; The result is an <attr-list> object.
929 (define (atlist-parse context attrs prefix)
930 (make <attr-list> prefix (attr-parse context attrs))
933 ;; Return the source form of an atlist's values.
934 ;; Externally scalar attributes (boolean, integer, enum and string) are
935 ;; ((name1 value1) (name2 value2) ...).
936 ;; Internally they are ((name1 . value1) (name2 . value2) ...).
937 ;; Externally bitset attributes are (name value1 value2 ...).
938 ;; Internally they are the same, (name value1 value2 ...).
939 ;; If the value is an rtx expression, externally it is (name (expr)),
940 ;; and internally it is the same, (name (expr)).
942 (define (atlist-source-form atlist)
944 (let ((value (cdr attr)))
946 (cons (car attr) value)
947 (list (car attr) value))))
948 (atlist-attrs atlist))
951 ; Cons an attribute to an attribute list to create a new attribute list.
952 ; ATLIST is either an attr-list object or #f or () (both of the latter two
953 ; signify an empty attribute list, in which case we make the prefix of the
956 (define (atlist-cons attr atlist)
957 (if (or (not atlist) (null? atlist))
958 (make <attr-list> "" (cons attr nil))
959 (make <attr-list> (atlist-prefix atlist) (cons attr (atlist-attrs atlist))))
962 ; Append one attribute list to another.
963 ; The prefix for the new atlist is taken from the first one.
965 (define (atlist-append attr-list1 attr-list2)
967 (atlist-prefix attr-list1)
968 (append (atlist-attrs attr-list1) (atlist-attrs attr-list2)))
971 ; Remove meta-attributes from ALIST.
972 ; "meta" may be the wrong adjective to use here.
973 ; The attributes in question are not intended to appear in generated files.
974 ; They started out being attributes of attributes, hence the name "meta".
976 (define (attr-remove-meta-attrs-alist alist)
977 (let ((all-attrs (current-attr-list)))
978 ; FIXME: Why not use find?
979 (let loop ((result nil) (alist alist))
982 (let ((attr (attr-lookup (caar alist) all-attrs)))
983 (if (and attr (has-attr? attr 'META))
984 (loop result (cdr alist))
985 (loop (cons (car alist) result) (cdr alist)))))))
988 ; Remove meta-attributes from ATTR-LIST.
989 ; "meta" may be the wrong adjective to use here.
990 ; The attributes in question are not intended to appear in generated files.
991 ; They started out being attributes of attributes, hence the name "meta".
993 (define (attr-remove-meta-attrs attr-list)
994 ; FIXME: Why not use find?
995 (let loop ((result nil) (attr-list attr-list))
996 (cond ((null? attr-list)
998 ((has-attr? (car attr-list) 'META)
999 (loop result (cdr attr-list)))
1001 (loop (cons (car attr-list) result) (cdr attr-list)))))
1004 ; Remove duplicates from ATTRS, a list of attributes.
1005 ; Attribute lists are typically small so we use a simple O^2 algorithm.
1006 ; The leading entry of an attribute overrides subsequent ones so this is
1007 ; defined to pick the first entry of each attribute.
1009 (define (attr-nub attrs)
1010 (let loop ((result nil) (attrs attrs))
1011 (cond ((null? attrs) (reverse! result))
1012 ((assq (caar attrs) result) (loop result (cdr attrs)))
1013 (else (loop (cons (car attrs) result) (cdr attrs)))))
1016 ; Return a list of all attrs in TABLE-LIST, a list of lists of arbitrary
1017 ; elements. A list of lists is passed to simplify computation of insn
1018 ; attributes where the insns and macro-insns are on separate lists and
1019 ; appending them into one list would be unnecessarily expensive.
1020 ; ACCESSOR is a function to access the attrs field from TABLE-LIST.
1021 ; Duplicates are eliminated and the list is sorted so non-boolean attributes
1022 ; are at the front (required by the C code that fetches attribute values).
1023 ; STD-ATTRS is an `attr-list' object of attrs that are always available.
1024 ; The actual values returned are random (e.g. #t vs #f). We could
1025 ; canonicalize them.
1026 ; The result is an alist of all the attributes that are used in TABLE-LIST.
1027 ; ??? The cdr of each element is some random value. Perhaps it should be
1028 ; the default value or perhaps we should just return a list of names.
1029 ; ??? No longer used.
1031 (define (attr-compute-all table-list accessor std-attrs)
1032 (let ((accessor (lambda (elm) (atlist-attrs (accessor elm)))))
1033 (attr-remove-meta-attrs-alist
1038 (map (lambda (table-elm)
1040 (find-apply accessor
1042 (let ((attrs (accessor e)))
1043 (not (null? attrs))))
1046 (atlist-attrs std-attrs))))))
1049 ; Return lists of attributes for particular object types.
1050 ; FIXME: The output shouldn't be required to be sorted.
1052 (define (current-attr-list-for type)
1053 (let ((sorted (/attr-sort (find (lambda (a)
1055 (memq type (atlist-for a))
1057 (attr-remove-meta-attrs
1058 (current-attr-list))))))
1059 ; Current behaviour puts the non-bools at the front.
1060 (append! (cdr sorted) (car sorted)))
1062 (define (current-ifld-attr-list)
1063 (current-attr-list-for 'ifield)
1065 (define (current-hw-attr-list)
1066 (current-attr-list-for 'hardware)
1068 (define (current-op-attr-list)
1069 (current-attr-list-for 'operand)
1071 (define (current-insn-attr-list)
1072 (current-attr-list-for 'insn)
1075 ; Methods to emit the C value of an attribute.
1076 ; These don't _really_ belong here (C code doesn't belong in the appl'n
1077 ; independent part of CGEN), but there isn't a better place for them
1078 ; (maybe utils-cgen.scm?) and there's only a few of them.
1081 <boolean-attribute> 'gen-value-for-defn-raw
1082 (lambda (self value)
1086 ;(string-upcase (string-append (obj:str-name self) "_" value)))
1090 <boolean-attribute> 'gen-value-for-defn
1091 (lambda (self value)
1092 (send self 'gen-value-for-defn-raw value))
1095 ;; NOTE: VALUE is a list of symbols in the bitset.
1098 <bitset-attribute> 'gen-value-for-defn-raw
1099 (lambda (self value)
1100 (if (string=? (string-downcase (gen-sym self)) "isa")
1101 (/bitset-attr->charmask value (elm-get self 'values))
1104 (string-map (lambda (x)
1105 (string-append "|(1<<"
1107 "_" (gen-c-symbol x) ")"))
1112 ;; NOTE: VALUE is a list of symbols in the bitset.
1115 <bitset-attribute> 'gen-value-for-defn
1116 (lambda (self value)
1119 (if (string=? (string-downcase (gen-sym self)) "isa")
1120 (/bitset-attr->charmask value (elm-get self 'values))
1125 (string-map (lambda (x)
1126 (string-append "|(1<<"
1128 "_" (gen-c-symbol x) ")"))
1136 <integer-attribute> 'gen-value-for-defn-raw
1137 (lambda (self value)
1138 (number->string value)
1143 <integer-attribute> 'gen-value-for-defn
1144 (lambda (self value)
1147 (send self 'gen-value-for-defn-raw value)
1153 <enum-attribute> 'gen-value-for-defn-raw
1154 (lambda (self value)
1156 (gen-c-symbol (string-append (obj:str-name self)
1158 (symbol->string value))))
1163 <enum-attribute> 'gen-value-for-defn
1164 (lambda (self value)
1167 (send self 'gen-value-for-defn-raw value)
1172 ;; Doesn't handle escape sequences.
1174 <string-attribute> 'gen-value-for-defn-raw
1175 (lambda (self value)
1176 (string-append "\"" value "\""))
1180 <string-attribute> 'gen-value-for-defn
1181 (lambda (self value)
1182 (send self 'gen-value-for-defn-raw value))
1186 ; Called before loading a .cpu file to initialize.
1188 (define (attr-init!)
1190 (reader-add-command! 'define-attr
1192 Define an attribute, name/value pair list version.
1194 nil 'arg-list define-attr)
1199 ; Called before a . cpu file is read in to install any builtins.
1200 ; One thing this does is define all attributes requiring a fixed index,
1201 ; keeping them all in one place.
1202 ; ??? Perhaps it would make sense to define all predefined attributes here.
1204 (define (attr-builtin!)
1205 (define-attr '(type boolean) '(name VIRTUAL) '(comment "virtual object"))
1207 ; The meta attribute is used for attributes that aren't to appear in
1208 ; generated output (need a better name).
1209 (define-attr '(for attr) '(type boolean) '(name META))
1211 ; Objects to keep local to a generated file.
1212 (define-attr '(for keyword) '(type boolean) '(name PRIVATE))
1214 ; Attributes requiring fixed indices.
1215 (define-attr '(for attr) '(type boolean) '(name INDEX) '(attrs META))
1217 ; ALIAS is used for instructions that are aliases of more general insns.
1218 ; ALIAS insns are ignored by the simulator.
1219 (define-attr '(for insn) '(type boolean) '(name ALIAS)
1220 '(comment "insn is an alias of another")
1226 ; Called after loading a .cpu file to perform any post-processing required.
1228 (define (attr-finish!)