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 4 kinds of attributes: boolean, integer, enum, and bitset. Boolean
7 ; attributes are really enum attributes with two possible values, but they
8 ; occur frequently enough that they are special cased.
10 ; All objects that use attributes must have two methods:
11 ; - 'get-atlist - returns the object's attr-list
12 ; - 'set-atlist! - set the object's attr-list
14 ; In .cpu files, attribute lists are associative lists of (NAME VALUE).
15 ; Boolean attributes are specified as (NAME #t) or (NAME #f),
16 ; but for convenience ATTR and !ATTR are also supported.
17 ; integer/enum attrs are specified as (ATTR value).
18 ; string attrs are specified as (ATTR value).
19 ; Bitset attrs are specified as (ATTR val1,val2,val3).
20 ; In all cases the value needn't be constant, and can be an expression,
21 ; though expressions are currently only supported for META-attributes
22 ; (attributes that don't appear in any generated code).
25 ; (FOO1 !FOO2 (BAR 3) (FOO3 X) (MACH sparc,sparclite))
27 ; ??? Implementation of expressions is being postponed as long
28 ; as possible, avoiding adding complications for complication's sake, and
29 ; because I'm not completely sure how I want to do them.
30 ; The syntax for an expression value is (ATTR (rtx-func ...)).
32 ; ??? May wish to allow a bitset attribute like (ATTR val1,!val2), where `!'
33 ; means to turn off that particular bit (or bits if val2 refers to several).
35 ; ??? May wish to allow specifying enum attributes by only having to
36 ; specify the value (move names into "enum space" or some such).
38 ; An attr-list (or "atlist") is a collection of attributes.
39 ; Attributes are stored as an associative list.
40 ; There is possible confusion between "alist" (associative-list) and
41 ; "atlist" (attribute-list) but in practice I haven't had a problem.
42 ; ??? May wish to change this to a list of objects, as the alist doesn't carry
43 ; enough info. However the alist is simple and fast.
45 (define <attr-list> (class-make '<attr-list> nil '(prefix attrs) nil))
47 (define atlist-prefix (elm-make-getter <attr-list> 'prefix))
48 (define atlist-attrs (elm-make-getter <attr-list> 'attrs))
50 (define (atlist? x) (class-instance? <attr-list> x))
52 ; An empty attribute-list.
54 (define atlist-empty (make <attr-list> "" nil))
56 ; The attribute baseclass.
57 ; The attributes of <ident> are the set of attributes for this attribute
58 ; [meaning attributes themselves can have attributes].
59 ; [Ya, that's clumsily written. I left it that way for fun.]
60 ; An odd notion that is of some use. It's current raison d'etre is to
61 ; support sanitization of attributes [which is implemented with the
62 ; `sanitize' attribute].
65 (class-make '<attribute>
68 ; List of object types this attribute is for.
69 ; Possible element values are:
70 ; attr, enum, cpu, mach, model, ifield, hardware, operand,
72 ; A value of #f means the attribute is for everything.
80 (define atlist-for (elm-make-getter <attribute> 'for))
82 ; A class for each type of attribute.
84 ; `values' exists for boolean-attribute to simplify the code, it's ignored.
85 ; Ditto for `default'. The default for boolean-attribute is always #f.
87 (define <boolean-attribute>
88 (class-make '<boolean-attribute>
94 ; VALUES is ignored for string-attribute.
96 (define <string-attribute>
97 (class-make '<string-attribute>
103 ; For bitset attributes VALUES is a list of
104 ; (symbol bit-number-or-#f attr-list comment-or-#f),
106 ; If bit-number is #f (unspecified), cgen will choose.
107 ; Int's are used to record the bitset in the generated code so there's a limit
108 ; of 32 elements, though there's nothing inherent in the description language
109 ; that precludes removing the limit.
110 ; NOTE: While one might want to record each element as an object, there's
111 ; currently no need for the added complexity.
113 (define <bitset-attribute>
114 (class-make '<bitset-attribute>
120 ; For integer attributes VALUES is a list of (int),
121 ; one for each possible value,
122 ; or the empty list of all values are permissible.
123 ; Note that each element is itself a list. This is for consistency.
125 (define <integer-attribute>
126 (class-make '<integer-attribute>
132 ; For enum attributes VALUES is a list of
133 ; (symbol enum-value-or-#f attr-list comment-or-#f),
134 ; one for each possible.
135 ; If enum-value is #f (unspecified) cgen will apply the standard rule for
136 ; assigning enum values.
137 ; NOTE: While one might want to record each element as an object, there's
138 ; currently no need for the added complexity.
140 (define <enum-attribute>
141 (class-make '<enum-attribute>
147 ; Return a boolean indicating if X is a <boolean-attribute> object.
149 (define (bool-attr? x) (class-instance? <boolean-attribute> x))
151 ; Return a boolean indicating if X is a <bitset-attribute> object.
153 (define (bitset-attr? x) (class-instance? <bitset-attribute> x))
155 ; Return a symbol indicating the kind of attribute ATTR is.
156 ; The result is one of boolean,integer,enum,bitset or string.
158 (define (attr-kind attr)
159 (case (object-class-name attr)
160 ((<boolean-attribute>) 'boolean)
161 ((<string-attribute>) 'string)
162 ((<integer-attribute>) 'integer)
163 ((<enum-attribute>) 'enum)
164 ((<bitset-attribute>) 'bitset)
165 (else (error "attr-kind: internal error, not an attribute class"
166 (object-class-name attr))))
171 (define (attr-default attr) (elm-xget attr 'default))
172 (define (attr-values attr) (elm-xget attr 'values))
174 ; Create an attribute.
175 ; Attributes are stored in attribute lists using the actual value
176 ; rather than an object containing the value, so we only have to cons
177 ; NAME and VALUE rather than building some object. This is for simplicity
178 ; and speed. We try to incrementally complicate things, only as necessary.
180 ; VALUE must be #f or #t.
182 (define (bool-attr-make name value) (cons name value))
184 ; VALUES must be a comma separated list of symbols
185 ; (e.g. val1,val2 not (val1 val2)).
186 ; FIXME: require values to be a string (i.e. "val1,val2")
188 (define (bitset-attr-make name values) (cons name values))
190 ; VALUE must be a number (or maybe a symbol).
192 (define (int-attr-make name value) (cons name value))
194 ; VALUE must be a symbol.
196 (define (enum-attr-make name value) (cons name value))
198 ;;; Return a procedure to parse an attribute.
199 ;;; RIGHT-TYPE? is a procedure that verifies the value is the right type.
200 ;;; MESSAGE is printed if there is an error.
202 (define (/parse-simple-attribute right-type? message)
203 (lambda (self context val)
204 (if (and (not (null? val))
205 (right-type? (car val))
207 (cons (obj:name self) (car val))
208 (parse-error context message (cons (obj:name self) val))))
211 ; A boolean attribute's value is either #t or #f.
214 <boolean-attribute> 'parse-value
215 (/parse-simple-attribute boolean? "boolean attribute not one of #f/#t")
219 <string-attribute> 'parse-value
220 (/parse-simple-attribute string? "invalid argument to string attribute"))
222 ; A bitset attribute's value is a comma separated list of elements.
223 ; We don't validate the values. In the case of the MACH attribute,
224 ; there's no current mechanism to create it after all define-mach's have
226 ; ??? Need to decide whether all define-mach's must appear before any
227 ; define-insn's. It would be nice to be able to spread an architecture's
228 ; description over several .cpu files.
229 ; ??? On the other hand, all machs are specified in define-arch.
230 ; Perhaps creation of builtins could be defered until then.
233 <bitset-attribute> 'parse-value
234 (/parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
235 "improper bitset attribute")
238 ; An integer attribute's value is a number
239 ; (or maybe a symbol representing that value).
242 <integer-attribute> 'parse-value
243 (/parse-simple-attribute (lambda (x) (or (number? x) (symbol? x)))
244 "improper integer attribute")
247 ; An enum attribute's value is a symbol representing that value.
250 <enum-attribute> 'parse-value
251 (/parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
252 "improper enum attribute")
255 ; Parse a boolean attribute's value definition.
258 <boolean-attribute> 'parse-value-def
259 (lambda (self context values)
260 (if (equal? values '(#f #t))
262 (parse-error context "boolean value list must be (#f #t)" values)))
265 ; Ignore values for strings. We can't do any error checking since
266 ; the default value is (#f #t).
269 <string-attribute> 'parse-value-def
270 (lambda (self context values) #f)
273 ; Parse a bitset attribute's value definition.
274 ; FIXME: treated as enum?
277 <bitset-attribute> 'parse-value-def
278 (lambda (self context values)
279 (parse-enum-vals context "" values))
282 ; Parse an integer attribute's value definition.
283 ; VALUES may be #f which means any value is ok.
286 <integer-attribute> 'parse-value-def
287 (lambda (self context values)
289 (for-each (lambda (val)
290 (if (or (not (list? val))
291 (not (number? (car val))))
293 "invalid element in integer attribute list"
299 ; Parse an enum attribute's value definition.
300 ; See parse-enum-vals for more info.
303 <enum-attribute> 'parse-value-def
304 (lambda (self context values)
305 (parse-enum-vals context "" values))
308 ; Make an attribute list object from a list of name/value pairs.
310 (define (atlist-make prefix . attrs) (make <attr-list> prefix attrs))
312 ; Parse an attribute definition.
313 ; This is the main routine for building an attribute object from a
314 ; description in the .cpu file.
315 ; All arguments are in raw (non-evaluated) form.
316 ; TYPE-CLASS is the class of the object to create.
317 ; i.e. one of <{boolean,bitset,integer,enum,string}-attribute>.
318 ; If DEFAULT is #f, use the first value.
319 ; ??? Allowable values for integer attributes is wip.
321 (define (/attr-parse context type-class name comment attrs for default values)
322 (logit 2 "Processing attribute " name " ...\n")
324 ;; Pick out name first to augment the error context.
325 (let* ((name (parse-name context name))
326 (context (context-append-name context name))
327 (result (new type-class))
328 (parsed-values (send result 'parse-value-def context values)))
330 (elm-xset! result 'name name)
331 (elm-xset! result 'comment (parse-comment context comment))
332 (elm-xset! result 'attrs (atlist-parse context attrs ""))
333 (elm-xset! result 'for for)
335 (case (class-name type-class)
336 ((<boolean-attribute>)
337 (if (and (not (memq default '(#f #t)))
338 (not (rtx? default)))
339 (parse-error context "invalid default" default))
340 (elm-xset! result 'default default))
341 ((<string-attribute>)
342 (let ((default (or default "")))
343 (if (and (not (string? default))
344 (not (rtx? default)))
345 (parse-error context "invalid default" default))
346 (elm-xset! result 'default default)))
347 ((<integer-attribute>)
348 (let ((default (if default default (if (null? values) 0 (car values)))))
349 (if (and (not (integer? default))
350 (not (rtx? default)))
351 (parse-error context "invalid default" default))
352 (elm-xset! result 'default default)))
353 ((<bitset-attribute> <enum-attribute>)
354 (let ((default (if default default (caar parsed-values))))
355 (if (and (not (assq default parsed-values))
356 (not (rtx? default)))
357 (parse-error context "invalid default" default))
358 (elm-xset! result 'default default))))
359 (elm-xset! result 'values parsed-values)
364 ; Read an attribute description
365 ; This is the main routine for analyzing attributes in the .cpu file.
366 ; CONTEXT is a <context> object for error messages.
367 ; ARG-LIST is an associative list of field name and field value.
368 ; /attr-parse is invoked to create the attribute object.
370 (define (/attr-read context . arg-list)
372 (type-class 'not-set) ; attribute type
376 (for #f) ; assume for everything
377 (default #f) ; #f indicates "not set"
378 (values #f) ; #f indicates "not set"
381 ; Loop over each element in ARG-LIST, recording what's found.
382 (let loop ((arg-list arg-list))
385 (let ((arg (car arg-list))
386 (elm-name (caar arg-list)))
388 ((type) (set! type-class (case (cadr arg)
389 ((boolean) <boolean-attribute>)
390 ((string) <string-attribute>)
391 ((bitset) <bitset-attribute>)
392 ((integer) <integer-attribute>)
393 ((enum) <enum-attribute>)
396 "invalid attribute type"
398 ((name) (set! name (cadr arg)))
399 ((comment) (set! comment (cadr arg)))
400 ((attrs) (set! attrs (cdr arg)))
401 ((for) (set! for (cdr arg)))
402 ((default) (set! default (cadr arg)))
403 ((values) (set! values (cdr arg)))
404 (else (parse-error context "invalid attribute arg" arg)))
405 (loop (cdr arg-list)))))
407 ; Must have type now.
408 (if (eq? type-class 'not-set)
409 (parse-error context "type not specified") arg-list)
410 ; Establish proper defaults now that we know the type.
411 (case (class-name type-class)
412 ((<boolean-attribute>)
414 (set! default #f)) ; really a nop, but for consistency
416 (set! values '(#f #t))))
417 ((bitset-attribute>) ;; FIXME
419 (parse-error context "bitset-attribute default not specified"
422 (parse-error context "bitset-attribute values not specified"
424 ((integer-attribute>) ;; FIXME
428 (set! values #f))) ; really a nop, but for consistency
429 ((enum-attribute>) ;; FIXME
431 (parse-error context "enum-attribute default not specified"
434 (parse-error context "bitset-attribute values not specified"
438 ; Now that we've identified the elements, build the object.
439 (/attr-parse context type-class name comment attrs for default values))
442 ; Main routines for defining attributes in .cpu files.
446 (let ((a (apply /attr-read (cons (make-current-context "define-attr")
448 (current-attr-add! a)
454 ; Lookup ATTR-NAME in ATTR-LIST.
455 ; The result is the object or #f if not found.
457 (define (attr-lookup attr-name attr-list)
458 (object-assq attr-name attr-list)
461 ; Return a boolean indicating if boolean attribute ATTR is "true" in
462 ; attribute alist ALIST.
463 ; Note that if the attribute isn't present, it is defined to be #f.
466 <attr-list> 'has-attr?
468 (let ((a (assq attr (elm-get self 'attrs))))
470 ((boolean? (cdr a)) (cdr a))
471 (else (error "Not a boolean attribute:" attr)))))
474 (define (atlist-has-attr? atlist attr)
475 (send atlist 'has-attr? attr)
478 ; Return a boolean indicating if attribute ATTR is present in
479 ; attribute alist ALIST.
482 <attr-list> 'attr-present?
484 (->bool (assq attr (elm-get self 'attrs))))
487 (define (atlist-attr-present? atlist attr)
488 (send atlist 'attr-present? attr)
491 ; Expand attribute value ATVAL, which is an rtx expression.
492 ; OWNER is the containing object or #f if there is none.
493 ; OWNER is needed if an attribute is defined in terms of other attributes.
494 ; If it's #f obviously ATVAL can't be defined in terms of others.
496 (define (/attr-eval atval owner)
497 (let* ((estate (estate-make-for-eval #f owner))
498 (expr (rtx-simplify #f owner (rtx-canonicalize #f 'DFLT atval nil) nil))
499 (value (rtx-eval-with-estate expr DFLT estate)))
500 (cond ((symbol? value) value)
501 ((number? value) value)
502 (error "/attr-eval: internal error, unsupported result:" value)))
505 ; Return value of ATTR in attribute alist ALIST.
506 ; If not present, return the default value.
507 ; OWNER is the containing object or #f if there is none.
509 (define (attr-value alist attr owner)
510 (let ((a (assq-ref alist attr)))
512 (if (pair? a) ; pair? -> cheap non-null-list?
515 (attr-lookup-default attr owner)))
518 ; Return the value of ATTR in ATLIST.
519 ; OWNER is the containing object or #f if there is none.
521 (define (atlist-attr-value atlist attr owner)
522 (attr-value (atlist-attrs atlist) attr owner)
525 ; Same as atlist-attr-value but return nil if attribute not present.
527 (define (atlist-attr-value-no-default atlist attr owner)
528 (let ((a (assq-ref (atlist-attrs atlist) attr)))
530 (if (pair? a) ; pair? -> cheap non-null-list?
536 ; Return the default for attribute A.
537 ; If A isn't a non-boolean attribute, we assume it's a boolean one, and
538 ; return #f (??? for backward's compatibility, to be removed in time).
539 ; OWNER is the containing object or #f if there is none.
541 (define (attr-lookup-default a owner)
542 (let ((at (current-attr-lookup a)))
546 (let ((deflt (attr-default at)))
548 (if (pair? deflt) ; pair? -> cheap non-null-list?
549 (/attr-eval deflt owner)
551 ; If no default was provided, use the first value.
552 (caar (attr-values at)))))
556 ; Return a boolean indicating if X is present in BITSET.
557 ; Bitset values are recorded as val1,val2,....
559 (define (bitset-attr-member? x bitset)
560 (->bool (memq x (bitset-attr->list bitset)))
563 ; Routines for accessing attributes in objects.
565 ; Get/set attributes of OBJ.
566 ; OBJ is any object which supports the get-atlist message.
568 (define (obj-atlist obj)
569 (let ((result (send obj 'get-atlist)))
570 ; As a speed up, we allow objects to specify an empty attribute list
571 ; with #f or (), rather than creating an attr-list object.
572 ; ??? There is atlist-empty now which should be used directly.
573 (if (or (null? result) (not result))
578 (define (obj-set-atlist! obj attrs) (send obj 'set-atlist! attrs))
580 ; Add attribute ATTR to OBJ.
581 ; The attribute is prepended to the front so it overrides any existing
584 (define (obj-cons-attr! obj attr)
585 (obj-set-atlist! obj (atlist-cons attr (obj-atlist obj)))
588 ; Add attribute list ATLIST to OBJ.
589 ; Attributes in ATLIST override existing values, so ATLIST is "prepended".
591 (define (obj-prepend-atlist! obj atlist)
592 ; Must have same prefix.
593 (assert (equal? (atlist-prefix (obj-atlist obj))
594 (atlist-prefix atlist)))
595 (obj-set-atlist! obj (atlist-append atlist (obj-atlist obj)))
598 ; Return boolean of whether OBJ has boolean attribute ATTR or not.
599 ; OBJ is any object that supports attributes.
601 (define (obj-has-attr? obj attr)
602 (atlist-has-attr? (obj-atlist obj) attr)
605 ; FIXME: for backward compatibility. Delete in time.
606 (define has-attr? obj-has-attr?)
608 ; Return a boolean indicating if attribute ATTR is present in OBJ.
610 (define (obj-attr-present? obj attr)
611 (atlist-attr-present? (obj-atlist obj) attr)
614 ; Return value of attribute ATTR in OBJ.
615 ; If the attribute isn't present, the default is returned.
616 ; OBJ is any object that supports the get-atlist method.
618 (define (obj-attr-value obj attr)
619 (let ((atlist (obj-atlist obj)))
620 (atlist-attr-value atlist attr obj))
623 ; Return boolean of whether OBJ has attribute ATTR value VALUE or not.
624 ; OBJ is any object that supports attributes.
625 ; NOTE: The default value of the attribute IS considered.
627 (define (obj-has-attr-value? obj attr value)
628 (let ((a (obj-attr-value obj attr)))
632 ; Return boolean of whether OBJ explicitly has attribute ATTR value VALUE
634 ; OBJ is any object that supports attributes.
635 ; NOTE: The default value of the attribute IS NOT considered.
637 (define (obj-has-attr-value-no-default? obj attr value)
638 (let* ((atlist (obj-atlist obj))
639 (objs-value (atlist-attr-value-no-default atlist attr obj)))
640 (and (not (null? objs-value)) (eq? value objs-value)))
645 ; Convert a bitset value "a,b,c" into a list (a b c).
647 (define (bitset-attr->list x)
648 (map string->symbol (string-cut (->string x) #\,))
651 ; Generate a list representing a bit mask of the indices of 'values'
652 ; within 'all-values'. Each element in the resulting list represents a byte.
653 ; Both bits and bytes are indexed from left to right starting at 0
654 ; with 8 bits in a byte.
655 (define (charmask-bytes values all-values vec-length)
656 (logit 3 "charmask-bytes for " values " " all-values "\n")
657 (let ((result (make-vector vec-length 0))
658 (indices (map (lambda (name)
659 (list-ref (map cadr all-values)
660 (element-lookup-index name (map car all-values) 0)))
662 (logit 3 "indices: " indices "\n")
663 (for-each (lambda (x)
664 (let* ((byteno (quotient x 8))
665 (bitno (- 7 (remainder x 8)))
666 (byteval (logior (vector-ref result byteno)
668 (vector-set! result byteno byteval)))
670 (logit 3 "result: " (vector->list result) "\n")
671 (vector->list result))
674 ; Convert a bitset value into a bit string based on the
675 ; index of each member in values
676 (define (bitset-attr->charmask value values)
677 (let* ((values-names (map car values))
678 (values-values (map cadr values))
679 (vec-length (+ 1 (quotient (apply max values-values) 8))))
680 (string-append "{ " (number->string vec-length) ", \""
681 (string-map (lambda (x)
682 (string-append "\\x" (number->hex x)))
683 (charmask-bytes (bitset-attr->list value)
687 ; Return the enum of ATTR-NAME for type TYPE.
688 ; TYPE is one of 'ifld, 'hw, 'operand, 'insn.
690 (define (gen-attr-enum type attr-name)
691 (string-upcase (string-append "CGEN_" type "_" (gen-sym attr-name)))
694 ; Return a list of enum value definitions for gen-enum-decl.
695 ; Attributes numbers are organized as follows: booleans are numbered 0-31.
696 ; The range is because that's what fits in a portable int. Unused numbers
697 ; are left unused. Non-booleans are numbered starting at 32.
698 ; An alternative is start numbering the booleans at 32. The generated code
699 ; is simpler with the current way (the "- 32" to get back the bit number or
700 ; array index number occurs less often).
702 ; Three special values are created:
703 ; END-BOOLS - mark end of boolean attributes
704 ; END-NBOOLS - mark end of non-boolean attributes
705 ; START-NBOOLS - marks the start of the non-boolean attributes
706 ; (needed in case first non-bool is sanytized out).
708 ; ATTR-OBJ-LIST is a list of <attribute> objects (always subclassed of course).
710 (define (attr-list-enum-list attr-obj-list)
711 (let ((sorted-attrs (/attr-sort (attr-remove-meta-attrs attr-obj-list))))
712 (assert (<= (length (car sorted-attrs)) 32))
714 (map (lambda (bool-attr)
715 (list (obj:name bool-attr) '-
716 (atlist-attrs (obj-atlist bool-attr))))
719 (list '(START-NBOOLS 31))
720 (map (lambda (nbool-attr)
721 (list (obj:name nbool-attr) '-
722 (atlist-attrs (obj-atlist nbool-attr))))
728 ; Sort an alist of attributes so non-boolean attributes are at the front.
729 ; This is used to sort a particular object's attributes.
730 ; This is required by the C support code (cgen.h:CGEN_ATTR_VALUE).
731 ; Boolean attributes appear as (NAME . #t/#f), non-boolean ones appear as
732 ; (NAME . VALUE). Attributes of the same type are sorted by name.
734 (define (/attr-sort-alist alist)
737 ;(display (list a b "\n"))
738 (cond ((and (boolean? (cdr a)) (boolean? (cdr b)))
739 (string<? (symbol->string (car a)) (symbol->string (car b))))
740 ((boolean? (cdr a)) #f) ; we know b is non-bool here
741 ((boolean? (cdr b)) #t) ; we know a is non-bool here
742 (else (string<? (symbol->string (car a))
743 (symbol->string (car b)))))))
746 ; Sort ATTR-LIST into two lists: bools and non-bools.
747 ; The car of the result is the bools, the cdr is the non-bools.
748 ; Attributes requiring a fixed index have the INDEX attribute,
749 ; and used for the few special attributes that are refered to by
750 ; architecture independent code.
751 ; For each of non-bools and bools, put attributes with the INDEX attribute
752 ; first. This is used to sort a list of attributes for output (e.g. define
755 ; FIXME: Record index number with the INDEX attribute and sort on it.
756 ; At present it's just a boolean.
758 (define (/attr-sort attr-list)
759 (let loop ((fixed-non-bools nil)
760 (non-fixed-non-bools nil)
762 (non-fixed-bools nil)
763 (attr-list attr-list))
764 (cond ((null? attr-list)
765 (cons (append! (reverse! fixed-bools)
766 (reverse! non-fixed-bools))
767 (append! (reverse! fixed-non-bools)
768 (reverse! non-fixed-non-bools))))
769 ((bool-attr? (car attr-list))
770 (if (obj-has-attr? (car attr-list) 'INDEX)
771 (loop fixed-non-bools non-fixed-non-bools
772 (cons (car attr-list) fixed-bools) non-fixed-bools
774 (loop fixed-non-bools non-fixed-non-bools
775 fixed-bools (cons (car attr-list) non-fixed-bools)
778 (if (obj-has-attr? (car attr-list) 'INDEX)
779 (loop (cons (car attr-list) fixed-non-bools) non-fixed-non-bools
780 fixed-bools non-fixed-bools
782 (loop fixed-non-bools (cons (car attr-list) non-fixed-non-bools)
783 fixed-bools non-fixed-bools
787 ; Return number of non-bools in attributes ATLIST.
789 (define (attr-count-non-bools atlist)
790 (count-true (map (lambda (a) (not (bool-attr? a)))
794 ; Given an alist of attributes, return the non-bools.
796 (define (attr-non-bool-attrs alist)
797 (let loop ((result nil) (alist alist))
798 (cond ((null? alist) (reverse! result))
799 ((boolean? (cdar alist)) (loop result (cdr alist)))
800 (else (loop (cons (car alist) result) (cdr alist)))))
803 ; Given an alist of attributes, return the bools.
805 (define (attr-bool-attrs alist)
806 (let loop ((result nil) (alist alist))
807 (cond ((null? alist) (reverse! result))
808 ((boolean? (cdar alist))
809 (loop (cons (car alist) result) (cdr alist)))
810 (else (loop result (cdr alist)))))
813 ; Parse an attribute spec.
814 ; CONTEXT is a <context> object or #f if there is none.
815 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
816 ; The result is the attribute alist.
818 (define (attr-parse context attrs)
819 (if (not (list? attrs))
820 (parse-error context "improper attribute list" attrs))
822 (for-each (lambda (elm)
825 (if (char=? (string-ref (symbol->string elm) 0) #\!)
826 (set! alist (acons (string->symbol (string-drop1 (symbol->string elm))) #f alist))
827 (set! alist (acons elm #t alist)))
828 (if (not (current-attr-lookup (caar alist)))
829 (parse-error context "unknown attribute" (caar alist))))
830 ((and (list? elm) (pair? elm) (symbol? (car elm)))
831 (let ((a (current-attr-lookup (car elm))))
833 (parse-error context "unknown attribute" elm))
834 (set! alist (cons (send a 'parse-value
837 (else (parse-error context "improper attribute" elm))))
842 ; Parse an object attribute spec.
843 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
844 ; The result is an <attr-list> object.
846 (define (atlist-parse context attrs prefix)
847 (make <attr-list> prefix (attr-parse context attrs))
850 ; Return the source form of an atlist's values.
851 ; Externally attributes are ((name1 value1) (name2 value2) ...).
852 ; Internally they are ((name1 . value1) (name2 . value2) ...).
854 (define (atlist-source-form atlist)
856 (list (car attr) (cdr attr)))
857 (atlist-attrs atlist))
860 ; Cons an attribute to an attribute list to create a new attribute list.
861 ; ATLIST is either an attr-list object or #f or () (both of the latter two
862 ; signify an empty attribute list, in which case we make the prefix of the
865 (define (atlist-cons attr atlist)
866 (if (or (not atlist) (null? atlist))
867 (make <attr-list> "" (cons attr nil))
868 (make <attr-list> (atlist-prefix atlist) (cons attr (atlist-attrs atlist))))
871 ; Append one attribute list to another.
872 ; The prefix for the new atlist is taken from the first one.
874 (define (atlist-append attr-list1 attr-list2)
876 (atlist-prefix attr-list1)
877 (append (atlist-attrs attr-list1) (atlist-attrs attr-list2)))
880 ; Remove meta-attributes from ALIST.
881 ; "meta" may be the wrong adjective to use here.
882 ; The attributes in question are not intended to appear in generated files.
883 ; They started out being attributes of attributes, hence the name "meta".
885 (define (attr-remove-meta-attrs-alist alist)
886 (let ((all-attrs (current-attr-list)))
887 ; FIXME: Why not use find?
888 (let loop ((result nil) (alist alist))
891 (let ((attr (attr-lookup (caar alist) all-attrs)))
892 (if (and attr (has-attr? attr 'META))
893 (loop result (cdr alist))
894 (loop (cons (car alist) result) (cdr alist)))))))
897 ; Remove meta-attributes from ATTR-LIST.
898 ; "meta" may be the wrong adjective to use here.
899 ; The attributes in question are not intended to appear in generated files.
900 ; They started out being attributes of attributes, hence the name "meta".
902 (define (attr-remove-meta-attrs attr-list)
903 ; FIXME: Why not use find?
904 (let loop ((result nil) (attr-list attr-list))
905 (cond ((null? attr-list)
907 ((has-attr? (car attr-list) 'META)
908 (loop result (cdr attr-list)))
910 (loop (cons (car attr-list) result) (cdr attr-list)))))
913 ; Remove duplicates from ATTRS, a list of attributes.
914 ; Attribute lists are typically small so we use a simple O^2 algorithm.
915 ; The leading entry of an attribute overrides subsequent ones so this is
916 ; defined to pick the first entry of each attribute.
918 (define (attr-nub attrs)
919 (let loop ((result nil) (attrs attrs))
920 (cond ((null? attrs) (reverse! result))
921 ((assq (caar attrs) result) (loop result (cdr attrs)))
922 (else (loop (cons (car attrs) result) (cdr attrs)))))
925 ; Return a list of all attrs in TABLE-LIST, a list of lists of arbitrary
926 ; elements. A list of lists is passed to simplify computation of insn
927 ; attributes where the insns and macro-insns are on separate lists and
928 ; appending them into one list would be unnecessarily expensive.
929 ; ACCESSOR is a function to access the attrs field from TABLE-LIST.
930 ; Duplicates are eliminated and the list is sorted so non-boolean attributes
931 ; are at the front (required by the C code that fetches attribute values).
932 ; STD-ATTRS is an `attr-list' object of attrs that are always available.
933 ; The actual values returned are random (e.g. #t vs #f). We could
935 ; The result is an alist of all the attributes that are used in TABLE-LIST.
936 ; ??? The cdr of each element is some random value. Perhaps it should be
937 ; the default value or perhaps we should just return a list of names.
938 ; ??? No longer used.
940 (define (attr-compute-all table-list accessor std-attrs)
941 (let ((accessor (lambda (elm) (atlist-attrs (accessor elm)))))
942 (attr-remove-meta-attrs-alist
947 (map (lambda (table-elm)
951 (let ((attrs (accessor e)))
952 (not (null? attrs))))
955 (atlist-attrs std-attrs))))))
958 ; Return lists of attributes for particular object types.
959 ; FIXME: The output shouldn't be required to be sorted.
961 (define (current-attr-list-for type)
962 (let ((sorted (/attr-sort (find (lambda (a)
964 (memq type (atlist-for a))
966 (attr-remove-meta-attrs
967 (current-attr-list))))))
968 ; Current behaviour puts the non-bools at the front.
969 (append! (cdr sorted) (car sorted)))
971 (define (current-ifld-attr-list)
972 (current-attr-list-for 'ifield)
974 (define (current-hw-attr-list)
975 (current-attr-list-for 'hardware)
977 (define (current-op-attr-list)
978 (current-attr-list-for 'operand)
980 (define (current-insn-attr-list)
981 (current-attr-list-for 'insn)
984 ; Methods to emit the C value of an attribute.
985 ; These don't _really_ belong here (C code doesn't belong in the appl'n
986 ; independent part of CGEN), but there isn't a better place for them
987 ; (maybe utils-cgen.scm?) and there's only a few of them.
990 <boolean-attribute> 'gen-value-for-defn-raw
995 ;(string-upcase (string-append (obj:str-name self) "_" value)))
999 <boolean-attribute> 'gen-value-for-defn
1000 (lambda (self value)
1001 (send self 'gen-value-for-defn-raw value))
1005 <bitset-attribute> 'gen-value-for-defn-raw
1006 (lambda (self value)
1007 (if (string=? (string-downcase (gen-sym self)) "isa")
1008 (bitset-attr->charmask value (elm-get self 'values))
1011 (string-map (lambda (x)
1012 (string-append "|(1<<"
1014 "_" (gen-c-symbol x) ")"))
1015 (bitset-attr->list value)))))
1020 <bitset-attribute> 'gen-value-for-defn
1021 (lambda (self value)
1024 (if (string=? (string-downcase (gen-sym self)) "isa")
1025 (bitset-attr->charmask value (elm-get self 'values))
1030 (string-map (lambda (x)
1031 (string-append "|(1<<"
1033 "_" (gen-c-symbol x) ")"))
1034 (bitset-attr->list value))))
1041 <integer-attribute> 'gen-value-for-defn-raw
1042 (lambda (self value)
1043 (number->string value)
1048 <integer-attribute> 'gen-value-for-defn
1049 (lambda (self value)
1052 (send self 'gen-value-for-defn-raw value)
1058 <enum-attribute> 'gen-value-for-defn-raw
1059 (lambda (self value)
1061 (gen-c-symbol (string-append (obj:str-name self)
1063 (symbol->string value))))
1068 <enum-attribute> 'gen-value-for-defn
1069 (lambda (self value)
1072 (send self 'gen-value-for-defn-raw value)
1077 ;; Doesn't handle escape sequences.
1079 <string-attribute> 'gen-value-for-defn-raw
1080 (lambda (self value)
1081 (string-append "\"" value "\""))
1085 <string-attribute> 'gen-value-for-defn
1086 (lambda (self value)
1087 (send self 'gen-value-for-defn-raw value))
1091 ; Called before loading a .cpu file to initialize.
1093 (define (attr-init!)
1095 (reader-add-command! 'define-attr
1097 Define an attribute, name/value pair list version.
1099 nil 'arg-list define-attr)
1104 ; Called before a . cpu file is read in to install any builtins.
1105 ; One thing this does is define all attributes requiring a fixed index,
1106 ; keeping them all in one place.
1107 ; ??? Perhaps it would make sense to define all predefined attributes here.
1109 (define (attr-builtin!)
1110 (define-attr '(type boolean) '(name VIRTUAL) '(comment "virtual object"))
1112 ; The meta attribute is used for attributes that aren't to appear in
1113 ; generated output (need a better name).
1114 (define-attr '(for attr) '(type boolean) '(name META))
1116 ; Objects to keep local to a generated file.
1117 (define-attr '(for keyword) '(type boolean) '(name PRIVATE))
1119 ; Attributes requiring fixed indices.
1120 (define-attr '(for attr) '(type boolean) '(name INDEX) '(attrs META))
1122 ; ALIAS is used for instructions that are aliases of more general insns.
1123 ; ALIAS insns are ignored by the simulator.
1124 (define-attr '(for insn) '(type boolean) '(name ALIAS)
1125 '(comment "insn is an alias of another")
1131 ; Called after loading a .cpu file to perform any post-processing required.
1133 (define (attr-finish!)