1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002, 2007, 2008, 2009, 2010, 2011 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. */
157 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
158 return +1; /* left = only */
159 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
160 return -1; /* right = only */
161 /* FIXME: Need to some what arbitrarily order conditional lists */
171 insn_word_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
176 if (l
== NULL
&& r
== NULL
)
177 return 0; /* all previous fields the same */
179 return -1; /* left shorter than right */
181 return +1; /* left longer than right */
182 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
184 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
186 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
188 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
190 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
200 opcode_bit_cmp (opcode_bits
*l
, opcode_bits
*r
)
202 if (l
== NULL
&& r
== NULL
)
203 return 0; /* all previous bits the same */
205 return -1; /* left shorter than right */
207 return +1; /* left longer than right */
208 /* most significant word */
209 if (l
->field
->word_nr
< r
->field
->word_nr
)
210 return +1; /* left has more significant word */
211 if (l
->field
->word_nr
> r
->field
->word_nr
)
212 return -1; /* right has more significant word */
213 /* most significant bit? */
214 if (l
->first
< r
->first
)
215 return +1; /* left as more significant bit */
216 if (l
->first
> r
->first
)
217 return -1; /* right as more significant bit */
219 if (l
->last
< r
->last
)
220 return +1; /* left as less bits */
221 if (l
->last
> r
->last
)
222 return -1; /* right as less bits */
224 if (l
->value
< r
->value
)
226 if (l
->value
> r
->value
)
234 opcode_bits_cmp (opcode_bits
*l
, opcode_bits
*r
)
239 if (l
== NULL
&& r
== NULL
)
240 return 0; /* all previous bits the same */
241 cmp
= opcode_bit_cmp (l
, r
);
251 new_opcode_bits (opcode_bits
*old_bits
,
254 int last
, insn_field_entry
*field
, opcode_field
*opcode
)
256 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
257 new_bits
->field
= field
;
258 new_bits
->value
= value
;
259 new_bits
->first
= first
;
260 new_bits
->last
= last
;
261 new_bits
->opcode
= opcode
;
263 if (old_bits
!= NULL
)
265 opcode_bits
*new_list
;
266 opcode_bits
**last
= &new_list
;
267 new_list
= new_opcode_bits (old_bits
->next
,
271 old_bits
->field
, old_bits
->opcode
);
272 while (*last
!= NULL
)
274 int cmp
= opcode_bit_cmp (new_bits
, *last
);
275 if (cmp
< 0) /* new < new_list */
281 ERROR ("Duplicated insn bits in list");
283 last
= &(*last
)->next
;
285 new_bits
->next
= *last
;
295 /* Same as strcmp(). */
297 name_cmp (const char *l
, const char *r
)
299 if (l
== NULL
&& r
== NULL
)
301 if (l
!= NULL
&& r
== NULL
)
303 if (l
== NULL
&& r
!= NULL
)
305 return strcmp (l
, r
);
311 merge_duplicate_insns
,
312 report_duplicate_insns
,
314 duplicate_insn_actions
;
317 insn_list_insert (insn_list
**cur_insn_ptr
,
320 opcode_bits
*expanded_bits
,
321 opcode_field
*opcodes
,
322 int nr_prefetched_words
,
323 duplicate_insn_actions duplicate_action
)
325 /* insert it according to the order of the fields & bits */
326 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
330 /* key#1 sort according to the constant fields of each instruction */
331 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
337 /* key#2 sort according to the expanded bits of each instruction */
338 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
344 /* key#3 sort according to the non-constant fields of each instruction */
345 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
351 if (duplicate_action
== merge_duplicate_insns
)
353 /* key#4: If we're going to merge duplicates, also sort
354 according to the format_name. Two instructions with
355 identical decode patterns, but different names, are
356 considered different when merging. Duplicates are only
357 important when creating a decode table (implied by
358 report_duplicate_insns) as such a table only has the
359 instruction's bit code as a way of differentiating
360 between instructions. */
361 int cmp
= name_cmp (insn
->format_name
,
362 (*cur_insn_ptr
)->insn
->format_name
);
369 if (duplicate_action
== merge_duplicate_insns
)
371 /* key#5: If we're going to merge duplicates, also sort
372 according to the name. See comment above for
374 int cmp
= name_cmp (insn
->name
, (*cur_insn_ptr
)->insn
->name
);
381 /* duplicate keys, report problem */
382 switch (duplicate_action
)
384 case report_duplicate_insns
:
385 /* It would appear that we have two instructions with the
386 same constant field values across all words and bits.
387 This error can also occure when insn_field_cmp() is
388 failing to differentiate between two instructions that
389 differ only in their conditional fields. */
391 "Two instructions with identical constant fields\n");
392 error ((*cur_insn_ptr
)->insn
->line
,
393 "Location of duplicate instruction\n");
394 case merge_duplicate_insns
:
395 /* Add the opcode path to the instructions list */
396 if (options
.trace
.insn_insertion
)
398 notify ((*cur_insn_ptr
)->insn
->line
,
399 "%s.%s: insert merge %s.%s\n",
400 (*cur_insn_ptr
)->insn
->format_name
,
401 (*cur_insn_ptr
)->insn
->name
,
407 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
408 while (*last
!= NULL
)
410 last
= &(*last
)->next
;
412 (*last
) = ZALLOC (insn_opcodes
);
413 (*last
)->opcode
= opcodes
;
415 /* Use the larger nr_prefetched_words */
416 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
417 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
418 return (*cur_insn_ptr
);
423 /* create a new list entry and insert it */
425 insn_list
*new_insn
= ZALLOC (insn_list
);
426 if (options
.trace
.insn_insertion
)
429 "%s.%s: insert new\n",
433 new_insn
->insn
= insn
;
434 new_insn
->expanded_bits
= expanded_bits
;
435 new_insn
->next
= (*cur_insn_ptr
);
436 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
439 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
440 new_insn
->opcodes
->opcode
= opcodes
;
442 (*cur_insn_ptr
) = new_insn
;
447 return (*cur_insn_ptr
);
452 gen_entry_traverse_tree (lf
*file
,
455 gen_entry_handler
* start
,
456 gen_entry_handler
* leaf
,
457 gen_entry_handler
* end
, void *data
)
461 ASSERT (table
!=NULL
);
462 ASSERT (table
->opcode
!= NULL
);
463 ASSERT (table
->nr_entries
> 0);
464 ASSERT (table
->entries
!= 0);
467 if (start
!= NULL
&& depth
>= 0)
469 start (file
, table
, depth
, data
);
472 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
474 if (entry
->entries
!= NULL
&& depth
!= 0)
476 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
477 start
, leaf
, end
, data
);
483 leaf (file
, entry
, depth
, data
);
488 if (end
!= NULL
&& depth
>= 0)
490 end (file
, table
, depth
, data
);
496 /* create a list element containing a single gen_table entry */
499 make_table (insn_table
*isa
, decode_table
*rules
, model_entry
*model
)
502 gen_list
*entry
= ZALLOC (gen_list
);
503 entry
->table
= ZALLOC (gen_entry
);
504 entry
->table
->top
= entry
;
505 entry
->model
= model
;
507 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
510 || insn
->processors
== NULL
511 || filter_is_member (insn
->processors
, model
->name
))
513 insn_list_insert (&entry
->table
->insns
, &entry
->table
->nr_insns
, insn
, NULL
, /* expanded_bits - none yet */
514 NULL
, /* opcodes - none yet */
515 0, /* nr_prefetched_words - none yet */
516 report_duplicate_insns
);
519 entry
->table
->opcode_rule
= rules
;
525 make_gen_tables (insn_table
*isa
, decode_table
*rules
)
527 gen_table
*gen
= ZALLOC (gen_table
);
530 if (options
.gen
.multi_sim
)
532 gen_list
**last
= &gen
->tables
;
535 if (options
.model_filter
!= NULL
)
536 processors
= options
.model_filter
;
538 processors
= isa
->model
->processors
;
539 for (model
= isa
->model
->models
; model
!= NULL
; model
= model
->next
)
541 if (filter_is_member (processors
, model
->name
))
543 *last
= make_table (isa
, rules
, model
);
544 last
= &(*last
)->next
;
550 gen
->tables
= make_table (isa
, rules
, NULL
);
556 /****************************************************************/
561 field_is_not_constant
= 0,
562 field_constant_int
= 1,
563 field_constant_reserved
= 2,
564 field_constant_string
= 3
566 constant_field_types
;
568 static constant_field_types
569 insn_field_is_constant (insn_field
* field
, decode_table
*rule
)
574 /* field is an integer */
575 return field_constant_int
;
576 case insn_field_reserved
:
577 /* field is `/' and treating that as a constant */
578 if (rule
->with_zero_reserved
)
579 return field_constant_reserved
;
581 return field_is_not_constant
;
582 case insn_field_wild
:
583 return field_is_not_constant
; /* never constant */
584 case insn_field_string
:
585 /* field, though variable, is on the list of forced constants */
586 if (filter_is_member (rule
->constant_field_names
, field
->val_string
))
587 return field_constant_string
;
589 return field_is_not_constant
;
591 ERROR ("Internal error");
592 return field_is_not_constant
;
597 /****************************************************************/
600 /* Is the bit, according to the decode rule, identical across all the
603 insns_bit_useless (insn_list
*insns
, decode_table
*rule
, int bit_nr
)
607 int is_useless
= 1; /* cleared if something actually found */
609 /* check the instructions for some constant value in at least one of
611 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
613 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
614 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
615 switch (bit
->field
->type
)
617 case insn_field_invalid
:
620 case insn_field_wild
:
621 case insn_field_reserved
:
622 /* neither useless or useful - ignore */
625 switch (rule
->search
)
627 case decode_find_strings
:
628 /* an integer isn't a string */
630 case decode_find_constants
:
631 case decode_find_mixed
:
632 /* an integer is useful if its value isn't the same
633 between all instructions. The first time through the
634 value is saved, the second time through (if the
635 values differ) it is marked as useful. */
638 else if (value
!= bit
->value
)
643 case insn_field_string
:
644 switch (rule
->search
)
646 case decode_find_strings
:
647 /* at least one string, keep checking */
650 case decode_find_constants
:
651 case decode_find_mixed
:
652 if (filter_is_member (rule
->constant_field_names
,
653 bit
->field
->val_string
))
654 /* a string field forced to constant? */
656 else if (rule
->search
== decode_find_constants
)
657 /* the string field isn't constant */
664 /* Given only one constant value has been found, check through all
665 the instructions to see if at least one conditional makes it
667 if (value
>= 0 && is_useless
)
669 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
671 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
672 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
673 switch (bit
->field
->type
)
675 case insn_field_invalid
:
678 case insn_field_wild
:
679 case insn_field_reserved
:
681 /* already processed */
683 case insn_field_string
:
684 switch (rule
->search
)
686 case decode_find_strings
:
687 case decode_find_constants
:
688 /* already processed */
690 case decode_find_mixed
:
691 /* string field with conditions. If this condition
692 eliminates the value then the compare is useful */
693 if (bit
->field
->conditions
!= NULL
)
695 insn_field_cond
*condition
;
696 int shift
= bit
->field
->last
- bit_nr
;
697 for (condition
= bit
->field
->conditions
;
698 condition
!= NULL
; condition
= condition
->next
)
700 switch (condition
->type
)
702 case insn_field_cond_value
:
703 switch (condition
->test
)
705 case insn_field_cond_ne
:
706 if (((condition
->value
>> shift
) & 1)
708 /* conditional field excludes the
712 case insn_field_cond_eq
:
713 if (((condition
->value
>> shift
) & 1)
715 /* conditional field requires the
721 case insn_field_cond_field
:
722 /* are these handled separatly? */
736 /* go through a gen-table's list of instruction formats looking for a
737 range of bits that meet the decode table RULEs requirements */
739 static opcode_field
*
740 gen_entry_find_opcode_field (insn_list
*insns
,
741 decode_table
*rule
, int string_only
)
743 opcode_field curr_opcode
;
744 ASSERT (rule
!= NULL
);
746 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
747 curr_opcode
.word_nr
= rule
->word_nr
;
748 curr_opcode
.first
= rule
->first
;
749 curr_opcode
.last
= rule
->last
;
751 /* Try to reduce the size of first..last in accordance with the
754 while (curr_opcode
.first
<= rule
->last
)
756 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
761 while (curr_opcode
.last
>= rule
->first
)
763 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
771 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
773 insn_word_entry
*fields
= entry
->insn
->word
[rule
->word_nr
];
774 opcode_field new_opcode
;
776 ASSERT (fields
!= NULL
);
778 /* find a start point for the opcode field */
779 new_opcode
.first
= rule
->first
;
780 while (new_opcode
.first
<= rule
->last
783 (insn_field_is_constant (fields
->bit
[new_opcode
.first
], rule
)
784 != field_constant_string
)) && (string_only
786 (insn_field_is_constant
788 bit
[new_opcode
.first
],
790 field_is_not_constant
)))
792 int new_first
= fields
->bit
[new_opcode
.first
]->last
+ 1;
793 ASSERT (new_first
> new_opcode
.first
);
794 new_opcode
.first
= new_first
;
796 ASSERT (new_opcode
.first
> rule
->last
798 && insn_field_is_constant (fields
->bit
[new_opcode
.first
],
799 rule
) == field_constant_string
)
801 && insn_field_is_constant (fields
->bit
[new_opcode
.first
],
804 /* find the end point for the opcode field */
805 new_opcode
.last
= rule
->last
;
806 while (new_opcode
.last
>= rule
->first
808 || insn_field_is_constant (fields
->bit
[new_opcode
.last
],
809 rule
) != field_constant_string
)
811 || !insn_field_is_constant (fields
->bit
[new_opcode
.last
],
814 int new_last
= fields
->bit
[new_opcode
.last
]->first
- 1;
815 ASSERT (new_last
< new_opcode
.last
);
816 new_opcode
.last
= new_last
;
818 ASSERT (new_opcode
.last
< rule
->first
820 && insn_field_is_constant (fields
->bit
[new_opcode
.last
],
821 rule
) == field_constant_string
)
823 && insn_field_is_constant (fields
->bit
[new_opcode
.last
],
826 /* now see if our current opcode needs expanding to include the
827 interesting fields within this instruction */
828 if (new_opcode
.first
<= rule
->last
829 && curr_opcode
.first
> new_opcode
.first
)
830 curr_opcode
.first
= new_opcode
.first
;
831 if (new_opcode
.last
>= rule
->first
832 && curr_opcode
.last
< new_opcode
.last
)
833 curr_opcode
.last
= new_opcode
.last
;
838 /* did the final opcode field end up being empty? */
839 if (curr_opcode
.first
> curr_opcode
.last
)
843 ASSERT (curr_opcode
.last
>= rule
->first
);
844 ASSERT (curr_opcode
.first
<= rule
->last
);
845 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
847 /* Ensure that, for the non string only case, the opcode includes
848 the range forced_first .. forced_last */
849 if (!string_only
&& curr_opcode
.first
> rule
->force_first
)
851 curr_opcode
.first
= rule
->force_first
;
853 if (!string_only
&& curr_opcode
.last
< rule
->force_last
)
855 curr_opcode
.last
= rule
->force_last
;
858 /* For the string only case, force just the lower bound (so that the
859 shift can be eliminated) */
860 if (string_only
&& rule
->force_last
== options
.insn_bit_size
- 1)
862 curr_opcode
.last
= options
.insn_bit_size
- 1;
865 /* handle any special cases */
868 case normal_decode_rule
:
869 /* let the above apply */
870 curr_opcode
.nr_opcodes
=
871 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
874 curr_opcode
.is_boolean
= 1;
875 curr_opcode
.boolean_constant
= rule
->constant
;
876 curr_opcode
.nr_opcodes
= 2;
881 opcode_field
*new_field
= ZALLOC (opcode_field
);
882 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
889 gen_entry_insert_insn (gen_entry
*table
,
890 insn_entry
* old_insn
,
892 int new_nr_prefetched_words
,
893 int new_opcode_nr
, opcode_bits
*new_bits
)
895 gen_entry
**entry
= &table
->entries
;
897 /* find the new table for this entry */
898 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
900 entry
= &(*entry
)->sibling
;
903 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
905 /* insert the missing entry */
906 gen_entry
*new_entry
= ZALLOC (gen_entry
);
907 new_entry
->sibling
= (*entry
);
908 (*entry
) = new_entry
;
911 new_entry
->top
= table
->top
;
912 new_entry
->opcode_nr
= new_opcode_nr
;
913 new_entry
->word_nr
= new_word_nr
;
914 new_entry
->expanded_bits
= new_bits
;
915 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
916 new_entry
->parent
= table
;
917 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
919 /* ASSERT new_bits == cur_entry bits */
920 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
921 insn_list_insert (&(*entry
)->insns
, &(*entry
)->nr_insns
, old_insn
, NULL
, /* expanded_bits - only in final list */
922 NULL
, /* opcodes - only in final list */
923 new_nr_prefetched_words
, /* for this table */
924 report_duplicate_insns
);
929 gen_entry_expand_opcode (gen_entry
*table
,
930 insn_entry
* instruction
,
931 int bit_nr
, int opcode_nr
, opcode_bits
*bits
)
933 if (bit_nr
> table
->opcode
->last
)
935 /* Only include the hardwired bit information with an entry IF
936 that entry (and hence its functions) are being duplicated. */
937 if (options
.trace
.insn_expansion
)
939 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
940 notify (NULL
, ": insert %d - %s.%s%s\n",
942 instruction
->format_name
,
944 (table
->opcode_rule
->
945 with_duplicates
? " (duplicated)" : ""));
947 if (table
->opcode_rule
->with_duplicates
)
949 gen_entry_insert_insn (table
, instruction
,
950 table
->opcode
->word_nr
,
951 table
->nr_prefetched_words
, opcode_nr
, bits
);
955 gen_entry_insert_insn (table
, instruction
,
956 table
->opcode
->word_nr
,
957 table
->nr_prefetched_words
, opcode_nr
, NULL
);
962 insn_word_entry
*word
= instruction
->word
[table
->opcode
->word_nr
];
963 insn_field_entry
*field
= word
->bit
[bit_nr
]->field
;
964 int last_pos
= ((field
->last
< table
->opcode
->last
)
965 ? field
->last
: table
->opcode
->last
);
966 int first_pos
= ((field
->first
> table
->opcode
->first
)
967 ? field
->first
: table
->opcode
->first
);
968 int width
= last_pos
- first_pos
+ 1;
974 val
= sub_val (field
->val_int
, field
->last
, first_pos
, last_pos
);
975 gen_entry_expand_opcode (table
, instruction
,
977 ((opcode_nr
<< width
) | val
), bits
);
982 if (field
->type
== insn_field_reserved
)
983 gen_entry_expand_opcode (table
, instruction
,
985 ((opcode_nr
<< width
)), bits
);
989 int last_val
= (table
->opcode
->is_boolean
? 2 : (1 << width
));
990 for (val
= 0; val
< last_val
; val
++)
992 /* check to see if the value has been precluded
993 (by a conditional) in some way */
995 insn_field_cond
*condition
;
996 for (condition
= field
->conditions
, is_precluded
= 0;
997 condition
!= NULL
&& !is_precluded
;
998 condition
= condition
->next
)
1000 switch (condition
->type
)
1002 case insn_field_cond_value
:
1005 sub_val (condition
->value
, field
->last
,
1006 first_pos
, last_pos
);
1007 switch (condition
->test
)
1009 case insn_field_cond_ne
:
1013 case insn_field_cond_eq
:
1020 case insn_field_cond_field
:
1024 gen_entry
*t
= NULL
;
1025 /* Try to find a value for the
1026 conditional by looking back through
1027 the previously defined bits for one
1028 that covers the designated
1029 conditional field */
1030 for (bit
= bits
; bit
!= NULL
; bit
= bit
->next
)
1032 if (bit
->field
->word_nr
==
1033 condition
->field
->word_nr
1034 && bit
->first
<= condition
->field
->first
1035 && bit
->last
>= condition
->field
->last
)
1037 /* the bit field fully specified
1038 the conditional field's value */
1039 value
= sub_val (bit
->value
, bit
->last
,
1046 /* Try to find a value by looking
1047 through this and previous tables */
1051 t
->parent
!= NULL
; t
= t
->parent
)
1053 if (t
->parent
->opcode
->word_nr
==
1054 condition
->field
->word_nr
1055 && t
->parent
->opcode
->first
<=
1056 condition
->field
->first
1057 && t
->parent
->opcode
->last
>=
1058 condition
->field
->last
)
1060 /* the table entry fully
1061 specified the condition
1063 /* extract the field's value
1066 sub_val (t
->opcode_nr
,
1067 t
->parent
->opcode
->last
,
1068 condition
->field
->first
,
1069 condition
->field
->last
);
1070 /* this is a requirement of
1072 refering to another field */
1073 ASSERT ((condition
->field
->first
-
1074 condition
->field
->last
) ==
1075 (first_pos
- last_pos
));
1077 ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
1078 value
, t
->opcode_nr
,
1079 t
->parent
->opcode
->last
,
1080 condition
->field
->first
,
1081 condition
->field
->last
);
1085 if (bit
== NULL
&& t
== NULL
)
1086 error (instruction
->line
,
1087 "Conditional `%s' of field `%s' isn't expanded",
1088 condition
->string
, field
->val_string
);
1089 switch (condition
->test
)
1091 case insn_field_cond_ne
:
1095 case insn_field_cond_eq
:
1106 /* Only add additional hardwired bit
1107 information if the entry is not going to
1108 later be combined */
1109 if (table
->opcode_rule
->with_combine
)
1111 gen_entry_expand_opcode (table
, instruction
,
1113 ((opcode_nr
<< width
) |
1118 opcode_bits
*new_bits
=
1119 new_opcode_bits (bits
, val
,
1120 first_pos
, last_pos
,
1123 gen_entry_expand_opcode (table
, instruction
,
1125 ((opcode_nr
<< width
) |
1137 gen_entry_insert_expanding (gen_entry
*table
, insn_entry
* instruction
)
1139 gen_entry_expand_opcode (table
,
1141 table
->opcode
->first
, 0, table
->expanded_bits
);
1146 insns_match_format_names (insn_list
*insns
, filter
*format_names
)
1148 if (format_names
!= NULL
)
1151 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1153 if (i
->insn
->format_name
!= NULL
1154 && !filter_is_member (format_names
, i
->insn
->format_name
))
1162 table_matches_path (gen_entry
*table
, decode_path_list
*paths
)
1166 while (paths
!= NULL
)
1168 gen_entry
*entry
= table
;
1169 decode_path
*path
= paths
->path
;
1172 if (entry
== NULL
&& path
== NULL
)
1174 if (entry
== NULL
|| path
== NULL
)
1176 if (entry
->opcode_nr
!= path
->opcode_nr
)
1178 entry
= entry
->parent
;
1179 path
= path
->parent
;
1181 paths
= paths
->next
;
1188 insns_match_conditions (insn_list
*insns
, decode_cond
*conditions
)
1190 if (conditions
!= NULL
)
1193 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1196 for (cond
= conditions
; cond
!= NULL
; cond
= cond
->next
)
1199 if (i
->insn
->nr_words
<= cond
->word_nr
)
1201 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
1203 if (!cond
->mask
[bit_nr
])
1205 if (!i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->mask
)
1207 if ((i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->value
1208 == cond
->value
[bit_nr
]) == !cond
->is_equal
)
1218 insns_match_nr_words (insn_list
*insns
, int nr_words
)
1221 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1223 if (i
->insn
->nr_words
< nr_words
)
1230 insn_list_cmp (insn_list
*l
, insn_list
*r
)
1235 if (l
== NULL
&& r
== NULL
)
1241 if (l
->insn
!= r
->insn
)
1242 return -1; /* somewhat arbitrary at present */
1243 /* skip this insn */
1245 while (l
!= NULL
&& l
->insn
== insn
)
1247 while (r
!= NULL
&& r
->insn
== insn
)
1255 gen_entry_expand_insns (gen_entry
*table
)
1257 decode_table
*opcode_rule
;
1259 ASSERT (table
->nr_insns
>= 1);
1261 /* determine a valid opcode */
1262 for (opcode_rule
= table
->opcode_rule
;
1263 opcode_rule
!= NULL
; opcode_rule
= opcode_rule
->next
)
1265 char *discard_reason
;
1266 if (table
->top
->model
!= NULL
1267 && opcode_rule
->model_names
!= NULL
1268 && !filter_is_member (opcode_rule
->model_names
,
1269 table
->top
->model
->name
))
1271 /* the rule isn't applicable to this processor */
1272 discard_reason
= "wrong model";
1274 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1276 /* for safety, require a pre-codition when attempting to
1277 apply a rule to a single instruction */
1278 discard_reason
= "need pre-condition when nr-insn == 1";
1280 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1282 /* Little point in expanding a single instruction when we're
1283 not duplicating the semantic functions that this table
1285 discard_reason
= "need duplication with nr-insns == 1";
1288 if (!insns_match_format_names
1289 (table
->insns
, opcode_rule
->format_names
))
1291 discard_reason
= "wrong format name";
1293 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1295 discard_reason
= "wrong nr words";
1297 else if (!table_matches_path (table
, opcode_rule
->paths
))
1299 discard_reason
= "path failed";
1302 if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1304 discard_reason
= "condition failed";
1308 discard_reason
= "no opcode field";
1309 table
->opcode
= gen_entry_find_opcode_field (table
->insns
,
1311 table
->nr_insns
== 1 /*string-only */
1313 if (table
->opcode
!= NULL
)
1315 table
->opcode_rule
= opcode_rule
;
1320 if (options
.trace
.rule_rejection
)
1322 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1323 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1327 /* did we find anything */
1328 if (opcode_rule
== NULL
)
1330 /* the decode table failed, this set of instructions haven't
1331 been uniquely identified */
1332 if (table
->nr_insns
> 1)
1334 print_gen_entry_insns (table
, warning
,
1335 "was not uniquely decoded",
1336 "decodes to the same entry");
1342 /* Determine the number of words that must have been prefetched for
1343 this table to function */
1344 if (table
->parent
== NULL
)
1345 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1346 else if (table
->opcode_rule
->word_nr
+ 1 >
1347 table
->parent
->nr_prefetched_words
)
1348 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1350 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1352 /* back link what we found to its parent */
1353 if (table
->parent
!= NULL
)
1355 ASSERT (table
->parent
->opcode
!= NULL
);
1356 table
->opcode
->parent
= table
->parent
->opcode
;
1359 /* report the rule being used to expand the instructions */
1360 if (options
.trace
.rule_selection
)
1362 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1364 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1365 table
->opcode
->word_nr
,
1366 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1367 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1368 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1369 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1370 table
->opcode
->nr_opcodes
, table
->nr_entries
);
1373 /* expand the raw instructions according to the opcode */
1376 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1378 if (options
.trace
.insn_expansion
)
1380 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1381 notify (NULL
, ": expand - %s.%s\n",
1382 entry
->insn
->format_name
, entry
->insn
->name
);
1384 gen_entry_insert_expanding (table
, entry
->insn
);
1388 /* dump the results */
1389 if (options
.trace
.entries
)
1392 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1395 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1396 notify (NULL
, ": %d - entries %d -",
1397 entry
->opcode_nr
, entry
->nr_insns
);
1398 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1399 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1400 notify (NULL
, "\n");
1404 /* perform a combine pass if needed */
1405 if (table
->opcode_rule
->with_combine
)
1408 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1410 if (entry
->combined_parent
== NULL
)
1412 gen_entry
**last
= &entry
->combined_next
;
1414 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1416 if (alt
->combined_parent
== NULL
1417 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1419 alt
->combined_parent
= entry
;
1421 last
= &alt
->combined_next
;
1426 if (options
.trace
.combine
)
1430 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1432 if (entry
->combined_parent
== NULL
)
1435 gen_entry
*duplicate
;
1437 print_gen_entry_path (table
->opcode_rule
->line
, entry
,
1439 for (duplicate
= entry
->combined_next
; duplicate
!= NULL
;
1440 duplicate
= duplicate
->combined_next
)
1442 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1444 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1445 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1447 notify (NULL
, " %s.%s",
1448 l
->insn
->format_name
, l
->insn
->name
);
1450 notify (NULL
, "\n");
1453 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1455 ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1456 table
->opcode
->word_nr
, i2target (options
.hi_bit_nr
,
1457 table
->opcode
->first
),
1458 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1459 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1460 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1461 table
->opcode
->nr_opcodes
, table
->nr_entries
, nr_unique
);
1465 /* Check that the rule did more than re-arange the order of the
1469 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1471 if (entry
->combined_parent
== NULL
)
1473 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1475 print_gen_entry_path (table
->opcode_rule
->line
, table
,
1478 ": Applying rule just copied all instructions\n");
1479 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1486 /* if some form of expanded table, fill in the missing dots */
1487 switch (table
->opcode_rule
->gen
)
1489 case padded_switch_gen
:
1491 case goto_switch_gen
:
1492 if (!table
->opcode
->is_boolean
)
1494 gen_entry
**entry
= &table
->entries
;
1495 gen_entry
*illegals
= NULL
;
1496 gen_entry
**last_illegal
= &illegals
;
1498 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1500 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1502 /* missing - insert it under our feet at *entry */
1503 gen_entry_insert_insn (table
, table
->top
->isa
->illegal_insn
, table
->opcode
->word_nr
, 0, /* nr_prefetched_words == 0 for invalid */
1505 ASSERT ((*entry
) != NULL
);
1506 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1507 (*last_illegal
) = *entry
;
1508 (*last_illegal
)->combined_parent
= illegals
;
1509 last_illegal
= &(*last_illegal
)->combined_next
;
1511 entry
= &(*entry
)->sibling
;
1514 /* oops, will have pointed the first illegal insn back to
1515 its self. Fix this */
1516 if (illegals
!= NULL
)
1517 illegals
->combined_parent
= NULL
;
1526 /* and do the same for the newly created sub entries but *only*
1527 expand entries that haven't been combined. */
1530 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1532 if (entry
->combined_parent
== NULL
)
1534 gen_entry_expand_insns (entry
);
1541 gen_tables_expand_insns (gen_table
*gen
)
1544 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1546 gen_entry_expand_insns (entry
->table
);
1551 /* create a list of all the semantic functions that need to be
1552 generated. Eliminate any duplicates. Verify that the decode stage
1556 make_gen_semantics_list (lf
*file
, gen_entry
*entry
, int depth
, void *data
)
1558 gen_table
*gen
= (gen_table
*) data
;
1560 /* Not interested in an entrie that have been combined into some
1561 other entry at the same level */
1562 if (entry
->combined_parent
!= NULL
)
1565 /* a leaf should contain exactly one instruction. If not the decode
1567 ASSERT (entry
->nr_insns
== 1);
1569 /* Enter this instruction into the list of semantic functions. */
1570 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1572 entry
->expanded_bits
,
1573 entry
->parent
->opcode
,
1574 entry
->insns
->nr_prefetched_words
,
1575 merge_duplicate_insns
);
1576 /* point the table entry at the real semantic function */
1577 ASSERT (insn
!= NULL
);
1578 entry
->insns
->semantic
= insn
;
1583 gen_tables_expand_semantics (gen_table
*gen
)
1586 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1588 gen_entry_traverse_tree (NULL
, entry
->table
, 1, /* depth */
1589 NULL
, /* start-handler */
1590 make_gen_semantics_list
, /* leaf-handler */
1591 NULL
, /* end-handler */
1602 dump_opcode_field (lf
*file
,
1604 opcode_field
*field
, char *suffix
, int levels
)
1606 lf_printf (file
, "%s(opcode_field *) 0x%lx", prefix
, (long) field
);
1607 if (levels
&& field
!= NULL
)
1609 lf_indent (file
, +1);
1610 lf_printf (file
, "\n(first %d)", field
->first
);
1611 lf_printf (file
, "\n(last %d)", field
->last
);
1612 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1613 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1614 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1615 dump_opcode_field (file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1616 lf_indent (file
, -1);
1618 lf_printf (file
, "%s", suffix
);
1623 dump_opcode_bits (lf
*file
,
1624 char *prefix
, opcode_bits
*bits
, char *suffix
, int levels
)
1626 lf_printf (file
, "%s(opcode_bits *) 0x%lx", prefix
, (long) bits
);
1628 if (levels
&& bits
!= NULL
)
1630 lf_indent (file
, +1);
1631 lf_printf (file
, "\n(value %d)", bits
->value
);
1632 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1633 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1634 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1635 lf_indent (file
, -1);
1637 lf_printf (file
, "%s", suffix
);
1643 dump_insn_list (lf
*file
, char *prefix
, insn_list
*entry
, char *suffix
)
1645 lf_printf (file
, "%s(insn_list *) 0x%lx", prefix
, (long) entry
);
1649 lf_indent (file
, +1);
1650 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1651 lf_printf (file
, "\n(next 0x%lx)", (long) entry
->next
);
1652 lf_indent (file
, -1);
1654 lf_printf (file
, "%s", suffix
);
1659 dump_insn_word_entry_list_entries (lf
*file
,
1661 insn_list
*entry
, char *suffix
)
1663 lf_printf (file
, "%s", prefix
);
1664 while (entry
!= NULL
)
1666 dump_insn_list (file
, "\n(", entry
, ")");
1667 entry
= entry
->next
;
1669 lf_printf (file
, "%s", suffix
);
1674 dump_gen_entry (lf
*file
,
1675 char *prefix
, gen_entry
*table
, char *suffix
, int levels
)
1678 lf_printf (file
, "%s(gen_entry *) 0x%lx", prefix
, (long) table
);
1680 if (levels
&& table
!=NULL
)
1683 lf_indent (file
, +1);
1684 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1685 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1686 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")",
1688 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1689 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
,
1691 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1692 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1693 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1694 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")",
1696 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1697 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1698 lf_indent (file
, -1);
1700 lf_printf (file
, "%s", suffix
);
1704 dump_gen_list (lf
*file
,
1705 char *prefix
, gen_list
*entry
, char *suffix
, int levels
)
1707 while (entry
!= NULL
)
1709 lf_printf (file
, "%s(gen_list *) 0x%lx", prefix
, (long) entry
);
1710 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1711 lf_printf (file
, "\n(next (gen_list *) 0x%lx)", (long) entry
->next
);
1712 lf_printf (file
, "%s", suffix
);
1718 dump_gen_table (lf
*file
,
1719 char *prefix
, gen_table
*gen
, char *suffix
, int levels
)
1721 lf_printf (file
, "%s(gen_table *) 0x%lx", prefix
, (long) gen
);
1722 lf_printf (file
, "\n(isa (insn_table *) 0x%lx)", (long) gen
->isa
);
1723 lf_printf (file
, "\n(rules (decode_table *) 0x%lx)", (long) gen
->rules
);
1724 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1725 lf_printf (file
, "%s", suffix
);
1729 igen_options options
;
1732 main (int argc
, char **argv
)
1734 decode_table
*decode_rules
;
1735 insn_table
*instructions
;
1741 "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1743 INIT_OPTIONS (options
);
1745 filter_parse (&options
.flags_filter
, argv
[1]);
1747 options
.hi_bit_nr
= a2i (argv
[2]);
1748 options
.insn_bit_size
= a2i (argv
[3]);
1749 options
.insn_specifying_widths
= a2i (argv
[4]);
1750 ASSERT (options
.hi_bit_nr
< options
.insn_bit_size
);
1752 instructions
= load_insn_table (argv
[6], NULL
);
1753 decode_rules
= load_decode_table (argv
[5]);
1754 gen
= make_gen_tables (instructions
, decode_rules
);
1756 gen_tables_expand_insns (gen
);
1758 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1760 dump_gen_table (l
, "(", gen
, ")\n", -1);