ld-insn.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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. static insn_word_entry *
  22. parse_insn_word (line_ref *line, char *string, int word_nr)
  23. {
  24. char *chp;
  25. insn_word_entry *word = ZALLOC (insn_word_entry);
  26. /* create a leading sentinal */
  27. word->first = ZALLOC (insn_field_entry);
  28. word->first->first = -1;
  29. word->first->last = -1;
  30. word->first->width = 0;
  31. /* and a trailing sentinal */
  32. word->last = ZALLOC (insn_field_entry);
  33. word->last->first = options.insn_bit_size;
  34. word->last->last = options.insn_bit_size;
  35. word->last->width = 0;
  36. /* link them together */
  37. word->first->next = word->last;
  38. word->last->prev = word->first;
  39. /* now work through the formats */
  40. chp = skip_spaces (string);
  41. while (*chp != '\0')
  42. {
  43. char *start_pos;
  44. int strlen_pos;
  45. char *start_val;
  46. int strlen_val;
  47. insn_field_entry *new_field;
  48. /* create / link in the new field */
  49. new_field = ZALLOC (insn_field_entry);
  50. new_field->next = word->last;
  51. new_field->prev = word->last->prev;
  52. new_field->next->prev = new_field;
  53. new_field->prev->next = new_field;
  54. new_field->word_nr = word_nr;
  55. /* break out the first field (if present) */
  56. start_pos = chp;
  57. chp = skip_to_separator (chp, ".,!");
  58. strlen_pos = back_spaces (start_pos, chp) - start_pos;
  59. /* break out the second field (if present) */
  60. if (*chp != '.')
  61. {
  62. /* assume what was specified was the value (and not the start
  63. position). Assume the value length implicitly specifies
  64. the number of bits */
  65. start_val = start_pos;
  66. strlen_val = strlen_pos;
  67. start_pos = "";
  68. strlen_pos = 0;
  69. }
  70. else
  71. {
  72. chp++; /* skip `.' */
  73. chp = skip_spaces (chp);
  74. start_val = chp;
  75. if (*chp == '/' || *chp == '*')
  76. {
  77. do
  78. {
  79. chp++;
  80. }
  81. while (*chp == '/' || *chp == '*');
  82. }
  83. else if (isalpha (*start_val))
  84. {
  85. do
  86. {
  87. chp++;
  88. }
  89. while (isalnum (*chp) || *chp == '_');
  90. }
  91. else if (isdigit (*start_val))
  92. {
  93. do
  94. {
  95. chp++;
  96. }
  97. while (isalnum (*chp));
  98. }
  99. strlen_val = chp - start_val;
  100. chp = skip_spaces (chp);
  101. }
  102. if (strlen_val == 0)
  103. error (line, "Empty value field\n");
  104. /* break out any conditional fields - { [ "!" | "=" [ <value> | <field-name> } */
  105. while (*chp == '!' || *chp == '=')
  106. {
  107. char *start;
  108. char *end;
  109. int len;
  110. insn_field_cond *new_cond = ZALLOC (insn_field_cond);
  111. /* determine the conditional test */
  112. switch (*chp)
  113. {
  114. case '=':
  115. new_cond->test = insn_field_cond_eq;
  116. break;
  117. case '!':
  118. new_cond->test = insn_field_cond_ne;
  119. break;
  120. default:
  121. ASSERT (0);
  122. }
  123. /* save the value */
  124. chp++;
  125. chp = skip_spaces (chp);
  126. start = chp;
  127. chp = skip_to_separator (chp, "+,:!=");
  128. end = back_spaces (start, chp);
  129. len = end - start;
  130. if (len == 0)
  131. error (line, "Missing or invalid conditional value\n");
  132. new_cond->string = NZALLOC (char, len + 1);
  133. strncpy (new_cond->string, start, len);
  134. /* determine the conditional type */
  135. if (isdigit (*start))
  136. {
  137. /* [ "!" | "=" ] <value> */
  138. new_cond->type = insn_field_cond_value;
  139. new_cond->value = a2i (new_cond->string);
  140. }
  141. else
  142. {
  143. /* [ "!" | "=" ] <field> - check field valid */
  144. new_cond->type = insn_field_cond_field;
  145. /* new_cond->field is determined in later */
  146. }
  147. /* Only a single `=' is permitted. */
  148. if ((new_cond->test == insn_field_cond_eq
  149. && new_field->conditions != NULL)
  150. || (new_field->conditions != NULL
  151. && new_field->conditions->test == insn_field_cond_eq))
  152. error (line, "Only single conditional when `=' allowed\n");
  153. /* insert it */
  154. {
  155. insn_field_cond **last = &new_field->conditions;
  156. while (*last != NULL)
  157. last = &(*last)->next;
  158. *last = new_cond;
  159. }
  160. }
  161. /* NOW verify that the field was finished */
  162. if (*chp == ',')
  163. {
  164. chp = skip_spaces (chp + 1);
  165. if (*chp == '\0')
  166. error (line, "empty field\n");
  167. }
  168. else if (*chp != '\0')
  169. {
  170. error (line, "Missing field separator\n");
  171. }
  172. /* copy the value */
  173. new_field->val_string = NZALLOC (char, strlen_val + 1);
  174. strncpy (new_field->val_string, start_val, strlen_val);
  175. if (isdigit (new_field->val_string[0]))
  176. {
  177. if (strlen_pos == 0)
  178. {
  179. /* when the length/pos field is omited, an integer field
  180. is always binary */
  181. uint64_t val = 0;
  182. int i;
  183. for (i = 0; i < strlen_val; i++)
  184. {
  185. if (new_field->val_string[i] != '0'
  186. && new_field->val_string[i] != '1')
  187. error (line, "invalid binary field %s\n",
  188. new_field->val_string);
  189. val = (val << 1) + (new_field->val_string[i] == '1');
  190. }
  191. new_field->val_int = val;
  192. new_field->type = insn_field_int;
  193. }
  194. else
  195. {
  196. new_field->val_int = a2i (new_field->val_string);
  197. new_field->type = insn_field_int;
  198. }
  199. }
  200. else if (new_field->val_string[0] == '/')
  201. {
  202. new_field->type = insn_field_reserved;
  203. }
  204. else if (new_field->val_string[0] == '*')
  205. {
  206. new_field->type = insn_field_wild;
  207. }
  208. else
  209. {
  210. new_field->type = insn_field_string;
  211. if (filter_is_member (word->field_names, new_field->val_string))
  212. error (line, "Field name %s is duplicated\n",
  213. new_field->val_string);
  214. filter_parse (&word->field_names, new_field->val_string);
  215. }
  216. if (new_field->type != insn_field_string
  217. && new_field->conditions != NULL)
  218. error (line, "Conditionals can only be applied to named fields\n");
  219. /* the copy the position */
  220. new_field->pos_string = NZALLOC (char, strlen_pos + 1);
  221. strncpy (new_field->pos_string, start_pos, strlen_pos);
  222. if (strlen_pos == 0)
  223. {
  224. new_field->first = new_field->prev->last + 1;
  225. if (new_field->first == 0 /* first field */
  226. && *chp == '\0' /* no further fields */
  227. && new_field->type == insn_field_string)
  228. {
  229. /* A single string without any position, assume that it
  230. represents the entire instruction word */
  231. new_field->width = options.insn_bit_size;
  232. }
  233. else
  234. {
  235. /* No explicit width/position, assume value implicitly
  236. supplies the width */
  237. new_field->width = strlen_val;
  238. }
  239. new_field->last = new_field->first + new_field->width - 1;
  240. if (new_field->last >= options.insn_bit_size)
  241. error (line, "Bit position %d exceed instruction bit size (%d)\n",
  242. new_field->last, options.insn_bit_size);
  243. }
  244. else if (options.insn_specifying_widths)
  245. {
  246. new_field->first = new_field->prev->last + 1;
  247. new_field->width = a2i (new_field->pos_string);
  248. new_field->last = new_field->first + new_field->width - 1;
  249. if (new_field->last >= options.insn_bit_size)
  250. error (line, "Bit position %d exceed instruction bit size (%d)\n",
  251. new_field->last, options.insn_bit_size);
  252. }
  253. else
  254. {
  255. new_field->first = target_a2i (options.hi_bit_nr,
  256. new_field->pos_string);
  257. new_field->last = new_field->next->first - 1; /* guess */
  258. new_field->width = new_field->last - new_field->first + 1; /* guess */
  259. new_field->prev->last = new_field->first - 1; /*fix */
  260. new_field->prev->width = new_field->first - new_field->prev->first; /*fix */
  261. }
  262. }
  263. /* fiddle first/last so that the sentinals disapear */
  264. ASSERT (word->first->last < 0);
  265. ASSERT (word->last->first >= options.insn_bit_size);
  266. word->first = word->first->next;
  267. word->last = word->last->prev;
  268. /* check that the last field goes all the way to the last bit */
  269. if (word->last->last != options.insn_bit_size - 1)
  270. {
  271. if (options.warn.width)
  272. options.warning (line, "Instruction format is not %d bits wide\n",
  273. options.insn_bit_size);
  274. word->last->last = options.insn_bit_size - 1;
  275. }
  276. /* now go over this again, pointing each bit position at a field
  277. record */
  278. {
  279. insn_field_entry *field;
  280. for (field = word->first;
  281. field->last < options.insn_bit_size; field = field->next)
  282. {
  283. int i;
  284. for (i = field->first; i <= field->last; i++)
  285. {
  286. word->bit[i] = ZALLOC (insn_bit_entry);
  287. word->bit[i]->field = field;
  288. switch (field->type)
  289. {
  290. case insn_field_invalid:
  291. ASSERT (0);
  292. break;
  293. case insn_field_int:
  294. word->bit[i]->mask = 1;
  295. word->bit[i]->value = ((field->val_int
  296. & ((insn_uint) 1 <<
  297. (field->last - i))) != 0);
  298. case insn_field_reserved:
  299. case insn_field_wild:
  300. case insn_field_string:
  301. /* if we encounter a constant conditional, encode
  302. their bit value. */
  303. if (field->conditions != NULL
  304. && field->conditions->test == insn_field_cond_eq
  305. && field->conditions->type == insn_field_cond_value)
  306. {
  307. word->bit[i]->mask = 1;
  308. word->bit[i]->value = ((field->conditions->value
  309. & ((insn_uint) 1 <<
  310. (field->last - i))) != 0);
  311. }
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. return word;
  318. }
  319. static void
  320. parse_insn_words (insn_entry * insn, char *formats)
  321. {
  322. insn_word_entry **last_word = &insn->words;
  323. char *chp;
  324. /* now work through the formats */
  325. insn->nr_words = 0;
  326. chp = formats;
  327. while (1)
  328. {
  329. char *start_pos;
  330. char *end_pos;
  331. int strlen_pos;
  332. char *format;
  333. insn_word_entry *new_word;
  334. /* skip leading spaces */
  335. chp = skip_spaces (chp);
  336. /* break out the format */
  337. start_pos = chp;
  338. chp = skip_to_separator (chp, "+");
  339. end_pos = back_spaces (start_pos, chp);
  340. strlen_pos = end_pos - start_pos;
  341. /* check that something was there */
  342. if (strlen_pos == 0)
  343. error (insn->line, "missing or empty instruction format\n");
  344. /* parse the field */
  345. format = NZALLOC (char, strlen_pos + 1);
  346. strncpy (format, start_pos, strlen_pos);
  347. new_word = parse_insn_word (insn->line, format, insn->nr_words);
  348. insn->nr_words++;
  349. if (filter_is_common (insn->field_names, new_word->field_names))
  350. error (insn->line, "Field name duplicated between two words\n");
  351. filter_add (&insn->field_names, new_word->field_names);
  352. /* insert it */
  353. *last_word = new_word;
  354. last_word = &new_word->next;
  355. /* last format? */
  356. if (*chp == '\0')
  357. break;
  358. ASSERT (*chp == '+');
  359. chp++;
  360. }
  361. /* create a quick access array (indexed by word) of the same structure */
  362. {
  363. int i;
  364. insn_word_entry *word;
  365. insn->word = NZALLOC (insn_word_entry *, insn->nr_words + 1);
  366. for (i = 0, word = insn->words;
  367. i < insn->nr_words; i++, word = word->next)
  368. insn->word[i] = word;
  369. }
  370. /* Go over all fields that have conditionals refering to other
  371. fields. Link the fields up. Verify that the two fields have the
  372. same size. Verify that the two fields are different */
  373. {
  374. int i;
  375. for (i = 0; i < insn->nr_words; i++)
  376. {
  377. insn_word_entry *word = insn->word[i];
  378. insn_field_entry *f;
  379. for (f = word->first; f->last < options.insn_bit_size; f = f->next)
  380. {
  381. insn_field_cond *cond;
  382. for (cond = f->conditions; cond != NULL; cond = cond->next)
  383. {
  384. if (cond->type == insn_field_cond_field)
  385. {
  386. int j;
  387. if (strcmp (cond->string, f->val_string) == 0)
  388. error (insn->line,
  389. "Conditional `%s' of field `%s' refers to its self\n",
  390. cond->string, f->val_string);
  391. for (j = 0; j <= i && cond->field == NULL; j++)
  392. {
  393. insn_word_entry *refered_word = insn->word[j];
  394. insn_field_entry *refered_field;
  395. for (refered_field = refered_word->first;
  396. refered_field != NULL && cond->field == NULL;
  397. refered_field = refered_field->next)
  398. {
  399. if (refered_field->type == insn_field_string
  400. && strcmp (refered_field->val_string,
  401. cond->string) == 0)
  402. {
  403. /* found field being refered to by conditonal */
  404. cond->field = refered_field;
  405. /* check refered to and this field are
  406. the same size */
  407. if (f->width != refered_field->width)
  408. error (insn->line,
  409. "Conditional `%s' of field `%s' should be of size %s\n",
  410. cond->string, f->val_string,
  411. refered_field->width);
  412. }
  413. }
  414. }
  415. if (cond->field == NULL)
  416. error (insn->line,
  417. "Conditional `%s' of field `%s' not yet defined\n",
  418. cond->string, f->val_string);
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. typedef enum
  426. {
  427. unknown_record = 0,
  428. insn_record, /* default */
  429. code_record,
  430. cache_record,
  431. compute_record,
  432. scratch_record,
  433. option_record,
  434. string_function_record,
  435. function_record,
  436. internal_record,
  437. define_record,
  438. include_record,
  439. model_processor_record,
  440. model_macro_record,
  441. model_data_record,
  442. model_static_record,
  443. model_function_record,
  444. model_internal_record,
  445. }
  446. insn_record_type;
  447. static const name_map insn_type_map[] = {
  448. {"option", option_record},
  449. {"cache", cache_record},
  450. {"compute", compute_record},
  451. {"scratch", scratch_record},
  452. {"define", define_record},
  453. {"include", include_record},
  454. {"%s", string_function_record},
  455. {"function", function_record},
  456. {"internal", internal_record},
  457. {"model", model_processor_record},
  458. {"model-macro", model_macro_record},
  459. {"model-data", model_data_record},
  460. {"model-static", model_static_record},
  461. {"model-internal", model_internal_record},
  462. {"model-function", model_function_record},
  463. {NULL, insn_record},
  464. };
  465. static int
  466. record_is_old (table_entry *entry)
  467. {
  468. if (entry->nr_fields > record_type_field
  469. && strlen (entry->field[record_type_field]) == 0)
  470. return 1;
  471. return 0;
  472. }
  473. static insn_record_type
  474. record_type (table_entry *entry)
  475. {
  476. switch (entry->type)
  477. {
  478. case table_code_entry:
  479. return code_record;
  480. case table_colon_entry:
  481. if (record_is_old (entry))
  482. {
  483. /* old-format? */
  484. if (entry->nr_fields > old_record_type_field)
  485. {
  486. int i = name2i (entry->field[old_record_type_field],
  487. insn_type_map);
  488. return i;
  489. }
  490. else
  491. {
  492. return unknown_record;
  493. }
  494. }
  495. else if (entry->nr_fields > record_type_field
  496. && entry->field[0][0] == '\0')
  497. {
  498. /* new-format? */
  499. int i = name2i (entry->field[record_type_field],
  500. insn_type_map);
  501. return i;
  502. }
  503. else
  504. return insn_record; /* default */
  505. }
  506. return unknown_record;
  507. }
  508. static int
  509. record_prefix_is (table_entry *entry, char ch, int nr_fields)
  510. {
  511. if (entry->type != table_colon_entry)
  512. return 0;
  513. if (entry->nr_fields < nr_fields)
  514. return 0;
  515. if (entry->field[0][0] != ch && ch != '\0')
  516. return 0;
  517. return 1;
  518. }
  519. static table_entry *
  520. parse_model_data_record (insn_table *isa,
  521. table *file,
  522. table_entry *record,
  523. int nr_fields, model_data **list)
  524. {
  525. table_entry *model_record = record;
  526. table_entry *code_record = NULL;
  527. model_data *new_data;
  528. if (record->nr_fields < nr_fields)
  529. error (record->line, "Incorrect number of fields\n");
  530. record = table_read (file);
  531. if (record->type == table_code_entry)
  532. {
  533. code_record = record;
  534. record = table_read (file);
  535. }
  536. /* create the new data record */
  537. new_data = ZALLOC (model_data);
  538. new_data->line = model_record->line;
  539. filter_parse (&new_data->flags,
  540. model_record->field[record_filter_flags_field]);
  541. new_data->entry = model_record;
  542. new_data->code = code_record;
  543. /* append it if not filtered out */
  544. if (!is_filtered_out (options.flags_filter,
  545. model_record->field[record_filter_flags_field])
  546. && !is_filtered_out (options.model_filter,
  547. model_record->field[record_filter_models_field]))
  548. {
  549. while (*list != NULL)
  550. list = &(*list)->next;
  551. *list = new_data;
  552. }
  553. return record;
  554. }
  555. typedef enum
  556. {
  557. insn_bit_size_option = 1,
  558. insn_specifying_widths_option,
  559. hi_bit_nr_option,
  560. flags_filter_option,
  561. model_filter_option,
  562. multi_sim_option,
  563. format_names_option,
  564. gen_delayed_branch,
  565. unknown_option,
  566. }
  567. option_names;
  568. static const name_map option_map[] = {
  569. {"insn-bit-size", insn_bit_size_option},
  570. {"insn-specifying-widths", insn_specifying_widths_option},
  571. {"hi-bit-nr", hi_bit_nr_option},
  572. {"flags-filter", flags_filter_option},
  573. {"model-filter", model_filter_option},
  574. {"multi-sim", multi_sim_option},
  575. {"format-names", format_names_option},
  576. {"gen-delayed-branch", gen_delayed_branch},
  577. {NULL, unknown_option},
  578. };
  579. static table_entry *
  580. parse_include_record (table *file, table_entry *record)
  581. {
  582. /* parse the include record */
  583. if (record->nr_fields < nr_include_fields)
  584. error (record->line, "Incorrect nr fields for include record\n");
  585. /* process it */
  586. if (!is_filtered_out (options.flags_filter,
  587. record->field[record_filter_flags_field])
  588. && !is_filtered_out (options.model_filter,
  589. record->field[record_filter_models_field]))
  590. {
  591. table_push (file, record->line, options.include,
  592. record->field[include_filename_field]);
  593. }
  594. /* nb: can't read next record until after the file has been pushed */
  595. record = table_read (file);
  596. return record;
  597. }
  598. static table_entry *
  599. parse_option_record (table *file, table_entry *record)
  600. {
  601. table_entry *option_record;
  602. /* parse the option record */
  603. option_record = record;
  604. if (record->nr_fields < nr_option_fields)
  605. error (record->line, "Incorrect nr of fields for option record\n");
  606. record = table_read (file);
  607. /* process it */
  608. if (!is_filtered_out (options.flags_filter,
  609. option_record->field[record_filter_flags_field])
  610. && !is_filtered_out (options.model_filter,
  611. option_record->field[record_filter_models_field]))
  612. {
  613. char *name = option_record->field[option_name_field];
  614. option_names option = name2i (name, option_map);
  615. char *value = option_record->field[option_value_field];
  616. switch (option)
  617. {
  618. case insn_bit_size_option:
  619. {
  620. options.insn_bit_size = a2i (value);
  621. if (options.insn_bit_size < 0
  622. || options.insn_bit_size > max_insn_bit_size)
  623. error (option_record->line,
  624. "Instruction bit size out of range\n");
  625. if (options.hi_bit_nr != options.insn_bit_size - 1
  626. && options.hi_bit_nr != 0)
  627. error (option_record->line,
  628. "insn-bit-size / hi-bit-nr conflict\n");
  629. break;
  630. }
  631. case insn_specifying_widths_option:
  632. {
  633. options.insn_specifying_widths = a2i (value);
  634. break;
  635. }
  636. case hi_bit_nr_option:
  637. {
  638. options.hi_bit_nr = a2i (value);
  639. if (options.hi_bit_nr != 0
  640. && options.hi_bit_nr != options.insn_bit_size - 1)
  641. error (option_record->line,
  642. "hi-bit-nr / insn-bit-size conflict\n");
  643. break;
  644. }
  645. case flags_filter_option:
  646. {
  647. filter_parse (&options.flags_filter, value);
  648. break;
  649. }
  650. case model_filter_option:
  651. {
  652. filter_parse (&options.model_filter, value);
  653. break;
  654. }
  655. case multi_sim_option:
  656. {
  657. options.gen.multi_sim = a2i (value);
  658. break;
  659. }
  660. case format_names_option:
  661. {
  662. filter_parse (&options.format_name_filter, value);
  663. break;
  664. }
  665. case gen_delayed_branch:
  666. {
  667. options.gen.delayed_branch = a2i (value);
  668. break;
  669. }
  670. case unknown_option:
  671. {
  672. error (option_record->line, "Unknown option - %s\n", name);
  673. break;
  674. }
  675. }
  676. }
  677. return record;
  678. }
  679. static table_entry *
  680. parse_function_record (table *file,
  681. table_entry *record,
  682. function_entry ** list,
  683. function_entry ** list_entry,
  684. int is_internal, model_table *model)
  685. {
  686. function_entry *new_function;
  687. new_function = ZALLOC (function_entry);
  688. new_function->line = record->line;
  689. new_function->is_internal = is_internal;
  690. /* parse the function header */
  691. if (record_is_old (record))
  692. {
  693. if (record->nr_fields < nr_old_function_fields)
  694. error (record->line, "Missing fields from (old) function record\n");
  695. new_function->type = record->field[old_function_typedef_field];
  696. new_function->type = record->field[old_function_typedef_field];
  697. if (record->nr_fields > old_function_param_field)
  698. new_function->param = record->field[old_function_param_field];
  699. new_function->name = record->field[old_function_name_field];
  700. }
  701. else
  702. {
  703. if (record->nr_fields < nr_function_fields)
  704. error (record->line, "Missing fields from function record\n");
  705. filter_parse (&new_function->flags,
  706. record->field[record_filter_flags_field]);
  707. filter_parse (&new_function->models,
  708. record->field[record_filter_models_field]);
  709. new_function->type = record->field[function_typedef_field];
  710. new_function->param = record->field[function_param_field];
  711. new_function->name = record->field[function_name_field];
  712. }
  713. record = table_read (file);
  714. /* parse any function-model records */
  715. while (record != NULL
  716. && record_prefix_is (record, '*', nr_function_model_fields))
  717. {
  718. char *model_name = record->field[function_model_name_field] + 1; /*skip `*' */
  719. filter_parse (&new_function->models, model_name);
  720. if (!filter_is_subset (model->processors, new_function->models))
  721. {
  722. error (record->line, "machine model `%s' undefined\n", model_name);
  723. }
  724. record = table_read (file);
  725. }
  726. /* parse the function body */
  727. if (record->type == table_code_entry)
  728. {
  729. new_function->code = record;
  730. record = table_read (file);
  731. }
  732. /* insert it */
  733. if (!filter_is_subset (options.flags_filter, new_function->flags))
  734. {
  735. if (options.warn.discard)
  736. notify (new_function->line, "Discarding function %s - filter flags\n",
  737. new_function->name);
  738. }
  739. else if (new_function->models != NULL
  740. && !filter_is_common (options.model_filter, new_function->models))
  741. {
  742. if (options.warn.discard)
  743. notify (new_function->line,
  744. "Discarding function %s - filter models\n",
  745. new_function->name);
  746. }
  747. else
  748. {
  749. while (*list != NULL)
  750. list = &(*list)->next;
  751. *list = new_function;
  752. if (list_entry != NULL)
  753. *list_entry = new_function;
  754. }
  755. /* done */
  756. return record;
  757. }
  758. static void
  759. parse_insn_model_record (table *file,
  760. table_entry *record,
  761. insn_entry * insn, model_table *model)
  762. {
  763. insn_model_entry **last_insn_model;
  764. insn_model_entry *new_insn_model = ZALLOC (insn_model_entry);
  765. /* parse it */
  766. new_insn_model->line = record->line;
  767. if (record->nr_fields > insn_model_unit_data_field)
  768. new_insn_model->unit_data = record->field[insn_model_unit_data_field];
  769. new_insn_model->insn = insn;
  770. /* parse the model names, verify that all were defined */
  771. new_insn_model->names = NULL;
  772. filter_parse (&new_insn_model->names,
  773. record->field[insn_model_name_field] + 1 /*skip `*' */ );
  774. if (new_insn_model->names == NULL)
  775. {
  776. /* No processor names - a generic model entry, enter it into all
  777. the non-empty fields */
  778. int index;
  779. for (index = 0; index < model->nr_models; index++)
  780. if (insn->model[index] == 0)
  781. {
  782. insn->model[index] = new_insn_model;
  783. }
  784. /* also add the complete processor set to this processor's set */
  785. filter_add (&insn->processors, model->processors);
  786. }
  787. else
  788. {
  789. /* Find the corresponding master model record for each name so
  790. that they can be linked in. */
  791. int index;
  792. char *name = "";
  793. while (1)
  794. {
  795. name = filter_next (new_insn_model->names, name);
  796. if (name == NULL)
  797. break;
  798. index = filter_is_member (model->processors, name) - 1;
  799. if (index < 0)
  800. {
  801. error (new_insn_model->line,
  802. "machine model `%s' undefined\n", name);
  803. }
  804. /* store it in the corresponding model array entry */
  805. if (insn->model[index] != NULL && insn->model[index]->names != NULL)
  806. {
  807. warning (new_insn_model->line,
  808. "machine model `%s' previously defined\n", name);
  809. error (insn->model[index]->line, "earlier definition\n");
  810. }
  811. insn->model[index] = new_insn_model;
  812. /* also add the name to the instructions processor set as an
  813. alternative lookup mechanism */
  814. filter_parse (&insn->processors, name);
  815. }
  816. }
  817. /* link it in */
  818. last_insn_model = &insn->models;
  819. while ((*last_insn_model) != NULL)
  820. last_insn_model = &(*last_insn_model)->next;
  821. *last_insn_model = new_insn_model;
  822. }
  823. static void
  824. parse_insn_mnemonic_record (table *file,
  825. table_entry *record, insn_entry * insn)
  826. {
  827. insn_mnemonic_entry **last_insn_mnemonic;
  828. insn_mnemonic_entry *new_insn_mnemonic = ZALLOC (insn_mnemonic_entry);
  829. /* parse it */
  830. new_insn_mnemonic->line = record->line;
  831. ASSERT (record->nr_fields > insn_mnemonic_format_field);
  832. new_insn_mnemonic->format = record->field[insn_mnemonic_format_field];
  833. ASSERT (new_insn_mnemonic->format[0] == '"');
  834. if (new_insn_mnemonic->format[strlen (new_insn_mnemonic->format) - 1] !=
  835. '"')
  836. error (new_insn_mnemonic->line,
  837. "Missing closing double quote in mnemonic field\n");
  838. if (record->nr_fields > insn_mnemonic_condition_field)
  839. new_insn_mnemonic->condition =
  840. record->field[insn_mnemonic_condition_field];
  841. new_insn_mnemonic->insn = insn;
  842. /* insert it */
  843. last_insn_mnemonic = &insn->mnemonics;
  844. while ((*last_insn_mnemonic) != NULL)
  845. last_insn_mnemonic = &(*last_insn_mnemonic)->next;
  846. insn->nr_mnemonics++;
  847. *last_insn_mnemonic = new_insn_mnemonic;
  848. }
  849. static table_entry *
  850. parse_macro_record (table *file, table_entry *record)
  851. {
  852. #if 1
  853. error (record->line, "Macros are not implemented");
  854. #else
  855. /* parse the define record */
  856. if (record->nr_fields < nr_define_fields)
  857. error (record->line, "Incorrect nr fields for define record\n");
  858. /* process it */
  859. if (!is_filtered_out (options.flags_filter,
  860. record->field[record_filter_flags_field])
  861. && !is_filtered_out (options.model_filter,
  862. record->field[record_filter_models_field]))
  863. {
  864. table_define (file,
  865. record->line,
  866. record->field[macro_name_field],
  867. record->field[macro_args_field],
  868. record->field[macro_expr_field]);
  869. }
  870. record = table_read (file);
  871. #endif
  872. return record;
  873. }
  874. insn_table *
  875. load_insn_table (char *file_name, cache_entry *cache)
  876. {
  877. table *file = table_open (file_name);
  878. table_entry *record = table_read (file);
  879. insn_table *isa = ZALLOC (insn_table);
  880. model_table *model = ZALLOC (model_table);
  881. isa->model = model;
  882. isa->caches = cache;
  883. while (record != NULL)
  884. {
  885. switch (record_type (record))
  886. {
  887. case include_record:
  888. {
  889. record = parse_include_record (file, record);
  890. break;
  891. }
  892. case option_record:
  893. {
  894. if (isa->insns != NULL)
  895. error (record->line, "Option after first instruction\n");
  896. record = parse_option_record (file, record);
  897. break;
  898. }
  899. case string_function_record:
  900. {
  901. function_entry *function = NULL;
  902. record = parse_function_record (file, record,
  903. &isa->functions,
  904. &function, 0 /*is-internal */ ,
  905. model);
  906. /* convert a string function record into an internal function */
  907. if (function != NULL)
  908. {
  909. char *name = NZALLOC (char,
  910. (strlen ("str_")
  911. + strlen (function->name) + 1));
  912. strcat (name, "str_");
  913. strcat (name, function->name);
  914. function->name = name;
  915. function->type = "const char *";
  916. }
  917. break;
  918. }
  919. case function_record: /* function record */
  920. {
  921. record = parse_function_record (file, record,
  922. &isa->functions,
  923. NULL, 0 /*is-internal */ ,
  924. model);
  925. break;
  926. }
  927. case internal_record:
  928. {
  929. /* only insert it into the function list if it is unknown */
  930. function_entry *function = NULL;
  931. record = parse_function_record (file, record,
  932. &isa->functions,
  933. &function, 1 /*is-internal */ ,
  934. model);
  935. /* check what was inserted to see if a pseudo-instruction
  936. entry also needs to be created */
  937. if (function != NULL)
  938. {
  939. insn_entry **insn = NULL;
  940. if (strcmp (function->name, "illegal") == 0)
  941. {
  942. /* illegal function save it away */
  943. if (isa->illegal_insn != NULL)
  944. {
  945. warning (function->line,
  946. "Multiple illegal instruction definitions\n");
  947. error (isa->illegal_insn->line,
  948. "Location of first illegal instruction\n");
  949. }
  950. else
  951. insn = &isa->illegal_insn;
  952. }
  953. if (insn != NULL)
  954. {
  955. *insn = ZALLOC (insn_entry);
  956. (*insn)->line = function->line;
  957. (*insn)->name = function->name;
  958. (*insn)->code = function->code;
  959. }
  960. }
  961. break;
  962. }
  963. case scratch_record: /* cache macro records */
  964. case cache_record:
  965. case compute_record:
  966. {
  967. cache_entry *new_cache;
  968. /* parse the cache record */
  969. if (record->nr_fields < nr_cache_fields)
  970. error (record->line,
  971. "Incorrect nr of fields for scratch/cache/compute record\n");
  972. /* create it */
  973. new_cache = ZALLOC (cache_entry);
  974. new_cache->line = record->line;
  975. filter_parse (&new_cache->flags,
  976. record->field[record_filter_flags_field]);
  977. filter_parse (&new_cache->models,
  978. record->field[record_filter_models_field]);
  979. new_cache->type = record->field[cache_typedef_field];
  980. new_cache->name = record->field[cache_name_field];
  981. filter_parse (&new_cache->original_fields,
  982. record->field[cache_original_fields_field]);
  983. new_cache->expression = record->field[cache_expression_field];
  984. /* insert it but only if not filtered out */
  985. if (!filter_is_subset (options.flags_filter, new_cache->flags))
  986. {
  987. notify (new_cache->line,
  988. "Discarding cache entry %s - filter flags\n",
  989. new_cache->name);
  990. }
  991. else if (is_filtered_out (options.model_filter,
  992. record->
  993. field[record_filter_models_field]))
  994. {
  995. notify (new_cache->line,
  996. "Discarding cache entry %s - filter models\n",
  997. new_cache->name);
  998. }
  999. else
  1000. {
  1001. cache_entry **last;
  1002. last = &isa->caches;
  1003. while (*last != NULL)
  1004. last = &(*last)->next;
  1005. *last = new_cache;
  1006. }
  1007. /* advance things */
  1008. record = table_read (file);
  1009. break;
  1010. }
  1011. /* model records */
  1012. case model_processor_record:
  1013. {
  1014. model_entry *new_model;
  1015. /* parse the model */
  1016. if (record->nr_fields < nr_model_processor_fields)
  1017. error (record->line,
  1018. "Incorrect nr of fields for model record\n");
  1019. if (isa->insns != NULL)
  1020. error (record->line, "Model appears after first instruction\n");
  1021. new_model = ZALLOC (model_entry);
  1022. filter_parse (&new_model->flags,
  1023. record->field[record_filter_flags_field]);
  1024. new_model->line = record->line;
  1025. new_model->name = record->field[model_name_field];
  1026. new_model->full_name = record->field[model_full_name_field];
  1027. new_model->unit_data = record->field[model_unit_data_field];
  1028. /* only insert it if not filtered out */
  1029. if (!filter_is_subset (options.flags_filter, new_model->flags))
  1030. {
  1031. notify (new_model->line,
  1032. "Discarding processor model %s - filter flags\n",
  1033. new_model->name);
  1034. }
  1035. else if (is_filtered_out (options.model_filter,
  1036. record->
  1037. field[record_filter_models_field]))
  1038. {
  1039. notify (new_model->line,
  1040. "Discarding processor model %s - filter models\n",
  1041. new_model->name);
  1042. }
  1043. else if (filter_is_member (model->processors, new_model->name))
  1044. {
  1045. error (new_model->line, "Duplicate processor model %s\n",
  1046. new_model->name);
  1047. }
  1048. else
  1049. {
  1050. model_entry **last;
  1051. last = &model->models;
  1052. while (*last != NULL)
  1053. last = &(*last)->next;
  1054. *last = new_model;
  1055. /* count it */
  1056. model->nr_models++;
  1057. filter_parse (&model->processors, new_model->name);
  1058. }
  1059. /* advance things */
  1060. record = table_read (file);
  1061. }
  1062. break;
  1063. case model_macro_record:
  1064. record = parse_model_data_record (isa, file, record,
  1065. nr_model_macro_fields,
  1066. &model->macros);
  1067. break;
  1068. case model_data_record:
  1069. record = parse_model_data_record (isa, file, record,
  1070. nr_model_data_fields,
  1071. &model->data);
  1072. break;
  1073. case model_static_record:
  1074. record = parse_function_record (file, record,
  1075. &model->statics,
  1076. NULL, 0 /*is internal */ ,
  1077. model);
  1078. break;
  1079. case model_internal_record:
  1080. record = parse_function_record (file, record,
  1081. &model->internals,
  1082. NULL, 1 /*is internal */ ,
  1083. model);
  1084. break;
  1085. case model_function_record:
  1086. record = parse_function_record (file, record,
  1087. &model->functions,
  1088. NULL, 0 /*is internal */ ,
  1089. model);
  1090. break;
  1091. case insn_record: /* instruction records */
  1092. {
  1093. insn_entry *new_insn;
  1094. char *format;
  1095. /* parse the instruction */
  1096. if (record->nr_fields < nr_insn_fields)
  1097. error (record->line,
  1098. "Incorrect nr of fields for insn record\n");
  1099. new_insn = ZALLOC (insn_entry);
  1100. new_insn->line = record->line;
  1101. filter_parse (&new_insn->flags,
  1102. record->field[record_filter_flags_field]);
  1103. /* save the format field. Can't parse it until after the
  1104. filter-out checks. Could be filtered out because the
  1105. format is invalid */
  1106. format = record->field[insn_word_field];
  1107. new_insn->format_name = record->field[insn_format_name_field];
  1108. if (options.format_name_filter != NULL
  1109. && !filter_is_member (options.format_name_filter,
  1110. new_insn->format_name))
  1111. error (new_insn->line,
  1112. "Unreconized instruction format name `%s'\n",
  1113. new_insn->format_name);
  1114. filter_parse (&new_insn->options,
  1115. record->field[insn_options_field]);
  1116. new_insn->name = record->field[insn_name_field];
  1117. record = table_read (file);
  1118. /* Parse any model/assember records */
  1119. new_insn->nr_models = model->nr_models;
  1120. new_insn->model =
  1121. NZALLOC (insn_model_entry *, model->nr_models + 1);
  1122. while (record != NULL)
  1123. {
  1124. if (record_prefix_is (record, '*', nr_insn_model_fields))
  1125. parse_insn_model_record (file, record, new_insn, model);
  1126. else
  1127. if (record_prefix_is (record, '"', nr_insn_mnemonic_fields))
  1128. parse_insn_mnemonic_record (file, record, new_insn);
  1129. else
  1130. break;
  1131. /* advance */
  1132. record = table_read (file);
  1133. }
  1134. /* Parse the code record */
  1135. if (record != NULL && record->type == table_code_entry)
  1136. {
  1137. new_insn->code = record;
  1138. record = table_read (file);
  1139. }
  1140. else if (options.warn.unimplemented)
  1141. notify (new_insn->line, "unimplemented\n");
  1142. /* insert it */
  1143. if (!filter_is_subset (options.flags_filter, new_insn->flags))
  1144. {
  1145. if (options.warn.discard)
  1146. notify (new_insn->line,
  1147. "Discarding instruction %s (flags-filter)\n",
  1148. new_insn->name);
  1149. }
  1150. else if (new_insn->processors != NULL
  1151. && options.model_filter != NULL
  1152. && !filter_is_common (options.model_filter,
  1153. new_insn->processors))
  1154. {
  1155. /* only discard an instruction based in the processor
  1156. model when both the instruction and the options are
  1157. nonempty */
  1158. if (options.warn.discard)
  1159. notify (new_insn->line,
  1160. "Discarding instruction %s (processor-model)\n",
  1161. new_insn->name);
  1162. }
  1163. else
  1164. {
  1165. insn_entry **last;
  1166. /* finish the parsing */
  1167. parse_insn_words (new_insn, format);
  1168. /* append it */
  1169. last = &isa->insns;
  1170. while (*last)
  1171. last = &(*last)->next;
  1172. *last = new_insn;
  1173. /* update global isa counters */
  1174. isa->nr_insns++;
  1175. if (isa->max_nr_words < new_insn->nr_words)
  1176. isa->max_nr_words = new_insn->nr_words;
  1177. filter_add (&isa->flags, new_insn->flags);
  1178. filter_add (&isa->options, new_insn->options);
  1179. }
  1180. break;
  1181. }
  1182. case define_record:
  1183. record = parse_macro_record (file, record);
  1184. break;
  1185. case unknown_record:
  1186. case code_record:
  1187. error (record->line, "Unknown or unexpected entry\n");
  1188. }
  1189. }
  1190. return isa;
  1191. }
  1192. void
  1193. print_insn_words (lf *file, insn_entry * insn)
  1194. {
  1195. insn_word_entry *word = insn->words;
  1196. if (word != NULL)
  1197. {
  1198. while (1)
  1199. {
  1200. insn_field_entry *field = word->first;
  1201. while (1)
  1202. {
  1203. insn_field_cond *cond;
  1204. if (options.insn_specifying_widths)
  1205. lf_printf (file, "%d.", field->width);
  1206. else
  1207. lf_printf (file, "%d.",
  1208. i2target (options.hi_bit_nr, field->first));
  1209. switch (field->type)
  1210. {
  1211. case insn_field_invalid:
  1212. ASSERT (0);
  1213. break;
  1214. case insn_field_int:
  1215. lf_printf (file, "0x%lx", (long) field->val_int);
  1216. break;
  1217. case insn_field_reserved:
  1218. lf_printf (file, "/");
  1219. break;
  1220. case insn_field_wild:
  1221. lf_printf (file, "*");
  1222. break;
  1223. case insn_field_string:
  1224. lf_printf (file, "%s", field->val_string);
  1225. if (field->conditions == NULL)
  1226. break;
  1227. if (field->conditions->test == insn_field_cond_eq)
  1228. {
  1229. if (field->conditions->type == insn_field_cond_value)
  1230. lf_printf (file, "=%ld",
  1231. (long) field->conditions->value);
  1232. else
  1233. lf_printf (file, "=%s", field->conditions->string);
  1234. /* There can be only one equality condition. */
  1235. ASSERT (field->conditions->next == NULL);
  1236. break;
  1237. }
  1238. for (cond = field->conditions;
  1239. cond != NULL;
  1240. cond = cond->next)
  1241. {
  1242. ASSERT (cond->test == insn_field_cond_ne);
  1243. if (cond->type == insn_field_cond_value)
  1244. lf_printf (file, "!%ld", (long) cond->value);
  1245. else
  1246. lf_printf (file, "!%s", cond->string);
  1247. }
  1248. break;
  1249. }
  1250. if (field == word->last)
  1251. break;
  1252. field = field->next;
  1253. lf_printf (file, ",");
  1254. }
  1255. word = word->next;
  1256. if (word == NULL)
  1257. break;
  1258. lf_printf (file, "+");
  1259. }
  1260. }
  1261. }
  1262. void
  1263. function_entry_traverse (lf *file,
  1264. function_entry * functions,
  1265. function_entry_handler * handler, void *data)
  1266. {
  1267. function_entry *function;
  1268. for (function = functions; function != NULL; function = function->next)
  1269. {
  1270. handler (file, function, data);
  1271. }
  1272. }
  1273. void
  1274. insn_table_traverse_insn (lf *file,
  1275. insn_table *isa,
  1276. insn_entry_handler * handler, void *data)
  1277. {
  1278. insn_entry *insn;
  1279. for (insn = isa->insns; insn != NULL; insn = insn->next)
  1280. {
  1281. handler (file, isa, insn, data);
  1282. }
  1283. }
  1284. static void
  1285. dump_function_entry (lf *file,
  1286. char *prefix, function_entry * entry, char *suffix)
  1287. {
  1288. lf_printf (file, "%s(function_entry *) 0x%lx", prefix, (long) entry);
  1289. if (entry != NULL)
  1290. {
  1291. dump_line_ref (file, "\n(line ", entry->line, ")");
  1292. dump_filter (file, "\n(flags ", entry->flags, ")");
  1293. lf_printf (file, "\n(type \"%s\")", entry->type);
  1294. lf_printf (file, "\n(name \"%s\")", entry->name);
  1295. lf_printf (file, "\n(param \"%s\")", entry->param);
  1296. dump_table_entry (file, "\n(code ", entry->code, ")");
  1297. lf_printf (file, "\n(is_internal %d)", entry->is_internal);
  1298. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1299. }
  1300. lf_printf (file, "%s", suffix);
  1301. }
  1302. static void
  1303. dump_function_entries (lf *file,
  1304. char *prefix, function_entry * entry, char *suffix)
  1305. {
  1306. lf_printf (file, "%s", prefix);
  1307. lf_indent (file, +1);
  1308. while (entry != NULL)
  1309. {
  1310. dump_function_entry (file, "\n(", entry, ")");
  1311. entry = entry->next;
  1312. }
  1313. lf_indent (file, -1);
  1314. lf_printf (file, "%s", suffix);
  1315. }
  1316. static char *
  1317. cache_entry_type_to_str (cache_entry_type type)
  1318. {
  1319. switch (type)
  1320. {
  1321. case scratch_value:
  1322. return "scratch";
  1323. case cache_value:
  1324. return "cache";
  1325. case compute_value:
  1326. return "compute";
  1327. }
  1328. ERROR ("Bad switch");
  1329. return 0;
  1330. }
  1331. static void
  1332. dump_cache_entry (lf *file, char *prefix, cache_entry *entry, char *suffix)
  1333. {
  1334. lf_printf (file, "%s(cache_entry *) 0x%lx", prefix, (long) entry);
  1335. if (entry != NULL)
  1336. {
  1337. dump_line_ref (file, "\n(line ", entry->line, ")");
  1338. dump_filter (file, "\n(flags ", entry->flags, ")");
  1339. lf_printf (file, "\n(entry_type \"%s\")",
  1340. cache_entry_type_to_str (entry->entry_type));
  1341. lf_printf (file, "\n(name \"%s\")", entry->name);
  1342. dump_filter (file, "\n(original_fields ", entry->original_fields, ")");
  1343. lf_printf (file, "\n(type \"%s\")", entry->type);
  1344. lf_printf (file, "\n(expression \"%s\")", entry->expression);
  1345. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1346. }
  1347. lf_printf (file, "%s", suffix);
  1348. }
  1349. void
  1350. dump_cache_entries (lf *file, char *prefix, cache_entry *entry, char *suffix)
  1351. {
  1352. lf_printf (file, "%s", prefix);
  1353. lf_indent (file, +1);
  1354. while (entry != NULL)
  1355. {
  1356. dump_cache_entry (file, "\n(", entry, ")");
  1357. entry = entry->next;
  1358. }
  1359. lf_indent (file, -1);
  1360. lf_printf (file, "%s", suffix);
  1361. }
  1362. static void
  1363. dump_model_data (lf *file, char *prefix, model_data *entry, char *suffix)
  1364. {
  1365. lf_printf (file, "%s(model_data *) 0x%lx", prefix, (long) entry);
  1366. if (entry != NULL)
  1367. {
  1368. lf_indent (file, +1);
  1369. dump_line_ref (file, "\n(line ", entry->line, ")");
  1370. dump_filter (file, "\n(flags ", entry->flags, ")");
  1371. dump_table_entry (file, "\n(entry ", entry->entry, ")");
  1372. dump_table_entry (file, "\n(code ", entry->code, ")");
  1373. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1374. lf_indent (file, -1);
  1375. }
  1376. lf_printf (file, "%s", prefix);
  1377. }
  1378. static void
  1379. dump_model_datas (lf *file, char *prefix, model_data *entry, char *suffix)
  1380. {
  1381. lf_printf (file, "%s", prefix);
  1382. lf_indent (file, +1);
  1383. while (entry != NULL)
  1384. {
  1385. dump_model_data (file, "\n(", entry, ")");
  1386. entry = entry->next;
  1387. }
  1388. lf_indent (file, -1);
  1389. lf_printf (file, "%s", suffix);
  1390. }
  1391. static void
  1392. dump_model_entry (lf *file, char *prefix, model_entry *entry, char *suffix)
  1393. {
  1394. lf_printf (file, "%s(model_entry *) 0x%lx", prefix, (long) entry);
  1395. if (entry != NULL)
  1396. {
  1397. lf_indent (file, +1);
  1398. dump_line_ref (file, "\n(line ", entry->line, ")");
  1399. dump_filter (file, "\n(flags ", entry->flags, ")");
  1400. lf_printf (file, "\n(name \"%s\")", entry->name);
  1401. lf_printf (file, "\n(full_name \"%s\")", entry->full_name);
  1402. lf_printf (file, "\n(unit_data \"%s\")", entry->unit_data);
  1403. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1404. lf_indent (file, -1);
  1405. }
  1406. lf_printf (file, "%s", prefix);
  1407. }
  1408. static void
  1409. dump_model_entries (lf *file, char *prefix, model_entry *entry, char *suffix)
  1410. {
  1411. lf_printf (file, "%s", prefix);
  1412. lf_indent (file, +1);
  1413. while (entry != NULL)
  1414. {
  1415. dump_model_entry (file, "\n(", entry, ")");
  1416. entry = entry->next;
  1417. }
  1418. lf_indent (file, -1);
  1419. lf_printf (file, "%s", suffix);
  1420. }
  1421. static void
  1422. dump_model_table (lf *file, char *prefix, model_table *entry, char *suffix)
  1423. {
  1424. lf_printf (file, "%s(model_table *) 0x%lx", prefix, (long) entry);
  1425. if (entry != NULL)
  1426. {
  1427. lf_indent (file, +1);
  1428. dump_filter (file, "\n(processors ", entry->processors, ")");
  1429. lf_printf (file, "\n(nr_models %d)", entry->nr_models);
  1430. dump_model_entries (file, "\n(models ", entry->models, ")");
  1431. dump_model_datas (file, "\n(macros ", entry->macros, ")");
  1432. dump_model_datas (file, "\n(data ", entry->data, ")");
  1433. dump_function_entries (file, "\n(statics ", entry->statics, ")");
  1434. dump_function_entries (file, "\n(internals ", entry->functions, ")");
  1435. dump_function_entries (file, "\n(functions ", entry->functions, ")");
  1436. lf_indent (file, -1);
  1437. }
  1438. lf_printf (file, "%s", suffix);
  1439. }
  1440. static char *
  1441. insn_field_type_to_str (insn_field_type type)
  1442. {
  1443. switch (type)
  1444. {
  1445. case insn_field_invalid:
  1446. ASSERT (0);
  1447. return "(invalid)";
  1448. case insn_field_int:
  1449. return "int";
  1450. case insn_field_reserved:
  1451. return "reserved";
  1452. case insn_field_wild:
  1453. return "wild";
  1454. case insn_field_string:
  1455. return "string";
  1456. }
  1457. ERROR ("bad switch");
  1458. return 0;
  1459. }
  1460. void
  1461. dump_insn_field (lf *file,
  1462. char *prefix, insn_field_entry *field, char *suffix)
  1463. {
  1464. char *sep = " ";
  1465. lf_printf (file, "%s(insn_field_entry *) 0x%lx", prefix, (long) field);
  1466. if (field != NULL)
  1467. {
  1468. lf_indent (file, +1);
  1469. lf_printf (file, "%s(first %d)", sep, field->first);
  1470. lf_printf (file, "%s(last %d)", sep, field->last);
  1471. lf_printf (file, "%s(width %d)", sep, field->width);
  1472. lf_printf (file, "%s(type %s)", sep,
  1473. insn_field_type_to_str (field->type));
  1474. switch (field->type)
  1475. {
  1476. case insn_field_invalid:
  1477. ASSERT (0);
  1478. break;
  1479. case insn_field_int:
  1480. lf_printf (file, "%s(val 0x%lx)", sep, (long) field->val_int);
  1481. break;
  1482. case insn_field_reserved:
  1483. /* nothing output */
  1484. break;
  1485. case insn_field_wild:
  1486. /* nothing output */
  1487. break;
  1488. case insn_field_string:
  1489. lf_printf (file, "%s(val \"%s\")", sep, field->val_string);
  1490. break;
  1491. }
  1492. lf_printf (file, "%s(next 0x%lx)", sep, (long) field->next);
  1493. lf_printf (file, "%s(prev 0x%lx)", sep, (long) field->prev);
  1494. lf_indent (file, -1);
  1495. }
  1496. lf_printf (file, "%s", suffix);
  1497. }
  1498. void
  1499. dump_insn_word_entry (lf *file,
  1500. char *prefix, insn_word_entry *word, char *suffix)
  1501. {
  1502. lf_printf (file, "%s(insn_word_entry *) 0x%lx", prefix, (long) word);
  1503. if (word != NULL)
  1504. {
  1505. int i;
  1506. insn_field_entry *field;
  1507. lf_indent (file, +1);
  1508. lf_printf (file, "\n(first 0x%lx)", (long) word->first);
  1509. lf_printf (file, "\n(last 0x%lx)", (long) word->last);
  1510. lf_printf (file, "\n(bit");
  1511. for (i = 0; i < options.insn_bit_size; i++)
  1512. lf_printf (file, "\n ((value %d) (mask %d) (field 0x%lx))",
  1513. word->bit[i]->value, word->bit[i]->mask,
  1514. (long) word->bit[i]->field);
  1515. lf_printf (file, ")");
  1516. for (field = word->first; field != NULL; field = field->next)
  1517. dump_insn_field (file, "\n(", field, ")");
  1518. dump_filter (file, "\n(field_names ", word->field_names, ")");
  1519. lf_printf (file, "\n(next 0x%lx)", (long) word->next);
  1520. lf_indent (file, -1);
  1521. }
  1522. lf_printf (file, "%s", suffix);
  1523. }
  1524. static void
  1525. dump_insn_word_entries (lf *file,
  1526. char *prefix, insn_word_entry *word, char *suffix)
  1527. {
  1528. lf_printf (file, "%s", prefix);
  1529. while (word != NULL)
  1530. {
  1531. dump_insn_word_entry (file, "\n(", word, ")");
  1532. word = word->next;
  1533. }
  1534. lf_printf (file, "%s", suffix);
  1535. }
  1536. static void
  1537. dump_insn_model_entry (lf *file,
  1538. char *prefix, insn_model_entry *model, char *suffix)
  1539. {
  1540. lf_printf (file, "%s(insn_model_entry *) 0x%lx", prefix, (long) model);
  1541. if (model != NULL)
  1542. {
  1543. lf_indent (file, +1);
  1544. dump_line_ref (file, "\n(line ", model->line, ")");
  1545. dump_filter (file, "\n(names ", model->names, ")");
  1546. lf_printf (file, "\n(full_name \"%s\")", model->full_name);
  1547. lf_printf (file, "\n(unit_data \"%s\")", model->unit_data);
  1548. lf_printf (file, "\n(insn (insn_entry *) 0x%lx)", (long) model->insn);
  1549. lf_printf (file, "\n(next (insn_model_entry *) 0x%lx)",
  1550. (long) model->next);
  1551. lf_indent (file, -1);
  1552. }
  1553. lf_printf (file, "%s", suffix);
  1554. }
  1555. static void
  1556. dump_insn_model_entries (lf *file,
  1557. char *prefix, insn_model_entry *model, char *suffix)
  1558. {
  1559. lf_printf (file, "%s", prefix);
  1560. while (model != NULL)
  1561. {
  1562. dump_insn_model_entry (file, "\n", model, "");
  1563. model = model->next;
  1564. }
  1565. lf_printf (file, "%s", suffix);
  1566. }
  1567. static void
  1568. dump_insn_mnemonic_entry (lf *file,
  1569. char *prefix,
  1570. insn_mnemonic_entry *mnemonic, char *suffix)
  1571. {
  1572. lf_printf (file, "%s(insn_mnemonic_entry *) 0x%lx", prefix,
  1573. (long) mnemonic);
  1574. if (mnemonic != NULL)
  1575. {
  1576. lf_indent (file, +1);
  1577. dump_line_ref (file, "\n(line ", mnemonic->line, ")");
  1578. lf_printf (file, "\n(format \"%s\")", mnemonic->format);
  1579. lf_printf (file, "\n(condition \"%s\")", mnemonic->condition);
  1580. lf_printf (file, "\n(insn (insn_entry *) 0x%lx)",
  1581. (long) mnemonic->insn);
  1582. lf_printf (file, "\n(next (insn_mnemonic_entry *) 0x%lx)",
  1583. (long) mnemonic->next);
  1584. lf_indent (file, -1);
  1585. }
  1586. lf_printf (file, "%s", suffix);
  1587. }
  1588. static void
  1589. dump_insn_mnemonic_entries (lf *file,
  1590. char *prefix,
  1591. insn_mnemonic_entry *mnemonic, char *suffix)
  1592. {
  1593. lf_printf (file, "%s", prefix);
  1594. while (mnemonic != NULL)
  1595. {
  1596. dump_insn_mnemonic_entry (file, "\n", mnemonic, "");
  1597. mnemonic = mnemonic->next;
  1598. }
  1599. lf_printf (file, "%s", suffix);
  1600. }
  1601. void
  1602. dump_insn_entry (lf *file, char *prefix, insn_entry * entry, char *suffix)
  1603. {
  1604. lf_printf (file, "%s(insn_entry *) 0x%lx", prefix, (long) entry);
  1605. if (entry != NULL)
  1606. {
  1607. int i;
  1608. lf_indent (file, +1);
  1609. dump_line_ref (file, "\n(line ", entry->line, ")");
  1610. dump_filter (file, "\n(flags ", entry->flags, ")");
  1611. lf_printf (file, "\n(nr_words %d)", entry->nr_words);
  1612. dump_insn_word_entries (file, "\n(words ", entry->words, ")");
  1613. lf_printf (file, "\n(word");
  1614. for (i = 0; i < entry->nr_models; i++)
  1615. lf_printf (file, " 0x%lx", (long) entry->word[i]);
  1616. lf_printf (file, ")");
  1617. dump_filter (file, "\n(field_names ", entry->field_names, ")");
  1618. lf_printf (file, "\n(format_name \"%s\")", entry->format_name);
  1619. dump_filter (file, "\n(options ", entry->options, ")");
  1620. lf_printf (file, "\n(name \"%s\")", entry->name);
  1621. lf_printf (file, "\n(nr_models %d)", entry->nr_models);
  1622. dump_insn_model_entries (file, "\n(models ", entry->models, ")");
  1623. lf_printf (file, "\n(model");
  1624. for (i = 0; i < entry->nr_models; i++)
  1625. lf_printf (file, " 0x%lx", (long) entry->model[i]);
  1626. lf_printf (file, ")");
  1627. dump_filter (file, "\n(processors ", entry->processors, ")");
  1628. dump_insn_mnemonic_entries (file, "\n(mnemonics ", entry->mnemonics,
  1629. ")");
  1630. dump_table_entry (file, "\n(code ", entry->code, ")");
  1631. lf_printf (file, "\n(next 0x%lx)", (long) entry->next);
  1632. lf_indent (file, -1);
  1633. }
  1634. lf_printf (file, "%s", suffix);
  1635. }
  1636. static void
  1637. dump_insn_entries (lf *file, char *prefix, insn_entry * entry, char *suffix)
  1638. {
  1639. lf_printf (file, "%s", prefix);
  1640. lf_indent (file, +1);
  1641. while (entry != NULL)
  1642. {
  1643. dump_insn_entry (file, "\n(", entry, ")");
  1644. entry = entry->next;
  1645. }
  1646. lf_indent (file, -1);
  1647. lf_printf (file, "%s", suffix);
  1648. }
  1649. void
  1650. dump_insn_table (lf *file, char *prefix, insn_table *isa, char *suffix)
  1651. {
  1652. lf_printf (file, "%s(insn_table *) 0x%lx", prefix, (long) isa);
  1653. if (isa != NULL)
  1654. {
  1655. lf_indent (file, +1);
  1656. dump_cache_entries (file, "\n(caches ", isa->caches, ")");
  1657. lf_printf (file, "\n(nr_insns %d)", isa->nr_insns);
  1658. lf_printf (file, "\n(max_nr_words %d)", isa->max_nr_words);
  1659. dump_insn_entries (file, "\n(insns ", isa->insns, ")");
  1660. dump_function_entries (file, "\n(functions ", isa->functions, ")");
  1661. dump_insn_entry (file, "\n(illegal_insn ", isa->illegal_insn, ")");
  1662. dump_model_table (file, "\n(model ", isa->model, ")");
  1663. dump_filter (file, "\n(flags ", isa->flags, ")");
  1664. dump_filter (file, "\n(options ", isa->options, ")");
  1665. lf_indent (file, -1);
  1666. }
  1667. lf_printf (file, "%s", suffix);
  1668. }
  1669. #ifdef MAIN
  1670. igen_options options;
  1671. int
  1672. main (int argc, char **argv)
  1673. {
  1674. insn_table *isa;
  1675. lf *l;
  1676. INIT_OPTIONS ();
  1677. if (argc == 3)
  1678. filter_parse (&options.flags_filter, argv[2]);
  1679. else if (argc != 2)
  1680. error (NULL, "Usage: insn <insn-table> [ <filter-in> ]\n");
  1681. isa = load_insn_table (argv[1], NULL);
  1682. l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-ld-insn");
  1683. dump_insn_table (l, "(isa ", isa, ")\n");
  1684. return 0;
  1685. }
  1686. #endif