1 /* This file is part of the program psim.
3 Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
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.
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.
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.
29 #include "ld-decode.h"
33 #include "gen-semantics.h"
34 #include "gen-idecode.h"
35 #include "gen-icache.h"
40 print_icache_function_header (lf *file,
42 const char *format_name,
43 opcode_bits *expanded_bits,
44 int is_function_definition,
45 int nr_prefetched_words)
47 lf_printf(file, "\n");
48 lf_print__function_type_function (file, print_icache_function_type,
49 "EXTERN_ICACHE", " ");
50 print_function_name (file,
51 basename, format_name, NULL,
53 function_name_prefix_icache);
54 lf_printf (file, "\n(");
55 print_icache_function_formal (file, nr_prefetched_words);
56 lf_printf (file, ")");
57 if (!is_function_definition)
58 lf_printf (file, ";");
59 lf_printf (file, "\n");
64 print_icache_declaration (lf *file,
66 opcode_bits *expanded_bits,
67 insn_opcodes *opcodes,
68 int nr_prefetched_words)
70 print_icache_function_header (file,
74 0/* is not function definition */,
81 print_icache_extraction (lf *file,
82 const char *format_name,
83 cache_entry_type cache_type,
84 const char *entry_name,
85 const char *entry_type,
86 const char *entry_expression,
87 char *single_insn_field,
89 insn_field_entry *cur_field,
90 opcode_bits *expanded_bits,
91 icache_decl_type what_to_declare,
92 icache_body_type what_to_do)
94 const char *expression;
97 ASSERT (format_name != NULL);
98 ASSERT (entry_name != NULL);
100 /* figure out exactly what should be going on here */
104 if ((what_to_do & put_values_in_icache)
105 || what_to_do == do_not_use_icache)
108 what_to_do = do_not_use_icache;
114 if ((what_to_do & get_values_from_icache)
115 || what_to_do == do_not_use_icache)
118 what_to_do = do_not_use_icache;
124 if ((what_to_declare != undef_variables)
125 || !(what_to_do & put_values_in_icache))
128 what_to_declare = ((what_to_do & put_values_in_icache)
137 /* For the type, default to a simple unsigned */
138 if (entry_type == NULL || strlen (entry_type) == 0)
139 entry_type = "unsigned";
141 /* look through the set of expanded sub fields to see if this field
142 has been given a constant value */
143 for (bits = expanded_bits;
147 if (bits->field == cur_field)
151 /* Define a storage area for the cache element */
152 switch (what_to_declare)
154 case undef_variables:
155 /* We've finished with the #define value - destory it */
156 lf_indent_suppress (file);
157 lf_printf (file, "#undef %s\n", entry_name);
159 case define_variables:
160 /* Using direct access for this entry, define it */
161 lf_indent_suppress (file);
162 lf_printf (file, "#define %s ((%s) ", entry_name, entry_type);
164 case declare_variables:
165 /* using variables to define the value */
167 lf_print__line_ref (file, line);
168 lf_printf (file, "%s const %s UNUSED = ", entry_type, entry_name);
173 /* define a value for that storage area as determined by what is in
176 && single_insn_field != NULL
177 && strcmp (entry_name, single_insn_field) == 0
178 && strcmp (entry_name, cur_field->val_string) == 0
179 && ((bits->opcode->is_boolean && bits->value == 0)
180 || (!bits->opcode->is_boolean)))
182 /* The cache rule is specifying what to do with a simple
185 Because of instruction expansion, the field is either a
186 constant value or equal to the specified constant (boolean
187 comparison). (The latter indicated by bits->value == 0).
189 The case of a field not being equal to the specified boolean
190 value is handled later. */
191 expression = "constant field";
192 ASSERT (bits->field == cur_field);
193 if (bits->opcode->is_boolean)
195 ASSERT (bits->value == 0);
196 lf_printf (file, "%d", bits->opcode->boolean_constant);
198 else if (bits->opcode->last < bits->field->last)
200 lf_printf (file, "%d",
201 bits->value << (bits->field->last - bits->opcode->last));
205 lf_printf (file, "%d", bits->value);
208 else if (bits != NULL
209 && single_insn_field != NULL
210 && strncmp (entry_name,
212 strlen (single_insn_field)) == 0
213 && strncmp (entry_name + strlen (single_insn_field),
215 strlen ("_is_")) == 0
216 && ((bits->opcode->is_boolean
217 && ((unsigned) atol (entry_name + strlen (single_insn_field) + strlen ("_is_"))
218 == bits->opcode->boolean_constant))
219 || (!bits->opcode->is_boolean)))
221 /* The cache rule defines an entry for the comparison between a
222 single instruction field and a constant. The value of the
223 comparison in someway matches that of the opcode field that
224 was made constant through expansion. */
225 expression = "constant compare";
226 if (bits->opcode->is_boolean)
228 lf_printf (file, "%d /* %s == %d */",
231 bits->opcode->boolean_constant);
233 else if (bits->opcode->last < bits->field->last)
235 lf_printf (file, "%d /* %s == %d */",
236 (atol (entry_name + strlen (single_insn_field) + strlen ("_is_"))
237 == (bits->value << (bits->field->last - bits->opcode->last))),
239 (bits->value << (bits->field->last - bits->opcode->last)));
243 lf_printf (file, "%d /* %s == %d */",
244 (atol (entry_name + strlen (single_insn_field) + strlen ("_is_"))
252 /* put the field in the local variable, possibly also enter it
254 expression = "extraction";
255 /* handle the cache */
256 if ((what_to_do & get_values_from_icache)
257 || (what_to_do & put_values_in_icache))
259 lf_printf (file, "cache_entry->crack.%s.%s",
262 if (what_to_do & put_values_in_icache) /* also put it in the cache? */
264 lf_printf (file, " = ");
267 if ((what_to_do & put_values_in_icache)
268 || what_to_do == do_not_use_icache)
270 if (cur_field != NULL)
272 if (entry_expression != NULL && strlen (entry_expression) > 0)
273 error (line, "Instruction field entry with nonempty expression\n");
274 if (cur_field->first == 0 && cur_field->last == options.insn_bit_size - 1)
275 lf_printf (file, "(instruction_%d)",
277 else if (cur_field->last == options.insn_bit_size - 1)
278 lf_printf (file, "MASKED%d (instruction_%d, %d, %d)",
279 options.insn_bit_size,
281 i2target (options.hi_bit_nr, cur_field->first),
282 i2target (options.hi_bit_nr, cur_field->last));
284 lf_printf (file, "EXTRACTED%d (instruction_%d, %d, %d)",
285 options.insn_bit_size,
287 i2target (options.hi_bit_nr, cur_field->first),
288 i2target (options.hi_bit_nr, cur_field->last));
292 lf_printf (file, "%s", entry_expression);
297 switch (what_to_declare)
299 case define_variables:
300 lf_printf (file, ")");
302 case undef_variables:
304 case declare_variables:
305 lf_printf (file, ";");
309 ASSERT (reason != NULL && expression != NULL);
310 lf_printf (file, " /* %s - %s */\n", reason, expression);
315 print_icache_body (lf *file,
316 insn_entry *instruction,
317 opcode_bits *expanded_bits,
318 cache_entry *cache_rules,
319 icache_decl_type what_to_declare,
320 icache_body_type what_to_do,
321 int nr_prefetched_words)
323 /* extract instruction fields */
324 lf_printf (file, "/* Extraction: %s\n", instruction->name);
325 lf_printf (file, " ");
326 switch (what_to_declare)
328 case define_variables:
329 lf_printf (file, "#define");
331 case declare_variables:
332 lf_printf (file, "declare");
334 case undef_variables:
335 lf_printf (file, "#undef");
338 lf_printf (file, " ");
341 case get_values_from_icache:
342 lf_printf (file, "get-values-from-icache");
344 case put_values_in_icache:
345 lf_printf (file, "put-values-in-icache");
347 case both_values_and_icache:
348 lf_printf (file, "get-values-from-icache|put-values-in-icache");
350 case do_not_use_icache:
351 lf_printf (file, "do-not-use-icache");
354 lf_printf (file, "\n ");
355 print_insn_words (file, instruction);
356 lf_printf(file, " */\n");
358 /* pass zero - fetch from memory any missing instructions.
360 Some of the instructions will have already been fetched (in the
361 instruction array), others will still need fetching. */
364 case get_values_from_icache:
366 case put_values_in_icache:
367 case both_values_and_icache:
368 case do_not_use_icache:
371 switch (what_to_declare)
373 case undef_variables:
375 case define_variables:
376 case declare_variables:
377 for (word_nr = nr_prefetched_words;
378 word_nr < instruction->nr_words;
381 /* FIXME - should be using print_icache_extraction? */
382 lf_printf (file, "%sinstruction_word instruction_%d UNUSED = ",
383 options.module.global.prefix.l,
385 lf_printf (file, "IMEM%d_IMMED (cia, %d)",
386 options.insn_bit_size, word_nr);
387 lf_printf (file, ";\n");
393 /* if putting the instruction words in the cache, define references
395 if (options.gen.insn_in_icache) {
396 /* FIXME: is the instruction_word type correct? */
397 print_icache_extraction (file,
398 instruction->format_name,
401 "instruction_word", /* type */
402 "instruction", /* expression */
409 lf_printf(file, "\n");
411 /* pass one - process instruction fields.
413 If there is no cache rule, the default is to enter the field into
416 insn_word_entry *word;
417 for (word = instruction->words;
421 insn_field_entry *cur_field;
422 for (cur_field = word->first;
423 cur_field->first < options.insn_bit_size;
424 cur_field = cur_field->next)
426 if (cur_field->type == insn_field_string)
428 cache_entry *cache_rule;
429 cache_entry_type value_type = cache_value;
430 line_ref *value_line = instruction->line;
431 /* check the cache table to see if it contains a rule
432 overriding the default cache action for an
434 for (cache_rule = cache_rules;
436 cache_rule = cache_rule->next)
438 if (filter_is_subset (instruction->field_names,
439 cache_rule->original_fields)
440 && strcmp (cache_rule->name, cur_field->val_string) == 0)
442 value_type = cache_rule->entry_type;
443 value_line = cache_rule->line;
444 if (value_type == compute_value)
446 options.warning (cache_rule->line,
447 "instruction field of type `compute' changed to `cache'\n");
448 cache_rule->entry_type = cache_value;
453 /* Define an entry for the field within the
455 print_icache_extraction (file,
456 instruction->format_name,
458 cur_field->val_string, /* name */
460 NULL, /* expression */
461 cur_field->val_string, /* insn field */
472 /* pass two - any cache fields not processed above */
474 cache_entry *cache_rule;
475 for (cache_rule = cache_rules;
477 cache_rule = cache_rule->next)
479 if (filter_is_subset (instruction->field_names,
480 cache_rule->original_fields)
481 && !filter_is_member (instruction->field_names,
484 char *single_field = filter_next (cache_rule->original_fields, "");
485 if (filter_next (cache_rule->original_fields, single_field) != NULL)
487 print_icache_extraction (file,
488 instruction->format_name,
489 cache_rule->entry_type,
492 cache_rule->expression,
495 NULL, /* cur_field */
503 lf_print__internal_ref (file);
508 typedef struct _form_fields form_fields;
509 struct _form_fields {
516 insn_table_cache_fields (insn_table *isa)
518 form_fields *forms = NULL;
520 for (insn = isa->insns;
523 form_fields **form = &forms;
528 /* new format name, add it */
529 form_fields *new_form = ZALLOC (form_fields);
530 new_form->name = insn->format_name;
531 filter_add (&new_form->fields, insn->field_names);
535 else if (strcmp ((*form)->name, insn->format_name) == 0)
537 /* already present, add field names to the existing list */
538 filter_add (&(*form)->fields, insn->field_names);
541 form = &(*form)->next;
550 print_icache_struct (lf *file,
552 cache_entry *cache_rules)
554 /* Create a list of all the different instruction formats with their
555 corresponding field names. */
556 form_fields *formats = insn_table_cache_fields (isa);
558 lf_printf (file, "\n");
559 lf_printf (file, "#define WITH_%sIDECODE_CACHE_SIZE %d\n",
560 options.module.global.prefix.u,
561 (options.gen.icache ? options.gen.icache_size : 0));
562 lf_printf (file, "\n");
564 /* create an instruction cache if being used */
565 if (options.gen.icache) {
566 lf_printf (file, "typedef struct _%sidecode_cache {\n",
567 options.module.global.prefix.l);
568 lf_indent (file, +2);
571 lf_printf (file, "unsigned_word address;\n");
572 lf_printf (file, "void *semantic;\n");
573 lf_printf (file, "union {\n");
574 lf_indent (file, +2);
575 for (format = formats;
577 format = format->next)
579 lf_printf (file, "struct {\n");
580 lf_indent (file, +2);
582 cache_entry *cache_rule;
584 /* space for any instruction words */
585 if (options.gen.insn_in_icache)
586 lf_printf (file, "instruction_word insn[%d];\n", isa->max_nr_words);
587 /* define an entry for any applicable cache rules */
588 for (cache_rule = cache_rules;
590 cache_rule = cache_rule->next)
592 /* nb - sort of correct - should really check against
593 individual instructions */
594 if (filter_is_subset (format->fields, cache_rule->original_fields))
597 lf_printf (file, "%s %s;",
598 (cache_rule->type == NULL
602 lf_printf (file, " /*");
603 for (memb = filter_next (cache_rule->original_fields, "");
605 memb = filter_next (cache_rule->original_fields, memb))
607 lf_printf (file, " %s", memb);
609 lf_printf (file, " */\n");
612 /* define an entry for any fields not covered by a cache rule */
613 for (field = filter_next (format->fields, "");
615 field = filter_next (format->fields, field))
617 cache_entry *cache_rule;
619 for (cache_rule = cache_rules;
621 cache_rule = cache_rule->next)
623 if (strcmp (cache_rule->name, field) == 0)
630 lf_printf (file, "unsigned %s; /* default */\n", field);
633 lf_indent (file, -2);
634 lf_printf (file, "} %s;\n", format->name);
636 lf_indent (file, -2);
637 lf_printf (file, "} crack;\n");
639 lf_indent (file, -2);
640 lf_printf (file, "} %sidecode_cache;\n", options.module.global.prefix.l);
644 /* alernativly, since no cache, emit a dummy definition for
645 idecode_cache so that code refering to the type can still compile */
646 lf_printf(file, "typedef void %sidecode_cache;\n",
647 options.module.global.prefix.l);
649 lf_printf (file, "\n");
655 print_icache_function (lf *file,
656 insn_entry *instruction,
657 opcode_bits *expanded_bits,
658 insn_opcodes *opcodes,
659 cache_entry *cache_rules,
660 int nr_prefetched_words)
664 /* generate code to enter decoded instruction into the icache */
665 lf_printf(file, "\n");
666 lf_print__function_type_function (file, print_icache_function_type,
667 "EXTERN_ICACHE", "\n");
668 indent = print_function_name (file,
670 instruction->format_name,
673 function_name_prefix_icache);
674 indent += lf_printf (file, " ");
675 lf_indent (file, +indent);
676 lf_printf (file, "(");
677 print_icache_function_formal (file, nr_prefetched_words);
678 lf_printf (file, ")\n");
679 lf_indent (file, -indent);
681 /* function header */
682 lf_printf (file, "{\n");
683 lf_indent (file, +2);
685 print_my_defines (file,
687 instruction->format_name,
689 print_itrace (file, instruction, 1/*putting-value-in-cache*/);
691 print_idecode_validate (file, instruction, opcodes);
693 lf_printf (file, "\n");
694 lf_printf (file, "{\n");
695 lf_indent (file, +2);
696 if (options.gen.semantic_icache)
697 lf_printf (file, "unsigned_word nia;\n");
698 print_icache_body (file,
702 (options.gen.direct_access
704 : declare_variables),
705 (options.gen.semantic_icache
706 ? both_values_and_icache
707 : put_values_in_icache),
708 nr_prefetched_words);
710 lf_printf (file, "\n");
711 lf_printf (file, "cache_entry->address = cia;\n");
712 lf_printf (file, "cache_entry->semantic = ");
713 print_function_name (file,
715 instruction->format_name,
718 function_name_prefix_semantics);
719 lf_printf (file, ";\n");
720 lf_printf (file, "\n");
722 if (options.gen.semantic_icache) {
723 lf_printf (file, "/* semantic routine */\n");
724 print_semantic_body (file,
728 lf_printf (file, "return nia;\n");
731 if (!options.gen.semantic_icache)
733 lf_printf (file, "/* return the function proper */\n");
734 lf_printf (file, "return ");
735 print_function_name (file,
737 instruction->format_name,
740 function_name_prefix_semantics);
741 lf_printf (file, ";\n");
744 if (options.gen.direct_access)
746 print_icache_body (file,
751 (options.gen.semantic_icache
752 ? both_values_and_icache
753 : put_values_in_icache),
754 nr_prefetched_words);
757 lf_indent (file, -2);
758 lf_printf (file, "}\n");
759 lf_indent (file, -2);
760 lf_printf (file, "}\n");
765 print_icache_definition (lf *file,
767 opcode_bits *expanded_bits,
768 insn_opcodes *opcodes,
769 cache_entry *cache_rules,
770 int nr_prefetched_words)
772 print_icache_function (file,
777 nr_prefetched_words);
783 print_icache_internal_function_declaration (lf *file,
784 function_entry *function,
787 ASSERT (options.gen.icache);
788 if (function->is_internal)
790 lf_printf (file, "\n");
791 lf_print__function_type_function (file, print_icache_function_type,
792 "INLINE_ICACHE", "\n");
793 print_function_name (file,
798 function_name_prefix_icache);
799 lf_printf (file, "\n(");
800 print_icache_function_formal (file, 0);
801 lf_printf (file, ");\n");
807 print_icache_internal_function_definition (lf *file,
808 function_entry *function,
811 ASSERT (options.gen.icache);
812 if (function->is_internal)
814 lf_printf (file, "\n");
815 lf_print__function_type_function (file, print_icache_function_type,
816 "INLINE_ICACHE", "\n");
817 print_function_name (file,
822 function_name_prefix_icache);
823 lf_printf (file, "\n(");
824 print_icache_function_formal (file, 0);
825 lf_printf (file, ")\n");
826 lf_printf (file, "{\n");
827 lf_indent (file, +2);
828 lf_printf (file, "/* semantic routine */\n");
829 if (options.gen.semantic_icache)
831 lf_print__line_ref (file, function->code->line);
832 table_print_code (file, function->code);
833 lf_printf (file, "error (\"Internal function must longjump\\n\");\n");
834 lf_printf (file, "return 0;\n");
838 lf_printf (file, "return ");
839 print_function_name (file,
844 function_name_prefix_semantics);
845 lf_printf (file, ";\n");
848 lf_print__internal_ref (file);
849 lf_indent (file, -2);
850 lf_printf (file, "}\n");