OSDN Git Service

438a6623ddf166f43ef9061a7a70abf2841efb8b
[pf3gnuchains/pf3gnuchains4x.git] / cgen / desc-cpu.scm
1 ; Generate .c/.h versions of main elements of cpu description file.
2 ; Copyright (C) 2000, 2001, 2002, 2003 Red Hat, Inc.
3 ; This file is part of CGEN.
4
5 ; ISA support code.
6
7 (define (-gen-isa-table-defns)
8   (logit 2 "Generating isa table defns ...\n")
9
10   (string-list
11    "\
12 /* Instruction set variants.  */
13
14 static const CGEN_ISA @arch@_cgen_isa_table[] = {
15 "
16    (string-list-map (lambda (isa)
17                       (gen-obj-sanitize
18                        isa
19                        (string-append "  { "
20                                       "\"" (obj:str-name isa) "\", "
21                                       (number->string
22                                        (isa-default-insn-bitsize isa))
23                                       ", "
24                                       (number->string
25                                        (isa-base-insn-bitsize isa))
26                                       ", "
27                                       (number->string
28                                        (isa-min-insn-bitsize isa))
29                                       ", "
30                                       (number->string
31                                        (isa-max-insn-bitsize isa))
32                                       " },\n")))
33                     (current-isa-list))
34    "\
35   { 0, 0, 0, 0, 0 }
36 };
37 \n"
38    )
39 )
40
41 ; Mach support code.
42
43 ; Return C code to describe the various cpu variants.
44 ; Currently this is quite simple, the various cpu names and their mach numbers
45 ; are recorded in a "keyword" table.
46 ; ??? No longer used as there is the mach attribute.
47 ;
48 ;(set! mach-table (make <keyword> 'mach "machine list"
49 ;                       (make <attr-list> "" nil) ; FIXME: sanitization?
50 ;                       (map (lambda (elm) (list (obj:name elm) (mach-number elm)))
51 ;                            (current-mach-list))))
52
53 (define (-gen-mach-table-decls)
54   (logit 2 "Generating machine table decls ...\n")
55   "" ; (gen-decl mach-table)
56 )
57
58 (define (-gen-mach-table-defns)
59   (logit 2 "Generating machine table defns ...\n")
60
61   (string-list
62    "\
63 /* Machine variants.  */
64
65 static const CGEN_MACH @arch@_cgen_mach_table[] = {
66 "
67    (string-list-map (lambda (mach)
68                       (gen-obj-sanitize
69                        mach
70                        (string-append "  { "
71                                       "\"" (obj:str-name mach) "\", "
72                                       "\"" (mach-bfd-name mach) "\", "
73                                       (mach-enum mach) ", "
74                                       (number->string (cpu-insn-chunk-bitsize (mach-cpu mach)))
75                                       " },\n")))
76                     (current-mach-list))
77    "\
78   { 0, 0, 0, 0 }
79 };
80 \n"
81    )
82 )
83 \f
84 ; Attribute support code.
85
86 ; Return C code to describe the various attributes.
87
88 (define (-gen-attr-table-decls)
89   (logit 2 "Generating attribute table decls ...\n")
90   (string-append
91    "/* Attributes.  */\n"
92    "extern const CGEN_ATTR_TABLE @arch@_cgen_hardware_attr_table[];\n"
93    "extern const CGEN_ATTR_TABLE @arch@_cgen_ifield_attr_table[];\n"
94    "extern const CGEN_ATTR_TABLE @arch@_cgen_operand_attr_table[];\n"
95    "extern const CGEN_ATTR_TABLE @arch@_cgen_insn_attr_table[];\n"
96    "\n"
97    )
98 )
99
100 ; Alternative GEN-MASK argument to gen-bool-attrs.
101 ; This uses the `A' macro to abbreviate the attribute definition.
102
103 (define (gen-A-attr-mask prefix name)
104   (string-append "A(" (string-upcase (gen-c-symbol name)) ")")
105 )
106 \f
107 ; Instruction fields support code.
108
109 ; Return C code to declare various ifield bits.
110
111 (define (gen-ifld-decls)
112   (logit 2 "Generating instruction field decls ...\n")
113   (string-list
114    "/* Ifield support.  */\n\n"
115    "/* Ifield attribute indices.  */\n\n"
116    (gen-attr-enum-decl "cgen_ifld" (current-ifld-attr-list))
117    (gen-enum-decl 'ifield_type "@arch@ ifield types"
118                   "@ARCH@_"
119                   (append (gen-obj-list-enums (non-derived-ifields (current-ifld-list)))
120                           '((f-max))))
121    "#define MAX_IFLD ((int) @ARCH@_F_MAX)\n\n"
122    )
123 )
124
125 ; Return C code to define the instruction field table,
126 ; and any other ifield related definitions.
127
128 (define (gen-ifld-defns)
129   (logit 2 "Generating ifield table ...\n")
130   (let* ((ifld-list (current-ifld-list))
131          (all-attrs (current-ifld-attr-list))
132          (num-non-bools (attr-count-non-bools all-attrs)))
133     (string-list
134      "
135 /* The instruction field table.  */
136
137 "
138      (gen-define-with-symcat "A(a) (1 << CGEN_IFLD_" "a)")
139      "
140 const CGEN_IFLD @arch@_cgen_ifld_table[] =
141 {
142 "
143      (string-list-map
144       (lambda (ifld)
145         (gen-obj-sanitize ifld
146                           (string-append
147                            "  { "
148                            (ifld-enum ifld) ", "
149                            "\"" (obj:str-name ifld) "\", "
150                            (if
151                             (or (has-attr? ifld 'VIRTUAL)
152                                 (derived-ifield? ifld))
153                              "0, 0, 0, 0,"
154                              (string-append
155                               (number->string (ifld-word-offset ifld)) ", "
156                               (number->string (ifld-word-length ifld)) ", "
157                               (number->string (ifld-start ifld #f)) ", "
158                               (number->string (ifld-length ifld)) ", "))
159                            (gen-obj-attr-defn 'ifld ifld all-attrs
160                                       num-non-bools gen-A-attr-mask)
161                            "  },\n")))
162       ifld-list)
163      "\
164   { 0, 0, 0, 0, 0, 0, {0, {0}} }
165 };
166
167 #undef A
168
169 "
170      ))
171 )
172 \f
173 ; Hardware support.
174
175 ; Return C code to declare the various hardware bits
176 ; that can be (or must be) defined before including opcode/cgen.h.
177
178 (define (gen-hw-decls)
179   (logit 2 "Generating hardware decls ...\n")
180   (string-list
181    "/* Hardware attribute indices.  */\n\n"
182    (gen-attr-enum-decl "cgen_hw" (current-hw-attr-list))
183    (gen-enum-decl 'cgen_hw_type "@arch@ hardware types"
184                   "HW_" ; FIXME: @ARCH@_
185                   (append (nub (map (lambda (hw)
186                                       (cons (hw-sem-name hw)
187                                             (cons '-
188                                                   (atlist-attrs
189                                                    (obj-atlist hw)))))
190                                     (current-hw-list))
191                                (lambda (elm) (car elm)))
192                           '((max))))
193    "#define MAX_HW ((int) HW_MAX)\n\n"
194    )
195 )
196
197 ; Return declarations of variables tables used by HW.
198
199 (define (-gen-hw-decl hw)
200   (string-append
201    (if (and (hw-indices hw)
202             ; ??? Commented out as opcode changes are needed
203             ) ; (not (obj-has-attr? (hw-indices hw) 'PRIVATE)))
204        (gen-decl (hw-indices hw))
205        "")
206    (if (and (hw-values hw)
207             ; ??? Commented out as opcode changes are needed
208             ) ; (not (obj-has-attr? (hw-values hw) 'PRIVATE)))
209        (gen-decl (hw-values hw))
210        "")
211    )
212 )
213
214 ; Return C code to declare the various hardware bits
215 ; that must be defined after including opcode/cgen.h.
216
217 (define (gen-hw-table-decls)
218   (logit 2 "Generating hardware table decls ...\n")
219   (string-list
220    "/* Hardware decls.  */\n\n"
221    (string-map -gen-hw-decl (current-hw-list))
222    "\n"
223    "extern const CGEN_HW_ENTRY @arch@_cgen_hw_table[];\n"
224    )
225 )
226
227 ; Return definitions of variables tables used by HW.
228 ; Only do this for `PRIVATE' elements.  Public ones are emitted elsewhere.
229
230 (define (-gen-hw-defn hw)
231   (string-append
232    (if (and (hw-indices hw)
233             (obj-has-attr? (hw-indices hw) 'PRIVATE))
234        (gen-defn (hw-indices hw))
235        "")
236    (if (and (hw-values hw)
237             (obj-has-attr? (hw-values hw) 'PRIVATE))
238        (gen-defn (hw-values hw))
239        "")
240    )
241 )
242
243 ; Generate the tables for the various hardware bits (register names, etc.).
244 ; A table is generated for each element, and then another table is generated
245 ; which collects them all together.
246 ; Uses include looking up a particular register set so that a new reg
247 ; can be added to it [at runtime].
248
249 (define (gen-hw-table-defns)
250   (logit 2 "Generating hardware table ...\n")
251   (let* ((all-attrs (current-hw-attr-list))
252          (num-non-bools (attr-count-non-bools all-attrs)))
253     (string-list
254      (string-list-map gen-defn (current-kw-list))
255      (string-list-map -gen-hw-defn (current-hw-list))
256      "
257 /* The hardware table.  */
258
259 "
260      (gen-define-with-symcat "A(a) (1 << CGEN_HW_" "a)")
261      "
262 const CGEN_HW_ENTRY @arch@_cgen_hw_table[] =
263 {
264 "
265      (string-list-map
266       (lambda (hw)
267         (gen-obj-sanitize hw
268                           (string-list
269                            "  { "
270                            "\"" (obj:str-name hw) "\", "
271                            (hw-enum hw) ", "
272                            ; ??? No element currently requires both indices and
273                            ; values specs so we only output the needed one.
274                            (or (and (hw-indices hw)
275                                     (send (hw-indices hw) 'gen-table-entry))
276                                (and (hw-values hw)
277                                     (send (hw-values hw) 'gen-table-entry))
278                                "CGEN_ASM_NONE, 0, ")
279                            (gen-obj-attr-defn 'hw hw all-attrs
280                                               num-non-bools gen-A-attr-mask)
281                            " },\n")))
282       (current-hw-list))
283      "\
284   { 0, 0, CGEN_ASM_NONE, 0, {0, {0}} }
285 };
286
287 #undef A
288
289 "
290      ))
291 )
292 \f
293 ; Utilities of cgen-opc.h.
294
295 ; Return #define's of several constants.
296 ; FIXME: Some of these to be moved into table of structs, one per cpu family.
297
298 (define (-gen-hash-defines)
299   (logit 2 "Generating #define's ...\n")
300   (string-list
301    "#define CGEN_ARCH @arch@\n\n"
302    "/* Given symbol S, return @arch@_cgen_<S>.  */\n"
303    (gen-define-with-symcat "CGEN_SYM(s) @arch@" "_cgen_" "s")
304    "\n\n/* Selected cpu families.  */\n"
305    ; FIXME: Move to sim's arch.h.
306    (string-map (lambda (cpu)
307                  (gen-obj-sanitize cpu
308                                    (string-append "#define HAVE_CPU_"
309                                                   (string-upcase (gen-sym cpu))
310                                                   "\n")))
311                (current-cpu-list))
312    "\n"
313    "#define CGEN_INSN_LSB0_P " (if (current-arch-insn-lsb0?) "1" "0")
314    "\n\n"
315    "/* Minimum size of any insn (in bytes).  */\n"
316    "#define CGEN_MIN_INSN_SIZE "
317    (number->string (bits->bytes
318                     (apply min (map isa-min-insn-bitsize (current-isa-list)))))
319    "\n\n"
320    "/* Maximum size of any insn (in bytes).  */\n"
321    "#define CGEN_MAX_INSN_SIZE "
322    (number->string (bits->bytes
323                     (apply max (map isa-max-insn-bitsize (current-isa-list)))))
324    "\n\n"
325    ; This tells the assembler/disassembler whether or not it can use an int to
326    ; record insns, which is faster.  Since this controls the typedef of the
327    ; insn buffer, only enable this if all isas support it.
328    "#define CGEN_INT_INSN_P "
329    (if (all-true? (map isa-integral-insn? (current-isa-list))) "1" "0")
330    "\n"
331    "\n"
332    "/* Maximum number of syntax elements in an instruction.  */\n"
333    "#define CGEN_ACTUAL_MAX_SYNTAX_ELEMENTS "
334    ; The +2 account for the leading "MNEM" and trailing 0.
335    (number->string (+ 2 (apply max (map (lambda (insn) 
336                                           (length (syntax-break-out (insn-syntax insn))))
337                                         (current-insn-list)))))
338    "\n"
339    "\n"
340    "/* CGEN_MNEMONIC_OPERANDS is defined if mnemonics have operands.\n"
341    "   e.g. In \"b,a foo\" the \",a\" is an operand.  If mnemonics have operands\n"
342    "   we can't hash on everything up to the space.  */\n"
343    (if strip-mnemonic?
344        "/*#define CGEN_MNEMONIC_OPERANDS*/\n"
345        "#define CGEN_MNEMONIC_OPERANDS\n")
346    "\n"
347    ; "/* Maximum number of operands any insn or macro-insn has.  */\n"
348    ; FIXME: Should compute.
349    ; "#define CGEN_MAX_INSN_OPERANDS 16\n"
350    ; "\n"
351    "/* Maximum number of fields in an instruction.  */\n"
352    "#define CGEN_ACTUAL_MAX_IFMT_OPERANDS "
353    (number->string (apply max (map (lambda (f) (length (ifmt-ifields f)))
354                                    (current-ifmt-list))))
355    "\n\n"
356   )
357 )
358 \f
359 ; Operand support.
360
361 ; Return C code to declare various operand bits.
362
363 (define (gen-operand-decls)
364   (logit 2 "Generating operand decls ...\n")
365   (string-list
366    "/* Operand attribute indices.  */\n\n"
367    (gen-attr-enum-decl "cgen_operand" (current-op-attr-list))
368    (gen-enum-decl 'cgen_operand_type "@arch@ operand types"
369                   "@ARCH@_OPERAND_"
370                   (nub (append (gen-obj-list-enums (current-op-list))
371                                '((max)))
372                        car))
373    "/* Number of operands types.  */\n"
374    "#define MAX_OPERANDS " (number->string (length (gen-obj-list-enums (current-op-list)))) "\n\n"
375    ; was: "#define MAX_OPERANDS ((int) @ARCH@_OPERAND_MAX)\n\n"
376    "/* Maximum number of operands referenced by any insn.  */\n"
377    "#define MAX_OPERAND_INSTANCES "
378    (number->string (max-operand-instances))
379    "\n\n"
380    )
381 )
382
383 ; Generate C code to define the operand table.
384
385 (define ifld-number-cache #f)
386 (define (ifld-number f)
387   (if (not ifld-number-cache)
388       (let* ((ls (find (lambda (f) (not (has-attr? f 'VIRTUAL)))
389                        (non-derived-ifields (current-ifld-list))))
390              (numls (iota (length ls))))
391         (set! ifld-number-cache 
392               (map (lambda (elt num) (cons (obj:name elt) num)) 
393                    ls numls))))
394   (number->string (cdr (assoc (obj:name f) ifld-number-cache))))
395
396 (define (gen-maybe-multi-ifld-of-op op)
397   (let* ((idx (op:index op))
398          (ty (hw-index:type idx))
399          (fld (hw-index:value idx)))
400     (gen-maybe-multi-ifld ty fld)))
401
402 (define (gen-maybe-multi-ifld ty fld)
403   (let* ((field-ref "0")
404          (field-count "0"))
405     (if (equal? ty 'ifield)
406         (if (multi-ifield? fld) 
407             (begin
408               (set! field-ref (string-append "&" (ifld-enum fld) "_MULTI_IFIELD[0]"))
409               (set! field-count (number->string (length (elm-get fld 'subfields)))))
410             ; else          
411               (set! field-ref (string-append "&@arch@_cgen_ifld_table[" (ifld-enum fld) "]"))))
412     (string-append "{ " field-count ", { (const PTR) " field-ref " } }")))
413
414 (define (gen-multi-ifield-nodes)
415   (let ((multis (find multi-ifield? (current-ifld-list))))
416     (apply string-append
417            (append 
418             
419             '("\n\n/* multi ifield declarations */\n\n")
420             (map   
421              (lambda (ifld) 
422                (string-append 
423                 "const CGEN_MAYBE_MULTI_IFLD " 
424                 (ifld-enum ifld) "_MULTI_IFIELD [];\n"))
425              multis)
426
427             '("\n\n/* multi ifield definitions */\n\n")
428             (map   
429              (lambda (ifld)
430                (string-append
431                 "const CGEN_MAYBE_MULTI_IFLD " 
432                 (ifld-enum ifld) "_MULTI_IFIELD [] =\n{"
433                 (apply string-append 
434                        (map (lambda (x) (string-append "\n    " (gen-maybe-multi-ifld 'ifield x) ",")) 
435                             (elm-get ifld 'subfields)))
436                 "\n    { 0, { (const PTR) 0 } }\n};\n"))
437              multis)))))
438
439 (define (gen-operand-table)
440   (logit 2 "Generating operand table ...\n")
441   (let* ((all-attrs (current-op-attr-list))
442          (num-non-bools (attr-count-non-bools all-attrs)))
443     (string-list
444      "
445 /* The operand table.  */
446
447 "
448      (gen-define-with-symcat "A(a) (1 << CGEN_OPERAND_" "a)")
449      (gen-define-with-symcat "OPERAND(op) @ARCH@_OPERAND_" "op")
450 "
451 const CGEN_OPERAND @arch@_cgen_operand_table[] =
452 {
453 "
454      (string-list-map
455       (lambda (op)
456         (gen-obj-sanitize op
457                           (string-append
458                            "/* " (obj:str-name op) ": " (obj:comment op) " */\n"
459                           (if (or (derived-operand? op)
460                                   (anyof-operand? op))
461                               ""
462                               (string-append 
463                                  "  { "
464                                  "\"" (obj:str-name op) "\", "
465                                  (op-enum op) ", "
466                                  (hw-enum (op:hw-name op)) ", "
467                                  (number->string (op:start op)) ", "
468                                  (number->string (op:length op)) ",\n"
469                                  "    "
470                                  (gen-maybe-multi-ifld-of-op op) ", \n"
471                                  "    "
472                                  (gen-obj-attr-defn 'operand op all-attrs
473                                                     num-non-bools gen-A-attr-mask)
474                                  "  },\n"
475                               )))))
476       (current-op-list))
477      "/* sentinel */\n\
478   { 0, 0, 0, 0, 0,\n    { 0, { (const PTR) 0 } },\n    { 0, { 0 } } }
479 };
480
481 #undef A
482
483 "
484      )
485     )
486 )
487 \f
488 ; Instruction table support.
489
490 ; Return C code to declare various insn bits.
491
492 (define (gen-insn-decls)
493   (logit 2 "Generating instruction decls ...\n")
494   (string-list
495    "/* Insn attribute indices.  */\n\n"
496    (gen-attr-enum-decl "cgen_insn" (current-insn-attr-list))
497    )
498 )
499
500 ; Generate an insn table entry for INSN.
501 ; ALL-ATTRS is a list of all instruction attributes.
502 ; NUM-NON-BOOLS is the number of non-boolean insn attributes.
503
504 (define (gen-insn-table-entry insn all-attrs num-non-bools)
505   (gen-obj-sanitize
506    insn
507    (string-list
508     "/* " (insn-syntax insn) " */\n"
509     "  {\n"
510     "    "
511     (if (has-attr? insn 'ALIAS) "-1" (insn-enum insn)) ", "
512     "\"" (obj:str-name insn) "\", "
513     "\"" (insn-mnemonic insn) "\", "
514     ;(if (has-attr? insn 'ALIAS) "0" (number->string (insn-length insn))) ",\n"
515     (number->string (insn-length insn)) ",\n"
516 ; ??? There is currently a problem with embedded newlines, and this might
517 ; best be put in another file [the table is already pretty big].
518 ; Might also wish to output bytecodes instead.
519 ;    "    "
520 ;    (if (insn-semantics insn)
521 ;       (string-append "\""
522 ;                      (with-output-to-string
523 ;                        ; ??? Should we do macro expansion here?
524 ;                        (lambda () (display (insn-semantics insn))))
525 ;                      "\"")
526 ;       "0")
527 ;    ",\n"
528     ; ??? Might wish to output the raw format spec here instead
529     ; (either as plain text or bytecodes).
530     ; Values could be lazily computed and cached.
531     "    "
532     (gen-obj-attr-defn 'insn insn all-attrs num-non-bools gen-A-attr-mask)
533     "\n  },\n"))
534 )
535
536 ; Generate insn table.
537
538 (define (gen-insn-table)
539   (logit 2 "Generating instruction table ...\n")
540   (let* ((all-attrs (current-insn-attr-list))
541          (num-non-bools (attr-count-non-bools all-attrs)))
542     (string-write
543      "
544 /* The instruction table.  */
545
546 #define OP(field) CGEN_SYNTAX_MAKE_FIELD (OPERAND (field))
547 "
548      (gen-define-with-symcat "A(a) (1 << CGEN_INSN_" "a)")
549 "
550 static const CGEN_IBASE @arch@_cgen_insn_table[MAX_INSNS] =
551 {
552   /* Special null first entry.
553      A `num' value of zero is thus invalid.
554      Also, the special `invalid' insn resides here.  */
555   { 0, 0, 0, 0, {0, {0}} },\n"
556
557      (lambda ()
558        (string-write-map (lambda (insn)
559                            (logit 3 "Generating insn table entry for " (obj:name insn) " ...\n")
560                            (gen-insn-table-entry insn all-attrs num-non-bools))
561                          (non-multi-insns (current-insn-list))))
562
563      "\
564 };
565
566 #undef OP
567 #undef A
568
569 "
570      )
571     )
572 )
573 \f
574 ; Cpu table handling support.
575 ;
576 ; ??? A lot of this can live in a machine independent file, but there's
577 ; currently no place to put this file (there's no libcgen).  libopcodes is the
578 ; wrong place as some simulator ports use this but they don't use libopcodes.
579
580 ; Return C routines to open/close a cpu description table.
581 ; This is defined here and not in cgen-opc.in because it refers to
582 ; CGEN_{ASM,DIS}_HASH and insn_table/macro_insn_table which is defined
583 ; earlier in the file.  ??? Things can certainly be rearranged though
584 ; and opcodes/cgen.sh modified to insert the generated part into the middle
585 ; of the file like is done for assembler/disassembler support.
586
587 (define (-gen-cpu-open)
588   (string-append
589    "\
590 static const CGEN_MACH * lookup_mach_via_bfd_name
591   PARAMS ((const CGEN_MACH *, const char *));
592 static void build_hw_table  PARAMS ((CGEN_CPU_TABLE *));
593 static void build_ifield_table  PARAMS ((CGEN_CPU_TABLE *));
594 static void build_operand_table PARAMS ((CGEN_CPU_TABLE *));
595 static void build_insn_table    PARAMS ((CGEN_CPU_TABLE *));
596 static void @arch@_cgen_rebuild_tables PARAMS ((CGEN_CPU_TABLE *));
597
598 /* Subroutine of @arch@_cgen_cpu_open to look up a mach via its bfd name.  */
599
600 static const CGEN_MACH *
601 lookup_mach_via_bfd_name (table, name)
602      const CGEN_MACH *table;
603      const char *name;
604 {
605   while (table->name)
606     {
607       if (strcmp (name, table->bfd_name) == 0)
608         return table;
609       ++table;
610     }
611   abort ();
612 }
613
614 /* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */
615
616 static void
617 build_hw_table (cd)
618      CGEN_CPU_TABLE *cd;
619 {
620   int i;
621   int machs = cd->machs;
622   const CGEN_HW_ENTRY *init = & @arch@_cgen_hw_table[0];
623   /* MAX_HW is only an upper bound on the number of selected entries.
624      However each entry is indexed by it's enum so there can be holes in
625      the table.  */
626   const CGEN_HW_ENTRY **selected =
627     (const CGEN_HW_ENTRY **) xmalloc (MAX_HW * sizeof (CGEN_HW_ENTRY *));
628
629   cd->hw_table.init_entries = init;
630   cd->hw_table.entry_size = sizeof (CGEN_HW_ENTRY);
631   memset (selected, 0, MAX_HW * sizeof (CGEN_HW_ENTRY *));
632   /* ??? For now we just use machs to determine which ones we want.  */
633   for (i = 0; init[i].name != NULL; ++i)
634     if (CGEN_HW_ATTR_VALUE (&init[i], CGEN_HW_MACH)
635         & machs)
636       selected[init[i].type] = &init[i];
637   cd->hw_table.entries = selected;
638   cd->hw_table.num_entries = MAX_HW;
639 }
640
641 /* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */
642
643 static void
644 build_ifield_table (cd)
645      CGEN_CPU_TABLE *cd;
646 {
647   cd->ifld_table = & @arch@_cgen_ifld_table[0];
648 }
649
650 /* Subroutine of @arch@_cgen_cpu_open to build the hardware table.  */
651
652 static void
653 build_operand_table (cd)
654      CGEN_CPU_TABLE *cd;
655 {
656   int i;
657   int machs = cd->machs;
658   const CGEN_OPERAND *init = & @arch@_cgen_operand_table[0];
659   /* MAX_OPERANDS is only an upper bound on the number of selected entries.
660      However each entry is indexed by it's enum so there can be holes in
661      the table.  */
662   const CGEN_OPERAND **selected =
663     (const CGEN_OPERAND **) xmalloc (MAX_OPERANDS * sizeof (CGEN_OPERAND *));
664
665   cd->operand_table.init_entries = init;
666   cd->operand_table.entry_size = sizeof (CGEN_OPERAND);
667   memset (selected, 0, MAX_OPERANDS * sizeof (CGEN_OPERAND *));
668   /* ??? For now we just use mach to determine which ones we want.  */
669   for (i = 0; init[i].name != NULL; ++i)
670     if (CGEN_OPERAND_ATTR_VALUE (&init[i], CGEN_OPERAND_MACH)
671         & machs)
672       selected[init[i].type] = &init[i];
673   cd->operand_table.entries = selected;
674   cd->operand_table.num_entries = MAX_OPERANDS;
675 }
676
677 /* Subroutine of @arch@_cgen_cpu_open to build the hardware table.
678    ??? This could leave out insns not supported by the specified mach/isa,
679    but that would cause errors like \"foo only supported by bar\" to become
680    \"unknown insn\", so for now we include all insns and require the app to
681    do the checking later.
682    ??? On the other hand, parsing of such insns may require their hardware or
683    operand elements to be in the table [which they mightn't be].  */
684
685 static void
686 build_insn_table (cd)
687      CGEN_CPU_TABLE *cd;
688 {
689   int i;
690   const CGEN_IBASE *ib = & @arch@_cgen_insn_table[0];
691   CGEN_INSN *insns = (CGEN_INSN *) xmalloc (MAX_INSNS * sizeof (CGEN_INSN));
692
693   memset (insns, 0, MAX_INSNS * sizeof (CGEN_INSN));
694   for (i = 0; i < MAX_INSNS; ++i)
695     insns[i].base = &ib[i];
696   cd->insn_table.init_entries = insns;
697   cd->insn_table.entry_size = sizeof (CGEN_IBASE);
698   cd->insn_table.num_init_entries = MAX_INSNS;
699 }
700
701 /* Subroutine of @arch@_cgen_cpu_open to rebuild the tables.  */
702
703 static void
704 @arch@_cgen_rebuild_tables (cd)
705      CGEN_CPU_TABLE *cd;
706 {
707   int i;
708   unsigned int isas = cd->isas;
709   unsigned int machs = cd->machs;
710
711   cd->int_insn_p = CGEN_INT_INSN_P;
712
713   /* Data derived from the isa spec.  */
714 #define UNSET (CGEN_SIZE_UNKNOWN + 1)
715   cd->default_insn_bitsize = UNSET;
716   cd->base_insn_bitsize = UNSET;
717   cd->min_insn_bitsize = 65535; /* some ridiculously big number */
718   cd->max_insn_bitsize = 0;
719   for (i = 0; i < MAX_ISAS; ++i)
720     if (((1 << i) & isas) != 0)
721       {
722         const CGEN_ISA *isa = & @arch@_cgen_isa_table[i];
723
724         /* Default insn sizes of all selected isas must be
725            equal or we set the result to 0, meaning \"unknown\".  */
726         if (cd->default_insn_bitsize == UNSET)
727           cd->default_insn_bitsize = isa->default_insn_bitsize;
728         else if (isa->default_insn_bitsize == cd->default_insn_bitsize)
729           ; /* this is ok */
730         else
731           cd->default_insn_bitsize = CGEN_SIZE_UNKNOWN;
732
733         /* Base insn sizes of all selected isas must be equal
734            or we set the result to 0, meaning \"unknown\".  */
735         if (cd->base_insn_bitsize == UNSET)
736           cd->base_insn_bitsize = isa->base_insn_bitsize;
737         else if (isa->base_insn_bitsize == cd->base_insn_bitsize)
738           ; /* this is ok */
739         else
740           cd->base_insn_bitsize = CGEN_SIZE_UNKNOWN;
741
742         /* Set min,max insn sizes.  */
743         if (isa->min_insn_bitsize < cd->min_insn_bitsize)
744           cd->min_insn_bitsize = isa->min_insn_bitsize;
745         if (isa->max_insn_bitsize > cd->max_insn_bitsize)
746           cd->max_insn_bitsize = isa->max_insn_bitsize;
747       }
748
749   /* Data derived from the mach spec.  */
750   for (i = 0; i < MAX_MACHS; ++i)
751     if (((1 << i) & machs) != 0)
752       {
753         const CGEN_MACH *mach = & @arch@_cgen_mach_table[i];
754
755         if (mach->insn_chunk_bitsize != 0)
756         {
757           if (cd->insn_chunk_bitsize != 0 && cd->insn_chunk_bitsize != mach->insn_chunk_bitsize)
758             {
759               fprintf (stderr, \"@arch@_cgen_rebuild_tables: conflicting insn-chunk-bitsize values: `%d' vs. `%d'\\n\",
760                        cd->insn_chunk_bitsize, mach->insn_chunk_bitsize);
761               abort ();
762             }
763
764           cd->insn_chunk_bitsize = mach->insn_chunk_bitsize;
765         }
766       }
767
768   /* Determine which hw elements are used by MACH.  */
769   build_hw_table (cd);
770
771   /* Build the ifield table.  */
772   build_ifield_table (cd);
773
774   /* Determine which operands are used by MACH/ISA.  */
775   build_operand_table (cd);
776
777   /* Build the instruction table.  */
778   build_insn_table (cd);
779 }
780
781 /* Initialize a cpu table and return a descriptor.
782    It's much like opening a file, and must be the first function called.
783    The arguments are a set of (type/value) pairs, terminated with
784    CGEN_CPU_OPEN_END.
785
786    Currently supported values:
787    CGEN_CPU_OPEN_ISAS:    bitmap of values in enum isa_attr
788    CGEN_CPU_OPEN_MACHS:   bitmap of values in enum mach_attr
789    CGEN_CPU_OPEN_BFDMACH: specify 1 mach using bfd name
790    CGEN_CPU_OPEN_ENDIAN:  specify endian choice
791    CGEN_CPU_OPEN_END:     terminates arguments
792
793    ??? Simultaneous multiple isas might not make sense, but it's not (yet)
794    precluded.
795
796    ??? We only support ISO C stdargs here, not K&R.
797    Laziness, plus experiment to see if anything requires K&R - eventually
798    K&R will no longer be supported - e.g. GDB is currently trying this.  */
799
800 CGEN_CPU_DESC
801 @arch@_cgen_cpu_open (enum cgen_cpu_open_arg arg_type, ...)
802 {
803   CGEN_CPU_TABLE *cd = (CGEN_CPU_TABLE *) xmalloc (sizeof (CGEN_CPU_TABLE));
804   static int init_p;
805   unsigned int isas = 0;  /* 0 = \"unspecified\" */
806   unsigned int machs = 0; /* 0 = \"unspecified\" */
807   enum cgen_endian endian = CGEN_ENDIAN_UNKNOWN;
808   va_list ap;
809
810   if (! init_p)
811     {
812       init_tables ();
813       init_p = 1;
814     }
815
816   memset (cd, 0, sizeof (*cd));
817
818   va_start (ap, arg_type);
819   while (arg_type != CGEN_CPU_OPEN_END)
820     {
821       switch (arg_type)
822         {
823         case CGEN_CPU_OPEN_ISAS :
824           isas = va_arg (ap, unsigned int);
825           break;
826         case CGEN_CPU_OPEN_MACHS :
827           machs = va_arg (ap, unsigned int);
828           break;
829         case CGEN_CPU_OPEN_BFDMACH :
830           {
831             const char *name = va_arg (ap, const char *);
832             const CGEN_MACH *mach =
833               lookup_mach_via_bfd_name (@arch@_cgen_mach_table, name);
834
835             machs |= 1 << mach->num;
836             break;
837           }
838         case CGEN_CPU_OPEN_ENDIAN :
839           endian = va_arg (ap, enum cgen_endian);
840           break;
841         default :
842           fprintf (stderr, \"@arch@_cgen_cpu_open: unsupported argument `%d'\\n\",
843                    arg_type);
844           abort (); /* ??? return NULL? */
845         }
846       arg_type = va_arg (ap, enum cgen_cpu_open_arg);
847     }
848   va_end (ap);
849
850   /* mach unspecified means \"all\" */
851   if (machs == 0)
852     machs = (1 << MAX_MACHS) - 1;
853   /* base mach is always selected */
854   machs |= 1;
855   /* isa unspecified means \"all\" */
856   if (isas == 0)
857     isas = (1 << MAX_ISAS) - 1;
858   if (endian == CGEN_ENDIAN_UNKNOWN)
859     {
860       /* ??? If target has only one, could have a default.  */
861       fprintf (stderr, \"@arch@_cgen_cpu_open: no endianness specified\\n\");
862       abort ();
863     }
864
865   cd->isas = isas;
866   cd->machs = machs;
867   cd->endian = endian;
868   /* FIXME: for the sparc case we can determine insn-endianness statically.
869      The worry here is where both data and insn endian can be independently
870      chosen, in which case this function will need another argument.
871      Actually, will want to allow for more arguments in the future anyway.  */
872   cd->insn_endian = endian;
873
874   /* Table (re)builder.  */
875   cd->rebuild_tables = @arch@_cgen_rebuild_tables;
876   @arch@_cgen_rebuild_tables (cd);
877
878   /* Default to not allowing signed overflow.  */
879   cd->signed_overflow_ok_p = 0;
880   
881   return (CGEN_CPU_DESC) cd;
882 }
883
884 /* Cover fn to @arch@_cgen_cpu_open to handle the simple case of 1 isa, 1 mach.
885    MACH_NAME is the bfd name of the mach.  */
886
887 CGEN_CPU_DESC
888 @arch@_cgen_cpu_open_1 (mach_name, endian)
889      const char *mach_name;
890      enum cgen_endian endian;
891 {
892   return @arch@_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
893                                CGEN_CPU_OPEN_ENDIAN, endian,
894                                CGEN_CPU_OPEN_END);
895 }
896
897 /* Close a cpu table.
898    ??? This can live in a machine independent file, but there's currently
899    no place to put this file (there's no libcgen).  libopcodes is the wrong
900    place as some simulator ports use this but they don't use libopcodes.  */
901
902 void
903 @arch@_cgen_cpu_close (cd)
904      CGEN_CPU_DESC cd;
905 {
906   unsigned int i;
907   const CGEN_INSN *insns;
908
909   if (cd->macro_insn_table.init_entries)
910     {
911       insns = cd->macro_insn_table.init_entries;
912       for (i = 0; i < cd->macro_insn_table.num_init_entries; ++i, ++insns)
913         {
914           if (CGEN_INSN_RX ((insns)))
915             regfree (CGEN_INSN_RX (insns));
916         }
917     }
918
919   if (cd->insn_table.init_entries)
920     {
921       insns = cd->insn_table.init_entries;
922       for (i = 0; i < cd->insn_table.num_init_entries; ++i, ++insns)
923         {
924           if (CGEN_INSN_RX (insns))
925             regfree (CGEN_INSN_RX (insns));
926         }
927     }
928
929   
930
931   if (cd->macro_insn_table.init_entries)
932     free ((CGEN_INSN *) cd->macro_insn_table.init_entries);
933
934   if (cd->insn_table.init_entries)
935     free ((CGEN_INSN *) cd->insn_table.init_entries);
936
937   if (cd->hw_table.entries)
938     free ((CGEN_HW_ENTRY *) cd->hw_table.entries);
939
940   if (cd->operand_table.entries)
941     free ((CGEN_HW_ENTRY *) cd->operand_table.entries);
942
943   free (cd);
944 }
945
946 ")
947 )
948
949 ; General initialization C code
950 ; Code is appended during processing.
951
952 (define -cputab-init-code "")
953 (define (cputab-add-init! code)
954   (set! -cputab-init-code (string-append -cputab-init-code code))
955 )
956
957 ; Return the C code to define the various initialization functions.
958 ; This does not include assembler/disassembler specific stuff.
959 ; Generally, this function doesn't do anything.
960 ; It exists to allow a global-static-constructor kind of thing should
961 ; one ever be necessary.
962
963 (define (gen-init-fns)
964   (logit 2 "Generating init fns ...\n")
965   (string-append
966    "\
967 /* Initialize anything needed to be done once, before any cpu_open call.  */
968 static void init_tables PARAMS ((void));
969
970 static void
971 init_tables ()
972 {\n"
973    -cputab-init-code
974    "}\n\n"
975   )
976 )
977 \f
978 ; Top level C code generators
979
980 ; FIXME: Create enum objects for all the enums we explicitly declare here.
981 ; Then they'd be usable and we wouldn't have to special case them here.
982
983 (define (cgen-desc.h)
984   (logit 1 "Generating " (current-arch-name) " desc.h ...\n")
985   (string-write
986    (gen-c-copyright "CPU data header for @arch@."
987                   CURRENT-COPYRIGHT CURRENT-PACKAGE)
988    "\
989 #ifndef @ARCH@_CPU_H
990 #define @ARCH@_CPU_H
991
992 "
993    -gen-hash-defines
994    ; This is defined in arch.h.  It's not defined here as there is yet to
995    ; be a need for it in the assembler/disassembler.
996    ;(gen-enum-decl 'model_type "model types"
997    ;              "MODEL_"
998    ;              (append (map list (map obj:name (current-model-list))) '((max))))
999    ;"#define MAX_MODELS ((int) MODEL_MAX)\n\n"
1000    "/* Enums.  */\n\n"
1001    (string-map gen-decl (current-enum-list))
1002    "/* Attributes.  */\n\n"
1003    (string-map gen-decl (current-attr-list))
1004    "/* Number of architecture variants.  */\n"
1005    ; If there is only 1 isa, leave out special handling.  */
1006    (if (= (length (current-isa-list)) 1)
1007        "#define MAX_ISAS  1\n"
1008        "#define MAX_ISAS  ((int) ISA_MAX)\n")
1009    "#define MAX_MACHS ((int) MACH_MAX)\n\n"
1010    gen-ifld-decls
1011    gen-hw-decls
1012    gen-operand-decls
1013    gen-insn-decls
1014    "/* cgen.h uses things we just defined.  */\n"
1015    "#include \"opcode/cgen.h\"\n\n"
1016    "extern const struct cgen_ifld @arch@_cgen_ifld_table[];\n\n"
1017    -gen-attr-table-decls
1018    -gen-mach-table-decls
1019    gen-hw-table-decls
1020    "\n"
1021    (lambda () (gen-extra-cpu.h (opc-file-path) (current-arch-name)))
1022    "
1023
1024 #endif /* @ARCH@_CPU_H */
1025 "
1026    )
1027 )
1028
1029 ; This file contains the "top level" definitions of the cpu.
1030 ; This includes various elements of the description file, expressed in C.
1031 ;
1032 ; ??? A lot of this file can go in a machine-independent file!  However,
1033 ; some simulators don't use the cgen opcodes support so there is currently
1034 ; no place to put this file.  To be revisited when we do have such a place.
1035
1036 (define (cgen-desc.c)
1037   (logit 1 "Generating " (current-arch-name) " desc.c ...\n")
1038   (string-write
1039    (gen-c-copyright "CPU data for @arch@."
1040                   CURRENT-COPYRIGHT CURRENT-PACKAGE)
1041    "\
1042 #include \"sysdep.h\"
1043 #include <stdio.h>
1044 #include <stdarg.h>
1045 #include \"ansidecl.h\"
1046 #include \"bfd.h\"
1047 #include \"symcat.h\"
1048 #include \"@arch@-desc.h\"
1049 #include \"@arch@-opc.h\"
1050 #include \"opintl.h\"
1051 #include \"libiberty.h\"
1052 #include \"xregex.h\"
1053 \n"
1054    (lambda () (gen-extra-cpu.c (opc-file-path) (current-arch-name)))
1055    gen-attr-table-defns
1056    -gen-isa-table-defns
1057    -gen-mach-table-defns
1058    gen-hw-table-defns
1059    gen-ifld-defns
1060    gen-multi-ifield-nodes
1061    gen-operand-table
1062    gen-insn-table
1063    gen-init-fns
1064    -gen-cpu-open
1065    )
1066 )