genrecog.c 25 KB

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