Don't bother generating trace prefix string when not tracing.
[platform/upstream/binutils.git] / sim / igen / igen.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1998, 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 #include "filter.h"
30
31 #include "igen.h"
32
33 #include "ld-insn.h"
34 #include "ld-decode.h"
35 #include "ld-cache.h"
36
37 #include "gen.h"
38
39 #include "gen-model.h"
40 #include "gen-icache.h"
41 #include "gen-itable.h"
42 #include "gen-idecode.h"
43 #include "gen-semantics.h"
44 #include "gen-engine.h"
45 #include "gen-support.h"
46 #include "gen-engine.h"
47
48
49 /****************************************************************/
50
51
52 /* Semantic functions */
53
54 int
55 print_semantic_function_formal (lf *file,
56                                 int nr_prefetched_words)
57 {
58   int nr = 0;
59   int word_nr;
60   if (options.gen.icache || nr_prefetched_words < 0)
61     {
62       nr += lf_printf (file, "SIM_DESC sd,\n");
63       nr += lf_printf (file, "%sidecode_cache *cache_entry,\n",
64                        options.prefix.global.name);
65       nr += lf_printf (file, "%sinstruction_address cia",
66                        options.prefix.global.name);
67     }
68   else if (options.gen.smp)
69     {
70       nr += lf_printf (file, "sim_cpu *cpu,\n");
71       for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
72         {
73           nr += lf_printf (file, "%sinstruction_word instruction_%d,\n",
74                            options.prefix.global.name,
75                            word_nr);
76         }
77       nr += lf_printf (file, "%sinstruction_address cia",
78                        options.prefix.global.name);
79     }
80   else
81     {
82       nr += lf_printf (file, "SIM_DESC sd,\n");
83       for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
84         {
85           nr += lf_printf (file, "%sinstruction_word instruction_%d,\n",
86                            options.prefix.global.name,
87                            word_nr);
88         }
89       nr += lf_printf (file, "%sinstruction_address cia",
90                        options.prefix.global.name);
91     }
92   return nr;
93 }
94
95 int
96 print_semantic_function_actual (lf *file,
97                                 int nr_prefetched_words)
98 {
99   int nr = 0;
100   int word_nr;
101   if (options.gen.icache || nr_prefetched_words < 0)
102     {
103       nr += lf_printf (file, "sd, cache_entry, cia");
104     }
105   else
106     {
107       if (options.gen.smp)
108         nr += lf_printf (file, "cpu");
109       else
110         nr += lf_printf (file, "sd");
111       for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
112         nr += lf_printf (file, ", instruction_%d", word_nr);
113       nr += lf_printf (file, ", cia");
114     }
115   return nr;
116 }
117
118 int
119 print_semantic_function_type (lf *file)
120 {
121   int nr = 0;
122   nr += lf_printf (file, "%sinstruction_address",
123                    options.prefix.global.name);
124   return nr;
125 }
126
127
128 /* Idecode functions */
129
130 int
131 print_icache_function_formal (lf *file,
132                               int nr_prefetched_words)
133 {
134   int nr = 0;
135   int word_nr;
136   if (options.gen.smp)
137       nr += lf_printf (file, "sim_cpu *cpu,\n");
138   else
139       nr += lf_printf (file, "SIM_DESC sd,\n");
140   for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
141     nr += lf_printf (file, " %sinstruction_word instruction_%d,\n",
142                      options.prefix.global.name, word_nr);
143   nr += lf_printf (file, " %sinstruction_address cia,\n",
144                    options.prefix.global.name);
145   nr += lf_printf (file, " %sidecode_cache *cache_entry",
146                    options.prefix.global.name);
147   return nr;
148 }
149
150 int
151 print_icache_function_actual (lf *file,
152                               int nr_prefetched_words)
153 {
154   int nr = 0;
155   int word_nr;
156   if (options.gen.smp)
157     nr += lf_printf (file, "cpu");
158   else
159     nr += lf_printf (file, "sd");
160   for (word_nr = 0; word_nr < nr_prefetched_words; word_nr++)
161     nr += lf_printf (file, ", instruction_%d", word_nr);
162   nr += lf_printf (file, ", cia, cache_entry");
163   return nr;
164 }
165
166 int
167 print_icache_function_type (lf *file)
168 {
169   int nr;
170   if (options.gen.semantic_icache)
171     {
172       nr = print_semantic_function_type (file);
173     }
174   else
175     {
176       nr = lf_printf (file, "%sidecode_semantic *",
177                       options.prefix.global.name);
178     }
179   return nr;
180 }
181
182
183 /* Function names */
184
185 static int
186 print_opcode_bits (lf *file,
187                    opcode_bits *bits)
188 {
189   int nr = 0;
190   if (bits == NULL)
191     return nr;
192   nr += lf_putchr (file, '_');
193   nr += lf_putstr (file, bits->field->val_string);
194   if (bits->opcode->is_boolean && bits->value == 0)
195     nr += lf_putint (file, bits->opcode->boolean_constant);
196   else if (!bits->opcode->is_boolean) {
197     if (bits->opcode->last < bits->field->last)
198       nr += lf_putint (file, bits->value << (bits->field->last - bits->opcode->last));
199     else
200       nr += lf_putint (file, bits->value);
201   }
202   nr += print_opcode_bits (file, bits->next);
203   return nr;
204 }
205
206 static int
207 print_c_name (lf *file,
208               const char *name)
209 {
210   int nr = 0;
211   const char *pos;
212   for (pos = name; *pos != '\0'; pos++)
213     {
214       switch (*pos)
215         {
216         case '/':
217         case '-':
218           break;
219         case ' ':
220         case '.':
221           nr += lf_putchr (file, '_');
222           break;
223         default:
224           nr += lf_putchr (file, *pos);
225           break;
226         }
227     }
228   return nr;
229 }
230
231 extern int
232 print_function_name (lf *file,
233                      const char *basename,
234                      const char *format_name,
235                      const char *model_name,
236                      opcode_bits *expanded_bits,
237                      lf_function_name_prefixes prefix)
238 {
239   int nr = 0;
240   /* the prefix */
241   switch (prefix)
242     {
243     case function_name_prefix_semantics:
244       nr += lf_printf (file, "%s", options.prefix.semantics.name);
245       nr += lf_printf (file, "semantic_");
246       break;
247     case function_name_prefix_idecode:
248       nr += lf_printf (file, "%s", options.prefix.idecode.name);
249       nr += lf_printf (file, "idecode_");
250       break;
251     case function_name_prefix_itable:
252       nr += lf_printf (file, "%sitable_", options.prefix.itable.name);
253       break;
254     case function_name_prefix_icache:
255       nr += lf_printf (file, "%s", options.prefix.icache.name);
256       nr += lf_printf (file, "icache_");
257       break;
258     case function_name_prefix_engine:
259       nr += lf_printf (file, "%s", options.prefix.engine.name);
260       nr += lf_printf (file, "engine_");
261     default:
262       break;
263     }
264   
265   if (model_name != NULL)
266     {
267       nr += print_c_name (file, model_name);
268       nr += lf_printf (file, "_");
269     }
270
271   /* the function name */
272   nr += print_c_name (file, basename);
273   
274   /* the format name if available */
275   if (format_name != NULL)
276     {
277       nr += lf_printf (file, "_");
278       nr += print_c_name (file, format_name);
279     }
280
281   /* the suffix */
282   nr += print_opcode_bits (file, expanded_bits);
283
284   return nr;
285 }
286
287
288 void
289 print_my_defines (lf *file,
290                   const char *basename,
291                   const char *format_name,
292                   opcode_bits *expanded_bits)
293 {
294   /* #define MY_INDEX xxxxx */
295   lf_indent_suppress (file);
296   lf_printf (file, "#undef MY_INDEX\n");
297   lf_indent_suppress (file);
298   lf_printf (file, "#define MY_INDEX ");
299   print_function_name (file,
300                        basename, format_name, NULL,
301                        NULL,
302                        function_name_prefix_itable);
303   lf_printf (file, "\n");
304   /* #define MY_PREFIX xxxxxx */
305   lf_indent_suppress (file);
306   lf_printf (file, "#undef ");
307   print_function_name (file,
308                        basename, format_name, NULL,
309                        expanded_bits,
310                        function_name_prefix_none);
311   lf_printf (file, "\n");
312   lf_indent_suppress (file);
313   lf_printf (file, "#undef MY_PREFIX\n");
314   lf_indent_suppress (file);
315   lf_printf (file, "#define MY_PREFIX ");
316   print_function_name (file,
317                        basename, format_name, NULL,
318                        expanded_bits,
319                        function_name_prefix_none);
320   lf_printf (file, "\n");
321   /* #define MY_NAME xxxxxx */
322   lf_indent_suppress (file);
323   lf_indent_suppress (file);
324   lf_printf (file, "#undef MY_NAME\n");
325   lf_indent_suppress (file);
326   lf_printf (file, "#define MY_NAME \"");
327   print_function_name (file,
328                        basename, format_name, NULL,
329                        expanded_bits,
330                        function_name_prefix_none);
331   lf_printf (file, "\"\n");
332 }
333
334
335 static int
336 print_itrace_prefix (lf *file)
337 {
338   const char *prefix = "trace_prefix (";
339   int indent = strlen (prefix);
340   lf_printf (file, "%sSD, CPU, cia, CIA, TRACE_LINENUM_P (CPU), \\\n", prefix);
341   lf_indent (file, +indent);
342   lf_printf (file, "%sitable[MY_INDEX].file, \\\n", options.prefix.itable.name);
343   lf_printf (file, "%sitable[MY_INDEX].line_nr, \\\n", options.prefix.itable.name);
344   lf_printf (file, "\"");
345   return indent;
346 }
347
348
349 static void
350 print_itrace_format (lf *file,
351                      insn_mnemonic_entry *assembler)
352 {
353   /* pass=1 is fmt string; pass=2 is arguments */
354   int pass;
355   /* print the format string */
356   for (pass = 1; pass <= 2; pass++)
357     {
358       const char *chp = assembler->format;
359       chp++; /* skip the leading quote */
360       /* write out the format/args */
361       while (*chp != '\0')
362         {
363           if (chp[0] == '\\' && (chp[1] == '<' || chp[1] == '>'))
364             {
365               if (pass == 1)
366                 lf_putchr (file, chp[1]);
367               chp += 2;
368             }
369           else if (chp[0] == '<' || chp[0] == '%')
370             {
371               /* parse [ "%" ... ] "<" [ func "#" ] param ">" */
372               const char *fmt;
373               const char *func;
374               int strlen_func;
375               const char *param;
376               int strlen_param;
377               /* the "%" ... "<" format */
378               fmt = chp;
379               while (chp[0] != '<' && chp[0] != '\0')
380                 chp++;
381               if (chp[0] != '<')
382                 error (assembler->line, "Missing `<' after `%%'\n");
383               chp++;
384               /* [ "func" # ] OR "param" */
385               func = chp;
386               param = chp;
387               while (chp[0] != '>' && chp[0] != '#' && chp[0] != '\0')
388                 chp++;
389               strlen_func = chp - func;
390               if (chp[0] == '#')
391                 {
392                   chp++;
393                   param = chp;
394                   while (chp[0] != '>' && chp[0] != '\0')
395                     chp++;
396                 }
397               strlen_param = chp - param;
398               if (chp[0] != '>')
399                 error (assembler->line, "Missing closing `>' in assembler string\n");
400               chp++;
401               /* now process it */
402               if (pass == 2)
403                 lf_printf (file, ", \\\n");
404               if (strncmp (fmt, "<", 1) == 0)
405                 /* implicit long int format */
406                 {
407                   if (pass == 1)
408                     lf_printf (file, "%%ld");
409                   else
410                     {
411                       lf_printf (file, "(long) ");
412                       lf_write (file, param, strlen_param);
413                     }
414                 }
415               else if (strncmp (fmt, "%<", 2) == 0)
416                 /* explicit format */
417                 {
418                   if (pass == 1)
419                     lf_printf (file, "%%");
420                   else
421                     lf_write (file, param, strlen_param);
422                 }
423               else if (strncmp (fmt, "%s<", 3) == 0)
424                 /* string format */
425                 {
426                   if (pass == 1)
427                     lf_printf (file, "%%s");
428                   else
429                     {
430                       lf_printf (file, "%sstr_", options.prefix.global.name);
431                       lf_write (file, func, strlen_func);
432                       lf_printf (file, " (SD_, ");
433                       lf_write (file, param, strlen_param);
434                       lf_printf (file, ")");
435                     }
436                 }
437               else if (strncmp (fmt, "%lx<", 4) == 0)
438                 /* simple hex */
439                 {
440                   if (pass == 1)
441                     lf_printf (file, "%%lx");
442                   else
443                     {
444                       lf_printf (file, "(unsigned long) ");
445                       lf_write (file, param, strlen_param);
446                     }
447                 }
448               else if (strncmp (fmt, "%08lx<", 6) == 0)
449                 /* simple hex */
450                 {
451                   if (pass == 1)
452                     lf_printf (file, "%%08lx");
453                   else
454                     {
455                       lf_printf (file, "(unsigned long) ");
456                       lf_write (file, param, strlen_param);
457                     }
458                 }
459               else
460                 error (assembler->line, "Unknown assembler string format\n");
461             }
462           else
463             {
464               if (pass == 1)
465                 lf_putchr (file, chp[0]);
466               chp += 1;
467             }
468         }
469     }
470   lf_printf (file, ");\n");
471 }
472
473
474 void
475 print_itrace (lf *file,
476               insn_entry *insn,
477               int idecode)
478 {
479   /* NB: Here we escape each EOLN. This is so that the the compiler
480      treats a trace function call as a single line.  Consequently any
481      errors in the line are refered back to the same igen assembler
482      source line */
483   const char *phase = (idecode) ? "DECODE" : "INSN";
484   lf_printf (file, "\n");
485   lf_indent_suppress (file);
486   lf_printf (file, "#if defined (WITH_TRACE)\n");
487   lf_printf (file, "/* generate a trace prefix if any tracing enabled */\n");
488   lf_printf (file, "if (TRACE_ANY_P (CPU))\n");
489   lf_printf (file, "  {\n");
490   lf_indent (file, +4);
491   {
492     if (insn->mnemonics != NULL)
493       {
494         insn_mnemonic_entry *assembler = insn->mnemonics;
495         int is_first = 1;
496         do
497           {
498             if (assembler->condition != NULL)
499               {
500                 int indent;
501                 lf_printf (file, "%sif (%s)\n",
502                            is_first ? "" : "else ",
503                            assembler->condition);
504                 lf_indent (file, +2);
505                 lf_print__line_ref (file, assembler->line);
506                 indent = print_itrace_prefix (file);
507                 print_itrace_format (file, assembler);
508                 lf_print__internal_ref (file);
509                 lf_indent (file, -indent);
510                 lf_indent (file, -2);
511                 if (assembler->next == NULL)
512                   error (assembler->line, "Missing final unconditional assembler\n");
513               }
514             else
515               {
516                 int indent;
517                 if (!is_first)
518                   {
519                     lf_printf (file, "else\n");
520                     lf_indent (file, +2);
521                   }
522                 lf_print__line_ref (file, assembler->line);
523                 indent = print_itrace_prefix (file);
524                 print_itrace_format (file, assembler);
525                 lf_print__internal_ref (file);
526                 lf_indent (file, -indent);
527                 if (!is_first)
528                   lf_indent (file, -2);
529                 if (assembler->next != NULL)
530                   error (assembler->line, "Unconditional assembler is not last\n");
531               }
532             is_first = 0;
533             assembler = assembler->next;
534           }
535         while (assembler != NULL);
536       }
537     else
538       {
539         int indent;
540         lf_indent (file, +2);
541         lf_print__line_ref (file, insn->line);
542         indent = print_itrace_prefix (file);
543         lf_printf (file, "%%s\", \\\n");
544         lf_printf (file, "itable[MY_INDEX].name);\n");
545         lf_print__internal_ref (file);
546         lf_indent (file, -indent);
547         lf_indent (file, -2);
548       }
549     lf_printf (file, "/* trace the instruction execution if enabled */\n");
550     lf_printf (file, "if (TRACE_%s_P (CPU))\n", phase);
551     lf_printf (file, "  trace_generic (SD, CPU, TRACE_%s_IDX, \" %%s\", itable[MY_INDEX].name);\n", phase);
552   }
553   lf_indent (file, -4);
554   lf_printf (file, "  }\n");
555   lf_indent_suppress (file);
556   lf_printf (file, "#endif\n");
557 }
558
559
560 void
561 print_sim_engine_abort (lf *file,
562                         const char *message)
563 {
564   lf_printf (file, "sim_engine_abort (SD, CPU, cia, ");
565   lf_printf (file, "\"%s\"", message);
566   lf_printf (file, ");\n");
567 }
568
569
570 /****************************************************************/
571
572
573 static void
574 gen_semantics_h (lf *file,
575                  insn_list *semantics,
576                  int max_nr_words)
577 {
578   int word_nr;
579   insn_list *semantic;
580   for (word_nr = -1; word_nr <= max_nr_words; word_nr++)
581     {
582       lf_printf (file, "typedef ");
583       print_semantic_function_type (file);
584       lf_printf (file, " %sidecode_semantic",
585                  options.prefix.global.name);
586       if (word_nr >= 0)
587         lf_printf (file, "_%d", word_nr);
588       lf_printf (file, "\n(");
589       lf_indent (file, +1);
590       print_semantic_function_formal (file, word_nr);
591       lf_indent (file, -1);
592       lf_printf (file, ");\n");
593       lf_printf (file, "\n");
594     }
595   switch (options.gen.code)
596     {
597     case generate_calls:
598       for (semantic = semantics; semantic != NULL; semantic = semantic->next)
599         {
600           /* Ignore any special/internal instructions */
601           if (semantic->insn->nr_words == 0)
602             continue;
603           print_semantic_declaration (file,
604                                       semantic->insn,
605                                       semantic->expanded_bits,
606                                       semantic->opcodes,
607                                       semantic->nr_prefetched_words);
608         }
609       break;
610     case generate_jumps:
611       lf_print__this_file_is_empty (file, "generating jumps");
612       break;
613     }
614 }
615
616
617 static void
618 gen_semantics_c (lf *file,
619                  insn_list *semantics,
620                  cache_entry *cache_rules)
621 {
622   if (options.gen.code == generate_calls)
623     {
624       insn_list *semantic;
625       lf_printf (file, "\n");
626       lf_printf (file, "#include \"sim-main.h\"\n");
627       lf_printf (file, "#include \"%sitable.h\"\n",
628                  options.prefix.itable.name);
629       lf_printf (file, "#include \"%sidecode.h\"\n",
630                  options.prefix.idecode.name);
631       lf_printf (file, "#include \"%ssemantics.h\"\n",
632                  options.prefix.semantics.name);
633       lf_printf (file, "#include \"%ssupport.h\"\n",
634                  options.prefix.support.name);
635       lf_printf (file, "\n");
636       for (semantic = semantics; semantic != NULL; semantic = semantic->next)
637         {
638           /* Ignore any special/internal instructions */
639           if (semantic->insn->nr_words == 0)
640             continue;
641           print_semantic_definition (file,
642                                      semantic->insn,
643                                      semantic->expanded_bits,
644                                      semantic->opcodes,
645                                      cache_rules,
646                                      semantic->nr_prefetched_words);
647         }
648     }
649   else
650     {
651       lf_print__this_file_is_empty (file, "generating jump engine");
652     }
653 }
654
655
656 /****************************************************************/
657
658
659 static void
660 gen_icache_h (lf *file,
661               insn_list *semantic,
662               function_entry *functions,
663               int max_nr_words)
664 {
665   int word_nr;
666   for (word_nr = 0; word_nr <= max_nr_words; word_nr++)
667     {
668       lf_printf (file, "typedef ");
669       print_icache_function_type(file);
670       lf_printf (file, " %sidecode_icache_%d\n(",
671                  options.prefix.global.name,
672                  word_nr);
673       print_icache_function_formal(file, word_nr);
674       lf_printf (file, ");\n");
675       lf_printf (file, "\n");
676     }
677   if (options.gen.code == generate_calls
678       && options.gen.icache)
679     {
680       function_entry_traverse (file, functions,
681                                print_icache_internal_function_declaration,
682                                NULL);
683       while (semantic != NULL)
684         {
685           print_icache_declaration (file,
686                                     semantic->insn,
687                                     semantic->expanded_bits,
688                                     semantic->opcodes,
689                                     semantic->nr_prefetched_words);
690           semantic = semantic->next;
691         }
692     }
693   else
694     {
695       lf_print__this_file_is_empty (file, "generating jump engine");
696     }
697 }
698
699 static void
700 gen_icache_c (lf *file,
701               insn_list *semantic,
702               function_entry *functions,
703               cache_entry *cache_rules)
704 {
705   /* output `internal' invalid/floating-point unavailable functions
706      where needed */
707   if (options.gen.code == generate_calls
708       && options.gen.icache)
709     {
710       lf_printf (file, "\n");
711       lf_printf (file, "#include \"cpu.h\"\n");
712       lf_printf (file, "#include \"idecode.h\"\n");
713       lf_printf (file, "#include \"semantics.h\"\n");
714       lf_printf (file, "#include \"icache.h\"\n");
715       lf_printf (file, "#include \"support.h\"\n");
716       lf_printf (file, "\n");
717       function_entry_traverse (file, functions,
718                                print_icache_internal_function_definition,
719                                NULL);
720       lf_printf (file, "\n");
721       while (semantic != NULL)
722         {
723           print_icache_definition (file,
724                                    semantic->insn,
725                                    semantic->expanded_bits,
726                                    semantic->opcodes,
727                                    cache_rules,
728                                    semantic->nr_prefetched_words);
729           semantic = semantic->next;
730         }
731     }
732   else
733     {
734       lf_print__this_file_is_empty (file, "generating jump engine");
735     }
736 }
737
738
739 /****************************************************************/
740
741
742 static void
743 gen_idecode_h (lf *file,
744                gen_table *gen,
745                insn_table *insns,
746                cache_entry *cache_rules)
747 {
748   lf_printf (file, "typedef unsigned%d %sinstruction_word;\n",
749              options.insn_bit_size, options.prefix.global.name);
750   if (options.gen.delayed_branch)
751     {
752       lf_printf (file, "typedef struct _%sinstruction_address {\n",
753                  options.prefix.global.name);
754       lf_printf (file, "  address_word ip; /* instruction pointer */\n");
755       lf_printf (file, "  address_word dp; /* delayed-slot pointer */\n");
756       lf_printf (file, "} %sinstruction_address;\n", options.prefix.global.name);
757     }
758   else
759     {
760       lf_printf (file, "typedef address_word %sinstruction_address;\n",
761                  options.prefix.global.name);
762       
763     }
764   if (options.gen.nia == nia_is_invalid
765       && strlen (options.prefix.global.uname) > 0)
766     {
767       lf_indent_suppress (file);
768       lf_printf (file, "#define %sINVALID_INSTRUCTION_ADDRESS ",
769                  options.prefix.global.uname);
770       lf_printf (file, "INVALID_INSTRUCTION_ADDRESS\n");
771     }
772   lf_printf (file, "\n");
773   print_icache_struct (file, insns, cache_rules);
774   lf_printf (file, "\n");
775   if (options.gen.icache)
776     {
777       ERROR ("FIXME - idecode with icache suffering from bit-rot");
778     }
779   else
780     {
781       gen_list *entry;
782       for (entry = gen->tables; entry != NULL; entry = entry->next)
783         {
784           print_idecode_issue_function_header (file,
785                                                (options.gen.multi_sim
786                                                 ? entry->model->name
787                                                 : NULL),
788                                                is_function_declaration,
789                                                1/*ALWAYS ONE WORD*/);
790         }
791       if (options.gen.multi_sim)
792         {
793           print_idecode_issue_function_header (file,
794                                                NULL,
795                                                is_function_variable,
796                                                1/*ALWAYS ONE WORD*/);
797         }
798     }
799 }
800
801
802 static void
803 gen_idecode_c (lf *file,
804                gen_table *gen,
805                insn_table *isa,
806                cache_entry *cache_rules)
807 {
808   /* the intro */
809   lf_printf (file, "#include \"sim-main.h\"\n");
810   lf_printf (file, "#include \"%sidecode.h\"\n", options.prefix.global.name);
811   lf_printf (file, "#include \"%ssemantics.h\"\n", options.prefix.global.name);
812   lf_printf (file, "#include \"%sicache.h\"\n", options.prefix.global.name);
813   lf_printf (file, "#include \"%ssupport.h\"\n", options.prefix.global.name);
814   lf_printf (file, "\n");
815   lf_printf (file, "\n");
816
817   print_idecode_globals (file);
818   lf_printf (file, "\n");
819   
820   switch (options.gen.code)
821     {
822     case generate_calls:
823       {
824         gen_list *entry;
825         for (entry = gen->tables; entry != NULL; entry = entry->next)
826           {
827             print_idecode_lookups (file, entry->table, cache_rules);
828         
829             /* output the main idecode routine */
830             if (!options.gen.icache)
831               {
832                 print_idecode_issue_function_header (file,
833                                                      (options.gen.multi_sim
834                                                       ? entry->model->name
835                                                       : NULL),
836                                                      1/*is definition*/,
837                                                      1/*ALWAYS ONE WORD*/);
838                 lf_printf (file, "{\n");
839                 lf_indent (file, +2);
840                 lf_printf (file, "%sinstruction_address nia;\n",
841                            options.prefix.global.name);
842                 print_idecode_body (file, entry->table, "nia =");
843                 lf_printf (file, "return nia;");
844                 lf_indent (file, -2);
845                 lf_printf (file, "}\n");
846               }
847           }
848         break;
849       }
850     case generate_jumps:
851       {
852         lf_print__this_file_is_empty (file, "generating a jump engine");
853         break;
854       }
855     }
856 }
857
858
859 /****************************************************************/
860
861
862 static void
863 gen_run_c (lf *file,
864            gen_table *gen)
865 {
866   gen_list *entry;
867   lf_printf (file, "#include \"sim-main.h\"\n");
868   lf_printf (file, "#include \"engine.h\"\n");
869   lf_printf (file, "#include \"idecode.h\"\n");
870   lf_printf (file, "#include \"bfd.h\"\n");
871   lf_printf (file, "\n");
872
873   if (options.gen.multi_sim)
874     {
875       print_idecode_issue_function_header (file, NULL, is_function_variable, 1);
876       lf_printf (file, "\n");
877       print_engine_run_function_header (file, NULL, is_function_variable);
878       lf_printf (file, "\n");
879     }
880   
881   lf_printf (file, "void\n");
882   lf_printf (file, "sim_engine_run (SIM_DESC sd,\n");
883   lf_printf (file, "                int next_cpu_nr,\n");
884   lf_printf (file, "                int nr_cpus,\n");
885   lf_printf (file, "                int siggnal)\n");
886   lf_printf (file, "{\n");
887   lf_indent (file, +2);
888   if (options.gen.multi_sim)
889     {
890       lf_printf (file, "int mach;\n");
891       lf_printf (file, "if (STATE_ARCHITECTURE (sd) == NULL)\n");
892       lf_printf (file, "  mach = 0;\n");
893       lf_printf (file, "else\n");
894       lf_printf (file, "  mach = STATE_ARCHITECTURE (sd)->mach;\n");
895       lf_printf (file, "switch (mach)\n");
896       lf_printf (file, "  {\n");
897       lf_indent (file, +2);
898       for (entry = gen->tables; entry != NULL; entry = entry->next)
899         {
900           if (options.gen.default_model != NULL
901               && (strcmp (entry->model->name, options.gen.default_model) == 0
902                   || strcmp (entry->model->full_name, options.gen.default_model) == 0))
903             lf_printf (file, "default:\n");
904           lf_printf (file, "case bfd_mach_%s:\n", entry->model->full_name);
905           lf_indent (file, +2);
906           print_function_name (file,
907                                "issue",
908                                NULL, /* format name */
909                                NULL, /* NO processor */
910                                NULL, /* expanded bits */
911                                function_name_prefix_idecode);
912           lf_printf (file, " = ");
913           print_function_name (file,
914                                "issue",
915                                NULL, /* format name */
916                                entry->model->name,
917                                NULL, /* expanded bits */
918                                function_name_prefix_idecode);
919           lf_printf (file, ";\n");
920           print_function_name (file,
921                                "run",
922                                NULL, /* format name */
923                                NULL, /* NO processor */
924                                NULL, /* expanded bits */
925                                function_name_prefix_engine);
926           lf_printf (file, " = ");
927           print_function_name (file,
928                                "run",
929                                NULL, /* format name */
930                                entry->model->name,
931                                NULL, /* expanded bits */
932                                function_name_prefix_engine);
933           lf_printf (file, ";\n");
934           lf_printf (file, "break;\n");
935           lf_indent (file, -2);
936         }
937       if (options.gen.default_model == NULL)
938         {
939           lf_printf (file, "default:\n");
940           lf_indent (file, +2);
941           lf_printf (file, "sim_engine_abort (sd, NULL, NULL_CIA,\n");
942           lf_printf (file, "                  \"sim_engine_run - unknown machine\");\n");
943           lf_printf (file, "break;\n");
944           lf_indent (file, -2);
945         }
946       lf_indent (file, -2);
947       lf_printf (file, "  }\n");
948     }
949   print_function_name (file,
950                        "run",
951                        NULL, /* format name */
952                        NULL, /* NO processor */
953                        NULL, /* expanded bits */
954                        function_name_prefix_engine);
955   lf_printf (file, " (sd, next_cpu_nr, nr_cpus, siggnal);\n");
956   lf_indent (file, -2);
957   lf_printf (file, "}\n");
958 }
959
960 /****************************************************************/
961
962 static gen_table *
963 do_gen (insn_table *isa,
964         decode_table *decode_rules)
965 {
966   gen_table *gen;
967   if (decode_rules == NULL)
968     error (NULL, "Must specify a decode table\n");
969   if (isa == NULL)
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);
976   return gen;
977 }
978
979 /****************************************************************/
980
981 igen_options options;
982
983 int
984 main (int argc,
985       char **argv,
986       char **envp)
987 {
988   cache_entry *cache_rules = NULL;
989   lf_file_references file_references = lf_include_references;
990   decode_table *decode_rules = NULL;
991   insn_table *isa = NULL;
992   gen_table *gen = NULL;
993   char *real_file_name = NULL;
994   int is_header = 0;
995   int ch;
996   lf *standard_out = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "igen");
997
998   INIT_OPTIONS (options);
999
1000   if (argc == 1)
1001     {
1002       printf ("Usage:\n");
1003       printf ("\n");
1004       printf ("  igen <config-opts> ... <input-opts>... <output-opts>...\n");
1005       printf ("\n");
1006       printf ("Config options:\n");
1007       printf ("\n");
1008       printf ("  -B <bit-size>\n");
1009       printf ("\t Set the number of bits in an instruction (depreciated).\n");
1010       printf ("\t This option can now be set directly in the instruction table.\n");
1011       printf ("\n");
1012       printf ("  -D <data-structure>\n");
1013       printf ("\t Dump the specified data structure to stdout. Valid structures include:\n");
1014       printf ("\t processor-names - list the names of all the processors (models)\n");
1015       printf ("\n");
1016       printf ("  -F <filter-list>\n");
1017       printf ("\t Filter out any instructions with a non-empty flags field that contains\n");
1018       printf ("\t a flag not listed in the <filter-list>.\n");
1019       printf ("\n");
1020       printf ("  -H <high-bit>\n");
1021       printf ("\t Set the number of the high (most significant) instruction bit (depreciated).\n");
1022       printf ("\t This option can now be set directly in the instruction table.\n");
1023       printf ("\n");
1024       printf ("  -I <directory>\n");
1025       printf ("\t Add <directory> to the list of directories searched when opening a file\n");
1026       printf ("\n");
1027       printf ("  -M <model-list>\n");
1028       printf ("\t Filter out any instructions that do not support at least one of the listed\n");
1029       printf ("\t models (An instructions with no model information is considered to support\n");
1030       printf ("\t all models.).\n");
1031       printf ("\n");
1032       printf ("  -N <nr-cpus>\n");
1033       printf ("\t Generate a simulator supporting <nr-cpus>\n");
1034       printf ("\t Specify `-N 0' to disable generation of the SMP. Specifying `-N 1' will\n");
1035       printf ("\t still generate an SMP enabled simulator but will only support one CPU.\n");
1036       printf ("\n");
1037       printf ("  -T <mechanism>\n");
1038       printf ("\t Override the decode mechanism specified by the decode rules\n");
1039       printf ("\n");
1040       printf ("  -P <prefix>\n");
1041       printf ("\t Prepend global names (except itable) with the string <prefix>.\n");
1042       printf ("\t Specify -P <module>=<prefix> to set the <modules> prefix.\n");
1043       printf ("\n");
1044       printf ("  -Werror\n");
1045       printf ("\t Make warnings errors\n");
1046       printf ("  -Wnodiscard\n");
1047       printf ("\t Suppress warnings about discarded functions and instructions\n");
1048       printf ("  -Wnowidth\n");
1049       printf ("\t Suppress warnings about instructions with invalid widths\n");
1050       printf ("\n");
1051       printf ("  -G [!]<gen-option>\n");
1052       printf ("\t Any of the following options:\n");
1053       printf ("\n");
1054       printf ("\t decode-duplicate       - Override the decode rules, forcing the duplication of\n");
1055       printf ("\t                          semantic functions\n");
1056       printf ("\t decode-combine         - Combine any duplicated entries within a table\n");
1057       printf ("\t decode-zero-reserved   - Override the decode rules, forcing reserved bits to be\n");
1058       printf ("\t                          treated as zero.\n");
1059       printf ("\t decode-switch-is-goto  - Overfide the padded-switch code type as a goto-switch\n");
1060       printf ("\n");
1061       printf ("\t gen-conditional-issue  - conditionally issue each instruction\n");
1062       printf ("\t gen-delayed-branch     - need both cia and nia passed around\n");
1063       printf ("\t gen-direct-access      - use #defines to directly access values\n");
1064       printf ("\t gen-zero-r<N>          - arch assumes GPR(<N>) == 0, keep it that way\n");
1065       printf ("\t gen-icache[=<N>        - generate an instruction cracking cache of size <N>\n");
1066       printf ("\t                          Default size is %d\n", options.gen.icache_size);
1067       printf ("\t gen-insn-in-icache     - save original instruction when cracking\n");
1068       printf ("\t gen-multi-sim[=MODEL]  - generate multiple simulators - one per model\n");
1069       printf ("\t                          If specified MODEL is made the default architecture.\n");
1070       printf ("\t                          By default, a single simulator that will\n");
1071       printf ("\t                          execute any instruction is generated\n");
1072       printf ("\t gen-multi-word         - generate code allowing for multi-word insns\n");
1073       printf ("\t gen-semantic-icache    - include semantic code in cracking functions\n");
1074       printf ("\t gen-slot-verification  - perform slot verification as part of decode\n");
1075       printf ("\t gen-nia-invalid        - NIA defaults to nia_invalid\n");
1076       printf ("\t gen-nia-void           - do not compute/return NIA\n");
1077       printf ("\n");
1078       printf ("\t trace-combine          - report combined entries a rule application\n");
1079       printf ("\t trace-entries          - report entries after a rules application\n");
1080       printf ("\t trace-rule-rejection   - report each rule as rejected\n");
1081       printf ("\t trace-rule-selection   - report each rule as selected\n");
1082       printf ("\n");
1083       printf ("\t field-widths          - instruction formats specify widths (depreciated)\n");
1084       printf ("\t                         By default, an instruction format specifies bit\n");
1085       printf ("\t                         positions\n");
1086       printf ("\t                         This option can now be set directly in the\n");
1087       printf ("\t                         instruction table\n");
1088       printf ("\t jumps                 - use jumps instead of function calls\n");
1089       printf ("\t omit-line-numbers     - do not include line number information in the output\n");
1090       printf ("\n");
1091       printf ("Input options:\n");
1092       printf ("\n");
1093       printf ("  -k <cache-rules> (depreciated)\n");
1094       printf ("  -o <decode-rules>\n");
1095       printf ("  -i <instruction-table>\n");
1096       printf ("\n");
1097       printf ("Output options:\n");
1098       printf ("\n");
1099       printf ("  -x                    Perform expansion (required)\n");
1100       printf ("  -n <real-name>        Specify the real name of the next output file\n"); 
1101       printf ("  -h                    Generate the header (.h) file rather than the body (.c)\n");
1102       printf ("  -c <output-file>      output icache\n");
1103       printf ("  -d <output-file>      output idecode\n");
1104       printf ("  -e <output-file>      output engine\n");
1105       printf ("  -f <output-file>      output support functions\n");
1106       printf ("  -m <output-file>      output model\n");
1107       printf ("  -r <output-file>      output multi-sim run\n");
1108       printf ("  -s <output-file>      output schematic\n");
1109       printf ("  -t <output-file>      output itable\n");
1110     }
1111   
1112   while ((ch = getopt(argc, argv,
1113                       "B:D:F:G:H:I:M:N:P:T:W:o:k:i:n:hc:d:e:m:r:s:t:f:x"))
1114          != -1)
1115     {
1116       fprintf (stderr, "  -%c ", ch);
1117       if (optarg)
1118         fprintf (stderr, "%s ", optarg);
1119       fprintf (stderr, "\\\n");
1120       
1121       switch(ch)
1122         {
1123           
1124         case 'M':
1125           filter_parse (&options.model_filter, optarg);
1126           break;
1127
1128         case 'D':
1129           if (strcmp (optarg, "processor-names"))
1130             {
1131               char *processor;
1132               for (processor = filter_next (options.model_filter, "");
1133                    processor != NULL;
1134                    processor = filter_next (options.model_filter, processor))
1135                 lf_printf (standard_out, "%s\n", processor);
1136             }
1137           else
1138             error (NULL, "Unknown data structure %s, not dumped\n", optarg);
1139           break;
1140
1141         case 'F':
1142           filter_parse (&options.flags_filter, optarg);
1143           break;
1144           
1145         case 'I':
1146           {
1147             table_include **dir = &options.include;
1148             while ((*dir) != NULL)
1149               dir = &(*dir)->next;
1150             (*dir) = ZALLOC (table_include);
1151             (*dir)->dir = strdup (optarg);
1152           }
1153           break;
1154           
1155         case 'B':
1156           options.insn_bit_size = a2i (optarg);
1157           if (options.insn_bit_size <= 0
1158               || options.insn_bit_size > max_insn_bit_size)
1159             {
1160               error (NULL, "Instruction bitsize must be in range 1..%d\n",
1161                      max_insn_bit_size);
1162             }
1163           if (options.hi_bit_nr != options.insn_bit_size - 1
1164               && options.hi_bit_nr != 0)
1165             {
1166               error (NULL, "Conflict betweem hi-bit-nr and insn-bit-size\n");
1167             }
1168           break;
1169           
1170         case 'H':
1171           options.hi_bit_nr = a2i (optarg);
1172           if (options.hi_bit_nr != options.insn_bit_size - 1
1173               && options.hi_bit_nr != 0)
1174             {
1175               error (NULL, "Conflict between hi-bit-nr and insn-bit-size\n");
1176             }
1177           break;
1178           
1179         case 'N':
1180           options.gen.smp = a2i (optarg);
1181           break;
1182           
1183         case 'P':
1184           {
1185             igen_prefix_name *names;
1186             char *chp;
1187             chp = strchr (optarg, '=');
1188             if (chp == NULL)
1189               {
1190                 names = &options.prefix.global;
1191                 chp = optarg;
1192               }
1193             else
1194               {
1195                 chp = chp + 1; /* skip `=' */
1196                 if (strncmp (optarg, "global=", chp - optarg) == 0)
1197                   {
1198                     names = &options.prefix.global;
1199                   }
1200                 if (strncmp (optarg, "engine=", chp - optarg) == 0)
1201                   {
1202                     names = &options.prefix.engine;
1203                   }
1204                 if (strncmp (optarg, "icache=", chp - optarg) == 0)
1205                   {
1206                     names = &options.prefix.icache;
1207                   }
1208                 if (strncmp (optarg, "idecode=", chp - optarg) == 0)
1209                   {
1210                     names = &options.prefix.idecode;
1211                   }
1212                 if (strncmp (optarg, "itable=", chp - optarg) == 0)
1213                   {
1214                     names = &options.prefix.itable;
1215                   }
1216                 if (strncmp (optarg, "semantics=", chp - optarg) == 0)
1217                   {
1218                     names = &options.prefix.semantics;
1219                   }
1220                 if (strncmp (optarg, "support=", chp - optarg) == 0)
1221                   {
1222                     names = &options.prefix.support;
1223                   }
1224                 else
1225                   {
1226                     names = NULL;
1227                     error (NULL, "Prefix `%s' unreconized\n", optarg);
1228                   }
1229               }
1230             names->name = strdup (chp);
1231             names->uname = strdup (chp);
1232             chp = names->uname;
1233             while (*chp) {
1234               if (islower(*chp))
1235                 *chp = toupper(*chp);
1236               chp++;
1237             }
1238             if (names == &options.prefix.global)
1239               {
1240                 options.prefix.engine = options.prefix.global;
1241                 options.prefix.icache = options.prefix.global;
1242                 options.prefix.idecode = options.prefix.global;
1243                 /* options.prefix.itable = options.prefix.global; */
1244                 options.prefix.semantics = options.prefix.global;
1245                 options.prefix.support = options.prefix.global;
1246               }
1247             break;
1248           }
1249         
1250         case 'W':
1251           {
1252             if (strcmp (optarg, "error") == 0)
1253               options.warning = error;
1254             else if (strcmp (optarg, "nodiscard") == 0)
1255               options.warn.discard = 0;
1256             else if (strcmp (optarg, "discard") == 0)
1257               options.warn.discard = 1;
1258             else if (strcmp (optarg, "nowidth") == 0)
1259               options.warn.width = 0;
1260             else if (strcmp (optarg, "width") == 0)
1261               options.warn.width = 1;
1262             else
1263               error (NULL, "Unknown -W argument `%s'\n", optarg);
1264             break;
1265           }
1266
1267
1268         case 'G':
1269           {
1270             int enable_p;
1271             char *argp;
1272             if (strncmp (optarg, "no-", strlen ("no-")) == 0)
1273               {
1274                 argp = optarg + strlen ("no-");
1275                 enable_p = 0;
1276               }
1277             else if (strncmp (optarg, "!", strlen ("!")) == 0)
1278               {
1279                 argp = optarg + strlen ("no-");
1280                 enable_p = 0;
1281               }
1282             else
1283               {
1284                 argp = optarg;
1285                 enable_p = 1;
1286               }
1287             if (strcmp (argp, "decode-duplicate") == 0)
1288               {
1289                 options.decode.duplicate = enable_p;
1290               }
1291             else if (strcmp (argp, "decode-combine") == 0)
1292               {
1293                 options.decode.combine = enable_p;
1294               }
1295             else if (strcmp (argp, "decode-zero-reserved") == 0)
1296               {
1297                 options.decode.zero_reserved = enable_p;
1298               }
1299             
1300             else if (strcmp (argp, "gen-conditional-issue") == 0)
1301               {
1302                 options.gen.conditional_issue = enable_p;
1303               }
1304             else if (strcmp (argp, "conditional-issue") == 0)
1305               {
1306                 options.gen.conditional_issue = enable_p;
1307                 options.warning (NULL, "Option conditional-issue replaced by gen-conditional-issue\n");
1308               }
1309             else if (strcmp (argp, "gen-delayed-branch") == 0)
1310               {
1311                 options.gen.delayed_branch = enable_p;
1312               }
1313             else if (strcmp (argp, "delayed-branch") == 0)
1314               {
1315                 options.gen.delayed_branch = enable_p;
1316                 options.warning (NULL, "Option delayed-branch replaced by gen-delayed-branch\n");
1317               }
1318             else if (strcmp (argp, "gen-direct-access") == 0)
1319               {
1320                 options.gen.direct_access = enable_p;
1321               }
1322             else if (strcmp (argp, "direct-access") == 0)
1323               {
1324                 options.gen.direct_access = enable_p;
1325                 options.warning (NULL, "Option direct-access replaced by gen-direct-access\n");
1326               }
1327             else if (strncmp (argp, "gen-zero-r", strlen ("gen-zero-r")) == 0)
1328               {
1329                 options.gen.zero_reg = enable_p;
1330                 options.gen.zero_reg_nr = atoi (argp + strlen ("gen-zero-r"));
1331               }
1332             else if (strncmp (argp, "zero-r", strlen ("zero-r")) == 0)
1333               {
1334                 options.gen.zero_reg = enable_p;
1335                 options.gen.zero_reg_nr = atoi (argp + strlen ("zero-r"));
1336                 options.warning (NULL, "Option zero-r<N> replaced by gen-zero-r<N>\n");
1337               }
1338             else if (strncmp (argp, "gen-icache", strlen ("gen-icache")) == 0)
1339               {
1340                 switch (argp[strlen ("gen-icache")])
1341                   {
1342                   case '=':
1343                     options.gen.icache_size = atoi (argp + strlen ("gen-icache") + 1);
1344                     options.gen.icache = enable_p;
1345                     break;
1346                   case '\0':
1347                     options.gen.icache = enable_p;
1348                     break;
1349                   default:
1350                     error (NULL, "Expecting -Ggen-icache or -Ggen-icache=<N>\n");
1351                   }
1352               }
1353             else if (strcmp (argp, "gen-insn-in-icache") == 0)
1354               {
1355                 options.gen.insn_in_icache = enable_p;
1356               }
1357             else if (strncmp (argp, "gen-multi-sim", strlen ("gen-multi-sim")) == 0)
1358               {
1359                 char *arg = &argp[strlen ("gen-multi-sim")];
1360                 switch (arg[0])
1361                   {
1362                   case '=':
1363                     options.gen.multi_sim = enable_p;
1364                     options.gen.default_model = arg + 1;
1365                     if (! filter_is_member (options.model_filter, options.gen.default_model))
1366                       error (NULL, "multi-sim model %s unknown\n", options.gen.default_model);
1367                     break;
1368                   case '\0':
1369                     options.gen.multi_sim = enable_p;
1370                     options.gen.default_model = NULL;
1371                     break;
1372                   default:
1373                     error (NULL, "Expecting -Ggen-multi-sim or -Ggen-multi-sim=<MODEL>\n");
1374                     break;
1375                   }
1376               }
1377             else if (strcmp (argp, "gen-multi-word") == 0)
1378               {
1379                 options.gen.multi_word = enable_p;
1380               }
1381             else if (strcmp (argp, "gen-semantic-icache") == 0)
1382               {
1383                 options.gen.semantic_icache = enable_p;
1384               }
1385             else if (strcmp (argp, "gen-slot-verification") == 0)
1386               {
1387                 options.gen.slot_verification = enable_p;
1388               }
1389             else if (strcmp (argp, "verify-slot") == 0)
1390               {
1391                 options.gen.slot_verification = enable_p;
1392                 options.warning (NULL, "Option verify-slot replaced by gen-slot-verification\n");
1393               }
1394             else if (strcmp (argp, "gen-nia-invalid") == 0)
1395               {
1396                 options.gen.nia = nia_is_invalid;
1397               }
1398             else if (strcmp (argp, "default-nia-minus-one") == 0)
1399               {
1400                 options.gen.nia = nia_is_invalid;
1401                 options.warning (NULL, "Option default-nia-minus-one replaced by gen-nia-invalid\n");
1402               }
1403             else if (strcmp (argp, "gen-nia-void") == 0)
1404               {
1405                 options.gen.nia = nia_is_void;
1406               }
1407             else if (strcmp (argp, "trace-combine") == 0)
1408               {
1409                 options.trace.combine = enable_p;
1410               }
1411             else if (strcmp (argp, "trace-entries") == 0)
1412               {
1413                 options.trace.entries = enable_p;
1414               }
1415             else if (strcmp (argp, "trace-rule-rejection") == 0)
1416               {
1417                 options.trace.rule_rejection = enable_p;
1418               }
1419             else if (strcmp (argp, "trace-rule-selection") == 0)
1420               {
1421                 options.trace.rule_selection = enable_p;
1422               }
1423             else if (strcmp (argp, "jumps") == 0)
1424               {
1425                 options.gen.code = generate_jumps;
1426               }
1427             else if (strcmp (argp, "field-widths") == 0)
1428               {
1429                 options.insn_specifying_widths = enable_p;
1430               }
1431             else if (strcmp (argp, "omit-line-numbers") == 0)
1432               {
1433                 file_references = lf_omit_references;
1434               }
1435             else
1436               {
1437                 error (NULL, "Unknown option %s\n", optarg);
1438               }
1439             break;
1440           }
1441         
1442         case 'i':
1443           isa = load_insn_table (optarg, cache_rules);
1444           if (isa->illegal_insn == NULL)
1445             error (NULL, "illegal-instruction missing from insn table\n");
1446           break;
1447
1448         case 'x':
1449           gen = do_gen (isa, decode_rules);
1450           break;
1451
1452         case 'o':
1453           decode_rules = load_decode_table (optarg);
1454           break;
1455
1456         case 'k':
1457           if (isa != NULL)
1458             error (NULL, "Cache file must appear before the insn file\n");
1459           cache_rules = load_cache_table (optarg);
1460           break;
1461
1462         case 'n':
1463           real_file_name = strdup(optarg);
1464           break;
1465
1466         case 'h':
1467           is_header = 1;
1468           break;
1469           
1470         case 'c':
1471         case 'd':
1472         case 'e':
1473         case 'f':
1474         case 'm':
1475         case 'r':
1476         case 's':
1477         case 't':
1478           {
1479             lf *file = lf_open(optarg, real_file_name, file_references,
1480                                (is_header ? lf_is_h : lf_is_c),
1481                                argv[0]);
1482             if (gen == NULL && ch != 't' && ch != 'm' && ch != 'f')
1483               {
1484                 options.warning (NULL, "Explicitly generate tables with -x option\n");
1485                 gen = do_gen (isa, decode_rules);
1486               }
1487             lf_print__file_start(file);
1488             switch (ch)
1489               {
1490               case 'm':
1491                 if (is_header)
1492                   gen_model_h (file, isa);
1493                 else
1494                   gen_model_c (file, isa);
1495                 break;
1496               case 't':
1497                 if (is_header)
1498                   gen_itable_h (file, isa);
1499                 else
1500                   gen_itable_c (file, isa);
1501                 break;
1502               case 'f':
1503                 if (is_header)
1504                   gen_support_h (file, isa);
1505                 else
1506                   gen_support_c (file, isa);
1507                 break;
1508               case 'r':
1509                 if (is_header)
1510                   options.warning (NULL, "-hr option ignored\n");
1511                 else
1512                   gen_run_c (file, gen);
1513                 break;
1514               case 's':
1515                 if(is_header)
1516                   gen_semantics_h (file, gen->semantics, isa->max_nr_words);
1517                 else
1518                   gen_semantics_c (file, gen->semantics, isa->caches);
1519                 break;
1520               case 'd':
1521                 if (is_header)
1522                   gen_idecode_h (file, gen, isa, cache_rules);
1523                 else
1524                   gen_idecode_c (file, gen, isa, cache_rules);
1525                 break;
1526               case 'e':
1527                 if (is_header)
1528                   gen_engine_h (file, gen, isa, cache_rules);
1529                 else
1530                   gen_engine_c (file, gen, isa, cache_rules);
1531                 break;
1532               case 'c':
1533                 if (is_header)
1534                   gen_icache_h (file,
1535                                 gen->semantics,
1536                                 isa->functions,
1537                                 isa->max_nr_words);
1538                 else
1539                   gen_icache_c (file,
1540                                 gen->semantics,
1541                                 isa->functions,
1542                                 cache_rules);
1543                 break;
1544               }
1545             lf_print__file_finish(file);
1546             lf_close(file);
1547             is_header = 0;
1548           }
1549         real_file_name = NULL;
1550         break;
1551         default:
1552           ERROR ("Bad switch");
1553         }
1554     }
1555   return (0);
1556 }