-
Notifications
You must be signed in to change notification settings - Fork 0
/
sid-cpu.scm
1458 lines (1254 loc) · 40.5 KB
/
sid-cpu.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
; CPU family related simulator generator, excluding decoding and model support.
; Copyright (C) 2000, 2002, 2003, 2005, 2006, 2009, 2010 Red Hat, Inc.
; This file is part of CGEN.
; ***********
; cgen-desc.h
(define (/last-insn)
(string-upcase (gen-c-symbol (caar (list-take -1
(gen-obj-list-enums (non-multi-insns (current-insn-list))))))))
; Declare the attributes.
(define (/gen-attr-decls)
(string-list
"// Insn attribute indices.\n\n"
(gen-attr-enum-decl "cgen_insn" (current-insn-attr-list))
"// Attributes.\n\n"
(string-list-map gen-decl (current-attr-list))
)
)
; Generate class to hold an instruction's attributes.
(define (/gen-insn-attr-decls)
(let ((attrs (current-insn-attr-list)))
(string-append
"// Insn attributes.\n\n"
; FIXME: maybe make class, but that'll require a constructor. Later.
"struct @arch@_insn_attr {\n"
" unsigned int bools;\n"
(string-map (lambda (attr)
(if (bool-attr? attr)
""
(string-append " "
(gen-attr-type attr)
" "
(string-downcase (gen-sym attr))
";\n")))
attrs)
;"public:\n"
(string-map (lambda (attr)
(string-append
" inline "
(gen-attr-type attr)
" get_" (string-downcase (gen-sym attr)) "_attr"
" () { return "
(if (bool-attr? attr)
(string-append "(bools & "
(gen-attr-mask "cgen_insn" (obj:name attr))
") != 0")
(string-downcase (gen-sym attr)))
"; }\n"))
attrs)
"};\n\n"
))
)
; Emit a macro that specifies the word-bitsize for each machine.
(define (/gen-mach-params)
(string-map (lambda (mach)
(string-append
"#define MACH_" (string-upcase (gen-sym mach)) "_INSN_CHUNK_BITSIZE "
(number->string (cpu-insn-chunk-bitsize (mach-cpu mach))) "\n"))
(current-mach-list))
)
; Generate <cpu>-desc.h.
(define (cgen-desc.h)
(logit 1 "Generating " (gen-cpu-name) "-desc.h ...\n")
(string-write
(gen-c-copyright "Misc. entries in the @arch@ description file."
copyright-red-hat package-red-hat-simulators)
"\
#ifndef DESC_@ARCH@_H
#define DESC_@ARCH@_H
#include \"cgen/bitset.h\"
namespace @arch@ {
\n"
(let ((enums (find (lambda (obj) (not (obj-has-attr? obj 'VIRTUAL)))
(current-enum-list))))
(if (null? enums)
""
(string-list
"// Enums.\n\n"
(string-map gen-decl enums))))
/gen-attr-decls
/gen-insn-attr-decls
/gen-mach-params
"
} // end @arch@ namespace
#endif /* DESC_@ARCH@_H */\n"
)
)
; **********
; cgen-cpu.h
; Print out file containing elements to add to cpu class.
; Get/set fns for hardware element HW.
(define (/gen-reg-access-defns hw)
(let ((scalar? (hw-scalar? hw))
(name (obj:name hw))
(getter (hw-getter hw))
(setter (hw-setter hw))
(type (gen-type hw)))
(let ((get-code (if getter
(let ((mode (hw-mode hw))
(args (car getter))
(expr (cadr getter)))
(string-append
"return "
(rtl-c++ mode
#f ;; h/w is not ISA-specific
(if scalar?
nil
(list (list (car args) 'UINT "regno")))
expr
#:rtl-cover-fns? #t)
";"))
(string-append
"return this->hardware."
(gen-c-symbol name)
(if scalar? "" "[regno]")
";")))
(set-code (if setter
(let ((args (car setter))
(expr (cadr setter)))
(rtl-c++
VOID ; not `mode', sets have mode VOID
#f ;; h/w is not ISA-specific
(if scalar?
(list (list (car args) (hw-mode hw) "newval"))
(list (list (car args) 'UINT "regno")
(list (cadr args) (hw-mode hw) "newval")))
expr
#:rtl-cover-fns? #t))
(string-append
"this->hardware."
(gen-c-symbol name)
(if scalar? "" "[regno]")
" = newval;"))))
(string-append
" inline " type " "
(gen-reg-get-fun-name hw)
" ("
(if scalar? "" "UINT regno")
") const"
" { " get-code " }"
"\n"
" inline void "
(gen-reg-set-fun-name hw)
" ("
(if scalar? "" "UINT regno, ")
type " newval)"
" { " set-code " }"
"\n\n")))
)
; Return a boolean indicating if hardware element HW needs storage allocated
; for it in the SIM_CPU struct.
(define (hw-need-storage? hw)
(and (register? hw)
(not (obj-has-attr? hw 'VIRTUAL)))
)
(define (hw-need-write-stack? hw)
(and (register? hw) (hw-used-in-delay-rtl? hw))
)
; Subroutine of /gen-hardware-types to generate the struct containing
; hardware elements of one isa.
(define (/gen-hardware-struct prefix hw-list)
(if (null? hw-list)
; If struct is empty, leave it out to simplify generated code.
""
(string-list
(if prefix
(string-append " // Hardware elements for " prefix ".\n")
" // Hardware elements.\n")
" struct {\n"
(string-list-map gen-defn hw-list)
" } "
(if prefix
(string-append prefix "_")
"")
"hardware;\n\n"
))
)
; Return C type declarations of all of the hardware elements.
; The name of the type is prepended with the cpu family name.
(define (/gen-hardware-types)
(string-list
"// CPU state information.\n\n"
(/gen-hardware-struct #f (find hw-need-storage? (current-hw-list))))
)
(define (/gen-hw-stream-and-destream-fns)
(let* ((sa string-append)
(regs (find hw-need-storage? (current-hw-list)))
(stack-regs (find hw-need-write-stack? (current-hw-list)))
(reg-dim (lambda (r)
(let ((dims (/hw-vector-dims r)))
(if (equal? 0 (length dims))
"0"
(number->string (car dims))))))
(write-stacks
(map (lambda (n) (sa n "_writes"))
(append (map (lambda (r) (gen-c-symbol (obj:name r))) stack-regs)
(map (lambda (m) (sa (symbol->string m) "_memory")) write-stack-memory-mode-names))))
(stream-reg (lambda (r)
(let ((rname (sa "hardware." (gen-c-symbol (obj:name r)))))
(if (hw-scalar? r)
(sa " ost << " rname " << ' ';\n")
(sa " for (int i = 0; i < " (reg-dim r)
"; i++)\n ost << " rname "[i] << ' ';\n")))))
(destream-reg (lambda (r)
(let ((rname (sa "hardware." (gen-c-symbol (obj:name r)))))
(if (hw-scalar? r)
(sa " ist >> " rname ";\n")
(sa " for (int i = 0; i < " (reg-dim r)
"; i++)\n ist >> " rname "[i];\n")))))
(stream-stacks (lambda (s) (sa " stream_stacks ( stacks." s ", ost);\n")))
(destream-stacks (lambda (s) (sa " destream_stacks ( stacks." s ", ist);\n")))
(stack-boilerplate
(sa
" template <typename ST> \n"
" void stream_stacks (const ST &st, std::ostream &ost) const\n"
" {\n"
" for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
" {\n"
" ost << st[i].t << ' ';\n"
" for (int j = 0; j <= st[i].t; j++)\n"
" {\n"
" ost << st[i].buf[j].pc << ' ';\n"
" ost << st[i].buf[j].val << ' ';\n"
" ost << st[i].buf[j].idx0 << ' ';\n"
" }\n"
" }\n"
" }\n"
" \n"
" template <typename ST> \n"
" void destream_stacks (ST &st, std::istream &ist)\n"
" {\n"
" for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
" {\n"
" ist >> st[i].t;\n"
" for (int j = 0; j <= st[i].t; j++)\n"
" {\n"
" ist >> st[i].buf[j].pc;\n"
" ist >> st[i].buf[j].val;\n"
" ist >> st[i].buf[j].idx0;\n"
" }\n"
" }\n"
" }\n"
" \n")))
(sa
" void stream_cgen_hardware (std::ostream &ost) const \n {\n"
(string-map stream-reg regs)
" }\n"
" void destream_cgen_hardware (std::istream &ist) \n {\n"
(string-map destream-reg regs)
" }\n"
(if (with-parallel?)
(sa stack-boilerplate
" void stream_cgen_write_stacks (std::ostream &ost, "
"const @prefix@::write_stacks &stacks) const \n {\n"
(string-map stream-stacks write-stacks)
" }\n"
" void destream_cgen_write_stacks (std::istream &ist, "
"@prefix@::write_stacks &stacks) \n {\n"
(string-map destream-stacks write-stacks)
" }\n")
""))))
; Generate <cpu>-cpu.h
(define (cgen-cpu.h)
(logit 1 "Generating " (gen-cpu-name) "-cpu.h ...\n")
(assert-keep-one)
; Turn parallel execution support on if cpu needs it.
(set-with-parallel?! (state-parallel-exec?))
; Initialize rtl->c generation.
(rtl-c-config! #:rtl-cover-fns? #t)
(string-write
(gen-c-copyright "CPU class elements for @cpu@."
copyright-red-hat package-red-hat-simulators)
"\
// This file is included in the middle of the cpu class struct.
public:
\n"
/gen-hardware-types
/gen-hw-stream-and-destream-fns
" // C++ register access function templates\n"
"#define current_cpu this\n\n"
(lambda ()
(string-list-map /gen-reg-access-defns
(find register? (current-hw-list))))
"#undef current_cpu\n\n"
)
)
; **********
; cgen-defs.h
; Print various parameters of the cpu family.
; A "cpu family" here is a collection of variants of a particular architecture
; that share sufficient commonality that they can be handled together.
(define (/gen-cpu-defines)
(string-append
"\
/* Maximum number of instructions that are fetched at a time.
This is for LIW type instructions sets (e.g. m32r). */\n"
"#define @CPU@_MAX_LIW_INSNS " (number->string (cpu-liw-insns (current-cpu))) "\n\n"
"/* Maximum number of instructions that can be executed in parallel. */\n"
"#define @CPU@_MAX_PARALLEL_INSNS " (number->string (cpu-parallel-insns (current-cpu))) "\n"
"\n"
; (gen-enum-decl '@prefix@_virtual
; "@prefix@ virtual insns"
; "@ARCH@_INSN_" ; not @CPU@ to match CGEN_INSN_TYPE in opc.h
; '((x-invalid 0)
; (x-before -1) (x-after -2)
; (x-begin -3) (x-chain -4) (x-cti-chain -5)))
)
)
; Generate type of struct holding model state while executing.
(define (/gen-model-decls)
(logit 2 "Generating model decls ...\n")
(string-list
(string-list-map
(lambda (model)
(string-list
"typedef struct {\n"
(if (null? (model:state model))
" int empty;\n"
(string-map (lambda (var)
(string-append " "
(mode:c-type (mode:lookup (cadr var)))
" "
(gen-c-symbol (car var))
";\n"))
(model:state model)))
"} "
(if (null? (model:state model)) "BLANK" "@CPU@") "_MODEL_DATA;\n\n"
))
(current-model-list))
"
typedef int (@CPU@_MODEL_FN) (struct @cpu@_cpu*, void*);
typedef struct {
/* This is an integer that identifies this insn.
How this works is up to the target. */
int num;
/* Function to handle insn-specific profiling. */
@CPU@_MODEL_FN *model_fn;
/* Array of function units used by this insn. */
UNIT units[MAX_UNITS];
} @CPU@_INSN_TIMING;"
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; begin stack-based write schedule
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define write-stack-memory-mode-names '())
(define (/calculated-memory-write-buffer-size)
(let* ((is-mem? (lambda (op) (eq? (hw-sem-name (op:type op)) 'h-memory)))
(count-mem-writes
(lambda (sfmt) (length (find is-mem? (sfmt-out-ops sfmt))))))
(apply max (append '(0) (map count-mem-writes (current-sfmt-list))))))
;; note: this doesn't really correctly approximate the worst case. user-supplied functions
;; might rewrite the pipeline extensively while it's running.
;(define (/worst-case-number-of-writes-to hw-name)
; (let* ((sfmts (current-sfmt-list))
; (out-ops (map sfmt-out-ops sfmts))
; (pred (lambda (op) (equal? hw-name (gen-c-symbol (obj:name (op:type op))))))
; (filtered-ops (map (lambda (ops) (find pred ops)) out-ops)))
; (apply max (cons 0 (map (lambda (ops) (length ops)) filtered-ops)))))
(define (/hw-gen-write-stack-decl nm mode)
(let* (
; for the time being, we're disabling this size-estimation stuff and just
; requiring the user to supply a parameter WRITE_BUF_SZ before they include -defs.h
; (pipe-sz (+ 1 (max-delay (cpu-max-delay (current-cpu)))))
; (sz (* pipe-sz (/worst-case-number-of-writes-to nm))))
(mode-pad (spaces (- 4 (string-length (symbol->string mode)))))
(stack-name (string-append nm "_writes")))
(string-append
" write_stack< write<" (symbol->string mode) "> >" mode-pad "\t" stack-name "\t[pipe_sz];\n")))
(define (/hw-gen-write-struct-decl)
(let* ((dims (/worst-case-index-dims))
(sa string-append)
(ns number->string)
(idxs (iota dims))
(ctor (sa "write (PCADDR _pc, MODE _val"
(string-map (lambda (x) (sa ", USI _idx" (ns x) "=0")) idxs)
") : pc(_pc), val(_val)"
(string-map (lambda (x) (sa ", idx" (ns x) "(_idx" (ns x) ")")) idxs)
" {} \n"))
(idx-fields (string-map (lambda (x) (sa " USI idx" (ns x) ";\n")) idxs)))
(sa
"\n\n"
" template <typename MODE>\n"
" struct write\n"
" {\n"
" USI pc;\n"
" MODE val;\n"
idx-fields
" " ctor
" write() {}\n"
" };\n" )))
(define (/hw-vector-dims hw) (elm-get (hw-type hw) 'dimensions))
(define (/worst-case-index-dims)
(apply max
(append '(1) ; for memory accesses
(map (lambda (hw) (length (/hw-vector-dims hw)))
(find (lambda (hw) (not (scalar? hw))) (current-hw-list))))))
(define (/gen-writestacks)
(let* ((hw (find hw-need-write-stack? (current-hw-list)))
(modes write-stack-memory-mode-names)
(hw-pairs (map (lambda (h) (list (gen-c-symbol (obj:name h))
(obj:name (hw-mode h))))
hw))
(mem-pairs (map (lambda (m) (list (string-append (symbol->string m)
"_memory") m))
modes))
(all-pairs (append mem-pairs hw-pairs))
(h1 "\n\n// write stacks used in parallel execution\n\n struct write_stacks\n {\n // types of stacks\n\n")
(wb (string-append
"\n\n // unified writeback function (defined in @[email protected])"
"\n void writeback (int tick, @cpu@::@cpu@_cpu* current_cpu);"
"\n // unified write-stack clearing function (defined in @[email protected])"
"\n void reset ();"))
(zz "\n\n }; // end struct @prefix@::write_stacks \n\n"))
(string-append
(/hw-gen-write-struct-decl)
(foldl (lambda (s pair) (string-append s (apply /hw-gen-write-stack-decl pair))) h1 all-pairs)
wb
zz)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; end stack-based write schedule
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Generate the definition of the structure that holds register values, etc.
; for use during parallel execution.
(define (gen-write-stack-structure)
(let ((membuf-sz (/calculated-memory-write-buffer-size))
(max-delay (cpu-max-delay (current-cpu))))
(logit 2 "Generating write stack structure ...\n")
(string-append
" static const int max_delay = "
(number->string max-delay) ";\n"
" static const int pipe_sz = "
(number->string (+ 1 max-delay)) "; // max_delay + 1\n"
"
template <typename ELT>
struct write_stack
{
int t;
const int sz;
ELT buf[WRITE_BUF_SZ];
write_stack () : t(-1), sz(WRITE_BUF_SZ) {}
inline bool empty () { return (t == -1); }
inline void clear () { t = -1; }
inline void pop () { if (t > -1) t--;}
inline void push (const ELT &e) { if (t+1 < sz) buf [++t] = e;}
inline ELT &top () { return buf [t>0 ? ( t<sz ? t : sz-1) : 0];}
};
// look ahead for latest write with index = idx, where time of write is
// <= dist steps from base (present) in write_stack array st.
// returning def if no scheduled write is found.
template <typename STKS, typename VAL>
inline VAL lookahead (int dist, int base, STKS &st, VAL def, int idx=0)
{
for (; dist > 0; --dist)
{
write_stack <VAL> &v = st [(base + dist) % pipe_sz];
for (int i = v.t; i > 0; --i)
if (v.buf [i].idx0 == idx) return v.buf [i];
}
return def;
}
"
(/gen-writestacks)
)))
; Generate the TRACE_RECORD struct definition.
(define (/gen-trace-record-type)
(string-list
"\
/* Collection of various things for the trace handler to use. */
typedef struct @prefix@_trace_record {
PCADDR pc;
/* FIXME:wip */
} @CPU@_TRACE_RECORD;
\n"
)
)
; Generate <cpu>-defs.h
(define (cgen-defs.h)
(logit 1 "Generating " (gen-cpu-name) "-defs.h ...\n")
(assert-keep-one)
; Turn parallel execution support on if cpu needs it.
(set-with-parallel?! (state-parallel-exec?))
; Initialize rtl->c generation.
(rtl-c-config! #:rtl-cover-fns? #t)
(string-write
(gen-c-copyright "CPU family header for @cpu@ / @prefix@."
copyright-red-hat package-red-hat-simulators)
"\
#ifndef DEFS_@PREFIX@_H
#define DEFS_@PREFIX@_H
")
(if (with-parallel?)
(string-write "\
#include <stack>
#include \"cgen-types.h\"
// forward declaration\n\n
namespace @cpu@ {
struct @cpu@_cpu;
}
namespace @prefix@ {
using namespace cgen;
"
gen-write-stack-structure
"\
} // end @prefix@ namespace
"))
(string-write "\
#endif /* DEFS_@PREFIX@_H */\n"
)
)
; **************
; cgen-write.cxx
; This is the other way of implementing parallel execution support.
; Instead of fetching all the input operands first, write all the output
; operands and their addresses to holding variables, and then run a
; post-processing pass to update the cpu state.
; Return C code to fetch and save all output operands to instructions with
; <sformat> SFMT.
; Generate <cpu>-write.cxx.
(define (/gen-register-writer nm mode dims)
(let* ((pad " ")
(sa string-append)
(mode (symbol->string mode))
(idx-args (string-map (lambda (x) (sa "w.idx" (number->string x) ", "))
(iota dims))))
(sa pad "while (! " nm "_writes[tick].empty())\n"
pad "{\n"
pad " write<" mode "> &w = " nm "_writes[tick].top();\n"
pad " current_cpu->" nm "_set(" idx-args "w.val);\n"
pad " " nm "_writes[tick].pop();\n"
pad "}\n\n")))
(define (/gen-memory-writer nm mode dims)
(let* ((pad " ")
(sa string-append)
(mode (symbol->string mode))
(idx-args (string-map (lambda (x) (sa ", w.idx" (number->string x) ""))
(iota dims))))
(sa pad "while (! " nm "_writes[tick].empty())\n"
pad "{\n"
pad " write<" mode "> &w = " nm "_writes[tick].top();\n"
pad " current_cpu->SETMEM" mode " (w.pc" idx-args ", w.val);\n"
pad " " nm "_writes[tick].pop();\n"
pad "}\n\n")))
(define (/gen-reset-fn)
(let* ((sa string-append)
(objs (append (map (lambda (h) (gen-c-symbol (obj:name h)))
(find hw-need-write-stack? (current-hw-list)))
(map (lambda (m) (sa (symbol->string m) "_memory"))
write-stack-memory-mode-names)))
(clr (lambda (elt) (sa " clear_stacks (" elt "_writes);\n"))))
(sa
" template <typename ST> \n"
" static void clear_stacks (ST &st)\n"
" {\n"
" for (int i = 0; i < @prefix@::pipe_sz; i++)\n"
" st[i].clear();\n"
" }\n\n"
" void @prefix@::write_stacks::reset ()\n {\n"
(string-map clr objs)
" }")))
(define (/gen-unified-write-fn)
(let* ((hw (find hw-need-write-stack? (current-hw-list)))
(modes write-stack-memory-mode-names)
(hw-triples (map (lambda (h) (list (gen-c-symbol (obj:name h))
(obj:name (hw-mode h))
(length (/hw-vector-dims h))))
hw))
(mem-triples (map (lambda (m) (list (string-append (symbol->string m)
"_memory")
m 1))
modes)))
(logit 2 "Generating writer function ...\n")
(string-append
"
void @prefix@::write_stacks::writeback (int tick, @cpu@::@cpu@_cpu* current_cpu)
{
"
"\n // register writeback loops\n"
(string-map (lambda (t) (apply /gen-register-writer t)) hw-triples)
"\n // memory writeback loops\n"
(string-map (lambda (t) (apply /gen-memory-writer t)) mem-triples)
"
}
")))
(define (cgen-write.cxx)
(logit 1 "Generating " (gen-cpu-name) "-write.cxx ...\n")
(assert-keep-one)
(sim-analyze-insns!)
; Turn parallel execution support off.
(set-with-parallel?! #f)
; Tell the rtx->c translator we are the simulator.
(rtl-c-config! #:rtl-cover-fns? #t)
(string-write
(gen-c-copyright (string-append "Simulator instruction operand writer for "
(symbol->string (current-arch-name))
".")
copyright-red-hat package-red-hat-simulators)
"\
#include \"@[email protected]\"
"
/gen-reset-fn
/gen-unified-write-fn
)
)
; ******************
; cgen-semantics.cxx
; Return C code to perform the semantics of INSN.
(define (gen-semantic-code insn)
(cond ((insn-compiled-semantics insn)
=> (lambda (sem)
(rtl-c++-parsed VOID sem
#:for-insn? #t
#:rtl-cover-fns? #t
#:owner insn)))
((insn-canonical-semantics insn)
=> (lambda (sem)
(rtl-c++-parsed VOID sem
#:for-insn? #t
#:rtl-cover-fns? #t
#:owner insn)))
(else
(context-error (make-obj-context insn #f)
"While generating semantic code"
"semantics of insn are not canonicalized")))
)
; Return definition of C function to perform INSN.
; This version handles the with-scache case.
(define (/gen-scache-semantic-fn insn)
(logit 2 "Processing semantics for " (obj:name insn) ": \"" (insn-syntax insn) "\" ...\n")
(set! /with-profile? /with-profile-fn?)
(let ((cti? (insn-cti? insn))
(insn-len (insn-length-bytes insn)))
(string-list
"// ********** " (obj:name insn) ": " (insn-syntax insn) "\n\n"
(if (with-parallel?)
"void\n"
"sem_status\n")
"@prefix@_sem_" (gen-sym insn)
(if (with-parallel?)
(string-append " (@cpu@_cpu* current_cpu, @prefix@_scache* sem, const int tick, \n\t"
"@prefix@::write_stacks &buf)\n")
" (@cpu@_cpu* current_cpu, @prefix@_scache* sem)\n")
"{\n"
(gen-define-field-macro (insn-sfmt insn))
" sem_status status = SEM_STATUS_NORMAL;\n"
" @prefix@_scache* abuf = sem;\n"
; Unconditionally written operands are not recorded here.
(if (or (with-profile?) (with-parallel-write?))
" unsigned long long written = 0;\n"
"")
; The address of this insn, needed by extraction and semantic code.
; Note that the address recorded in the cpu state struct is not used.
; For faster engines that copy will be out of date.
" PCADDR pc = abuf->addr;\n"
" PCADDR npc = pc + " (number->string insn-len) ";\n"
"\n"
(gen-semantic-code insn)
"\n"
; Only update what's been written if some are conditionally written.
; Otherwise we know they're all written so there's no point in
; keeping track.
(if (or (with-profile?) (with-parallel-write?))
(if (/any-cond-written? (insn-sfmt insn))
" abuf->written = written;\n"
"")
"")
(if cti?
" current_cpu->done_cti_insn (npc, status);\n"
" current_cpu->done_insn (npc, status);\n")
(if (with-parallel?)
""
" return status;\n")
(gen-undef-field-macro (insn-sfmt insn))
"}\n\n"
))
)
(define (/gen-all-semantic-fns)
(logit 2 "Processing semantics ...\n")
(let ((insns (scache-engine-insns)))
(if (with-scache?)
(string-write-map /gen-scache-semantic-fn insns)
(error "must specify `with-scache'")))
)
; Generate <cpu>-sem.cxx.
; Each instruction is implemented in its own function.
(define (cgen-semantics.cxx)
(logit 1 "Generating " (gen-cpu-name) "-semantics.cxx ...\n")
(assert-keep-one)
(sim-analyze-insns!)
; Turn parallel execution support on if cpu needs it.
(set-with-parallel?! (state-parallel-exec?))
; Tell the rtx->c translator we are the simulator.
(rtl-c-config! #:rtl-cover-fns? #t)
; Indicate we're currently not generating a pbb engine.
(set-current-pbb-engine?! #f)
(string-write
(gen-c-copyright "Simulator instruction semantics for @prefix@."
copyright-red-hat package-red-hat-simulators)
"\
#if HAVE_CONFIG_H
#include \"config.h\"
#endif
#include \"@[email protected]\"
using namespace @cpu@; // FIXME: namespace organization still wip\n")
(if (with-parallel?)
(string-write "\
using namespace @prefix@; // FIXME: namespace organization still wip\n"))
(string-write "\
#define GET_ATTR(name) GET_ATTR_##name ()
\n"
/gen-all-semantic-fns
)
)
; *******************
; cgen-sem-switch.cxx
;
; The semantic switch engine has two flavors: one case per insn, and one
; case per "frag" (where each insn is split into one or more fragments).
; Utility of /gen-sem-case to return the mask of operands always written
; to in <sformat> SFMT.
; ??? Not currently used.
(define (/uncond-written-mask sfmt)
(apply + (map (lambda (op)
(if (op:cond? op)
0
(logsll 1 (op:num op))))
(sfmt-out-ops sfmt)))
)
; Utility of /gen-sem-case to return #t if any operand in <sformat> SFMT is
; conditionally written to.
(define (/any-cond-written? sfmt)
(any-true? (map op:cond? (sfmt-out-ops sfmt)))
)
; One case per insn version.
; Generate a switch case to perform INSN.
(define (/gen-sem-case insn parallel?)
(logit 2 "Processing "
(if parallel? "parallel " "")
"semantic switch case for " (obj:name insn) ": \""
(insn-syntax insn) "\" ...\n")
(set! /with-profile? /with-profile-sw?)
(let ((cti? (insn-cti? insn))
(insn-len (insn-length-bytes insn)))
(string-list
; INSN_ is prepended here and not elsewhere to avoid name collisions
; with symbols like AND, etc.
"\
// ********** " (insn-syntax insn) "
CASE (INSN_" (if parallel? "PAR_" "") (string-upcase (gen-sym insn)) "):
{
@prefix@_scache* abuf = vpc;\n"
(if (with-scache?)
(gen-define-field-macro (insn-sfmt insn))
"")
; Unconditionally written operands are not recorded here.
(if (or (with-profile?) (with-parallel-write?))
" unsigned long long written = 0;\n"
"")
; The address of this insn, needed by extraction and semantic code.
; Note that the address recorded in the cpu state struct is not used.
" PCADDR pc = abuf->addr;\n"
(if (and cti? (not parallel?))
(string-append " PCADDR npc;\n"
" branch_status br_status = BRANCH_UNTAKEN;\n")
"")
(string-list " vpc = vpc + 1;\n")
; Emit setup-semantics code for real insns.
(if (and (insn-real? insn)
(isa-setup-semantics (current-isa)))
(string-append
" "
(rtl-c++ VOID (obj-isa-list insn) nil
(isa-setup-semantics (current-isa))
#:for-insn? #t
#:rtl-cover-fns? #t
#:owner insn))
"")
"\n"
(gen-semantic-code insn)
"\n"
; Only update what's been written if some are conditionally written.
; Otherwise we know they're all written so there's no point in
; keeping track.
(if (or (with-profile?) (with-parallel-write?))
(if (/any-cond-written? (insn-sfmt insn))
" abuf->written = written;\n"
"")
"")
(if (and cti? (not parallel?))
(string-append " pbb_br_npc = npc;\n"
" pbb_br_status = br_status;\n")
"")
(if (with-scache?)
(gen-undef-field-macro (insn-sfmt insn))
"")
" }\n"
" NEXT (vpc);\n\n"
))
)
(define (/gen-sem-switch)
(logit 2 "Processing semantic switch ...\n")
; Turn parallel execution support off.
(set-with-parallel?! #f)
(string-write-map (lambda (insn) (/gen-sem-case insn #f))
(non-multi-insns (non-alias-insns (current-insn-list))))
)
; Generate the guts of a C switch statement to execute parallel instructions.
; This switch is included after the non-parallel instructions in the semantic
; switch.
;
; ??? We duplicate the writeback case for each insn, even though we only need
; one case per insn format. The former keeps the code for each insn
; together and might improve cache usage. On the other hand the latter
; reduces the amount of code, though it is believed that in this particular
; instance the win isn't big enough.
(define (/gen-parallel-sem-switch)
(logit 2 "Processing parallel insn semantic switch ...\n")
; Turn parallel execution support on.
(set-with-parallel?! #t)
(string-write-map (lambda (insn)
(string-list (/gen-sem-case insn #t)
(/gen-write-case (insn-sfmt insn) insn)))
(parallel-insns (current-insn-list)))
)
; Return computed-goto engine.
(define (/gen-sem-switch-engine)
(string-write
"\
void
@cpu@_cpu::@prefix@_pbb_run ()
{
@cpu@_cpu* current_cpu = this;
@prefix@_scache* vpc;
// These two are used to pass data from cti insns to the cti-chain insn.
PCADDR pbb_br_npc;
branch_status pbb_br_status;
#ifdef __GNUC__
{
static const struct sem_labels
{
enum @prefix@_insn_type insn;
void *label;
}
labels[] =
{\n"
(lambda ()
(string-write-map (lambda (insn)
(string-append " { "
"@PREFIX@_INSN_"
(string-upcase (gen-sym insn))
", && case_INSN_"
(string-upcase (gen-sym insn))
" },\n"))
(non-multi-insns (non-alias-insns (current-insn-list)))))
(if (state-parallel-exec?)
(lambda ()
(string-write-map (lambda (insn)
(string-append " { "
"@PREFIX@_INSN_PAR_"
(string-upcase (gen-sym insn))
", && case_INSN_PAR_"
(string-upcase (gen-sym insn))
" },\n"
" { "