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