Enable --trace-linenum support
[platform/upstream/binutils.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   lf_printf(file, "\n");
252   lf_indent_suppress(file);
253   lf_printf(file, "#if defined(WITH_TRACE)\n");
254   lf_printf(file, "/* trace the instructions execution if enabled */\n");
255   lf_printf(file, "if (TRACE_%s_P (CPU)) {\n", (idecode) ? "DECODE" : "INSN");
256   lf_printf(file, "  trace_one_insn (SD, CPU, \"%s\", %d, %d, %s, itable[MY_INDEX].name);\n",
257             filter_filename(file_entry->file_name),
258             file_entry->line_nr,
259             idecode,
260             (code & generate_with_semantic_delayed_branch) ? "cia.ip" : "cia");
261             
262   lf_printf(file, "}\n");
263   lf_indent_suppress(file);
264   lf_printf(file, "#endif\n");
265 }
266
267
268 /****************************************************************/
269
270
271 static void
272 gen_semantics_h(insn_table *table,
273                 lf *file,
274                 igen_code generate)
275 {
276   lf_printf(file, "typedef ");
277   print_semantic_function_type(file);
278   lf_printf(file, " %sidecode_semantic\n(", global_name_prefix);
279   print_semantic_function_formal(file);
280   lf_printf(file, ");\n");
281   lf_printf(file, "\n");
282   if ((code & generate_calls)) {
283     if (generate_expanded_instructions)
284       insn_table_traverse_tree(table,
285                                file, NULL,
286                                1,
287                                NULL, /* start */
288                                print_semantic_declaration, /* leaf */
289                                NULL, /* end */
290                                NULL); /* padding */
291     else
292       insn_table_traverse_insn(table,
293                                file, NULL,
294                                print_semantic_declaration);
295     
296   }
297   else {
298     lf_print__this_file_is_empty(file);
299   }
300 }
301
302
303 static void
304 gen_semantics_c(insn_table *table,
305                 cache_table *cache_rules,
306                 lf *file,
307                 igen_code generate)
308 {
309   if ((code & generate_calls)) {
310     lf_printf(file, "\n");
311     lf_printf(file, "#include \"sim-main.h\"\n");
312     lf_printf(file, "#include \"%sidecode.h\"\n", global_name_prefix);
313     lf_printf(file, "#include \"%ssemantics.h\"\n", global_name_prefix);
314     lf_printf(file, "#include \"%ssupport.h\"\n", global_name_prefix);
315     lf_printf(file, "\n");
316     if (generate_expanded_instructions)
317       insn_table_traverse_tree(table,
318                                file, cache_rules,
319                                1,
320                                NULL, /* start */
321                                print_semantic_definition, /* leaf */
322                                NULL, /* end */
323                                NULL); /* padding */
324     else
325       insn_table_traverse_insn(table,
326                                file, cache_rules,
327                                print_semantic_definition);
328     
329   }
330   else {
331     lf_print__this_file_is_empty(file);
332   }
333 }
334
335
336 /****************************************************************/
337
338
339 static void
340 gen_icache_h(insn_table *table,
341              lf *file,
342              igen_code generate)
343 {
344   lf_printf(file, "typedef ");
345   print_icache_function_type(file);
346   lf_printf(file, " %sidecode_icache\n(", global_name_prefix);
347   print_icache_function_formal(file);
348   lf_printf(file, ");\n");
349   lf_printf(file, "\n");
350   if ((code & generate_calls)
351       && (code & generate_with_icache)) {
352     insn_table_traverse_function(table,
353                                  file, NULL,
354                                  print_icache_internal_function_declaration);
355     if (generate_expanded_instructions)
356       insn_table_traverse_tree(table,
357                                file, NULL,
358                                1,
359                                NULL, /* start */
360                                print_icache_declaration, /* leaf */
361                                NULL, /* end */
362                                NULL); /* padding */
363     else
364       insn_table_traverse_insn(table,
365                                file, NULL,
366                                print_icache_declaration);
367     
368   }
369   else {
370     lf_print__this_file_is_empty(file);
371   }
372 }
373
374 static void
375 gen_icache_c(insn_table *table,
376              cache_table *cache_rules,
377              lf *file,
378              igen_code generate)
379 {
380   /* output `internal' invalid/floating-point unavailable functions
381      where needed */
382   if ((code & generate_calls)
383       && (code & generate_with_icache)) {
384     lf_printf(file, "\n");
385     lf_printf(file, "#include \"cpu.h\"\n");
386     lf_printf(file, "#include \"idecode.h\"\n");
387     lf_printf(file, "#include \"semantics.h\"\n");
388     lf_printf(file, "#include \"icache.h\"\n");
389     lf_printf(file, "#include \"support.h\"\n");
390     lf_printf(file, "\n");
391     insn_table_traverse_function(table,
392                                  file, NULL,
393                                  print_icache_internal_function_definition);
394     lf_printf(file, "\n");
395     if (generate_expanded_instructions)
396       insn_table_traverse_tree(table,
397                                file, cache_rules,
398                                1,
399                                NULL, /* start */
400                                print_icache_definition, /* leaf */
401                                NULL, /* end */
402                                NULL); /* padding */
403     else
404       insn_table_traverse_insn(table,
405                                file, cache_rules,
406                                print_icache_definition);
407     
408   }
409   else {
410     lf_print__this_file_is_empty(file);
411   }
412 }
413
414
415 /****************************************************************/
416
417
418 int
419 main(int argc,
420      char **argv,
421      char **envp)
422 {
423   cache_table *cache_rules = NULL;
424   lf_file_references file_references = lf_include_references;
425   decode_table *decode_rules = NULL;
426   filter *filters = NULL;
427   insn_table *instructions = NULL;
428   char *real_file_name = NULL;
429   int is_header = 0;
430   int ch;
431
432   if (argc == 1) {
433     printf("Usage:\n");
434     printf("  igen <config-opts> ... <input-opts>... <output-opts>...\n");
435     printf("Config options:\n");
436     printf("  -F <filter-flag>      eg -F 32 to include 32bit instructions\n");
437     printf("  -I <icache-size>  Specify size of cracking cache\n");
438     printf("  -B <bit-size>         Set the number of bits in an instruction\n");
439     printf("  -H <high-bit>         Set the number of the high (msb) instruction bit\n");
440     printf("  -N <nr-cpus>          Specify the max number of cpus the simulation will support\n");
441     printf("  -T <mechanism>        Override the decode mechanism specified by the decode rules\n");
442     printf("  -P <prefix>           Prepend all functions with the specified prefix\n");
443     printf("  -G <gen-option>   Any of the following options:\n");
444     printf("                      field-widths - instruction table specifies widths (not ofsesets)\n");
445     printf("                      jumps - use jumps instead of function calls\n");
446     printf("                      duplicate - expand (duplicate) semantic functions\n");
447     printf("                      omit-line-numbers - do not include line nr info in output\n");
448     printf("                      direct-access - use #defines to directly access values\n");
449     printf("                      icache - generate an instruction cracking cache\n");
450     printf("                      semantic-icache - include semantic code in cracking functions\n");
451     printf("                      insn-in-icache - save original instruction when cracking\n");
452     printf("                      default-nia-minus-one - instead of cia + insn-size\n");
453     printf("                      delayed-branch - instead of cia + insn-size\n");
454     printf("                      conditional-issue - conditionally issue each instruction\n");
455     printf("                      validate-slot - perform slot verification as part of decode\n");
456     printf("\n");
457     printf("Input options:\n");
458     printf("  -o <decode-rules>\n");
459     printf("  -k <cache-rules>\n");
460     printf("  -i <instruction-table>\n");
461     printf("\n");
462     printf("Output options:\n");
463     printf("  -n <real-name>        Specify the real name of the next output file\n"); 
464     printf("  -h                    Generate header file\n");
465     printf("  -c <output-file>      output icache\n");
466     printf("  -d <output-file>      output idecode\n");
467     printf("  -e <output-file>      output engine\n");
468     printf("  -f <output-file>      output support functions\n");
469     printf("  -m <output-file>      output model\n");
470     printf("  -s <output-file>      output schematic\n");
471     printf("  -t <output-file>      output itable\n");
472   }
473
474   while ((ch = getopt(argc, argv,
475                       "F:I:B:H:N:T:P:G:o:k:i:n:hc:d:e:m:s:t:f:"))
476          != -1) {
477     fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
478
479     switch(ch) {
480
481     case 'F':
482       filters = new_filter(optarg, filters);
483       break;
484
485     case 'I':
486       icache_size = a2i(optarg);
487       code |= generate_with_icache;
488       break;
489
490     case 'B':
491       insn_bit_size = a2i(optarg);
492       ASSERT(insn_bit_size > 0 && insn_bit_size <= max_insn_bit_size
493              && (hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0));
494       break;
495
496     case 'H':
497       hi_bit_nr = a2i(optarg);
498       ASSERT(hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0);
499       break;
500
501     case 'N':
502       generate_smp = a2i(optarg);
503       break;
504
505     case 'T':
506       force_decode_gen_type(optarg);
507       break;
508
509     case 'P':
510       {
511         char *chp;
512         global_name_prefix = strdup(optarg);
513         chp = strdup(optarg);
514         global_uname_prefix = chp;
515         while (*chp) {
516           if (islower(*chp))
517             *chp = toupper(*chp);
518           chp++;
519         }
520       }
521       break;
522
523     case 'G':
524       if (strcmp(optarg, "jumps") == 0) {
525         code &= ~generate_calls;
526         code |= generate_jumps;
527       }
528       else if (strcmp(optarg, "field-widths") == 0) {
529         insn_specifying_widths = 1;
530       }
531       else if (strcmp(optarg, "duplicate") == 0) {
532         generate_expanded_instructions = 1;
533       }
534       else if (strcmp(optarg, "omit-line-numbers") == 0) {
535         file_references = lf_omit_references;
536       }
537       else if (strcmp(optarg, "direct-access") == 0) {
538         code |= generate_with_direct_access;
539       }
540       else if (strcmp(optarg, "icache") == 0) {
541       }
542       else if (strcmp(optarg, "semantic-icache") == 0) {
543         code |= generate_with_icache;
544         code |= generate_with_semantic_icache;
545       }
546       else if (strcmp(optarg, "insn-in-icache") == 0) {
547         code |= generate_with_icache;
548         code |= generate_with_insn_in_icache;
549       }
550       else if (strcmp(optarg, "default-nia-minus-one") == 0) {
551         code |= generate_with_semantic_returning_modified_nia_only;
552         code &= ~generate_with_semantic_delayed_branch;
553       }
554       else if (strcmp(optarg, "delayed-branch") == 0) {
555         code |= generate_with_semantic_delayed_branch;
556         code &= ~generate_with_semantic_returning_modified_nia_only;
557       }
558       else if (strcmp(optarg, "conditional-issue") == 0) {
559         code |= generate_with_semantic_conditional_issue;
560       }
561       else if (strcmp(optarg, "verify-slot") == 0) {
562         code |= generate_with_idecode_slot_verification;
563       }
564       else
565         error("Unknown option %s", optarg);
566       break;
567
568     case 'i':
569       if (decode_rules == NULL || cache_rules == NULL) {
570         fprintf(stderr, "Must specify decode and cache tables\n");
571         exit (1);
572       }
573       instructions = load_insn_table(optarg, decode_rules, filters);
574       fprintf(stderr, "\texpanding ...\n");
575       insn_table_expand_insns(instructions);
576       break;
577     case 'o':
578       decode_rules = load_decode_table(optarg, hi_bit_nr);
579       break;
580     case 'k':
581       cache_rules = load_cache_table(optarg, hi_bit_nr);
582       break;
583     case 'n':
584       real_file_name = strdup(optarg);
585       break;
586     case 'h':
587       is_header = 1;
588       break;
589     case 's':
590     case 'd':
591     case 'e':
592     case 'm':
593     case 't':
594     case 'f':
595     case 'c':
596       {
597         lf *file = lf_open(optarg, real_file_name, file_references,
598                            (is_header ? lf_is_h : lf_is_c),
599                            argv[0]);
600         lf_print__file_start(file);
601         ASSERT(instructions != NULL);
602         switch (ch) {
603         case 's':
604           if(is_header)
605             gen_semantics_h(instructions, file, code);
606           else
607             gen_semantics_c(instructions, cache_rules, file, code);
608           break;
609         case 'd':
610           if (is_header)
611             gen_idecode_h(file, instructions, cache_rules);
612           else
613             gen_idecode_c(file, instructions, cache_rules);
614           break;
615         case 'e':
616           if (is_header)
617             gen_engine_h(file, instructions, cache_rules);
618           else
619             gen_engine_c(file, instructions, cache_rules);
620           break;
621         case 'm':
622           if (is_header)
623             gen_model_h(instructions, file);
624           else
625             gen_model_c(instructions, file);
626           break;
627         case 't':
628           if (is_header)
629             gen_itable_h(instructions, file);
630           else
631             gen_itable_c(instructions, file);
632           break;
633         case 'f':
634           if (is_header)
635             gen_support_h(instructions, file);
636           else
637             gen_support_c(instructions, file);
638           break;
639         case 'c':
640           if (is_header)
641             gen_icache_h(instructions, file, code);
642           else
643             gen_icache_c(instructions, cache_rules, file, code);
644           break;
645         }
646         lf_print__file_finish(file);
647         lf_close(file);
648         is_header = 0;
649       }
650       real_file_name = NULL;
651       break;
652     default:
653       error("unknown option\n");
654     }
655   }
656   return 0;
657 }