1 /* This file is part of the program psim.
3 Copyright (C) 1994,1995,1996, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /* load the opcode stat structure */
30 #include "ld-decode.h"
37 static const name_map decode_type_map[] = {
38 { "normal", normal_decode_rule },
39 { "boolean", boolean_rule },
40 { NULL, normal_decode_rule },
43 static const name_map decode_gen_map[] = {
44 { "array", array_gen },
45 { "switch", switch_gen },
46 { "padded-switch", padded_switch_gen },
47 { "goto-switch", goto_switch_gen },
51 static const name_map decode_reserved_map[] = {
52 { "zero-reserved", 1 },
56 static const name_map decode_duplicates_map[] = {
61 static const name_map decode_combine_map[] = {
66 static const name_map decode_search_map[] = {
67 { "constants", decode_find_constants },
68 { "mixed", decode_find_mixed },
69 { "strings", decode_find_strings },
70 { NULL, decode_find_mixed },
75 set_bits (int bit[max_insn_bit_size],
79 for (bit_nr = 0; bit_nr < max_insn_bit_size; bit_nr++)
81 if (bit_nr < options.insn_bit_size)
82 bit[bit_nr] = (value >> (options.insn_bit_size - bit_nr - 1)) & 1;
89 load_decode_table(char *file_name)
91 table *file = table_open (file_name);
93 decode_table *table = NULL;
94 decode_table **curr_rule = &table;
95 while ((entry = table_read (file)) != NULL)
97 char *decode_options = entry->field[decode_options_field];
98 decode_table *new_rule = ZALLOC (decode_table);
99 if (entry->nr_fields < min_nr_decode_fields)
100 error (entry->line, "Missing decode table fields\n");
101 new_rule->line = entry->line;
103 /* the options field */
104 new_rule->type = name2i (decode_options, decode_type_map);
105 if (options.decode.overriding_gen != NULL)
106 new_rule->gen = 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
110 && options.decode.switch_as_goto)
111 new_rule->gen = goto_switch_gen;
112 if (options.decode.zero_reserved)
113 new_rule->with_zero_reserved = 1;
115 new_rule->with_zero_reserved = name2i (decode_options, decode_reserved_map);
116 if (options.decode.duplicate)
117 new_rule->with_duplicates = 1;
119 new_rule->with_duplicates = name2i (decode_options, decode_duplicates_map);
120 if (options.decode.combine)
121 new_rule->with_combine = 1;
123 new_rule->with_combine = name2i (decode_options, decode_combine_map);
124 if (new_rule->type == boolean_rule)
126 char *chp = decode_options;
131 new_rule->constant = a2i (chp);
134 chp = skip_to_separator (chp, ",");
135 chp = skip_spaces (chp);
140 if (entry->nr_fields > decode_first_field
141 && strlen (entry->field[decode_first_field]) > 0)
143 new_rule->first = target_a2i (options.hi_bit_nr,
144 entry->field[decode_first_field]);
145 if (new_rule->first < 0 || new_rule->first >= options.insn_bit_size)
146 error (new_rule->line, "First field out of range\n");
150 if (entry->nr_fields > decode_last_field
151 && strlen (entry->field[decode_last_field]) > 0)
153 new_rule->last = target_a2i (options.hi_bit_nr,
154 entry->field[decode_last_field]);
155 if (new_rule->last < 0 || new_rule->last >= options.insn_bit_size)
156 error (new_rule->line, "Last field out of range\n");
159 new_rule->last = options.insn_bit_size - 1;
160 if (new_rule->first > new_rule->last)
161 error (new_rule->line, "First must preceed last\n");
163 /* force first/last, with default values based on first/last */
164 if (entry->nr_fields > decode_force_first_field
165 && strlen (entry->field[decode_force_first_field]) > 0)
167 new_rule->force_first = target_a2i (options.hi_bit_nr,
168 entry->field[decode_force_first_field]);
169 if (new_rule->force_first < new_rule->first
170 || new_rule->force_first > new_rule->last + 1)
171 error (new_rule->line, "Force first out of range\n");
174 new_rule->force_first = new_rule->last + 1;
175 if (entry->nr_fields > decode_force_last_field
176 && strlen (entry->field[decode_force_last_field]) > 0)
178 new_rule->force_last = target_a2i (options.hi_bit_nr,
179 entry->field[decode_force_last_field]);
180 if (new_rule->force_last > new_rule->last
181 || new_rule->force_last < new_rule->first - 1)
182 error (new_rule->line, "Force-last out of range\n");
185 new_rule->force_last = new_rule->first - 1;
187 /* fields to be treated as constant */
188 if (entry->nr_fields > decode_constant_field_names_field)
189 filter_parse (&new_rule->constant_field_names,
190 entry->field[decode_constant_field_names_field]);
192 /* applicable word nr */
193 if (entry->nr_fields > decode_word_nr_field)
194 new_rule->word_nr = a2i (entry->field[decode_word_nr_field]);
196 /* required instruction format names */
197 if (entry->nr_fields > decode_format_names_field)
198 filter_parse (&new_rule->format_names,
199 entry->field[decode_format_names_field]);
201 /* required processor models */
202 if (entry->nr_fields > decode_model_names_field)
203 filter_parse (&new_rule->model_names,
204 entry->field[decode_model_names_field]);
207 if (entry->nr_fields > decode_paths_field
208 && strlen (entry->field[decode_paths_field]) > 0)
210 decode_path_list **last = &new_rule->paths;
211 char *chp = entry->field[decode_paths_field];
214 (*last) = ZALLOC (decode_path_list);
215 /* extra root/zero entry */
216 (*last)->path = ZALLOC (decode_path);
219 decode_path *entry = ZALLOC (decode_path);
220 entry->opcode_nr = a2i (chp);
221 entry->parent = (*last)->path;
222 (*last)->path = entry;
223 chp = skip_digits (chp);
224 chp = skip_spaces (chp);
227 last = &(*last)->next;
231 error (entry->line, "Invalid path field\n");
234 /* collect up the list of optional special conditions applicable
237 int field_nr = nr_decode_fields;
238 while (entry->nr_fields > field_nr)
240 decode_cond *cond = ZALLOC (decode_cond);
242 if (entry->nr_fields > field_nr + decode_cond_mask_field)
243 set_bits (cond->mask, a2i (entry->field[field_nr + decode_cond_mask_field]));
244 if (entry->nr_fields > field_nr + decode_cond_value_field)
246 if (entry->field[field_nr + decode_cond_value_field][0] == '!')
249 set_bits (cond->value, a2i (entry->field[field_nr + decode_cond_value_field] + 1));
254 set_bits (cond->value, a2i (entry->field[field_nr + decode_cond_value_field]));
257 if (entry->nr_fields > field_nr + decode_cond_word_nr_field)
258 cond->word_nr = a2i (entry->field[field_nr + decode_cond_word_nr_field]);
259 field_nr += nr_decode_cond_fields;
261 last = &new_rule->conditions;
262 while (*last != NULL)
263 last = &(*last)->next;
267 *curr_rule = new_rule;
268 curr_rule = &new_rule->next;
275 decode_table_max_word_nr (decode_table *entry)
278 while (entry != NULL)
281 if (entry->word_nr > max_word_nr)
282 max_word_nr = entry->word_nr;
283 for (cond = entry->conditions; cond != NULL; cond = cond->next)
285 if (cond->word_nr > max_word_nr)
286 max_word_nr = cond->word_nr;
296 dump_decode_cond (lf *file,
301 lf_printf (file, "%s(decode_cond *) 0x%lx", prefix, (long) cond);
304 lf_indent (file, +1);
305 lf_printf (file, "\n(word_nr %d)", cond->word_nr);
306 lf_printf (file, "\n(mask 0x%lx)", (long) cond->mask);
307 lf_printf (file, "\n(value 0x%lx)", (long) cond->value);
308 lf_printf (file, "\n(is_equal 0x%lx)", (long) cond->is_equal);
309 lf_printf (file, "\n(next (decode_cond *) 0%lx)", (long) cond->next);
310 lf_indent (file, -1);
312 lf_printf (file, "%s", suffix);
317 dump_decode_conds (lf *file,
322 lf_printf (file, "%s(decode_cond *) 0x%lx", prefix, (long) cond);
325 dump_decode_cond (file, "\n(", cond, ")");
328 lf_printf (file, "%s", suffix);
333 dump_decode_rule (lf *file,
338 lf_printf (file, "%s(decode_table *) 0x%lx", prefix, (long) rule);
341 lf_indent (file, +1);
342 dump_line_ref (file, "\n(line ", rule->line, ")");
343 lf_printf (file, "\n(type %s)", i2name(rule->type, decode_type_map));
344 lf_printf (file, "\n(gen %s)", i2name(rule->gen, decode_gen_map));
345 lf_printf (file, "\n(first %d)", rule->first);
346 lf_printf (file, "\n(last %d)", rule->last);
347 lf_printf (file, "\n(force_first %d)", rule->force_first);
348 lf_printf (file, "\n(force_last %d)", rule->force_last);
349 dump_filter (file, "\n(constant_field_names \"", rule->constant_field_names, "\")");
350 lf_printf (file, "\n(constant 0x%x)", rule->constant);
351 lf_printf (file, "\n(word_nr %d)", rule->word_nr);
352 lf_printf (file, "\n(with_zero_reserved %d)", rule->with_zero_reserved);
353 lf_printf (file, "\n(with_duplicates %d)", rule->with_duplicates);
354 lf_printf (file, "\n(with_combine %d)", rule->with_combine);
355 dump_filter (file, "\n(format_names \"", rule->format_names, "\")");
356 dump_filter (file, "\n(model_names \"", rule->model_names, "\")");
357 dump_decode_conds (file, "\n(conditions ", rule->conditions, ")");
358 lf_printf (file, "\n(next 0x%lx)", (long) rule->next);
359 lf_indent (file, -1);
361 lf_printf (file, "%s", suffix);
368 dump_decode_rules (lf *file,
373 lf_printf (file, "%s", prefix);
376 lf_indent (file, +1);
377 dump_decode_rule (file, "\n(", rule, ")");
378 lf_indent (file, -1);
381 lf_printf (file, "%s", suffix);
384 igen_options options;
387 main(int argc, char **argv)
392 INIT_OPTIONS (options);
395 error (NULL, "Usage: decode <decode-file> <hi-bit-nr>\n");
397 options.hi_bit_nr = a2i (argv[2]);
398 rules = load_decode_table (argv[1]);
399 l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-ld-insn");
400 dump_decode_rules (l, "(rules ", rules, ")\n");