ld-insn.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /* This file is part of the program psim.
  2. Copyright 1994, 1995, 1996, 2003 Andrew Cagney
  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. static void
  23. update_depth(insn_table *entry,
  24. lf *file,
  25. void *data,
  26. insn *instruction,
  27. int depth)
  28. {
  29. int *max_depth = (int*)data;
  30. if (*max_depth < depth)
  31. *max_depth = depth;
  32. }
  33. int
  34. insn_table_depth(insn_table *table)
  35. {
  36. int depth = 0;
  37. insn_table_traverse_tree(table,
  38. NULL,
  39. &depth,
  40. 1,
  41. NULL, /*start*/
  42. update_depth,
  43. NULL, /*end*/
  44. NULL); /*padding*/
  45. return depth;
  46. }
  47. static insn_fields *
  48. parse_insn_format(table_entry *entry,
  49. char *format)
  50. {
  51. char *chp;
  52. insn_fields *fields = ZALLOC(insn_fields);
  53. /* create a leading sentinal */
  54. fields->first = ZALLOC(insn_field);
  55. fields->first->first = -1;
  56. fields->first->last = -1;
  57. fields->first->width = 0;
  58. /* and a trailing sentinal */
  59. fields->last = ZALLOC(insn_field);
  60. fields->last->first = insn_bit_size;
  61. fields->last->last = insn_bit_size;
  62. fields->last->width = 0;
  63. /* link them together */
  64. fields->first->next = fields->last;
  65. fields->last->prev = fields->first;
  66. /* now work through the formats */
  67. chp = format;
  68. while (*chp != '\0') {
  69. char *start_pos;
  70. char *start_val;
  71. int strlen_val;
  72. int strlen_pos;
  73. insn_field *new_field;
  74. /* sanity check */
  75. if (!isdigit(*chp)) {
  76. error("%s:%d: missing position field at `%s'\n",
  77. entry->file_name, entry->line_nr, chp);
  78. }
  79. /* break out the bit position */
  80. start_pos = chp;
  81. while (isdigit(*chp))
  82. chp++;
  83. strlen_pos = chp - start_pos;
  84. if (*chp == '.' && strlen_pos > 0)
  85. chp++;
  86. else {
  87. error("%s:%d: missing field value at %s\n",
  88. entry->file_name, entry->line_nr, chp);
  89. break;
  90. }
  91. /* break out the value */
  92. start_val = chp;
  93. while ((*start_val == '/' && *chp == '/')
  94. || (isdigit(*start_val) && isdigit(*chp))
  95. || (isalpha(*start_val) && (isalnum(*chp) || *chp == '_')))
  96. chp++;
  97. strlen_val = chp - start_val;
  98. if (*chp == ',')
  99. chp++;
  100. else if (*chp != '\0' || strlen_val == 0) {
  101. error("%s:%d: missing field terminator at %s\n",
  102. entry->file_name, entry->line_nr, chp);
  103. break;
  104. }
  105. /* create a new field and insert it */
  106. new_field = ZALLOC(insn_field);
  107. new_field->next = fields->last;
  108. new_field->prev = fields->last->prev;
  109. new_field->next->prev = new_field;
  110. new_field->prev->next = new_field;
  111. /* the value */
  112. new_field->val_string = (char*)zalloc(strlen_val+1);
  113. strncpy(new_field->val_string, start_val, strlen_val);
  114. if (isdigit(*new_field->val_string)) {
  115. new_field->val_int = a2i(new_field->val_string);
  116. new_field->is_int = 1;
  117. }
  118. else if (new_field->val_string[0] == '/') {
  119. new_field->is_slash = 1;
  120. }
  121. else {
  122. new_field->is_string = 1;
  123. }
  124. /* the pos */
  125. new_field->pos_string = (char*)zalloc(strlen_pos+1);
  126. strncpy(new_field->pos_string, start_pos, strlen_pos);
  127. new_field->first = target_a2i(hi_bit_nr, new_field->pos_string);
  128. new_field->last = new_field->next->first - 1; /* guess */
  129. new_field->width = new_field->last - new_field->first + 1; /* guess */
  130. new_field->prev->last = new_field->first-1; /*fix*/
  131. new_field->prev->width = new_field->first - new_field->prev->first; /*fix*/
  132. }
  133. /* fiddle first/last so that the sentinals `disapear' */
  134. ASSERT(fields->first->last < 0);
  135. ASSERT(fields->last->first >= insn_bit_size);
  136. fields->first = fields->first->next;
  137. fields->last = fields->last->prev;
  138. /* now go over this again, pointing each bit position at a field
  139. record */
  140. {
  141. int i;
  142. insn_field *field;
  143. field = fields->first;
  144. for (i = 0; i < insn_bit_size; i++) {
  145. while (field->last < i)
  146. field = field->next;
  147. fields->bits[i] = field;
  148. }
  149. }
  150. /* go over each of the fields, and compute a `value' for the insn */
  151. {
  152. insn_field *field;
  153. fields->value = 0;
  154. for (field = fields->first;
  155. field->last < insn_bit_size;
  156. field = field->next) {
  157. fields->value <<= field->width;
  158. if (field->is_int)
  159. fields->value |= field->val_int;
  160. }
  161. }
  162. return fields;
  163. }
  164. void
  165. parse_include_entry (table *file,
  166. table_entry *file_entry,
  167. filter *filters,
  168. table_include *includes)
  169. {
  170. /* parse the include file_entry */
  171. if (file_entry->nr_fields < 4)
  172. error ("Incorrect nr fields for include record\n");
  173. /* process it */
  174. if (!is_filtered_out(file_entry->fields[include_flags], filters))
  175. {
  176. table_push (file, includes,
  177. file_entry->fields[include_path],
  178. file_entry->nr_fields, file_entry->nr_fields);
  179. }
  180. }
  181. static void
  182. model_table_insert(insn_table *table,
  183. table_entry *file_entry)
  184. {
  185. int len;
  186. /* create a new model */
  187. model *new_model = ZALLOC(model);
  188. new_model->name = file_entry->fields[model_identifer];
  189. new_model->printable_name = file_entry->fields[model_name];
  190. new_model->insn_default = file_entry->fields[model_default];
  191. while (*new_model->insn_default && isspace(*new_model->insn_default))
  192. new_model->insn_default++;
  193. len = strlen(new_model->insn_default);
  194. if (max_model_fields_len < len)
  195. max_model_fields_len = len;
  196. /* append it to the end of the model list */
  197. if (last_model)
  198. last_model->next = new_model;
  199. else
  200. models = new_model;
  201. last_model = new_model;
  202. }
  203. static void
  204. model_table_insert_specific(insn_table *table,
  205. table_entry *file_entry,
  206. insn **start_ptr,
  207. insn **end_ptr)
  208. {
  209. insn *ptr = ZALLOC(insn);
  210. ptr->file_entry = file_entry;
  211. if (*end_ptr)
  212. (*end_ptr)->next = ptr;
  213. else
  214. (*start_ptr) = ptr;
  215. (*end_ptr) = ptr;
  216. }
  217. static void
  218. insn_table_insert_function(insn_table *table,
  219. table_entry *file_entry)
  220. {
  221. /* create a new function */
  222. insn *new_function = ZALLOC(insn);
  223. new_function->file_entry = file_entry;
  224. /* append it to the end of the function list */
  225. if (table->last_function)
  226. table->last_function->next = new_function;
  227. else
  228. table->functions = new_function;
  229. table->last_function = new_function;
  230. }
  231. extern void
  232. insn_table_insert_insn(insn_table *table,
  233. table_entry *file_entry,
  234. insn_fields *fields)
  235. {
  236. insn **ptr_to_cur_insn = &table->insns;
  237. insn *cur_insn = *ptr_to_cur_insn;
  238. table_model_entry *insn_model_ptr;
  239. model *model_ptr;
  240. /* create a new instruction */
  241. insn *new_insn = ZALLOC(insn);
  242. new_insn->file_entry = file_entry;
  243. new_insn->fields = fields;
  244. /* Check out any model information returned to make sure the model
  245. is correct. */
  246. for(insn_model_ptr = file_entry->model_first; insn_model_ptr; insn_model_ptr = insn_model_ptr->next) {
  247. char *name = insn_model_ptr->fields[insn_model_name];
  248. int len = strlen (insn_model_ptr->fields[insn_model_fields]);
  249. while (len > 0 && isspace(*insn_model_ptr->fields[insn_model_fields])) {
  250. len--;
  251. insn_model_ptr->fields[insn_model_fields]++;
  252. }
  253. if (max_model_fields_len < len)
  254. max_model_fields_len = len;
  255. for(model_ptr = models; model_ptr; model_ptr = model_ptr->next) {
  256. if (strcmp(name, model_ptr->printable_name) == 0) {
  257. /* Replace the name field with that of the global model, so that when we
  258. want to print it out, we can just compare pointers. */
  259. insn_model_ptr->fields[insn_model_name] = model_ptr->printable_name;
  260. break;
  261. }
  262. }
  263. if (!model_ptr)
  264. error("%s:%d: machine model `%s' was not known about\n",
  265. file_entry->file_name, file_entry->line_nr, name);
  266. }
  267. /* insert it according to the order of the fields */
  268. while (cur_insn != NULL
  269. && new_insn->fields->value >= cur_insn->fields->value) {
  270. ptr_to_cur_insn = &cur_insn->next;
  271. cur_insn = *ptr_to_cur_insn;
  272. }
  273. new_insn->next = cur_insn;
  274. *ptr_to_cur_insn = new_insn;
  275. table->nr_insn++;
  276. }
  277. insn_table *
  278. load_insn_table(const char *file_name,
  279. decode_table *decode_rules,
  280. filter *filters,
  281. table_include *includes,
  282. cache_table **cache_rules)
  283. {
  284. table *file = table_open(file_name, nr_insn_table_fields, nr_insn_model_table_fields);
  285. insn_table *table = ZALLOC(insn_table);
  286. table_entry *file_entry;
  287. table->opcode_rule = decode_rules;
  288. while ((file_entry = table_entry_read(file)) != NULL) {
  289. if (it_is("function", file_entry->fields[insn_flags])
  290. || it_is("internal", file_entry->fields[insn_flags])) {
  291. insn_table_insert_function(table, file_entry);
  292. }
  293. else if ((it_is("function", file_entry->fields[insn_form])
  294. || it_is("internal", file_entry->fields[insn_form]))
  295. && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
  296. /* Ok, this is evil. Need to convert a new style function into
  297. an old style function. Construct an old style table and then
  298. copy it back. */
  299. char *fields[nr_insn_table_fields];
  300. memset (fields, 0, sizeof fields);
  301. fields[insn_flags] = file_entry->fields[insn_form];
  302. fields[function_type] = file_entry->fields[insn_name];
  303. fields[function_name] = file_entry->fields[insn_comment];
  304. fields[function_param] = file_entry->fields[insn_field_6];
  305. memcpy (file_entry->fields, fields,
  306. sizeof (fields[0]) * file_entry->nr_fields);
  307. insn_table_insert_function(table, file_entry);
  308. #if 0
  309. ":" "..."
  310. ":" <filter-flags>
  311. ":" <filter-models>
  312. ":" <typedef>
  313. ":" <name>
  314. [ ":" <parameter-list> ]
  315. <nl>
  316. [ <function-model> ]
  317. <code-block>
  318. #endif
  319. }
  320. else if (it_is("model", file_entry->fields[insn_flags])) {
  321. model_table_insert(table, file_entry);
  322. }
  323. else if (it_is("model-macro", file_entry->fields[insn_flags])) {
  324. model_table_insert_specific(table, file_entry, &model_macros, &last_model_macro);
  325. }
  326. else if (it_is("model-function", file_entry->fields[insn_flags])) {
  327. model_table_insert_specific(table, file_entry, &model_functions, &last_model_function);
  328. }
  329. else if (it_is("model-internal", file_entry->fields[insn_flags])) {
  330. model_table_insert_specific(table, file_entry, &model_internal, &last_model_internal);
  331. }
  332. else if (it_is("model-static", file_entry->fields[insn_flags])) {
  333. model_table_insert_specific(table, file_entry, &model_static, &last_model_static);
  334. }
  335. else if (it_is("model-data", file_entry->fields[insn_flags])) {
  336. model_table_insert_specific(table, file_entry, &model_data, &last_model_data);
  337. }
  338. else if (it_is("include", file_entry->fields[insn_form])
  339. && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
  340. parse_include_entry (file, file_entry, filters, includes);
  341. }
  342. else if ((it_is("cache", file_entry->fields[insn_form])
  343. || it_is("compute", file_entry->fields[insn_form])
  344. || it_is("scratch", file_entry->fields[insn_form]))
  345. && !is_filtered_out(file_entry->fields[insn_flags], filters)) {
  346. append_cache_rule (cache_rules,
  347. file_entry->fields[insn_form], /* type */
  348. file_entry->fields[cache_name],
  349. file_entry->fields[cache_derived_name],
  350. file_entry->fields[cache_type_def],
  351. file_entry->fields[cache_expression],
  352. file_entry);
  353. }
  354. else {
  355. insn_fields *fields;
  356. /* skip instructions that aren't relevant to the mode */
  357. if (is_filtered_out(file_entry->fields[insn_flags], filters)) {
  358. fprintf(stderr, "Dropping %s - %s\n",
  359. file_entry->fields[insn_name],
  360. file_entry->fields[insn_flags]);
  361. }
  362. else {
  363. /* create/insert the new instruction */
  364. fields = parse_insn_format(file_entry,
  365. file_entry->fields[insn_format]);
  366. insn_table_insert_insn(table, file_entry, fields);
  367. }
  368. }
  369. }
  370. return table;
  371. }
  372. extern void
  373. insn_table_traverse_tree(insn_table *table,
  374. lf *file,
  375. void *data,
  376. int depth,
  377. leaf_handler *start,
  378. insn_handler *leaf,
  379. leaf_handler *end,
  380. padding_handler *padding)
  381. {
  382. insn_table *entry;
  383. int entry_nr;
  384. ASSERT(table != NULL
  385. && table->opcode != NULL
  386. && table->nr_entries > 0
  387. && table->entries != 0);
  388. if (start != NULL && depth >= 0)
  389. start(table, file, data, depth);
  390. for (entry_nr = 0, entry = table->entries;
  391. entry_nr < (table->opcode->is_boolean
  392. ? 2
  393. : (1 << (table->opcode->last - table->opcode->first + 1)));
  394. entry_nr ++) {
  395. if (entry == NULL
  396. || (!table->opcode->is_boolean
  397. && entry_nr < entry->opcode_nr)) {
  398. if (padding != NULL && depth >= 0)
  399. padding(table, file, data, depth, entry_nr);
  400. }
  401. else {
  402. ASSERT(entry != NULL && (entry->opcode_nr == entry_nr
  403. || table->opcode->is_boolean));
  404. if (entry->opcode != NULL && depth != 0) {
  405. insn_table_traverse_tree(entry, file, data, depth+1,
  406. start, leaf, end, padding);
  407. }
  408. else if (depth >= 0) {
  409. if (leaf != NULL)
  410. leaf(entry, file, data, entry->insns, depth);
  411. }
  412. entry = entry->sibling;
  413. }
  414. }
  415. if (end != NULL && depth >= 0)
  416. end(table, file, data, depth);
  417. }
  418. extern void
  419. insn_table_traverse_function(insn_table *table,
  420. lf *file,
  421. void *data,
  422. function_handler *leaf)
  423. {
  424. insn *function;
  425. for (function = table->functions;
  426. function != NULL;
  427. function = function->next) {
  428. leaf(table, file, data, function->file_entry);
  429. }
  430. }
  431. extern void
  432. insn_table_traverse_insn(insn_table *table,
  433. lf *file,
  434. void *data,
  435. insn_handler *handler)
  436. {
  437. insn *instruction;
  438. for (instruction = table->insns;
  439. instruction != NULL;
  440. instruction = instruction->next) {
  441. handler(table, file, data, instruction, 0);
  442. }
  443. }
  444. /****************************************************************/
  445. typedef enum {
  446. field_constant_int = 1,
  447. field_constant_slash = 2,
  448. field_constant_string = 3
  449. } constant_field_types;
  450. static int
  451. insn_field_is_constant(insn_field *field,
  452. decode_table *rule)
  453. {
  454. /* field is an integer */
  455. if (field->is_int)
  456. return field_constant_int;
  457. /* field is `/' and treating that as a constant */
  458. if (field->is_slash && rule->force_slash)
  459. return field_constant_slash;
  460. /* field, though variable is on the list */
  461. if (field->is_string && rule->force_expansion != NULL) {
  462. char *forced_fields = rule->force_expansion;
  463. while (*forced_fields != '\0') {
  464. int field_len;
  465. char *end = strchr(forced_fields, ',');
  466. if (end == NULL)
  467. field_len = strlen(forced_fields);
  468. else
  469. field_len = end-forced_fields;
  470. if (strncmp(forced_fields, field->val_string, field_len) == 0
  471. && field->val_string[field_len] == '\0')
  472. return field_constant_string;
  473. forced_fields += field_len;
  474. if (*forced_fields == ',')
  475. forced_fields++;
  476. }
  477. }
  478. return 0;
  479. }
  480. static opcode_field *
  481. insn_table_find_opcode_field(insn *insns,
  482. decode_table *rule,
  483. int string_only)
  484. {
  485. opcode_field *curr_opcode = ZALLOC(opcode_field);
  486. insn *entry;
  487. ASSERT(rule);
  488. curr_opcode->first = insn_bit_size;
  489. curr_opcode->last = -1;
  490. for (entry = insns; entry != NULL; entry = entry->next) {
  491. insn_fields *fields = entry->fields;
  492. opcode_field new_opcode;
  493. /* find a start point for the opcode field */
  494. new_opcode.first = rule->first;
  495. while (new_opcode.first <= rule->last
  496. && (!string_only
  497. || insn_field_is_constant(fields->bits[new_opcode.first],
  498. rule) != field_constant_string)
  499. && (string_only
  500. || !insn_field_is_constant(fields->bits[new_opcode.first],
  501. rule)))
  502. new_opcode.first = fields->bits[new_opcode.first]->last + 1;
  503. ASSERT(new_opcode.first > rule->last
  504. || (string_only
  505. && insn_field_is_constant(fields->bits[new_opcode.first],
  506. rule) == field_constant_string)
  507. || (!string_only
  508. && insn_field_is_constant(fields->bits[new_opcode.first],
  509. rule)));
  510. /* find the end point for the opcode field */
  511. new_opcode.last = rule->last;
  512. while (new_opcode.last >= rule->first
  513. && (!string_only
  514. || insn_field_is_constant(fields->bits[new_opcode.last],
  515. rule) != field_constant_string)
  516. && (string_only
  517. || !insn_field_is_constant(fields->bits[new_opcode.last],
  518. rule)))
  519. new_opcode.last = fields->bits[new_opcode.last]->first - 1;
  520. ASSERT(new_opcode.last < rule->first
  521. || (string_only
  522. && insn_field_is_constant(fields->bits[new_opcode.last],
  523. rule) == field_constant_string)
  524. || (!string_only
  525. && insn_field_is_constant(fields->bits[new_opcode.last],
  526. rule)));
  527. /* now see if our current opcode needs expanding */
  528. if (new_opcode.first <= rule->last
  529. && curr_opcode->first > new_opcode.first)
  530. curr_opcode->first = new_opcode.first;
  531. if (new_opcode.last >= rule->first
  532. && curr_opcode->last < new_opcode.last)
  533. curr_opcode->last = new_opcode.last;
  534. }
  535. /* was any thing interesting found? */
  536. if (curr_opcode->first > rule->last) {
  537. ASSERT(curr_opcode->last < rule->first);
  538. return NULL;
  539. }
  540. ASSERT(curr_opcode->last >= rule->first);
  541. ASSERT(curr_opcode->first <= rule->last);
  542. /* if something was found, check it includes the forced field range */
  543. if (!string_only
  544. && curr_opcode->first > rule->force_first) {
  545. curr_opcode->first = rule->force_first;
  546. }
  547. if (!string_only
  548. && curr_opcode->last < rule->force_last) {
  549. curr_opcode->last = rule->force_last;
  550. }
  551. /* handle special case elminating any need to do shift after mask */
  552. if (string_only
  553. && rule->force_last == insn_bit_size-1) {
  554. curr_opcode->last = insn_bit_size-1;
  555. }
  556. /* handle any special cases */
  557. switch (rule->type) {
  558. case normal_decode_rule:
  559. /* let the above apply */
  560. break;
  561. case expand_forced_rule:
  562. /* expand a limited nr of bits, ignoring the rest */
  563. curr_opcode->first = rule->force_first;
  564. curr_opcode->last = rule->force_last;
  565. break;
  566. case boolean_rule:
  567. curr_opcode->is_boolean = 1;
  568. curr_opcode->boolean_constant = rule->special_constant;
  569. break;
  570. default:
  571. error("Something is going wrong\n");
  572. }
  573. return curr_opcode;
  574. }
  575. static void
  576. insn_table_insert_expanded(insn_table *table,
  577. insn *old_insn,
  578. int new_opcode_nr,
  579. insn_bits *new_bits)
  580. {
  581. insn_table **ptr_to_cur_entry = &table->entries;
  582. insn_table *cur_entry = *ptr_to_cur_entry;
  583. /* find the new table for this entry */
  584. while (cur_entry != NULL
  585. && cur_entry->opcode_nr < new_opcode_nr) {
  586. ptr_to_cur_entry = &cur_entry->sibling;
  587. cur_entry = *ptr_to_cur_entry;
  588. }
  589. if (cur_entry == NULL || cur_entry->opcode_nr != new_opcode_nr) {
  590. insn_table *new_entry = ZALLOC(insn_table);
  591. new_entry->opcode_nr = new_opcode_nr;
  592. new_entry->expanded_bits = new_bits;
  593. new_entry->opcode_rule = table->opcode_rule->next;
  594. new_entry->sibling = cur_entry;
  595. new_entry->parent = table;
  596. *ptr_to_cur_entry = new_entry;
  597. cur_entry = new_entry;
  598. table->nr_entries++;
  599. }
  600. /* ASSERT new_bits == cur_entry bits */
  601. ASSERT(cur_entry != NULL && cur_entry->opcode_nr == new_opcode_nr);
  602. insn_table_insert_insn(cur_entry,
  603. old_insn->file_entry,
  604. old_insn->fields);
  605. }
  606. static void
  607. insn_table_expand_opcode(insn_table *table,
  608. insn *instruction,
  609. int field_nr,
  610. int opcode_nr,
  611. insn_bits *bits)
  612. {
  613. if (field_nr > table->opcode->last) {
  614. insn_table_insert_expanded(table, instruction, opcode_nr, bits);
  615. }
  616. else {
  617. insn_field *field = instruction->fields->bits[field_nr];
  618. if (field->is_int || field->is_slash) {
  619. ASSERT(field->first >= table->opcode->first
  620. && field->last <= table->opcode->last);
  621. insn_table_expand_opcode(table, instruction, field->last+1,
  622. ((opcode_nr << field->width) + field->val_int),
  623. bits);
  624. }
  625. else {
  626. int val;
  627. int last_pos = ((field->last < table->opcode->last)
  628. ? field->last : table->opcode->last);
  629. int first_pos = ((field->first > table->opcode->first)
  630. ? field->first : table->opcode->first);
  631. int width = last_pos - first_pos + 1;
  632. int last_val = (table->opcode->is_boolean
  633. ? 2 : (1 << width));
  634. for (val = 0; val < last_val; val++) {
  635. insn_bits *new_bits = ZALLOC(insn_bits);
  636. new_bits->field = field;
  637. new_bits->value = val;
  638. new_bits->last = bits;
  639. new_bits->opcode = table->opcode;
  640. insn_table_expand_opcode(table, instruction, last_pos+1,
  641. ((opcode_nr << width) | val),
  642. new_bits);
  643. }
  644. }
  645. }
  646. }
  647. static void
  648. insn_table_insert_expanding(insn_table *table,
  649. insn *entry)
  650. {
  651. insn_table_expand_opcode(table,
  652. entry,
  653. table->opcode->first,
  654. 0,
  655. table->expanded_bits);
  656. }
  657. extern void
  658. insn_table_expand_insns(insn_table *table)
  659. {
  660. ASSERT(table->nr_insn >= 1);
  661. /* determine a valid opcode */
  662. while (table->opcode_rule) {
  663. /* specials only for single instructions */
  664. if ((table->nr_insn > 1
  665. && table->opcode_rule->special_mask == 0
  666. && table->opcode_rule->type == normal_decode_rule)
  667. || (table->nr_insn == 1
  668. && table->opcode_rule->special_mask != 0
  669. && ((table->insns->fields->value
  670. & table->opcode_rule->special_mask)
  671. == table->opcode_rule->special_value))
  672. || (generate_expanded_instructions
  673. && table->opcode_rule->special_mask == 0
  674. && table->opcode_rule->type == normal_decode_rule))
  675. table->opcode =
  676. insn_table_find_opcode_field(table->insns,
  677. table->opcode_rule,
  678. table->nr_insn == 1/*string*/
  679. );
  680. if (table->opcode != NULL)
  681. break;
  682. table->opcode_rule = table->opcode_rule->next;
  683. }
  684. /* did we find anything */
  685. if (table->opcode == NULL) {
  686. return;
  687. }
  688. ASSERT(table->opcode != NULL);
  689. /* back link what we found to its parent */
  690. if (table->parent != NULL) {
  691. ASSERT(table->parent->opcode != NULL);
  692. table->opcode->parent = table->parent->opcode;
  693. }
  694. /* expand the raw instructions according to the opcode */
  695. {
  696. insn *entry;
  697. for (entry = table->insns; entry != NULL; entry = entry->next) {
  698. insn_table_insert_expanding(table, entry);
  699. }
  700. }
  701. /* and do the same for the sub entries */
  702. {
  703. insn_table *entry;
  704. for (entry = table->entries; entry != NULL; entry = entry->sibling) {
  705. insn_table_expand_insns(entry);
  706. }
  707. }
  708. }
  709. #ifdef MAIN
  710. static void
  711. dump_insn_field(insn_field *field,
  712. int indent)
  713. {
  714. printf("(insn_field*)0x%x\n", (unsigned)field);
  715. dumpf(indent, "(first %d)\n", field->first);
  716. dumpf(indent, "(last %d)\n", field->last);
  717. dumpf(indent, "(width %d)\n", field->width);
  718. if (field->is_int)
  719. dumpf(indent, "(is_int %d)\n", field->val_int);
  720. if (field->is_slash)
  721. dumpf(indent, "(is_slash)\n");
  722. if (field->is_string)
  723. dumpf(indent, "(is_string `%s')\n", field->val_string);
  724. dumpf(indent, "(next 0x%x)\n", field->next);
  725. dumpf(indent, "(prev 0x%x)\n", field->prev);
  726. }
  727. static void
  728. dump_insn_fields(insn_fields *fields,
  729. int indent)
  730. {
  731. int i;
  732. printf("(insn_fields*)%p\n", fields);
  733. dumpf(indent, "(first 0x%x)\n", fields->first);
  734. dumpf(indent, "(last 0x%x)\n", fields->last);
  735. dumpf(indent, "(value 0x%x)\n", fields->value);
  736. for (i = 0; i < insn_bit_size; i++) {
  737. dumpf(indent, "(bits[%d] ", i, fields->bits[i]);
  738. dump_insn_field(fields->bits[i], indent+1);
  739. dumpf(indent, " )\n");
  740. }
  741. }
  742. static void
  743. dump_opcode_field(opcode_field *field, int indent, int levels)
  744. {
  745. printf("(opcode_field*)%p\n", field);
  746. if (levels && field != NULL) {
  747. dumpf(indent, "(first %d)\n", field->first);
  748. dumpf(indent, "(last %d)\n", field->last);
  749. dumpf(indent, "(is_boolean %d)\n", field->is_boolean);
  750. dumpf(indent, "(parent ");
  751. dump_opcode_field(field->parent, indent, levels-1);
  752. }
  753. }
  754. static void
  755. dump_insn_bits(insn_bits *bits, int indent, int levels)
  756. {
  757. printf("(insn_bits*)%p\n", bits);
  758. if (levels && bits != NULL) {
  759. dumpf(indent, "(value %d)\n", bits->value);
  760. dumpf(indent, "(opcode ");
  761. dump_opcode_field(bits->opcode, indent+1, 0);
  762. dumpf(indent, " )\n");
  763. dumpf(indent, "(field ");
  764. dump_insn_field(bits->field, indent+1);
  765. dumpf(indent, " )\n");
  766. dumpf(indent, "(last ");
  767. dump_insn_bits(bits->last, indent+1, levels-1);
  768. }
  769. }
  770. static void
  771. dump_insn(insn *entry, int indent, int levels)
  772. {
  773. printf("(insn*)%p\n", entry);
  774. if (levels && entry != NULL) {
  775. dumpf(indent, "(file_entry ");
  776. dump_table_entry(entry->file_entry, indent+1);
  777. dumpf(indent, " )\n");
  778. dumpf(indent, "(fields ");
  779. dump_insn_fields(entry->fields, indent+1);
  780. dumpf(indent, " )\n");
  781. dumpf(indent, "(next ");
  782. dump_insn(entry->next, indent+1, levels-1);
  783. dumpf(indent, " )\n");
  784. }
  785. }
  786. static void
  787. dump_insn_table(insn_table *table,
  788. int indent, int levels)
  789. {
  790. printf("(insn_table*)%p\n", table);
  791. if (levels && table != NULL) {
  792. dumpf(indent, "(opcode_nr %d)\n", table->opcode_nr);
  793. dumpf(indent, "(expanded_bits ");
  794. dump_insn_bits(table->expanded_bits, indent+1, -1);
  795. dumpf(indent, " )\n");
  796. dumpf(indent, "(int nr_insn %d)\n", table->nr_insn);
  797. dumpf(indent, "(insns ");
  798. dump_insn(table->insns, indent+1, table->nr_insn);
  799. dumpf(indent, " )\n");
  800. dumpf(indent, "(opcode_rule ");
  801. dump_decode_rule(table->opcode_rule, indent+1);
  802. dumpf(indent, " )\n");
  803. dumpf(indent, "(opcode ");
  804. dump_opcode_field(table->opcode, indent+1, 1);
  805. dumpf(indent, " )\n");
  806. dumpf(indent, "(nr_entries %d)\n", table->entries);
  807. dumpf(indent, "(entries ");
  808. dump_insn_table(table->entries, indent+1, table->nr_entries);
  809. dumpf(indent, " )\n");
  810. dumpf(indent, "(sibling ", table->sibling);
  811. dump_insn_table(table->sibling, indent+1, levels-1);
  812. dumpf(indent, " )\n");
  813. dumpf(indent, "(parent ", table->parent);
  814. dump_insn_table(table->parent, indent+1, 0);
  815. dumpf(indent, " )\n");
  816. }
  817. }
  818. int insn_bit_size = max_insn_bit_size;
  819. int hi_bit_nr;
  820. int generate_expanded_instructions;
  821. int
  822. main(int argc, char **argv)
  823. {
  824. filter *filters = NULL;
  825. decode_table *decode_rules = NULL;
  826. insn_table *instructions = NULL;
  827. cache_table *cache_rules = NULL;
  828. if (argc != 5)
  829. error("Usage: insn <filter> <hi-bit-nr> <decode-table> <insn-table>\n");
  830. filters = new_filter(argv[1], filters);
  831. hi_bit_nr = a2i(argv[2]);
  832. ASSERT(hi_bit_nr < insn_bit_size);
  833. decode_rules = load_decode_table(argv[3], hi_bit_nr);
  834. instructions = load_insn_table(argv[4], decode_rules, filters, NULL,
  835. &cache_rules);
  836. insn_table_expand_insns(instructions);
  837. dump_insn_table(instructions, 0, -1);
  838. return 0;
  839. }
  840. #endif