2002-11-21 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / sim / igen / gen-itable.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3 Copyright 2002 Free Software Foundation, Inc.
4
5 Contributed by Andrew Cagney.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24
25
26 #include "misc.h"
27 #include "lf.h"
28 #include "table.h"
29 #include "filter.h"
30 #include "igen.h"
31
32 #include "ld-insn.h"
33 #include "ld-decode.h"
34
35 #include "gen.h"
36
37 #include "gen-itable.h"
38
39 #ifndef NULL
40 #define NULL 0
41 #endif
42
43
44
45 typedef struct _itable_info {
46 int sizeof_form;
47 int sizeof_name;
48 int sizeof_file;
49 } itable_info;
50
51
52 static void
53 itable_h_insn (lf *file,
54 insn_table *entry,
55 insn_entry *instruction,
56 void *data)
57 {
58 int len;
59 itable_info *info = data;
60 lf_print__line_ref (file, instruction->line);
61 lf_printf (file, " ");
62 print_function_name (file,
63 instruction->name,
64 instruction->format_name,
65 NULL,
66 NULL,
67 function_name_prefix_itable);
68 lf_printf (file, ",\n");
69 /* update summary info */
70 len = strlen (instruction->format_name);
71 if (info->sizeof_form <= len)
72 info->sizeof_form = len + 1;
73 len = strlen (instruction->name);
74 if (info->sizeof_name <= len)
75 info->sizeof_name = len + 1;
76 len = strlen (filter_filename (instruction->line->file_name));
77 if (info->sizeof_file <= len)
78 info->sizeof_file = len + 1;
79 }
80
81
82 /* print the list of all the different options */
83
84 static void
85 itable_print_enum (lf *file,
86 filter *set,
87 char *name)
88 {
89 char *elem;
90 lf_printf (file, "typedef enum {\n");
91 lf_indent (file, +2);
92 for (elem = filter_next (set, "");
93 elem != NULL;
94 elem = filter_next (set, elem))
95 {
96 lf_printf (file, "%sitable_%s_%s,\n",
97 options.module.itable.prefix.l, name, elem);
98 if (strlen (options.module.itable.prefix.l) > 0)
99 {
100 lf_indent_suppress (file);
101 lf_printf (file, "#define itable_%s_%s %sitable_%s_%s\n",
102 name, elem, options.module.itable.prefix.l, name, elem);
103 }
104 }
105 lf_printf (file, "nr_%sitable_%ss,\n", options.module.itable.prefix.l, name);
106
107 lf_indent (file, -2);
108 lf_printf (file, "} %sitable_%ss;\n", options.module.itable.prefix.l, name);
109 if (strlen (options.module.itable.prefix.l) > 0)
110 {
111 lf_indent_suppress (file);
112 lf_printf (file, "#define itable_%ss %sitable_%ss\n",
113 name, options.module.itable.prefix.l, name);
114 lf_indent_suppress (file);
115 lf_printf (file, "#define nr_itable_%ss nr_%sitable_%ss\n",
116 name, options.module.itable.prefix.l, name);
117 }
118 }
119
120 /* print an array of the option names as strings */
121
122 static void
123 itable_print_names (lf *file,
124 filter *set,
125 char *name)
126 {
127 char *elem;
128 lf_printf (file, "const char *%sitable_%s_names[nr_%sitable_%ss + 1] = {\n",
129 options.module.itable.prefix.l, name,
130 options.module.itable.prefix.l, name);
131 lf_indent (file, +2);
132 for (elem = filter_next (set, "");
133 elem != NULL;
134 elem = filter_next (set, elem))
135 {
136 lf_printf (file, "\"%s\",\n", elem);
137 }
138 lf_printf (file, "0,\n");
139 lf_indent (file, -2);
140 lf_printf (file, "};\n");
141 }
142
143 extern void
144 gen_itable_h (lf *file,
145 insn_table *isa)
146 {
147 itable_info *info = ZALLOC (itable_info);
148
149 /* output an enumerated type for each instruction */
150 lf_printf (file, "typedef enum {\n");
151 insn_table_traverse_insn (file, isa, itable_h_insn, info);
152 lf_printf (file, " nr_%sitable_entries,\n", options.module.itable.prefix.l);
153 lf_printf (file, "} %sitable_index;\n", options.module.itable.prefix.l);
154 lf_printf (file, "\n");
155
156 /* output an enumeration type for each flag */
157 itable_print_enum (file, isa->flags, "flag");
158 lf_printf (file, "extern const char *%sitable_flag_names[];\n",
159 options.module.itable.prefix.l);
160 lf_printf (file, "\n");
161
162 /* output an enumeration of all the possible options */
163 itable_print_enum (file, isa->options, "option");
164 lf_printf (file, "extern const char *%sitable_option_names[];\n",
165 options.module.itable.prefix.l);
166 lf_printf (file, "\n");
167
168 /* output an enumeration of all the processor models */
169 itable_print_enum (file, isa->model->processors, "processor");
170 lf_printf (file, "extern const char *%sitable_processor_names[];\n",
171 options.module.itable.prefix.l);
172 lf_printf (file, "\n");
173
174 /* output the table that contains the actual instruction info */
175 lf_printf (file, "typedef struct _%sitable_instruction_info {\n",
176 options.module.itable.prefix.l);
177 lf_printf (file, " %sitable_index nr;\n", options.module.itable.prefix.l);
178 lf_printf (file, " char *format;\n");
179 lf_printf (file, " char *form;\n");
180 lf_printf (file, " char *flags;\n");
181
182 /* nr_itable_* may be zero, so we add 1 to avoid an
183 illegal zero-sized array. */
184 lf_printf (file, " char flag[nr_%sitable_flags + 1];\n",
185 options.module.itable.prefix.l);
186 lf_printf (file, " char *options;\n");
187 lf_printf (file, " char option[nr_%sitable_options + 1];\n",
188 options.module.itable.prefix.l);
189 lf_printf (file, " char *processors;\n");
190 lf_printf (file, " char processor[nr_%sitable_processors + 1];\n",
191 options.module.itable.prefix.l);
192 lf_printf (file, " char *name;\n");
193 lf_printf (file, " char *file;\n");
194 lf_printf (file, " int line_nr;\n");
195 lf_printf (file, "} %sitable_info;\n", options.module.itable.prefix.l);
196 lf_printf (file, "\n");
197 lf_printf (file, "extern %sitable_info %sitable[nr_%sitable_entries];\n",
198 options.module.itable.prefix.l, options.module.itable.prefix.l,
199 options.module.itable.prefix.l);
200 if (strlen (options.module.itable.prefix.l) > 0)
201 {
202 lf_indent_suppress (file);
203 lf_printf (file, "#define itable %sitable\n",
204 options.module.itable.prefix.l);
205 }
206 lf_printf (file, "\n");
207
208 /* output an enum defining the max size of various itable members */
209 lf_printf (file, "enum {\n");
210 lf_printf (file, " sizeof_%sitable_form = %d,\n",
211 options.module.itable.prefix.l, info->sizeof_form);
212 lf_printf (file, " sizeof_%sitable_name = %d,\n",
213 options.module.itable.prefix.l, info->sizeof_name);
214 lf_printf (file, " sizeof_%sitable_file = %d,\n",
215 options.module.itable.prefix.l, info->sizeof_file);
216 lf_printf (file, "};\n");
217 }
218
219
220 /****************************************************************/
221
222 static void
223 itable_print_set (lf *file,
224 filter *set,
225 filter *members)
226 {
227 char *elem;
228 lf_printf (file, "\"");
229 elem = filter_next (members, "");
230 if (elem != NULL)
231 {
232 while (1)
233 {
234 lf_printf (file, "%s", elem);
235 elem = filter_next (members, elem);
236 if (elem == NULL)
237 break;
238 lf_printf (file, ",");
239 }
240 }
241 lf_printf (file, "\",\n");
242
243 lf_printf(file, "{");
244 for (elem = filter_next (set, "");
245 elem != NULL;
246 elem = filter_next (set, elem))
247 {
248 if (filter_is_member (members, elem))
249 {
250 lf_printf (file, " 1,");
251 }
252 else
253 {
254 lf_printf (file, " 0,");
255 }
256
257 }
258 /* always print a dummy element, to avoid empty initializers. */
259 lf_printf(file, " 99 },\n");
260 }
261
262
263 static void
264 itable_c_insn (lf *file,
265 insn_table *isa,
266 insn_entry *instruction,
267 void *data)
268 {
269 lf_printf (file, "{ ");
270 lf_indent (file, +2);
271 print_function_name (file,
272 instruction->name,
273 instruction->format_name,
274 NULL,
275 NULL,
276 function_name_prefix_itable);
277 lf_printf (file, ",\n");
278 lf_printf (file, "\"");
279 print_insn_words (file, instruction);
280 lf_printf (file, "\",\n");
281 lf_printf (file, "\"%s\",\n", instruction->format_name);
282
283 itable_print_set (file, isa->flags, instruction->flags);
284 itable_print_set (file, isa->options, instruction->options);
285 itable_print_set (file, isa->model->processors, instruction->processors);
286
287 lf_printf(file, "\"%s\",\n", instruction->name);
288 lf_printf(file, "\"%s\",\n",
289 filter_filename (instruction->line->file_name));
290 lf_printf(file, "%d,\n", instruction->line->line_nr);
291 lf_printf(file, "},\n");
292 lf_indent (file, -2);
293 }
294
295
296 extern void
297 gen_itable_c (lf *file,
298 insn_table *isa)
299 {
300 /* leader */
301 lf_printf(file, "#include \"%sitable.h\"\n", options.module.itable.prefix.l);
302 lf_printf(file, "\n");
303
304 /* FIXME - output model data??? */
305 /* FIXME - output assembler data??? */
306
307 /* output the flag, option and processor name tables */
308 itable_print_names (file, isa->flags, "flag");
309 itable_print_names (file, isa->options, "option");
310 itable_print_names (file, isa->model->processors, "processor");
311
312 /* output the table that contains the actual instruction info */
313 lf_printf (file, "%sitable_info %sitable[nr_%sitable_entries] = {\n",
314 options.module.itable.prefix.l,
315 options.module.itable.prefix.l,
316 options.module.itable.prefix.l);
317 insn_table_traverse_insn (file, isa, itable_c_insn, NULL);
318
319 lf_printf(file, "};\n");
320 }
This page took 0.0379 seconds and 5 git commands to generate.