1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* load the opcode stat structure */
34 #include "ld-decode.h"
41 static const name_map decode_type_map
[] = {
42 {"normal", normal_decode_rule
},
43 {"boolean", boolean_rule
},
44 {NULL
, normal_decode_rule
},
47 static const name_map decode_gen_map
[] = {
49 {"switch", switch_gen
},
50 {"padded-switch", padded_switch_gen
},
51 {"goto-switch", goto_switch_gen
},
55 static const name_map decode_reserved_map
[] = {
60 static const name_map decode_duplicates_map
[] = {
65 static const name_map decode_combine_map
[] = {
70 static const name_map decode_search_map
[] = {
71 {"constants", decode_find_constants
},
72 {"mixed", decode_find_mixed
},
73 {"strings", decode_find_strings
},
74 {NULL
, decode_find_mixed
},
79 set_bits (int bit
[max_insn_bit_size
], unsigned64 value
)
82 for (bit_nr
= 0; bit_nr
< max_insn_bit_size
; bit_nr
++)
84 if (bit_nr
< options
.insn_bit_size
)
85 bit
[bit_nr
] = (value
>> (options
.insn_bit_size
- bit_nr
- 1)) & 1;
92 load_decode_table (char *file_name
)
94 table
*file
= table_open (file_name
);
96 decode_table
*table
= NULL
;
97 decode_table
**curr_rule
= &table
;
98 while ((entry
= table_read (file
)) != NULL
)
100 char *decode_options
= entry
->field
[decode_options_field
];
101 decode_table
*new_rule
= ZALLOC (decode_table
);
102 if (entry
->nr_fields
< min_nr_decode_fields
)
103 error (entry
->line
, "Missing decode table fields\n");
104 new_rule
->line
= entry
->line
;
106 /* the options field */
107 new_rule
->type
= name2i (decode_options
, decode_type_map
);
108 if (options
.decode
.overriding_gen
!= NULL
)
110 name2i (options
.decode
.overriding_gen
, decode_gen_map
);
112 new_rule
->gen
= name2i (decode_options
, decode_gen_map
);
113 if (new_rule
->gen
== padded_switch_gen
&& options
.decode
.switch_as_goto
)
114 new_rule
->gen
= goto_switch_gen
;
115 if (options
.decode
.zero_reserved
)
116 new_rule
->with_zero_reserved
= 1;
118 new_rule
->with_zero_reserved
=
119 name2i (decode_options
, decode_reserved_map
);
120 if (options
.decode
.duplicate
)
121 new_rule
->with_duplicates
= 1;
123 new_rule
->with_duplicates
=
124 name2i (decode_options
, decode_duplicates_map
);
125 if (options
.decode
.combine
)
126 new_rule
->with_combine
= 1;
128 new_rule
->with_combine
= name2i (decode_options
, decode_combine_map
);
129 if (new_rule
->type
== boolean_rule
)
131 char *chp
= decode_options
;
136 new_rule
->constant
= a2i (chp
);
139 chp
= skip_to_separator (chp
, ",");
140 chp
= skip_spaces (chp
);
145 if (entry
->nr_fields
> decode_first_field
146 && strlen (entry
->field
[decode_first_field
]) > 0)
148 new_rule
->first
= target_a2i (options
.hi_bit_nr
,
149 entry
->field
[decode_first_field
]);
150 if (new_rule
->first
< 0 || new_rule
->first
>= options
.insn_bit_size
)
151 error (new_rule
->line
, "First field out of range\n");
155 if (entry
->nr_fields
> decode_last_field
156 && strlen (entry
->field
[decode_last_field
]) > 0)
158 new_rule
->last
= target_a2i (options
.hi_bit_nr
,
159 entry
->field
[decode_last_field
]);
160 if (new_rule
->last
< 0 || new_rule
->last
>= options
.insn_bit_size
)
161 error (new_rule
->line
, "Last field out of range\n");
164 new_rule
->last
= options
.insn_bit_size
- 1;
165 if (new_rule
->first
> new_rule
->last
)
166 error (new_rule
->line
, "First must preceed last\n");
168 /* force first/last, with default values based on first/last */
169 if (entry
->nr_fields
> decode_force_first_field
170 && strlen (entry
->field
[decode_force_first_field
]) > 0)
172 new_rule
->force_first
= target_a2i (options
.hi_bit_nr
,
175 [decode_force_first_field
]);
176 if (new_rule
->force_first
< new_rule
->first
177 || new_rule
->force_first
> new_rule
->last
+ 1)
178 error (new_rule
->line
, "Force first out of range\n");
181 new_rule
->force_first
= new_rule
->last
+ 1;
182 if (entry
->nr_fields
> decode_force_last_field
183 && strlen (entry
->field
[decode_force_last_field
]) > 0)
185 new_rule
->force_last
= target_a2i (options
.hi_bit_nr
,
187 field
[decode_force_last_field
]);
188 if (new_rule
->force_last
> new_rule
->last
189 || new_rule
->force_last
< new_rule
->first
- 1)
190 error (new_rule
->line
, "Force-last out of range\n");
193 new_rule
->force_last
= new_rule
->first
- 1;
195 /* fields to be treated as constant */
196 if (entry
->nr_fields
> decode_constant_field_names_field
)
197 filter_parse (&new_rule
->constant_field_names
,
198 entry
->field
[decode_constant_field_names_field
]);
200 /* applicable word nr */
201 if (entry
->nr_fields
> decode_word_nr_field
)
202 new_rule
->word_nr
= a2i (entry
->field
[decode_word_nr_field
]);
204 /* required instruction format names */
205 if (entry
->nr_fields
> decode_format_names_field
)
206 filter_parse (&new_rule
->format_names
,
207 entry
->field
[decode_format_names_field
]);
209 /* required processor models */
210 if (entry
->nr_fields
> decode_model_names_field
)
211 filter_parse (&new_rule
->model_names
,
212 entry
->field
[decode_model_names_field
]);
215 if (entry
->nr_fields
> decode_paths_field
216 && strlen (entry
->field
[decode_paths_field
]) > 0)
218 decode_path_list
**last
= &new_rule
->paths
;
219 char *chp
= entry
->field
[decode_paths_field
];
222 (*last
) = ZALLOC (decode_path_list
);
223 /* extra root/zero entry */
224 (*last
)->path
= ZALLOC (decode_path
);
227 decode_path
*entry
= ZALLOC (decode_path
);
228 entry
->opcode_nr
= a2i (chp
);
229 entry
->parent
= (*last
)->path
;
230 (*last
)->path
= entry
;
231 chp
= skip_digits (chp
);
232 chp
= skip_spaces (chp
);
235 last
= &(*last
)->next
;
239 error (entry
->line
, "Invalid path field\n");
242 /* collect up the list of optional special conditions applicable
245 int field_nr
= nr_decode_fields
;
246 while (entry
->nr_fields
> field_nr
)
248 decode_cond
*cond
= ZALLOC (decode_cond
);
250 if (entry
->nr_fields
> field_nr
+ decode_cond_mask_field
)
251 set_bits (cond
->mask
,
253 field
[field_nr
+ decode_cond_mask_field
]));
254 if (entry
->nr_fields
> field_nr
+ decode_cond_value_field
)
256 if (entry
->field
[field_nr
+ decode_cond_value_field
][0] ==
260 set_bits (cond
->value
,
262 field
[field_nr
+ decode_cond_value_field
] +
268 set_bits (cond
->value
,
271 decode_cond_value_field
]));
274 if (entry
->nr_fields
> field_nr
+ decode_cond_word_nr_field
)
276 a2i (entry
->field
[field_nr
+ decode_cond_word_nr_field
]);
277 field_nr
+= nr_decode_cond_fields
;
279 last
= &new_rule
->conditions
;
280 while (*last
!= NULL
)
281 last
= &(*last
)->next
;
285 *curr_rule
= new_rule
;
286 curr_rule
= &new_rule
->next
;
293 decode_table_max_word_nr (decode_table
*entry
)
296 while (entry
!= NULL
)
299 if (entry
->word_nr
> max_word_nr
)
300 max_word_nr
= entry
->word_nr
;
301 for (cond
= entry
->conditions
; cond
!= NULL
; cond
= cond
->next
)
303 if (cond
->word_nr
> max_word_nr
)
304 max_word_nr
= cond
->word_nr
;
314 dump_decode_cond (lf
*file
, char *prefix
, decode_cond
*cond
, char *suffix
)
316 lf_printf (file
, "%s(decode_cond *) 0x%lx", prefix
, (long) cond
);
319 lf_indent (file
, +1);
320 lf_printf (file
, "\n(word_nr %d)", cond
->word_nr
);
321 lf_printf (file
, "\n(mask 0x%lx)", (long) cond
->mask
);
322 lf_printf (file
, "\n(value 0x%lx)", (long) cond
->value
);
323 lf_printf (file
, "\n(is_equal 0x%lx)", (long) cond
->is_equal
);
324 lf_printf (file
, "\n(next (decode_cond *) 0%lx)", (long) cond
->next
);
325 lf_indent (file
, -1);
327 lf_printf (file
, "%s", suffix
);
332 dump_decode_conds (lf
*file
, char *prefix
, decode_cond
*cond
, char *suffix
)
334 lf_printf (file
, "%s(decode_cond *) 0x%lx", prefix
, (long) cond
);
337 dump_decode_cond (file
, "\n(", cond
, ")");
340 lf_printf (file
, "%s", suffix
);
345 dump_decode_rule (lf
*file
, char *prefix
, decode_table
*rule
, char *suffix
)
347 lf_printf (file
, "%s(decode_table *) 0x%lx", prefix
, (long) 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 0x%lx)", (long) rule
->next
);
369 lf_indent (file
, -1);
371 lf_printf (file
, "%s", suffix
);
378 dump_decode_rules (lf
*file
, char *prefix
, decode_table
*rule
, char *suffix
)
380 lf_printf (file
, "%s", prefix
);
383 lf_indent (file
, +1);
384 dump_decode_rule (file
, "\n(", rule
, ")");
385 lf_indent (file
, -1);
388 lf_printf (file
, "%s", suffix
);
391 igen_options options
;
394 main (int argc
, char **argv
)
399 INIT_OPTIONS (options
);
402 error (NULL
, "Usage: decode <decode-file> <hi-bit-nr>\n");
404 options
.hi_bit_nr
= a2i (argv
[2]);
405 rules
= load_decode_table (argv
[1]);
406 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
407 dump_decode_rules (l
, "(rules ", rules
, ")\n");