OSDN Git Service

(-attr-read): Add some fixmes.
[pf3gnuchains/pf3gnuchains3x.git] / cgen / attr.scm
1 ; Attributes.
2 ; Copyright (C) 2000, 2003, 2005, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4 ; See file COPYING.CGEN for details.
5
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.
9 ;
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
13 ;
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).
23 ;
24 ; Example:
25 ; (FOO1 !FOO2 (BAR 3) (FOO3 X) (MACH sparc,sparclite))
26 ;
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 ...)).
31 ;
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).
34 ;
35 ; ??? May wish to allow specifying enum attributes by only having to
36 ; specify the value (move names into "enum space" or some such).
37 \f
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.
44
45 (define <attr-list> (class-make '<attr-list> nil '(prefix attrs) nil))
46
47 (define atlist-prefix (elm-make-getter <attr-list> 'prefix))
48 (define atlist-attrs (elm-make-getter <attr-list> 'attrs))
49
50 (define (atlist? x) (class-instance? <attr-list> x))
51
52 ; An empty attribute-list.
53
54 (define atlist-empty (make <attr-list> "" nil))
55
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].
63
64 (define <attribute>
65   (class-make '<attribute>
66               '(<ident>)
67               '(
68                 ; List of object types this attribute is for.
69                 ; Possible element values are:
70                 ; attr, enum, cpu, mach, model, ifield, hardware, operand,
71                 ; insn
72                 ; A value of #f means the attribute is for everything.
73                 for
74                 )
75               nil)
76 )
77
78 ; Accessors.
79
80 (define atlist-for (elm-make-getter <attribute> 'for))
81
82 ; A class for each type of attribute.
83
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.
86
87 (define <boolean-attribute>
88   (class-make '<boolean-attribute>
89               '(<attribute>)
90               '(default values)
91               nil)
92 )
93
94 ; VALUES is ignored for string-attribute.
95
96 (define <string-attribute>
97   (class-make '<string-attribute>
98               '(<attribute>)
99               '(default values)
100               nil)
101 )
102
103 ; For bitset attributes VALUES is a list of
104 ; (symbol bit-number-or-#f attr-list comment-or-#f),
105 ; one for each bit.
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.
112
113 (define <bitset-attribute>
114   (class-make '<bitset-attribute>
115               '(<attribute>)
116               '(default values)
117               nil)
118 )
119
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.
124
125 (define <integer-attribute>
126   (class-make '<integer-attribute>
127               '(<attribute>)
128               '(default values)
129               nil)
130 )
131
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.
139
140 (define <enum-attribute>
141   (class-make '<enum-attribute>
142               '(<attribute>)
143               '(default values)
144               nil)
145 )
146
147 ; Return a boolean indicating if X is a <boolean-attribute> object.
148
149 (define (bool-attr? x) (class-instance? <boolean-attribute> x))
150
151 ; Return a boolean indicating if X is a <bitset-attribute> object.
152
153 (define (bitset-attr? x) (class-instance? <bitset-attribute> x))
154
155 ; Return a symbol indicating the kind of attribute ATTR is.
156 ; The result is one of boolean,integer,enum,bitset or string.
157
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))))
167 )
168
169 ; Accessors.
170
171 (define (attr-default attr) (elm-xget attr 'default))
172 (define (attr-values attr) (elm-xget attr 'values))
173
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.
179
180 ; VALUE must be #f or #t.
181
182 (define (bool-attr-make name value) (cons name value))
183
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")
187
188 (define (bitset-attr-make name values) (cons name values))
189
190 ; VALUE must be a number (or maybe a symbol).
191
192 (define (int-attr-make name value) (cons name value))
193
194 ; VALUE must be a symbol.
195
196 (define (enum-attr-make name value) (cons name value))
197
198 (define (parse-simple-attribute right-type? message)
199   (lambda (self errtxt val)
200     (if (and (not (null? val))
201              (right-type? (car val))
202              (null? (cdr val)))
203         (cons (obj:name self) (car val))
204         (parse-error errtxt message (cons (obj:name self) val))))
205 )
206
207 ; A boolean attribute's value is either #t or #f.
208
209 (method-make!
210  <boolean-attribute> 'parse-value
211  (parse-simple-attribute boolean? "boolean attribute not one of #f/#t")
212 )
213
214 (method-make!
215  <string-attribute> 'parse-value
216  (parse-simple-attribute string? "invalid argument to string attribute"))
217
218 ; A bitset attribute's value is a comma separated list of elements.
219 ; We don't validate the values.  In the case of the MACH attribute,
220 ; there's no current mechanism to create it after all define-mach's have
221 ; been read in.
222 ; ??? Need to decide whether all define-mach's must appear before any
223 ; define-insn's.  It would be nice to be able to spread an architecture's
224 ; description over several .cpu files.
225 ; ??? On the other hand, all machs are specified in define-arch.
226 ; Perhaps creation of builtins could be defered until then.
227
228 (method-make!
229  <bitset-attribute> 'parse-value
230  (parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
231                          "improper bitset attribute")
232 )
233
234 ; An integer attribute's value is a number
235 ; (or maybe a symbol representing that value).
236
237 (method-make!
238  <integer-attribute> 'parse-value
239  (parse-simple-attribute (lambda (x) (or (number? x) (symbol? x)))
240                          "improper integer attribute")
241 )
242
243 ; An enum attribute's value is a symbol representing that value.
244
245 (method-make!
246  <enum-attribute> 'parse-value
247  (parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
248                          "improper enum attribute")
249 )
250
251 ; Parse a boolean attribute's value definition.
252
253 (method-make!
254  <boolean-attribute> 'parse-value-def
255  (lambda (self errtxt values)
256    (if (equal? values '(#f #t))
257        values
258        (parse-error errtxt "boolean value list must be (#f #t)" values)))
259 )
260
261 ; Ignore values for strings.  We can't do any error checking since
262 ; the default value is (#f #t).
263
264 (method-make!
265  <string-attribute> 'parse-value-def
266  (lambda (self errtxt values) #f)
267 )
268
269 ; Parse a bitset attribute's value definition.
270 ; FIXME: treated as enum?
271
272 (method-make!
273  <bitset-attribute> 'parse-value-def
274  (lambda (self errtxt values)
275    (parse-enum-vals errtxt "" values))
276 )
277
278 ; Parse an integer attribute's value definition.
279 ; VALUES may be #f which means any value is ok.
280
281 (method-make!
282  <integer-attribute> 'parse-value-def
283  (lambda (self errtxt values)
284    (if values
285        (for-each (lambda (val)
286                    (if (or (not (list? val))
287                            (not (number? (car val))))
288                        (parse-error errtxt "invalid element in integer attribute list" val)))
289                  values))
290    values)
291 )
292
293 ; Parse an enum attribute's value definition.
294 ; See parse-enum-vals for more info.
295
296 (method-make!
297  <enum-attribute> 'parse-value-def
298  (lambda (self errtxt values)
299    (parse-enum-vals errtxt "" values))
300 )
301
302 ; Make an attribute list object from a list of name/value pairs.
303
304 (define (atlist-make prefix . attrs) (make <attr-list> prefix attrs))
305 \f
306 ; Parse an attribute definition.
307 ; This is the main routine for building an attribute object from a
308 ; description in the .cpu file.
309 ; All arguments are in raw (non-evaluated) form.
310 ; TYPE-CLASS is the class of the object to create.
311 ; i.e. one of <{boolean,bitset,integer,enum,string}-attribute>.
312 ; If DEFAULT is #f, use the first value.
313 ; ??? Allowable values for integer attributes is wip.
314
315 (define (-attr-parse errtxt type-class name comment attrs for default values)
316   (logit 2 "Processing attribute " name " ...\n")
317   (let* ((name (parse-name name errtxt))
318          (errtxt (stringsym-append errtxt ":" name))
319          (result (new type-class))
320          (parsed-values (send result 'parse-value-def errtxt values)))
321     (elm-xset! result 'name name)
322     (elm-xset! result 'comment (parse-comment comment errtxt))
323     (elm-xset! result 'attrs (atlist-parse attrs "" errtxt))
324     (elm-xset! result 'for for)
325     ; Set the default.
326     (case (class-name type-class)
327       ((<boolean-attribute>)
328        (if (and (not (memq default '(#f #t)))
329                 (not (rtx? default)))
330            (parse-error errtxt "invalid default" default))
331        (elm-xset! result 'default default))
332       ((<string-attribute>)
333        (let ((default (or default "")))
334          (if (and (not (string? default))
335                   (not (rtx? default)))
336              (parse-error errtxt "invalid default" default))
337          (elm-xset! result 'default default)))
338       ((<integer-attribute>)
339        (let ((default (if default default (if (null? values) 0 (car values)))))
340          (if (and (not (integer? default))
341                   (not (rtx? default)))
342              (parse-error errtxt "invalid default" default))
343          (elm-xset! result 'default default)))
344       ((<bitset-attribute> <enum-attribute>)
345        (let ((default (if default default (caar parsed-values))))
346          (if (and (not (assq default parsed-values))
347                   (not (rtx? default)))
348              (parse-error errtxt "invalid default" default))
349          (elm-xset! result 'default default))))
350     (elm-xset! result 'values parsed-values)
351     result)
352 )
353
354 ; Read an attribute description
355 ; This is the main routine for analyzing attributes in the .cpu file.
356 ; ERRTXT is prepended to error messages to provide context.
357 ; ARG-LIST is an associative list of field name and field value.
358 ; -attr-parse is invoked to create the attribute object.
359
360 (define (-attr-read errtxt . arg-list)
361   (let (; Current attribute elements:
362         (type-class 'not-set) ; attribute type
363         (name nil)
364         (comment "")
365         (attrs nil)
366         (for #f) ; assume for everything
367         (default #f) ; indicates "not set"
368         (values #f) ; indicates "not set"
369         )
370     ; Loop over each element in ARG-LIST, recording what's found.
371     (let loop ((arg-list arg-list))
372       (if (null? arg-list)
373           nil
374           (let ((arg (car arg-list))
375                 (elm-name (caar arg-list)))
376             (case elm-name
377               ((type) (set! type-class (case (cadr arg)
378                                         ((boolean) <boolean-attribute>)
379                                         ((string) <string-attribute>)
380                                         ((bitset) <bitset-attribute>)
381                                         ((integer) <integer-attribute>)
382                                         ((enum) <enum-attribute>)
383                                         (else (parse-error
384                                                errtxt
385                                                "invalid attribute type"
386                                                (cadr arg))))))
387               ((name) (set! name (cadr arg)))
388               ((comment) (set! comment (cadr arg)))
389               ((attrs) (set! attrs (cdr arg)))
390               ((for) (set! for (cdr arg)))
391               ((default) (set! default (cadr arg)))
392               ((values) (set! values (cdr arg)))
393               (else (parse-error errtxt "invalid attribute arg" arg)))
394             (loop (cdr arg-list)))))
395     ; Must have type now.
396     (if (eq? type-class 'not-set)
397         (parse-error errtxt "type not specified"))
398     ; Establish proper defaults now that we know the type.
399     (case (class-name type-class)
400       ((<boolean-attribute>)
401        (if (eq? default #f)
402            (set! default #f)) ; really a nop, but for consistency
403        (if (eq? values #f)
404            (set! values '(#f #t))))
405       ((bitset-attribute>) ;; FIXME
406        (if (eq? default #f)
407            (parse-error errtxt "bitset-attribute default not specified"))
408        (if (eq? values #f)
409            (parse-error errtxt "bitset-attribute values not specified")))
410       ((integer-attribute>) ;; FIXME
411        (if (eq? default #f)
412            (set! default 0))
413        (if (eq? values #f)
414            (set! values #f))) ; really a nop, but for consistency
415       ((enum-attribute>) ;; FIXME
416        (if (eq? default #f)
417            (parse-error errtxt "enum-attribute default not specified"))
418        (if (eq? values #f)
419            (parse-error errtxt "bitset-attribute values not specified")))
420       )
421     ; Now that we've identified the elements, build the object.
422     (-attr-parse errtxt type-class name comment attrs for default values)
423     )
424 )
425
426 ; Main routines for defining attributes in .cpu files.
427
428 (define define-attr
429   (lambda arg-list
430     (let ((a (apply -attr-read (cons "define-attr" arg-list))))
431       (current-attr-add! a)
432       a))
433 )
434 \f
435 ; Query routines.
436
437 ; Lookup ATTR-NAME in ATTR-LIST.
438 ; The result is the object or #f if not found.
439
440 (define (attr-lookup attr-name attr-list)
441   (object-assq attr-name attr-list)
442 )
443
444 ; Return a boolean indicating if boolean attribute ATTR is "true" in
445 ; attribute alist ALIST.
446 ; Note that if the attribute isn't present, it is defined to be #f.
447
448 (method-make!
449  <attr-list> 'has-attr?
450  (lambda (self attr)
451    (let ((a (assq attr (elm-get self 'attrs))))
452      (cond ((not a) a)
453            ((boolean? (cdr a)) (cdr a))
454            (else (error "Not a boolean attribute:" attr)))))
455 )
456
457 (define (atlist-has-attr? atlist attr)
458   (send atlist 'has-attr? attr)
459 )
460
461 ; Return a boolean indicating if attribute ATTR is present in
462 ; attribute alist ALIST.
463
464 (method-make!
465  <attr-list> 'attr-present?
466  (lambda (self attr)
467    (->bool (assq attr (elm-get self 'attrs))))
468 )
469
470 (define (atlist-attr-present? atlist attr)
471   (send atlist 'attr-present? attr)
472 )
473
474 ; Expand attribute value ATVAL, which is an rtx expression.
475 ; OWNER is the containing object or #f if there is none.
476 ; OWNER is needed if an attribute is defined in terms of other attributes.
477 ; If it's #f obviously ATVAL can't be defined in terms of others.
478
479 (define (-attr-eval atval owner)
480   (let* ((estate (estate-make-for-eval #f owner))
481          (expr (rtx-compile #f (rtx-simplify #f owner atval nil) nil))
482          (value (rtx-eval-with-estate expr 'DFLT estate)))
483     (cond ((symbol? value) value)
484           ((number? value) value)
485           (error "-attr-eval: internal error, unsupported result:" value)))
486 )
487
488 ; Return value of ATTR in attribute alist ALIST.
489 ; If not present, return the default value.
490 ; OWNER is the containing object or #f if there is none.
491
492 (define (attr-value alist attr owner)
493   (let ((a (assq-ref alist attr)))
494     (if a
495         (if (pair? a) ; pair? -> cheap non-null-list?
496             (-attr-eval a owner)
497             a)
498         (attr-lookup-default attr owner)))
499 )
500
501 ; Return the value of ATTR in ATLIST.
502 ; OWNER is the containing object or #f if there is none.
503
504 (define (atlist-attr-value atlist attr owner)
505   (attr-value (atlist-attrs atlist) attr owner)
506 )
507
508 ; Same as atlist-attr-value but return nil if attribute not present.
509
510 (define (atlist-attr-value-no-default atlist attr owner)
511   (let ((a (assq-ref (atlist-attrs atlist) attr)))
512     (if a
513         (if (pair? a) ; pair? -> cheap non-null-list?
514             (-attr-eval a owner)
515             a)
516         nil))
517 )
518
519 ; Return the default for attribute A.
520 ; If A isn't a non-boolean attribute, we assume it's a boolean one, and
521 ; return #f (??? for backward's compatibility, to be removed in time).
522 ; OWNER is the containing object or #f if there is none.
523
524 (define (attr-lookup-default a owner)
525   (let ((at (current-attr-lookup a)))
526     (if at
527         (if (bool-attr? at)
528             #f
529             (let ((deflt (attr-default at)))
530               (if deflt
531                   (if (pair? deflt) ; pair? -> cheap non-null-list?
532                       (-attr-eval deflt owner)
533                       deflt)
534                   ; If no default was provided, use the first value.
535                   (caar (attr-values at)))))
536         #f))
537 )
538
539 ; Return a boolean indicating if X is present in BITSET.
540 ; Bitset values are recorded as val1,val2,....
541
542 (define (bitset-attr-member? x bitset)
543   (->bool (memq x (bitset-attr->list bitset)))
544 )
545 \f
546 ; Routines for accessing attributes in objects.
547
548 ; Get/set attributes of OBJ.
549 ; OBJ is any object which supports the get-atlist message.
550
551 (define (obj-atlist obj)
552   (let ((result (send obj 'get-atlist)))
553     ; As a speed up, we allow objects to specify an empty attribute list
554     ; with #f or (), rather than creating an attr-list object.
555     ; ??? There is atlist-empty now which should be used directly.
556     (if (or (null? result) (not result))
557         atlist-empty
558         result))
559 )
560
561 (define (obj-set-atlist! obj attrs) (send obj 'set-atlist! attrs))
562
563 ; Add attribute ATTR to OBJ.
564 ; The attribute is prepended to the front so it overrides any existing
565 ; definition.
566
567 (define (obj-cons-attr! obj attr)
568   (obj-set-atlist! obj (atlist-cons attr (obj-atlist obj)))
569 )
570
571 ; Add attribute list ATLIST to OBJ.
572 ; Attributes in ATLIST override existing values, so ATLIST is "prepended".
573
574 (define (obj-prepend-atlist! obj atlist)
575   ; Must have same prefix.
576   (assert (equal? (atlist-prefix (obj-atlist obj))
577                   (atlist-prefix atlist)))
578   (obj-set-atlist! obj (atlist-append atlist (obj-atlist obj)))
579 )
580
581 ; Return boolean of whether OBJ has boolean attribute ATTR or not.
582 ; OBJ is any object that supports attributes.
583
584 (define (obj-has-attr? obj attr)
585   (atlist-has-attr? (obj-atlist obj) attr)
586 )
587
588 ; FIXME: for backward compatibility.  Delete in time.
589 (define has-attr? obj-has-attr?)
590
591 ; Return a boolean indicating if attribute ATTR is present in OBJ.
592
593 (define (obj-attr-present? obj attr)
594   (atlist-attr-present? (obj-atlist obj) attr)
595 )
596
597 ; Return value of attribute ATTR in OBJ.
598 ; If the attribute isn't present, the default is returned.
599 ; OBJ is any object that supports the get-atlist method.
600
601 (define (obj-attr-value obj attr)
602   (let ((atlist (obj-atlist obj)))
603     (atlist-attr-value atlist attr obj))
604 )
605
606 ; Return boolean of whether OBJ has attribute ATTR value VALUE or not.
607 ; OBJ is any object that supports attributes.
608 ; NOTE: The default value of the attribute IS considered.
609
610 (define (obj-has-attr-value? obj attr value)
611   (let ((a (obj-attr-value obj attr)))
612     (eq? a value))
613 )
614
615 ; Return boolean of whether OBJ explicitly has attribute ATTR value VALUE
616 ; or not.
617 ; OBJ is any object that supports attributes.
618 ; NOTE: The default value of the attribute IS NOT considered.
619
620 (define (obj-has-attr-value-no-default? obj attr value)
621   (let* ((atlist (obj-atlist obj))
622          (objs-value (atlist-attr-value-no-default atlist attr obj)))
623     (and (not (null? objs-value)) (eq? value objs-value)))
624 )
625 \f
626 ; Utilities.
627
628 ; Convert a bitset value "a,b,c" into a list (a b c).
629
630 (define (bitset-attr->list x)
631   (map string->symbol (string-cut (->string x) #\,))
632 )
633
634 ; Generate a list representing a bit mask of the indices of 'values'
635 ; within 'all-values'. Each element in the resulting list represents a byte.
636 ; Both bits and bytes are indexed from left to right starting at 0
637 ; with 8 bits in a byte.
638 (define (charmask-bytes values all-values vec-length)
639   (logit 3 "charmask-bytes for " values " " all-values "\n")
640   (let ((result (make-vector vec-length 0))
641         (indices (map (lambda (name)
642                         (list-ref (map cadr all-values)
643                                   (element-lookup-index name (map car all-values) 0)))
644                       values)))
645     (logit 3 "indices: " indices "\n")
646     (for-each (lambda (x)
647                 (let* ((byteno (quotient x 8))
648                        (bitno (- 7 (remainder x 8)))
649                        (byteval (logior (vector-ref result byteno)
650                                         (ash 1 bitno))))
651                   (vector-set! result byteno byteval)))
652               indices)
653     (logit 3 "result: " (vector->list result) "\n")
654     (vector->list result))
655 )
656
657 ; Convert a bitset value into a bit string based on the
658 ; index of each member in values
659 (define (bitset-attr->charmask value values)
660   (let* ((values-names (map car values))
661          (values-values (map cadr values))
662          (vec-length (+ 1 (quotient (apply max values-values) 8))))
663     (string-append "{ " (number->string vec-length) ", \""
664                    (string-map (lambda (x)
665                                  (string-append "\\x" (number->hex x)))
666                                (charmask-bytes (bitset-attr->list value)
667                                                values vec-length))
668                    "\" }"))
669 )
670 ; Return the enum of ATTR-NAME for type TYPE.
671 ; TYPE is one of 'ifld, 'hw, 'operand, 'insn.
672
673 (define (gen-attr-enum type attr-name)
674   (string-upcase (string-append "CGEN_" type "_" (gen-sym attr-name)))
675 )
676
677 ; Return a list of enum value definitions for gen-enum-decl.
678 ; Attributes numbers are organized as follows: booleans are numbered 0-31.
679 ; The range is because that's what fits in a portable int.  Unused numbers
680 ; are left unused.  Non-booleans are numbered starting at 32.
681 ; An alternative is start numbering the booleans at 32.  The generated code
682 ; is simpler with the current way (the "- 32" to get back the bit number or
683 ; array index number occurs less often).
684 ;
685 ; Three special values are created:
686 ; END-BOOLS - mark end of boolean attributes
687 ; END-NBOOLS - mark end of non-boolean attributes
688 ; START-NBOOLS - marks the start of the non-boolean attributes
689 ; (needed in case first non-bool is sanytized out).
690 ;
691 ; ATTR-OBJ-LIST is a list of <attribute> objects (always subclassed of course).
692
693 (define (attr-list-enum-list attr-obj-list)
694   (let ((sorted-attrs (-attr-sort (attr-remove-meta-attrs attr-obj-list))))
695     (assert (<= (length (car sorted-attrs)) 32))
696     (append!
697      (map (lambda (bool-attr)
698             (list (obj:name bool-attr) '-
699                   (atlist-attrs (obj-atlist bool-attr))))
700           (car sorted-attrs))
701      (list '(END-BOOLS))
702      (list '(START-NBOOLS 31))
703      (map (lambda (nbool-attr)
704             (list (obj:name nbool-attr) '-
705                   (atlist-attrs (obj-atlist nbool-attr))))
706           (cdr sorted-attrs))
707      (list '(END-NBOOLS))
708      ))
709 )
710
711 ; Sort an alist of attributes so non-boolean attributes are at the front.
712 ; This is used to sort a particular object's attributes.
713 ; This is required by the C support code (cgen.h:CGEN_ATTR_VALUE).
714 ; Boolean attributes appear as (NAME . #t/#f), non-boolean ones appear as
715 ; (NAME . VALUE).  Attributes of the same type are sorted by name.
716
717 (define (-attr-sort-alist alist)
718   (sort alist
719         (lambda (a b)
720           ;(display (list a b "\n"))
721           (cond ((and (boolean? (cdr a)) (boolean? (cdr b)))
722                  (string<? (symbol->string (car a)) (symbol->string (car b))))
723                 ((boolean? (cdr a)) #f) ; we know b is non-bool here
724                 ((boolean? (cdr b)) #t) ; we know a is non-bool here
725                 (else (string<? (symbol->string (car a))
726                                 (symbol->string (car b)))))))
727 )
728
729 ; Sort ATTR-LIST into two lists: bools and non-bools.
730 ; The car of the result is the bools, the cdr is the non-bools.
731 ; Attributes requiring a fixed index have the INDEX attribute,
732 ; and used for the few special attributes that are refered to by
733 ; architecture independent code.
734 ; For each of non-bools and bools, put attributes with the INDEX attribute
735 ; first.  This is used to sort a list of attributes for output (e.g. define
736 ; the attr enum).
737 ;
738 ; FIXME: Record index number with the INDEX attribute and sort on it.
739 ; At present it's just a boolean.
740
741 (define (-attr-sort attr-list)
742   (let loop ((fixed-non-bools nil)
743              (non-fixed-non-bools nil)
744              (fixed-bools nil)
745              (non-fixed-bools nil)
746              (attr-list attr-list))
747     (cond ((null? attr-list)
748            (cons (append! (reverse! fixed-bools)
749                           (reverse! non-fixed-bools))
750                  (append! (reverse! fixed-non-bools)
751                           (reverse! non-fixed-non-bools))))
752           ((bool-attr? (car attr-list))
753            (if (obj-has-attr? (car attr-list) 'INDEX)
754                (loop fixed-non-bools non-fixed-non-bools
755                      (cons (car attr-list) fixed-bools) non-fixed-bools
756                      (cdr attr-list))
757                (loop fixed-non-bools non-fixed-non-bools
758                      fixed-bools (cons (car attr-list) non-fixed-bools)
759                      (cdr attr-list))))
760           (else
761            (if (obj-has-attr? (car attr-list) 'INDEX)
762                (loop (cons (car attr-list) fixed-non-bools) non-fixed-non-bools
763                      fixed-bools non-fixed-bools
764                      (cdr attr-list))
765                (loop fixed-non-bools (cons (car attr-list) non-fixed-non-bools)
766                      fixed-bools non-fixed-bools
767                      (cdr attr-list))))))
768 )
769
770 ; Return number of non-bools in attributes ATLIST.
771
772 (define (attr-count-non-bools atlist)
773   (count-true (map (lambda (a) (not (bool-attr? a)))
774                    atlist))
775 )
776
777 ; Given an alist of attributes, return the non-bools.
778
779 (define (attr-non-bool-attrs alist)
780   (let loop ((result nil) (alist alist))
781     (cond ((null? alist) (reverse! result))
782           ((boolean? (cdar alist)) (loop result (cdr alist)))
783           (else (loop (cons (car alist) result) (cdr alist)))))
784 )
785
786 ; Given an alist of attributes, return the bools.
787
788 (define (attr-bool-attrs alist)
789   (let loop ((result nil) (alist alist))
790     (cond ((null? alist) (reverse! result))
791           ((boolean? (cdar alist))
792            (loop (cons (car alist) result) (cdr alist)))
793           (else (loop result (cdr alist)))))
794 )
795
796 ; Parse an attribute spec.
797 ; CONTEXT is a <context> object or #f if there is none.
798 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
799 ; The result is the attribute alist.
800
801 (define (attr-parse context attrs)
802   (if (not (list? attrs))
803       (context-error context "improper attribute list" attrs))
804   (let ((alist nil))
805     (for-each (lambda (elm)
806                 (cond ((symbol? elm)
807                        ; boolean attribute
808                        (if (char=? (string-ref (symbol->string elm) 0) #\!)
809                            (set! alist (acons (string->symbol (string-drop1 (symbol->string elm))) #f alist))
810                            (set! alist (acons elm #t alist)))
811                        (if (not (current-attr-lookup (caar alist)))
812                            (context-error context "unknown attribute" (caar alist))))
813                       ((and (list? elm) (pair? elm) (symbol? (car elm)))
814                        (let ((a (current-attr-lookup (car elm))))
815                          (if (not a)
816                              (context-error context "unknown attribute" elm))
817                          (set! alist (cons (send a 'parse-value
818                                                  (context-prefix context);FIXME
819                                                  (cdr elm)) alist))))
820                       (else (context-error context "improper attribute" elm))))
821               attrs)
822     alist)
823 )
824
825 ; Parse an object attribute spec.
826 ; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
827 ; The result is an <attr-list> object.
828
829 (define (atlist-parse attrs prefix errtxt)
830   (make <attr-list> prefix (attr-parse (context-make-prefix errtxt) attrs))
831 )
832
833 ; Return the source form of an atlist's values.
834 ; Externally attributes are ((name1 value1) (name2 value2) ...).
835 ; Internally they are ((name1 . value1) (name2 . value2) ...).
836
837 (define (atlist-source-form atlist)
838   (map (lambda (attr)
839          (list (car attr) (cdr attr)))
840        (atlist-attrs atlist))
841 )
842
843 ; Cons an attribute to an attribute list to create a new attribute list.
844 ; ATLIST is either an attr-list object or #f or () (both of the latter two
845 ; signify an empty attribute list, in which case we make the prefix of the
846 ; result "").
847
848 (define (atlist-cons attr atlist)
849   (if (or (not atlist) (null? atlist))
850       (make <attr-list> "" (cons attr nil))
851       (make <attr-list> (atlist-prefix atlist) (cons attr (atlist-attrs atlist))))
852 )
853
854 ; Append one attribute list to another.
855 ; The prefix for the new atlist is taken from the first one.
856
857 (define (atlist-append attr-list1 attr-list2)
858   (make <attr-list>
859         (atlist-prefix attr-list1)
860         (append (atlist-attrs attr-list1) (atlist-attrs attr-list2)))
861 )
862
863 ; Remove meta-attributes from ALIST.
864 ; "meta" may be the wrong adjective to use here.
865 ; The attributes in question are not intended to appear in generated files.
866 ; They started out being attributes of attributes, hence the name "meta".
867
868 (define (attr-remove-meta-attrs-alist alist)
869   (let ((all-attrs (current-attr-list)))
870     ; FIXME: Why not use find?
871     (let loop ((result nil) (alist alist))
872       (if (null? alist)
873           (reverse! result)
874           (let ((attr (attr-lookup (caar alist) all-attrs)))
875             (if (and attr (has-attr? attr 'META))
876                 (loop result (cdr alist))
877                 (loop (cons (car alist) result) (cdr alist)))))))
878 )
879
880 ; Remove meta-attributes from ATTR-LIST.
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".
884
885 (define (attr-remove-meta-attrs attr-list)
886   ; FIXME: Why not use find?
887   (let loop ((result nil) (attr-list attr-list))
888     (cond ((null? attr-list)
889            (reverse! result))
890           ((has-attr? (car attr-list) 'META)
891            (loop result (cdr attr-list)))
892           (else
893            (loop (cons (car attr-list) result) (cdr attr-list)))))
894 )
895
896 ; Remove duplicates from ATTRS, a list of attributes.
897 ; Attribute lists are typically small so we use a simple O^2 algorithm.
898 ; The leading entry of an attribute overrides subsequent ones so this is
899 ; defined to pick the first entry of each attribute.
900
901 (define (attr-nub attrs)
902   (let loop ((result nil) (attrs attrs))
903     (cond ((null? attrs) (reverse! result))
904           ((assq (caar attrs) result) (loop result (cdr attrs)))
905           (else (loop (cons (car attrs) result) (cdr attrs)))))
906 )
907
908 ; Return a list of all attrs in TABLE-LIST, a list of lists of arbitrary
909 ; elements.   A list of lists is passed to simplify computation of insn
910 ; attributes where the insns and macro-insns are on separate lists and
911 ; appending them into one list would be unnecessarily expensive.
912 ; ACCESSOR is a function to access the attrs field from TABLE-LIST.
913 ; Duplicates are eliminated and the list is sorted so non-boolean attributes
914 ; are at the front (required by the C code that fetches attribute values).
915 ; STD-ATTRS is an `attr-list' object of attrs that are always available.
916 ; The actual values returned are random (e.g. #t vs #f).  We could
917 ; canonicalize them.
918 ; The result is an alist of all the attributes that are used in TABLE-LIST.
919 ; ??? The cdr of each element is some random value.  Perhaps it should be
920 ; the default value or perhaps we should just return a list of names.
921 ; ??? No longer used.
922
923 (define (attr-compute-all table-list accessor std-attrs)
924   (let ((accessor (lambda (elm) (atlist-attrs (accessor elm)))))
925     (attr-remove-meta-attrs-alist
926      (attr-nub
927       (-attr-sort-alist
928        (append
929         (apply append
930                (map (lambda (table-elm)
931                       (apply append
932                              (find-apply accessor
933                                          (lambda (e)
934                                            (let ((attrs (accessor e)))
935                                              (not (null? attrs))))
936                                          table-elm)))
937                     table-list))
938         (atlist-attrs std-attrs))))))
939 )
940
941 ; Return lists of attributes for particular object types.
942 ; FIXME: The output shouldn't be required to be sorted.
943
944 (define (current-attr-list-for type)
945   (let ((sorted (-attr-sort (find (lambda (a)
946                                     (if (atlist-for a)
947                                         (memq type (atlist-for a))
948                                         #t))
949                                   (attr-remove-meta-attrs
950                                    (current-attr-list))))))
951     ; Current behaviour puts the non-bools at the front.
952     (append! (cdr sorted) (car sorted)))
953 )
954 (define (current-ifld-attr-list)
955   (current-attr-list-for 'ifield)
956 )
957 (define (current-hw-attr-list)
958   (current-attr-list-for 'hardware)
959 )
960 (define (current-op-attr-list)
961   (current-attr-list-for 'operand)
962 )
963 (define (current-insn-attr-list)
964   (current-attr-list-for 'insn)
965 )
966 \f
967 ; Methods to emit the C value of an attribute.
968 ; These don't _really_ belong here (C code doesn't belong in the appl'n
969 ; independent part of CGEN), but there isn't a better place for them
970 ; (maybe utils-cgen.scm?) and there's only a few of them.
971
972 (method-make!
973  <boolean-attribute> 'gen-value-for-defn-raw
974  (lambda (self value)
975    (if (not value)
976        "0"
977        "1"))
978  ;(string-upcase (string-append (obj:str-name self) "_" value)))
979 )
980
981 (method-make!
982  <boolean-attribute> 'gen-value-for-defn
983  (lambda (self value)
984    (send self 'gen-value-for-defn-raw value))
985 )
986
987 (method-make!
988  <bitset-attribute> 'gen-value-for-defn-raw
989  (lambda (self value)
990    (if (string=? (string-downcase (gen-sym self)) "isa")
991        (bitset-attr->charmask value (elm-get self 'values))
992        (string-drop1
993         (string-upcase
994          (string-map (lambda (x)
995                        (string-append "|(1<<"
996                                       (gen-sym self)
997                                       "_" (gen-c-symbol x) ")"))
998                      (bitset-attr->list value)))))
999  )
1000 )
1001
1002 (method-make!
1003  <bitset-attribute> 'gen-value-for-defn
1004  (lambda (self value)
1005    (string-append
1006     "{ "
1007     (if (string=? (string-downcase (gen-sym self)) "isa")
1008         (bitset-attr->charmask value (elm-get self 'values))
1009         (string-append
1010          "{ "
1011          (string-drop1
1012           (string-upcase
1013            (string-map (lambda (x)
1014                          (string-append "|(1<<"
1015                                         (gen-sym self)
1016                                         "_" (gen-c-symbol x) ")"))
1017                        (bitset-attr->list value))))
1018          ", 0 }"))
1019     " }")
1020  )
1021 )
1022
1023 (method-make!
1024  <integer-attribute> 'gen-value-for-defn-raw
1025  (lambda (self value)
1026    (number->string value)
1027  )
1028 )
1029
1030 (method-make!
1031  <integer-attribute> 'gen-value-for-defn
1032  (lambda (self value)
1033    (string-append
1034     "{ { "
1035     (send self 'gen-value-for-defn-raw value)
1036     ", 0 } }")
1037  )
1038 )
1039
1040 (method-make!
1041  <enum-attribute> 'gen-value-for-defn-raw
1042  (lambda (self value)
1043    (string-upcase
1044     (gen-c-symbol (string-append (obj:str-name self)
1045                                  "_"
1046                                  (symbol->string value))))
1047  )
1048 )
1049
1050 (method-make!
1051  <enum-attribute> 'gen-value-for-defn
1052  (lambda (self value)
1053    (string-append
1054     "{ { "
1055      (send self 'gen-value-for-defn-raw value)
1056      ", 0 } }")
1057  )
1058 )
1059
1060 ;; Doesn't handle escape sequences.
1061 (method-make!
1062  <string-attribute> 'gen-value-for-defn-raw
1063  (lambda (self value)
1064    (string-append "\"" value "\""))
1065 )
1066
1067 (method-make!
1068  <string-attribute> 'gen-value-for-defn
1069  (lambda (self value)
1070    (send self 'gen-value-for-defn-raw value))
1071 )
1072
1073 \f
1074 ; Called before loading a .cpu file to initialize.
1075
1076 (define (attr-init!)
1077
1078   (reader-add-command! 'define-attr
1079                        "\
1080 Define an attribute, name/value pair list version.
1081 "
1082                        nil 'arg-list define-attr)
1083
1084   *UNSPECIFIED*
1085 )
1086
1087 ; Called before a . cpu file is read in to install any builtins.
1088 ; One thing this does is define all attributes requiring a fixed index,
1089 ; keeping them all in one place.
1090 ; ??? Perhaps it would make sense to define all predefined attributes here.
1091
1092 (define (attr-builtin!)
1093   (define-attr '(type boolean) '(name VIRTUAL) '(comment "virtual object"))
1094
1095   ; The meta attribute is used for attributes that aren't to appear in
1096   ; generated output (need a better name).
1097   (define-attr '(for attr) '(type boolean) '(name META))
1098
1099   ; Objects to keep local to a generated file.
1100   (define-attr '(for keyword) '(type boolean) '(name PRIVATE))
1101
1102   ; Attributes requiring fixed indices.
1103   (define-attr '(for attr) '(type boolean) '(name INDEX) '(attrs META))
1104
1105   ; ALIAS is used for instructions that are aliases of more general insns.
1106   ; ALIAS insns are ignored by the simulator.
1107   (define-attr '(for insn) '(type boolean) '(name ALIAS)
1108     '(comment "insn is an alias of another")
1109     '(attrs INDEX))
1110
1111   *UNSPECIFIED*
1112 )
1113
1114 ; Called after loading a .cpu file to perform any post-processing required.
1115
1116 (define (attr-finish!)
1117   *UNSPECIFIED*
1118 )