gen.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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. static insn_uint
  24. sub_val (insn_uint val, int val_last_pos, int first_pos, int last_pos)
  25. {
  26. return ((val >> (val_last_pos - last_pos))
  27. & (((insn_uint) 1 << (last_pos - first_pos + 1)) - 1));
  28. }
  29. static void
  30. update_depth (lf *file, gen_entry *entry, int depth, void *data)
  31. {
  32. int *max_depth = (int *) data;
  33. if (*max_depth < depth)
  34. *max_depth = depth;
  35. }
  36. int
  37. gen_entry_depth (gen_entry *table)
  38. {
  39. int depth = 0;
  40. gen_entry_traverse_tree (NULL, table, 1, NULL, /*start */
  41. update_depth, NULL, /*end */
  42. &depth); /* data */
  43. return depth;
  44. }
  45. static void
  46. print_gen_entry_path (line_ref *line, gen_entry *table, error_func *print)
  47. {
  48. if (table->parent == NULL)
  49. {
  50. if (table->top->model != NULL)
  51. print (line, "%s", table->top->model->name);
  52. else
  53. print (line, "");
  54. }
  55. else
  56. {
  57. print_gen_entry_path (line, table->parent, print);
  58. print (NULL, ".%d", table->opcode_nr);
  59. }
  60. }
  61. static void
  62. print_gen_entry_insns (gen_entry *table,
  63. error_func *print,
  64. char *first_message, char *next_message)
  65. {
  66. insn_list *i;
  67. char *message;
  68. message = first_message;
  69. for (i = table->insns; i != NULL; i = i->next)
  70. {
  71. insn_entry *insn = i->insn;
  72. print_gen_entry_path (insn->line, table, print);
  73. print (NULL, ": %s.%s %s\n", insn->format_name, insn->name, message);
  74. if (next_message != NULL)
  75. message = next_message;
  76. }
  77. }
  78. /* same as strcmp */
  79. static int
  80. insn_field_cmp (insn_word_entry *l, insn_word_entry *r)
  81. {
  82. while (1)
  83. {
  84. int bit_nr;
  85. if (l == NULL && r == NULL)
  86. return 0; /* all previous fields the same */
  87. if (l == NULL)
  88. return -1; /* left shorter than right */
  89. if (r == NULL)
  90. return +1; /* left longer than right */
  91. for (bit_nr = 0; bit_nr < options.insn_bit_size; bit_nr++)
  92. {
  93. if (l->bit[bit_nr]->field->type != insn_field_string)
  94. continue;
  95. if (r->bit[bit_nr]->field->type != insn_field_string)
  96. continue;
  97. if (l->bit[bit_nr]->field->conditions == NULL)
  98. continue;
  99. if (r->bit[bit_nr]->field->conditions == NULL)
  100. continue;
  101. if (0)
  102. printf ("%s%s%s VS %s%s%s\n",
  103. l->bit[bit_nr]->field->val_string,
  104. l->bit[bit_nr]->field->conditions->test ==
  105. insn_field_cond_eq ? "=" : "!",
  106. l->bit[bit_nr]->field->conditions->string,
  107. r->bit[bit_nr]->field->val_string,
  108. r->bit[bit_nr]->field->conditions->test ==
  109. insn_field_cond_eq ? "=" : "!",
  110. r->bit[bit_nr]->field->conditions->string);
  111. if (l->bit[bit_nr]->field->conditions->test == insn_field_cond_eq
  112. && r->bit[bit_nr]->field->conditions->test ==
  113. insn_field_cond_eq)
  114. {
  115. if (l->bit[bit_nr]->field->conditions->type ==
  116. insn_field_cond_field
  117. && r->bit[bit_nr]->field->conditions->type ==
  118. insn_field_cond_field)
  119. /* somewhat arbitrary */
  120. {
  121. int cmp = strcmp (l->bit[bit_nr]->field->conditions->string,
  122. r->bit[bit_nr]->field->conditions->
  123. string);
  124. if (cmp != 0)
  125. return cmp;
  126. else
  127. continue;
  128. }
  129. if (l->bit[bit_nr]->field->conditions->type ==
  130. insn_field_cond_field)
  131. return +1;
  132. if (r->bit[bit_nr]->field->conditions->type ==
  133. insn_field_cond_field)
  134. return -1;
  135. /* The case of both fields having constant values should have
  136. already have been handled because such fields are converted
  137. into normal constant fields, but we must not make this
  138. an assert, as we wouldn't gracefully handle an (invalid)
  139. duplicate insn description. */
  140. continue;
  141. }
  142. if (l->bit[bit_nr]->field->conditions->test == insn_field_cond_eq)
  143. return +1; /* left = only */
  144. if (r->bit[bit_nr]->field->conditions->test == insn_field_cond_eq)
  145. return -1; /* right = only */
  146. /* FIXME: Need to some what arbitrarily order conditional lists */
  147. continue;
  148. }
  149. l = l->next;
  150. r = r->next;
  151. }
  152. }
  153. /* same as strcmp */
  154. static int
  155. insn_word_cmp (insn_word_entry *l, insn_word_entry *r)
  156. {
  157. while (1)
  158. {
  159. int bit_nr;
  160. if (l == NULL && r == NULL)
  161. return 0; /* all previous fields the same */
  162. if (l == NULL)
  163. return -1; /* left shorter than right */
  164. if (r == NULL)
  165. return +1; /* left longer than right */
  166. for (bit_nr = 0; bit_nr < options.insn_bit_size; bit_nr++)
  167. {
  168. if (l->bit[bit_nr]->mask < r->bit[bit_nr]->mask)
  169. return -1;
  170. if (l->bit[bit_nr]->mask > r->bit[bit_nr]->mask)
  171. return 1;
  172. if (l->bit[bit_nr]->value < r->bit[bit_nr]->value)
  173. return -1;
  174. if (l->bit[bit_nr]->value > r->bit[bit_nr]->value)
  175. return 1;
  176. }
  177. l = l->next;
  178. r = r->next;
  179. }
  180. }
  181. /* same as strcmp */
  182. static int
  183. opcode_bit_cmp (opcode_bits *l, opcode_bits *r)
  184. {
  185. if (l == NULL && r == NULL)
  186. return 0; /* all previous bits the same */
  187. if (l == NULL)
  188. return -1; /* left shorter than right */
  189. if (r == NULL)
  190. return +1; /* left longer than right */
  191. /* most significant word */
  192. if (l->field->word_nr < r->field->word_nr)
  193. return +1; /* left has more significant word */
  194. if (l->field->word_nr > r->field->word_nr)
  195. return -1; /* right has more significant word */
  196. /* most significant bit? */
  197. if (l->first < r->first)
  198. return +1; /* left as more significant bit */
  199. if (l->first > r->first)
  200. return -1; /* right as more significant bit */
  201. /* nr bits? */
  202. if (l->last < r->last)
  203. return +1; /* left as less bits */
  204. if (l->last > r->last)
  205. return -1; /* right as less bits */
  206. /* value? */
  207. if (l->value < r->value)
  208. return -1;
  209. if (l->value > r->value)
  210. return 1;
  211. return 0;
  212. }
  213. /* same as strcmp */
  214. static int
  215. opcode_bits_cmp (opcode_bits *l, opcode_bits *r)
  216. {
  217. while (1)
  218. {
  219. int cmp;
  220. if (l == NULL && r == NULL)
  221. return 0; /* all previous bits the same */
  222. cmp = opcode_bit_cmp (l, r);
  223. if (cmp != 0)
  224. return cmp;
  225. l = l->next;
  226. r = r->next;
  227. }
  228. }
  229. /* same as strcmp */
  230. static opcode_bits *
  231. new_opcode_bits (opcode_bits *old_bits,
  232. int value,
  233. int first,
  234. int last, insn_field_entry *field, opcode_field *opcode)
  235. {
  236. opcode_bits *new_bits = ZALLOC (opcode_bits);
  237. new_bits->field = field;
  238. new_bits->value = value;
  239. new_bits->first = first;
  240. new_bits->last = last;
  241. new_bits->opcode = opcode;
  242. if (old_bits != NULL)
  243. {
  244. opcode_bits *new_list;
  245. opcode_bits **last = &new_list;
  246. new_list = new_opcode_bits (old_bits->next,
  247. old_bits->value,
  248. old_bits->first,
  249. old_bits->last,
  250. old_bits->field, old_bits->opcode);
  251. while (*last != NULL)
  252. {
  253. int cmp = opcode_bit_cmp (new_bits, *last);
  254. if (cmp < 0) /* new < new_list */
  255. {
  256. break;
  257. }
  258. if (cmp == 0)
  259. {
  260. ERROR ("Duplicated insn bits in list");
  261. }
  262. last = &(*last)->next;
  263. }
  264. new_bits->next = *last;
  265. *last = new_bits;
  266. return new_list;
  267. }
  268. else
  269. {
  270. return new_bits;
  271. }
  272. }
  273. /* Same as strcmp(). */
  274. static int
  275. name_cmp (const char *l, const char *r)
  276. {
  277. if (l == NULL && r == NULL)
  278. return 0;
  279. if (l != NULL && r == NULL)
  280. return -1;
  281. if (l == NULL && r != NULL)
  282. return +1;
  283. return strcmp (l, r);
  284. }
  285. typedef enum
  286. {
  287. merge_duplicate_insns,
  288. report_duplicate_insns,
  289. }
  290. duplicate_insn_actions;
  291. static insn_list *
  292. insn_list_insert (insn_list **cur_insn_ptr,
  293. int *nr_insns,
  294. insn_entry * insn,
  295. opcode_bits *expanded_bits,
  296. opcode_field *opcodes,
  297. int nr_prefetched_words,
  298. duplicate_insn_actions duplicate_action)
  299. {
  300. /* insert it according to the order of the fields & bits */
  301. for (; (*cur_insn_ptr) != NULL; cur_insn_ptr = &(*cur_insn_ptr)->next)
  302. {
  303. int cmp;
  304. /* key#1 sort according to the constant fields of each instruction */
  305. cmp = insn_word_cmp (insn->words, (*cur_insn_ptr)->insn->words);
  306. if (cmp < 0)
  307. break;
  308. else if (cmp > 0)
  309. continue;
  310. /* key#2 sort according to the expanded bits of each instruction */
  311. cmp = opcode_bits_cmp (expanded_bits, (*cur_insn_ptr)->expanded_bits);
  312. if (cmp < 0)
  313. break;
  314. else if (cmp > 0)
  315. continue;
  316. /* key#3 sort according to the non-constant fields of each instruction */
  317. cmp = insn_field_cmp (insn->words, (*cur_insn_ptr)->insn->words);
  318. if (cmp < 0)
  319. break;
  320. else if (cmp > 0)
  321. continue;
  322. if (duplicate_action == merge_duplicate_insns)
  323. {
  324. /* key#4: If we're going to merge duplicates, also sort
  325. according to the format_name. Two instructions with
  326. identical decode patterns, but different names, are
  327. considered different when merging. Duplicates are only
  328. important when creating a decode table (implied by
  329. report_duplicate_insns) as such a table only has the
  330. instruction's bit code as a way of differentiating
  331. between instructions. */
  332. int cmp = name_cmp (insn->format_name,
  333. (*cur_insn_ptr)->insn->format_name);
  334. if (cmp < 0)
  335. break;
  336. else if (cmp > 0)
  337. continue;
  338. }
  339. if (duplicate_action == merge_duplicate_insns)
  340. {
  341. /* key#5: If we're going to merge duplicates, also sort
  342. according to the name. See comment above for
  343. format_name. */
  344. int cmp = name_cmp (insn->name, (*cur_insn_ptr)->insn->name);
  345. if (cmp < 0)
  346. break;
  347. else if (cmp > 0)
  348. continue;
  349. }
  350. /* duplicate keys, report problem */
  351. switch (duplicate_action)
  352. {
  353. case report_duplicate_insns:
  354. /* It would appear that we have two instructions with the
  355. same constant field values across all words and bits.
  356. This error can also occure when insn_field_cmp() is
  357. failing to differentiate between two instructions that
  358. differ only in their conditional fields. */
  359. warning (insn->line,
  360. "Two instructions with identical constant fields\n");
  361. error ((*cur_insn_ptr)->insn->line,
  362. "Location of duplicate instruction\n");
  363. case merge_duplicate_insns:
  364. /* Add the opcode path to the instructions list */
  365. if (options.trace.insn_insertion)
  366. {
  367. notify ((*cur_insn_ptr)->insn->line,
  368. "%s.%s: insert merge %s.%s\n",
  369. (*cur_insn_ptr)->insn->format_name,
  370. (*cur_insn_ptr)->insn->name,
  371. insn->format_name,
  372. insn->name);
  373. }
  374. if (opcodes != NULL)
  375. {
  376. insn_opcodes **last = &(*cur_insn_ptr)->opcodes;
  377. while (*last != NULL)
  378. {
  379. last = &(*last)->next;
  380. }
  381. (*last) = ZALLOC (insn_opcodes);
  382. (*last)->opcode = opcodes;
  383. }
  384. /* Use the larger nr_prefetched_words */
  385. if ((*cur_insn_ptr)->nr_prefetched_words < nr_prefetched_words)
  386. (*cur_insn_ptr)->nr_prefetched_words = nr_prefetched_words;
  387. return (*cur_insn_ptr);
  388. }
  389. }
  390. /* create a new list entry and insert it */
  391. {
  392. insn_list *new_insn = ZALLOC (insn_list);
  393. if (options.trace.insn_insertion)
  394. {
  395. notify (insn->line,
  396. "%s.%s: insert new\n",
  397. insn->format_name,
  398. insn->name);
  399. }
  400. new_insn->insn = insn;
  401. new_insn->expanded_bits = expanded_bits;
  402. new_insn->next = (*cur_insn_ptr);
  403. new_insn->nr_prefetched_words = nr_prefetched_words;
  404. if (opcodes != NULL)
  405. {
  406. new_insn->opcodes = ZALLOC (insn_opcodes);
  407. new_insn->opcodes->opcode = opcodes;
  408. }
  409. (*cur_insn_ptr) = new_insn;
  410. }
  411. *nr_insns += 1;
  412. return (*cur_insn_ptr);
  413. }
  414. extern void
  415. gen_entry_traverse_tree (lf *file,
  416. gen_entry *table,
  417. int depth,
  418. gen_entry_handler * start,
  419. gen_entry_handler * leaf,
  420. gen_entry_handler * end, void *data)
  421. {
  422. gen_entry *entry;
  423. ASSERT (table !=NULL);
  424. ASSERT (table->opcode != NULL);
  425. ASSERT (table->nr_entries > 0);
  426. ASSERT (table->entries != 0);
  427. /* prefix */
  428. if (start != NULL && depth >= 0)
  429. {
  430. start (file, table, depth, data);
  431. }
  432. /* infix leaves */
  433. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  434. {
  435. if (entry->entries != NULL && depth != 0)
  436. {
  437. gen_entry_traverse_tree (file, entry, depth + 1,
  438. start, leaf, end, data);
  439. }
  440. else if (depth >= 0)
  441. {
  442. if (leaf != NULL)
  443. {
  444. leaf (file, entry, depth, data);
  445. }
  446. }
  447. }
  448. /* postfix */
  449. if (end != NULL && depth >= 0)
  450. {
  451. end (file, table, depth, data);
  452. }
  453. }
  454. /* create a list element containing a single gen_table entry */
  455. static gen_list *
  456. make_table (insn_table *isa, decode_table *rules, model_entry *model)
  457. {
  458. insn_entry *insn;
  459. gen_list *entry = ZALLOC (gen_list);
  460. entry->table = ZALLOC (gen_entry);
  461. entry->table->top = entry;
  462. entry->model = model;
  463. entry->isa = isa;
  464. for (insn = isa->insns; insn != NULL; insn = insn->next)
  465. {
  466. if (model == NULL
  467. || insn->processors == NULL
  468. || filter_is_member (insn->processors, model->name))
  469. {
  470. insn_list_insert (&entry->table->insns, &entry->table->nr_insns, insn, NULL, /* expanded_bits - none yet */
  471. NULL, /* opcodes - none yet */
  472. 0, /* nr_prefetched_words - none yet */
  473. report_duplicate_insns);
  474. }
  475. }
  476. entry->table->opcode_rule = rules;
  477. return entry;
  478. }
  479. gen_table *
  480. make_gen_tables (insn_table *isa, decode_table *rules)
  481. {
  482. gen_table *gen = ZALLOC (gen_table);
  483. gen->isa = isa;
  484. gen->rules = rules;
  485. if (options.gen.multi_sim)
  486. {
  487. gen_list **last = &gen->tables;
  488. model_entry *model;
  489. filter *processors;
  490. if (options.model_filter != NULL)
  491. processors = options.model_filter;
  492. else
  493. processors = isa->model->processors;
  494. for (model = isa->model->models; model != NULL; model = model->next)
  495. {
  496. if (filter_is_member (processors, model->name))
  497. {
  498. *last = make_table (isa, rules, model);
  499. last = &(*last)->next;
  500. }
  501. }
  502. }
  503. else
  504. {
  505. gen->tables = make_table (isa, rules, NULL);
  506. }
  507. return gen;
  508. }
  509. /****************************************************************/
  510. /* Is the bit, according to the decode rule, identical across all the
  511. instructions? */
  512. static int
  513. insns_bit_useless (insn_list *insns, decode_table *rule, int bit_nr)
  514. {
  515. insn_list *entry;
  516. int value = -1;
  517. int is_useless = 1; /* cleared if something actually found */
  518. /* check the instructions for some constant value in at least one of
  519. the bit fields */
  520. for (entry = insns; entry != NULL; entry = entry->next)
  521. {
  522. insn_word_entry *word = entry->insn->word[rule->word_nr];
  523. insn_bit_entry *bit = word->bit[bit_nr];
  524. switch (bit->field->type)
  525. {
  526. case insn_field_invalid:
  527. ASSERT (0);
  528. break;
  529. case insn_field_wild:
  530. case insn_field_reserved:
  531. /* neither useless or useful - ignore */
  532. break;
  533. case insn_field_int:
  534. switch (rule->search)
  535. {
  536. case decode_find_strings:
  537. /* an integer isn't a string */
  538. return 1;
  539. case decode_find_constants:
  540. case decode_find_mixed:
  541. /* an integer is useful if its value isn't the same
  542. between all instructions. The first time through the
  543. value is saved, the second time through (if the
  544. values differ) it is marked as useful. */
  545. if (value < 0)
  546. value = bit->value;
  547. else if (value != bit->value)
  548. is_useless = 0;
  549. break;
  550. }
  551. break;
  552. case insn_field_string:
  553. switch (rule->search)
  554. {
  555. case decode_find_strings:
  556. /* at least one string, keep checking */
  557. is_useless = 0;
  558. break;
  559. case decode_find_constants:
  560. case decode_find_mixed:
  561. if (filter_is_member (rule->constant_field_names,
  562. bit->field->val_string))
  563. /* a string field forced to constant? */
  564. is_useless = 0;
  565. else if (bit->field->conditions != NULL
  566. && bit->field->conditions->test == insn_field_cond_eq
  567. && bit->field->conditions->type == insn_field_cond_value)
  568. {
  569. int shift = bit->field->last - bit_nr;
  570. int bitvalue = (bit->field->conditions->value >> shift) & 1;
  571. if (value < 0)
  572. value = bitvalue;
  573. else if (value != bitvalue)
  574. is_useless = 0;
  575. }
  576. else if (rule->search == decode_find_constants)
  577. /* the string field isn't constant */
  578. return 1;
  579. break;
  580. }
  581. }
  582. }
  583. /* Given only one constant value has been found, check through all
  584. the instructions to see if at least one conditional makes it
  585. usefull */
  586. if (value >= 0 && is_useless)
  587. {
  588. for (entry = insns; entry != NULL; entry = entry->next)
  589. {
  590. insn_word_entry *word = entry->insn->word[rule->word_nr];
  591. insn_bit_entry *bit = word->bit[bit_nr];
  592. switch (bit->field->type)
  593. {
  594. case insn_field_invalid:
  595. ASSERT (0);
  596. break;
  597. case insn_field_wild:
  598. case insn_field_reserved:
  599. case insn_field_int:
  600. /* already processed */
  601. break;
  602. case insn_field_string:
  603. switch (rule->search)
  604. {
  605. case decode_find_strings:
  606. case decode_find_constants:
  607. /* already processed */
  608. break;
  609. case decode_find_mixed:
  610. /* string field with conditions. If this condition
  611. eliminates the value then the compare is useful */
  612. if (bit->field->conditions != NULL)
  613. {
  614. insn_field_cond *condition;
  615. int shift = bit->field->last - bit_nr;
  616. for (condition = bit->field->conditions;
  617. condition != NULL; condition = condition->next)
  618. {
  619. switch (condition->type)
  620. {
  621. case insn_field_cond_value:
  622. switch (condition->test)
  623. {
  624. case insn_field_cond_ne:
  625. if (((condition->value >> shift) & 1)
  626. == (unsigned) value)
  627. /* conditional field excludes the
  628. current value */
  629. is_useless = 0;
  630. break;
  631. case insn_field_cond_eq:
  632. if (((condition->value >> shift) & 1)
  633. != (unsigned) value)
  634. /* conditional field requires the
  635. current value */
  636. is_useless = 0;
  637. break;
  638. }
  639. break;
  640. case insn_field_cond_field:
  641. /* are these handled separatly? */
  642. break;
  643. }
  644. }
  645. }
  646. }
  647. }
  648. }
  649. }
  650. return is_useless;
  651. }
  652. /* go through a gen-table's list of instruction formats looking for a
  653. range of bits that meet the decode table RULEs requirements */
  654. static opcode_field *
  655. gen_entry_find_opcode_field (insn_list *insns,
  656. decode_table *rule, int string_only)
  657. {
  658. opcode_field curr_opcode;
  659. ASSERT (rule != NULL);
  660. memset (&curr_opcode, 0, sizeof (curr_opcode));
  661. curr_opcode.word_nr = rule->word_nr;
  662. curr_opcode.first = rule->first;
  663. curr_opcode.last = rule->last;
  664. /* Try to reduce the size of first..last in accordance with the
  665. decode rules */
  666. while (curr_opcode.first <= rule->last)
  667. {
  668. if (insns_bit_useless (insns, rule, curr_opcode.first))
  669. curr_opcode.first++;
  670. else
  671. break;
  672. }
  673. while (curr_opcode.last >= rule->first)
  674. {
  675. if (insns_bit_useless (insns, rule, curr_opcode.last))
  676. curr_opcode.last--;
  677. else
  678. break;
  679. }
  680. /* did the final opcode field end up being empty? */
  681. if (curr_opcode.first > curr_opcode.last)
  682. {
  683. return NULL;
  684. }
  685. ASSERT (curr_opcode.last >= rule->first);
  686. ASSERT (curr_opcode.first <= rule->last);
  687. ASSERT (curr_opcode.first <= curr_opcode.last);
  688. /* Ensure that, for the non string only case, the opcode includes
  689. the range forced_first .. forced_last */
  690. if (!string_only && curr_opcode.first > rule->force_first)
  691. {
  692. curr_opcode.first = rule->force_first;
  693. }
  694. if (!string_only && curr_opcode.last < rule->force_last)
  695. {
  696. curr_opcode.last = rule->force_last;
  697. }
  698. /* For the string only case, force just the lower bound (so that the
  699. shift can be eliminated) */
  700. if (string_only && rule->force_last == options.insn_bit_size - 1)
  701. {
  702. curr_opcode.last = options.insn_bit_size - 1;
  703. }
  704. /* handle any special cases */
  705. switch (rule->type)
  706. {
  707. case normal_decode_rule:
  708. /* let the above apply */
  709. curr_opcode.nr_opcodes =
  710. (1 << (curr_opcode.last - curr_opcode.first + 1));
  711. break;
  712. case boolean_rule:
  713. curr_opcode.is_boolean = 1;
  714. curr_opcode.boolean_constant = rule->constant;
  715. curr_opcode.nr_opcodes = 2;
  716. break;
  717. }
  718. {
  719. opcode_field *new_field = ZALLOC (opcode_field);
  720. memcpy (new_field, &curr_opcode, sizeof (opcode_field));
  721. return new_field;
  722. }
  723. }
  724. static void
  725. gen_entry_insert_insn (gen_entry *table,
  726. insn_entry * old_insn,
  727. int new_word_nr,
  728. int new_nr_prefetched_words,
  729. int new_opcode_nr, opcode_bits *new_bits)
  730. {
  731. gen_entry **entry = &table->entries;
  732. /* find the new table for this entry */
  733. while ((*entry) != NULL && (*entry)->opcode_nr < new_opcode_nr)
  734. {
  735. entry = &(*entry)->sibling;
  736. }
  737. if ((*entry) == NULL || (*entry)->opcode_nr != new_opcode_nr)
  738. {
  739. /* insert the missing entry */
  740. gen_entry *new_entry = ZALLOC (gen_entry);
  741. new_entry->sibling = (*entry);
  742. (*entry) = new_entry;
  743. table->nr_entries++;
  744. /* fill it in */
  745. new_entry->top = table->top;
  746. new_entry->opcode_nr = new_opcode_nr;
  747. new_entry->word_nr = new_word_nr;
  748. new_entry->expanded_bits = new_bits;
  749. new_entry->opcode_rule = table->opcode_rule->next;
  750. new_entry->parent = table;
  751. new_entry->nr_prefetched_words = new_nr_prefetched_words;
  752. }
  753. /* ASSERT new_bits == cur_entry bits */
  754. ASSERT ((*entry) != NULL && (*entry)->opcode_nr == new_opcode_nr);
  755. insn_list_insert (&(*entry)->insns, &(*entry)->nr_insns, old_insn, NULL, /* expanded_bits - only in final list */
  756. NULL, /* opcodes - only in final list */
  757. new_nr_prefetched_words, /* for this table */
  758. report_duplicate_insns);
  759. }
  760. static void
  761. gen_entry_expand_opcode (gen_entry *table,
  762. insn_entry * instruction,
  763. int bit_nr, int opcode_nr, opcode_bits *bits)
  764. {
  765. if (bit_nr > table->opcode->last)
  766. {
  767. /* Only include the hardwired bit information with an entry IF
  768. that entry (and hence its functions) are being duplicated. */
  769. if (options.trace.insn_expansion)
  770. {
  771. print_gen_entry_path (table->opcode_rule->line, table, notify);
  772. notify (NULL, ": insert %d - %s.%s%s\n",
  773. opcode_nr,
  774. instruction->format_name,
  775. instruction->name,
  776. (table->opcode_rule->
  777. with_duplicates ? " (duplicated)" : ""));
  778. }
  779. if (table->opcode_rule->with_duplicates)
  780. {
  781. gen_entry_insert_insn (table, instruction,
  782. table->opcode->word_nr,
  783. table->nr_prefetched_words, opcode_nr, bits);
  784. }
  785. else
  786. {
  787. gen_entry_insert_insn (table, instruction,
  788. table->opcode->word_nr,
  789. table->nr_prefetched_words, opcode_nr, NULL);
  790. }
  791. }
  792. else
  793. {
  794. insn_word_entry *word = instruction->word[table->opcode->word_nr];
  795. insn_field_entry *field = word->bit[bit_nr]->field;
  796. int last_pos = ((field->last < table->opcode->last)
  797. ? field->last : table->opcode->last);
  798. int first_pos = ((field->first > table->opcode->first)
  799. ? field->first : table->opcode->first);
  800. int width = last_pos - first_pos + 1;
  801. switch (field->type)
  802. {
  803. case insn_field_int:
  804. {
  805. int val;
  806. val = sub_val (field->val_int, field->last, first_pos, last_pos);
  807. gen_entry_expand_opcode (table, instruction,
  808. last_pos + 1,
  809. ((opcode_nr << width) | val), bits);
  810. break;
  811. }
  812. default:
  813. {
  814. if (field->type == insn_field_reserved)
  815. gen_entry_expand_opcode (table, instruction,
  816. last_pos + 1,
  817. ((opcode_nr << width)), bits);
  818. else
  819. {
  820. int val;
  821. int last_val = (table->opcode->is_boolean ? 2 : (1 << width));
  822. for (val = 0; val < last_val; val++)
  823. {
  824. /* check to see if the value has been precluded
  825. (by a conditional) in some way */
  826. int is_precluded;
  827. insn_field_cond *condition;
  828. for (condition = field->conditions, is_precluded = 0;
  829. condition != NULL && !is_precluded;
  830. condition = condition->next)
  831. {
  832. switch (condition->type)
  833. {
  834. case insn_field_cond_value:
  835. {
  836. int value =
  837. sub_val (condition->value, field->last,
  838. first_pos, last_pos);
  839. switch (condition->test)
  840. {
  841. case insn_field_cond_ne:
  842. if (value == val)
  843. is_precluded = 1;
  844. break;
  845. case insn_field_cond_eq:
  846. if (value != val)
  847. is_precluded = 1;
  848. break;
  849. }
  850. break;
  851. }
  852. case insn_field_cond_field:
  853. {
  854. int value = -1;
  855. opcode_bits *bit;
  856. gen_entry *t = NULL;
  857. /* Try to find a value for the
  858. conditional by looking back through
  859. the previously defined bits for one
  860. that covers the designated
  861. conditional field */
  862. for (bit = bits; bit != NULL; bit = bit->next)
  863. {
  864. if (bit->field->word_nr ==
  865. condition->field->word_nr
  866. && bit->first <= condition->field->first
  867. && bit->last >= condition->field->last)
  868. {
  869. /* the bit field fully specified
  870. the conditional field's value */
  871. value = sub_val (bit->value, bit->last,
  872. condition->field->
  873. first,
  874. condition->field->
  875. last);
  876. }
  877. }
  878. /* Try to find a value by looking
  879. through this and previous tables */
  880. if (bit == NULL)
  881. {
  882. for (t = table;
  883. t->parent != NULL; t = t->parent)
  884. {
  885. if (t->parent->opcode->word_nr ==
  886. condition->field->word_nr
  887. && t->parent->opcode->first <=
  888. condition->field->first
  889. && t->parent->opcode->last >=
  890. condition->field->last)
  891. {
  892. /* the table entry fully
  893. specified the condition
  894. field's value */
  895. /* extract the field's value
  896. from the opcode */
  897. value =
  898. sub_val (t->opcode_nr,
  899. t->parent->opcode->last,
  900. condition->field->first,
  901. condition->field->last);
  902. /* this is a requirement of
  903. a conditonal field
  904. refering to another field */
  905. ASSERT ((condition->field->first -
  906. condition->field->last) ==
  907. (first_pos - last_pos));
  908. printf
  909. ("value=%d, opcode_nr=%d, last=%d, [%d..%d]\n",
  910. value, t->opcode_nr,
  911. t->parent->opcode->last,
  912. condition->field->first,
  913. condition->field->last);
  914. }
  915. }
  916. }
  917. if (bit == NULL && t == NULL)
  918. error (instruction->line,
  919. "Conditional `%s' of field `%s' isn't expanded",
  920. condition->string, field->val_string);
  921. switch (condition->test)
  922. {
  923. case insn_field_cond_ne:
  924. if (value == val)
  925. is_precluded = 1;
  926. break;
  927. case insn_field_cond_eq:
  928. if (value != val)
  929. is_precluded = 1;
  930. break;
  931. }
  932. break;
  933. }
  934. }
  935. }
  936. if (!is_precluded)
  937. {
  938. /* Only add additional hardwired bit
  939. information if the entry is not going to
  940. later be combined */
  941. if (table->opcode_rule->with_combine)
  942. {
  943. gen_entry_expand_opcode (table, instruction,
  944. last_pos + 1,
  945. ((opcode_nr << width) |
  946. val), bits);
  947. }
  948. else
  949. {
  950. opcode_bits *new_bits =
  951. new_opcode_bits (bits, val,
  952. first_pos, last_pos,
  953. field,
  954. table->opcode);
  955. gen_entry_expand_opcode (table, instruction,
  956. last_pos + 1,
  957. ((opcode_nr << width) |
  958. val), new_bits);
  959. }
  960. }
  961. }
  962. }
  963. }
  964. }
  965. }
  966. }
  967. static void
  968. gen_entry_insert_expanding (gen_entry *table, insn_entry * instruction)
  969. {
  970. gen_entry_expand_opcode (table,
  971. instruction,
  972. table->opcode->first, 0, table->expanded_bits);
  973. }
  974. static int
  975. insns_match_format_names (insn_list *insns, filter *format_names)
  976. {
  977. if (format_names != NULL)
  978. {
  979. insn_list *i;
  980. for (i = insns; i != NULL; i = i->next)
  981. {
  982. if (i->insn->format_name != NULL
  983. && !filter_is_member (format_names, i->insn->format_name))
  984. return 0;
  985. }
  986. }
  987. return 1;
  988. }
  989. static int
  990. table_matches_path (gen_entry *table, decode_path_list *paths)
  991. {
  992. if (paths == NULL)
  993. return 1;
  994. while (paths != NULL)
  995. {
  996. gen_entry *entry = table;
  997. decode_path *path = paths->path;
  998. while (1)
  999. {
  1000. if (entry == NULL && path == NULL)
  1001. return 1;
  1002. if (entry == NULL || path == NULL)
  1003. break;
  1004. if (entry->opcode_nr != path->opcode_nr)
  1005. break;
  1006. entry = entry->parent;
  1007. path = path->parent;
  1008. }
  1009. paths = paths->next;
  1010. }
  1011. return 0;
  1012. }
  1013. static int
  1014. insns_match_conditions (insn_list *insns, decode_cond *conditions)
  1015. {
  1016. if (conditions != NULL)
  1017. {
  1018. insn_list *i;
  1019. for (i = insns; i != NULL; i = i->next)
  1020. {
  1021. decode_cond *cond;
  1022. for (cond = conditions; cond != NULL; cond = cond->next)
  1023. {
  1024. int bit_nr;
  1025. if (i->insn->nr_words <= cond->word_nr)
  1026. return 0;
  1027. for (bit_nr = 0; bit_nr < options.insn_bit_size; bit_nr++)
  1028. {
  1029. if (!cond->mask[bit_nr])
  1030. continue;
  1031. if (!i->insn->word[cond->word_nr]->bit[bit_nr]->mask)
  1032. return 0;
  1033. if ((i->insn->word[cond->word_nr]->bit[bit_nr]->value
  1034. == cond->value[bit_nr]) == !cond->is_equal)
  1035. return 0;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. return 1;
  1041. }
  1042. static int
  1043. insns_match_nr_words (insn_list *insns, int nr_words)
  1044. {
  1045. insn_list *i;
  1046. for (i = insns; i != NULL; i = i->next)
  1047. {
  1048. if (i->insn->nr_words < nr_words)
  1049. return 0;
  1050. }
  1051. return 1;
  1052. }
  1053. static int
  1054. insn_list_cmp (insn_list *l, insn_list *r)
  1055. {
  1056. while (1)
  1057. {
  1058. insn_entry *insn;
  1059. if (l == NULL && r == NULL)
  1060. return 0;
  1061. if (l == NULL)
  1062. return -1;
  1063. if (r == NULL)
  1064. return 1;
  1065. if (l->insn != r->insn)
  1066. return -1; /* somewhat arbitrary at present */
  1067. /* skip this insn */
  1068. insn = l->insn;
  1069. while (l != NULL && l->insn == insn)
  1070. l = l->next;
  1071. while (r != NULL && r->insn == insn)
  1072. r = r->next;
  1073. }
  1074. }
  1075. static void
  1076. gen_entry_expand_insns (gen_entry *table)
  1077. {
  1078. decode_table *opcode_rule;
  1079. ASSERT (table->nr_insns >= 1);
  1080. /* determine a valid opcode */
  1081. for (opcode_rule = table->opcode_rule;
  1082. opcode_rule != NULL; opcode_rule = opcode_rule->next)
  1083. {
  1084. char *discard_reason;
  1085. if (table->top->model != NULL
  1086. && opcode_rule->model_names != NULL
  1087. && !filter_is_member (opcode_rule->model_names,
  1088. table->top->model->name))
  1089. {
  1090. /* the rule isn't applicable to this processor */
  1091. discard_reason = "wrong model";
  1092. }
  1093. else if (table->nr_insns == 1 && opcode_rule->conditions == NULL)
  1094. {
  1095. /* for safety, require a pre-codition when attempting to
  1096. apply a rule to a single instruction */
  1097. discard_reason = "need pre-condition when nr-insn == 1";
  1098. }
  1099. else if (table->nr_insns == 1 && !opcode_rule->with_duplicates)
  1100. {
  1101. /* Little point in expanding a single instruction when we're
  1102. not duplicating the semantic functions that this table
  1103. calls */
  1104. discard_reason = "need duplication with nr-insns == 1";
  1105. }
  1106. else
  1107. if (!insns_match_format_names
  1108. (table->insns, opcode_rule->format_names))
  1109. {
  1110. discard_reason = "wrong format name";
  1111. }
  1112. else if (!insns_match_nr_words (table->insns, opcode_rule->word_nr + 1))
  1113. {
  1114. discard_reason = "wrong nr words";
  1115. }
  1116. else if (!table_matches_path (table, opcode_rule->paths))
  1117. {
  1118. discard_reason = "path failed";
  1119. }
  1120. else
  1121. if (!insns_match_conditions (table->insns, opcode_rule->conditions))
  1122. {
  1123. discard_reason = "condition failed";
  1124. }
  1125. else
  1126. {
  1127. discard_reason = "no opcode field";
  1128. table->opcode = gen_entry_find_opcode_field (table->insns,
  1129. opcode_rule,
  1130. table->nr_insns == 1 /*string-only */
  1131. );
  1132. if (table->opcode != NULL)
  1133. {
  1134. table->opcode_rule = opcode_rule;
  1135. break;
  1136. }
  1137. }
  1138. if (options.trace.rule_rejection)
  1139. {
  1140. print_gen_entry_path (opcode_rule->line, table, notify);
  1141. notify (NULL, ": rule discarded - %s\n", discard_reason);
  1142. }
  1143. }
  1144. /* did we find anything */
  1145. if (opcode_rule == NULL)
  1146. {
  1147. /* the decode table failed, this set of instructions haven't
  1148. been uniquely identified */
  1149. if (table->nr_insns > 1)
  1150. {
  1151. print_gen_entry_insns (table, warning,
  1152. "was not uniquely decoded",
  1153. "decodes to the same entry");
  1154. error (NULL, "");
  1155. }
  1156. return;
  1157. }
  1158. /* Determine the number of words that must have been prefetched for
  1159. this table to function */
  1160. if (table->parent == NULL)
  1161. table->nr_prefetched_words = table->opcode_rule->word_nr + 1;
  1162. else if (table->opcode_rule->word_nr + 1 >
  1163. table->parent->nr_prefetched_words)
  1164. table->nr_prefetched_words = table->opcode_rule->word_nr + 1;
  1165. else
  1166. table->nr_prefetched_words = table->parent->nr_prefetched_words;
  1167. /* back link what we found to its parent */
  1168. if (table->parent != NULL)
  1169. {
  1170. ASSERT (table->parent->opcode != NULL);
  1171. table->opcode->parent = table->parent->opcode;
  1172. }
  1173. /* report the rule being used to expand the instructions */
  1174. if (options.trace.rule_selection)
  1175. {
  1176. print_gen_entry_path (table->opcode_rule->line, table, notify);
  1177. notify (NULL,
  1178. ": decode - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d\n",
  1179. table->opcode->word_nr,
  1180. i2target (options.hi_bit_nr, table->opcode->first),
  1181. i2target (options.hi_bit_nr, table->opcode->last),
  1182. i2target (options.hi_bit_nr, table->opcode_rule->first),
  1183. i2target (options.hi_bit_nr, table->opcode_rule->last),
  1184. table->opcode->nr_opcodes, table->nr_entries);
  1185. }
  1186. /* expand the raw instructions according to the opcode */
  1187. {
  1188. insn_list *entry;
  1189. for (entry = table->insns; entry != NULL; entry = entry->next)
  1190. {
  1191. if (options.trace.insn_expansion)
  1192. {
  1193. print_gen_entry_path (table->opcode_rule->line, table, notify);
  1194. notify (NULL, ": expand - %s.%s\n",
  1195. entry->insn->format_name, entry->insn->name);
  1196. }
  1197. gen_entry_insert_expanding (table, entry->insn);
  1198. }
  1199. }
  1200. /* dump the results */
  1201. if (options.trace.entries)
  1202. {
  1203. gen_entry *entry;
  1204. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  1205. {
  1206. insn_list *l;
  1207. print_gen_entry_path (table->opcode_rule->line, entry, notify);
  1208. notify (NULL, ": %d - entries %d -",
  1209. entry->opcode_nr, entry->nr_insns);
  1210. for (l = entry->insns; l != NULL; l = l->next)
  1211. notify (NULL, " %s.%s", l->insn->format_name, l->insn->name);
  1212. notify (NULL, "\n");
  1213. }
  1214. }
  1215. /* perform a combine pass if needed */
  1216. if (table->opcode_rule->with_combine)
  1217. {
  1218. gen_entry *entry;
  1219. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  1220. {
  1221. if (entry->combined_parent == NULL)
  1222. {
  1223. gen_entry **last = &entry->combined_next;
  1224. gen_entry *alt;
  1225. for (alt = entry->sibling; alt != NULL; alt = alt->sibling)
  1226. {
  1227. if (alt->combined_parent == NULL
  1228. && insn_list_cmp (entry->insns, alt->insns) == 0)
  1229. {
  1230. alt->combined_parent = entry;
  1231. *last = alt;
  1232. last = &alt->combined_next;
  1233. }
  1234. }
  1235. }
  1236. }
  1237. if (options.trace.combine)
  1238. {
  1239. int nr_unique = 0;
  1240. gen_entry *entry;
  1241. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  1242. {
  1243. if (entry->combined_parent == NULL)
  1244. {
  1245. insn_list *l;
  1246. gen_entry *duplicate;
  1247. nr_unique++;
  1248. print_gen_entry_path (table->opcode_rule->line, entry,
  1249. notify);
  1250. for (duplicate = entry->combined_next; duplicate != NULL;
  1251. duplicate = duplicate->combined_next)
  1252. {
  1253. notify (NULL, "+%d", duplicate->opcode_nr);
  1254. }
  1255. notify (NULL, ": entries %d -", entry->nr_insns);
  1256. for (l = entry->insns; l != NULL; l = l->next)
  1257. {
  1258. notify (NULL, " %s.%s",
  1259. l->insn->format_name, l->insn->name);
  1260. }
  1261. notify (NULL, "\n");
  1262. }
  1263. }
  1264. print_gen_entry_path (table->opcode_rule->line, table, notify);
  1265. notify (NULL,
  1266. ": combine - word %d, bits [%d..%d] in [%d..%d], opcodes %d, entries %d, unique %d\n",
  1267. table->opcode->word_nr, i2target (options.hi_bit_nr,
  1268. table->opcode->first),
  1269. i2target (options.hi_bit_nr, table->opcode->last),
  1270. i2target (options.hi_bit_nr, table->opcode_rule->first),
  1271. i2target (options.hi_bit_nr, table->opcode_rule->last),
  1272. table->opcode->nr_opcodes, table->nr_entries, nr_unique);
  1273. }
  1274. }
  1275. /* Check that the rule did more than re-arange the order of the
  1276. instructions */
  1277. {
  1278. gen_entry *entry;
  1279. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  1280. {
  1281. if (entry->combined_parent == NULL)
  1282. {
  1283. if (insn_list_cmp (table->insns, entry->insns) == 0)
  1284. {
  1285. print_gen_entry_path (table->opcode_rule->line, table,
  1286. warning);
  1287. warning (NULL,
  1288. ": Applying rule just copied all instructions\n");
  1289. print_gen_entry_insns (entry, warning, "Copied", NULL);
  1290. error (NULL, "");
  1291. }
  1292. }
  1293. }
  1294. }
  1295. /* if some form of expanded table, fill in the missing dots */
  1296. switch (table->opcode_rule->gen)
  1297. {
  1298. case padded_switch_gen:
  1299. case array_gen:
  1300. case goto_switch_gen:
  1301. if (!table->opcode->is_boolean)
  1302. {
  1303. gen_entry **entry = &table->entries;
  1304. gen_entry *illegals = NULL;
  1305. gen_entry **last_illegal = &illegals;
  1306. int opcode_nr = 0;
  1307. while (opcode_nr < table->opcode->nr_opcodes)
  1308. {
  1309. if ((*entry) == NULL || (*entry)->opcode_nr != opcode_nr)
  1310. {
  1311. /* missing - insert it under our feet at *entry */
  1312. gen_entry_insert_insn (table, table->top->isa->illegal_insn, table->opcode->word_nr, 0, /* nr_prefetched_words == 0 for invalid */
  1313. opcode_nr, NULL);
  1314. ASSERT ((*entry) != NULL);
  1315. ASSERT ((*entry)->opcode_nr == opcode_nr);
  1316. (*last_illegal) = *entry;
  1317. (*last_illegal)->combined_parent = illegals;
  1318. last_illegal = &(*last_illegal)->combined_next;
  1319. }
  1320. entry = &(*entry)->sibling;
  1321. opcode_nr++;
  1322. }
  1323. /* oops, will have pointed the first illegal insn back to
  1324. its self. Fix this */
  1325. if (illegals != NULL)
  1326. illegals->combined_parent = NULL;
  1327. }
  1328. break;
  1329. case switch_gen:
  1330. case invalid_gen:
  1331. /* ignore */
  1332. break;
  1333. }
  1334. /* and do the same for the newly created sub entries but *only*
  1335. expand entries that haven't been combined. */
  1336. {
  1337. gen_entry *entry;
  1338. for (entry = table->entries; entry != NULL; entry = entry->sibling)
  1339. {
  1340. if (entry->combined_parent == NULL)
  1341. {
  1342. gen_entry_expand_insns (entry);
  1343. }
  1344. }
  1345. }
  1346. }
  1347. void
  1348. gen_tables_expand_insns (gen_table *gen)
  1349. {
  1350. gen_list *entry;
  1351. for (entry = gen->tables; entry != NULL; entry = entry->next)
  1352. {
  1353. gen_entry_expand_insns (entry->table);
  1354. }
  1355. }
  1356. /* create a list of all the semantic functions that need to be
  1357. generated. Eliminate any duplicates. Verify that the decode stage
  1358. worked. */
  1359. static void
  1360. make_gen_semantics_list (lf *file, gen_entry *entry, int depth, void *data)
  1361. {
  1362. gen_table *gen = (gen_table *) data;
  1363. insn_list *insn;
  1364. /* Not interested in an entrie that have been combined into some
  1365. other entry at the same level */
  1366. if (entry->combined_parent != NULL)
  1367. return;
  1368. /* a leaf should contain exactly one instruction. If not the decode
  1369. stage failed. */
  1370. ASSERT (entry->nr_insns == 1);
  1371. /* Enter this instruction into the list of semantic functions. */
  1372. insn = insn_list_insert (&gen->semantics, &gen->nr_semantics,
  1373. entry->insns->insn,
  1374. entry->expanded_bits,
  1375. entry->parent->opcode,
  1376. entry->insns->nr_prefetched_words,
  1377. merge_duplicate_insns);
  1378. /* point the table entry at the real semantic function */
  1379. ASSERT (insn != NULL);
  1380. entry->insns->semantic = insn;
  1381. }
  1382. void
  1383. gen_tables_expand_semantics (gen_table *gen)
  1384. {
  1385. gen_list *entry;
  1386. for (entry = gen->tables; entry != NULL; entry = entry->next)
  1387. {
  1388. gen_entry_traverse_tree (NULL, entry->table, 1, /* depth */
  1389. NULL, /* start-handler */
  1390. make_gen_semantics_list, /* leaf-handler */
  1391. NULL, /* end-handler */
  1392. gen); /* data */
  1393. }
  1394. }
  1395. #ifdef MAIN
  1396. static void
  1397. dump_opcode_field (lf *file,
  1398. char *prefix,
  1399. opcode_field *field, char *suffix, int levels)
  1400. {
  1401. lf_printf (file, "%s(opcode_field *) 0x%lx", prefix, (long) field);
  1402. if (levels && field != NULL)
  1403. {
  1404. lf_indent (file, +1);
  1405. lf_printf (file, "\n(first %d)", field->first);
  1406. lf_printf (file, "\n(last %d)", field->last);
  1407. lf_printf (file, "\n(nr_opcodes %d)", field->nr_opcodes);
  1408. lf_printf (file, "\n(is_boolean %d)", field->is_boolean);
  1409. lf_printf (file, "\n(boolean_constant %d)", field->boolean_constant);
  1410. dump_opcode_field (file, "\n(parent ", field->parent, ")", levels - 1);
  1411. lf_indent (file, -1);
  1412. }
  1413. lf_printf (file, "%s", suffix);
  1414. }
  1415. static void
  1416. dump_opcode_bits (lf *file,
  1417. char *prefix, opcode_bits *bits, char *suffix, int levels)
  1418. {
  1419. lf_printf (file, "%s(opcode_bits *) 0x%lx", prefix, (long) bits);
  1420. if (levels && bits != NULL)
  1421. {
  1422. lf_indent (file, +1);
  1423. lf_printf (file, "\n(value %d)", bits->value);
  1424. dump_opcode_field (file, "\n(opcode ", bits->opcode, ")", 0);
  1425. dump_insn_field (file, "\n(field ", bits->field, ")");
  1426. dump_opcode_bits (file, "\n(next ", bits->next, ")", levels - 1);
  1427. lf_indent (file, -1);
  1428. }
  1429. lf_printf (file, "%s", suffix);
  1430. }
  1431. static void
  1432. dump_insn_list (lf *file, char *prefix, insn_list *entry, char *suffix)
  1433. {
  1434. lf_printf (file, "%s(insn_list *) 0x%lx", prefix, (long) entry);
  1435. if (entry != NULL)
  1436. {
  1437. lf_indent (file, +1);
  1438. dump_insn_entry (file, "\n(insn ", entry->insn, ")");
  1439. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1440. lf_indent (file, -1);
  1441. }
  1442. lf_printf (file, "%s", suffix);
  1443. }
  1444. static void
  1445. dump_insn_word_entry_list_entries (lf *file,
  1446. char *prefix,
  1447. insn_list *entry, char *suffix)
  1448. {
  1449. lf_printf (file, "%s", prefix);
  1450. while (entry != NULL)
  1451. {
  1452. dump_insn_list (file, "\n(", entry, ")");
  1453. entry = entry->next;
  1454. }
  1455. lf_printf (file, "%s", suffix);
  1456. }
  1457. static void
  1458. dump_gen_entry (lf *file,
  1459. char *prefix, gen_entry *table, char *suffix, int levels)
  1460. {
  1461. lf_printf (file, "%s(gen_entry *) 0x%lx", prefix, (long) table);
  1462. if (levels && table !=NULL)
  1463. {
  1464. lf_indent (file, +1);
  1465. lf_printf (file, "\n(opcode_nr %d)", table->opcode_nr);
  1466. lf_printf (file, "\n(word_nr %d)", table->word_nr);
  1467. dump_opcode_bits (file, "\n(expanded_bits ", table->expanded_bits, ")",
  1468. -1);
  1469. lf_printf (file, "\n(nr_insns %d)", table->nr_insns);
  1470. dump_insn_word_entry_list_entries (file, "\n(insns ", table->insns,
  1471. ")");
  1472. dump_decode_rule (file, "\n(opcode_rule ", table->opcode_rule, ")");
  1473. dump_opcode_field (file, "\n(opcode ", table->opcode, ")", 0);
  1474. lf_printf (file, "\n(nr_entries %d)", table->nr_entries);
  1475. dump_gen_entry (file, "\n(entries ", table->entries, ")",
  1476. table->nr_entries);
  1477. dump_gen_entry (file, "\n(sibling ", table->sibling, ")", levels - 1);
  1478. dump_gen_entry (file, "\n(parent ", table->parent, ")", 0);
  1479. lf_indent (file, -1);
  1480. }
  1481. lf_printf (file, "%s", suffix);
  1482. }
  1483. static void
  1484. dump_gen_list (lf *file,
  1485. char *prefix, gen_list *entry, char *suffix, int levels)
  1486. {
  1487. while (entry != NULL)
  1488. {
  1489. lf_printf (file, "%s(gen_list *) 0x%lx", prefix, (long) entry);
  1490. dump_gen_entry (file, "\n(", entry->table, ")", levels);
  1491. lf_printf (file, "\n(next (gen_list *) 0x%lx)", (long) entry->next);
  1492. lf_printf (file, "%s", suffix);
  1493. }
  1494. }
  1495. static void
  1496. dump_gen_table (lf *file,
  1497. char *prefix, gen_table *gen, char *suffix, int levels)
  1498. {
  1499. lf_printf (file, "%s(gen_table *) 0x%lx", prefix, (long) gen);
  1500. lf_printf (file, "\n(isa (insn_table *) 0x%lx)", (long) gen->isa);
  1501. lf_printf (file, "\n(rules (decode_table *) 0x%lx)", (long) gen->rules);
  1502. dump_gen_list (file, "\n(", gen->tables, ")", levels);
  1503. lf_printf (file, "%s", suffix);
  1504. }
  1505. igen_options options;
  1506. int
  1507. main (int argc, char **argv)
  1508. {
  1509. decode_table *decode_rules;
  1510. insn_table *instructions;
  1511. gen_table *gen;
  1512. lf *l;
  1513. if (argc != 7)
  1514. error (NULL,
  1515. "Usage: insn <filter-in> <hi-bit-nr> <insn-bit-size> <widths> <decode-table> <insn-table>\n");
  1516. INIT_OPTIONS ();
  1517. filter_parse (&options.flags_filter, argv[1]);
  1518. options.hi_bit_nr = a2i (argv[2]);
  1519. options.insn_bit_size = a2i (argv[3]);
  1520. options.insn_specifying_widths = a2i (argv[4]);
  1521. ASSERT (options.hi_bit_nr < options.insn_bit_size);
  1522. instructions = load_insn_table (argv[6], NULL);
  1523. decode_rules = load_decode_table (argv[5]);
  1524. gen = make_gen_tables (instructions, decode_rules);
  1525. gen_tables_expand_insns (gen);
  1526. l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-ld-insn");
  1527. dump_gen_table (l, "(", gen, ")\n", -1);
  1528. return 0;
  1529. }
  1530. #endif