OSDN Git Service

Do not test overflow bit for decrement_and_branch
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines for insn-output.c for Intel X86.
2    Copyright (C) 1988, 1992, 1994, 1995 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include <setjmp.h>
23 #include <ctype.h>
24 #include "config.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "function.h"
37
38 #ifdef EXTRA_CONSTRAINT
39 /* If EXTRA_CONSTRAINT is defined, then the 'S'
40    constraint in REG_CLASS_FROM_LETTER will no longer work, and various
41    asm statements that need 'S' for class SIREG will break.  */
42  error EXTRA_CONSTRAINT conflicts with S constraint letter
43 /* The previous line used to be #error, but some compilers barf
44    even if the conditional was untrue.  */
45 #endif
46
47 #define AT_BP(mode) (gen_rtx (MEM, (mode), frame_pointer_rtx))
48
49 extern FILE *asm_out_file;
50 extern char *strcat ();
51
52 char *singlemove_string ();
53 char *output_move_const_single ();
54 char *output_fp_cc0_set ();
55
56 char *hi_reg_name[] = HI_REGISTER_NAMES;
57 char *qi_reg_name[] = QI_REGISTER_NAMES;
58 char *qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
59
60 /* Array of the smallest class containing reg number REGNO, indexed by
61    REGNO.  Used by REGNO_REG_CLASS in i386.h. */
62
63 enum reg_class regclass_map[FIRST_PSEUDO_REGISTER] =
64 {
65   /* ax, dx, cx, bx */
66   AREG, DREG, CREG, BREG,
67   /* si, di, bp, sp */
68   SIREG, DIREG, INDEX_REGS, GENERAL_REGS,
69   /* FP registers */
70   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
71   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,       
72   /* arg pointer */
73   INDEX_REGS
74 };
75
76 /* Test and compare insns in i386.md store the information needed to
77    generate branch and scc insns here.  */
78
79 struct rtx_def *i386_compare_op0 = NULL_RTX;
80 struct rtx_def *i386_compare_op1 = NULL_RTX;
81 struct rtx_def *(*i386_compare_gen)(), *(*i386_compare_gen_eq)();
82
83 /* Register allocation order */
84 char *i386_reg_alloc_order;
85 static char regs_allocated[FIRST_PSEUDO_REGISTER];
86
87 /* # of registers to use to pass arguments. */
88 char *i386_regparm_string;                      /* # registers to use to pass args */
89 int i386_regparm;                               /* i386_regparm_string as a number */
90
91 /* Alignment to use for loops and jumps */
92 char *i386_align_loops_string;                  /* power of two alignment for loops */
93 char *i386_align_jumps_string;                  /* power of two alignment for non-loop jumps */
94 char *i386_align_funcs_string;                  /* power of two alignment for functions */
95
96 int i386_align_loops;                           /* power of two alignment for loops */
97 int i386_align_jumps;                           /* power of two alignment for non-loop jumps */
98 int i386_align_funcs;                           /* power of two alignment for functions */
99
100 \f
101 /* Sometimes certain combinations of command options do not make
102    sense on a particular target machine.  You can define a macro
103    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
104    defined, is executed once just after all the command options have
105    been parsed.
106
107    Don't use this macro to turn on various extra optimizations for
108    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
109
110 void
111 override_options ()
112 {
113   int ch, i, regno;
114   char *p;
115   int def_align;
116
117 #ifdef SUBTARGET_OVERRIDE_OPTIONS
118   SUBTARGET_OVERRIDE_OPTIONS;
119 #endif
120
121   /* Validate registers in register allocation order */
122   if (i386_reg_alloc_order)
123     {
124       for (i = 0; (ch = i386_reg_alloc_order[i]) != '\0'; i++)
125         {
126           switch (ch)
127             {
128             case 'a':   regno = 0;      break;
129             case 'd':   regno = 1;      break;
130             case 'c':   regno = 2;      break;
131             case 'b':   regno = 3;      break;
132             case 'S':   regno = 4;      break;
133             case 'D':   regno = 5;      break;
134             case 'B':   regno = 6;      break;
135
136             default:    fatal ("Register '%c' is unknown", ch);
137             }
138
139           if (regs_allocated[regno])
140             fatal ("Register '%c' was already specified in the allocation order", ch);
141
142           regs_allocated[regno] = 1;
143         }
144     }
145
146   /* Validate -mregparm= value */
147   if (i386_regparm_string)
148     {
149       i386_regparm = atoi (i386_regparm_string);
150       if (i386_regparm < 0 || i386_regparm > REGPARM_MAX)
151         fatal ("-mregparm=%d is not between 0 and %d", i386_regparm, REGPARM_MAX);
152     }
153
154   def_align = (TARGET_386) ? 2 : 4;
155
156   /* Validate -malign-loops= value, or provide default */
157   if (i386_align_loops_string)
158     {
159       i386_align_loops = atoi (i386_align_loops_string);
160       if (i386_align_loops < 0 || i386_align_loops > MAX_CODE_ALIGN)
161         fatal ("-malign-loops=%d is not between 0 and %d",
162                i386_align_loops, MAX_CODE_ALIGN);
163     }
164   else
165     i386_align_loops = 2;
166
167   /* Validate -malign-jumps= value, or provide default */
168   if (i386_align_jumps_string)
169     {
170       i386_align_jumps = atoi (i386_align_jumps_string);
171       if (i386_align_jumps < 0 || i386_align_jumps > MAX_CODE_ALIGN)
172         fatal ("-malign-jumps=%d is not between 0 and %d",
173                i386_align_jumps, MAX_CODE_ALIGN);
174     }
175   else
176     i386_align_jumps = def_align;
177
178   /* Validate -malign-functions= value, or provide default */
179   if (i386_align_funcs_string)
180     {
181       i386_align_funcs = atoi (i386_align_funcs_string);
182       if (i386_align_funcs < 0 || i386_align_funcs > MAX_CODE_ALIGN)
183         fatal ("-malign-functions=%d is not between 0 and %d",
184                i386_align_funcs, MAX_CODE_ALIGN);
185     }
186   else
187     i386_align_funcs = def_align;
188 }
189 \f
190 /* A C statement (sans semicolon) to choose the order in which to
191    allocate hard registers for pseudo-registers local to a basic
192    block.
193
194    Store the desired register order in the array `reg_alloc_order'.
195    Element 0 should be the register to allocate first; element 1, the
196    next register; and so on.
197
198    The macro body should not assume anything about the contents of
199    `reg_alloc_order' before execution of the macro.
200
201    On most machines, it is not necessary to define this macro.  */
202
203 void
204 order_regs_for_local_alloc ()
205 {
206   int i, ch, order, regno;
207
208   /* User specified the register allocation order */
209   if (i386_reg_alloc_order)
210     {
211       for (i = order = 0; (ch = i386_reg_alloc_order[i]) != '\0'; i++)
212         {
213           switch (ch)
214             {
215             case 'a':   regno = 0;      break;
216             case 'd':   regno = 1;      break;
217             case 'c':   regno = 2;      break;
218             case 'b':   regno = 3;      break;
219             case 'S':   regno = 4;      break;
220             case 'D':   regno = 5;      break;
221             case 'B':   regno = 6;      break;
222             }
223
224           reg_alloc_order[order++] = regno;
225         }
226
227       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
228         {
229           if (!regs_allocated[i])
230             reg_alloc_order[order++] = i;
231         }
232     }
233
234   /* If users did not specify a register allocation order, favor eax
235      normally except if DImode variables are used, in which case
236      favor edx before eax, which seems to cause less spill register
237      not found messages.  */
238   else
239     {
240       rtx insn;
241
242       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
243         reg_alloc_order[i] = i;
244
245       if (optimize)
246         {
247           int use_dca = FALSE;
248
249           for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
250             {
251               if (GET_CODE (insn) == INSN)
252                 {
253                   rtx set = NULL_RTX;
254                   rtx pattern = PATTERN (insn);
255
256                   if (GET_CODE (pattern) == SET)
257                     set = pattern;
258
259                   else if ((GET_CODE (pattern) == PARALLEL
260                             || GET_CODE (pattern) == SEQUENCE)
261                            && GET_CODE (XVECEXP (pattern, 0, 0)) == SET)
262                     set = XVECEXP (pattern, 0, 0);
263
264                   if (set && GET_MODE (SET_SRC (set)) == DImode)
265                     {
266                       use_dca = TRUE;
267                       break;
268                     }
269                 }
270             }
271
272           if (use_dca)
273             {
274               reg_alloc_order[0] = 1;   /* edx */
275               reg_alloc_order[1] = 2;   /* ecx */
276               reg_alloc_order[2] = 0;   /* eax */
277             }
278         }
279     }
280 }
281
282 \f
283 /* Return nonzero if IDENTIFIER with arguments ARGS is a valid machine specific
284    attribute for DECL.  The attributes in ATTRIBUTES have previously been
285    assigned to DECL.  */
286
287 int
288 i386_valid_decl_attribute_p (decl, attributes, identifier, args)
289      tree decl;
290      tree attributes;
291      tree identifier;
292      tree args;
293 {
294   return 0;
295 }
296
297 /* Return nonzero if IDENTIFIER with arguments ARGS is a valid machine specific
298    attribute for TYPE.  The attributes in ATTRIBUTES have previously been
299    assigned to TYPE.  */
300
301 int
302 i386_valid_type_attribute_p (type, attributes, identifier, args)
303      tree type;
304      tree attributes;
305      tree identifier;
306      tree args;
307 {
308   if (TREE_CODE (type) != FUNCTION_TYPE
309       && TREE_CODE (type) != FIELD_DECL
310       && TREE_CODE (type) != TYPE_DECL)
311     return 0;
312
313   /* Stdcall attribute says callee is responsible for popping arguments
314      if they are not variable.  */
315   if (is_attribute_p ("stdcall", identifier))
316     return (args == NULL_TREE);
317
318   /* Cdecl attribute says the callee is a normal C declaration */
319   if (is_attribute_p ("cdecl", identifier))
320     return (args == NULL_TREE);
321
322   /* Regparm attribute specifies how many integer arguments are to be
323      passed in registers */
324   if (is_attribute_p ("regparm", identifier))
325     {
326       tree cst;
327
328       if (!args || TREE_CODE (args) != TREE_LIST
329           || TREE_CHAIN (args) != NULL_TREE
330           || TREE_VALUE (args) == NULL_TREE)
331         return 0;
332
333       cst = TREE_VALUE (args);
334       if (TREE_CODE (cst) != INTEGER_CST)
335         return 0;
336
337       if (TREE_INT_CST_HIGH (cst) != 0
338           || TREE_INT_CST_LOW (cst) < 0
339           || TREE_INT_CST_LOW (cst) > REGPARM_MAX)
340         return 0;
341
342       return 1;
343     }
344
345   return 0;
346 }
347
348 /* Return 0 if the attributes for two types are incompatible, 1 if they
349    are compatible, and 2 if they are nearly compatible (which causes a
350    warning to be generated).  */
351
352 int
353 i386_comp_type_attributes (type1, type2)
354      tree type1;
355      tree type2;
356 {
357   return 1;
358 }
359
360 \f
361 /* Value is the number of bytes of arguments automatically
362    popped when returning from a subroutine call.
363    FUNDECL is the declaration node of the function (as a tree),
364    FUNTYPE is the data type of the function (as a tree),
365    or for a library call it is an identifier node for the subroutine name.
366    SIZE is the number of bytes of arguments passed on the stack.
367
368    On the 80386, the RTD insn may be used to pop them if the number
369      of args is fixed, but if the number is variable then the caller
370      must pop them all.  RTD can't be used for library calls now
371      because the library is compiled with the Unix compiler.
372    Use of RTD is a selectable option, since it is incompatible with
373    standard Unix calling sequences.  If the option is not selected,
374    the caller must always pop the args.
375
376    The attribute stdcall is equivalent to RTD on a per module basis.  */
377
378 int
379 i386_return_pops_args (fundecl, funtype, size)
380      tree fundecl;
381      tree funtype;
382      int size;
383 {
384   int rtd = TARGET_RTD;
385
386   if (TREE_CODE (funtype) == IDENTIFIER_NODE)
387     return 0;
388
389   if (fundecl && TREE_CODE_CLASS (TREE_CODE (fundecl)) == 'd')
390     {
391       /* Cdecl functions override -mrtd, and never pop the stack */
392       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
393         return 0;
394
395       /* Stdcall functions will pop the stack if not variable args */
396       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype)))
397         rtd = 1;
398     }
399
400   if (rtd)
401     {
402       if (TYPE_ARG_TYPES (funtype) == NULL_TREE
403           || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype))) == void_type_node))
404         return size;
405
406       if (aggregate_value_p (TREE_TYPE (funtype)))
407         return GET_MODE_SIZE (Pmode);
408     }
409
410   return 0;
411 }
412
413 \f
414 /* Argument support functions.  */
415
416 /* Initialize a variable CUM of type CUMULATIVE_ARGS
417    for a call to a function whose data type is FNTYPE.
418    For a library call, FNTYPE is 0.  */
419
420 void
421 init_cumulative_args (cum, fntype, libname)
422      CUMULATIVE_ARGS *cum;      /* argument info to initialize */
423      tree fntype;               /* tree ptr for function decl */
424      rtx libname;               /* SYMBOL_REF of library name or 0 */
425 {
426   static CUMULATIVE_ARGS zero_cum;
427   tree param, next_param;
428
429   if (TARGET_DEBUG_ARG)
430     {
431       fprintf (stderr, "\ninit_cumulative_args (");
432       if (fntype)
433         {
434           tree ret_type = TREE_TYPE (fntype);
435           fprintf (stderr, "fntype code = %s, ret code = %s",
436                    tree_code_name[ (int)TREE_CODE (fntype) ],
437                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
438         }
439       else
440         fprintf (stderr, "no fntype");
441
442       if (libname)
443         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
444     }
445
446   *cum = zero_cum;
447
448   /* Set up the number of registers to use for passing arguments.  */
449   cum->nregs = i386_regparm;
450   if (fntype)
451     {
452       tree attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (fntype));
453       if (attr)
454         cum->nregs = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
455     }
456
457   /* Determine if this function has variable arguments.  This is
458      indicated by the last argument being 'void_type_mode' if there
459      are no variable arguments.  If there are variable arguments, then
460      we won't pass anything in registers */
461
462   if (cum->nregs)
463     {
464       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
465            param != (tree)0;
466            param = next_param)
467         {
468           next_param = TREE_CHAIN (param);
469           if (next_param == (tree)0 && TREE_VALUE (param) != void_type_node)
470             cum->nregs = 0;
471         }
472     }
473
474   if (TARGET_DEBUG_ARG)
475     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
476
477   return;
478 }
479
480 /* Update the data in CUM to advance over an argument
481    of mode MODE and data type TYPE.
482    (TYPE is null for libcalls where that information may not be available.)  */
483
484 void
485 function_arg_advance (cum, mode, type, named)
486      CUMULATIVE_ARGS *cum;      /* current arg information */
487      enum machine_mode mode;    /* current arg mode */
488      tree type;                 /* type of the argument or 0 if lib support */
489      int named;                 /* whether or not the argument was named */
490 {
491   int bytes = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
492   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
493
494   if (TARGET_DEBUG_ARG)
495     fprintf (stderr,
496              "function_adv( size=%d, words=%2d, nregs=%d, mode=%4s, named=%d )\n\n",
497              words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
498
499   cum->words += words;
500   cum->nregs -= words;
501   cum->regno += words;
502
503   if (cum->nregs <= 0)
504     {
505       cum->nregs = 0;
506       cum->regno = 0;
507     }
508
509   return;
510 }
511
512 /* Define where to put the arguments to a function.
513    Value is zero to push the argument on the stack,
514    or a hard register in which to store the argument.
515
516    MODE is the argument's machine mode.
517    TYPE is the data type of the argument (as a tree).
518     This is null for libcalls where that information may
519     not be available.
520    CUM is a variable of type CUMULATIVE_ARGS which gives info about
521     the preceding args and about the function being called.
522    NAMED is nonzero if this argument is a named parameter
523     (otherwise it is an extra parameter matching an ellipsis).  */
524
525 struct rtx_def *
526 function_arg (cum, mode, type, named)
527      CUMULATIVE_ARGS *cum;      /* current arg information */
528      enum machine_mode mode;    /* current arg mode */
529      tree type;                 /* type of the argument or 0 if lib support */
530      int named;                 /* != 0 for normal args, == 0 for ... args */
531 {
532   rtx ret   = NULL_RTX;
533   int bytes = (mode == BLKmode) ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
534   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
535
536   switch (mode)
537     {
538     default:                    /* for now, pass fp/complex values on the stack */
539       break;
540
541     case BLKmode:
542     case DImode:
543     case SImode:
544     case HImode:
545     case QImode:
546       if (words <= cum->nregs)
547         ret = gen_rtx (REG, mode, cum->regno);
548       break;
549     }
550
551   if (TARGET_DEBUG_ARG)
552     {
553       fprintf (stderr,
554                "function_arg( size=%d, words=%2d, nregs=%d, mode=%4s, named=%d",
555                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
556
557       if (ret)
558         fprintf (stderr, ", reg=%%e%s", reg_names[ REGNO(ret) ]);
559       else
560         fprintf (stderr, ", stack");
561
562       fprintf (stderr, " )\n");
563     }
564
565   return ret;
566 }
567
568 /* For an arg passed partly in registers and partly in memory,
569    this is the number of registers used.
570    For args passed entirely in registers or entirely in memory, zero.  */
571
572 int
573 function_arg_partial_nregs (cum, mode, type, named)
574      CUMULATIVE_ARGS *cum;      /* current arg information */
575      enum machine_mode mode;    /* current arg mode */
576      tree type;                 /* type of the argument or 0 if lib support */
577      int named;                 /* != 0 for normal args, == 0 for ... args */
578 {
579   return 0;
580 }
581
582 \f
583 /* Output an insn whose source is a 386 integer register.  SRC is the
584    rtx for the register, and TEMPLATE is the op-code template.  SRC may
585    be either SImode or DImode.
586
587    The template will be output with operands[0] as SRC, and operands[1]
588    as a pointer to the top of the 386 stack.  So a call from floatsidf2
589    would look like this:
590
591       output_op_from_reg (operands[1], AS1 (fild%z0,%1));
592
593    where %z0 corresponds to the caller's operands[1], and is used to
594    emit the proper size suffix.
595
596    ??? Extend this to handle HImode - a 387 can load and store HImode
597    values directly. */
598
599 void
600 output_op_from_reg (src, template)
601      rtx src;
602      char *template;
603 {
604   rtx xops[4];
605   int size = GET_MODE_SIZE (GET_MODE (src));
606
607   xops[0] = src;
608   xops[1] = AT_SP (Pmode);
609   xops[2] = GEN_INT (size);
610   xops[3] = stack_pointer_rtx;
611
612   if (size > UNITS_PER_WORD)
613     {
614       rtx high;
615       if (size > 2 * UNITS_PER_WORD)
616         {
617           high = gen_rtx (REG, SImode, REGNO (src) + 2);
618           output_asm_insn (AS1 (push%L0,%0), &high);
619         }
620       high = gen_rtx (REG, SImode, REGNO (src) + 1);
621       output_asm_insn (AS1 (push%L0,%0), &high);
622     }
623   output_asm_insn (AS1 (push%L0,%0), &src);
624
625   output_asm_insn (template, xops);
626
627   output_asm_insn (AS2 (add%L3,%2,%3), xops);
628 }
629 \f
630 /* Output an insn to pop an value from the 387 top-of-stack to 386
631    register DEST. The 387 register stack is popped if DIES is true.  If
632    the mode of DEST is an integer mode, a `fist' integer store is done,
633    otherwise a `fst' float store is done. */
634
635 void
636 output_to_reg (dest, dies)
637      rtx dest;
638      int dies;
639 {
640   rtx xops[4];
641   int size = GET_MODE_SIZE (GET_MODE (dest));
642
643   xops[0] = AT_SP (Pmode);
644   xops[1] = stack_pointer_rtx;
645   xops[2] = GEN_INT (size);
646   xops[3] = dest;
647
648   output_asm_insn (AS2 (sub%L1,%2,%1), xops);
649
650   if (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
651     {
652       if (dies)
653         output_asm_insn (AS1 (fistp%z3,%y0), xops);
654       else
655         output_asm_insn (AS1 (fist%z3,%y0), xops);
656     }
657   else if (GET_MODE_CLASS (GET_MODE (dest)) == MODE_FLOAT)
658     {
659       if (dies)
660         output_asm_insn (AS1 (fstp%z3,%y0), xops);
661       else
662         {
663           if (GET_MODE (dest) == XFmode)
664             {
665               output_asm_insn (AS1 (fstp%z3,%y0), xops);
666               output_asm_insn (AS1 (fld%z3,%y0), xops);
667             }
668           else
669             output_asm_insn (AS1 (fst%z3,%y0), xops);
670         }
671     }
672   else
673     abort ();
674
675   output_asm_insn (AS1 (pop%L0,%0), &dest);
676
677   if (size > UNITS_PER_WORD)
678     {
679       dest = gen_rtx (REG, SImode, REGNO (dest) + 1);
680       output_asm_insn (AS1 (pop%L0,%0), &dest);
681       if (size > 2 * UNITS_PER_WORD)
682         {
683           dest = gen_rtx (REG, SImode, REGNO (dest) + 1);
684           output_asm_insn (AS1 (pop%L0,%0), &dest);
685         }
686     }
687 }
688 \f
689 char *
690 singlemove_string (operands)
691      rtx *operands;
692 {
693   rtx x;
694   if (GET_CODE (operands[0]) == MEM
695       && GET_CODE (x = XEXP (operands[0], 0)) == PRE_DEC)
696     {
697       if (XEXP (x, 0) != stack_pointer_rtx)
698         abort ();
699       return "push%L1 %1";
700     }
701   else if (GET_CODE (operands[1]) == CONST_DOUBLE)
702     {
703       return output_move_const_single (operands);
704     }
705   else if (GET_CODE (operands[0]) == REG || GET_CODE (operands[1]) == REG)
706     return AS2 (mov%L0,%1,%0);
707   else if (CONSTANT_P (operands[1]))
708     return AS2 (mov%L0,%1,%0);
709   else
710     {
711       output_asm_insn ("push%L1 %1", operands);
712       return "pop%L0 %0";
713     }
714 }
715 \f
716 /* Return a REG that occurs in ADDR with coefficient 1.
717    ADDR can be effectively incremented by incrementing REG.  */
718
719 static rtx
720 find_addr_reg (addr)
721      rtx addr;
722 {
723   while (GET_CODE (addr) == PLUS)
724     {
725       if (GET_CODE (XEXP (addr, 0)) == REG)
726         addr = XEXP (addr, 0);
727       else if (GET_CODE (XEXP (addr, 1)) == REG)
728         addr = XEXP (addr, 1);
729       else if (CONSTANT_P (XEXP (addr, 0)))
730         addr = XEXP (addr, 1);
731       else if (CONSTANT_P (XEXP (addr, 1)))
732         addr = XEXP (addr, 0);
733       else
734         abort ();
735     }
736   if (GET_CODE (addr) == REG)
737     return addr;
738   abort ();
739 }
740
741 \f
742 /* Output an insn to add the constant N to the register X.  */
743
744 static void
745 asm_add (n, x)
746      int n;
747      rtx x;
748 {
749   rtx xops[2];
750   xops[0] = x;
751
752   if (n == -1)
753     output_asm_insn (AS1 (dec%L0,%0), xops);
754   else if (n == 1)
755     output_asm_insn (AS1 (inc%L0,%0), xops);
756   else if (n < 0)
757     {
758       xops[1] = GEN_INT (-n);
759       output_asm_insn (AS2 (sub%L0,%1,%0), xops);
760     }
761   else if (n > 0)
762     {
763       xops[1] = GEN_INT (n);
764       output_asm_insn (AS2 (add%L0,%1,%0), xops);
765     }
766 }
767
768 \f
769 /* Output assembler code to perform a doubleword move insn
770    with operands OPERANDS.  */
771
772 char *
773 output_move_double (operands)
774      rtx *operands;
775 {
776   enum {REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP } optype0, optype1;
777   rtx latehalf[2];
778   rtx middlehalf[2];
779   rtx xops[2];
780   rtx addreg0 = 0, addreg1 = 0;
781   int dest_overlapped_low = 0;
782   int size = GET_MODE_SIZE (GET_MODE (operands[0]));
783
784   middlehalf[0] = 0;
785   middlehalf[1] = 0;
786
787   /* First classify both operands.  */
788
789   if (REG_P (operands[0]))
790     optype0 = REGOP;
791   else if (offsettable_memref_p (operands[0]))
792     optype0 = OFFSOP;
793   else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
794     optype0 = POPOP;
795   else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
796     optype0 = PUSHOP;
797   else if (GET_CODE (operands[0]) == MEM)
798     optype0 = MEMOP;
799   else
800     optype0 = RNDOP;
801
802   if (REG_P (operands[1]))
803     optype1 = REGOP;
804   else if (CONSTANT_P (operands[1]))
805     optype1 = CNSTOP;
806   else if (offsettable_memref_p (operands[1]))
807     optype1 = OFFSOP;
808   else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
809     optype1 = POPOP;
810   else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
811     optype1 = PUSHOP;
812   else if (GET_CODE (operands[1]) == MEM)
813     optype1 = MEMOP;
814   else
815     optype1 = RNDOP;
816
817   /* Check for the cases that the operand constraints are not
818      supposed to allow to happen.  Abort if we get one,
819      because generating code for these cases is painful.  */
820
821   if (optype0 == RNDOP || optype1 == RNDOP)
822     abort ();
823
824   /* If one operand is decrementing and one is incrementing
825      decrement the former register explicitly
826      and change that operand into ordinary indexing.  */
827
828   if (optype0 == PUSHOP && optype1 == POPOP)
829     {
830       /* ??? Can this ever happen on i386? */
831       operands[0] = XEXP (XEXP (operands[0], 0), 0);
832       asm_add (-size, operands[0]);
833       if (GET_MODE (operands[1]) == XFmode)
834         operands[0] = gen_rtx (MEM, XFmode, operands[0]);
835       else if (GET_MODE (operands[0]) == DFmode)
836         operands[0] = gen_rtx (MEM, DFmode, operands[0]);
837       else
838         operands[0] = gen_rtx (MEM, DImode, operands[0]);
839       optype0 = OFFSOP;
840     }
841
842   if (optype0 == POPOP && optype1 == PUSHOP)
843     {
844       /* ??? Can this ever happen on i386? */
845       operands[1] = XEXP (XEXP (operands[1], 0), 0);
846       asm_add (-size, operands[1]);
847       if (GET_MODE (operands[1]) == XFmode)
848         operands[1] = gen_rtx (MEM, XFmode, operands[1]);
849       else if (GET_MODE (operands[1]) == DFmode)
850         operands[1] = gen_rtx (MEM, DFmode, operands[1]);
851       else
852         operands[1] = gen_rtx (MEM, DImode, operands[1]);
853       optype1 = OFFSOP;
854     }
855
856   /* If an operand is an unoffsettable memory ref, find a register
857      we can increment temporarily to make it refer to the second word.  */
858
859   if (optype0 == MEMOP)
860     addreg0 = find_addr_reg (XEXP (operands[0], 0));
861
862   if (optype1 == MEMOP)
863     addreg1 = find_addr_reg (XEXP (operands[1], 0));
864
865   /* Ok, we can do one word at a time.
866      Normally we do the low-numbered word first,
867      but if either operand is autodecrementing then we
868      do the high-numbered word first.
869
870      In either case, set up in LATEHALF the operands to use
871      for the high-numbered word and in some cases alter the
872      operands in OPERANDS to be suitable for the low-numbered word.  */
873
874   if (size == 12)
875     {
876       if (optype0 == REGOP)
877         {
878           middlehalf[0] = gen_rtx (REG, SImode, REGNO (operands[0]) + 1);
879           latehalf[0] = gen_rtx (REG, SImode, REGNO (operands[0]) + 2);
880         }
881       else if (optype0 == OFFSOP)
882         {
883           middlehalf[0] = adj_offsettable_operand (operands[0], 4);
884           latehalf[0] = adj_offsettable_operand (operands[0], 8);
885         }
886       else
887         {
888          middlehalf[0] = operands[0];
889          latehalf[0] = operands[0];
890         }
891     
892       if (optype1 == REGOP)
893         {
894           middlehalf[1] = gen_rtx (REG, SImode, REGNO (operands[1]) + 1);
895           latehalf[1] = gen_rtx (REG, SImode, REGNO (operands[1]) + 2);
896         }
897       else if (optype1 == OFFSOP)
898         {
899           middlehalf[1] = adj_offsettable_operand (operands[1], 4);
900           latehalf[1] = adj_offsettable_operand (operands[1], 8);
901         }
902       else if (optype1 == CNSTOP)
903         {
904           if (GET_CODE (operands[1]) == CONST_DOUBLE)
905             {
906               REAL_VALUE_TYPE r; long l[3];
907
908               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
909               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
910               operands[1] = GEN_INT (l[0]);
911               middlehalf[1] = GEN_INT (l[1]);
912               latehalf[1] = GEN_INT (l[2]);
913             }
914           else if (CONSTANT_P (operands[1]))
915             /* No non-CONST_DOUBLE constant should ever appear here.  */
916             abort ();
917         }
918       else
919         {
920           middlehalf[1] = operands[1];
921           latehalf[1] = operands[1];
922         }
923     }
924   else /* size is not 12: */
925     {
926       if (optype0 == REGOP)
927         latehalf[0] = gen_rtx (REG, SImode, REGNO (operands[0]) + 1);
928       else if (optype0 == OFFSOP)
929         latehalf[0] = adj_offsettable_operand (operands[0], 4);
930       else
931         latehalf[0] = operands[0];
932
933       if (optype1 == REGOP)
934         latehalf[1] = gen_rtx (REG, SImode, REGNO (operands[1]) + 1);
935       else if (optype1 == OFFSOP)
936         latehalf[1] = adj_offsettable_operand (operands[1], 4);
937       else if (optype1 == CNSTOP)
938         split_double (operands[1], &operands[1], &latehalf[1]);
939       else
940         latehalf[1] = operands[1];
941     }
942
943   /* If insn is effectively movd N (sp),-(sp) then we will do the
944      high word first.  We should use the adjusted operand 1
945      (which is N+4 (sp) or N+8 (sp))
946      for the low word and middle word as well,
947      to compensate for the first decrement of sp.  */
948   if (optype0 == PUSHOP
949       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
950       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
951     middlehalf[1] = operands[1] = latehalf[1];
952
953   /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
954      if the upper part of reg N does not appear in the MEM, arrange to
955      emit the move late-half first.  Otherwise, compute the MEM address
956      into the upper part of N and use that as a pointer to the memory
957      operand.  */
958   if (optype0 == REGOP
959       && (optype1 == OFFSOP || optype1 == MEMOP))
960     {
961       if (reg_mentioned_p (operands[0], XEXP (operands[1], 0))
962           && reg_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
963         {
964           /* If both halves of dest are used in the src memory address,
965              compute the address into latehalf of dest.  */
966 compadr:
967           xops[0] = latehalf[0];
968           xops[1] = XEXP (operands[1], 0);
969           output_asm_insn (AS2 (lea%L0,%a1,%0), xops);
970           if( GET_MODE (operands[1]) == XFmode )
971             {
972 /*          abort (); */
973               operands[1] = gen_rtx (MEM, XFmode, latehalf[0]);
974               middlehalf[1] = adj_offsettable_operand (operands[1], size-8);
975               latehalf[1] = adj_offsettable_operand (operands[1], size-4);
976             }
977           else
978             {
979               operands[1] = gen_rtx (MEM, DImode, latehalf[0]);
980               latehalf[1] = adj_offsettable_operand (operands[1], size-4);
981             }
982         }
983       else if (size == 12
984                  && reg_mentioned_p (middlehalf[0], XEXP (operands[1], 0)))
985         {
986           /* Check for two regs used by both source and dest. */
987           if (reg_mentioned_p (operands[0], XEXP (operands[1], 0))
988                 || reg_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
989                 goto compadr;
990
991           /* JRV says this can't happen: */
992           if (addreg0 || addreg1)
993               abort();
994
995           /* Only the middle reg conflicts; simply put it last. */
996           output_asm_insn (singlemove_string (operands), operands);
997           output_asm_insn (singlemove_string (latehalf), latehalf);
998           output_asm_insn (singlemove_string (middlehalf), middlehalf);
999           return "";
1000         }
1001       else if (reg_mentioned_p (operands[0], XEXP (operands[1], 0)))
1002         /* If the low half of dest is mentioned in the source memory
1003            address, the arrange to emit the move late half first.  */
1004         dest_overlapped_low = 1;
1005     }
1006
1007   /* If one or both operands autodecrementing,
1008      do the two words, high-numbered first.  */
1009
1010   /* Likewise,  the first move would clobber the source of the second one,
1011      do them in the other order.  This happens only for registers;
1012      such overlap can't happen in memory unless the user explicitly
1013      sets it up, and that is an undefined circumstance.  */
1014
1015 /*
1016   if (optype0 == PUSHOP || optype1 == PUSHOP
1017       || (optype0 == REGOP && optype1 == REGOP
1018           && REGNO (operands[0]) == REGNO (latehalf[1]))
1019       || dest_overlapped_low)
1020 */
1021   if (optype0 == PUSHOP || optype1 == PUSHOP
1022       || (optype0 == REGOP && optype1 == REGOP
1023           && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
1024               || REGNO (operands[0]) == REGNO (latehalf[1])))
1025       || dest_overlapped_low)
1026     {
1027       /* Make any unoffsettable addresses point at high-numbered word.  */
1028       if (addreg0)
1029         asm_add (size-4, addreg0);
1030       if (addreg1)
1031         asm_add (size-4, addreg1);
1032
1033       /* Do that word.  */
1034       output_asm_insn (singlemove_string (latehalf), latehalf);
1035
1036       /* Undo the adds we just did.  */
1037       if (addreg0)
1038          asm_add (-4, addreg0);
1039       if (addreg1)
1040         asm_add (-4, addreg1);
1041
1042       if (size == 12)
1043         {
1044         output_asm_insn (singlemove_string (middlehalf), middlehalf);
1045         if (addreg0)
1046            asm_add (-4, addreg0);
1047         if (addreg1)
1048            asm_add (-4, addreg1);
1049         }
1050
1051       /* Do low-numbered word.  */
1052       return singlemove_string (operands);
1053     }
1054
1055   /* Normal case: do the two words, low-numbered first.  */
1056
1057   output_asm_insn (singlemove_string (operands), operands);
1058
1059   /* Do the middle one of the three words for long double */
1060   if (size == 12)
1061     {
1062       if (addreg0)
1063         asm_add (4, addreg0);
1064       if (addreg1)
1065         asm_add (4, addreg1);
1066
1067       output_asm_insn (singlemove_string (middlehalf), middlehalf);
1068     }
1069
1070   /* Make any unoffsettable addresses point at high-numbered word.  */
1071   if (addreg0)
1072     asm_add (4, addreg0);
1073   if (addreg1)
1074     asm_add (4, addreg1);
1075
1076   /* Do that word.  */
1077   output_asm_insn (singlemove_string (latehalf), latehalf);
1078
1079   /* Undo the adds we just did.  */
1080   if (addreg0)
1081     asm_add (4-size, addreg0);
1082   if (addreg1)
1083     asm_add (4-size, addreg1);
1084
1085   return "";
1086 }
1087
1088 \f
1089 #define MAX_TMPS 2              /* max temporary registers used */
1090
1091 /* Output the appropriate code to move push memory on the stack */
1092
1093 char *
1094 output_move_pushmem (operands, insn, length, tmp_start, n_operands)
1095      rtx operands[];
1096      rtx insn;
1097      int length;
1098      int tmp_start;
1099      int n_operands;
1100 {
1101
1102   struct {
1103     char *load;
1104     char *push;
1105     rtx   xops[2];
1106   } tmp_info[MAX_TMPS];
1107
1108   rtx src = operands[1];
1109   int max_tmps = 0;
1110   int offset = 0;
1111   int stack_p = reg_overlap_mentioned_p (stack_pointer_rtx, src);
1112   int stack_offset = 0;
1113   int i, num_tmps;
1114   rtx xops[1];
1115
1116   if (!offsettable_memref_p (src))
1117     fatal_insn ("Source is not offsettable", insn);
1118
1119   if ((length & 3) != 0)
1120     fatal_insn ("Pushing non-word aligned size", insn);
1121
1122   /* Figure out which temporary registers we have available */
1123   for (i = tmp_start; i < n_operands; i++)
1124     {
1125       if (GET_CODE (operands[i]) == REG)
1126         {
1127           if (reg_overlap_mentioned_p (operands[i], src))
1128             continue;
1129
1130           tmp_info[ max_tmps++ ].xops[1] = operands[i];
1131           if (max_tmps == MAX_TMPS)
1132             break;
1133         }
1134     }
1135
1136   if (max_tmps == 0)
1137     for (offset = length - 4; offset >= 0; offset -= 4)
1138       {
1139         xops[0] = adj_offsettable_operand (src, offset + stack_offset);
1140         output_asm_insn (AS1(push%L0,%0), xops);
1141         if (stack_p)
1142           stack_offset += 4;
1143       }
1144
1145   else
1146     for (offset = length - 4; offset >= 0; )
1147       {
1148         for (num_tmps = 0; num_tmps < max_tmps && offset >= 0; num_tmps++)
1149           {
1150             tmp_info[num_tmps].load    = AS2(mov%L0,%0,%1);
1151             tmp_info[num_tmps].push    = AS1(push%L0,%1);
1152             tmp_info[num_tmps].xops[0] = adj_offsettable_operand (src, offset + stack_offset);
1153             offset -= 4;
1154           }
1155
1156         for (i = 0; i < num_tmps; i++)
1157           output_asm_insn (tmp_info[i].load, tmp_info[i].xops);
1158
1159         for (i = 0; i < num_tmps; i++)
1160           output_asm_insn (tmp_info[i].push, tmp_info[i].xops);
1161
1162         if (stack_p)
1163           stack_offset += 4*num_tmps;
1164       }
1165
1166   return "";
1167 }
1168
1169 \f
1170
1171 /* Output the appropriate code to move data between two memory locations */
1172
1173 char *
1174 output_move_memory (operands, insn, length, tmp_start, n_operands)
1175      rtx operands[];
1176      rtx insn;
1177      int length;
1178      int tmp_start;
1179      int n_operands;
1180 {
1181   struct {
1182     char *load;
1183     char *store;
1184     rtx   xops[3];
1185   } tmp_info[MAX_TMPS];
1186
1187   rtx dest = operands[0];
1188   rtx src  = operands[1];
1189   rtx qi_tmp = NULL_RTX;
1190   int max_tmps = 0;
1191   int offset = 0;
1192   int i, num_tmps;
1193   rtx xops[3];
1194
1195   if (GET_CODE (dest) == MEM
1196       && GET_CODE (XEXP (dest, 0)) == PRE_INC
1197       && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx)
1198     return output_move_pushmem (operands, insn, length, tmp_start, n_operands);
1199
1200   if (!offsettable_memref_p (src))
1201     fatal_insn ("Source is not offsettable", insn);
1202
1203   if (!offsettable_memref_p (dest))
1204     fatal_insn ("Destination is not offsettable", insn);
1205
1206   /* Figure out which temporary registers we have available */
1207   for (i = tmp_start; i < n_operands; i++)
1208     {
1209       if (GET_CODE (operands[i]) == REG)
1210         {
1211           if ((length & 1) != 0 && !qi_tmp && QI_REG_P (operands[i]))
1212             qi_tmp = operands[i];
1213
1214           if (reg_overlap_mentioned_p (operands[i], dest))
1215             fatal_insn ("Temporary register overlaps the destination", insn);
1216
1217           if (reg_overlap_mentioned_p (operands[i], src))
1218             fatal_insn ("Temporary register overlaps the source", insn);
1219
1220           tmp_info[ max_tmps++ ].xops[2] = operands[i];
1221           if (max_tmps == MAX_TMPS)
1222             break;
1223         }
1224     }
1225
1226   if (max_tmps == 0)
1227     fatal_insn ("No scratch registers were found to do memory->memory moves", insn);
1228
1229   if ((length & 1) != 0)
1230     {
1231       if (!qi_tmp)
1232         fatal_insn ("No byte register found when moving odd # of bytes.", insn);
1233     }
1234
1235   while (length > 1)
1236     {
1237       for (num_tmps = 0; num_tmps < max_tmps; num_tmps++)
1238         {
1239           if (length >= 4)
1240             {
1241               tmp_info[num_tmps].load    = AS2(mov%L0,%1,%2);
1242               tmp_info[num_tmps].store   = AS2(mov%L0,%2,%0);
1243               tmp_info[num_tmps].xops[0] = adj_offsettable_operand (dest, offset);
1244               tmp_info[num_tmps].xops[1] = adj_offsettable_operand (src, offset);
1245               offset += 4;
1246               length -= 4;
1247             }
1248           else if (length >= 2)
1249             {
1250               tmp_info[num_tmps].load    = AS2(mov%W0,%1,%2);
1251               tmp_info[num_tmps].store   = AS2(mov%W0,%2,%0);
1252               tmp_info[num_tmps].xops[0] = adj_offsettable_operand (dest, offset);
1253               tmp_info[num_tmps].xops[1] = adj_offsettable_operand (src, offset);
1254               offset += 2;
1255               length -= 2;
1256             }
1257           else
1258             break;
1259         }
1260
1261       for (i = 0; i < num_tmps; i++)
1262         output_asm_insn (tmp_info[i].load, tmp_info[i].xops);
1263
1264       for (i = 0; i < num_tmps; i++)
1265         output_asm_insn (tmp_info[i].store, tmp_info[i].xops);
1266     }
1267
1268   if (length == 1)
1269     {
1270       xops[0] = adj_offsettable_operand (dest, offset);
1271       xops[1] = adj_offsettable_operand (src, offset);
1272       xops[2] = qi_tmp;
1273       output_asm_insn (AS2(mov%B0,%1,%2), xops);
1274       output_asm_insn (AS2(mov%B0,%2,%0), xops);
1275     }
1276
1277   return "";
1278 }
1279
1280 \f
1281 int
1282 standard_80387_constant_p (x)
1283      rtx x;
1284 {
1285 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1286   REAL_VALUE_TYPE d;
1287   jmp_buf handler;
1288   int is0, is1;
1289
1290   if (setjmp (handler))
1291     return 0;
1292
1293   set_float_handler (handler);
1294   REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1295   is0 = REAL_VALUES_EQUAL (d, dconst0);
1296   is1 = REAL_VALUES_EQUAL (d, dconst1);
1297   set_float_handler (NULL_PTR);
1298
1299   if (is0)
1300     return 1;
1301
1302   if (is1)
1303     return 2;
1304
1305   /* Note that on the 80387, other constants, such as pi,
1306      are much slower to load as standard constants
1307      than to load from doubles in memory!  */
1308 #endif
1309
1310   return 0;
1311 }
1312
1313 char *
1314 output_move_const_single (operands)
1315      rtx *operands;
1316 {
1317   if (FP_REG_P (operands[0]))
1318     {
1319       int conval = standard_80387_constant_p (operands[1]);
1320
1321       if (conval == 1)
1322         return "fldz";
1323
1324       if (conval == 2)
1325         return "fld1";
1326     }
1327   if (GET_CODE (operands[1]) == CONST_DOUBLE)
1328     {
1329       REAL_VALUE_TYPE r; long l;
1330
1331       if (GET_MODE (operands[1]) == XFmode)
1332         abort ();
1333
1334       REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
1335       REAL_VALUE_TO_TARGET_SINGLE (r, l);
1336       operands[1] = GEN_INT (l);
1337     }
1338   return singlemove_string (operands);
1339 }
1340 \f
1341 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
1342    reference and a constant.  */
1343
1344 int
1345 symbolic_operand (op, mode)
1346      register rtx op;
1347      enum machine_mode mode;
1348 {
1349   switch (GET_CODE (op))
1350     {
1351     case SYMBOL_REF:
1352     case LABEL_REF:
1353       return 1;
1354     case CONST:
1355       op = XEXP (op, 0);
1356       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1357                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1358               && GET_CODE (XEXP (op, 1)) == CONST_INT);
1359     default:
1360       return 0;
1361     }
1362 }
1363
1364 /* Test for a valid operand for a call instruction.
1365    Don't allow the arg pointer register or virtual regs
1366    since they may change into reg + const, which the patterns
1367    can't handle yet.  */
1368
1369 int
1370 call_insn_operand (op, mode)
1371      rtx op;
1372      enum machine_mode mode;
1373 {
1374   if (GET_CODE (op) == MEM
1375       && ((CONSTANT_ADDRESS_P (XEXP (op, 0))
1376            /* This makes a difference for PIC.  */
1377            && general_operand (XEXP (op, 0), Pmode))
1378           || (GET_CODE (XEXP (op, 0)) == REG
1379               && XEXP (op, 0) != arg_pointer_rtx
1380               && !(REGNO (XEXP (op, 0)) >= FIRST_PSEUDO_REGISTER
1381                    && REGNO (XEXP (op, 0)) <= LAST_VIRTUAL_REGISTER))))
1382     return 1;
1383   return 0;
1384 }
1385
1386 /* Like call_insn_operand but allow (mem (symbol_ref ...))
1387    even if pic.  */
1388
1389 int
1390 expander_call_insn_operand (op, mode)
1391      rtx op;
1392      enum machine_mode mode;
1393 {
1394   if (GET_CODE (op) == MEM
1395       && (CONSTANT_ADDRESS_P (XEXP (op, 0))
1396           || (GET_CODE (XEXP (op, 0)) == REG
1397               && XEXP (op, 0) != arg_pointer_rtx
1398               && !(REGNO (XEXP (op, 0)) >= FIRST_PSEUDO_REGISTER
1399                    && REGNO (XEXP (op, 0)) <= LAST_VIRTUAL_REGISTER))))
1400     return 1;
1401   return 0;
1402 }
1403 \f
1404 /* Returns 1 if OP contains a symbol reference */
1405
1406 int
1407 symbolic_reference_mentioned_p (op)
1408      rtx op;
1409 {
1410   register char *fmt;
1411   register int i;
1412
1413   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1414     return 1;
1415
1416   fmt = GET_RTX_FORMAT (GET_CODE (op));
1417   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
1418     {
1419       if (fmt[i] == 'E')
1420         {
1421           register int j;
1422
1423           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
1424             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
1425               return 1;
1426         }
1427       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
1428         return 1;
1429     }
1430
1431   return 0;
1432 }
1433 \f
1434 /* This function generates the assembly code for function entry.
1435    FILE is an stdio stream to output the code to.
1436    SIZE is an int: how many units of temporary storage to allocate. */
1437
1438 void
1439 function_prologue (file, size)
1440      FILE *file;
1441      int size;
1442 {
1443   register int regno;
1444   int limit;
1445   rtx xops[4];
1446   int pic_reg_used = flag_pic && (current_function_uses_pic_offset_table
1447                                   || current_function_uses_const_pool);
1448
1449   xops[0] = stack_pointer_rtx;
1450   xops[1] = frame_pointer_rtx;
1451   xops[2] = GEN_INT (size);
1452   if (frame_pointer_needed)
1453     {
1454       output_asm_insn ("push%L1 %1", xops);
1455       output_asm_insn (AS2 (mov%L0,%0,%1), xops);
1456     }
1457
1458   if (size)
1459     output_asm_insn (AS2 (sub%L0,%2,%0), xops);
1460
1461   /* Note If use enter it is NOT reversed args.
1462      This one is not reversed from intel!!
1463      I think enter is slower.  Also sdb doesn't like it.
1464      But if you want it the code is:
1465      {
1466      xops[3] = const0_rtx;
1467      output_asm_insn ("enter %2,%3", xops);
1468      }
1469      */
1470   limit = (frame_pointer_needed ? FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM);
1471   for (regno = limit - 1; regno >= 0; regno--)
1472     if ((regs_ever_live[regno] && ! call_used_regs[regno])
1473         || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1474       {
1475         xops[0] = gen_rtx (REG, SImode, regno);
1476         output_asm_insn ("push%L0 %0", xops);
1477       }
1478
1479   if (pic_reg_used)
1480     {
1481       xops[0] = pic_offset_table_rtx;
1482       xops[1] = (rtx) gen_label_rtx ();
1483
1484       output_asm_insn (AS1 (call,%P1), xops);
1485       ASM_OUTPUT_INTERNAL_LABEL (file, "L", CODE_LABEL_NUMBER (xops[1]));
1486       output_asm_insn (AS1 (pop%L0,%0), xops);
1487       output_asm_insn ("addl $_GLOBAL_OFFSET_TABLE_+[.-%P1],%0", xops);
1488     }
1489 }
1490
1491 /* Return 1 if it is appropriate to emit `ret' instructions in the
1492    body of a function.  Do this only if the epilogue is simple, needing a
1493    couple of insns.  Prior to reloading, we can't tell how many registers
1494    must be saved, so return 0 then.
1495
1496    If NON_SAVING_SETJMP is defined and true, then it is not possible
1497    for the epilogue to be simple, so return 0.  This is a special case
1498    since NON_SAVING_SETJMP will not cause regs_ever_live to change until
1499    final, but jump_optimize may need to know sooner if a `return' is OK.  */
1500
1501 int
1502 simple_386_epilogue ()
1503 {
1504   int regno;
1505   int nregs = 0;
1506   int reglimit = (frame_pointer_needed
1507                   ? FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM);
1508   int pic_reg_used = flag_pic && (current_function_uses_pic_offset_table
1509                                   || current_function_uses_const_pool);
1510
1511 #ifdef NON_SAVING_SETJMP
1512   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
1513     return 0;
1514 #endif
1515
1516   if (! reload_completed)
1517     return 0;
1518
1519   for (regno = reglimit - 1; regno >= 0; regno--)
1520     if ((regs_ever_live[regno] && ! call_used_regs[regno])
1521         || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1522       nregs++;
1523
1524   return nregs == 0 || ! frame_pointer_needed;
1525 }
1526
1527 \f
1528 /* This function generates the assembly code for function exit.
1529    FILE is an stdio stream to output the code to.
1530    SIZE is an int: how many units of temporary storage to deallocate. */
1531
1532 void
1533 function_epilogue (file, size)
1534      FILE *file;
1535      int size;
1536 {
1537   register int regno;
1538   register int nregs, limit;
1539   int offset;
1540   rtx xops[3];
1541   int pic_reg_used = flag_pic && (current_function_uses_pic_offset_table
1542                                   || current_function_uses_const_pool);
1543
1544   /* Compute the number of registers to pop */
1545
1546   limit = (frame_pointer_needed
1547            ? FRAME_POINTER_REGNUM
1548            : STACK_POINTER_REGNUM);
1549
1550   nregs = 0;
1551
1552   for (regno = limit - 1; regno >= 0; regno--)
1553     if ((regs_ever_live[regno] && ! call_used_regs[regno])
1554         || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1555       nregs++;
1556
1557   /* sp is often  unreliable so we must go off the frame pointer,
1558    */
1559
1560   /* In reality, we may not care if sp is unreliable, because we can
1561      restore the register relative to the frame pointer.  In theory,
1562      since each move is the same speed as a pop, and we don't need the
1563      leal, this is faster.  For now restore multiple registers the old
1564      way. */
1565
1566   offset = -size - (nregs * UNITS_PER_WORD);
1567
1568   xops[2] = stack_pointer_rtx;
1569
1570   if (nregs > 1 || ! frame_pointer_needed)
1571     {
1572       if (frame_pointer_needed)
1573         {
1574           xops[0] = adj_offsettable_operand (AT_BP (Pmode), offset);
1575           output_asm_insn (AS2 (lea%L2,%0,%2), xops);
1576         }
1577
1578       for (regno = 0; regno < limit; regno++)
1579         if ((regs_ever_live[regno] && ! call_used_regs[regno])
1580             || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1581           {
1582             xops[0] = gen_rtx (REG, SImode, regno);
1583             output_asm_insn ("pop%L0 %0", xops);
1584           }
1585     }
1586   else
1587     for (regno = 0; regno < limit; regno++)
1588       if ((regs_ever_live[regno] && ! call_used_regs[regno])
1589           || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1590         {
1591           xops[0] = gen_rtx (REG, SImode, regno);
1592           xops[1] = adj_offsettable_operand (AT_BP (Pmode), offset);
1593           output_asm_insn (AS2 (mov%L0,%1,%0), xops);
1594           offset += 4;
1595         }
1596
1597   if (frame_pointer_needed)
1598     {
1599       /* On i486, mov & pop is faster than "leave". */
1600
1601       if (!TARGET_386)
1602         {
1603           xops[0] = frame_pointer_rtx;
1604           output_asm_insn (AS2 (mov%L2,%0,%2), xops);
1605           output_asm_insn ("pop%L0 %0", xops);
1606         }
1607       else
1608         output_asm_insn ("leave", xops);
1609     }
1610   else if (size)
1611     {
1612       /* If there is no frame pointer, we must still release the frame. */
1613
1614       xops[0] = GEN_INT (size);
1615       output_asm_insn (AS2 (add%L2,%0,%2), xops);
1616     }
1617
1618   if (current_function_pops_args && current_function_args_size)
1619     {
1620       xops[1] = GEN_INT (current_function_pops_args);
1621
1622       /* i386 can only pop 32K bytes (maybe 64K?  Is it signed?).  If
1623          asked to pop more, pop return address, do explicit add, and jump
1624          indirectly to the caller. */
1625
1626       if (current_function_pops_args >= 32768)
1627         {
1628           /* ??? Which register to use here? */
1629           xops[0] = gen_rtx (REG, SImode, 2);
1630           output_asm_insn ("pop%L0 %0", xops);
1631           output_asm_insn (AS2 (add%L2,%1,%2), xops);
1632           output_asm_insn ("jmp %*%0", xops);
1633         }
1634       else
1635           output_asm_insn ("ret %1", xops);
1636     }
1637   else
1638     output_asm_insn ("ret", xops);
1639 }
1640
1641 \f
1642 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
1643    that is a valid memory address for an instruction.
1644    The MODE argument is the machine mode for the MEM expression
1645    that wants to use this address.
1646
1647    On x86, legitimate addresses are:
1648         base                            movl (base),reg
1649         displacement                    movl disp,reg
1650         base + displacement             movl disp(base),reg
1651         index + base                    movl (base,index),reg
1652         (index + base) + displacement   movl disp(base,index),reg
1653         index*scale                     movl (,index,scale),reg
1654         index*scale + disp              movl disp(,index,scale),reg
1655         index*scale + base              movl (base,index,scale),reg
1656         (index*scale + base) + disp     movl disp(base,index,scale),reg
1657
1658         In each case, scale can be 1, 2, 4, 8.  */
1659
1660 /* This is exactly the same as print_operand_addr, except that
1661    it recognizes addresses instead of printing them.
1662
1663    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
1664    convert common non-canonical forms to canonical form so that they will
1665    be recognized.  */
1666
1667 #define ADDR_INVALID(msg,insn)                                          \
1668 do {                                                                    \
1669   if (TARGET_DEBUG_ADDR)                                                \
1670     {                                                                   \
1671       fprintf (stderr, msg);                                            \
1672       debug_rtx (insn);                                                 \
1673     }                                                                   \
1674 } while (0)
1675
1676 int
1677 legitimate_address_p (mode, addr, strict)
1678      enum machine_mode mode;
1679      register rtx addr;
1680      int strict;
1681 {
1682   rtx base  = NULL_RTX;
1683   rtx indx  = NULL_RTX;
1684   rtx scale = NULL_RTX;
1685   rtx disp  = NULL_RTX;
1686
1687   if (TARGET_DEBUG_ADDR)
1688     {
1689       fprintf (stderr,
1690                "\n==========\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
1691                GET_MODE_NAME (mode), strict);
1692
1693       debug_rtx (addr);
1694     }
1695
1696   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
1697       base = addr;                              /* base reg */
1698
1699   else if (GET_CODE (addr) == PLUS)
1700     {
1701       rtx op0 = XEXP (addr, 0);
1702       rtx op1 = XEXP (addr, 1);
1703       enum rtx_code code0 = GET_CODE (op0);
1704       enum rtx_code code1 = GET_CODE (op1);
1705
1706       if (code0 == REG || code0 == SUBREG)
1707         {
1708           if (code1 == REG || code1 == SUBREG)
1709             {
1710               indx = op0;                       /* index + base */
1711               base = op1;
1712             }
1713
1714           else
1715             {
1716               base = op0;                       /* base + displacement */
1717               disp = op1;
1718             }
1719         }
1720
1721       else if (code0 == MULT)
1722         {
1723           indx  = XEXP (op0, 0);
1724           scale = XEXP (op0, 1);
1725
1726           if (code1 == REG || code1 == SUBREG)
1727             base = op1;                         /* index*scale + base */
1728
1729           else
1730             disp = op1;                         /* index*scale + disp */
1731         }
1732
1733       else if (code0 == PLUS && GET_CODE (XEXP (op0, 0)) == MULT)
1734         {
1735           indx  = XEXP (XEXP (op0, 0), 0);      /* index*scale + base + disp */
1736           scale = XEXP (XEXP (op0, 0), 1);
1737           base  = XEXP (op0, 1);
1738           disp  = op1;
1739         }
1740
1741       else if (code0 == PLUS)
1742         {
1743           indx = XEXP (op0, 0);                 /* index + base + disp */
1744           base = XEXP (op0, 1);
1745           disp = op1;
1746         }
1747
1748       else
1749         {
1750           ADDR_INVALID ("PLUS subcode is not valid.\n", op0);
1751           return FALSE;
1752         }
1753     }
1754
1755   else if (GET_CODE (addr) == MULT)
1756     {
1757       indx  = XEXP (addr, 0);                   /* index*scale */
1758       scale = XEXP (addr, 1);
1759     }
1760
1761   else
1762     disp = addr;                                /* displacement */
1763
1764   /* Allow arg pointer and stack pointer as index if there is not scaling */
1765   if (base && indx && !scale
1766       && (indx == arg_pointer_rtx || indx == stack_pointer_rtx))
1767     {
1768       rtx tmp = base;
1769       base = indx;
1770       indx = tmp;
1771     }
1772
1773   /* Validate base register */
1774   /* Don't allow SUBREG's here, it can lead to spill failures when the base
1775      is one word out of a two word structure, which is represented internally
1776      as a DImode int.  */
1777   if (base)
1778     {
1779       if (GET_CODE (base) != REG)
1780         {
1781           ADDR_INVALID ("Base is not a register.\n", base);
1782           return FALSE;
1783         }
1784
1785       if ((strict && !REG_OK_FOR_BASE_STRICT_P (base))
1786           || (!strict && !REG_OK_FOR_BASE_NONSTRICT_P (base)))
1787         {
1788           ADDR_INVALID ("Base is not valid.\n", base);
1789           return FALSE;
1790         }
1791     }
1792
1793   /* Validate index register */
1794   /* Don't allow SUBREG's here, it can lead to spill failures when the index
1795      is one word out of a two word structure, which is represented internally
1796      as a DImode int.  */
1797   if (indx)
1798     {
1799       if (GET_CODE (indx) != REG)
1800         {
1801           ADDR_INVALID ("Index is not a register.\n", indx);
1802           return FALSE;
1803         }
1804
1805       if ((strict && !REG_OK_FOR_INDEX_STRICT_P (indx))
1806           || (!strict && !REG_OK_FOR_INDEX_NONSTRICT_P (indx)))
1807         {
1808           ADDR_INVALID ("Index is not valid.\n", indx);
1809           return FALSE;
1810         }
1811     }
1812   else if (scale)
1813     abort ();                                   /* scale w/o index invalid */
1814
1815   /* Validate scale factor */
1816   if (scale)
1817     {
1818       HOST_WIDE_INT value;
1819
1820       if (GET_CODE (scale) != CONST_INT)
1821         {
1822           ADDR_INVALID ("Scale is not valid.\n", scale);
1823           return FALSE;
1824         }
1825
1826       value = INTVAL (scale);
1827       if (value != 1 && value != 2 && value != 4 && value != 8)
1828         {
1829           ADDR_INVALID ("Scale is not a good multiplier.\n", scale);
1830           return FALSE;
1831         }
1832     }
1833
1834   /* Validate displacement */
1835   if (disp)
1836     {
1837       if (!CONSTANT_ADDRESS_P (disp))
1838         {
1839           ADDR_INVALID ("Displacement is not valid.\n", disp);
1840           return FALSE;
1841         }
1842
1843       if (GET_CODE (disp) == CONST_DOUBLE)
1844         {
1845           ADDR_INVALID ("Displacement is a const_double.\n", disp);
1846           return FALSE;
1847         }
1848
1849       if (flag_pic && SYMBOLIC_CONST (disp) && base != pic_offset_table_rtx
1850           && (indx != pic_offset_table_rtx || scale != NULL_RTX))
1851         {
1852           ADDR_INVALID ("Displacement is an invalid pic reference.\n", disp);
1853           return FALSE;
1854         }
1855
1856       if (HALF_PIC_P () && HALF_PIC_ADDRESS_P (disp)
1857           && (base != NULL_RTX || indx != NULL_RTX))
1858         {
1859           ADDR_INVALID ("Displacement is an invalid half-pic reference.\n", disp);
1860           return FALSE;
1861         }
1862     }
1863
1864   if (TARGET_DEBUG_ADDR)
1865     fprintf (stderr, "Address is valid.\n");
1866
1867   /* Everything looks valid, return true */
1868   return TRUE;
1869 }
1870
1871 \f
1872 /* Return a legitimate reference for ORIG (an address) using the
1873    register REG.  If REG is 0, a new pseudo is generated.
1874
1875    There are three types of references that must be handled:
1876
1877    1. Global data references must load the address from the GOT, via
1878       the PIC reg.  An insn is emitted to do this load, and the reg is
1879       returned.
1880
1881    2. Static data references must compute the address as an offset
1882       from the GOT, whose base is in the PIC reg.  An insn is emitted to
1883       compute the address into a reg, and the reg is returned.  Static
1884       data objects have SYMBOL_REF_FLAG set to differentiate them from
1885       global data objects.
1886
1887    3. Constant pool addresses must be handled special.  They are
1888       considered legitimate addresses, but only if not used with regs.
1889       When printed, the output routines know to print the reference with the
1890       PIC reg, even though the PIC reg doesn't appear in the RTL.
1891
1892    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
1893    reg also appears in the address (except for constant pool references,
1894    noted above).
1895
1896    "switch" statements also require special handling when generating
1897    PIC code.  See comments by the `casesi' insn in i386.md for details.  */
1898
1899 rtx
1900 legitimize_pic_address (orig, reg)
1901      rtx orig;
1902      rtx reg;
1903 {
1904   rtx addr = orig;
1905   rtx new = orig;
1906
1907   if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
1908     {
1909       if (GET_CODE (addr) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (addr))
1910         reg = new = orig;
1911       else
1912         {
1913           if (reg == 0)
1914             reg = gen_reg_rtx (Pmode);
1915
1916           if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_FLAG (addr))
1917               || GET_CODE (addr) == LABEL_REF)
1918             new = gen_rtx (PLUS, Pmode, pic_offset_table_rtx, orig);
1919           else
1920             new = gen_rtx (MEM, Pmode,
1921                            gen_rtx (PLUS, Pmode,
1922                                     pic_offset_table_rtx, orig));
1923
1924           emit_move_insn (reg, new);
1925         }
1926       current_function_uses_pic_offset_table = 1;
1927       return reg;
1928     }
1929   else if (GET_CODE (addr) == CONST || GET_CODE (addr) == PLUS)
1930     {
1931       rtx base;
1932
1933       if (GET_CODE (addr) == CONST)
1934         {
1935           addr = XEXP (addr, 0);
1936           if (GET_CODE (addr) != PLUS)
1937             abort ();
1938         }
1939
1940       if (XEXP (addr, 0) == pic_offset_table_rtx)
1941         return orig;
1942
1943       if (reg == 0)
1944         reg = gen_reg_rtx (Pmode);
1945
1946       base = legitimize_pic_address (XEXP (addr, 0), reg);
1947       addr = legitimize_pic_address (XEXP (addr, 1),
1948                                      base == reg ? NULL_RTX : reg);
1949
1950       if (GET_CODE (addr) == CONST_INT)
1951         return plus_constant (base, INTVAL (addr));
1952
1953       if (GET_CODE (addr) == PLUS && CONSTANT_P (XEXP (addr, 1)))
1954         {
1955           base = gen_rtx (PLUS, Pmode, base, XEXP (addr, 0));
1956           addr = XEXP (addr, 1);
1957         }
1958         return gen_rtx (PLUS, Pmode, base, addr);
1959     }
1960   return new;
1961 }
1962 \f
1963
1964 /* Emit insns to move operands[1] into operands[0].  */
1965
1966 void
1967 emit_pic_move (operands, mode)
1968      rtx *operands;
1969      enum machine_mode mode;
1970 {
1971   rtx temp = reload_in_progress ? operands[0] : gen_reg_rtx (Pmode);
1972
1973   if (GET_CODE (operands[0]) == MEM && SYMBOLIC_CONST (operands[1]))
1974     operands[1] = (rtx) force_reg (SImode, operands[1]);
1975   else
1976     operands[1] = legitimize_pic_address (operands[1], temp);
1977 }
1978
1979 \f
1980 /* Try machine-dependent ways of modifying an illegitimate address
1981    to be legitimate.  If we find one, return the new, valid address.
1982    This macro is used in only one place: `memory_address' in explow.c.
1983
1984    OLDX is the address as it was before break_out_memory_refs was called.
1985    In some cases it is useful to look at this to decide what needs to be done.
1986
1987    MODE and WIN are passed so that this macro can use
1988    GO_IF_LEGITIMATE_ADDRESS.
1989
1990    It is always safe for this macro to do nothing.  It exists to recognize
1991    opportunities to optimize the output.
1992
1993    For the 80386, we handle X+REG by loading X into a register R and
1994    using R+REG.  R will go in a general reg and indexing will be used.
1995    However, if REG is a broken-out memory address or multiplication,
1996    nothing needs to be done because REG can certainly go in a general reg.
1997
1998    When -fpic is used, special handling is needed for symbolic references.
1999    See comments by legitimize_pic_address in i386.c for details.  */
2000
2001 rtx
2002 legitimize_address (x, oldx, mode)
2003      register rtx x;
2004      register rtx oldx;
2005      enum machine_mode mode;
2006 {
2007   int changed = 0;
2008   unsigned log;
2009
2010   if (TARGET_DEBUG_ADDR)
2011     {
2012       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n", GET_MODE_NAME (mode));
2013       debug_rtx (x);
2014     }
2015
2016   if (flag_pic && SYMBOLIC_CONST (x))
2017     return legitimize_pic_address (x, 0);
2018
2019   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
2020   if (GET_CODE (x) == ASHIFT
2021       && GET_CODE (XEXP (x, 1)) == CONST_INT
2022       && (log = (unsigned)exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
2023     {
2024       changed = 1;
2025       x = gen_rtx (MULT, Pmode,
2026                    force_reg (Pmode, XEXP (x, 0)),
2027                    GEN_INT (1 << log));
2028     }
2029
2030   if (GET_CODE (x) == PLUS)
2031     {
2032       /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
2033       if (GET_CODE (XEXP (x, 0)) == ASHIFT
2034           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2035           && (log = (unsigned)exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
2036         {
2037           changed = 1;
2038           XEXP (x, 0) = gen_rtx (MULT, Pmode,
2039                                  force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
2040                                  GEN_INT (1 << log));
2041         }
2042
2043       if (GET_CODE (XEXP (x, 1)) == ASHIFT
2044           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
2045           && (log = (unsigned)exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
2046         {
2047           changed = 1;
2048           XEXP (x, 1) = gen_rtx (MULT, Pmode,
2049                                  force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
2050                                  GEN_INT (1 << log));
2051         }
2052
2053       /* Put multiply first if it isn't already */
2054       if (GET_CODE (XEXP (x, 1)) == MULT)
2055         {
2056           rtx tmp = XEXP (x, 0);
2057           XEXP (x, 0) = XEXP (x, 1);
2058           XEXP (x, 1) = tmp;
2059           changed = 1;
2060         }
2061
2062       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
2063          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
2064          created by virtual register instantiation, register elimination, and
2065          similar optimizations.  */
2066       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
2067         {
2068           changed = 1;
2069           x = gen_rtx (PLUS, Pmode,
2070                        gen_rtx (PLUS, Pmode, XEXP (x, 0), XEXP (XEXP (x, 1), 0)),
2071                        XEXP (XEXP (x, 1), 1));
2072         }
2073
2074       /* Canonicalize (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
2075          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
2076       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
2077                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2078                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
2079                && CONSTANT_P (XEXP (x, 1)))
2080         {
2081           rtx constant, other;
2082
2083           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2084             {
2085               constant = XEXP (x, 1);
2086               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
2087             }
2088           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
2089             {
2090               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
2091               other = XEXP (x, 1);
2092             }
2093           else
2094             constant = 0;
2095
2096           if (constant)
2097             {
2098               changed = 1;
2099               x = gen_rtx (PLUS, Pmode,
2100                            gen_rtx (PLUS, Pmode, XEXP (XEXP (x, 0), 0),
2101                                     XEXP (XEXP (XEXP (x, 0), 1), 0)),
2102                            plus_constant (other, INTVAL (constant)));
2103             }
2104         }
2105
2106       if (changed && legitimate_address_p (mode, x, FALSE))
2107         return x;
2108
2109       if (GET_CODE (XEXP (x, 0)) == MULT)
2110         {
2111           changed = 1;
2112           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
2113         }
2114
2115       if (GET_CODE (XEXP (x, 1)) == MULT)
2116         {
2117           changed = 1;
2118           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
2119         }
2120
2121       if (changed
2122           && GET_CODE (XEXP (x, 1)) == REG
2123           && GET_CODE (XEXP (x, 0)) == REG)
2124         return x;
2125
2126       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
2127         {
2128           changed = 1;
2129           x = legitimize_pic_address (x, 0);
2130         }
2131
2132       if (changed && legitimate_address_p (mode, x, FALSE))
2133         return x;
2134
2135       if (GET_CODE (XEXP (x, 0)) == REG)
2136         {
2137           register rtx temp = gen_reg_rtx (Pmode);
2138           register rtx val  = force_operand (XEXP (x, 1), temp);
2139           if (val != temp)
2140             emit_move_insn (temp, val);
2141
2142           XEXP (x, 1) = temp;
2143           return x;
2144         }
2145
2146       else if (GET_CODE (XEXP (x, 1)) == REG)
2147         {
2148           register rtx temp = gen_reg_rtx (Pmode);
2149           register rtx val  = force_operand (XEXP (x, 0), temp);
2150           if (val != temp)
2151             emit_move_insn (temp, val);
2152
2153           XEXP (x, 0) = temp;
2154           return x;
2155         }
2156     }
2157
2158   return x;
2159 }
2160
2161 \f
2162 /* Print an integer constant expression in assembler syntax.  Addition
2163    and subtraction are the only arithmetic that may appear in these
2164    expressions.  FILE is the stdio stream to write to, X is the rtx, and
2165    CODE is the operand print code from the output string.  */
2166
2167 static void
2168 output_pic_addr_const (file, x, code)
2169      FILE *file;
2170      rtx x;
2171      int code;
2172 {
2173   char buf[256];
2174
2175   switch (GET_CODE (x))
2176     {
2177     case PC:
2178       if (flag_pic)
2179         putc ('.', file);
2180       else
2181         abort ();
2182       break;
2183
2184     case SYMBOL_REF:
2185     case LABEL_REF:
2186       if (GET_CODE (x) == SYMBOL_REF)
2187         assemble_name (file, XSTR (x, 0));
2188       else
2189         {
2190           ASM_GENERATE_INTERNAL_LABEL (buf, "L",
2191                                        CODE_LABEL_NUMBER (XEXP (x, 0)));
2192           assemble_name (asm_out_file, buf);
2193         }
2194
2195       if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
2196         fprintf (file, "@GOTOFF(%%ebx)");
2197       else if (code == 'P')
2198         fprintf (file, "@PLT");
2199       else if (GET_CODE (x) == LABEL_REF)
2200         fprintf (file, "@GOTOFF");
2201       else if (! SYMBOL_REF_FLAG (x))
2202         fprintf (file, "@GOT");
2203       else
2204         fprintf (file, "@GOTOFF");
2205
2206       break;
2207
2208     case CODE_LABEL:
2209       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
2210       assemble_name (asm_out_file, buf);
2211       break;
2212
2213     case CONST_INT:
2214       fprintf (file, "%d", INTVAL (x));
2215       break;
2216
2217     case CONST:
2218       /* This used to output parentheses around the expression,
2219          but that does not work on the 386 (either ATT or BSD assembler).  */
2220       output_pic_addr_const (file, XEXP (x, 0), code);
2221       break;
2222
2223     case CONST_DOUBLE:
2224       if (GET_MODE (x) == VOIDmode)
2225         {
2226           /* We can use %d if the number is <32 bits and positive.  */
2227           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
2228             fprintf (file, "0x%x%08x",
2229                      CONST_DOUBLE_HIGH (x), CONST_DOUBLE_LOW (x));
2230           else
2231             fprintf (file, "%d", CONST_DOUBLE_LOW (x));
2232         }
2233       else
2234         /* We can't handle floating point constants;
2235            PRINT_OPERAND must handle them.  */
2236         output_operand_lossage ("floating constant misused");
2237       break;
2238
2239     case PLUS:
2240       /* Some assemblers need integer constants to appear last (eg masm).  */
2241       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
2242         {
2243           output_pic_addr_const (file, XEXP (x, 1), code);
2244           if (INTVAL (XEXP (x, 0)) >= 0)
2245             fprintf (file, "+");
2246           output_pic_addr_const (file, XEXP (x, 0), code);
2247         }
2248       else
2249         {
2250           output_pic_addr_const (file, XEXP (x, 0), code);
2251           if (INTVAL (XEXP (x, 1)) >= 0)
2252             fprintf (file, "+");
2253           output_pic_addr_const (file, XEXP (x, 1), code);
2254         }
2255       break;
2256
2257     case MINUS:
2258       output_pic_addr_const (file, XEXP (x, 0), code);
2259       fprintf (file, "-");
2260       output_pic_addr_const (file, XEXP (x, 1), code);
2261       break;
2262
2263     default:
2264       output_operand_lossage ("invalid expression as operand");
2265     }
2266 }
2267 \f
2268 /* Meaning of CODE:
2269    f -- float insn (print a CONST_DOUBLE as a float rather than in hex).
2270    D,L,W,B,Q,S -- print the opcode suffix for specified size of operand.
2271    R -- print the prefix for register names.
2272    z -- print the opcode suffix for the size of the current operand.
2273    * -- print a star (in certain assembler syntax)
2274    w -- print the operand as if it's a "word" (HImode) even if it isn't.
2275    c -- don't print special prefixes before constant operands.
2276    J -- print the appropriate jump operand.
2277 */
2278
2279 void
2280 print_operand (file, x, code)
2281      FILE *file;
2282      rtx x;
2283      int code;
2284 {
2285   if (code)
2286     {
2287       switch (code)
2288         {
2289         case '*':
2290           if (USE_STAR)
2291             putc ('*', file);
2292           return;
2293
2294         case 'L':
2295           PUT_OP_SIZE (code, 'l', file);
2296           return;
2297
2298         case 'W':
2299           PUT_OP_SIZE (code, 'w', file);
2300           return;
2301
2302         case 'B':
2303           PUT_OP_SIZE (code, 'b', file);
2304           return;
2305
2306         case 'Q':
2307           PUT_OP_SIZE (code, 'l', file);
2308           return;
2309
2310         case 'S':
2311           PUT_OP_SIZE (code, 's', file);
2312           return;
2313
2314         case 'T':
2315           PUT_OP_SIZE (code, 't', file);
2316           return;
2317
2318         case 'z':
2319           /* 387 opcodes don't get size suffixes if the operands are
2320              registers. */
2321
2322           if (STACK_REG_P (x))
2323             return;
2324
2325           /* this is the size of op from size of operand */
2326           switch (GET_MODE_SIZE (GET_MODE (x)))
2327             {
2328             case 1:
2329               PUT_OP_SIZE ('B', 'b', file);
2330               return;
2331
2332             case 2:
2333               PUT_OP_SIZE ('W', 'w', file);
2334               return;
2335
2336             case 4:
2337               if (GET_MODE (x) == SFmode)
2338                 {
2339                   PUT_OP_SIZE ('S', 's', file);
2340                   return;
2341                 }
2342               else
2343                 PUT_OP_SIZE ('L', 'l', file);
2344               return;
2345
2346             case 12:
2347                   PUT_OP_SIZE ('T', 't', file);
2348                   return;
2349
2350             case 8:
2351               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
2352                 {
2353 #ifdef GAS_MNEMONICS
2354                   PUT_OP_SIZE ('Q', 'q', file);
2355                   return;
2356 #else
2357                   PUT_OP_SIZE ('Q', 'l', file); /* Fall through */
2358 #endif
2359                 }
2360
2361               PUT_OP_SIZE ('Q', 'l', file);
2362               return;
2363             }
2364
2365         case 'b':
2366         case 'w':
2367         case 'k':
2368         case 'h':
2369         case 'y':
2370         case 'P':
2371           break;
2372
2373         case 'J':
2374           switch (GET_CODE (x))
2375             {
2376               /* These conditions are appropriate for testing the result
2377                  of an arithmetic operation, not for a compare operation.
2378                  Cases GE, LT assume CC_NO_OVERFLOW true. All cases assume
2379                  CC_Z_IN_NOT_C false and not floating point.  */
2380             case NE:  fputs ("jne", file); return;
2381             case EQ:  fputs ("je",  file); return;
2382             case GE:  fputs ("jns", file); return;
2383             case GT:  fputs ("jg",  file); return;
2384             case LE:  fputs ("jle", file); return;
2385             case LT:  fputs ("js",  file); return;
2386             case GEU: fputs ("jae", file); return;
2387             case GTU: fputs ("ja",  file); return;
2388             case LEU: fputs ("jbe", file); return;
2389             case LTU: fputs ("jb",  file); return;
2390             }
2391           abort ();
2392
2393         default:
2394           {
2395             char str[50];
2396
2397             sprintf (str, "invalid operand code `%c'", code);
2398             output_operand_lossage (str);
2399           }
2400         }
2401     }
2402   if (GET_CODE (x) == REG)
2403     {
2404       PRINT_REG (x, code, file);
2405     }
2406   else if (GET_CODE (x) == MEM)
2407     {
2408       PRINT_PTR (x, file);
2409       if (CONSTANT_ADDRESS_P (XEXP (x, 0)))
2410         {
2411           if (flag_pic)
2412             output_pic_addr_const (file, XEXP (x, 0), code);
2413           else
2414             output_addr_const (file, XEXP (x, 0));
2415         }
2416       else
2417         output_address (XEXP (x, 0));
2418     }
2419   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
2420     {
2421       REAL_VALUE_TYPE r; long l;
2422       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2423       REAL_VALUE_TO_TARGET_SINGLE (r, l);
2424       PRINT_IMMED_PREFIX (file);
2425       fprintf (file, "0x%x", l);
2426     }
2427  /* These float cases don't actually occur as immediate operands. */
2428  else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
2429     {
2430       REAL_VALUE_TYPE r; char dstr[30];
2431       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2432       REAL_VALUE_TO_DECIMAL (r, "%.22e", dstr);
2433       fprintf (file, "%s", dstr);
2434     }
2435   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == XFmode)
2436     {
2437       REAL_VALUE_TYPE r; char dstr[30];
2438       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2439       REAL_VALUE_TO_DECIMAL (r, "%.22e", dstr);
2440       fprintf (file, "%s", dstr);
2441     }
2442   else 
2443     {
2444       if (code != 'P')
2445         {
2446           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
2447             PRINT_IMMED_PREFIX (file);
2448           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
2449                    || GET_CODE (x) == LABEL_REF)
2450             PRINT_OFFSET_PREFIX (file);
2451         }
2452       if (flag_pic)
2453         output_pic_addr_const (file, x, code);
2454       else
2455         output_addr_const (file, x);
2456     }
2457 }
2458 \f
2459 /* Print a memory operand whose address is ADDR.  */
2460
2461 void
2462 print_operand_address (file, addr)
2463      FILE *file;
2464      register rtx addr;
2465 {
2466   register rtx reg1, reg2, breg, ireg;
2467   rtx offset;
2468
2469   switch (GET_CODE (addr))
2470     {
2471     case REG:
2472       ADDR_BEG (file);
2473       fprintf (file, "%se", RP);
2474       fputs (hi_reg_name[REGNO (addr)], file);
2475       ADDR_END (file);
2476       break;
2477
2478     case PLUS:
2479       reg1 = 0;
2480       reg2 = 0;
2481       ireg = 0;
2482       breg = 0;
2483       offset = 0;
2484       if (CONSTANT_ADDRESS_P (XEXP (addr, 0)))
2485         {
2486           offset = XEXP (addr, 0);
2487           addr = XEXP (addr, 1);
2488         }
2489       else if (CONSTANT_ADDRESS_P (XEXP (addr, 1)))
2490         {
2491           offset = XEXP (addr, 1);
2492           addr = XEXP (addr, 0);
2493         }
2494       if (GET_CODE (addr) != PLUS) ;
2495       else if (GET_CODE (XEXP (addr, 0)) == MULT)
2496         {
2497           reg1 = XEXP (addr, 0);
2498           addr = XEXP (addr, 1);
2499         }
2500       else if (GET_CODE (XEXP (addr, 1)) == MULT)
2501         {
2502           reg1 = XEXP (addr, 1);
2503           addr = XEXP (addr, 0);
2504         }
2505       else if (GET_CODE (XEXP (addr, 0)) == REG)
2506         {
2507           reg1 = XEXP (addr, 0);
2508           addr = XEXP (addr, 1);
2509         }
2510       else if (GET_CODE (XEXP (addr, 1)) == REG)
2511         {
2512           reg1 = XEXP (addr, 1);
2513           addr = XEXP (addr, 0);
2514         }
2515       if (GET_CODE (addr) == REG || GET_CODE (addr) == MULT)
2516         {
2517           if (reg1 == 0) reg1 = addr;
2518           else reg2 = addr;
2519           addr = 0;
2520         }
2521       if (offset != 0)
2522         {
2523           if (addr != 0) abort ();
2524           addr = offset;
2525         }
2526       if ((reg1 && GET_CODE (reg1) == MULT)
2527           || (reg2 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg2))))
2528         {
2529           breg = reg2;
2530           ireg = reg1;
2531         }
2532       else if (reg1 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg1)))
2533         {
2534           breg = reg1;
2535           ireg = reg2;
2536         }
2537
2538       if (ireg != 0 || breg != 0)
2539         {
2540           int scale = 1;
2541
2542           if (addr != 0)
2543             {
2544               if (flag_pic)
2545                 output_pic_addr_const (file, addr, 0);
2546
2547               else if (GET_CODE (addr) == LABEL_REF)
2548                 output_asm_label (addr);
2549
2550               else
2551                 output_addr_const (file, addr);
2552             }
2553
2554           if (ireg != 0 && GET_CODE (ireg) == MULT)
2555             {
2556               scale = INTVAL (XEXP (ireg, 1));
2557               ireg = XEXP (ireg, 0);
2558             }
2559
2560           /* The stack pointer can only appear as a base register,
2561              never an index register, so exchange the regs if it is wrong. */
2562
2563           if (scale == 1 && ireg && REGNO (ireg) == STACK_POINTER_REGNUM)
2564             {
2565               rtx tmp;
2566
2567               tmp = breg;
2568               breg = ireg;
2569               ireg = tmp;
2570             }
2571
2572           /* output breg+ireg*scale */
2573           PRINT_B_I_S (breg, ireg, scale, file);
2574           break;
2575         }
2576
2577     case MULT:
2578       {
2579         int scale;
2580         if (GET_CODE (XEXP (addr, 0)) == CONST_INT)
2581           {
2582             scale = INTVAL (XEXP (addr, 0));
2583             ireg = XEXP (addr, 1);
2584           }
2585         else
2586           {
2587             scale = INTVAL (XEXP (addr, 1));
2588             ireg = XEXP (addr, 0);
2589           }
2590         output_addr_const (file, const0_rtx);
2591         PRINT_B_I_S ((rtx) 0, ireg, scale, file);
2592       }
2593       break;
2594
2595     default:
2596       if (GET_CODE (addr) == CONST_INT
2597           && INTVAL (addr) < 0x8000
2598           && INTVAL (addr) >= -0x8000)
2599         fprintf (file, "%d", INTVAL (addr));
2600       else
2601         {
2602           if (flag_pic)
2603             output_pic_addr_const (file, addr, 0);
2604           else
2605             output_addr_const (file, addr);
2606         }
2607     }
2608 }
2609 \f
2610 /* Set the cc_status for the results of an insn whose pattern is EXP.
2611    On the 80386, we assume that only test and compare insns, as well
2612    as SI, HI, & DI mode ADD, SUB, NEG, AND, IOR, XOR, ASHIFT,
2613    ASHIFTRT, and LSHIFTRT instructions set the condition codes usefully.
2614    Also, we assume that jumps, moves and sCOND don't affect the condition
2615    codes.  All else clobbers the condition codes, by assumption.
2616
2617    We assume that ALL integer add, minus, etc. instructions effect the
2618    condition codes.  This MUST be consistent with i386.md.
2619
2620    We don't record any float test or compare - the redundant test &
2621    compare check in final.c does not handle stack-like regs correctly. */
2622
2623 void
2624 notice_update_cc (exp)
2625      rtx exp;
2626 {
2627   if (GET_CODE (exp) == SET)
2628     {
2629       /* Jumps do not alter the cc's.  */
2630       if (SET_DEST (exp) == pc_rtx)
2631         return;
2632       /* Moving register or memory into a register:
2633          it doesn't alter the cc's, but it might invalidate
2634          the RTX's which we remember the cc's came from.
2635          (Note that moving a constant 0 or 1 MAY set the cc's).  */
2636       if (REG_P (SET_DEST (exp))
2637           && (REG_P (SET_SRC (exp)) || GET_CODE (SET_SRC (exp)) == MEM
2638               || GET_RTX_CLASS (GET_CODE (SET_SRC (exp))) == '<'))
2639         {
2640           if (cc_status.value1
2641               && reg_overlap_mentioned_p (SET_DEST (exp), cc_status.value1))
2642             cc_status.value1 = 0;
2643           if (cc_status.value2
2644               && reg_overlap_mentioned_p (SET_DEST (exp), cc_status.value2))
2645             cc_status.value2 = 0;
2646           return;
2647         }
2648       /* Moving register into memory doesn't alter the cc's.
2649          It may invalidate the RTX's which we remember the cc's came from.  */
2650       if (GET_CODE (SET_DEST (exp)) == MEM
2651           && (REG_P (SET_SRC (exp))
2652               || GET_RTX_CLASS (GET_CODE (SET_SRC (exp))) == '<'))
2653         {
2654           if (cc_status.value1 && GET_CODE (cc_status.value1) == MEM)
2655             cc_status.value1 = 0;
2656           if (cc_status.value2 && GET_CODE (cc_status.value2) == MEM)
2657             cc_status.value2 = 0;
2658           return;
2659         }
2660       /* Function calls clobber the cc's.  */
2661       else if (GET_CODE (SET_SRC (exp)) == CALL)
2662         {
2663           CC_STATUS_INIT;
2664           return;
2665         }
2666       /* Tests and compares set the cc's in predictable ways.  */
2667       else if (SET_DEST (exp) == cc0_rtx)
2668         {
2669           CC_STATUS_INIT;
2670           cc_status.value1 = SET_SRC (exp);
2671           return;
2672         }
2673       /* Certain instructions effect the condition codes. */
2674       else if (GET_MODE (SET_SRC (exp)) == SImode
2675                || GET_MODE (SET_SRC (exp)) == HImode
2676                || GET_MODE (SET_SRC (exp)) == QImode)
2677         switch (GET_CODE (SET_SRC (exp)))
2678           {
2679           case ASHIFTRT: case LSHIFTRT:
2680           case ASHIFT:
2681             /* Shifts on the 386 don't set the condition codes if the
2682                shift count is zero. */
2683             if (GET_CODE (XEXP (SET_SRC (exp), 1)) != CONST_INT)
2684               {
2685                 CC_STATUS_INIT;
2686                 break;
2687               }
2688             /* We assume that the CONST_INT is non-zero (this rtx would
2689                have been deleted if it were zero. */
2690
2691           case PLUS: case MINUS: case NEG:
2692           case AND: case IOR: case XOR:
2693             cc_status.flags = CC_NO_OVERFLOW;
2694             cc_status.value1 = SET_SRC (exp);
2695             cc_status.value2 = SET_DEST (exp);
2696             break;
2697
2698           default:
2699             CC_STATUS_INIT;
2700           }
2701       else
2702         {
2703           CC_STATUS_INIT;
2704         }
2705     }
2706   else if (GET_CODE (exp) == PARALLEL
2707            && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
2708     {
2709       if (SET_DEST (XVECEXP (exp, 0, 0)) == pc_rtx)
2710         return;
2711       if (SET_DEST (XVECEXP (exp, 0, 0)) == cc0_rtx)
2712         {
2713           CC_STATUS_INIT;
2714           if (stack_regs_mentioned_p (SET_SRC (XVECEXP (exp, 0, 0))))
2715             cc_status.flags |= CC_IN_80387;
2716           else
2717             cc_status.value1 = SET_SRC (XVECEXP (exp, 0, 0));
2718           return;
2719         }
2720       CC_STATUS_INIT;
2721     }
2722   else
2723     {
2724       CC_STATUS_INIT;
2725     }
2726 }
2727 \f
2728 /* Split one or more DImode RTL references into pairs of SImode
2729    references.  The RTL can be REG, offsettable MEM, integer constant, or
2730    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
2731    split and "num" is its length.  lo_half and hi_half are output arrays
2732    that parallel "operands". */
2733
2734 void
2735 split_di (operands, num, lo_half, hi_half)
2736      rtx operands[];
2737      int num;
2738      rtx lo_half[], hi_half[];
2739 {
2740   while (num--)
2741     {
2742       if (GET_CODE (operands[num]) == REG)
2743         {
2744           lo_half[num] = gen_rtx (REG, SImode, REGNO (operands[num]));
2745           hi_half[num] = gen_rtx (REG, SImode, REGNO (operands[num]) + 1);
2746         }
2747       else if (CONSTANT_P (operands[num]))
2748         {
2749           split_double (operands[num], &lo_half[num], &hi_half[num]);
2750         }
2751       else if (offsettable_memref_p (operands[num]))
2752         {
2753           lo_half[num] = operands[num];
2754           hi_half[num] = adj_offsettable_operand (operands[num], 4);
2755         }
2756       else
2757         abort();
2758     }
2759 }
2760 \f
2761 /* Return 1 if this is a valid binary operation on a 387.
2762    OP is the expression matched, and MODE is its mode. */
2763
2764 int
2765 binary_387_op (op, mode)
2766     register rtx op;
2767     enum machine_mode mode;
2768 {
2769   if (mode != VOIDmode && mode != GET_MODE (op))
2770     return 0;
2771
2772   switch (GET_CODE (op))
2773     {
2774     case PLUS:
2775     case MINUS:
2776     case MULT:
2777     case DIV:
2778       return GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT;
2779
2780     default:
2781       return 0;
2782     }
2783 }
2784
2785 \f
2786 /* Return 1 if this is a valid shift or rotate operation on a 386.
2787    OP is the expression matched, and MODE is its mode. */
2788
2789 int
2790 shift_op (op, mode)
2791     register rtx op;
2792     enum machine_mode mode;
2793 {
2794   rtx operand = XEXP (op, 0);
2795
2796   if (mode != VOIDmode && mode != GET_MODE (op))
2797     return 0;
2798
2799   if (GET_MODE (operand) != GET_MODE (op)
2800       || GET_MODE_CLASS (GET_MODE (op)) != MODE_INT)
2801     return 0;
2802
2803   return (GET_CODE (op) == ASHIFT
2804           || GET_CODE (op) == ASHIFTRT
2805           || GET_CODE (op) == LSHIFTRT
2806           || GET_CODE (op) == ROTATE
2807           || GET_CODE (op) == ROTATERT);
2808 }
2809
2810 /* Return 1 if OP is COMPARE rtx with mode VOIDmode.
2811    MODE is not used.  */
2812
2813 int
2814 VOIDmode_compare_op (op, mode)
2815     register rtx op;
2816     enum machine_mode mode;
2817 {
2818   return GET_CODE (op) == COMPARE && GET_MODE (op) == VOIDmode;
2819 }
2820 \f
2821 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
2822    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
2823    is the expression of the binary operation.  The output may either be
2824    emitted here, or returned to the caller, like all output_* functions.
2825
2826    There is no guarantee that the operands are the same mode, as they
2827    might be within FLOAT or FLOAT_EXTEND expressions. */
2828
2829 char *
2830 output_387_binary_op (insn, operands)
2831      rtx insn;
2832      rtx *operands;
2833 {
2834   rtx temp;
2835   char *base_op;
2836   static char buf[100];
2837
2838   switch (GET_CODE (operands[3]))
2839     {
2840     case PLUS:
2841       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
2842           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
2843         base_op = "fiadd";
2844       else
2845         base_op = "fadd";
2846       break;
2847
2848     case MINUS:
2849       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
2850           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
2851         base_op = "fisub";
2852       else
2853         base_op = "fsub";
2854       break;
2855
2856     case MULT:
2857       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
2858           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
2859         base_op = "fimul";
2860       else
2861         base_op = "fmul";
2862       break;
2863
2864     case DIV:
2865       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
2866           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
2867         base_op = "fidiv";
2868       else
2869         base_op = "fdiv";
2870       break;
2871
2872     default:
2873       abort ();
2874     }
2875
2876   strcpy (buf, base_op);
2877
2878   switch (GET_CODE (operands[3]))
2879     {
2880     case MULT:
2881     case PLUS:
2882       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
2883         {
2884           temp = operands[2];
2885           operands[2] = operands[1];
2886           operands[1] = temp;
2887         }
2888
2889       if (GET_CODE (operands[2]) == MEM)
2890         return strcat (buf, AS1 (%z2,%2));
2891
2892       if (NON_STACK_REG_P (operands[1]))
2893         {
2894           output_op_from_reg (operands[1], strcat (buf, AS1 (%z0,%1)));
2895           RET;
2896         }
2897       else if (NON_STACK_REG_P (operands[2]))
2898         {
2899           output_op_from_reg (operands[2], strcat (buf, AS1 (%z0,%1)));
2900           RET;
2901         }
2902
2903       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
2904         return strcat (buf, AS2 (p,%2,%0));
2905
2906       if (STACK_TOP_P (operands[0]))
2907         return strcat (buf, AS2C (%y2,%0));
2908       else
2909         return strcat (buf, AS2C (%2,%0));
2910
2911     case MINUS:
2912     case DIV:
2913       if (GET_CODE (operands[1]) == MEM)
2914         return strcat (buf, AS1 (r%z1,%1));
2915
2916       if (GET_CODE (operands[2]) == MEM)
2917         return strcat (buf, AS1 (%z2,%2));
2918
2919       if (NON_STACK_REG_P (operands[1]))
2920         {
2921           output_op_from_reg (operands[1], strcat (buf, AS1 (r%z0,%1)));
2922           RET;
2923         }
2924       else if (NON_STACK_REG_P (operands[2]))
2925         {
2926           output_op_from_reg (operands[2], strcat (buf, AS1 (%z0,%1)));
2927           RET;
2928         }
2929
2930       if (! STACK_REG_P (operands[1]) || ! STACK_REG_P (operands[2]))
2931         abort ();
2932
2933       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
2934         return strcat (buf, AS2 (rp,%2,%0));
2935
2936       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
2937         return strcat (buf, AS2 (p,%1,%0));
2938
2939       if (STACK_TOP_P (operands[0]))
2940         {
2941           if (STACK_TOP_P (operands[1]))
2942             return strcat (buf, AS2C (%y2,%0));
2943           else
2944             return strcat (buf, AS2 (r,%y1,%0));
2945         }
2946       else if (STACK_TOP_P (operands[1]))
2947         return strcat (buf, AS2C (%1,%0));
2948       else
2949         return strcat (buf, AS2 (r,%2,%0));
2950
2951     default:
2952       abort ();
2953     }
2954 }
2955 \f
2956 /* Output code for INSN to convert a float to a signed int.  OPERANDS
2957    are the insn operands.  The output may be SFmode or DFmode and the
2958    input operand may be SImode or DImode.  As a special case, make sure
2959    that the 387 stack top dies if the output mode is DImode, because the
2960    hardware requires this.  */
2961
2962 char *
2963 output_fix_trunc (insn, operands)
2964      rtx insn;
2965      rtx *operands;
2966 {
2967   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
2968   rtx xops[2];
2969
2970   if (! STACK_TOP_P (operands[1]) ||
2971       (GET_MODE (operands[0]) == DImode && ! stack_top_dies))
2972     abort ();
2973
2974   xops[0] = GEN_INT (12);
2975   xops[1] = operands[4];
2976
2977   output_asm_insn (AS1 (fnstc%W2,%2), operands);
2978   output_asm_insn (AS2 (mov%L2,%2,%4), operands);
2979   output_asm_insn (AS2 (mov%B1,%0,%h1), xops);
2980   output_asm_insn (AS2 (mov%L4,%4,%3), operands);
2981   output_asm_insn (AS1 (fldc%W3,%3), operands);
2982
2983   if (NON_STACK_REG_P (operands[0]))
2984     output_to_reg (operands[0], stack_top_dies);
2985   else if (GET_CODE (operands[0]) == MEM)
2986     {
2987       if (stack_top_dies)
2988         output_asm_insn (AS1 (fistp%z0,%0), operands);
2989       else
2990         output_asm_insn (AS1 (fist%z0,%0), operands);
2991     }
2992   else
2993     abort ();
2994
2995   return AS1 (fldc%W2,%2);
2996 }
2997 \f
2998 /* Output code for INSN to compare OPERANDS.  The two operands might
2999    not have the same mode: one might be within a FLOAT or FLOAT_EXTEND
3000    expression.  If the compare is in mode CCFPEQmode, use an opcode that
3001    will not fault if a qNaN is present. */
3002
3003 char *
3004 output_float_compare (insn, operands)
3005      rtx insn;
3006      rtx *operands;
3007 {
3008   int stack_top_dies;
3009   rtx body = XVECEXP (PATTERN (insn), 0, 0);
3010   int unordered_compare = GET_MODE (SET_SRC (body)) == CCFPEQmode;
3011
3012   if (! STACK_TOP_P (operands[0]))
3013     abort ();
3014
3015   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
3016
3017   if (STACK_REG_P (operands[1])
3018       && stack_top_dies
3019       && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
3020       && REGNO (operands[1]) != FIRST_STACK_REG)
3021     {
3022       /* If both the top of the 387 stack dies, and the other operand
3023          is also a stack register that dies, then this must be a
3024          `fcompp' float compare */
3025
3026       if (unordered_compare)
3027         output_asm_insn ("fucompp", operands);
3028       else
3029         output_asm_insn ("fcompp", operands);
3030     }
3031   else
3032     {
3033       static char buf[100];
3034
3035       /* Decide if this is the integer or float compare opcode, or the
3036          unordered float compare. */
3037
3038       if (unordered_compare)
3039         strcpy (buf, "fucom");
3040       else if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_FLOAT)
3041         strcpy (buf, "fcom");
3042       else
3043         strcpy (buf, "ficom");
3044
3045       /* Modify the opcode if the 387 stack is to be popped. */
3046
3047       if (stack_top_dies)
3048         strcat (buf, "p");
3049
3050       if (NON_STACK_REG_P (operands[1]))
3051         output_op_from_reg (operands[1], strcat (buf, AS1 (%z0,%1)));
3052       else
3053         output_asm_insn (strcat (buf, AS1 (%z1,%y1)), operands);
3054     }
3055
3056   /* Now retrieve the condition code. */
3057
3058   return output_fp_cc0_set (insn);
3059 }
3060 \f
3061 /* Output opcodes to transfer the results of FP compare or test INSN
3062    from the FPU to the CPU flags.  If TARGET_IEEE_FP, ensure that if the
3063    result of the compare or test is unordered, no comparison operator
3064    succeeds except NE.  Return an output template, if any.  */
3065
3066 char *
3067 output_fp_cc0_set (insn)
3068      rtx insn;
3069 {
3070   rtx xops[3];
3071   rtx unordered_label;
3072   rtx next;
3073   enum rtx_code code;
3074
3075   xops[0] = gen_rtx (REG, HImode, 0);
3076   output_asm_insn (AS1 (fnsts%W0,%0), xops);
3077
3078   if (! TARGET_IEEE_FP)
3079     return "sahf";
3080
3081   next = next_cc0_user (insn);
3082   if (next == NULL_RTX)
3083     abort ();
3084
3085   if (GET_CODE (next) == JUMP_INSN
3086       && GET_CODE (PATTERN (next)) == SET
3087       && SET_DEST (PATTERN (next)) == pc_rtx
3088       && GET_CODE (SET_SRC (PATTERN (next))) == IF_THEN_ELSE)
3089     {
3090       code = GET_CODE (XEXP (SET_SRC (PATTERN (next)), 0));
3091     }
3092   else if (GET_CODE (PATTERN (next)) == SET)
3093     {
3094       code = GET_CODE (SET_SRC (PATTERN (next)));
3095     }
3096   else
3097     abort ();
3098
3099   xops[0] = gen_rtx (REG, QImode, 0);
3100
3101   switch (code)
3102     {
3103     case GT:
3104       xops[1] = GEN_INT (0x45);
3105       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3106       /* je label */
3107       break;
3108
3109     case LT:
3110       xops[1] = GEN_INT (0x45);
3111       xops[2] = GEN_INT (0x01);
3112       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3113       output_asm_insn (AS2 (cmp%B0,%2,%h0), xops);
3114       /* je label */
3115       break;
3116
3117     case GE:
3118       xops[1] = GEN_INT (0x05);
3119       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3120       /* je label */
3121       break;
3122
3123     case LE:
3124       xops[1] = GEN_INT (0x45);
3125       xops[2] = GEN_INT (0x40);
3126       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3127       output_asm_insn (AS1 (dec%B0,%h0), xops);
3128       output_asm_insn (AS2 (cmp%B0,%2,%h0), xops);
3129       /* jb label */
3130       break;
3131
3132     case EQ:
3133       xops[1] = GEN_INT (0x45);
3134       xops[2] = GEN_INT (0x40);
3135       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3136       output_asm_insn (AS2 (cmp%B0,%2,%h0), xops);
3137       /* je label */
3138       break;
3139
3140     case NE:
3141       xops[1] = GEN_INT (0x44);
3142       xops[2] = GEN_INT (0x40);
3143       output_asm_insn (AS2 (and%B0,%1,%h0), xops);
3144       output_asm_insn (AS2 (xor%B0,%2,%h0), xops);
3145       /* jne label */
3146       break;
3147
3148     case GTU:
3149     case LTU:
3150     case GEU:
3151     case LEU:
3152     default:
3153       abort ();
3154     }
3155   RET;
3156 }
3157 \f
3158 #define MAX_386_STACK_LOCALS 2
3159
3160 static rtx i386_stack_locals[(int) MAX_MACHINE_MODE][MAX_386_STACK_LOCALS];
3161
3162 /* Define the structure for the machine field in struct function.  */
3163 struct machine_function
3164 {
3165   rtx i386_stack_locals[(int) MAX_MACHINE_MODE][MAX_386_STACK_LOCALS];
3166 };
3167
3168 /* Functions to save and restore i386_stack_locals.
3169    These will be called, via pointer variables,
3170    from push_function_context and pop_function_context.  */
3171
3172 void
3173 save_386_machine_status (p)
3174      struct function *p;
3175 {
3176   p->machine = (struct machine_function *) xmalloc (sizeof i386_stack_locals);
3177   bcopy ((char *) i386_stack_locals, (char *) p->machine->i386_stack_locals,
3178          sizeof i386_stack_locals);
3179 }
3180
3181 void
3182 restore_386_machine_status (p)
3183      struct function *p;
3184 {
3185   bcopy ((char *) p->machine->i386_stack_locals, (char *) i386_stack_locals,
3186          sizeof i386_stack_locals);
3187   free (p->machine);
3188 }
3189
3190 /* Clear stack slot assignments remembered from previous functions.
3191    This is called from INIT_EXPANDERS once before RTL is emitted for each
3192    function.  */
3193
3194 void
3195 clear_386_stack_locals ()
3196 {
3197   enum machine_mode mode;
3198   int n;
3199
3200   for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
3201        mode = (enum machine_mode) ((int) mode + 1))
3202     for (n = 0; n < MAX_386_STACK_LOCALS; n++)
3203       i386_stack_locals[(int) mode][n] = NULL_RTX;
3204
3205   /* Arrange to save and restore i386_stack_locals around nested functions.  */
3206   save_machine_status = save_386_machine_status;
3207   restore_machine_status = restore_386_machine_status;
3208 }
3209
3210 /* Return a MEM corresponding to a stack slot with mode MODE.
3211    Allocate a new slot if necessary.
3212
3213    The RTL for a function can have several slots available: N is
3214    which slot to use.  */
3215
3216 rtx
3217 assign_386_stack_local (mode, n)
3218      enum machine_mode mode;
3219      int n;
3220 {
3221   if (n < 0 || n >= MAX_386_STACK_LOCALS)
3222     abort ();
3223
3224   if (i386_stack_locals[(int) mode][n] == NULL_RTX)
3225     i386_stack_locals[(int) mode][n]
3226       = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
3227
3228   return i386_stack_locals[(int) mode][n];
3229 }