]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/igen/igen.c
Add semantic tracing to the tic80
[thirdparty/binutils-gdb.git] / sim / igen / igen.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
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.
9
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.
14
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.
18
19 */
20
21
22
23 #include <getopt.h>
24
25 #include "misc.h"
26 #include "lf.h"
27 #include "table.h"
28 #include "config.h"
29
30 #include "filter.h"
31
32 #include "ld-decode.h"
33 #include "ld-cache.h"
34 #include "ld-insn.h"
35
36 #include "igen.h"
37
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"
44
45 int hi_bit_nr = 0;
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
51 int code = generate_calls;
52
53 int generate_expanded_instructions;
54 int icache_size = 1024;
55 int generate_smp;
56
57 /****************************************************************/
58
59
60 /* Semantic functions */
61
62 int
63 print_semantic_function_formal(lf *file)
64 {
65 int nr;
66 if ((code & generate_with_icache))
67 nr = lf_printf(file, "SIM_DESC sd,\n %sidecode_cache *cache_entry,\n %sinstruction_address cia",
68 global_name_prefix, global_name_prefix);
69 else if (generate_smp)
70 nr = lf_printf(file, "sim_cpu *cpu,\n %sinstruction_word instruction,\n %sinstruction_address cia",
71 global_name_prefix, global_name_prefix);
72 else
73 nr = lf_printf(file, "SIM_DESC sd,\n %sinstruction_word instruction,\n %sinstruction_address cia",
74 global_name_prefix, global_name_prefix);
75 return nr;
76 }
77
78 int
79 print_semantic_function_actual(lf *file)
80 {
81 int nr;
82 if ((code & generate_with_icache))
83 nr = lf_printf(file, "sd, cache_entry, cia");
84 else if (generate_smp)
85 nr = lf_printf(file, "cpu, instruction, cia");
86 else
87 nr = lf_printf(file, "sd, instruction, cia");
88 return nr;
89 }
90
91 int
92 print_semantic_function_type(lf *file)
93 {
94 int nr;
95 nr = lf_printf(file, "%sinstruction_address", global_name_prefix);
96 return nr;
97 }
98
99
100 /* Idecode functions */
101
102 int
103 print_icache_function_formal(lf *file)
104 {
105 int nr = 0;
106 if (generate_smp)
107 nr += lf_printf(file, "sim_cpu *cpu,\n");
108 else
109 nr += lf_printf(file, "SIM_DESC sd,\n");
110 nr += lf_printf(file, " %sinstruction_word instruction,\n", global_name_prefix);
111 nr += lf_printf(file, " %sinstruction_address cia,\n", global_name_prefix);
112 nr += lf_printf(file, " %sidecode_cache *cache_entry", global_name_prefix);
113 return nr;
114 }
115
116 int
117 print_icache_function_actual(lf *file)
118 {
119 int nr;
120 if (generate_smp)
121 nr = lf_printf(file, "cpu, instruction, cia, cache_entry");
122 else
123 nr = lf_printf(file, "sd, instruction, cia, cache_entry");
124 return nr;
125 }
126
127 int
128 print_icache_function_type(lf *file)
129 {
130 int nr;
131 if ((code & generate_with_semantic_icache))
132 nr = print_semantic_function_type(file);
133 else
134 nr = lf_printf(file, "%sidecode_semantic *", global_name_prefix);
135 return nr;
136 }
137
138
139 /* Function names */
140
141 static int
142 print_insn_bits(lf *file, insn_bits *bits)
143 {
144 int nr = 0;
145 if (bits == NULL)
146 return nr;
147 nr += print_insn_bits(file, bits->last);
148 nr += lf_putchr(file, '_');
149 nr += lf_putstr(file, bits->field->val_string);
150 if (bits->opcode->is_boolean && bits->value == 0)
151 nr += lf_putint(file, bits->opcode->boolean_constant);
152 else if (!bits->opcode->is_boolean) {
153 if (bits->opcode->last < bits->field->last)
154 nr += lf_putint(file, bits->value << (bits->field->last - bits->opcode->last));
155 else
156 nr += lf_putint(file, bits->value);
157 }
158 return nr;
159 }
160
161 extern int
162 print_function_name(lf *file,
163 const char *basename,
164 insn_bits *expanded_bits,
165 lf_function_name_prefixes prefix)
166 {
167 int nr = 0;
168 /* the prefix */
169 switch (prefix) {
170 case function_name_prefix_semantics:
171 nr += lf_putstr(file, global_name_prefix);
172 nr += lf_putstr(file, "semantic_");
173 break;
174 case function_name_prefix_idecode:
175 nr += lf_putstr(file, global_name_prefix);
176 nr += lf_printf(file, "idecode_");
177 break;
178 case function_name_prefix_itable:
179 nr += lf_putstr(file, "itable_");
180 break;
181 case function_name_prefix_icache:
182 nr += lf_putstr(file, global_name_prefix);
183 nr += lf_putstr(file, "icache_");
184 break;
185 default:
186 break;
187 }
188
189 /* the function name */
190 {
191 const char *pos;
192 for (pos = basename;
193 *pos != '\0';
194 pos++) {
195 switch (*pos) {
196 case '/':
197 case '-':
198 break;
199 case ' ':
200 case '.':
201 nr += lf_putchr(file, '_');
202 break;
203 default:
204 nr += lf_putchr(file, *pos);
205 break;
206 }
207 }
208 }
209
210 /* the suffix */
211 if (generate_expanded_instructions)
212 nr += print_insn_bits(file, expanded_bits);
213
214 return nr;
215 }
216
217
218 void
219 print_my_defines(lf *file,
220 insn_bits *expanded_bits,
221 table_entry *file_entry)
222 {
223 /* #define MY_INDEX xxxxx */
224 lf_indent_suppress(file);
225 lf_printf(file, "#undef MY_INDEX\n");
226 lf_indent_suppress(file);
227 lf_printf(file, "#define MY_INDEX ");
228 print_function_name(file,
229 file_entry->fields[insn_name],
230 NULL,
231 function_name_prefix_itable);
232 lf_printf(file, "\n");
233 /* #define MY_PREFIX xxxxxx */
234 lf_indent_suppress(file);
235 lf_printf(file, "#undef MY_PREFIX\n");
236 lf_indent_suppress(file);
237 lf_printf(file, "#define MY_PREFIX ");
238 print_function_name(file,
239 file_entry->fields[insn_name],
240 expanded_bits,
241 function_name_prefix_none);
242 lf_printf(file, "\n");
243 }
244
245
246 void
247 print_itrace(lf *file,
248 table_entry *file_entry,
249 int idecode)
250 {
251 const char *phase = (idecode) ? "DECODE" : "INSN";
252 const char *phase_lc = (idecode) ? "decode" : "insn";
253 lf_printf(file, "\n");
254 lf_indent_suppress(file);
255 lf_printf(file, "#if defined(WITH_TRACE)\n");
256 lf_printf(file, "/* trace the instructions execution if enabled */\n");
257 lf_printf(file, "if (TRACE_%s_P (CPU)) {\n", phase);
258 lf_printf(file, " trace_one_insn (SD, CPU, %s, TRACE_LINENUM_P (CPU),\n",
259 (code & generate_with_semantic_delayed_branch) ? "cia.ip" : "cia");
260
261 lf_printf(file, " itable[MY_INDEX].file, MY_INDEX, \"%s\",\n", phase_lc);
262 lf_printf(file, " itable[MY_INDEX].name);\n");
263
264 lf_printf(file, "}\n");
265 lf_indent_suppress(file);
266 lf_printf(file, "#endif\n");
267 }
268
269
270 /****************************************************************/
271
272
273 static void
274 gen_semantics_h(insn_table *table,
275 lf *file,
276 igen_code generate)
277 {
278 lf_printf(file, "typedef ");
279 print_semantic_function_type(file);
280 lf_printf(file, " %sidecode_semantic\n(", global_name_prefix);
281 print_semantic_function_formal(file);
282 lf_printf(file, ");\n");
283 lf_printf(file, "\n");
284 if ((code & generate_calls)) {
285 if (generate_expanded_instructions)
286 insn_table_traverse_tree(table,
287 file, NULL,
288 1,
289 NULL, /* start */
290 print_semantic_declaration, /* leaf */
291 NULL, /* end */
292 NULL); /* padding */
293 else
294 insn_table_traverse_insn(table,
295 file, NULL,
296 print_semantic_declaration);
297
298 }
299 else {
300 lf_print__this_file_is_empty(file);
301 }
302 }
303
304
305 static void
306 gen_semantics_c(insn_table *table,
307 cache_table *cache_rules,
308 lf *file,
309 igen_code generate)
310 {
311 if ((code & generate_calls)) {
312 lf_printf(file, "\n");
313 lf_printf(file, "#include \"sim-main.h\"\n");
314 lf_printf(file, "#include \"%sidecode.h\"\n", global_name_prefix);
315 lf_printf(file, "#include \"%ssemantics.h\"\n", global_name_prefix);
316 lf_printf(file, "#include \"%ssupport.h\"\n", global_name_prefix);
317 lf_printf(file, "\n");
318 if (generate_expanded_instructions)
319 insn_table_traverse_tree(table,
320 file, cache_rules,
321 1,
322 NULL, /* start */
323 print_semantic_definition, /* leaf */
324 NULL, /* end */
325 NULL); /* padding */
326 else
327 insn_table_traverse_insn(table,
328 file, cache_rules,
329 print_semantic_definition);
330
331 }
332 else {
333 lf_print__this_file_is_empty(file);
334 }
335 }
336
337
338 /****************************************************************/
339
340
341 static void
342 gen_icache_h(insn_table *table,
343 lf *file,
344 igen_code generate)
345 {
346 lf_printf(file, "typedef ");
347 print_icache_function_type(file);
348 lf_printf(file, " %sidecode_icache\n(", global_name_prefix);
349 print_icache_function_formal(file);
350 lf_printf(file, ");\n");
351 lf_printf(file, "\n");
352 if ((code & generate_calls)
353 && (code & generate_with_icache)) {
354 insn_table_traverse_function(table,
355 file, NULL,
356 print_icache_internal_function_declaration);
357 if (generate_expanded_instructions)
358 insn_table_traverse_tree(table,
359 file, NULL,
360 1,
361 NULL, /* start */
362 print_icache_declaration, /* leaf */
363 NULL, /* end */
364 NULL); /* padding */
365 else
366 insn_table_traverse_insn(table,
367 file, NULL,
368 print_icache_declaration);
369
370 }
371 else {
372 lf_print__this_file_is_empty(file);
373 }
374 }
375
376 static void
377 gen_icache_c(insn_table *table,
378 cache_table *cache_rules,
379 lf *file,
380 igen_code generate)
381 {
382 /* output `internal' invalid/floating-point unavailable functions
383 where needed */
384 if ((code & generate_calls)
385 && (code & generate_with_icache)) {
386 lf_printf(file, "\n");
387 lf_printf(file, "#include \"cpu.h\"\n");
388 lf_printf(file, "#include \"idecode.h\"\n");
389 lf_printf(file, "#include \"semantics.h\"\n");
390 lf_printf(file, "#include \"icache.h\"\n");
391 lf_printf(file, "#include \"support.h\"\n");
392 lf_printf(file, "\n");
393 insn_table_traverse_function(table,
394 file, NULL,
395 print_icache_internal_function_definition);
396 lf_printf(file, "\n");
397 if (generate_expanded_instructions)
398 insn_table_traverse_tree(table,
399 file, cache_rules,
400 1,
401 NULL, /* start */
402 print_icache_definition, /* leaf */
403 NULL, /* end */
404 NULL); /* padding */
405 else
406 insn_table_traverse_insn(table,
407 file, cache_rules,
408 print_icache_definition);
409
410 }
411 else {
412 lf_print__this_file_is_empty(file);
413 }
414 }
415
416
417 /****************************************************************/
418
419
420 int
421 main(int argc,
422 char **argv,
423 char **envp)
424 {
425 cache_table *cache_rules = NULL;
426 lf_file_references file_references = lf_include_references;
427 decode_table *decode_rules = NULL;
428 filter *filters = NULL;
429 insn_table *instructions = NULL;
430 char *real_file_name = NULL;
431 int is_header = 0;
432 int ch;
433
434 if (argc == 1) {
435 printf("Usage:\n");
436 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
437 printf("Config options:\n");
438 printf(" -F <filter-flag> eg -F 32 to include 32bit instructions\n");
439 printf(" -I <icache-size> Specify size of cracking cache\n");
440 printf(" -B <bit-size> Set the number of bits in an instruction\n");
441 printf(" -H <high-bit> Set the number of the high (msb) instruction bit\n");
442 printf(" -N <nr-cpus> Specify the max number of cpus the simulation will support\n");
443 printf(" -T <mechanism> Override the decode mechanism specified by the decode rules\n");
444 printf(" -P <prefix> Prepend all functions with the specified prefix\n");
445 printf(" -G <gen-option> Any of the following options:\n");
446 printf(" field-widths - instruction table specifies widths (not ofsesets)\n");
447 printf(" jumps - use jumps instead of function calls\n");
448 printf(" duplicate - expand (duplicate) semantic functions\n");
449 printf(" omit-line-numbers - do not include line nr info in output\n");
450 printf(" direct-access - use #defines to directly access values\n");
451 printf(" icache - generate an instruction cracking cache\n");
452 printf(" semantic-icache - include semantic code in cracking functions\n");
453 printf(" insn-in-icache - save original instruction when cracking\n");
454 printf(" default-nia-minus-one - instead of cia + insn-size\n");
455 printf(" delayed-branch - instead of cia + insn-size\n");
456 printf(" conditional-issue - conditionally issue each instruction\n");
457 printf(" validate-slot - perform slot verification as part of decode\n");
458 printf("\n");
459 printf("Input options:\n");
460 printf(" -o <decode-rules>\n");
461 printf(" -k <cache-rules>\n");
462 printf(" -i <instruction-table>\n");
463 printf("\n");
464 printf("Output options:\n");
465 printf(" -n <real-name> Specify the real name of the next output file\n");
466 printf(" -h Generate header file\n");
467 printf(" -c <output-file> output icache\n");
468 printf(" -d <output-file> output idecode\n");
469 printf(" -e <output-file> output engine\n");
470 printf(" -f <output-file> output support functions\n");
471 printf(" -m <output-file> output model\n");
472 printf(" -s <output-file> output schematic\n");
473 printf(" -t <output-file> output itable\n");
474 }
475
476 while ((ch = getopt(argc, argv,
477 "F:I:B:H:N:T:P:G:o:k:i:n:hc:d:e:m:s:t:f:"))
478 != -1) {
479 fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
480
481 switch(ch) {
482
483 case 'F':
484 filters = new_filter(optarg, filters);
485 break;
486
487 case 'I':
488 icache_size = a2i(optarg);
489 code |= generate_with_icache;
490 break;
491
492 case 'B':
493 insn_bit_size = a2i(optarg);
494 ASSERT(insn_bit_size > 0 && insn_bit_size <= max_insn_bit_size
495 && (hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0));
496 break;
497
498 case 'H':
499 hi_bit_nr = a2i(optarg);
500 ASSERT(hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0);
501 break;
502
503 case 'N':
504 generate_smp = a2i(optarg);
505 break;
506
507 case 'T':
508 force_decode_gen_type(optarg);
509 break;
510
511 case 'P':
512 {
513 char *chp;
514 global_name_prefix = strdup(optarg);
515 chp = strdup(optarg);
516 global_uname_prefix = chp;
517 while (*chp) {
518 if (islower(*chp))
519 *chp = toupper(*chp);
520 chp++;
521 }
522 }
523 break;
524
525 case 'G':
526 if (strcmp(optarg, "jumps") == 0) {
527 code &= ~generate_calls;
528 code |= generate_jumps;
529 }
530 else if (strcmp(optarg, "field-widths") == 0) {
531 insn_specifying_widths = 1;
532 }
533 else if (strcmp(optarg, "duplicate") == 0) {
534 generate_expanded_instructions = 1;
535 }
536 else if (strcmp(optarg, "omit-line-numbers") == 0) {
537 file_references = lf_omit_references;
538 }
539 else if (strcmp(optarg, "direct-access") == 0) {
540 code |= generate_with_direct_access;
541 }
542 else if (strcmp(optarg, "icache") == 0) {
543 }
544 else if (strcmp(optarg, "semantic-icache") == 0) {
545 code |= generate_with_icache;
546 code |= generate_with_semantic_icache;
547 }
548 else if (strcmp(optarg, "insn-in-icache") == 0) {
549 code |= generate_with_icache;
550 code |= generate_with_insn_in_icache;
551 }
552 else if (strcmp(optarg, "default-nia-minus-one") == 0) {
553 code |= generate_with_semantic_returning_modified_nia_only;
554 code &= ~generate_with_semantic_delayed_branch;
555 }
556 else if (strcmp(optarg, "delayed-branch") == 0) {
557 code |= generate_with_semantic_delayed_branch;
558 code &= ~generate_with_semantic_returning_modified_nia_only;
559 }
560 else if (strcmp(optarg, "conditional-issue") == 0) {
561 code |= generate_with_semantic_conditional_issue;
562 }
563 else if (strcmp(optarg, "verify-slot") == 0) {
564 code |= generate_with_idecode_slot_verification;
565 }
566 else
567 error("Unknown option %s", optarg);
568 break;
569
570 case 'i':
571 if (decode_rules == NULL || cache_rules == NULL) {
572 fprintf(stderr, "Must specify decode and cache tables\n");
573 exit (1);
574 }
575 instructions = load_insn_table(optarg, decode_rules, filters);
576 fprintf(stderr, "\texpanding ...\n");
577 insn_table_expand_insns(instructions);
578 break;
579 case 'o':
580 decode_rules = load_decode_table(optarg, hi_bit_nr);
581 break;
582 case 'k':
583 cache_rules = load_cache_table(optarg, hi_bit_nr);
584 break;
585 case 'n':
586 real_file_name = strdup(optarg);
587 break;
588 case 'h':
589 is_header = 1;
590 break;
591 case 's':
592 case 'd':
593 case 'e':
594 case 'm':
595 case 't':
596 case 'f':
597 case 'c':
598 {
599 lf *file = lf_open(optarg, real_file_name, file_references,
600 (is_header ? lf_is_h : lf_is_c),
601 argv[0]);
602 lf_print__file_start(file);
603 ASSERT(instructions != NULL);
604 switch (ch) {
605 case 's':
606 if(is_header)
607 gen_semantics_h(instructions, file, code);
608 else
609 gen_semantics_c(instructions, cache_rules, file, code);
610 break;
611 case 'd':
612 if (is_header)
613 gen_idecode_h(file, instructions, cache_rules);
614 else
615 gen_idecode_c(file, instructions, cache_rules);
616 break;
617 case 'e':
618 if (is_header)
619 gen_engine_h(file, instructions, cache_rules);
620 else
621 gen_engine_c(file, instructions, cache_rules);
622 break;
623 case 'm':
624 if (is_header)
625 gen_model_h(instructions, file);
626 else
627 gen_model_c(instructions, file);
628 break;
629 case 't':
630 if (is_header)
631 gen_itable_h(instructions, file);
632 else
633 gen_itable_c(instructions, file);
634 break;
635 case 'f':
636 if (is_header)
637 gen_support_h(instructions, file);
638 else
639 gen_support_c(instructions, file);
640 break;
641 case 'c':
642 if (is_header)
643 gen_icache_h(instructions, file, code);
644 else
645 gen_icache_c(instructions, cache_rules, file, code);
646 break;
647 }
648 lf_print__file_finish(file);
649 lf_close(file);
650 is_header = 0;
651 }
652 real_file_name = NULL;
653 break;
654 default:
655 error("unknown option\n");
656 }
657 }
658 return 0;
659 }