1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2018 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "ld-decode.h"
34 sub_val (insn_uint val
, int val_last_pos
, int first_pos
, int last_pos
)
36 return ((val
>> (val_last_pos
- last_pos
))
37 & (((insn_uint
) 1 << (last_pos
- first_pos
+ 1)) - 1));
41 update_depth (lf
*file
, gen_entry
*entry
, int depth
, void *data
)
43 int *max_depth
= (int *) data
;
44 if (*max_depth
< depth
)
50 gen_entry_depth (gen_entry
*table
)
53 gen_entry_traverse_tree (NULL
, table
, 1, NULL
, /*start */
54 update_depth
, NULL
, /*end */
61 print_gen_entry_path (line_ref
*line
, gen_entry
*table
, error_func
*print
)
63 if (table
->parent
== NULL
)
65 if (table
->top
->model
!= NULL
)
66 print (line
, "%s", table
->top
->model
->name
);
72 print_gen_entry_path (line
, table
->parent
, print
);
73 print (NULL
, ".%d", table
->opcode_nr
);
78 print_gen_entry_insns (gen_entry
*table
,
80 char *first_message
, char *next_message
)
84 message
= first_message
;
85 for (i
= table
->insns
; i
!= NULL
; i
= i
->next
)
87 insn_entry
*insn
= i
->insn
;
88 print_gen_entry_path (insn
->line
, table
, print
);
89 print (NULL
, ": %s.%s %s\n", insn
->format_name
, insn
->name
, message
);
90 if (next_message
!= NULL
)
91 message
= next_message
;
97 insn_field_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
102 if (l
== NULL
&& r
== NULL
)
103 return 0; /* all previous fields the same */
105 return -1; /* left shorter than right */
107 return +1; /* left longer than right */
108 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
110 if (l
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
112 if (r
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
114 if (l
->bit
[bit_nr
]->field
->conditions
== NULL
)
116 if (r
->bit
[bit_nr
]->field
->conditions
== NULL
)
119 printf ("%s%s%s VS %s%s%s\n",
120 l
->bit
[bit_nr
]->field
->val_string
,
121 l
->bit
[bit_nr
]->field
->conditions
->test
==
122 insn_field_cond_eq
? "=" : "!",
123 l
->bit
[bit_nr
]->field
->conditions
->string
,
124 r
->bit
[bit_nr
]->field
->val_string
,
125 r
->bit
[bit_nr
]->field
->conditions
->test
==
126 insn_field_cond_eq
? "=" : "!",
127 r
->bit
[bit_nr
]->field
->conditions
->string
);
128 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
129 && r
->bit
[bit_nr
]->field
->conditions
->test
==
132 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
133 insn_field_cond_field
134 && r
->bit
[bit_nr
]->field
->conditions
->type
==
135 insn_field_cond_field
)
136 /* somewhat arbitrary */
138 int cmp
= strcmp (l
->bit
[bit_nr
]->field
->conditions
->string
,
139 r
->bit
[bit_nr
]->field
->conditions
->
146 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
147 insn_field_cond_field
)
149 if (r
->bit
[bit_nr
]->field
->conditions
->type
==
150 insn_field_cond_field
)
152 /* The case of both fields having constant values should have
153 already have been handled because such fields are converted
154 into normal constant fields, but we must not make this
155 an assert, as we wouldn't gracefully handle an (invalid)
156 duplicate insn description. */
159 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
160 return +1; /* left = only */
161 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
162 return -1; /* right = only */
163 /* FIXME: Need to some what arbitrarily order conditional lists */
173 insn_word_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
178 if (l
== NULL
&& r
== NULL
)
179 return 0; /* all previous fields the same */
181 return -1; /* left shorter than right */
183 return +1; /* left longer than right */
184 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
186 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
188 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
190 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
192 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
202 opcode_bit_cmp (opcode_bits
*l
, opcode_bits
*r
)
204 if (l
== NULL
&& r
== NULL
)
205 return 0; /* all previous bits the same */
207 return -1; /* left shorter than right */
209 return +1; /* left longer than right */
210 /* most significant word */
211 if (l
->field
->word_nr
< r
->field
->word_nr
)
212 return +1; /* left has more significant word */
213 if (l
->field
->word_nr
> r
->field
->word_nr
)
214 return -1; /* right has more significant word */
215 /* most significant bit? */
216 if (l
->first
< r
->first
)
217 return +1; /* left as more significant bit */
218 if (l
->first
> r
->first
)
219 return -1; /* right as more significant bit */
221 if (l
->last
< r
->last
)
222 return +1; /* left as less bits */
223 if (l
->last
> r
->last
)
224 return -1; /* right as less bits */
226 if (l
->value
< r
->value
)
228 if (l
->value
> r
->value
)
236 opcode_bits_cmp (opcode_bits
*l
, opcode_bits
*r
)
241 if (l
== NULL
&& r
== NULL
)
242 return 0; /* all previous bits the same */
243 cmp
= opcode_bit_cmp (l
, r
);
253 new_opcode_bits (opcode_bits
*old_bits
,
256 int last
, insn_field_entry
*field
, opcode_field
*opcode
)
258 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
259 new_bits
->field
= field
;
260 new_bits
->value
= value
;
261 new_bits
->first
= first
;
262 new_bits
->last
= last
;
263 new_bits
->opcode
= opcode
;
265 if (old_bits
!= NULL
)
267 opcode_bits
*new_list
;
268 opcode_bits
**last
= &new_list
;
269 new_list
= new_opcode_bits (old_bits
->next
,
273 old_bits
->field
, old_bits
->opcode
);
274 while (*last
!= NULL
)
276 int cmp
= opcode_bit_cmp (new_bits
, *last
);
277 if (cmp
< 0) /* new < new_list */
283 ERROR ("Duplicated insn bits in list");
285 last
= &(*last
)->next
;
287 new_bits
->next
= *last
;
297 /* Same as strcmp(). */
299 name_cmp (const char *l
, const char *r
)
301 if (l
== NULL
&& r
== NULL
)
303 if (l
!= NULL
&& r
== NULL
)
305 if (l
== NULL
&& r
!= NULL
)
307 return strcmp (l
, r
);
313 merge_duplicate_insns
,
314 report_duplicate_insns
,
316 duplicate_insn_actions
;
319 insn_list_insert (insn_list
**cur_insn_ptr
,
322 opcode_bits
*expanded_bits
,
323 opcode_field
*opcodes
,
324 int nr_prefetched_words
,
325 duplicate_insn_actions duplicate_action
)
327 /* insert it according to the order of the fields & bits */
328 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
332 /* key#1 sort according to the constant fields of each instruction */
333 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
339 /* key#2 sort according to the expanded bits of each instruction */
340 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
346 /* key#3 sort according to the non-constant fields of each instruction */
347 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
353 if (duplicate_action
== merge_duplicate_insns
)
355 /* key#4: If we're going to merge duplicates, also sort
356 according to the format_name. Two instructions with
357 identical decode patterns, but different names, are
358 considered different when merging. Duplicates are only
359 important when creating a decode table (implied by
360 report_duplicate_insns) as such a table only has the
361 instruction's bit code as a way of differentiating
362 between instructions. */
363 int cmp
= name_cmp (insn
->format_name
,
364 (*cur_insn_ptr
)->insn
->format_name
);
371 if (duplicate_action
== merge_duplicate_insns
)
373 /* key#5: If we're going to merge duplicates, also sort
374 according to the name. See comment above for
376 int cmp
= name_cmp (insn
->name
, (*cur_insn_ptr
)->insn
->name
);
383 /* duplicate keys, report problem */
384 switch (duplicate_action
)
386 case report_duplicate_insns
:
387 /* It would appear that we have two instructions with the
388 same constant field values across all words and bits.
389 This error can also occure when insn_field_cmp() is
390 failing to differentiate between two instructions that
391 differ only in their conditional fields. */
393 "Two instructions with identical constant fields\n");
394 error ((*cur_insn_ptr
)->insn
->line
,
395 "Location of duplicate instruction\n");
396 case merge_duplicate_insns
:
397 /* Add the opcode path to the instructions list */
398 if (options
.trace
.insn_insertion
)
400 notify ((*cur_insn_ptr
)->insn
->line
,
401 "%s.%s: insert merge %s.%s\n",
402 (*cur_insn_ptr
)->insn
->format_name
,
403 (*cur_insn_ptr
)->insn
->name
,
409 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
410 while (*last
!= NULL
)
412 last
= &(*last
)->next
;
414 (*last
) = ZALLOC (insn_opcodes
);
415 (*last
)->opcode
= opcodes
;
417 /* Use the larger nr_prefetched_words */
418 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
419 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
420 return (*cur_insn_ptr
);
425 /* create a new list entry and insert it */
427 insn_list
*new_insn
= ZALLOC (insn_list
);
428 if (options
.trace
.insn_insertion
)
431 "%s.%s: insert new\n",
435 new_insn
->insn
= insn
;
436 new_insn
->expanded_bits
= expanded_bits
;
437 new_insn
->next
= (*cur_insn_ptr
);
438 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
441 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
442 new_insn
->opcodes
->opcode
= opcodes
;
444 (*cur_insn_ptr
) = new_insn
;
449 return (*cur_insn_ptr
);
454 gen_entry_traverse_tree (lf
*file
,
457 gen_entry_handler
* start
,
458 gen_entry_handler
* leaf
,
459 gen_entry_handler
* end
, void *data
)
463 ASSERT (table
!=NULL
);
464 ASSERT (table
->opcode
!= NULL
);
465 ASSERT (table
->nr_entries
> 0);
466 ASSERT (table
->entries
!= 0);
469 if (start
!= NULL
&& depth
>= 0)
471 start (file
, table
, depth
, data
);
474 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
476 if (entry
->entries
!= NULL
&& depth
!= 0)
478 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
479 start
, leaf
, end
, data
);
485 leaf (file
, entry
, depth
, data
);
490 if (end
!= NULL
&& depth
>= 0)
492 end (file
, table
, depth
, data
);
498 /* create a list element containing a single gen_table entry */
501 make_table (insn_table
*isa
, decode_table
*rules
, model_entry
*model
)
504 gen_list
*entry
= ZALLOC (gen_list
);
505 entry
->table
= ZALLOC (gen_entry
);
506 entry
->table
->top
= entry
;
507 entry
->model
= model
;
509 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
512 || insn
->processors
== NULL
513 || filter_is_member (insn
->processors
, model
->name
))
515 insn_list_insert (&entry
->table
->insns
, &entry
->table
->nr_insns
, insn
, NULL
, /* expanded_bits - none yet */
516 NULL
, /* opcodes - none yet */
517 0, /* nr_prefetched_words - none yet */
518 report_duplicate_insns
);
521 entry
->table
->opcode_rule
= rules
;
527 make_gen_tables (insn_table
*isa
, decode_table
*rules
)
529 gen_table
*gen
= ZALLOC (gen_table
);
532 if (options
.gen
.multi_sim
)
534 gen_list
**last
= &gen
->tables
;
537 if (options
.model_filter
!= NULL
)
538 processors
= options
.model_filter
;
540 processors
= isa
->model
->processors
;
541 for (model
= isa
->model
->models
; model
!= NULL
; model
= model
->next
)
543 if (filter_is_member (processors
, model
->name
))
545 *last
= make_table (isa
, rules
, model
);
546 last
= &(*last
)->next
;
552 gen
->tables
= make_table (isa
, rules
, NULL
);
558 /****************************************************************/
560 /* Is the bit, according to the decode rule, identical across all the
563 insns_bit_useless (insn_list
*insns
, decode_table
*rule
, int bit_nr
)
567 int is_useless
= 1; /* cleared if something actually found */
569 /* check the instructions for some constant value in at least one of
571 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
573 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
574 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
575 switch (bit
->field
->type
)
577 case insn_field_invalid
:
580 case insn_field_wild
:
581 case insn_field_reserved
:
582 /* neither useless or useful - ignore */
585 switch (rule
->search
)
587 case decode_find_strings
:
588 /* an integer isn't a string */
590 case decode_find_constants
:
591 case decode_find_mixed
:
592 /* an integer is useful if its value isn't the same
593 between all instructions. The first time through the
594 value is saved, the second time through (if the
595 values differ) it is marked as useful. */
598 else if (value
!= bit
->value
)
603 case insn_field_string
:
604 switch (rule
->search
)
606 case decode_find_strings
:
607 /* at least one string, keep checking */
610 case decode_find_constants
:
611 case decode_find_mixed
:
612 if (filter_is_member (rule
->constant_field_names
,
613 bit
->field
->val_string
))
614 /* a string field forced to constant? */
616 else if (bit
->field
->conditions
!= NULL
617 && bit
->field
->conditions
->test
== insn_field_cond_eq
618 && bit
->field
->conditions
->type
== insn_field_cond_value
)
620 int shift
= bit
->field
->last
- bit_nr
;
621 int bitvalue
= (bit
->field
->conditions
->value
>> shift
) & 1;
625 else if (value
!= bitvalue
)
628 else if (rule
->search
== decode_find_constants
)
629 /* the string field isn't constant */
636 /* Given only one constant value has been found, check through all
637 the instructions to see if at least one conditional makes it
639 if (value
>= 0 && is_useless
)
641 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
643 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
644 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
645 switch (bit
->field
->type
)
647 case insn_field_invalid
:
650 case insn_field_wild
:
651 case insn_field_reserved
:
653 /* already processed */
655 case insn_field_string
:
656 switch (rule
->search
)
658 case decode_find_strings
:
659 case decode_find_constants
:
660 /* already processed */
662 case decode_find_mixed
:
663 /* string field with conditions. If this condition
664 eliminates the value then the compare is useful */
665 if (bit
->field
->conditions
!= NULL
)
667 insn_field_cond
*condition
;
668 int shift
= bit
->field
->last
- bit_nr
;
669 for (condition
= bit
->field
->conditions
;
670 condition
!= NULL
; condition
= condition
->next
)
672 switch (condition
->type
)
674 case insn_field_cond_value
:
675 switch (condition
->test
)
677 case insn_field_cond_ne
:
678 if (((condition
->value
>> shift
) & 1)
680 /* conditional field excludes the
684 case insn_field_cond_eq
:
685 if (((condition
->value
>> shift
) & 1)
687 /* conditional field requires the
693 case insn_field_cond_field
:
694 /* are these handled separatly? */
708 /* go through a gen-table's list of instruction formats looking for a
709 range of bits that meet the decode table RULEs requirements */
711 static opcode_field
*
712 gen_entry_find_opcode_field (insn_list
*insns
,
713 decode_table
*rule
, int string_only
)
715 opcode_field curr_opcode
;
716 ASSERT (rule
!= NULL
);
718 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
719 curr_opcode
.word_nr
= rule
->word_nr
;
720 curr_opcode
.first
= rule
->first
;
721 curr_opcode
.last
= rule
->last
;
723 /* Try to reduce the size of first..last in accordance with the
726 while (curr_opcode
.first
<= rule
->last
)
728 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
733 while (curr_opcode
.last
>= rule
->first
)
735 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
741 /* did the final opcode field end up being empty? */
742 if (curr_opcode
.first
> curr_opcode
.last
)
746 ASSERT (curr_opcode
.last
>= rule
->first
);
747 ASSERT (curr_opcode
.first
<= rule
->last
);
748 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
750 /* Ensure that, for the non string only case, the opcode includes
751 the range forced_first .. forced_last */
752 if (!string_only
&& curr_opcode
.first
> rule
->force_first
)
754 curr_opcode
.first
= rule
->force_first
;
756 if (!string_only
&& curr_opcode
.last
< rule
->force_last
)
758 curr_opcode
.last
= rule
->force_last
;
761 /* For the string only case, force just the lower bound (so that the
762 shift can be eliminated) */
763 if (string_only
&& rule
->force_last
== options
.insn_bit_size
- 1)
765 curr_opcode
.last
= options
.insn_bit_size
- 1;
768 /* handle any special cases */
771 case normal_decode_rule
:
772 /* let the above apply */
773 curr_opcode
.nr_opcodes
=
774 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
777 curr_opcode
.is_boolean
= 1;
778 curr_opcode
.boolean_constant
= rule
->constant
;
779 curr_opcode
.nr_opcodes
= 2;
784 opcode_field
*new_field
= ZALLOC (opcode_field
);
785 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
792 gen_entry_insert_insn (gen_entry
*table
,
793 insn_entry
* old_insn
,
795 int new_nr_prefetched_words
,
796 int new_opcode_nr
, opcode_bits
*new_bits
)
798 gen_entry
**entry
= &table
->entries
;
800 /* find the new table for this entry */
801 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
803 entry
= &(*entry
)->sibling
;
806 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
808 /* insert the missing entry */
809 gen_entry
*new_entry
= ZALLOC (gen_entry
);
810 new_entry
->sibling
= (*entry
);
811 (*entry
) = new_entry
;
814 new_entry
->top
= table
->top
;
815 new_entry
->opcode_nr
= new_opcode_nr
;
816 new_entry
->word_nr
= new_word_nr
;
817 new_entry
->expanded_bits
= new_bits
;
818 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
819 new_entry
->parent
= table
;
820 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
822 /* ASSERT new_bits == cur_entry bits */
823 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
824 insn_list_insert (&(*entry
)->insns
, &(*entry
)->nr_insns
, old_insn
, NULL
, /* expanded_bits - only in final list */
825 NULL
, /* opcodes - only in final list */
826 new_nr_prefetched_words
, /* for this table */
827 report_duplicate_insns
);
832 gen_entry_expand_opcode (gen_entry
*table
,
833 insn_entry
* instruction
,
834 int bit_nr
, int opcode_nr
, opcode_bits
*bits
)
836 if (bit_nr
> table
->opcode
->last
)
838 /* Only include the hardwired bit information with an entry IF
839 that entry (and hence its functions) are being duplicated. */
840 if (options
.trace
.insn_expansion
)
842 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
843 notify (NULL
, ": insert %d - %s.%s%s\n",
845 instruction
->format_name
,
847 (table
->opcode_rule
->
848 with_duplicates
? " (duplicated)" : ""));
850 if (table
->opcode_rule
->with_duplicates
)
852 gen_entry_insert_insn (table
, instruction
,
853 table
->opcode
->word_nr
,
854 table
->nr_prefetched_words
, opcode_nr
, bits
);
858 gen_entry_insert_insn (table
, instruction
,
859 table
->opcode
->word_nr
,
860 table
->nr_prefetched_words
, opcode_nr
, NULL
);
865 insn_word_entry
*word
= instruction
->word
[table
->opcode
->word_nr
];
866 insn_field_entry
*field
= word
->bit
[bit_nr
]->field
;
867 int last_pos
= ((field
->last
< table
->opcode
->last
)
868 ? field
->last
: table
->opcode
->last
);
869 int first_pos
= ((field
->first
> table
->opcode
->first
)
870 ? field
->first
: table
->opcode
->first
);
871 int width
= last_pos
- first_pos
+ 1;
877 val
= sub_val (field
->val_int
, field
->last
, first_pos
, last_pos
);
878 gen_entry_expand_opcode (table
, instruction
,
880 ((opcode_nr
<< width
) | val
), bits
);
885 if (field
->type
== insn_field_reserved
)
886 gen_entry_expand_opcode (table
, instruction
,
888 ((opcode_nr
<< width
)), bits
);
892 int last_val
= (table
->opcode
->is_boolean
? 2 : (1 << width
));
893 for (val
= 0; val
< last_val
; val
++)
895 /* check to see if the value has been precluded
896 (by a conditional) in some way */
898 insn_field_cond
*condition
;
899 for (condition
= field
->conditions
, is_precluded
= 0;
900 condition
!= NULL
&& !is_precluded
;
901 condition
= condition
->next
)
903 switch (condition
->type
)
905 case insn_field_cond_value
:
908 sub_val (condition
->value
, field
->last
,
909 first_pos
, last_pos
);
910 switch (condition
->test
)
912 case insn_field_cond_ne
:
916 case insn_field_cond_eq
:
923 case insn_field_cond_field
:
928 /* Try to find a value for the
929 conditional by looking back through
930 the previously defined bits for one
931 that covers the designated
933 for (bit
= bits
; bit
!= NULL
; bit
= bit
->next
)
935 if (bit
->field
->word_nr
==
936 condition
->field
->word_nr
937 && bit
->first
<= condition
->field
->first
938 && bit
->last
>= condition
->field
->last
)
940 /* the bit field fully specified
941 the conditional field's value */
942 value
= sub_val (bit
->value
, bit
->last
,
949 /* Try to find a value by looking
950 through this and previous tables */
954 t
->parent
!= NULL
; t
= t
->parent
)
956 if (t
->parent
->opcode
->word_nr
==
957 condition
->field
->word_nr
958 && t
->parent
->opcode
->first
<=
959 condition
->field
->first
960 && t
->parent
->opcode
->last
>=
961 condition
->field
->last
)
963 /* the table entry fully
964 specified the condition
966 /* extract the field's value
969 sub_val (t
->opcode_nr
,
970 t
->parent
->opcode
->last
,
971 condition
->field
->first
,
972 condition
->field
->last
);
973 /* this is a requirement of
975 refering to another field */
976 ASSERT ((condition
->field
->first
-
977 condition
->field
->last
) ==
978 (first_pos
- last_pos
));
980 ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
982 t
->parent
->opcode
->last
,
983 condition
->field
->first
,
984 condition
->field
->last
);
988 if (bit
== NULL
&& t
== NULL
)
989 error (instruction
->line
,
990 "Conditional `%s' of field `%s' isn't expanded",
991 condition
->string
, field
->val_string
);
992 switch (condition
->test
)
994 case insn_field_cond_ne
:
998 case insn_field_cond_eq
:
1009 /* Only add additional hardwired bit
1010 information if the entry is not going to
1011 later be combined */
1012 if (table
->opcode_rule
->with_combine
)
1014 gen_entry_expand_opcode (table
, instruction
,
1016 ((opcode_nr
<< width
) |
1021 opcode_bits
*new_bits
=
1022 new_opcode_bits (bits
, val
,
1023 first_pos
, last_pos
,
1026 gen_entry_expand_opcode (table
, instruction
,
1028 ((opcode_nr
<< width
) |
1040 gen_entry_insert_expanding (gen_entry
*table
, insn_entry
* instruction
)
1042 gen_entry_expand_opcode (table
,
1044 table
->opcode
->first
, 0, table
->expanded_bits
);
1049 insns_match_format_names (insn_list
*insns
, filter
*format_names
)
1051 if (format_names
!= NULL
)
1054 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1056 if (i
->insn
->format_name
!= NULL
1057 && !filter_is_member (format_names
, i
->insn
->format_name
))
1065 table_matches_path (gen_entry
*table
, decode_path_list
*paths
)
1069 while (paths
!= NULL
)
1071 gen_entry
*entry
= table
;
1072 decode_path
*path
= paths
->path
;
1075 if (entry
== NULL
&& path
== NULL
)
1077 if (entry
== NULL
|| path
== NULL
)
1079 if (entry
->opcode_nr
!= path
->opcode_nr
)
1081 entry
= entry
->parent
;
1082 path
= path
->parent
;
1084 paths
= paths
->next
;
1091 insns_match_conditions (insn_list
*insns
, decode_cond
*conditions
)
1093 if (conditions
!= NULL
)
1096 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1099 for (cond
= conditions
; cond
!= NULL
; cond
= cond
->next
)
1102 if (i
->insn
->nr_words
<= cond
->word_nr
)
1104 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
1106 if (!cond
->mask
[bit_nr
])
1108 if (!i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->mask
)
1110 if ((i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->value
1111 == cond
->value
[bit_nr
]) == !cond
->is_equal
)
1121 insns_match_nr_words (insn_list
*insns
, int nr_words
)
1124 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1126 if (i
->insn
->nr_words
< nr_words
)
1133 insn_list_cmp (insn_list
*l
, insn_list
*r
)
1138 if (l
== NULL
&& r
== NULL
)
1144 if (l
->insn
!= r
->insn
)
1145 return -1; /* somewhat arbitrary at present */
1146 /* skip this insn */
1148 while (l
!= NULL
&& l
->insn
== insn
)
1150 while (r
!= NULL
&& r
->insn
== insn
)
1158 gen_entry_expand_insns (gen_entry
*table
)
1160 decode_table
*opcode_rule
;
1162 ASSERT (table
->nr_insns
>= 1);
1164 /* determine a valid opcode */
1165 for (opcode_rule
= table
->opcode_rule
;
1166 opcode_rule
!= NULL
; opcode_rule
= opcode_rule
->next
)
1168 char *discard_reason
;
1169 if (table
->top
->model
!= NULL
1170 && opcode_rule
->model_names
!= NULL
1171 && !filter_is_member (opcode_rule
->model_names
,
1172 table
->top
->model
->name
))
1174 /* the rule isn't applicable to this processor */
1175 discard_reason
= "wrong model";
1177 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1179 /* for safety, require a pre-codition when attempting to
1180 apply a rule to a single instruction */
1181 discard_reason
= "need pre-condition when nr-insn == 1";
1183 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1185 /* Little point in expanding a single instruction when we're
1186 not duplicating the semantic functions that this table
1188 discard_reason
= "need duplication with nr-insns == 1";
1191 if (!insns_match_format_names
1192 (table
->insns
, opcode_rule
->format_names
))
1194 discard_reason
= "wrong format name";
1196 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1198 discard_reason
= "wrong nr words";
1200 else if (!table_matches_path (table
, opcode_rule
->paths
))
1202 discard_reason
= "path failed";
1205 if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1207 discard_reason
= "condition failed";
1211 discard_reason
= "no opcode field";
1212 table
->opcode
= gen_entry_find_opcode_field (table
->insns
,
1214 table
->nr_insns
== 1 /*string-only */
1216 if (table
->opcode
!= NULL
)
1218 table
->opcode_rule
= opcode_rule
;
1223 if (options
.trace
.rule_rejection
)
1225 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1226 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1230 /* did we find anything */
1231 if (opcode_rule
== NULL
)
1233 /* the decode table failed, this set of instructions haven't
1234 been uniquely identified */
1235 if (table
->nr_insns
> 1)
1237 print_gen_entry_insns (table
, warning
,
1238 "was not uniquely decoded",
1239 "decodes to the same entry");
1245 /* Determine the number of words that must have been prefetched for
1246 this table to function */
1247 if (table
->parent
== NULL
)
1248 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1249 else if (table
->opcode_rule
->word_nr
+ 1 >
1250 table
->parent
->nr_prefetched_words
)
1251 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1253 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1255 /* back link what we found to its parent */
1256 if (table
->parent
!= NULL
)
1258 ASSERT (table
->parent
->opcode
!= NULL
);
1259 table
->opcode
->parent
= table
->parent
->opcode
;
1262 /* report the rule being used to expand the instructions */
1263 if (options
.trace
.rule_selection
)
1265 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1267 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1268 table
->opcode
->word_nr
,
1269 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1270 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1271 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1272 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1273 table
->opcode
->nr_opcodes
, table
->nr_entries
);
1276 /* expand the raw instructions according to the opcode */
1279 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1281 if (options
.trace
.insn_expansion
)
1283 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1284 notify (NULL
, ": expand - %s.%s\n",
1285 entry
->insn
->format_name
, entry
->insn
->name
);
1287 gen_entry_insert_expanding (table
, entry
->insn
);
1291 /* dump the results */
1292 if (options
.trace
.entries
)
1295 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1298 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1299 notify (NULL
, ": %d - entries %d -",
1300 entry
->opcode_nr
, entry
->nr_insns
);
1301 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1302 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1303 notify (NULL
, "\n");
1307 /* perform a combine pass if needed */
1308 if (table
->opcode_rule
->with_combine
)
1311 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1313 if (entry
->combined_parent
== NULL
)
1315 gen_entry
**last
= &entry
->combined_next
;
1317 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1319 if (alt
->combined_parent
== NULL
1320 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1322 alt
->combined_parent
= entry
;
1324 last
= &alt
->combined_next
;
1329 if (options
.trace
.combine
)
1333 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1335 if (entry
->combined_parent
== NULL
)
1338 gen_entry
*duplicate
;
1340 print_gen_entry_path (table
->opcode_rule
->line
, entry
,
1342 for (duplicate
= entry
->combined_next
; duplicate
!= NULL
;
1343 duplicate
= duplicate
->combined_next
)
1345 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1347 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1348 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1350 notify (NULL
, " %s.%s",
1351 l
->insn
->format_name
, l
->insn
->name
);
1353 notify (NULL
, "\n");
1356 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1358 ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1359 table
->opcode
->word_nr
, i2target (options
.hi_bit_nr
,
1360 table
->opcode
->first
),
1361 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1362 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1363 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1364 table
->opcode
->nr_opcodes
, table
->nr_entries
, nr_unique
);
1368 /* Check that the rule did more than re-arange the order of the
1372 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1374 if (entry
->combined_parent
== NULL
)
1376 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1378 print_gen_entry_path (table
->opcode_rule
->line
, table
,
1381 ": Applying rule just copied all instructions\n");
1382 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1389 /* if some form of expanded table, fill in the missing dots */
1390 switch (table
->opcode_rule
->gen
)
1392 case padded_switch_gen
:
1394 case goto_switch_gen
:
1395 if (!table
->opcode
->is_boolean
)
1397 gen_entry
**entry
= &table
->entries
;
1398 gen_entry
*illegals
= NULL
;
1399 gen_entry
**last_illegal
= &illegals
;
1401 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1403 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1405 /* missing - insert it under our feet at *entry */
1406 gen_entry_insert_insn (table
, table
->top
->isa
->illegal_insn
, table
->opcode
->word_nr
, 0, /* nr_prefetched_words == 0 for invalid */
1408 ASSERT ((*entry
) != NULL
);
1409 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1410 (*last_illegal
) = *entry
;
1411 (*last_illegal
)->combined_parent
= illegals
;
1412 last_illegal
= &(*last_illegal
)->combined_next
;
1414 entry
= &(*entry
)->sibling
;
1417 /* oops, will have pointed the first illegal insn back to
1418 its self. Fix this */
1419 if (illegals
!= NULL
)
1420 illegals
->combined_parent
= NULL
;
1429 /* and do the same for the newly created sub entries but *only*
1430 expand entries that haven't been combined. */
1433 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1435 if (entry
->combined_parent
== NULL
)
1437 gen_entry_expand_insns (entry
);
1444 gen_tables_expand_insns (gen_table
*gen
)
1447 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1449 gen_entry_expand_insns (entry
->table
);
1454 /* create a list of all the semantic functions that need to be
1455 generated. Eliminate any duplicates. Verify that the decode stage
1459 make_gen_semantics_list (lf
*file
, gen_entry
*entry
, int depth
, void *data
)
1461 gen_table
*gen
= (gen_table
*) data
;
1463 /* Not interested in an entrie that have been combined into some
1464 other entry at the same level */
1465 if (entry
->combined_parent
!= NULL
)
1468 /* a leaf should contain exactly one instruction. If not the decode
1470 ASSERT (entry
->nr_insns
== 1);
1472 /* Enter this instruction into the list of semantic functions. */
1473 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1475 entry
->expanded_bits
,
1476 entry
->parent
->opcode
,
1477 entry
->insns
->nr_prefetched_words
,
1478 merge_duplicate_insns
);
1479 /* point the table entry at the real semantic function */
1480 ASSERT (insn
!= NULL
);
1481 entry
->insns
->semantic
= insn
;
1486 gen_tables_expand_semantics (gen_table
*gen
)
1489 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1491 gen_entry_traverse_tree (NULL
, entry
->table
, 1, /* depth */
1492 NULL
, /* start-handler */
1493 make_gen_semantics_list
, /* leaf-handler */
1494 NULL
, /* end-handler */
1505 dump_opcode_field (lf
*file
,
1507 opcode_field
*field
, char *suffix
, int levels
)
1509 lf_printf (file
, "%s(opcode_field *) 0x%lx", prefix
, (long) field
);
1510 if (levels
&& field
!= NULL
)
1512 lf_indent (file
, +1);
1513 lf_printf (file
, "\n(first %d)", field
->first
);
1514 lf_printf (file
, "\n(last %d)", field
->last
);
1515 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1516 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1517 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1518 dump_opcode_field (file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1519 lf_indent (file
, -1);
1521 lf_printf (file
, "%s", suffix
);
1526 dump_opcode_bits (lf
*file
,
1527 char *prefix
, opcode_bits
*bits
, char *suffix
, int levels
)
1529 lf_printf (file
, "%s(opcode_bits *) 0x%lx", prefix
, (long) bits
);
1531 if (levels
&& bits
!= NULL
)
1533 lf_indent (file
, +1);
1534 lf_printf (file
, "\n(value %d)", bits
->value
);
1535 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1536 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1537 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1538 lf_indent (file
, -1);
1540 lf_printf (file
, "%s", suffix
);
1546 dump_insn_list (lf
*file
, char *prefix
, insn_list
*entry
, char *suffix
)
1548 lf_printf (file
, "%s(insn_list *) 0x%lx", prefix
, (long) entry
);
1552 lf_indent (file
, +1);
1553 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1554 lf_printf (file
, "\n(next 0x%lx)", (long) entry
->next
);
1555 lf_indent (file
, -1);
1557 lf_printf (file
, "%s", suffix
);
1562 dump_insn_word_entry_list_entries (lf
*file
,
1564 insn_list
*entry
, char *suffix
)
1566 lf_printf (file
, "%s", prefix
);
1567 while (entry
!= NULL
)
1569 dump_insn_list (file
, "\n(", entry
, ")");
1570 entry
= entry
->next
;
1572 lf_printf (file
, "%s", suffix
);
1577 dump_gen_entry (lf
*file
,
1578 char *prefix
, gen_entry
*table
, char *suffix
, int levels
)
1581 lf_printf (file
, "%s(gen_entry *) 0x%lx", prefix
, (long) table
);
1583 if (levels
&& table
!=NULL
)
1586 lf_indent (file
, +1);
1587 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1588 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1589 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")",
1591 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1592 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
,
1594 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1595 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1596 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1597 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")",
1599 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1600 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1601 lf_indent (file
, -1);
1603 lf_printf (file
, "%s", suffix
);
1607 dump_gen_list (lf
*file
,
1608 char *prefix
, gen_list
*entry
, char *suffix
, int levels
)
1610 while (entry
!= NULL
)
1612 lf_printf (file
, "%s(gen_list *) 0x%lx", prefix
, (long) entry
);
1613 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1614 lf_printf (file
, "\n(next (gen_list *) 0x%lx)", (long) entry
->next
);
1615 lf_printf (file
, "%s", suffix
);
1621 dump_gen_table (lf
*file
,
1622 char *prefix
, gen_table
*gen
, char *suffix
, int levels
)
1624 lf_printf (file
, "%s(gen_table *) 0x%lx", prefix
, (long) gen
);
1625 lf_printf (file
, "\n(isa (insn_table *) 0x%lx)", (long) gen
->isa
);
1626 lf_printf (file
, "\n(rules (decode_table *) 0x%lx)", (long) gen
->rules
);
1627 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1628 lf_printf (file
, "%s", suffix
);
1632 igen_options options
;
1635 main (int argc
, char **argv
)
1637 decode_table
*decode_rules
;
1638 insn_table
*instructions
;
1644 "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1646 INIT_OPTIONS (options
);
1648 filter_parse (&options
.flags_filter
, argv
[1]);
1650 options
.hi_bit_nr
= a2i (argv
[2]);
1651 options
.insn_bit_size
= a2i (argv
[3]);
1652 options
.insn_specifying_widths
= a2i (argv
[4]);
1653 ASSERT (options
.hi_bit_nr
< options
.insn_bit_size
);
1655 instructions
= load_insn_table (argv
[6], NULL
);
1656 decode_rules
= load_decode_table (argv
[5]);
1657 gen
= make_gen_tables (instructions
, decode_rules
);
1659 gen_tables_expand_insns (gen
);
1661 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1663 dump_gen_table (l
, "(", gen
, ")\n", -1);