OSDN Git Service

Hand patch: update to github/binutils.
[pf3gnuchains/pf3gnuchains4x.git] / cgen / opc-opinst.scm
1 ; Operand instance support.
2 ; Copyright (C) 2000, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
4
5 ; Return C code to define one instance of operand object OP.
6 ; TYPE is one of "INPUT" or "OUTPUT".
7
8 (define (/gen-operand-instance op type)
9   (let ((index (op:index op)))
10     (string-append "  { "
11                    type ", "
12                    "\"" (gen-sym op) "\", "
13                    (hw-enum (op:type op)) ", "
14                    ; FIXME: Revisit CGEN_ prefix, use MODE (FOO) instead.
15                    "CGEN_" (mode:enum (op:mode op)) ", "
16                    ; FIXME: We don't handle memory properly yet.  Later.
17                    (cond ((memory? (op:type op))
18                           "0, 0")
19                          ((has-attr? op 'SEM-ONLY)
20                           "0, 0")
21                          ((eq? (hw-index:type index) 'ifield)
22                           (if (= (ifld-length (hw-index:value index)) 0)
23                               "0, 0"
24                               (string-append "OP_ENT ("
25                                              (string-upcase (gen-sym op))
26                                              "), 0")))
27                          ((eq? (hw-index:type index) 'constant)
28                           (string-append "0, "
29                                          (number->string (hw-index:value index))))
30                          ((eq? (hw-index:type index) 'enum)
31                           (let ((sym (hw-index-enum-name index))
32                                 (obj (hw-index-enum-obj index)))
33                             (string-append "0, "
34                                            (gen-enum-sym obj sym))))
35                          (else "0, 0"))
36                    ", " (if (op:cond? op) "COND_REF" "0")
37                    " },\n"))
38 )
39
40 ; Return C code to define arrays of operand instances read from and written
41 ; to by <sformat> SFMT.
42 ; This is based on the semantics of the instruction.
43 ; ??? All runtime chosen values (e.g. a particular register in a register bank)
44 ; is assumed to be selected statically by the instruction.  When some cpu
45 ; violates this assumption (say because a previous instruction determines
46 ; which register(s) the next instruction operates on), this will need
47 ; additional support.
48
49 (define (/gen-operand-instance-table sfmt)
50   (let ((ins (sfmt-in-ops sfmt))
51         (outs (sfmt-out-ops sfmt)))
52     ; This used to exclude outputing anything if there were no ins or outs.
53     (gen-obj-sanitize
54      (sfmt-eg-insn sfmt) ; sanitize based on the example insn
55      (string-append
56       "static const CGEN_OPINST "
57       (gen-sym sfmt) "_ops[] ATTRIBUTE_UNUSED = {\n"
58       (string-map (lambda (op) (/gen-operand-instance op "INPUT"))
59                   ins)
60       (string-map (lambda (op)  (/gen-operand-instance op "OUTPUT"))
61                   outs)
62       "  { END, (const char *)0, (enum cgen_hw_type)0, (enum cgen_mode)0, (enum cgen_operand_type)0, 0, 0 }\n};\n\n")))
63 )
64
65 (define (/gen-operand-instance-tables)
66   (string-write
67    "\
68 /* Operand references.  */
69
70 "
71    (gen-define-with-symcat "OP_ENT(op) @ARCH@_OPERAND_" "op")
72 "\
73 #define INPUT CGEN_OPINST_INPUT
74 #define OUTPUT CGEN_OPINST_OUTPUT
75 #define END CGEN_OPINST_END
76 #define COND_REF CGEN_OPINST_COND_REF
77
78 "
79    (lambda () (string-write-map /gen-operand-instance-table (current-sfmt-list)))
80    "\
81 #undef OP_ENT
82 #undef INPUT
83 #undef OUTPUT
84 #undef END
85 #undef COND_REF
86
87 "
88    )
89 )
90
91 ; Return C code for INSN's operand instance table.
92
93 (define (gen-operand-instance-ref insn)
94   (let* ((sfmt (insn-sfmt insn))
95          (ins (sfmt-in-ops sfmt))
96          (outs (sfmt-out-ops sfmt)))
97     (if (and (null? ins) (null? outs))
98         "0"
99         (string-append "& " (gen-sym sfmt) "_ops[0]")))
100 )
101
102 ; Return C code to define a table to lookup an insn's operand instance table.
103
104 (define (/gen-insn-opinst-lookup-table)
105   (string-list
106    "/* Operand instance lookup table.  */\n\n"
107    "static const CGEN_OPINST *@arch@_cgen_opinst_table[MAX_INSNS] = {\n"
108    "  0,\n" ; null first entry
109    (string-list-map
110     (lambda (insn)
111       (gen-obj-sanitize
112        insn
113        (string-append "  & " (gen-sym (insn-sfmt insn)) "_ops[0],\n")))
114     (current-insn-list))
115    "};\n\n"
116    "\
117 /* Function to call before using the operand instance table.  */
118
119 void
120 @arch@_cgen_init_opinst_table (cd)
121      CGEN_CPU_DESC cd;
122 {
123   int i;
124   const CGEN_OPINST **oi = & @arch@_cgen_opinst_table[0];
125   CGEN_INSN *insns = (CGEN_INSN *) cd->insn_table.init_entries;
126   for (i = 0; i < MAX_INSNS; ++i)
127     insns[i].opinst = oi[i];
128 }
129 "
130    )
131 )
132
133 ; Return the maximum number of operand instances used by any insn.
134 ; If not generating the operand instance table, use a heuristic.
135
136 (define (max-operand-instances)
137   (if /opcodes-build-operand-instance-table?
138       (apply max
139              (map (lambda (insn)
140                     (+ (length (sfmt-in-ops (insn-sfmt insn)))
141                        (length (sfmt-out-ops (insn-sfmt insn)))))
142                   (current-insn-list)))
143       8) ; FIXME: for now
144 )
145
146 ; Generate $arch-opinst.c.
147
148 (define (cgen-opinst.c)
149   (logit 1 "Generating " (current-arch-name) "-opinst.c ...\n")
150
151   ; If instruction semantics haven't been analyzed, do that now.
152   (if (not (arch-semantics-analyzed? CURRENT-ARCH))
153       (begin
154         (logit 1 "Instruction semantics weren't analyzed when .cpu file was loaded.\n")
155         (logit 1 "Doing so now ...\n")
156         (arch-analyze-insns! CURRENT-ARCH
157                              #t ; include aliases
158                              #t) ; /opcodes-build-operand-instance-table?
159         ))
160
161   (string-write
162    (gen-c-copyright "Semantic operand instances for @arch@."
163                   CURRENT-COPYRIGHT CURRENT-PACKAGE)
164    "\
165 #include \"sysdep.h\"
166 #include \"ansidecl.h\"
167 #include \"bfd.h\"
168 #include \"symcat.h\"
169 #include \"@prefix@-desc.h\"
170 #include \"@prefix@-opc.h\"
171 \n"
172    /gen-operand-instance-tables
173    /gen-insn-opinst-lookup-table
174    )
175 )