ld-insn.c revision 1.1.1.1 1 1.1 christos /* This file is part of the program psim.
2 1.1 christos
3 1.1 christos Copyright 1994, 1995, 1996, 2003 Andrew Cagney
4 1.1 christos
5 1.1 christos This program is free software; you can redistribute it and/or modify
6 1.1 christos it under the terms of the GNU General Public License as published by
7 1.1 christos the Free Software Foundation; either version 3 of the License, or
8 1.1 christos (at your option) any later version.
9 1.1 christos
10 1.1 christos This program is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with this program; if not, see <http://www.gnu.org/licenses/>.
17 1.1 christos
18 1.1 christos */
19 1.1 christos
20 1.1 christos
21 1.1 christos #include "misc.h"
22 1.1 christos #include "lf.h"
23 1.1 christos #include "table.h"
24 1.1 christos #include "filter.h"
25 1.1 christos #include "ld-decode.h"
26 1.1 christos #include "ld-cache.h"
27 1.1 christos #include "ld-insn.h"
28 1.1 christos
29 1.1 christos #include "igen.h"
30 1.1 christos
31 1.1 christos static void
32 1.1 christos update_depth(insn_table *entry,
33 1.1 christos lf *file,
34 1.1 christos void *data,
35 1.1 christos insn *instruction,
36 1.1 christos int depth)
37 1.1 christos {
38 1.1 christos int *max_depth = (int*)data;
39 1.1 christos if (*max_depth < depth)
40 1.1 christos *max_depth = depth;
41 1.1 christos }
42 1.1 christos
43 1.1 christos
44 1.1 christos int
45 1.1 christos insn_table_depth(insn_table *table)
46 1.1 christos {
47 1.1 christos int depth = 0;
48 1.1 christos insn_table_traverse_tree(table,
49 1.1 christos NULL,
50 1.1 christos &depth,
51 1.1 christos 1,
52 1.1 christos NULL, /*start*/
53 1.1 christos update_depth,
54 1.1 christos NULL, /*end*/
55 1.1 christos NULL); /*padding*/
56 1.1 christos return depth;
57 1.1 christos }
58 1.1 christos
59 1.1 christos
60 1.1 christos static insn_fields *
61 1.1 christos parse_insn_format(table_entry *entry,
62 1.1 christos char *format)
63 1.1 christos {
64 1.1 christos char *chp;
65 1.1 christos insn_fields *fields = ZALLOC(insn_fields);
66 1.1 christos
67 1.1 christos /* create a leading sentinal */
68 1.1 christos fields->first = ZALLOC(insn_field);
69 1.1 christos fields->first->first = -1;
70 1.1 christos fields->first->last = -1;
71 1.1 christos fields->first->width = 0;
72 1.1 christos
73 1.1 christos /* and a trailing sentinal */
74 1.1 christos fields->last = ZALLOC(insn_field);
75 1.1 christos fields->last->first = insn_bit_size;
76 1.1 christos fields->last->last = insn_bit_size;
77 1.1 christos fields->last->width = 0;
78 1.1 christos
79 1.1 christos /* link them together */
80 1.1 christos fields->first->next = fields->last;
81 1.1 christos fields->last->prev = fields->first;
82 1.1 christos
83 1.1 christos /* now work through the formats */
84 1.1 christos chp = format;
85 1.1 christos
86 1.1 christos while (*chp != '\0') {
87 1.1 christos char *start_pos;
88 1.1 christos char *start_val;
89 1.1 christos int strlen_val;
90 1.1 christos int strlen_pos;
91 1.1 christos insn_field *new_field;
92 1.1 christos
93 1.1 christos /* sanity check */
94 1.1 christos if (!isdigit(*chp)) {
95 1.1 christos error("%s:%d: missing position field at `%s'\n",
96 1.1 christos entry->file_name, entry->line_nr, chp);
97 1.1 christos }
98 1.1 christos
99 1.1 christos /* break out the bit position */
100 1.1 christos start_pos = chp;
101 1.1 christos while (isdigit(*chp))
102 1.1 christos chp++;
103 1.1 christos strlen_pos = chp - start_pos;
104 1.1 christos if (*chp == '.' && strlen_pos > 0)
105 1.1 christos chp++;
106 1.1 christos else {
107 1.1 christos error("%s:%d: missing field value at %s\n",
108 1.1 christos entry->file_name, entry->line_nr, chp);
109 1.1 christos break;
110 1.1 christos }
111 1.1 christos
112 1.1 christos /* break out the value */
113 1.1 christos start_val = chp;
114 1.1 christos while ((*start_val == '/' && *chp == '/')
115 1.1 christos || (isdigit(*start_val) && isdigit(*chp))
116 1.1 christos || (isalpha(*start_val) && (isalnum(*chp) || *chp == '_')))
117 1.1 christos chp++;
118 1.1 christos strlen_val = chp - start_val;
119 1.1 christos if (*chp == ',')
120 1.1 christos chp++;
121 1.1 christos else if (*chp != '\0' || strlen_val == 0) {
122 1.1 christos error("%s:%d: missing field terminator at %s\n",
123 1.1 christos entry->file_name, entry->line_nr, chp);
124 1.1 christos break;
125 1.1 christos }
126 1.1 christos
127 1.1 christos /* create a new field and insert it */
128 1.1 christos new_field = ZALLOC(insn_field);
129 1.1 christos new_field->next = fields->last;
130 1.1 christos new_field->prev = fields->last->prev;
131 1.1 christos new_field->next->prev = new_field;
132 1.1 christos new_field->prev->next = new_field;
133 1.1 christos
134 1.1 christos /* the value */
135 1.1 christos new_field->val_string = (char*)zalloc(strlen_val+1);
136 1.1 christos strncpy(new_field->val_string, start_val, strlen_val);
137 1.1 christos if (isdigit(*new_field->val_string)) {
138 1.1 christos new_field->val_int = a2i(new_field->val_string);
139 1.1 christos new_field->is_int = 1;
140 1.1 christos }
141 1.1 christos else if (new_field->val_string[0] == '/') {
142 1.1 christos new_field->is_slash = 1;
143 1.1 christos }
144 1.1 christos else {
145 1.1 christos new_field->is_string = 1;
146 1.1 christos }
147 1.1 christos
148 1.1 christos /* the pos */
149 1.1 christos new_field->pos_string = (char*)zalloc(strlen_pos+1);
150 1.1 christos strncpy(new_field->pos_string, start_pos, strlen_pos);
151 1.1 christos new_field->first = target_a2i(hi_bit_nr, new_field->pos_string);
152 1.1 christos new_field->last = new_field->next->first - 1; /* guess */
153 1.1 christos new_field->width = new_field->last - new_field->first + 1; /* guess */
154 1.1 christos new_field->prev->last = new_field->first-1; /*fix*/
155 1.1 christos new_field->prev->width = new_field->first - new_field->prev->first; /*fix*/
156 1.1 christos }
157 1.1 christos
158 1.1 christos /* fiddle first/last so that the sentinals `disapear' */
159 1.1 christos ASSERT(fields->first->last < 0);
160 1.1 christos ASSERT(fields->last->first >= insn_bit_size);
161 1.1 christos fields->first = fields->first->next;
162 1.1 christos fields->last = fields->last->prev;
163 1.1 christos
164 1.1 christos /* now go over this again, pointing each bit position at a field
165 1.1 christos record */
166 1.1 christos {
167 1.1 christos int i;
168 1.1 christos insn_field *field;
169 1.1 christos field = fields->first;
170 1.1 christos for (i = 0; i < insn_bit_size; i++) {
171 1.1 christos while (field->last < i)
172 1.1 christos field = field->next;
173 1.1 christos fields->bits[i] = field;
174 1.1 christos }
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* go over each of the fields, and compute a `value' for the insn */
178 1.1 christos {
179 1.1 christos insn_field *field;
180 1.1 christos fields->value = 0;
181 1.1 christos for (field = fields->first;
182 1.1 christos field->last < insn_bit_size;
183 1.1 christos field = field->next) {
184 1.1 christos fields->value <<= field->width;
185 1.1 christos if (field->is_int)
186 1.1 christos fields->value |= field->val_int;
187 1.1 christos }
188 1.1 christos }
189 1.1 christos return fields;
190 1.1 christos }
191 1.1 christos
192 1.1 christos
193 1.1 christos void
194 1.1 christos parse_include_entry (table *file,
195 1.1 christos table_entry *file_entry,
196 1.1 christos filter *filters,
197 1.1 christos table_include *includes)
198 1.1 christos {
199 1.1 christos /* parse the include file_entry */
200 1.1 christos if (file_entry->nr_fields < 4)
201 1.1 christos error ("Incorrect nr fields for include record\n");
202 1.1 christos /* process it */
203 1.1 christos if (!is_filtered_out(file_entry->fields[include_flags], filters))
204 1.1 christos {
205 1.1 christos table_push (file, includes,
206 1.1 christos file_entry->fields[include_path],
207 1.1 christos file_entry->nr_fields, file_entry->nr_fields);
208 1.1 christos }
209 1.1 christos }
210 1.1 christos
211 1.1 christos static void
212 1.1 christos model_table_insert(insn_table *table,
213 1.1 christos table_entry *file_entry)
214 1.1 christos {
215 1.1 christos int len;
216 1.1 christos
217 1.1 christos /* create a new model */
218 1.1 christos model *new_model = ZALLOC(model);
219 1.1 christos
220 1.1 christos new_model->name = file_entry->fields[model_identifer];
221 1.1 christos new_model->printable_name = file_entry->fields[model_name];
222 1.1 christos new_model->insn_default = file_entry->fields[model_default];
223 1.1 christos
224 1.1 christos while (*new_model->insn_default && isspace(*new_model->insn_default))
225 1.1 christos new_model->insn_default++;
226 1.1 christos
227 1.1 christos len = strlen(new_model->insn_default);
228 1.1 christos if (max_model_fields_len < len)
229 1.1 christos max_model_fields_len = len;
230 1.1 christos
231 1.1 christos /* append it to the end of the model list */
232 1.1 christos if (last_model)
233 1.1 christos last_model->next = new_model;
234 1.1 christos else
235 1.1 christos models = new_model;
236 1.1 christos last_model = new_model;
237 1.1 christos }
238 1.1 christos
239 1.1 christos static void
240 1.1 christos model_table_insert_specific(insn_table *table,
241 1.1 christos table_entry *file_entry,
242 1.1 christos insn **start_ptr,
243 1.1 christos insn **end_ptr)
244 1.1 christos {
245 1.1 christos insn *ptr = ZALLOC(insn);
246 1.1 christos ptr->file_entry = file_entry;
247 1.1 christos if (*end_ptr)
248 1.1 christos (*end_ptr)->next = ptr;
249 1.1 christos else
250 1.1 christos (*start_ptr) = ptr;
251 1.1 christos (*end_ptr) = ptr;
252 1.1 christos }
253 1.1 christos
254 1.1 christos
255 1.1 christos static void
256 1.1 christos insn_table_insert_function(insn_table *table,
257 1.1 christos table_entry *file_entry)
258 1.1 christos {
259 1.1 christos /* create a new function */
260 1.1 christos insn *new_function = ZALLOC(insn);
261 1.1 christos new_function->file_entry = file_entry;
262 1.1 christos
263 1.1 christos /* append it to the end of the function list */
264 1.1 christos if (table->last_function)
265 1.1 christos table->last_function->next = new_function;
266 1.1 christos else
267 1.1 christos table->functions = new_function;
268 1.1 christos table->last_function = new_function;
269 1.1 christos }
270 1.1 christos
271 1.1 christos extern void
272 1.1 christos insn_table_insert_insn(insn_table *table,
273 1.1 christos table_entry *file_entry,
274 1.1 christos insn_fields *fields)
275 1.1 christos {
276 1.1 christos insn **ptr_to_cur_insn = &table->insns;
277 1.1 christos insn *cur_insn = *ptr_to_cur_insn;
278 1.1 christos table_model_entry *insn_model_ptr;
279 1.1 christos model *model_ptr;
280 1.1 christos
281 1.1 christos /* create a new instruction */
282 1.1 christos insn *new_insn = ZALLOC(insn);
283 1.1 christos new_insn->file_entry = file_entry;
284 1.1 christos new_insn->fields = fields;
285 1.1 christos
286 1.1 christos /* Check out any model information returned to make sure the model
287 1.1 christos is correct. */
288 1.1 christos for(insn_model_ptr = file_entry->model_first; insn_model_ptr; insn_model_ptr = insn_model_ptr->next) {
289 1.1 christos char *name = insn_model_ptr->fields[insn_model_name];
290 1.1 christos int len = strlen (insn_model_ptr->fields[insn_model_fields]);
291 1.1 christos
292 1.1 christos while (len > 0 && isspace(*insn_model_ptr->fields[insn_model_fields])) {
293 1.1 christos len--;
294 1.1 christos insn_model_ptr->fields[insn_model_fields]++;
295 1.1 christos }
296 1.1 christos
297 1.1 christos if (max_model_fields_len < len)
298 1.1 christos max_model_fields_len = len;
299 1.1 christos
300 1.1 christos for(model_ptr = models; model_ptr; model_ptr = model_ptr->next) {
301 1.1 christos if (strcmp(name, model_ptr->printable_name) == 0) {
302 1.1 christos
303 1.1 christos /* Replace the name field with that of the global model, so that when we
304 1.1 christos want to print it out, we can just compare pointers. */
305 1.1 christos insn_model_ptr->fields[insn_model_name] = model_ptr->printable_name;
306 1.1 christos break;
307 1.1 christos }
308 1.1 christos }
309 1.1 christos
310 1.1 christos if (!model_ptr)
311 1.1 christos error("%s:%d: machine model `%s' was not known about\n",
312 1.1 christos file_entry->file_name, file_entry->line_nr, name);
313 1.1 christos }
314 1.1 christos
315 1.1 christos /* insert it according to the order of the fields */
316 1.1 christos while (cur_insn != NULL
317 1.1 christos && new_insn->fields->value >= cur_insn->fields->value) {
318 1.1 christos ptr_to_cur_insn = &cur_insn->next;
319 1.1 christos cur_insn = *ptr_to_cur_insn;
320 1.1 christos }
321 1.1 christos
322 1.1 christos new_insn->next = cur_insn;
323 1.1 christos *ptr_to_cur_insn = new_insn;
324 1.1 christos
325 1.1 christos table->nr_insn++;
326 1.1 christos }
327 1.1 christos
328 1.1 christos
329 1.1 christos
330 1.1 christos insn_table *
331 1.1 christos load_insn_table(const char *file_name,
332 1.1 christos decode_table *decode_rules,
333 1.1 christos filter *filters,
334 1.1 christos table_include *includes,
335 1.1 christos cache_table **cache_rules)
336 1.1 christos {
337 1.1 christos table *file = table_open(file_name, nr_insn_table_fields, nr_insn_model_table_fields);
338 1.1 christos insn_table *table = ZALLOC(insn_table);
339 1.1 christos table_entry *file_entry;
340 1.1 christos table->opcode_rule = decode_rules;
341 1.1 christos
342 1.1 christos while ((file_entry = table_entry_read(file)) != NULL) {
343 1.1 christos if (it_is("function", file_entry->fields[insn_flags])
344 1.1 christos || it_is("internal", file_entry->fields[insn_flags])) {
345 1.1 christos insn_table_insert_function(table, file_entry);
346 1.1 christos }
347 1.1 christos else if ((it_is("function", file_entry->fields[insn_form])
348 1.1 christos || it_is("internal", file_entry->fields[insn_form]))
349 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
350 1.1 christos /* Ok, this is evil. Need to convert a new style function into
351 1.1 christos an old style function. Construct an old style table and then
352 1.1 christos copy it back. */
353 1.1 christos char *fields[nr_insn_table_fields];
354 1.1 christos memset (fields, 0, sizeof fields);
355 1.1 christos fields[insn_flags] = file_entry->fields[insn_form];
356 1.1 christos fields[function_type] = file_entry->fields[insn_name];
357 1.1 christos fields[function_name] = file_entry->fields[insn_comment];
358 1.1 christos fields[function_param] = file_entry->fields[insn_field_6];
359 1.1 christos memcpy (file_entry->fields, fields,
360 1.1 christos sizeof (fields[0]) * file_entry->nr_fields);
361 1.1 christos insn_table_insert_function(table, file_entry);
362 1.1 christos #if 0
363 1.1 christos ":" "..."
364 1.1 christos ":" <filter-flags>
365 1.1 christos ":" <filter-models>
366 1.1 christos ":" <typedef>
367 1.1 christos ":" <name>
368 1.1 christos [ ":" <parameter-list> ]
369 1.1 christos <nl>
370 1.1 christos [ <function-model> ]
371 1.1 christos <code-block>
372 1.1 christos #endif
373 1.1 christos }
374 1.1 christos else if (it_is("model", file_entry->fields[insn_flags])) {
375 1.1 christos model_table_insert(table, file_entry);
376 1.1 christos }
377 1.1 christos else if (it_is("model-macro", file_entry->fields[insn_flags])) {
378 1.1 christos model_table_insert_specific(table, file_entry, &model_macros, &last_model_macro);
379 1.1 christos }
380 1.1 christos else if (it_is("model-function", file_entry->fields[insn_flags])) {
381 1.1 christos model_table_insert_specific(table, file_entry, &model_functions, &last_model_function);
382 1.1 christos }
383 1.1 christos else if (it_is("model-internal", file_entry->fields[insn_flags])) {
384 1.1 christos model_table_insert_specific(table, file_entry, &model_internal, &last_model_internal);
385 1.1 christos }
386 1.1 christos else if (it_is("model-static", file_entry->fields[insn_flags])) {
387 1.1 christos model_table_insert_specific(table, file_entry, &model_static, &last_model_static);
388 1.1 christos }
389 1.1 christos else if (it_is("model-data", file_entry->fields[insn_flags])) {
390 1.1 christos model_table_insert_specific(table, file_entry, &model_data, &last_model_data);
391 1.1 christos }
392 1.1 christos else if (it_is("include", file_entry->fields[insn_form])
393 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
394 1.1 christos parse_include_entry (file, file_entry, filters, includes);
395 1.1 christos }
396 1.1 christos else if ((it_is("cache", file_entry->fields[insn_form])
397 1.1 christos || it_is("compute", file_entry->fields[insn_form])
398 1.1 christos || it_is("scratch", file_entry->fields[insn_form]))
399 1.1 christos && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
400 1.1 christos append_cache_rule (cache_rules,
401 1.1 christos file_entry->fields[insn_form], /* type */
402 1.1 christos file_entry->fields[cache_name],
403 1.1 christos file_entry->fields[cache_derived_name],
404 1.1 christos file_entry->fields[cache_type_def],
405 1.1 christos file_entry->fields[cache_expression],
406 1.1 christos file_entry);
407 1.1 christos }
408 1.1 christos else {
409 1.1 christos insn_fields *fields;
410 1.1 christos /* skip instructions that aren't relevant to the mode */
411 1.1 christos if (is_filtered_out(file_entry->fields[insn_flags], filters)) {
412 1.1 christos fprintf(stderr, "Dropping %s - %s\n",
413 1.1 christos file_entry->fields[insn_name],
414 1.1 christos file_entry->fields[insn_flags]);
415 1.1 christos }
416 1.1 christos else {
417 1.1 christos /* create/insert the new instruction */
418 1.1 christos fields = parse_insn_format(file_entry,
419 1.1 christos file_entry->fields[insn_format]);
420 1.1 christos insn_table_insert_insn(table, file_entry, fields);
421 1.1 christos }
422 1.1 christos }
423 1.1 christos }
424 1.1 christos return table;
425 1.1 christos }
426 1.1 christos
427 1.1 christos
428 1.1 christos extern void
429 1.1 christos insn_table_traverse_tree(insn_table *table,
430 1.1 christos lf *file,
431 1.1 christos void *data,
432 1.1 christos int depth,
433 1.1 christos leaf_handler *start,
434 1.1 christos insn_handler *leaf,
435 1.1 christos leaf_handler *end,
436 1.1 christos padding_handler *padding)
437 1.1 christos {
438 1.1 christos insn_table *entry;
439 1.1 christos int entry_nr;
440 1.1 christos
441 1.1 christos ASSERT(table != NULL
442 1.1 christos && table->opcode != NULL
443 1.1 christos && table->nr_entries > 0
444 1.1 christos && table->entries != 0);
445 1.1 christos
446 1.1 christos if (start != NULL && depth >= 0)
447 1.1 christos start(table, file, data, depth);
448 1.1 christos
449 1.1 christos for (entry_nr = 0, entry = table->entries;
450 1.1 christos entry_nr < (table->opcode->is_boolean
451 1.1 christos ? 2
452 1.1 christos : (1 << (table->opcode->last - table->opcode->first + 1)));
453 1.1 christos entry_nr ++) {
454 1.1 christos if (entry == NULL
455 1.1 christos || (!table->opcode->is_boolean
456 1.1 christos && entry_nr < entry->opcode_nr)) {
457 1.1 christos if (padding != NULL && depth >= 0)
458 1.1 christos padding(table, file, data, depth, entry_nr);
459 1.1 christos }
460 1.1 christos else {
461 1.1 christos ASSERT(entry != NULL && (entry->opcode_nr == entry_nr
462 1.1 christos || table->opcode->is_boolean));
463 1.1 christos if (entry->opcode != NULL && depth != 0) {
464 1.1 christos insn_table_traverse_tree(entry, file, data, depth+1,
465 1.1 christos start, leaf, end, padding);
466 1.1 christos }
467 1.1 christos else if (depth >= 0) {
468 1.1 christos if (leaf != NULL)
469 1.1 christos leaf(entry, file, data, entry->insns, depth);
470 1.1 christos }
471 1.1 christos entry = entry->sibling;
472 1.1 christos }
473 1.1 christos }
474 1.1 christos if (end != NULL && depth >= 0)
475 1.1 christos end(table, file, data, depth);
476 1.1 christos }
477 1.1 christos
478 1.1 christos
479 1.1 christos extern void
480 1.1 christos insn_table_traverse_function(insn_table *table,
481 1.1 christos lf *file,
482 1.1 christos void *data,
483 1.1 christos function_handler *leaf)
484 1.1 christos {
485 1.1 christos insn *function;
486 1.1 christos for (function = table->functions;
487 1.1 christos function != NULL;
488 1.1 christos function = function->next) {
489 1.1 christos leaf(table, file, data, function->file_entry);
490 1.1 christos }
491 1.1 christos }
492 1.1 christos
493 1.1 christos extern void
494 1.1 christos insn_table_traverse_insn(insn_table *table,
495 1.1 christos lf *file,
496 1.1 christos void *data,
497 1.1 christos insn_handler *handler)
498 1.1 christos {
499 1.1 christos insn *instruction;
500 1.1 christos for (instruction = table->insns;
501 1.1 christos instruction != NULL;
502 1.1 christos instruction = instruction->next) {
503 1.1 christos handler(table, file, data, instruction, 0);
504 1.1 christos }
505 1.1 christos }
506 1.1 christos
507 1.1 christos
508 1.1 christos /****************************************************************/
509 1.1 christos
510 1.1 christos typedef enum {
511 1.1 christos field_constant_int = 1,
512 1.1 christos field_constant_slash = 2,
513 1.1 christos field_constant_string = 3
514 1.1 christos } constant_field_types;
515 1.1 christos
516 1.1 christos
517 1.1 christos static int
518 1.1 christos insn_field_is_constant(insn_field *field,
519 1.1 christos decode_table *rule)
520 1.1 christos {
521 1.1 christos /* field is an integer */
522 1.1 christos if (field->is_int)
523 1.1 christos return field_constant_int;
524 1.1 christos /* field is `/' and treating that as a constant */
525 1.1 christos if (field->is_slash && rule->force_slash)
526 1.1 christos return field_constant_slash;
527 1.1 christos /* field, though variable is on the list */
528 1.1 christos if (field->is_string && rule->force_expansion != NULL) {
529 1.1 christos char *forced_fields = rule->force_expansion;
530 1.1 christos while (*forced_fields != '\0') {
531 1.1 christos int field_len;
532 1.1 christos char *end = strchr(forced_fields, ',');
533 1.1 christos if (end == NULL)
534 1.1 christos field_len = strlen(forced_fields);
535 1.1 christos else
536 1.1 christos field_len = end-forced_fields;
537 1.1 christos if (strncmp(forced_fields, field->val_string, field_len) == 0
538 1.1 christos && field->val_string[field_len] == '\0')
539 1.1 christos return field_constant_string;
540 1.1 christos forced_fields += field_len;
541 1.1 christos if (*forced_fields == ',')
542 1.1 christos forced_fields++;
543 1.1 christos }
544 1.1 christos }
545 1.1 christos return 0;
546 1.1 christos }
547 1.1 christos
548 1.1 christos
549 1.1 christos static opcode_field *
550 1.1 christos insn_table_find_opcode_field(insn *insns,
551 1.1 christos decode_table *rule,
552 1.1 christos int string_only)
553 1.1 christos {
554 1.1 christos opcode_field *curr_opcode = ZALLOC(opcode_field);
555 1.1 christos insn *entry;
556 1.1 christos ASSERT(rule);
557 1.1 christos
558 1.1 christos curr_opcode->first = insn_bit_size;
559 1.1 christos curr_opcode->last = -1;
560 1.1 christos for (entry = insns; entry != NULL; entry = entry->next) {
561 1.1 christos insn_fields *fields = entry->fields;
562 1.1 christos opcode_field new_opcode;
563 1.1 christos
564 1.1 christos /* find a start point for the opcode field */
565 1.1 christos new_opcode.first = rule->first;
566 1.1 christos while (new_opcode.first <= rule->last
567 1.1 christos && (!string_only
568 1.1 christos || insn_field_is_constant(fields->bits[new_opcode.first],
569 1.1 christos rule) != field_constant_string)
570 1.1 christos && (string_only
571 1.1 christos || !insn_field_is_constant(fields->bits[new_opcode.first],
572 1.1 christos rule)))
573 1.1 christos new_opcode.first = fields->bits[new_opcode.first]->last + 1;
574 1.1 christos ASSERT(new_opcode.first > rule->last
575 1.1 christos || (string_only
576 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.first],
577 1.1 christos rule) == field_constant_string)
578 1.1 christos || (!string_only
579 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.first],
580 1.1 christos rule)));
581 1.1 christos
582 1.1 christos /* find the end point for the opcode field */
583 1.1 christos new_opcode.last = rule->last;
584 1.1 christos while (new_opcode.last >= rule->first
585 1.1 christos && (!string_only
586 1.1 christos || insn_field_is_constant(fields->bits[new_opcode.last],
587 1.1 christos rule) != field_constant_string)
588 1.1 christos && (string_only
589 1.1 christos || !insn_field_is_constant(fields->bits[new_opcode.last],
590 1.1 christos rule)))
591 1.1 christos new_opcode.last = fields->bits[new_opcode.last]->first - 1;
592 1.1 christos ASSERT(new_opcode.last < rule->first
593 1.1 christos || (string_only
594 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.last],
595 1.1 christos rule) == field_constant_string)
596 1.1 christos || (!string_only
597 1.1 christos && insn_field_is_constant(fields->bits[new_opcode.last],
598 1.1 christos rule)));
599 1.1 christos
600 1.1 christos /* now see if our current opcode needs expanding */
601 1.1 christos if (new_opcode.first <= rule->last
602 1.1 christos && curr_opcode->first > new_opcode.first)
603 1.1 christos curr_opcode->first = new_opcode.first;
604 1.1 christos if (new_opcode.last >= rule->first
605 1.1 christos && curr_opcode->last < new_opcode.last)
606 1.1 christos curr_opcode->last = new_opcode.last;
607 1.1 christos
608 1.1 christos }
609 1.1 christos
610 1.1 christos /* was any thing interesting found? */
611 1.1 christos if (curr_opcode->first > rule->last) {
612 1.1 christos ASSERT(curr_opcode->last < rule->first);
613 1.1 christos return NULL;
614 1.1 christos }
615 1.1 christos ASSERT(curr_opcode->last >= rule->first);
616 1.1 christos ASSERT(curr_opcode->first <= rule->last);
617 1.1 christos
618 1.1 christos /* if something was found, check it includes the forced field range */
619 1.1 christos if (!string_only
620 1.1 christos && curr_opcode->first > rule->force_first) {
621 1.1 christos curr_opcode->first = rule->force_first;
622 1.1 christos }
623 1.1 christos if (!string_only
624 1.1 christos && curr_opcode->last < rule->force_last) {
625 1.1 christos curr_opcode->last = rule->force_last;
626 1.1 christos }
627 1.1 christos /* handle special case elminating any need to do shift after mask */
628 1.1 christos if (string_only
629 1.1 christos && rule->force_last == insn_bit_size-1) {
630 1.1 christos curr_opcode->last = insn_bit_size-1;
631 1.1 christos }
632 1.1 christos
633 1.1 christos /* handle any special cases */
634 1.1 christos switch (rule->type) {
635 1.1 christos case normal_decode_rule:
636 1.1 christos /* let the above apply */
637 1.1 christos break;
638 1.1 christos case expand_forced_rule:
639 1.1 christos /* expand a limited nr of bits, ignoring the rest */
640 1.1 christos curr_opcode->first = rule->force_first;
641 1.1 christos curr_opcode->last = rule->force_last;
642 1.1 christos break;
643 1.1 christos case boolean_rule:
644 1.1 christos curr_opcode->is_boolean = 1;
645 1.1 christos curr_opcode->boolean_constant = rule->special_constant;
646 1.1 christos break;
647 1.1 christos default:
648 1.1 christos error("Something is going wrong\n");
649 1.1 christos }
650 1.1 christos
651 1.1 christos return curr_opcode;
652 1.1 christos }
653 1.1 christos
654 1.1 christos
655 1.1 christos static void
656 1.1 christos insn_table_insert_expanded(insn_table *table,
657 1.1 christos insn *old_insn,
658 1.1 christos int new_opcode_nr,
659 1.1 christos insn_bits *new_bits)
660 1.1 christos {
661 1.1 christos insn_table **ptr_to_cur_entry = &table->entries;
662 1.1 christos insn_table *cur_entry = *ptr_to_cur_entry;
663 1.1 christos
664 1.1 christos /* find the new table for this entry */
665 1.1 christos while (cur_entry != NULL
666 1.1 christos && cur_entry->opcode_nr < new_opcode_nr) {
667 1.1 christos ptr_to_cur_entry = &cur_entry->sibling;
668 1.1 christos cur_entry = *ptr_to_cur_entry;
669 1.1 christos }
670 1.1 christos
671 1.1 christos if (cur_entry == NULL || cur_entry->opcode_nr != new_opcode_nr) {
672 1.1 christos insn_table *new_entry = ZALLOC(insn_table);
673 1.1 christos new_entry->opcode_nr = new_opcode_nr;
674 1.1 christos new_entry->expanded_bits = new_bits;
675 1.1 christos new_entry->opcode_rule = table->opcode_rule->next;
676 1.1 christos new_entry->sibling = cur_entry;
677 1.1 christos new_entry->parent = table;
678 1.1 christos *ptr_to_cur_entry = new_entry;
679 1.1 christos cur_entry = new_entry;
680 1.1 christos table->nr_entries++;
681 1.1 christos }
682 1.1 christos /* ASSERT new_bits == cur_entry bits */
683 1.1 christos ASSERT(cur_entry != NULL && cur_entry->opcode_nr == new_opcode_nr);
684 1.1 christos insn_table_insert_insn(cur_entry,
685 1.1 christos old_insn->file_entry,
686 1.1 christos old_insn->fields);
687 1.1 christos }
688 1.1 christos
689 1.1 christos static void
690 1.1 christos insn_table_expand_opcode(insn_table *table,
691 1.1 christos insn *instruction,
692 1.1 christos int field_nr,
693 1.1 christos int opcode_nr,
694 1.1 christos insn_bits *bits)
695 1.1 christos {
696 1.1 christos
697 1.1 christos if (field_nr > table->opcode->last) {
698 1.1 christos insn_table_insert_expanded(table, instruction, opcode_nr, bits);
699 1.1 christos }
700 1.1 christos else {
701 1.1 christos insn_field *field = instruction->fields->bits[field_nr];
702 1.1 christos if (field->is_int || field->is_slash) {
703 1.1 christos ASSERT(field->first >= table->opcode->first
704 1.1 christos && field->last <= table->opcode->last);
705 1.1 christos insn_table_expand_opcode(table, instruction, field->last+1,
706 1.1 christos ((opcode_nr << field->width) + field->val_int),
707 1.1 christos bits);
708 1.1 christos }
709 1.1 christos else {
710 1.1 christos int val;
711 1.1 christos int last_pos = ((field->last < table->opcode->last)
712 1.1 christos ? field->last : table->opcode->last);
713 1.1 christos int first_pos = ((field->first > table->opcode->first)
714 1.1 christos ? field->first : table->opcode->first);
715 1.1 christos int width = last_pos - first_pos + 1;
716 1.1 christos int last_val = (table->opcode->is_boolean
717 1.1 christos ? 2 : (1 << width));
718 1.1 christos for (val = 0; val < last_val; val++) {
719 1.1 christos insn_bits *new_bits = ZALLOC(insn_bits);
720 1.1 christos new_bits->field = field;
721 1.1 christos new_bits->value = val;
722 1.1 christos new_bits->last = bits;
723 1.1 christos new_bits->opcode = table->opcode;
724 1.1 christos insn_table_expand_opcode(table, instruction, last_pos+1,
725 1.1 christos ((opcode_nr << width) | val),
726 1.1 christos new_bits);
727 1.1 christos }
728 1.1 christos }
729 1.1 christos }
730 1.1 christos }
731 1.1 christos
732 1.1 christos static void
733 1.1 christos insn_table_insert_expanding(insn_table *table,
734 1.1 christos insn *entry)
735 1.1 christos {
736 1.1 christos insn_table_expand_opcode(table,
737 1.1 christos entry,
738 1.1 christos table->opcode->first,
739 1.1 christos 0,
740 1.1 christos table->expanded_bits);
741 1.1 christos }
742 1.1 christos
743 1.1 christos
744 1.1 christos extern void
745 1.1 christos insn_table_expand_insns(insn_table *table)
746 1.1 christos {
747 1.1 christos
748 1.1 christos ASSERT(table->nr_insn >= 1);
749 1.1 christos
750 1.1 christos /* determine a valid opcode */
751 1.1 christos while (table->opcode_rule) {
752 1.1 christos /* specials only for single instructions */
753 1.1 christos if ((table->nr_insn > 1
754 1.1 christos && table->opcode_rule->special_mask == 0
755 1.1 christos && table->opcode_rule->type == normal_decode_rule)
756 1.1 christos || (table->nr_insn == 1
757 1.1 christos && table->opcode_rule->special_mask != 0
758 1.1 christos && ((table->insns->fields->value
759 1.1 christos & table->opcode_rule->special_mask)
760 1.1 christos == table->opcode_rule->special_value))
761 1.1 christos || (generate_expanded_instructions
762 1.1 christos && table->opcode_rule->special_mask == 0
763 1.1 christos && table->opcode_rule->type == normal_decode_rule))
764 1.1 christos table->opcode =
765 1.1 christos insn_table_find_opcode_field(table->insns,
766 1.1 christos table->opcode_rule,
767 1.1 christos table->nr_insn == 1/*string*/
768 1.1 christos );
769 1.1 christos if (table->opcode != NULL)
770 1.1 christos break;
771 1.1 christos table->opcode_rule = table->opcode_rule->next;
772 1.1 christos }
773 1.1 christos
774 1.1 christos /* did we find anything */
775 1.1 christos if (table->opcode == NULL) {
776 1.1 christos return;
777 1.1 christos }
778 1.1 christos ASSERT(table->opcode != NULL);
779 1.1 christos
780 1.1 christos /* back link what we found to its parent */
781 1.1 christos if (table->parent != NULL) {
782 1.1 christos ASSERT(table->parent->opcode != NULL);
783 1.1 christos table->opcode->parent = table->parent->opcode;
784 1.1 christos }
785 1.1 christos
786 1.1 christos /* expand the raw instructions according to the opcode */
787 1.1 christos {
788 1.1 christos insn *entry;
789 1.1 christos for (entry = table->insns; entry != NULL; entry = entry->next) {
790 1.1 christos insn_table_insert_expanding(table, entry);
791 1.1 christos }
792 1.1 christos }
793 1.1 christos
794 1.1 christos /* and do the same for the sub entries */
795 1.1 christos {
796 1.1 christos insn_table *entry;
797 1.1 christos for (entry = table->entries; entry != NULL; entry = entry->sibling) {
798 1.1 christos insn_table_expand_insns(entry);
799 1.1 christos }
800 1.1 christos }
801 1.1 christos }
802 1.1 christos
803 1.1 christos
804 1.1 christos
805 1.1 christos
806 1.1 christos #ifdef MAIN
807 1.1 christos
808 1.1 christos static void
809 1.1 christos dump_insn_field(insn_field *field,
810 1.1 christos int indent)
811 1.1 christos {
812 1.1 christos
813 1.1 christos printf("(insn_field*)0x%x\n", (unsigned)field);
814 1.1 christos
815 1.1 christos dumpf(indent, "(first %d)\n", field->first);
816 1.1 christos
817 1.1 christos dumpf(indent, "(last %d)\n", field->last);
818 1.1 christos
819 1.1 christos dumpf(indent, "(width %d)\n", field->width);
820 1.1 christos
821 1.1 christos if (field->is_int)
822 1.1 christos dumpf(indent, "(is_int %d)\n", field->val_int);
823 1.1 christos
824 1.1 christos if (field->is_slash)
825 1.1 christos dumpf(indent, "(is_slash)\n");
826 1.1 christos
827 1.1 christos if (field->is_string)
828 1.1 christos dumpf(indent, "(is_string `%s')\n", field->val_string);
829 1.1 christos
830 1.1 christos dumpf(indent, "(next 0x%x)\n", field->next);
831 1.1 christos
832 1.1 christos dumpf(indent, "(prev 0x%x)\n", field->prev);
833 1.1 christos
834 1.1 christos
835 1.1 christos }
836 1.1 christos
837 1.1 christos static void
838 1.1 christos dump_insn_fields(insn_fields *fields,
839 1.1 christos int indent)
840 1.1 christos {
841 1.1 christos int i;
842 1.1 christos
843 1.1 christos printf("(insn_fields*)%p\n", fields);
844 1.1 christos
845 1.1 christos dumpf(indent, "(first 0x%x)\n", fields->first);
846 1.1 christos dumpf(indent, "(last 0x%x)\n", fields->last);
847 1.1 christos
848 1.1 christos dumpf(indent, "(value 0x%x)\n", fields->value);
849 1.1 christos
850 1.1 christos for (i = 0; i < insn_bit_size; i++) {
851 1.1 christos dumpf(indent, "(bits[%d] ", i, fields->bits[i]);
852 1.1 christos dump_insn_field(fields->bits[i], indent+1);
853 1.1 christos dumpf(indent, " )\n");
854 1.1 christos }
855 1.1 christos
856 1.1 christos }
857 1.1 christos
858 1.1 christos
859 1.1 christos static void
860 1.1 christos dump_opcode_field(opcode_field *field, int indent, int levels)
861 1.1 christos {
862 1.1 christos printf("(opcode_field*)%p\n", field);
863 1.1 christos if (levels && field != NULL) {
864 1.1 christos dumpf(indent, "(first %d)\n", field->first);
865 1.1 christos dumpf(indent, "(last %d)\n", field->last);
866 1.1 christos dumpf(indent, "(is_boolean %d)\n", field->is_boolean);
867 1.1 christos dumpf(indent, "(parent ");
868 1.1 christos dump_opcode_field(field->parent, indent, levels-1);
869 1.1 christos }
870 1.1 christos }
871 1.1 christos
872 1.1 christos
873 1.1 christos static void
874 1.1 christos dump_insn_bits(insn_bits *bits, int indent, int levels)
875 1.1 christos {
876 1.1 christos printf("(insn_bits*)%p\n", bits);
877 1.1 christos
878 1.1 christos if (levels && bits != NULL) {
879 1.1 christos dumpf(indent, "(value %d)\n", bits->value);
880 1.1 christos dumpf(indent, "(opcode ");
881 1.1 christos dump_opcode_field(bits->opcode, indent+1, 0);
882 1.1 christos dumpf(indent, " )\n");
883 1.1 christos dumpf(indent, "(field ");
884 1.1 christos dump_insn_field(bits->field, indent+1);
885 1.1 christos dumpf(indent, " )\n");
886 1.1 christos dumpf(indent, "(last ");
887 1.1 christos dump_insn_bits(bits->last, indent+1, levels-1);
888 1.1 christos }
889 1.1 christos }
890 1.1 christos
891 1.1 christos
892 1.1 christos
893 1.1 christos static void
894 1.1 christos dump_insn(insn *entry, int indent, int levels)
895 1.1 christos {
896 1.1 christos printf("(insn*)%p\n", entry);
897 1.1 christos
898 1.1 christos if (levels && entry != NULL) {
899 1.1 christos
900 1.1 christos dumpf(indent, "(file_entry ");
901 1.1 christos dump_table_entry(entry->file_entry, indent+1);
902 1.1 christos dumpf(indent, " )\n");
903 1.1 christos
904 1.1 christos dumpf(indent, "(fields ");
905 1.1 christos dump_insn_fields(entry->fields, indent+1);
906 1.1 christos dumpf(indent, " )\n");
907 1.1 christos
908 1.1 christos dumpf(indent, "(next ");
909 1.1 christos dump_insn(entry->next, indent+1, levels-1);
910 1.1 christos dumpf(indent, " )\n");
911 1.1 christos
912 1.1 christos }
913 1.1 christos
914 1.1 christos }
915 1.1 christos
916 1.1 christos
917 1.1 christos static void
918 1.1 christos dump_insn_table(insn_table *table,
919 1.1 christos int indent, int levels)
920 1.1 christos {
921 1.1 christos
922 1.1 christos printf("(insn_table*)%p\n", table);
923 1.1 christos
924 1.1 christos if (levels && table != NULL) {
925 1.1 christos
926 1.1 christos dumpf(indent, "(opcode_nr %d)\n", table->opcode_nr);
927 1.1 christos
928 1.1 christos dumpf(indent, "(expanded_bits ");
929 1.1 christos dump_insn_bits(table->expanded_bits, indent+1, -1);
930 1.1 christos dumpf(indent, " )\n");
931 1.1 christos
932 1.1 christos dumpf(indent, "(int nr_insn %d)\n", table->nr_insn);
933 1.1 christos
934 1.1 christos dumpf(indent, "(insns ");
935 1.1 christos dump_insn(table->insns, indent+1, table->nr_insn);
936 1.1 christos dumpf(indent, " )\n");
937 1.1 christos
938 1.1 christos dumpf(indent, "(opcode_rule ");
939 1.1 christos dump_decode_rule(table->opcode_rule, indent+1);
940 1.1 christos dumpf(indent, " )\n");
941 1.1 christos
942 1.1 christos dumpf(indent, "(opcode ");
943 1.1 christos dump_opcode_field(table->opcode, indent+1, 1);
944 1.1 christos dumpf(indent, " )\n");
945 1.1 christos
946 1.1 christos dumpf(indent, "(nr_entries %d)\n", table->entries);
947 1.1 christos dumpf(indent, "(entries ");
948 1.1 christos dump_insn_table(table->entries, indent+1, table->nr_entries);
949 1.1 christos dumpf(indent, " )\n");
950 1.1 christos
951 1.1 christos dumpf(indent, "(sibling ", table->sibling);
952 1.1 christos dump_insn_table(table->sibling, indent+1, levels-1);
953 1.1 christos dumpf(indent, " )\n");
954 1.1 christos
955 1.1 christos dumpf(indent, "(parent ", table->parent);
956 1.1 christos dump_insn_table(table->parent, indent+1, 0);
957 1.1 christos dumpf(indent, " )\n");
958 1.1 christos
959 1.1 christos }
960 1.1 christos }
961 1.1 christos
962 1.1 christos int insn_bit_size = max_insn_bit_size;
963 1.1 christos int hi_bit_nr;
964 1.1 christos int generate_expanded_instructions;
965 1.1 christos
966 1.1 christos int
967 1.1 christos main(int argc, char **argv)
968 1.1 christos {
969 1.1 christos filter *filters = NULL;
970 1.1 christos decode_table *decode_rules = NULL;
971 1.1 christos insn_table *instructions = NULL;
972 1.1 christos cache_table *cache_rules = NULL;
973 1.1 christos
974 1.1 christos if (argc != 5)
975 1.1 christos error("Usage: insn <filter> <hi-bit-nr> <decode-table> <insn-table>\n");
976 1.1 christos
977 1.1 christos filters = new_filter(argv[1], filters);
978 1.1 christos hi_bit_nr = a2i(argv[2]);
979 1.1 christos ASSERT(hi_bit_nr < insn_bit_size);
980 1.1 christos decode_rules = load_decode_table(argv[3], hi_bit_nr);
981 1.1 christos instructions = load_insn_table(argv[4], decode_rules, filters, NULL,
982 1.1 christos &cache_rules);
983 1.1 christos insn_table_expand_insns(instructions);
984 1.1 christos
985 1.1 christos dump_insn_table(instructions, 0, -1);
986 1.1 christos return 0;
987 1.1 christos }
988 1.1 christos
989 1.1 christos #endif
990