]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/ppc/igen.c
* reloc.c: Add relocs BFD_RELOC_M32R_{HI16_[US]LO,LO16}.
[thirdparty/binutils-gdb.git] / sim / ppc / igen.c
CommitLineData
c143ef62
MM
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 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
30c87b55
MM
21
22
c143ef62
MM
23#include <getopt.h>
24
25#include "misc.h"
26#include "lf.h"
30c87b55
MM
27#include "table.h"
28#include "config.h"
c143ef62 29
30c87b55 30#include "filter.h"
c143ef62 31
30c87b55
MM
32#include "ld-decode.h"
33#include "ld-cache.h"
34#include "ld-insn.h"
c143ef62 35
30c87b55 36#include "igen.h"
c143ef62 37
30c87b55
MM
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"
c143ef62 44
30c87b55
MM
45int hi_bit_nr;
46int insn_bit_size = max_insn_bit_size;
c143ef62 47
30c87b55 48igen_code code = generate_calls;
c143ef62 49
30c87b55
MM
50int generate_expanded_instructions;
51int icache_size;
52int generate_smp;
c143ef62
MM
53
54/****************************************************************/
55
30c87b55
MM
56static int
57print_insn_bits(lf *file, insn_bits *bits)
c143ef62 58{
30c87b55
MM
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));
c143ef62 70 else
30c87b55 71 nr += lf_putint(file, bits->value);
c143ef62 72 }
30c87b55
MM
73 return nr;
74}
c143ef62 75
30c87b55
MM
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;
c143ef62
MM
99 }
100
30c87b55
MM
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 }
8a1d8a0b 119 }
c143ef62 120
30c87b55
MM
121 /* the suffix */
122 if (generate_expanded_instructions)
123 nr += print_insn_bits(file, expanded_bits);
c143ef62 124
30c87b55 125 return nr;
c143ef62
MM
126}
127
128
30c87b55
MM
129void
130print_define_my_index(lf *file,
131 table_entry *file_entry)
c143ef62 132{
30c87b55
MM
133 lf_indent_suppress(file);
134 lf_printf(file, "#undef MY_INDEX\n");
135 lf_indent_suppress(file);
136 lf_printf(file, "#define MY_INDEX ");
137 print_function_name(file,
138 file_entry->fields[insn_name],
139 NULL,
140 function_name_prefix_itable);
c143ef62 141 lf_printf(file, "\n");
c143ef62
MM
142}
143
c143ef62 144
30c87b55
MM
145void
146print_itrace(lf *file,
147 table_entry *file_entry,
148 int idecode)
c143ef62 149{
30c87b55
MM
150 lf_print__external_reference(file, file_entry->line_nr, file_entry->file_name);
151 lf_printf(file, "ITRACE(trace_%s, (\"%s %s\\n\"));\n",
152 (idecode ? "idecode" : "semantics"),
153 (idecode ? "idecode" : "semantics"),
154 file_entry->fields[insn_name]);
155 lf_print__internal_reference(file);
c143ef62
MM
156}
157
158
a3114052
MM
159/****************************************************************/
160
1dc7c0ed
MM
161
162static void
30c87b55
MM
163gen_semantics_h(insn_table *table,
164 lf *file,
165 igen_code generate)
a3114052 166{
30c87b55
MM
167 lf_printf(file, "typedef %s idecode_semantic\n(%s);\n",
168 SEMANTIC_FUNCTION_TYPE,
169 SEMANTIC_FUNCTION_FORMAL);
80948f39 170 lf_printf(file, "\n");
30c87b55
MM
171 if ((code & generate_calls)) {
172 if (generate_expanded_instructions)
173 insn_table_traverse_tree(table,
174 file, NULL,
175 1,
176 NULL, /* start */
177 print_semantic_declaration, /* leaf */
178 NULL, /* end */
179 NULL); /* padding */
180 else
181 insn_table_traverse_insn(table,
182 file, NULL,
183 print_semantic_declaration);
184
80948f39 185 }
30c87b55
MM
186 else {
187 lf_print__this_file_is_empty(file);
80948f39 188 }
30c87b55 189}
80948f39 190
eb4ef197 191
30c87b55
MM
192static void
193gen_semantics_c(insn_table *table,
194 cache_table *cache_rules,
195 lf *file,
196 igen_code generate)
197{
198 if ((code & generate_calls)) {
80948f39 199 lf_printf(file, "\n");
30c87b55
MM
200 lf_printf(file, "#include \"cpu.h\"\n");
201 lf_printf(file, "#include \"idecode.h\"\n");
202 lf_printf(file, "#include \"semantics.h\"\n");
203 lf_printf(file, "#include \"support.h\"\n");
80948f39 204 lf_printf(file, "\n");
30c87b55
MM
205 if (generate_expanded_instructions)
206 insn_table_traverse_tree(table,
207 file, cache_rules,
208 1,
209 NULL, /* start */
210 print_semantic_definition, /* leaf */
211 NULL, /* end */
212 NULL); /* padding */
213 else
214 insn_table_traverse_insn(table,
215 file, cache_rules,
216 print_semantic_definition);
217
80948f39 218 }
30c87b55
MM
219 else {
220 lf_print__this_file_is_empty(file);
80948f39 221 }
a3114052
MM
222}
223
a3114052 224
30c87b55 225/****************************************************************/
a3114052 226
a3114052 227
eb4ef197 228static void
30c87b55
MM
229gen_icache_h(insn_table *table,
230 lf *file,
231 igen_code generate)
eb4ef197 232{
30c87b55
MM
233 lf_printf(file, "typedef %s idecode_icache\n(%s);\n",
234 ICACHE_FUNCTION_TYPE,
235 ICACHE_FUNCTION_FORMAL);
236 lf_printf(file, "\n");
237 if ((code & generate_calls)
238 && (code & generate_with_icache)) {
239 insn_table_traverse_function(table,
240 file, NULL,
241 print_icache_internal_function_declaration);
242 if (generate_expanded_instructions)
243 insn_table_traverse_tree(table,
244 file, NULL,
245 1,
246 NULL, /* start */
247 print_icache_declaration, /* leaf */
248 NULL, /* end */
249 NULL); /* padding */
250 else
251 insn_table_traverse_insn(table,
252 file, NULL,
253 print_icache_declaration);
254
eb4ef197
MM
255 }
256 else {
30c87b55 257 lf_print__this_file_is_empty(file);
eb4ef197 258 }
eb4ef197
MM
259}
260
30c87b55
MM
261static void
262gen_icache_c(insn_table *table,
263 cache_table *cache_rules,
264 lf *file,
265 igen_code generate)
a3114052 266{
30c87b55
MM
267 /* output `internal' invalid/floating-point unavailable functions
268 where needed */
269 if ((code & generate_calls)
270 && (code & generate_with_icache)) {
80948f39 271 lf_printf(file, "\n");
30c87b55
MM
272 lf_printf(file, "#include \"cpu.h\"\n");
273 lf_printf(file, "#include \"idecode.h\"\n");
274 lf_printf(file, "#include \"semantics.h\"\n");
275 lf_printf(file, "#include \"icache.h\"\n");
276 lf_printf(file, "#include \"support.h\"\n");
80948f39 277 lf_printf(file, "\n");
30c87b55
MM
278 insn_table_traverse_function(table,
279 file, NULL,
280 print_icache_internal_function_definition);
845ff5a4 281 lf_printf(file, "\n");
30c87b55
MM
282 if (generate_expanded_instructions)
283 insn_table_traverse_tree(table,
284 file, cache_rules,
285 1,
286 NULL, /* start */
287 print_icache_definition, /* leaf */
288 NULL, /* end */
289 NULL); /* padding */
290 else
291 insn_table_traverse_insn(table,
292 file, cache_rules,
293 print_icache_definition);
294
80948f39 295 }
30c87b55
MM
296 else {
297 lf_print__this_file_is_empty(file);
28816f45 298 }
c143ef62
MM
299}
300
30c87b55 301
c143ef62
MM
302/****************************************************************/
303
304
305int
306main(int argc,
307 char **argv,
308 char **envp)
309{
30c87b55
MM
310 cache_table *cache_rules = NULL;
311 lf_file_references file_references = lf_include_references;
312 decode_table *decode_rules = NULL;
313 filter *filters = NULL;
c143ef62 314 insn_table *instructions = NULL;
c143ef62 315 char *real_file_name = NULL;
30c87b55 316 int is_header = 0;
c143ef62
MM
317 int ch;
318
319 if (argc == 1) {
320 printf("Usage:\n");
a983c8f0
MM
321 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
322 printf("Config options:\n");
30c87b55
MM
323 printf(" -F <filter-out-flag> eg -F 64 to skip 64bit instructions\n");
324 printf(" -C Include semantics in cache functions\n");
325 printf(" -E Expand (duplicate) semantic functions\n");
326 printf(" -I <icache-size> Generate cracking cache version\n");
f2181eff 327 printf(" -R Use defines to reference cache vars\n");
30c87b55
MM
328 printf(" -L Supress line numbering in output files\n");
329 printf(" -B <bit-size> Set the number of bits in an instruction\n");
330 printf(" -H <high-bit> Set the nr of the high (msb bit)\n");
331 printf(" -N <nr-cpus> Specify the max number of cpus the simulation will support\n");
332 printf(" -J Use jumps instead of function calls\n");
f2181eff 333 printf("\n");
a983c8f0 334 printf("Input options (ucase version also dumps loaded table):\n");
30c87b55
MM
335 printf(" -o <opcode-rules>\n");
336 printf(" -k <cache-rules>\n");
337 printf(" -i <instruction-table>\n");
f2181eff 338 printf("\n");
a983c8f0 339 printf("Output options:\n");
30c87b55
MM
340 printf(" -n <real-name> Specify the real name of for the next output file\n");
341 printf(" -h Generate header file\n");
342 printf(" -c <output-file> output icache\n");
343 printf(" -d <output-file> output idecode\n");
344 printf(" -m <output-file> output model\n");
345 printf(" -s <output-file> output schematic\n");
346 printf(" -t <output-file> output itable\n");
347 printf(" -f <output-file> output support functions\n");
c143ef62
MM
348 }
349
350 while ((ch = getopt(argc, argv,
30c87b55
MM
351 "F:EI:RLJCB:H:N:o:k:i:n:hc:d:m:s:t:f:"))
352 != -1) {
c143ef62
MM
353 fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
354 switch(ch) {
30c87b55
MM
355 case 'C':
356 code |= generate_with_icache;
357 code |= generate_with_semantic_icache;
358 if (icache_size == 0)
359 icache_size = 1024;
360 break;
361 case 'L':
362 file_references = lf_omit_references;
363 break;
364 case 'E':
365 generate_expanded_instructions = 1;
c143ef62 366 break;
30c87b55
MM
367 case 'I':
368 icache_size = a2i(optarg);
369 code |= generate_with_icache;
c143ef62 370 break;
30c87b55
MM
371 case 'N':
372 generate_smp = a2i(optarg);
c143ef62 373 break;
f2181eff 374 case 'R':
30c87b55
MM
375 code |= generate_with_icache;
376 code |= generate_with_direct_access_icache;
377 if (icache_size == 0)
378 icache_size = 1024;
f2181eff 379 break;
30c87b55
MM
380 case 'B':
381 insn_bit_size = a2i(optarg);
382 ASSERT(insn_bit_size > 0 && insn_bit_size <= max_insn_bit_size
383 && (hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0));
a983c8f0 384 break;
30c87b55 385 case 'H':
a983c8f0 386 hi_bit_nr = a2i(optarg);
30c87b55
MM
387 ASSERT(hi_bit_nr == insn_bit_size-1 || hi_bit_nr == 0);
388 break;
389 case 'F':
390 filters = new_filter(optarg, filters);
391 break;
392 case 'J':
393 code &= ~generate_calls;
394 code |= generate_jumps;
a983c8f0 395 break;
c143ef62 396 case 'i':
30c87b55
MM
397 if (decode_rules == NULL || cache_rules == NULL) {
398 fprintf(stderr, "Must specify decode and cache tables\n");
399 exit (1);
400 }
401 instructions = load_insn_table(optarg, decode_rules, filters);
c143ef62
MM
402 fprintf(stderr, "\texpanding ...\n");
403 insn_table_expand_insns(instructions);
c143ef62 404 break;
c143ef62 405 case 'o':
30c87b55 406 decode_rules = load_decode_table(optarg, hi_bit_nr);
c143ef62 407 break;
c143ef62 408 case 'k':
30c87b55 409 cache_rules = load_cache_table(optarg, hi_bit_nr);
c143ef62
MM
410 break;
411 case 'n':
412 real_file_name = strdup(optarg);
413 break;
30c87b55
MM
414 case 'h':
415 is_header = 1;
416 break;
c143ef62 417 case 's':
c143ef62 418 case 'd':
a3114052 419 case 'm':
c143ef62 420 case 't':
30c87b55
MM
421 case 'f':
422 case 'c':
c143ef62 423 {
30c87b55
MM
424 lf *file = lf_open(optarg, real_file_name, file_references,
425 (is_header ? lf_is_h : lf_is_c),
426 argv[0]);
427 lf_print__file_start(file);
c143ef62
MM
428 ASSERT(instructions != NULL);
429 switch (ch) {
c143ef62 430 case 's':
30c87b55
MM
431 if(is_header)
432 gen_semantics_h(instructions, file, code);
433 else
434 gen_semantics_c(instructions, cache_rules, file, code);
c143ef62
MM
435 break;
436 case 'd':
30c87b55
MM
437 if (is_header)
438 gen_idecode_h(file, instructions, cache_rules);
439 else
440 gen_idecode_c(file, instructions, cache_rules);
a3114052
MM
441 break;
442 case 'm':
30c87b55
MM
443 if (is_header)
444 gen_model_h(instructions, file);
445 else
446 gen_model_c(instructions, file);
c143ef62
MM
447 break;
448 case 't':
30c87b55
MM
449 if (is_header)
450 gen_itable_h(instructions, file);
451 else
452 gen_itable_c(instructions, file);
453 break;
454 case 'f':
455 if (is_header)
456 gen_support_h(instructions, file);
457 else
458 gen_support_c(instructions, file);
c143ef62 459 break;
30c87b55
MM
460 case 'c':
461 if (is_header)
462 gen_icache_h(instructions, file, code);
463 else
464 gen_icache_c(instructions, cache_rules, file, code);
c143ef62
MM
465 break;
466 }
30c87b55 467 lf_print__file_finish(file);
c143ef62 468 lf_close(file);
30c87b55 469 is_header = 0;
c143ef62
MM
470 }
471 real_file_name = NULL;
472 break;
473 default:
474 error("unknown option\n");
475 }
476 }
477 return 0;
478}