]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/ppc/igen.c
sim: switch to libiberty environ.h
[thirdparty/binutils-gdb.git] / sim / ppc / igen.c
CommitLineData
c906108c
SS
1/* This file is part of the program psim.
2
8d64d0fd 3 Copyright 1994, 1995, 1996, 1997, 2003 Andrew Cagney
c906108c
SS
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
3fd725ef 7 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
51b318de 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
c906108c
SS
17
18 */
19
c906108c 20#include <getopt.h>
3d52735b 21#include <stdlib.h>
c906108c
SS
22
23#include "misc.h"
24#include "lf.h"
25#include "table.h"
68a3e151 26#include "build-config.h"
c906108c
SS
27
28#include "filter.h"
29
c906108c 30#include "ld-cache.h"
d81bb16a 31#include "ld-decode.h"
c906108c
SS
32#include "ld-insn.h"
33
34#include "igen.h"
35
36#include "gen-model.h"
37#include "gen-icache.h"
38#include "gen-itable.h"
39#include "gen-idecode.h"
40#include "gen-semantics.h"
41#include "gen-support.h"
42
43int hi_bit_nr;
44int insn_bit_size = max_insn_bit_size;
45
46igen_code code = generate_calls;
47
48int generate_expanded_instructions;
49int icache_size = 1024;
50int generate_smp;
51
52/****************************************************************/
53
54static int
55print_insn_bits(lf *file, insn_bits *bits)
56{
57 int nr = 0;
58 if (bits == NULL)
59 return nr;
60 nr += print_insn_bits(file, bits->last);
61 nr += lf_putchr(file, '_');
62 nr += lf_putstr(file, bits->field->val_string);
63 if (bits->opcode->is_boolean && bits->value == 0)
64 nr += lf_putint(file, bits->opcode->boolean_constant);
65 else if (!bits->opcode->is_boolean) {
66 if (bits->opcode->last < bits->field->last)
67 nr += lf_putint(file, bits->value << (bits->field->last - bits->opcode->last));
68 else
69 nr += lf_putint(file, bits->value);
70 }
71 return nr;
72}
73
74extern int
75print_function_name(lf *file,
76 const char *basename,
77 insn_bits *expanded_bits,
78 lf_function_name_prefixes prefix)
79{
80 int nr = 0;
81 /* the prefix */
82 switch (prefix) {
83 case function_name_prefix_semantics:
84 nr += lf_putstr(file, "semantic_");
85 break;
86 case function_name_prefix_idecode:
87 nr += lf_printf(file, "idecode_");
88 break;
89 case function_name_prefix_itable:
90 nr += lf_putstr(file, "itable_");
91 break;
92 case function_name_prefix_icache:
93 nr += lf_putstr(file, "icache_");
94 break;
95 default:
96 break;
97 }
98
99 /* the function name */
100 {
101 const char *pos;
102 for (pos = basename;
103 *pos != '\0';
104 pos++) {
105 switch (*pos) {
106 case '/':
107 case '-':
d29d5195
MG
108 case '(':
109 case ')':
c906108c
SS
110 break;
111 case ' ':
112 nr += lf_putchr(file, '_');
113 break;
114 default:
115 nr += lf_putchr(file, *pos);
116 break;
117 }
118 }
119 }
120
121 /* the suffix */
122 if (generate_expanded_instructions)
123 nr += print_insn_bits(file, expanded_bits);
124
125 return nr;
126}
127
128
129void
130print_my_defines(lf *file,
131 insn_bits *expanded_bits,
132 table_entry *file_entry)
133{
134 /* #define MY_INDEX xxxxx */
135 lf_indent_suppress(file);
136 lf_printf(file, "#undef MY_INDEX\n");
137 lf_indent_suppress(file);
138 lf_printf(file, "#define MY_INDEX ");
139 print_function_name(file,
140 file_entry->fields[insn_name],
141 NULL,
142 function_name_prefix_itable);
143 lf_printf(file, "\n");
144 /* #define MY_PREFIX xxxxxx */
145 lf_indent_suppress(file);
146 lf_printf(file, "#undef MY_PREFIX\n");
147 lf_indent_suppress(file);
148 lf_printf(file, "#define MY_PREFIX ");
149 print_function_name(file,
150 file_entry->fields[insn_name],
151 expanded_bits,
152 function_name_prefix_none);
153 lf_printf(file, "\n");
154}
155
156
157void
158print_itrace(lf *file,
159 table_entry *file_entry,
160 int idecode)
161{
162 lf_print__external_reference(file, file_entry->line_nr, file_entry->file_name);
163 lf_printf(file, "ITRACE(trace_%s, (\"%s %s\\n\"));\n",
164 (idecode ? "idecode" : "semantics"),
165 (idecode ? "idecode" : "semantics"),
166 file_entry->fields[insn_name]);
167 lf_print__internal_reference(file);
168}
169
170
171/****************************************************************/
172
173
174static void
175gen_semantics_h(insn_table *table,
176 lf *file,
177 igen_code generate)
178{
179 lf_printf(file, "typedef %s idecode_semantic\n(%s);\n",
180 SEMANTIC_FUNCTION_TYPE,
181 SEMANTIC_FUNCTION_FORMAL);
182 lf_printf(file, "\n");
183 if ((code & generate_calls)) {
c906108c
SS
184 lf_printf(file, "extern int option_mpc860c0;\n");
185 lf_printf(file, "#define PAGE_SIZE 0x1000\n");
186 lf_printf(file, "\n");
d29d5195 187 lf_printf(file, "PSIM_EXTERN_SEMANTICS(void)\n");
c906108c
SS
188 lf_printf(file, "semantic_init(device* root);\n");
189 lf_printf(file, "\n");
c906108c
SS
190 if (generate_expanded_instructions)
191 insn_table_traverse_tree(table,
192 file, NULL,
193 1,
194 NULL, /* start */
195 print_semantic_declaration, /* leaf */
196 NULL, /* end */
197 NULL); /* padding */
198 else
199 insn_table_traverse_insn(table,
200 file, NULL,
201 print_semantic_declaration);
202
203 }
204 else {
205 lf_print__this_file_is_empty(file);
206 }
207}
208
209
210static void
211gen_semantics_c(insn_table *table,
212 cache_table *cache_rules,
213 lf *file,
214 igen_code generate)
215{
216 if ((code & generate_calls)) {
217 lf_printf(file, "\n");
218 lf_printf(file, "#include \"cpu.h\"\n");
219 lf_printf(file, "#include \"idecode.h\"\n");
220 lf_printf(file, "#include \"semantics.h\"\n");
8d64d0fd 221 lf_printf(file, "#ifdef HAVE_COMMON_FPU\n");
d29d5195
MG
222 lf_printf(file, "#include \"sim-inline.h\"\n");
223 lf_printf(file, "#include \"sim-fpu.h\"\n");
8d64d0fd
AC
224 lf_printf(file, "#endif\n");
225 lf_printf(file, "#include \"support.h\"\n");
c906108c 226 lf_printf(file, "\n");
c906108c
SS
227 lf_printf(file, "int option_mpc860c0 = 0;\n");
228 lf_printf(file, "\n");
d29d5195 229 lf_printf(file, "PSIM_EXTERN_SEMANTICS(void)\n");
c906108c
SS
230 lf_printf(file, "semantic_init(device* root)\n");
231 lf_printf(file, "{\n");
232 lf_printf(file, " option_mpc860c0 = 0;\n");
233 lf_printf(file, " if (tree_find_property(root, \"/options/mpc860c0\"))\n");
234 lf_printf(file, " option_mpc860c0 = tree_find_integer_property(root, \"/options/mpc860c0\");\n");
7a292a7a 235 lf_printf(file, " option_mpc860c0 *= 4; /* convert word count to byte count */\n");
c906108c
SS
236 lf_printf(file, "}\n");
237 lf_printf(file, "\n");
c906108c
SS
238 if (generate_expanded_instructions)
239 insn_table_traverse_tree(table,
240 file, cache_rules,
241 1,
242 NULL, /* start */
243 print_semantic_definition, /* leaf */
244 NULL, /* end */
245 NULL); /* padding */
246 else
247 insn_table_traverse_insn(table,
248 file, cache_rules,
249 print_semantic_definition);
250
251 }
252 else {
253 lf_print__this_file_is_empty(file);
254 }
255}
256
257
258/****************************************************************/
259
260
261static void
262gen_icache_h(insn_table *table,
263 lf *file,
264 igen_code generate)
265{
266 lf_printf(file, "typedef %s idecode_icache\n(%s);\n",
267 ICACHE_FUNCTION_TYPE,
268 ICACHE_FUNCTION_FORMAL);
269 lf_printf(file, "\n");
270 if ((code & generate_calls)
271 && (code & generate_with_icache)) {
272 insn_table_traverse_function(table,
273 file, NULL,
274 print_icache_internal_function_declaration);
275 if (generate_expanded_instructions)
276 insn_table_traverse_tree(table,
277 file, NULL,
278 1,
279 NULL, /* start */
280 print_icache_declaration, /* leaf */
281 NULL, /* end */
282 NULL); /* padding */
283 else
284 insn_table_traverse_insn(table,
285 file, NULL,
286 print_icache_declaration);
287
288 }
289 else {
290 lf_print__this_file_is_empty(file);
291 }
292}
293
294static void
295gen_icache_c(insn_table *table,
296 cache_table *cache_rules,
297 lf *file,
298 igen_code generate)
299{
300 /* output `internal' invalid/floating-point unavailable functions
301 where needed */
302 if ((code & generate_calls)
303 && (code & generate_with_icache)) {
304 lf_printf(file, "\n");
305 lf_printf(file, "#include \"cpu.h\"\n");
306 lf_printf(file, "#include \"idecode.h\"\n");
307 lf_printf(file, "#include \"semantics.h\"\n");
308 lf_printf(file, "#include \"icache.h\"\n");
8d64d0fd 309 lf_printf(file, "#ifdef HAVE_COMMON_FPU\n");
d29d5195
MG
310 lf_printf(file, "#include \"sim-inline.h\"\n");
311 lf_printf(file, "#include \"sim-fpu.h\"\n");
8d64d0fd
AC
312 lf_printf(file, "#endif\n");
313 lf_printf(file, "#include \"support.h\"\n");
c906108c
SS
314 lf_printf(file, "\n");
315 insn_table_traverse_function(table,
316 file, NULL,
317 print_icache_internal_function_definition);
318 lf_printf(file, "\n");
319 if (generate_expanded_instructions)
320 insn_table_traverse_tree(table,
321 file, cache_rules,
322 1,
323 NULL, /* start */
324 print_icache_definition, /* leaf */
325 NULL, /* end */
326 NULL); /* padding */
327 else
328 insn_table_traverse_insn(table,
329 file, cache_rules,
330 print_icache_definition);
331
332 }
333 else {
334 lf_print__this_file_is_empty(file);
335 }
336}
337
338
339/****************************************************************/
340
341
342int
343main(int argc,
344 char **argv,
345 char **envp)
346{
347 cache_table *cache_rules = NULL;
348 lf_file_references file_references = lf_include_references;
349 decode_table *decode_rules = NULL;
350 filter *filters = NULL;
351 insn_table *instructions = NULL;
43c4bab0 352 table_include *includes = NULL;
c906108c
SS
353 char *real_file_name = NULL;
354 int is_header = 0;
355 int ch;
356
357 if (argc == 1) {
358 printf("Usage:\n");
359 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
360 printf("Config options:\n");
361 printf(" -F <filter-out-flag> eg -F 64 to skip 64bit instructions\n");
362 printf(" -E Expand (duplicate) semantic functions\n");
363 printf(" -I <icache-size> Generate cracking cache version\n");
364 printf(" -C Include semantics in cache functions\n");
365 printf(" -S Include insn (instruction) in icache\n");
366 printf(" -R Use defines to reference cache vars\n");
367 printf(" -L Supress line numbering in output files\n");
368 printf(" -B <bit-size> Set the number of bits in an instruction\n");
369 printf(" -H <high-bit> Set the nr of the high (msb bit)\n");
370 printf(" -N <nr-cpus> Specify the max number of cpus the simulation will support\n");
371 printf(" -J Use jumps instead of function calls\n");
372 printf(" -T <mechanism> Override the mechanism used to decode an instruction\n");
373 printf(" using <mechanism> instead of what was specified in the\n");
374 printf(" decode-rules input file\n");
375 printf("\n");
376 printf("Input options (ucase version also dumps loaded table):\n");
377 printf(" -o <decode-rules>\n");
378 printf(" -k <cache-rules>\n");
379 printf(" -i <instruction-table>\n");
380 printf("\n");
381 printf("Output options:\n");
382 printf(" -n <real-name> Specify the real name of for the next output file\n");
383 printf(" -h Generate header file\n");
384 printf(" -c <output-file> output icache\n");
385 printf(" -d <output-file> output idecode\n");
386 printf(" -m <output-file> output model\n");
387 printf(" -s <output-file> output schematic\n");
388 printf(" -t <output-file> output itable\n");
389 printf(" -f <output-file> output support functions\n");
390 }
391
392 while ((ch = getopt(argc, argv,
393 "F:EI:RSLJT:CB:H:N:o:k:i:n:hc:d:m:s:t:f:"))
394 != -1) {
395 fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
396 switch(ch) {
397 case 'C':
398 code |= generate_with_icache;
399 code |= generate_with_semantic_icache;
400 break;
401 case 'S':
402 code |= generate_with_icache;
403 code |= generate_with_insn_in_icache;
404 break;
405 case 'L':
406 file_references = lf_omit_references;
407 break;
408 case 'E':
409 generate_expanded_instructions = 1;
410 break;
43c4bab0
MG
411 case 'G':
412 {
413 int enable_p;
414 char *argp;
415 if (strncmp (optarg, "no-", strlen ("no-")) == 0)
416 {
417 argp = optarg + strlen ("no-");
418 enable_p = 0;
419 }
420 else if (strncmp (optarg, "!", strlen ("!")) == 0)
421 {
422 argp = optarg + strlen ("no-");
423 enable_p = 0;
424 }
425 else
426 {
427 argp = optarg;
428 enable_p = 1;
429 }
430 if (strncmp (argp, "gen-icache", strlen ("gen-icache")) == 0)
431 {
432 switch (argp[strlen ("gen-icache")])
433 {
434 case '=':
435 icache_size = atoi (argp + strlen ("gen-icache") + 1);
436 code |= generate_with_icache;
437 break;
438 case '\0':
439 code |= generate_with_icache;
440 break;
441 default:
442 error (NULL, "Expecting -Ggen-icache or -Ggen-icache=<N>\n");
443 }
444 }
445 }
c906108c 446 case 'I':
43c4bab0
MG
447 {
448 table_include **dir = &includes;
449 while ((*dir) != NULL)
450 dir = &(*dir)->next;
451 (*dir) = ZALLOC (table_include);
452 (*dir)->dir = strdup (optarg);
453 }
c906108c
SS
454 break;
455 case 'N':
456 generate_smp = a2i(optarg);
457 break;
458 case 'R':
459 code |= generate_with_direct_access;
460 break;
461 case 'B':
462 insn_bit_size = a2i(optarg);
463 ASSERT(insn_bit_size > 0 && insn_bit_size <= max_insn_bit_size
464 && (hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0));
465 break;
466 case 'H':
467 hi_bit_nr = a2i(optarg);
468 ASSERT(hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0);
469 break;
470 case 'F':
471 filters = new_filter(optarg, filters);
472 break;
473 case 'J':
474 code &= ~generate_calls;
475 code |= generate_jumps;
476 break;
477 case 'T':
478 force_decode_gen_type(optarg);
479 break;
480 case 'i':
d81bb16a
AC
481 if (decode_rules == NULL) {
482 fprintf(stderr, "Must specify decode tables\n");
c906108c
SS
483 exit (1);
484 }
d81bb16a
AC
485 instructions = load_insn_table(optarg, decode_rules, filters, includes,
486 &cache_rules);
c906108c
SS
487 fprintf(stderr, "\texpanding ...\n");
488 insn_table_expand_insns(instructions);
489 break;
490 case 'o':
491 decode_rules = load_decode_table(optarg, hi_bit_nr);
492 break;
493 case 'k':
494 cache_rules = load_cache_table(optarg, hi_bit_nr);
495 break;
496 case 'n':
497 real_file_name = strdup(optarg);
498 break;
499 case 'h':
500 is_header = 1;
501 break;
502 case 's':
503 case 'd':
504 case 'm':
505 case 't':
506 case 'f':
507 case 'c':
508 {
509 lf *file = lf_open(optarg, real_file_name, file_references,
510 (is_header ? lf_is_h : lf_is_c),
511 argv[0]);
512 lf_print__file_start(file);
513 ASSERT(instructions != NULL);
514 switch (ch) {
515 case 's':
516 if(is_header)
517 gen_semantics_h(instructions, file, code);
518 else
519 gen_semantics_c(instructions, cache_rules, file, code);
520 break;
521 case 'd':
522 if (is_header)
523 gen_idecode_h(file, instructions, cache_rules);
524 else
525 gen_idecode_c(file, instructions, cache_rules);
526 break;
527 case 'm':
528 if (is_header)
529 gen_model_h(instructions, file);
530 else
531 gen_model_c(instructions, file);
532 break;
533 case 't':
534 if (is_header)
535 gen_itable_h(instructions, file);
536 else
537 gen_itable_c(instructions, file);
538 break;
539 case 'f':
540 if (is_header)
541 gen_support_h(instructions, file);
542 else
543 gen_support_c(instructions, file);
544 break;
545 case 'c':
546 if (is_header)
547 gen_icache_h(instructions, file, code);
548 else
549 gen_icache_c(instructions, cache_rules, file, code);
550 break;
551 }
552 lf_print__file_finish(file);
553 lf_close(file);
554 is_header = 0;
555 }
556 real_file_name = NULL;
557 break;
558 default:
559 error("unknown option\n");
560 }
561 }
562 return 0;
563}