Copyright updates for 2007.
[external/binutils.git] / sim / igen / gen-model.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3    Copyright 2002, 2007 Free Software Foundation, Inc.
4
5    Contributed by Andrew Cagney.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24
25 #include "misc.h"
26 #include "lf.h"
27 #include "table.h"
28
29 #include "filter.h"
30
31 #include "ld-decode.h"
32 #include "ld-insn.h"
33
34 #include "gen-model.h"
35
36 #ifndef NULL
37 #define NULL 0
38 #endif
39
40
41 #if 0
42 static void
43 model_c_or_h_data (insn_table *table, lf *file, table_entry *data)
44 {
45   if (data->annex)
46     {
47       table_entry_print_cpp_line_nr (file, data->annex_line);
48       lf_print__c_code (file, data->annex);
49       lf_print__internal_reference (file);
50       lf_printf (file, "\n");
51     }
52 }
53
54 static void
55 model_c_or_h_function (insn_table *entry,
56                        lf *file, table_entry *function, char *prefix)
57 {
58   if (function->fields[function_type] == NULL
59       || function->fields[function_type][0] == '\0')
60     {
61       error ("Model function type not specified for %s",
62              function->fields[function_name]);
63     }
64   lf_printf (file, "\n");
65   lf_print_function_type (file, function->fields[function_type], prefix, " ");
66   lf_printf (file, "%s\n(%s);\n",
67              function->fields[function_name],
68              function->fields[function_param]);
69   lf_printf (file, "\n");
70 }
71
72 void
73 gen_model_h (insn_table *table, lf *file)
74 {
75   insn *insn_ptr;
76   model *model_ptr;
77   insn *macro;
78   char *name;
79   int model_create_p = 0;
80   int model_init_p = 0;
81   int model_halt_p = 0;
82   int model_mon_info_p = 0;
83   int model_mon_info_free_p = 0;
84
85   for (macro = model_macros; macro; macro = macro->next)
86     {
87       model_c_or_h_data (table, file, macro->file_entry);
88     }
89
90   lf_printf (file, "typedef enum _model_enum {\n");
91   lf_printf (file, "  MODEL_NONE,\n");
92   for (model_ptr = models; model_ptr; model_ptr = model_ptr->next)
93     {
94       lf_printf (file, "  MODEL_%s,\n", model_ptr->name);
95     }
96   lf_printf (file, "  nr_models\n");
97   lf_printf (file, "} model_enum;\n");
98   lf_printf (file, "\n");
99
100   lf_printf (file, "#define DEFAULT_MODEL MODEL_%s\n",
101              (models) ? models->name : "NONE");
102   lf_printf (file, "\n");
103
104   lf_printf (file, "typedef struct _model_data model_data;\n");
105   lf_printf (file, "typedef struct _model_time model_time;\n");
106   lf_printf (file, "\n");
107
108   lf_printf (file, "extern model_enum current_model;\n");
109   lf_printf (file, "extern const char *model_name[ (int)nr_models ];\n");
110   lf_printf (file,
111              "extern const char *const *const model_func_unit_name[ (int)nr_models ];\n");
112   lf_printf (file,
113              "extern const model_time *const model_time_mapping[ (int)nr_models ];\n");
114   lf_printf (file, "\n");
115
116   for (insn_ptr = model_functions; insn_ptr; insn_ptr = insn_ptr->next)
117     {
118       model_c_or_h_function (table, file, insn_ptr->file_entry,
119                              "INLINE_MODEL");
120       name = insn_ptr->file_entry->fields[function_name];
121       if (strcmp (name, "model_create") == 0)
122         model_create_p = 1;
123       else if (strcmp (name, "model_init") == 0)
124         model_init_p = 1;
125       else if (strcmp (name, "model_halt") == 0)
126         model_halt_p = 1;
127       else if (strcmp (name, "model_mon_info") == 0)
128         model_mon_info_p = 1;
129       else if (strcmp (name, "model_mon_info_free") == 0)
130         model_mon_info_free_p = 1;
131     }
132
133   if (!model_create_p)
134     {
135       lf_print_function_type (file, "model_data *", "INLINE_MODEL", " ");
136       lf_printf (file, "model_create\n");
137       lf_printf (file, "(sim_cpu *cpu);\n");
138       lf_printf (file, "\n");
139     }
140
141   if (!model_init_p)
142     {
143       lf_print_function_type (file, "void", "INLINE_MODEL", " ");
144       lf_printf (file, "model_init\n");
145       lf_printf (file, "(model_data *model_ptr);\n");
146       lf_printf (file, "\n");
147     }
148
149   if (!model_halt_p)
150     {
151       lf_print_function_type (file, "void", "INLINE_MODEL", " ");
152       lf_printf (file, "model_halt\n");
153       lf_printf (file, "(model_data *model_ptr);\n");
154       lf_printf (file, "\n");
155     }
156
157   if (!model_mon_info_p)
158     {
159       lf_print_function_type (file, "model_print *", "INLINE_MODEL", " ");
160       lf_printf (file, "model_mon_info\n");
161       lf_printf (file, "(model_data *model_ptr);\n");
162       lf_printf (file, "\n");
163     }
164
165   if (!model_mon_info_free_p)
166     {
167       lf_print_function_type (file, "void", "INLINE_MODEL", " ");
168       lf_printf (file, "model_mon_info_free\n");
169       lf_printf (file, "(model_data *model_ptr,\n");
170       lf_printf (file, " model_print *info_ptr);\n");
171       lf_printf (file, "\n");
172     }
173
174   lf_print_function_type (file, "void", "INLINE_MODEL", " ");
175   lf_printf (file, "model_set\n");
176   lf_printf (file, "(const char *name);\n");
177 }
178
179 /****************************************************************/
180
181 typedef struct _model_c_passed_data model_c_passed_data;
182 struct _model_c_passed_data
183 {
184   lf *file;
185   model *model_ptr;
186 };
187
188 static void
189 model_c_insn (insn_table *entry,
190               lf *phony_file, void *data, insn * instruction, int depth)
191 {
192   model_c_passed_data *data_ptr = (model_c_passed_data *) data;
193   lf *file = data_ptr->file;
194   char *current_name = data_ptr->model_ptr->printable_name;
195   table_model_entry *model_ptr = instruction->file_entry->model_first;
196
197   while (model_ptr)
198     {
199       if (model_ptr->fields[insn_model_name] == current_name)
200         {
201           lf_printf (file, "  { %-*s },  /* %s */\n",
202                      max_model_fields_len,
203                      model_ptr->fields[insn_model_fields],
204                      instruction->file_entry->fields[insn_name]);
205           return;
206         }
207
208       model_ptr = model_ptr->next;
209     }
210
211   lf_printf (file, "  { %-*s },  /* %s */\n",
212              max_model_fields_len,
213              data_ptr->model_ptr->insn_default,
214              instruction->file_entry->fields[insn_name]);
215 }
216
217 static void
218 model_c_function (insn_table *table,
219                   lf *file, table_entry *function, const char *prefix)
220 {
221   if (function->fields[function_type] == NULL
222       || function->fields[function_type][0] == '\0')
223     {
224       error ("Model function return type not specified for %s",
225              function->fields[function_name]);
226     }
227   else
228     {
229       lf_printf (file, "\n");
230       lf_print_function_type (file, function->fields[function_type], prefix,
231                               "\n");
232       lf_printf (file, "%s(%s)\n", function->fields[function_name],
233                  function->fields[function_param]);
234     }
235   lf_printf (file, "{\n");
236   if (function->annex)
237     {
238       lf_indent (file, +2);
239       table_entry_print_cpp_line_nr (file, function->annex_line);
240       lf_print__c_code (file, function->annex);
241       lf_indent (file, -2);
242     }
243   lf_printf (file, "}\n");
244   lf_print__internal_reference (file);
245   lf_printf (file, "\n");
246 }
247
248 void
249 gen_model_c (insn_table *table, lf *file)
250 {
251   insn *insn_ptr;
252   model *model_ptr;
253   char *name;
254   int model_create_p = 0;
255   int model_init_p = 0;
256   int model_halt_p = 0;
257   int model_mon_info_p = 0;
258   int model_mon_info_free_p = 0;
259
260   lf_printf (file, "\n");
261   lf_printf (file, "#include \"cpu.h\"\n");
262   lf_printf (file, "#include \"mon.h\"\n");
263   lf_printf (file, "\n");
264   lf_printf (file, "#ifdef HAVE_STDLIB_H\n");
265   lf_printf (file, "#include <stdlib.h>\n");
266   lf_printf (file, "#endif\n");
267   lf_printf (file, "\n");
268
269   for (insn_ptr = model_data; insn_ptr; insn_ptr = insn_ptr->next)
270     {
271       model_c_or_h_data (table, file, insn_ptr->file_entry);
272     }
273
274   for (insn_ptr = model_static; insn_ptr; insn_ptr = insn_ptr->next)
275     {
276       model_c_or_h_function (table, file, insn_ptr->file_entry,
277                              "/*h*/STATIC");
278     }
279
280   for (insn_ptr = model_internal; insn_ptr; insn_ptr = insn_ptr->next)
281     {
282       model_c_or_h_function (table, file, insn_ptr->file_entry,
283                              "STATIC_INLINE_MODEL");
284     }
285
286   for (insn_ptr = model_static; insn_ptr; insn_ptr = insn_ptr->next)
287     {
288       model_c_function (table, file, insn_ptr->file_entry, "/*c*/STATIC");
289     }
290
291   for (insn_ptr = model_internal; insn_ptr; insn_ptr = insn_ptr->next)
292     {
293       model_c_function (table, file, insn_ptr->file_entry,
294                         "STATIC_INLINE_MODEL");
295     }
296
297   for (insn_ptr = model_functions; insn_ptr; insn_ptr = insn_ptr->next)
298     {
299       model_c_function (table, file, insn_ptr->file_entry, "INLINE_MODEL");
300       name = insn_ptr->file_entry->fields[function_name];
301       if (strcmp (name, "model_create") == 0)
302         model_create_p = 1;
303       else if (strcmp (name, "model_init") == 0)
304         model_init_p = 1;
305       else if (strcmp (name, "model_halt") == 0)
306         model_halt_p = 1;
307       else if (strcmp (name, "model_mon_info") == 0)
308         model_mon_info_p = 1;
309       else if (strcmp (name, "model_mon_info_free") == 0)
310         model_mon_info_free_p = 1;
311     }
312
313   if (!model_create_p)
314     {
315       lf_print_function_type (file, "model_data *", "INLINE_MODEL", "\n");
316       lf_printf (file, "model_create(sim_cpu *cpu)\n");
317       lf_printf (file, "{\n");
318       lf_printf (file, "  return (model_data *)0;\n");
319       lf_printf (file, "}\n");
320       lf_printf (file, "\n");
321     }
322
323   if (!model_init_p)
324     {
325       lf_print_function_type (file, "void", "INLINE_MODEL", "\n");
326       lf_printf (file, "model_init(model_data *model_ptr)\n");
327       lf_printf (file, "{\n");
328       lf_printf (file, "}\n");
329       lf_printf (file, "\n");
330     }
331
332   if (!model_halt_p)
333     {
334       lf_print_function_type (file, "void", "INLINE_MODEL", "\n");
335       lf_printf (file, "model_halt(model_data *model_ptr)\n");
336       lf_printf (file, "{\n");
337       lf_printf (file, "}\n");
338       lf_printf (file, "\n");
339     }
340
341   if (!model_mon_info_p)
342     {
343       lf_print_function_type (file, "model_print *", "INLINE_MODEL", "\n");
344       lf_printf (file, "model_mon_info(model_data *model_ptr)\n");
345       lf_printf (file, "{\n");
346       lf_printf (file, "  return (model_print *)0;\n");
347       lf_printf (file, "}\n");
348       lf_printf (file, "\n");
349     }
350
351   if (!model_mon_info_free_p)
352     {
353       lf_print_function_type (file, "void", "INLINE_MODEL", "\n");
354       lf_printf (file, "model_mon_info_free(model_data *model_ptr,\n");
355       lf_printf (file, "                    model_print *info_ptr)\n");
356       lf_printf (file, "{\n");
357       lf_printf (file, "}\n");
358       lf_printf (file, "\n");
359     }
360
361   lf_printf (file, "/* Insn functional unit info */\n");
362   for (model_ptr = models; model_ptr; model_ptr = model_ptr->next)
363     {
364       model_c_passed_data data;
365
366       lf_printf (file, "static const model_time model_time_%s[] = {\n",
367                  model_ptr->name);
368       data.file = file;
369       data.model_ptr = model_ptr;
370       insn_table_traverse_insn (table, NULL, (void *) &data, model_c_insn);
371
372       lf_printf (file, "};\n");
373       lf_printf (file, "\n");
374       lf_printf (file, "\f\n");
375     }
376
377   lf_printf (file, "#ifndef _INLINE_C_\n");
378   lf_printf (file,
379              "const model_time *const model_time_mapping[ (int)nr_models ] = {\n");
380   lf_printf (file, "  (const model_time *const)0,\n");
381   for (model_ptr = models; model_ptr; model_ptr = model_ptr->next)
382     {
383       lf_printf (file, "  model_time_%s,\n", model_ptr->name);
384     }
385   lf_printf (file, "};\n");
386   lf_printf (file, "#endif\n");
387   lf_printf (file, "\n");
388
389   lf_printf (file, "\f\n");
390   lf_printf (file, "/* map model enumeration into printable string */\n");
391   lf_printf (file, "#ifndef _INLINE_C_\n");
392   lf_printf (file, "const char *model_name[ (int)nr_models ] = {\n");
393   lf_printf (file, "  \"NONE\",\n");
394   for (model_ptr = models; model_ptr; model_ptr = model_ptr->next)
395     {
396       lf_printf (file, "  \"%s\",\n", model_ptr->printable_name);
397     }
398   lf_printf (file, "};\n");
399   lf_printf (file, "#endif\n");
400   lf_printf (file, "\n");
401
402   lf_print_function_type (file, "void", "INLINE_MODEL", "\n");
403   lf_printf (file, "model_set(const char *name)\n");
404   lf_printf (file, "{\n");
405   if (models)
406     {
407       lf_printf (file, "  model_enum model;\n");
408       lf_printf (file,
409                  "  for(model = MODEL_%s; model < nr_models; model++) {\n",
410                  models->name);
411       lf_printf (file, "    if(strcmp(name, model_name[model]) == 0) {\n");
412       lf_printf (file, "      current_model = model;\n");
413       lf_printf (file, "      return;\n");
414       lf_printf (file, "    }\n");
415       lf_printf (file, "  }\n");
416       lf_printf (file, "\n");
417       lf_printf (file,
418                  "  error(\"Unknown model '%%s', Models which are known are:%%s\n\",\n");
419       lf_printf (file, "        name,\n");
420       lf_printf (file, "        \"");
421       for (model_ptr = models; model_ptr; model_ptr = model_ptr->next)
422         {
423           lf_printf (file, "\\n\\t%s", model_ptr->printable_name);
424         }
425       lf_printf (file, "\");\n");
426     }
427   else
428     {
429       lf_printf (file, "  error(\"No models are currently known about\");\n");
430     }
431
432   lf_printf (file, "}\n");
433 }
434
435 #endif
436
437
438
439 void
440 gen_model_h (lf *file, insn_table *table)
441 {
442   lf_print__this_file_is_empty (file, "suffering bit rot");
443 }
444
445
446 void
447 gen_model_c (lf *file, insn_table *table)
448 {
449   lf_print__this_file_is_empty (file, "suffering bit rot");
450 }