]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/igen.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / sim / ppc / igen.c
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
45 int hi_bit_nr;
46 int insn_bit_size = max_insn_bit_size;
47
48 igen_code code = generate_calls;
49
50 int generate_expanded_instructions;
51 int icache_size = 1024;
52 int generate_smp;
53
54 /****************************************************************/
55
56 static int
57 print_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
76 extern int
77 print_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 '-':
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
129 void
130 print_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
157 void
158 print_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
174 static void
175 gen_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)) {
184 lf_printf(file, "#ifdef WITH_OPTION_MPC860C0\n");
185 lf_printf(file, "extern int option_mpc860c0;\n");
186 lf_printf(file, "#define PAGE_SIZE 0x1000\n");
187 lf_printf(file, "\n");
188 lf_printf(file, "EXTERN_SEMANTICS(void)\n");
189 lf_printf(file, "semantic_init(device* root);\n");
190 lf_printf(file, "\n");
191 lf_printf(file, "#endif // WITH_OPTION_MPC860C0\n");
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
212 static void
213 gen_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");
224 lf_printf(file, "\n");
225 lf_printf(file, "#ifdef WITH_OPTION_MPC860C0\n");
226 lf_printf(file, "int option_mpc860c0 = 0;\n");
227 lf_printf(file, "\n");
228 lf_printf(file, "EXTERN_SEMANTICS(void)\n");
229 lf_printf(file, "semantic_init(device* root)\n");
230 lf_printf(file, "{\n");
231 lf_printf(file, " option_mpc860c0 = 0;\n");
232 lf_printf(file, " if (tree_find_property(root, \"/options/mpc860c0\"))\n");
233 lf_printf(file, " option_mpc860c0 = tree_find_integer_property(root, \"/options/mpc860c0\");\n");
234 lf_printf(file, "}\n");
235 lf_printf(file, "\n");
236 lf_printf(file, "#endif // WITH_OPTION_MPC860C0\n");
237 if (generate_expanded_instructions)
238 insn_table_traverse_tree(table,
239 file, cache_rules,
240 1,
241 NULL, /* start */
242 print_semantic_definition, /* leaf */
243 NULL, /* end */
244 NULL); /* padding */
245 else
246 insn_table_traverse_insn(table,
247 file, cache_rules,
248 print_semantic_definition);
249
250 }
251 else {
252 lf_print__this_file_is_empty(file);
253 }
254 }
255
256
257 /****************************************************************/
258
259
260 static void
261 gen_icache_h(insn_table *table,
262 lf *file,
263 igen_code generate)
264 {
265 lf_printf(file, "typedef %s idecode_icache\n(%s);\n",
266 ICACHE_FUNCTION_TYPE,
267 ICACHE_FUNCTION_FORMAL);
268 lf_printf(file, "\n");
269 if ((code & generate_calls)
270 && (code & generate_with_icache)) {
271 insn_table_traverse_function(table,
272 file, NULL,
273 print_icache_internal_function_declaration);
274 if (generate_expanded_instructions)
275 insn_table_traverse_tree(table,
276 file, NULL,
277 1,
278 NULL, /* start */
279 print_icache_declaration, /* leaf */
280 NULL, /* end */
281 NULL); /* padding */
282 else
283 insn_table_traverse_insn(table,
284 file, NULL,
285 print_icache_declaration);
286
287 }
288 else {
289 lf_print__this_file_is_empty(file);
290 }
291 }
292
293 static void
294 gen_icache_c(insn_table *table,
295 cache_table *cache_rules,
296 lf *file,
297 igen_code generate)
298 {
299 /* output `internal' invalid/floating-point unavailable functions
300 where needed */
301 if ((code & generate_calls)
302 && (code & generate_with_icache)) {
303 lf_printf(file, "\n");
304 lf_printf(file, "#include \"cpu.h\"\n");
305 lf_printf(file, "#include \"idecode.h\"\n");
306 lf_printf(file, "#include \"semantics.h\"\n");
307 lf_printf(file, "#include \"icache.h\"\n");
308 lf_printf(file, "#include \"support.h\"\n");
309 lf_printf(file, "\n");
310 insn_table_traverse_function(table,
311 file, NULL,
312 print_icache_internal_function_definition);
313 lf_printf(file, "\n");
314 if (generate_expanded_instructions)
315 insn_table_traverse_tree(table,
316 file, cache_rules,
317 1,
318 NULL, /* start */
319 print_icache_definition, /* leaf */
320 NULL, /* end */
321 NULL); /* padding */
322 else
323 insn_table_traverse_insn(table,
324 file, cache_rules,
325 print_icache_definition);
326
327 }
328 else {
329 lf_print__this_file_is_empty(file);
330 }
331 }
332
333
334 /****************************************************************/
335
336
337 int
338 main(int argc,
339 char **argv,
340 char **envp)
341 {
342 cache_table *cache_rules = NULL;
343 lf_file_references file_references = lf_include_references;
344 decode_table *decode_rules = NULL;
345 filter *filters = NULL;
346 insn_table *instructions = NULL;
347 char *real_file_name = NULL;
348 int is_header = 0;
349 int ch;
350
351 if (argc == 1) {
352 printf("Usage:\n");
353 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
354 printf("Config options:\n");
355 printf(" -F <filter-out-flag> eg -F 64 to skip 64bit instructions\n");
356 printf(" -E Expand (duplicate) semantic functions\n");
357 printf(" -I <icache-size> Generate cracking cache version\n");
358 printf(" -C Include semantics in cache functions\n");
359 printf(" -S Include insn (instruction) in icache\n");
360 printf(" -R Use defines to reference cache vars\n");
361 printf(" -L Supress line numbering in output files\n");
362 printf(" -B <bit-size> Set the number of bits in an instruction\n");
363 printf(" -H <high-bit> Set the nr of the high (msb bit)\n");
364 printf(" -N <nr-cpus> Specify the max number of cpus the simulation will support\n");
365 printf(" -J Use jumps instead of function calls\n");
366 printf(" -T <mechanism> Override the mechanism used to decode an instruction\n");
367 printf(" using <mechanism> instead of what was specified in the\n");
368 printf(" decode-rules input file\n");
369 printf("\n");
370 printf("Input options (ucase version also dumps loaded table):\n");
371 printf(" -o <decode-rules>\n");
372 printf(" -k <cache-rules>\n");
373 printf(" -i <instruction-table>\n");
374 printf("\n");
375 printf("Output options:\n");
376 printf(" -n <real-name> Specify the real name of for the next output file\n");
377 printf(" -h Generate header file\n");
378 printf(" -c <output-file> output icache\n");
379 printf(" -d <output-file> output idecode\n");
380 printf(" -m <output-file> output model\n");
381 printf(" -s <output-file> output schematic\n");
382 printf(" -t <output-file> output itable\n");
383 printf(" -f <output-file> output support functions\n");
384 }
385
386 while ((ch = getopt(argc, argv,
387 "F:EI:RSLJT:CB:H:N:o:k:i:n:hc:d:m:s:t:f:"))
388 != -1) {
389 fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
390 switch(ch) {
391 case 'C':
392 code |= generate_with_icache;
393 code |= generate_with_semantic_icache;
394 break;
395 case 'S':
396 code |= generate_with_icache;
397 code |= generate_with_insn_in_icache;
398 break;
399 case 'L':
400 file_references = lf_omit_references;
401 break;
402 case 'E':
403 generate_expanded_instructions = 1;
404 break;
405 case 'I':
406 icache_size = a2i(optarg);
407 code |= generate_with_icache;
408 break;
409 case 'N':
410 generate_smp = a2i(optarg);
411 break;
412 case 'R':
413 code |= generate_with_direct_access;
414 break;
415 case 'B':
416 insn_bit_size = a2i(optarg);
417 ASSERT(insn_bit_size > 0 && insn_bit_size <= max_insn_bit_size
418 && (hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0));
419 break;
420 case 'H':
421 hi_bit_nr = a2i(optarg);
422 ASSERT(hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0);
423 break;
424 case 'F':
425 filters = new_filter(optarg, filters);
426 break;
427 case 'J':
428 code &= ~generate_calls;
429 code |= generate_jumps;
430 break;
431 case 'T':
432 force_decode_gen_type(optarg);
433 break;
434 case 'i':
435 if (decode_rules == NULL || cache_rules == NULL) {
436 fprintf(stderr, "Must specify decode and cache tables\n");
437 exit (1);
438 }
439 instructions = load_insn_table(optarg, decode_rules, filters);
440 fprintf(stderr, "\texpanding ...\n");
441 insn_table_expand_insns(instructions);
442 break;
443 case 'o':
444 decode_rules = load_decode_table(optarg, hi_bit_nr);
445 break;
446 case 'k':
447 cache_rules = load_cache_table(optarg, hi_bit_nr);
448 break;
449 case 'n':
450 real_file_name = strdup(optarg);
451 break;
452 case 'h':
453 is_header = 1;
454 break;
455 case 's':
456 case 'd':
457 case 'm':
458 case 't':
459 case 'f':
460 case 'c':
461 {
462 lf *file = lf_open(optarg, real_file_name, file_references,
463 (is_header ? lf_is_h : lf_is_c),
464 argv[0]);
465 lf_print__file_start(file);
466 ASSERT(instructions != NULL);
467 switch (ch) {
468 case 's':
469 if(is_header)
470 gen_semantics_h(instructions, file, code);
471 else
472 gen_semantics_c(instructions, cache_rules, file, code);
473 break;
474 case 'd':
475 if (is_header)
476 gen_idecode_h(file, instructions, cache_rules);
477 else
478 gen_idecode_c(file, instructions, cache_rules);
479 break;
480 case 'm':
481 if (is_header)
482 gen_model_h(instructions, file);
483 else
484 gen_model_c(instructions, file);
485 break;
486 case 't':
487 if (is_header)
488 gen_itable_h(instructions, file);
489 else
490 gen_itable_c(instructions, file);
491 break;
492 case 'f':
493 if (is_header)
494 gen_support_h(instructions, file);
495 else
496 gen_support_c(instructions, file);
497 break;
498 case 'c':
499 if (is_header)
500 gen_icache_h(instructions, file, code);
501 else
502 gen_icache_c(instructions, cache_rules, file, code);
503 break;
504 }
505 lf_print__file_finish(file);
506 lf_close(file);
507 is_header = 0;
508 }
509 real_file_name = NULL;
510 break;
511 default:
512 error("unknown option\n");
513 }
514 }
515 return 0;
516 }