1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002, 2007, 2008 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
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.
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.
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/>. */
29 #include "ld-decode.h"
32 #include "gen-model.h"
41 model_c_or_h_data (insn_table
*table
, lf
*file
, table_entry
*data
)
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");
53 model_c_or_h_function (insn_table
*entry
,
54 lf
*file
, table_entry
*function
, char *prefix
)
56 if (function
->fields
[function_type
] == NULL
57 || function
->fields
[function_type
][0] == '\0')
59 error ("Model function type not specified for %s",
60 function
->fields
[function_name
]);
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");
71 gen_model_h (insn_table
*table
, lf
*file
)
77 int model_create_p
= 0;
80 int model_mon_info_p
= 0;
81 int model_mon_info_free_p
= 0;
83 for (macro
= model_macros
; macro
; macro
= macro
->next
)
85 model_c_or_h_data (table
, file
, macro
->file_entry
);
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
)
92 lf_printf (file
, " MODEL_%s,\n", model_ptr
->name
);
94 lf_printf (file
, " nr_models\n");
95 lf_printf (file
, "} model_enum;\n");
96 lf_printf (file
, "\n");
98 lf_printf (file
, "#define DEFAULT_MODEL MODEL_%s\n",
99 (models
) ? models
->name
: "NONE");
100 lf_printf (file
, "\n");
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");
106 lf_printf (file
, "extern model_enum current_model;\n");
107 lf_printf (file
, "extern const char *model_name[ (int)nr_models ];\n");
109 "extern const char *const *const model_func_unit_name[ (int)nr_models ];\n");
111 "extern const model_time *const model_time_mapping[ (int)nr_models ];\n");
112 lf_printf (file
, "\n");
114 for (insn_ptr
= model_functions
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
116 model_c_or_h_function (table
, file
, insn_ptr
->file_entry
,
118 name
= insn_ptr
->file_entry
->fields
[function_name
];
119 if (strcmp (name
, "model_create") == 0)
121 else if (strcmp (name
, "model_init") == 0)
123 else if (strcmp (name
, "model_halt") == 0)
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;
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");
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");
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");
155 if (!model_mon_info_p
)
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");
163 if (!model_mon_info_free_p
)
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");
172 lf_print_function_type (file
, "void", "INLINE_MODEL", " ");
173 lf_printf (file
, "model_set\n");
174 lf_printf (file
, "(const char *name);\n");
177 /****************************************************************/
179 typedef struct _model_c_passed_data model_c_passed_data
;
180 struct _model_c_passed_data
187 model_c_insn (insn_table
*entry
,
188 lf
*phony_file
, void *data
, insn
* instruction
, int depth
)
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
;
197 if (model_ptr
->fields
[insn_model_name
] == current_name
)
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
]);
206 model_ptr
= model_ptr
->next
;
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
]);
216 model_c_function (insn_table
*table
,
217 lf
*file
, table_entry
*function
, const char *prefix
)
219 if (function
->fields
[function_type
] == NULL
220 || function
->fields
[function_type
][0] == '\0')
222 error ("Model function return type not specified for %s",
223 function
->fields
[function_name
]);
227 lf_printf (file
, "\n");
228 lf_print_function_type (file
, function
->fields
[function_type
], prefix
,
230 lf_printf (file
, "%s(%s)\n", function
->fields
[function_name
],
231 function
->fields
[function_param
]);
233 lf_printf (file
, "{\n");
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);
241 lf_printf (file
, "}\n");
242 lf_print__internal_reference (file
);
243 lf_printf (file
, "\n");
247 gen_model_c (insn_table
*table
, lf
*file
)
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;
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");
267 for (insn_ptr
= model_data
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
269 model_c_or_h_data (table
, file
, insn_ptr
->file_entry
);
272 for (insn_ptr
= model_static
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
274 model_c_or_h_function (table
, file
, insn_ptr
->file_entry
,
278 for (insn_ptr
= model_internal
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
280 model_c_or_h_function (table
, file
, insn_ptr
->file_entry
,
281 "STATIC_INLINE_MODEL");
284 for (insn_ptr
= model_static
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
286 model_c_function (table
, file
, insn_ptr
->file_entry
, "/*c*/STATIC");
289 for (insn_ptr
= model_internal
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
291 model_c_function (table
, file
, insn_ptr
->file_entry
,
292 "STATIC_INLINE_MODEL");
295 for (insn_ptr
= model_functions
; insn_ptr
; insn_ptr
= insn_ptr
->next
)
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)
301 else if (strcmp (name
, "model_init") == 0)
303 else if (strcmp (name
, "model_halt") == 0)
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;
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");
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");
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");
339 if (!model_mon_info_p
)
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");
349 if (!model_mon_info_free_p
)
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");
359 lf_printf (file
, "/* Insn functional unit info */\n");
360 for (model_ptr
= models
; model_ptr
; model_ptr
= model_ptr
->next
)
362 model_c_passed_data data
;
364 lf_printf (file
, "static const model_time model_time_%s[] = {\n",
367 data
.model_ptr
= model_ptr
;
368 insn_table_traverse_insn (table
, NULL
, (void *) &data
, model_c_insn
);
370 lf_printf (file
, "};\n");
371 lf_printf (file
, "\n");
372 lf_printf (file
, "\f\n");
375 lf_printf (file
, "#ifndef _INLINE_C_\n");
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
)
381 lf_printf (file
, " model_time_%s,\n", model_ptr
->name
);
383 lf_printf (file
, "};\n");
384 lf_printf (file
, "#endif\n");
385 lf_printf (file
, "\n");
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
)
394 lf_printf (file
, " \"%s\",\n", model_ptr
->printable_name
);
396 lf_printf (file
, "};\n");
397 lf_printf (file
, "#endif\n");
398 lf_printf (file
, "\n");
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");
405 lf_printf (file
, " model_enum model;\n");
407 " for(model = MODEL_%s; model < nr_models; model++) {\n",
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");
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
)
421 lf_printf (file
, "\\n\\t%s", model_ptr
->printable_name
);
423 lf_printf (file
, "\");\n");
427 lf_printf (file
, " error(\"No models are currently known about\");\n");
430 lf_printf (file
, "}\n");
438 gen_model_h (lf
*file
, insn_table
*table
)
440 lf_print__this_file_is_empty (file
, "suffering bit rot");
445 gen_model_c (lf
*file
, insn_table
*table
)
447 lf_print__this_file_is_empty (file
, "suffering bit rot");