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