-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifield.scm
1255 lines (1059 loc) · 37.4 KB
/
ifield.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
; Instruction fields.
; Copyright (C) 2000, 2002, 2009 Red Hat, Inc.
; This file is part of CGEN.
; See file COPYING.CGEN for details.
; The `<ifield>' class.
; (pronounced "I-field")
;
; These describe raw data, little semantic content is attributed to them.
; The goal being to avoid interfering with future applications.
;
; FIXME: Move start, word-offset, word-length into the instruction format?
; - would require proper ordering of fields in insns, but that's ok.
; (??? though the sparc64 description shows a case where its useful to
; not have to worry about instruction ordering - different versions of an
; insn take different fields and these fields are passed via a macro)
;
; ??? One could treat all ifields as being unsigned. They could be thought of
; as indices into a table of values, be they signed, unsigned, floating point,
; whatever. Just an idea.
;
; ??? Split into two? One for definition, and one for value.
(define <ifield>
(class-make '<ifield>
'(<source-ident>)
'(
; The mode the raw value is to be interpreted in.
; This is a <mode> object.
mode
; A <bitrange> object.
; This contains the field's offset, start, length, word-length,
; and orientation (msb==0, lsb==0). The orientation is
; recorded to keep the <bitrange> object self-contained.
; Endianness is not recorded.
bitrange
; Argument to :follows, as an object.
; FIXME: wip
(follows . #f)
; ENCODE/DECODE operate on the raw value, absent of any context
; save `pc' and mode of field.
; If #f, no special processing is required.
; ??? It's not clear where the best place to process fields is.
; An earlier version had insert/extract fields in operands to
; handle more complicated cases. Following the goal of
; incremental complication, the special handling for m32r's
; f-disp8 field is handled entirely here, rather than partially
; here and partially in the operand.
encode decode
; Value of field, if there is one, or #f.
; Possible types are: integer, <operand>, ???
(value . #f)
)
nil)
)
; {ordinal} is missing on purpose, it's handled at a higher level.
; {value},{follows} are missing on purpose.
; {value} is handled specially.
; {follows} is rarely used.
(method-make-make! <ifield>
'(location name comment attrs mode bitrange encode decode))
; Accessor fns
; ??? `value' is treated specially, needed anymore?
(define-getters <ifield> ifld (mode encode decode follows))
(define-setters <ifield> ifld (follows))
; internal fn
(define /ifld-bitrange (elm-make-getter <ifield> 'bitrange))
(define (ifld-word-offset f) (bitrange-word-offset (/ifld-bitrange f)))
(define (ifld-word-length f) (bitrange-word-length (/ifld-bitrange f)))
; Return the mode of the decoded value of <ifield> F.
; ??? This is made easy because we require the decode expression to have
; an explicit mode.
(define (ifld-decode-mode f)
(assert (elm-bound? f 'decode))
(let ((d (ifld-decode f)))
(if d
;; FIXME: Does this work with canonicalized rtl?
(mode:lookup (cadr (cadr d)))
(ifld-mode f)))
)
; Return start of ifield.
(method-make!
<ifield> 'field-start
(lambda (self)
(bitrange-start (/ifld-bitrange self)))
)
(define (ifld-start ifld)
(send ifld 'field-start)
)
(method-make!
<ifield> 'field-length
(lambda (self)
(bitrange-length (elm-get self 'bitrange)))
)
(define (ifld-length f) (send f 'field-length))
; FIXME: It might make things more "readable" if enum values were preserved in
; their symbolic form and the get-field-value method did the lookup.
(method-make!
<ifield> 'get-field-value
(lambda (self)
(elm-get self 'value))
)
(define (ifld-get-value self)
(send self 'get-field-value)
)
(method-make!
<ifield> 'set-field-value!
(lambda (self new-val)
(elm-set! self 'value new-val))
)
(define (ifld-set-value! self new-val)
(send self 'set-field-value! new-val)
)
; Return a boolean indicating if X is an <ifield>.
(define (ifield? x) (class-instance? <ifield> x))
; Return ilk of field as a string.
; ("ilk" sounds klunky but "type" is too ambiguous. Here "ilk" means
; the kind of the hardware element, enum, etc.)
; The result is a character string naming the field type.
(define (ifld-ilk fld)
(let ((value (elm-xget fld 'value)))
; ??? One could require that the `value' field always be an object.
; I can't get too worked up over it yet.
(if (object? value)
(symbol->string (obj:name value)) ; send 'get-name to fetch the name
"#")) ; # -> "it's a number"
)
; Generate the name of the enum for instruction field ifld.
; If PREFIX? is present and #f, the @ARCH@_ prefix is omitted.
(define (ifld-enum ifld . prefix?)
(string-upcase (string-append (if (or (null? prefix?) (car prefix?))
"@ARCH@_"
"")
(gen-sym ifld)))
)
; Return a boolean indicating if ifield F is an opcode field
; (has a constant value).
(define (ifld-constant? f)
(number? (ifld-get-value f))
; (and (number? (ifld-get-value f))
; (if option:reserved-as-opcode?
; #t
; (not (has-attr? f 'RESERVED))))
)
; Return a boolean indicating if ifield F is signed.
(define (ifld-signed? f)
(eq? (mode:class (ifld-mode f)) 'INT)
)
; Return a boolean indicating if ifield F is an operand.
; FIXME: Should check for operand? or some such.
(define (ifld-operand? f) (not (number? (ifld-get-value f))))
; Return known value table for rtx-simplify of <ifield> list ifld-list.
(define (ifld-known-values ifld-list)
(let ((constant-iflds (find ifld-constant? (ifields-base-ifields ifld-list))))
(map (lambda (f)
(cons (obj:name f)
(rtx-make-const 'INT (ifld-get-value f))))
constant-iflds))
)
; Return mask to use for a field in <bitrange> CONTAINER.
; If the bitrange is outside the range of the field, return 0.
; If CONTAINER is #f, use the recorded bitrange.
; BASE-LEN, if non-#f, overrides the base insn length of the insn.
; BASE-LEN is present for architectures like the m32r where there are insns
; smaller than the base insn size (LIW).
;
; Simplifying restrictions [to be relaxed as necessary]:
; - the field must either be totally contained within CONTAINER or totally
; outside it, partial overlaps aren't handled
; - CONTAINER must be an integral number of bytes, beginning on a
; byte boundary [simplifies things]
; - both SELF's bitrange and CONTAINER must have the same word length
; - LSB0? of SELF's bitrange and CONTAINER must be the same
(method-make!
<ifield> 'field-mask
(lambda (self base-len container)
(let* ((container (or container (/ifld-bitrange self)))
(bitrange (/ifld-bitrange self))
(recorded-word-length (bitrange-word-length bitrange))
(word-offset (bitrange-word-offset bitrange)))
(let ((lsb0? (bitrange-lsb0? bitrange))
(start (bitrange-start bitrange))
(length (bitrange-length bitrange))
(word-length (or (and (= word-offset 0) base-len)
recorded-word-length))
(container-word-offset (bitrange-word-offset container))
(container-word-length (bitrange-word-length container)))
(cond
; must be same lsb0
((not (eq? lsb0? (bitrange-lsb0? container)))
(error "field-mask: different lsb0? values"))
((not (= word-length container-word-length))
0)
; container occurs after?
((<= (+ word-offset word-length) container-word-offset)
0)
; container occurs before?
((>= word-offset (+ container-word-offset container-word-length))
0)
(else
(word-mask start length word-length lsb0? #f))))))
)
(define (ifld-mask ifld base-len container)
(send ifld 'field-mask base-len container)
)
; Return VALUE inserted into the field's position.
; BASE-LEN, if non-#f, overrides the base insn length of the insn.
; BASE-LEN is present for architectures like the m32r where there are insns
; smaller than the base insn size (LIW).
(method-make!
<ifield> 'field-value
(lambda (self base-len value)
(let* ((bitrange (/ifld-bitrange self))
(recorded-word-length (bitrange-word-length bitrange))
(word-offset (bitrange-word-offset bitrange))
(word-length (or (and (= word-offset 0) base-len)
recorded-word-length)))
(word-value (ifld-start self)
(bitrange-length bitrange)
word-length
(bitrange-lsb0? bitrange) #f
value)))
)
; FIXME: confusion with ifld-get-value.
(define (ifld-value f base-len value)
(send f 'field-value base-len value)
)
; Return a list of ifields required to compute <ifield> F's value.
; Normally this is just F itself. For multi-ifields it will be more.
; ??? It can also be more if F's value is derived from other fields but
; that isn't supported yet.
(method-make!
<ifield> 'needed-iflds
(lambda (self)
(list self))
)
(define (ifld-needed-iflds f)
(send f 'needed-iflds)
)
; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
; VALUE is the entire insn's value if it fits in a word, or is a list
; of values, one per word (not implemented, sigh).
; ??? The instruction's format should specify where the word boundaries are.
(method-make!
<ifield> 'field-extract
(lambda (self insn value)
(let ((base-len (insn-base-mask-length insn)))
(word-extract (ifld-start self)
(ifld-length self)
base-len
(ifld-lsb0? self)
#f ; start is msb
value)))
)
(define (ifld-extract ifld value insn)
(send ifld 'field-extract value insn)
)
; Return a boolean indicating if bit 0 is the least significant bit.
(method-make!
<ifield> 'field-lsb0?
(lambda (self)
(bitrange-lsb0? (/ifld-bitrange self)))
)
(define (ifld-lsb0? f) (send f 'field-lsb0?))
; Return the minimum value of a field.
(method-make!
<ifield> 'min-value
(lambda (self)
(case (mode:class (ifld-mode self))
((INT) (- (integer-expt 2 (- (ifld-length self) 1))))
((UINT) 0)
(else (error "unsupported mode class" (mode:class (ifld-mode self))))))
)
; Return the maximum value of a field.
(method-make!
<ifield> 'max-value
(lambda (self)
(case (mode:class (ifld-mode self))
((INT) (- (integer-expt 2 (- (ifld-length self) 1)) 1))
((UINT) (- (integer-expt 2 (ifld-length self)) 1))
(else (error "unsupported mode class" (mode:class (ifld-mode self))))))
)
; Create a copy of field F with value VALUE.
; VALUE is either ... ???
(define (ifld-new-value f value)
(let ((new-f (object-copy f)))
(ifld-set-value! new-f value)
new-f)
)
; Change the offset of the word containing an ifield to {word-offset}.
(method-make!
<ifield> 'set-word-offset!
(lambda (self word-offset)
(let ((bitrange (object-copy (/ifld-bitrange self))))
(bitrange-set-word-offset! bitrange word-offset)
(elm-set! self 'bitrange bitrange)
*UNSPECIFIED*))
)
(define (ifld-set-word-offset! f word-offset)
(send f 'set-word-offset! word-offset)
)
; Return a copy of F with new {word-offset}.
(define (ifld-new-word-offset f word-offset)
(let ((new-f (object-copy f)))
(ifld-set-word-offset! new-f word-offset)
new-f)
)
; Return the bit offset of the word after the word <ifield> F is in.
; What a `word' here is defined by F in its bitrange.
(method-make!
<ifield> 'next-word
(lambda (self)
(let ((br (/ifld-bitrange f)))
(bitrange-next-word br)))
)
(define (ifld-next-word f) (send f 'next-word))
; Return a boolean indicating if <ifield> F1 precedes <ifield> F2.
; FIXME: Move into a method as different subclasses will need
; different handling.
(define (ifld-precedes? f1 f2)
(let ((br1 (/ifld-bitrange f1))
(br2 (/ifld-bitrange f2)))
(cond ((< (bitrange-word-offset br1) (bitrange-word-offset br2))
#t)
((= (bitrange-word-offset br1) (bitrange-word-offset br2))
(begin
(assert (eq? (bitrange-lsb0? br1) (bitrange-lsb0? br2)))
(assert (= (bitrange-word-length br1) (bitrange-word-length br1)))
; ??? revisit
(if (bitrange-lsb0? br1)
(> (bitrange-start br1) (bitrange-start br2))
(< (bitrange-start br1) (bitrange-start br2)))))
(else
#f)))
)
;; Pretty print an ifield, typically for error messages.
(method-make!
<ifield> 'pretty-print
(lambda (self)
(string-append "(" (obj:str-name self)
" " (number->string (ifld-start self))
" " (number->string (ifld-length self))
")"))
)
(define (ifld-pretty-print f)
(send f 'pretty-print)
)
; Parse an ifield definition.
; This is the main routine for building an ifield 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).
;
; Two forms of specification are supported, loosely defined as the RISC way
; and the CISC way. The reason for the distinction is to simplify ifield
; specification of RISC-like cpus.
; Note that VLIW's are another way. These are handled like the RISC way, with
; the possible addition of instruction framing (which is, surprise surprise,
; wip).
;
; RISC:
; WORD-OFFSET and WORD-LENGTH are #f. Insns are assumed to be N copies of
; (isa-default-insn-word-bitsize). WORD-OFFSET is computed from START.
; START is the offset in bits from the start of the insn.
; FLENGTH is the length of the field in bits.
;
; CISC:
; WORD-OFFSET is the offset in bits from the start to the first byte of the
; word containing the ifield.
; WORD-LENGTH is the length in bits of the word containing the ifield.
; START is the starting bit number in the word. Bit numbering is taken from
; (current-arch-insn-lsb0?).
; FLENGTH is the length in bits of the ifield. It is named that way to avoid
; collision with the proc named `length'.
;
; FIXME: More error checking.
(define (/ifield-parse context name comment attrs
word-offset word-length start flength follows
mode encode decode)
(logit 2 "Processing ifield " name " ...\n")
;; Pick out name first to augment the error context.
(let* ((name (parse-name context name))
(context (context-append-name context name))
(atlist (atlist-parse context attrs "cgen_ifld"))
(isas (atlist-attr-value atlist 'ISA #f)))
; No longer ensure only one isa specified.
;(if (!= (length isas) 1)
; (parse-error context "can only specify 1 isa" attrs))
(if (not (eq? (->bool word-offset)
(->bool word-length)))
(parse-error context "either both or neither of word-offset,word-length can be specified"))
(if (keep-isa-atlist? atlist #f)
(let ((isa (current-isa-lookup (car isas)))
(word-offset (and word-offset
(parse-number context word-offset '(0 . 256))))
(word-length (and word-length
(parse-number context word-length '(0 . 128))))
; ??? 0.127 for now
(start (parse-number context start '(0 . 127)))
; ??? 0.127 for now
(flength (parse-number context flength '(0 . 127)))
(lsb0? (current-arch-insn-lsb0?))
(mode-obj (parse-mode-name context mode))
(follows-obj (/ifld-parse-follows context follows isas))
)
; Calculate the <bitrange> object.
; ??? Move positional info to format?
(let ((bitrange
(if word-offset
; CISC-like. Easy. Everything must be specified.
(make <bitrange>
word-offset start flength word-length lsb0?)
; RISC-like. Hard. Have to make best choice of start,
; flength. This doesn't have to be perfect, just easily
; explainable. Cases this doesn't handle can explicitly
; specify word-offset,word-length.
; One can certainly argue the choice of the term
; "RISC-like" is inaccurate. Perhaps.
(let* ((diwb (isa-default-insn-word-bitsize isa))
(word-offset (/get-ifld-word-offset start flength diwb lsb0?))
(word-length (/get-ifld-word-length start flength diwb lsb0?))
(start (- start word-offset))
)
(make <bitrange>
word-offset
start
flength
word-length
lsb0?))))
)
(let ((result
(make <ifield>
(context-location context)
name
(parse-comment context comment)
atlist
mode-obj
bitrange
(/ifld-parse-encode context encode)
(/ifld-parse-decode context decode))))
(if follows-obj
(ifld-set-follows! result follows-obj))
result)))
; Else ignore entry.
(begin
(logit 2 "Ignoring " name ".\n")
#f)))
)
; Subroutine of /ifield-parse to simplify it.
; Given START,FLENGTH, return the "best" choice for the offset to the word
; containing the ifield.
; This is easy to visualize, hard to put into words.
; Imagine several words of size DIWB laid out from the start of the insn.
; On top of that lay the ifield.
; Now pick the minimal set of words that are required to contain the ifield.
; That's what we want.
; No claim is made that this is always the correct choice for any
; particular architecture. For those where this isn't correct, the ifield
; must be fully specified (i.e. word-offset,word-length explicitly specified).
(define (/get-ifld-word-offset start flength diwb lsb0?)
(if lsb0?
; Convert to non-lsb0 case, then it's easy.
; NOTE: The conversion is seemingly wrong because `start' is misnamed.
; It's now `end'.
(set! start (+ (- start flength) 1)))
(- start (remainder start diwb))
)
; Subroutine of /ifield-parse to simplify it.
; Given START,FLENGTH, return the "best" choice for the length of the word
; containing the ifield.
; DIWB = default insn word bitsize
; See -get-ifld-word-offset for more info.
(define (/get-ifld-word-length start flength diwb lsb0?)
(if lsb0?
; Convert to non-lsb0 case, then it's easy.
; NOTE: The conversion is seemingly wrong because `start' is misnamed.
; It's now `end'.
(set! start (+ (- start flength) 1)))
(* (quotient (+ (remainder start diwb) flength (- diwb 1))
diwb)
diwb)
)
; Read an instruction field description.
; This is the main routine for analyzing instruction fields in the .cpu file.
; CONTEXT is a <context> object for error messages.
; ARG-LIST is an associative list of field name and field value.
; /ifield-parse is invoked to create the <ifield> object.
(define (/ifield-read context . arg-list)
(let (
(name #f)
(comment "")
(attrs nil)
(word-offset #f)
(word-length #f)
(start 0)
; FIXME: Hobbit computes the wrong symbol for `length'
; in the `case' expression below because there is a local var
; of the same name ("__1" gets appended to the symbol name).
; As a workaround we name it "length-".
(length- 0)
(follows #f)
(mode 'UINT)
(encode #f)
(decode #f)
)
; 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
((name) (set! name (cadr arg)))
((comment) (set! comment (cadr arg)))
((attrs) (set! attrs (cdr arg)))
((mode) (set! mode (cadr arg)))
((word-offset) (set! word-offset (cadr arg)))
((word-length) (set! word-length (cadr arg)))
((start) (set! start (cadr arg)))
((length) (set! length- (cadr arg)))
((follows) (set! follows (cadr arg)))
((encode) (set! encode (cdr arg)))
((decode) (set! decode (cdr arg)))
(else (parse-error context "invalid ifield arg" arg)))
(loop (cdr arg-list)))))
; See if encode/decode were specified as "unspecified".
; This happens with shorthand macros.
(if (and (pair? encode)
(eq? (car encode) #f))
(set! encode #f))
(if (and (pair? decode)
(eq? (car decode) #f))
(set! decode #f))
; Now that we've identified the elements, build the object.
(/ifield-parse context name comment attrs
word-offset word-length start length- follows
mode encode decode))
)
; Parse a `follows' spec.
(define (/ifld-parse-follows context follows isas)
(if follows
(let ((follows-obj (current-op-lookup follows isas)))
(if (not follows-obj)
(parse-error context "unknown operand to follow" follows))
follows-obj)
#f)
)
; Do common parts of <ifield> encode/decode processing.
(define (/ifld-parse-encode-decode context which value)
(if value
(begin
(if (or (not (list? value))
(not (= (length value) 2))
(not (list? (car value)))
(not (= (length (car value)) 2))
(not (list? (cadr value))))
(parse-error context
(string-append "bad ifield " which " spec")
value))
(if (or (not (> (length (cadr value)) 2))
(not (mode:lookup (cadr (cadr value)))))
(parse-error context
(string-append which " expression must have a mode")
value))))
value
)
; Parse an <ifield> encode spec.
(define (/ifld-parse-encode context encode)
(/ifld-parse-encode-decode context "encode" encode)
)
; Parse an <ifield> decode spec.
(define (/ifld-parse-decode context decode)
(/ifld-parse-encode-decode context "decode" decode)
)
; Define an instruction field object, name/value pair list version.
(define define-ifield
(lambda arg-list
(let ((f (apply /ifield-read (cons (make-current-context "define-ifield")
arg-list))))
(if f
(current-ifld-add! f))
f))
)
; Define an instruction field object, all arguments specified.
; ??? Leave out word-offset,word-length,follows for now (RISC version).
; FIXME: Eventually this should be fixed to take *all* arguments.
(define (define-full-ifield name comment attrs start length mode encode decode)
(let ((f (/ifield-parse (make-current-context "define-full-ifield")
name comment attrs
#f #f start length #f mode encode decode)))
(if f
(current-ifld-add! f))
f)
)
(define (/ifield-add-commands!)
(reader-add-command! 'define-ifield
"\
Define an instruction field, name/value pair list version.
"
nil 'arg-list define-ifield)
(reader-add-command! 'define-full-ifield
"\
Define an instruction field, all arguments specified.
"
nil '(name comment attrs start length mode encode decode)
define-full-ifield)
(reader-add-command! 'define-multi-ifield
"\
Define an instruction multi-field, name/value pair list version.
"
nil 'arg-list define-multi-ifield)
(reader-add-command! 'define-full-multi-ifield
"\
Define an instruction multi-field, all arguments specified.
"
nil '(name comment attrs mode subflds insert extract)
define-full-multi-ifield)
*UNSPECIFIED*
)
; Instruction fields consisting of multiple parts.
(define <multi-ifield>
(class-make '<multi-ifield>
'(<ifield>)
'(
; List of <ifield> objects.
subfields
; rtl to set SUBFIELDS from self
insert
; rtl to set self from SUBFIELDS
extract
)
nil)
)
(method-make-make! <multi-ifield> '(name comment attrs
mode bitrange encode decode
subfields insert extract))
; Accessors
(define-getters <multi-ifield> multi-ifld
(subfields insert extract)
)
; Return a boolean indicating if X is an <ifield>.
(define (multi-ifield? x) (class-instance? <multi-ifield> x))
(define (non-multi-ifields ifld-list)
(find (lambda (ifld) (not (multi-ifield? ifld))) ifld-list)
)
(define (non-derived-ifields ifld-list)
(find (lambda (ifld) (not (derived-ifield? ifld))) ifld-list)
)
; Return the starting bit number of the first field.
(method-make!
<multi-ifield> 'field-start
(lambda (self)
(apply min (map (lambda (f) (ifld-start f)) (elm-get self 'subfields))))
)
; Return the total length.
(method-make!
<multi-ifield> 'field-length
(lambda (self)
(apply + (map ifld-length (elm-get self 'subfields))))
)
; Return the bit offset of the word after the last word SELF is in.
; What a `word' here is defined by subfields in their bitranges.
(method-make!
<multi-ifield> 'next-word
(lambda (self)
(apply max (map (lambda (f)
(bitrange-next-word (/ifld-bitrange f)))
(multi-ifld-subfields self))))
)
; Return mask of field in bitrange CONTAINER.
(method-make!
<multi-ifield> 'field-mask
(lambda (self base-len container)
(apply + (map (lambda (f) (ifld-mask f base-len container)) (elm-get self 'subfields))))
)
; Return VALUE inserted into the field's position.
; The value is spread out over the various subfields in sorted order.
; We assume the subfields have been sorted by starting bit position.
(method-make!
<multi-ifield> 'field-value
(lambda (self base-len value)
(apply + (map (lambda (f) (ifld-value f base-len value)) (elm-get self 'subfields))))
)
; Return a list of ifields required to compute the field's value.
(method-make!
<multi-ifield> 'needed-iflds
(lambda (self)
(cons self (elm-get self 'subfields)))
)
; Extract <ifield> IFLD's value out of VALUE in <insn> INSN.
; VALUE is the entire insn's value if it fits in a word, or is a list
; of values, one per word (not implemented, sigh).
; ??? The instruction's format should specify where the word boundaries are.
(method-make!
<multi-ifield> 'field-extract
(lambda (self insn value)
(let* ((subflds (sort-ifield-list (elm-get self 'subfields)
(not (ifld-lsb0? self))))
(subvals (map (lambda (subfld)
(ifld-extract subfld insn value))
subflds))
)
; We have each subfield's value, now concatenate them.
(letrec ((plus-scan (lambda (lengths current)
; do the -1 drop here as it's easier
(if (null? (cdr lengths))
nil
(cons current
(plus-scan (cdr lengths)
(+ current (car lengths))))))))
(apply + (map logsll
subvals
(plus-scan (map ifld-length subflds) 0))))))
)
; Return a boolean indicating if bit 0 is the least significant bit.
(method-make!
<multi-ifield> 'field-lsb0?
(lambda (self)
(ifld-lsb0? (car (elm-get self 'subfields))))
)
;; Pretty print a multi-ifield, typically for error messages.
(method-make!
<multi-ifield> 'pretty-print
(lambda (self)
(string-append "(" (obj:str-name self)
(string-map (lambda (f)
(string-append " " (ifld-pretty-print f)))
(elm-get self 'subfields))
")"))
)
; Multi-ifield parsing.
; Subroutine of /multi-ifield-parse to build the default insert expression.
(define (/multi-ifield-make-default-insert container-name subfields)
(let* ((lengths (map ifld-length subfields))
(shifts (list-tail-drop 1 (plus-scan (cons 0 lengths)))))
; Build RTL expression to shift and mask each ifield into right spot.
(let ((exprs (map (lambda (f length shift)
(rtx-make 'and (rtx-make 'srl container-name shift)
(mask length)))
subfields lengths shifts)))
; Now set each ifield with their respective values.
(apply rtx-make (cons 'sequence
(cons nil
(map (lambda (f expr)
(rtx-make-set f expr))
subfields exprs))))))
)
; Subroutine of /multi-ifield-parse to build the default extract expression.
(define (/multi-ifield-make-default-extract container-name subfields)
(let* ((lengths (map ifld-length subfields))
(shifts (list-tail-drop 1 (plus-scan (cons 0 lengths)))))
; Build RTL expression to shift and mask each ifield into right spot.
(let ((exprs (map (lambda (f length shift)
(rtx-make 'sll (rtx-make 'and (obj:name f)
(mask length))
shift))
subfields lengths shifts)))
; Now set {container-name} with all the values or'd together.
(rtx-make-set container-name
(rtx-combine 'or exprs))))
)
; Parse a multi-ifield spec.
; This is the main routine for building the object from 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).
(define (/multi-ifield-parse context name comment attrs mode
subfields insert extract encode decode)
(logit 2 "Processing multi-ifield element " name " ...\n")
(if (null? subfields)
(parse-error context "empty subfield list" subfields))
;; Pick out name first to augment the error context.
(let* ((name (parse-name context name))
(context (context-append-name context name))
(atlist (atlist-parse context attrs "cgen_ifld"))
(isas (atlist-attr-value atlist 'ISA #f)))
; No longer ensure only one isa specified.
; (if (!= (length isas) 1)
; (parse-error context "can only specify 1 isa" attrs))
(if (keep-isa-atlist? atlist #f)
(begin
(let ((result (new <multi-ifield>))
(subfields (map (lambda (subfld)
(let ((f (current-ifld-lookup subfld)))
(if (not f)
(parse-error context "unknown ifield"
subfld))
f))
subfields)))
(elm-xset! result 'name name)
(elm-xset! result 'comment (parse-comment context comment))
(elm-xset! result 'attrs
;; multi-ifields are always VIRTUAL
(atlist-parse context (cons 'VIRTUAL attrs)
"multi-ifield"))
(elm-xset! result 'mode (parse-mode-name context mode))
(elm-xset! result 'encode (/ifld-parse-encode context encode))
(elm-xset! result 'decode (/ifld-parse-encode context decode))
(elm-xset! result 'bitrange "multi-ifields don't have bitranges") ;; FIXME
(if insert
(elm-xset! result 'insert insert)
(elm-xset! result 'insert
(/multi-ifield-make-default-insert name subfields)))
(if extract
(elm-xset! result 'extract extract)
(elm-xset! result 'extract
(/multi-ifield-make-default-extract name subfields)))
(elm-xset! result 'subfields subfields)
result))
; else don't keep isa
#f))
)
; Read an instruction multi-ifield.
; This is the main routine for analyzing multi-ifields in the .cpu file.
; CONTEXT is a <context> object for error messages.
; ARG-LIST is an associative list of field name and field value.
; /multi-ifield-parse is invoked to create the `multi-ifield' object.
(define (/multi-ifield-read context . arg-list)
(let (
(name nil)
(comment "")
(attrs nil)
(mode 'UINT)
(subflds nil)
(insert #f)
(extract #f)
(encode #f)
(decode #f)
)
; 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
((name) (set! name (cadr arg)))
((comment) (set! comment (cadr arg)))
((attrs) (set! attrs (cdr arg)))
((mode) (set! mode (cadr arg)))
((subfields) (set! subflds (cdr arg)))
((insert) (set! insert (cadr arg)))
((extract) (set! extract (cadr arg)))
((encode) (set! encode (cdr arg)))
((decode) (set! decode (cdr arg)))
(else (parse-error context "invalid ifield arg" arg)))
(loop (cdr arg-list)))))
; Now that we've identified the elements, build the object.
(/multi-ifield-parse context name comment attrs mode subflds
insert extract encode decode))
)
; Define an instruction multi-field object, name/value pair list version.
(define define-multi-ifield