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
, int val_last_pos
, int first_pos
, int last_pos
)
38 return ((val
>> (val_last_pos
- last_pos
))
39 & (((insn_uint
) 1 << (last_pos
- first_pos
+ 1)) - 1));
43 update_depth (lf
*file
, gen_entry
*entry
, int depth
, void *data
)
45 int *max_depth
= (int *) data
;
46 if (*max_depth
< depth
)
52 gen_entry_depth (gen_entry
*table
)
55 gen_entry_traverse_tree (NULL
, table
, 1, NULL
, /*start */
56 update_depth
, NULL
, /*end */
63 print_gen_entry_path (line_ref
*line
, gen_entry
*table
, error_func
*print
)
65 if (table
->parent
== NULL
)
67 if (table
->top
->model
!= NULL
)
68 print (line
, "%s", table
->top
->model
->name
);
74 print_gen_entry_path (line
, table
->parent
, print
);
75 print (NULL
, ".%d", table
->opcode_nr
);
80 print_gen_entry_insns (gen_entry
*table
,
82 char *first_message
, char *next_message
)
86 message
= first_message
;
87 for (i
= table
->insns
; i
!= NULL
; i
= i
->next
)
89 insn_entry
*insn
= i
->insn
;
90 print_gen_entry_path (insn
->line
, table
, print
);
91 print (NULL
, ": %s.%s %s\n", insn
->format_name
, insn
->name
, message
);
92 if (next_message
!= NULL
)
93 message
= next_message
;
99 insn_field_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
104 if (l
== NULL
&& r
== NULL
)
105 return 0; /* all previous fields the same */
107 return -1; /* left shorter than right */
109 return +1; /* left longer than right */
110 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
112 if (l
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
114 if (r
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
116 if (l
->bit
[bit_nr
]->field
->conditions
== NULL
)
118 if (r
->bit
[bit_nr
]->field
->conditions
== NULL
)
121 printf ("%s%s%s VS %s%s%s\n",
122 l
->bit
[bit_nr
]->field
->val_string
,
123 l
->bit
[bit_nr
]->field
->conditions
->test
==
124 insn_field_cond_eq
? "=" : "!",
125 l
->bit
[bit_nr
]->field
->conditions
->string
,
126 r
->bit
[bit_nr
]->field
->val_string
,
127 r
->bit
[bit_nr
]->field
->conditions
->test
==
128 insn_field_cond_eq
? "=" : "!",
129 r
->bit
[bit_nr
]->field
->conditions
->string
);
130 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
131 && r
->bit
[bit_nr
]->field
->conditions
->test
==
134 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
135 insn_field_cond_field
136 && r
->bit
[bit_nr
]->field
->conditions
->type
==
137 insn_field_cond_field
)
138 /* somewhat arbitrary */
140 int cmp
= strcmp (l
->bit
[bit_nr
]->field
->conditions
->string
,
141 r
->bit
[bit_nr
]->field
->conditions
->
148 if (l
->bit
[bit_nr
]->field
->conditions
->type
==
149 insn_field_cond_field
)
151 if (r
->bit
[bit_nr
]->field
->conditions
->type
==
152 insn_field_cond_field
)
154 /* The case of both fields having constant values should have
155 already have been handled because such fields are converted
156 into normal constant fields. */
159 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
160 return +1; /* left = only */
161 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
162 return -1; /* right = only */
163 /* FIXME: Need to some what arbitrarily order conditional lists */
173 insn_word_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
178 if (l
== NULL
&& r
== NULL
)
179 return 0; /* all previous fields the same */
181 return -1; /* left shorter than right */
183 return +1; /* left longer than right */
184 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
186 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
188 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
190 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
192 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
202 opcode_bit_cmp (opcode_bits
*l
, opcode_bits
*r
)
204 if (l
== NULL
&& r
== NULL
)
205 return 0; /* all previous bits the same */
207 return -1; /* left shorter than right */
209 return +1; /* left longer than right */
210 /* most significant word */
211 if (l
->field
->word_nr
< r
->field
->word_nr
)
212 return +1; /* left has more significant word */
213 if (l
->field
->word_nr
> r
->field
->word_nr
)
214 return -1; /* right has more significant word */
215 /* most significant bit? */
216 if (l
->first
< r
->first
)
217 return +1; /* left as more significant bit */
218 if (l
->first
> r
->first
)
219 return -1; /* right as more significant bit */
221 if (l
->last
< r
->last
)
222 return +1; /* left as less bits */
223 if (l
->last
> r
->last
)
224 return -1; /* right as less bits */
226 if (l
->value
< r
->value
)
228 if (l
->value
> r
->value
)
236 opcode_bits_cmp (opcode_bits
*l
, opcode_bits
*r
)
241 if (l
== NULL
&& r
== NULL
)
242 return 0; /* all previous bits the same */
243 cmp
= opcode_bit_cmp (l
, r
);
253 new_opcode_bits (opcode_bits
*old_bits
,
256 int last
, insn_field_entry
*field
, opcode_field
*opcode
)
258 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
259 new_bits
->field
= field
;
260 new_bits
->value
= value
;
261 new_bits
->first
= first
;
262 new_bits
->last
= last
;
263 new_bits
->opcode
= opcode
;
265 if (old_bits
!= NULL
)
267 opcode_bits
*new_list
;
268 opcode_bits
**last
= &new_list
;
269 new_list
= new_opcode_bits (old_bits
->next
,
273 old_bits
->field
, old_bits
->opcode
);
274 while (*last
!= NULL
)
276 int cmp
= opcode_bit_cmp (new_bits
, *last
);
277 if (cmp
< 0) /* new < new_list */
283 ERROR ("Duplicated insn bits in list");
285 last
= &(*last
)->next
;
287 new_bits
->next
= *last
;
297 /* Same as strcmp(). */
299 format_name_cmp (const char *l
, const char *r
)
301 if (l
== NULL
&& r
== NULL
)
303 if (l
!= NULL
&& r
== NULL
)
305 if (l
== NULL
&& r
!= NULL
)
307 return strcmp (l
, r
);
313 merge_duplicate_insns
,
314 report_duplicate_insns
,
316 duplicate_insn_actions
;
319 insn_list_insert (insn_list
**cur_insn_ptr
,
322 opcode_bits
*expanded_bits
,
323 opcode_field
*opcodes
,
324 int nr_prefetched_words
,
325 duplicate_insn_actions duplicate_action
)
327 /* insert it according to the order of the fields & bits */
328 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
332 /* key#1 sort according to the constant fields of each instruction */
333 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
339 /* key#2 sort according to the expanded bits of each instruction */
340 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
346 /* key#3 sort according to the non-constant fields of each instruction */
347 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
353 /* key#4 sort according to the format-name. If two apparently
354 identical instructions have unique format-names, then the
355 instructions are different. This is because the
356 format-name's use is overloaded, it not only indicates the
357 format name but also provides a unique semantic name for the
360 format_name_cmp (insn
->format_name
,
361 (*cur_insn_ptr
)->insn
->format_name
);
367 /* duplicate keys, report problem */
368 switch (duplicate_action
)
370 case report_duplicate_insns
:
371 /* It would appear that we have two instructions with the
372 same constant field values across all words and bits.
373 This error can also occure when insn_field_cmp() is
374 failing to differentiate between two instructions that
375 differ only in their conditional fields. */
377 "Two instructions with identical constant fields\n");
378 error ((*cur_insn_ptr
)->insn
->line
,
379 "Location of duplicate instruction\n");
380 case merge_duplicate_insns
:
381 /* Add the opcode path to the instructions list */
384 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
385 while (*last
!= NULL
)
387 last
= &(*last
)->next
;
389 (*last
) = ZALLOC (insn_opcodes
);
390 (*last
)->opcode
= opcodes
;
392 /* Use the larger nr_prefetched_words */
393 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
394 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
395 return (*cur_insn_ptr
);
400 /* create a new list entry and insert it */
402 insn_list
*new_insn
= ZALLOC (insn_list
);
403 new_insn
->insn
= insn
;
404 new_insn
->expanded_bits
= expanded_bits
;
405 new_insn
->next
= (*cur_insn_ptr
);
406 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
409 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
410 new_insn
->opcodes
->opcode
= opcodes
;
412 (*cur_insn_ptr
) = new_insn
;
417 return (*cur_insn_ptr
);
422 gen_entry_traverse_tree (lf
*file
,
425 gen_entry_handler
* start
,
426 gen_entry_handler
* leaf
,
427 gen_entry_handler
* end
, void *data
)
431 ASSERT (table
!=NULL
);
432 ASSERT (table
->opcode
!= NULL
);
433 ASSERT (table
->nr_entries
> 0);
434 ASSERT (table
->entries
!= 0);
437 if (start
!= NULL
&& depth
>= 0)
439 start (file
, table
, depth
, data
);
442 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
444 if (entry
->entries
!= NULL
&& depth
!= 0)
446 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
447 start
, leaf
, end
, data
);
453 leaf (file
, entry
, depth
, data
);
458 if (end
!= NULL
&& depth
>= 0)
460 end (file
, table
, depth
, data
);
466 /* create a list element containing a single gen_table entry */
469 make_table (insn_table
*isa
, decode_table
*rules
, model_entry
*model
)
472 gen_list
*entry
= ZALLOC (gen_list
);
473 entry
->table
= ZALLOC (gen_entry
);
474 entry
->table
->top
= entry
;
475 entry
->model
= model
;
477 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
480 || insn
->processors
== NULL
481 || filter_is_member (insn
->processors
, model
->name
))
483 insn_list_insert (&entry
->table
->insns
, &entry
->table
->nr_insns
, insn
, NULL
, /* expanded_bits - none yet */
484 NULL
, /* opcodes - none yet */
485 0, /* nr_prefetched_words - none yet */
486 report_duplicate_insns
);
489 entry
->table
->opcode_rule
= rules
;
495 make_gen_tables (insn_table
*isa
, decode_table
*rules
)
497 gen_table
*gen
= ZALLOC (gen_table
);
500 if (options
.gen
.multi_sim
)
502 gen_list
**last
= &gen
->tables
;
505 if (options
.model_filter
!= NULL
)
506 processors
= options
.model_filter
;
508 processors
= isa
->model
->processors
;
509 for (model
= isa
->model
->models
; model
!= NULL
; model
= model
->next
)
511 if (filter_is_member (processors
, model
->name
))
513 *last
= make_table (isa
, rules
, model
);
514 last
= &(*last
)->next
;
520 gen
->tables
= make_table (isa
, rules
, NULL
);
526 /****************************************************************/
531 field_is_not_constant
= 0,
532 field_constant_int
= 1,
533 field_constant_reserved
= 2,
534 field_constant_string
= 3
536 constant_field_types
;
538 static constant_field_types
539 insn_field_is_constant (insn_field
* field
, decode_table
*rule
)
544 /* field is an integer */
545 return field_constant_int
;
546 case insn_field_reserved
:
547 /* field is `/' and treating that as a constant */
548 if (rule
->with_zero_reserved
)
549 return field_constant_reserved
;
551 return field_is_not_constant
;
552 case insn_field_wild
:
553 return field_is_not_constant
; /* never constant */
554 case insn_field_string
:
555 /* field, though variable, is on the list of forced constants */
556 if (filter_is_member (rule
->constant_field_names
, field
->val_string
))
557 return field_constant_string
;
559 return field_is_not_constant
;
561 ERROR ("Internal error");
562 return field_is_not_constant
;
567 /****************************************************************/
570 /* Is the bit, according to the decode rule, identical across all the
573 insns_bit_useless (insn_list
*insns
, decode_table
*rule
, int bit_nr
)
577 int is_useless
= 1; /* cleared if something actually found */
579 /* check the instructions for some constant value in at least one of
581 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
583 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
584 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
585 switch (bit
->field
->type
)
587 case insn_field_invalid
:
590 case insn_field_wild
:
591 case insn_field_reserved
:
592 /* neither useless or useful - ignore */
595 switch (rule
->search
)
597 case decode_find_strings
:
598 /* an integer isn't a string */
600 case decode_find_constants
:
601 case decode_find_mixed
:
602 /* an integer is useful if its value isn't the same
603 between all instructions. The first time through the
604 value is saved, the second time through (if the
605 values differ) it is marked as useful. */
608 else if (value
!= bit
->value
)
613 case insn_field_string
:
614 switch (rule
->search
)
616 case decode_find_strings
:
617 /* at least one string, keep checking */
620 case decode_find_constants
:
621 case decode_find_mixed
:
622 if (filter_is_member (rule
->constant_field_names
,
623 bit
->field
->val_string
))
624 /* a string field forced to constant? */
626 else if (rule
->search
== decode_find_constants
)
627 /* the string field isn't constant */
634 /* Given only one constant value has been found, check through all
635 the instructions to see if at least one conditional makes it
637 if (value
>= 0 && is_useless
)
639 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
641 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
642 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
643 switch (bit
->field
->type
)
645 case insn_field_invalid
:
648 case insn_field_wild
:
649 case insn_field_reserved
:
651 /* already processed */
653 case insn_field_string
:
654 switch (rule
->search
)
656 case decode_find_strings
:
657 case decode_find_constants
:
658 /* already processed */
660 case decode_find_mixed
:
661 /* string field with conditions. If this condition
662 eliminates the value then the compare is useful */
663 if (bit
->field
->conditions
!= NULL
)
665 insn_field_cond
*condition
;
666 int shift
= bit
->field
->last
- bit_nr
;
667 for (condition
= bit
->field
->conditions
;
668 condition
!= NULL
; condition
= condition
->next
)
670 switch (condition
->type
)
672 case insn_field_cond_value
:
673 switch (condition
->test
)
675 case insn_field_cond_ne
:
676 if (((condition
->value
>> shift
) & 1)
678 /* conditional field excludes the
682 case insn_field_cond_eq
:
683 if (((condition
->value
>> shift
) & 1)
685 /* conditional field requires the
691 case insn_field_cond_field
:
692 /* are these handled separatly? */
706 /* go through a gen-table's list of instruction formats looking for a
707 range of bits that meet the decode table RULEs requirements */
709 static opcode_field
*
710 gen_entry_find_opcode_field (insn_list
*insns
,
711 decode_table
*rule
, int string_only
)
713 opcode_field curr_opcode
;
714 ASSERT (rule
!= NULL
);
716 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
717 curr_opcode
.word_nr
= rule
->word_nr
;
718 curr_opcode
.first
= rule
->first
;
719 curr_opcode
.last
= rule
->last
;
721 /* Try to reduce the size of first..last in accordance with the
724 while (curr_opcode
.first
<= rule
->last
)
726 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
731 while (curr_opcode
.last
>= rule
->first
)
733 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
741 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
743 insn_word_entry
*fields
= entry
->insn
->word
[rule
->word_nr
];
744 opcode_field new_opcode
;
746 ASSERT (fields
!= NULL
);
748 /* find a start point for the opcode field */
749 new_opcode
.first
= rule
->first
;
750 while (new_opcode
.first
<= rule
->last
753 (insn_field_is_constant (fields
->bit
[new_opcode
.first
], rule
)
754 != field_constant_string
)) && (string_only
756 (insn_field_is_constant
758 bit
[new_opcode
.first
],
760 field_is_not_constant
)))
762 int new_first
= fields
->bit
[new_opcode
.first
]->last
+ 1;
763 ASSERT (new_first
> new_opcode
.first
);
764 new_opcode
.first
= new_first
;
766 ASSERT (new_opcode
.first
> rule
->last
768 && insn_field_is_constant (fields
->bit
[new_opcode
.first
],
769 rule
) == field_constant_string
)
771 && insn_field_is_constant (fields
->bit
[new_opcode
.first
],
774 /* find the end point for the opcode field */
775 new_opcode
.last
= rule
->last
;
776 while (new_opcode
.last
>= rule
->first
778 || insn_field_is_constant (fields
->bit
[new_opcode
.last
],
779 rule
) != field_constant_string
)
781 || !insn_field_is_constant (fields
->bit
[new_opcode
.last
],
784 int new_last
= fields
->bit
[new_opcode
.last
]->first
- 1;
785 ASSERT (new_last
< new_opcode
.last
);
786 new_opcode
.last
= new_last
;
788 ASSERT (new_opcode
.last
< rule
->first
790 && insn_field_is_constant (fields
->bit
[new_opcode
.last
],
791 rule
) == field_constant_string
)
793 && insn_field_is_constant (fields
->bit
[new_opcode
.last
],
796 /* now see if our current opcode needs expanding to include the
797 interesting fields within this instruction */
798 if (new_opcode
.first
<= rule
->last
799 && curr_opcode
.first
> new_opcode
.first
)
800 curr_opcode
.first
= new_opcode
.first
;
801 if (new_opcode
.last
>= rule
->first
802 && curr_opcode
.last
< new_opcode
.last
)
803 curr_opcode
.last
= new_opcode
.last
;
808 /* did the final opcode field end up being empty? */
809 if (curr_opcode
.first
> curr_opcode
.last
)
813 ASSERT (curr_opcode
.last
>= rule
->first
);
814 ASSERT (curr_opcode
.first
<= rule
->last
);
815 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
817 /* Ensure that, for the non string only case, the opcode includes
818 the range forced_first .. forced_last */
819 if (!string_only
&& curr_opcode
.first
> rule
->force_first
)
821 curr_opcode
.first
= rule
->force_first
;
823 if (!string_only
&& curr_opcode
.last
< rule
->force_last
)
825 curr_opcode
.last
= rule
->force_last
;
828 /* For the string only case, force just the lower bound (so that the
829 shift can be eliminated) */
830 if (string_only
&& rule
->force_last
== options
.insn_bit_size
- 1)
832 curr_opcode
.last
= options
.insn_bit_size
- 1;
835 /* handle any special cases */
838 case normal_decode_rule
:
839 /* let the above apply */
840 curr_opcode
.nr_opcodes
=
841 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
844 curr_opcode
.is_boolean
= 1;
845 curr_opcode
.boolean_constant
= rule
->constant
;
846 curr_opcode
.nr_opcodes
= 2;
851 opcode_field
*new_field
= ZALLOC (opcode_field
);
852 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
859 gen_entry_insert_insn (gen_entry
*table
,
860 insn_entry
* old_insn
,
862 int new_nr_prefetched_words
,
863 int new_opcode_nr
, opcode_bits
*new_bits
)
865 gen_entry
**entry
= &table
->entries
;
867 /* find the new table for this entry */
868 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
870 entry
= &(*entry
)->sibling
;
873 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
875 /* insert the missing entry */
876 gen_entry
*new_entry
= ZALLOC (gen_entry
);
877 new_entry
->sibling
= (*entry
);
878 (*entry
) = new_entry
;
881 new_entry
->top
= table
->top
;
882 new_entry
->opcode_nr
= new_opcode_nr
;
883 new_entry
->word_nr
= new_word_nr
;
884 new_entry
->expanded_bits
= new_bits
;
885 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
886 new_entry
->parent
= table
;
887 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
889 /* ASSERT new_bits == cur_entry bits */
890 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
891 insn_list_insert (&(*entry
)->insns
, &(*entry
)->nr_insns
, old_insn
, NULL
, /* expanded_bits - only in final list */
892 NULL
, /* opcodes - only in final list */
893 new_nr_prefetched_words
, /* for this table */
894 report_duplicate_insns
);
899 gen_entry_expand_opcode (gen_entry
*table
,
900 insn_entry
* instruction
,
901 int bit_nr
, int opcode_nr
, opcode_bits
*bits
)
903 if (bit_nr
> table
->opcode
->last
)
905 /* Only include the hardwired bit information with an entry IF
906 that entry (and hence its functions) are being duplicated. */
907 if (options
.trace
.insn_expansion
)
909 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
910 notify (NULL
, ": insert %d - %s.%s%s\n",
912 instruction
->format_name
,
914 (table
->opcode_rule
->
915 with_duplicates
? " (duplicated)" : ""));
917 if (table
->opcode_rule
->with_duplicates
)
919 gen_entry_insert_insn (table
, instruction
,
920 table
->opcode
->word_nr
,
921 table
->nr_prefetched_words
, opcode_nr
, bits
);
925 gen_entry_insert_insn (table
, instruction
,
926 table
->opcode
->word_nr
,
927 table
->nr_prefetched_words
, opcode_nr
, NULL
);
932 insn_word_entry
*word
= instruction
->word
[table
->opcode
->word_nr
];
933 insn_field_entry
*field
= word
->bit
[bit_nr
]->field
;
934 int last_pos
= ((field
->last
< table
->opcode
->last
)
935 ? field
->last
: table
->opcode
->last
);
936 int first_pos
= ((field
->first
> table
->opcode
->first
)
937 ? field
->first
: table
->opcode
->first
);
938 int width
= last_pos
- first_pos
+ 1;
944 val
= sub_val (field
->val_int
, field
->last
, first_pos
, last_pos
);
945 gen_entry_expand_opcode (table
, instruction
,
947 ((opcode_nr
<< width
) | val
), bits
);
952 if (field
->type
== insn_field_reserved
)
953 gen_entry_expand_opcode (table
, instruction
,
955 ((opcode_nr
<< width
)), bits
);
959 int last_val
= (table
->opcode
->is_boolean
? 2 : (1 << width
));
960 for (val
= 0; val
< last_val
; val
++)
962 /* check to see if the value has been precluded
963 (by a conditional) in some way */
965 insn_field_cond
*condition
;
966 for (condition
= field
->conditions
, is_precluded
= 0;
967 condition
!= NULL
&& !is_precluded
;
968 condition
= condition
->next
)
970 switch (condition
->type
)
972 case insn_field_cond_value
:
975 sub_val (condition
->value
, field
->last
,
976 first_pos
, last_pos
);
977 switch (condition
->test
)
979 case insn_field_cond_ne
:
983 case insn_field_cond_eq
:
990 case insn_field_cond_field
:
995 /* Try to find a value for the
996 conditional by looking back through
997 the previously defined bits for one
998 that covers the designated
1000 for (bit
= bits
; bit
!= NULL
; bit
= bit
->next
)
1002 if (bit
->field
->word_nr
==
1003 condition
->field
->word_nr
1004 && bit
->first
<= condition
->field
->first
1005 && bit
->last
>= condition
->field
->last
)
1007 /* the bit field fully specified
1008 the conditional field's value */
1009 value
= sub_val (bit
->value
, bit
->last
,
1016 /* Try to find a value by looking
1017 through this and previous tables */
1021 t
->parent
!= NULL
; t
= t
->parent
)
1023 if (t
->parent
->opcode
->word_nr
==
1024 condition
->field
->word_nr
1025 && t
->parent
->opcode
->first
<=
1026 condition
->field
->first
1027 && t
->parent
->opcode
->last
>=
1028 condition
->field
->last
)
1030 /* the table entry fully
1031 specified the condition
1033 /* extract the field's value
1036 sub_val (t
->opcode_nr
,
1037 t
->parent
->opcode
->last
,
1038 condition
->field
->first
,
1039 condition
->field
->last
);
1040 /* this is a requirement of
1042 refering to another field */
1043 ASSERT ((condition
->field
->first
-
1044 condition
->field
->last
) ==
1045 (first_pos
- last_pos
));
1047 ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
1048 value
, t
->opcode_nr
,
1049 t
->parent
->opcode
->last
,
1050 condition
->field
->first
,
1051 condition
->field
->last
);
1055 if (bit
== NULL
&& t
== NULL
)
1056 error (instruction
->line
,
1057 "Conditional `%s' of field `%s' isn't expanded",
1058 condition
->string
, field
->val_string
);
1059 switch (condition
->test
)
1061 case insn_field_cond_ne
:
1065 case insn_field_cond_eq
:
1076 /* Only add additional hardwired bit
1077 information if the entry is not going to
1078 later be combined */
1079 if (table
->opcode_rule
->with_combine
)
1081 gen_entry_expand_opcode (table
, instruction
,
1083 ((opcode_nr
<< width
) |
1088 opcode_bits
*new_bits
=
1089 new_opcode_bits (bits
, val
,
1090 first_pos
, last_pos
,
1093 gen_entry_expand_opcode (table
, instruction
,
1095 ((opcode_nr
<< width
) |
1107 gen_entry_insert_expanding (gen_entry
*table
, insn_entry
* instruction
)
1109 gen_entry_expand_opcode (table
,
1111 table
->opcode
->first
, 0, table
->expanded_bits
);
1116 insns_match_format_names (insn_list
*insns
, filter
*format_names
)
1118 if (format_names
!= NULL
)
1121 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1123 if (i
->insn
->format_name
!= NULL
1124 && !filter_is_member (format_names
, i
->insn
->format_name
))
1132 table_matches_path (gen_entry
*table
, decode_path_list
*paths
)
1136 while (paths
!= NULL
)
1138 gen_entry
*entry
= table
;
1139 decode_path
*path
= paths
->path
;
1142 if (entry
== NULL
&& path
== NULL
)
1144 if (entry
== NULL
|| path
== NULL
)
1146 if (entry
->opcode_nr
!= path
->opcode_nr
)
1148 entry
= entry
->parent
;
1149 path
= path
->parent
;
1151 paths
= paths
->next
;
1158 insns_match_conditions (insn_list
*insns
, decode_cond
*conditions
)
1160 if (conditions
!= NULL
)
1163 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1166 for (cond
= conditions
; cond
!= NULL
; cond
= cond
->next
)
1169 if (i
->insn
->nr_words
<= cond
->word_nr
)
1171 for (bit_nr
= 0; bit_nr
< options
.insn_bit_size
; bit_nr
++)
1173 if (!cond
->mask
[bit_nr
])
1175 if (!i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->mask
)
1177 if ((i
->insn
->word
[cond
->word_nr
]->bit
[bit_nr
]->value
1178 == cond
->value
[bit_nr
]) == !cond
->is_equal
)
1188 insns_match_nr_words (insn_list
*insns
, int nr_words
)
1191 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1193 if (i
->insn
->nr_words
< nr_words
)
1200 insn_list_cmp (insn_list
*l
, insn_list
*r
)
1205 if (l
== NULL
&& r
== NULL
)
1211 if (l
->insn
!= r
->insn
)
1212 return -1; /* somewhat arbitrary at present */
1213 /* skip this insn */
1215 while (l
!= NULL
&& l
->insn
== insn
)
1217 while (r
!= NULL
&& r
->insn
== insn
)
1225 gen_entry_expand_insns (gen_entry
*table
)
1227 decode_table
*opcode_rule
;
1229 ASSERT (table
->nr_insns
>= 1);
1231 /* determine a valid opcode */
1232 for (opcode_rule
= table
->opcode_rule
;
1233 opcode_rule
!= NULL
; opcode_rule
= opcode_rule
->next
)
1235 char *discard_reason
;
1236 if (table
->top
->model
!= NULL
1237 && opcode_rule
->model_names
!= NULL
1238 && !filter_is_member (opcode_rule
->model_names
,
1239 table
->top
->model
->name
))
1241 /* the rule isn't applicable to this processor */
1242 discard_reason
= "wrong model";
1244 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1246 /* for safety, require a pre-codition when attempting to
1247 apply a rule to a single instruction */
1248 discard_reason
= "need pre-condition when nr-insn == 1";
1250 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1252 /* Little point in expanding a single instruction when we're
1253 not duplicating the semantic functions that this table
1255 discard_reason
= "need duplication with nr-insns == 1";
1258 if (!insns_match_format_names
1259 (table
->insns
, opcode_rule
->format_names
))
1261 discard_reason
= "wrong format name";
1263 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1265 discard_reason
= "wrong nr words";
1267 else if (!table_matches_path (table
, opcode_rule
->paths
))
1269 discard_reason
= "path failed";
1272 if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1274 discard_reason
= "condition failed";
1278 discard_reason
= "no opcode field";
1279 table
->opcode
= gen_entry_find_opcode_field (table
->insns
,
1281 table
->nr_insns
== 1 /*string-only */
1283 if (table
->opcode
!= NULL
)
1285 table
->opcode_rule
= opcode_rule
;
1290 if (options
.trace
.rule_rejection
)
1292 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1293 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1297 /* did we find anything */
1298 if (opcode_rule
== NULL
)
1300 /* the decode table failed, this set of instructions haven't
1301 been uniquely identified */
1302 if (table
->nr_insns
> 1)
1304 print_gen_entry_insns (table
, warning
,
1305 "was not uniquely decoded",
1306 "decodes to the same entry");
1312 /* Determine the number of words that must have been prefetched for
1313 this table to function */
1314 if (table
->parent
== NULL
)
1315 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1316 else if (table
->opcode_rule
->word_nr
+ 1 >
1317 table
->parent
->nr_prefetched_words
)
1318 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1320 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1322 /* back link what we found to its parent */
1323 if (table
->parent
!= NULL
)
1325 ASSERT (table
->parent
->opcode
!= NULL
);
1326 table
->opcode
->parent
= table
->parent
->opcode
;
1329 /* report the rule being used to expand the instructions */
1330 if (options
.trace
.rule_selection
)
1332 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1334 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1335 table
->opcode
->word_nr
,
1336 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1337 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1338 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1339 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1340 table
->opcode
->nr_opcodes
, table
->nr_entries
);
1343 /* expand the raw instructions according to the opcode */
1346 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1348 if (options
.trace
.insn_expansion
)
1350 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1351 notify (NULL
, ": expand - %s.%s\n",
1352 entry
->insn
->format_name
, entry
->insn
->name
);
1354 gen_entry_insert_expanding (table
, entry
->insn
);
1358 /* dump the results */
1359 if (options
.trace
.entries
)
1362 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1365 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1366 notify (NULL
, ": %d - entries %d -",
1367 entry
->opcode_nr
, entry
->nr_insns
);
1368 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1369 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1370 notify (NULL
, "\n");
1374 /* perform a combine pass if needed */
1375 if (table
->opcode_rule
->with_combine
)
1378 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1380 if (entry
->combined_parent
== NULL
)
1382 gen_entry
**last
= &entry
->combined_next
;
1384 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1386 if (alt
->combined_parent
== NULL
1387 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1389 alt
->combined_parent
= entry
;
1391 last
= &alt
->combined_next
;
1396 if (options
.trace
.combine
)
1400 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1402 if (entry
->combined_parent
== NULL
)
1405 gen_entry
*duplicate
;
1407 print_gen_entry_path (table
->opcode_rule
->line
, entry
,
1409 for (duplicate
= entry
->combined_next
; duplicate
!= NULL
;
1410 duplicate
= duplicate
->combined_next
)
1412 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1414 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1415 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1417 notify (NULL
, " %s.%s",
1418 l
->insn
->format_name
, l
->insn
->name
);
1420 notify (NULL
, "\n");
1423 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1425 ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1426 table
->opcode
->word_nr
, i2target (options
.hi_bit_nr
,
1427 table
->opcode
->first
),
1428 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1429 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1430 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1431 table
->opcode
->nr_opcodes
, table
->nr_entries
, nr_unique
);
1435 /* Check that the rule did more than re-arange the order of the
1439 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1441 if (entry
->combined_parent
== NULL
)
1443 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1445 print_gen_entry_path (table
->opcode_rule
->line
, table
,
1448 ": Applying rule just copied all instructions\n");
1449 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1456 /* if some form of expanded table, fill in the missing dots */
1457 switch (table
->opcode_rule
->gen
)
1459 case padded_switch_gen
:
1461 case goto_switch_gen
:
1462 if (!table
->opcode
->is_boolean
)
1464 gen_entry
**entry
= &table
->entries
;
1465 gen_entry
*illegals
= NULL
;
1466 gen_entry
**last_illegal
= &illegals
;
1468 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1470 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1472 /* missing - insert it under our feet at *entry */
1473 gen_entry_insert_insn (table
, table
->top
->isa
->illegal_insn
, table
->opcode
->word_nr
, 0, /* nr_prefetched_words == 0 for invalid */
1475 ASSERT ((*entry
) != NULL
);
1476 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1477 (*last_illegal
) = *entry
;
1478 (*last_illegal
)->combined_parent
= illegals
;
1479 last_illegal
= &(*last_illegal
)->combined_next
;
1481 entry
= &(*entry
)->sibling
;
1484 /* oops, will have pointed the first illegal insn back to
1485 its self. Fix this */
1486 if (illegals
!= NULL
)
1487 illegals
->combined_parent
= NULL
;
1496 /* and do the same for the newly created sub entries but *only*
1497 expand entries that haven't been combined. */
1500 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1502 if (entry
->combined_parent
== NULL
)
1504 gen_entry_expand_insns (entry
);
1511 gen_tables_expand_insns (gen_table
*gen
)
1514 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1516 gen_entry_expand_insns (entry
->table
);
1521 /* create a list of all the semantic functions that need to be
1522 generated. Eliminate any duplicates. Verify that the decode stage
1526 make_gen_semantics_list (lf
*file
, gen_entry
*entry
, int depth
, void *data
)
1528 gen_table
*gen
= (gen_table
*) data
;
1530 /* Not interested in an entrie that have been combined into some
1531 other entry at the same level */
1532 if (entry
->combined_parent
!= NULL
)
1535 /* a leaf should contain exactly one instruction. If not the decode
1537 ASSERT (entry
->nr_insns
== 1);
1539 /* Enter this instruction into the list of semantic functions. */
1540 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1542 entry
->expanded_bits
,
1543 entry
->parent
->opcode
,
1544 entry
->insns
->nr_prefetched_words
,
1545 merge_duplicate_insns
);
1546 /* point the table entry at the real semantic function */
1547 ASSERT (insn
!= NULL
);
1548 entry
->insns
->semantic
= insn
;
1553 gen_tables_expand_semantics (gen_table
*gen
)
1556 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1558 gen_entry_traverse_tree (NULL
, entry
->table
, 1, /* depth */
1559 NULL
, /* start-handler */
1560 make_gen_semantics_list
, /* leaf-handler */
1561 NULL
, /* end-handler */
1572 dump_opcode_field (lf
*file
,
1574 opcode_field
*field
, char *suffix
, int levels
)
1576 lf_printf (file
, "%s(opcode_field *) 0x%lx", prefix
, (long) field
);
1577 if (levels
&& field
!= NULL
)
1579 lf_indent (file
, +1);
1580 lf_printf (file
, "\n(first %d)", field
->first
);
1581 lf_printf (file
, "\n(last %d)", field
->last
);
1582 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1583 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1584 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1585 dump_opcode_field (file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1586 lf_indent (file
, -1);
1588 lf_printf (file
, "%s", suffix
);
1593 dump_opcode_bits (lf
*file
,
1594 char *prefix
, opcode_bits
*bits
, char *suffix
, int levels
)
1596 lf_printf (file
, "%s(opcode_bits *) 0x%lx", prefix
, (long) bits
);
1598 if (levels
&& bits
!= NULL
)
1600 lf_indent (file
, +1);
1601 lf_printf (file
, "\n(value %d)", bits
->value
);
1602 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1603 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1604 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1605 lf_indent (file
, -1);
1607 lf_printf (file
, "%s", suffix
);
1613 dump_insn_list (lf
*file
, char *prefix
, insn_list
*entry
, char *suffix
)
1615 lf_printf (file
, "%s(insn_list *) 0x%lx", prefix
, (long) entry
);
1619 lf_indent (file
, +1);
1620 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1621 lf_printf (file
, "\n(next 0x%lx)", (long) entry
->next
);
1622 lf_indent (file
, -1);
1624 lf_printf (file
, "%s", suffix
);
1629 dump_insn_word_entry_list_entries (lf
*file
,
1631 insn_list
*entry
, char *suffix
)
1633 lf_printf (file
, "%s", prefix
);
1634 while (entry
!= NULL
)
1636 dump_insn_list (file
, "\n(", entry
, ")");
1637 entry
= entry
->next
;
1639 lf_printf (file
, "%s", suffix
);
1644 dump_gen_entry (lf
*file
,
1645 char *prefix
, gen_entry
*table
, char *suffix
, int levels
)
1648 lf_printf (file
, "%s(gen_entry *) 0x%lx", prefix
, (long) table
);
1650 if (levels
&& table
!=NULL
)
1653 lf_indent (file
, +1);
1654 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1655 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1656 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")",
1658 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1659 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
,
1661 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1662 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1663 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1664 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")",
1666 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1667 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1668 lf_indent (file
, -1);
1670 lf_printf (file
, "%s", suffix
);
1674 dump_gen_list (lf
*file
,
1675 char *prefix
, gen_list
*entry
, char *suffix
, int levels
)
1677 while (entry
!= NULL
)
1679 lf_printf (file
, "%s(gen_list *) 0x%lx", prefix
, (long) entry
);
1680 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1681 lf_printf (file
, "\n(next (gen_list *) 0x%lx)", (long) entry
->next
);
1682 lf_printf (file
, "%s", suffix
);
1688 dump_gen_table (lf
*file
,
1689 char *prefix
, gen_table
*gen
, char *suffix
, int levels
)
1691 lf_printf (file
, "%s(gen_table *) 0x%lx", prefix
, (long) gen
);
1692 lf_printf (file
, "\n(isa (insn_table *) 0x%lx)", (long) gen
->isa
);
1693 lf_printf (file
, "\n(rules (decode_table *) 0x%lx)", (long) gen
->rules
);
1694 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1695 lf_printf (file
, "%s", suffix
);
1699 igen_options options
;
1702 main (int argc
, char **argv
)
1704 decode_table
*decode_rules
;
1705 insn_table
*instructions
;
1711 "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1713 INIT_OPTIONS (options
);
1715 filter_parse (&options
.flags_filter
, argv
[1]);
1717 options
.hi_bit_nr
= a2i (argv
[2]);
1718 options
.insn_bit_size
= a2i (argv
[3]);
1719 options
.insn_specifying_widths
= a2i (argv
[4]);
1720 ASSERT (options
.hi_bit_nr
< options
.insn_bit_size
);
1722 instructions
= load_insn_table (argv
[6], NULL
);
1723 decode_rules
= load_decode_table (argv
[5]);
1724 gen
= make_gen_tables (instructions
, decode_rules
);
1726 gen_tables_expand_insns (gen
);
1728 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1730 dump_gen_table (l
, "(", gen
, ")\n", -1);