📄 final.c
字号:
/* Convert RTL to assembler code and output it, for GNU compiler. Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* This is the final pass of the compiler. It looks at the rtl code for a function and outputs assembler code. Call `final_start_function' to output the assembler code for function entry, `final' to output assembler code for some RTL code, `final_end_function' to output assembler code for function exit. If a function is compiled in several pieces, each piece is output separately with `final'. Some optimizations are also done at this level. Move instructions that were made unnecessary by good register allocation are detected and omitted from the output. (Though most of these are removed by the last jump pass.) Instructions to set the condition codes are omitted when it can be seen that the condition codes already had the desired values. In some cases it is sufficient if the inherited condition codes have related values, but this may require the following insn (the one that tests the condition codes) to be modified. The code for the function prologue and epilogue are generated directly as assembler code by the macros FUNCTION_PROLOGUE and FUNCTION_EPILOGUE. Those instructions never exist as rtl. */#include <stdio.h>#include "config.h"#include "rtl.h"#include "regs.h"#include "insn-config.h"#include "recog.h"#include "conditions.h"#include "gdbfiles.h"#include "flags.h"#include "real.h"#include "output.h"/* Get N_SLINE and N_SOL from stab.h if we can expect the file to exist. */#ifdef DBX_DEBUGGING_INFO#ifdef USG#include "gstab.h" /* If doing DBX on sysV, use our own stab.h. */#else#include <stab.h> /* On BSD, use the system's stab.h. */#endif /* not USG */#endif /* DBX_DEBUGGING_INFO *//* .stabd code for line number. */#ifndef N_SLINE#define N_SLINE 0x44#endif/* .stabs code for included file name. */#ifndef N_SOL#define N_SOL 0x84#endif#define min(A,B) ((A) < (B) ? (A) : (B))rtx peephole ();void output_asm_insn ();rtx alter_subreg ();static int alter_cond ();void output_asm_label ();static void output_operand ();void output_address ();void output_addr_const ();static void output_source_line ();rtx final_scan_insn ();/* the sdb debugger needs the line given as an offset from the beginning of the current function -wfs*/extern int sdb_begin_function_line;/* Line number of last NOTE. */static int last_linenum;/* Number of basic blocks seen so far; used if profile_block_flag is set. */static int count_basic_blocks;/* Nonzero while outputting an `asm' with operands. This means that inconsistencies are the user's fault, so don't abort. The precise value is the insn being output, to pass to error_for_asm. */static rtx this_is_asm_operands;/* Number of operands of this insn, for an `asm' with operands. */static int insn_noperands;/* File in which assembler code is being written. */extern FILE *asm_out_file;/* Compare optimization flag. */static rtx last_ignored_compare = 0;/* Flag indicating this insn is the start of a new basic block. */static int new_block = 1;/* All the symbol-blocks (levels of scoping) in the compilation are assigned sequence numbers in order of appearance of the beginnings of the symbol-blocks. Both final and dbxout do this, and assume that they will both give the same number to each block. Final uses these sequence numbers to generate assembler label names LBBnnn and LBEnnn for the beginning and end of the symbol-block. Dbxout uses the sequence nunbers to generate references to the same labels from the dbx debugging information. Sdb records this level at the beginning of each function, so that when it recurses down the declarations, it may find the current level, since it outputs the block beginning and endings at the point in the asm file, where the blocks would begin and end. */int next_block_index;/* Chain of all `struct gdbfile's. */struct gdbfile *gdbfiles;/* `struct gdbfile' for the last file we wrote a line number for. */static struct gdbfile *current_gdbfile;/* Filenum to assign to the next distinct source file encountered. */static int next_gdb_filenum;/* This variable contains machine-dependent flags (defined in tm-...h) set and examined by output routines that describe how to interpret the condition codes properly. */CC_STATUS cc_status;/* During output of an insn, this contains a copy of cc_status from before the insn. */CC_STATUS cc_prev_status;/* Last source file name mentioned in a NOTE insn. */static char *lastfile;/* Indexed by hardware reg number, is 1 if that register is ever used in the current function. In life_analysis, or in stupid_life_analysis, this is set up to record the hard regs used explicitly. Reload adds in the hard regs used for holding pseudo regs. Final uses it to generate the code in the function prologue and epilogue to save and restore registers as needed. */char regs_ever_live[FIRST_PSEUDO_REGISTER];/* Nonzero means current function must be given a frame pointer. Set in stmt.c if anything is allocated on the stack there. Set in reload1.c if anything is allocated on the stack there. */int frame_pointer_needed;/* Assign unique numbers to labels generated for profiling. */int profile_label_no;/* Length so far allocated in PENDING_BLOCKS. */static int max_block_depth;/* Stack of sequence numbers of symbol-blocks of which we have seen the beginning but not yet the end. Sequence numbers are assigned at the beginning; this stack allows us to find the sequence number of a block that is ending. */static int *pending_blocks;/* Number of elements currently in use in PENDING_BLOCKS. */static int block_depth;/* Nonzero if have enabled APP processing of our assembler output. */static int app_on;/* If we are outputting an insn sequence, this contains the sequence rtx. Zero otherwise. */rtx final_sequence;/* Initialize data in final at the beginning of a compilation. */voidinit_final (filename) char *filename;{ next_block_index = 2; lastfile = filename; app_on = 0; max_block_depth = 20; pending_blocks = (int *) xmalloc (20 * sizeof *pending_blocks); gdbfiles = 0; next_gdb_filenum = 0; final_sequence = 0;}/* Called at end of source file, to output the block-profiling table for this entire compilation. */voidend_final (filename) char *filename;{ int i; if (profile_block_flag) { char name[12]; data_section (); /* Output the main header, of 6 words: 0: 1 if this file's initialized, else 0. 1: address of file name. 2: address of table of counts. 4: number of counts in the table. 5: always 0, for compatibility with Sun. 6: extra word added by GNU: address of address table which contains addresses of basic blocks, in parallel with the table of counts. */ ASM_OUTPUT_ALIGN (asm_out_file, exact_log2 (min (UNITS_PER_WORD, BIGGEST_ALIGNMENT / BITS_PER_UNIT))); ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 0); assemble_integer_zero (); ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 1); ASM_OUTPUT_INT (asm_out_file, gen_rtx (SYMBOL_REF, Pmode, name)); ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2); ASM_OUTPUT_INT (asm_out_file, gen_rtx (SYMBOL_REF, Pmode, name)); ASM_OUTPUT_INT (asm_out_file, gen_rtx (CONST_INT, VOIDmode, count_basic_blocks)); assemble_integer_zero (); ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 3); ASM_OUTPUT_INT (asm_out_file, gen_rtx (SYMBOL_REF, Pmode, name)); /* Output the file name. */ ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 1); { int len = strlen (filename); char *data_file = (char *) alloca (len + 3); strcpy (data_file, filename); if (len > 2 && ! strcmp (".c", data_file + len - 2)) data_file[len - 2] = 0; else if (len > 2 && ! strcmp (".i", data_file + len - 2)) data_file[len - 2] = 0; else if (len > 3 && ! strcmp (".co", data_file + len - 3)) data_file[len - 3] = 0; strcat (data_file, ".d"); assemble_string (data_file, strlen (data_file) + 1); } /* Realign data section. */ ASM_OUTPUT_ALIGN (asm_out_file, exact_log2 (min (UNITS_PER_WORD, BIGGEST_ALIGNMENT / BITS_PER_UNIT))); /* Make space for the table of counts. */ ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 2); ASM_OUTPUT_SKIP (asm_out_file, UNITS_PER_WORD * count_basic_blocks); /* Output the table of addresses. */ text_section (); ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 3); for (i = 0; i < count_basic_blocks; i++) { char name[12]; ASM_GENERATE_INTERNAL_LABEL (name, "LPB", i); ASM_OUTPUT_INT (asm_out_file, gen_rtx (SYMBOL_REF, Pmode, name)); } /* End with the address of the table of addresses, so we can find it easily, as the last word in the file's text. */ ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 3); ASM_OUTPUT_INT (asm_out_file, gen_rtx (SYMBOL_REF, Pmode, name)); }}/* Enable APP processing of subsequent output. Used before the output from an `asm' statement. */voidapp_enable (){ if (! app_on) { fprintf (asm_out_file, ASM_APP_ON); app_on = 1; }}/* Enable APP processing of subsequent output. Called from varasm.c before most kinds of output. */voidapp_disable (){ if (app_on) { fprintf (asm_out_file, ASM_APP_OFF); app_on = 0; }}/* Return the number of slots filled in the current delayed branch sequence. */#ifdef HAVE_DELAYED_BRANCHintdbr_sequence_length (){ int i; int slots = 0; /* It's zero if we are not scheduling or not in a sequence. (We never count the first insn.) */ if (flag_delayed_branch && final_sequence != 0) { for (i = 1; i < XVECLEN (final_sequence, 0); i++) slots += DBR_INSN_SLOTS (XVECEXP (final_sequence, 0, i)); } return slots;}#endif/* Output assembler code for the start of a function, and initialize some of the variables in this file for the new function. The label for the function and associated assembler pseudo-ops have already been output in `assemble_function'. FIRST is the first insn of the rtl for the function being compiled. FILE is the file to write assembler code to. WRITE_SYMBOLS says which kind of debugging info to write (or none). OPTIMIZE is nonzero if we should eliminate redundant test and compare insns. */voidfinal_start_function (first, file, write_symbols, optimize) rtx first; FILE *file; enum debugger write_symbols; int optimize;{ block_depth = 0; this_is_asm_operands = 0; /* Record beginning of the symbol-block that's the entire function. */ if (write_symbols == GDB_DEBUG) { pending_blocks[block_depth++] = next_block_index; fprintf (file, "\t.gdbbeg %d\n", next_block_index++); } /* Initial line number is supposed to be output before the function's prologue and label so that the function's address will not appear to be in the last statement of the preceding function. */ if (NOTE_LINE_NUMBER (first) != NOTE_INSN_DELETED) { if (write_symbols == SDB_DEBUG) /* For sdb, let's not, but say we did. We need to set last_linenum for sdbout_function_begin, but we can't have an actual line number before the .bf symbol. (sdb_begin_function_line is not set, and other compilers don't do it.) */ last_linenum = NOTE_LINE_NUMBER (first); else output_source_line (file, first, write_symbols); } /* The Sun386i and perhaps other machines don't work right if the profiling code comes after the prologue. */#ifdef PROFILE_BEFORE_PROLOGUE if (profile_flag) profile_function (file);#endif /* PROFILE_BEFORE_PROLOGUE */#ifdef FUNCTION_PROLOGUE /* First output the function prologue: code to set up the stack frame. */ FUNCTION_PROLOGUE (file, get_frame_size ());#endif#ifdef SDB_DEBUGGING_INFO next_block_index = 1;#endif#ifdef FUNCTION_BLOCK_PROFILER if (profile_block_flag) { FUNCTION_BLOCK_PROFILER (file, profile_label_no); }#endif /* FUNCTION_BLOCK_PROFILER */#ifndef PROFILE_BEFORE_PROLOGUE if (profile_flag) profile_function (file);#endif /* not PROFILE_BEFORE_PROLOGUE */ profile_label_no++;}profile_function (file) FILE *file;{ int align = min (BIGGEST_ALIGNMENT, BITS_PER_WORD); extern int current_function_returns_struct; extern int current_function_needs_context; int sval = current_function_returns_struct; int cxt = current_function_needs_context; data_section (); ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT)); ASM_OUTPUT_INTERNAL_LABEL (file, "LP", profile_label_no); assemble_integer_zero (); text_section ();#ifdef STRUCT_VALUE_INCOMING_REGNUM if (sval) ASM_OUTPUT_REG_PUSH (file, STRUCT_VALUE_INCOMING_REGNUM);#else#ifdef STRUCT_VALUE_REGNUM if (sval) ASM_OUTPUT_REG_PUSH (file, STRUCT_VALUE_REGNUM);#endif#endif#if 0#ifdef STATIC_CHAIN_INCOMING_REGNUM if (cxt) ASM_OUTPUT_REG_PUSH (file, STATIC_CHAIN_INCOMING_REGNUM);#else#ifdef STATIC_CHAIN_REGNUM if (cxt) ASM_OUTPUT_REG_PUSH (file, STATIC_CHAIN_REGNUM);#endif#endif#endif /* 0 */ FUNCTION_PROFILER (file, profile_label_no);#if 0#ifdef STATIC_CHAIN_INCOMING_REGNUM if (cxt) ASM_OUTPUT_REG_POP (file, STATIC_CHAIN_INCOMING_REGNUM);#else#ifdef STATIC_CHAIN_REGNUM if (cxt) ASM_OUTPUT_REG_POP (file, STATIC_CHAIN_REGNUM);#endif#endif#endif /* 0 */#ifdef STRUCT_VALUE_INCOMING_REGNUM if (sval) ASM_OUTPUT_REG_POP (file, STRUCT_VALUE_INCOMING_REGNUM);#else#ifdef STRUCT_VALUE_REGNUM if (sval) ASM_OUTPUT_REG_POP (file, STRUCT_VALUE_REGNUM);#endif#endif}/* Output assembler code for the end of a function. For clarity, args are same as those of `final_start_function' even though not all of them are needed. */voidfinal_end_function (first, file, write_symbols, optimize) rtx first; FILE *file; enum debugger write_symbols; int optimize;{ if (app_on) { fprintf (file, ASM_APP_OFF); app_on = 0; } if (write_symbols == GDB_DEBUG) fprintf (file, "\t.gdbend %d\n", pending_blocks[0]);#ifdef SDB_DEBUGGING_INFO if (write_symbols == SDB_DEBUG) sdbout_end_function (last_linenum);#endif#ifdef FUNCTION_EPILOGUE /* Finally, output the function epilogue: code to restore the stack frame and return to the caller. */ FUNCTION_EPILOGUE (file, get_frame_size ());#endif#ifdef SDB_DEBUGGING_INFO if (write_symbols == SDB_DEBUG) sdbout_end_epilogue ();#endif /* If FUNCTION_EPILOGUE is not defined, then the function body itself contains return instructions wherever needed. */}/* Output assembler code for some insns: all or part of a function. For description of args, see `final_start_function', above. PRESCAN is 1 if we are not really outputting, just scanning as if we were outputting. Prescanning deletes and rearranges insns just like ordinary output. PRESCAN is -2 if we are outputting after having prescanned. In this case, don't try to delete or rearrange insns because that has already been done. Prescanning is done only on certain machines. */voidfinal (first, file, write_symbols, optimize, prescan) rtx first; FILE *file; enum debugger write_symbols;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -