1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2024 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
, const gen_entry
*entry
, int depth
, void *data
)
43 int *max_depth
= (int *) data
;
44 if (*max_depth
< depth
)
50 gen_entry_depth (const gen_entry
*table
)
53 gen_entry_traverse_tree (NULL
, table
, 1, NULL
, /*start */
54 update_depth
, NULL
, /*end */
61 print_gen_entry_path (const line_ref
*line
,
62 const gen_entry
*table
,
65 if (table
->parent
== NULL
)
67 if (table
->top
->model
!= NULL
)
68 print (line
, "%s", table
->top
->model
->name
);
71 /* We don't want to output things, but we want the side-effects they
72 might have (e.g. checking line != NULL). */
73 print (line
, "%s", "");
78 print_gen_entry_path (line
, table
->parent
, print
);
79 print (NULL
, ".%d", table
->opcode_nr
);
84 print_gen_entry_insns (const gen_entry
*table
,
86 const char *first_message
,
87 const char *next_message
)
91 message
= first_message
;
92 for (i
= table
->insns
; i
!= NULL
; i
= i
->next
)
94 insn_entry
*insn
= i
->insn
;
95 print_gen_entry_path (insn
->line
, table
, print
);
96 print (NULL
, ": %s.%s %s\n", insn
->format_name
, insn
->name
, message
);
97 if (next_message
!= NULL
)
98 message
= next_message
;
104 insn_field_cmp (const insn_word_entry
*l
, const insn_word_entry
*r
)
109 if (l
== NULL
&& r
== NULL
)
110 return 0; /* all previous fields the same */
112 return -1; /* left shorter than right */
114 return +1; /* left longer than right */
115 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
117 if (l
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
119 if (r
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
121 if (l
->bit
[bit_nr
]->field
->conditions
== NULL
)
123 if (r
->bit
[bit_nr
]->field
->conditions
== NULL
)
126 printf ("%s%s%s VS %s%s%s\n",
127 l
->bit
[bit_nr
]->field
->val_string
,
128 l
->bit
[bit_nr
]->field
->conditions
->test
==
129 insn_field_cond_eq
? "=" : "!",
130 l
->bit
[bit_nr
]->field
->conditions
->string
,
131 r
->bit
[bit_nr
]->field
->val_string
,
132 r
->bit
[bit_nr
]->field
->conditions
->test
==
133 insn_field_cond_eq
? "=" : "!",
134 r
->bit
[bit_nr
]->field
->conditions
->string
);
135 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
136 && r
->bit
[bit_nr
]->field
->conditions
->test
==
139 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
140 insn_field_cond_field
141 && r
->bit
[bit_nr
]->field
->conditions
->type
==
142 insn_field_cond_field
)
143 /* somewhat arbitrary */
145 int cmp
= strcmp (l
->bit
[bit_nr
]->field
->conditions
->string
,
146 r
->bit
[bit_nr
]->field
->conditions
->
153 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
154 insn_field_cond_field
)
156 if (r
->bit
[bit_nr
]->field
->conditions
->type
==
157 insn_field_cond_field
)
159 /* The case of both fields having constant values should have
160 already have been handled because such fields are converted
161 into normal constant fields, but we must not make this
162 an assert, as we wouldn't gracefully handle an (invalid)
163 duplicate insn description. */
166 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
167 return +1; /* left = only */
168 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
169 return -1; /* right = only */
170 /* FIXME: Need to some what arbitrarily order conditional lists */
180 insn_word_cmp (const insn_word_entry
*l
, const insn_word_entry
*r
)
185 if (l
== NULL
&& r
== NULL
)
186 return 0; /* all previous fields the same */
188 return -1; /* left shorter than right */
190 return +1; /* left longer than right */
191 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
193 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
195 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
197 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
199 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
209 opcode_bit_cmp (const opcode_bits
*l
, const opcode_bits
*r
)
211 if (l
== NULL
&& r
== NULL
)
212 return 0; /* all previous bits the same */
214 return -1; /* left shorter than right */
216 return +1; /* left longer than right */
217 /* most significant word */
218 if (l
->field
->word_nr
< r
->field
->word_nr
)
219 return +1; /* left has more significant word */
220 if (l
->field
->word_nr
> r
->field
->word_nr
)
221 return -1; /* right has more significant word */
222 /* most significant bit? */
223 if (l
->first
< r
->first
)
224 return +1; /* left as more significant bit */
225 if (l
->first
> r
->first
)
226 return -1; /* right as more significant bit */
228 if (l
->last
< r
->last
)
229 return +1; /* left as less bits */
230 if (l
->last
> r
->last
)
231 return -1; /* right as less bits */
233 if (l
->value
< r
->value
)
235 if (l
->value
> r
->value
)
243 opcode_bits_cmp (const opcode_bits
*l
, const opcode_bits
*r
)
248 if (l
== NULL
&& r
== NULL
)
249 return 0; /* all previous bits the same */
250 cmp
= opcode_bit_cmp (l
, r
);
260 new_opcode_bits (opcode_bits
*old_bits
,
263 int last
, insn_field_entry
*field
, opcode_field
*opcode
)
265 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
266 new_bits
->field
= field
;
267 new_bits
->value
= value
;
268 new_bits
->first
= first
;
269 new_bits
->last
= last
;
270 new_bits
->opcode
= opcode
;
272 if (old_bits
!= NULL
)
274 opcode_bits
*new_list
;
275 opcode_bits
**last
= &new_list
;
276 new_list
= new_opcode_bits (old_bits
->next
,
280 old_bits
->field
, old_bits
->opcode
);
281 while (*last
!= NULL
)
283 int cmp
= opcode_bit_cmp (new_bits
, *last
);
284 if (cmp
< 0) /* new < new_list */
290 ERROR ("Duplicated insn bits in list");
292 last
= &(*last
)->next
;
294 new_bits
->next
= *last
;
304 /* Same as strcmp(). */
306 name_cmp (const char *l
, const char *r
)
308 if (l
== NULL
&& r
== NULL
)
310 if (l
!= NULL
&& r
== NULL
)
312 if (l
== NULL
&& r
!= NULL
)
314 return strcmp (l
, r
);
320 merge_duplicate_insns
,
321 report_duplicate_insns
,
323 duplicate_insn_actions
;
326 insn_list_insert (insn_list
**cur_insn_ptr
,
329 opcode_bits
*expanded_bits
,
330 opcode_field
*opcodes
,
331 int nr_prefetched_words
,
332 duplicate_insn_actions duplicate_action
)
334 /* insert it according to the order of the fields & bits */
335 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
339 /* key#1 sort according to the constant fields of each instruction */
340 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
346 /* key#2 sort according to the expanded bits of each instruction */
347 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
353 /* key#3 sort according to the non-constant fields of each instruction */
354 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
360 if (duplicate_action
== merge_duplicate_insns
)
362 /* key#4: If we're going to merge duplicates, also sort
363 according to the format_name. Two instructions with
364 identical decode patterns, but different names, are
365 considered different when merging. Duplicates are only
366 important when creating a decode table (implied by
367 report_duplicate_insns) as such a table only has the
368 instruction's bit code as a way of differentiating
369 between instructions. */
370 int cmp
= name_cmp (insn
->format_name
,
371 (*cur_insn_ptr
)->insn
->format_name
);
378 if (duplicate_action
== merge_duplicate_insns
)
380 /* key#5: If we're going to merge duplicates, also sort
381 according to the name. See comment above for
383 int cmp
= name_cmp (insn
->name
, (*cur_insn_ptr
)->insn
->name
);
390 /* duplicate keys, report problem */
391 switch (duplicate_action
)
393 case report_duplicate_insns
:
394 /* It would appear that we have two instructions with the
395 same constant field values across all words and bits.
396 This error can also occure when insn_field_cmp() is
397 failing to differentiate between two instructions that
398 differ only in their conditional fields. */
400 "Two instructions with identical constant fields\n");
401 error ((*cur_insn_ptr
)->insn
->line
,
402 "Location of duplicate instruction\n");
403 case merge_duplicate_insns
:
404 /* Add the opcode path to the instructions list */
405 if (options
.trace
.insn_insertion
)
407 notify ((*cur_insn_ptr
)->insn
->line
,
408 "%s.%s: insert merge %s.%s\n",
409 (*cur_insn_ptr
)->insn
->format_name
,
410 (*cur_insn_ptr
)->insn
->name
,
416 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
417 while (*last
!= NULL
)
419 last
= &(*last
)->next
;
421 (*last
) = ZALLOC (insn_opcodes
);
422 (*last
)->opcode
= opcodes
;
424 /* Use the larger nr_prefetched_words */
425 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
426 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
427 return (*cur_insn_ptr
);
432 /* create a new list entry and insert it */
434 insn_list
*new_insn
= ZALLOC (insn_list
);
435 if (options
.trace
.insn_insertion
)
438 "%s.%s: insert new\n",
442 new_insn
->insn
= insn
;
443 new_insn
->expanded_bits
= expanded_bits
;
444 new_insn
->next
= (*cur_insn_ptr
);
445 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
448 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
449 new_insn
->opcodes
->opcode
= opcodes
;
451 (*cur_insn_ptr
) = new_insn
;
456 return (*cur_insn_ptr
);
461 gen_entry_traverse_tree (lf
*file
,
462 const gen_entry
*table
,
464 gen_entry_handler
* start
,
465 gen_entry_handler
* leaf
,
466 gen_entry_handler
* end
, void *data
)
470 ASSERT (table
!=NULL
);
471 ASSERT (table
->opcode
!= NULL
);
472 ASSERT (table
->nr_entries
> 0);
473 ASSERT (table
->entries
!= 0);
476 if (start
!= NULL
&& depth
>= 0)
478 start (file
, table
, depth
, data
);
481 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
483 if (entry
->entries
!= NULL
&& depth
!= 0)
485 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
486 start
, leaf
, end
, data
);
492 leaf (file
, entry
, depth
, data
);
497 if (end
!= NULL
&& depth
>= 0)
499 end (file
, table
, depth
, data
);
505 /* create a list element containing a single gen_table entry */
508 make_table (const insn_table
*isa
,
509 const decode_table
*rules
,
510 const model_entry
*model
)
513 gen_list
*entry
= ZALLOC (gen_list
);
514 entry
->table
= ZALLOC (gen_entry
);
515 entry
->table
->top
= entry
;
516 entry
->model
= model
;
518 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
521 || insn
->processors
== NULL
522 || filter_is_member (insn
->processors
, model
->name
))
524 insn_list_insert (&entry
->table
->insns
, &entry
->table
->nr_insns
, insn
, NULL
, /* expanded_bits - none yet */
525 NULL
, /* opcodes - none yet */
526 0, /* nr_prefetched_words - none yet */
527 report_duplicate_insns
);
530 entry
->table
->opcode_rule
= rules
;
536 make_gen_tables (const insn_table
*isa
, const decode_table
*rules
)
538 gen_table
*gen
= ZALLOC (gen_table
);
541 if (options
.gen
.multi_sim
)
543 gen_list
**last
= &gen
->tables
;
546 if (options
.model_filter
!= NULL
)
547 processors
= options
.model_filter
;
549 processors
= isa
->model
->processors
;
550 for (model
= isa
->model
->models
; model
!= NULL
; model
= model
->next
)
552 if (filter_is_member (processors
, model
->name
))
554 *last
= make_table (isa
, rules
, model
);
555 last
= &(*last
)->next
;
561 gen
->tables
= make_table (isa
, rules
, NULL
);
567 /****************************************************************/
569 /* Is the bit, according to the decode rule, identical across all the
572 insns_bit_useless (const insn_list
*insns
, const decode_table
*rule
, int bit_nr
)
574 const insn_list
*entry
;
576 int is_useless
= 1; /* cleared if something actually found */
578 /* check the instructions for some constant value in at least one of
580 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
582 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
583 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
584 switch (bit
->field
->type
)
586 case insn_field_invalid
:
589 case insn_field_wild
:
590 case insn_field_reserved
:
591 /* neither useless or useful - ignore */
594 switch (rule
->search
)
596 case decode_find_strings
:
597 /* an integer isn't a string */
599 case decode_find_constants
:
600 case decode_find_mixed
:
601 /* an integer is useful if its value isn't the same
602 between all instructions. The first time through the
603 value is saved, the second time through (if the
604 values differ) it is marked as useful. */
607 else if (value
!= bit
->value
)
612 case insn_field_string
:
613 switch (rule
->search
)
615 case decode_find_strings
:
616 /* at least one string, keep checking */
619 case decode_find_constants
:
620 case decode_find_mixed
:
621 if (filter_is_member (rule
->constant_field_names
,
622 bit
->field
->val_string
))
623 /* a string field forced to constant? */
625 else if (bit
->field
->conditions
!= NULL
626 && bit
->field
->conditions
->test
== insn_field_cond_eq
627 && bit
->field
->conditions
->type
== insn_field_cond_value
)
629 int shift
= bit
->field
->last
- bit_nr
;
630 int bitvalue
= (bit
->field
->conditions
->value
>> shift
) & 1;
634 else if (value
!= bitvalue
)
637 else if (rule
->search
== decode_find_constants
)
638 /* the string field isn't constant */
645 /* Given only one constant value has been found, check through all
646 the instructions to see if at least one conditional makes it
648 if (value
>= 0 && is_useless
)
650 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
652 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
653 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
654 switch (bit
->field
->type
)
656 case insn_field_invalid
:
659 case insn_field_wild
:
660 case insn_field_reserved
:
662 /* already processed */
664 case insn_field_string
:
665 switch (rule
->search
)
667 case decode_find_strings
:
668 case decode_find_constants
:
669 /* already processed */
671 case decode_find_mixed
:
672 /* string field with conditions. If this condition
673 eliminates the value then the compare is useful */
674 if (bit
->field
->conditions
!= NULL
)
676 insn_field_cond
*condition
;
677 int shift
= bit
->field
->last
- bit_nr
;
678 for (condition
= bit
->field
->conditions
;
679 condition
!= NULL
; condition
= condition
->next
)
681 switch (condition
->type
)
683 case insn_field_cond_value
:
684 switch (condition
->test
)
686 case insn_field_cond_ne
:
687 if (((condition
->value
>> shift
) & 1)
689 /* conditional field excludes the
693 case insn_field_cond_eq
:
694 if (((condition
->value
>> shift
) & 1)
696 /* conditional field requires the
702 case insn_field_cond_field
:
703 /* are these handled separatly? */
717 /* go through a gen-table's list of instruction formats looking for a
718 range of bits that meet the decode table RULEs requirements */
720 static opcode_field
*
721 gen_entry_find_opcode_field (insn_list
*insns
,
722 const decode_table
*rule
, int string_only
)
724 opcode_field curr_opcode
;
725 ASSERT (rule
!= NULL
);
727 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
728 curr_opcode
.word_nr
= rule
->word_nr
;
729 curr_opcode
.first
= rule
->first
;
730 curr_opcode
.last
= rule
->last
;
732 /* Try to reduce the size of first..last in accordance with the
735 while (curr_opcode
.first
<= rule
->last
)
737 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
742 while (curr_opcode
.last
>= rule
->first
)
744 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
750 /* did the final opcode field end up being empty? */
751 if (curr_opcode
.first
> curr_opcode
.last
)
755 ASSERT (curr_opcode
.last
>= rule
->first
);
756 ASSERT (curr_opcode
.first
<= rule
->last
);
757 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
759 /* Ensure that, for the non string only case, the opcode includes
760 the range forced_first .. forced_last */
761 if (!string_only
&& curr_opcode
.first
> rule
->force_first
)
763 curr_opcode
.first
= rule
->force_first
;
765 if (!string_only
&& curr_opcode
.last
< rule
->force_last
)
767 curr_opcode
.last
= rule
->force_last
;
770 /* For the string only case, force just the lower bound (so that the
771 shift can be eliminated) */
772 if (string_only
&& rule
->force_last
== options
.insn_bit_size
- 1)
774 curr_opcode
.last
= options
.insn_bit_size
- 1;
777 /* handle any special cases */
780 case normal_decode_rule
:
781 /* let the above apply */
782 curr_opcode
.nr_opcodes
=
783 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
786 curr_opcode
.is_boolean
= 1;
787 curr_opcode
.boolean_constant
= rule
->constant
;
788 curr_opcode
.nr_opcodes
= 2;
793 opcode_field
*new_field
= ZALLOC (opcode_field
);
794 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
801 gen_entry_insert_insn (gen_entry
*table
,
802 insn_entry
* old_insn
,
804 int new_nr_prefetched_words
,
805 int new_opcode_nr
, opcode_bits
*new_bits
)
807 gen_entry
**entry
= &table
->entries
;
809 /* find the new table for this entry */
810 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
812 entry
= &(*entry
)->sibling
;
815 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
817 /* insert the missing entry */
818 gen_entry
*new_entry
= ZALLOC (gen_entry
);
819 new_entry
->sibling
= (*entry
);
820 (*entry
) = new_entry
;
823 new_entry
->top
= table
->top
;
824 new_entry
->opcode_nr
= new_opcode_nr
;
825 new_entry
->word_nr
= new_word_nr
;
826 new_entry
->expanded_bits
= new_bits
;
827 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
828 new_entry
->parent
= table
;
829 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
831 /* ASSERT new_bits == cur_entry bits */
832 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
833 insn_list_insert (&(*entry
)->insns
, &(*entry
)->nr_insns
, old_insn
, NULL
, /* expanded_bits - only in final list */
834 NULL
, /* opcodes - only in final list */
835 new_nr_prefetched_words
, /* for this table */
836 report_duplicate_insns
);
841 gen_entry_expand_opcode (gen_entry
*table
,
842 insn_entry
* instruction
,
843 int bit_nr
, int opcode_nr
, opcode_bits
*bits
)
845 if (bit_nr
> table
->opcode
->last
)
847 /* Only include the hardwired bit information with an entry IF
848 that entry (and hence its functions) are being duplicated. */
849 if (options
.trace
.insn_expansion
)
851 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
852 notify (NULL
, ": insert %d - %s.%s%s\n",
854 instruction
->format_name
,
856 (table
->opcode_rule
->
857 with_duplicates
? " (duplicated)" : ""));
859 if (table
->opcode_rule
->with_duplicates
)
861 gen_entry_insert_insn (table
, instruction
,
862 table
->opcode
->word_nr
,
863 table
->nr_prefetched_words
, opcode_nr
, bits
);
867 gen_entry_insert_insn (table
, instruction
,
868 table
->opcode
->word_nr
,
869 table
->nr_prefetched_words
, opcode_nr
, NULL
);
874 insn_word_entry
*word
= instruction
->word
[table
->opcode
->word_nr
];
875 insn_field_entry
*field
= word
->bit
[bit_nr
]->field
;
876 int last_pos
= ((field
->last
< table
->opcode
->last
)
877 ? field
->last
: table
->opcode
->last
);
878 int first_pos
= ((field
->first
> table
->opcode
->first
)
879 ? field
->first
: table
->opcode
->first
);
880 int width
= last_pos
- first_pos
+ 1;
886 val
= sub_val (field
->val_int
, field
->last
, first_pos
, last_pos
);
887 gen_entry_expand_opcode (table
, instruction
,
889 ((opcode_nr
<< width
) | val
), bits
);
894 if (field
->type
== insn_field_reserved
)
895 gen_entry_expand_opcode (table
, instruction
,
897 ((opcode_nr
<< width
)), bits
);
901 int last_val
= (table
->opcode
->is_boolean
? 2 : (1 << width
));
902 for (val
= 0; val
< last_val
; val
++)
904 /* check to see if the value has been precluded
905 (by a conditional) in some way */
907 insn_field_cond
*condition
;
908 for (condition
= field
->conditions
, is_precluded
= 0;
909 condition
!= NULL
&& !is_precluded
;
910 condition
= condition
->next
)
912 switch (condition
->type
)
914 case insn_field_cond_value
:
917 sub_val (condition
->value
, field
->last
,
918 first_pos
, last_pos
);
919 switch (condition
->test
)
921 case insn_field_cond_ne
:
925 case insn_field_cond_eq
:
932 case insn_field_cond_field
:
937 /* Try to find a value for the
938 conditional by looking back through
939 the previously defined bits for one
940 that covers the designated
942 for (bit
= bits
; bit
!= NULL
; bit
= bit
->next
)
944 if (bit
->field
->word_nr
==
945 condition
->field
->word_nr
946 && bit
->first
<= condition
->field
->first
947 && bit
->last
>= condition
->field
->last
)
949 /* the bit field fully specified
950 the conditional field's value */
951 value
= sub_val (bit
->value
, bit
->last
,
958 /* Try to find a value by looking
959 through this and previous tables */
963 t
->parent
!= NULL
; t
= t
->parent
)
965 if (t
->parent
->opcode
->word_nr
==
966 condition
->field
->word_nr
967 && t
->parent
->opcode
->first
<=
968 condition
->field
->first
969 && t
->parent
->opcode
->last
>=
970 condition
->field
->last
)
972 /* the table entry fully
973 specified the condition
975 /* extract the field's value
978 sub_val (t
->opcode_nr
,
979 t
->parent
->opcode
->last
,
980 condition
->field
->first
,
981 condition
->field
->last
);
982 /* this is a requirement of
984 referring to another field */
985 ASSERT ((condition
->field
->first
-
986 condition
->field
->last
) ==
987 (first_pos
- last_pos
));
989 ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
991 t
->parent
->opcode
->last
,
992 condition
->field
->first
,
993 condition
->field
->last
);
997 if (bit
== NULL
&& t
== NULL
)
998 error (instruction
->line
,
999 "Conditional `%s' of field `%s' isn't expanded\n",
1000 condition
->string
, field
->val_string
);
1001 switch (condition
->test
)
1003 case insn_field_cond_ne
:
1007 case insn_field_cond_eq
:
1018 /* Only add additional hardwired bit
1019 information if the entry is not going to
1020 later be combined */
1021 if (table
->opcode_rule
->with_combine
)
1023 gen_entry_expand_opcode (table
, instruction
,
1025 ((opcode_nr
<< width
) |
1030 opcode_bits
*new_bits
=
1031 new_opcode_bits (bits
, val
,
1032 first_pos
, last_pos
,
1035 gen_entry_expand_opcode (table
, instruction
,
1037 ((opcode_nr
<< width
) |
1049 gen_entry_insert_expanding (gen_entry
*table
, insn_entry
* instruction
)
1051 gen_entry_expand_opcode (table
,
1053 table
->opcode
->first
, 0, table
->expanded_bits
);
1058 insns_match_format_names (insn_list
*insns
, filter
*format_names
)
1060 if (format_names
!= NULL
)
1063 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1065 if (i
->insn
->format_name
!= NULL
1066 && !filter_is_member (format_names
, i
->insn
->format_name
))
1074 table_matches_path (gen_entry
*table
, decode_path_list
*paths
)
1078 while (paths
!= NULL
)
1080 gen_entry
*entry
= table
;
1081 decode_path
*path
= paths
->path
;
1084 if (entry
== NULL
&& path
== NULL
)
1086 if (entry
== NULL
|| path
== NULL
)
1088 if (entry
->opcode_nr
!= path
->opcode_nr
)
1090 entry
= entry
->parent
;
1091 path
= path
->parent
;
1093 paths
= paths
->next
;
1100 insns_match_conditions (insn_list
*insns
, decode_cond
*conditions
)
1102 if (conditions
!= NULL
)
1105 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1108 for (cond
= conditions
; cond
!= NULL
; cond
= cond
->next
)
1111 if (i
->insn
->nr_words
<= cond
->word_nr
)
1113 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
1115 if (!cond
->mask
[bit_nr
])
1117 if (!i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->mask
)
1119 if ((i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->value
1120 == cond
->value
[bit_nr
]) == !cond
->is_equal
)
1130 insns_match_nr_words (const insn_list
*insns
, int nr_words
)
1133 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1135 if (i
->insn
->nr_words
< nr_words
)
1142 insn_list_cmp (const insn_list
*l
, const insn_list
*r
)
1146 const insn_entry
*insn
;
1147 if (l
== NULL
&& r
== NULL
)
1153 if (l
->insn
!= r
->insn
)
1154 return -1; /* somewhat arbitrary at present */
1155 /* skip this insn */
1157 while (l
!= NULL
&& l
->insn
== insn
)
1159 while (r
!= NULL
&& r
->insn
== insn
)
1167 gen_entry_expand_insns (gen_entry
*table
)
1169 const decode_table
*opcode_rule
;
1171 ASSERT (table
->nr_insns
>= 1);
1173 /* determine a valid opcode */
1174 for (opcode_rule
= table
->opcode_rule
;
1175 opcode_rule
!= NULL
; opcode_rule
= opcode_rule
->next
)
1177 char *discard_reason
;
1178 if (table
->top
->model
!= NULL
1179 && opcode_rule
->model_names
!= NULL
1180 && !filter_is_member (opcode_rule
->model_names
,
1181 table
->top
->model
->name
))
1183 /* the rule isn't applicable to this processor */
1184 discard_reason
= "wrong model";
1186 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1188 /* for safety, require a pre-codition when attempting to
1189 apply a rule to a single instruction */
1190 discard_reason
= "need pre-condition when nr-insn == 1";
1192 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1194 /* Little point in expanding a single instruction when we're
1195 not duplicating the semantic functions that this table
1197 discard_reason
= "need duplication with nr-insns == 1";
1200 if (!insns_match_format_names
1201 (table
->insns
, opcode_rule
->format_names
))
1203 discard_reason
= "wrong format name";
1205 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1207 discard_reason
= "wrong nr words";
1209 else if (!table_matches_path (table
, opcode_rule
->paths
))
1211 discard_reason
= "path failed";
1214 if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1216 discard_reason
= "condition failed";
1220 discard_reason
= "no opcode field";
1221 table
->opcode
= gen_entry_find_opcode_field (table
->insns
,
1223 table
->nr_insns
== 1 /*string-only */
1225 if (table
->opcode
!= NULL
)
1227 table
->opcode_rule
= opcode_rule
;
1232 if (options
.trace
.rule_rejection
)
1234 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1235 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1239 /* did we find anything */
1240 if (opcode_rule
== NULL
)
1242 /* the decode table failed, this set of instructions haven't
1243 been uniquely identified */
1244 if (table
->nr_insns
> 1)
1246 print_gen_entry_insns (table
, warning
,
1247 "was not uniquely decoded",
1248 "decodes to the same entry");
1249 error (NULL
, "unrecoverable\n");
1254 /* Determine the number of words that must have been prefetched for
1255 this table to function */
1256 if (table
->parent
== NULL
)
1257 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1258 else if (table
->opcode_rule
->word_nr
+ 1 >
1259 table
->parent
->nr_prefetched_words
)
1260 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1262 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1264 /* back link what we found to its parent */
1265 if (table
->parent
!= NULL
)
1267 ASSERT (table
->parent
->opcode
!= NULL
);
1268 table
->opcode
->parent
= table
->parent
->opcode
;
1271 /* report the rule being used to expand the instructions */
1272 if (options
.trace
.rule_selection
)
1274 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1276 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1277 table
->opcode
->word_nr
,
1278 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1279 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1280 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1281 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1282 table
->opcode
->nr_opcodes
, table
->nr_entries
);
1285 /* expand the raw instructions according to the opcode */
1288 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1290 if (options
.trace
.insn_expansion
)
1292 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1293 notify (NULL
, ": expand - %s.%s\n",
1294 entry
->insn
->format_name
, entry
->insn
->name
);
1296 gen_entry_insert_expanding (table
, entry
->insn
);
1300 /* dump the results */
1301 if (options
.trace
.entries
)
1304 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1307 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1308 notify (NULL
, ": %d - entries %d -",
1309 entry
->opcode_nr
, entry
->nr_insns
);
1310 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1311 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1312 notify (NULL
, "\n");
1316 /* perform a combine pass if needed */
1317 if (table
->opcode_rule
->with_combine
)
1320 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1322 if (entry
->combined_parent
== NULL
)
1324 gen_entry
**last
= &entry
->combined_next
;
1326 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1328 if (alt
->combined_parent
== NULL
1329 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1331 alt
->combined_parent
= entry
;
1333 last
= &alt
->combined_next
;
1338 if (options
.trace
.combine
)
1342 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1344 if (entry
->combined_parent
== NULL
)
1347 gen_entry
*duplicate
;
1349 print_gen_entry_path (table
->opcode_rule
->line
, entry
,
1351 for (duplicate
= entry
->combined_next
; duplicate
!= NULL
;
1352 duplicate
= duplicate
->combined_next
)
1354 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1356 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1357 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1359 notify (NULL
, " %s.%s",
1360 l
->insn
->format_name
, l
->insn
->name
);
1362 notify (NULL
, "\n");
1365 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1367 ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1368 table
->opcode
->word_nr
, i2target (options
.hi_bit_nr
,
1369 table
->opcode
->first
),
1370 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1371 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1372 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1373 table
->opcode
->nr_opcodes
, table
->nr_entries
, nr_unique
);
1377 /* Check that the rule did more than re-arange the order of the
1381 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1383 if (entry
->combined_parent
== NULL
)
1385 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1387 print_gen_entry_path (table
->opcode_rule
->line
, table
,
1390 ": Applying rule just copied all instructions\n");
1391 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1392 error (NULL
, "unrecoverable\n");
1398 /* if some form of expanded table, fill in the missing dots */
1399 switch (table
->opcode_rule
->gen
)
1401 case padded_switch_gen
:
1403 case goto_switch_gen
:
1404 if (!table
->opcode
->is_boolean
)
1406 gen_entry
**entry
= &table
->entries
;
1407 gen_entry
*illegals
= NULL
;
1408 gen_entry
**last_illegal
= &illegals
;
1410 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1412 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1414 /* missing - insert it under our feet at *entry */
1415 gen_entry_insert_insn (table
, table
->top
->isa
->illegal_insn
, table
->opcode
->word_nr
, 0, /* nr_prefetched_words == 0 for invalid */
1417 ASSERT ((*entry
) != NULL
);
1418 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1419 (*last_illegal
) = *entry
;
1420 (*last_illegal
)->combined_parent
= illegals
;
1421 last_illegal
= &(*last_illegal
)->combined_next
;
1423 entry
= &(*entry
)->sibling
;
1426 /* oops, will have pointed the first illegal insn back to
1427 its self. Fix this */
1428 if (illegals
!= NULL
)
1429 illegals
->combined_parent
= NULL
;
1438 /* and do the same for the newly created sub entries but *only*
1439 expand entries that haven't been combined. */
1442 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1444 if (entry
->combined_parent
== NULL
)
1446 gen_entry_expand_insns (entry
);
1453 gen_tables_expand_insns (gen_table
*gen
)
1456 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1458 gen_entry_expand_insns (entry
->table
);
1463 /* create a list of all the semantic functions that need to be
1464 generated. Eliminate any duplicates. Verify that the decode stage
1468 make_gen_semantics_list (lf
*file
, const gen_entry
*entry
, int depth
, void *data
)
1470 gen_table
*gen
= (gen_table
*) data
;
1472 /* Not interested in an entrie that have been combined into some
1473 other entry at the same level */
1474 if (entry
->combined_parent
!= NULL
)
1477 /* a leaf should contain exactly one instruction. If not the decode
1479 ASSERT (entry
->nr_insns
== 1);
1481 /* Enter this instruction into the list of semantic functions. */
1482 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1484 entry
->expanded_bits
,
1485 entry
->parent
->opcode
,
1486 entry
->insns
->nr_prefetched_words
,
1487 merge_duplicate_insns
);
1488 /* point the table entry at the real semantic function */
1489 ASSERT (insn
!= NULL
);
1490 entry
->insns
->semantic
= insn
;
1495 gen_tables_expand_semantics (gen_table
*gen
)
1498 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1500 gen_entry_traverse_tree (NULL
, entry
->table
, 1, /* depth */
1501 NULL
, /* start-handler */
1502 make_gen_semantics_list
, /* leaf-handler */
1503 NULL
, /* end-handler */
1514 dump_opcode_field (lf
*file
,
1516 opcode_field
*field
, char *suffix
, int levels
)
1518 lf_printf (file
, "%s(opcode_field *) %p", prefix
, field
);
1519 if (levels
&& field
!= NULL
)
1521 lf_indent (file
, +1);
1522 lf_printf (file
, "\n(first %d)", field
->first
);
1523 lf_printf (file
, "\n(last %d)", field
->last
);
1524 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1525 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1526 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1527 dump_opcode_field (file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1528 lf_indent (file
, -1);
1530 lf_printf (file
, "%s", suffix
);
1535 dump_opcode_bits (lf
*file
,
1536 char *prefix
, opcode_bits
*bits
, char *suffix
, int levels
)
1538 lf_printf (file
, "%s(opcode_bits *) %p", prefix
, bits
);
1540 if (levels
&& bits
!= NULL
)
1542 lf_indent (file
, +1);
1543 lf_printf (file
, "\n(value %d)", bits
->value
);
1544 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1545 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1546 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1547 lf_indent (file
, -1);
1549 lf_printf (file
, "%s", suffix
);
1555 dump_insn_list (lf
*file
, char *prefix
, insn_list
*entry
, char *suffix
)
1557 lf_printf (file
, "%s(insn_list *) %p", prefix
, entry
);
1561 lf_indent (file
, +1);
1562 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1563 lf_printf (file
, "\n(next %p)", entry
->next
);
1564 lf_indent (file
, -1);
1566 lf_printf (file
, "%s", suffix
);
1571 dump_insn_word_entry_list_entries (lf
*file
,
1573 insn_list
*entry
, char *suffix
)
1575 lf_printf (file
, "%s", prefix
);
1576 while (entry
!= NULL
)
1578 dump_insn_list (file
, "\n(", entry
, ")");
1579 entry
= entry
->next
;
1581 lf_printf (file
, "%s", suffix
);
1586 dump_gen_entry (lf
*file
,
1587 char *prefix
, gen_entry
*table
, char *suffix
, int levels
)
1590 lf_printf (file
, "%s(gen_entry *) %p", prefix
, table
);
1592 if (levels
&& table
!=NULL
)
1595 lf_indent (file
, +1);
1596 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1597 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1598 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")",
1600 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1601 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
,
1603 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1604 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1605 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1606 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")",
1608 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1609 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1610 lf_indent (file
, -1);
1612 lf_printf (file
, "%s", suffix
);
1616 dump_gen_list (lf
*file
,
1617 char *prefix
, gen_list
*entry
, char *suffix
, int levels
)
1619 while (entry
!= NULL
)
1621 lf_printf (file
, "%s(gen_list *) %p", prefix
, entry
);
1622 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1623 lf_printf (file
, "\n(next (gen_list *) %p)", entry
->next
);
1624 lf_printf (file
, "%s", suffix
);
1630 dump_gen_table (lf
*file
,
1631 char *prefix
, gen_table
*gen
, char *suffix
, int levels
)
1633 lf_printf (file
, "%s(gen_table *) %p", prefix
, gen
);
1634 lf_printf (file
, "\n(isa (insn_table *) %p)", gen
->isa
);
1635 lf_printf (file
, "\n(rules (decode_table *) %p)", gen
->rules
);
1636 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1637 lf_printf (file
, "%s", suffix
);
1641 igen_options options
;
1644 main (int argc
, char **argv
)
1646 decode_table
*decode_rules
;
1647 insn_table
*instructions
;
1653 "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1657 filter_parse (&options
.flags_filter
, argv
[1]);
1659 options
.hi_bit_nr
= a2i (argv
[2]);
1660 options
.insn_bit_size
= a2i (argv
[3]);
1661 options
.insn_specifying_widths
= a2i (argv
[4]);
1662 ASSERT (options
.hi_bit_nr
< options
.insn_bit_size
);
1664 instructions
= load_insn_table (argv
[6], NULL
);
1665 decode_rules
= load_decode_table (argv
[5]);
1666 gen
= make_gen_tables (instructions
, decode_rules
);
1668 gen_tables_expand_insns (gen
);
1670 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1672 dump_gen_table (l
, "(", gen
, ")\n", -1);