Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / sim / igen / ld-decode.c
CommitLineData
feaee4bd
AC
1/* The IGEN simulator generator for GDB, the GNU Debugger.
2
88b9d363 3 Copyright 2002-2022 Free Software Foundation, Inc.
feaee4bd
AC
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
4744ac1b 11 the Free Software Foundation; either version 3 of the License, or
feaee4bd
AC
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
4744ac1b 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
feaee4bd 21
c906108c
SS
22
23/* load the opcode stat structure */
24
25#include "misc.h"
26#include "lf.h"
27#include "table.h"
28#include "filter.h"
29
30#include "igen.h"
31
32#include "ld-decode.h"
33
c906108c
SS
34
35static const name_map decode_type_map[] = {
4e0bf4c4
AC
36 {"normal", normal_decode_rule},
37 {"boolean", boolean_rule},
38 {NULL, normal_decode_rule},
c906108c
SS
39};
40
41static const name_map decode_gen_map[] = {
4e0bf4c4
AC
42 {"array", array_gen},
43 {"switch", switch_gen},
44 {"padded-switch", padded_switch_gen},
45 {"goto-switch", goto_switch_gen},
46 {NULL, -1},
c906108c
SS
47};
48
49static const name_map decode_reserved_map[] = {
4e0bf4c4
AC
50 {"zero-reserved", 1},
51 {NULL, 0},
c906108c
SS
52};
53
54static const name_map decode_duplicates_map[] = {
4e0bf4c4
AC
55 {"duplicate", 1},
56 {NULL, 0},
c906108c
SS
57};
58
59static const name_map decode_combine_map[] = {
4e0bf4c4
AC
60 {"combine", 1},
61 {NULL, 0},
c906108c
SS
62};
63
64static const name_map decode_search_map[] = {
4e0bf4c4
AC
65 {"constants", decode_find_constants},
66 {"mixed", decode_find_mixed},
67 {"strings", decode_find_strings},
68 {NULL, decode_find_mixed},
c906108c
SS
69};
70
71
72static void
4e0bf4c4 73set_bits (int bit[max_insn_bit_size], unsigned64 value)
c906108c
SS
74{
75 int bit_nr;
76 for (bit_nr = 0; bit_nr < max_insn_bit_size; bit_nr++)
77 {
78 if (bit_nr < options.insn_bit_size)
79 bit[bit_nr] = (value >> (options.insn_bit_size - bit_nr - 1)) & 1;
80 else
81 bit[bit_nr] = 0;
82 }
83}
84
85decode_table *
4e0bf4c4 86load_decode_table (char *file_name)
c906108c
SS
87{
88 table *file = table_open (file_name);
89 table_entry *entry;
90 decode_table *table = NULL;
91 decode_table **curr_rule = &table;
92 while ((entry = table_read (file)) != NULL)
93 {
94 char *decode_options = entry->field[decode_options_field];
95 decode_table *new_rule = ZALLOC (decode_table);
96 if (entry->nr_fields < min_nr_decode_fields)
97 error (entry->line, "Missing decode table fields\n");
98 new_rule->line = entry->line;
99
100 /* the options field */
101 new_rule->type = name2i (decode_options, decode_type_map);
102 if (options.decode.overriding_gen != NULL)
4e0bf4c4
AC
103 new_rule->gen =
104 name2i (options.decode.overriding_gen, decode_gen_map);
c906108c
SS
105 else
106 new_rule->gen = name2i (decode_options, decode_gen_map);
4e0bf4c4 107 if (new_rule->gen == padded_switch_gen && options.decode.switch_as_goto)
c906108c
SS
108 new_rule->gen = goto_switch_gen;
109 if (options.decode.zero_reserved)
110 new_rule->with_zero_reserved = 1;
111 else
4e0bf4c4
AC
112 new_rule->with_zero_reserved =
113 name2i (decode_options, decode_reserved_map);
c906108c
SS
114 if (options.decode.duplicate)
115 new_rule->with_duplicates = 1;
116 else
4e0bf4c4
AC
117 new_rule->with_duplicates =
118 name2i (decode_options, decode_duplicates_map);
c906108c
SS
119 if (options.decode.combine)
120 new_rule->with_combine = 1;
121 else
122 new_rule->with_combine = name2i (decode_options, decode_combine_map);
123 if (new_rule->type == boolean_rule)
124 {
125 char *chp = decode_options;
126 while (*chp != '\0')
127 {
128 if (isdigit (*chp))
129 {
130 new_rule->constant = a2i (chp);
131 break;
132 }
133 chp = skip_to_separator (chp, ",");
134 chp = skip_spaces (chp);
135 }
136 }
137
138 /* First and last */
139 if (entry->nr_fields > decode_first_field
140 && strlen (entry->field[decode_first_field]) > 0)
141 {
142 new_rule->first = target_a2i (options.hi_bit_nr,
143 entry->field[decode_first_field]);
144 if (new_rule->first < 0 || new_rule->first >= options.insn_bit_size)
145 error (new_rule->line, "First field out of range\n");
146 }
147 else
148 new_rule->first = 0;
149 if (entry->nr_fields > decode_last_field
150 && strlen (entry->field[decode_last_field]) > 0)
151 {
152 new_rule->last = target_a2i (options.hi_bit_nr,
153 entry->field[decode_last_field]);
154 if (new_rule->last < 0 || new_rule->last >= options.insn_bit_size)
155 error (new_rule->line, "Last field out of range\n");
156 }
157 else
158 new_rule->last = options.insn_bit_size - 1;
159 if (new_rule->first > new_rule->last)
160 error (new_rule->line, "First must preceed last\n");
161
162 /* force first/last, with default values based on first/last */
163 if (entry->nr_fields > decode_force_first_field
164 && strlen (entry->field[decode_force_first_field]) > 0)
165 {
166 new_rule->force_first = target_a2i (options.hi_bit_nr,
4e0bf4c4
AC
167 entry->
168 field
169 [decode_force_first_field]);
c906108c
SS
170 if (new_rule->force_first < new_rule->first
171 || new_rule->force_first > new_rule->last + 1)
172 error (new_rule->line, "Force first out of range\n");
173 }
174 else
175 new_rule->force_first = new_rule->last + 1;
176 if (entry->nr_fields > decode_force_last_field
177 && strlen (entry->field[decode_force_last_field]) > 0)
178 {
179 new_rule->force_last = target_a2i (options.hi_bit_nr,
4e0bf4c4
AC
180 entry->
181 field[decode_force_last_field]);
c906108c
SS
182 if (new_rule->force_last > new_rule->last
183 || new_rule->force_last < new_rule->first - 1)
184 error (new_rule->line, "Force-last out of range\n");
185 }
186 else
187 new_rule->force_last = new_rule->first - 1;
188
189 /* fields to be treated as constant */
190 if (entry->nr_fields > decode_constant_field_names_field)
191 filter_parse (&new_rule->constant_field_names,
192 entry->field[decode_constant_field_names_field]);
193
194 /* applicable word nr */
195 if (entry->nr_fields > decode_word_nr_field)
196 new_rule->word_nr = a2i (entry->field[decode_word_nr_field]);
197
198 /* required instruction format names */
199 if (entry->nr_fields > decode_format_names_field)
200 filter_parse (&new_rule->format_names,
201 entry->field[decode_format_names_field]);
202
203 /* required processor models */
204 if (entry->nr_fields > decode_model_names_field)
205 filter_parse (&new_rule->model_names,
206 entry->field[decode_model_names_field]);
207
208 /* required paths */
209 if (entry->nr_fields > decode_paths_field
210 && strlen (entry->field[decode_paths_field]) > 0)
211 {
212 decode_path_list **last = &new_rule->paths;
213 char *chp = entry->field[decode_paths_field];
214 do
215 {
216 (*last) = ZALLOC (decode_path_list);
217 /* extra root/zero entry */
4e0bf4c4 218 (*last)->path = ZALLOC (decode_path);
c906108c
SS
219 do
220 {
221 decode_path *entry = ZALLOC (decode_path);
222 entry->opcode_nr = a2i (chp);
223 entry->parent = (*last)->path;
224 (*last)->path = entry;
225 chp = skip_digits (chp);
226 chp = skip_spaces (chp);
227 }
228 while (*chp == '.');
229 last = &(*last)->next;
230 }
231 while (*chp == ',');
232 if (*chp != '\0')
233 error (entry->line, "Invalid path field\n");
234 }
235
236 /* collect up the list of optional special conditions applicable
237 to the rule */
238 {
239 int field_nr = nr_decode_fields;
240 while (entry->nr_fields > field_nr)
241 {
242 decode_cond *cond = ZALLOC (decode_cond);
243 decode_cond **last;
244 if (entry->nr_fields > field_nr + decode_cond_mask_field)
4e0bf4c4
AC
245 set_bits (cond->mask,
246 a2i (entry->
247 field[field_nr + decode_cond_mask_field]));
c906108c 248 if (entry->nr_fields > field_nr + decode_cond_value_field)
4e0bf4c4
AC
249 {
250 if (entry->field[field_nr + decode_cond_value_field][0] ==
251 '!')
252 {
253 cond->is_equal = 0;
254 set_bits (cond->value,
255 a2i (entry->
256 field[field_nr + decode_cond_value_field] +
257 1));
258 }
259 else
260 {
261 cond->is_equal = 1;
262 set_bits (cond->value,
263 a2i (entry->
264 field[field_nr +
265 decode_cond_value_field]));
266 }
267 }
c906108c 268 if (entry->nr_fields > field_nr + decode_cond_word_nr_field)
4e0bf4c4
AC
269 cond->word_nr =
270 a2i (entry->field[field_nr + decode_cond_word_nr_field]);
c906108c
SS
271 field_nr += nr_decode_cond_fields;
272 /* insert it */
273 last = &new_rule->conditions;
274 while (*last != NULL)
275 last = &(*last)->next;
276 *last = cond;
277 }
278 }
279 *curr_rule = new_rule;
280 curr_rule = &new_rule->next;
281 }
282 return table;
283}
284
4e0bf4c4 285
c906108c
SS
286int
287decode_table_max_word_nr (decode_table *entry)
288{
289 int max_word_nr = 0;
290 while (entry != NULL)
291 {
292 decode_cond *cond;
293 if (entry->word_nr > max_word_nr)
294 max_word_nr = entry->word_nr;
295 for (cond = entry->conditions; cond != NULL; cond = cond->next)
296 {
297 if (cond->word_nr > max_word_nr)
298 max_word_nr = cond->word_nr;
299 }
300 entry = entry->next;
301 }
302 return max_word_nr;
303}
304
305
306
307static void
4e0bf4c4 308dump_decode_cond (lf *file, char *prefix, decode_cond *cond, char *suffix)
c906108c
SS
309{
310 lf_printf (file, "%s(decode_cond *) 0x%lx", prefix, (long) cond);
311 if (cond != NULL)
312 {
313 lf_indent (file, +1);
314 lf_printf (file, "\n(word_nr %d)", cond->word_nr);
315 lf_printf (file, "\n(mask 0x%lx)", (long) cond->mask);
316 lf_printf (file, "\n(value 0x%lx)", (long) cond->value);
317 lf_printf (file, "\n(is_equal 0x%lx)", (long) cond->is_equal);
318 lf_printf (file, "\n(next (decode_cond *) 0%lx)", (long) cond->next);
319 lf_indent (file, -1);
320 }
321 lf_printf (file, "%s", suffix);
322}
323
324
325static void
4e0bf4c4 326dump_decode_conds (lf *file, char *prefix, decode_cond *cond, char *suffix)
c906108c
SS
327{
328 lf_printf (file, "%s(decode_cond *) 0x%lx", prefix, (long) cond);
329 while (cond != NULL)
330 {
331 dump_decode_cond (file, "\n(", cond, ")");
332 cond = cond->next;
333 }
334 lf_printf (file, "%s", suffix);
335}
336
337
338void
4e0bf4c4 339dump_decode_rule (lf *file, char *prefix, decode_table *rule, char *suffix)
c906108c
SS
340{
341 lf_printf (file, "%s(decode_table *) 0x%lx", prefix, (long) rule);
342 if (rule != NULL)
343 {
344 lf_indent (file, +1);
345 dump_line_ref (file, "\n(line ", rule->line, ")");
4e0bf4c4
AC
346 lf_printf (file, "\n(type %s)", i2name (rule->type, decode_type_map));
347 lf_printf (file, "\n(gen %s)", i2name (rule->gen, decode_gen_map));
c906108c
SS
348 lf_printf (file, "\n(first %d)", rule->first);
349 lf_printf (file, "\n(last %d)", rule->last);
350 lf_printf (file, "\n(force_first %d)", rule->force_first);
351 lf_printf (file, "\n(force_last %d)", rule->force_last);
4e0bf4c4
AC
352 dump_filter (file, "\n(constant_field_names \"",
353 rule->constant_field_names, "\")");
c906108c
SS
354 lf_printf (file, "\n(constant 0x%x)", rule->constant);
355 lf_printf (file, "\n(word_nr %d)", rule->word_nr);
356 lf_printf (file, "\n(with_zero_reserved %d)", rule->with_zero_reserved);
357 lf_printf (file, "\n(with_duplicates %d)", rule->with_duplicates);
358 lf_printf (file, "\n(with_combine %d)", rule->with_combine);
359 dump_filter (file, "\n(format_names \"", rule->format_names, "\")");
360 dump_filter (file, "\n(model_names \"", rule->model_names, "\")");
361 dump_decode_conds (file, "\n(conditions ", rule->conditions, ")");
362 lf_printf (file, "\n(next 0x%lx)", (long) rule->next);
363 lf_indent (file, -1);
364 }
365 lf_printf (file, "%s", suffix);
366}
367
368
369#ifdef MAIN
370
371static void
4e0bf4c4 372dump_decode_rules (lf *file, char *prefix, decode_table *rule, char *suffix)
c906108c
SS
373{
374 lf_printf (file, "%s", prefix);
375 while (rule != NULL)
376 {
377 lf_indent (file, +1);
378 dump_decode_rule (file, "\n(", rule, ")");
379 lf_indent (file, -1);
380 rule = rule->next;
381 }
382 lf_printf (file, "%s", suffix);
383}
384
385igen_options options;
386
387int
4e0bf4c4 388main (int argc, char **argv)
c906108c
SS
389{
390 lf *l;
391 decode_table *rules;
392
2916e3e1 393 INIT_OPTIONS ();
c906108c
SS
394
395 if (argc != 3)
396 error (NULL, "Usage: decode <decode-file> <hi-bit-nr>\n");
397
398 options.hi_bit_nr = a2i (argv[2]);
399 rules = load_decode_table (argv[1]);
400 l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-ld-insn");
401 dump_decode_rules (l, "(rules ", rules, ")\n");
402
403 return 0;
404}
405#endif
This page took 1.012333 seconds and 4 git commands to generate.