gen-idecode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /* The IGEN simulator generator for GDB, the GNU Debugger.
  2. Copyright 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Andrew Cagney.
  4. This file is part of GDB.
  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 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "misc.h"
  16. #include "lf.h"
  17. #include "table.h"
  18. #include "filter.h"
  19. #include "igen.h"
  20. #include "ld-insn.h"
  21. #include "ld-decode.h"
  22. #include "gen.h"
  23. #include "gen-idecode.h"
  24. #include "gen-icache.h"
  25. #include "gen-semantics.h"
  26. static void
  27. lf_print_opcodes (lf *file, gen_entry *table)
  28. {
  29. if (table !=NULL)
  30. {
  31. while (1)
  32. {
  33. ASSERT (table->opcode != NULL);
  34. lf_printf (file, "_%d_%d",
  35. table->opcode->first, table->opcode->last);
  36. if (table->parent == NULL)
  37. break;
  38. lf_printf (file, "__%d", table->opcode_nr);
  39. table = table->parent;
  40. }
  41. }
  42. }
  43. static void
  44. print_idecode_ifetch (lf *file,
  45. int previous_nr_prefetched_words,
  46. int current_nr_prefetched_words)
  47. {
  48. int word_nr;
  49. for (word_nr = previous_nr_prefetched_words;
  50. word_nr < current_nr_prefetched_words; word_nr++)
  51. {
  52. lf_printf (file,
  53. "instruction_word instruction_%d = IMEM%d_IMMED (cia, %d);\n",
  54. word_nr, options.insn_bit_size, word_nr);
  55. }
  56. }
  57. /****************************************************************/
  58. static void
  59. lf_print_table_name (lf *file, gen_entry *table)
  60. {
  61. lf_printf (file, "idecode_table");
  62. lf_print_opcodes (file, table);
  63. }
  64. static void
  65. print_idecode_table (lf *file, gen_entry *entry, const char *result)
  66. {
  67. lf_printf (file, "/* prime the search */\n");
  68. lf_printf (file, "idecode_table_entry *table = ");
  69. lf_print_table_name (file, entry);
  70. lf_printf (file, ";\n");
  71. lf_printf (file, "int opcode = EXTRACTED%d (instruction, %d, %d);\n",
  72. options.insn_bit_size,
  73. i2target (options.hi_bit_nr, entry->opcode->first),
  74. i2target (options.hi_bit_nr, entry->opcode->last));
  75. lf_printf (file, "idecode_table_entry *table_entry = table + opcode;\n");
  76. lf_printf (file, "\n");
  77. lf_printf (file, "/* iterate until a leaf */\n");
  78. lf_printf (file, "while (1) {\n");
  79. lf_printf (file, " signed shift = table_entry->shift;\n");
  80. lf_printf (file, "if (shift == function_entry) break;\n");
  81. lf_printf (file, " if (shift >= 0) {\n");
  82. lf_printf (file, " table = ((idecode_table_entry*)\n");
  83. lf_printf (file, " table_entry->function_or_table);\n");
  84. lf_printf (file, " opcode = ((instruction & table_entry->mask)\n");
  85. lf_printf (file, " >> shift);\n");
  86. lf_printf (file, " table_entry = table + opcode;\n");
  87. lf_printf (file, " }\n");
  88. lf_printf (file, " else {\n");
  89. lf_printf (file, " /* must be a boolean */\n");
  90. lf_printf (file, " ASSERT(table_entry->shift == boolean_entry);\n");
  91. lf_printf (file, " opcode = ((instruction & table_entry->mask)\n");
  92. lf_printf (file, " != table_entry->value);\n");
  93. lf_printf (file, " table = ((idecode_table_entry*)\n");
  94. lf_printf (file, " table_entry->function_or_table);\n");
  95. lf_printf (file, " table_entry = table + opcode;\n");
  96. lf_printf (file, " }\n");
  97. lf_printf (file, "}\n");
  98. lf_printf (file, "\n");
  99. lf_printf (file, "/* call the leaf code */\n");
  100. if (options.gen.code == generate_jumps)
  101. {
  102. lf_printf (file, "goto *table_entry->function_or_table;\n");
  103. }
  104. else
  105. {
  106. lf_printf (file, "%s ", result);
  107. if (options.gen.icache)
  108. {
  109. lf_printf (file,
  110. "(((idecode_icache*)table_entry->function_or_table)\n");
  111. lf_printf (file, " (");
  112. print_icache_function_actual (file, 1);
  113. lf_printf (file, "));\n");
  114. }
  115. else
  116. {
  117. lf_printf (file,
  118. "((idecode_semantic*)table_entry->function_or_table)\n");
  119. lf_printf (file, " (");
  120. print_semantic_function_actual (file, 1);
  121. lf_printf (file, ");\n");
  122. }
  123. }
  124. }
  125. static void
  126. print_idecode_table_start (lf *file, gen_entry *table, int depth, void *data)
  127. {
  128. ASSERT (depth == 0);
  129. /* start of the table */
  130. if (table->opcode_rule->gen == array_gen)
  131. {
  132. lf_printf (file, "\n");
  133. lf_printf (file, "static idecode_table_entry ");
  134. lf_print_table_name (file, table);
  135. lf_printf (file, "[] = {\n");
  136. }
  137. }
  138. static void
  139. print_idecode_table_leaf (lf *file, gen_entry *entry, int depth, void *data)
  140. {
  141. gen_entry *master_entry;
  142. ASSERT (entry->parent != NULL);
  143. ASSERT (depth == 0);
  144. if (entry->combined_parent == NULL)
  145. master_entry = entry;
  146. else
  147. master_entry = entry->combined_parent;
  148. /* add an entry to the table */
  149. if (entry->parent->opcode_rule->gen == array_gen)
  150. {
  151. lf_printf (file, " /*%d*/ { ", entry->opcode_nr);
  152. if (entry->opcode == NULL)
  153. {
  154. ASSERT (entry->nr_insns == 1);
  155. /* table leaf entry */
  156. lf_printf (file, "function_entry, 0, 0, ");
  157. if (options.gen.code == generate_jumps)
  158. {
  159. lf_printf (file, "&&");
  160. }
  161. print_function_name (file,
  162. entry->insns->insn->name,
  163. entry->insns->insn->format_name,
  164. NULL,
  165. master_entry->expanded_bits,
  166. (options.gen.icache
  167. ? function_name_prefix_icache
  168. : function_name_prefix_semantics));
  169. }
  170. else if (entry->opcode_rule->gen == switch_gen
  171. || entry->opcode_rule->gen == goto_switch_gen
  172. || entry->opcode_rule->gen == padded_switch_gen)
  173. {
  174. /* table calling switch statement */
  175. lf_printf (file, "function_entry, 0, 0, ");
  176. if (options.gen.code == generate_jumps)
  177. {
  178. lf_printf (file, "&&");
  179. }
  180. lf_print_table_name (file, entry);
  181. }
  182. else if (entry->opcode->is_boolean)
  183. {
  184. /* table `calling' boolean table */
  185. lf_printf (file, "boolean_entry, ");
  186. lf_printf (file, "MASK32(%d, %d), ",
  187. i2target (options.hi_bit_nr, entry->opcode->first),
  188. i2target (options.hi_bit_nr, entry->opcode->last));
  189. lf_printf (file, "INSERTED32(%d, %d, %d), ",
  190. entry->opcode->boolean_constant,
  191. i2target (options.hi_bit_nr, entry->opcode->first),
  192. i2target (options.hi_bit_nr, entry->opcode->last));
  193. lf_print_table_name (file, entry);
  194. }
  195. else
  196. {
  197. /* table `calling' another table */
  198. lf_printf (file, "%d, ",
  199. options.insn_bit_size - entry->opcode->last - 1);
  200. lf_printf (file, "MASK%d(%d,%d), ", options.insn_bit_size,
  201. i2target (options.hi_bit_nr, entry->opcode->first),
  202. i2target (options.hi_bit_nr, entry->opcode->last));
  203. lf_printf (file, "0, ");
  204. lf_print_table_name (file, entry);
  205. }
  206. lf_printf (file, " },\n");
  207. }
  208. }
  209. static void
  210. print_idecode_table_end (lf *file, gen_entry *table, int depth, void *data)
  211. {
  212. ASSERT (depth == 0);
  213. if (table->opcode_rule->gen == array_gen)
  214. {
  215. lf_printf (file, "};\n");
  216. }
  217. }
  218. /****************************************************************/
  219. static void
  220. print_goto_switch_name (lf *file, gen_entry *entry)
  221. {
  222. lf_printf (file, "case_");
  223. if (entry->opcode == NULL)
  224. {
  225. print_function_name (file,
  226. entry->insns->insn->name,
  227. entry->insns->insn->format_name,
  228. NULL,
  229. entry->expanded_bits,
  230. (options.gen.icache
  231. ? function_name_prefix_icache
  232. : function_name_prefix_semantics));
  233. }
  234. else
  235. {
  236. lf_print_table_name (file, entry);
  237. }
  238. }
  239. static void
  240. print_goto_switch_table_leaf (lf *file,
  241. gen_entry *entry, int depth, void *data)
  242. {
  243. ASSERT (entry->parent != NULL);
  244. ASSERT (depth == 0);
  245. ASSERT (entry->parent->opcode_rule->gen == goto_switch_gen);
  246. ASSERT (entry->parent->opcode);
  247. lf_printf (file, "/* %d */ &&", entry->opcode_nr);
  248. if (entry->combined_parent != NULL)
  249. print_goto_switch_name (file, entry->combined_parent);
  250. else
  251. print_goto_switch_name (file, entry);
  252. lf_printf (file, ",\n");
  253. }
  254. static void
  255. print_goto_switch_break (lf *file, gen_entry *entry)
  256. {
  257. lf_printf (file, "goto break_");
  258. lf_print_table_name (file, entry->parent);
  259. lf_printf (file, ";\n");
  260. }
  261. static void
  262. print_goto_switch_table (lf *file, gen_entry *table)
  263. {
  264. lf_printf (file, "const static void *");
  265. lf_print_table_name (file, table);
  266. lf_printf (file, "[] = {\n");
  267. lf_indent (file, +2);
  268. gen_entry_traverse_tree (file, table, 0, NULL /*start */ ,
  269. print_goto_switch_table_leaf, NULL /*end */ ,
  270. NULL /*data */ );
  271. lf_indent (file, -2);
  272. lf_printf (file, "};\n");
  273. }
  274. void print_idecode_switch (lf *file, gen_entry *table, const char *result);
  275. static void
  276. print_idecode_switch_start (lf *file, gen_entry *table, int depth, void *data)
  277. {
  278. /* const char *result = data; */
  279. ASSERT (depth == 0);
  280. ASSERT (table->opcode_rule->gen == switch_gen
  281. || table->opcode_rule->gen == goto_switch_gen
  282. || table->opcode_rule->gen == padded_switch_gen);
  283. if (table->opcode->is_boolean
  284. || table->opcode_rule->gen == switch_gen
  285. || table->opcode_rule->gen == padded_switch_gen)
  286. {
  287. lf_printf (file, "switch (EXTRACTED%d (instruction_%d, %d, %d))\n",
  288. options.insn_bit_size,
  289. table->opcode_rule->word_nr,
  290. i2target (options.hi_bit_nr, table->opcode->first),
  291. i2target (options.hi_bit_nr, table->opcode->last));
  292. lf_indent (file, +2);
  293. lf_printf (file, "{\n");
  294. }
  295. else if (table->opcode_rule->gen == goto_switch_gen)
  296. {
  297. if (table->parent != NULL
  298. && (table->parent->opcode_rule->gen == switch_gen
  299. || table->parent->opcode_rule->gen == goto_switch_gen
  300. || table->parent->opcode_rule->gen == padded_switch_gen))
  301. {
  302. lf_printf (file, "{\n");
  303. lf_indent (file, +2);
  304. }
  305. print_goto_switch_table (file, table);
  306. lf_printf (file, "ASSERT (EXTRACTED%d (instruction_%d, %d, %d)\n",
  307. options.insn_bit_size,
  308. table->opcode->word_nr,
  309. i2target (options.hi_bit_nr, table->opcode->first),
  310. i2target (options.hi_bit_nr, table->opcode->last));
  311. lf_printf (file, " < (sizeof (");
  312. lf_print_table_name (file, table);
  313. lf_printf (file, ") / sizeof(void*)));\n");
  314. lf_printf (file, "goto *");
  315. lf_print_table_name (file, table);
  316. lf_printf (file, "[EXTRACTED%d (instruction_%d, %d, %d)];\n",
  317. options.insn_bit_size,
  318. table->opcode->word_nr,
  319. i2target (options.hi_bit_nr, table->opcode->first),
  320. i2target (options.hi_bit_nr, table->opcode->last));
  321. }
  322. else
  323. {
  324. ASSERT ("bad switch" == NULL);
  325. }
  326. }
  327. static void
  328. print_idecode_switch_leaf (lf *file, gen_entry *entry, int depth, void *data)
  329. {
  330. const char *result = data;
  331. ASSERT (entry->parent != NULL);
  332. ASSERT (depth == 0);
  333. ASSERT (entry->parent->opcode_rule->gen == switch_gen
  334. || entry->parent->opcode_rule->gen == goto_switch_gen
  335. || entry->parent->opcode_rule->gen == padded_switch_gen);
  336. ASSERT (entry->parent->opcode);
  337. /* skip over any instructions combined into another entry */
  338. if (entry->combined_parent != NULL)
  339. return;
  340. if (entry->parent->opcode->is_boolean && entry->opcode_nr == 0)
  341. {
  342. /* case: boolean false target */
  343. lf_printf (file, "case %d:\n", entry->parent->opcode->boolean_constant);
  344. }
  345. else if (entry->parent->opcode->is_boolean && entry->opcode_nr != 0)
  346. {
  347. /* case: boolean true case */
  348. lf_printf (file, "default:\n");
  349. }
  350. else if (entry->parent->opcode_rule->gen == switch_gen
  351. || entry->parent->opcode_rule->gen == padded_switch_gen)
  352. {
  353. /* case: <opcode-nr> - switch */
  354. gen_entry *cob;
  355. for (cob = entry; cob != NULL; cob = cob->combined_next)
  356. lf_printf (file, "case %d:\n", cob->opcode_nr);
  357. }
  358. else if (entry->parent->opcode_rule->gen == goto_switch_gen)
  359. {
  360. /* case: <opcode-nr> - goto-switch */
  361. print_goto_switch_name (file, entry);
  362. lf_printf (file, ":\n");
  363. }
  364. else
  365. {
  366. ERROR ("bad switch");
  367. }
  368. lf_printf (file, " {\n");
  369. lf_indent (file, +4);
  370. {
  371. if (entry->opcode == NULL)
  372. {
  373. /* switch calling leaf */
  374. ASSERT (entry->nr_insns == 1);
  375. print_idecode_ifetch (file, entry->nr_prefetched_words,
  376. entry->insns->semantic->nr_prefetched_words);
  377. switch (options.gen.code)
  378. {
  379. case generate_jumps:
  380. lf_printf (file, "goto ");
  381. break;
  382. case generate_calls:
  383. lf_printf (file, "%s", result);
  384. break;
  385. }
  386. print_function_name (file,
  387. entry->insns->insn->name,
  388. entry->insns->insn->format_name,
  389. NULL,
  390. entry->expanded_bits,
  391. (options.gen.icache
  392. ? function_name_prefix_icache
  393. : function_name_prefix_semantics));
  394. if (options.gen.code == generate_calls)
  395. {
  396. lf_printf (file, " (");
  397. print_semantic_function_actual (file,
  398. entry->insns->semantic->
  399. nr_prefetched_words);
  400. lf_printf (file, ")");
  401. }
  402. lf_printf (file, ";\n");
  403. }
  404. else if (entry->opcode_rule->gen == switch_gen
  405. || entry->opcode_rule->gen == goto_switch_gen
  406. || entry->opcode_rule->gen == padded_switch_gen)
  407. {
  408. /* switch calling switch */
  409. lf_printf (file, "{\n");
  410. lf_indent (file, +2);
  411. print_idecode_ifetch (file, entry->parent->nr_prefetched_words,
  412. entry->nr_prefetched_words);
  413. print_idecode_switch (file, entry, result);
  414. lf_indent (file, -2);
  415. lf_printf (file, "}\n");
  416. }
  417. else
  418. {
  419. /* switch looking up a table */
  420. lf_printf (file, "{\n");
  421. lf_indent (file, +2);
  422. print_idecode_ifetch (file, entry->parent->nr_prefetched_words,
  423. entry->nr_prefetched_words);
  424. print_idecode_table (file, entry, result);
  425. lf_indent (file, -2);
  426. lf_printf (file, "}\n");
  427. }
  428. if (entry->parent->opcode->is_boolean
  429. || entry->parent->opcode_rule->gen == switch_gen
  430. || entry->parent->opcode_rule->gen == padded_switch_gen)
  431. {
  432. lf_printf (file, "break;\n");
  433. }
  434. else if (entry->parent->opcode_rule->gen == goto_switch_gen)
  435. {
  436. print_goto_switch_break (file, entry);
  437. }
  438. else
  439. {
  440. ERROR ("bad switch");
  441. }
  442. }
  443. lf_indent (file, -4);
  444. lf_printf (file, " }\n");
  445. }
  446. static void
  447. print_idecode_switch_illegal (lf *file, const char *result)
  448. {
  449. lf_indent (file, +2);
  450. print_idecode_invalid (file, result, invalid_illegal);
  451. lf_printf (file, "break;\n");
  452. lf_indent (file, -2);
  453. }
  454. static void
  455. print_idecode_switch_end (lf *file, gen_entry *table, int depth, void *data)
  456. {
  457. const char *result = data;
  458. ASSERT (depth == 0);
  459. ASSERT (table->opcode_rule->gen == switch_gen
  460. || table->opcode_rule->gen == goto_switch_gen
  461. || table->opcode_rule->gen == padded_switch_gen);
  462. ASSERT (table->opcode);
  463. if (table->opcode->is_boolean)
  464. {
  465. lf_printf (file, "}\n");
  466. lf_indent (file, -2);
  467. }
  468. else if (table->opcode_rule->gen == switch_gen
  469. || table->opcode_rule->gen == padded_switch_gen)
  470. {
  471. lf_printf (file, "default:\n");
  472. lf_indent (file, +2);
  473. if (table->nr_entries == table->opcode->nr_opcodes)
  474. {
  475. print_sim_engine_abort (file,
  476. "Internal error - bad switch generated");
  477. lf_printf (file, "%sNULL_CIA;\n", result);
  478. lf_printf (file, "break;\n");
  479. }
  480. else
  481. {
  482. print_idecode_switch_illegal (file, result);
  483. }
  484. lf_indent (file, -2);
  485. lf_printf (file, "}\n");
  486. lf_indent (file, -2);
  487. }
  488. else if (table->opcode_rule->gen == goto_switch_gen)
  489. {
  490. lf_printf (file, "illegal_");
  491. lf_print_table_name (file, table);
  492. lf_printf (file, ":\n");
  493. print_idecode_invalid (file, result, invalid_illegal);
  494. lf_printf (file, "break_");
  495. lf_print_table_name (file, table);
  496. lf_printf (file, ":;\n");
  497. if (table->parent != NULL
  498. && (table->parent->opcode_rule->gen == switch_gen
  499. || table->parent->opcode_rule->gen == goto_switch_gen
  500. || table->parent->opcode_rule->gen == padded_switch_gen))
  501. {
  502. lf_indent (file, -2);
  503. lf_printf (file, "}\n");
  504. }
  505. }
  506. else
  507. {
  508. ERROR ("bad switch");
  509. }
  510. }
  511. void
  512. print_idecode_switch (lf *file, gen_entry *table, const char *result)
  513. {
  514. gen_entry_traverse_tree (file, table,
  515. 0,
  516. print_idecode_switch_start,
  517. print_idecode_switch_leaf,
  518. print_idecode_switch_end, (void *) result);
  519. }
  520. static void
  521. print_idecode_switch_function_header (lf *file,
  522. gen_entry *table,
  523. int is_function_definition,
  524. int nr_prefetched_words)
  525. {
  526. lf_printf (file, "\n");
  527. if (options.gen.code == generate_calls)
  528. {
  529. lf_printf (file, "static ");
  530. if (options.gen.icache)
  531. {
  532. lf_printf (file, "idecode_semantic *");
  533. }
  534. else
  535. {
  536. lf_printf (file, "unsigned_word");
  537. }
  538. if (is_function_definition)
  539. {
  540. lf_printf (file, "\n");
  541. }
  542. else
  543. {
  544. lf_printf (file, " ");
  545. }
  546. lf_print_table_name (file, table);
  547. lf_printf (file, "\n(");
  548. print_icache_function_formal (file, nr_prefetched_words);
  549. lf_printf (file, ")");
  550. if (!is_function_definition)
  551. {
  552. lf_printf (file, ";");
  553. }
  554. lf_printf (file, "\n");
  555. }
  556. if (options.gen.code == generate_jumps && is_function_definition)
  557. {
  558. lf_indent (file, -1);
  559. lf_print_table_name (file, table);
  560. lf_printf (file, ":\n");
  561. lf_indent (file, +1);
  562. }
  563. }
  564. static void
  565. idecode_declare_if_switch (lf *file, gen_entry *table, int depth, void *data)
  566. {
  567. if ((table->opcode_rule->gen == switch_gen || table->opcode_rule->gen == goto_switch_gen || table->opcode_rule->gen == padded_switch_gen) &&table->parent != NULL /* don't declare the top one yet */
  568. && table->parent->opcode_rule->gen == array_gen)
  569. {
  570. print_idecode_switch_function_header (file,
  571. table,
  572. 0 /*isnt function definition */ ,
  573. 0);
  574. }
  575. }
  576. static void
  577. idecode_expand_if_switch (lf *file, gen_entry *table, int depth, void *data)
  578. {
  579. if ((table->opcode_rule->gen == switch_gen || table->opcode_rule->gen == goto_switch_gen || table->opcode_rule->gen == padded_switch_gen) &&table->parent != NULL /* don't expand the top one yet */
  580. && table->parent->opcode_rule->gen == array_gen)
  581. {
  582. print_idecode_switch_function_header (file,
  583. table,
  584. 1 /*is function definition */ ,
  585. 0);
  586. if (options.gen.code == generate_calls)
  587. {
  588. lf_printf (file, "{\n");
  589. lf_indent (file, +2);
  590. }
  591. print_idecode_switch (file, table, "return");
  592. if (options.gen.code == generate_calls)
  593. {
  594. lf_indent (file, -2);
  595. lf_printf (file, "}\n");
  596. }
  597. }
  598. }
  599. /****************************************************************/
  600. void
  601. print_idecode_lookups (lf *file, gen_entry *table, cache_entry *cache_rules)
  602. {
  603. int depth;
  604. /* output switch function declarations where needed by tables */
  605. gen_entry_traverse_tree (file, table, 1, idecode_declare_if_switch, /* START */
  606. NULL, NULL, NULL);
  607. /* output tables where needed */
  608. for (depth = gen_entry_depth (table); depth > 0; depth--)
  609. {
  610. gen_entry_traverse_tree (file, table,
  611. 1 - depth,
  612. print_idecode_table_start,
  613. print_idecode_table_leaf,
  614. print_idecode_table_end, NULL);
  615. }
  616. /* output switch functions where needed */
  617. gen_entry_traverse_tree (file, table, 1, idecode_expand_if_switch, /* START */
  618. NULL, NULL, NULL);
  619. }
  620. void
  621. print_idecode_body (lf *file, gen_entry *table, const char *result)
  622. {
  623. if (table->opcode_rule->gen == switch_gen
  624. || table->opcode_rule->gen == goto_switch_gen
  625. || table->opcode_rule->gen == padded_switch_gen)
  626. {
  627. print_idecode_switch (file, table, result);
  628. }
  629. else
  630. {
  631. print_idecode_table (file, table, result);
  632. }
  633. }
  634. /****************************************************************/
  635. /* Output code to do any final checks on the decoded instruction.
  636. This includes things like verifying any on decoded fields have the
  637. correct value and checking that (for floating point) floating point
  638. hardware isn't disabled */
  639. void
  640. print_idecode_validate (lf *file,
  641. insn_entry * instruction, insn_opcodes *opcode_paths)
  642. {
  643. /* Validate: unchecked instruction fields
  644. If any constant fields in the instruction were not checked by the
  645. idecode tables, output code to check that they have the correct
  646. value here */
  647. {
  648. int nr_checks = 0;
  649. int word_nr;
  650. lf_printf (file, "\n");
  651. lf_indent_suppress (file);
  652. lf_printf (file, "#if defined (WITH_RESERVED_BITS)\n");
  653. lf_printf (file, "/* validate: ");
  654. print_insn_words (file, instruction);
  655. lf_printf (file, " */\n");
  656. for (word_nr = 0; word_nr < instruction->nr_words; word_nr++)
  657. {
  658. insn_uint check_mask = 0;
  659. insn_uint check_val = 0;
  660. insn_word_entry *word = instruction->word[word_nr];
  661. int bit_nr;
  662. /* form check_mask/check_val containing what needs to be checked
  663. in the instruction */
  664. for (bit_nr = 0; bit_nr < options.insn_bit_size; bit_nr++)
  665. {
  666. insn_bit_entry *bit = word->bit[bit_nr];
  667. insn_field_entry *field = bit->field;
  668. /* Make space for the next bit */
  669. check_mask <<= 1;
  670. check_val <<= 1;
  671. /* Only need to validate constant (and reserved)
  672. bits. Skip any others */
  673. if (field->type != insn_field_int
  674. && field->type != insn_field_reserved
  675. /* Consider a named field equal to a value to be just as
  676. constant as an integer field. */
  677. && (field->type != insn_field_string
  678. || field->conditions == NULL
  679. || field->conditions->test != insn_field_cond_eq
  680. || field->conditions->type != insn_field_cond_value))
  681. continue;
  682. /* Look through the list of opcode paths that lead to this
  683. instruction. See if any have failed to check the
  684. relevant bit */
  685. if (opcode_paths != NULL)
  686. {
  687. insn_opcodes *entry;
  688. for (entry = opcode_paths; entry != NULL; entry = entry->next)
  689. {
  690. opcode_field *opcode;
  691. for (opcode = entry->opcode;
  692. opcode != NULL; opcode = opcode->parent)
  693. {
  694. if (opcode->word_nr == word_nr
  695. && opcode->first <= bit_nr
  696. && opcode->last >= bit_nr)
  697. /* we've decoded on this bit */
  698. break;
  699. }
  700. if (opcode == NULL)
  701. /* the bit wasn't decoded on */
  702. break;
  703. }
  704. if (entry == NULL)
  705. /* all the opcode paths decoded on BIT_NR, no need
  706. to check it */
  707. continue;
  708. }
  709. check_mask |= 1;
  710. check_val |= bit->value;
  711. }
  712. /* if any bits not checked by opcode tables, output code to check them */
  713. if (check_mask)
  714. {
  715. if (nr_checks == 0)
  716. {
  717. lf_printf (file, "if (WITH_RESERVED_BITS)\n");
  718. lf_printf (file, " {\n");
  719. lf_indent (file, +4);
  720. }
  721. nr_checks++;
  722. if (options.insn_bit_size > 32)
  723. {
  724. lf_printf (file, "if ((instruction_%d\n", word_nr);
  725. lf_printf (file, " & UNSIGNED64 (0x%08lx%08lx))\n",
  726. (unsigned long) (check_mask >> 32),
  727. (unsigned long) (check_mask));
  728. lf_printf (file, " != UNSIGNED64 (0x%08lx%08lx))\n",
  729. (unsigned long) (check_val >> 32),
  730. (unsigned long) (check_val));
  731. }
  732. else
  733. {
  734. lf_printf (file,
  735. "if ((instruction_%d & 0x%08lx) != 0x%08lx)\n",
  736. word_nr, (unsigned long) (check_mask),
  737. (unsigned long) (check_val));
  738. }
  739. lf_indent (file, +2);
  740. print_idecode_invalid (file, "return", invalid_illegal);
  741. lf_indent (file, -2);
  742. }
  743. }
  744. if (nr_checks > 0)
  745. {
  746. lf_indent (file, -4);
  747. lf_printf (file, " }\n");
  748. }
  749. lf_indent_suppress (file);
  750. lf_printf (file, "#endif\n");
  751. }
  752. /* Validate: Floating Point hardware
  753. If the simulator is being built with out floating point hardware
  754. (different to it being disabled in the MSR) then floating point
  755. instructions are invalid */
  756. {
  757. if (filter_is_member (instruction->flags, "f"))
  758. {
  759. lf_printf (file, "\n");
  760. lf_indent_suppress (file);
  761. lf_printf (file, "#if defined(CURRENT_FLOATING_POINT)\n");
  762. lf_printf (file, "/* Validate: FP hardware exists */\n");
  763. lf_printf (file,
  764. "if (CURRENT_FLOATING_POINT != HARD_FLOATING_POINT) {\n");
  765. lf_indent (file, +2);
  766. print_idecode_invalid (file, "return", invalid_illegal);
  767. lf_indent (file, -2);
  768. lf_printf (file, "}\n");
  769. lf_indent_suppress (file);
  770. lf_printf (file, "#endif\n");
  771. }
  772. }
  773. /* Validate: Floating Point available
  774. If floating point is not available, we enter a floating point
  775. unavailable interrupt into the cache instead of the instruction
  776. proper.
  777. The PowerPC spec requires a CSI after MSR[FP] is changed and when
  778. ever a CSI occures we flush the instruction cache. */
  779. {
  780. if (filter_is_member (instruction->flags, "f"))
  781. {
  782. lf_printf (file, "\n");
  783. lf_indent_suppress (file);
  784. lf_printf (file, "#if defined(IS_FP_AVAILABLE)\n");
  785. lf_printf (file, "/* Validate: FP available according to cpu */\n");
  786. lf_printf (file, "if (!IS_FP_AVAILABLE) {\n");
  787. lf_indent (file, +2);
  788. print_idecode_invalid (file, "return", invalid_fp_unavailable);
  789. lf_indent (file, -2);
  790. lf_printf (file, "}\n");
  791. lf_indent_suppress (file);
  792. lf_printf (file, "#endif\n");
  793. }
  794. }
  795. /* Validate: Validate Instruction in correct slot
  796. Some architectures place restrictions on the slot that an
  797. instruction can be issued in */
  798. {
  799. if (filter_is_member (instruction->options, "s")
  800. || options.gen.slot_verification)
  801. {
  802. lf_printf (file, "\n");
  803. lf_indent_suppress (file);
  804. lf_printf (file, "#if defined(IS_WRONG_SLOT)\n");
  805. lf_printf (file,
  806. "/* Validate: Instruction issued in correct slot */\n");
  807. lf_printf (file, "if (IS_WRONG_SLOT) {\n");
  808. lf_indent (file, +2);
  809. print_idecode_invalid (file, "return", invalid_wrong_slot);
  810. lf_indent (file, -2);
  811. lf_printf (file, "}\n");
  812. lf_indent_suppress (file);
  813. lf_printf (file, "#endif\n");
  814. }
  815. }
  816. }
  817. /****************************************************************/
  818. void
  819. print_idecode_issue_function_header (lf *file,
  820. const char *processor,
  821. function_decl_type decl_type,
  822. int nr_prefetched_words)
  823. {
  824. int indent;
  825. lf_printf (file, "\n");
  826. switch (decl_type)
  827. {
  828. case is_function_declaration:
  829. lf_print__function_type_function (file, print_semantic_function_type,
  830. "INLINE_IDECODE", " ");
  831. break;
  832. case is_function_definition:
  833. lf_print__function_type_function (file, print_semantic_function_type,
  834. "INLINE_IDECODE", "\n");
  835. break;
  836. case is_function_variable:
  837. if (lf_get_file_type (file) == lf_is_h)
  838. lf_printf (file, "extern ");
  839. print_semantic_function_type (file);
  840. lf_printf (file, " (*");
  841. break;
  842. }
  843. indent = print_function_name (file,
  844. "issue",
  845. NULL,
  846. processor,
  847. NULL, function_name_prefix_idecode);
  848. switch (decl_type)
  849. {
  850. case is_function_definition:
  851. indent += lf_printf (file, " (");
  852. break;
  853. case is_function_declaration:
  854. lf_putstr (file, "\n(");
  855. indent = 1;
  856. break;
  857. case is_function_variable:
  858. lf_putstr (file, ")\n(");
  859. indent = 1;
  860. break;
  861. }
  862. lf_indent (file, +indent);
  863. print_semantic_function_formal (file, nr_prefetched_words);
  864. lf_putstr (file, ")");
  865. lf_indent (file, -indent);
  866. switch (decl_type)
  867. {
  868. case is_function_definition:
  869. lf_printf (file, "\n");
  870. break;
  871. case is_function_declaration:
  872. case is_function_variable:
  873. lf_putstr (file, ";\n");
  874. break;
  875. }
  876. }
  877. void
  878. print_idecode_globals (lf *file)
  879. {
  880. lf_printf (file, "enum {\n");
  881. lf_printf (file, " /* greater or equal to zero => table */\n");
  882. lf_printf (file, " function_entry = -1,\n");
  883. lf_printf (file, " boolean_entry = -2,\n");
  884. lf_printf (file, "};\n");
  885. lf_printf (file, "\n");
  886. lf_printf (file, "typedef struct _idecode_table_entry {\n");
  887. lf_printf (file, " int shift;\n");
  888. lf_printf (file, " uint%d_t mask;\n", options.insn_bit_size);
  889. lf_printf (file, " uint%d_t value;\n", options.insn_bit_size);
  890. lf_printf (file, " void *function_or_table;\n");
  891. lf_printf (file, "} idecode_table_entry;\n");
  892. }