gen-semantics.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "misc.h"
  15. #include "lf.h"
  16. #include "table.h"
  17. #include "filter.h"
  18. #include "ld-decode.h"
  19. #include "ld-cache.h"
  20. #include "ld-insn.h"
  21. #include "igen.h"
  22. #include "gen-semantics.h"
  23. #include "gen-icache.h"
  24. #include "gen-idecode.h"
  25. static void
  26. print_semantic_function_header(lf *file,
  27. const char *basename,
  28. insn_bits *expanded_bits,
  29. int is_function_definition)
  30. {
  31. int indent;
  32. lf_printf(file, "\n");
  33. lf_print_function_type(file, SEMANTIC_FUNCTION_TYPE, "PSIM_EXTERN_SEMANTICS",
  34. (is_function_definition ? "\n" : " "));
  35. indent = print_function_name(file,
  36. basename,
  37. expanded_bits,
  38. function_name_prefix_semantics);
  39. if (is_function_definition)
  40. lf_indent(file, +indent);
  41. else
  42. lf_printf(file, "\n");
  43. lf_printf(file, "(%s)", SEMANTIC_FUNCTION_FORMAL);
  44. if (is_function_definition)
  45. lf_indent(file, -indent);
  46. else
  47. lf_printf(file, ";");
  48. lf_printf(file, "\n");
  49. }
  50. void
  51. print_semantic_declaration(insn_table *entry,
  52. lf *file,
  53. void *data,
  54. insn *instruction,
  55. int depth)
  56. {
  57. if (generate_expanded_instructions) {
  58. ASSERT(entry->nr_insn == 1);
  59. print_semantic_function_header(file,
  60. instruction->file_entry->fields[insn_name],
  61. entry->expanded_bits,
  62. 0/* is not function definition*/);
  63. }
  64. else {
  65. print_semantic_function_header(file,
  66. instruction->file_entry->fields[insn_name],
  67. NULL,
  68. 0/* is not function definition*/);
  69. }
  70. }
  71. /* generate the semantics.c file */
  72. void
  73. print_idecode_illegal(lf *file,
  74. const char *result)
  75. {
  76. if ((code & generate_jumps))
  77. lf_printf(file, "goto %s_illegal;\n", (code & generate_with_icache) ? "icache" : "semantic");
  78. else if ((code & generate_with_icache))
  79. lf_printf(file, "%s icache_illegal(%s);\n", result, ICACHE_FUNCTION_ACTUAL);
  80. else
  81. lf_printf(file, "%s semantic_illegal(%s);\n", result, SEMANTIC_FUNCTION_ACTUAL);
  82. }
  83. void
  84. print_semantic_body(lf *file,
  85. insn *instruction,
  86. insn_bits *expanded_bits,
  87. opcode_field *opcodes)
  88. {
  89. print_itrace(file, instruction->file_entry, 0/*put_value_in_cache*/);
  90. /* validate the instruction, if a cache this has already been done */
  91. if (!(code & generate_with_icache))
  92. print_idecode_validate(file, instruction, opcodes);
  93. /* generate the profiling call - this is delayed until after the
  94. instruction has been verified */
  95. lf_printf(file, "\n");
  96. lf_printf(file, "/* monitoring: */\n");
  97. lf_printf(file, "if (WITH_MON & MONITOR_INSTRUCTION_ISSUE) {\n");
  98. lf_printf(file, " mon_issue(");
  99. print_function_name(file,
  100. instruction->file_entry->fields[insn_name],
  101. NULL,
  102. function_name_prefix_itable);
  103. lf_printf(file, ", processor, cia);\n");
  104. lf_printf(file, "}\n");
  105. /* generate the code (or at least something */
  106. lf_printf(file, "\n");
  107. lf_printf(file, "/* semantics: */\n");
  108. lf_printf(file, "nia = cia + %d;\n", insn_bit_size / 8);
  109. if (instruction->file_entry->annex != NULL) {
  110. /* true code */
  111. table_entry_print_cpp_line_nr(file, instruction->file_entry);
  112. lf_printf(file, "{\n");
  113. lf_indent(file, +2);
  114. lf_print__c_code(file, instruction->file_entry->annex);
  115. lf_indent(file, -2);
  116. lf_printf(file, "}\n");
  117. lf_print__internal_reference(file);
  118. }
  119. else if (it_is("nop", instruction->file_entry->fields[insn_flags])) {
  120. lf_print__internal_reference(file);
  121. }
  122. else {
  123. /* abort so it is implemented now */
  124. table_entry_print_cpp_line_nr(file, instruction->file_entry);
  125. lf_putstr(file, "error(\"%s:%d:0x%08lx:%s unimplemented\\n\",\n");
  126. lf_printf(file, " itable[MY_INDEX].file, itable[MY_INDEX].line_nr, (long)cia, itable[MY_INDEX].name);\n");
  127. lf_print__internal_reference(file);
  128. }
  129. }
  130. static void
  131. print_c_semantic(lf *file,
  132. insn *instruction,
  133. insn_bits *expanded_bits,
  134. opcode_field *opcodes,
  135. cache_table *cache_rules)
  136. {
  137. lf_printf(file, "{\n");
  138. lf_indent(file, +2);
  139. print_my_defines(file, expanded_bits, instruction->file_entry);
  140. lf_printf(file, "\n");
  141. print_icache_body(file,
  142. instruction,
  143. expanded_bits,
  144. cache_rules,
  145. ((code & generate_with_direct_access)
  146. ? define_variables
  147. : declare_variables),
  148. ((code & generate_with_icache)
  149. ? get_values_from_icache
  150. : do_not_use_icache));
  151. lf_printf(file, "unsigned_word nia;\n");
  152. print_semantic_body(file,
  153. instruction,
  154. expanded_bits,
  155. opcodes);
  156. lf_printf(file, "return nia;\n");
  157. /* generate something to clean up any #defines created for the cache */
  158. if (code & generate_with_direct_access)
  159. print_icache_body(file,
  160. instruction,
  161. expanded_bits,
  162. cache_rules,
  163. undef_variables,
  164. ((code & generate_with_icache)
  165. ? get_values_from_icache
  166. : do_not_use_icache));
  167. lf_indent(file, -2);
  168. lf_printf(file, "}\n");
  169. }
  170. static void
  171. print_c_semantic_function(lf *file,
  172. insn *instruction,
  173. insn_bits *expanded_bits,
  174. opcode_field *opcodes,
  175. cache_table *cache_rules)
  176. {
  177. /* build the semantic routine to execute the instruction */
  178. print_semantic_function_header(file,
  179. instruction->file_entry->fields[insn_name],
  180. expanded_bits,
  181. 1/*is-function-definition*/);
  182. print_c_semantic(file,
  183. instruction,
  184. expanded_bits,
  185. opcodes,
  186. cache_rules);
  187. }
  188. void
  189. print_semantic_definition(insn_table *entry,
  190. lf *file,
  191. void *data,
  192. insn *instruction,
  193. int depth)
  194. {
  195. cache_table *cache_rules = (cache_table*)data;
  196. if (generate_expanded_instructions) {
  197. ASSERT(entry->nr_insn == 1
  198. && entry->opcode == NULL
  199. && entry->parent != NULL
  200. && entry->parent->opcode != NULL);
  201. ASSERT(entry->nr_insn == 1
  202. && entry->opcode == NULL
  203. && entry->parent != NULL
  204. && entry->parent->opcode != NULL
  205. && entry->parent->opcode_rule != NULL);
  206. print_c_semantic_function(file,
  207. entry->insns,
  208. entry->expanded_bits,
  209. entry->parent->opcode,
  210. cache_rules);
  211. }
  212. else {
  213. print_c_semantic_function(file, instruction,
  214. NULL, NULL,
  215. cache_rules);
  216. }
  217. }