-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr.scm
1230 lines (1046 loc) · 39 KB
/
attr.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Attributes.
; Copyright (C) 2000, 2003, 2005, 2009 Red Hat, Inc.
; This file is part of CGEN.
; See file COPYING.CGEN for details.
; There are 5 kinds of attributes: boolean, integer, enum, bitset, and string.
; Boolean attributes are really enum attributes with two possible values,
; but they occur frequently enough that they are special cased.
; String attributes are intentionally not documented in the manual as
; being supported - they're still a bit of work-in-progress.
;
; All objects that use attributes must have two methods:
; - 'get-atlist - returns the object's attr-list
; - 'set-atlist! - set the object's attr-list
;
; In .cpu files, attribute lists are associative lists of (NAME VALUE).
; Boolean attributes are specified as (NAME #t) or (NAME #f),
; but for convenience ATTR and !ATTR are also supported.
; integer/enum attrs are specified as (ATTR value).
; string attrs are specified as (ATTR "value").
; Bitset attrs are specified as (ATTR val1 val2 val3), each value must be
; a valid Scheme symbol (stick with valid C symbols + "-" and you'll be fine).
; For backwards compatibility (ATTR val1,val2,val3) and
; (ATTR "val1,val2,val3") are also supported for bitset values.
; val1,val2,val3 is not portable (e.g. mzscheme will reject it).
; In all cases the value needn't be constant, and can be an expression,
; though expressions are currently only supported for META-attributes
; (attributes that don't appear in any generated code).
;
; Example:
; (FOO1 !FOO2 (BAR 3) (FOO3 X) (MACH sparc sparclite))
;
; ??? Implementation of expressions is being postponed as long
; as possible, avoiding adding complications for complication's sake, and
; because I'm not completely sure how I want to do them.
; The syntax for an expression value is (ATTR (rtx-func ...)).
;
; ??? May wish to allow a bitset attribute like (ATTR val1 !val2), where `!'
; means to turn off that particular bit (or bits if val2 refers to several).
;
; ??? May wish to allow specifying enum attributes by only having to
; specify the value (move names into "enum space" or some such).
; An attr-list (or "atlist") is a collection of attributes.
; Attributes are stored as an associative list.
; There is possible confusion between "alist" (associative-list) and
; "atlist" (attribute-list) but in practice I haven't had a problem.
; ??? May wish to change this to a list of objects, as the alist doesn't carry
; enough info. However the alist is simple and fast.
(define <attr-list> (class-make '<attr-list> nil '(prefix attrs) nil))
(define atlist-prefix (elm-make-getter <attr-list> 'prefix))
(define atlist-attrs (elm-make-getter <attr-list> 'attrs))
(define (atlist? x) (class-instance? <attr-list> x))
; An empty attribute-list.
(define atlist-empty (make <attr-list> "" nil))
; The attribute baseclass.
; The attributes of <ident> are the set of attributes for this attribute
; [meaning attributes themselves can have attributes].
; [Ya, that's clumsily written. I left it that way for fun.]
; An odd notion that is of some use. It's current raison d'etre is to
; support sanitization of attributes [which is implemented with the
; `sanitize' attribute].
(define <attribute>
(class-make '<attribute>
'(<ident>)
'(
; List of object types this attribute is for.
; Possible element values are:
; attr, enum, cpu, mach, model, ifield, hardware, operand,
; insn
; A value of #f means the attribute is for everything.
for
)
nil)
)
; Accessors.
(define atlist-for (elm-make-getter <attribute> 'for))
; A class for each type of attribute.
; `values' exists for boolean-attribute to simplify the code, it's ignored.
; Ditto for `default'. The default for boolean-attribute is always #f.
(define <boolean-attribute>
(class-make '<boolean-attribute>
'(<attribute>)
'(default values)
nil)
)
; VALUES is ignored for string-attribute.
(define <string-attribute>
(class-make '<string-attribute>
'(<attribute>)
'(default values)
nil)
)
; For bitset attributes VALUES is a list of
; (symbol bit-number-or-#f attr-list comment-or-#f),
; one for each bit.
; If bit-number is #f (unspecified), cgen will choose.
; Int's are used to record the bitset in the generated code so there's a limit
; of 32 elements, though there's nothing inherent in the description language
; that precludes removing the limit.
; NOTE: While one might want to record each element as an object, there's
; currently no need for the added complexity.
(define <bitset-attribute>
(class-make '<bitset-attribute>
'(<attribute>)
'(default values)
nil)
)
; For integer attributes VALUES is a list of (int),
; one for each possible value,
; or the empty list of all values are permissible.
; Note that each element is itself a list. This is for consistency.
(define <integer-attribute>
(class-make '<integer-attribute>
'(<attribute>)
'(default values)
nil)
)
; For enum attributes VALUES is a list of
; (symbol enum-value-or-#f attr-list comment-or-#f),
; one for each possible.
; If enum-value is #f (unspecified) cgen will apply the standard rule for
; assigning enum values.
; NOTE: While one might want to record each element as an object, there's
; currently no need for the added complexity.
(define <enum-attribute>
(class-make '<enum-attribute>
'(<attribute>)
'(default values)
nil)
)
; Return a boolean indicating if X is a <boolean-attribute> object.
(define (bool-attr? x) (class-instance? <boolean-attribute> x))
; Return a symbol indicating the kind of attribute ATTR is.
; The result is one of boolean,integer,enum,bitset or string.
(define (attr-kind attr)
(case (object-class-name attr)
((<boolean-attribute>) 'boolean)
((<string-attribute>) 'string)
((<integer-attribute>) 'integer)
((<enum-attribute>) 'enum)
((<bitset-attribute>) 'bitset)
(else (error "attr-kind: internal error, not an attribute class"
(object-class-name attr))))
)
; Accessors.
(define (attr-default attr) (elm-xget attr 'default))
(define (attr-values attr) (elm-xget attr 'values))
; Create an attribute.
; Attributes are stored in attribute lists using the actual value
; rather than an object containing the value, so we only have to cons
; NAME and VALUE rather than building some object. This is for simplicity
; and speed. We try to incrementally complicate things, only as necessary.
; VALUE must be #f or #t.
(define (bool-attr-make name value) (cons name value))
; VALUES must be a list of symbols.
; E.g., (val1 val2) not val1,val2.
(define (bitset-attr-make name values) (cons name values))
; VALUE must be a number (or maybe a symbol).
(define (int-attr-make name value) (cons name value))
; VALUE must be a symbol.
(define (enum-attr-make name value) (cons name value))
;; Return a procedure to parse an attribute.
;; RIGHT-TYPE? is a procedure that verifies the value is the right type.
;; MESSAGE is printed if there is an error.
;; The result of the parsed attribute is (name . value).
(define (/parse-simple-attribute right-type? message)
(lambda (self context val)
(if (and (not (null? val))
(right-type? (car val))
(null? (cdr val)))
(cons (obj:name self) (car val))
(parse-error context message (cons (obj:name self) val))))
)
; A boolean attribute's value is either #t or #f.
(method-make!
<boolean-attribute> 'parse-value
(/parse-simple-attribute boolean? "boolean attribute not one of #f/#t")
)
(method-make!
<string-attribute> 'parse-value
(/parse-simple-attribute string? "invalid argument to string attribute"))
; A bitset attribute's value is a list of symbols.
; For backwards compatibility (ATTR val1,val2,val3) and
; (ATTR "val1,val2,val3") are also supported for bitset values.
; val1,val2,val3 is not portable (e.g. mzscheme will reject it).
;
; We don't validate the values. In the case of the MACH attribute,
; there's no current mechanism to create it after all define-mach's have
; been read in.
; ??? Need to decide whether all define-mach's must appear before any
; define-insn's. It would be nice to be able to spread an architecture's
; description over several .cpu files.
; ??? On the other hand, all machs are specified in define-arch.
; Perhaps creation of builtins could be defered until then.
(method-make!
<bitset-attribute> 'parse-value
(lambda (self context val)
(let ((value (if (and (= (length val) 1)
(or (symbol? (car val)) (string? (car val))))
(map string->symbol (string-cut (->string (car val)) #\,))
val))
(message "improper bitset attribute"))
;; NOTE: An empty list is ok.
(if (all-true? (map symbol? value))
(cons (obj:name self) value)
(parse-error context message (cons (obj:name self) val)))))
)
; An integer attribute's value is a number
; (or maybe a symbol representing that value).
(method-make!
<integer-attribute> 'parse-value
(/parse-simple-attribute (lambda (x) (or (number? x) (symbol? x)))
"improper integer attribute")
)
; An enum attribute's value is a symbol representing that value.
(method-make!
<enum-attribute> 'parse-value
(/parse-simple-attribute (lambda (x) (or (symbol? x) (string? x)))
"improper enum attribute")
)
; Parse a boolean attribute's value definition.
(method-make!
<boolean-attribute> 'parse-value-def
(lambda (self context values)
(if (equal? values '(#f #t))
values
(parse-error context "boolean value list must be (#f #t)" values)))
)
; Ignore values for strings.
; They're not supported and /attr-read catches this.
(method-make!
<string-attribute> 'parse-value-def
(lambda (self context values) #f)
)
; Parse a bitset attribute's value definition.
(method-make!
<bitset-attribute> 'parse-value-def
(lambda (self context values)
;; parse-enum-vals works well enough
(parse-enum-vals context "" values))
)
; Parse an integer attribute's value definition.
; VALUES may be #f which means any value is ok.
; A fixed set of VALUES is work-in-progress.
(method-make!
<integer-attribute> 'parse-value-def
(lambda (self context values)
(if values
(for-each (lambda (val)
;; A list entry is for providing a sanitization key.
(if (or (not (list? val))
(not (number? (car val))))
(parse-error context
"invalid element in integer attribute list"
val)))
values))
values)
)
; Parse an enum attribute's value definition.
; See parse-enum-vals for more info.
(method-make!
<enum-attribute> 'parse-value-def
(lambda (self context values)
(parse-enum-vals context "" values))
)
; Make an attribute list object from a list of name/value pairs.
(define (atlist-make prefix . attrs) (make <attr-list> prefix attrs))
; Parse an attribute definition.
; This is the main routine for building an attribute object from a
; description in the .cpu file.
; All arguments are in raw (non-evaluated) form.
; TYPE-CLASS is the class of the object to create.
; i.e. one of <{boolean,bitset,integer,enum,string}-attribute>.
; For enum attributes, if DEFAULT is #f use the first value.
; For all other attribute kinds, we use what /attr-read gives us.
; ??? Allowable values for integer attributes is wip,
; for now it is the portable set of integers (int32_t).
(define (/attr-parse context type-class name comment attrs for default values)
(logit 2 "Processing attribute " name " ...\n")
;; Pick out name first to augment the error context.
(let* ((name (parse-name context name))
(context (context-append-name context name))
(result (new type-class))
(parsed-values (send result 'parse-value-def context values)))
(elm-xset! result 'name name)
(elm-xset! result 'comment (parse-comment context comment))
(elm-xset! result 'attrs (atlist-parse context attrs ""))
(elm-xset! result 'for for)
;; Set the default.
;; FIXME: Clean up with /attr-read.
(case (class-name type-class)
((<boolean-attribute>)
;; ??? docs say default must be #f, but we want to allow an rtx to
;; specify the default.
(if (and (not (memq default '(#f #t)))
(not (/attr-val-is-rtx? default)))
(parse-error context "invalid default" default))
(elm-xset! result 'default default))
((<string-attribute>)
(let ((default (or default "")))
(if (and (not (string? default))
(not (/attr-val-is-rtx? default)))
(parse-error context "invalid default" default))
(elm-xset! result 'default default)))
((<integer-attribute>)
(let ((default (if default default (if (null? values) 0 (car values)))))
(if (and (not (integer? default))
(not (/attr-val-is-rtx? default)))
(parse-error context "invalid default" default))
(elm-xset! result 'default default)))
((<enum-attribute>)
(let ((default (if default default (caar parsed-values))))
(if (and (not (assq default parsed-values))
(not (/attr-val-is-rtx? default)))
(parse-error context "invalid default" default))
(elm-xset! result 'default default)))
((<bitset-attribute>)
;; bitset attributes must specify a default, /attr-read catches this
(assert default)
;; It's also /attr-read's job to ensure it is a list.
(assert (list? default))
(let ((default default))
;; NOTE: We don't allow an rtx for bitset attributes,
;; the rtl language currently doesn't support them.
(if (/attr-val-is-rtx? default)
(parse-error context "invalid default, rtx not supported for bitset" default))
(if (not (all-true? (map (lambda (v) (assq v parsed-values))
default)))
(parse-error context "invalid default" default))
(elm-xset! result 'default default))))
(elm-xset! result 'values parsed-values)
result)
)
; Read an attribute description
; This is the main routine for analyzing attributes in the .cpu file.
; CONTEXT is a <context> object for error messages.
; ARG-LIST is an associative list of field name and field value.
; /attr-parse is invoked to create the attribute object.
(define (/attr-read context . arg-list)
(let (
(type 'not-set) ;; attribute type
(type-class 'not-set) ;; attribute class
(name #f)
(comment "")
(attrs nil)
(for #f) ;; assume for everything
(default #f) ;; #f indicates "not set"
(values #f) ;; #f indicates "not set"
)
;; Loop over each element in ARG-LIST, recording what's found.
(let loop ((arg-list arg-list))
(if (null? arg-list)
nil
(let ((arg (car arg-list))
(elm-name (caar arg-list)))
(case elm-name
((type)
(set! type-class (case (cadr arg)
((boolean) <boolean-attribute>)
((string) <string-attribute>)
((bitset) <bitset-attribute>)
((integer) <integer-attribute>)
((enum) <enum-attribute>)
(else (parse-error
context
"invalid attribute type"
(cadr arg)))))
(set! type (cadr arg)))
((name) (set! name (cadr arg)))
((comment) (set! comment (cadr arg)))
((attrs) (set! attrs (cdr arg)))
((for) (set! for (cdr arg)))
((default) (set! default (cdr arg)))
((values) (set! values (cdr arg)))
(else (parse-error context "invalid attribute arg" arg)))
(loop (cdr arg-list)))))
;; Must have type now.
(if (eq? type-class 'not-set)
(parse-error context "type not specified") arg-list)
;; For scalar attributes, fix up the default.
(if (and default (memq type '(boolean string integer enum)))
(begin
(if (!= (length default) 1)
(parse-error context "invalid default" default))
;; Don't change rtx values.
(if (not (pair? (car default)))
(set! default (car default)))))
;; Establish proper defaults now that we know the type.
;; FIXME: Clean up with /attr-parse.
(case type
((boolean)
(if (eq? default #f)
(set! default #f)) ;; really a nop, but for consistency
(if (eq? values #f)
(set! values '(#f #t))))
((bitset) ;; FIXME
(if (eq? default #f)
(parse-error context "bitset attribute default not specified"
arg-list))
(if (eq? values #f)
(parse-error context "bitset attribute values not specified"
arg-list)))
((integer) ;; FIXME
(if (eq? default #f)
(set! default 0))
(if (eq? values #f)
(set! values #f))) ;; really a nop, but for consistency
((enum) ;; FIXME
;; There are some existing cases where no default is specified,
;; expecting that the first value is the default.
;; (if (eq? default #f)
;; (parse-error context "enum attribute default not specified"
;; arg-list))
(if (eq? values #f)
(parse-error context "enum attribute values not specified"
arg-list)))
((string)
(if (eq? default #f)
(set! default ""))
(if (not (eq? values #f))
(parse-error context "string attribute values specified"
arg-list)))
)
;; Now that we've identified the elements, build the object.
(/attr-parse context type-class name comment attrs for default values))
)
; Main routines for defining attributes in .cpu files.
(define define-attr
(lambda arg-list
(let ((a (apply /attr-read (cons (make-current-context "define-attr")
arg-list))))
(current-attr-add! a)
a))
)
; Query routines.
; Lookup ATTR-NAME in ATTR-LIST.
; The result is the object or #f if not found.
(define (attr-lookup attr-name attr-list)
(object-assq attr-name attr-list)
)
; Return a boolean indicating if boolean attribute ATTR is "true" in
; attribute alist ALIST.
; Note that if the attribute isn't present, it is defined to be #f.
(method-make!
<attr-list> 'has-attr?
(lambda (self attr)
(let ((a (assq attr (elm-get self 'attrs))))
(cond ((not a) a)
((boolean? (cdr a)) (cdr a))
(else (error "Not a boolean attribute:" attr)))))
)
(define (atlist-has-attr? atlist attr)
(send atlist 'has-attr? attr)
)
; Return a boolean indicating if attribute ATTR is present in
; attribute alist ALIST.
(method-make!
<attr-list> 'attr-present?
(lambda (self attr)
(->bool (assq attr (elm-get self 'attrs))))
)
(define (atlist-attr-present? atlist attr)
(send atlist 'attr-present? attr)
)
;; Return #t if attribute value VAL is an rtx expression.
;; RTXs in attributes are recorded as a list of one element
;; which is the rtx.
;; I.e., ((rtx foo bar)).
(define (/attr-val-is-rtx? val)
(and (pair? val)
(null? (cdr val))
(pair? (car val))) ;; pair? -> cheap non-null-list?
)
; Expand attribute value ATVAL, which is an rtx expression.
; OWNER is the containing object or #f if there is none.
; OWNER is needed if an attribute is defined in terms of other attributes.
; OWNER is also needed to get the ISA(s) in which to evaluate the expression.
; If it's #f obviously ATVAL can't be defined in terms of others,
; or refer to operands that require an ISA to disambiguate.
(define (/attr-eval atval owner)
(let* ((atval-expr (car atval))
(expr (rtx-simplify #f owner
(rtx-canonicalize #f 'DFLT
(and owner (obj-isa-list owner))
nil atval-expr)
nil))
(value (rtx-value expr owner)))
(cond ((symbol? value) value)
((number? value) value)
(error "/attr-eval: internal error, unsupported result:" value)))
)
; Return value of ATTR in attribute alist ALIST.
; If not present, return the default value.
; If ATTR is an unknown attribute, return #f.
; OWNER is the containing object or #f if there is none.
(define (attr-value alist attr owner)
(let ((a (assq-ref alist attr)))
(if a
(if (/attr-val-is-rtx? a)
(/attr-eval a owner)
a)
(attr-lookup-default attr owner)))
)
; Return the value of ATTR in ATLIST.
; If not present, return the default value.
; If ATTR is an unknown attribute, return #f.
; OWNER is the containing object or #f if there is none.
(define (atlist-attr-value atlist attr owner)
(attr-value (atlist-attrs atlist) attr owner)
)
; Same as atlist-attr-value but return nil if attribute not present.
(define (atlist-attr-value-no-default atlist attr owner)
(let ((a (assq-ref (atlist-attrs atlist) attr)))
(if a
(if (/attr-val-is-rtx? a)
(/attr-eval a owner)
a)
nil))
)
; Return the default for attribute A.
;
; If A is unknown return #f.
; This means the caller can't distinguish booleans from unknowns,
; but the caller is left to deal with that.
;
; OWNER is the containing object or #f if there is none.
(define (attr-lookup-default a owner)
(let ((at (current-attr-lookup a)))
(if at
(if (bool-attr? at)
#f ;; FIXME: should fetch default from the attribute
(let ((deflt (attr-default at)))
(if deflt
(if (/attr-val-is-rtx? deflt)
(/attr-eval deflt owner)
deflt)
;; If no default was provided, use the first value.
;; FIXME: This shouldn't happen. /attr-parse should DTRT.
(caar (attr-values at)))))
#f))
)
; Return a boolean indicating if X is present in BITSET.
; Bitset values are recorded as (val1 val2 ...).
(define (bitset-attr-member? x bitset)
(->bool (memq x bitset))
)
; Routines for accessing attributes in objects.
; Get/set attributes of OBJ.
; OBJ is any object which supports the get-atlist message.
(define (obj-atlist obj)
(let ((result (send obj 'get-atlist)))
; As a speed up, we allow objects to specify an empty attribute list
; with #f or (), rather than creating an attr-list object.
; ??? There is atlist-empty now which should be used directly.
(if (or (null? result) (not result))
atlist-empty
result))
)
(define (obj-set-atlist! obj attrs) (send obj 'set-atlist! attrs))
; Add attribute ATTR to OBJ.
; The attribute is prepended to the front so it overrides any existing
; definition.
(define (obj-cons-attr! obj attr)
(obj-set-atlist! obj (atlist-cons attr (obj-atlist obj)))
)
; Add attribute list ATLIST to OBJ.
; Attributes in ATLIST override existing values, so ATLIST is "prepended".
(define (obj-prepend-atlist! obj atlist)
; Must have same prefix.
(assert (equal? (atlist-prefix (obj-atlist obj))
(atlist-prefix atlist)))
(obj-set-atlist! obj (atlist-append atlist (obj-atlist obj)))
)
; Return boolean of whether OBJ has boolean attribute ATTR or not.
; OBJ is any object that supports attributes.
(define (obj-has-attr? obj attr)
(atlist-has-attr? (obj-atlist obj) attr)
)
; FIXME: for backward compatibility. Delete in time.
(define has-attr? obj-has-attr?)
; Return a boolean indicating if attribute ATTR is present in OBJ.
(define (obj-attr-present? obj attr)
(atlist-attr-present? (obj-atlist obj) attr)
)
; Return value of attribute ATTR in OBJ.
; If the attribute isn't present, the default is returned.
; If ATTR is an unknown attribute, return #f.
; OBJ is any object that supports the get-atlist method.
(define (obj-attr-value obj attr)
(let ((atlist (obj-atlist obj)))
(atlist-attr-value atlist attr obj))
)
; Return boolean of whether OBJ has attribute ATTR value VALUE or not.
; OBJ is any object that supports attributes.
; NOTE: The default value of the attribute IS considered.
(define (obj-has-attr-value? obj attr value)
(let ((a (obj-attr-value obj attr)))
(eq? a value))
)
; Return boolean of whether OBJ explicitly has attribute ATTR value VALUE
; or not.
; OBJ is any object that supports attributes.
; NOTE: The default value of the attribute IS NOT considered.
(define (obj-has-attr-value-no-default? obj attr value)
(let* ((atlist (obj-atlist obj))
(objs-value (atlist-attr-value-no-default atlist attr obj)))
(and (not (null? objs-value)) (eq? value objs-value)))
)
; Utilities.
; Generate a list representing a bit mask of the indices of 'values'
; within 'all-values'. Each element in the resulting list represents a byte.
; Both bits and bytes are indexed from left to right starting at 0
; with 8 bits in a byte.
(define (charmask-bytes values all-values vec-length)
(logit 3 "charmask-bytes for " values " " all-values "\n")
(let ((result (make-vector vec-length 0))
(indices (map (lambda (name)
(list-ref (map cadr all-values)
(element-lookup-index name (map car all-values) 0)))
values)))
(logit 3 "indices: " indices "\n")
(for-each (lambda (x)
(let* ((byteno (quotient x 8))
(bitno (- 7 (remainder x 8)))
(byteval (logior (vector-ref result byteno)
(ash 1 bitno))))
(vector-set! result byteno byteval)))
indices)
(logit 3 "result: " (vector->list result) "\n")
(vector->list result))
)
; Convert a bitset value into a bit string based on the
; index of each member in values.
; VALUE is a list of symbols in the bitset.
; VALUES is the values member of the attribute's definition.
(define (/bitset-attr->charmask value values)
(let* ((values-names (map car values))
(values-values (map cadr values))
(vec-length (+ 1 (quotient (apply max values-values) 8))))
(string-append "{ " (number->string vec-length) ", \""
(string-map (lambda (x)
(string-append "\\x" (number->hex x)))
(charmask-bytes value values vec-length))
"\" }"))
)
; Return the enum of ATTR-NAME for type TYPE.
; TYPE is one of 'ifld, 'hw, 'operand, 'insn.
(define (gen-attr-enum type attr-name)
(string-upcase (string-append "CGEN_" type "_" (gen-sym attr-name)))
)
; Return a list of enum value definitions for gen-enum-decl.
; Attributes numbers are organized as follows: booleans are numbered 0-31.
; The range is because that's what fits in a portable int. Unused numbers
; are left unused. Non-booleans are numbered starting at 32.
; An alternative is start numbering the booleans at 32. The generated code
; is simpler with the current way (the "- 32" to get back the bit number or
; array index number occurs less often).
;
; Three special values are created:
; END-BOOLS - mark end of boolean attributes
; END-NBOOLS - mark end of non-boolean attributes
; START-NBOOLS - marks the start of the non-boolean attributes
; (needed in case first non-bool is sanytized out).
;
; ATTR-OBJ-LIST is a list of <attribute> objects (always subclassed of course).
(define (attr-list-enum-list attr-obj-list)
(let ((sorted-attrs (/attr-sort (attr-remove-meta-attrs attr-obj-list))))
(assert (<= (length (car sorted-attrs)) 32))
(append!
(map (lambda (bool-attr)
(list (obj:name bool-attr) '-
(atlist-attrs (obj-atlist bool-attr))))
(car sorted-attrs))
(list '(END-BOOLS))
(list '(START-NBOOLS 31))
(map (lambda (nbool-attr)
(list (obj:name nbool-attr) '-
(atlist-attrs (obj-atlist nbool-attr))))
(cdr sorted-attrs))
(list '(END-NBOOLS))
))
)
; Sort an alist of attributes so non-boolean attributes are at the front.
; This is used to sort a particular object's attributes.
; This is required by the C support code (cgen.h:CGEN_ATTR_VALUE).
; Boolean attributes appear as (NAME . #t/#f), non-boolean ones appear as
; (NAME . VALUE). Attributes of the same type are sorted by name.
(define (/attr-sort-alist alist)
(sort alist
(lambda (a b)
;(display (list a b "\n"))
(cond ((and (boolean? (cdr a)) (boolean? (cdr b)))
(string<? (symbol->string (car a)) (symbol->string (car b))))
((boolean? (cdr a)) #f) ; we know b is non-bool here
((boolean? (cdr b)) #t) ; we know a is non-bool here
(else (string<? (symbol->string (car a))
(symbol->string (car b)))))))
)
; Sort ATTR-LIST into two lists: bools and non-bools.
; The car of the result is the bools, the cdr is the non-bools.
; Attributes requiring a fixed index have the INDEX attribute,
; and used for the few special attributes that are refered to by
; architecture independent code.
; For each of non-bools and bools, put attributes with the INDEX attribute
; first. This is used to sort a list of attributes for output (e.g. define
; the attr enum).
;
; FIXME: Record index number with the INDEX attribute and sort on it.
; At present it's just a boolean.
(define (/attr-sort attr-list)
(let loop ((fixed-non-bools nil)
(non-fixed-non-bools nil)
(fixed-bools nil)
(non-fixed-bools nil)
(attr-list attr-list))
(cond ((null? attr-list)
(cons (append! (reverse! fixed-bools)
(reverse! non-fixed-bools))
(append! (reverse! fixed-non-bools)
(reverse! non-fixed-non-bools))))
((bool-attr? (car attr-list))
(if (obj-has-attr? (car attr-list) 'INDEX)
(loop fixed-non-bools non-fixed-non-bools
(cons (car attr-list) fixed-bools) non-fixed-bools
(cdr attr-list))
(loop fixed-non-bools non-fixed-non-bools
fixed-bools (cons (car attr-list) non-fixed-bools)
(cdr attr-list))))
(else
(if (obj-has-attr? (car attr-list) 'INDEX)
(loop (cons (car attr-list) fixed-non-bools) non-fixed-non-bools
fixed-bools non-fixed-bools
(cdr attr-list))
(loop fixed-non-bools (cons (car attr-list) non-fixed-non-bools)
fixed-bools non-fixed-bools
(cdr attr-list))))))
)
; Return number of non-bools in attributes ATLIST.
(define (attr-count-non-bools atlist)
(count-true (map (lambda (a) (not (bool-attr? a)))
atlist))
)
; Given an alist of attributes, return the non-bools.
(define (attr-non-bool-attrs alist)
(let loop ((result nil) (alist alist))
(cond ((null? alist) (reverse! result))
((boolean? (cdar alist)) (loop result (cdr alist)))
(else (loop (cons (car alist) result) (cdr alist)))))
)
; Given an alist of attributes, return the bools.
(define (attr-bool-attrs alist)
(let loop ((result nil) (alist alist))
(cond ((null? alist) (reverse! result))
((boolean? (cdar alist))
(loop (cons (car alist) result) (cdr alist)))
(else (loop result (cdr alist)))))
)
; Parse an attribute spec.
; CONTEXT is a <context> object or #f if there is none.
; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
; The result is the attribute alist.
(define (attr-parse context attrs)
(logit 4 (list 'attr-parse context attrs) "\n")
(if (not (list? attrs))
(parse-error context "improper attribute list" attrs))
(let ((alist nil))
(for-each (lambda (elm)
(cond ((symbol? elm)
; boolean attribute
(if (char=? (string-ref (symbol->string elm) 0) #\!)
(set! alist (acons (string->symbol (string-drop1 (symbol->string elm))) #f alist))
(set! alist (acons elm #t alist)))
(if (not (current-attr-lookup (caar alist)))
(parse-error context "unknown attribute" (caar alist))))
((and (list? elm) (pair? elm) (symbol? (car elm)))
(let ((a (current-attr-lookup (car elm))))
(if (not a)
(parse-error context "unknown attribute" elm))
(set! alist (cons (send a 'parse-value
context (cdr elm))
alist))))
(else (parse-error context "improper attribute" elm))))
attrs)
alist)
)
; Parse an object attribute spec.
; ATTRS is a list of attribute specs (e.g. (FOO !BAR (BAZ 3))).
; The result is an <attr-list> object.
(define (atlist-parse context attrs prefix)
(make <attr-list> prefix (attr-parse context attrs))
)
;; Return the source form of an atlist's values.
;; Externally scalar attributes (boolean, integer, enum and string) are
;; ((name1 value1) (name2 value2) ...).
;; Internally they are ((name1 . value1) (name2 . value2) ...).
;; Externally bitset attributes are (name value1 value2 ...).
;; Internally they are the same, (name value1 value2 ...).
;; If the value is an rtx expression, externally it is (name (expr)),
;; and internally it is the same, (name (expr)).
(define (atlist-source-form atlist)
(map (lambda (attr)
(let ((value (cdr attr)))
(if (pair? value)
(cons (car attr) value)
(list (car attr) value))))
(atlist-attrs atlist))
)
; Cons an attribute to an attribute list to create a new attribute list.
; ATLIST is either an attr-list object or #f or () (both of the latter two
; signify an empty attribute list, in which case we make the prefix of the
; result "").
(define (atlist-cons attr atlist)
(if (or (not atlist) (null? atlist))
(make <attr-list> "" (cons attr nil))
(make <attr-list> (atlist-prefix atlist) (cons attr (atlist-attrs atlist))))
)
; Append one attribute list to another.
; The prefix for the new atlist is taken from the first one.
(define (atlist-append attr-list1 attr-list2)
(make <attr-list>
(atlist-prefix attr-list1)
(append (atlist-attrs attr-list1) (atlist-attrs attr-list2)))
)
; Remove meta-attributes from ALIST.
; "meta" may be the wrong adjective to use here.
; The attributes in question are not intended to appear in generated files.
; They started out being attributes of attributes, hence the name "meta".
(define (attr-remove-meta-attrs-alist alist)
(let ((all-attrs (current-attr-list)))
; FIXME: Why not use find?
(let loop ((result nil) (alist alist))
(if (null? alist)
(reverse! result)
(let ((attr (attr-lookup (caar alist) all-attrs)))
(if (and attr (has-attr? attr 'META))
(loop result (cdr alist))
(loop (cons (car alist) result) (cdr alist)))))))
)
; Remove meta-attributes from ATTR-LIST.
; "meta" may be the wrong adjective to use here.
; The attributes in question are not intended to appear in generated files.
; They started out being attributes of attributes, hence the name "meta".
(define (attr-remove-meta-attrs attr-list)
; FIXME: Why not use find?
(let loop ((result nil) (attr-list attr-list))
(cond ((null? attr-list)
(reverse! result))
((has-attr? (car attr-list) 'META)
(loop result (cdr attr-list)))
(else