1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2013 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/>. */
35 #include "ld-decode.h"
40 #include "gen-model.h"
41 #include "gen-icache.h"
42 #include "gen-itable.h"
43 #include "gen-idecode.h"
44 #include "gen-semantics.h"
45 #include "gen-engine.h"
46 #include "gen-support.h"
47 #include "gen-engine.h"
50 /****************************************************************/
53 /* Semantic functions */
56 print_semantic_function_formal (lf *file, int nr_prefetched_words)
60 if (options.gen.icache || nr_prefetched_words < 0)
62 nr += lf_printf (file, "SIM_DESC sd,\n");
63 nr += lf_printf (file, "%sidecode_cache *cache_entry,\n",
64 options.module.global.prefix.l);
65 nr += lf_printf (file, "%sinstruction_address cia",
66 options.module.global.prefix.l);
68 else if (options.gen.smp)
70 nr += lf_printf (file, "sim_cpu *cpu,\n");
71 for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
73 nr += lf_printf (file, "%sinstruction_word instruction_%d,\n",
74 options.module.global.prefix.l, word_nr);
76 nr += lf_printf (file, "%sinstruction_address cia",
77 options.module.global.prefix.l);
81 nr += lf_printf (file, "SIM_DESC sd,\n");
82 for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
84 nr += lf_printf (file, "%sinstruction_word instruction_%d,\n",
85 options.module.global.prefix.l, word_nr);
87 nr += lf_printf (file, "%sinstruction_address cia",
88 options.module.global.prefix.l);
94 print_semantic_function_actual (lf *file, int nr_prefetched_words)
98 if (options.gen.icache || nr_prefetched_words < 0)
100 nr += lf_printf (file, "sd, cache_entry, cia");
105 nr += lf_printf (file, "cpu");
107 nr += lf_printf (file, "sd");
108 for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
109 nr += lf_printf (file, ", instruction_%d", word_nr);
110 nr += lf_printf (file, ", cia");
116 print_semantic_function_type (lf *file)
119 nr += lf_printf (file, "%sinstruction_address",
120 options.module.global.prefix.l);
125 /* Idecode functions */
128 print_icache_function_formal (lf *file, int nr_prefetched_words)
133 nr += lf_printf (file, "sim_cpu *cpu,\n");
135 nr += lf_printf (file, "SIM_DESC sd,\n");
136 for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
137 nr += lf_printf (file, " %sinstruction_word instruction_%d,\n",
138 options.module.global.prefix.l, word_nr);
139 nr += lf_printf (file, " %sinstruction_address cia,\n",
140 options.module.global.prefix.l);
141 nr += lf_printf (file, " %sidecode_cache *cache_entry",
142 options.module.global.prefix.l);
147 print_icache_function_actual (lf *file, int nr_prefetched_words)
152 nr += lf_printf (file, "cpu");
154 nr += lf_printf (file, "sd");
155 for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
156 nr += lf_printf (file, ", instruction_%d", word_nr);
157 nr += lf_printf (file, ", cia, cache_entry");
162 print_icache_function_type (lf *file)
165 if (options.gen.semantic_icache)
167 nr = print_semantic_function_type (file);
171 nr = lf_printf (file, "%sidecode_semantic *",
172 options.module.global.prefix.l);
181 print_opcode_bits (lf *file, opcode_bits *bits)
186 nr += lf_putchr (file, '_');
187 nr += lf_putstr (file, bits->field->val_string);
188 if (bits->opcode->is_boolean && bits->value == 0)
189 nr += lf_putint (file, bits->opcode->boolean_constant);
190 else if (!bits->opcode->is_boolean)
192 if (bits->opcode->last < bits->field->last)
195 bits->value << (bits->field->last - bits->opcode->last));
197 nr += lf_putint (file, bits->value);
199 nr += print_opcode_bits (file, bits->next);
204 print_c_name (lf *file, const char *name)
208 for (pos = name; *pos != '\0'; pos++)
217 nr += lf_putchr (file, '_');
220 nr += lf_putchr (file, *pos);
228 print_function_name (lf *file,
229 const char *basename,
230 const char *format_name,
231 const char *model_name,
232 opcode_bits *expanded_bits,
233 lf_function_name_prefixes prefix)
239 case function_name_prefix_semantics:
240 nr += lf_printf (file, "%s", options.module.semantics.prefix.l);
241 nr += lf_printf (file, "semantic_");
243 case function_name_prefix_idecode:
244 nr += lf_printf (file, "%s", options.module.idecode.prefix.l);
245 nr += lf_printf (file, "idecode_");
247 case function_name_prefix_itable:
248 nr += lf_printf (file, "%sitable_", options.module.itable.prefix.l);
250 case function_name_prefix_icache:
251 nr += lf_printf (file, "%s", options.module.icache.prefix.l);
252 nr += lf_printf (file, "icache_");
254 case function_name_prefix_engine:
255 nr += lf_printf (file, "%s", options.module.engine.prefix.l);
256 nr += lf_printf (file, "engine_");
261 if (model_name != NULL)
263 nr += print_c_name (file, model_name);
264 nr += lf_printf (file, "_");
267 /* the function name */
268 nr += print_c_name (file, basename);
270 /* the format name if available */
271 if (format_name != NULL)
273 nr += lf_printf (file, "_");
274 nr += print_c_name (file, format_name);
278 nr += print_opcode_bits (file, expanded_bits);
285 print_my_defines (lf *file,
286 const char *basename,
287 const char *format_name, opcode_bits *expanded_bits)
289 /* #define MY_INDEX xxxxx */
290 lf_indent_suppress (file);
291 lf_printf (file, "#undef MY_INDEX\n");
292 lf_indent_suppress (file);
293 lf_printf (file, "#define MY_INDEX ");
294 print_function_name (file,
295 basename, format_name, NULL,
296 NULL, function_name_prefix_itable);
297 lf_printf (file, "\n");
298 /* #define MY_PREFIX xxxxxx */
299 lf_indent_suppress (file);
300 lf_printf (file, "#undef ");
301 print_function_name (file,
302 basename, format_name, NULL,
303 expanded_bits, function_name_prefix_none);
304 lf_printf (file, "\n");
305 lf_indent_suppress (file);
306 lf_printf (file, "#undef MY_PREFIX\n");
307 lf_indent_suppress (file);
308 lf_printf (file, "#define MY_PREFIX ");
309 print_function_name (file,
310 basename, format_name, NULL,
311 expanded_bits, function_name_prefix_none);
312 lf_printf (file, "\n");
313 /* #define MY_NAME xxxxxx */
314 lf_indent_suppress (file);
315 lf_indent_suppress (file);
316 lf_printf (file, "#undef MY_NAME\n");
317 lf_indent_suppress (file);
318 lf_printf (file, "#define MY_NAME \"");
319 print_function_name (file,
320 basename, format_name, NULL,
321 expanded_bits, function_name_prefix_none);
322 lf_printf (file, "\"\n");
327 print_itrace_prefix (lf *file)
329 const char *prefix = "trace_prefix (";
330 int indent = strlen (prefix);
331 lf_printf (file, "%sSD, CPU, cia, CIA, TRACE_LINENUM_P (CPU), \\\n",
333 lf_indent (file, +indent);
334 lf_printf (file, "%sitable[MY_INDEX].file, \\\n",
335 options.module.itable.prefix.l);
336 lf_printf (file, "%sitable[MY_INDEX].line_nr, \\\n",
337 options.module.itable.prefix.l);
338 lf_printf (file, "\"");
344 print_itrace_format (lf *file, insn_mnemonic_entry *assembler)
346 /* pass=1 is fmt string; pass=2 is arguments */
348 /* print the format string */
349 for (pass = 1; pass <= 2; pass++)
351 const char *chp = assembler->format;
352 chp++; /* skip the leading quote */
353 /* write out the format/args */
356 if (chp[0] == '\\' && (chp[1] == '<' || chp[1] == '>'))
359 lf_putchr (file, chp[1]);
362 else if (chp[0] == '<' || chp[0] == '%')
364 /* parse [ "%" ... ] "<" [ func "#" ] param ">" */
370 /* the "%" ... "<" format */
372 while (chp[0] != '<' && chp[0] != '\0')
375 error (assembler->line, "Missing `<' after `%%'\n");
377 /* [ "func" # ] OR "param" */
380 while (chp[0] != '>' && chp[0] != '#' && chp[0] != '\0')
382 strlen_func = chp - func;
387 while (chp[0] != '>' && chp[0] != '\0')
390 strlen_param = chp - param;
392 error (assembler->line,
393 "Missing closing `>' in assembler string\n");
397 lf_printf (file, ", \\\n");
398 if (strncmp (fmt, "<", 1) == 0)
399 /* implicit long int format */
402 lf_printf (file, "%%ld");
405 lf_printf (file, "(long) ");
406 lf_write (file, param, strlen_param);
409 else if (strncmp (fmt, "%<", 2) == 0)
410 /* explicit format */
413 lf_printf (file, "%%");
415 lf_write (file, param, strlen_param);
417 else if (strncmp (fmt, "%s<", 3) == 0)
421 lf_printf (file, "%%s");
424 lf_printf (file, "%sstr_",
425 options.module.global.prefix.l);
426 lf_write (file, func, strlen_func);
427 lf_printf (file, " (SD_, ");
428 lf_write (file, param, strlen_param);
429 lf_printf (file, ")");
432 else if (strncmp (fmt, "%lx<", 4) == 0)
436 lf_printf (file, "%%lx");
439 lf_printf (file, "(unsigned long) ");
440 lf_write (file, param, strlen_param);
443 else if (strncmp (fmt, "%#lx<", 5) == 0)
444 /* simple hex with 0x prefix */
447 lf_printf (file, "%%#lx");
450 lf_printf (file, "(unsigned long) ");
451 lf_write (file, param, strlen_param);
454 else if (strncmp (fmt, "%08lx<", 6) == 0)
458 lf_printf (file, "%%08lx");
461 lf_printf (file, "(unsigned long) ");
462 lf_write (file, param, strlen_param);
466 error (assembler->line, "Unknown assembler string format\n");
471 lf_putchr (file, chp[0]);
476 lf_printf (file, ");\n");
481 print_itrace (lf *file, insn_entry * insn, int idecode)
483 /* NB: Here we escape each EOLN. This is so that the the compiler
484 treats a trace function call as a single line. Consequently any
485 errors in the line are refered back to the same igen assembler
487 const char *phase = (idecode) ? "DECODE" : "INSN";
488 lf_printf (file, "\n");
489 lf_indent_suppress (file);
490 lf_printf (file, "#if defined (WITH_TRACE)\n");
491 lf_printf (file, "/* generate a trace prefix if any tracing enabled */\n");
492 lf_printf (file, "if (TRACE_ANY_P (CPU))\n");
493 lf_printf (file, " {\n");
494 lf_indent (file, +4);
496 if (insn->mnemonics != NULL)
498 insn_mnemonic_entry *assembler = insn->mnemonics;
502 if (assembler->condition != NULL)
505 lf_printf (file, "%sif (%s)\n",
506 is_first ? "" : "else ", assembler->condition);
507 lf_indent (file, +2);
508 lf_print__line_ref (file, assembler->line);
509 indent = print_itrace_prefix (file);
510 print_itrace_format (file, assembler);
511 lf_print__internal_ref (file);
512 lf_indent (file, -indent);
513 lf_indent (file, -2);
514 if (assembler->next == NULL)
515 error (assembler->line,
516 "Missing final unconditional assembler\n");
523 lf_printf (file, "else\n");
524 lf_indent (file, +2);
526 lf_print__line_ref (file, assembler->line);
527 indent = print_itrace_prefix (file);
528 print_itrace_format (file, assembler);
529 lf_print__internal_ref (file);
530 lf_indent (file, -indent);
532 lf_indent (file, -2);
533 if (assembler->next != NULL)
534 error (assembler->line,
535 "Unconditional assembler is not last\n");
538 assembler = assembler->next;
540 while (assembler != NULL);
545 lf_indent (file, +2);
546 lf_print__line_ref (file, insn->line);
547 indent = print_itrace_prefix (file);
548 lf_printf (file, "%%s\", \\\n");
549 lf_printf (file, "itable[MY_INDEX].name);\n");
550 lf_print__internal_ref (file);
551 lf_indent (file, -indent);
552 lf_indent (file, -2);
554 lf_printf (file, "/* trace the instruction execution if enabled */\n");
555 lf_printf (file, "if (TRACE_%s_P (CPU))\n", phase);
557 " trace_generic (SD, CPU, TRACE_%s_IDX, \" %%s\", itable[MY_INDEX].name);\n",
560 lf_indent (file, -4);
561 lf_printf (file, " }\n");
562 lf_indent_suppress (file);
563 lf_printf (file, "#endif\n");
568 print_sim_engine_abort (lf *file, const char *message)
570 lf_printf (file, "sim_engine_abort (SD, CPU, cia, ");
571 lf_printf (file, "\"%s\"", message);
572 lf_printf (file, ");\n");
577 print_include (lf *file, igen_module module)
579 lf_printf (file, "#include \"%s%s.h\"\n", module.prefix.l, module.suffix.l);
583 print_include_inline (lf *file, igen_module module)
585 lf_printf (file, "#if C_REVEALS_MODULE_P (%s_INLINE)\n", module.suffix.u);
586 lf_printf (file, "#include \"%s%s.c\"\n", module.prefix.l, module.suffix.l);
587 lf_printf (file, "#else\n");
588 print_include (file, module);
589 lf_printf (file, "#endif\n");
590 lf_printf (file, "\n");
594 print_includes (lf *file)
596 lf_printf (file, "\n");
597 lf_printf (file, "#include \"sim-inline.c\"\n");
598 lf_printf (file, "\n");
599 print_include_inline (file, options.module.itable);
600 print_include_inline (file, options.module.idecode);
601 print_include_inline (file, options.module.support);
605 /****************************************************************/
609 gen_semantics_h (lf *file, insn_list *semantics, int max_nr_words)
613 for (word_nr = -1; word_nr <= max_nr_words; word_nr++)
615 lf_printf (file, "typedef ");
616 print_semantic_function_type (file);
617 lf_printf (file, " %sidecode_semantic", options.module.global.prefix.l);
619 lf_printf (file, "_%d", word_nr);
620 lf_printf (file, "\n(");
621 lf_indent (file, +1);
622 print_semantic_function_formal (file, word_nr);
623 lf_indent (file, -1);
624 lf_printf (file, ");\n");
625 lf_printf (file, "\n");
627 switch (options.gen.code)
630 for (semantic = semantics; semantic != NULL; semantic = semantic->next)
632 /* Ignore any special/internal instructions */
633 if (semantic->insn->nr_words == 0)
635 print_semantic_declaration (file,
637 semantic->expanded_bits,
639 semantic->nr_prefetched_words);
643 lf_print__this_file_is_empty (file, "generating jumps");
650 gen_semantics_c (lf *file, insn_list *semantics, cache_entry *cache_rules)
652 if (options.gen.code == generate_calls)
655 print_includes (file);
656 print_include (file, options.module.semantics);
657 lf_printf (file, "\n");
659 for (semantic = semantics; semantic != NULL; semantic = semantic->next)
661 /* Ignore any special/internal instructions */
662 if (semantic->insn->nr_words == 0)
664 print_semantic_definition (file,
666 semantic->expanded_bits,
669 semantic->nr_prefetched_words);
674 lf_print__this_file_is_empty (file, "generating jump engine");
679 /****************************************************************/
683 gen_icache_h (lf *file,
685 function_entry * functions, int max_nr_words)
688 for (word_nr = 0; word_nr <= max_nr_words; word_nr++)
690 lf_printf (file, "typedef ");
691 print_icache_function_type (file);
692 lf_printf (file, " %sidecode_icache_%d\n(",
693 options.module.global.prefix.l, word_nr);
694 print_icache_function_formal (file, word_nr);
695 lf_printf (file, ");\n");
696 lf_printf (file, "\n");
698 if (options.gen.code == generate_calls && options.gen.icache)
700 function_entry_traverse (file, functions,
701 print_icache_internal_function_declaration,
703 while (semantic != NULL)
705 print_icache_declaration (file,
707 semantic->expanded_bits,
709 semantic->nr_prefetched_words);
710 semantic = semantic->next;
715 lf_print__this_file_is_empty (file, "generating jump engine");
720 gen_icache_c (lf *file,
722 function_entry * functions, cache_entry *cache_rules)
724 /* output `internal' invalid/floating-point unavailable functions
726 if (options.gen.code == generate_calls && options.gen.icache)
728 lf_printf (file, "\n");
729 lf_printf (file, "#include \"cpu.h\"\n");
730 lf_printf (file, "#include \"idecode.h\"\n");
731 lf_printf (file, "#include \"semantics.h\"\n");
732 lf_printf (file, "#include \"icache.h\"\n");
733 lf_printf (file, "#include \"support.h\"\n");
734 lf_printf (file, "\n");
735 function_entry_traverse (file, functions,
736 print_icache_internal_function_definition,
738 lf_printf (file, "\n");
739 while (semantic != NULL)
741 print_icache_definition (file,
743 semantic->expanded_bits,
746 semantic->nr_prefetched_words);
747 semantic = semantic->next;
752 lf_print__this_file_is_empty (file, "generating jump engine");
757 /****************************************************************/
761 gen_idecode_h (lf *file,
762 gen_table *gen, insn_table *insns, cache_entry *cache_rules)
764 lf_printf (file, "typedef unsigned%d %sinstruction_word;\n",
765 options.insn_bit_size, options.module.global.prefix.l);
766 if (options.gen.delayed_branch)
768 lf_printf (file, "typedef struct _%sinstruction_address {\n",
769 options.module.global.prefix.l);
770 lf_printf (file, " address_word ip; /* instruction pointer */\n");
771 lf_printf (file, " address_word dp; /* delayed-slot pointer */\n");
772 lf_printf (file, "} %sinstruction_address;\n",
773 options.module.global.prefix.l);
777 lf_printf (file, "typedef address_word %sinstruction_address;\n",
778 options.module.global.prefix.l);
781 if (options.gen.nia == nia_is_invalid
782 && strlen (options.module.global.prefix.u) > 0)
784 lf_indent_suppress (file);
785 lf_printf (file, "#define %sINVALID_INSTRUCTION_ADDRESS ",
786 options.module.global.prefix.u);
787 lf_printf (file, "INVALID_INSTRUCTION_ADDRESS\n");
789 lf_printf (file, "\n");
790 print_icache_struct (file, insns, cache_rules);
791 lf_printf (file, "\n");
792 if (options.gen.icache)
794 ERROR ("FIXME - idecode with icache suffering from bit-rot");
799 for (entry = gen->tables; entry != NULL; entry = entry->next)
801 print_idecode_issue_function_header (file,
802 (options.gen.multi_sim
805 is_function_declaration,
806 1 /*ALWAYS ONE WORD */ );
808 if (options.gen.multi_sim)
810 print_idecode_issue_function_header (file,
812 is_function_variable,
813 1 /*ALWAYS ONE WORD */ );
820 gen_idecode_c (lf *file,
821 gen_table *gen, insn_table *isa, cache_entry *cache_rules)
824 print_includes (file);
825 print_include_inline (file, options.module.semantics);
826 lf_printf (file, "\n");
828 print_idecode_globals (file);
829 lf_printf (file, "\n");
831 switch (options.gen.code)
836 for (entry = gen->tables; entry != NULL; entry = entry->next)
838 print_idecode_lookups (file, entry->table, cache_rules);
840 /* output the main idecode routine */
841 if (!options.gen.icache)
843 print_idecode_issue_function_header (file,
844 (options.gen.multi_sim
847 1 /*is definition */ ,
848 1 /*ALWAYS ONE WORD */ );
849 lf_printf (file, "{\n");
850 lf_indent (file, +2);
851 lf_printf (file, "%sinstruction_address nia;\n",
852 options.module.global.prefix.l);
853 print_idecode_body (file, entry->table, "nia =");
854 lf_printf (file, "return nia;");
855 lf_indent (file, -2);
856 lf_printf (file, "}\n");
863 lf_print__this_file_is_empty (file, "generating a jump engine");
870 /****************************************************************/
874 gen_run_c (lf *file, gen_table *gen)
877 lf_printf (file, "#include \"sim-main.h\"\n");
878 lf_printf (file, "#include \"engine.h\"\n");
879 lf_printf (file, "#include \"idecode.h\"\n");
880 lf_printf (file, "#include \"bfd.h\"\n");
881 lf_printf (file, "\n");
883 if (options.gen.multi_sim)
885 print_idecode_issue_function_header (file, NULL, is_function_variable,
887 lf_printf (file, "\n");
888 print_engine_run_function_header (file, NULL, is_function_variable);
889 lf_printf (file, "\n");
892 lf_printf (file, "void\n");
893 lf_printf (file, "sim_engine_run (SIM_DESC sd,\n");
894 lf_printf (file, " int next_cpu_nr,\n");
895 lf_printf (file, " int nr_cpus,\n");
896 lf_printf (file, " int siggnal)\n");
897 lf_printf (file, "{\n");
898 lf_indent (file, +2);
899 if (options.gen.multi_sim)
901 lf_printf (file, "int mach;\n");
902 lf_printf (file, "if (STATE_ARCHITECTURE (sd) == NULL)\n");
903 lf_printf (file, " mach = 0;\n");
904 lf_printf (file, "else\n");
905 lf_printf (file, " mach = STATE_ARCHITECTURE (sd)->mach;\n");
906 lf_printf (file, "switch (mach)\n");
907 lf_printf (file, " {\n");
908 lf_indent (file, +2);
909 for (entry = gen->tables; entry != NULL; entry = entry->next)
911 if (options.gen.default_model != NULL
912 && (strcmp (entry->model->name, options.gen.default_model) == 0
913 || strcmp (entry->model->full_name,
914 options.gen.default_model) == 0))
915 lf_printf (file, "default:\n");
916 lf_printf (file, "case bfd_mach_%s:\n", entry->model->full_name);
917 lf_indent (file, +2);
918 print_function_name (file, "issue", NULL, /* format name */
919 NULL, /* NO processor */
920 NULL, /* expanded bits */
921 function_name_prefix_idecode);
922 lf_printf (file, " = ");
923 print_function_name (file, "issue", NULL, /* format name */
924 entry->model->name, NULL, /* expanded bits */
925 function_name_prefix_idecode);
926 lf_printf (file, ";\n");
927 print_function_name (file, "run", NULL, /* format name */
928 NULL, /* NO processor */
929 NULL, /* expanded bits */
930 function_name_prefix_engine);
931 lf_printf (file, " = ");
932 print_function_name (file, "run", NULL, /* format name */
933 entry->model->name, NULL, /* expanded bits */
934 function_name_prefix_engine);
935 lf_printf (file, ";\n");
936 lf_printf (file, "break;\n");
937 lf_indent (file, -2);
939 if (options.gen.default_model == NULL)
941 lf_printf (file, "default:\n");
942 lf_indent (file, +2);
943 lf_printf (file, "sim_engine_abort (sd, NULL, NULL_CIA,\n");
945 " \"sim_engine_run - unknown machine\");\n");
946 lf_printf (file, "break;\n");
947 lf_indent (file, -2);
949 lf_indent (file, -2);
950 lf_printf (file, " }\n");
952 print_function_name (file, "run", NULL, /* format name */
953 NULL, /* NO processor */
954 NULL, /* expanded bits */
955 function_name_prefix_engine);
956 lf_printf (file, " (sd, next_cpu_nr, nr_cpus, siggnal);\n");
957 lf_indent (file, -2);
958 lf_printf (file, "}\n");
961 /****************************************************************/
964 do_gen (insn_table *isa, decode_table *decode_rules)
967 if (decode_rules == NULL)
968 error (NULL, "Must specify a decode table\n");
970 error (NULL, "Must specify an instruction table\n");
971 if (decode_table_max_word_nr (decode_rules) > 0)
972 options.gen.multi_word = decode_table_max_word_nr (decode_rules);
973 gen = make_gen_tables (isa, decode_rules);
974 gen_tables_expand_insns (gen);
975 gen_tables_expand_semantics (gen);
979 /****************************************************************/
981 igen_options options;
984 main (int argc, char **argv, char **envp)
986 cache_entry *cache_rules = NULL;
987 lf_file_references file_references = lf_include_references;
988 decode_table *decode_rules = NULL;
989 insn_table *isa = NULL;
990 gen_table *gen = NULL;
991 char *real_file_name = NULL;
995 lf_open ("-", "stdout", lf_omit_references, lf_is_text, "igen");
1001 printf ("Usage:\n");
1003 printf (" igen <config-opts> ... <input-opts>... <output-opts>...\n");
1005 printf ("Config options:\n");
1007 printf (" -B <bit-size>\n");
1008 printf ("\t Set the number of bits in an instruction (deprecated).\n");
1010 ("\t This option can now be set directly in the instruction table.\n");
1012 printf (" -D <data-structure>\n");
1014 ("\t Dump the specified data structure to stdout. Valid structures include:\n");
1016 ("\t processor-names - list the names of all the processors (models)\n");
1018 printf (" -F <filter-list>\n");
1020 ("\t Filter out any instructions with a non-empty flags field that contains\n");
1021 printf ("\t a flag not listed in the <filter-list>.\n");
1023 printf (" -H <high-bit>\n");
1025 ("\t Set the number of the high (most significant) instruction bit (deprecated).\n");
1027 ("\t This option can now be set directly in the instruction table.\n");
1029 printf (" -I <directory>\n");
1031 ("\t Add <directory> to the list of directories searched when opening a file\n");
1033 printf (" -M <model-list>\n");
1035 ("\t Filter out any instructions that do not support at least one of the listed\n");
1037 ("\t models (An instructions with no model information is considered to support\n");
1038 printf ("\t all models.).\n");
1040 printf (" -N <nr-cpus>\n");
1041 printf ("\t Generate a simulator supporting <nr-cpus>\n");
1043 ("\t Specify `-N 0' to disable generation of the SMP. Specifying `-N 1' will\n");
1045 ("\t still generate an SMP enabled simulator but will only support one CPU.\n");
1047 printf (" -T <mechanism>\n");
1049 ("\t Override the decode mechanism specified by the decode rules\n");
1051 printf (" -P <prefix>\n");
1053 ("\t Prepend global names (except itable) with the string <prefix>.\n");
1055 ("\t Specify -P <module>=<prefix> to set a specific <module>'s prefix.\n");
1057 printf (" -S <suffix>\n");
1059 ("\t Replace a global name (suffix) (except itable) with the string <suffix>.\n");
1061 ("\t Specify -S <module>=<suffix> to change a specific <module>'s name (suffix).\n");
1063 printf (" -Werror\n");
1064 printf ("\t Make warnings errors\n");
1065 printf (" -Wnodiscard\n");
1067 ("\t Suppress warnings about discarded functions and instructions\n");
1068 printf (" -Wnowidth\n");
1070 ("\t Suppress warnings about instructions with invalid widths\n");
1071 printf (" -Wnounimplemented\n");
1072 printf ("\t Suppress warnings about unimplemented instructions\n");
1074 printf (" -G [!]<gen-option>\n");
1075 printf ("\t Any of the following options:\n");
1078 ("\t decode-duplicate - Override the decode rules, forcing the duplication of\n");
1079 printf ("\t semantic functions\n");
1081 ("\t decode-combine - Combine any duplicated entries within a table\n");
1083 ("\t decode-zero-reserved - Override the decode rules, forcing reserved bits to be\n");
1084 printf ("\t treated as zero.\n");
1086 ("\t decode-switch-is-goto - Overfide the padded-switch code type as a goto-switch\n");
1089 ("\t gen-conditional-issue - conditionally issue each instruction\n");
1091 ("\t gen-delayed-branch - need both cia and nia passed around\n");
1093 ("\t gen-direct-access - use #defines to directly access values\n");
1095 ("\t gen-zero-r<N> - arch assumes GPR(<N>) == 0, keep it that way\n");
1097 ("\t gen-icache[=<N> - generate an instruction cracking cache of size <N>\n");
1098 printf ("\t Default size is %d\n",
1099 options.gen.icache_size);
1101 ("\t gen-insn-in-icache - save original instruction when cracking\n");
1103 ("\t gen-multi-sim[=MODEL] - generate multiple simulators - one per model\n");
1105 ("\t If specified MODEL is made the default architecture.\n");
1107 ("\t By default, a single simulator that will\n");
1109 ("\t execute any instruction is generated\n");
1111 ("\t gen-multi-word - generate code allowing for multi-word insns\n");
1113 ("\t gen-semantic-icache - include semantic code in cracking functions\n");
1115 ("\t gen-slot-verification - perform slot verification as part of decode\n");
1116 printf ("\t gen-nia-invalid - NIA defaults to nia_invalid\n");
1117 printf ("\t gen-nia-void - do not compute/return NIA\n");
1120 ("\t trace-combine - report combined entries a rule application\n");
1122 ("\t trace-entries - report entries after a rules application\n");
1123 printf ("\t trace-rule-rejection - report each rule as rejected\n");
1124 printf ("\t trace-rule-selection - report each rule as selected\n");
1126 ("\t trace-insn-insertion - report each instruction as it is inserted into a decode table\n");
1128 ("\t trace-rule-expansion - report each instruction as it is expanded (before insertion into a decode table)\n");
1129 printf ("\t trace-all - enable all trace options\n");
1132 ("\t field-widths - instruction formats specify widths (deprecated)\n");
1134 ("\t By default, an instruction format specifies bit\n");
1135 printf ("\t positions\n");
1137 ("\t This option can now be set directly in the\n");
1138 printf ("\t instruction table\n");
1140 ("\t jumps - use jumps instead of function calls\n");
1142 ("\t omit-line-numbers - do not include line number information in the output\n");
1144 printf ("Input options:\n");
1146 printf (" -k <cache-rules> (deprecated)\n");
1147 printf (" -o <decode-rules>\n");
1148 printf (" -i <instruction-table>\n");
1150 printf ("Output options:\n");
1152 printf (" -x Perform expansion (required)\n");
1154 (" -n <real-name> Specify the real name of the next output file\n");
1156 (" -h Generate the header (.h) file rather than the body (.c)\n");
1157 printf (" -c <output-file> output icache\n");
1158 printf (" -d <output-file> output idecode\n");
1159 printf (" -e <output-file> output engine\n");
1160 printf (" -f <output-file> output support functions\n");
1161 printf (" -m <output-file> output model\n");
1162 printf (" -r <output-file> output multi-sim run\n");
1163 printf (" -s <output-file> output schematic\n");
1164 printf (" -t <output-file> output itable\n");
1167 while ((ch = getopt (argc, argv,
1168 "B:D:F:G:H:I:M:N:P:T:W:o:k:i:n:hc:d:e:m:r:s:t:f:x"))
1171 fprintf (stderr, " -%c ", ch);
1173 fprintf (stderr, "%s ", optarg);
1174 fprintf (stderr, "\\\n");
1180 filter_parse (&options.model_filter, optarg);
1184 if (strcmp (optarg, "processor-names"))
1187 for (processor = filter_next (options.model_filter, "");
1189 processor = filter_next (options.model_filter, processor))
1190 lf_printf (standard_out, "%s\n", processor);
1193 error (NULL, "Unknown data structure %s, not dumped\n", optarg);
1197 filter_parse (&options.flags_filter, optarg);
1202 table_include **dir = &options.include;
1203 while ((*dir) != NULL)
1204 dir = &(*dir)->next;
1205 (*dir) = ZALLOC (table_include);
1206 (*dir)->dir = strdup (optarg);
1211 options.insn_bit_size = a2i (optarg);
1212 if (options.insn_bit_size <= 0
1213 || options.insn_bit_size > max_insn_bit_size)
1215 error (NULL, "Instruction bitsize must be in range 1..%d\n",
1218 if (options.hi_bit_nr != options.insn_bit_size - 1
1219 && options.hi_bit_nr != 0)
1221 error (NULL, "Conflict betweem hi-bit-nr and insn-bit-size\n");
1226 options.hi_bit_nr = a2i (optarg);
1227 if (options.hi_bit_nr != options.insn_bit_size - 1
1228 && options.hi_bit_nr != 0)
1230 error (NULL, "Conflict between hi-bit-nr and insn-bit-size\n");
1235 options.gen.smp = a2i (optarg);
1244 chp = strchr (optarg, '=');
1247 names = &options.module.global;
1252 chp = chp + 1; /* skip `=' */
1254 if (strncmp (optarg, "global=", chp - optarg) == 0)
1256 names = &options.module.global;
1258 if (strncmp (optarg, "engine=", chp - optarg) == 0)
1260 names = &options.module.engine;
1262 if (strncmp (optarg, "icache=", chp - optarg) == 0)
1264 names = &options.module.icache;
1266 if (strncmp (optarg, "idecode=", chp - optarg) == 0)
1268 names = &options.module.idecode;
1270 if (strncmp (optarg, "itable=", chp - optarg) == 0)
1272 names = &options.module.itable;
1274 if (strncmp (optarg, "semantics=", chp - optarg) == 0)
1276 names = &options.module.semantics;
1278 if (strncmp (optarg, "support=", chp - optarg) == 0)
1280 names = &options.module.support;
1284 error (NULL, "Prefix `%s' unreconized\n", optarg);
1290 name = &names->prefix;
1293 name = &names->suffix;
1296 abort (); /* Bad switch. */
1298 name->u = strdup (chp);
1299 name->l = strdup (chp);
1304 *chp = toupper (*chp);
1307 if (name == &options.module.global.prefix)
1309 options.module.engine.prefix = options.module.global.prefix;
1310 options.module.icache.prefix = options.module.global.prefix;
1311 options.module.idecode.prefix = options.module.global.prefix;
1312 /* options.module.itable.prefix = options.module.global.prefix; */
1313 options.module.semantics.prefix =
1314 options.module.global.prefix;
1315 options.module.support.prefix = options.module.global.prefix;
1317 if (name == &options.module.global.suffix)
1319 options.module.engine.suffix = options.module.global.suffix;
1320 options.module.icache.suffix = options.module.global.suffix;
1321 options.module.idecode.suffix = options.module.global.suffix;
1322 /* options.module.itable.suffix = options.module.global.suffix; */
1323 options.module.semantics.suffix =
1324 options.module.global.suffix;
1325 options.module.support.suffix = options.module.global.suffix;
1332 if (strcmp (optarg, "error") == 0)
1333 options.warning = error;
1334 else if (strcmp (optarg, "nodiscard") == 0)
1335 options.warn.discard = 0;
1336 else if (strcmp (optarg, "discard") == 0)
1337 options.warn.discard = 1;
1338 else if (strcmp (optarg, "nowidth") == 0)
1339 options.warn.width = 0;
1340 else if (strcmp (optarg, "width") == 0)
1341 options.warn.width = 1;
1342 else if (strcmp (optarg, "nounimplemented") == 0)
1343 options.warn.unimplemented = 0;
1344 else if (strcmp (optarg, "unimplemented") == 0)
1345 options.warn.unimplemented = 1;
1347 error (NULL, "Unknown -W argument `%s'\n", optarg);
1356 if (strncmp (optarg, "no-", strlen ("no-")) == 0)
1358 argp = optarg + strlen ("no-");
1361 else if (strncmp (optarg, "!", strlen ("!")) == 0)
1363 argp = optarg + strlen ("no-");
1371 if (strcmp (argp, "decode-duplicate") == 0)
1373 options.decode.duplicate = enable_p;
1375 else if (strcmp (argp, "decode-combine") == 0)
1377 options.decode.combine = enable_p;
1379 else if (strcmp (argp, "decode-zero-reserved") == 0)
1381 options.decode.zero_reserved = enable_p;
1384 else if (strcmp (argp, "gen-conditional-issue") == 0)
1386 options.gen.conditional_issue = enable_p;
1388 else if (strcmp (argp, "conditional-issue") == 0)
1390 options.gen.conditional_issue = enable_p;
1391 options.warning (NULL,
1392 "Option conditional-issue replaced by gen-conditional-issue\n");
1394 else if (strcmp (argp, "gen-delayed-branch") == 0)
1396 options.gen.delayed_branch = enable_p;
1398 else if (strcmp (argp, "delayed-branch") == 0)
1400 options.gen.delayed_branch = enable_p;
1401 options.warning (NULL,
1402 "Option delayed-branch replaced by gen-delayed-branch\n");
1404 else if (strcmp (argp, "gen-direct-access") == 0)
1406 options.gen.direct_access = enable_p;
1408 else if (strcmp (argp, "direct-access") == 0)
1410 options.gen.direct_access = enable_p;
1411 options.warning (NULL,
1412 "Option direct-access replaced by gen-direct-access\n");
1414 else if (strncmp (argp, "gen-zero-r", strlen ("gen-zero-r")) == 0)
1416 options.gen.zero_reg = enable_p;
1417 options.gen.zero_reg_nr = atoi (argp + strlen ("gen-zero-r"));
1419 else if (strncmp (argp, "zero-r", strlen ("zero-r")) == 0)
1421 options.gen.zero_reg = enable_p;
1422 options.gen.zero_reg_nr = atoi (argp + strlen ("zero-r"));
1423 options.warning (NULL,
1424 "Option zero-r<N> replaced by gen-zero-r<N>\n");
1426 else if (strncmp (argp, "gen-icache", strlen ("gen-icache")) == 0)
1428 switch (argp[strlen ("gen-icache")])
1431 options.gen.icache_size =
1432 atoi (argp + strlen ("gen-icache") + 1);
1433 options.gen.icache = enable_p;
1436 options.gen.icache = enable_p;
1440 "Expecting -Ggen-icache or -Ggen-icache=<N>\n");
1443 else if (strcmp (argp, "gen-insn-in-icache") == 0)
1445 options.gen.insn_in_icache = enable_p;
1447 else if (strncmp (argp, "gen-multi-sim", strlen ("gen-multi-sim"))
1450 char *arg = &argp[strlen ("gen-multi-sim")];
1454 options.gen.multi_sim = enable_p;
1455 options.gen.default_model = arg + 1;
1456 if (!filter_is_member
1457 (options.model_filter, options.gen.default_model))
1458 error (NULL, "multi-sim model %s unknown\n",
1459 options.gen.default_model);
1462 options.gen.multi_sim = enable_p;
1463 options.gen.default_model = NULL;
1467 "Expecting -Ggen-multi-sim or -Ggen-multi-sim=<MODEL>\n");
1471 else if (strcmp (argp, "gen-multi-word") == 0)
1473 options.gen.multi_word = enable_p;
1475 else if (strcmp (argp, "gen-semantic-icache") == 0)
1477 options.gen.semantic_icache = enable_p;
1479 else if (strcmp (argp, "gen-slot-verification") == 0)
1481 options.gen.slot_verification = enable_p;
1483 else if (strcmp (argp, "verify-slot") == 0)
1485 options.gen.slot_verification = enable_p;
1486 options.warning (NULL,
1487 "Option verify-slot replaced by gen-slot-verification\n");
1489 else if (strcmp (argp, "gen-nia-invalid") == 0)
1491 options.gen.nia = nia_is_invalid;
1493 else if (strcmp (argp, "default-nia-minus-one") == 0)
1495 options.gen.nia = nia_is_invalid;
1496 options.warning (NULL,
1497 "Option default-nia-minus-one replaced by gen-nia-invalid\n");
1499 else if (strcmp (argp, "gen-nia-void") == 0)
1501 options.gen.nia = nia_is_void;
1503 else if (strcmp (argp, "trace-all") == 0)
1505 memset (&options.trace, enable_p, sizeof (options.trace));
1507 else if (strcmp (argp, "trace-combine") == 0)
1509 options.trace.combine = enable_p;
1511 else if (strcmp (argp, "trace-entries") == 0)
1513 options.trace.entries = enable_p;
1515 else if (strcmp (argp, "trace-rule-rejection") == 0)
1517 options.trace.rule_rejection = enable_p;
1519 else if (strcmp (argp, "trace-rule-selection") == 0)
1521 options.trace.rule_selection = enable_p;
1523 else if (strcmp (argp, "trace-insn-insertion") == 0)
1525 options.trace.insn_insertion = enable_p;
1527 else if (strcmp (argp, "trace-insn-expansion") == 0)
1529 options.trace.insn_expansion = enable_p;
1531 else if (strcmp (argp, "jumps") == 0)
1533 options.gen.code = generate_jumps;
1535 else if (strcmp (argp, "field-widths") == 0)
1537 options.insn_specifying_widths = enable_p;
1539 else if (strcmp (argp, "omit-line-numbers") == 0)
1541 file_references = lf_omit_references;
1545 error (NULL, "Unknown option %s\n", optarg);
1551 isa = load_insn_table (optarg, cache_rules);
1552 if (isa->illegal_insn == NULL)
1553 error (NULL, "illegal-instruction missing from insn table\n");
1557 gen = do_gen (isa, decode_rules);
1561 decode_rules = load_decode_table (optarg);
1566 error (NULL, "Cache file must appear before the insn file\n");
1567 cache_rules = load_cache_table (optarg);
1571 real_file_name = strdup (optarg);
1587 lf *file = lf_open (optarg, real_file_name, file_references,
1588 (is_header ? lf_is_h : lf_is_c),
1590 if (gen == NULL && ch != 't' && ch != 'm' && ch != 'f')
1592 options.warning (NULL,
1593 "Explicitly generate tables with -x option\n");
1594 gen = do_gen (isa, decode_rules);
1596 lf_print__file_start (file);
1601 gen_model_h (file, isa);
1603 gen_model_c (file, isa);
1607 gen_itable_h (file, isa);
1609 gen_itable_c (file, isa);
1613 gen_support_h (file, isa);
1615 gen_support_c (file, isa);
1619 options.warning (NULL, "-hr option ignored\n");
1621 gen_run_c (file, gen);
1625 gen_semantics_h (file, gen->semantics, isa->max_nr_words);
1627 gen_semantics_c (file, gen->semantics, isa->caches);
1631 gen_idecode_h (file, gen, isa, cache_rules);
1633 gen_idecode_c (file, gen, isa, cache_rules);
1637 gen_engine_h (file, gen, isa, cache_rules);
1639 gen_engine_c (file, gen, isa, cache_rules);
1645 isa->functions, isa->max_nr_words);
1648 gen->semantics, isa->functions, cache_rules);
1651 lf_print__file_finish (file);
1655 real_file_name = NULL;
1658 ERROR ("Bad switch");