1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2021 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "ld-decode.h"
35 #include "gen-semantics.h"
36 #include "gen-idecode.h"
37 #include "gen-icache.h"
42 print_icache_function_header (lf
*file
,
44 const char *format_name
,
45 opcode_bits
*expanded_bits
,
46 int is_function_definition
,
47 int nr_prefetched_words
)
49 lf_printf (file
, "\n");
50 lf_print__function_type_function (file
, print_icache_function_type
,
51 "EXTERN_ICACHE", " ");
52 print_function_name (file
,
53 basename
, format_name
, NULL
,
54 expanded_bits
, function_name_prefix_icache
);
55 lf_printf (file
, "\n(");
56 print_icache_function_formal (file
, nr_prefetched_words
);
57 lf_printf (file
, ")");
58 if (!is_function_definition
)
59 lf_printf (file
, ";");
60 lf_printf (file
, "\n");
65 print_icache_declaration (lf
*file
,
67 opcode_bits
*expanded_bits
,
68 insn_opcodes
*opcodes
, int nr_prefetched_words
)
70 print_icache_function_header (file
,
74 0 /* is not function definition */ ,
81 print_icache_extraction (lf
*file
,
82 const char *format_name
,
83 cache_entry_type cache_type
,
84 const char *entry_name
,
85 const char *entry_type
,
86 const char *entry_expression
,
87 char *single_insn_field
,
89 insn_field_entry
*cur_field
,
90 opcode_bits
*expanded_bits
,
91 icache_decl_type what_to_declare
,
92 icache_body_type what_to_do
)
94 const char *expression
;
97 ASSERT (format_name
!= NULL
);
98 ASSERT (entry_name
!= NULL
);
100 /* figure out exactly what should be going on here */
104 if ((what_to_do
& put_values_in_icache
)
105 || what_to_do
== do_not_use_icache
)
108 what_to_do
= do_not_use_icache
;
114 if ((what_to_do
& get_values_from_icache
)
115 || what_to_do
== do_not_use_icache
)
118 what_to_do
= do_not_use_icache
;
124 if ((what_to_declare
!= undef_variables
)
125 || !(what_to_do
& put_values_in_icache
))
128 what_to_declare
= ((what_to_do
& put_values_in_icache
)
129 ? declare_variables
: what_to_declare
);
135 abort (); /* Bad switch. */
138 /* For the type, default to a simple unsigned */
139 if (entry_type
== NULL
|| strlen (entry_type
) == 0)
140 entry_type
= "unsigned";
142 /* look through the set of expanded sub fields to see if this field
143 has been given a constant value */
144 for (bits
= expanded_bits
; bits
!= NULL
; bits
= bits
->next
)
146 if (bits
->field
== cur_field
)
150 /* Define a storage area for the cache element */
151 switch (what_to_declare
)
153 case undef_variables
:
154 /* We've finished with the #define value - destory it */
155 lf_indent_suppress (file
);
156 lf_printf (file
, "#undef %s\n", entry_name
);
158 case define_variables
:
159 /* Using direct access for this entry, clear any prior
160 definition, then define it */
161 lf_indent_suppress (file
);
162 lf_printf (file
, "#undef %s\n", entry_name
);
163 /* Don't type cast pointer types! */
164 lf_indent_suppress (file
);
165 if (strchr (entry_type
, '*') != NULL
)
166 lf_printf (file
, "#define %s (", entry_name
);
168 lf_printf (file
, "#define %s ((%s) ", entry_name
, entry_type
);
170 case declare_variables
:
171 /* using variables to define the value */
173 lf_print__line_ref (file
, line
);
174 lf_printf (file
, "%s const %s UNUSED = ", entry_type
, entry_name
);
179 /* define a value for that storage area as determined by what is in
182 && single_insn_field
!= NULL
183 && strcmp (entry_name
, single_insn_field
) == 0
184 && strcmp (entry_name
, cur_field
->val_string
) == 0
185 && ((bits
->opcode
->is_boolean
&& bits
->value
== 0)
186 || (!bits
->opcode
->is_boolean
)))
188 /* The cache rule is specifying what to do with a simple
191 Because of instruction expansion, the field is either a
192 constant value or equal to the specified constant (boolean
193 comparison). (The latter indicated by bits->value == 0).
195 The case of a field not being equal to the specified boolean
196 value is handled later. */
197 expression
= "constant field";
198 ASSERT (bits
->field
== cur_field
);
199 if (bits
->opcode
->is_boolean
)
201 ASSERT (bits
->value
== 0);
202 lf_printf (file
, "%d", bits
->opcode
->boolean_constant
);
204 else if (bits
->opcode
->last
< bits
->field
->last
)
206 lf_printf (file
, "%d",
207 bits
->value
<< (bits
->field
->last
- bits
->opcode
->last
));
211 lf_printf (file
, "%d", bits
->value
);
214 else if (bits
!= NULL
215 && single_insn_field
!= NULL
216 && strncmp (entry_name
,
218 strlen (single_insn_field
)) == 0
219 && strncmp (entry_name
+ strlen (single_insn_field
),
221 strlen ("_is_")) == 0
222 && ((bits
->opcode
->is_boolean
224 atol (entry_name
+ strlen (single_insn_field
) +
225 strlen ("_is_")) == bits
->opcode
->boolean_constant
))
226 || (!bits
->opcode
->is_boolean
)))
228 /* The cache rule defines an entry for the comparison between a
229 single instruction field and a constant. The value of the
230 comparison in someway matches that of the opcode field that
231 was made constant through expansion. */
232 expression
= "constant compare";
233 if (bits
->opcode
->is_boolean
)
235 lf_printf (file
, "%d /* %s == %d */",
237 single_insn_field
, bits
->opcode
->boolean_constant
);
239 else if (bits
->opcode
->last
< bits
->field
->last
)
241 lf_printf (file
, "%d /* %s == %d */",
243 (entry_name
+ strlen (single_insn_field
) +
246 value
<< (bits
->field
->last
- bits
->opcode
->last
))),
249 value
<< (bits
->field
->last
- bits
->opcode
->last
)));
253 lf_printf (file
, "%d /* %s == %d */",
255 (entry_name
+ strlen (single_insn_field
) +
256 strlen ("_is_")) == bits
->value
), single_insn_field
,
262 /* put the field in the local variable, possibly also enter it
264 expression
= "extraction";
265 /* handle the cache */
266 if ((what_to_do
& get_values_from_icache
)
267 || (what_to_do
& put_values_in_icache
))
269 lf_printf (file
, "cache_entry->crack.%s.%s",
270 format_name
, entry_name
);
271 if (what_to_do
& put_values_in_icache
) /* also put it in the cache? */
273 lf_printf (file
, " = ");
276 if ((what_to_do
& put_values_in_icache
)
277 || what_to_do
== do_not_use_icache
)
279 if (cur_field
!= NULL
)
281 if (entry_expression
!= NULL
&& strlen (entry_expression
) > 0)
283 "Instruction field entry with nonempty expression\n");
284 if (cur_field
->first
== 0
285 && cur_field
->last
== options
.insn_bit_size
- 1)
286 lf_printf (file
, "(instruction_%d)", cur_field
->word_nr
);
287 else if (cur_field
->last
== options
.insn_bit_size
- 1)
288 lf_printf (file
, "MASKED%d (instruction_%d, %d, %d)",
289 options
.insn_bit_size
,
291 i2target (options
.hi_bit_nr
, cur_field
->first
),
292 i2target (options
.hi_bit_nr
, cur_field
->last
));
294 lf_printf (file
, "EXTRACTED%d (instruction_%d, %d, %d)",
295 options
.insn_bit_size
,
297 i2target (options
.hi_bit_nr
, cur_field
->first
),
298 i2target (options
.hi_bit_nr
, cur_field
->last
));
302 lf_printf (file
, "%s", entry_expression
);
307 switch (what_to_declare
)
309 case define_variables
:
310 lf_printf (file
, ")");
312 case undef_variables
:
314 case declare_variables
:
315 lf_printf (file
, ";");
319 ASSERT (reason
!= NULL
&& expression
!= NULL
);
320 lf_printf (file
, " /* %s - %s */\n", reason
, expression
);
325 print_icache_body (lf
*file
,
326 insn_entry
* instruction
,
327 opcode_bits
*expanded_bits
,
328 cache_entry
*cache_rules
,
329 icache_decl_type what_to_declare
,
330 icache_body_type what_to_do
, int nr_prefetched_words
)
332 /* extract instruction fields */
333 lf_printf (file
, "/* Extraction: %s\n", instruction
->name
);
334 lf_printf (file
, " ");
335 switch (what_to_declare
)
337 case define_variables
:
338 lf_printf (file
, "#define");
340 case declare_variables
:
341 lf_printf (file
, "declare");
343 case undef_variables
:
344 lf_printf (file
, "#undef");
347 lf_printf (file
, " ");
350 case get_values_from_icache
:
351 lf_printf (file
, "get-values-from-icache");
353 case put_values_in_icache
:
354 lf_printf (file
, "put-values-in-icache");
356 case both_values_and_icache
:
357 lf_printf (file
, "get-values-from-icache|put-values-in-icache");
359 case do_not_use_icache
:
360 lf_printf (file
, "do-not-use-icache");
363 lf_printf (file
, "\n ");
364 print_insn_words (file
, instruction
);
365 lf_printf (file
, " */\n");
367 /* pass zero - fetch from memory any missing instructions.
369 Some of the instructions will have already been fetched (in the
370 instruction array), others will still need fetching. */
373 case get_values_from_icache
:
375 case put_values_in_icache
:
376 case both_values_and_icache
:
377 case do_not_use_icache
:
380 switch (what_to_declare
)
382 case undef_variables
:
384 case define_variables
:
385 case declare_variables
:
386 for (word_nr
= nr_prefetched_words
;
387 word_nr
< instruction
->nr_words
; word_nr
++)
389 /* FIXME - should be using print_icache_extraction? */
391 "%sinstruction_word instruction_%d UNUSED = ",
392 options
.module
.global
.prefix
.l
, word_nr
);
393 lf_printf (file
, "IMEM%d_IMMED (cia, %d)",
394 options
.insn_bit_size
, word_nr
);
395 lf_printf (file
, ";\n");
401 /* if putting the instruction words in the cache, define references
403 if (options
.gen
.insn_in_icache
)
405 /* FIXME: is the instruction_word type correct? */
406 print_icache_extraction (file
, instruction
->format_name
, cache_value
, "insn", /* name */
407 "instruction_word", /* type */
408 "instruction", /* expression */
411 NULL
, NULL
, what_to_declare
, what_to_do
);
413 lf_printf (file
, "\n");
415 /* pass one - process instruction fields.
417 If there is no cache rule, the default is to enter the field into
420 insn_word_entry
*word
;
421 for (word
= instruction
->words
; word
!= NULL
; word
= word
->next
)
423 insn_field_entry
*cur_field
;
424 for (cur_field
= word
->first
;
425 cur_field
->first
< options
.insn_bit_size
;
426 cur_field
= cur_field
->next
)
428 /* Always expand named fields (even if constant), so
429 references are valid. */
430 if (cur_field
->type
== insn_field_string
)
432 cache_entry
*cache_rule
;
433 cache_entry_type value_type
= cache_value
;
434 line_ref
*value_line
= instruction
->line
;
435 /* check the cache table to see if it contains a rule
436 overriding the default cache action for an
438 for (cache_rule
= cache_rules
;
439 cache_rule
!= NULL
; cache_rule
= cache_rule
->next
)
441 if (filter_is_subset (instruction
->field_names
,
442 cache_rule
->original_fields
)
443 && strcmp (cache_rule
->name
,
444 cur_field
->val_string
) == 0)
446 value_type
= cache_rule
->entry_type
;
447 value_line
= cache_rule
->line
;
448 if (value_type
== compute_value
)
450 options
.warning (cache_rule
->line
,
451 "instruction field of type `compute' changed to `cache'\n");
452 cache_rule
->entry_type
= cache_value
;
457 /* Define an entry for the field within the
459 print_icache_extraction (file
, instruction
->format_name
, value_type
, cur_field
->val_string
, /* name */
461 NULL
, /* expression */
462 cur_field
->val_string
, /* insn field */
466 what_to_declare
, what_to_do
);
472 /* pass two - any cache fields not processed above */
474 cache_entry
*cache_rule
;
475 for (cache_rule
= cache_rules
;
476 cache_rule
!= NULL
; cache_rule
= cache_rule
->next
)
478 if (filter_is_subset (instruction
->field_names
,
479 cache_rule
->original_fields
)
480 && !filter_is_member (instruction
->field_names
, cache_rule
->name
))
483 filter_next (cache_rule
->original_fields
, "");
484 if (filter_next (cache_rule
->original_fields
, single_field
) !=
487 print_icache_extraction (file
, instruction
->format_name
, cache_rule
->entry_type
, cache_rule
->name
, cache_rule
->type
, cache_rule
->expression
, single_field
, cache_rule
->line
, NULL
, /* cur_field */
489 what_to_declare
, what_to_do
);
494 lf_print__internal_ref (file
);
499 typedef struct _form_fields form_fields
;
508 insn_table_cache_fields (insn_table
*isa
)
510 form_fields
*forms
= NULL
;
512 for (insn
= isa
->insns
; insn
!= NULL
; insn
= insn
->next
)
514 form_fields
**form
= &forms
;
519 /* new format name, add it */
520 form_fields
*new_form
= ZALLOC (form_fields
);
521 new_form
->name
= insn
->format_name
;
522 filter_add (&new_form
->fields
, insn
->field_names
);
526 else if (strcmp ((*form
)->name
, insn
->format_name
) == 0)
528 /* already present, add field names to the existing list */
529 filter_add (&(*form
)->fields
, insn
->field_names
);
532 form
= &(*form
)->next
;
541 print_icache_struct (lf
*file
, insn_table
*isa
, cache_entry
*cache_rules
)
543 /* Create a list of all the different instruction formats with their
544 corresponding field names. */
545 form_fields
*formats
= insn_table_cache_fields (isa
);
547 lf_printf (file
, "\n");
548 lf_printf (file
, "#define WITH_%sIDECODE_CACHE_SIZE %d\n",
549 options
.module
.global
.prefix
.u
,
550 (options
.gen
.icache
? options
.gen
.icache_size
: 0));
551 lf_printf (file
, "\n");
553 /* create an instruction cache if being used */
554 if (options
.gen
.icache
)
556 lf_printf (file
, "typedef struct _%sidecode_cache {\n",
557 options
.module
.global
.prefix
.l
);
558 lf_indent (file
, +2);
561 lf_printf (file
, "unsigned_word address;\n");
562 lf_printf (file
, "void *semantic;\n");
563 lf_printf (file
, "union {\n");
564 lf_indent (file
, +2);
565 for (format
= formats
; format
!= NULL
; format
= format
->next
)
567 lf_printf (file
, "struct {\n");
568 lf_indent (file
, +2);
570 cache_entry
*cache_rule
;
572 /* space for any instruction words */
573 if (options
.gen
.insn_in_icache
)
574 lf_printf (file
, "instruction_word insn[%d];\n",
576 /* define an entry for any applicable cache rules */
577 for (cache_rule
= cache_rules
;
578 cache_rule
!= NULL
; cache_rule
= cache_rule
->next
)
580 /* nb - sort of correct - should really check against
581 individual instructions */
583 (format
->fields
, cache_rule
->original_fields
))
586 lf_printf (file
, "%s %s;",
587 (cache_rule
->type
== NULL
589 : cache_rule
->type
), cache_rule
->name
);
590 lf_printf (file
, " /*");
592 filter_next (cache_rule
->original_fields
, "");
595 filter_next (cache_rule
->original_fields
, memb
))
597 lf_printf (file
, " %s", memb
);
599 lf_printf (file
, " */\n");
602 /* define an entry for any fields not covered by a cache rule */
603 for (field
= filter_next (format
->fields
, "");
604 field
!= NULL
; field
= filter_next (format
->fields
, field
))
606 cache_entry
*cache_rule
;
608 for (cache_rule
= cache_rules
;
609 cache_rule
!= NULL
; cache_rule
= cache_rule
->next
)
611 if (strcmp (cache_rule
->name
, field
) == 0)
618 lf_printf (file
, "unsigned %s; /* default */\n", field
);
621 lf_indent (file
, -2);
622 lf_printf (file
, "} %s;\n", format
->name
);
624 lf_indent (file
, -2);
625 lf_printf (file
, "} crack;\n");
627 lf_indent (file
, -2);
628 lf_printf (file
, "} %sidecode_cache;\n",
629 options
.module
.global
.prefix
.l
);
633 /* alernativly, since no cache, emit a dummy definition for
634 idecode_cache so that code refering to the type can still compile */
635 lf_printf (file
, "typedef void %sidecode_cache;\n",
636 options
.module
.global
.prefix
.l
);
638 lf_printf (file
, "\n");
644 print_icache_function (lf
*file
,
645 insn_entry
* instruction
,
646 opcode_bits
*expanded_bits
,
647 insn_opcodes
*opcodes
,
648 cache_entry
*cache_rules
, int nr_prefetched_words
)
652 /* generate code to enter decoded instruction into the icache */
653 lf_printf (file
, "\n");
654 lf_print__function_type_function (file
, print_icache_function_type
,
655 "EXTERN_ICACHE", "\n");
656 indent
= print_function_name (file
,
658 instruction
->format_name
,
660 expanded_bits
, function_name_prefix_icache
);
661 indent
+= lf_printf (file
, " ");
662 lf_indent (file
, +indent
);
663 lf_printf (file
, "(");
664 print_icache_function_formal (file
, nr_prefetched_words
);
665 lf_printf (file
, ")\n");
666 lf_indent (file
, -indent
);
668 /* function header */
669 lf_printf (file
, "{\n");
670 lf_indent (file
, +2);
672 print_my_defines (file
,
674 instruction
->format_name
, expanded_bits
);
675 print_itrace (file
, instruction
, 1 /*putting-value-in-cache */ );
677 print_idecode_validate (file
, instruction
, opcodes
);
679 lf_printf (file
, "\n");
680 lf_printf (file
, "{\n");
681 lf_indent (file
, +2);
682 if (options
.gen
.semantic_icache
)
683 lf_printf (file
, "unsigned_word nia;\n");
684 print_icache_body (file
,
688 (options
.gen
.direct_access
690 : declare_variables
),
691 (options
.gen
.semantic_icache
692 ? both_values_and_icache
693 : put_values_in_icache
), nr_prefetched_words
);
695 lf_printf (file
, "\n");
696 lf_printf (file
, "cache_entry->address = cia;\n");
697 lf_printf (file
, "cache_entry->semantic = ");
698 print_function_name (file
,
700 instruction
->format_name
,
701 NULL
, expanded_bits
, function_name_prefix_semantics
);
702 lf_printf (file
, ";\n");
703 lf_printf (file
, "\n");
705 if (options
.gen
.semantic_icache
)
707 lf_printf (file
, "/* semantic routine */\n");
708 print_semantic_body (file
, instruction
, expanded_bits
, opcodes
);
709 lf_printf (file
, "return nia;\n");
712 if (!options
.gen
.semantic_icache
)
714 lf_printf (file
, "/* return the function proper */\n");
715 lf_printf (file
, "return ");
716 print_function_name (file
,
718 instruction
->format_name
,
720 expanded_bits
, function_name_prefix_semantics
);
721 lf_printf (file
, ";\n");
724 if (options
.gen
.direct_access
)
726 print_icache_body (file
,
731 (options
.gen
.semantic_icache
732 ? both_values_and_icache
733 : put_values_in_icache
), nr_prefetched_words
);
736 lf_indent (file
, -2);
737 lf_printf (file
, "}\n");
738 lf_indent (file
, -2);
739 lf_printf (file
, "}\n");
744 print_icache_definition (lf
*file
,
746 opcode_bits
*expanded_bits
,
747 insn_opcodes
*opcodes
,
748 cache_entry
*cache_rules
, int nr_prefetched_words
)
750 print_icache_function (file
,
753 opcodes
, cache_rules
, nr_prefetched_words
);
759 print_icache_internal_function_declaration (lf
*file
,
760 function_entry
* function
,
763 ASSERT (options
.gen
.icache
);
764 if (function
->is_internal
)
766 lf_printf (file
, "\n");
767 lf_print__function_type_function (file
, print_icache_function_type
,
768 "INLINE_ICACHE", "\n");
769 print_function_name (file
,
771 NULL
, NULL
, NULL
, function_name_prefix_icache
);
772 lf_printf (file
, "\n(");
773 print_icache_function_formal (file
, 0);
774 lf_printf (file
, ");\n");
780 print_icache_internal_function_definition (lf
*file
,
781 function_entry
* function
,
784 ASSERT (options
.gen
.icache
);
785 if (function
->is_internal
)
787 lf_printf (file
, "\n");
788 lf_print__function_type_function (file
, print_icache_function_type
,
789 "INLINE_ICACHE", "\n");
790 print_function_name (file
,
792 NULL
, NULL
, NULL
, function_name_prefix_icache
);
793 lf_printf (file
, "\n(");
794 print_icache_function_formal (file
, 0);
795 lf_printf (file
, ")\n");
796 lf_printf (file
, "{\n");
797 lf_indent (file
, +2);
798 lf_printf (file
, "/* semantic routine */\n");
799 if (options
.gen
.semantic_icache
)
801 lf_print__line_ref (file
, function
->code
->line
);
802 table_print_code (file
, function
->code
);
804 "error (\"Internal function must longjump\\n\");\n");
805 lf_printf (file
, "return 0;\n");
809 lf_printf (file
, "return ");
810 print_function_name (file
,
813 NULL
, NULL
, function_name_prefix_semantics
);
814 lf_printf (file
, ";\n");
817 lf_print__internal_ref (file
);
818 lf_indent (file
, -2);
819 lf_printf (file
, "}\n");