f3c848c56b01b4ebd699f3761e3d45b25777a74f
[external/binutils.git] / sim / igen / gen-itable.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3    Copyright 2002, 2007 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 {
47   int sizeof_form;
48   int sizeof_name;
49   int sizeof_file;
50 }
51 itable_info;
52
53
54 static void
55 itable_h_insn (lf *file,
56                insn_table *entry, insn_entry * instruction, 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, NULL, function_name_prefix_itable);
66   lf_printf (file, ",\n");
67   /* update summary info */
68   len = strlen (instruction->format_name);
69   if (info->sizeof_form <= len)
70     info->sizeof_form = len + 1;
71   len = strlen (instruction->name);
72   if (info->sizeof_name <= len)
73     info->sizeof_name = len + 1;
74   len = strlen (filter_filename (instruction->line->file_name));
75   if (info->sizeof_file <= len)
76     info->sizeof_file = len + 1;
77 }
78
79
80 /* print the list of all the different options */
81
82 static void
83 itable_print_enum (lf *file, filter *set, char *name)
84 {
85   char *elem;
86   lf_printf (file, "typedef enum {\n");
87   lf_indent (file, +2);
88   for (elem = filter_next (set, "");
89        elem != NULL; elem = filter_next (set, elem))
90     {
91       lf_printf (file, "%sitable_%s_%s,\n",
92                  options.module.itable.prefix.l, name, elem);
93       if (strlen (options.module.itable.prefix.l) > 0)
94         {
95           lf_indent_suppress (file);
96           lf_printf (file, "#define itable_%s_%s %sitable_%s_%s\n",
97                      name, elem, options.module.itable.prefix.l, name, elem);
98         }
99     }
100   lf_printf (file, "nr_%sitable_%ss,\n", options.module.itable.prefix.l,
101              name);
102
103   lf_indent (file, -2);
104   lf_printf (file, "} %sitable_%ss;\n", options.module.itable.prefix.l, name);
105   if (strlen (options.module.itable.prefix.l) > 0)
106     {
107       lf_indent_suppress (file);
108       lf_printf (file, "#define itable_%ss %sitable_%ss\n",
109                  name, options.module.itable.prefix.l, name);
110       lf_indent_suppress (file);
111       lf_printf (file, "#define nr_itable_%ss nr_%sitable_%ss\n",
112                  name, options.module.itable.prefix.l, name);
113     }
114 }
115
116 /* print an array of the option names as strings */
117
118 static void
119 itable_print_names (lf *file, filter *set, char *name)
120 {
121   char *elem;
122   lf_printf (file, "const char *%sitable_%s_names[nr_%sitable_%ss + 1] = {\n",
123              options.module.itable.prefix.l, name,
124              options.module.itable.prefix.l, name);
125   lf_indent (file, +2);
126   for (elem = filter_next (set, "");
127        elem != NULL; elem = filter_next (set, elem))
128     {
129       lf_printf (file, "\"%s\",\n", elem);
130     }
131   lf_printf (file, "0,\n");
132   lf_indent (file, -2);
133   lf_printf (file, "};\n");
134 }
135
136 extern void
137 gen_itable_h (lf *file, insn_table *isa)
138 {
139   itable_info *info = ZALLOC (itable_info);
140
141   /* output an enumerated type for each instruction */
142   lf_printf (file, "typedef enum {\n");
143   insn_table_traverse_insn (file, isa, itable_h_insn, info);
144   lf_printf (file, "  nr_%sitable_entries,\n",
145              options.module.itable.prefix.l);
146   lf_printf (file, "} %sitable_index;\n", options.module.itable.prefix.l);
147   lf_printf (file, "\n");
148
149   /* output an enumeration type for each flag */
150   itable_print_enum (file, isa->flags, "flag");
151   lf_printf (file, "extern const char *%sitable_flag_names[];\n",
152              options.module.itable.prefix.l);
153   lf_printf (file, "\n");
154
155   /* output an enumeration of all the possible options */
156   itable_print_enum (file, isa->options, "option");
157   lf_printf (file, "extern const char *%sitable_option_names[];\n",
158              options.module.itable.prefix.l);
159   lf_printf (file, "\n");
160
161   /* output an enumeration of all the processor models */
162   itable_print_enum (file, isa->model->processors, "processor");
163   lf_printf (file, "extern const char *%sitable_processor_names[];\n",
164              options.module.itable.prefix.l);
165   lf_printf (file, "\n");
166
167   /* output the table that contains the actual instruction info */
168   lf_printf (file, "typedef struct _%sitable_instruction_info {\n",
169              options.module.itable.prefix.l);
170   lf_printf (file, "  %sitable_index nr;\n", options.module.itable.prefix.l);
171   lf_printf (file, "  char *format;\n");
172   lf_printf (file, "  char *form;\n");
173   lf_printf (file, "  char *flags;\n");
174
175   /* nr_itable_* may be zero, so we add 1 to avoid an
176      illegal zero-sized array. */
177   lf_printf (file, "  char flag[nr_%sitable_flags + 1];\n",
178              options.module.itable.prefix.l);
179   lf_printf (file, "  char *options;\n");
180   lf_printf (file, "  char option[nr_%sitable_options + 1];\n",
181              options.module.itable.prefix.l);
182   lf_printf (file, "  char *processors;\n");
183   lf_printf (file, "  char processor[nr_%sitable_processors + 1];\n",
184              options.module.itable.prefix.l);
185   lf_printf (file, "  char *name;\n");
186   lf_printf (file, "  char *file;\n");
187   lf_printf (file, "  int line_nr;\n");
188   lf_printf (file, "} %sitable_info;\n", options.module.itable.prefix.l);
189   lf_printf (file, "\n");
190   lf_printf (file, "extern %sitable_info %sitable[nr_%sitable_entries];\n",
191              options.module.itable.prefix.l, options.module.itable.prefix.l,
192              options.module.itable.prefix.l);
193   if (strlen (options.module.itable.prefix.l) > 0)
194     {
195       lf_indent_suppress (file);
196       lf_printf (file, "#define itable %sitable\n",
197                  options.module.itable.prefix.l);
198     }
199   lf_printf (file, "\n");
200
201   /* output an enum defining the max size of various itable members */
202   lf_printf (file, "enum {\n");
203   lf_printf (file, "  sizeof_%sitable_form = %d,\n",
204              options.module.itable.prefix.l, info->sizeof_form);
205   lf_printf (file, "  sizeof_%sitable_name = %d,\n",
206              options.module.itable.prefix.l, info->sizeof_name);
207   lf_printf (file, "  sizeof_%sitable_file = %d,\n",
208              options.module.itable.prefix.l, info->sizeof_file);
209   lf_printf (file, "};\n");
210 }
211
212
213 /****************************************************************/
214
215 static void
216 itable_print_set (lf *file, filter *set, filter *members)
217 {
218   char *elem;
219   lf_printf (file, "\"");
220   elem = filter_next (members, "");
221   if (elem != NULL)
222     {
223       while (1)
224         {
225           lf_printf (file, "%s", elem);
226           elem = filter_next (members, elem);
227           if (elem == NULL)
228             break;
229           lf_printf (file, ",");
230         }
231     }
232   lf_printf (file, "\",\n");
233
234   lf_printf (file, "{");
235   for (elem = filter_next (set, "");
236        elem != NULL; elem = filter_next (set, elem))
237     {
238       if (filter_is_member (members, elem))
239         {
240           lf_printf (file, " 1,");
241         }
242       else
243         {
244           lf_printf (file, " 0,");
245         }
246
247     }
248   /* always print a dummy element, to avoid empty initializers. */
249   lf_printf (file, " 99 },\n");
250 }
251
252
253 static void
254 itable_c_insn (lf *file,
255                insn_table *isa, insn_entry * instruction, void *data)
256 {
257   lf_printf (file, "{ ");
258   lf_indent (file, +2);
259   print_function_name (file,
260                        instruction->name,
261                        instruction->format_name,
262                        NULL, NULL, function_name_prefix_itable);
263   lf_printf (file, ",\n");
264   lf_printf (file, "\"");
265   print_insn_words (file, instruction);
266   lf_printf (file, "\",\n");
267   lf_printf (file, "\"%s\",\n", instruction->format_name);
268
269   itable_print_set (file, isa->flags, instruction->flags);
270   itable_print_set (file, isa->options, instruction->options);
271   itable_print_set (file, isa->model->processors, instruction->processors);
272
273   lf_printf (file, "\"%s\",\n", instruction->name);
274   lf_printf (file, "\"%s\",\n",
275              filter_filename (instruction->line->file_name));
276   lf_printf (file, "%d,\n", instruction->line->line_nr);
277   lf_printf (file, "},\n");
278   lf_indent (file, -2);
279 }
280
281
282 extern void
283 gen_itable_c (lf *file, insn_table *isa)
284 {
285   /* leader */
286   lf_printf (file, "#include \"%sitable.h\"\n",
287              options.module.itable.prefix.l);
288   lf_printf (file, "\n");
289
290   /* FIXME - output model data??? */
291   /* FIXME - output assembler data??? */
292
293   /* output the flag, option and processor name tables */
294   itable_print_names (file, isa->flags, "flag");
295   itable_print_names (file, isa->options, "option");
296   itable_print_names (file, isa->model->processors, "processor");
297
298   /* output the table that contains the actual instruction info */
299   lf_printf (file, "%sitable_info %sitable[nr_%sitable_entries] = {\n",
300              options.module.itable.prefix.l,
301              options.module.itable.prefix.l, options.module.itable.prefix.l);
302   insn_table_traverse_insn (file, isa, itable_c_insn, NULL);
303
304   lf_printf (file, "};\n");
305 }