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