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