1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "ld-decode.h"
33 sub_val (insn_uint val
,
38 return ((val
>> (val_last_pos
- last_pos
))
39 & (((insn_uint
)1 << (last_pos
- first_pos
+ 1)) - 1));
43 update_depth (lf
*file
,
48 int *max_depth
= (int*)data
;
49 if (*max_depth
< depth
)
55 gen_entry_depth (gen_entry
*table
)
58 gen_entry_traverse_tree (NULL
,
70 print_gen_entry_path (line_ref
*line
,
74 if (table
->parent
== NULL
)
76 if (table
->top
->model
!= NULL
)
77 print (line
, "%s", table
->top
->model
->name
);
83 print_gen_entry_path (line
, table
->parent
, print
);
84 print (NULL
, ".%d", table
->opcode_nr
);
89 print_gen_entry_insns (gen_entry
*table
,
96 message
= first_message
;
97 for (i
= table
->insns
; i
!= NULL
; i
= i
->next
)
99 insn_entry
*insn
= i
->insn
;
100 print_gen_entry_path (insn
->line
, table
, print
);
101 print (NULL
, ": %s.%s %s\n",
105 if (next_message
!= NULL
)
106 message
= next_message
;
112 insn_field_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
117 if (l
== NULL
&& r
== NULL
)
118 return 0; /* all previous fields the same */
120 return -1; /* left shorter than right */
122 return +1; /* left longer than right */
124 bit_nr
< options
.insn_bit_size
;
127 if (l
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
129 if (r
->bit
[bit_nr
]->field
->type
!= insn_field_string
)
131 if (l
->bit
[bit_nr
]->field
->conditions
== NULL
)
133 if (r
->bit
[bit_nr
]->field
->conditions
== NULL
)
136 printf ("%s%s%s VS %s%s%s\n",
137 l
->bit
[bit_nr
]->field
->val_string
,
138 l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
? "=" : "!",
139 l
->bit
[bit_nr
]->field
->conditions
->string
,
140 r
->bit
[bit_nr
]->field
->val_string
,
141 r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
? "=" : "!",
142 r
->bit
[bit_nr
]->field
->conditions
->string
);
143 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
144 && r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
146 if (l
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
147 && r
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
148 /* somewhat arbitrary */
150 int cmp
= strcmp (l
->bit
[bit_nr
]->field
->conditions
->string
,
151 r
->bit
[bit_nr
]->field
->conditions
->string
);
157 if (l
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
159 if (r
->bit
[bit_nr
]->field
->conditions
->type
== insn_field_cond_field
)
161 /* The case of both fields having constant values should have
162 already have been handled because such fields are converted
163 into normal constant fields. */
166 if (l
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
167 return +1; /* left = only */
168 if (r
->bit
[bit_nr
]->field
->conditions
->test
== insn_field_cond_eq
)
169 return -1; /* right = only */
170 /* FIXME: Need to some what arbitrarily order conditional lists */
180 insn_word_cmp (insn_word_entry
*l
, insn_word_entry
*r
)
185 if (l
== NULL
&& r
== NULL
)
186 return 0; /* all previous fields the same */
188 return -1; /* left shorter than right */
190 return +1; /* left longer than right */
192 bit_nr
< options
.insn_bit_size
;
195 if (l
->bit
[bit_nr
]->mask
< r
->bit
[bit_nr
]->mask
)
197 if (l
->bit
[bit_nr
]->mask
> r
->bit
[bit_nr
]->mask
)
199 if (l
->bit
[bit_nr
]->value
< r
->bit
[bit_nr
]->value
)
201 if (l
->bit
[bit_nr
]->value
> r
->bit
[bit_nr
]->value
)
211 opcode_bit_cmp (opcode_bits
*l
,
214 if (l
== NULL
&& r
== NULL
)
215 return 0; /* all previous bits the same */
217 return -1; /* left shorter than right */
219 return +1; /* left longer than right */
220 /* most significant word */
221 if (l
->field
->word_nr
< r
->field
->word_nr
)
222 return +1; /* left has more significant word */
223 if (l
->field
->word_nr
> r
->field
->word_nr
)
224 return -1; /* right has more significant word */
225 /* most significant bit? */
226 if (l
->first
< r
->first
)
227 return +1; /* left as more significant bit */
228 if (l
->first
> r
->first
)
229 return -1; /* right as more significant bit */
231 if (l
->last
< r
->last
)
232 return +1; /* left as less bits */
233 if (l
->last
> r
->last
)
234 return -1; /* right as less bits */
236 if (l
->value
< r
->value
)
238 if (l
->value
> r
->value
)
246 opcode_bits_cmp (opcode_bits
*l
,
252 if (l
== NULL
&& r
== NULL
)
253 return 0; /* all previous bits the same */
254 cmp
= opcode_bit_cmp (l
, r
);
264 new_opcode_bits (opcode_bits
*old_bits
,
268 insn_field_entry
*field
,
269 opcode_field
*opcode
)
271 opcode_bits
*new_bits
= ZALLOC (opcode_bits
);
272 new_bits
->field
= field
;
273 new_bits
->value
= value
;
274 new_bits
->first
= first
;
275 new_bits
->last
= last
;
276 new_bits
->opcode
= opcode
;
278 if (old_bits
!= NULL
)
280 opcode_bits
*new_list
;
281 opcode_bits
**last
= &new_list
;
282 new_list
= new_opcode_bits (old_bits
->next
,
288 while (*last
!= NULL
)
290 int cmp
= opcode_bit_cmp (new_bits
, *last
);
291 if (cmp
< 0) /* new < new_list */
297 ERROR ("Duplicated insn bits in list");
299 last
= &(*last
)->next
;
301 new_bits
->next
= *last
;
311 /* Same as strcmp(). */
313 format_name_cmp (const char *l
, const char *r
)
315 if (l
== NULL
&& r
== NULL
)
317 if (l
!= NULL
&& r
== NULL
)
319 if (l
== NULL
&& r
!= NULL
)
321 return strcmp (l
, r
);
326 merge_duplicate_insns
,
327 report_duplicate_insns
,
328 } duplicate_insn_actions
;
331 insn_list_insert (insn_list
**cur_insn_ptr
,
334 opcode_bits
*expanded_bits
,
335 opcode_field
*opcodes
,
336 int nr_prefetched_words
,
337 duplicate_insn_actions duplicate_action
)
339 /* insert it according to the order of the fields & bits */
340 for (; (*cur_insn_ptr
) != NULL
; cur_insn_ptr
= &(*cur_insn_ptr
)->next
)
344 /* key#1 sort according to the constant fields of each instruction */
345 cmp
= insn_word_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
351 /* key#2 sort according to the expanded bits of each instruction */
352 cmp
= opcode_bits_cmp (expanded_bits
, (*cur_insn_ptr
)->expanded_bits
);
358 /* key#3 sort according to the non-constant fields of each instruction */
359 cmp
= insn_field_cmp (insn
->words
, (*cur_insn_ptr
)->insn
->words
);
365 /* key#4 sort according to the format-name. If two apparently
366 identical instructions have unique format-names, then the
367 instructions are different. This is because the
368 format-name's use is overloaded, it not only indicates the
369 format name but also provides a unique semantic name for the
371 cmp
= format_name_cmp (insn
->format_name
, (*cur_insn_ptr
)->insn
->format_name
);
377 /* duplicate keys, report problem */
378 switch (duplicate_action
)
380 case report_duplicate_insns
:
381 /* It would appear that we have two instructions with the
382 same constant field values across all words and bits.
383 This error can also occure when insn_field_cmp() is
384 failing to differentiate between two instructions that
385 differ only in their conditional fields. */
387 "Two instructions with identical constant fields\n");
388 error ((*cur_insn_ptr
)->insn
->line
,
389 "Location of duplicate instruction\n");
390 case merge_duplicate_insns
:
391 /* Add the opcode path to the instructions list */
394 insn_opcodes
**last
= &(*cur_insn_ptr
)->opcodes
;
395 while (*last
!= NULL
)
397 last
= &(*last
)->next
;
399 (*last
) = ZALLOC (insn_opcodes
);
400 (*last
)->opcode
= opcodes
;
402 /* Use the larger nr_prefetched_words */
403 if ((*cur_insn_ptr
)->nr_prefetched_words
< nr_prefetched_words
)
404 (*cur_insn_ptr
)->nr_prefetched_words
= nr_prefetched_words
;
405 return (*cur_insn_ptr
);
410 /* create a new list entry and insert it */
412 insn_list
*new_insn
= ZALLOC (insn_list
);
413 new_insn
->insn
= insn
;
414 new_insn
->expanded_bits
= expanded_bits
;
415 new_insn
->next
= (*cur_insn_ptr
);
416 new_insn
->nr_prefetched_words
= nr_prefetched_words
;
419 new_insn
->opcodes
= ZALLOC (insn_opcodes
);
420 new_insn
->opcodes
->opcode
= opcodes
;
422 (*cur_insn_ptr
) = new_insn
;
427 return (*cur_insn_ptr
);
432 gen_entry_traverse_tree (lf
*file
,
435 gen_entry_handler
*start
,
436 gen_entry_handler
*leaf
,
437 gen_entry_handler
*end
,
442 ASSERT (table
!= NULL
);
443 ASSERT (table
->opcode
!= NULL
);
444 ASSERT (table
->nr_entries
> 0);
445 ASSERT (table
->entries
!= 0);
448 if (start
!= NULL
&& depth
>= 0)
450 start (file
, table
, depth
, data
);
453 for (entry
= table
->entries
;
455 entry
= entry
->sibling
)
457 if (entry
->entries
!= NULL
&& depth
!= 0)
459 gen_entry_traverse_tree (file
, entry
, depth
+ 1,
460 start
, leaf
, end
, data
);
466 leaf (file
, entry
, depth
, data
);
471 if (end
!= NULL
&& depth
>= 0)
473 end (file
, table
, depth
, data
);
479 /* create a list element containing a single gen_table entry */
482 make_table (insn_table
*isa
,
487 gen_list
*entry
= ZALLOC (gen_list
);
488 entry
->table
= ZALLOC (gen_entry
);
489 entry
->table
->top
= entry
;
490 entry
->model
= model
;
492 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
495 || insn
->processors
== NULL
496 || filter_is_member (insn
->processors
, model
->name
))
498 insn_list_insert (&entry
->table
->insns
,
499 &entry
->table
->nr_insns
,
501 NULL
, /* expanded_bits - none yet */
502 NULL
, /* opcodes - none yet */
503 0, /* nr_prefetched_words - none yet */
504 report_duplicate_insns
);
507 entry
->table
->opcode_rule
= rules
;
513 make_gen_tables (insn_table
*isa
,
516 gen_table
*gen
= ZALLOC (gen_table
);
519 if (options
.gen
.multi_sim
)
521 gen_list
**last
= &gen
->tables
;
524 if (options
.model_filter
!= NULL
)
525 processors
= options
.model_filter
;
527 processors
= isa
->model
->processors
;
528 for (model
= isa
->model
->models
;
532 if (filter_is_member (processors
, model
->name
))
534 *last
= make_table (isa
, rules
, model
);
535 last
= &(*last
)->next
;
541 gen
->tables
= make_table (isa
, rules
, NULL
);
547 /****************************************************************/
551 field_is_not_constant
= 0,
552 field_constant_int
= 1,
553 field_constant_reserved
= 2,
554 field_constant_string
= 3
555 } constant_field_types
;
557 static constant_field_types
558 insn_field_is_constant (insn_field
*field
,
564 /* field is an integer */
565 return field_constant_int
;
566 case insn_field_reserved
:
567 /* field is `/' and treating that as a constant */
568 if (rule
->with_zero_reserved
)
569 return field_constant_reserved
;
571 return field_is_not_constant
;
572 case insn_field_wild
:
573 return field_is_not_constant
; /* never constant */
574 case insn_field_string
:
575 /* field, though variable, is on the list of forced constants */
576 if (filter_is_member (rule
->constant_field_names
, field
->val_string
))
577 return field_constant_string
;
579 return field_is_not_constant
;
581 ERROR ("Internal error");
582 return field_is_not_constant
;
587 /****************************************************************/
590 /* Is the bit, according to the decode rule, identical across all the
593 insns_bit_useless (insn_list
*insns
,
599 int is_useless
= 1; /* cleared if something actually found */
601 /* check the instructions for some constant value in at least one of
603 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
605 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
606 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
607 switch (bit
->field
->type
)
609 case insn_field_invalid
:
612 case insn_field_wild
:
613 case insn_field_reserved
:
614 /* neither useless or useful - ignore */
617 switch (rule
->search
)
619 case decode_find_strings
:
620 /* an integer isn't a string */
622 case decode_find_constants
:
623 case decode_find_mixed
:
624 /* an integer is useful if its value isn't the same
625 between all instructions. The first time through the
626 value is saved, the second time through (if the
627 values differ) it is marked as useful. */
630 else if (value
!= bit
->value
)
635 case insn_field_string
:
636 switch (rule
->search
)
638 case decode_find_strings
:
639 /* at least one string, keep checking */
642 case decode_find_constants
:
643 case decode_find_mixed
:
644 if (filter_is_member (rule
->constant_field_names
,
645 bit
->field
->val_string
))
646 /* a string field forced to constant? */
648 else if (rule
->search
== decode_find_constants
)
649 /* the string field isn't constant */
656 /* Given only one constant value has been found, check through all
657 the instructions to see if at least one conditional makes it
659 if (value
>= 0 && is_useless
)
661 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
663 insn_word_entry
*word
= entry
->insn
->word
[rule
->word_nr
];
664 insn_bit_entry
*bit
= word
->bit
[bit_nr
];
665 switch (bit
->field
->type
)
667 case insn_field_invalid
:
670 case insn_field_wild
:
671 case insn_field_reserved
:
673 /* already processed */
675 case insn_field_string
:
676 switch (rule
->search
)
678 case decode_find_strings
:
679 case decode_find_constants
:
680 /* already processed */
682 case decode_find_mixed
:
683 /* string field with conditions. If this condition
684 eliminates the value then the compare is useful */
685 if (bit
->field
->conditions
!= NULL
)
687 insn_field_cond
*condition
;
688 int shift
= bit
->field
->last
- bit_nr
;
689 for (condition
= bit
->field
->conditions
;
691 condition
= condition
->next
)
693 switch (condition
->type
)
695 case insn_field_cond_value
:
696 switch (condition
->test
)
698 case insn_field_cond_ne
:
699 if (((condition
->value
>> shift
) & 1)
701 /* conditional field excludes the
705 case insn_field_cond_eq
:
706 if (((condition
->value
>> shift
) & 1)
708 /* conditional field requires the
714 case insn_field_cond_field
:
715 /* are these handled separatly? */
729 /* go through a gen-table's list of instruction formats looking for a
730 range of bits that meet the decode table RULEs requirements */
732 static opcode_field
*
733 gen_entry_find_opcode_field (insn_list
*insns
,
737 opcode_field curr_opcode
;
738 ASSERT (rule
!= NULL
);
740 memset (&curr_opcode
, 0, sizeof (curr_opcode
));
741 curr_opcode
.word_nr
= rule
->word_nr
;
742 curr_opcode
.first
= rule
->first
;
743 curr_opcode
.last
= rule
->last
;
745 /* Try to reduce the size of first..last in accordance with the
748 while (curr_opcode
.first
<= rule
->last
)
750 if (insns_bit_useless (insns
, rule
, curr_opcode
.first
))
751 curr_opcode
.first
++;
755 while (curr_opcode
.last
>= rule
->first
)
757 if (insns_bit_useless (insns
, rule
, curr_opcode
.last
))
765 for (entry
= insns
; entry
!= NULL
; entry
= entry
->next
)
767 insn_word_entry
*fields
= entry
->insn
->word
[rule
->word_nr
];
768 opcode_field new_opcode
;
770 ASSERT (fields
!= NULL
);
772 /* find a start point for the opcode field */
773 new_opcode
.first
= rule
->first
;
774 while (new_opcode
.first
<= rule
->last
776 || (insn_field_is_constant(fields
->bit
[new_opcode
.first
], rule
)
777 != field_constant_string
))
779 || (insn_field_is_constant(fields
->bit
[new_opcode
.first
], rule
)
780 == field_is_not_constant
)))
782 int new_first
= fields
->bit
[new_opcode
.first
]->last
+ 1;
783 ASSERT (new_first
> new_opcode
.first
);
784 new_opcode
.first
= new_first
;
786 ASSERT(new_opcode
.first
> rule
->last
788 && insn_field_is_constant(fields
->bit
[new_opcode
.first
],
789 rule
) == field_constant_string
)
791 && insn_field_is_constant(fields
->bit
[new_opcode
.first
],
794 /* find the end point for the opcode field */
795 new_opcode
.last
= rule
->last
;
796 while (new_opcode
.last
>= rule
->first
798 || insn_field_is_constant(fields
->bit
[new_opcode
.last
],
799 rule
) != field_constant_string
)
801 || !insn_field_is_constant(fields
->bit
[new_opcode
.last
],
804 int new_last
= fields
->bit
[new_opcode
.last
]->first
- 1;
805 ASSERT (new_last
< new_opcode
.last
);
806 new_opcode
.last
= new_last
;
808 ASSERT(new_opcode
.last
< rule
->first
810 && insn_field_is_constant(fields
->bit
[new_opcode
.last
],
811 rule
) == field_constant_string
)
813 && insn_field_is_constant(fields
->bit
[new_opcode
.last
],
816 /* now see if our current opcode needs expanding to include the
817 interesting fields within this instruction */
818 if (new_opcode
.first
<= rule
->last
819 && curr_opcode
.first
> new_opcode
.first
)
820 curr_opcode
.first
= new_opcode
.first
;
821 if (new_opcode
.last
>= rule
->first
822 && curr_opcode
.last
< new_opcode
.last
)
823 curr_opcode
.last
= new_opcode
.last
;
828 /* did the final opcode field end up being empty? */
829 if (curr_opcode
.first
> curr_opcode
.last
)
833 ASSERT (curr_opcode
.last
>= rule
->first
);
834 ASSERT (curr_opcode
.first
<= rule
->last
);
835 ASSERT (curr_opcode
.first
<= curr_opcode
.last
);
837 /* Ensure that, for the non string only case, the opcode includes
838 the range forced_first .. forced_last */
840 && curr_opcode
.first
> rule
->force_first
)
842 curr_opcode
.first
= rule
->force_first
;
845 && curr_opcode
.last
< rule
->force_last
)
847 curr_opcode
.last
= rule
->force_last
;
850 /* For the string only case, force just the lower bound (so that the
851 shift can be eliminated) */
853 && rule
->force_last
== options
.insn_bit_size
- 1)
855 curr_opcode
.last
= options
.insn_bit_size
- 1;
858 /* handle any special cases */
861 case normal_decode_rule
:
862 /* let the above apply */
863 curr_opcode
.nr_opcodes
=
864 (1 << (curr_opcode
.last
- curr_opcode
.first
+ 1));
867 curr_opcode
.is_boolean
= 1;
868 curr_opcode
.boolean_constant
= rule
->constant
;
869 curr_opcode
.nr_opcodes
= 2;
874 opcode_field
*new_field
= ZALLOC (opcode_field
);
875 memcpy (new_field
, &curr_opcode
, sizeof (opcode_field
));
882 gen_entry_insert_insn (gen_entry
*table
,
883 insn_entry
*old_insn
,
885 int new_nr_prefetched_words
,
887 opcode_bits
*new_bits
)
889 gen_entry
**entry
= &table
->entries
;
891 /* find the new table for this entry */
892 while ((*entry
) != NULL
&& (*entry
)->opcode_nr
< new_opcode_nr
)
894 entry
= &(*entry
)->sibling
;
897 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= new_opcode_nr
)
899 /* insert the missing entry */
900 gen_entry
*new_entry
= ZALLOC (gen_entry
);
901 new_entry
->sibling
= (*entry
);
902 (*entry
) = new_entry
;
905 new_entry
->top
= table
->top
;
906 new_entry
->opcode_nr
= new_opcode_nr
;
907 new_entry
->word_nr
= new_word_nr
;
908 new_entry
->expanded_bits
= new_bits
;
909 new_entry
->opcode_rule
= table
->opcode_rule
->next
;
910 new_entry
->parent
= table
;
911 new_entry
->nr_prefetched_words
= new_nr_prefetched_words
;
913 /* ASSERT new_bits == cur_entry bits */
914 ASSERT ((*entry
) != NULL
&& (*entry
)->opcode_nr
== new_opcode_nr
);
915 insn_list_insert (&(*entry
)->insns
,
918 NULL
, /* expanded_bits - only in final list */
919 NULL
, /* opcodes - only in final list */
920 new_nr_prefetched_words
, /* for this table */
921 report_duplicate_insns
);
926 gen_entry_expand_opcode (gen_entry
*table
,
927 insn_entry
*instruction
,
932 if (bit_nr
> table
->opcode
->last
)
934 /* Only include the hardwired bit information with an entry IF
935 that entry (and hence its functions) are being duplicated. */
936 if (options
.trace
.insn_expansion
)
938 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
939 notify (NULL
, ": insert %d - %s.%s%s\n",
941 instruction
->format_name
,
943 (table
->opcode_rule
->with_duplicates
? " (duplicated)" : ""));
945 if (table
->opcode_rule
->with_duplicates
)
947 gen_entry_insert_insn (table
, instruction
,
948 table
->opcode
->word_nr
,
949 table
->nr_prefetched_words
,
954 gen_entry_insert_insn (table
, instruction
,
955 table
->opcode
->word_nr
,
956 table
->nr_prefetched_words
,
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
)
966 : table
->opcode
->last
);
967 int first_pos
= ((field
->first
> table
->opcode
->first
)
969 : table
->opcode
->first
);
970 int width
= last_pos
- first_pos
+ 1;
976 val
= sub_val (field
->val_int
, field
->last
,
977 first_pos
, last_pos
);
978 gen_entry_expand_opcode (table
, instruction
,
980 ((opcode_nr
<< width
) | val
),
986 if (field
->type
== insn_field_reserved
)
987 gen_entry_expand_opcode (table
, instruction
,
989 ((opcode_nr
<< width
)),
994 int last_val
= (table
->opcode
->is_boolean
997 for (val
= 0; val
< last_val
; val
++)
999 /* check to see if the value has been precluded
1000 (by a conditional) in some way */
1002 insn_field_cond
*condition
;
1003 for (condition
= field
->conditions
, is_precluded
= 0;
1004 condition
!= NULL
&& !is_precluded
;
1005 condition
= condition
->next
)
1007 switch (condition
->type
)
1009 case insn_field_cond_value
:
1011 int value
= sub_val (condition
->value
, field
->last
,
1012 first_pos
, last_pos
);
1013 switch (condition
->test
)
1015 case insn_field_cond_ne
:
1019 case insn_field_cond_eq
:
1026 case insn_field_cond_field
:
1030 gen_entry
*t
= NULL
;
1031 /* Try to find a value for the
1032 conditional by looking back through
1033 the previously defined bits for one
1034 that covers the designated
1035 conditional field */
1040 if (bit
->field
->word_nr
== condition
->field
->word_nr
1041 && bit
->first
<= condition
->field
->first
1042 && bit
->last
>= condition
->field
->last
)
1044 /* the bit field fully specified
1045 the conditional field's value */
1046 value
= sub_val (bit
->value
, bit
->last
,
1047 condition
->field
->first
,
1048 condition
->field
->last
);
1051 /* Try to find a value by looking
1052 through this and previous tables */
1059 if (t
->parent
->opcode
->word_nr
== condition
->field
->word_nr
1060 && t
->parent
->opcode
->first
<= condition
->field
->first
1061 && t
->parent
->opcode
->last
>= condition
->field
->last
)
1063 /* the table entry fully
1064 specified the condition
1066 /* extract the field's value
1068 value
= sub_val (t
->opcode_nr
, t
->parent
->opcode
->last
,
1069 condition
->field
->first
, condition
->field
->last
);
1070 /* this is a requirement of
1072 refering to another field */
1073 ASSERT ((condition
->field
->first
- condition
->field
->last
)
1074 == (first_pos
- last_pos
));
1075 printf ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
1076 value
, t
->opcode_nr
, t
->parent
->opcode
->last
, condition
->field
->first
, condition
->field
->last
);
1080 if (bit
== NULL
&& t
== NULL
)
1081 error (instruction
->line
,
1082 "Conditional `%s' of field `%s' isn't expanded",
1083 condition
->string
, field
->val_string
);
1084 switch (condition
->test
)
1086 case insn_field_cond_ne
:
1090 case insn_field_cond_eq
:
1101 /* Only add additional hardwired bit
1102 information if the entry is not going to
1103 later be combined */
1104 if (table
->opcode_rule
->with_combine
)
1106 gen_entry_expand_opcode (table
, instruction
,
1108 ((opcode_nr
<< width
) | val
),
1113 opcode_bits
*new_bits
= new_opcode_bits (bits
, val
,
1114 first_pos
, last_pos
,
1117 gen_entry_expand_opcode (table
, instruction
,
1119 ((opcode_nr
<< width
) | val
),
1131 gen_entry_insert_expanding (gen_entry
*table
,
1132 insn_entry
*instruction
)
1134 gen_entry_expand_opcode (table
,
1136 table
->opcode
->first
,
1138 table
->expanded_bits
);
1143 insns_match_format_names (insn_list
*insns
,
1144 filter
*format_names
)
1146 if (format_names
!= NULL
)
1149 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1151 if ( i
->insn
->format_name
!= NULL
1152 && !filter_is_member (format_names
, i
->insn
->format_name
))
1160 table_matches_path (gen_entry
*table
,
1161 decode_path_list
*paths
)
1165 while (paths
!= NULL
)
1167 gen_entry
*entry
= table
;
1168 decode_path
*path
= paths
->path
;
1171 if (entry
== NULL
&& path
== NULL
)
1173 if (entry
== NULL
|| path
== NULL
)
1175 if (entry
->opcode_nr
!= path
->opcode_nr
)
1177 entry
= entry
->parent
;
1178 path
= path
->parent
;
1180 paths
= paths
->next
;
1187 insns_match_conditions (insn_list
*insns
,
1188 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
])
1219 insns_match_nr_words (insn_list
*insns
,
1223 for (i
= insns
; i
!= NULL
; i
= i
->next
)
1225 if (i
->insn
->nr_words
< nr_words
)
1232 insn_list_cmp (insn_list
*l
,
1238 if (l
== NULL
&& r
== NULL
)
1244 if (l
->insn
!= r
->insn
)
1245 return -1; /* somewhat arbitrary at present */
1246 /* skip this insn */
1248 while (l
!= NULL
&& l
->insn
== insn
)
1250 while (r
!= NULL
&& r
->insn
== insn
)
1258 gen_entry_expand_insns (gen_entry
*table
)
1260 decode_table
*opcode_rule
;
1262 ASSERT(table
->nr_insns
>= 1);
1264 /* determine a valid opcode */
1265 for (opcode_rule
= table
->opcode_rule
;
1266 opcode_rule
!= NULL
;
1267 opcode_rule
= opcode_rule
->next
)
1269 char *discard_reason
;
1270 if (table
->top
->model
!= NULL
1271 && opcode_rule
->model_names
!= NULL
1272 && !filter_is_member (opcode_rule
->model_names
,
1273 table
->top
->model
->name
))
1275 /* the rule isn't applicable to this processor */
1276 discard_reason
= "wrong model";
1278 else if (table
->nr_insns
== 1 && opcode_rule
->conditions
== NULL
)
1280 /* for safety, require a pre-codition when attempting to
1281 apply a rule to a single instruction */
1282 discard_reason
= "need pre-condition when nr-insn == 1";
1284 else if (table
->nr_insns
== 1 && !opcode_rule
->with_duplicates
)
1286 /* Little point in expanding a single instruction when we're
1287 not duplicating the semantic functions that this table
1289 discard_reason
= "need duplication with nr-insns == 1";
1291 else if (!insns_match_format_names (table
->insns
, opcode_rule
->format_names
))
1293 discard_reason
= "wrong format name";
1295 else if (!insns_match_nr_words (table
->insns
, opcode_rule
->word_nr
+ 1))
1297 discard_reason
= "wrong nr words";
1299 else if (!table_matches_path (table
, opcode_rule
->paths
))
1301 discard_reason
= "path failed";
1303 else if (!insns_match_conditions (table
->insns
, opcode_rule
->conditions
))
1305 discard_reason
= "condition failed";
1309 discard_reason
= "no opcode field";
1311 gen_entry_find_opcode_field (table
->insns
,
1313 table
->nr_insns
== 1/*string-only*/
1315 if (table
->opcode
!= NULL
)
1317 table
->opcode_rule
= opcode_rule
;
1322 if (options
.trace
.rule_rejection
)
1324 print_gen_entry_path (opcode_rule
->line
, table
, notify
);
1325 notify (NULL
, ": rule discarded - %s\n", discard_reason
);
1329 /* did we find anything */
1330 if (opcode_rule
== NULL
)
1332 /* the decode table failed, this set of instructions haven't
1333 been uniquely identified */
1334 if (table
->nr_insns
> 1)
1336 print_gen_entry_insns (table
, warning
,
1337 "was not uniquely decoded",
1338 "decodes to the same entry");
1344 /* Determine the number of words that must have been prefetched for
1345 this table to function */
1346 if (table
->parent
== NULL
)
1347 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1348 else if (table
->opcode_rule
->word_nr
+ 1 > table
->parent
->nr_prefetched_words
)
1349 table
->nr_prefetched_words
= table
->opcode_rule
->word_nr
+ 1;
1351 table
->nr_prefetched_words
= table
->parent
->nr_prefetched_words
;
1353 /* back link what we found to its parent */
1354 if (table
->parent
!= NULL
)
1356 ASSERT(table
->parent
->opcode
!= NULL
);
1357 table
->opcode
->parent
= table
->parent
->opcode
;
1360 /* report the rule being used to expand the instructions */
1361 if (options
.trace
.rule_selection
)
1363 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1365 ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
1366 table
->opcode
->word_nr
,
1367 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1368 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1369 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1370 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1371 table
->opcode
->nr_opcodes
,
1375 /* expand the raw instructions according to the opcode */
1378 for (entry
= table
->insns
; entry
!= NULL
; entry
= entry
->next
)
1380 if (options
.trace
.insn_expansion
)
1382 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1383 notify (NULL
, ": expand - %s.%s\n",
1384 entry
->insn
->format_name
,
1387 gen_entry_insert_expanding (table
, entry
->insn
);
1391 /* dump the results */
1392 if (options
.trace
.entries
)
1395 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1398 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1399 notify (NULL
, ": %d - entries %d -",
1402 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1403 notify (NULL
, " %s.%s", l
->insn
->format_name
, l
->insn
->name
);
1404 notify (NULL
, "\n");
1408 /* perform a combine pass if needed */
1409 if (table
->opcode_rule
->with_combine
)
1412 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1414 if (entry
->combined_parent
== NULL
)
1416 gen_entry
**last
= &entry
->combined_next
;
1418 for (alt
= entry
->sibling
; alt
!= NULL
; alt
= alt
->sibling
)
1420 if (alt
->combined_parent
== NULL
1421 && insn_list_cmp (entry
->insns
, alt
->insns
) == 0)
1423 alt
->combined_parent
= entry
;
1425 last
= &alt
->combined_next
;
1430 if (options
.trace
.combine
)
1434 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1436 if (entry
->combined_parent
== NULL
)
1439 gen_entry
*duplicate
;
1441 print_gen_entry_path (table
->opcode_rule
->line
, entry
, notify
);
1442 for (duplicate
= entry
->combined_next
;
1444 duplicate
= duplicate
->combined_next
)
1446 notify (NULL
, "+%d", duplicate
->opcode_nr
);
1448 notify (NULL
, ": entries %d -", entry
->nr_insns
);
1449 for (l
= entry
->insns
; l
!= NULL
; l
= l
->next
)
1451 notify (NULL
, " %s.%s",
1452 l
->insn
->format_name
,
1455 notify (NULL
, "\n");
1458 print_gen_entry_path (table
->opcode_rule
->line
, table
, notify
);
1459 notify (NULL
, ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
1460 table
->opcode
->word_nr
,
1461 i2target (options
.hi_bit_nr
, table
->opcode
->first
),
1462 i2target (options
.hi_bit_nr
, table
->opcode
->last
),
1463 i2target (options
.hi_bit_nr
, table
->opcode_rule
->first
),
1464 i2target (options
.hi_bit_nr
, table
->opcode_rule
->last
),
1465 table
->opcode
->nr_opcodes
,
1471 /* Check that the rule did more than re-arange the order of the
1475 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1477 if (entry
->combined_parent
== NULL
)
1479 if (insn_list_cmp (table
->insns
, entry
->insns
) == 0)
1481 print_gen_entry_path (table
->opcode_rule
->line
, table
, warning
);
1482 warning (NULL
, ": Applying rule just copied all instructions\n");
1483 print_gen_entry_insns (entry
, warning
, "Copied", NULL
);
1490 /* if some form of expanded table, fill in the missing dots */
1491 switch (table
->opcode_rule
->gen
)
1493 case padded_switch_gen
:
1495 case goto_switch_gen
:
1496 if (!table
->opcode
->is_boolean
)
1498 gen_entry
**entry
= &table
->entries
;
1499 gen_entry
*illegals
= NULL
;
1500 gen_entry
**last_illegal
= &illegals
;
1502 while (opcode_nr
< table
->opcode
->nr_opcodes
)
1504 if ((*entry
) == NULL
|| (*entry
)->opcode_nr
!= opcode_nr
)
1506 /* missing - insert it under our feet at *entry */
1507 gen_entry_insert_insn (table
,
1508 table
->top
->isa
->illegal_insn
,
1509 table
->opcode
->word_nr
,
1510 0, /* nr_prefetched_words == 0 for invalid */
1512 ASSERT ((*entry
) != NULL
);
1513 ASSERT ((*entry
)->opcode_nr
== opcode_nr
);
1514 (*last_illegal
) = *entry
;
1515 (*last_illegal
)->combined_parent
= illegals
;
1516 last_illegal
= &(*last_illegal
)->combined_next
;
1518 entry
= &(*entry
)->sibling
;
1521 /* oops, will have pointed the first illegal insn back to
1522 its self. Fix this */
1523 if (illegals
!= NULL
)
1524 illegals
->combined_parent
= NULL
;
1533 /* and do the same for the newly created sub entries but *only*
1534 expand entries that haven't been combined. */
1537 for (entry
= table
->entries
; entry
!= NULL
; entry
= entry
->sibling
)
1539 if (entry
->combined_parent
== NULL
)
1541 gen_entry_expand_insns (entry
);
1548 gen_tables_expand_insns (gen_table
*gen
)
1551 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1553 gen_entry_expand_insns (entry
->table
);
1558 /* create a list of all the semantic functions that need to be
1559 generated. Eliminate any duplicates. Verify that the decode stage
1563 make_gen_semantics_list (lf
*file
,
1568 gen_table
*gen
= (gen_table
*) data
;
1570 /* Not interested in an entrie that have been combined into some
1571 other entry at the same level */
1572 if (entry
->combined_parent
!= NULL
)
1575 /* a leaf should contain exactly one instruction. If not the decode
1577 ASSERT (entry
->nr_insns
== 1);
1579 /* Enter this instruction into the list of semantic functions. */
1580 insn
= insn_list_insert (&gen
->semantics
, &gen
->nr_semantics
,
1582 entry
->expanded_bits
,
1583 entry
->parent
->opcode
,
1584 entry
->insns
->nr_prefetched_words
,
1585 merge_duplicate_insns
);
1586 /* point the table entry at the real semantic function */
1587 ASSERT (insn
!= NULL
);
1588 entry
->insns
->semantic
= insn
;
1593 gen_tables_expand_semantics (gen_table
*gen
)
1596 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
1598 gen_entry_traverse_tree (NULL
,
1601 NULL
, /* start-handler */
1602 make_gen_semantics_list
, /* leaf-handler */
1603 NULL
, /* end-handler */
1614 dump_opcode_field (lf
*file
,
1616 opcode_field
*field
,
1620 lf_printf (file
, "%s(opcode_field *) 0x%lx", prefix
, (long) field
);
1621 if (levels
&& field
!= NULL
) {
1622 lf_indent (file
, +1);
1623 lf_printf (file
, "\n(first %d)", field
->first
);
1624 lf_printf (file
, "\n(last %d)", field
->last
);
1625 lf_printf (file
, "\n(nr_opcodes %d)", field
->nr_opcodes
);
1626 lf_printf (file
, "\n(is_boolean %d)", field
->is_boolean
);
1627 lf_printf (file
, "\n(boolean_constant %d)", field
->boolean_constant
);
1628 dump_opcode_field(file
, "\n(parent ", field
->parent
, ")", levels
- 1);
1629 lf_indent (file
, -1);
1631 lf_printf (file
, "%s", suffix
);
1636 dump_opcode_bits (lf
*file
,
1642 lf_printf (file
, "%s(opcode_bits *) 0x%lx", prefix
, (long) bits
);
1644 if (levels
&& bits
!= NULL
)
1646 lf_indent (file
, +1);
1647 lf_printf (file
, "\n(value %d)", bits
->value
);
1648 dump_opcode_field (file
, "\n(opcode ", bits
->opcode
, ")", 0);
1649 dump_insn_field (file
, "\n(field ", bits
->field
, ")");
1650 dump_opcode_bits (file
, "\n(next ", bits
->next
, ")", levels
- 1);
1651 lf_indent (file
, -1);
1653 lf_printf (file
, "%s", suffix
);
1659 dump_insn_list (lf
*file
,
1664 lf_printf (file
, "%s(insn_list *) 0x%lx", prefix
, (long) entry
);
1666 if (entry
!= NULL
) {
1667 lf_indent (file
, +1);
1668 dump_insn_entry (file
, "\n(insn ", entry
->insn
, ")");
1669 lf_printf (file
, "\n(next 0x%lx)", (long) entry
->next
);
1670 lf_indent (file
, -1);
1672 lf_printf (file
, "%s", suffix
);
1677 dump_insn_word_entry_list_entries (lf
*file
,
1682 lf_printf (file
, "%s", prefix
);
1683 while (entry
!= NULL
)
1685 dump_insn_list (file
, "\n(", entry
, ")");
1686 entry
= entry
->next
;
1688 lf_printf (file
, "%s", suffix
);
1693 dump_gen_entry (lf
*file
,
1700 lf_printf (file
, "%s(gen_entry *) 0x%lx", prefix
, (long) table
);
1702 if (levels
&& table
!= NULL
) {
1704 lf_indent (file
, +1);
1705 lf_printf (file
, "\n(opcode_nr %d)", table
->opcode_nr
);
1706 lf_printf (file
, "\n(word_nr %d)", table
->word_nr
);
1707 dump_opcode_bits (file
, "\n(expanded_bits ", table
->expanded_bits
, ")", -1);
1708 lf_printf (file
, "\n(nr_insns %d)", table
->nr_insns
);
1709 dump_insn_word_entry_list_entries (file
, "\n(insns ", table
->insns
, ")");
1710 dump_decode_rule (file
, "\n(opcode_rule ", table
->opcode_rule
, ")");
1711 dump_opcode_field (file
, "\n(opcode ", table
->opcode
, ")", 0);
1712 lf_printf (file
, "\n(nr_entries %d)", table
->nr_entries
);
1713 dump_gen_entry (file
, "\n(entries ", table
->entries
, ")", table
->nr_entries
);
1714 dump_gen_entry (file
, "\n(sibling ", table
->sibling
, ")", levels
- 1);
1715 dump_gen_entry (file
, "\n(parent ", table
->parent
, ")", 0);
1716 lf_indent (file
, -1);
1718 lf_printf (file
, "%s", suffix
);
1722 dump_gen_list (lf
*file
,
1728 while (entry
!= NULL
)
1730 lf_printf (file
, "%s(gen_list *) 0x%lx", prefix
, (long) entry
);
1731 dump_gen_entry (file
, "\n(", entry
->table
, ")", levels
);
1732 lf_printf (file
, "\n(next (gen_list *) 0x%lx)", (long) entry
->next
);
1733 lf_printf (file
, "%s", suffix
);
1739 dump_gen_table (lf
*file
,
1745 lf_printf (file
, "%s(gen_table *) 0x%lx", prefix
, (long) gen
);
1746 lf_printf (file
, "\n(isa (insn_table *) 0x%lx)", (long) gen
->isa
);
1747 lf_printf (file
, "\n(rules (decode_table *) 0x%lx)", (long) gen
->rules
);
1748 dump_gen_list (file
, "\n(", gen
->tables
, ")", levels
);
1749 lf_printf (file
, "%s", suffix
);
1753 igen_options options
;
1759 decode_table
*decode_rules
;
1760 insn_table
*instructions
;
1765 error (NULL
, "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
1767 INIT_OPTIONS (options
);
1769 filter_parse (&options
.flags_filter
, argv
[1]);
1771 options
.hi_bit_nr
= a2i(argv
[2]);
1772 options
.insn_bit_size
= a2i(argv
[3]);
1773 options
.insn_specifying_widths
= a2i(argv
[4]);
1774 ASSERT(options
.hi_bit_nr
< options
.insn_bit_size
);
1776 instructions
= load_insn_table (argv
[6], NULL
);
1777 decode_rules
= load_decode_table (argv
[5]);
1778 gen
= make_gen_tables (instructions
, decode_rules
);
1780 gen_tables_expand_insns (gen
);
1782 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-ld-insn");
1784 dump_gen_table (l
, "(", gen
, ")\n", -1);