genrecog.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /* Generate code from machine description to emit insns as rtl.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This program is used to produce insn-recog.c, which contains
  18. one function called `recog'. This function contains a decision tree
  19. that recognizes whether an rtx, the argument given to recog,
  20. is a valid instruction.
  21. recog returns -1 if the rtx is not valid.
  22. If the rtx is valid, recog returns a nonnegative number
  23. which is the insn code number for the pattern that matched.
  24. This is the same as the order in the machine description of the
  25. entry that matched. This number can be used as an index into
  26. insn_templates and insn_n_operands (found in insn-output.c)
  27. or as an argument to output_insn_hairy (also in insn-output.c). */
  28. #include <stdio.h>
  29. #include "rtl.h"
  30. #include <obstack.h>
  31. struct obstack obstack;
  32. struct obstack *current_obstack = &obstack;
  33. #define obstack_chunk_alloc xmalloc
  34. #define obstack_chunk_free free
  35. extern int xmalloc ();
  36. extern void free ();
  37. /* Data structure for decision tree for recognizing
  38. legitimate instructions. */
  39. struct decision
  40. {
  41. int number;
  42. char *position;
  43. RTX_CODE code;
  44. char *exact;
  45. enum machine_mode mode;
  46. char *tests;
  47. int insn_code_number;
  48. struct decision *next;
  49. struct decision *success;
  50. int opno;
  51. int dupno;
  52. int dupcount;
  53. int test_elt_zero_int;
  54. int elt_zero_int;
  55. int test_elt_one_int;
  56. int elt_one_int;
  57. int ignmode;
  58. struct decision *afterward;
  59. int label_needed;
  60. char *c_test;
  61. char *reg_class;
  62. char enforce_mode;
  63. int veclen;
  64. };
  65. /*
  66. recognize (top)
  67. {
  68. staten:
  69. x = XVECEXP (top, 0, 3);
  70. if (test_code (GET_CODE (x))
  71. && test_mode (MODE (x))
  72. && whatever_else)
  73. goto statep;
  74. else if (next one...)
  75. goto statem:
  76. goto stater;
  77. statep:
  78. actions...;
  79. return 1;
  80. statem:
  81. x = stack[depth--];
  82. more tests...;
  83. stateq:
  84. stack[++depth] = x;
  85. x = XEXP (stack[depth], 0);
  86. more tests...;
  87. stater:
  88. x = XEXP (stack[depth], 1);
  89. }
  90. */
  91. struct decision *tree;
  92. int next_number;
  93. int next_insn_code;
  94. /* Number of MATCH_DUP's seen so far in this instruction. */
  95. int dupcount;
  96. struct decision *add_to_sequence ();
  97. void add_action ();
  98. struct decision *try_merge_2 ();
  99. char *copystr ();
  100. char *concat ();
  101. void fatal ();
  102. struct decision *first;
  103. /* Construct and return a sequence of decisions
  104. that will recognize INSN. */
  105. struct decision *
  106. make_sequence (pattern)
  107. rtx pattern;
  108. {
  109. register int i;
  110. rtx x;
  111. char *c_test = XSTR (pattern, 2);
  112. struct decision *last;
  113. dupcount = 0;
  114. if (XVECLEN (pattern, 1) == 1)
  115. x = XVECEXP (pattern, 1, 0);
  116. else
  117. {
  118. x = rtx_alloc (PARALLEL);
  119. XVEC (x, 0) = XVEC (pattern, 1);
  120. PUT_MODE (x, VOIDmode);
  121. }
  122. last = add_to_sequence (x, 0, "");
  123. if (c_test[0])
  124. last->c_test = c_test;
  125. last->insn_code_number = next_insn_code++;
  126. return first;
  127. }
  128. struct decision *
  129. add_to_sequence (pattern, last, position)
  130. rtx pattern;
  131. struct decision *last;
  132. char *position;
  133. {
  134. register RTX_CODE code;
  135. register struct decision *new
  136. = (struct decision *) xmalloc (sizeof (struct decision));
  137. struct decision *this;
  138. char *newpos;
  139. register char *fmt;
  140. register int i;
  141. int depth;
  142. int len;
  143. new->number = next_number++;
  144. new->position = copystr (position);
  145. new->exact = 0;
  146. new->next = 0;
  147. new->success = 0;
  148. new->insn_code_number = -1;
  149. new->tests = 0;
  150. new->opno = -1;
  151. new->dupno = -1;
  152. new->dupcount = -1;
  153. new->test_elt_zero_int = 0;
  154. new->test_elt_one_int = 0;
  155. new->elt_zero_int = 0;
  156. new->elt_one_int = 0;
  157. new->enforce_mode = 0;
  158. new->ignmode = 0;
  159. new->afterward = 0;
  160. new->label_needed = 0;
  161. new->c_test = 0;
  162. new->reg_class = 0;
  163. new->veclen = 0;
  164. this = new;
  165. if (last == 0)
  166. first = new;
  167. else
  168. last->success = new;
  169. depth = strlen (position);
  170. newpos = (char *) alloca (depth + 2);
  171. strcpy (newpos, position);
  172. newpos[depth + 1] = 0;
  173. restart:
  174. if (pattern == 0)
  175. {
  176. new->exact = "0";
  177. new->code = UNKNOWN;
  178. new->mode = VOIDmode;
  179. return new;
  180. }
  181. switch (GET_MODE (pattern))
  182. {
  183. case 0:
  184. new->mode = VOIDmode;
  185. break;
  186. default:
  187. new->mode = GET_MODE (pattern);
  188. break;
  189. }
  190. new->code = code = GET_CODE (pattern);;
  191. switch (code)
  192. {
  193. case MATCH_OPERAND:
  194. new->opno = XINT (pattern, 0);
  195. new->code = UNKNOWN;
  196. new->tests = XSTR (pattern, 1);
  197. if (*new->tests == 0)
  198. new->tests = 0;
  199. new->reg_class = XSTR (pattern, 2);
  200. if (*new->reg_class == 0)
  201. new->reg_class = 0;
  202. return new;
  203. case MATCH_DUP:
  204. new->dupno = XINT (pattern, 0);
  205. new->dupcount = dupcount++;
  206. new->code = UNKNOWN;
  207. return new;
  208. case ADDRESS:
  209. pattern = XEXP (pattern, 0);
  210. goto restart;
  211. case PC:
  212. new->exact = "pc_rtx";
  213. return new;
  214. case CC0:
  215. new->exact = "cc0_rtx";
  216. return new;
  217. case CONST_INT:
  218. if (INTVAL (pattern) == 0)
  219. {
  220. new->exact = "const0_rtx";
  221. return new;
  222. }
  223. if (INTVAL (pattern) == 1)
  224. {
  225. new->exact = "const1_rtx";
  226. return new;
  227. }
  228. break;
  229. case SET:
  230. newpos[depth] = '0';
  231. new = add_to_sequence (SET_DEST (pattern), new, newpos);
  232. this->success->enforce_mode = 1;
  233. newpos[depth] = '1';
  234. new = add_to_sequence (SET_SRC (pattern), new, newpos);
  235. return new;
  236. case STRICT_LOW_PART:
  237. newpos[depth] = '0';
  238. new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  239. this->success->enforce_mode = 1;
  240. return new;
  241. case SUBREG:
  242. this->test_elt_one_int = 1;
  243. this->elt_one_int = XINT (pattern, 1);
  244. newpos[depth] = '0';
  245. new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  246. this->success->enforce_mode = 1;
  247. return new;
  248. case ZERO_EXTRACT:
  249. case SIGN_EXTRACT:
  250. newpos[depth] = '0';
  251. new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  252. this->success->enforce_mode = 1;
  253. newpos[depth] = '1';
  254. new = add_to_sequence (XEXP (pattern, 1), new, newpos);
  255. newpos[depth] = '2';
  256. new = add_to_sequence (XEXP (pattern, 2), new, newpos);
  257. return new;
  258. }
  259. fmt = GET_RTX_FORMAT (code);
  260. len = GET_RTX_LENGTH (code);
  261. for (i = 0; i < len; i++)
  262. {
  263. newpos[depth] = '0' + i;
  264. if (fmt[i] == 'e' || fmt[i] == 'u')
  265. new = add_to_sequence (XEXP (pattern, i), new, newpos);
  266. else if (fmt[i] == 'i' && i == 0)
  267. {
  268. this->test_elt_zero_int = 1;
  269. this->elt_zero_int = XINT (pattern, i);
  270. }
  271. else if (fmt[i] == 'i' && i == 1)
  272. {
  273. this->test_elt_one_int = 1;
  274. this->elt_one_int = XINT (pattern, i);
  275. }
  276. else if (fmt[i] == 'E')
  277. {
  278. register int j;
  279. /* We do not handle a vector appearing as other than
  280. the first item, just because nothing uses them
  281. and by handling only the special case
  282. we can use one element in newpos for either
  283. the item number of a subexpression
  284. or the element number in a vector. */
  285. if (i != 0)
  286. abort ();
  287. this->veclen = XVECLEN (pattern, i);
  288. for (j = 0; j < XVECLEN (pattern, i); j++)
  289. {
  290. newpos[depth] = 'a' + j;
  291. new = add_to_sequence (XVECEXP (pattern, i, j),
  292. new, newpos);
  293. }
  294. }
  295. else if (fmt[i] != '0')
  296. abort ();
  297. }
  298. return new;
  299. }
  300. /* Merge two decision trees OLD and ADD,
  301. modifying OLD destructively,
  302. and return the merged tree. */
  303. struct decision *
  304. merge_trees (old, add)
  305. register struct decision *old, *add;
  306. {
  307. while (add)
  308. {
  309. register struct decision *next = add->next;
  310. add->next = 0;
  311. if (!try_merge_1 (old, add))
  312. old = try_merge_2 (old, add);
  313. add = next;
  314. }
  315. return old;
  316. }
  317. /* Merge ADD into the next-chain starting with OLD
  318. only if it overlaps a condition already tested in OLD.
  319. Returns 1 if successful (OLD is modified),
  320. 0 if nothing has been done. */
  321. int
  322. try_merge_1 (old, add)
  323. register struct decision *old, *add;
  324. {
  325. while (old)
  326. {
  327. if ((old->position == add->position
  328. || (old->position && add->position
  329. && !strcmp (old->position, add->position)))
  330. && (old->tests == add->tests
  331. || (old->tests && add->tests && !strcmp (old->tests, add->tests)))
  332. && (old->c_test == add->c_test
  333. || (old->c_test && add->c_test && !strcmp (old->c_test, add->c_test)))
  334. && old->test_elt_zero_int == add->test_elt_zero_int
  335. && old->elt_zero_int == add->elt_zero_int
  336. && old->test_elt_one_int == add->test_elt_one_int
  337. && old->elt_one_int == add->elt_one_int
  338. && old->veclen == add->veclen
  339. && old->dupno == add->dupno
  340. && old->opno == add->opno
  341. && (old->tests == 0
  342. || (add->enforce_mode ? no_same_mode (old) : old->next == 0))
  343. && old->code == add->code
  344. && old->mode == add->mode)
  345. {
  346. old->success = merge_trees (old->success, add->success);
  347. if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
  348. fatal ("Two actions at one point in tree.");
  349. if (old->insn_code_number == -1)
  350. old->insn_code_number = add->insn_code_number;
  351. return 1;
  352. }
  353. old = old->next;
  354. }
  355. return 0;
  356. }
  357. /* Merge ADD into the next-chain that starts with OLD,
  358. preferably after something that tests the same place
  359. that ADD does.
  360. The next-chain of ADD itself is ignored, and it is set
  361. up for entering ADD into the new chain.
  362. Returns the new chain. */
  363. struct decision *
  364. try_merge_2 (old, add)
  365. struct decision *old, *add;
  366. {
  367. register struct decision *p = old, *last = 0, *prev_win = 0;
  368. /* Put this in after the others that test the same place,
  369. if there are any. If not, find the last chain element
  370. and insert there.
  371. One modification: if this one is NOT a MATCH_OPERAND,
  372. put it before any MATCH_OPERANDS that test the same place.
  373. Another: if enforce_mode (i.e. this is first operand of a SET),
  374. put this after the last thing that tests the same place for
  375. the same mode. */
  376. int operand = 0 != add->tests;
  377. while (p)
  378. {
  379. if (p->position == add->position
  380. || (p->position && add->position
  381. && !strcmp (p->position, add->position)))
  382. {
  383. if (!operand && p->tests)
  384. break;
  385. if (prev_win && prev_win->enforce_mode
  386. && prev_win->mode == add->mode
  387. && p->mode != add->mode)
  388. {
  389. add->next = prev_win->next;
  390. prev_win->next = add;
  391. return old;
  392. }
  393. prev_win = p;
  394. }
  395. else if (prev_win)
  396. {
  397. add->next = prev_win->next;
  398. prev_win->next = add;
  399. return old;
  400. }
  401. last = p;
  402. p = p->next;
  403. }
  404. /* If we leave the loop, p is place to insert before. */
  405. if (last)
  406. {
  407. add->next = last->next;
  408. last->next = add;
  409. return old;
  410. }
  411. add->next = old;
  412. return add;
  413. }
  414. no_same_mode (node)
  415. struct decision *node;
  416. {
  417. register struct decision *p;
  418. register enum machine_mode mode = node->mode;
  419. for (p = node->next; p; p = p->next)
  420. if (p->mode == mode)
  421. return 0;
  422. return 1;
  423. }
  424. /* Write out C code to perform the decisions in the tree. */
  425. write_tree (tree, prevpos, afterward, afterpos)
  426. struct decision *tree;
  427. char *prevpos;
  428. int afterward;
  429. char *afterpos;
  430. {
  431. register struct decision *p, *p1;
  432. char *pos;
  433. register int depth;
  434. int ignmode;
  435. enum { NO_SWITCH, CODE_SWITCH, MODE_SWITCH } in_switch = NO_SWITCH;
  436. char modemap[NUM_MACHINE_MODE];
  437. char codemap[NUM_RTX_CODE];
  438. pos = prevpos;
  439. tree->label_needed = 1;
  440. for (p = tree; p; p = p->next)
  441. {
  442. /* Find the next alternative to p
  443. that might be true when p is true.
  444. Test that one next if p's successors fail.
  445. Note that when the `tests' field is nonzero
  446. it is up to the specified test-function to compare machine modes
  447. and some (such as general_operand) don't always do so.
  448. But when inside a switch-on-modes we ignore this and
  449. consider all modes mutually exclusive. */
  450. for (p1 = p->next; p1; p1 = p1->next)
  451. if (((p->code == UNKNOWN || p1->code == UNKNOWN || p->code == p1->code)
  452. && (p->mode == VOIDmode || p1->mode == VOIDmode
  453. || p->mode == p1->mode
  454. || (in_switch != MODE_SWITCH && (p->tests || p1->tests))))
  455. || strcmp (p1->position, p->position))
  456. break;
  457. p->afterward = p1;
  458. if (p1) p1->label_needed = 1;
  459. if (in_switch == MODE_SWITCH && (p->mode == VOIDmode || p->tests != 0))
  460. {
  461. in_switch = NO_SWITCH;
  462. printf (" }\n");
  463. }
  464. if (in_switch == CODE_SWITCH && p->code == UNKNOWN)
  465. {
  466. in_switch = NO_SWITCH;
  467. printf (" }\n");
  468. }
  469. if (p->label_needed)
  470. printf (" L%d:\n", p->number);
  471. if (p->success == 0 && p->insn_code_number < 0)
  472. abort ();
  473. change_state (pos, p->position);
  474. pos = p->position;
  475. depth = strlen (pos);
  476. ignmode = p->ignmode || pos[depth - 1] == '*' || p->tests;
  477. if (in_switch == NO_SWITCH)
  478. {
  479. /* If p and its alternatives all want the same mode,
  480. reject all others at once, first, then ignore the mode. */
  481. if (!ignmode && p->mode != VOIDmode && p->next && same_modes (p, p->mode))
  482. {
  483. printf (" if (GET_MODE (x%d) != %smode)\n",
  484. depth, GET_MODE_NAME (p->mode));
  485. if (afterward)
  486. {
  487. printf (" {\n ");
  488. change_state (pos, afterpos);
  489. printf (" goto L%d;\n }\n", afterward);
  490. }
  491. else
  492. printf (" goto ret0;\n");
  493. clear_modes (p);
  494. ignmode = 1;
  495. }
  496. /* If p and its alternatives all want the same code,
  497. reject all others at once, first, then ignore the code. */
  498. if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
  499. {
  500. printf (" if (GET_CODE (x%d) != ", depth);
  501. print_code (p->code);
  502. printf (")\n");
  503. if (afterward)
  504. {
  505. printf (" {");
  506. change_state (pos, afterpos);
  507. printf (" goto L%d; }\n", afterward);
  508. }
  509. else
  510. printf (" goto ret0;\n");
  511. clear_codes (p);
  512. }
  513. }
  514. /* If p and its alternatives all have different modes
  515. and there are at least 4 of them, make a switch. */
  516. if (in_switch == NO_SWITCH && pos[depth-1] != '*')
  517. {
  518. register int i;
  519. bzero (modemap, sizeof modemap);
  520. for (p1 = p, i = 0; p1 && p1->mode != VOIDmode && p1->tests == 0;
  521. p1 = p1->next, i++)
  522. {
  523. if (! p->enforce_mode && modemap[(int) p1->mode])
  524. break;
  525. modemap[(int) p1->mode] = 1;
  526. }
  527. if ((p1 == 0 || p1->mode == VOIDmode || p1->tests != 0) && i >= 4)
  528. {
  529. in_switch = MODE_SWITCH;
  530. printf (" switch (GET_MODE (x%d))\n {\n", depth);
  531. }
  532. }
  533. if (in_switch == NO_SWITCH)
  534. {
  535. register int i;
  536. bzero (codemap, sizeof codemap);
  537. for (p1 = p, i = 0; p1 && p1->code != UNKNOWN; p1 = p1->next, i++)
  538. {
  539. if (codemap[(int) p1->code])
  540. break;
  541. codemap[(int) p1->code] = 1;
  542. }
  543. if ((p1 == 0 || p1->code == UNKNOWN) && i >= 4)
  544. {
  545. in_switch = CODE_SWITCH;
  546. printf (" switch (GET_CODE (x%d))\n {\n", depth);
  547. }
  548. }
  549. if (in_switch == MODE_SWITCH)
  550. {
  551. if (modemap[(int) p->mode])
  552. {
  553. printf (" case %smode:\n", GET_MODE_NAME (p->mode));
  554. modemap[(int) p->mode] = 0;
  555. }
  556. }
  557. if (in_switch == CODE_SWITCH)
  558. {
  559. if (codemap[(int) p->code])
  560. {
  561. printf (" case ");
  562. print_code (p->code);
  563. printf (":\n");
  564. codemap[(int) p->code] = 0;
  565. }
  566. }
  567. printf (" if (");
  568. if (p->code != UNKNOWN && (p->exact || in_switch != CODE_SWITCH))
  569. {
  570. register char *p1;
  571. if (p->exact)
  572. printf ("x%d == %s", depth, p->exact);
  573. else
  574. {
  575. printf ("GET_CODE (x%d) == ", depth);
  576. print_code (p->code);
  577. }
  578. printf (" && ");
  579. }
  580. if (p->mode && !ignmode && in_switch != MODE_SWITCH)
  581. printf ("GET_MODE (x%d) == %smode && ",
  582. depth, GET_MODE_NAME (p->mode));
  583. if (p->test_elt_zero_int)
  584. printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
  585. if (p->veclen)
  586. printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
  587. if (p->test_elt_one_int)
  588. printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
  589. if (p->dupno >= 0)
  590. printf ("rtx_equal_p (x%d, recog_operand[%d]) && ", depth, p->dupno);
  591. if (p->tests)
  592. printf ("%s (x%d, %smode)", p->tests, depth,
  593. GET_MODE_NAME (p->mode));
  594. else
  595. printf ("1");
  596. if (p->opno >= 0)
  597. printf (")\n { recog_operand[%d] = x%d; ",
  598. p->opno, depth);
  599. else
  600. printf (")\n ");
  601. if (p->c_test)
  602. printf ("if (%s) ", p->c_test);
  603. if (p->insn_code_number >= 0)
  604. printf ("return %d;", p->insn_code_number);
  605. else
  606. printf ("goto L%d;", p->success->number);
  607. if (p->opno >= 0)
  608. printf (" }\n");
  609. else
  610. printf ("\n");
  611. /* Now, if inside a switch, branch to next switch member
  612. that might also need to be tested if this one fails. */
  613. if (in_switch == CODE_SWITCH)
  614. {
  615. /* Find the next alternative to p
  616. that might be applicable if p was applicable. */
  617. for (p1 = p->next; p1; p1 = p1->next)
  618. if (p1->code == UNKNOWN || p->code == p1->code)
  619. break;
  620. if (p1 == 0 || p1->code == UNKNOWN)
  621. printf (" break;\n");
  622. else if (p1 != p->next)
  623. {
  624. printf (" goto L%d;\n", p1->number);
  625. p1->label_needed = 1;
  626. }
  627. }
  628. if (in_switch == MODE_SWITCH)
  629. {
  630. /* Find the next alternative to p
  631. that might be applicable if p was applicable. */
  632. for (p1 = p->next; p1; p1 = p1->next)
  633. if (p1->mode == VOIDmode || p->mode == p1->mode)
  634. break;
  635. if (p1 == 0 || p1->mode == VOIDmode)
  636. printf (" break;\n");
  637. else if (p1 != p->next)
  638. {
  639. printf (" goto L%d;\n", p1->number);
  640. p1->label_needed = 1;
  641. }
  642. }
  643. }
  644. if (in_switch != NO_SWITCH)
  645. printf (" }\n");
  646. if (afterward)
  647. {
  648. change_state (pos, afterpos);
  649. printf (" goto L%d;\n", afterward);
  650. }
  651. else
  652. printf (" goto ret0;\n");
  653. for (p = tree; p; p = p->next)
  654. if (p->success)
  655. {
  656. pos = p->position;
  657. write_tree (p->success, pos,
  658. p->afterward ? p->afterward->number : afterward,
  659. p->afterward ? pos : afterpos);
  660. }
  661. }
  662. print_code (code)
  663. RTX_CODE code;
  664. {
  665. register char *p1;
  666. for (p1 = GET_RTX_NAME (code); *p1; p1++)
  667. {
  668. if (*p1 >= 'a' && *p1 <= 'z')
  669. putchar (*p1 + 'A' - 'a');
  670. else
  671. putchar (*p1);
  672. }
  673. }
  674. same_codes (p, code)
  675. register struct decision *p;
  676. register RTX_CODE code;
  677. {
  678. for (; p; p = p->next)
  679. if (p->code != code)
  680. return 0;
  681. return 1;
  682. }
  683. clear_codes (p)
  684. register struct decision *p;
  685. {
  686. for (; p; p = p->next)
  687. p->code = UNKNOWN;
  688. }
  689. same_modes (p, mode)
  690. register struct decision *p;
  691. register enum machine_mode mode;
  692. {
  693. for (; p; p = p->next)
  694. if (p->mode != mode || p->tests)
  695. return 0;
  696. return 1;
  697. }
  698. clear_modes (p)
  699. register struct decision *p;
  700. {
  701. for (; p; p = p->next)
  702. p->ignmode = 1;
  703. }
  704. change_state (oldpos, newpos)
  705. char *oldpos;
  706. char *newpos;
  707. {
  708. int odepth = strlen (oldpos);
  709. int depth = odepth;
  710. int ndepth = strlen (newpos);
  711. /* Pop up as many levels as necessary. */
  712. while (strncmp (oldpos, newpos, depth))
  713. --depth;
  714. /* Go down to desired level. */
  715. while (depth < ndepth)
  716. {
  717. if (newpos[depth] == '*')
  718. printf (" x%d = recog_addr_dummy;\n XEXP (x%d, 0) = x%d;\n",
  719. depth + 1, depth + 1, depth);
  720. else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
  721. printf (" x%d = XVECEXP (x%d, 0, %c);\n",
  722. depth + 1, depth, '0' + newpos[depth] - 'a');
  723. else
  724. printf (" x%d = XEXP (x%d, %c);\n",
  725. depth + 1, depth, newpos[depth]);
  726. ++depth;
  727. }
  728. }
  729. char *
  730. copystr (s1)
  731. char *s1;
  732. {
  733. register char *tem;
  734. if (s1 == 0)
  735. return 0;
  736. tem = (char *) xmalloc (strlen (s1) + 1);
  737. strcpy (tem, s1);
  738. return tem;
  739. }
  740. char *
  741. concat (s1, s2)
  742. char *s1, *s2;
  743. {
  744. register char *tem;
  745. if (s1 == 0)
  746. return s2;
  747. if (s2 == 0)
  748. return s1;
  749. tem = (char *) xmalloc (strlen (s1) + strlen (s2) + 2);
  750. strcpy (tem, s1);
  751. strcat (tem, " ");
  752. strcat (tem, s2);
  753. return tem;
  754. }
  755. int
  756. xrealloc (ptr, size)
  757. char *ptr;
  758. int size;
  759. {
  760. int result = realloc (ptr, size);
  761. if (!result)
  762. abort ();
  763. return result;
  764. }
  765. xmalloc (size)
  766. {
  767. register int val = malloc (size);
  768. if (val == 0)
  769. abort ();
  770. return val;
  771. }
  772. void
  773. fatal (s, a1, a2)
  774. {
  775. fprintf (stderr, "genrecog: ");
  776. fprintf (stderr, s, a1, a2);
  777. fprintf (stderr, "\n");
  778. fprintf (stderr, "after %d instruction definitions\n",
  779. next_insn_code);
  780. exit (1);
  781. }
  782. main (argc, argv)
  783. int argc;
  784. char **argv;
  785. {
  786. rtx desc;
  787. struct decision *tree = 0;
  788. FILE *infile;
  789. extern rtx read_rtx ();
  790. char *startpos;
  791. register int c;
  792. obstack_begin (current_obstack, 4060);
  793. if (argc <= 1)
  794. fatal ("No input file name.");
  795. infile = fopen (argv[1], "r");
  796. if (infile == 0)
  797. {
  798. perror (argv[1]);
  799. exit (1);
  800. }
  801. init_rtl ();
  802. next_insn_code = 0;
  803. printf ("/* Generated automatically by the program `genrecog'\n\
  804. from the machine description file `md'. */\n\n");
  805. /* Read the machine description. */
  806. while (1)
  807. {
  808. c = read_skip_spaces (infile);
  809. if (c == EOF)
  810. break;
  811. ungetc (c, infile);
  812. desc = read_rtx (infile);
  813. tree = merge_trees (tree, make_sequence (desc));
  814. }
  815. printf ("#include \"config.h\"\n");
  816. printf ("#include \"rtl.h\"\n");
  817. printf ("#include \"insn-config.h\"\n");
  818. printf ("#include \"recog.h\"\n");
  819. printf ("\n\
  820. /* `recog' contains a decision tree\n\
  821. that recognizes whether the rtx X0 is a valid instruction.\n\
  822. \n\
  823. recog returns -1 if the rtx is not valid.\n\
  824. If the rtx is valid, recog returns a nonnegative number\n\
  825. which is the insn code number for the pattern that matched.\n\
  826. This is the same as the order in the machine description of the\n\
  827. entry that matched. This number can be used as an index into\n\
  828. insn_templates and insn_n_operands (found in insn-output.c)\n\
  829. or as an argument to output_insn_hairy (also in insn-output.c). */\n\n");
  830. printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
  831. printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
  832. printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
  833. printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
  834. printf ("extern rtx recog_addr_dummy;\n\n");
  835. printf ("#define operands recog_operand\n\n");
  836. printf ("int\nrecog (x0, insn)\n register rtx x0;\n rtx insn;{\n");
  837. printf (" register rtx x1, x2, x3, x4, x5;\n rtx x6, x7, x8, x9, x10, x11;\n");
  838. startpos = "";
  839. write_tree (tree, startpos, 0, "");
  840. printf (" ret0: return -1;\n}\n");
  841. return 0;
  842. }