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