stmt.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /* Expands front end tree to back end RTL for GNU C-Compiler
  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 file handles the generation of rtl code from tree structure
  18. at the level of statements using subroutines in exp*.c and emit-rtl.c.
  19. It also creates the rtl expressions for parameters and auto variables
  20. and has full responsibility for allocating stack slots.
  21. A few routines in this file are called during later passes
  22. when stack frame management requires it.
  23. The main entry point is expand_function, which is at the end. */
  24. #include "config.h"
  25. #include <stdio.h>
  26. #include "rtl.h"
  27. #include "tree.h"
  28. #include "insn-flags.h"
  29. #include "stupid.h"
  30. #include "expr.h"
  31. #define MAX(x,y) (((x) > (y)) ? (x) : (y))
  32. #define MIN(x,y) (((x) < (y)) ? (x) : (y))
  33. /* Label that will go on function epilogue.
  34. Jumping to this label serves as a "return" instruction
  35. on machines which require execution of the epilogue on all returns. */
  36. static rtx return_label;
  37. /* The FUNCTION_DECL node for the function being compiled. */
  38. static tree this_function;
  39. /* Offset to end of allocated area of stack frame.
  40. If stack grows down, this is the address of the last stack slot allocated.
  41. If stack grows up, this is the address for the next slot. */
  42. static int frame_offset;
  43. /* Length in bytes of largest structure value returned by
  44. any function called so far in this function. */
  45. static int max_structure_value_size;
  46. /* Label to jump back to for tail recursion, or 0 if we have
  47. not yet needed one for this function. */
  48. static rtx tail_recursion_label;
  49. /* Place after which to insert the tail_recursion_label if we need one. */
  50. static rtx tail_recursion_reentry;
  51. static int tail_recursion_args ();
  52. /* Estimate the complexity of the compiled code for STMT.
  53. This is a rough estimate and is used for purposes
  54. of deciding which optimizations are worth applying. */
  55. static int
  56. stmt_complexity (stmt)
  57. tree stmt;
  58. {
  59. register tree s;
  60. register int c = 0;
  61. for (s = stmt; s; s = TREE_CHAIN (s))
  62. switch (TREE_CODE (s))
  63. {
  64. case LABEL_STMT:
  65. break;
  66. case COMPOUND_STMT:
  67. c += stmt_complexity (STMT_BODY (s));
  68. break;
  69. case LOOP_STMT:
  70. c += 1 + stmt_complexity (STMT_BODY (s));
  71. break;
  72. case EXIT_STMT:
  73. c += 2;
  74. break;
  75. case GOTO_STMT:
  76. case ASM_STMT:
  77. c += 1;
  78. break;
  79. case IF_STMT:
  80. c += 2 + stmt_complexity (STMT_THEN (s))
  81. + stmt_complexity (STMT_ELSE (s));
  82. break;
  83. case EXPR_STMT:
  84. case RETURN_STMT:
  85. c += 3;
  86. break;
  87. /* The body of the case statement is actually not included
  88. in the CASE_STMT, so it will be counted separately. */
  89. case CASE_STMT:
  90. c += 3;
  91. break;
  92. case LET_STMT:
  93. case WITH_STMT:
  94. c += list_length (STMT_VARS (s)) + stmt_complexity (STMT_BODY (s));
  95. break;
  96. default: abort ();
  97. }
  98. return c;
  99. }
  100. static void expand_stmt ();
  101. static void expand_stmts ();
  102. static void expand_case_stmt ();
  103. /* Set nonzero at beginning of function
  104. to prevent output of the NOTE_INSN_BLOCK_BEG for the outermost block.
  105. This is because `final' generates it specially,
  106. before the function prologue. */
  107. static int inhibit_block_beg;
  108. /* Return the rtx-label that corresponds to a LABEL_DECL,
  109. creating it if necessary. */
  110. static rtx
  111. label_rtx (label)
  112. tree label;
  113. {
  114. if (DECL_RTL (label))
  115. return DECL_RTL (label);
  116. return DECL_RTL (label) = gen_label_rtx ();
  117. }
  118. /* Add an unconditional jump to LABEL as the next sequential instruction. */
  119. void
  120. emit_jump (label)
  121. rtx label;
  122. {
  123. do_pending_stack_adjust ();
  124. emit_jump_insn (gen_jump (label));
  125. emit_barrier ();
  126. }
  127. /* Return nonzero if T is a "simple enough" expression
  128. such that we prefer to duplicate it as a loop exit condition.
  129. We accept only comparisons whose operands are constants or variables. */
  130. static int
  131. exit_simple_enough_p (t)
  132. tree t;
  133. {
  134. register enum tree_code code = TREE_CODE (t);
  135. register tree op;
  136. if (!(code == EQ_EXPR || code == NE_EXPR
  137. || code == LT_EXPR || code == LE_EXPR
  138. || code == GT_EXPR || code == GE_EXPR))
  139. return 0;
  140. op = TREE_OPERAND (t, 0);
  141. if (TREE_CODE (op) != VAR_DECL
  142. && TREE_CODE (op) != INTEGER_CST
  143. && TREE_CODE (op) != REAL_CST)
  144. return 0;
  145. op = TREE_OPERAND (t, 1);
  146. if (TREE_CODE (op) != VAR_DECL
  147. && TREE_CODE (op) != INTEGER_CST
  148. && TREE_CODE (op) != REAL_CST)
  149. return 0;
  150. return 1;
  151. }
  152. /* Generate rtl code for a sequence of statements
  153. chained through the TREE_CHAIN.
  154. LOOP_EXIT says where an EXIT_STMT should jump to. */
  155. static void
  156. expand_stmts (stmts, loop_exit)
  157. tree stmts;
  158. rtx loop_exit;
  159. {
  160. register tree stmt;
  161. for (stmt = stmts; stmt; stmt = TREE_CHAIN (stmt))
  162. expand_stmt (stmt, loop_exit);
  163. }
  164. /* Generate rtl for one statement, STMT.
  165. LOOP_EXIT is an rtl CODE_LABEL to jump to to exit a loop. */
  166. /* Stack of LET_STMT blocks that we are currently within
  167. during the rtl-generation tree walk. */
  168. struct block_stack
  169. {
  170. tree block; /* the LET_STMT tree node */
  171. rtx stack_level; /* the saved-on-entry stack pointer */
  172. struct block_stack *next; /* data for the containing LET_STMT or 0 */
  173. };
  174. struct block_stack *block_stack;
  175. static void
  176. expand_stmt (stmt, loop_exit)
  177. tree stmt;
  178. rtx loop_exit;
  179. {
  180. struct block_stack thisblock;
  181. if (STMT_SOURCE_LINE (stmt) != 0)
  182. emit_note (STMT_SOURCE_FILE (stmt), STMT_SOURCE_LINE (stmt));
  183. switch (TREE_CODE (stmt))
  184. {
  185. case LABEL_STMT:
  186. do_pending_stack_adjust ();
  187. emit_label (label_rtx (STMT_BODY (stmt)));
  188. break;
  189. case GOTO_STMT:
  190. if (GET_CODE (label_rtx (STMT_BODY (stmt))) != CODE_LABEL)
  191. abort ();
  192. /* Look at the binding contours (LET_STMTs) we are jumping out of
  193. and if any of them allocates a variable size auto variable
  194. reset the stack to the appropriate level. */
  195. {
  196. tree context = DECL_CONTEXT (STMT_BODY (stmt));
  197. struct block_stack *block;
  198. rtx stack_level = 0;
  199. /* Chase contexts up from the target label. */
  200. while (context)
  201. {
  202. /* Chase contexts up from where we are.
  203. We want the innermost block containing both
  204. the goto and the label. */
  205. for (block = block_stack; block;
  206. block = block->next)
  207. {
  208. if (block->stack_level != 0)
  209. stack_level = block->stack_level;
  210. if (block->block == context)
  211. {
  212. if (stack_level != 0)
  213. emit_move_insn (gen_rtx (REG, Pmode,
  214. STACK_POINTER_REGNUM),
  215. stack_level);
  216. goto context_done;
  217. }
  218. }
  219. context = STMT_SUPERCONTEXT (context);
  220. }
  221. context_done: ;
  222. }
  223. emit_jump (label_rtx (STMT_BODY (stmt)));
  224. break;
  225. case EXPR_STMT:
  226. expand_expr (STMT_BODY (stmt), 0, VOIDmode, 0);
  227. break;
  228. case COMPOUND_STMT:
  229. expand_stmts (STMT_BODY (stmt), loop_exit);
  230. break;
  231. case ASM_STMT:
  232. emit_insn (gen_rtx (ASM_INPUT, VOIDmode,
  233. TREE_STRING_POINTER (STMT_BODY (stmt))));
  234. break;
  235. case IF_STMT:
  236. {
  237. register rtx afterlabel = gen_label_rtx ();
  238. /* Simpler handling if there is no else-part
  239. or a null then-part. */
  240. if (STMT_THEN (stmt) == 0)
  241. {
  242. do_jump (STMT_COND (stmt), NULL, afterlabel);
  243. expand_stmts (STMT_ELSE (stmt), loop_exit);
  244. }
  245. else if (STMT_ELSE (stmt) == 0)
  246. {
  247. do_jump (STMT_COND (stmt), afterlabel, NULL);
  248. expand_stmts (STMT_THEN (stmt), loop_exit);
  249. }
  250. else
  251. {
  252. register rtx elselabel = gen_label_rtx ();
  253. do_jump (STMT_COND (stmt), elselabel, NULL);
  254. expand_stmts (STMT_THEN (stmt), loop_exit);
  255. emit_jump (afterlabel);
  256. emit_label (elselabel);
  257. expand_stmts (STMT_ELSE (stmt), loop_exit);
  258. }
  259. do_pending_stack_adjust ();
  260. emit_label (afterlabel);
  261. }
  262. break;
  263. case EXIT_STMT:
  264. /* Exit if the condition is false. */
  265. do_jump (STMT_BODY (stmt), loop_exit, NULL);
  266. break;
  267. case RETURN_STMT:
  268. if (STMT_BODY (stmt))
  269. {
  270. register rtx val = 0;
  271. register rtx op0;
  272. /* For tail-recursive call to current function,
  273. just jump back to the beginning.
  274. It's unsafe if any auto variable in this function
  275. has its address taken; for simplicity,
  276. require stack frame to be empty. */
  277. if (! cse_not_expected
  278. && frame_offset == 0
  279. && TREE_CODE (STMT_BODY (stmt)) == MODIFY_EXPR
  280. && TREE_CODE (TREE_OPERAND (STMT_BODY (stmt), 1)) == CALL_EXPR
  281. && TREE_CODE (TREE_OPERAND (TREE_OPERAND (STMT_BODY (stmt), 1), 0)) == ADDR_EXPR
  282. && TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (STMT_BODY (stmt), 1), 0), 0) == this_function
  283. /* Finish checking validity, and if valid emit code
  284. to set the argument variables for the new call. */
  285. && tail_recursion_args (TREE_OPERAND (TREE_OPERAND (STMT_BODY (stmt), 1), 1),
  286. DECL_ARGUMENTS (this_function)))
  287. {
  288. ;
  289. if (tail_recursion_label == 0)
  290. {
  291. tail_recursion_label = gen_label_rtx ();
  292. emit_label_after (tail_recursion_label,
  293. tail_recursion_reentry);
  294. }
  295. emit_jump (tail_recursion_label);
  296. emit_barrier ();
  297. break;
  298. }
  299. #ifndef FUNCTION_EPILOGUE
  300. /* If this is return x == y; then generate
  301. if (x == y) return 1; else return 0;
  302. if we can do it with explicit return insns. */
  303. if (TREE_CODE (STMT_BODY (stmt)) == MODIFY_EXPR)
  304. switch (TREE_CODE (TREE_OPERAND (STMT_BODY (stmt), 1)))
  305. {
  306. case EQ_EXPR:
  307. case NE_EXPR:
  308. case GT_EXPR:
  309. case GE_EXPR:
  310. case LT_EXPR:
  311. case LE_EXPR:
  312. case TRUTH_ANDIF_EXPR:
  313. case TRUTH_ORIF_EXPR:
  314. case TRUTH_NOT_EXPR:
  315. op0 = gen_label_rtx ();
  316. val = DECL_RTL (DECL_RESULT (this_function));
  317. jumpifnot (TREE_OPERAND (STMT_BODY (stmt), 1), op0);
  318. emit_move_insn (val, const1_rtx);
  319. emit_insn (gen_rtx (USE, VOIDmode, val));
  320. emit_jump_insn (gen_return ());
  321. emit_barrier ();
  322. emit_label (op0);
  323. emit_move_insn (val, const0_rtx);
  324. emit_insn (gen_rtx (USE, VOIDmode, val));
  325. emit_jump_insn (gen_return ());
  326. emit_barrier ();
  327. }
  328. if (val != 0)
  329. break;
  330. #endif
  331. val = expand_expr (STMT_BODY (stmt), 0, VOIDmode, 0);
  332. if (GET_CODE (val) == REG)
  333. emit_insn (gen_rtx (USE, VOIDmode, val));
  334. emit_queue ();
  335. }
  336. /* Return insn or function epilogue ignore the stack pointer. */
  337. clear_pending_stack_adjust ();
  338. #ifdef FUNCTION_EPILOGUE
  339. emit_jump (return_label);
  340. #else
  341. emit_jump_insn (gen_return ());
  342. #endif
  343. emit_barrier ();
  344. break;
  345. case LET_STMT:
  346. {
  347. rtx oldstack = 0;
  348. register tree decl;
  349. /* Make an entry on BLOCK_STACK for the block we are entering. */
  350. thisblock.block = stmt;
  351. thisblock.next = block_stack;
  352. thisblock.stack_level = 0;
  353. block_stack = &thisblock;
  354. /* Output a NOTE to mark the beginning of the scope,
  355. except when inhibited (for a function's outermost block). */
  356. if (inhibit_block_beg)
  357. inhibit_block_beg = 0;
  358. else
  359. emit_note (0, NOTE_INSN_BLOCK_BEG);
  360. if (reg_birth_insn)
  361. {
  362. /* If doing stupid register allocation,
  363. mark all register variables of this block
  364. as beginning life here. */
  365. register rtx last_insn = get_last_insn ();
  366. for (decl = STMT_VARS (stmt); decl; decl = TREE_CHAIN (decl))
  367. {
  368. if (TREE_CODE (decl) == VAR_DECL
  369. && DECL_RTL (decl) != 0
  370. && GET_CODE (DECL_RTL (decl)) == REG)
  371. reg_birth_insn[REGNO (DECL_RTL (decl))] = last_insn;
  372. }
  373. }
  374. /* Allocate space for all variable-size variables,
  375. and set OLDSTACK nonzero if there are any. */
  376. for (decl = STMT_VARS (stmt); decl; decl = TREE_CHAIN (decl))
  377. if (TREE_CODE (decl) == VAR_DECL
  378. && !TREE_LITERAL (DECL_SIZE (decl)))
  379. {
  380. rtx address, size;
  381. if (oldstack == 0)
  382. {
  383. do_pending_stack_adjust ();
  384. oldstack = copy_to_reg (gen_rtx (REG, Pmode,
  385. STACK_POINTER_REGNUM));
  386. thisblock.stack_level = oldstack;
  387. }
  388. size = expand_expr (DECL_SIZE (decl), 0, VOIDmode, 0);
  389. #ifdef STACK_GROWS_DOWNWARD
  390. anti_adjust_stack (size);
  391. #endif
  392. address = copy_to_reg (gen_rtx (REG, Pmode,
  393. STACK_POINTER_REGNUM));
  394. #ifndef STACK_GROWS_DOWNWARD
  395. anti_adjust_stack (size);
  396. #endif
  397. DECL_RTL (decl) = gen_rtx (MEM, DECL_MODE (decl), address);
  398. }
  399. /* Compute and store the initial values
  400. of all nonstatic variables bound here. */
  401. for (decl = STMT_VARS (stmt); decl; decl = TREE_CHAIN (decl))
  402. if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl)
  403. && ! TREE_STATIC (decl))
  404. {
  405. if (DECL_VOFFSET (decl)
  406. || !TREE_LITERAL (DECL_SIZE (decl)))
  407. abort ();
  408. emit_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
  409. expand_assignment (decl, DECL_INITIAL (decl));
  410. }
  411. /* Generate code for the body of the block. */
  412. expand_stmts (STMT_BODY (stmt), 0);
  413. /* Mark the end of the scope. */
  414. emit_note (0, NOTE_INSN_BLOCK_END);
  415. if (reg_death_insn)
  416. {
  417. /* If doing stupid register allocation,
  418. mark all register variables of this block
  419. as having just died. */
  420. register rtx last_insn = get_last_insn ();
  421. for (decl = STMT_VARS (stmt); decl; decl = TREE_CHAIN (decl))
  422. {
  423. if (TREE_CODE (decl) == VAR_DECL
  424. && DECL_RTL (decl) != 0
  425. && GET_CODE (DECL_RTL (decl)) == REG)
  426. reg_death_insn[REGNO (DECL_RTL (decl))] = last_insn;
  427. }
  428. }
  429. /* Restore stack level in effect before the block
  430. (only if variable-size objects allocated). */
  431. if (oldstack != 0)
  432. emit_move_insn (gen_rtx (REG, Pmode,
  433. STACK_POINTER_REGNUM),
  434. oldstack);
  435. /* Restore block_stack level for containing block. */
  436. block_stack = thisblock.next;
  437. }
  438. break;
  439. case LOOP_STMT:
  440. {
  441. register rtx lab1, lab2;
  442. register tree x1 = tree_last (STMT_BODY (stmt));
  443. /* There are several ways to arrange the compilation of a loop.
  444. We choose one depending on where the exits are and what kinds
  445. of conditions they test. */
  446. lab1 = gen_label_rtx ();
  447. lab2 = gen_label_rtx ();
  448. /* If the body ends with a conditional exit or goto,
  449. just compile it straight through. The conditional at the end
  450. will combine with the branch back. */
  451. if (TREE_CODE (x1) == EXIT_STMT
  452. || (TREE_CODE (x1) == IF_STMT
  453. && (TREE_CODE (STMT_THEN (x1)) == GOTO_STMT
  454. || (STMT_ELSE (x1)
  455. && TREE_CODE (STMT_ELSE (x1)) == GOTO_STMT))))
  456. {
  457. do_pending_stack_adjust ();
  458. emit_note (0, NOTE_INSN_LOOP_BEG);
  459. emit_label (lab1);
  460. expand_stmts (STMT_BODY (stmt), lab2);
  461. emit_jump (lab1);
  462. }
  463. #if 0
  464. /* If the loop starts with a conditional exit that tests
  465. a very simple condition, duplicate the test, jumping around
  466. the loop if we don't want to execute it even once.
  467. Then put the test at the end of the loop. */
  468. else if (! cse_not_expected
  469. && TREE_CODE (STMT_BODY (stmt)) == EXIT_STMT
  470. && stmt_complexity (stmt) < 15
  471. && exit_simple_enough_p (STMT_BODY (STMT_BODY (stmt))))
  472. {
  473. do_jump (STMT_BODY (STMT_BODY (stmt)), lab2, 0);
  474. emit_note (0, NOTE_INSN_LOOP_BEG);
  475. emit_label (lab1);
  476. expand_stmts (TREE_CHAIN (STMT_BODY (stmt)), lab2);
  477. do_jump (STMT_BODY (STMT_BODY (stmt)), 0, lab1);
  478. }
  479. #endif
  480. /* If the loop starts with a conditional exit that tests
  481. a very simple condition, put that exit at the end of the loop
  482. and enter by jumping to that test. */
  483. else if (! cse_not_expected
  484. && TREE_CODE (STMT_BODY (stmt)) == EXIT_STMT)
  485. {
  486. register rtx lab3 = gen_label_rtx ();
  487. do_pending_stack_adjust ();
  488. emit_note (0, NOTE_INSN_LOOP_BEG);
  489. emit_jump (lab3);
  490. emit_label (lab1);
  491. expand_stmts (TREE_CHAIN (STMT_BODY (stmt)), lab2);
  492. do_pending_stack_adjust ();
  493. emit_label (lab3);
  494. do_jump (STMT_BODY (STMT_BODY (stmt)), 0, lab1);
  495. }
  496. /* Neither starts nor ends with a conditional exit. Strange.
  497. Do it the simplest possible way. */
  498. else
  499. {
  500. do_pending_stack_adjust ();
  501. emit_note (0, NOTE_INSN_LOOP_BEG);
  502. emit_label (lab1);
  503. expand_stmts (STMT_BODY (stmt), lab2);
  504. emit_jump (lab1);
  505. }
  506. emit_note (0, NOTE_INSN_LOOP_END);
  507. emit_label (lab2);
  508. }
  509. break;
  510. case CASE_STMT:
  511. expand_case_stmt (stmt);
  512. break;
  513. default:
  514. abort ();
  515. }
  516. /* Perform any postincrements or postdecrements. */
  517. emit_queue ();
  518. }
  519. /* Emit code to alter this function's formal parms for a tail-recursive call.
  520. ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
  521. FORMALS is the chain of decls of formals.
  522. Return 1 if this can be done;
  523. otherwise return 0 and do not emit any code. */
  524. static int
  525. tail_recursion_args (actuals, formals)
  526. tree actuals, formals;
  527. {
  528. register tree a = actuals, f = formals;
  529. register int i;
  530. register rtx *argvec;
  531. /* Check that number and types of actuals are compatible
  532. with the formals. This is not always true in valid C code.
  533. Also check that no formal needs to be addressable
  534. and that all formals are scalars. */
  535. /* Also count the args. */
  536. for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
  537. {
  538. if (TREE_TYPE (TREE_VALUE (a)) != TREE_TYPE (f))
  539. return 0;
  540. if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
  541. return 0;
  542. }
  543. if (a != 0 || f != 0)
  544. return 0;
  545. /* Compute all the actuals. */
  546. argvec = (rtx *) alloca (i * sizeof (rtx));
  547. for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
  548. argvec[i] = expand_expr (TREE_VALUE (a), 0, VOIDmode, 0);
  549. /* Find which actual values refer to current values of previous formals.
  550. Copy each of them now, before any formal is changed. */
  551. for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
  552. {
  553. int copy = 0;
  554. register int j;
  555. for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
  556. if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
  557. { copy = 1; break; }
  558. if (copy)
  559. argvec[i] = copy_to_reg (argvec[i]);
  560. }
  561. /* Store the values of the actuals into the formals. */
  562. for (f = formals, i = 0; f; f = TREE_CHAIN (f), i++)
  563. {
  564. if (DECL_MODE (f) == GET_MODE (argvec[i]))
  565. emit_move_insn (DECL_RTL (f), argvec[i]);
  566. else
  567. convert_move (DECL_RTL (f), argvec[i]);
  568. }
  569. return 1;
  570. }
  571. /* Generate code for a CASE_STMT node,
  572. which stands for a dispatch table. */
  573. static void
  574. expand_case_stmt (stmt)
  575. tree stmt;
  576. {
  577. tree minval, maxval, range;
  578. rtx default_label = 0;
  579. register tree elt;
  580. register tree c;
  581. int count;
  582. tree index_exp;
  583. rtx index;
  584. rtx table_label = gen_label_rtx ();
  585. int ncases;
  586. rtx *labelvec;
  587. register int i;
  588. /* Get upper and lower bounds of case values. */
  589. count = 0;
  590. for (c = STMT_CASE_LIST (stmt); c; c = TREE_CHAIN (c))
  591. if (elt = TREE_PURPOSE (c))
  592. {
  593. /* Note that in Pascal it will be possible
  594. to have a RANGE_EXPR here as long as both
  595. ends of the range are constant.
  596. It will be necessary to extend this function
  597. to handle them. */
  598. if (TREE_CODE (elt) != INTEGER_CST)
  599. abort ();
  600. if (count++ == 0)
  601. {
  602. minval = maxval = elt;
  603. }
  604. else
  605. {
  606. if (INT_CST_LT (elt, minval))
  607. minval = elt;
  608. if (INT_CST_LT (maxval, elt))
  609. maxval = elt;
  610. }
  611. }
  612. else
  613. default_label = label_rtx (TREE_VALUE (c));
  614. if (default_label == 0)
  615. abort ();
  616. /* Compute span of values. */
  617. range = combine (MINUS_EXPR, maxval, minval);
  618. /* If range of values is much bigger than number of values,
  619. make a sequence of conditional branches instead of a dispatch. */
  620. if (TREE_INT_CST_HIGH (range) != 0
  621. #ifdef HAVE_casesi
  622. || count < 4
  623. #else
  624. /* If machine does not have a case insn that compares the
  625. bounds, this means extra overhead for dispatch tables
  626. which raises the threshold for using them. */
  627. || count < 7
  628. #endif
  629. || TREE_INT_CST_LOW (range) > 10 * count)
  630. {
  631. index_exp = get_unwidened (STMT_CASE_INDEX (stmt), 0);
  632. index = expand_expr (index_exp, 0, VOIDmode, 0);
  633. emit_queue ();
  634. index = protect_from_queue (index, 0);
  635. if (GET_CODE (index) == MEM)
  636. index = copy_to_reg (index);
  637. do_pending_stack_adjust ();
  638. for (c = STMT_CASE_LIST (stmt); c; c = TREE_CHAIN (c))
  639. if ((elt = TREE_PURPOSE (c))
  640. && int_fits_type_p (elt, TREE_TYPE (index_exp)))
  641. do_jump_if_equal (expand_expr (elt, 0, VOIDmode, 0), index,
  642. label_rtx (TREE_VALUE (c)));
  643. emit_jump (default_label);
  644. return;
  645. }
  646. index_exp = STMT_CASE_INDEX (stmt);
  647. #ifdef HAVE_casesi
  648. if (TYPE_MODE (TREE_TYPE (index_exp)) == DImode)
  649. {
  650. index_exp = build2 (MINUS_EXPR, index_exp, minval);
  651. TREE_TYPE (index_exp) = TREE_TYPE (STMT_CASE_INDEX (stmt));
  652. index_exp = convert (integer_type_node, index_exp);
  653. minval = integer_zero_node;
  654. }
  655. else if (TYPE_MODE (TREE_TYPE (index_exp)) != SImode)
  656. index_exp = convert (integer_type_node, index_exp);
  657. index = expand_expr (index_exp, 0, VOIDmode, 0);
  658. emit_queue ();
  659. index = protect_from_queue (index, 0);
  660. do_pending_stack_adjust ();
  661. emit_jump_insn (gen_casesi (index, expand_expr (minval, 0, VOIDmode, 0),
  662. expand_expr (range, 0, VOIDmode, 0),
  663. table_label));
  664. #else
  665. #ifdef HAVE_tablejump
  666. index_exp = build2 (MINUS_EXPR, index_exp, minval);
  667. TREE_TYPE (index_exp) = TREE_TYPE (STMT_CASE_INDEX (stmt));
  668. index_exp = convert (integer_type_node, index_exp);
  669. index = expand_expr (index_exp, 0, VOIDmode, 0);
  670. emit_queue ();
  671. index = protect_from_queue (index, 0);
  672. do_pending_stack_adjust ();
  673. do_tablejump (index,
  674. gen_rtx (CONST_INT, VOIDmode, TREE_INT_CST_LOW (range)),
  675. table_label, default_label);
  676. #else
  677. lossage;
  678. #endif /* not HAVE_tablejump */
  679. #endif /* not HAVE_casesi */
  680. /* Get table of labels to jump to, in order of case index. */
  681. ncases = TREE_INT_CST_LOW (range) + 1;
  682. labelvec = (rtx *) alloca (ncases * sizeof (rtx));
  683. bzero (labelvec, ncases * sizeof (rtx));
  684. for (c = STMT_CASE_LIST (stmt); c; c = TREE_CHAIN (c))
  685. if (elt = TREE_PURPOSE (c))
  686. {
  687. register int i = TREE_INT_CST_LOW (elt) - TREE_INT_CST_LOW (minval);
  688. labelvec[i] = gen_rtx (LABEL_REF, Pmode, label_rtx (TREE_VALUE (c)));
  689. }
  690. /* Fill in the gaps with the default. */
  691. for (i = 0; i < ncases; i++)
  692. if (labelvec[i] == 0)
  693. labelvec[i] = gen_rtx (LABEL_REF, Pmode, default_label);
  694. /* Output the table */
  695. emit_label (table_label);
  696. #ifdef CASE_VECTOR_PC_RELATIVE
  697. emit_jump_insn (gen_rtx (ADDR_DIFF_VEC, CASE_VECTOR_MODE,
  698. gen_rtx (LABEL_REF, Pmode, table_label),
  699. gen_rtvec_v (ncases, labelvec)));
  700. #else
  701. emit_jump_insn (gen_rtx (ADDR_VEC, CASE_VECTOR_MODE,
  702. gen_rtvec_v (ncases, labelvec)));
  703. #endif
  704. emit_jump (default_label);
  705. }
  706. /* Find all the variables declared within a function
  707. and give them rtl definitions. */
  708. /* Return size needed for stack frame based on slots so far allocated. */
  709. int
  710. get_frame_size ()
  711. {
  712. return frame_offset;
  713. }
  714. /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
  715. with machine mode MODE. */
  716. rtx
  717. assign_stack_local (mode, size)
  718. enum machine_mode mode;
  719. int size;
  720. {
  721. register rtx value;
  722. /* This function may not be used during rtl generation
  723. because at that time space is being allocated for
  724. structure values returned by function calls,
  725. but we don't know how big the space is until the end
  726. of rtl generation. */
  727. if (max_structure_value_size > 0)
  728. abort ();
  729. /* Make each stack slot a multiple of the main allocation unit. */
  730. size = (((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
  731. / (BIGGEST_ALIGNMENT / BITS_PER_UNIT))
  732. * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  733. #ifdef FRAME_GROWS_DOWNWARD
  734. frame_offset -= size;
  735. #endif
  736. value = gen_rtx (MEM, mode,
  737. gen_rtx (PLUS, Pmode,
  738. gen_rtx (REG, SImode, FRAME_POINTER_REGNUM),
  739. gen_rtx (CONST_INT, VOIDmode, frame_offset)));
  740. #ifndef FRAME_GROWS_DOWNWARD
  741. frame_offset += size;
  742. #endif
  743. return value;
  744. }
  745. /* 1 + last pseudo register number used for one of the user's variables
  746. (as opposed to compiler-generated temporaries). */
  747. int first_temp_reg_num;
  748. static void assign_vars_1 ();
  749. /* Assign stack slots or pseudo-registers to all the variables
  750. local to the body of a function being compiled (STMT). */
  751. static void
  752. assign_all_vars (stmt)
  753. tree stmt;
  754. {
  755. frame_offset = STARTING_FRAME_OFFSET;
  756. assign_vars_1 (stmt);
  757. first_temp_reg_num = max_reg_num ();
  758. }
  759. /* Assign stack slots or pseudo-registers to all the identifiers
  760. local within STMT, by recursive tree walk, except for variables
  761. of varying size. */
  762. static void
  763. assign_vars_1 (stmt)
  764. register tree stmt;
  765. {
  766. register tree decl;
  767. while (stmt)
  768. {
  769. switch (TREE_CODE (stmt))
  770. {
  771. case COMPOUND_STMT:
  772. case LOOP_STMT:
  773. assign_vars_1 (STMT_BODY (stmt));
  774. break;
  775. case IF_STMT:
  776. assign_vars_1 (STMT_THEN (stmt));
  777. assign_vars_1 (STMT_ELSE (stmt));
  778. break;
  779. case LET_STMT:
  780. for (decl = STMT_VARS (stmt); decl; decl = TREE_CHAIN (decl))
  781. {
  782. if (TREE_TYPE (decl) == error_mark_node)
  783. DECL_RTL (decl) = gen_rtx (MEM, BLKmode, const0_rtx);
  784. else if (TREE_CODE (decl) == FUNCTION_DECL)
  785. /* External function */
  786. DECL_RTL (decl)
  787. = gen_rtx (MEM, FUNCTION_MODE,
  788. gen_rtx (SYMBOL_REF, Pmode,
  789. IDENTIFIER_POINTER (DECL_NAME (decl))));
  790. else if (TREE_CODE (decl) != VAR_DECL)
  791. ;
  792. else if (TREE_STATIC (decl) || TREE_EXTERNAL (decl))
  793. ; /* These were done by assemble_variable. */
  794. else if (DECL_MODE (decl) != BLKmode
  795. && ! TREE_VOLATILE (decl)
  796. && ! TREE_ADDRESSABLE (decl)
  797. && (TREE_REGDECL (decl) || ! obey_regdecls))
  798. {
  799. /* Variable that can go in a register. */
  800. DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
  801. if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
  802. mark_reg_pointer (DECL_RTL (decl));
  803. }
  804. else if (TREE_LITERAL (DECL_SIZE (decl)))
  805. /* Variable of fixed size that goes on the stack. */
  806. DECL_RTL (decl)
  807. = assign_stack_local (DECL_MODE (decl),
  808. (TREE_INT_CST_LOW (DECL_SIZE (decl))
  809. * DECL_SIZE_UNIT (decl)
  810. + BITS_PER_UNIT - 1)
  811. / BITS_PER_UNIT);
  812. /* Rtl for a dynamic-size object is set up when
  813. the storage for the object is pushed. */
  814. }
  815. assign_vars_1 (STMT_BODY (stmt));
  816. }
  817. stmt = TREE_CHAIN (stmt);
  818. }
  819. }
  820. /* 1 + last pseudo register number used for loading a copy
  821. of a parameter of this function. */
  822. static int max_parm_reg;
  823. /* Assign RTL expressions to the function's parameters.
  824. This may involve copying them into registers and using
  825. those registers as the RTL for them. */
  826. static void
  827. assign_parms (fndecl)
  828. tree fndecl;
  829. {
  830. register tree parm;
  831. register rtx parmloc;
  832. register int i;
  833. for (parm = DECL_ARGUMENTS (fndecl), i = 0; parm; parm = TREE_CHAIN (parm), i++)
  834. {
  835. if (DECL_VOFFSET (parm))
  836. abort ();
  837. if (TREE_TYPE (parm) == error_mark_node)
  838. parmloc = gen_rtx (MEM, BLKmode, const0_rtx);
  839. else
  840. parmloc
  841. = gen_rtx (MEM, TYPE_MODE (DECL_ARG_TYPE (parm)),
  842. gen_rtx (PLUS, SImode,
  843. gen_rtx (REG, SImode, ARG_POINTER_REGNUM),
  844. gen_rtx (CONST_INT, VOIDmode,
  845. DECL_OFFSET (parm) / BITS_PER_UNIT)));
  846. /* PARMLOC now refers to the parameter in the arglist
  847. in the form in which it is passed.
  848. Now output code if necessary to convert it to
  849. the type in which this function declares it,
  850. and store a reference to that value in DECL_RTL.
  851. This reference may be the same as PARMLOC
  852. if no conversion is required. */
  853. if (GET_MODE (parmloc) == BLKmode)
  854. DECL_RTL (parm) = parmloc;
  855. else if (! (TREE_ADDRESSABLE (parm)
  856. || (obey_regdecls && ! TREE_REGDECL (parm))))
  857. {
  858. /* Store the parm in a register during the function. */
  859. register rtx parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
  860. DECL_RTL (parm) = parmreg;
  861. /* Copy the value into the register. */
  862. if (GET_MODE (parmreg) != GET_MODE (parmloc))
  863. convert_move (parmreg, parmloc, 0);
  864. else
  865. emit_move_insn (parmreg, parmloc);
  866. /* Mark the register as eliminable if we did no conversion. */
  867. if (GET_MODE (parmreg) == GET_MODE (parmloc))
  868. REG_NOTES (get_last_insn ()) = gen_rtx (EXPR_LIST, REG_CONST,
  869. parmreg, 0);
  870. /* For pointer data type, suggest pointer register. */
  871. if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  872. mark_reg_pointer (parmreg);
  873. }
  874. else if (GET_MODE (parmloc) != TYPE_MODE (TREE_TYPE (parm)))
  875. {
  876. /* Don't store in a register, but conversion is required.
  877. Convert it via a register and store back in the parm list
  878. in the new format. The debugger will expect this anyway. */
  879. register rtx parmlcl
  880. = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (parm)),
  881. copy_rtx (XEXP (parmloc, 0)));
  882. register rtx parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
  883. convert_move (parmreg, parmloc, 0);
  884. emit_move_insn (parmlcl, parmreg);
  885. DECL_RTL (parm) = parmlcl;
  886. }
  887. else
  888. DECL_RTL (parm) = parmloc;
  889. }
  890. max_parm_reg = max_reg_num ();
  891. }
  892. /* Allocation of space for returned structure values.
  893. During the rtl generation pass, `get_structure_value_addr'
  894. is called from time to time to request the address of a block in our
  895. stack frame in which called functions will store the structures
  896. they are returning. The same space is used for all of these blocks.
  897. `get_structure_value_addr' records the maximum block size needed.
  898. At the end of generation `allocate_structure_value_space' is
  899. called to adjust `frame_offset' so that the needed space is allocated. */
  900. rtx
  901. get_structure_value_addr (sizex)
  902. rtx sizex;
  903. {
  904. register int size;
  905. if (GET_CODE (sizex) != CONST_INT)
  906. abort ();
  907. size = INTVAL (sizex);
  908. /* Round up to a multiple of the main allocation unit. */
  909. size = (((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
  910. / (BIGGEST_ALIGNMENT / BITS_PER_UNIT))
  911. * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  912. if (size > max_structure_value_size)
  913. {
  914. max_structure_value_size = size;
  915. }
  916. #ifdef FRAME_GROWS_DOWNWARD
  917. return gen_rtx (PLUS, Pmode,
  918. gen_rtx (REG, SImode, FRAME_POINTER_REGNUM),
  919. gen_rtx (CONST_INT, VOIDmode, frame_offset - size));
  920. #else
  921. return gen_rtx (PLUS, Pmode,
  922. gen_rtx (REG, SImode, FRAME_POINTER_REGNUM),
  923. gen_rtx (CONST_INT, VOIDmode, frame_offset));
  924. #endif
  925. }
  926. static void
  927. allocate_structure_value_space ()
  928. {
  929. #ifdef FRAME_GROWS_DOWNWARD
  930. frame_offset -= max_structure_value_size;
  931. #else
  932. frame_offset += max_structure_value_size;
  933. #endif
  934. /* Allow `assign_stack_local' to be used once again. */
  935. max_structure_value_size = 0;
  936. }
  937. /* Main entry point: generate the rtl code for a function SUBR
  938. represented as a tree. Returns the first insn.
  939. NO_CSE is 1 if cse is not going to be done;
  940. this is passed because when cse is to be done it is sometimes
  941. desirable to generate excess temporaries at this stage to give
  942. cse an opportunity to go to work. */
  943. rtx
  944. expand_function (subr, no_cse)
  945. tree subr;
  946. int no_cse;
  947. {
  948. register int i;
  949. this_function = subr;
  950. cse_not_expected = no_cse;
  951. init_queue ();
  952. #ifdef FUNCTION_EPILOGUE
  953. return_label = gen_label_rtx ();
  954. #endif
  955. max_structure_value_size = 0;
  956. /* We are not currently within any block. */
  957. block_stack = 0;
  958. tail_recursion_label = 0;
  959. clear_pending_stack_adjust ();
  960. clear_current_args_size ();
  961. /* Prevent ever trying to delete the first instruction of a function.
  962. Also tell final how to output a linenum before the function prologue. */
  963. emit_note (DECL_SOURCE_FILE (subr), DECL_SOURCE_LINE (subr));
  964. /* Make sure first insn is a note even if we don't want linenums.
  965. This makes sure the first insn will never be deleted.
  966. Also, final expects a note to appear there. */
  967. emit_note (0, NOTE_INSN_DELETED);
  968. /* Initialize rtx for parameters and local variables.
  969. In some cases this requires emitting insns. */
  970. assign_parms (subr);
  971. /* After the parm initializations is where the tail-recursion label
  972. should go, if we end up needing one. */
  973. tail_recursion_reentry = get_last_insn ();
  974. assign_all_vars (DECL_INITIAL (subr));
  975. /* Initialize rtx used to return the value. */
  976. if (DECL_MODE (DECL_RESULT (subr)) == BLKmode)
  977. {
  978. /* Returning something that won't go in a register. */
  979. register rtx value_address;
  980. /* Expect to be passed the address of a place to store the value,
  981. in the same register that is normally used to return values. */
  982. value_address = gen_reg_rtx (Pmode);
  983. emit_move_insn (value_address,
  984. gen_rtx (REG, Pmode, STRUCT_VALUE_REGNUM));
  985. DECL_RTL (DECL_RESULT (subr))
  986. = gen_rtx (MEM, DECL_MODE (DECL_RESULT (subr)),
  987. value_address);
  988. }
  989. else
  990. DECL_RTL (DECL_RESULT (subr))
  991. = gen_rtx (REG, DECL_MODE (DECL_RESULT (subr)),
  992. FUNCTION_VALUE_REGNUM);
  993. /* If doing stupid allocation, mark parms as born here. */
  994. if (obey_regdecls)
  995. {
  996. rtx insn = get_last_insn ();
  997. reg_birth_insn = (rtx *) oballoc (first_temp_reg_num * sizeof (rtx));
  998. reg_death_insn = (rtx *) oballoc (first_temp_reg_num * sizeof (rtx));
  999. bzero (reg_birth_insn, first_temp_reg_num * sizeof (rtx));
  1000. bzero (reg_death_insn, first_temp_reg_num * sizeof (rtx));
  1001. for (i = 0; i < max_parm_reg; i++)
  1002. reg_birth_insn[i] = insn;
  1003. }
  1004. /* Don't generate a NOTE_INSN_BLOCK_BEG for the function's topmost block.
  1005. final will do it specially, in order to make it come before
  1006. the function prologue, and we don't want to have two of them. */
  1007. inhibit_block_beg = 1;
  1008. /* Generate the actual code for the function.
  1009. `assign_stack_local' may not be called again
  1010. until after `allocate_structure_value_space'. */
  1011. expand_stmt (DECL_INITIAL (subr), 0);
  1012. /* If doing stupid register allocation,
  1013. mark any argument variables as dying here in the last insn generated
  1014. (which is always an end-of-block comment, so it is never deleted). */
  1015. if (obey_regdecls)
  1016. {
  1017. rtx insn = get_last_insn ();
  1018. for (i = 0; i < max_parm_reg; i++)
  1019. reg_death_insn[i] = insn;
  1020. }
  1021. /* Return insn or function epilogue ignore the stack pointer. */
  1022. clear_pending_stack_adjust ();
  1023. /* If we require a true epilogue,
  1024. put here the label that return statements jump to.
  1025. If there will be no epilogue, write a return instruction. */
  1026. #ifdef FUNCTION_EPILOGUE
  1027. emit_label (return_label);
  1028. #else
  1029. emit_jump_insn (gen_return ());
  1030. #endif
  1031. allocate_structure_value_space ();
  1032. return get_insns ();
  1033. }