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
[] = {
48 { "array", array_gen
},
49 { "switch", switch_gen
},
50 { "padded-switch", padded_switch_gen
},
51 { "goto-switch", goto_switch_gen
},
55 static const name_map decode_reserved_map
[] = {
56 { "zero-reserved", 1 },
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
],
83 for (bit_nr
= 0; bit_nr
< max_insn_bit_size
; bit_nr
++)
85 if (bit_nr
< options
.insn_bit_size
)
86 bit
[bit_nr
] = (value
>> (options
.insn_bit_size
- bit_nr
- 1)) & 1;
93 load_decode_table(char *file_name
)
95 table
*file
= table_open (file_name
);
97 decode_table
*table
= NULL
;
98 decode_table
**curr_rule
= &table
;
99 while ((entry
= table_read (file
)) != NULL
)
101 char *decode_options
= entry
->field
[decode_options_field
];
102 decode_table
*new_rule
= ZALLOC (decode_table
);
103 if (entry
->nr_fields
< min_nr_decode_fields
)
104 error (entry
->line
, "Missing decode table fields\n");
105 new_rule
->line
= entry
->line
;
107 /* the options field */
108 new_rule
->type
= name2i (decode_options
, decode_type_map
);
109 if (options
.decode
.overriding_gen
!= NULL
)
110 new_rule
->gen
= 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
114 && options
.decode
.switch_as_goto
)
115 new_rule
->gen
= goto_switch_gen
;
116 if (options
.decode
.zero_reserved
)
117 new_rule
->with_zero_reserved
= 1;
119 new_rule
->with_zero_reserved
= name2i (decode_options
, decode_reserved_map
);
120 if (options
.decode
.duplicate
)
121 new_rule
->with_duplicates
= 1;
123 new_rule
->with_duplicates
= name2i (decode_options
, decode_duplicates_map
);
124 if (options
.decode
.combine
)
125 new_rule
->with_combine
= 1;
127 new_rule
->with_combine
= name2i (decode_options
, decode_combine_map
);
128 if (new_rule
->type
== boolean_rule
)
130 char *chp
= decode_options
;
135 new_rule
->constant
= a2i (chp
);
138 chp
= skip_to_separator (chp
, ",");
139 chp
= skip_spaces (chp
);
144 if (entry
->nr_fields
> decode_first_field
145 && strlen (entry
->field
[decode_first_field
]) > 0)
147 new_rule
->first
= target_a2i (options
.hi_bit_nr
,
148 entry
->field
[decode_first_field
]);
149 if (new_rule
->first
< 0 || new_rule
->first
>= options
.insn_bit_size
)
150 error (new_rule
->line
, "First field out of range\n");
154 if (entry
->nr_fields
> decode_last_field
155 && strlen (entry
->field
[decode_last_field
]) > 0)
157 new_rule
->last
= target_a2i (options
.hi_bit_nr
,
158 entry
->field
[decode_last_field
]);
159 if (new_rule
->last
< 0 || new_rule
->last
>= options
.insn_bit_size
)
160 error (new_rule
->line
, "Last field out of range\n");
163 new_rule
->last
= options
.insn_bit_size
- 1;
164 if (new_rule
->first
> new_rule
->last
)
165 error (new_rule
->line
, "First must preceed last\n");
167 /* force first/last, with default values based on first/last */
168 if (entry
->nr_fields
> decode_force_first_field
169 && strlen (entry
->field
[decode_force_first_field
]) > 0)
171 new_rule
->force_first
= target_a2i (options
.hi_bit_nr
,
172 entry
->field
[decode_force_first_field
]);
173 if (new_rule
->force_first
< new_rule
->first
174 || new_rule
->force_first
> new_rule
->last
+ 1)
175 error (new_rule
->line
, "Force first out of range\n");
178 new_rule
->force_first
= new_rule
->last
+ 1;
179 if (entry
->nr_fields
> decode_force_last_field
180 && strlen (entry
->field
[decode_force_last_field
]) > 0)
182 new_rule
->force_last
= target_a2i (options
.hi_bit_nr
,
183 entry
->field
[decode_force_last_field
]);
184 if (new_rule
->force_last
> new_rule
->last
185 || new_rule
->force_last
< new_rule
->first
- 1)
186 error (new_rule
->line
, "Force-last out of range\n");
189 new_rule
->force_last
= new_rule
->first
- 1;
191 /* fields to be treated as constant */
192 if (entry
->nr_fields
> decode_constant_field_names_field
)
193 filter_parse (&new_rule
->constant_field_names
,
194 entry
->field
[decode_constant_field_names_field
]);
196 /* applicable word nr */
197 if (entry
->nr_fields
> decode_word_nr_field
)
198 new_rule
->word_nr
= a2i (entry
->field
[decode_word_nr_field
]);
200 /* required instruction format names */
201 if (entry
->nr_fields
> decode_format_names_field
)
202 filter_parse (&new_rule
->format_names
,
203 entry
->field
[decode_format_names_field
]);
205 /* required processor models */
206 if (entry
->nr_fields
> decode_model_names_field
)
207 filter_parse (&new_rule
->model_names
,
208 entry
->field
[decode_model_names_field
]);
211 if (entry
->nr_fields
> decode_paths_field
212 && strlen (entry
->field
[decode_paths_field
]) > 0)
214 decode_path_list
**last
= &new_rule
->paths
;
215 char *chp
= entry
->field
[decode_paths_field
];
218 (*last
) = ZALLOC (decode_path_list
);
219 /* extra root/zero entry */
220 (*last
)->path
= ZALLOC (decode_path
);
223 decode_path
*entry
= ZALLOC (decode_path
);
224 entry
->opcode_nr
= a2i (chp
);
225 entry
->parent
= (*last
)->path
;
226 (*last
)->path
= entry
;
227 chp
= skip_digits (chp
);
228 chp
= skip_spaces (chp
);
231 last
= &(*last
)->next
;
235 error (entry
->line
, "Invalid path field\n");
238 /* collect up the list of optional special conditions applicable
241 int field_nr
= nr_decode_fields
;
242 while (entry
->nr_fields
> field_nr
)
244 decode_cond
*cond
= ZALLOC (decode_cond
);
246 if (entry
->nr_fields
> field_nr
+ decode_cond_mask_field
)
247 set_bits (cond
->mask
, a2i (entry
->field
[field_nr
+ decode_cond_mask_field
]));
248 if (entry
->nr_fields
> field_nr
+ decode_cond_value_field
)
250 if (entry
->field
[field_nr
+ decode_cond_value_field
][0] == '!')
253 set_bits (cond
->value
, a2i (entry
->field
[field_nr
+ decode_cond_value_field
] + 1));
258 set_bits (cond
->value
, a2i (entry
->field
[field_nr
+ decode_cond_value_field
]));
261 if (entry
->nr_fields
> field_nr
+ decode_cond_word_nr_field
)
262 cond
->word_nr
= a2i (entry
->field
[field_nr
+ decode_cond_word_nr_field
]);
263 field_nr
+= nr_decode_cond_fields
;
265 last
= &new_rule
->conditions
;
266 while (*last
!= NULL
)
267 last
= &(*last
)->next
;
271 *curr_rule
= new_rule
;
272 curr_rule
= &new_rule
->next
;
279 decode_table_max_word_nr (decode_table
*entry
)
282 while (entry
!= NULL
)
285 if (entry
->word_nr
> max_word_nr
)
286 max_word_nr
= entry
->word_nr
;
287 for (cond
= entry
->conditions
; cond
!= NULL
; cond
= cond
->next
)
289 if (cond
->word_nr
> max_word_nr
)
290 max_word_nr
= cond
->word_nr
;
300 dump_decode_cond (lf
*file
,
305 lf_printf (file
, "%s(decode_cond *) 0x%lx", prefix
, (long) cond
);
308 lf_indent (file
, +1);
309 lf_printf (file
, "\n(word_nr %d)", cond
->word_nr
);
310 lf_printf (file
, "\n(mask 0x%lx)", (long) cond
->mask
);
311 lf_printf (file
, "\n(value 0x%lx)", (long) cond
->value
);
312 lf_printf (file
, "\n(is_equal 0x%lx)", (long) cond
->is_equal
);
313 lf_printf (file
, "\n(next (decode_cond *) 0%lx)", (long) cond
->next
);
314 lf_indent (file
, -1);
316 lf_printf (file
, "%s", suffix
);
321 dump_decode_conds (lf
*file
,
326 lf_printf (file
, "%s(decode_cond *) 0x%lx", prefix
, (long) cond
);
329 dump_decode_cond (file
, "\n(", cond
, ")");
332 lf_printf (file
, "%s", suffix
);
337 dump_decode_rule (lf
*file
,
342 lf_printf (file
, "%s(decode_table *) 0x%lx", prefix
, (long) rule
);
345 lf_indent (file
, +1);
346 dump_line_ref (file
, "\n(line ", rule
->line
, ")");
347 lf_printf (file
, "\n(type %s)", i2name(rule
->type
, decode_type_map
));
348 lf_printf (file
, "\n(gen %s)", i2name(rule
->gen
, decode_gen_map
));
349 lf_printf (file
, "\n(first %d)", rule
->first
);
350 lf_printf (file
, "\n(last %d)", rule
->last
);
351 lf_printf (file
, "\n(force_first %d)", rule
->force_first
);
352 lf_printf (file
, "\n(force_last %d)", rule
->force_last
);
353 dump_filter (file
, "\n(constant_field_names \"", 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);
365 lf_printf (file
, "%s", suffix
);
372 dump_decode_rules (lf
*file
,
377 lf_printf (file
, "%s", prefix
);
380 lf_indent (file
, +1);
381 dump_decode_rule (file
, "\n(", rule
, ")");
382 lf_indent (file
, -1);
385 lf_printf (file
, "%s", suffix
);
388 igen_options options
;
391 main(int argc
, char **argv
)
396 INIT_OPTIONS (options
);
399 error (NULL
, "Usage: decode <decode-file> <hi-bit-nr>\n");
401 options
.hi_bit_nr
= a2i (argv
[2]);
402 rules
= load_decode_table (argv
[1]);
403 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
404 dump_decode_rules (l
, "(rules ", rules
, ")\n");