1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2024 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
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.
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.
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/>. */
23 /* load the opcode stat structure */
32 #include "ld-decode.h"
35 static const name_map decode_type_map
[] = {
36 {"normal", normal_decode_rule
},
37 {"boolean", boolean_rule
},
38 {NULL
, normal_decode_rule
},
41 static const name_map decode_gen_map
[] = {
43 {"switch", switch_gen
},
44 {"padded-switch", padded_switch_gen
},
45 {"goto-switch", goto_switch_gen
},
49 static const name_map decode_reserved_map
[] = {
54 static const name_map decode_duplicates_map
[] = {
59 static const name_map decode_combine_map
[] = {
65 static const name_map decode_search_map
[] = {
66 {"constants", decode_find_constants
},
67 {"mixed", decode_find_mixed
},
68 {"strings", decode_find_strings
},
69 {NULL
, decode_find_mixed
},
75 set_bits (int bit
[max_insn_bit_size
], uint64_t value
)
78 for (bit_nr
= 0; bit_nr
< max_insn_bit_size
; bit_nr
++)
80 if (bit_nr
< options
.insn_bit_size
)
81 bit
[bit_nr
] = (value
>> (options
.insn_bit_size
- bit_nr
- 1)) & 1;
88 load_decode_table (const char *file_name
)
90 table
*file
= table_open (file_name
);
92 decode_table
*table
= NULL
;
93 decode_table
**curr_rule
= &table
;
94 while ((entry
= table_read (file
)) != NULL
)
96 char *decode_options
= entry
->field
[decode_options_field
];
97 decode_table
*new_rule
= ZALLOC (decode_table
);
98 if (entry
->nr_fields
< min_nr_decode_fields
)
99 error (entry
->line
, "Missing decode table fields\n");
100 new_rule
->line
= entry
->line
;
102 /* the options field */
103 new_rule
->type
= name2i (decode_options
, decode_type_map
);
104 if (options
.decode
.overriding_gen
!= NULL
)
106 name2i (options
.decode
.overriding_gen
, decode_gen_map
);
108 new_rule
->gen
= name2i (decode_options
, decode_gen_map
);
109 if (new_rule
->gen
== padded_switch_gen
&& options
.decode
.switch_as_goto
)
110 new_rule
->gen
= goto_switch_gen
;
111 if (options
.decode
.zero_reserved
)
112 new_rule
->with_zero_reserved
= 1;
114 new_rule
->with_zero_reserved
=
115 name2i (decode_options
, decode_reserved_map
);
116 if (options
.decode
.duplicate
)
117 new_rule
->with_duplicates
= 1;
119 new_rule
->with_duplicates
=
120 name2i (decode_options
, decode_duplicates_map
);
121 if (options
.decode
.combine
)
122 new_rule
->with_combine
= 1;
124 new_rule
->with_combine
= name2i (decode_options
, decode_combine_map
);
125 if (new_rule
->type
== boolean_rule
)
127 char *chp
= decode_options
;
132 new_rule
->constant
= a2i (chp
);
135 chp
= skip_to_separator (chp
, ",");
138 chp
= skip_spaces (chp
);
143 if (entry
->nr_fields
> decode_first_field
144 && strlen (entry
->field
[decode_first_field
]) > 0)
146 new_rule
->first
= target_a2i (options
.hi_bit_nr
,
147 entry
->field
[decode_first_field
]);
148 if (new_rule
->first
< 0 || new_rule
->first
>= options
.insn_bit_size
)
149 error (new_rule
->line
, "First field out of range\n");
153 if (entry
->nr_fields
> decode_last_field
154 && strlen (entry
->field
[decode_last_field
]) > 0)
156 new_rule
->last
= target_a2i (options
.hi_bit_nr
,
157 entry
->field
[decode_last_field
]);
158 if (new_rule
->last
< 0 || new_rule
->last
>= options
.insn_bit_size
)
159 error (new_rule
->line
, "Last field out of range\n");
162 new_rule
->last
= options
.insn_bit_size
- 1;
163 if (new_rule
->first
> new_rule
->last
)
164 error (new_rule
->line
, "First must precede last\n");
166 /* force first/last, with default values based on first/last */
167 if (entry
->nr_fields
> decode_force_first_field
168 && strlen (entry
->field
[decode_force_first_field
]) > 0)
170 new_rule
->force_first
= target_a2i (options
.hi_bit_nr
,
173 [decode_force_first_field
]);
174 if (new_rule
->force_first
< new_rule
->first
175 || new_rule
->force_first
> new_rule
->last
+ 1)
176 error (new_rule
->line
, "Force first out of range\n");
179 new_rule
->force_first
= new_rule
->last
+ 1;
180 if (entry
->nr_fields
> decode_force_last_field
181 && strlen (entry
->field
[decode_force_last_field
]) > 0)
183 new_rule
->force_last
= target_a2i (options
.hi_bit_nr
,
185 field
[decode_force_last_field
]);
186 if (new_rule
->force_last
> new_rule
->last
187 || new_rule
->force_last
< new_rule
->first
- 1)
188 error (new_rule
->line
, "Force-last out of range\n");
191 new_rule
->force_last
= new_rule
->first
- 1;
193 /* fields to be treated as constant */
194 if (entry
->nr_fields
> decode_constant_field_names_field
)
195 filter_parse (&new_rule
->constant_field_names
,
196 entry
->field
[decode_constant_field_names_field
]);
198 /* applicable word nr */
199 if (entry
->nr_fields
> decode_word_nr_field
)
200 new_rule
->word_nr
= a2i (entry
->field
[decode_word_nr_field
]);
202 /* required instruction format names */
203 if (entry
->nr_fields
> decode_format_names_field
)
204 filter_parse (&new_rule
->format_names
,
205 entry
->field
[decode_format_names_field
]);
207 /* required processor models */
208 if (entry
->nr_fields
> decode_model_names_field
)
209 filter_parse (&new_rule
->model_names
,
210 entry
->field
[decode_model_names_field
]);
213 if (entry
->nr_fields
> decode_paths_field
214 && strlen (entry
->field
[decode_paths_field
]) > 0)
216 decode_path_list
**last
= &new_rule
->paths
;
217 char *chp
= entry
->field
[decode_paths_field
];
220 (*last
) = ZALLOC (decode_path_list
);
221 /* extra root/zero entry */
222 (*last
)->path
= ZALLOC (decode_path
);
225 decode_path
*entry
= ZALLOC (decode_path
);
226 entry
->opcode_nr
= a2i (chp
);
227 entry
->parent
= (*last
)->path
;
228 (*last
)->path
= entry
;
229 chp
= skip_digits (chp
);
230 chp
= skip_spaces (chp
);
233 last
= &(*last
)->next
;
237 error (entry
->line
, "Invalid path field\n");
240 /* collect up the list of optional special conditions applicable
243 int field_nr
= nr_decode_fields
;
244 while (entry
->nr_fields
> field_nr
)
246 decode_cond
*cond
= ZALLOC (decode_cond
);
248 if (entry
->nr_fields
> field_nr
+ decode_cond_mask_field
)
249 set_bits (cond
->mask
,
251 field
[field_nr
+ decode_cond_mask_field
]));
252 if (entry
->nr_fields
> field_nr
+ decode_cond_value_field
)
254 if (entry
->field
[field_nr
+ decode_cond_value_field
][0] ==
258 set_bits (cond
->value
,
260 field
[field_nr
+ decode_cond_value_field
] +
266 set_bits (cond
->value
,
269 decode_cond_value_field
]));
272 if (entry
->nr_fields
> field_nr
+ decode_cond_word_nr_field
)
274 a2i (entry
->field
[field_nr
+ decode_cond_word_nr_field
]);
275 field_nr
+= nr_decode_cond_fields
;
277 last
= &new_rule
->conditions
;
278 while (*last
!= NULL
)
279 last
= &(*last
)->next
;
283 *curr_rule
= new_rule
;
284 curr_rule
= &new_rule
->next
;
291 decode_table_max_word_nr (const decode_table
*entry
)
294 while (entry
!= NULL
)
297 if (entry
->word_nr
> max_word_nr
)
298 max_word_nr
= entry
->word_nr
;
299 for (cond
= entry
->conditions
; cond
!= NULL
; cond
= cond
->next
)
301 if (cond
->word_nr
> max_word_nr
)
302 max_word_nr
= cond
->word_nr
;
311 dump_decode_cond (lf
*file
, const char *prefix
, const decode_cond
*cond
,
314 lf_printf (file
, "%s(decode_cond *) %p", prefix
, cond
);
317 lf_indent (file
, +1);
318 lf_printf (file
, "\n(word_nr %d)", cond
->word_nr
);
319 lf_printf (file
, "\n(mask %p)", cond
->mask
);
320 lf_printf (file
, "\n(value %p)", cond
->value
);
321 lf_printf (file
, "\n(is_equal %d)", cond
->is_equal
);
322 lf_printf (file
, "\n(next (decode_cond *) %p)", cond
->next
);
323 lf_indent (file
, -1);
325 lf_printf (file
, "%s", suffix
);
330 dump_decode_conds (lf
*file
, const char *prefix
, const decode_cond
*cond
,
333 lf_printf (file
, "%s(decode_cond *) %p", prefix
, cond
);
336 dump_decode_cond (file
, "\n(", cond
, ")");
339 lf_printf (file
, "%s", suffix
);
344 dump_decode_rule (lf
*file
, const char *prefix
, const decode_table
*rule
,
347 lf_printf (file
, "%s(decode_table *) %p", prefix
, rule
);
350 lf_indent (file
, +1);
351 dump_line_ref (file
, "\n(line ", rule
->line
, ")");
352 lf_printf (file
, "\n(type %s)", i2name (rule
->type
, decode_type_map
));
353 lf_printf (file
, "\n(gen %s)", i2name (rule
->gen
, decode_gen_map
));
354 lf_printf (file
, "\n(first %d)", rule
->first
);
355 lf_printf (file
, "\n(last %d)", rule
->last
);
356 lf_printf (file
, "\n(force_first %d)", rule
->force_first
);
357 lf_printf (file
, "\n(force_last %d)", rule
->force_last
);
358 dump_filter (file
, "\n(constant_field_names \"",
359 rule
->constant_field_names
, "\")");
360 lf_printf (file
, "\n(constant 0x%x)", rule
->constant
);
361 lf_printf (file
, "\n(word_nr %d)", rule
->word_nr
);
362 lf_printf (file
, "\n(with_zero_reserved %d)", rule
->with_zero_reserved
);
363 lf_printf (file
, "\n(with_duplicates %d)", rule
->with_duplicates
);
364 lf_printf (file
, "\n(with_combine %d)", rule
->with_combine
);
365 dump_filter (file
, "\n(format_names \"", rule
->format_names
, "\")");
366 dump_filter (file
, "\n(model_names \"", rule
->model_names
, "\")");
367 dump_decode_conds (file
, "\n(conditions ", rule
->conditions
, ")");
368 lf_printf (file
, "\n(next %p)", rule
->next
);
369 lf_indent (file
, -1);
371 lf_printf (file
, "%s", suffix
);
378 dump_decode_rules (lf
*file
,
380 const decode_table
*rule
,
383 lf_printf (file
, "%s", prefix
);
386 lf_indent (file
, +1);
387 dump_decode_rule (file
, "\n(", rule
, ")");
388 lf_indent (file
, -1);
391 lf_printf (file
, "%s", suffix
);
394 igen_options options
;
397 main (int argc
, char **argv
)
405 error (NULL
, "Usage: decode <decode-file> <hi-bit-nr>\n");
407 options
.hi_bit_nr
= a2i (argv
[2]);
408 rules
= load_decode_table (argv
[1]);
409 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
410 dump_decode_rules (l
, "(rules ", rules
, ")\n");