rtl.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /* Manage RTL for C-Compiler
  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 file contains the low level primitives for allocating,
  18. printing and reading rtl expressions and vectors.
  19. It also contains some functions for semantic analysis
  20. on rtl expressions. */
  21. #include "config.h"
  22. #include <ctype.h>
  23. #include <stdio.h>
  24. #include "rtl.h"
  25. #include "obstack.h"
  26. #define obstack_chunk_alloc xmalloc
  27. #define obstack_chunk_free free
  28. extern int xmalloc ();
  29. extern void free ();
  30. /* Obstack used for allocating RTL objects.
  31. Between functions, this is the permanent_obstack.
  32. While parsing and expanding a function, this is maybepermanent_obstack
  33. so we can save it if it is an inline function.
  34. During optimization and output, this is temporary_obstack. */
  35. extern struct obstack *rtl_obstack;
  36. #define MIN(x,y) ((x < y) ? x : y)
  37. extern long ftell();
  38. /* Indexed by rtx code, gives number of operands for an rtx with that code.
  39. Does NOT include rtx header data (code and links).
  40. This array is initialized in init_rtx. */
  41. int rtx_length[NUM_RTX_CODE + 1];
  42. /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
  43. #define DEF_RTL_EXPR(ENUM, NAME, FORMAT) NAME ,
  44. char *rtx_name[] = {
  45. #include "rtl.def" /* rtl expressions are documented here */
  46. };
  47. #undef DEF_RTL_EXPR
  48. /* Indexed by machine mode, gives the name of that machine mode.
  49. This name does not include the letters "mode". */
  50. #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT) NAME,
  51. char *mode_name[] = {
  52. #include "machmode.def"
  53. };
  54. #undef DEF_MACHMODE
  55. /* Indexed by machine mode, gives the length of the mode, in bytes.
  56. GET_MODE_CLASS uses this. */
  57. #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT) CLASS,
  58. enum mode_class mode_class[] = {
  59. #include "machmode.def"
  60. };
  61. #undef DEF_MACHMODE
  62. /* Indexed by machine mode, gives the length of the mode, in bytes.
  63. GET_MODE_SIZE uses this. */
  64. #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT) SIZE,
  65. int mode_size[] = {
  66. #include "machmode.def"
  67. };
  68. #undef DEF_MACHMODE
  69. /* Indexed by machine mode, gives the length of the mode's subunit.
  70. GET_MODE_UNIT_SIZE uses this. */
  71. #define DEF_MACHMODE(SYM, NAME, CLASS, SIZE, UNIT) UNIT,
  72. int mode_unit_size[] = {
  73. #include "machmode.def" /* machine modes are documented here */
  74. };
  75. #undef DEF_MACHMODE
  76. /* Indexed by rtx code, gives a sequence of operand-types for
  77. rtx's of that code. The sequence is a C string in which
  78. each charcter describes one operand. */
  79. char *rtx_format[] = {
  80. /* "*" undefined.
  81. can cause a warning message
  82. "0" field is unused (or used in a phase-dependent manner)
  83. prints nothing
  84. "i" an integer
  85. prints the integer
  86. "s" a pointer to a string
  87. prints the string
  88. "e" a pointer to an rtl expression
  89. prints the expression
  90. "E" a pointer to a vector that points to a number of rtl expressions
  91. prints a list of the rtl expressions
  92. "u" a pointer to another insn
  93. prints the uid of the insn. */
  94. #define DEF_RTL_EXPR(ENUM, NAME, FORMAT) FORMAT ,
  95. #include "rtl.def" /* rtl expressions are defined here */
  96. #undef DEF_RTL_EXPR
  97. };
  98. /* Allocate an rtx vector of N elements.
  99. Store the length, and initialize all elements to zero. */
  100. rtvec
  101. rtvec_alloc (n)
  102. int n;
  103. {
  104. rtvec rt;
  105. int i;
  106. rt = (rtvec) obstack_alloc (rtl_obstack,
  107. sizeof (struct rtvec_def)
  108. + (( n - 1) * sizeof (rtunion)));
  109. /* clear out the vector */
  110. PUT_NUM_ELEM(rt, n);
  111. for (i=0; i < n; i++)
  112. rt->elem[i].rtvec = NULL; /* @@ not portable due to rtunion */
  113. return rt;
  114. }
  115. /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
  116. all the rest is initialized to zero. */
  117. rtx
  118. rtx_alloc (code)
  119. RTX_CODE code;
  120. {
  121. rtx rt;
  122. register int nelts = GET_RTX_LENGTH (code);
  123. register int length = sizeof (struct rtx_def)
  124. + (nelts - 1) * sizeof (rtunion);
  125. rt = (rtx) obstack_alloc (rtl_obstack, length);
  126. * (int *) rt = 0;
  127. PUT_CODE (rt, code);
  128. return rt;
  129. }
  130. /* Create a new copy of an rtx.
  131. Recursively copies the operands of the rtx,
  132. except for those few rtx codes that are sharable. */
  133. rtx
  134. copy_rtx (orig)
  135. register rtx orig;
  136. {
  137. register rtx copy;
  138. register int i, j;
  139. register RTX_CODE code;
  140. register char *format_ptr;
  141. code = GET_CODE (orig);
  142. switch (code)
  143. {
  144. case REG:
  145. case QUEUED:
  146. case CONST_INT:
  147. case CONST_DOUBLE:
  148. case SYMBOL_REF:
  149. case CODE_LABEL:
  150. case PC:
  151. case CC0:
  152. return orig;
  153. }
  154. copy = rtx_alloc (code);
  155. PUT_MODE (copy, GET_MODE (orig));
  156. copy->in_struct = orig->in_struct;
  157. copy->volatil = orig->volatil;
  158. copy->unchanging = orig->unchanging;
  159. copy->integrated = orig->integrated;
  160. format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
  161. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
  162. {
  163. switch (*format_ptr++)
  164. {
  165. case 'e':
  166. XEXP (copy, i) = copy_rtx (XEXP (orig, i));
  167. break;
  168. case 'E':
  169. XVEC (copy, i) = XVEC (orig, i);
  170. if (XVEC (orig, i) != NULL)
  171. {
  172. XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
  173. for (j = 0; j < XVECLEN (copy, i); j++)
  174. XVECEXP (copy, i, j) = copy_rtx (XVECEXP (orig, i, j));
  175. }
  176. break;
  177. default:
  178. XINT (copy, i) = XINT (orig, i);
  179. break;
  180. }
  181. }
  182. return copy;
  183. }
  184. /* Return 1 if the value of X is unstable
  185. (would be different at a different point in the program).
  186. The frame pointer, arg pointer, etc. are considered stable
  187. (within one function) and so is anything marked `unchanging'. */
  188. int
  189. rtx_unstable_p (x)
  190. rtx x;
  191. {
  192. register RTX_CODE code = GET_CODE (x);
  193. register int i;
  194. register char *fmt;
  195. if (code == MEM)
  196. return ! x->unchanging;
  197. if (code == QUEUED)
  198. return 1;
  199. if (code == CONST || code == CONST_INT)
  200. return 0;
  201. if (code == REG)
  202. return ! (REGNO (x) == FRAME_POINTER_REGNUM
  203. || REGNO (x) == ARG_POINTER_REGNUM
  204. || x->unchanging);
  205. fmt = GET_RTX_FORMAT (code);
  206. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  207. if (fmt[i] == 'e')
  208. if (rtx_unstable_p (XEXP (x, i)))
  209. return 1;
  210. return 0;
  211. }
  212. /* Return 1 if X has a value that can vary even between two
  213. executions of the program. 0 means X can be compared reliably
  214. against certain constants or near-constants.
  215. The frame pointer and the arg pointer are considered constant. */
  216. int
  217. rtx_varies_p (x)
  218. rtx x;
  219. {
  220. register RTX_CODE code = GET_CODE (x);
  221. register int i;
  222. register char *fmt;
  223. if (code == MEM)
  224. return 1;
  225. if (code == QUEUED)
  226. return 1;
  227. if (code == CONST || code == CONST_INT)
  228. return 0;
  229. if (code == REG)
  230. return ! (REGNO (x) == FRAME_POINTER_REGNUM
  231. || REGNO (x) == ARG_POINTER_REGNUM);
  232. fmt = GET_RTX_FORMAT (code);
  233. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  234. if (fmt[i] == 'e')
  235. if (rtx_varies_p (XEXP (x, i)))
  236. return 1;
  237. return 0;
  238. }
  239. /* Return 1 if X refers to a memory location whose address
  240. cannot be compared reliably with constant addresses,
  241. or if X refers to a BLKmode memory object. */
  242. int
  243. rtx_addr_varies_p (x)
  244. rtx x;
  245. {
  246. register RTX_CODE code = GET_CODE (x);
  247. register int i;
  248. register char *fmt;
  249. if (code == MEM)
  250. return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
  251. fmt = GET_RTX_FORMAT (code);
  252. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  253. if (fmt[i] == 'e')
  254. if (rtx_addr_varies_p (XEXP (x, i)))
  255. return 1;
  256. return 0;
  257. }
  258. /* Nonzero if register REG appears somewhere within IN.
  259. Also works if REG is not a register; in this case it checks
  260. for a subexpression of IN that is Lisp "equal" to REG. */
  261. int
  262. reg_mentioned_p (reg, in)
  263. register rtx reg, in;
  264. {
  265. register char *fmt;
  266. register int i;
  267. register enum rtx_code code;
  268. if (in == 0)
  269. return 0;
  270. if (reg == in)
  271. return 1;
  272. code = GET_CODE (in);
  273. switch (code)
  274. {
  275. /* Compare registers by number. */
  276. case REG:
  277. return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
  278. /* These codes have no constituent expressions
  279. and are unique. */
  280. case CC0:
  281. case PC:
  282. case CONST_INT:
  283. case CONST:
  284. case CONST_DOUBLE:
  285. case LABEL_REF:
  286. case SYMBOL_REF:
  287. return 0;
  288. }
  289. if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
  290. return 1;
  291. fmt = GET_RTX_FORMAT (code);
  292. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  293. {
  294. if (fmt[i] == 'E')
  295. {
  296. register int j;
  297. for (j = XVECLEN (in, i) - 1; j >= 0; j--)
  298. if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
  299. return 1;
  300. }
  301. else if (fmt[i] == 'e'
  302. && reg_mentioned_p (reg, XEXP (in, i)))
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. /* Nonzero if register REG is used in an insn between
  308. FROM_INSN and TO_INSN (exclusive of those two). */
  309. int
  310. reg_used_between_p (reg, from_insn, to_insn)
  311. rtx reg, from_insn, to_insn;
  312. {
  313. register rtx insn;
  314. register RTX_CODE code;
  315. for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
  316. if (((code = GET_CODE (insn)) == INSN
  317. || code == JUMP_INSN || code == CALL_INSN)
  318. && reg_mentioned_p (reg, PATTERN (insn)))
  319. return 1;
  320. return 0;
  321. }
  322. /* Return 1 if X and Y are identical-looking rtx's.
  323. This is the Lisp function EQUAL for rtx arguments. */
  324. int
  325. rtx_equal_p (x, y)
  326. rtx x, y;
  327. {
  328. register int i;
  329. register int hash = 0;
  330. register RTX_CODE code = GET_CODE (x);
  331. register char *fmt;
  332. if (x == y)
  333. return 1;
  334. /* Rtx's of different codes cannot be equal. */
  335. if (code != GET_CODE (y))
  336. return 0;
  337. /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
  338. (REG:SI x) and (REG:HI x) are NOT equivalent. */
  339. if (GET_MODE (x) != GET_MODE (y))
  340. return 0;
  341. /* These three types of rtx's can be compared nonrecursively. */
  342. if (code == REG)
  343. return (REGNO (x) == REGNO (y));
  344. if (code == LABEL_REF)
  345. return XEXP (x, 0) == XEXP (y, 0);
  346. if (code == SYMBOL_REF)
  347. return XSTR (x, 0) == XSTR (y, 0);
  348. /* Compare the elements. If any pair of corresponding elements
  349. fail to match, return 0 for the whole things. */
  350. fmt = GET_RTX_FORMAT (code);
  351. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  352. {
  353. switch (fmt[i])
  354. {
  355. case 'i':
  356. if (XINT (x, i) != XINT (y, i))
  357. return 0;
  358. break;
  359. case 'e':
  360. if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
  361. return 0;
  362. break;
  363. case 's':
  364. if (strcmp (XSTR (x, i), XSTR (y, i)))
  365. return 0;
  366. break;
  367. case '0':
  368. break;
  369. /* It is believed that rtx's at this level will never
  370. contain anything but integers and other rtx's,
  371. except for within LABEL_REFs and SYMBOL_REFs. */
  372. default:
  373. abort ();
  374. }
  375. }
  376. return 1;
  377. }
  378. /* Call FUN on each register or MEM that is stored into or clobbered by X.
  379. (X would be the pattern of an insn).
  380. FUN receives two arguments:
  381. the REG, MEM, CC0 or PC being stored in or clobbered,
  382. the SET or CLOBBER rtx that does the store. */
  383. void
  384. note_stores (x, fun)
  385. register rtx x;
  386. void (*fun) ();
  387. {
  388. if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
  389. {
  390. register rtx dest = SET_DEST (x);
  391. while (GET_CODE (dest) == SUBREG
  392. || GET_CODE (dest) == ZERO_EXTRACT
  393. || GET_CODE (dest) == SIGN_EXTRACT
  394. || GET_CODE (dest) == STRICT_LOW_PART)
  395. dest = XEXP (dest, 0);
  396. (*fun) (dest, GET_CODE (x) == CLOBBER);
  397. }
  398. else if (GET_CODE (x) == PARALLEL)
  399. {
  400. register int i;
  401. for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  402. {
  403. register rtx y = XVECEXP (x, 0, i);
  404. if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
  405. {
  406. register rtx dest = SET_DEST (y);
  407. while (GET_CODE (dest) == SUBREG
  408. || GET_CODE (dest) == ZERO_EXTRACT
  409. || GET_CODE (dest) == SIGN_EXTRACT
  410. || GET_CODE (dest) == STRICT_LOW_PART)
  411. dest = XEXP (dest, 0);
  412. (*fun) (dest, GET_CODE (y) == CLOBBER);
  413. }
  414. }
  415. }
  416. }
  417. /* Return nonzero if register REG's old contents don't survive after INSN.
  418. This can be because REG dies in INSN or because INSN entirely sets REG.
  419. "Entirely set" means set directly and not through a SUBREG,
  420. ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
  421. REG may be a hard or pseudo reg. Renumbering is not taken into account,
  422. but for this use that makes no difference, since regs don't overlap
  423. during their lifetimes. Therefore, this function may be used
  424. at any time after deaths have been computed (in flow.c). */
  425. int
  426. dead_or_set_p (insn, reg)
  427. rtx insn;
  428. rtx reg;
  429. {
  430. register rtx link;
  431. register int regno = REGNO (reg);
  432. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  433. if ((REG_NOTE_KIND (link) == REG_DEAD
  434. || REG_NOTE_KIND (link) == REG_INC)
  435. && REGNO (XEXP (link, 0)) == regno)
  436. return 1;
  437. if (GET_CODE (PATTERN (insn)) == SET)
  438. return SET_DEST (PATTERN (insn)) == reg;
  439. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  440. {
  441. register int i;
  442. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  443. {
  444. if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
  445. && SET_DEST (XVECEXP (PATTERN (insn), 0, i)) == reg)
  446. return 1;
  447. }
  448. }
  449. return 0;
  450. }
  451. /* Return the reg-note of kind KIND in insn INSN, if there is one.
  452. If DATUM is nonzero, look for one whose datum is DATUM. */
  453. rtx
  454. find_reg_note (insn, kind, datum)
  455. rtx insn;
  456. enum reg_note kind;
  457. rtx datum;
  458. {
  459. register rtx link;
  460. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  461. if (REG_NOTE_KIND (link) == kind
  462. && (datum == 0 || datum == XEXP (link, 0)))
  463. return link;
  464. return 0;
  465. }
  466. /* Return the reg-note of kind KIND in insn INSN which applies to register
  467. number REGNO, if any. Return 0 if there is no such reg-note. */
  468. rtx
  469. find_regno_note (insn, kind, regno)
  470. rtx insn;
  471. enum reg_note kind;
  472. int regno;
  473. {
  474. register rtx link;
  475. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  476. if (REG_NOTE_KIND (link) == kind
  477. && REGNO (XEXP (link, 0)) == regno)
  478. return link;
  479. return 0;
  480. }
  481. /* Printing rtl for debugging dumps. */
  482. static FILE *outfile;
  483. char spaces[] = " ";
  484. static int sawclose = 0;
  485. /* Print IN_RTX onto OUTFILE. This is the recursive part of printing. */
  486. static void
  487. print_rtx (in_rtx)
  488. register rtx in_rtx;
  489. {
  490. static int indent;
  491. register int i, j;
  492. register char *format_ptr;
  493. if (sawclose)
  494. {
  495. fprintf (outfile, "\n%s",
  496. (spaces + (sizeof spaces - indent * 2)));
  497. sawclose = 0;
  498. }
  499. if (in_rtx == 0)
  500. {
  501. fprintf (outfile, "(nil)");
  502. sawclose = 1;
  503. return;
  504. }
  505. /* print name of expression code */
  506. fprintf (outfile, "(%s", GET_RTX_NAME (GET_CODE (in_rtx)));
  507. if (in_rtx->in_struct)
  508. fprintf (outfile, "/s");
  509. if (in_rtx->volatil)
  510. fprintf (outfile, "/v");
  511. if (in_rtx->unchanging)
  512. fprintf (outfile, "/u");
  513. if (in_rtx->integrated)
  514. fprintf (outfile, "/i");
  515. if (GET_MODE (in_rtx) != VOIDmode)
  516. fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
  517. format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx));
  518. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
  519. switch (*format_ptr++)
  520. {
  521. case 's':
  522. if (XSTR (in_rtx, i) == 0)
  523. fprintf (outfile, " \"\"");
  524. else
  525. fprintf (outfile, " (\"%s\")", XSTR (in_rtx, i));
  526. sawclose = 1;
  527. break;
  528. /* 0 indicates a field for internal use that should not be printed. */
  529. case '0':
  530. break;
  531. case 'e':
  532. indent += 2;
  533. if (!sawclose)
  534. fprintf (outfile, " ");
  535. print_rtx (XEXP (in_rtx, i));
  536. indent -= 2;
  537. break;
  538. case 'E':
  539. indent += 2;
  540. if (sawclose)
  541. {
  542. fprintf (outfile, "\n%s",
  543. (spaces + (sizeof spaces - indent * 2)));
  544. sawclose = 0;
  545. }
  546. fprintf (outfile, "[ ");
  547. if (NULL != XVEC (in_rtx, i))
  548. {
  549. indent += 2;
  550. if (XVECLEN (in_rtx, i))
  551. sawclose = 1;
  552. for (j = 0; j < XVECLEN (in_rtx, i); j++)
  553. print_rtx (XVECEXP (in_rtx, i, j));
  554. indent -= 2;
  555. }
  556. if (sawclose)
  557. fprintf (outfile, "\n%s",
  558. (spaces + (sizeof spaces - indent * 2)));
  559. fprintf (outfile, "] ");
  560. sawclose = 1;
  561. indent -= 2;
  562. break;
  563. case 'i':
  564. fprintf (outfile, " %d", XINT (in_rtx, i));
  565. sawclose = 0;
  566. break;
  567. case 'u':
  568. if (XEXP (in_rtx, i) != NULL)
  569. fprintf(outfile, " %d", INSN_UID (XEXP (in_rtx, i)));
  570. else
  571. fprintf(outfile, " 0");
  572. sawclose = 0;
  573. break;
  574. default:
  575. fprintf (stderr,
  576. "switch format wrong in rtl.print_rtx(). format was: %c.\n",
  577. format_ptr[-1]);
  578. abort ();
  579. }
  580. fprintf (outfile, ")");
  581. sawclose = 1;
  582. }
  583. /* Call this function from the debugger to see what X looks like. */
  584. void
  585. debug_rtx (x)
  586. rtx x;
  587. {
  588. outfile = stderr;
  589. print_rtx (x);
  590. fprintf (stderr, "\n");
  591. }
  592. /* External entry point for printing a chain of INSNs
  593. starting with RTX_FIRST onto file OUTF. */
  594. void
  595. print_rtl (outf, rtx_first)
  596. FILE *outf;
  597. rtx rtx_first;
  598. {
  599. register rtx tmp_rtx;
  600. outfile = outf;
  601. sawclose = 0;
  602. for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
  603. {
  604. print_rtx (tmp_rtx);
  605. fprintf (outfile, "\n");
  606. }
  607. }
  608. /* Subroutines of read_rtx. */
  609. /* Dump code after printing a message. Used when read_rtx finds
  610. invalid data. */
  611. static void
  612. dump_and_abort (expected_c, actual_c, infile)
  613. int expected_c, actual_c;
  614. FILE *infile;
  615. {
  616. int c, i;
  617. fprintf (stderr,
  618. "Expected character %c. Read character %c. At file position: %ld\n",
  619. expected_c, actual_c, ftell (infile));
  620. fprintf (stderr, "Following characters are:\n\t");
  621. for (i = 0; i < 200; i++)
  622. {
  623. c = getc (infile);
  624. if (EOF == c) break;
  625. putc (c, stderr);
  626. }
  627. fprintf (stderr, "Aborting.\n");
  628. abort ();
  629. }
  630. /* Read chars from INFILE until a non-whitespace char
  631. and return that. Comments, both Lisp style and C style,
  632. are treated as whitespace.
  633. Tools such as genflags use this function. */
  634. int
  635. read_skip_spaces (infile)
  636. FILE *infile;
  637. {
  638. register int c;
  639. while (c = getc (infile))
  640. {
  641. if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
  642. ;
  643. else if (c == ';')
  644. {
  645. while ((c = getc (infile)) && c != '\n') ;
  646. }
  647. else if (c == '/')
  648. {
  649. register int prevc;
  650. c = getc (infile);
  651. if (c != '*')
  652. dump_and_abort ('*', c, infile);
  653. prevc = 0;
  654. while (c = getc (infile))
  655. {
  656. if (prevc == '*' && c == '/')
  657. break;
  658. prevc = c;
  659. }
  660. }
  661. else break;
  662. }
  663. return c;
  664. }
  665. /* Read an rtx code name into the buffer STR[].
  666. It is terminated by any of the punctuation chars of rtx printed syntax. */
  667. static void
  668. read_name (str, infile)
  669. char *str;
  670. FILE *infile;
  671. {
  672. register char *p;
  673. register int c;
  674. c = read_skip_spaces(infile);
  675. p = str;
  676. while (1)
  677. {
  678. if (c == ' ' || c == '\n' || c == '\t' || c == '\f')
  679. break;
  680. if (c == ':' || c == ')' || c == ']' || c == '"' || c == '/'
  681. || c == '(' || c == '[')
  682. {
  683. ungetc (c, infile);
  684. break;
  685. }
  686. *p++ = c;
  687. c = getc (infile);
  688. }
  689. *p = NULL;
  690. }
  691. /* Read an rtx in printed representation from INFILE
  692. and return an actual rtx in core constructed accordingly.
  693. read_rtx is not used in the compiler proper, but rather in
  694. the utilities gen*.c that construct C code from machine descriptions. */
  695. rtx
  696. read_rtx (infile)
  697. FILE *infile;
  698. {
  699. register int i, j, list_counter;
  700. RTX_CODE tmp_code;
  701. register char *format_ptr;
  702. /* tmp_char is a buffer used for reading decimal integers
  703. and names of rtx types and machine modes.
  704. Therefore, 256 must be enough. */
  705. char tmp_char[256];
  706. rtx return_rtx;
  707. register int c;
  708. int tmp_int;
  709. /* Linked list structure for making RTXs: */
  710. struct rtx_list
  711. {
  712. struct rtx_list *next;
  713. rtx value; /* Value of this node... */
  714. };
  715. c = read_skip_spaces (infile); /* Should be open paren. */
  716. if (c != '(')
  717. dump_and_abort ('(', c, infile);
  718. read_name (tmp_char, infile);
  719. tmp_code = UNKNOWN;
  720. for (i=0; i < NUM_RTX_CODE; i++) /* @@ might speed this search up */
  721. {
  722. if (!(strcmp (tmp_char, GET_RTX_NAME (i))))
  723. {
  724. tmp_code = (RTX_CODE) i; /* get value for name */
  725. break;
  726. }
  727. }
  728. if (tmp_code == UNKNOWN)
  729. {
  730. fprintf (stderr,
  731. "Unknown rtx read in rtl.read_rtx(). Code name was %s .",
  732. tmp_char);
  733. }
  734. /* (NIL) stands for an expression that isn't there. */
  735. if (tmp_code == NIL)
  736. {
  737. /* Discard the closeparen. */
  738. while ((c = getc (infile)) && c != ')');
  739. return 0;
  740. }
  741. return_rtx = rtx_alloc (tmp_code); /* if we end up with an insn expression
  742. then we free this space below. */
  743. format_ptr = GET_RTX_FORMAT (GET_CODE (return_rtx));
  744. /* If what follows is `: mode ', read it and
  745. store the mode in the rtx. */
  746. i = read_skip_spaces (infile);
  747. if (i == ':')
  748. {
  749. register int k;
  750. read_name (tmp_char, infile);
  751. for (k = 0; k < NUM_MACHINE_MODES; k++)
  752. if (!strcmp (GET_MODE_NAME (k), tmp_char))
  753. break;
  754. PUT_MODE (return_rtx, (enum machine_mode) k );
  755. }
  756. else
  757. ungetc (i, infile);
  758. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (return_rtx)); i++)
  759. switch (*format_ptr++)
  760. {
  761. /* 0 means a field for internal use only.
  762. Don't expect it to be present in the input. */
  763. case '0':
  764. break;
  765. case 'e':
  766. case 'u':
  767. XEXP (return_rtx, i) = read_rtx (infile);
  768. break;
  769. case 'E':
  770. {
  771. register struct rtx_list *next_rtx, *rtx_list_link;
  772. struct rtx_list *list_rtx;
  773. c = read_skip_spaces (infile);
  774. if (c != '[')
  775. dump_and_abort ('[', c, infile);
  776. /* add expressions to a list, while keeping a count */
  777. next_rtx = NULL;
  778. list_counter = 0;
  779. while ((c = read_skip_spaces (infile)) && c != ']')
  780. {
  781. ungetc (c, infile);
  782. list_counter++;
  783. rtx_list_link = (struct rtx_list *)
  784. alloca (sizeof (struct rtx_list));
  785. rtx_list_link->value = read_rtx (infile);
  786. if (next_rtx == 0)
  787. list_rtx = rtx_list_link;
  788. else
  789. next_rtx->next = rtx_list_link;
  790. next_rtx = rtx_list_link;
  791. rtx_list_link->next = 0;
  792. }
  793. /* get vector length and allocate it */
  794. XVEC (return_rtx, i) = (list_counter
  795. ? rtvec_alloc (list_counter)
  796. : NULL);
  797. if (list_counter > 0)
  798. {
  799. next_rtx = list_rtx;
  800. for (j = 0; j < list_counter; j++,
  801. next_rtx = next_rtx->next)
  802. XVECEXP (return_rtx, i, j) = next_rtx->value;
  803. }
  804. /* close bracket gotten */
  805. }
  806. break;
  807. case 's':
  808. {
  809. int saw_paren = 0;
  810. register char *stringbuf;
  811. int stringbufsize;
  812. c = read_skip_spaces (infile);
  813. if (c == '(')
  814. {
  815. saw_paren = 1;
  816. c = read_skip_spaces (infile);
  817. }
  818. if (c != '"')
  819. dump_and_abort ('"', c, infile);
  820. j = 0;
  821. stringbufsize = 10;
  822. stringbuf = (char *) xmalloc (stringbufsize + 1);
  823. while (1)
  824. {
  825. if (j >= stringbufsize - 4)
  826. {
  827. stringbufsize *= 2;
  828. stringbuf = (char *) xrealloc (stringbuf, stringbufsize + 1);
  829. }
  830. stringbuf[j] = getc (infile); /* Read the string */
  831. if (stringbuf[j] == '\\')
  832. {
  833. stringbuf[j] = getc (infile); /* Read the string */
  834. /* \; makes stuff for a C string constant containing
  835. newline and tab. */
  836. if (stringbuf[j] == ';')
  837. {
  838. strcpy (&stringbuf[j], "\\n\\t");
  839. j += 3;
  840. }
  841. }
  842. else if (stringbuf[j] == '"')
  843. break;
  844. j++;
  845. }
  846. stringbuf[j] = 0; /* NUL terminate the string */
  847. stringbuf = (char *) xrealloc (stringbuf, j + 1);
  848. if (saw_paren)
  849. {
  850. c = read_skip_spaces (infile);
  851. if (c != ')')
  852. dump_and_abort (')', c, infile);
  853. }
  854. XSTR (return_rtx, i) = stringbuf;
  855. }
  856. break;
  857. case 'i':
  858. read_name (tmp_char, infile);
  859. tmp_int = atoi (tmp_char);
  860. XINT (return_rtx, i) = tmp_int;
  861. break;
  862. default:
  863. fprintf (stderr,
  864. "switch format wrong in rtl.read_rtx(). format was: %c.\n",
  865. format_ptr[-1]);
  866. fprintf (stderr, "\tfile position: %ld\n", ftell (infile));
  867. abort ();
  868. }
  869. c = read_skip_spaces (infile);
  870. if (c != ')')
  871. dump_and_abort (')', c, infile);
  872. return return_rtx;
  873. }
  874. /* This is called once per compilation, before any rtx's are constructed.
  875. It initializes the vector `rtx_length'. */
  876. void
  877. init_rtl ()
  878. {
  879. int i;
  880. for (i = 0; i < NUM_RTX_CODE; i++)
  881. rtx_length[i] = strlen (rtx_format[i]);
  882. }