OSDN Git Service

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