📄 emit-rtl.c
字号:
/* Emit RTL for the GNU C-Compiler expander. Copyright (C) 1987, 1988 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. *//* Middle-to-low level generation of rtx code and insns. This file contains the functions `gen_rtx', `gen_reg_rtx' and `gen_label_rtx' that are the usual ways of creating rtl expressions for most purposes. It also has the functions for creating insns and linking them in the doubly-linked chain. The patterns of the insns are created by machine-dependent routines in insn-emit.c, which is generated automatically from the machine description. These routines use `gen_rtx' to make the individual rtx's of the pattern; what is machine dependent is the kind of rtx's they make and what arguments they use. */#include "config.h"#include <stdio.h>#include "gvarargs.h"#include "rtl.h"#include "regs.h"#include "insn-config.h"#include "real.h"#define max(A,B) ((A) > (B) ? (A) : (B))#define min(A,B) ((A) < (B) ? (A) : (B))/* This is reset to FIRST_PSEUDO_REGISTER at the start each function. After rtl generation, it is 1 plus the largest register number used. */int reg_rtx_no = FIRST_PSEUDO_REGISTER;/* This is *not* reset after each function. It gives each CODE_LABEL in the entire compilation a unique label number. */static int label_num = 1;/* Value of `label_num' at start of current function. */static int first_label_num;/* Nonzero means do not generate NOTEs for source line numbers. */static int no_line_numbers;/* Commonly used rtx's, so that we only need space for one copy. These are initialized once for the entire compilation. All of these except perhaps fconst0_rtx and dconst0_rtx are unique; no other rtx-object will be equal to any of these. */rtx pc_rtx; /* (PC) */rtx cc0_rtx; /* (CC0) */rtx cc1_rtx; /* (CC1) (not actually used nowadays) */rtx const0_rtx; /* (CONST_INT 0) */rtx const1_rtx; /* (CONST_INT 1) */rtx fconst0_rtx; /* (CONST_DOUBLE:SF 0) */rtx dconst0_rtx; /* (CONST_DOUBLE:DF 0) *//* All references to the following fixed hard registers go through these unique rtl objects. On machines where the frame-pointer and arg-pointer are the same register, they use the same unique object. After register allocation, other rtl objects which used to be pseudo-regs may be clobbered to refer to the frame-pointer register. But references that were originally to the frame-pointer can be distinguished from the others because they contain frame_pointer_rtx. In an inline procedure, the stack and frame pointer rtxs may not be used for anything else. */rtx stack_pointer_rtx; /* (REG:Pmode STACK_POINTER_REGNUM) */rtx frame_pointer_rtx; /* (REG:Pmode FRAME_POINTER_REGNUM) */rtx arg_pointer_rtx; /* (REG:Pmode ARG_POINTER_REGNUM) */rtx struct_value_rtx; /* (REG:Pmode STRUCT_VALUE_REGNUM) */rtx struct_value_incoming_rtx; /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */rtx static_chain_rtx; /* (REG:Pmode STATIC_CHAIN_REGNUM) */rtx static_chain_incoming_rtx; /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) *//* The ends of the doubly-linked chain of rtl for the current function. Both are reset to null at the start of rtl generation for the function. start_sequence saves both of these on `sequence_stack' and then starts a new, nested sequence of insns. */static rtx first_insn = NULL;static rtx last_insn = NULL;/* Stack of pending (incomplete) sequences saved by `start_sequence'. This looks like (INSN_LIST saved-first-insn (INSN_LIST saved-last-insn ...more saved sequences...)). The main insn-chain is saved in the last two links of the chain, unless the chain is empty. */rtx sequence_stack = 0;/* INSN_UID for next insn emitted. Reset to 1 for each function compiled. */static int cur_insn_uid = 1;/* Line number and source file of the last line-number NOTE emitted. This is used to avoid generating duplicates. */static int last_linenum = 0;static char *last_filename = 0;/* A vector indexed by pseudo reg number. The allocated length of this vector is regno_pointer_flag_length. Since this vector is needed during the expansion phase when the total number of registers in the function is not yet known, it is copied and made bigger when necessary. */char *regno_pointer_flag;int regno_pointer_flag_length;/* Indexed by pseudo register number, gives the rtx for that pseudo. Allocated in parallel with regno_pointer_flag. */rtx *regno_reg_rtx;/* Filename and line number of last line-number note, whether we actually emitted it or not. */extern char *emit_filename;extern int emit_lineno;rtx change_address ();/* rtx gen_rtx (code, mode, [element1, ..., elementn])**** This routine generates an RTX of the size specified by** <code>, which is an RTX code. The RTX structure is initialized** from the arguments <element1> through <elementn>, which are** interpreted according to the specific RTX type's format. The** special machine mode associated with the rtx (if any) is specified** in <mode>.**** gen_rtx() can be invoked in a way which resembles the lisp-like** rtx it will generate. For example, the following rtx structure:**** (plus:QI (mem:QI (reg:SI 1))** (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))**** ...would be generated by the following C code:**** gen_rtx (PLUS, QImode,** gen_rtx (MEM, QImode,** gen_rtx (REG, SImode, 1)),** gen_rtx (MEM, QImode,** gen_rtx (PLUS, SImode,** gen_rtx (REG, SImode, 2),** gen_rtx (REG, SImode, 3)))),*//*VARARGS2*/rtxgen_rtx (va_alist) va_dcl{ va_list p; enum rtx_code code; enum machine_mode mode; register int i; /* Array indices... */ register char *fmt; /* Current rtx's format... */ register rtx rt_val; /* RTX to return to caller... */ va_start (p); code = va_arg (p, enum rtx_code); mode = va_arg (p, enum machine_mode); if (code == CONST_INT) { int arg = va_arg (p, int); if (arg == 0) return const0_rtx; if (arg == 1) return const1_rtx; rt_val = rtx_alloc (code); INTVAL (rt_val) = arg; } else { rt_val = rtx_alloc (code); /* Allocate the storage space. */ rt_val->mode = mode; /* Store the machine mode... */ fmt = GET_RTX_FORMAT (code); /* Find the right format... */ for (i = 0; i < GET_RTX_LENGTH (code); i++) { switch (*fmt++) { case '0': /* Unused field. */ break; case 'i': /* An integer? */ XINT (rt_val, i) = va_arg (p, int); break; case 's': /* A string? */ XSTR (rt_val, i) = va_arg (p, char *); break; case 'e': /* An expression? */ case 'u': /* An insn? Same except when printing. */ XEXP (rt_val, i) = va_arg (p, rtx); break; case 'E': /* An RTX vector? */ XVEC (rt_val, i) = va_arg (p, rtvec); break; default: abort(); } } } va_end (p); return rt_val; /* Return the new RTX... */}/* gen_rtvec (n, [rt1, ..., rtn])**** This routine creates an rtvec and stores within it the** pointers to rtx's which are its arguments.*//*VARARGS1*/rtvecgen_rtvec (va_alist) va_dcl{ int n, i; va_list p; rtx *vector; va_start (p); n = va_arg (p, int); if (n == 0) return NULL_RTVEC; /* Don't allocate an empty rtvec... */ vector = (rtx *) alloca (n * sizeof (rtx)); for (i = 0; i < n; i++) vector[i] = va_arg (p, rtx); va_end (p); return gen_rtvec_v (n, vector);}rtvecgen_rtvec_v (n, argp) int n; rtx *argp;{ register int i; register rtvec rt_val; if (n == 0) return NULL_RTVEC; /* Don't allocate an empty rtvec... */ rt_val = rtvec_alloc (n); /* Allocate an rtvec... */ for (i = 0; i < n; i++) rt_val->elem[i].rtx = *argp++; return rt_val;}/* Generate a REG rtx for a new pseudo register of mode MODE. This pseudo is assigned the next sequential register number. */rtxgen_reg_rtx (mode) enum machine_mode mode;{ register rtx val; /* Make sure regno_pointer_flag and regno_reg_rtx are large enough to have an element for this pseudo reg number. */ if (reg_rtx_no == regno_pointer_flag_length) { rtx *new1; char *new = (char *) oballoc (regno_pointer_flag_length * 2); bzero (new, regno_pointer_flag_length * 2); bcopy (regno_pointer_flag, new, regno_pointer_flag_length); regno_pointer_flag = new; new1 = (rtx *) oballoc (regno_pointer_flag_length * 2 * sizeof (rtx)); bzero (new1, regno_pointer_flag_length * 2 * sizeof (rtx)); bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx)); regno_reg_rtx = new1; regno_pointer_flag_length *= 2; } val = gen_rtx (REG, mode, reg_rtx_no); regno_reg_rtx[reg_rtx_no++] = val; return val;}/* Identify REG as a probable pointer register. */voidmark_reg_pointer (reg) rtx reg;{ REGNO_POINTER_FLAG (REGNO (reg)) = 1;}/* Return 1 plus largest pseudo reg number used in the current function. */intmax_reg_num (){ return reg_rtx_no;}/* Return 1 + the largest label number used so far. */intmax_label_num (){ return label_num;}/* Return first label number used in this function (if any were used). */intget_first_label_num (){ return first_label_num;}/* Assuming that X is an rtx (MEM, REG or SUBREG) for a fixed-point number, return a MEM or SUBREG rtx that refers to the least-significant part of X. MODE specifies how big a part of X to return; it must not be larger than a word. If X is a MEM whose address is a QUEUED, the value may be so also. */rtxgen_lowpart (mode, x) enum machine_mode mode; register rtx x;{ /* This case loses if X is a subreg. To catch bugs early, complain if an invalid MODE is used even in other cases. */ if (GET_MODE_SIZE (mode) > UNITS_PER_WORD && GET_MODE_SIZE (mode) != GET_MODE_UNIT_SIZE (GET_MODE (x))) abort (); if (GET_MODE (x) == mode) return x; if (GET_CODE (x) == CONST_INT) return gen_rtx (CONST_INT, VOIDmode, INTVAL (x) & GET_MODE_MASK (mode)); if (GET_CODE (x) == CONST_DOUBLE)/* In version 1.37, try this: *//* if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT) abort (); */ /* Assume it's an int, so ..._LOW means the low-order word. */ return gen_rtx (CONST_INT, VOIDmode, CONST_DOUBLE_LOW (x) & GET_MODE_MASK (mode)); if (GET_CODE (x) == MEM) { register int offset = 0;#ifdef WORDS_BIG_ENDIAN offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD) - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));#endif#ifdef BYTES_BIG_ENDIAN /* Adjust the address so that the address-after-the-data is unchanged. */ offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode)) - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));#endif return change_address (x, mode, plus_constant (XEXP (x, 0), offset)); } else if (GET_CODE (x) == SUBREG) return (GET_MODE (SUBREG_REG (x)) == mode && SUBREG_WORD (x) == 0 ? SUBREG_REG (x) : gen_rtx (SUBREG, mode, SUBREG_REG (x), SUBREG_WORD (x))); else if (GET_CODE (x) == REG) {#ifdef WORDS_BIG_ENDIAN if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) { return gen_rtx (SUBREG, mode, x, ((GET_MODE_SIZE (GET_MODE (x)) - max (GET_MODE_SIZE (mode), UNITS_PER_WORD)) / UNITS_PER_WORD)); }#endif return gen_rtx (SUBREG, mode, x, 0); } else abort ();}/* Like `gen_lowpart', but refer to the most significant part. */rtxgen_highpart (mode, x) enum machine_mode mode; register rtx x;{ if (GET_CODE (x) == MEM) { register int offset = 0;#ifndef WORDS_BIG_ENDIAN offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD) - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));#endif#ifndef BYTES_BIG_ENDIAN if (GET_MODE_SIZE (mode) < UNITS_PER_WORD) offset -= (GET_MODE_SIZE (mode) - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));#endif return change_address (x, mode, plus_constant (XEXP (x, 0), offset)); } else if (GET_CODE (x) == REG) {#ifndef WORDS_BIG_ENDIAN if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) { return gen_rtx (SUBREG, mode, x, ((GET_MODE_SIZE (GET_MODE (x)) - max (GET_MODE_SIZE (mode), UNITS_PER_WORD)) / UNITS_PER_WORD)); }#endif return gen_rtx (SUBREG, mode, x, 0); } else if (GET_CODE (x) == CONST_INT) /* Assume that a const_int being used where a double is wanted should be sign-extended. This is right only if the use of const_int is carefully restricted. In fact, I think it can happen only for numbers like 0 and 1. */ return gen_rtx (CONST_INT, VOIDmode, (INTVAL (x) >> (BITS_PER_WORD - 1))); else abort ();}/* Return 1 iff X, assumed to be a SUBREG, refers to the least significant part of its containing reg. If X is not a SUBREG, always return 1 (it is its own low part!). */intsubreg_lowpart_p (x) rtx x;{ if (GET_CODE (x) != SUBREG) return 1;#ifdef WORDS_BIG_ENDIAN if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) { register enum machine_mode mode = GET_MODE (SUBREG_REG (x)); return (SUBREG_WORD (x) == ((GET_MODE_SIZE (GET_MODE (x)) - max (GET_MODE_SIZE (mode), UNITS_PER_WORD)) / UNITS_PER_WORD)); }#endif return SUBREG_WORD (x) == 0;}/* Return a memory reference like MEMREF, but with its mode changed to MODE and its address changed to ADDR. (VOIDmode means don't change the mode. NULL for ADDR means don't change the address.) */rtxchange_address (memref, mode, addr) rtx memref; enum machine_mode mode; rtx addr;{ rtx new; if (GET_CODE (memref) != MEM) abort (); if (mode == VOIDmode) mode = GET_MODE (memref); if (addr == 0) addr = XEXP (memref, 0); new = gen_rtx (MEM, mode, memory_address (mode, addr)); MEM_VOLATILE_P (new) = MEM_VOLATILE_P (memref); RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (memref); MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (memref); return new;}/* Return a newly created CODE_LABEL rtx with a unique label number. */rtxgen_label_rtx (){ register rtx label = gen_rtx (CODE_LABEL, VOIDmode, 0, 0, 0, label_num++); LABEL_NUSES (label) = 0; return label;}/* For procedure integration. *//* Return a newly created INLINE_HEADER rtx. Should allocate this from a permanent obstack when the opportunity arises. */rtxgen_inline_header_rtx (insn, last_insn, first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size, stack_slots) rtx insn, last_insn; int first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size; rtx stack_slots;{ rtx header = gen_rtx (INLINE_HEADER, VOIDmode, cur_insn_uid++, NULL, insn, last_insn, first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size, stack_slots); return header;}/* Install new pointers to the first and last insns in the chain. Used for an inline-procedure after copying the insn chain. */voidset_new_first_and_last_insn (first, last) rtx first, last;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -