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.
28 #include "ld-decode.h"
32 #include "gen-idecode.h"
33 #include "gen-icache.h"
34 #include "gen-semantics.h"
39 lf_print_opcodes (lf *file,
46 ASSERT (table->opcode != NULL);
47 lf_printf (file, "_%d_%d",
50 if (table->parent == NULL) break;
51 lf_printf (file, "__%d", table->opcode_nr);
52 table = table->parent;
61 print_idecode_ifetch (lf *file,
62 int previous_nr_prefetched_words,
63 int current_nr_prefetched_words)
66 for (word_nr = previous_nr_prefetched_words;
67 word_nr < current_nr_prefetched_words;
70 lf_printf (file, "instruction_word instruction_%d = IMEM%d_IMMED (cia, %d);\n",
71 word_nr, options.insn_bit_size, word_nr);
78 /****************************************************************/
82 lf_print_table_name (lf *file,
85 lf_printf (file, "idecode_table");
86 lf_print_opcodes (file, table);
92 print_idecode_table (lf *file,
96 lf_printf (file, "/* prime the search */\n");
97 lf_printf (file, "idecode_table_entry *table = ");
98 lf_print_table_name (file, entry);
99 lf_printf (file, ";\n");
100 lf_printf (file, "int opcode = EXTRACTED%d (instruction, %d, %d);\n",
101 options.insn_bit_size,
102 i2target (options.hi_bit_nr, entry->opcode->first),
103 i2target (options.hi_bit_nr, entry->opcode->last));
104 lf_printf (file, "idecode_table_entry *table_entry = table + opcode;\n");
106 lf_printf (file, "\n");
107 lf_printf (file, "/* iterate until a leaf */\n");
108 lf_printf (file, "while (1) {\n");
109 lf_printf (file, " signed shift = table_entry->shift;\n");
110 lf_printf (file, "if (shift == function_entry) break;\n");
111 lf_printf (file, " if (shift >= 0) {\n");
112 lf_printf (file, " table = ((idecode_table_entry*)\n");
113 lf_printf (file, " table_entry->function_or_table);\n");
114 lf_printf (file, " opcode = ((instruction & table_entry->mask)\n");
115 lf_printf (file, " >> shift);\n");
116 lf_printf (file, " table_entry = table + opcode;\n");
117 lf_printf (file, " }\n");
118 lf_printf (file, " else {\n");
119 lf_printf (file, " /* must be a boolean */\n");
120 lf_printf (file, " ASSERT(table_entry->shift == boolean_entry);\n");
121 lf_printf (file, " opcode = ((instruction & table_entry->mask)\n");
122 lf_printf (file, " != table_entry->value);\n");
123 lf_printf (file, " table = ((idecode_table_entry*)\n");
124 lf_printf (file, " table_entry->function_or_table);\n");
125 lf_printf (file, " table_entry = table + opcode;\n");
126 lf_printf (file, " }\n");
127 lf_printf (file, "}\n");
129 lf_printf (file, "\n");
130 lf_printf (file, "/* call the leaf code */\n");
131 if (options.gen.code == generate_jumps)
133 lf_printf (file, "goto *table_entry->function_or_table;\n");
137 lf_printf (file, "%s ", result);
138 if (options.gen.icache)
140 lf_printf (file, "(((idecode_icache*)table_entry->function_or_table)\n");
141 lf_printf (file, " (");
142 print_icache_function_actual (file, 1);
143 lf_printf (file, "));\n");
147 lf_printf (file, "((idecode_semantic*)table_entry->function_or_table)\n");
148 lf_printf (file, " (");
149 print_semantic_function_actual (file, 1);
150 lf_printf (file, ");\n");
157 print_idecode_table_start (lf *file,
163 /* start of the table */
164 if (table->opcode_rule->gen == array_gen)
166 lf_printf (file, "\n");
167 lf_printf (file, "static idecode_table_entry ");
168 lf_print_table_name (file, table);
169 lf_printf (file, "[] = {\n");
174 print_idecode_table_leaf (lf *file,
179 gen_entry *master_entry;
180 ASSERT (entry->parent != NULL);
182 if (entry->combined_parent == NULL)
183 master_entry = entry;
185 master_entry = entry->combined_parent;
187 /* add an entry to the table */
188 if (entry->parent->opcode_rule->gen == array_gen)
190 lf_printf (file, " /*%d*/ { ", entry->opcode_nr);
191 if (entry->opcode == NULL)
193 ASSERT (entry->nr_insns == 1);
194 /* table leaf entry */
195 lf_printf (file, "function_entry, 0, 0, ");
196 if (options.gen.code == generate_jumps)
198 lf_printf (file, "&&");
200 print_function_name (file,
201 entry->insns->insn->name,
202 entry->insns->insn->format_name,
204 master_entry->expanded_bits,
206 ? function_name_prefix_icache
207 : function_name_prefix_semantics));
209 else if (entry->opcode_rule->gen == switch_gen
210 || entry->opcode_rule->gen == goto_switch_gen
211 || entry->opcode_rule->gen == padded_switch_gen)
213 /* table calling switch statement */
214 lf_printf (file, "function_entry, 0, 0, ");
215 if (options.gen.code == generate_jumps)
217 lf_printf (file, "&&");
219 lf_print_table_name (file, entry);
221 else if (entry->opcode->is_boolean)
223 /* table `calling' boolean table */
224 lf_printf (file, "boolean_entry, ");
225 lf_printf (file, "MASK32(%d, %d), ",
226 i2target (options.hi_bit_nr, entry->opcode->first),
227 i2target (options.hi_bit_nr, entry->opcode->last));
228 lf_printf (file, "INSERTED32(%d, %d, %d), ",
229 entry->opcode->boolean_constant,
230 i2target (options.hi_bit_nr, entry->opcode->first),
231 i2target (options.hi_bit_nr, entry->opcode->last));
232 lf_print_table_name (file, entry);
236 /* table `calling' another table */
237 lf_printf (file, "%d, ", options.insn_bit_size - entry->opcode->last - 1);
238 lf_printf (file, "MASK%d(%d,%d), ",
239 options.insn_bit_size,
240 i2target (options.hi_bit_nr, entry->opcode->first),
241 i2target (options.hi_bit_nr, entry->opcode->last));
242 lf_printf (file, "0, ");
243 lf_print_table_name (file, entry);
245 lf_printf (file, " },\n");
250 print_idecode_table_end (lf *file,
256 if (table->opcode_rule->gen == array_gen) {
257 lf_printf (file, "};\n");
261 /****************************************************************/
265 print_goto_switch_name (lf *file,
268 lf_printf (file, "case_");
269 if (entry->opcode == NULL)
271 print_function_name (file,
272 entry->insns->insn->name,
273 entry->insns->insn->format_name,
275 entry->expanded_bits,
277 ? function_name_prefix_icache
278 : function_name_prefix_semantics));
282 lf_print_table_name(file, entry);
287 print_goto_switch_table_leaf (lf *file,
292 ASSERT (entry->parent != NULL);
294 ASSERT (entry->parent->opcode_rule->gen == goto_switch_gen);
295 ASSERT (entry->parent->opcode);
297 lf_printf (file, "/* %d */ &&", entry->opcode_nr);
298 if (entry->combined_parent != NULL)
299 print_goto_switch_name (file, entry->combined_parent);
301 print_goto_switch_name (file, entry);
302 lf_printf (file, ",\n");
306 print_goto_switch_break (lf *file,
309 lf_printf (file, "goto break_");
310 lf_print_table_name (file, entry->parent);
311 lf_printf (file, ";\n");
316 print_goto_switch_table (lf *file,
319 lf_printf (file, "const static void *");
320 lf_print_table_name (file, table);
321 lf_printf (file, "[] = {\n");
322 lf_indent (file, +2);
323 gen_entry_traverse_tree (file, table,
326 print_goto_switch_table_leaf,
329 lf_indent (file, -2);
330 lf_printf (file, "};\n");
334 void print_idecode_switch
340 print_idecode_switch_start (lf *file,
345 /* const char *result = data; */
347 ASSERT (table->opcode_rule->gen == switch_gen
348 || table->opcode_rule->gen == goto_switch_gen
349 || table->opcode_rule->gen == padded_switch_gen);
351 if (table->opcode->is_boolean
352 || table->opcode_rule->gen == switch_gen
353 || table->opcode_rule->gen == padded_switch_gen)
355 lf_printf (file, "switch (EXTRACTED%d (instruction_%d, %d, %d))\n",
356 options.insn_bit_size,
357 table->opcode_rule->word_nr,
358 i2target (options.hi_bit_nr, table->opcode->first),
359 i2target (options.hi_bit_nr, table->opcode->last));
360 lf_indent (file, +2);
361 lf_printf (file, "{\n");
363 else if (table->opcode_rule->gen == goto_switch_gen)
365 if (table->parent != NULL
366 && (table->parent->opcode_rule->gen == switch_gen
367 || table->parent->opcode_rule->gen == goto_switch_gen
368 || table->parent->opcode_rule->gen == padded_switch_gen))
370 lf_printf (file, "{\n");
371 lf_indent (file, +2);
373 print_goto_switch_table (file, table);
374 lf_printf (file, "ASSERT (EXTRACTED%d (instruction_%d, %d, %d)\n",
375 options.insn_bit_size,
376 table->opcode->word_nr,
377 i2target (options.hi_bit_nr, table->opcode->first),
378 i2target (options.hi_bit_nr, table->opcode->last));
379 lf_printf (file, " < (sizeof (");
380 lf_print_table_name (file, table);
381 lf_printf (file, ") / sizeof(void*)));\n");
382 lf_printf (file, "goto *");
383 lf_print_table_name (file, table);
384 lf_printf (file, "[EXTRACTED%d (instruction_%d, %d, %d)];\n",
385 options.insn_bit_size,
386 table->opcode->word_nr,
387 i2target (options.hi_bit_nr, table->opcode->first),
388 i2target (options.hi_bit_nr, table->opcode->last));
392 ASSERT("bad switch" == NULL);
398 print_idecode_switch_leaf (lf *file,
403 const char *result = data;
404 ASSERT (entry->parent != NULL);
406 ASSERT (entry->parent->opcode_rule->gen == switch_gen
407 || entry->parent->opcode_rule->gen == goto_switch_gen
408 || entry->parent->opcode_rule->gen == padded_switch_gen);
409 ASSERT (entry->parent->opcode);
411 /* skip over any instructions combined into another entry */
412 if (entry->combined_parent != NULL)
415 if (entry->parent->opcode->is_boolean
416 && entry->opcode_nr == 0)
418 /* case: boolean false target */
419 lf_printf (file, "case %d:\n", entry->parent->opcode->boolean_constant);
421 else if (entry->parent->opcode->is_boolean
422 && entry->opcode_nr != 0)
424 /* case: boolean true case */
425 lf_printf (file, "default:\n");
427 else if (entry->parent->opcode_rule->gen == switch_gen
428 || entry->parent->opcode_rule->gen == padded_switch_gen)
430 /* case: <opcode-nr> - switch */
432 for (cob = entry; cob != NULL; cob = cob->combined_next)
433 lf_printf (file, "case %d:\n", cob->opcode_nr);
435 else if (entry->parent->opcode_rule->gen == goto_switch_gen)
437 /* case: <opcode-nr> - goto-switch */
438 print_goto_switch_name (file, entry);
439 lf_printf (file, ":\n");
443 ERROR ("bad switch");
445 lf_printf (file, " {\n");
446 lf_indent (file, +4);
448 if (entry->opcode == NULL)
450 /* switch calling leaf */
451 ASSERT (entry->nr_insns == 1);
452 print_idecode_ifetch (file, entry->nr_prefetched_words,
453 entry->insns->semantic->nr_prefetched_words);
454 switch (options.gen.code)
457 lf_printf (file, "goto ");
460 lf_printf (file, "%s", result);
463 print_function_name (file,
464 entry->insns->insn->name,
465 entry->insns->insn->format_name,
467 entry->expanded_bits,
469 ? function_name_prefix_icache
470 : function_name_prefix_semantics));
471 if (options.gen.code == generate_calls)
473 lf_printf (file, " (");
474 print_semantic_function_actual (file, entry->insns->semantic->nr_prefetched_words);
475 lf_printf (file, ")");
477 lf_printf (file, ";\n");
479 else if (entry->opcode_rule->gen == switch_gen
480 || entry->opcode_rule->gen == goto_switch_gen
481 || entry->opcode_rule->gen == padded_switch_gen)
483 /* switch calling switch */
484 lf_printf (file, "{\n");
485 lf_indent (file, +2);
486 print_idecode_ifetch (file, entry->parent->nr_prefetched_words,
487 entry->nr_prefetched_words);
488 print_idecode_switch (file, entry, result);
489 lf_indent (file, -2);
490 lf_printf (file, "}\n");
494 /* switch looking up a table */
495 lf_printf (file, "{\n");
496 lf_indent (file, +2);
497 print_idecode_ifetch (file, entry->parent->nr_prefetched_words,
498 entry->nr_prefetched_words);
499 print_idecode_table (file, entry, result);
500 lf_indent (file, -2);
501 lf_printf (file, "}\n");
503 if (entry->parent->opcode->is_boolean
504 || entry->parent->opcode_rule->gen == switch_gen
505 || entry->parent->opcode_rule->gen == padded_switch_gen)
507 lf_printf (file, "break;\n");
509 else if (entry->parent->opcode_rule->gen == goto_switch_gen)
511 print_goto_switch_break(file, entry);
515 ERROR ("bad switch");
518 lf_indent (file, -4);
519 lf_printf (file, " }\n");
524 print_idecode_switch_illegal (lf *file,
527 lf_indent (file, +2);
528 print_idecode_invalid (file, result, invalid_illegal);
529 lf_printf (file, "break;\n");
530 lf_indent (file, -2);
534 print_idecode_switch_end (lf *file,
539 const char *result = data;
541 ASSERT (table->opcode_rule->gen == switch_gen
542 || table->opcode_rule->gen == goto_switch_gen
543 || table->opcode_rule->gen == padded_switch_gen);
544 ASSERT (table->opcode);
546 if (table->opcode->is_boolean)
548 lf_printf (file, "}\n");
549 lf_indent (file, -2);
551 else if (table->opcode_rule->gen == switch_gen
552 || table->opcode_rule->gen == padded_switch_gen)
554 lf_printf (file, "default:\n");
555 lf_indent (file, +2);
556 if (table->nr_entries == table->opcode->nr_opcodes)
558 print_sim_engine_abort (file, "Internal error - bad switch generated");
559 lf_printf (file, "%sNULL_CIA;\n", result);
560 lf_printf (file, "break;\n");
564 print_idecode_switch_illegal (file, result);
566 lf_indent (file, -2);
567 lf_printf (file, "}\n");
568 lf_indent (file, -2);
570 else if (table->opcode_rule->gen == goto_switch_gen)
572 lf_printf (file, "illegal_");
573 lf_print_table_name (file, table);
574 lf_printf (file, ":\n");
575 print_idecode_invalid (file, result, invalid_illegal);
576 lf_printf (file, "break_");
577 lf_print_table_name(file, table);
578 lf_printf (file, ":;\n");
579 if (table->parent != NULL
580 && (table->parent->opcode_rule->gen == switch_gen
581 || table->parent->opcode_rule->gen == goto_switch_gen
582 || table->parent->opcode_rule->gen == padded_switch_gen))
584 lf_indent (file, -2);
585 lf_printf (file, "}\n");
590 ERROR ("bad switch");
596 print_idecode_switch (lf *file,
600 gen_entry_traverse_tree (file, table,
602 print_idecode_switch_start,
603 print_idecode_switch_leaf,
604 print_idecode_switch_end,
610 print_idecode_switch_function_header (lf *file,
612 int is_function_definition,
613 int nr_prefetched_words)
615 lf_printf (file, "\n");
616 if (options.gen.code == generate_calls)
618 lf_printf (file, "static ");
619 if (options.gen.icache)
621 lf_printf (file, "idecode_semantic *");
625 lf_printf (file, "unsigned_word");
627 if (is_function_definition)
629 lf_printf (file, "\n");
633 lf_printf (file, " ");
635 lf_print_table_name (file, table);
636 lf_printf (file, "\n(");
637 print_icache_function_formal (file, nr_prefetched_words);
638 lf_printf (file, ")");
639 if (!is_function_definition)
641 lf_printf (file, ";");
643 lf_printf (file, "\n");
645 if (options.gen.code == generate_jumps && is_function_definition)
647 lf_indent (file, -1);
648 lf_print_table_name (file, table);
649 lf_printf (file, ":\n");
650 lf_indent (file, +1);
656 idecode_declare_if_switch (lf *file,
661 if ((table->opcode_rule->gen == switch_gen
662 || table->opcode_rule->gen == goto_switch_gen
663 || table->opcode_rule->gen == padded_switch_gen)
664 && table->parent != NULL /* don't declare the top one yet */
665 && table->parent->opcode_rule->gen == array_gen)
667 print_idecode_switch_function_header (file,
669 0/*isnt function definition*/,
676 idecode_expand_if_switch (lf *file,
681 if ((table->opcode_rule->gen == switch_gen
682 || table->opcode_rule->gen == goto_switch_gen
683 || table->opcode_rule->gen == padded_switch_gen)
684 && table->parent != NULL /* don't expand the top one yet */
685 && table->parent->opcode_rule->gen == array_gen)
687 print_idecode_switch_function_header(file,
689 1/*is function definition*/,
691 if (options.gen.code == generate_calls)
693 lf_printf (file, "{\n");
694 lf_indent (file, +2);
696 print_idecode_switch(file, table, "return");
697 if (options.gen.code == generate_calls)
699 lf_indent (file, -2);
700 lf_printf (file, "}\n");
706 /****************************************************************/
710 print_idecode_lookups (lf *file,
712 cache_entry *cache_rules)
716 /* output switch function declarations where needed by tables */
717 gen_entry_traverse_tree (file, table,
719 idecode_declare_if_switch, /* START */
723 /* output tables where needed */
724 for (depth = gen_entry_depth (table);
728 gen_entry_traverse_tree (file, table,
730 print_idecode_table_start,
731 print_idecode_table_leaf,
732 print_idecode_table_end,
736 /* output switch functions where needed */
737 gen_entry_traverse_tree (file, table,
739 idecode_expand_if_switch, /* START */
746 print_idecode_body (lf *file,
750 if (table->opcode_rule->gen == switch_gen
751 || table->opcode_rule->gen == goto_switch_gen
752 || table->opcode_rule->gen == padded_switch_gen)
754 print_idecode_switch (file, table, result);
758 print_idecode_table (file, table, result);
763 /****************************************************************/
767 print_jump (lf *file,
772 lf_putstr (file, "if (keep_running != NULL && !*keep_running)\n");
773 lf_putstr (file, " cpu_halt(cpu, nia, was_continuing, 0/*na*/);\n");
776 if (!options.generate_smp)
778 lf_putstr (file, "if (WITH_EVENTS) {\n");
779 lf_putstr (file, " if (event_queue_tick(events)) {\n");
780 lf_putstr (file, " cpu_set_program_counter(cpu, nia);\n");
781 lf_putstr (file, " event_queue_process(events);\n");
782 lf_putstr (file, " nia = cpu_get_program_counter(cpu);\n");
783 lf_putstr (file, " }\n");
784 lf_putstr (file, "}\n");
787 if (options.generate_smp)
791 lf_putstr (file, "cpu_set_program_counter(cpu, nia);\n");
793 lf_putstr (file, "if (WITH_EVENTS) {\n");
794 lf_putstr (file, " current_cpu += 1;\n");
795 lf_putstr (file, " if (current_cpu >= nr_cpus) {\n");
796 lf_putstr (file, " if (event_queue_tick(events)) {\n");
797 lf_putstr (file, " event_queue_process(events);\n");
798 lf_putstr (file, " }\n");
799 lf_putstr (file, " current_cpu = 0;\n");
800 lf_putstr (file, " }\n");
801 lf_putstr (file, "}\n");
802 lf_putstr (file, "else {\n");
803 lf_putstr (file, " current_cpu = (current_cpu + 1) % nr_cpus;\n");
804 lf_putstr (file, "}\n");
805 lf_putstr (file, "cpu = cpus[current_cpu];\n");
806 lf_putstr (file, "nia = cpu_get_program_counter(cpu);\n");
809 if (options.gen.icache)
811 lf_putstr (file, "cache_entry = cpu_icache_entry(cpu, nia);\n");
812 lf_putstr (file, "if (cache_entry->address == nia) {\n");
813 lf_putstr (file, " /* cache hit */\n");
814 lf_putstr (file, " goto *cache_entry->semantic;\n");
815 lf_putstr (file, "}\n");
818 lf_putstr (file, "goto cache_miss;\n");
822 if (!options.gen.icache && is_tail)
824 lf_printf (file, "goto idecode;\n");
834 print_jump_insn (lf *file,
835 insn_entry *instruction,
836 insn_bits *expanded_bits,
837 opcode_field *opcodes,
838 cache_entry *cache_rules)
841 /* what we are for the moment */
842 lf_printf (file, "\n");
843 print_my_defines (file, expanded_bits, instruction->name);
845 /* output the icache entry */
846 if (options.gen.icache)
848 lf_printf (file, "\n");
849 lf_indent (file, -1);
850 print_function_name (file,
853 function_name_prefix_icache);
854 lf_printf (file, ":\n");
855 lf_indent (file, +1);
856 lf_printf (file, "{\n");
857 lf_indent (file, +2);
858 lf_putstr (file, "const unsigned_word cia = nia;\n");
859 print_itrace (file, instruction, 1/*putting-value-in-cache*/);
860 print_idecode_validate (file, instruction, opcodes);
861 lf_printf (file, "\n");
862 lf_printf (file, "{\n");
863 lf_indent (file, +2);
864 print_icache_body (file,
869 put_values_in_icache);
870 lf_printf (file, "cache_entry->address = nia;\n");
871 lf_printf (file, "cache_entry->semantic = &&");
872 print_function_name (file,
875 function_name_prefix_semantics);
876 lf_printf (file, ";\n");
877 if (options.gen.semantic_icache)
879 print_semantic_body (file,
883 print_jump (file, 1/*is-tail*/);
887 lf_printf (file, "/* goto ");
888 print_function_name (file,
891 function_name_prefix_semantics);
892 lf_printf (file, "; */\n");
894 lf_indent (file, -2);
895 lf_putstr (file, "}\n");
896 lf_indent (file, -2);
897 lf_printf (file, "}\n");
900 /* print the semantics */
901 lf_printf (file, "\n");
902 lf_indent (file, -1);
903 print_function_name (file,
906 function_name_prefix_semantics);
907 lf_printf (file, ":\n");
908 lf_indent (file, +1);
909 lf_printf (file, "{\n");
910 lf_indent (file, +2);
911 lf_putstr (file, "const unsigned_word cia = nia;\n");
912 print_icache_body (file,
916 (options.gen.direct_access
918 : declare_variables),
920 ? get_values_from_icache
921 : do_not_use_icache));
922 print_semantic_body (file,
926 if (options.gen.direct_access)
927 print_icache_body (file,
933 ? get_values_from_icache
934 : do_not_use_icache));
935 print_jump (file, 1/*is tail*/);
936 lf_indent (file, -2);
937 lf_printf (file, "}\n");
944 print_jump_definition (lf *file,
950 cache_entry *cache_rules = (cache_entry*)data;
951 if (options.generate_expanded_instructions)
953 ASSERT (entry->nr_insns == 1
954 && entry->opcode == NULL
955 && entry->parent != NULL
956 && entry->parent->opcode != NULL);
957 ASSERT (entry->nr_insns == 1
958 && entry->opcode == NULL
959 && entry->parent != NULL
960 && entry->parent->opcode != NULL
961 && entry->parent->opcode_rule != NULL);
962 print_jump_insn (file,
963 entry->insns->words[0]->insn,
964 entry->expanded_bits,
970 print_jump_insn (file,
971 instruction->words[0]->insn,
981 print_jump_internal_function (lf *file,
983 function_entry *function,
986 if (function->is_internal)
988 lf_printf (file, "\n");
989 lf_print__line_ref (file, function->line);
990 lf_indent (file, -1);
991 print_function_name (file,
995 ? function_name_prefix_icache
996 : function_name_prefix_semantics));
997 lf_printf (file, ":\n");
998 lf_indent (file, +1);
999 lf_printf (file, "{\n");
1000 lf_indent (file, +2);
1001 lf_printf (file, "const unsigned_word cia = nia;\n");
1002 table_print_code (file, function->code);
1003 lf_print__internal_ref (file);
1004 print_sim_engine_abort (file, "Internal function must longjump");
1005 lf_indent (file, -2);
1006 lf_printf (file, "}\n");
1015 print_jump_until_stop_body(lf *file,
1017 cache_table *cache_rules)
1019 lf_printf (file, "{\n");
1020 lf_indent (file, +2);
1021 lf_putstr (file, "jmp_buf halt;\n");
1022 lf_putstr (file, "jmp_buf restart;\n");
1023 lf_putstr (file, "sim_cpu *cpu = NULL;\n");
1024 lf_putstr (file, "unsigned_word nia = -1;\n");
1025 lf_putstr (file, "instruction_word instruction = 0;\n");
1026 if ((code & generate_with_icache)) {
1027 lf_putstr (file, "idecode_cache *cache_entry = NULL;\n");
1030 lf_putstr (file, "int current_cpu = -1;\n");
1033 /* all the switches and tables - they know about jumping */
1034 print_idecode_lookups(file, table, cache_rules);
1036 /* start the simulation up */
1037 if ((code & generate_with_icache)) {
1038 lf_putstr (file, "\n");
1039 lf_putstr (file, "{\n");
1040 lf_putstr (file, " int cpu_nr;\n");
1041 lf_putstr (file, " for (cpu_nr = 0; cpu_nr < nr_cpus; cpu_nr++)\n");
1042 lf_putstr (file, " cpu_flush_icache(cpus[cpu_nr]);\n");
1043 lf_putstr (file, "}\n");
1046 lf_putstr (file, "\n");
1047 lf_putstr (file, "psim_set_halt_and_restart(system, &halt, &restart);\n");
1049 lf_putstr (file, "\n");
1050 lf_putstr (file, "if (setjmp(halt))\n");
1051 lf_putstr (file, " return;\n");
1053 lf_putstr (file, "\n");
1054 lf_putstr (file, "setjmp(restart);\n");
1056 lf_putstr (file, "\n");
1057 if (!generate_smp) {
1058 lf_putstr (file, "cpu = cpus[0];\n");
1059 lf_putstr (file, "nia = cpu_get_program_counter(cpu);\n");
1062 lf_putstr (file, "current_cpu = psim_last_cpu(system);\n");
1065 if (!(code & generate_with_icache)) {
1066 lf_printf (file, "\n");
1067 lf_indent (file, -1);
1068 lf_printf (file, "idecode:\n");
1069 lf_indent (file, +1);
1072 print_jump(file, 0/*is_tail*/);
1074 if ((code & generate_with_icache)) {
1075 lf_indent (file, -1);
1076 lf_printf (file, "cache_miss:\n");
1077 lf_indent (file, +1);
1080 lf_putstr (file, "instruction\n");
1081 lf_putstr (file, " = vm_instruction_map_read(cpu_instruction_map(cpu),\n");
1082 lf_putstr (file, " cpu, nia);\n");
1083 print_idecode_body(file, table, "/*IGORE*/");
1085 /* print out a table of all the internals functions */
1086 insn_table_traverse_function(table,
1088 print_jump_internal_function);
1090 /* print out a table of all the instructions */
1091 if (generate_expanded_instructions)
1092 insn_table_traverse_tree(table,
1096 print_jump_definition, /* leaf */
1098 NULL); /* padding */
1100 insn_table_traverse_insn(table,
1102 print_jump_definition);
1103 lf_indent (file, -2);
1104 lf_printf (file, "}\n");
1108 /****************************************************************/
1112 /* Output code to do any final checks on the decoded instruction.
1113 This includes things like verifying any on decoded fields have the
1114 correct value and checking that (for floating point) floating point
1115 hardware isn't disabled */
1118 print_idecode_validate (lf *file,
1119 insn_entry *instruction,
1120 insn_opcodes *opcode_paths)
1122 /* Validate: unchecked instruction fields
1124 If any constant fields in the instruction were not checked by the
1125 idecode tables, output code to check that they have the correct
1130 lf_printf (file, "\n");
1131 lf_indent_suppress (file);
1132 lf_printf (file, "#if defined (WITH_RESERVED_BITS)\n");
1133 lf_printf (file, "/* validate: ");
1134 print_insn_words (file, instruction);
1135 lf_printf (file, " */\n");
1136 for (word_nr = 0; word_nr < instruction->nr_words; word_nr++)
1138 insn_uint check_mask = 0;
1139 insn_uint check_val = 0;
1140 insn_word_entry *word = instruction->word[word_nr];
1143 /* form check_mask/check_val containing what needs to be checked
1144 in the instruction */
1145 for (bit_nr = 0; bit_nr < options.insn_bit_size; bit_nr++)
1147 insn_bit_entry *bit = word->bit[bit_nr];
1148 insn_field_entry *field = bit->field;
1150 /* Make space for the next bit */
1154 /* Only need to validate constant (and reserved)
1155 bits. Skip any others */
1156 if (field->type != insn_field_int
1157 && field->type != insn_field_reserved)
1160 /* Look through the list of opcode paths that lead to this
1161 instruction. See if any have failed to check the
1163 if (opcode_paths != NULL)
1165 insn_opcodes *entry;
1166 for (entry = opcode_paths;
1168 entry = entry->next)
1170 opcode_field *opcode;
1171 for (opcode = entry->opcode;
1173 opcode = opcode->parent)
1175 if (opcode->word_nr == word_nr
1176 && opcode->first <= bit_nr
1177 && opcode->last >= bit_nr)
1178 /* we've decoded on this bit */
1182 /* the bit wasn't decoded on */
1186 /* all the opcode paths decoded on BIT_NR, no need
1192 check_val |= bit->value;
1195 /* if any bits not checked by opcode tables, output code to check them */
1200 lf_printf (file, "if (WITH_RESERVED_BITS)\n");
1201 lf_printf (file, " {\n");
1202 lf_indent (file, +4);
1205 if (options.insn_bit_size > 32)
1207 lf_printf (file, "if ((instruction_%d\n", word_nr);
1208 lf_printf (file, " & UNSIGNED64 (0x%08lx%08lx))\n",
1209 (unsigned long)(check_mask >> 32),
1210 (unsigned long)(check_mask));
1211 lf_printf (file, " != UNSIGNED64 (0x%08lx%08lx))\n",
1212 (unsigned long)(check_val >> 32),
1213 (unsigned long)(check_val));
1217 lf_printf (file, "if ((instruction_%d & 0x%08lx) != 0x%08lx)\n",
1219 (unsigned long)(check_mask),
1220 (unsigned long)(check_val));
1222 lf_indent (file, +2);
1223 print_idecode_invalid (file, "return", invalid_illegal);
1224 lf_indent (file, -2);
1229 lf_indent (file, -4);
1230 lf_printf (file, " }\n");
1232 lf_indent_suppress(file);
1233 lf_printf (file, "#endif\n");
1236 /* Validate: Floating Point hardware
1238 If the simulator is being built with out floating point hardware
1239 (different to it being disabled in the MSR) then floating point
1240 instructions are invalid */
1242 if (filter_is_member (instruction->flags, "f"))
1244 lf_printf (file, "\n");
1245 lf_indent_suppress (file);
1246 lf_printf (file, "#if defined(CURRENT_FLOATING_POINT)\n");
1247 lf_printf (file, "/* Validate: FP hardware exists */\n");
1248 lf_printf (file, "if (CURRENT_FLOATING_POINT != HARD_FLOATING_POINT) {\n");
1249 lf_indent (file, +2);
1250 print_idecode_invalid (file, "return", invalid_illegal);
1251 lf_indent (file, -2);
1252 lf_printf (file, "}\n");
1253 lf_indent_suppress (file);
1254 lf_printf (file, "#endif\n");
1258 /* Validate: Floating Point available
1260 If floating point is not available, we enter a floating point
1261 unavailable interrupt into the cache instead of the instruction
1264 The PowerPC spec requires a CSI after MSR[FP] is changed and when
1265 ever a CSI occures we flush the instruction cache. */
1268 if (filter_is_member (instruction->flags, "f"))
1270 lf_printf (file, "\n");
1271 lf_indent_suppress (file);
1272 lf_printf (file, "#if defined(IS_FP_AVAILABLE)\n");
1273 lf_printf (file, "/* Validate: FP available according to cpu */\n");
1274 lf_printf (file, "if (!IS_FP_AVAILABLE) {\n");
1275 lf_indent (file, +2);
1276 print_idecode_invalid (file, "return", invalid_fp_unavailable);
1277 lf_indent (file, -2);
1278 lf_printf (file, "}\n");
1279 lf_indent_suppress (file);
1280 lf_printf (file, "#endif\n");
1284 /* Validate: Validate Instruction in correct slot
1286 Some architectures place restrictions on the slot that an
1287 instruction can be issued in */
1290 if (filter_is_member (instruction->options, "s")
1291 || options.gen.slot_verification)
1293 lf_printf (file, "\n");
1294 lf_indent_suppress (file);
1295 lf_printf (file, "#if defined(IS_WRONG_SLOT)\n");
1296 lf_printf (file, "/* Validate: Instruction issued in correct slot */\n");
1297 lf_printf (file, "if (IS_WRONG_SLOT) {\n");
1298 lf_indent (file, +2);
1299 print_idecode_invalid (file, "return", invalid_wrong_slot);
1300 lf_indent (file, -2);
1301 lf_printf (file, "}\n");
1302 lf_indent_suppress (file);
1303 lf_printf (file, "#endif\n");
1310 /****************************************************************/
1314 print_idecode_issue_function_header (lf *file,
1315 const char *processor,
1316 function_decl_type decl_type,
1317 int nr_prefetched_words)
1320 lf_printf (file, "\n");
1323 case is_function_declaration:
1324 lf_print__function_type_function (file, print_semantic_function_type,
1328 case is_function_definition:
1329 lf_print__function_type_function (file, print_semantic_function_type,
1333 case is_function_variable:
1334 print_semantic_function_type (file);
1335 lf_printf (file, " (*");
1338 indent = print_function_name (file,
1343 function_name_prefix_idecode);
1346 case is_function_definition:
1347 indent += lf_printf (file, " (");
1349 case is_function_declaration:
1350 lf_putstr (file, "\n(");
1353 case is_function_variable:
1354 lf_putstr (file, ")\n(");
1358 lf_indent (file, +indent);
1359 print_semantic_function_formal (file, nr_prefetched_words);
1360 lf_putstr (file, ")");
1361 lf_indent (file, -indent);
1364 case is_function_definition:
1365 lf_printf (file, "\n");
1367 case is_function_declaration:
1368 case is_function_variable:
1369 lf_putstr (file, ";\n");
1377 print_idecode_globals (lf *file)
1379 lf_printf (file, "enum {\n");
1380 lf_printf (file, " /* greater or equal to zero => table */\n");
1381 lf_printf (file, " function_entry = -1,\n");
1382 lf_printf (file, " boolean_entry = -2,\n");
1383 lf_printf (file, "};\n");
1384 lf_printf (file, "\n");
1385 lf_printf (file, "typedef struct _idecode_table_entry {\n");
1386 lf_printf (file, " int shift;\n");
1387 lf_printf (file, " unsigned%d mask;\n", options.insn_bit_size);
1388 lf_printf (file, " unsigned%d value;\n", options.insn_bit_size);
1389 lf_printf (file, " void *function_or_table;\n");
1390 lf_printf (file, "} idecode_table_entry;\n");