1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include "ld-decode.h"
38 #include "gen-model.h"
39 #include "gen-icache.h"
40 #include "gen-itable.h"
41 #include "gen-idecode.h"
42 #include "gen-semantics.h"
43 #include "gen-support.h"
46 int insn_bit_size
= default_insn_bit_size
;
47 int insn_specifying_widths
= 0;
48 const char *global_name_prefix
= "";
49 const char *global_uname_prefix
= "";
50 int semantic_zero_reg_nr
= 0;
52 int code
= generate_calls
;
54 int generate_expanded_instructions
;
55 int icache_size
= 1024;
58 /****************************************************************/
61 /* Semantic functions */
64 print_semantic_function_formal(lf
*file
)
67 if ((code
& generate_with_icache
))
68 nr
= lf_printf(file
, "SIM_DESC sd,\n %sidecode_cache *cache_entry,\n %sinstruction_address cia",
69 global_name_prefix
, global_name_prefix
);
70 else if (generate_smp
)
71 nr
= lf_printf(file
, "sim_cpu *cpu,\n %sinstruction_word instruction,\n %sinstruction_address cia",
72 global_name_prefix
, global_name_prefix
);
74 nr
= lf_printf(file
, "SIM_DESC sd,\n %sinstruction_word instruction,\n %sinstruction_address cia",
75 global_name_prefix
, global_name_prefix
);
80 print_semantic_function_actual(lf
*file
)
83 if ((code
& generate_with_icache
))
84 nr
= lf_printf(file
, "sd, cache_entry, cia");
85 else if (generate_smp
)
86 nr
= lf_printf(file
, "cpu, instruction, cia");
88 nr
= lf_printf(file
, "sd, instruction, cia");
93 print_semantic_function_type(lf
*file
)
96 nr
= lf_printf(file
, "%sinstruction_address", global_name_prefix
);
101 /* Idecode functions */
104 print_icache_function_formal(lf
*file
)
108 nr
+= lf_printf(file
, "sim_cpu *cpu,\n");
110 nr
+= lf_printf(file
, "SIM_DESC sd,\n");
111 nr
+= lf_printf(file
, " %sinstruction_word instruction,\n", global_name_prefix
);
112 nr
+= lf_printf(file
, " %sinstruction_address cia,\n", global_name_prefix
);
113 nr
+= lf_printf(file
, " %sidecode_cache *cache_entry", global_name_prefix
);
118 print_icache_function_actual(lf
*file
)
122 nr
= lf_printf(file
, "cpu, instruction, cia, cache_entry");
124 nr
= lf_printf(file
, "sd, instruction, cia, cache_entry");
129 print_icache_function_type(lf
*file
)
132 if ((code
& generate_with_semantic_icache
))
133 nr
= print_semantic_function_type(file
);
135 nr
= lf_printf(file
, "%sidecode_semantic *", global_name_prefix
);
143 print_insn_bits(lf
*file
, insn_bits
*bits
)
148 nr
+= print_insn_bits(file
, bits
->last
);
149 nr
+= lf_putchr(file
, '_');
150 nr
+= lf_putstr(file
, bits
->field
->val_string
);
151 if (bits
->opcode
->is_boolean
&& bits
->value
== 0)
152 nr
+= lf_putint(file
, bits
->opcode
->boolean_constant
);
153 else if (!bits
->opcode
->is_boolean
) {
154 if (bits
->opcode
->last
< bits
->field
->last
)
155 nr
+= lf_putint(file
, bits
->value
<< (bits
->field
->last
- bits
->opcode
->last
));
157 nr
+= lf_putint(file
, bits
->value
);
163 print_function_name(lf
*file
,
164 const char *basename
,
165 insn_bits
*expanded_bits
,
166 lf_function_name_prefixes prefix
)
171 case function_name_prefix_semantics
:
172 nr
+= lf_putstr(file
, global_name_prefix
);
173 nr
+= lf_putstr(file
, "semantic_");
175 case function_name_prefix_idecode
:
176 nr
+= lf_putstr(file
, global_name_prefix
);
177 nr
+= lf_printf(file
, "idecode_");
179 case function_name_prefix_itable
:
180 nr
+= lf_putstr(file
, "itable_");
182 case function_name_prefix_icache
:
183 nr
+= lf_putstr(file
, global_name_prefix
);
184 nr
+= lf_putstr(file
, "icache_");
190 /* the function name */
202 nr
+= lf_putchr(file
, '_');
205 nr
+= lf_putchr(file
, *pos
);
212 if (generate_expanded_instructions
)
213 nr
+= print_insn_bits(file
, expanded_bits
);
220 print_my_defines (lf
*file
,
221 insn_bits
*expanded_bits
,
222 table_entry
*file_entry
)
224 /* #define MY_INDEX xxxxx */
225 lf_indent_suppress (file
);
226 lf_printf (file
, "#undef MY_INDEX\n");
227 lf_indent_suppress (file
);
228 lf_printf (file
, "#define MY_INDEX ");
229 print_function_name (file
,
230 file_entry
->fields
[insn_name
],
232 function_name_prefix_itable
);
233 lf_printf (file
, "\n");
234 /* #define MY_PREFIX xxxxxx */
235 lf_indent_suppress (file
);
236 lf_printf (file
, "#undef MY_PREFIX\n");
237 lf_indent_suppress (file
);
238 lf_printf (file
, "#define MY_PREFIX ");
239 print_function_name (file
,
240 file_entry
->fields
[insn_name
],
242 function_name_prefix_none
);
243 lf_printf (file
, "\n");
248 print_itrace_prefix (lf
*file
,
249 table_entry
*file_entry
,
250 const char *phase_lc
)
252 const char *prefix
= "trace_one_insn (";
253 int indent
= strlen (prefix
);
254 lf_printf (file
, "%sSD, CPU, %s, TRACE_LINENUM_P (CPU),\n",
255 prefix
, (code
& generate_with_semantic_delayed_branch
) ? "cia.ip" : "cia");
256 lf_indent (file
, +indent
);
257 lf_printf (file
, "itable[MY_INDEX].file,\n");
258 lf_printf (file
, "itable[MY_INDEX].line_nr,\n");
259 lf_printf (file
, "\"%s\",\n", phase_lc
);
265 print_itrace_format (lf
*file
,
266 table_assembler_entry
*assembler
)
268 /* pass=1 is fmt string; pass=2 is is arguments */
271 /* print the format string */
272 for (pass
= 1; pass
<= 2; pass
++)
274 const char *chp
= assembler
->format
;
275 chp
++; /* skip the leading quote */
276 /* prefix the format with the insn `name' */
279 lf_printf (file
, "\"%%s - %%s - ");
283 lf_printf (file
, ",\n");
284 lf_printf (file
, "itable[MY_INDEX].name");
285 lf_printf (file
, ",\n");
286 lf_printf (file
, "XSTRING(MY_PREFIX)");
288 /* write out the format/args */
291 if (chp
[0] == '\\' && (chp
[1] == '<' || chp
[1] == '>'))
294 lf_putchr (file
, chp
[1]);
297 else if (chp
[0] == '<' || chp
[0] == '%')
299 /* parse [ "%" ... ] "<" [ func "#" ] param ">" */
305 /* the "%" ... "<" format */
307 while (chp
[0] != '<' && chp
[0] != '\0')
310 error ("%s:%d: Missing `<' after `%%'",
311 assembler
->file_name
,
314 /* [ "func" # ] OR "param" */
317 while (chp
[0] != '>' && chp
[0] != '#' && chp
[0] != '\0')
319 strlen_func
= chp
- func
;
324 while (chp
[0] != '>' && chp
[0] != '\0')
327 strlen_param
= chp
- param
;
329 error ("%s:%d: Missing closing `>' in assembler string",
330 assembler
->file_name
,
335 lf_printf (file
, ",\n");
336 if (strncmp (fmt
, "<", 1) == 0)
337 /* implicit long int format */
340 lf_printf (file
, "%%ld");
343 lf_printf (file
, "(long) ");
344 lf_write (file
, param
, strlen_param
);
347 else if (strncmp (fmt
, "%<", 2) == 0)
348 /* explicit format */
351 lf_printf (file
, "%%");
353 lf_write (file
, param
, strlen_param
);
355 else if (strncmp (fmt
, "%s<", 3) == 0)
359 lf_printf (file
, "%%s");
362 lf_printf (file
, "%sstr_", global_name_prefix
);
363 lf_write (file
, func
, strlen_func
);
364 lf_printf (file
, " (_SD, ");
365 lf_write (file
, param
, strlen_param
);
366 lf_printf (file
, ")");
369 else if (strncmp (fmt
, "%lx<", 4) == 0)
373 lf_printf (file
, "%%lx");
376 lf_printf (file
, "(unsigned long) ");
377 lf_write (file
, param
, strlen_param
);
380 else if (strncmp (fmt
, "%08lx<", 6) == 0)
384 lf_printf (file
, "%%08lx");
387 lf_printf (file
, "(unsigned long) ");
388 lf_write (file
, param
, strlen_param
);
392 error ("%s:%d: Unknown assembler string format",
393 assembler
->file_name
,
399 lf_putchr (file
, chp
[0]);
404 lf_printf (file
, ");\n");
409 print_itrace (lf
*file
,
410 table_entry
*file_entry
,
413 const char *phase
= (idecode
) ? "DECODE" : "INSN";
414 const char *phase_lc
= (idecode
) ? "decode" : "insn";
415 lf_printf (file
, "\n");
416 lf_indent_suppress (file
);
417 lf_printf (file
, "#if defined (WITH_TRACE)\n");
418 lf_printf (file
, "/* trace the instructions execution if enabled */\n");
419 lf_printf (file
, "if (TRACE_%s_P (CPU)) {\n", phase
);
420 lf_indent (file
, +2);
421 if (file_entry
->assembler
!= NULL
)
423 table_assembler_entry
*assembler
= file_entry
->assembler
;
427 if (assembler
->condition
!= NULL
)
430 lf_printf (file
, "%sif (%s)\n",
431 is_first
? "" : "else ",
432 assembler
->condition
);
433 lf_indent (file
, +2);
434 indent
= print_itrace_prefix (file
, file_entry
, phase_lc
);
435 print_itrace_format (file
, assembler
);
436 lf_indent (file
, -indent
);
437 lf_indent (file
, -2);
438 if (assembler
->next
== NULL
)
439 error ("%s:%d: Missing final unconditional assembler",
440 assembler
->file_name
,
448 lf_printf (file
, "else\n");
449 lf_indent (file
, +2);
451 indent
= print_itrace_prefix (file
, file_entry
, phase_lc
);
452 print_itrace_format (file
, assembler
);
453 lf_indent (file
, -indent
);
455 lf_indent (file
, -2);
456 if (assembler
->next
!= NULL
)
457 error ("%s:%d: Unconditional assembler is not last",
458 assembler
->file_name
,
462 assembler
= assembler
->next
;
464 while (assembler
!= NULL
);
468 int indent
= print_itrace_prefix (file
, file_entry
, phase_lc
);
469 lf_printf (file
, "\"%%s - %%s - ?\",\n");
470 lf_printf (file
, "itable[MY_INDEX].name,\n");
471 lf_printf (file
, "XSTRING(MY_PREFIX));\n");
472 lf_indent (file
, -indent
);
474 lf_indent (file
, -2);
475 lf_printf (file
, "}\n");
476 lf_indent_suppress (file
);
477 lf_printf (file
, "#endif\n");
481 /****************************************************************/
485 gen_semantics_h(insn_table
*table
,
489 lf_printf(file
, "typedef ");
490 print_semantic_function_type(file
);
491 lf_printf(file
, " %sidecode_semantic\n(", global_name_prefix
);
492 print_semantic_function_formal(file
);
493 lf_printf(file
, ");\n");
494 lf_printf(file
, "\n");
495 if ((code
& generate_calls
)) {
496 if (generate_expanded_instructions
)
497 insn_table_traverse_tree(table
,
501 print_semantic_declaration
, /* leaf */
505 insn_table_traverse_insn(table
,
507 print_semantic_declaration
);
511 lf_print__this_file_is_empty(file
);
517 gen_semantics_c(insn_table
*table
,
518 cache_table
*cache_rules
,
522 if ((code
& generate_calls
)) {
523 lf_printf(file
, "\n");
524 lf_printf(file
, "#include \"sim-main.h\"\n");
525 lf_printf(file
, "#include \"%sidecode.h\"\n", global_name_prefix
);
526 lf_printf(file
, "#include \"%ssemantics.h\"\n", global_name_prefix
);
527 lf_printf(file
, "#include \"%ssupport.h\"\n", global_name_prefix
);
528 lf_printf(file
, "\n");
529 if (generate_expanded_instructions
)
530 insn_table_traverse_tree(table
,
534 print_semantic_definition
, /* leaf */
538 insn_table_traverse_insn(table
,
540 print_semantic_definition
);
544 lf_print__this_file_is_empty(file
);
549 /****************************************************************/
553 gen_icache_h(insn_table
*table
,
557 lf_printf(file
, "typedef ");
558 print_icache_function_type(file
);
559 lf_printf(file
, " %sidecode_icache\n(", global_name_prefix
);
560 print_icache_function_formal(file
);
561 lf_printf(file
, ");\n");
562 lf_printf(file
, "\n");
563 if ((code
& generate_calls
)
564 && (code
& generate_with_icache
)) {
565 insn_table_traverse_function(table
,
567 print_icache_internal_function_declaration
);
568 if (generate_expanded_instructions
)
569 insn_table_traverse_tree(table
,
573 print_icache_declaration
, /* leaf */
577 insn_table_traverse_insn(table
,
579 print_icache_declaration
);
583 lf_print__this_file_is_empty(file
);
588 gen_icache_c(insn_table
*table
,
589 cache_table
*cache_rules
,
593 /* output `internal' invalid/floating-point unavailable functions
595 if ((code
& generate_calls
)
596 && (code
& generate_with_icache
)) {
597 lf_printf(file
, "\n");
598 lf_printf(file
, "#include \"cpu.h\"\n");
599 lf_printf(file
, "#include \"idecode.h\"\n");
600 lf_printf(file
, "#include \"semantics.h\"\n");
601 lf_printf(file
, "#include \"icache.h\"\n");
602 lf_printf(file
, "#include \"support.h\"\n");
603 lf_printf(file
, "\n");
604 insn_table_traverse_function(table
,
606 print_icache_internal_function_definition
);
607 lf_printf(file
, "\n");
608 if (generate_expanded_instructions
)
609 insn_table_traverse_tree(table
,
613 print_icache_definition
, /* leaf */
617 insn_table_traverse_insn(table
,
619 print_icache_definition
);
623 lf_print__this_file_is_empty(file
);
628 /****************************************************************/
636 cache_table
*cache_rules
= NULL
;
637 lf_file_references file_references
= lf_include_references
;
638 decode_table
*decode_rules
= NULL
;
639 filter
*filters
= NULL
;
640 insn_table
*instructions
= NULL
;
641 char *real_file_name
= NULL
;
647 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
648 printf("Config options:\n");
649 printf(" -F <filter-flag> eg -F 32 to include 32bit instructions\n");
650 printf(" -I <icache-size> Specify size of cracking cache\n");
651 printf(" -B <bit-size> Set the number of bits in an instruction\n");
652 printf(" -H <high-bit> Set the number of the high (msb) instruction bit\n");
653 printf(" -N <nr-cpus> Specify the max number of cpus the simulation will support\n");
654 printf(" -T <mechanism> Override the decode mechanism specified by the decode rules\n");
655 printf(" -P <prefix> Prepend all functions with the specified prefix\n");
656 printf(" -G <gen-option> Any of the following options:\n");
657 printf(" field-widths - instruction table specifies widths (not ofsesets)\n");
658 printf(" jumps - use jumps instead of function calls\n");
659 printf(" duplicate - expand (duplicate) semantic functions\n");
660 printf(" omit-line-numbers - do not include line nr info in output\n");
661 printf(" direct-access - use #defines to directly access values\n");
662 printf(" icache - generate an instruction cracking cache\n");
663 printf(" semantic-icache - include semantic code in cracking functions\n");
664 printf(" insn-in-icache - save original instruction when cracking\n");
665 printf(" default-nia-minus-one - instead of cia + insn-size\n");
666 printf(" delayed-branch - instead of cia + insn-size\n");
667 printf(" zero-r<N> - arch assumes GPR(<N>) == 0, keep it that way\n");
668 printf(" conditional-issue - conditionally issue each instruction\n");
669 printf(" validate-slot - perform slot verification as part of decode\n");
671 printf("Input options:\n");
672 printf(" -o <decode-rules>\n");
673 printf(" -k <cache-rules>\n");
674 printf(" -i <instruction-table>\n");
676 printf("Output options:\n");
677 printf(" -n <real-name> Specify the real name of the next output file\n");
678 printf(" -h Generate header file\n");
679 printf(" -c <output-file> output icache\n");
680 printf(" -d <output-file> output idecode\n");
681 printf(" -e <output-file> output engine\n");
682 printf(" -f <output-file> output support functions\n");
683 printf(" -m <output-file> output model\n");
684 printf(" -s <output-file> output schematic\n");
685 printf(" -t <output-file> output itable\n");
688 while ((ch
= getopt(argc
, argv
,
689 "F:I:B:H:N:T:P:G:o:k:i:n:hc:d:e:m:s:t:f:"))
691 fprintf(stderr
, "\t-%c %s\n", ch
, (optarg
? optarg
: ""));
696 filters
= new_filter(optarg
, filters
);
700 icache_size
= a2i(optarg
);
701 code
|= generate_with_icache
;
705 insn_bit_size
= a2i(optarg
);
706 ASSERT(insn_bit_size
> 0 && insn_bit_size
<= max_insn_bit_size
707 && (hi_bit_nr
== insn_bit_size
-1 || hi_bit_nr
== 0));
711 hi_bit_nr
= a2i(optarg
);
712 ASSERT(hi_bit_nr
== insn_bit_size
-1 || hi_bit_nr
== 0);
716 generate_smp
= a2i(optarg
);
720 force_decode_gen_type(optarg
);
726 global_name_prefix
= strdup(optarg
);
727 chp
= strdup(optarg
);
728 global_uname_prefix
= chp
;
731 *chp
= toupper(*chp
);
738 if (strcmp(optarg
, "jumps") == 0) {
739 code
&= ~generate_calls
;
740 code
|= generate_jumps
;
742 else if (strcmp(optarg
, "field-widths") == 0) {
743 insn_specifying_widths
= 1;
745 else if (strcmp(optarg
, "duplicate") == 0) {
746 generate_expanded_instructions
= 1;
748 else if (strcmp(optarg
, "omit-line-numbers") == 0) {
749 file_references
= lf_omit_references
;
751 else if (strcmp(optarg
, "direct-access") == 0) {
752 code
|= generate_with_direct_access
;
754 else if (strcmp(optarg
, "icache") == 0) {
756 else if (strcmp(optarg
, "semantic-icache") == 0) {
757 code
|= generate_with_icache
;
758 code
|= generate_with_semantic_icache
;
760 else if (strcmp(optarg
, "insn-in-icache") == 0) {
761 code
|= generate_with_icache
;
762 code
|= generate_with_insn_in_icache
;
764 else if (strcmp(optarg
, "default-nia-minus-one") == 0) {
765 code
|= generate_with_semantic_returning_modified_nia_only
;
766 code
&= ~generate_with_semantic_delayed_branch
;
768 else if (strcmp(optarg
, "delayed-branch") == 0) {
769 code
|= generate_with_semantic_delayed_branch
;
770 code
&= ~generate_with_semantic_returning_modified_nia_only
;
772 else if (strcmp(optarg
, "conditional-issue") == 0) {
773 code
|= generate_with_semantic_conditional_issue
;
775 else if (strncmp (optarg
, "zero-r", strlen ("zero-r")) == 0) {
776 code
|= generate_with_semantic_zero_reg
;
777 semantic_zero_reg_nr
= atoi (optarg
+ strlen ("zero-r"));
779 else if (strcmp(optarg
, "verify-slot") == 0) {
780 code
|= generate_with_idecode_slot_verification
;
783 error("Unknown option %s", optarg
);
787 if (decode_rules
== NULL
|| cache_rules
== NULL
) {
788 fprintf(stderr
, "Must specify decode and cache tables\n");
791 instructions
= load_insn_table(optarg
, decode_rules
, filters
);
792 fprintf(stderr
, "\texpanding ...\n");
793 insn_table_expand_insns(instructions
);
796 decode_rules
= load_decode_table(optarg
, hi_bit_nr
);
799 cache_rules
= load_cache_table(optarg
, hi_bit_nr
);
802 real_file_name
= strdup(optarg
);
815 lf
*file
= lf_open(optarg
, real_file_name
, file_references
,
816 (is_header
? lf_is_h
: lf_is_c
),
818 lf_print__file_start(file
);
819 ASSERT(instructions
!= NULL
);
823 gen_semantics_h(instructions
, file
, code
);
825 gen_semantics_c(instructions
, cache_rules
, file
, code
);
829 gen_idecode_h(file
, instructions
, cache_rules
);
831 gen_idecode_c(file
, instructions
, cache_rules
);
835 gen_engine_h(file
, instructions
, cache_rules
);
837 gen_engine_c(file
, instructions
, cache_rules
);
841 gen_model_h(instructions
, file
);
843 gen_model_c(instructions
, file
);
847 gen_itable_h(instructions
, file
);
849 gen_itable_c(instructions
, file
);
853 gen_support_h(instructions
, file
);
855 gen_support_c(instructions
, file
);
859 gen_icache_h(instructions
, file
, code
);
861 gen_icache_c(instructions
, cache_rules
, file
, code
);
864 lf_print__file_finish(file
);
868 real_file_name
= NULL
;
871 error("unknown option\n");