OSDN Git Service

* config/frv/frv.c (frv_frame_access): Do not use reg+reg
[pf3gnuchains/gcc-fork.git] / gcc / config / frv / frv.c
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
2    2008  Free Software Foundation, Inc.
3    Contributed by Red Hat, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "recog.h"
37 #include "reload.h"
38 #include "expr.h"
39 #include "obstack.h"
40 #include "except.h"
41 #include "function.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "tm_p.h"
46 #include "ggc.h"
47 #include <ctype.h>
48 #include "target.h"
49 #include "target-def.h"
50 #include "targhooks.h"
51 #include "integrate.h"
52 #include "langhooks.h"
53 #include "df.h"
54
55 #ifndef FRV_INLINE
56 #define FRV_INLINE inline
57 #endif
58
59 /* The maximum number of distinct NOP patterns.  There are three:
60    nop, fnop and mnop.  */
61 #define NUM_NOP_PATTERNS 3
62
63 /* Classification of instructions and units: integer, floating-point/media,
64    branch and control.  */
65 enum frv_insn_group { GROUP_I, GROUP_FM, GROUP_B, GROUP_C, NUM_GROUPS };
66
67 /* The DFA names of the units, in packet order.  */
68 static const char *const frv_unit_names[] =
69 {
70   "c",
71   "i0", "f0",
72   "i1", "f1",
73   "i2", "f2",
74   "i3", "f3",
75   "b0", "b1"
76 };
77
78 /* The classification of each unit in frv_unit_names[].  */
79 static const enum frv_insn_group frv_unit_groups[ARRAY_SIZE (frv_unit_names)] =
80 {
81   GROUP_C,
82   GROUP_I, GROUP_FM,
83   GROUP_I, GROUP_FM,
84   GROUP_I, GROUP_FM,
85   GROUP_I, GROUP_FM,
86   GROUP_B, GROUP_B
87 };
88
89 /* Return the DFA unit code associated with the Nth unit of integer
90    or floating-point group GROUP,  */
91 #define NTH_UNIT(GROUP, N) frv_unit_codes[(GROUP) + (N) * 2 + 1]
92
93 /* Return the number of integer or floating-point unit UNIT
94    (1 for I1, 2 for F2, etc.).  */
95 #define UNIT_NUMBER(UNIT) (((UNIT) - 1) / 2)
96
97 /* The DFA unit number for each unit in frv_unit_names[].  */
98 static int frv_unit_codes[ARRAY_SIZE (frv_unit_names)];
99
100 /* FRV_TYPE_TO_UNIT[T] is the last unit in frv_unit_names[] that can issue
101    an instruction of type T.  The value is ARRAY_SIZE (frv_unit_names) if
102    no instruction of type T has been seen.  */
103 static unsigned int frv_type_to_unit[TYPE_UNKNOWN + 1];
104
105 /* An array of dummy nop INSNs, one for each type of nop that the
106    target supports.  */
107 static GTY(()) rtx frv_nops[NUM_NOP_PATTERNS];
108
109 /* The number of nop instructions in frv_nops[].  */
110 static unsigned int frv_num_nops;
111
112 /* Information about one __builtin_read or __builtin_write access, or
113    the combination of several such accesses.  The most general value
114    is all-zeros (an unknown access to an unknown address).  */
115 struct frv_io {
116   /* The type of access.  FRV_IO_UNKNOWN means the access can be either
117      a read or a write.  */
118   enum { FRV_IO_UNKNOWN, FRV_IO_READ, FRV_IO_WRITE } type;
119
120   /* The constant address being accessed, or zero if not known.  */
121   HOST_WIDE_INT const_address;
122
123   /* The run-time address, as used in operand 0 of the membar pattern.  */
124   rtx var_address;
125 };
126
127 /* Return true if instruction INSN should be packed with the following
128    instruction.  */
129 #define PACKING_FLAG_P(INSN) (GET_MODE (INSN) == TImode)
130
131 /* Set the value of PACKING_FLAG_P(INSN).  */
132 #define SET_PACKING_FLAG(INSN) PUT_MODE (INSN, TImode)
133 #define CLEAR_PACKING_FLAG(INSN) PUT_MODE (INSN, VOIDmode)
134
135 /* Loop with REG set to each hard register in rtx X.  */
136 #define FOR_EACH_REGNO(REG, X)                                          \
137   for (REG = REGNO (X);                                                 \
138        REG < REGNO (X) + HARD_REGNO_NREGS (REGNO (X), GET_MODE (X));    \
139        REG++)
140
141 /* This structure contains machine specific function data.  */
142 struct GTY(()) machine_function
143 {
144   /* True if we have created an rtx that relies on the stack frame.  */
145   int frame_needed;
146
147   /* True if this function contains at least one __builtin_{read,write}*.  */
148   bool has_membar_p;
149 };
150
151 /* Temporary register allocation support structure.  */
152 typedef struct frv_tmp_reg_struct
153   {
154     HARD_REG_SET regs;          /* possible registers to allocate */
155     int next_reg[N_REG_CLASSES];        /* next register to allocate per class */
156   }
157 frv_tmp_reg_t;
158
159 /* Register state information for VLIW re-packing phase.  */
160 #define REGSTATE_CC_MASK        0x07    /* Mask to isolate CCn for cond exec */
161 #define REGSTATE_MODIFIED       0x08    /* reg modified in current VLIW insn */
162 #define REGSTATE_IF_TRUE        0x10    /* reg modified in cond exec true */
163 #define REGSTATE_IF_FALSE       0x20    /* reg modified in cond exec false */
164
165 #define REGSTATE_IF_EITHER      (REGSTATE_IF_TRUE | REGSTATE_IF_FALSE)
166
167 typedef unsigned char regstate_t;
168
169 /* Used in frv_frame_accessor_t to indicate the direction of a register-to-
170    memory move.  */
171 enum frv_stack_op
172 {
173   FRV_LOAD,
174   FRV_STORE
175 };
176
177 /* Information required by frv_frame_access.  */
178 typedef struct
179 {
180   /* This field is FRV_LOAD if registers are to be loaded from the stack and
181      FRV_STORE if they should be stored onto the stack.  FRV_STORE implies
182      the move is being done by the prologue code while FRV_LOAD implies it
183      is being done by the epilogue.  */
184   enum frv_stack_op op;
185
186   /* The base register to use when accessing the stack.  This may be the
187      frame pointer, stack pointer, or a temporary.  The choice of register
188      depends on which part of the frame is being accessed and how big the
189      frame is.  */
190   rtx base;
191
192   /* The offset of BASE from the bottom of the current frame, in bytes.  */
193   int base_offset;
194 } frv_frame_accessor_t;
195
196 /* Define the information needed to generate branch and scc insns.  This is
197    stored from the compare operation.  */
198 rtx frv_compare_op0;
199 rtx frv_compare_op1;
200
201 /* Conditional execution support gathered together in one structure.  */
202 typedef struct
203   {
204     /* Linked list of insns to add if the conditional execution conversion was
205        successful.  Each link points to an EXPR_LIST which points to the pattern
206        of the insn to add, and the insn to be inserted before.  */
207     rtx added_insns_list;
208
209     /* Identify which registers are safe to allocate for if conversions to
210        conditional execution.  We keep the last allocated register in the
211        register classes between COND_EXEC statements.  This will mean we allocate
212        different registers for each different COND_EXEC group if we can.  This
213        might allow the scheduler to intermix two different COND_EXEC sections.  */
214     frv_tmp_reg_t tmp_reg;
215
216     /* For nested IFs, identify which CC registers are used outside of setting
217        via a compare isnsn, and using via a check insn.  This will allow us to
218        know if we can rewrite the register to use a different register that will
219        be paired with the CR register controlling the nested IF-THEN blocks.  */
220     HARD_REG_SET nested_cc_ok_rewrite;
221
222     /* Temporary registers allocated to hold constants during conditional
223        execution.  */
224     rtx scratch_regs[FIRST_PSEUDO_REGISTER];
225
226     /* Current number of temp registers available.  */
227     int cur_scratch_regs;
228
229     /* Number of nested conditional execution blocks.  */
230     int num_nested_cond_exec;
231
232     /* Map of insns that set up constants in scratch registers.  */
233     bitmap scratch_insns_bitmap;
234
235     /* Conditional execution test register (CC0..CC7).  */
236     rtx cr_reg;
237
238     /* Conditional execution compare register that is paired with cr_reg, so that
239        nested compares can be done.  The csubcc and caddcc instructions don't
240        have enough bits to specify both a CC register to be set and a CR register
241        to do the test on, so the same bit number is used for both.  Needless to
242        say, this is rather inconvenient for GCC.  */
243     rtx nested_cc_reg;
244
245     /* Extra CR registers used for &&, ||.  */
246     rtx extra_int_cr;
247     rtx extra_fp_cr;
248
249     /* Previous CR used in nested if, to make sure we are dealing with the same
250        nested if as the previous statement.  */
251     rtx last_nested_if_cr;
252   }
253 frv_ifcvt_t;
254
255 static /* GTY(()) */ frv_ifcvt_t frv_ifcvt;
256
257 /* Map register number to smallest register class.  */
258 enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER];
259
260 /* Map class letter into register class.  */
261 enum reg_class reg_class_from_letter[256];
262
263 /* Cached value of frv_stack_info.  */
264 static frv_stack_t *frv_stack_cache = (frv_stack_t *)0;
265
266 /* -mcpu= support */
267 frv_cpu_t frv_cpu_type = CPU_TYPE;      /* value of -mcpu= */
268
269 /* Forward references */
270
271 static bool frv_handle_option                   (size_t, const char *, int);
272 static int frv_default_flags_for_cpu            (void);
273 static int frv_string_begins_with               (const_tree, const char *);
274 static FRV_INLINE bool frv_small_data_reloc_p   (rtx, int);
275 static void frv_print_operand_memory_reference_reg
276                                                 (FILE *, rtx);
277 static void frv_print_operand_memory_reference  (FILE *, rtx, int);
278 static int frv_print_operand_jump_hint          (rtx);
279 static const char *comparison_string            (enum rtx_code, rtx);
280 static FRV_INLINE int frv_regno_ok_for_base_p   (int, int);
281 static rtx single_set_pattern                   (rtx);
282 static int frv_function_contains_far_jump       (void);
283 static rtx frv_alloc_temp_reg                   (frv_tmp_reg_t *,
284                                                  enum reg_class,
285                                                  enum machine_mode,
286                                                  int, int);
287 static rtx frv_frame_offset_rtx                 (int);
288 static rtx frv_frame_mem                        (enum machine_mode, rtx, int);
289 static rtx frv_dwarf_store                      (rtx, int);
290 static void frv_frame_insn                      (rtx, rtx);
291 static void frv_frame_access                    (frv_frame_accessor_t*,
292                                                  rtx, int);
293 static void frv_frame_access_multi              (frv_frame_accessor_t*,
294                                                  frv_stack_t *, int);
295 static void frv_frame_access_standard_regs      (enum frv_stack_op,
296                                                  frv_stack_t *);
297 static struct machine_function *frv_init_machine_status         (void);
298 static rtx frv_int_to_acc                       (enum insn_code, int, rtx);
299 static enum machine_mode frv_matching_accg_mode (enum machine_mode);
300 static rtx frv_read_argument                    (tree, unsigned int);
301 static rtx frv_read_iacc_argument               (enum machine_mode, tree, unsigned int);
302 static int frv_check_constant_argument          (enum insn_code, int, rtx);
303 static rtx frv_legitimize_target                (enum insn_code, rtx);
304 static rtx frv_legitimize_argument              (enum insn_code, int, rtx);
305 static rtx frv_legitimize_tls_address           (rtx, enum tls_model);
306 static rtx frv_expand_set_builtin               (enum insn_code, tree, rtx);
307 static rtx frv_expand_unop_builtin              (enum insn_code, tree, rtx);
308 static rtx frv_expand_binop_builtin             (enum insn_code, tree, rtx);
309 static rtx frv_expand_cut_builtin               (enum insn_code, tree, rtx);
310 static rtx frv_expand_binopimm_builtin          (enum insn_code, tree, rtx);
311 static rtx frv_expand_voidbinop_builtin         (enum insn_code, tree);
312 static rtx frv_expand_int_void2arg              (enum insn_code, tree);
313 static rtx frv_expand_prefetches                (enum insn_code, tree);
314 static rtx frv_expand_voidtriop_builtin         (enum insn_code, tree);
315 static rtx frv_expand_voidaccop_builtin         (enum insn_code, tree);
316 static rtx frv_expand_mclracc_builtin           (tree);
317 static rtx frv_expand_mrdacc_builtin            (enum insn_code, tree);
318 static rtx frv_expand_mwtacc_builtin            (enum insn_code, tree);
319 static rtx frv_expand_noargs_builtin            (enum insn_code);
320 static void frv_split_iacc_move                 (rtx, rtx);
321 static rtx frv_emit_comparison                  (enum rtx_code, rtx, rtx);
322 static int frv_clear_registers_used             (rtx *, void *);
323 static void frv_ifcvt_add_insn                  (rtx, rtx, int);
324 static rtx frv_ifcvt_rewrite_mem                (rtx, enum machine_mode, rtx);
325 static rtx frv_ifcvt_load_value                 (rtx, rtx);
326 static int frv_acc_group_1                      (rtx *, void *);
327 static unsigned int frv_insn_unit               (rtx);
328 static bool frv_issues_to_branch_unit_p         (rtx);
329 static int frv_cond_flags                       (rtx);
330 static bool frv_regstate_conflict_p             (regstate_t, regstate_t);
331 static int frv_registers_conflict_p_1           (rtx *, void *);
332 static bool frv_registers_conflict_p            (rtx);
333 static void frv_registers_update_1              (rtx, const_rtx, void *);
334 static void frv_registers_update                (rtx);
335 static void frv_start_packet                    (void);
336 static void frv_start_packet_block              (void);
337 static void frv_finish_packet                   (void (*) (void));
338 static bool frv_pack_insn_p                     (rtx);
339 static void frv_add_insn_to_packet              (rtx);
340 static void frv_insert_nop_in_packet            (rtx);
341 static bool frv_for_each_packet                 (void (*) (void));
342 static bool frv_sort_insn_group_1               (enum frv_insn_group,
343                                                  unsigned int, unsigned int,
344                                                  unsigned int, unsigned int,
345                                                  state_t);
346 static int frv_compare_insns                    (const void *, const void *);
347 static void frv_sort_insn_group                 (enum frv_insn_group);
348 static void frv_reorder_packet                  (void);
349 static void frv_fill_unused_units               (enum frv_insn_group);
350 static void frv_align_label                     (void);
351 static void frv_reorg_packet                    (void);
352 static void frv_register_nop                    (rtx);
353 static void frv_reorg                           (void);
354 static void frv_pack_insns                      (void);
355 static void frv_function_prologue               (FILE *, HOST_WIDE_INT);
356 static void frv_function_epilogue               (FILE *, HOST_WIDE_INT);
357 static bool frv_assemble_integer                (rtx, unsigned, int);
358 static void frv_init_builtins                   (void);
359 static rtx frv_expand_builtin                   (tree, rtx, rtx, enum machine_mode, int);
360 static void frv_init_libfuncs                   (void);
361 static bool frv_in_small_data_p                 (const_tree);
362 static void frv_asm_output_mi_thunk
363   (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
364 static void frv_setup_incoming_varargs          (CUMULATIVE_ARGS *,
365                                                  enum machine_mode,
366                                                  tree, int *, int);
367 static rtx frv_expand_builtin_saveregs          (void);
368 static void frv_expand_builtin_va_start         (tree, rtx);
369 static bool frv_rtx_costs                       (rtx, int, int, int*, bool);
370 static void frv_asm_out_constructor             (rtx, int);
371 static void frv_asm_out_destructor              (rtx, int);
372 static bool frv_function_symbol_referenced_p    (rtx);
373 static bool frv_cannot_force_const_mem          (rtx);
374 static const char *unspec_got_name              (int);
375 static void frv_output_const_unspec             (FILE *,
376                                                  const struct frv_unspec *);
377 static bool frv_function_ok_for_sibcall         (tree, tree);
378 static rtx frv_struct_value_rtx                 (tree, int);
379 static bool frv_must_pass_in_stack (enum machine_mode mode, const_tree type);
380 static int frv_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
381                                   tree, bool);
382 static void frv_output_dwarf_dtprel             (FILE *, int, rtx)
383   ATTRIBUTE_UNUSED;
384 static bool frv_secondary_reload                (bool, rtx, enum reg_class,
385                                                  enum machine_mode,
386                                                  secondary_reload_info *);
387 \f
388 /* Allow us to easily change the default for -malloc-cc.  */
389 #ifndef DEFAULT_NO_ALLOC_CC
390 #define MASK_DEFAULT_ALLOC_CC   MASK_ALLOC_CC
391 #else
392 #define MASK_DEFAULT_ALLOC_CC   0
393 #endif
394 \f
395 /* Initialize the GCC target structure.  */
396 #undef  TARGET_ASM_FUNCTION_PROLOGUE
397 #define TARGET_ASM_FUNCTION_PROLOGUE frv_function_prologue
398 #undef  TARGET_ASM_FUNCTION_EPILOGUE
399 #define TARGET_ASM_FUNCTION_EPILOGUE frv_function_epilogue
400 #undef  TARGET_ASM_INTEGER
401 #define TARGET_ASM_INTEGER frv_assemble_integer
402 #undef TARGET_DEFAULT_TARGET_FLAGS
403 #define TARGET_DEFAULT_TARGET_FLAGS             \
404   (MASK_DEFAULT_ALLOC_CC                        \
405    | MASK_COND_MOVE                             \
406    | MASK_SCC                                   \
407    | MASK_COND_EXEC                             \
408    | MASK_VLIW_BRANCH                           \
409    | MASK_MULTI_CE                              \
410    | MASK_NESTED_CE)
411 #undef TARGET_HANDLE_OPTION
412 #define TARGET_HANDLE_OPTION frv_handle_option
413 #undef TARGET_INIT_BUILTINS
414 #define TARGET_INIT_BUILTINS frv_init_builtins
415 #undef TARGET_EXPAND_BUILTIN
416 #define TARGET_EXPAND_BUILTIN frv_expand_builtin
417 #undef TARGET_INIT_LIBFUNCS
418 #define TARGET_INIT_LIBFUNCS frv_init_libfuncs
419 #undef TARGET_IN_SMALL_DATA_P
420 #define TARGET_IN_SMALL_DATA_P frv_in_small_data_p
421 #undef TARGET_RTX_COSTS
422 #define TARGET_RTX_COSTS frv_rtx_costs
423 #undef TARGET_ASM_CONSTRUCTOR
424 #define TARGET_ASM_CONSTRUCTOR frv_asm_out_constructor
425 #undef TARGET_ASM_DESTRUCTOR
426 #define TARGET_ASM_DESTRUCTOR frv_asm_out_destructor
427
428 #undef TARGET_ASM_OUTPUT_MI_THUNK
429 #define TARGET_ASM_OUTPUT_MI_THUNK frv_asm_output_mi_thunk
430 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
431 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
432
433 #undef  TARGET_SCHED_ISSUE_RATE
434 #define TARGET_SCHED_ISSUE_RATE frv_issue_rate
435
436 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
437 #define TARGET_FUNCTION_OK_FOR_SIBCALL frv_function_ok_for_sibcall
438 #undef TARGET_CANNOT_FORCE_CONST_MEM
439 #define TARGET_CANNOT_FORCE_CONST_MEM frv_cannot_force_const_mem
440
441 #undef TARGET_HAVE_TLS
442 #define TARGET_HAVE_TLS HAVE_AS_TLS
443
444 #undef TARGET_STRUCT_VALUE_RTX
445 #define TARGET_STRUCT_VALUE_RTX frv_struct_value_rtx
446 #undef TARGET_MUST_PASS_IN_STACK
447 #define TARGET_MUST_PASS_IN_STACK frv_must_pass_in_stack
448 #undef TARGET_PASS_BY_REFERENCE
449 #define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
450 #undef TARGET_ARG_PARTIAL_BYTES
451 #define TARGET_ARG_PARTIAL_BYTES frv_arg_partial_bytes
452
453 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
454 #define TARGET_EXPAND_BUILTIN_SAVEREGS frv_expand_builtin_saveregs
455 #undef TARGET_SETUP_INCOMING_VARARGS
456 #define TARGET_SETUP_INCOMING_VARARGS frv_setup_incoming_varargs
457 #undef TARGET_MACHINE_DEPENDENT_REORG
458 #define TARGET_MACHINE_DEPENDENT_REORG frv_reorg
459
460 #undef TARGET_EXPAND_BUILTIN_VA_START
461 #define TARGET_EXPAND_BUILTIN_VA_START frv_expand_builtin_va_start
462
463 #if HAVE_AS_TLS
464 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
465 #define TARGET_ASM_OUTPUT_DWARF_DTPREL frv_output_dwarf_dtprel
466 #endif
467
468 #undef  TARGET_SECONDARY_RELOAD
469 #define TARGET_SECONDARY_RELOAD frv_secondary_reload
470
471 struct gcc_target targetm = TARGET_INITIALIZER;
472
473 #define FRV_SYMBOL_REF_TLS_P(RTX) \
474   (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
475
476 \f
477 /* Any function call that satisfies the machine-independent
478    requirements is eligible on FR-V.  */
479
480 static bool
481 frv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
482                              tree exp ATTRIBUTE_UNUSED)
483 {
484   return true;
485 }
486
487 /* Return true if SYMBOL is a small data symbol and relocation RELOC
488    can be used to access it directly in a load or store.  */
489
490 static FRV_INLINE bool
491 frv_small_data_reloc_p (rtx symbol, int reloc)
492 {
493   return (GET_CODE (symbol) == SYMBOL_REF
494           && SYMBOL_REF_SMALL_P (symbol)
495           && (!TARGET_FDPIC || flag_pic == 1)
496           && (reloc == R_FRV_GOTOFF12 || reloc == R_FRV_GPREL12));
497 }
498
499 /* Return true if X is a valid relocation unspec.  If it is, fill in UNSPEC
500    appropriately.  */
501
502 bool
503 frv_const_unspec_p (rtx x, struct frv_unspec *unspec)
504 {
505   if (GET_CODE (x) == CONST)
506     {
507       unspec->offset = 0;
508       x = XEXP (x, 0);
509       if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
510         {
511           unspec->offset += INTVAL (XEXP (x, 1));
512           x = XEXP (x, 0);
513         }
514       if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOT)
515         {
516           unspec->symbol = XVECEXP (x, 0, 0);
517           unspec->reloc = INTVAL (XVECEXP (x, 0, 1));
518
519           if (unspec->offset == 0)
520             return true;
521
522           if (frv_small_data_reloc_p (unspec->symbol, unspec->reloc)
523               && unspec->offset > 0
524               && (unsigned HOST_WIDE_INT) unspec->offset < g_switch_value)
525             return true;
526         }
527     }
528   return false;
529 }
530
531 /* Decide whether we can force certain constants to memory.  If we
532    decide we can't, the caller should be able to cope with it in
533    another way.
534
535    We never allow constants to be forced into memory for TARGET_FDPIC.
536    This is necessary for several reasons:
537
538    1. Since LEGITIMATE_CONSTANT_P rejects constant pool addresses, the
539       target-independent code will try to force them into the constant
540       pool, thus leading to infinite recursion.
541
542    2. We can never introduce new constant pool references during reload.
543       Any such reference would require use of the pseudo FDPIC register.
544
545    3. We can't represent a constant added to a function pointer (which is
546       not the same as a pointer to a function+constant).
547
548    4. In many cases, it's more efficient to calculate the constant in-line.  */
549
550 static bool
551 frv_cannot_force_const_mem (rtx x ATTRIBUTE_UNUSED)
552 {
553   return TARGET_FDPIC;
554 }
555 \f
556 /* Implement TARGET_HANDLE_OPTION.  */
557
558 static bool
559 frv_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
560 {
561   switch (code)
562     {
563     case OPT_mcpu_:
564       if (strcmp (arg, "simple") == 0)
565         frv_cpu_type = FRV_CPU_SIMPLE;
566       else if (strcmp (arg, "tomcat") == 0)
567         frv_cpu_type = FRV_CPU_TOMCAT;
568       else if (strcmp (arg, "fr550") == 0)
569         frv_cpu_type = FRV_CPU_FR550;
570       else if (strcmp (arg, "fr500") == 0)
571         frv_cpu_type = FRV_CPU_FR500;
572       else if (strcmp (arg, "fr450") == 0)
573         frv_cpu_type = FRV_CPU_FR450;
574       else if (strcmp (arg, "fr405") == 0)
575         frv_cpu_type = FRV_CPU_FR405;
576       else if (strcmp (arg, "fr400") == 0)
577         frv_cpu_type = FRV_CPU_FR400;
578       else if (strcmp (arg, "fr300") == 0)
579         frv_cpu_type = FRV_CPU_FR300;
580       else if (strcmp (arg, "frv") == 0)
581         frv_cpu_type = FRV_CPU_GENERIC;
582       else
583         return false;
584       return true;
585
586     default:
587       return true;
588     }
589 }
590
591 static int
592 frv_default_flags_for_cpu (void)
593 {
594   switch (frv_cpu_type)
595     {
596     case FRV_CPU_GENERIC:
597       return MASK_DEFAULT_FRV;
598
599     case FRV_CPU_FR550:
600       return MASK_DEFAULT_FR550;
601
602     case FRV_CPU_FR500:
603     case FRV_CPU_TOMCAT:
604       return MASK_DEFAULT_FR500;
605
606     case FRV_CPU_FR450:
607       return MASK_DEFAULT_FR450;
608
609     case FRV_CPU_FR405:
610     case FRV_CPU_FR400:
611       return MASK_DEFAULT_FR400;
612
613     case FRV_CPU_FR300:
614     case FRV_CPU_SIMPLE:
615       return MASK_DEFAULT_SIMPLE;
616
617     default:
618       gcc_unreachable ();
619     }
620 }
621
622 /* Sometimes certain combinations of command options do not make
623    sense on a particular target machine.  You can define a macro
624    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
625    defined, is executed once just after all the command options have
626    been parsed.
627
628    Don't use this macro to turn on various extra optimizations for
629    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
630
631 void
632 frv_override_options (void)
633 {
634   int regno;
635   unsigned int i;
636
637   target_flags |= (frv_default_flags_for_cpu () & ~target_flags_explicit);
638
639   /* -mlibrary-pic sets -fPIC and -G0 and also suppresses warnings from the
640      linker about linking pic and non-pic code.  */
641   if (TARGET_LIBPIC)
642     {
643       if (!flag_pic)            /* -fPIC */
644         flag_pic = 2;
645
646       if (! g_switch_set)       /* -G0 */
647         {
648           g_switch_set = 1;
649           g_switch_value = 0;
650         }
651     }
652
653   /* A C expression whose value is a register class containing hard
654      register REGNO.  In general there is more than one such class;
655      choose a class which is "minimal", meaning that no smaller class
656      also contains the register.  */
657
658   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
659     {
660       enum reg_class rclass;
661
662       if (GPR_P (regno))
663         {
664           int gpr_reg = regno - GPR_FIRST;
665
666           if (gpr_reg == GR8_REG)
667             rclass = GR8_REGS;
668
669           else if (gpr_reg == GR9_REG)
670             rclass = GR9_REGS;
671
672           else if (gpr_reg == GR14_REG)
673             rclass = FDPIC_FPTR_REGS;
674
675           else if (gpr_reg == FDPIC_REGNO)
676             rclass = FDPIC_REGS;
677
678           else if ((gpr_reg & 3) == 0)
679             rclass = QUAD_REGS;
680
681           else if ((gpr_reg & 1) == 0)
682             rclass = EVEN_REGS;
683
684           else
685             rclass = GPR_REGS;
686         }
687
688       else if (FPR_P (regno))
689         {
690           int fpr_reg = regno - GPR_FIRST;
691           if ((fpr_reg & 3) == 0)
692             rclass = QUAD_FPR_REGS;
693
694           else if ((fpr_reg & 1) == 0)
695             rclass = FEVEN_REGS;
696
697           else
698             rclass = FPR_REGS;
699         }
700
701       else if (regno == LR_REGNO)
702         rclass = LR_REG;
703
704       else if (regno == LCR_REGNO)
705         rclass = LCR_REG;
706
707       else if (ICC_P (regno))
708         rclass = ICC_REGS;
709
710       else if (FCC_P (regno))
711         rclass = FCC_REGS;
712
713       else if (ICR_P (regno))
714         rclass = ICR_REGS;
715
716       else if (FCR_P (regno))
717         rclass = FCR_REGS;
718
719       else if (ACC_P (regno))
720         {
721           int r = regno - ACC_FIRST;
722           if ((r & 3) == 0)
723             rclass = QUAD_ACC_REGS;
724           else if ((r & 1) == 0)
725             rclass = EVEN_ACC_REGS;
726           else
727             rclass = ACC_REGS;
728         }
729
730       else if (ACCG_P (regno))
731         rclass = ACCG_REGS;
732
733       else
734         rclass = NO_REGS;
735
736       regno_reg_class[regno] = rclass;
737     }
738
739   /* Check for small data option */
740   if (!g_switch_set)
741     g_switch_value = SDATA_DEFAULT_SIZE;
742
743   /* A C expression which defines the machine-dependent operand
744      constraint letters for register classes.  If CHAR is such a
745      letter, the value should be the register class corresponding to
746      it.  Otherwise, the value should be `NO_REGS'.  The register
747      letter `r', corresponding to class `GENERAL_REGS', will not be
748      passed to this macro; you do not need to handle it.
749
750      The following letters are unavailable, due to being used as
751      constraints:
752         '0'..'9'
753         '<', '>'
754         'E', 'F', 'G', 'H'
755         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'
756         'Q', 'R', 'S', 'T', 'U'
757         'V', 'X'
758         'g', 'i', 'm', 'n', 'o', 'p', 'r', 's' */
759
760   for (i = 0; i < 256; i++)
761     reg_class_from_letter[i] = NO_REGS;
762
763   reg_class_from_letter['a'] = ACC_REGS;
764   reg_class_from_letter['b'] = EVEN_ACC_REGS;
765   reg_class_from_letter['c'] = CC_REGS;
766   reg_class_from_letter['d'] = GPR_REGS;
767   reg_class_from_letter['e'] = EVEN_REGS;
768   reg_class_from_letter['f'] = FPR_REGS;
769   reg_class_from_letter['h'] = FEVEN_REGS;
770   reg_class_from_letter['l'] = LR_REG;
771   reg_class_from_letter['q'] = QUAD_REGS;
772   reg_class_from_letter['t'] = ICC_REGS;
773   reg_class_from_letter['u'] = FCC_REGS;
774   reg_class_from_letter['v'] = ICR_REGS;
775   reg_class_from_letter['w'] = FCR_REGS;
776   reg_class_from_letter['x'] = QUAD_FPR_REGS;
777   reg_class_from_letter['y'] = LCR_REG;
778   reg_class_from_letter['z'] = SPR_REGS;
779   reg_class_from_letter['A'] = QUAD_ACC_REGS;
780   reg_class_from_letter['B'] = ACCG_REGS;
781   reg_class_from_letter['C'] = CR_REGS;
782   reg_class_from_letter['W'] = FDPIC_CALL_REGS; /* gp14+15 */
783   reg_class_from_letter['Z'] = FDPIC_REGS; /* gp15 */
784
785   /* There is no single unaligned SI op for PIC code.  Sometimes we
786      need to use ".4byte" and sometimes we need to use ".picptr".
787      See frv_assemble_integer for details.  */
788   if (flag_pic || TARGET_FDPIC)
789     targetm.asm_out.unaligned_op.si = 0;
790
791   if ((target_flags_explicit & MASK_LINKED_FP) == 0)
792     target_flags |= MASK_LINKED_FP;
793
794   if ((target_flags_explicit & MASK_OPTIMIZE_MEMBAR) == 0)
795     target_flags |= MASK_OPTIMIZE_MEMBAR;
796
797   for (i = 0; i < ARRAY_SIZE (frv_unit_names); i++)
798     frv_unit_codes[i] = get_cpu_unit_code (frv_unit_names[i]);
799
800   for (i = 0; i < ARRAY_SIZE (frv_type_to_unit); i++)
801     frv_type_to_unit[i] = ARRAY_SIZE (frv_unit_codes);
802
803   init_machine_status = frv_init_machine_status;
804 }
805
806 \f
807 /* Some machines may desire to change what optimizations are performed for
808    various optimization levels.  This macro, if defined, is executed once just
809    after the optimization level is determined and before the remainder of the
810    command options have been parsed.  Values set in this macro are used as the
811    default values for the other command line options.
812
813    LEVEL is the optimization level specified; 2 if `-O2' is specified, 1 if
814    `-O' is specified, and 0 if neither is specified.
815
816    SIZE is nonzero if `-Os' is specified, 0 otherwise.
817
818    You should not use this macro to change options that are not
819    machine-specific.  These should uniformly selected by the same optimization
820    level on all supported machines.  Use this macro to enable machine-specific
821    optimizations.
822
823    *Do not examine `write_symbols' in this macro!* The debugging options are
824    *not supposed to alter the generated code.  */
825
826 /* On the FRV, possibly disable VLIW packing which is done by the 2nd
827    scheduling pass at the current time.  */
828 void
829 frv_optimization_options (int level, int size ATTRIBUTE_UNUSED)
830 {
831   if (level >= 2)
832     {
833 #ifdef DISABLE_SCHED2
834       flag_schedule_insns_after_reload = 0;
835 #endif
836 #ifdef ENABLE_RCSP
837       flag_rcsp = 1;
838 #endif
839     }
840 }
841
842 \f
843 /* Return true if NAME (a STRING_CST node) begins with PREFIX.  */
844
845 static int
846 frv_string_begins_with (const_tree name, const char *prefix)
847 {
848   const int prefix_len = strlen (prefix);
849
850   /* Remember: NAME's length includes the null terminator.  */
851   return (TREE_STRING_LENGTH (name) > prefix_len
852           && strncmp (TREE_STRING_POINTER (name), prefix, prefix_len) == 0);
853 }
854 \f
855 /* Zero or more C statements that may conditionally modify two variables
856    `fixed_regs' and `call_used_regs' (both of type `char []') after they have
857    been initialized from the two preceding macros.
858
859    This is necessary in case the fixed or call-clobbered registers depend on
860    target flags.
861
862    You need not define this macro if it has no work to do.
863
864    If the usage of an entire class of registers depends on the target flags,
865    you may indicate this to GCC by using this macro to modify `fixed_regs' and
866    `call_used_regs' to 1 for each of the registers in the classes which should
867    not be used by GCC.  Also define the macro `REG_CLASS_FROM_LETTER' to return
868    `NO_REGS' if it is called with a letter for a class that shouldn't be used.
869
870    (However, if this class is not included in `GENERAL_REGS' and all of the
871    insn patterns whose constraints permit this class are controlled by target
872    switches, then GCC will automatically avoid using these registers when the
873    target switches are opposed to them.)  */
874
875 void
876 frv_conditional_register_usage (void)
877 {
878   int i;
879
880   for (i = GPR_FIRST + NUM_GPRS; i <= GPR_LAST; i++)
881     fixed_regs[i] = call_used_regs[i] = 1;
882
883   for (i = FPR_FIRST + NUM_FPRS; i <= FPR_LAST; i++)
884     fixed_regs[i] = call_used_regs[i] = 1;
885
886   /* Reserve the registers used for conditional execution.  At present, we need
887      1 ICC and 1 ICR register.  */
888   fixed_regs[ICC_TEMP] = call_used_regs[ICC_TEMP] = 1;
889   fixed_regs[ICR_TEMP] = call_used_regs[ICR_TEMP] = 1;
890
891   if (TARGET_FIXED_CC)
892     {
893       fixed_regs[ICC_FIRST] = call_used_regs[ICC_FIRST] = 1;
894       fixed_regs[FCC_FIRST] = call_used_regs[FCC_FIRST] = 1;
895       fixed_regs[ICR_FIRST] = call_used_regs[ICR_FIRST] = 1;
896       fixed_regs[FCR_FIRST] = call_used_regs[FCR_FIRST] = 1;
897     }
898
899   if (TARGET_FDPIC)
900     fixed_regs[GPR_FIRST + 16] = fixed_regs[GPR_FIRST + 17] =
901       call_used_regs[GPR_FIRST + 16] = call_used_regs[GPR_FIRST + 17] = 0;
902
903 #if 0
904   /* If -fpic, SDA_BASE_REG is the PIC register.  */
905   if (g_switch_value == 0 && !flag_pic)
906     fixed_regs[SDA_BASE_REG] = call_used_regs[SDA_BASE_REG] = 0;
907
908   if (!flag_pic)
909     fixed_regs[PIC_REGNO] = call_used_regs[PIC_REGNO] = 0;
910 #endif
911 }
912
913 \f
914 /*
915  * Compute the stack frame layout
916  *
917  * Register setup:
918  * +---------------+-----------------------+-----------------------+
919  * |Register       |type                   |caller-save/callee-save|
920  * +---------------+-----------------------+-----------------------+
921  * |GR0            |Zero register          |        -              |
922  * |GR1            |Stack pointer(SP)      |        -              |
923  * |GR2            |Frame pointer(FP)      |        -              |
924  * |GR3            |Hidden parameter       |        caller save    |
925  * |GR4-GR7        |        -              |        caller save    |
926  * |GR8-GR13       |Argument register      |        caller save    |
927  * |GR14-GR15      |        -              |        caller save    |
928  * |GR16-GR31      |        -              |        callee save    |
929  * |GR32-GR47      |        -              |        caller save    |
930  * |GR48-GR63      |        -              |        callee save    |
931  * |FR0-FR15       |        -              |        caller save    |
932  * |FR16-FR31      |        -              |        callee save    |
933  * |FR32-FR47      |        -              |        caller save    |
934  * |FR48-FR63      |        -              |        callee save    |
935  * +---------------+-----------------------+-----------------------+
936  *
937  * Stack frame setup:
938  * Low
939  *     SP-> |-----------------------------------|
940  *          |         Argument area             |
941  *          |-----------------------------------|
942  *          |    Register save area             |
943  *          |-----------------------------------|
944  *          |   Local variable save area        |
945  *     FP-> |-----------------------------------|
946  *          |       Old FP                      |
947  *          |-----------------------------------|
948  *          |    Hidden parameter save area     |
949  *          |-----------------------------------|
950  *          | Return address(LR) storage area   |
951  *          |-----------------------------------|
952  *          |     Padding for alignment         |
953  *          |-----------------------------------|
954  *          |     Register argument area        |
955  * OLD SP-> |-----------------------------------|
956  *          |       Parameter area              |
957  *          |-----------------------------------|
958  * High
959  *
960  * Argument area/Parameter area:
961  *
962  * When a function is called, this area is used for argument transfer.  When
963  * the argument is set up by the caller function, this area is referred to as
964  * the argument area.  When the argument is referenced by the callee function,
965  * this area is referred to as the parameter area.  The area is allocated when
966  * all arguments cannot be placed on the argument register at the time of
967  * argument transfer.
968  *
969  * Register save area:
970  *
971  * This is a register save area that must be guaranteed for the caller
972  * function.  This area is not secured when the register save operation is not
973  * needed.
974  *
975  * Local variable save area:
976  *
977  * This is the area for local variables and temporary variables.
978  *
979  * Old FP:
980  *
981  * This area stores the FP value of the caller function.
982  *
983  * Hidden parameter save area:
984  *
985  * This area stores the start address of the return value storage
986  * area for a struct/union return function.
987  * When a struct/union is used as the return value, the caller
988  * function stores the return value storage area start address in
989  * register GR3 and passes it to the caller function.
990  * The callee function interprets the address stored in the GR3
991  * as the return value storage area start address.
992  * When register GR3 needs to be saved into memory, the callee
993  * function saves it in the hidden parameter save area.  This
994  * area is not secured when the save operation is not needed.
995  *
996  * Return address(LR) storage area:
997  *
998  * This area saves the LR.  The LR stores the address of a return to the caller
999  * function for the purpose of function calling.
1000  *
1001  * Argument register area:
1002  *
1003  * This area saves the argument register.  This area is not secured when the
1004  * save operation is not needed.
1005  *
1006  * Argument:
1007  *
1008  * Arguments, the count of which equals the count of argument registers (6
1009  * words), are positioned in registers GR8 to GR13 and delivered to the callee
1010  * function.  When a struct/union return function is called, the return value
1011  * area address is stored in register GR3.  Arguments not placed in the
1012  * argument registers will be stored in the stack argument area for transfer
1013  * purposes.  When an 8-byte type argument is to be delivered using registers,
1014  * it is divided into two and placed in two registers for transfer.  When
1015  * argument registers must be saved to memory, the callee function secures an
1016  * argument register save area in the stack.  In this case, a continuous
1017  * argument register save area must be established in the parameter area.  The
1018  * argument register save area must be allocated as needed to cover the size of
1019  * the argument register to be saved.  If the function has a variable count of
1020  * arguments, it saves all argument registers in the argument register save
1021  * area.
1022  *
1023  * Argument Extension Format:
1024  *
1025  * When an argument is to be stored in the stack, its type is converted to an
1026  * extended type in accordance with the individual argument type.  The argument
1027  * is freed by the caller function after the return from the callee function is
1028  * made.
1029  *
1030  * +-----------------------+---------------+------------------------+
1031  * |    Argument Type      |Extended Type  |Stack Storage Size(byte)|
1032  * +-----------------------+---------------+------------------------+
1033  * |char                   |int            |        4               |
1034  * |signed char            |int            |        4               |
1035  * |unsigned char          |int            |        4               |
1036  * |[signed] short int     |int            |        4               |
1037  * |unsigned short int     |int            |        4               |
1038  * |[signed] int           |No extension   |        4               |
1039  * |unsigned int           |No extension   |        4               |
1040  * |[signed] long int      |No extension   |        4               |
1041  * |unsigned long int      |No extension   |        4               |
1042  * |[signed] long long int |No extension   |        8               |
1043  * |unsigned long long int |No extension   |        8               |
1044  * |float                  |double         |        8               |
1045  * |double                 |No extension   |        8               |
1046  * |long double            |No extension   |        8               |
1047  * |pointer                |No extension   |        4               |
1048  * |struct/union           |-              |        4 (*1)          |
1049  * +-----------------------+---------------+------------------------+
1050  *
1051  * When a struct/union is to be delivered as an argument, the caller copies it
1052  * to the local variable area and delivers the address of that area.
1053  *
1054  * Return Value:
1055  *
1056  * +-------------------------------+----------------------+
1057  * |Return Value Type              |Return Value Interface|
1058  * +-------------------------------+----------------------+
1059  * |void                           |None                  |
1060  * |[signed|unsigned] char         |GR8                   |
1061  * |[signed|unsigned] short int    |GR8                   |
1062  * |[signed|unsigned] int          |GR8                   |
1063  * |[signed|unsigned] long int     |GR8                   |
1064  * |pointer                        |GR8                   |
1065  * |[signed|unsigned] long long int|GR8 & GR9             |
1066  * |float                          |GR8                   |
1067  * |double                         |GR8 & GR9             |
1068  * |long double                    |GR8 & GR9             |
1069  * |struct/union                   |(*1)                  |
1070  * +-------------------------------+----------------------+
1071  *
1072  * When a struct/union is used as the return value, the caller function stores
1073  * the start address of the return value storage area into GR3 and then passes
1074  * it to the callee function.  The callee function interprets GR3 as the start
1075  * address of the return value storage area.  When this address needs to be
1076  * saved in memory, the callee function secures the hidden parameter save area
1077  * and saves the address in that area.
1078  */
1079
1080 frv_stack_t *
1081 frv_stack_info (void)
1082 {
1083   static frv_stack_t info, zero_info;
1084   frv_stack_t *info_ptr = &info;
1085   tree fndecl           = current_function_decl;
1086   int varargs_p         = 0;
1087   tree cur_arg;
1088   tree next_arg;
1089   int range;
1090   int alignment;
1091   int offset;
1092
1093   /* If we've already calculated the values and reload is complete,
1094      just return now.  */
1095   if (frv_stack_cache)
1096     return frv_stack_cache;
1097
1098   /* Zero all fields.  */
1099   info = zero_info;
1100
1101   /* Set up the register range information.  */
1102   info_ptr->regs[STACK_REGS_GPR].name         = "gpr";
1103   info_ptr->regs[STACK_REGS_GPR].first        = LAST_ARG_REGNUM + 1;
1104   info_ptr->regs[STACK_REGS_GPR].last         = GPR_LAST;
1105   info_ptr->regs[STACK_REGS_GPR].dword_p      = TRUE;
1106
1107   info_ptr->regs[STACK_REGS_FPR].name         = "fpr";
1108   info_ptr->regs[STACK_REGS_FPR].first        = FPR_FIRST;
1109   info_ptr->regs[STACK_REGS_FPR].last         = FPR_LAST;
1110   info_ptr->regs[STACK_REGS_FPR].dword_p      = TRUE;
1111
1112   info_ptr->regs[STACK_REGS_LR].name          = "lr";
1113   info_ptr->regs[STACK_REGS_LR].first         = LR_REGNO;
1114   info_ptr->regs[STACK_REGS_LR].last          = LR_REGNO;
1115   info_ptr->regs[STACK_REGS_LR].special_p     = 1;
1116
1117   info_ptr->regs[STACK_REGS_CC].name          = "cc";
1118   info_ptr->regs[STACK_REGS_CC].first         = CC_FIRST;
1119   info_ptr->regs[STACK_REGS_CC].last          = CC_LAST;
1120   info_ptr->regs[STACK_REGS_CC].field_p       = TRUE;
1121
1122   info_ptr->regs[STACK_REGS_LCR].name         = "lcr";
1123   info_ptr->regs[STACK_REGS_LCR].first        = LCR_REGNO;
1124   info_ptr->regs[STACK_REGS_LCR].last         = LCR_REGNO;
1125
1126   info_ptr->regs[STACK_REGS_STDARG].name      = "stdarg";
1127   info_ptr->regs[STACK_REGS_STDARG].first     = FIRST_ARG_REGNUM;
1128   info_ptr->regs[STACK_REGS_STDARG].last      = LAST_ARG_REGNUM;
1129   info_ptr->regs[STACK_REGS_STDARG].dword_p   = 1;
1130   info_ptr->regs[STACK_REGS_STDARG].special_p = 1;
1131
1132   info_ptr->regs[STACK_REGS_STRUCT].name      = "struct";
1133   info_ptr->regs[STACK_REGS_STRUCT].first     = FRV_STRUCT_VALUE_REGNUM;
1134   info_ptr->regs[STACK_REGS_STRUCT].last      = FRV_STRUCT_VALUE_REGNUM;
1135   info_ptr->regs[STACK_REGS_STRUCT].special_p = 1;
1136
1137   info_ptr->regs[STACK_REGS_FP].name          = "fp";
1138   info_ptr->regs[STACK_REGS_FP].first         = FRAME_POINTER_REGNUM;
1139   info_ptr->regs[STACK_REGS_FP].last          = FRAME_POINTER_REGNUM;
1140   info_ptr->regs[STACK_REGS_FP].special_p     = 1;
1141
1142   /* Determine if this is a stdarg function.  If so, allocate space to store
1143      the 6 arguments.  */
1144   if (cfun->stdarg)
1145     varargs_p = 1;
1146
1147   else
1148     {
1149       /* Find the last argument, and see if it is __builtin_va_alist.  */
1150       for (cur_arg = DECL_ARGUMENTS (fndecl); cur_arg != (tree)0; cur_arg = next_arg)
1151         {
1152           next_arg = TREE_CHAIN (cur_arg);
1153           if (next_arg == (tree)0)
1154             {
1155               if (DECL_NAME (cur_arg)
1156                   && !strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)), "__builtin_va_alist"))
1157                 varargs_p = 1;
1158
1159               break;
1160             }
1161         }
1162     }
1163
1164   /* Iterate over all of the register ranges.  */
1165   for (range = 0; range < STACK_REGS_MAX; range++)
1166     {
1167       frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1168       int first = reg_ptr->first;
1169       int last = reg_ptr->last;
1170       int size_1word = 0;
1171       int size_2words = 0;
1172       int regno;
1173
1174       /* Calculate which registers need to be saved & save area size.  */
1175       switch (range)
1176         {
1177         default:
1178           for (regno = first; regno <= last; regno++)
1179             {
1180               if ((df_regs_ever_live_p (regno) && !call_used_regs[regno])
1181                   || (crtl->calls_eh_return
1182                       && (regno >= FIRST_EH_REGNUM && regno <= LAST_EH_REGNUM))
1183                   || (!TARGET_FDPIC && flag_pic
1184                       && crtl->uses_pic_offset_table && regno == PIC_REGNO))
1185                 {
1186                   info_ptr->save_p[regno] = REG_SAVE_1WORD;
1187                   size_1word += UNITS_PER_WORD;
1188                 }
1189             }
1190           break;
1191
1192           /* Calculate whether we need to create a frame after everything else
1193              has been processed.  */
1194         case STACK_REGS_FP:
1195           break;
1196
1197         case STACK_REGS_LR:
1198           if (df_regs_ever_live_p (LR_REGNO)
1199               || profile_flag
1200               /* This is set for __builtin_return_address, etc.  */
1201               || cfun->machine->frame_needed
1202               || (TARGET_LINKED_FP && frame_pointer_needed)
1203               || (!TARGET_FDPIC && flag_pic
1204                   && crtl->uses_pic_offset_table))
1205             {
1206               info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
1207               size_1word += UNITS_PER_WORD;
1208             }
1209           break;
1210
1211         case STACK_REGS_STDARG:
1212           if (varargs_p)
1213             {
1214               /* If this is a stdarg function with a non varardic
1215                  argument split between registers and the stack,
1216                  adjust the saved registers downward.  */
1217               last -= (ADDR_ALIGN (crtl->args.pretend_args_size, UNITS_PER_WORD)
1218                        / UNITS_PER_WORD);
1219
1220               for (regno = first; regno <= last; regno++)
1221                 {
1222                   info_ptr->save_p[regno] = REG_SAVE_1WORD;
1223                   size_1word += UNITS_PER_WORD;
1224                 }
1225
1226               info_ptr->stdarg_size = size_1word;
1227             }
1228           break;
1229
1230         case STACK_REGS_STRUCT:
1231           if (cfun->returns_struct)
1232             {
1233               info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
1234               size_1word += UNITS_PER_WORD;
1235             }
1236           break;
1237         }
1238
1239
1240       if (size_1word)
1241         {
1242           /* If this is a field, it only takes one word.  */
1243           if (reg_ptr->field_p)
1244             size_1word = UNITS_PER_WORD;
1245
1246           /* Determine which register pairs can be saved together.  */
1247           else if (reg_ptr->dword_p && TARGET_DWORD)
1248             {
1249               for (regno = first; regno < last; regno += 2)
1250                 {
1251                   if (info_ptr->save_p[regno] && info_ptr->save_p[regno+1])
1252                     {
1253                       size_2words += 2 * UNITS_PER_WORD;
1254                       size_1word -= 2 * UNITS_PER_WORD;
1255                       info_ptr->save_p[regno] = REG_SAVE_2WORDS;
1256                       info_ptr->save_p[regno+1] = REG_SAVE_NO_SAVE;
1257                     }
1258                 }
1259             }
1260
1261           reg_ptr->size_1word = size_1word;
1262           reg_ptr->size_2words = size_2words;
1263
1264           if (! reg_ptr->special_p)
1265             {
1266               info_ptr->regs_size_1word += size_1word;
1267               info_ptr->regs_size_2words += size_2words;
1268             }
1269         }
1270     }
1271
1272   /* Set up the sizes of each each field in the frame body, making the sizes
1273      of each be divisible by the size of a dword if dword operations might
1274      be used, or the size of a word otherwise.  */
1275   alignment = (TARGET_DWORD? 2 * UNITS_PER_WORD : UNITS_PER_WORD);
1276
1277   info_ptr->parameter_size = ADDR_ALIGN (crtl->outgoing_args_size, alignment);
1278   info_ptr->regs_size = ADDR_ALIGN (info_ptr->regs_size_2words
1279                                     + info_ptr->regs_size_1word,
1280                                     alignment);
1281   info_ptr->vars_size = ADDR_ALIGN (get_frame_size (), alignment);
1282
1283   info_ptr->pretend_size = crtl->args.pretend_args_size;
1284
1285   /* Work out the size of the frame, excluding the header.  Both the frame
1286      body and register parameter area will be dword-aligned.  */
1287   info_ptr->total_size
1288     = (ADDR_ALIGN (info_ptr->parameter_size
1289                    + info_ptr->regs_size
1290                    + info_ptr->vars_size,
1291                    2 * UNITS_PER_WORD)
1292        + ADDR_ALIGN (info_ptr->pretend_size
1293                      + info_ptr->stdarg_size,
1294                      2 * UNITS_PER_WORD));
1295
1296   /* See if we need to create a frame at all, if so add header area.  */
1297   if (info_ptr->total_size  > 0
1298       || frame_pointer_needed
1299       || info_ptr->regs[STACK_REGS_LR].size_1word > 0
1300       || info_ptr->regs[STACK_REGS_STRUCT].size_1word > 0)
1301     {
1302       offset = info_ptr->parameter_size;
1303       info_ptr->header_size = 4 * UNITS_PER_WORD;
1304       info_ptr->total_size += 4 * UNITS_PER_WORD;
1305
1306       /* Calculate the offsets to save normal register pairs.  */
1307       for (range = 0; range < STACK_REGS_MAX; range++)
1308         {
1309           frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1310           if (! reg_ptr->special_p)
1311             {
1312               int first = reg_ptr->first;
1313               int last = reg_ptr->last;
1314               int regno;
1315
1316               for (regno = first; regno <= last; regno++)
1317                 if (info_ptr->save_p[regno] == REG_SAVE_2WORDS
1318                     && regno != FRAME_POINTER_REGNUM
1319                     && (regno < FIRST_ARG_REGNUM
1320                         || regno > LAST_ARG_REGNUM))
1321                   {
1322                     info_ptr->reg_offset[regno] = offset;
1323                     offset += 2 * UNITS_PER_WORD;
1324                   }
1325             }
1326         }
1327
1328       /* Calculate the offsets to save normal single registers.  */
1329       for (range = 0; range < STACK_REGS_MAX; range++)
1330         {
1331           frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1332           if (! reg_ptr->special_p)
1333             {
1334               int first = reg_ptr->first;
1335               int last = reg_ptr->last;
1336               int regno;
1337
1338               for (regno = first; regno <= last; regno++)
1339                 if (info_ptr->save_p[regno] == REG_SAVE_1WORD
1340                     && regno != FRAME_POINTER_REGNUM
1341                     && (regno < FIRST_ARG_REGNUM
1342                         || regno > LAST_ARG_REGNUM))
1343                   {
1344                     info_ptr->reg_offset[regno] = offset;
1345                     offset += UNITS_PER_WORD;
1346                   }
1347             }
1348         }
1349
1350       /* Calculate the offset to save the local variables at.  */
1351       offset = ADDR_ALIGN (offset, alignment);
1352       if (info_ptr->vars_size)
1353         {
1354           info_ptr->vars_offset = offset;
1355           offset += info_ptr->vars_size;
1356         }
1357
1358       /* Align header to a dword-boundary.  */
1359       offset = ADDR_ALIGN (offset, 2 * UNITS_PER_WORD);
1360
1361       /* Calculate the offsets in the fixed frame.  */
1362       info_ptr->save_p[FRAME_POINTER_REGNUM] = REG_SAVE_1WORD;
1363       info_ptr->reg_offset[FRAME_POINTER_REGNUM] = offset;
1364       info_ptr->regs[STACK_REGS_FP].size_1word = UNITS_PER_WORD;
1365
1366       info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
1367       info_ptr->reg_offset[LR_REGNO] = offset + 2*UNITS_PER_WORD;
1368       info_ptr->regs[STACK_REGS_LR].size_1word = UNITS_PER_WORD;
1369
1370       if (cfun->returns_struct)
1371         {
1372           info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
1373           info_ptr->reg_offset[FRV_STRUCT_VALUE_REGNUM] = offset + UNITS_PER_WORD;
1374           info_ptr->regs[STACK_REGS_STRUCT].size_1word = UNITS_PER_WORD;
1375         }
1376
1377       /* Calculate the offsets to store the arguments passed in registers
1378          for stdarg functions.  The register pairs are first and the single
1379          register if any is last.  The register save area starts on a
1380          dword-boundary.  */
1381       if (info_ptr->stdarg_size)
1382         {
1383           int first = info_ptr->regs[STACK_REGS_STDARG].first;
1384           int last  = info_ptr->regs[STACK_REGS_STDARG].last;
1385           int regno;
1386
1387           /* Skip the header.  */
1388           offset += 4 * UNITS_PER_WORD;
1389           for (regno = first; regno <= last; regno++)
1390             {
1391               if (info_ptr->save_p[regno] == REG_SAVE_2WORDS)
1392                 {
1393                   info_ptr->reg_offset[regno] = offset;
1394                   offset += 2 * UNITS_PER_WORD;
1395                 }
1396               else if (info_ptr->save_p[regno] == REG_SAVE_1WORD)
1397                 {
1398                   info_ptr->reg_offset[regno] = offset;
1399                   offset += UNITS_PER_WORD;
1400                 }
1401             }
1402         }
1403     }
1404
1405   if (reload_completed)
1406     frv_stack_cache = info_ptr;
1407
1408   return info_ptr;
1409 }
1410
1411 \f
1412 /* Print the information about the frv stack offsets, etc. when debugging.  */
1413
1414 void
1415 frv_debug_stack (frv_stack_t *info)
1416 {
1417   int range;
1418
1419   if (!info)
1420     info = frv_stack_info ();
1421
1422   fprintf (stderr, "\nStack information for function %s:\n",
1423            ((current_function_decl && DECL_NAME (current_function_decl))
1424             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
1425             : "<unknown>"));
1426
1427   fprintf (stderr, "\ttotal_size\t= %6d\n", info->total_size);
1428   fprintf (stderr, "\tvars_size\t= %6d\n", info->vars_size);
1429   fprintf (stderr, "\tparam_size\t= %6d\n", info->parameter_size);
1430   fprintf (stderr, "\tregs_size\t= %6d, 1w = %3d, 2w = %3d\n",
1431            info->regs_size, info->regs_size_1word, info->regs_size_2words);
1432
1433   fprintf (stderr, "\theader_size\t= %6d\n", info->header_size);
1434   fprintf (stderr, "\tpretend_size\t= %6d\n", info->pretend_size);
1435   fprintf (stderr, "\tvars_offset\t= %6d\n", info->vars_offset);
1436   fprintf (stderr, "\tregs_offset\t= %6d\n", info->regs_offset);
1437
1438   for (range = 0; range < STACK_REGS_MAX; range++)
1439     {
1440       frv_stack_regs_t *regs = &(info->regs[range]);
1441       if ((regs->size_1word + regs->size_2words) > 0)
1442         {
1443           int first = regs->first;
1444           int last  = regs->last;
1445           int regno;
1446
1447           fprintf (stderr, "\t%s\tsize\t= %6d, 1w = %3d, 2w = %3d, save =",
1448                    regs->name, regs->size_1word + regs->size_2words,
1449                    regs->size_1word, regs->size_2words);
1450
1451           for (regno = first; regno <= last; regno++)
1452             {
1453               if (info->save_p[regno] == REG_SAVE_1WORD)
1454                 fprintf (stderr, " %s (%d)", reg_names[regno],
1455                          info->reg_offset[regno]);
1456
1457               else if (info->save_p[regno] == REG_SAVE_2WORDS)
1458                 fprintf (stderr, " %s-%s (%d)", reg_names[regno],
1459                          reg_names[regno+1], info->reg_offset[regno]);
1460             }
1461
1462           fputc ('\n', stderr);
1463         }
1464     }
1465
1466   fflush (stderr);
1467 }
1468
1469
1470 \f
1471
1472 /* Used during final to control the packing of insns.  The value is
1473    1 if the current instruction should be packed with the next one,
1474    0 if it shouldn't or -1 if packing is disabled altogether.  */
1475
1476 static int frv_insn_packing_flag;
1477
1478 /* True if the current function contains a far jump.  */
1479
1480 static int
1481 frv_function_contains_far_jump (void)
1482 {
1483   rtx insn = get_insns ();
1484   while (insn != NULL
1485          && !(GET_CODE (insn) == JUMP_INSN
1486               /* Ignore tablejump patterns.  */
1487               && GET_CODE (PATTERN (insn)) != ADDR_VEC
1488               && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
1489               && get_attr_far_jump (insn) == FAR_JUMP_YES))
1490     insn = NEXT_INSN (insn);
1491   return (insn != NULL);
1492 }
1493
1494 /* For the FRV, this function makes sure that a function with far jumps
1495    will return correctly.  It also does the VLIW packing.  */
1496
1497 static void
1498 frv_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1499 {
1500   /* If no frame was created, check whether the function uses a call
1501      instruction to implement a far jump.  If so, save the link in gr3 and
1502      replace all returns to LR with returns to GR3.  GR3 is used because it
1503      is call-clobbered, because is not available to the register allocator,
1504      and because all functions that take a hidden argument pointer will have
1505      a stack frame.  */
1506   if (frv_stack_info ()->total_size == 0 && frv_function_contains_far_jump ())
1507     {
1508       rtx insn;
1509
1510       /* Just to check that the above comment is true.  */
1511       gcc_assert (!df_regs_ever_live_p (GPR_FIRST + 3));
1512
1513       /* Generate the instruction that saves the link register.  */
1514       fprintf (file, "\tmovsg lr,gr3\n");
1515
1516       /* Replace the LR with GR3 in *return_internal patterns.  The insn
1517          will now return using jmpl @(gr3,0) rather than bralr.  We cannot
1518          simply emit a different assembly directive because bralr and jmpl
1519          execute in different units.  */
1520       for (insn = get_insns(); insn != NULL; insn = NEXT_INSN (insn))
1521         if (GET_CODE (insn) == JUMP_INSN)
1522           {
1523             rtx pattern = PATTERN (insn);
1524             if (GET_CODE (pattern) == PARALLEL
1525                 && XVECLEN (pattern, 0) >= 2
1526                 && GET_CODE (XVECEXP (pattern, 0, 0)) == RETURN
1527                 && GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
1528               {
1529                 rtx address = XEXP (XVECEXP (pattern, 0, 1), 0);
1530                 if (GET_CODE (address) == REG && REGNO (address) == LR_REGNO)
1531                   SET_REGNO (address, GPR_FIRST + 3);
1532               }
1533           }
1534     }
1535
1536   frv_pack_insns ();
1537
1538   /* Allow the garbage collector to free the nops created by frv_reorg.  */
1539   memset (frv_nops, 0, sizeof (frv_nops));
1540 }
1541
1542 \f
1543 /* Return the next available temporary register in a given class.  */
1544
1545 static rtx
1546 frv_alloc_temp_reg (
1547      frv_tmp_reg_t *info,       /* which registers are available */
1548      enum reg_class rclass,     /* register class desired */
1549      enum machine_mode mode,    /* mode to allocate register with */
1550      int mark_as_used,          /* register not available after allocation */
1551      int no_abort)              /* return NULL instead of aborting */
1552 {
1553   int regno = info->next_reg[ (int)rclass ];
1554   int orig_regno = regno;
1555   HARD_REG_SET *reg_in_class = &reg_class_contents[ (int)rclass ];
1556   int i, nr;
1557
1558   for (;;)
1559     {
1560       if (TEST_HARD_REG_BIT (*reg_in_class, regno)
1561           && TEST_HARD_REG_BIT (info->regs, regno))
1562           break;
1563
1564       if (++regno >= FIRST_PSEUDO_REGISTER)
1565         regno = 0;
1566       if (regno == orig_regno)
1567         {
1568           gcc_assert (no_abort);
1569           return NULL_RTX;
1570         }
1571     }
1572
1573   nr = HARD_REGNO_NREGS (regno, mode);
1574   info->next_reg[ (int)rclass ] = regno + nr;
1575
1576   if (mark_as_used)
1577     for (i = 0; i < nr; i++)
1578       CLEAR_HARD_REG_BIT (info->regs, regno+i);
1579
1580   return gen_rtx_REG (mode, regno);
1581 }
1582
1583 \f
1584 /* Return an rtx with the value OFFSET, which will either be a register or a
1585    signed 12-bit integer.  It can be used as the second operand in an "add"
1586    instruction, or as the index in a load or store.
1587
1588    The function returns a constant rtx if OFFSET is small enough, otherwise
1589    it loads the constant into register OFFSET_REGNO and returns that.  */
1590 static rtx
1591 frv_frame_offset_rtx (int offset)
1592 {
1593   rtx offset_rtx = GEN_INT (offset);
1594   if (IN_RANGE_P (offset, -2048, 2047))
1595     return offset_rtx;
1596   else
1597     {
1598       rtx reg_rtx = gen_rtx_REG (SImode, OFFSET_REGNO);
1599       if (IN_RANGE_P (offset, -32768, 32767))
1600         emit_insn (gen_movsi (reg_rtx, offset_rtx));
1601       else
1602         {
1603           emit_insn (gen_movsi_high (reg_rtx, offset_rtx));
1604           emit_insn (gen_movsi_lo_sum (reg_rtx, offset_rtx));
1605         }
1606       return reg_rtx;
1607     }
1608 }
1609
1610 /* Generate (mem:MODE (plus:Pmode BASE (frv_frame_offset OFFSET)))).  The
1611    prologue and epilogue uses such expressions to access the stack.  */
1612 static rtx
1613 frv_frame_mem (enum machine_mode mode, rtx base, int offset)
1614 {
1615   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode,
1616                                           base,
1617                                           frv_frame_offset_rtx (offset)));
1618 }
1619
1620 /* Generate a frame-related expression:
1621
1622         (set REG (mem (plus (sp) (const_int OFFSET)))).
1623
1624    Such expressions are used in FRAME_RELATED_EXPR notes for more complex
1625    instructions.  Marking the expressions as frame-related is superfluous if
1626    the note contains just a single set.  But if the note contains a PARALLEL
1627    or SEQUENCE that has several sets, each set must be individually marked
1628    as frame-related.  */
1629 static rtx
1630 frv_dwarf_store (rtx reg, int offset)
1631 {
1632   rtx set = gen_rtx_SET (VOIDmode,
1633                          gen_rtx_MEM (GET_MODE (reg),
1634                                       plus_constant (stack_pointer_rtx,
1635                                                      offset)),
1636                          reg);
1637   RTX_FRAME_RELATED_P (set) = 1;
1638   return set;
1639 }
1640
1641 /* Emit a frame-related instruction whose pattern is PATTERN.  The
1642    instruction is the last in a sequence that cumulatively performs the
1643    operation described by DWARF_PATTERN.  The instruction is marked as
1644    frame-related and has a REG_FRAME_RELATED_EXPR note containing
1645    DWARF_PATTERN.  */
1646 static void
1647 frv_frame_insn (rtx pattern, rtx dwarf_pattern)
1648 {
1649   rtx insn = emit_insn (pattern);
1650   RTX_FRAME_RELATED_P (insn) = 1;
1651   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1652                                       dwarf_pattern,
1653                                       REG_NOTES (insn));
1654 }
1655
1656 /* Emit instructions that transfer REG to or from the memory location (sp +
1657    STACK_OFFSET).  The register is stored in memory if ACCESSOR->OP is
1658    FRV_STORE and loaded if it is FRV_LOAD.  Only the prologue uses this
1659    function to store registers and only the epilogue uses it to load them.
1660
1661    The caller sets up ACCESSOR so that BASE is equal to (sp + BASE_OFFSET).
1662    The generated instruction will use BASE as its base register.  BASE may
1663    simply be the stack pointer, but if several accesses are being made to a
1664    region far away from the stack pointer, it may be more efficient to set
1665    up a temporary instead.
1666
1667    Store instructions will be frame-related and will be annotated with the
1668    overall effect of the store.  Load instructions will be followed by a
1669    (use) to prevent later optimizations from zapping them.
1670
1671    The function takes care of the moves to and from SPRs, using TEMP_REGNO
1672    as a temporary in such cases.  */
1673 static void
1674 frv_frame_access (frv_frame_accessor_t *accessor, rtx reg, int stack_offset)
1675 {
1676   enum machine_mode mode = GET_MODE (reg);
1677   rtx mem = frv_frame_mem (mode,
1678                            accessor->base,
1679                            stack_offset - accessor->base_offset);
1680
1681   if (accessor->op == FRV_LOAD)
1682     {
1683       if (SPR_P (REGNO (reg)))
1684         {
1685           rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
1686           emit_insn (gen_rtx_SET (VOIDmode, temp, mem));
1687           emit_insn (gen_rtx_SET (VOIDmode, reg, temp));
1688         }
1689       else
1690         {
1691           /* We cannot use reg+reg addressing for DImode access.  */
1692           if (mode == DImode
1693               && GET_CODE (XEXP (mem, 0)) == PLUS
1694               && GET_CODE (XEXP (XEXP (mem, 0), 0)) == REG
1695               && GET_CODE (XEXP (XEXP (mem, 0), 1)) == REG)
1696             {
1697               rtx temp = gen_rtx_REG (SImode, TEMP_REGNO);
1698               rtx insn = emit_move_insn (temp,
1699                                          gen_rtx_PLUS (SImode, XEXP (XEXP (mem, 0), 0),
1700                                                        XEXP (XEXP (mem, 0), 1)));
1701               mem = gen_rtx_MEM (DImode, temp);
1702             }
1703           emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
1704         }
1705       emit_use (reg);
1706     }
1707   else
1708     {
1709       if (SPR_P (REGNO (reg)))
1710         {
1711           rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
1712           emit_insn (gen_rtx_SET (VOIDmode, temp, reg));
1713           frv_frame_insn (gen_rtx_SET (Pmode, mem, temp),
1714                           frv_dwarf_store (reg, stack_offset));
1715         }
1716       else if (mode == DImode)
1717         {
1718           /* For DImode saves, the dwarf2 version needs to be a SEQUENCE
1719              with a separate save for each register.  */
1720           rtx reg1 = gen_rtx_REG (SImode, REGNO (reg));
1721           rtx reg2 = gen_rtx_REG (SImode, REGNO (reg) + 1);
1722           rtx set1 = frv_dwarf_store (reg1, stack_offset);
1723           rtx set2 = frv_dwarf_store (reg2, stack_offset + 4);
1724
1725           /* Also we cannot use reg+reg addressing.  */
1726           if (GET_CODE (XEXP (mem, 0)) == PLUS
1727               && GET_CODE (XEXP (XEXP (mem, 0), 0)) == REG
1728               && GET_CODE (XEXP (XEXP (mem, 0), 1)) == REG)
1729             {
1730               rtx temp = gen_rtx_REG (SImode, TEMP_REGNO);
1731               rtx insn = emit_move_insn (temp,
1732                                          gen_rtx_PLUS (SImode, XEXP (XEXP (mem, 0), 0),
1733                                                        XEXP (XEXP (mem, 0), 1)));
1734               mem = gen_rtx_MEM (DImode, temp);
1735             }
1736
1737           frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
1738                           gen_rtx_PARALLEL (VOIDmode,
1739                                             gen_rtvec (2, set1, set2)));
1740         }
1741       else
1742         frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
1743                         frv_dwarf_store (reg, stack_offset));
1744     }
1745 }
1746
1747 /* A function that uses frv_frame_access to transfer a group of registers to
1748    or from the stack.  ACCESSOR is passed directly to frv_frame_access, INFO
1749    is the stack information generated by frv_stack_info, and REG_SET is the
1750    number of the register set to transfer.  */
1751 static void
1752 frv_frame_access_multi (frv_frame_accessor_t *accessor,
1753                         frv_stack_t *info,
1754                         int reg_set)
1755 {
1756   frv_stack_regs_t *regs_info;
1757   int regno;
1758
1759   regs_info = &info->regs[reg_set];
1760   for (regno = regs_info->first; regno <= regs_info->last; regno++)
1761     if (info->save_p[regno])
1762       frv_frame_access (accessor,
1763                         info->save_p[regno] == REG_SAVE_2WORDS
1764                         ? gen_rtx_REG (DImode, regno)
1765                         : gen_rtx_REG (SImode, regno),
1766                         info->reg_offset[regno]);
1767 }
1768
1769 /* Save or restore callee-saved registers that are kept outside the frame
1770    header.  The function saves the registers if OP is FRV_STORE and restores
1771    them if OP is FRV_LOAD.  INFO is the stack information generated by
1772    frv_stack_info.  */
1773 static void
1774 frv_frame_access_standard_regs (enum frv_stack_op op, frv_stack_t *info)
1775 {
1776   frv_frame_accessor_t accessor;
1777
1778   accessor.op = op;
1779   accessor.base = stack_pointer_rtx;
1780   accessor.base_offset = 0;
1781   frv_frame_access_multi (&accessor, info, STACK_REGS_GPR);
1782   frv_frame_access_multi (&accessor, info, STACK_REGS_FPR);
1783   frv_frame_access_multi (&accessor, info, STACK_REGS_LCR);
1784 }
1785
1786
1787 /* Called after register allocation to add any instructions needed for the
1788    prologue.  Using a prologue insn is favored compared to putting all of the
1789    instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
1790    it allows the scheduler to intermix instructions with the saves of
1791    the caller saved registers.  In some cases, it might be necessary
1792    to emit a barrier instruction as the last insn to prevent such
1793    scheduling.
1794
1795    Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
1796    so that the debug info generation code can handle them properly.  */
1797 void
1798 frv_expand_prologue (void)
1799 {
1800   frv_stack_t *info = frv_stack_info ();
1801   rtx sp = stack_pointer_rtx;
1802   rtx fp = frame_pointer_rtx;
1803   frv_frame_accessor_t accessor;
1804
1805   if (TARGET_DEBUG_STACK)
1806     frv_debug_stack (info);
1807
1808   if (info->total_size == 0)
1809     return;
1810
1811   /* We're interested in three areas of the frame here:
1812
1813          A: the register save area
1814          B: the old FP
1815          C: the header after B
1816
1817      If the frame pointer isn't used, we'll have to set up A, B and C
1818      using the stack pointer.  If the frame pointer is used, we'll access
1819      them as follows:
1820
1821          A: set up using sp
1822          B: set up using sp or a temporary (see below)
1823          C: set up using fp
1824
1825      We set up B using the stack pointer if the frame is small enough.
1826      Otherwise, it's more efficient to copy the old stack pointer into a
1827      temporary and use that.
1828
1829      Note that it's important to make sure the prologue and epilogue use the
1830      same registers to access A and C, since doing otherwise will confuse
1831      the aliasing code.  */
1832
1833   /* Set up ACCESSOR for accessing region B above.  If the frame pointer
1834      isn't used, the same method will serve for C.  */
1835   accessor.op = FRV_STORE;
1836   if (frame_pointer_needed && info->total_size > 2048)
1837     {
1838       rtx insn;
1839
1840       accessor.base = gen_rtx_REG (Pmode, OLD_SP_REGNO);
1841       accessor.base_offset = info->total_size;
1842       insn = emit_insn (gen_movsi (accessor.base, sp));
1843     }
1844   else
1845     {
1846       accessor.base = stack_pointer_rtx;
1847       accessor.base_offset = 0;
1848     }
1849
1850   /* Allocate the stack space.  */
1851   {
1852     rtx asm_offset = frv_frame_offset_rtx (-info->total_size);
1853     rtx dwarf_offset = GEN_INT (-info->total_size);
1854
1855     frv_frame_insn (gen_stack_adjust (sp, sp, asm_offset),
1856                     gen_rtx_SET (Pmode,
1857                                  sp,
1858                                  gen_rtx_PLUS (Pmode, sp, dwarf_offset)));
1859   }
1860
1861   /* If the frame pointer is needed, store the old one at (sp + FP_OFFSET)
1862      and point the new one to that location.  */
1863   if (frame_pointer_needed)
1864     {
1865       int fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];
1866
1867       /* ASM_SRC and DWARF_SRC both point to the frame header.  ASM_SRC is
1868          based on ACCESSOR.BASE but DWARF_SRC is always based on the stack
1869          pointer.  */
1870       rtx asm_src = plus_constant (accessor.base,
1871                                    fp_offset - accessor.base_offset);
1872       rtx dwarf_src = plus_constant (sp, fp_offset);
1873
1874       /* Store the old frame pointer at (sp + FP_OFFSET).  */
1875       frv_frame_access (&accessor, fp, fp_offset);
1876
1877       /* Set up the new frame pointer.  */
1878       frv_frame_insn (gen_rtx_SET (VOIDmode, fp, asm_src),
1879                       gen_rtx_SET (VOIDmode, fp, dwarf_src));
1880
1881       /* Access region C from the frame pointer.  */
1882       accessor.base = fp;
1883       accessor.base_offset = fp_offset;
1884     }
1885
1886   /* Set up region C.  */
1887   frv_frame_access_multi (&accessor, info, STACK_REGS_STRUCT);
1888   frv_frame_access_multi (&accessor, info, STACK_REGS_LR);
1889   frv_frame_access_multi (&accessor, info, STACK_REGS_STDARG);
1890
1891   /* Set up region A.  */
1892   frv_frame_access_standard_regs (FRV_STORE, info);
1893
1894   /* If this is a varargs/stdarg function, issue a blockage to prevent the
1895      scheduler from moving loads before the stores saving the registers.  */
1896   if (info->stdarg_size > 0)
1897     emit_insn (gen_blockage ());
1898
1899   /* Set up pic register/small data register for this function.  */
1900   if (!TARGET_FDPIC && flag_pic && crtl->uses_pic_offset_table)
1901     emit_insn (gen_pic_prologue (gen_rtx_REG (Pmode, PIC_REGNO),
1902                                  gen_rtx_REG (Pmode, LR_REGNO),
1903                                  gen_rtx_REG (SImode, OFFSET_REGNO)));
1904 }
1905
1906 \f
1907 /* Under frv, all of the work is done via frv_expand_epilogue, but
1908    this function provides a convenient place to do cleanup.  */
1909
1910 static void
1911 frv_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
1912                        HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1913 {
1914   frv_stack_cache = (frv_stack_t *)0;
1915
1916   /* Zap last used registers for conditional execution.  */
1917   memset (&frv_ifcvt.tmp_reg, 0, sizeof (frv_ifcvt.tmp_reg));
1918
1919   /* Release the bitmap of created insns.  */
1920   BITMAP_FREE (frv_ifcvt.scratch_insns_bitmap);
1921 }
1922
1923 \f
1924 /* Called after register allocation to add any instructions needed for the
1925    epilogue.  Using an epilogue insn is favored compared to putting all of the
1926    instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
1927    it allows the scheduler to intermix instructions with the saves of
1928    the caller saved registers.  In some cases, it might be necessary
1929    to emit a barrier instruction as the last insn to prevent such
1930    scheduling.  */
1931
1932 void
1933 frv_expand_epilogue (bool emit_return)
1934 {
1935   frv_stack_t *info = frv_stack_info ();
1936   rtx fp = frame_pointer_rtx;
1937   rtx sp = stack_pointer_rtx;
1938   rtx return_addr;
1939   int fp_offset;
1940
1941   fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];
1942
1943   /* Restore the stack pointer to its original value if alloca or the like
1944      is used.  */
1945   if (! current_function_sp_is_unchanging)
1946     emit_insn (gen_addsi3 (sp, fp, frv_frame_offset_rtx (-fp_offset)));
1947
1948   /* Restore the callee-saved registers that were used in this function.  */
1949   frv_frame_access_standard_regs (FRV_LOAD, info);
1950
1951   /* Set RETURN_ADDR to the address we should return to.  Set it to NULL if
1952      no return instruction should be emitted.  */
1953   if (info->save_p[LR_REGNO])
1954     {
1955       int lr_offset;
1956       rtx mem;
1957
1958       /* Use the same method to access the link register's slot as we did in
1959          the prologue.  In other words, use the frame pointer if available,
1960          otherwise use the stack pointer.
1961
1962          LR_OFFSET is the offset of the link register's slot from the start
1963          of the frame and MEM is a memory rtx for it.  */
1964       lr_offset = info->reg_offset[LR_REGNO];
1965       if (frame_pointer_needed)
1966         mem = frv_frame_mem (Pmode, fp, lr_offset - fp_offset);
1967       else
1968         mem = frv_frame_mem (Pmode, sp, lr_offset);
1969
1970       /* Load the old link register into a GPR.  */
1971       return_addr = gen_rtx_REG (Pmode, TEMP_REGNO);
1972       emit_insn (gen_rtx_SET (VOIDmode, return_addr, mem));
1973     }
1974   else
1975     return_addr = gen_rtx_REG (Pmode, LR_REGNO);
1976
1977   /* Restore the old frame pointer.  Emit a USE afterwards to make sure
1978      the load is preserved.  */
1979   if (frame_pointer_needed)
1980     {
1981       emit_insn (gen_rtx_SET (VOIDmode, fp, gen_rtx_MEM (Pmode, fp)));
1982       emit_use (fp);
1983     }
1984
1985   /* Deallocate the stack frame.  */
1986   if (info->total_size != 0)
1987     {
1988       rtx offset = frv_frame_offset_rtx (info->total_size);
1989       emit_insn (gen_stack_adjust (sp, sp, offset));
1990     }
1991
1992   /* If this function uses eh_return, add the final stack adjustment now.  */
1993   if (crtl->calls_eh_return)
1994     emit_insn (gen_stack_adjust (sp, sp, EH_RETURN_STACKADJ_RTX));
1995
1996   if (emit_return)
1997     emit_jump_insn (gen_epilogue_return (return_addr));
1998   else
1999     {
2000       rtx lr = return_addr;
2001
2002       if (REGNO (return_addr) != LR_REGNO)
2003         {
2004           lr = gen_rtx_REG (Pmode, LR_REGNO);
2005           emit_move_insn (lr, return_addr);
2006         }
2007
2008       emit_use (lr);
2009     }
2010 }
2011
2012 \f
2013 /* Worker function for TARGET_ASM_OUTPUT_MI_THUNK.  */
2014
2015 static void
2016 frv_asm_output_mi_thunk (FILE *file,
2017                          tree thunk_fndecl ATTRIBUTE_UNUSED,
2018                          HOST_WIDE_INT delta,
2019                          HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2020                          tree function)
2021 {
2022   const char *name_func = XSTR (XEXP (DECL_RTL (function), 0), 0);
2023   const char *name_arg0 = reg_names[FIRST_ARG_REGNUM];
2024   const char *name_jmp = reg_names[JUMP_REGNO];
2025   const char *parallel = (frv_issue_rate () > 1 ? ".p" : "");
2026
2027   /* Do the add using an addi if possible.  */
2028   if (IN_RANGE_P (delta, -2048, 2047))
2029     fprintf (file, "\taddi %s,#%d,%s\n", name_arg0, (int) delta, name_arg0);
2030   else
2031     {
2032       const char *const name_add = reg_names[TEMP_REGNO];
2033       fprintf (file, "\tsethi%s #hi(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
2034                parallel, delta, name_add);
2035       fprintf (file, "\tsetlo #lo(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
2036                delta, name_add);
2037       fprintf (file, "\tadd %s,%s,%s\n", name_add, name_arg0, name_arg0);
2038     }
2039
2040   if (TARGET_FDPIC)
2041     {
2042       const char *name_pic = reg_names[FDPIC_REGNO];
2043       name_jmp = reg_names[FDPIC_FPTR_REGNO];
2044
2045       if (flag_pic != 1)
2046         {
2047           fprintf (file, "\tsethi%s #gotofffuncdeschi(", parallel);
2048           assemble_name (file, name_func);
2049           fprintf (file, "),%s\n", name_jmp);
2050
2051           fprintf (file, "\tsetlo #gotofffuncdesclo(");
2052           assemble_name (file, name_func);
2053           fprintf (file, "),%s\n", name_jmp);
2054
2055           fprintf (file, "\tldd @(%s,%s), %s\n", name_jmp, name_pic, name_jmp);
2056         }
2057       else
2058         {
2059           fprintf (file, "\tlddo @(%s,#gotofffuncdesc12(", name_pic);
2060           assemble_name (file, name_func);
2061           fprintf (file, "\t)), %s\n", name_jmp);
2062         }
2063     }
2064   else if (!flag_pic)
2065     {
2066       fprintf (file, "\tsethi%s #hi(", parallel);
2067       assemble_name (file, name_func);
2068       fprintf (file, "),%s\n", name_jmp);
2069
2070       fprintf (file, "\tsetlo #lo(");
2071       assemble_name (file, name_func);
2072       fprintf (file, "),%s\n", name_jmp);
2073     }
2074   else
2075     {
2076       /* Use JUMP_REGNO as a temporary PIC register.  */
2077       const char *name_lr = reg_names[LR_REGNO];
2078       const char *name_gppic = name_jmp;
2079       const char *name_tmp = reg_names[TEMP_REGNO];
2080
2081       fprintf (file, "\tmovsg %s,%s\n", name_lr, name_tmp);
2082       fprintf (file, "\tcall 1f\n");
2083       fprintf (file, "1:\tmovsg %s,%s\n", name_lr, name_gppic);
2084       fprintf (file, "\tmovgs %s,%s\n", name_tmp, name_lr);
2085       fprintf (file, "\tsethi%s #gprelhi(1b),%s\n", parallel, name_tmp);
2086       fprintf (file, "\tsetlo #gprello(1b),%s\n", name_tmp);
2087       fprintf (file, "\tsub %s,%s,%s\n", name_gppic, name_tmp, name_gppic);
2088
2089       fprintf (file, "\tsethi%s #gprelhi(", parallel);
2090       assemble_name (file, name_func);
2091       fprintf (file, "),%s\n", name_tmp);
2092
2093       fprintf (file, "\tsetlo #gprello(");
2094       assemble_name (file, name_func);
2095       fprintf (file, "),%s\n", name_tmp);
2096
2097       fprintf (file, "\tadd %s,%s,%s\n", name_gppic, name_tmp, name_jmp);
2098     }
2099
2100   /* Jump to the function address.  */
2101   fprintf (file, "\tjmpl @(%s,%s)\n", name_jmp, reg_names[GPR_FIRST+0]);
2102 }
2103
2104 \f
2105 /* A C expression which is nonzero if a function must have and use a frame
2106    pointer.  This expression is evaluated in the reload pass.  If its value is
2107    nonzero the function will have a frame pointer.
2108
2109    The expression can in principle examine the current function and decide
2110    according to the facts, but on most machines the constant 0 or the constant
2111    1 suffices.  Use 0 when the machine allows code to be generated with no
2112    frame pointer, and doing so saves some time or space.  Use 1 when there is
2113    no possible advantage to avoiding a frame pointer.
2114
2115    In certain cases, the compiler does not know how to produce valid code
2116    without a frame pointer.  The compiler recognizes those cases and
2117    automatically gives the function a frame pointer regardless of what
2118    `FRAME_POINTER_REQUIRED' says.  You don't need to worry about them.
2119
2120    In a function that does not require a frame pointer, the frame pointer
2121    register can be allocated for ordinary usage, unless you mark it as a fixed
2122    register.  See `FIXED_REGISTERS' for more information.  */
2123
2124 /* On frv, create a frame whenever we need to create stack.  */
2125
2126 int
2127 frv_frame_pointer_required (void)
2128 {
2129   /* If we forgoing the usual linkage requirements, we only need
2130      a frame pointer if the stack pointer might change.  */
2131   if (!TARGET_LINKED_FP)
2132     return !current_function_sp_is_unchanging;
2133
2134   if (! current_function_is_leaf)
2135     return TRUE;
2136
2137   if (get_frame_size () != 0)
2138     return TRUE;
2139
2140   if (cfun->stdarg)
2141     return TRUE;
2142
2143   if (!current_function_sp_is_unchanging)
2144     return TRUE;
2145
2146   if (!TARGET_FDPIC && flag_pic && crtl->uses_pic_offset_table)
2147     return TRUE;
2148
2149   if (profile_flag)
2150     return TRUE;
2151
2152   if (cfun->machine->frame_needed)
2153     return TRUE;
2154
2155   return FALSE;
2156 }
2157
2158 \f
2159 /* This macro is similar to `INITIAL_FRAME_POINTER_OFFSET'.  It specifies the
2160    initial difference between the specified pair of registers.  This macro must
2161    be defined if `ELIMINABLE_REGS' is defined.  */
2162
2163 /* See frv_stack_info for more details on the frv stack frame.  */
2164
2165 int
2166 frv_initial_elimination_offset (int from, int to)
2167 {
2168   frv_stack_t *info = frv_stack_info ();
2169   int ret = 0;
2170
2171   if (to == STACK_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
2172     ret = info->total_size - info->pretend_size;
2173
2174   else if (to == STACK_POINTER_REGNUM && from == FRAME_POINTER_REGNUM)
2175     ret = info->reg_offset[FRAME_POINTER_REGNUM];
2176
2177   else if (to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
2178     ret = (info->total_size
2179            - info->reg_offset[FRAME_POINTER_REGNUM]
2180            - info->pretend_size);
2181
2182   else
2183     gcc_unreachable ();
2184
2185   if (TARGET_DEBUG_STACK)
2186     fprintf (stderr, "Eliminate %s to %s by adding %d\n",
2187              reg_names [from], reg_names[to], ret);
2188
2189   return ret;
2190 }
2191
2192 \f
2193 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
2194
2195 static void
2196 frv_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
2197                             enum machine_mode mode,
2198                             tree type ATTRIBUTE_UNUSED,
2199                             int *pretend_size,
2200                             int second_time)
2201 {
2202   if (TARGET_DEBUG_ARG)
2203     fprintf (stderr,
2204              "setup_vararg: words = %2d, mode = %4s, pretend_size = %d, second_time = %d\n",
2205              *cum, GET_MODE_NAME (mode), *pretend_size, second_time);
2206 }
2207
2208 \f
2209 /* Worker function for TARGET_EXPAND_BUILTIN_SAVEREGS.  */
2210
2211 static rtx
2212 frv_expand_builtin_saveregs (void)
2213 {
2214   int offset = UNITS_PER_WORD * FRV_NUM_ARG_REGS;
2215
2216   if (TARGET_DEBUG_ARG)
2217     fprintf (stderr, "expand_builtin_saveregs: offset from ap = %d\n",
2218              offset);
2219
2220   return gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx, GEN_INT (- offset));
2221 }
2222
2223 \f
2224 /* Expand __builtin_va_start to do the va_start macro.  */
2225
2226 static void
2227 frv_expand_builtin_va_start (tree valist, rtx nextarg)
2228 {
2229   tree t;
2230   int num = crtl->args.info - FIRST_ARG_REGNUM - FRV_NUM_ARG_REGS;
2231
2232   nextarg = gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx,
2233                           GEN_INT (UNITS_PER_WORD * num));
2234
2235   if (TARGET_DEBUG_ARG)
2236     {
2237       fprintf (stderr, "va_start: args_info = %d, num = %d\n",
2238                crtl->args.info, num);
2239
2240       debug_rtx (nextarg);
2241     }
2242
2243   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
2244               fold_convert (TREE_TYPE (valist),
2245                             make_tree (sizetype, nextarg)));
2246   TREE_SIDE_EFFECTS (t) = 1;
2247
2248   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2249 }
2250
2251 \f
2252 /* Expand a block move operation, and return 1 if successful.  Return 0
2253    if we should let the compiler generate normal code.
2254
2255    operands[0] is the destination
2256    operands[1] is the source
2257    operands[2] is the length
2258    operands[3] is the alignment */
2259
2260 /* Maximum number of loads to do before doing the stores */
2261 #ifndef MAX_MOVE_REG
2262 #define MAX_MOVE_REG 4
2263 #endif
2264
2265 /* Maximum number of total loads to do.  */
2266 #ifndef TOTAL_MOVE_REG
2267 #define TOTAL_MOVE_REG 8
2268 #endif
2269
2270 int
2271 frv_expand_block_move (rtx operands[])
2272 {
2273   rtx orig_dest = operands[0];
2274   rtx orig_src  = operands[1];
2275   rtx bytes_rtx = operands[2];
2276   rtx align_rtx = operands[3];
2277   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
2278   int align;
2279   int bytes;
2280   int offset;
2281   int num_reg;
2282   int i;
2283   rtx src_reg;
2284   rtx dest_reg;
2285   rtx src_addr;
2286   rtx dest_addr;
2287   rtx src_mem;
2288   rtx dest_mem;
2289   rtx tmp_reg;
2290   rtx stores[MAX_MOVE_REG];
2291   int move_bytes;
2292   enum machine_mode mode;
2293
2294   /* If this is not a fixed size move, just call memcpy.  */
2295   if (! constp)
2296     return FALSE;
2297
2298   /* This should be a fixed size alignment.  */
2299   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
2300
2301   align = INTVAL (align_rtx);
2302
2303   /* Anything to move? */
2304   bytes = INTVAL (bytes_rtx);
2305   if (bytes <= 0)
2306     return TRUE;
2307
2308   /* Don't support real large moves.  */
2309   if (bytes > TOTAL_MOVE_REG*align)
2310     return FALSE;
2311
2312   /* Move the address into scratch registers.  */
2313   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
2314   src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));
2315
2316   num_reg = offset = 0;
2317   for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
2318     {
2319       /* Calculate the correct offset for src/dest.  */
2320       if (offset == 0)
2321         {
2322           src_addr  = src_reg;
2323           dest_addr = dest_reg;
2324         }
2325       else
2326         {
2327           src_addr = plus_constant (src_reg, offset);
2328           dest_addr = plus_constant (dest_reg, offset);
2329         }
2330
2331       /* Generate the appropriate load and store, saving the stores
2332          for later.  */
2333       if (bytes >= 4 && align >= 4)
2334         mode = SImode;
2335       else if (bytes >= 2 && align >= 2)
2336         mode = HImode;
2337       else
2338         mode = QImode;
2339
2340       move_bytes = GET_MODE_SIZE (mode);
2341       tmp_reg = gen_reg_rtx (mode);
2342       src_mem = change_address (orig_src, mode, src_addr);
2343       dest_mem = change_address (orig_dest, mode, dest_addr);
2344       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg, src_mem));
2345       stores[num_reg++] = gen_rtx_SET (VOIDmode, dest_mem, tmp_reg);
2346
2347       if (num_reg >= MAX_MOVE_REG)
2348         {
2349           for (i = 0; i < num_reg; i++)
2350             emit_insn (stores[i]);
2351           num_reg = 0;
2352         }
2353     }
2354
2355   for (i = 0; i < num_reg; i++)
2356     emit_insn (stores[i]);
2357
2358   return TRUE;
2359 }
2360
2361 \f
2362 /* Expand a block clear operation, and return 1 if successful.  Return 0
2363    if we should let the compiler generate normal code.
2364
2365    operands[0] is the destination
2366    operands[1] is the length
2367    operands[3] is the alignment */
2368
2369 int
2370 frv_expand_block_clear (rtx operands[])
2371 {
2372   rtx orig_dest = operands[0];
2373   rtx bytes_rtx = operands[1];
2374   rtx align_rtx = operands[3];
2375   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
2376   int align;
2377   int bytes;
2378   int offset;
2379   int num_reg;
2380   rtx dest_reg;
2381   rtx dest_addr;
2382   rtx dest_mem;
2383   int clear_bytes;
2384   enum machine_mode mode;
2385
2386   /* If this is not a fixed size move, just call memcpy.  */
2387   if (! constp)
2388     return FALSE;
2389
2390   /* This should be a fixed size alignment.  */
2391   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
2392
2393   align = INTVAL (align_rtx);
2394
2395   /* Anything to move? */
2396   bytes = INTVAL (bytes_rtx);
2397   if (bytes <= 0)
2398     return TRUE;
2399
2400   /* Don't support real large clears.  */
2401   if (bytes > TOTAL_MOVE_REG*align)
2402     return FALSE;
2403
2404   /* Move the address into a scratch register.  */
2405   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
2406
2407   num_reg = offset = 0;
2408   for ( ; bytes > 0; (bytes -= clear_bytes), (offset += clear_bytes))
2409     {
2410       /* Calculate the correct offset for src/dest.  */
2411       dest_addr = ((offset == 0)
2412                    ? dest_reg
2413                    : plus_constant (dest_reg, offset));
2414
2415       /* Generate the appropriate store of gr0.  */
2416       if (bytes >= 4 && align >= 4)
2417         mode = SImode;
2418       else if (bytes >= 2 && align >= 2)
2419         mode = HImode;
2420       else
2421         mode = QImode;
2422
2423       clear_bytes = GET_MODE_SIZE (mode);
2424       dest_mem = change_address (orig_dest, mode, dest_addr);
2425       emit_insn (gen_rtx_SET (VOIDmode, dest_mem, const0_rtx));
2426     }
2427
2428   return TRUE;
2429 }
2430
2431 \f
2432 /* The following variable is used to output modifiers of assembler
2433    code of the current output insn.  */
2434
2435 static rtx *frv_insn_operands;
2436
2437 /* The following function is used to add assembler insn code suffix .p
2438    if it is necessary.  */
2439
2440 const char *
2441 frv_asm_output_opcode (FILE *f, const char *ptr)
2442 {
2443   int c;
2444
2445   if (frv_insn_packing_flag <= 0)
2446     return ptr;
2447
2448   for (; *ptr && *ptr != ' ' && *ptr != '\t';)
2449     {
2450       c = *ptr++;
2451       if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
2452                        || (*ptr >= 'A' && *ptr <= 'Z')))
2453         {
2454           int letter = *ptr++;
2455
2456           c = atoi (ptr);
2457           frv_print_operand (f, frv_insn_operands [c], letter);
2458           while ((c = *ptr) >= '0' && c <= '9')
2459             ptr++;
2460         }
2461       else
2462         fputc (c, f);
2463     }
2464
2465   fprintf (f, ".p");
2466
2467   return ptr;
2468 }
2469
2470 /* Set up the packing bit for the current output insn.  Note that this
2471    function is not called for asm insns.  */
2472
2473 void
2474 frv_final_prescan_insn (rtx insn, rtx *opvec,
2475                         int noperands ATTRIBUTE_UNUSED)
2476 {
2477   if (INSN_P (insn))
2478     {
2479       if (frv_insn_packing_flag >= 0)
2480         {
2481           frv_insn_operands = opvec;
2482           frv_insn_packing_flag = PACKING_FLAG_P (insn);
2483         }
2484       else if (recog_memoized (insn) >= 0
2485                && get_attr_acc_group (insn) == ACC_GROUP_ODD)
2486         /* Packing optimizations have been disabled, but INSN can only
2487            be issued in M1.  Insert an mnop in M0.  */
2488         fprintf (asm_out_file, "\tmnop.p\n");
2489     }
2490 }
2491
2492
2493 \f
2494 /* A C expression whose value is RTL representing the address in a stack frame
2495    where the pointer to the caller's frame is stored.  Assume that FRAMEADDR is
2496    an RTL expression for the address of the stack frame itself.
2497
2498    If you don't define this macro, the default is to return the value of
2499    FRAMEADDR--that is, the stack frame address is also the address of the stack
2500    word that points to the previous frame.  */
2501
2502 /* The default is correct, but we need to make sure the frame gets created.  */
2503 rtx
2504 frv_dynamic_chain_address (rtx frame)
2505 {
2506   cfun->machine->frame_needed = 1;
2507   return frame;
2508 }
2509
2510
2511 /* A C expression whose value is RTL representing the value of the return
2512    address for the frame COUNT steps up from the current frame, after the
2513    prologue.  FRAMEADDR is the frame pointer of the COUNT frame, or the frame
2514    pointer of the COUNT - 1 frame if `RETURN_ADDR_IN_PREVIOUS_FRAME' is
2515    defined.
2516
2517    The value of the expression must always be the correct address when COUNT is
2518    zero, but may be `NULL_RTX' if there is not way to determine the return
2519    address of other frames.  */
2520
2521 rtx
2522 frv_return_addr_rtx (int count, rtx frame)
2523 {
2524   if (count != 0)
2525     return const0_rtx;
2526   cfun->machine->frame_needed = 1;
2527   return gen_rtx_MEM (Pmode, plus_constant (frame, 8));
2528 }
2529
2530 /* Given a memory reference MEMREF, interpret the referenced memory as
2531    an array of MODE values, and return a reference to the element
2532    specified by INDEX.  Assume that any pre-modification implicit in
2533    MEMREF has already happened.
2534
2535    MEMREF must be a legitimate operand for modes larger than SImode.
2536    GO_IF_LEGITIMATE_ADDRESS forbids register+register addresses, which
2537    this function cannot handle.  */
2538 rtx
2539 frv_index_memory (rtx memref, enum machine_mode mode, int index)
2540 {
2541   rtx base = XEXP (memref, 0);
2542   if (GET_CODE (base) == PRE_MODIFY)
2543     base = XEXP (base, 0);
2544   return change_address (memref, mode,
2545                          plus_constant (base, index * GET_MODE_SIZE (mode)));
2546 }
2547
2548 \f
2549 /* Print a memory address as an operand to reference that memory location.  */
2550 void
2551 frv_print_operand_address (FILE * stream, rtx x)
2552 {
2553   if (GET_CODE (x) == MEM)
2554     x = XEXP (x, 0);
2555
2556   switch (GET_CODE (x))
2557     {
2558     case REG:
2559       fputs (reg_names [ REGNO (x)], stream);
2560       return;
2561
2562     case CONST_INT:
2563       fprintf (stream, "%ld", (long) INTVAL (x));
2564       return;
2565
2566     case SYMBOL_REF:
2567       assemble_name (stream, XSTR (x, 0));
2568       return;
2569
2570     case LABEL_REF:
2571     case CONST:
2572       output_addr_const (stream, x);
2573       return;
2574
2575     case PLUS:
2576       /* Poorly constructed asm statements can trigger this alternative.
2577          See gcc/testsuite/gcc.dg/asm-4.c for an example.  */
2578       frv_print_operand_memory_reference (stream, x, 0);
2579       return;
2580       
2581     default:
2582       break;
2583     }
2584
2585   fatal_insn ("bad insn to frv_print_operand_address:", x);
2586 }
2587
2588 \f
2589 static void
2590 frv_print_operand_memory_reference_reg (FILE * stream, rtx x)
2591 {
2592   int regno = true_regnum (x);
2593   if (GPR_P (regno))
2594     fputs (reg_names[regno], stream);
2595   else
2596     fatal_insn ("bad register to frv_print_operand_memory_reference_reg:", x);
2597 }
2598
2599 /* Print a memory reference suitable for the ld/st instructions.  */
2600
2601 static void
2602 frv_print_operand_memory_reference (FILE * stream, rtx x, int addr_offset)
2603 {
2604   struct frv_unspec unspec;
2605   rtx x0 = NULL_RTX;
2606   rtx x1 = NULL_RTX;
2607
2608   switch (GET_CODE (x))
2609     {
2610     case SUBREG:
2611     case REG:
2612       x0 = x;
2613       break;
2614
2615     case PRE_MODIFY:            /* (pre_modify (reg) (plus (reg) (reg))) */
2616       x0 = XEXP (x, 0);
2617       x1 = XEXP (XEXP (x, 1), 1);
2618       break;
2619
2620     case CONST_INT:
2621       x1 = x;
2622       break;
2623
2624     case PLUS:
2625       x0 = XEXP (x, 0);
2626       x1 = XEXP (x, 1);
2627       if (GET_CODE (x0) == CONST_INT)
2628         {
2629           x0 = XEXP (x, 1);
2630           x1 = XEXP (x, 0);
2631         }
2632       break;
2633
2634     default:
2635       fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
2636       break;
2637
2638     }
2639
2640   if (addr_offset)
2641     {
2642       if (!x1)
2643         x1 = const0_rtx;
2644       else if (GET_CODE (x1) != CONST_INT)
2645         fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
2646     }
2647
2648   fputs ("@(", stream);
2649   if (!x0)
2650     fputs (reg_names[GPR_R0], stream);
2651   else if (GET_CODE (x0) == REG || GET_CODE (x0) == SUBREG)
2652     frv_print_operand_memory_reference_reg (stream, x0);
2653   else
2654     fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
2655
2656   fputs (",", stream);
2657   if (!x1)
2658     fputs (reg_names [GPR_R0], stream);
2659
2660   else
2661     {
2662       switch (GET_CODE (x1))
2663         {
2664         case SUBREG:
2665         case REG:
2666           frv_print_operand_memory_reference_reg (stream, x1);
2667           break;
2668
2669         case CONST_INT:
2670           fprintf (stream, "%ld", (long) (INTVAL (x1) + addr_offset));
2671           break;
2672
2673         case CONST:
2674           if (!frv_const_unspec_p (x1, &unspec))
2675             fatal_insn ("bad insn to frv_print_operand_memory_reference:", x1);
2676           frv_output_const_unspec (stream, &unspec);
2677           break;
2678
2679         default:
2680           fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
2681         }
2682     }
2683
2684   fputs (")", stream);
2685 }
2686
2687 \f
2688 /* Return 2 for likely branches and 0 for non-likely branches  */
2689
2690 #define FRV_JUMP_LIKELY 2
2691 #define FRV_JUMP_NOT_LIKELY 0
2692
2693 static int
2694 frv_print_operand_jump_hint (rtx insn)
2695 {
2696   rtx note;
2697   rtx labelref;
2698   int ret;
2699   HOST_WIDE_INT prob = -1;
2700   enum { UNKNOWN, BACKWARD, FORWARD } jump_type = UNKNOWN;
2701
2702   gcc_assert (GET_CODE (insn) == JUMP_INSN);
2703
2704   /* Assume any non-conditional jump is likely.  */
2705   if (! any_condjump_p (insn))
2706     ret = FRV_JUMP_LIKELY;
2707
2708   else
2709     {
2710       labelref = condjump_label (insn);
2711       if (labelref)
2712         {
2713           rtx label = XEXP (labelref, 0);
2714           jump_type = (insn_current_address > INSN_ADDRESSES (INSN_UID (label))
2715                        ? BACKWARD
2716                        : FORWARD);
2717         }
2718
2719       note = find_reg_note (insn, REG_BR_PROB, 0);
2720       if (!note)
2721         ret = ((jump_type == BACKWARD) ? FRV_JUMP_LIKELY : FRV_JUMP_NOT_LIKELY);
2722
2723       else
2724         {
2725           prob = INTVAL (XEXP (note, 0));
2726           ret = ((prob >= (REG_BR_PROB_BASE / 2))
2727                  ? FRV_JUMP_LIKELY
2728                  : FRV_JUMP_NOT_LIKELY);
2729         }
2730     }
2731
2732 #if 0
2733   if (TARGET_DEBUG)
2734     {
2735       char *direction;
2736
2737       switch (jump_type)
2738         {
2739         default:
2740         case UNKNOWN:   direction = "unknown jump direction";   break;
2741         case BACKWARD:  direction = "jump backward";            break;
2742         case FORWARD:   direction = "jump forward";             break;
2743         }
2744
2745       fprintf (stderr,
2746                "%s: uid %ld, %s, probability = %ld, max prob. = %ld, hint = %d\n",
2747                IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
2748                (long)INSN_UID (insn), direction, (long)prob,
2749                (long)REG_BR_PROB_BASE, ret);
2750     }
2751 #endif
2752
2753   return ret;
2754 }
2755
2756 \f
2757 /* Return the comparison operator to use for CODE given that the ICC
2758    register is OP0.  */
2759
2760 static const char *
2761 comparison_string (enum rtx_code code, rtx op0)
2762 {
2763   bool is_nz_p = GET_MODE (op0) == CC_NZmode;
2764   switch (code)
2765     {
2766     default:  output_operand_lossage ("bad condition code");
2767     case EQ:  return "eq";
2768     case NE:  return "ne";
2769     case LT:  return is_nz_p ? "n" : "lt";
2770     case LE:  return "le";
2771     case GT:  return "gt";
2772     case GE:  return is_nz_p ? "p" : "ge";
2773     case LTU: return is_nz_p ? "no" : "c";
2774     case LEU: return is_nz_p ? "eq" : "ls";
2775     case GTU: return is_nz_p ? "ne" : "hi";
2776     case GEU: return is_nz_p ? "ra" : "nc";
2777     }
2778 }
2779
2780 /* Print an operand to an assembler instruction.
2781
2782    `%' followed by a letter and a digit says to output an operand in an
2783    alternate fashion.  Four letters have standard, built-in meanings described
2784    below.  The machine description macro `PRINT_OPERAND' can define additional
2785    letters with nonstandard meanings.
2786
2787    `%cDIGIT' can be used to substitute an operand that is a constant value
2788    without the syntax that normally indicates an immediate operand.
2789
2790    `%nDIGIT' is like `%cDIGIT' except that the value of the constant is negated
2791    before printing.
2792
2793    `%aDIGIT' can be used to substitute an operand as if it were a memory
2794    reference, with the actual operand treated as the address.  This may be
2795    useful when outputting a "load address" instruction, because often the
2796    assembler syntax for such an instruction requires you to write the operand
2797    as if it were a memory reference.
2798
2799    `%lDIGIT' is used to substitute a `label_ref' into a jump instruction.
2800
2801    `%=' outputs a number which is unique to each instruction in the entire
2802    compilation.  This is useful for making local labels to be referred to more
2803    than once in a single template that generates multiple assembler
2804    instructions.
2805
2806    `%' followed by a punctuation character specifies a substitution that does
2807    not use an operand.  Only one case is standard: `%%' outputs a `%' into the
2808    assembler code.  Other nonstandard cases can be defined in the
2809    `PRINT_OPERAND' macro.  You must also define which punctuation characters
2810    are valid with the `PRINT_OPERAND_PUNCT_VALID_P' macro.  */
2811
2812 void
2813 frv_print_operand (FILE * file, rtx x, int code)
2814 {
2815   struct frv_unspec unspec;
2816   HOST_WIDE_INT value;
2817   int offset;
2818
2819   if (code != 0 && !ISALPHA (code))
2820     value = 0;
2821
2822   else if (GET_CODE (x) == CONST_INT)
2823     value = INTVAL (x);
2824
2825   else if (GET_CODE (x) == CONST_DOUBLE)
2826     {
2827       if (GET_MODE (x) == SFmode)
2828         {
2829           REAL_VALUE_TYPE rv;
2830           long l;
2831
2832           REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
2833           REAL_VALUE_TO_TARGET_SINGLE (rv, l);
2834           value = l;
2835         }
2836
2837       else if (GET_MODE (x) == VOIDmode)
2838         value = CONST_DOUBLE_LOW (x);
2839
2840       else
2841         fatal_insn ("bad insn in frv_print_operand, bad const_double", x);
2842     }
2843
2844   else
2845     value = 0;
2846
2847   switch (code)
2848     {
2849
2850     case '.':
2851       /* Output r0.  */
2852       fputs (reg_names[GPR_R0], file);
2853       break;
2854
2855     case '#':
2856       fprintf (file, "%d", frv_print_operand_jump_hint (current_output_insn));
2857       break;
2858
2859     case '@':
2860       /* Output small data area base register (gr16).  */
2861       fputs (reg_names[SDA_BASE_REG], file);
2862       break;
2863
2864     case '~':
2865       /* Output pic register (gr17).  */
2866       fputs (reg_names[PIC_REGNO], file);
2867       break;
2868
2869     case '*':
2870       /* Output the temporary integer CCR register.  */
2871       fputs (reg_names[ICR_TEMP], file);
2872       break;
2873
2874     case '&':
2875       /* Output the temporary integer CC register.  */
2876       fputs (reg_names[ICC_TEMP], file);
2877       break;
2878
2879     /* case 'a': print an address.  */
2880
2881     case 'C':
2882       /* Print appropriate test for integer branch false operation.  */
2883       fputs (comparison_string (reverse_condition (GET_CODE (x)),
2884                                 XEXP (x, 0)), file);
2885       break;
2886
2887     case 'c':
2888       /* Print appropriate test for integer branch true operation.  */
2889       fputs (comparison_string (GET_CODE (x), XEXP (x, 0)), file);
2890       break;
2891
2892     case 'e':
2893       /* Print 1 for a NE and 0 for an EQ to give the final argument
2894          for a conditional instruction.  */
2895       if (GET_CODE (x) == NE)
2896         fputs ("1", file);
2897
2898       else if (GET_CODE (x) == EQ)
2899         fputs ("0", file);
2900
2901       else
2902         fatal_insn ("bad insn to frv_print_operand, 'e' modifier:", x);
2903       break;
2904
2905     case 'F':
2906       /* Print appropriate test for floating point branch false operation.  */
2907       switch (GET_CODE (x))
2908         {
2909         default:
2910           fatal_insn ("bad insn to frv_print_operand, 'F' modifier:", x);
2911
2912         case EQ:  fputs ("ne",  file); break;
2913         case NE:  fputs ("eq",  file); break;
2914         case LT:  fputs ("uge", file); break;
2915         case LE:  fputs ("ug",  file); break;
2916         case GT:  fputs ("ule", file); break;
2917         case GE:  fputs ("ul",  file); break;
2918         }
2919       break;
2920
2921     case 'f':
2922       /* Print appropriate test for floating point branch true operation.  */
2923       switch (GET_CODE (x))
2924         {
2925         default:
2926           fatal_insn ("bad insn to frv_print_operand, 'f' modifier:", x);
2927
2928         case EQ:  fputs ("eq",  file); break;
2929         case NE:  fputs ("ne",  file); break;
2930         case LT:  fputs ("lt",  file); break;
2931         case LE:  fputs ("le",  file); break;
2932         case GT:  fputs ("gt",  file); break;
2933         case GE:  fputs ("ge",  file); break;
2934         }
2935       break;
2936
2937     case 'g':
2938       /* Print appropriate GOT function.  */
2939       if (GET_CODE (x) != CONST_INT)
2940         fatal_insn ("bad insn to frv_print_operand, 'g' modifier:", x);
2941       fputs (unspec_got_name (INTVAL (x)), file);
2942       break;
2943
2944     case 'I':
2945       /* Print 'i' if the operand is a constant, or is a memory reference that
2946          adds a constant.  */
2947       if (GET_CODE (x) == MEM)
2948         x = ((GET_CODE (XEXP (x, 0)) == PLUS)
2949              ? XEXP (XEXP (x, 0), 1)
2950              : XEXP (x, 0));
2951       else if (GET_CODE (x) == PLUS)
2952         x = XEXP (x, 1);
2953
2954       switch (GET_CODE (x))
2955         {
2956         default:
2957           break;
2958
2959         case CONST_INT:
2960         case SYMBOL_REF:
2961         case CONST:
2962           fputs ("i", file);
2963           break;
2964         }
2965       break;
2966
2967     case 'i':
2968       /* For jump instructions, print 'i' if the operand is a constant or
2969          is an expression that adds a constant.  */
2970       if (GET_CODE (x) == CONST_INT)
2971         fputs ("i", file);
2972
2973       else
2974         {
2975           if (GET_CODE (x) == CONST_INT
2976               || (GET_CODE (x) == PLUS
2977                   && (GET_CODE (XEXP (x, 1)) == CONST_INT
2978                       || GET_CODE (XEXP (x, 0)) == CONST_INT)))
2979             fputs ("i", file);
2980         }
2981       break;
2982
2983     case 'L':
2984       /* Print the lower register of a double word register pair */
2985       if (GET_CODE (x) == REG)
2986         fputs (reg_names[ REGNO (x)+1 ], file);
2987       else
2988         fatal_insn ("bad insn to frv_print_operand, 'L' modifier:", x);
2989       break;
2990
2991     /* case 'l': print a LABEL_REF.  */
2992
2993     case 'M':
2994     case 'N':
2995       /* Print a memory reference for ld/st/jmp, %N prints a memory reference
2996          for the second word of double memory operations.  */
2997       offset = (code == 'M') ? 0 : UNITS_PER_WORD;
2998       switch (GET_CODE (x))
2999         {
3000         default:
3001           fatal_insn ("bad insn to frv_print_operand, 'M/N' modifier:", x);
3002
3003         case MEM:
3004           frv_print_operand_memory_reference (file, XEXP (x, 0), offset);
3005           break;
3006
3007         case REG:
3008         case SUBREG:
3009         case CONST_INT:
3010         case PLUS:
3011         case SYMBOL_REF:
3012           frv_print_operand_memory_reference (file, x, offset);
3013           break;
3014         }
3015       break;
3016
3017     case 'O':
3018       /* Print the opcode of a command.  */
3019       switch (GET_CODE (x))
3020         {
3021         default:
3022           fatal_insn ("bad insn to frv_print_operand, 'O' modifier:", x);
3023
3024         case PLUS:     fputs ("add", file); break;
3025         case MINUS:    fputs ("sub", file); break;
3026         case AND:      fputs ("and", file); break;
3027         case IOR:      fputs ("or",  file); break;
3028         case XOR:      fputs ("xor", file); break;
3029         case ASHIFT:   fputs ("sll", file); break;
3030         case ASHIFTRT: fputs ("sra", file); break;
3031         case LSHIFTRT: fputs ("srl", file); break;
3032         }
3033       break;
3034
3035     /* case 'n': negate and print a constant int.  */
3036
3037     case 'P':
3038       /* Print PIC label using operand as the number.  */
3039       if (GET_CODE (x) != CONST_INT)
3040         fatal_insn ("bad insn to frv_print_operand, P modifier:", x);
3041
3042       fprintf (file, ".LCF%ld", (long)INTVAL (x));
3043       break;
3044
3045     case 'U':
3046       /* Print 'u' if the operand is a update load/store.  */
3047       if (GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
3048         fputs ("u", file);
3049       break;
3050
3051     case 'z':
3052       /* If value is 0, print gr0, otherwise it must be a register.  */
3053       if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0)
3054         fputs (reg_names[GPR_R0], file);
3055
3056       else if (GET_CODE (x) == REG)
3057         fputs (reg_names [REGNO (x)], file);
3058
3059       else
3060         fatal_insn ("bad insn in frv_print_operand, z case", x);
3061       break;
3062
3063     case 'x':
3064       /* Print constant in hex.  */
3065       if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
3066         {
3067           fprintf (file, "%s0x%.4lx", IMMEDIATE_PREFIX, (long) value);
3068           break;
3069         }
3070
3071       /* Fall through.  */
3072
3073     case '\0':
3074       if (GET_CODE (x) == REG)
3075         fputs (reg_names [REGNO (x)], file);
3076
3077       else if (GET_CODE (x) == CONST_INT
3078               || GET_CODE (x) == CONST_DOUBLE)
3079         fprintf (file, "%s%ld", IMMEDIATE_PREFIX, (long) value);
3080
3081       else if (frv_const_unspec_p (x, &unspec))
3082         frv_output_const_unspec (file, &unspec);
3083
3084       else if (GET_CODE (x) == MEM)
3085         frv_print_operand_address (file, XEXP (x, 0));
3086
3087       else if (CONSTANT_ADDRESS_P (x))
3088         frv_print_operand_address (file, x);
3089
3090       else
3091         fatal_insn ("bad insn in frv_print_operand, 0 case", x);
3092
3093       break;
3094
3095     default:
3096       fatal_insn ("frv_print_operand: unknown code", x);
3097       break;
3098     }
3099
3100   return;
3101 }
3102
3103 \f
3104 /* A C statement (sans semicolon) for initializing the variable CUM for the
3105    state at the beginning of the argument list.  The variable has type
3106    `CUMULATIVE_ARGS'.  The value of FNTYPE is the tree node for the data type
3107    of the function which will receive the args, or 0 if the args are to a
3108    compiler support library function.  The value of INDIRECT is nonzero when
3109    processing an indirect call, for example a call through a function pointer.
3110    The value of INDIRECT is zero for a call to an explicitly named function, a
3111    library function call, or when `INIT_CUMULATIVE_ARGS' is used to find
3112    arguments for the function being compiled.
3113
3114    When processing a call to a compiler support library function, LIBNAME
3115    identifies which one.  It is a `symbol_ref' rtx which contains the name of
3116    the function, as a string.  LIBNAME is 0 when an ordinary C function call is
3117    being processed.  Thus, each time this macro is called, either LIBNAME or
3118    FNTYPE is nonzero, but never both of them at once.  */
3119
3120 void
3121 frv_init_cumulative_args (CUMULATIVE_ARGS *cum,
3122                           tree fntype,
3123                           rtx libname,
3124                           tree fndecl,
3125                           int incoming)
3126 {
3127   *cum = FIRST_ARG_REGNUM;
3128
3129   if (TARGET_DEBUG_ARG)
3130     {
3131       fprintf (stderr, "\ninit_cumulative_args:");
3132       if (!fndecl && fntype)
3133         fputs (" indirect", stderr);
3134
3135       if (incoming)
3136         fputs (" incoming", stderr);
3137
3138       if (fntype)
3139         {
3140           tree ret_type = TREE_TYPE (fntype);
3141           fprintf (stderr, " return=%s,",
3142                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
3143         }
3144
3145       if (libname && GET_CODE (libname) == SYMBOL_REF)
3146         fprintf (stderr, " libname=%s", XSTR (libname, 0));
3147
3148       if (cfun->returns_struct)
3149         fprintf (stderr, " return-struct");
3150
3151       putc ('\n', stderr);
3152     }
3153 }
3154
3155 \f
3156 /* Return true if we should pass an argument on the stack rather than
3157    in registers.  */
3158
3159 static bool
3160 frv_must_pass_in_stack (enum machine_mode mode, const_tree type)
3161 {
3162   if (mode == BLKmode)
3163     return true;
3164   if (type == NULL)
3165     return false;
3166   return AGGREGATE_TYPE_P (type);
3167 }
3168
3169 /* If defined, a C expression that gives the alignment boundary, in bits, of an
3170    argument with the specified mode and type.  If it is not defined,
3171    `PARM_BOUNDARY' is used for all arguments.  */
3172
3173 int
3174 frv_function_arg_boundary (enum machine_mode mode ATTRIBUTE_UNUSED,
3175                            tree type ATTRIBUTE_UNUSED)
3176 {
3177   return BITS_PER_WORD;
3178 }
3179
3180 rtx
3181 frv_function_arg (CUMULATIVE_ARGS *cum,
3182                   enum machine_mode mode,
3183                   tree type ATTRIBUTE_UNUSED,
3184                   int named,
3185                   int incoming ATTRIBUTE_UNUSED)
3186 {
3187   enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
3188   int arg_num = *cum;
3189   rtx ret;
3190   const char *debstr;
3191
3192   /* Return a marker for use in the call instruction.  */
3193   if (xmode == VOIDmode)
3194     {
3195       ret = const0_rtx;
3196       debstr = "<0>";
3197     }
3198
3199   else if (arg_num <= LAST_ARG_REGNUM)
3200     {
3201       ret = gen_rtx_REG (xmode, arg_num);
3202       debstr = reg_names[arg_num];
3203     }
3204
3205   else
3206     {
3207       ret = NULL_RTX;
3208       debstr = "memory";
3209     }
3210
3211   if (TARGET_DEBUG_ARG)
3212     fprintf (stderr,
3213              "function_arg: words = %2d, mode = %4s, named = %d, size = %3d, arg = %s\n",
3214              arg_num, GET_MODE_NAME (mode), named, GET_MODE_SIZE (mode), debstr);
3215
3216   return ret;
3217 }
3218
3219 \f
3220 /* A C statement (sans semicolon) to update the summarizer variable CUM to
3221    advance past an argument in the argument list.  The values MODE, TYPE and
3222    NAMED describe that argument.  Once this is done, the variable CUM is
3223    suitable for analyzing the *following* argument with `FUNCTION_ARG', etc.
3224
3225    This macro need not do anything if the argument in question was passed on
3226    the stack.  The compiler knows how to track the amount of stack space used
3227    for arguments without any special help.  */
3228
3229 void
3230 frv_function_arg_advance (CUMULATIVE_ARGS *cum,
3231                           enum machine_mode mode,
3232                           tree type ATTRIBUTE_UNUSED,
3233                           int named)
3234 {
3235   enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
3236   int bytes = GET_MODE_SIZE (xmode);
3237   int words = (bytes + UNITS_PER_WORD  - 1) / UNITS_PER_WORD;
3238   int arg_num = *cum;
3239
3240   *cum = arg_num + words;
3241
3242   if (TARGET_DEBUG_ARG)
3243     fprintf (stderr,
3244              "function_adv: words = %2d, mode = %4s, named = %d, size = %3d\n",
3245              arg_num, GET_MODE_NAME (mode), named, words * UNITS_PER_WORD);
3246 }
3247
3248 \f
3249 /* A C expression for the number of words, at the beginning of an argument,
3250    must be put in registers.  The value must be zero for arguments that are
3251    passed entirely in registers or that are entirely pushed on the stack.
3252
3253    On some machines, certain arguments must be passed partially in registers
3254    and partially in memory.  On these machines, typically the first N words of
3255    arguments are passed in registers, and the rest on the stack.  If a
3256    multi-word argument (a `double' or a structure) crosses that boundary, its
3257    first few words must be passed in registers and the rest must be pushed.
3258    This macro tells the compiler when this occurs, and how many of the words
3259    should go in registers.
3260
3261    `FUNCTION_ARG' for these arguments should return the first register to be
3262    used by the caller for this argument; likewise `FUNCTION_INCOMING_ARG', for
3263    the called function.  */
3264
3265 static int
3266 frv_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3267                        tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
3268 {
3269   enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
3270   int bytes = GET_MODE_SIZE (xmode);
3271   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3272   int arg_num = *cum;
3273   int ret;
3274
3275   ret = ((arg_num <= LAST_ARG_REGNUM && arg_num + words > LAST_ARG_REGNUM+1)
3276          ? LAST_ARG_REGNUM - arg_num + 1
3277          : 0);
3278   ret *= UNITS_PER_WORD;
3279
3280   if (TARGET_DEBUG_ARG && ret)
3281     fprintf (stderr, "frv_arg_partial_bytes: %d\n", ret);
3282
3283   return ret;
3284 }
3285
3286 \f
3287 /* Return true if a register is ok to use as a base or index register.  */
3288
3289 static FRV_INLINE int
3290 frv_regno_ok_for_base_p (int regno, int strict_p)
3291 {
3292   if (GPR_P (regno))
3293     return TRUE;
3294
3295   if (strict_p)
3296     return (reg_renumber[regno] >= 0 && GPR_P (reg_renumber[regno]));
3297
3298   if (regno == ARG_POINTER_REGNUM)
3299     return TRUE;
3300
3301   return (regno >= FIRST_PSEUDO_REGISTER);
3302 }
3303
3304 \f
3305 /* A C compound statement with a conditional `goto LABEL;' executed if X (an
3306    RTX) is a legitimate memory address on the target machine for a memory
3307    operand of mode MODE.
3308
3309    It usually pays to define several simpler macros to serve as subroutines for
3310    this one.  Otherwise it may be too complicated to understand.
3311
3312    This macro must exist in two variants: a strict variant and a non-strict
3313    one.  The strict variant is used in the reload pass.  It must be defined so
3314    that any pseudo-register that has not been allocated a hard register is
3315    considered a memory reference.  In contexts where some kind of register is
3316    required, a pseudo-register with no hard register must be rejected.
3317
3318    The non-strict variant is used in other passes.  It must be defined to
3319    accept all pseudo-registers in every context where some kind of register is
3320    required.
3321
3322    Compiler source files that want to use the strict variant of this macro
3323    define the macro `REG_OK_STRICT'.  You should use an `#ifdef REG_OK_STRICT'
3324    conditional to define the strict variant in that case and the non-strict
3325    variant otherwise.
3326
3327    Subroutines to check for acceptable registers for various purposes (one for
3328    base registers, one for index registers, and so on) are typically among the
3329    subroutines used to define `GO_IF_LEGITIMATE_ADDRESS'.  Then only these
3330    subroutine macros need have two variants; the higher levels of macros may be
3331    the same whether strict or not.
3332
3333    Normally, constant addresses which are the sum of a `symbol_ref' and an
3334    integer are stored inside a `const' RTX to mark them as constant.
3335    Therefore, there is no need to recognize such sums specifically as
3336    legitimate addresses.  Normally you would simply recognize any `const' as
3337    legitimate.
3338
3339    Usually `PRINT_OPERAND_ADDRESS' is not prepared to handle constant sums that
3340    are not marked with `const'.  It assumes that a naked `plus' indicates
3341    indexing.  If so, then you *must* reject such naked constant sums as
3342    illegitimate addresses, so that none of them will be given to
3343    `PRINT_OPERAND_ADDRESS'.
3344
3345    On some machines, whether a symbolic address is legitimate depends on the
3346    section that the address refers to.  On these machines, define the macro
3347    `ENCODE_SECTION_INFO' to store the information into the `symbol_ref', and
3348    then check for it here.  When you see a `const', you will have to look
3349    inside it to find the `symbol_ref' in order to determine the section.
3350
3351    The best way to modify the name string is by adding text to the beginning,
3352    with suitable punctuation to prevent any ambiguity.  Allocate the new name
3353    in `saveable_obstack'.  You will have to modify `ASM_OUTPUT_LABELREF' to
3354    remove and decode the added text and output the name accordingly, and define
3355    `(* targetm.strip_name_encoding)' to access the original name string.
3356
3357    You can check the information stored here into the `symbol_ref' in the
3358    definitions of the macros `GO_IF_LEGITIMATE_ADDRESS' and
3359    `PRINT_OPERAND_ADDRESS'.  */
3360
3361 int
3362 frv_legitimate_address_p (enum machine_mode mode,
3363                           rtx x,
3364                           int strict_p,
3365                           int condexec_p,
3366                           int allow_double_reg_p)
3367 {
3368   rtx x0, x1;
3369   int ret = 0;
3370   HOST_WIDE_INT value;
3371   unsigned regno0;
3372
3373   if (FRV_SYMBOL_REF_TLS_P (x))
3374     return 0;
3375
3376   switch (GET_CODE (x))
3377     {
3378     default:
3379       break;
3380
3381     case SUBREG:
3382       x = SUBREG_REG (x);
3383       if (GET_CODE (x) != REG)
3384         break;
3385
3386       /* Fall through.  */
3387
3388     case REG:
3389       ret = frv_regno_ok_for_base_p (REGNO (x), strict_p);
3390       break;
3391
3392     case PRE_MODIFY:
3393       x0 = XEXP (x, 0);
3394       x1 = XEXP (x, 1);
3395       if (GET_CODE (x0) != REG
3396           || ! frv_regno_ok_for_base_p (REGNO (x0), strict_p)
3397           || GET_CODE (x1) != PLUS
3398           || ! rtx_equal_p (x0, XEXP (x1, 0))
3399           || GET_CODE (XEXP (x1, 1)) != REG
3400           || ! frv_regno_ok_for_base_p (REGNO (XEXP (x1, 1)), strict_p))
3401         break;
3402
3403       ret = 1;
3404       break;
3405
3406     case CONST_INT:
3407       /* 12-bit immediate */
3408       if (condexec_p)
3409         ret = FALSE;
3410       else
3411         {
3412           ret = IN_RANGE_P (INTVAL (x), -2048, 2047);
3413
3414           /* If we can't use load/store double operations, make sure we can
3415              address the second word.  */
3416           if (ret && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
3417             ret = IN_RANGE_P (INTVAL (x) + GET_MODE_SIZE (mode) - 1,
3418                               -2048, 2047);
3419         }
3420       break;
3421
3422     case PLUS:
3423       x0 = XEXP (x, 0);
3424       x1 = XEXP (x, 1);
3425
3426       if (GET_CODE (x0) == SUBREG)
3427         x0 = SUBREG_REG (x0);
3428
3429       if (GET_CODE (x0) != REG)
3430         break;
3431
3432       regno0 = REGNO (x0);
3433       if (!frv_regno_ok_for_base_p (regno0, strict_p))
3434         break;
3435