OSDN Git Service

8fdd97918d81639391a259c0f1adb2c1377059b7
[pf3gnuchains/gcc-fork.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 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 GCC.
10
11 GCC 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 GCC 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 GCC; 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 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.h>
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "insn-attr.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "tree.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "optabs.h"
45 #include "flags.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "ggc.h"
49 #include "gstab.h"
50 #include "hashtab.h"
51 #include "debug.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "integrate.h"
55 #include "langhooks.h"
56 #include "cfglayout.h"
57
58 /* Enumeration for all of the relational tests, so that we can build
59    arrays indexed by the test type, and not worry about the order
60    of EQ, NE, etc.  */
61
62 enum internal_test {
63   ITEST_EQ,
64   ITEST_NE,
65   ITEST_GT,
66   ITEST_GE,
67   ITEST_LT,
68   ITEST_LE,
69   ITEST_GTU,
70   ITEST_GEU,
71   ITEST_LTU,
72   ITEST_LEU,
73   ITEST_MAX
74 };
75
76 /* True if X is an unspec wrapper around a SYMBOL_REF or LABEL_REF.  */
77 #define UNSPEC_ADDRESS_P(X)                                     \
78   (GET_CODE (X) == UNSPEC                                       \
79    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST                       \
80    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
81
82 /* Extract the symbol or label from UNSPEC wrapper X.  */
83 #define UNSPEC_ADDRESS(X) \
84   XVECEXP (X, 0, 0)
85
86 /* Extract the symbol type from UNSPEC wrapper X.  */
87 #define UNSPEC_ADDRESS_TYPE(X) \
88   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
89
90 /* True if X is (const $gp).  This is used to initialize the mips16
91    gp pseudo register.  */
92 #define CONST_GP_P(X) \
93   (GET_CODE (X) == CONST && XEXP (X, 0) == pic_offset_table_rtx)
94
95 /* The maximum distance between the top of the stack frame and the
96    value $sp has when we save & restore registers.
97
98    Use a maximum gap of 0x100 in the mips16 case.  We can then use
99    unextended instructions to save and restore registers, and to
100    allocate and deallocate the top part of the frame.
101
102    The value in the !mips16 case must be a SMALL_OPERAND and must
103    preserve the maximum stack alignment.  It could really be 0x7ff0,
104    but SGI's assemblers implement daddiu $sp,$sp,-0x7ff0 as a
105    multi-instruction addu sequence.  Use 0x7fe0 to work around this.  */
106 #define MIPS_MAX_FIRST_STACK_STEP (TARGET_MIPS16 ? 0x100 : 0x7fe0)
107
108 /* Classifies a SYMBOL_REF, LABEL_REF or UNSPEC address.
109
110    SYMBOL_GENERAL
111        Used when none of the below apply.
112
113    SYMBOL_SMALL_DATA
114        The symbol refers to something in a small data section.
115
116    SYMBOL_CONSTANT_POOL
117        The symbol refers to something in the mips16 constant pool.
118
119    SYMBOL_GOT_LOCAL
120        The symbol refers to local data that will be found using
121        the global offset table.
122
123    SYMBOL_GOT_GLOBAL
124        Likewise non-local data.
125
126    SYMBOL_GOTOFF_PAGE
127        An UNSPEC wrapper around a SYMBOL_GOT_LOCAL.  It represents the
128        offset from _gp of a GOT page entry.
129
130    SYMBOL_GOTOFF_GLOBAL
131        An UNSPEC wrapper around a SYMBOL_GOT_GLOBAL.  It represents the
132        the offset from _gp of the symbol's GOT entry.
133
134    SYMBOL_GOTOFF_CALL
135        Like SYMBOL_GOTOFF_GLOBAL, but used when calling a global function.
136        The GOT entry is allowed to point to a stub rather than to the
137        function itself.
138
139    SYMBOL_GOTOFF_LOADGP
140        An UNSPEC wrapper around a function's address.  It represents the
141        offset of _gp from the start of the function.  */
142 enum mips_symbol_type {
143   SYMBOL_GENERAL,
144   SYMBOL_SMALL_DATA,
145   SYMBOL_CONSTANT_POOL,
146   SYMBOL_GOT_LOCAL,
147   SYMBOL_GOT_GLOBAL,
148   SYMBOL_GOTOFF_PAGE,
149   SYMBOL_GOTOFF_GLOBAL,
150   SYMBOL_GOTOFF_CALL,
151   SYMBOL_GOTOFF_LOADGP
152 };
153 #define NUM_SYMBOL_TYPES (SYMBOL_GOTOFF_LOADGP + 1)
154
155
156 /* Classifies an address.
157
158    ADDRESS_REG
159        A natural register + offset address.  The register satisfies
160        mips_valid_base_register_p and the offset is a const_arith_operand.
161
162    ADDRESS_LO_SUM
163        A LO_SUM rtx.  The first operand is a valid base register and
164        the second operand is a symbolic address.
165
166    ADDRESS_CONST_INT
167        A signed 16-bit constant address.
168
169    ADDRESS_SYMBOLIC:
170        A constant symbolic address (equivalent to CONSTANT_SYMBOLIC).  */
171 enum mips_address_type {
172   ADDRESS_REG,
173   ADDRESS_LO_SUM,
174   ADDRESS_CONST_INT,
175   ADDRESS_SYMBOLIC
176 };
177
178 /* A function to save or store a register.  The first argument is the
179    register and the second is the stack slot.  */
180 typedef void (*mips_save_restore_fn) (rtx, rtx);
181
182 struct constant;
183 struct mips_arg_info;
184 struct mips_address_info;
185 struct mips_integer_op;
186
187 static enum mips_symbol_type mips_classify_symbol (rtx);
188 static void mips_split_const (rtx, rtx *, HOST_WIDE_INT *);
189 static bool mips_offset_within_object_p (rtx, HOST_WIDE_INT);
190 static bool mips_symbolic_constant_p (rtx, enum mips_symbol_type *);
191 static bool mips_valid_base_register_p (rtx, enum machine_mode, int);
192 static bool mips_symbolic_address_p (enum mips_symbol_type, enum machine_mode);
193 static bool mips_classify_address (struct mips_address_info *, rtx,
194                                    enum machine_mode, int);
195 static int mips_symbol_insns (enum mips_symbol_type);
196 static bool mips16_unextended_reference_p (enum machine_mode mode, rtx, rtx);
197 static rtx mips_force_temporary (rtx, rtx);
198 static rtx mips_split_symbol (rtx, rtx);
199 static rtx mips_unspec_address (rtx, enum mips_symbol_type);
200 static rtx mips_unspec_offset_high (rtx, rtx, rtx, enum mips_symbol_type);
201 static rtx mips_add_offset (rtx, HOST_WIDE_INT);
202 static unsigned int mips_build_shift (struct mips_integer_op *, HOST_WIDE_INT);
203 static unsigned int mips_build_lower (struct mips_integer_op *,
204                                       unsigned HOST_WIDE_INT);
205 static unsigned int mips_build_integer (struct mips_integer_op *,
206                                         unsigned HOST_WIDE_INT);
207 static void mips_move_integer (rtx, unsigned HOST_WIDE_INT);
208 static void mips_legitimize_const_move (enum machine_mode, rtx, rtx);
209 static int m16_check_op (rtx, int, int, int);
210 static bool mips_rtx_costs (rtx, int, int, int *);
211 static int mips_address_cost (rtx);
212 static enum internal_test map_test_to_internal_test (enum rtx_code);
213 static void get_float_compare_codes (enum rtx_code, enum rtx_code *,
214                                      enum rtx_code *);
215 static void mips_load_call_address (rtx, rtx, int);
216 static bool mips_function_ok_for_sibcall (tree, tree);
217 static void mips_block_move_straight (rtx, rtx, HOST_WIDE_INT);
218 static void mips_adjust_block_mem (rtx, HOST_WIDE_INT, rtx *, rtx *);
219 static void mips_block_move_loop (rtx, rtx, HOST_WIDE_INT);
220 static void mips_arg_info (const CUMULATIVE_ARGS *, enum machine_mode,
221                            tree, int, struct mips_arg_info *);
222 static bool mips_get_unaligned_mem (rtx *, unsigned int, int, rtx *, rtx *);
223 static void mips_set_architecture (const struct mips_cpu_info *);
224 static void mips_set_tune (const struct mips_cpu_info *);
225 static struct machine_function *mips_init_machine_status (void);
226 static void print_operand_reloc (FILE *, rtx, const char **);
227 static bool mips_assemble_integer (rtx, unsigned int, int);
228 static void mips_file_start (void);
229 static void mips_file_end (void);
230 static bool mips_rewrite_small_data_p (rtx);
231 static int small_data_pattern_1 (rtx *, void *);
232 static int mips_rewrite_small_data_1 (rtx *, void *);
233 static bool mips_function_has_gp_insn (void);
234 static unsigned int mips_global_pointer (void);
235 static bool mips_save_reg_p (unsigned int);
236 static void mips_save_restore_reg (enum machine_mode, int, HOST_WIDE_INT,
237                                    mips_save_restore_fn);
238 static void mips_for_each_saved_reg (HOST_WIDE_INT, mips_save_restore_fn);
239 static void mips_output_cplocal (void);
240 static void mips_emit_loadgp (void);
241 static void mips_output_function_prologue (FILE *, HOST_WIDE_INT);
242 static void mips_set_frame_expr (rtx);
243 static rtx mips_frame_set (rtx, rtx);
244 static void mips_save_reg (rtx, rtx);
245 static void mips_output_function_epilogue (FILE *, HOST_WIDE_INT);
246 static void mips_restore_reg (rtx, rtx);
247 static void mips_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
248                                   HOST_WIDE_INT, tree);
249 static int symbolic_expression_p (rtx);
250 static void mips_select_rtx_section (enum machine_mode, rtx,
251                                      unsigned HOST_WIDE_INT);
252 static void mips_select_section (tree, int, unsigned HOST_WIDE_INT)
253                                   ATTRIBUTE_UNUSED;
254 static bool mips_in_small_data_p (tree);
255 static void mips_encode_section_info (tree, rtx, int);
256 static int mips_fpr_return_fields (tree, tree *);
257 static bool mips_return_in_msb (tree);
258 static rtx mips_return_fpr_pair (enum machine_mode mode,
259                                  enum machine_mode mode1, HOST_WIDE_INT,
260                                  enum machine_mode mode2, HOST_WIDE_INT);
261 static rtx mips16_gp_pseudo_reg (void);
262 static void mips16_fp_args (FILE *, int, int);
263 static void build_mips16_function_stub (FILE *);
264 static rtx add_constant (struct constant **, rtx, enum machine_mode);
265 static void dump_constants (struct constant *, rtx);
266 static rtx mips_find_symbol (rtx);
267 static void mips16_lay_out_constants (void);
268 static void mips_avoid_hazard (rtx, rtx, int *, rtx *, rtx);
269 static void mips_avoid_hazards (void);
270 static void mips_reorg (void);
271 static bool mips_strict_matching_cpu_name_p (const char *, const char *);
272 static bool mips_matching_cpu_name_p (const char *, const char *);
273 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
274 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
275 static int mips_adjust_cost (rtx, rtx, rtx, int);
276 static bool mips_return_in_memory (tree, tree);
277 static bool mips_strict_argument_naming (CUMULATIVE_ARGS *);
278 static int mips_issue_rate (void);
279 static int mips_use_dfa_pipeline_interface (void);
280 static void mips_init_libfuncs (void);
281 static void mips_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
282                                          tree, int *, int);
283 static tree mips_build_builtin_va_list (void);
284
285 #if TARGET_IRIX
286 static void irix_asm_named_section_1 (const char *, unsigned int,
287                                       unsigned int);
288 static void irix_asm_named_section (const char *, unsigned int);
289 static int irix_section_align_entry_eq (const void *, const void *);
290 static hashval_t irix_section_align_entry_hash (const void *);
291 static void irix_file_start (void);
292 static int irix_section_align_1 (void **, void *);
293 static void copy_file_data (FILE *, FILE *);
294 static void irix_file_end (void);
295 static unsigned int irix_section_type_flags (tree, const char *, int);
296 #endif
297
298 /* Structure to be filled in by compute_frame_size with register
299    save masks, and offsets for the current function.  */
300
301 struct mips_frame_info GTY(())
302 {
303   HOST_WIDE_INT total_size;     /* # bytes that the entire frame takes up */
304   HOST_WIDE_INT var_size;       /* # bytes that variables take up */
305   HOST_WIDE_INT args_size;      /* # bytes that outgoing arguments take up */
306   HOST_WIDE_INT cprestore_size; /* # bytes that the .cprestore slot takes up */
307   HOST_WIDE_INT gp_reg_size;    /* # bytes needed to store gp regs */
308   HOST_WIDE_INT fp_reg_size;    /* # bytes needed to store fp regs */
309   unsigned int mask;            /* mask of saved gp registers */
310   unsigned int fmask;           /* mask of saved fp registers */
311   HOST_WIDE_INT gp_save_offset; /* offset from vfp to store gp registers */
312   HOST_WIDE_INT fp_save_offset; /* offset from vfp to store fp registers */
313   HOST_WIDE_INT gp_sp_offset;   /* offset from new sp to store gp registers */
314   HOST_WIDE_INT fp_sp_offset;   /* offset from new sp to store fp registers */
315   bool initialized;             /* true if frame size already calculated */
316   int num_gp;                   /* number of gp registers saved */
317   int num_fp;                   /* number of fp registers saved */
318 };
319
320 struct machine_function GTY(()) {
321   /* Pseudo-reg holding the address of the current function when
322      generating embedded PIC code.  */
323   rtx embedded_pic_fnaddr_rtx;
324
325   /* Pseudo-reg holding the value of $28 in a mips16 function which
326      refers to GP relative global variables.  */
327   rtx mips16_gp_pseudo_rtx;
328
329   /* Current frame information, calculated by compute_frame_size.  */
330   struct mips_frame_info frame;
331
332   /* Length of instructions in function; mips16 only.  */
333   long insns_len;
334
335   /* The register to use as the global pointer within this function.  */
336   unsigned int global_pointer;
337
338   /* True if mips_adjust_insn_length should ignore an instruction's
339      hazard attribute.  */
340   bool ignore_hazard_length_p;
341
342   /* True if the whole function is suitable for .set noreorder and
343      .set nomacro.  */
344   bool all_noreorder_p;
345
346   /* True if the function is known to have an instruction that needs $gp.  */
347   bool has_gp_insn_p;
348 };
349
350 /* Information about a single argument.  */
351 struct mips_arg_info
352 {
353   /* True if the argument is passed in a floating-point register, or
354      would have been if we hadn't run out of registers.  */
355   bool fpr_p;
356
357   /* The argument's size, in bytes.  */
358   unsigned int num_bytes;
359
360   /* The number of words passed in registers, rounded up.  */
361   unsigned int reg_words;
362
363   /* The offset of the first register from GP_ARG_FIRST or FP_ARG_FIRST,
364      or MAX_ARGS_IN_REGISTERS if the argument is passed entirely
365      on the stack.  */
366   unsigned int reg_offset;
367
368   /* The number of words that must be passed on the stack, rounded up.  */
369   unsigned int stack_words;
370
371   /* The offset from the start of the stack overflow area of the argument's
372      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
373   unsigned int stack_offset;
374 };
375
376
377 /* Information about an address described by mips_address_type.
378
379    ADDRESS_CONST_INT
380        No fields are used.
381
382    ADDRESS_REG
383        REG is the base register and OFFSET is the constant offset.
384
385    ADDRESS_LO_SUM
386        REG is the register that contains the high part of the address,
387        OFFSET is the symbolic address being referenced and SYMBOL_TYPE
388        is the type of OFFSET's symbol.
389
390    ADDRESS_SYMBOLIC
391        SYMBOL_TYPE is the type of symbol being referenced.  */
392
393 struct mips_address_info
394 {
395   enum mips_address_type type;
396   rtx reg;
397   rtx offset;
398   enum mips_symbol_type symbol_type;
399 };
400
401
402 /* One stage in a constant building sequence.  These sequences have
403    the form:
404
405         A = VALUE[0]
406         A = A CODE[1] VALUE[1]
407         A = A CODE[2] VALUE[2]
408         ...
409
410    where A is an accumulator, each CODE[i] is a binary rtl operation
411    and each VALUE[i] is a constant integer.  */
412 struct mips_integer_op {
413   enum rtx_code code;
414   unsigned HOST_WIDE_INT value;
415 };
416
417
418 /* The largest number of operations needed to load an integer constant.
419    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
420    When the lowest bit is clear, we can try, but reject a sequence with
421    an extra SLL at the end.  */
422 #define MIPS_MAX_INTEGER_OPS 7
423
424
425 /* Global variables for machine-dependent things.  */
426
427 /* Threshold for data being put into the small data/bss area, instead
428    of the normal data area.  */
429 int mips_section_threshold = -1;
430
431 /* Count the number of .file directives, so that .loc is up to date.  */
432 int num_source_filenames = 0;
433
434 /* Count the number of sdb related labels are generated (to find block
435    start and end boundaries).  */
436 int sdb_label_count = 0;
437
438 /* Next label # for each statement for Silicon Graphics IRIS systems.  */
439 int sym_lineno = 0;
440
441 /* Linked list of all externals that are to be emitted when optimizing
442    for the global pointer if they haven't been declared by the end of
443    the program with an appropriate .comm or initialization.  */
444
445 struct extern_list GTY (())
446 {
447   struct extern_list *next;     /* next external */
448   const char *name;             /* name of the external */
449   int size;                     /* size in bytes */
450 };
451
452 static GTY (()) struct extern_list *extern_head = 0;
453
454 /* Name of the file containing the current function.  */
455 const char *current_function_file = "";
456
457 /* Number of nested .set noreorder, noat, nomacro, and volatile requests.  */
458 int set_noreorder;
459 int set_noat;
460 int set_nomacro;
461 int set_volatile;
462
463 /* The next branch instruction is a branch likely, not branch normal.  */
464 int mips_branch_likely;
465
466 /* Cached operands, and operator to compare for use in set/branch/trap
467    on condition codes.  */
468 rtx branch_cmp[2];
469
470 /* what type of branch to use */
471 enum cmp_type branch_type;
472
473 /* The target cpu for code generation.  */
474 enum processor_type mips_arch;
475 const struct mips_cpu_info *mips_arch_info;
476
477 /* The target cpu for optimization and scheduling.  */
478 enum processor_type mips_tune;
479 const struct mips_cpu_info *mips_tune_info;
480
481 /* Which instruction set architecture to use.  */
482 int mips_isa;
483
484 /* Which ABI to use.  */
485 int mips_abi;
486
487 /* Strings to hold which cpu and instruction set architecture to use.  */
488 const char *mips_arch_string;   /* for -march=<xxx> */
489 const char *mips_tune_string;   /* for -mtune=<xxx> */
490 const char *mips_isa_string;    /* for -mips{1,2,3,4} */
491 const char *mips_abi_string;    /* for -mabi={32,n32,64,eabi} */
492
493 /* Whether we are generating mips16 hard float code.  In mips16 mode
494    we always set TARGET_SOFT_FLOAT; this variable is nonzero if
495    -msoft-float was not specified by the user, which means that we
496    should arrange to call mips32 hard floating point code.  */
497 int mips16_hard_float;
498
499 const char *mips_cache_flush_func = CACHE_FLUSH_FUNC;
500
501 /* If TRUE, we split addresses into their high and low parts in the RTL.  */
502 int mips_split_addresses;
503
504 /* Mode used for saving/restoring general purpose registers.  */
505 static enum machine_mode gpr_mode;
506
507 /* Array giving truth value on whether or not a given hard register
508    can support a given mode.  */
509 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
510
511 /* The length of all strings seen when compiling for the mips16.  This
512    is used to tell how many strings are in the constant pool, so that
513    we can see if we may have an overflow.  This is reset each time the
514    constant pool is output.  */
515 int mips_string_length;
516
517 /* When generating mips16 code, a list of all strings that are to be
518    output after the current function.  */
519
520 static GTY(()) rtx mips16_strings;
521
522 /* In mips16 mode, we build a list of all the string constants we see
523    in a particular function.  */
524
525 struct string_constant
526 {
527   struct string_constant *next;
528   const char *label;
529 };
530
531 static struct string_constant *string_constants;
532
533 /* List of all MIPS punctuation characters used by print_operand.  */
534 char mips_print_operand_punct[256];
535
536 /* Map GCC register number to debugger register number.  */
537 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
538
539 /* A copy of the original flag_delayed_branch: see override_options.  */
540 static int mips_flag_delayed_branch;
541
542 static GTY (()) int mips_output_filename_first_time = 1;
543
544 /* mips_split_p[X] is true if symbols of type X can be split by
545    mips_split_symbol().  */
546 static bool mips_split_p[NUM_SYMBOL_TYPES];
547
548 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
549    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
550    if they are matched by a special .md file pattern.  */
551 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
552
553 /* Likewise for HIGHs.  */
554 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
555
556 /* Hardware names for the registers.  If -mrnames is used, this
557    will be overwritten with mips_sw_reg_names.  */
558
559 char mips_reg_names[][8] =
560 {
561  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
562  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
563  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
564  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
565  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
566  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
567  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
568  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
569  "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
570  "$fcc5","$fcc6","$fcc7","", "", "$arg", "$frame", "$fakec",
571  "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
572  "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
573  "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
574  "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
575  "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
576  "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
577  "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
578  "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
579  "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
580  "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
581  "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
582  "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
583 };
584
585 /* Mips software names for the registers, used to overwrite the
586    mips_reg_names array.  */
587
588 char mips_sw_reg_names[][8] =
589 {
590   "$zero","$at",  "$v0",  "$v1",  "$a0",  "$a1",  "$a2",  "$a3",
591   "$t0",  "$t1",  "$t2",  "$t3",  "$t4",  "$t5",  "$t6",  "$t7",
592   "$s0",  "$s1",  "$s2",  "$s3",  "$s4",  "$s5",  "$s6",  "$s7",
593   "$t8",  "$t9",  "$k0",  "$k1",  "$gp",  "$sp",  "$fp",  "$ra",
594   "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
595   "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
596   "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
597   "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
598   "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
599   "$fcc5","$fcc6","$fcc7","$rap", "", "$arg", "$frame", "$fakec",
600   "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
601   "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
602   "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
603   "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
604   "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
605   "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
606   "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
607   "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
608   "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
609   "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
610   "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
611   "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
612 };
613
614 /* Map hard register number to register class */
615 const enum reg_class mips_regno_to_class[] =
616 {
617   LEA_REGS,     LEA_REGS,       M16_NA_REGS,    M16_NA_REGS,
618   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
619   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
620   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
621   M16_NA_REGS,  M16_NA_REGS,    LEA_REGS,       LEA_REGS,
622   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
623   T_REG,        PIC_FN_ADDR_REG, LEA_REGS,      LEA_REGS,
624   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
625   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
626   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
627   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
628   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
629   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
630   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
631   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
632   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
633   HI_REG,       LO_REG,         NO_REGS,        ST_REGS,
634   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
635   ST_REGS,      ST_REGS,        ST_REGS,        NO_REGS,
636   NO_REGS,      ALL_REGS,       ALL_REGS,       NO_REGS,
637   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
638   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
639   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
640   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
641   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
642   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
643   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
644   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
645   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
646   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
647   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
648   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
649   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
650   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
651   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
652   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
653   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
654   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
655   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
656   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
657   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
658   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
659   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
660   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS
661 };
662
663 /* Map register constraint character to register class.  */
664 enum reg_class mips_char_to_class[256];
665 \f
666 /* A table describing all the processors gcc knows about.  Names are
667    matched in the order listed.  The first mention of an ISA level is
668    taken as the canonical name for that ISA.
669
670    To ease comparison, please keep this table in the same order as
671    gas's mips_cpu_info_table[].  */
672 const struct mips_cpu_info mips_cpu_info_table[] = {
673   /* Entries for generic ISAs */
674   { "mips1", PROCESSOR_R3000, 1 },
675   { "mips2", PROCESSOR_R6000, 2 },
676   { "mips3", PROCESSOR_R4000, 3 },
677   { "mips4", PROCESSOR_R8000, 4 },
678   { "mips32", PROCESSOR_4KC, 32 },
679   { "mips32r2", PROCESSOR_M4K, 33 },
680   { "mips64", PROCESSOR_5KC, 64 },
681
682   /* MIPS I */
683   { "r3000", PROCESSOR_R3000, 1 },
684   { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
685   { "r3900", PROCESSOR_R3900, 1 },
686
687   /* MIPS II */
688   { "r6000", PROCESSOR_R6000, 2 },
689
690   /* MIPS III */
691   { "r4000", PROCESSOR_R4000, 3 },
692   { "vr4100", PROCESSOR_R4100, 3 },
693   { "vr4111", PROCESSOR_R4111, 3 },
694   { "vr4120", PROCESSOR_R4120, 3 },
695   { "vr4300", PROCESSOR_R4300, 3 },
696   { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
697   { "r4600", PROCESSOR_R4600, 3 },
698   { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
699   { "r4650", PROCESSOR_R4650, 3 },
700
701   /* MIPS IV */
702   { "r8000", PROCESSOR_R8000, 4 },
703   { "vr5000", PROCESSOR_R5000, 4 },
704   { "vr5400", PROCESSOR_R5400, 4 },
705   { "vr5500", PROCESSOR_R5500, 4 },
706   { "rm7000", PROCESSOR_R7000, 4 },
707   { "rm9000", PROCESSOR_R9000, 4 },
708
709   /* MIPS32 */
710   { "4kc", PROCESSOR_4KC, 32 },
711   { "4kp", PROCESSOR_4KC, 32 }, /* = 4kc */
712
713   /* MIPS32 Release 2 */
714   { "m4k", PROCESSOR_M4K, 33 },
715
716   /* MIPS64 */
717   { "5kc", PROCESSOR_5KC, 64 },
718   { "20kc", PROCESSOR_20KC, 64 },
719   { "sb1", PROCESSOR_SB1, 64 },
720   { "sr71000", PROCESSOR_SR71000, 64 },
721
722   /* End marker */
723   { 0, 0, 0 }
724 };
725 \f
726 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT.  */
727 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
728 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
729 #endif
730 \f
731 /* Initialize the GCC target structure.  */
732 #undef TARGET_ASM_ALIGNED_HI_OP
733 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
734 #undef TARGET_ASM_ALIGNED_SI_OP
735 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
736 #undef TARGET_ASM_INTEGER
737 #define TARGET_ASM_INTEGER mips_assemble_integer
738
739 #undef TARGET_ASM_FUNCTION_PROLOGUE
740 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
741 #undef TARGET_ASM_FUNCTION_EPILOGUE
742 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
743 #undef TARGET_ASM_SELECT_RTX_SECTION
744 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
745
746 #undef TARGET_SCHED_ADJUST_COST
747 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
748 #undef TARGET_SCHED_ISSUE_RATE
749 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
750 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
751 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE mips_use_dfa_pipeline_interface
752
753 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
754 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
755
756 #undef TARGET_VALID_POINTER_MODE
757 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
758 #undef TARGET_RTX_COSTS
759 #define TARGET_RTX_COSTS mips_rtx_costs
760 #undef TARGET_ADDRESS_COST
761 #define TARGET_ADDRESS_COST mips_address_cost
762
763 #undef TARGET_ENCODE_SECTION_INFO
764 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
765 #undef TARGET_IN_SMALL_DATA_P
766 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
767
768 #undef TARGET_MACHINE_DEPENDENT_REORG
769 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
770
771 #undef TARGET_ASM_FILE_START
772 #undef TARGET_ASM_FILE_END
773 #if TARGET_IRIX
774 #define TARGET_ASM_FILE_START irix_file_start
775 #define TARGET_ASM_FILE_END irix_file_end
776 #else
777 #define TARGET_ASM_FILE_START mips_file_start
778 #define TARGET_ASM_FILE_END mips_file_end
779 #endif
780 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
781 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
782
783 #if TARGET_IRIX
784 #undef TARGET_SECTION_TYPE_FLAGS
785 #define TARGET_SECTION_TYPE_FLAGS irix_section_type_flags
786 #endif
787
788 #undef TARGET_INIT_LIBFUNCS
789 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
790
791 #undef TARGET_BUILD_BUILTIN_VA_LIST
792 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
793
794 #undef TARGET_PROMOTE_FUNCTION_ARGS
795 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
796 #undef TARGET_PROMOTE_FUNCTION_RETURN
797 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
798 #undef TARGET_PROMOTE_PROTOTYPES
799 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
800
801 #undef TARGET_RETURN_IN_MEMORY
802 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
803 #undef TARGET_RETURN_IN_MSB
804 #define TARGET_RETURN_IN_MSB mips_return_in_msb
805
806 #undef TARGET_ASM_OUTPUT_MI_THUNK
807 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
808 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
809 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
810
811 #undef TARGET_SETUP_INCOMING_VARARGS
812 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
813 #undef TARGET_STRICT_ARGUMENT_NAMING
814 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
815
816 struct gcc_target targetm = TARGET_INITIALIZER;
817 \f
818 /* Classify symbol X, which must be a SYMBOL_REF or a LABEL_REF.  */
819
820 static enum mips_symbol_type
821 mips_classify_symbol (rtx x)
822 {
823   if (GET_CODE (x) == LABEL_REF)
824     return (TARGET_ABICALLS ? SYMBOL_GOT_LOCAL : SYMBOL_GENERAL);
825
826   if (GET_CODE (x) != SYMBOL_REF)
827     abort ();
828
829   if (CONSTANT_POOL_ADDRESS_P (x))
830     {
831       if (TARGET_MIPS16)
832         return SYMBOL_CONSTANT_POOL;
833
834       if (TARGET_ABICALLS)
835         return SYMBOL_GOT_LOCAL;
836
837       if (GET_MODE_SIZE (get_pool_mode (x)) <= mips_section_threshold)
838         return SYMBOL_SMALL_DATA;
839
840       return SYMBOL_GENERAL;
841     }
842
843   if (SYMBOL_REF_SMALL_P (x))
844     return SYMBOL_SMALL_DATA;
845
846   /* When generating mips16 code, SYMBOL_REF_FLAG indicates a string
847      in the current function's constant pool.  */
848   if (TARGET_MIPS16 && SYMBOL_REF_FLAG (x))
849     return SYMBOL_CONSTANT_POOL;
850
851   if (TARGET_ABICALLS)
852     {
853       if (SYMBOL_REF_DECL (x) == 0)
854         return SYMBOL_REF_LOCAL_P (x) ? SYMBOL_GOT_LOCAL : SYMBOL_GOT_GLOBAL;
855
856       /* There are three cases to consider:
857
858             - o32 PIC (either with or without explicit relocs)
859             - n32/n64 PIC without explicit relocs
860             - n32/n64 PIC with explicit relocs
861
862          In the first case, both local and global accesses will use an
863          R_MIPS_GOT16 relocation.  We must correctly predict which of
864          the two semantics (local or global) the assembler and linker
865          will apply.  The choice doesn't depend on the symbol's
866          visibility, so we deliberately ignore decl_visibility and
867          binds_local_p here.
868
869          In the second case, the assembler will not use R_MIPS_GOT16
870          relocations, but it chooses between local and global accesses
871          in the same way as for o32 PIC.
872
873          In the third case we have more freedom since both forms of
874          access will work for any kind of symbol.  However, there seems
875          little point in doing things differently.  */
876       if (DECL_P (SYMBOL_REF_DECL (x)) && TREE_PUBLIC (SYMBOL_REF_DECL (x)))
877         return SYMBOL_GOT_GLOBAL;
878
879       return SYMBOL_GOT_LOCAL;
880     }
881
882   return SYMBOL_GENERAL;
883 }
884
885
886 /* Split X into a base and a constant offset, storing them in *BASE
887    and *OFFSET respectively.  */
888
889 static void
890 mips_split_const (rtx x, rtx *base, HOST_WIDE_INT *offset)
891 {
892   *offset = 0;
893
894   if (GET_CODE (x) == CONST)
895     x = XEXP (x, 0);
896
897   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
898     {
899       *offset += INTVAL (XEXP (x, 1));
900       x = XEXP (x, 0);
901     }
902   *base = x;
903 }
904
905
906 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
907    to the same object as SYMBOL.  */
908
909 static bool
910 mips_offset_within_object_p (rtx symbol, HOST_WIDE_INT offset)
911 {
912   if (GET_CODE (symbol) != SYMBOL_REF)
913     return false;
914
915   if (CONSTANT_POOL_ADDRESS_P (symbol)
916       && offset >= 0
917       && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
918     return true;
919
920   if (SYMBOL_REF_DECL (symbol) != 0
921       && offset >= 0
922       && offset < int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (symbol))))
923     return true;
924
925   return false;
926 }
927
928
929 /* Return true if X is a symbolic constant that can be calculated in
930    the same way as a bare symbol.  If it is, store the type of the
931    symbol in *SYMBOL_TYPE.  */
932
933 static bool
934 mips_symbolic_constant_p (rtx x, enum mips_symbol_type *symbol_type)
935 {
936   HOST_WIDE_INT offset;
937
938   mips_split_const (x, &x, &offset);
939   if (UNSPEC_ADDRESS_P (x))
940     *symbol_type = UNSPEC_ADDRESS_TYPE (x);
941   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
942     *symbol_type = mips_classify_symbol (x);
943   else
944     return false;
945
946   if (offset == 0)
947     return true;
948
949   /* Check whether a nonzero offset is valid for the underlying
950      relocations.  */
951   switch (*symbol_type)
952     {
953     case SYMBOL_GENERAL:
954       /* If the target has 64-bit pointers and the object file only
955          supports 32-bit symbols, the values of those symbols will be
956          sign-extended.  In this case we can't allow an arbitrary offset
957          in case the 32-bit value X + OFFSET has a different sign from X.  */
958       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
959         return mips_offset_within_object_p (x, offset);
960
961       /* In other cases the relocations can handle any offset.  */
962       return true;
963
964     case SYMBOL_SMALL_DATA:
965     case SYMBOL_CONSTANT_POOL:
966       /* Make sure that the offset refers to something within the
967          underlying object.  This should guarantee that the final
968          PC- or GP-relative offset is within the 16-bit limit.  */
969       return mips_offset_within_object_p (x, offset);
970
971     case SYMBOL_GOT_LOCAL:
972     case SYMBOL_GOTOFF_PAGE:
973       /* The linker should provide enough local GOT entries for a
974          16-bit offset.  Larger offsets may lead to GOT overflow.  */
975       return SMALL_OPERAND (offset);
976
977     case SYMBOL_GOT_GLOBAL:
978     case SYMBOL_GOTOFF_GLOBAL:
979     case SYMBOL_GOTOFF_CALL:
980     case SYMBOL_GOTOFF_LOADGP:
981       return false;
982     }
983   abort ();
984 }
985
986
987 /* This function is used to implement REG_MODE_OK_FOR_BASE_P.  */
988
989 int
990 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode, int strict)
991 {
992   if (regno >= FIRST_PSEUDO_REGISTER)
993     {
994       if (!strict)
995         return true;
996       regno = reg_renumber[regno];
997     }
998
999   /* These fake registers will be eliminated to either the stack or
1000      hard frame pointer, both of which are usually valid base registers.
1001      Reload deals with the cases where the eliminated form isn't valid.  */
1002   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1003     return true;
1004
1005   /* In mips16 mode, the stack pointer can only address word and doubleword
1006      values, nothing smaller.  There are two problems here:
1007
1008        (a) Instantiating virtual registers can introduce new uses of the
1009            stack pointer.  If these virtual registers are valid addresses,
1010            the stack pointer should be too.
1011
1012        (b) Most uses of the stack pointer are not made explicit until
1013            FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1014            We don't know until that stage whether we'll be eliminating to the
1015            stack pointer (which needs the restriction) or the hard frame
1016            pointer (which doesn't).
1017
1018      All in all, it seems more consistent to only enforce this restriction
1019      during and after reload.  */
1020   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1021     return !strict || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1022
1023   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1024 }
1025
1026
1027 /* Return true if X is a valid base register for the given mode.
1028    Allow only hard registers if STRICT.  */
1029
1030 static bool
1031 mips_valid_base_register_p (rtx x, enum machine_mode mode, int strict)
1032 {
1033   if (!strict && GET_CODE (x) == SUBREG)
1034     x = SUBREG_REG (x);
1035
1036   return (GET_CODE (x) == REG
1037           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict));
1038 }
1039
1040
1041 /* Return true if symbols of type SYMBOL_TYPE can directly address a value
1042    with mode MODE.  This is used for both symbolic and LO_SUM addresses.  */
1043
1044 static bool
1045 mips_symbolic_address_p (enum mips_symbol_type symbol_type,
1046                          enum machine_mode mode)
1047 {
1048   switch (symbol_type)
1049     {
1050     case SYMBOL_GENERAL:
1051       return !TARGET_MIPS16;
1052
1053     case SYMBOL_SMALL_DATA:
1054       return true;
1055
1056     case SYMBOL_CONSTANT_POOL:
1057       /* PC-relative addressing is only available for lw, sw, ld and sd.  */
1058       return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1059
1060     case SYMBOL_GOT_LOCAL:
1061       return true;
1062
1063     case SYMBOL_GOT_GLOBAL:
1064       /* The address will have to be loaded from the GOT first.  */
1065       return false;
1066
1067     case SYMBOL_GOTOFF_PAGE:
1068     case SYMBOL_GOTOFF_GLOBAL:
1069     case SYMBOL_GOTOFF_CALL:
1070     case SYMBOL_GOTOFF_LOADGP:
1071       return true;
1072     }
1073   abort ();
1074 }
1075
1076
1077 /* Return true if X is a valid address for machine mode MODE.  If it is,
1078    fill in INFO appropriately.  STRICT is true if we should only accept
1079    hard base registers.  */
1080
1081 static bool
1082 mips_classify_address (struct mips_address_info *info, rtx x,
1083                        enum machine_mode mode, int strict)
1084 {
1085   switch (GET_CODE (x))
1086     {
1087     case REG:
1088     case SUBREG:
1089       info->type = ADDRESS_REG;
1090       info->reg = x;
1091       info->offset = const0_rtx;
1092       return mips_valid_base_register_p (info->reg, mode, strict);
1093
1094     case PLUS:
1095       info->type = ADDRESS_REG;
1096       info->reg = XEXP (x, 0);
1097       info->offset = XEXP (x, 1);
1098       return (mips_valid_base_register_p (info->reg, mode, strict)
1099               && const_arith_operand (info->offset, VOIDmode));
1100
1101     case LO_SUM:
1102       info->type = ADDRESS_LO_SUM;
1103       info->reg = XEXP (x, 0);
1104       info->offset = XEXP (x, 1);
1105       return (mips_valid_base_register_p (info->reg, mode, strict)
1106               && mips_symbolic_constant_p (info->offset, &info->symbol_type)
1107               && mips_symbolic_address_p (info->symbol_type, mode)
1108               && mips_lo_relocs[info->symbol_type] != 0);
1109
1110     case CONST_INT:
1111       /* Small-integer addresses don't occur very often, but they
1112          are legitimate if $0 is a valid base register.  */
1113       info->type = ADDRESS_CONST_INT;
1114       return !TARGET_MIPS16 && SMALL_INT (x);
1115
1116     case CONST:
1117     case LABEL_REF:
1118     case SYMBOL_REF:
1119       info->type = ADDRESS_SYMBOLIC;
1120       return (mips_symbolic_constant_p (x, &info->symbol_type)
1121               && mips_symbolic_address_p (info->symbol_type, mode)
1122               && !mips_split_p[info->symbol_type]);
1123
1124     default:
1125       return false;
1126     }
1127 }
1128 \f
1129 /* Return the number of instructions needed to load a symbol of the
1130    given type into a register.  If valid in an address, the same number
1131    of instructions are needed for loads and stores.  Treat extended
1132    mips16 instructions as two instructions.  */
1133
1134 static int
1135 mips_symbol_insns (enum mips_symbol_type type)
1136 {
1137   switch (type)
1138     {
1139     case SYMBOL_GENERAL:
1140       /* In mips16 code, general symbols must be fetched from the
1141          constant pool.  */
1142       if (TARGET_MIPS16)
1143         return 0;
1144
1145       /* When using 64-bit symbols, we need 5 preparatory instructions,
1146          such as:
1147
1148              lui     $at,%highest(symbol)
1149              daddiu  $at,$at,%higher(symbol)
1150              dsll    $at,$at,16
1151              daddiu  $at,$at,%hi(symbol)
1152              dsll    $at,$at,16
1153
1154          The final address is then $at + %lo(symbol).  With 32-bit
1155          symbols we just need a preparatory lui.  */
1156       return (ABI_HAS_64BIT_SYMBOLS ? 6 : 2);
1157
1158     case SYMBOL_SMALL_DATA:
1159       return 1;
1160
1161     case SYMBOL_CONSTANT_POOL:
1162       /* This case is for mips16 only.  Assume we'll need an
1163          extended instruction.  */
1164       return 2;
1165
1166     case SYMBOL_GOT_LOCAL:
1167     case SYMBOL_GOT_GLOBAL:
1168       /* Unless -funit-at-a-time is in effect, we can't be sure whether
1169          the local/global classification is accurate.  See override_options
1170          for details.
1171
1172          The worst cases are:
1173
1174          (1) For local symbols when generating o32 or o64 code.  The assembler
1175              will use:
1176
1177                  lw           $at,%got(symbol)
1178                  nop
1179
1180              ...and the final address will be $at + %lo(symbol).
1181
1182          (2) For global symbols when -mxgot.  The assembler will use:
1183
1184                  lui     $at,%got_hi(symbol)
1185                  (d)addu $at,$at,$gp
1186
1187              ...and the final address will be $at + %got_lo(symbol).  */
1188       return 3;
1189
1190     case SYMBOL_GOTOFF_PAGE:
1191     case SYMBOL_GOTOFF_GLOBAL:
1192     case SYMBOL_GOTOFF_CALL:
1193     case SYMBOL_GOTOFF_LOADGP:
1194       /* Check whether the offset is a 16- or 32-bit value.  */
1195       return mips_split_p[type] ? 2 : 1;
1196     }
1197   abort ();
1198 }
1199
1200
1201 /* Return true if a value at OFFSET bytes from BASE can be accessed
1202    using an unextended mips16 instruction.  MODE is the mode of the
1203    value.
1204
1205    Usually the offset in an unextended instruction is a 5-bit field.
1206    The offset is unsigned and shifted left once for HIs, twice
1207    for SIs, and so on.  An exception is SImode accesses off the
1208    stack pointer, which have an 8-bit immediate field.  */
1209
1210 static bool
1211 mips16_unextended_reference_p (enum machine_mode mode, rtx base, rtx offset)
1212 {
1213   if (TARGET_MIPS16
1214       && GET_CODE (offset) == CONST_INT
1215       && INTVAL (offset) >= 0
1216       && (INTVAL (offset) & (GET_MODE_SIZE (mode) - 1)) == 0)
1217     {
1218       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1219         return INTVAL (offset) < 256 * GET_MODE_SIZE (mode);
1220       return INTVAL (offset) < 32 * GET_MODE_SIZE (mode);
1221     }
1222   return false;
1223 }
1224
1225
1226 /* Return the number of instructions needed to load or store a value
1227    of mode MODE at X.  Return 0 if X isn't valid for MODE.
1228
1229    For mips16 code, count extended instructions as two instructions.  */
1230
1231 int
1232 mips_address_insns (rtx x, enum machine_mode mode)
1233 {
1234   struct mips_address_info addr;
1235   int factor;
1236
1237   if (mode == BLKmode)
1238     /* BLKmode is used for single unaligned loads and stores.  */
1239     factor = 1;
1240   else
1241     /* Each word of a multi-word value will be accessed individually.  */
1242     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1243
1244   if (mips_classify_address (&addr, x, mode, false))
1245     switch (addr.type)
1246       {
1247       case ADDRESS_REG:
1248         if (TARGET_MIPS16
1249             && !mips16_unextended_reference_p (mode, addr.reg, addr.offset))
1250           return factor * 2;
1251         return factor;
1252
1253       case ADDRESS_LO_SUM:
1254         return (TARGET_MIPS16 ? factor * 2 : factor);
1255
1256       case ADDRESS_CONST_INT:
1257         return factor;
1258
1259       case ADDRESS_SYMBOLIC:
1260         return factor * mips_symbol_insns (addr.symbol_type);
1261       }
1262   return 0;
1263 }
1264
1265
1266 /* Likewise for constant X.  */
1267
1268 int
1269 mips_const_insns (rtx x)
1270 {
1271   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1272   enum mips_symbol_type symbol_type;
1273   HOST_WIDE_INT offset;
1274
1275   switch (GET_CODE (x))
1276     {
1277     case CONSTANT_P_RTX:
1278       return 1;
1279
1280     case HIGH:
1281       if (TARGET_MIPS16
1282           || !mips_symbolic_constant_p (XEXP (x, 0), &symbol_type)
1283           || !mips_split_p[symbol_type])
1284         return 0;
1285
1286       return 1;
1287
1288     case CONST_INT:
1289       if (TARGET_MIPS16)
1290         /* Unsigned 8-bit constants can be loaded using an unextended
1291            LI instruction.  Unsigned 16-bit constants can be loaded
1292            using an extended LI.  Negative constants must be loaded
1293            using LI and then negated.  */
1294         return (INTVAL (x) >= 0 && INTVAL (x) < 256 ? 1
1295                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
1296                 : INTVAL (x) > -256 && INTVAL (x) < 0 ? 2
1297                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
1298                 : 0);
1299
1300       return mips_build_integer (codes, INTVAL (x));
1301
1302     case CONST_DOUBLE:
1303       return (!TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0);
1304
1305     case CONST:
1306       if (CONST_GP_P (x))
1307         return 1;
1308
1309       /* See if we can refer to X directly.  */
1310       if (mips_symbolic_constant_p (x, &symbol_type))
1311         return mips_symbol_insns (symbol_type);
1312
1313       /* Otherwise try splitting the constant into a base and offset.
1314          16-bit offsets can be added using an extra addiu.  Larger offsets
1315          must be calculated separately and then added to the base.  */
1316       mips_split_const (x, &x, &offset);
1317       if (offset != 0)
1318         {
1319           int n = mips_const_insns (x);
1320           if (n != 0)
1321             {
1322               if (SMALL_OPERAND (offset))
1323                 return n + 1;
1324               else
1325                 return n + 1 + mips_build_integer (codes, offset);
1326             }
1327         }
1328       return 0;
1329
1330     case SYMBOL_REF:
1331     case LABEL_REF:
1332       return mips_symbol_insns (mips_classify_symbol (x));
1333
1334     default:
1335       return 0;
1336     }
1337 }
1338
1339
1340 /* Return the number of instructions needed for memory reference X.
1341    Count extended mips16 instructions as two instructions.  */
1342
1343 int
1344 mips_fetch_insns (rtx x)
1345 {
1346   if (GET_CODE (x) != MEM)
1347     abort ();
1348
1349   return mips_address_insns (XEXP (x, 0), GET_MODE (x));
1350 }
1351
1352
1353 /* Return the number of instructions needed for an integer division.  */
1354
1355 int
1356 mips_idiv_insns (void)
1357 {
1358   int count;
1359
1360   count = 1;
1361   if (TARGET_CHECK_ZERO_DIV)
1362     {
1363       if (TARGET_MIPS16)
1364         count += 2;
1365       else
1366         count += 3;
1367     }
1368   if (TARGET_FIX_R4000)
1369     count++;
1370   return count;
1371 }
1372
1373
1374 /* Return truth value of whether OP can be used as an operands
1375    where a register or 16 bit unsigned integer is needed.  */
1376
1377 int
1378 uns_arith_operand (rtx op, enum machine_mode mode)
1379 {
1380   if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
1381     return 1;
1382
1383   return register_operand (op, mode);
1384 }
1385
1386
1387 /* True if OP can be treated as a signed 16-bit constant.  */
1388
1389 int
1390 const_arith_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1391 {
1392   return GET_CODE (op) == CONST_INT && SMALL_INT (op);
1393 }
1394
1395
1396 /* Return true if OP is a register operand or a signed 16-bit constant.  */
1397
1398 int
1399 arith_operand (rtx op, enum machine_mode mode)
1400 {
1401   return const_arith_operand (op, mode) || register_operand (op, mode);
1402 }
1403
1404 /* Return truth value of whether OP is an integer which fits in 16 bits.  */
1405
1406 int
1407 small_int (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1408 {
1409   return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
1410 }
1411
1412 /* Return truth value of whether OP is a register or the constant 0.
1413    Do not accept 0 in mips16 mode since $0 is not one of the core 8
1414    registers.  */
1415
1416 int
1417 reg_or_0_operand (rtx op, enum machine_mode mode)
1418 {
1419   switch (GET_CODE (op))
1420     {
1421     case CONST_INT:
1422       if (TARGET_MIPS16)
1423         return 0;
1424       return INTVAL (op) == 0;
1425
1426     case CONST_DOUBLE:
1427       if (TARGET_MIPS16)
1428         return 0;
1429       return op == CONST0_RTX (mode);
1430
1431     default:
1432       return register_operand (op, mode);
1433     }
1434 }
1435
1436 /* Accept a register or the floating point constant 1 in the appropriate mode.  */
1437
1438 int
1439 reg_or_const_float_1_operand (rtx op, enum machine_mode mode)
1440 {
1441   REAL_VALUE_TYPE d;
1442
1443   switch (GET_CODE (op))
1444     {
1445     case CONST_DOUBLE:
1446       if (mode != GET_MODE (op)
1447           || (mode != DFmode && mode != SFmode))
1448         return 0;
1449
1450       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1451       return REAL_VALUES_EQUAL (d, dconst1);
1452
1453     default:
1454       return register_operand (op, mode);
1455     }
1456 }
1457
1458 /* Accept the floating point constant 1 in the appropriate mode.  */
1459
1460 int
1461 const_float_1_operand (rtx op, enum machine_mode mode)
1462 {
1463   REAL_VALUE_TYPE d;
1464
1465   if (GET_CODE (op) != CONST_DOUBLE
1466       || mode != GET_MODE (op)
1467       || (mode != DFmode && mode != SFmode))
1468     return 0;
1469
1470   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1471
1472   return REAL_VALUES_EQUAL (d, dconst1);
1473 }
1474
1475 /* Return true if OP is either the HI or LO register.  */
1476
1477 int
1478 hilo_operand (rtx op, enum machine_mode mode)
1479 {
1480   return ((mode == VOIDmode || mode == GET_MODE (op))
1481           && REG_P (op) && MD_REG_P (REGNO (op)));
1482 }
1483
1484 /* Return true if OP is an extension operator.  */
1485
1486 int
1487 extend_operator (rtx op, enum machine_mode mode)
1488 {
1489   return ((mode == VOIDmode || mode == GET_MODE (op))
1490           && (GET_CODE (op) == ZERO_EXTEND || GET_CODE (op) == SIGN_EXTEND));
1491 }
1492
1493 /* Return nonzero if the code of this rtx pattern is EQ or NE.  */
1494
1495 int
1496 equality_op (rtx op, enum machine_mode mode)
1497 {
1498   if (mode != GET_MODE (op))
1499     return 0;
1500
1501   return GET_CODE (op) == EQ || GET_CODE (op) == NE;
1502 }
1503
1504 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
1505
1506 int
1507 cmp_op (rtx op, enum machine_mode mode)
1508 {
1509   if (mode != GET_MODE (op))
1510     return 0;
1511
1512   return GET_RTX_CLASS (GET_CODE (op)) == '<';
1513 }
1514
1515 /* Return nonzero if the code is a relational operation suitable for a
1516    conditional trap instruction (only EQ, NE, LT, LTU, GE, GEU).
1517    We need this in the insn that expands `trap_if' in order to prevent
1518    combine from erroneously altering the condition.  */
1519
1520 int
1521 trap_cmp_op (rtx op, enum machine_mode mode)
1522 {
1523   if (mode != GET_MODE (op))
1524     return 0;
1525
1526   switch (GET_CODE (op))
1527     {
1528     case EQ:
1529     case NE:
1530     case LT:
1531     case LTU:
1532     case GE:
1533     case GEU:
1534       return 1;
1535
1536     default:
1537       return 0;
1538     }
1539 }
1540
1541 /* Return nonzero if the operand is either the PC or a label_ref.  */
1542
1543 int
1544 pc_or_label_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1545 {
1546   if (op == pc_rtx)
1547     return 1;
1548
1549   if (GET_CODE (op) == LABEL_REF)
1550     return 1;
1551
1552   return 0;
1553 }
1554
1555 /* Test for a valid call address.  */
1556
1557 int
1558 call_insn_operand (rtx op, enum machine_mode mode)
1559 {
1560   enum mips_symbol_type symbol_type;
1561
1562   if (mips_symbolic_constant_p (op, &symbol_type))
1563     switch (symbol_type)
1564       {
1565       case SYMBOL_GENERAL:
1566         /* If -mlong-calls, force all calls to use register addressing.  */
1567         return !TARGET_LONG_CALLS;
1568
1569       case SYMBOL_GOT_GLOBAL:
1570         /* Without explicit relocs, there is no special syntax for
1571            loading the address of a call destination into a register.
1572            Using "la $25,foo; jal $25" would prevent the lazy binding
1573            of "foo", so keep the address of global symbols with the
1574            jal macro.  */
1575         return !TARGET_EXPLICIT_RELOCS;
1576
1577       default:
1578         return false;
1579       }
1580   return register_operand (op, mode);
1581 }
1582
1583
1584 /* Return nonzero if OP is valid as a source operand for a move
1585    instruction.  */
1586
1587 int
1588 move_operand (rtx op, enum machine_mode mode)
1589 {
1590   enum mips_symbol_type symbol_type;
1591
1592   if (!general_operand (op, mode))
1593     return false;
1594
1595   switch (GET_CODE (op))
1596     {
1597     case CONST_INT:
1598       /* When generating mips16 code, LEGITIMATE_CONSTANT_P rejects
1599          CONST_INTs that can't be loaded using simple insns.  */
1600       if (TARGET_MIPS16)
1601         return true;
1602
1603       /* Otherwise check whether the constant can be loaded in a single
1604          instruction.  */
1605       return LUI_INT (op) || SMALL_INT (op) || SMALL_INT_UNSIGNED (op);
1606
1607     case CONST:
1608     case SYMBOL_REF:
1609     case LABEL_REF:
1610       if (CONST_GP_P (op))
1611         return true;
1612
1613       return (mips_symbolic_constant_p (op, &symbol_type)
1614               && !mips_split_p[symbol_type]);
1615
1616     default:
1617       return true;
1618     }
1619 }
1620
1621
1622 /* Accept any operand that can appear in a mips16 constant table
1623    instruction.  We can't use any of the standard operand functions
1624    because for these instructions we accept values that are not
1625    accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs.  */
1626
1627 int
1628 consttable_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1629 {
1630   return CONSTANT_P (op);
1631 }
1632
1633 /* Return 1 if OP is a symbolic operand, i.e. a symbol_ref or a label_ref,
1634    possibly with an offset.  */
1635
1636 int
1637 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1638 {
1639   enum mips_symbol_type symbol_type;
1640
1641   return mips_symbolic_constant_p (op, &symbol_type);
1642 }
1643
1644
1645 /* Return true if we're generating PIC and OP is a global symbol.  */
1646
1647 int
1648 global_got_operand (rtx op, enum machine_mode mode)
1649 {
1650   enum mips_symbol_type symbol_type;
1651
1652   return ((mode == VOIDmode || mode == GET_MODE (op))
1653           && mips_symbolic_constant_p (op, &symbol_type)
1654           && symbol_type == SYMBOL_GOT_GLOBAL);
1655 }
1656
1657
1658 /* Likewise for local symbols.  */
1659
1660 int
1661 local_got_operand (rtx op, enum machine_mode mode)
1662 {
1663   enum mips_symbol_type symbol_type;
1664
1665   return ((mode == VOIDmode || mode == GET_MODE (op))
1666           && mips_symbolic_constant_p (op, &symbol_type)
1667           && symbol_type == SYMBOL_GOT_LOCAL);
1668 }
1669
1670
1671 /* Return true if OP is a memory reference that uses the stack pointer
1672    as a base register.  */
1673
1674 int
1675 stack_operand (rtx op, enum machine_mode mode)
1676 {
1677   struct mips_address_info addr;
1678
1679   return ((mode == VOIDmode || mode == GET_MODE (op))
1680           && GET_CODE (op) == MEM
1681           && mips_classify_address (&addr, XEXP (op, 0), GET_MODE (op), false)
1682           && addr.type == ADDRESS_REG
1683           && addr.reg == stack_pointer_rtx);
1684 }
1685
1686
1687 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS.  It
1688    returns a nonzero value if X is a legitimate address for a memory
1689    operand of the indicated MODE.  STRICT is nonzero if this function
1690    is called during reload.  */
1691
1692 bool
1693 mips_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1694 {
1695   struct mips_address_info addr;
1696
1697   return mips_classify_address (&addr, x, mode, strict);
1698 }
1699
1700
1701 /* Copy VALUE to a register and return that register.  If new psuedos
1702    are allowed, copy it into a new register, otherwise use DEST.  */
1703
1704 static rtx
1705 mips_force_temporary (rtx dest, rtx value)
1706 {
1707   if (!no_new_pseudos)
1708     return force_reg (Pmode, value);
1709   else
1710     {
1711       emit_move_insn (copy_rtx (dest), value);
1712       return dest;
1713     }
1714 }
1715
1716
1717 /* Return a LO_SUM expression for ADDR.  TEMP is as for mips_force_temporary
1718    and is used to load the high part into a register.  */
1719
1720 static rtx
1721 mips_split_symbol (rtx temp, rtx addr)
1722 {
1723   rtx high;
1724
1725   if (TARGET_MIPS16)
1726     high = mips16_gp_pseudo_reg ();
1727   else
1728     high = mips_force_temporary (temp, gen_rtx_HIGH (Pmode, copy_rtx (addr)));
1729   return gen_rtx_LO_SUM (Pmode, high, addr);
1730 }
1731
1732
1733 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1734    type SYMBOL_TYPE.  */
1735
1736 static rtx
1737 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
1738 {
1739   rtx base;
1740   HOST_WIDE_INT offset;
1741
1742   mips_split_const (address, &base, &offset);
1743   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1744                          UNSPEC_ADDRESS_FIRST + symbol_type);
1745   return plus_constant (gen_rtx_CONST (Pmode, base), offset);
1746 }
1747
1748
1749 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1750    high part to BASE and return the result.  Just return BASE otherwise.
1751    TEMP is available as a temporary register if needed.
1752
1753    The returned expression can be used as the first operand to a LO_SUM.  */
1754
1755 static rtx
1756 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
1757                          enum mips_symbol_type symbol_type)
1758 {
1759   if (mips_split_p[symbol_type])
1760     {
1761       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
1762       addr = mips_force_temporary (temp, addr);
1763       return mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
1764     }
1765   return base;
1766 }
1767
1768
1769 /* Return the offset of a GOT page entry for local address ADDR.  */
1770
1771 rtx
1772 mips_gotoff_page (rtx addr)
1773 {
1774   return mips_unspec_address (addr, SYMBOL_GOTOFF_PAGE);
1775 }
1776
1777
1778 /* Return the offset of ADDR's GOT entry from _gp.  ADDR is a
1779    global_got_operand.  */
1780
1781 rtx
1782 mips_gotoff_global (rtx addr)
1783 {
1784   return mips_unspec_address (addr, SYMBOL_GOTOFF_GLOBAL);
1785 }
1786
1787
1788 /* Return a legitimate address for REG + OFFSET.  This function will
1789    create a temporary register if OFFSET is not a SMALL_OPERAND.  */
1790
1791 static rtx
1792 mips_add_offset (rtx reg, HOST_WIDE_INT offset)
1793 {
1794   if (!SMALL_OPERAND (offset))
1795     reg = expand_simple_binop (GET_MODE (reg), PLUS,
1796                                GEN_INT (CONST_HIGH_PART (offset)),
1797                                reg, NULL, 0, OPTAB_WIDEN);
1798
1799   return plus_constant (reg, CONST_LOW_PART (offset));
1800 }
1801
1802
1803 /* This function is used to implement LEGITIMIZE_ADDRESS.  If *XLOC can
1804    be legitimized in a way that the generic machinery might not expect,
1805    put the new address in *XLOC and return true.  MODE is the mode of
1806    the memory being accessed.  */
1807
1808 bool
1809 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
1810 {
1811   enum mips_symbol_type symbol_type;
1812
1813   /* See if the address can split into a high part and a LO_SUM.  */
1814   if (mips_symbolic_constant_p (*xloc, &symbol_type)
1815       && mips_symbolic_address_p (symbol_type, mode)
1816       && mips_split_p[symbol_type])
1817     {
1818       *xloc = mips_split_symbol (0, *xloc);
1819       return true;
1820     }
1821
1822   if (GET_CODE (*xloc) == PLUS && GET_CODE (XEXP (*xloc, 1)) == CONST_INT)
1823     {
1824       /* Handle REG + CONSTANT using mips_add_offset.  */
1825       rtx reg;
1826
1827       reg = XEXP (*xloc, 0);
1828       if (!mips_valid_base_register_p (reg, mode, 0))
1829         reg = copy_to_mode_reg (Pmode, reg);
1830       *xloc = mips_add_offset (reg, INTVAL (XEXP (*xloc, 1)));
1831       return true;
1832     }
1833
1834   return false;
1835 }
1836
1837
1838 /* Subroutine of mips_build_integer (with the same interface).
1839    Assume that the final action in the sequence should be a left shift.  */
1840
1841 static unsigned int
1842 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1843 {
1844   unsigned int i, shift;
1845
1846   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1847      since signed numbers are easier to load than unsigned ones.  */
1848   shift = 0;
1849   while ((value & 1) == 0)
1850     value /= 2, shift++;
1851
1852   i = mips_build_integer (codes, value);
1853   codes[i].code = ASHIFT;
1854   codes[i].value = shift;
1855   return i + 1;
1856 }
1857
1858
1859 /* As for mips_build_shift, but assume that the final action will be
1860    an IOR or PLUS operation.  */
1861
1862 static unsigned int
1863 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1864 {
1865   unsigned HOST_WIDE_INT high;
1866   unsigned int i;
1867
1868   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1869   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1870     {
1871       /* The constant is too complex to load with a simple lui/ori pair
1872          so our goal is to clear as many trailing zeros as possible.
1873          In this case, we know bit 16 is set and that the low 16 bits
1874          form a negative number.  If we subtract that number from VALUE,
1875          we will clear at least the lowest 17 bits, maybe more.  */
1876       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1877       codes[i].code = PLUS;
1878       codes[i].value = CONST_LOW_PART (value);
1879     }
1880   else
1881     {
1882       i = mips_build_integer (codes, high);
1883       codes[i].code = IOR;
1884       codes[i].value = value & 0xffff;
1885     }
1886   return i + 1;
1887 }
1888
1889
1890 /* Fill CODES with a sequence of rtl operations to load VALUE.
1891    Return the number of operations needed.  */
1892
1893 static unsigned int
1894 mips_build_integer (struct mips_integer_op *codes,
1895                     unsigned HOST_WIDE_INT value)
1896 {
1897   if (SMALL_OPERAND (value)
1898       || SMALL_OPERAND_UNSIGNED (value)
1899       || LUI_OPERAND (value))
1900     {
1901       /* The value can be loaded with a single instruction.  */
1902       codes[0].code = NIL;
1903       codes[0].value = value;
1904       return 1;
1905     }
1906   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1907     {
1908       /* Either the constant is a simple LUI/ORI combination or its
1909          lowest bit is set.  We don't want to shift in this case.  */
1910       return mips_build_lower (codes, value);
1911     }
1912   else if ((value & 0xffff) == 0)
1913     {
1914       /* The constant will need at least three actions.  The lowest
1915          16 bits are clear, so the final action will be a shift.  */
1916       return mips_build_shift (codes, value);
1917     }
1918   else
1919     {
1920       /* The final action could be a shift, add or inclusive OR.
1921          Rather than use a complex condition to select the best
1922          approach, try both mips_build_shift and mips_build_lower
1923          and pick the one that gives the shortest sequence.
1924          Note that this case is only used once per constant.  */
1925       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1926       unsigned int cost, alt_cost;
1927
1928       cost = mips_build_shift (codes, value);
1929       alt_cost = mips_build_lower (alt_codes, value);
1930       if (alt_cost < cost)
1931         {
1932           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1933           cost = alt_cost;
1934         }
1935       return cost;
1936     }
1937 }
1938
1939
1940 /* Move VALUE into register DEST.  */
1941
1942 static void
1943 mips_move_integer (rtx dest, unsigned HOST_WIDE_INT value)
1944 {
1945   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1946   enum machine_mode mode;
1947   unsigned int i, cost;
1948   rtx x;
1949
1950   mode = GET_MODE (dest);
1951   cost = mips_build_integer (codes, value);
1952
1953   /* Apply each binary operation to X.  Invariant: X is a legitimate
1954      source operand for a SET pattern.  */
1955   x = GEN_INT (codes[0].value);
1956   for (i = 1; i < cost; i++)
1957     {
1958       if (no_new_pseudos)
1959         emit_move_insn (dest, x), x = dest;
1960       else
1961         x = force_reg (mode, x);
1962       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1963     }
1964
1965   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1966 }
1967
1968
1969 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
1970    DEST given that SRC satisfies immediate_operand but doesn't satisfy
1971    move_operand.  */
1972
1973 static void
1974 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
1975 {
1976   rtx base;
1977   HOST_WIDE_INT offset;
1978   enum mips_symbol_type symbol_type;
1979
1980   /* Split moves of big integers into smaller pieces.  In mips16 code,
1981      it's better to force the constant into memory instead.  */
1982   if (GET_CODE (src) == CONST_INT && !TARGET_MIPS16)
1983     {
1984       mips_move_integer (dest, INTVAL (src));
1985       return;
1986     }
1987
1988   /* See if the symbol can be split.  For mips16, this is often worse than
1989      forcing it in the constant pool since it needs the single-register form
1990      of addiu or daddiu.  */
1991   if (!TARGET_MIPS16
1992       && mips_symbolic_constant_p (src, &symbol_type)
1993       && mips_split_p[symbol_type])
1994     {
1995       emit_move_insn (dest, mips_split_symbol (dest, src));
1996       return;
1997     }
1998
1999   /* If we have (const (plus symbol offset)), load the symbol first
2000      and then add in the offset.  This is usually better than forcing
2001      the constant into memory, at least in non-mips16 code.  */
2002   mips_split_const (src, &base, &offset);
2003   if (!TARGET_MIPS16
2004       && offset != 0
2005       && (!no_new_pseudos || SMALL_OPERAND (offset)))
2006     {
2007       base = mips_force_temporary (dest, base);
2008       emit_move_insn (dest, mips_add_offset (base, offset));
2009       return;
2010     }
2011
2012   src = force_const_mem (mode, src);
2013
2014   /* When using explicit relocs, constant pool references are sometimes
2015      not legitimate addresses.  */
2016   if (!memory_operand (src, VOIDmode))
2017     src = replace_equiv_address (src, mips_split_symbol (dest, XEXP (src, 0)));
2018   emit_move_insn (dest, src);
2019 }
2020
2021
2022 /* If (set DEST SRC) is not a valid instruction, emit an equivalent
2023    sequence that is valid.  */
2024
2025 bool
2026 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2027 {
2028   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2029     {
2030       emit_move_insn (dest, force_reg (mode, src));
2031       return true;
2032     }
2033
2034   /* The source of an SImode move must be a move_operand.  Likewise
2035      DImode moves on 64-bit targets.  We need to deal with constants
2036      that would be legitimate immediate_operands but not legitimate
2037      move_operands.  */
2038   if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2039       && CONSTANT_P (src)
2040       && !move_operand (src, mode))
2041     {
2042       mips_legitimize_const_move (mode, dest, src);
2043       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2044       return true;
2045     }
2046   return false;
2047 }
2048 \f
2049 /* We need a lot of little routines to check constant values on the
2050    mips16.  These are used to figure out how long the instruction will
2051    be.  It would be much better to do this using constraints, but
2052    there aren't nearly enough letters available.  */
2053
2054 static int
2055 m16_check_op (rtx op, int low, int high, int mask)
2056 {
2057   return (GET_CODE (op) == CONST_INT
2058           && INTVAL (op) >= low
2059           && INTVAL (op) <= high
2060           && (INTVAL (op) & mask) == 0);
2061 }
2062
2063 int
2064 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2065 {
2066   return m16_check_op (op, 0x1, 0x8, 0);
2067 }
2068
2069 int
2070 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2071 {
2072   return m16_check_op (op, - 0x8, 0x7, 0);
2073 }
2074
2075 int
2076 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2077 {
2078   return m16_check_op (op, - 0x7, 0x8, 0);
2079 }
2080
2081 int
2082 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2083 {
2084   return m16_check_op (op, - 0x10, 0xf, 0);
2085 }
2086
2087 int
2088 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2089 {
2090   return m16_check_op (op, - 0xf, 0x10, 0);
2091 }
2092
2093 int
2094 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2095 {
2096   return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
2097 }
2098
2099 int
2100 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2101 {
2102   return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
2103 }
2104
2105 int
2106 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2107 {
2108   return m16_check_op (op, - 0x80, 0x7f, 0);
2109 }
2110
2111 int
2112 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2113 {
2114   return m16_check_op (op, - 0x7f, 0x80, 0);
2115 }
2116
2117 int
2118 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2119 {
2120   return m16_check_op (op, 0x0, 0xff, 0);
2121 }
2122
2123 int
2124 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2125 {
2126   return m16_check_op (op, - 0xff, 0x0, 0);
2127 }
2128
2129 int
2130 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2131 {
2132   return m16_check_op (op, - 0x1, 0xfe, 0);
2133 }
2134
2135 int
2136 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2137 {
2138   return m16_check_op (op, 0x0, 0xff << 2, 3);
2139 }
2140
2141 int
2142 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2143 {
2144   return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
2145 }
2146
2147 int
2148 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2149 {
2150   return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
2151 }
2152
2153 int
2154 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2155 {
2156   return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
2157 }
2158
2159 /* References to the string table on the mips16 only use a small
2160    offset if the function is small.  We can't check for LABEL_REF here,
2161    because the offset is always large if the label is before the
2162    referencing instruction.  */
2163
2164 int
2165 m16_usym8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2166 {
2167   if (GET_CODE (op) == SYMBOL_REF
2168       && SYMBOL_REF_FLAG (op)
2169       && cfun->machine->insns_len > 0
2170       && (cfun->machine->insns_len + get_pool_size () + mips_string_length
2171           < 4 * 0x100))
2172     {
2173       struct string_constant *l;
2174
2175       /* Make sure this symbol is on thelist of string constants to be
2176          output for this function.  It is possible that it has already
2177          been output, in which case this requires a large offset.  */
2178       for (l = string_constants; l != NULL; l = l->next)
2179         if (strcmp (l->label, XSTR (op, 0)) == 0)
2180           return 1;
2181     }
2182
2183   return 0;
2184 }
2185
2186 int
2187 m16_usym5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2188 {
2189   if (GET_CODE (op) == SYMBOL_REF
2190       && SYMBOL_REF_FLAG (op)
2191       && cfun->machine->insns_len > 0
2192       && (cfun->machine->insns_len + get_pool_size () + mips_string_length
2193           < 4 * 0x20))
2194     {
2195       struct string_constant *l;
2196
2197       /* Make sure this symbol is on thelist of string constants to be
2198          output for this function.  It is possible that it has already
2199          been output, in which case this requires a large offset.  */
2200       for (l = string_constants; l != NULL; l = l->next)
2201         if (strcmp (l->label, XSTR (op, 0)) == 0)
2202           return 1;
2203     }
2204
2205   return 0;
2206 }
2207 \f
2208 static bool
2209 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
2210 {
2211   enum machine_mode mode = GET_MODE (x);
2212
2213   switch (code)
2214     {
2215     case CONST_INT:
2216       if (!TARGET_MIPS16)
2217         {
2218           /* Always return 0, since we don't have different sized
2219              instructions, hence different costs according to Richard
2220              Kenner */
2221           *total = 0;
2222           return true;
2223         }
2224
2225       /* A number between 1 and 8 inclusive is efficient for a shift.
2226          Otherwise, we will need an extended instruction.  */
2227       if ((outer_code) == ASHIFT || (outer_code) == ASHIFTRT
2228           || (outer_code) == LSHIFTRT)
2229         {
2230           if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
2231             *total = 0;
2232           else
2233             *total = COSTS_N_INSNS (1);
2234           return true;
2235         }
2236
2237       /* We can use cmpi for an xor with an unsigned 16 bit value.  */
2238       if ((outer_code) == XOR
2239           && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
2240         {
2241           *total = 0;
2242           return true;
2243         }
2244
2245       /* We may be able to use slt or sltu for a comparison with a
2246          signed 16 bit value.  (The boundary conditions aren't quite
2247          right, but this is just a heuristic anyhow.)  */
2248       if (((outer_code) == LT || (outer_code) == LE
2249            || (outer_code) == GE || (outer_code) == GT
2250            || (outer_code) == LTU || (outer_code) == LEU
2251            || (outer_code) == GEU || (outer_code) == GTU)
2252           && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
2253         {
2254           *total = 0;
2255           return true;
2256         }
2257
2258       /* Equality comparisons with 0 are cheap.  */
2259       if (((outer_code) == EQ || (outer_code) == NE)
2260           && INTVAL (x) == 0)
2261         {
2262           *total = 0;
2263           return true;
2264         }
2265
2266       /* Otherwise fall through to the handling below.  */
2267
2268     case CONST:
2269     case SYMBOL_REF:
2270     case LABEL_REF:
2271     case CONST_DOUBLE:
2272       if (LEGITIMATE_CONSTANT_P (x))
2273         {
2274           *total = COSTS_N_INSNS (1);
2275           return true;
2276         }
2277       else
2278         {
2279           /* The value will need to be fetched from the constant pool.  */
2280           *total = CONSTANT_POOL_COST;
2281           return true;
2282         }
2283
2284     case MEM:
2285       {
2286         /* If the address is legitimate, return the number of
2287            instructions it needs, otherwise use the default handling.  */
2288         int n = mips_address_insns (XEXP (x, 0), GET_MODE (x));
2289         if (n > 0)
2290           {
2291             *total = COSTS_N_INSNS (1 + n);
2292             return true;
2293           }
2294         return false;
2295       }
2296
2297     case FFS:
2298       *total = COSTS_N_INSNS (6);
2299       return true;
2300
2301     case NOT:
2302       *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
2303       return true;
2304
2305     case AND:
2306     case IOR:
2307     case XOR:
2308       if (mode == DImode && !TARGET_64BIT)
2309         {
2310           *total = COSTS_N_INSNS (2);
2311           return true;
2312         }
2313       return false;
2314
2315     case ASHIFT:
2316     case ASHIFTRT:
2317     case LSHIFTRT:
2318       if (mode == DImode && !TARGET_64BIT)
2319         {
2320           *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
2321                                   ? 4 : 12);
2322           return true;
2323         }
2324       return false;
2325
2326     case ABS:
2327       if (mode == SFmode || mode == DFmode)
2328         *total = COSTS_N_INSNS (1);
2329       else
2330         *total = COSTS_N_INSNS (4);
2331       return true;
2332
2333     case LO_SUM:
2334       *total = COSTS_N_INSNS (1);
2335       return true;
2336
2337     case PLUS:
2338     case MINUS:
2339       if (mode == SFmode || mode == DFmode)
2340         {
2341           if (TUNE_MIPS3000 || TUNE_MIPS3900)
2342             *total = COSTS_N_INSNS (2);
2343           else if (TUNE_MIPS6000)
2344             *total = COSTS_N_INSNS (3);
2345           else
2346             *total = COSTS_N_INSNS (6);
2347           return true;
2348         }
2349       if (mode == DImode && !TARGET_64BIT)
2350         {
2351           *total = COSTS_N_INSNS (4);
2352           return true;
2353         }
2354       return false;
2355
2356     case NEG:
2357       if (mode == DImode && !TARGET_64BIT)
2358         {
2359           *total = 4;
2360           return true;
2361         }
2362       return false;
2363
2364     case MULT:
2365       if (mode == SFmode)
2366         {
2367           if (TUNE_MIPS3000
2368               || TUNE_MIPS3900
2369               || TUNE_MIPS5000)
2370             *total = COSTS_N_INSNS (4);
2371           else if (TUNE_MIPS6000
2372                    || TUNE_MIPS5400
2373                    || TUNE_MIPS5500)
2374             *total = COSTS_N_INSNS (5);
2375           else
2376             *total = COSTS_N_INSNS (7);
2377           return true;
2378         }
2379
2380       if (mode == DFmode)
2381         {
2382           if (TUNE_MIPS3000
2383               || TUNE_MIPS3900
2384               || TUNE_MIPS5000)
2385             *total = COSTS_N_INSNS (5);
2386           else if (TUNE_MIPS6000
2387                    || TUNE_MIPS5400
2388                    || TUNE_MIPS5500)
2389             *total = COSTS_N_INSNS (6);
2390           else
2391             *total = COSTS_N_INSNS (8);
2392           return true;
2393         }
2394
2395       if (TUNE_MIPS3000)
2396         *total = COSTS_N_INSNS (12);
2397       else if (TUNE_MIPS3900)
2398         *total = COSTS_N_INSNS (2);
2399       else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2400         *total = COSTS_N_INSNS ((mode == DImode) ? 4 : 3);
2401       else if (TUNE_MIPS7000)
2402         *total = COSTS_N_INSNS (mode == DImode ? 9 : 5);
2403       else if (TUNE_MIPS9000)
2404         *total = COSTS_N_INSNS (mode == DImode ? 8 : 3);
2405       else if (TUNE_MIPS6000)
2406         *total = COSTS_N_INSNS (17);
2407       else if (TUNE_MIPS5000)
2408         *total = COSTS_N_INSNS (5);
2409       else
2410         *total = COSTS_N_INSNS (10);
2411       return true;
2412
2413     case DIV:
2414     case MOD:
2415       if (mode == SFmode)
2416         {
2417           if (TUNE_MIPS3000
2418               || TUNE_MIPS3900)
2419             *total = COSTS_N_INSNS (12);
2420           else if (TUNE_MIPS6000)
2421             *total = COSTS_N_INSNS (15);
2422           else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2423             *total = COSTS_N_INSNS (30);
2424           else
2425             *total = COSTS_N_INSNS (23);
2426           return true;
2427         }
2428
2429       if (mode == DFmode)
2430         {
2431           if (TUNE_MIPS3000
2432               || TUNE_MIPS3900)
2433             *total = COSTS_N_INSNS (19);
2434           else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2435             *total = COSTS_N_INSNS (59);
2436           else if (TUNE_MIPS6000)
2437             *total = COSTS_N_INSNS (16);
2438           else
2439             *total = COSTS_N_INSNS (36);
2440           return true;
2441         }
2442       /* Fall through.  */
2443
2444     case UDIV:
2445     case UMOD:
2446       if (TUNE_MIPS3000
2447           || TUNE_MIPS3900)
2448         *total = COSTS_N_INSNS (35);
2449       else if (TUNE_MIPS6000)
2450         *total = COSTS_N_INSNS (38);
2451       else if (TUNE_MIPS5000)
2452         *total = COSTS_N_INSNS (36);
2453       else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2454         *total = COSTS_N_INSNS ((mode == SImode) ? 42 : 74);
2455       else
2456         *total = COSTS_N_INSNS (69);
2457       return true;
2458
2459     case SIGN_EXTEND:
2460       /* A sign extend from SImode to DImode in 64 bit mode is often
2461          zero instructions, because the result can often be used
2462          directly by another instruction; we'll call it one.  */
2463       if (TARGET_64BIT && mode == DImode
2464           && GET_MODE (XEXP (x, 0)) == SImode)
2465         *total = COSTS_N_INSNS (1);
2466       else
2467         *total = COSTS_N_INSNS (2);
2468       return true;
2469
2470     case ZERO_EXTEND:
2471       if (TARGET_64BIT && mode == DImode
2472           && GET_MODE (XEXP (x, 0)) == SImode)
2473         *total = COSTS_N_INSNS (2);
2474       else
2475         *total = COSTS_N_INSNS (1);
2476       return true;
2477
2478     default:
2479       return false;
2480     }
2481 }
2482
2483 /* Provide the costs of an addressing mode that contains ADDR.
2484    If ADDR is not a valid address, its cost is irrelevant.  */
2485
2486 static int
2487 mips_address_cost (rtx addr)
2488 {
2489   return mips_address_insns (addr, SImode);
2490 }
2491 \f
2492 /* Return a pseudo that points to the address of the current function.
2493    The first time it is called for a function, an initializer for the
2494    pseudo is emitted in the beginning of the function.  */
2495
2496 rtx
2497 embedded_pic_fnaddr_reg (void)
2498 {
2499   if (cfun->machine->embedded_pic_fnaddr_rtx == NULL)
2500     {
2501       rtx seq;
2502
2503       cfun->machine->embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
2504
2505       /* Output code at function start to initialize the pseudo-reg.  */
2506       /* ??? We used to do this in FINALIZE_PIC, but that does not work for
2507          inline functions, because it is called after RTL for the function
2508          has been copied.  The pseudo-reg in embedded_pic_fnaddr_rtx however
2509          does not get copied, and ends up not matching the rest of the RTL.
2510          This solution works, but means that we get unnecessary code to
2511          initialize this value every time a function is inlined into another
2512          function.  */
2513       start_sequence ();
2514       emit_insn (gen_get_fnaddr (cfun->machine->embedded_pic_fnaddr_rtx,
2515                                  XEXP (DECL_RTL (current_function_decl), 0)));
2516       seq = get_insns ();
2517       end_sequence ();
2518       push_topmost_sequence ();
2519       emit_insn_after (seq, get_insns ());
2520       pop_topmost_sequence ();
2521     }
2522
2523   return cfun->machine->embedded_pic_fnaddr_rtx;
2524 }
2525
2526 /* Return RTL for the offset from the current function to the argument.
2527    X is the symbol whose offset from the current function we want.  */
2528
2529 rtx
2530 embedded_pic_offset (rtx x)
2531 {
2532   /* Make sure it is emitted.  */
2533   embedded_pic_fnaddr_reg ();
2534
2535   return
2536     gen_rtx_CONST (Pmode,
2537                    gen_rtx_MINUS (Pmode, x,
2538                                   XEXP (DECL_RTL (current_function_decl), 0)));
2539 }
2540 \f
2541 /* Return one word of double-word value OP, taking into account the fixed
2542    endianness of certain registers.  HIGH_P is true to select the high part,
2543    false to select the low part.  */
2544
2545 rtx
2546 mips_subword (rtx op, int high_p)
2547 {
2548   unsigned int byte;
2549   enum machine_mode mode;
2550
2551   mode = GET_MODE (op);
2552   if (mode == VOIDmode)
2553     mode = DImode;
2554
2555   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
2556     byte = UNITS_PER_WORD;
2557   else
2558     byte = 0;
2559
2560   if (GET_CODE (op) == REG)
2561     {
2562       if (FP_REG_P (REGNO (op)))
2563         return gen_rtx_REG (word_mode, high_p ? REGNO (op) + 1 : REGNO (op));
2564       if (REGNO (op) == HI_REGNUM)
2565         return gen_rtx_REG (word_mode, high_p ? HI_REGNUM : LO_REGNUM);
2566     }
2567
2568   if (GET_CODE (op) == MEM)
2569     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2570
2571   return simplify_gen_subreg (word_mode, op, mode, byte);
2572 }
2573
2574
2575 /* Return true if a 64-bit move from SRC to DEST should be split into two.  */
2576
2577 bool
2578 mips_split_64bit_move_p (rtx dest, rtx src)
2579 {
2580   if (TARGET_64BIT)
2581     return false;
2582
2583   /* FP->FP moves can be done in a single instruction.  */
2584   if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2585     return false;
2586
2587   /* Check for floating-point loads and stores.  They can be done using
2588      ldc1 and sdc1 on MIPS II and above.  */
2589   if (mips_isa > 1)
2590     {
2591       if (FP_REG_RTX_P (dest) && GET_CODE (src) == MEM)
2592         return false;
2593       if (FP_REG_RTX_P (src) && GET_CODE (dest) == MEM)
2594         return false;
2595     }
2596   return true;
2597 }
2598
2599
2600 /* Split a 64-bit move from SRC to DEST assuming that
2601    mips_split_64bit_move_p holds.
2602
2603    Moves into and out of FPRs cause some difficulty here.  Such moves
2604    will always be DFmode, since paired FPRs are not allowed to store
2605    DImode values.  The most natural representation would be two separate
2606    32-bit moves, such as:
2607
2608         (set (reg:SI $f0) (mem:SI ...))
2609         (set (reg:SI $f1) (mem:SI ...))
2610
2611    However, the second insn is invalid because odd-numbered FPRs are
2612    not allowed to store independent values.  Use the patterns load_df_low,
2613    load_df_high and store_df_high instead.  */
2614
2615 void
2616 mips_split_64bit_move (rtx dest, rtx src)
2617 {
2618   if (FP_REG_RTX_P (dest))
2619     {
2620       /* Loading an FPR from memory or from GPRs.  */
2621       emit_insn (gen_load_df_low (copy_rtx (dest), mips_subword (src, 0)));
2622       emit_insn (gen_load_df_high (dest, mips_subword (src, 1),
2623                                    copy_rtx (dest)));
2624     }
2625   else if (FP_REG_RTX_P (src))
2626     {
2627       /* Storing an FPR into memory or GPRs.  */
2628       emit_move_insn (mips_subword (dest, 0), mips_subword (src, 0));
2629       emit_insn (gen_store_df_high (mips_subword (dest, 1), src));
2630     }
2631   else
2632     {
2633       /* The operation can be split into two normal moves.  Decide in
2634          which order to do them.  */
2635       rtx low_dest;
2636
2637       low_dest = mips_subword (dest, 0);
2638       if (GET_CODE (low_dest) == REG
2639           && reg_overlap_mentioned_p (low_dest, src))
2640         {
2641           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2642           emit_move_insn (low_dest, mips_subword (src, 0));
2643         }
2644       else
2645         {
2646           emit_move_insn (low_dest, mips_subword (src, 0));
2647           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2648         }
2649     }
2650 }
2651 \f
2652 /* Return the appropriate instructions to move SRC into DEST.  Assume
2653    that SRC is operand 1 and DEST is operand 0.  */
2654
2655 const char *
2656 mips_output_move (rtx dest, rtx src)
2657 {
2658   enum rtx_code dest_code, src_code;
2659   bool dbl_p;
2660
2661   dest_code = GET_CODE (dest);
2662   src_code = GET_CODE (src);
2663   dbl_p = (GET_MODE_SIZE (GET_MODE (dest)) == 8);
2664
2665   if (dbl_p && mips_split_64bit_move_p (dest, src))
2666     return "#";
2667
2668   if ((src_code == REG && GP_REG_P (REGNO (src)))
2669       || (!TARGET_MIPS16 && src == CONST0_RTX (GET_MODE (dest))))
2670     {
2671       if (dest_code == REG)
2672         {
2673           if (GP_REG_P (REGNO (dest)))
2674             return "move\t%0,%z1";
2675
2676           if (MD_REG_P (REGNO (dest)))
2677             return "mt%0\t%z1";
2678
2679           if (FP_REG_P (REGNO (dest)))
2680             return (dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0");
2681
2682           if (ALL_COP_REG_P (REGNO (dest)))
2683             {
2684               static char retval[] = "dmtc_\t%z1,%0";
2685
2686               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2687               return (dbl_p ? retval : retval + 1);
2688             }
2689         }
2690       if (dest_code == MEM)
2691         return (dbl_p ? "sd\t%z1,%0" : "sw\t%z1,%0");
2692     }
2693   if (dest_code == REG && GP_REG_P (REGNO (dest)))
2694     {
2695       if (src_code == REG)
2696         {
2697           if (MD_REG_P (REGNO (src)))
2698             return "mf%1\t%0";
2699
2700           if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
2701             return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
2702
2703           if (FP_REG_P (REGNO (src)))
2704             return (dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1");
2705
2706           if (ALL_COP_REG_P (REGNO (src)))
2707             {
2708               static char retval[] = "dmfc_\t%0,%1";
2709
2710               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2711               return (dbl_p ? retval : retval + 1);
2712             }
2713         }
2714
2715       if (src_code == MEM)
2716         return (dbl_p ? "ld\t%0,%1" : "lw\t%0,%1");
2717
2718       if (src_code == CONST_INT)
2719         {
2720           /* Don't use the X format, because that will give out of
2721              range numbers for 64 bit hosts and 32 bit targets.  */
2722           if (!TARGET_MIPS16)
2723             return "li\t%0,%1\t\t\t# %X1";
2724
2725           if (INTVAL (src) >= 0 && INTVAL (src) <= 0xffff)
2726             return "li\t%0,%1";
2727
2728           if (INTVAL (src) < 0 && INTVAL (src) >= -0xffff)
2729             return "li\t%0,%n1\n\tneg\t%0";
2730         }
2731
2732       if (src_code == HIGH)
2733         return "lui\t%0,%h1";
2734
2735       if (CONST_GP_P (src))
2736         return "move\t%0,%1";
2737
2738       if (symbolic_operand (src, VOIDmode))
2739         return (dbl_p ? "dla\t%0,%1" : "la\t%0,%1");
2740     }
2741   if (src_code == REG && FP_REG_P (REGNO (src)))
2742     {
2743       if (dest_code == REG && FP_REG_P (REGNO (dest)))
2744         return (dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1");
2745
2746       if (dest_code == MEM)
2747         return (dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0");
2748     }
2749   if (dest_code == REG && FP_REG_P (REGNO (dest)))
2750     {
2751       if (src_code == MEM)
2752         return (dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1");
2753     }
2754   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
2755     {
2756       static char retval[] = "l_c_\t%0,%1";
2757
2758       retval[1] = (dbl_p ? 'd' : 'w');
2759       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2760       return retval;
2761     }
2762   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
2763     {
2764       static char retval[] = "s_c_\t%1,%0";
2765
2766       retval[1] = (dbl_p ? 'd' : 'w');
2767       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2768       return retval;
2769     }
2770   abort ();
2771 }
2772 \f
2773 /* Return an rtx for the gp save slot.  Valid only when using o32 or
2774    o64 abicalls.  */
2775
2776 rtx
2777 mips_gp_save_slot (void)
2778 {
2779   rtx loc;
2780
2781   if (!TARGET_ABICALLS || TARGET_NEWABI)
2782     abort ();
2783
2784   if (frame_pointer_needed)
2785     loc = hard_frame_pointer_rtx;
2786   else
2787     loc = stack_pointer_rtx;
2788   loc = plus_constant (loc, current_function_outgoing_args_size);
2789   loc = gen_rtx_MEM (Pmode, loc);
2790   RTX_UNCHANGING_P (loc) = 1;
2791   return loc;
2792 }
2793 \f
2794 /* Make normal rtx_code into something we can index from an array */
2795
2796 static enum internal_test
2797 map_test_to_internal_test (enum rtx_code test_code)
2798 {
2799   enum internal_test test = ITEST_MAX;
2800
2801   switch (test_code)
2802     {
2803     case EQ:  test = ITEST_EQ;  break;
2804     case NE:  test = ITEST_NE;  break;
2805     case GT:  test = ITEST_GT;  break;
2806     case GE:  test = ITEST_GE;  break;
2807     case LT:  test = ITEST_LT;  break;
2808     case LE:  test = ITEST_LE;  break;
2809     case GTU: test = ITEST_GTU; break;
2810     case GEU: test = ITEST_GEU; break;
2811     case LTU: test = ITEST_LTU; break;
2812     case LEU: test = ITEST_LEU; break;
2813     default:                    break;
2814     }
2815
2816   return test;
2817 }
2818
2819 \f
2820 /* Generate the code to compare two integer values.  The return value is:
2821    (reg:SI xx)          The pseudo register the comparison is in
2822    0                    No register, generate a simple branch.
2823
2824    ??? This is called with result nonzero by the Scond patterns in
2825    mips.md.  These patterns are called with a target in the mode of
2826    the Scond instruction pattern.  Since this must be a constant, we
2827    must use SImode.  This means that if RESULT is nonzero, it will
2828    always be an SImode register, even if TARGET_64BIT is true.  We
2829    cope with this by calling convert_move rather than emit_move_insn.
2830    This will sometimes lead to an unnecessary extension of the result;
2831    for example:
2832
2833    long long
2834    foo (long long i)
2835    {
2836      return i < 5;
2837    }
2838
2839    TEST_CODE is the rtx code for the comparison.
2840    CMP0 and CMP1 are the two operands to compare.
2841    RESULT is the register in which the result should be stored (null for
2842      branches).
2843    For branches, P_INVERT points to an integer that is nonzero on return
2844      if the branch should be inverted.  */
2845
2846 rtx
2847 gen_int_relational (enum rtx_code test_code, rtx result, rtx cmp0,
2848                     rtx cmp1, int *p_invert)
2849 {
2850   struct cmp_info
2851   {
2852     enum rtx_code test_code;    /* code to use in instruction (LT vs. LTU) */
2853     int const_low;              /* low bound of constant we can accept */
2854     int const_high;             /* high bound of constant we can accept */
2855     int const_add;              /* constant to add (convert LE -> LT) */
2856     int reverse_regs;           /* reverse registers in test */
2857     int invert_const;           /* != 0 if invert value if cmp1 is constant */
2858     int invert_reg;             /* != 0 if invert value if cmp1 is register */
2859     int unsignedp;              /* != 0 for unsigned comparisons.  */
2860   };
2861
2862   static const struct cmp_info info[ (int)ITEST_MAX ] = {
2863
2864     { XOR,       0,  65535,  0,  0,  0,  0, 0 },        /* EQ  */
2865     { XOR,       0,  65535,  0,  0,  1,  1, 0 },        /* NE  */
2866     { LT,   -32769,  32766,  1,  1,  1,  0, 0 },        /* GT  */
2867     { LT,   -32768,  32767,  0,  0,  1,  1, 0 },        /* GE  */
2868     { LT,   -32768,  32767,  0,  0,  0,  0, 0 },        /* LT  */
2869     { LT,   -32769,  32766,  1,  1,  0,  1, 0 },        /* LE  */
2870     { LTU,  -32769,  32766,  1,  1,  1,  0, 1 },        /* GTU */
2871     { LTU,  -32768,  32767,  0,  0,  1,  1, 1 },        /* GEU */
2872     { LTU,  -32768,  32767,  0,  0,  0,  0, 1 },        /* LTU */
2873     { LTU,  -32769,  32766,  1,  1,  0,  1, 1 },        /* LEU */
2874   };
2875
2876   enum internal_test test;
2877   enum machine_mode mode;
2878   const struct cmp_info *p_info;
2879   int branch_p;
2880   int eqne_p;
2881   int invert;
2882   rtx reg;
2883   rtx reg2;
2884
2885   test = map_test_to_internal_test (test_code);
2886   if (test == ITEST_MAX)
2887     abort ();
2888
2889   p_info = &info[(int) test];
2890   eqne_p = (p_info->test_code == XOR);
2891
2892   mode = GET_MODE (cmp0);
2893   if (mode == VOIDmode)
2894     mode = GET_MODE (cmp1);
2895
2896   /* Eliminate simple branches.  */
2897   branch_p = (result == 0);
2898   if (branch_p)
2899     {
2900       if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
2901         {
2902           /* Comparisons against zero are simple branches.  */
2903           if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
2904               && (! TARGET_MIPS16 || eqne_p))
2905             return 0;
2906
2907           /* Test for beq/bne.  */
2908           if (eqne_p && ! TARGET_MIPS16)
2909             return 0;
2910         }
2911
2912       /* Allocate a pseudo to calculate the value in.  */
2913       result = gen_reg_rtx (mode);
2914     }
2915
2916   /* Make sure we can handle any constants given to us.  */
2917   if (GET_CODE (cmp0) == CONST_INT)
2918     cmp0 = force_reg (mode, cmp0);
2919
2920   if (GET_CODE (cmp1) == CONST_INT)
2921     {
2922       HOST_WIDE_INT value = INTVAL (cmp1);
2923
2924       if (value < p_info->const_low
2925           || value > p_info->const_high
2926           /* ??? Why?  And why wasn't the similar code below modified too?  */
2927           || (TARGET_64BIT
2928               && HOST_BITS_PER_WIDE_INT < 64
2929               && p_info->const_add != 0
2930               && ((p_info->unsignedp
2931                    ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
2932                       > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
2933                    : (value + p_info->const_add) > INTVAL (cmp1))
2934                   != (p_info->const_add > 0))))
2935         cmp1 = force_reg (mode, cmp1);
2936     }
2937
2938   /* See if we need to invert the result.  */
2939   invert = (GET_CODE (cmp1) == CONST_INT
2940             ? p_info->invert_const : p_info->invert_reg);
2941
2942   if (p_invert != (int *)0)
2943     {
2944       *p_invert = invert;
2945       invert = 0;
2946     }
2947
2948   /* Comparison to constants, may involve adding 1 to change a LT into LE.
2949      Comparison between two registers, may involve switching operands.  */
2950   if (GET_CODE (cmp1) == CONST_INT)
2951     {
2952       if (p_info->const_add != 0)
2953         {
2954           HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
2955
2956           /* If modification of cmp1 caused overflow,
2957              we would get the wrong answer if we follow the usual path;
2958              thus, x > 0xffffffffU would turn into x > 0U.  */
2959           if ((p_info->unsignedp
2960                ? (unsigned HOST_WIDE_INT) new >
2961                (unsigned HOST_WIDE_INT) INTVAL (cmp1)
2962                : new > INTVAL (cmp1))
2963               != (p_info->const_add > 0))
2964             {
2965               /* This test is always true, but if INVERT is true then
2966                  the result of the test needs to be inverted so 0 should
2967                  be returned instead.  */
2968               emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
2969               return result;
2970             }
2971           else
2972             cmp1 = GEN_INT (new);
2973         }
2974     }
2975
2976   else if (p_info->reverse_regs)
2977     {
2978       rtx temp = cmp0;
2979       cmp0 = cmp1;
2980       cmp1 = temp;
2981     }
2982
2983   if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
2984     reg = cmp0;
2985   else
2986     {
2987       reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
2988       convert_move (reg, gen_rtx_fmt_ee (p_info->test_code,
2989                                          mode, cmp0, cmp1), 0);
2990     }
2991
2992   if (test == ITEST_NE)
2993     {
2994       if (! TARGET_MIPS16)
2995         {
2996           convert_move (result, gen_rtx_GTU (mode, reg, const0_rtx), 0);
2997           if (p_invert != NULL)
2998             *p_invert = 0;
2999           invert = 0;
3000         }
3001       else
3002         {
3003           reg2 = invert ? gen_reg_rtx (mode) : result;
3004           convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3005           reg = reg2;
3006         }
3007     }
3008
3009   else if (test == ITEST_EQ)
3010     {
3011       reg2 = invert ? gen_reg_rtx (mode) : result;
3012       convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3013       reg = reg2;
3014     }
3015
3016   if (invert)
3017     {
3018       rtx one;
3019
3020       if (! TARGET_MIPS16)
3021         one = const1_rtx;
3022       else
3023         {
3024           /* The value is in $24.  Copy it to another register, so
3025              that reload doesn't think it needs to store the $24 and
3026              the input to the XOR in the same location.  */
3027           reg2 = gen_reg_rtx (mode);
3028           emit_move_insn (reg2, reg);
3029           reg = reg2;
3030           one = force_reg (mode, const1_rtx);
3031         }
3032       convert_move (result, gen_rtx_XOR (mode, reg, one), 0);
3033     }
3034
3035   return result;
3036 }
3037 \f
3038 /* Work out how to check a floating-point condition.  We need a
3039    separate comparison instruction (C.cond.fmt), followed by a
3040    branch or conditional move.  Given that IN_CODE is the
3041    required condition, set *CMP_CODE to the C.cond.fmt code
3042    and *action_code to the branch or move code.  */
3043
3044 static void
3045 get_float_compare_codes (enum rtx_code in_code, enum rtx_code *cmp_code,
3046                          enum rtx_code *action_code)
3047 {
3048   switch (in_code)
3049     {
3050     case NE:
3051     case UNGE:
3052     case UNGT:
3053     case LTGT:
3054     case ORDERED:
3055       *cmp_code = reverse_condition_maybe_unordered (in_code);
3056       *action_code = EQ;
3057       break;
3058
3059     default:
3060       *cmp_code = in_code;
3061       *action_code = NE;
3062       break;
3063     }
3064 }
3065
3066 /* Emit the common code for doing conditional branches.
3067    operand[0] is the label to jump to.
3068    The comparison operands are saved away by cmp{si,di,sf,df}.  */
3069
3070 void
3071 gen_conditional_branch (rtx *operands, enum rtx_code test_code)
3072 {
3073   enum cmp_type type = branch_type;
3074   rtx cmp0 = branch_cmp[0];
3075   rtx cmp1 = branch_cmp[1];
3076   enum machine_mode mode;
3077   enum rtx_code cmp_code;
3078   rtx reg;
3079   int invert;
3080   rtx label1, label2;
3081
3082   switch (type)
3083     {
3084     case CMP_SI:
3085     case CMP_DI:
3086       mode = type == CMP_SI ? SImode : DImode;
3087       invert = 0;
3088       reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3089
3090       if (reg)
3091         {
3092           cmp0 = reg;
3093           cmp1 = const0_rtx;
3094           test_code = NE;
3095         }
3096       else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3097         /* We don't want to build a comparison against a nonzero
3098            constant.  */
3099         cmp1 = force_reg (mode, cmp1);
3100
3101       break;
3102
3103     case CMP_SF:
3104     case CMP_DF:
3105       if (! ISA_HAS_8CC)
3106         reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3107       else
3108         reg = gen_reg_rtx (CCmode);
3109
3110       get_float_compare_codes (test_code, &cmp_code, &test_code);
3111       emit_insn (gen_rtx_SET (VOIDmode, reg,
3112                               gen_rtx_fmt_ee (cmp_code, CCmode, cmp0, cmp1)));
3113
3114       mode = CCmode;
3115       cmp0 = reg;
3116       cmp1 = const0_rtx;
3117       invert = 0;
3118       break;
3119
3120     default:
3121       fatal_insn ("bad test",
3122                   gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
3123     }
3124
3125   /* Generate the branch.  */
3126
3127   label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3128   label2 = pc_rtx;
3129
3130   if (invert)
3131     {
3132       label2 = label1;
3133       label1 = pc_rtx;
3134     }
3135
3136   emit_jump_insn
3137     (gen_rtx_SET (VOIDmode, pc_rtx,
3138                   gen_rtx_IF_THEN_ELSE (VOIDmode,
3139                                         gen_rtx_fmt_ee (test_code, mode,
3140                                                         cmp0, cmp1),
3141                                         label1, label2)));
3142 }
3143
3144 /* Emit the common code for conditional moves.  OPERANDS is the array
3145    of operands passed to the conditional move define_expand.  */
3146
3147 void
3148 gen_conditional_move (rtx *operands)
3149 {
3150   rtx op0 = branch_cmp[0];
3151   rtx op1 = branch_cmp[1];
3152   enum machine_mode mode = GET_MODE (branch_cmp[0]);
3153   enum rtx_code cmp_code = GET_CODE (operands[1]);
3154   enum rtx_code move_code = NE;
3155   enum machine_mode op_mode = GET_MODE (operands[0]);
3156   enum machine_mode cmp_mode;
3157   rtx cmp_reg;
3158
3159   if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3160     {
3161       switch (cmp_code)
3162         {
3163         case EQ:
3164           cmp_code = XOR;
3165           move_code = EQ;
3166           break;
3167         case NE:
3168           cmp_code = XOR;
3169           break;
3170         case LT:
3171           break;
3172         case GE:
3173           cmp_code = LT;
3174           move_code = EQ;
3175           break;
3176         case GT:
3177           cmp_code = LT;
3178           op0 = force_reg (mode, branch_cmp[1]);
3179           op1 = branch_cmp[0];
3180           break;
3181         case LE:
3182           cmp_code = LT;
3183           op0 = force_reg (mode, branch_cmp[1]);
3184           op1 = branch_cmp[0];
3185           move_code = EQ;
3186           break;
3187         case LTU:
3188           break;
3189         case GEU:
3190           cmp_code = LTU;
3191           move_code = EQ;
3192           break;
3193         case GTU:
3194           cmp_code = LTU;
3195           op0 = force_reg (mode, branch_cmp[1]);
3196           op1 = branch_cmp[0];
3197           break;
3198         case LEU:
3199           cmp_code = LTU;
3200           op0 = force_reg (mode, branch_cmp[1]);
3201           op1 = branch_cmp[0];
3202           move_code = EQ;
3203           break;
3204         default:
3205           abort ();
3206         }
3207     }
3208   else
3209     get_float_compare_codes (cmp_code, &cmp_code, &move_code);
3210
3211   if (mode == SImode || mode == DImode)
3212     cmp_mode = mode;
3213   else if (mode == SFmode || mode == DFmode)
3214     cmp_mode = CCmode;
3215   else
3216     abort ();
3217
3218   cmp_reg = gen_reg_rtx (cmp_mode);
3219   emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3220                           gen_rtx_fmt_ee (cmp_code, cmp_mode, op0, op1)));
3221
3222   emit_insn (gen_rtx_SET (op_mode, operands[0],
3223                           gen_rtx_IF_THEN_ELSE (op_mode,
3224                                                 gen_rtx_fmt_ee (move_code,
3225                                                                 VOIDmode,
3226                                                                 cmp_reg,
3227                                                                 const0_rtx),
3228                                                 operands[2], operands[3])));
3229 }
3230
3231 /* Emit a conditional trap.  OPERANDS is the array of operands passed to
3232    the conditional_trap expander.  */
3233
3234 void
3235 mips_gen_conditional_trap (rtx *operands)
3236 {
3237   rtx op0, op1;
3238   enum rtx_code cmp_code = GET_CODE (operands[0]);
3239   enum machine_mode mode = GET_MODE (branch_cmp[0]);
3240
3241   /* MIPS conditional trap machine instructions don't have GT or LE
3242      flavors, so we must invert the comparison and convert to LT and
3243      GE, respectively.  */
3244   switch (cmp_code)
3245     {
3246     case GT: cmp_code = LT; break;
3247     case LE: cmp_code = GE; break;
3248     case GTU: cmp_code = LTU; break;
3249     case LEU: cmp_code = GEU; break;
3250     default: break;
3251     }
3252   if (cmp_code == GET_CODE (operands[0]))
3253     {
3254       op0 = force_reg (mode, branch_cmp[0]);
3255       op1 = branch_cmp[1];
3256     }
3257   else
3258     {
3259       op0 = force_reg (mode, branch_cmp[1]);
3260       op1 = branch_cmp[0];
3261     }
3262   if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3263     op1 = force_reg (mode, op1);
3264
3265   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3266                               gen_rtx_fmt_ee (cmp_code, GET_MODE (operands[0]),
3267                                               op0, op1),
3268                               operands[1]));
3269 }
3270 \f
3271 /* Load function address ADDR into register DEST.  SIBCALL_P is true
3272    if the address is needed for a sibling call.  */
3273
3274 static void
3275 mips_load_call_address (rtx dest, rtx addr, int sibcall_p)
3276 {
3277   /* If we're generating PIC, and this call is to a global function,
3278      try to allow its address to be resolved lazily.  This isn't
3279      possible for NewABI sibcalls since the value of $gp on entry
3280      to the stub would be our caller's gp, not ours.  */
3281   if (TARGET_EXPLICIT_RELOCS
3282       && !(sibcall_p && TARGET_NEWABI)
3283       && global_got_operand (addr, VOIDmode))
3284     {
3285       rtx high, lo_sum_symbol;
3286
3287       high = mips_unspec_offset_high (dest, pic_offset_table_rtx,
3288                                       addr, SYMBOL_GOTOFF_CALL);
3289       lo_sum_symbol = mips_unspec_address (addr, SYMBOL_GOTOFF_CALL);
3290       if (Pmode == SImode)
3291         emit_insn (gen_load_callsi (dest, high, lo_sum_symbol));
3292       else
3293         emit_insn (gen_load_calldi (dest, high, lo_sum_symbol));
3294     }
3295   else
3296     emit_move_insn (dest, addr);
3297 }
3298
3299
3300 /* Expand a call or call_value instruction.  RESULT is where the
3301    result will go (null for calls), ADDR is the address of the
3302    function, ARGS_SIZE is the size of the arguments and AUX is
3303    the value passed to us by mips_function_arg.  SIBCALL_P is true
3304    if we are expanding a sibling call, false if we're expanding
3305    a normal call.  */
3306
3307 void
3308 mips_expand_call (rtx result, rtx addr, rtx args_size, rtx aux, int sibcall_p)
3309 {
3310   rtx orig_addr, pattern, insn;
3311
3312   orig_addr = addr;
3313   if (!call_insn_operand (addr, VOIDmode))
3314     {
3315       addr = gen_reg_rtx (Pmode);
3316       mips_load_call_address (addr, orig_addr, sibcall_p);
3317     }
3318
3319   if (TARGET_MIPS16
3320       && mips16_hard_float
3321       && build_mips16_call_stub (result, addr, args_size,
3322                                  aux == 0 ? 0 : (int) GET_MODE (aux)))
3323     return;
3324
3325   if (result == 0)
3326     pattern = (sibcall_p
3327                ? gen_sibcall_internal (addr, args_size)
3328                : gen_call_internal (addr, args_size));
3329   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
3330     {
3331       rtx reg1, reg2;
3332
3333       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
3334       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
3335       pattern =
3336         (sibcall_p
3337          ? gen_sibcall_value_multiple_internal (reg1, addr, args_size, reg2)
3338          : gen_call_value_multiple_internal (reg1, addr, args_size, reg2));
3339     }
3340   else
3341     pattern = (sibcall_p
3342                ? gen_sibcall_value_internal (result, addr, args_size)
3343                : gen_call_value_internal (result, addr, args_size));
3344
3345   insn = emit_call_insn (pattern);
3346
3347   /* Lazy-binding stubs require $gp to be valid on entry.  */
3348   if (global_got_operand (orig_addr, VOIDmode))
3349     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3350 }
3351
3352
3353 /* We can handle any sibcall when TARGET_SIBCALLS is true.  */
3354
3355 static bool
3356 mips_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
3357                               tree exp ATTRIBUTE_UNUSED)
3358 {
3359   return TARGET_SIBCALLS;
3360 }
3361 \f
3362 /* Return true if operand OP is a condition code register.
3363    Only for use during or after reload.  */
3364
3365 int
3366 fcc_register_operand (rtx op, enum machine_mode mode)
3367 {
3368   return ((mode == VOIDmode || mode == GET_MODE (op))
3369           && (reload_in_progress || reload_completed)
3370           && (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
3371           && ST_REG_P (true_regnum (op)));
3372 }
3373
3374 /* Emit code to move general operand SRC into condition-code
3375    register DEST.  SCRATCH is a scratch TFmode float register.
3376    The sequence is:
3377
3378         FP1 = SRC
3379         FP2 = 0.0f
3380         DEST = FP2 < FP1
3381
3382    where FP1 and FP2 are single-precision float registers
3383    taken from SCRATCH.  */
3384
3385 void
3386 mips_emit_fcc_reload (rtx dest, rtx src, rtx scratch)
3387 {
3388   rtx fp1, fp2;
3389
3390   /* Change the source to SFmode.  */
3391   if (GET_CODE (src) == MEM)
3392     src = adjust_address (src, SFmode, 0);
3393   else if (GET_CODE (src) == REG || GET_CODE (src) == SUBREG)
3394     src = gen_rtx_REG (SFmode, true_regnum (src));
3395
3396   fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3397   fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3398
3399   emit_move_insn (copy_rtx (fp1), src);
3400   emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3401   emit_insn (gen_slt_sf (dest, fp2, fp1));
3402 }
3403 \f
3404 /* Emit code to change the current function's return address to
3405    ADDRESS.  SCRATCH is available as a scratch register, if needed.
3406    ADDRESS and SCRATCH are both word-mode GPRs.  */
3407
3408 void
3409 mips_set_return_address (rtx address, rtx scratch)
3410 {
3411   HOST_WIDE_INT gp_offset;
3412
3413   compute_frame_size (get_frame_size ());
3414   if (((cfun->machine->frame.mask >> 31) & 1) == 0)
3415     abort ();
3416   gp_offset = cfun->machine->frame.gp_sp_offset;
3417
3418   /* Reduce SP + GP_OFSET to a legitimate address and put it in SCRATCH.  */
3419   if (gp_offset < 32768)
3420     scratch = plus_constant (stack_pointer_rtx, gp_offset);
3421   else
3422     {
3423       emit_move_insn (scratch, GEN_INT (gp_offset));
3424       if (Pmode == DImode)
3425         emit_insn (gen_adddi3 (scratch, scratch, stack_pointer_rtx));
3426       else
3427         emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
3428     }
3429
3430   emit_move_insn (gen_rtx_MEM (GET_MODE (address), scratch), address);
3431 }
3432 \f
3433 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
3434    Assume that the areas do not overlap.  */
3435
3436 static void
3437 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
3438 {
3439   HOST_WIDE_INT offset, delta;
3440   unsigned HOST_WIDE_INT bits;
3441   int i;
3442   enum machine_mode mode;
3443   rtx *regs;
3444
3445   /* Work out how many bits to move at a time.  If both operands have
3446      half-word alignment, it is usually better to move in half words.
3447      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
3448      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
3449      Otherwise move word-sized chunks.  */
3450   if (MEM_ALIGN (src) == BITS_PER_WORD / 2
3451       && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
3452     bits = BITS_PER_WORD / 2;
3453   else
3454     bits = BITS_PER_WORD;
3455
3456   mode = mode_for_size (bits, MODE_INT, 0);
3457   delta = bits / BITS_PER_UNIT;
3458
3459   /* Allocate a buffer for the temporary registers.  */
3460   regs = alloca (sizeof (rtx) * length / delta);
3461
3462   /* Load as many BITS-sized chunks as possible.  Use a normal load if
3463      the source has enough alignment, otherwise use left/right pairs.  */
3464   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3465     {
3466       rtx part;
3467
3468       regs[i] = gen_reg_rtx (mode);
3469       part = adjust_address (src, mode, offset);
3470       if (MEM_ALIGN (part) >= bits)
3471         emit_move_insn (regs[i], part);
3472       else if (!mips_expand_unaligned_load (regs[i], part, bits, 0))
3473         abort ();
3474     }
3475
3476   /* Copy the chunks to the destination.  */
3477   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3478     {
3479       rtx part;
3480
3481       part = adjust_address (dest, mode, offset);
3482       if (MEM_ALIGN (part) >= bits)
3483         emit_move_insn (part, regs[i]);
3484       else if (!mips_expand_unaligned_store (part, regs[i], bits, 0))
3485         abort ();
3486     }
3487
3488   /* Mop up any left-over bytes.  */
3489   if (offset < length)
3490     {
3491       src = adjust_address (src, mode, offset);
3492       dest = adjust_address (dest, mode, offset);
3493       move_by_pieces (dest, src, length - offset,
3494                       MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
3495     }
3496 }
3497 \f
3498 #define MAX_MOVE_REGS 4
3499 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3500
3501
3502 /* Helper function for doing a loop-based block operation on memory
3503    reference MEM.  Each iteration of the loop will operate on LENGTH
3504    bytes of MEM.
3505
3506    Create a new base register for use within the loop and point it to
3507    the start of MEM.  Create a new memory reference that uses this
3508    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
3509
3510 static void
3511 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
3512                        rtx *loop_reg, rtx *loop_mem)
3513 {
3514   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
3515
3516   /* Although the new mem does not refer to a known location,
3517      it does keep up to LENGTH bytes of alignment.  */
3518   *loop_mem = change_address (mem, BLKmode, *loop_reg);
3519   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
3520 }
3521
3522
3523 /* Move LENGTH bytes from SRC to DEST using a loop that moves MAX_MOVE_BYTES
3524    per iteration.  LENGTH must be at least MAX_MOVE_BYTES.  Assume that the
3525    memory regions do not overlap.  */
3526
3527 static void
3528 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length)
3529 {
3530   rtx label, src_reg, dest_reg, final_src;
3531   HOST_WIDE_INT leftover;
3532
3533   leftover = length % MAX_MOVE_BYTES;
3534   length -= leftover;
3535
3536   /* Create registers and memory references for use within the loop.  */
3537   mips_adjust_block_mem (src, MAX_MOVE_BYTES, &src_reg, &src);
3538   mips_adjust_block_mem (dest, MAX_MOVE_BYTES, &dest_reg, &dest);
3539
3540   /* Calculate the value that SRC_REG should have after the last iteration
3541      of the loop.  */
3542   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
3543                                    0, 0, OPTAB_WIDEN);
3544
3545   /* Emit the start of the loop.  */
3546   label = gen_label_rtx ();
3547   emit_label (label);
3548
3549   /* Emit the loop body.  */
3550   mips_block_move_straight (dest, src, MAX_MOVE_BYTES);
3551
3552   /* Move on to the next block.  */
3553   emit_move_insn (src_reg, plus_constant (src_reg, MAX_MOVE_BYTES));
3554   emit_move_insn (dest_reg, plus_constant (dest_reg, MAX_MOVE_BYTES));
3555
3556   /* Emit the loop condition.  */
3557   if (Pmode == DImode)
3558     emit_insn (gen_cmpdi (src_reg, final_src));
3559   else
3560     emit_insn (gen_cmpsi (src_reg, final_src));
3561   emit_jump_insn (gen_bne (label));
3562
3563   /* Mop up any left-over bytes.  */
3564   if (leftover)
3565     mips_block_move_straight (dest, src, leftover);
3566 }
3567 \f
3568 /* Expand a movstrsi instruction.  */
3569
3570 bool
3571 mips_expand_block_move (rtx dest, rtx src, rtx length)
3572 {
3573   if (GET_CODE (length) == CONST_INT)
3574     {
3575       if (INTVAL (length) <= 2 * MAX_MOVE_BYTES)
3576         {
3577           mips_block_move_straight (dest, src, INTVAL (length));
3578           return true;
3579         }
3580       else if (optimize)
3581         {
3582           mips_block_move_loop (dest, src, INTVAL (length));
3583           return true;
3584         }
3585     }
3586   return false;
3587 }
3588 \f
3589 /* Argument support functions.  */
3590
3591 /* Initialize CUMULATIVE_ARGS for a function.  */
3592
3593 void
3594 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
3595                       rtx libname ATTRIBUTE_UNUSED)
3596 {
3597   static CUMULATIVE_ARGS zero_cum;
3598   tree param, next_param;
3599
3600   *cum = zero_cum;
3601   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3602
3603   /* Determine if this function has variable arguments.  This is
3604      indicated by the last argument being 'void_type_mode' if there
3605      are no variable arguments.  The standard MIPS calling sequence
3606      passes all arguments in the general purpose registers in this case.  */
3607
3608   for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3609        param != 0; param = next_param)
3610     {
3611       next_param = TREE_CHAIN (param);
3612       if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3613         cum->gp_reg_found = 1;
3614     }
3615 }
3616
3617
3618 /* Fill INFO with information about a single argument.  CUM is the
3619    cumulative state for earlier arguments.  MODE is the mode of this
3620    argument and TYPE is its type (if known).  NAMED is true if this
3621    is a named (fixed) argument rather than a variable one.  */
3622
3623 static void
3624 mips_arg_info (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3625                tree type, int named, struct mips_arg_info *info)
3626 {
3627   bool even_reg_p;
3628   unsigned int num_words, max_regs;
3629
3630   /* Decide whether this argument should go in a floating-point register,
3631      assuming one is free.  Later code checks for availability.  */
3632
3633   info->fpr_p = (GET_MODE_CLASS (mode) == MODE_FLOAT
3634                  && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3635
3636   if (info->fpr_p)
3637     switch (mips_abi)
3638       {
3639       case ABI_32:
3640       case ABI_O64:
3641         info->fpr_p = (!cum->gp_reg_found
3642                        && cum->arg_number < 2
3643                        && (type == 0 || FLOAT_TYPE_P (type)));
3644         break;
3645
3646       case ABI_N32:
3647       case ABI_64:
3648         info->fpr_p = (named && (type == 0 || FLOAT_TYPE_P (type)));
3649         break;
3650       }
3651
3652   /* Now decide whether the argument must go in an even-numbered register.  */
3653
3654   even_reg_p = false;
3655   if (info->fpr_p)
3656     {
3657       /* Under the O64 ABI, the second float argument goes in $f13 if it
3658          is a double, but $f14 if it is a single.  Otherwise, on a
3659          32-bit double-float machine, each FP argument must start in a
3660          new register pair.  */
3661       even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_HWFPVALUE
3662                     || (mips_abi == ABI_O64 && mode == SFmode)
3663                     || FP_INC > 1);
3664     }
3665   else if (!TARGET_64BIT || LONG_DOUBLE_TYPE_SIZE == 128)
3666     {
3667       if (GET_MODE_CLASS (mode) == MODE_INT
3668           || GET_MODE_CLASS (mode) == MODE_FLOAT)
3669         even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_WORD);
3670
3671       else if (type != NULL_TREE && TYPE_ALIGN (type) > BITS_PER_WORD)
3672         even_reg_p = true;
3673     }
3674
3675   if (mips_abi != ABI_EABI && MUST_PASS_IN_STACK (mode, type))
3676     /* This argument must be passed on the stack.  Eat up all the
3677        remaining registers.  */
3678     info->reg_offset = MAX_ARGS_IN_REGISTERS;
3679   else
3680     {
3681       /* Set REG_OFFSET to the register count we're interested in.
3682          The EABI allocates the floating-point registers separately,
3683          but the other ABIs allocate them like integer registers.  */
3684       info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
3685                           ? cum->num_fprs
3686                           : cum->num_gprs);
3687
3688       if (even_reg_p)
3689         info->reg_offset += info->reg_offset & 1;
3690     }
3691
3692   /* The alignment applied to registers is also applied to stack arguments.  */
3693   info->stack_offset = cum->stack_words;
3694   if (even_reg_p)
3695     info->stack_offset += info->stack_offset & 1;
3696
3697   if (mode == BLKmode)
3698     info->num_bytes = int_size_in_bytes (type);
3699   else
3700     info->num_bytes = GET_MODE_SIZE (mode);
3701
3702   num_words = (info->num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3703   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
3704
3705   /* Partition the argument between registers and stack.  */
3706   info->reg_words = MIN (num_words, max_regs);
3707   info->stack_words = num_words - info->reg_words;
3708 }
3709
3710
3711 /* Implement FUNCTION_ARG_ADVANCE.  */
3712
3713 void
3714 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3715                       tree type, int named)
3716 {
3717   struct mips_arg_info info;
3718
3719   mips_arg_info (cum, mode, type, named, &info);
3720
3721   if (!info.fpr_p)
3722     cum->gp_reg_found = true;
3723
3724   /* See the comment above the cumulative args structure in mips.h
3725      for an explanation of what this code does.  It assumes the O32
3726      ABI, which passes at most 2 arguments in float registers.  */
3727   if (cum->arg_number < 2 && info.fpr_p)
3728     cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
3729
3730   if (mips_abi != ABI_EABI || !info.fpr_p)
3731     cum->num_gprs = info.reg_offset + info.reg_words;
3732   else if (info.reg_words > 0)
3733     cum->num_fprs += FP_INC;
3734
3735   if (info.stack_words > 0)
3736     cum->stack_words = info.stack_offset + info.stack_words;
3737
3738   cum->arg_number++;
3739 }
3740
3741 /* Implement FUNCTION_ARG.  */
3742
3743 struct rtx_def *
3744 function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3745               tree type, int named)
3746 {
3747   struct mips_arg_info info;
3748
3749   /* We will be called with a mode of VOIDmode after the last argument
3750      has been seen.  Whatever we return will be passed to the call
3751      insn.  If we need a mips16 fp_code, return a REG with the code
3752      stored as the mode.  */
3753   if (mode == VOIDmode)
3754     {
3755       if (TARGET_MIPS16 && cum->fp_code != 0)
3756         return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
3757
3758       else
3759         return 0;
3760     }
3761
3762   mips_arg_info (cum, mode, type, named, &info);
3763
3764   /* Return straight away if the whole argument is passed on the stack.  */
3765   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
3766     return 0;
3767
3768   if (type != 0
3769       && TREE_CODE (type) == RECORD_TYPE
3770       && TARGET_NEWABI
3771       && TYPE_SIZE_UNIT (type)
3772       && host_integerp (TYPE_SIZE_UNIT (type), 1)
3773       && named)
3774     {
3775       /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
3776          structure contains a double in its entirety, then that 64 bit
3777          chunk is passed in a floating point register.  */
3778       tree field;
3779
3780       /* First check to see if there is any such field.  */
3781       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3782         if (TREE_CODE (field) == FIELD_DECL
3783             && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3784             && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
3785             && host_integerp (bit_position (field), 0)
3786             && int_bit_position (field) % BITS_PER_WORD == 0)
3787           break;
3788
3789       if (field != 0)
3790         {
3791           /* Now handle the special case by returning a PARALLEL
3792              indicating where each 64 bit chunk goes.  INFO.REG_WORDS
3793              chunks are passed in registers.  */
3794           unsigned int i;
3795           HOST_WIDE_INT bitpos;
3796           rtx ret;
3797
3798           /* assign_parms checks the mode of ENTRY_PARM, so we must
3799              use the actual mode here.  */
3800           ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
3801
3802           bitpos = 0;
3803           field = TYPE_FIELDS (type);
3804           for (i = 0; i < info.reg_words; i++)
3805             {
3806               rtx reg;
3807
3808               for (; field; field = TREE_CHAIN (field))
3809                 if (TREE_CODE (field) == FIELD_DECL
3810                     && int_bit_position (field) >= bitpos)
3811                   break;
3812
3813               if (field
3814                   && int_bit_position (field) == bitpos
3815                   && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3816                   && !TARGET_SOFT_FLOAT
3817                   && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
3818                 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
3819               else
3820                 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
3821
3822               XVECEXP (ret, 0, i)
3823                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
3824                                      GEN_INT (bitpos / BITS_PER_UNIT));
3825
3826               bitpos += BITS_PER_WORD;
3827             }
3828           return ret;
3829         }
3830     }
3831
3832   if (info.fpr_p)
3833     return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
3834   else
3835     return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
3836 }
3837
3838
3839 /* Implement FUNCTION_ARG_PARTIAL_NREGS.  */
3840
3841 int
3842 function_arg_partial_nregs (const CUMULATIVE_ARGS *cum,
3843                             enum machine_mode mode, tree type, int named)
3844 {
3845   struct mips_arg_info info;
3846
3847   mips_arg_info (cum, mode, type, named, &info);
3848   return info.stack_words > 0 ? info.reg_words : 0;
3849 }
3850
3851
3852 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
3853    upward rather than downward.  In other words, return true if the
3854    first byte of the stack slot has useful data, false if the last
3855    byte does.  */
3856
3857 bool
3858 mips_pad_arg_upward (enum machine_mode mode, tree type)
3859 {
3860   /* On little-endian targets, the first byte of every stack argument
3861      is passed in the first byte of the stack slot.  */
3862   if (!BYTES_BIG_ENDIAN)
3863     return true;
3864
3865   /* Otherwise, integral types are padded downward: the last byte of a
3866      stack argument is passed in the last byte of the stack slot.  */
3867   if (type != 0
3868       ? INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
3869       : GET_MODE_CLASS (mode) == MODE_INT)
3870     return false;
3871
3872   /* Big-endian o64 pads floating-point arguments downward.  */
3873   if (mips_abi == ABI_O64)
3874     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3875       return false;
3876
3877   /* Other types are padded upward for o32, o64, n32 and n64.  */
3878   if (mips_abi != ABI_EABI)
3879     return true;
3880
3881   /* Arguments smaller than a stack slot are padded downward.  */
3882   if (mode != BLKmode)
3883     return (GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY);
3884   else
3885     return (int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT));
3886 }
3887
3888
3889 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
3890    if the least significant byte of the register has useful data.  Return
3891    the opposite if the most significant byte does.  */
3892
3893 bool
3894 mips_pad_reg_upward (enum machine_mode mode, tree type)
3895 {
3896   /* No shifting is required for floating-point arguments.  */
3897   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3898     return !BYTES_BIG_ENDIAN;
3899
3900   /* Otherwise, apply the same padding to register arguments as we do
3901      to stack arguments.  */
3902   return mips_pad_arg_upward (mode, type);
3903 }
3904 \f
3905 static void
3906 mips_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3907                              tree type, int *pretend_size, int no_rtl)
3908 {
3909   CUMULATIVE_ARGS local_cum;
3910   int gp_saved, fp_saved;
3911
3912   /* The caller has advanced CUM up to, but not beyond, the last named
3913      argument.  Advance a local copy of CUM past the last "real" named
3914      argument, to find out how many registers are left over.  */
3915
3916   local_cum = *cum;
3917   FUNCTION_ARG_ADVANCE (local_cum, mode, type, 1);
3918
3919   /* Found out how many registers we need to save.  */
3920   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
3921   fp_saved = (EABI_FLOAT_VARARGS_P
3922               ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
3923               : 0);
3924
3925   if (!no_rtl)
3926     {
3927       if (gp_saved > 0)
3928         {
3929           rtx ptr, mem;
3930
3931           ptr = virtual_incoming_args_rtx;
3932           switch (mips_abi)
3933             {
3934             case ABI_32:
3935             case ABI_O64:
3936               ptr = plus_constant (ptr, local_cum.num_gprs * UNITS_PER_WORD);
3937               break;
3938
3939             case ABI_EABI:
3940               ptr = plus_constant (ptr, -gp_saved * UNITS_PER_WORD);
3941               break;
3942             }
3943           mem = gen_rtx_MEM (BLKmode, ptr);
3944           set_mem_alias_set (mem, get_varargs_alias_set ());
3945
3946           move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
3947                                mem, gp_saved);
3948         }
3949       if (fp_saved > 0)
3950         {
3951           /* We can't use move_block_from_reg, because it will use
3952              the wrong mode.  */
3953           enum machine_mode mode;
3954           int off, i;
3955
3956           /* Set OFF to the offset from virtual_incoming_args_rtx of
3957              the first float register.   The FP save area lies below
3958              the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
3959           off = -gp_saved * UNITS_PER_WORD;
3960           off &= ~(UNITS_PER_FPVALUE - 1);
3961           off -= fp_saved * UNITS_PER_FPREG;
3962
3963           mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
3964
3965           for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS; i += FP_INC)
3966             {
3967               rtx ptr, mem;
3968
3969               ptr = plus_constant (virtual_incoming_args_rtx, off);
3970               mem = gen_rtx_MEM (mode, ptr);
3971               set_mem_alias_set (mem, get_varargs_alias_set ());
3972               emit_move_insn (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
3973               off += UNITS_PER_HWFPVALUE;
3974             }
3975         }
3976     }
3977   if (TARGET_OLDABI)
3978     {
3979       /* No need for pretend arguments: the register parameter area was
3980          allocated by the caller.  */
3981       *pretend_size = 0;
3982       return;
3983     }
3984   *pretend_size = (gp_saved * UNITS_PER_WORD) + (fp_saved * UNITS_PER_FPREG);
3985 }
3986
3987 /* Create the va_list data type.
3988    We keep 3 pointers, and two offsets.
3989    Two pointers are to the overflow area, which starts at the CFA.
3990      One of these is constant, for addressing into the GPR save area below it.
3991      The other is advanced up the stack through the overflow region.
3992    The third pointer is to the GPR save area.  Since the FPR save area
3993      is just below it, we can address FPR slots off this pointer.
3994    We also keep two one-byte offsets, which are to be subtracted from the
3995      constant pointers to yield addresses in the GPR and FPR save areas.
3996      These are downcounted as float or non-float arguments are used,
3997      and when they get to zero, the argument must be obtained from the
3998      overflow region.
3999    If !EABI_FLOAT_VARARGS_P, then no FPR save area exists, and a single
4000      pointer is enough.  It's started at the GPR save area, and is
4001      advanced, period.
4002    Note that the GPR save area is not constant size, due to optimization
4003      in the prologue.  Hence, we can't use a design with two pointers
4004      and two offsets, although we could have designed this with two pointers
4005      and three offsets.  */
4006
4007 static tree
4008 mips_build_builtin_va_list (void)
4009 {
4010   if (EABI_FLOAT_VARARGS_P)
4011     {
4012       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
4013       tree array, index;
4014
4015       record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4016
4017       f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
4018                           ptr_type_node);
4019       f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
4020                           ptr_type_node);
4021       f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
4022                           ptr_type_node);
4023       f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
4024                           unsigned_char_type_node);
4025       f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
4026                           unsigned_char_type_node);
4027       /* Explicitly pad to the size of a pointer, so that -Wpadded won't
4028          warn on every user file.  */
4029       index = build_int_2 (GET_MODE_SIZE (ptr_mode) - 2 - 1, 0);
4030       array = build_array_type (unsigned_char_type_node,
4031                                 build_index_type (index));
4032       f_res = build_decl (FIELD_DECL, get_identifier ("__reserved"), array);
4033
4034       DECL_FIELD_CONTEXT (f_ovfl) = record;
4035       DECL_FIELD_CONTEXT (f_gtop) = record;
4036       DECL_FIELD_CONTEXT (f_ftop) = record;
4037       DECL_FIELD_CONTEXT (f_goff) = record;
4038       DECL_FIELD_CONTEXT (f_foff) = record;
4039       DECL_FIELD_CONTEXT (f_res) = record;
4040
4041       TYPE_FIELDS (record) = f_ovfl;
4042       TREE_CHAIN (f_ovfl) = f_gtop;
4043       TREE_CHAIN (f_gtop) = f_ftop;
4044       TREE_CHAIN (f_ftop) = f_goff;
4045       TREE_CHAIN (f_goff) = f_foff;
4046       TREE_CHAIN (f_foff) = f_res;
4047
4048       layout_type (record);
4049       return record;
4050     }
4051   else if (TARGET_IRIX && !TARGET_IRIX5)
4052     /* On IRIX 6, this type is 'char *'.  */
4053     return build_pointer_type (char_type_node);
4054   else
4055     /* Otherwise, we use 'void *'.  */
4056     return ptr_type_node;
4057 }
4058
4059 /* Implement va_start.  */
4060
4061 void
4062 mips_va_start (tree valist, rtx nextarg)
4063 {
4064   const CUMULATIVE_ARGS *cum = &current_function_args_info;
4065
4066   /* ARG_POINTER_REGNUM is initialized to STACK_POINTER_BOUNDARY, but
4067      since the stack is aligned for a pair of argument-passing slots,
4068      and the beginning of a variable argument list may be an odd slot,
4069      we have to decrease its alignment.  */
4070   if (cfun && cfun->emit->regno_pointer_align)
4071     while (((current_function_pretend_args_size * BITS_PER_UNIT)
4072             & (REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) - 1)) != 0)
4073       REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) /= 2;
4074
4075   if (mips_abi == ABI_EABI)
4076     {
4077       int gpr_save_area_size;
4078
4079       gpr_save_area_size
4080         = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
4081
4082       if (EABI_FLOAT_VARARGS_P)
4083         {
4084           tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4085           tree ovfl, gtop, ftop, goff, foff;
4086           tree t;
4087           int fpr_offset;
4088           int fpr_save_area_size;
4089
4090           f_ovfl = TYPE_FIELDS (va_list_type_node);
4091           f_gtop = TREE_CHAIN (f_ovfl);
4092           f_ftop = TREE_CHAIN (f_gtop);
4093           f_goff = TREE_CHAIN (f_ftop);
4094           f_foff = TREE_CHAIN (f_goff);
4095
4096           ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4097           gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4098           ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4099           goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4100           foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4101
4102           /* Emit code to initialize OVFL, which points to the next varargs
4103              stack argument.  CUM->STACK_WORDS gives the number of stack
4104              words used by named arguments.  */
4105           t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
4106           if (cum->stack_words > 0)
4107             t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
4108                        build_int_2 (cum->stack_words * UNITS_PER_WORD, 0));
4109           t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4110           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4111
4112           /* Emit code to initialize GTOP, the top of the GPR save area.  */
4113           t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
4114           t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
4115           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4116
4117           /* Emit code to initialize FTOP, the top of the FPR save area.
4118              This address is gpr_save_area_bytes below GTOP, rounded
4119              down to the next fp-aligned boundary.  */
4120           t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
4121           fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
4122           fpr_offset &= ~(UNITS_PER_FPVALUE - 1);
4123           if (fpr_offset)
4124             t = build (PLUS_EXPR, TREE_TYPE (ftop), t,
4125                        build_int_2 (-fpr_offset, -1));
4126           t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
4127           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4128
4129           /* Emit code to initialize GOFF, the offset from GTOP of the
4130              next GPR argument.  */
4131           t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
4132                      build_int_2 (gpr_save_area_size, 0));
4133           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4134
4135           /* Likewise emit code to initialize FOFF, the offset from FTOP
4136              of the next FPR argument.  */
4137           fpr_save_area_size
4138             = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
4139           t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
4140                      build_int_2 (fpr_save_area_size, 0));
4141           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4142         }
4143       else
4144         {
4145           /* Everything is in the GPR save area, or in the overflow
4146              area which is contiguous with it.  */
4147           nextarg = plus_constant (nextarg, -gpr_save_area_size);
4148           std_expand_builtin_va_start (valist, nextarg);
4149         }
4150     }
4151   else
4152     std_expand_builtin_va_start (valist, nextarg);
4153 }
4154 \f
4155 /* Implement va_arg.  */
4156
4157 rtx
4158 mips_va_arg (tree valist, tree type)
4159 {
4160   HOST_WIDE_INT size, rsize;
4161   rtx addr_rtx;
4162   tree t;
4163
4164   size = int_size_in_bytes (type);
4165   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4166
4167   if (mips_abi == ABI_EABI)
4168     {
4169       bool indirect;
4170       rtx r;
4171
4172       indirect
4173         = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
4174
4175       if (indirect)
4176         {
4177           size = POINTER_SIZE / BITS_PER_UNIT;
4178           rsize = UNITS_PER_WORD;
4179         }
4180
4181       addr_rtx = gen_reg_rtx (Pmode);
4182
4183       if (!EABI_FLOAT_VARARGS_P)
4184         {
4185           /* Case of all args in a merged stack.  No need to check bounds,
4186              just advance valist along the stack.  */
4187
4188           tree gpr = valist;
4189           if (!indirect
4190               && !TARGET_64BIT
4191               && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
4192             {
4193               /* Align the pointer using: ap = (ap + align - 1) & -align,
4194                  where align is 2 * UNITS_PER_WORD.  */
4195               t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4196                          build_int_2 (2 * UNITS_PER_WORD - 1, 0));
4197               t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
4198                          build_int_2 (-2 * UNITS_PER_WORD, -1));
4199               t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4200               expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4201             }
4202
4203           /* Emit code to set addr_rtx to the valist, and postincrement
4204              the valist by the size of the argument, rounded up to the
4205              next word.  */
4206           t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
4207                      size_int (rsize));
4208           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4209           if (r != addr_rtx)
4210             emit_move_insn (addr_rtx, r);
4211
4212           /* Flush the POSTINCREMENT.  */
4213           emit_queue();
4214         }
4215       else
4216         {
4217           /* Not a simple merged stack.  */
4218
4219           tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4220           tree ovfl, top, off;
4221           rtx lab_over = NULL_RTX, lab_false;
4222           HOST_WIDE_INT osize;
4223
4224           f_ovfl = TYPE_FIELDS (va_list_type_node);
4225           f_gtop = TREE_CHAIN (f_ovfl);
4226           f_ftop = TREE_CHAIN (f_gtop);
4227           f_goff = TREE_CHAIN (f_ftop);
4228           f_foff = TREE_CHAIN (f_goff);
4229
4230           /* We maintain separate pointers and offsets for floating-point
4231              and integer arguments, but we need similar code in both cases.
4232              Let:
4233
4234                  TOP be the top of the register save area;
4235                  OFF be the offset from TOP of the next register;
4236                  ADDR_RTX be the address of the argument;
4237                  RSIZE be the number of bytes used to store the argument
4238                    when it's in the register save area;
4239                  OSIZE be the number of bytes used to store it when it's
4240                    in the stack overflow area; and
4241                  PADDING be (BYTES_BIG_ENDIAN ? OSIZE - RSIZE : 0)
4242
4243              The code we want is:
4244
4245                   1: off &= -rsize;       // round down
4246                   2: if (off != 0)
4247                   3:   {
4248                   4:     addr_rtx = top - off;
4249                   5:     off -= rsize;
4250                   6:   }
4251                   7: else
4252                   8:   {
4253                   9:     ovfl += ((intptr_t) ovfl + osize - 1) & -osize;
4254                  10:     addr_rtx = ovfl + PADDING;
4255                  11:     ovfl += osize;
4256                  14:   }
4257
4258              [1] and [9] can sometimes be optimized away.  */
4259
4260           lab_false = gen_label_rtx ();
4261           lab_over = gen_label_rtx ();
4262
4263           ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4264           if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
4265               && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
4266             {
4267               top = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4268               off = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4269
4270               /* When floating-point registers are saved to the stack,
4271                  each one will take up UNITS_PER_HWFPVALUE bytes, regardless
4272                  of the float's precision.  */
4273               rsize = UNITS_PER_HWFPVALUE;
4274             }
4275           else
4276             {
4277               top = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4278               off = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4279               if (rsize > UNITS_PER_WORD)
4280                 {
4281                   /* [1] Emit code for: off &= -rsize.  */
4282                   t = build (BIT_AND_EXPR, TREE_TYPE (off), off,
4283                              build_int_2 (-rsize, -1));
4284                   t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
4285                   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4286                 }
4287             }
4288           /* Every overflow argument must take up at least UNITS_PER_WORD
4289              bytes (= PARM_BOUNDARY bits).  RSIZE can sometimes be smaller
4290              than that, such as in the combination -mgp64 -msingle-float
4291              -fshort-double.  Doubles passed in registers will then take
4292              up UNITS_PER_HWFPVALUE bytes, but those passed on the stack
4293              take up UNITS_PER_WORD bytes.  */
4294           osize = MAX (rsize, UNITS_PER_WORD);
4295
4296           /* [2] Emit code to branch if off == 0.  */
4297           r = expand_expr (off, NULL_RTX, TYPE_MODE (TREE_TYPE (off)),
4298                            EXPAND_NORMAL);
4299           emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
4300                                    1, lab_false);
4301
4302           /* [4] Emit code for: addr_rtx = top - off.  */
4303           t = build (MINUS_EXPR, TREE_TYPE (top), top, off);
4304           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4305           if (r != addr_rtx)
4306             emit_move_insn (addr_rtx, r);
4307
4308           /* [5] Emit code for: off -= rsize.  */
4309           t = build (MINUS_EXPR, TREE_TYPE (off), off, build_int_2 (rsize, 0));
4310           t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
4311           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4312
4313           /* [7] Emit code to jump over the else clause, then the label
4314              that starts it.  */
4315           emit_queue();
4316           emit_jump (lab_over);
4317           emit_barrier ();
4318           emit_label (lab_false);
4319
4320           if (osize > UNITS_PER_WORD)
4321             {
4322               /* [9] Emit: ovfl += ((intptr_t) ovfl + osize - 1) & -osize.  */
4323               t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl,
4324                          build_int_2 (osize - 1, 0));
4325               t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), t,
4326                          build_int_2 (-osize, -1));
4327               t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4328               expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4329             }
4330
4331           /* [10, 11].  Emit code to store ovfl in addr_rtx, then
4332              post-increment ovfl by osize.  On big-endian machines,
4333              the argument has OSIZE - RSIZE bytes of leading padding.  */
4334           t = build (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl,
4335                      size_int (osize));
4336           if (BYTES_BIG_ENDIAN && osize > rsize)
4337             t = build (PLUS_EXPR, TREE_TYPE (t), t,
4338                        build_int_2 (osize - rsize, 0));
4339           r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4340           if (r != addr_rtx)
4341             emit_move_insn (addr_rtx, r);
4342
4343           emit_queue();
4344           emit_label (lab_over);
4345         }
4346       if (BYTES_BIG_ENDIAN && rsize != size)
4347         addr_rtx = plus_constant (addr_rtx, rsize - size);
4348       if (indirect)
4349         {
4350           addr_rtx = force_reg (Pmode, addr_rtx);
4351           r = gen_rtx_MEM (Pmode, addr_rtx);
4352           set_mem_alias_set (r, get_varargs_alias_set ());
4353           emit_move_insn (addr_rtx, r);
4354         }
4355       return addr_rtx;
4356     }
4357   else
4358     {
4359       /* Not EABI.  */
4360       int align;
4361       HOST_WIDE_INT min_offset;
4362
4363       /* ??? The original va-mips.h did always align, despite the fact
4364          that alignments <= UNITS_PER_WORD are preserved by the va_arg
4365          increment mechanism.  */
4366
4367       if (TARGET_NEWABI && TYPE_ALIGN (type) > 64)
4368         align = 16;
4369       else if (TARGET_64BIT)
4370         align = 8;
4371       else if (TYPE_ALIGN (type) > 32)
4372         align = 8;
4373       else
4374         align = 4;
4375
4376       t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4377                  build_int_2 (align - 1, 0));
4378       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
4379
4380       /* If arguments of type TYPE must be passed on the stack,
4381          set MIN_OFFSET to the offset of the first stack parameter.  */
4382       if (!MUST_PASS_IN_STACK (TYPE_MODE (type), type))
4383         min_offset = 0;
4384       else if (TARGET_NEWABI)
4385         min_offset = current_function_pretend_args_size;
4386       else
4387         min_offset = REG_PARM_STACK_SPACE (current_function_decl);
4388
4389       /* Make sure the new address is at least MIN_OFFSET bytes from
4390          the incoming argument pointer.  */
4391       if (min_offset > 0)
4392         t = build (MAX_EXPR, TREE_TYPE (valist), t,
4393                    make_tree (TREE_TYPE (valist),
4394                               plus_constant (virtual_incoming_args_rtx,
4395                                              min_offset)));
4396
4397       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4398       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4399
4400       /* Everything past the alignment is standard.  */
4401       return std_expand_builtin_va_arg (valist, type);
4402     }
4403 }
4404 \f
4405 /* Return true if it is possible to use left/right accesses for a
4406    bitfield of WIDTH bits starting BITPOS bits into *OP.  When
4407    returning true, update *OP, *LEFT and *RIGHT as follows:
4408
4409    *OP is a BLKmode reference to the whole field.
4410
4411    *LEFT is a QImode reference to the first byte if big endian or
4412    the last byte if little endian.  This address can be used in the
4413    left-side instructions (lwl, swl, ldl, sdl).
4414
4415    *RIGHT is a QImode reference to the opposite end of the field and
4416    can be used in the parterning right-side instruction.  */
4417
4418 static bool
4419 mips_get_unaligned_mem (rtx *op, unsigned int width, int bitpos,
4420                         rtx *left, rtx *right)
4421 {
4422   rtx first, last;
4423
4424   /* Check that the operand really is a MEM.  Not all the extv and
4425      extzv predicates are checked.  */
4426   if (GET_CODE (*op) != MEM)
4427     return false;
4428
4429   /* Check that the size is valid.  */
4430   if (width != 32 && (!TARGET_64BIT || width != 64))
4431     return false;
4432
4433   /* We can only access byte-aligned values.  Since we are always passed
4434      a reference to the first byte of the field, it is not necessary to
4435      do anything with BITPOS after this check.  */
4436   if (bitpos % BITS_PER_UNIT != 0)
4437     return false;
4438
4439   /* Reject aligned bitfields: we want to use a normal load or store
4440      instead of a left/right pair.  */
4441   if (MEM_ALIGN (*op) >= width)
4442     return false;
4443
4444   /* Adjust *OP to refer to the whole field.  This also has the effect
4445      of legitimizing *OP's address for BLKmode, possibly simplifying it.  */
4446   *op = adjust_address (*op, BLKmode, 0);
4447   set_mem_size (*op, GEN_INT (width / BITS_PER_UNIT));
4448
4449   /* Get references to both ends of the field.  We deliberately don't
4450      use the original QImode *OP for FIRST since the new BLKmode one
4451      might have a simpler address.  */
4452   first = adjust_address (*op, QImode, 0);
4453   last = adjust_address (*op, QImode, width / BITS_PER_UNIT - 1);
4454
4455   /* Allocate to LEFT and RIGHT according to endianness.  LEFT should
4456      be the upper word and RIGHT the lower word.  */
4457   if (TARGET_BIG_ENDIAN)
4458     *left = first, *right = last;
4459   else
4460     *left = last, *right = first;
4461
4462   return true;
4463 }
4464
4465
4466 /* Try to emit the equivalent of (set DEST (zero_extract SRC WIDTH BITPOS)).
4467    Return true on success.  We only handle cases where zero_extract is
4468    equivalent to sign_extract.  */
4469
4470 bool
4471 mips_expand_unaligned_load (rtx dest, rtx src, unsigned int width, int bitpos)
4472 {
4473   rtx left, right;
4474
4475   /* If TARGET_64BIT, the destination of a 32-bit load will be a
4476      paradoxical word_mode subreg.  This is the only case in which
4477      we allow the destination to be larger than the source.  */
4478   if (GET_CODE (dest) == SUBREG
4479       && GET_MODE (dest) == DImode
4480       && SUBREG_BYTE (dest) == 0
4481       && GET_MODE (SUBREG_REG (dest)) == SImode)
4482     dest = SUBREG_REG (dest);
4483
4484   /* After the above adjustment, the destination must be the same
4485      width as the source.  */
4486   if (GET_MODE_BITSIZE (GET_MODE (dest)) != width)
4487     return false;
4488
4489   if (!mips_get_unaligned_mem (&src, width, bitpos, &left, &right))
4490     return false;
4491
4492   if (GET_MODE (dest) == DImode)
4493     {
4494       emit_insn (gen_mov_ldl (dest, src, left));
4495       emit_insn (gen_mov_ldr (copy_rtx (dest), copy_rtx (src),
4496                               right, copy_rtx (dest)));
4497     }
4498   else
4499     {
4500       emit_insn (gen_mov_lwl (dest, src, left));
4501       emit_insn (gen_mov_lwr (copy_rtx (dest), copy_rtx (src),
4502                               right, copy_rtx (dest)));
4503     }
4504   return true;
4505 }
4506
4507
4508 /* Try to expand (set (zero_extract DEST WIDTH BITPOS) SRC).  Return
4509    true on success.  */
4510
4511 bool
4512 mips_expand_unaligned_store (rtx dest, rtx src, unsigned int width, int bitpos)
4513 {
4514   rtx left, right;
4515
4516   if (!mips_get_unaligned_mem (&dest, width, bitpos, &left, &right))
4517     return false;
4518
4519   src = gen_lowpart (mode_for_size (width, MODE_INT, 0), src);
4520
4521   if (GET_MODE (src) == DImode)
4522     {
4523       emit_insn (gen_mov_sdl (dest, src, left));
4524       emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
4525     }
4526   else
4527     {
4528       emit_insn (gen_mov_swl (dest, src, left));
4529       emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
4530     }
4531   return true;
4532 }
4533 \f
4534 /* Set up globals to generate code for the ISA or processor
4535    described by INFO.  */
4536
4537 static void
4538 mips_set_architecture (const struct mips_cpu_info *info)
4539 {
4540   if (info != 0)
4541     {
4542       mips_arch_info = info;
4543       mips_arch = info->cpu;
4544       mips_isa = info->isa;
4545     }
4546 }
4547
4548
4549 /* Likewise for tuning.  */
4550
4551 static void
4552 mips_set_tune (const struct mips_cpu_info *info)
4553 {
4554   if (info != 0)
4555     {
4556       mips_tune_info = info;
4557       mips_tune = info->cpu;
4558     }
4559 }
4560
4561
4562 /* Set up the threshold for data to go into the small data area, instead
4563    of the normal data area, and detect any conflicts in the switches.  */
4564
4565 void
4566 override_options (void)
4567 {
4568   int i, start, regno;
4569   enum machine_mode mode;
4570
4571   mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4572
4573   /* Interpret -mabi.  */
4574   mips_abi = MIPS_ABI_DEFAULT;
4575   if (mips_abi_string != 0)
4576     {
4577       if (strcmp (mips_abi_string, "32") == 0)
4578         mips_abi = ABI_32;
4579       else if (strcmp (mips_abi_string, "o64") == 0)
4580         mips_abi = ABI_O64;
4581       else if (strcmp (mips_abi_string, "n32") == 0)
4582         mips_abi = ABI_N32;
4583       else if (strcmp (mips_abi_string, "64") == 0)
4584         mips_abi = ABI_64;
4585       else if (strcmp (mips_abi_string, "eabi") == 0)
4586         mips_abi = ABI_EABI;
4587       else
4588         fatal_error ("bad value (%s) for -mabi= switch", mips_abi_string);
4589     }
4590
4591   /* The following code determines the architecture and register size.
4592      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
4593      The GAS and GCC code should be kept in sync as much as possible.  */
4594
4595   if (mips_arch_string != 0)
4596     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
4597
4598   if (mips_isa_string != 0)
4599     {
4600       /* Handle -mipsN.  */
4601       char *whole_isa_str = concat ("mips", mips_isa_string, NULL);
4602       const struct mips_cpu_info *isa_info;
4603
4604       isa_info = mips_parse_cpu ("-mips option", whole_isa_str);
4605       free (whole_isa_str);
4606
4607       /* -march takes precedence over -mipsN, since it is more descriptive.
4608          There's no harm in specifying both as long as the ISA levels
4609          are the same.  */
4610       if (mips_arch_info != 0 && mips_isa != isa_info->isa)
4611         error ("-mips%s conflicts with the other architecture options, "
4612                "which specify a MIPS%d processor",
4613                mips_isa_string, mips_isa);
4614
4615       /* Set architecture based on the given option.  */
4616       mips_set_architecture (isa_info);
4617     }
4618
4619   if (mips_arch_info == 0)
4620     {
4621 #ifdef MIPS_CPU_STRING_DEFAULT
4622       mips_set_architecture (mips_parse_cpu ("default CPU",
4623                                              MIPS_CPU_STRING_DEFAULT));
4624 #else
4625       mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
4626 #endif
4627     }
4628
4629   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
4630     error ("-march=%s is not compatible with the selected ABI",
4631            mips_arch_info->name);
4632
4633   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
4634   if (mips_tune_string != 0)
4635     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
4636
4637   if (mips_tune_info == 0)
4638     mips_set_tune (mips_arch_info);
4639
4640   if ((target_flags_explicit & MASK_64BIT) != 0)
4641     {
4642       /* The user specified the size of the integer registers.  Make sure
4643          it agrees with the ABI and ISA.  */
4644       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
4645         error ("-mgp64 used with a 32-bit processor");
4646       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
4647         error ("-mgp32 used with a 64-bit ABI");
4648       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
4649         error ("-mgp64 used with a 32-bit ABI");
4650     }
4651   else
4652     {
4653       /* Infer the integer register size from the ABI and processor.
4654          Restrict ourselves to 32-bit registers if that's all the
4655          processor has, or if the ABI cannot handle 64-bit registers.  */
4656       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
4657         target_flags &= ~MASK_64BIT;
4658       else
4659         target_flags |= MASK_64BIT;
4660     }
4661
4662   if ((target_flags_explicit & MASK_FLOAT64) != 0)
4663     {
4664       /* Really, -mfp32 and -mfp64 are ornamental options.  There's
4665          only one right answer here.  */
4666       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
4667         error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
4668       else if (!TARGET_64BIT && TARGET_FLOAT64)
4669         error ("unsupported combination: %s", "-mgp32 -mfp64");
4670       else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
4671         error ("unsupported combination: %s", "-mfp64 -msingle-float");
4672     }
4673   else
4674     {
4675       /* -msingle-float selects 32-bit float registers.  Otherwise the
4676          float registers should be the same size as the integer ones.  */
4677       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
4678         target_flags |= MASK_FLOAT64;
4679       else
4680         target_flags &= ~MASK_FLOAT64;
4681     }
4682
4683   /* End of code shared with GAS.  */
4684
4685   if ((target_flags_explicit & MASK_LONG64) == 0)
4686     {
4687       /* If no type size setting options (-mlong64,-mint64,-mlong32)
4688          were used, then set the type sizes.  In the EABI in 64 bit mode,
4689          longs and pointers are 64 bits.  Likewise for the SGI Irix6 N64
4690          ABI.  */
4691       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
4692         target_flags |= MASK_LONG64;
4693       else
4694         target_flags &= ~MASK_LONG64;
4695     }
4696
4697   if (MIPS_MARCH_CONTROLS_SOFT_FLOAT
4698       && (target_flags_explicit & MASK_SOFT_FLOAT) == 0)
4699     {
4700       /* For some configurations, it is useful to have -march control
4701          the default setting of MASK_SOFT_FLOAT.  */
4702       switch ((int) mips_arch)
4703         {
4704         case PROCESSOR_R4100:
4705         case PROCESSOR_R4111:
4706         case PROCESSOR_R4120:
4707           target_flags |= MASK_SOFT_FLOAT;
4708           break;
4709
4710         default:
4711           target_flags &= ~MASK_SOFT_FLOAT;
4712           break;
4713         }
4714     }
4715
4716   if (!TARGET_OLDABI)
4717     flag_pcc_struct_return = 0;
4718
4719 #if defined(USE_COLLECT2)
4720   /* For IRIX 5 or IRIX 6 with integrated O32 ABI support, USE_COLLECT2 is
4721      always defined when GNU as is not in use, but collect2 is only used
4722      for the O32 ABI, so override the toplev.c and target-def.h defaults
4723      for flag_gnu_linker, TARGET_ASM_{CONSTRUCTOR, DESTRUCTOR} and
4724      TARGET_HAVE_CTORS_DTORS.
4725
4726      Since the IRIX 5 and IRIX 6 O32 assemblers cannot handle named
4727      sections, constructor/destructor handling depends on the ABI in use.
4728
4729      Since USE_COLLECT2 is defined, we only need to restore the non-collect2
4730      defaults for the N32/N64 ABIs.  */
4731   if (TARGET_IRIX && !TARGET_SGI_O32_AS)
4732     {
4733       targetm.have_ctors_dtors = true;
4734       targetm.asm_out.constructor = default_named_section_asm_out_constructor;
4735       targetm.asm_out.destructor = default_named_section_asm_out_destructor;
4736     }
4737 #endif
4738
4739   /* Handle some quirks of the IRIX 5 and IRIX 6 O32 assemblers.  */
4740
4741   if (TARGET_SGI_O32_AS)
4742     {
4743       /* They don't recognize `.[248]byte'.  */
4744       targetm.asm_out.unaligned_op.hi = "\t.align 0\n\t.half\t";
4745       targetm.asm_out.unaligned_op.si = "\t.align 0\n\t.word\t";
4746       /* The IRIX 6 O32 assembler gives an error for `align 0; .dword',
4747          contrary to the documentation, so disable it.  */
4748       targetm.asm_out.unaligned_op.di = NULL;
4749
4750       /* They cannot handle named sections.  */
4751       targetm.have_named_sections = false;
4752       /* Therefore, EH_FRAME_SECTION_NAME isn't defined and we must use
4753          collect2.  */
4754       targetm.terminate_dw2_eh_frame_info = true;
4755       targetm.asm_out.eh_frame_section = collect2_eh_frame_section;
4756
4757       /* They cannot handle debug information.  */
4758       if (write_symbols != NO_DEBUG)
4759         {
4760           /* Adapt wording to IRIX version: IRIX 5 only had a single ABI,
4761              so -mabi=32 isn't usually specified.  */
4762           if (TARGET_IRIX5)
4763             inform ("-g is only supported using GNU as,");
4764           else
4765             inform ("-g is only supported using GNU as with -mabi=32,");
4766           inform ("-g option disabled");
4767           write_symbols = NO_DEBUG;
4768         }
4769     }
4770
4771   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
4772     {
4773       /* If neither -mbranch-likely nor -mno-branch-likely was given
4774          on the command line, set MASK_BRANCHLIKELY based on the target
4775          architecture.
4776
4777          By default, we enable use of Branch Likely instructions on
4778          all architectures which support them except for MIPS32 and MIPS64
4779          (i.e., the generic MIPS32 and MIPS64 ISAs, and processors which
4780          implement them).
4781
4782          The MIPS32 and MIPS64 architecture specifications say "Software
4783          is strongly encouraged to avoid use of Branch Likely
4784          instructions, as they will be removed from a future revision
4785          of the [MIPS32 and MIPS64] architecture."  Therefore, we do not
4786          issue those instructions unless instructed to do so by
4787          -mbranch-likely.  */
4788       if (ISA_HAS_BRANCHLIKELY && !(ISA_MIPS32 || ISA_MIPS32R2 || ISA_MIPS64))
4789         target_flags |= MASK_BRANCHLIKELY;
4790       else
4791         target_flags &= ~MASK_BRANCHLIKELY;
4792     }
4793   if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
4794     warning ("generation of Branch Likely instructions enabled, but not supported by architecture");
4795
4796   /* The effect of -mabicalls isn't defined for the EABI.  */
4797   if (mips_abi == ABI_EABI && TARGET_ABICALLS)
4798     {
4799       error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
4800       target_flags &= ~MASK_ABICALLS;
4801     }
4802
4803   /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined.  We need
4804      to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work.  */
4805   /* ??? -non_shared turns off pic code generation, but this is not
4806      implemented.  */
4807   if (TARGET_ABICALLS)
4808     {
4809       flag_pic = 1;
4810       if (mips_section_threshold > 0)
4811         warning ("-G is incompatible with PIC code which is the default");
4812     }
4813
4814   /* The MIPS and SGI o32 assemblers expect small-data variables to
4815      be declared before they are used.  Although we once had code to
4816      do this, it was very invasive and fragile.  It no longer seems
4817      worth the effort.  */
4818   if (!TARGET_EXPLICIT_RELOCS && !TARGET_GAS)
4819     mips_section_threshold = 0;
4820
4821   /* We switch to small data sections using ".section", which the native
4822      o32 irix assemblers don't understand.  Disable -G accordingly.
4823      We must do this regardless of command-line options since otherwise
4824      the compiler would abort.  */
4825   if (!targetm.have_named_sections)
4826     mips_section_threshold = 0;
4827
4828   /* -membedded-pic is a form of PIC code suitable for embedded
4829      systems.  All calls are made using PC relative addressing, and
4830      all data is addressed using the $gp register.  This requires gas,
4831      which does most of the work, and GNU ld, which automatically
4832      expands PC relative calls which are out of range into a longer
4833      instruction sequence.  All gcc really does differently is
4834      generate a different sequence for a switch.  */
4835   if (TARGET_EMBEDDED_PIC)
4836     {
4837       flag_pic = 1;
4838       if (TARGET_ABICALLS)
4839         warning ("-membedded-pic and -mabicalls are incompatible");
4840
4841       if (g_switch_set)
4842         warning ("-G and -membedded-pic are incompatible");
4843
4844       /* Setting mips_section_threshold is not required, because gas
4845          will force everything to be GP addressable anyhow, but
4846          setting it will cause gcc to make better estimates of the
4847          number of instructions required to access a particular data
4848          item.  */
4849       mips_section_threshold = 0x7fffffff;
4850     }
4851
4852   /* mips_split_addresses is a half-way house between explicit
4853      relocations and the traditional assembler macros.  It can
4854      split absolute 32-bit symbolic constants into a high/lo_sum
4855      pair but uses macros for other sorts of access.
4856
4857      Like explicit relocation support for REL targets, it relies
4858      on GNU extensions in the assembler and the linker.
4859
4860      Although this code should work for -O0, it has traditionally
4861      been treated as an optimization.  */
4862   if (TARGET_GAS && !TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES
4863       && optimize && !flag_pic
4864       && !ABI_HAS_64BIT_SYMBOLS)
4865     mips_split_addresses = 1;
4866   else
4867     mips_split_addresses = 0;
4868
4869   /* -mexplicit-relocs doesn't yet support non-PIC n64.  We don't know
4870      how to generate %highest/%higher/%hi/%lo sequences.  */
4871   if (mips_abi == ABI_64 && !TARGET_ABICALLS)
4872     {
4873       if ((target_flags_explicit & target_flags & MASK_EXPLICIT_RELOCS) != 0)
4874         sorry ("non-PIC n64 with explicit relocations");
4875       target_flags &= ~MASK_EXPLICIT_RELOCS;
4876     }
4877
4878   /* Explicit relocations for "old" ABIs are a GNU extension.  Unless
4879      the user has said otherwise, assume that they are not available
4880      with assemblers other than gas.  */
4881   if (!TARGET_NEWABI && !TARGET_GAS
4882       && (target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4883     target_flags &= ~MASK_EXPLICIT_RELOCS;
4884
4885   /* Make -mabicalls -fno-unit-at-a-time imply -mno-explicit-relocs
4886      unless the user says otherwise.
4887
4888      There are two problems here:
4889
4890        (1) The value of an R_MIPS_GOT16 relocation depends on whether
4891            the symbol is local or global.  We therefore need to know
4892            a symbol's binding before refering to it using %got().
4893
4894        (2) R_MIPS_CALL16 can only be applied to global symbols.
4895
4896      When not using -funit-at-a-time, a symbol's binding may change
4897      after it has been used.  For example, the C++ front-end will
4898      initially assume that the typeinfo for an incomplete type will be
4899      comdat, on the basis that the type could be completed later in the
4900      file.  But if the type never is completed, the typeinfo will become
4901      local instead.  */
4902   if (!flag_unit_at_a_time
4903       && TARGET_ABICALLS
4904       && (target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4905     target_flags &= ~MASK_EXPLICIT_RELOCS;
4906
4907   /* -mrnames says to use the MIPS software convention for register
4908      names instead of the hardware names (ie, $a0 instead of $4).
4909      We do this by switching the names in mips_reg_names, which the
4910      reg_names points into via the REGISTER_NAMES macro.  */
4911
4912   if (TARGET_NAME_REGS)
4913     memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
4914
4915   /* When compiling for the mips16, we can not use floating point.  We
4916      record the original hard float value in mips16_hard_float.  */
4917   if (TARGET_MIPS16)
4918     {
4919       if (TARGET_SOFT_FLOAT)
4920         mips16_hard_float = 0;
4921       else
4922         mips16_hard_float = 1;
4923       target_flags |= MASK_SOFT_FLOAT;
4924
4925       /* Don't run the scheduler before reload, since it tends to
4926          increase register pressure.  */
4927       flag_schedule_insns = 0;
4928
4929       /* Silently disable -mexplicit-relocs since it doesn't apply
4930          to mips16 code.  Even so, it would overly pedantic to warn
4931          about "-mips16 -mexplicit-relocs", especially given that
4932          we use a %gprel() operator.  */
4933       target_flags &= ~MASK_EXPLICIT_RELOCS;
4934     }
4935
4936   /* When using explicit relocs, we call dbr_schedule from within
4937      mips_reorg.  */
4938   if (TARGET_EXPLICIT_RELOCS)
4939     {
4940       mips_flag_delayed_branch = flag_delayed_branch;
4941       flag_delayed_branch = 0;
4942     }
4943
4944 #ifdef MIPS_TFMODE_FORMAT
4945   REAL_MODE_FORMAT (TFmode) = &MIPS_TFMODE_FORMAT;
4946 #endif
4947
4948   mips_print_operand_punct['?'] = 1;
4949   mips_print_operand_punct['#'] = 1;
4950   mips_print_operand_punct['/'] = 1;
4951   mips_print_operand_punct['&'] = 1;
4952   mips_print_operand_punct['!'] = 1;
4953   mips_print_operand_punct['*'] = 1;
4954   mips_print_operand_punct['@'] = 1;
4955   mips_print_operand_punct['.'] = 1;
4956   mips_print_operand_punct['('] = 1;
4957   mips_print_operand_punct[')'] = 1;
4958   mips_print_operand_punct['['] = 1;
4959   mips_print_operand_punct[']'] = 1;
4960   mips_print_operand_punct['<'] = 1;
4961   mips_print_operand_punct['>'] = 1;
4962   mips_print_operand_punct['{'] = 1;
4963   mips_print_operand_punct['}'] = 1;
4964   mips_print_operand_punct['^'] = 1;
4965   mips_print_operand_punct['$'] = 1;
4966   mips_print_operand_punct['+'] = 1;
4967   mips_print_operand_punct['~'] = 1;
4968
4969   mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
4970   mips_char_to_class['t'] = T_REG;
4971   mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
4972   mips_char_to_class['h'] = HI_REG;
4973   mips_char_to_class['l'] = LO_REG;
4974   mips_char_to_class['x'] = MD_REGS;
4975   mips_char_to_class['b'] = ALL_REGS;
4976   mips_char_to_class['c'] = (TARGET_ABICALLS ? PIC_FN_ADDR_REG :
4977                              TARGET_MIPS16 ? M16_NA_REGS :
4978                              GR_REGS);
4979   mips_char_to_class['e'] = LEA_REGS;
4980   mips_char_to_class['j'] = PIC_FN_ADDR_REG;
4981   mips_char_to_class['y'] = GR_REGS;
4982   mips_char_to_class['z'] = ST_REGS;
4983   mips_char_to_class['B'] = COP0_REGS;
4984   mips_char_to_class['C'] = COP2_REGS;
4985   mips_char_to_class['D'] = COP3_REGS;
4986
4987   /* Set up array to map GCC register number to debug register number.
4988      Ignore the special purpose register numbers.  */
4989
4990   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4991     mips_dbx_regno[i] = -1;
4992
4993   start = GP_DBX_FIRST - GP_REG_FIRST;
4994   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
4995     mips_dbx_regno[i] = i + start;
4996
4997   start = FP_DBX_FIRST - FP_REG_FIRST;
4998   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
4999     mips_dbx_regno[i] = i + start;
5000
5001   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
5002   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
5003
5004   /* Set up array giving whether a given register can hold a given mode.  */
5005
5006   for (mode = VOIDmode;
5007        mode != MAX_MACHINE_MODE;
5008        mode = (enum machine_mode) ((int)mode + 1))
5009     {
5010       register int size              = GET_MODE_SIZE (mode);
5011       register enum mode_class class = GET_MODE_CLASS (mode);
5012
5013       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5014         {
5015           register int temp;
5016
5017           if (mode == CCmode)
5018             {
5019               if (! ISA_HAS_8CC)
5020                 temp = (regno == FPSW_REGNUM);
5021               else
5022                 temp = (ST_REG_P (regno) || GP_REG_P (regno)
5023                         || FP_REG_P (regno));
5024             }
5025
5026           else if (GP_REG_P (regno))
5027             temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
5028
5029           else if (FP_REG_P (regno))
5030             temp = ((regno % FP_INC) == 0)
5031                     && (((class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
5032                          && size <= UNITS_PER_FPVALUE)
5033                         /* Allow integer modes that fit into a single
5034                            register.  We need to put integers into FPRs
5035                            when using instructions like cvt and trunc.  */
5036                         || (class == MODE_INT && size <= UNITS_PER_FPREG)
5037                         /* Allow TFmode for CCmode reloads.  */
5038                         || (ISA_HAS_8CC && mode == TFmode));
5039
5040           else if (MD_REG_P (regno))
5041             temp = (class == MODE_INT
5042                     && (size <= UNITS_PER_WORD
5043                         || (regno == MD_REG_FIRST
5044                             && size == 2 * UNITS_PER_WORD)));
5045
5046           else if (ALL_COP_REG_P (regno))
5047             temp = (class == MODE_INT && size <= UNITS_PER_WORD);
5048           else
5049             temp = 0;
5050
5051           mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5052         }
5053     }
5054
5055   /* Save GPR registers in word_mode sized hunks.  word_mode hasn't been
5056      initialized yet, so we can't use that here.  */
5057   gpr_mode = TARGET_64BIT ? DImode : SImode;
5058
5059   /* Provide default values for align_* for 64-bit targets.  */
5060   if (TARGET_64BIT && !TARGET_MIPS16)
5061     {
5062       if (align_loops == 0)
5063         align_loops = 8;
5064       if (align_jumps == 0)
5065         align_jumps = 8;
5066       if (align_functions == 0)
5067         align_functions = 8;
5068     }
5069
5070   /* Function to allocate machine-dependent function status.  */
5071   init_machine_status = &mips_init_machine_status;
5072
5073   if (TARGET_EXPLICIT_RELOCS || mips_split_addresses)
5074     {
5075       mips_split_p[SYMBOL_GENERAL] = true;
5076       mips_hi_relocs[SYMBOL_GENERAL] = "%hi(";
5077       mips_lo_relocs[SYMBOL_GENERAL] = "%lo(";
5078     }
5079
5080   if (TARGET_MIPS16)
5081     {
5082       /* The high part is provided by a pseudo copy of $gp.  */
5083       mips_split_p[SYMBOL_SMALL_DATA] = true;
5084       mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gprel(";
5085     }
5086
5087   if (TARGET_EXPLICIT_RELOCS)
5088     {
5089       /* Small data constants are kept whole until after reload,
5090          then lowered by mips_rewrite_small_data.  */
5091       mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gp_rel(";
5092
5093       mips_split_p[SYMBOL_GOT_LOCAL] = true;
5094       if (TARGET_NEWABI)
5095         {
5096           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
5097           mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%got_ofst(";
5098         }
5099       else
5100         {
5101           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
5102           mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%lo(";
5103         }
5104
5105       if (TARGET_XGOT)
5106         {
5107           /* The HIGH and LO_SUM are matched by special .md patterns.  */
5108           mips_split_p[SYMBOL_GOT_GLOBAL] = true;
5109
5110           mips_split_p[SYMBOL_GOTOFF_GLOBAL] = true;
5111           mips_hi_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_hi(";
5112           mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_lo(";
5113
5114           mips_split_p[SYMBOL_GOTOFF_CALL] = true;
5115           mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
5116           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
5117         }
5118       else
5119         {
5120           if (TARGET_NEWABI)
5121             mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_disp(";
5122           else
5123             mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got(";
5124           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
5125         }
5126     }
5127
5128   if (TARGET_NEWABI)
5129     {
5130       mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
5131       mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
5132       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
5133     }
5134
5135   /* Default to working around R4000 errata only if the processor
5136      was selected explicitly.  */
5137   if ((target_flags_explicit & MASK_FIX_R4000) == 0
5138       && mips_matching_cpu_name_p (mips_arch_info->name, "r4000"))
5139     target_flags |= MASK_FIX_R4000;
5140 }
5141
5142 /* Implement CONDITIONAL_REGISTER_USAGE.  */
5143
5144 void
5145 mips_conditional_register_usage (void)
5146 {
5147   if (!TARGET_HARD_FLOAT)
5148     {
5149       int regno;
5150
5151       for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5152         fixed_regs[regno] = call_used_regs[regno] = 1;
5153       for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5154         fixed_regs[regno] = call_used_regs[regno] = 1;
5155     }
5156   else if (! ISA_HAS_8CC)
5157     {
5158       int regno;
5159
5160       /* We only have a single condition code register.  We
5161          implement this by hiding all the condition code registers,
5162          and generating RTL that refers directly to ST_REG_FIRST.  */
5163       for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5164         fixed_regs[regno] = call_used_regs[regno] = 1;
5165     }
5166   /* In mips16 mode, we permit the $t temporary registers to be used
5167      for reload.  We prohibit the unused $s registers, since they
5168      are caller saved, and saving them via a mips16 register would
5169      probably waste more time than just reloading the value.  */
5170   if (TARGET_MIPS16)
5171     {
5172       fixed_regs[18] = call_used_regs[18] = 1;
5173       fixed_regs[19] = call_used_regs[19] = 1;
5174       fixed_regs[20] = call_used_regs[20] = 1;
5175       fixed_regs[21] = call_used_regs[21] = 1;
5176       fixed_regs[22] = call_used_regs[22] = 1;
5177       fixed_regs[23] = call_used_regs[23] = 1;
5178       fixed_regs[26] = call_used_regs[26] = 1;
5179       fixed_regs[27] = call_used_regs[27] = 1;
5180       fixed_regs[30] = call_used_regs[30] = 1;
5181     }
5182   /* fp20-23 are now caller saved.  */
5183   if (mips_abi == ABI_64)
5184     {
5185       int regno;
5186       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
5187         call_really_used_regs[regno] = call_used_regs[regno] = 1;
5188     }
5189   /* Odd registers from fp21 to fp31 are now caller saved.  */
5190   if (mips_abi == ABI_N32)
5191     {
5192       int regno;
5193       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
5194         call_really_used_regs[regno] = call_used_regs[regno] = 1;
5195     }
5196 }
5197
5198 /* Allocate a chunk of memory for per-function machine-dependent data.  */
5199 static struct machine_function *
5200 mips_init_machine_status (void)
5201 {
5202   return ((struct machine_function *)
5203           ggc_alloc_cleared (sizeof (struct machine_function)));
5204 }
5205
5206 /* On the mips16, we want to allocate $24 (T_REG) before other
5207    registers for instructions for which it is possible.  This helps
5208    avoid shuffling registers around in order to set up for an xor,
5209    encouraging the compiler to use a cmp instead.  */
5210
5211 void
5212 mips_order_regs_for_local_alloc (void)
5213 {
5214   register int i;
5215
5216   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5217     reg_alloc_order[i] = i;
5218
5219   if (TARGET_MIPS16)
5220     {
5221       /* It really doesn't matter where we put register 0, since it is
5222          a fixed register anyhow.  */
5223       reg_alloc_order[0] = 24;
5224       reg_alloc_order[24] = 0;
5225     }
5226 }
5227
5228 \f
5229 /* The MIPS debug format wants all automatic variables and arguments
5230    to be in terms of the virtual frame pointer (stack pointer before
5231    any adjustment in the function), while the MIPS 3.0 linker wants
5232    the frame pointer to be the stack pointer after the initial
5233    adjustment.  So, we do the adjustment here.  The arg pointer (which
5234    is eliminated) points to the virtual frame pointer, while the frame
5235    pointer (which may be eliminated) points to the stack pointer after
5236    the initial adjustments.  */
5237
5238 HOST_WIDE_INT
5239 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
5240 {
5241   rtx offset2 = const0_rtx;
5242   rtx reg = eliminate_constant_term (addr, &offset2);
5243
5244   if (offset == 0)
5245     offset = INTVAL (offset2);
5246
5247   if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5248       || reg == hard_frame_pointer_rtx)
5249     {
5250       HOST_WIDE_INT frame_size = (!cfun->machine->frame.initialized)
5251                                   ? compute_frame_size (get_frame_size ())
5252                                   : cfun->machine->frame.total_size;
5253
5254       /* MIPS16 frame is smaller */
5255       if (frame_pointer_needed && TARGET_MIPS16)
5256         frame_size -= cfun->machine->frame.args_size;
5257
5258       offset = offset - frame_size;
5259     }
5260
5261   /* sdbout_parms does not want this to crash for unrecognized cases.  */
5262 #if 0
5263   else if (reg != arg_pointer_rtx)
5264     fatal_insn ("mips_debugger_offset called with non stack/frame/arg pointer",
5265                 addr);
5266 #endif
5267
5268   return offset;
5269 }
5270 \f
5271 /* Implement the PRINT_OPERAND macro.  The MIPS-specific operand codes are:
5272
5273    'X'  OP is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5274    'x'  OP is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
5275    'h'  OP is HIGH, prints %hi(X),
5276    'd'  output integer constant in decimal,
5277    'z'  if the operand is 0, use $0 instead of normal operand.
5278    'D'  print second part of double-word register or memory operand.
5279    'L'  print low-order register of double-word register operand.
5280    'M'  print high-order register of double-word register operand.
5281    'C'  print part of opcode for a branch condition.
5282    'F'  print part of opcode for a floating-point branch condition.
5283    'N'  print part of opcode for a branch condition, inverted.
5284    'W'  print part of opcode for a floating-point branch condition, inverted.
5285    'S'  OP is CODE_LABEL, print with prefix of "LS" (for embedded switch).
5286    'B'  print 'z' for EQ, 'n' for NE
5287    'b'  print 'n' for EQ, 'z' for NE
5288    'T'  print 'f' for EQ, 't' for NE
5289    't'  print 't' for EQ, 'f' for NE
5290    'Z'  print register and a comma, but print nothing for $fcc0
5291    'R'  print the reloc associated with LO_SUM
5292
5293    The punctuation characters are:
5294
5295    '('  Turn on .set noreorder
5296    ')'  Turn on .set reorder
5297    '['  Turn on .set noat
5298    ']'  Turn on .set at
5299    '<'  Turn on .set nomacro
5300    '>'  Turn on .set macro
5301    '{'  Turn on .set volatile (not GAS)
5302    '}'  Turn on .set novolatile (not GAS)
5303    '&'  Turn on .set noreorder if filling delay slots
5304    '*'  Turn on both .set noreorder and .set nomacro if filling delay slots
5305    '!'  Turn on .set nomacro if filling delay slots
5306    '#'  Print nop if in a .set noreorder section.
5307    '/'  Like '#', but does nothing within a delayed branch sequence
5308    '?'  Print 'l' if we are to use a branch likely instead of normal branch.
5309    '@'  Print the name of the assembler temporary register (at or $1).
5310    '.'  Print the name of the register with a hard-wired zero (zero or $0).
5311    '^'  Print the name of the pic call-through register (t9 or $25).
5312    '$'  Print the name of the stack pointer register (sp or $29).
5313    '+'  Print the name of the gp register (usually gp or $28).
5314    '~'  Output a branch alignment to LABEL_ALIGN(NULL).  */
5315
5316 void
5317 print_operand (FILE *file, rtx op, int letter)
5318 {
5319   register enum rtx_code code;
5320
5321   if (PRINT_OPERAND_PUNCT_VALID_P (letter))
5322     {
5323       switch (letter)
5324         {
5325         case '?':
5326           if (mips_branch_likely)
5327             putc ('l', file);
5328           break;
5329
5330         case '@':
5331           fputs (reg_names [GP_REG_FIRST + 1], file);
5332           break;
5333
5334         case '^':
5335           fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
5336           break;
5337
5338         case '.':
5339           fputs (reg_names [GP_REG_FIRST + 0], file);
5340           break;
5341
5342         case '$':
5343           fputs (reg_names[STACK_POINTER_REGNUM], file);
5344           break;
5345
5346         case '+':
5347           fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
5348           break;
5349
5350         case '&':
5351           if (final_sequence != 0 && set_noreorder++ == 0)
5352             fputs (".set\tnoreorder\n\t", file);
5353           break;
5354
5355         case '*':
5356           if (final_sequence != 0)
5357             {
5358               if (set_noreorder++ == 0)
5359                 fputs (".set\tnoreorder\n\t", file);
5360
5361               if (set_nomacro++ == 0)
5362                 fputs (".set\tnomacro\n\t", file);
5363             }
5364           break;
5365
5366         case '!':
5367           if (final_sequence != 0 && set_nomacro++ == 0)
5368             fputs ("\n\t.set\tnomacro", file);
5369           break;
5370
5371         case '#':
5372           if (set_noreorder != 0)
5373             fputs ("\n\tnop", file);
5374           break;
5375
5376         case '/':
5377           /* Print an extra newline so that the delayed insn is separated
5378              from the following ones.  This looks neater and is consistent
5379              with non-nop delayed sequences.  */
5380           if (set_noreorder != 0 && final_sequence == 0)
5381             fputs ("\n\tnop\n", file);
5382           break;
5383
5384         case '(':
5385           if (set_noreorder++ == 0)
5386             fputs (".set\tnoreorder\n\t", file);
5387           break;
5388
5389         case ')':
5390           if (set_noreorder == 0)
5391             error ("internal error: %%) found without a %%( in assembler pattern");
5392
5393           else if (--set_noreorder == 0)
5394             fputs ("\n\t.set\treorder", file);
5395
5396           break;
5397
5398         case '[':
5399           if (set_noat++ == 0)
5400             fputs (".set\tnoat\n\t", file);
5401           break;
5402
5403         case ']':
5404           if (set_noat == 0)
5405             error ("internal error: %%] found without a %%[ in assembler pattern");
5406           else if (--set_noat == 0)
5407             fputs ("\n\t.set\tat", file);
5408
5409           break;
5410
5411         case '<':
5412           if (set_nomacro++ == 0)
5413             fputs (".set\tnomacro\n\t", file);
5414           break;
5415
5416         case '>':
5417           if (set_nomacro == 0)
5418             error ("internal error: %%> found without a %%< in assembler pattern");
5419           else if (--set_nomacro == 0)
5420             fputs ("\n\t.set\tmacro", file);
5421
5422           break;
5423
5424         case '{':
5425           if (set_volatile++ == 0)
5426             fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
5427           break;
5428
5429         case '}':
5430           if (set_volatile == 0)
5431             error ("internal error: %%} found without a %%{ in assembler pattern");
5432           else if (--set_volatile == 0)
5433             fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
5434
5435           break;
5436
5437         case '~':
5438           {
5439             if (align_labels_log > 0)
5440               ASM_OUTPUT_ALIGN (file, align_labels_log);
5441           }
5442         break;
5443
5444         default:
5445           error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
5446           break;
5447         }
5448
5449       return;
5450     }
5451
5452   if (! op)
5453     {
5454       error ("PRINT_OPERAND null pointer");
5455       return;
5456     }
5457
5458   code = GET_CODE (op);
5459
5460   if (letter == 'C')
5461     switch (code)
5462       {
5463       case EQ:  fputs ("eq",  file); break;
5464       case NE:  fputs ("ne",  file); break;
5465       case GT:  fputs ("gt",  file); break;
5466       case GE:  fputs ("ge",  file); break;
5467       case LT:  fputs ("lt",  file); break;
5468       case LE:  fputs ("le",  file); break;
5469       case GTU: fputs ("gtu", file); break;
5470       case GEU: fputs ("geu", file); break;
5471       case LTU: fputs ("ltu", file); break;
5472       case LEU: fputs ("leu", file); break;
5473       default:
5474         fatal_insn ("PRINT_OPERAND, invalid insn for %%C", op);
5475       }
5476
5477   else if (letter == 'N')
5478     switch (code)
5479       {
5480       case EQ:  fputs ("ne",  file); break;
5481       case NE:  fputs ("eq",  file); break;
5482       case GT:  fputs ("le",  file); break;
5483       case GE:  fputs ("lt",  file); break;
5484       case LT:  fputs ("ge",  file); break;
5485       case LE:  fputs ("gt",  file); break;
5486       case GTU: fputs ("leu", file); break;
5487       case GEU: fputs ("ltu", file); break;
5488       case LTU: fputs ("geu", file); break;
5489       case LEU: fputs ("gtu", file); break;
5490       default:
5491         fatal_insn ("PRINT_OPERAND, invalid insn for %%N", op);
5492       }
5493
5494   else if (letter == 'F')
5495     switch (code)
5496       {
5497       case EQ: fputs ("c1f", file); break;
5498       case NE: fputs ("c1t", file); break;
5499       default:
5500         fatal_insn ("PRINT_OPERAND, invalid insn for %%F", op);
5501       }
5502
5503   else if (letter == 'W')
5504     switch (code)
5505       {
5506       case EQ: fputs ("c1t", file); break;
5507       case NE: fputs ("c1f", file); break;
5508       default:
5509         fatal_insn ("PRINT_OPERAND, invalid insn for %%W", op);
5510       }
5511
5512   else if (letter == 'h')
5513     {
5514       if (GET_CODE (op) == HIGH)
5515         op = XEXP (op, 0);
5516
5517       print_operand_reloc (file, op, mips_hi_relocs);
5518     }
5519
5520   else if (letter == 'R')
5521     print_operand_reloc (file, op, mips_lo_relocs);
5522
5523   else if (letter == 'S')
5524     {
5525       char buffer[100];
5526
5527       ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
5528       assemble_name (file, buffer);
5529     }
5530
5531   else if (letter == 'Z')
5532     {
5533       register int regnum;
5534
5535       if (code != REG)
5536         abort ();
5537
5538       regnum = REGNO (op);
5539       if (! ST_REG_P (regnum))
5540         abort ();
5541
5542       if (regnum != ST_REG_FIRST)
5543         fprintf (file, "%s,", reg_names[regnum]);
5544     }
5545
5546   else if (code == REG || code == SUBREG)
5547     {
5548       register int regnum;
5549
5550       if (code == REG)
5551         regnum = REGNO (op);
5552       else
5553         regnum = true_regnum (op);
5554
5555       if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
5556           || (letter == 'L' && WORDS_BIG_ENDIAN)
5557           || letter == 'D')
5558         regnum++;
5559
5560       fprintf (file, "%s", reg_names[regnum]);
5561     }
5562
5563   else if (code == MEM)
5564     {
5565       if (letter == 'D')
5566         output_address (plus_constant (XEXP (op, 0), 4));
5567       else
5568         output_address (XEXP (op, 0));
5569     }
5570
5571   else if (letter == 'x' && GET_CODE (op) == CONST_INT)
5572     fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
5573
5574   else if (letter == 'X' && GET_CODE(op) == CONST_INT)
5575     fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
5576
5577   else if (letter == 'd' && GET_CODE(op) == CONST_INT)
5578     fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
5579
5580   else if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
5581     fputs (reg_names[GP_REG_FIRST], file);
5582
5583   else if (letter == 'd' || letter == 'x' || letter == 'X')
5584     output_operand_lossage ("invalid use of %%d, %%x, or %%X");
5585
5586   else if (letter == 'B')
5587     fputs (code == EQ ? "z" : "n", file);
5588   else if (letter == 'b')
5589     fputs (code == EQ ? "n" : "z", file);
5590   else if (letter == 'T')
5591     fputs (code == EQ ? "f" : "t", file);
5592   else if (letter == 't')
5593     fputs (code == EQ ? "t" : "f", file);
5594
5595   else if (CONST_GP_P (op))
5596     print_operand (file, XEXP (op, 0), letter);
5597
5598   else
5599     output_addr_const (file, op);
5600 }
5601
5602
5603 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM.
5604    RELOCS is the array of relocations to use.  */
5605
5606 static void
5607 print_operand_reloc (FILE *file, rtx op, const char **relocs)
5608 {
5609   enum mips_symbol_type symbol_type;
5610   const char *p;
5611   rtx base;
5612   HOST_WIDE_INT offset;
5613
5614   if (!mips_symbolic_constant_p (op, &symbol_type) || relocs[symbol_type] == 0)
5615     fatal_insn ("PRINT_OPERAND, invalid operand for relocation", op);
5616
5617   /* If OP uses an UNSPEC address, we want to print the inner symbol.  */
5618   mips_split_const (op, &base, &offset);
5619   if (UNSPEC_ADDRESS_P (base))
5620     op = plus_constant (UNSPEC_ADDRESS (base), offset);
5621
5622   fputs (relocs[symbol_type], file);
5623   output_addr_const (file, op);
5624   for (p = relocs[symbol_type]; *p != 0; p++)
5625     if (*p == '(')
5626       fputc (')', file);
5627 }
5628 \f
5629 /* Output address operand X to FILE.  */
5630
5631 void
5632 print_operand_address (FILE *file, rtx x)
5633 {
5634   struct mips_address_info addr;
5635
5636   if (mips_classify_address (&addr, x, word_mode, true))
5637     switch (addr.type)
5638       {
5639       case ADDRESS_REG:
5640         print_operand (file, addr.offset, 0);
5641         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5642         return;
5643
5644       case ADDRESS_LO_SUM:
5645         print_operand (file, addr.offset, 'R');
5646         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5647         return;
5648
5649       case ADDRESS_CONST_INT:
5650       case ADDRESS_SYMBOLIC:
5651         output_addr_const (file, x);
5652         return;
5653       }
5654   abort ();
5655 }
5656 \f
5657 /* Target hook for assembling integer objects.  It appears that the Irix
5658    6 assembler can't handle 64-bit decimal integers, so avoid printing
5659    such an integer here.  */
5660
5661 static bool
5662 mips_assemble_integer (rtx x, unsigned int size, int aligned_p)
5663 {
5664   if ((TARGET_64BIT || TARGET_GAS) && size == 8 && aligned_p)
5665     {
5666       fputs ("\t.dword\t", asm_out_file);
5667       if (HOST_BITS_PER_WIDE_INT < 64 || GET_CODE (x) != CONST_INT)
5668         output_addr_const (asm_out_file, x);
5669       else
5670         print_operand (asm_out_file, x, 'X');
5671       fputc ('\n', asm_out_file);
5672       return true;
5673     }
5674   return default_assemble_integer (x, size, aligned_p);
5675 }
5676 \f
5677 /* When using assembler macros, keep track of all of small-data externs
5678    so that mips_file_end can emit the appropriate declarations for them.
5679
5680    In most cases it would be safe (though pointless) to emit .externs
5681    for other symbols too.  One exception is when an object is within
5682    the -G limit but declared by the user to be in a section other
5683    than .sbss or .sdata.  */
5684
5685 int
5686 mips_output_external (FILE *file ATTRIBUTE_UNUSED, tree decl, const char *name)
5687 {
5688   register struct extern_list *p;
5689
5690   if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
5691     {
5692       p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5693       p->next = extern_head;
5694       p->name = name;
5695       p->size = int_size_in_bytes (TREE_TYPE (decl));
5696       extern_head = p;
5697     }
5698
5699   if (TARGET_IRIX && mips_abi == ABI_32 && TREE_CODE (decl) == FUNCTION_DECL)
5700     {
5701       p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5702       p->next = extern_head;
5703       p->name = name;
5704       p->size = -1;
5705       extern_head = p;
5706     }
5707
5708   return 0;
5709 }
5710
5711 #if TARGET_IRIX
5712 void
5713 irix_output_external_libcall (rtx fun)
5714 {
5715   register struct extern_list *p;
5716
5717   if (mips_abi == ABI_32)
5718     {
5719       p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5720       p->next = extern_head;
5721       p->name = XSTR (fun, 0);
5722       p->size = -1;
5723       extern_head = p;
5724     }
5725 }
5726 #endif
5727 \f
5728 /* Emit a new filename to a stream.  If we are smuggling stabs, try to
5729    put out a MIPS ECOFF file and a stab.  */
5730
5731 void
5732 mips_output_filename (FILE *stream, const char *name)
5733 {
5734   char ltext_label_name[100];
5735
5736   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
5737      directives.  */
5738   if (write_symbols == DWARF2_DEBUG)
5739     return;
5740   else if (mips_output_filename_first_time)
5741     {
5742       mips_output_filename_first_time = 0;
5743       SET_FILE_NUMBER ();
5744       current_function_file = name;
5745       ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5746       /* This tells mips-tfile that stabs will follow.  */
5747       if (!TARGET_GAS && write_symbols == DBX_DEBUG)
5748         fprintf (stream, "\t#@stabs\n");
5749     }
5750
5751   else if (write_symbols == DBX_DEBUG)
5752     {
5753       ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
5754       fprintf (stream, "%s", ASM_STABS_OP);
5755       output_quoted_string (stream, name);
5756       fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
5757     }
5758
5759   else if (name != current_function_file
5760            && strcmp (name, current_function_file) != 0)
5761     {
5762       SET_FILE_NUMBER ();
5763       current_function_file = name;
5764       ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5765     }
5766 }
5767 \f
5768 /* Emit a linenumber.  For encapsulated stabs, we need to put out a stab
5769    as well as a .loc, since it is possible that MIPS ECOFF might not be
5770    able to represent the location for inlines that come from a different
5771    file.  */
5772
5773 void
5774 mips_output_lineno (FILE *stream, int line)
5775 {
5776   if (write_symbols == DBX_DEBUG)
5777     {
5778       ++sym_lineno;
5779       fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
5780                LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
5781                LOCAL_LABEL_PREFIX, sym_lineno);
5782     }
5783   else
5784     {
5785       fprintf (stream, "\n\t.loc\t%d %d\n", num_source_filenames, line);
5786       LABEL_AFTER_LOC (stream);
5787     }
5788 }
5789 \f
5790 /* Output an ASCII string, in a space-saving way.  PREFIX is the string
5791    that should be written before the opening quote, such as "\t.ascii\t"
5792    for real string data or "\t# " for a comment.  */
5793
5794 void
5795 mips_output_ascii (FILE *stream, const char *string_param, size_t len,
5796                    const char *prefix)
5797 {
5798   size_t i;
5799   int cur_pos = 17;
5800   register const unsigned char *string =
5801     (const unsigned char *)string_param;
5802
5803   fprintf (stream, "%s\"", prefix);
5804   for (i = 0; i < len; i++)
5805     {
5806       register int c = string[i];
5807
5808       switch (c)
5809         {
5810         case '\"':
5811         case '\\':
5812           putc ('\\', stream);
5813           putc (c, stream);
5814           cur_pos += 2;
5815           break;
5816
5817         case TARGET_NEWLINE:
5818           fputs ("\\n", stream);
5819           if (i+1 < len
5820               && (((c = string[i+1]) >= '\040' && c <= '~')
5821                   || c == TARGET_TAB))
5822             cur_pos = 32767;            /* break right here */
5823           else
5824             cur_pos += 2;
5825           break;
5826
5827         case TARGET_TAB:
5828           fputs ("\\t", stream);
5829           cur_pos += 2;
5830           break;
5831
5832         case TARGET_FF:
5833           fputs ("\\f", stream);
5834           cur_pos += 2;
5835           break;
5836
5837         case TARGET_BS:
5838           fputs ("\\b", stream);
5839           cur_pos += 2;
5840           break;
5841
5842         case TARGET_CR:
5843           fputs ("\\r", stream);
5844           cur_pos += 2;
5845           break;
5846
5847         default:
5848           if (c >= ' ' && c < 0177)
5849             {
5850               putc (c, stream);
5851               cur_pos++;
5852             }
5853           else
5854             {
5855               fprintf (stream, "\\%03o", c);
5856               cur_pos += 4;
5857             }
5858         }
5859
5860       if (cur_pos > 72 && i+1 < len)
5861         {
5862           cur_pos = 17;
5863           fprintf (stream, "\"\n%s\"", prefix);
5864         }
5865     }
5866   fprintf (stream, "\"\n");
5867 }
5868 \f
5869 /* Implement TARGET_ASM_FILE_START.  */
5870
5871 static void
5872 mips_file_start (void)
5873 {
5874   default_file_start ();
5875
5876   /* Versions of the MIPS assembler before 2.20 generate errors if a branch
5877      inside of a .set noreorder section jumps to a label outside of the .set
5878      noreorder section.  Revision 2.20 just set nobopt silently rather than
5879      fixing the bug.  */
5880
5881   if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
5882     fprintf (asm_out_file, "\t.set\tnobopt\n");
5883
5884   if (TARGET_GAS)
5885     {
5886 #if defined(OBJECT_FORMAT_ELF) && !TARGET_IRIX
5887       /* Generate a special section to describe the ABI switches used to
5888          produce the resultant binary.  This used to be done by the assembler
5889          setting bits in the ELF header's flags field, but we have run out of
5890          bits.  GDB needs this information in order to be able to correctly
5891          debug these binaries.  See the function mips_gdbarch_init() in
5892          gdb/mips-tdep.c.  This is unnecessary for the IRIX 5/6 ABIs and
5893          causes unnecessary IRIX 6 ld warnings.  */
5894       const char * abi_string = NULL;
5895
5896       switch (mips_abi)
5897         {
5898         case ABI_32:   abi_string = "abi32"; break;
5899         case ABI_N32:  abi_string = "abiN32"; break;
5900         case ABI_64:   abi_string = "abi64"; break;
5901         case ABI_O64:  abi_string = "abiO64"; break;
5902         case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
5903         default:
5904           abort ();
5905         }
5906       /* Note - we use fprintf directly rather than called named_section()
5907          because in this way we can avoid creating an allocated section.  We
5908          do not want this section to take up any space in the running
5909          executable.  */
5910       fprintf (asm_out_file, "\t.section .mdebug.%s\n", abi_string);
5911
5912       /* Restore the default section.  */
5913       fprintf (asm_out_file, "\t.previous\n");
5914 #endif
5915     }
5916
5917   /* Generate the pseudo ops that System V.4 wants.  */
5918 #ifndef ABICALLS_ASM_OP
5919 #define ABICALLS_ASM_OP "\t.abicalls"
5920 #endif
5921   if (TARGET_ABICALLS)
5922     /* ??? but do not want this (or want pic0) if -non-shared? */
5923     fprintf (asm_out_file, "%s\n", ABICALLS_ASM_OP);
5924
5925   if (TARGET_MIPS16)
5926     fprintf (asm_out_file, "\t.set\tmips16\n");
5927
5928   if (flag_verbose_asm)
5929     fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
5930              ASM_COMMENT_START,
5931              mips_section_threshold, mips_arch_info->name, mips_isa);
5932 }
5933
5934 #ifdef BSS_SECTION_ASM_OP
5935 /* Implement ASM_OUTPUT_ALIGNED_BSS.  This differs from the default only
5936    in the use of sbss.  */
5937
5938 void
5939 mips_output_aligned_bss (FILE *stream, tree decl, const char *name,
5940                          unsigned HOST_WIDE_INT size, int align)
5941 {
5942   extern tree last_assemble_variable_decl;
5943
5944   if (mips_in_small_data_p (decl))
5945     named_section (0, ".sbss", 0);
5946   else
5947     bss_section ();
5948   ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
5949   last_assemble_variable_decl = decl;
5950   ASM_DECLARE_OBJECT_NAME (stream, name, decl);
5951   ASM_OUTPUT_SKIP (stream, size != 0 ? size : 1);
5952 }
5953 #endif
5954 \f
5955 /* Implement TARGET_ASM_FILE_END.  When using assembler macros, emit
5956    .externs for any small-data variables that turned out to be external.  */
5957
5958 static void
5959 mips_file_end (void)
5960 {
5961   tree name_tree;
5962   struct extern_list *p;
5963
5964   if (extern_head)
5965     {
5966       fputs ("\n", asm_out_file);
5967
5968       for (p = extern_head; p != 0; p = p->next)
5969         {
5970           name_tree = get_identifier (p->name);
5971
5972           /* Positively ensure only one .extern for any given symbol.  */
5973           if (!TREE_ASM_WRITTEN (name_tree)
5974               && TREE_SYMBOL_REFERENCED (name_tree))
5975             {
5976               TREE_ASM_WRITTEN (name_tree) = 1;
5977               /* In IRIX 5 or IRIX 6 for the O32 ABI, we must output a
5978                  `.global name .text' directive for every used but
5979                  undefined function.  If we don't, the linker may perform
5980                  an optimization (skipping over the insns that set $gp)
5981                  when it is unsafe.  */
5982               if (TARGET_IRIX && mips_abi == ABI_32 && p->size == -1)
5983                 {
5984                   fputs ("\t.globl ", asm_out_file);                   
5985                   assemble_name (asm_out_file, p->name);
5986                   fputs (" .text\n", asm_out_file);
5987                 }
5988               else
5989                 {
5990                   fputs ("\t.extern\t", asm_out_file);
5991                   assemble_name (asm_out_file, p->name);
5992                   fprintf (asm_out_file, ", %d\n", p->size);
5993                 }
5994             }
5995         }
5996     }
5997 }
5998
5999 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON.  This is usually the same as
6000    the elfos.h version, but we also need to handle -muninit-const-in-rodata
6001    and the limitations of the SGI o32 assembler.  */
6002
6003 void
6004 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
6005                                  unsigned HOST_WIDE_INT size,
6006                                  unsigned int align)
6007 {
6008   /* If the target wants uninitialized const declarations in
6009      .rdata then don't put them in .comm.   */
6010   if (TARGET_EMBEDDED_DATA && TARGET_UNINIT_CONST_IN_RODATA
6011       && TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl)
6012       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
6013     {
6014       if (TREE_PUBLIC (decl) && DECL_NAME (decl))
6015         targetm.asm_out.globalize_label (stream, name);
6016
6017       readonly_data_section ();
6018       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
6019       mips_declare_object (stream, name, "",
6020                            ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
6021                            size);
6022     }
6023   else if (TARGET_SGI_O32_AS)
6024     {
6025       /* The SGI o32 assembler doesn't accept an alignment, so round up
6026          the size instead.  */
6027       size += (align / BITS_PER_UNIT) - 1;
6028       size -= size % (align / BITS_PER_UNIT);
6029       mips_declare_object (stream, name, "\n\t.comm\t",
6030                            "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
6031     }
6032   else
6033     mips_declare_object (stream, name, "\n\t.comm\t",
6034                          "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
6035                          size, align / BITS_PER_UNIT);
6036 }
6037
6038 /* Emit either a label, .comm, or .lcomm directive.  When using assembler
6039    macros, mark the symbol as written so that mips_file_end won't emit an
6040    .extern for it.  STREAM is the output file, NAME is the name of the
6041    symbol, INIT_STRING is the string that should be written before the
6042    symbol and FINAL_STRING is the string that should be written after it.
6043    FINAL_STRING is a printf() format that consumes the remaining arguments.  */
6044
6045 void
6046 mips_declare_object (FILE *stream, const char *name, const char *init_string,
6047                      const char *final_string, ...)
6048 {
6049   va_list ap;
6050
6051   fputs (init_string, stream);
6052   assemble_name (stream, name);
6053   va_start (ap, final_string);
6054   vfprintf (stream, final_string, ap);
6055   va_end (ap);
6056
6057   if (!TARGET_EXPLICIT_RELOCS)
6058     {
6059       tree name_tree = get_identifier (name);
6060       TREE_ASM_WRITTEN (name_tree) = 1;
6061     }
6062 }
6063
6064 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
6065 extern int size_directive_output;
6066
6067 /* Implement ASM_DECLARE_OBJECT_NAME.  This is like most of the standard ELF
6068    definitions except that it uses mips_declare_object() to emit the label.  */
6069
6070 void
6071 mips_declare_object_name (FILE *stream, const char *name,
6072                           tree decl ATTRIBUTE_UNUSED)
6073 {
6074   if (!TARGET_SGI_O32_AS)
6075     {
6076 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
6077       ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
6078 #endif
6079
6080       size_directive_output = 0;
6081       if (!flag_inhibit_size_directive && DECL_SIZE (decl))
6082         {
6083           HOST_WIDE_INT size;
6084
6085           size_directive_output = 1;
6086           size = int_size_in_bytes (TREE_TYPE (decl));
6087           ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6088         }
6089     }
6090
6091   mips_declare_object (stream, name, "", ":\n", 0);
6092 }
6093
6094 /* Implement ASM_FINISH_DECLARE_OBJECT.  This is generic ELF stuff.  */
6095
6096 void
6097 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
6098 {
6099   const char *name;
6100
6101   name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6102   if (!TARGET_SGI_O32_AS
6103       && !flag_inhibit_size_directive
6104       && DECL_SIZE (decl) != 0
6105       && !at_end && top_level
6106       && DECL_INITIAL (decl) == error_mark_node
6107       && !size_directive_output)
6108     {
6109       HOST_WIDE_INT size;
6110
6111       size_directive_output = 1;
6112       size = int_size_in_bytes (TREE_TYPE (decl));
6113       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6114     }
6115 }
6116 #endif
6117 \f
6118 /* Return true if X is a small data address that can be rewritten
6119    as a LO_SUM.  */
6120
6121 static bool
6122 mips_rewrite_small_data_p (rtx x)
6123 {
6124   enum mips_symbol_type symbol_type;
6125
6126   return (TARGET_EXPLICIT_RELOCS
6127           && mips_symbolic_constant_p (x, &symbol_type)
6128           && symbol_type == SYMBOL_SMALL_DATA);
6129 }
6130
6131
6132 /* A for_each_rtx callback for small_data_pattern.  */
6133
6134 static int
6135 small_data_pattern_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6136 {
6137   if (GET_CODE (*loc) == LO_SUM)
6138     return -1;
6139
6140   return mips_rewrite_small_data_p (*loc);
6141 }
6142
6143 /* Return true if OP refers to small data symbols directly, not through
6144    a LO_SUM.  */
6145
6146 int
6147 small_data_pattern (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
6148 {
6149   return (GET_CODE (op) != SEQUENCE
6150           && for_each_rtx (&op, small_data_pattern_1, 0));
6151 }
6152 \f
6153 /* A for_each_rtx callback, used by mips_rewrite_small_data.  */
6154
6155 static int
6156 mips_rewrite_small_data_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6157 {
6158   if (mips_rewrite_small_data_p (*loc))
6159     *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
6160
6161   if (GET_CODE (*loc) == LO_SUM)
6162     return -1;
6163
6164   return 0;
6165 }
6166
6167 /* If possible, rewrite OP so that it refers to small data using
6168    explicit relocations.  */
6169
6170 rtx
6171 mips_rewrite_small_data (rtx op)
6172 {
6173   op = copy_insn (op);
6174   for_each_rtx (&op, mips_rewrite_small_data_1, 0);
6175   return op;
6176 }
6177 \f
6178 /* Return true if the current function has an insn that implicitly
6179    refers to $gp.  */
6180
6181 static bool
6182 mips_function_has_gp_insn (void)
6183 {
6184   /* Don't bother rechecking if we found one last time.  */
6185   if (!cfun->machine->has_gp_insn_p)
6186     {
6187       rtx insn;
6188
6189       push_topmost_sequence ();
6190       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6191         if (INSN_P (insn)
6192             && GET_CODE (PATTERN (insn)) != USE
6193             && GET_CODE (PATTERN (insn)) != CLOBBER
6194             && (get_attr_got (insn) != GOT_UNSET
6195                 || small_data_pattern (PATTERN (insn), VOIDmode)))
6196           break;
6197       pop_topmost_sequence ();
6198
6199       cfun->machine->has_gp_insn_p = (insn != 0);
6200     }
6201   return cfun->machine->has_gp_insn_p;
6202 }
6203
6204
6205 /* Return the register that should be used as the global pointer
6206    within this function.  Return 0 if the function doesn't need
6207    a global pointer.  */
6208
6209 static unsigned int
6210 mips_global_pointer (void)
6211 {
6212   unsigned int regno;
6213
6214   /* $gp is always available in non-abicalls code.  */
6215   if (!TARGET_ABICALLS)
6216     return GLOBAL_POINTER_REGNUM;
6217
6218   /* We must always provide $gp when it is used implicitly.  */
6219   if (!TARGET_EXPLICIT_RELOCS)
6220     return GLOBAL_POINTER_REGNUM;
6221
6222   /* FUNCTION_PROFILER includes a jal macro, so we need to give it
6223      a valid gp.  */
6224   if (current_function_profile)
6225     return GLOBAL_POINTER_REGNUM;
6226
6227   /* If the function has a nonlocal goto, $gp must hold the correct
6228      global pointer for the target function.  */
6229   if (current_function_has_nonlocal_goto)
6230     return GLOBAL_POINTER_REGNUM;
6231
6232   /* If the gp is never referenced, there's no need to initialize it.
6233      Note that reload can sometimes introduce constant pool references
6234      into a function that otherwise didn't need them.  For example,
6235      suppose we have an instruction like:
6236
6237           (set (reg:DF R1) (float:DF (reg:SI R2)))
6238
6239      If R2 turns out to be constant such as 1, the instruction may have a
6240      REG_EQUAL note saying that R1 == 1.0.  Reload then has the option of
6241      using this constant if R2 doesn't get allocated to a register.
6242
6243      In cases like these, reload will have added the constant to the pool
6244      but no instruction will yet refer to it.  */
6245   if (!regs_ever_live[GLOBAL_POINTER_REGNUM]
6246       && !current_function_uses_const_pool
6247       && !mips_function_has_gp_insn ())
6248     return 0;
6249
6250   /* We need a global pointer, but perhaps we can use a call-clobbered
6251      register instead of $gp.  */
6252   if (TARGET_NEWABI && current_function_is_leaf)
6253     for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6254       if (!regs_ever_live[regno]
6255           && call_used_regs[regno]
6256           && !fixed_regs[regno]
6257           && regno != PIC_FUNCTION_ADDR_REGNUM)
6258         return regno;
6259
6260   return GLOBAL_POINTER_REGNUM;
6261 }
6262
6263
6264 /* Return true if the current function must save REGNO.  */
6265
6266 static bool
6267 mips_save_reg_p (unsigned int regno)
6268 {
6269   /* We only need to save $gp for NewABI PIC.  */
6270   if (regno == GLOBAL_POINTER_REGNUM)
6271     return (TARGET_ABICALLS && TARGET_NEWABI
6272             && cfun->machine->global_pointer == regno);
6273
6274   /* Check call-saved registers.  */
6275   if (regs_ever_live[regno] && !call_used_regs[regno])
6276     return true;
6277
6278   /* We need to save the old frame pointer before setting up a new one.  */
6279   if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
6280     return true;
6281
6282   /* We need to save the incoming return address if it is ever clobbered
6283      within the function.  */
6284   if (regno == GP_REG_FIRST + 31 && regs_ever_live[regno])
6285     return true;
6286
6287   if (TARGET_MIPS16)
6288     {
6289       tree return_type;
6290
6291       return_type = DECL_RESULT (current_function_decl);
6292
6293       /* $18 is a special case in mips16 code.  It may be used to call
6294          a function which returns a floating point value, but it is
6295          marked in call_used_regs.  */
6296       if (regno == GP_REG_FIRST + 18 && regs_ever_live[regno])
6297         return true;
6298
6299       /* $31 is also a special case.  It will be used to copy a return
6300          value into the floating point registers if the return value is
6301          floating point.  */
6302       if (regno == GP_REG_FIRST + 31
6303           && mips16_hard_float
6304           && !aggregate_value_p (return_type, current_function_decl)
6305           && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
6306           && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
6307         return true;
6308     }
6309
6310   return false;
6311 }
6312
6313
6314 /* Return the bytes needed to compute the frame pointer from the current
6315    stack pointer.  SIZE is the size (in bytes) of the local variables.
6316
6317    Mips stack frames look like:
6318
6319              Before call                        After call
6320         +-----------------------+       +-----------------------+
6321    high |                       |       |                       |
6322    mem. |                       |       |                       |
6323         |  caller's temps.      |       |  caller's temps.      |
6324         |                       |       |                       |
6325         +-----------------------+       +-----------------------+
6326         |                       |       |                       |
6327         |  arguments on stack.  |       |  arguments on stack.  |
6328         |                       |       |                       |
6329         +-----------------------+       +-----------------------+
6330         |  4 words to save      |       |  4 words to save      |
6331         |  arguments passed     |       |  arguments passed     |
6332         |  in registers, even   |       |  in registers, even   |
6333     SP->|  if not passed.       |  VFP->|  if not passed.       |
6334         +-----------------------+       +-----------------------+
6335                                         |                       |
6336                                         |  fp register save     |
6337                                         |                       |
6338                                         +-----------------------+
6339                                         |                       |
6340                                         |  gp register save     |
6341                                         |                       |
6342                                         +-----------------------+
6343                                         |                       |
6344                                         |  local variables      |
6345                                         |                       |
6346                                         +-----------------------+
6347                                         |                       |
6348                                         |  alloca allocations   |
6349                                         |                       |
6350                                         +-----------------------+
6351                                         |                       |
6352                                         |  GP save for V.4 abi  |
6353                                         |                       |
6354                                         +-----------------------+
6355                                         |                       |
6356                                         |  arguments on stack   |
6357                                         |                       |
6358                                         +-----------------------+
6359                                         |  4 words to save      |
6360                                         |  arguments passed     |
6361                                         |  in registers, even   |
6362    low                              SP->|  if not passed.       |
6363    memory                               +-----------------------+
6364
6365 */
6366
6367 HOST_WIDE_INT
6368 compute_frame_size (HOST_WIDE_INT size)
6369 {
6370   unsigned int regno;
6371   HOST_WIDE_INT total_size;     /* # bytes that the entire frame takes up */
6372   HOST_WIDE_INT var_size;       /* # bytes that variables take up */
6373   HOST_WIDE_INT args_size;      /* # bytes that outgoing arguments take up */
6374   HOST_WIDE_INT cprestore_size; /* # bytes that the cprestore slot takes up */
6375   HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6376   HOST_WIDE_INT gp_reg_size;    /* # bytes needed to store gp regs */
6377   HOST_WIDE_INT fp_reg_size;    /* # bytes needed to store fp regs */
6378   unsigned int mask;            /* mask of saved gp registers */
6379   unsigned int fmask;           /* mask of saved fp registers */
6380
6381   cfun->machine->global_pointer = mips_global_pointer ();
6382
6383   gp_reg_size = 0;
6384   fp_reg_size = 0;
6385   mask = 0;
6386   fmask = 0;
6387   var_size = MIPS_STACK_ALIGN (size);
6388   args_size = current_function_outgoing_args_size;
6389   cprestore_size = MIPS_STACK_ALIGN (STARTING_FRAME_OFFSET) - args_size;
6390
6391   /* The space set aside by STARTING_FRAME_OFFSET isn't needed in leaf
6392      functions.  If the function has local variables, we're committed
6393      to allocating it anyway.  Otherwise reclaim it here.  */
6394   if (var_size == 0 && current_function_is_leaf)
6395     cprestore_size = args_size = 0;
6396
6397   /* The MIPS 3.0 linker does not like functions that dynamically
6398      allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6399      looks like we are trying to create a second frame pointer to the
6400      function, so allocate some stack space to make it happy.  */
6401
6402   if (args_size == 0 && current_function_calls_alloca)
6403     args_size = 4 * UNITS_PER_WORD;
6404
6405   total_size = var_size + args_size + cprestore_size;
6406
6407   /* Calculate space needed for gp registers.  */
6408   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6409     if (mips_save_reg_p (regno))
6410       {
6411         gp_reg_size += GET_MODE_SIZE (gpr_mode);
6412         mask |= 1 << (regno - GP_REG_FIRST);
6413       }
6414
6415   /* We need to restore these for the handler.  */
6416   if (current_function_calls_eh_return)
6417     {
6418       unsigned int i;
6419       for (i = 0; ; ++i)
6420         {
6421           regno = EH_RETURN_DATA_REGNO (i);
6422           if (regno == INVALID_REGNUM)
6423             break;
6424           gp_reg_size += GET_MODE_SIZE (gpr_mode);
6425           mask |= 1 << (regno - GP_REG_FIRST);
6426         }
6427     }
6428
6429   /* This loop must iterate over the same space as its companion in
6430      save_restore_insns.  */
6431   for (regno = (FP_REG_LAST - FP_INC + 1);
6432        regno >= FP_REG_FIRST;
6433        regno -= FP_INC)
6434     {
6435       if (mips_save_reg_p (regno))
6436         {
6437           fp_reg_size += FP_INC * UNITS_PER_FPREG;
6438           fmask |= ((1 << FP_INC) - 1) << (regno - FP_REG_FIRST);
6439         }
6440     }
6441
6442   gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6443   total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
6444
6445   /* Add in space reserved on the stack by the callee for storing arguments
6446      passed in registers.  */
6447   if (!TARGET_OLDABI)
6448     total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
6449
6450   /* Save other computed information.  */
6451   cfun->machine->frame.total_size = total_size;
6452   cfun->machine->frame.var_size = var_size;
6453   cfun->machine->frame.args_size = args_size;
6454   cfun->machine->frame.cprestore_size = cprestore_size;
6455   cfun->machine->frame.gp_reg_size = gp_reg_size;
6456   cfun->machine->frame.fp_reg_size = fp_reg_size;
6457   cfun->machine->frame.mask = mask;
6458   cfun->machine->frame.fmask = fmask;
6459   cfun->machine->frame.initialized = reload_completed;
6460   cfun->machine->frame.num_gp = gp_reg_size / UNITS_PER_WORD;
6461   cfun->machine->frame.num_fp = fp_reg_size / (FP_INC * UNITS_PER_FPREG);
6462
6463   if (mask)
6464     {
6465       HOST_WIDE_INT offset;
6466
6467       offset = (args_size + cprestore_size + var_size
6468                 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
6469       cfun->machine->frame.gp_sp_offset = offset;
6470       cfun->machine->frame.gp_save_offset = offset - total_size;
6471     }
6472   else
6473     {
6474       cfun->machine->frame.gp_sp_offset = 0;
6475       cfun->machine->frame.gp_save_offset = 0;
6476     }
6477
6478   if (fmask)
6479     {
6480       HOST_WIDE_INT offset;
6481
6482       offset = (args_size + cprestore_size + var_size
6483                 + gp_reg_rounded + fp_reg_size
6484                 - FP_INC * UNITS_PER_FPREG);
6485       cfun->machine->frame.fp_sp_offset = offset;
6486       cfun->machine->frame.fp_save_offset = offset - total_size;
6487     }
6488   else
6489     {
6490       cfun->machine->frame.fp_sp_offset = 0;
6491       cfun->machine->frame.fp_save_offset = 0;
6492     }
6493
6494   /* Ok, we're done.  */
6495   return total_size;
6496 }
6497 \f
6498 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame
6499    pointer or argument pointer.  TO is either the stack pointer or
6500    hard frame pointer.  */
6501
6502 HOST_WIDE_INT
6503 mips_initial_elimination_offset (int from, int to)
6504 {
6505   HOST_WIDE_INT offset;
6506
6507   compute_frame_size (get_frame_size ());
6508
6509   /* Set OFFSET to the offset from the stack pointer.  */
6510   switch (from)
6511     {
6512     case FRAME_POINTER_REGNUM:
6513       offset = 0;
6514       break;
6515
6516     case ARG_POINTER_REGNUM:
6517       offset = cfun->machine->frame.total_size;
6518       if (TARGET_NEWABI)
6519         offset -= current_function_pretend_args_size;
6520       break;
6521
6522     default:
6523       abort ();
6524     }
6525
6526   if (TARGET_MIPS16 && to == HARD_FRAME_POINTER_REGNUM)
6527     offset -= cfun->machine->frame.args_size;
6528
6529   return offset;
6530 }
6531 \f
6532 /* Implement RETURN_ADDR_RTX.  Note, we do not support moving
6533    back to a previous frame.  */
6534 rtx
6535 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
6536 {
6537   if (count != 0)
6538     return const0_rtx;
6539
6540   return get_hard_reg_initial_val (Pmode, GP_REG_FIRST + 31);
6541 }
6542 \f
6543 /* Use FN to save or restore register REGNO.  MODE is the register's
6544    mode and OFFSET is the offset of its save slot from the current
6545    stack pointer.  */
6546
6547 static void
6548 mips_save_restore_reg (enum machine_mode mode, int regno,
6549                        HOST_WIDE_INT offset, mips_save_restore_fn fn)
6550 {
6551   rtx mem;
6552
6553   mem = gen_rtx_MEM (mode, plus_constant (stack_pointer_rtx, offset));
6554   if (!current_function_calls_eh_return)
6555     RTX_UNCHANGING_P (mem) = 1;
6556
6557   fn (gen_rtx_REG (mode, regno), mem);
6558 }
6559
6560
6561 /* Call FN for each register that is saved by the current function.
6562    SP_OFFSET is the offset of the current stack pointer from the start
6563    of the frame.  */
6564
6565 static void
6566 mips_for_each_saved_reg (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
6567 {
6568 #define BITSET_P(VALUE, BIT) (((VALUE) & (1L << (BIT))) != 0)
6569
6570   enum machine_mode fpr_mode;
6571   HOST_WIDE_INT offset;
6572   int regno;
6573
6574   /* Save registers starting from high to low.  The debuggers prefer at least
6575      the return register be stored at func+4, and also it allows us not to
6576      need a nop in the epilog if at least one register is reloaded in
6577      addition to return address.  */
6578   offset = cfun->machine->frame.gp_sp_offset - sp_offset;
6579   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
6580     if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
6581       {
6582         mips_save_restore_reg (gpr_mode, regno, offset, fn);
6583         offset -= GET_MODE_SIZE (gpr_mode);
6584       }
6585
6586   /* This loop must iterate over the same space as its companion in
6587      compute_frame_size.  */
6588   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
6589   fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
6590   for (regno = (FP_REG_LAST - FP_INC + 1);
6591        regno >= FP_REG_FIRST;
6592        regno -= FP_INC)
6593     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
6594       {
6595         mips_save_restore_reg (fpr_mode, regno, offset, fn);
6596         offset -= GET_MODE_SIZE (fpr_mode);
6597       }
6598 #undef BITSET_P
6599 }
6600 \f
6601 /* If we're generating n32 or n64 abicalls, and the current function
6602    does not use $28 as its global pointer, emit a cplocal directive.
6603    Use pic_offset_table_rtx as the argument to the directive.  */
6604
6605 static void
6606 mips_output_cplocal (void)
6607 {
6608   if (!TARGET_EXPLICIT_RELOCS
6609       && cfun->machine->global_pointer > 0
6610       && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
6611     output_asm_insn (".cplocal %+", 0);
6612 }
6613
6614 /* If we're generating n32 or n64 abicalls, emit instructions
6615    to set up the global pointer.  */
6616
6617 static void
6618 mips_emit_loadgp (void)
6619 {
6620   if (TARGET_ABICALLS && TARGET_NEWABI && cfun->machine->global_pointer > 0)
6621     {
6622       rtx addr, offset, incoming_address;
6623
6624       addr = XEXP (DECL_RTL (current_function_decl), 0);
6625       offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
6626       incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
6627       emit_insn (gen_loadgp (offset, incoming_address));
6628       if (!TARGET_EXPLICIT_RELOCS)
6629         emit_insn (gen_loadgp_blockage ());
6630     }
6631 }
6632
6633 /* Set up the stack and frame (if desired) for the function.  */
6634
6635 static void
6636 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6637 {
6638   const char *fnname;
6639   HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
6640
6641   /* ??? When is this really needed?  At least the GNU assembler does not
6642      need the source filename more than once in the file, beyond what is
6643      emitted by the debug information.  */
6644   if (!TARGET_GAS)
6645     ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
6646
6647 #ifdef SDB_DEBUGGING_INFO
6648   if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6649     ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl), 0);
6650 #endif
6651
6652   /* In mips16 mode, we may need to generate a 32 bit to handle
6653      floating point arguments.  The linker will arrange for any 32 bit
6654      functions to call this stub, which will then jump to the 16 bit
6655      function proper.  */
6656   if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6657       && current_function_args_info.fp_code != 0)
6658     build_mips16_function_stub (file);
6659
6660   if (!FUNCTION_NAME_ALREADY_DECLARED)
6661     {
6662       /* Get the function name the same way that toplev.c does before calling
6663          assemble_start_function.  This is needed so that the name used here
6664          exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
6665       fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6666
6667       if (!flag_inhibit_size_directive)
6668         {
6669           fputs ("\t.ent\t", file);
6670           assemble_name (file, fnname);
6671           fputs ("\n", file);
6672         }
6673
6674       assemble_name (file, fnname);
6675       fputs (":\n", file);
6676     }
6677
6678   if (!flag_inhibit_size_directive)
6679     {
6680       /* .frame FRAMEREG, FRAMESIZE, RETREG */
6681       fprintf (file,
6682                "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
6683                "# vars= " HOST_WIDE_INT_PRINT_DEC ", regs= %d/%d"
6684                ", args= " HOST_WIDE_INT_PRINT_DEC
6685                ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
6686                (reg_names[(frame_pointer_needed)
6687                           ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
6688                ((frame_pointer_needed && TARGET_MIPS16)
6689                 ? tsize - cfun->machine->frame.args_size
6690                 : tsize),
6691                reg_names[GP_REG_FIRST + 31],
6692                cfun->machine->frame.var_size,
6693                cfun->machine->frame.num_gp,
6694                cfun->machine->frame.num_fp,
6695                cfun->machine->frame.args_size,
6696                cfun->machine->frame.cprestore_size);
6697
6698       /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
6699       fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6700                cfun->machine->frame.mask,
6701                cfun->machine->frame.gp_save_offset);
6702       fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6703                cfun->machine->frame.fmask,
6704                cfun->machine->frame.fp_save_offset);
6705
6706       /* Require:
6707          OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
6708          HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs.  */
6709     }
6710
6711   if (TARGET_ABICALLS && !TARGET_NEWABI && cfun->machine->global_pointer > 0)
6712     {
6713       /* Handle the initialization of $gp for SVR4 PIC.  */
6714       if (!cfun->machine->all_noreorder_p)
6715         output_asm_insn ("%(.cpload\t%^%)", 0);
6716       else
6717         output_asm_insn ("%(.cpload\t%^\n\t%<", 0);
6718     }
6719   else if (cfun->machine->all_noreorder_p)
6720     output_asm_insn ("%(%<", 0);
6721
6722   /* Tell the assembler which register we're using as the global
6723      pointer.  This is needed for thunks, since they can use either
6724      explicit relocs or assembler macros.  */
6725   mips_output_cplocal ();
6726 }
6727 \f
6728 /* Make the last instruction frame related and note that it performs
6729    the operation described by FRAME_PATTERN.  */
6730
6731 static void
6732 mips_set_frame_expr (rtx frame_pattern)
6733 {
6734   rtx insn;
6735
6736   insn = get_last_insn ();
6737   RTX_FRAME_RELATED_P (insn) = 1;
6738   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6739                                       frame_pattern,
6740                                       REG_NOTES (insn));
6741 }
6742
6743
6744 /* Return a frame-related rtx that stores REG at MEM.
6745    REG must be a single register.  */
6746
6747 static rtx
6748 mips_frame_set (rtx mem, rtx reg)
6749 {
6750   rtx set = gen_rtx_SET (VOIDmode, mem, reg);
6751   RTX_FRAME_RELATED_P (set) = 1;
6752   return set;
6753 }
6754
6755
6756 /* Save register REG to MEM.  Make the instruction frame-related.  */
6757
6758 static void
6759 mips_save_reg (rtx reg, rtx mem)
6760 {
6761   if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
6762     {
6763       rtx x1, x2;
6764
6765       if (mips_split_64bit_move_p (mem, reg))
6766         mips_split_64bit_move (mem, reg);
6767       else
6768         emit_move_insn (mem, reg);
6769
6770       x1 = mips_frame_set (mips_subword (mem, 0), mips_subword (reg, 0));
6771       x2 = mips_frame_set (mips_subword (mem, 1), mips_subword (reg, 1));
6772       mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
6773     }
6774   else
6775     {
6776       if (TARGET_MIPS16
6777           && REGNO (reg) != GP_REG_FIRST + 31
6778           && !M16_REG_P (REGNO (reg)))
6779         {
6780           /* Save a non-mips16 register by moving it through a temporary.
6781              We don't need to do this for $31 since there's a special
6782              instruction for it.  */
6783           emit_move_insn (MIPS_PROLOGUE_TEMP (GET_MODE (reg)), reg);
6784           emit_move_insn (mem, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
6785         }
6786       else
6787         emit_move_insn (mem, reg);
6788
6789       mips_set_frame_expr (mips_frame_set (mem, reg));
6790     }
6791 }
6792
6793
6794 /* Expand the prologue into a bunch of separate insns.  */
6795
6796 void
6797 mips_expand_prologue (void)
6798 {
6799   HOST_WIDE_INT size;
6800
6801   if (cfun->machine->global_pointer > 0)
6802     REGNO (pic_offset_table_rtx) = cfun->machine->global_pointer;
6803
6804   size = compute_frame_size (get_frame_size ());
6805
6806   /* Save the registers.  Allocate up to MIPS_MAX_FIRST_STACK_STEP
6807      bytes beforehand; this is enough to cover the register save area
6808      without going out of range.  */
6809   if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
6810     {
6811       HOST_WIDE_INT step1;
6812
6813       step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
6814       RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6815                                                      stack_pointer_rtx,
6816                                                      GEN_INT (-step1)))) = 1;
6817       size -= step1;
6818       mips_for_each_saved_reg (size, mips_save_reg);
6819     }
6820
6821   /* Allocate the rest of the frame.  */
6822   if (size > 0)
6823     {
6824       if (SMALL_OPERAND (-size))
6825         RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6826                                                        stack_pointer_rtx,
6827                                                        GEN_INT (-size)))) = 1;
6828       else
6829         {
6830           emit_move_insn (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
6831           if (TARGET_MIPS16)
6832             {
6833               /* There are no instructions to add or subtract registers
6834                  from the stack pointer, so use the frame pointer as a
6835                  temporary.  We should always be using a frame pointer
6836                  in this case anyway.  */
6837               if (!frame_pointer_needed)
6838                 abort ();
6839
6840               emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6841               emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
6842                                         hard_frame_pointer_rtx,
6843                                         MIPS_PROLOGUE_TEMP (Pmode)));
6844               emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
6845             }
6846           else
6847             emit_insn (gen_sub3_insn (stack_pointer_rtx,
6848                                       stack_pointer_rtx,
6849                                       MIPS_PROLOGUE_TEMP (Pmode)));
6850
6851           /* Describe the combined effect of the previous instructions.  */
6852           mips_set_frame_expr
6853             (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6854                           plus_constant (stack_pointer_rtx, -size)));
6855         }
6856     }
6857
6858   /* Set up the frame pointer, if we're using one.  In mips16 code,
6859      we point the frame pointer ahead of the outgoing argument area.
6860      This should allow more variables & incoming arguments to be
6861      accessed with unextended instructions.  */
6862   if (frame_pointer_needed)
6863     {
6864       if (TARGET_MIPS16 && cfun->machine->frame.args_size != 0)
6865         {
6866           rtx offset = GEN_INT (cfun->machine->frame.args_size);
6867           RTX_FRAME_RELATED_P
6868             (emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
6869                                        stack_pointer_rtx,
6870                                        offset))) = 1;
6871         }
6872       else
6873         RTX_FRAME_RELATED_P (emit_move_insn (hard_frame_pointer_rtx,
6874                                              stack_pointer_rtx)) = 1;
6875     }
6876
6877   /* If generating o32/o64 abicalls, save $gp on the stack.  */
6878   if (TARGET_ABICALLS && !TARGET_NEWABI && !current_function_is_leaf)
6879     emit_insn (gen_cprestore (GEN_INT (current_function_outgoing_args_size)));
6880
6881   mips_emit_loadgp ();
6882
6883   /* If we are profiling, make sure no instructions are scheduled before
6884      the call to mcount.  */
6885
6886   if (current_function_profile)
6887     emit_insn (gen_blockage ());
6888 }
6889 \f
6890 /* Do any necessary cleanup after a function to restore stack, frame,
6891    and regs.  */
6892
6893 #define RA_MASK BITMASK_HIGH    /* 1 << 31 */
6894
6895 static void
6896 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6897                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6898 {
6899   rtx string;
6900
6901   /* Reinstate the normal $gp.  */
6902   REGNO (pic_offset_table_rtx) = GLOBAL_POINTER_REGNUM;
6903   mips_output_cplocal ();
6904
6905   if (cfun->machine->all_noreorder_p)
6906     {
6907       /* Avoid using %>%) since it adds excess whitespace.  */
6908       output_asm_insn (".set\tmacro", 0);
6909       output_asm_insn (".set\treorder", 0);
6910       set_noreorder = set_nomacro = 0;
6911     }
6912
6913   if (!FUNCTION_NAME_ALREADY_DECLARED && !flag_inhibit_size_directive)
6914     {
6915       const char *fnname;
6916
6917       /* Get the function name the same way that toplev.c does before calling
6918          assemble_start_function.  This is needed so that the name used here
6919          exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
6920       fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6921       fputs ("\t.end\t", file);
6922       assemble_name (file, fnname);
6923       fputs ("\n", file);
6924     }
6925
6926   while (string_constants != NULL)
6927     {
6928       struct string_constant *next;
6929
6930       next = string_constants->next;
6931       free (string_constants);
6932       string_constants = next;
6933     }
6934
6935   /* If any following function uses the same strings as this one, force
6936      them to refer those strings indirectly.  Nearby functions could
6937      refer them using pc-relative addressing, but it isn't safe in
6938      general.  For instance, some functions may be placed in sections
6939      other than .text, and we don't know whether they be close enough
6940      to this one.  In large files, even other .text functions can be
6941      too far away.  */
6942   for (string = mips16_strings; string != 0; string = XEXP (string, 1))
6943     SYMBOL_REF_FLAG (XEXP (string, 0)) = 0;
6944   free_EXPR_LIST_list (&mips16_strings);
6945 }
6946 \f
6947 /* Emit instructions to restore register REG from slot MEM.  */
6948
6949 static void
6950 mips_restore_reg (rtx reg, rtx mem)
6951 {
6952   /* There's no mips16 instruction to load $31 directly.  Load into
6953      $7 instead and adjust the return insn appropriately.  */
6954   if (TARGET_MIPS16 && REGNO (reg) == GP_REG_FIRST + 31)
6955     reg = gen_rtx_REG (GET_MODE (reg), 7);
6956
6957   if (TARGET_MIPS16 && !M16_REG_P (REGNO (reg)))
6958     {
6959       /* Can't restore directly; move through a temporary.  */
6960       emit_move_insn (MIPS_EPILOGUE_TEMP (GET_MODE (reg)), mem);
6961       emit_move_insn (reg, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
6962     }
6963   else
6964     emit_move_insn (reg, mem);
6965 }
6966
6967
6968 /* Expand the epilogue into a bunch of separate insns.  SIBCALL_P is true
6969    if this epilogue precedes a sibling call, false if it is for a normal
6970    "epilogue" pattern.  */
6971
6972 void
6973 mips_expand_epilogue (int sibcall_p)
6974 {
6975   HOST_WIDE_INT step1, step2;
6976   rtx base, target;
6977
6978   if (!sibcall_p && mips_can_use_return_insn ())
6979     {
6980       emit_jump_insn (gen_return ());
6981       return;
6982     }
6983
6984   /* Split the frame into two.  STEP1 is the amount of stack we should
6985      deallocate before restoring the registers.  STEP2 is the amount we
6986      should deallocate afterwards.
6987
6988      Start off by assuming that no registers need to be restored.  */
6989   step1 = cfun->machine->frame.total_size;
6990   step2 = 0;
6991
6992   /* Work out which register holds the frame address.  Account for the
6993      frame pointer offset used by mips16 code.  */
6994   if (!frame_pointer_needed)
6995     base = stack_pointer_rtx;
6996   else
6997     {
6998       base = hard_frame_pointer_rtx;
6999       if (TARGET_MIPS16)
7000         step1 -= cfun->machine->frame.args_size;
7001     }
7002
7003   /* If we need to restore registers, deallocate as much stack as
7004      possible in the second step without going out of range.  */
7005   if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
7006     {
7007       step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
7008       step1 -= step2;
7009     }
7010
7011   /* Set TARGET to BASE + STEP1.  */
7012   target = base;
7013   if (step1 > 0)
7014     {
7015       rtx adjust;
7016
7017       /* Get an rtx for STEP1 that we can add to BASE.  */
7018       adjust = GEN_INT (step1);
7019       if (!SMALL_OPERAND (step1))
7020         {
7021           emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), adjust);
7022           adjust = MIPS_EPILOGUE_TEMP (Pmode);
7023         }
7024
7025       /* Normal mode code can copy the result straight into $sp.  */
7026       if (!TARGET_MIPS16)
7027         target = stack_pointer_rtx;
7028
7029       emit_insn (gen_add3_insn (target, base, adjust));
7030     }
7031
7032   /* Copy TARGET into the stack pointer.  */
7033   if (target != stack_pointer_rtx)
7034     emit_move_insn (stack_pointer_rtx, target);
7035
7036   /* If we're using addressing macros for n32/n64 abicalls, $gp is
7037      implicitly used by all SYMBOL_REFs.  We must emit a blockage
7038      insn before restoring it.  */
7039   if (TARGET_ABICALLS && TARGET_NEWABI && !TARGET_EXPLICIT_RELOCS)
7040     emit_insn (gen_blockage ());
7041
7042   /* Restore the registers.  */
7043   mips_for_each_saved_reg (cfun->machine->frame.total_size - step2,
7044                            mips_restore_reg);
7045
7046   /* Deallocate the final bit of the frame.  */
7047   if (step2 > 0)
7048     emit_insn (gen_add3_insn (stack_pointer_rtx,
7049                               stack_pointer_rtx,
7050                               GEN_INT (step2)));
7051     
7052   /* Add in the __builtin_eh_return stack adjustment.   We need to
7053      use a temporary in mips16 code.  */
7054   if (current_function_calls_eh_return)
7055     {
7056       if (TARGET_MIPS16)
7057         {
7058           emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
7059           emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
7060                                     MIPS_EPILOGUE_TEMP (Pmode),
7061                                     EH_RETURN_STACKADJ_RTX));
7062           emit_move_insn (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
7063         }
7064       else
7065         emit_insn (gen_add3_insn (stack_pointer_rtx,
7066                                   stack_pointer_rtx,
7067                                   EH_RETURN_STACKADJ_RTX));
7068     }
7069
7070   if (!sibcall_p)
7071     {
7072       /* The mips16 loads the return address into $7, not $31.  */
7073       if (TARGET_MIPS16 && (cfun->machine->frame.mask & RA_MASK) != 0)
7074         emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
7075                                                           GP_REG_FIRST + 7)));
7076       else
7077         emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
7078                                                           GP_REG_FIRST + 31)));
7079     }
7080 }
7081 \f
7082 /* Return nonzero if this function is known to have a null epilogue.
7083    This allows the optimizer to omit jumps to jumps if no stack
7084    was created.  */
7085
7086 int
7087 mips_can_use_return_insn (void)
7088 {
7089   tree return_type;
7090
7091   if (! reload_completed)
7092     return 0;
7093
7094   if (regs_ever_live[31] || current_function_profile)
7095     return 0;
7096
7097   return_type = DECL_RESULT (current_function_decl);
7098
7099   /* In mips16 mode, a function which returns a floating point value
7100      needs to arrange to copy the return value into the floating point
7101      registers.  */
7102   if (TARGET_MIPS16
7103       && mips16_hard_float
7104       && ! aggregate_value_p (return_type, current_function_decl)
7105       && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
7106       && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
7107     return 0;
7108
7109   if (cfun->machine->frame.initialized)
7110     return cfun->machine->frame.total_size == 0;
7111
7112   return compute_frame_size (get_frame_size ()) == 0;
7113 }
7114 \f
7115 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
7116    in order to avoid duplicating too much logic from elsewhere.  */
7117
7118 static void
7119 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
7120                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7121                       tree function)
7122 {
7123   rtx this, temp1, temp2, insn, fnaddr;
7124
7125   /* Pretend to be a post-reload pass while generating rtl.  */
7126   no_new_pseudos = 1;
7127   reload_completed = 1;
7128
7129   /* Pick a global pointer for -mabicalls.  Use $15 rather than $28
7130      for TARGET_NEWABI since the latter is a call-saved register.  */
7131   if (TARGET_ABICALLS)
7132     cfun->machine->global_pointer
7133       = REGNO (pic_offset_table_rtx)
7134       = TARGET_NEWABI ? 15 : GLOBAL_POINTER_REGNUM;
7135
7136   /* Set up the global pointer for n32 or n64 abicalls.  */
7137   mips_emit_loadgp ();
7138
7139   /* We need two temporary registers in some cases.  */
7140   temp1 = gen_rtx_REG (Pmode, 2);
7141   temp2 = gen_rtx_REG (Pmode, 3);
7142
7143   /* Find out which register contains the "this" pointer.  */
7144   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
7145     this = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
7146   else
7147     this = gen_rtx_REG (Pmode, GP_ARG_FIRST);
7148
7149   /* Add DELTA to THIS.  */
7150   if (delta != 0)
7151     {
7152       rtx offset = GEN_INT (delta);
7153       if (!SMALL_OPERAND (delta))
7154         {
7155           emit_move_insn (temp1, offset);
7156           offset = temp1;
7157         }
7158       emit_insn (gen_add3_insn (this, this, offset));
7159     }
7160
7161   /* If needed, add *(*THIS + VCALL_OFFSET) to THIS.  */
7162   if (vcall_offset != 0)
7163     {
7164       rtx addr;
7165
7166       /* Set TEMP1 to *THIS.  */
7167       emit_move_insn (temp1, gen_rtx_MEM (Pmode, this));
7168
7169       /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET.  */
7170       if (SMALL_OPERAND (vcall_offset))
7171         addr = gen_rtx_PLUS (Pmode, temp1, GEN_INT (vcall_offset));
7172       else if (TARGET_MIPS16)
7173         {
7174           /* Load the full offset into a register so that we can use
7175              an unextended instruction for the load itself.  */
7176           emit_move_insn (temp2, GEN_INT (vcall_offset));
7177           emit_insn (gen_add3_insn (temp1, temp1, temp2));
7178           addr = temp1;
7179         }
7180       else
7181         {
7182           /* Load the high part of the offset into a register and
7183              leave the low part for the address.  */
7184           emit_move_insn (temp2, GEN_INT (CONST_HIGH_PART (vcall_offset)));
7185           emit_insn (gen_add3_insn (temp1, temp1, temp2));
7186           addr = gen_rtx_PLUS (Pmode, temp1,
7187                                GEN_INT (CONST_LOW_PART (vcall_offset)));
7188         }
7189
7190       /* Load the offset and add it to THIS.  */
7191       emit_move_insn (temp1, gen_rtx_MEM (Pmode, addr));
7192       emit_insn (gen_add3_insn (this, this, temp1));
7193     }
7194
7195   /* Jump to the target function.  Use a sibcall if direct jumps are
7196      allowed, otherwise load the address into a register first.  */
7197   fnaddr = XEXP (DECL_RTL (function), 0);
7198   if (TARGET_MIPS16 || TARGET_ABICALLS || TARGET_LONG_CALLS)
7199     {
7200       /* This is messy.  gas treats "la $25,foo" as part of a call
7201          sequence and may allow a global "foo" to be lazily bound.
7202          The general move patterns therefore reject this combination.
7203
7204          In this context, lazy binding would actually be OK for o32 and o64,
7205          but it's still wrong for n32 and n64; see mips_load_call_address.
7206          We must therefore load the address via a temporary register if
7207          mips_dangerous_for_la25_p.
7208
7209          If we jump to the temporary register rather than $25, the assembler
7210          can use the move insn to fill the jump's delay slot.  */
7211       if (TARGET_ABICALLS && !mips_dangerous_for_la25_p (fnaddr))
7212         temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
7213       mips_load_call_address (temp1, fnaddr, true);
7214
7215       if (TARGET_ABICALLS && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
7216         emit_move_insn (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
7217       emit_jump_insn (gen_indirect_jump (temp1));
7218     }
7219   else
7220     {
7221       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
7222       SIBLING_CALL_P (insn) = 1;
7223     }
7224
7225   /* Run just enough of rest_of_compilation.  This sequence was
7226      "borrowed" from alpha.c.  */
7227   insn = get_insns ();
7228   insn_locators_initialize ();
7229   split_all_insns_noflow ();
7230   shorten_branches (insn);
7231   final_start_function (insn, file, 1);
7232   final (insn, file, 1, 0);
7233   final_end_function ();
7234
7235   /* Clean up the vars set above.  Note that final_end_function resets
7236      the global pointer for us.  */
7237   reload_completed = 0;
7238   no_new_pseudos = 0;
7239 }
7240 \f
7241 /* Returns nonzero if X contains a SYMBOL_REF.  */
7242
7243 static int
7244 symbolic_expression_p (rtx x)
7245 {
7246   if (GET_CODE (x) == SYMBOL_REF)
7247     return 1;
7248
7249   if (GET_CODE (x) == CONST)
7250     return symbolic_expression_p (XEXP (x, 0));
7251
7252   if (GET_RTX_CLASS (GET_CODE (x)) == '1')
7253     return symbolic_expression_p (XEXP (x, 0));
7254
7255   if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
7256       || GET_RTX_CLASS (GET_CODE (x)) == '2')
7257     return (symbolic_expression_p (XEXP (x, 0))
7258             || symbolic_expression_p (XEXP (x, 1)));
7259
7260   return 0;
7261 }
7262
7263 /* Choose the section to use for the constant rtx expression X that has
7264    mode MODE.  */
7265
7266 static void
7267 mips_select_rtx_section (enum machine_mode mode, rtx x,
7268                          unsigned HOST_WIDE_INT align)
7269 {
7270   if (TARGET_MIPS16)
7271     {
7272       /* In mips16 mode, the constant table always goes in the same section
7273          as the function, so that constants can be loaded using PC relative
7274          addressing.  */
7275       function_section (current_function_decl);
7276     }
7277   else if (TARGET_EMBEDDED_DATA)
7278     {
7279       /* For embedded applications, always put constants in read-only data,
7280          in order to reduce RAM usage.  */
7281       mergeable_constant_section (mode, align, 0);
7282     }
7283   else
7284     {
7285       /* For hosted applications, always put constants in small data if
7286          possible, as this gives the best performance.  */
7287       /* ??? Consider using mergeable small data sections.  */
7288
7289       if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
7290           && mips_section_threshold > 0)
7291         named_section (0, ".sdata", 0);
7292       else if (flag_pic && symbolic_expression_p (x))
7293         {
7294           if (targetm.have_named_sections)
7295             named_section (0, ".data.rel.ro", 3);
7296           else
7297             data_section ();
7298         }
7299       else
7300         mergeable_constant_section (mode, align, 0);
7301     }
7302 }
7303
7304 /* Choose the section to use for DECL.  RELOC is true if its value contains
7305    any relocatable expression.  */
7306
7307 static void
7308 mips_select_section (tree decl, int reloc,
7309                      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7310 {
7311   if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
7312       && TREE_CODE (decl) == STRING_CST)
7313     /* For embedded position independent code, put constant strings in the
7314        text section, because the data section is limited to 64K in size.
7315        For mips16 code, put strings in the text section so that a PC
7316        relative load instruction can be used to get their address.  */
7317     text_section ();
7318   else if (targetm.have_named_sections)
7319     default_elf_select_section (decl, reloc, align);
7320   else
7321     /* The native irix o32 assembler doesn't support named sections.  */
7322     default_select_section (decl, reloc, align);
7323 }
7324
7325
7326 /* Implement TARGET_IN_SMALL_DATA_P.  Return true if it would be safe to
7327    access DECL using %gp_rel(...)($gp).  */
7328
7329 static bool
7330 mips_in_small_data_p (tree decl)
7331 {
7332   HOST_WIDE_INT size;
7333
7334   if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
7335     return false;
7336
7337   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
7338     {
7339       const char *name;
7340
7341       /* Reject anything that isn't in a known small-data section.  */
7342       name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7343       if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
7344         return false;
7345
7346       /* If a symbol is defined externally, the assembler will use the
7347          usual -G rules when deciding how to implement macros.  */
7348       if (TARGET_EXPLICIT_RELOCS || !DECL_EXTERNAL (decl))
7349         return true;
7350     }
7351   else if (TARGET_EMBEDDED_DATA)
7352     {
7353       /* Don't put constants into the small data section: we want them
7354          to be in ROM rather than RAM.  */
7355       if (TREE_CODE (decl) != VAR_DECL)
7356         return false;
7357
7358       if (TREE_READONLY (decl)
7359           && !TREE_SIDE_EFFECTS (decl)
7360           && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
7361         return false;
7362     }
7363
7364   size = int_size_in_bytes (TREE_TYPE (decl));
7365   return (size > 0 && size <= mips_section_threshold);
7366 }
7367
7368
7369 /* When generating embedded PIC code, SYMBOL_REF_FLAG is set for
7370    symbols which are not in the .text section.
7371
7372    When generating mips16 code, SYMBOL_REF_FLAG is set for string
7373    constants which are put in the .text section.  We also record the
7374    total length of all such strings; this total is used to decide
7375    whether we need to split the constant table, and need not be
7376    precisely correct.  */
7377
7378 static void
7379 mips_encode_section_info (tree decl, rtx rtl, int first)
7380 {
7381   rtx symbol;
7382
7383   if (GET_CODE (rtl) != MEM)
7384     return;
7385
7386   symbol = XEXP (rtl, 0);
7387
7388   if (GET_CODE (symbol) != SYMBOL_REF)
7389     return;
7390
7391   if (TARGET_MIPS16)
7392     {
7393       if (first && TREE_CODE (decl) == STRING_CST
7394           /* If this string is from a function, and the function will
7395              go in a gnu linkonce section, then we can't directly
7396              access the string.  This gets an assembler error
7397              "unsupported PC relative reference to different section".
7398              If we modify SELECT_SECTION to put it in function_section
7399              instead of text_section, it still fails because
7400              DECL_SECTION_NAME isn't set until assemble_start_function.
7401              If we fix that, it still fails because strings are shared
7402              among multiple functions, and we have cross section
7403              references again.  We force it to work by putting string
7404              addresses in the constant pool and indirecting.  */
7405           && (! current_function_decl
7406               || ! DECL_ONE_ONLY (current_function_decl)))
7407         {
7408           mips16_strings = alloc_EXPR_LIST (0, symbol, mips16_strings);
7409           SYMBOL_REF_FLAG (symbol) = 1;
7410           mips_string_length += TREE_STRING_LENGTH (decl);
7411         }
7412     }
7413
7414   if (TARGET_EMBEDDED_PIC)
7415     {
7416       if (TREE_CODE (decl) == VAR_DECL)
7417         SYMBOL_REF_FLAG (symbol) = 1;
7418       else if (TREE_CODE (decl) == FUNCTION_DECL)
7419         SYMBOL_REF_FLAG (symbol) = 0;
7420       else if (TREE_CODE (decl) == STRING_CST)
7421         SYMBOL_REF_FLAG (symbol) = 0;
7422       else
7423         SYMBOL_REF_FLAG (symbol) = 1;
7424     }
7425
7426   default_encode_section_info (decl, rtl, first);
7427 }
7428 \f
7429 /* See whether VALTYPE is a record whose fields should be returned in
7430    floating-point registers.  If so, return the number of fields and
7431    list them in FIELDS (which should have two elements).  Return 0
7432    otherwise.
7433
7434    For n32 & n64, a structure with one or two fields is returned in
7435    floating-point registers as long as every field has a floating-point
7436    type.  */
7437
7438 static int
7439 mips_fpr_return_fields (tree valtype, tree *fields)
7440 {
7441   tree field;
7442   int i;
7443
7444   if (!TARGET_NEWABI)
7445     return 0;
7446
7447   if (TREE_CODE (valtype) != RECORD_TYPE)
7448     return 0;
7449
7450   i = 0;
7451   for (field = TYPE_FIELDS (valtype); field != 0; field = TREE_CHAIN (field))
7452     {
7453       if (TREE_CODE (field) != FIELD_DECL)
7454         continue;
7455
7456       if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE)
7457         return 0;
7458
7459       if (i == 2)
7460         return 0;
7461
7462       fields[i++] = field;
7463     }
7464   return i;
7465 }
7466
7467
7468 /* Implement TARGET_RETURN_IN_MSB.  For n32 & n64, we should return
7469    a value in the most significant part of $2/$3 if:
7470
7471       - the target is big-endian;
7472
7473       - the value has a structure or union type (we generalize this to
7474         cover aggregates from other languages too); and
7475
7476       - the structure is not returned in floating-point registers.  */
7477
7478 static bool
7479 mips_return_in_msb (tree valtype)
7480 {
7481   tree fields[2];
7482
7483   return (TARGET_NEWABI
7484           && TARGET_BIG_ENDIAN
7485           && AGGREGATE_TYPE_P (valtype)
7486           && mips_fpr_return_fields (valtype, fields) == 0);
7487 }
7488
7489
7490 /* Return a composite value in a pair of floating-point registers.
7491    MODE1 and OFFSET1 are the mode and byte offset for the first value,
7492    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
7493    complete value.
7494
7495    For n32 & n64, $f0 always holds the first value and $f2 the second.
7496    Otherwise the values are packed together as closely as possible.  */
7497
7498 static rtx
7499 mips_return_fpr_pair (enum machine_mode mode,
7500                       enum machine_mode mode1, HOST_WIDE_INT offset1,
7501                       enum machine_mode mode2, HOST_WIDE_INT offset2)
7502 {
7503   int inc;
7504
7505   inc = (TARGET_NEWABI ? 2 : FP_INC);
7506   return gen_rtx_PARALLEL
7507     (mode,
7508      gen_rtvec (2,
7509                 gen_rtx_EXPR_LIST (VOIDmode,
7510                                    gen_rtx_REG (mode1, FP_RETURN),
7511                                    GEN_INT (offset1)),
7512                 gen_rtx_EXPR_LIST (VOIDmode,
7513                                    gen_rtx_REG (mode2, FP_RETURN + inc),
7514                                    GEN_INT (offset2))));
7515
7516 }
7517
7518
7519 /* Implement FUNCTION_VALUE and LIBCALL_VALUE.  For normal calls,
7520    VALTYPE is the return type and MODE is VOIDmode.  For libcalls,
7521    VALTYPE is null and MODE is the mode of the return value.  */
7522
7523 rtx
7524 mips_function_value (tree valtype, tree func ATTRIBUTE_UNUSED,
7525                      enum machine_mode mode)
7526 {
7527   if (valtype)
7528     {
7529       tree fields[2];
7530       int unsignedp;
7531
7532       mode = TYPE_MODE (valtype);
7533       unsignedp = TREE_UNSIGNED (valtype);
7534
7535       /* Since we define TARGET_PROMOTE_FUNCTION_RETURN that returns
7536          true, we must promote the mode just as PROMOTE_MODE does.  */
7537       mode = promote_mode (valtype, mode, &unsignedp, 1);
7538
7539       /* Handle structures whose fields are returned in $f0/$f2.  */
7540       switch (mips_fpr_return_fields (valtype, fields))
7541         {
7542         case 1:
7543           return gen_rtx_REG (mode, FP_RETURN);
7544
7545         case 2:
7546           return mips_return_fpr_pair (mode,
7547                                        TYPE_MODE (TREE_TYPE (fields[0])),
7548                                        int_byte_position (fields[0]),
7549                                        TYPE_MODE (TREE_TYPE (fields[1])),
7550                                        int_byte_position (fields[1]));
7551         }
7552
7553       /* If a value is passed in the most significant part of a register, see
7554          whether we have to round the mode up to a whole number of words.  */
7555       if (mips_return_in_msb (valtype))
7556         {
7557           HOST_WIDE_INT size = int_size_in_bytes (valtype);
7558           if (size % UNITS_PER_WORD != 0)
7559             {
7560               size += UNITS_PER_WORD - size % UNITS_PER_WORD;
7561               mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
7562             }
7563         }
7564     }
7565
7566   if (GET_MODE_CLASS (mode) == MODE_FLOAT
7567       && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
7568     return gen_rtx_REG (mode, FP_RETURN);
7569
7570   /* Handle long doubles for n32 & n64.  */
7571   if (mode == TFmode)
7572     return mips_return_fpr_pair (mode,
7573                                  DImode, 0,
7574                                  DImode, GET_MODE_SIZE (mode) / 2);
7575
7576   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
7577       && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)
7578     return mips_return_fpr_pair (mode,
7579                                  GET_MODE_INNER (mode), 0,
7580                                  GET_MODE_INNER (mode),
7581                                  GET_MODE_SIZE (mode) / 2);
7582
7583   return gen_rtx_REG (mode, GP_RETURN);
7584 }
7585
7586 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE.  Return
7587    nonzero when an argument must be passed by reference.  */
7588
7589 int
7590 function_arg_pass_by_reference (const CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
7591                                 enum machine_mode mode, tree type,
7592                                 int named ATTRIBUTE_UNUSED)
7593 {
7594   int size;
7595
7596   /* The EABI is the only one to pass args by reference.  */
7597   if (mips_abi != ABI_EABI)
7598     return 0;
7599
7600   /* ??? How should SCmode be handled?  */
7601   if (type == NULL_TREE || mode == DImode || mode == DFmode)
7602     return 0;
7603
7604   size = int_size_in_bytes (type);
7605   return size == -1 || size > UNITS_PER_WORD;
7606 }
7607
7608 /* Return the class of registers for which a mode change from FROM to TO
7609    is invalid.
7610
7611    In little-endian mode, the hi-lo registers are numbered backwards,
7612    so (subreg:SI (reg:DI hi) 0) gets the high word instead of the low
7613    word as intended.
7614
7615    Similarly, when using paired floating-point registers, the first
7616    register holds the low word, regardless of endianness.  So in big
7617    endian mode, (subreg:SI (reg:DF $f0) 0) does not get the high word
7618    as intended.
7619
7620    Also, loading a 32-bit value into a 64-bit floating-point register
7621    will not sign-extend the value, despite what LOAD_EXTEND_OP says.
7622    We can't allow 64-bit float registers to change from a 32-bit
7623    mode to a 64-bit mode.  */
7624
7625 bool
7626 mips_cannot_change_mode_class (enum machine_mode from,
7627                                enum machine_mode to, enum reg_class class)
7628 {
7629   if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
7630     {
7631       if (TARGET_BIG_ENDIAN)
7632         return reg_classes_intersect_p (FP_REGS, class);
7633       if (TARGET_FLOAT64)
7634         return reg_classes_intersect_p (HI_AND_FP_REGS, class);
7635       return reg_classes_intersect_p (HI_REG, class);
7636     }
7637   return false;
7638 }
7639
7640 /* Return true if X should not be moved directly into register $25.
7641    We need this because many versions of GAS will treat "la $25,foo" as
7642    part of a call sequence and so allow a global "foo" to be lazily bound.  */
7643
7644 bool
7645 mips_dangerous_for_la25_p (rtx x)
7646 {
7647   HOST_WIDE_INT offset;
7648
7649   if (TARGET_EXPLICIT_RELOCS)
7650     return false;
7651
7652   mips_split_const (x, &x, &offset);
7653   return global_got_operand (x, VOIDmode);
7654 }
7655
7656 /* Implement PREFERRED_RELOAD_CLASS.  */
7657
7658 enum reg_class
7659 mips_preferred_reload_class (rtx x, enum reg_class class)
7660 {
7661   if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, class))
7662     return LEA_REGS;
7663
7664   if (TARGET_HARD_FLOAT
7665       && FLOAT_MODE_P (GET_MODE (x))
7666       && reg_class_subset_p (FP_REGS, class))
7667     return FP_REGS;
7668
7669   if (reg_class_subset_p (GR_REGS, class))
7670     class = GR_REGS;
7671
7672   if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, class))
7673     class = M16_REGS;
7674
7675   return class;
7676 }
7677
7678 /* This function returns the register class required for a secondary
7679    register when copying between one of the registers in CLASS, and X,
7680    using MODE.  If IN_P is nonzero, the copy is going from X to the
7681    register, otherwise the register is the source.  A return value of
7682    NO_REGS means that no secondary register is required.  */
7683
7684 enum reg_class
7685 mips_secondary_reload_class (enum reg_class class,
7686                              enum machine_mode mode, rtx x, int in_p)
7687 {
7688   enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
7689   int regno = -1;
7690   int gp_reg_p;
7691
7692   if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
7693     regno = true_regnum (x);
7694
7695   gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
7696
7697   if (mips_dangerous_for_la25_p (x))
7698     {
7699       gr_regs = LEA_REGS;
7700       if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], 25))
7701         return gr_regs;
7702     }
7703
7704   /* Copying from HI or LO to anywhere other than a general register
7705      requires a general register.  */
7706   if (class == HI_REG || class == LO_REG || class == MD_REGS)
7707     {
7708       if (TARGET_MIPS16 && in_p)
7709         {
7710           /* We can't really copy to HI or LO at all in mips16 mode.  */
7711           return M16_REGS;
7712         }
7713       return gp_reg_p ? NO_REGS : gr_regs;
7714     }
7715   if (MD_REG_P (regno))
7716     {
7717       if (TARGET_MIPS16 && ! in_p)
7718         {
7719           /* We can't really copy to HI or LO at all in mips16 mode.  */
7720           return M16_REGS;
7721         }
7722       return class == gr_regs ? NO_REGS : gr_regs;
7723     }
7724
7725   /* We can only copy a value to a condition code register from a
7726      floating point register, and even then we require a scratch
7727      floating point register.  We can only copy a value out of a
7728      condition code register into a general register.  */
7729   if (class == ST_REGS)
7730     {
7731       if (in_p)
7732         return FP_REGS;
7733       return gp_reg_p ? NO_REGS : gr_regs;
7734     }
7735   if (ST_REG_P (regno))
7736     {
7737       if (! in_p)
7738         return FP_REGS;
7739       return class == gr_regs ? NO_REGS : gr_regs;
7740     }
7741
7742   if (class == FP_REGS)
7743     {
7744       if (GET_CODE (x) == MEM)
7745         {
7746           /* In this case we can use lwc1, swc1, ldc1 or sdc1.  */
7747           return NO_REGS;
7748         }
7749       else if (CONSTANT_P (x) && GET_MODE_CLASS (mode) == MODE_FLOAT)
7750         {
7751           /* We can use the l.s and l.d macros to load floating-point
7752              constants.  ??? For l.s, we could probably get better
7753              code by returning GR_REGS here.  */
7754           return NO_REGS;
7755         }
7756       else if (gp_reg_p || x == CONST0_RTX (mode))
7757         {
7758           /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
7759           return NO_REGS;
7760         }
7761       else if (FP_REG_P (regno))
7762         {
7763           /* In this case we can use mov.s or mov.d.  */
7764           return NO_REGS;
7765         }
7766       else
7767         {
7768           /* Otherwise, we need to reload through an integer register.  */
7769           return gr_regs;
7770         }
7771     }
7772
7773   /* In mips16 mode, going between memory and anything but M16_REGS
7774      requires an M16_REG.  */
7775   if (TARGET_MIPS16)
7776     {
7777       if (class != M16_REGS && class != M16_NA_REGS)
7778         {
7779           if (gp_reg_p)
7780             return NO_REGS;
7781           return M16_REGS;
7782         }
7783       if (! gp_reg_p)
7784         {
7785           if (class == M16_REGS || class == M16_NA_REGS)
7786             return NO_REGS;
7787           return M16_REGS;
7788         }
7789     }
7790
7791   return NO_REGS;
7792 }
7793
7794 /* Implement CLASS_MAX_NREGS.
7795
7796    Usually all registers are word-sized.  The only supported exception
7797    is -mgp64 -msingle-float, which has 64-bit words but 32-bit float
7798    registers.  A word-based calculation is correct even in that case,
7799    since -msingle-float disallows multi-FPR values.  */
7800
7801 int
7802 mips_class_max_nregs (enum reg_class class ATTRIBUTE_UNUSED,
7803                       enum machine_mode mode)
7804 {
7805   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7806 }
7807
7808 bool
7809 mips_valid_pointer_mode (enum machine_mode mode)
7810 {
7811   return (mode == SImode || (TARGET_64BIT && mode == DImode));
7812 }
7813
7814 \f
7815 /* If we can access small data directly (using gp-relative relocation
7816    operators) return the small data pointer, otherwise return null.
7817
7818    For each mips16 function which refers to GP relative symbols, we
7819    use a pseudo register, initialized at the start of the function, to
7820    hold the $gp value.  */
7821
7822 static rtx
7823 mips16_gp_pseudo_reg (void)
7824 {
7825   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
7826     {
7827       rtx const_gp;
7828       rtx insn, scan;
7829
7830       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
7831       RTX_UNCHANGING_P (cfun->machine->mips16_gp_pseudo_rtx) = 1;
7832
7833       /* We want to initialize this to a value which gcc will believe
7834          is constant.  */
7835       const_gp = gen_rtx_CONST (Pmode, pic_offset_table_rtx);
7836       start_sequence ();
7837       emit_move_insn (cfun->machine->mips16_gp_pseudo_rtx,
7838                       const_gp);
7839       insn = get_insns ();
7840       end_sequence ();
7841
7842       push_topmost_sequence ();
7843       /* We need to emit the initialization after the FUNCTION_BEG
7844          note, so that it will be integrated.  */
7845       for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
7846         if (GET_CODE (scan) == NOTE
7847             && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
7848           break;
7849       if (scan == NULL_RTX)
7850         scan = get_insns ();
7851       insn = emit_insn_after (insn, scan);
7852       pop_topmost_sequence ();
7853     }
7854
7855   return cfun->machine->mips16_gp_pseudo_rtx;
7856 }
7857
7858 /* Write out code to move floating point arguments in or out of
7859    general registers.  Output the instructions to FILE.  FP_CODE is
7860    the code describing which arguments are present (see the comment at
7861    the definition of CUMULATIVE_ARGS in mips.h).  FROM_FP_P is nonzero if
7862    we are copying from the floating point registers.  */
7863
7864 static void
7865 mips16_fp_args (FILE *file, int fp_code, int from_fp_p)
7866 {
7867   const char *s;
7868   int gparg, fparg;
7869   unsigned int f;
7870
7871   /* This code only works for the original 32 bit ABI and the O64 ABI.  */
7872   if (!TARGET_OLDABI)
7873     abort ();
7874
7875   if (from_fp_p)
7876     s = "mfc1";
7877   else
7878     s = "mtc1";
7879   gparg = GP_ARG_FIRST;
7880   fparg = FP_ARG_FIRST;
7881   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7882     {
7883       if ((f & 3) == 1)
7884         {
7885           if ((fparg & 1) != 0)
7886             ++fparg;
7887           fprintf (file, "\t%s\t%s,%s\n", s,
7888                    reg_names[gparg], reg_names[fparg]);
7889         }
7890       else if ((f & 3) == 2)
7891         {
7892           if (TARGET_64BIT)
7893             fprintf (file, "\td%s\t%s,%s\n", s,
7894                      reg_names[gparg], reg_names[fparg]);
7895           else
7896             {
7897               if ((fparg & 1) != 0)
7898                 ++fparg;
7899               if (TARGET_BIG_ENDIAN)
7900                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7901                          reg_names[gparg], reg_names[fparg + 1], s,
7902                          reg_names[gparg + 1], reg_names[fparg]);
7903               else
7904                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7905                          reg_names[gparg], reg_names[fparg], s,
7906                          reg_names[gparg + 1], reg_names[fparg + 1]);
7907               ++gparg;
7908               ++fparg;
7909             }
7910         }
7911       else
7912         abort ();
7913
7914       ++gparg;
7915       ++fparg;
7916     }
7917 }
7918
7919 /* Build a mips16 function stub.  This is used for functions which
7920    take arguments in the floating point registers.  It is 32 bit code
7921    that moves the floating point args into the general registers, and
7922    then jumps to the 16 bit code.  */
7923
7924 static void
7925 build_mips16_function_stub (FILE *file)
7926 {
7927   const char *fnname;
7928   char *secname, *stubname;
7929   tree stubid, stubdecl;
7930   int need_comma;
7931   unsigned int f;
7932
7933   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7934   secname = (char *) alloca (strlen (fnname) + 20);
7935   sprintf (secname, ".mips16.fn.%s", fnname);
7936   stubname = (char *) alloca (strlen (fnname) + 20);
7937   sprintf (stubname, "__fn_stub_%s", fnname);
7938   stubid = get_identifier (stubname);
7939   stubdecl = build_decl (FUNCTION_DECL, stubid,
7940                          build_function_type (void_type_node, NULL_TREE));
7941   DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
7942
7943   fprintf (file, "\t# Stub function for %s (", current_function_name ());
7944   need_comma = 0;
7945   for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
7946     {
7947       fprintf (file, "%s%s",
7948                need_comma ? ", " : "",
7949                (f & 3) == 1 ? "float" : "double");
7950       need_comma = 1;
7951     }
7952   fprintf (file, ")\n");
7953
7954   fprintf (file, "\t.set\tnomips16\n");
7955   function_section (stubdecl);
7956   ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
7957
7958   /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
7959      within a .ent, and we can not emit another .ent.  */
7960   if (!FUNCTION_NAME_ALREADY_DECLARED)
7961     {
7962       fputs ("\t.ent\t", file);
7963       assemble_name (file, stubname);
7964       fputs ("\n", file);
7965     }
7966
7967   assemble_name (file, stubname);
7968   fputs (":\n", file);
7969
7970   /* We don't want the assembler to insert any nops here.  */
7971   fprintf (file, "\t.set\tnoreorder\n");
7972
7973   mips16_fp_args (file, current_function_args_info.fp_code, 1);
7974
7975   fprintf (asm_out_file, "\t.set\tnoat\n");
7976   fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
7977   assemble_name (file, fnname);
7978   fprintf (file, "\n");
7979   fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
7980   fprintf (asm_out_file, "\t.set\tat\n");
7981
7982   /* Unfortunately, we can't fill the jump delay slot.  We can't fill
7983      with one of the mfc1 instructions, because the result is not
7984      available for one instruction, so if the very first instruction
7985      in the function refers to the register, it will see the wrong
7986      value.  */
7987   fprintf (file, "\tnop\n");
7988
7989   fprintf (file, "\t.set\treorder\n");
7990
7991   if (!FUNCTION_NAME_ALREADY_DECLARED)
7992     {
7993       fputs ("\t.end\t", file);
7994       assemble_name (file, stubname);
7995       fputs ("\n", file);
7996     }
7997
7998   fprintf (file, "\t.set\tmips16\n");
7999
8000   function_section (current_function_decl);
8001 }
8002
8003 /* We keep a list of functions for which we have already built stubs
8004    in build_mips16_call_stub.  */
8005
8006 struct mips16_stub
8007 {
8008   struct mips16_stub *next;
8009   char *name;
8010   int fpret;
8011 };
8012
8013 static struct mips16_stub *mips16_stubs;
8014
8015 /* Build a call stub for a mips16 call.  A stub is needed if we are
8016    passing any floating point values which should go into the floating
8017    point registers.  If we are, and the call turns out to be to a 32
8018    bit function, the stub will be used to move the values into the
8019    floating point registers before calling the 32 bit function.  The
8020    linker will magically adjust the function call to either the 16 bit
8021    function or the 32 bit stub, depending upon where the function call
8022    is actually defined.
8023
8024    Similarly, we need a stub if the return value might come back in a
8025    floating point register.
8026
8027    RETVAL is the location of the return value, or null if this is
8028    a call rather than a call_value.  FN is the address of the
8029    function and ARG_SIZE is the size of the arguments.  FP_CODE
8030    is the code built by function_arg.  This function returns a nonzero
8031    value if it builds the call instruction itself.  */
8032
8033 int
8034 build_mips16_call_stub (rtx retval, rtx fn, rtx arg_size, int fp_code)
8035 {
8036   int fpret;
8037   const char *fnname;
8038   char *secname, *stubname;
8039   struct mips16_stub *l;
8040   tree stubid, stubdecl;
8041   int need_comma;
8042   unsigned int f;
8043
8044   /* We don't need to do anything if we aren't in mips16 mode, or if
8045      we were invoked with the -msoft-float option.  */
8046   if (! TARGET_MIPS16 || ! mips16_hard_float)
8047     return 0;
8048
8049   /* Figure out whether the value might come back in a floating point
8050      register.  */
8051   fpret = (retval != 0
8052            && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
8053            && GET_MODE_SIZE (GET_MODE (retval)) <= UNITS_PER_FPVALUE);
8054
8055   /* We don't need to do anything if there were no floating point
8056      arguments and the value will not be returned in a floating point
8057      register.  */
8058   if (fp_code == 0 && ! fpret)
8059     return 0;
8060
8061   /* We don't need to do anything if this is a call to a special
8062      mips16 support function.  */
8063   if (GET_CODE (fn) == SYMBOL_REF
8064       && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
8065     return 0;
8066
8067   /* This code will only work for o32 and o64 abis.  The other ABI's
8068      require more sophisticated support.  */
8069   if (!TARGET_OLDABI)
8070     abort ();
8071
8072   /* We can only handle SFmode and DFmode floating point return
8073      values.  */
8074   if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
8075     abort ();
8076
8077   /* If we're calling via a function pointer, then we must always call
8078      via a stub.  There are magic stubs provided in libgcc.a for each
8079      of the required cases.  Each of them expects the function address
8080      to arrive in register $2.  */
8081
8082   if (GET_CODE (fn) != SYMBOL_REF)
8083     {
8084       char buf[30];
8085       tree id;
8086       rtx stub_fn, insn;
8087
8088       /* ??? If this code is modified to support other ABI's, we need
8089          to handle PARALLEL return values here.  */
8090
8091       sprintf (buf, "__mips16_call_stub_%s%d",
8092                (fpret
8093                 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
8094                 : ""),
8095                fp_code);
8096       id = get_identifier (buf);
8097       stub_fn = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (id));
8098
8099       emit_move_insn (gen_rtx_REG (Pmode, 2), fn);
8100
8101       if (retval == NULL_RTX)
8102         insn = gen_call_internal (stub_fn, arg_size);
8103       else
8104         insn = gen_call_value_internal (retval, stub_fn, arg_size);
8105       insn = emit_call_insn (insn);
8106
8107       /* Put the register usage information on the CALL.  */
8108       if (GET_CODE (insn) != CALL_INSN)
8109         abort ();
8110       CALL_INSN_FUNCTION_USAGE (insn) =
8111         gen_rtx_EXPR_LIST (VOIDmode,
8112                            gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 2)),
8113                            CALL_INSN_FUNCTION_USAGE (insn));
8114
8115       /* If we are handling a floating point return value, we need to
8116          save $18 in the function prologue.  Putting a note on the
8117          call will mean that regs_ever_live[$18] will be true if the
8118          call is not eliminated, and we can check that in the prologue
8119          code.  */
8120       if (fpret)
8121         CALL_INSN_FUNCTION_USAGE (insn) =
8122           gen_rtx_EXPR_LIST (VOIDmode,
8123                              gen_rtx_USE (VOIDmode,
8124                                           gen_rtx_REG (word_mode, 18)),
8125                              CALL_INSN_FUNCTION_USAGE (insn));
8126
8127       /* Return 1 to tell the caller that we've generated the call
8128          insn.  */
8129       return 1;
8130     }
8131
8132   /* We know the function we are going to call.  If we have already
8133      built a stub, we don't need to do anything further.  */
8134
8135   fnname = XSTR (fn, 0);
8136   for (l = mips16_stubs; l != NULL; l = l->next)
8137     if (strcmp (l->name, fnname) == 0)
8138       break;
8139
8140   if (l == NULL)
8141     {
8142       /* Build a special purpose stub.  When the linker sees a
8143          function call in mips16 code, it will check where the target
8144          is defined.  If the target is a 32 bit call, the linker will
8145          search for the section defined here.  It can tell which
8146          symbol this section is associated with by looking at the
8147          relocation information (the name is unreliable, since this
8148          might be a static function).  If such a section is found, the
8149          linker will redirect the call to the start of the magic
8150          section.
8151
8152          If the function does not return a floating point value, the
8153          special stub section is named
8154              .mips16.call.FNNAME
8155
8156          If the function does return a floating point value, the stub
8157          section is named
8158              .mips16.call.fp.FNNAME
8159          */
8160
8161       secname = (char *) alloca (strlen (fnname) + 40);
8162       sprintf (secname, ".mips16.call.%s%s",
8163                fpret ? "fp." : "",
8164                fnname);
8165       stubname = (char *) alloca (strlen (fnname) + 20);
8166       sprintf (stubname, "__call_stub_%s%s",
8167                fpret ? "fp_" : "",
8168                fnname);
8169       stubid = get_identifier (stubname);
8170       stubdecl = build_decl (FUNCTION_DECL, stubid,
8171                              build_function_type (void_type_node, NULL_TREE));
8172       DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8173
8174       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8175                (fpret
8176                 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8177                 : ""),
8178                fnname);
8179       need_comma = 0;
8180       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8181         {
8182           fprintf (asm_out_file, "%s%s",
8183                    need_comma ? ", " : "",
8184                    (f & 3) == 1 ? "float" : "double");
8185           need_comma = 1;
8186         }
8187       fprintf (asm_out_file, ")\n");
8188
8189       fprintf (asm_out_file, "\t.set\tnomips16\n");
8190       assemble_start_function (stubdecl, stubname);
8191
8192       if (!FUNCTION_NAME_ALREADY_DECLARED)
8193         {
8194           fputs ("\t.ent\t", asm_out_file);
8195           assemble_name (asm_out_file, stubname);
8196           fputs ("\n", asm_out_file);
8197
8198           assemble_name (asm_out_file, stubname);
8199           fputs (":\n", asm_out_file);
8200         }
8201
8202       /* We build the stub code by hand.  That's the only way we can
8203          do it, since we can't generate 32 bit code during a 16 bit
8204          compilation.  */
8205
8206       /* We don't want the assembler to insert any nops here.  */
8207       fprintf (asm_out_file, "\t.set\tnoreorder\n");
8208
8209       mips16_fp_args (asm_out_file, fp_code, 0);
8210
8211       if (! fpret)
8212         {
8213           fprintf (asm_out_file, "\t.set\tnoat\n");
8214           fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
8215                    fnname);
8216           fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8217           fprintf (asm_out_file, "\t.set\tat\n");
8218           /* Unfortunately, we can't fill the jump delay slot.  We
8219              can't fill with one of the mtc1 instructions, because the
8220              result is not available for one instruction, so if the
8221              very first instruction in the function refers to the
8222              register, it will see the wrong value.  */
8223           fprintf (asm_out_file, "\tnop\n");
8224         }
8225       else
8226         {
8227           fprintf (asm_out_file, "\tmove\t%s,%s\n",
8228                    reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
8229           fprintf (asm_out_file, "\tjal\t%s\n", fnname);
8230           /* As above, we can't fill the delay slot.  */
8231           fprintf (asm_out_file, "\tnop\n");
8232           if (GET_MODE (retval) == SFmode)
8233             fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8234                      reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
8235           else
8236             {
8237               if (TARGET_BIG_ENDIAN)
8238                 {
8239                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8240                            reg_names[GP_REG_FIRST + 2],
8241                            reg_names[FP_REG_FIRST + 1]);
8242                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8243                            reg_names[GP_REG_FIRST + 3],
8244                            reg_names[FP_REG_FIRST + 0]);
8245                 }
8246               else
8247                 {
8248                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8249                            reg_names[GP_REG_FIRST + 2],
8250                            reg_names[FP_REG_FIRST + 0]);
8251                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8252                            reg_names[GP_REG_FIRST + 3],
8253                            reg_names[FP_REG_FIRST + 1]);
8254                 }
8255             }
8256           fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
8257           /* As above, we can't fill the delay slot.  */
8258           fprintf (asm_out_file, "\tnop\n");
8259         }
8260
8261       fprintf (asm_out_file, "\t.set\treorder\n");
8262
8263 #ifdef ASM_DECLARE_FUNCTION_SIZE
8264       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8265 #endif
8266
8267       if (!FUNCTION_NAME_ALREADY_DECLARED)
8268         {
8269           fputs ("\t.end\t", asm_out_file);
8270           assemble_name (asm_out_file, stubname);
8271           fputs ("\n", asm_out_file);
8272         }
8273
8274       fprintf (asm_out_file, "\t.set\tmips16\n");
8275
8276       /* Record this stub.  */
8277       l = (struct mips16_stub *) xmalloc (sizeof *l);
8278       l->name = xstrdup (fnname);
8279       l->fpret = fpret;
8280       l->next = mips16_stubs;
8281       mips16_stubs = l;
8282     }
8283
8284   /* If we expect a floating point return value, but we've built a
8285      stub which does not expect one, then we're in trouble.  We can't
8286      use the existing stub, because it won't handle the floating point
8287      value.  We can't build a new stub, because the linker won't know
8288      which stub to use for the various calls in this object file.
8289      Fortunately, this case is illegal, since it means that a function
8290      was declared in two different ways in a single compilation.  */
8291   if (fpret && ! l->fpret)
8292     error ("can not handle inconsistent calls to `%s'", fnname);
8293
8294   /* If we are calling a stub which handles a floating point return
8295      value, we need to arrange to save $18 in the prologue.  We do
8296      this by marking the function call as using the register.  The
8297      prologue will later see that it is used, and emit code to save
8298      it.  */
8299
8300   if (l->fpret)
8301     {
8302       rtx insn;
8303
8304       if (retval == NULL_RTX)
8305         insn = gen_call_internal (fn, arg_size);
8306       else
8307         insn = gen_call_value_internal (retval, fn, arg_size);
8308       insn = emit_call_insn (insn);
8309
8310       if (GET_CODE (insn) != CALL_INSN)
8311         abort ();
8312
8313       CALL_INSN_FUNCTION_USAGE (insn) =
8314         gen_rtx_EXPR_LIST (VOIDmode,
8315                            gen_rtx_USE (VOIDmode, gen_rtx_REG (word_mode, 18)),
8316                            CALL_INSN_FUNCTION_USAGE (insn));
8317
8318       /* Return 1 to tell the caller that we've generated the call
8319          insn.  */
8320       return 1;
8321     }
8322
8323   /* Return 0 to let the caller generate the call insn.  */
8324   return 0;
8325 }
8326
8327 /* We keep a list of constants we which we have to add to internal
8328    constant tables in the middle of large functions.  */
8329
8330 struct constant
8331 {
8332   struct constant *next;
8333   rtx value;
8334   rtx label;
8335   enum machine_mode mode;
8336 };
8337
8338 /* Add a constant to the list in *PCONSTANTS.  */
8339
8340 static rtx
8341 add_constant (struct constant **pconstants, rtx val, enum machine_mode mode)
8342 {
8343   struct constant *c;
8344
8345   for (c = *pconstants; c != NULL; c = c->next)
8346     if (mode == c->mode && rtx_equal_p (val, c->value))
8347       return c->label;
8348
8349   c = (struct constant *) xmalloc (sizeof *c);
8350   c->value = val;
8351   c->mode = mode;
8352   c->label = gen_label_rtx ();
8353   c->next = *pconstants;
8354   *pconstants = c;
8355   return c->label;
8356 }
8357
8358 /* Dump out the constants in CONSTANTS after INSN.  */
8359
8360 static void
8361 dump_constants (struct constant *constants, rtx insn)
8362 {
8363   struct constant *c;
8364   int align;
8365
8366   c = constants;
8367   align = 0;
8368   while (c != NULL)
8369     {
8370       rtx r;
8371       struct constant *next;
8372
8373       switch (GET_MODE_SIZE (c->mode))
8374         {
8375         case 1:
8376           align = 0;
8377           break;
8378         case 2:
8379           if (align < 1)
8380             insn = emit_insn_after (gen_align_2 (), insn);
8381           align = 1;
8382           break;
8383         case 4:
8384           if (align < 2)
8385             insn = emit_insn_after (gen_align_4 (), insn);
8386           align = 2;
8387           break;
8388         default:
8389           if (align < 3)
8390             insn = emit_insn_after (gen_align_8 (), insn);
8391           align = 3;
8392           break;
8393         }
8394
8395       insn = emit_label_after (c->label, insn);
8396
8397       switch (c->mode)
8398         {
8399         case QImode:
8400           r = gen_consttable_qi (c->value);
8401           break;
8402         case HImode:
8403           r = gen_consttable_hi (c->value);
8404           break;
8405         case SImode:
8406           r = gen_consttable_si (c->value);
8407           break;
8408         case SFmode:
8409           r = gen_consttable_sf (c->value);
8410           break;
8411         case DImode:
8412           r = gen_consttable_di (c->value);
8413           break;
8414         case DFmode:
8415           r = gen_consttable_df (c->value);
8416           break;
8417         default:
8418           abort ();
8419         }
8420
8421       insn = emit_insn_after (r, insn);
8422
8423       next = c->next;
8424       free (c);
8425       c = next;
8426     }
8427
8428   emit_barrier_after (insn);
8429 }
8430
8431 /* Find the symbol in an address expression.  */
8432
8433 static rtx
8434 mips_find_symbol (rtx addr)
8435 {
8436   if (GET_CODE (addr) == MEM)
8437     addr = XEXP (addr, 0);
8438   while (GET_CODE (addr) == CONST)
8439     addr = XEXP (addr, 0);
8440   if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
8441     return addr;
8442   if (GET_CODE (addr) == PLUS)
8443     {
8444       rtx l1, l2;
8445
8446       l1 = mips_find_symbol (XEXP (addr, 0));
8447       l2 = mips_find_symbol (XEXP (addr, 1));
8448       if (l1 != NULL_RTX && l2 == NULL_RTX)
8449         return l1;
8450       else if (l1 == NULL_RTX && l2 != NULL_RTX)
8451         return l2;
8452     }
8453   return NULL_RTX;
8454 }
8455
8456 /* In mips16 mode, we need to look through the function to check for
8457    PC relative loads that are out of range.  */
8458
8459 static void
8460 mips16_lay_out_constants (void)
8461 {
8462   int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
8463   rtx first, insn;
8464   struct constant *constants;
8465
8466   first = get_insns ();
8467
8468   /* Scan the function looking for PC relative loads which may be out
8469      of range.  All such loads will either be from the constant table,
8470      or be getting the address of a constant string.  If the size of
8471      the function plus the size of the constant table is less than
8472      0x8000, then all loads are in range.  */
8473
8474   insns_len = 0;
8475   for (insn = first; insn; insn = NEXT_INSN (insn))
8476     {
8477       insns_len += get_attr_length (insn);
8478
8479       /* ??? We put switch tables in .text, but we don't define
8480          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
8481          compute their lengths correctly.  */
8482       if (GET_CODE (insn) == JUMP_INSN)
8483         {
8484           rtx body;
8485
8486           body = PATTERN (insn);
8487           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
8488             insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
8489                           * GET_MODE_SIZE (GET_MODE (body)));
8490           insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
8491         }
8492     }
8493
8494   /* Store the original value of insns_len in cfun->machine, so
8495      that m16_usym8_4 and m16_usym5_4 can look at it.  */
8496   cfun->machine->insns_len = insns_len;
8497
8498   pool_size = get_pool_size ();
8499   if (insns_len + pool_size + mips_string_length < 0x8000)
8500     return;
8501
8502   /* Loop over the insns and figure out what the maximum internal pool
8503      size could be.  */
8504   max_internal_pool_size = 0;
8505   for (insn = first; insn; insn = NEXT_INSN (insn))
8506     {
8507       if (GET_CODE (insn) == INSN
8508           && GET_CODE (PATTERN (insn)) == SET)
8509         {
8510           rtx src;
8511
8512           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
8513           if (src == NULL_RTX)
8514             continue;
8515           if (CONSTANT_POOL_ADDRESS_P (src))
8516             max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
8517           else if (SYMBOL_REF_FLAG (src))
8518             max_internal_pool_size += GET_MODE_SIZE (Pmode);
8519         }
8520     }
8521
8522   constants = NULL;
8523   addr = 0;
8524   first_constant_ref = -1;
8525
8526   for (insn = first; insn; insn = NEXT_INSN (insn))
8527     {
8528       if (GET_CODE (insn) == INSN
8529           && GET_CODE (PATTERN (insn)) == SET)
8530         {
8531           rtx val, src;
8532           enum machine_mode mode = VOIDmode;
8533
8534           val = NULL_RTX;
8535           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
8536           if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
8537             {
8538               /* ??? This is very conservative, which means that we
8539                  will generate too many copies of the constant table.
8540                  The only solution would seem to be some form of
8541                  relaxing.  */
8542               if (((insns_len - addr)
8543                    + max_internal_pool_size
8544                    + get_pool_offset (src))
8545                   >= 0x8000)
8546                 {
8547                   val = get_pool_constant (src);
8548                   mode = get_pool_mode (src);
8549                 }
8550               max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
8551             }
8552           else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
8553             {
8554               /* Including all of mips_string_length is conservative,
8555                  and so is including all of max_internal_pool_size.  */
8556               if (((insns_len - addr)
8557                    + max_internal_pool_size
8558                    + pool_size
8559                    + mips_string_length)
8560                   >= 0x8000)
8561                 {
8562                   val = src;
8563                   mode = Pmode;
8564                 }
8565               max_internal_pool_size -= Pmode;
8566             }
8567
8568           if (val != NULL_RTX)
8569             {
8570               rtx lab, newsrc;
8571
8572               /* This PC relative load is out of range.  ??? In the
8573                  case of a string constant, we are only guessing that
8574                  it is range, since we don't know the offset of a
8575                  particular string constant.  */
8576
8577               lab = add_constant (&constants, val, mode);
8578               newsrc = gen_rtx_MEM (mode,
8579                                     gen_rtx_LABEL_REF (VOIDmode, lab));
8580               RTX_UNCHANGING_P (newsrc) = 1;
8581               PATTERN (insn) = gen_rtx_SET (VOIDmode,
8582                                             SET_DEST (PATTERN (insn)),
8583                                             newsrc);
8584               INSN_CODE (insn) = -1;
8585
8586               if (first_constant_ref < 0)
8587                 first_constant_ref = addr;
8588             }
8589         }
8590
8591       addr += get_attr_length (insn);
8592
8593       /* ??? We put switch tables in .text, but we don't define
8594          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
8595          compute their lengths correctly.  */
8596       if (GET_CODE (insn) == JUMP_INSN)
8597         {
8598           rtx body;
8599
8600           body = PATTERN (insn);
8601           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
8602             addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
8603                           * GET_MODE_SIZE (GET_MODE (body)));
8604           addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
8605         }
8606
8607       if (GET_CODE (insn) == BARRIER)
8608         {
8609           /* Output any constants we have accumulated.  Note that we
8610              don't need to change ADDR, since its only use is
8611              subtraction from INSNS_LEN, and both would be changed by
8612              the same amount.
8613              ??? If the instructions up to the next barrier reuse a
8614              constant, it would often be better to continue
8615              accumulating.  */
8616           if (constants != NULL)
8617             dump_constants (constants, insn);
8618           constants = NULL;
8619           first_constant_ref = -1;
8620         }
8621
8622       if (constants != NULL
8623                && (NEXT_INSN (insn) == NULL
8624                    || (first_constant_ref >= 0
8625                        && (((addr - first_constant_ref)
8626                             + 2 /* for alignment */
8627                             + 2 /* for a short jump insn */
8628                             + pool_size)
8629                            >= 0x8000))))
8630         {
8631           /* If we haven't had a barrier within 0x8000 bytes of a
8632              constant reference or we are at the end of the function,
8633              emit a barrier now.  */
8634
8635           rtx label, jump, barrier;
8636
8637           label = gen_label_rtx ();
8638           jump = emit_jump_insn_after (gen_jump (label), insn);
8639           JUMP_LABEL (jump) = label;
8640           LABEL_NUSES (label) = 1;
8641           barrier = emit_barrier_after (jump);
8642           emit_label_after (label, barrier);
8643           first_constant_ref = -1;
8644         }
8645      }
8646
8647   /* ??? If we output all references to a constant in internal
8648      constants table, we don't need to output the constant in the real
8649      constant table, but we have no way to prevent that.  */
8650 }
8651
8652
8653 /* Subroutine of mips_reorg.  If there is a hazard between INSN
8654    and a previous instruction, avoid it by inserting nops after
8655    instruction AFTER.
8656
8657    *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
8658    this point.  If *DELAYED_REG is non-null, INSN must wait a cycle
8659    before using the value of that register.  *HILO_DELAY counts the
8660    number of instructions since the last hilo hazard (that is,
8661    the number of instructions since the last mflo or mfhi).
8662
8663    After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
8664    for the next instruction.
8665
8666    LO_REG is an rtx for the LO register, used in dependence checking.  */
8667
8668 static void
8669 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
8670                    rtx *delayed_reg, rtx lo_reg)
8671 {
8672   rtx pattern, set;
8673   int nops, ninsns;
8674
8675   if (!INSN_P (insn))
8676     return;
8677
8678   pattern = PATTERN (insn);
8679
8680   /* Do not put the whole function in .set noreorder if it contains
8681      an asm statement.  We don't know whether there will be hazards
8682      between the asm statement and the gcc-generated code.  */
8683   if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
8684     cfun->machine->all_noreorder_p = false;
8685
8686   /* Ignore zero-length instructions (barriers and the like).  */
8687   ninsns = get_attr_length (insn) / 4;
8688   if (ninsns == 0)
8689     return;
8690
8691   /* Work out how many nops are needed.  Note that we only care about
8692      registers that are explicitly mentioned in the instruction's pattern.
8693      It doesn't matter that calls use the argument registers or that they
8694      clobber hi and lo.  */
8695   if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
8696     nops = 2 - *hilo_delay;
8697   else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
8698     nops = 1;
8699   else
8700     nops = 0;
8701
8702   /* Insert the nops between this instruction and the previous one.
8703      Each new nop takes us further from the last hilo hazard.  */
8704   *hilo_delay += nops;
8705   while (nops-- > 0)
8706     emit_insn_after (gen_hazard_nop (), after);
8707
8708   /* Set up the state for the next instruction.  */
8709   *hilo_delay += ninsns;
8710   *delayed_reg = 0;
8711   if (INSN_CODE (insn) >= 0)
8712     switch (get_attr_hazard (insn))
8713       {
8714       case HAZARD_NONE:
8715         break;
8716
8717       case HAZARD_HILO:
8718         *hilo_delay = 0;
8719         break;
8720
8721       case HAZARD_DELAY:
8722         set = single_set (insn);
8723         if (set == 0)
8724           abort ();
8725         *delayed_reg = SET_DEST (set);
8726         break;
8727       }
8728 }
8729
8730
8731 /* Go through the instruction stream and insert nops where necessary.
8732    See if the whole function can then be put into .set noreorder &
8733    .set nomacro.  */
8734
8735 static void
8736 mips_avoid_hazards (void)
8737 {
8738   rtx insn, last_insn, lo_reg, delayed_reg;
8739   int hilo_delay, i;
8740
8741   /* Recalculate instruction lengths without taking nops into account.  */
8742   cfun->machine->ignore_hazard_length_p = true;
8743   shorten_branches (get_insns ());
8744
8745   /* The profiler code uses assembler macros.  */
8746   cfun->machine->all_noreorder_p = !current_function_profile;
8747
8748   last_insn = 0;
8749   hilo_delay = 2;
8750   delayed_reg = 0;
8751   lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
8752
8753   for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
8754     if (INSN_P (insn))
8755       {
8756         if (GET_CODE (PATTERN (insn)) == SEQUENCE)
8757           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8758             mips_avoid_hazard (last_insn, XVECEXP (PATTERN (insn), 0, i),
8759                                &hilo_delay, &delayed_reg, lo_reg);
8760         else
8761           mips_avoid_hazard (last_insn, insn, &hilo_delay,
8762                              &delayed_reg, lo_reg);
8763
8764         last_insn = insn;
8765       }
8766 }
8767
8768
8769 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */
8770
8771 static void
8772 mips_reorg (void)
8773 {
8774   if (TARGET_MIPS16)
8775     mips16_lay_out_constants ();
8776   else if (TARGET_EXPLICIT_RELOCS)
8777     {
8778       if (mips_flag_delayed_branch)
8779         dbr_schedule (get_insns (), dump_file);
8780       mips_avoid_hazards ();
8781     }
8782 }
8783
8784 /* We need to use a special set of functions to handle hard floating
8785    point code in mips16 mode.  Also, allow for --enable-gofast.  */
8786
8787 #include "config/gofast.h"
8788
8789 static void
8790 mips_init_libfuncs (void)
8791 {
8792   if (TARGET_MIPS16 && mips16_hard_float)
8793     {
8794       set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
8795       set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
8796       set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
8797       set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
8798
8799       set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
8800       set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
8801       set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
8802       set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
8803       set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
8804       set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
8805
8806       set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fixsfsi");
8807       set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
8808
8809       if (TARGET_DOUBLE_FLOAT)
8810         {
8811           set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
8812           set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
8813           set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
8814           set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
8815
8816           set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
8817           set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
8818           set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
8819           set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
8820           set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
8821           set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
8822
8823           set_conv_libfunc (sext_optab, DFmode, SFmode, "__mips16_extendsfdf2");
8824           set_conv_libfunc (trunc_optab, SFmode, DFmode, "__mips16_truncdfsf2");
8825
8826           set_conv_libfunc (sfix_optab, SImode, DFmode, "__mips16_fixdfsi");
8827           set_conv_libfunc (sfloat_optab, DFmode, SImode, "__mips16_floatsidf");
8828         }
8829     }
8830   else
8831     gofast_maybe_init_libfuncs ();
8832 }
8833
8834 /* Return a number assessing the cost of moving a register in class
8835    FROM to class TO.  The classes are expressed using the enumeration
8836    values such as `GENERAL_REGS'.  A value of 2 is the default; other
8837    values are interpreted relative to that.
8838
8839    It is not required that the cost always equal 2 when FROM is the
8840    same as TO; on some machines it is expensive to move between
8841    registers if they are not general registers.
8842
8843    If reload sees an insn consisting of a single `set' between two
8844    hard registers, and if `REGISTER_MOVE_COST' applied to their
8845    classes returns a value of 2, reload does not check to ensure that
8846    the constraints of the insn are met.  Setting a cost of other than
8847    2 will allow reload to verify that the constraints are met.  You
8848    should do this if the `movM' pattern's constraints do not allow
8849    such copying.
8850
8851    ??? We make the cost of moving from HI/LO into general
8852    registers the same as for one of moving general registers to
8853    HI/LO for TARGET_MIPS16 in order to prevent allocating a
8854    pseudo to HI/LO.  This might hurt optimizations though, it
8855    isn't clear if it is wise.  And it might not work in all cases.  We
8856    could solve the DImode LO reg problem by using a multiply, just
8857    like reload_{in,out}si.  We could solve the SImode/HImode HI reg
8858    problem by using divide instructions.  divu puts the remainder in
8859    the HI reg, so doing a divide by -1 will move the value in the HI
8860    reg for all values except -1.  We could handle that case by using a
8861    signed divide, e.g.  -1 / 2 (or maybe 1 / -2?).  We'd have to emit
8862    a compare/branch to test the input value to see which instruction
8863    we need to use.  This gets pretty messy, but it is feasible.  */
8864
8865 int
8866 mips_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
8867                          enum reg_class to, enum reg_class from)
8868 {
8869   if (from == M16_REGS && GR_REG_CLASS_P (to))
8870     return 2;
8871   else if (from == M16_NA_REGS && GR_REG_CLASS_P (to))
8872     return 2;
8873   else if (GR_REG_CLASS_P (from))
8874     {
8875       if (to == M16_REGS)
8876         return 2;
8877       else if (to == M16_NA_REGS)
8878         return 2;
8879       else if (GR_REG_CLASS_P (to))
8880         {
8881           if (TARGET_MIPS16)
8882             return 4;
8883           else
8884             return 2;
8885         }
8886       else if (to == FP_REGS)
8887         return 4;
8888       else if (to == HI_REG || to == LO_REG || to == MD_REGS)
8889         {
8890           if (TARGET_MIPS16)
8891             return 12;
8892           else
8893             return 6;
8894         }
8895       else if (COP_REG_CLASS_P (to))
8896         {
8897           return 5;
8898         }
8899     }  /* GR_REG_CLASS_P (from) */
8900   else if (from == FP_REGS)
8901     {
8902       if (GR_REG_CLASS_P (to))
8903         return 4;
8904       else if (to == FP_REGS)
8905         return 2;
8906       else if (to == ST_REGS)
8907         return 8;
8908     }  /* from == FP_REGS */
8909   else if (from == HI_REG || from == LO_REG || from == MD_REGS)
8910     {
8911       if (GR_REG_CLASS_P (to))
8912         {
8913           if (TARGET_MIPS16)
8914             return 12;
8915           else
8916             return 6;
8917         }
8918     }  /* from == HI_REG, etc.  */
8919   else if (from == ST_REGS && GR_REG_CLASS_P (to))
8920     return 4;
8921   else if (COP_REG_CLASS_P (from))
8922     {
8923       return 5;
8924     }  /* COP_REG_CLASS_P (from) */
8925
8926   /* Fall through.  */
8927
8928   return 12;
8929 }
8930
8931 /* Return the length of INSN.  LENGTH is the initial length computed by
8932    attributes in the machine-description file.  */
8933
8934 int
8935 mips_adjust_insn_length (rtx insn, int length)
8936 {
8937   /* A unconditional jump has an unfilled delay slot if it is not part
8938      of a sequence.  A conditional jump normally has a delay slot, but
8939      does not on MIPS16.  */
8940   if (simplejump_p (insn)
8941       || (!TARGET_MIPS16  && (GET_CODE (insn) == JUMP_INSN
8942                               || GET_CODE (insn) == CALL_INSN)))
8943     length += 4;
8944
8945   /* See how many nops might be needed to avoid hardware hazards.  */
8946   if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
8947     switch (get_attr_hazard (insn))
8948       {
8949       case HAZARD_NONE:
8950         break;
8951
8952       case HAZARD_DELAY:
8953         length += 4;
8954         break;
8955
8956       case HAZARD_HILO:
8957         length += 8;
8958         break;
8959       }
8960
8961   /* All MIPS16 instructions are a measly two bytes.  */
8962   if (TARGET_MIPS16)
8963     length /= 2;
8964
8965   return length;
8966 }
8967
8968
8969 /* Return an asm sequence to start a noat block and load the address
8970    of a label into $1.  */
8971
8972 const char *
8973 mips_output_load_label (void)
8974 {
8975   if (TARGET_EXPLICIT_RELOCS)
8976     switch (mips_abi)
8977       {
8978       case ABI_N32:
8979         return "%[lw\t%@,%%got_page(%0)(%+)\n\taddiu\t%@,%@,%%got_ofst(%0)";
8980
8981       case ABI_64:
8982         return "%[ld\t%@,%%got_page(%0)(%+)\n\tdaddiu\t%@,%@,%%got_ofst(%0)";
8983
8984       default:
8985         if (ISA_HAS_LOAD_DELAY)
8986           return "%[lw\t%@,%%got(%0)(%+)%#\n\taddiu\t%@,%@,%%lo(%0)";
8987         return "%[lw\t%@,%%got(%0)(%+)\n\taddiu\t%@,%@,%%lo(%0)";
8988       }
8989   else
8990     {
8991       if (Pmode == DImode)
8992         return "%[dla\t%@,%0";
8993       else
8994         return "%[la\t%@,%0";
8995     }
8996 }
8997
8998
8999 /* Output assembly instructions to peform a conditional branch.
9000
9001    INSN is the branch instruction.  OPERANDS[0] is the condition.
9002    OPERANDS[1] is the target of the branch.  OPERANDS[2] is the target
9003    of the first operand to the condition.  If TWO_OPERANDS_P is
9004    nonzero the comparison takes two operands; OPERANDS[3] will be the
9005    second operand.
9006
9007    If INVERTED_P is nonzero we are to branch if the condition does
9008    not hold.  If FLOAT_P is nonzero this is a floating-point comparison.
9009
9010    LENGTH is the length (in bytes) of the sequence we are to generate.
9011    That tells us whether to generate a simple conditional branch, or a
9012    reversed conditional branch around a `jr' instruction.  */
9013 const char *
9014 mips_output_conditional_branch (rtx insn, rtx *operands, int two_operands_p,
9015                                 int float_p, int inverted_p, int length)
9016 {
9017   static char buffer[200];
9018   /* The kind of comparison we are doing.  */
9019   enum rtx_code code = GET_CODE (operands[0]);
9020   /* Nonzero if the opcode for the comparison needs a `z' indicating
9021      that it is a comparison against zero.  */
9022   int need_z_p;
9023   /* A string to use in the assembly output to represent the first
9024      operand.  */
9025   const char *op1 = "%z2";
9026   /* A string to use in the assembly output to represent the second
9027      operand.  Use the hard-wired zero register if there's no second
9028      operand.  */
9029   const char *op2 = (two_operands_p ? ",%z3" : ",%.");
9030   /* The operand-printing string for the comparison.  */
9031   const char *const comp = (float_p ? "%F0" : "%C0");
9032   /* The operand-printing string for the inverted comparison.  */
9033   const char *const inverted_comp = (float_p ? "%W0" : "%N0");
9034
9035   /* The MIPS processors (for levels of the ISA at least two), have
9036      "likely" variants of each branch instruction.  These instructions
9037      annul the instruction in the delay slot if the branch is not
9038      taken.  */
9039   mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
9040
9041   if (!two_operands_p)
9042     {
9043       /* To compute whether than A > B, for example, we normally
9044          subtract B from A and then look at the sign bit.  But, if we
9045          are doing an unsigned comparison, and B is zero, we don't
9046          have to do the subtraction.  Instead, we can just check to
9047          see if A is nonzero.  Thus, we change the CODE here to
9048          reflect the simpler comparison operation.  */
9049       switch (code)
9050         {
9051         case GTU:
9052           code = NE;
9053           break;
9054
9055         case LEU:
9056           code = EQ;
9057           break;
9058
9059         case GEU:
9060           /* A condition which will always be true.  */
9061           code = EQ;
9062           op1 = "%.";
9063           break;
9064
9065         case LTU:
9066           /* A condition which will always be false.  */
9067           code = NE;
9068           op1 = "%.";
9069           break;
9070
9071         default:
9072           /* Not a special case.  */
9073           break;
9074         }
9075     }
9076
9077   /* Relative comparisons are always done against zero.  But
9078      equality comparisons are done between two operands, and therefore
9079      do not require a `z' in the assembly language output.  */
9080   need_z_p = (!float_p && code != EQ && code != NE);
9081   /* For comparisons against zero, the zero is not provided
9082      explicitly.  */
9083   if (need_z_p)
9084     op2 = "";
9085
9086   /* Begin by terminating the buffer.  That way we can always use
9087      strcat to add to it.  */
9088   buffer[0] = '\0';
9089
9090   switch (length)
9091     {
9092     case 4:
9093     case 8:
9094       /* Just a simple conditional branch.  */
9095       if (float_p)
9096         sprintf (buffer, "%%*b%s%%?\t%%Z2%%1%%/",
9097                  inverted_p ? inverted_comp : comp);
9098       else
9099         sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1%%/",
9100                  inverted_p ? inverted_comp : comp,
9101                  need_z_p ? "z" : "",
9102                  op1,
9103                  op2);
9104       return buffer;
9105
9106     case 12:
9107     case 16:
9108     case 24:
9109     case 28:
9110       {
9111         /* Generate a reversed conditional branch around ` j'
9112            instruction:
9113
9114                 .set noreorder
9115                 .set nomacro
9116                 bc    l
9117                 delay_slot or #nop
9118                 j     target
9119                 #nop
9120              l:
9121                 .set macro
9122                 .set reorder
9123
9124            If the original branch was a likely branch, the delay slot
9125            must be executed only if the branch is taken, so generate:
9126
9127                 .set noreorder
9128                 .set nomacro
9129                 bc    l
9130                 #nop
9131                 j     target
9132                 delay slot or #nop
9133              l:
9134                 .set macro
9135                 .set reorder
9136
9137            When generating non-embedded PIC, instead of:
9138
9139                 j     target
9140
9141            we emit:
9142
9143                 .set noat
9144                 la    $at, target
9145                 jr    $at
9146                 .set at
9147         */
9148
9149         rtx orig_target;
9150         rtx target = gen_label_rtx ();
9151
9152         orig_target = operands[1];
9153         operands[1] = target;
9154         /* Generate the reversed comparison.  This takes four
9155            bytes.  */
9156         if (float_p)
9157           sprintf (buffer, "%%*b%s\t%%Z2%%1",
9158                    inverted_p ? comp : inverted_comp);
9159         else
9160           sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
9161                    inverted_p ? comp : inverted_comp,
9162                    need_z_p ? "z" : "",
9163                    op1,
9164                    op2);
9165         output_asm_insn (buffer, operands);
9166
9167         if (length != 16 && length != 28 && ! mips_branch_likely)
9168           {
9169             /* Output delay slot instruction.  */
9170             rtx insn = final_sequence;
9171             final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9172                              optimize, 0, 1, NULL);
9173             INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9174           }
9175         else
9176           output_asm_insn ("%#", 0);
9177
9178         if (length <= 16)
9179           output_asm_insn ("j\t%0", &orig_target);
9180         else
9181           {
9182             output_asm_insn (mips_output_load_label (), &orig_target);
9183             output_asm_insn ("jr\t%@%]", 0);
9184           }
9185
9186         if (length != 16 && length != 28 && mips_branch_likely)
9187           {
9188             /* Output delay slot instruction.  */
9189             rtx insn = final_sequence;
9190             final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9191                              optimize, 0, 1, NULL);
9192             INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9193           }
9194         else
9195           output_asm_insn ("%#", 0);
9196
9197         (*targetm.asm_out.internal_label) (asm_out_file, "L",
9198                                    CODE_LABEL_NUMBER (target));
9199
9200         return "";
9201       }
9202
9203     default:
9204       abort ();
9205     }
9206
9207   /* NOTREACHED */
9208   return 0;
9209 }
9210 \f
9211 /* Used to output div or ddiv instruction DIVISION, which has the
9212    operands given by OPERANDS.  If we need a divide-by-zero check,
9213    output the instruction and return an asm string that traps if
9214    operand 2 is zero.
9215
9216    The original R4000 has a cpu bug.  If a double-word or a variable
9217    shift executes immediately after starting an integer division, the
9218    shift may give an incorrect result.  Avoid this by adding a nop on
9219    the R4000.  See quotations of errata #16 and #28 from "MIPS
9220    R4000PC/SC Errata, Processor Revision 2.2 and 3.0" in mips.md for
9221    details.
9222
9223    Otherwise just return DIVISION itself.  */
9224
9225 const char *
9226 mips_output_division (const char *division, rtx *operands)
9227 {
9228   const char *s = division;
9229
9230   if (TARGET_CHECK_ZERO_DIV)
9231     {
9232       output_asm_insn (s, operands);
9233
9234       if (TARGET_MIPS16)
9235         s = "bnez\t%2,1f\n\tbreak\t7\n1:";
9236       else
9237         s = "bne\t%2,%.,1f%#\n\tbreak\t7\n1:";
9238     }
9239   if (TARGET_FIX_R4000)
9240     {
9241       output_asm_insn (s, operands);
9242       s = "nop";
9243     }
9244   return s;
9245 }
9246 \f
9247 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
9248    with a final "000" replaced by "k".  Ignore case.
9249
9250    Note: this function is shared between GCC and GAS.  */
9251
9252 static bool
9253 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
9254 {
9255   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
9256     given++, canonical++;
9257
9258   return ((*given == 0 && *canonical == 0)
9259           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
9260 }
9261
9262
9263 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
9264    CPU name.  We've traditionally allowed a lot of variation here.
9265
9266    Note: this function is shared between GCC and GAS.  */
9267
9268 static bool
9269 mips_matching_cpu_name_p (const char *canonical, const char *given)
9270 {
9271   /* First see if the name matches exactly, or with a final "000"
9272      turned into "k".  */
9273   if (mips_strict_matching_cpu_name_p (canonical, given))
9274     return true;
9275
9276   /* If not, try comparing based on numerical designation alone.
9277      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
9278   if (TOLOWER (*given) == 'r')
9279     given++;
9280   if (!ISDIGIT (*given))
9281     return false;
9282
9283   /* Skip over some well-known prefixes in the canonical name,
9284      hoping to find a number there too.  */
9285   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
9286     canonical += 2;
9287   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
9288     canonical += 2;
9289   else if (TOLOWER (canonical[0]) == 'r')
9290     canonical += 1;
9291
9292   return mips_strict_matching_cpu_name_p (canonical, given);
9293 }
9294
9295
9296 /* Parse an option that takes the name of a processor as its argument.
9297    OPTION is the name of the option and CPU_STRING is the argument.
9298    Return the corresponding processor enumeration if the CPU_STRING is
9299    recognized, otherwise report an error and return null.
9300
9301    A similar function exists in GAS.  */
9302
9303 static const struct mips_cpu_info *
9304 mips_parse_cpu (const char *option, const char *cpu_string)
9305 {
9306   const struct mips_cpu_info *p;
9307   const char *s;
9308
9309   /* In the past, we allowed upper-case CPU names, but it doesn't
9310      work well with the multilib machinery.  */
9311   for (s = cpu_string; *s != 0; s++)
9312     if (ISUPPER (*s))
9313       {
9314         warning ("the cpu name must be lower case");
9315         break;
9316       }
9317
9318   /* 'from-abi' selects the most compatible architecture for the given
9319      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
9320      EABIs, we have to decide whether we're using the 32-bit or 64-bit
9321      version.  Look first at the -mgp options, if given, otherwise base
9322      the choice on MASK_64BIT in TARGET_DEFAULT.  */
9323   if (strcasecmp (cpu_string, "from-abi") == 0)
9324     return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
9325                                    : ABI_NEEDS_64BIT_REGS ? 3
9326                                    : (TARGET_64BIT ? 3 : 1));
9327
9328   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
9329   if (strcasecmp (cpu_string, "default") == 0)
9330     return 0;
9331
9332   for (p = mips_cpu_info_table; p->name != 0; p++)
9333     if (mips_matching_cpu_name_p (p->name, cpu_string))
9334       return p;
9335
9336   error ("bad value (%s) for %s", cpu_string, option);
9337   return 0;
9338 }
9339
9340
9341 /* Return the processor associated with the given ISA level, or null
9342    if the ISA isn't valid.  */
9343
9344 static const struct mips_cpu_info *
9345 mips_cpu_info_from_isa (int isa)
9346 {
9347   const struct mips_cpu_info *p;
9348
9349   for (p = mips_cpu_info_table; p->name != 0; p++)
9350     if (p->isa == isa)
9351       return p;
9352
9353   return 0;
9354 }
9355 \f
9356 /* Adjust the cost of INSN based on the relationship between INSN that
9357    is dependent on DEP_INSN through the dependence LINK.  The default
9358    is to make no adjustment to COST.
9359
9360    On the MIPS, ignore the cost of anti- and output-dependencies.  */
9361 static int
9362 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
9363                   rtx dep ATTRIBUTE_UNUSED, int cost)
9364 {
9365   if (REG_NOTE_KIND (link) != 0)
9366     return 0;   /* Anti or output dependence.  */
9367   return cost;
9368 }
9369
9370 /* Implement HARD_REGNO_NREGS.  The size of FP registers are controlled
9371    by UNITS_PER_FPREG.  All other registers are word sized.  */
9372
9373 unsigned int
9374 mips_hard_regno_nregs (int regno, enum machine_mode mode)
9375 {
9376   if (! FP_REG_P (regno))
9377     return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
9378   else
9379     return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
9380 }
9381
9382 /* Implement TARGET_RETURN_IN_MEMORY.  Under the old (i.e., 32 and O64 ABIs)
9383    all BLKmode objects are returned in memory.  Under the new (N32 and
9384    64-bit MIPS ABIs) small structures are returned in a register.
9385    Objects with varying size must still be returned in memory, of
9386    course.  */
9387
9388 static bool
9389 mips_return_in_memory (tree type, tree fndecl ATTRIBUTE_UNUSED)
9390 {
9391   if (TARGET_OLDABI)
9392     return (TYPE_MODE (type) == BLKmode);
9393   else
9394     return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
9395             || (int_size_in_bytes (type) == -1));
9396 }
9397
9398 static bool
9399 mips_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
9400 {
9401   return !TARGET_OLDABI;
9402 }
9403
9404 static int
9405 mips_issue_rate (void)
9406 {
9407   switch (mips_tune)
9408     {
9409     case PROCESSOR_R5400:
9410     case PROCESSOR_R5500:
9411     case PROCESSOR_R7000:
9412     case PROCESSOR_R9000:
9413       return 2;
9414
9415     default:
9416       return 1;
9417     }
9418
9419   abort ();
9420
9421 }
9422
9423 /* Implements TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE.  Return true for
9424    processors that have a DFA pipeline description.  */
9425
9426 static int
9427 mips_use_dfa_pipeline_interface (void)
9428 {
9429   switch (mips_tune)
9430     {
9431     case PROCESSOR_R5400:
9432     case PROCESSOR_R5500:
9433     case PROCESSOR_R7000:
9434     case PROCESSOR_R9000:
9435     case PROCESSOR_SR71000:
9436       return true;
9437
9438     default:
9439       return false;
9440     }
9441 }
9442
9443
9444 const char *
9445 mips_emit_prefetch (rtx *operands)
9446 {
9447   int write = INTVAL (operands[1]);
9448   int locality = INTVAL (operands[2]);
9449   int indexed = GET_CODE (operands[3]) == REG;
9450   int code;
9451   char buffer[30];
9452   
9453   if (locality <= 0)
9454     code = (write ? 5 : 4);     /* store_streamed / load_streamed.  */
9455   else if (locality <= 2)
9456     code = (write ? 1 : 0);     /* store / load.  */
9457   else
9458     code = (write ? 7 : 6);     /* store_retained / load_retained.  */
9459
9460   sprintf (buffer, "%s\t%d,%%3(%%0)", indexed ? "prefx" : "pref", code);
9461   output_asm_insn (buffer, operands);
9462   return "";
9463 }
9464
9465
9466 \f
9467 #if TARGET_IRIX
9468 /* Output assembly to switch to section NAME with attribute FLAGS.  */
9469
9470 static void
9471 irix_asm_named_section_1 (const char *name, unsigned int flags,
9472                            unsigned int align)
9473 {
9474   unsigned int sh_type, sh_flags, sh_entsize;
9475
9476   sh_flags = 0;
9477   if (!(flags & SECTION_DEBUG))
9478     sh_flags |= 2; /* SHF_ALLOC */
9479   if (flags & SECTION_WRITE)
9480     sh_flags |= 1; /* SHF_WRITE */
9481   if (flags & SECTION_CODE)
9482     sh_flags |= 4; /* SHF_EXECINSTR */
9483   if (flags & SECTION_SMALL)
9484     sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
9485   if (strcmp (name, ".debug_frame") == 0)
9486     sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
9487   if (flags & SECTION_DEBUG)
9488     sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
9489   else if (flags & SECTION_BSS)
9490     sh_type = 8; /* SHT_NOBITS */
9491   else
9492     sh_type = 1; /* SHT_PROGBITS */
9493
9494   if (flags & SECTION_CODE)
9495     sh_entsize = 4;
9496   else
9497     sh_entsize = 0;
9498
9499   fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
9500            name, sh_type, sh_flags, sh_entsize, align);
9501 }
9502
9503 static void
9504 irix_asm_named_section (const char *name, unsigned int flags)
9505 {
9506   if (TARGET_SGI_O32_AS)
9507     default_no_named_section (name, flags);
9508   else if (mips_abi == ABI_32 && TARGET_GAS)
9509     default_elf_asm_named_section (name, flags);
9510   else
9511     irix_asm_named_section_1 (name, flags, 0);
9512 }
9513
9514 /* In addition to emitting a .align directive, record the maximum
9515    alignment requested for the current section.  */
9516
9517 struct GTY (()) irix_section_align_entry
9518 {
9519   const char *name;
9520   unsigned int log;
9521   unsigned int flags;
9522 };
9523
9524 static htab_t irix_section_align_htab;
9525 static FILE *irix_orig_asm_out_file;
9526
9527 static int
9528 irix_section_align_entry_eq (const void *p1, const void *p2)
9529 {
9530   const struct irix_section_align_entry *old = p1;
9531   const char *new = p2;
9532
9533   return strcmp (old->name, new) == 0;
9534 }
9535
9536 static hashval_t
9537 irix_section_align_entry_hash (const void *p)
9538 {
9539   const struct irix_section_align_entry *old = p;
9540   return htab_hash_string (old->name);
9541 }
9542
9543 void
9544 irix_asm_output_align (FILE *file, unsigned int log)
9545 {
9546   const char *section = current_section_name ();
9547   struct irix_section_align_entry **slot, *entry;
9548
9549   if (mips_abi != ABI_32)
9550     {
9551       if (! section)
9552         abort ();
9553
9554       slot = (struct irix_section_align_entry **)
9555         htab_find_slot_with_hash (irix_section_align_htab, section,
9556                                   htab_hash_string (section), INSERT);
9557       entry = *slot;
9558       if (! entry)
9559         {
9560           entry = (struct irix_section_align_entry *)
9561             xmalloc (sizeof (struct irix_section_align_entry));
9562           *slot = entry;
9563           entry->name = section;
9564           entry->log = log;
9565           entry->flags = current_section_flags ();
9566         }
9567       else if (entry->log < log)
9568         entry->log = log;
9569     }
9570
9571   fprintf (file, "\t.align\t%u\n", log);
9572 }
9573
9574 /* The IRIX assembler does not record alignment from .align directives,
9575    but takes it from the first .section directive seen.  Play file
9576    switching games so that we can emit a .section directive at the
9577    beginning of the file with the proper alignment attached.  */
9578
9579 static void
9580 irix_file_start (void)
9581 {
9582   mips_file_start ();
9583
9584   if (mips_abi == ABI_32)
9585     return;
9586
9587   irix_orig_asm_out_file = asm_out_file;
9588   asm_out_file = tmpfile ();
9589
9590   irix_section_align_htab = htab_create (31, irix_section_align_entry_hash,
9591                                          irix_section_align_entry_eq, NULL);
9592 }
9593
9594 static int
9595 irix_section_align_1 (void **slot, void *data ATTRIBUTE_UNUSED)
9596 {
9597   const struct irix_section_align_entry *entry
9598     = *(const struct irix_section_align_entry **) slot;
9599
9600   irix_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
9601   return 1;
9602 }
9603
9604 static void
9605 copy_file_data (FILE *to, FILE *from)
9606 {
9607   char buffer[8192];
9608   size_t len;
9609   rewind (from);
9610   if (ferror (from))
9611     fatal_error ("can't rewind temp file: %m");
9612
9613   while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
9614     if (fwrite (buffer, 1, len, to) != len)
9615       fatal_error ("can't write to output file: %m");
9616
9617   if (ferror (from))
9618     fatal_error ("can't read from temp file: %m");
9619
9620   if (fclose (from))
9621     fatal_error ("can't close temp file: %m");
9622 }
9623
9624 static void
9625 irix_file_end (void)
9626 {
9627   if (mips_abi != ABI_32)
9628     {
9629       /* Emit section directives with the proper alignment at the top of the
9630          real output file.  */
9631       FILE *temp = asm_out_file;
9632       asm_out_file = irix_orig_asm_out_file;
9633       htab_traverse (irix_section_align_htab, irix_section_align_1, NULL);
9634
9635       /* Copy the data emitted to the temp file to the real output file.  */
9636       copy_file_data (asm_out_file, temp);
9637     }
9638
9639   mips_file_end ();
9640 }
9641
9642
9643 /* Implement TARGET_SECTION_TYPE_FLAGS.  Make sure that .sdata and
9644    .sbss sections get the SECTION_SMALL flag: this isn't set by the
9645    default code.  */
9646
9647 static unsigned int
9648 irix_section_type_flags (tree decl, const char *section, int relocs_p)
9649 {
9650   unsigned int flags;
9651
9652   flags = default_section_type_flags (decl, section, relocs_p);
9653
9654   if (strcmp (section, ".sdata") == 0
9655       || strcmp (section, ".sbss") == 0
9656       || strncmp (section, ".gnu.linkonce.s.", 16) == 0
9657       || strncmp (section, ".gnu.linkonce.sb.", 17) == 0)
9658     flags |= SECTION_SMALL;
9659
9660   return flags;
9661 }
9662
9663 #endif /* TARGET_IRIX */
9664
9665 #include "gt-mips.h"