-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.scm
2168 lines (1906 loc) · 63.2 KB
/
sim.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
; Simulator generator support routines.
; Copyright (C) 2000, 2001, 2002, 2006, 2009 Red Hat, Inc.
; This file is part of CGEN.
; One goal of this file is to provide cover functions for all methods.
; i.e. this file fills in the missing pieces of the interface between
; the application independent part of CGEN (i.e. the code loaded by read.scm)
; and the application dependent part (i.e. sim-*.scm).
; `send' is not intended to appear in sim-*.scm.
; [It still does but that's to be fixed.]
; Specify which application.
(set! APPLICATION 'SIMULATOR)
; Cover functions for various methods.
; Return the C type of something. This isn't always a mode.
(define (gen-type self) (send self 'gen-type))
; Return the C type of an index's value or #f if not needed (scalar).
(define (gen-index-type op sfmt)
(let ((index-mode (send op 'get-index-mode)))
(if index-mode
(mode:c-type index-mode)
#f))
)
; Misc. state info.
; Currently supported options:
; with-scache
; generate code to use the scache
; This is an all or nothing option, either scache is used or it's not.
; with-profile fn|sw
; generate code to do profiling in the semantic function
; code (fn) or in the semantic switch (sw)
; with-generic-write
; For architectures that have parallel execution.
; Execute the semantics by recording the results in a generic buffer,
; and doing a post-semantics writeback pass.
; with-parallel-only
; Only generate parallel versions of each insn.
; with-multiple-isa
; Enable multiple-isa support (eg. arm+thumb).
; copyright fsf|redhat
; emit an FSF or Cygnus copyright (temporary, pending decision)
; package gnusim|cygsim
; indicate the software package
; #t if the scache is being used
(define /with-scache? #f)
(define (with-scache?) /with-scache?)
; #t if we're generating profiling code
; Each of the function and switch semantic code can have profiling.
; The options as passed are stored in /with-profile-{fn,sw}?, and
; /with-profile? is set at code generation time.
(define /with-profile-fn? #f)
(define /with-profile-sw? #f)
(define /with-profile? #f)
(define (with-profile?) /with-profile?)
(define (with-any-profile?) (or /with-profile-fn? /with-profile-sw?))
; #t if multiple isa support is enabled
(define /with-multiple-isa? #f)
(define (with-multiple-isa?) /with-multiple-isa?)
; Handle parallel execution with generic writeback pass.
(define /with-generic-write? #f)
(define (with-generic-write?) /with-generic-write?)
; Only generate parallel versions of each insn.
(define /with-parallel-only? #f)
(define (with-parallel-only?) /with-parallel-only?)
; String containing copyright text.
(define CURRENT-COPYRIGHT #f)
; String containing text defining the package we're generating code for.
(define CURRENT-PACKAGE #f)
; Initialize the options.
(define (option-init!)
(set! /with-scache? #f)
(set! /with-profile-fn? #f)
(set! /with-profile-sw? #f)
(set! /with-multiple-isa? #f)
(set! /with-generic-write? #f)
(set! /with-parallel-only? #f)
(set! CURRENT-COPYRIGHT copyright-fsf)
(set! CURRENT-PACKAGE package-gnu-simulators)
*UNSPECIFIED*
)
; Handle an option passed in from the command line.
(define (option-set! name value)
(case name
((with-scache) (set! /with-scache? #t))
((with-profile) (cond ((equal? value '("fn"))
(set! /with-profile-fn? #t))
((equal? value '("sw"))
(set! /with-profile-sw? #t))
(else (error "invalid with-profile value" value))))
((with-multiple-isa) (set! /with-multiple-isa? #t))
((with-generic-write) (set! /with-generic-write? #t))
((with-parallel-only) (set! /with-parallel-only? #t))
((copyright) (cond ((equal? value '("fsf"))
(set! CURRENT-COPYRIGHT copyright-fsf))
((equal? value '("redhat"))
(set! CURRENT-COPYRIGHT copyright-red-hat))
(else (error "invalid copyright value" value))))
((package) (cond ((equal? value '("gnusim"))
(set! CURRENT-PACKAGE package-gnu-simulators))
((equal? value '("cygsim"))
(set! CURRENT-PACKAGE package-red-hat-simulators))
(else (error "invalid package value" value))))
(else (error "unknown option" name))
)
*UNSPECIFIED*
)
; #t if the cpu can execute insns parallely.
; This one isn't passed on the command line, but we follow the convention
; of prefixing these things with `with-'.
; While processing operand reading (or writing), parallel execution support
; needs to be turned off, so it is up to the appropriate cgen-foo.c proc to
; set-with-parallel?! appropriately.
(define /with-parallel? #f)
(define (with-parallel?) /with-parallel?)
(define (set-with-parallel?! flag) (set! /with-parallel? flag))
; Kind of parallel support.
; If 'read, read pre-processing is done.
; If 'write, write post-processing is done.
; ??? At present we always use write post-processing, though the previous
; version used read pre-processing. Not sure supporting both is useful
; in the long run.
(define /with-parallel-kind 'write)
; #t if parallel support is provided by read pre-processing.
(define (with-parallel-read?)
(and /with-parallel? (eq? /with-parallel-kind 'read))
)
; #t if parallel support is provided by write post-processing.
(define (with-parallel-write?)
(and /with-parallel? (eq? /with-parallel-kind 'write))
)
; Misc. utilities.
; All machine generated cpu elements are accessed through a cover macro
; to hide the details of the underlying implementation.
(define c-cpu-macro "CPU")
(define (gen-cpu-ref sym)
(string-append c-cpu-macro " (" sym ")")
)
; Return C code to fetch a value from instruction memory.
; PC-VAR is the C expression containing the address of the start of the
; instruction.
; ??? Aligned/unaligned support?
(define (gen-ifetch pc-var bitoffset bitsize)
(string-append "GETIMEM"
(case bitsize
((8) "UQI")
((16) "UHI")
((32) "USI")
(else (error "bad bitsize argument to gen-ifetch" bitsize)))
" (current_cpu, "
pc-var " + " (number->string (quotient bitoffset 8))
")")
)
; Instruction field support code.
; Return a <c-expr> object of the value of an ifield.
(define (/cxmake-ifld-val mode f)
(if (with-scache?)
; ??? Perhaps a better way would be to defer evaluating the src of a
; set until the method processing the dest.
(cx:make-with-atlist mode (gen-ifld-argbuf-ref f)
(atlist-make "" (bool-attr-make 'CACHED #t)))
(cx:make mode (gen-extracted-ifld-value f)))
)
; Type system.
; Methods:
; gen-type - return C code representing the type
; gen-sym-defn - generate decl using the provided symbol
; gen-sym-get-macro - generate GET macro for accessing CPU elements
; gen-sym-set-macro - generate SET macro for accessing CPU elements
; Scalar type
(method-make!
<scalar> 'gen-type
(lambda (self) (mode:c-type (elm-get self 'mode)))
)
(method-make!
<scalar> 'gen-sym-defn
(lambda (self sym comment)
(string-append
" /* " comment " */\n"
" " (send self 'gen-type) " "
(gen-c-symbol sym) ";\n"))
)
(method-make!
<scalar> 'gen-sym-get-macro
(lambda (self sym comment)
(let ((sym (gen-c-symbol sym)))
(gen-get-macro sym "" (gen-cpu-ref sym))))
)
(method-make!
<scalar> 'gen-sym-set-macro
(lambda (self sym comment)
(let ((sym (gen-c-symbol sym)))
(gen-set-macro sym "" (gen-cpu-ref sym))))
)
(method-make! <scalar> 'gen-ref (lambda (self sym index estate) sym))
; Array type
(method-make!
<array> 'gen-type
(lambda (self) (mode:c-type (elm-get self 'mode)))
)
(method-make!
<array> 'gen-sym-defn
(lambda (self sym comment)
(string-append
" /* " comment " */\n"
" " (send self 'gen-type) " "
(gen-c-symbol sym)
(gen-array-ref (elm-get self 'dimensions))
";\n")
)
)
(method-make!
<array> 'gen-sym-get-macro
(lambda (self sym comment)
(let ((sym (gen-c-symbol sym))
(rank (length (elm-get self 'dimensions))))
(string-append
"#define GET_" (string-upcase sym)
"(" (string-drop 2 (gen-macro-args rank)) ") "
(gen-cpu-ref sym) (gen-array-ref (macro-args rank)) "\n"
)))
)
(method-make!
<array> 'gen-sym-set-macro
(lambda (self sym comment)
(let ((sym (gen-c-symbol sym))
(rank (length (elm-get self 'dimensions))))
(string-append
"#define SET_" (string-upcase sym)
"(" (string-drop 2 (gen-macro-args rank)) ", x) "
"(" (gen-cpu-ref sym) (gen-array-ref (macro-args rank))
" = (x))\n"
)))
)
; Return a reference to the array.
; SYM is the name of the array.
; INDEX is either a single index object or a (possibly empty) list of objects,
; one object per dimension.
(method-make!
<array> 'gen-ref
(lambda (self sym index estate)
(let ((gen-index1 (lambda (idx)
(string-append "["
(/gen-hw-index idx estate)
"]"))))
(string-append sym
(cond ((list? index) (string-map gen-index1 index))
(else (gen-index1 index))))))
)
; Integers
;
;(method-make!
; <integer> 'gen-type
; (lambda (self)
; (mode:c-type (mode-find (elm-get self 'bits)
; (if (has-attr? self 'UNSIGNED)
; 'UINT 'INT)))
; )
;)
;
;(method-make! <integer> 'gen-sym-defn (lambda (self sym comment) ""))
;(method-make! <integer> 'gen-sym-get-macro (lambda (self sym comment) ""))
;(method-make! <integer> 'gen-sym-set-macro (lambda (self sym comment) ""))
; Hardware descriptions support code.
;
; Various operations are required for each h/w object to support the various
; things the simulator will want to do with it.
;
; Methods:
; gen-type - C type to use to record value, as a string.
; ??? Delete and just use get-mode?
; gen-defn - generate a definition of the h/w element
; gen-get-macro - Generate definition of the GET access macro.
; gen-set-macro - Generate definition of the SET access macro.
; gen-write - Same as gen-read except done on output operands
; cxmake-get - Return a <c-expr> object to fetch the value.
; gen-set-quiet - Set the value.
; ??? Could just call this gen-set as there is no gen-set-trace
; but for consistency with the messages passed to operands
; we use this same.
; save-index? - return #t if an index needs to be saved for parallel
; execution post-write processing
; gen-profile-decl
; gen-record-profile
; get-mode
; gen-profile-locals
; gen-sym-get-macro - Generate default GET access macro.
; gen-sym-set-macro - Generate default SET access macro.
; gen-ref - Return a C reference to the object.
; gen-type handler, must be overridden.
(method-make!
<hardware-base> 'gen-type
(lambda (self) (error "gen-type not overridden:" self))
)
; Generate CPU state struct entries, must be overridden.
(method-make!
<hardware-base> 'gen-defn
(lambda (self) (error "gen-defn not overridden:" self))
)
(method-make! <hardware-base> 'gen-sym-decl (lambda (self sym comment) ""))
; Return a C reference to a hardware object.
(method-make! <hardware-base> 'gen-ref (lambda (self sym index estate) sym))
; Each hardware type must provide its own gen-write method.
(method-make!
<hardware-base> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
(error "gen-write method not overridden:" self))
)
(method-make! <hardware-base> 'gen-profile-decl (lambda (self) ""))
; Default gen-record-profile method.
(method-make!
<hardware-base> 'gen-record-profile
(lambda (self index sfmt estate)
"") ; nothing to do
)
; Default cxmake-get method.
; Return a <c-expr> object of the value of SELF.
; ESTATE is the current rtl evaluator state.
; INDEX is a <hw-index> object. It must be an ifield.
; SELECTOR is a hardware selector RTX.
(method-make!
<hardware-base> 'cxmake-get
(lambda (self estate mode index selector)
(if (not (eq? 'ifield (hw-index:type index)))
(error "not an ifield hw-index" index))
(/cxmake-ifld-val mode (hw-index:value index)))
)
; Handle gen-get-macro/gen-set-macro.
(method-make!
<hardware-base> 'gen-get-macro
(lambda (self)
"")
)
(method-make!
<hardware-base> 'gen-set-macro
(lambda (self)
"")
)
; PC support
; 'gen-set-quiet helper for PC values.
; NEWVAL is a <c-expr> object of the value to be assigned.
; If OPTIONS contains #:direct, set the PC directly, bypassing semantic
; code considerations.
; ??? OPTIONS support wip. Probably want a new form (or extend existing form)
; of rtx: that takes a variable number of named arguments.
; ??? Another way to get #:direct might be (raw-reg h-pc).
(define (/hw-gen-set-quiet-pc self estate mode index selector newval . options)
(if (not (send self 'pc?)) (error "Not a PC:" self))
(cond ((memq #:direct options)
(/hw-gen-set-quiet self estate mode index selector newval))
((has-attr? newval 'CACHED)
(string-append "SEM_BRANCH_VIA_CACHE (current_cpu, sem_arg, "
(cx:c newval)
", vpc);\n"))
(else
(string-append "SEM_BRANCH_VIA_ADDR (current_cpu, sem_arg, "
(cx:c newval)
", vpc);\n")))
)
(method-make! <hw-pc> 'gen-set-quiet /hw-gen-set-quiet-pc)
; Handle updates of the pc during parallel execution.
; This is done in a post-processing pass after semantic evaluation.
; SFMT is the <sformat>.
; OP is the operand.
; ACCESS-MACRO is the runtime C macro to use to fetch indices computed
; during semantic evaluation.
;
; ??? This wouldn't be necessary if gen-set-quiet were a virtual method.
; At this point I'm reluctant to willy nilly make methods virtual.
(method-make!
<hw-pc> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
(string-append " "
(send self 'gen-set-quiet estate VOID index hw-selector-default
(cx:make DFLT (string-append access-macro
" (" (gen-sym op) ")")))))
)
(method-make!
<hw-pc> 'cxmake-skip
(lambda (self estate yes?)
(cx:make VOID
(string-append "if ("
yes?
")\n"
" SEM_SKIP_INSN (current_cpu, sem_arg, vpc);\n")))
)
; Registers.
(method-make-forward! <hw-register> 'type '(gen-type))
(method-make!
<hw-register> 'gen-defn
(lambda (self)
(send (elm-get self 'type) 'gen-sym-defn (obj:name self) (obj:comment self)))
)
(method-make-forward! <hw-register> 'type '(gen-ref
gen-sym-get-macro
gen-sym-set-macro))
; For parallel instructions supported by queueing outputs for later update,
; return a boolean indicating if an index needs to be recorded.
; An example of when the index isn't needed is if the index can be determined
; during extraction.
(method-make!
<hw-register> 'save-index?
(lambda (self op)
; FIXME: Later handle case where register number is determined at runtime.
#f)
)
; Handle updates of registers during parallel execution.
; This is done in a post-processing pass after semantic evaluation.
; SFMT is the <sformat>.
; OP is the <operand>.
; ACCESS-MACRO is the runtime C macro to use to fetch indices computed
; during semantic evaluation.
; FIXME: May need mode of OP.
(method-make!
<hw-register> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
; First get a hw-index object to use during indexing.
; Some indices, e.g. memory addresses, are computed during semantic
; evaluation. Others are computed during the extraction phase.
(let ((index (send index 'get-write-index self sfmt op access-macro)))
(string-append " "
(send self 'gen-set-quiet estate mode index hw-selector-default
(cx:make DFLT (string-append access-macro
" (" (gen-sym op) ")"))))))
)
(method-make!
<hw-register> 'gen-profile-decl
(lambda (self)
(string-append
" /* " (obj:comment self) " */\n"
" unsigned long " (gen-c-symbol (obj:name self)) ";\n"))
)
(method-make!
<hw-register> 'gen-record-profile
(lambda (self index sfmt estate)
; FIXME: Need to handle scalars.
(/gen-hw-index-raw index estate))
)
(method-make!
<hw-register> 'gen-get-macro
(lambda (self)
(let ((getter (elm-get self 'get))
(mode (send self 'get-mode)))
(if getter
(let ((args (car getter))
(expr (cadr getter)))
(gen-get-macro2 (gen-sym self)
(if (hw-scalar? self) "" "index")
(rtl-c mode
#f ;; h/w is not ISA-specific
(if (hw-scalar? self)
nil
(list (list (car args) 'UINT "index")))
expr
#:rtl-cover-fns? #t #:macro? #t)))
(send self 'gen-sym-get-macro
(obj:name self) (obj:comment self)))))
)
(method-make!
<hw-register> 'gen-set-macro
(lambda (self)
(let ((setter (elm-get self 'set))
(mode (send self 'get-mode)))
(if setter
(let ((args (car setter))
(expr (cadr setter)))
(gen-set-macro2 (gen-sym self)
(if (hw-scalar? self) "" "index")
"x"
(rtl-c VOID ;; not `mode', sets have mode VOID
#f ;; h/w is not ISA-specific
(if (hw-scalar? self)
(list (list (car args) (hw-mode self) "(x)"))
(list (list (car args) 'UINT "(index)")
(list (cadr args) (hw-mode self) "(x)")))
expr
#:rtl-cover-fns? #t #:macro? #t)))
(send self 'gen-sym-set-macro
(obj:name self) (obj:comment self)))))
)
; Utility to build a <c-expr> object to fetch the value of a register.
(define (/hw-cxmake-get hw estate mode index selector)
(let ((mode (if (mode:eq? 'DFLT mode)
(send hw 'get-mode)
mode))
(getter (hw-getter hw)))
; If the register is accessed via a cover function/macro, do it.
; Otherwise fetch the value from the cached address or from the CPU struct.
(cx:make mode
(cond (getter
(let ((scalar? (hw-scalar? hw))
(c-index (/gen-hw-index index estate)))
(string-append "GET_"
(string-upcase (gen-sym hw))
" ("
(if scalar? "" c-index)
")")))
((and (hw-cache-addr? hw) ; FIXME: redo test
(eq? 'ifield (hw-index:type index)))
(string-append
"* "
(if (with-scache?)
(gen-hw-index-argbuf-ref index)
(gen-hw-index-argbuf-name index))))
(else (gen-cpu-ref (send hw 'gen-ref
(gen-sym hw) index estate))))))
)
(method-make! <hw-register> 'cxmake-get /hw-cxmake-get)
; raw-reg: support
; ??? raw-reg: support is wip
(method-make!
<hw-register> 'cxmake-get-raw
(lambda (self estate mode index selector)
(let ((mode (if (mode:eq? 'DFLT mode)
(send self 'get-mode)
mode)))
(cx:make mode (gen-cpu-ref (send self 'gen-ref
(gen-sym self) index estate)))))
)
; Utilities to generate C code to assign a variable to a register.
(define (/hw-gen-set-quiet hw estate mode index selector newval)
(let ((setter (hw-setter hw)))
(cond (setter
(let ((scalar? (hw-scalar? hw))
(c-index (/gen-hw-index index estate)))
(string-append "SET_"
(string-upcase (gen-sym hw))
" ("
(if scalar? "" (string-append c-index ", "))
(cx:c newval)
");\n")))
((and (hw-cache-addr? hw) ; FIXME: redo test
(eq? 'ifield (hw-index:type index)))
(string-append "* "
(if (with-scache?)
(gen-hw-index-argbuf-ref index)
(gen-hw-index-argbuf-name index))
" = " (cx:c newval) ";\n"))
(else (string-append (gen-cpu-ref (send hw 'gen-ref
(gen-sym hw) index estate))
" = " (cx:c newval) ";\n"))))
)
(method-make! <hw-register> 'gen-set-quiet /hw-gen-set-quiet)
; raw-reg: support
; ??? wip
(method-make!
<hw-register> 'gen-set-quiet-raw
(lambda (self estate mode index selector newval)
(string-append (gen-cpu-ref (send self 'gen-ref
(gen-sym self) index estate))
" = " (cx:c newval) ";\n"))
)
; Return name of C access function for getting/setting a register.
(define (gen-reg-getter-fn hw prefix)
(string-append prefix "_" (gen-sym hw) "_get")
)
(define (gen-reg-setter-fn hw prefix)
(string-append prefix "_" (gen-sym hw) "_set")
)
; Generate decls for access fns of register HW, beginning with
; PREFIX, using C type TYPE.
; SCALAR? is #t if the register is a scalar. Otherwise it is #f and the
; register is a bank of registers.
(define (gen-reg-access-decl hw prefix type scalar?)
(string-append
type " "
(gen-reg-getter-fn hw prefix)
" (SIM_CPU *"
(if scalar? "" ", UINT")
");\n"
"void "
(gen-reg-setter-fn hw prefix)
" (SIM_CPU *, "
(if scalar? "" "UINT, ")
type ");\n"
)
)
; Generate defns of access fns of register HW, beginning with
; PREFIX, using C type TYPE.
; SCALAR? is #t if the register is a scalar. Otherwise it is #f and the
; register is a bank of registers.
; GET/SET-CODE are C fragments to get/set the value.
; ??? Inlining left for later.
(define (gen-reg-access-defn hw prefix type scalar? get-code set-code)
(string-append
"/* Get the value of " (obj:str-name hw) ". */\n\n"
type "\n"
(gen-reg-getter-fn hw prefix)
" (SIM_CPU *current_cpu"
(if scalar? "" ", UINT regno")
")\n{\n"
get-code
"}\n\n"
"/* Set a value for " (obj:str-name hw) ". */\n\n"
"void\n"
(gen-reg-setter-fn hw prefix)
" (SIM_CPU *current_cpu, "
(if scalar? "" "UINT regno, ")
type " newval)\n"
"{\n"
set-code
"}\n\n")
)
; Memory support.
(method-make!
<hw-memory> 'cxmake-get
(lambda (self estate mode index selector)
(let ((mode (if (mode:eq? 'DFLT mode)
(hw-mode self)
mode))
(default-selector? (hw-selector-default? selector)))
(cx:make mode
(string-append "GETMEM" (obj:str-name mode)
(if default-selector? "" "ASI")
" ("
"current_cpu, pc, "
(/gen-hw-index index estate)
(if default-selector?
""
(string-append ", "
(/gen-hw-selector selector)))
")"))))
)
(method-make!
<hw-memory> 'gen-set-quiet
(lambda (self estate mode index selector newval)
(let ((mode (if (mode:eq? 'DFLT mode)
(hw-mode self)
mode))
(default-selector? (hw-selector-default? selector)))
(string-append "SETMEM" (obj:str-name mode)
(if default-selector? "" "ASI")
" ("
"current_cpu, pc, "
(/gen-hw-index index estate)
(if default-selector?
""
(string-append ", "
(/gen-hw-selector selector)))
", " (cx:c newval) ");\n")))
)
(method-make-forward! <hw-memory> 'type '(gen-type))
(method-make! <hw-memory> 'gen-defn (lambda (self sym comment) ""))
(method-make! <hw-memory> 'gen-sym-get-macro (lambda (self sym comment) ""))
(method-make! <hw-memory> 'gen-sym-set-macro (lambda (self sym comment) ""))
; For parallel instructions supported by queueing outputs for later update,
; return the type of the index or #f if not needed.
(method-make!
<hw-memory> 'save-index?
(lambda (self op)
; In the case of the complete memory address being an immediate
; argument, we can return #f (later).
AI)
)
(method-make!
<hw-memory> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
(let ((index (send index 'get-write-index self sfmt op access-macro)))
(string-append " "
(send self 'gen-set-quiet estate mode index
hw-selector-default
(cx:make DFLT (string-append access-macro " ("
(gen-sym op)
")"))))))
)
; Immediates, addresses.
(method-make-forward! <hw-immediate> 'type '(gen-type))
(method-make!
<hw-immediate> 'gen-defn
(lambda (self)
(send (elm-get self 'type) 'gen-sym-defn (obj:name self) (obj:comment self)))
)
(method-make-forward! <hw-immediate> 'type '(gen-sym-get-macro
gen-sym-set-macro))
(method-make!
<hw-immediate> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
(error "gen-write of <hw-immediate> shouldn't happen"))
)
;; FIXME
(method-make! <hw-address> 'gen-type (lambda (self) "ADDR"))
(method-make! <hw-address> 'gen-defn (lambda (self sym comment) ""))
(method-make! <hw-address> 'gen-sym-get-macro (lambda (self sym comment) ""))
(method-make! <hw-address> 'gen-sym-set-macro (lambda (self sym comment) ""))
; Return a <c-expr> object of the value of SELF.
; ESTATE is the current rtl evaluator state.
; INDEX is a hw-index object. It must be an ifield.
; Needed because we record our own copy of the ifield in ARGBUF.
; SELECTOR is a hardware selector RTX.
(method-make!
<hw-address> 'cxmake-get
(lambda (self estate mode index selector)
(if (not (eq? 'ifield (hw-index:type index)))
(error "not an ifield hw-index" index))
(if (with-scache?)
(cx:make mode (gen-hw-index-argbuf-ref index))
(cx:make mode (gen-hw-index-argbuf-name index))))
)
(method-make!
<hw-address> 'gen-write
(lambda (self estate index mode sfmt op access-macro)
(error "gen-write of <hw-address> shouldn't happen"))
)
;; FIXME: consistency says there should be gen-defn, gen-sym-[gs]et-macro
(method-make! <hw-iaddress> 'gen-type (lambda (self) "IADDR"))
; Return a <c-expr> object of the value of SELF.
; ESTATE is the current rtl evaluator state.
; INDEX is a <hw-index> object. It must be an ifield.
; Needed because we record our own copy of the ifield in ARGBUF,
; *and* because we want to record in the result the 'CACHED attribute
; since instruction addresses based on ifields are fixed [and thus cacheable].
; SELECTOR is a hardware selector RTX.
(method-make!
<hw-iaddress> 'cxmake-get
(lambda (self estate mode index selector)
(if (not (eq? 'ifield (hw-index:type index)))
(error "not an ifield hw-index" index))
(if (with-scache?)
; ??? Perhaps a better way would be to defer evaluating the src of a
; set until the method processing the dest.
(cx:make-with-atlist mode (gen-hw-index-argbuf-ref index)
(atlist-make "" (bool-attr-make 'CACHED #t)))
(cx:make mode (gen-hw-index-argbuf-name index))))
)
; Hardware index support code.
; Return the index to use by the gen-write method.
; In the cases where this is needed (the index isn't known until insn
; execution time), the index is computed along with the value to be stored,
; so this is easy.
(method-make!
<hw-index> 'get-write-index
(lambda (self hw sfmt op access-macro)
(if (memq (hw-index:type self) '(scalar constant enum str-expr ifield))
self
(let ((index-mode (send hw 'get-index-mode)))
(if index-mode
(make <hw-index> 'anonymous 'str-expr index-mode
(string-append access-macro " (" (/op-index-name op) ")"))
(hw-index-scalar)))))
)
; Return the name of the PAREXEC structure member holding a hardware index
; for operand OP.
(define (/op-index-name op)
(string-append (gen-sym op) "_idx")
)
; Cover fn to hardware indices to generate the actual C code.
; INDEX is the hw-index object (i.e. op:index).
; The result is a string of C code.
; FIXME:wip
(define (/gen-hw-index-raw index estate)
(let ((type (hw-index:type index))
(mode (hw-index:mode index))
(value (hw-index:value index)))
(case type
((scalar) "")
; special case UINT to cut down on unnecessary verbosity.
; ??? May wish to handle more similarily.
((constant) (if (mode:eq? 'UINT mode)
(number->string value)
(string-append "((" (mode:c-type mode) ") "
(number->string value)
")")))
((enum) (let ((sym (hw-index-enum-name index))
(obj (hw-index-enum-obj index)))
(gen-enum-sym obj sym)))
((str-expr) value)
((rtx) (rtl-c-with-estate estate mode value))
((ifield) (if (= (ifld-length value) 0)
""
(gen-extracted-ifld-value value)))
((operand) (cx:c (send value 'cxmake-get estate mode (op:index value)
(op:selector value) #f)))
(else (error "/gen-hw-index-raw: invalid index:" index))))
)
; Same as /gen-hw-index-raw except used where speedups are possible.
; e.g. doing array index calcs at extraction time.
(define (/gen-hw-index index estate)
(let ((type (hw-index:type index))
(mode (hw-index:mode index))
(value (hw-index:value index)))
(case type
((scalar) "")
((constant) (string-append "((" (mode:c-type mode) ") "
(number->string value)
")"))
((enum) (let ((sym (hw-index-enum-name index))
(obj (hw-index-enum-obj index)))
(gen-enum-sym obj sym)))
((str-expr) value)
((rtx) (rtl-c-with-estate estate mode value))
((ifield) (if (= (ifld-length value) 0)
""
(cx:c (/cxmake-ifld-val mode value))))
((operand) (cx:c (send value 'cxmake-get estate mode (op:index value)
(op:selector value))))
(else (error "/gen-hw-index: invalid index:" index))))
)
; Return address where HW is stored.
(define (/gen-hw-addr hw estate index)
(let ((setter (hw-setter hw)))
(cond ((and (hw-cache-addr? hw) ; FIXME: redo test
(eq? 'ifield (hw-index:type index)))
(if (with-scache?)
(gen-hw-index-argbuf-ref index)
(gen-hw-index-argbuf-name index)))
(else
(string-append "& "
(gen-cpu-ref (send hw 'gen-ref
(gen-sym hw) index estate))))))
)
; Return a <c-expr> object of the value of a hardware index.
(method-make!
<hw-index> 'cxmake-get
(lambda (self estate mode)
(let ((mode (if (mode:eq? 'DFLT mode) (elm-get self 'mode) mode)))
; If MODE is VOID, abort.
(if (mode:eq? 'VOID mode)
(error "hw-index:cxmake-get: result needs a mode" self))
(cx:make (if (mode:host? mode)
; FIXME: Temporary hack to generate same code as before.
(let ((xmode (object-copy mode)))
(obj-cons-attr! xmode (bool-attr-make 'FORCE-C #t))
xmode)
mode)
(/gen-hw-index self estate))))
)
; Hardware selector support code.
; Generate C code for SEL.
(define (/gen-hw-selector sel)
(rtl-c INT #f nil sel)
)
; Instruction operand support code.
; Methods:
; gen-type - Return C type to use to hold operand's value.
; gen-read - Record an operand's value prior to parallely executing
; several instructions. Not used if gen-write used.
; gen-write - Write back an operand's value after parallely executing
; several instructions. Not used if gen-read used.
; cxmake-get - Return C code to fetch the value of an operand.
; gen-set-quiet - Return C code to set the value of an operand.
; gen-set-trace - Return C code to set the value of an operand, and print
; a result trace message. ??? Ideally this will go away when
; trace record support is complete.
; Return the C type of an operand.
; Generally we forward things on to TYPE, but for the actual type we need to
; use the get-mode method.
;(method-make-forward! <operand> 'type '(gen-type))
(method-make!
<operand> 'gen-type
(lambda (self)
; First get the mode.
(let ((mode (send self 'get-mode)))
; If it's VOID use the type's type.
(if (mode:eq? 'DFLT mode)
(send (op:type self) 'gen-type)
(mode:c-type mode))))
)
; Extra pc operand methods.
(method-make!
<pc> 'cxmake-get