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