summaryrefslogtreecommitdiff
path: root/cgen/desc-cpu.scm
blob: 1e3799c94df597c5b920e5ccc1570ce2eed5d7c9 (plain)
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
; Generate .c/.h versions of main elements of cpu description file.
; Copyright (C) 2000 Red Hat, Inc.
; This file is part of CGEN.

; ISA support code.

(define (-gen-isa-table-defns)
  (logit 2 "Generating isa table defns ...\n")

  (string-list
   "\
/* Instruction set variants.  */

static const CGEN_ISA @arch@_cgen_isa_table[] = {
"
   (string-list-map (lambda (isa)
		      (gen-obj-sanitize
		       isa
		       (string-append "  { "
				      "\"" (obj:name isa) "\", "
				      (number->string
				       (isa-default-insn-bitsize isa))
				      ", "
				      (number->string
				       (isa-base-insn-bitsize isa))
				      ", "
				      (number->string
				       (isa-min-insn-bitsize isa))
				      ", "
				      (number->string
				       (isa-max-insn-bitsize isa))
				      " },\n")))
		    (current-isa-list))
   "\
  { 0, 0, 0, 0, 0 }
};
\n"
   )
)

; Mach support code.

; Return C code to describe the various cpu variants.
; Currently this is quite simple, the various cpu names and their mach numbers
; are recorded in a "keyword" table.
; ??? No longer used as there is the mach attribute.
;
;(set! mach-table (make <keyword> 'mach "machine list"
;			(make <attr-list> "" nil) ; FIXME: sanitization?
;			(map (lambda (elm) (list (obj:name elm) (mach-number elm)))
;			     (current-mach-list))))

(define (-gen-mach-table-decls)
  (logit 2 "Generating machine table decls ...\n")
  "" ; (gen-decl mach-table)
)

(define (-gen-mach-table-defns)
  (logit 2 "Generating machine table defns ...\n")

  (string-list
   "\
/* Machine variants.  */

static const CGEN_MACH @arch@_cgen_mach_table[] = {
"
   (string-list-map (lambda (mach)
		      (gen-obj-sanitize
		       mach
		       (string-append "  { "
				      "\"" (obj:name mach) "\", "
				      "\"" (mach-bfd-name mach) "\", "
				      (mach-enum mach)
				      " },\n")))
		    (current-mach-list))
   "\
  { 0, 0, 0 }
};
\n"
   )
)

; Attribute support code.

; Return C code to describe the various attributes.

(define (-gen-attr-table-decls)
  (logit 2 "Generating attribute table decls ...\n")
  (string-append
   "/* Attributes.  */\n"
   "extern const CGEN_ATTR_TABLE @arch@_cgen_hardware_attr_table[];\n"
   "extern const CGEN_ATTR_TABLE @arch@_cgen_ifield_attr_table[];\n"
   "extern const CGEN_ATTR_TABLE @arch@_cgen_operand_attr_table[];\n"
   "extern const CGEN_ATTR_TABLE @arch@_cgen_insn_attr_table[];\n"
   "\n"
   )
)

; Alternative GEN-MASK argument to gen-bool-attrs.
; This uses the `A' macro to abbreviate the attribute definition.

(define (gen-A-attr-mask prefix name)
  (string-append "A(" (string-upcase (gen-c-symbol name)) ")")
)

; Instruction fields support code.

; Return C code to declare various ifield bits.

(define (gen-ifld-decls)
  (logit 2 "Generating instruction field decls ...\n")
  (string-list
   "/* Ifield support.  */\n\n"
   "extern const struct cgen_ifld @arch@_cgen_ifld_table[];\n\n"
   "/* Ifield attribute indices.  */\n\n"
   (gen-attr-enum-decl "cgen_ifld" (current-ifld-attr-list))
   (gen-enum-decl 'ifield_type "@arch@ ifield types"
		  "@ARCH@_"
		  (append (gen-obj-list-enums (non-derived-ifields (current-ifld-list)))
			  '((f-max))))
   "#define MAX_IFLD ((int) @ARCH@_F_MAX)\n\n"
   )
)

; Return C code to define the instruction field table,
; and any other ifield related definitions.

(define (gen-ifld-defns)
  (logit 2 "Generating ifield table ...\n")
  (let* ((ifld-list (find (lambda (f) (not (has-attr? f 'VIRTUAL)))
			  (non-derived-ifields (current-ifld-list))))
	 (all-attrs (current-ifld-attr-list))
	 (num-non-bools (attr-count-non-bools all-attrs)))
    (string-list
     "\
/* The instruction field table.  */

#define A(a) (1 << CONCAT2 (CGEN_IFLD_,a))

const CGEN_IFLD @arch@_cgen_ifld_table[] =
{
"
     (string-list-map
      (lambda (ifld)
	(gen-obj-sanitize ifld
			  (string-append
			   "  { "
			   (ifld-enum ifld) ", "
			   "\"" (obj:name ifld) "\", "
			   (number->string (ifld-word-offset ifld)) ", "
			   (number->string (ifld-word-length ifld)) ", "
			   (number->string (ifld-start ifld #f)) ", "
			   (number->string (ifld-length ifld)) ", "
			   (gen-obj-attr-defn 'ifld ifld all-attrs
					      num-non-bools gen-A-attr-mask)
			   "  },\n")))
      ifld-list)
     "\
  { 0, 0, 0, 0, 0, 0, {0, {0}} }
};

#undef A

"
     ))
)

; Hardware support.

; Return C code to declare the various hardware bits
; that can be (or must be) defined before including opcode/cgen.h.

(define (gen-hw-decls)
  (logit 2 "Generating hardware decls ...\n")
  (string-list
   "/* Hardware attribute indices.  */\n\n"
   (gen-attr-enum-decl "cgen_hw" (current-hw-attr-list))
   (gen-enum-decl 'cgen_hw_type "@arch@ hardware types"
		  "HW_" ; FIXME: @ARCH@_
		  (append (nub (map (lambda (hw)
				      (cons (hw-sem-name hw)
					    (cons '-
						  (atlist-attrs
						   (obj-atlist hw)))))
				    (current-hw-list))
			       (lambda (elm) (car elm)))
			  '((max))))
   "#define MAX_HW ((int) HW_MAX)\n\n"
   )
)

; Return declarations of variables tables used by HW.

(define (-gen-hw-decl hw)
  (string-append
   (if (and (hw-indices hw)
	    ; ??? Commented out as opcode changes are needed
	    ) ; (not (obj-has-attr? (hw-indices hw) 'PRIVATE)))
       (gen-decl (hw-indices hw))
       "")
   (if (and (hw-values hw)
	    ; ??? Commented out as opcode changes are needed
	    ) ; (not (obj-has-attr? (hw-values hw) 'PRIVATE)))
       (gen-decl (hw-values hw))
       "")
   )
)

; Return C code to declare the various hardware bits
; that must be defined after including opcode/cgen.h.

(define (gen-hw-table-decls)
  (logit 2 "Generating hardware table decls ...\n")
  (string-list
   "/* Hardware decls.  */\n\n"
   (string-map -gen-hw-decl (current-hw-list))
   "\n"
   )
)

; Return definitions of variables tables used by HW.
; Only do this for `PRIVATE' elements.  Public ones are emitted elsewhere.

(define (-gen-hw-defn hw)
  (string-append
   (if (and (hw-indices hw)
	    (obj-has-attr? (hw-indices hw) 'PRIVATE))
       (gen-defn (hw-indices hw))
       "")
   (if (and (hw-values hw)
	    (obj-has-attr? (hw-values hw) 'PRIVATE))
       (gen-defn (hw-values hw))
       "")
   )
)

; Generate the tables for the various hardware bits (register names, etc.).
; A table is generated for each element, and then another table is generated
; which collects them all together.
; Uses include looking up a particular register set so that a new reg
; can be added to it [at runtime].

(define (gen-hw-table-defns)
  (logit 2 "Generating hardware table ...\n")
  (let* ((all-attrs (current-hw-attr-list))
	 (num-non-bools (attr-count-non-bools all-attrs)))
    (string-list
     (string-list-map gen-defn (current-kw-list))
     (string-list-map -gen-hw-defn (current-hw-list))
     "

/* The hardware table.  */

#define A(a) (1 << CONCAT2 (CGEN_HW_,a))

const CGEN_HW_ENTRY @arch@_cgen_hw_table[] =
{
"
     (string-list-map
      (lambda (hw)
	(gen-obj-sanitize hw
			  (string-list
			   "  { "
			   "\"" (obj:name hw) "\", "
			   (hw-enum hw) ", "
			   ; ??? No element currently requires both indices and
			   ; values specs so we only output the needed one.
			   (or (and (hw-indices hw)
				    (send (hw-indices hw) 'gen-table-entry))
			       (and (hw-values hw)
				    (send (hw-values hw) 'gen-table-entry))
			       "CGEN_ASM_NONE, 0, ")
			   (gen-obj-attr-defn 'hw hw all-attrs
					      num-non-bools gen-A-attr-mask)
			   " },\n")))
      (current-hw-list))
     "\
  { 0, 0, CGEN_ASM_NONE, 0, {0, {0}} }
};

#undef A

"
     ))
)

; Utilities of cgen-opc.h.

; Return #define's of several constants.
; FIXME: Some of these to be moved into table of structs, one per cpu family.

(define (-gen-hash-defines)
  (logit 2 "Generating #define's ...\n")
  (string-list
   "#define CGEN_ARCH @arch@\n\n"
   "/* Given symbol S, return @arch@_cgen_<S>.  */\n"
   "#define CGEN_SYM(s) CONCAT3 (@arch@,_cgen_,s)\n\n"
   "/* Selected cpu families.  */\n"
   ; FIXME: Move to sim's arch.h.
   (string-map (lambda (cpu)
		 (gen-obj-sanitize cpu
				   (string-append "#define HAVE_CPU_"
						  (string-upcase (gen-sym cpu))
						  "\n")))
	       (current-cpu-list))
   "\n"
   "#define CGEN_INSN_LSB0_P " (if (current-arch-insn-lsb0?) "1" "0")
   "\n\n"
   "/* Minimum size of any insn (in bytes).  */\n"
   "#define CGEN_MIN_INSN_SIZE "
   (number->string (bits->bytes
		    (apply min (map isa-min-insn-bitsize (current-isa-list)))))
   "\n\n"
   "/* Maximum size of any insn (in bytes).  */\n"
   "#define CGEN_MAX_INSN_SIZE "
   (number->string (bits->bytes
		    (apply max (map isa-max-insn-bitsize (current-isa-list)))))
   "\n\n"
   ; This tells the assembler/disassembler whether or not it can use an int to
   ; record insns, which is faster.  Since this controls the typedef of the
   ; insn buffer, only enable this if all isas support it.
   "#define CGEN_INT_INSN_P "
   (if (all-true? (map isa-integral-insn? (current-isa-list))) "1" "0")
   "\n"
   "\n"
   "/* Maximum nymber of syntax bytes in an instruction.  */\n"
   "#define CGEN_ACTUAL_MAX_SYNTAX_BYTES "
   ; The +2 account for the leading "MNEM" and trailing 0.
   (number->string (+ 2 (apply max (map (lambda (insn) 
					  (length (syntax-break-out (insn-syntax insn))))
					(current-insn-list)))))
   "\n"
   "\n"
   "/* CGEN_MNEMONIC_OPERANDS is defined if mnemonics have operands.\n"
   "   e.g. In \"b,a foo\" the \",a\" is an operand.  If mnemonics have operands\n"
   "   we can't hash on everything up to the space.  */\n"
   (if strip-mnemonic?
       "/*#define CGEN_MNEMONIC_OPERANDS*/\n"
       "#define CGEN_MNEMONIC_OPERANDS\n")
   "\n"
   ; "/* Maximum number of operands any insn or macro-insn has.  */\n"
   ; FIXME: Should compute.
   ; "#define CGEN_MAX_INSN_OPERANDS 16\n"
   ; "\n"
   "/* Maximum number of fields in an instruction.  */\n"
   "#define CGEN_ACTUAL_MAX_IFMT_OPERANDS "
   (number->string (apply max (map (lambda (f) (length (ifmt-ifields f)))
				   (current-ifmt-list))))
   "\n\n"
  )
)

; Operand support.

; Return C code to declare various operand bits.

(define (gen-operand-decls)
  (logit 2 "Generating operand decls ...\n")
  (string-list
   "/* Operand attribute indices.  */\n\n"
   (gen-attr-enum-decl "cgen_operand" (current-op-attr-list))
   (gen-enum-decl 'cgen_operand_type "@arch@ operand types"
		  "@ARCH@_OPERAND_"
		  (nub (append (gen-obj-list-enums (current-op-list))
			       '((max)))
		       car))
   "/* Number of operands types.  */\n"
   "#define MAX_OPERANDS ((int) @ARCH@_OPERAND_MAX)\n\n"
   "/* Maximum number of operands referenced by any insn.  */\n"
   "#define MAX_OPERAND_INSTANCES "
   (number->string (max-operand-instances))
   "\n\n"
   )
)

; Generate C code to define the operand table.

(define (gen-operand-table)
  (logit 2 "Generating operand table ...\n")
  (let* ((all-attrs (current-op-attr-list))
	 (num-non-bools (attr-count-non-bools all-attrs)))
    (string-list
     "\
/* The operand table.  */

#define A(a) (1 << CONCAT2 (CGEN_OPERAND_,a))
#define OPERAND(op) CONCAT2 (@ARCH@_OPERAND_,op)

const CGEN_OPERAND @arch@_cgen_operand_table[] =
{
"
     (string-list-map
      (lambda (op)
	(gen-obj-sanitize op
			  (string-append
			   "/* " (obj:name op) ": " (obj:comment op) " */\n"
                          (if (or (derived-operand? op)
                                  (anyof-operand? op))
                              ""
                              (string-append 
			         "  { "
    		   	         "\"" (obj:name op) "\", "
			         (op-enum op) ", "
			         (hw-enum (op:hw-name op)) ", "
			         (number->string (op:start op)) ", "
			         (number->string (op:length op)) ",\n"
			         "    "
			         (gen-obj-attr-defn 'operand op all-attrs
				       	            num-non-bools gen-A-attr-mask)
			         "  },\n"
			      )))))
      (current-op-list))
     "\
  { 0, 0, 0, 0, 0, {0, {0}} }
};

#undef A

"
     )
    )
)

; Instruction table support.

; Return C code to declare various insn bits.

(define (gen-insn-decls)
  (logit 2 "Generating instruction decls ...\n")
  (string-list
   "/* Insn attribute indices.  */\n\n"
   (gen-attr-enum-decl "cgen_insn" (current-insn-attr-list))
   )
)

; Generate an insn table entry for INSN.
; ALL-ATTRS is a list of all instruction attributes.
; NUM-NON-BOOLS is the number of non-boolean insn attributes.

(define (gen-insn-table-entry insn all-attrs num-non-bools)
  (gen-obj-sanitize
   insn
   (string-list
    "/* " (insn-syntax insn) " */\n"
    "  {\n"
    "    "
    (if (has-attr? insn 'ALIAS) "-1" (insn-enum insn)) ", "
    "\"" (obj:name insn) "\", "
    "\"" (insn-mnemonic insn) "\", "
    ;(if (has-attr? insn 'ALIAS) "0" (number->string (insn-length insn))) ",\n"
    (number->string (insn-length insn)) ",\n"
; ??? There is currently a problem with embedded newlines, and this might
; best be put in another file [the table is already pretty big].
; Might also wish to output bytecodes instead.
;    "    "
;    (if (insn-semantics insn)
;	(string-append "\""
;		       (with-output-to-string
;			 ; ??? Should we do macro expansion here?
;			 (lambda () (display (insn-semantics insn))))
;		       "\"")
;	"0")
;    ",\n"
    ; ??? Might wish to output the raw format spec here instead
    ; (either as plain text or bytecodes).
    ; Values could be lazily computed and cached.
    "    "
    (gen-obj-attr-defn 'insn insn all-attrs num-non-bools gen-A-attr-mask)
    "\n  },\n"))
)

; Generate insn table.

(define (gen-insn-table)
  (logit 2 "Generating instruction table ...\n")
  (let* ((all-attrs (current-insn-attr-list))
	 (num-non-bools (attr-count-non-bools all-attrs)))
    (string-write
     "\
#define A(a) (1 << CONCAT2 (CGEN_INSN_,a))
#define OP(field) CGEN_SYNTAX_MAKE_FIELD (OPERAND (field))

/* The instruction table.  */

static const CGEN_IBASE @arch@_cgen_insn_table[MAX_INSNS] =
{
  /* Special null first entry.
     A `num' value of zero is thus invalid.
     Also, the special `invalid' insn resides here.  */
  { 0, 0, 0, 0, {0, {0}} },\n"

     (lambda ()
       (string-write-map (lambda (insn)
                           (logit 3 "Generating insn table entry for " (obj:name insn) " ...\n")
                           (gen-insn-table-entry insn all-attrs num-non-bools))
                         (non-multi-insns (current-insn-list))))

     "\
};

#undef A
#undef MNEM
#undef OP

"
     )
    )
)

; Cpu table handling support.
;
; ??? A lot of this can live in a machine independent file, but there's
; currently no place to put this file (there's no libcgen).  libopcodes is the
; wrong place as some simulator ports use this but they don't use libopcodes.

; Return C routines to open/close a cpu description table.
; This is defined here and not in cgen-opc.in because it refers to
; CGEN_{ASM,DIS}_HASH and insn_table/macro_insn_table which is defined
; earlier in the file.  ??? Things can certainly be rearranged though
; and opcodes/cgen.sh modified to insert the generated part into the middle
; of the file like is done for assembler/disassembler support.

(define (-gen-cpu-open)
  (string-append
   "\
/* Subroutine of @arch@_cgen_cpu_open to look up a mach via its bfd name.  */

static const CGEN_MACH *
lookup_mach_via_bfd_name (table, name)
     const CGEN_MACH *table;
     const char *name;
{
  while (table->name)
    {
      if (strcmp (name, table->bfd_name) == 0)
	return table;
      ++table;
    }
  abort ();
}

/* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */

static void
build_hw_table (cd)
     CGEN_CPU_TABLE *cd;
{
  int i;
  int machs = cd->machs;
  const CGEN_HW_ENTRY *init = & @arch@_cgen_hw_table[0];
  /* MAX_HW is only an upper bound on the number of selected entries.
     However each entry is indexed by it's enum so there can be holes in
     the table.  */
  const CGEN_HW_ENTRY **selected =
    (const CGEN_HW_ENTRY **) xmalloc (MAX_HW * sizeof (CGEN_HW_ENTRY *));

  cd->hw_table.init_entries = init;
  cd->hw_table.entry_size = sizeof (CGEN_HW_ENTRY);
  memset (selected, 0, MAX_HW * sizeof (CGEN_HW_ENTRY *));
  /* ??? For now we just use machs to determine which ones we want.  */
  for (i = 0; init[i].name != NULL; ++i)
    if (CGEN_HW_ATTR_VALUE (&init[i], CGEN_HW_MACH)
	& machs)
      selected[init[i].type] = &init[i];
  cd->hw_table.entries = selected;
  cd->hw_table.num_entries = MAX_HW;
}

/* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */

static void
build_ifield_table (cd)
     CGEN_CPU_TABLE *cd;
{
  cd->ifld_table = & @arch@_cgen_ifld_table[0];
}

/* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */

static void
build_operand_table (cd)
     CGEN_CPU_TABLE *cd;
{
  int i;
  int machs = cd->machs;
  const CGEN_OPERAND *init = & @arch@_cgen_operand_table[0];
  /* MAX_OPERANDS is only an upper bound on the number of selected entries.
     However each entry is indexed by it's enum so there can be holes in
     the table.  */
  const CGEN_OPERAND **selected =
    (const CGEN_OPERAND **) xmalloc (MAX_OPERANDS * sizeof (CGEN_OPERAND *));

  cd->operand_table.init_entries = init;
  cd->operand_table.entry_size = sizeof (CGEN_OPERAND);
  memset (selected, 0, MAX_OPERANDS * sizeof (CGEN_OPERAND *));
  /* ??? For now we just use mach to determine which ones we want.  */
  for (i = 0; init[i].name != NULL; ++i)
    if (CGEN_OPERAND_ATTR_VALUE (&init[i], CGEN_OPERAND_MACH)
	& machs)
      selected[init[i].type] = &init[i];
  cd->operand_table.entries = selected;
  cd->operand_table.num_entries = MAX_OPERANDS;
}

/* Subroutine of @arch@_cgen_cpu_open to build the hardware table.
   ??? This could leave out insns not supported by the specified mach/isa,
   but that would cause errors like \"foo only supported by bar\" to become
   \"unknown insn\", so for now we include all insns and require the app to
   do the checking later.
   ??? On the other hand, parsing of such insns may require their hardware or
   operand elements to be in the table [which they mightn't be].  */

static void
build_insn_table (cd)
     CGEN_CPU_TABLE *cd;
{
  int i;
  const CGEN_IBASE *ib = & @arch@_cgen_insn_table[0];
  CGEN_INSN *insns = (CGEN_INSN *) xmalloc (MAX_INSNS * sizeof (CGEN_INSN));

  memset (insns, 0, MAX_INSNS * sizeof (CGEN_INSN));
  for (i = 0; i < MAX_INSNS; ++i)
    insns[i].base = &ib[i];
  cd->insn_table.init_entries = insns;
  cd->insn_table.entry_size = sizeof (CGEN_IBASE);
  cd->insn_table.num_init_entries = MAX_INSNS;
}

/* Subroutine of @arch@_cgen_cpu_open to rebuild the tables.  */

static void
@arch@_cgen_rebuild_tables (cd)
     CGEN_CPU_TABLE *cd;
{
  int i,n_isas;
  unsigned int isas = cd->isas;
#if 0
  unsigned int machs = cd->machs;
#endif

  cd->int_insn_p = CGEN_INT_INSN_P;

  /* Data derived from the isa spec.  */
#define UNSET (CGEN_SIZE_UNKNOWN + 1)
  cd->default_insn_bitsize = UNSET;
  cd->base_insn_bitsize = UNSET;
  cd->min_insn_bitsize = 65535; /* some ridiculously big number */
  cd->max_insn_bitsize = 0;
  for (i = 0; i < MAX_ISAS; ++i)
    if (((1 << i) & isas) != 0)
      {
	const CGEN_ISA *isa = & @arch@_cgen_isa_table[i];

	/* Default insn sizes of all selected isas must be equal or we set
	   the result to 0, meaning \"unknown\".  */
	if (cd->default_insn_bitsize == UNSET)
	  cd->default_insn_bitsize = isa->default_insn_bitsize;
	else if (isa->default_insn_bitsize == cd->default_insn_bitsize)
	  ; /* this is ok */
	else
	  cd->default_insn_bitsize = CGEN_SIZE_UNKNOWN;

	/* Base insn sizes of all selected isas must be equal or we set
	   the result to 0, meaning \"unknown\".  */
	if (cd->base_insn_bitsize == UNSET)
	  cd->base_insn_bitsize = isa->base_insn_bitsize;
	else if (isa->base_insn_bitsize == cd->base_insn_bitsize)
	  ; /* this is ok */
	else
	  cd->base_insn_bitsize = CGEN_SIZE_UNKNOWN;

	/* Set min,max insn sizes.  */
	if (isa->min_insn_bitsize < cd->min_insn_bitsize)
	  cd->min_insn_bitsize = isa->min_insn_bitsize;
	if (isa->max_insn_bitsize > cd->max_insn_bitsize)
	  cd->max_insn_bitsize = isa->max_insn_bitsize;

	++n_isas;
      }

#if 0 /* Does nothing?? */
  /* Data derived from the mach spec.  */
  for (i = 0; i < MAX_MACHS; ++i)
    if (((1 << i) & machs) != 0)
      {
	const CGEN_MACH *mach = & @arch@_cgen_mach_table[i];

	++n_machs;
      }
#endif

  /* Determine which hw elements are used by MACH.  */
  build_hw_table (cd);

  /* Build the ifield table.  */
  build_ifield_table (cd);

  /* Determine which operands are used by MACH/ISA.  */
  build_operand_table (cd);

  /* Build the instruction table.  */
  build_insn_table (cd);
}

/* Initialize a cpu table and return a descriptor.
   It's much like opening a file, and must be the first function called.
   The arguments are a set of (type/value) pairs, terminated with
   CGEN_CPU_OPEN_END.

   Currently supported values:
   CGEN_CPU_OPEN_ISAS:    bitmap of values in enum isa_attr
   CGEN_CPU_OPEN_MACHS:   bitmap of values in enum mach_attr
   CGEN_CPU_OPEN_BFDMACH: specify 1 mach using bfd name
   CGEN_CPU_OPEN_ENDIAN:  specify endian choice
   CGEN_CPU_OPEN_END:     terminates arguments

   ??? Simultaneous multiple isas might not make sense, but it's not (yet)
   precluded.

   ??? We only support ISO C stdargs here, not K&R.
   Laziness, plus experiment to see if anything requires K&R - eventually
   K&R will no longer be supported - e.g. GDB is currently trying this.  */

CGEN_CPU_DESC
@arch@_cgen_cpu_open (enum cgen_cpu_open_arg arg_type, ...)
{
  CGEN_CPU_TABLE *cd = (CGEN_CPU_TABLE *) xmalloc (sizeof (CGEN_CPU_TABLE));
  static int init_p;
  unsigned int isas = 0;  /* 0 = \"unspecified\" */
  unsigned int machs = 0; /* 0 = \"unspecified\" */
  enum cgen_endian endian = CGEN_ENDIAN_UNKNOWN;
  va_list ap;

  if (! init_p)
    {
      init_tables ();
      init_p = 1;
    }

  memset (cd, 0, sizeof (*cd));

  va_start (ap, arg_type);
  while (arg_type != CGEN_CPU_OPEN_END)
    {
      switch (arg_type)
	{
	case CGEN_CPU_OPEN_ISAS :
	  isas = va_arg (ap, unsigned int);
	  break;
	case CGEN_CPU_OPEN_MACHS :
	  machs = va_arg (ap, unsigned int);
	  break;
	case CGEN_CPU_OPEN_BFDMACH :
	  {
	    const char *name = va_arg (ap, const char *);
	    const CGEN_MACH *mach =
	      lookup_mach_via_bfd_name (@arch@_cgen_mach_table, name);

	    machs |= mach->num << 1;
	    break;
	  }
	case CGEN_CPU_OPEN_ENDIAN :
	  endian = va_arg (ap, enum cgen_endian);
	  break;
	default :
	  fprintf (stderr, \"@arch@_cgen_cpu_open: unsupported argument `%d'\\n\",
		   arg_type);
	  abort (); /* ??? return NULL? */
	}
      arg_type = va_arg (ap, enum cgen_cpu_open_arg);
    }
  va_end (ap);

  /* mach unspecified means \"all\" */
  if (machs == 0)
    machs = (1 << MAX_MACHS) - 1;
  /* base mach is always selected */
  machs |= 1;
  /* isa unspecified means \"all\" */
  if (isas == 0)
    isas = (1 << MAX_ISAS) - 1;
  if (endian == CGEN_ENDIAN_UNKNOWN)
    {
      /* ??? If target has only one, could have a default.  */
      fprintf (stderr, \"@arch@_cgen_cpu_open: no endianness specified\\n\");
      abort ();
    }

  cd->isas = isas;
  cd->machs = machs;
  cd->endian = endian;
  /* FIXME: for the sparc case we can determine insn-endianness statically.
     The worry here is where both data and insn endian can be independently
     chosen, in which case this function will need another argument.
     Actually, will want to allow for more arguments in the future anyway.  */
  cd->insn_endian = endian;

  /* Table (re)builder.  */
  cd->rebuild_tables = @arch@_cgen_rebuild_tables;
  @arch@_cgen_rebuild_tables (cd);

  /* Default to not allowing signed overflow.  */
  cd->signed_overflow_ok_p = 0;
  
  return (CGEN_CPU_DESC) cd;
}

/* Cover fn to @arch@_cgen_cpu_open to handle the simple case of 1 isa, 1 mach.
   MACH_NAME is the bfd name of the mach.  */

CGEN_CPU_DESC
@arch@_cgen_cpu_open_1 (mach_name, endian)
     const char *mach_name;
     enum cgen_endian endian;
{
  return @arch@_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
			       CGEN_CPU_OPEN_ENDIAN, endian,
			       CGEN_CPU_OPEN_END);
}

/* Close a cpu table.
   ??? This can live in a machine independent file, but there's currently
   no place to put this file (there's no libcgen).  libopcodes is the wrong
   place as some simulator ports use this but they don't use libopcodes.  */

void
@arch@_cgen_cpu_close (cd)
     CGEN_CPU_DESC cd;
{
  if (cd->insn_table.init_entries)
    free ((CGEN_INSN *) cd->insn_table.init_entries);
  if (cd->hw_table.entries)
    free ((CGEN_HW_ENTRY *) cd->hw_table.entries);
  free (cd);
}

")
)

; General initialization C code
; Code is appended during processing.

(define -cputab-init-code "")
(define (cputab-add-init! code)
  (set! -cputab-init-code (string-append -cputab-init-code code))
)

; Return the C code to define the various initialization functions.
; This does not include assembler/disassembler specific stuff.
; Generally, this function doesn't do anything.
; It exists to allow a global-static-constructor kind of thing should
; one ever be necessary.

(define (gen-init-fns)
  (logit 2 "Generating init fns ...\n")
  (string-append
   "\
/* Initialize anything needed to be done once, before any cpu_open call.  */

static void
init_tables ()
{\n"
   -cputab-init-code
   "}\n\n"
  )
)

; Top level C code generators

; FIXME: Create enum objects for all the enums we explicitly declare here.
; Then they'd be usable and we wouldn't have to special case them here.

(define (cgen-desc.h)
  (logit 1 "Generating " (current-arch-name) "-desc.h ...\n")
  (string-write
   (gen-copyright "CPU data header for @arch@."
		  CURRENT-COPYRIGHT CURRENT-PACKAGE)
   "\
#ifndef @ARCH@_CPU_H
#define @ARCH@_CPU_H

"
   -gen-hash-defines
   ; This is defined in arch.h.  It's not defined here as there is yet to
   ; be a need for it in the assembler/disassembler.
   ;(gen-enum-decl 'model_type "model types"
   ;		  "MODEL_"
   ;		  (append (map list (map obj:name (current-model-list))) '((max))))
   ;"#define MAX_MODELS ((int) MODEL_MAX)\n\n"
   "/* Enums.  */\n\n"
   (string-map gen-decl (current-enum-list))
   "/* Attributes.  */\n\n"
   (string-map gen-decl (current-attr-list))
   "/* Number of architecture variants.  */\n"
   ; If there is only 1 isa, leave out special handling.  */
   (if (= (length (current-isa-list)) 1)
       "#define MAX_ISAS  1\n"
       "#define MAX_ISAS  ((int) ISA_MAX)\n")
   "#define MAX_MACHS ((int) MACH_MAX)\n\n"
   gen-ifld-decls
   gen-hw-decls
   gen-operand-decls
   gen-insn-decls
   "/* cgen.h uses things we just defined.  */\n"
   "#include \"opcode/cgen.h\"\n\n"
   -gen-attr-table-decls
   -gen-mach-table-decls
   gen-hw-table-decls
   "\n"
   (lambda () (gen-extra-cpu.h srcdir (current-arch-name))) ; from <arch>.opc
   "

#endif /* @ARCH@_CPU_H */
"
   )
)

; This file contains the "top level" definitions of the cpu.
; This includes various elements of the description file, expressed in C.
;
; ??? A lot of this file can go in a machine-independent file!  However,
; some simulators don't use the cgen opcodes support so there is currently
; no place to put this file.  To be revisited when we do have such a place.

(define (cgen-desc.c)
  (logit 1 "Generating " (current-arch-name) "-desc.c ...\n")
  (string-write
   (gen-copyright "CPU data for @arch@."
		  CURRENT-COPYRIGHT CURRENT-PACKAGE)
   "\
#include \"sysdep.h\"
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include \"ansidecl.h\"
#include \"bfd.h\"
#include \"symcat.h\"
#include \"@prefix@-desc.h\"
#include \"@prefix@-opc.h\"
#include \"opintl.h\"
#include \"libiberty.h\"
\n"
   (lambda () (gen-extra-cpu.c srcdir (current-arch-name))) ; from <arch>.opc
   gen-attr-table-defns
   -gen-isa-table-defns
   -gen-mach-table-defns
   gen-hw-table-defns
   gen-ifld-defns
   gen-operand-table
   gen-insn-table
   gen-init-fns
   -gen-cpu-open
   )
)