final.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /* Convert RTL to assembler code and output it, for GNU 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 is the final pass of the compiler.
  18. It looks at the rtl code for a function and outputs assembler code.
  19. Call `final_start_function' to output the assembler code for function entry,
  20. `final' to output assembler code for some RTL code,
  21. `final_end_function' to output assembler code for function exit.
  22. If a function is compiled in several pieces, each piece is
  23. output separately with `final'.
  24. Some optimizations are also done at this level.
  25. Move instructions that were made unnecessary by good register allocation
  26. are detected and omitted from the output. (Though most of these
  27. are removed by the last jump pass.)
  28. Instructions to set the condition codes are omitted when it can be
  29. seen that the condition codes already had the desired values.
  30. In some cases it is sufficient if the inherited condition codes
  31. have related values, but this may require the following insn
  32. (the one that tests the condition codes) to be modified.
  33. The code for the function prologue and epilogue are generated
  34. directly as assembler code by the macros FUNCTION_PROLOGUE and
  35. FUNCTION_EPILOGUE. Those instructions never exist as rtl. */
  36. #include <stdio.h>
  37. #include "config.h"
  38. #include "rtl.h"
  39. #include "regs.h"
  40. #include "insn-config.h"
  41. #include "recog.h"
  42. #include "conditions.h"
  43. #include "gdbfiles.h"
  44. /* .stabd code for line number. */
  45. #ifndef N_SLINE
  46. #define N_SLINE 0x44
  47. #endif
  48. /* .stabs code for included file name. */
  49. #ifndef N_SOL
  50. #define N_SOL 0x84
  51. #endif
  52. #define min(A,B) ((A) < (B) ? (A) : (B))
  53. void output_asm_insn ();
  54. static void alter_subreg ();
  55. static int alter_cond ();
  56. static void output_asm_label ();
  57. static void output_operand ();
  58. void output_address ();
  59. void output_addr_const ();
  60. static void output_source_line ();
  61. static char *reg_name[] = REGISTER_NAMES;
  62. /* File in which assembler code is being written. */
  63. extern FILE *asm_out_file;
  64. /* All the symbol-blocks (levels of scoping) in the compilation
  65. are assigned sequence numbers in order of appearance of the
  66. beginnings of the symbol-blocks. Both final and dbxout do this,
  67. and assume that they will both give the same number to each block.
  68. Final uses these sequence numbers to generate assembler label names
  69. LBBnnn and LBEnnn for the beginning and end of the symbol-block.
  70. Dbxout uses the sequence nunbers to generate references to the same labels
  71. from the dbx debugging information. */
  72. static next_block_index;
  73. /* Chain of all `struct gdbfile's. */
  74. struct gdbfile *gdbfiles;
  75. /* `struct gdbfile' for the last file we wrote a line number for. */
  76. static struct gdbfile *current_gdbfile;
  77. /* Filenum to assign to the next distinct source file encountered. */
  78. static int next_gdb_filenum;
  79. /* This variable contains machine-dependent flags (defined in tm-...h)
  80. set and examined by output routines
  81. that describe how to interpret the condition codes properly. */
  82. CC_STATUS cc_status;
  83. /* During output of an insn, this contains a copy of cc_status
  84. from before the insn. */
  85. CC_STATUS cc_prev_status;
  86. /* Last source file name mentioned in a NOTE insn. */
  87. static char *lastfile;
  88. /* Indexed by hardware reg number, is 1 if that register is ever
  89. used in the current function.
  90. In life_analysis, or in stupid_life_analysis, this is set
  91. up to record the hard regs used explicitly. Reload adds
  92. in the hard regs used for holding pseudo regs. Final uses
  93. it to generate the code in the function prologue and epilogue
  94. to save and restore registers as needed. */
  95. char regs_ever_live[FIRST_PSEUDO_REGISTER];
  96. /* Nonzero means current function must be given a frame pointer.
  97. Set in stmt.c if anything is allocated on the stack there.
  98. Set in reload1.c if anything is allocated on the stack there. */
  99. int frame_pointer_needed;
  100. /* Assign unique numbers to labels generated for profiling. */
  101. int profile_label_no;
  102. /* Length so far allocated in PENDING_BLOCKS. */
  103. static int max_block_depth;
  104. /* Stack of sequence numbers of symbol-blocks of which we have seen the
  105. beginning but not yet the end. Sequence numbers are assigned at
  106. the beginning; this stack allows us to find the sequence number
  107. of a block that is ending. */
  108. static int *pending_blocks;
  109. /* Number of elements currently in use in PENDING_BLOCKS. */
  110. static int block_depth;
  111. /* Nonzero if have enabled APP processing of our assembler output. */
  112. static int app_on;
  113. /* Initialize data in final at the beginning of a compilation. */
  114. void
  115. init_final (filename)
  116. char *filename;
  117. {
  118. next_block_index = 2;
  119. lastfile = filename;
  120. app_on = 0;
  121. max_block_depth = 20;
  122. pending_blocks = (int *) xmalloc (20 * sizeof *pending_blocks);
  123. gdbfiles = 0;
  124. next_gdb_filenum = 0;
  125. }
  126. /* Enable APP processing of subsequent output.
  127. Used before the output from an `asm' statement. */
  128. void
  129. app_enable ()
  130. {
  131. if (! app_on)
  132. {
  133. fprintf (asm_out_file, ASM_APP_ON);
  134. app_on = 1;
  135. }
  136. }
  137. /* Enable APP processing of subsequent output.
  138. Called from varasm.c before most kinds of output. */
  139. void
  140. app_disable ()
  141. {
  142. if (app_on)
  143. {
  144. fprintf (asm_out_file, ASM_APP_OFF);
  145. app_on = 0;
  146. }
  147. }
  148. /* Output assembler code for the start of a function,
  149. and initialize some of the variables in this file
  150. for the new function. The label for the function and associated
  151. assembler pseudo-ops have already been output in `assemble_function'.
  152. FIRST is the first insn of the rtl for the function being compiled.
  153. FILE is the file to write assembler code to.
  154. WRITE_SYMBOLS is 1 for gdb symbols, 2 for dbx symbols.
  155. OPTIMIZE is nonzero if we should eliminate redundant
  156. test and compare insns. */
  157. void
  158. final_start_function (first, file, write_symbols, optimize)
  159. rtx first;
  160. FILE *file;
  161. int write_symbols;
  162. int optimize;
  163. {
  164. extern int profile_flag;
  165. init_recog ();
  166. block_depth = 0;
  167. /* Record beginning of the symbol-block that's the entire function. */
  168. if (write_symbols == 1)
  169. {
  170. pending_blocks[block_depth++] = next_block_index;
  171. fprintf (file, "\t.gdbbeg %d\n", next_block_index++);
  172. }
  173. /* Initial line number is supposed to be output
  174. before the function's prologue and label
  175. so that the function's address will not appear to be
  176. in the last statement of the preceding function. */
  177. if (NOTE_LINE_NUMBER (first) != NOTE_INSN_DELETED)
  178. output_source_line (file, first, write_symbols);
  179. #ifdef FUNCTION_PROLOGUE
  180. /* First output the function prologue: code to set up the stack frame. */
  181. FUNCTION_PROLOGUE (file, get_frame_size ());
  182. #endif
  183. if (profile_flag)
  184. {
  185. int align = min (BIGGEST_ALIGNMENT, BITS_PER_WORD);
  186. fprintf (file, "\t%s\n", DATA_SECTION_ASM_OP);
  187. ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
  188. ASM_OUTPUT_INTERNAL_LABEL (file, "LP", profile_label_no);
  189. assemble_integer_zero ();
  190. fprintf (file, "\t%s\n", TEXT_SECTION_ASM_OP);
  191. FUNCTION_PROFILER (file, profile_label_no);
  192. profile_label_no++;
  193. }
  194. CC_STATUS_INIT;
  195. }
  196. /* Output assembler code for the end of a function.
  197. For clarity, args are same as those of `final_start_function'
  198. even though not all of them are needed. */
  199. void
  200. final_end_function (first, file, write_symbols, optimize)
  201. rtx first;
  202. FILE *file;
  203. int write_symbols;
  204. int optimize;
  205. {
  206. if (app_on)
  207. {
  208. fprintf (file, ASM_APP_OFF);
  209. app_on = 0;
  210. }
  211. if (write_symbols == 1)
  212. fprintf (file, "\t.gdbend %d\n", pending_blocks[0]);
  213. #ifdef FUNCTION_EPILOGUE
  214. /* Finally, output the function epilogue:
  215. code to restore the stack frame and return to the caller. */
  216. FUNCTION_EPILOGUE (file, get_frame_size ());
  217. #endif
  218. /* If FUNCTION_EPILOGUE is not defined, then the function body
  219. itself contains return instructions wherever needed. */
  220. }
  221. /* Output assembler code for some insns: all or part of a function.
  222. For description of args, see `final_start_function', above. */
  223. void
  224. final (first, file, write_symbols, optimize)
  225. rtx first;
  226. FILE *file;
  227. int write_symbols;
  228. int optimize;
  229. {
  230. register rtx insn;
  231. register int i;
  232. for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
  233. {
  234. switch (GET_CODE (insn))
  235. {
  236. case NOTE:
  237. if (! write_symbols)
  238. break;
  239. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
  240. abort (); /* Obsolete; shouldn't appear */
  241. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  242. || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  243. break;
  244. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
  245. break; /* An insn that was "deleted" */
  246. if (app_on)
  247. {
  248. fprintf (file, ASM_APP_OFF);
  249. app_on = 0;
  250. }
  251. if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
  252. {
  253. /* Beginning of a symbol-block. Assign it a sequence number
  254. and push the number onto the stack PENDING_BLOCKS. */
  255. if (block_depth == max_block_depth)
  256. {
  257. /* PENDING_BLOCKS is full; make it longer. */
  258. max_block_depth *= 2;
  259. pending_blocks
  260. = (int *) xrealloc (pending_blocks,
  261. max_block_depth * sizeof (int));
  262. }
  263. pending_blocks[block_depth++] = next_block_index;
  264. /* Output debugging info about the symbol-block beginning. */
  265. if (write_symbols == 2)
  266. ASM_OUTPUT_INTERNAL_LABEL (file, "LBB", next_block_index);
  267. else
  268. fprintf (file, "\t.gdbbeg %d\n", next_block_index);
  269. next_block_index++;
  270. }
  271. else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
  272. {
  273. /* End of a symbol-block. Pop its sequence number off
  274. PENDING_BLOCKS and output debugging info based on that. */
  275. if (write_symbols == 2)
  276. {
  277. if (block_depth > 0)
  278. ASM_OUTPUT_INTERNAL_LABEL (file, "LBE",
  279. pending_blocks[--block_depth]);
  280. }
  281. else
  282. fprintf (file, "\t.gdbend %d\n", pending_blocks[--block_depth]);
  283. }
  284. else if (NOTE_LINE_NUMBER (insn) > 0)
  285. /* This note is a line-number. */
  286. output_source_line (file, insn, write_symbols);
  287. break;
  288. case BARRIER:
  289. break;
  290. case CODE_LABEL:
  291. if (app_on)
  292. {
  293. fprintf (file, ASM_APP_OFF);
  294. app_on = 0;
  295. }
  296. #ifdef ASM_OUTPUT_CASE_LABEL
  297. if (NEXT_INSN (insn) != 0
  298. && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN)
  299. {
  300. rtx nextbody = PATTERN (NEXT_INSN (insn));
  301. /* If this label is followed by a jump-table,
  302. output the two of them together in a special way. */
  303. if (GET_CODE (nextbody) == ADDR_VEC
  304. || GET_CODE (nextbody) == ADDR_DIFF_VEC)
  305. {
  306. ASM_OUTPUT_CASE_LABEL (file, "L", CODE_LABEL_NUMBER (insn),
  307. NEXT_INSN (insn));
  308. break;
  309. }
  310. }
  311. #endif
  312. ASM_OUTPUT_INTERNAL_LABEL (file, "L", CODE_LABEL_NUMBER (insn));
  313. CC_STATUS_INIT;
  314. break;
  315. default:
  316. {
  317. register rtx body = PATTERN (insn);
  318. int insn_code_number;
  319. char *template;
  320. /* An INSN, JUMP_INSN or CALL_INSN.
  321. First check for special kinds that recog doesn't recognize. */
  322. if (GET_CODE (body) == USE /* These are just declarations */
  323. || GET_CODE (body) == CLOBBER)
  324. break;
  325. if (GET_CODE (body) == ASM_INPUT)
  326. {
  327. if (! app_on)
  328. {
  329. fprintf (file, ASM_APP_ON);
  330. app_on = 1;
  331. }
  332. fprintf (asm_out_file, "\t%s\n", XSTR (body, 0));
  333. /* There's no telling what that did to the condition codes. */
  334. CC_STATUS_INIT;
  335. break;
  336. }
  337. /* Detect `asm' construct with operands. */
  338. if (asm_noperands (body) > 0)
  339. {
  340. int noperands = asm_noperands (body);
  341. rtx *ops = (rtx *) malloc (noperands * sizeof (rtx));
  342. char *string;
  343. if (! app_on)
  344. {
  345. fprintf (file, ASM_APP_ON);
  346. app_on = 1;
  347. }
  348. /* Get out the operand values. */
  349. string = decode_asm_operands (body, ops, 0, 0, 0);
  350. /* Output the insn using them. */
  351. output_asm_insn (string, ops);
  352. /* There's no telling what that did to the condition codes. */
  353. CC_STATUS_INIT;
  354. break;
  355. }
  356. if (app_on)
  357. {
  358. fprintf (file, ASM_APP_OFF);
  359. app_on = 0;
  360. }
  361. /* Detect insns that are really jump-tables
  362. and output them as such. */
  363. if (GET_CODE (body) == ADDR_VEC)
  364. {
  365. enum machine_mode mode = GET_MODE (body);
  366. register int vlen, idx;
  367. vlen = XVECLEN (body, 0);
  368. for (idx = 0; idx < vlen; idx++)
  369. ASM_OUTPUT_ADDR_VEC_ELT (file,
  370. CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
  371. break;
  372. }
  373. if (GET_CODE (body) == ADDR_DIFF_VEC)
  374. {
  375. enum machine_mode mode = GET_MODE (body);
  376. register int vlen, idx;
  377. vlen = XVECLEN (body, 1);
  378. for (idx = 0; idx < vlen; idx++)
  379. ASM_OUTPUT_ADDR_DIFF_ELT (file,
  380. CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
  381. CODE_LABEL_NUMBER (XEXP (XEXP (body, 0), 0)));
  382. break;
  383. }
  384. /* We have a real machine instruction as rtl. */
  385. body = PATTERN (insn);
  386. /* Check for redundant move insns moving a reg into itself.
  387. This takes little time and does not affect the semantics
  388. so we do it even when `optimize' is 0.
  389. It is not safe to do this for memory references;
  390. we would not know if they were volatile. */
  391. if (GET_CODE (body) == SET
  392. && SET_DEST (body) == SET_SRC (body)
  393. && GET_CODE (SET_DEST (body)) == REG)
  394. break;
  395. /* Check for redundant test and compare instructions
  396. (when the condition codes are already set up as desired).
  397. This is done only when optimizing; if not optimizing,
  398. it should be possible for the user to alter a variable
  399. with the debugger in between statements
  400. and the next statement should reexamine the variable
  401. to compute the condition codes. */
  402. if (optimize
  403. && GET_CODE (body) == SET
  404. && GET_CODE (SET_DEST (body)) == CC0)
  405. {
  406. if (GET_CODE (SET_SRC (body)) == SUBREG)
  407. alter_subreg (SET_SRC (body));
  408. if ((cc_status.value1 != 0
  409. && rtx_equal_p (SET_SRC (body), cc_status.value1))
  410. || (cc_status.value2 != 0
  411. && rtx_equal_p (SET_SRC (body), cc_status.value2)))
  412. {
  413. /* Don't delete insn if has an addressing side-effect */
  414. if (! find_reg_note (insn, REG_INC, 0))
  415. break;
  416. }
  417. }
  418. /* If this is a conditional branch, maybe modify it
  419. if the cc's are in a nonstandard state
  420. so that it accomplishes the same thing that it would
  421. do straightforwardly if the cc's were set up normally. */
  422. if (cc_status.flags != 0
  423. && GET_CODE (insn) == JUMP_INSN
  424. && GET_CODE (body) == SET
  425. && SET_DEST (body) == pc_rtx
  426. && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE)
  427. {
  428. /* This function may alter the contents of its argument
  429. and clear some of the cc_status.flags bits.
  430. It may also return 1 meaning condition now always true
  431. or -1 meaning condition now always false
  432. or 2 meaning condition nontrivial but altered. */
  433. register int result = alter_cond (XEXP (SET_SRC (body), 0));
  434. /* If condition now has fixed value, replace the IF_THEN_ELSE
  435. with its then-operand or its else-operand. */
  436. if (result == 1)
  437. SET_SRC (body) = XEXP (SET_SRC (body), 1);
  438. if (result == -1)
  439. SET_SRC (body) = XEXP (SET_SRC (body), 2);
  440. /* The jump is now either unconditional or a no-op.
  441. If it has become a no-op, don't try to output it.
  442. (It would not be recognized.) */
  443. if (SET_SRC (body) == pc_rtx)
  444. continue;
  445. /* Rerecognize the instruction if it has changed. */
  446. if (result != 0)
  447. INSN_CODE (insn) = -1;
  448. }
  449. /* Make same adjustments to instructions that examine the
  450. condition codes without jumping. */
  451. if (cc_status.flags != 0
  452. && GET_CODE (body) == SET)
  453. switch (GET_CODE (SET_SRC (body)))
  454. {
  455. case GTU:
  456. case GT:
  457. case LTU:
  458. case LT:
  459. case GEU:
  460. case GE:
  461. case LEU:
  462. case LE:
  463. case EQ:
  464. case NE:
  465. {
  466. register int result = alter_cond (SET_SRC (body));
  467. if (result == 1)
  468. SET_SRC (body) = gen_rtx (CONST_INT, VOIDmode, -1);
  469. if (result == -1)
  470. SET_SRC (body) = const0_rtx;
  471. if (result != 0)
  472. INSN_CODE (insn) = -1;
  473. }
  474. }
  475. /* Try to recognize the instruction.
  476. If successful, verify that the operands satisfy the
  477. constraints for the instruction. Crash if they don't,
  478. since `reload' should have changed them so that they do. */
  479. insn_code_number = recog_memoized (insn);
  480. insn_extract (insn);
  481. for (i = 0; i < insn_n_operands[insn_code_number]; i++)
  482. if (GET_CODE (recog_operand[i]) == SUBREG)
  483. alter_subreg (recog_operand[i]);
  484. #ifdef REGISTER_CONSTRAINTS
  485. if (! constrain_operands (insn_code_number))
  486. abort ();
  487. #endif
  488. cc_prev_status = cc_status;
  489. /* Update `cc_status' for this instruction.
  490. The instruction's output routine may change it further.
  491. This should be a no-op for jump instructions
  492. because their output routines may need to examine `cc_status',
  493. below. That's ok since jump insns don't normally alter
  494. the condition codes. */
  495. NOTICE_UPDATE_CC (body);
  496. /* If the proper template needs to be chosen by some C code,
  497. run that code and get the real template. */
  498. template = insn_template[insn_code_number];
  499. if (template == 0)
  500. template = insn_outfun[insn_code_number] (recog_operand, insn);
  501. /* Output assembler code from the template. */
  502. output_asm_insn (template, recog_operand);
  503. }
  504. }
  505. }
  506. }
  507. /* Set up FILENAME as the current file for GDB line-number output. */
  508. void
  509. set_current_gdbfile (filename)
  510. char *filename;
  511. {
  512. register struct gdbfile *f;
  513. for (f = gdbfiles; f; f = f->next)
  514. if (!strcmp (f->name, filename))
  515. break;
  516. if (!f)
  517. {
  518. f = (struct gdbfile *) permalloc (sizeof (struct gdbfile));
  519. f->next = gdbfiles;
  520. gdbfiles = f;
  521. f->name = filename;
  522. f->filenum = next_gdb_filenum++;
  523. f->nlines = 0;
  524. }
  525. current_gdbfile = f;
  526. lastfile = filename;
  527. }
  528. /* Output debugging info to the assembler file FILE
  529. based on the NOTE-insn INSN, assumed to be a line number. */
  530. static void
  531. output_source_line (file, insn, write_symbols)
  532. FILE *file;
  533. rtx insn;
  534. int write_symbols;
  535. {
  536. register char *filename = NOTE_SOURCE_FILE (insn);
  537. if (write_symbols == 1)
  538. {
  539. /* Output GDB-format line number info. */
  540. /* If this is not the same source file as last time,
  541. find or assign a GDB-file-number to this file. */
  542. if (filename && (lastfile == 0 || strcmp (filename, lastfile)
  543. || current_gdbfile == 0))
  544. set_current_gdbfile (filename);
  545. ++current_gdbfile->nlines;
  546. fprintf (file, "\t.gdbline %d,%d\n",
  547. current_gdbfile->filenum, NOTE_LINE_NUMBER (insn));
  548. }
  549. else
  550. {
  551. /* Write DBX line number data. */
  552. if (filename && (lastfile == 0 || strcmp (filename, lastfile)))
  553. #ifdef ASM_OUTPUT_SOURCE_FILENAME
  554. ASM_OUTPUT_SOURCE_FILENAME (file, filename);
  555. #else
  556. fprintf (file, "\t.stabs \"%s\",%d,0,0,Ltext\n",
  557. filename, N_SOL);
  558. #endif
  559. lastfile = filename;
  560. #ifdef ASM_OUTPUT_SOURCE_LINE
  561. ASM_OUTPUT_SOURCE_LINE (file, NOTE_LINE_NUMBER (insn));
  562. #else
  563. fprintf (file, "\t.stabd %d,0,%d\n",
  564. N_SLINE, NOTE_LINE_NUMBER (insn));
  565. #endif
  566. }
  567. }
  568. /* If X is a SUBREG, replace it with a REG or a MEM,
  569. based on the thing it is a subreg of. */
  570. static void
  571. alter_subreg (x)
  572. register rtx x;
  573. {
  574. register rtx y = SUBREG_REG (x);
  575. if (GET_CODE (y) == SUBREG)
  576. alter_subreg (y);
  577. if (GET_CODE (y) == REG)
  578. {
  579. /* If the containing reg really gets a hard reg, so do we. */
  580. PUT_CODE (x, REG);
  581. REGNO (x) = REGNO (y) + SUBREG_WORD (x);
  582. }
  583. else if (GET_CODE (y) == MEM)
  584. {
  585. register int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
  586. #ifdef BYTES_BIG_ENDIAN
  587. offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x)))
  588. - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (y))));
  589. #endif
  590. PUT_CODE (x, MEM);
  591. XEXP (x, 0) = plus_constant (XEXP (y, 0), offset);
  592. }
  593. }
  594. /* Do alter_subreg on all the SUBREGs contained in X. */
  595. static rtx
  596. walk_alter_subreg (x)
  597. rtx x;
  598. {
  599. switch (GET_CODE (x))
  600. {
  601. case PLUS:
  602. case MULT:
  603. XEXP (x, 0) = walk_alter_subreg (XEXP (x, 0));
  604. XEXP (x, 1) = walk_alter_subreg (XEXP (x, 1));
  605. break;
  606. case MEM:
  607. XEXP (x, 0) = walk_alter_subreg (XEXP (x, 0));
  608. break;
  609. case SUBREG:
  610. alter_subreg (x);
  611. }
  612. return x;
  613. }
  614. /* Given BODY, the body of a jump instruction, alter the jump condition
  615. as required by the bits that are set in cc_status.flags.
  616. Not all of the bits there can be handled at this level in all cases.
  617. The bits that are taken care of here are cleared.
  618. The value is normally 0.
  619. In this case, COND itself has usually been altered.
  620. 1 means that the condition has become always true.
  621. -1 means that the condition has become always false. */
  622. static int
  623. alter_cond (cond)
  624. register rtx cond;
  625. {
  626. int value = 0;
  627. if (cc_status.flags & CC_REVERSED)
  628. {
  629. value = 2;
  630. switch (GET_CODE (cond))
  631. {
  632. case LE:
  633. PUT_CODE (cond, GE);
  634. break;
  635. case GE:
  636. PUT_CODE (cond, LE);
  637. break;
  638. case LT:
  639. PUT_CODE (cond, GT);
  640. break;
  641. case GT:
  642. PUT_CODE (cond, LT);
  643. break;
  644. case LEU:
  645. PUT_CODE (cond, GEU);
  646. break;
  647. case GEU:
  648. PUT_CODE (cond, LEU);
  649. break;
  650. case LTU:
  651. PUT_CODE (cond, GTU);
  652. break;
  653. case GTU:
  654. PUT_CODE (cond, LTU);
  655. break;
  656. }
  657. }
  658. if (cond != 0 && cc_status.flags & CC_NOT_POSITIVE)
  659. switch (GET_CODE (cond))
  660. {
  661. case LE:
  662. case LEU:
  663. case GEU:
  664. /* Jump becomes unconditional. */
  665. return 1;
  666. case GT:
  667. case GTU:
  668. case LTU:
  669. /* Jump becomes no-op. */
  670. return -1;
  671. case GE:
  672. PUT_CODE (cond, EQ);
  673. value = 2;
  674. break;
  675. case LT:
  676. PUT_CODE (cond, NE);
  677. value = 2;
  678. break;
  679. }
  680. if (cond != 0 && cc_status.flags & CC_NOT_NEGATIVE)
  681. switch (GET_CODE (cond))
  682. {
  683. case GE:
  684. case GEU:
  685. /* Jump becomes unconditional. */
  686. return 1;
  687. case LT:
  688. case LTU:
  689. /* Jump becomes no-op. */
  690. return -1;
  691. case LE:
  692. case LEU:
  693. PUT_CODE (cond, EQ);
  694. value = 2;
  695. break;
  696. case GT:
  697. case GTU:
  698. PUT_CODE (cond, NE);
  699. value = 2;
  700. break;
  701. }
  702. if (cond != 0 && cc_status.flags & CC_NO_OVERFLOW)
  703. switch (GET_CODE (cond))
  704. {
  705. case GEU:
  706. /* Jump becomes unconditional. */
  707. return 1;
  708. case LEU:
  709. PUT_CODE (cond, EQ);
  710. value = 2;
  711. break;
  712. case GTU:
  713. PUT_CODE (cond, NE);
  714. value = 2;
  715. break;
  716. case LTU:
  717. /* Jump becomes no-op. */
  718. return -1;
  719. }
  720. return value;
  721. }
  722. /* Output of assembler code from a template, and its subroutines. */
  723. /* Output text from TEMPLATE to the assembler output file,
  724. obeying %-directions to substitute operands taken from
  725. the vector OPERANDS.
  726. %N (for N a digit) means print operand N in usual manner.
  727. %lN means require operand N to be a CODE_LABEL or LABEL_REF
  728. and print the label name with no punctuation.
  729. %cN means require operand N to be a constant
  730. and print the constant expression with no punctuation.
  731. %aN means expect operand N to be a memory address
  732. (not a memory reference!) and print a reference
  733. to that address.
  734. %nN means expect operand N to be a constant
  735. and print a constant expression for minus the value
  736. of the operand, with no other punctuation. */
  737. void
  738. output_asm_insn (template, operands)
  739. char *template;
  740. rtx *operands;
  741. {
  742. register char *p;
  743. register int c;
  744. /* An insn may return a null string template
  745. in a case where no assembler code is needed. */
  746. if (*template == 0)
  747. return;
  748. p = template;
  749. putc ('\t', asm_out_file);
  750. #ifdef ASM_OUTPUT_OPCODE
  751. ASM_OUTPUT_OPCODE (asm_out_file, p);
  752. #endif
  753. while (c = *p++)
  754. {
  755. #ifdef ASM_OUTPUT_OPCODE
  756. if (c == '\n')
  757. {
  758. putc (c, asm_out_file);
  759. while ((c = *p) == '\t')
  760. {
  761. putc (c, asm_out_file);
  762. p++;
  763. }
  764. ASM_OUTPUT_OPCODE (asm_out_file, p);
  765. }
  766. else
  767. #endif
  768. if (c != '%')
  769. putc (c, asm_out_file);
  770. else
  771. {
  772. /* %% outputs a single %. */
  773. if (*p == '%')
  774. {
  775. p++;
  776. putc (c, asm_out_file);
  777. }
  778. /* % followed by a letter and some digits
  779. outputs an operand in a special way depending on the letter.
  780. Letters `acln' are implemented here.
  781. Other letters are passed to `output_operand' so that
  782. the PRINT_OPERAND macro can define them. */
  783. else if ((*p >= 'a' && *p <= 'z')
  784. || (*p >= 'A' && *p <= 'Z'))
  785. {
  786. int letter = *p++;
  787. c = atoi (p);
  788. if (letter == 'l')
  789. output_asm_label (operands[c]);
  790. else if (letter == 'a')
  791. output_address (operands[c]);
  792. else if (letter == 'c')
  793. {
  794. if (CONSTANT_ADDRESS_P (operands[c]))
  795. output_addr_const (asm_out_file, operands[c]);
  796. else
  797. output_operand (operands[c], 'c');
  798. }
  799. else if (letter == 'n')
  800. {
  801. if (GET_CODE (operands[c]) == CONST_INT)
  802. fprintf (asm_out_file, "%d", - INTVAL (operands[c]));
  803. else
  804. {
  805. putc ('-', asm_out_file);
  806. output_addr_const (asm_out_file, operands[c]);
  807. }
  808. }
  809. else if (*p >= '0' && *p <= '9')
  810. output_operand (operands[c], letter);
  811. else
  812. /* No operand-number follows the letter. */
  813. output_operand (0, letter);
  814. while ((c = *p) >= '0' && c <= '9') p++;
  815. }
  816. /* % followed by a digit outputs an operand the default way. */
  817. else if (*p >= '0' && *p <= '9')
  818. {
  819. c = atoi (p);
  820. output_operand (operands[c], 0);
  821. while ((c = *p) >= '0' && c <= '9') p++;
  822. }
  823. /* % followed by punctuation: output something for that
  824. punctuation character alone, with no operand.
  825. The PRINT_OPERAND macro decides what is actually done. */
  826. else
  827. output_operand (0, *p++);
  828. }
  829. }
  830. putc ('\n', asm_out_file);
  831. }
  832. static void
  833. output_asm_label (x)
  834. rtx x;
  835. {
  836. char buf[20];
  837. if (GET_CODE (x) == LABEL_REF)
  838. ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (x, 0)));
  839. else if (GET_CODE (x) == CODE_LABEL)
  840. ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
  841. else
  842. abort ();
  843. assemble_name (asm_out_file, buf);
  844. }
  845. /* Print operand X using machine-dependent assembler syntax.
  846. The macro PRINT_OPERAND is defined just to control this function.
  847. CODE is a non-digit that preceded the operand-number in the % spec,
  848. such as 'z' if the spec was `%z3'. CODE is 0 if there was no char
  849. between the % and the digits.
  850. When CODE is a non-letter, X is 0.
  851. The meanings of the letters are machine-dependent and controlled
  852. by PRINT_OPERAND. */
  853. static void
  854. output_operand (x, code)
  855. rtx x;
  856. int code;
  857. {
  858. if (x && GET_CODE (x) == SUBREG)
  859. alter_subreg (x);
  860. PRINT_OPERAND (asm_out_file, x, code);
  861. }
  862. /* Print a memory reference operand for address X
  863. using machine-dependent assembler syntax.
  864. The macro PRINT_OPERAND_ADDRESS exists just to control this function. */
  865. void
  866. output_address (x)
  867. rtx x;
  868. {
  869. walk_alter_subreg (x);
  870. PRINT_OPERAND_ADDRESS (asm_out_file, x);
  871. }
  872. /* Print an integer constant expression in assembler syntax.
  873. Addition and subtraction are the only arithmetic
  874. that may appear in these expressions. */
  875. void
  876. output_addr_const (file, x)
  877. FILE *file;
  878. rtx x;
  879. {
  880. char buf[20];
  881. restart:
  882. switch (GET_CODE (x))
  883. {
  884. case SYMBOL_REF:
  885. assemble_name (file, XSTR (x, 0));
  886. break;
  887. case LABEL_REF:
  888. ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (x, 0)));
  889. assemble_name (asm_out_file, buf);
  890. break;
  891. case CODE_LABEL:
  892. ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
  893. assemble_name (asm_out_file, buf);
  894. break;
  895. case CONST_INT:
  896. fprintf (file, "%d", INTVAL (x));
  897. break;
  898. case CONST:
  899. x = XEXP (x, 0);
  900. goto restart;
  901. case PLUS:
  902. output_addr_const (file, XEXP (x, 0));
  903. fprintf (file, "+");
  904. output_addr_const (file, XEXP (x, 1));
  905. break;
  906. case MINUS:
  907. output_addr_const (file, XEXP (x, 0));
  908. fprintf (file, "-");
  909. output_addr_const (file, XEXP (x, 1));
  910. break;
  911. default:
  912. abort ();
  913. }
  914. }