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