New simulator changes from Andrew
[deliverable/binutils-gdb.git] / sim / ppc / igen.c
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
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;
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_define_my_index(lf *file,
131 table_entry *file_entry)
132 {
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);
141 lf_printf(file, "\n");
142 }
143
144
145 void
146 print_itrace(lf *file,
147 table_entry *file_entry,
148 int idecode)
149 {
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);
156 }
157
158
159 /****************************************************************/
160
161
162 static void
163 gen_semantics_h(insn_table *table,
164 lf *file,
165 igen_code generate)
166 {
167 lf_printf(file, "typedef %s idecode_semantic\n(%s);\n",
168 SEMANTIC_FUNCTION_TYPE,
169 SEMANTIC_FUNCTION_FORMAL);
170 lf_printf(file, "\n");
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
185 }
186 else {
187 lf_print__this_file_is_empty(file);
188 }
189 }
190
191
192 static void
193 gen_semantics_c(insn_table *table,
194 cache_table *cache_rules,
195 lf *file,
196 igen_code generate)
197 {
198 if ((code & generate_calls)) {
199 lf_printf(file, "\n");
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");
204 lf_printf(file, "\n");
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
218 }
219 else {
220 lf_print__this_file_is_empty(file);
221 }
222 }
223
224
225 /****************************************************************/
226
227
228 static void
229 gen_icache_h(insn_table *table,
230 lf *file,
231 igen_code generate)
232 {
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
255 }
256 else {
257 lf_print__this_file_is_empty(file);
258 }
259 }
260
261 static void
262 gen_icache_c(insn_table *table,
263 cache_table *cache_rules,
264 lf *file,
265 igen_code generate)
266 {
267 /* output `internal' invalid/floating-point unavailable functions
268 where needed */
269 if ((code & generate_calls)
270 && (code & generate_with_icache)) {
271 lf_printf(file, "\n");
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");
277 lf_printf(file, "\n");
278 insn_table_traverse_function(table,
279 file, NULL,
280 print_icache_internal_function_definition);
281 lf_printf(file, "\n");
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
295 }
296 else {
297 lf_print__this_file_is_empty(file);
298 }
299 }
300
301
302 /****************************************************************/
303
304
305 int
306 main(int argc,
307 char **argv,
308 char **envp)
309 {
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;
314 insn_table *instructions = NULL;
315 char *real_file_name = NULL;
316 int is_header = 0;
317 int ch;
318
319 if (argc == 1) {
320 printf("Usage:\n");
321 printf(" igen <config-opts> ... <input-opts>... <output-opts>...\n");
322 printf("Config options:\n");
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");
327 printf(" -R Use defines to reference cache vars\n");
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");
333 printf("\n");
334 printf("Input options (ucase version also dumps loaded table):\n");
335 printf(" -o <opcode-rules>\n");
336 printf(" -k <cache-rules>\n");
337 printf(" -i <instruction-table>\n");
338 printf("\n");
339 printf("Output options:\n");
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");
348 }
349
350 while ((ch = getopt(argc, argv,
351 "F:EI:RLJCB:H:N:o:k:i:n:hc:d:m:s:t:f:"))
352 != -1) {
353 fprintf(stderr, "\t-%c %s\n", ch, (optarg ? optarg : ""));
354 switch(ch) {
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;
366 break;
367 case 'I':
368 icache_size = a2i(optarg);
369 code |= generate_with_icache;
370 break;
371 case 'N':
372 generate_smp = a2i(optarg);
373 break;
374 case 'R':
375 code |= generate_with_icache;
376 code |= generate_with_direct_access_icache;
377 if (icache_size == 0)
378 icache_size = 1024;
379 break;
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));
384 break;
385 case 'H':
386 hi_bit_nr = a2i(optarg);
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;
395 break;
396 case 'i':
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);
402 fprintf(stderr, "\texpanding ...\n");
403 insn_table_expand_insns(instructions);
404 break;
405 case 'o':
406 decode_rules = load_decode_table(optarg, hi_bit_nr);
407 break;
408 case 'k':
409 cache_rules = load_cache_table(optarg, hi_bit_nr);
410 break;
411 case 'n':
412 real_file_name = strdup(optarg);
413 break;
414 case 'h':
415 is_header = 1;
416 break;
417 case 's':
418 case 'd':
419 case 'm':
420 case 't':
421 case 'f':
422 case 'c':
423 {
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);
428 ASSERT(instructions != NULL);
429 switch (ch) {
430 case 's':
431 if(is_header)
432 gen_semantics_h(instructions, file, code);
433 else
434 gen_semantics_c(instructions, cache_rules, file, code);
435 break;
436 case 'd':
437 if (is_header)
438 gen_idecode_h(file, instructions, cache_rules);
439 else
440 gen_idecode_c(file, instructions, cache_rules);
441 break;
442 case 'm':
443 if (is_header)
444 gen_model_h(instructions, file);
445 else
446 gen_model_c(instructions, file);
447 break;
448 case 't':
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);
459 break;
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);
465 break;
466 }
467 lf_print__file_finish(file);
468 lf_close(file);
469 is_header = 0;
470 }
471 real_file_name = NULL;
472 break;
473 default:
474 error("unknown option\n");
475 }
476 }
477 return 0;
478 }
This page took 0.038766 seconds and 5 git commands to generate.