📄 final.c
字号:
/* Convert RTL to assembler code and output it, for GNU compiler. Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 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 2, 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, 59 Temple Place - Suite 330,Boston, MA 02111-1307, 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 "config.h"#ifdef __STDC__#include <stdarg.h>#else#include <varargs.h>#endif#include <stdio.h>#include <ctype.h>#include "tree.h"#include "rtl.h"#include "regs.h"#include "insn-config.h"#include "insn-flags.h"#include "insn-attr.h"#include "insn-codes.h"#include "recog.h"#include "conditions.h"#include "flags.h"#include "real.h"#include "hard-reg-set.h"#include "defaults.h"#include "output.h"/* Get N_SLINE and N_SOL from stab.h if we can expect the file to exist. */#if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)#if defined (USG) || defined (NO_STAB_H)#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 || XCOFF_DEBUGGING_INFO */#ifdef XCOFF_DEBUGGING_INFO#include "xcoffout.h"#endif/* .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#ifndef INT_TYPE_SIZE#define INT_TYPE_SIZE BITS_PER_WORD#endif/* If we aren't using cc0, CC_STATUS_INIT shouldn't exist. So define a null default for it to save conditionalization later. */#ifndef CC_STATUS_INIT#define CC_STATUS_INIT#endif/* How to start an assembler comment. */#ifndef ASM_COMMENT_START#define ASM_COMMENT_START ";#"#endif/* Is the given character a logical line separator for the assembler? */#ifndef IS_ASM_LOGICAL_LINE_SEPARATOR#define IS_ASM_LOGICAL_LINE_SEPARATOR(C) ((C) == ';')#endif/* Nonzero means this function is a leaf function, with no function calls. This variable exists to be examined in FUNCTION_PROLOGUE and FUNCTION_EPILOGUE. Always zero, unless set by some action. */int leaf_function;/* Last insn processed by final_scan_insn. */static rtx debug_insn = 0;/* Line number of last NOTE. */static int last_linenum;/* Highest line number in current block. */static int high_block_linenum;/* Likewise for function. */static int high_function_linenum;/* Filename of last NOTE. */static char *last_filename;/* 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;/* 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 numbers to generate references to the same labels from the dbx debugging information. Sdb records this level at the beginning of each function, in order to find the current level when recursing down declarations. 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;/* Assign a unique number to each insn that is output. This can be used to generate unique local labels. */static int insn_counter = 0;#ifdef HAVE_cc0/* 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;#endif/* 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;#ifdef ASSEMBLER_DIALECT/* Number of the assembler dialect to use, starting at 0. */static int dialect_number;#endif/* Indexed by line number, nonzero if there is a note for that line. */static char *line_note_exists;/* Linked list to hold line numbers for each basic block. */struct bb_list { struct bb_list *next; /* pointer to next basic block */ int line_num; /* line number */ int file_label_num; /* LPBC<n> label # for stored filename */ int func_label_num; /* LPBC<n> label # for stored function name */};static struct bb_list *bb_head = 0; /* Head of basic block list */static struct bb_list **bb_tail = &bb_head; /* Ptr to store next bb ptr */static int bb_file_label_num = -1; /* Current label # for file */static int bb_func_label_num = -1; /* Current label # for func *//* Linked list to hold the strings for each file and function name output. */struct bb_str { struct bb_str *next; /* pointer to next string */ char *string; /* string */ int label_num; /* label number */ int length; /* string length */};extern rtx peephole PROTO((rtx));static struct bb_str *sbb_head = 0; /* Head of string list. */static struct bb_str **sbb_tail = &sbb_head; /* Ptr to store next bb str */static int sbb_label_num = 0; /* Last label used */static int asm_insn_count PROTO((rtx));static void profile_function PROTO((FILE *));static void profile_after_prologue PROTO((FILE *));static void add_bb PROTO((FILE *));static int add_bb_string PROTO((char *, int));static void output_source_line PROTO((FILE *, rtx));static rtx walk_alter_subreg PROTO((rtx));static int alter_cond PROTO((rtx));static void output_asm_name PROTO((void));static void output_operand PROTO((rtx, int));static void leaf_renumber_regs PROTO((rtx));extern char *getpwd ();/* Initialize data in final at the beginning of a compilation. */voidinit_final (filename) char *filename;{ next_block_index = 2; app_on = 0; max_block_depth = 20; pending_blocks = (int *) xmalloc (20 * sizeof *pending_blocks); final_sequence = 0;#ifdef ASSEMBLER_DIALECT dialect_number = ASSEMBLER_DIALECT;#endif}/* 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[20]; int align = exact_log2 (BIGGEST_ALIGNMENT / BITS_PER_UNIT); int size = (POINTER_SIZE / BITS_PER_UNIT) * count_basic_blocks; int rounded = size; struct bb_list *ptr; struct bb_str *sptr; rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1; rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT) * (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); data_section (); /* Output the main header, of 10 words: 0: 1 if this file's initialized, else 0. 1: address of file name (LPBX1). 2: address of table of counts (LPBX2). 3: number of counts in the table. 4: always 0, for compatibility with Sun. The following are GNU extensions: 5: address of table of start addrs of basic blocks (LPBX3). 6: Number of bytes in this header. 7: address of table of function names (LPBX4). 8: address of table of line numbers (LPBX5) or 0. 9: address of table of file names (LPBX6) or 0. */ ASM_OUTPUT_ALIGN (asm_out_file, align); ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 0); /* zero word */ assemble_integer (const0_rtx, UNITS_PER_WORD, 1); /* address of filename */ ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 1); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); /* address of count table */ ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); /* count of the # of basic blocks */ assemble_integer (GEN_INT (count_basic_blocks), UNITS_PER_WORD, 1); /* zero word (link field) */ assemble_integer (const0_rtx, UNITS_PER_WORD, 1); /* address of basic block start address table */ ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 3); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); /* byte count for extended structure. */ assemble_integer (GEN_INT (10 * UNITS_PER_WORD), UNITS_PER_WORD, 1); /* address of function name table */ ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 4); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); /* address of line number and filename tables if debugging. */ if (write_symbols != NO_DEBUG) { ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 5); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 6); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); } else { assemble_integer (const0_rtx, UNITS_PER_WORD, 1); assemble_integer (const0_rtx, UNITS_PER_WORD, 1); } /* Output the file name changing the suffix to .d for Sun tcov compatibility. */ ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 1); { char *cwd = getpwd (); int len = strlen (filename) + strlen (cwd) + 1; char *data_file = (char *) alloca (len + 4); strcpy (data_file, cwd); strcat (data_file, "/"); strcat (data_file, filename); strip_off_ending (data_file, len); strcat (data_file, ".d"); assemble_string (data_file, strlen (data_file) + 1); } /* Make space for the table of counts. */ if (flag_no_common || size == 0) { /* Realign data section. */ ASM_OUTPUT_ALIGN (asm_out_file, align); ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 2); if (size != 0) assemble_zeros (size); } else { ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2);#ifdef ASM_OUTPUT_SHARED_LOCAL if (flag_shared_data) ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded); else#endif#ifdef ASM_OUTPUT_ALIGNED_LOCAL ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);#else ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);#endif } /* Output any basic block strings */ readonly_data_section (); if (sbb_head) { ASM_OUTPUT_ALIGN (asm_out_file, align); for (sptr = sbb_head; sptr != 0; sptr = sptr->next) { ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBC", sptr->label_num); assemble_string (sptr->string, sptr->length); } } /* Output the table of addresses. */ /* Realign in new section */ ASM_OUTPUT_ALIGN (asm_out_file, align); ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 3); for (i = 0; i < count_basic_blocks; i++) { ASM_GENERATE_INTERNAL_LABEL (name, "LPB", i); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); } /* Output the table of function names. */ ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LPBX", 4); for ((ptr = bb_head), (i = 0); ptr != 0; (ptr = ptr->next), i++) { if (ptr->func_label_num >= 0) { ASM_GENERATE_INTERNAL_LABEL (name, "LPBC", ptr->func_label_num); assemble_integer (gen_rtx (SYMBOL_REF, Pmode, name), UNITS_PER_WORD, 1); } else assemble_integer (const0_rtx, UNITS_PER_WORD, 1); } for ( ; i < count_basic_blocks; i++) assemble_integer (const0_rtx, UNITS_PER_WORD, 1); if (write_symbols != NO_DEBUG) { /* Output the table of line numbers. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -