ld-insn.c 27 KB

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