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/>. */
34 #include "ld-decode.h"
39 #include "gen-model.h"
40 #include "gen-icache.h"
41 #include "gen-itable.h"
42 #include "gen-idecode.h"
43 #include "gen-semantics.h"
44 #include "gen-engine.h"
45 #include "gen-support.h"
46 #include "gen-engine.h"
49 /****************************************************************/
52 /* Semantic functions */
55 print_semantic_function_formal (lf
*file
, int nr_prefetched_words
)
59 if (options
.gen
.icache
|| nr_prefetched_words
< 0)
61 nr
+= lf_printf (file
, "SIM_DESC sd,\n");
62 nr
+= lf_printf (file
, "%sidecode_cache *cache_entry,\n",
63 options
.module
.global
.prefix
.l
);
64 nr
+= lf_printf (file
, "%sinstruction_address cia",
65 options
.module
.global
.prefix
.l
);
67 else if (options
.gen
.smp
)
69 nr
+= lf_printf (file
, "sim_cpu *cpu,\n");
70 for (word_nr
= 0; word_nr
< nr_prefetched_words
; word_nr
++)
72 nr
+= lf_printf (file
, "%sinstruction_word instruction_%d,\n",
73 options
.module
.global
.prefix
.l
, word_nr
);
75 nr
+= lf_printf (file
, "%sinstruction_address cia",
76 options
.module
.global
.prefix
.l
);
80 nr
+= lf_printf (file
, "SIM_DESC sd,\n");
81 for (word_nr
= 0; word_nr
< nr_prefetched_words
; word_nr
++)
83 nr
+= lf_printf (file
, "%sinstruction_word instruction_%d,\n",
84 options
.module
.global
.prefix
.l
, word_nr
);
86 nr
+= lf_printf (file
, "%sinstruction_address cia",
87 options
.module
.global
.prefix
.l
);
93 print_semantic_function_actual (lf
*file
, int nr_prefetched_words
)
97 if (options
.gen
.icache
|| nr_prefetched_words
< 0)
99 nr
+= lf_printf (file
, "sd, cache_entry, cia");
104 nr
+= lf_printf (file
, "cpu");
106 nr
+= lf_printf (file
, "sd");
107 for (word_nr
= 0; word_nr
< nr_prefetched_words
; word_nr
++)
108 nr
+= lf_printf (file
, ", instruction_%d", word_nr
);
109 nr
+= lf_printf (file
, ", cia");
115 print_semantic_function_type (lf
*file
)
118 nr
+= lf_printf (file
, "%sinstruction_address",
119 options
.module
.global
.prefix
.l
);
124 /* Idecode functions */
127 print_icache_function_formal (lf
*file
, int nr_prefetched_words
)
132 nr
+= lf_printf (file
, "sim_cpu *cpu,\n");
134 nr
+= lf_printf (file
, "SIM_DESC sd,\n");
135 for (word_nr
= 0; word_nr
< nr_prefetched_words
; word_nr
++)
136 nr
+= lf_printf (file
, " %sinstruction_word instruction_%d,\n",
137 options
.module
.global
.prefix
.l
, word_nr
);
138 nr
+= lf_printf (file
, " %sinstruction_address cia,\n",
139 options
.module
.global
.prefix
.l
);
140 nr
+= lf_printf (file
, " %sidecode_cache *cache_entry",
141 options
.module
.global
.prefix
.l
);
146 print_icache_function_actual (lf
*file
, int nr_prefetched_words
)
151 nr
+= lf_printf (file
, "cpu");
153 nr
+= lf_printf (file
, "sd");
154 for (word_nr
= 0; word_nr
< nr_prefetched_words
; word_nr
++)
155 nr
+= lf_printf (file
, ", instruction_%d", word_nr
);
156 nr
+= lf_printf (file
, ", cia, cache_entry");
161 print_icache_function_type (lf
*file
)
164 if (options
.gen
.semantic_icache
)
166 nr
= print_semantic_function_type (file
);
170 nr
= lf_printf (file
, "%sidecode_semantic *",
171 options
.module
.global
.prefix
.l
);
180 print_opcode_bits (lf
*file
, opcode_bits
*bits
)
185 nr
+= lf_putchr (file
, '_');
186 nr
+= lf_putstr (file
, bits
->field
->val_string
);
187 if (bits
->opcode
->is_boolean
&& bits
->value
== 0)
188 nr
+= lf_putint (file
, bits
->opcode
->boolean_constant
);
189 else if (!bits
->opcode
->is_boolean
)
191 if (bits
->opcode
->last
< bits
->field
->last
)
194 bits
->value
<< (bits
->field
->last
- bits
->opcode
->last
));
196 nr
+= lf_putint (file
, bits
->value
);
198 nr
+= print_opcode_bits (file
, bits
->next
);
203 print_c_name (lf
*file
, const char *name
)
207 for (pos
= name
; *pos
!= '\0'; pos
++)
216 nr
+= lf_putchr (file
, '_');
219 nr
+= lf_putchr (file
, *pos
);
227 print_function_name (lf
*file
,
228 const char *basename
,
229 const char *format_name
,
230 const char *model_name
,
231 opcode_bits
*expanded_bits
,
232 lf_function_name_prefixes prefix
)
238 case function_name_prefix_semantics
:
239 nr
+= lf_printf (file
, "%s", options
.module
.semantics
.prefix
.l
);
240 nr
+= lf_printf (file
, "semantic_");
242 case function_name_prefix_idecode
:
243 nr
+= lf_printf (file
, "%s", options
.module
.idecode
.prefix
.l
);
244 nr
+= lf_printf (file
, "idecode_");
246 case function_name_prefix_itable
:
247 nr
+= lf_printf (file
, "%sitable_", options
.module
.itable
.prefix
.l
);
249 case function_name_prefix_icache
:
250 nr
+= lf_printf (file
, "%s", options
.module
.icache
.prefix
.l
);
251 nr
+= lf_printf (file
, "icache_");
253 case function_name_prefix_engine
:
254 nr
+= lf_printf (file
, "%s", options
.module
.engine
.prefix
.l
);
255 nr
+= lf_printf (file
, "engine_");
260 if (model_name
!= NULL
)
262 nr
+= print_c_name (file
, model_name
);
263 nr
+= lf_printf (file
, "_");
266 /* the function name */
267 nr
+= print_c_name (file
, basename
);
269 /* the format name if available */
270 if (format_name
!= NULL
)
272 nr
+= lf_printf (file
, "_");
273 nr
+= print_c_name (file
, format_name
);
277 nr
+= print_opcode_bits (file
, expanded_bits
);
284 print_my_defines (lf
*file
,
285 const char *basename
,
286 const char *format_name
, opcode_bits
*expanded_bits
)
288 /* #define MY_INDEX xxxxx */
289 lf_indent_suppress (file
);
290 lf_printf (file
, "#undef MY_INDEX\n");
291 lf_indent_suppress (file
);
292 lf_printf (file
, "#define MY_INDEX ");
293 print_function_name (file
,
294 basename
, format_name
, NULL
,
295 NULL
, function_name_prefix_itable
);
296 lf_printf (file
, "\n");
297 /* #define MY_PREFIX xxxxxx */
298 lf_indent_suppress (file
);
299 lf_printf (file
, "#undef ");
300 print_function_name (file
,
301 basename
, format_name
, NULL
,
302 expanded_bits
, function_name_prefix_none
);
303 lf_printf (file
, "\n");
304 lf_indent_suppress (file
);
305 lf_printf (file
, "#undef MY_PREFIX\n");
306 lf_indent_suppress (file
);
307 lf_printf (file
, "#define MY_PREFIX ");
308 print_function_name (file
,
309 basename
, format_name
, NULL
,
310 expanded_bits
, function_name_prefix_none
);
311 lf_printf (file
, "\n");
312 /* #define MY_NAME xxxxxx */
313 lf_indent_suppress (file
);
314 lf_indent_suppress (file
);
315 lf_printf (file
, "#undef MY_NAME\n");
316 lf_indent_suppress (file
);
317 lf_printf (file
, "#define MY_NAME \"");
318 print_function_name (file
,
319 basename
, format_name
, NULL
,
320 expanded_bits
, function_name_prefix_none
);
321 lf_printf (file
, "\"\n");
326 print_itrace_prefix (lf
*file
)
328 const char *prefix
= "trace_prefix (";
329 int indent
= strlen (prefix
);
330 lf_printf (file
, "%sSD, CPU, cia, CIA, TRACE_LINENUM_P (CPU), \\\n",
332 lf_indent (file
, +indent
);
333 lf_printf (file
, "%sitable[MY_INDEX].file, \\\n",
334 options
.module
.itable
.prefix
.l
);
335 lf_printf (file
, "%sitable[MY_INDEX].line_nr, \\\n",
336 options
.module
.itable
.prefix
.l
);
337 lf_printf (file
, "\"");
343 print_itrace_format (lf
*file
, insn_mnemonic_entry
*assembler
)
345 /* pass=1 is fmt string; pass=2 is arguments */
347 /* print the format string */
348 for (pass
= 1; pass
<= 2; pass
++)
350 const char *chp
= assembler
->format
;
351 chp
++; /* skip the leading quote */
352 /* write out the format/args */
355 if (chp
[0] == '\\' && (chp
[1] == '<' || chp
[1] == '>'))
358 lf_putchr (file
, chp
[1]);
361 else if (chp
[0] == '<' || chp
[0] == '%')
363 /* parse [ "%" ... ] "<" [ func "#" ] param ">" */
369 /* the "%" ... "<" format */
371 while (chp
[0] != '<' && chp
[0] != '\0')
374 error (assembler
->line
, "Missing `<' after `%%'\n");
376 /* [ "func" # ] OR "param" */
379 while (chp
[0] != '>' && chp
[0] != '#' && chp
[0] != '\0')
381 strlen_func
= chp
- func
;
386 while (chp
[0] != '>' && chp
[0] != '\0')
389 strlen_param
= chp
- param
;
391 error (assembler
->line
,
392 "Missing closing `>' in assembler string\n");
396 lf_printf (file
, ", \\\n");
397 if (strncmp (fmt
, "<", 1) == 0)
398 /* implicit long int format */
401 lf_printf (file
, "%%ld");
404 lf_printf (file
, "(long) ");
405 lf_write (file
, param
, strlen_param
);
408 else if (strncmp (fmt
, "%<", 2) == 0)
409 /* explicit format */
412 lf_printf (file
, "%%");
414 lf_write (file
, param
, strlen_param
);
416 else if (strncmp (fmt
, "%s<", 3) == 0)
420 lf_printf (file
, "%%s");
423 lf_printf (file
, "%sstr_",
424 options
.module
.global
.prefix
.l
);
425 lf_write (file
, func
, strlen_func
);
426 lf_printf (file
, " (SD_, ");
427 lf_write (file
, param
, strlen_param
);
428 lf_printf (file
, ")");
431 else if (strncmp (fmt
, "%lx<", 4) == 0)
435 lf_printf (file
, "%%lx");
438 lf_printf (file
, "(unsigned long) ");
439 lf_write (file
, param
, strlen_param
);
442 else if (strncmp (fmt
, "%#lx<", 5) == 0)
443 /* simple hex with 0x prefix */
446 lf_printf (file
, "%%#lx");
449 lf_printf (file
, "(unsigned long) ");
450 lf_write (file
, param
, strlen_param
);
453 else if (strncmp (fmt
, "%08lx<", 6) == 0)
457 lf_printf (file
, "%%08lx");
460 lf_printf (file
, "(unsigned long) ");
461 lf_write (file
, param
, strlen_param
);
465 error (assembler
->line
, "Unknown assembler string format\n");
470 lf_putchr (file
, chp
[0]);
475 lf_printf (file
, ");\n");
480 print_itrace (lf
*file
, insn_entry
* insn
, int idecode
)
482 /* NB: Here we escape each EOLN. This is so that the the compiler
483 treats a trace function call as a single line. Consequently any
484 errors in the line are refered back to the same igen assembler
486 const char *phase
= (idecode
) ? "DECODE" : "INSN";
487 lf_printf (file
, "\n");
488 lf_indent_suppress (file
);
489 lf_printf (file
, "#if defined (WITH_TRACE)\n");
490 lf_printf (file
, "/* generate a trace prefix if any tracing enabled */\n");
491 lf_printf (file
, "if (TRACE_ANY_P (CPU))\n");
492 lf_printf (file
, " {\n");
493 lf_indent (file
, +4);
495 if (insn
->mnemonics
!= NULL
)
497 insn_mnemonic_entry
*assembler
= insn
->mnemonics
;
501 if (assembler
->condition
!= NULL
)
504 lf_printf (file
, "%sif (%s)\n",
505 is_first
? "" : "else ", assembler
->condition
);
506 lf_indent (file
, +2);
507 lf_print__line_ref (file
, assembler
->line
);
508 indent
= print_itrace_prefix (file
);
509 print_itrace_format (file
, assembler
);
510 lf_print__internal_ref (file
);
511 lf_indent (file
, -indent
);
512 lf_indent (file
, -2);
513 if (assembler
->next
== NULL
)
514 error (assembler
->line
,
515 "Missing final unconditional assembler\n");
522 lf_printf (file
, "else\n");
523 lf_indent (file
, +2);
525 lf_print__line_ref (file
, assembler
->line
);
526 indent
= print_itrace_prefix (file
);
527 print_itrace_format (file
, assembler
);
528 lf_print__internal_ref (file
);
529 lf_indent (file
, -indent
);
531 lf_indent (file
, -2);
532 if (assembler
->next
!= NULL
)
533 error (assembler
->line
,
534 "Unconditional assembler is not last\n");
537 assembler
= assembler
->next
;
539 while (assembler
!= NULL
);
544 lf_indent (file
, +2);
545 lf_print__line_ref (file
, insn
->line
);
546 indent
= print_itrace_prefix (file
);
547 lf_printf (file
, "%%s\", \\\n");
548 lf_printf (file
, "itable[MY_INDEX].name);\n");
549 lf_print__internal_ref (file
);
550 lf_indent (file
, -indent
);
551 lf_indent (file
, -2);
553 lf_printf (file
, "/* trace the instruction execution if enabled */\n");
554 lf_printf (file
, "if (TRACE_%s_P (CPU))\n", phase
);
556 " trace_generic (SD, CPU, TRACE_%s_IDX, \" %%s\", itable[MY_INDEX].name);\n",
559 lf_indent (file
, -4);
560 lf_printf (file
, " }\n");
561 lf_indent_suppress (file
);
562 lf_printf (file
, "#endif\n");
567 print_sim_engine_abort (lf
*file
, const char *message
)
569 lf_printf (file
, "sim_engine_abort (SD, CPU, cia, ");
570 lf_printf (file
, "\"%s\"", message
);
571 lf_printf (file
, ");\n");
576 print_include (lf
*file
, igen_module module
)
578 lf_printf (file
, "#include \"%s%s.h\"\n", module
.prefix
.l
, module
.suffix
.l
);
582 print_include_inline (lf
*file
, igen_module module
)
584 lf_printf (file
, "#if C_REVEALS_MODULE_P (%s_INLINE)\n", module
.suffix
.u
);
585 lf_printf (file
, "#include \"%s%s.c\"\n", module
.prefix
.l
, module
.suffix
.l
);
586 lf_printf (file
, "#else\n");
587 print_include (file
, module
);
588 lf_printf (file
, "#endif\n");
589 lf_printf (file
, "\n");
593 print_includes (lf
*file
)
595 lf_printf (file
, "\n");
596 lf_printf (file
, "#include \"sim-inline.c\"\n");
597 lf_printf (file
, "\n");
598 print_include_inline (file
, options
.module
.itable
);
599 print_include_inline (file
, options
.module
.idecode
);
600 print_include_inline (file
, options
.module
.support
);
604 /****************************************************************/
608 gen_semantics_h (lf
*file
, insn_list
*semantics
, int max_nr_words
)
612 for (word_nr
= -1; word_nr
<= max_nr_words
; word_nr
++)
614 lf_printf (file
, "typedef ");
615 print_semantic_function_type (file
);
616 lf_printf (file
, " %sidecode_semantic", options
.module
.global
.prefix
.l
);
618 lf_printf (file
, "_%d", word_nr
);
619 lf_printf (file
, "\n(");
620 lf_indent (file
, +1);
621 print_semantic_function_formal (file
, word_nr
);
622 lf_indent (file
, -1);
623 lf_printf (file
, ");\n");
624 lf_printf (file
, "\n");
626 switch (options
.gen
.code
)
629 for (semantic
= semantics
; semantic
!= NULL
; semantic
= semantic
->next
)
631 /* Ignore any special/internal instructions */
632 if (semantic
->insn
->nr_words
== 0)
634 print_semantic_declaration (file
,
636 semantic
->expanded_bits
,
638 semantic
->nr_prefetched_words
);
642 lf_print__this_file_is_empty (file
, "generating jumps");
649 gen_semantics_c (lf
*file
, insn_list
*semantics
, cache_entry
*cache_rules
)
651 if (options
.gen
.code
== generate_calls
)
654 print_includes (file
);
655 print_include (file
, options
.module
.semantics
);
656 lf_printf (file
, "\n");
658 for (semantic
= semantics
; semantic
!= NULL
; semantic
= semantic
->next
)
660 /* Ignore any special/internal instructions */
661 if (semantic
->insn
->nr_words
== 0)
663 print_semantic_definition (file
,
665 semantic
->expanded_bits
,
668 semantic
->nr_prefetched_words
);
673 lf_print__this_file_is_empty (file
, "generating jump engine");
678 /****************************************************************/
682 gen_icache_h (lf
*file
,
684 function_entry
* functions
, int max_nr_words
)
687 for (word_nr
= 0; word_nr
<= max_nr_words
; word_nr
++)
689 lf_printf (file
, "typedef ");
690 print_icache_function_type (file
);
691 lf_printf (file
, " %sidecode_icache_%d\n(",
692 options
.module
.global
.prefix
.l
, word_nr
);
693 print_icache_function_formal (file
, word_nr
);
694 lf_printf (file
, ");\n");
695 lf_printf (file
, "\n");
697 if (options
.gen
.code
== generate_calls
&& options
.gen
.icache
)
699 function_entry_traverse (file
, functions
,
700 print_icache_internal_function_declaration
,
702 while (semantic
!= NULL
)
704 print_icache_declaration (file
,
706 semantic
->expanded_bits
,
708 semantic
->nr_prefetched_words
);
709 semantic
= semantic
->next
;
714 lf_print__this_file_is_empty (file
, "generating jump engine");
719 gen_icache_c (lf
*file
,
721 function_entry
* functions
, cache_entry
*cache_rules
)
723 /* output `internal' invalid/floating-point unavailable functions
725 if (options
.gen
.code
== generate_calls
&& options
.gen
.icache
)
727 lf_printf (file
, "\n");
728 lf_printf (file
, "#include \"cpu.h\"\n");
729 lf_printf (file
, "#include \"idecode.h\"\n");
730 lf_printf (file
, "#include \"semantics.h\"\n");
731 lf_printf (file
, "#include \"icache.h\"\n");
732 lf_printf (file
, "#include \"support.h\"\n");
733 lf_printf (file
, "\n");
734 function_entry_traverse (file
, functions
,
735 print_icache_internal_function_definition
,
737 lf_printf (file
, "\n");
738 while (semantic
!= NULL
)
740 print_icache_definition (file
,
742 semantic
->expanded_bits
,
745 semantic
->nr_prefetched_words
);
746 semantic
= semantic
->next
;
751 lf_print__this_file_is_empty (file
, "generating jump engine");
756 /****************************************************************/
760 gen_idecode_h (lf
*file
,
761 gen_table
*gen
, insn_table
*insns
, cache_entry
*cache_rules
)
763 lf_printf (file
, "typedef unsigned%d %sinstruction_word;\n",
764 options
.insn_bit_size
, options
.module
.global
.prefix
.l
);
765 if (options
.gen
.delayed_branch
)
767 lf_printf (file
, "typedef struct _%sinstruction_address {\n",
768 options
.module
.global
.prefix
.l
);
769 lf_printf (file
, " address_word ip; /* instruction pointer */\n");
770 lf_printf (file
, " address_word dp; /* delayed-slot pointer */\n");
771 lf_printf (file
, "} %sinstruction_address;\n",
772 options
.module
.global
.prefix
.l
);
776 lf_printf (file
, "typedef address_word %sinstruction_address;\n",
777 options
.module
.global
.prefix
.l
);
780 if (options
.gen
.nia
== nia_is_invalid
781 && strlen (options
.module
.global
.prefix
.u
) > 0)
783 lf_indent_suppress (file
);
784 lf_printf (file
, "#define %sINVALID_INSTRUCTION_ADDRESS ",
785 options
.module
.global
.prefix
.u
);
786 lf_printf (file
, "INVALID_INSTRUCTION_ADDRESS\n");
788 lf_printf (file
, "\n");
789 print_icache_struct (file
, insns
, cache_rules
);
790 lf_printf (file
, "\n");
791 if (options
.gen
.icache
)
793 ERROR ("FIXME - idecode with icache suffering from bit-rot");
798 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
800 print_idecode_issue_function_header (file
,
801 (options
.gen
.multi_sim
804 is_function_declaration
,
805 1 /*ALWAYS ONE WORD */ );
807 if (options
.gen
.multi_sim
)
809 print_idecode_issue_function_header (file
,
811 is_function_variable
,
812 1 /*ALWAYS ONE WORD */ );
819 gen_idecode_c (lf
*file
,
820 gen_table
*gen
, insn_table
*isa
, cache_entry
*cache_rules
)
823 print_includes (file
);
824 print_include_inline (file
, options
.module
.semantics
);
825 lf_printf (file
, "\n");
827 print_idecode_globals (file
);
828 lf_printf (file
, "\n");
830 switch (options
.gen
.code
)
835 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
837 print_idecode_lookups (file
, entry
->table
, cache_rules
);
839 /* output the main idecode routine */
840 if (!options
.gen
.icache
)
842 print_idecode_issue_function_header (file
,
843 (options
.gen
.multi_sim
846 1 /*is definition */ ,
847 1 /*ALWAYS ONE WORD */ );
848 lf_printf (file
, "{\n");
849 lf_indent (file
, +2);
850 lf_printf (file
, "%sinstruction_address nia;\n",
851 options
.module
.global
.prefix
.l
);
852 print_idecode_body (file
, entry
->table
, "nia =");
853 lf_printf (file
, "return nia;");
854 lf_indent (file
, -2);
855 lf_printf (file
, "}\n");
862 lf_print__this_file_is_empty (file
, "generating a jump engine");
869 /****************************************************************/
873 gen_run_c (lf
*file
, gen_table
*gen
)
876 lf_printf (file
, "#include \"sim-main.h\"\n");
877 lf_printf (file
, "#include \"engine.h\"\n");
878 lf_printf (file
, "#include \"idecode.h\"\n");
879 lf_printf (file
, "#include \"bfd.h\"\n");
880 lf_printf (file
, "\n");
882 if (options
.gen
.multi_sim
)
884 print_idecode_issue_function_header (file
, NULL
, is_function_variable
,
886 lf_printf (file
, "\n");
887 print_engine_run_function_header (file
, NULL
, is_function_variable
);
888 lf_printf (file
, "\n");
891 lf_printf (file
, "void\n");
892 lf_printf (file
, "sim_engine_run (SIM_DESC sd,\n");
893 lf_printf (file
, " int next_cpu_nr,\n");
894 lf_printf (file
, " int nr_cpus,\n");
895 lf_printf (file
, " int siggnal)\n");
896 lf_printf (file
, "{\n");
897 lf_indent (file
, +2);
898 if (options
.gen
.multi_sim
)
900 lf_printf (file
, "int mach;\n");
901 lf_printf (file
, "if (STATE_ARCHITECTURE (sd) == NULL)\n");
902 lf_printf (file
, " mach = 0;\n");
903 lf_printf (file
, "else\n");
904 lf_printf (file
, " mach = STATE_ARCHITECTURE (sd)->mach;\n");
905 lf_printf (file
, "switch (mach)\n");
906 lf_printf (file
, " {\n");
907 lf_indent (file
, +2);
908 for (entry
= gen
->tables
; entry
!= NULL
; entry
= entry
->next
)
910 if (options
.gen
.default_model
!= NULL
911 && (strcmp (entry
->model
->name
, options
.gen
.default_model
) == 0
912 || strcmp (entry
->model
->full_name
,
913 options
.gen
.default_model
) == 0))
914 lf_printf (file
, "default:\n");
915 lf_printf (file
, "case bfd_mach_%s:\n", entry
->model
->full_name
);
916 lf_indent (file
, +2);
917 print_function_name (file
, "issue", NULL
, /* format name */
918 NULL
, /* NO processor */
919 NULL
, /* expanded bits */
920 function_name_prefix_idecode
);
921 lf_printf (file
, " = ");
922 print_function_name (file
, "issue", NULL
, /* format name */
923 entry
->model
->name
, NULL
, /* expanded bits */
924 function_name_prefix_idecode
);
925 lf_printf (file
, ";\n");
926 print_function_name (file
, "run", NULL
, /* format name */
927 NULL
, /* NO processor */
928 NULL
, /* expanded bits */
929 function_name_prefix_engine
);
930 lf_printf (file
, " = ");
931 print_function_name (file
, "run", NULL
, /* format name */
932 entry
->model
->name
, NULL
, /* expanded bits */
933 function_name_prefix_engine
);
934 lf_printf (file
, ";\n");
935 lf_printf (file
, "break;\n");
936 lf_indent (file
, -2);
938 if (options
.gen
.default_model
== NULL
)
940 lf_printf (file
, "default:\n");
941 lf_indent (file
, +2);
942 lf_printf (file
, "sim_engine_abort (sd, NULL, NULL_CIA,\n");
944 " \"sim_engine_run - unknown machine\");\n");
945 lf_printf (file
, "break;\n");
946 lf_indent (file
, -2);
948 lf_indent (file
, -2);
949 lf_printf (file
, " }\n");
951 print_function_name (file
, "run", NULL
, /* format name */
952 NULL
, /* NO processor */
953 NULL
, /* expanded bits */
954 function_name_prefix_engine
);
955 lf_printf (file
, " (sd, next_cpu_nr, nr_cpus, siggnal);\n");
956 lf_indent (file
, -2);
957 lf_printf (file
, "}\n");
960 /****************************************************************/
963 do_gen (insn_table
*isa
, decode_table
*decode_rules
)
966 if (decode_rules
== NULL
)
967 error (NULL
, "Must specify a decode table\n");
969 error (NULL
, "Must specify an instruction table\n");
970 if (decode_table_max_word_nr (decode_rules
) > 0)
971 options
.gen
.multi_word
= decode_table_max_word_nr (decode_rules
);
972 gen
= make_gen_tables (isa
, decode_rules
);
973 gen_tables_expand_insns (gen
);
974 gen_tables_expand_semantics (gen
);
978 /****************************************************************/
980 igen_options options
;
983 main (int argc
, char **argv
, char **envp
)
985 cache_entry
*cache_rules
= NULL
;
986 lf_file_references file_references
= lf_include_references
;
987 decode_table
*decode_rules
= NULL
;
988 insn_table
*isa
= NULL
;
989 gen_table
*gen
= NULL
;
990 char *real_file_name
= NULL
;
994 lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "igen");
1000 printf ("Usage:\n");
1002 printf (" igen <config-opts> ... <input-opts>... <output-opts>...\n");
1004 printf ("Config options:\n");
1006 printf (" -B <bit-size>\n");
1007 printf ("\t Set the number of bits in an instruction (deprecated).\n");
1009 ("\t This option can now be set directly in the instruction table.\n");
1011 printf (" -D <data-structure>\n");
1013 ("\t Dump the specified data structure to stdout. Valid structures include:\n");
1015 ("\t processor-names - list the names of all the processors (models)\n");
1017 printf (" -F <filter-list>\n");
1019 ("\t Filter out any instructions with a non-empty flags field that contains\n");
1020 printf ("\t a flag not listed in the <filter-list>.\n");
1022 printf (" -H <high-bit>\n");
1024 ("\t Set the number of the high (most significant) instruction bit (deprecated).\n");
1026 ("\t This option can now be set directly in the instruction table.\n");
1028 printf (" -I <directory>\n");
1030 ("\t Add <directory> to the list of directories searched when opening a file\n");
1032 printf (" -M <model-list>\n");
1034 ("\t Filter out any instructions that do not support at least one of the listed\n");
1036 ("\t models (An instructions with no model information is considered to support\n");
1037 printf ("\t all models.).\n");
1039 printf (" -N <nr-cpus>\n");
1040 printf ("\t Generate a simulator supporting <nr-cpus>\n");
1042 ("\t Specify `-N 0' to disable generation of the SMP. Specifying `-N 1' will\n");
1044 ("\t still generate an SMP enabled simulator but will only support one CPU.\n");
1046 printf (" -T <mechanism>\n");
1048 ("\t Override the decode mechanism specified by the decode rules\n");
1050 printf (" -P <prefix>\n");
1052 ("\t Prepend global names (except itable) with the string <prefix>.\n");
1054 ("\t Specify -P <module>=<prefix> to set a specific <module>'s prefix.\n");
1056 printf (" -S <suffix>\n");
1058 ("\t Replace a global name (suffix) (except itable) with the string <suffix>.\n");
1060 ("\t Specify -S <module>=<suffix> to change a specific <module>'s name (suffix).\n");
1062 printf (" -Werror\n");
1063 printf ("\t Make warnings errors\n");
1064 printf (" -Wnodiscard\n");
1066 ("\t Suppress warnings about discarded functions and instructions\n");
1067 printf (" -Wnowidth\n");
1069 ("\t Suppress warnings about instructions with invalid widths\n");
1070 printf (" -Wnounimplemented\n");
1071 printf ("\t Suppress warnings about unimplemented instructions\n");
1073 printf (" -G [!]<gen-option>\n");
1074 printf ("\t Any of the following options:\n");
1077 ("\t decode-duplicate - Override the decode rules, forcing the duplication of\n");
1078 printf ("\t semantic functions\n");
1080 ("\t decode-combine - Combine any duplicated entries within a table\n");
1082 ("\t decode-zero-reserved - Override the decode rules, forcing reserved bits to be\n");
1083 printf ("\t treated as zero.\n");
1085 ("\t decode-switch-is-goto - Overfide the padded-switch code type as a goto-switch\n");
1088 ("\t gen-conditional-issue - conditionally issue each instruction\n");
1090 ("\t gen-delayed-branch - need both cia and nia passed around\n");
1092 ("\t gen-direct-access - use #defines to directly access values\n");
1094 ("\t gen-zero-r<N> - arch assumes GPR(<N>) == 0, keep it that way\n");
1096 ("\t gen-icache[=<N> - generate an instruction cracking cache of size <N>\n");
1097 printf ("\t Default size is %d\n",
1098 options
.gen
.icache_size
);
1100 ("\t gen-insn-in-icache - save original instruction when cracking\n");
1102 ("\t gen-multi-sim[=MODEL] - generate multiple simulators - one per model\n");
1104 ("\t If specified MODEL is made the default architecture.\n");
1106 ("\t By default, a single simulator that will\n");
1108 ("\t execute any instruction is generated\n");
1110 ("\t gen-multi-word - generate code allowing for multi-word insns\n");
1112 ("\t gen-semantic-icache - include semantic code in cracking functions\n");
1114 ("\t gen-slot-verification - perform slot verification as part of decode\n");
1115 printf ("\t gen-nia-invalid - NIA defaults to nia_invalid\n");
1116 printf ("\t gen-nia-void - do not compute/return NIA\n");
1119 ("\t trace-combine - report combined entries a rule application\n");
1121 ("\t trace-entries - report entries after a rules application\n");
1122 printf ("\t trace-rule-rejection - report each rule as rejected\n");
1123 printf ("\t trace-rule-selection - report each rule as selected\n");
1125 ("\t trace-insn-insertion - report each instruction as it is inserted into a decode table\n");
1127 ("\t trace-rule-expansion - report each instruction as it is expanded (before insertion into a decode table)\n");
1128 printf ("\t trace-all - enable all trace options\n");
1131 ("\t field-widths - instruction formats specify widths (deprecated)\n");
1133 ("\t By default, an instruction format specifies bit\n");
1134 printf ("\t positions\n");
1136 ("\t This option can now be set directly in the\n");
1137 printf ("\t instruction table\n");
1139 ("\t jumps - use jumps instead of function calls\n");
1141 ("\t omit-line-numbers - do not include line number information in the output\n");
1143 printf ("Input options:\n");
1145 printf (" -k <cache-rules> (deprecated)\n");
1146 printf (" -o <decode-rules>\n");
1147 printf (" -i <instruction-table>\n");
1149 printf ("Output options:\n");
1151 printf (" -x Perform expansion (required)\n");
1153 (" -n <real-name> Specify the real name of the next output file\n");
1155 (" -h Generate the header (.h) file rather than the body (.c)\n");
1156 printf (" -c <output-file> output icache\n");
1157 printf (" -d <output-file> output idecode\n");
1158 printf (" -e <output-file> output engine\n");
1159 printf (" -f <output-file> output support functions\n");
1160 printf (" -m <output-file> output model\n");
1161 printf (" -r <output-file> output multi-sim run\n");
1162 printf (" -s <output-file> output schematic\n");
1163 printf (" -t <output-file> output itable\n");
1166 while ((ch
= getopt (argc
, argv
,
1167 "B:D:F:G:H:I:M:N:P:T:W:o:k:i:n:hc:d:e:m:r:s:t:f:x"))
1170 fprintf (stderr
, " -%c ", ch
);
1172 fprintf (stderr
, "%s ", optarg
);
1173 fprintf (stderr
, "\\\n");
1179 filter_parse (&options
.model_filter
, optarg
);
1183 if (strcmp (optarg
, "processor-names"))
1186 for (processor
= filter_next (options
.model_filter
, "");
1188 processor
= filter_next (options
.model_filter
, processor
))
1189 lf_printf (standard_out
, "%s\n", processor
);
1192 error (NULL
, "Unknown data structure %s, not dumped\n", optarg
);
1196 filter_parse (&options
.flags_filter
, optarg
);
1201 table_include
**dir
= &options
.include
;
1202 while ((*dir
) != NULL
)
1203 dir
= &(*dir
)->next
;
1204 (*dir
) = ZALLOC (table_include
);
1205 (*dir
)->dir
= strdup (optarg
);
1210 options
.insn_bit_size
= a2i (optarg
);
1211 if (options
.insn_bit_size
<= 0
1212 || options
.insn_bit_size
> max_insn_bit_size
)
1214 error (NULL
, "Instruction bitsize must be in range 1..%d\n",
1217 if (options
.hi_bit_nr
!= options
.insn_bit_size
- 1
1218 && options
.hi_bit_nr
!= 0)
1220 error (NULL
, "Conflict betweem hi-bit-nr and insn-bit-size\n");
1225 options
.hi_bit_nr
= a2i (optarg
);
1226 if (options
.hi_bit_nr
!= options
.insn_bit_size
- 1
1227 && options
.hi_bit_nr
!= 0)
1229 error (NULL
, "Conflict between hi-bit-nr and insn-bit-size\n");
1234 options
.gen
.smp
= a2i (optarg
);
1243 chp
= strchr (optarg
, '=');
1246 names
= &options
.module
.global
;
1251 chp
= chp
+ 1; /* skip `=' */
1253 if (strncmp (optarg
, "global=", chp
- optarg
) == 0)
1255 names
= &options
.module
.global
;
1257 if (strncmp (optarg
, "engine=", chp
- optarg
) == 0)
1259 names
= &options
.module
.engine
;
1261 if (strncmp (optarg
, "icache=", chp
- optarg
) == 0)
1263 names
= &options
.module
.icache
;
1265 if (strncmp (optarg
, "idecode=", chp
- optarg
) == 0)
1267 names
= &options
.module
.idecode
;
1269 if (strncmp (optarg
, "itable=", chp
- optarg
) == 0)
1271 names
= &options
.module
.itable
;
1273 if (strncmp (optarg
, "semantics=", chp
- optarg
) == 0)
1275 names
= &options
.module
.semantics
;
1277 if (strncmp (optarg
, "support=", chp
- optarg
) == 0)
1279 names
= &options
.module
.support
;
1283 error (NULL
, "Prefix `%s' unreconized\n", optarg
);
1289 name
= &names
->prefix
;
1292 name
= &names
->suffix
;
1295 abort (); /* Bad switch. */
1297 name
->u
= strdup (chp
);
1298 name
->l
= strdup (chp
);
1303 *chp
= toupper (*chp
);
1306 if (name
== &options
.module
.global
.prefix
)
1308 options
.module
.engine
.prefix
= options
.module
.global
.prefix
;
1309 options
.module
.icache
.prefix
= options
.module
.global
.prefix
;
1310 options
.module
.idecode
.prefix
= options
.module
.global
.prefix
;
1311 /* options.module.itable.prefix = options.module.global.prefix; */
1312 options
.module
.semantics
.prefix
=
1313 options
.module
.global
.prefix
;
1314 options
.module
.support
.prefix
= options
.module
.global
.prefix
;
1316 if (name
== &options
.module
.global
.suffix
)
1318 options
.module
.engine
.suffix
= options
.module
.global
.suffix
;
1319 options
.module
.icache
.suffix
= options
.module
.global
.suffix
;
1320 options
.module
.idecode
.suffix
= options
.module
.global
.suffix
;
1321 /* options.module.itable.suffix = options.module.global.suffix; */
1322 options
.module
.semantics
.suffix
=
1323 options
.module
.global
.suffix
;
1324 options
.module
.support
.suffix
= options
.module
.global
.suffix
;
1331 if (strcmp (optarg
, "error") == 0)
1332 options
.warning
= error
;
1333 else if (strcmp (optarg
, "nodiscard") == 0)
1334 options
.warn
.discard
= 0;
1335 else if (strcmp (optarg
, "discard") == 0)
1336 options
.warn
.discard
= 1;
1337 else if (strcmp (optarg
, "nowidth") == 0)
1338 options
.warn
.width
= 0;
1339 else if (strcmp (optarg
, "width") == 0)
1340 options
.warn
.width
= 1;
1341 else if (strcmp (optarg
, "nounimplemented") == 0)
1342 options
.warn
.unimplemented
= 0;
1343 else if (strcmp (optarg
, "unimplemented") == 0)
1344 options
.warn
.unimplemented
= 1;
1346 error (NULL
, "Unknown -W argument `%s'\n", optarg
);
1355 if (strncmp (optarg
, "no-", strlen ("no-")) == 0)
1357 argp
= optarg
+ strlen ("no-");
1360 else if (strncmp (optarg
, "!", strlen ("!")) == 0)
1362 argp
= optarg
+ strlen ("no-");
1370 if (strcmp (argp
, "decode-duplicate") == 0)
1372 options
.decode
.duplicate
= enable_p
;
1374 else if (strcmp (argp
, "decode-combine") == 0)
1376 options
.decode
.combine
= enable_p
;
1378 else if (strcmp (argp
, "decode-zero-reserved") == 0)
1380 options
.decode
.zero_reserved
= enable_p
;
1383 else if (strcmp (argp
, "gen-conditional-issue") == 0)
1385 options
.gen
.conditional_issue
= enable_p
;
1387 else if (strcmp (argp
, "conditional-issue") == 0)
1389 options
.gen
.conditional_issue
= enable_p
;
1390 options
.warning (NULL
,
1391 "Option conditional-issue replaced by gen-conditional-issue\n");
1393 else if (strcmp (argp
, "gen-delayed-branch") == 0)
1395 options
.gen
.delayed_branch
= enable_p
;
1397 else if (strcmp (argp
, "delayed-branch") == 0)
1399 options
.gen
.delayed_branch
= enable_p
;
1400 options
.warning (NULL
,
1401 "Option delayed-branch replaced by gen-delayed-branch\n");
1403 else if (strcmp (argp
, "gen-direct-access") == 0)
1405 options
.gen
.direct_access
= enable_p
;
1407 else if (strcmp (argp
, "direct-access") == 0)
1409 options
.gen
.direct_access
= enable_p
;
1410 options
.warning (NULL
,
1411 "Option direct-access replaced by gen-direct-access\n");
1413 else if (strncmp (argp
, "gen-zero-r", strlen ("gen-zero-r")) == 0)
1415 options
.gen
.zero_reg
= enable_p
;
1416 options
.gen
.zero_reg_nr
= atoi (argp
+ strlen ("gen-zero-r"));
1418 else if (strncmp (argp
, "zero-r", strlen ("zero-r")) == 0)
1420 options
.gen
.zero_reg
= enable_p
;
1421 options
.gen
.zero_reg_nr
= atoi (argp
+ strlen ("zero-r"));
1422 options
.warning (NULL
,
1423 "Option zero-r<N> replaced by gen-zero-r<N>\n");
1425 else if (strncmp (argp
, "gen-icache", strlen ("gen-icache")) == 0)
1427 switch (argp
[strlen ("gen-icache")])
1430 options
.gen
.icache_size
=
1431 atoi (argp
+ strlen ("gen-icache") + 1);
1432 options
.gen
.icache
= enable_p
;
1435 options
.gen
.icache
= enable_p
;
1439 "Expecting -Ggen-icache or -Ggen-icache=<N>\n");
1442 else if (strcmp (argp
, "gen-insn-in-icache") == 0)
1444 options
.gen
.insn_in_icache
= enable_p
;
1446 else if (strncmp (argp
, "gen-multi-sim", strlen ("gen-multi-sim"))
1449 char *arg
= &argp
[strlen ("gen-multi-sim")];
1453 options
.gen
.multi_sim
= enable_p
;
1454 options
.gen
.default_model
= arg
+ 1;
1455 if (!filter_is_member
1456 (options
.model_filter
, options
.gen
.default_model
))
1457 error (NULL
, "multi-sim model %s unknown\n",
1458 options
.gen
.default_model
);
1461 options
.gen
.multi_sim
= enable_p
;
1462 options
.gen
.default_model
= NULL
;
1466 "Expecting -Ggen-multi-sim or -Ggen-multi-sim=<MODEL>\n");
1470 else if (strcmp (argp
, "gen-multi-word") == 0)
1472 options
.gen
.multi_word
= enable_p
;
1474 else if (strcmp (argp
, "gen-semantic-icache") == 0)
1476 options
.gen
.semantic_icache
= enable_p
;
1478 else if (strcmp (argp
, "gen-slot-verification") == 0)
1480 options
.gen
.slot_verification
= enable_p
;
1482 else if (strcmp (argp
, "verify-slot") == 0)
1484 options
.gen
.slot_verification
= enable_p
;
1485 options
.warning (NULL
,
1486 "Option verify-slot replaced by gen-slot-verification\n");
1488 else if (strcmp (argp
, "gen-nia-invalid") == 0)
1490 options
.gen
.nia
= nia_is_invalid
;
1492 else if (strcmp (argp
, "default-nia-minus-one") == 0)
1494 options
.gen
.nia
= nia_is_invalid
;
1495 options
.warning (NULL
,
1496 "Option default-nia-minus-one replaced by gen-nia-invalid\n");
1498 else if (strcmp (argp
, "gen-nia-void") == 0)
1500 options
.gen
.nia
= nia_is_void
;
1502 else if (strcmp (argp
, "trace-all") == 0)
1504 memset (&options
.trace
, enable_p
, sizeof (options
.trace
));
1506 else if (strcmp (argp
, "trace-combine") == 0)
1508 options
.trace
.combine
= enable_p
;
1510 else if (strcmp (argp
, "trace-entries") == 0)
1512 options
.trace
.entries
= enable_p
;
1514 else if (strcmp (argp
, "trace-rule-rejection") == 0)
1516 options
.trace
.rule_rejection
= enable_p
;
1518 else if (strcmp (argp
, "trace-rule-selection") == 0)
1520 options
.trace
.rule_selection
= enable_p
;
1522 else if (strcmp (argp
, "trace-insn-insertion") == 0)
1524 options
.trace
.insn_insertion
= enable_p
;
1526 else if (strcmp (argp
, "trace-insn-expansion") == 0)
1528 options
.trace
.insn_expansion
= enable_p
;
1530 else if (strcmp (argp
, "jumps") == 0)
1532 options
.gen
.code
= generate_jumps
;
1534 else if (strcmp (argp
, "field-widths") == 0)
1536 options
.insn_specifying_widths
= enable_p
;
1538 else if (strcmp (argp
, "omit-line-numbers") == 0)
1540 file_references
= lf_omit_references
;
1544 error (NULL
, "Unknown option %s\n", optarg
);
1550 isa
= load_insn_table (optarg
, cache_rules
);
1551 if (isa
->illegal_insn
== NULL
)
1552 error (NULL
, "illegal-instruction missing from insn table\n");
1556 gen
= do_gen (isa
, decode_rules
);
1560 decode_rules
= load_decode_table (optarg
);
1565 error (NULL
, "Cache file must appear before the insn file\n");
1566 cache_rules
= load_cache_table (optarg
);
1570 real_file_name
= strdup (optarg
);
1586 lf
*file
= lf_open (optarg
, real_file_name
, file_references
,
1587 (is_header
? lf_is_h
: lf_is_c
),
1589 if (gen
== NULL
&& ch
!= 't' && ch
!= 'm' && ch
!= 'f')
1591 options
.warning (NULL
,
1592 "Explicitly generate tables with -x option\n");
1593 gen
= do_gen (isa
, decode_rules
);
1595 lf_print__file_start (file
);
1600 gen_model_h (file
, isa
);
1602 gen_model_c (file
, isa
);
1606 gen_itable_h (file
, isa
);
1608 gen_itable_c (file
, isa
);
1612 gen_support_h (file
, isa
);
1614 gen_support_c (file
, isa
);
1618 options
.warning (NULL
, "-hr option ignored\n");
1620 gen_run_c (file
, gen
);
1624 gen_semantics_h (file
, gen
->semantics
, isa
->max_nr_words
);
1626 gen_semantics_c (file
, gen
->semantics
, isa
->caches
);
1630 gen_idecode_h (file
, gen
, isa
, cache_rules
);
1632 gen_idecode_c (file
, gen
, isa
, cache_rules
);
1636 gen_engine_h (file
, gen
, isa
, cache_rules
);
1638 gen_engine_c (file
, gen
, isa
, cache_rules
);
1644 isa
->functions
, isa
->max_nr_words
);
1647 gen
->semantics
, isa
->functions
, cache_rules
);
1650 lf_print__file_finish (file
);
1654 real_file_name
= NULL
;
1657 ERROR ("Bad switch");