1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
32 #include "ld-decode.h"
36 sub_val (insn_uint val
,
41 return ((val
>> (val_last_pos
- last_pos
))
42 & (((insn_uint
)1 << (last_pos
- first_pos
+ 1)) - 1));
46 update_depth (lf
*file
,
51 int *max_depth
= (int*)data
;
52 if (*max_depth
< depth
)
58 gen_entry_depth (gen_entry
*table
)
61 gen_entry_traverse_tree (NULL
,
73 print_gen_entry_path (line_ref
*line
,
77 if (table
->parent
== NULL
)
79 if (table
->top
->model
!= NULL
)
80 print (line
, "%s", table
->top
->model
->name
);
86 print_gen_entry_path (line
, table
->parent
, print
);
87 print (NULL
, ".%d", table
->opcode_nr
);
92 print_gen_entry_insns (gen_entry
*table
,
99 message
= first_message
;
100 for (i
= table
->insns
; i
!= NULL
; i
= i
->next
)
102 insn_entry
*insn
= i
->insn
;
103 print_gen_entry_path (insn
->line
, table
, print
);
104 print (NULL
, ": %s.%s %s\n",
108 if (next_message
!= NULL
)
109 message
= next_message
;
115 insn_field_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
120 if (l
== NULL
&& r
== NULL
)
121 return 0; /* all previous fields the same */
123 return -1; /* left shorter than right */
125 return +1; /* left longer than right */
127 bit_nr
< options
.insn_bit_size
;
130 if (l
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
132 if (r
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
134 if (l
->bit
[bit_nr
]->field
->conditions
== NULL
)
136 if (r
->bit
[bit_nr
]->field
->conditions
== NULL
)
139 printf ("%s%s%s VS %s%s%s\n",
140 l
->bit
[bit_nr
]->field
->val_string
,
141 l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
? "=" : "!",
142 l
->bit
[bit_nr
]->field
->conditions
->string
,
143 r
->bit
[bit_nr
]->field
->val_string
,
144 r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
? "=" : "!",
145 r
->bit
[bit_nr
]->field
->conditions
->string
);
146 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
147 && r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
149 if (l
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
150 && r
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
151 /* somewhat arbitrary */
153 int cmp
= strcmp (l
->bit
[bit_nr
]->field
->conditions
->string
,
154 r
->bit
[bit_nr
]->field
->conditions
->string
);
160 if (l
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
162 if (r
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
164 /* The case of both fields having constant values should have
165 already have been handled because such fields are converted
166 into normal constant fields. */
169 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
170 return +1; /* left = only */
171 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
172 return -1; /* right = only */
173 /* FIXME: Need to some what arbitrarily order conditional lists */
183 insn_word_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
188 if (l
== NULL
&& r
== NULL
)
189 return 0; /* all previous fields the same */
191 return -1; /* left shorter than right */
193 return +1; /* left longer than right */
195 bit_nr
< options
.insn_bit_size
;
198 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
200 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
202 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
204 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
214 opcode_bit_cmp (opcode_bits
*l
,
217 if (l
== NULL
&& r
== NULL
)
218 return 0; /* all previous bits the same */
220 return -1; /* left shorter than right */
222 return +1; /* left longer than right */
223 /* most significant word */
224 if (l
->field
->word_nr
< r
->field
->word_nr
)
225 return +1; /* left has more significant word */
226 if (l
->field
->word_nr
> r
->field
->word_nr
)
227 return -1; /* right has more significant word */
228 /* most significant bit? */
229 if (l
->first
< r
->first
)
230 return +1; /* left as more significant bit */
231 if (l
->first
> r
->first
)
232 return -1; /* right as more significant bit */
234 if (l
->last
< r
->last
)
235 return +1; /* left as less bits */
236 if (l
->last
> r
->last
)
237 return -1; /* right as less bits */
239 if (l
->value
< r
->value
)
241 if (l
->value
> r
->value
)
249 opcode_bits_cmp (opcode_bits
*l
,
255 if (l
== NULL
&& r
== NULL
)
256 return 0; /* all previous bits the same */
257 cmp
= opcode_bit_cmp (l
, r
);
267 new_opcode_bits (opcode_bits
*old_bits
,
271 insn_field_entry
*field
,
272 opcode_field
*opcode
)
274 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
275 new_bits
->field
= field
;
276 new_bits
->value
= value
;
277 new_bits
->first
= first
;
278 new_bits
->last
= last
;
279 new_bits
->opcode
= opcode
;
281 if (old_bits
!= NULL
)
283 opcode_bits
*new_list
;
284 opcode_bits
**last
= &new_list
;
285 new_list
= new_opcode_bits (old_bits
->next
,
291 while (*last
!= NULL
)
293 int cmp
= opcode_bit_cmp (new_bits
, *last
);
294 if (cmp
< 0) /* new < new_list */
300 ERROR ("Duplicated insn bits in list");
302 last
= &(*last
)->next
;
304 new_bits
->next
= *last
;
314 /* Same as strcmp(). */
316 format_name_cmp (const char *l
, const char *r
)
318 if (l
== NULL
&& r
== NULL
)
320 if (l
!= NULL
&& r
== NULL
)
322 if (l
== NULL
&& r
!= NULL
)
324 return strcmp (l
, r
);
329 merge_duplicate_insns
,
330 report_duplicate_insns
,
331 } duplicate_insn_actions
;
334 insn_list_insert (insn_list
**cur_insn_ptr
,
337 opcode_bits
*expanded_bits
,
338 opcode_field
*opcodes
,
339 int nr_prefetched_words
,
340 duplicate_insn_actions duplicate_action
)
342 /* insert it according to the order of the fields & bits */
343 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
347 /* key#1 sort according to the constant fields of each instruction */
348 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
354 /* key#2 sort according to the expanded bits of each instruction */
355 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
361 /* key#3 sort according to the non-constant fields of each instruction */
362 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
368 /* key#4 sort according to the format-name. If two apparently
369 identical instructions have unique format-names, then the
370 instructions are different. This is because the
371 format-name's use is overloaded, it not only indicates the
372 format name but also provides a unique semantic name for the
374 cmp
= format_name_cmp (insn
->format_name
, (*cur_insn_ptr
)->insn
->format_name
);
380 /* duplicate keys, report problem */
381 switch (duplicate_action
)
383 case report_duplicate_insns
:
384 /* It would appear that we have two instructions with the
385 same constant field values across all words and bits.
386 This error can also occure when insn_field_cmp() is
387 failing to differentiate between two instructions that
388 differ only in their conditional fields. */
390 "Two instructions with identical constant fields\n");
391 error ((*cur_insn_ptr
)->insn
->line
,
392 "Location of duplicate instruction\n");
393 case merge_duplicate_insns
:
394 /* Add the opcode path to the instructions list */
397 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
398 while (*last
!= NULL
)
400 last
= &(*last
)->next
;
402 (*last
) = ZALLOC (insn_opcodes
);
403 (*last
)->opcode
= opcodes
;
405 /* Use the larger nr_prefetched_words */
406 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
407 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
408 return (*cur_insn_ptr
);
413 /* create a new list entry and insert it */
415 insn_list
*new_insn
= ZALLOC (insn_list
);
416 new_insn
->insn
= insn
;
417 new_insn
->expanded_bits
= expanded_bits
;
418 new_insn
->next
= (*cur_insn_ptr
);
419 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
422 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
423 new_insn
->opcodes
->opcode
= opcodes
;
425 (*cur_insn_ptr
) = new_insn
;
430 return (*cur_insn_ptr
);
435 gen_entry_traverse_tree (lf
*file
,
438 gen_entry_handler
*start
,
439 gen_entry_handler
*leaf
,
440 gen_entry_handler
*end
,
445 ASSERT (table
!= NULL
);
446 ASSERT (table
->opcode
!= NULL
);
447 ASSERT (table
->nr_entries
> 0);
448 ASSERT (table
->entries
!= 0);
451 if (start
!= NULL
&& depth
>= 0)
453 start (file
, table
, depth
, data
);
456 for (entry
= table
->entries
;
458 entry
= entry
->sibling
)
460 if (entry
->entries
!= NULL
&& depth
!= 0)
462 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
463 start
, leaf
, end
, data
);
469 leaf (file
, entry
, depth
, data
);
474 if (end
!= NULL
&& depth
>= 0)
476 end (file
, table
, depth
, data
);
482 /* create a list element containing a single gen_table entry */
485 make_table (insn_table
*isa
,
490 gen_list
*entry
= ZALLOC (gen_list
);
491 entry
->table
= ZALLOC (gen_entry
);
492 entry
->table
->top
= entry
;
493 entry
->model
= model
;
495 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
498 || insn
->processors
== NULL
499 || filter_is_member (insn
->processors
, model
->name
))
501 insn_list_insert (&entry
->table
->insns
,
502 &entry
->table
->nr_insns
,
504 NULL
, /* expanded_bits - none yet */
505 NULL
, /* opcodes - none yet */
506 0, /* nr_prefetched_words - none yet */
507 report_duplicate_insns
);
510 entry
->table
->opcode_rule
= rules
;
516 make_gen_tables (insn_table
*isa
,
519 gen_table
*gen
= ZALLOC (gen_table
);
522 if (options
.gen
.multi_sim
)
524 gen_list
**last
= &gen
->tables
;
527 if (options
.model_filter
!= NULL
)
528 processors
= options
.model_filter
;
530 processors
= isa
->model
->processors
;
531 for (model
= isa
->model
->models
;
535 if (filter_is_member (processors
, model
->name
))
537 *last
= make_table (isa
, rules
, model
);
538 last
= &(*last
)->next
;
544 gen
->tables
= make_table (isa
, rules
, NULL
);
550 /****************************************************************/
554 field_is_not_constant
= 0,
555 field_constant_int
= 1,
556 field_constant_reserved
= 2,
557 field_constant_string
= 3
558 } constant_field_types
;
560 static constant_field_types
561 insn_field_is_constant (insn_field
*field
,
567 /* field is an integer */
568 return field_constant_int
;
569 case insn_field_reserved
:
570 /* field is `/' and treating that as a constant */
571 if (rule
->with_zero_reserved
)
572 return field_constant_reserved
;
574 return field_is_not_constant
;
575 case insn_field_wild
:
576 return field_is_not_constant
; /* never constant */
577 case insn_field_string
:
578 /* field, though variable, is on the list of forced constants */
579 if (filter_is_member (rule
->constant_field_names
, field
->val_string
))
580 return field_constant_string
;
582 return field_is_not_constant
;
584 ERROR ("Internal error");
585 return field_is_not_constant
;
590 /****************************************************************/
593 /* Is the bit, according to the decode rule, identical across all the
596 insns_bit_useless (insn_list
*insns
,
602 int is_useless
= 1; /* cleared if something actually found */
604 /* check the instructions for some constant value in at least one of
606 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
608 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
609 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
610 switch (bit
->field
->type
)
612 case insn_field_invalid
:
615 case insn_field_wild
:
616 case insn_field_reserved
:
617 /* neither useless or useful - ignore */
620 switch (rule
->search
)
622 case decode_find_strings
:
623 /* an integer isn't a string */
625 case decode_find_constants
:
626 case decode_find_mixed
:
627 /* an integer is useful if its value isn't the same
628 between all instructions. The first time through the
629 value is saved, the second time through (if the
630 values differ) it is marked as useful. */
633 else if (value
!= bit
->value
)
638 case insn_field_string
:
639 switch (rule
->search
)
641 case decode_find_strings
:
642 /* at least one string, keep checking */
645 case decode_find_constants
:
646 case decode_find_mixed
:
647 if (filter_is_member (rule
->constant_field_names
,
648 bit
->field
->val_string
))
649 /* a string field forced to constant? */
651 else if (rule
->search
== decode_find_constants
)
652 /* the string field isn't constant */
659 /* Given only one constant value has been found, check through all
660 the instructions to see if at least one conditional makes it
662 if (value
>= 0 && is_useless
)
664 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
666 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
667 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
668 switch (bit
->field
->type
)
670 case insn_field_invalid
:
673 case insn_field_wild
:
674 case insn_field_reserved
:
676 /* already processed */
678 case insn_field_string
:
679 switch (rule
->search
)
681 case decode_find_strings
:
682 case decode_find_constants
:
683 /* already processed */
685 case decode_find_mixed
:
686 /* string field with conditions. If this condition
687 eliminates the value then the compare is useful */
688 if (bit
->field
->conditions
!= NULL
)
690 insn_field_cond
*condition
;
691 int shift
= bit
->field
->last
- bit_nr
;
692 for (condition
= bit
->field
->conditions
;
694 condition
= condition
->next
)
696 switch (condition
->type
)
698 case insn_field_cond_value
:
699 switch (condition
->test
)
701 case insn_field_cond_ne
:
702 if (((condition
->value
>> shift
) & 1)
704 /* conditional field excludes the
708 case insn_field_cond_eq
:
709 if (((condition
->value
>> shift
) & 1)
711 /* conditional field requires the
717 case insn_field_cond_field
:
718 /* are these handled separatly? */
732 /* go through a gen-table's list of instruction formats looking for a
733 range of bits that meet the decode table RULEs requirements */
735 static opcode_field
*
736 gen_entry_find_opcode_field (insn_list
*insns
,
740 opcode_field curr_opcode
;
741 ASSERT (rule
!= NULL
);
743 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
744 curr_opcode
.word_nr
= rule
->word_nr
;
745 curr_opcode
.first
= rule
->first
;
746 curr_opcode
.last
= rule
->last
;
748 /* Try to reduce the size of first..last in accordance with the
751 while (curr_opcode
.first
<= rule
->last
)
753 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
754 curr_opcode
.first
++;
758 while (curr_opcode
.last
>= rule
->first
)
760 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
768 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
770 insn_word_entry
*fields
= entry
->insn
->word
[rule
->word_nr
];
771 opcode_field new_opcode
;
773 ASSERT (fields
!= NULL
);
775 /* find a start point for the opcode field */
776 new_opcode
.first
= rule
->first
;
777 while (new_opcode
.first
<= rule
->last
779 || (insn_field_is_constant(fields
->bit
[new_opcode
.first
], rule
)
780 != field_constant_string
))
782 || (insn_field_is_constant(fields
->bit
[new_opcode
.first
], rule
)
783 == field_is_not_constant
)))
785 int new_first
= fields
->bit
[new_opcode
.first
]->last
+ 1;
786 ASSERT (new_first
> new_opcode
.first
);
787 new_opcode
.first
= new_first
;
789 ASSERT(new_opcode
.first
> rule
->last
791 && insn_field_is_constant(fields
->bit
[new_opcode
.first
],
792 rule
) == field_constant_string
)
794 && insn_field_is_constant(fields
->bit
[new_opcode
.first
],
797 /* find the end point for the opcode field */
798 new_opcode
.last
= rule
->last
;
799 while (new_opcode
.last
>= rule
->first
801 || insn_field_is_constant(fields
->bit
[new_opcode
.last
],
802 rule
) != field_constant_string
)
804 || !insn_field_is_constant(fields
->bit
[new_opcode
.last
],
807 int new_last
= fields
->bit
[new_opcode
.last
]->first
- 1;
808 ASSERT (new_last
< new_opcode
.last
);
809 new_opcode
.last
= new_last
;
811 ASSERT(new_opcode
.last
< rule
->first
813 && insn_field_is_constant(fields
->bit
[new_opcode
.last
],
814 rule
) == field_constant_string
)
816 && insn_field_is_constant(fields
->bit
[new_opcode
.last
],
819 /* now see if our current opcode needs expanding to include the
820 interesting fields within this instruction */
821 if (new_opcode
.first
<= rule
->last
822 && curr_opcode
.first
> new_opcode
.first
)
823 curr_opcode
.first
= new_opcode
.first
;
824 if (new_opcode
.last
>= rule
->first
825 && curr_opcode
.last
< new_opcode
.last
)
826 curr_opcode
.last
= new_opcode
.last
;
831 /* did the final opcode field end up being empty? */
832 if (curr_opcode
.first
> curr_opcode
.last
)
836 ASSERT (curr_opcode
.last
>= rule
->first
);
837 ASSERT (curr_opcode
.first
<= rule
->last
);
838 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
840 /* Ensure that, for the non string only case, the opcode includes
841 the range forced_first .. forced_last */
843 && curr_opcode
.first
> rule
->force_first
)
845 curr_opcode
.first
= rule
->force_first
;
848 && curr_opcode
.last
< rule
->force_last
)
850 curr_opcode
.last
= rule
->force_last
;
853 /* For the string only case, force just the lower bound (so that the
854 shift can be eliminated) */
856 && rule
->force_last
== options
.insn_bit_size
- 1)
858 curr_opcode
.last
= options
.insn_bit_size
- 1;
861 /* handle any special cases */
864 case normal_decode_rule
:
865 /* let the above apply */
866 curr_opcode
.nr_opcodes
=
867 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
870 curr_opcode
.is_boolean
= 1;
871 curr_opcode
.boolean_constant
= rule
->constant
;
872 curr_opcode
.nr_opcodes
= 2;
877 opcode_field
*new_field
= ZALLOC (opcode_field
);
878 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
885 gen_entry_insert_insn (gen_entry
*table
,
886 insn_entry
*old_insn
,
888 int new_nr_prefetched_words
,
890 opcode_bits
*new_bits
)
892 gen_entry
**entry
= &table
->entries
;
894 /* find the new table for this entry */
895 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
897 entry
= &(*entry
)->sibling
;
900 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
902 /* insert the missing entry */
903 gen_entry
*new_entry
= ZALLOC (gen_entry
);
904 new_entry
->sibling
= (*entry
);
905 (*entry
) = new_entry
;
908 new_entry
->top
= table
->top
;
909 new_entry
->opcode_nr
= new_opcode_nr
;
910 new_entry
->word_nr
= new_word_nr
;
911 new_entry
->expanded_bits
= new_bits
;
912 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
913 new_entry
->parent
= table
;
914 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
916 /* ASSERT new_bits == cur_entry bits */
917 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
918 insn_list_insert (&(*entry
)->insns
,
921 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
,
935 if (bit_nr
> table
->opcode
->last
)
937 /* Only include the hardwired bit information with an entry IF
938 that entry (and hence its functions) are being duplicated. */
939 if (options
.trace
.insn_expansion
)
941 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
942 notify (NULL
, ": insert %d - %s.%s%s\n",
944 instruction
->format_name
,
946 (table
->opcode_rule
->with_duplicates
? " (duplicated)" : ""));
948 if (table
->opcode_rule
->with_duplicates
)
950 gen_entry_insert_insn (table
, instruction
,
951 table
->opcode
->word_nr
,
952 table
->nr_prefetched_words
,
957 gen_entry_insert_insn (table
, instruction
,
958 table
->opcode
->word_nr
,
959 table
->nr_prefetched_words
,
965 insn_word_entry
*word
= instruction
->word
[table
->opcode
->word_nr
];
966 insn_field_entry
*field
= word
->bit
[bit_nr
]->field
;
967 int last_pos
= ((field
->last
< table
->opcode
->last
)
969 : table
->opcode
->last
);
970 int first_pos
= ((field
->first
> table
->opcode
->first
)
972 : table
->opcode
->first
);
973 int width
= last_pos
- first_pos
+ 1;
979 val
= sub_val (field
->val_int
, field
->last
,
980 first_pos
, last_pos
);
981 gen_entry_expand_opcode (table
, instruction
,
983 ((opcode_nr
<< width
) | val
),
989 if (field
->type
== insn_field_reserved
)
990 gen_entry_expand_opcode (table
, instruction
,
992 ((opcode_nr
<< width
)),
997 int last_val
= (table
->opcode
->is_boolean
1000 for (val
= 0; val
< last_val
; val
++)
1002 /* check to see if the value has been precluded
1003 (by a conditional) in some way */
1005 insn_field_cond
*condition
;
1006 for (condition
= field
->conditions
, is_precluded
= 0;
1007 condition
!= NULL
&& !is_precluded
;
1008 condition
= condition
->next
)
1010 switch (condition
->type
)
1012 case insn_field_cond_value
:
1014 int value
= sub_val (condition
->value
, field
->last
,
1015 first_pos
, last_pos
);
1016 switch (condition
->test
)
1018 case insn_field_cond_ne
:
1022 case insn_field_cond_eq
:
1029 case insn_field_cond_field
:
1033 gen_entry
*t
= NULL
;
1034 /* Try to find a value for the
1035 conditional by looking back through
1036 the previously defined bits for one
1037 that covers the designated
1038 conditional field */
1043 if (bit
->field
->word_nr
== condition
->field
->word_nr
1044 && bit
->first
<= condition
->field
->first
1045 && bit
->last
>= condition
->field
->last
)
1047 /* the bit field fully specified
1048 the conditional field's value */
1049 value
= sub_val (bit
->value
, bit
->last
,
1050 condition
->field
->first
,
1051 condition
->field
->last
);
1054 /* Try to find a value by looking
1055 through this and previous tables */
1062 if (t
->parent
->opcode
->word_nr
== condition
->field
->word_nr
1063 && t
->parent
->opcode
->first
<= condition
->field
->first
1064 && t
->parent
->opcode
->last
>= condition
->field
->last
)
1066 /* the table entry fully
1067 specified the condition
1069 /* extract the field's value
1071 value
= sub_val (t
->opcode_nr
, t
->parent
->opcode
->last
,
1072 condition
->field
->first
, condition
->field
->last
);
1073 /* this is a requirement of
1075 refering to another field */
1076 ASSERT ((condition
->field
->first
- condition
->field
->last
)
1077 == (first_pos
- last_pos
));
1078 printf ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
1079 value
, t
->opcode_nr
, t
->parent
->opcode
->last
, condition
->field
->first
, condition
->field
->last
);
1083 if (bit
== NULL
&& t
== NULL
)
1084 error (instruction
->line
,
1085 "Conditional `%s' of field `%s' isn't expanded",
1086 condition
->string
, field
->val_string
);
1087 switch (condition
->test
)
1089 case insn_field_cond_ne
:
1093 case insn_field_cond_eq
:
1104 /* Only add additional hardwired bit
1105 information if the entry is not going to
1106 later be combined */
1107 if (table
->opcode_rule
->with_combine
)
1109 gen_entry_expand_opcode (table
, instruction
,
1111 ((opcode_nr
<< width
) | val
),
1116 opcode_bits
*new_bits
= new_opcode_bits (bits
, val
,
1117 first_pos
, last_pos
,
1120 gen_entry_expand_opcode (table
, instruction
,
1122 ((opcode_nr
<< width
) | val
),
1134 gen_entry_insert_expanding (gen_entry
*table
,
1135 insn_entry
*instruction
)
1137 gen_entry_expand_opcode (table
,
1139 table
->opcode
->first
,
1141 table
->expanded_bits
);
1146 insns_match_format_names (insn_list
*insns
,
1147 filter
*format_names
)
1149 if (format_names
!= NULL
)
1152 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1154 if ( i
->insn
->format_name
!= NULL
1155 && !filter_is_member (format_names
, i
->insn
->format_name
))
1163 table_matches_path (gen_entry
*table
,
1164 decode_path_list
*paths
)
1168 while (paths
!= NULL
)
1170 gen_entry
*entry
= table
;
1171 decode_path
*path
= paths
->path
;
1174 if (entry
== NULL
&& path
== NULL
)
1176 if (entry
== NULL
|| path
== NULL
)
1178 if (entry
->opcode_nr
!= path
->opcode_nr
)
1180 entry
= entry
->parent
;
1181 path
= path
->parent
;
1183 paths
= paths
->next
;
1190 insns_match_conditions (insn_list
*insns
,
1191 decode_cond
*conditions
)
1193 if (conditions
!= NULL
)
1196 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1199 for (cond
= conditions
; cond
!= NULL
; cond
= cond
->next
)
1202 if (i
->insn
->nr_words
<= cond
->word_nr
)
1204 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
1206 if (!cond
->mask
[bit_nr
])
1208 if (!i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->mask
)
1210 if ((i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->value
1211 == cond
->value
[bit_nr
])
1222 insns_match_nr_words (insn_list
*insns
,
1226 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1228 if (i
->insn
->nr_words
< nr_words
)
1235 insn_list_cmp (insn_list
*l
,
1241 if (l
== NULL
&& r
== NULL
)
1247 if (l
->insn
!= r
->insn
)
1248 return -1; /* somewhat arbitrary at present */
1249 /* skip this insn */
1251 while (l
!= NULL
&& l
->insn
== insn
)
1253 while (r
!= NULL
&& r
->insn
== insn
)
1261 gen_entry_expand_insns (gen_entry
*table
)
1263 decode_table
*opcode_rule
;
1265 ASSERT(table
->nr_insns
>= 1);
1267 /* determine a valid opcode */
1268 for (opcode_rule
= table
->opcode_rule
;
1269 opcode_rule
!= NULL
;
1270 opcode_rule
= opcode_rule
->next
)
1272 char *discard_reason
;
1273 if (table
->top
->model
!= NULL
1274 && opcode_rule
->model_names
!= NULL
1275 && !filter_is_member (opcode_rule
->model_names
,
1276 table
->top
->model
->name
))
1278 /* the rule isn't applicable to this processor */
1279 discard_reason
= "wrong model";
1281 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1283 /* for safety, require a pre-codition when attempting to
1284 apply a rule to a single instruction */
1285 discard_reason
= "need pre-condition when nr-insn == 1";
1287 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1289 /* Little point in expanding a single instruction when we're
1290 not duplicating the semantic functions that this table
1292 discard_reason
= "need duplication with nr-insns == 1";
1294 else if (!insns_match_format_names (table
->insns
, opcode_rule
->format_names
))
1296 discard_reason
= "wrong format name";
1298 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1300 discard_reason
= "wrong nr words";
1302 else if (!table_matches_path (table
, opcode_rule
->paths
))
1304 discard_reason
= "path failed";
1306 else if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1308 discard_reason
= "condition failed";
1312 discard_reason
= "no opcode field";
1314 gen_entry_find_opcode_field (table
->insns
,
1316 table
->nr_insns
== 1/*string-only*/
1318 if (table
->opcode
!= NULL
)
1320 table
->opcode_rule
= opcode_rule
;
1325 if (options
.trace
.rule_rejection
)
1327 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1328 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1332 /* did we find anything */
1333 if (opcode_rule
== NULL
)
1335 /* the decode table failed, this set of instructions haven't
1336 been uniquely identified */
1337 if (table
->nr_insns
> 1)
1339 print_gen_entry_insns (table
, warning
,
1340 "was not uniquely decoded",
1341 "decodes to the same entry");
1347 /* Determine the number of words that must have been prefetched for
1348 this table to function */
1349 if (table
->parent
== NULL
)
1350 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1351 else if (table
->opcode_rule
->word_nr
+ 1 > table
->parent
->nr_prefetched_words
)
1352 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1354 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1356 /* back link what we found to its parent */
1357 if (table
->parent
!= NULL
)
1359 ASSERT(table
->parent
->opcode
!= NULL
);
1360 table
->opcode
->parent
= table
->parent
->opcode
;
1363 /* report the rule being used to expand the instructions */
1364 if (options
.trace
.rule_selection
)
1366 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1368 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1369 table
->opcode
->word_nr
,
1370 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1371 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1372 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1373 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1374 table
->opcode
->nr_opcodes
,
1378 /* expand the raw instructions according to the opcode */
1381 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1383 if (options
.trace
.insn_expansion
)
1385 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1386 notify (NULL
, ": expand - %s.%s\n",
1387 entry
->insn
->format_name
,
1390 gen_entry_insert_expanding (table
, entry
->insn
);
1394 /* dump the results */
1395 if (options
.trace
.entries
)
1398 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1401 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1402 notify (NULL
, ": %d - entries %d -",
1405 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1406 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1407 notify (NULL
, "\n");
1411 /* perform a combine pass if needed */
1412 if (table
->opcode_rule
->with_combine
)
1415 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1417 if (entry
->combined_parent
== NULL
)
1419 gen_entry
**last
= &entry
->combined_next
;
1421 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1423 if (alt
->combined_parent
== NULL
1424 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1426 alt
->combined_parent
= entry
;
1428 last
= &alt
->combined_next
;
1433 if (options
.trace
.combine
)
1437 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1439 if (entry
->combined_parent
== NULL
)
1442 gen_entry
*duplicate
;
1444 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1445 for (duplicate
= entry
->combined_next
;
1447 duplicate
= duplicate
->combined_next
)
1449 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1451 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1452 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1454 notify (NULL
, " %s.%s",
1455 l
->insn
->format_name
,
1458 notify (NULL
, "\n");
1461 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1462 notify (NULL
, ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1463 table
->opcode
->word_nr
,
1464 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1465 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1466 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1467 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1468 table
->opcode
->nr_opcodes
,
1474 /* Check that the rule did more than re-arange the order of the
1478 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1480 if (entry
->combined_parent
== NULL
)
1482 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1484 print_gen_entry_path (table
->opcode_rule
->line
, table
, warning
);
1485 warning (NULL
, ": Applying rule just copied all instructions\n");
1486 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1493 /* if some form of expanded table, fill in the missing dots */
1494 switch (table
->opcode_rule
->gen
)
1496 case padded_switch_gen
:
1498 case goto_switch_gen
:
1499 if (!table
->opcode
->is_boolean
)
1501 gen_entry
**entry
= &table
->entries
;
1502 gen_entry
*illegals
= NULL
;
1503 gen_entry
**last_illegal
= &illegals
;
1505 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1507 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1509 /* missing - insert it under our feet at *entry */
1510 gen_entry_insert_insn (table
,
1511 table
->top
->isa
->illegal_insn
,
1512 table
->opcode
->word_nr
,
1513 0, /* nr_prefetched_words == 0 for invalid */
1515 ASSERT ((*entry
) != NULL
);
1516 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1517 (*last_illegal
) = *entry
;
1518 (*last_illegal
)->combined_parent
= illegals
;
1519 last_illegal
= &(*last_illegal
)->combined_next
;
1521 entry
= &(*entry
)->sibling
;
1524 /* oops, will have pointed the first illegal insn back to
1525 its self. Fix this */
1526 if (illegals
!= NULL
)
1527 illegals
->combined_parent
= NULL
;
1536 /* and do the same for the newly created sub entries but *only*
1537 expand entries that haven't been combined. */
1540 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1542 if (entry
->combined_parent
== NULL
)
1544 gen_entry_expand_insns (entry
);
1551 gen_tables_expand_insns (gen_table
*gen
)
1554 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1556 gen_entry_expand_insns (entry
->table
);
1561 /* create a list of all the semantic functions that need to be
1562 generated. Eliminate any duplicates. Verify that the decode stage
1566 make_gen_semantics_list (lf
*file
,
1571 gen_table
*gen
= (gen_table
*) data
;
1573 /* Not interested in an entrie that have been combined into some
1574 other entry at the same level */
1575 if (entry
->combined_parent
!= NULL
)
1578 /* a leaf should contain exactly one instruction. If not the decode
1580 ASSERT (entry
->nr_insns
== 1);
1582 /* Enter this instruction into the list of semantic functions. */
1583 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1585 entry
->expanded_bits
,
1586 entry
->parent
->opcode
,
1587 entry
->insns
->nr_prefetched_words
,
1588 merge_duplicate_insns
);
1589 /* point the table entry at the real semantic function */
1590 ASSERT (insn
!= NULL
);
1591 entry
->insns
->semantic
= insn
;
1596 gen_tables_expand_semantics (gen_table
*gen
)
1599 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1601 gen_entry_traverse_tree (NULL
,
1604 NULL
, /* start-handler */
1605 make_gen_semantics_list
, /* leaf-handler */
1606 NULL
, /* end-handler */
1617 dump_opcode_field (lf
*file
,
1619 opcode_field
*field
,
1623 lf_printf (file
, "%s(opcode_field *) 0x%lx", prefix
, (long) field
);
1624 if (levels
&& field
!= NULL
) {
1625 lf_indent (file
, +1);
1626 lf_printf (file
, "\n(first %d)", field
->first
);
1627 lf_printf (file
, "\n(last %d)", field
->last
);
1628 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1629 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1630 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1631 dump_opcode_field(file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1632 lf_indent (file
, -1);
1634 lf_printf (file
, "%s", suffix
);
1639 dump_opcode_bits (lf
*file
,
1645 lf_printf (file
, "%s(opcode_bits *) 0x%lx", prefix
, (long) bits
);
1647 if (levels
&& bits
!= NULL
)
1649 lf_indent (file
, +1);
1650 lf_printf (file
, "\n(value %d)", bits
->value
);
1651 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1652 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1653 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1654 lf_indent (file
, -1);
1656 lf_printf (file
, "%s", suffix
);
1662 dump_insn_list (lf
*file
,
1667 lf_printf (file
, "%s(insn_list *) 0x%lx", prefix
, (long) entry
);
1669 if (entry
!= NULL
) {
1670 lf_indent (file
, +1);
1671 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1672 lf_printf (file
, "\n(next 0x%lx)", (long) entry
->next
);
1673 lf_indent (file
, -1);
1675 lf_printf (file
, "%s", suffix
);
1680 dump_insn_word_entry_list_entries (lf
*file
,
1685 lf_printf (file
, "%s", prefix
);
1686 while (entry
!= NULL
)
1688 dump_insn_list (file
, "\n(", entry
, ")");
1689 entry
= entry
->next
;
1691 lf_printf (file
, "%s", suffix
);
1696 dump_gen_entry (lf
*file
,
1703 lf_printf (file
, "%s(gen_entry *) 0x%lx", prefix
, (long) table
);
1705 if (levels
&& table
!= NULL
) {
1707 lf_indent (file
, +1);
1708 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1709 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1710 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")", -1);
1711 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1712 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
, ")");
1713 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1714 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1715 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1716 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")", table
->nr_entries
);
1717 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1718 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1719 lf_indent (file
, -1);
1721 lf_printf (file
, "%s", suffix
);
1725 dump_gen_list (lf
*file
,
1731 while (entry
!= NULL
)
1733 lf_printf (file
, "%s(gen_list *) 0x%lx", prefix
, (long) entry
);
1734 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1735 lf_printf (file
, "\n(next (gen_list *) 0x%lx)", (long) entry
->next
);
1736 lf_printf (file
, "%s", suffix
);
1742 dump_gen_table (lf
*file
,
1748 lf_printf (file
, "%s(gen_table *) 0x%lx", prefix
, (long) gen
);
1749 lf_printf (file
, "\n(isa (insn_table *) 0x%lx)", (long) gen
->isa
);
1750 lf_printf (file
, "\n(rules (decode_table *) 0x%lx)", (long) gen
->rules
);
1751 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1752 lf_printf (file
, "%s", suffix
);
1756 igen_options options
;
1762 decode_table
*decode_rules
;
1763 insn_table
*instructions
;
1768 error (NULL
, "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1770 INIT_OPTIONS (options
);
1772 filter_parse (&options
.flags_filter
, argv
[1]);
1774 options
.hi_bit_nr
= a2i(argv
[2]);
1775 options
.insn_bit_size
= a2i(argv
[3]);
1776 options
.insn_specifying_widths
= a2i(argv
[4]);
1777 ASSERT(options
.hi_bit_nr
< options
.insn_bit_size
);
1779 instructions
= load_insn_table (argv
[6], NULL
);
1780 decode_rules
= load_decode_table (argv
[5]);
1781 gen
= make_gen_tables (instructions
, decode_rules
);
1783 gen_tables_expand_insns (gen
);
1785 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1787 dump_gen_table (l
, "(", gen
, ")\n", -1);