1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002, 2007 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "ld-decode.h"
34 #include "gen-idecode.h"
35 #include "gen-engine.h"
36 #include "gen-icache.h"
37 #include "gen-semantics.h"
41 print_engine_issue_prefix_hook (lf *file)
43 lf_printf (file, "\n");
44 lf_indent_suppress (file);
45 lf_printf (file, "#if defined (ENGINE_ISSUE_PREFIX_HOOK)\n");
46 lf_printf (file, "ENGINE_ISSUE_PREFIX_HOOK();\n");
47 lf_indent_suppress (file);
48 lf_printf (file, "#endif\n");
49 lf_printf (file, "\n");
53 print_engine_issue_postfix_hook (lf *file)
55 lf_printf (file, "\n");
56 lf_indent_suppress (file);
57 lf_printf (file, "#if defined (ENGINE_ISSUE_POSTFIX_HOOK)\n");
58 lf_printf (file, "ENGINE_ISSUE_POSTFIX_HOOK();\n");
59 lf_indent_suppress (file);
60 lf_printf (file, "#endif\n");
61 lf_printf (file, "\n");
66 print_run_body (lf *file, gen_entry *table)
68 /* Output the function to execute real code:
70 Unfortunatly, there are multiple cases to consider vis:
74 Consequently this function is written in multiple different ways */
76 lf_printf (file, "{\n");
80 lf_printf (file, "%sinstruction_address cia;\n",
81 options.module.global.prefix.l);
83 lf_printf (file, "int current_cpu = next_cpu_nr;\n");
85 if (options.gen.icache)
87 lf_printf (file, "/* flush the icache of a possible break insn */\n");
88 lf_printf (file, "{\n");
89 lf_printf (file, " int cpu_nr;\n");
90 lf_printf (file, " for (cpu_nr = 0; cpu_nr < nr_cpus; cpu_nr++)\n");
91 lf_printf (file, " cpu_flush_icache (STATE_CPU (sd, cpu_nr));\n");
92 lf_printf (file, "}\n");
99 /* CASE 1: NO SMP (with or with out instruction cache).\n\
101 In this case, we can take advantage of the fact that the current\n\
102 instruction address (CIA) does not need to be read from / written to\n\
103 the CPU object after the execution of an instruction.\n\
105 Instead, CIA is only saved when the main loop exits. This occures\n\
106 when either sim_engine_halt or sim_engine_restart is called. Both of\n\
107 these functions save the current instruction address before halting /\n\
108 restarting the simulator.\n\
110 As a variation, there may also be support for an instruction cracking\n\
115 lf_putstr (file, "\n");
116 lf_putstr (file, "/* prime the main loop */\n");
117 lf_putstr (file, "SIM_ASSERT (current_cpu == 0);\n");
118 lf_putstr (file, "SIM_ASSERT (nr_cpus == 1);\n");
119 lf_putstr (file, "cia = CIA_GET (CPU);\n");
121 lf_putstr (file, "\n");
122 lf_putstr (file, "while (1)\n");
123 lf_putstr (file, " {\n");
124 lf_indent (file, +4);
126 lf_printf (file, "%sinstruction_address nia;\n",
127 options.module.global.prefix.l);
129 lf_printf (file, "\n");
130 if (!options.gen.icache)
133 "%sinstruction_word instruction_0 = IMEM%d (cia);\n",
134 options.module.global.prefix.l, options.insn_bit_size);
135 print_engine_issue_prefix_hook (file);
136 print_idecode_body (file, table, "nia = ");
137 print_engine_issue_postfix_hook (file);
141 lf_putstr (file, "idecode_cache *cache_entry =\n");
142 lf_putstr (file, " cpu_icache_entry (cpu, cia);\n");
143 lf_putstr (file, "if (cache_entry->address == cia)\n");
144 lf_putstr (file, " {\n");
145 lf_indent (file, -4);
146 lf_putstr (file, "/* cache hit */\n");
148 "idecode_semantic *const semantic = cache_entry->semantic;\n");
149 lf_putstr (file, "cia = semantic (cpu, cache_entry, cia);\n");
151 lf_indent (file, -4);
152 lf_putstr (file, " }\n");
153 lf_putstr (file, "else\n");
154 lf_putstr (file, " {\n");
155 lf_indent (file, +4);
156 lf_putstr (file, "/* cache miss */\n");
157 if (!options.gen.semantic_icache)
159 lf_putstr (file, "idecode_semantic *semantic;\n");
161 lf_printf (file, "instruction_word instruction = IMEM%d (cia);\n",
162 options.insn_bit_size);
163 lf_putstr (file, "if (WITH_MON != 0)\n");
165 " mon_event (mon_event_icache_miss, cpu, cia);\n");
166 if (options.gen.semantic_icache)
168 lf_putstr (file, "{\n");
169 lf_indent (file, +2);
170 print_engine_issue_prefix_hook (file);
171 print_idecode_body (file, table, "nia =");
172 print_engine_issue_postfix_hook (file);
173 lf_indent (file, -2);
174 lf_putstr (file, "}\n");
178 print_engine_issue_prefix_hook (file);
179 print_idecode_body (file, table, "semantic =");
180 lf_putstr (file, "nia = semantic (cpu, cache_entry, cia);\n");
181 print_engine_issue_postfix_hook (file);
183 lf_indent (file, -4);
184 lf_putstr (file, " }\n");
187 /* update the cpu if necessary */
188 switch (options.gen.nia)
190 case nia_is_cia_plus_one:
191 lf_printf (file, "\n");
192 lf_printf (file, "/* Update the instruction address */\n");
193 lf_printf (file, "cia = nia;\n");
197 ERROR ("engine gen when NIA complex");
201 lf_putstr (file, "\n");
202 lf_putstr (file, "/* process any events */\n");
203 lf_putstr (file, "if (sim_events_tick (sd))\n");
204 lf_putstr (file, " {\n");
205 lf_putstr (file, " CIA_SET (CPU, cia);\n");
206 lf_putstr (file, " sim_events_process (sd);\n");
207 lf_putstr (file, " cia = CIA_GET (CPU);\n");
208 lf_putstr (file, " }\n");
210 lf_indent (file, -4);
211 lf_printf (file, " }\n");
218 /* CASE 2: SMP (With or without ICACHE)\n\
220 The complexity here comes from needing to correctly halt the simulator\n\
221 when it is aborted. For instance, if cpu0 requests a restart then\n\
222 cpu1 will normally be the next cpu that is run. Cpu0 being restarted\n\
223 after all the other CPU's and the event queue have been processed */\n\
227 lf_putstr (file, "\n");
229 "/* have ensured that the event queue is NOT next */\n");
230 lf_printf (file, "SIM_ASSERT (current_cpu >= 0);\n");
231 lf_printf (file, "SIM_ASSERT (current_cpu <= nr_cpus - 1);\n");
232 lf_printf (file, "SIM_ASSERT (nr_cpus <= MAX_NR_PROCESSORS);\n");
234 lf_putstr (file, "\n");
235 lf_putstr (file, "while (1)\n");
236 lf_putstr (file, " {\n");
237 lf_indent (file, +4);
238 lf_putstr (file, "sim_cpu *cpu = STATE_CPU (sd, current_cpu);\n");
239 lf_putstr (file, "instruction_address cia = CIA_GET (cpu);\n");
240 lf_putstr (file, "\n");
242 if (!options.gen.icache)
244 lf_printf (file, "instruction_word instruction_0 = IMEM%d (cia);\n",
245 options.insn_bit_size);
246 print_engine_issue_prefix_hook (file);
247 print_idecode_body (file, table, "cia =");
248 lf_putstr (file, "CIA_SET (cpu, cia);\n");
249 print_engine_issue_postfix_hook (file);
252 if (options.gen.icache)
254 lf_putstr (file, "engine_cache *cache_entry =\n");
255 lf_putstr (file, " cpu_icache_entry(processor, cia);\n");
256 lf_putstr (file, "\n");
257 lf_putstr (file, "if (cache_entry->address == cia) {\n");
259 lf_indent (file, +2);
260 lf_putstr (file, "\n");
261 lf_putstr (file, "/* cache hit */\n");
263 "engine_semantic *semantic = cache_entry->semantic;\n");
265 "cia = semantic(processor, cache_entry, cia);\n");
267 lf_putstr (file, "cpu_set_program_counter(processor, cia);\n");
268 lf_putstr (file, "\n");
269 lf_indent (file, -2);
271 lf_putstr (file, "}\n");
272 lf_putstr (file, "else {\n");
274 lf_indent (file, +2);
275 lf_putstr (file, "\n");
276 lf_putstr (file, "/* cache miss */\n");
277 if (!options.gen.semantic_icache)
279 lf_putstr (file, "engine_semantic *semantic;\n");
281 lf_printf (file, "instruction_word instruction = IMEM%d (cia);\n",
282 options.insn_bit_size);
283 lf_putstr (file, "if (WITH_MON != 0)\n");
285 " mon_event(mon_event_icache_miss, processors[current_cpu], cia);\n");
286 if (options.gen.semantic_icache)
288 lf_putstr (file, "{\n");
289 lf_indent (file, +2);
290 print_engine_issue_prefix_hook (file);
291 print_idecode_body (file, table, "cia =");
292 print_engine_issue_postfix_hook (file);
293 lf_indent (file, -2);
294 lf_putstr (file, "}\n");
298 print_engine_issue_prefix_hook (file);
299 print_idecode_body (file, table, "semantic = ");
301 "cia = semantic(processor, cache_entry, cia);\n");
302 print_engine_issue_postfix_hook (file);
305 lf_putstr (file, "cpu_set_program_counter(processor, cia);\n");
306 lf_putstr (file, "\n");
307 lf_indent (file, -2);
309 lf_putstr (file, "}\n");
312 lf_putstr (file, "\n");
313 lf_putstr (file, "current_cpu += 1;\n");
314 lf_putstr (file, "if (current_cpu == nr_cpus)\n");
315 lf_putstr (file, " {\n");
316 lf_putstr (file, " if (sim_events_tick (sd))\n");
317 lf_putstr (file, " {\n");
318 lf_putstr (file, " sim_events_process (sd);\n");
319 lf_putstr (file, " }\n");
320 lf_putstr (file, " current_cpu = 0;\n");
321 lf_putstr (file, " }\n");
324 lf_indent (file, -4);
325 lf_putstr (file, " }\n");
329 lf_indent (file, -2);
330 lf_putstr (file, "}\n");
334 /****************************************************************/
338 print_jump (lf *file, int is_tail)
340 if (!options.gen.smp)
342 lf_putstr (file, "if (event_queue_tick (sd))\n");
343 lf_putstr (file, " {\n");
344 lf_putstr (file, " CPU_CIA (processor) = nia;\n");
345 lf_putstr (file, " sim_events_process (sd);\n");
346 lf_putstr (file, " }\n");
347 lf_putstr (file, "}\n");
353 lf_putstr (file, "cpu_set_program_counter(processor, nia);\n");
354 lf_putstr (file, "current_cpu += 1;\n");
355 lf_putstr (file, "if (current_cpu >= nr_cpus)\n");
356 lf_putstr (file, " {\n");
357 lf_putstr (file, " if (sim_events_tick (sd))\n");
358 lf_putstr (file, " {\n");
359 lf_putstr (file, " sim_events_process (sd);\n");
360 lf_putstr (file, " }\n");
361 lf_putstr (file, " current_cpu = 0;\n");
362 lf_putstr (file, " }\n");
363 lf_putstr (file, "processor = processors[current_cpu];\n");
364 lf_putstr (file, "nia = cpu_get_program_counter(processor);\n");
367 if (options.gen.icache)
369 lf_putstr (file, "cache_entry = cpu_icache_entry(processor, nia);\n");
370 lf_putstr (file, "if (cache_entry->address == nia) {\n");
371 lf_putstr (file, " /* cache hit */\n");
372 lf_putstr (file, " goto *cache_entry->semantic;\n");
373 lf_putstr (file, "}\n");
376 lf_putstr (file, "goto cache_miss;\n");
380 if (!options.gen.icache && is_tail)
382 lf_printf (file, "goto engine;\n");
391 print_jump_insn (lf *file,
392 insn_entry * instruction,
393 opcode_bits *expanded_bits,
394 opcode_field *opcodes, cache_entry *cache_rules)
396 insn_opcodes opcode_path;
398 memset (&opcode_path, 0, sizeof (opcode_path));
399 opcode_path.opcode = opcodes;
401 /* what we are for the moment */
402 lf_printf (file, "\n");
403 print_my_defines (file,
405 instruction->format_name, expanded_bits);
407 /* output the icache entry */
408 if (options.gen.icache)
410 lf_printf (file, "\n");
411 lf_indent (file, -1);
412 print_function_name (file,
414 instruction->format_name,
415 NULL, expanded_bits, function_name_prefix_icache);
416 lf_printf (file, ":\n");
417 lf_indent (file, +1);
418 lf_printf (file, "{\n");
419 lf_indent (file, +2);
420 lf_putstr (file, "const unsigned_word cia = nia;\n");
421 print_itrace (file, instruction, 1 /*putting-value-in-cache */ );
422 print_idecode_validate (file, instruction, &opcode_path);
423 lf_printf (file, "\n");
424 lf_printf (file, "{\n");
425 lf_indent (file, +2);
426 print_icache_body (file, instruction, expanded_bits, cache_rules, 0, /*use_defines */
427 put_values_in_icache);
428 lf_printf (file, "cache_entry->address = nia;\n");
429 lf_printf (file, "cache_entry->semantic = &&");
430 print_function_name (file,
432 instruction->format_name,
434 expanded_bits, function_name_prefix_semantics);
435 lf_printf (file, ";\n");
436 if (options.gen.semantic_icache)
438 print_semantic_body (file,
439 instruction, expanded_bits, &opcode_path);
440 print_jump (file, 1 /*is-tail */ );
444 lf_printf (file, "/* goto ");
445 print_function_name (file,
447 instruction->format_name,
449 expanded_bits, function_name_prefix_semantics);
450 lf_printf (file, "; */\n");
452 lf_indent (file, -2);
453 lf_putstr (file, "}\n");
454 lf_indent (file, -2);
455 lf_printf (file, "}\n");
458 /* print the semantics */
459 lf_printf (file, "\n");
460 lf_indent (file, -1);
461 print_function_name (file,
463 instruction->format_name,
464 NULL, expanded_bits, function_name_prefix_semantics);
465 lf_printf (file, ":\n");
466 lf_indent (file, +1);
467 lf_printf (file, "{\n");
468 lf_indent (file, +2);
469 lf_putstr (file, "const unsigned_word cia = nia;\n");
470 print_icache_body (file,
474 (options.gen.direct_access
476 : declare_variables),
478 ? get_values_from_icache : do_not_use_icache));
479 print_semantic_body (file, instruction, expanded_bits, &opcode_path);
480 if (options.gen.direct_access)
481 print_icache_body (file,
487 ? get_values_from_icache : do_not_use_icache));
488 print_jump (file, 1 /*is tail */ );
489 lf_indent (file, -2);
490 lf_printf (file, "}\n");
497 print_jump_definition (lf *file, gen_entry *entry, int depth, void *data)
499 cache_entry *cache_rules = (cache_entry *) data;
500 if (entry->opcode_rule->with_duplicates)
502 ASSERT (entry->nr_insns == 1
503 && entry->opcode == NULL
504 && entry->parent != NULL && entry->parent->opcode != NULL);
505 ASSERT (entry->nr_insns == 1
506 && entry->opcode == NULL
507 && entry->parent != NULL
508 && entry->parent->opcode != NULL
509 && entry->parent->opcode_rule != NULL);
510 print_jump_insn (file,
512 entry->expanded_bits, entry->opcode, cache_rules);
516 print_jump_insn (file, entry->insns->insn, NULL, NULL, cache_rules);
524 print_jump_internal_function (lf *file, function_entry * function, void *data)
526 if (function->is_internal)
528 lf_printf (file, "\n");
529 lf_print__line_ref (file, function->line);
530 lf_indent (file, -1);
531 print_function_name (file,
537 ? function_name_prefix_icache
538 : function_name_prefix_semantics));
539 lf_printf (file, ":\n");
540 lf_indent (file, +1);
541 lf_printf (file, "{\n");
542 lf_indent (file, +2);
543 lf_printf (file, "const unsigned_word cia = nia;\n");
544 table_print_code (file, function->code);
545 lf_print__internal_ref (file);
546 lf_printf (file, "error(\"Internal function must longjump\\n\");\n");
547 lf_indent (file, -2);
548 lf_printf (file, "}\n");
556 print_jump_body (lf *file,
557 gen_entry *entry, insn_table *isa, cache_entry *cache_rules)
559 lf_printf (file, "{\n");
560 lf_indent (file, +2);
561 lf_putstr (file, "jmp_buf halt;\n");
562 lf_putstr (file, "jmp_buf restart;\n");
563 lf_putstr (file, "cpu *processor = NULL;\n");
564 lf_putstr (file, "unsigned_word nia = -1;\n");
565 lf_putstr (file, "instruction_word instruction = 0;\n");
566 if (options.gen.icache)
568 lf_putstr (file, "engine_cache *cache_entry = NULL;\n");
572 lf_putstr (file, "int current_cpu = -1;\n");
575 /* all the switches and tables - they know about jumping */
576 print_idecode_lookups (file, entry, cache_rules);
578 /* start the simulation up */
579 if (options.gen.icache)
581 lf_putstr (file, "\n");
582 lf_putstr (file, "{\n");
583 lf_putstr (file, " int cpu_nr;\n");
584 lf_putstr (file, " for (cpu_nr = 0; cpu_nr < nr_cpus; cpu_nr++)\n");
585 lf_putstr (file, " cpu_flush_icache(processors[cpu_nr]);\n");
586 lf_putstr (file, "}\n");
589 lf_putstr (file, "\n");
590 lf_putstr (file, "psim_set_halt_and_restart(system, &halt, &restart);\n");
592 lf_putstr (file, "\n");
593 lf_putstr (file, "if (setjmp(halt))\n");
594 lf_putstr (file, " return;\n");
596 lf_putstr (file, "\n");
597 lf_putstr (file, "setjmp(restart);\n");
599 lf_putstr (file, "\n");
600 if (!options.gen.smp)
602 lf_putstr (file, "processor = processors[0];\n");
603 lf_putstr (file, "nia = cpu_get_program_counter(processor);\n");
607 lf_putstr (file, "current_cpu = psim_last_cpu(system);\n");
610 if (!options.gen.icache)
612 lf_printf (file, "\n");
613 lf_indent (file, -1);
614 lf_printf (file, "engine:\n");
615 lf_indent (file, +1);
618 print_jump (file, 0 /*is_tail */ );
620 if (options.gen.icache)
622 lf_indent (file, -1);
623 lf_printf (file, "cache_miss:\n");
624 lf_indent (file, +1);
627 print_engine_issue_prefix_hook (file);
628 lf_putstr (file, "instruction\n");
630 " = vm_instruction_map_read(cpu_instruction_map(processor),\n");
631 lf_putstr (file, " processor, nia);\n");
632 print_engine_issue_prefix_hook (file);
633 print_idecode_body (file, entry, "/*IGORE*/");
634 print_engine_issue_postfix_hook (file);
636 /* print out a table of all the internals functions */
637 function_entry_traverse (file, isa->functions,
638 print_jump_internal_function, NULL);
640 /* print out a table of all the instructions */
641 ERROR ("Use the list of semantic functions, not travere_tree");
642 gen_entry_traverse_tree (file, entry, 1, NULL, /* start */
643 print_jump_definition, /* leaf */
646 lf_indent (file, -2);
647 lf_printf (file, "}\n");
652 /****************************************************************/
656 print_engine_run_function_header (lf *file,
658 function_decl_type decl_type)
661 lf_printf (file, "\n");
664 case is_function_declaration:
665 lf_print__function_type (file, "void", "INLINE_ENGINE", "\n");
667 case is_function_definition:
668 lf_print__function_type (file, "void", "INLINE_ENGINE", " ");
670 case is_function_variable:
671 lf_printf (file, "void (*");
674 indent = print_function_name (file, "run", NULL, /* format name */
675 processor, NULL, /* expanded bits */
676 function_name_prefix_engine);
679 case is_function_definition:
680 lf_putstr (file, "\n(");
683 case is_function_declaration:
684 indent += lf_printf (file, " (");
686 case is_function_variable:
687 lf_putstr (file, ")\n(");
691 lf_indent (file, +indent);
692 lf_printf (file, "SIM_DESC sd,\n");
693 lf_printf (file, "int next_cpu_nr,\n");
694 lf_printf (file, "int nr_cpus,\n");
695 lf_printf (file, "int siggnal)");
696 lf_indent (file, -indent);
699 case is_function_definition:
700 lf_putstr (file, "\n");
702 case is_function_variable:
703 case is_function_declaration:
704 lf_putstr (file, ";\n");
711 gen_engine_h (lf *file,
712 gen_table *gen, insn_table *isa, cache_entry *cache_rules)
715 for (entry = gen->tables; entry != NULL; entry = entry->next)
717 print_engine_run_function_header (file,
718 (options.gen.multi_sim
720 : NULL), is_function_declaration);
726 gen_engine_c (lf *file,
727 gen_table *gen, insn_table *isa, cache_entry *cache_rules)
731 print_includes (file);
732 print_include_inline (file, options.module.semantics);
733 print_include (file, options.module.engine);
734 lf_printf (file, "\n");
735 lf_printf (file, "#include \"sim-assert.h\"\n");
736 lf_printf (file, "\n");
737 print_idecode_globals (file);
738 lf_printf (file, "\n");
740 for (entry = gen->tables; entry != NULL; entry = entry->next)
742 switch (options.gen.code)
745 print_idecode_lookups (file, entry->table, cache_rules);
747 /* output the main engine routine */
748 print_engine_run_function_header (file,
749 (options.gen.multi_sim
751 : NULL), is_function_definition);
752 print_run_body (file, entry->table);
756 ERROR ("Jumps currently unimplemented");
758 print_engine_run_function_header (file,
760 is_function_definition);
761 print_jump_body (file, entry->table, isa, cache_rules);