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