OSDN Git Service

* varasm.c (output_constant_def): Do not consult
[pf3gnuchains/gcc-fork.git] / gcc / config / mips / mips.c
1 /* Subroutines for insn-output.c for MIPS
2    Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4    Contributed by A. Lichnewsky, lich@inria.inria.fr.
5    Changes by Michael Meissner, meissner@osf.org.
6    64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
7    Brendan Eich, brendan@microunity.com.
8
9 This file is part of GNU CC.
10
11 GNU CC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GNU CC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GNU CC; see the file COPYING.  If not, write to
23 the Free Software Foundation, 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA.  */
25
26 /* ??? The TARGET_FP_CALL_32 macros are intended to simulate a 32 bit
27    calling convention in 64 bit mode.  It doesn't work though, and should
28    be replaced with something better designed.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include <signal.h>
35 #include "rtl.h"
36 #include "regs.h"
37 #include "hard-reg-set.h"
38 #include "real.h"
39 #include "insn-config.h"
40 #include "conditions.h"
41 #include "insn-attr.h"
42 #include "recog.h"
43 #include "toplev.h"
44 #include "output.h"
45 #include "tree.h"
46 #include "function.h"
47 #include "expr.h"
48 #include "flags.h"
49 #include "reload.h"
50 #include "output.h"
51 #include "tm_p.h"
52 #include "ggc.h"
53 #include "gstab.h"
54 #include "hashtab.h"
55 #include "debug.h"
56 #include "target.h"
57 #include "target-def.h"
58
59 #ifdef __GNU_STAB__
60 #define STAB_CODE_TYPE enum __stab_debug_code
61 #else
62 #define STAB_CODE_TYPE int
63 #endif
64
65 extern tree   lookup_name PARAMS ((tree));
66
67 /* Enumeration for all of the relational tests, so that we can build
68    arrays indexed by the test type, and not worry about the order
69    of EQ, NE, etc.  */
70
71 enum internal_test {
72     ITEST_EQ,
73     ITEST_NE,
74     ITEST_GT,
75     ITEST_GE,
76     ITEST_LT,
77     ITEST_LE,
78     ITEST_GTU,
79     ITEST_GEU,
80     ITEST_LTU,
81     ITEST_LEU,
82     ITEST_MAX
83   };
84
85
86 struct constant;
87 struct mips_arg_info;
88 static enum internal_test map_test_to_internal_test     PARAMS ((enum rtx_code));
89 static void get_float_compare_codes PARAMS ((enum rtx_code, enum rtx_code *,
90                                              enum rtx_code *));
91 static int mips16_simple_memory_operand         PARAMS ((rtx, rtx,
92                                                         enum machine_mode));
93 static int m16_check_op                         PARAMS ((rtx, int, int, int));
94 static void block_move_loop                     PARAMS ((rtx, rtx,
95                                                          unsigned int,
96                                                          int,
97                                                          rtx, rtx));
98 static void block_move_call                     PARAMS ((rtx, rtx, rtx));
99 static void mips_arg_info               PARAMS ((const CUMULATIVE_ARGS *,
100                                                  enum machine_mode,
101                                                  tree, int,
102                                                  struct mips_arg_info *));
103 static rtx mips_add_large_offset_to_sp          PARAMS ((HOST_WIDE_INT));
104 static void mips_annotate_frame_insn            PARAMS ((rtx, rtx));
105 static rtx mips_frame_set                       PARAMS ((enum machine_mode,
106                                                          int, int));
107 static void mips_emit_frame_related_store       PARAMS ((rtx, rtx,
108                                                          HOST_WIDE_INT));
109 static void save_restore_insns                  PARAMS ((int, rtx, long));
110 static void mips16_output_gp_offset             PARAMS ((FILE *, rtx));
111 static void mips16_fp_args                      PARAMS ((FILE *, int, int));
112 static void build_mips16_function_stub          PARAMS ((FILE *));
113 static void mips16_optimize_gp                  PARAMS ((rtx));
114 static rtx add_constant                         PARAMS ((struct constant **,
115                                                         rtx,
116                                                         enum machine_mode));
117 static void dump_constants                      PARAMS ((struct constant *,
118                                                         rtx));
119 static rtx mips_find_symbol                     PARAMS ((rtx));
120 static void abort_with_insn                     PARAMS ((rtx, const char *))
121   ATTRIBUTE_NORETURN;
122 static int symbolic_expression_p                PARAMS ((rtx));
123 static bool mips_assemble_integer         PARAMS ((rtx, unsigned int, int));
124 static void mips_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
125 static void mips_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
126 static void mips_set_architecture    PARAMS ((const struct mips_cpu_info *));
127 static void mips_set_tune            PARAMS ((const struct mips_cpu_info *));
128 static bool mips_strict_matching_cpu_name_p     PARAMS ((const char *,
129                                                          const char *));
130 static bool mips_matching_cpu_name_p            PARAMS ((const char *,
131                                                          const char *));
132 static const struct mips_cpu_info *mips_parse_cpu   PARAMS ((const char *,
133                                                               const char *));
134 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
135 static void copy_file_data                      PARAMS ((FILE *, FILE *));
136 #ifdef TARGET_IRIX6
137 static void iris6_asm_named_section_1           PARAMS ((const char *,
138                                                          unsigned int,
139                                                          unsigned int));
140 static void iris6_asm_named_section             PARAMS ((const char *,
141                                                          unsigned int));
142 static int iris_section_align_entry_eq          PARAMS ((const PTR, const PTR));
143 static hashval_t iris_section_align_entry_hash  PARAMS ((const PTR));
144 static int iris6_section_align_1                PARAMS ((void **, void *));
145 #endif
146 static int mips_adjust_cost                     PARAMS ((rtx, rtx, rtx, int));
147 static int mips_issue_rate                      PARAMS ((void));
148
149 static struct machine_function * mips_init_machine_status PARAMS ((void));
150 static void mips_select_section PARAMS ((tree, int, unsigned HOST_WIDE_INT))
151         ATTRIBUTE_UNUSED;
152 static void mips_unique_section                 PARAMS ((tree, int))
153         ATTRIBUTE_UNUSED;
154 static void mips_select_rtx_section PARAMS ((enum machine_mode, rtx,
155                                              unsigned HOST_WIDE_INT));
156 static int mips_use_dfa_pipeline_interface      PARAMS ((void));
157 static void mips_encode_section_info            PARAMS ((tree, int));
158 static bool mips_rtx_costs                      PARAMS ((rtx, int, int, int *));
159 static int mips_address_cost                    PARAMS ((rtx));
160
161
162 /* Structure to be filled in by compute_frame_size with register
163    save masks, and offsets for the current function.  */
164
165 struct mips_frame_info GTY(())
166 {
167   long total_size;              /* # bytes that the entire frame takes up */
168   long var_size;                /* # bytes that variables take up */
169   long args_size;               /* # bytes that outgoing arguments take up */
170   long extra_size;              /* # bytes of extra gunk */
171   int  gp_reg_size;             /* # bytes needed to store gp regs */
172   int  fp_reg_size;             /* # bytes needed to store fp regs */
173   long mask;                    /* mask of saved gp registers */
174   long fmask;                   /* mask of saved fp registers */
175   long gp_save_offset;          /* offset from vfp to store gp registers */
176   long fp_save_offset;          /* offset from vfp to store fp registers */
177   long gp_sp_offset;            /* offset from new sp to store gp registers */
178   long fp_sp_offset;            /* offset from new sp to store fp registers */
179   int  initialized;             /* != 0 if frame size already calculated */
180   int  num_gp;                  /* number of gp registers saved */
181   int  num_fp;                  /* number of fp registers saved */
182 };
183
184 struct machine_function GTY(()) {
185   /* Pseudo-reg holding the address of the current function when
186      generating embedded PIC code.  Created by LEGITIMIZE_ADDRESS,
187      used by mips_finalize_pic if it was created.  */
188   rtx embedded_pic_fnaddr_rtx;
189
190   /* Pseudo-reg holding the value of $28 in a mips16 function which
191      refers to GP relative global variables.  */
192   rtx mips16_gp_pseudo_rtx;
193
194   /* Current frame information, calculated by compute_frame_size.  */
195   struct mips_frame_info frame;
196
197   /* Length of instructions in function; mips16 only.  */
198   long insns_len;
199 };
200
201 /* Information about a single argument.  */
202 struct mips_arg_info
203 {
204   /* True if the argument is a record or union type.  */
205   bool struct_p;
206
207   /* True if the argument is passed in a floating-point register, or
208      would have been if we hadn't run out of registers.  */
209   bool fpr_p;
210
211   /* The argument's size, in bytes.  */
212   unsigned int num_bytes;
213
214   /* The number of words passed in registers, rounded up.  */
215   unsigned int reg_words;
216
217   /* The offset of the first register from GP_ARG_FIRST or FP_ARG_FIRST,
218      or MAX_ARGS_IN_REGISTERS if the argument is passed entirely
219      on the stack.  */
220   unsigned int reg_offset;
221
222   /* The number of words that must be passed on the stack, rounded up.  */
223   unsigned int stack_words;
224
225   /* The offset from the start of the stack overflow area of the argument's
226      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
227   unsigned int stack_offset;
228 };
229
230 /* Global variables for machine-dependent things.  */
231
232 /* Threshold for data being put into the small data/bss area, instead
233    of the normal data area (references to the small data/bss area take
234    1 instruction, and use the global pointer, references to the normal
235    data area takes 2 instructions).  */
236 int mips_section_threshold = -1;
237
238 /* Count the number of .file directives, so that .loc is up to date.  */
239 int num_source_filenames = 0;
240
241 /* Count the number of sdb related labels are generated (to find block
242    start and end boundaries).  */
243 int sdb_label_count = 0;
244
245 /* Next label # for each statement for Silicon Graphics IRIS systems.  */
246 int sym_lineno = 0;
247
248 /* Nonzero if inside of a function, because the stupid MIPS asm can't
249    handle .files inside of functions.  */
250 int inside_function = 0;
251
252 /* Files to separate the text and the data output, so that all of the data
253    can be emitted before the text, which will mean that the assembler will
254    generate smaller code, based on the global pointer.  */
255 FILE *asm_out_data_file;
256 FILE *asm_out_text_file;
257
258 /* Linked list of all externals that are to be emitted when optimizing
259    for the global pointer if they haven't been declared by the end of
260    the program with an appropriate .comm or initialization.  */
261
262 struct extern_list
263 {
264   struct extern_list *next;     /* next external */
265   const char *name;             /* name of the external */
266   int size;                     /* size in bytes */
267 } *extern_head = 0;
268
269 /* Name of the file containing the current function.  */
270 const char *current_function_file = "";
271
272 /* Warning given that Mips ECOFF can't support changing files
273    within a function.  */
274 int file_in_function_warning = FALSE;
275
276 /* Whether to suppress issuing .loc's because the user attempted
277    to change the filename within a function.  */
278 int ignore_line_number = FALSE;
279
280 /* Number of nested .set noreorder, noat, nomacro, and volatile requests.  */
281 int set_noreorder;
282 int set_noat;
283 int set_nomacro;
284 int set_volatile;
285
286 /* The next branch instruction is a branch likely, not branch normal.  */
287 int mips_branch_likely;
288
289 /* Count of delay slots and how many are filled.  */
290 int dslots_load_total;
291 int dslots_load_filled;
292 int dslots_jump_total;
293 int dslots_jump_filled;
294
295 /* # of nops needed by previous insn */
296 int dslots_number_nops;
297
298 /* Number of 1/2/3 word references to data items (ie, not jal's).  */
299 int num_refs[3];
300
301 /* registers to check for load delay */
302 rtx mips_load_reg, mips_load_reg2, mips_load_reg3, mips_load_reg4;
303
304 /* Cached operands, and operator to compare for use in set/branch/trap
305    on condition codes.  */
306 rtx branch_cmp[2];
307
308 /* what type of branch to use */
309 enum cmp_type branch_type;
310
311 /* The target cpu for code generation.  */
312 enum processor_type mips_arch;
313 const struct mips_cpu_info *mips_arch_info;
314
315 /* The target cpu for optimization and scheduling.  */
316 enum processor_type mips_tune;
317 const struct mips_cpu_info *mips_tune_info;
318
319 /* which instruction set architecture to use.  */
320 int mips_isa;
321
322 /* which abi to use.  */
323 int mips_abi;
324
325 /* Strings to hold which cpu and instruction set architecture to use.  */
326 const char *mips_arch_string;   /* for -march=<xxx> */
327 const char *mips_tune_string;   /* for -mtune=<xxx> */
328 const char *mips_isa_string;    /* for -mips{1,2,3,4} */
329 const char *mips_abi_string;    /* for -mabi={32,n32,64,eabi} */
330
331 /* Whether we are generating mips16 code.  This is a synonym for
332    TARGET_MIPS16, and exists for use as an attribute.  */
333 int mips16;
334
335 /* This variable is set by -mno-mips16.  We only care whether
336    -mno-mips16 appears or not, and using a string in this fashion is
337    just a way to avoid using up another bit in target_flags.  */
338 const char *mips_no_mips16_string;
339
340 /* Whether we are generating mips16 hard float code.  In mips16 mode
341    we always set TARGET_SOFT_FLOAT; this variable is nonzero if
342    -msoft-float was not specified by the user, which means that we
343    should arrange to call mips32 hard floating point code.  */
344 int mips16_hard_float;
345
346 /* This variable is set by -mentry.  We only care whether -mentry
347    appears or not, and using a string in this fashion is just a way to
348    avoid using up another bit in target_flags.  */
349 const char *mips_entry_string;
350
351 const char *mips_cache_flush_func = CACHE_FLUSH_FUNC;
352
353 /* Whether we should entry and exit pseudo-ops in mips16 mode.  */
354 int mips_entry;
355
356 /* If TRUE, we split addresses into their high and low parts in the RTL.  */
357 int mips_split_addresses;
358
359 /* Generating calls to position independent functions?  */
360 enum mips_abicalls_type mips_abicalls;
361
362 /* Mode used for saving/restoring general purpose registers.  */
363 static enum machine_mode gpr_mode;
364
365 /* Array giving truth value on whether or not a given hard register
366    can support a given mode.  */
367 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
368
369 /* The length of all strings seen when compiling for the mips16.  This
370    is used to tell how many strings are in the constant pool, so that
371    we can see if we may have an overflow.  This is reset each time the
372    constant pool is output.  */
373 int mips_string_length;
374
375 /* When generating mips16 code, a list of all strings that are to be
376    output after the current function.  */
377
378 static GTY(()) rtx mips16_strings;
379
380 /* In mips16 mode, we build a list of all the string constants we see
381    in a particular function.  */
382
383 struct string_constant
384 {
385   struct string_constant *next;
386   const char *label;
387 };
388
389 static struct string_constant *string_constants;
390
391 /* List of all MIPS punctuation characters used by print_operand.  */
392 char mips_print_operand_punct[256];
393
394 /* Map GCC register number to debugger register number.  */
395 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
396
397 /* Buffer to use to enclose a load/store operation with %{ %} to
398    turn on .set volatile.  */
399 static char volatile_buffer[60];
400
401 /* Hardware names for the registers.  If -mrnames is used, this
402    will be overwritten with mips_sw_reg_names.  */
403
404 char mips_reg_names[][8] =
405 {
406  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
407  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
408  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
409  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
410  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
411  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
412  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
413  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
414  "hi",   "lo",   "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
415  "$fcc5","$fcc6","$fcc7","$rap", "",     "",     "",     "",
416  "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
417  "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
418  "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
419  "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
420  "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
421  "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
422  "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
423  "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
424  "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
425  "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
426  "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
427  "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
428 };
429
430 /* Mips software names for the registers, used to overwrite the
431    mips_reg_names array.  */
432
433 char mips_sw_reg_names[][8] =
434 {
435   "$zero","$at",  "$v0",  "$v1",  "$a0",  "$a1",  "$a2",  "$a3",
436   "$t0",  "$t1",  "$t2",  "$t3",  "$t4",  "$t5",  "$t6",  "$t7",
437   "$s0",  "$s1",  "$s2",  "$s3",  "$s4",  "$s5",  "$s6",  "$s7",
438   "$t8",  "$t9",  "$k0",  "$k1",  "$gp",  "$sp",  "$fp",  "$ra",
439   "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
440   "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
441   "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
442   "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
443   "hi",   "lo",   "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
444   "$fcc5","$fcc6","$fcc7","$rap", "",     "",     "",     "",
445   "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
446   "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
447   "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
448   "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
449   "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
450   "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
451   "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
452   "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
453   "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
454   "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
455   "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
456   "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
457 };
458
459 /* Map hard register number to register class */
460 const enum reg_class mips_regno_to_class[] =
461 {
462   GR_REGS,      GR_REGS,        M16_NA_REGS,    M16_NA_REGS,
463   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
464   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
465   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
466   M16_NA_REGS,  M16_NA_REGS,    GR_REGS,        GR_REGS,
467   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
468   T_REG,        GR_REGS,        GR_REGS,        GR_REGS,
469   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
470   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
471   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
472   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
473   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
474   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
475   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
476   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
477   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
478   HI_REG,       LO_REG,         HILO_REG,       ST_REGS,
479   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
480   ST_REGS,      ST_REGS,        ST_REGS,        GR_REGS,
481   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
482   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
483   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
484   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
485   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
486   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
487   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
488   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
489   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
490   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
491   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
492   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
493   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
494   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
495   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
496   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
497   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
498   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
499   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
500   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
501   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
502   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
503   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
504   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
505   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS
506 };
507
508 /* Map register constraint character to register class.  */
509 enum reg_class mips_char_to_class[256] =
510 {
511   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
512   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
513   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
514   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
515   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
516   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
517   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
518   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
519   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
520   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
521   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
522   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
523   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
524   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
525   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
526   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
527   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
528   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
529   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
530   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
531   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
532   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
533   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
534   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
535   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
536   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
537   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
538   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
539   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
540   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
541   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
542   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
543   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
544   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
545   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
546   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
547   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
548   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
549   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
550   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
551   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
552   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
553   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
554   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
555   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
556   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
557   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
558   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
559   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
560   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
561   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
562   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
563   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
564   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
565   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
566   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
567   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
568   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
569   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
570   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
571   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
572   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
573   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
574   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
575 };
576 \f
577 /* A table describing all the processors gcc knows about.  Names are
578    matched in the order listed.  The first mention of an ISA level is
579    taken as the canonical name for that ISA.
580
581    To ease comparison, please keep this table in the same order as
582    gas's mips_cpu_info_table[].  */
583 const struct mips_cpu_info mips_cpu_info_table[] = {
584   /* Entries for generic ISAs */
585   { "mips1", PROCESSOR_R3000, 1 },
586   { "mips2", PROCESSOR_R6000, 2 },
587   { "mips3", PROCESSOR_R4000, 3 },
588   { "mips4", PROCESSOR_R8000, 4 },
589   { "mips32", PROCESSOR_4KC, 32 },
590   { "mips32r2", PROCESSOR_M4K, 33 },
591   { "mips64", PROCESSOR_5KC, 64 },
592
593   /* MIPS I */
594   { "r3000", PROCESSOR_R3000, 1 },
595   { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
596   { "r3900", PROCESSOR_R3900, 1 },
597
598   /* MIPS II */
599   { "r6000", PROCESSOR_R6000, 2 },
600
601   /* MIPS III */
602   { "r4000", PROCESSOR_R4000, 3 },
603   { "vr4100", PROCESSOR_R4100, 3 },
604   { "vr4111", PROCESSOR_R4111, 3 },
605   { "vr4120", PROCESSOR_R4120, 3 },
606   { "vr4300", PROCESSOR_R4300, 3 },
607   { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
608   { "r4600", PROCESSOR_R4600, 3 },
609   { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
610   { "r4650", PROCESSOR_R4650, 3 },
611
612   /* MIPS IV */
613   { "r8000", PROCESSOR_R8000, 4 },
614   { "vr5000", PROCESSOR_R5000, 4 },
615   { "vr5400", PROCESSOR_R5400, 4 },
616   { "vr5500", PROCESSOR_R5500, 4 },
617
618   /* MIPS32 */
619   { "4kc", PROCESSOR_4KC, 32 },
620   { "4kp", PROCESSOR_4KC, 32 }, /* = 4kc */
621
622   /* MIPS32 Release 2 */
623   { "m4k", PROCESSOR_M4K, 33 },
624
625   /* MIPS64 */
626   { "5kc", PROCESSOR_5KC, 64 },
627   { "20kc", PROCESSOR_20KC, 64 },
628   { "sb1", PROCESSOR_SB1, 64 },
629   { "sr71000", PROCESSOR_SR71000, 64 },
630
631   /* End marker */
632   { 0, 0, 0 }
633 };
634 \f
635 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT.  */
636 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
637 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
638 #endif
639 \f
640 /* Initialize the GCC target structure.  */
641 #undef TARGET_ASM_ALIGNED_HI_OP
642 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
643 #undef TARGET_ASM_ALIGNED_SI_OP
644 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
645 #undef TARGET_ASM_INTEGER
646 #define TARGET_ASM_INTEGER mips_assemble_integer
647
648 #if TARGET_IRIX5 && !TARGET_IRIX6
649 #undef TARGET_ASM_UNALIGNED_HI_OP
650 #define TARGET_ASM_UNALIGNED_HI_OP "\t.align 0\n\t.half\t"
651 #undef TARGET_ASM_UNALIGNED_SI_OP
652 #define TARGET_ASM_UNALIGNED_SI_OP "\t.align 0\n\t.word\t"
653 #undef TARGET_ASM_UNALIGNED_DI_OP
654 #define TARGET_ASM_UNALIGNED_DI_OP "\t.align 0\n\t.dword\t"
655 #endif
656
657 #undef TARGET_ASM_FUNCTION_PROLOGUE
658 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
659 #undef TARGET_ASM_FUNCTION_EPILOGUE
660 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
661 #undef TARGET_ASM_SELECT_RTX_SECTION
662 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
663
664 #undef TARGET_SCHED_ADJUST_COST
665 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
666 #undef TARGET_SCHED_ISSUE_RATE
667 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
668 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
669 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE mips_use_dfa_pipeline_interface
670
671 #undef TARGET_ENCODE_SECTION_INFO
672 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
673
674 #undef TARGET_VALID_POINTER_MODE
675 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
676 #undef TARGET_RTX_COSTS
677 #define TARGET_RTX_COSTS mips_rtx_costs
678 #undef TARGET_ADDRESS_COST
679 #define TARGET_ADDRESS_COST mips_address_cost
680
681 struct gcc_target targetm = TARGET_INITIALIZER;
682 \f
683 /* Return truth value of whether OP can be used as an operands
684    where a register or 16 bit unsigned integer is needed.  */
685
686 int
687 uns_arith_operand (op, mode)
688      rtx op;
689      enum machine_mode mode;
690 {
691   if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
692     return 1;
693
694   return register_operand (op, mode);
695 }
696
697 /* Return truth value of whether OP can be used as an operands
698    where a 16 bit integer is needed  */
699
700 int
701 arith_operand (op, mode)
702      rtx op;
703      enum machine_mode mode;
704 {
705   if (GET_CODE (op) == CONST_INT && SMALL_INT (op))
706     return 1;
707
708   /* On the mips16, a GP relative value is a signed 16 bit offset.  */
709   if (TARGET_MIPS16 && GET_CODE (op) == CONST && mips16_gp_offset_p (op))
710     return 1;
711
712   return register_operand (op, mode);
713 }
714
715 /* Return truth value of whether OP can be used as an operand in a two
716    address arithmetic insn (such as set 123456,%o4) of mode MODE.  */
717
718 int
719 arith32_operand (op, mode)
720      rtx op;
721      enum machine_mode mode;
722 {
723   if (GET_CODE (op) == CONST_INT)
724     return 1;
725
726   return register_operand (op, mode);
727 }
728
729 /* Return truth value of whether OP is an integer which fits in 16 bits.  */
730
731 int
732 small_int (op, mode)
733      rtx op;
734      enum machine_mode mode ATTRIBUTE_UNUSED;
735 {
736   return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
737 }
738
739 /* Return truth value of whether OP is a 32 bit integer which is too big to
740    be loaded with one instruction.  */
741
742 int
743 large_int (op, mode)
744      rtx op;
745      enum machine_mode mode ATTRIBUTE_UNUSED;
746 {
747   HOST_WIDE_INT value;
748
749   if (GET_CODE (op) != CONST_INT)
750     return 0;
751
752   value = INTVAL (op);
753
754   /* ior reg,$r0,value */
755   if ((value & ~ ((HOST_WIDE_INT) 0x0000ffff)) == 0)
756     return 0;
757
758   /* subu reg,$r0,value */
759   if (((unsigned HOST_WIDE_INT) (value + 32768)) <= 32767)
760     return 0;
761
762   /* lui reg,value>>16 */
763   if ((value & 0x0000ffff) == 0)
764     return 0;
765
766   return 1;
767 }
768
769 /* Return truth value of whether OP is a register or the constant 0.
770    In mips16 mode, we only accept a register, since the mips16 does
771    not have $0.  */
772
773 int
774 reg_or_0_operand (op, mode)
775      rtx op;
776      enum machine_mode mode;
777 {
778   switch (GET_CODE (op))
779     {
780     case CONST_INT:
781       if (TARGET_MIPS16)
782         return 0;
783       return INTVAL (op) == 0;
784
785     case CONST_DOUBLE:
786       if (TARGET_MIPS16)
787         return 0;
788       return op == CONST0_RTX (mode);
789
790     default:
791       return register_operand (op, mode);
792     }
793 }
794
795 /* Return truth value of whether OP is a register or the constant 0,
796    even in mips16 mode.  */
797
798 int
799 true_reg_or_0_operand (op, mode)
800      rtx op;
801      enum machine_mode mode;
802 {
803   switch (GET_CODE (op))
804     {
805     case CONST_INT:
806       return INTVAL (op) == 0;
807
808     case CONST_DOUBLE:
809       return op == CONST0_RTX (mode);
810
811     default:
812       return register_operand (op, mode);
813     }
814 }
815
816 /* Return truth value if a CONST_DOUBLE is ok to be a legitimate constant.  */
817
818 int
819 mips_const_double_ok (op, mode)
820      rtx op;
821      enum machine_mode mode;
822 {
823   if (GET_CODE (op) != CONST_DOUBLE)
824     return 0;
825
826   if (mode == VOIDmode)
827     return 1;
828
829   /* We've no zero register in mips16 mode.  */
830   if (TARGET_MIPS16)
831     return 0;
832
833   if (mode != SFmode && mode != DFmode)
834     return 0;
835
836   if (op == CONST0_RTX (mode))
837     return 1;
838
839   return 0;
840 }
841
842 /* Accept the floating point constant 1 in the appropriate mode.  */
843
844 int
845 const_float_1_operand (op, mode)
846      rtx op;
847      enum machine_mode mode;
848 {
849   REAL_VALUE_TYPE d;
850
851   if (GET_CODE (op) != CONST_DOUBLE
852       || mode != GET_MODE (op)
853       || (mode != DFmode && mode != SFmode))
854     return 0;
855
856   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
857
858   return REAL_VALUES_EQUAL (d, dconst1);
859 }
860
861 /* Return true if a memory load or store of REG plus OFFSET in MODE
862    can be represented in a single word on the mips16.  */
863
864 static int
865 mips16_simple_memory_operand (reg, offset, mode)
866      rtx reg;
867      rtx offset;
868      enum machine_mode mode;
869 {
870   unsigned int size;
871   int off;
872
873   if (mode == BLKmode)
874     {
875       /* We can't tell, because we don't know how the value will
876          eventually be accessed.  Returning 0 here does no great
877          harm; it just prevents some possible instruction scheduling.  */
878       return 0;
879     }
880
881   size = GET_MODE_SIZE (mode);
882
883   if (INTVAL (offset) % size != 0)
884     return 0;
885   if (REGNO (reg) == STACK_POINTER_REGNUM && GET_MODE_SIZE (mode) == 4)
886     off = 0x100;
887   else
888     off = 0x20;
889   if (INTVAL (offset) >= 0 && INTVAL (offset) < (HOST_WIDE_INT)(off * size))
890     return 1;
891   return 0;
892 }
893
894 /* Return truth value if a memory operand fits in a single instruction
895    (ie, register + small offset).  */
896
897 int
898 simple_memory_operand (op, mode)
899      rtx op;
900      enum machine_mode mode;
901 {
902   rtx addr, plus0, plus1;
903
904   /* Eliminate non-memory operations */
905   if (GET_CODE (op) != MEM)
906     return 0;
907
908   /* dword operations really put out 2 instructions, so eliminate them.  */
909   /* ??? This isn't strictly correct.  It is OK to accept multiword modes
910      here, since the length attributes are being set correctly, but only
911      if the address is offsettable.  LO_SUM is not offsettable.  */
912   if (GET_MODE_SIZE (GET_MODE (op)) > (unsigned) UNITS_PER_WORD)
913     return 0;
914
915   /* Decode the address now.  */
916   addr = XEXP (op, 0);
917   switch (GET_CODE (addr))
918     {
919     case REG:
920     case LO_SUM:
921       return 1;
922
923     case CONST_INT:
924       if (TARGET_MIPS16)
925         return 0;
926       return SMALL_INT (addr);
927
928     case PLUS:
929       plus0 = XEXP (addr, 0);
930       plus1 = XEXP (addr, 1);
931       if (GET_CODE (plus0) == REG
932           && GET_CODE (plus1) == CONST_INT && SMALL_INT (plus1)
933           && (! TARGET_MIPS16
934               || mips16_simple_memory_operand (plus0, plus1, mode)))
935         return 1;
936
937       else if (GET_CODE (plus1) == REG
938                && GET_CODE (plus0) == CONST_INT && SMALL_INT (plus0)
939                && (! TARGET_MIPS16
940                    || mips16_simple_memory_operand (plus1, plus0, mode)))
941         return 1;
942
943       else
944         return 0;
945
946 #if 0
947       /* We used to allow small symbol refs here (ie, stuff in .sdata
948          or .sbss), but this causes some bugs in G++.  Also, it won't
949          interfere if the MIPS linker rewrites the store instruction
950          because the function is PIC.  */
951
952     case LABEL_REF:             /* never gp relative */
953       break;
954
955     case CONST:
956       /* If -G 0, we can never have a GP relative memory operation.
957          Also, save some time if not optimizing.  */
958       if (!TARGET_GP_OPT)
959         return 0;
960
961       {
962         rtx offset = const0_rtx;
963         addr = eliminate_constant_term (XEXP (addr, 0), &offset);
964         if (GET_CODE (op) != SYMBOL_REF)
965           return 0;
966
967         /* let's be paranoid....  */
968         if (! SMALL_INT (offset))
969           return 0;
970       }
971
972       /* fall through */
973
974     case SYMBOL_REF:
975       return SYMBOL_REF_FLAG (addr);
976 #endif
977
978       /* This SYMBOL_REF case is for the mips16.  If the above case is
979          reenabled, this one should be merged in.  */
980     case SYMBOL_REF:
981       /* References to the constant pool on the mips16 use a small
982          offset if the function is small.  The only time we care about
983          getting this right is during delayed branch scheduling, so
984          don't need to check until then.  The machine_dependent_reorg
985          function will set the total length of the instructions used
986          in the function (cfun->machine->insns_len).  If that is small
987          enough, we know for sure that this is a small offset.  It
988          would be better if we could take into account the location of
989          the instruction within the function, but we can't, because we
990          don't know where we are.  */
991       if (TARGET_MIPS16
992           && CONSTANT_POOL_ADDRESS_P (addr)
993           && cfun->machine->insns_len > 0)
994         {
995           long size;
996
997           size = cfun->machine->insns_len + get_pool_size ();
998           if (GET_MODE_SIZE (mode) == 4)
999             return size < 4 * 0x100;
1000           else if (GET_MODE_SIZE (mode) == 8)
1001             return size < 8 * 0x20;
1002           else
1003             return 0;
1004         }
1005
1006       return 0;
1007
1008     default:
1009       break;
1010     }
1011
1012   return 0;
1013 }
1014
1015 /* Return nonzero for a memory address that can be used to load or store
1016    a doubleword.  */
1017
1018 int
1019 double_memory_operand (op, mode)
1020      rtx op;
1021      enum machine_mode mode;
1022 {
1023   if (GET_CODE (op) != MEM
1024       || ! memory_operand (op, mode))
1025     {
1026       /* During reload, we accept a pseudo register if it has an
1027          appropriate memory address.  If we don't do this, we will
1028          wind up reloading into a register, and then reloading that
1029          register from memory, when we could just reload directly from
1030          memory.  */
1031       if (reload_in_progress
1032           && GET_CODE (op) == REG
1033           && REGNO (op) >= FIRST_PSEUDO_REGISTER
1034           && reg_renumber[REGNO (op)] < 0
1035           && reg_equiv_mem[REGNO (op)] != 0
1036           && double_memory_operand (reg_equiv_mem[REGNO (op)], mode))
1037         return 1;
1038
1039       /* All reloaded addresses are valid in TARGET_64BIT mode.  This is
1040          the same test performed for 'm' in find_reloads.  */
1041
1042       if (reload_in_progress
1043           && TARGET_64BIT
1044           && (GET_CODE (op) == MEM
1045               || (GET_CODE (op) == REG
1046                   && REGNO (op) >= FIRST_PSEUDO_REGISTER
1047                   && reg_renumber[REGNO (op)] < 0)))
1048         return 1;
1049
1050       if (reload_in_progress
1051           && TARGET_MIPS16
1052           && GET_CODE (op) == MEM)
1053         {
1054           rtx addr;
1055
1056           addr = XEXP (op, 0);
1057
1058           /* During reload on the mips16, we accept a large offset
1059              from the frame pointer or the stack pointer.  This large
1060              address will get reloaded anyhow.  */
1061           if (GET_CODE (addr) == PLUS
1062               && GET_CODE (XEXP (addr, 0)) == REG
1063               && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1064                   || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
1065               && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
1066                    && ! SMALL_INT (XEXP (addr, 1)))
1067                   || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
1068                       && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
1069             return 1;
1070
1071           /* Similarly, we accept a case where the memory address is
1072              itself on the stack, and will be reloaded.  */
1073           if (GET_CODE (addr) == MEM)
1074             {
1075               rtx maddr;
1076
1077               maddr = XEXP (addr, 0);
1078               if (GET_CODE (maddr) == PLUS
1079                   && GET_CODE (XEXP (maddr, 0)) == REG
1080                   && (REGNO (XEXP (maddr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1081                       || REGNO (XEXP (maddr, 0)) == STACK_POINTER_REGNUM)
1082                   && ((GET_CODE (XEXP (maddr, 1)) == CONST_INT
1083                        && ! SMALL_INT (XEXP (maddr, 1)))
1084                       || (GET_CODE (XEXP (maddr, 1)) == SYMBOL_REF
1085                           && CONSTANT_POOL_ADDRESS_P (XEXP (maddr, 1)))))
1086                 return 1;
1087             }
1088
1089           /* We also accept the same case when we have a 16 bit signed
1090              offset mixed in as well.  The large address will get
1091              reloaded, and the 16 bit offset will be OK.  */
1092           if (GET_CODE (addr) == PLUS
1093               && GET_CODE (XEXP (addr, 0)) == MEM
1094               && GET_CODE (XEXP (addr, 1)) == CONST_INT
1095               && SMALL_INT (XEXP (addr, 1)))
1096             {
1097               addr = XEXP (XEXP (addr, 0), 0);
1098               if (GET_CODE (addr) == PLUS
1099                   && GET_CODE (XEXP (addr, 0)) == REG
1100                   && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1101                       || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
1102                   && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
1103                        && ! SMALL_INT (XEXP (addr, 1)))
1104                       || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
1105                           && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
1106                 return 1;
1107             }
1108         }
1109
1110       return 0;
1111     }
1112
1113   if (TARGET_64BIT)
1114     {
1115       /* In this case we can use an instruction like sd.  */
1116       return 1;
1117     }
1118
1119   /* Make sure that 4 added to the address is a valid memory address.
1120      This essentially just checks for overflow in an added constant.  */
1121
1122   if (CONSTANT_ADDRESS_P (XEXP (op, 0)))
1123     return 1;
1124
1125   op = adjust_address_nv (op, GET_MODE_CLASS (mode) == MODE_INT
1126                           ? SImode : SFmode, 4);
1127   return memory_address_p (GET_MODE (op), XEXP (op, 0));
1128 }
1129
1130 /* Return nonzero if the code of this rtx pattern is EQ or NE.  */
1131
1132 int
1133 equality_op (op, mode)
1134      rtx op;
1135      enum machine_mode mode;
1136 {
1137   if (mode != GET_MODE (op))
1138     return 0;
1139
1140   return GET_CODE (op) == EQ || GET_CODE (op) == NE;
1141 }
1142
1143 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
1144
1145 int
1146 cmp_op (op, mode)
1147      rtx op;
1148      enum machine_mode mode;
1149 {
1150   if (mode != GET_MODE (op))
1151     return 0;
1152
1153   return GET_RTX_CLASS (GET_CODE (op)) == '<';
1154 }
1155
1156 /* Return nonzero if the code is a relational operation suitable for a
1157    conditional trap instructuion (only EQ, NE, LT, LTU, GE, GEU).
1158    We need this in the insn that expands `trap_if' in order to prevent
1159    combine from erroneously altering the condition.  */
1160
1161 int
1162 trap_cmp_op (op, mode)
1163      rtx op;
1164      enum machine_mode mode;
1165 {
1166   if (mode != GET_MODE (op))
1167     return 0;
1168
1169   switch (GET_CODE (op))
1170     {
1171     case EQ:
1172     case NE:
1173     case LT:
1174     case LTU:
1175     case GE:
1176     case GEU:
1177       return 1;
1178
1179     default:
1180       return 0;
1181     }
1182 }
1183
1184 /* Return nonzero if the operand is either the PC or a label_ref.  */
1185
1186 int
1187 pc_or_label_operand (op, mode)
1188      rtx op;
1189      enum machine_mode mode ATTRIBUTE_UNUSED;
1190 {
1191   if (op == pc_rtx)
1192     return 1;
1193
1194   if (GET_CODE (op) == LABEL_REF)
1195     return 1;
1196
1197   return 0;
1198 }
1199
1200 /* Test for a valid operand for a call instruction.
1201    Don't allow the arg pointer register or virtual regs
1202    since they may change into reg + const, which the patterns
1203    can't handle yet.  */
1204
1205 int
1206 call_insn_operand (op, mode)
1207      rtx op;
1208      enum machine_mode mode ATTRIBUTE_UNUSED;
1209 {
1210   return (CONSTANT_ADDRESS_P (op)
1211           || (GET_CODE (op) == REG && op != arg_pointer_rtx
1212               && ! (REGNO (op) >= FIRST_PSEUDO_REGISTER
1213                     && REGNO (op) <= LAST_VIRTUAL_REGISTER)));
1214 }
1215
1216 /* Return nonzero if OPERAND is valid as a source operand for a move
1217    instruction.  */
1218
1219 int
1220 move_operand (op, mode)
1221      rtx op;
1222      enum machine_mode mode;
1223 {
1224   /* Accept any general operand after reload has started; doing so
1225      avoids losing if reload does an in-place replacement of a register
1226      with a SYMBOL_REF or CONST.  */
1227   return (general_operand (op, mode)
1228           && (! (mips_split_addresses && mips_check_split (op, mode))
1229               || reload_in_progress || reload_completed)
1230           && ! (TARGET_MIPS16
1231                 && GET_CODE (op) == SYMBOL_REF
1232                 && ! mips16_constant (op, mode, 1, 0)));
1233 }
1234
1235 /* Return nonzero if OPERAND is valid as a source operand for movdi.
1236    This accepts not only general_operand, but also sign extended
1237    move_operands.  Note that we need to accept sign extended constants
1238    in case a sign extended register which is used in an expression,
1239    and is equivalent to a constant, is spilled.  We need to accept
1240    sign-extended memory in order to reload registers from stack slots,
1241    and so that we generate efficient code for extendsidi2.  */
1242
1243 int
1244 movdi_operand (op, mode)
1245      rtx op;
1246      enum machine_mode mode;
1247 {
1248   if (TARGET_64BIT
1249       && mode == DImode
1250       && GET_CODE (op) == SIGN_EXTEND
1251       && GET_MODE (op) == DImode
1252       && move_operand (XEXP (op, 0), SImode))
1253     return 1;
1254
1255   return (general_operand (op, mode)
1256           && ! (TARGET_MIPS16
1257                 && GET_CODE (op) == SYMBOL_REF
1258                 && ! mips16_constant (op, mode, 1, 0)));
1259 }
1260
1261 /* Like register_operand, but when in 64 bit mode also accept a sign
1262    extend of a 32 bit register, since the value is known to be already
1263    sign extended.  */
1264
1265 int
1266 se_register_operand (op, mode)
1267      rtx op;
1268      enum machine_mode mode;
1269 {
1270   if (TARGET_64BIT
1271       && mode == DImode
1272       && GET_CODE (op) == SIGN_EXTEND
1273       && GET_MODE (op) == DImode
1274       && GET_MODE (XEXP (op, 0)) == SImode
1275       && register_operand (XEXP (op, 0), SImode))
1276     return 1;
1277
1278   return register_operand (op, mode);
1279 }
1280
1281 /* Like reg_or_0_operand, but when in 64 bit mode also accept a sign
1282    extend of a 32 bit register, since the value is known to be already
1283    sign extended.  */
1284
1285 int
1286 se_reg_or_0_operand (op, mode)
1287      rtx op;
1288      enum machine_mode mode;
1289 {
1290   if (TARGET_64BIT
1291       && mode == DImode
1292       && GET_CODE (op) == SIGN_EXTEND
1293       && GET_MODE (op) == DImode
1294       && GET_MODE (XEXP (op, 0)) == SImode
1295       && register_operand (XEXP (op, 0), SImode))
1296     return 1;
1297
1298   return reg_or_0_operand (op, mode);
1299 }
1300
1301 /* Like uns_arith_operand, but when in 64 bit mode also accept a sign
1302    extend of a 32 bit register, since the value is known to be already
1303    sign extended.  */
1304
1305 int
1306 se_uns_arith_operand (op, mode)
1307      rtx op;
1308      enum machine_mode mode;
1309 {
1310   if (TARGET_64BIT
1311       && mode == DImode
1312       && GET_CODE (op) == SIGN_EXTEND
1313       && GET_MODE (op) == DImode
1314       && GET_MODE (XEXP (op, 0)) == SImode
1315       && register_operand (XEXP (op, 0), SImode))
1316     return 1;
1317
1318   return uns_arith_operand (op, mode);
1319 }
1320
1321 /* Like arith_operand, but when in 64 bit mode also accept a sign
1322    extend of a 32 bit register, since the value is known to be already
1323    sign extended.  */
1324
1325 int
1326 se_arith_operand (op, mode)
1327      rtx op;
1328      enum machine_mode mode;
1329 {
1330   if (TARGET_64BIT
1331       && mode == DImode
1332       && GET_CODE (op) == SIGN_EXTEND
1333       && GET_MODE (op) == DImode
1334       && GET_MODE (XEXP (op, 0)) == SImode
1335       && register_operand (XEXP (op, 0), SImode))
1336     return 1;
1337
1338   return arith_operand (op, mode);
1339 }
1340
1341 /* Like nonmemory_operand, but when in 64 bit mode also accept a sign
1342    extend of a 32 bit register, since the value is known to be already
1343    sign extended.  */
1344
1345 int
1346 se_nonmemory_operand (op, mode)
1347      rtx op;
1348      enum machine_mode mode;
1349 {
1350   if (TARGET_64BIT
1351       && mode == DImode
1352       && GET_CODE (op) == SIGN_EXTEND
1353       && GET_MODE (op) == DImode
1354       && GET_MODE (XEXP (op, 0)) == SImode
1355       && register_operand (XEXP (op, 0), SImode))
1356     return 1;
1357
1358   return nonmemory_operand (op, mode);
1359 }
1360
1361 /* Accept any operand that can appear in a mips16 constant table
1362    instruction.  We can't use any of the standard operand functions
1363    because for these instructions we accept values that are not
1364    accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs.  */
1365
1366 int
1367 consttable_operand (op, mode)
1368      rtx op;
1369      enum machine_mode mode ATTRIBUTE_UNUSED;
1370 {
1371   return CONSTANT_P (op);
1372 }
1373
1374 /* Coprocessor operand; return true if rtx is a REG and refers to a
1375    coprocessor.  */
1376
1377 int
1378 coprocessor_operand (op, mode)
1379      rtx op;
1380      enum machine_mode mode ATTRIBUTE_UNUSED;
1381 {
1382   return (GET_CODE (op) == REG
1383           && COP0_REG_FIRST <= REGNO (op)
1384           && REGNO (op) <= COP3_REG_LAST);
1385 }
1386
1387 int
1388 coprocessor2_operand (op, mode)
1389      rtx op;
1390      enum machine_mode mode ATTRIBUTE_UNUSED;
1391 {
1392   return (GET_CODE (op) == REG
1393           && COP2_REG_FIRST <= REGNO (op)
1394           && REGNO (op) <= COP2_REG_LAST);
1395 }
1396
1397 /* Returns 1 if OP is a symbolic operand, i.e. a symbol_ref or a label_ref,
1398    possibly with an offset.  */
1399
1400 int
1401 symbolic_operand (op, mode)
1402       register rtx op;
1403       enum machine_mode mode;
1404 {
1405   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && mode != GET_MODE (op))
1406     return 0;
1407   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1408     return 1;
1409   if (GET_CODE (op) == CONST
1410       && GET_CODE (XEXP (op,0)) == PLUS
1411       && GET_CODE (XEXP (XEXP (op,0), 0)) == SYMBOL_REF
1412       && GET_CODE (XEXP (XEXP (op,0), 1)) == CONST_INT)
1413     return 1;
1414   return 0;
1415 }
1416
1417 /* Return nonzero if we split the address into high and low parts.  */
1418
1419 /* ??? We should also handle reg+array somewhere.  We get four
1420    instructions currently, lui %hi/addui %lo/addui reg/lw.  Better is
1421    lui %hi/addui reg/lw %lo.  Fixing GO_IF_LEGITIMATE_ADDRESS to accept
1422    (plus (reg) (symbol_ref)) doesn't work because the SYMBOL_REF is broken
1423    out of the address, then we have 4 instructions to combine.  Perhaps
1424    add a 3->2 define_split for combine.  */
1425
1426 /* ??? We could also split a CONST_INT here if it is a large_int().
1427    However, it doesn't seem to be very useful to have %hi(constant).
1428    We would be better off by doing the masking ourselves and then putting
1429    the explicit high part of the constant in the RTL.  This will give better
1430    optimization.  Also, %hi(constant) needs assembler changes to work.
1431    There is already a define_split that does this.  */
1432
1433 int
1434 mips_check_split (address, mode)
1435      rtx address;
1436      enum machine_mode mode;
1437 {
1438   /* ??? This is the same check used in simple_memory_operand.
1439      We use it here because LO_SUM is not offsettable.  */
1440   if (GET_MODE_SIZE (mode) > (unsigned) UNITS_PER_WORD)
1441     return 0;
1442
1443   if ((GET_CODE (address) == SYMBOL_REF && ! SYMBOL_REF_FLAG (address))
1444       || (GET_CODE (address) == CONST
1445           && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF
1446           && ! SYMBOL_REF_FLAG (XEXP (XEXP (address, 0), 0)))
1447       || GET_CODE (address) == LABEL_REF)
1448     return 1;
1449
1450   return 0;
1451 }
1452
1453 /* This function is used to implement REG_MODE_OK_FOR_BASE_P.  */
1454
1455 int
1456 mips_reg_mode_ok_for_base_p (reg, mode, strict)
1457      rtx reg;
1458      enum machine_mode mode;
1459      int strict;
1460 {
1461   return (strict
1462           ? REGNO_MODE_OK_FOR_BASE_P (REGNO (reg), mode)
1463           : GP_REG_OR_PSEUDO_NONSTRICT_P (REGNO (reg), mode));
1464 }
1465
1466 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS.  It
1467    returns a nonzero value if XINSN is a legitimate address for a
1468    memory operand of the indicated MODE.  STRICT is nonzero if this
1469    function is called during reload.  */
1470
1471 int
1472 mips_legitimate_address_p (mode, xinsn, strict)
1473      enum machine_mode mode;
1474      rtx xinsn;
1475      int strict;
1476 {
1477   if (TARGET_DEBUG_B_MODE)
1478     {
1479       GO_PRINTF2 ("\n========== GO_IF_LEGITIMATE_ADDRESS, %sstrict\n",
1480                   strict ? "" : "not ");
1481       GO_DEBUG_RTX (xinsn);
1482     }
1483
1484   /* Check for constant before stripping off SUBREG, so that we don't
1485      accept (subreg (const_int)) which will fail to reload.  */
1486   if (CONSTANT_ADDRESS_P (xinsn)
1487       && ! (mips_split_addresses && mips_check_split (xinsn, mode))
1488       && (! TARGET_MIPS16 || mips16_constant (xinsn, mode, 1, 0)))
1489     return 1;
1490
1491   while (GET_CODE (xinsn) == SUBREG)
1492     xinsn = SUBREG_REG (xinsn);
1493
1494   /* The mips16 can only use the stack pointer as a base register when
1495      loading SImode or DImode values.  */
1496   if (GET_CODE (xinsn) == REG
1497       && mips_reg_mode_ok_for_base_p (xinsn, mode, strict))
1498     return 1;
1499
1500   if (GET_CODE (xinsn) == LO_SUM && mips_split_addresses)
1501     {
1502       register rtx xlow0 = XEXP (xinsn, 0);
1503       register rtx xlow1 = XEXP (xinsn, 1);
1504
1505       while (GET_CODE (xlow0) == SUBREG)
1506         xlow0 = SUBREG_REG (xlow0);
1507       if (GET_CODE (xlow0) == REG
1508           && mips_reg_mode_ok_for_base_p (xlow0, mode, strict)
1509           && mips_check_split (xlow1, mode))
1510         return 1;
1511     }
1512
1513   if (GET_CODE (xinsn) == PLUS)
1514     {
1515       register rtx xplus0 = XEXP (xinsn, 0);
1516       register rtx xplus1 = XEXP (xinsn, 1);
1517       register enum rtx_code code0;
1518       register enum rtx_code code1;
1519
1520       while (GET_CODE (xplus0) == SUBREG)
1521         xplus0 = SUBREG_REG (xplus0);
1522       code0 = GET_CODE (xplus0);
1523
1524       while (GET_CODE (xplus1) == SUBREG)
1525         xplus1 = SUBREG_REG (xplus1);
1526       code1 = GET_CODE (xplus1);
1527
1528       /* The mips16 can only use the stack pointer as a base register
1529          when loading SImode or DImode values.  */
1530       if (code0 == REG
1531           && mips_reg_mode_ok_for_base_p (xplus0, mode, strict))
1532         {
1533           if (code1 == CONST_INT && SMALL_INT (xplus1))
1534             return 1;
1535
1536           /* On the mips16, we represent GP relative offsets in RTL.
1537              These are 16 bit signed values, and can serve as register
1538              offsets.  */
1539           if (TARGET_MIPS16
1540               && mips16_gp_offset_p (xplus1))
1541             return 1;
1542
1543           /* For some code sequences, you actually get better code by
1544              pretending that the MIPS supports an address mode of a
1545              constant address + a register, even though the real
1546              machine doesn't support it.  This is because the
1547              assembler can use $r1 to load just the high 16 bits, add
1548              in the register, and fold the low 16 bits into the memory
1549              reference, whereas the compiler generates a 4 instruction
1550              sequence.  On the other hand, CSE is not as effective.
1551              It would be a win to generate the lui directly, but the
1552              MIPS assembler does not have syntax to generate the
1553              appropriate relocation.  */
1554
1555           /* Also accept CONST_INT addresses here, so no else.  */
1556           /* Reject combining an embedded PIC text segment reference
1557              with a register.  That requires an additional
1558              instruction.  */
1559           /* ??? Reject combining an address with a register for the MIPS
1560              64 bit ABI, because the SGI assembler can not handle this.  */
1561           if (!TARGET_DEBUG_A_MODE
1562               && (mips_abi == ABI_32
1563                   || mips_abi == ABI_O64
1564                   || mips_abi == ABI_EABI)
1565               && CONSTANT_ADDRESS_P (xplus1)
1566               && ! mips_split_addresses
1567               && (!TARGET_EMBEDDED_PIC
1568                   || code1 != CONST
1569                   || GET_CODE (XEXP (xplus1, 0)) != MINUS)
1570               /* When assembling for machines with 64 bit registers,
1571                  the assembler will sign-extend the constant "foo"
1572                  in "la x, foo(x)" yielding the wrong result for:
1573                  (set (blah:DI) (plus x y)).  */
1574               && (!TARGET_64BIT
1575                   || (code1 == CONST_INT
1576                       && trunc_int_for_mode (INTVAL (xplus1),
1577                                              SImode) == INTVAL (xplus1)))
1578               && !TARGET_MIPS16)
1579             return 1;
1580         }
1581     }
1582
1583   if (TARGET_DEBUG_B_MODE)
1584     GO_PRINTF ("Not a legitimate address\n");
1585
1586   /* The address was not legitimate.  */
1587   return 0;
1588 }
1589
1590 \f
1591 /* We need a lot of little routines to check constant values on the
1592    mips16.  These are used to figure out how long the instruction will
1593    be.  It would be much better to do this using constraints, but
1594    there aren't nearly enough letters available.  */
1595
1596 static int
1597 m16_check_op (op, low, high, mask)
1598      rtx op;
1599      int low;
1600      int high;
1601      int mask;
1602 {
1603   return (GET_CODE (op) == CONST_INT
1604           && INTVAL (op) >= low
1605           && INTVAL (op) <= high
1606           && (INTVAL (op) & mask) == 0);
1607 }
1608
1609 int
1610 m16_uimm3_b (op, mode)
1611      rtx op;
1612      enum machine_mode mode ATTRIBUTE_UNUSED;
1613 {
1614   return m16_check_op (op, 0x1, 0x8, 0);
1615 }
1616
1617 int
1618 m16_simm4_1 (op, mode)
1619      rtx op;
1620      enum machine_mode mode ATTRIBUTE_UNUSED;
1621 {
1622   return m16_check_op (op, - 0x8, 0x7, 0);
1623 }
1624
1625 int
1626 m16_nsimm4_1 (op, mode)
1627      rtx op;
1628      enum machine_mode mode ATTRIBUTE_UNUSED;
1629 {
1630   return m16_check_op (op, - 0x7, 0x8, 0);
1631 }
1632
1633 int
1634 m16_simm5_1 (op, mode)
1635      rtx op;
1636      enum machine_mode mode ATTRIBUTE_UNUSED;
1637 {
1638   return m16_check_op (op, - 0x10, 0xf, 0);
1639 }
1640
1641 int
1642 m16_nsimm5_1 (op, mode)
1643      rtx op;
1644      enum machine_mode mode ATTRIBUTE_UNUSED;
1645 {
1646   return m16_check_op (op, - 0xf, 0x10, 0);
1647 }
1648
1649 int
1650 m16_uimm5_4 (op, mode)
1651      rtx op;
1652      enum machine_mode mode ATTRIBUTE_UNUSED;
1653 {
1654   return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
1655 }
1656
1657 int
1658 m16_nuimm5_4 (op, mode)
1659      rtx op;
1660      enum machine_mode mode ATTRIBUTE_UNUSED;
1661 {
1662   return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
1663 }
1664
1665 int
1666 m16_simm8_1 (op, mode)
1667      rtx op;
1668      enum machine_mode mode ATTRIBUTE_UNUSED;
1669 {
1670   return m16_check_op (op, - 0x80, 0x7f, 0);
1671 }
1672
1673 int
1674 m16_nsimm8_1 (op, mode)
1675      rtx op;
1676      enum machine_mode mode ATTRIBUTE_UNUSED;
1677 {
1678   return m16_check_op (op, - 0x7f, 0x80, 0);
1679 }
1680
1681 int
1682 m16_uimm8_1 (op, mode)
1683      rtx op;
1684      enum machine_mode mode ATTRIBUTE_UNUSED;
1685 {
1686   return m16_check_op (op, 0x0, 0xff, 0);
1687 }
1688
1689 int
1690 m16_nuimm8_1 (op, mode)
1691      rtx op;
1692      enum machine_mode mode ATTRIBUTE_UNUSED;
1693 {
1694   return m16_check_op (op, - 0xff, 0x0, 0);
1695 }
1696
1697 int
1698 m16_uimm8_m1_1 (op, mode)
1699      rtx op;
1700      enum machine_mode mode ATTRIBUTE_UNUSED;
1701 {
1702   return m16_check_op (op, - 0x1, 0xfe, 0);
1703 }
1704
1705 int
1706 m16_uimm8_4 (op, mode)
1707      rtx op;
1708      enum machine_mode mode ATTRIBUTE_UNUSED;
1709 {
1710   return m16_check_op (op, 0x0, 0xff << 2, 3);
1711 }
1712
1713 int
1714 m16_nuimm8_4 (op, mode)
1715      rtx op;
1716      enum machine_mode mode ATTRIBUTE_UNUSED;
1717 {
1718   return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
1719 }
1720
1721 int
1722 m16_simm8_8 (op, mode)
1723      rtx op;
1724      enum machine_mode mode ATTRIBUTE_UNUSED;
1725 {
1726   return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
1727 }
1728
1729 int
1730 m16_nsimm8_8 (op, mode)
1731      rtx op;
1732      enum machine_mode mode ATTRIBUTE_UNUSED;
1733 {
1734   return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
1735 }
1736
1737 /* References to the string table on the mips16 only use a small
1738    offset if the function is small.  See the comment in the SYMBOL_REF
1739    case in simple_memory_operand.  We can't check for LABEL_REF here,
1740    because the offset is always large if the label is before the
1741    referencing instruction.  */
1742
1743 int
1744 m16_usym8_4 (op, mode)
1745      rtx op;
1746      enum machine_mode mode ATTRIBUTE_UNUSED;
1747 {
1748   if (GET_CODE (op) == SYMBOL_REF
1749       && SYMBOL_REF_FLAG (op)
1750       && cfun->machine->insns_len > 0
1751       && XSTR (op, 0)[0] == '*'
1752       && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1753                   sizeof LOCAL_LABEL_PREFIX - 1) == 0
1754       && (cfun->machine->insns_len + get_pool_size () + mips_string_length
1755           < 4 * 0x100))
1756     {
1757       struct string_constant *l;
1758
1759       /* Make sure this symbol is on thelist of string constants to be
1760          output for this function.  It is possible that it has already
1761          been output, in which case this requires a large offset.  */
1762       for (l = string_constants; l != NULL; l = l->next)
1763         if (strcmp (l->label, XSTR (op, 0)) == 0)
1764           return 1;
1765     }
1766
1767   return 0;
1768 }
1769
1770 int
1771 m16_usym5_4 (op, mode)
1772      rtx op;
1773      enum machine_mode mode ATTRIBUTE_UNUSED;
1774 {
1775   if (GET_CODE (op) == SYMBOL_REF
1776       && SYMBOL_REF_FLAG (op)
1777       && cfun->machine->insns_len > 0
1778       && XSTR (op, 0)[0] == '*'
1779       && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1780                   sizeof LOCAL_LABEL_PREFIX - 1) == 0
1781       && (cfun->machine->insns_len + get_pool_size () + mips_string_length
1782           < 4 * 0x20))
1783     {
1784       struct string_constant *l;
1785
1786       /* Make sure this symbol is on thelist of string constants to be
1787          output for this function.  It is possible that it has already
1788          been output, in which case this requires a large offset.  */
1789       for (l = string_constants; l != NULL; l = l->next)
1790         if (strcmp (l->label, XSTR (op, 0)) == 0)
1791           return 1;
1792     }
1793
1794   return 0;
1795 }
1796 \f
1797 /* Returns an operand string for the given instruction's delay slot,
1798    after updating filled delay slot statistics.
1799
1800    We assume that operands[0] is the target register that is set.
1801
1802    In order to check the next insn, most of this functionality is moved
1803    to FINAL_PRESCAN_INSN, and we just set the global variables that
1804    it needs.  */
1805
1806 /* ??? This function no longer does anything useful, because final_prescan_insn
1807    now will never emit a nop.  */
1808
1809 const char *
1810 mips_fill_delay_slot (ret, type, operands, cur_insn)
1811      const char *ret;           /* normal string to return */
1812      enum delay_type type;      /* type of delay */
1813      rtx operands[];            /* operands to use */
1814      rtx cur_insn;              /* current insn */
1815 {
1816   register rtx set_reg;
1817   register enum machine_mode mode;
1818   register rtx next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL_RTX;
1819   register int num_nops;
1820
1821   if (type == DELAY_LOAD || type == DELAY_FCMP)
1822     num_nops = 1;
1823
1824   else if (type == DELAY_HILO)
1825     num_nops = 2;
1826
1827   else
1828     num_nops = 0;
1829
1830   /* Make sure that we don't put nop's after labels.  */
1831   next_insn = NEXT_INSN (cur_insn);
1832   while (next_insn != 0 && GET_CODE (next_insn) == NOTE)
1833     next_insn = NEXT_INSN (next_insn);
1834
1835   dslots_load_total += num_nops;
1836   if (TARGET_DEBUG_F_MODE
1837       || !optimize
1838       || type == DELAY_NONE
1839       || operands == 0
1840       || cur_insn == 0
1841       || next_insn == 0
1842       || GET_CODE (next_insn) == CODE_LABEL
1843       || (set_reg = operands[0]) == 0)
1844     {
1845       dslots_number_nops = 0;
1846       mips_load_reg  = 0;
1847       mips_load_reg2 = 0;
1848       mips_load_reg3 = 0;
1849       mips_load_reg4 = 0;
1850       return ret;
1851     }
1852
1853   set_reg = operands[0];
1854   if (set_reg == 0)
1855     return ret;
1856
1857   while (GET_CODE (set_reg) == SUBREG)
1858     set_reg = SUBREG_REG (set_reg);
1859
1860   mode = GET_MODE (set_reg);
1861   dslots_number_nops = num_nops;
1862   mips_load_reg = set_reg;
1863   if (GET_MODE_SIZE (mode)
1864       > (unsigned) (FP_REG_P (REGNO (set_reg)) ? UNITS_PER_FPREG : UNITS_PER_WORD))
1865     mips_load_reg2 = gen_rtx_REG (SImode, REGNO (set_reg) + 1);
1866   else
1867     mips_load_reg2 = 0;
1868
1869   if (type == DELAY_HILO)
1870     {
1871       mips_load_reg3 = gen_rtx_REG (SImode, MD_REG_FIRST);
1872       mips_load_reg4 = gen_rtx_REG (SImode, MD_REG_FIRST+1);
1873     }
1874   else
1875     {
1876       mips_load_reg3 = 0;
1877       mips_load_reg4 = 0;
1878     }
1879
1880   return ret;
1881 }
1882
1883 \f
1884 /* Determine whether a memory reference takes one (based off of the GP
1885    pointer), two (normal), or three (label + reg) instructions, and bump the
1886    appropriate counter for -mstats.  */
1887
1888 void
1889 mips_count_memory_refs (op, num)
1890      rtx op;
1891      int num;
1892 {
1893   int additional = 0;
1894   int n_words = 0;
1895   rtx addr, plus0, plus1;
1896   enum rtx_code code0, code1;
1897   int looping;
1898
1899   if (TARGET_DEBUG_B_MODE)
1900     {
1901       fprintf (stderr, "\n========== mips_count_memory_refs:\n");
1902       debug_rtx (op);
1903     }
1904
1905   /* Skip MEM if passed, otherwise handle movsi of address.  */
1906   addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
1907
1908   /* Loop, going through the address RTL.  */
1909   do
1910     {
1911       looping = FALSE;
1912       switch (GET_CODE (addr))
1913         {
1914         case REG:
1915         case CONST_INT:
1916         case LO_SUM:
1917           break;
1918
1919         case PLUS:
1920           plus0 = XEXP (addr, 0);
1921           plus1 = XEXP (addr, 1);
1922           code0 = GET_CODE (plus0);
1923           code1 = GET_CODE (plus1);
1924
1925           if (code0 == REG)
1926             {
1927               additional++;
1928               addr = plus1;
1929               looping = 1;
1930               continue;
1931             }
1932
1933           if (code0 == CONST_INT)
1934             {
1935               addr = plus1;
1936               looping = 1;
1937               continue;
1938             }
1939
1940           if (code1 == REG)
1941             {
1942               additional++;
1943               addr = plus0;
1944               looping = 1;
1945               continue;
1946             }
1947
1948           if (code1 == CONST_INT)
1949             {
1950               addr = plus0;
1951               looping = 1;
1952               continue;
1953             }
1954
1955           if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
1956             {
1957               addr = plus0;
1958               looping = 1;
1959               continue;
1960             }
1961
1962           if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
1963             {
1964               addr = plus1;
1965               looping = 1;
1966               continue;
1967             }
1968
1969           break;
1970
1971         case LABEL_REF:
1972           n_words = 2;          /* always 2 words */
1973           break;
1974
1975         case CONST:
1976           addr = XEXP (addr, 0);
1977           looping = 1;
1978           continue;
1979
1980         case SYMBOL_REF:
1981           n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
1982           break;
1983
1984         default:
1985           break;
1986         }
1987     }
1988   while (looping);
1989
1990   if (n_words == 0)
1991     return;
1992
1993   n_words += additional;
1994   if (n_words > 3)
1995     n_words = 3;
1996
1997   num_refs[n_words-1] += num;
1998 }
1999
2000 \f
2001 /* Return a pseudo that points to the address of the current function.
2002    The first time it is called for a function, an initializer for the
2003    pseudo is emitted in the beginning of the function.  */
2004
2005 rtx
2006 embedded_pic_fnaddr_reg ()
2007 {
2008   if (cfun->machine->embedded_pic_fnaddr_rtx == NULL)
2009     {
2010       rtx seq;
2011
2012       cfun->machine->embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
2013
2014       /* Output code at function start to initialize the pseudo-reg.  */
2015       /* ??? We used to do this in FINALIZE_PIC, but that does not work for
2016          inline functions, because it is called after RTL for the function
2017          has been copied.  The pseudo-reg in embedded_pic_fnaddr_rtx however
2018          does not get copied, and ends up not matching the rest of the RTL.
2019          This solution works, but means that we get unnecessary code to
2020          initialize this value every time a function is inlined into another
2021          function.  */
2022       start_sequence ();
2023       emit_insn (gen_get_fnaddr (cfun->machine->embedded_pic_fnaddr_rtx,
2024                                  XEXP (DECL_RTL (current_function_decl), 0)));
2025       seq = get_insns ();
2026       end_sequence ();
2027       push_topmost_sequence ();
2028       emit_insn_after (seq, get_insns ());
2029       pop_topmost_sequence ();
2030     }
2031
2032   return cfun->machine->embedded_pic_fnaddr_rtx;
2033 }
2034
2035 /* Return RTL for the offset from the current function to the argument.
2036    X is the symbol whose offset from the current function we want.  */
2037
2038 rtx
2039 embedded_pic_offset (x)
2040      rtx x;
2041 {
2042   /* Make sure it is emitted.  */
2043   embedded_pic_fnaddr_reg ();
2044
2045   return
2046     gen_rtx_CONST (Pmode,
2047                    gen_rtx_MINUS (Pmode, x,
2048                                   XEXP (DECL_RTL (current_function_decl), 0)));
2049 }
2050
2051 /* Return the appropriate instructions to move one operand to another.  */
2052
2053 const char *
2054 mips_move_1word (operands, insn, unsignedp)
2055      rtx operands[];
2056      rtx insn;
2057      int unsignedp;
2058 {
2059   const char *ret = 0;
2060   rtx op0 = operands[0];
2061   rtx op1 = operands[1];
2062   enum rtx_code code0 = GET_CODE (op0);
2063   enum rtx_code code1 = GET_CODE (op1);
2064   enum machine_mode mode = GET_MODE (op0);
2065   int subreg_offset0 = 0;
2066   int subreg_offset1 = 0;
2067   enum delay_type delay = DELAY_NONE;
2068
2069   while (code0 == SUBREG)
2070     {
2071       subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2072                                              GET_MODE (SUBREG_REG (op0)),
2073                                              SUBREG_BYTE (op0),
2074                                              GET_MODE (op0));
2075       op0 = SUBREG_REG (op0);
2076       code0 = GET_CODE (op0);
2077     }
2078
2079   while (code1 == SUBREG)
2080     {
2081       subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2082                                              GET_MODE (SUBREG_REG (op1)),
2083                                              SUBREG_BYTE (op1),
2084                                              GET_MODE (op1));
2085       op1 = SUBREG_REG (op1);
2086       code1 = GET_CODE (op1);
2087     }
2088
2089   /* For our purposes, a condition code mode is the same as SImode.  */
2090   if (mode == CCmode)
2091     mode = SImode;
2092
2093   if (code0 == REG)
2094     {
2095       int regno0 = REGNO (op0) + subreg_offset0;
2096
2097       if (code1 == REG)
2098         {
2099           int regno1 = REGNO (op1) + subreg_offset1;
2100
2101           /* Just in case, don't do anything for assigning a register
2102              to itself, unless we are filling a delay slot.  */
2103           if (regno0 == regno1 && set_nomacro == 0)
2104             ret = "";
2105
2106           else if (GP_REG_P (regno0))
2107             {
2108               if (GP_REG_P (regno1))
2109                 ret = "move\t%0,%1";
2110
2111               else if (MD_REG_P (regno1))
2112                 {
2113                   delay = DELAY_HILO;
2114                   if (regno1 != HILO_REGNUM)
2115                     ret = "mf%1\t%0";
2116                   else
2117                     ret = "mflo\t%0";
2118                 }
2119
2120               else if (ST_REG_P (regno1) && ISA_HAS_8CC)
2121                 ret = "li\t%0,1\n\tmovf\t%0,%.,%1";
2122
2123               else
2124                 {
2125                   delay = DELAY_LOAD;
2126                   if (FP_REG_P (regno1))
2127                     ret = "mfc1\t%0,%1";
2128                   else if (ALL_COP_REG_P (regno1))
2129                     {
2130                       static char retval[] = "mfc_\t%0,%1";
2131
2132                       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2133                       ret = retval;
2134                     }
2135                   else if (regno1 == FPSW_REGNUM && ! ISA_HAS_8CC)
2136                     ret = "cfc1\t%0,$31";
2137                 }
2138             }
2139
2140           else if (FP_REG_P (regno0))
2141             {
2142               if (GP_REG_P (regno1))
2143                 {
2144                   delay = DELAY_LOAD;
2145                   ret = "mtc1\t%1,%0";
2146                 }
2147
2148               if (FP_REG_P (regno1))
2149                 ret = "mov.s\t%0,%1";
2150             }
2151
2152           else if (MD_REG_P (regno0))
2153             {
2154               if (GP_REG_P (regno1))
2155                 {
2156                   delay = DELAY_HILO;
2157                   if (regno0 != HILO_REGNUM && ! TARGET_MIPS16)
2158                     ret = "mt%0\t%1";
2159                 }
2160             }
2161
2162           else if (regno0 == FPSW_REGNUM && ! ISA_HAS_8CC)
2163             {
2164               if (GP_REG_P (regno1))
2165                 {
2166                   delay = DELAY_LOAD;
2167                   ret = "ctc1\t%0,$31";
2168                 }
2169             }
2170           else if (ALL_COP_REG_P (regno0))
2171             {
2172               if (GP_REG_P (regno1))
2173                 {
2174                   static char retval[] = "mtc_\t%1,%0";
2175                   char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2176
2177                   if (cop == '0')
2178                     abort_with_insn (insn,
2179                                      "mtc0 not supported; it disturbs virtual address translation");
2180                   delay = DELAY_LOAD;
2181                   retval[3] = cop;
2182                   ret = retval;
2183                 }
2184             }
2185         }
2186
2187       else if (code1 == MEM)
2188         {
2189           delay = DELAY_LOAD;
2190
2191           if (TARGET_STATS)
2192             mips_count_memory_refs (op1, 1);
2193
2194           if (GP_REG_P (regno0))
2195             {
2196               /* For loads, use the mode of the memory item, instead of the
2197                  target, so zero/sign extend can use this code as well.  */
2198               switch (GET_MODE (op1))
2199                 {
2200                 default:
2201                   break;
2202                 case SFmode:
2203                   ret = "lw\t%0,%1";
2204                   break;
2205                 case SImode:
2206                 case CCmode:
2207                   ret = ((unsignedp && TARGET_64BIT)
2208                          ? "lwu\t%0,%1"
2209                          : "lw\t%0,%1");
2210                   break;
2211                 case HImode:
2212                   ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
2213                   break;
2214                 case QImode:
2215                   ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
2216                   break;
2217                 }
2218             }
2219
2220           else if (FP_REG_P (regno0) && (mode == SImode || mode == SFmode))
2221             ret = "l.s\t%0,%1";
2222
2223           else if (ALL_COP_REG_P (regno0))
2224             {
2225               static char retval[] = "lwc_\t%0,%1";
2226               char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2227
2228               if (cop == '0')
2229                 abort_with_insn (insn,
2230                                  "loads from memory to COP0 are illegal");
2231               delay = DELAY_LOAD;
2232               retval[3] = cop;
2233               ret = retval;
2234             }
2235
2236           if (ret != (char *)0 && MEM_VOLATILE_P (op1))
2237             {
2238               size_t i = strlen (ret);
2239               if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2240                 abort ();
2241
2242               sprintf (volatile_buffer, "%%{%s%%}", ret);
2243               ret = volatile_buffer;
2244             }
2245         }
2246
2247       else if (code1 == CONST_INT
2248                || (code1 == CONST_DOUBLE
2249                    && GET_MODE (op1) == VOIDmode))
2250         {
2251           if (code1 == CONST_DOUBLE)
2252             {
2253               /* This can happen when storing constants into long long
2254                  bitfields.  Just store the least significant word of
2255                  the value.  */
2256               operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2257             }
2258
2259           if (INTVAL (op1) == 0 && ! TARGET_MIPS16)
2260             {
2261               if (GP_REG_P (regno0))
2262                 ret = "move\t%0,%z1";
2263
2264               else if (FP_REG_P (regno0))
2265                 {
2266                   delay = DELAY_LOAD;
2267                   ret = "mtc1\t%z1,%0";
2268                 }
2269
2270               else if (MD_REG_P (regno0))
2271                 {
2272                   delay = DELAY_HILO;
2273                   ret = "mt%0\t%.";
2274                 }
2275             }
2276
2277           else if (GP_REG_P (regno0))
2278             {
2279               /* Don't use X format, because that will give out of
2280                  range numbers for 64 bit host and 32 bit target.  */
2281               if (! TARGET_MIPS16)
2282                 ret = "li\t%0,%1\t\t\t# %X1";
2283               else
2284                 {
2285                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2286                     ret = "li\t%0,%1";
2287                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2288                     ret = "li\t%0,%n1\n\tneg\t%0";
2289                 }
2290             }
2291         }
2292
2293       else if (code1 == CONST_DOUBLE && mode == SFmode)
2294         {
2295           if (op1 == CONST0_RTX (SFmode))
2296             {
2297               if (GP_REG_P (regno0))
2298                 ret = "move\t%0,%.";
2299
2300               else if (FP_REG_P (regno0))
2301                 {
2302                   delay = DELAY_LOAD;
2303                   ret = "mtc1\t%.,%0";
2304                 }
2305             }
2306
2307           else
2308             {
2309               delay = DELAY_LOAD;
2310               ret = "li.s\t%0,%1";
2311             }
2312         }
2313
2314       else if (code1 == LABEL_REF)
2315         {
2316           if (TARGET_STATS)
2317             mips_count_memory_refs (op1, 1);
2318
2319           ret = "la\t%0,%a1";
2320         }
2321
2322       else if (code1 == SYMBOL_REF || code1 == CONST)
2323         {
2324           if (TARGET_MIPS16
2325               && code1 == CONST
2326               && GET_CODE (XEXP (op1, 0)) == REG
2327               && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2328             {
2329               /* This case arises on the mips16; see
2330                  mips16_gp_pseudo_reg.  */
2331               ret = "move\t%0,%+";
2332             }
2333           else if (TARGET_MIPS16
2334                    && code1 == SYMBOL_REF
2335                    && SYMBOL_REF_FLAG (op1)
2336                    && (XSTR (op1, 0)[0] != '*'
2337                        || strncmp (XSTR (op1, 0) + 1,
2338                                    LOCAL_LABEL_PREFIX,
2339                                    sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2340             {
2341               /* This can occur when reloading the address of a GP
2342                  relative symbol on the mips16.  */
2343               ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2344             }
2345           else
2346             {
2347               if (TARGET_STATS)
2348                 mips_count_memory_refs (op1, 1);
2349
2350               ret = "la\t%0,%a1";
2351             }
2352         }
2353
2354       else if (code1 == PLUS)
2355         {
2356           rtx add_op0 = XEXP (op1, 0);
2357           rtx add_op1 = XEXP (op1, 1);
2358
2359           if (GET_CODE (XEXP (op1, 1)) == REG
2360               && GET_CODE (XEXP (op1, 0)) == CONST_INT)
2361             add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
2362
2363           operands[2] = add_op0;
2364           operands[3] = add_op1;
2365           ret = "add%:\t%0,%2,%3";
2366         }
2367
2368       else if (code1 == HIGH)
2369         {
2370           operands[1] = XEXP (op1, 0);
2371           ret = "lui\t%0,%%hi(%1)";
2372         }
2373     }
2374
2375   else if (code0 == MEM)
2376     {
2377       if (TARGET_STATS)
2378         mips_count_memory_refs (op0, 1);
2379
2380       if (code1 == REG)
2381         {
2382           int regno1 = REGNO (op1) + subreg_offset1;
2383
2384           if (GP_REG_P (regno1))
2385             {
2386               switch (mode)
2387                 {
2388                 case SFmode: ret = "sw\t%1,%0"; break;
2389                 case SImode: ret = "sw\t%1,%0"; break;
2390                 case HImode: ret = "sh\t%1,%0"; break;
2391                 case QImode: ret = "sb\t%1,%0"; break;
2392                 default: break;
2393                 }
2394             }
2395
2396           else if (FP_REG_P (regno1) && (mode == SImode || mode == SFmode))
2397             ret = "s.s\t%1,%0";
2398           else if (ALL_COP_REG_P (regno1))
2399             {
2400               static char retval[] = "swc_\t%1,%0";
2401
2402               retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2403               ret = retval;
2404             }
2405         }
2406
2407       else if (code1 == CONST_INT && INTVAL (op1) == 0)
2408         {
2409           switch (mode)
2410             {
2411             case SFmode: ret = "sw\t%z1,%0"; break;
2412             case SImode: ret = "sw\t%z1,%0"; break;
2413             case HImode: ret = "sh\t%z1,%0"; break;
2414             case QImode: ret = "sb\t%z1,%0"; break;
2415             default: break;
2416             }
2417         }
2418
2419       else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
2420         {
2421           switch (mode)
2422             {
2423             case SFmode: ret = "sw\t%.,%0"; break;
2424             case SImode: ret = "sw\t%.,%0"; break;
2425             case HImode: ret = "sh\t%.,%0"; break;
2426             case QImode: ret = "sb\t%.,%0"; break;
2427             default: break;
2428             }
2429         }
2430
2431       if (ret != 0 && MEM_VOLATILE_P (op0))
2432         {
2433           size_t i = strlen (ret);
2434
2435           if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2436             abort ();
2437
2438           sprintf (volatile_buffer, "%%{%s%%}", ret);
2439           ret = volatile_buffer;
2440         }
2441     }
2442
2443   if (ret == 0)
2444     {
2445       abort_with_insn (insn, "bad move");
2446       return 0;
2447     }
2448
2449   if (delay != DELAY_NONE)
2450     return mips_fill_delay_slot (ret, delay, operands, insn);
2451
2452   return ret;
2453 }
2454 \f
2455 /* Return instructions to restore the global pointer from the stack,
2456    assuming TARGET_ABICALLS.  Used by exception_receiver to set up
2457    the GP for exception handlers.
2458
2459    OPERANDS is an array of operands whose contents are undefined
2460    on entry.  INSN is the exception_handler instruction.  */
2461
2462 const char *
2463 mips_restore_gp (operands, insn)
2464      rtx *operands, insn;
2465 {
2466   rtx loc;
2467
2468   operands[0] = pic_offset_table_rtx;
2469   if (frame_pointer_needed)
2470     loc = hard_frame_pointer_rtx;
2471   else
2472     loc = stack_pointer_rtx;
2473   loc = plus_constant (loc, cfun->machine->frame.args_size);
2474   operands[1] = gen_rtx_MEM (Pmode, loc);
2475
2476   return mips_move_1word (operands, insn, 0);
2477 }
2478 \f
2479 /* Return an instruction to sign-extend SImode value SRC and store it
2480    in DImode value DEST.  INSN is the original extendsidi2-type insn.  */
2481
2482 const char *
2483 mips_sign_extend (insn, dest, src)
2484      rtx insn, dest, src;
2485 {
2486   rtx operands[MAX_RECOG_OPERANDS];
2487
2488   if ((register_operand (src, SImode) && FP_REG_P (true_regnum (src)))
2489       || memory_operand (src, SImode))
2490     {
2491       /* If the source is a floating-point register, we need to use a
2492          32-bit move, since the float register is not kept sign-extended.
2493          If the source is in memory, we need a 32-bit load.  */
2494       operands[0] = gen_lowpart_SUBREG (SImode, dest);
2495       operands[1] = src;
2496       return mips_move_1word (operands, insn, false);
2497     }
2498   else
2499     {
2500       operands[0] = dest;
2501       operands[1] = src;
2502       return mips_move_2words (operands, insn);
2503     }
2504 }
2505 \f
2506 /* Return the appropriate instructions to move 2 words */
2507
2508 const char *
2509 mips_move_2words (operands, insn)
2510      rtx operands[];
2511      rtx insn;
2512 {
2513   const char *ret = 0;
2514   rtx op0 = operands[0];
2515   rtx op1 = operands[1];
2516   enum rtx_code code0 = GET_CODE (operands[0]);
2517   enum rtx_code code1 = GET_CODE (operands[1]);
2518   int subreg_offset0 = 0;
2519   int subreg_offset1 = 0;
2520   enum delay_type delay = DELAY_NONE;
2521
2522   if (code1 == SIGN_EXTEND)
2523     return mips_sign_extend (insn, op0, XEXP (op1, 0));
2524
2525   while (code0 == SUBREG)
2526     {
2527       subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2528                                              GET_MODE (SUBREG_REG (op0)),
2529                                              SUBREG_BYTE (op0),
2530                                              GET_MODE (op0));
2531       op0 = SUBREG_REG (op0);
2532       code0 = GET_CODE (op0);
2533     }
2534
2535   while (code1 == SUBREG)
2536     {
2537       subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2538                                              GET_MODE (SUBREG_REG (op1)),
2539                                              SUBREG_BYTE (op1),
2540                                              GET_MODE (op1));
2541       op1 = SUBREG_REG (op1);
2542       code1 = GET_CODE (op1);
2543     }
2544
2545   if (code0 == REG)
2546     {
2547       int regno0 = REGNO (op0) + subreg_offset0;
2548
2549       if (code1 == REG)
2550         {
2551           int regno1 = REGNO (op1) + subreg_offset1;
2552
2553           /* Just in case, don't do anything for assigning a register
2554              to itself, unless we are filling a delay slot.  */
2555           if (regno0 == regno1 && set_nomacro == 0)
2556             ret = "";
2557
2558           else if (FP_REG_P (regno0))
2559             {
2560               if (FP_REG_P (regno1))
2561                 ret = "mov.d\t%0,%1";
2562
2563               else
2564                 {
2565                   delay = DELAY_LOAD;
2566                   if (TARGET_FLOAT64)
2567                     {
2568                       if (!TARGET_64BIT)
2569                         abort_with_insn (insn, "bad move");
2570
2571 #ifdef TARGET_FP_CALL_32
2572                       if (FP_CALL_GP_REG_P (regno1))
2573                         ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tdmtc1\t%1,%0";
2574                       else
2575 #endif
2576                         ret = "dmtc1\t%1,%0";
2577                     }
2578                   else
2579                     ret = "mtc1\t%L1,%0\n\tmtc1\t%M1,%D0";
2580                 }
2581             }
2582
2583           else if (FP_REG_P (regno1))
2584             {
2585               delay = DELAY_LOAD;
2586               if (TARGET_FLOAT64)
2587                 {
2588                   if (!TARGET_64BIT)
2589                     abort_with_insn (insn, "bad move");
2590
2591 #ifdef TARGET_FP_CALL_32
2592                   if (FP_CALL_GP_REG_P (regno0))
2593                     ret = "dmfc1\t%0,%1\n\tmfc1\t%D0,%1\n\tdsrl\t%0,32";
2594                   else
2595 #endif
2596                     ret = "dmfc1\t%0,%1";
2597                 }
2598               else
2599                 ret = "mfc1\t%L0,%1\n\tmfc1\t%M0,%D1";
2600             }
2601
2602           else if (MD_REG_P (regno0) && GP_REG_P (regno1) && !TARGET_MIPS16)
2603             {
2604               delay = DELAY_HILO;
2605               if (TARGET_64BIT)
2606                 {
2607                   if (regno0 != HILO_REGNUM)
2608                     ret = "mt%0\t%1";
2609                   else if (regno1 == 0)
2610                     ret = "mtlo\t%.\n\tmthi\t%.";
2611                 }
2612               else
2613                 ret = "mthi\t%M1\n\tmtlo\t%L1";
2614             }
2615
2616           else if (GP_REG_P (regno0) && MD_REG_P (regno1))
2617             {
2618               delay = DELAY_HILO;
2619               if (TARGET_64BIT)
2620                 {
2621                   if (regno1 != HILO_REGNUM)
2622                     ret = "mf%1\t%0";
2623                 }
2624               else
2625                 ret = "mfhi\t%M0\n\tmflo\t%L0";
2626             }
2627           else if (GP_REG_P (regno0) && ALL_COP_REG_P (regno1)
2628                    && TARGET_64BIT)
2629             {
2630               static char retval[] = "dmfc_\t%0,%1";
2631
2632               delay = DELAY_LOAD;
2633               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2634               ret = retval;
2635             }
2636           else if (ALL_COP_REG_P (regno0) && GP_REG_P (regno1)
2637                    && TARGET_64BIT)
2638             {
2639               static char retval[] = "dmtc_\t%1,%0";
2640               char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2641
2642               if (cop == '0')
2643                 abort_with_insn (insn,
2644                                  "dmtc0 not supported; it disturbs virtual address translation");
2645               delay = DELAY_LOAD;
2646               retval[4] = cop;
2647               ret = retval;
2648             }
2649           else if (TARGET_64BIT)
2650             ret = "move\t%0,%1";
2651
2652           else if (regno0 != (regno1+1))
2653             ret = "move\t%0,%1\n\tmove\t%D0,%D1";
2654
2655           else
2656             ret = "move\t%D0,%D1\n\tmove\t%0,%1";
2657         }
2658
2659       else if (code1 == CONST_DOUBLE)
2660         {
2661           /* Move zero from $0 unless !TARGET_64BIT and recipient
2662              is 64-bit fp reg, in which case generate a constant.  */
2663           if (op1 != CONST0_RTX (GET_MODE (op1))
2664               || (TARGET_FLOAT64 && !TARGET_64BIT && FP_REG_P (regno0)))
2665             {
2666               if (GET_MODE (op1) == DFmode)
2667                 {
2668                   delay = DELAY_LOAD;
2669
2670 #ifdef TARGET_FP_CALL_32
2671                   if (FP_CALL_GP_REG_P (regno0))
2672                     {
2673                       if (TARGET_FLOAT64 && !TARGET_64BIT)
2674                         {
2675                           split_double (op1, operands + 2, operands + 3);
2676                           ret = "li\t%0,%2\n\tli\t%D0,%3";
2677                         }
2678                       else
2679                         ret = "li.d\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32";
2680                     }
2681                   else
2682 #endif
2683                     /* GNU as emits 64-bit code for li.d if the ISA is 3
2684                        or higher.  For !TARGET_64BIT && gp registers we
2685                        need to avoid this by using two li instructions
2686                        instead.  */
2687                     if (ISA_HAS_64BIT_REGS
2688                         && ! TARGET_64BIT
2689                         && ! FP_REG_P (regno0))
2690                       {
2691                         split_double (op1, operands + 2, operands + 3);
2692                         ret = "li\t%0,%2\n\tli\t%D0,%3";
2693                       }
2694                     else
2695                       ret = "li.d\t%0,%1";
2696                 }
2697
2698               else if (TARGET_64BIT)
2699                 {
2700                   if (! TARGET_MIPS16)
2701                     ret = "dli\t%0,%1";
2702                 }
2703
2704               else
2705                 {
2706                   split_double (op1, operands + 2, operands + 3);
2707                   ret = "li\t%0,%2\n\tli\t%D0,%3";
2708                 }
2709             }
2710
2711           else
2712             {
2713               if (GP_REG_P (regno0))
2714                 ret = (TARGET_64BIT
2715 #ifdef TARGET_FP_CALL_32
2716                        && ! FP_CALL_GP_REG_P (regno0)
2717 #endif
2718                        ? "move\t%0,%."
2719                        : "move\t%0,%.\n\tmove\t%D0,%.");
2720
2721               else if (FP_REG_P (regno0))
2722                 {
2723                   delay = DELAY_LOAD;
2724                   ret = (TARGET_64BIT
2725                          ? "dmtc1\t%.,%0"
2726                          : "mtc1\t%.,%0\n\tmtc1\t%.,%D0");
2727                 }
2728             }
2729         }
2730
2731       else if (code1 == CONST_INT && INTVAL (op1) == 0 && ! TARGET_MIPS16)
2732         {
2733           if (GP_REG_P (regno0))
2734             ret = (TARGET_64BIT
2735                    ? "move\t%0,%."
2736                    : "move\t%0,%.\n\tmove\t%D0,%.");
2737
2738           else if (FP_REG_P (regno0))
2739             {
2740               delay = DELAY_LOAD;
2741               ret = (TARGET_64BIT
2742                      ? "dmtc1\t%.,%0"
2743                      : (TARGET_FLOAT64
2744                         ? "li.d\t%0,%1"
2745                         : "mtc1\t%.,%0\n\tmtc1\t%.,%D0"));
2746             }
2747           else if (MD_REG_P (regno0))
2748             {
2749               delay = DELAY_HILO;
2750               ret =  (regno0 == HILO_REGNUM
2751                       ? "mtlo\t%.\n\tmthi\t%."
2752                       : "mt%0\t%.\n");
2753             }
2754         }
2755
2756       else if (code1 == CONST_INT && GET_MODE (op0) == DImode
2757                && GP_REG_P (regno0))
2758         {
2759           if (TARGET_64BIT)
2760             {
2761               if (TARGET_MIPS16)
2762                 {
2763                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2764                     ret = "li\t%0,%1";
2765                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2766                     ret = "li\t%0,%n1\n\tneg\t%0";
2767                 }
2768               else if (GET_CODE (operands[1]) == SIGN_EXTEND)
2769                 ret = "li\t%0,%1\t\t# %X1";
2770               else if (HOST_BITS_PER_WIDE_INT < 64)
2771                 /* We can't use 'X' for negative numbers, because then we won't
2772                    get the right value for the upper 32 bits.  */
2773                 ret = (INTVAL (op1) < 0
2774                        ? "dli\t%0,%1\t\t\t# %X1"
2775                        : "dli\t%0,%X1\t\t# %1");
2776               else
2777                 /* We must use 'X', because otherwise LONG_MIN will print as
2778                    a number that the assembler won't accept.  */
2779                 ret = "dli\t%0,%X1\t\t# %1";
2780             }
2781           else if (HOST_BITS_PER_WIDE_INT < 64)
2782             {
2783               operands[2] = GEN_INT (INTVAL (operands[1]) >= 0 ? 0 : -1);
2784               if (TARGET_MIPS16)
2785                 {
2786                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2787                     ret = "li\t%M0,%2\n\tli\t%L0,%1";
2788                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2789                     {
2790                       operands[2] = GEN_INT (1);
2791                       ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2792                     }
2793                 }
2794               else
2795                 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2796             }
2797           else
2798             {
2799               /* We use multiple shifts here, to avoid warnings about out
2800                  of range shifts on 32 bit hosts.  */
2801               operands[2] = GEN_INT (INTVAL (operands[1]) >> 16 >> 16);
2802               operands[1]
2803                 = GEN_INT (INTVAL (operands[1]) << 16 << 16 >> 16 >> 16);
2804               if (TARGET_MIPS16)
2805                 {
2806                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2807                     ret = "li\t%M0,%2\n\tli\t%L0,%1";
2808                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2809                     {
2810                       operands[2] = GEN_INT (1);
2811                       ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2812                     }
2813                 }
2814               else
2815                 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2816             }
2817         }
2818
2819       else if (code1 == MEM)
2820         {
2821           delay = DELAY_LOAD;
2822
2823           if (TARGET_STATS)
2824             mips_count_memory_refs (op1, 2);
2825
2826           if (FP_REG_P (regno0))
2827             ret = "l.d\t%0,%1";
2828
2829           else if (ALL_COP_REG_P (regno0) && TARGET_64BIT)
2830             {
2831               static char retval[] = "ldc_\t%0,%1";
2832               char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2833
2834               if (cop == '0')
2835                 abort_with_insn (insn,
2836                                  "loads from memory to COP0 are illegal");
2837               delay = DELAY_LOAD;
2838               retval[3] = cop;
2839               ret = retval;
2840             }
2841
2842           else if (TARGET_64BIT)
2843             {
2844
2845 #ifdef TARGET_FP_CALL_32
2846               if (FP_CALL_GP_REG_P (regno0))
2847                 ret = (double_memory_operand (op1, GET_MODE (op1))
2848                        ? "lwu\t%0,%1\n\tlwu\t%D0,4+%1"
2849                        : "ld\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32");
2850               else
2851 #endif
2852                 ret = "ld\t%0,%1";
2853             }
2854
2855           else if (double_memory_operand (op1, GET_MODE (op1)))
2856             ret = (reg_mentioned_p (op0, op1)
2857                    ? "lw\t%D0,%D1\n\tlw\t%0,%1"
2858                    : "lw\t%0,%1\n\tlw\t%D0,%D1");
2859
2860           if (ret != 0 && MEM_VOLATILE_P (op1))
2861             {
2862               size_t i = strlen (ret);
2863
2864               if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2865                 abort ();
2866
2867               sprintf (volatile_buffer, "%%{%s%%}", ret);
2868               ret = volatile_buffer;
2869             }
2870         }
2871
2872       else if (code1 == LABEL_REF)
2873         {
2874           if (TARGET_STATS)
2875             mips_count_memory_refs (op1, 2);
2876
2877           if (GET_CODE (operands[1]) == SIGN_EXTEND)
2878             /* We deliberately remove the 'a' from '%1', so that we don't
2879                have to add SIGN_EXTEND support to print_operand_address.
2880                print_operand will just call print_operand_address in this
2881                case, so there is no problem.  */
2882             ret = "la\t%0,%1";
2883           else
2884             ret = "dla\t%0,%a1";
2885         }
2886       else if (code1 == SYMBOL_REF || code1 == CONST)
2887         {
2888           if (TARGET_MIPS16
2889               && code1 == CONST
2890               && GET_CODE (XEXP (op1, 0)) == REG
2891               && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2892             {
2893               /* This case arises on the mips16; see
2894                  mips16_gp_pseudo_reg.  */
2895               ret = "move\t%0,%+";
2896             }
2897           else if (TARGET_MIPS16
2898                    && code1 == SYMBOL_REF
2899                    && SYMBOL_REF_FLAG (op1)
2900                    && (XSTR (op1, 0)[0] != '*'
2901                        || strncmp (XSTR (op1, 0) + 1,
2902                                    LOCAL_LABEL_PREFIX,
2903                                    sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2904             {
2905               /* This can occur when reloading the address of a GP
2906                  relative symbol on the mips16.  */
2907               ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2908             }
2909           else
2910             {
2911               if (TARGET_STATS)
2912                 mips_count_memory_refs (op1, 2);
2913
2914               if (GET_CODE (operands[1]) == SIGN_EXTEND)
2915                 /* We deliberately remove the 'a' from '%1', so that we don't
2916                    have to add SIGN_EXTEND support to print_operand_address.
2917                    print_operand will just call print_operand_address in this
2918                    case, so there is no problem.  */
2919                 ret = "la\t%0,%1";
2920               else
2921                 ret = "dla\t%0,%a1";
2922             }
2923         }
2924     }
2925
2926   else if (code0 == MEM)
2927     {
2928       if (code1 == REG)
2929         {
2930           int regno1 = REGNO (op1) + subreg_offset1;
2931
2932           if (FP_REG_P (regno1))
2933             ret = "s.d\t%1,%0";
2934
2935           else if (ALL_COP_REG_P (regno1) && TARGET_64BIT)
2936             {
2937               static char retval[] = "sdc_\t%1,%0";
2938
2939               retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2940               ret = retval;
2941             }
2942           else if (TARGET_64BIT)
2943             {
2944
2945 #ifdef TARGET_FP_CALL_32
2946               if (FP_CALL_GP_REG_P (regno1))
2947                 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tsd\t%1,%0";
2948               else
2949 #endif
2950                 ret = "sd\t%1,%0";
2951             }
2952
2953           else if (double_memory_operand (op0, GET_MODE (op0)))
2954             ret = "sw\t%1,%0\n\tsw\t%D1,%D0";
2955         }
2956
2957       else if (((code1 == CONST_INT && INTVAL (op1) == 0)
2958                 || (code1 == CONST_DOUBLE
2959                     && op1 == CONST0_RTX (GET_MODE (op1))))
2960                && (TARGET_64BIT
2961                    || double_memory_operand (op0, GET_MODE (op0))))
2962         {
2963           if (TARGET_64BIT)
2964             ret = "sd\t%.,%0";
2965           else
2966             ret = "sw\t%.,%0\n\tsw\t%.,%D0";
2967         }
2968
2969       if (TARGET_STATS)
2970         mips_count_memory_refs (op0, 2);
2971
2972       if (ret != 0 && MEM_VOLATILE_P (op0))
2973         {
2974           size_t i = strlen (ret);
2975
2976           if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2977             abort ();
2978
2979           sprintf (volatile_buffer, "%%{%s%%}", ret);
2980           ret = volatile_buffer;
2981         }
2982     }
2983
2984   if (ret == 0)
2985     {
2986       abort_with_insn (insn, "bad move");
2987       return 0;
2988     }
2989
2990   if (delay != DELAY_NONE)
2991     return mips_fill_delay_slot (ret, delay, operands, insn);
2992
2993   return ret;
2994 }
2995 \f
2996 static bool
2997 mips_rtx_costs (x, code, outer_code, total)
2998      rtx x;
2999      int code, outer_code;
3000      int *total;
3001 {
3002   enum machine_mode mode = GET_MODE (x);
3003
3004   switch (code)
3005     {
3006     case CONST_INT:
3007       if (! TARGET_MIPS16)
3008         {
3009           /* Always return 0, since we don't have different sized insns,
3010              hence different costs according to Richard Kenner.  */
3011           *total = 0;
3012           return true;
3013         }
3014
3015       if (outer_code == SET)
3016         {
3017           if (INTVAL (x) >= 0 && INTVAL (x) < 0x100)
3018             *total = 0;
3019           else if ((INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3020                    || (INTVAL (x) < 0 && INTVAL (x) > -0x100))
3021             *total = COSTS_N_INSNS (1);
3022           else
3023             *total = COSTS_N_INSNS (2);
3024           return true;
3025         }
3026
3027       /* A PLUS could be an address.  We don't want to force an address
3028          to use a register, so accept any signed 16 bit value without
3029          complaint.  */
3030       if (outer_code == PLUS
3031           && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
3032         {
3033           *total = 0;
3034           return true;
3035         }
3036
3037       /* A number between 1 and 8 inclusive is efficient for a shift.
3038          Otherwise, we will need an extended instruction.  */
3039       if (outer_code == ASHIFT || outer_code == ASHIFTRT
3040           || outer_code == LSHIFTRT)
3041         {
3042           if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
3043             *total = 0;
3044           else
3045             *total = COSTS_N_INSNS (1);
3046           return true;
3047         }
3048
3049       /* We can use cmpi for an xor with an unsigned 16 bit value.  */
3050       if (outer_code == XOR
3051           && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3052         {
3053           *total = 0;
3054           return true;
3055         }
3056
3057       /* We may be able to use slt or sltu for a comparison with a
3058          signed 16 bit value.  (The boundary conditions aren't quite
3059          right, but this is just a heuristic anyhow.)  */
3060       if ((outer_code == LT || outer_code == LE
3061            || outer_code == GE || outer_code == GT
3062            || outer_code == LTU || outer_code == LEU
3063            || outer_code == GEU || outer_code == GTU)
3064           && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
3065         {
3066           *total = 0;
3067           return true;
3068         }
3069
3070       /* Equality comparisons with 0 are cheap.  */
3071       if ((outer_code == EQ || outer_code == NE)
3072           && INTVAL (x) == 0)
3073         return 0;
3074
3075       /* Otherwise, work out the cost to load the value into a
3076          register.  */
3077       if (INTVAL (x) >= 0 && INTVAL (x) < 0x100)
3078         *total = COSTS_N_INSNS (1);
3079       else if ((INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3080                || (INTVAL (x) < 0 && INTVAL (x) > -0x100))
3081         *total = COSTS_N_INSNS (2);
3082       else
3083         *total = COSTS_N_INSNS (3);
3084       return true;
3085
3086     case LABEL_REF:
3087       *total = COSTS_N_INSNS (2);
3088       return true;
3089
3090     case CONST:
3091       {
3092         rtx offset = const0_rtx;
3093         rtx symref = eliminate_constant_term (XEXP (x, 0), &offset);
3094
3095         if (TARGET_MIPS16 && mips16_gp_offset_p (x))
3096           {
3097             /* Treat this like a signed 16 bit CONST_INT.  */
3098             if (outer_code == PLUS)
3099               *total = 0;
3100             else if (outer_code == SET)
3101               *total = COSTS_N_INSNS (1);
3102             else
3103               *total = COSTS_N_INSNS (2);
3104             return true;
3105           }
3106
3107         if (GET_CODE (symref) == LABEL_REF)
3108           *total = COSTS_N_INSNS (2);
3109         else if (GET_CODE (symref) != SYMBOL_REF)
3110           *total = COSTS_N_INSNS (4);
3111         else if (INTVAL (offset) < -32768 || INTVAL (offset) > 32767)
3112           *total = COSTS_N_INSNS (2);
3113         else
3114           *total = COSTS_N_INSNS (SYMBOL_REF_FLAG (symref) ? 1 : 2);
3115
3116         return true;
3117       }
3118
3119     case SYMBOL_REF:
3120       *total = COSTS_N_INSNS (SYMBOL_REF_FLAG (x) ? 1 : 2);
3121       return true;
3122
3123     case CONST_DOUBLE:
3124       {
3125         rtx high, low;
3126         if (TARGET_MIPS16)
3127           {
3128             *total = COSTS_N_INSNS (4);
3129             return true;
3130           }
3131
3132         split_double (x, &high, &low);
3133         *total = COSTS_N_INSNS ((high == CONST0_RTX (GET_MODE (high))
3134                                  || low == CONST0_RTX (GET_MODE (low)))
3135                                 ? 2 : 4);
3136         return true;
3137       }
3138
3139     case MEM:
3140       {
3141         int num_words = (GET_MODE_SIZE (mode) > UNITS_PER_WORD) ? 2 : 1;
3142         if (simple_memory_operand (x, mode))
3143           *total = COSTS_N_INSNS (num_words);
3144         else
3145           *total = COSTS_N_INSNS (2*num_words);
3146         return true;
3147       }
3148
3149     case FFS:
3150       *total = COSTS_N_INSNS (6);
3151       return true;
3152
3153     case NOT:
3154       *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
3155       return true;
3156
3157     case AND:
3158     case IOR:
3159     case XOR:
3160       if (mode == DImode && !TARGET_64BIT)
3161         {
3162           *total = COSTS_N_INSNS (2);
3163           return true;
3164         }
3165       return false;
3166
3167     case ASHIFT:
3168     case ASHIFTRT:
3169     case LSHIFTRT:
3170       if (mode == DImode && !TARGET_64BIT)
3171         {
3172           *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
3173                                   ? 4 : 12);
3174           return true;
3175         }
3176       return false;
3177
3178     case ABS:
3179       if (mode == SFmode || mode == DFmode)
3180         *total = COSTS_N_INSNS (1);
3181       else
3182         *total = COSTS_N_INSNS (4);
3183       return true;
3184
3185     case PLUS:
3186     case MINUS:
3187       if (mode == SFmode || mode == DFmode)
3188         {
3189           if (TUNE_MIPS3000 || TUNE_MIPS3900)
3190             *total = COSTS_N_INSNS (2);
3191           else if (TUNE_MIPS6000)
3192             *total = COSTS_N_INSNS (3);
3193           else
3194             *total = COSTS_N_INSNS (6);
3195           return true;
3196         }
3197       if (mode == DImode && !TARGET_64BIT)
3198         {
3199           *total = COSTS_N_INSNS (4);
3200           return true;
3201         }
3202       return false;
3203
3204     case NEG:
3205       if (mode == DImode && !TARGET_64BIT)
3206         {
3207           *total = 4;
3208           return true;
3209         }
3210       return false;
3211
3212     case MULT:
3213       if (mode == SFmode)
3214         {
3215           if (TUNE_MIPS3000
3216               || TUNE_MIPS3900
3217               || TUNE_MIPS5000)
3218             *total = COSTS_N_INSNS (4);
3219           else if (TUNE_MIPS6000
3220                    || TUNE_MIPS5400
3221                    || TUNE_MIPS5500)
3222             *total = COSTS_N_INSNS (5);
3223           else
3224             *total = COSTS_N_INSNS (7);
3225           return true;
3226         }
3227
3228       if (mode == DFmode)
3229         {
3230           if (TUNE_MIPS3000
3231               || TUNE_MIPS3900
3232               || TUNE_MIPS5000)
3233             *total = COSTS_N_INSNS (5);
3234           else if (TUNE_MIPS6000
3235                    || TUNE_MIPS5400
3236                    || TUNE_MIPS5500)
3237             *total = COSTS_N_INSNS (6);
3238           else
3239             *total = COSTS_N_INSNS (8);
3240           return true;
3241         }
3242
3243       if (TUNE_MIPS3000)
3244         *total = COSTS_N_INSNS (12);
3245       else if (TUNE_MIPS3900)
3246         *total = COSTS_N_INSNS (2);
3247       else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3248         *total = COSTS_N_INSNS ((mode == DImode) ? 4 : 3);
3249       else if (TUNE_MIPS6000)
3250         *total = COSTS_N_INSNS (17);
3251       else if (TUNE_MIPS5000)
3252         *total = COSTS_N_INSNS (5);
3253       else
3254         *total = COSTS_N_INSNS (10);
3255       return true;
3256
3257     case DIV:
3258     case MOD:
3259       if (mode == SFmode)
3260         {
3261           if (TUNE_MIPS3000
3262               || TUNE_MIPS3900)
3263             *total = COSTS_N_INSNS (12);
3264           else if (TUNE_MIPS6000)
3265             *total = COSTS_N_INSNS (15);
3266           else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3267             *total = COSTS_N_INSNS (30);
3268           else
3269             *total = COSTS_N_INSNS (23);
3270           return true;
3271         }
3272
3273       if (mode == DFmode)
3274         {
3275           if (TUNE_MIPS3000
3276               || TUNE_MIPS3900)
3277             *total = COSTS_N_INSNS (19);
3278           else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3279             *total = COSTS_N_INSNS (59);
3280           else if (TUNE_MIPS6000)
3281             *total = COSTS_N_INSNS (16);
3282           else
3283             *total = COSTS_N_INSNS (36);
3284           return true;
3285         }
3286       /* FALLTHRU */
3287
3288     case UDIV:
3289     case UMOD:
3290       if (TUNE_MIPS3000
3291           || TUNE_MIPS3900)
3292         *total = COSTS_N_INSNS (35);
3293       else if (TUNE_MIPS6000)
3294         *total = COSTS_N_INSNS (38);
3295       else if (TUNE_MIPS5000)
3296         *total = COSTS_N_INSNS (36);
3297       else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3298         *total = COSTS_N_INSNS ((mode == SImode) ? 42 : 74);
3299       else
3300         *total = COSTS_N_INSNS (69);
3301       return true;
3302
3303     case SIGN_EXTEND:
3304       /* A sign extend from SImode to DImode in 64 bit mode is often
3305          zero instructions, because the result can often be used
3306          directly by another instruction; we'll call it one.  */
3307       if (TARGET_64BIT && mode == DImode
3308           && GET_MODE (XEXP (x, 0)) == SImode)
3309         *total = COSTS_N_INSNS (1);
3310       else
3311         *total = COSTS_N_INSNS (2);
3312       return true;
3313
3314     case ZERO_EXTEND:
3315       if (TARGET_64BIT && mode == DImode
3316           && GET_MODE (XEXP (x, 0)) == SImode)
3317         *total = COSTS_N_INSNS (2);
3318       else
3319         *total = COSTS_N_INSNS (1);
3320       return true;
3321
3322     default:
3323       return false;
3324     }
3325 }
3326
3327 /* Provide the costs of an addressing mode that contains ADDR.
3328    If ADDR is not a valid address, its cost is irrelevant.  */
3329
3330 static int
3331 mips_address_cost (addr)
3332      rtx addr;
3333 {
3334   switch (GET_CODE (addr))
3335     {
3336     case REG:
3337       return 1;
3338
3339     case LO_SUM:
3340       return 1;
3341
3342     case LABEL_REF:
3343       return 2;
3344
3345     case CONST:
3346       {
3347         rtx offset = const0_rtx;
3348         addr = eliminate_constant_term (XEXP (addr, 0), &offset);
3349         if (GET_CODE (addr) == LABEL_REF)
3350           return 2;
3351
3352         if (GET_CODE (addr) != SYMBOL_REF)
3353           return 4;
3354
3355         if (! SMALL_INT (offset))
3356           return 2;
3357       }
3358
3359       /* ... fall through ...  */
3360
3361     case SYMBOL_REF:
3362       return SYMBOL_REF_FLAG (addr) ? 1 : 2;
3363
3364     case PLUS:
3365       {
3366         register rtx plus0 = XEXP (addr, 0);
3367         register rtx plus1 = XEXP (addr, 1);
3368
3369         if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
3370           plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
3371
3372         if (GET_CODE (plus0) != REG)
3373           break;
3374
3375         switch (GET_CODE (plus1))
3376           {
3377           case CONST_INT:
3378             return SMALL_INT (plus1) ? 1 : 2;
3379
3380           case CONST:
3381           case SYMBOL_REF:
3382           case LABEL_REF:
3383           case HIGH:
3384           case LO_SUM:
3385             return mips_address_cost (plus1) + 1;
3386
3387           default:
3388             break;
3389           }
3390       }
3391
3392     default:
3393       break;
3394     }
3395
3396   return 4;
3397 }
3398
3399 /* Return nonzero if X is an address which needs a temporary register when
3400    reloaded while generating PIC code.  */
3401
3402 int
3403 pic_address_needs_scratch (x)
3404      rtx x;
3405 {
3406   /* An address which is a symbolic plus a non SMALL_INT needs a temp reg.  */
3407   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
3408       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
3409       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3410       && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
3411     return 1;
3412
3413   return 0;
3414 }
3415 \f
3416 /* Make normal rtx_code into something we can index from an array */
3417
3418 static enum internal_test
3419 map_test_to_internal_test (test_code)
3420      enum rtx_code test_code;
3421 {
3422   enum internal_test test = ITEST_MAX;
3423
3424   switch (test_code)
3425     {
3426     case EQ:  test = ITEST_EQ;  break;
3427     case NE:  test = ITEST_NE;  break;
3428     case GT:  test = ITEST_GT;  break;
3429     case GE:  test = ITEST_GE;  break;
3430     case LT:  test = ITEST_LT;  break;
3431     case LE:  test = ITEST_LE;  break;
3432     case GTU: test = ITEST_GTU; break;
3433     case GEU: test = ITEST_GEU; break;
3434     case LTU: test = ITEST_LTU; break;
3435     case LEU: test = ITEST_LEU; break;
3436     default:                    break;
3437     }
3438
3439   return test;
3440 }
3441
3442 \f
3443 /* Generate the code to compare two integer values.  The return value is:
3444    (reg:SI xx)          The pseudo register the comparison is in
3445    0                    No register, generate a simple branch.
3446
3447    ??? This is called with result nonzero by the Scond patterns in
3448    mips.md.  These patterns are called with a target in the mode of
3449    the Scond instruction pattern.  Since this must be a constant, we
3450    must use SImode.  This means that if RESULT is nonzero, it will
3451    always be an SImode register, even if TARGET_64BIT is true.  We
3452    cope with this by calling convert_move rather than emit_move_insn.
3453    This will sometimes lead to an unnecessary extension of the result;
3454    for example:
3455
3456    long long
3457    foo (long long i)
3458    {
3459      return i < 5;
3460    }
3461
3462    */
3463
3464 rtx
3465 gen_int_relational (test_code, result, cmp0, cmp1, p_invert)
3466      enum rtx_code test_code;   /* relational test (EQ, etc) */
3467      rtx result;                /* result to store comp. or 0 if branch */
3468      rtx cmp0;                  /* first operand to compare */
3469      rtx cmp1;                  /* second operand to compare */
3470      int *p_invert;             /* NULL or ptr to hold whether branch needs */
3471                                 /* to reverse its test */
3472 {
3473   struct cmp_info
3474   {
3475     enum rtx_code test_code;    /* code to use in instruction (LT vs. LTU) */
3476     int const_low;              /* low bound of constant we can accept */
3477     int const_high;             /* high bound of constant we can accept */
3478     int const_add;              /* constant to add (convert LE -> LT) */
3479     int reverse_regs;           /* reverse registers in test */
3480     int invert_const;           /* != 0 if invert value if cmp1 is constant */
3481     int invert_reg;             /* != 0 if invert value if cmp1 is register */
3482     int unsignedp;              /* != 0 for unsigned comparisons.  */
3483   };
3484
3485   static const struct cmp_info info[ (int)ITEST_MAX ] = {
3486
3487     { XOR,       0,  65535,  0,  0,  0,  0, 0 },        /* EQ  */
3488     { XOR,       0,  65535,  0,  0,  1,  1, 0 },        /* NE  */
3489     { LT,   -32769,  32766,  1,  1,  1,  0, 0 },        /* GT  */
3490     { LT,   -32768,  32767,  0,  0,  1,  1, 0 },        /* GE  */
3491     { LT,   -32768,  32767,  0,  0,  0,  0, 0 },        /* LT  */
3492     { LT,   -32769,  32766,  1,  1,  0,  1, 0 },        /* LE  */
3493     { LTU,  -32769,  32766,  1,  1,  1,  0, 1 },        /* GTU */
3494     { LTU,  -32768,  32767,  0,  0,  1,  1, 1 },        /* GEU */
3495     { LTU,  -32768,  32767,  0,  0,  0,  0, 1 },        /* LTU */
3496     { LTU,  -32769,  32766,  1,  1,  0,  1, 1 },        /* LEU */
3497   };
3498
3499   enum internal_test test;
3500   enum machine_mode mode;
3501   const struct cmp_info *p_info;
3502   int branch_p;
3503   int eqne_p;
3504   int invert;
3505   rtx reg;
3506   rtx reg2;
3507
3508   test = map_test_to_internal_test (test_code);
3509   if (test == ITEST_MAX)
3510     abort ();
3511
3512   p_info = &info[(int) test];
3513   eqne_p = (p_info->test_code == XOR);
3514
3515   mode = GET_MODE (cmp0);
3516   if (mode == VOIDmode)
3517     mode = GET_MODE (cmp1);
3518
3519   /* Eliminate simple branches */
3520   branch_p = (result == 0);
3521   if (branch_p)
3522     {
3523       if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
3524         {
3525           /* Comparisons against zero are simple branches */
3526           if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
3527               && (! TARGET_MIPS16 || eqne_p))
3528             return 0;
3529
3530           /* Test for beq/bne.  */
3531           if (eqne_p && ! TARGET_MIPS16)
3532             return 0;
3533         }
3534
3535       /* allocate a pseudo to calculate the value in.  */
3536       result = gen_reg_rtx (mode);
3537     }
3538
3539   /* Make sure we can handle any constants given to us.  */
3540   if (GET_CODE (cmp0) == CONST_INT)
3541     cmp0 = force_reg (mode, cmp0);
3542
3543   if (GET_CODE (cmp1) == CONST_INT)
3544     {
3545       HOST_WIDE_INT value = INTVAL (cmp1);
3546
3547       if (value < p_info->const_low
3548           || value > p_info->const_high
3549           /* ??? Why?  And why wasn't the similar code below modified too?  */
3550           || (TARGET_64BIT
3551               && HOST_BITS_PER_WIDE_INT < 64
3552               && p_info->const_add != 0
3553               && ((p_info->unsignedp
3554                    ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
3555                       > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
3556                    : (value + p_info->const_add) > INTVAL (cmp1))
3557                   != (p_info->const_add > 0))))
3558         cmp1 = force_reg (mode, cmp1);
3559     }
3560
3561   /* See if we need to invert the result.  */
3562   invert = (GET_CODE (cmp1) == CONST_INT
3563             ? p_info->invert_const : p_info->invert_reg);
3564
3565   if (p_invert != (int *)0)
3566     {
3567       *p_invert = invert;
3568       invert = 0;
3569     }
3570
3571   /* Comparison to constants, may involve adding 1 to change a LT into LE.
3572      Comparison between two registers, may involve switching operands.  */
3573   if (GET_CODE (cmp1) == CONST_INT)
3574     {
3575       if (p_info->const_add != 0)
3576         {
3577           HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
3578
3579           /* If modification of cmp1 caused overflow,
3580              we would get the wrong answer if we follow the usual path;
3581              thus, x > 0xffffffffU would turn into x > 0U.  */
3582           if ((p_info->unsignedp
3583                ? (unsigned HOST_WIDE_INT) new >
3584                (unsigned HOST_WIDE_INT) INTVAL (cmp1)
3585                : new > INTVAL (cmp1))
3586               != (p_info->const_add > 0))
3587             {
3588               /* This test is always true, but if INVERT is true then
3589                  the result of the test needs to be inverted so 0 should
3590                  be returned instead.  */
3591               emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
3592               return result;
3593             }
3594           else
3595             cmp1 = GEN_INT (new);
3596         }
3597     }
3598
3599   else if (p_info->reverse_regs)
3600     {
3601       rtx temp = cmp0;
3602       cmp0 = cmp1;
3603       cmp1 = temp;
3604     }
3605
3606   if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
3607     reg = cmp0;
3608   else
3609     {
3610       reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
3611       convert_move (reg, gen_rtx (p_info->test_code, mode, cmp0, cmp1), 0);
3612     }
3613
3614   if (test == ITEST_NE)
3615     {
3616       if (! TARGET_MIPS16)
3617         {
3618           convert_move (result, gen_rtx (GTU, mode, reg, const0_rtx), 0);
3619           if (p_invert != NULL)
3620             *p_invert = 0;
3621           invert = 0;
3622         }
3623       else
3624         {
3625           reg2 = invert ? gen_reg_rtx (mode) : result;
3626           convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
3627           reg = reg2;
3628         }
3629     }
3630
3631   else if (test == ITEST_EQ)
3632     {
3633       reg2 = invert ? gen_reg_rtx (mode) : result;
3634       convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3635       reg = reg2;
3636     }
3637
3638   if (invert)
3639     {
3640       rtx one;
3641
3642       if (! TARGET_MIPS16)
3643         one = const1_rtx;
3644       else
3645         {
3646           /* The value is in $24.  Copy it to another register, so
3647              that reload doesn't think it needs to store the $24 and
3648              the input to the XOR in the same location.  */
3649           reg2 = gen_reg_rtx (mode);
3650           emit_move_insn (reg2, reg);
3651           reg = reg2;
3652           one = force_reg (mode, const1_rtx);
3653         }
3654       convert_move (result, gen_rtx (XOR, mode, reg, one), 0);
3655     }
3656
3657   return result;
3658 }
3659 \f
3660 /* Work out how to check a floating-point condition.  We need a
3661    separate comparison instruction (C.cond.fmt), followed by a
3662    branch or conditional move.  Given that IN_CODE is the
3663    required condition, set *CMP_CODE to the C.cond.fmt code
3664    and *action_code to the branch or move code.  */
3665
3666 static void
3667 get_float_compare_codes (in_code, cmp_code, action_code)
3668      enum rtx_code in_code, *cmp_code, *action_code;
3669 {
3670   switch (in_code)
3671     {
3672     case NE:
3673     case UNGE:
3674     case UNGT:
3675     case LTGT:
3676     case ORDERED:
3677       *cmp_code = reverse_condition_maybe_unordered (in_code);
3678       *action_code = EQ;
3679       break;
3680
3681     default:
3682       *cmp_code = in_code;
3683       *action_code = NE;
3684       break;
3685     }
3686 }
3687
3688 /* Emit the common code for doing conditional branches.
3689    operand[0] is the label to jump to.
3690    The comparison operands are saved away by cmp{si,di,sf,df}.  */
3691
3692 void
3693 gen_conditional_branch (operands, test_code)
3694      rtx operands[];
3695      enum rtx_code test_code;
3696 {
3697   enum cmp_type type = branch_type;
3698   rtx cmp0 = branch_cmp[0];
3699   rtx cmp1 = branch_cmp[1];
3700   enum machine_mode mode;
3701   enum rtx_code cmp_code;
3702   rtx reg;
3703   int invert;
3704   rtx label1, label2;
3705
3706   switch (type)
3707     {
3708     case CMP_SI:
3709     case CMP_DI:
3710       mode = type == CMP_SI ? SImode : DImode;
3711       invert = 0;
3712       reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3713
3714       if (reg)
3715         {
3716           cmp0 = reg;
3717           cmp1 = const0_rtx;
3718           test_code = NE;
3719         }
3720       else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3721         /* We don't want to build a comparison against a nonzero
3722            constant.  */
3723         cmp1 = force_reg (mode, cmp1);
3724
3725       break;
3726
3727     case CMP_SF:
3728     case CMP_DF:
3729       if (! ISA_HAS_8CC)
3730         reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3731       else
3732         reg = gen_reg_rtx (CCmode);
3733
3734       get_float_compare_codes (test_code, &cmp_code, &test_code);
3735       emit_insn (gen_rtx_SET (VOIDmode, reg,
3736                               gen_rtx (cmp_code, CCmode, cmp0, cmp1)));
3737
3738       mode = CCmode;
3739       cmp0 = reg;
3740       cmp1 = const0_rtx;
3741       invert = 0;
3742       break;
3743
3744     default:
3745       abort_with_insn (gen_rtx (test_code, VOIDmode, cmp0, cmp1), "bad test");
3746     }
3747
3748   /* Generate the branch.  */
3749
3750   label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3751   label2 = pc_rtx;
3752
3753   if (invert)
3754     {
3755       label2 = label1;
3756       label1 = pc_rtx;
3757     }
3758
3759   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
3760                                gen_rtx_IF_THEN_ELSE (VOIDmode,
3761                                                      gen_rtx (test_code, mode,
3762                                                               cmp0, cmp1),
3763                                                      label1, label2)));
3764 }
3765
3766 /* Emit the common code for conditional moves.  OPERANDS is the array
3767    of operands passed to the conditional move defined_expand.  */
3768
3769 void
3770 gen_conditional_move (operands)
3771      rtx *operands;
3772 {
3773   rtx op0 = branch_cmp[0];
3774   rtx op1 = branch_cmp[1];
3775   enum machine_mode mode = GET_MODE (branch_cmp[0]);
3776   enum rtx_code cmp_code = GET_CODE (operands[1]);
3777   enum rtx_code move_code = NE;
3778   enum machine_mode op_mode = GET_MODE (operands[0]);
3779   enum machine_mode cmp_mode;
3780   rtx cmp_reg;
3781
3782   if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3783     {
3784       switch (cmp_code)
3785         {
3786         case EQ:
3787           cmp_code = XOR;
3788           move_code = EQ;
3789           break;
3790         case NE:
3791           cmp_code = XOR;
3792           break;
3793         case LT:
3794           break;
3795         case GE:
3796           cmp_code = LT;
3797           move_code = EQ;
3798           break;
3799         case GT:
3800           cmp_code = LT;
3801           op0 = force_reg (mode, branch_cmp[1]);
3802           op1 = branch_cmp[0];
3803           break;
3804         case LE:
3805           cmp_code = LT;
3806           op0 = force_reg (mode, branch_cmp[1]);
3807           op1 = branch_cmp[0];
3808           move_code = EQ;
3809           break;
3810         case LTU:
3811           break;
3812         case GEU:
3813           cmp_code = LTU;
3814           move_code = EQ;
3815           break;
3816         case GTU:
3817           cmp_code = LTU;
3818           op0 = force_reg (mode, branch_cmp[1]);
3819           op1 = branch_cmp[0];
3820           break;
3821         case LEU:
3822           cmp_code = LTU;
3823           op0 = force_reg (mode, branch_cmp[1]);
3824           op1 = branch_cmp[0];
3825           move_code = EQ;
3826           break;
3827         default:
3828           abort ();
3829         }
3830     }
3831   else
3832     get_float_compare_codes (cmp_code, &cmp_code, &move_code);
3833
3834   if (mode == SImode || mode == DImode)
3835     cmp_mode = mode;
3836   else if (mode == SFmode || mode == DFmode)
3837     cmp_mode = CCmode;
3838   else
3839     abort ();
3840
3841   cmp_reg = gen_reg_rtx (cmp_mode);
3842   emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3843                           gen_rtx (cmp_code, cmp_mode, op0, op1)));
3844
3845   emit_insn (gen_rtx_SET (op_mode, operands[0],
3846                           gen_rtx_IF_THEN_ELSE (op_mode,
3847                                                 gen_rtx (move_code, VOIDmode,
3848                                                          cmp_reg,
3849                                                          CONST0_RTX (SImode)),
3850                                                 operands[2], operands[3])));
3851 }
3852
3853 /* Emit the common code for conditional moves.  OPERANDS is the array
3854    of operands passed to the conditional move defined_expand.  */
3855
3856 void
3857 mips_gen_conditional_trap (operands)
3858      rtx operands[];
3859 {
3860   rtx op0, op1;
3861   enum rtx_code cmp_code = GET_CODE (operands[0]);
3862   enum machine_mode mode = GET_MODE (branch_cmp[0]);
3863
3864   /* MIPS conditional trap machine instructions don't have GT or LE
3865      flavors, so we must invert the comparison and convert to LT and
3866      GE, respectively.  */
3867   switch (cmp_code)
3868     {
3869     case GT: cmp_code = LT; break;
3870     case LE: cmp_code = GE; break;
3871     case GTU: cmp_code = LTU; break;
3872     case LEU: cmp_code = GEU; break;
3873     default: break;
3874     }
3875   if (cmp_code == GET_CODE (operands[0]))
3876     {
3877       op0 = force_reg (mode, branch_cmp[0]);
3878       op1 = branch_cmp[1];
3879     }
3880   else
3881     {
3882       op0 = force_reg (mode, branch_cmp[1]);
3883       op1 = branch_cmp[0];
3884     }
3885   if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3886     op1 = force_reg (mode, op1);
3887
3888   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3889                               gen_rtx (cmp_code, GET_MODE (operands[0]), op0, op1),
3890                               operands[1]));
3891 }
3892 \f
3893 /* Return true if operand OP is a condition code register.
3894    Only for use during or after reload.  */
3895
3896 int
3897 fcc_register_operand (op, mode)
3898      rtx op;
3899      enum machine_mode mode;
3900 {
3901   return ((mode == VOIDmode || mode == GET_MODE (op))
3902           && (reload_in_progress || reload_completed)
3903           && (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
3904           && ST_REG_P (true_regnum (op)));
3905 }
3906
3907 /* Emit code to move general operand SRC into condition-code
3908    register DEST.  SCRATCH is a scratch TFmode float register.
3909    The sequence is:
3910
3911         FP1 = SRC
3912         FP2 = 0.0f
3913         DEST = FP2 < FP1
3914
3915    where FP1 and FP2 are single-precision float registers
3916    taken from SCRATCH.  */
3917
3918 void
3919 mips_emit_fcc_reload (dest, src, scratch)
3920      rtx dest, src, scratch;
3921 {
3922   rtx fp1, fp2;
3923
3924   /* Change the source to SFmode.  */
3925   if (GET_CODE (src) == MEM)
3926     src = adjust_address (src, SFmode, 0);
3927   else if (GET_CODE (src) == REG || GET_CODE (src) == SUBREG)
3928     src = gen_rtx_REG (SFmode, true_regnum (src));
3929
3930   fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3931   fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3932
3933   emit_move_insn (copy_rtx (fp1), src);
3934   emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3935   emit_insn (gen_slt_sf (dest, fp2, fp1));
3936 }
3937 \f
3938 /* Emit code to change the current function's return address to
3939    ADDRESS.  SCRATCH is available as a scratch register, if needed.
3940    ADDRESS and SCRATCH are both word-mode GPRs.  */
3941
3942 void
3943 mips_set_return_address (address, scratch)
3944      rtx address, scratch;
3945 {
3946   HOST_WIDE_INT gp_offset;
3947
3948   compute_frame_size (get_frame_size ());
3949   if (((cfun->machine->frame.mask >> 31) & 1) == 0)
3950     abort ();
3951   gp_offset = cfun->machine->frame.gp_sp_offset;
3952
3953   /* Reduce SP + GP_OFSET to a legitimate address and put it in SCRATCH.  */
3954   if (gp_offset < 32768)
3955     scratch = plus_constant (stack_pointer_rtx, gp_offset);
3956   else
3957     {
3958       emit_move_insn (scratch, GEN_INT (gp_offset));
3959       if (Pmode == DImode)
3960         emit_insn (gen_adddi3 (scratch, scratch, stack_pointer_rtx));
3961       else
3962         emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
3963     }
3964
3965   emit_move_insn (gen_rtx_MEM (GET_MODE (address), scratch), address);
3966 }
3967 \f
3968 /* Write a loop to move a constant number of bytes.
3969    Generate load/stores as follows:
3970
3971    do {
3972      temp1 = src[0];
3973      temp2 = src[1];
3974      ...
3975      temp<last> = src[MAX_MOVE_REGS-1];
3976      dest[0] = temp1;
3977      dest[1] = temp2;
3978      ...
3979      dest[MAX_MOVE_REGS-1] = temp<last>;
3980      src += MAX_MOVE_REGS;
3981      dest += MAX_MOVE_REGS;
3982    } while (src != final);
3983
3984    This way, no NOP's are needed, and only MAX_MOVE_REGS+3 temp
3985    registers are needed.
3986
3987    Aligned moves move MAX_MOVE_REGS*4 bytes every (2*MAX_MOVE_REGS)+3
3988    cycles, unaligned moves move MAX_MOVE_REGS*4 bytes every
3989    (4*MAX_MOVE_REGS)+3 cycles, assuming no cache misses.  */
3990
3991 #define MAX_MOVE_REGS 4
3992 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3993
3994 static void
3995 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src)
3996      rtx dest_reg;              /* register holding destination address */
3997      rtx src_reg;               /* register holding source address */
3998      unsigned int bytes;        /* # bytes to move */
3999      int align;                 /* alignment */
4000      rtx orig_dest;             /* original dest */
4001      rtx orig_src;              /* original source for making a reg note */
4002 {
4003   rtx dest_mem = replace_equiv_address (orig_dest, dest_reg);
4004   rtx src_mem = replace_equiv_address (orig_src, src_reg);
4005   rtx align_rtx = GEN_INT (align);
4006   rtx label;
4007   rtx final_src;
4008   rtx bytes_rtx;
4009   int leftover;
4010
4011   if (bytes < (unsigned)2 * MAX_MOVE_BYTES)
4012     abort ();
4013
4014   leftover = bytes % MAX_MOVE_BYTES;
4015   bytes -= leftover;
4016
4017   label = gen_label_rtx ();
4018   final_src = gen_reg_rtx (Pmode);
4019   bytes_rtx = GEN_INT (bytes);
4020
4021   if (bytes > 0x7fff)
4022     {
4023       if (Pmode == DImode)
4024         {
4025           emit_insn (gen_movdi (final_src, bytes_rtx));
4026           emit_insn (gen_adddi3 (final_src, final_src, src_reg));
4027         }
4028       else
4029         {
4030           emit_insn (gen_movsi (final_src, bytes_rtx));
4031           emit_insn (gen_addsi3 (final_src, final_src, src_reg));
4032         }
4033     }
4034   else
4035     {
4036       if (Pmode == DImode)
4037         emit_insn (gen_adddi3 (final_src, src_reg, bytes_rtx));
4038       else
4039         emit_insn (gen_addsi3 (final_src, src_reg, bytes_rtx));
4040     }
4041
4042   emit_label (label);
4043
4044   bytes_rtx = GEN_INT (MAX_MOVE_BYTES);
4045   emit_insn (gen_movstrsi_internal (dest_mem, src_mem, bytes_rtx, align_rtx));
4046
4047   if (Pmode == DImode)
4048     {
4049       emit_insn (gen_adddi3 (src_reg, src_reg, bytes_rtx));
4050       emit_insn (gen_adddi3 (dest_reg, dest_reg, bytes_rtx));
4051       emit_insn (gen_cmpdi (src_reg, final_src));
4052     }
4053   else
4054     {
4055       emit_insn (gen_addsi3 (src_reg, src_reg, bytes_rtx));
4056       emit_insn (gen_addsi3 (dest_reg, dest_reg, bytes_rtx));
4057       emit_insn (gen_cmpsi (src_reg, final_src));
4058     }
4059
4060   emit_jump_insn (gen_bne (label));
4061
4062   if (leftover)
4063     emit_insn (gen_movstrsi_internal (dest_mem, src_mem, GEN_INT (leftover),
4064                                       align_rtx));
4065 }
4066 \f
4067 /* Use a library function to move some bytes.  */
4068
4069 static void
4070 block_move_call (dest_reg, src_reg, bytes_rtx)
4071      rtx dest_reg;
4072      rtx src_reg;
4073      rtx bytes_rtx;
4074 {
4075   /* We want to pass the size as Pmode, which will normally be SImode
4076      but will be DImode if we are using 64 bit longs and pointers.  */
4077   if (GET_MODE (bytes_rtx) != VOIDmode
4078       && GET_MODE (bytes_rtx) != (unsigned) Pmode)
4079     bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
4080
4081 #ifdef TARGET_MEM_FUNCTIONS
4082   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memcpy"), 0,
4083                      VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
4084                      convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
4085                                       TREE_UNSIGNED (sizetype)),
4086                      TYPE_MODE (sizetype));
4087 #else
4088   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "bcopy"), 0,
4089                      VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
4090                      convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
4091                                       TREE_UNSIGNED (integer_type_node)),
4092                      TYPE_MODE (integer_type_node));
4093 #endif
4094 }
4095 \f
4096 /* Expand string/block move operations.
4097
4098    operands[0] is the pointer to the destination.
4099    operands[1] is the pointer to the source.
4100    operands[2] is the number of bytes to move.
4101    operands[3] is the alignment.  */
4102
4103 void
4104 expand_block_move (operands)
4105      rtx operands[];
4106 {
4107   rtx bytes_rtx = operands[2];
4108   rtx align_rtx = operands[3];
4109   int constp = GET_CODE (bytes_rtx) == CONST_INT;
4110   unsigned HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
4111   unsigned int align = INTVAL (align_rtx);
4112   rtx orig_src  = operands[1];
4113   rtx orig_dest = operands[0];
4114   rtx src_reg;
4115   rtx dest_reg;
4116
4117   if (constp && bytes == 0)
4118     return;
4119
4120   if (align > (unsigned) UNITS_PER_WORD)
4121     align = UNITS_PER_WORD;
4122
4123   /* Move the address into scratch registers.  */
4124   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
4125   src_reg  = copy_addr_to_reg (XEXP (orig_src, 0));
4126
4127   if (TARGET_MEMCPY)
4128     block_move_call (dest_reg, src_reg, bytes_rtx);
4129
4130   else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES
4131            && align == (unsigned) UNITS_PER_WORD)
4132     move_by_pieces (orig_dest, orig_src, bytes, align * BITS_PER_WORD);
4133
4134   else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES)
4135     emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
4136                                                              dest_reg),
4137                                       replace_equiv_address (orig_src,
4138                                                              src_reg),
4139                                       bytes_rtx, align_rtx));
4140
4141   else if (constp && align >= (unsigned) UNITS_PER_WORD && optimize)
4142     block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src);
4143
4144   else if (constp && optimize)
4145     {
4146       /* If the alignment is not word aligned, generate a test at
4147          runtime, to see whether things wound up aligned, and we
4148          can use the faster lw/sw instead ulw/usw.  */
4149
4150       rtx temp = gen_reg_rtx (Pmode);
4151       rtx aligned_label = gen_label_rtx ();
4152       rtx join_label = gen_label_rtx ();
4153       int leftover = bytes % MAX_MOVE_BYTES;
4154
4155       bytes -= leftover;
4156
4157       if (Pmode == DImode)
4158         {
4159           emit_insn (gen_iordi3 (temp, src_reg, dest_reg));
4160           emit_insn (gen_anddi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
4161           emit_insn (gen_cmpdi (temp, const0_rtx));
4162         }
4163       else
4164         {
4165           emit_insn (gen_iorsi3 (temp, src_reg, dest_reg));
4166           emit_insn (gen_andsi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
4167           emit_insn (gen_cmpsi (temp, const0_rtx));
4168         }
4169
4170       emit_jump_insn (gen_beq (aligned_label));
4171
4172       /* Unaligned loop.  */
4173       block_move_loop (dest_reg, src_reg, bytes, 1, orig_dest, orig_src);
4174       emit_jump_insn (gen_jump (join_label));
4175       emit_barrier ();
4176
4177       /* Aligned loop.  */
4178       emit_label (aligned_label);
4179       block_move_loop (dest_reg, src_reg, bytes, UNITS_PER_WORD, orig_dest,
4180                        orig_src);
4181       emit_label (join_label);
4182
4183       /* Bytes at the end of the loop.  */
4184       if (leftover)
4185         emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
4186                                                                  dest_reg),
4187                                           replace_equiv_address (orig_src,
4188                                                                  src_reg),
4189                                           GEN_INT (leftover),
4190                                           GEN_INT (align)));
4191     }
4192
4193   else
4194     block_move_call (dest_reg, src_reg, bytes_rtx);
4195 }
4196 \f
4197 /* Emit load/stores for a small constant block_move.
4198
4199    operands[0] is the memory address of the destination.
4200    operands[1] is the memory address of the source.
4201    operands[2] is the number of bytes to move.
4202    operands[3] is the alignment.
4203    operands[4] is a temp register.
4204    operands[5] is a temp register.
4205    ...
4206    operands[3+num_regs] is the last temp register.
4207
4208    The block move type can be one of the following:
4209         BLOCK_MOVE_NORMAL       Do all of the block move.
4210         BLOCK_MOVE_NOT_LAST     Do all but the last store.
4211         BLOCK_MOVE_LAST         Do just the last store.  */
4212
4213 const char *
4214 output_block_move (insn, operands, num_regs, move_type)
4215      rtx insn;
4216      rtx operands[];
4217      int num_regs;
4218      enum block_move_type move_type;
4219 {
4220   rtx dest_reg = XEXP (operands[0], 0);
4221   rtx src_reg = XEXP (operands[1], 0);
4222   HOST_WIDE_INT bytes = INTVAL (operands[2]);
4223   int align = INTVAL (operands[3]);
4224   int num = 0;
4225   int offset = 0;
4226   int use_lwl_lwr = 0;
4227   int last_operand = num_regs + 4;
4228   int safe_regs = 4;
4229   int i;
4230   rtx xoperands[10];
4231
4232   struct {
4233     const char *load;           /* load insn without nop */
4234     const char *load_nop;       /* load insn with trailing nop */
4235     const char *store;          /* store insn */
4236     const char *final;          /* if last_store used: NULL or swr */
4237     const char *last_store;     /* last store instruction */
4238     int offset;                 /* current offset */
4239     enum machine_mode mode;     /* mode to use on (MEM) */
4240   } load_store[4];
4241
4242   /* ??? Detect a bug in GCC, where it can give us a register
4243      the same as one of the addressing registers and reduce
4244      the number of registers available.  */
4245   for (i = 4; i < last_operand && safe_regs < (int) ARRAY_SIZE (xoperands); i++)
4246     if (! reg_mentioned_p (operands[i], operands[0])
4247         && ! reg_mentioned_p (operands[i], operands[1]))
4248       xoperands[safe_regs++] = operands[i];
4249
4250   if (safe_regs < last_operand)
4251     {
4252       xoperands[0] = operands[0];
4253       xoperands[1] = operands[1];
4254       xoperands[2] = operands[2];
4255       xoperands[3] = operands[3];
4256       return output_block_move (insn, xoperands, safe_regs - 4, move_type);
4257     }
4258
4259   /* If we are given global or static addresses, and we would be
4260      emitting a few instructions, try to save time by using a
4261      temporary register for the pointer.  */
4262   /* ??? The SGI Irix6 assembler fails when a SYMBOL_REF is used in
4263      an ldl/ldr instruction pair.  We play it safe, and always move
4264      constant addresses into registers when generating N32/N64 code, just
4265      in case we might emit an unaligned load instruction.  */
4266   if (num_regs > 2 && (bytes > 2 * align || move_type != BLOCK_MOVE_NORMAL
4267                        || mips_abi == ABI_MEABI
4268                        || mips_abi == ABI_N32
4269                        || mips_abi == ABI_64))
4270     {
4271       if (CONSTANT_P (src_reg))
4272         {
4273           if (TARGET_STATS)
4274             mips_count_memory_refs (operands[1], 1);
4275
4276           src_reg = operands[3 + num_regs--];
4277           if (move_type != BLOCK_MOVE_LAST)
4278             {
4279               xoperands[1] = operands[1];
4280               xoperands[0] = src_reg;
4281               if (Pmode == DImode)
4282                 output_asm_insn ("dla\t%0,%1", xoperands);
4283               else
4284                 output_asm_insn ("la\t%0,%1", xoperands);
4285             }
4286         }
4287
4288       if (CONSTANT_P (dest_reg))
4289         {
4290           if (TARGET_STATS)
4291             mips_count_memory_refs (operands[0], 1);
4292
4293           dest_reg = operands[3 + num_regs--];
4294           if (move_type != BLOCK_MOVE_LAST)
4295             {
4296               xoperands[1] = operands[0];
4297               xoperands[0] = dest_reg;
4298               if (Pmode == DImode)
4299                 output_asm_insn ("dla\t%0,%1", xoperands);
4300               else
4301                 output_asm_insn ("la\t%0,%1", xoperands);
4302             }
4303         }
4304     }
4305
4306   /* ??? We really shouldn't get any LO_SUM addresses here, because they
4307      are not offsettable, however, offsettable_address_p says they are
4308      offsettable. I think this is a bug in offsettable_address_p.
4309      For expediency, we fix this by just loading the address into a register
4310      if we happen to get one.  */
4311
4312   if (GET_CODE (src_reg) == LO_SUM)
4313     {
4314       src_reg = operands[3 + num_regs--];
4315       if (move_type != BLOCK_MOVE_LAST)
4316         {
4317           xoperands[2] = XEXP (XEXP (operands[1], 0), 1);
4318           xoperands[1] = XEXP (XEXP (operands[1], 0), 0);
4319           xoperands[0] = src_reg;
4320           if (Pmode == DImode)
4321             output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
4322           else
4323             output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
4324         }
4325     }
4326
4327   if (GET_CODE (dest_reg) == LO_SUM)
4328     {
4329       dest_reg = operands[3 + num_regs--];
4330       if (move_type != BLOCK_MOVE_LAST)
4331         {
4332           xoperands[2] = XEXP (XEXP (operands[0], 0), 1);
4333           xoperands[1] = XEXP (XEXP (operands[0], 0), 0);
4334           xoperands[0] = dest_reg;
4335           if (Pmode == DImode)
4336             output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
4337           else
4338             output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
4339         }
4340     }
4341
4342   if (num_regs > (int) ARRAY_SIZE (load_store))
4343     num_regs = ARRAY_SIZE (load_store);
4344
4345   else if (num_regs < 1)
4346     abort_with_insn (insn,
4347                      "cannot do block move, not enough scratch registers");
4348
4349   while (bytes > 0)
4350     {
4351       load_store[num].offset = offset;
4352
4353       if (TARGET_64BIT && bytes >= 8 && align >= 8)
4354         {
4355           load_store[num].load = "ld\t%0,%1";
4356           load_store[num].load_nop = "ld\t%0,%1%#";
4357           load_store[num].store = "sd\t%0,%1";
4358           load_store[num].last_store = "sd\t%0,%1";
4359           load_store[num].final = 0;
4360           load_store[num].mode = DImode;
4361           offset += 8;
4362           bytes -= 8;
4363         }
4364
4365       /* ??? Fails because of a MIPS assembler bug?  */
4366       else if (TARGET_64BIT && bytes >= 8
4367                && ! TARGET_SR71K
4368                && ! TARGET_MIPS16)
4369         {
4370           if (BYTES_BIG_ENDIAN)
4371             {
4372               load_store[num].load = "ldl\t%0,%1\n\tldr\t%0,%2";
4373               load_store[num].load_nop = "ldl\t%0,%1\n\tldr\t%0,%2%#";
4374               load_store[num].store = "sdl\t%0,%1\n\tsdr\t%0,%2";
4375               load_store[num].last_store = "sdr\t%0,%2";
4376               load_store[num].final = "sdl\t%0,%1";
4377             }
4378           else
4379             {
4380               load_store[num].load = "ldl\t%0,%2\n\tldr\t%0,%1";
4381               load_store[num].load_nop = "ldl\t%0,%2\n\tldr\t%0,%1%#";
4382               load_store[num].store = "sdl\t%0,%2\n\tsdr\t%0,%1";
4383               load_store[num].last_store = "sdr\t%0,%1";
4384               load_store[num].final = "sdl\t%0,%2";
4385             }
4386
4387           load_store[num].mode = DImode;
4388           offset += 8;
4389           bytes -= 8;
4390           use_lwl_lwr = 1;
4391         }
4392
4393       else if (bytes >= 4 && align >= 4)
4394         {
4395           load_store[num].load = "lw\t%0,%1";
4396           load_store[num].load_nop = "lw\t%0,%1%#";
4397           load_store[num].store = "sw\t%0,%1";
4398           load_store[num].last_store = "sw\t%0,%1";
4399           load_store[num].final = 0;
4400           load_store[num].mode = SImode;
4401           offset += 4;
4402           bytes -= 4;
4403         }
4404
4405       else if (bytes >= 4
4406                && ! TARGET_SR71K
4407                && ! TARGET_MIPS16)
4408         {
4409           if (BYTES_BIG_ENDIAN)
4410             {
4411               load_store[num].load = "lwl\t%0,%1\n\tlwr\t%0,%2";
4412               load_store[num].load_nop = "lwl\t%0,%1\n\tlwr\t%0,%2%#";
4413               load_store[num].store = "swl\t%0,%1\n\tswr\t%0,%2";
4414               load_store[num].last_store = "swr\t%0,%2";
4415               load_store[num].final = "swl\t%0,%1";
4416             }
4417           else
4418             {
4419               load_store[num].load = "lwl\t%0,%2\n\tlwr\t%0,%1";
4420               load_store[num].load_nop = "lwl\t%0,%2\n\tlwr\t%0,%1%#";
4421               load_store[num].store = "swl\t%0,%2\n\tswr\t%0,%1";
4422               load_store[num].last_store = "swr\t%0,%1";
4423               load_store[num].final = "swl\t%0,%2";
4424             }
4425
4426           load_store[num].mode = SImode;
4427           offset += 4;
4428           bytes -= 4;
4429           use_lwl_lwr = 1;
4430         }
4431
4432       else if (bytes >= 2 && align >= 2)
4433         {
4434           load_store[num].load = "lh\t%0,%1";
4435           load_store[num].load_nop = "lh\t%0,%1%#";
4436           load_store[num].store = "sh\t%0,%1";
4437           load_store[num].last_store = "sh\t%0,%1";
4438           load_store[num].final = 0;
4439           load_store[num].mode = HImode;
4440           offset += 2;
4441           bytes -= 2;
4442         }
4443       else
4444         {
4445           load_store[num].load = "lb\t%0,%1";
4446           load_store[num].load_nop = "lb\t%0,%1%#";
4447           load_store[num].store = "sb\t%0,%1";
4448           load_store[num].last_store = "sb\t%0,%1";
4449           load_store[num].final = 0;
4450           load_store[num].mode = QImode;
4451           offset++;
4452           bytes--;
4453         }
4454
4455       if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
4456         {
4457           dslots_load_total++;
4458           dslots_load_filled++;
4459
4460           if (CONSTANT_P (src_reg))
4461             mips_count_memory_refs (src_reg, 1);
4462
4463           if (CONSTANT_P (dest_reg))
4464             mips_count_memory_refs (dest_reg, 1);
4465         }
4466
4467       /* Emit load/stores now if we have run out of registers or are
4468          at the end of the move.  */
4469
4470       if (++num == num_regs || bytes == 0)
4471         {
4472           /* If only load/store, we need a NOP after the load.  */
4473           if (num == 1)
4474             {
4475               load_store[0].load = load_store[0].load_nop;
4476               if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
4477                 dslots_load_filled--;
4478             }
4479
4480           if (move_type != BLOCK_MOVE_LAST)
4481             {
4482               for (i = 0; i < num; i++)
4483                 {
4484                   int offset;
4485
4486                   if (!operands[i + 4])
4487                     abort ();
4488
4489                   if (GET_MODE (operands[i + 4]) != load_store[i].mode)
4490                     operands[i + 4] = gen_rtx_REG (load_store[i].mode,
4491                                                    REGNO (operands[i + 4]));
4492
4493                   offset = load_store[i].offset;
4494                   xoperands[0] = operands[i + 4];
4495                   xoperands[1] = gen_rtx_MEM (load_store[i].mode,
4496                                               plus_constant (src_reg, offset));
4497
4498                   if (use_lwl_lwr)
4499                     {
4500                       int extra_offset
4501                         = GET_MODE_SIZE (load_store[i].mode) - 1;
4502
4503                       xoperands[2] = gen_rtx_MEM (load_store[i].mode,
4504                                                   plus_constant (src_reg,
4505                                                                  extra_offset
4506                                                                  + offset));
4507                     }
4508
4509                   output_asm_insn (load_store[i].load, xoperands);
4510                 }
4511             }
4512
4513           for (i = 0; i < num; i++)
4514             {
4515               int last_p = (i == num-1 && bytes == 0);
4516               int offset = load_store[i].offset;
4517
4518               xoperands[0] = operands[i + 4];
4519               xoperands[1] = gen_rtx_MEM (load_store[i].mode,
4520                                           plus_constant (dest_reg, offset));
4521
4522
4523               if (use_lwl_lwr)
4524                 {
4525                   int extra_offset = GET_MODE_SIZE (load_store[i].mode) - 1;
4526                   xoperands[2] = gen_rtx_MEM (load_store[i].mode,
4527                                               plus_constant (dest_reg,
4528                                                              extra_offset
4529                                                              + offset));
4530                 }
4531
4532               if (move_type == BLOCK_MOVE_NORMAL)
4533                 output_asm_insn (load_store[i].store, xoperands);
4534
4535               else if (move_type == BLOCK_MOVE_NOT_LAST)
4536                 {
4537                   if (!last_p)
4538                     output_asm_insn (load_store[i].store, xoperands);
4539
4540                   else if (load_store[i].final != 0)
4541                     output_asm_insn (load_store[i].final, xoperands);
4542                 }
4543
4544               else if (last_p)
4545                 output_asm_insn (load_store[i].last_store, xoperands);
4546             }
4547
4548           num = 0;              /* reset load_store */
4549           use_lwl_lwr = 0;
4550         }
4551     }
4552
4553   return "";
4554 }
4555 \f
4556 /* Argument support functions.  */
4557
4558 /* Initialize CUMULATIVE_ARGS for a function.  */
4559
4560 void
4561 init_cumulative_args (cum, fntype, libname)
4562      CUMULATIVE_ARGS *cum;              /* argument info to initialize */
4563      tree fntype;                       /* tree ptr for function decl */
4564      rtx libname ATTRIBUTE_UNUSED;      /* SYMBOL_REF of library name or 0 */
4565 {
4566   static CUMULATIVE_ARGS zero_cum;
4567   tree param, next_param;
4568
4569   if (TARGET_DEBUG_E_MODE)
4570     {
4571       fprintf (stderr,
4572                "\ninit_cumulative_args, fntype = 0x%.8lx", (long)fntype);
4573
4574       if (!fntype)
4575         fputc ('\n', stderr);
4576
4577       else
4578         {
4579           tree ret_type = TREE_TYPE (fntype);
4580           fprintf (stderr, ", fntype code = %s, ret code = %s\n",
4581                    tree_code_name[(int)TREE_CODE (fntype)],
4582                    tree_code_name[(int)TREE_CODE (ret_type)]);
4583         }
4584     }
4585
4586   *cum = zero_cum;
4587   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
4588
4589   /* Determine if this function has variable arguments.  This is
4590      indicated by the last argument being 'void_type_mode' if there
4591      are no variable arguments.  The standard MIPS calling sequence
4592      passes all arguments in the general purpose registers in this case.  */
4593
4594   for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
4595        param != 0; param = next_param)
4596     {
4597       next_param = TREE_CHAIN (param);
4598       if (next_param == 0 && TREE_VALUE (param) != void_type_node)
4599         cum->gp_reg_found = 1;
4600     }
4601 }
4602
4603 static void
4604 mips_arg_info (cum, mode, type, named, info)
4605      const CUMULATIVE_ARGS *cum;
4606      enum machine_mode mode;
4607      tree type;
4608      int named;
4609      struct mips_arg_info *info;
4610 {
4611   bool even_reg_p;
4612   unsigned int num_words, max_regs;
4613
4614   info->struct_p = (type != 0
4615                     && (TREE_CODE (type) == RECORD_TYPE
4616                         || TREE_CODE (type) == UNION_TYPE
4617                         || TREE_CODE (type) == QUAL_UNION_TYPE));
4618
4619   /* Decide whether this argument should go in a floating-point register,
4620      assuming one is free.  Later code checks for availablity.  */
4621
4622   info->fpr_p = false;
4623   if (GET_MODE_CLASS (mode) == MODE_FLOAT
4624       && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE)
4625     {
4626       switch (mips_abi)
4627         {
4628         case ABI_32:
4629         case ABI_O64:
4630           info->fpr_p = (!cum->gp_reg_found && cum->arg_number < 2);
4631           break;
4632
4633         case ABI_EABI:
4634           info->fpr_p = true;
4635           break;
4636
4637         case ABI_MEABI:
4638           /* The MIPS eabi says only structures containing doubles get
4639              passed in a fp register, so force a structure containing
4640              a float to be passed in the integer registers.  */
4641           info->fpr_p = (named && !(mode == SFmode && info->struct_p));
4642           break;
4643
4644         default:
4645           info->fpr_p = named;
4646           break;
4647         }
4648     }
4649
4650   /* Now decide whether the argument must go in an even-numbered register.  */
4651
4652   even_reg_p = false;
4653   if (info->fpr_p)
4654     {
4655       /* Under the O64 ABI, the second float argument goes in $f13 if it
4656          is a double, but $f14 if it is a single.  Otherwise, on a
4657          32-bit double-float machine, each FP argument must start in a
4658          new register pair.  */
4659       even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_HWFPVALUE
4660                     || (mips_abi == ABI_O64 && mode == SFmode)
4661                     || FP_INC > 1);
4662     }
4663   else if (!TARGET_64BIT || LONG_DOUBLE_TYPE_SIZE == 128)
4664     {
4665       if (GET_MODE_CLASS (mode) == MODE_INT
4666           || GET_MODE_CLASS (mode) == MODE_FLOAT)
4667         even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_WORD);
4668
4669       else if (type != NULL_TREE && TYPE_ALIGN (type) > BITS_PER_WORD)
4670         even_reg_p = true;
4671     }
4672
4673   /* Set REG_OFFSET to the register count we're interested in.
4674      The EABI allocates the floating-point registers separately,
4675      but the other ABIs allocate them like integer registers.  */
4676   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
4677                       ? cum->num_fprs
4678                       : cum->num_gprs);
4679
4680   if (even_reg_p)
4681     info->reg_offset += info->reg_offset & 1;
4682
4683   /* The alignment applied to registers is also applied to stack arguments.  */
4684   info->stack_offset = cum->stack_words;
4685   if (even_reg_p)
4686     info->stack_offset += info->stack_offset & 1;
4687
4688   if (mode == BLKmode)
4689     info->num_bytes = int_size_in_bytes (type);
4690   else
4691     info->num_bytes = GET_MODE_SIZE (mode);
4692
4693   num_words = (info->num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4694   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
4695
4696   /* Partition the argument between registers and stack.  */
4697   info->reg_words = MIN (num_words, max_regs);
4698   info->stack_words = num_words - info->reg_words;
4699 }
4700
4701
4702 /* Advance the argument to the next argument position.  */
4703
4704 void
4705 function_arg_advance (cum, mode, type, named)
4706      CUMULATIVE_ARGS *cum;      /* current arg information */
4707      enum machine_mode mode;    /* current arg mode */
4708      tree type;                 /* type of the argument or 0 if lib support */
4709      int named;                 /* whether or not the argument was named */
4710 {
4711   struct mips_arg_info info;
4712
4713   mips_arg_info (cum, mode, type, named, &info);
4714
4715   /* The following is a hack in order to pass 1 byte structures
4716      the same way that the MIPS compiler does (namely by passing
4717      the structure in the high byte or half word of the register).
4718      This also makes varargs work.  If we have such a structure,
4719      we save the adjustment RTL, and the call define expands will
4720      emit them.  For the VOIDmode argument (argument after the
4721      last real argument), pass back a parallel vector holding each
4722      of the adjustments.  */
4723
4724   /* ??? This scheme requires everything smaller than the word size to
4725      shifted to the left, but when TARGET_64BIT and ! TARGET_INT64,
4726      that would mean every int needs to be shifted left, which is very
4727      inefficient.  Let's not carry this compatibility to the 64 bit
4728      calling convention for now.  */
4729
4730   if (info.struct_p
4731       && info.reg_words == 1
4732       && info.num_bytes < UNITS_PER_WORD
4733       && !TARGET_64BIT
4734       && mips_abi != ABI_EABI
4735       && mips_abi != ABI_MEABI)
4736     {
4737       rtx amount = GEN_INT (BITS_PER_WORD - info.num_bytes * BITS_PER_UNIT);
4738       rtx reg = gen_rtx_REG (word_mode, GP_ARG_FIRST + info.reg_offset);
4739
4740       if (TARGET_64BIT)
4741         cum->adjust[cum->num_adjusts++] = PATTERN (gen_ashldi3 (reg, reg, amount));
4742       else
4743         cum->adjust[cum->num_adjusts++] = PATTERN (gen_ashlsi3 (reg, reg, amount));
4744     }
4745
4746   if (!info.fpr_p)
4747     cum->gp_reg_found = true;
4748
4749   /* See the comment above the cumulative args structure in mips.h
4750      for an explanation of what this code does.  It assumes the O32
4751      ABI, which passes at most 2 arguments in float registers.  */
4752   if (cum->arg_number < 2 && info.fpr_p)
4753     cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
4754
4755   if (mips_abi != ABI_EABI || !info.fpr_p)
4756     cum->num_gprs = info.reg_offset + info.reg_words;
4757   else if (info.reg_words > 0)
4758     cum->num_fprs += FP_INC;
4759
4760   if (info.stack_words > 0)
4761     cum->stack_words = info.stack_offset + info.stack_words;
4762
4763   cum->arg_number++;
4764 }
4765
4766 /* Return an RTL expression containing the register for the given mode,
4767    or 0 if the argument is to be passed on the stack.  */
4768
4769 struct rtx_def *
4770 function_arg (cum, mode, type, named)
4771      const CUMULATIVE_ARGS *cum; /* current arg information */
4772      enum machine_mode mode;    /* current arg mode */
4773      tree type;                 /* type of the argument or 0 if lib support */
4774      int named;                 /* != 0 for normal args, == 0 for ... args */
4775 {
4776   struct mips_arg_info info;
4777
4778   /* We will be called with a mode of VOIDmode after the last argument
4779      has been seen.  Whatever we return will be passed to the call
4780      insn.  If we need any shifts for small structures, return them in
4781      a PARALLEL; in that case, stuff the mips16 fp_code in as the
4782      mode.  Otherwise, if we need a mips16 fp_code, return a REG
4783      with the code stored as the mode.  */
4784   if (mode == VOIDmode)
4785     {
4786       if (cum->num_adjusts > 0)
4787         return gen_rtx_PARALLEL ((enum machine_mode) cum->fp_code,
4788                                  gen_rtvec_v (cum->num_adjusts,
4789                                               (rtx *) cum->adjust));
4790
4791       else if (TARGET_MIPS16 && cum->fp_code != 0)
4792         return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
4793
4794       else
4795         return 0;
4796     }
4797
4798   mips_arg_info (cum, mode, type, named, &info);
4799
4800   /* Return straight away if the whole argument is passed on the stack.  */
4801   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
4802     return 0;
4803
4804   if (type != 0
4805       && TREE_CODE (type) == RECORD_TYPE
4806       && (mips_abi == ABI_N32 || mips_abi == ABI_64)
4807       && TYPE_SIZE_UNIT (type)
4808       && host_integerp (TYPE_SIZE_UNIT (type), 1)
4809       && named
4810       && mode != DFmode)
4811     {
4812       /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
4813          structure contains a double in its entirety, then that 64 bit
4814          chunk is passed in a floating point register.  */
4815       tree field;
4816
4817       /* First check to see if there is any such field.  */
4818       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4819         if (TREE_CODE (field) == FIELD_DECL
4820             && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4821             && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4822             && host_integerp (bit_position (field), 0)
4823             && int_bit_position (field) % BITS_PER_WORD == 0)
4824           break;
4825
4826       if (field != 0)
4827         {
4828           /* Now handle the special case by returning a PARALLEL
4829              indicating where each 64 bit chunk goes.  INFO.REG_WORDS
4830              chunks are passed in registers.  */
4831           unsigned int i;
4832           HOST_WIDE_INT bitpos;
4833           rtx ret;
4834
4835           /* assign_parms checks the mode of ENTRY_PARM, so we must
4836              use the actual mode here.  */
4837           ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
4838
4839           bitpos = 0;
4840           field = TYPE_FIELDS (type);
4841           for (i = 0; i < info.reg_words; i++)
4842             {
4843               rtx reg;
4844
4845               for (; field; field = TREE_CHAIN (field))
4846                 if (TREE_CODE (field) == FIELD_DECL
4847                     && int_bit_position (field) >= bitpos)
4848                   break;
4849
4850               if (field
4851                   && int_bit_position (field) == bitpos
4852                   && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4853                   && !TARGET_SOFT_FLOAT
4854                   && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4855                 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
4856               else
4857                 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
4858
4859               XVECEXP (ret, 0, i)
4860                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4861                                      GEN_INT (bitpos / BITS_PER_UNIT));
4862
4863               bitpos += BITS_PER_WORD;
4864             }
4865           return ret;
4866         }
4867     }
4868
4869   if (mips_abi == ABI_MEABI && info.fpr_p && !cum->prototype)
4870     {
4871       /* To make K&R varargs work we need to pass floating
4872          point arguments in both integer and FP registers.  */
4873       return gen_rtx_PARALLEL
4874         (mode,
4875          gen_rtvec (2,
4876                     gen_rtx_EXPR_LIST (VOIDmode,
4877                                        gen_rtx_REG (mode,
4878                                                     GP_ARG_FIRST
4879                                                     + info.reg_offset),
4880                                        const0_rtx),
4881                     gen_rtx_EXPR_LIST (VOIDmode,
4882                                        gen_rtx_REG (mode,
4883                                                     FP_ARG_FIRST
4884                                                     + info.reg_offset),
4885                                        const0_rtx)));
4886     }
4887
4888   if (info.fpr_p)
4889     return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
4890   else
4891     return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
4892 }
4893
4894 int
4895 function_arg_partial_nregs (cum, mode, type, named)
4896      const CUMULATIVE_ARGS *cum; /* current arg information */
4897      enum machine_mode mode;    /* current arg mode */
4898      tree type;                 /* type of the argument or 0 if lib support */
4899      int named;                 /* != 0 for normal args, == 0 for ... args */
4900 {
4901   struct mips_arg_info info;
4902
4903   mips_arg_info (cum, mode, type, named, &info);
4904   return info.stack_words > 0 ? info.reg_words : 0;
4905 }
4906 \f
4907 int
4908 mips_setup_incoming_varargs (cum, mode, type, no_rtl)
4909      const CUMULATIVE_ARGS *cum;
4910      enum machine_mode mode;
4911      tree type;
4912      int no_rtl;
4913 {
4914   CUMULATIVE_ARGS local_cum;
4915   int gp_saved, fp_saved;
4916
4917   if (mips_abi == ABI_32 || mips_abi == ABI_O64)
4918     return 0;
4919
4920   /* The caller has advanced CUM up to, but not beyond, the last named
4921      argument.  Advance a local copy of CUM past the last "real" named
4922      argument, to find out how many registers are left over.  */
4923
4924   local_cum = *cum;
4925   FUNCTION_ARG_ADVANCE (local_cum, mode, type, 1);
4926
4927   /* Found out how many registers we need to save.  */
4928   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
4929   fp_saved = (EABI_FLOAT_VARARGS_P
4930               ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
4931               : 0);
4932
4933   if (!no_rtl)
4934     {
4935       if (gp_saved > 0)
4936         {
4937           rtx ptr, mem;
4938
4939           ptr = virtual_incoming_args_rtx;
4940           if (mips_abi == ABI_EABI)
4941             ptr = plus_constant (ptr, -gp_saved * UNITS_PER_WORD);
4942           mem = gen_rtx_MEM (BLKmode, ptr);
4943
4944           /* va_arg is an array access in this case, which causes
4945              it to get MEM_IN_STRUCT_P set.  We must set it here
4946              so that the insn scheduler won't assume that these
4947              stores can't possibly overlap with the va_arg loads.  */
4948           if (mips_abi != ABI_EABI && BYTES_BIG_ENDIAN)
4949             MEM_SET_IN_STRUCT_P (mem, 1);
4950
4951           move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST, mem,
4952                                gp_saved, gp_saved * UNITS_PER_WORD);
4953         }
4954       if (fp_saved > 0)
4955         {
4956           /* We can't use move_block_from_reg, because it will use
4957              the wrong mode. */
4958           enum machine_mode mode;
4959           int off, i;
4960
4961           /* Set OFF to the offset from virtual_incoming_args_rtx of
4962              the first float register.   The FP save area lies below
4963              the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
4964           off = -gp_saved * UNITS_PER_WORD;
4965           off &= ~(UNITS_PER_FPVALUE - 1);
4966           off -= fp_saved * UNITS_PER_FPREG;
4967
4968           mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
4969
4970           for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS; i += FP_INC)
4971             {
4972               rtx ptr = plus_constant (virtual_incoming_args_rtx, off);
4973               emit_move_insn (gen_rtx_MEM (mode, ptr),
4974                               gen_rtx_REG (mode, FP_ARG_FIRST + i));
4975               off += UNITS_PER_HWFPVALUE;
4976             }
4977         }
4978     }
4979   return (gp_saved * UNITS_PER_WORD) + (fp_saved * UNITS_PER_FPREG);
4980 }
4981
4982 /* Create the va_list data type.
4983    We keep 3 pointers, and two offsets.
4984    Two pointers are to the overflow area, which starts at the CFA.
4985      One of these is constant, for addressing into the GPR save area below it.
4986      The other is advanced up the stack through the overflow region.
4987    The third pointer is to the GPR save area.  Since the FPR save area
4988      is just below it, we can address FPR slots off this pointer.
4989    We also keep two one-byte offsets, which are to be subtracted from the
4990      constant pointers to yield addresses in the GPR and FPR save areas.
4991      These are downcounted as float or non-float arguments are used,
4992      and when they get to zero, the argument must be obtained from the
4993      overflow region.
4994    If !EABI_FLOAT_VARARGS_P, then no FPR save area exists, and a single
4995      pointer is enough.  It's started at the GPR save area, and is
4996      advanced, period.
4997    Note that the GPR save area is not constant size, due to optimization
4998      in the prologue.  Hence, we can't use a design with two pointers
4999      and two offsets, although we could have designed this with two pointers
5000      and three offsets.  */
5001
5002
5003 tree
5004 mips_build_va_list ()
5005 {
5006   if (EABI_FLOAT_VARARGS_P)
5007     {
5008       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, record;
5009
5010       record = make_node (RECORD_TYPE);
5011
5012       f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
5013                           ptr_type_node);
5014       f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
5015                           ptr_type_node);
5016       f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
5017                           ptr_type_node);
5018       f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
5019                           unsigned_char_type_node);
5020       f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
5021                           unsigned_char_type_node);
5022
5023
5024       DECL_FIELD_CONTEXT (f_ovfl) = record;
5025       DECL_FIELD_CONTEXT (f_gtop) = record;
5026       DECL_FIELD_CONTEXT (f_ftop) = record;
5027       DECL_FIELD_CONTEXT (f_goff) = record;
5028       DECL_FIELD_CONTEXT (f_foff) = record;
5029
5030       TYPE_FIELDS (record) = f_ovfl;
5031       TREE_CHAIN (f_ovfl) = f_gtop;
5032       TREE_CHAIN (f_gtop) = f_ftop;
5033       TREE_CHAIN (f_ftop) = f_goff;
5034       TREE_CHAIN (f_goff) = f_foff;
5035
5036       layout_type (record);
5037       return record;
5038     }
5039   else
5040     return ptr_type_node;
5041 }
5042
5043 /* Implement va_start.   stdarg_p is always 1.  */
5044
5045 void
5046 mips_va_start (valist, nextarg)
5047      tree valist;
5048      rtx nextarg;
5049 {
5050   const CUMULATIVE_ARGS *cum = &current_function_args_info;
5051
5052   /* ARG_POINTER_REGNUM is initialized to STACK_POINTER_BOUNDARY, but
5053      since the stack is aligned for a pair of argument-passing slots,
5054      and the beginning of a variable argument list may be an odd slot,
5055      we have to decrease its alignment.  */
5056   if (cfun && cfun->emit->regno_pointer_align)
5057     while (((current_function_pretend_args_size * BITS_PER_UNIT)
5058             & (REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) - 1)) != 0)
5059       REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) /= 2;
5060
5061   if (mips_abi == ABI_EABI)
5062     {
5063       int gpr_save_area_size;
5064
5065       gpr_save_area_size
5066         = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5067
5068       if (EABI_FLOAT_VARARGS_P)
5069         {
5070           tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5071           tree ovfl, gtop, ftop, goff, foff;
5072           tree t;
5073           int fpr_offset;
5074           int fpr_save_area_size;
5075
5076           f_ovfl = TYPE_FIELDS (va_list_type_node);
5077           f_gtop = TREE_CHAIN (f_ovfl);
5078           f_ftop = TREE_CHAIN (f_gtop);
5079           f_goff = TREE_CHAIN (f_ftop);
5080           f_foff = TREE_CHAIN (f_goff);
5081
5082           ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
5083           gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
5084           ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
5085           goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
5086           foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
5087
5088           /* Emit code to initialize OVFL, which points to the next varargs
5089              stack argument.  CUM->STACK_WORDS gives the number of stack
5090              words used by named arguments.  */
5091           t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5092           if (cum->stack_words > 0)
5093             t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
5094                        build_int_2 (cum->stack_words * UNITS_PER_WORD, 0));
5095           t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5096           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5097
5098           /* Emit code to initialize GTOP, the top of the GPR save area.  */
5099           t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5100           t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5101           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5102
5103           /* Emit code to initialize FTOP, the top of the FPR save area.
5104              This address is gpr_save_area_bytes below GTOP, rounded
5105              down to the next fp-aligned boundary.  */
5106           t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5107           fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5108           fpr_offset &= ~(UNITS_PER_FPVALUE - 1);
5109           if (fpr_offset)
5110             t = build (PLUS_EXPR, TREE_TYPE (ftop), t,
5111                        build_int_2 (-fpr_offset, -1));
5112           t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5113           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5114
5115           /* Emit code to initialize GOFF, the offset from GTOP of the
5116              next GPR argument.  */
5117           t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
5118                      build_int_2 (gpr_save_area_size, 0));
5119           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5120
5121           /* Likewise emit code to initialize FOFF, the offset from FTOP
5122              of the next FPR argument.  */
5123           fpr_save_area_size
5124             = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5125           t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
5126                      build_int_2 (fpr_save_area_size, 0));
5127           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5128         }
5129       else
5130         {
5131           /* Everything is in the GPR save area, or in the overflow
5132              area which is contiguous with it.  */
5133           nextarg = plus_constant (nextarg, -gpr_save_area_size);
5134           std_expand_builtin_va_start (valist, nextarg);
5135         }
5136     }
5137   else
5138     std_expand_builtin_va_start (valist, nextarg);
5139 }
5140
5141 /* Implement va_arg.  */
5142
5143 rtx
5144 mips_va_arg (valist, type)
5145      tree valist, type;
5146 {
5147   HOST_WIDE_INT size, rsize;
5148   rtx addr_rtx;
5149   tree t;
5150
5151   size = int_size_in_bytes (type);
5152   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5153
5154   if (mips_abi == ABI_EABI)
5155     {
5156       bool indirect;
5157       rtx r;
5158
5159       indirect
5160         = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5161
5162       if (indirect)
5163         {
5164           size = POINTER_SIZE / BITS_PER_UNIT;
5165           rsize = UNITS_PER_WORD;
5166         }
5167
5168       addr_rtx = gen_reg_rtx (Pmode);
5169
5170       if (!EABI_FLOAT_VARARGS_P)
5171         {
5172           /* Case of all args in a merged stack.  No need to check bounds,
5173              just advance valist along the stack.  */
5174
5175           tree gpr = valist;
5176           if (!indirect
5177               && !TARGET_64BIT
5178               && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
5179             {
5180               /* Align the pointer using: ap = (ap + align - 1) & -align,
5181                  where align is 2 * UNITS_PER_WORD.  */
5182               t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
5183                          build_int_2 (2 * UNITS_PER_WORD - 1, 0));
5184               t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
5185                          build_int_2 (-2 * UNITS_PER_WORD, -1));
5186               t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
5187               expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5188             }
5189
5190           /* Emit code to set addr_rtx to the valist, and postincrement
5191              the valist by the size of the argument, rounded up to the
5192              next word.  */
5193           t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
5194                      size_int (rsize));
5195           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5196           if (r != addr_rtx)
5197             emit_move_insn (addr_rtx, r);
5198
5199           /* Flush the POSTINCREMENT.  */
5200           emit_queue();
5201         }
5202       else
5203         {
5204           /* Not a simple merged stack.  */
5205
5206           tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5207           tree ovfl, top, off;
5208           rtx lab_over = NULL_RTX, lab_false;
5209           HOST_WIDE_INT osize;
5210
5211           f_ovfl = TYPE_FIELDS (va_list_type_node);
5212           f_gtop = TREE_CHAIN (f_ovfl);
5213           f_ftop = TREE_CHAIN (f_gtop);
5214           f_goff = TREE_CHAIN (f_ftop);
5215           f_foff = TREE_CHAIN (f_goff);
5216
5217           /* We maintain separate pointers and offsets for floating-point
5218              and integer arguments, but we need similar code in both cases.
5219              Let:
5220
5221                  TOP be the top of the register save area;
5222                  OFF be the offset from TOP of the next register;
5223                  ADDR_RTX be the address of the argument; and
5224                  RSIZE be the number of bytes used to store the argument
5225                    when it's in the register save area
5226                  OSIZE be the number of bytes used to store it when it's
5227                    in the stack overflow area
5228                  PADDING be (BYTES_BIG_ENDIAN ? OSIZE - RSIZE : 0)
5229
5230              The code we want is:
5231
5232                   1: off &= -rsize;       // round down
5233                   2: if (off != 0)
5234                   3:   {
5235                   4:     addr_rtx = top - off;
5236                   5:     off -= rsize;
5237                   6:   }
5238                   7: else
5239                   8:   {
5240                   9:     ovfl += ((intptr_t) ovfl + osize - 1) & -osize;
5241                  10:     addr_rtx = ovfl + PADDING;
5242                  11:     ovfl += osize;
5243                  14:   }
5244
5245              [1] and [9] can sometimes be optimized away.  */
5246
5247           lab_false = gen_label_rtx ();
5248           lab_over = gen_label_rtx ();
5249
5250           ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
5251
5252           if (TREE_CODE (type) == REAL_TYPE)
5253             {
5254               top = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
5255               off = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
5256
5257               /* When floating-point registers are saved to the stack,
5258                  each one will take up UNITS_PER_HWFPVALUE bytes, regardless
5259                  of the float's precision.  */
5260               rsize = UNITS_PER_HWFPVALUE;
5261             }
5262           else
5263             {
5264               top = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
5265               off = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
5266               if (rsize > UNITS_PER_WORD)
5267                 {
5268                   /* [1] Emit code for: off &= -rsize.  */
5269                   t = build (BIT_AND_EXPR, TREE_TYPE (off), off,
5270                              build_int_2 (-rsize, -1));
5271                   t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
5272                   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5273                 }
5274             }
5275           /* Every overflow argument must take up at least UNITS_PER_WORD
5276              bytes (= PARM_BOUNDARY bits).  RSIZE can sometimes be smaller
5277              than that, such as in the combination -mgp64 -msingle-float
5278              -fshort-double.  Doubles passed in registers will then take
5279              up UNITS_PER_HWFPVALUE bytes, but those passed on the stack
5280              take up UNITS_PER_WORD bytes.  */
5281           osize = MAX (rsize, UNITS_PER_WORD);
5282
5283           /* [2] Emit code to branch if off == 0.  */
5284           r = expand_expr (off, NULL_RTX, TYPE_MODE (TREE_TYPE (off)),
5285                            EXPAND_NORMAL);
5286           emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
5287                                    1, lab_false);
5288
5289           /* [4] Emit code for: addr_rtx = top - off.  */
5290           t = build (MINUS_EXPR, TREE_TYPE (top), top, off);
5291           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5292           if (r != addr_rtx)
5293             emit_move_insn (addr_rtx, r);
5294
5295           /* [5] Emit code for: off -= rsize.  */
5296           t = build (MINUS_EXPR, TREE_TYPE (off), off, build_int_2 (rsize, 0));
5297           t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
5298           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5299
5300           /* [7] Emit code to jump over the else clause, then the label
5301              that starts it.  */
5302           emit_queue();
5303           emit_jump (lab_over);
5304           emit_barrier ();
5305           emit_label (lab_false);
5306
5307           if (osize > UNITS_PER_WORD)
5308             {
5309               /* [9] Emit: ovfl += ((intptr_t) ovfl + osize - 1) & -osize.  */
5310               t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl,
5311                          build_int_2 (osize - 1, 0));
5312               t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), t,
5313                          build_int_2 (-osize, -1));
5314               t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5315               expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5316             }
5317
5318           /* [10, 11].  Emit code to store ovfl in addr_rtx, then
5319              post-increment ovfl by osize.  On big-endian machines,
5320              the argument has OSIZE - RSIZE bytes of leading padding.  */
5321           t = build (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl,
5322                      size_int (osize));
5323           if (BYTES_BIG_ENDIAN && osize > rsize)
5324             t = build (PLUS_EXPR, TREE_TYPE (t), t,
5325                        build_int_2 (osize - rsize, 0));
5326           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5327           if (r != addr_rtx)
5328             emit_move_insn (addr_rtx, r);
5329
5330           emit_queue();
5331           emit_label (lab_over);
5332         }
5333       if (BYTES_BIG_ENDIAN && rsize != size)
5334         addr_rtx = plus_constant (addr_rtx, rsize - size);
5335       if (indirect)
5336         {
5337           addr_rtx = force_reg (Pmode, addr_rtx);
5338           r = gen_rtx_MEM (Pmode, addr_rtx);
5339           set_mem_alias_set (r, get_varargs_alias_set ());
5340           emit_move_insn (addr_rtx, r);
5341         }
5342       return addr_rtx;
5343     }
5344   else
5345     {
5346       /* Not EABI.  */
5347       int align;
5348
5349       /* ??? The original va-mips.h did always align, despite the fact
5350          that alignments <= UNITS_PER_WORD are preserved by the va_arg
5351          increment mechanism.  */
5352
5353       if ((mips_abi == ABI_N32 || mips_abi == ABI_64)
5354           && TYPE_ALIGN (type) > 64)
5355         align = 16;
5356       else if (TARGET_64BIT)
5357         align = 8;
5358       else if (TYPE_ALIGN (type) > 32)
5359         align = 8;
5360       else
5361         align = 4;
5362
5363       t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
5364                  build_int_2 (align - 1, 0));
5365       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
5366       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5367       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5368
5369       /* Everything past the alignment is standard.  */
5370       return std_expand_builtin_va_arg (valist, type);
5371     }
5372 }
5373 \f
5374 /* Abort after printing out a specific insn.  */
5375
5376 static void
5377 abort_with_insn (insn, reason)
5378      rtx insn;
5379      const char *reason;
5380 {
5381   error (reason);
5382   debug_rtx (insn);
5383   abort ();
5384 }
5385 \f
5386 /* Set up globals to generate code for the ISA or processor
5387    described by INFO.  */
5388
5389 static void
5390 mips_set_architecture (info)
5391      const struct mips_cpu_info *info;
5392 {
5393   if (info != 0)
5394     {
5395       mips_arch_info = info;
5396       mips_arch = info->cpu;
5397       mips_isa = info->isa;
5398     }
5399 }
5400
5401
5402 /* Likewise for tuning.  */
5403
5404 static void
5405 mips_set_tune (info)
5406      const struct mips_cpu_info *info;
5407 {
5408   if (info != 0)
5409     {
5410       mips_tune_info = info;
5411       mips_tune = info->cpu;
5412     }
5413 }
5414
5415
5416 /* Set up the threshold for data to go into the small data area, instead
5417    of the normal data area, and detect any conflicts in the switches.  */
5418
5419 void
5420 override_options ()
5421 {
5422   int i, start, regno;
5423   enum machine_mode mode;
5424
5425   mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
5426
5427   if (mips_section_threshold <= 0)
5428     target_flags &= ~MASK_GPOPT;
5429   else if (optimize)
5430     target_flags |= MASK_GPOPT;
5431
5432   /* If both single-float and soft-float are set, then clear the one that
5433      was set by TARGET_DEFAULT, leaving the one that was set by the
5434      user.  We assume here that the specs prevent both being set by the
5435      user.  */
5436 #ifdef TARGET_DEFAULT
5437   if (TARGET_SINGLE_FLOAT && TARGET_SOFT_FLOAT)
5438     target_flags &= ~((TARGET_DEFAULT) & (MASK_SOFT_FLOAT | MASK_SINGLE_FLOAT));
5439 #endif
5440
5441   /* Interpret -mabi.  */
5442   mips_abi = MIPS_ABI_DEFAULT;
5443   if (mips_abi_string != 0)
5444     {
5445       if (strcmp (mips_abi_string, "32") == 0)
5446         mips_abi = ABI_32;
5447       else if (strcmp (mips_abi_string, "o64") == 0)
5448         mips_abi = ABI_O64;
5449       else if (strcmp (mips_abi_string, "n32") == 0)
5450         mips_abi = ABI_N32;
5451       else if (strcmp (mips_abi_string, "64") == 0)
5452         mips_abi = ABI_64;
5453       else if (strcmp (mips_abi_string, "eabi") == 0)
5454         mips_abi = ABI_EABI;
5455       else if (strcmp (mips_abi_string, "meabi") == 0)
5456         mips_abi = ABI_MEABI;
5457       else
5458         fatal_error ("bad value (%s) for -mabi= switch", mips_abi_string);
5459     }
5460
5461   /* The following code determines the architecture and register size.
5462      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
5463      The GAS and GCC code should be kept in sync as much as possible.  */
5464
5465   if (mips_arch_string != 0)
5466     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
5467
5468   if (mips_tune_string != 0)
5469     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
5470
5471   if (mips_isa_string != 0)
5472     {
5473       /* Handle -mipsN.  */
5474
5475       if (strcmp (mips_isa_string, "16") == 0)
5476         {
5477           /* -mips16 specifies an ASE rather than a processor, so don't
5478              change mips_arch here.  -mno-mips16 overrides -mips16.  */
5479           if (mips_no_mips16_string == NULL)
5480             target_flags |= MASK_MIPS16;
5481         }
5482       else
5483         {
5484           char *whole_isa_str = concat ("mips", mips_isa_string, NULL);
5485           const struct mips_cpu_info *isa_info;
5486
5487           isa_info = mips_parse_cpu ("-mips option", whole_isa_str);
5488           free (whole_isa_str);
5489
5490           /* -march takes precedence over -mipsN, since it is more descriptive.
5491              There's no harm in specifying both as long as the ISA levels
5492              are the same.  */
5493           if (mips_arch_info != 0 && mips_isa != isa_info->isa)
5494             error ("-mips%s conflicts with the other architecture options, which specify a MIPS%d processor",
5495                    mips_isa_string, mips_isa);
5496
5497           /* Set architecture based on the given option.  */
5498           mips_set_architecture (isa_info);
5499         }
5500     }
5501
5502   if (mips_arch_info == 0)
5503     {
5504 #ifdef MIPS_CPU_STRING_DEFAULT
5505       mips_set_architecture (mips_parse_cpu ("default CPU",
5506                                              MIPS_CPU_STRING_DEFAULT));
5507 #else
5508       mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
5509 #endif
5510     }
5511
5512   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
5513     error ("-march=%s is not compatible with the selected ABI",
5514            mips_arch_info->name);
5515
5516   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
5517   if (mips_tune_info == 0)
5518     mips_set_tune (mips_arch_info);
5519
5520   if ((target_flags_explicit & MASK_64BIT) != 0)
5521     {
5522       /* The user specified the size of the integer registers.  Make sure
5523          it agrees with the ABI and ISA.  */
5524       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
5525         error ("-mgp64 used with a 32-bit processor");
5526       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
5527         error ("-mgp32 used with a 64-bit ABI");
5528       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
5529         error ("-mgp64 used with a 32-bit ABI");
5530     }
5531   else
5532     {
5533       /* Infer the integer register size from the ABI and processor.
5534          Restrict ourselves to 32-bit registers if that's all the
5535          processor has, or if the ABI cannot handle 64-bit registers.  */
5536       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
5537         target_flags &= ~MASK_64BIT;
5538       else
5539         target_flags |= MASK_64BIT;
5540     }
5541
5542   if ((target_flags_explicit & MASK_FLOAT64) != 0)
5543     {
5544       /* Really, -mfp32 and -mfp64 are ornamental options.  There's
5545          only one right answer here.  */
5546       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
5547         error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
5548       else if (!TARGET_64BIT && TARGET_FLOAT64)
5549         error ("unsupported combination: %s", "-mgp32 -mfp64");
5550       else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
5551         error ("unsupported combination: %s", "-mfp64 -msingle-float");
5552     }
5553   else
5554     {
5555       /* -msingle-float selects 32-bit float registers.  Otherwise the
5556          float registers should be the same size as the integer ones.  */
5557       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
5558         target_flags |= MASK_FLOAT64;
5559       else
5560         target_flags &= ~MASK_FLOAT64;
5561     }
5562
5563   /* End of code shared with GAS.  */
5564
5565   if ((target_flags_explicit & MASK_LONG64) == 0)
5566     {
5567       /* If no type size setting options (-mlong64,-mint64,-mlong32)
5568          were used, then set the type sizes.  In the EABI in 64 bit mode,
5569          longs and pointers are 64 bits.  Likewise for the SGI Irix6 N64
5570          ABI.  */
5571       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
5572         target_flags |= MASK_LONG64;
5573       else
5574         target_flags &= ~MASK_LONG64;
5575     }
5576
5577   if (MIPS_MARCH_CONTROLS_SOFT_FLOAT
5578       && (target_flags_explicit & MASK_SOFT_FLOAT) == 0)
5579     {
5580       /* For some configurations, it is useful to have -march control
5581          the default setting of MASK_SOFT_FLOAT.  */
5582       switch ((int) mips_arch)
5583         {
5584         case PROCESSOR_R4100:
5585         case PROCESSOR_R4120:
5586           target_flags |= MASK_SOFT_FLOAT;
5587           break;
5588
5589         default:
5590           target_flags &= ~MASK_SOFT_FLOAT;
5591           break;
5592         }
5593     }
5594
5595   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
5596     flag_pcc_struct_return = 0;
5597
5598   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
5599     {
5600       /* If neither -mbranch-likely nor -mno-branch-likely was given
5601          on the command line, set MASK_BRANCHLIKELY based on the target
5602          architecture.
5603
5604          By default, we enable use of Branch Likely instructions on
5605          all architectures which support them except for MIPS32 and MIPS64
5606          (i.e., the generic MIPS32 and MIPS64 ISAs, and processors which
5607          implement them).
5608
5609          The MIPS32 and MIPS64 architecture specifications say "Software
5610          is strongly encouraged to avoid use of Branch Likely
5611          instructions, as they will be removed from a future revision
5612          of the [MIPS32 and MIPS64] architecture."  Therefore, we do not
5613          issue those instructions unless instructed to do so by
5614          -mbranch-likely.  */
5615       if (ISA_HAS_BRANCHLIKELY && !(ISA_MIPS32 || ISA_MIPS32R2 || ISA_MIPS64))
5616         target_flags |= MASK_BRANCHLIKELY;
5617       else
5618         target_flags &= ~MASK_BRANCHLIKELY;
5619     }
5620   if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
5621     warning ("generation of Branch Likely instructions enabled, but not supported by architecture");
5622
5623   /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined.  We need
5624      to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work.  */
5625   /* ??? -non_shared turns off pic code generation, but this is not
5626      implemented.  */
5627   if (TARGET_ABICALLS)
5628     {
5629       mips_abicalls = MIPS_ABICALLS_YES;
5630       flag_pic = 1;
5631       if (mips_section_threshold > 0)
5632         warning ("-G is incompatible with PIC code which is the default");
5633     }
5634   else
5635     mips_abicalls = MIPS_ABICALLS_NO;
5636
5637   /* -membedded-pic is a form of PIC code suitable for embedded
5638      systems.  All calls are made using PC relative addressing, and
5639      all data is addressed using the $gp register.  This requires gas,
5640      which does most of the work, and GNU ld, which automatically
5641      expands PC relative calls which are out of range into a longer
5642      instruction sequence.  All gcc really does differently is
5643      generate a different sequence for a switch.  */
5644   if (TARGET_EMBEDDED_PIC)
5645     {
5646       flag_pic = 1;
5647       if (TARGET_ABICALLS)
5648         warning ("-membedded-pic and -mabicalls are incompatible");
5649
5650       if (g_switch_set)
5651         warning ("-G and -membedded-pic are incompatible");
5652
5653       /* Setting mips_section_threshold is not required, because gas
5654          will force everything to be GP addressable anyhow, but
5655          setting it will cause gcc to make better estimates of the
5656          number of instructions required to access a particular data
5657          item.  */
5658       mips_section_threshold = 0x7fffffff;
5659     }
5660
5661   /* This optimization requires a linker that can support a R_MIPS_LO16
5662      relocation which is not immediately preceded by a R_MIPS_HI16 relocation.
5663      GNU ld has this support, but not all other MIPS linkers do, so we enable
5664      this optimization only if the user requests it, or if GNU ld is the
5665      standard linker for this configuration.  */
5666   /* ??? This does not work when target addresses are DImode.
5667      This is because we are missing DImode high/lo_sum patterns.  */
5668   if (TARGET_GAS && ! TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES && optimize && ! flag_pic
5669       && Pmode == SImode)
5670     mips_split_addresses = 1;
5671   else
5672     mips_split_addresses = 0;
5673
5674   /* -mrnames says to use the MIPS software convention for register
5675      names instead of the hardware names (ie, $a0 instead of $4).
5676      We do this by switching the names in mips_reg_names, which the
5677      reg_names points into via the REGISTER_NAMES macro.  */
5678
5679   if (TARGET_NAME_REGS)
5680     memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
5681
5682   /* When compiling for the mips16, we can not use floating point.  We
5683      record the original hard float value in mips16_hard_float.  */
5684   if (TARGET_MIPS16)
5685     {
5686       if (TARGET_SOFT_FLOAT)
5687         mips16_hard_float = 0;
5688       else
5689         mips16_hard_float = 1;
5690       target_flags |= MASK_SOFT_FLOAT;
5691
5692       /* Don't run the scheduler before reload, since it tends to
5693          increase register pressure.  */
5694       flag_schedule_insns = 0;
5695     }
5696
5697   /* We put -mentry in TARGET_OPTIONS rather than TARGET_SWITCHES only
5698      to avoid using up another bit in target_flags.  */
5699   if (mips_entry_string != NULL)
5700     {
5701       if (*mips_entry_string != '\0')
5702         error ("invalid option `entry%s'", mips_entry_string);
5703
5704       if (! TARGET_MIPS16)
5705         warning ("-mentry is only meaningful with -mips-16");
5706       else
5707         mips_entry = 1;
5708     }
5709
5710   /* We copy TARGET_MIPS16 into the mips16 global variable, so that
5711      attributes can access it.  */
5712   if (TARGET_MIPS16)
5713     mips16 = 1;
5714   else
5715     mips16 = 0;
5716
5717   real_format_for_mode[SFmode - QFmode] = &mips_single_format;
5718   real_format_for_mode[DFmode - QFmode] = &mips_double_format;
5719 #ifdef MIPS_TFMODE_FORMAT
5720   real_format_for_mode[TFmode - QFmode] = &MIPS_TFMODE_FORMAT;
5721 #else
5722   real_format_for_mode[TFmode - QFmode] = &mips_quad_format;
5723 #endif
5724   
5725   mips_print_operand_punct['?'] = 1;
5726   mips_print_operand_punct['#'] = 1;
5727   mips_print_operand_punct['&'] = 1;
5728   mips_print_operand_punct['!'] = 1;
5729   mips_print_operand_punct['*'] = 1;
5730   mips_print_operand_punct['@'] = 1;
5731   mips_print_operand_punct['.'] = 1;
5732   mips_print_operand_punct['('] = 1;
5733   mips_print_operand_punct[')'] = 1;
5734   mips_print_operand_punct['['] = 1;
5735   mips_print_operand_punct[']'] = 1;
5736   mips_print_operand_punct['<'] = 1;
5737   mips_print_operand_punct['>'] = 1;
5738   mips_print_operand_punct['{'] = 1;
5739   mips_print_operand_punct['}'] = 1;
5740   mips_print_operand_punct['^'] = 1;
5741   mips_print_operand_punct['$'] = 1;
5742   mips_print_operand_punct['+'] = 1;
5743   mips_print_operand_punct['~'] = 1;
5744
5745   mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
5746   mips_char_to_class['e'] = M16_NA_REGS;
5747   mips_char_to_class['t'] = T_REG;
5748   mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
5749   mips_char_to_class['h'] = HI_REG;
5750   mips_char_to_class['l'] = LO_REG;
5751   mips_char_to_class['a'] = HILO_REG;
5752   mips_char_to_class['x'] = MD_REGS;
5753   mips_char_to_class['b'] = ALL_REGS;
5754   mips_char_to_class['y'] = GR_REGS;
5755   mips_char_to_class['z'] = ST_REGS;
5756   mips_char_to_class['B'] = COP0_REGS;
5757   mips_char_to_class['C'] = COP2_REGS;
5758   mips_char_to_class['D'] = COP3_REGS;
5759
5760   /* Set up array to map GCC register number to debug register number.
5761      Ignore the special purpose register numbers.  */
5762
5763   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5764     mips_dbx_regno[i] = -1;
5765
5766   start = GP_DBX_FIRST - GP_REG_FIRST;
5767   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
5768     mips_dbx_regno[i] = i + start;
5769
5770   start = FP_DBX_FIRST - FP_REG_FIRST;
5771   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
5772     mips_dbx_regno[i] = i + start;
5773
5774   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
5775   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
5776
5777   /* Set up array giving whether a given register can hold a given mode.
5778      At present, restrict ints from being in FP registers, because reload
5779      is a little enthusiastic about storing extra values in FP registers,
5780      and this is not good for things like OS kernels.  Also, due to the
5781      mandatory delay, it is as fast to load from cached memory as to move
5782      from the FP register.  */
5783
5784   for (mode = VOIDmode;
5785        mode != MAX_MACHINE_MODE;
5786        mode = (enum machine_mode) ((int)mode + 1))
5787     {
5788       register int size              = GET_MODE_SIZE (mode);
5789       register enum mode_class class = GET_MODE_CLASS (mode);
5790
5791       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5792         {
5793           register int temp;
5794
5795           if (mode == CCmode)
5796             {
5797               if (! ISA_HAS_8CC)
5798                 temp = (regno == FPSW_REGNUM);
5799               else
5800                 temp = (ST_REG_P (regno) || GP_REG_P (regno)
5801                         || FP_REG_P (regno));
5802             }
5803
5804           else if (GP_REG_P (regno))
5805             temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
5806
5807           else if (FP_REG_P (regno))
5808             temp = (((regno % FP_INC) == 0
5809                      /* I think this change is OK regardless of abi, but
5810                         I'm being cautions untill I can test this more.
5811                         HARD_REGNO_MODE_OK is about whether or not you
5812                         can move to and from a register without changing
5813                         the value, not about whether math works on the
5814                         register. */
5815                      || (mips_abi == ABI_MEABI && size <= 4))
5816                     && (((class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
5817                          && size <= UNITS_PER_FPVALUE)
5818                         /* Allow integer modes that fit into a single
5819                            register.  We need to put integers into FPRs
5820                            when using instructions like cvt and trunc.  */
5821                         || (class == MODE_INT && size <= UNITS_PER_FPREG)
5822                         /* Allow TFmode for CCmode reloads.  */
5823                         || (ISA_HAS_8CC && mode == TFmode)));
5824
5825           else if (MD_REG_P (regno))
5826             temp = (class == MODE_INT
5827                     && (size <= UNITS_PER_WORD
5828                         || (regno == MD_REG_FIRST
5829                             && size == 2 * UNITS_PER_WORD)));
5830
5831           else if (ALL_COP_REG_P (regno))
5832             temp = (class == MODE_INT && size <= UNITS_PER_WORD);
5833           else
5834             temp = 0;
5835
5836           mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5837         }
5838     }
5839
5840   /* Save GPR registers in word_mode sized hunks.  word_mode hasn't been
5841      initialized yet, so we can't use that here.  */
5842   gpr_mode = TARGET_64BIT ? DImode : SImode;
5843
5844   /* Provide default values for align_* for 64-bit targets.  */
5845   if (TARGET_64BIT && !TARGET_MIPS16)
5846     {
5847       if (align_loops == 0)
5848         align_loops = 8;
5849       if (align_jumps == 0)
5850         align_jumps = 8;
5851       if (align_functions == 0)
5852         align_functions = 8;
5853     }
5854
5855   /* Function to allocate machine-dependent function status.  */
5856   init_machine_status = &mips_init_machine_status;
5857 }
5858
5859 /* Implement CONDITIONAL_REGISTER_USAGE.  */
5860
5861 void
5862 mips_conditional_register_usage ()
5863 {
5864   if (!TARGET_HARD_FLOAT)
5865     {
5866       int regno;
5867
5868       for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5869         fixed_regs[regno] = call_used_regs[regno] = 1;
5870       for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5871         fixed_regs[regno] = call_used_regs[regno] = 1;
5872     }
5873   else if (! ISA_HAS_8CC)
5874     {
5875       int regno;
5876
5877       /* We only have a single condition code register.  We
5878          implement this by hiding all the condition code registers,
5879          and generating RTL that refers directly to ST_REG_FIRST.  */
5880       for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5881         fixed_regs[regno] = call_used_regs[regno] = 1;
5882     }
5883   /* In mips16 mode, we permit the $t temporary registers to be used
5884      for reload.  We prohibit the unused $s registers, since they
5885      are caller saved, and saving them via a mips16 register would
5886      probably waste more time than just reloading the value.  */
5887   if (TARGET_MIPS16)
5888     {
5889       fixed_regs[18] = call_used_regs[18] = 1;
5890       fixed_regs[19] = call_used_regs[19] = 1;
5891       fixed_regs[20] = call_used_regs[20] = 1;
5892       fixed_regs[21] = call_used_regs[21] = 1;
5893       fixed_regs[22] = call_used_regs[22] = 1;
5894       fixed_regs[23] = call_used_regs[23] = 1;
5895       fixed_regs[26] = call_used_regs[26] = 1;
5896       fixed_regs[27] = call_used_regs[27] = 1;
5897       fixed_regs[30] = call_used_regs[30] = 1;
5898     }
5899   /* fp20-23 are now caller saved.  */
5900   if (mips_abi == ABI_64)
5901     {
5902       int regno;
5903       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
5904         call_really_used_regs[regno] = call_used_regs[regno] = 1;
5905     }
5906   /* odd registers from fp21 to fp31 are now caller saved.  */
5907   if (mips_abi == ABI_N32 || mips_abi == ABI_MEABI)
5908     {
5909       int regno;
5910       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
5911         call_really_used_regs[regno] = call_used_regs[regno] = 1;
5912     }
5913 }
5914
5915 /* Allocate a chunk of memory for per-function machine-dependent data.  */
5916 static struct machine_function *
5917 mips_init_machine_status ()
5918 {
5919   return ((struct machine_function *)
5920           ggc_alloc_cleared (sizeof (struct machine_function)));
5921 }
5922
5923 /* On the mips16, we want to allocate $24 (T_REG) before other
5924    registers for instructions for which it is possible.  This helps
5925    avoid shuffling registers around in order to set up for an xor,
5926    encouraging the compiler to use a cmp instead.  */
5927
5928 void
5929 mips_order_regs_for_local_alloc ()
5930 {
5931   register int i;
5932
5933   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5934     reg_alloc_order[i] = i;
5935
5936   if (TARGET_MIPS16)
5937     {
5938       /* It really doesn't matter where we put register 0, since it is
5939          a fixed register anyhow.  */
5940       reg_alloc_order[0] = 24;
5941       reg_alloc_order[24] = 0;
5942     }
5943 }
5944
5945 \f
5946 /* The MIPS debug format wants all automatic variables and arguments
5947    to be in terms of the virtual frame pointer (stack pointer before
5948    any adjustment in the function), while the MIPS 3.0 linker wants
5949    the frame pointer to be the stack pointer after the initial
5950    adjustment.  So, we do the adjustment here.  The arg pointer (which
5951    is eliminated) points to the virtual frame pointer, while the frame
5952    pointer (which may be eliminated) points to the stack pointer after
5953    the initial adjustments.  */
5954
5955 HOST_WIDE_INT
5956 mips_debugger_offset (addr, offset)
5957      rtx addr;
5958      HOST_WIDE_INT offset;
5959 {
5960   rtx offset2 = const0_rtx;
5961   rtx reg = eliminate_constant_term (addr, &offset2);
5962
5963   if (offset == 0)
5964     offset = INTVAL (offset2);
5965
5966   if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5967       || reg == hard_frame_pointer_rtx)
5968     {
5969       HOST_WIDE_INT frame_size = (!cfun->machine->frame.initialized)
5970                                   ? compute_frame_size (get_frame_size ())
5971                                   : cfun->machine->frame.total_size;
5972
5973       /* MIPS16 frame is smaller */
5974       if (frame_pointer_needed && TARGET_MIPS16)
5975         frame_size -= current_function_outgoing_args_size;
5976
5977       offset = offset - frame_size;
5978     }
5979
5980   /* sdbout_parms does not want this to crash for unrecognized cases.  */
5981 #if 0
5982   else if (reg != arg_pointer_rtx)
5983     abort_with_insn (addr, "mips_debugger_offset called with non stack/frame/arg pointer");
5984 #endif
5985
5986   return offset;
5987 }
5988 \f
5989 /* A C compound statement to output to stdio stream STREAM the
5990    assembler syntax for an instruction operand X.  X is an RTL
5991    expression.
5992
5993    CODE is a value that can be used to specify one of several ways
5994    of printing the operand.  It is used when identical operands
5995    must be printed differently depending on the context.  CODE
5996    comes from the `%' specification that was used to request
5997    printing of the operand.  If the specification was just `%DIGIT'
5998    then CODE is 0; if the specification was `%LTR DIGIT' then CODE
5999    is the ASCII code for LTR.
6000
6001    If X is a register, this macro should print the register's name.
6002    The names can be found in an array `reg_names' whose type is
6003    `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.
6004
6005    When the machine description has a specification `%PUNCT' (a `%'
6006    followed by a punctuation character), this macro is called with
6007    a null pointer for X and the punctuation character for CODE.
6008
6009    The MIPS specific codes are:
6010
6011    'X'  X is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
6012    'x'  X is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
6013    'd'  output integer constant in decimal,
6014    'z'  if the operand is 0, use $0 instead of normal operand.
6015    'D'  print second part of double-word register or memory operand.
6016    'L'  print low-order register of double-word register operand.
6017    'M'  print high-order register of double-word register operand.
6018    'C'  print part of opcode for a branch condition.
6019    'F'  print part of opcode for a floating-point branch condition.
6020    'N'  print part of opcode for a branch condition, inverted.
6021    'W'  print part of opcode for a floating-point branch condition, inverted.
6022    'S'  X is CODE_LABEL, print with prefix of "LS" (for embedded switch).
6023    'B'  print 'z' for EQ, 'n' for NE
6024    'b'  print 'n' for EQ, 'z' for NE
6025    'T'  print 'f' for EQ, 't' for NE
6026    't'  print 't' for EQ, 'f' for NE
6027    'Z'  print register and a comma, but print nothing for $fcc0
6028    '('  Turn on .set noreorder
6029    ')'  Turn on .set reorder
6030    '['  Turn on .set noat
6031    ']'  Turn on .set at
6032    '<'  Turn on .set nomacro
6033    '>'  Turn on .set macro
6034    '{'  Turn on .set volatile (not GAS)
6035    '}'  Turn on .set novolatile (not GAS)
6036    '&'  Turn on .set noreorder if filling delay slots
6037    '*'  Turn on both .set noreorder and .set nomacro if filling delay slots
6038    '!'  Turn on .set nomacro if filling delay slots
6039    '#'  Print nop if in a .set noreorder section.
6040    '?'  Print 'l' if we are to use a branch likely instead of normal branch.
6041    '@'  Print the name of the assembler temporary register (at or $1).
6042    '.'  Print the name of the register with a hard-wired zero (zero or $0).
6043    '^'  Print the name of the pic call-through register (t9 or $25).
6044    '$'  Print the name of the stack pointer register (sp or $29).
6045    '+'  Print the name of the gp register (gp or $28).
6046    '~'  Output a branch alignment to LABEL_ALIGN(NULL).  */
6047
6048 void
6049 print_operand (file, op, letter)
6050      FILE *file;                /* file to write to */
6051      rtx op;                    /* operand to print */
6052      int letter;                /* %<letter> or 0 */
6053 {
6054   register enum rtx_code code;
6055
6056   if (PRINT_OPERAND_PUNCT_VALID_P (letter))
6057     {
6058       switch (letter)
6059         {
6060         case '?':
6061           if (mips_branch_likely)
6062             putc ('l', file);
6063           break;
6064
6065         case '@':
6066           fputs (reg_names [GP_REG_FIRST + 1], file);
6067           break;
6068
6069         case '^':
6070           fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
6071           break;
6072
6073         case '.':
6074           fputs (reg_names [GP_REG_FIRST + 0], file);
6075           break;
6076
6077         case '$':
6078           fputs (reg_names[STACK_POINTER_REGNUM], file);
6079           break;
6080
6081         case '+':
6082           fputs (reg_names[GP_REG_FIRST + 28], file);
6083           break;
6084
6085         case '&':
6086           if (final_sequence != 0 && set_noreorder++ == 0)
6087             fputs (".set\tnoreorder\n\t", file);
6088           break;
6089
6090         case '*':
6091           if (final_sequence != 0)
6092             {
6093               if (set_noreorder++ == 0)
6094                 fputs (".set\tnoreorder\n\t", file);
6095
6096               if (set_nomacro++ == 0)
6097                 fputs (".set\tnomacro\n\t", file);
6098             }
6099           break;
6100
6101         case '!':
6102           if (final_sequence != 0 && set_nomacro++ == 0)
6103             fputs ("\n\t.set\tnomacro", file);
6104           break;
6105
6106         case '#':
6107           if (set_noreorder != 0)
6108             fputs ("\n\tnop", file);
6109           else if (TARGET_STATS)
6110             fputs ("\n\t#nop", file);
6111
6112           break;
6113
6114         case '(':
6115           if (set_noreorder++ == 0)
6116             fputs (".set\tnoreorder\n\t", file);
6117           break;
6118
6119         case ')':
6120           if (set_noreorder == 0)
6121             error ("internal error: %%) found without a %%( in assembler pattern");
6122
6123           else if (--set_noreorder == 0)
6124             fputs ("\n\t.set\treorder", file);
6125
6126           break;
6127
6128         case '[':
6129           if (set_noat++ == 0)
6130             fputs (".set\tnoat\n\t", file);
6131           break;
6132
6133         case ']':
6134           if (set_noat == 0)
6135             error ("internal error: %%] found without a %%[ in assembler pattern");
6136           else if (--set_noat == 0)
6137             fputs ("\n\t.set\tat", file);
6138
6139           break;
6140
6141         case '<':
6142           if (set_nomacro++ == 0)
6143             fputs (".set\tnomacro\n\t", file);
6144           break;
6145
6146         case '>':
6147           if (set_nomacro == 0)
6148             error ("internal error: %%> found without a %%< in assembler pattern");
6149           else if (--set_nomacro == 0)
6150             fputs ("\n\t.set\tmacro", file);
6151
6152           break;
6153
6154         case '{':
6155           if (set_volatile++ == 0)
6156             fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
6157           break;
6158
6159         case '}':
6160           if (set_volatile == 0)
6161             error ("internal error: %%} found without a %%{ in assembler pattern");
6162           else if (--set_volatile == 0)
6163             fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
6164
6165           break;
6166
6167         case '~':
6168           {
6169             if (align_labels_log > 0)
6170               ASM_OUTPUT_ALIGN (file, align_labels_log);
6171           }
6172         break;
6173
6174         default:
6175           error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
6176           break;
6177         }
6178
6179       return;
6180     }
6181
6182   if (! op)
6183     {
6184       error ("PRINT_OPERAND null pointer");
6185       return;
6186     }
6187
6188   code = GET_CODE (op);
6189
6190   if (code == SIGN_EXTEND)
6191     op = XEXP (op, 0), code = GET_CODE (op);
6192
6193   if (letter == 'C')
6194     switch (code)
6195       {
6196       case EQ:  fputs ("eq",  file); break;
6197       case NE:  fputs ("ne",  file); break;
6198       case GT:  fputs ("gt",  file); break;
6199       case GE:  fputs ("ge",  file); break;
6200       case LT:  fputs ("lt",  file); break;
6201       case LE:  fputs ("le",  file); break;
6202       case GTU: fputs ("gtu", file); break;
6203       case GEU: fputs ("geu", file); break;
6204       case LTU: fputs ("ltu", file); break;
6205       case LEU: fputs ("leu", file); break;
6206       default:
6207         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
6208       }
6209
6210   else if (letter == 'N')
6211     switch (code)
6212       {
6213       case EQ:  fputs ("ne",  file); break;
6214       case NE:  fputs ("eq",  file); break;
6215       case GT:  fputs ("le",  file); break;
6216       case GE:  fputs ("lt",  file); break;
6217       case LT:  fputs ("ge",  file); break;
6218       case LE:  fputs ("gt",  file); break;
6219       case GTU: fputs ("leu", file); break;
6220       case GEU: fputs ("ltu", file); break;
6221       case LTU: fputs ("geu", file); break;
6222       case LEU: fputs ("gtu", file); break;
6223       default:
6224         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
6225       }
6226
6227   else if (letter == 'F')
6228     switch (code)
6229       {
6230       case EQ: fputs ("c1f", file); break;
6231       case NE: fputs ("c1t", file); break;
6232       default:
6233         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
6234       }
6235
6236   else if (letter == 'W')
6237     switch (code)
6238       {
6239       case EQ: fputs ("c1t", file); break;
6240       case NE: fputs ("c1f", file); break;
6241       default:
6242         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
6243       }
6244
6245   else if (letter == 'S')
6246     {
6247       char buffer[100];
6248
6249       ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
6250       assemble_name (file, buffer);
6251     }
6252
6253   else if (letter == 'Z')
6254     {
6255       register int regnum;
6256
6257       if (code != REG)
6258         abort ();
6259
6260       regnum = REGNO (op);
6261       if (! ST_REG_P (regnum))
6262         abort ();
6263
6264       if (regnum != ST_REG_FIRST)
6265         fprintf (file, "%s,", reg_names[regnum]);
6266     }
6267
6268   else if (code == REG || code == SUBREG)
6269     {
6270       register int regnum;
6271
6272       if (code == REG)
6273         regnum = REGNO (op);
6274       else
6275         regnum = true_regnum (op);
6276
6277       if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
6278           || (letter == 'L' && WORDS_BIG_ENDIAN)
6279           || letter == 'D')
6280         regnum++;
6281
6282       fprintf (file, "%s", reg_names[regnum]);
6283     }
6284
6285   else if (code == MEM)
6286     {
6287       if (letter == 'D')
6288         output_address (plus_constant (XEXP (op, 0), 4));
6289       else
6290         output_address (XEXP (op, 0));
6291     }
6292
6293   else if (code == CONST_DOUBLE
6294            && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
6295     {
6296       char s[60];
6297
6298       real_to_decimal (s, CONST_DOUBLE_REAL_VALUE (op), sizeof (s), 0, 1);
6299       fputs (s, file);
6300     }
6301
6302   else if (letter == 'x' && GET_CODE (op) == CONST_INT)
6303     fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
6304
6305   else if (letter == 'X' && GET_CODE(op) == CONST_INT)
6306     fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
6307
6308   else if (letter == 'd' && GET_CODE(op) == CONST_INT)
6309     fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
6310
6311   else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
6312     fputs (reg_names[GP_REG_FIRST], file);
6313
6314   else if (letter == 'd' || letter == 'x' || letter == 'X')
6315     output_operand_lossage ("invalid use of %%d, %%x, or %%X");
6316
6317   else if (letter == 'B')
6318     fputs (code == EQ ? "z" : "n", file);
6319   else if (letter == 'b')
6320     fputs (code == EQ ? "n" : "z", file);
6321   else if (letter == 'T')
6322     fputs (code == EQ ? "f" : "t", file);
6323   else if (letter == 't')
6324     fputs (code == EQ ? "t" : "f", file);
6325
6326   else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
6327     {
6328       /* This case arises on the mips16; see mips16_gp_pseudo_reg.  */
6329       print_operand (file, XEXP (op, 0), letter);
6330     }
6331
6332   else if (TARGET_MIPS16 && code == CONST && mips16_gp_offset_p (op))
6333     {
6334       fputs ("%gprel(", file);
6335       mips16_output_gp_offset (file, op);
6336       fputs (")", file);
6337     }
6338
6339   else
6340     output_addr_const (file, op);
6341 }
6342 \f
6343 /* A C compound statement to output to stdio stream STREAM the
6344    assembler syntax for an instruction operand that is a memory
6345    reference whose address is ADDR.  ADDR is an RTL expression.  */
6346
6347 void
6348 print_operand_address (file, addr)
6349      FILE *file;
6350      rtx addr;
6351 {
6352   if (!addr)
6353     error ("PRINT_OPERAND_ADDRESS, null pointer");
6354
6355   else
6356     switch (GET_CODE (addr))
6357       {
6358       case REG:
6359         if (! TARGET_MIPS16 && REGNO (addr) == ARG_POINTER_REGNUM)
6360           abort_with_insn (addr, "arg pointer not eliminated");
6361
6362         fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
6363         break;
6364
6365       case LO_SUM:
6366         {
6367           register rtx arg0 = XEXP (addr, 0);
6368           register rtx arg1 = XEXP (addr, 1);
6369
6370           if (! mips_split_addresses)
6371             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, Spurious LO_SUM");
6372
6373           if (GET_CODE (arg0) != REG)
6374             abort_with_insn (addr,
6375                              "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG");
6376
6377           fprintf (file, "%%lo(");
6378           print_operand_address (file, arg1);
6379           fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
6380         }
6381         break;
6382
6383       case PLUS:
6384         {
6385           register rtx reg = 0;
6386           register rtx offset = 0;
6387           register rtx arg0 = XEXP (addr, 0);
6388           register rtx arg1 = XEXP (addr, 1);
6389
6390           if (GET_CODE (arg0) == REG)
6391             {
6392               reg = arg0;
6393               offset = arg1;
6394               if (GET_CODE (offset) == REG)
6395                 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
6396             }
6397
6398           else if (GET_CODE (arg1) == REG)
6399               reg = arg1, offset = arg0;
6400           else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
6401             {
6402               output_addr_const (file, addr);
6403               break;
6404             }
6405           else
6406             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
6407
6408           if (! CONSTANT_P (offset))
6409             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
6410
6411           if (REGNO (reg) == ARG_POINTER_REGNUM)
6412             abort_with_insn (addr, "arg pointer not eliminated");
6413
6414           if (TARGET_MIPS16
6415               && GET_CODE (offset) == CONST
6416               && mips16_gp_offset_p (offset))
6417             {
6418               fputs ("%gprel(", file);
6419               mips16_output_gp_offset (file, offset);
6420               fputs (")", file);
6421             }
6422           else
6423             output_addr_const (file, offset);
6424           fprintf (file, "(%s)", reg_names [REGNO (reg)]);
6425         }
6426         break;
6427
6428       case LABEL_REF:
6429       case SYMBOL_REF:
6430       case CONST_INT:
6431       case CONST:
6432         output_addr_const (file, addr);
6433         break;
6434
6435       default:
6436         abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
6437         break;
6438     }
6439 }
6440 \f
6441 /* Target hook for assembling integer objects.  It appears that the Irix
6442    6 assembler can't handle 64-bit decimal integers, so avoid printing
6443    such an integer here.  */
6444
6445 static bool
6446 mips_assemble_integer (x, size, aligned_p)
6447      rtx x;
6448      unsigned int size;
6449      int aligned_p;
6450 {
6451   if ((TARGET_64BIT || TARGET_GAS) && size == 8 && aligned_p)
6452     {
6453       fputs ("\t.dword\t", asm_out_file);
6454       if (HOST_BITS_PER_WIDE_INT < 64 || GET_CODE (x) != CONST_INT)
6455         output_addr_const (asm_out_file, x);
6456       else
6457         print_operand (asm_out_file, x, 'X');
6458       fputc ('\n', asm_out_file);
6459       return true;
6460     }
6461   return default_assemble_integer (x, size, aligned_p);
6462 }
6463 \f
6464 /* If optimizing for the global pointer, keep track of all of the externs, so
6465    that at the end of the file, we can emit the appropriate .extern
6466    declaration for them, before writing out the text section.  We assume all
6467    names passed to us are in the permanent obstack, so they will be valid at
6468    the end of the compilation.
6469
6470    If we have -G 0, or the extern size is unknown, or the object is in a user
6471    specified section that is not .sbss/.sdata, don't bother emitting the
6472    .externs.  In the case of user specified sections this behavior is
6473    required as otherwise GAS will think the object lives in .sbss/.sdata.  */
6474
6475 int
6476 mips_output_external (file, decl, name)
6477      FILE *file ATTRIBUTE_UNUSED;
6478      tree decl;
6479      const char *name;
6480 {
6481   register struct extern_list *p;
6482   int len;
6483   tree section_name;
6484
6485   if (TARGET_GP_OPT
6486       && TREE_CODE (decl) != FUNCTION_DECL
6487       && !DECL_COMDAT (decl)
6488       && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
6489       && ((section_name = DECL_SECTION_NAME (decl)) == NULL
6490           || strcmp (TREE_STRING_POINTER (section_name), ".sbss") == 0
6491           || strcmp (TREE_STRING_POINTER (section_name), ".sdata") == 0))
6492     {
6493       p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6494       p->next = extern_head;
6495       p->name = name;
6496       p->size = len;
6497       extern_head = p;
6498     }
6499
6500 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6501   if (TREE_CODE (decl) == FUNCTION_DECL
6502       /* ??? Don't include alloca, since gcc will always expand it
6503          inline.  If we don't do this, the C++ library fails to build.  */
6504       && strcmp (name, "alloca")
6505       /* ??? Don't include __builtin_next_arg, because then gcc will not
6506          bootstrap under Irix 5.1.  */
6507       && strcmp (name, "__builtin_next_arg"))
6508     {
6509       p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6510       p->next = extern_head;
6511       p->name = name;
6512       p->size = -1;
6513       extern_head = p;
6514     }
6515 #endif
6516
6517   return 0;
6518 }
6519
6520 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6521 int
6522 mips_output_external_libcall (file, name)
6523      FILE *file ATTRIBUTE_UNUSED;
6524      const char *name;
6525 {
6526   register struct extern_list *p;
6527
6528   p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6529   p->next = extern_head;
6530   p->name = name;
6531   p->size = -1;
6532   extern_head = p;
6533
6534   return 0;
6535 }
6536 #endif
6537 \f
6538 /* Emit a new filename to a stream.  If this is MIPS ECOFF, watch out
6539    for .file's that start within a function.  If we are smuggling stabs, try to
6540    put out a MIPS ECOFF file and a stab.  */
6541
6542 void
6543 mips_output_filename (stream, name)
6544      FILE *stream;
6545      const char *name;
6546 {
6547   static int first_time = 1;
6548   char ltext_label_name[100];
6549
6550   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
6551      directives.  */
6552   if (write_symbols == DWARF2_DEBUG)
6553     return;
6554   else if (first_time)
6555     {
6556       first_time = 0;
6557       SET_FILE_NUMBER ();
6558       current_function_file = name;
6559       ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
6560       /* This tells mips-tfile that stabs will follow.  */
6561       if (!TARGET_GAS && write_symbols == DBX_DEBUG)
6562         fprintf (stream, "\t#@stabs\n");
6563     }
6564
6565   else if (write_symbols == DBX_DEBUG)
6566     {
6567       ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
6568       fprintf (stream, "%s", ASM_STABS_OP);
6569       output_quoted_string (stream, name);
6570       fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
6571     }
6572
6573   else if (name != current_function_file
6574       && strcmp (name, current_function_file) != 0)
6575     {
6576       if (inside_function && !TARGET_GAS)
6577         {
6578           if (!file_in_function_warning)
6579             {
6580               file_in_function_warning = 1;
6581               ignore_line_number = 1;
6582               warning ("MIPS ECOFF format does not allow changing filenames within functions with #line");
6583             }
6584         }
6585       else
6586         {
6587           SET_FILE_NUMBER ();
6588           current_function_file = name;
6589           ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
6590         }
6591     }
6592 }
6593 \f
6594 /* Emit a linenumber.  For encapsulated stabs, we need to put out a stab
6595    as well as a .loc, since it is possible that MIPS ECOFF might not be
6596    able to represent the location for inlines that come from a different
6597    file.  */
6598
6599 void
6600 mips_output_lineno (stream, line)
6601      FILE *stream;
6602      int line;
6603 {
6604   if (write_symbols == DBX_DEBUG)
6605     {
6606       ++sym_lineno;
6607       fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
6608                LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
6609                LOCAL_LABEL_PREFIX, sym_lineno);
6610     }
6611   else
6612     {
6613       fprintf (stream, "\n\t%s.loc\t%d %d\n",
6614                (ignore_line_number) ? "#" : "",
6615                num_source_filenames, line);
6616
6617       LABEL_AFTER_LOC (stream);
6618     }
6619 }
6620 \f
6621 /* Output an ASCII string, in a space-saving way.  */
6622
6623 void
6624 mips_output_ascii (stream, string_param, len)
6625      FILE *stream;
6626      const char *string_param;
6627      size_t len;
6628 {
6629   size_t i;
6630   int cur_pos = 17;
6631   register const unsigned char *string =
6632     (const unsigned char *)string_param;
6633
6634   fprintf (stream, "\t.ascii\t\"");
6635   for (i = 0; i < len; i++)
6636     {
6637       register int c = string[i];
6638
6639       switch (c)
6640         {
6641         case '\"':
6642         case '\\':
6643           putc ('\\', stream);
6644           putc (c, stream);
6645           cur_pos += 2;
6646           break;
6647
6648         case TARGET_NEWLINE:
6649           fputs ("\\n", stream);
6650           if (i+1 < len
6651               && (((c = string[i+1]) >= '\040' && c <= '~')
6652                   || c == TARGET_TAB))
6653             cur_pos = 32767;            /* break right here */
6654           else
6655             cur_pos += 2;
6656           break;
6657
6658         case TARGET_TAB:
6659           fputs ("\\t", stream);
6660           cur_pos += 2;
6661           break;
6662
6663         case TARGET_FF:
6664           fputs ("\\f", stream);
6665           cur_pos += 2;
6666           break;
6667
6668         case TARGET_BS:
6669           fputs ("\\b", stream);
6670           cur_pos += 2;
6671           break;
6672
6673         case TARGET_CR:
6674           fputs ("\\r", stream);
6675           cur_pos += 2;
6676           break;
6677
6678         default:
6679           if (c >= ' ' && c < 0177)
6680             {
6681               putc (c, stream);
6682               cur_pos++;
6683             }
6684           else
6685             {
6686               fprintf (stream, "\\%03o", c);
6687               cur_pos += 4;
6688             }
6689         }
6690
6691       if (cur_pos > 72 && i+1 < len)
6692         {
6693           cur_pos = 17;
6694           fprintf (stream, "\"\n\t.ascii\t\"");
6695         }
6696     }
6697   fprintf (stream, "\"\n");
6698 }
6699 \f
6700 /* If defined, a C statement to be executed just prior to the output of
6701    assembler code for INSN, to modify the extracted operands so they will be
6702    output differently.
6703
6704    Here the argument OPVEC is the vector containing the operands extracted
6705    from INSN, and NOPERANDS is the number of elements of the vector which
6706    contain meaningful data for this insn.  The contents of this vector are
6707    what will be used to convert the insn template into assembler code, so you
6708    can change the assembler output by changing the contents of the vector.
6709
6710    We use it to check if the current insn needs a nop in front of it because
6711    of load delays, and also to update the delay slot statistics.  */
6712
6713 /* ??? There is no real need for this function, because it never actually
6714    emits a NOP anymore.  */
6715
6716 void
6717 final_prescan_insn (insn, opvec, noperands)
6718      rtx insn;
6719      rtx opvec[] ATTRIBUTE_UNUSED;
6720      int noperands ATTRIBUTE_UNUSED;
6721 {
6722   if (dslots_number_nops > 0)
6723     {
6724       rtx pattern = PATTERN (insn);
6725       int length = get_attr_length (insn);
6726
6727       /* Do we need to emit a NOP? */
6728       if (length == 0
6729           || (mips_load_reg != 0 && reg_mentioned_p (mips_load_reg,  pattern))
6730           || (mips_load_reg2 != 0 && reg_mentioned_p (mips_load_reg2, pattern))
6731           || (mips_load_reg3 != 0 && reg_mentioned_p (mips_load_reg3, pattern))
6732           || (mips_load_reg4 != 0
6733               && reg_mentioned_p (mips_load_reg4, pattern)))
6734         fputs ("\t#nop\n", asm_out_file);
6735
6736       else
6737         dslots_load_filled++;
6738
6739       while (--dslots_number_nops > 0)
6740         fputs ("\t#nop\n", asm_out_file);
6741
6742       mips_load_reg = 0;
6743       mips_load_reg2 = 0;
6744       mips_load_reg3 = 0;
6745       mips_load_reg4 = 0;
6746     }
6747
6748   if (TARGET_STATS
6749       && (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CALL_INSN))
6750     dslots_jump_total++;
6751 }
6752 \f
6753 /* Output at beginning of assembler file.
6754
6755    If we are optimizing to use the global pointer, create a temporary file to
6756    hold all of the text stuff, and write it out to the end. This is needed
6757    because the MIPS assembler is evidently one pass, and if it hasn't seen the
6758    relevant .comm/.lcomm/.extern/.sdata declaration when the code is
6759    processed, it generates a two instruction sequence.  */
6760
6761 void
6762 mips_asm_file_start (stream)
6763      FILE *stream;
6764 {
6765   ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
6766
6767   /* Versions of the MIPS assembler before 2.20 generate errors if a branch
6768      inside of a .set noreorder section jumps to a label outside of the .set
6769      noreorder section.  Revision 2.20 just set nobopt silently rather than
6770      fixing the bug.  */
6771
6772   if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
6773     fprintf (stream, "\t.set\tnobopt\n");
6774
6775   if (TARGET_GAS)
6776     {
6777 #if defined(OBJECT_FORMAT_ELF) && !(TARGET_IRIX5 || TARGET_IRIX6)
6778       /* Generate a special section to describe the ABI switches used to
6779          produce the resultant binary.  This used to be done by the assembler
6780          setting bits in the ELF header's flags field, but we have run out of
6781          bits.  GDB needs this information in order to be able to correctly
6782          debug these binaries.  See the function mips_gdbarch_init() in
6783          gdb/mips-tdep.c.  This is unnecessary for the IRIX 5/6 ABIs and
6784          causes unnecessary IRIX 6 ld warnings.  */
6785       const char * abi_string = NULL;
6786
6787       switch (mips_abi)
6788         {
6789         case ABI_32:   abi_string = "abi32"; break;
6790         case ABI_N32:  abi_string = "abiN32"; break;
6791         case ABI_64:   abi_string = "abi64"; break;
6792         case ABI_O64:  abi_string = "abiO64"; break;
6793         case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
6794         case ABI_MEABI:abi_string = TARGET_64BIT ? "meabi64" : "meabi32"; break;
6795         default:
6796           abort ();
6797         }
6798       /* Note - we use fprintf directly rather than called named_section()
6799          because in this way we can avoid creating an allocated section.  We
6800          do not want this section to take up any space in the running
6801          executable.  */
6802       fprintf (stream, "\t.section .mdebug.%s\n", abi_string);
6803
6804       /* Restore the default section.  */
6805       fprintf (stream, "\t.previous\n");
6806 #endif
6807     }
6808
6809
6810
6811   /* Generate the pseudo ops that System V.4 wants.  */
6812 #ifndef ABICALLS_ASM_OP
6813 #define ABICALLS_ASM_OP "\t.abicalls"
6814 #endif
6815   if (TARGET_ABICALLS)
6816     /* ??? but do not want this (or want pic0) if -non-shared? */
6817     fprintf (stream, "%s\n", ABICALLS_ASM_OP);
6818
6819   if (TARGET_MIPS16)
6820     fprintf (stream, "\t.set\tmips16\n");
6821
6822   /* This code exists so that we can put all externs before all symbol
6823      references.  This is necessary for the MIPS assembler's global pointer
6824      optimizations to work.  */
6825   if (TARGET_FILE_SWITCHING)
6826     {
6827       asm_out_data_file = stream;
6828       asm_out_text_file = tmpfile ();
6829     }
6830   else
6831     asm_out_data_file = asm_out_text_file = stream;
6832
6833   if (flag_verbose_asm)
6834     fprintf (stream, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
6835              ASM_COMMENT_START,
6836              mips_section_threshold, mips_arch_info->name, mips_isa);
6837 }
6838 \f
6839 /* If we are optimizing the global pointer, emit the text section now and any
6840    small externs which did not have .comm, etc that are needed.  Also, give a
6841    warning if the data area is more than 32K and -pic because 3 instructions
6842    are needed to reference the data pointers.  */
6843
6844 void
6845 mips_asm_file_end (file)
6846      FILE *file;
6847 {
6848   tree name_tree;
6849   struct extern_list *p;
6850
6851   if (extern_head)
6852     {
6853       fputs ("\n", file);
6854
6855       for (p = extern_head; p != 0; p = p->next)
6856         {
6857           name_tree = get_identifier (p->name);
6858
6859           /* Positively ensure only one .extern for any given symbol.  */
6860           if (! TREE_ASM_WRITTEN (name_tree))
6861             {
6862               TREE_ASM_WRITTEN (name_tree) = 1;
6863 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6864               if (p->size == -1)
6865                 ASM_OUTPUT_UNDEF_FUNCTION (file, p->name);
6866               else
6867 #endif
6868                 {
6869                   fputs ("\t.extern\t", file);
6870                   assemble_name (file, p->name);
6871                   fprintf (file, ", %d\n", p->size);
6872                 }
6873             }
6874         }
6875     }
6876
6877   if (TARGET_FILE_SWITCHING)
6878     {
6879       fprintf (file, "\n\t.text\n");
6880       copy_file_data (file, asm_out_text_file);
6881     }
6882 }
6883
6884 static void
6885 copy_file_data (to, from)
6886      FILE *to, *from;
6887 {
6888   char buffer[8192];
6889   size_t len;
6890   rewind (from);
6891   if (ferror (from))
6892     fatal_io_error ("can't rewind temp file");
6893
6894   while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
6895     if (fwrite (buffer, 1, len, to) != len)
6896       fatal_io_error ("can't write to output file");
6897
6898   if (ferror (from))
6899     fatal_io_error ("can't read from temp file");
6900
6901   if (fclose (from))
6902     fatal_io_error ("can't close temp file");
6903 }
6904
6905 /* Emit either a label, .comm, or .lcomm directive, and mark that the symbol
6906    is used, so that we don't emit an .extern for it in mips_asm_file_end.  */
6907
6908 void
6909 mips_declare_object (stream, name, init_string, final_string, size)
6910      FILE *stream;
6911      const char *name;
6912      const char *init_string;
6913      const char *final_string;
6914      int size;
6915 {
6916   fputs (init_string, stream);          /* "", "\t.comm\t", or "\t.lcomm\t" */
6917   assemble_name (stream, name);
6918   fprintf (stream, final_string, size); /* ":\n", ",%u\n", ",%u\n" */
6919
6920   if (TARGET_GP_OPT)
6921     {
6922       tree name_tree = get_identifier (name);
6923       TREE_ASM_WRITTEN (name_tree) = 1;
6924     }
6925 }
6926 \f
6927 /* Return the bytes needed to compute the frame pointer from the current
6928    stack pointer.
6929
6930    Mips stack frames look like:
6931
6932              Before call                        After call
6933         +-----------------------+       +-----------------------+
6934    high |                       |       |                       |
6935    mem. |                       |       |                       |
6936         |  caller's temps.      |       |  caller's temps.      |
6937         |                       |       |                       |
6938         +-----------------------+       +-----------------------+
6939         |                       |       |                       |
6940         |  arguments on stack.  |       |  arguments on stack.  |
6941         |                       |       |                       |
6942         +-----------------------+       +-----------------------+
6943         |  4 words to save      |       |  4 words to save      |
6944         |  arguments passed     |       |  arguments passed     |
6945         |  in registers, even   |       |  in registers, even   |
6946     SP->|  if not passed.       |  VFP->|  if not passed.       |
6947         +-----------------------+       +-----------------------+
6948                                         |                       |
6949                                         |  fp register save     |
6950                                         |                       |
6951                                         +-----------------------+
6952                                         |                       |
6953                                         |  gp register save     |
6954                                         |                       |
6955                                         +-----------------------+
6956                                         |                       |
6957                                         |  local variables      |
6958                                         |                       |
6959                                         +-----------------------+
6960                                         |                       |
6961                                         |  alloca allocations   |
6962                                         |                       |
6963                                         +-----------------------+
6964                                         |                       |
6965                                         |  GP save for V.4 abi  |
6966                                         |                       |
6967                                         +-----------------------+
6968                                         |                       |
6969                                         |  arguments on stack   |
6970                                         |                       |
6971                                         +-----------------------+
6972                                         |  4 words to save      |
6973                                         |  arguments passed     |
6974                                         |  in registers, even   |
6975    low                              SP->|  if not passed.       |
6976    memory                               +-----------------------+
6977
6978 */
6979
6980 HOST_WIDE_INT
6981 compute_frame_size (size)
6982      HOST_WIDE_INT size;        /* # of var. bytes allocated */
6983 {
6984   unsigned int regno;
6985   HOST_WIDE_INT total_size;     /* # bytes that the entire frame takes up */
6986   HOST_WIDE_INT var_size;       /* # bytes that variables take up */
6987   HOST_WIDE_INT args_size;      /* # bytes that outgoing arguments take up */
6988   HOST_WIDE_INT extra_size;     /* # extra bytes */
6989   HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6990   HOST_WIDE_INT gp_reg_size;    /* # bytes needed to store gp regs */
6991   HOST_WIDE_INT fp_reg_size;    /* # bytes needed to store fp regs */
6992   long mask;                    /* mask of saved gp registers */
6993   long fmask;                   /* mask of saved fp registers */
6994   tree return_type;
6995
6996   gp_reg_size = 0;
6997   fp_reg_size = 0;
6998   mask = 0;
6999   fmask = 0;
7000   extra_size = MIPS_STACK_ALIGN (((TARGET_ABICALLS) ? UNITS_PER_WORD : 0));
7001   var_size = MIPS_STACK_ALIGN (size);
7002   args_size = MIPS_STACK_ALIGN (current_function_outgoing_args_size);
7003
7004   /* The MIPS 3.0 linker does not like functions that dynamically
7005      allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
7006      looks like we are trying to create a second frame pointer to the
7007      function, so allocate some stack space to make it happy.  */
7008
7009   if (args_size == 0 && current_function_calls_alloca)
7010     args_size = 4 * UNITS_PER_WORD;
7011
7012   total_size = var_size + args_size + extra_size;
7013   return_type = DECL_RESULT (current_function_decl);
7014
7015   /* Calculate space needed for gp registers.  */
7016   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
7017     {
7018       /* $18 is a special case on the mips16.  It may be used to call
7019          a function which returns a floating point value, but it is
7020          marked in call_used_regs.  $31 is also a special case.  When
7021          not using -mentry, it will be used to copy a return value
7022          into the floating point registers if the return value is
7023          floating point.  */
7024       if (MUST_SAVE_REGISTER (regno)
7025           || (TARGET_MIPS16
7026               && regno == GP_REG_FIRST + 18
7027               && regs_ever_live[regno])
7028           || (TARGET_MIPS16
7029               && regno == GP_REG_FIRST + 31
7030               && mips16_hard_float
7031               && ! mips_entry
7032               && ! aggregate_value_p (return_type)
7033               && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
7034               && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE))
7035         {
7036           gp_reg_size += GET_MODE_SIZE (gpr_mode);
7037           mask |= 1L << (regno - GP_REG_FIRST);
7038
7039           /* The entry and exit pseudo instructions can not save $17
7040              without also saving $16.  */
7041           if (mips_entry
7042               && regno == GP_REG_FIRST + 17
7043               && ! MUST_SAVE_REGISTER (GP_REG_FIRST + 16))
7044             {
7045               gp_reg_size += UNITS_PER_WORD;
7046               mask |= 1L << 16;
7047             }
7048         }
7049     }
7050
7051   /* We need to restore these for the handler.  */
7052   if (current_function_calls_eh_return)
7053     {
7054       unsigned int i;
7055       for (i = 0; ; ++i)
7056         {
7057           regno = EH_RETURN_DATA_REGNO (i);
7058           if (regno == INVALID_REGNUM)
7059             break;
7060           gp_reg_size += GET_MODE_SIZE (gpr_mode);
7061           mask |= 1L << (regno - GP_REG_FIRST);
7062         }
7063     }
7064
7065   /* This loop must iterate over the same space as its companion in
7066      save_restore_insns.  */
7067   for (regno = (FP_REG_LAST - FP_INC + 1);
7068        regno >= FP_REG_FIRST;
7069        regno -= FP_INC)
7070     {
7071       if (regs_ever_live[regno] && !call_used_regs[regno])
7072         {
7073           fp_reg_size += FP_INC * UNITS_PER_FPREG;
7074           fmask |= ((1 << FP_INC) - 1) << (regno - FP_REG_FIRST);
7075         }
7076     }
7077
7078   gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
7079   total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
7080
7081   /* The gp reg is caller saved in the 32 bit ABI, so there is no need
7082      for leaf routines (total_size == extra_size) to save the gp reg.
7083      The gp reg is callee saved in the 64 bit ABI, so all routines must
7084      save the gp reg.  This is not a leaf routine if -p, because of the
7085      call to mcount.  */
7086   if (total_size == extra_size
7087       && (mips_abi == ABI_32 || mips_abi == ABI_O64 || mips_abi == ABI_EABI)
7088       && ! current_function_profile)
7089     total_size = extra_size = 0;
7090   else if (TARGET_ABICALLS)
7091     {
7092       /* Add the context-pointer to the saved registers.  */
7093       gp_reg_size += UNITS_PER_WORD;
7094       mask |= 1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST);
7095       total_size -= gp_reg_rounded;
7096       gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
7097       total_size += gp_reg_rounded;
7098     }
7099
7100   /* Add in space reserved on the stack by the callee for storing arguments
7101      passed in registers.  */
7102   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
7103     total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
7104
7105   /* The entry pseudo instruction will allocate 32 bytes on the stack.  */
7106   if (mips_entry && total_size > 0 && total_size < 32)
7107     total_size = 32;
7108
7109   /* Save other computed information.  */
7110   cfun->machine->frame.total_size = total_size;
7111   cfun->machine->frame.var_size = var_size;
7112   cfun->machine->frame.args_size = args_size;
7113   cfun->machine->frame.extra_size = extra_size;
7114   cfun->machine->frame.gp_reg_size = gp_reg_size;
7115   cfun->machine->frame.fp_reg_size = fp_reg_size;
7116   cfun->machine->frame.mask = mask;
7117   cfun->machine->frame.fmask = fmask;
7118   cfun->machine->frame.initialized = reload_completed;
7119   cfun->machine->frame.num_gp = gp_reg_size / UNITS_PER_WORD;
7120   cfun->machine->frame.num_fp = fp_reg_size / (FP_INC * UNITS_PER_FPREG);
7121
7122   if (mask)
7123     {
7124       unsigned long offset;
7125
7126       /* When using mips_entry, the registers are always saved at the
7127          top of the stack.  */
7128       if (! mips_entry)
7129         offset = (args_size + extra_size + var_size
7130                   + gp_reg_size - GET_MODE_SIZE (gpr_mode));
7131       else
7132         offset = total_size - GET_MODE_SIZE (gpr_mode);
7133
7134       cfun->machine->frame.gp_sp_offset = offset;
7135       cfun->machine->frame.gp_save_offset = offset - total_size;
7136     }
7137   else
7138     {
7139       cfun->machine->frame.gp_sp_offset = 0;
7140       cfun->machine->frame.gp_save_offset = 0;
7141     }
7142
7143   if (fmask)
7144     {
7145       unsigned long offset = (args_size + extra_size + var_size
7146                               + gp_reg_rounded + fp_reg_size
7147                               - FP_INC * UNITS_PER_FPREG);
7148       cfun->machine->frame.fp_sp_offset = offset;
7149       cfun->machine->frame.fp_save_offset = offset - total_size;
7150     }
7151   else
7152     {
7153       cfun->machine->frame.fp_sp_offset = 0;
7154       cfun->machine->frame.fp_save_offset = 0;
7155     }
7156
7157   /* Ok, we're done.  */
7158   return total_size;
7159 }
7160 \f
7161 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame
7162    pointer, argument pointer, or return address pointer.  TO is either
7163    the stack pointer or hard frame pointer.  */
7164
7165 int
7166 mips_initial_elimination_offset (from, to)
7167      int from, to;
7168 {
7169   int offset;
7170
7171   /* Set OFFSET to the offset from the stack pointer.  */
7172   switch (from)
7173     {
7174     case FRAME_POINTER_REGNUM:
7175       offset = 0;
7176       break;
7177
7178     case ARG_POINTER_REGNUM:
7179       compute_frame_size (get_frame_size ());
7180       offset = cfun->machine->frame.total_size;
7181       if (mips_abi == ABI_N32 || mips_abi == ABI_64 || mips_abi == ABI_MEABI)
7182         offset -= current_function_pretend_args_size;
7183       break;
7184
7185     case RETURN_ADDRESS_POINTER_REGNUM:
7186       compute_frame_size (get_frame_size ());
7187       offset = cfun->machine->frame.gp_sp_offset;
7188       if (BYTES_BIG_ENDIAN)
7189         offset += UNITS_PER_WORD - (POINTER_SIZE / BITS_PER_UNIT);
7190       break;
7191
7192     default:
7193       abort ();
7194     }
7195
7196   if (TARGET_MIPS16 && to == HARD_FRAME_POINTER_REGNUM)
7197     offset -= current_function_outgoing_args_size;
7198
7199   return offset;
7200 }
7201 \f
7202 /* Common code to emit the insns (or to write the instructions to a file)
7203    to save/restore registers.
7204
7205    Other parts of the code assume that MIPS_TEMP1_REGNUM (aka large_reg)
7206    is not modified within save_restore_insns.  */
7207
7208 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
7209
7210 /* Emit instructions to load the value (SP + OFFSET) into MIPS_TEMP2_REGNUM
7211    and return an rtl expression for the register.
7212
7213    This function is a subroutine of save_restore_insns.  It is used when
7214    OFFSET is too large to add in a single instruction.  */
7215
7216 static rtx
7217 mips_add_large_offset_to_sp (offset)
7218      HOST_WIDE_INT offset;
7219 {
7220   rtx reg = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7221   rtx offset_rtx = GEN_INT (offset);
7222
7223   emit_move_insn (reg, offset_rtx);
7224   if (Pmode == DImode)
7225     emit_insn (gen_adddi3 (reg, reg, stack_pointer_rtx));
7226   else
7227     emit_insn (gen_addsi3 (reg, reg, stack_pointer_rtx));
7228   return reg;
7229 }
7230
7231 /* Make INSN frame related and note that it performs the frame-related
7232    operation DWARF_PATTERN.  */
7233
7234 static void
7235 mips_annotate_frame_insn (insn, dwarf_pattern)
7236      rtx insn, dwarf_pattern;
7237 {
7238   RTX_FRAME_RELATED_P (insn) = 1;
7239   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
7240                                       dwarf_pattern,
7241                                       REG_NOTES (insn));
7242 }
7243
7244 /* Return a frame-related rtx that stores register REGNO at (SP + OFFSET).
7245    The expression should only be used to store single registers.  */
7246
7247 static rtx
7248 mips_frame_set (mode, regno, offset)
7249      enum machine_mode mode;
7250      int regno;
7251      int offset;
7252 {
7253   rtx address = plus_constant (stack_pointer_rtx, offset);
7254   rtx set = gen_rtx_SET (mode,
7255                          gen_rtx_MEM (mode, address),
7256                          gen_rtx_REG (mode, regno));
7257   RTX_FRAME_RELATED_P (set) = 1;
7258   return set;
7259 }
7260
7261
7262 /* Emit a move instruction that stores REG in MEM.  Make the instruction
7263    frame related and note that it stores REG at (SP + OFFSET).  This
7264    function may be asked to store an FPR pair.  */
7265
7266 static void
7267 mips_emit_frame_related_store (mem, reg, offset)
7268      rtx mem;
7269      rtx reg;
7270      HOST_WIDE_INT offset;
7271 {
7272   rtx dwarf_expr;
7273
7274   if (GET_MODE (reg) == DFmode && ! TARGET_FLOAT64)
7275     {
7276       /* Two registers are being stored, so the frame-related expression
7277          must be a PARALLEL rtx with one SET for each register.  The
7278          higher numbered register is stored in the lower address on
7279          big-endian targets.  */
7280       int regno1 = TARGET_BIG_ENDIAN ? REGNO (reg) + 1 : REGNO (reg);
7281       int regno2 = TARGET_BIG_ENDIAN ? REGNO (reg) : REGNO (reg) + 1;
7282       rtx set1 = mips_frame_set (SFmode, regno1, offset);
7283       rtx set2 = mips_frame_set (SFmode, regno2, offset + UNITS_PER_FPREG);
7284       dwarf_expr = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set1, set2));
7285     }
7286   else
7287     dwarf_expr = mips_frame_set (GET_MODE (reg), REGNO (reg), offset);
7288
7289   mips_annotate_frame_insn (emit_move_insn (mem, reg), dwarf_expr);
7290 }
7291
7292 static void
7293 save_restore_insns (store_p, large_reg, large_offset)
7294      int store_p;       /* true if this is prologue */
7295      rtx large_reg;     /* register holding large offset constant or NULL */
7296      long large_offset; /* large constant offset value */
7297 {
7298   long mask = cfun->machine->frame.mask;
7299   long fmask = cfun->machine->frame.fmask;
7300   long real_mask = mask;
7301   int regno;
7302   rtx base_reg_rtx;
7303   HOST_WIDE_INT base_offset;
7304   HOST_WIDE_INT gp_offset;
7305   HOST_WIDE_INT fp_offset;
7306   HOST_WIDE_INT end_offset;
7307
7308   if (frame_pointer_needed
7309       && ! BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST))
7310     abort ();
7311
7312   /* Do not restore GP under certain conditions.  */
7313   if (! store_p
7314       && TARGET_ABICALLS
7315       && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7316     mask &= ~(1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST));
7317
7318   if (mask == 0 && fmask == 0)
7319     return;
7320
7321   /* Save registers starting from high to low.  The debuggers prefer at least
7322      the return register be stored at func+4, and also it allows us not to
7323      need a nop in the epilog if at least one register is reloaded in
7324      addition to return address.  */
7325
7326   /* Save GP registers if needed.  */
7327   if (mask)
7328     {
7329       /* Pick which pointer to use as a base register.  For small frames, just
7330          use the stack pointer.  Otherwise, use a temporary register.  Save 2
7331          cycles if the save area is near the end of a large frame, by reusing
7332          the constant created in the prologue/epilogue to adjust the stack
7333          frame.  */
7334
7335       gp_offset = cfun->machine->frame.gp_sp_offset;
7336       end_offset
7337         = gp_offset - (cfun->machine->frame.gp_reg_size
7338                        - GET_MODE_SIZE (gpr_mode));
7339
7340       if (gp_offset < 0 || end_offset < 0)
7341         internal_error
7342           ("gp_offset (%ld) or end_offset (%ld) is less than zero",
7343            (long) gp_offset, (long) end_offset);
7344
7345       /* If we see a large frame in mips16 mode, we save the registers
7346          before adjusting the stack pointer, and load them afterward.  */
7347       else if (TARGET_MIPS16 && large_offset > 32767)
7348         base_reg_rtx = stack_pointer_rtx, base_offset = large_offset;
7349
7350       else if (gp_offset < 32768)
7351         base_reg_rtx = stack_pointer_rtx, base_offset  = 0;
7352
7353       else if (large_reg != 0
7354                && (unsigned HOST_WIDE_INT) (large_offset - gp_offset) < 32768
7355                && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
7356         {
7357           base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7358           base_offset = large_offset;
7359           if (Pmode == DImode)
7360             emit_insn (gen_adddi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7361           else
7362             emit_insn (gen_addsi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7363         }
7364       else
7365         {
7366           base_offset = gp_offset;
7367           base_reg_rtx = mips_add_large_offset_to_sp (base_offset);
7368         }
7369
7370       /* When we restore the registers in MIPS16 mode, then if we are
7371          using a frame pointer, and this is not a large frame, the
7372          current stack pointer will be offset by
7373          current_function_outgoing_args_size.  Doing it this way lets
7374          us avoid offsetting the frame pointer before copying it into
7375          the stack pointer; there is no instruction to set the stack
7376          pointer to the sum of a register and a constant.  */
7377       if (TARGET_MIPS16
7378           && ! store_p
7379           && frame_pointer_needed
7380           && large_offset <= 32767)
7381         base_offset += current_function_outgoing_args_size;
7382
7383       for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
7384         {
7385           if (BITSET_P (mask, regno - GP_REG_FIRST))
7386             {
7387               rtx reg_rtx;
7388               rtx mem_rtx
7389                 = gen_rtx (MEM, gpr_mode,
7390                            gen_rtx (PLUS, Pmode, base_reg_rtx,
7391                                     GEN_INT (gp_offset - base_offset)));
7392
7393               if (! current_function_calls_eh_return)
7394                 RTX_UNCHANGING_P (mem_rtx) = 1;
7395
7396               /* The mips16 does not have an instruction to load
7397                  $31, so we load $7 instead, and work things out
7398                  in mips_expand_epilogue.  */
7399               if (TARGET_MIPS16 && ! store_p && regno == GP_REG_FIRST + 31)
7400                 reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 7);
7401               /* The mips16 sometimes needs to save $18.  */
7402               else if (TARGET_MIPS16
7403                        && regno != GP_REG_FIRST + 31
7404                        && ! M16_REG_P (regno))
7405                 {
7406                   if (! store_p)
7407                     reg_rtx = gen_rtx (REG, gpr_mode, 6);
7408                   else
7409                     {
7410                       reg_rtx = gen_rtx (REG, gpr_mode, 3);
7411                       emit_move_insn (reg_rtx,
7412                                       gen_rtx (REG, gpr_mode, regno));
7413                     }
7414                 }
7415               else
7416                 reg_rtx = gen_rtx (REG, gpr_mode, regno);
7417
7418               if (store_p)
7419                 mips_emit_frame_related_store (mem_rtx, reg_rtx, gp_offset);
7420               else
7421                 {
7422                   emit_move_insn (reg_rtx, mem_rtx);
7423                   if (TARGET_MIPS16
7424                       && regno != GP_REG_FIRST + 31
7425                       && ! M16_REG_P (regno))
7426                     emit_move_insn (gen_rtx (REG, gpr_mode, regno),
7427                                     reg_rtx);
7428                 }
7429             }
7430           /* If the restore is being supressed, still take into account
7431              the offset at which it is stored.  */
7432           if (BITSET_P (real_mask, regno - GP_REG_FIRST))
7433             gp_offset -= GET_MODE_SIZE (gpr_mode);
7434         }
7435     }
7436   else
7437     base_reg_rtx = 0, base_offset  = 0;
7438
7439   /* Save floating point registers if needed.  */
7440   if (fmask)
7441     {
7442       /* Pick which pointer to use as a base register.  */
7443       fp_offset = cfun->machine->frame.fp_sp_offset;
7444       end_offset = fp_offset - (cfun->machine->frame.fp_reg_size
7445                                 - UNITS_PER_HWFPVALUE);
7446
7447       if (fp_offset < 0 || end_offset < 0)
7448         internal_error
7449           ("fp_offset (%ld) or end_offset (%ld) is less than zero",
7450            (long) fp_offset, (long) end_offset);
7451
7452       else if (fp_offset < 32768)
7453         base_reg_rtx = stack_pointer_rtx, base_offset  = 0;
7454
7455       else if (base_reg_rtx != 0
7456                && (unsigned HOST_WIDE_INT) (base_offset - fp_offset) < 32768
7457                && (unsigned HOST_WIDE_INT) (base_offset - end_offset) < 32768)
7458         ;                       /* already set up for gp registers above */
7459
7460       else if (large_reg != 0
7461                && (unsigned HOST_WIDE_INT) (large_offset - fp_offset) < 32768
7462                && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
7463         {
7464           base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7465           base_offset = large_offset;
7466           if (Pmode == DImode)
7467             emit_insn (gen_adddi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7468           else
7469             emit_insn (gen_addsi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7470         }
7471       else
7472         {
7473           base_offset = fp_offset;
7474           base_reg_rtx = mips_add_large_offset_to_sp (fp_offset);
7475         }
7476
7477       /* This loop must iterate over the same space as its companion in
7478          compute_frame_size.  */
7479       for (regno = (FP_REG_LAST - FP_INC + 1);
7480            regno >= FP_REG_FIRST;
7481            regno -= FP_INC)
7482         if (BITSET_P (fmask, regno - FP_REG_FIRST))
7483           {
7484             enum machine_mode sz = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
7485             rtx reg_rtx = gen_rtx (REG, sz, regno);
7486             rtx mem_rtx = gen_rtx (MEM, sz,
7487                                    gen_rtx (PLUS, Pmode, base_reg_rtx,
7488                                             GEN_INT (fp_offset
7489                                                      - base_offset)));
7490             if (! current_function_calls_eh_return)
7491               RTX_UNCHANGING_P (mem_rtx) = 1;
7492
7493             if (store_p)
7494               mips_emit_frame_related_store (mem_rtx, reg_rtx, fp_offset);
7495             else
7496               emit_move_insn (reg_rtx, mem_rtx);
7497
7498             fp_offset -= UNITS_PER_HWFPVALUE;
7499           }
7500     }
7501 }
7502 \f
7503 /* Set up the stack and frame (if desired) for the function.  */
7504
7505 static void
7506 mips_output_function_prologue (file, size)
7507      FILE *file;
7508      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
7509 {
7510 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7511   const char *fnname;
7512 #endif
7513   HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
7514
7515   /* ??? When is this really needed?  At least the GNU assembler does not
7516      need the source filename more than once in the file, beyond what is
7517      emitted by the debug information.  */
7518   if (!TARGET_GAS)
7519     ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
7520
7521 #ifdef SDB_DEBUGGING_INFO
7522   if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
7523     ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
7524 #endif
7525
7526   /* In mips16 mode, we may need to generate a 32 bit to handle
7527      floating point arguments.  The linker will arrange for any 32 bit
7528      functions to call this stub, which will then jump to the 16 bit
7529      function proper.  */
7530   if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
7531       && current_function_args_info.fp_code != 0)
7532     build_mips16_function_stub (file);
7533
7534   inside_function = 1;
7535
7536 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7537   /* Get the function name the same way that toplev.c does before calling
7538      assemble_start_function.  This is needed so that the name used here
7539      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
7540   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7541
7542   if (!flag_inhibit_size_directive)
7543     {
7544       fputs ("\t.ent\t", file);
7545       assemble_name (file, fnname);
7546       fputs ("\n", file);
7547     }
7548
7549   assemble_name (file, fnname);
7550   fputs (":\n", file);
7551 #endif
7552
7553   if (!flag_inhibit_size_directive)
7554     {
7555       /* .frame FRAMEREG, FRAMESIZE, RETREG */
7556       fprintf (file,
7557                "\t.frame\t%s,%ld,%s\t\t# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
7558                (reg_names[(frame_pointer_needed)
7559                           ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
7560                ((frame_pointer_needed && TARGET_MIPS16)
7561                 ? ((long) tsize - current_function_outgoing_args_size)
7562                 : (long) tsize),
7563                reg_names[GP_REG_FIRST + 31],
7564                cfun->machine->frame.var_size,
7565                cfun->machine->frame.num_gp,
7566                cfun->machine->frame.num_fp,
7567                current_function_outgoing_args_size,
7568                cfun->machine->frame.extra_size);
7569
7570       /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
7571       fprintf (file, "\t.mask\t0x%08lx,%ld\n\t.fmask\t0x%08lx,%ld\n",
7572                cfun->machine->frame.mask,
7573                cfun->machine->frame.gp_save_offset,
7574                cfun->machine->frame.fmask,
7575                cfun->machine->frame.fp_save_offset);
7576
7577       /* Require:
7578          OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
7579          HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs.  */
7580     }
7581
7582   if (mips_entry && ! mips_can_use_return_insn ())
7583     {
7584       int save16 = BITSET_P (cfun->machine->frame.mask, 16);
7585       int save17 = BITSET_P (cfun->machine->frame.mask, 17);
7586       int save31 = BITSET_P (cfun->machine->frame.mask, 31);
7587       int savearg = 0;
7588       rtx insn;
7589
7590       /* Look through the initial insns to see if any of them store
7591          the function parameters into the incoming parameter storage
7592          area.  If they do, we delete the insn, and save the register
7593          using the entry pseudo-instruction instead.  We don't try to
7594          look past a label, jump, or call.  */
7595       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
7596         {
7597           rtx note, set, src, dest, base, offset;
7598           int hireg;
7599
7600           if (GET_CODE (insn) == CODE_LABEL
7601               || GET_CODE (insn) == JUMP_INSN
7602               || GET_CODE (insn) == CALL_INSN)
7603             break;
7604           if (GET_CODE (insn) != INSN)
7605             continue;
7606           set = PATTERN (insn);
7607           if (GET_CODE (set) != SET)
7608             continue;
7609
7610           /* An insn storing a function parameter will still have a
7611              REG_EQUIV note on it mentioning the argument pointer.  */
7612           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
7613           if (note == NULL_RTX)
7614             continue;
7615           if (! reg_mentioned_p (arg_pointer_rtx, XEXP (note, 0)))
7616             continue;
7617
7618           src = SET_SRC (set);
7619           if (GET_CODE (src) != REG
7620               || REGNO (src) < GP_REG_FIRST + 4
7621               || REGNO (src) > GP_REG_FIRST + 7)
7622             continue;
7623
7624           dest = SET_DEST (set);
7625           if (GET_CODE (dest) != MEM)
7626             continue;
7627           if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned) UNITS_PER_WORD)
7628             ;
7629           else if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned)2 * UNITS_PER_WORD
7630                    && REGNO (src) < GP_REG_FIRST + 7)
7631             ;
7632           else
7633             continue;
7634           offset = const0_rtx;
7635           base = eliminate_constant_term (XEXP (dest, 0), &offset);
7636           if (GET_CODE (base) != REG
7637               || GET_CODE (offset) != CONST_INT)
7638             continue;
7639           if (REGNO (base) == (unsigned) STACK_POINTER_REGNUM
7640               && INTVAL (offset) == tsize + (REGNO (src) - 4) * UNITS_PER_WORD)
7641             ;
7642           else if (REGNO (base) == (unsigned) HARD_FRAME_POINTER_REGNUM
7643                    && (INTVAL (offset)
7644                        == (tsize
7645                            + (REGNO (src) - 4) * UNITS_PER_WORD
7646                            - current_function_outgoing_args_size)))
7647             ;
7648           else
7649             continue;
7650
7651           /* This insn stores a parameter onto the stack, in the same
7652              location where the entry pseudo-instruction will put it.
7653              Delete the insn, and arrange to tell the entry
7654              instruction to save the register.  */
7655           PUT_CODE (insn, NOTE);
7656           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7657           NOTE_SOURCE_FILE (insn) = 0;
7658
7659           hireg = (REGNO (src)
7660                    + HARD_REGNO_NREGS (REGNO (src), GET_MODE (dest))
7661                    - 1);
7662           if (hireg > savearg)
7663             savearg = hireg;
7664         }
7665
7666       /* If this is a varargs function, we need to save all the
7667          registers onto the stack anyhow.  */
7668       if (current_function_stdarg)
7669         savearg = GP_REG_FIRST + 7;
7670
7671       fprintf (file, "\tentry\t");
7672       if (savearg > 0)
7673         {
7674           if (savearg == GP_REG_FIRST + 4)
7675             fprintf (file, "%s", reg_names[savearg]);
7676           else
7677             fprintf (file, "%s-%s", reg_names[GP_REG_FIRST + 4],
7678                      reg_names[savearg]);
7679         }
7680       if (save16 || save17)
7681         {
7682           if (savearg > 0)
7683             fprintf (file, ",");
7684           fprintf (file, "%s", reg_names[GP_REG_FIRST + 16]);
7685           if (save17)
7686             fprintf (file, "-%s", reg_names[GP_REG_FIRST + 17]);
7687         }
7688       if (save31)
7689         {
7690           if (savearg > 0 || save16 || save17)
7691             fprintf (file, ",");
7692           fprintf (file, "%s", reg_names[GP_REG_FIRST + 31]);
7693         }
7694       fprintf (file, "\n");
7695     }
7696
7697   if (TARGET_ABICALLS && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7698     {
7699       const char *const sp_str = reg_names[STACK_POINTER_REGNUM];
7700
7701       fprintf (file, "\t.set\tnoreorder\n\t.cpload\t%s\n\t.set\treorder\n",
7702                reg_names[PIC_FUNCTION_ADDR_REGNUM]);
7703       if (tsize > 0)
7704         {
7705           fprintf (file, "\t%s\t%s,%s,%ld\n",
7706                    (Pmode == DImode ? "dsubu" : "subu"),
7707                    sp_str, sp_str, (long) tsize);
7708           fprintf (file, "\t.cprestore %ld\n", cfun->machine->frame.args_size);
7709         }
7710
7711       if (dwarf2out_do_frame ())
7712         dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, tsize);
7713     }
7714 }
7715 \f
7716 /* Expand the prologue into a bunch of separate insns.  */
7717
7718 void
7719 mips_expand_prologue ()
7720 {
7721   int regno;
7722   HOST_WIDE_INT tsize;
7723   rtx tmp_rtx = 0;
7724   int last_arg_is_vararg_marker = 0;
7725   tree fndecl = current_function_decl;
7726   tree fntype = TREE_TYPE (fndecl);
7727   tree fnargs = DECL_ARGUMENTS (fndecl);
7728   rtx next_arg_reg;
7729   int i;
7730   tree next_arg;
7731   tree cur_arg;
7732   CUMULATIVE_ARGS args_so_far;
7733   rtx reg_18_save = NULL_RTX;
7734   int store_args_on_stack = (mips_abi == ABI_32 || mips_abi == ABI_O64)
7735                             && (! mips_entry || mips_can_use_return_insn ());
7736
7737   /* If struct value address is treated as the first argument, make it so.  */
7738   if (aggregate_value_p (DECL_RESULT (fndecl))
7739       && ! current_function_returns_pcc_struct
7740       && struct_value_incoming_rtx == 0)
7741     {
7742       tree type = build_pointer_type (fntype);
7743       tree function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
7744
7745       DECL_ARG_TYPE (function_result_decl) = type;
7746       TREE_CHAIN (function_result_decl) = fnargs;
7747       fnargs = function_result_decl;
7748     }
7749
7750   /* For arguments passed in registers, find the register number
7751      of the first argument in the variable part of the argument list,
7752      otherwise GP_ARG_LAST+1.  Note also if the last argument is
7753      the varargs special argument, and treat it as part of the
7754      variable arguments.
7755
7756      This is only needed if store_args_on_stack is true.  */
7757
7758   INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, current_function_decl);
7759   regno = GP_ARG_FIRST;
7760
7761   for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
7762     {
7763       tree passed_type = DECL_ARG_TYPE (cur_arg);
7764       enum machine_mode passed_mode = TYPE_MODE (passed_type);
7765       rtx entry_parm;
7766
7767       if (TREE_ADDRESSABLE (passed_type))
7768         {
7769           passed_type = build_pointer_type (passed_type);
7770           passed_mode = Pmode;
7771         }
7772
7773       entry_parm = FUNCTION_ARG (args_so_far, passed_mode, passed_type, 1);
7774
7775       FUNCTION_ARG_ADVANCE (args_so_far, passed_mode, passed_type, 1);
7776       next_arg = TREE_CHAIN (cur_arg);
7777
7778       if (entry_parm && store_args_on_stack)
7779         {
7780           if (next_arg == 0
7781               && DECL_NAME (cur_arg)
7782               && ((0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7783                                 "__builtin_va_alist"))
7784                   || (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7785                                    "va_alist"))))
7786             {
7787               last_arg_is_vararg_marker = 1;
7788               if (GET_CODE (entry_parm) == REG)
7789                 regno = REGNO (entry_parm);
7790               else
7791                 regno = GP_ARG_LAST + 1;
7792               break;
7793             }
7794           else
7795             {
7796               int words;
7797
7798               if (GET_CODE (entry_parm) != REG)
7799                 abort ();
7800
7801               /* passed in a register, so will get homed automatically */
7802               if (GET_MODE (entry_parm) == BLKmode)
7803                 words = (int_size_in_bytes (passed_type) + 3) / 4;
7804               else
7805                 words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
7806
7807               regno = REGNO (entry_parm) + words - 1;
7808             }
7809         }
7810       else
7811         {
7812           regno = GP_ARG_LAST+1;
7813           break;
7814         }
7815     }
7816
7817   /* In order to pass small structures by value in registers compatibly with
7818      the MIPS compiler, we need to shift the value into the high part of the
7819      register.  Function_arg has encoded a PARALLEL rtx, holding a vector of
7820      adjustments to be made as the next_arg_reg variable, so we split up the
7821      insns, and emit them separately.  */
7822
7823   next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1);
7824   if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
7825     {
7826       rtvec adjust = XVEC (next_arg_reg, 0);
7827       int num = GET_NUM_ELEM (adjust);
7828
7829       for (i = 0; i < num; i++)
7830         {
7831           rtx insn, pattern;
7832
7833           pattern = RTVEC_ELT (adjust, i);
7834           if (GET_CODE (pattern) != SET
7835               || GET_CODE (SET_SRC (pattern)) != ASHIFT)
7836             abort_with_insn (pattern, "insn is not a shift");
7837           PUT_CODE (SET_SRC (pattern), ASHIFTRT);
7838
7839           insn = emit_insn (pattern);
7840
7841           /* Global life information isn't valid at this point, so we
7842              can't check whether these shifts are actually used.  Mark
7843              them MAYBE_DEAD so that flow2 will remove them, and not
7844              complain about dead code in the prologue.  */
7845           REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
7846                                                REG_NOTES (insn));
7847         }
7848     }
7849
7850   tsize = compute_frame_size (get_frame_size ());
7851
7852   /* If this function is a varargs function, store any registers that
7853      would normally hold arguments ($4 - $7) on the stack.  */
7854   if (store_args_on_stack
7855       && ((TYPE_ARG_TYPES (fntype) != 0
7856            && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
7857                != void_type_node))
7858           || last_arg_is_vararg_marker))
7859     {
7860       int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
7861       rtx ptr = stack_pointer_rtx;
7862
7863       /* If we are doing svr4-abi, sp has already been decremented by tsize.  */
7864       if (TARGET_ABICALLS)
7865         offset += tsize;
7866
7867       for (; regno <= GP_ARG_LAST; regno++)
7868         {
7869           if (offset != 0)
7870             ptr = gen_rtx (PLUS, Pmode, stack_pointer_rtx, GEN_INT (offset));
7871           emit_move_insn (gen_rtx (MEM, gpr_mode, ptr),
7872                           gen_rtx (REG, gpr_mode, regno));
7873
7874           offset += GET_MODE_SIZE (gpr_mode);
7875         }
7876     }
7877
7878   /* If we are using the entry pseudo instruction, it will
7879      automatically subtract 32 from the stack pointer, so we don't
7880      need to.  The entry pseudo instruction is emitted by
7881      function_prologue.  */
7882   if (mips_entry && ! mips_can_use_return_insn ())
7883     {
7884       if (tsize > 0 && tsize <= 32 && frame_pointer_needed)
7885         {
7886           rtx insn;
7887
7888           /* If we are using a frame pointer with a small stack frame,
7889              we need to initialize it here since it won't be done
7890              below.  */
7891           if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7892             {
7893               rtx incr = GEN_INT (current_function_outgoing_args_size);
7894               if (Pmode == DImode)
7895                 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7896                                               stack_pointer_rtx,
7897                                               incr));
7898               else
7899                 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7900                                               stack_pointer_rtx,
7901                                               incr));
7902             }
7903           else if (Pmode == DImode)
7904             insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7905                                          stack_pointer_rtx));
7906           else
7907             insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7908                                          stack_pointer_rtx));
7909
7910           RTX_FRAME_RELATED_P (insn) = 1;
7911         }
7912
7913       /* We may need to save $18, if it is used to call a function
7914          which may return a floating point value.  Set up a sequence
7915          of instructions to do so.  Later on we emit them at the right
7916          moment.  */
7917       if (TARGET_MIPS16 && BITSET_P (cfun->machine->frame.mask, 18))
7918         {
7919           rtx reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 3);
7920           long gp_offset, base_offset;
7921
7922           gp_offset = cfun->machine->frame.gp_sp_offset;
7923           if (BITSET_P (cfun->machine->frame.mask, 16))
7924             gp_offset -= UNITS_PER_WORD;
7925           if (BITSET_P (cfun->machine->frame.mask, 17))
7926             gp_offset -= UNITS_PER_WORD;
7927           if (BITSET_P (cfun->machine->frame.mask, 31))
7928             gp_offset -= UNITS_PER_WORD;
7929           if (tsize > 32767)
7930             base_offset = tsize;
7931           else
7932             base_offset = 0;
7933           start_sequence ();
7934           emit_move_insn (reg_rtx,
7935                           gen_rtx (REG, gpr_mode, GP_REG_FIRST + 18));
7936           emit_move_insn (gen_rtx (MEM, gpr_mode,
7937                                    gen_rtx (PLUS, Pmode, stack_pointer_rtx,
7938                                             GEN_INT (gp_offset
7939                                                      - base_offset))),
7940                           reg_rtx);
7941           reg_18_save = get_insns ();
7942           end_sequence ();
7943         }
7944
7945       if (tsize > 32)
7946         tsize -= 32;
7947       else
7948         {
7949           tsize = 0;
7950           if (reg_18_save != NULL_RTX)
7951             emit_insn (reg_18_save);
7952         }
7953     }
7954
7955   if (tsize > 0)
7956     {
7957       rtx tsize_rtx = GEN_INT (tsize);
7958
7959       /* If we are doing svr4-abi, sp move is done by
7960          function_prologue.  In mips16 mode with a large frame, we
7961          save the registers before adjusting the stack.  */
7962       if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7963           && (!TARGET_MIPS16 || tsize <= 32767))
7964         {
7965           rtx adjustment_rtx, insn, dwarf_pattern;
7966
7967           if (tsize > 32767)
7968             {
7969               adjustment_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
7970               emit_move_insn (adjustment_rtx, tsize_rtx);
7971             }
7972           else
7973             adjustment_rtx = tsize_rtx;
7974
7975           if (Pmode == DImode)
7976             insn = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
7977                                           adjustment_rtx));
7978           else
7979             insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
7980                                           adjustment_rtx));
7981
7982           dwarf_pattern = gen_rtx_SET (Pmode, stack_pointer_rtx,
7983                                        plus_constant (stack_pointer_rtx,
7984                                                       -tsize));
7985
7986           mips_annotate_frame_insn (insn, dwarf_pattern);
7987         }
7988
7989       if (! mips_entry)
7990         save_restore_insns (1, tmp_rtx, tsize);
7991       else if (reg_18_save != NULL_RTX)
7992         emit_insn (reg_18_save);
7993
7994       if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7995           && TARGET_MIPS16
7996           && tsize > 32767)
7997         {
7998           rtx reg_rtx;
7999
8000           if (!frame_pointer_needed)
8001             abort ();
8002
8003           reg_rtx = gen_rtx (REG, Pmode, 3);
8004           emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
8005           emit_move_insn (reg_rtx, tsize_rtx);
8006           if (Pmode == DImode)
8007             emit_insn (gen_subdi3 (hard_frame_pointer_rtx,
8008                                    hard_frame_pointer_rtx,
8009                                    reg_rtx));
8010           else
8011             emit_insn (gen_subsi3 (hard_frame_pointer_rtx,
8012                                    hard_frame_pointer_rtx,
8013                                    reg_rtx));
8014           emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
8015         }
8016
8017       if (frame_pointer_needed)
8018         {
8019           rtx insn = 0;
8020
8021           /* On the mips16, we encourage the use of unextended
8022              instructions when using the frame pointer by pointing the
8023              frame pointer ahead of the argument space allocated on
8024              the stack.  */
8025           if ((! TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
8026               && TARGET_MIPS16
8027               && tsize > 32767)
8028             {
8029               /* In this case, we have already copied the stack
8030                  pointer into the frame pointer, above.  We need only
8031                  adjust for the outgoing argument size.  */
8032               if (current_function_outgoing_args_size != 0)
8033                 {
8034                   rtx incr = GEN_INT (current_function_outgoing_args_size);
8035                   if (Pmode == DImode)
8036                     insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8037                                                   hard_frame_pointer_rtx,
8038                                                   incr));
8039                   else
8040                     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8041                                                   hard_frame_pointer_rtx,
8042                                                   incr));
8043                 }
8044             }
8045           else if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
8046             {
8047               rtx incr = GEN_INT (current_function_outgoing_args_size);
8048               if (Pmode == DImode)
8049                 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8050                                               stack_pointer_rtx,
8051                                               incr));
8052               else
8053                 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8054                                               stack_pointer_rtx,
8055                                               incr));
8056             }
8057           else if (Pmode == DImode)
8058             insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
8059                                          stack_pointer_rtx));
8060           else
8061             insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
8062                                          stack_pointer_rtx));
8063
8064           if (insn)
8065             RTX_FRAME_RELATED_P (insn) = 1;
8066         }
8067
8068       if (TARGET_ABICALLS && (mips_abi != ABI_32 && mips_abi != ABI_O64))
8069         emit_insn (gen_loadgp (XEXP (DECL_RTL (current_function_decl), 0),
8070                                gen_rtx_REG (DImode, 25)));
8071     }
8072
8073   /* If we are profiling, make sure no instructions are scheduled before
8074      the call to mcount.  */
8075
8076   if (current_function_profile)
8077     emit_insn (gen_blockage ());
8078 }
8079 \f
8080 /* Do any necessary cleanup after a function to restore stack, frame,
8081    and regs.  */
8082
8083 #define RA_MASK BITMASK_HIGH    /* 1 << 31 */
8084 #define PIC_OFFSET_TABLE_MASK (1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
8085
8086 static void
8087 mips_output_function_epilogue (file, size)
8088      FILE *file ATTRIBUTE_UNUSED;
8089      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
8090 {
8091   const char *fnname = "";      /* FIXME: Correct initialisation?  */
8092   rtx string;
8093
8094 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8095   /* Get the function name the same way that toplev.c does before calling
8096      assemble_start_function.  This is needed so that the name used here
8097      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
8098   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
8099
8100   if (!flag_inhibit_size_directive)
8101     {
8102       fputs ("\t.end\t", file);
8103       assemble_name (file, fnname);
8104       fputs ("\n", file);
8105     }
8106 #endif
8107
8108   if (TARGET_STATS)
8109     {
8110       int num_gp_regs = cfun->machine->frame.gp_reg_size / 4;
8111       int num_fp_regs = cfun->machine->frame.fp_reg_size / 8;
8112       int num_regs = num_gp_regs + num_fp_regs;
8113       const char *name = fnname;
8114
8115       if (name[0] == '*')
8116         name++;
8117
8118       dslots_load_total += num_regs;
8119
8120       fprintf (stderr,
8121                "%-20s fp=%c leaf=%c alloca=%c setjmp=%c stack=%4ld arg=%3d reg=%2d/%d delay=%3d/%3dL %3d/%3dJ refs=%3d/%3d/%3d",
8122                name, frame_pointer_needed ? 'y' : 'n',
8123                (cfun->machine->frame.mask & RA_MASK) != 0 ? 'n' : 'y',
8124                current_function_calls_alloca ? 'y' : 'n',
8125                current_function_calls_setjmp ? 'y' : 'n',
8126                cfun->machine->frame.total_size,
8127                current_function_outgoing_args_size, num_gp_regs, num_fp_regs,
8128                dslots_load_total, dslots_load_filled,
8129                dslots_jump_total, dslots_jump_filled,
8130                num_refs[0], num_refs[1], num_refs[2]);
8131
8132       fputc ('\n', stderr);
8133     }
8134
8135   /* Reset state info for each function.  */
8136   inside_function = 0;
8137   ignore_line_number = 0;
8138   dslots_load_total = 0;
8139   dslots_jump_total = 0;
8140   dslots_load_filled = 0;
8141   dslots_jump_filled = 0;
8142   num_refs[0] = 0;
8143   num_refs[1] = 0;
8144   num_refs[2] = 0;
8145   mips_load_reg = 0;
8146   mips_load_reg2 = 0;
8147
8148   while (string_constants != NULL)
8149     {
8150       struct string_constant *next;
8151
8152       next = string_constants->next;
8153       free (string_constants);
8154       string_constants = next;
8155     }
8156
8157   /* If any following function uses the same strings as this one, force
8158      them to refer those strings indirectly.  Nearby functions could
8159      refer them using pc-relative addressing, but it isn't safe in
8160      general.  For instance, some functions may be placed in sections
8161      other than .text, and we don't know whether they be close enough
8162      to this one.  In large files, even other .text functions can be
8163      too far away.  */
8164   for (string = mips16_strings; string != 0; string = XEXP (string, 1))
8165     SYMBOL_REF_FLAG (XEXP (string, 0)) = 0;
8166   free_EXPR_LIST_list (&mips16_strings);
8167
8168   /* Restore the output file if optimizing the GP (optimizing the GP causes
8169      the text to be diverted to a tempfile, so that data decls come before
8170      references to the data).  */
8171   if (TARGET_FILE_SWITCHING)
8172     {
8173       asm_out_file = asm_out_data_file;
8174       data_section ();
8175     }
8176 }
8177 \f
8178 /* Expand the epilogue into a bunch of separate insns.  */
8179
8180 void
8181 mips_expand_epilogue ()
8182 {
8183   HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
8184   rtx tsize_rtx = GEN_INT (tsize);
8185   rtx tmp_rtx = (rtx)0;
8186
8187   if (mips_can_use_return_insn ())
8188     {
8189       emit_jump_insn (gen_return ());
8190       return;
8191     }
8192
8193   if (mips_entry && ! mips_can_use_return_insn ())
8194     tsize -= 32;
8195
8196   if (tsize > 32767 && ! TARGET_MIPS16)
8197     {
8198       tmp_rtx = gen_rtx_REG (Pmode, MIPS_TEMP1_REGNUM);
8199       emit_move_insn (tmp_rtx, tsize_rtx);
8200       tsize_rtx = tmp_rtx;
8201     }
8202
8203   if (tsize > 0)
8204     {
8205       long orig_tsize = tsize;
8206
8207       if (frame_pointer_needed)
8208         {
8209           emit_insn (gen_blockage ());
8210
8211           /* On the mips16, the frame pointer is offset from the stack
8212              pointer by current_function_outgoing_args_size.  We
8213              account for that by changing tsize.  Note that this can
8214              actually make tsize negative.  */
8215           if (TARGET_MIPS16)
8216             {
8217               tsize -= current_function_outgoing_args_size;
8218
8219               /* If we have a large frame, it's easier to add to $6
8220                  than to $sp, since the mips16 has no instruction to
8221                  add a register to $sp.  */
8222               if (orig_tsize > 32767)
8223                 {
8224                   rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
8225
8226                   emit_move_insn (g6_rtx, GEN_INT (tsize));
8227                   if (Pmode == DImode)
8228                     emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8229                                            hard_frame_pointer_rtx,
8230                                            g6_rtx));
8231                   else
8232                     emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8233                                            hard_frame_pointer_rtx,
8234                                            g6_rtx));
8235                   tsize = 0;
8236                 }
8237
8238               if (tsize && tsize != orig_tsize)
8239                 tsize_rtx = GEN_INT (tsize);
8240             }
8241
8242           if (Pmode == DImode)
8243             emit_insn (gen_movdi (stack_pointer_rtx, hard_frame_pointer_rtx));
8244           else
8245             emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
8246         }
8247
8248       /* The GP/PIC register is implicitly used by all SYMBOL_REFs, so if we
8249          are going to restore it, then we must emit a blockage insn to
8250          prevent the scheduler from moving the restore out of the epilogue.  */
8251       else if (TARGET_ABICALLS && mips_abi != ABI_32 && mips_abi != ABI_O64
8252                && (cfun->machine->frame.mask
8253                    & (1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))))
8254         emit_insn (gen_blockage ());
8255
8256       save_restore_insns (0, tmp_rtx, orig_tsize);
8257
8258       /* In mips16 mode with a large frame, we adjust the stack
8259          pointer before restoring the registers.  In this case, we
8260          should always be using a frame pointer, so everything should
8261          have been handled above.  */
8262       if (tsize > 32767 && TARGET_MIPS16)
8263         abort ();
8264
8265       if (current_function_calls_eh_return)
8266         {
8267           rtx eh_ofs = EH_RETURN_STACKADJ_RTX;
8268           if (Pmode == DImode)
8269             emit_insn (gen_adddi3 (eh_ofs, eh_ofs, tsize_rtx));
8270           else
8271             emit_insn (gen_addsi3 (eh_ofs, eh_ofs, tsize_rtx));
8272           tsize_rtx = eh_ofs;
8273         }
8274
8275       emit_insn (gen_blockage ());
8276
8277       if (tsize != 0 || current_function_calls_eh_return)
8278         {
8279           if (!TARGET_MIPS16)
8280             {
8281               if (Pmode == DImode)
8282                 emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
8283                                        tsize_rtx));
8284               else
8285                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8286                                        tsize_rtx));
8287             }
8288           else
8289             {
8290               /* We need to work around not being able to add a register
8291                  to the stack pointer directly. Use register $6 as an
8292                  intermediate step.  */
8293
8294               rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
8295
8296               if (Pmode == DImode)
8297                 {
8298                   emit_insn (gen_movdi (g6_rtx, stack_pointer_rtx));
8299                   emit_insn (gen_adddi3 (g6_rtx, g6_rtx, tsize_rtx));
8300                   emit_insn (gen_movdi (stack_pointer_rtx, g6_rtx));
8301                 }
8302               else
8303                 {
8304                   emit_insn (gen_movsi (g6_rtx, stack_pointer_rtx));
8305                   emit_insn (gen_addsi3 (g6_rtx, g6_rtx, tsize_rtx));
8306                   emit_insn (gen_movsi (stack_pointer_rtx, g6_rtx));
8307                 }
8308             }
8309
8310         }
8311     }
8312
8313   /* The mips16 loads the return address into $7, not $31.  */
8314   if (TARGET_MIPS16 && (cfun->machine->frame.mask & RA_MASK) != 0)
8315     emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
8316                                                   GP_REG_FIRST + 7)));
8317   else
8318     emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
8319                                                   GP_REG_FIRST + 31)));
8320 }
8321 \f
8322 /* Return nonzero if this function is known to have a null epilogue.
8323    This allows the optimizer to omit jumps to jumps if no stack
8324    was created.  */
8325
8326 int
8327 mips_can_use_return_insn ()
8328 {
8329   tree return_type;
8330
8331   if (! reload_completed)
8332     return 0;
8333
8334   if (regs_ever_live[31] || current_function_profile)
8335     return 0;
8336
8337   return_type = DECL_RESULT (current_function_decl);
8338
8339   /* In mips16 mode, a function which returns a floating point value
8340      needs to arrange to copy the return value into the floating point
8341      registers.  */
8342   if (TARGET_MIPS16
8343       && mips16_hard_float
8344       && ! aggregate_value_p (return_type)
8345       && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
8346       && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
8347     return 0;
8348
8349   if (cfun->machine->frame.initialized)
8350     return cfun->machine->frame.total_size == 0;
8351
8352   return compute_frame_size (get_frame_size ()) == 0;
8353 }
8354 \f
8355 /* Returns nonzero if X contains a SYMBOL_REF.  */
8356
8357 static int
8358 symbolic_expression_p (x)
8359      rtx x;
8360 {
8361   if (GET_CODE (x) == SYMBOL_REF)
8362     return 1;
8363
8364   if (GET_CODE (x) == CONST)
8365     return symbolic_expression_p (XEXP (x, 0));
8366
8367   if (GET_RTX_CLASS (GET_CODE (x)) == '1')
8368     return symbolic_expression_p (XEXP (x, 0));
8369
8370   if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
8371       || GET_RTX_CLASS (GET_CODE (x)) == '2')
8372     return (symbolic_expression_p (XEXP (x, 0))
8373             || symbolic_expression_p (XEXP (x, 1)));
8374
8375   return 0;
8376 }
8377
8378 /* Choose the section to use for the constant rtx expression X that has
8379    mode MODE.  */
8380
8381 static void
8382 mips_select_rtx_section (mode, x, align)
8383      enum machine_mode mode;
8384      rtx x;
8385      unsigned HOST_WIDE_INT align;
8386 {
8387   if (TARGET_MIPS16)
8388     {
8389       /* In mips16 mode, the constant table always goes in the same section
8390          as the function, so that constants can be loaded using PC relative
8391          addressing.  */
8392       function_section (current_function_decl);
8393     }
8394   else if (TARGET_EMBEDDED_DATA)
8395     {
8396       /* For embedded applications, always put constants in read-only data,
8397          in order to reduce RAM usage.  */
8398       mergeable_constant_section (mode, align, 0);
8399     }
8400   else
8401     {
8402       /* For hosted applications, always put constants in small data if
8403          possible, as this gives the best performance.  */
8404       /* ??? Consider using mergable small data sections.  */
8405
8406       if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
8407           && mips_section_threshold > 0)
8408         SMALL_DATA_SECTION ();
8409       else if (flag_pic && symbolic_expression_p (x))
8410         {
8411           if (targetm.have_named_sections)
8412             named_section (NULL_TREE, ".data.rel.ro", 3);
8413           else
8414             data_section ();
8415         }
8416       else
8417         mergeable_constant_section (mode, align, 0);
8418     }
8419 }
8420
8421 /* Choose the section to use for DECL.  RELOC is true if its value contains
8422    any relocatable expression.
8423
8424    Some of the logic used here needs to be replicated in
8425    mips_encode_section_info so that references to these symbols are
8426    done correctly.  Specifically, at least all symbols assigned here
8427    to rom (.text and/or .rodata) must not be referenced via
8428    mips_encode_section_info with %gprel, as the rom might be too far
8429    away.
8430
8431    If you need to make a change here, you probably should check
8432    mips_encode_section_info to see if it needs a similar change.
8433
8434    ??? This would be fixed by implementing targetm.is_small_data_p.  */
8435
8436 static void
8437 mips_select_section (decl, reloc, align)
8438      tree decl;
8439      int reloc;
8440      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
8441 {
8442   int size = int_size_in_bytes (TREE_TYPE (decl));
8443
8444   if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
8445       && TREE_CODE (decl) == STRING_CST
8446       && !flag_writable_strings)
8447     /* For embedded position independent code, put constant strings in the
8448        text section, because the data section is limited to 64K in size.
8449        For mips16 code, put strings in the text section so that a PC
8450        relative load instruction can be used to get their address.  */
8451     text_section ();
8452   else if (TARGET_EMBEDDED_DATA)
8453     {
8454       /* For embedded applications, always put an object in read-only data
8455          if possible, in order to reduce RAM usage.  */
8456
8457       if (((TREE_CODE (decl) == VAR_DECL
8458             && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
8459             && DECL_INITIAL (decl)
8460             && (DECL_INITIAL (decl) == error_mark_node
8461                 || TREE_CONSTANT (DECL_INITIAL (decl))))
8462            /* Deal with calls from output_constant_def_contents.  */
8463            || (TREE_CODE (decl) != VAR_DECL
8464                && (TREE_CODE (decl) != STRING_CST
8465                    || !flag_writable_strings)))
8466           && ! (flag_pic && reloc))
8467         readonly_data_section ();
8468       else if (size > 0 && size <= mips_section_threshold)
8469         SMALL_DATA_SECTION ();
8470       else
8471         data_section ();
8472     }
8473   else
8474     {
8475       /* For hosted applications, always put an object in small data if
8476          possible, as this gives the best performance.  */
8477
8478       if (size > 0 && size <= mips_section_threshold)
8479         SMALL_DATA_SECTION ();
8480       else if (((TREE_CODE (decl) == VAR_DECL
8481                  && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
8482                  && DECL_INITIAL (decl)
8483                  && (DECL_INITIAL (decl) == error_mark_node
8484                      || TREE_CONSTANT (DECL_INITIAL (decl))))
8485                 /* Deal with calls from output_constant_def_contents.  */
8486                 || (TREE_CODE (decl) != VAR_DECL
8487                     && (TREE_CODE (decl) != STRING_CST
8488                         || !flag_writable_strings)))
8489                && ! (flag_pic && reloc))
8490         readonly_data_section ();
8491       else
8492         data_section ();
8493     }
8494 }
8495
8496 /* When optimizing for the $gp pointer, SYMBOL_REF_FLAG is set for all
8497    small objects.
8498
8499    When generating embedded PIC code, SYMBOL_REF_FLAG is set for
8500    symbols which are not in the .text section.
8501
8502    When generating mips16 code, SYMBOL_REF_FLAG is set for string
8503    constants which are put in the .text section.  We also record the
8504    total length of all such strings; this total is used to decide
8505    whether we need to split the constant table, and need not be
8506    precisely correct.
8507
8508    When not mips16 code nor embedded PIC, if a symbol is in a
8509    gp addressable section, SYMBOL_REF_FLAG is set prevent gcc from
8510    splitting the reference so that gas can generate a gp relative
8511    reference.
8512
8513    When TARGET_EMBEDDED_DATA is set, we assume that all const
8514    variables will be stored in ROM, which is too far from %gp to use
8515    %gprel addressing.  Note that (1) we include "extern const"
8516    variables in this, which mips_select_section doesn't, and (2) we
8517    can't always tell if they're really const (they might be const C++
8518    objects with non-const constructors), so we err on the side of
8519    caution and won't use %gprel anyway (otherwise we'd have to defer
8520    this decision to the linker/loader).  The handling of extern consts
8521    is why the DECL_INITIAL macros differ from mips_select_section.  */
8522
8523 static void
8524 mips_encode_section_info (decl, first)
8525      tree decl;
8526      int first;
8527 {
8528   if (TARGET_MIPS16)
8529     {
8530       if (first && TREE_CODE (decl) == STRING_CST
8531           && ! flag_writable_strings
8532           /* If this string is from a function, and the function will
8533              go in a gnu linkonce section, then we can't directly
8534              access the string.  This gets an assembler error
8535              "unsupported PC relative reference to different section".
8536              If we modify SELECT_SECTION to put it in function_section
8537              instead of text_section, it still fails because
8538              DECL_SECTION_NAME isn't set until assemble_start_function.
8539              If we fix that, it still fails because strings are shared
8540              among multiple functions, and we have cross section
8541              references again.  We force it to work by putting string
8542              addresses in the constant pool and indirecting.  */
8543           && (! current_function_decl
8544               || ! DECL_ONE_ONLY (current_function_decl)))
8545         {
8546           rtx symref;
8547
8548           symref = XEXP (TREE_CST_RTL (decl), 0);
8549           mips16_strings = alloc_EXPR_LIST (0, symref, mips16_strings);
8550           SYMBOL_REF_FLAG (symref) = 1;
8551           mips_string_length += TREE_STRING_LENGTH (decl);
8552         }
8553     }
8554
8555   if (TARGET_EMBEDDED_DATA
8556       && (TREE_CODE (decl) == VAR_DECL
8557           && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl))
8558       && (!DECL_INITIAL (decl)
8559           || TREE_CONSTANT (DECL_INITIAL (decl))))
8560     {
8561       SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 0;
8562     }
8563
8564   else if (TARGET_EMBEDDED_PIC)
8565     {
8566       if (TREE_CODE (decl) == VAR_DECL)
8567         SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8568       else if (TREE_CODE (decl) == FUNCTION_DECL)
8569         SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 0;
8570       else if (TREE_CODE (decl) == STRING_CST
8571                && ! flag_writable_strings)
8572         SYMBOL_REF_FLAG (XEXP (TREE_CST_RTL (decl), 0)) = 0;
8573       else
8574         SYMBOL_REF_FLAG (XEXP (TREE_CST_RTL (decl), 0)) = 1;
8575     }
8576
8577   else if (TREE_CODE (decl) == VAR_DECL
8578            && DECL_SECTION_NAME (decl) != NULL_TREE
8579            && (0 == strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
8580                             ".sdata")
8581                || 0 == strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
8582                                ".sbss")))
8583     {
8584       SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8585     }
8586
8587   /* We can not perform GP optimizations on variables which are in
8588        specific sections, except for .sdata and .sbss which are
8589        handled above.  */
8590   else if (TARGET_GP_OPT && TREE_CODE (decl) == VAR_DECL
8591            && DECL_SECTION_NAME (decl) == NULL_TREE
8592            && ! (TARGET_MIPS16 && TREE_PUBLIC (decl)
8593                  && (DECL_COMMON (decl)
8594                      || DECL_ONE_ONLY (decl)
8595                      || DECL_WEAK (decl))))
8596     {
8597       int size = int_size_in_bytes (TREE_TYPE (decl));
8598
8599       if (size > 0 && size <= mips_section_threshold)
8600         SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8601     }
8602
8603 }
8604 \f
8605 /* Return register to use for a function return value with VALTYPE for
8606    function FUNC.  MODE is used instead of VALTYPE for LIBCALLs.  */
8607
8608 rtx
8609 mips_function_value (valtype, func, mode)
8610      tree valtype;
8611      tree func ATTRIBUTE_UNUSED;
8612      enum machine_mode mode;
8613 {
8614   int reg = GP_RETURN;
8615   enum mode_class mclass;
8616   int unsignedp = 1;
8617
8618   if (valtype)
8619     {
8620       mode = TYPE_MODE (valtype);
8621       unsignedp = TREE_UNSIGNED (valtype);
8622
8623       /* Since we define PROMOTE_FUNCTION_RETURN, we must promote
8624          the mode just as PROMOTE_MODE does.  */
8625       mode = promote_mode (valtype, mode, &unsignedp, 1);
8626     }
8627   mclass = GET_MODE_CLASS (mode);
8628
8629   if (mclass == MODE_FLOAT && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
8630     reg = FP_RETURN;
8631
8632   else if (mclass == MODE_FLOAT && mode == TFmode)
8633     /* long doubles are really split between f0 and f2, not f1.  Eek.
8634        Use DImode for each component, since GCC wants integer modes
8635        for subregs.  */
8636     return gen_rtx_PARALLEL
8637       (VOIDmode,
8638        gen_rtvec (2,
8639                   gen_rtx_EXPR_LIST (VOIDmode,
8640                                      gen_rtx_REG (DImode, FP_RETURN),
8641                                      GEN_INT (0)),
8642                   gen_rtx_EXPR_LIST (VOIDmode,
8643                                      gen_rtx_REG (DImode, FP_RETURN + 2),
8644                                      GEN_INT (GET_MODE_SIZE (mode) / 2))));
8645        
8646
8647   else if (mclass == MODE_COMPLEX_FLOAT
8648            && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)
8649     {
8650       enum machine_mode cmode = GET_MODE_INNER (mode);
8651
8652       return gen_rtx_PARALLEL
8653         (VOIDmode,
8654          gen_rtvec (2,
8655                     gen_rtx_EXPR_LIST (VOIDmode,
8656                                        gen_rtx_REG (cmode, FP_RETURN),
8657                                        GEN_INT (0)),
8658                     gen_rtx_EXPR_LIST (VOIDmode,
8659                                        gen_rtx_REG (cmode, FP_RETURN + FP_INC),
8660                                        GEN_INT (GET_MODE_SIZE (cmode)))));
8661     }
8662
8663   else if (valtype && TREE_CODE (valtype) == RECORD_TYPE
8664            && mips_abi != ABI_32
8665            && mips_abi != ABI_O64
8666            && mips_abi != ABI_EABI)
8667     {
8668       /* A struct with only one or two floating point fields is returned in
8669          the floating point registers.  */
8670       tree field, fields[2];
8671       int i;
8672
8673       for (i = 0, field = TYPE_FIELDS (valtype); field;
8674            field = TREE_CHAIN (field))
8675         {
8676           if (TREE_CODE (field) != FIELD_DECL)
8677             continue;
8678
8679           if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE || i >= 2)
8680             break;
8681
8682           fields[i++] = field;
8683         }
8684
8685       /* Must check i, so that we reject structures with no elements.  */
8686       if (! field)
8687         {
8688           if (i == 1)
8689             {
8690               /* The structure has DImode, but we don't allow DImode values
8691                  in FP registers, so we use a PARALLEL even though it isn't
8692                  strictly necessary.  */
8693               enum machine_mode field_mode = TYPE_MODE (TREE_TYPE (fields[0]));
8694
8695               return gen_rtx_PARALLEL
8696                 (mode,
8697                  gen_rtvec (1,
8698                             gen_rtx_EXPR_LIST (VOIDmode,
8699                                                gen_rtx_REG (field_mode,
8700                                                             FP_RETURN),
8701                                                const0_rtx)));
8702             }
8703
8704           else if (i == 2)
8705             {
8706               enum machine_mode first_mode
8707                 = TYPE_MODE (TREE_TYPE (fields[0]));
8708               enum machine_mode second_mode
8709                 = TYPE_MODE (TREE_TYPE (fields[1]));
8710               HOST_WIDE_INT first_offset = int_byte_position (fields[0]);
8711               HOST_WIDE_INT second_offset = int_byte_position (fields[1]);
8712
8713               return gen_rtx_PARALLEL
8714                 (mode,
8715                  gen_rtvec (2,
8716                             gen_rtx_EXPR_LIST (VOIDmode,
8717                                                gen_rtx_REG (first_mode,
8718                                                             FP_RETURN),
8719                                                GEN_INT (first_offset)),
8720                             gen_rtx_EXPR_LIST (VOIDmode,
8721                                                gen_rtx_REG (second_mode,
8722                                                             FP_RETURN + 2),
8723                                                GEN_INT (second_offset))));
8724             }
8725         }
8726     }
8727
8728   return gen_rtx_REG (mode, reg);
8729 }
8730
8731 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE.  Return
8732    nonzero when an argument must be passed by reference.  */
8733
8734 int
8735 function_arg_pass_by_reference (cum, mode, type, named)
8736      const CUMULATIVE_ARGS *cum;
8737      enum machine_mode mode;
8738      tree type;
8739      int named ATTRIBUTE_UNUSED;
8740 {
8741   int size;
8742
8743   if (mips_abi == ABI_32 || mips_abi == ABI_O64)
8744     return 0;
8745
8746   /* We must pass by reference if we would be both passing in registers
8747      and the stack.  This is because any subsequent partial arg would be
8748      handled incorrectly in this case.
8749
8750      ??? This is really a kludge.  We should either fix GCC so that such
8751      a situation causes an abort and then do something in the MIPS port
8752      to prevent it, or add code to function.c to properly handle the case.  */
8753   /* ??? cum can be NULL when called from mips_va_arg.  The problem handled
8754      here hopefully is not relevant to mips_va_arg.  */
8755   if (cum && MUST_PASS_IN_STACK (mode, type)
8756       && mips_abi != ABI_MEABI
8757       && FUNCTION_ARG (*cum, mode, type, named) != 0)
8758     return 1;
8759
8760   /* Otherwise, we only do this if EABI is selected.  */
8761   if (mips_abi != ABI_EABI)
8762     return 0;
8763
8764   /* ??? How should SCmode be handled?  */
8765   if (type == NULL_TREE || mode == DImode || mode == DFmode)
8766     return 0;
8767
8768   size = int_size_in_bytes (type);
8769   return size == -1 || size > UNITS_PER_WORD;
8770 }
8771
8772 /* Return the class of registers for which a mode change from FROM to TO
8773    is invalid.
8774
8775    In little-endian mode, the hi-lo registers are numbered backwards,
8776    so (subreg:SI (reg:DI hi) 0) gets the high word instead of the low
8777    word as intended.
8778
8779    Similarly, when using paired floating-point registers, the first
8780    register holds the low word, regardless of endianness.  So in big
8781    endian mode, (subreg:SI (reg:DF $f0) 0) does not get the high word
8782    as intended.
8783
8784    Also, loading a 32-bit value into a 64-bit floating-point register
8785    will not sign-extend the value, despite what LOAD_EXTEND_OP says.
8786    We can't allow 64-bit float registers to change from a 32-bit
8787    mode to a 64-bit mode.  */
8788
8789 bool
8790 mips_cannot_change_mode_class (from, to, class)
8791      enum machine_mode from, to;
8792      enum reg_class class;
8793 {
8794   if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
8795     {
8796       if (TARGET_BIG_ENDIAN)
8797         return reg_classes_intersect_p (FP_REGS, class);
8798       if (TARGET_FLOAT64)
8799         return reg_classes_intersect_p (HI_AND_FP_REGS, class);
8800       return reg_classes_intersect_p (HI_REG, class);
8801     }
8802   return false;
8803 }
8804
8805 /* This function returns the register class required for a secondary
8806    register when copying between one of the registers in CLASS, and X,
8807    using MODE.  If IN_P is nonzero, the copy is going from X to the
8808    register, otherwise the register is the source.  A return value of
8809    NO_REGS means that no secondary register is required.  */
8810
8811 enum reg_class
8812 mips_secondary_reload_class (class, mode, x, in_p)
8813      enum reg_class class;
8814      enum machine_mode mode;
8815      rtx x;
8816      int in_p;
8817 {
8818   enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
8819   int regno = -1;
8820   int gp_reg_p;
8821
8822   if (GET_CODE (x) == SIGN_EXTEND)
8823     {
8824       int off = 0;
8825
8826       x = XEXP (x, 0);
8827
8828       /* We may be called with reg_renumber NULL from regclass.
8829          ??? This is probably a bug.  */
8830       if (reg_renumber)
8831         regno = true_regnum (x);
8832       else
8833         {
8834           while (GET_CODE (x) == SUBREG)
8835             {
8836               off += subreg_regno_offset (REGNO (SUBREG_REG (x)),
8837                                           GET_MODE (SUBREG_REG (x)),
8838                                           SUBREG_BYTE (x),
8839                                           GET_MODE (x));
8840               x = SUBREG_REG (x);
8841             }
8842
8843           if (GET_CODE (x) == REG)
8844             regno = REGNO (x) + off;
8845         }
8846
8847       /* 64-bit floating-point registers don't store 32-bit values
8848          in sign-extended form.  The only way we can reload
8849          (sign_extend:DI (reg:SI $f0)) is by moving $f0 into
8850          an integer register using a 32-bit move.  */
8851       if (FP_REG_P (regno))
8852         return (class == GR_REGS ? NO_REGS : GR_REGS);
8853
8854       /* For the same reason, we can only reload (sign_extend:DI FOO) into
8855          a floating-point register when FOO is an integer register. */
8856       if (class == FP_REGS)
8857         return (GP_REG_P (regno) ? NO_REGS : GR_REGS);
8858     }
8859
8860   else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
8861     regno = true_regnum (x);
8862
8863   gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
8864
8865   /* We always require a general register when copying anything to
8866      HILO_REGNUM, except when copying an SImode value from HILO_REGNUM
8867      to a general register, or when copying from register 0.  */
8868   if (class == HILO_REG && regno != GP_REG_FIRST + 0)
8869     return ((! in_p
8870              && gp_reg_p
8871              && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8872             ? NO_REGS : gr_regs);
8873   else if (regno == HILO_REGNUM)
8874     return ((in_p
8875              && class == gr_regs
8876              && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8877             ? NO_REGS : gr_regs);
8878
8879   /* Copying from HI or LO to anywhere other than a general register
8880      requires a general register.  */
8881   if (class == HI_REG || class == LO_REG || class == MD_REGS)
8882     {
8883       if (TARGET_MIPS16 && in_p)
8884         {
8885           /* We can't really copy to HI or LO at all in mips16 mode.  */
8886           return M16_REGS;
8887         }
8888       return gp_reg_p ? NO_REGS : gr_regs;
8889     }
8890   if (MD_REG_P (regno))
8891     {
8892       if (TARGET_MIPS16 && ! in_p)
8893         {
8894           /* We can't really copy to HI or LO at all in mips16 mode.  */
8895           return M16_REGS;
8896         }
8897       return class == gr_regs ? NO_REGS : gr_regs;
8898     }
8899
8900   /* We can only copy a value to a condition code register from a
8901      floating point register, and even then we require a scratch
8902      floating point register.  We can only copy a value out of a
8903      condition code register into a general register.  */
8904   if (class == ST_REGS)
8905     {
8906       if (in_p)
8907         return FP_REGS;
8908       return GP_REG_P (regno) ? NO_REGS : GR_REGS;
8909     }
8910   if (ST_REG_P (regno))
8911     {
8912       if (! in_p)
8913         return FP_REGS;
8914       return class == GR_REGS ? NO_REGS : GR_REGS;
8915     }
8916
8917   if (class == FP_REGS)
8918     {
8919       if (GET_CODE (x) == MEM)
8920         {
8921           /* In this case we can use lwc1, swc1, ldc1 or sdc1. */
8922           return NO_REGS;
8923         }
8924       else if (CONSTANT_P (x) && GET_MODE_CLASS (mode) == MODE_FLOAT)
8925         {
8926           /* We can use the l.s and l.d macros to load floating-point
8927              constants.  ??? For l.s, we could probably get better
8928              code by returning GR_REGS here.  */
8929           return NO_REGS;
8930         }
8931       else if (GP_REG_P (regno) || x == CONST0_RTX (mode))
8932         {
8933           /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
8934           return NO_REGS;
8935         }
8936       else if (FP_REG_P (regno))
8937         {
8938           /* In this case we can use mov.s or mov.d.  */
8939           return NO_REGS;
8940         }
8941       else
8942         {
8943           /* Otherwise, we need to reload through an integer register.  */
8944           return GR_REGS;
8945         }
8946     }
8947
8948   /* In mips16 mode, going between memory and anything but M16_REGS
8949      requires an M16_REG.  */
8950   if (TARGET_MIPS16)
8951     {
8952       if (class != M16_REGS && class != M16_NA_REGS)
8953         {
8954           if (gp_reg_p)
8955             return NO_REGS;
8956           return M16_REGS;
8957         }
8958       if (! gp_reg_p)
8959         {
8960           /* The stack pointer isn't a valid operand to an add instruction,
8961              so we need to load it into M16_REGS first.  This can happen as
8962              a result of register elimination and form_sum converting
8963              (plus reg (plus SP CONST)) to (plus (plus reg SP) CONST).  We
8964              need an extra register if the dest is the same as the other
8965              register.  In that case, we can't fix the problem by loading SP
8966              into the dest first.  */
8967           if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
8968               && GET_CODE (XEXP (x, 1)) == REG
8969               && (XEXP (x, 0) == stack_pointer_rtx
8970                   || XEXP (x, 1) == stack_pointer_rtx))
8971             return (class == M16_REGS ? M16_NA_REGS : M16_REGS);
8972
8973           if (class == M16_REGS || class == M16_NA_REGS)
8974             return NO_REGS;
8975           return M16_REGS;
8976         }
8977     }
8978
8979   return NO_REGS;
8980 }
8981
8982 /* This function returns the maximum number of consecutive registers
8983    needed to represent mode MODE in registers of class CLASS.  */
8984
8985 int
8986 mips_class_max_nregs (class, mode)
8987      enum reg_class class;
8988      enum machine_mode mode;
8989 {
8990   if (class == FP_REGS)
8991     return FP_INC;
8992   else
8993     return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
8994 }
8995
8996 bool
8997 mips_valid_pointer_mode (mode)
8998      enum machine_mode mode;
8999 {
9000   return (mode == SImode || (TARGET_64BIT && mode == DImode));
9001 }
9002
9003 \f
9004 /* For each mips16 function which refers to GP relative symbols, we
9005    use a pseudo register, initialized at the start of the function, to
9006    hold the $gp value.  */
9007
9008 rtx
9009 mips16_gp_pseudo_reg ()
9010 {
9011   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
9012     {
9013       rtx const_gp;
9014       rtx insn, scan;
9015
9016       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
9017       RTX_UNCHANGING_P (cfun->machine->mips16_gp_pseudo_rtx) = 1;
9018
9019       /* We want to initialize this to a value which gcc will believe
9020          is constant.  */
9021       const_gp = gen_rtx (CONST, Pmode,
9022                           gen_rtx (REG, Pmode, GP_REG_FIRST + 28));
9023
9024       start_sequence ();
9025       emit_move_insn (cfun->machine->mips16_gp_pseudo_rtx,
9026                       const_gp);
9027       insn = get_insns ();
9028       end_sequence ();
9029
9030       push_topmost_sequence ();
9031       /* We need to emit the initialization after the FUNCTION_BEG
9032          note, so that it will be integrated.  */
9033       for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
9034         if (GET_CODE (scan) == NOTE
9035             && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
9036           break;
9037       if (scan == NULL_RTX)
9038         scan = get_insns ();
9039       insn = emit_insn_after (insn, scan);
9040       pop_topmost_sequence ();
9041     }
9042
9043   return cfun->machine->mips16_gp_pseudo_rtx;
9044 }
9045
9046 /* Return an RTX which represents the signed 16 bit offset from the
9047    $gp register for the given symbol.  This is only used on the
9048    mips16.  */
9049
9050 rtx
9051 mips16_gp_offset (sym)
9052      rtx sym;
9053 {
9054   tree gp;
9055
9056   if (GET_CODE (sym) != SYMBOL_REF
9057       || ! SYMBOL_REF_FLAG (sym))
9058     abort ();
9059
9060   /* We use a special identifier to represent the value of the gp
9061      register.  */
9062   gp = get_identifier ("__mips16_gp_value");
9063
9064   return gen_rtx (CONST, Pmode,
9065                   gen_rtx (MINUS, Pmode, sym,
9066                            gen_rtx (SYMBOL_REF, Pmode,
9067                                     IDENTIFIER_POINTER (gp))));
9068 }
9069
9070 /* Return nonzero if the given RTX represents a signed 16 bit offset
9071    from the $gp register.  */
9072
9073 int
9074 mips16_gp_offset_p (x)
9075      rtx x;
9076 {
9077   if (GET_CODE (x) == CONST)
9078     x = XEXP (x, 0);
9079
9080   /* It's OK to add a small integer value to a gp offset.  */
9081   if (GET_CODE (x) == PLUS)
9082     {
9083       if (GET_CODE (XEXP (x, 1)) == CONST_INT
9084           && SMALL_INT (XEXP (x, 1)))
9085         return mips16_gp_offset_p (XEXP (x, 0));
9086       if (GET_CODE (XEXP (x, 0)) == CONST_INT
9087           && SMALL_INT (XEXP (x, 0)))
9088         return mips16_gp_offset_p (XEXP (x, 1));
9089       return 0;
9090     }
9091
9092   /* Make sure it is in the form SYM - __mips16_gp_value.  */
9093   return (GET_CODE (x) == MINUS
9094           && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
9095           && SYMBOL_REF_FLAG (XEXP (x, 0))
9096           && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
9097           && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0);
9098 }
9099
9100 /* Output a GP offset.  We don't want to print the subtraction of
9101    __mips16_gp_value; it is implicitly represented by the %gprel which
9102    should have been printed by the caller.  */
9103
9104 static void
9105 mips16_output_gp_offset (file, x)
9106      FILE *file;
9107      rtx x;
9108 {
9109   if (GET_CODE (x) == CONST)
9110     x = XEXP (x, 0);
9111
9112   if (GET_CODE (x) == PLUS)
9113     {
9114       mips16_output_gp_offset (file, XEXP (x, 0));
9115       fputs ("+", file);
9116       mips16_output_gp_offset (file, XEXP (x, 1));
9117       return;
9118     }
9119
9120   if (GET_CODE (x) == MINUS
9121       && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
9122       && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0)
9123     {
9124       mips16_output_gp_offset (file, XEXP (x, 0));
9125       return;
9126     }
9127
9128   output_addr_const (file, x);
9129 }
9130
9131 /* Validate a constant for the mips16.  This rejects general symbolic
9132    addresses, which must be loaded from memory.  If ADDR is nonzero,
9133    this should reject anything which is not a legal address.  If
9134    ADDEND is nonzero, this is being added to something else.  */
9135
9136 int
9137 mips16_constant (x, mode, addr, addend)
9138      rtx x;
9139      enum machine_mode mode;
9140      int addr;
9141      int addend;
9142 {
9143   while (GET_CODE (x) == CONST)
9144     x = XEXP (x, 0);
9145
9146   switch (GET_CODE (x))
9147     {
9148     default:
9149       return 0;
9150
9151     case PLUS:
9152       return (mips16_constant (XEXP (x, 0), mode, addr, 1)
9153               && mips16_constant (XEXP (x, 1), mode, addr, 1));
9154
9155     case SYMBOL_REF:
9156       if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
9157         return 0;
9158       if (CONSTANT_POOL_ADDRESS_P (x))
9159         return 1;
9160
9161       /* If we aren't looking for a memory address, we can accept a GP
9162          relative symbol, which will have SYMBOL_REF_FLAG set; movsi
9163          knows how to handle this.  We can always accept a string
9164          constant, which is the other case in which SYMBOL_REF_FLAG
9165          will be set.  */
9166       if (! addr
9167           && ! addend
9168           && SYMBOL_REF_FLAG (x)
9169           && mode == (enum machine_mode) Pmode)
9170         return 1;
9171
9172       /* We can accept a string constant, which will have
9173          SYMBOL_REF_FLAG set but must be recognized by name to
9174          distinguish from a GP accessible symbol.  The name of a
9175          string constant will have been generated by
9176          ASM_GENERATE_INTERNAL_LABEL as called by output_constant_def.  */
9177       if (SYMBOL_REF_FLAG (x))
9178         {
9179           const char *name = XSTR (x, 0);
9180
9181           return (name[0] == '*'
9182                   && strncmp (name + 1, LOCAL_LABEL_PREFIX,
9183                               sizeof LOCAL_LABEL_PREFIX - 1) == 0);
9184         }
9185
9186       return 0;
9187
9188     case LABEL_REF:
9189       if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
9190         return 0;
9191       return 1;
9192
9193     case CONST_INT:
9194       if (addr && ! addend)
9195         return 0;
9196       return INTVAL (x) > - 0x10000 && INTVAL (x) <= 0xffff;
9197
9198     case REG:
9199       /* We need to treat $gp as a legitimate constant, because
9200          mips16_gp_pseudo_reg assumes that.  */
9201       return REGNO (x) == GP_REG_FIRST + 28;
9202     }
9203 }
9204
9205 /* Write out code to move floating point arguments in or out of
9206    general registers.  Output the instructions to FILE.  FP_CODE is
9207    the code describing which arguments are present (see the comment at
9208    the definition of CUMULATIVE_ARGS in mips.h).  FROM_FP_P is nonzero if
9209    we are copying from the floating point registers.  */
9210
9211 static void
9212 mips16_fp_args (file, fp_code, from_fp_p)
9213      FILE *file;
9214      int fp_code;
9215      int from_fp_p;
9216 {
9217   const char *s;
9218   int gparg, fparg;
9219   unsigned int f;
9220
9221   /* This code only works for the original 32 bit ABI and the O64 ABI.  */
9222   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
9223     abort ();
9224
9225   if (from_fp_p)
9226     s = "mfc1";
9227   else
9228     s = "mtc1";
9229   gparg = GP_ARG_FIRST;
9230   fparg = FP_ARG_FIRST;
9231   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
9232     {
9233       if ((f & 3) == 1)
9234         {
9235           if ((fparg & 1) != 0)
9236             ++fparg;
9237           fprintf (file, "\t%s\t%s,%s\n", s,
9238                    reg_names[gparg], reg_names[fparg]);
9239         }
9240       else if ((f & 3) == 2)
9241         {
9242           if (TARGET_64BIT)
9243             fprintf (file, "\td%s\t%s,%s\n", s,
9244                      reg_names[gparg], reg_names[fparg]);
9245           else
9246             {
9247               if ((fparg & 1) != 0)
9248                 ++fparg;
9249               if (TARGET_BIG_ENDIAN)
9250                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
9251                          reg_names[gparg], reg_names[fparg + 1], s,
9252                          reg_names[gparg + 1], reg_names[fparg]);
9253               else
9254                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
9255                          reg_names[gparg], reg_names[fparg], s,
9256                          reg_names[gparg + 1], reg_names[fparg + 1]);
9257               ++gparg;
9258               ++fparg;
9259             }
9260         }
9261       else
9262         abort ();
9263
9264       ++gparg;
9265       ++fparg;
9266     }
9267 }
9268
9269 /* Build a mips16 function stub.  This is used for functions which
9270    take aruments in the floating point registers.  It is 32 bit code
9271    that moves the floating point args into the general registers, and
9272    then jumps to the 16 bit code.  */
9273
9274 static void
9275 build_mips16_function_stub (file)
9276      FILE *file;
9277 {
9278   const char *fnname;
9279   char *secname, *stubname;
9280   tree stubid, stubdecl;
9281   int need_comma;
9282   unsigned int f;
9283
9284   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9285   secname = (char *) alloca (strlen (fnname) + 20);
9286   sprintf (secname, ".mips16.fn.%s", fnname);
9287   stubname = (char *) alloca (strlen (fnname) + 20);
9288   sprintf (stubname, "__fn_stub_%s", fnname);
9289   stubid = get_identifier (stubname);
9290   stubdecl = build_decl (FUNCTION_DECL, stubid,
9291                          build_function_type (void_type_node, NULL_TREE));
9292   DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
9293
9294   fprintf (file, "\t# Stub function for %s (", current_function_name);
9295   need_comma = 0;
9296   for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
9297     {
9298       fprintf (file, "%s%s",
9299                need_comma ? ", " : "",
9300                (f & 3) == 1 ? "float" : "double");
9301       need_comma = 1;
9302     }
9303   fprintf (file, ")\n");
9304
9305   fprintf (file, "\t.set\tnomips16\n");
9306   function_section (stubdecl);
9307   ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
9308
9309   /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
9310      within a .ent, and we can not emit another .ent.  */
9311 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9312   fputs ("\t.ent\t", file);
9313   assemble_name (file, stubname);
9314   fputs ("\n", file);
9315 #endif
9316
9317   assemble_name (file, stubname);
9318   fputs (":\n", file);
9319
9320   /* We don't want the assembler to insert any nops here.  */
9321   fprintf (file, "\t.set\tnoreorder\n");
9322
9323   mips16_fp_args (file, current_function_args_info.fp_code, 1);
9324
9325   fprintf (asm_out_file, "\t.set\tnoat\n");
9326   fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
9327   assemble_name (file, fnname);
9328   fprintf (file, "\n");
9329   fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
9330   fprintf (asm_out_file, "\t.set\tat\n");
9331
9332   /* Unfortunately, we can't fill the jump delay slot.  We can't fill
9333      with one of the mfc1 instructions, because the result is not
9334      available for one instruction, so if the very first instruction
9335      in the function refers to the register, it will see the wrong
9336      value.  */
9337   fprintf (file, "\tnop\n");
9338
9339   fprintf (file, "\t.set\treorder\n");
9340
9341 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9342   fputs ("\t.end\t", file);
9343   assemble_name (file, stubname);
9344   fputs ("\n", file);
9345 #endif
9346
9347   fprintf (file, "\t.set\tmips16\n");
9348
9349   function_section (current_function_decl);
9350 }
9351
9352 /* We keep a list of functions for which we have already built stubs
9353    in build_mips16_call_stub.  */
9354
9355 struct mips16_stub
9356 {
9357   struct mips16_stub *next;
9358   char *name;
9359   int fpret;
9360 };
9361
9362 static struct mips16_stub *mips16_stubs;
9363
9364 /* Build a call stub for a mips16 call.  A stub is needed if we are
9365    passing any floating point values which should go into the floating
9366    point registers.  If we are, and the call turns out to be to a 32
9367    bit function, the stub will be used to move the values into the
9368    floating point registers before calling the 32 bit function.  The
9369    linker will magically adjust the function call to either the 16 bit
9370    function or the 32 bit stub, depending upon where the function call
9371    is actually defined.
9372
9373    Similarly, we need a stub if the return value might come back in a
9374    floating point register.
9375
9376    RETVAL, FNMEM, and ARG_SIZE are the values passed to the call insn
9377    (RETVAL is NULL if this is call rather than call_value).  FP_CODE
9378    is the code built by function_arg.  This function returns a nonzero
9379    value if it builds the call instruction itself.  */
9380
9381 int
9382 build_mips16_call_stub (retval, fnmem, arg_size, fp_code)
9383      rtx retval;
9384      rtx fnmem;
9385      rtx arg_size;
9386      int fp_code;
9387 {
9388   int fpret;
9389   rtx fn;
9390   const char *fnname;
9391   char *secname, *stubname;
9392   struct mips16_stub *l;
9393   tree stubid, stubdecl;
9394   int need_comma;
9395   unsigned int f;
9396
9397   /* We don't need to do anything if we aren't in mips16 mode, or if
9398      we were invoked with the -msoft-float option.  */
9399   if (! TARGET_MIPS16 || ! mips16_hard_float)
9400     return 0;
9401
9402   /* Figure out whether the value might come back in a floating point
9403      register.  */
9404   fpret = (retval != 0
9405            && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
9406            && GET_MODE_SIZE (GET_MODE (retval)) <= UNITS_PER_FPVALUE);
9407
9408   /* We don't need to do anything if there were no floating point
9409      arguments and the value will not be returned in a floating point
9410      register.  */
9411   if (fp_code == 0 && ! fpret)
9412     return 0;
9413
9414   if (GET_CODE (fnmem) != MEM)
9415     abort ();
9416   fn = XEXP (fnmem, 0);
9417
9418   /* We don't need to do anything if this is a call to a special
9419      mips16 support function.  */
9420   if (GET_CODE (fn) == SYMBOL_REF
9421       && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
9422     return 0;
9423
9424   /* This code will only work for o32 and o64 abis.  The other ABI's
9425      require more sophisticated support.  */
9426   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
9427     abort ();
9428
9429   /* We can only handle SFmode and DFmode floating point return
9430      values.  */
9431   if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
9432     abort ();
9433
9434   /* If we're calling via a function pointer, then we must always call
9435      via a stub.  There are magic stubs provided in libgcc.a for each
9436      of the required cases.  Each of them expects the function address
9437      to arrive in register $2.  */
9438
9439   if (GET_CODE (fn) != SYMBOL_REF)
9440     {
9441       char buf[30];
9442       tree id;
9443       rtx stub_fn, stub_mem, insn;
9444
9445       /* ??? If this code is modified to support other ABI's, we need
9446          to handle PARALLEL return values here.  */
9447
9448       sprintf (buf, "__mips16_call_stub_%s%d",
9449                (fpret
9450                 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
9451                 : ""),
9452                fp_code);
9453       id = get_identifier (buf);
9454       stub_fn = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (id));
9455       stub_mem = gen_rtx (MEM, Pmode, stub_fn);
9456
9457       emit_move_insn (gen_rtx (REG, Pmode, 2), fn);
9458
9459       if (retval == NULL_RTX)
9460         insn = gen_call_internal0 (stub_mem, arg_size,
9461                                    gen_rtx (REG, SImode,
9462                                             GP_REG_FIRST + 31));
9463       else
9464         insn = gen_call_value_internal0 (retval, stub_mem, arg_size,
9465                                          gen_rtx (REG, SImode,
9466                                                   GP_REG_FIRST + 31));
9467       insn = emit_call_insn (insn);
9468
9469       /* Put the register usage information on the CALL.  */
9470       if (GET_CODE (insn) != CALL_INSN)
9471         abort ();
9472       CALL_INSN_FUNCTION_USAGE (insn) =
9473         gen_rtx (EXPR_LIST, VOIDmode,
9474                  gen_rtx (USE, VOIDmode, gen_rtx (REG, Pmode, 2)),
9475                  CALL_INSN_FUNCTION_USAGE (insn));
9476
9477       /* If we are handling a floating point return value, we need to
9478          save $18 in the function prologue.  Putting a note on the
9479          call will mean that regs_ever_live[$18] will be true if the
9480          call is not eliminated, and we can check that in the prologue
9481          code.  */
9482       if (fpret)
9483         CALL_INSN_FUNCTION_USAGE (insn) =
9484           gen_rtx (EXPR_LIST, VOIDmode,
9485                    gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
9486                    CALL_INSN_FUNCTION_USAGE (insn));
9487
9488       /* Return 1 to tell the caller that we've generated the call
9489          insn.  */
9490       return 1;
9491     }
9492
9493   /* We know the function we are going to call.  If we have already
9494      built a stub, we don't need to do anything further.  */
9495
9496   fnname = XSTR (fn, 0);
9497   for (l = mips16_stubs; l != NULL; l = l->next)
9498     if (strcmp (l->name, fnname) == 0)
9499       break;
9500
9501   if (l == NULL)
9502     {
9503       /* Build a special purpose stub.  When the linker sees a
9504          function call in mips16 code, it will check where the target
9505          is defined.  If the target is a 32 bit call, the linker will
9506          search for the section defined here.  It can tell which
9507          symbol this section is associated with by looking at the
9508          relocation information (the name is unreliable, since this
9509          might be a static function).  If such a section is found, the
9510          linker will redirect the call to the start of the magic
9511          section.
9512
9513          If the function does not return a floating point value, the
9514          special stub section is named
9515              .mips16.call.FNNAME
9516
9517          If the function does return a floating point value, the stub
9518          section is named
9519              .mips16.call.fp.FNNAME
9520          */
9521
9522       secname = (char *) alloca (strlen (fnname) + 40);
9523       sprintf (secname, ".mips16.call.%s%s",
9524                fpret ? "fp." : "",
9525                fnname);
9526       stubname = (char *) alloca (strlen (fnname) + 20);
9527       sprintf (stubname, "__call_stub_%s%s",
9528                fpret ? "fp_" : "",
9529                fnname);
9530       stubid = get_identifier (stubname);
9531       stubdecl = build_decl (FUNCTION_DECL, stubid,
9532                              build_function_type (void_type_node, NULL_TREE));
9533       DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
9534
9535       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
9536                (fpret
9537                 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
9538                 : ""),
9539                fnname);
9540       need_comma = 0;
9541       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
9542         {
9543           fprintf (asm_out_file, "%s%s",
9544                    need_comma ? ", " : "",
9545                    (f & 3) == 1 ? "float" : "double");
9546           need_comma = 1;
9547         }
9548       fprintf (asm_out_file, ")\n");
9549
9550       fprintf (asm_out_file, "\t.set\tnomips16\n");
9551       assemble_start_function (stubdecl, stubname);
9552
9553 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9554       fputs ("\t.ent\t", asm_out_file);
9555       assemble_name (asm_out_file, stubname);
9556       fputs ("\n", asm_out_file);
9557
9558       assemble_name (asm_out_file, stubname);
9559       fputs (":\n", asm_out_file);
9560 #endif
9561
9562       /* We build the stub code by hand.  That's the only way we can
9563          do it, since we can't generate 32 bit code during a 16 bit
9564          compilation.  */
9565
9566       /* We don't want the assembler to insert any nops here.  */
9567       fprintf (asm_out_file, "\t.set\tnoreorder\n");
9568
9569       mips16_fp_args (asm_out_file, fp_code, 0);
9570
9571       if (! fpret)
9572         {
9573           fprintf (asm_out_file, "\t.set\tnoat\n");
9574           fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
9575                    fnname);
9576           fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
9577           fprintf (asm_out_file, "\t.set\tat\n");
9578           /* Unfortunately, we can't fill the jump delay slot.  We
9579              can't fill with one of the mtc1 instructions, because the
9580              result is not available for one instruction, so if the
9581              very first instruction in the function refers to the
9582              register, it will see the wrong value.  */
9583           fprintf (asm_out_file, "\tnop\n");
9584         }
9585       else
9586         {
9587           fprintf (asm_out_file, "\tmove\t%s,%s\n",
9588                    reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
9589           fprintf (asm_out_file, "\tjal\t%s\n", fnname);
9590           /* As above, we can't fill the delay slot.  */
9591           fprintf (asm_out_file, "\tnop\n");
9592           if (GET_MODE (retval) == SFmode)
9593             fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9594                      reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
9595           else
9596             {
9597               if (TARGET_BIG_ENDIAN)
9598                 {
9599                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9600                            reg_names[GP_REG_FIRST + 2],
9601                            reg_names[FP_REG_FIRST + 1]);
9602                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9603                            reg_names[GP_REG_FIRST + 3],
9604                            reg_names[FP_REG_FIRST + 0]);
9605                 }
9606               else
9607                 {
9608                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9609                            reg_names[GP_REG_FIRST + 2],
9610                            reg_names[FP_REG_FIRST + 0]);
9611                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9612                            reg_names[GP_REG_FIRST + 3],
9613                            reg_names[FP_REG_FIRST + 1]);
9614                 }
9615             }
9616           fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
9617           /* As above, we can't fill the delay slot.  */
9618           fprintf (asm_out_file, "\tnop\n");
9619         }
9620
9621       fprintf (asm_out_file, "\t.set\treorder\n");
9622
9623 #ifdef ASM_DECLARE_FUNCTION_SIZE
9624       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
9625 #endif
9626
9627 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9628       fputs ("\t.end\t", asm_out_file);
9629       assemble_name (asm_out_file, stubname);
9630       fputs ("\n", asm_out_file);
9631 #endif
9632
9633       fprintf (asm_out_file, "\t.set\tmips16\n");
9634
9635       /* Record this stub.  */
9636       l = (struct mips16_stub *) xmalloc (sizeof *l);
9637       l->name = xstrdup (fnname);
9638       l->fpret = fpret;
9639       l->next = mips16_stubs;
9640       mips16_stubs = l;
9641     }
9642
9643   /* If we expect a floating point return value, but we've built a
9644      stub which does not expect one, then we're in trouble.  We can't
9645      use the existing stub, because it won't handle the floating point
9646      value.  We can't build a new stub, because the linker won't know
9647      which stub to use for the various calls in this object file.
9648      Fortunately, this case is illegal, since it means that a function
9649      was declared in two different ways in a single compilation.  */
9650   if (fpret && ! l->fpret)
9651     error ("can not handle inconsistent calls to `%s'", fnname);
9652
9653   /* If we are calling a stub which handles a floating point return
9654      value, we need to arrange to save $18 in the prologue.  We do
9655      this by marking the function call as using the register.  The
9656      prologue will later see that it is used, and emit code to save
9657      it.  */
9658
9659   if (l->fpret)
9660     {
9661       rtx insn;
9662
9663       if (retval == NULL_RTX)
9664         insn = gen_call_internal0 (fnmem, arg_size,
9665                                    gen_rtx (REG, SImode,
9666                                             GP_REG_FIRST + 31));
9667       else
9668         insn = gen_call_value_internal0 (retval, fnmem, arg_size,
9669                                          gen_rtx (REG, SImode,
9670                                                   GP_REG_FIRST + 31));
9671       insn = emit_call_insn (insn);
9672
9673       if (GET_CODE (insn) != CALL_INSN)
9674         abort ();
9675
9676       CALL_INSN_FUNCTION_USAGE (insn) =
9677         gen_rtx (EXPR_LIST, VOIDmode,
9678                  gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
9679                  CALL_INSN_FUNCTION_USAGE (insn));
9680
9681       /* Return 1 to tell the caller that we've generated the call
9682          insn.  */
9683       return 1;
9684     }
9685
9686   /* Return 0 to let the caller generate the call insn.  */
9687   return 0;
9688 }
9689
9690 /* This function looks through the code for a function, and tries to
9691    optimize the usage of the $gp register.  We arrange to copy $gp
9692    into a pseudo-register, and then let gcc's normal reload handling
9693    deal with the pseudo-register.  Unfortunately, if reload choose to
9694    put the pseudo-register into a call-clobbered register, it will
9695    emit saves and restores for that register around any function
9696    calls.  We don't need the saves, and it's faster to copy $gp than
9697    to do an actual restore.  ??? This still means that we waste a
9698    stack slot.
9699
9700    This is an optimization, and the code which gcc has actually
9701    generated is correct, so we do not need to catch all cases.  */
9702
9703 static void
9704 mips16_optimize_gp (first)
9705      rtx first;
9706 {
9707   rtx gpcopy, slot, insn;
9708
9709   /* Look through the instructions.  Set GPCOPY to the register which
9710      holds a copy of $gp.  Set SLOT to the stack slot where it is
9711      saved.  If we find an instruction which sets GPCOPY to anything
9712      other than $gp or SLOT, then we can't use it.  If we find an
9713      instruction which sets SLOT to anything other than GPCOPY, we
9714      can't use it.  */
9715
9716   gpcopy = NULL_RTX;
9717   slot = NULL_RTX;
9718   for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9719     {
9720       rtx set;
9721
9722       if (! INSN_P (insn))
9723         continue;
9724
9725       set = PATTERN (insn);
9726
9727       /* We know that all references to memory will be inside a SET,
9728          because there is no other way to access memory on the mips16.
9729          We don't have to worry about a PARALLEL here, because the
9730          mips.md file will never generate them for memory references.  */
9731       if (GET_CODE (set) != SET)
9732         continue;
9733
9734       if (gpcopy == NULL_RTX
9735           && GET_CODE (SET_SRC (set)) == CONST
9736           && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
9737           && REGNO (XEXP (SET_SRC (set), 0)) == GP_REG_FIRST + 28
9738           && GET_CODE (SET_DEST (set)) == REG
9739           && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
9740         gpcopy = SET_DEST (set);
9741       else if (slot == NULL_RTX
9742                && gpcopy != NULL_RTX
9743                && GET_CODE (SET_DEST (set)) == MEM
9744                && GET_CODE (SET_SRC (set)) == REG
9745                && REGNO (SET_SRC (set)) == REGNO (gpcopy)
9746                && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
9747         {
9748           rtx base, offset;
9749
9750           offset = const0_rtx;
9751           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
9752           if (GET_CODE (base) == REG
9753               && (REGNO (base) == STACK_POINTER_REGNUM
9754                   || REGNO (base) == FRAME_POINTER_REGNUM))
9755             slot = SET_DEST (set);
9756         }
9757       else if (gpcopy != NULL_RTX
9758                && (GET_CODE (SET_DEST (set)) == REG
9759                    || GET_CODE (SET_DEST (set)) == SUBREG)
9760                && reg_overlap_mentioned_p (SET_DEST (set), gpcopy)
9761                && (GET_CODE (SET_DEST (set)) != REG
9762                    || REGNO (SET_DEST (set)) != REGNO (gpcopy)
9763                    || GET_MODE (SET_DEST (set)) != (unsigned) Pmode
9764                    || ((GET_CODE (SET_SRC (set)) != CONST
9765                         || GET_CODE (XEXP (SET_SRC (set), 0)) != REG
9766                         || (REGNO (XEXP (SET_SRC (set), 0))
9767                             != GP_REG_FIRST + 28))
9768                        && ! rtx_equal_p (SET_SRC (set), slot))))
9769         break;
9770       else if (slot != NULL_RTX
9771                && GET_CODE (SET_DEST (set)) == MEM
9772                && rtx_equal_p (SET_DEST (set), slot)
9773                && (GET_CODE (SET_SRC (set)) != REG
9774                    || REGNO (SET_SRC (set)) != REGNO (gpcopy)))
9775         break;
9776     }
9777
9778   /* If we couldn't find a unique value for GPCOPY or SLOT, then try a
9779      different optimization.  Any time we find a copy of $28 into a
9780      register, followed by an add of a symbol_ref to that register, we
9781      convert it to load the value from the constant table instead.
9782      The copy and add will take six bytes, just as the load and
9783      constant table entry will take six bytes.  However, it is
9784      possible that the constant table entry will be shared.
9785
9786      This could be a peephole optimization, but I don't know if the
9787      peephole code can call force_const_mem.
9788
9789      Using the same register for the copy of $28 and the add of the
9790      symbol_ref is actually pretty likely, since the add instruction
9791      requires the destination and the first addend to be the same
9792      register.  */
9793
9794   if (insn != NULL_RTX || gpcopy == NULL_RTX || slot == NULL_RTX)
9795     {
9796       rtx next;
9797
9798       /* This optimization is only reasonable if the constant table
9799          entries are only 4 bytes.  */
9800       if (Pmode != SImode)
9801         return;
9802
9803       for (insn = first; insn != NULL_RTX; insn = next)
9804         {
9805           rtx set1, set2;
9806
9807           next = insn;
9808           do
9809             {
9810               next = NEXT_INSN (next);
9811             }
9812           while (next != NULL_RTX
9813                  && (GET_CODE (next) == NOTE
9814                      || (GET_CODE (next) == INSN
9815                          && (GET_CODE (PATTERN (next)) == USE
9816                              || GET_CODE (PATTERN (next)) == CLOBBER))));
9817
9818           if (next == NULL_RTX)
9819             break;
9820
9821           if (! INSN_P (insn))
9822             continue;
9823
9824           if (! INSN_P (next))
9825             continue;
9826
9827           set1 = PATTERN (insn);
9828           if (GET_CODE (set1) != SET)
9829             continue;
9830           set2 = PATTERN (next);
9831           if (GET_CODE (set2) != SET)
9832             continue;
9833
9834           if (GET_CODE (SET_DEST (set1)) == REG
9835               && GET_CODE (SET_SRC (set1)) == CONST
9836               && GET_CODE (XEXP (SET_SRC (set1), 0)) == REG
9837               && REGNO (XEXP (SET_SRC (set1), 0)) == GP_REG_FIRST + 28
9838               && rtx_equal_p (SET_DEST (set1), SET_DEST (set2))
9839               && GET_CODE (SET_SRC (set2)) == PLUS
9840               && rtx_equal_p (SET_DEST (set1), XEXP (SET_SRC (set2), 0))
9841               && mips16_gp_offset_p (XEXP (SET_SRC (set2), 1))
9842               && GET_CODE (XEXP (XEXP (SET_SRC (set2), 1), 0)) == MINUS)
9843             {
9844               rtx sym;
9845
9846               /* We've found a case we can change to load from the
9847                  constant table.  */
9848
9849               sym = XEXP (XEXP (XEXP (SET_SRC (set2), 1), 0), 0);
9850               if (GET_CODE (sym) != SYMBOL_REF)
9851                 abort ();
9852               emit_insn_after (gen_rtx (SET, VOIDmode, SET_DEST (set1),
9853                                         force_const_mem (Pmode, sym)),
9854                                next);
9855
9856               PUT_CODE (insn, NOTE);
9857               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9858               NOTE_SOURCE_FILE (insn) = 0;
9859
9860               PUT_CODE (next, NOTE);
9861               NOTE_LINE_NUMBER (next) = NOTE_INSN_DELETED;
9862               NOTE_SOURCE_FILE (next) = 0;
9863             }
9864         }
9865
9866       return;
9867     }
9868
9869   /* We can safely remove all assignments to SLOT from GPCOPY, and
9870      replace all assignments from SLOT to GPCOPY with assignments from
9871      $28.  */
9872
9873   for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9874     {
9875       rtx set;
9876
9877       if (! INSN_P (insn))
9878         continue;
9879
9880       set = PATTERN (insn);
9881       if (GET_CODE (set) != SET
9882           || GET_MODE (SET_DEST (set)) != (unsigned) Pmode)
9883         continue;
9884
9885       if (GET_CODE (SET_DEST (set)) == MEM
9886           && rtx_equal_p (SET_DEST (set), slot)
9887           && GET_CODE (SET_SRC (set)) == REG
9888           && REGNO (SET_SRC (set)) == REGNO (gpcopy))
9889         {
9890           PUT_CODE (insn, NOTE);
9891           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9892           NOTE_SOURCE_FILE (insn) = 0;
9893         }
9894       else if (GET_CODE (SET_DEST (set)) == REG
9895                && REGNO (SET_DEST (set)) == REGNO (gpcopy)
9896                && GET_CODE (SET_SRC (set)) == MEM
9897                && rtx_equal_p (SET_SRC (set), slot))
9898         {
9899           emit_insn_after (gen_rtx (SET, Pmode, SET_DEST (set),
9900                                     gen_rtx (CONST, Pmode,
9901                                              gen_rtx (REG, Pmode,
9902                                                       GP_REG_FIRST + 28))),
9903                            insn);
9904           PUT_CODE (insn, NOTE);
9905           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9906           NOTE_SOURCE_FILE (insn) = 0;
9907         }
9908     }
9909 }
9910
9911 /* We keep a list of constants we which we have to add to internal
9912    constant tables in the middle of large functions.  */
9913
9914 struct constant
9915 {
9916   struct constant *next;
9917   rtx value;
9918   rtx label;
9919   enum machine_mode mode;
9920 };
9921
9922 /* Add a constant to the list in *PCONSTANTS.  */
9923
9924 static rtx
9925 add_constant (pconstants, val, mode)
9926      struct constant **pconstants;
9927      rtx val;
9928      enum machine_mode mode;
9929 {
9930   struct constant *c;
9931
9932   for (c = *pconstants; c != NULL; c = c->next)
9933     if (mode == c->mode && rtx_equal_p (val, c->value))
9934       return c->label;
9935
9936   c = (struct constant *) xmalloc (sizeof *c);
9937   c->value = val;
9938   c->mode = mode;
9939   c->label = gen_label_rtx ();
9940   c->next = *pconstants;
9941   *pconstants = c;
9942   return c->label;
9943 }
9944
9945 /* Dump out the constants in CONSTANTS after INSN.  */
9946
9947 static void
9948 dump_constants (constants, insn)
9949      struct constant *constants;
9950      rtx insn;
9951 {
9952   struct constant *c;
9953   int align;
9954
9955   c = constants;
9956   align = 0;
9957   while (c != NULL)
9958     {
9959       rtx r;
9960       struct constant *next;
9961
9962       switch (GET_MODE_SIZE (c->mode))
9963         {
9964         case 1:
9965           align = 0;
9966           break;
9967         case 2:
9968           if (align < 1)
9969             insn = emit_insn_after (gen_align_2 (), insn);
9970           align = 1;
9971           break;
9972         case 4:
9973           if (align < 2)
9974             insn = emit_insn_after (gen_align_4 (), insn);
9975           align = 2;
9976           break;
9977         default:
9978           if (align < 3)
9979             insn = emit_insn_after (gen_align_8 (), insn);
9980           align = 3;
9981           break;
9982         }
9983
9984       insn = emit_label_after (c->label, insn);
9985
9986       switch (c->mode)
9987         {
9988         case QImode:
9989           r = gen_consttable_qi (c->value);
9990           break;
9991         case HImode:
9992           r = gen_consttable_hi (c->value);
9993           break;
9994         case SImode:
9995           r = gen_consttable_si (c->value);
9996           break;
9997         case SFmode:
9998           r = gen_consttable_sf (c->value);
9999           break;
10000         case DImode:
10001           r = gen_consttable_di (c->value);
10002           break;
10003         case DFmode:
10004           r = gen_consttable_df (c->value);
10005           break;
10006         default:
10007           abort ();
10008         }
10009
10010       insn = emit_insn_after (r, insn);
10011
10012       next = c->next;
10013       free (c);
10014       c = next;
10015     }
10016
10017   emit_barrier_after (insn);
10018 }
10019
10020 /* Find the symbol in an address expression.  */
10021
10022 static rtx
10023 mips_find_symbol (addr)
10024      rtx addr;
10025 {
10026   if (GET_CODE (addr) == MEM)
10027     addr = XEXP (addr, 0);
10028   while (GET_CODE (addr) == CONST)
10029     addr = XEXP (addr, 0);
10030   if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
10031     return addr;
10032   if (GET_CODE (addr) == PLUS)
10033     {
10034       rtx l1, l2;
10035
10036       l1 = mips_find_symbol (XEXP (addr, 0));
10037       l2 = mips_find_symbol (XEXP (addr, 1));
10038       if (l1 != NULL_RTX && l2 == NULL_RTX)
10039         return l1;
10040       else if (l1 == NULL_RTX && l2 != NULL_RTX)
10041         return l2;
10042     }
10043   return NULL_RTX;
10044 }
10045
10046 /* Exported to toplev.c.
10047
10048    Do a final pass over the function, just before delayed branch
10049    scheduling.  */
10050
10051 void
10052 machine_dependent_reorg (first)
10053      rtx first;
10054 {
10055   int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
10056   rtx insn;
10057   struct constant *constants;
10058
10059   if (! TARGET_MIPS16)
10060     return;
10061
10062   /* If $gp is used, try to remove stores, and replace loads with
10063      copies from $gp.  */
10064   if (optimize)
10065     mips16_optimize_gp (first);
10066
10067   /* Scan the function looking for PC relative loads which may be out
10068      of range.  All such loads will either be from the constant table,
10069      or be getting the address of a constant string.  If the size of
10070      the function plus the size of the constant table is less than
10071      0x8000, then all loads are in range.  */
10072
10073   insns_len = 0;
10074   for (insn = first; insn; insn = NEXT_INSN (insn))
10075     {
10076       insns_len += get_attr_length (insn);
10077
10078       /* ??? We put switch tables in .text, but we don't define
10079          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
10080          compute their lengths correctly.  */
10081       if (GET_CODE (insn) == JUMP_INSN)
10082         {
10083           rtx body;
10084
10085           body = PATTERN (insn);
10086           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
10087             insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
10088                           * GET_MODE_SIZE (GET_MODE (body)));
10089           insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
10090         }
10091     }
10092
10093   /* Store the original value of insns_len in cfun->machine, so
10094      that simple_memory_operand can look at it.  */
10095   cfun->machine->insns_len = insns_len;
10096
10097   pool_size = get_pool_size ();
10098   if (insns_len + pool_size + mips_string_length < 0x8000)
10099     return;
10100
10101   /* Loop over the insns and figure out what the maximum internal pool
10102      size could be.  */
10103   max_internal_pool_size = 0;
10104   for (insn = first; insn; insn = NEXT_INSN (insn))
10105     {
10106       if (GET_CODE (insn) == INSN
10107           && GET_CODE (PATTERN (insn)) == SET)
10108         {
10109           rtx src;
10110
10111           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
10112           if (src == NULL_RTX)
10113             continue;
10114           if (CONSTANT_POOL_ADDRESS_P (src))
10115             max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
10116           else if (SYMBOL_REF_FLAG (src))
10117             max_internal_pool_size += GET_MODE_SIZE (Pmode);
10118         }
10119     }
10120
10121   constants = NULL;
10122   addr = 0;
10123   first_constant_ref = -1;
10124
10125   for (insn = first; insn; insn = NEXT_INSN (insn))
10126     {
10127       if (GET_CODE (insn) == INSN
10128           && GET_CODE (PATTERN (insn)) == SET)
10129         {
10130           rtx val, src;
10131           enum machine_mode mode = VOIDmode;
10132
10133           val = NULL_RTX;
10134           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
10135           if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
10136             {
10137               /* ??? This is very conservative, which means that we
10138                  will generate too many copies of the constant table.
10139                  The only solution would seem to be some form of
10140                  relaxing.  */
10141               if (((insns_len - addr)
10142                    + max_internal_pool_size
10143                    + get_pool_offset (src))
10144                   >= 0x8000)
10145                 {
10146                   val = get_pool_constant (src);
10147                   mode = get_pool_mode (src);
10148                 }
10149               max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
10150             }
10151           else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
10152             {
10153               /* Including all of mips_string_length is conservative,
10154                  and so is including all of max_internal_pool_size.  */
10155               if (((insns_len - addr)
10156                    + max_internal_pool_size
10157                    + pool_size
10158                    + mips_string_length)
10159                   >= 0x8000)
10160                 {
10161                   val = src;
10162                   mode = Pmode;
10163                 }
10164               max_internal_pool_size -= Pmode;
10165             }
10166
10167           if (val != NULL_RTX)
10168             {
10169               rtx lab, newsrc;
10170
10171               /* This PC relative load is out of range.  ??? In the
10172                  case of a string constant, we are only guessing that
10173                  it is range, since we don't know the offset of a
10174                  particular string constant.  */
10175
10176               lab = add_constant (&constants, val, mode);
10177               newsrc = gen_rtx (MEM, mode,
10178                                 gen_rtx (LABEL_REF, VOIDmode, lab));
10179               RTX_UNCHANGING_P (newsrc) = 1;
10180               PATTERN (insn) = gen_rtx (SET, VOIDmode,
10181                                         SET_DEST (PATTERN (insn)),
10182                                         newsrc);
10183               INSN_CODE (insn) = -1;
10184
10185               if (first_constant_ref < 0)
10186                 first_constant_ref = addr;
10187             }
10188         }
10189
10190       addr += get_attr_length (insn);
10191
10192       /* ??? We put switch tables in .text, but we don't define
10193          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
10194          compute their lengths correctly.  */
10195       if (GET_CODE (insn) == JUMP_INSN)
10196         {
10197           rtx body;
10198
10199           body = PATTERN (insn);
10200           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
10201             addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
10202                           * GET_MODE_SIZE (GET_MODE (body)));
10203           addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
10204         }
10205
10206       if (GET_CODE (insn) == BARRIER)
10207         {
10208           /* Output any constants we have accumulated.  Note that we
10209              don't need to change ADDR, since its only use is
10210              subtraction from INSNS_LEN, and both would be changed by
10211              the same amount.
10212              ??? If the instructions up to the next barrier reuse a
10213              constant, it would often be better to continue
10214              accumulating.  */
10215           if (constants != NULL)
10216             dump_constants (constants, insn);
10217           constants = NULL;
10218           first_constant_ref = -1;
10219         }
10220
10221       if (constants != NULL
10222                && (NEXT_INSN (insn) == NULL
10223                    || (first_constant_ref >= 0
10224                        && (((addr - first_constant_ref)
10225                             + 2 /* for alignment */
10226                             + 2 /* for a short jump insn */
10227                             + pool_size)
10228                            >= 0x8000))))
10229         {
10230           /* If we haven't had a barrier within 0x8000 bytes of a
10231              constant reference or we are at the end of the function,
10232              emit a barrier now.  */
10233
10234           rtx label, jump, barrier;
10235
10236           label = gen_label_rtx ();
10237           jump = emit_jump_insn_after (gen_jump (label), insn);
10238           JUMP_LABEL (jump) = label;
10239           LABEL_NUSES (label) = 1;
10240           barrier = emit_barrier_after (jump);
10241           emit_label_after (label, barrier);
10242           first_constant_ref = -1;
10243         }
10244      }
10245
10246   /* ??? If we output all references to a constant in internal
10247      constants table, we don't need to output the constant in the real
10248      constant table, but we have no way to prevent that.  */
10249 }
10250
10251 /* Return nonzero if X is a SIGN or ZERO extend operator.  */
10252 int
10253 extend_operator (x, mode)
10254      rtx x;
10255      enum machine_mode mode ATTRIBUTE_UNUSED;
10256 {
10257   enum rtx_code code = GET_CODE (x);
10258   return code == SIGN_EXTEND || code == ZERO_EXTEND;
10259 }
10260
10261 /* Accept any operator that can be used to shift the high half of the
10262    input value to the lower half, suitable for truncation.  The
10263    remainder (the lower half of the input, and the upper half of the
10264    output) will be discarded.  */
10265 int
10266 highpart_shift_operator (x, mode)
10267      rtx x;
10268      enum machine_mode mode ATTRIBUTE_UNUSED;
10269 {
10270   enum rtx_code code = GET_CODE (x);
10271   return (code == LSHIFTRT
10272           || code == ASHIFTRT
10273           || code == ROTATERT
10274           || code == ROTATE);
10275 }
10276
10277 /* Return a number assessing the cost of moving a register in class
10278    FROM to class TO.  The classes are expressed using the enumeration
10279    values such as `GENERAL_REGS'.  A value of 2 is the default; other
10280    values are interpreted relative to that.
10281
10282    It is not required that the cost always equal 2 when FROM is the
10283    same as TO; on some machines it is expensive to move between
10284    registers if they are not general registers.
10285
10286    If reload sees an insn consisting of a single `set' between two
10287    hard registers, and if `REGISTER_MOVE_COST' applied to their
10288    classes returns a value of 2, reload does not check to ensure that
10289    the constraints of the insn are met.  Setting a cost of other than
10290    2 will allow reload to verify that the constraints are met.  You
10291    should do this if the `movM' pattern's constraints do not allow
10292    such copying.
10293
10294    ??? We make make the cost of moving from HI/LO/HILO/MD into general
10295    registers the same as for one of moving general registers to
10296    HI/LO/HILO/MD for TARGET_MIPS16 in order to prevent allocating a
10297    pseudo to HI/LO/HILO/MD.  This might hurt optimizations though, it
10298    isn't clear if it is wise.  And it might not work in all cases.  We
10299    could solve the DImode LO reg problem by using a multiply, just
10300    like reload_{in,out}si.  We could solve the SImode/HImode HI reg
10301    problem by using divide instructions.  divu puts the remainder in
10302    the HI reg, so doing a divide by -1 will move the value in the HI
10303    reg for all values except -1.  We could handle that case by using a
10304    signed divide, e.g.  -1 / 2 (or maybe 1 / -2?).  We'd have to emit
10305    a compare/branch to test the input value to see which instruction
10306    we need to use.  This gets pretty messy, but it is feasible.  */
10307
10308 int
10309 mips_register_move_cost (mode, to, from)
10310      enum machine_mode mode ATTRIBUTE_UNUSED;
10311      enum reg_class to, from;
10312 {
10313   if (from == M16_REGS && GR_REG_CLASS_P (to))
10314     return 2;
10315   else if (from == M16_NA_REGS && GR_REG_CLASS_P (to))
10316     return 2;
10317   else if (GR_REG_CLASS_P (from))
10318     {
10319       if (to == M16_REGS)
10320         return 2;
10321       else if (to == M16_NA_REGS)
10322         return 2;
10323       else if (GR_REG_CLASS_P (to))
10324         {
10325           if (TARGET_MIPS16)
10326             return 4;
10327           else
10328             return 2;
10329         }
10330       else if (to == FP_REGS)
10331         return 4;
10332       else if (to == HI_REG || to == LO_REG || to == MD_REGS
10333                || to == HILO_REG)
10334         {
10335           if (TARGET_MIPS16)
10336             return 12;
10337           else
10338             return 6;
10339         }
10340       else if (COP_REG_CLASS_P (to))
10341         {
10342           return 5;
10343         }
10344     }  /* GR_REG_CLASS_P (from) */
10345   else if (from == FP_REGS)
10346     {
10347       if (GR_REG_CLASS_P (to))
10348         return 4;
10349       else if (to == FP_REGS)
10350         return 2;
10351       else if (to == ST_REGS)
10352         return 8;
10353     }  /* from == FP_REGS */
10354   else if (from == HI_REG || from == LO_REG || from == MD_REGS
10355            || from == HILO_REG)
10356     {
10357       if (GR_REG_CLASS_P (to))
10358         {
10359           if (TARGET_MIPS16)
10360             return 12;
10361           else
10362             return 6;
10363         }
10364     }  /* from == HI_REG, etc. */
10365   else if (from == ST_REGS && GR_REG_CLASS_P (to))
10366     return 4;
10367   else if (COP_REG_CLASS_P (from))
10368     {
10369       return 5;
10370     }  /* COP_REG_CLASS_P (from) */
10371
10372   /* fallthru */
10373
10374   return 12;
10375 }
10376
10377 /* Return the length of INSN.  LENGTH is the initial length computed by
10378    attributes in the machine-description file.  */
10379
10380 int
10381 mips_adjust_insn_length (insn, length)
10382      rtx insn;
10383      int length;
10384 {
10385   /* A unconditional jump has an unfilled delay slot if it is not part
10386      of a sequence.  A conditional jump normally has a delay slot, but
10387      does not on MIPS16.  */
10388   if (simplejump_p (insn)
10389       || (!TARGET_MIPS16  && (GET_CODE (insn) == JUMP_INSN
10390                               || GET_CODE (insn) == CALL_INSN)))
10391     length += 4;
10392
10393   /* All MIPS16 instructions are a measly two bytes.  */
10394   if (TARGET_MIPS16)
10395     length /= 2;
10396
10397   return length;
10398 }
10399
10400 /* Output assembly instructions to peform a conditional branch.
10401
10402    INSN is the branch instruction.  OPERANDS[0] is the condition.
10403    OPERANDS[1] is the target of the branch.  OPERANDS[2] is the target
10404    of the first operand to the condition.  If TWO_OPERANDS_P is
10405    nonzero the comparison takes two operands; OPERANDS[3] will be the
10406    second operand.
10407
10408    If INVERTED_P is nonzero we are to branch if the condition does
10409    not hold.  If FLOAT_P is nonzero this is a floating-point comparison.
10410
10411    LENGTH is the length (in bytes) of the sequence we are to generate.
10412    That tells us whether to generate a simple conditional branch, or a
10413    reversed conditional branch around a `jr' instruction.  */
10414 const char *
10415 mips_output_conditional_branch (insn,
10416                                 operands,
10417                                 two_operands_p,
10418                                 float_p,
10419                                 inverted_p,
10420                                 length)
10421      rtx insn;
10422      rtx *operands;
10423      int two_operands_p;
10424      int float_p;
10425      int inverted_p;
10426      int length;
10427 {
10428   static char buffer[200];
10429   /* The kind of comparison we are doing.  */
10430   enum rtx_code code = GET_CODE (operands[0]);
10431   /* Nonzero if the opcode for the comparison needs a `z' indicating
10432      that it is a comparision against zero.  */
10433   int need_z_p;
10434   /* A string to use in the assembly output to represent the first
10435      operand.  */
10436   const char *op1 = "%z2";
10437   /* A string to use in the assembly output to represent the second
10438      operand.  Use the hard-wired zero register if there's no second
10439      operand.  */
10440   const char *op2 = (two_operands_p ? ",%z3" : ",%.");
10441   /* The operand-printing string for the comparison.  */
10442   const char *const comp = (float_p ? "%F0" : "%C0");
10443   /* The operand-printing string for the inverted comparison.  */
10444   const char *const inverted_comp = (float_p ? "%W0" : "%N0");
10445
10446   /* The MIPS processors (for levels of the ISA at least two), have
10447      "likely" variants of each branch instruction.  These instructions
10448      annul the instruction in the delay slot if the branch is not
10449      taken.  */
10450   mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
10451
10452   if (!two_operands_p)
10453     {
10454       /* To compute whether than A > B, for example, we normally
10455          subtract B from A and then look at the sign bit.  But, if we
10456          are doing an unsigned comparison, and B is zero, we don't
10457          have to do the subtraction.  Instead, we can just check to
10458          see if A is nonzero.  Thus, we change the CODE here to
10459          reflect the simpler comparison operation.  */
10460       switch (code)
10461         {
10462         case GTU:
10463           code = NE;
10464           break;
10465
10466         case LEU:
10467           code = EQ;
10468           break;
10469
10470         case GEU:
10471           /* A condition which will always be true.  */
10472           code = EQ;
10473           op1 = "%.";
10474           break;
10475
10476         case LTU:
10477           /* A condition which will always be false.  */
10478           code = NE;
10479           op1 = "%.";
10480           break;
10481
10482         default:
10483           /* Not a special case.  */
10484           break;
10485         }
10486     }
10487
10488   /* Relative comparisons are always done against zero.  But
10489      equality comparisons are done between two operands, and therefore
10490      do not require a `z' in the assembly language output.  */
10491   need_z_p = (!float_p && code != EQ && code != NE);
10492   /* For comparisons against zero, the zero is not provided
10493      explicitly.  */
10494   if (need_z_p)
10495     op2 = "";
10496
10497   /* Begin by terminating the buffer.  That way we can always use
10498      strcat to add to it.  */
10499   buffer[0] = '\0';
10500
10501   switch (length)
10502     {
10503     case 4:
10504     case 8:
10505       /* Just a simple conditional branch.  */
10506       if (float_p)
10507         sprintf (buffer, "%%*b%s%%?\t%%Z2%%1",
10508                  inverted_p ? inverted_comp : comp);
10509       else
10510         sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1",
10511                  inverted_p ? inverted_comp : comp,
10512                  need_z_p ? "z" : "",
10513                  op1,
10514                  op2);
10515       return buffer;
10516
10517     case 12:
10518     case 16:
10519     case 24:
10520     case 28:
10521       {
10522         /* Generate a reversed conditional branch around ` j'
10523            instruction:
10524
10525                 .set noreorder
10526                 .set nomacro
10527                 bc    l
10528                 delay_slot or #nop
10529                 j     target
10530                 #nop
10531              l:
10532                 .set macro
10533                 .set reorder
10534
10535            If the original branch was a likely branch, the delay slot
10536            must be executed only if the branch is taken, so generate:
10537
10538                 .set noreorder
10539                 .set nomacro
10540                 bc    l
10541                 #nop
10542                 j     target
10543                 delay slot or #nop
10544              l:
10545                 .set macro
10546                 .set reorder
10547            
10548            When generating non-embedded PIC, instead of:
10549
10550                 j     target
10551
10552            we emit:
10553
10554                 .set noat
10555                 la    $at, target
10556                 jr    $at
10557                 .set at
10558         */
10559
10560         rtx orig_target;
10561         rtx target = gen_label_rtx ();
10562
10563         orig_target = operands[1];
10564         operands[1] = target;
10565         /* Generate the reversed comparison.  This takes four
10566            bytes.  */
10567         if (float_p)
10568           sprintf (buffer, "%%*b%s\t%%Z2%%1",
10569                    inverted_p ? comp : inverted_comp);
10570         else
10571           sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
10572                    inverted_p ? comp : inverted_comp,
10573                    need_z_p ? "z" : "",
10574                    op1,
10575                    op2);
10576         output_asm_insn (buffer, operands);
10577
10578         if (length != 16 && length != 28 && ! mips_branch_likely)
10579           {
10580             /* Output delay slot instruction.  */
10581             rtx insn = final_sequence;
10582             final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
10583                              optimize, 0, 1);
10584             INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
10585           }
10586         else
10587           output_asm_insn ("%#", 0);
10588
10589         if (length <= 16)
10590           output_asm_insn ("j\t%0", &orig_target);
10591         else
10592           {
10593             if (Pmode == DImode)
10594               output_asm_insn ("%[dla\t%@,%0\n\tjr\t%@%]", &orig_target);
10595             else
10596               output_asm_insn ("%[la\t%@,%0\n\tjr\t%@%]", &orig_target);
10597           }
10598
10599         if (length != 16 && length != 28 && mips_branch_likely)
10600           {
10601             /* Output delay slot instruction.  */
10602             rtx insn = final_sequence;
10603             final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
10604                              optimize, 0, 1);
10605             INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
10606           }
10607         else
10608           output_asm_insn ("%#", 0);
10609
10610         (*targetm.asm_out.internal_label) (asm_out_file, "L",
10611                                    CODE_LABEL_NUMBER (target));
10612
10613         return "";
10614       }
10615
10616     /* We do not currently use this code.  It handles jumps to
10617        arbitrary locations, using `jr', even across a 256MB boundary.
10618        We could add a -mhuge switch, and then use this code instead of
10619        the `j' alternative above when -mhuge was used.  */
10620 #if 0
10621     case 16:
10622     case 20:
10623       {
10624         /* Generate a reversed conditional branch around a `jr'
10625            instruction:
10626
10627                  .set noreorder
10628                  .set nomacro
10629                  .set noat
10630                  bc    l
10631                  la    $at, target
10632                  jr    $at
10633                  .set at
10634                  .set macro
10635                  .set reorder
10636               l:
10637
10638            Not pretty, but allows a conditional branch anywhere in the
10639            32-bit address space.  If the original branch is annulled,
10640            then the instruction in the delay slot should be executed
10641            only if the branch is taken.  The la instruction is really
10642            a macro which will usually take eight bytes, but sometimes
10643            takes only four, if the instruction to which we're jumping
10644            gets its own entry in the global pointer table, which will
10645            happen if its a case label.  The assembler will then
10646            generate only a four-byte sequence, rather than eight, and
10647            there seems to be no way to tell it not to.  Thus, we can't
10648            just use a `.+x' addressing form; we don't know what value
10649            to give for `x'.
10650
10651            So, we resort to using the explicit relocation syntax
10652            available in the assembler and do:
10653
10654               lw $at,%got_page(target)($gp)
10655               daddiu $at,$at,%got_ofst(target)
10656
10657            That way, this always takes up eight bytes, and we can use
10658            the `.+x' form.  Of course, these explicit machinations
10659            with relocation will not work with old assemblers.  Then
10660            again, neither do out-of-range branches, so we haven't lost
10661            anything.  */
10662
10663         /* The target of the reversed branch.  */
10664         const char *const target
10665           = ((mips_branch_likely || length == 20) ? ".+20" : ".+16");
10666         const char *at_register = mips_reg_names[ASSEMBLER_SCRATCH_REGNUM];
10667         const char *gp_register = mips_reg_names[PIC_OFFSET_TABLE_REGNUM];
10668         char *c;
10669
10670         strcpy (buffer, "%(%<%[");
10671         c = strchr (buffer, '\0');
10672         /* Generate the reversed comparision.  This takes four
10673            bytes.  */
10674         if (float_p)
10675           sprintf (c, "%%*b%s\t%%Z2%s",
10676                    inverted_p ? comp : inverted_comp,
10677                    target);
10678         else
10679           sprintf (c, "%%*b%s%s\t%s%s,%s",
10680                    inverted_p ? comp : inverted_comp,
10681                    need_z_p ? "z" : "",
10682                    op1,
10683                    op2,
10684                    target);
10685         c = strchr (buffer, '\0');
10686         /* Generate the load-address, and jump.  This takes twelve
10687            bytes, for a total of 16.  */
10688         sprintf (c,
10689                  "\n\tlw\t%s,%%%%got_page(%%1)(%s)\n\tdaddiu\t%s,%s,%%%%got_ofst(%%1)\n\tjr\t%s",
10690                  at_register,
10691                  gp_register,
10692                  at_register,
10693                  at_register,
10694                  at_register);
10695         if (length == 20)
10696           /* The delay slot was unfilled.  Since we're inside
10697              .noreorder, the assembler will not fill in the NOP for
10698              us, so we must do it ourselves.  */
10699           strcat (buffer, "\n\tnop");
10700         strcat (buffer, "%]%>%)");
10701         return buffer;
10702       }
10703 #endif
10704
10705     default:
10706       abort ();
10707     }
10708
10709   /* NOTREACHED */
10710   return 0;
10711 }
10712 \f
10713 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
10714    with a final "000" replaced by "k".  Ignore case.
10715
10716    Note: this function is shared between GCC and GAS.  */
10717
10718 static bool
10719 mips_strict_matching_cpu_name_p (canonical, given)
10720      const char *canonical, *given;
10721 {
10722   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
10723     given++, canonical++;
10724
10725   return ((*given == 0 && *canonical == 0)
10726           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
10727 }
10728
10729
10730 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
10731    CPU name.  We've traditionally allowed a lot of variation here.
10732
10733    Note: this function is shared between GCC and GAS.  */
10734
10735 static bool
10736 mips_matching_cpu_name_p (canonical, given)
10737      const char *canonical, *given;
10738 {
10739   /* First see if the name matches exactly, or with a final "000"
10740      turned into "k".  */
10741   if (mips_strict_matching_cpu_name_p (canonical, given))
10742     return true;
10743
10744   /* If not, try comparing based on numerical designation alone.
10745      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
10746   if (TOLOWER (*given) == 'r')
10747     given++;
10748   if (!ISDIGIT (*given))
10749     return false;
10750
10751   /* Skip over some well-known prefixes in the canonical name,
10752      hoping to find a number there too.  */
10753   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
10754     canonical += 2;
10755   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
10756     canonical += 2;
10757   else if (TOLOWER (canonical[0]) == 'r')
10758     canonical += 1;
10759
10760   return mips_strict_matching_cpu_name_p (canonical, given);
10761 }
10762
10763
10764 /* Parse an option that takes the name of a processor as its argument.
10765    OPTION is the name of the option and CPU_STRING is the argument.
10766    Return the corresponding processor enumeration if the CPU_STRING is
10767    recognized, otherwise report an error and return null.
10768
10769    A similar function exists in GAS.  */
10770
10771 static const struct mips_cpu_info *
10772 mips_parse_cpu (option, cpu_string)
10773      const char *option, *cpu_string;
10774 {
10775   const struct mips_cpu_info *p;
10776   const char *s;
10777
10778   /* In the past, we allowed upper-case CPU names, but it doesn't
10779      work well with the multilib machinery.  */
10780   for (s = cpu_string; *s != 0; s++)
10781     if (ISUPPER (*s))
10782       {
10783         warning ("the cpu name must be lower case");
10784         break;
10785       }
10786
10787   /* 'from-abi' selects the most compatible architecture for the given
10788      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
10789      EABIs, we have to decide whether we're using the 32-bit or 64-bit
10790      version.  Look first at the -mgp options, if given, otherwise base
10791      the choice on MASK_64BIT in TARGET_DEFAULT.  */
10792   if (strcasecmp (cpu_string, "from-abi") == 0)
10793     return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
10794                                    : ABI_NEEDS_64BIT_REGS ? 3
10795                                    : (TARGET_64BIT ? 3 : 1));
10796
10797   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
10798   if (strcasecmp (cpu_string, "default") == 0)
10799     return 0;
10800
10801   for (p = mips_cpu_info_table; p->name != 0; p++)
10802     if (mips_matching_cpu_name_p (p->name, cpu_string))
10803       return p;
10804
10805   error ("bad value (%s) for %s", cpu_string, option);
10806   return 0;
10807 }
10808
10809
10810 /* Return the processor associated with the given ISA level, or null
10811    if the ISA isn't valid.  */
10812
10813 static const struct mips_cpu_info *
10814 mips_cpu_info_from_isa (isa)
10815      int isa;
10816 {
10817   const struct mips_cpu_info *p;
10818
10819   for (p = mips_cpu_info_table; p->name != 0; p++)
10820     if (p->isa == isa)
10821       return p;
10822
10823   return 0;
10824 }
10825 \f
10826 /* Adjust the cost of INSN based on the relationship between INSN that
10827    is dependent on DEP_INSN through the dependence LINK.  The default
10828    is to make no adjustment to COST.
10829
10830    On the MIPS, ignore the cost of anti- and output-dependencies.  */
10831 static int
10832 mips_adjust_cost (insn, link, dep, cost)
10833      rtx insn ATTRIBUTE_UNUSED;
10834      rtx link;
10835      rtx dep ATTRIBUTE_UNUSED;
10836      int cost;
10837 {
10838   if (REG_NOTE_KIND (link) != 0)
10839     return 0;   /* Anti or output dependence.  */
10840   return cost;
10841 }
10842
10843 /* ??? This could be replaced with the default elf version if
10844    TARGET_IS_SMALL_DATA_P is set properly.  */
10845
10846 static void
10847 mips_unique_section (decl, reloc)
10848      tree decl;
10849      int reloc;
10850 {
10851   int len, size, sec;
10852   const char *name, *prefix;
10853   char *string;
10854   static const char *const prefixes[4][2] = {
10855     { ".text.", ".gnu.linkonce.t." },
10856     { ".rodata.", ".gnu.linkonce.r." },
10857     { ".data.", ".gnu.linkonce.d." },
10858     { ".sdata.", ".gnu.linkonce.s." }
10859   };
10860
10861   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10862   name = (* targetm.strip_name_encoding) (name);
10863   size = int_size_in_bytes (TREE_TYPE (decl));
10864
10865   /* Determine the base section we are interested in:
10866      0=text, 1=rodata, 2=data, 3=sdata, [4=bss].  */
10867   if (TREE_CODE (decl) == FUNCTION_DECL)
10868     sec = 0;
10869   else if (DECL_INITIAL (decl) == 0
10870            || DECL_INITIAL (decl) == error_mark_node)
10871     sec = 2;
10872   else if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
10873       && TREE_CODE (decl) == STRING_CST
10874       && !flag_writable_strings)
10875     {
10876       /* For embedded position independent code, put constant
10877          strings in the text section, because the data section
10878          is limited to 64K in size.  For mips16 code, put
10879          strings in the text section so that a PC relative load
10880          instruction can be used to get their address.  */
10881       sec = 0;
10882     }
10883   else if (TARGET_EMBEDDED_DATA)
10884     {
10885       /* For embedded applications, always put an object in
10886          read-only data if possible, in order to reduce RAM
10887          usage.  */
10888
10889       if (decl_readonly_section (decl, reloc))
10890         sec = 1;
10891       else if (size > 0 && size <= mips_section_threshold)
10892         sec = 3;
10893       else
10894         sec = 2;
10895     }
10896   else
10897     {
10898       /* For hosted applications, always put an object in
10899          small data if possible, as this gives the best
10900          performance.  */
10901
10902       if (size > 0 && size <= mips_section_threshold)
10903         sec = 3;
10904       else if (decl_readonly_section (decl, reloc))
10905         sec = 1;
10906       else
10907         sec = 2;
10908     }
10909
10910   prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
10911   len = strlen (name) + strlen (prefix);
10912   string = alloca (len + 1);
10913   sprintf (string, "%s%s", prefix, name);
10914
10915   DECL_SECTION_NAME (decl) = build_string (len, string);
10916 }
10917
10918 unsigned int
10919 mips_hard_regno_nregs (regno, mode)
10920     int regno;
10921     enum machine_mode mode;
10922 {
10923   if (! FP_REG_P (regno))
10924     return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
10925   else
10926     return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
10927 }
10928
10929 int
10930 mips_return_in_memory (type)
10931      tree type;
10932 {
10933   /* Under the old (i.e., 32 and O64 ABIs) all BLKmode objects are
10934      returned in memory.  Under the new (N32 and 64-bit MIPS ABIs) small
10935      structures are returned in a register.  Objects with varying size
10936      must still be returned in memory, of course.  */
10937
10938   if (mips_abi == ABI_32 || mips_abi == ABI_O64)
10939     return (TYPE_MODE (type) == BLKmode);
10940   else
10941     return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
10942             || (int_size_in_bytes (type) == -1));
10943 }
10944
10945 static int
10946 mips_issue_rate ()
10947 {
10948   switch (mips_tune)
10949     {
10950     case PROCESSOR_R3000: return 1;
10951     case PROCESSOR_R5400: return 2;
10952     case PROCESSOR_R5500: return 2;
10953
10954     default:
10955       return 1;
10956     }
10957
10958   abort ();
10959
10960 }
10961
10962 /* Implements TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE.  Return true for
10963    processors that have a DFA pipeline description.  */
10964
10965 static int
10966 mips_use_dfa_pipeline_interface ()
10967 {
10968   switch (mips_tune)
10969     {
10970     case PROCESSOR_R5400:
10971     case PROCESSOR_R5500:
10972     case PROCESSOR_SR71000:
10973       return true;
10974
10975     default:
10976       return false;
10977     }
10978 }
10979
10980
10981 const char *
10982 mips_emit_prefetch (operands)
10983      rtx operands[];
10984 {
10985  /* For the mips32/64 architectures the hint fields are arranged
10986     by operation (load/store) and locality (normal/streamed/retained).
10987     Irritatingly, numbers 2 and 3 are reserved leaving no simple
10988     algorithm for figuring the hint.  */
10989
10990     int write = INTVAL (operands[1]);
10991     int locality = INTVAL (operands[2]);
10992
10993     static const char * const alt[2][4] = {
10994         {
10995          "pref\t4,%a0",
10996          "pref\t0,%a0",
10997          "pref\t0,%a0",
10998          "pref\t6,%a0"
10999         },
11000         {
11001          "pref\t5,%a0",
11002          "pref\t1,%a0",
11003          "pref\t1,%a0",
11004          "pref\t7,%a0"
11005         }
11006     };
11007
11008     return alt[write][locality];
11009 }
11010
11011
11012 \f
11013 #ifdef TARGET_IRIX6
11014 /* Output assembly to switch to section NAME with attribute FLAGS.  */
11015
11016 static void
11017 iris6_asm_named_section_1 (name, flags, align)
11018      const char *name;
11019      unsigned int flags;
11020      unsigned int align;
11021 {
11022   unsigned int sh_type, sh_flags, sh_entsize;
11023
11024   sh_flags = 0;
11025   if (!(flags & SECTION_DEBUG))
11026     sh_flags |= 2; /* SHF_ALLOC */
11027   if (flags & SECTION_WRITE)
11028     sh_flags |= 1; /* SHF_WRITE */
11029   if (flags & SECTION_CODE)
11030     sh_flags |= 4; /* SHF_EXECINSTR */
11031   if (flags & SECTION_SMALL)
11032     sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
11033   if (strcmp (name, ".debug_frame") == 0)
11034     sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
11035   if (flags & SECTION_DEBUG)
11036     sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
11037   else if (flags & SECTION_BSS)
11038     sh_type = 8; /* SHT_NOBITS */
11039   else
11040     sh_type = 1; /* SHT_PROGBITS */
11041
11042   if (flags & SECTION_CODE)
11043     sh_entsize = 4;
11044   else
11045     sh_entsize = 0;
11046
11047   fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
11048            name, sh_type, sh_flags, sh_entsize, align);
11049 }
11050
11051 static void
11052 iris6_asm_named_section (name, flags)
11053      const char *name;
11054      unsigned int flags;
11055 {
11056   if (TARGET_FILE_SWITCHING && (flags & SECTION_CODE))
11057     asm_out_file = asm_out_text_file;
11058   iris6_asm_named_section_1 (name, flags, 0);
11059 }
11060
11061 /* In addition to emitting a .align directive, record the maximum
11062    alignment requested for the current section.  */
11063
11064 struct iris_section_align_entry
11065 {
11066   const char *name;
11067   unsigned int log;
11068   unsigned int flags;
11069 };
11070
11071 static htab_t iris_section_align_htab;
11072 static FILE *iris_orig_asm_out_file;
11073
11074 static int
11075 iris_section_align_entry_eq (p1, p2)
11076      const PTR p1;
11077      const PTR p2;
11078 {
11079   const struct iris_section_align_entry *old = p1;
11080   const char *new = p2;
11081
11082   return strcmp (old->name, new) == 0;
11083 }
11084
11085 static hashval_t
11086 iris_section_align_entry_hash (p)
11087      const PTR p;
11088 {
11089   const struct iris_section_align_entry *old = p;
11090   return htab_hash_string (old->name);
11091 }
11092
11093 void
11094 iris6_asm_output_align (file, log)
11095      FILE *file;
11096      unsigned int log;
11097 {
11098   const char *section = current_section_name ();
11099   struct iris_section_align_entry **slot, *entry;
11100
11101   if (! section)
11102     abort ();
11103
11104   slot = (struct iris_section_align_entry **)
11105     htab_find_slot_with_hash (iris_section_align_htab, section,
11106                               htab_hash_string (section), INSERT);
11107   entry = *slot;
11108   if (! entry)
11109     {
11110       entry = (struct iris_section_align_entry *)
11111         xmalloc (sizeof (struct iris_section_align_entry));
11112       *slot = entry;
11113       entry->name = section;
11114       entry->log = log;
11115       entry->flags = current_section_flags ();
11116     }
11117   else if (entry->log < log)
11118     entry->log = log;
11119
11120   fprintf (file, "\t.align\t%u\n", log);
11121 }
11122
11123 /* The Iris assembler does not record alignment from .align directives,
11124    but takes it from the first .section directive seen.  Play yet more
11125    file switching games so that we can emit a .section directive at the
11126    beginning of the file with the proper alignment attached.  */
11127
11128 void
11129 iris6_asm_file_start (stream)
11130      FILE *stream;
11131 {
11132   mips_asm_file_start (stream);
11133
11134   iris_orig_asm_out_file = asm_out_file;
11135   stream = tmpfile ();
11136   asm_out_file = stream;
11137   asm_out_data_file = stream;
11138   if (! TARGET_FILE_SWITCHING)
11139     asm_out_text_file = stream;
11140
11141   iris_section_align_htab = htab_create (31, iris_section_align_entry_hash,
11142                                          iris_section_align_entry_eq, NULL);
11143 }
11144
11145 static int
11146 iris6_section_align_1 (slot, data)
11147      void **slot;
11148      void *data ATTRIBUTE_UNUSED;
11149 {
11150   const struct iris_section_align_entry *entry
11151     = *(const struct iris_section_align_entry **) slot;
11152
11153   iris6_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
11154   return 1;
11155 }
11156
11157 void
11158 iris6_asm_file_end (stream)
11159      FILE *stream;
11160 {
11161   /* Emit section directives with the proper alignment at the top of the
11162      real output file.  */
11163   asm_out_file = iris_orig_asm_out_file;
11164   htab_traverse (iris_section_align_htab, iris6_section_align_1, NULL);
11165
11166   /* Copy the data emitted to the temp file to the real output file.  */
11167   copy_file_data (asm_out_file, stream);
11168
11169   mips_asm_file_end (stream);
11170 }
11171 #endif /* TARGET_IRIX6 */
11172
11173 #include "gt-mips.h"