OSDN Git Service

3c382d96ca52c4f6d819d45329bf6d5008f08a0d
[pf3gnuchains/gcc-fork.git] / gcc / config / xtensa / xtensa.c
1 /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
2    Copyright 2001,2002,2003 Free Software Foundation, Inc.
3    Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "insn-attr.h"
35 #include "insn-codes.h"
36 #include "recog.h"
37 #include "output.h"
38 #include "tree.h"
39 #include "expr.h"
40 #include "flags.h"
41 #include "reload.h"
42 #include "tm_p.h"
43 #include "function.h"
44 #include "toplev.h"
45 #include "optabs.h"
46 #include "libfuncs.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "langhooks.h"
51
52 /* Enumeration for all of the relational tests, so that we can build
53    arrays indexed by the test type, and not worry about the order
54    of EQ, NE, etc.  */
55
56 enum internal_test {
57     ITEST_EQ,
58     ITEST_NE,
59     ITEST_GT,
60     ITEST_GE,
61     ITEST_LT,
62     ITEST_LE,
63     ITEST_GTU,
64     ITEST_GEU,
65     ITEST_LTU,
66     ITEST_LEU,
67     ITEST_MAX
68   };
69
70 /* Cached operands, and operator to compare for use in set/branch on
71    condition codes.  */
72 rtx branch_cmp[2];
73
74 /* what type of branch to use */
75 enum cmp_type branch_type;
76
77 /* Array giving truth value on whether or not a given hard register
78    can support a given mode.  */
79 char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
80
81 /* Current frame size calculated by compute_frame_size.  */
82 unsigned xtensa_current_frame_size;
83
84 /* Tables of ld/st opcode names for block moves */
85 const char *xtensa_ld_opcodes[(int) MAX_MACHINE_MODE];
86 const char *xtensa_st_opcodes[(int) MAX_MACHINE_MODE];
87 #define LARGEST_MOVE_RATIO 15
88
89 /* Define the structure for the machine field in struct function.  */
90 struct machine_function GTY(())
91 {
92   int accesses_prev_frame;
93   bool incoming_a7_copied;
94 };
95
96 /* Vector, indexed by hard register number, which contains 1 for a
97    register that is allowable in a candidate for leaf function
98    treatment.  */
99
100 const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
101 {
102   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103   1, 1, 1,
104   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105   1
106 };
107
108 /* Map hard register number to register class */
109 const enum reg_class xtensa_regno_to_class[FIRST_PSEUDO_REGISTER] =
110 {
111   RL_REGS,      SP_REG,         RL_REGS,        RL_REGS,
112   RL_REGS,      RL_REGS,        RL_REGS,        GR_REGS,
113   RL_REGS,      RL_REGS,        RL_REGS,        RL_REGS,
114   RL_REGS,      RL_REGS,        RL_REGS,        RL_REGS,
115   AR_REGS,      AR_REGS,        BR_REGS,
116   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
117   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
118   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
119   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
120   ACC_REG,
121 };
122
123 /* Map register constraint character to register class.  */
124 enum reg_class xtensa_char_to_class[256] =
125 {
126   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
127   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
128   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
129   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
130   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
131   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
132   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
133   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
134   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
135   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
136   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
137   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
138   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
139   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
140   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
141   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
142   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
143   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
144   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
145   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
146   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
147   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
148   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
149   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
150   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
151   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
152   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
153   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
154   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
155   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
156   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
157   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
158   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
159   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
160   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
161   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
162   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
163   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
164   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
165   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
166   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
167   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
168   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
169   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
170   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
171   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
172   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
173   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
174   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
175   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
176   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
177   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
178   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
179   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
180   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
181   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
182   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
183   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
184   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
185   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
186   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
187   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
188   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
189   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
190 };
191
192 static int b4const_or_zero PARAMS ((int));
193 static enum internal_test map_test_to_internal_test PARAMS ((enum rtx_code));
194 static rtx gen_int_relational PARAMS ((enum rtx_code, rtx, rtx, int *));
195 static rtx gen_float_relational PARAMS ((enum rtx_code, rtx, rtx));
196 static rtx gen_conditional_move PARAMS ((rtx));
197 static rtx fixup_subreg_mem PARAMS ((rtx x));
198 static enum machine_mode xtensa_find_mode_for_size PARAMS ((unsigned));
199 static struct machine_function * xtensa_init_machine_status PARAMS ((void));
200 static void printx PARAMS ((FILE *, signed int));
201 static void xtensa_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
202 static unsigned int xtensa_multibss_section_type_flags
203   PARAMS ((tree, const char *, int));
204 static void xtensa_select_rtx_section
205   PARAMS ((enum machine_mode, rtx, unsigned HOST_WIDE_INT));
206 static bool xtensa_rtx_costs PARAMS ((rtx, int, int, int *));
207
208 static int current_function_arg_words;
209 static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
210   REG_ALLOC_ORDER;
211 \f
212
213 /* This macro generates the assembly code for function exit,
214    on machines that need it.  If FUNCTION_EPILOGUE is not defined
215    then individual return instructions are generated for each
216    return statement.  Args are same as for FUNCTION_PROLOGUE.  */
217
218 #undef TARGET_ASM_FUNCTION_EPILOGUE
219 #define TARGET_ASM_FUNCTION_EPILOGUE xtensa_function_epilogue
220
221 /* These hooks specify assembly directives for creating certain kinds
222    of integer object.  */
223
224 #undef TARGET_ASM_ALIGNED_SI_OP
225 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
226
227 #undef TARGET_ASM_SELECT_RTX_SECTION
228 #define TARGET_ASM_SELECT_RTX_SECTION  xtensa_select_rtx_section
229
230 #undef TARGET_RTX_COSTS
231 #define TARGET_RTX_COSTS xtensa_rtx_costs
232 #undef TARGET_ADDRESS_COST
233 #define TARGET_ADDRESS_COST hook_int_rtx_0
234
235 struct gcc_target targetm = TARGET_INITIALIZER;
236 \f
237
238 /*
239  * Functions to test Xtensa immediate operand validity.
240  */
241
242 int
243 xtensa_b4constu (v)
244      int v;
245 {
246   switch (v)
247     {
248     case 32768:
249     case 65536:
250     case 2:
251     case 3:
252     case 4:
253     case 5:
254     case 6:
255     case 7:
256     case 8:
257     case 10:
258     case 12:
259     case 16:
260     case 32:
261     case 64:
262     case 128:
263     case 256:
264       return 1;
265     }
266   return 0;
267 }
268
269 int
270 xtensa_simm8x256 (v)
271      int v;
272 {
273   return (v & 255) == 0 && (v >= -32768 && v <= 32512);
274 }
275
276 int
277 xtensa_ai4const (v)
278      int v;
279 {
280   return (v == -1 || (v >= 1 && v <= 15));
281 }
282
283 int
284 xtensa_simm7 (v)
285      int v;
286 {
287   return v >= -32 && v <= 95;
288 }
289
290 int
291 xtensa_b4const (v)
292      int v;
293 {
294   switch (v)
295     {
296     case -1:
297     case 1:
298     case 2:
299     case 3:
300     case 4:
301     case 5:
302     case 6:
303     case 7:
304     case 8:
305     case 10:
306     case 12:
307     case 16:
308     case 32:
309     case 64:
310     case 128:
311     case 256:
312       return 1;
313     }
314   return 0;
315 }
316
317 int
318 xtensa_simm8 (v)
319      int v;
320 {
321   return v >= -128 && v <= 127;
322 }
323
324 int
325 xtensa_tp7 (v)
326      int v;
327 {
328   return (v >= 7 && v <= 22);
329 }
330
331 int
332 xtensa_lsi4x4 (v)
333      int v;
334 {
335   return (v & 3) == 0 && (v >= 0 && v <= 60);
336 }
337
338 int
339 xtensa_simm12b (v)
340      int v;
341 {
342   return v >= -2048 && v <= 2047;
343 }
344
345 int
346 xtensa_uimm8 (v)
347      int v;
348 {
349   return v >= 0 && v <= 255;
350 }
351
352 int
353 xtensa_uimm8x2 (v)
354      int v;
355 {
356   return (v & 1) == 0 && (v >= 0 && v <= 510);
357 }
358
359 int
360 xtensa_uimm8x4 (v)
361      int v;
362 {
363   return (v & 3) == 0 && (v >= 0 && v <= 1020);
364 }
365
366
367 /* This is just like the standard true_regnum() function except that it
368    works even when reg_renumber is not initialized.  */
369
370 int
371 xt_true_regnum (x)
372      rtx x;
373 {
374   if (GET_CODE (x) == REG)
375     {
376       if (reg_renumber
377           && REGNO (x) >= FIRST_PSEUDO_REGISTER
378           && reg_renumber[REGNO (x)] >= 0)
379         return reg_renumber[REGNO (x)];
380       return REGNO (x);
381     }
382   if (GET_CODE (x) == SUBREG)
383     {
384       int base = xt_true_regnum (SUBREG_REG (x));
385       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
386         return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
387                                            GET_MODE (SUBREG_REG (x)),
388                                            SUBREG_BYTE (x), GET_MODE (x));
389     }
390   return -1;
391 }
392
393
394 int
395 add_operand (op, mode)
396      rtx op;
397      enum machine_mode mode;
398 {
399   if (GET_CODE (op) == CONST_INT)
400     return (xtensa_simm8 (INTVAL (op)) || xtensa_simm8x256 (INTVAL (op)));
401
402   return register_operand (op, mode);
403 }
404
405
406 int
407 arith_operand (op, mode)
408      rtx op;
409      enum machine_mode mode;
410 {
411   if (GET_CODE (op) == CONST_INT)
412     return xtensa_simm8 (INTVAL (op));
413
414   return register_operand (op, mode);
415 }
416
417
418 int
419 nonimmed_operand (op, mode)
420      rtx op;
421      enum machine_mode mode;
422 {
423   /* We cannot use the standard nonimmediate_operand() predicate because
424      it includes constant pool memory operands.  */
425
426   if (memory_operand (op, mode))
427     return !constantpool_address_p (XEXP (op, 0));
428
429   return register_operand (op, mode);
430 }
431
432
433 int
434 mem_operand (op, mode)
435      rtx op;
436      enum machine_mode mode;
437 {
438   /* We cannot use the standard memory_operand() predicate because
439      it includes constant pool memory operands.  */
440
441   if (memory_operand (op, mode))
442     return !constantpool_address_p (XEXP (op, 0));
443
444   return FALSE;
445 }
446
447
448 int
449 xtensa_valid_move (mode, operands)
450      enum machine_mode mode;
451      rtx *operands;
452 {
453   /* Either the destination or source must be a register, and the
454      MAC16 accumulator doesn't count.  */
455
456   if (register_operand (operands[0], mode))
457     {
458       int dst_regnum = xt_true_regnum (operands[0]);
459
460       /* The stack pointer can only be assigned with a MOVSP opcode.  */
461       if (dst_regnum == STACK_POINTER_REGNUM)
462         return (mode == SImode
463                 && register_operand (operands[1], mode)
464                 && !ACC_REG_P (xt_true_regnum (operands[1])));
465
466       if (!ACC_REG_P (dst_regnum))
467         return true;
468     }
469   if (register_operand (operands[1], mode))
470     {
471       int src_regnum = xt_true_regnum (operands[1]);
472       if (!ACC_REG_P (src_regnum))
473         return true;
474     }
475   return FALSE;
476 }
477
478
479 int
480 mask_operand (op, mode)
481      rtx op;
482      enum machine_mode mode;
483 {
484   if (GET_CODE (op) == CONST_INT)
485     return xtensa_mask_immediate (INTVAL (op));
486
487   return register_operand (op, mode);
488 }
489
490
491 int
492 extui_fldsz_operand (op, mode)
493      rtx op;
494      enum machine_mode mode ATTRIBUTE_UNUSED;
495 {
496   return ((GET_CODE (op) == CONST_INT)
497           && xtensa_mask_immediate ((1 << INTVAL (op)) - 1));
498 }
499
500
501 int
502 sext_operand (op, mode)
503      rtx op;
504      enum machine_mode mode;
505 {
506   if (TARGET_SEXT)
507     return nonimmed_operand (op, mode);
508   return mem_operand (op, mode);
509 }
510
511
512 int
513 sext_fldsz_operand (op, mode)
514      rtx op;
515      enum machine_mode mode ATTRIBUTE_UNUSED;
516 {
517   return ((GET_CODE (op) == CONST_INT) && xtensa_tp7 (INTVAL (op) - 1));
518 }
519
520
521 int
522 lsbitnum_operand (op, mode)
523      rtx op;
524      enum machine_mode mode ATTRIBUTE_UNUSED;
525 {
526   if (GET_CODE (op) == CONST_INT)
527     {
528       return (BITS_BIG_ENDIAN
529               ? (INTVAL (op) == BITS_PER_WORD-1)
530               : (INTVAL (op) == 0));
531     }
532   return FALSE;
533 }
534
535
536 static int
537 b4const_or_zero (v)
538      int v;
539 {
540   if (v == 0)
541     return TRUE;
542   return xtensa_b4const (v);
543 }
544
545
546 int
547 branch_operand (op, mode)
548      rtx op;
549      enum machine_mode mode;
550 {
551   if (GET_CODE (op) == CONST_INT)
552     return b4const_or_zero (INTVAL (op));
553
554   return register_operand (op, mode);
555 }
556
557
558 int
559 ubranch_operand (op, mode)
560      rtx op;
561      enum machine_mode mode;
562 {
563   if (GET_CODE (op) == CONST_INT)
564     return xtensa_b4constu (INTVAL (op));
565
566   return register_operand (op, mode);
567 }
568
569
570 int
571 call_insn_operand (op, mode)
572      rtx op;
573      enum machine_mode mode ATTRIBUTE_UNUSED;
574 {
575   if ((GET_CODE (op) == REG)
576       && (op != arg_pointer_rtx)
577       && ((REGNO (op) < FRAME_POINTER_REGNUM)
578           || (REGNO (op) > LAST_VIRTUAL_REGISTER)))
579     return TRUE;
580
581   if (CONSTANT_ADDRESS_P (op))
582     {
583       /* Direct calls only allowed to static functions with PIC.  */
584       return (!flag_pic
585               || (GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op)));
586     }
587
588   return FALSE;
589 }
590
591
592 int
593 move_operand (op, mode)
594      rtx op;
595      enum machine_mode mode;
596 {
597   if (register_operand (op, mode)
598       || memory_operand (op, mode))
599     return TRUE;
600
601   switch (mode)
602     {
603     case DFmode:
604     case SFmode:
605       return TARGET_CONST16 && CONSTANT_P (op);
606
607     case DImode:
608     case SImode:
609       if (TARGET_CONST16)
610         return CONSTANT_P (op);
611       /* fall through */
612
613     case HImode:
614     case QImode:
615       /* Accept CONSTANT_P_RTX, since it will be gone by CSE1 and
616          result in 0/1.  */
617       if (GET_CODE (op) == CONSTANT_P_RTX)
618         return TRUE;
619
620       if (GET_CODE (op) == CONST_INT && xtensa_simm12b (INTVAL (op)))
621         return TRUE;
622       break;
623
624     default:
625       break;
626     }
627
628   return FALSE;
629 }
630
631
632 int
633 smalloffset_mem_p (op)
634      rtx op;
635 {
636   if (GET_CODE (op) == MEM)
637     {
638       rtx addr = XEXP (op, 0);
639       if (GET_CODE (addr) == REG)
640         return REG_OK_FOR_BASE_P (addr);
641       if (GET_CODE (addr) == PLUS)
642         {
643           rtx offset = XEXP (addr, 0);
644           if (GET_CODE (offset) != CONST_INT)
645             offset = XEXP (addr, 1);
646           if (GET_CODE (offset) != CONST_INT)
647             return FALSE;
648           return xtensa_lsi4x4 (INTVAL (offset));
649         }
650     }
651   return FALSE;
652 }
653
654
655 int
656 constantpool_address_p (addr)
657      rtx addr;
658 {
659   rtx sym = addr;
660
661   if (GET_CODE (addr) == CONST)
662     {
663       rtx offset;
664
665       /* only handle (PLUS (SYM, OFFSET)) form */
666       addr = XEXP (addr, 0);
667       if (GET_CODE (addr) != PLUS)
668         return FALSE;
669
670       /* make sure the address is word aligned */
671       offset = XEXP (addr, 1);
672       if ((GET_CODE (offset) != CONST_INT)
673           || ((INTVAL (offset) & 3) != 0))
674         return FALSE;
675
676       sym = XEXP (addr, 0);
677     }
678
679   if ((GET_CODE (sym) == SYMBOL_REF)
680       && CONSTANT_POOL_ADDRESS_P (sym))
681     return TRUE;
682   return FALSE;
683 }
684
685
686 int
687 constantpool_mem_p (op)
688      rtx op;
689 {
690   if (GET_CODE (op) == MEM)
691     return constantpool_address_p (XEXP (op, 0));
692   return FALSE;
693 }
694
695
696 /* Accept the floating point constant 1 in the appropriate mode.  */
697
698 int
699 const_float_1_operand (op, mode)
700      rtx op;
701      enum machine_mode mode;
702 {
703   REAL_VALUE_TYPE d;
704   static REAL_VALUE_TYPE onedf;
705   static REAL_VALUE_TYPE onesf;
706   static int one_initialized;
707
708   if ((GET_CODE (op) != CONST_DOUBLE)
709       || (mode != GET_MODE (op))
710       || (mode != DFmode && mode != SFmode))
711     return FALSE;
712
713   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
714
715   if (! one_initialized)
716     {
717       onedf = REAL_VALUE_ATOF ("1.0", DFmode);
718       onesf = REAL_VALUE_ATOF ("1.0", SFmode);
719       one_initialized = TRUE;
720     }
721
722   if (mode == DFmode)
723     return REAL_VALUES_EQUAL (d, onedf);
724   else
725     return REAL_VALUES_EQUAL (d, onesf);
726 }
727
728
729 int
730 fpmem_offset_operand (op, mode)
731      rtx op;
732      enum machine_mode mode ATTRIBUTE_UNUSED;
733 {
734   if (GET_CODE (op) == CONST_INT)
735     return xtensa_mem_offset (INTVAL (op), SFmode);
736   return 0;
737 }
738
739
740 void
741 xtensa_extend_reg (dst, src)
742      rtx dst;
743      rtx src;
744 {
745   rtx temp = gen_reg_rtx (SImode);
746   rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
747
748   /* generate paradoxical subregs as needed so that the modes match */
749   src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
750   dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
751
752   emit_insn (gen_ashlsi3 (temp, src, shift));
753   emit_insn (gen_ashrsi3 (dst, temp, shift));
754 }
755
756
757 int
758 branch_operator (x, mode)
759      rtx x;
760      enum machine_mode mode;
761 {
762   if (GET_MODE (x) != mode)
763     return FALSE;
764
765   switch (GET_CODE (x))
766     {
767     case EQ:
768     case NE:
769     case LT:
770     case GE:
771       return TRUE;
772     default:
773       break;
774     }
775   return FALSE;
776 }
777
778
779 int
780 ubranch_operator (x, mode)
781      rtx x;
782      enum machine_mode mode;
783 {
784   if (GET_MODE (x) != mode)
785     return FALSE;
786
787   switch (GET_CODE (x))
788     {
789     case LTU:
790     case GEU:
791       return TRUE;
792     default:
793       break;
794     }
795   return FALSE;
796 }
797
798
799 int
800 boolean_operator (x, mode)
801      rtx x;
802      enum machine_mode mode;
803 {
804   if (GET_MODE (x) != mode)
805     return FALSE;
806
807   switch (GET_CODE (x))
808     {
809     case EQ:
810     case NE:
811       return TRUE;
812     default:
813       break;
814     }
815   return FALSE;
816 }
817
818
819 int
820 xtensa_mask_immediate (v)
821      int v;
822 {
823 #define MAX_MASK_SIZE 16
824   int mask_size;
825
826   for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
827     {
828       if ((v & 1) == 0)
829         return FALSE;
830       v = v >> 1;
831       if (v == 0)
832         return TRUE;
833     }
834
835   return FALSE;
836 }
837
838
839 int
840 xtensa_mem_offset (v, mode)
841      unsigned v;
842      enum machine_mode mode;
843 {
844   switch (mode)
845     {
846     case BLKmode:
847       /* Handle the worst case for block moves.  See xtensa_expand_block_move
848          where we emit an optimized block move operation if the block can be
849          moved in < "move_ratio" pieces.  The worst case is when the block is
850          aligned but has a size of (3 mod 4) (does this happen?) so that the
851          last piece requires a byte load/store.  */
852       return (xtensa_uimm8 (v)
853               && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
854
855     case QImode:
856       return xtensa_uimm8 (v);
857
858     case HImode:
859       return xtensa_uimm8x2 (v);
860
861     case DFmode:
862       return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
863
864     default:
865       break;
866     }
867
868   return xtensa_uimm8x4 (v);
869 }
870
871
872 /* Make normal rtx_code into something we can index from an array */
873
874 static enum internal_test
875 map_test_to_internal_test (test_code)
876      enum rtx_code test_code;
877 {
878   enum internal_test test = ITEST_MAX;
879
880   switch (test_code)
881     {
882     default:                    break;
883     case EQ:  test = ITEST_EQ;  break;
884     case NE:  test = ITEST_NE;  break;
885     case GT:  test = ITEST_GT;  break;
886     case GE:  test = ITEST_GE;  break;
887     case LT:  test = ITEST_LT;  break;
888     case LE:  test = ITEST_LE;  break;
889     case GTU: test = ITEST_GTU; break;
890     case GEU: test = ITEST_GEU; break;
891     case LTU: test = ITEST_LTU; break;
892     case LEU: test = ITEST_LEU; break;
893     }
894
895   return test;
896 }
897
898
899 /* Generate the code to compare two integer values.  The return value is
900    the comparison expression.  */
901
902 static rtx
903 gen_int_relational (test_code, cmp0, cmp1, p_invert)
904      enum rtx_code test_code;   /* relational test (EQ, etc) */
905      rtx cmp0;                  /* first operand to compare */
906      rtx cmp1;                  /* second operand to compare */
907      int *p_invert;             /* whether branch needs to reverse its test */
908 {
909   struct cmp_info {
910     enum rtx_code test_code;    /* test code to use in insn */
911     int (*const_range_p) PARAMS ((int)); /* predicate function to check range */
912     int const_add;              /* constant to add (convert LE -> LT) */
913     int reverse_regs;           /* reverse registers in test */
914     int invert_const;           /* != 0 if invert value if cmp1 is constant */
915     int invert_reg;             /* != 0 if invert value if cmp1 is register */
916     int unsignedp;              /* != 0 for unsigned comparisons.  */
917   };
918
919   static struct cmp_info info[ (int)ITEST_MAX ] = {
920
921     { EQ,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* EQ  */
922     { NE,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* NE  */
923
924     { LT,       b4const_or_zero,        1, 1, 1, 0, 0 },        /* GT  */
925     { GE,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* GE  */
926     { LT,       b4const_or_zero,        0, 0, 0, 0, 0 },        /* LT  */
927     { GE,       b4const_or_zero,        1, 1, 1, 0, 0 },        /* LE  */
928
929     { LTU,      xtensa_b4constu,        1, 1, 1, 0, 1 },        /* GTU */
930     { GEU,      xtensa_b4constu,        0, 0, 0, 0, 1 },        /* GEU */
931     { LTU,      xtensa_b4constu,        0, 0, 0, 0, 1 },        /* LTU */
932     { GEU,      xtensa_b4constu,        1, 1, 1, 0, 1 },        /* LEU */
933   };
934
935   enum internal_test test;
936   enum machine_mode mode;
937   struct cmp_info *p_info;
938
939   test = map_test_to_internal_test (test_code);
940   if (test == ITEST_MAX)
941     abort ();
942
943   p_info = &info[ (int)test ];
944
945   mode = GET_MODE (cmp0);
946   if (mode == VOIDmode)
947     mode = GET_MODE (cmp1);
948
949   /* Make sure we can handle any constants given to us.  */
950   if (GET_CODE (cmp1) == CONST_INT)
951     {
952       HOST_WIDE_INT value = INTVAL (cmp1);
953       unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
954
955       /* if the immediate overflows or does not fit in the immediate field,
956          spill it to a register */
957
958       if ((p_info->unsignedp ?
959            (uvalue + p_info->const_add > uvalue) :
960            (value + p_info->const_add > value)) != (p_info->const_add > 0))
961         {
962           cmp1 = force_reg (mode, cmp1);
963         }
964       else if (!(p_info->const_range_p) (value + p_info->const_add))
965         {
966           cmp1 = force_reg (mode, cmp1);
967         }
968     }
969   else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
970     {
971       cmp1 = force_reg (mode, cmp1);
972     }
973
974   /* See if we need to invert the result.  */
975   *p_invert = ((GET_CODE (cmp1) == CONST_INT)
976                ? p_info->invert_const
977                : p_info->invert_reg);
978
979   /* Comparison to constants, may involve adding 1 to change a LT into LE.
980      Comparison between two registers, may involve switching operands.  */
981   if (GET_CODE (cmp1) == CONST_INT)
982     {
983       if (p_info->const_add != 0)
984         cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
985
986     }
987   else if (p_info->reverse_regs)
988     {
989       rtx temp = cmp0;
990       cmp0 = cmp1;
991       cmp1 = temp;
992     }
993
994   return gen_rtx (p_info->test_code, VOIDmode, cmp0, cmp1);
995 }
996
997
998 /* Generate the code to compare two float values.  The return value is
999    the comparison expression.  */
1000
1001 static rtx
1002 gen_float_relational (test_code, cmp0, cmp1)
1003      enum rtx_code test_code;   /* relational test (EQ, etc) */
1004      rtx cmp0;                  /* first operand to compare */
1005      rtx cmp1;                  /* second operand to compare */
1006 {
1007   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx));
1008   rtx brtmp;
1009   int reverse_regs, invert;
1010
1011   switch (test_code)
1012     {
1013     case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
1014     case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
1015     case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
1016     case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
1017     case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
1018     case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
1019     default:
1020       fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1021       reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
1022     }
1023
1024   if (reverse_regs)
1025     {
1026       rtx temp = cmp0;
1027       cmp0 = cmp1;
1028       cmp1 = temp;
1029     }
1030
1031   brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
1032   emit_insn (gen_fn (brtmp, cmp0, cmp1));
1033
1034   return gen_rtx (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
1035 }
1036
1037
1038 void
1039 xtensa_expand_conditional_branch (operands, test_code)
1040      rtx *operands;
1041      enum rtx_code test_code;
1042 {
1043   enum cmp_type type = branch_type;
1044   rtx cmp0 = branch_cmp[0];
1045   rtx cmp1 = branch_cmp[1];
1046   rtx cmp;
1047   int invert;
1048   rtx label1, label2;
1049
1050   switch (type)
1051     {
1052     case CMP_DF:
1053     default:
1054       fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1055
1056     case CMP_SI:
1057       invert = FALSE;
1058       cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
1059       break;
1060
1061     case CMP_SF:
1062       if (!TARGET_HARD_FLOAT)
1063         fatal_insn ("bad test", gen_rtx (test_code, VOIDmode, cmp0, cmp1));
1064       invert = FALSE;
1065       cmp = gen_float_relational (test_code, cmp0, cmp1);
1066       break;
1067     }
1068
1069   /* Generate the branch.  */
1070
1071   label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
1072   label2 = pc_rtx;
1073
1074   if (invert)
1075     {
1076       label2 = label1;
1077       label1 = pc_rtx;
1078     }
1079
1080   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
1081                                gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
1082                                                      label1,
1083                                                      label2)));
1084 }
1085
1086
1087 static rtx
1088 gen_conditional_move (cmp)
1089      rtx cmp;
1090 {
1091   enum rtx_code code = GET_CODE (cmp);
1092   rtx op0 = branch_cmp[0];
1093   rtx op1 = branch_cmp[1];
1094
1095   if (branch_type == CMP_SI)
1096     {
1097       /* Jump optimization calls get_condition() which canonicalizes
1098          comparisons like (GE x <const>) to (GT x <const-1>).
1099          Transform those comparisons back to GE, since that is the
1100          comparison supported in Xtensa.  We shouldn't have to
1101          transform <LE x const> comparisons, because neither
1102          xtensa_expand_conditional_branch() nor get_condition() will
1103          produce them.  */
1104
1105       if ((code == GT) && (op1 == constm1_rtx))
1106         {
1107           code = GE;
1108           op1 = const0_rtx;
1109         }
1110       cmp = gen_rtx (code, VOIDmode, cc0_rtx, const0_rtx);
1111
1112       if (boolean_operator (cmp, VOIDmode))
1113         {
1114           /* swap the operands to make const0 second */
1115           if (op0 == const0_rtx)
1116             {
1117               op0 = op1;
1118               op1 = const0_rtx;
1119             }
1120
1121           /* if not comparing against zero, emit a comparison (subtract) */
1122           if (op1 != const0_rtx)
1123             {
1124               op0 = expand_binop (SImode, sub_optab, op0, op1,
1125                                   0, 0, OPTAB_LIB_WIDEN);
1126               op1 = const0_rtx;
1127             }
1128         }
1129       else if (branch_operator (cmp, VOIDmode))
1130         {
1131           /* swap the operands to make const0 second */
1132           if (op0 == const0_rtx)
1133             {
1134               op0 = op1;
1135               op1 = const0_rtx;
1136
1137               switch (code)
1138                 {
1139                 case LT: code = GE; break;
1140                 case GE: code = LT; break;
1141                 default: abort ();
1142                 }
1143             }
1144
1145           if (op1 != const0_rtx)
1146             return 0;
1147         }
1148       else
1149         return 0;
1150
1151       return gen_rtx (code, VOIDmode, op0, op1);
1152     }
1153
1154   if (TARGET_HARD_FLOAT && (branch_type == CMP_SF))
1155     return gen_float_relational (code, op0, op1);
1156
1157   return 0;
1158 }
1159
1160
1161 int
1162 xtensa_expand_conditional_move (operands, isflt)
1163     rtx *operands;
1164     int isflt;
1165 {
1166   rtx cmp;
1167   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx, rtx, rtx));
1168
1169   if (!(cmp = gen_conditional_move (operands[1])))
1170     return 0;
1171
1172   if (isflt)
1173     gen_fn = (branch_type == CMP_SI
1174               ? gen_movsfcc_internal0
1175               : gen_movsfcc_internal1);
1176   else
1177     gen_fn = (branch_type == CMP_SI
1178               ? gen_movsicc_internal0
1179               : gen_movsicc_internal1);
1180
1181   emit_insn (gen_fn (operands[0], XEXP (cmp, 0),
1182                      operands[2], operands[3], cmp));
1183   return 1;
1184 }
1185
1186
1187 int
1188 xtensa_expand_scc (operands)
1189      rtx *operands;
1190 {
1191   rtx dest = operands[0];
1192   rtx cmp = operands[1];
1193   rtx one_tmp, zero_tmp;
1194   rtx (*gen_fn) PARAMS ((rtx, rtx, rtx, rtx, rtx));
1195
1196   if (!(cmp = gen_conditional_move (cmp)))
1197     return 0;
1198
1199   one_tmp = gen_reg_rtx (SImode);
1200   zero_tmp = gen_reg_rtx (SImode);
1201   emit_insn (gen_movsi (one_tmp, const_true_rtx));
1202   emit_insn (gen_movsi (zero_tmp, const0_rtx));
1203
1204   gen_fn = (branch_type == CMP_SI
1205             ? gen_movsicc_internal0
1206             : gen_movsicc_internal1);
1207   emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
1208   return 1;
1209 }
1210
1211
1212 /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1].  MODE is
1213    for the output, i.e., the input operands are twice as big as MODE.  */
1214
1215 void
1216 xtensa_split_operand_pair (operands, mode)
1217      rtx operands[4];
1218      enum machine_mode mode;
1219 {
1220   switch (GET_CODE (operands[1]))
1221     {
1222     case REG:
1223       operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
1224       operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
1225       break;
1226
1227     case MEM:
1228       operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
1229       operands[2] = adjust_address (operands[1], mode, 0);
1230       break;
1231
1232     case CONST_INT:
1233     case CONST_DOUBLE:
1234       split_double (operands[1], &operands[2], &operands[3]);
1235       break;
1236
1237     default:
1238       abort ();
1239     }
1240
1241   switch (GET_CODE (operands[0]))
1242     {
1243     case REG:
1244       operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
1245       operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
1246       break;
1247
1248     case MEM:
1249       operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
1250       operands[0] = adjust_address (operands[0], mode, 0);
1251       break;
1252
1253     default:
1254       abort ();
1255     }
1256 }
1257
1258
1259 /* Emit insns to move operands[1] into operands[0].
1260    Return 1 if we have written out everything that needs to be done to
1261    do the move.  Otherwise, return 0 and the caller will emit the move
1262    normally.  */
1263
1264 int
1265 xtensa_emit_move_sequence (operands, mode)
1266      rtx *operands;
1267      enum machine_mode mode;
1268 {
1269   if (CONSTANT_P (operands[1])
1270       && GET_CODE (operands[1]) != CONSTANT_P_RTX
1271       && (GET_CODE (operands[1]) != CONST_INT
1272           || !xtensa_simm12b (INTVAL (operands[1]))))
1273     {
1274       if (!TARGET_CONST16)
1275         operands[1] = force_const_mem (SImode, operands[1]);
1276
1277       /* PC-relative loads are always SImode, and CONST16 is only
1278          supported in the movsi pattern, so add a SUBREG for any other
1279          (smaller) mode.  */
1280
1281       if (mode != SImode)
1282         {
1283           if (register_operand (operands[0], mode))
1284             {
1285               operands[0] = simplify_gen_subreg (SImode, operands[0], mode, 0);
1286               emit_move_insn (operands[0], operands[1]);
1287               return 1;
1288             }
1289           else
1290             {
1291               operands[1] = force_reg (SImode, operands[1]);
1292               operands[1] = gen_lowpart_SUBREG (mode, operands[1]);
1293             }
1294         }
1295     }
1296
1297   if (!(reload_in_progress | reload_completed))
1298     {
1299       if (!xtensa_valid_move (mode, operands))
1300         operands[1] = force_reg (mode, operands[1]);
1301
1302       if (xtensa_copy_incoming_a7 (operands, mode))
1303         return 1;
1304     }
1305
1306   /* During reload we don't want to emit (subreg:X (mem:Y)) since that
1307      instruction won't be recognized after reload, so we remove the
1308      subreg and adjust mem accordingly.  */
1309   if (reload_in_progress)
1310     {
1311       operands[0] = fixup_subreg_mem (operands[0]);
1312       operands[1] = fixup_subreg_mem (operands[1]);
1313     }
1314   return 0;
1315 }
1316
1317
1318 static rtx
1319 fixup_subreg_mem (x)
1320      rtx x;
1321 {
1322   if (GET_CODE (x) == SUBREG
1323       && GET_CODE (SUBREG_REG (x)) == REG
1324       && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1325     {
1326       rtx temp =
1327         gen_rtx_SUBREG (GET_MODE (x),
1328                         reg_equiv_mem [REGNO (SUBREG_REG (x))],
1329                         SUBREG_BYTE (x));
1330       x = alter_subreg (&temp);
1331     }
1332   return x;
1333 }
1334
1335
1336 /* Check if this move is copying an incoming argument in a7.  If so,
1337    emit the move, followed by the special "set_frame_ptr"
1338    unspec_volatile insn, at the very beginning of the function.  This
1339    is necessary because the register allocator will ignore conflicts
1340    with a7 and may assign some other pseudo to a7.  If that pseudo was
1341    assigned prior to this move, it would clobber the incoming argument
1342    in a7.  By copying the argument out of a7 as the very first thing,
1343    and then immediately following that with an unspec_volatile to keep
1344    the scheduler away, we should avoid any problems.  */
1345
1346 bool
1347 xtensa_copy_incoming_a7 (operands, mode)
1348      rtx *operands;
1349      enum machine_mode mode;
1350 {
1351   if (a7_overlap_mentioned_p (operands[1])
1352       && !cfun->machine->incoming_a7_copied)
1353     {
1354       rtx mov;
1355       switch (mode)
1356         {
1357         case DFmode:
1358           mov = gen_movdf_internal (operands[0], operands[1]);
1359           break;
1360         case SFmode:
1361           mov = gen_movsf_internal (operands[0], operands[1]);
1362           break;
1363         case DImode:
1364           mov = gen_movdi_internal (operands[0], operands[1]);
1365           break;
1366         case SImode:
1367           mov = gen_movsi_internal (operands[0], operands[1]);
1368           break;
1369         case HImode:
1370           mov = gen_movhi_internal (operands[0], operands[1]);
1371           break;
1372         case QImode:
1373           mov = gen_movqi_internal (operands[0], operands[1]);
1374           break;
1375         default:
1376           abort ();
1377         }
1378
1379       /* Insert the instructions before any other argument copies.
1380          (The set_frame_ptr insn comes _after_ the move, so push it
1381          out first.)  */
1382       push_topmost_sequence ();
1383       emit_insn_after (gen_set_frame_ptr (), get_insns ());
1384       emit_insn_after (mov, get_insns ());
1385       pop_topmost_sequence ();
1386
1387       /* Ideally the incoming argument in a7 would only be copied
1388          once, since propagating a7 into the body of a function
1389          will almost certainly lead to errors.  However, there is
1390          at least one harmless case (in GCSE) where the original
1391          copy from a7 is changed to copy into a new pseudo.  Thus,
1392          we use a flag to only do this special treatment for the
1393          first copy of a7.  */
1394
1395       cfun->machine->incoming_a7_copied = true;
1396
1397       return 1;
1398     }
1399
1400   return 0;
1401 }
1402
1403
1404 /* Try to expand a block move operation to an RTL block move instruction.
1405    If not optimizing or if the block size is not a constant or if the
1406    block is small, the expansion fails and GCC falls back to calling
1407    memcpy().
1408
1409    operands[0] is the destination
1410    operands[1] is the source
1411    operands[2] is the length
1412    operands[3] is the alignment */
1413
1414 int
1415 xtensa_expand_block_move (operands)
1416      rtx *operands;
1417 {
1418   rtx dest = operands[0];
1419   rtx src = operands[1];
1420   int bytes = INTVAL (operands[2]);
1421   int align = XINT (operands[3], 0);
1422   int num_pieces, move_ratio;
1423
1424   /* If this is not a fixed size move, just call memcpy */
1425   if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
1426     return 0;
1427
1428   /* Anything to move? */
1429   if (bytes <= 0)
1430     return 1;
1431
1432   if (align > MOVE_MAX)
1433     align = MOVE_MAX;
1434
1435   /* decide whether to expand inline based on the optimization level */
1436   move_ratio = 4;
1437   if (optimize > 2)
1438     move_ratio = LARGEST_MOVE_RATIO;
1439   num_pieces = (bytes / align) + (bytes % align); /* close enough anyway */
1440   if (num_pieces >= move_ratio)
1441     return 0;
1442
1443   /* make sure the memory addresses are valid */
1444   operands[0] = validize_mem (dest);
1445   operands[1] = validize_mem (src);
1446
1447   emit_insn (gen_movstrsi_internal (operands[0], operands[1],
1448                                     operands[2], operands[3]));
1449   return 1;
1450 }
1451
1452
1453 /*  Emit a sequence of instructions to implement a block move, trying
1454     to hide load delay slots as much as possible.  Load N values into
1455     temporary registers, store those N values, and repeat until the
1456     complete block has been moved.  N=delay_slots+1 */
1457
1458 struct meminsnbuf {
1459   char template[30];
1460   rtx operands[2];
1461 };
1462
1463 void
1464 xtensa_emit_block_move (operands, tmpregs, delay_slots)
1465      rtx *operands;
1466      rtx *tmpregs;
1467      int delay_slots;
1468 {
1469   rtx dest = operands[0];
1470   rtx src = operands[1];
1471   int bytes = INTVAL (operands[2]);
1472   int align = XINT (operands[3], 0);
1473   rtx from_addr = XEXP (src, 0);
1474   rtx to_addr = XEXP (dest, 0);
1475   int from_struct = MEM_IN_STRUCT_P (src);
1476   int to_struct = MEM_IN_STRUCT_P (dest);
1477   int offset = 0;
1478   int chunk_size, item_size;
1479   struct meminsnbuf *ldinsns, *stinsns;
1480   const char *ldname, *stname;
1481   enum machine_mode mode;
1482
1483   if (align > MOVE_MAX)
1484     align = MOVE_MAX;
1485   item_size = align;
1486   chunk_size = delay_slots + 1;
1487
1488   ldinsns = (struct meminsnbuf *)
1489     alloca (chunk_size * sizeof (struct meminsnbuf));
1490   stinsns = (struct meminsnbuf *)
1491     alloca (chunk_size * sizeof (struct meminsnbuf));
1492
1493   mode = xtensa_find_mode_for_size (item_size);
1494   item_size = GET_MODE_SIZE (mode);
1495   ldname = xtensa_ld_opcodes[(int) mode];
1496   stname = xtensa_st_opcodes[(int) mode];
1497
1498   while (bytes > 0)
1499     {
1500       int n;
1501
1502       for (n = 0; n < chunk_size; n++)
1503         {
1504           rtx addr, mem;
1505
1506           if (bytes == 0)
1507             {
1508               chunk_size = n;
1509               break;
1510             }
1511
1512           if (bytes < item_size)
1513             {
1514               /* find a smaller item_size which we can load & store */
1515               item_size = bytes;
1516               mode = xtensa_find_mode_for_size (item_size);
1517               item_size = GET_MODE_SIZE (mode);
1518               ldname = xtensa_ld_opcodes[(int) mode];
1519               stname = xtensa_st_opcodes[(int) mode];
1520             }
1521
1522           /* record the load instruction opcode and operands */
1523           addr = plus_constant (from_addr, offset);
1524           mem = gen_rtx_MEM (mode, addr);
1525           if (! memory_address_p (mode, addr))
1526             abort ();
1527           MEM_IN_STRUCT_P (mem) = from_struct;
1528           ldinsns[n].operands[0] = tmpregs[n];
1529           ldinsns[n].operands[1] = mem;
1530           sprintf (ldinsns[n].template, "%s\t%%0, %%1", ldname);
1531
1532           /* record the store instruction opcode and operands */
1533           addr = plus_constant (to_addr, offset);
1534           mem = gen_rtx_MEM (mode, addr);
1535           if (! memory_address_p (mode, addr))
1536             abort ();
1537           MEM_IN_STRUCT_P (mem) = to_struct;
1538           stinsns[n].operands[0] = tmpregs[n];
1539           stinsns[n].operands[1] = mem;
1540           sprintf (stinsns[n].template, "%s\t%%0, %%1", stname);
1541
1542           offset += item_size;
1543           bytes -= item_size;
1544         }
1545
1546       /* now output the loads followed by the stores */
1547       for (n = 0; n < chunk_size; n++)
1548         output_asm_insn (ldinsns[n].template, ldinsns[n].operands);
1549       for (n = 0; n < chunk_size; n++)
1550         output_asm_insn (stinsns[n].template, stinsns[n].operands);
1551     }
1552 }
1553
1554
1555 static enum machine_mode
1556 xtensa_find_mode_for_size (item_size)
1557      unsigned item_size;
1558 {
1559   enum machine_mode mode, tmode;
1560
1561   while (1)
1562     {
1563       mode = VOIDmode;
1564
1565       /* find mode closest to but not bigger than item_size */
1566       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1567            tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
1568         if (GET_MODE_SIZE (tmode) <= item_size)
1569           mode = tmode;
1570       if (mode == VOIDmode)
1571         abort ();
1572
1573       item_size = GET_MODE_SIZE (mode);
1574
1575       if (xtensa_ld_opcodes[(int) mode]
1576           && xtensa_st_opcodes[(int) mode])
1577         break;
1578
1579       /* cannot load & store this mode; try something smaller */
1580       item_size -= 1;
1581     }
1582
1583   return mode;
1584 }
1585
1586
1587 void
1588 xtensa_expand_nonlocal_goto (operands)
1589      rtx *operands;
1590 {
1591   rtx goto_handler = operands[1];
1592   rtx containing_fp = operands[3];
1593
1594   /* generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
1595      is too big to generate in-line */
1596
1597   if (GET_CODE (containing_fp) != REG)
1598     containing_fp = force_reg (Pmode, containing_fp);
1599
1600   goto_handler = replace_rtx (copy_rtx (goto_handler),
1601                               virtual_stack_vars_rtx,
1602                               containing_fp);
1603
1604   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
1605                      0, VOIDmode, 2,
1606                      containing_fp, Pmode,
1607                      goto_handler, Pmode);
1608 }
1609
1610
1611 static struct machine_function *
1612 xtensa_init_machine_status ()
1613 {
1614   return ggc_alloc_cleared (sizeof (struct machine_function));
1615 }
1616
1617
1618 void
1619 xtensa_setup_frame_addresses ()
1620 {
1621   /* Set flag to cause FRAME_POINTER_REQUIRED to be set.  */
1622   cfun->machine->accesses_prev_frame = 1;
1623
1624   emit_library_call
1625     (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
1626      0, VOIDmode, 0);
1627 }
1628
1629
1630 /* Emit the assembly for the end of a zero-cost loop.  Normally we just emit
1631    a comment showing where the end of the loop is.  However, if there is a
1632    label or a branch at the end of the loop then we need to place a nop
1633    there.  If the loop ends with a label we need the nop so that branches
1634    targetting that label will target the nop (and thus remain in the loop),
1635    instead of targetting the instruction after the loop (and thus exiting
1636    the loop).  If the loop ends with a branch, we need the nop in case the
1637    branch is targetting a location inside the loop.  When the branch
1638    executes it will cause the loop count to be decremented even if it is
1639    taken (because it is the last instruction in the loop), so we need to
1640    nop after the branch to prevent the loop count from being decremented
1641    when the branch is taken.  */
1642
1643 void
1644 xtensa_emit_loop_end (insn, operands)
1645      rtx insn;
1646      rtx *operands;
1647 {
1648   char done = 0;
1649
1650   for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
1651     {
1652       switch (GET_CODE (insn))
1653         {
1654         case NOTE:
1655         case BARRIER:
1656           break;
1657
1658         case CODE_LABEL:
1659           output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1660           done = 1;
1661           break;
1662
1663         default:
1664           {
1665             rtx body = PATTERN (insn);
1666
1667             if (GET_CODE (body) == JUMP_INSN)
1668               {
1669                 output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
1670                 done = 1;
1671               }
1672             else if ((GET_CODE (body) != USE)
1673                      && (GET_CODE (body) != CLOBBER))
1674               done = 1;
1675           }
1676           break;
1677         }
1678     }
1679
1680   output_asm_insn ("# loop end for %0", operands);
1681 }
1682
1683
1684 char *
1685 xtensa_emit_call (callop, operands)
1686      int callop;
1687      rtx *operands;
1688 {
1689   static char result[64];
1690   rtx tgt = operands[callop];
1691
1692   if (GET_CODE (tgt) == CONST_INT)
1693     sprintf (result, "call8\t0x%lx", INTVAL (tgt));
1694   else if (register_operand (tgt, VOIDmode))
1695     sprintf (result, "callx8\t%%%d", callop);
1696   else
1697     sprintf (result, "call8\t%%%d", callop);
1698
1699   return result;
1700 }
1701
1702
1703 /* Return the stabs register number to use for 'regno'.  */
1704
1705 int
1706 xtensa_dbx_register_number (regno)
1707      int regno;
1708 {
1709   int first = -1;
1710
1711   if (GP_REG_P (regno))
1712     {
1713       regno -= GP_REG_FIRST;
1714       first = 0;
1715     }
1716   else if (BR_REG_P (regno))
1717     {
1718       regno -= BR_REG_FIRST;
1719       first = 16;
1720     }
1721   else if (FP_REG_P (regno))
1722     {
1723       regno -= FP_REG_FIRST;
1724       /* The current numbering convention is that TIE registers are
1725          numbered in libcc order beginning with 256.  We can't guarantee
1726          that the FP registers will come first, so the following is just
1727          a guess.  It seems like we should make a special case for FP
1728          registers and give them fixed numbers < 256.  */
1729       first = 256;
1730     }
1731   else if (ACC_REG_P (regno))
1732     {
1733       first = 0;
1734       regno = -1;
1735     }
1736
1737   /* When optimizing, we sometimes get asked about pseudo-registers
1738      that don't represent hard registers.  Return 0 for these.  */
1739   if (first == -1)
1740     return 0;
1741
1742   return first + regno;
1743 }
1744
1745
1746 /* Argument support functions.  */
1747
1748 /* Initialize CUMULATIVE_ARGS for a function.  */
1749
1750 void
1751 init_cumulative_args (cum, fntype, libname)
1752      CUMULATIVE_ARGS *cum;      /* argument info to initialize */
1753      tree fntype ATTRIBUTE_UNUSED;      /* tree ptr for function decl */
1754      rtx libname ATTRIBUTE_UNUSED;      /* SYMBOL_REF of library name or 0 */
1755 {
1756   cum->arg_words = 0;
1757 }
1758
1759 /* Advance the argument to the next argument position.  */
1760
1761 void
1762 function_arg_advance (cum, mode, type)
1763      CUMULATIVE_ARGS *cum;      /* current arg information */
1764      enum machine_mode mode;    /* current arg mode */
1765      tree type;                 /* type of the argument or 0 if lib support */
1766 {
1767   int words, max;
1768   int *arg_words;
1769
1770   arg_words = &cum->arg_words;
1771   max = MAX_ARGS_IN_REGISTERS;
1772
1773   words = (((mode != BLKmode)
1774             ? (int) GET_MODE_SIZE (mode)
1775             : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1776
1777   if ((*arg_words + words > max) && (*arg_words < max))
1778     *arg_words = max;
1779
1780   *arg_words += words;
1781 }
1782
1783
1784 /* Return an RTL expression containing the register for the given mode,
1785    or 0 if the argument is to be passed on the stack.  */
1786
1787 rtx
1788 function_arg (cum, mode, type, incoming_p)
1789      CUMULATIVE_ARGS *cum;      /* current arg information */
1790      enum machine_mode mode;    /* current arg mode */
1791      tree type;                 /* type of the argument or 0 if lib support */
1792      int incoming_p;            /* computing the incoming registers? */
1793 {
1794   int regbase, words, max;
1795   int *arg_words;
1796   int regno;
1797   enum machine_mode result_mode;
1798
1799   arg_words = &cum->arg_words;
1800   regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
1801   max = MAX_ARGS_IN_REGISTERS;
1802
1803   words = (((mode != BLKmode)
1804             ? (int) GET_MODE_SIZE (mode)
1805             : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1806
1807   if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
1808     *arg_words += (*arg_words & 1);
1809
1810   if (*arg_words + words > max)
1811     return (rtx)0;
1812
1813   regno = regbase + *arg_words;
1814   result_mode = (mode == BLKmode ? TYPE_MODE (type) : mode);
1815
1816   /* We need to make sure that references to a7 are represented with
1817      rtx that is not equal to hard_frame_pointer_rtx.  For BLKmode and
1818      modes bigger than 2 words (because we only have patterns for
1819      modes of 2 words or smaller), we can't control the expansion
1820      unless we explicitly list the individual registers in a PARALLEL.  */
1821
1822   if ((mode == BLKmode || words > 2)
1823       && regno < A7_REG
1824       && regno + words > A7_REG)
1825     {
1826       rtx result;
1827       int n;
1828
1829       result = gen_rtx_PARALLEL (result_mode, rtvec_alloc (words));
1830       for (n = 0; n < words; n++)
1831         {
1832           XVECEXP (result, 0, n) =
1833             gen_rtx_EXPR_LIST (VOIDmode,
1834                                gen_raw_REG (SImode, regno + n),
1835                                GEN_INT (n * UNITS_PER_WORD));
1836         }
1837       return result;
1838     }
1839
1840   return gen_raw_REG (result_mode, regno);
1841 }
1842
1843
1844 void
1845 override_options ()
1846 {
1847   int regno;
1848   enum machine_mode mode;
1849
1850   if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
1851     error ("boolean registers required for the floating-point option");
1852
1853   /* set up the tables of ld/st opcode names for block moves */
1854   xtensa_ld_opcodes[(int) SImode] = "l32i";
1855   xtensa_ld_opcodes[(int) HImode] = "l16ui";
1856   xtensa_ld_opcodes[(int) QImode] = "l8ui";
1857   xtensa_st_opcodes[(int) SImode] = "s32i";
1858   xtensa_st_opcodes[(int) HImode] = "s16i";
1859   xtensa_st_opcodes[(int) QImode] = "s8i";
1860
1861   xtensa_char_to_class['q'] = SP_REG;
1862   xtensa_char_to_class['a'] = GR_REGS;
1863   xtensa_char_to_class['b'] = ((TARGET_BOOLEANS) ? BR_REGS : NO_REGS);
1864   xtensa_char_to_class['f'] = ((TARGET_HARD_FLOAT) ? FP_REGS : NO_REGS);
1865   xtensa_char_to_class['A'] = ((TARGET_MAC16) ? ACC_REG : NO_REGS);
1866   xtensa_char_to_class['B'] = ((TARGET_SEXT) ? GR_REGS : NO_REGS);
1867   xtensa_char_to_class['C'] = ((TARGET_MUL16) ? GR_REGS: NO_REGS);
1868   xtensa_char_to_class['D'] = ((TARGET_DENSITY) ? GR_REGS: NO_REGS);
1869   xtensa_char_to_class['d'] = ((TARGET_DENSITY) ? AR_REGS: NO_REGS);
1870   xtensa_char_to_class['W'] = ((TARGET_CONST16) ? GR_REGS: NO_REGS);
1871
1872   /* Set up array giving whether a given register can hold a given mode.  */
1873   for (mode = VOIDmode;
1874        mode != MAX_MACHINE_MODE;
1875        mode = (enum machine_mode) ((int) mode + 1))
1876     {
1877       int size = GET_MODE_SIZE (mode);
1878       enum mode_class class = GET_MODE_CLASS (mode);
1879
1880       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1881         {
1882           int temp;
1883
1884           if (ACC_REG_P (regno))
1885             temp = (TARGET_MAC16
1886                     && (class == MODE_INT) && (size <= UNITS_PER_WORD));
1887           else if (GP_REG_P (regno))
1888             temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
1889           else if (FP_REG_P (regno))
1890             temp = (TARGET_HARD_FLOAT && (mode == SFmode));
1891           else if (BR_REG_P (regno))
1892             temp = (TARGET_BOOLEANS && (mode == CCmode));
1893           else
1894             temp = FALSE;
1895
1896           xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
1897         }
1898     }
1899
1900   init_machine_status = xtensa_init_machine_status;
1901
1902   /* Check PIC settings.  PIC is only supported when using L32R
1903      instructions, and some targets need to always use PIC.  */
1904   if (flag_pic && TARGET_CONST16)
1905     error ("-f%s is not supported with CONST16 instructions",
1906            (flag_pic > 1 ? "PIC" : "pic"));
1907   else if (XTENSA_ALWAYS_PIC)
1908     {
1909       if (TARGET_CONST16)
1910         error ("PIC is required but not supported with CONST16 instructions");
1911       flag_pic = 1;
1912     }
1913   /* There's no need for -fPIC (as opposed to -fpic) on Xtensa.  */
1914   if (flag_pic > 1)
1915     flag_pic = 1;
1916 }
1917
1918
1919 /* A C compound statement to output to stdio stream STREAM the
1920    assembler syntax for an instruction operand X.  X is an RTL
1921    expression.
1922
1923    CODE is a value that can be used to specify one of several ways
1924    of printing the operand.  It is used when identical operands
1925    must be printed differently depending on the context.  CODE
1926    comes from the '%' specification that was used to request
1927    printing of the operand.  If the specification was just '%DIGIT'
1928    then CODE is 0; if the specification was '%LTR DIGIT' then CODE
1929    is the ASCII code for LTR.
1930
1931    If X is a register, this macro should print the register's name.
1932    The names can be found in an array 'reg_names' whose type is
1933    'char *[]'.  'reg_names' is initialized from 'REGISTER_NAMES'.
1934
1935    When the machine description has a specification '%PUNCT' (a '%'
1936    followed by a punctuation character), this macro is called with
1937    a null pointer for X and the punctuation character for CODE.
1938
1939    'a', 'c', 'l', and 'n' are reserved.
1940
1941    The Xtensa specific codes are:
1942
1943    'd'  CONST_INT, print as signed decimal
1944    'x'  CONST_INT, print as signed hexadecimal
1945    'K'  CONST_INT, print number of bits in mask for EXTUI
1946    'R'  CONST_INT, print (X & 0x1f)
1947    'L'  CONST_INT, print ((32 - X) & 0x1f)
1948    'D'  REG, print second register of double-word register operand
1949    'N'  MEM, print address of next word following a memory operand
1950    'v'  MEM, if memory reference is volatile, output a MEMW before it
1951    't'  any constant, add "@h" suffix for top 16 bits
1952    'b'  any constant, add "@l" suffix for bottom 16 bits
1953 */
1954
1955 static void
1956 printx (file, val)
1957      FILE *file;
1958      signed int val;
1959 {
1960   /* print a hexadecimal value in a nice way */
1961   if ((val > -0xa) && (val < 0xa))
1962     fprintf (file, "%d", val);
1963   else if (val < 0)
1964     fprintf (file, "-0x%x", -val);
1965   else
1966     fprintf (file, "0x%x", val);
1967 }
1968
1969
1970 void
1971 print_operand (file, x, letter)
1972      FILE *file;                /* file to write to */
1973      rtx x;                     /* operand to print */
1974      int letter;                /* %<letter> or 0 */
1975 {
1976   if (!x)
1977     error ("PRINT_OPERAND null pointer");
1978
1979   switch (letter)
1980     {
1981     case 'D':
1982       if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
1983         fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
1984       else
1985         output_operand_lossage ("invalid %%D value");
1986       break;
1987
1988     case 'v':
1989       if (GET_CODE (x) == MEM)
1990         {
1991           /* For a volatile memory reference, emit a MEMW before the
1992              load or store.  */
1993           if (MEM_VOLATILE_P (x))
1994             fprintf (file, "memw\n\t");
1995         }
1996       else
1997         output_operand_lossage ("invalid %%v value");
1998       break;
1999
2000     case 'N':
2001       if (GET_CODE (x) == MEM
2002           && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
2003         {
2004           x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
2005           output_address (XEXP (x, 0));
2006         }
2007       else
2008         output_operand_lossage ("invalid %%N value");
2009       break;
2010
2011     case 'K':
2012       if (GET_CODE (x) == CONST_INT)
2013         {
2014           int num_bits = 0;
2015           unsigned val = INTVAL (x);
2016           while (val & 1)
2017             {
2018               num_bits += 1;
2019               val = val >> 1;
2020             }
2021           if ((val != 0) || (num_bits == 0) || (num_bits > 16))
2022             fatal_insn ("invalid mask", x);
2023
2024           fprintf (file, "%d", num_bits);
2025         }
2026       else
2027         output_operand_lossage ("invalid %%K value");
2028       break;
2029
2030     case 'L':
2031       if (GET_CODE (x) == CONST_INT)
2032         fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
2033       else
2034         output_operand_lossage ("invalid %%L value");
2035       break;
2036
2037     case 'R':
2038       if (GET_CODE (x) == CONST_INT)
2039         fprintf (file, "%ld", INTVAL (x) & 0x1f);
2040       else
2041         output_operand_lossage ("invalid %%R value");
2042       break;
2043
2044     case 'x':
2045       if (GET_CODE (x) == CONST_INT)
2046         printx (file, INTVAL (x));
2047       else
2048         output_operand_lossage ("invalid %%x value");
2049       break;
2050
2051     case 'd':
2052       if (GET_CODE (x) == CONST_INT)
2053         fprintf (file, "%ld", INTVAL (x));
2054       else
2055         output_operand_lossage ("invalid %%d value");
2056       break;
2057
2058     case 't':
2059     case 'b':
2060       if (GET_CODE (x) == CONST_INT)
2061         {
2062           printx (file, INTVAL (x));
2063           fputs (letter == 't' ? "@h" : "@l", file);
2064         }
2065       else if (GET_CODE (x) == CONST_DOUBLE)
2066         {
2067           REAL_VALUE_TYPE r;
2068           REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2069           if (GET_MODE (x) == SFmode)
2070             {
2071               long l;
2072               REAL_VALUE_TO_TARGET_SINGLE (r, l);
2073               fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
2074             }
2075           else
2076             output_operand_lossage ("invalid %%t/%%b value");
2077         }
2078       else if (GET_CODE (x) == CONST)
2079         {
2080           /* X must be a symbolic constant on ELF.  Write an expression
2081              suitable for 'const16' that sets the high or low 16 bits.  */
2082           if (GET_CODE (XEXP (x, 0)) != PLUS
2083               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
2084                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2085               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
2086             output_operand_lossage ("invalid %%t/%%b value");
2087           print_operand (file, XEXP (XEXP (x, 0), 0), 0);
2088           fputs (letter == 't' ? "@h" : "@l", file);
2089           /* There must be a non-alphanumeric character between 'h' or 'l'
2090              and the number.  The '-' is added by print_operand() already.  */
2091           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
2092             fputs ("+", file);
2093           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
2094         }
2095       else
2096         {
2097           output_addr_const (file, x);
2098           fputs (letter == 't' ? "@h" : "@l", file);
2099         }
2100       break;
2101
2102     default:
2103       if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2104         fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
2105       else if (GET_CODE (x) == MEM)
2106         output_address (XEXP (x, 0));
2107       else if (GET_CODE (x) == CONST_INT)
2108         fprintf (file, "%ld", INTVAL (x));
2109       else
2110         output_addr_const (file, x);
2111     }
2112 }
2113
2114
2115 /* A C compound statement to output to stdio stream STREAM the
2116    assembler syntax for an instruction operand that is a memory
2117    reference whose address is ADDR.  ADDR is an RTL expression.  */
2118
2119 void
2120 print_operand_address (file, addr)
2121      FILE *file;
2122      rtx addr;
2123 {
2124   if (!addr)
2125     error ("PRINT_OPERAND_ADDRESS, null pointer");
2126
2127   switch (GET_CODE (addr))
2128     {
2129     default:
2130       fatal_insn ("invalid address", addr);
2131       break;
2132
2133     case REG:
2134       fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
2135       break;
2136
2137     case PLUS:
2138       {
2139         rtx reg = (rtx)0;
2140         rtx offset = (rtx)0;
2141         rtx arg0 = XEXP (addr, 0);
2142         rtx arg1 = XEXP (addr, 1);
2143
2144         if (GET_CODE (arg0) == REG)
2145           {
2146             reg = arg0;
2147             offset = arg1;
2148           }
2149         else if (GET_CODE (arg1) == REG)
2150           {
2151             reg = arg1;
2152             offset = arg0;
2153           }
2154         else
2155           fatal_insn ("no register in address", addr);
2156
2157         if (CONSTANT_P (offset))
2158           {
2159             fprintf (file, "%s, ", reg_names [REGNO (reg)]);
2160             output_addr_const (file, offset);
2161           }
2162         else
2163           fatal_insn ("address offset not a constant", addr);
2164       }
2165       break;
2166
2167     case LABEL_REF:
2168     case SYMBOL_REF:
2169     case CONST_INT:
2170     case CONST:
2171       output_addr_const (file, addr);
2172       break;
2173     }
2174 }
2175
2176
2177 void
2178 xtensa_output_literal (file, x, mode, labelno)
2179      FILE *file;
2180      rtx x;
2181      enum machine_mode mode;
2182      int labelno;
2183 {
2184   long value_long[2];
2185   REAL_VALUE_TYPE r;
2186   int size;
2187
2188   fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
2189
2190   switch (GET_MODE_CLASS (mode))
2191     {
2192     case MODE_FLOAT:
2193       if (GET_CODE (x) != CONST_DOUBLE)
2194         abort ();
2195
2196       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2197       switch (mode)
2198         {
2199         case SFmode:
2200           REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
2201           fprintf (file, "0x%08lx\n", value_long[0]);
2202           break;
2203
2204         case DFmode:
2205           REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
2206           fprintf (file, "0x%08lx, 0x%08lx\n",
2207                    value_long[0], value_long[1]);
2208           break;
2209
2210         default:
2211           abort ();
2212         }
2213
2214       break;
2215
2216     case MODE_INT:
2217     case MODE_PARTIAL_INT:
2218       size = GET_MODE_SIZE (mode);
2219       if (size == 4)
2220         {
2221           output_addr_const (file, x);
2222           fputs ("\n", file);
2223         }
2224       else if (size == 8)
2225         {
2226           output_addr_const (file, operand_subword (x, 0, 0, DImode));
2227           fputs (", ", file);
2228           output_addr_const (file, operand_subword (x, 1, 0, DImode));
2229           fputs ("\n", file);
2230         }
2231       else
2232         abort ();
2233       break;
2234
2235     default:
2236       abort ();
2237     }
2238 }
2239
2240
2241 /* Return the bytes needed to compute the frame pointer from the current
2242    stack pointer.  */
2243
2244 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
2245 #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
2246
2247 long
2248 compute_frame_size (size)
2249      int size;                  /* # of var. bytes allocated */
2250 {
2251   /* add space for the incoming static chain value */
2252   if (current_function_needs_context)
2253     size += (1 * UNITS_PER_WORD);
2254
2255   xtensa_current_frame_size =
2256     XTENSA_STACK_ALIGN (size
2257                         + current_function_outgoing_args_size
2258                         + (WINDOW_SIZE * UNITS_PER_WORD));
2259   return xtensa_current_frame_size;
2260 }
2261
2262
2263 int
2264 xtensa_frame_pointer_required ()
2265 {
2266   /* The code to expand builtin_frame_addr and builtin_return_addr
2267      currently uses the hard_frame_pointer instead of frame_pointer.
2268      This seems wrong but maybe it's necessary for other architectures.
2269      This function is derived from the i386 code.  */
2270
2271   if (cfun->machine->accesses_prev_frame)
2272     return 1;
2273
2274   return 0;
2275 }
2276
2277
2278 void
2279 xtensa_expand_prologue ()
2280 {
2281   HOST_WIDE_INT total_size;
2282   rtx size_rtx;
2283
2284   total_size = compute_frame_size (get_frame_size ());
2285   size_rtx = GEN_INT (total_size);
2286
2287   if (total_size < (1 << (12+3)))
2288     emit_insn (gen_entry (size_rtx, size_rtx));
2289   else
2290     {
2291       /* Use a8 as a temporary since a0-a7 may be live.  */
2292       rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
2293       emit_insn (gen_entry (size_rtx, GEN_INT (MIN_FRAME_SIZE)));
2294       emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
2295       emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
2296       emit_move_insn (stack_pointer_rtx, tmp_reg);
2297     }
2298
2299   if (frame_pointer_needed)
2300     {
2301       rtx first, insn, set_frame_ptr_insn = 0;
2302
2303       push_topmost_sequence ();
2304       first = get_insns ();
2305       pop_topmost_sequence ();
2306
2307       /* Search all instructions, looking for the insn that sets up the
2308          frame pointer.  This search will fail if the function does not
2309          have an incoming argument in $a7, but in that case, we can just
2310          set up the frame pointer at the very beginning of the
2311          function.  */
2312
2313       for (insn = first; insn; insn = NEXT_INSN (insn))
2314         {
2315           rtx pat;
2316
2317           if (!INSN_P (insn))
2318             continue;
2319
2320           pat = PATTERN (insn);
2321           if (GET_CODE (pat) == SET
2322               && GET_CODE (SET_SRC (pat)) == UNSPEC_VOLATILE
2323               && (XINT (SET_SRC (pat), 1) == UNSPECV_SET_FP))
2324             {
2325               set_frame_ptr_insn = insn;
2326               break;
2327             }
2328         }
2329
2330       if (set_frame_ptr_insn)
2331         {
2332           /* For all instructions prior to set_frame_ptr_insn, replace
2333              hard_frame_pointer references with stack_pointer.  */
2334           for (insn = first;
2335                insn != set_frame_ptr_insn;
2336                insn = NEXT_INSN (insn))
2337             {
2338               if (INSN_P (insn))
2339                 PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
2340                                               hard_frame_pointer_rtx,
2341                                               stack_pointer_rtx);
2342             }
2343         }
2344       else
2345         emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
2346     }
2347 }
2348
2349
2350 /* Clear variables at function end.  */
2351
2352 void
2353 xtensa_function_epilogue (file, size)
2354      FILE *file ATTRIBUTE_UNUSED;
2355      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
2356 {
2357   xtensa_current_frame_size = 0;
2358 }
2359
2360
2361 rtx
2362 xtensa_return_addr (count, frame)
2363      int count;
2364      rtx frame;
2365 {
2366   rtx result, retaddr;
2367
2368   if (count == -1)
2369     retaddr = gen_rtx_REG (Pmode, A0_REG);
2370   else
2371     {
2372       rtx addr = plus_constant (frame, -4 * UNITS_PER_WORD);
2373       addr = memory_address (Pmode, addr);
2374       retaddr = gen_reg_rtx (Pmode);
2375       emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
2376     }
2377
2378   /* The 2 most-significant bits of the return address on Xtensa hold
2379      the register window size.  To get the real return address, these
2380      bits must be replaced with the high bits from the current PC.  */
2381
2382   result = gen_reg_rtx (Pmode);
2383   emit_insn (gen_fix_return_addr (result, retaddr));
2384   return result;
2385 }
2386
2387
2388 /* Create the va_list data type.
2389    This structure is set up by __builtin_saveregs.  The __va_reg
2390    field points to a stack-allocated region holding the contents of the
2391    incoming argument registers.  The __va_ndx field is an index initialized
2392    to the position of the first unnamed (variable) argument.  This same index
2393    is also used to address the arguments passed in memory.  Thus, the
2394    __va_stk field is initialized to point to the position of the first
2395    argument in memory offset to account for the arguments passed in
2396    registers.  E.G., if there are 6 argument registers, and each register is
2397    4 bytes, then __va_stk is set to $sp - (6 * 4); then __va_reg[N*4]
2398    references argument word N for 0 <= N < 6, and __va_stk[N*4] references
2399    argument word N for N >= 6.  */
2400
2401 tree
2402 xtensa_build_va_list ()
2403 {
2404   tree f_stk, f_reg, f_ndx, record, type_decl;
2405
2406   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2407   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2408
2409   f_stk = build_decl (FIELD_DECL, get_identifier ("__va_stk"),
2410                       ptr_type_node);
2411   f_reg = build_decl (FIELD_DECL, get_identifier ("__va_reg"),
2412                       ptr_type_node);
2413   f_ndx = build_decl (FIELD_DECL, get_identifier ("__va_ndx"),
2414                       integer_type_node);
2415
2416   DECL_FIELD_CONTEXT (f_stk) = record;
2417   DECL_FIELD_CONTEXT (f_reg) = record;
2418   DECL_FIELD_CONTEXT (f_ndx) = record;
2419
2420   TREE_CHAIN (record) = type_decl;
2421   TYPE_NAME (record) = type_decl;
2422   TYPE_FIELDS (record) = f_stk;
2423   TREE_CHAIN (f_stk) = f_reg;
2424   TREE_CHAIN (f_reg) = f_ndx;
2425
2426   layout_type (record);
2427   return record;
2428 }
2429
2430
2431 /* Save the incoming argument registers on the stack.  Returns the
2432    address of the saved registers.  */
2433
2434 rtx
2435 xtensa_builtin_saveregs ()
2436 {
2437   rtx gp_regs, dest;
2438   int arg_words = current_function_arg_words;
2439   int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
2440   int i;
2441
2442   if (gp_left == 0)
2443     return const0_rtx;
2444
2445   /* allocate the general-purpose register space */
2446   gp_regs = assign_stack_local
2447     (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
2448   set_mem_alias_set (gp_regs, get_varargs_alias_set ());
2449
2450   /* Now store the incoming registers.  */
2451   dest = change_address (gp_regs, SImode,
2452                          plus_constant (XEXP (gp_regs, 0),
2453                                         arg_words * UNITS_PER_WORD));
2454
2455   /* Note: Don't use move_block_from_reg() here because the incoming
2456      argument in a7 cannot be represented by hard_frame_pointer_rtx.
2457      Instead, call gen_raw_REG() directly so that we get a distinct
2458      instance of (REG:SI 7).  */
2459   for (i = 0; i < gp_left; i++)
2460     {
2461       emit_move_insn (operand_subword (dest, i, 1, BLKmode),
2462                       gen_raw_REG (SImode, GP_ARG_FIRST + arg_words + i));
2463     }
2464
2465   return XEXP (gp_regs, 0);
2466 }
2467
2468
2469 /* Implement `va_start' for varargs and stdarg.  We look at the
2470    current function to fill in an initial va_list.  */
2471
2472 void
2473 xtensa_va_start (valist, nextarg)
2474      tree valist;
2475      rtx nextarg ATTRIBUTE_UNUSED;
2476 {
2477   tree f_stk, stk;
2478   tree f_reg, reg;
2479   tree f_ndx, ndx;
2480   tree t, u;
2481   int arg_words;
2482
2483   arg_words = current_function_args_info.arg_words;
2484
2485   f_stk = TYPE_FIELDS (va_list_type_node);
2486   f_reg = TREE_CHAIN (f_stk);
2487   f_ndx = TREE_CHAIN (f_reg);
2488
2489   stk = build (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk);
2490   reg = build (COMPONENT_REF, TREE_TYPE (f_reg), valist, f_reg);
2491   ndx = build (COMPONENT_REF, TREE_TYPE (f_ndx), valist, f_ndx);
2492
2493   /* Call __builtin_saveregs; save the result in __va_reg */
2494   current_function_arg_words = arg_words;
2495   u = make_tree (ptr_type_node, expand_builtin_saveregs ());
2496   t = build (MODIFY_EXPR, ptr_type_node, reg, u);
2497   TREE_SIDE_EFFECTS (t) = 1;
2498   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2499
2500   /* Set the __va_stk member to $arg_ptr - (size of __va_reg area) */
2501   u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
2502   u = fold (build (PLUS_EXPR, ptr_type_node, u,
2503                    build_int_2 (-MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1)));
2504   t = build (MODIFY_EXPR, ptr_type_node, stk, u);
2505   TREE_SIDE_EFFECTS (t) = 1;
2506   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2507
2508   /* Set the __va_ndx member.  */
2509   u = build_int_2 (arg_words * UNITS_PER_WORD, 0);
2510   t = build (MODIFY_EXPR, integer_type_node, ndx, u);
2511   TREE_SIDE_EFFECTS (t) = 1;
2512   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2513 }
2514
2515
2516 /* Implement `va_arg'.  */
2517
2518 rtx
2519 xtensa_va_arg (valist, type)
2520      tree valist, type;
2521 {
2522   tree f_stk, stk;
2523   tree f_reg, reg;
2524   tree f_ndx, ndx;
2525   tree tmp, addr_tree, type_size;
2526   rtx array, orig_ndx, r, addr, size, va_size;
2527   rtx lab_false, lab_over, lab_false2;
2528
2529   f_stk = TYPE_FIELDS (va_list_type_node);
2530   f_reg = TREE_CHAIN (f_stk);
2531   f_ndx = TREE_CHAIN (f_reg);
2532
2533   stk = build (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk);
2534   reg = build (COMPONENT_REF, TREE_TYPE (f_reg), valist, f_reg);
2535   ndx = build (COMPONENT_REF, TREE_TYPE (f_ndx), valist, f_ndx);
2536
2537   type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type));
2538
2539   va_size = gen_reg_rtx (SImode);
2540   tmp = fold (build (MULT_EXPR, sizetype,
2541                      fold (build (TRUNC_DIV_EXPR, sizetype,
2542                                   fold (build (PLUS_EXPR, sizetype,
2543                                                type_size,
2544                                                size_int (UNITS_PER_WORD - 1))),
2545                                   size_int (UNITS_PER_WORD))),
2546                      size_int (UNITS_PER_WORD)));
2547   r = expand_expr (tmp, va_size, SImode, EXPAND_NORMAL);
2548   if (r != va_size)
2549     emit_move_insn (va_size, r);
2550
2551
2552   /* First align __va_ndx to a double word boundary if necessary for this arg:
2553
2554      if (__alignof__ (TYPE) > 4)
2555        (AP).__va_ndx = (((AP).__va_ndx + 7) & -8)
2556   */
2557
2558   if (TYPE_ALIGN (type) > BITS_PER_WORD)
2559     {
2560       tmp = build (PLUS_EXPR, integer_type_node, ndx,
2561                    build_int_2 ((2 * UNITS_PER_WORD) - 1, 0));
2562       tmp = build (BIT_AND_EXPR, integer_type_node, tmp,
2563                    build_int_2 (-2 * UNITS_PER_WORD, -1));
2564       tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2565       TREE_SIDE_EFFECTS (tmp) = 1;
2566       expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2567     }
2568
2569
2570   /* Increment __va_ndx to point past the argument:
2571
2572      orig_ndx = (AP).__va_ndx;
2573      (AP).__va_ndx += __va_size (TYPE);
2574   */
2575
2576   orig_ndx = gen_reg_rtx (SImode);
2577   r = expand_expr (ndx, orig_ndx, SImode, EXPAND_NORMAL);
2578   if (r != orig_ndx)
2579     emit_move_insn (orig_ndx, r);
2580
2581   tmp = build (PLUS_EXPR, integer_type_node, ndx,
2582                make_tree (intSI_type_node, va_size));
2583   tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2584   TREE_SIDE_EFFECTS (tmp) = 1;
2585   expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2586
2587
2588   /* Check if the argument is in registers:
2589
2590      if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
2591          && !MUST_PASS_IN_STACK (type))
2592         __array = (AP).__va_reg;
2593   */
2594
2595   array = gen_reg_rtx (Pmode);
2596
2597   lab_over = NULL_RTX;
2598   if (!MUST_PASS_IN_STACK (VOIDmode, type))
2599     {
2600       lab_false = gen_label_rtx ();
2601       lab_over = gen_label_rtx ();
2602
2603       emit_cmp_and_jump_insns (expand_expr (ndx, NULL_RTX, SImode,
2604                                             EXPAND_NORMAL),
2605                                GEN_INT (MAX_ARGS_IN_REGISTERS
2606                                         * UNITS_PER_WORD),
2607                                GT, const1_rtx, SImode, 0, lab_false);
2608
2609       r = expand_expr (reg, array, Pmode, EXPAND_NORMAL);
2610       if (r != array)
2611         emit_move_insn (array, r);
2612
2613       emit_jump_insn (gen_jump (lab_over));
2614       emit_barrier ();
2615       emit_label (lab_false);
2616     }
2617
2618   /* ...otherwise, the argument is on the stack (never split between
2619      registers and the stack -- change __va_ndx if necessary):
2620
2621      else
2622        {
2623          if (orig_ndx < __MAX_ARGS_IN_REGISTERS * 4)
2624              (AP).__va_ndx = __MAX_ARGS_IN_REGISTERS * 4 + __va_size (TYPE);
2625          __array = (AP).__va_stk;
2626        }
2627   */
2628
2629   lab_false2 = gen_label_rtx ();
2630   emit_cmp_and_jump_insns (orig_ndx,
2631                            GEN_INT (MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD),
2632                            GE, const1_rtx, SImode, 0, lab_false2);
2633
2634   tmp = build (PLUS_EXPR, sizetype, make_tree (intSI_type_node, va_size),
2635                build_int_2 (MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, 0));
2636   tmp = build (MODIFY_EXPR, integer_type_node, ndx, tmp);
2637   TREE_SIDE_EFFECTS (tmp) = 1;
2638   expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2639
2640   emit_label (lab_false2);
2641
2642   r = expand_expr (stk, array, Pmode, EXPAND_NORMAL);
2643   if (r != array)
2644     emit_move_insn (array, r);
2645
2646   if (lab_over != NULL_RTX)
2647     emit_label (lab_over);
2648
2649
2650   /* Given the base array pointer (__array) and index to the subsequent
2651      argument (__va_ndx), find the address:
2652
2653      __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
2654                                 ? sizeof (TYPE)
2655                                 : __va_size (TYPE))
2656
2657      The results are endian-dependent because values smaller than one word
2658      are aligned differently.
2659   */
2660
2661   size = gen_reg_rtx (SImode);
2662   emit_move_insn (size, va_size);
2663
2664   if (BYTES_BIG_ENDIAN)
2665     {
2666       rtx lab_use_va_size = gen_label_rtx ();
2667
2668       emit_cmp_and_jump_insns (expand_expr (type_size, NULL_RTX, SImode,
2669                                             EXPAND_NORMAL),
2670                                GEN_INT (PARM_BOUNDARY / BITS_PER_UNIT),
2671                                GE, const1_rtx, SImode, 0, lab_use_va_size);
2672
2673       r = expand_expr (type_size, size, SImode, EXPAND_NORMAL);
2674       if (r != size)
2675         emit_move_insn (size, r);
2676
2677       emit_label (lab_use_va_size);
2678     }
2679
2680   addr_tree = build (PLUS_EXPR, ptr_type_node,
2681                      make_tree (ptr_type_node, array),
2682                      ndx);
2683   addr_tree = build (MINUS_EXPR, ptr_type_node, addr_tree,
2684                      make_tree (intSI_type_node, size));
2685   addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2686   addr = copy_to_reg (addr);
2687   return addr;
2688 }
2689
2690
2691 enum reg_class
2692 xtensa_preferred_reload_class (x, class, isoutput)
2693      rtx x;
2694      enum reg_class class;
2695      int isoutput;
2696 {
2697   if (!isoutput && CONSTANT_P (x) && GET_CODE (x) == CONST_DOUBLE)
2698     return NO_REGS;
2699
2700   /* Don't use the stack pointer or hard frame pointer for reloads!
2701      The hard frame pointer would normally be OK except that it may
2702      briefly hold an incoming argument in the prologue, and reload
2703      won't know that it is live because the hard frame pointer is
2704      treated specially.  */
2705
2706   if (class == AR_REGS || class == GR_REGS)
2707     return RL_REGS;
2708
2709   return class;
2710 }
2711
2712
2713 enum reg_class
2714 xtensa_secondary_reload_class (class, mode, x, isoutput)
2715      enum reg_class class;
2716      enum machine_mode mode ATTRIBUTE_UNUSED;
2717      rtx x;
2718      int isoutput;
2719 {
2720   int regno;
2721
2722   if (GET_CODE (x) == SIGN_EXTEND)
2723     x = XEXP (x, 0);
2724   regno = xt_true_regnum (x);
2725
2726   if (!isoutput)
2727     {
2728       if (class == FP_REGS && constantpool_mem_p (x))
2729         return RL_REGS;
2730     }
2731
2732   if (ACC_REG_P (regno))
2733     return ((class == GR_REGS || class == RL_REGS) ? NO_REGS : RL_REGS);
2734   if (class == ACC_REG)
2735     return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
2736
2737   return NO_REGS;
2738 }
2739
2740
2741 void
2742 order_regs_for_local_alloc ()
2743 {
2744   if (!leaf_function_p ())
2745     {
2746       memcpy (reg_alloc_order, reg_nonleaf_alloc_order,
2747               FIRST_PSEUDO_REGISTER * sizeof (int));
2748     }
2749   else
2750     {
2751       int i, num_arg_regs;
2752       int nxt = 0;
2753
2754       /* use the AR registers in increasing order (skipping a0 and a1)
2755          but save the incoming argument registers for a last resort */
2756       num_arg_regs = current_function_args_info.arg_words;
2757       if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
2758         num_arg_regs = MAX_ARGS_IN_REGISTERS;
2759       for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
2760         reg_alloc_order[nxt++] = i + num_arg_regs;
2761       for (i = 0; i < num_arg_regs; i++)
2762         reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
2763
2764       /* list the coprocessor registers in order */
2765       for (i = 0; i < BR_REG_NUM; i++)
2766         reg_alloc_order[nxt++] = BR_REG_FIRST + i;
2767
2768       /* list the FP registers in order for now */
2769       for (i = 0; i < 16; i++)
2770         reg_alloc_order[nxt++] = FP_REG_FIRST + i;
2771
2772       /* GCC requires that we list *all* the registers....  */
2773       reg_alloc_order[nxt++] = 0;       /* a0 = return address */
2774       reg_alloc_order[nxt++] = 1;       /* a1 = stack pointer */
2775       reg_alloc_order[nxt++] = 16;      /* pseudo frame pointer */
2776       reg_alloc_order[nxt++] = 17;      /* pseudo arg pointer */
2777
2778       reg_alloc_order[nxt++] = ACC_REG_FIRST;   /* MAC16 accumulator */
2779     }
2780 }
2781
2782
2783 /* A customized version of reg_overlap_mentioned_p that only looks for
2784    references to a7 (as opposed to hard_frame_pointer_rtx).  */
2785
2786 int
2787 a7_overlap_mentioned_p (x)
2788      rtx x;
2789 {
2790   int i, j;
2791   unsigned int x_regno;
2792   const char *fmt;
2793
2794   if (GET_CODE (x) == REG)
2795     {
2796       x_regno = REGNO (x);
2797       return (x != hard_frame_pointer_rtx
2798               && x_regno < A7_REG + 1
2799               && x_regno + HARD_REGNO_NREGS (A7_REG, GET_MODE (x)) > A7_REG);
2800     }
2801
2802   if (GET_CODE (x) == SUBREG
2803       && GET_CODE (SUBREG_REG (x)) == REG
2804       && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
2805     {
2806       x_regno = subreg_regno (x);
2807       return (SUBREG_REG (x) != hard_frame_pointer_rtx
2808               && x_regno < A7_REG + 1
2809               && x_regno + HARD_REGNO_NREGS (A7_REG, GET_MODE (x)) > A7_REG);
2810     }
2811
2812   /* X does not match, so try its subexpressions.  */
2813   fmt = GET_RTX_FORMAT (GET_CODE (x));
2814   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2815     {
2816       if (fmt[i] == 'e')
2817         {
2818           if (a7_overlap_mentioned_p (XEXP (x, i)))
2819             return 1;
2820         }
2821       else if (fmt[i] == 'E')
2822         {
2823           for (j = XVECLEN (x, i) - 1; j >=0; j--)
2824             if (a7_overlap_mentioned_p (XVECEXP (x, i, j)))
2825               return 1;
2826         }
2827     }
2828
2829   return 0;
2830 }
2831
2832
2833 /* Some Xtensa targets support multiple bss sections.  If the section
2834    name ends with ".bss", add SECTION_BSS to the flags.  */
2835
2836 static unsigned int
2837 xtensa_multibss_section_type_flags (decl, name, reloc)
2838      tree decl;
2839      const char *name;
2840      int reloc;
2841 {
2842   unsigned int flags = default_section_type_flags (decl, name, reloc);
2843   const char *suffix;
2844
2845   suffix = strrchr (name, '.');
2846   if (suffix && strcmp (suffix, ".bss") == 0)
2847     {
2848       if (!decl || (TREE_CODE (decl) == VAR_DECL
2849                     && DECL_INITIAL (decl) == NULL_TREE))
2850         flags |= SECTION_BSS;  /* @nobits */
2851       else
2852         warning ("only uninitialized variables can be placed in a "
2853                  ".bss section");
2854     }
2855
2856   return flags;
2857 }
2858
2859
2860 /* The literal pool stays with the function.  */
2861
2862 static void
2863 xtensa_select_rtx_section (mode, x, align)
2864      enum machine_mode mode ATTRIBUTE_UNUSED;
2865      rtx x ATTRIBUTE_UNUSED;
2866      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
2867 {
2868   function_section (current_function_decl);
2869 }
2870
2871 /* Compute a (partial) cost for rtx X.  Return true if the complete
2872    cost has been computed, and false if subexpressions should be
2873    scanned.  In either case, *TOTAL contains the cost result.  */
2874
2875 static bool
2876 xtensa_rtx_costs (x, code, outer_code, total)
2877      rtx x;
2878      int code, outer_code;
2879      int *total;
2880 {
2881   switch (code)
2882     {
2883     case CONST_INT:
2884       switch (outer_code)
2885         {
2886         case SET:
2887           if (xtensa_simm12b (INTVAL (x)))
2888             {
2889               *total = 4;
2890               return true;
2891             }
2892           break;
2893         case PLUS:
2894           if (xtensa_simm8 (INTVAL (x))
2895               || xtensa_simm8x256 (INTVAL (x)))
2896             {
2897               *total = 0;
2898               return true;
2899             }
2900           break;
2901         case AND:
2902           if (xtensa_mask_immediate (INTVAL (x)))
2903             {
2904               *total = 0;
2905               return true;
2906             }
2907           break;
2908         case COMPARE:
2909           if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
2910             {
2911               *total = 0;
2912               return true;
2913             }
2914           break;
2915         case ASHIFT:
2916         case ASHIFTRT:
2917         case LSHIFTRT:
2918         case ROTATE:
2919         case ROTATERT:
2920           /* no way to tell if X is the 2nd operand so be conservative */
2921         default: break;
2922         }
2923       if (xtensa_simm12b (INTVAL (x)))
2924         *total = 5;
2925       else if (TARGET_CONST16)
2926         *total = COSTS_N_INSNS (2);
2927       else
2928         *total = 6;
2929       return true;
2930
2931     case CONST:
2932     case LABEL_REF:
2933     case SYMBOL_REF:
2934       if (TARGET_CONST16)
2935         *total = COSTS_N_INSNS (2);
2936       else
2937         *total = 5;
2938       return true;
2939
2940     case CONST_DOUBLE:
2941       if (TARGET_CONST16)
2942         *total = COSTS_N_INSNS (4);
2943       else
2944         *total = 7;
2945       return true;
2946
2947     case MEM:
2948       {
2949         int num_words =
2950           (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ?  2 : 1;
2951
2952         if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
2953           *total = COSTS_N_INSNS (num_words);
2954         else
2955           *total = COSTS_N_INSNS (2*num_words);
2956         return true;
2957       }
2958
2959     case FFS:
2960       *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
2961       return true;
2962
2963     case NOT:
2964       *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
2965       return true;
2966
2967     case AND:
2968     case IOR:
2969     case XOR:
2970       if (GET_MODE (x) == DImode)
2971         *total = COSTS_N_INSNS (2);
2972       else
2973         *total = COSTS_N_INSNS (1);
2974       return true;
2975
2976     case ASHIFT:
2977     case ASHIFTRT:
2978     case LSHIFTRT:
2979       if (GET_MODE (x) == DImode)
2980         *total = COSTS_N_INSNS (50);
2981       else
2982         *total = COSTS_N_INSNS (1);
2983       return true;
2984
2985     case ABS:
2986       {
2987         enum machine_mode xmode = GET_MODE (x);
2988         if (xmode == SFmode)
2989           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
2990         else if (xmode == DFmode)
2991           *total = COSTS_N_INSNS (50);
2992         else
2993           *total = COSTS_N_INSNS (4);
2994         return true;
2995       }
2996
2997     case PLUS:
2998     case MINUS:
2999       {
3000         enum machine_mode xmode = GET_MODE (x);
3001         if (xmode == SFmode)
3002           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
3003         else if (xmode == DFmode || xmode == DImode)
3004           *total = COSTS_N_INSNS (50);
3005         else
3006           *total = COSTS_N_INSNS (1);
3007         return true;
3008       }
3009
3010     case NEG:
3011       *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
3012       return true;
3013
3014     case MULT:
3015       {
3016         enum machine_mode xmode = GET_MODE (x);
3017         if (xmode == SFmode)
3018           *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
3019         else if (xmode == DFmode || xmode == DImode)
3020           *total = COSTS_N_INSNS (50);
3021         else if (TARGET_MUL32)
3022           *total = COSTS_N_INSNS (4);
3023         else if (TARGET_MAC16)
3024           *total = COSTS_N_INSNS (16);
3025         else if (TARGET_MUL16)
3026           *total = COSTS_N_INSNS (12);
3027         else
3028           *total = COSTS_N_INSNS (50);
3029         return true;
3030       }
3031
3032     case DIV:
3033     case MOD:
3034       {
3035         enum machine_mode xmode = GET_MODE (x);
3036         if (xmode == SFmode)
3037           {
3038             *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
3039             return true;
3040           }
3041         else if (xmode == DFmode)
3042           {
3043             *total = COSTS_N_INSNS (50);
3044             return true;
3045           }
3046       }
3047       /* fall through */
3048
3049     case UDIV:
3050     case UMOD:
3051       {
3052         enum machine_mode xmode = GET_MODE (x);
3053         if (xmode == DImode)
3054           *total = COSTS_N_INSNS (50);
3055         else if (TARGET_DIV32)
3056           *total = COSTS_N_INSNS (32);
3057         else
3058           *total = COSTS_N_INSNS (50);
3059         return true;
3060       }
3061
3062     case SQRT:
3063       if (GET_MODE (x) == SFmode)
3064         *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
3065       else
3066         *total = COSTS_N_INSNS (50);
3067       return true;
3068
3069     case SMIN:
3070     case UMIN:
3071     case SMAX:
3072     case UMAX:
3073       *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
3074       return true;
3075
3076     case SIGN_EXTRACT:
3077     case SIGN_EXTEND:
3078       *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
3079       return true;
3080
3081     case ZERO_EXTRACT:
3082     case ZERO_EXTEND:
3083       *total = COSTS_N_INSNS (1);
3084       return true;
3085
3086     default:
3087       return false;
3088     }
3089 }
3090
3091 #include "gt-xtensa.h"