-
Notifications
You must be signed in to change notification settings - Fork 0
/
operand.scm
1702 lines (1467 loc) · 53.9 KB
/
operand.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
; Operands
; Copyright (C) 2000, 2001, 2005, 2009, 2010 Red Hat, Inc.
; This file is part of CGEN.
; See file COPYING.CGEN for details.
; Operands map a set of values (registers, whatever) to an instruction field
; or other indexing mechanism. Operands are also how the semantic code refers
; to hardware elements.
; The `<operand>' class.
;
; ??? Need a new lighterweight version for instances in semantics.
; This should only contain the static elements from the description file.
;
; ??? Derived operands don't use all the current class members. Perhaps
; split <operand> into two.
(define <operand>
(class-make '<operand>
'(<source-ident>)
'(
; Name as used in semantic code.
; Generally this is the same as NAME. It is changed by the
; `operand:' rtx function. One reason is to set a "pretty"
; name in tracing output (most useful in memory operands).
; A more important reason is to help match semantic operands
; with function unit input/output arguments.
sem-name
; Pretty name as used in tracing code.
; Generally this is the same as the hardware element's name.
pretty-sem-name
; Semantic name of hardware element refered to by this operand.
hw-name
; Hardware type of operand, a subclass of <hardware-base>.
; This is computed lazily from HW-NAME as many hardware
; elements can have the same semantic name. Applications
; that require a unique hardware element to be refered to are
; required to ensure duplicates are discarded (usually done
; by keeping the appropriate machs).
; All h/w elements with the same semantic name are required
; to be the same kind (register, immediate, etc.).
; FIXME: Rename to hw.
(type . #f)
; Name of mode, as specified in description file.
; This needn't be the actual mode, as WI will get coerced
; to the actual word int mode.
mode-name
; The mode TYPE is being referenced in.
; This is also looked up lazily for the same reasons as TYPE.
(mode . #f)
; Selector.
; A number or #f used to select a variant of the hardware
; element. An example is ASI's on sparc.
; ??? I really need to be better at picking names.
(selector . #f)
; Index into type, class <hw-index>.
; For example in the case of an array of registers
; it can be an instruction field or in the case of a memory
; reference it can be a register operand (or general rtx).
; ??? At present <hw-index> is a facade over the real index
; type. Not sure what the best way to do this is.
(index . #f)
; Code to run when the operand is read or #f meaning pass
; the request on to the hardware object.
(getter . #f)
; Code to run when the operand is written or #f meaning pass
; the request on to the hardware object.
(setter . #f)
; Associative list of (symbol . "handler") entries.
; Each entry maps an operation to its handler (which is up to
; the application but is generally a function name).
(handlers . ())
; Ordinal number of the operand in an insn's semantic
; description. There is no relation between the number and
; where in the semantics the operand appears. An operand that
; is both read and written are given separate ordinal numbers
; (inputs are treated separately from outputs).
(num . -1)
; Boolean indicating if the operand is conditionally
; referenced. #f means the operand is always referenced by
; the instruction.
(cond? . #f)
; whether (and by how much) this instance of the operand is
; delayed.
(delayed . #f)
)
nil)
)
; The default make! assigns the default h/w selector.
(method-make!
<operand> 'make!
(lambda (self location name comment attrs
hw-name mode-name index handlers getter setter)
(elm-set! self 'location location)
(elm-set! self 'name name)
(elm-set! self 'sem-name name)
(elm-set! self 'pretty-sem-name hw-name)
(elm-set! self 'comment comment)
(elm-set! self 'attrs attrs)
(elm-set! self 'hw-name hw-name)
(elm-set! self 'mode-name mode-name)
(elm-set! self 'selector hw-selector-default)
(elm-set! self 'index index)
(elm-set! self 'handlers handlers)
(elm-set! self 'getter getter)
(elm-set! self 'setter setter)
self)
)
; FIXME: The prefix field- doesn't seem right. Indices needn't be
; ifields, though for operands defined in .cpu files they usually are.
(method-make-forward! <operand> 'index '(field-start field-length))
; Accessor fns
(define op:sem-name (elm-make-getter <operand> 'sem-name))
(define op:set-sem-name! (elm-make-setter <operand> 'sem-name))
(define op:set-pretty-sem-name! (elm-make-setter <operand> 'pretty-sem-name))
(define op:hw-name (elm-make-getter <operand> 'hw-name))
(define op:mode-name (elm-make-getter <operand> 'mode-name))
(define op:selector (elm-make-getter <operand> 'selector))
; FIXME: op:index should be named op:hwindex.
(define op:index (elm-make-getter <operand> 'index))
(define op:handlers (elm-make-getter <operand> 'handlers))
(define op:getter (elm-make-getter <operand> 'getter))
(define op:setter (elm-make-getter <operand> 'setter))
(define op:num (elm-make-getter <operand> 'num))
(define op:set-num! (elm-make-setter <operand> 'num))
(define op:cond? (elm-make-getter <operand> 'cond?))
(define op:set-cond?! (elm-make-setter <operand> 'cond?))
(define op:delay (elm-make-getter <operand> 'delayed))
(define op:set-delay! (elm-make-setter <operand> 'delayed))
; Compute the hardware type lazily.
; FIXME: op:type should be named op:hwtype or some such.
(define op:type
(let ((getter (elm-make-getter <operand> 'type)))
(lambda (op)
(let ((type (getter op)))
(if type
type
(let* ((hw-name (op:hw-name op))
(hw-objs (current-hw-sem-lookup hw-name)))
(if (!= (length hw-objs) 1)
(error "cannot resolve h/w reference" hw-name))
((elm-make-setter <operand> 'type) op (car hw-objs))
(car hw-objs))))))
)
; Compute the operand's mode lazily (depends on hardware type which is
; computed lazily).
(define op:mode
(let ((getter (elm-make-getter <operand> 'mode)))
(lambda (op)
(let ((mode (getter op)))
(if mode
mode
(let ((mode-name (op:mode-name op))
(type (op:type op)))
(let ((mode (if (eq? mode-name 'DFLT)
(hw-default-mode type)
(mode:lookup mode-name))))
((elm-make-setter <operand> 'mode) op mode)
mode))))))
)
(method-make! <operand> 'get-mode (lambda (self) (op:mode self)))
; FIXME: wip
; Result is the <ifield> object or #f if there is none.
(define (op-ifield op)
(logit 4 " op-ifield op= " (obj:name op)
", indx= " (obj:name (op:index op)) "\n")
(let ((indx (op:index op)))
(if indx
(let ((maybe-ifld (hw-index:value (op:index op))))
(logit 4 " ifld=" (obj:name maybe-ifld) "\n")
(cond ((ifield? maybe-ifld) maybe-ifld)
((derived-ifield? maybe-ifld) maybe-ifld)
((ifield? indx) indx)
((derived-ifield? indx) indx)
(else #f)))
#f))
)
; Return mode to use for index or #f if scalar.
; This can't use method-make-forward! as we need to call op:type to
; resolve the hardware reference.
(method-make!
<operand> 'get-index-mode
(lambda (self) (send (op:type self) 'get-index-mode))
)
; Return the operand's enum.
(define (op-enum op)
(string-upcase (string-append "@ARCH@_OPERAND_" (gen-sym op)))
)
; Return a boolean indicating if X is an operand.
(define (operand? x) (class-instance? <operand> x))
; Default gen-pretty-name method.
; Return a C string of the name intended for users.
;
; FIXME: The current implementation is a quick hack. Parallel execution
; support can create operands with long names. e.g. h-memory-add-WI-src2-slo16
; The eventual way this will be handled is to record with each operand the
; entry number (or some such) in the operand instance table so that for
; registers we can compute the register's name.
(method-make!
<operand> 'gen-pretty-name
(lambda (self mode)
(let* ((name (->string (if (elm-bound? self 'pretty-sem-name)
(elm-get self 'pretty-sem-name)
(if (elm-bound? self 'sem-name)
(elm-get self 'sem-name)
(obj:name self)))))
(pname (cond ((string=? "h-memory" (string-take 8 name)) "memory")
((string=? "h-" (string-take 2 name)) (string-drop 2 name))
(else name))))
(string-append "\"" pname "\"")))
)
; Mode support.
; Create a copy of operand OP in mode NEW-MODE-NAME.
; NOTE: Even if the mode isn't changing this creates a copy.
; If OP has been subclassed the result must contain the complete class
; (e.g. the behaviour of `object-copy').
; NEW-MODE-NAME must be a valid numeric mode.
(define (op:new-mode op new-mode-name)
(let ((result (object-copy op)))
; (logit 1 "op:new-mode op=" (op:sem-name op)
; " class=" (object-class-name op)
; " hw-name=" (op:hw-name op)
; " mode=" (op:mode op)
; " newmode=" new-mode-name)
; (if (or (eq? new-mode-name 'DFLT)
; (eq? new-mode-name 'VOID) ; temporary: for upward compatibility
; (mode:eq? new-mode-name (op:mode op)))
; ; Mode isn't changing.
; result
(if #t ;; FIXME
; See if new mode is supported by the hardware.
(if (hw-mode-ok? (op:type op) new-mode-name (op:index op))
(let ((new-mode (mode:lookup new-mode-name)))
(if (not new-mode)
(error "op:new-mode: internal error, bad mode"
new-mode-name))
(elm-xset! result 'mode-name new-mode-name)
(elm-xset! result 'mode new-mode)
result)
(parse-error (make-obj-context op "op:new-mode")
(string-append "invalid mode for operand `"
(->string (obj:name op))
"'")
new-mode-name))))
)
; Return #t if operand OP references its h/w element in its natural mode.
(define (op-natural-mode? op)
(or (eq? (op:mode-name op) 'DFLT)
(mode-compatible? 'samesize (op:mode op) (hw-default-mode (op:type op))))
)
; Ifield support.
; Return list of ifields used by OP.
(define (op-iflds-used op)
(if (derived-operand? op)
(collect op-iflds-used (derived-args op))
; else
(let ((indx (op:index op)))
(if (and (eq? (hw-index:type indx) 'ifield)
(not (= (ifld-length (hw-index:value indx)) 0)))
(ifld-needed-iflds (hw-index:value indx))
nil)))
)
; The `hw-index' class.
; [Was named `index' but that conflicts with the C library function and caused
; problems when using Hobbit. And `index' is too generic a name anyway.]
;
; An operand combines a hardware object with its index.
; e.g. in an array of registers an operand serves to combine the register bank
; with the instruction field that chooses which one.
; Hardware elements are accessed via other means as well besides instruction
; fields so we need a way to designate something as being an index.
; The `hw-index' class does that. It serves as a facade to the underlying
; details.
; ??? Not sure whether this is the best way to handle this or not.
;
; NAME is the name of the index or 'anonymous.
; This is used, for example, to give a name to the simulator extraction
; structure member.
; TYPE is a symbol that indicates what VALUE is.
; scalar: the hardware object is a scalar, no index is required
; [MODE and VALUE are #f to denote "undefined" in this case]
; constant: a (non-negative) integer (FIXME: rename to const)
; enum: an enum value stored as (enum-name . (enum-lookup-val enum-name)),
; i.e. (name value . enum-obj)
; str-expr: a C expression as a string
; rtx: an rtx to be expanded
; ifield: an <ifield> object
; derived-ifield: a <derived-ifield> object ???
; operand: an <operand> object
; ??? A useful simplification may be to always record the value as an rtx
; [which may require extensions to rtl so is deferred].
; ??? We could use runtime type identification, but doing things this way
; adds more structure.
;
; MODE is the mode of VALUE, as a <mode> object.
; If DFLT, mode must be obtained from VALUE.
; DFLT is only allowable for rtx and operand types.
(define <hw-index> (class-make '<hw-index> nil '(name type mode value) nil))
; Accessors.
; Use obj:name for `name'.
(define hw-index:type (elm-make-getter <hw-index> 'type))
(define hw-index:mode (elm-make-getter <hw-index> 'mode))
(define hw-index:value (elm-make-getter <hw-index> 'value))
; Allow the mode to be specified by its name.
(method-make!
<hw-index> 'make!
(lambda (self name type mode value)
(elm-set! self 'name name)
(elm-set! self 'type type)
(elm-set! self 'mode (mode-maybe-lookup mode))
(elm-set! self 'value value)
self)
)
; get-name handler
(method-make!
<hw-index> 'get-name
(lambda (self)
(elm-get self 'name))
)
; get-atlist handler
(method-make!
<hw-index> 'get-atlist
(lambda (self)
(case (hw-index:type self)
((ifield) (obj-atlist (hw-index:value self)))
(else atlist-empty)))
)
; ??? Until other things settle.
(method-make!
<hw-index> 'field-start
(lambda (self)
(if (eq? (hw-index:type self) 'ifield)
(send (hw-index:value self) 'field-start)
0))
)
(method-make!
<hw-index> 'field-length
(lambda (self)
(if (eq? (hw-index:type self) 'ifield)
(send (hw-index:value self) 'field-length)
0))
)
;; Return #t if index is a constant.
(define (hw-index-constant? hw-index)
(memq (hw-index:type hw-index) '(constant enum))
)
;; Given that (hw-index-constant? hw-index) is true, return the value.
(define (hw-index-constant-value hw-index)
(case (hw-index:type hw-index)
((constant) (hw-index:value hw-index))
((enum) (hw-index-enum-value hw-index))
(else (error "invalid constant hw-index" hw-index)))
)
;; Make an enum <hw-index> given the enum's name.
(define (make-enum-hw-index name enum-name)
(make <hw-index> name 'enum UINT
(cons enum-name (enum-lookup-val enum-name)))
)
;; Given an enum <hw-index>, return the enum's name.
(define (hw-index-enum-name hw-index)
(car (hw-index:value hw-index))
)
;; Given an enum <hw-index>, return the enum's value.
(define (hw-index-enum-value hw-index)
(cadr (hw-index:value hw-index))
)
;; Given an enum <hw-index>, return the enum's object.
(define (hw-index-enum-obj hw-index)
(cddr (hw-index:value hw-index))
)
; There only ever needs to be one of these objects, so create one.
(define hw-index-scalar
; We can't use `make' here as the make! method calls mode:lookup which
; (a) doesn't exist if we're compiled with Hobbit and mode.scm isn't
; and (b) will fail anyway since #f isn't a valid mode.
(let ((scalar-index (new <hw-index>)))
(elm-xset! scalar-index 'name 'hw-index-scalar)
(elm-xset! scalar-index 'type 'scalar)
(elm-xset! scalar-index 'mode #f)
(elm-xset! scalar-index 'value #f)
(lambda () scalar-index))
)
; Placeholder for indices of "anyof" operands.
; There only needs to be one of these, so we create one and always use that.
(define hw-index-anyof
; We can't use `make' here as the make! method calls mode:lookup which
; (a) doesn't exist if we're compiled with Hobbit and mode.scm isn't
; and (b) will fail anyway since #f isn't a valid mode.
(let ((anyof-index (new <hw-index>)))
(elm-xset! anyof-index 'name 'hw-index-anyof)
(elm-xset! anyof-index 'type 'scalar)
(elm-xset! anyof-index 'mode #f)
(elm-xset! anyof-index 'value #f)
(lambda () anyof-index))
)
(define hw-index-derived
; We can't use `make' here as the make! method calls mode:lookup which
; (a) doesn't exist if we're compiled with Hobbit and mode.scm isn't
; and (b) will fail anyway since #f isn't a valid mode.
(let ((derived-index (new <hw-index>)))
(elm-xset! derived-index 'name 'hw-index-derived)
(elm-xset! derived-index 'type 'scalar)
(elm-xset! derived-index 'mode #f)
(elm-xset! derived-index 'value #f)
(lambda () derived-index))
)
; Hardware selector support.
;
; A hardware "selector" is like an index except is along an atypical axis
; and thus is rarely used. It exists to support things like ASI's on Sparc.
; What to pass to indicate "default selector".
; (??? value is temporary choice to be revisited).
(define hw-selector-default '(symbol NONE))
(define (hw-selector-default? sel) (equal? sel hw-selector-default))
; Hardware support.
; Return list of hardware elements refered to in OP-LIST
; with no duplicates.
(define (op-nub-hw op-list)
; Build a list of hw elements.
(let ((hw-list (map (lambda (op)
(if (hw-ref? op) ; FIXME: hw-ref? is undefined
op
(op:type op)))
op-list)))
; Now build an alist of (name . obj) elements, take the nub, then the cdr.
; ??? These lists tend to be small so sorting first is probably overkill.
(map cdr
(alist-nub (alist-sort (map (lambda (hw) (cons (obj:name hw) hw))
hw-list)))))
)
; Parsing support.
; Utility of /operand-parse-[gs]etter to build the expected syntax,
; for use in error messages.
(define (/operand-g/setter-syntax rank setter?)
(string-append "("
(string-drop1
(numbers->string (iota rank) " index"))
(if setter?
(if (>= rank 1)
" newval"
"newval")
"")
") (expression)")
)
; Parse a getter spec.
; The syntax is (([index-names]) (... code ...)).
; Omit `index-names' for scalar objects.
; {rank} is the required number of elements in {index-names}.
(define (/operand-parse-getter context getter rank)
(if (null? getter)
#f ; use default
(let ()
(if (or (not (list? getter))
(!= (length getter) 2)
(not (and (list? (car getter))
(= (length (car getter)) rank))))
(parse-error context
(string-append "invalid getter, should be "
(/operand-g/setter-syntax rank #f))
getter))
(if (not (rtx? (cadr getter)))
(parse-error context "invalid rtx expression" getter))
getter))
)
; Parse a setter spec.
; The syntax is (([index-names] newval) (... code ...)).
; Omit `index-names' for scalar objects.
; {rank} is the required number of elements in {index-names}.
(define (/operand-parse-setter context setter rank)
(if (null? setter)
#f ; use default
(let ()
(if (or (not (list? setter))
(!= (length setter) 2)
(not (and (list? (car setter))
(= (+ 1 (length (car setter)) rank)))))
(parse-error context
(string-append "invalid setter, should be "
(/operand-g/setter-syntax rank #t))
setter))
(if (not (rtx? (cadr setter)))
(parse-error context "invalid rtx expression" setter))
setter))
)
; Parse an operand definition.
; This is the main routine for building an operand object from a
; description in the .cpu file.
; All arguments are in raw (non-evaluated) form.
; The result is the parsed object or #f if object isn't for selected mach(s).
; ??? This only takes insn fields as the index. May need another proc (or an
; enhancement of this one) that takes other kinds of indices.
(define (/operand-parse context name comment attrs hw mode index handlers getter setter)
(logit 2 "Processing operand " name " ...\n")
;; Pick out name first to augment the error context.
(let* ((name (parse-name context name))
(context (context-append-name context name))
(atlist-obj (atlist-parse context attrs "cgen_operand"))
(isa-name-list (atlist-attr-value atlist-obj 'ISA #f)))
;; Verify all specified ISAs are valid.
(if (not (all-true? (map current-isa-lookup isa-name-list)))
(parse-error context "unknown isa in isa list" isa-name-list))
(if (keep-atlist? atlist-obj #f)
(let ((hw-objs (current-hw-sem-lookup hw))
(mode-obj (parse-mode-name context mode))
(index-val (cond ((integer? index)
index)
((and (symbol? index) (enum-lookup-val index))
=> (lambda (x) x))
((and (symbol? index) (current-ifld-lookup index isa-name-list))
=> (lambda (x) x))
(else
(if (symbol? index)
(parse-error context "unknown enum or ifield" index)
(parse-error context "invalid operand index" index))))))
(if (not mode-obj)
(parse-error context "unknown mode" mode))
;; Disallow some obviously invalid numeric indices.
(if (and (number? index-val)
(or (not (integer? index-val))
(< index-val 0)))
(parse-error context "invalid integer index" index))
;; If an enum is used, it must be non-negative.
(if (and (pair? index-val)
(< (car index-val) 0))
(parse-error context "negative enum value" index))
;; NOTE: Don't validate HW until we know whether this operand
;; will be kept or not. If not, HW may have been discarded too.
(if (null? hw-objs)
(parse-error context "unknown hardware element" hw))
;; At this point INDEX-VAL is either an integer, (value . enum-obj),
;; or an <ifield> object.
;; Since we can't look up the hardware element at this time
;; [well, actually we should be able to with a bit of work],
;; we determine scalarness from an index of f-nil.
(let ((hw-index
(cond ((integer? index-val)
(make <hw-index> (symbol-append 'i- name)
;; FIXME: constant -> const
'constant UINT index-val))
((pair? index-val) ;; enum?
(make <hw-index> (symbol-append 'i- name)
'enum UINT (cons index index-val)))
((ifld-nil? index-val)
(hw-index-scalar))
(else
(make <hw-index> (symbol-append 'i- name)
'ifield UINT index-val)))))
(make <operand>
(context-location context)
name
(parse-comment context comment)
;; Copy FLD's attributes so one needn't duplicate attrs like
;; PCREL-ADDR, etc. An operand inherits the attributes of
;; its field. They are overridable of course, which is why we use
;; `atlist-append' here.
(if (ifield? index-val)
(atlist-append atlist-obj (obj-atlist index-val))
atlist-obj)
hw ;; note that this is the hw's name, not an object
mode ;; ditto, this is a name, not an object
hw-index
(parse-handlers context '(parse print) handlers)
(/operand-parse-getter context getter (if scalar? 0 1))
(/operand-parse-setter context setter (if scalar? 0 1))
)))
(begin
(logit 2 "Ignoring " name ".\n")
#f)))
)
; Read an operand description.
; This is the main routine for analyzing operands in the .cpu file.
; CONTEXT is a <context> object for error messages.
; ARG-LIST is an associative list of field name and field value.
; /operand-parse is invoked to create the <operand> object.
(define (/operand-read context . arg-list)
(let (
(name nil)
(comment nil)
(attrs nil)
(type nil)
(mode 'DFLT) ; use default mode of TYPE
(index nil)
(handlers nil)
(getter nil)
(setter nil)
)
(let loop ((arg-list arg-list))
(if (null? arg-list)
nil
(let ((arg (car arg-list))
(elm-name (caar arg-list)))
(case elm-name
((name) (set! name (cadr arg)))
((comment) (set! comment (cadr arg)))
((attrs) (set! attrs (cdr arg)))
((type) (set! type (cadr arg)))
((mode) (set! mode (cadr arg)))
((index) (set! index (cadr arg)))
((handlers) (set! handlers (cdr arg)))
((getter) (set! getter (cdr arg)))
((setter) (set! setter (cdr arg)))
(else (parse-error context "invalid operand arg" arg)))
(loop (cdr arg-list)))))
; Now that we've identified the elements, build the object.
(/operand-parse context name comment attrs type mode index handlers
getter setter))
)
; Define an operand object, name/value pair list version.
(define define-operand
(lambda arg-list
(let ((op (apply /operand-read (cons (make-current-context "define-operand")
arg-list))))
(if op
(current-op-add! op))
op))
)
; Define an operand object, all arguments specified.
(define (define-full-operand name comment attrs type mode index handlers getter setter)
(let ((op (/operand-parse (make-current-context "define-full-operand")
name comment attrs
type mode index handlers getter setter)))
(if op
(current-op-add! op))
op)
)
; Derived operands.
;
; Derived operands are used to implement operands more complex than just
; the mapping of an instruction field to a register bank. Their present
; raison d'etre is to create a new axis on which to implement the complex
; addressing modes of the i386 and m68k. The brute force way of describing
; these instruction sets would be to have one `dni' per addressing mode
; per instruction. What's needed is to abstract away the various addressing
; modes within something like operands.
;
; ??? While internally we end up with the "brute force" approach, in and of
; itself that's ok because it's an internal implementation issue.
; See <multi-insn>.
;
; ??? Another way to go is to have one dni per addressing mode. That seems
; less clean though as one dni would be any of add, sub, and, or, xor, etc.
;
; ??? Some addressing modes have side-effects (e.g. pre-dec, etc. like insns).
; This can be represented, but if two operands have side-effects special
; trickery may be required to get the order of side-effects right. Need to
; avoid any "trickery" at all.
;
; ??? Not yet handled are modelling parameters.
; ??? Not yet handled are the handlers,getter,setter spec of normal operands.
;
; ??? Division of class members b/w <operand> and <derived-operand> is wip.
; ??? As is potential introduction of other classes to properly organize
; things.
(define <derived-operand>
(class-make '<derived-operand>
'(<operand>)
'(
; Args (list of <operands> objects).
args
; Syntax string.
syntax
; Base ifield, common to all choices.
; ??? experiment
base-ifield
; <derived-ifield> object.
encoding
; Assertions of any ifield values or #f if none.
(ifield-assertion . #f)
)
'())
)
;; <derived-operand> constructor.
;; MODE is a <mode> object.
(method-make!
<derived-operand> 'make!
(lambda (self name comment attrs mode
args syntax base-ifield encoding ifield-assertion
getter setter)
(elm-set! self 'name name)
(elm-set! self 'comment comment)
(elm-set! self 'attrs attrs)
(elm-set! self 'sem-name name)
(elm-set! self 'pretty-sem-name #f) ;; FIXME
(elm-set! self 'hw-name #f) ;; FIXME
(elm-set! self 'mode mode)
(elm-set! self 'mode-name (obj:name mode))
(elm-set! self 'getter getter)
(elm-set! self 'setter setter)
;; These are the additional fields in <derived-operand>.
(elm-set! self 'args args)
(elm-set! self 'syntax syntax)
(elm-set! self 'base-ifield base-ifield)
(elm-set! self 'encoding encoding)
(elm-set! self 'ifield-assertion ifield-assertion)
self)
)
(define (derived-operand? x) (class-instance? <derived-operand> x))
(define-getters <derived-operand> derived
(args syntax base-ifield encoding ifield-assertion)
)
; "anyof" operands are subclassed from derived operands.
; They typically handle multiple addressing modes of CISC architectures.
(define <anyof-operand>
(class-make '<anyof-operand>
'(<operand>)
'(
; Base ifield, common to all choices.
; FIXME: wip
base-ifield
; List of <derived-operand> objects.
; ??? Maybe allow <operand>'s too?
choices
)
'())
)
(define (anyof-operand? x) (class-instance? <anyof-operand> x))
(method-make!
<anyof-operand> 'make!
(lambda (self name comment attrs mode-name base-ifield choices)
(elm-set! self 'name name)
(elm-set! self 'comment comment)
(elm-set! self 'attrs attrs)
(elm-set! self 'sem-name name)
(elm-set! self 'pretty-sem-name #f) ;; FIXME
(elm-set! self 'hw-name #f) ;; FIXME
(elm-set! self 'mode-name mode-name)
(elm-set! self 'base-ifield base-ifield)
(elm-set! self 'choices choices)
; Set index to a special marker value.
(elm-set! self 'index (hw-index-anyof))
self)
)
(define-getters <anyof-operand> anyof (choices))
; Derived/Anyof parsing support.
; Subroutine of /derived-operand-parse to parse the encoding.
; The result is a <derived-ifield> object.
; The {owner} member still needs to be set!
(define (/derived-parse-encoding context isa-name-list operand-name encoding)
(if (or (null? encoding)
(not (list? encoding)))
(parse-error context "encoding not a list" encoding))
(if (not (eq? (car encoding) '+))
(parse-error context "encoding must begin with `+'" encoding))
; ??? Calling /parse-insn-format is a quick hack.
; It's an internal routine of some other file.
(let ((iflds (/parse-insn-format context #f isa-name-list encoding)))
(make <derived-ifield>
operand-name
'derived-ifield ; (string-append "<derived-ifield> for " operand-name)
atlist-empty
#f ; owner
iflds ; subfields
))
)
;; Subroutine of /derived-operand-parse to parse the ifield assertion.
;; The ifield assertion is either () or a (restricted) RTL expression
;; asserting something about the ifield values of the containing insn.
;; The result is #f if the assertion is (), or the canonical rtl.
(define (/derived-parse-ifield-assertion context isa-name-list ifield-assertion)
(if (null? ifield-assertion)
#f
(rtx-canonicalize context 'INT isa-name-list nil ifield-assertion))
)
; Parse a derived operand definition.
; This is the main routine for building a derived operand object from a
; description in the .cpu file.
; All arguments are in raw (non-evaluated) form.
; The result is the parsed object or #f if object isn't for selected mach(s).
;
; ??? Currently no support for handlers(,???) found in normal operands.
; Later, when necessary.
(define (/derived-operand-parse context name comment attrs mode
args syntax
base-ifield encoding ifield-assertion
getter setter)
(logit 2 "Processing derived operand " name " ...\n")
;; Pick out name first to augment the error context.
(let* ((name (parse-name context name))
(context (context-append-name context name))
(atlist-obj (atlist-parse context attrs "cgen_operand"))
(isa-name-list (atlist-attr-value atlist-obj 'ISA #f)))
;; Verify all specified ISAs are valid.
(if (not (all-true? (map current-isa-lookup isa-name-list)))
(parse-error context "unknown isa in isa list" isa-name-list))
(if (keep-atlist? atlist-obj #f)
(let* ((mode-obj (parse-mode-name context mode))
(parsed-encoding (/derived-parse-encoding context isa-name-list
name encoding)))
(if (not mode-obj)
(parse-error context "unknown mode" mode))
(let ((result
(make <derived-operand>
name
(parse-comment context comment)
atlist-obj
mode-obj
(map (lambda (a)
(if (not (symbol? a))
(parse-error context "arg not a symbol" a))
(let ((op (current-op-lookup a isa-name-list)))
(if (not op)
(parse-error context "not an operand" a))
op))
args)
syntax
base-ifield ; FIXME: validate
parsed-encoding
(/derived-parse-ifield-assertion context isa-name-list
ifield-assertion)
(if (null? getter)
#f
(/operand-parse-getter
context
(list args
(rtx-canonicalize context mode
isa-name-list nil
getter))
(length args)))
(if (null? setter)
#f
(/operand-parse-setter
context
(list (append args '(newval))
(rtx-canonicalize context 'VOID
isa-name-list
(list (list 'newval mode #f))
setter))
(length args)))
)))
(elm-set! result 'hw-name (obj:name (hardware-for-mode mode-obj)))
;(elm-set! result 'hw-name (obj:name parsed-encoding))
;(elm-set! result 'hw-name base-ifield)
(elm-set! result 'index parsed-encoding)
; (elm-set! result 'index (hw-index-derived)) ; A temporary dummy
(logit 2 " new derived-operand; name= " name
", hw-name= " (op:hw-name result)
", index=" (obj:name parsed-encoding) "\n")
(derived-ifield-set-owner! parsed-encoding result)
result))
(begin
(logit 2 "Ignoring " name ".\n")
#f)))
)
; Read a derived operand description.
; This is the main routine for analyzing derived operands in the .cpu file.
; CONTEXT is a <context> object for error messages.
; ARG-LIST is an associative list of field name and field value.
; /derived-operand-parse is invoked to create the <derived-operand> object.
(define (/derived-operand-read context . arg-list)
(let (
(name nil)
(comment nil)
(attrs nil)
(mode 'DFLT) ; use default mode of TYPE
(args nil)
(syntax nil)
(base-ifield nil)
(encoding nil)
(ifield-assertion nil)
(getter nil)
(setter nil)
)
(let loop ((arg-list arg-list))
(if (null? arg-list)
nil
(let ((arg (car arg-list))
(elm-name (caar arg-list)))
(case elm-name
((name) (set! name (cadr arg)))
((comment) (set! comment (cadr arg)))
((attrs) (set! attrs (cdr arg)))