OSDN Git Service

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