OSDN Git Service

Use CONST_DOUBLE_FROM_REAL_VALUE macro for immed_real_const_1.
[pf3gnuchains/gcc-fork.git] / gcc / emit-rtl.c
1 /* Emit RTL for the GNU C-Compiler expander.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* Middle-to-low level generation of rtx code and insns.
22
23    This file contains the functions `gen_rtx', `gen_reg_rtx'
24    and `gen_label_rtx' that are the usual ways of creating rtl
25    expressions for most purposes.
26
27    It also has the functions for creating insns and linking
28    them in the doubly-linked chain.
29
30    The patterns of the insns are created by machine-dependent
31    routines in insn-emit.c, which is generated automatically from
32    the machine description.  These routines use `gen_rtx' to make
33    the individual rtx's of the pattern; what is machine dependent
34    is the kind of rtx's they make and what arguments they use.  */
35
36 #include "config.h"
37 #ifdef __STDC__
38 #include <stdarg.h>
39 #else
40 #include <varargs.h>
41 #endif
42 #include "rtl.h"
43 #include "tree.h"
44 #include "flags.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "real.h"
50 #include "obstack.h"
51
52 #include "bytecode.h"
53 #include "machmode.h"
54 #include "bc-opcode.h"
55 #include "bc-typecd.h"
56 #include "bc-optab.h"
57 #include "bc-emit.h"
58
59 #include <stdio.h>
60
61
62 /* Opcode names */
63 #ifdef BCDEBUG_PRINT_CODE
64 char *opcode_name[] =
65 {
66 #include "bc-opname.h"
67
68 "***END***"
69 };
70 #endif
71
72
73 /* Commonly used modes.  */
74
75 enum machine_mode byte_mode;    /* Mode whose width is BITS_PER_UNIT */
76 enum machine_mode word_mode;    /* Mode whose width is BITS_PER_WORD */
77
78 /* This is reset to LAST_VIRTUAL_REGISTER + 1 at the start of each function.
79    After rtl generation, it is 1 plus the largest register number used.  */
80
81 int reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
82
83 /* This is *not* reset after each function.  It gives each CODE_LABEL
84    in the entire compilation a unique label number.  */
85
86 static int label_num = 1;
87
88 /* Lowest label number in current function.  */
89
90 static int first_label_num;
91
92 /* Highest label number in current function.
93    Zero means use the value of label_num instead.
94    This is nonzero only when belatedly compiling an inline function.  */
95
96 static int last_label_num;
97
98 /* Value label_num had when set_new_first_and_last_label_number was called.
99    If label_num has not changed since then, last_label_num is valid.  */
100
101 static int base_label_num;
102
103 /* Nonzero means do not generate NOTEs for source line numbers.  */
104
105 static int no_line_numbers;
106
107 /* Commonly used rtx's, so that we only need space for one copy.
108    These are initialized once for the entire compilation.
109    All of these except perhaps the floating-point CONST_DOUBLEs
110    are unique; no other rtx-object will be equal to any of these.  */
111
112 rtx pc_rtx;                     /* (PC) */
113 rtx cc0_rtx;                    /* (CC0) */
114 rtx cc1_rtx;                    /* (CC1) (not actually used nowadays) */
115 rtx const0_rtx;                 /* (CONST_INT 0) */
116 rtx const1_rtx;                 /* (CONST_INT 1) */
117 rtx const2_rtx;                 /* (CONST_INT 2) */
118 rtx constm1_rtx;                /* (CONST_INT -1) */
119 rtx const_true_rtx;             /* (CONST_INT STORE_FLAG_VALUE) */
120
121 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
122    the values of 0, 1, and 2.  For the integer entries and VOIDmode, we
123    record a copy of const[012]_rtx.  */
124
125 rtx const_tiny_rtx[3][(int) MAX_MACHINE_MODE];
126
127 REAL_VALUE_TYPE dconst0;
128 REAL_VALUE_TYPE dconst1;
129 REAL_VALUE_TYPE dconst2;
130 REAL_VALUE_TYPE dconstm1;
131
132 /* All references to the following fixed hard registers go through
133    these unique rtl objects.  On machines where the frame-pointer and
134    arg-pointer are the same register, they use the same unique object.
135
136    After register allocation, other rtl objects which used to be pseudo-regs
137    may be clobbered to refer to the frame-pointer register.
138    But references that were originally to the frame-pointer can be
139    distinguished from the others because they contain frame_pointer_rtx.
140
141    When to use frame_pointer_rtx and hard_frame_pointer_rtx is a little
142    tricky: until register elimination has taken place hard_frame_pointer_rtx
143    should be used if it is being set, and frame_pointer_rtx otherwise.  After 
144    register elimination hard_frame_pointer_rtx should always be used.
145    On machines where the two registers are same (most) then these are the
146    same.
147
148    In an inline procedure, the stack and frame pointer rtxs may not be
149    used for anything else.  */
150 rtx stack_pointer_rtx;          /* (REG:Pmode STACK_POINTER_REGNUM) */
151 rtx frame_pointer_rtx;          /* (REG:Pmode FRAME_POINTER_REGNUM) */
152 rtx hard_frame_pointer_rtx;     /* (REG:Pmode HARD_FRAME_POINTER_REGNUM) */
153 rtx arg_pointer_rtx;            /* (REG:Pmode ARG_POINTER_REGNUM) */
154 rtx struct_value_rtx;           /* (REG:Pmode STRUCT_VALUE_REGNUM) */
155 rtx struct_value_incoming_rtx;  /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */
156 rtx static_chain_rtx;           /* (REG:Pmode STATIC_CHAIN_REGNUM) */
157 rtx static_chain_incoming_rtx;  /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) */
158 rtx pic_offset_table_rtx;       /* (REG:Pmode PIC_OFFSET_TABLE_REGNUM) */
159
160 rtx virtual_incoming_args_rtx;  /* (REG:Pmode VIRTUAL_INCOMING_ARGS_REGNUM) */
161 rtx virtual_stack_vars_rtx;     /* (REG:Pmode VIRTUAL_STACK_VARS_REGNUM) */
162 rtx virtual_stack_dynamic_rtx;  /* (REG:Pmode VIRTUAL_STACK_DYNAMIC_REGNUM) */
163 rtx virtual_outgoing_args_rtx;  /* (REG:Pmode VIRTUAL_OUTGOING_ARGS_REGNUM) */
164
165 /* We make one copy of (const_int C) where C is in
166    [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
167    to save space during the compilation and simplify comparisons of
168    integers.  */
169
170 #define MAX_SAVED_CONST_INT 64
171
172 static rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
173
174 /* The ends of the doubly-linked chain of rtl for the current function.
175    Both are reset to null at the start of rtl generation for the function.
176    
177    start_sequence saves both of these on `sequence_stack' along with
178    `sequence_rtl_expr' and then starts a new, nested sequence of insns.  */
179
180 static rtx first_insn = NULL;
181 static rtx last_insn = NULL;
182
183 /* RTL_EXPR within which the current sequence will be placed.  Use to
184    prevent reuse of any temporaries within the sequence until after the
185    RTL_EXPR is emitted.  */
186
187 tree sequence_rtl_expr = NULL;
188
189 /* INSN_UID for next insn emitted.
190    Reset to 1 for each function compiled.  */
191
192 static int cur_insn_uid = 1;
193
194 /* Line number and source file of the last line-number NOTE emitted.
195    This is used to avoid generating duplicates.  */
196
197 static int last_linenum = 0;
198 static char *last_filename = 0;
199
200 /* A vector indexed by pseudo reg number.  The allocated length
201    of this vector is regno_pointer_flag_length.  Since this
202    vector is needed during the expansion phase when the total
203    number of registers in the function is not yet known,
204    it is copied and made bigger when necessary.  */
205
206 char *regno_pointer_flag;
207 int regno_pointer_flag_length;
208
209 /* Indexed by pseudo register number, gives the rtx for that pseudo.
210    Allocated in parallel with regno_pointer_flag.  */
211
212 rtx *regno_reg_rtx;
213
214 /* Stack of pending (incomplete) sequences saved by `start_sequence'.
215    Each element describes one pending sequence.
216    The main insn-chain is saved in the last element of the chain,
217    unless the chain is empty.  */
218
219 struct sequence_stack *sequence_stack;
220
221 /* start_sequence and gen_sequence can make a lot of rtx expressions which are
222    shortly thrown away.  We use two mechanisms to prevent this waste:
223
224    First, we keep a list of the expressions used to represent the sequence
225    stack in sequence_element_free_list.
226
227    Second, for sizes up to 5 elements, we keep a SEQUENCE and its associated
228    rtvec for use by gen_sequence.  One entry for each size is sufficient
229    because most cases are calls to gen_sequence followed by immediately
230    emitting the SEQUENCE.  Reuse is safe since emitting a sequence is
231    destructive on the insn in it anyway and hence can't be redone.
232
233    We do not bother to save this cached data over nested function calls.
234    Instead, we just reinitialize them.  */
235
236 #define SEQUENCE_RESULT_SIZE 5
237
238 static struct sequence_stack *sequence_element_free_list;
239 static rtx sequence_result[SEQUENCE_RESULT_SIZE];
240
241 extern int rtx_equal_function_value_matters;
242
243 /* Filename and line number of last line-number note,
244    whether we actually emitted it or not.  */
245 extern char *emit_filename;
246 extern int emit_lineno;
247
248 rtx change_address ();
249 void init_emit ();
250 \f
251 extern struct obstack *rtl_obstack;
252
253 extern int stack_depth;
254 extern int max_stack_depth;
255 \f
256 /* rtx gen_rtx (code, mode, [element1, ..., elementn])
257 **
258 **          This routine generates an RTX of the size specified by
259 **      <code>, which is an RTX code.   The RTX structure is initialized
260 **      from the arguments <element1> through <elementn>, which are
261 **      interpreted according to the specific RTX type's format.   The
262 **      special machine mode associated with the rtx (if any) is specified
263 **      in <mode>.
264 **
265 **          gen_rtx can be invoked in a way which resembles the lisp-like
266 **      rtx it will generate.   For example, the following rtx structure:
267 **
268 **            (plus:QI (mem:QI (reg:SI 1))
269 **                     (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))
270 **
271 **              ...would be generated by the following C code:
272 **
273 **              gen_rtx (PLUS, QImode,
274 **                  gen_rtx (MEM, QImode,
275 **                      gen_rtx (REG, SImode, 1)),
276 **                  gen_rtx (MEM, QImode,
277 **                      gen_rtx (PLUS, SImode,
278 **                          gen_rtx (REG, SImode, 2),
279 **                          gen_rtx (REG, SImode, 3)))),
280 */
281
282 /*VARARGS2*/
283 rtx
284 gen_rtx VPROTO((enum rtx_code code, enum machine_mode mode, ...))
285 {
286 #ifndef __STDC__
287   enum rtx_code code;
288   enum machine_mode mode;
289 #endif
290   va_list p;
291   register int i;               /* Array indices...                     */
292   register char *fmt;           /* Current rtx's format...              */
293   register rtx rt_val;          /* RTX to return to caller...           */
294
295   VA_START (p, mode);
296
297 #ifndef __STDC__
298   code = va_arg (p, enum rtx_code);
299   mode = va_arg (p, enum machine_mode);
300 #endif
301
302   if (code == CONST_INT)
303     {
304       HOST_WIDE_INT arg = va_arg (p, HOST_WIDE_INT);
305
306       if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
307         return const_int_rtx[arg + MAX_SAVED_CONST_INT];
308
309       if (const_true_rtx && arg == STORE_FLAG_VALUE)
310         return const_true_rtx;
311
312       rt_val = rtx_alloc (code);
313       INTVAL (rt_val) = arg;
314     }
315   else if (code == REG)
316     {
317       int regno = va_arg (p, int);
318
319       /* In case the MD file explicitly references the frame pointer, have
320          all such references point to the same frame pointer.  This is used
321          during frame pointer elimination to distinguish the explicit
322          references to these registers from pseudos that happened to be
323          assigned to them.
324
325          If we have eliminated the frame pointer or arg pointer, we will
326          be using it as a normal register, for example as a spill register.
327          In such cases, we might be accessing it in a mode that is not
328          Pmode and therefore cannot use the pre-allocated rtx.
329
330          Also don't do this when we are making new REGs in reload,
331          since we don't want to get confused with the real pointers.  */
332
333       if (frame_pointer_rtx && regno == FRAME_POINTER_REGNUM && mode == Pmode
334           && ! reload_in_progress)
335         return frame_pointer_rtx;
336 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
337       if (hard_frame_pointer_rtx && regno == HARD_FRAME_POINTER_REGNUM
338           && mode == Pmode && ! reload_in_progress)
339         return hard_frame_pointer_rtx;
340 #endif
341 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM && HARD_FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
342       if (arg_pointer_rtx && regno == ARG_POINTER_REGNUM && mode == Pmode
343           && ! reload_in_progress)
344         return arg_pointer_rtx;
345 #endif
346       if (stack_pointer_rtx && regno == STACK_POINTER_REGNUM && mode == Pmode
347           && ! reload_in_progress)
348         return stack_pointer_rtx;
349       else
350         {
351           rt_val = rtx_alloc (code);
352           rt_val->mode = mode;
353           REGNO (rt_val) = regno;
354           return rt_val;
355         }
356     }
357   else
358     {
359       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
360       rt_val->mode = mode;              /* Store the machine mode...  */
361
362       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
363       for (i = 0; i < GET_RTX_LENGTH (code); i++)
364         {
365           switch (*fmt++)
366             {
367             case '0':           /* Unused field.  */
368               break;
369
370             case 'i':           /* An integer?  */
371               XINT (rt_val, i) = va_arg (p, int);
372               break;
373
374             case 'w':           /* A wide integer? */
375               XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
376               break;
377
378             case 's':           /* A string?  */
379               XSTR (rt_val, i) = va_arg (p, char *);
380               break;
381
382             case 'e':           /* An expression?  */
383             case 'u':           /* An insn?  Same except when printing.  */
384               XEXP (rt_val, i) = va_arg (p, rtx);
385               break;
386
387             case 'E':           /* An RTX vector?  */
388               XVEC (rt_val, i) = va_arg (p, rtvec);
389               break;
390
391             default:
392               abort ();
393             }
394         }
395     }
396   va_end (p);
397   return rt_val;                /* Return the new RTX...                */
398 }
399
400 /* gen_rtvec (n, [rt1, ..., rtn])
401 **
402 **          This routine creates an rtvec and stores within it the
403 **      pointers to rtx's which are its arguments.
404 */
405
406 /*VARARGS1*/
407 rtvec
408 gen_rtvec VPROTO((int n, ...))
409 {
410 #ifndef __STDC__
411   int n;
412 #endif
413   int i;
414   va_list p;
415   rtx *vector;
416
417   VA_START (p, n);
418
419 #ifndef __STDC__
420   n = va_arg (p, int);
421 #endif
422
423   if (n == 0)
424     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
425
426   vector = (rtx *) alloca (n * sizeof (rtx));
427
428   for (i = 0; i < n; i++)
429     vector[i] = va_arg (p, rtx);
430   va_end (p);
431
432   return gen_rtvec_v (n, vector);
433 }
434
435 rtvec
436 gen_rtvec_v (n, argp)
437      int n;
438      rtx *argp;
439 {
440   register int i;
441   register rtvec rt_val;
442
443   if (n == 0)
444     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
445
446   rt_val = rtvec_alloc (n);     /* Allocate an rtvec...                 */
447
448   for (i = 0; i < n; i++)
449     rt_val->elem[i].rtx = *argp++;
450
451   return rt_val;
452 }
453 \f
454 /* Generate a REG rtx for a new pseudo register of mode MODE.
455    This pseudo is assigned the next sequential register number.  */
456
457 rtx
458 gen_reg_rtx (mode)
459      enum machine_mode mode;
460 {
461   register rtx val;
462
463   /* Don't let anything called by or after reload create new registers
464      (actually, registers can't be created after flow, but this is a good
465      approximation).  */
466
467   if (reload_in_progress || reload_completed)
468     abort ();
469
470   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
471       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
472     {
473       /* For complex modes, don't make a single pseudo.
474          Instead, make a CONCAT of two pseudos.
475          This allows noncontiguous allocation of the real and imaginary parts,
476          which makes much better code.  Besides, allocating DCmode
477          pseudos overstrains reload on some machines like the 386.  */
478       rtx realpart, imagpart;
479       int size = GET_MODE_UNIT_SIZE (mode);
480       enum machine_mode partmode
481         = mode_for_size (size * BITS_PER_UNIT,
482                          (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
483                           ? MODE_FLOAT : MODE_INT),
484                          0);
485
486       realpart = gen_reg_rtx (partmode);
487       imagpart = gen_reg_rtx (partmode);
488       return gen_rtx (CONCAT, mode, realpart, imagpart);
489     }
490
491   /* Make sure regno_pointer_flag and regno_reg_rtx are large
492      enough to have an element for this pseudo reg number.  */
493
494   if (reg_rtx_no == regno_pointer_flag_length)
495     {
496       rtx *new1;
497       char *new =
498         (char *) oballoc (regno_pointer_flag_length * 2);
499       bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
500       bzero (&new[regno_pointer_flag_length], regno_pointer_flag_length);
501       regno_pointer_flag = new;
502
503       new1 = (rtx *) oballoc (regno_pointer_flag_length * 2 * sizeof (rtx));
504       bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
505       bzero (&new1[regno_pointer_flag_length],
506              regno_pointer_flag_length * sizeof (rtx));
507       regno_reg_rtx = new1;
508
509       regno_pointer_flag_length *= 2;
510     }
511
512   val = gen_rtx (REG, mode, reg_rtx_no);
513   regno_reg_rtx[reg_rtx_no++] = val;
514   return val;
515 }
516
517 /* Identify REG as a probable pointer register.  */
518
519 void
520 mark_reg_pointer (reg)
521      rtx reg;
522 {
523   REGNO_POINTER_FLAG (REGNO (reg)) = 1;
524 }
525
526 /* Return 1 plus largest pseudo reg number used in the current function.  */
527
528 int
529 max_reg_num ()
530 {
531   return reg_rtx_no;
532 }
533
534 /* Return 1 + the largest label number used so far in the current function.  */
535
536 int
537 max_label_num ()
538 {
539   if (last_label_num && label_num == base_label_num)
540     return last_label_num;
541   return label_num;
542 }
543
544 /* Return first label number used in this function (if any were used).  */
545
546 int
547 get_first_label_num ()
548 {
549   return first_label_num;
550 }
551 \f
552 /* Return a value representing some low-order bits of X, where the number
553    of low-order bits is given by MODE.  Note that no conversion is done
554    between floating-point and fixed-point values, rather, the bit 
555    representation is returned.
556
557    This function handles the cases in common between gen_lowpart, below,
558    and two variants in cse.c and combine.c.  These are the cases that can
559    be safely handled at all points in the compilation.
560
561    If this is not a case we can handle, return 0.  */
562
563 rtx
564 gen_lowpart_common (mode, x)
565      enum machine_mode mode;
566      register rtx x;
567 {
568   int word = 0;
569
570   if (GET_MODE (x) == mode)
571     return x;
572
573   /* MODE must occupy no more words than the mode of X.  */
574   if (GET_MODE (x) != VOIDmode
575       && ((GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
576           > ((GET_MODE_SIZE (GET_MODE (x)) + (UNITS_PER_WORD - 1))
577              / UNITS_PER_WORD)))
578     return 0;
579
580   if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
581     word = ((GET_MODE_SIZE (GET_MODE (x))
582              - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
583             / UNITS_PER_WORD);
584
585   if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
586       && (GET_MODE_CLASS (mode) == MODE_INT
587           || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
588     {
589       /* If we are getting the low-order part of something that has been
590          sign- or zero-extended, we can either just use the object being
591          extended or make a narrower extension.  If we want an even smaller
592          piece than the size of the object being extended, call ourselves
593          recursively.
594
595          This case is used mostly by combine and cse.  */
596
597       if (GET_MODE (XEXP (x, 0)) == mode)
598         return XEXP (x, 0);
599       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
600         return gen_lowpart_common (mode, XEXP (x, 0));
601       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
602         return gen_rtx (GET_CODE (x), mode, XEXP (x, 0));
603     }
604   else if (GET_CODE (x) == SUBREG
605            && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
606                || GET_MODE_SIZE (mode) == GET_MODE_UNIT_SIZE (GET_MODE (x))))
607     return (GET_MODE (SUBREG_REG (x)) == mode && SUBREG_WORD (x) == 0
608             ? SUBREG_REG (x)
609             : gen_rtx (SUBREG, mode, SUBREG_REG (x), SUBREG_WORD (x)));
610   else if (GET_CODE (x) == REG)
611     {
612       /* If the register is not valid for MODE, return 0.  If we don't
613          do this, there is no way to fix up the resulting REG later.  
614          But we do do this if the current REG is not valid for its
615          mode.  This latter is a kludge, but is required due to the
616          way that parameters are passed on some machines, most
617          notably Sparc.  */
618       if (REGNO (x) < FIRST_PSEUDO_REGISTER
619           && ! HARD_REGNO_MODE_OK (REGNO (x) + word, mode)
620           && HARD_REGNO_MODE_OK (REGNO (x), GET_MODE (x)))
621         return 0;
622       else if (REGNO (x) < FIRST_PSEUDO_REGISTER
623                /* integrate.c can't handle parts of a return value register. */
624                && (! REG_FUNCTION_VALUE_P (x)
625                    || ! rtx_equal_function_value_matters)
626                /* We want to keep the stack, frame, and arg pointers
627                   special.  */
628                && x != frame_pointer_rtx
629 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
630                && x != arg_pointer_rtx
631 #endif
632                && x != stack_pointer_rtx)
633         return gen_rtx (REG, mode, REGNO (x) + word);
634       else
635         return gen_rtx (SUBREG, mode, x, word);
636     }
637   /* If X is a CONST_INT or a CONST_DOUBLE, extract the appropriate bits
638      from the low-order part of the constant.  */
639   else if ((GET_MODE_CLASS (mode) == MODE_INT
640             || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
641            && GET_MODE (x) == VOIDmode
642            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
643     {
644       /* If MODE is twice the host word size, X is already the desired
645          representation.  Otherwise, if MODE is wider than a word, we can't
646          do this.  If MODE is exactly a word, return just one CONST_INT.
647          If MODE is smaller than a word, clear the bits that don't belong
648          in our mode, unless they and our sign bit are all one.  So we get
649          either a reasonable negative value or a reasonable unsigned value
650          for this mode.  */
651
652       if (GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT)
653         return x;
654       else if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
655         return 0;
656       else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
657         return (GET_CODE (x) == CONST_INT ? x
658                 : GEN_INT (CONST_DOUBLE_LOW (x)));
659       else
660         {
661           /* MODE must be narrower than HOST_BITS_PER_INT.  */
662           int width = GET_MODE_BITSIZE (mode);
663           HOST_WIDE_INT val = (GET_CODE (x) == CONST_INT ? INTVAL (x)
664                                : CONST_DOUBLE_LOW (x));
665
666           if (((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
667                != ((HOST_WIDE_INT) (-1) << (width - 1))))
668             val &= ((HOST_WIDE_INT) 1 << width) - 1;
669
670           return (GET_CODE (x) == CONST_INT && INTVAL (x) == val ? x
671                   : GEN_INT (val));
672         }
673     }
674
675   /* If X is an integral constant but we want it in floating-point, it
676      must be the case that we have a union of an integer and a floating-point
677      value.  If the machine-parameters allow it, simulate that union here
678      and return the result.  The two-word and single-word cases are 
679      different.  */
680
681   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
682              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
683             || flag_pretend_float)
684            && GET_MODE_CLASS (mode) == MODE_FLOAT
685            && GET_MODE_SIZE (mode) == UNITS_PER_WORD
686            && GET_CODE (x) == CONST_INT
687            && sizeof (float) * HOST_BITS_PER_CHAR == HOST_BITS_PER_WIDE_INT)
688 #ifdef REAL_ARITHMETIC
689     {
690       REAL_VALUE_TYPE r;
691       HOST_WIDE_INT i;
692
693       i = INTVAL (x);
694       r = REAL_VALUE_FROM_TARGET_SINGLE (i);
695       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
696     }
697 #else
698     {
699       union {HOST_WIDE_INT i; float d; } u;
700
701       u.i = INTVAL (x);
702       return CONST_DOUBLE_FROM_REAL_VALUE (u.d, mode);
703     }
704 #endif
705   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
706              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
707             || flag_pretend_float)
708            && GET_MODE_CLASS (mode) == MODE_FLOAT
709            && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
710            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
711            && GET_MODE (x) == VOIDmode
712            && (sizeof (double) * HOST_BITS_PER_CHAR
713                == 2 * HOST_BITS_PER_WIDE_INT))
714 #ifdef REAL_ARITHMETIC
715     {
716       REAL_VALUE_TYPE r;
717       HOST_WIDE_INT i[2];
718       HOST_WIDE_INT low, high;
719
720       if (GET_CODE (x) == CONST_INT)
721         low = INTVAL (x), high = low >> (HOST_BITS_PER_WIDE_INT -1);
722       else
723         low = CONST_DOUBLE_LOW (x), high = CONST_DOUBLE_HIGH (x);
724
725       /* REAL_VALUE_TARGET_DOUBLE takes the addressing order of the
726          target machine. */
727       if (WORDS_BIG_ENDIAN)
728         i[0] = high, i[1] = low;
729       else
730         i[0] = low, i[1] = high;
731
732       r = REAL_VALUE_FROM_TARGET_DOUBLE (i);
733       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
734     }
735 #else
736     {
737       union {HOST_WIDE_INT i[2]; double d; } u;
738       HOST_WIDE_INT low, high;
739
740       if (GET_CODE (x) == CONST_INT)
741         low = INTVAL (x), high = low >> (HOST_BITS_PER_WIDE_INT -1);
742       else
743         low = CONST_DOUBLE_LOW (x), high = CONST_DOUBLE_HIGH (x);
744
745 #ifdef HOST_WORDS_BIG_ENDIAN
746       u.i[0] = high, u.i[1] = low;
747 #else
748       u.i[0] = low, u.i[1] = high;
749 #endif
750
751       return CONST_DOUBLE_FROM_REAL_VALUE (u.d, mode);
752     }
753 #endif
754   /* Similarly, if this is converting a floating-point value into a
755      single-word integer.  Only do this is the host and target parameters are
756      compatible.  */
757
758   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
759              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
760             || flag_pretend_float)
761            && (GET_MODE_CLASS (mode) == MODE_INT
762                || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
763            && GET_CODE (x) == CONST_DOUBLE
764            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
765            && GET_MODE_BITSIZE (mode) == BITS_PER_WORD)
766     return operand_subword (x, 0, 0, GET_MODE (x));
767
768   /* Similarly, if this is converting a floating-point value into a
769      two-word integer, we can do this one word at a time and make an
770      integer.  Only do this is the host and target parameters are
771      compatible.  */
772
773   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
774              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
775             || flag_pretend_float)
776            && (GET_MODE_CLASS (mode) == MODE_INT
777                || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
778            && GET_CODE (x) == CONST_DOUBLE
779            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
780            && GET_MODE_BITSIZE (mode) == 2 * BITS_PER_WORD)
781     {
782       rtx lowpart = operand_subword (x, WORDS_BIG_ENDIAN, 0, GET_MODE (x));
783       rtx highpart = operand_subword (x, ! WORDS_BIG_ENDIAN, 0, GET_MODE (x));
784
785       if (lowpart && GET_CODE (lowpart) == CONST_INT
786           && highpart && GET_CODE (highpart) == CONST_INT)
787         return immed_double_const (INTVAL (lowpart), INTVAL (highpart), mode);
788     }
789
790   /* Otherwise, we can't do this.  */
791   return 0;
792 }
793 \f
794 /* Return the real part (which has mode MODE) of a complex value X.
795    This always comes at the low address in memory.  */
796
797 rtx
798 gen_realpart (mode, x)
799      enum machine_mode mode;
800      register rtx x;
801 {
802   if (GET_CODE (x) == CONCAT && GET_MODE (XEXP (x, 0)) == mode)
803     return XEXP (x, 0);
804   else if (WORDS_BIG_ENDIAN)
805     return gen_highpart (mode, x);
806   else
807     return gen_lowpart (mode, x);
808 }
809
810 /* Return the imaginary part (which has mode MODE) of a complex value X.
811    This always comes at the high address in memory.  */
812
813 rtx
814 gen_imagpart (mode, x)
815      enum machine_mode mode;
816      register rtx x;
817 {
818   if (GET_CODE (x) == CONCAT && GET_MODE (XEXP (x, 0)) == mode)
819     return XEXP (x, 1);
820   else if (WORDS_BIG_ENDIAN)
821     return gen_lowpart (mode, x);
822   else
823     return gen_highpart (mode, x);
824 }
825
826 /* Return 1 iff X, assumed to be a SUBREG,
827    refers to the real part of the complex value in its containing reg.
828    Complex values are always stored with the real part in the first word,
829    regardless of WORDS_BIG_ENDIAN.  */
830
831 int
832 subreg_realpart_p (x)
833      rtx x;
834 {
835   if (GET_CODE (x) != SUBREG)
836     abort ();
837
838   return SUBREG_WORD (x) == 0;
839 }
840 \f
841 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a value,
842    return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
843    least-significant part of X.
844    MODE specifies how big a part of X to return;
845    it usually should not be larger than a word.
846    If X is a MEM whose address is a QUEUED, the value may be so also.  */
847
848 rtx
849 gen_lowpart (mode, x)
850      enum machine_mode mode;
851      register rtx x;
852 {
853   rtx result = gen_lowpart_common (mode, x);
854
855   if (result)
856     return result;
857   else if (GET_CODE (x) == MEM)
858     {
859       /* The only additional case we can do is MEM.  */
860       register int offset = 0;
861       if (WORDS_BIG_ENDIAN)
862         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
863                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
864
865       if (BYTES_BIG_ENDIAN)
866         /* Adjust the address so that the address-after-the-data
867            is unchanged.  */
868         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
869                    - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
870
871       return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
872     }
873   else
874     abort ();
875 }
876
877 /* Like `gen_lowpart', but refer to the most significant part. 
878    This is used to access the imaginary part of a complex number.  */
879
880 rtx
881 gen_highpart (mode, x)
882      enum machine_mode mode;
883      register rtx x;
884 {
885   /* This case loses if X is a subreg.  To catch bugs early,
886      complain if an invalid MODE is used even in other cases.  */
887   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
888       && GET_MODE_SIZE (mode) != GET_MODE_UNIT_SIZE (GET_MODE (x)))
889     abort ();
890   if (GET_CODE (x) == CONST_DOUBLE
891 #if !(TARGET_FLOAT_FORMAT != HOST_FLOAT_FORMAT || defined (REAL_IS_NOT_DOUBLE))
892       && GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT
893 #endif
894       )
895     return gen_rtx (CONST_INT, VOIDmode,
896                     CONST_DOUBLE_HIGH (x) & GET_MODE_MASK (mode));
897   else if (GET_CODE (x) == CONST_INT)
898     return const0_rtx;
899   else if (GET_CODE (x) == MEM)
900     {
901       register int offset = 0;
902       if (! WORDS_BIG_ENDIAN)
903         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
904                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
905
906       if (! BYTES_BIG_ENDIAN
907           && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
908         offset -= (GET_MODE_SIZE (mode)
909                    - MIN (UNITS_PER_WORD,
910                           GET_MODE_SIZE (GET_MODE (x))));
911
912       return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
913     }
914   else if (GET_CODE (x) == SUBREG)
915     {
916       /* The only time this should occur is when we are looking at a
917          multi-word item with a SUBREG whose mode is the same as that of the
918          item.  It isn't clear what we would do if it wasn't.  */
919       if (SUBREG_WORD (x) != 0)
920         abort ();
921       return gen_highpart (mode, SUBREG_REG (x));
922     }
923   else if (GET_CODE (x) == REG)
924     {
925       int word = 0;
926
927       if (! WORDS_BIG_ENDIAN
928           && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
929         word = ((GET_MODE_SIZE (GET_MODE (x))
930                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
931                 / UNITS_PER_WORD);
932
933       if (REGNO (x) < FIRST_PSEUDO_REGISTER
934           /* integrate.c can't handle parts of a return value register. */
935           && (! REG_FUNCTION_VALUE_P (x)
936               || ! rtx_equal_function_value_matters)
937           /* We want to keep the stack, frame, and arg pointers special.  */
938           && x != frame_pointer_rtx
939 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
940           && x != arg_pointer_rtx
941 #endif
942           && x != stack_pointer_rtx)
943         return gen_rtx (REG, mode, REGNO (x) + word);
944       else
945         return gen_rtx (SUBREG, mode, x, word);
946     }
947   else
948     abort ();
949 }
950
951 /* Return 1 iff X, assumed to be a SUBREG,
952    refers to the least significant part of its containing reg.
953    If X is not a SUBREG, always return 1 (it is its own low part!).  */
954
955 int
956 subreg_lowpart_p (x)
957      rtx x;
958 {
959   if (GET_CODE (x) != SUBREG)
960     return 1;
961
962   if (WORDS_BIG_ENDIAN
963       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD)
964     return (SUBREG_WORD (x)
965             == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
966                  - MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD))
967                 / UNITS_PER_WORD));
968
969   return SUBREG_WORD (x) == 0;
970 }
971 \f
972 /* Return subword I of operand OP.
973    The word number, I, is interpreted as the word number starting at the
974    low-order address.  Word 0 is the low-order word if not WORDS_BIG_ENDIAN,
975    otherwise it is the high-order word.
976
977    If we cannot extract the required word, we return zero.  Otherwise, an
978    rtx corresponding to the requested word will be returned.
979
980    VALIDATE_ADDRESS is nonzero if the address should be validated.  Before
981    reload has completed, a valid address will always be returned.  After
982    reload, if a valid address cannot be returned, we return zero.
983
984    If VALIDATE_ADDRESS is zero, we simply form the required address; validating
985    it is the responsibility of the caller.
986
987    MODE is the mode of OP in case it is a CONST_INT.  */
988
989 rtx
990 operand_subword (op, i, validate_address, mode)
991      rtx op;
992      int i;
993      int validate_address;
994      enum machine_mode mode;
995 {
996   HOST_WIDE_INT val;
997   int size_ratio = HOST_BITS_PER_WIDE_INT / BITS_PER_WORD;
998
999   if (mode == VOIDmode)
1000     mode = GET_MODE (op);
1001
1002   if (mode == VOIDmode)
1003     abort ();
1004
1005   /* If OP is narrower than a word or if we want a word outside OP, fail.  */
1006   if (mode != BLKmode
1007       && (GET_MODE_SIZE (mode) < UNITS_PER_WORD
1008           || (i + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode)))
1009     return 0;
1010
1011   /* If OP is already an integer word, return it.  */
1012   if (GET_MODE_CLASS (mode) == MODE_INT
1013       && GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1014     return op;
1015
1016   /* If OP is a REG or SUBREG, we can handle it very simply.  */
1017   if (GET_CODE (op) == REG)
1018     {
1019       /* If the register is not valid for MODE, return 0.  If we don't
1020          do this, there is no way to fix up the resulting REG later.  */
1021       if (REGNO (op) < FIRST_PSEUDO_REGISTER
1022           && ! HARD_REGNO_MODE_OK (REGNO (op) + i, word_mode))
1023         return 0;
1024       else if (REGNO (op) >= FIRST_PSEUDO_REGISTER
1025                || (REG_FUNCTION_VALUE_P (op)
1026                    && rtx_equal_function_value_matters)
1027                /* We want to keep the stack, frame, and arg pointers
1028                   special.  */
1029                || op == frame_pointer_rtx
1030 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1031                || op == arg_pointer_rtx
1032 #endif
1033                || op == stack_pointer_rtx)
1034         return gen_rtx (SUBREG, word_mode, op, i);
1035       else
1036         return gen_rtx (REG, word_mode, REGNO (op) + i);
1037     }
1038   else if (GET_CODE (op) == SUBREG)
1039     return gen_rtx (SUBREG, word_mode, SUBREG_REG (op), i + SUBREG_WORD (op));
1040   else if (GET_CODE (op) == CONCAT)
1041     {
1042       int partwords = GET_MODE_UNIT_SIZE (GET_MODE (op)) / UNITS_PER_WORD;
1043       if (i < partwords)
1044         return operand_subword (XEXP (op, 0), i, validate_address, mode);
1045       return operand_subword (XEXP (op, 1), i - partwords,
1046                               validate_address, mode);
1047     }
1048
1049   /* Form a new MEM at the requested address.  */
1050   if (GET_CODE (op) == MEM)
1051     {
1052       rtx addr = plus_constant (XEXP (op, 0), i * UNITS_PER_WORD);
1053       rtx new;
1054
1055       if (validate_address)
1056         {
1057           if (reload_completed)
1058             {
1059               if (! strict_memory_address_p (word_mode, addr))
1060                 return 0;
1061             }
1062           else
1063             addr = memory_address (word_mode, addr);
1064         }
1065
1066       new = gen_rtx (MEM, word_mode, addr);
1067
1068       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (op);
1069       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (op);
1070       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (op);
1071
1072       return new;
1073     }
1074
1075   /* The only remaining cases are when OP is a constant.  If the host and
1076      target floating formats are the same, handling two-word floating
1077      constants are easy.  Note that REAL_VALUE_TO_TARGET_{SINGLE,DOUBLE}
1078      are defined as returning 32 bit and 64-bit values, respectively,
1079      and not values of BITS_PER_WORD and 2 * BITS_PER_WORD bits.  */
1080 #ifdef REAL_ARITHMETIC
1081   if ((HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1082       && GET_MODE_CLASS (mode) == MODE_FLOAT
1083       && GET_MODE_BITSIZE (mode) == 64
1084       && GET_CODE (op) == CONST_DOUBLE)
1085     {
1086       HOST_WIDE_INT k[2];
1087       REAL_VALUE_TYPE rv;
1088
1089       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1090       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1091
1092       /* We handle 32-bit and 64-bit host words here.  Note that the order in
1093          which the words are written depends on the word endianness.
1094
1095          ??? This is a potential portability problem and should
1096          be fixed at some point.  */
1097       if (HOST_BITS_PER_WIDE_INT == 32)
1098         return GEN_INT (k[i]);
1099       else if (HOST_BITS_PER_WIDE_INT == 64 && i == 0)
1100         return GEN_INT ((k[! WORDS_BIG_ENDIAN] << (HOST_BITS_PER_WIDE_INT / 2))
1101                         | k[WORDS_BIG_ENDIAN]);
1102       else
1103         abort ();
1104     }
1105 #else /* no REAL_ARITHMETIC */
1106   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
1107         && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1108        || flag_pretend_float)
1109       && GET_MODE_CLASS (mode) == MODE_FLOAT
1110       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1111       && GET_CODE (op) == CONST_DOUBLE)
1112     {
1113       /* The constant is stored in the host's word-ordering,
1114          but we want to access it in the target's word-ordering.  Some
1115          compilers don't like a conditional inside macro args, so we have two
1116          copies of the return.  */
1117 #ifdef HOST_WORDS_BIG_ENDIAN
1118       return GEN_INT (i == WORDS_BIG_ENDIAN
1119                       ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
1120 #else
1121       return GEN_INT (i != WORDS_BIG_ENDIAN
1122                       ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
1123 #endif
1124     }
1125 #endif /* no REAL_ARITHMETIC */
1126
1127   /* Single word float is a little harder, since single- and double-word
1128      values often do not have the same high-order bits.  We have already
1129      verified that we want the only defined word of the single-word value.  */
1130 #ifdef REAL_ARITHMETIC
1131   if ((HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1132       && GET_MODE_CLASS (mode) == MODE_FLOAT
1133       && GET_MODE_BITSIZE (mode) == 32
1134       && GET_CODE (op) == CONST_DOUBLE)
1135     {
1136       HOST_WIDE_INT l;
1137       REAL_VALUE_TYPE rv;
1138
1139       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1140       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1141       return GEN_INT (l);
1142     }
1143 #else
1144   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
1145         && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1146        || flag_pretend_float)
1147       && GET_MODE_CLASS (mode) == MODE_FLOAT
1148       && GET_MODE_SIZE (mode) == UNITS_PER_WORD
1149       && GET_CODE (op) == CONST_DOUBLE)
1150     {
1151       double d;
1152       union {float f; HOST_WIDE_INT i; } u;
1153
1154       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1155
1156       u.f = d;
1157       return GEN_INT (u.i);
1158     }
1159 #endif /* no REAL_ARITHMETIC */
1160       
1161   /* The only remaining cases that we can handle are integers.
1162      Convert to proper endianness now since these cases need it.
1163      At this point, i == 0 means the low-order word.  
1164
1165      We do not want to handle the case when BITS_PER_WORD <= HOST_BITS_PER_INT
1166      in general.  However, if OP is (const_int 0), we can just return
1167      it for any word.  */
1168
1169   if (op == const0_rtx)
1170     return op;
1171
1172   if (GET_MODE_CLASS (mode) != MODE_INT
1173       || (GET_CODE (op) != CONST_INT && GET_CODE (op) != CONST_DOUBLE)
1174       || BITS_PER_WORD > HOST_BITS_PER_INT)
1175     return 0;
1176
1177   if (WORDS_BIG_ENDIAN)
1178     i = GET_MODE_SIZE (mode) / UNITS_PER_WORD - 1 - i;
1179
1180   /* Find out which word on the host machine this value is in and get
1181      it from the constant.  */
1182   val = (i / size_ratio == 0
1183          ? (GET_CODE (op) == CONST_INT ? INTVAL (op) : CONST_DOUBLE_LOW (op))
1184          : (GET_CODE (op) == CONST_INT
1185             ? (INTVAL (op) < 0 ? ~0 : 0) : CONST_DOUBLE_HIGH (op)));
1186
1187   /* If BITS_PER_WORD is smaller than an int, get the appropriate bits.  */
1188   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT)
1189     val = ((val >> ((i % size_ratio) * BITS_PER_WORD))
1190            & (((HOST_WIDE_INT) 1
1191                << (BITS_PER_WORD % HOST_BITS_PER_WIDE_INT)) - 1));
1192
1193   return GEN_INT (val);
1194 }
1195
1196 /* Similar to `operand_subword', but never return 0.  If we can't extract
1197    the required subword, put OP into a register and try again.  If that fails,
1198    abort.  We always validate the address in this case.  It is not valid
1199    to call this function after reload; it is mostly meant for RTL
1200    generation. 
1201
1202    MODE is the mode of OP, in case it is CONST_INT.  */
1203
1204 rtx
1205 operand_subword_force (op, i, mode)
1206      rtx op;
1207      int i;
1208      enum machine_mode mode;
1209 {
1210   rtx result = operand_subword (op, i, 1, mode);
1211
1212   if (result)
1213     return result;
1214
1215   if (mode != BLKmode && mode != VOIDmode)
1216     op = force_reg (mode, op);
1217
1218   result = operand_subword (op, i, 1, mode);
1219   if (result == 0)
1220     abort ();
1221
1222   return result;
1223 }
1224 \f
1225 /* Given a compare instruction, swap the operands.
1226    A test instruction is changed into a compare of 0 against the operand.  */
1227
1228 void
1229 reverse_comparison (insn)
1230      rtx insn;
1231 {
1232   rtx body = PATTERN (insn);
1233   rtx comp;
1234
1235   if (GET_CODE (body) == SET)
1236     comp = SET_SRC (body);
1237   else
1238     comp = SET_SRC (XVECEXP (body, 0, 0));
1239
1240   if (GET_CODE (comp) == COMPARE)
1241     {
1242       rtx op0 = XEXP (comp, 0);
1243       rtx op1 = XEXP (comp, 1);
1244       XEXP (comp, 0) = op1;
1245       XEXP (comp, 1) = op0;
1246     }
1247   else
1248     {
1249       rtx new = gen_rtx (COMPARE, VOIDmode,
1250                          CONST0_RTX (GET_MODE (comp)), comp);
1251       if (GET_CODE (body) == SET)
1252         SET_SRC (body) = new;
1253       else
1254         SET_SRC (XVECEXP (body, 0, 0)) = new;
1255     }
1256 }
1257 \f
1258 /* Return a memory reference like MEMREF, but with its mode changed
1259    to MODE and its address changed to ADDR.
1260    (VOIDmode means don't change the mode.
1261    NULL for ADDR means don't change the address.)  */
1262
1263 rtx
1264 change_address (memref, mode, addr)
1265      rtx memref;
1266      enum machine_mode mode;
1267      rtx addr;
1268 {
1269   rtx new;
1270
1271   if (GET_CODE (memref) != MEM)
1272     abort ();
1273   if (mode == VOIDmode)
1274     mode = GET_MODE (memref);
1275   if (addr == 0)
1276     addr = XEXP (memref, 0);
1277
1278   /* If reload is in progress or has completed, ADDR must be valid.
1279      Otherwise, we can call memory_address to make it valid.  */
1280   if (reload_completed || reload_in_progress)
1281     {
1282       if (! memory_address_p (mode, addr))
1283         abort ();
1284     }
1285   else
1286     addr = memory_address (mode, addr);
1287         
1288   new = gen_rtx (MEM, mode, addr);
1289   MEM_VOLATILE_P (new) = MEM_VOLATILE_P (memref);
1290   RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (memref);
1291   MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (memref);
1292   return new;
1293 }
1294 \f
1295 /* Return a newly created CODE_LABEL rtx with a unique label number.  */
1296
1297 rtx
1298 gen_label_rtx ()
1299 {
1300   register rtx label;
1301
1302   label = (output_bytecode
1303            ? gen_rtx (CODE_LABEL, VOIDmode, NULL, bc_get_bytecode_label ())
1304            : gen_rtx (CODE_LABEL, VOIDmode, 0, 0, 0, label_num++, NULL_PTR));
1305
1306   LABEL_NUSES (label) = 0;
1307   return label;
1308 }
1309 \f
1310 /* For procedure integration.  */
1311
1312 /* Return a newly created INLINE_HEADER rtx.  Should allocate this
1313    from a permanent obstack when the opportunity arises.  */
1314
1315 rtx
1316 gen_inline_header_rtx (first_insn, first_parm_insn, first_labelno,
1317                        last_labelno, max_parm_regnum, max_regnum, args_size,
1318                        pops_args, stack_slots, function_flags,
1319                        outgoing_args_size, original_arg_vector,
1320                        original_decl_initial)
1321      rtx first_insn, first_parm_insn;
1322      int first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size;
1323      int pops_args;
1324      rtx stack_slots;
1325      int function_flags;
1326      int outgoing_args_size;
1327      rtvec original_arg_vector;
1328      rtx original_decl_initial;
1329 {
1330   rtx header = gen_rtx (INLINE_HEADER, VOIDmode,
1331                         cur_insn_uid++, NULL_RTX,
1332                         first_insn, first_parm_insn,
1333                         first_labelno, last_labelno,
1334                         max_parm_regnum, max_regnum, args_size, pops_args,
1335                         stack_slots, function_flags, outgoing_args_size,
1336                         original_arg_vector, original_decl_initial);
1337   return header;
1338 }
1339
1340 /* Install new pointers to the first and last insns in the chain.
1341    Used for an inline-procedure after copying the insn chain.  */
1342
1343 void
1344 set_new_first_and_last_insn (first, last)
1345      rtx first, last;
1346 {
1347   first_insn = first;
1348   last_insn = last;
1349 }
1350
1351 /* Set the range of label numbers found in the current function.
1352    This is used when belatedly compiling an inline function.  */
1353
1354 void
1355 set_new_first_and_last_label_num (first, last)
1356      int first, last;
1357 {
1358   base_label_num = label_num;
1359   first_label_num = first;
1360   last_label_num = last;
1361 }
1362 \f
1363 /* Save all variables describing the current status into the structure *P.
1364    This is used before starting a nested function.  */
1365
1366 void
1367 save_emit_status (p)
1368      struct function *p;
1369 {
1370   p->reg_rtx_no = reg_rtx_no;
1371   p->first_label_num = first_label_num;
1372   p->first_insn = first_insn;
1373   p->last_insn = last_insn;
1374   p->sequence_rtl_expr = sequence_rtl_expr;
1375   p->sequence_stack = sequence_stack;
1376   p->cur_insn_uid = cur_insn_uid;
1377   p->last_linenum = last_linenum;
1378   p->last_filename = last_filename;
1379   p->regno_pointer_flag = regno_pointer_flag;
1380   p->regno_pointer_flag_length = regno_pointer_flag_length;
1381   p->regno_reg_rtx = regno_reg_rtx;
1382 }
1383
1384 /* Restore all variables describing the current status from the structure *P.
1385    This is used after a nested function.  */
1386
1387 void
1388 restore_emit_status (p)
1389      struct function *p;
1390 {
1391   int i;
1392
1393   reg_rtx_no = p->reg_rtx_no;
1394   first_label_num = p->first_label_num;
1395   last_label_num = 0;
1396   first_insn = p->first_insn;
1397   last_insn = p->last_insn;
1398   sequence_rtl_expr = p->sequence_rtl_expr;
1399   sequence_stack = p->sequence_stack;
1400   cur_insn_uid = p->cur_insn_uid;
1401   last_linenum = p->last_linenum;
1402   last_filename = p->last_filename;
1403   regno_pointer_flag = p->regno_pointer_flag;
1404   regno_pointer_flag_length = p->regno_pointer_flag_length;
1405   regno_reg_rtx = p->regno_reg_rtx;
1406
1407   /* Clear our cache of rtx expressions for start_sequence and gen_sequence. */
1408   sequence_element_free_list = 0;
1409   for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
1410     sequence_result[i] = 0;
1411 }
1412 \f
1413 /* Go through all the RTL insn bodies and copy any invalid shared structure.
1414    It does not work to do this twice, because the mark bits set here
1415    are not cleared afterwards.  */
1416
1417 void
1418 unshare_all_rtl (insn)
1419      register rtx insn;
1420 {
1421   for (; insn; insn = NEXT_INSN (insn))
1422     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
1423         || GET_CODE (insn) == CALL_INSN)
1424       {
1425         PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
1426         REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
1427         LOG_LINKS (insn) = copy_rtx_if_shared (LOG_LINKS (insn));
1428       }
1429
1430   /* Make sure the addresses of stack slots found outside the insn chain
1431      (such as, in DECL_RTL of a variable) are not shared
1432      with the insn chain.
1433
1434      This special care is necessary when the stack slot MEM does not
1435      actually appear in the insn chain.  If it does appear, its address
1436      is unshared from all else at that point.  */
1437
1438   copy_rtx_if_shared (stack_slot_list);
1439 }
1440
1441 /* Mark ORIG as in use, and return a copy of it if it was already in use.
1442    Recursively does the same for subexpressions.  */
1443
1444 rtx
1445 copy_rtx_if_shared (orig)
1446      rtx orig;
1447 {
1448   register rtx x = orig;
1449   register int i;
1450   register enum rtx_code code;
1451   register char *format_ptr;
1452   int copied = 0;
1453
1454   if (x == 0)
1455     return 0;
1456
1457   code = GET_CODE (x);
1458
1459   /* These types may be freely shared.  */
1460
1461   switch (code)
1462     {
1463     case REG:
1464     case QUEUED:
1465     case CONST_INT:
1466     case CONST_DOUBLE:
1467     case SYMBOL_REF:
1468     case CODE_LABEL:
1469     case PC:
1470     case CC0:
1471     case SCRATCH:
1472       /* SCRATCH must be shared because they represent distinct values. */
1473       return x;
1474
1475     case CONST:
1476       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
1477          a LABEL_REF, it isn't sharable.  */
1478       if (GET_CODE (XEXP (x, 0)) == PLUS
1479           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1480           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1481         return x;
1482       break;
1483
1484     case INSN:
1485     case JUMP_INSN:
1486     case CALL_INSN:
1487     case NOTE:
1488     case BARRIER:
1489       /* The chain of insns is not being copied.  */
1490       return x;
1491
1492     case MEM:
1493       /* A MEM is allowed to be shared if its address is constant
1494          or is a constant plus one of the special registers.  */
1495       if (CONSTANT_ADDRESS_P (XEXP (x, 0))
1496           || XEXP (x, 0) == virtual_stack_vars_rtx
1497           || XEXP (x, 0) == virtual_incoming_args_rtx)
1498         return x;
1499
1500       if (GET_CODE (XEXP (x, 0)) == PLUS
1501           && (XEXP (XEXP (x, 0), 0) == virtual_stack_vars_rtx
1502               || XEXP (XEXP (x, 0), 0) == virtual_incoming_args_rtx)
1503           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
1504         {
1505           /* This MEM can appear in more than one place,
1506              but its address better not be shared with anything else.  */
1507           if (! x->used)
1508             XEXP (x, 0) = copy_rtx_if_shared (XEXP (x, 0));
1509           x->used = 1;
1510           return x;
1511         }
1512     }
1513
1514   /* This rtx may not be shared.  If it has already been seen,
1515      replace it with a copy of itself.  */
1516
1517   if (x->used)
1518     {
1519       register rtx copy;
1520
1521       copy = rtx_alloc (code);
1522       bcopy (x, copy, (sizeof (*copy) - sizeof (copy->fld)
1523                        + sizeof (copy->fld[0]) * GET_RTX_LENGTH (code)));
1524       x = copy;
1525       copied = 1;
1526     }
1527   x->used = 1;
1528
1529   /* Now scan the subexpressions recursively.
1530      We can store any replaced subexpressions directly into X
1531      since we know X is not shared!  Any vectors in X
1532      must be copied if X was copied.  */
1533
1534   format_ptr = GET_RTX_FORMAT (code);
1535
1536   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1537     {
1538       switch (*format_ptr++)
1539         {
1540         case 'e':
1541           XEXP (x, i) = copy_rtx_if_shared (XEXP (x, i));
1542           break;
1543
1544         case 'E':
1545           if (XVEC (x, i) != NULL)
1546             {
1547               register int j;
1548               int len = XVECLEN (x, i);
1549
1550               if (copied && len > 0)
1551                 XVEC (x, i) = gen_rtvec_v (len, &XVECEXP (x, i, 0));
1552               for (j = 0; j < len; j++)
1553                 XVECEXP (x, i, j) = copy_rtx_if_shared (XVECEXP (x, i, j));
1554             }
1555           break;
1556         }
1557     }
1558   return x;
1559 }
1560
1561 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
1562    to look for shared sub-parts.  */
1563
1564 void
1565 reset_used_flags (x)
1566      rtx x;
1567 {
1568   register int i, j;
1569   register enum rtx_code code;
1570   register char *format_ptr;
1571
1572   if (x == 0)
1573     return;
1574
1575   code = GET_CODE (x);
1576
1577   /* These types may be freely shared so we needn't do any reseting
1578      for them.  */
1579
1580   switch (code)
1581     {
1582     case REG:
1583     case QUEUED:
1584     case CONST_INT:
1585     case CONST_DOUBLE:
1586     case SYMBOL_REF:
1587     case CODE_LABEL:
1588     case PC:
1589     case CC0:
1590       return;
1591
1592     case INSN:
1593     case JUMP_INSN:
1594     case CALL_INSN:
1595     case NOTE:
1596     case LABEL_REF:
1597     case BARRIER:
1598       /* The chain of insns is not being copied.  */
1599       return;
1600     }
1601
1602   x->used = 0;
1603
1604   format_ptr = GET_RTX_FORMAT (code);
1605   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1606     {
1607       switch (*format_ptr++)
1608         {
1609         case 'e':
1610           reset_used_flags (XEXP (x, i));
1611           break;
1612
1613         case 'E':
1614           for (j = 0; j < XVECLEN (x, i); j++)
1615             reset_used_flags (XVECEXP (x, i, j));
1616           break;
1617         }
1618     }
1619 }
1620 \f
1621 /* Copy X if necessary so that it won't be altered by changes in OTHER.
1622    Return X or the rtx for the pseudo reg the value of X was copied into.
1623    OTHER must be valid as a SET_DEST.  */
1624
1625 rtx
1626 make_safe_from (x, other)
1627      rtx x, other;
1628 {
1629   while (1)
1630     switch (GET_CODE (other))
1631       {
1632       case SUBREG:
1633         other = SUBREG_REG (other);
1634         break;
1635       case STRICT_LOW_PART:
1636       case SIGN_EXTEND:
1637       case ZERO_EXTEND:
1638         other = XEXP (other, 0);
1639         break;
1640       default:
1641         goto done;
1642       }
1643  done:
1644   if ((GET_CODE (other) == MEM
1645        && ! CONSTANT_P (x)
1646        && GET_CODE (x) != REG
1647        && GET_CODE (x) != SUBREG)
1648       || (GET_CODE (other) == REG
1649           && (REGNO (other) < FIRST_PSEUDO_REGISTER
1650               || reg_mentioned_p (other, x))))
1651     {
1652       rtx temp = gen_reg_rtx (GET_MODE (x));
1653       emit_move_insn (temp, x);
1654       return temp;
1655     }
1656   return x;
1657 }
1658 \f
1659 /* Emission of insns (adding them to the doubly-linked list).  */
1660
1661 /* Return the first insn of the current sequence or current function.  */
1662
1663 rtx
1664 get_insns ()
1665 {
1666   return first_insn;
1667 }
1668
1669 /* Return the last insn emitted in current sequence or current function.  */
1670
1671 rtx
1672 get_last_insn ()
1673 {
1674   return last_insn;
1675 }
1676
1677 /* Specify a new insn as the last in the chain.  */
1678
1679 void
1680 set_last_insn (insn)
1681      rtx insn;
1682 {
1683   if (NEXT_INSN (insn) != 0)
1684     abort ();
1685   last_insn = insn;
1686 }
1687
1688 /* Return the last insn emitted, even if it is in a sequence now pushed.  */
1689
1690 rtx
1691 get_last_insn_anywhere ()
1692 {
1693   struct sequence_stack *stack;
1694   if (last_insn)
1695     return last_insn;
1696   for (stack = sequence_stack; stack; stack = stack->next)
1697     if (stack->last != 0)
1698       return stack->last;
1699   return 0;
1700 }
1701
1702 /* Return a number larger than any instruction's uid in this function.  */
1703
1704 int
1705 get_max_uid ()
1706 {
1707   return cur_insn_uid;
1708 }
1709 \f
1710 /* Return the next insn.  If it is a SEQUENCE, return the first insn
1711    of the sequence.  */
1712
1713 rtx
1714 next_insn (insn)
1715      rtx insn;
1716 {
1717   if (insn)
1718     {
1719       insn = NEXT_INSN (insn);
1720       if (insn && GET_CODE (insn) == INSN
1721           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1722         insn = XVECEXP (PATTERN (insn), 0, 0);
1723     }
1724
1725   return insn;
1726 }
1727
1728 /* Return the previous insn.  If it is a SEQUENCE, return the last insn
1729    of the sequence.  */
1730
1731 rtx
1732 previous_insn (insn)
1733      rtx insn;
1734 {
1735   if (insn)
1736     {
1737       insn = PREV_INSN (insn);
1738       if (insn && GET_CODE (insn) == INSN
1739           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1740         insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
1741     }
1742
1743   return insn;
1744 }
1745
1746 /* Return the next insn after INSN that is not a NOTE.  This routine does not
1747    look inside SEQUENCEs.  */
1748
1749 rtx
1750 next_nonnote_insn (insn)
1751      rtx insn;
1752 {
1753   while (insn)
1754     {
1755       insn = NEXT_INSN (insn);
1756       if (insn == 0 || GET_CODE (insn) != NOTE)
1757         break;
1758     }
1759
1760   return insn;
1761 }
1762
1763 /* Return the previous insn before INSN that is not a NOTE.  This routine does
1764    not look inside SEQUENCEs.  */
1765
1766 rtx
1767 prev_nonnote_insn (insn)
1768      rtx insn;
1769 {
1770   while (insn)
1771     {
1772       insn = PREV_INSN (insn);
1773       if (insn == 0 || GET_CODE (insn) != NOTE)
1774         break;
1775     }
1776
1777   return insn;
1778 }
1779
1780 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
1781    or 0, if there is none.  This routine does not look inside
1782    SEQUENCEs. */
1783
1784 rtx
1785 next_real_insn (insn)
1786      rtx insn;
1787 {
1788   while (insn)
1789     {
1790       insn = NEXT_INSN (insn);
1791       if (insn == 0 || GET_CODE (insn) == INSN
1792           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
1793         break;
1794     }
1795
1796   return insn;
1797 }
1798
1799 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
1800    or 0, if there is none.  This routine does not look inside
1801    SEQUENCEs.  */
1802
1803 rtx
1804 prev_real_insn (insn)
1805      rtx insn;
1806 {
1807   while (insn)
1808     {
1809       insn = PREV_INSN (insn);
1810       if (insn == 0 || GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
1811           || GET_CODE (insn) == JUMP_INSN)
1812         break;
1813     }
1814
1815   return insn;
1816 }
1817
1818 /* Find the next insn after INSN that really does something.  This routine
1819    does not look inside SEQUENCEs.  Until reload has completed, this is the
1820    same as next_real_insn.  */
1821
1822 rtx
1823 next_active_insn (insn)
1824      rtx insn;
1825 {
1826   while (insn)
1827     {
1828       insn = NEXT_INSN (insn);
1829       if (insn == 0
1830           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1831           || (GET_CODE (insn) == INSN
1832               && (! reload_completed
1833                   || (GET_CODE (PATTERN (insn)) != USE
1834                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
1835         break;
1836     }
1837
1838   return insn;
1839 }
1840
1841 /* Find the last insn before INSN that really does something.  This routine
1842    does not look inside SEQUENCEs.  Until reload has completed, this is the
1843    same as prev_real_insn.  */
1844
1845 rtx
1846 prev_active_insn (insn)
1847      rtx insn;
1848 {
1849   while (insn)
1850     {
1851       insn = PREV_INSN (insn);
1852       if (insn == 0
1853           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1854           || (GET_CODE (insn) == INSN
1855               && (! reload_completed
1856                   || (GET_CODE (PATTERN (insn)) != USE
1857                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
1858         break;
1859     }
1860
1861   return insn;
1862 }
1863
1864 /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none.  */
1865
1866 rtx
1867 next_label (insn)
1868      rtx insn;
1869 {
1870   while (insn)
1871     {
1872       insn = NEXT_INSN (insn);
1873       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1874         break;
1875     }
1876
1877   return insn;
1878 }
1879
1880 /* Return the last CODE_LABEL before the insn INSN, or 0 if there is none.  */
1881
1882 rtx
1883 prev_label (insn)
1884      rtx insn;
1885 {
1886   while (insn)
1887     {
1888       insn = PREV_INSN (insn);
1889       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1890         break;
1891     }
1892
1893   return insn;
1894 }
1895 \f
1896 #ifdef HAVE_cc0
1897 /* INSN uses CC0 and is being moved into a delay slot.  Set up REG_CC_SETTER
1898    and REG_CC_USER notes so we can find it.  */
1899
1900 void
1901 link_cc0_insns (insn)
1902      rtx insn;
1903 {
1904   rtx user = next_nonnote_insn (insn);
1905
1906   if (GET_CODE (user) == INSN && GET_CODE (PATTERN (user)) == SEQUENCE)
1907     user = XVECEXP (PATTERN (user), 0, 0);
1908
1909   REG_NOTES (user) = gen_rtx (INSN_LIST, REG_CC_SETTER, insn,
1910                               REG_NOTES (user));
1911   REG_NOTES (insn) = gen_rtx (INSN_LIST, REG_CC_USER, user, REG_NOTES (insn));
1912 }
1913
1914 /* Return the next insn that uses CC0 after INSN, which is assumed to
1915    set it.  This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
1916    applied to the result of this function should yield INSN).
1917
1918    Normally, this is simply the next insn.  However, if a REG_CC_USER note
1919    is present, it contains the insn that uses CC0.
1920
1921    Return 0 if we can't find the insn.  */
1922
1923 rtx
1924 next_cc0_user (insn)
1925      rtx insn;
1926 {
1927   rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
1928
1929   if (note)
1930     return XEXP (note, 0);
1931
1932   insn = next_nonnote_insn (insn);
1933   if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
1934     insn = XVECEXP (PATTERN (insn), 0, 0);
1935
1936   if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1937       && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
1938     return insn;
1939
1940   return 0;
1941 }
1942
1943 /* Find the insn that set CC0 for INSN.  Unless INSN has a REG_CC_SETTER
1944    note, it is the previous insn.  */
1945
1946 rtx
1947 prev_cc0_setter (insn)
1948      rtx insn;
1949 {
1950   rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
1951   rtx link;
1952
1953   if (note)
1954     return XEXP (note, 0);
1955
1956   insn = prev_nonnote_insn (insn);
1957   if (! sets_cc0_p (PATTERN (insn)))
1958     abort ();
1959
1960   return insn;
1961 }
1962 #endif
1963 \f
1964 /* Try splitting insns that can be split for better scheduling.
1965    PAT is the pattern which might split.
1966    TRIAL is the insn providing PAT.
1967    LAST is non-zero if we should return the last insn of the sequence produced.
1968
1969    If this routine succeeds in splitting, it returns the first or last
1970    replacement insn depending on the value of LAST.  Otherwise, it
1971    returns TRIAL.  If the insn to be returned can be split, it will be.  */
1972
1973 rtx
1974 try_split (pat, trial, last)
1975      rtx pat, trial;
1976      int last;
1977 {
1978   rtx before = PREV_INSN (trial);
1979   rtx after = NEXT_INSN (trial);
1980   rtx seq = split_insns (pat, trial);
1981   int has_barrier = 0;
1982   rtx tem;
1983
1984   /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
1985      We may need to handle this specially.  */
1986   if (after && GET_CODE (after) == BARRIER)
1987     {
1988       has_barrier = 1;
1989       after = NEXT_INSN (after);
1990     }
1991
1992   if (seq)
1993     {
1994       /* SEQ can either be a SEQUENCE or the pattern of a single insn.
1995          The latter case will normally arise only when being done so that
1996          it, in turn, will be split (SFmode on the 29k is an example).  */
1997       if (GET_CODE (seq) == SEQUENCE)
1998         {
1999           /* If we are splitting a JUMP_INSN, look for the JUMP_INSN in
2000              SEQ and copy our JUMP_LABEL to it.  If JUMP_LABEL is non-zero,
2001              increment the usage count so we don't delete the label.  */
2002           int i;
2003
2004           if (GET_CODE (trial) == JUMP_INSN)
2005             for (i = XVECLEN (seq, 0) - 1; i >= 0; i--)
2006               if (GET_CODE (XVECEXP (seq, 0, i)) == JUMP_INSN)
2007                 {
2008                   JUMP_LABEL (XVECEXP (seq, 0, i)) = JUMP_LABEL (trial);
2009
2010                   if (JUMP_LABEL (trial))
2011                     LABEL_NUSES (JUMP_LABEL (trial))++;
2012                 }
2013
2014           tem = emit_insn_after (seq, before);
2015
2016           delete_insn (trial);
2017           if (has_barrier)
2018             emit_barrier_after (tem);
2019
2020           /* Recursively call try_split for each new insn created; by the
2021              time control returns here that insn will be fully split, so
2022              set LAST and continue from the insn after the one returned.
2023              We can't use next_active_insn here since AFTER may be a note. */
2024           for (tem = NEXT_INSN (before); tem != after;
2025                tem = NEXT_INSN (tem))
2026             tem = try_split (PATTERN (tem), tem, 1);
2027         }
2028       /* Avoid infinite loop if the result matches the original pattern.  */
2029       else if (rtx_equal_p (seq, pat))
2030         return trial;
2031       else
2032         {
2033           PATTERN (trial) = seq;
2034           INSN_CODE (trial) = -1;
2035           try_split (seq, trial, last);
2036         }
2037
2038       /* Return either the first or the last insn, depending on which was
2039          requested.  */
2040       return last ? prev_active_insn (after) : next_active_insn (before);
2041     }
2042
2043   return trial;
2044 }
2045 \f
2046 /* Make and return an INSN rtx, initializing all its slots.
2047    Store PATTERN in the pattern slots.  */
2048
2049 rtx
2050 make_insn_raw (pattern)
2051      rtx pattern;
2052 {
2053   register rtx insn;
2054
2055   insn = rtx_alloc (INSN);
2056   INSN_UID (insn) = cur_insn_uid++;
2057
2058   PATTERN (insn) = pattern;
2059   INSN_CODE (insn) = -1;
2060   LOG_LINKS (insn) = NULL;
2061   REG_NOTES (insn) = NULL;
2062
2063   return insn;
2064 }
2065
2066 /* Like `make_insn' but make a JUMP_INSN instead of an insn.  */
2067
2068 static rtx
2069 make_jump_insn_raw (pattern)
2070      rtx pattern;
2071 {
2072   register rtx insn;
2073
2074   insn = rtx_alloc (JUMP_INSN);
2075   INSN_UID (insn) = cur_insn_uid++;
2076
2077   PATTERN (insn) = pattern;
2078   INSN_CODE (insn) = -1;
2079   LOG_LINKS (insn) = NULL;
2080   REG_NOTES (insn) = NULL;
2081   JUMP_LABEL (insn) = NULL;
2082
2083   return insn;
2084 }
2085 \f
2086 /* Add INSN to the end of the doubly-linked list.
2087    INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE.  */
2088
2089 void
2090 add_insn (insn)
2091      register rtx insn;
2092 {
2093   PREV_INSN (insn) = last_insn;
2094   NEXT_INSN (insn) = 0;
2095
2096   if (NULL != last_insn)
2097     NEXT_INSN (last_insn) = insn;
2098
2099   if (NULL == first_insn)
2100     first_insn = insn;
2101
2102   last_insn = insn;
2103 }
2104
2105 /* Add INSN into the doubly-linked list after insn AFTER.  This should be the
2106    only function called to insert an insn once delay slots have been filled
2107    since only it knows how to update a SEQUENCE.  */
2108
2109 void
2110 add_insn_after (insn, after)
2111      rtx insn, after;
2112 {
2113   rtx next = NEXT_INSN (after);
2114
2115   NEXT_INSN (insn) = next;
2116   PREV_INSN (insn) = after;
2117
2118   if (next)
2119     {
2120       PREV_INSN (next) = insn;
2121       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2122         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
2123     }
2124   else if (last_insn == after)
2125     last_insn = insn;
2126   else
2127     {
2128       struct sequence_stack *stack = sequence_stack;
2129       /* Scan all pending sequences too.  */
2130       for (; stack; stack = stack->next)
2131         if (after == stack->last)
2132           stack->last = insn;
2133     }
2134
2135   NEXT_INSN (after) = insn;
2136   if (GET_CODE (after) == INSN && GET_CODE (PATTERN (after)) == SEQUENCE)
2137     {
2138       rtx sequence = PATTERN (after);
2139       NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
2140     }
2141 }
2142
2143 /* Delete all insns made since FROM.
2144    FROM becomes the new last instruction.  */
2145
2146 void
2147 delete_insns_since (from)
2148      rtx from;
2149 {
2150   if (from == 0)
2151     first_insn = 0;
2152   else
2153     NEXT_INSN (from) = 0;
2154   last_insn = from;
2155 }
2156
2157 /* Move a consecutive bunch of insns to a different place in the chain.
2158    The insns to be moved are those between FROM and TO.
2159    They are moved to a new position after the insn AFTER.
2160    AFTER must not be FROM or TO or any insn in between.
2161
2162    This function does not know about SEQUENCEs and hence should not be
2163    called after delay-slot filling has been done.  */
2164
2165 void
2166 reorder_insns (from, to, after)
2167      rtx from, to, after;
2168 {
2169   /* Splice this bunch out of where it is now.  */
2170   if (PREV_INSN (from))
2171     NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
2172   if (NEXT_INSN (to))
2173     PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
2174   if (last_insn == to)
2175     last_insn = PREV_INSN (from);
2176   if (first_insn == from)
2177     first_insn = NEXT_INSN (to);
2178
2179   /* Make the new neighbors point to it and it to them.  */
2180   if (NEXT_INSN (after))
2181     PREV_INSN (NEXT_INSN (after)) = to;
2182
2183   NEXT_INSN (to) = NEXT_INSN (after);
2184   PREV_INSN (from) = after;
2185   NEXT_INSN (after) = from;
2186   if (after == last_insn)
2187     last_insn = to;
2188 }
2189
2190 /* Return the line note insn preceding INSN.  */
2191
2192 static rtx
2193 find_line_note (insn)
2194      rtx insn;
2195 {
2196   if (no_line_numbers)
2197     return 0;
2198
2199   for (; insn; insn = PREV_INSN (insn))
2200     if (GET_CODE (insn) == NOTE
2201         && NOTE_LINE_NUMBER (insn) >= 0)
2202       break;
2203
2204   return insn;
2205 }
2206
2207 /* Like reorder_insns, but inserts line notes to preserve the line numbers
2208    of the moved insns when debugging.  This may insert a note between AFTER
2209    and FROM, and another one after TO.  */
2210
2211 void
2212 reorder_insns_with_line_notes (from, to, after)
2213      rtx from, to, after;
2214 {
2215   rtx from_line = find_line_note (from);
2216   rtx after_line = find_line_note (after);
2217
2218   reorder_insns (from, to, after);
2219
2220   if (from_line == after_line)
2221     return;
2222
2223   if (from_line)
2224     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
2225                           NOTE_LINE_NUMBER (from_line),
2226                           after);
2227   if (after_line)
2228     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
2229                           NOTE_LINE_NUMBER (after_line),
2230                           to);
2231 }
2232 \f
2233 /* Emit an insn of given code and pattern
2234    at a specified place within the doubly-linked list.  */
2235
2236 /* Make an instruction with body PATTERN
2237    and output it before the instruction BEFORE.  */
2238
2239 rtx
2240 emit_insn_before (pattern, before)
2241      register rtx pattern, before;
2242 {
2243   register rtx insn = before;
2244
2245   if (GET_CODE (pattern) == SEQUENCE)
2246     {
2247       register int i;
2248
2249       for (i = 0; i < XVECLEN (pattern, 0); i++)
2250         {
2251           insn = XVECEXP (pattern, 0, i);
2252           add_insn_after (insn, PREV_INSN (before));
2253         }
2254       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2255         sequence_result[XVECLEN (pattern, 0)] = pattern;
2256     }
2257   else
2258     {
2259       insn = make_insn_raw (pattern);
2260       add_insn_after (insn, PREV_INSN (before));
2261     }
2262
2263   return insn;
2264 }
2265
2266 /* Make an instruction with body PATTERN and code JUMP_INSN
2267    and output it before the instruction BEFORE.  */
2268
2269 rtx
2270 emit_jump_insn_before (pattern, before)
2271      register rtx pattern, before;
2272 {
2273   register rtx insn;
2274
2275   if (GET_CODE (pattern) == SEQUENCE)
2276     insn = emit_insn_before (pattern, before);
2277   else
2278     {
2279       insn = make_jump_insn_raw (pattern);
2280       add_insn_after (insn, PREV_INSN (before));
2281     }
2282
2283   return insn;
2284 }
2285
2286 /* Make an instruction with body PATTERN and code CALL_INSN
2287    and output it before the instruction BEFORE.  */
2288
2289 rtx
2290 emit_call_insn_before (pattern, before)
2291      register rtx pattern, before;
2292 {
2293   rtx insn = emit_insn_before (pattern, before);
2294   PUT_CODE (insn, CALL_INSN);
2295   return insn;
2296 }
2297
2298 /* Make an insn of code BARRIER
2299    and output it before the insn AFTER.  */
2300
2301 rtx
2302 emit_barrier_before (before)
2303      register rtx before;
2304 {
2305   register rtx insn = rtx_alloc (BARRIER);
2306
2307   INSN_UID (insn) = cur_insn_uid++;
2308
2309   add_insn_after (insn, PREV_INSN (before));
2310   return insn;
2311 }
2312
2313 /* Emit a note of subtype SUBTYPE before the insn BEFORE.  */
2314
2315 rtx
2316 emit_note_before (subtype, before)
2317      int subtype;
2318      rtx before;
2319 {
2320   register rtx note = rtx_alloc (NOTE);
2321   INSN_UID (note) = cur_insn_uid++;
2322   NOTE_SOURCE_FILE (note) = 0;
2323   NOTE_LINE_NUMBER (note) = subtype;
2324
2325   add_insn_after (note, PREV_INSN (before));
2326   return note;
2327 }
2328 \f
2329 /* Make an insn of code INSN with body PATTERN
2330    and output it after the insn AFTER.  */
2331
2332 rtx
2333 emit_insn_after (pattern, after)
2334      register rtx pattern, after;
2335 {
2336   register rtx insn = after;
2337
2338   if (GET_CODE (pattern) == SEQUENCE)
2339     {
2340       register int i;
2341
2342       for (i = 0; i < XVECLEN (pattern, 0); i++)
2343         {
2344           insn = XVECEXP (pattern, 0, i);
2345           add_insn_after (insn, after);
2346           after = insn;
2347         }
2348       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2349         sequence_result[XVECLEN (pattern, 0)] = pattern;
2350     }
2351   else
2352     {
2353       insn = make_insn_raw (pattern);
2354       add_insn_after (insn, after);
2355     }
2356
2357   return insn;
2358 }
2359
2360 /* Similar to emit_insn_after, except that line notes are to be inserted so
2361    as to act as if this insn were at FROM.  */
2362
2363 void
2364 emit_insn_after_with_line_notes (pattern, after, from)
2365      rtx pattern, after, from;
2366 {
2367   rtx from_line = find_line_note (from);
2368   rtx after_line = find_line_note (after);
2369   rtx insn = emit_insn_after (pattern, after);
2370
2371   if (from_line)
2372     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
2373                           NOTE_LINE_NUMBER (from_line),
2374                           after);
2375
2376   if (after_line)
2377     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
2378                           NOTE_LINE_NUMBER (after_line),
2379                           insn);
2380 }
2381
2382 /* Make an insn of code JUMP_INSN with body PATTERN
2383    and output it after the insn AFTER.  */
2384
2385 rtx
2386 emit_jump_insn_after (pattern, after)
2387      register rtx pattern, after;
2388 {
2389   register rtx insn;
2390
2391   if (GET_CODE (pattern) == SEQUENCE)
2392     insn = emit_insn_after (pattern, after);
2393   else
2394     {
2395       insn = make_jump_insn_raw (pattern);
2396       add_insn_after (insn, after);
2397     }
2398
2399   return insn;
2400 }
2401
2402 /* Make an insn of code BARRIER
2403    and output it after the insn AFTER.  */
2404
2405 rtx
2406 emit_barrier_after (after)
2407      register rtx after;
2408 {
2409   register rtx insn = rtx_alloc (BARRIER);
2410
2411   INSN_UID (insn) = cur_insn_uid++;
2412
2413   add_insn_after (insn, after);
2414   return insn;
2415 }
2416
2417 /* Emit the label LABEL after the insn AFTER.  */
2418
2419 rtx
2420 emit_label_after (label, after)
2421      rtx label, after;
2422 {
2423   /* This can be called twice for the same label
2424      as a result of the confusion that follows a syntax error!
2425      So make it harmless.  */
2426   if (INSN_UID (label) == 0)
2427     {
2428       INSN_UID (label) = cur_insn_uid++;
2429       add_insn_after (label, after);
2430     }
2431
2432   return label;
2433 }
2434
2435 /* Emit a note of subtype SUBTYPE after the insn AFTER.  */
2436
2437 rtx
2438 emit_note_after (subtype, after)
2439      int subtype;
2440      rtx after;
2441 {
2442   register rtx note = rtx_alloc (NOTE);
2443   INSN_UID (note) = cur_insn_uid++;
2444   NOTE_SOURCE_FILE (note) = 0;
2445   NOTE_LINE_NUMBER (note) = subtype;
2446   add_insn_after (note, after);
2447   return note;
2448 }
2449
2450 /* Emit a line note for FILE and LINE after the insn AFTER.  */
2451
2452 rtx
2453 emit_line_note_after (file, line, after)
2454      char *file;
2455      int line;
2456      rtx after;
2457 {
2458   register rtx note;
2459
2460   if (no_line_numbers && line > 0)
2461     {
2462       cur_insn_uid++;
2463       return 0;
2464     }
2465
2466   note  = rtx_alloc (NOTE);
2467   INSN_UID (note) = cur_insn_uid++;
2468   NOTE_SOURCE_FILE (note) = file;
2469   NOTE_LINE_NUMBER (note) = line;
2470   add_insn_after (note, after);
2471   return note;
2472 }
2473 \f
2474 /* Make an insn of code INSN with pattern PATTERN
2475    and add it to the end of the doubly-linked list.
2476    If PATTERN is a SEQUENCE, take the elements of it
2477    and emit an insn for each element.
2478
2479    Returns the last insn emitted.  */
2480
2481 rtx
2482 emit_insn (pattern)
2483      rtx pattern;
2484 {
2485   rtx insn = last_insn;
2486
2487   if (GET_CODE (pattern) == SEQUENCE)
2488     {
2489       register int i;
2490
2491       for (i = 0; i < XVECLEN (pattern, 0); i++)
2492         {
2493           insn = XVECEXP (pattern, 0, i);
2494           add_insn (insn);
2495         }
2496       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2497         sequence_result[XVECLEN (pattern, 0)] = pattern;
2498     }
2499   else
2500     {
2501       insn = make_insn_raw (pattern);
2502       add_insn (insn);
2503     }
2504
2505   return insn;
2506 }
2507
2508 /* Emit the insns in a chain starting with INSN.
2509    Return the last insn emitted.  */
2510
2511 rtx
2512 emit_insns (insn)
2513      rtx insn;
2514 {
2515   rtx last = 0;
2516
2517   while (insn)
2518     {
2519       rtx next = NEXT_INSN (insn);
2520       add_insn (insn);
2521       last = insn;
2522       insn = next;
2523     }
2524
2525   return last;
2526 }
2527
2528 /* Emit the insns in a chain starting with INSN and place them in front of
2529    the insn BEFORE.  Return the last insn emitted.  */
2530
2531 rtx
2532 emit_insns_before (insn, before)
2533      rtx insn;
2534      rtx before;
2535 {
2536   rtx last = 0;
2537
2538   while (insn)
2539     {
2540       rtx next = NEXT_INSN (insn);
2541       add_insn_after (insn, PREV_INSN (before));
2542       last = insn;
2543       insn = next;
2544     }
2545
2546   return last;
2547 }
2548
2549 /* Emit the insns in a chain starting with FIRST and place them in back of
2550    the insn AFTER.  Return the last insn emitted.  */
2551
2552 rtx
2553 emit_insns_after (first, after)
2554      register rtx first;
2555      register rtx after;
2556 {
2557   register rtx last;
2558   register rtx after_after;
2559
2560   if (!after)
2561     abort ();
2562
2563   if (!first)
2564     return first;
2565
2566   for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
2567     continue;
2568
2569   after_after = NEXT_INSN (after);
2570
2571   NEXT_INSN (after) = first;
2572   PREV_INSN (first) = after;
2573   NEXT_INSN (last) = after_after;
2574   if (after_after)
2575     PREV_INSN (after_after) = last;
2576
2577   if (after == last_insn)
2578     last_insn = last;
2579   return last;
2580 }
2581
2582 /* Make an insn of code JUMP_INSN with pattern PATTERN
2583    and add it to the end of the doubly-linked list.  */
2584
2585 rtx
2586 emit_jump_insn (pattern)
2587      rtx pattern;
2588 {
2589   if (GET_CODE (pattern) == SEQUENCE)
2590     return emit_insn (pattern);
2591   else
2592     {
2593       register rtx insn = make_jump_insn_raw (pattern);
2594       add_insn (insn);
2595       return insn;
2596     }
2597 }
2598
2599 /* Make an insn of code CALL_INSN with pattern PATTERN
2600    and add it to the end of the doubly-linked list.  */
2601
2602 rtx
2603 emit_call_insn (pattern)
2604      rtx pattern;
2605 {
2606   if (GET_CODE (pattern) == SEQUENCE)
2607     return emit_insn (pattern);
2608   else
2609     {
2610       register rtx insn = make_insn_raw (pattern);
2611       add_insn (insn);
2612       PUT_CODE (insn, CALL_INSN);
2613       return insn;
2614     }
2615 }
2616
2617 /* Add the label LABEL to the end of the doubly-linked list.  */
2618
2619 rtx
2620 emit_label (label)
2621      rtx label;
2622 {
2623   /* This can be called twice for the same label
2624      as a result of the confusion that follows a syntax error!
2625      So make it harmless.  */
2626   if (INSN_UID (label) == 0)
2627     {
2628       INSN_UID (label) = cur_insn_uid++;
2629       add_insn (label);
2630     }
2631   return label;
2632 }
2633
2634 /* Make an insn of code BARRIER
2635    and add it to the end of the doubly-linked list.  */
2636
2637 rtx
2638 emit_barrier ()
2639 {
2640   register rtx barrier = rtx_alloc (BARRIER);
2641   INSN_UID (barrier) = cur_insn_uid++;
2642   add_insn (barrier);
2643   return barrier;
2644 }
2645
2646 /* Make an insn of code NOTE
2647    with data-fields specified by FILE and LINE
2648    and add it to the end of the doubly-linked list,
2649    but only if line-numbers are desired for debugging info.  */
2650
2651 rtx
2652 emit_line_note (file, line)
2653      char *file;
2654      int line;
2655 {
2656   if (output_bytecode)
2657     {
2658       /* FIXME: for now we do nothing, but eventually we will have to deal with
2659          debugging information.  */
2660       return 0;
2661     }
2662
2663   emit_filename = file;
2664   emit_lineno = line;
2665
2666 #if 0
2667   if (no_line_numbers)
2668     return 0;
2669 #endif
2670
2671   return emit_note (file, line);
2672 }
2673
2674 /* Make an insn of code NOTE
2675    with data-fields specified by FILE and LINE
2676    and add it to the end of the doubly-linked list.
2677    If it is a line-number NOTE, omit it if it matches the previous one.  */
2678
2679 rtx
2680 emit_note (file, line)
2681      char *file;
2682      int line;
2683 {
2684   register rtx note;
2685
2686   if (line > 0)
2687     {
2688       if (file && last_filename && !strcmp (file, last_filename)
2689           && line == last_linenum)
2690         return 0;
2691       last_filename = file;
2692       last_linenum = line;
2693     }
2694
2695   if (no_line_numbers && line > 0)
2696     {
2697       cur_insn_uid++;
2698       return 0;
2699     }
2700
2701   note = rtx_alloc (NOTE);
2702   INSN_UID (note) = cur_insn_uid++;
2703   NOTE_SOURCE_FILE (note) = file;
2704   NOTE_LINE_NUMBER (note) = line;
2705   add_insn (note);
2706   return note;
2707 }
2708
2709 /* Emit a NOTE, and don't omit it even if LINE it the previous note.  */
2710
2711 rtx
2712 emit_line_note_force (file, line)
2713      char *file;
2714      int line;
2715 {
2716   last_linenum = -1;
2717   return emit_line_note (file, line);
2718 }
2719
2720 /* Cause next statement to emit a line note even if the line number
2721    has not changed.  This is used at the beginning of a function.  */
2722
2723 void
2724 force_next_line_note ()
2725 {
2726   last_linenum = -1;
2727 }
2728 \f
2729 /* Return an indication of which type of insn should have X as a body.
2730    The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN.  */
2731
2732 enum rtx_code
2733 classify_insn (x)
2734      rtx x;
2735 {
2736   if (GET_CODE (x) == CODE_LABEL)
2737     return CODE_LABEL;
2738   if (GET_CODE (x) == CALL)
2739     return CALL_INSN;
2740   if (GET_CODE (x) == RETURN)
2741     return JUMP_INSN;
2742   if (GET_CODE (x) == SET)
2743     {
2744       if (SET_DEST (x) == pc_rtx)
2745         return JUMP_INSN;
2746       else if (GET_CODE (SET_SRC (x)) == CALL)
2747         return CALL_INSN;
2748       else
2749         return INSN;
2750     }
2751   if (GET_CODE (x) == PARALLEL)
2752     {
2753       register int j;
2754       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
2755         if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
2756           return CALL_INSN;
2757         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2758                  && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
2759           return JUMP_INSN;
2760         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2761                  && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
2762           return CALL_INSN;
2763     }
2764   return INSN;
2765 }
2766
2767 /* Emit the rtl pattern X as an appropriate kind of insn.
2768    If X is a label, it is simply added into the insn chain.  */
2769
2770 rtx
2771 emit (x)
2772      rtx x;
2773 {
2774   enum rtx_code code = classify_insn (x);
2775
2776   if (code == CODE_LABEL)
2777     return emit_label (x);
2778   else if (code == INSN)
2779     return emit_insn (x);
2780   else if (code == JUMP_INSN)
2781     {
2782       register rtx insn = emit_jump_insn (x);
2783       if (simplejump_p (insn) || GET_CODE (x) == RETURN)
2784         return emit_barrier ();
2785       return insn;
2786     }
2787   else if (code == CALL_INSN)
2788     return emit_call_insn (x);
2789   else
2790     abort ();
2791 }
2792 \f
2793 /* Begin emitting insns to a sequence which can be packaged in an RTL_EXPR.  */
2794
2795 void
2796 start_sequence ()
2797 {
2798   struct sequence_stack *tem;
2799
2800   if (sequence_element_free_list)
2801     {
2802       /* Reuse a previously-saved struct sequence_stack.  */
2803       tem = sequence_element_free_list;
2804       sequence_element_free_list = tem->next;
2805     }
2806   else
2807     tem = (struct sequence_stack *) permalloc (sizeof (struct sequence_stack));
2808
2809   tem->next = sequence_stack;
2810   tem->first = first_insn;
2811   tem->last = last_insn;
2812   tem->sequence_rtl_expr = sequence_rtl_expr;
2813
2814   sequence_stack = tem;
2815
2816   first_insn = 0;
2817   last_insn = 0;
2818 }
2819
2820 /* Similarly, but indicate that this sequence will be placed in 
2821    T, an RTL_EXPR.  */
2822
2823 void
2824 start_sequence_for_rtl_expr (t)
2825      tree t;
2826 {
2827   start_sequence ();
2828
2829   sequence_rtl_expr = t;
2830 }
2831
2832 /* Set up the insn chain starting with FIRST
2833    as the current sequence, saving the previously current one.  */
2834
2835 void
2836 push_to_sequence (first)
2837      rtx first;
2838 {
2839   rtx last;
2840
2841   start_sequence ();
2842
2843   for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last));
2844
2845   first_insn = first;
2846   last_insn = last;
2847 }
2848
2849 /* Set up the outer-level insn chain
2850    as the current sequence, saving the previously current one.  */
2851
2852 void
2853 push_topmost_sequence ()
2854 {
2855   struct sequence_stack *stack, *top;
2856
2857   start_sequence ();
2858
2859   for (stack = sequence_stack; stack; stack = stack->next)
2860     top = stack;
2861
2862   first_insn = top->first;
2863   last_insn = top->last;
2864   sequence_rtl_expr = top->sequence_rtl_expr;
2865 }
2866
2867 /* After emitting to the outer-level insn chain, update the outer-level
2868    insn chain, and restore the previous saved state.  */
2869
2870 void
2871 pop_topmost_sequence ()
2872 {
2873   struct sequence_stack *stack, *top;
2874
2875   for (stack = sequence_stack; stack; stack = stack->next)
2876     top = stack;
2877
2878   top->first = first_insn;
2879   top->last = last_insn;
2880   /* ??? Why don't we save sequence_rtl_expr here?  */
2881
2882   end_sequence ();
2883 }
2884
2885 /* After emitting to a sequence, restore previous saved state.
2886
2887    To get the contents of the sequence just made,
2888    you must call `gen_sequence' *before* calling here.  */
2889
2890 void
2891 end_sequence ()
2892 {
2893   struct sequence_stack *tem = sequence_stack;
2894
2895   first_insn = tem->first;
2896   last_insn = tem->last;
2897   sequence_rtl_expr = tem->sequence_rtl_expr;
2898   sequence_stack = tem->next;
2899
2900   tem->next = sequence_element_free_list;
2901   sequence_element_free_list = tem;
2902 }
2903
2904 /* Return 1 if currently emitting into a sequence.  */
2905
2906 int
2907 in_sequence_p ()
2908 {
2909   return sequence_stack != 0;
2910 }
2911
2912 /* Generate a SEQUENCE rtx containing the insns already emitted
2913    to the current sequence.
2914
2915    This is how the gen_... function from a DEFINE_EXPAND
2916    constructs the SEQUENCE that it returns.  */
2917
2918 rtx
2919 gen_sequence ()
2920 {
2921   rtx result;
2922   rtx tem;
2923   int i;
2924   int len;
2925
2926   /* Count the insns in the chain.  */
2927   len = 0;
2928   for (tem = first_insn; tem; tem = NEXT_INSN (tem))
2929     len++;
2930
2931   /* If only one insn, return its pattern rather than a SEQUENCE.
2932      (Now that we cache SEQUENCE expressions, it isn't worth special-casing
2933      the case of an empty list.)  */
2934   if (len == 1
2935       && (GET_CODE (first_insn) == INSN
2936           || GET_CODE (first_insn) == JUMP_INSN
2937           || GET_CODE (first_insn) == CALL_INSN))
2938     return PATTERN (first_insn);
2939
2940   /* Put them in a vector.  See if we already have a SEQUENCE of the
2941      appropriate length around.  */
2942   if (len < SEQUENCE_RESULT_SIZE && (result = sequence_result[len]) != 0)
2943     sequence_result[len] = 0;
2944   else
2945     {
2946       /* Ensure that this rtl goes in saveable_obstack, since we may be
2947          caching it.  */
2948       push_obstacks_nochange ();
2949       rtl_in_saveable_obstack ();
2950       result = gen_rtx (SEQUENCE, VOIDmode, rtvec_alloc (len));
2951       pop_obstacks ();
2952     }
2953
2954   for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
2955     XVECEXP (result, 0, i) = tem;
2956
2957   return result;
2958 }
2959 \f
2960 /* Set up regno_reg_rtx, reg_rtx_no and regno_pointer_flag
2961    according to the chain of insns starting with FIRST.
2962
2963    Also set cur_insn_uid to exceed the largest uid in that chain.
2964
2965    This is used when an inline function's rtl is saved
2966    and passed to rest_of_compilation later.  */
2967
2968 static void restore_reg_data_1 ();
2969
2970 void
2971 restore_reg_data (first)
2972      rtx first;
2973 {
2974   register rtx insn;
2975   int i;
2976   register int max_uid = 0;
2977
2978   for (insn = first; insn; insn = NEXT_INSN (insn))
2979     {
2980       if (INSN_UID (insn) >= max_uid)
2981         max_uid = INSN_UID (insn);
2982
2983       switch (GET_CODE (insn))
2984         {
2985         case NOTE:
2986         case CODE_LABEL:
2987         case BARRIER:
2988           break;
2989
2990         case JUMP_INSN:
2991         case CALL_INSN:
2992         case INSN:
2993           restore_reg_data_1 (PATTERN (insn));
2994           break;
2995         }
2996     }
2997
2998   /* Don't duplicate the uids already in use.  */
2999   cur_insn_uid = max_uid + 1;
3000
3001   /* If any regs are missing, make them up.  
3002
3003      ??? word_mode is not necessarily the right mode.  Most likely these REGs
3004      are never used.  At some point this should be checked.  */
3005
3006   for (i = FIRST_PSEUDO_REGISTER; i < reg_rtx_no; i++)
3007     if (regno_reg_rtx[i] == 0)
3008       regno_reg_rtx[i] = gen_rtx (REG, word_mode, i);
3009 }
3010
3011 static void
3012 restore_reg_data_1 (orig)
3013      rtx orig;
3014 {
3015   register rtx x = orig;
3016   register int i;
3017   register enum rtx_code code;
3018   register char *format_ptr;
3019
3020   code = GET_CODE (x);
3021
3022   switch (code)
3023     {
3024     case QUEUED:
3025     case CONST_INT:
3026     case CONST_DOUBLE:
3027     case SYMBOL_REF:
3028     case CODE_LABEL:
3029     case PC:
3030     case CC0:
3031     case LABEL_REF:
3032       return;
3033
3034     case REG:
3035       if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
3036         {
3037           /* Make sure regno_pointer_flag and regno_reg_rtx are large
3038              enough to have an element for this pseudo reg number.  */
3039           if (REGNO (x) >= reg_rtx_no)
3040             {
3041               reg_rtx_no = REGNO (x);
3042
3043               if (reg_rtx_no >= regno_pointer_flag_length)
3044                 {
3045                   int newlen = MAX (regno_pointer_flag_length * 2,
3046                                     reg_rtx_no + 30);
3047                   rtx *new1;
3048                   char *new = (char *) oballoc (newlen);
3049                   bzero (new, newlen);
3050                   bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
3051
3052                   new1 = (rtx *) oballoc (newlen * sizeof (rtx));
3053                   bzero (new1, newlen * sizeof (rtx));
3054                   bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
3055
3056                   regno_pointer_flag = new;
3057                   regno_reg_rtx = new1;
3058                   regno_pointer_flag_length = newlen;
3059                 }
3060               reg_rtx_no ++;
3061             }
3062           regno_reg_rtx[REGNO (x)] = x;
3063         }
3064       return;
3065
3066     case MEM:
3067       if (GET_CODE (XEXP (x, 0)) == REG)
3068         mark_reg_pointer (XEXP (x, 0));
3069       restore_reg_data_1 (XEXP (x, 0));
3070       return;
3071     }
3072
3073   /* Now scan the subexpressions recursively.  */
3074
3075   format_ptr = GET_RTX_FORMAT (code);
3076
3077   for (i = 0; i < GET_RTX_LENGTH (code); i++)
3078     {
3079       switch (*format_ptr++)
3080         {
3081         case 'e':
3082           restore_reg_data_1 (XEXP (x, i));
3083           break;
3084
3085         case 'E':
3086           if (XVEC (x, i) != NULL)
3087             {
3088               register int j;
3089
3090               for (j = 0; j < XVECLEN (x, i); j++)
3091                 restore_reg_data_1 (XVECEXP (x, i, j));
3092             }
3093           break;
3094         }
3095     }
3096 }
3097 \f
3098 /* Initialize data structures and variables in this file
3099    before generating rtl for each function.  */
3100
3101 void
3102 init_emit ()
3103 {
3104   int i;
3105
3106   first_insn = NULL;
3107   last_insn = NULL;
3108   sequence_rtl_expr = NULL;
3109   cur_insn_uid = 1;
3110   reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
3111   last_linenum = 0;
3112   last_filename = 0;
3113   first_label_num = label_num;
3114   last_label_num = 0;
3115   sequence_stack = NULL;
3116
3117   /* Clear the start_sequence/gen_sequence cache.  */
3118   sequence_element_free_list = 0;
3119   for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
3120     sequence_result[i] = 0;
3121
3122   /* Init the tables that describe all the pseudo regs.  */
3123
3124   regno_pointer_flag_length = LAST_VIRTUAL_REGISTER + 101;
3125
3126   regno_pointer_flag 
3127     = (char *) oballoc (regno_pointer_flag_length);
3128   bzero (regno_pointer_flag, regno_pointer_flag_length);
3129
3130   regno_reg_rtx 
3131     = (rtx *) oballoc (regno_pointer_flag_length * sizeof (rtx));
3132   bzero (regno_reg_rtx, regno_pointer_flag_length * sizeof (rtx));
3133
3134   /* Put copies of all the virtual register rtx into regno_reg_rtx.  */
3135   regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
3136   regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
3137   regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
3138   regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
3139
3140   /* Indicate that the virtual registers and stack locations are
3141      all pointers.  */
3142   REGNO_POINTER_FLAG (STACK_POINTER_REGNUM) = 1;
3143   REGNO_POINTER_FLAG (FRAME_POINTER_REGNUM) = 1;
3144   REGNO_POINTER_FLAG (ARG_POINTER_REGNUM) = 1;
3145
3146   REGNO_POINTER_FLAG (VIRTUAL_INCOMING_ARGS_REGNUM) = 1;
3147   REGNO_POINTER_FLAG (VIRTUAL_STACK_VARS_REGNUM) = 1;
3148   REGNO_POINTER_FLAG (VIRTUAL_STACK_DYNAMIC_REGNUM) = 1;
3149   REGNO_POINTER_FLAG (VIRTUAL_OUTGOING_ARGS_REGNUM) = 1;
3150
3151 #ifdef INIT_EXPANDERS
3152   INIT_EXPANDERS;
3153 #endif
3154 }
3155
3156 /* Create some permanent unique rtl objects shared between all functions.
3157    LINE_NUMBERS is nonzero if line numbers are to be generated.  */
3158
3159 void
3160 init_emit_once (line_numbers)
3161      int line_numbers;
3162 {
3163   int i;
3164   enum machine_mode mode;
3165
3166   no_line_numbers = ! line_numbers;
3167
3168   sequence_stack = NULL;
3169
3170   /* Compute the word and byte modes.  */
3171
3172   byte_mode = VOIDmode;
3173   word_mode = VOIDmode;
3174
3175   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3176        mode = GET_MODE_WIDER_MODE (mode))
3177     {
3178       if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
3179           && byte_mode == VOIDmode)
3180         byte_mode = mode;
3181
3182       if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
3183           && word_mode == VOIDmode)
3184         word_mode = mode;
3185     }
3186
3187   /* Create the unique rtx's for certain rtx codes and operand values.  */
3188
3189   pc_rtx = gen_rtx (PC, VOIDmode);
3190   cc0_rtx = gen_rtx (CC0, VOIDmode);
3191
3192   /* Don't use gen_rtx here since gen_rtx in this case
3193      tries to use these variables.  */
3194   for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
3195     {
3196       const_int_rtx[i + MAX_SAVED_CONST_INT] = rtx_alloc (CONST_INT);
3197       PUT_MODE (const_int_rtx[i + MAX_SAVED_CONST_INT], VOIDmode);
3198       INTVAL (const_int_rtx[i + MAX_SAVED_CONST_INT]) = i;
3199     }
3200
3201   /* These four calls obtain some of the rtx expressions made above.  */
3202   const0_rtx = GEN_INT (0);
3203   const1_rtx = GEN_INT (1);
3204   const2_rtx = GEN_INT (2);
3205   constm1_rtx = GEN_INT (-1);
3206
3207   /* This will usually be one of the above constants, but may be a new rtx.  */
3208   const_true_rtx = GEN_INT (STORE_FLAG_VALUE);
3209
3210   dconst0 = REAL_VALUE_ATOF ("0", DFmode);
3211   dconst1 = REAL_VALUE_ATOF ("1", DFmode);
3212   dconst2 = REAL_VALUE_ATOF ("2", DFmode);
3213   dconstm1 = REAL_VALUE_ATOF ("-1", DFmode);
3214
3215   for (i = 0; i <= 2; i++)
3216     {
3217       for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
3218            mode = GET_MODE_WIDER_MODE (mode))
3219         {
3220           rtx tem = rtx_alloc (CONST_DOUBLE);
3221           union real_extract u;
3222
3223           bzero (&u, sizeof u);  /* Zero any holes in a structure.  */
3224           u.d = i == 0 ? dconst0 : i == 1 ? dconst1 : dconst2;
3225
3226           bcopy (&u, &CONST_DOUBLE_LOW (tem), sizeof u);
3227           CONST_DOUBLE_MEM (tem) = cc0_rtx;
3228           PUT_MODE (tem, mode);
3229
3230           const_tiny_rtx[i][(int) mode] = tem;
3231         }
3232
3233       const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
3234
3235       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3236            mode = GET_MODE_WIDER_MODE (mode))
3237         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
3238
3239       for (mode = GET_CLASS_NARROWEST_MODE (MODE_PARTIAL_INT);
3240            mode != VOIDmode;
3241            mode = GET_MODE_WIDER_MODE (mode))
3242         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
3243     }
3244
3245   for (mode = GET_CLASS_NARROWEST_MODE (MODE_CC); mode != VOIDmode;
3246        mode = GET_MODE_WIDER_MODE (mode))
3247     const_tiny_rtx[0][(int) mode] = const0_rtx;
3248
3249   stack_pointer_rtx = gen_rtx (REG, Pmode, STACK_POINTER_REGNUM);
3250   frame_pointer_rtx = gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM);
3251
3252   if (HARD_FRAME_POINTER_REGNUM == FRAME_POINTER_REGNUM)
3253     hard_frame_pointer_rtx = frame_pointer_rtx;
3254   else
3255     hard_frame_pointer_rtx = gen_rtx (REG, Pmode, HARD_FRAME_POINTER_REGNUM);
3256   
3257   if (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM)
3258     arg_pointer_rtx = frame_pointer_rtx;
3259   else if (HARD_FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM)
3260     arg_pointer_rtx = hard_frame_pointer_rtx;
3261   else if (STACK_POINTER_REGNUM == ARG_POINTER_REGNUM)
3262     arg_pointer_rtx = stack_pointer_rtx;
3263   else
3264     arg_pointer_rtx = gen_rtx (REG, Pmode, ARG_POINTER_REGNUM);
3265
3266   /* Create the virtual registers.  Do so here since the following objects
3267      might reference them.  */
3268
3269   virtual_incoming_args_rtx = gen_rtx (REG, Pmode,
3270                                        VIRTUAL_INCOMING_ARGS_REGNUM);
3271   virtual_stack_vars_rtx = gen_rtx (REG, Pmode,
3272                                     VIRTUAL_STACK_VARS_REGNUM);
3273   virtual_stack_dynamic_rtx = gen_rtx (REG, Pmode,
3274                                        VIRTUAL_STACK_DYNAMIC_REGNUM);
3275   virtual_outgoing_args_rtx = gen_rtx (REG, Pmode,
3276                                        VIRTUAL_OUTGOING_ARGS_REGNUM);
3277
3278 #ifdef STRUCT_VALUE
3279   struct_value_rtx = STRUCT_VALUE;
3280 #else
3281   struct_value_rtx = gen_rtx (REG, Pmode, STRUCT_VALUE_REGNUM);
3282 #endif
3283
3284 #ifdef STRUCT_VALUE_INCOMING
3285   struct_value_incoming_rtx = STRUCT_VALUE_INCOMING;
3286 #else
3287 #ifdef STRUCT_VALUE_INCOMING_REGNUM
3288   struct_value_incoming_rtx
3289     = gen_rtx (REG, Pmode, STRUCT_VALUE_INCOMING_REGNUM);
3290 #else
3291   struct_value_incoming_rtx = struct_value_rtx;
3292 #endif
3293 #endif
3294
3295 #ifdef STATIC_CHAIN_REGNUM
3296   static_chain_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_REGNUM);
3297
3298 #ifdef STATIC_CHAIN_INCOMING_REGNUM
3299   if (STATIC_CHAIN_INCOMING_REGNUM != STATIC_CHAIN_REGNUM)
3300     static_chain_incoming_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_INCOMING_REGNUM);
3301   else
3302 #endif
3303     static_chain_incoming_rtx = static_chain_rtx;
3304 #endif
3305
3306 #ifdef STATIC_CHAIN
3307   static_chain_rtx = STATIC_CHAIN;
3308
3309 #ifdef STATIC_CHAIN_INCOMING
3310   static_chain_incoming_rtx = STATIC_CHAIN_INCOMING;
3311 #else
3312   static_chain_incoming_rtx = static_chain_rtx;
3313 #endif
3314 #endif
3315
3316 #ifdef PIC_OFFSET_TABLE_REGNUM
3317   pic_offset_table_rtx = gen_rtx (REG, Pmode, PIC_OFFSET_TABLE_REGNUM);
3318 #endif
3319 }