Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / sim / igen / ld-decode.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3 Copyright 2002-2022 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 /* 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
34
35 static const name_map decode_type_map[] = {
36 {"normal", normal_decode_rule},
37 {"boolean", boolean_rule},
38 {NULL, normal_decode_rule},
39 };
40
41 static const name_map decode_gen_map[] = {
42 {"array", array_gen},
43 {"switch", switch_gen},
44 {"padded-switch", padded_switch_gen},
45 {"goto-switch", goto_switch_gen},
46 {NULL, -1},
47 };
48
49 static const name_map decode_reserved_map[] = {
50 {"zero-reserved", 1},
51 {NULL, 0},
52 };
53
54 static const name_map decode_duplicates_map[] = {
55 {"duplicate", 1},
56 {NULL, 0},
57 };
58
59 static const name_map decode_combine_map[] = {
60 {"combine", 1},
61 {NULL, 0},
62 };
63
64 static const name_map decode_search_map[] = {
65 {"constants", decode_find_constants},
66 {"mixed", decode_find_mixed},
67 {"strings", decode_find_strings},
68 {NULL, decode_find_mixed},
69 };
70
71
72 static void
73 set_bits (int bit[max_insn_bit_size], unsigned64 value)
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
85 decode_table *
86 load_decode_table (char *file_name)
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)
103 new_rule->gen =
104 name2i (options.decode.overriding_gen, decode_gen_map);
105 else
106 new_rule->gen = name2i (decode_options, decode_gen_map);
107 if (new_rule->gen == padded_switch_gen && options.decode.switch_as_goto)
108 new_rule->gen = goto_switch_gen;
109 if (options.decode.zero_reserved)
110 new_rule->with_zero_reserved = 1;
111 else
112 new_rule->with_zero_reserved =
113 name2i (decode_options, decode_reserved_map);
114 if (options.decode.duplicate)
115 new_rule->with_duplicates = 1;
116 else
117 new_rule->with_duplicates =
118 name2i (decode_options, decode_duplicates_map);
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,
167 entry->
168 field
169 [decode_force_first_field]);
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,
180 entry->
181 field[decode_force_last_field]);
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 */
218 (*last)->path = ZALLOC (decode_path);
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)
245 set_bits (cond->mask,
246 a2i (entry->
247 field[field_nr + decode_cond_mask_field]));
248 if (entry->nr_fields > field_nr + decode_cond_value_field)
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 }
268 if (entry->nr_fields > field_nr + decode_cond_word_nr_field)
269 cond->word_nr =
270 a2i (entry->field[field_nr + decode_cond_word_nr_field]);
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
285
286 int
287 decode_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
307 static void
308 dump_decode_cond (lf *file, char *prefix, decode_cond *cond, char *suffix)
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
325 static void
326 dump_decode_conds (lf *file, char *prefix, decode_cond *cond, char *suffix)
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
338 void
339 dump_decode_rule (lf *file, char *prefix, decode_table *rule, char *suffix)
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, ")");
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));
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);
352 dump_filter (file, "\n(constant_field_names \"",
353 rule->constant_field_names, "\")");
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
371 static void
372 dump_decode_rules (lf *file, char *prefix, decode_table *rule, char *suffix)
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
385 igen_options options;
386
387 int
388 main (int argc, char **argv)
389 {
390 lf *l;
391 decode_table *rules;
392
393 INIT_OPTIONS ();
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 0.039074 seconds and 4 git commands to generate.