OSDN Git Service

* c-aux-info.c (concat): Don't define.
[pf3gnuchains/gcc-fork.git] / gcc / config / mips / mips.c
1 /* Subroutines for insn-output.c for MIPS
2    Copyright (C) 1989, 90, 91, 93-98, 1999 Free Software Foundation, Inc.
3    Contributed by A. Lichnewsky, lich@inria.inria.fr.
4    Changes by Michael Meissner, meissner@osf.org.
5    64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6    Brendan Eich, brendan@microunity.com.
7
8 This file is part of GNU CC.
9
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING.  If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 /* ??? The TARGET_FP_CALL_32 macros are intended to simulate a 32 bit
26    calling convention in 64 bit mode.  It doesn't work though, and should
27    be replaced with something better designed.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include <signal.h>
32
33 #include "rtl.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "real.h"
37 #include "insn-config.h"
38 #include "conditions.h"
39 #include "insn-flags.h"
40 #include "insn-attr.h"
41 #include "insn-codes.h"
42 #include "recog.h"
43 #include "toplev.h"
44 #include "tree.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "flags.h"
48 #include "reload.h"
49 #include "output.h"
50
51 #if defined(USG) || !defined(HAVE_STAB_H)
52 #include "gstab.h"  /* If doing DBX on sysV, use our own stab.h.  */
53 #else
54 #include <stab.h>  /* On BSD, use the system's stab.h.  */
55 #endif /* not USG */
56
57 #ifdef __GNU_STAB__
58 #define STAB_CODE_TYPE enum __stab_debug_code
59 #else
60 #define STAB_CODE_TYPE int
61 #endif
62
63 extern char  *mktemp ();
64 extern tree   lookup_name ();
65
66 /* Enumeration for all of the relational tests, so that we can build
67    arrays indexed by the test type, and not worry about the order
68    of EQ, NE, etc. */
69
70 enum internal_test {
71     ITEST_EQ,
72     ITEST_NE,
73     ITEST_GT,
74     ITEST_GE,
75     ITEST_LT,
76     ITEST_LE,
77     ITEST_GTU,
78     ITEST_GEU,
79     ITEST_LTU,
80     ITEST_LEU,
81     ITEST_MAX
82   };
83
84
85 struct constant;
86 static enum internal_test map_test_to_internal_test     PROTO ((enum rtx_code));
87 static int mips16_simple_memory_operand         PROTO ((rtx, rtx,
88                                                         enum machine_mode));
89 static int m16_check_op                         PROTO ((rtx, int, int, int));
90 static void block_move_loop                     PROTO ((rtx, rtx, int, int,
91                                                         rtx, rtx));
92 static void block_move_call                     PROTO ((rtx, rtx, rtx));
93 static FILE *make_temp_file                     PROTO ((void));
94 static void save_restore_insns                  PROTO ((int, rtx,
95                                                         long, FILE *));
96 static void mips16_output_gp_offset             PROTO ((FILE *, rtx));
97 static void mips16_fp_args                      PROTO ((FILE *, int, int));
98 static void build_mips16_function_stub          PROTO ((FILE *));
99 static void mips16_optimize_gp                  PROTO ((rtx));
100 static rtx add_constant                         PROTO ((struct constant **,
101                                                         rtx,
102                                                         enum machine_mode));
103 static void dump_constants                      PROTO ((struct constant *,
104                                                         rtx));
105 static rtx mips_find_symbol                     PROTO ((rtx));
106 static void abort_with_insn                     PROTO ((rtx, const char *))
107   ATTRIBUTE_NORETURN;
108 static int symbolic_expression_p                PROTO ((rtx));
109
110 /* Global variables for machine-dependent things.  */
111
112 /* Threshold for data being put into the small data/bss area, instead
113    of the normal data area (references to the small data/bss area take
114    1 instruction, and use the global pointer, references to the normal
115    data area takes 2 instructions).  */
116 int mips_section_threshold = -1;
117
118 /* Count the number of .file directives, so that .loc is up to date.  */
119 int num_source_filenames = 0;
120
121 /* Count the number of sdb related labels are generated (to find block
122    start and end boundaries).  */
123 int sdb_label_count = 0;
124
125 /* Next label # for each statement for Silicon Graphics IRIS systems. */
126 int sym_lineno = 0;
127
128 /* Non-zero if inside of a function, because the stupid MIPS asm can't
129    handle .files inside of functions.  */
130 int inside_function = 0;
131
132 /* Files to separate the text and the data output, so that all of the data
133    can be emitted before the text, which will mean that the assembler will
134    generate smaller code, based on the global pointer.  */
135 FILE *asm_out_data_file;
136 FILE *asm_out_text_file;
137
138 /* Linked list of all externals that are to be emitted when optimizing
139    for the global pointer if they haven't been declared by the end of
140    the program with an appropriate .comm or initialization.  */
141
142 struct extern_list
143 {
144   struct extern_list *next;     /* next external */
145   char *name;                   /* name of the external */
146   int size;                     /* size in bytes */
147 } *extern_head = 0;
148
149 /* Name of the file containing the current function.  */
150 const char *current_function_file = "";
151
152 /* Warning given that Mips ECOFF can't support changing files
153    within a function.  */
154 int file_in_function_warning = FALSE;
155
156 /* Whether to suppress issuing .loc's because the user attempted
157    to change the filename within a function.  */
158 int ignore_line_number = FALSE;
159
160 /* Number of nested .set noreorder, noat, nomacro, and volatile requests.  */
161 int set_noreorder;
162 int set_noat;
163 int set_nomacro;
164 int set_volatile;
165
166 /* The next branch instruction is a branch likely, not branch normal.  */
167 int mips_branch_likely;
168
169 /* Count of delay slots and how many are filled.  */
170 int dslots_load_total;
171 int dslots_load_filled;
172 int dslots_jump_total;
173 int dslots_jump_filled;
174
175 /* # of nops needed by previous insn */
176 int dslots_number_nops;
177
178 /* Number of 1/2/3 word references to data items (ie, not jal's).  */
179 int num_refs[3];
180
181 /* registers to check for load delay */
182 rtx mips_load_reg, mips_load_reg2, mips_load_reg3, mips_load_reg4;
183
184 /* Cached operands, and operator to compare for use in set/branch on
185    condition codes.  */
186 rtx branch_cmp[2];
187
188 /* what type of branch to use */
189 enum cmp_type branch_type;
190
191 /* Number of previously seen half-pic pointers and references.  */
192 static int prev_half_pic_ptrs = 0;
193 static int prev_half_pic_refs = 0;
194
195 /* which cpu are we scheduling for */
196 enum processor_type mips_cpu;
197
198 /* which instruction set architecture to use.  */
199 int mips_isa;
200
201 #ifdef MIPS_ABI_DEFAULT
202 /* Which ABI to use.  This is defined to a constant in mips.h if the target
203    doesn't support multiple ABIs.  */
204 int mips_abi;
205 #endif
206
207 /* Strings to hold which cpu and instruction set architecture to use.  */
208 const char *mips_cpu_string;    /* for -mcpu=<xxx> */
209 const char *mips_isa_string;    /* for -mips{1,2,3,4} */
210 const char *mips_abi_string;    /* for -mabi={32,n32,64,eabi} */
211
212 /* Whether we are generating mips16 code.  This is a synonym for
213    TARGET_MIPS16, and exists for use as an attribute.  */
214 int mips16;
215
216 /* This variable is set by -mno-mips16.  We only care whether
217    -mno-mips16 appears or not, and using a string in this fashion is
218    just a way to avoid using up another bit in target_flags.  */
219 const char *mips_no_mips16_string;
220
221 /* This is only used to determine if an type size setting option was 
222    explicitly specified (-mlong64, -mint64, -mlong32).  The specs
223    set this option if such an option is used. */
224 const char *mips_explicit_type_size_string;
225
226 /* Whether we are generating mips16 hard float code.  In mips16 mode
227    we always set TARGET_SOFT_FLOAT; this variable is nonzero if
228    -msoft-float was not specified by the user, which means that we
229    should arrange to call mips32 hard floating point code.  */
230 int mips16_hard_float;
231
232 /* This variable is set by -mentry.  We only care whether -mentry
233    appears or not, and using a string in this fashion is just a way to
234    avoid using up another bit in target_flags.  */
235 const char *mips_entry_string;
236
237 /* Whether we should entry and exit pseudo-ops in mips16 mode.  */
238 int mips_entry;
239
240 /* If TRUE, we split addresses into their high and low parts in the RTL.  */
241 int mips_split_addresses;
242
243 /* Generating calls to position independent functions?  */
244 enum mips_abicalls_type mips_abicalls;
245
246 /* High and low marks for floating point values which we will accept
247    as legitimate constants for LEGITIMATE_CONSTANT_P.  These are
248    initialized in override_options.  */
249 REAL_VALUE_TYPE dfhigh, dflow, sfhigh, sflow;
250
251 /* Mode used for saving/restoring general purpose registers.  */
252 static enum machine_mode gpr_mode;
253
254 /* Array giving truth value on whether or not a given hard register
255    can support a given mode.  */
256 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
257
258 /* Current frame information calculated by compute_frame_size.  */
259 struct mips_frame_info current_frame_info;
260
261 /* Zero structure to initialize current_frame_info.  */
262 struct mips_frame_info zero_frame_info;
263
264 /* Temporary filename used to buffer .text until end of program
265    for -mgpopt.  */
266 static char *temp_filename;
267
268 /* Pseudo-reg holding the address of the current function when
269    generating embedded PIC code.  Created by LEGITIMIZE_ADDRESS, used
270    by mips_finalize_pic if it was created.  */
271 rtx embedded_pic_fnaddr_rtx;
272
273 /* The length of all strings seen when compiling for the mips16.  This
274    is used to tell how many strings are in the constant pool, so that
275    we can see if we may have an overflow.  This is reset each time the
276    constant pool is output.  */
277 int mips_string_length;
278
279 /* Pseudo-reg holding the value of $28 in a mips16 function which
280    refers to GP relative global variables.  */
281 rtx mips16_gp_pseudo_rtx;
282
283 /* In mips16 mode, we build a list of all the string constants we see
284    in a particular function.  */
285
286 struct string_constant
287 {
288   struct string_constant *next;
289   char *label;
290 };
291
292 static struct string_constant *string_constants;
293
294 /* List of all MIPS punctuation characters used by print_operand.  */
295 char mips_print_operand_punct[256];
296
297 /* Map GCC register number to debugger register number.  */
298 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
299
300 /* Buffer to use to enclose a load/store operation with %{ %} to
301    turn on .set volatile.  */
302 static char volatile_buffer[60];
303
304 /* Hardware names for the registers.  If -mrnames is used, this
305    will be overwritten with mips_sw_reg_names.  */
306
307 char mips_reg_names[][8] =
308 {
309  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
310  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
311  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
312  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
313  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
314  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
315  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
316  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
317  "hi",   "lo",   "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
318  "$fcc5","$fcc6","$fcc7","$rap"
319 };
320
321 /* Mips software names for the registers, used to overwrite the
322    mips_reg_names array.  */
323
324 char mips_sw_reg_names[][8] =
325 {
326   "$zero","$at",  "$v0",  "$v1",  "$a0",  "$a1",  "$a2",  "$a3",
327   "$t0",  "$t1",  "$t2",  "$t3",  "$t4",  "$t5",  "$t6",  "$t7",
328   "$s0",  "$s1",  "$s2",  "$s3",  "$s4",  "$s5",  "$s6",  "$s7",
329   "$t8",  "$t9",  "$k0",  "$k1",  "$gp",  "$sp",  "$fp",  "$ra",
330   "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
331   "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
332   "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
333   "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
334   "hi",   "lo",   "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
335   "$fcc5","$fcc6","$fcc7","$rap"
336 };
337
338 /* Map hard register number to register class */
339 enum reg_class mips_regno_to_class[] =
340 {
341   GR_REGS,      GR_REGS,        M16_NA_REGS,    M16_NA_REGS,
342   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
343   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
344   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
345   M16_NA_REGS,  M16_NA_REGS,    GR_REGS,        GR_REGS,
346   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
347   T_REG,        GR_REGS,        GR_REGS,        GR_REGS,
348   GR_REGS,      GR_REGS,        GR_REGS,        GR_REGS,
349   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
350   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
351   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
352   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
353   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
354   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
355   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
356   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
357   HI_REG,       LO_REG,         HILO_REG,       ST_REGS,
358   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
359   ST_REGS,      ST_REGS,        ST_REGS,        GR_REGS
360 };
361
362 /* Map register constraint character to register class.  */
363 enum reg_class mips_char_to_class[256] =
364 {
365   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
366   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
367   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
368   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
369   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
370   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
371   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
372   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
373   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
374   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
375   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
376   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
377   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
378   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
379   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
380   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
381   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
382   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
383   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
384   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
385   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
386   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
387   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
388   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
389   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
390   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
391   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
392   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
393   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
394   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
395   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
396   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
397   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
398   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
399   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
400   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
401   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
402   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
403   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
404   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
405   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
406   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
407   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
408   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
409   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
410   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
411   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
412   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
413   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
414   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
415   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
416   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
417   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
418   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
419   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
420   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
421   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
422   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
423   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
424   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
425   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
426   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
427   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
428   NO_REGS,      NO_REGS,        NO_REGS,        NO_REGS,
429 };
430
431 \f
432 /* Return truth value of whether OP can be used as an operands
433    where a register or 16 bit unsigned integer is needed.  */
434
435 int
436 uns_arith_operand (op, mode)
437      rtx op;
438      enum machine_mode mode;
439 {
440   if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
441     return 1;
442
443   return register_operand (op, mode);
444 }
445
446 /* Return truth value of whether OP can be used as an operands
447    where a 16 bit integer is needed  */
448
449 int
450 arith_operand (op, mode)
451      rtx op;
452      enum machine_mode mode;
453 {
454   if (GET_CODE (op) == CONST_INT && SMALL_INT (op))
455     return 1;
456
457   /* On the mips16, a GP relative value is a signed 16 bit offset.  */
458   if (TARGET_MIPS16 && GET_CODE (op) == CONST && mips16_gp_offset_p (op))
459     return 1;
460
461   return register_operand (op, mode);
462 }
463
464 /* Return truth value of whether OP can be used as an operand in a two
465    address arithmetic insn (such as set 123456,%o4) of mode MODE.  */
466
467 int
468 arith32_operand (op, mode)
469      rtx op;
470      enum machine_mode mode;
471 {
472   if (GET_CODE (op) == CONST_INT)
473     return 1;
474
475   return register_operand (op, mode);
476 }
477
478 /* Return truth value of whether OP is a integer which fits in 16 bits  */
479
480 int
481 small_int (op, mode)
482      rtx op;
483      enum machine_mode mode ATTRIBUTE_UNUSED;
484 {
485   return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
486 }
487
488 /* Return truth value of whether OP is a 32 bit integer which is too big to
489    be loaded with one instruction.  */
490
491 int
492 large_int (op, mode)
493      rtx op;
494      enum machine_mode mode ATTRIBUTE_UNUSED;
495 {
496   HOST_WIDE_INT value;
497
498   if (GET_CODE (op) != CONST_INT)
499     return 0;
500
501   value = INTVAL (op);
502
503   /* ior reg,$r0,value */
504   if ((value & ~ ((HOST_WIDE_INT) 0x0000ffff)) == 0)
505     return 0;
506
507   /* subu reg,$r0,value */
508   if (((unsigned HOST_WIDE_INT) (value + 32768)) <= 32767)
509     return 0;
510
511   /* lui reg,value>>16 */
512   if ((value & 0x0000ffff) == 0)
513     return 0;
514
515   return 1;
516 }
517
518 /* Return truth value of whether OP is a register or the constant 0.
519    In mips16 mode, we only accept a register, since the mips16 does
520    not have $0.  */
521
522 int
523 reg_or_0_operand (op, mode)
524      rtx op;
525      enum machine_mode mode;
526 {
527   switch (GET_CODE (op))
528     {
529     case CONST_INT:
530       if (TARGET_MIPS16)
531         return 0;
532       return INTVAL (op) == 0;
533
534     case CONST_DOUBLE:
535       if (TARGET_MIPS16)
536         return 0;
537       return op == CONST0_RTX (mode);
538
539     case REG:
540     case SUBREG:
541       return register_operand (op, mode);
542
543     default:
544       break;
545     }
546
547   return 0;
548 }
549
550 /* Return truth value of whether OP is a register or the constant 0,
551    even in mips16 mode. */
552
553 int
554 true_reg_or_0_operand (op, mode)
555      rtx op;
556      enum machine_mode mode;
557 {
558   switch (GET_CODE (op))
559     {
560     case CONST_INT:
561       return INTVAL (op) == 0;
562
563     case CONST_DOUBLE:
564       return op == CONST0_RTX (mode);
565
566     case REG:
567     case SUBREG:
568       return register_operand (op, mode);
569
570     default:
571       break;
572     }
573
574   return 0;
575 }
576
577 /* Return truth value if a CONST_DOUBLE is ok to be a legitimate constant.  */
578
579 int
580 mips_const_double_ok (op, mode)
581      rtx op;
582      enum machine_mode mode;
583 {
584   REAL_VALUE_TYPE d;
585
586   if (GET_CODE (op) != CONST_DOUBLE)
587     return 0;
588
589   if (mode == VOIDmode)
590     return 1;
591
592   if (mode != SFmode && mode != DFmode)
593     return 0;
594
595   if (op == CONST0_RTX (mode))
596     return 1;
597
598   /* ??? li.s does not work right with SGI's Irix 6 assembler.  */
599   if (mips_abi != ABI_32 && mips_abi != ABI_O64 && mips_abi != ABI_EABI)
600     return 0;
601
602   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
603
604   if (REAL_VALUE_ISNAN (d))
605     return FALSE;
606
607   if (REAL_VALUE_NEGATIVE (d))
608     d = REAL_VALUE_NEGATE (d);
609
610   if (mode == DFmode)
611     {
612       if (REAL_VALUES_LESS (d, dfhigh)
613           && REAL_VALUES_LESS (dflow, d))
614         return 1;
615     }
616   else
617     {
618       if (REAL_VALUES_LESS (d, sfhigh)
619           && REAL_VALUES_LESS (sflow, d))
620         return 1;
621     }
622
623   return 0;
624 }
625
626 /* Accept the floating point constant 1 in the appropriate mode.  */
627
628 int
629 const_float_1_operand (op, mode)
630      rtx op;
631      enum machine_mode mode;
632 {
633   REAL_VALUE_TYPE d;
634   static REAL_VALUE_TYPE onedf;
635   static REAL_VALUE_TYPE onesf;
636   static int one_initialized;
637
638   if (GET_CODE (op) != CONST_DOUBLE
639       || mode != GET_MODE (op)
640       || (mode != DFmode && mode != SFmode))
641     return 0;
642
643   REAL_VALUE_FROM_CONST_DOUBLE (d, op);
644
645   /* We only initialize these values if we need them, since we will
646      never get called unless mips_isa >= 4.  */
647   if (! one_initialized)
648     {
649       onedf = REAL_VALUE_ATOF ("1.0", DFmode);
650       onesf = REAL_VALUE_ATOF ("1.0", SFmode);
651       one_initialized = 1;
652     }
653
654   if (mode == DFmode)
655     return REAL_VALUES_EQUAL (d, onedf);
656   else
657     return REAL_VALUES_EQUAL (d, onesf);
658 }
659
660 /* Return true if a memory load or store of REG plus OFFSET in MODE
661    can be represented in a single word on the mips16.  */
662
663 static int
664 mips16_simple_memory_operand (reg, offset, mode)
665      rtx reg;
666      rtx offset;
667      enum machine_mode mode;
668 {
669   int size, off;
670
671   if (mode == BLKmode)
672     {
673       /* We can't tell, because we don't know how the value will
674          eventually be accessed.  Returning 0 here does no great
675          harm; it just prevents some possible instruction scheduling.  */
676       return 0;
677     }
678
679   size = GET_MODE_SIZE (mode);
680
681   if (INTVAL (offset) % size != 0)
682     return 0;
683   if (REGNO (reg) == STACK_POINTER_REGNUM && GET_MODE_SIZE (mode) == 4)
684     off = 0x100;
685   else
686     off = 0x20;
687   if (INTVAL (offset) >= 0 && INTVAL (offset) < off * size)
688     return 1;
689   return 0;
690 }
691
692 /* Return truth value if a memory operand fits in a single instruction
693    (ie, register + small offset).  */
694
695 int
696 simple_memory_operand (op, mode)
697      rtx op;
698      enum machine_mode mode;
699 {
700   rtx addr, plus0, plus1;
701
702   /* Eliminate non-memory operations */
703   if (GET_CODE (op) != MEM)
704     return 0;
705
706   /* dword operations really put out 2 instructions, so eliminate them.  */
707   /* ??? This isn't strictly correct.  It is OK to accept multiword modes
708      here, since the length attributes are being set correctly, but only
709      if the address is offsettable.  LO_SUM is not offsettable.  */
710   if (GET_MODE_SIZE (GET_MODE (op)) > UNITS_PER_WORD)
711     return 0;
712
713   /* Decode the address now.  */
714   addr = XEXP (op, 0);
715   switch (GET_CODE (addr))
716     {
717     case REG:
718     case LO_SUM:
719       return 1;
720
721     case CONST_INT:
722       if (TARGET_MIPS16)
723         return 0;
724       return SMALL_INT (op);
725
726     case PLUS:
727       plus0 = XEXP (addr, 0);
728       plus1 = XEXP (addr, 1);
729       if (GET_CODE (plus0) == REG
730           && GET_CODE (plus1) == CONST_INT && SMALL_INT (plus1)
731           && (! TARGET_MIPS16
732               || mips16_simple_memory_operand (plus0, plus1, mode)))
733         return 1;
734
735       else if (GET_CODE (plus1) == REG
736                && GET_CODE (plus0) == CONST_INT && SMALL_INT (plus0)
737                && (! TARGET_MIPS16
738                    || mips16_simple_memory_operand (plus1, plus0, mode)))
739         return 1;
740
741       else
742         return 0;
743
744 #if 0
745       /* We used to allow small symbol refs here (ie, stuff in .sdata
746          or .sbss), but this causes some bugs in G++.  Also, it won't
747          interfere if the MIPS linker rewrites the store instruction
748          because the function is PIC.  */
749
750     case LABEL_REF:             /* never gp relative */
751       break;
752
753     case CONST:
754       /* If -G 0, we can never have a GP relative memory operation.
755          Also, save some time if not optimizing.  */
756       if (!TARGET_GP_OPT)
757         return 0;
758
759       {
760         rtx offset = const0_rtx;
761         addr = eliminate_constant_term (XEXP (addr, 0), &offset);
762         if (GET_CODE (op) != SYMBOL_REF)
763           return 0;
764
765         /* let's be paranoid.... */
766         if (! SMALL_INT (offset))
767           return 0;
768       }
769
770       /* fall through */
771
772     case SYMBOL_REF:
773       return SYMBOL_REF_FLAG (addr);
774 #endif
775
776       /* This SYMBOL_REF case is for the mips16.  If the above case is
777          reenabled, this one should be merged in.  */
778     case SYMBOL_REF:
779       /* References to the constant pool on the mips16 use a small
780          offset if the function is small.  The only time we care about
781          getting this right is during delayed branch scheduling, so
782          don't need to check until then.  The machine_dependent_reorg
783          function will set the total length of the instructions used
784          in the function in current_frame_info.  If that is small
785          enough, we know for sure that this is a small offset.  It
786          would be better if we could take into account the location of
787          the instruction within the function, but we can't, because we
788          don't know where we are.  */
789       if (TARGET_MIPS16
790           && CONSTANT_POOL_ADDRESS_P (addr)
791           && current_frame_info.insns_len > 0)
792         {
793           long size;
794
795           size = current_frame_info.insns_len + get_pool_size ();
796           if (GET_MODE_SIZE (mode) == 4)
797             return size < 4 * 0x100;
798           else if (GET_MODE_SIZE (mode) == 8)
799             return size < 8 * 0x20;
800           else
801             return 0;
802         }
803
804       return 0;
805
806     default:
807       break;
808     }
809
810   return 0;
811 }
812
813 /* Return nonzero for a memory address that can be used to load or store
814    a doubleword.  */
815
816 int
817 double_memory_operand (op, mode)
818      rtx op;
819      enum machine_mode mode;
820 {
821   rtx addr;
822
823   if (GET_CODE (op) != MEM
824       || ! memory_operand (op, mode))
825     {
826       /* During reload, we accept a pseudo register if it has an
827          appropriate memory address.  If we don't do this, we will
828          wind up reloading into a register, and then reloading that
829          register from memory, when we could just reload directly from
830          memory.  */
831       if (reload_in_progress
832           && GET_CODE (op) == REG
833           && REGNO (op) >= FIRST_PSEUDO_REGISTER
834           && reg_renumber[REGNO (op)] < 0
835           && reg_equiv_mem[REGNO (op)] != 0
836           && double_memory_operand (reg_equiv_mem[REGNO (op)], mode))
837         return 1;
838
839       /* All reloaded addresses are valid in TARGET_64BIT mode.  This is
840          the same test performed for 'm' in find_reloads.  */
841
842       if (reload_in_progress
843           && TARGET_64BIT
844           && (GET_CODE (op) == MEM
845               || (GET_CODE (op) == REG
846                   && REGNO (op) >= FIRST_PSEUDO_REGISTER
847                   && reg_renumber[REGNO (op)] < 0)))
848         return 1;
849
850       if (reload_in_progress
851           && TARGET_MIPS16
852           && GET_CODE (op) == MEM)
853         {
854           rtx addr;
855
856           addr = XEXP (op, 0);
857
858           /* During reload on the mips16, we accept a large offset
859              from the frame pointer or the stack pointer.  This large
860              address will get reloaded anyhow.  */
861           if (GET_CODE (addr) == PLUS
862               && GET_CODE (XEXP (addr, 0)) == REG
863               && (REGNO (XEXP (addr, 0)) == HARD_FRAME_POINTER_REGNUM
864                   || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
865               && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
866                    && ! SMALL_INT (XEXP (addr, 1)))
867                   || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
868                       && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
869             return 1;
870
871           /* Similarly, we accept a case where the memory address is
872              itself on the stack, and will be reloaded. */
873           if (GET_CODE (addr) == MEM)
874             {
875               rtx maddr;
876
877               maddr = XEXP (addr, 0);
878               if (GET_CODE (maddr) == PLUS
879                   && GET_CODE (XEXP (maddr, 0)) == REG
880                   && (REGNO (XEXP (maddr, 0)) == HARD_FRAME_POINTER_REGNUM
881                       || REGNO (XEXP (maddr, 0)) == STACK_POINTER_REGNUM)
882                   && ((GET_CODE (XEXP (maddr, 1)) == CONST_INT
883                        && ! SMALL_INT (XEXP (maddr, 1)))
884                       || (GET_CODE (XEXP (maddr, 1)) == SYMBOL_REF
885                           && CONSTANT_POOL_ADDRESS_P (XEXP (maddr, 1)))))
886                 return 1;
887             }
888
889           /* We also accept the same case when we have a 16 bit signed
890              offset mixed in as well.  The large address will get
891              reloaded, and the 16 bit offset will be OK.  */
892           if (GET_CODE (addr) == PLUS
893               && GET_CODE (XEXP (addr, 0)) == MEM
894               && GET_CODE (XEXP (addr, 1)) == CONST_INT
895               && SMALL_INT (XEXP (addr, 1)))
896             {
897               addr = XEXP (XEXP (addr, 0), 0);
898               if (GET_CODE (addr) == PLUS
899                   && GET_CODE (XEXP (addr, 0)) == REG
900                   && (REGNO (XEXP (addr, 0)) == HARD_FRAME_POINTER_REGNUM
901                       || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
902                   && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
903                        && ! SMALL_INT (XEXP (addr, 1)))
904                       || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
905                           && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
906                 return 1;
907             }
908         }
909
910       return 0;
911     }
912
913   if (TARGET_64BIT)
914     {
915       /* In this case we can use an instruction like sd.  */
916       return 1;
917     }
918
919   /* Make sure that 4 added to the address is a valid memory address.
920      This essentially just checks for overflow in an added constant.  */
921
922   addr = XEXP (op, 0);
923
924   if (CONSTANT_ADDRESS_P (addr))
925     return 1;
926
927   return memory_address_p ((GET_MODE_CLASS (mode) == MODE_INT
928                             ? SImode
929                             : SFmode),
930                            plus_constant_for_output (addr, 4));
931 }
932
933 /* Return nonzero if the code of this rtx pattern is EQ or NE.  */
934
935 int
936 equality_op (op, mode)
937      rtx op;
938      enum machine_mode mode;
939 {
940   if (mode != GET_MODE (op))
941     return 0;
942
943   return GET_CODE (op) == EQ || GET_CODE (op) == NE;
944 }
945
946 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
947
948 int
949 cmp_op (op, mode)
950      rtx op;
951      enum machine_mode mode;
952 {
953   if (mode != GET_MODE (op))
954     return 0;
955
956   return GET_RTX_CLASS (GET_CODE (op)) == '<';
957 }
958
959 /* Return nonzero if the operand is either the PC or a label_ref.  */
960
961 int
962 pc_or_label_operand (op, mode)
963      rtx op;
964      enum machine_mode mode ATTRIBUTE_UNUSED;
965 {
966   if (op == pc_rtx)
967     return 1;
968
969   if (GET_CODE (op) == LABEL_REF)
970     return 1;
971
972   return 0;
973 }
974
975 /* Test for a valid operand for a call instruction.
976    Don't allow the arg pointer register or virtual regs
977    since they may change into reg + const, which the patterns
978    can't handle yet.  */
979
980 int
981 call_insn_operand (op, mode)
982      rtx op;
983      enum machine_mode mode ATTRIBUTE_UNUSED;
984 {
985   return (CONSTANT_ADDRESS_P (op)
986           || (GET_CODE (op) == REG && op != arg_pointer_rtx
987               && ! (REGNO (op) >= FIRST_PSEUDO_REGISTER
988                     && REGNO (op) <= LAST_VIRTUAL_REGISTER)));
989 }
990
991 /* Return nonzero if OPERAND is valid as a source operand for a move
992    instruction.  */
993
994 int
995 move_operand (op, mode)
996      rtx op;
997      enum machine_mode mode;
998 {
999   /* Accept any general operand after reload has started; doing so
1000      avoids losing if reload does an in-place replacement of a register
1001      with a SYMBOL_REF or CONST.  */
1002   return (general_operand (op, mode)
1003           && (! (mips_split_addresses && mips_check_split (op, mode))
1004               || reload_in_progress || reload_completed)
1005           && ! (TARGET_MIPS16
1006                 && GET_CODE (op) == SYMBOL_REF
1007                 && ! mips16_constant (op, mode, 1, 0)));
1008 }
1009
1010 /* Return nonzero if OPERAND is valid as a source operand for movdi.
1011    This accepts not only general_operand, but also sign extended
1012    constants and registers.  We need to accept sign extended constants
1013    in case a sign extended register which is used in an expression,
1014    and is equivalent to a constant, is spilled.  */
1015
1016 int
1017 movdi_operand (op, mode)
1018      rtx op;
1019      enum machine_mode mode;
1020 {
1021   if (TARGET_64BIT
1022       && mode == DImode
1023       && GET_CODE (op) == SIGN_EXTEND
1024       && GET_MODE (op) == DImode
1025       && (GET_MODE (XEXP (op, 0)) == SImode
1026           || (GET_CODE (XEXP (op, 0)) == CONST_INT
1027               && GET_MODE (XEXP (op, 0)) == VOIDmode))
1028       && (register_operand (XEXP (op, 0), SImode)
1029           || immediate_operand (XEXP (op, 0), SImode)))
1030     return 1;
1031
1032   return (general_operand (op, mode)
1033           && ! (TARGET_MIPS16
1034                 && GET_CODE (op) == SYMBOL_REF
1035                 && ! mips16_constant (op, mode, 1, 0)));
1036 }
1037
1038 /* Like register_operand, but when in 64 bit mode also accept a sign
1039    extend of a 32 bit register, since the value is known to be already
1040    sign extended.  */
1041
1042 int
1043 se_register_operand (op, mode)
1044      rtx op;
1045      enum machine_mode mode;
1046 {
1047   if (TARGET_64BIT
1048       && mode == DImode
1049       && GET_CODE (op) == SIGN_EXTEND
1050       && GET_MODE (op) == DImode
1051       && GET_MODE (XEXP (op, 0)) == SImode
1052       && register_operand (XEXP (op, 0), SImode))
1053     return 1;
1054
1055   return register_operand (op, mode);
1056 }
1057
1058 /* Like reg_or_0_operand, but when in 64 bit mode also accept a sign
1059    extend of a 32 bit register, since the value is known to be already
1060    sign extended.  */
1061
1062 int
1063 se_reg_or_0_operand (op, mode)
1064      rtx op;
1065      enum machine_mode mode;
1066 {
1067   if (TARGET_64BIT
1068       && mode == DImode
1069       && GET_CODE (op) == SIGN_EXTEND
1070       && GET_MODE (op) == DImode
1071       && GET_MODE (XEXP (op, 0)) == SImode
1072       && register_operand (XEXP (op, 0), SImode))
1073     return 1;
1074
1075   return reg_or_0_operand (op, mode);
1076 }
1077
1078 /* Like uns_arith_operand, but when in 64 bit mode also accept a sign
1079    extend of a 32 bit register, since the value is known to be already
1080    sign extended.  */
1081
1082 int
1083 se_uns_arith_operand (op, mode)
1084      rtx op;
1085      enum machine_mode mode;
1086 {
1087   if (TARGET_64BIT
1088       && mode == DImode
1089       && GET_CODE (op) == SIGN_EXTEND
1090       && GET_MODE (op) == DImode
1091       && GET_MODE (XEXP (op, 0)) == SImode
1092       && register_operand (XEXP (op, 0), SImode))
1093     return 1;
1094
1095   return uns_arith_operand (op, mode);
1096 }
1097
1098 /* Like arith_operand, but when in 64 bit mode also accept a sign
1099    extend of a 32 bit register, since the value is known to be already
1100    sign extended.  */
1101
1102 int
1103 se_arith_operand (op, mode)
1104      rtx op;
1105      enum machine_mode mode;
1106 {
1107   if (TARGET_64BIT
1108       && mode == DImode
1109       && GET_CODE (op) == SIGN_EXTEND
1110       && GET_MODE (op) == DImode
1111       && GET_MODE (XEXP (op, 0)) == SImode
1112       && register_operand (XEXP (op, 0), SImode))
1113     return 1;
1114
1115   return arith_operand (op, mode);
1116 }
1117
1118 /* Like nonmemory_operand, but when in 64 bit mode also accept a sign
1119    extend of a 32 bit register, since the value is known to be already
1120    sign extended.  */
1121
1122 int
1123 se_nonmemory_operand (op, mode)
1124      rtx op;
1125      enum machine_mode mode;
1126 {
1127   if (TARGET_64BIT
1128       && mode == DImode
1129       && GET_CODE (op) == SIGN_EXTEND
1130       && GET_MODE (op) == DImode
1131       && GET_MODE (XEXP (op, 0)) == SImode
1132       && register_operand (XEXP (op, 0), SImode))
1133     return 1;
1134
1135   return nonmemory_operand (op, mode);
1136 }
1137
1138 /* Like nonimmediate_operand, but when in 64 bit mode also accept a
1139    sign extend of a 32 bit register, since the value is known to be
1140    already sign extended.  */
1141
1142 int
1143 se_nonimmediate_operand (op, mode)
1144      rtx op;
1145      enum machine_mode mode;
1146 {
1147   if (TARGET_64BIT
1148       && mode == DImode
1149       && GET_CODE (op) == SIGN_EXTEND
1150       && GET_MODE (op) == DImode
1151       && GET_MODE (XEXP (op, 0)) == SImode
1152       && register_operand (XEXP (op, 0), SImode))
1153     return 1;
1154
1155   return nonimmediate_operand (op, mode);
1156 }
1157
1158 /* Accept any operand that can appear in a mips16 constant table
1159    instruction.  We can't use any of the standard operand functions
1160    because for these instructions we accept values that are not
1161    accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs.  */
1162
1163 int
1164 consttable_operand (op, mode)
1165      rtx op;
1166      enum machine_mode mode ATTRIBUTE_UNUSED;
1167 {
1168   return CONSTANT_P (op);
1169 }
1170
1171 /* Return nonzero if we split the address into high and low parts.  */
1172
1173 /* ??? We should also handle reg+array somewhere.  We get four
1174    instructions currently, lui %hi/addui %lo/addui reg/lw.  Better is
1175    lui %hi/addui reg/lw %lo.  Fixing GO_IF_LEGITIMATE_ADDRESS to accept
1176    (plus (reg) (symbol_ref)) doesn't work because the SYMBOL_REF is broken
1177    out of the address, then we have 4 instructions to combine.  Perhaps
1178    add a 3->2 define_split for combine.  */
1179
1180 /* ??? We could also split a CONST_INT here if it is a large_int().
1181    However, it doesn't seem to be very useful to have %hi(constant).
1182    We would be better off by doing the masking ourselves and then putting
1183    the explicit high part of the constant in the RTL.  This will give better
1184    optimization.  Also, %hi(constant) needs assembler changes to work.
1185    There is already a define_split that does this.  */
1186
1187 int
1188 mips_check_split (address, mode)
1189      rtx address;
1190      enum machine_mode mode;
1191 {     
1192   /* ??? This is the same check used in simple_memory_operand.
1193      We use it here because LO_SUM is not offsettable.  */
1194   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1195     return 0;
1196
1197   if ((GET_CODE (address) == SYMBOL_REF && ! SYMBOL_REF_FLAG (address))
1198       || (GET_CODE (address) == CONST
1199           && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF
1200           && ! SYMBOL_REF_FLAG (XEXP (XEXP (address, 0), 0)))
1201       || GET_CODE (address) == LABEL_REF)
1202     return 1;
1203
1204   return 0;
1205 }
1206 \f
1207 /* We need a lot of little routines to check constant values on the
1208    mips16.  These are used to figure out how long the instruction will
1209    be.  It would be much better to do this using constraints, but
1210    there aren't nearly enough letters available.  */
1211
1212 static int
1213 m16_check_op (op, low, high, mask)
1214      rtx op;
1215      int low;
1216      int high;
1217      int mask;
1218 {
1219   return (GET_CODE (op) == CONST_INT
1220           && INTVAL (op) >= low
1221           && INTVAL (op) <= high
1222           && (INTVAL (op) & mask) == 0);
1223 }
1224
1225 int
1226 m16_uimm3_b (op, mode)
1227      rtx op;
1228      enum machine_mode mode ATTRIBUTE_UNUSED;
1229 {
1230   return m16_check_op (op, 0x1, 0x8, 0);
1231 }
1232
1233 int
1234 m16_simm4_1 (op, mode)
1235      rtx op;
1236      enum machine_mode mode ATTRIBUTE_UNUSED;
1237 {
1238   return m16_check_op (op, - 0x8, 0x7, 0);
1239 }
1240
1241 int
1242 m16_nsimm4_1 (op, mode)
1243      rtx op;
1244      enum machine_mode mode ATTRIBUTE_UNUSED;
1245 {
1246   return m16_check_op (op, - 0x7, 0x8, 0);
1247 }
1248
1249 int
1250 m16_simm5_1 (op, mode)
1251      rtx op;
1252      enum machine_mode mode ATTRIBUTE_UNUSED;
1253 {
1254   return m16_check_op (op, - 0x10, 0xf, 0);
1255 }
1256
1257 int
1258 m16_nsimm5_1 (op, mode)
1259      rtx op;
1260      enum machine_mode mode ATTRIBUTE_UNUSED;
1261 {
1262   return m16_check_op (op, - 0xf, 0x10, 0);
1263 }
1264
1265 int
1266 m16_uimm5_4 (op, mode)
1267      rtx op;
1268      enum machine_mode mode ATTRIBUTE_UNUSED;
1269 {
1270   return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
1271 }
1272
1273 int
1274 m16_nuimm5_4 (op, mode)
1275      rtx op;
1276      enum machine_mode mode ATTRIBUTE_UNUSED;
1277 {
1278   return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
1279 }
1280
1281 int
1282 m16_simm8_1 (op, mode)
1283      rtx op;
1284      enum machine_mode mode ATTRIBUTE_UNUSED;
1285 {
1286   return m16_check_op (op, - 0x80, 0x7f, 0);
1287 }
1288
1289 int
1290 m16_nsimm8_1 (op, mode)
1291      rtx op;
1292      enum machine_mode mode ATTRIBUTE_UNUSED;
1293 {
1294   return m16_check_op (op, - 0x7f, 0x80, 0);
1295 }
1296
1297 int
1298 m16_uimm8_1 (op, mode)
1299      rtx op;
1300      enum machine_mode mode ATTRIBUTE_UNUSED;
1301 {
1302   return m16_check_op (op, 0x0, 0xff, 0);
1303 }
1304
1305 int
1306 m16_nuimm8_1 (op, mode)
1307      rtx op;
1308      enum machine_mode mode ATTRIBUTE_UNUSED;
1309 {
1310   return m16_check_op (op, - 0xff, 0x0, 0);
1311 }
1312
1313 int
1314 m16_uimm8_m1_1 (op, mode)
1315      rtx op;
1316      enum machine_mode mode ATTRIBUTE_UNUSED;
1317 {
1318   return m16_check_op (op, - 0x1, 0xfe, 0);
1319 }
1320
1321 int
1322 m16_uimm8_4 (op, mode)
1323      rtx op;
1324      enum machine_mode mode ATTRIBUTE_UNUSED;
1325 {
1326   return m16_check_op (op, 0x0, 0xff << 2, 3);
1327 }
1328
1329 int
1330 m16_nuimm8_4 (op, mode)
1331      rtx op;
1332      enum machine_mode mode ATTRIBUTE_UNUSED;
1333 {
1334   return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
1335 }
1336
1337 int
1338 m16_simm8_8 (op, mode)
1339      rtx op;
1340      enum machine_mode mode ATTRIBUTE_UNUSED;
1341 {
1342   return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
1343 }
1344
1345 int
1346 m16_nsimm8_8 (op, mode)
1347      rtx op;
1348      enum machine_mode mode ATTRIBUTE_UNUSED;
1349 {
1350   return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
1351 }
1352
1353 /* References to the string table on the mips16 only use a small
1354    offset if the function is small.  See the comment in the SYMBOL_REF
1355    case in simple_memory_operand.  We can't check for LABEL_REF here,
1356    because the offset is always large if the label is before the
1357    referencing instruction.  */
1358
1359 int
1360 m16_usym8_4 (op, mode)
1361      rtx op;
1362      enum machine_mode mode ATTRIBUTE_UNUSED;
1363 {
1364   if (GET_CODE (op) == SYMBOL_REF
1365       && SYMBOL_REF_FLAG (op)
1366       && current_frame_info.insns_len > 0
1367       && XSTR (op, 0)[0] == '*'
1368       && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1369                   sizeof LOCAL_LABEL_PREFIX - 1) == 0
1370       && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1371           < 4 * 0x100))
1372     {
1373       struct string_constant *l;
1374
1375       /* Make sure this symbol is on thelist of string constants to be
1376          output for this function.  It is possible that it has already
1377          been output, in which case this requires a large offset.  */
1378       for (l = string_constants; l != NULL; l = l->next)
1379         if (strcmp (l->label, XSTR (op, 0)) == 0)
1380           return 1;
1381     }
1382
1383   return 0;
1384 }
1385
1386 int
1387 m16_usym5_4 (op, mode)
1388      rtx op;
1389      enum machine_mode mode ATTRIBUTE_UNUSED;
1390 {
1391   if (GET_CODE (op) == SYMBOL_REF
1392       && SYMBOL_REF_FLAG (op)
1393       && current_frame_info.insns_len > 0
1394       && XSTR (op, 0)[0] == '*'
1395       && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1396                   sizeof LOCAL_LABEL_PREFIX - 1) == 0
1397       && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1398           < 4 * 0x20))
1399     {
1400       struct string_constant *l;
1401
1402       /* Make sure this symbol is on thelist of string constants to be
1403          output for this function.  It is possible that it has already
1404          been output, in which case this requires a large offset.  */
1405       for (l = string_constants; l != NULL; l = l->next)
1406         if (strcmp (l->label, XSTR (op, 0)) == 0)
1407           return 1;
1408     }
1409
1410   return 0;
1411 }
1412 \f
1413 /* Returns an operand string for the given instruction's delay slot,
1414    after updating filled delay slot statistics.
1415
1416    We assume that operands[0] is the target register that is set.
1417
1418    In order to check the next insn, most of this functionality is moved
1419    to FINAL_PRESCAN_INSN, and we just set the global variables that
1420    it needs.  */
1421
1422 /* ??? This function no longer does anything useful, because final_prescan_insn
1423    now will never emit a nop.  */
1424
1425 char *
1426 mips_fill_delay_slot (ret, type, operands, cur_insn)
1427      char *ret;                 /* normal string to return */
1428      enum delay_type type;      /* type of delay */
1429      rtx operands[];            /* operands to use */
1430      rtx cur_insn;              /* current insn */
1431 {
1432   register rtx set_reg;
1433   register enum machine_mode mode;
1434   register rtx next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL_RTX;
1435   register int num_nops;
1436
1437   if (type == DELAY_LOAD || type == DELAY_FCMP)
1438     num_nops = 1;
1439
1440   else if (type == DELAY_HILO)
1441     num_nops = 2;
1442
1443   else
1444     num_nops = 0;
1445
1446   /* Make sure that we don't put nop's after labels.  */
1447   next_insn = NEXT_INSN (cur_insn);
1448   while (next_insn != 0 && GET_CODE (next_insn) == NOTE)
1449     next_insn = NEXT_INSN (next_insn);
1450
1451   dslots_load_total += num_nops;
1452   if (TARGET_DEBUG_F_MODE
1453       || !optimize
1454       || type == DELAY_NONE
1455       || operands == 0
1456       || cur_insn == 0
1457       || next_insn == 0
1458       || GET_CODE (next_insn) == CODE_LABEL
1459       || (set_reg = operands[0]) == 0)
1460     {
1461       dslots_number_nops = 0;
1462       mips_load_reg  = 0;
1463       mips_load_reg2 = 0;
1464       mips_load_reg3 = 0;
1465       mips_load_reg4 = 0;
1466       return ret;
1467     }
1468
1469   set_reg = operands[0];
1470   if (set_reg == 0)
1471     return ret;
1472
1473   while (GET_CODE (set_reg) == SUBREG)
1474     set_reg = SUBREG_REG (set_reg);
1475
1476   mode = GET_MODE (set_reg);
1477   dslots_number_nops = num_nops;
1478   mips_load_reg = set_reg;
1479   if (GET_MODE_SIZE (mode)
1480       > (FP_REG_P (REGNO (set_reg)) ? UNITS_PER_FPREG : UNITS_PER_WORD))
1481     mips_load_reg2 = gen_rtx (REG, SImode, REGNO (set_reg) + 1);
1482   else
1483     mips_load_reg2 = 0;
1484
1485   if (type == DELAY_HILO)
1486     {
1487       mips_load_reg3 = gen_rtx (REG, SImode, MD_REG_FIRST);
1488       mips_load_reg4 = gen_rtx (REG, SImode, MD_REG_FIRST+1);
1489     }
1490   else
1491     {
1492       mips_load_reg3 = 0;
1493       mips_load_reg4 = 0;
1494     }
1495
1496   return ret;
1497 }
1498
1499 \f
1500 /* Determine whether a memory reference takes one (based off of the GP
1501    pointer), two (normal), or three (label + reg) instructions, and bump the
1502    appropriate counter for -mstats.  */
1503
1504 void
1505 mips_count_memory_refs (op, num)
1506      rtx op;
1507      int num;
1508 {
1509   int additional = 0;
1510   int n_words = 0;
1511   rtx addr, plus0, plus1;
1512   enum rtx_code code0, code1;
1513   int looping;
1514
1515   if (TARGET_DEBUG_B_MODE)
1516     {
1517       fprintf (stderr, "\n========== mips_count_memory_refs:\n");
1518       debug_rtx (op);
1519     }
1520
1521   /* Skip MEM if passed, otherwise handle movsi of address.  */
1522   addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
1523
1524   /* Loop, going through the address RTL.  */
1525   do
1526     {
1527       looping = FALSE;
1528       switch (GET_CODE (addr))
1529         {
1530         case REG:
1531         case CONST_INT:
1532         case LO_SUM:
1533           break;
1534
1535         case PLUS:
1536           plus0 = XEXP (addr, 0);
1537           plus1 = XEXP (addr, 1);
1538           code0 = GET_CODE (plus0);
1539           code1 = GET_CODE (plus1);
1540
1541           if (code0 == REG)
1542             {
1543               additional++;
1544               addr = plus1;
1545               looping = 1;
1546               continue;
1547             }
1548
1549           if (code0 == CONST_INT)
1550             {
1551               addr = plus1;
1552               looping = 1;
1553               continue;
1554             }
1555
1556           if (code1 == REG)
1557             {
1558               additional++;
1559               addr = plus0;
1560               looping = 1;
1561               continue;
1562             }
1563
1564           if (code1 == CONST_INT)
1565             {
1566               addr = plus0;
1567               looping = 1;
1568               continue;
1569             }
1570
1571           if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
1572             {
1573               addr = plus0;
1574               looping = 1;
1575               continue;
1576             }
1577
1578           if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
1579             {
1580               addr = plus1;
1581               looping = 1;
1582               continue;
1583             }
1584
1585           break;
1586
1587         case LABEL_REF:
1588           n_words = 2;          /* always 2 words */
1589           break;
1590
1591         case CONST:
1592           addr = XEXP (addr, 0);
1593           looping = 1;
1594           continue;
1595
1596         case SYMBOL_REF:
1597           n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
1598           break;
1599
1600         default:
1601           break;
1602         }
1603     }
1604   while (looping);
1605
1606   if (n_words == 0)
1607     return;
1608
1609   n_words += additional;
1610   if (n_words > 3)
1611     n_words = 3;
1612
1613   num_refs[n_words-1] += num;
1614 }
1615
1616 \f
1617 /* Return RTL for the offset from the current function to the argument.
1618
1619    ??? Which argument is this?  */
1620
1621 rtx
1622 embedded_pic_offset (x)
1623      rtx x;
1624 {
1625   if (embedded_pic_fnaddr_rtx == NULL)
1626     {
1627       rtx seq;
1628
1629       embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
1630       
1631       /* Output code at function start to initialize the pseudo-reg.  */
1632       /* ??? We used to do this in FINALIZE_PIC, but that does not work for
1633          inline functions, because it is called after RTL for the function
1634          has been copied.  The pseudo-reg in embedded_pic_fnaddr_rtx however
1635          does not get copied, and ends up not matching the rest of the RTL.
1636          This solution works, but means that we get unnecessary code to
1637          initialize this value every time a function is inlined into another
1638          function.  */
1639       start_sequence ();
1640       emit_insn (gen_get_fnaddr (embedded_pic_fnaddr_rtx,
1641                                  XEXP (DECL_RTL (current_function_decl), 0)));
1642       seq = gen_sequence ();
1643       end_sequence ();
1644       push_topmost_sequence ();
1645       emit_insn_after (seq, get_insns ());
1646       pop_topmost_sequence ();
1647     }
1648
1649   return gen_rtx (CONST, Pmode,
1650                   gen_rtx (MINUS, Pmode, x,
1651                            XEXP (DECL_RTL (current_function_decl), 0)));
1652 }
1653
1654 /* Return the appropriate instructions to move one operand to another.  */
1655
1656 const char *
1657 mips_move_1word (operands, insn, unsignedp)
1658      rtx operands[];
1659      rtx insn;
1660      int unsignedp;
1661 {
1662   const char *ret = 0;
1663   rtx op0 = operands[0];
1664   rtx op1 = operands[1];
1665   enum rtx_code code0 = GET_CODE (op0);
1666   enum rtx_code code1 = GET_CODE (op1);
1667   enum machine_mode mode = GET_MODE (op0);
1668   int subreg_word0 = 0;
1669   int subreg_word1 = 0;
1670   enum delay_type delay = DELAY_NONE;
1671
1672   while (code0 == SUBREG)
1673     {
1674       subreg_word0 += SUBREG_WORD (op0);
1675       op0 = SUBREG_REG (op0);
1676       code0 = GET_CODE (op0);
1677     }
1678
1679   while (code1 == SUBREG)
1680     {
1681       subreg_word1 += SUBREG_WORD (op1);
1682       op1 = SUBREG_REG (op1);
1683       code1 = GET_CODE (op1);
1684     }
1685
1686   /* For our purposes, a condition code mode is the same as SImode.  */
1687   if (mode == CCmode)
1688     mode = SImode;
1689
1690   if (code0 == REG)
1691     {
1692       int regno0 = REGNO (op0) + subreg_word0;
1693
1694       if (code1 == REG)
1695         {
1696           int regno1 = REGNO (op1) + subreg_word1;
1697
1698           /* Just in case, don't do anything for assigning a register
1699              to itself, unless we are filling a delay slot.  */
1700           if (regno0 == regno1 && set_nomacro == 0)
1701             ret = "";
1702
1703           else if (GP_REG_P (regno0))
1704             {
1705               if (GP_REG_P (regno1))
1706                 ret = "move\t%0,%1";
1707
1708               else if (MD_REG_P (regno1))
1709                 {
1710                   delay = DELAY_HILO;
1711                   if (regno1 != HILO_REGNUM)
1712                     ret = "mf%1\t%0";
1713                   else
1714                     ret = "mflo\t%0";
1715                 }
1716
1717               else if (ST_REG_P (regno1) && mips_isa >= 4)
1718                 ret = "li\t%0,1\n\tmovf\t%0,%.,%1";
1719
1720               else
1721                 {
1722                   delay = DELAY_LOAD;
1723                   if (FP_REG_P (regno1))
1724                     ret = "mfc1\t%0,%1";
1725
1726                   else if (regno1 == FPSW_REGNUM && mips_isa < 4)
1727                     ret = "cfc1\t%0,$31";
1728                 }
1729             }
1730
1731           else if (FP_REG_P (regno0))
1732             {
1733               if (GP_REG_P (regno1))
1734                 {
1735                   delay = DELAY_LOAD;
1736                   ret = "mtc1\t%1,%0";
1737                 }
1738
1739               if (FP_REG_P (regno1))
1740                 ret = "mov.s\t%0,%1";
1741             }
1742
1743           else if (MD_REG_P (regno0))
1744             {
1745               if (GP_REG_P (regno1))
1746                 {
1747                   delay = DELAY_HILO;
1748                   if (regno0 != HILO_REGNUM && ! TARGET_MIPS16)
1749                     ret = "mt%0\t%1";
1750                 }
1751             }
1752
1753           else if (regno0 == FPSW_REGNUM && mips_isa < 4)
1754             {
1755               if (GP_REG_P (regno1))
1756                 {
1757                   delay = DELAY_LOAD;
1758                   ret = "ctc1\t%0,$31";
1759                 }
1760             }
1761         }
1762
1763       else if (code1 == MEM)
1764         {
1765           delay = DELAY_LOAD;
1766
1767           if (TARGET_STATS)
1768             mips_count_memory_refs (op1, 1);
1769
1770           if (GP_REG_P (regno0))
1771             {
1772               /* For loads, use the mode of the memory item, instead of the
1773                  target, so zero/sign extend can use this code as well.  */
1774               switch (GET_MODE (op1))
1775                 {
1776                 default:
1777                   break;
1778                 case SFmode:
1779                   ret = "lw\t%0,%1";
1780                   break;
1781                 case SImode:
1782                 case CCmode:
1783                   ret = ((unsignedp && TARGET_64BIT)
1784                          ? "lwu\t%0,%1"
1785                          : "lw\t%0,%1");
1786                   break;
1787                 case HImode:
1788                   ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
1789                   break;
1790                 case QImode:
1791                   ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
1792                   break;
1793                 }
1794             }
1795
1796           else if (FP_REG_P (regno0) && (mode == SImode || mode == SFmode))
1797             ret = "l.s\t%0,%1";
1798
1799           if (ret != (char *)0 && MEM_VOLATILE_P (op1))
1800             {
1801               size_t i = strlen (ret);
1802               if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
1803                 abort ();
1804
1805               sprintf (volatile_buffer, "%%{%s%%}", ret);
1806               ret = volatile_buffer;
1807             }
1808         }
1809
1810       else if (code1 == CONST_INT
1811                || (code1 == CONST_DOUBLE
1812                    && GET_MODE (op1) == VOIDmode))
1813         {
1814           if (code1 == CONST_DOUBLE)
1815             {
1816               /* This can happen when storing constants into long long
1817                  bitfields.  Just store the least significant word of
1818                  the value.  */
1819               operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
1820             }
1821
1822           if (INTVAL (op1) == 0 && ! TARGET_MIPS16)
1823             {
1824               if (GP_REG_P (regno0))
1825                 ret = "move\t%0,%z1";
1826
1827               else if (FP_REG_P (regno0))
1828                 {
1829                   delay = DELAY_LOAD;
1830                   ret = "mtc1\t%z1,%0";
1831                 }
1832
1833               else if (MD_REG_P (regno0))
1834                 {
1835                   delay = DELAY_HILO;
1836                   ret = "mt%0\t%.";
1837                 }
1838             }
1839
1840           else if (GP_REG_P (regno0))
1841             {
1842               /* Don't use X format, because that will give out of
1843                  range numbers for 64 bit host and 32 bit target.  */
1844               if (! TARGET_MIPS16)
1845                 ret = "li\t%0,%1\t\t\t# %X1";
1846               else
1847                 {
1848                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
1849                     ret = "li\t%0,%1";
1850                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
1851                     ret = "li\t%0,%n1\n\tneg\t%0";
1852                 }
1853             }
1854         }
1855
1856       else if (code1 == CONST_DOUBLE && mode == SFmode)
1857         {
1858           if (op1 == CONST0_RTX (SFmode))
1859             {
1860               if (GP_REG_P (regno0))
1861                 ret = "move\t%0,%.";
1862
1863               else if (FP_REG_P (regno0))
1864                 {
1865                   delay = DELAY_LOAD;
1866                   ret = "mtc1\t%.,%0";
1867                 }
1868             }
1869
1870           else
1871             {
1872               delay = DELAY_LOAD;
1873               ret = "li.s\t%0,%1";
1874             }
1875         }
1876
1877       else if (code1 == LABEL_REF)
1878         {
1879           if (TARGET_STATS)
1880             mips_count_memory_refs (op1, 1);
1881
1882           ret = "la\t%0,%a1";
1883         }
1884
1885       else if (code1 == SYMBOL_REF || code1 == CONST)
1886         {
1887           if (HALF_PIC_P () && CONSTANT_P (op1) && HALF_PIC_ADDRESS_P (op1))
1888             {
1889               rtx offset = const0_rtx;
1890
1891               if (GET_CODE (op1) == CONST)
1892                 op1 = eliminate_constant_term (XEXP (op1, 0), &offset);
1893
1894               if (GET_CODE (op1) == SYMBOL_REF)
1895                 {
1896                   operands[2] = HALF_PIC_PTR (op1);
1897
1898                   if (TARGET_STATS)
1899                     mips_count_memory_refs (operands[2], 1);
1900
1901                   if (INTVAL (offset) == 0)
1902                     {
1903                       delay = DELAY_LOAD;
1904                       ret = (unsignedp && TARGET_64BIT
1905                              ? "lwu\t%0,%2"
1906                              : "lw\t%0,%2");
1907                     }
1908                   else
1909                     {
1910                       dslots_load_total++;
1911                       operands[3] = offset;
1912                       if (unsignedp && TARGET_64BIT)
1913                         ret = (SMALL_INT (offset)
1914                                ? "lwu\t%0,%2%#\n\tadd\t%0,%0,%3"
1915                                : "lwu\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
1916                       else
1917                         ret = (SMALL_INT (offset)
1918                                ? "lw\t%0,%2%#\n\tadd\t%0,%0,%3"
1919                                : "lw\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
1920                     }
1921                 }
1922             }
1923           else if (TARGET_MIPS16
1924                    && code1 == CONST
1925                    && GET_CODE (XEXP (op1, 0)) == REG
1926                    && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
1927             {
1928               /* This case arises on the mips16; see
1929                  mips16_gp_pseudo_reg.  */
1930               ret = "move\t%0,%+";
1931             }
1932           else if (TARGET_MIPS16
1933                    && code1 == SYMBOL_REF
1934                    && SYMBOL_REF_FLAG (op1)
1935                    && (XSTR (op1, 0)[0] != '*'
1936                        || strncmp (XSTR (op1, 0) + 1,
1937                                    LOCAL_LABEL_PREFIX,
1938                                    sizeof LOCAL_LABEL_PREFIX - 1) != 0))
1939             {
1940               /* This can occur when reloading the address of a GP
1941                  relative symbol on the mips16.  */
1942               ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
1943             }
1944           else
1945             {
1946               if (TARGET_STATS)
1947                 mips_count_memory_refs (op1, 1);
1948
1949               ret = "la\t%0,%a1";
1950             }
1951         }
1952
1953       else if (code1 == PLUS)
1954         {
1955           rtx add_op0 = XEXP (op1, 0);
1956           rtx add_op1 = XEXP (op1, 1);
1957
1958           if (GET_CODE (XEXP (op1, 1)) == REG
1959               && GET_CODE (XEXP (op1, 0)) == CONST_INT)
1960             add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
1961
1962           operands[2] = add_op0;
1963           operands[3] = add_op1;
1964           ret = "add%:\t%0,%2,%3";
1965         }
1966
1967       else if (code1 == HIGH)
1968         {
1969           operands[1] = XEXP (op1, 0);
1970           ret = "lui\t%0,%%hi(%1)";
1971         }
1972     }
1973
1974   else if (code0 == MEM)
1975     {
1976       if (TARGET_STATS)
1977         mips_count_memory_refs (op0, 1);
1978
1979       if (code1 == REG)
1980         {
1981           int regno1 = REGNO (op1) + subreg_word1;
1982
1983           if (GP_REG_P (regno1))
1984             {
1985               switch (mode)
1986                 {
1987                 case SFmode: ret = "sw\t%1,%0"; break;
1988                 case SImode: ret = "sw\t%1,%0"; break;
1989                 case HImode: ret = "sh\t%1,%0"; break;
1990                 case QImode: ret = "sb\t%1,%0"; break;
1991                 default: break;
1992                 }
1993             }
1994
1995           else if (FP_REG_P (regno1) && (mode == SImode || mode == SFmode))
1996             ret = "s.s\t%1,%0";
1997         }
1998
1999       else if (code1 == CONST_INT && INTVAL (op1) == 0)
2000         {
2001           switch (mode)
2002             {
2003             case SFmode: ret = "sw\t%z1,%0"; break;
2004             case SImode: ret = "sw\t%z1,%0"; break;
2005             case HImode: ret = "sh\t%z1,%0"; break;
2006             case QImode: ret = "sb\t%z1,%0"; break;
2007             default: break;
2008             }
2009         }
2010
2011       else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
2012         {
2013           switch (mode)
2014             {
2015             case SFmode: ret = "sw\t%.,%0"; break;
2016             case SImode: ret = "sw\t%.,%0"; break;
2017             case HImode: ret = "sh\t%.,%0"; break;
2018             case QImode: ret = "sb\t%.,%0"; break;
2019             default: break;
2020             }
2021         }
2022
2023       if (ret != 0 && MEM_VOLATILE_P (op0))
2024         {
2025           size_t i = strlen (ret);
2026
2027           if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2028             abort ();
2029           
2030           sprintf (volatile_buffer, "%%{%s%%}", ret);
2031           ret = volatile_buffer;
2032         }
2033     }
2034
2035   if (ret == 0)
2036     {
2037       abort_with_insn (insn, "Bad move");
2038       return 0;
2039     }
2040
2041   if (delay != DELAY_NONE)
2042     return mips_fill_delay_slot (ret, delay, operands, insn);
2043
2044   return ret;
2045 }
2046
2047 \f
2048 /* Return the appropriate instructions to move 2 words */
2049
2050 const char *
2051 mips_move_2words (operands, insn)
2052      rtx operands[];
2053      rtx insn;
2054 {
2055   const char *ret = 0;
2056   rtx op0 = operands[0];
2057   rtx op1 = operands[1];
2058   enum rtx_code code0 = GET_CODE (operands[0]);
2059   enum rtx_code code1 = GET_CODE (operands[1]);
2060   int subreg_word0 = 0;
2061   int subreg_word1 = 0;
2062   enum delay_type delay = DELAY_NONE;
2063
2064   while (code0 == SUBREG)
2065     {
2066       subreg_word0 += SUBREG_WORD (op0);
2067       op0 = SUBREG_REG (op0);
2068       code0 = GET_CODE (op0);
2069     }
2070
2071   if (code1 == SIGN_EXTEND)
2072     {
2073       op1 = XEXP (op1, 0);
2074       code1 = GET_CODE (op1);
2075     }
2076
2077   while (code1 == SUBREG)
2078     {
2079       subreg_word1 += SUBREG_WORD (op1);
2080       op1 = SUBREG_REG (op1);
2081       code1 = GET_CODE (op1);
2082     }
2083       
2084   /* Sanity check.  */
2085   if (GET_CODE (operands[1]) == SIGN_EXTEND
2086       && code1 != REG
2087       && code1 != CONST_INT
2088       /* The following three can happen as the result of a questionable
2089          cast.  */
2090       && code1 != LABEL_REF
2091       && code1 != SYMBOL_REF
2092       && code1 != CONST)
2093     abort ();
2094
2095   if (code0 == REG)
2096     {
2097       int regno0 = REGNO (op0) + subreg_word0;
2098
2099       if (code1 == REG)
2100         {
2101           int regno1 = REGNO (op1) + subreg_word1;
2102
2103           /* Just in case, don't do anything for assigning a register
2104              to itself, unless we are filling a delay slot.  */
2105           if (regno0 == regno1 && set_nomacro == 0)
2106             ret = "";
2107
2108           else if (FP_REG_P (regno0))
2109             {
2110               if (FP_REG_P (regno1))
2111                 ret = "mov.d\t%0,%1";
2112
2113               else
2114                 {
2115                   delay = DELAY_LOAD;
2116                   if (TARGET_FLOAT64)
2117                     {
2118                       if (!TARGET_64BIT)
2119                         abort_with_insn (insn, "Bad move");
2120
2121 #ifdef TARGET_FP_CALL_32
2122                       if (FP_CALL_GP_REG_P (regno1))
2123                         ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tdmtc1\t%1,%0";
2124                       else
2125 #endif
2126                         ret = "dmtc1\t%1,%0";
2127                     }
2128                   else
2129                     ret = "mtc1\t%L1,%0\n\tmtc1\t%M1,%D0";
2130                 }
2131             }
2132
2133           else if (FP_REG_P (regno1))
2134             {
2135               delay = DELAY_LOAD;
2136               if (TARGET_FLOAT64)
2137                 {
2138                   if (!TARGET_64BIT)
2139                     abort_with_insn (insn, "Bad move");
2140
2141 #ifdef TARGET_FP_CALL_32
2142                   if (FP_CALL_GP_REG_P (regno0))
2143                     ret = "dmfc1\t%0,%1\n\tmfc1\t%D0,%1\n\tdsrl\t%0,32";
2144                   else
2145 #endif
2146                     ret = "dmfc1\t%0,%1";
2147                 }
2148               else
2149                 ret = "mfc1\t%L0,%1\n\tmfc1\t%M0,%D1";
2150             }
2151
2152           else if (MD_REG_P (regno0) && GP_REG_P (regno1) && !TARGET_MIPS16)
2153             {
2154               delay = DELAY_HILO;
2155               if (TARGET_64BIT)
2156                 {
2157                   if (regno0 != HILO_REGNUM)
2158                     ret = "mt%0\t%1";
2159                   else if (regno1 == 0)
2160                     ret = "mtlo\t%.\n\tmthi\t%.";
2161                 }
2162               else
2163                 ret = "mthi\t%M1\n\tmtlo\t%L1";
2164             }
2165
2166           else if (GP_REG_P (regno0) && MD_REG_P (regno1))
2167             {
2168               delay = DELAY_HILO;
2169               if (TARGET_64BIT)
2170                 {
2171                   if (regno1 != HILO_REGNUM)
2172                     ret = "mf%1\t%0";
2173                 }
2174               else
2175                 ret = "mfhi\t%M0\n\tmflo\t%L0";
2176             }
2177
2178           else if (TARGET_64BIT)
2179             ret = "move\t%0,%1";
2180
2181           else if (regno0 != (regno1+1))
2182             ret = "move\t%0,%1\n\tmove\t%D0,%D1";
2183
2184           else
2185             ret = "move\t%D0,%D1\n\tmove\t%0,%1";
2186         }
2187
2188       else if (code1 == CONST_DOUBLE)
2189         {
2190           /* Move zero from $0 unless !TARGET_64BIT and recipient
2191              is 64-bit fp reg, in which case generate a constant.  */
2192           if (op1 != CONST0_RTX (GET_MODE (op1))
2193               || (TARGET_FLOAT64 && !TARGET_64BIT && FP_REG_P (regno0)))
2194             {
2195               if (GET_MODE (op1) == DFmode)
2196                 {
2197                   delay = DELAY_LOAD;
2198
2199 #ifdef TARGET_FP_CALL_32
2200                   if (FP_CALL_GP_REG_P (regno0))
2201                     {
2202                       if (TARGET_FLOAT64 && !TARGET_64BIT)
2203                         {
2204                           split_double (op1, operands + 2, operands + 3);
2205                           ret = "li\t%0,%2\n\tli\t%D0,%3";
2206                         }
2207                       else
2208                         ret = "li.d\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32";
2209                     }
2210                   else
2211 #endif
2212                     ret = "li.d\t%0,%1";
2213                 }
2214
2215               else if (TARGET_64BIT)
2216                 {
2217                   if (! TARGET_MIPS16)
2218                     ret = "dli\t%0,%1";
2219                 }
2220
2221               else
2222                 {
2223                   split_double (op1, operands + 2, operands + 3);
2224                   ret = "li\t%0,%2\n\tli\t%D0,%3";
2225                 }
2226             }
2227
2228           else
2229             {
2230               if (GP_REG_P (regno0))
2231                 ret = (TARGET_64BIT
2232 #ifdef TARGET_FP_CALL_32
2233                        && ! FP_CALL_GP_REG_P (regno0)
2234 #endif
2235                        ? "move\t%0,%."
2236                        : "move\t%0,%.\n\tmove\t%D0,%.");
2237
2238               else if (FP_REG_P (regno0))
2239                 {
2240                   delay = DELAY_LOAD;
2241                   ret = (TARGET_64BIT
2242                          ? "dmtc1\t%.,%0"
2243                          : "mtc1\t%.,%0\n\tmtc1\t%.,%D0");
2244                 }
2245             }
2246         }
2247
2248       else if (code1 == CONST_INT && INTVAL (op1) == 0 && ! TARGET_MIPS16)
2249         {
2250           if (GP_REG_P (regno0))
2251             ret = (TARGET_64BIT
2252                    ? "move\t%0,%."
2253                    : "move\t%0,%.\n\tmove\t%D0,%.");
2254
2255           else if (FP_REG_P (regno0))
2256             {
2257               delay = DELAY_LOAD;
2258               ret = (TARGET_64BIT
2259                      ? "dmtc1\t%.,%0"
2260                      : (TARGET_FLOAT64
2261                         ? "li.d\t%0,%1"
2262                         : "mtc1\t%.,%0\n\tmtc1\t%.,%D0"));
2263             }
2264           else if (MD_REG_P (regno0))
2265             {
2266               delay = DELAY_HILO;
2267               ret =  (regno0 == HILO_REGNUM
2268                       ? "mtlo\t%.\n\tmthi\t%."
2269                       : "mt%0\t%.\n");
2270             }
2271         }
2272         
2273       else if (code1 == CONST_INT && GET_MODE (op0) == DImode
2274                && GP_REG_P (regno0))
2275         {
2276           if (TARGET_64BIT)
2277             {
2278               if (TARGET_MIPS16)
2279                 {
2280                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2281                     ret = "li\t%0,%1";
2282                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2283                     ret = "li\t%0,%n1\n\tneg\t%0";
2284                 }
2285               else if (GET_CODE (operands[1]) == SIGN_EXTEND)
2286                 ret = "li\t%0,%1\t\t# %X1";
2287               else if (HOST_BITS_PER_WIDE_INT < 64)
2288                 /* We can't use 'X' for negative numbers, because then we won't
2289                    get the right value for the upper 32 bits.  */
2290                 ret = (INTVAL (op1) < 0
2291                        ? "dli\t%0,%1\t\t\t# %X1"
2292                        : "dli\t%0,%X1\t\t# %1");
2293               else
2294                 /* We must use 'X', because otherwise LONG_MIN will print as
2295                    a number that the assembler won't accept.  */
2296                 ret = "dli\t%0,%X1\t\t# %1";
2297             }
2298           else if (HOST_BITS_PER_WIDE_INT < 64)
2299             {
2300               operands[2] = GEN_INT (INTVAL (operands[1]) >= 0 ? 0 : -1);
2301               if (TARGET_MIPS16)
2302                 {
2303                   if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2304                     ret = "li\t%M0,%2\n\tli\t%L0,%1";
2305                   else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2306                     {
2307                       operands[2] = GEN_INT (1);
2308                       ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2309                     }
2310                 }
2311               else
2312                 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2313             }
2314           else
2315             {
2316               /* We use multiple shifts here, to avoid warnings about out
2317                  of range shifts on 32 bit hosts.  */
2318               operands[2] = GEN_INT (INTVAL (operands[1]) >> 16 >> 16);
2319               operands[1]
2320                 = GEN_INT (INTVAL (operands[1]) << 16 << 16 >> 16 >> 16);
2321               ret = "li\t%M0,%2\n\tli\t%L0,%1";
2322             }
2323         }
2324
2325       else if (code1 == MEM)
2326         {
2327           delay = DELAY_LOAD;
2328
2329           if (TARGET_STATS)
2330             mips_count_memory_refs (op1, 2);
2331
2332           if (FP_REG_P (regno0))
2333             ret = "l.d\t%0,%1";
2334
2335           else if (TARGET_64BIT)
2336             {
2337
2338 #ifdef TARGET_FP_CALL_32
2339               if (FP_CALL_GP_REG_P (regno0))
2340                 ret = (double_memory_operand (op1, GET_MODE (op1))
2341                        ? "lwu\t%0,%1\n\tlwu\t%D0,4+%1"
2342                        : "ld\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32");
2343               else
2344 #endif
2345                 ret = "ld\t%0,%1";
2346             }
2347
2348           else if (double_memory_operand (op1, GET_MODE (op1)))
2349             {
2350               operands[2] = adj_offsettable_operand (op1, 4);
2351               ret = (reg_mentioned_p (op0, op1)
2352                      ? "lw\t%D0,%2\n\tlw\t%0,%1"
2353                      : "lw\t%0,%1\n\tlw\t%D0,%2");
2354             }
2355
2356           if (ret != 0 && MEM_VOLATILE_P (op1))
2357             {
2358               size_t i = strlen (ret);
2359
2360               if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2361                 abort ();
2362
2363               sprintf (volatile_buffer, "%%{%s%%}", ret);
2364               ret = volatile_buffer;
2365             }
2366         }
2367
2368       else if (code1 == LABEL_REF)
2369         {
2370           if (TARGET_STATS)
2371             mips_count_memory_refs (op1, 2);
2372
2373           if (GET_CODE (operands[1]) == SIGN_EXTEND)
2374             /* We deliberately remove the 'a' from '%1', so that we don't
2375                have to add SIGN_EXTEND support to print_operand_address.
2376                print_operand will just call print_operand_address in this
2377                case, so there is no problem.  */
2378             ret = "la\t%0,%1";
2379           else
2380             ret = "dla\t%0,%a1";
2381         }
2382       else if (code1 == SYMBOL_REF || code1 == CONST)
2383         {
2384           if (TARGET_MIPS16
2385               && code1 == CONST
2386               && GET_CODE (XEXP (op1, 0)) == REG
2387               && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2388             {
2389               /* This case arises on the mips16; see
2390                  mips16_gp_pseudo_reg.  */
2391               ret = "move\t%0,%+";
2392             }
2393           else if (TARGET_MIPS16
2394                    && code1 == SYMBOL_REF
2395                    && SYMBOL_REF_FLAG (op1)
2396                    && (XSTR (op1, 0)[0] != '*'
2397                        || strncmp (XSTR (op1, 0) + 1,
2398                                    LOCAL_LABEL_PREFIX,
2399                                    sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2400             {
2401               /* This can occur when reloading the address of a GP
2402                  relative symbol on the mips16.  */
2403               ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2404             }
2405           else
2406             {
2407               if (TARGET_STATS)
2408                 mips_count_memory_refs (op1, 2);
2409
2410               if (GET_CODE (operands[1]) == SIGN_EXTEND)
2411                 /* We deliberately remove the 'a' from '%1', so that we don't
2412                    have to add SIGN_EXTEND support to print_operand_address.
2413                    print_operand will just call print_operand_address in this
2414                    case, so there is no problem.  */
2415                 ret = "la\t%0,%1";
2416               else
2417                 ret = "dla\t%0,%a1";
2418             }
2419         }
2420     }
2421
2422   else if (code0 == MEM)
2423     {
2424       if (code1 == REG)
2425         {
2426           int regno1 = REGNO (op1) + subreg_word1;
2427
2428           if (FP_REG_P (regno1))
2429             ret = "s.d\t%1,%0";
2430
2431           else if (TARGET_64BIT)
2432             {
2433
2434 #ifdef TARGET_FP_CALL_32
2435               if (FP_CALL_GP_REG_P (regno1))
2436                 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tsd\t%1,%0";
2437               else
2438 #endif
2439                 ret = "sd\t%1,%0";
2440             }
2441
2442           else if (double_memory_operand (op0, GET_MODE (op0)))
2443             {
2444               operands[2] = adj_offsettable_operand (op0, 4);
2445               ret = "sw\t%1,%0\n\tsw\t%D1,%2";
2446             }
2447         }
2448
2449       else if (((code1 == CONST_INT && INTVAL (op1) == 0)
2450                 || (code1 == CONST_DOUBLE
2451                     && op1 == CONST0_RTX (GET_MODE (op1))))
2452                && (TARGET_64BIT
2453                    || double_memory_operand (op0, GET_MODE (op0))))
2454         {
2455           if (TARGET_64BIT)
2456             ret = "sd\t%.,%0";
2457           else
2458             {
2459               operands[2] = adj_offsettable_operand (op0, 4);
2460               ret = "sw\t%.,%0\n\tsw\t%.,%2";
2461             }
2462         }
2463
2464       if (TARGET_STATS)
2465         mips_count_memory_refs (op0, 2);
2466
2467       if (ret != 0 && MEM_VOLATILE_P (op0))
2468         {
2469           size_t i = strlen (ret);
2470
2471           if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2472             abort ();
2473           
2474           sprintf (volatile_buffer, "%%{%s%%}", ret);
2475           ret = volatile_buffer;
2476         }
2477     }
2478
2479   if (ret == 0)
2480     {
2481       abort_with_insn (insn, "Bad move");
2482       return 0;
2483     }
2484
2485   if (delay != DELAY_NONE)
2486     return mips_fill_delay_slot (ret, delay, operands, insn);
2487
2488   return ret;
2489 }
2490 \f
2491 /* Provide the costs of an addressing mode that contains ADDR.
2492    If ADDR is not a valid address, its cost is irrelevant.  */
2493
2494 int
2495 mips_address_cost (addr)
2496      rtx addr;
2497 {
2498   switch (GET_CODE (addr))
2499     {
2500     case LO_SUM:
2501       return 1;
2502
2503     case LABEL_REF:
2504       return 2;
2505
2506     case CONST:
2507       {
2508         rtx offset = const0_rtx;
2509         addr = eliminate_constant_term (XEXP (addr, 0), &offset);
2510         if (GET_CODE (addr) == LABEL_REF)
2511           return 2;
2512
2513         if (GET_CODE (addr) != SYMBOL_REF)
2514           return 4;
2515
2516         if (! SMALL_INT (offset))
2517           return 2;
2518       }
2519
2520       /* ... fall through ... */
2521
2522     case SYMBOL_REF:
2523       return SYMBOL_REF_FLAG (addr) ? 1 : 2;
2524
2525     case PLUS:
2526       {
2527         register rtx plus0 = XEXP (addr, 0);
2528         register rtx plus1 = XEXP (addr, 1);
2529
2530         if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
2531           plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
2532
2533         if (GET_CODE (plus0) != REG)
2534           break;
2535
2536         switch (GET_CODE (plus1))
2537           {
2538           case CONST_INT:
2539             return SMALL_INT (plus1) ? 1 : 2;
2540
2541           case CONST:
2542           case SYMBOL_REF:
2543           case LABEL_REF:
2544           case HIGH:
2545           case LO_SUM:
2546             return mips_address_cost (plus1) + 1;
2547
2548           default:
2549             break;
2550           }
2551       }
2552
2553     default:
2554       break;
2555     }
2556
2557   return 4;
2558 }
2559
2560 /* Return nonzero if X is an address which needs a temporary register when 
2561    reloaded while generating PIC code.  */
2562
2563 int
2564 pic_address_needs_scratch (x)
2565      rtx x;
2566 {
2567   /* An address which is a symbolic plus a non SMALL_INT needs a temp reg.  */
2568   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
2569       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
2570       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2571       && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
2572     return 1;
2573
2574   return 0;
2575 }
2576 \f
2577 /* Make normal rtx_code into something we can index from an array */
2578
2579 static enum internal_test
2580 map_test_to_internal_test (test_code)
2581      enum rtx_code test_code;
2582 {
2583   enum internal_test test = ITEST_MAX;
2584
2585   switch (test_code)
2586     {
2587     case EQ:  test = ITEST_EQ;  break;
2588     case NE:  test = ITEST_NE;  break;
2589     case GT:  test = ITEST_GT;  break;
2590     case GE:  test = ITEST_GE;  break;
2591     case LT:  test = ITEST_LT;  break;
2592     case LE:  test = ITEST_LE;  break;
2593     case GTU: test = ITEST_GTU; break;
2594     case GEU: test = ITEST_GEU; break;
2595     case LTU: test = ITEST_LTU; break;
2596     case LEU: test = ITEST_LEU; break;
2597     default:                    break;
2598     }
2599
2600   return test;
2601 }
2602
2603 \f
2604 /* Generate the code to compare two integer values.  The return value is:
2605    (reg:SI xx)          The pseudo register the comparison is in
2606    0                    No register, generate a simple branch.
2607
2608    ??? This is called with result nonzero by the Scond patterns in
2609    mips.md.  These patterns are called with a target in the mode of
2610    the Scond instruction pattern.  Since this must be a constant, we
2611    must use SImode.  This means that if RESULT is non-zero, it will
2612    always be an SImode register, even if TARGET_64BIT is true.  We
2613    cope with this by calling convert_move rather than emit_move_insn.
2614    This will sometimes lead to an unnecessary extension of the result;
2615    for example:
2616
2617    long long
2618    foo (long long i)
2619    {
2620      return i < 5;
2621    }
2622
2623    */
2624
2625 rtx
2626 gen_int_relational (test_code, result, cmp0, cmp1, p_invert)
2627      enum rtx_code test_code;   /* relational test (EQ, etc) */
2628      rtx result;                /* result to store comp. or 0 if branch */
2629      rtx cmp0;                  /* first operand to compare */
2630      rtx cmp1;                  /* second operand to compare */
2631      int *p_invert;             /* NULL or ptr to hold whether branch needs */
2632                                 /* to reverse its test */
2633 {
2634   struct cmp_info
2635   {
2636     enum rtx_code test_code;    /* code to use in instruction (LT vs. LTU) */
2637     int const_low;              /* low bound of constant we can accept */
2638     int const_high;             /* high bound of constant we can accept */
2639     int const_add;              /* constant to add (convert LE -> LT) */
2640     int reverse_regs;           /* reverse registers in test */
2641     int invert_const;           /* != 0 if invert value if cmp1 is constant */
2642     int invert_reg;             /* != 0 if invert value if cmp1 is register */
2643     int unsignedp;              /* != 0 for unsigned comparisons.  */
2644   };
2645
2646   static struct cmp_info info[ (int)ITEST_MAX ] = {
2647
2648     { XOR,       0,  65535,  0,  0,  0,  0, 0 },        /* EQ  */
2649     { XOR,       0,  65535,  0,  0,  1,  1, 0 },        /* NE  */
2650     { LT,   -32769,  32766,  1,  1,  1,  0, 0 },        /* GT  */
2651     { LT,   -32768,  32767,  0,  0,  1,  1, 0 },        /* GE  */
2652     { LT,   -32768,  32767,  0,  0,  0,  0, 0 },        /* LT  */
2653     { LT,   -32769,  32766,  1,  1,  0,  1, 0 },        /* LE  */
2654     { LTU,  -32769,  32766,  1,  1,  1,  0, 1 },        /* GTU */
2655     { LTU,  -32768,  32767,  0,  0,  1,  1, 1 },        /* GEU */
2656     { LTU,  -32768,  32767,  0,  0,  0,  0, 1 },        /* LTU */
2657     { LTU,  -32769,  32766,  1,  1,  0,  1, 1 },        /* LEU */
2658   };
2659
2660   enum internal_test test;
2661   enum machine_mode mode;
2662   struct cmp_info *p_info;
2663   int branch_p;
2664   int eqne_p;
2665   int invert;
2666   rtx reg;
2667   rtx reg2;
2668
2669   test = map_test_to_internal_test (test_code);
2670   if (test == ITEST_MAX)
2671     abort ();
2672
2673   p_info = &info[(int) test];
2674   eqne_p = (p_info->test_code == XOR);
2675
2676   mode = GET_MODE (cmp0);
2677   if (mode == VOIDmode)
2678     mode = GET_MODE (cmp1);
2679
2680   /* Eliminate simple branches */
2681   branch_p = (result == 0);
2682   if (branch_p)
2683     {
2684       if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
2685         {
2686           /* Comparisons against zero are simple branches */
2687           if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
2688               && (! TARGET_MIPS16 || eqne_p))
2689             return 0;
2690
2691           /* Test for beq/bne.  */
2692           if (eqne_p && ! TARGET_MIPS16)
2693             return 0;
2694         }
2695
2696       /* allocate a pseudo to calculate the value in.  */
2697       result = gen_reg_rtx (mode);
2698     }
2699
2700   /* Make sure we can handle any constants given to us.  */
2701   if (GET_CODE (cmp0) == CONST_INT)
2702     cmp0 = force_reg (mode, cmp0);
2703
2704   if (GET_CODE (cmp1) == CONST_INT)
2705     {
2706       HOST_WIDE_INT value = INTVAL (cmp1);
2707
2708       if (value < p_info->const_low
2709           || value > p_info->const_high
2710           /* ??? Why?  And why wasn't the similar code below modified too?  */
2711           || (TARGET_64BIT
2712               && HOST_BITS_PER_WIDE_INT < 64
2713               && p_info->const_add != 0
2714               && ((p_info->unsignedp
2715                    ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
2716                       > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
2717                    : (value + p_info->const_add) > INTVAL (cmp1))
2718                   != (p_info->const_add > 0))))
2719         cmp1 = force_reg (mode, cmp1);
2720     }
2721
2722   /* See if we need to invert the result.  */
2723   invert = (GET_CODE (cmp1) == CONST_INT
2724             ? p_info->invert_const : p_info->invert_reg);
2725
2726   if (p_invert != (int *)0)
2727     {
2728       *p_invert = invert;
2729       invert = 0;
2730     }
2731
2732   /* Comparison to constants, may involve adding 1 to change a LT into LE.
2733      Comparison between two registers, may involve switching operands.  */
2734   if (GET_CODE (cmp1) == CONST_INT)
2735     {
2736       if (p_info->const_add != 0)
2737         {
2738           HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
2739
2740           /* If modification of cmp1 caused overflow,
2741              we would get the wrong answer if we follow the usual path;
2742              thus, x > 0xffffffffU would turn into x > 0U.  */
2743           if ((p_info->unsignedp
2744                ? (unsigned HOST_WIDE_INT) new >
2745                (unsigned HOST_WIDE_INT) INTVAL (cmp1)
2746                : new > INTVAL (cmp1))
2747               != (p_info->const_add > 0))
2748             {
2749               /* This test is always true, but if INVERT is true then
2750                  the result of the test needs to be inverted so 0 should
2751                  be returned instead.  */
2752               emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
2753               return result;
2754             }
2755           else
2756             cmp1 = GEN_INT (new);
2757         }
2758     }
2759
2760   else if (p_info->reverse_regs)
2761     {
2762       rtx temp = cmp0;
2763       cmp0 = cmp1;
2764       cmp1 = temp;
2765     }
2766
2767   if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
2768     reg = cmp0;
2769   else
2770     {
2771       reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
2772       convert_move (reg, gen_rtx (p_info->test_code, mode, cmp0, cmp1), 0);
2773     }
2774
2775   if (test == ITEST_NE)
2776     {
2777       if (! TARGET_MIPS16)
2778         {
2779           convert_move (result, gen_rtx (GTU, mode, reg, const0_rtx), 0);
2780           invert = 0;
2781         }
2782       else
2783         {
2784           reg2 = invert ? gen_reg_rtx (mode) : result;
2785           convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
2786           reg = reg2;
2787         }
2788     }
2789
2790   else if (test == ITEST_EQ)
2791     {
2792       reg2 = invert ? gen_reg_rtx (mode) : result;
2793       convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
2794       reg = reg2;
2795     }
2796
2797   if (invert)
2798     {
2799       rtx one;
2800
2801       if (! TARGET_MIPS16)
2802         one = const1_rtx;
2803       else
2804         {
2805           /* The value is in $24.  Copy it to another register, so
2806              that reload doesn't think it needs to store the $24 and
2807              the input to the XOR in the same location.  */
2808           reg2 = gen_reg_rtx (mode);
2809           emit_move_insn (reg2, reg);
2810           reg = reg2;
2811           one = force_reg (mode, const1_rtx);
2812         }
2813       convert_move (result, gen_rtx (XOR, mode, reg, one), 0);
2814     }
2815
2816   return result;
2817 }
2818 \f
2819 /* Emit the common code for doing conditional branches.
2820    operand[0] is the label to jump to.
2821    The comparison operands are saved away by cmp{si,di,sf,df}.  */
2822
2823 void
2824 gen_conditional_branch (operands, test_code)
2825      rtx operands[];
2826      enum rtx_code test_code;
2827 {
2828   enum cmp_type type = branch_type;
2829   rtx cmp0 = branch_cmp[0];
2830   rtx cmp1 = branch_cmp[1];
2831   enum machine_mode mode;
2832   rtx reg;
2833   int invert;
2834   rtx label1, label2;
2835
2836   switch (type)
2837     {
2838     case CMP_SI:
2839     case CMP_DI:
2840       mode = type == CMP_SI ? SImode : DImode;
2841       invert = 0;
2842       reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
2843
2844       if (reg)
2845         {
2846           cmp0 = reg;
2847           cmp1 = const0_rtx;
2848           test_code = NE;
2849         }
2850       else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
2851         /* We don't want to build a comparison against a non-zero
2852            constant.  */
2853         cmp1 = force_reg (mode, cmp1);
2854
2855       break;
2856
2857     case CMP_SF:
2858     case CMP_DF:
2859       if (mips_isa < 4)
2860         reg = gen_rtx (REG, CCmode, FPSW_REGNUM);
2861       else
2862         reg = gen_reg_rtx (CCmode);
2863
2864       /* For cmp0 != cmp1, build cmp0 == cmp1, and test for result ==
2865          0 in the instruction built below.  The MIPS FPU handles
2866          inequality testing by testing for equality and looking for a
2867          false result.  */
2868       emit_insn (gen_rtx (SET, VOIDmode, reg,
2869                           gen_rtx (test_code == NE ? EQ : test_code,
2870                                    CCmode, cmp0, cmp1)));
2871
2872       test_code = test_code == NE ? EQ : NE;
2873       mode = CCmode;
2874       cmp0 = reg;
2875       cmp1 = const0_rtx;
2876       invert = 0;
2877       break;
2878
2879     default:
2880       abort_with_insn (gen_rtx (test_code, VOIDmode, cmp0, cmp1), "bad test");
2881     }
2882
2883   /* Generate the branch.  */
2884
2885   label1 = gen_rtx (LABEL_REF, VOIDmode, operands[0]);
2886   label2 = pc_rtx;
2887
2888   if (invert)
2889     {
2890       label2 = label1;
2891       label1 = pc_rtx;
2892     }
2893
2894   emit_jump_insn (gen_rtx (SET, VOIDmode, pc_rtx,
2895                            gen_rtx (IF_THEN_ELSE, VOIDmode,
2896                                     gen_rtx (test_code, mode, cmp0, cmp1),
2897                                     label1, label2)));
2898 }
2899
2900 /* Emit the common code for conditional moves.  OPERANDS is the array
2901    of operands passed to the conditional move defined_expand.  */
2902
2903 void
2904 gen_conditional_move (operands)
2905      rtx *operands;
2906 {
2907   rtx op0 = branch_cmp[0];
2908   rtx op1 = branch_cmp[1];
2909   enum machine_mode mode = GET_MODE (branch_cmp[0]);
2910   enum rtx_code cmp_code = GET_CODE (operands[1]);
2911   enum rtx_code move_code = NE;
2912   enum machine_mode op_mode = GET_MODE (operands[0]);
2913   enum machine_mode cmp_mode;
2914   rtx cmp_reg;
2915
2916   if (GET_MODE_CLASS (mode) != MODE_FLOAT)
2917     {
2918       switch (cmp_code)
2919         {
2920         case EQ:
2921           cmp_code = XOR;
2922           move_code = EQ;
2923           break;
2924         case NE:
2925           cmp_code = XOR;
2926           break;
2927         case LT:
2928           break;
2929         case GE:
2930           cmp_code = LT;
2931           move_code = EQ;
2932           break;
2933         case GT:
2934           cmp_code = LT;
2935           op0 = force_reg (mode, branch_cmp[1]);
2936           op1 = branch_cmp[0];
2937           break;
2938         case LE:
2939           cmp_code = LT;
2940           op0 = force_reg (mode, branch_cmp[1]);
2941           op1 = branch_cmp[0];
2942           move_code = EQ;
2943           break;
2944         case LTU:
2945           break;
2946         case GEU:
2947           cmp_code = LTU;
2948           move_code = EQ;
2949           break;
2950         case GTU:
2951           cmp_code = LTU;
2952           op0 = force_reg (mode, branch_cmp[1]);
2953           op1 = branch_cmp[0];
2954           break;
2955         case LEU:
2956           cmp_code = LTU;
2957           op0 = force_reg (mode, branch_cmp[1]);
2958           op1 = branch_cmp[0];
2959           move_code = EQ;
2960           break;
2961         default:
2962           abort ();
2963         }
2964     }
2965   else if (cmp_code == NE)
2966     cmp_code = EQ, move_code = EQ;
2967           
2968   if (mode == SImode || mode == DImode)
2969     cmp_mode = mode;
2970   else if (mode == SFmode || mode == DFmode)
2971     cmp_mode = CCmode;
2972   else
2973     abort ();
2974
2975   cmp_reg = gen_reg_rtx (cmp_mode);
2976   emit_insn (gen_rtx (SET, cmp_mode, cmp_reg,
2977                       gen_rtx (cmp_code, cmp_mode, op0, op1)));
2978
2979   emit_insn (gen_rtx (SET, op_mode, operands[0],
2980                       gen_rtx (IF_THEN_ELSE, op_mode,
2981                                gen_rtx (move_code, VOIDmode,
2982                                         cmp_reg, CONST0_RTX (SImode)),
2983                                operands[2], operands[3])));
2984 }
2985 \f
2986 /* Write a loop to move a constant number of bytes.
2987    Generate load/stores as follows:
2988
2989    do {
2990      temp1 = src[0];
2991      temp2 = src[1];
2992      ...
2993      temp<last> = src[MAX_MOVE_REGS-1];
2994      dest[0] = temp1;
2995      dest[1] = temp2;
2996      ...
2997      dest[MAX_MOVE_REGS-1] = temp<last>;
2998      src += MAX_MOVE_REGS;
2999      dest += MAX_MOVE_REGS;
3000    } while (src != final);
3001
3002    This way, no NOP's are needed, and only MAX_MOVE_REGS+3 temp
3003    registers are needed.
3004
3005    Aligned moves move MAX_MOVE_REGS*4 bytes every (2*MAX_MOVE_REGS)+3
3006    cycles, unaligned moves move MAX_MOVE_REGS*4 bytes every
3007    (4*MAX_MOVE_REGS)+3 cycles, assuming no cache misses.  */
3008
3009 #define MAX_MOVE_REGS 4
3010 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3011
3012 static void
3013 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src)
3014      rtx dest_reg;              /* register holding destination address */
3015      rtx src_reg;               /* register holding source address */
3016      int bytes;                 /* # bytes to move */
3017      int align;                 /* alignment */
3018      rtx orig_dest;             /* original dest for change_address */
3019      rtx orig_src;              /* original source for making a reg note */
3020 {
3021   rtx dest_mem = change_address (orig_dest, BLKmode, dest_reg);
3022   rtx src_mem = change_address (orig_src, BLKmode, src_reg);
3023   rtx align_rtx = GEN_INT (align);
3024   rtx label;
3025   rtx final_src;
3026   rtx bytes_rtx;
3027   int leftover;
3028
3029   if (bytes < 2 * MAX_MOVE_BYTES)
3030     abort ();
3031
3032   leftover = bytes % MAX_MOVE_BYTES;
3033   bytes -= leftover;
3034
3035   label = gen_label_rtx ();
3036   final_src = gen_reg_rtx (Pmode);
3037   bytes_rtx = GEN_INT (bytes);
3038
3039   if (bytes > 0x7fff)
3040     {
3041       if (Pmode == DImode)
3042         {
3043           emit_insn (gen_movdi (final_src, bytes_rtx));
3044           emit_insn (gen_adddi3 (final_src, final_src, src_reg));
3045         }
3046       else
3047         {
3048           emit_insn (gen_movsi (final_src, bytes_rtx));
3049           emit_insn (gen_addsi3 (final_src, final_src, src_reg));
3050         }
3051     }
3052   else
3053     {
3054       if (Pmode == DImode)
3055         emit_insn (gen_adddi3 (final_src, src_reg, bytes_rtx));
3056       else
3057         emit_insn (gen_addsi3 (final_src, src_reg, bytes_rtx));
3058     }
3059
3060   emit_label (label);
3061
3062   bytes_rtx = GEN_INT (MAX_MOVE_BYTES);
3063   emit_insn (gen_movstrsi_internal (dest_mem, src_mem, bytes_rtx, align_rtx));
3064
3065   if (Pmode == DImode)
3066     {
3067       emit_insn (gen_adddi3 (src_reg, src_reg, bytes_rtx));
3068       emit_insn (gen_adddi3 (dest_reg, dest_reg, bytes_rtx));
3069       emit_insn (gen_cmpdi (src_reg, final_src));
3070     }
3071   else
3072     {
3073       emit_insn (gen_addsi3 (src_reg, src_reg, bytes_rtx));
3074       emit_insn (gen_addsi3 (dest_reg, dest_reg, bytes_rtx));
3075       emit_insn (gen_cmpsi (src_reg, final_src));
3076     }
3077
3078   emit_jump_insn (gen_bne (label));
3079
3080   if (leftover)
3081     emit_insn (gen_movstrsi_internal (dest_mem, src_mem, GEN_INT (leftover),
3082                                       align_rtx));
3083 }
3084 \f
3085 /* Use a library function to move some bytes.  */
3086
3087 static void
3088 block_move_call (dest_reg, src_reg, bytes_rtx)
3089      rtx dest_reg;
3090      rtx src_reg;
3091      rtx bytes_rtx;
3092 {
3093   /* We want to pass the size as Pmode, which will normally be SImode
3094      but will be DImode if we are using 64 bit longs and pointers.  */
3095   if (GET_MODE (bytes_rtx) != VOIDmode
3096       && GET_MODE (bytes_rtx) != Pmode)
3097     bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
3098
3099 #ifdef TARGET_MEM_FUNCTIONS
3100   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "memcpy"), 0,
3101                      VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
3102                      convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
3103                                       TREE_UNSIGNED (sizetype)),
3104                      TYPE_MODE (sizetype));
3105 #else
3106   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "bcopy"), 0,
3107                      VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
3108                      convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
3109                                       TREE_UNSIGNED (integer_type_node)),
3110                      TYPE_MODE (integer_type_node));
3111 #endif
3112 }
3113 \f
3114 /* Expand string/block move operations.
3115
3116    operands[0] is the pointer to the destination.
3117    operands[1] is the pointer to the source.
3118    operands[2] is the number of bytes to move.
3119    operands[3] is the alignment.  */
3120
3121 void
3122 expand_block_move (operands)
3123      rtx operands[];
3124 {
3125   rtx bytes_rtx = operands[2];
3126   rtx align_rtx = operands[3];
3127   int constp = GET_CODE (bytes_rtx) == CONST_INT;
3128   HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
3129   int align = INTVAL (align_rtx);
3130   rtx orig_src  = operands[1];
3131   rtx orig_dest = operands[0];
3132   rtx src_reg;
3133   rtx dest_reg;
3134
3135   if (constp && bytes <= 0)
3136     return;
3137
3138   if (align > UNITS_PER_WORD)
3139     align = UNITS_PER_WORD;
3140
3141   /* Move the address into scratch registers.  */
3142   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
3143   src_reg  = copy_addr_to_reg (XEXP (orig_src, 0));
3144
3145   if (TARGET_MEMCPY)
3146     block_move_call (dest_reg, src_reg, bytes_rtx);
3147
3148   else if (constp && bytes <= 2 * MAX_MOVE_BYTES
3149            && align == UNITS_PER_WORD)
3150     move_by_pieces (orig_dest, orig_src, bytes, align);
3151         
3152   else if (constp && bytes <= 2 * MAX_MOVE_BYTES)
3153     emit_insn (gen_movstrsi_internal (change_address (orig_dest, BLKmode,
3154                                                       dest_reg),
3155                                       change_address (orig_src, BLKmode,
3156                                                       src_reg),
3157                                       bytes_rtx, align_rtx));
3158
3159   else if (constp && align >= UNITS_PER_WORD && optimize)
3160     block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src);
3161
3162   else if (constp && optimize)
3163     {
3164       /* If the alignment is not word aligned, generate a test at
3165          runtime, to see whether things wound up aligned, and we
3166          can use the faster lw/sw instead ulw/usw.  */
3167
3168       rtx temp = gen_reg_rtx (Pmode);
3169       rtx aligned_label = gen_label_rtx ();
3170       rtx join_label = gen_label_rtx ();
3171       int leftover = bytes % MAX_MOVE_BYTES;
3172
3173       bytes -= leftover;
3174
3175       if (Pmode == DImode)
3176         {
3177           emit_insn (gen_iordi3 (temp, src_reg, dest_reg));
3178           emit_insn (gen_anddi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3179           emit_insn (gen_cmpdi (temp, const0_rtx));
3180         }
3181       else
3182         {
3183           emit_insn (gen_iorsi3 (temp, src_reg, dest_reg));
3184           emit_insn (gen_andsi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3185           emit_insn (gen_cmpsi (temp, const0_rtx));
3186         }
3187
3188       emit_jump_insn (gen_beq (aligned_label));
3189
3190       /* Unaligned loop.  */
3191       block_move_loop (dest_reg, src_reg, bytes, 1, orig_dest, orig_src);
3192       emit_jump_insn (gen_jump (join_label));
3193       emit_barrier ();
3194
3195       /* Aligned loop.  */
3196       emit_label (aligned_label);
3197       block_move_loop (dest_reg, src_reg, bytes, UNITS_PER_WORD, orig_dest,
3198                        orig_src);
3199       emit_label (join_label);
3200
3201       /* Bytes at the end of the loop.  */
3202       if (leftover)
3203         emit_insn (gen_movstrsi_internal (change_address (orig_dest, BLKmode,
3204                                                           dest_reg),
3205                                           change_address (orig_src, BLKmode,
3206                                                           src_reg),
3207                                           GEN_INT (leftover),
3208                                           GEN_INT (align)));
3209     }
3210
3211   else
3212     block_move_call (dest_reg, src_reg, bytes_rtx);
3213 }
3214 \f
3215 /* Emit load/stores for a small constant block_move. 
3216
3217    operands[0] is the memory address of the destination.
3218    operands[1] is the memory address of the source.
3219    operands[2] is the number of bytes to move.
3220    operands[3] is the alignment.
3221    operands[4] is a temp register.
3222    operands[5] is a temp register.
3223    ...
3224    operands[3+num_regs] is the last temp register.
3225
3226    The block move type can be one of the following:
3227         BLOCK_MOVE_NORMAL       Do all of the block move.
3228         BLOCK_MOVE_NOT_LAST     Do all but the last store.
3229         BLOCK_MOVE_LAST         Do just the last store. */
3230
3231 const char *
3232 output_block_move (insn, operands, num_regs, move_type)
3233      rtx insn;
3234      rtx operands[];
3235      int num_regs;
3236      enum block_move_type move_type;
3237 {
3238   rtx dest_reg = XEXP (operands[0], 0);
3239   rtx src_reg = XEXP (operands[1], 0);
3240   HOST_WIDE_INT bytes = INTVAL (operands[2]);
3241   int align = INTVAL (operands[3]);
3242   int num = 0;
3243   int offset = 0;
3244   int use_lwl_lwr = 0;
3245   int last_operand = num_regs + 4;
3246   int safe_regs = 4;
3247   int i;
3248   rtx xoperands[10];
3249
3250   struct {
3251     const char *load;           /* load insn without nop */
3252     const char *load_nop;       /* load insn with trailing nop */
3253     const char *store;          /* store insn */
3254     const char *final;          /* if last_store used: NULL or swr */
3255     const char *last_store;     /* last store instruction */
3256     int offset;                 /* current offset */
3257     enum machine_mode mode;     /* mode to use on (MEM) */
3258   } load_store[4];
3259
3260   /* ??? Detect a bug in GCC, where it can give us a register
3261      the same as one of the addressing registers and reduce
3262      the number of registers available.  */
3263   for (i = 4;
3264        i < last_operand
3265        && safe_regs < (int)(sizeof(xoperands) / sizeof(xoperands[0]));
3266        i++)
3267     if (! reg_mentioned_p (operands[i], operands[0])
3268         && ! reg_mentioned_p (operands[i], operands[1]))
3269       xoperands[safe_regs++] = operands[i];
3270
3271   if (safe_regs < last_operand)
3272     {
3273       xoperands[0] = operands[0];
3274       xoperands[1] = operands[1];
3275       xoperands[2] = operands[2];
3276       xoperands[3] = operands[3];
3277       return output_block_move (insn, xoperands, safe_regs - 4, move_type);
3278     }
3279
3280   /* If we are given global or static addresses, and we would be
3281      emitting a few instructions, try to save time by using a
3282      temporary register for the pointer.  */
3283   /* ??? The SGI Irix6 assembler fails when a SYMBOL_REF is used in
3284      an ldl/ldr instruction pair.  We play it safe, and always move
3285      constant addresses into registers when generating N32/N64 code, just
3286      in case we might emit an unaligned load instruction.  */
3287   if (num_regs > 2 && (bytes > 2 * align || move_type != BLOCK_MOVE_NORMAL
3288                        || mips_abi == ABI_N32 || mips_abi == ABI_64))
3289     {
3290       if (CONSTANT_P (src_reg))
3291         {
3292           if (TARGET_STATS)
3293             mips_count_memory_refs (operands[1], 1);
3294
3295           src_reg = operands[3 + num_regs--];
3296           if (move_type != BLOCK_MOVE_LAST)
3297             {
3298               xoperands[1] = operands[1];
3299               xoperands[0] = src_reg;
3300               if (Pmode == DImode)
3301                 output_asm_insn ("dla\t%0,%1", xoperands);
3302               else
3303                 output_asm_insn ("la\t%0,%1", xoperands);
3304             }
3305         }
3306
3307       if (CONSTANT_P (dest_reg))
3308         {
3309           if (TARGET_STATS)
3310             mips_count_memory_refs (operands[0], 1);
3311
3312           dest_reg = operands[3 + num_regs--];
3313           if (move_type != BLOCK_MOVE_LAST)
3314             {
3315               xoperands[1] = operands[0];
3316               xoperands[0] = dest_reg;
3317               if (Pmode == DImode)
3318                 output_asm_insn ("dla\t%0,%1", xoperands);
3319               else
3320                 output_asm_insn ("la\t%0,%1", xoperands);
3321             }
3322         }
3323     }
3324
3325   /* ??? We really shouldn't get any LO_SUM addresses here, because they
3326      are not offsettable, however, offsettable_address_p says they are
3327      offsettable. I think this is a bug in offsettable_address_p.
3328      For expediency, we fix this by just loading the address into a register
3329      if we happen to get one.  */
3330
3331   if (GET_CODE (src_reg) == LO_SUM)
3332     {
3333       src_reg = operands[3 + num_regs--];
3334       if (move_type != BLOCK_MOVE_LAST)
3335         {
3336           xoperands[2] = XEXP (XEXP (operands[1], 0), 1);
3337           xoperands[1] = XEXP (XEXP (operands[1], 0), 0);
3338           xoperands[0] = src_reg;
3339           if (Pmode == DImode)
3340             output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3341           else
3342             output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3343         }
3344     }
3345
3346   if (GET_CODE (dest_reg) == LO_SUM)
3347     {
3348       dest_reg = operands[3 + num_regs--];
3349       if (move_type != BLOCK_MOVE_LAST)
3350         {
3351           xoperands[2] = XEXP (XEXP (operands[0], 0), 1);
3352           xoperands[1] = XEXP (XEXP (operands[0], 0), 0);
3353           xoperands[0] = dest_reg;
3354           if (Pmode == DImode)
3355             output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3356           else
3357             output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3358         }
3359     }
3360
3361   if (num_regs > (int)(sizeof (load_store) / sizeof (load_store[0])))
3362     num_regs = sizeof (load_store) / sizeof (load_store[0]);
3363
3364   else if (num_regs < 1)
3365     abort_with_insn (insn,
3366                      "Cannot do block move, not enough scratch registers");
3367
3368   while (bytes > 0)
3369     {
3370       load_store[num].offset = offset;
3371
3372       if (TARGET_64BIT && bytes >= 8 && align >= 8)
3373         {
3374           load_store[num].load = "ld\t%0,%1";
3375           load_store[num].load_nop = "ld\t%0,%1%#";
3376           load_store[num].store = "sd\t%0,%1";
3377           load_store[num].last_store = "sd\t%0,%1";
3378           load_store[num].final = 0;
3379           load_store[num].mode = DImode;
3380           offset += 8;
3381           bytes -= 8;
3382         }
3383
3384       /* ??? Fails because of a MIPS assembler bug?  */
3385       else if (TARGET_64BIT && bytes >= 8 && ! TARGET_MIPS16)
3386         {
3387           if (BYTES_BIG_ENDIAN)
3388             {
3389               load_store[num].load = "ldl\t%0,%1\n\tldr\t%0,%2";
3390               load_store[num].load_nop = "ldl\t%0,%1\n\tldr\t%0,%2%#";
3391               load_store[num].store = "sdl\t%0,%1\n\tsdr\t%0,%2";
3392               load_store[num].last_store = "sdr\t%0,%2";
3393               load_store[num].final = "sdl\t%0,%1";
3394             }
3395           else
3396             {
3397               load_store[num].load = "ldl\t%0,%2\n\tldr\t%0,%1";
3398               load_store[num].load_nop = "ldl\t%0,%2\n\tldr\t%0,%1%#";
3399               load_store[num].store = "sdl\t%0,%2\n\tsdr\t%0,%1";
3400               load_store[num].last_store = "sdr\t%0,%1";
3401               load_store[num].final = "sdl\t%0,%2";
3402             }
3403
3404           load_store[num].mode = DImode;
3405           offset += 8;
3406           bytes -= 8;
3407           use_lwl_lwr = 1;
3408         }
3409
3410       else if (bytes >= 4 && align >= 4)
3411         {
3412           load_store[num].load = "lw\t%0,%1";
3413           load_store[num].load_nop = "lw\t%0,%1%#";
3414           load_store[num].store = "sw\t%0,%1";
3415           load_store[num].last_store = "sw\t%0,%1";
3416           load_store[num].final = 0;
3417           load_store[num].mode = SImode;
3418           offset += 4;
3419           bytes -= 4;
3420         }
3421
3422       else if (bytes >= 4 && ! TARGET_MIPS16)
3423         {
3424           if (BYTES_BIG_ENDIAN)
3425             {
3426               load_store[num].load = "lwl\t%0,%1\n\tlwr\t%0,%2";
3427               load_store[num].load_nop = "lwl\t%0,%1\n\tlwr\t%0,%2%#";
3428               load_store[num].store = "swl\t%0,%1\n\tswr\t%0,%2";
3429               load_store[num].last_store = "swr\t%0,%2";
3430               load_store[num].final = "swl\t%0,%1";
3431             }
3432           else
3433             {
3434               load_store[num].load = "lwl\t%0,%2\n\tlwr\t%0,%1";
3435               load_store[num].load_nop = "lwl\t%0,%2\n\tlwr\t%0,%1%#";
3436               load_store[num].store = "swl\t%0,%2\n\tswr\t%0,%1";
3437               load_store[num].last_store = "swr\t%0,%1";
3438               load_store[num].final = "swl\t%0,%2";
3439             }
3440
3441           load_store[num].mode = SImode;
3442           offset += 4;
3443           bytes -= 4;
3444           use_lwl_lwr = 1;
3445         }
3446
3447       else if (bytes >= 2 && align >= 2)
3448         {
3449           load_store[num].load = "lh\t%0,%1";
3450           load_store[num].load_nop = "lh\t%0,%1%#";
3451           load_store[num].store = "sh\t%0,%1";
3452           load_store[num].last_store = "sh\t%0,%1";
3453           load_store[num].final = 0;
3454           load_store[num].mode = HImode;
3455           offset += 2;
3456           bytes -= 2;
3457         }
3458       else
3459         {
3460           load_store[num].load = "lb\t%0,%1";
3461           load_store[num].load_nop = "lb\t%0,%1%#";
3462           load_store[num].store = "sb\t%0,%1";
3463           load_store[num].last_store = "sb\t%0,%1";
3464           load_store[num].final = 0;
3465           load_store[num].mode = QImode;
3466           offset++;
3467           bytes--;
3468         }
3469
3470       if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3471         {
3472           dslots_load_total++;
3473           dslots_load_filled++;
3474
3475           if (CONSTANT_P (src_reg))
3476             mips_count_memory_refs (src_reg, 1);
3477
3478           if (CONSTANT_P (dest_reg))
3479             mips_count_memory_refs (dest_reg, 1);
3480         }
3481
3482       /* Emit load/stores now if we have run out of registers or are
3483          at the end of the move.  */
3484
3485       if (++num == num_regs || bytes == 0)
3486         {
3487           /* If only load/store, we need a NOP after the load.  */
3488           if (num == 1)
3489             {
3490               load_store[0].load = load_store[0].load_nop;
3491               if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3492                 dslots_load_filled--;
3493             }
3494
3495           if (move_type != BLOCK_MOVE_LAST)
3496             {
3497               for (i = 0; i < num; i++)
3498                 {
3499                   int offset;
3500
3501                   if (!operands[i + 4])
3502                     abort ();
3503
3504                   if (GET_MODE (operands[i + 4]) != load_store[i].mode)
3505                     operands[i + 4] = gen_rtx (REG, load_store[i].mode,
3506                                                REGNO (operands[i + 4]));
3507
3508                   offset = load_store[i].offset;
3509                   xoperands[0] = operands[i + 4];
3510                   xoperands[1] = gen_rtx (MEM, load_store[i].mode,
3511                                           plus_constant (src_reg, offset));
3512
3513                   if (use_lwl_lwr)
3514                     {
3515                       int extra_offset
3516                         = GET_MODE_SIZE (load_store[i].mode) - 1;
3517
3518                       xoperands[2] = gen_rtx (MEM, load_store[i].mode,
3519                                               plus_constant (src_reg,
3520                                                              extra_offset
3521                                                              + offset));
3522                     }
3523
3524                   output_asm_insn (load_store[i].load, xoperands);
3525                 }
3526             }
3527
3528           for (i = 0; i < num; i++)
3529             {
3530               int last_p = (i == num-1 && bytes == 0);
3531               int offset = load_store[i].offset;
3532
3533               xoperands[0] = operands[i + 4];
3534               xoperands[1] = gen_rtx (MEM, load_store[i].mode,
3535                                       plus_constant (dest_reg, offset));
3536
3537
3538               if (use_lwl_lwr)
3539                 {
3540                   int extra_offset = GET_MODE_SIZE (load_store[i].mode) - 1;
3541                   xoperands[2] = gen_rtx (MEM, load_store[i].mode,
3542                                           plus_constant (dest_reg,
3543                                                          extra_offset
3544                                                          + offset));
3545                 }
3546
3547               if (move_type == BLOCK_MOVE_NORMAL)
3548                 output_asm_insn (load_store[i].store, xoperands);
3549
3550               else if (move_type == BLOCK_MOVE_NOT_LAST)
3551                 {
3552                   if (!last_p)
3553                     output_asm_insn (load_store[i].store, xoperands);
3554
3555                   else if (load_store[i].final != 0)
3556                     output_asm_insn (load_store[i].final, xoperands);
3557                 }
3558
3559               else if (last_p)
3560                 output_asm_insn (load_store[i].last_store, xoperands);
3561             }
3562
3563           num = 0;              /* reset load_store */
3564           use_lwl_lwr = 0;
3565         }
3566     }
3567
3568   return "";
3569 }
3570 \f
3571 /* Argument support functions.  */
3572
3573 /* Initialize CUMULATIVE_ARGS for a function.  */
3574
3575 void
3576 init_cumulative_args (cum, fntype, libname)
3577      CUMULATIVE_ARGS *cum;              /* argument info to initialize */
3578      tree fntype;                       /* tree ptr for function decl */
3579      rtx libname ATTRIBUTE_UNUSED;      /* SYMBOL_REF of library name or 0 */
3580 {
3581   static CUMULATIVE_ARGS zero_cum;
3582   tree param, next_param;
3583
3584   if (TARGET_DEBUG_E_MODE)
3585     {
3586       fprintf (stderr,
3587                "\ninit_cumulative_args, fntype = 0x%.8lx", (long)fntype);
3588
3589       if (!fntype)
3590         fputc ('\n', stderr);
3591
3592       else
3593         {
3594           tree ret_type = TREE_TYPE (fntype);
3595           fprintf (stderr, ", fntype code = %s, ret code = %s\n",
3596                    tree_code_name[(int)TREE_CODE (fntype)],
3597                    tree_code_name[(int)TREE_CODE (ret_type)]);
3598         }
3599     }
3600
3601   *cum = zero_cum;
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 /* Advance the argument to the next argument position.  */
3618
3619 void
3620 function_arg_advance (cum, mode, type, named)
3621      CUMULATIVE_ARGS *cum;      /* current arg information */
3622      enum machine_mode mode;    /* current arg mode */
3623      tree type;                 /* type of the argument or 0 if lib support */
3624      int named;                 /* whether or not the argument was named */
3625 {
3626   if (TARGET_DEBUG_E_MODE)
3627     {
3628       fprintf (stderr,
3629                "function_adv({gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3630                cum->gp_reg_found, cum->arg_number, cum->arg_words,
3631                GET_MODE_NAME (mode));
3632       fprintf (stderr, HOST_PTR_PRINTF, type);
3633       fprintf (stderr, ", %d )\n\n", named);
3634     }
3635
3636   cum->arg_number++;
3637   switch (mode)
3638     {
3639     case VOIDmode:
3640       break;
3641
3642     default:
3643       if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
3644           && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3645         abort ();
3646
3647       cum->gp_reg_found = 1;
3648       cum->arg_words += ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
3649                          / UNITS_PER_WORD);
3650       break;
3651
3652     case BLKmode:
3653       cum->gp_reg_found = 1;
3654       cum->arg_words += ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
3655                          / UNITS_PER_WORD);
3656       break;
3657
3658     case SFmode:
3659       if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3660         cum->fp_arg_words++;
3661       else
3662         cum->arg_words++;
3663       if (! cum->gp_reg_found && cum->arg_number <= 2)
3664         cum->fp_code += 1 << ((cum->arg_number - 1) * 2);
3665       break;
3666
3667     case DFmode:
3668       if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
3669         cum->fp_arg_words += (TARGET_64BIT ? 1 : 2);
3670       else
3671         cum->arg_words += (TARGET_64BIT ? 1 : 2);
3672       if (! cum->gp_reg_found && ! TARGET_SINGLE_FLOAT && cum->arg_number <= 2)
3673         cum->fp_code += 2 << ((cum->arg_number - 1) * 2);
3674       break;
3675
3676     case DImode:
3677       cum->gp_reg_found = 1;
3678       cum->arg_words += (TARGET_64BIT ? 1 : 2);
3679       break;
3680
3681     case QImode:
3682     case HImode:
3683     case SImode:
3684       cum->gp_reg_found = 1;
3685       cum->arg_words++;
3686       break;
3687     }
3688 }
3689
3690 /* Return an RTL expression containing the register for the given mode,
3691    or 0 if the argument is to be passed on the stack.  */
3692
3693 struct rtx_def *
3694 function_arg (cum, mode, type, named)
3695      CUMULATIVE_ARGS *cum;      /* current arg information */
3696      enum machine_mode mode;    /* current arg mode */
3697      tree type;                 /* type of the argument or 0 if lib support */
3698      int named;                 /* != 0 for normal args, == 0 for ... args */
3699 {
3700   rtx ret;
3701   int regbase = -1;
3702   int bias = 0;
3703   int *arg_words = &cum->arg_words;
3704   int struct_p = (type != 0
3705                   && (TREE_CODE (type) == RECORD_TYPE
3706                       || TREE_CODE (type) == UNION_TYPE
3707                       || TREE_CODE (type) == QUAL_UNION_TYPE));
3708
3709   if (TARGET_DEBUG_E_MODE)
3710     {
3711       fprintf (stderr,
3712                "function_arg( {gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3713                cum->gp_reg_found, cum->arg_number, cum->arg_words,
3714                GET_MODE_NAME (mode));
3715       fprintf (stderr, HOST_PTR_PRINTF, type);
3716       fprintf (stderr, ", %d ) = ", named);
3717     }
3718   
3719
3720   cum->last_arg_fp = 0;
3721   switch (mode)
3722     {
3723     case SFmode:
3724       if (mips_abi == ABI_32 || mips_abi == ABI_O64)
3725         {
3726           if (cum->gp_reg_found || cum->arg_number >= 2 || TARGET_SOFT_FLOAT)
3727             regbase = GP_ARG_FIRST;
3728           else
3729             {
3730               regbase = FP_ARG_FIRST;
3731
3732               /* If the first arg was a float in a floating point register,
3733                  then set bias to align this float arg properly.  */
3734               if (cum->arg_words == 1)
3735                 bias = 1;
3736             }
3737         }
3738       else if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3739         {
3740           if (! TARGET_64BIT)
3741             cum->fp_arg_words += cum->fp_arg_words & 1;
3742           cum->last_arg_fp = 1;
3743           arg_words = &cum->fp_arg_words;
3744           regbase = FP_ARG_FIRST;
3745         }
3746       else
3747         regbase = (TARGET_SOFT_FLOAT || ! named ? GP_ARG_FIRST : FP_ARG_FIRST);
3748       break;
3749
3750     case DFmode:
3751       if (! TARGET_64BIT)
3752         {
3753           if (mips_abi == ABI_EABI
3754               && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
3755             cum->fp_arg_words += cum->fp_arg_words & 1;
3756           else
3757             cum->arg_words += cum->arg_words & 1;
3758         }
3759
3760       if (mips_abi == ABI_32 || mips_abi == ABI_O64)
3761         regbase = ((cum->gp_reg_found
3762                     || TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT
3763                     || cum->arg_number >= 2)
3764                    ? GP_ARG_FIRST : FP_ARG_FIRST);
3765       else if (mips_abi == ABI_EABI
3766                && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
3767         {
3768           cum->last_arg_fp = 1;
3769           arg_words = &cum->fp_arg_words;
3770           regbase = FP_ARG_FIRST;
3771         }
3772       else
3773         regbase = (TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT || ! named
3774                    ? GP_ARG_FIRST : FP_ARG_FIRST);
3775       break;
3776
3777     default:
3778       if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
3779           && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3780         abort ();
3781
3782       /* Drops through.  */
3783     case BLKmode:
3784       if (type != (tree)0 && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD
3785           && ! TARGET_64BIT && mips_abi != ABI_EABI)
3786         cum->arg_words += (cum->arg_words & 1);
3787       regbase = GP_ARG_FIRST;
3788       break;
3789
3790     case VOIDmode:
3791     case QImode:
3792     case HImode:
3793     case SImode:
3794       regbase = GP_ARG_FIRST;
3795       break;
3796
3797     case DImode:
3798       if (! TARGET_64BIT)
3799         cum->arg_words += (cum->arg_words & 1);
3800       regbase = GP_ARG_FIRST;
3801     }
3802
3803   if (*arg_words >= MAX_ARGS_IN_REGISTERS)
3804     {
3805       if (TARGET_DEBUG_E_MODE)
3806         fprintf (stderr, "<stack>%s\n", struct_p ? ", [struct]" : "");
3807
3808       ret = 0;
3809     }
3810   else
3811     {
3812       if (regbase == -1)
3813         abort ();
3814
3815       if (! type || TREE_CODE (type) != RECORD_TYPE || mips_abi == ABI_32
3816           || mips_abi == ABI_EABI || mips_abi == ABI_O64 || ! named)
3817         ret = gen_rtx (REG, mode, regbase + *arg_words + bias);
3818       else
3819         {
3820           /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
3821              structure contains a double in its entirety, then that 64 bit
3822              chunk is passed in a floating point register.  */
3823           tree field;
3824
3825           /* First check to see if there is any such field.  */
3826           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3827             if (TREE_CODE (field) == FIELD_DECL
3828                 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3829                 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
3830                 && (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
3831                     % BITS_PER_WORD == 0))
3832               break;
3833
3834           /* If the whole struct fits a DFmode register,
3835              we don't need the PARALLEL.  */
3836           if (! field || mode == DFmode)
3837             ret = gen_rtx (REG, mode, regbase + *arg_words + bias);
3838           else
3839             {
3840               /* Now handle the special case by returning a PARALLEL
3841                  indicating where each 64 bit chunk goes.  */
3842               int chunks;
3843               int bitpos;
3844               int regno;
3845               int i;
3846
3847               /* ??? If this is a packed structure, then the last hunk won't
3848                  be 64 bits.  */
3849
3850               chunks = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_WORD;
3851               if (chunks + *arg_words + bias > MAX_ARGS_IN_REGISTERS)
3852                 chunks = MAX_ARGS_IN_REGISTERS - *arg_words - bias;
3853
3854               /* assign_parms checks the mode of ENTRY_PARM, so we must
3855                  use the actual mode here.  */
3856               ret = gen_rtx (PARALLEL, mode, rtvec_alloc (chunks));
3857
3858               bitpos = 0;
3859               regno = regbase + *arg_words + bias;
3860               field = TYPE_FIELDS (type);
3861               for (i = 0; i < chunks; i++)
3862                 {
3863                   rtx reg;
3864
3865                   for (; field; field = TREE_CHAIN (field))
3866                     if (TREE_CODE (field) == FIELD_DECL
3867                         && (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
3868                             >= bitpos))
3869                       break;
3870
3871                   if (field
3872                       && TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)) == bitpos
3873                       && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3874                       && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
3875                     reg = gen_rtx (REG, DFmode,
3876                                    regno + FP_ARG_FIRST - GP_ARG_FIRST);
3877                   else
3878                     reg = gen_rtx (REG, word_mode, regno);
3879
3880                   XVECEXP (ret, 0, i) 
3881                     = gen_rtx (EXPR_LIST, VOIDmode, reg,
3882                                GEN_INT (bitpos / BITS_PER_UNIT));
3883
3884                   bitpos += 64;
3885                   regno++;
3886                 }
3887             }
3888         }
3889
3890       if (TARGET_DEBUG_E_MODE)
3891         fprintf (stderr, "%s%s\n", reg_names[regbase + *arg_words + bias],
3892                  struct_p ? ", [struct]" : "");
3893
3894       /* The following is a hack in order to pass 1 byte structures
3895          the same way that the MIPS compiler does (namely by passing
3896          the structure in the high byte or half word of the register).
3897          This also makes varargs work.  If we have such a structure,
3898          we save the adjustment RTL, and the call define expands will
3899          emit them.  For the VOIDmode argument (argument after the
3900          last real argument), pass back a parallel vector holding each
3901          of the adjustments.  */
3902
3903       /* ??? function_arg can be called more than once for each argument.
3904          As a result, we compute more adjustments than we need here.
3905          See the CUMULATIVE_ARGS definition in mips.h.  */
3906
3907       /* ??? This scheme requires everything smaller than the word size to
3908          shifted to the left, but when TARGET_64BIT and ! TARGET_INT64,
3909          that would mean every int needs to be shifted left, which is very
3910          inefficient.  Let's not carry this compatibility to the 64 bit
3911          calling convention for now.  */
3912
3913       if (struct_p && int_size_in_bytes (type) < UNITS_PER_WORD
3914           && ! TARGET_64BIT && mips_abi != ABI_EABI)
3915         {
3916           rtx amount = GEN_INT (BITS_PER_WORD
3917                                 - int_size_in_bytes (type) * BITS_PER_UNIT);
3918           rtx reg = gen_rtx (REG, word_mode, regbase + *arg_words + bias);
3919
3920           if (TARGET_64BIT)
3921             cum->adjust[cum->num_adjusts++] = gen_ashldi3 (reg, reg, amount);
3922           else
3923             cum->adjust[cum->num_adjusts++] = gen_ashlsi3 (reg, reg, amount);
3924         }
3925     }
3926
3927   /* We will be called with a mode of VOIDmode after the last argument
3928      has been seen.  Whatever we return will be passed to the call
3929      insn.  If we need any shifts for small structures, return them in
3930      a PARALLEL; in that case, stuff the mips16 fp_code in as the
3931      mode.  Otherwise, if we have need a mips16 fp_code, return a REG
3932      with the code stored as the mode.  */
3933   if (mode == VOIDmode)
3934     {
3935       if (cum->num_adjusts > 0)
3936         ret = gen_rtx (PARALLEL, (enum machine_mode) cum->fp_code,
3937                        gen_rtvec_v (cum->num_adjusts, cum->adjust));
3938       else if (TARGET_MIPS16 && cum->fp_code != 0)
3939         ret = gen_rtx (REG, (enum machine_mode) cum->fp_code, 0);
3940     }
3941
3942   return ret;
3943 }
3944
3945 int
3946 function_arg_partial_nregs (cum, mode, type, named)
3947      CUMULATIVE_ARGS *cum;      /* current arg information */
3948      enum machine_mode mode;    /* current arg mode */
3949      tree type;                 /* type of the argument or 0 if lib support */
3950      int named ATTRIBUTE_UNUSED;/* != 0 for normal args, == 0 for ... args */
3951 {
3952   if ((mode == BLKmode
3953        || GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
3954        || GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3955       && cum->arg_words < MAX_ARGS_IN_REGISTERS
3956       && mips_abi != ABI_EABI)
3957     {
3958       int words;
3959       if (mode == BLKmode)
3960         words = ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
3961                  / UNITS_PER_WORD);
3962       else
3963         words = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3964
3965       if (words + cum->arg_words <= MAX_ARGS_IN_REGISTERS)
3966         return 0;               /* structure fits in registers */
3967
3968       if (TARGET_DEBUG_E_MODE)
3969         fprintf (stderr, "function_arg_partial_nregs = %d\n",
3970                  MAX_ARGS_IN_REGISTERS - cum->arg_words);
3971
3972       return MAX_ARGS_IN_REGISTERS - cum->arg_words;
3973     }
3974
3975   else if (mode == DImode && cum->arg_words == MAX_ARGS_IN_REGISTERS-1
3976            && ! TARGET_64BIT && mips_abi != ABI_EABI)
3977     {
3978       if (TARGET_DEBUG_E_MODE)
3979         fprintf (stderr, "function_arg_partial_nregs = 1\n");
3980
3981       return 1;
3982     }
3983
3984   return 0;
3985 }
3986 \f
3987 /* Abort after printing out a specific insn.  */
3988
3989 static void
3990 abort_with_insn (insn, reason)
3991      rtx insn;
3992      const char *reason;
3993 {
3994   error (reason);
3995   debug_rtx (insn);
3996   abort ();
3997 }
3998
3999 /* Write a message to stderr (for use in macros expanded in files that do not
4000    include stdio.h).  */
4001
4002 void
4003 trace (s, s1, s2)
4004      char *s, *s1, *s2;
4005 {
4006   fprintf (stderr, s, s1, s2);
4007 }
4008 \f
4009 /* Set up the threshold for data to go into the small data area, instead
4010    of the normal data area, and detect any conflicts in the switches.  */
4011
4012 void
4013 override_options ()
4014 {
4015   register int i, start;
4016   register int regno;
4017   register enum machine_mode mode;
4018
4019   mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4020
4021   if (mips_section_threshold <= 0)
4022     target_flags &= ~MASK_GPOPT;
4023   else if (optimize)
4024     target_flags |= MASK_GPOPT;
4025
4026 #ifndef MIPS_ISA_DEFAULT
4027 #define MIPS_ISA_DEFAULT 1
4028 #endif
4029
4030   /* If both single-float and soft-float are set, then clear the one that
4031      was set by TARGET_DEFAULT, leaving the one that was set by the
4032      user.  We assume here that the specs prevent both being set by the 
4033      user. */
4034 #ifdef TARGET_DEFAULT
4035   if (TARGET_SINGLE_FLOAT && TARGET_SOFT_FLOAT)
4036     target_flags &= ~(TARGET_DEFAULT&(MASK_SOFT_FLOAT|MASK_SINGLE_FLOAT));
4037 #endif
4038
4039   /* Get the architectural level.  */
4040   if (mips_isa_string == 0)
4041     mips_isa = MIPS_ISA_DEFAULT;
4042
4043   else if (ISDIGIT (*mips_isa_string))
4044     {
4045       mips_isa = atoi (mips_isa_string);
4046       if (mips_isa == 16)
4047         {
4048           /* -mno-mips16 overrides -mips16.  */
4049           if (mips_no_mips16_string == NULL)
4050             {
4051               target_flags |= MASK_MIPS16;
4052               if (TARGET_64BIT)
4053                 mips_isa = 3;
4054               else
4055                 mips_isa = MIPS_ISA_DEFAULT;
4056             }
4057           else
4058             {
4059               mips_isa = MIPS_ISA_DEFAULT;
4060             }
4061         }
4062       else if (mips_isa < 1 || mips_isa > 4)
4063         {
4064           error ("-mips%d not supported", mips_isa);
4065           mips_isa = 1;
4066         }
4067     }
4068
4069   else
4070     {
4071       error ("bad value (%s) for -mips switch", mips_isa_string);
4072       mips_isa = 1;
4073     }
4074
4075 #ifdef MIPS_ABI_DEFAULT
4076   /* Get the ABI to use. */
4077   if (mips_abi_string == (char *) 0)
4078     mips_abi = MIPS_ABI_DEFAULT;
4079   else if (! strcmp (mips_abi_string, "32"))
4080     mips_abi = ABI_32;
4081   else if (! strcmp (mips_abi_string, "o64"))
4082     mips_abi = ABI_O64;
4083   else if (! strcmp (mips_abi_string, "n32"))
4084     mips_abi = ABI_N32;
4085   else if (! strcmp (mips_abi_string, "64"))
4086     mips_abi = ABI_64;
4087   else if (! strcmp (mips_abi_string, "eabi"))
4088     mips_abi = ABI_EABI;
4089   else
4090     error ("bad value (%s) for -mabi= switch", mips_abi_string);
4091
4092   /* A specified ISA defaults the ABI if it was not specified.  */
4093   if (mips_abi_string == 0 && mips_isa_string 
4094       && mips_abi != ABI_EABI && mips_abi != ABI_O64)
4095     {
4096       if (mips_isa <= 2)
4097         mips_abi = ABI_32;
4098       else
4099         mips_abi = ABI_64;
4100     }
4101
4102   /* A specified ABI defaults the ISA if it was not specified.  */
4103   else if (mips_isa_string == 0 && mips_abi_string 
4104            && mips_abi != ABI_EABI && mips_abi != ABI_O64)
4105     {
4106       if (mips_abi == ABI_32)
4107         mips_isa = 1;
4108       else if (mips_abi == ABI_N32)
4109         mips_isa = 3;
4110       else
4111         mips_isa = 4;
4112     }
4113
4114   /* If both ABI and ISA were specified, check for conflicts.  */
4115   else if (mips_isa_string && mips_abi_string)
4116     {
4117       if ((mips_isa <= 2 && (mips_abi == ABI_N32 || mips_abi == ABI_64
4118                              || mips_abi == ABI_O64))
4119           || (mips_isa >= 3 && mips_abi == ABI_32))
4120         error ("-mabi=%s does not support -mips%d", mips_abi_string, mips_isa);
4121     }
4122
4123   /* Override TARGET_DEFAULT if necessary.  */
4124   if (mips_abi == ABI_32)
4125     target_flags &= ~ (MASK_FLOAT64|MASK_64BIT);
4126
4127   /* If no type size setting options (-mlong64,-mint64,-mlong32) were used
4128      then set the type sizes.  In the EABI in 64 bit mode, longs and
4129      pointers are 64 bits.  Likewise for the SGI Irix6 N64 ABI.  */
4130   if (mips_explicit_type_size_string == NULL
4131       && ((mips_abi == ABI_EABI && TARGET_64BIT)
4132           || mips_abi == ABI_64))
4133     target_flags |= MASK_LONG64;
4134
4135   /* ??? This doesn't work yet, so don't let people try to use it.  */
4136   if (mips_abi == ABI_32)
4137     error ("The -mabi=32 support does not work yet.");
4138
4139 #else
4140   if (mips_abi_string)
4141     error ("This target does not support the -mabi switch.");
4142 #endif
4143
4144 #ifdef MIPS_CPU_STRING_DEFAULT
4145   /* ??? There is a minor inconsistency here.  If the user specifies an ISA
4146      greater than that supported by the default processor, then the user gets
4147      an error.  Normally, the compiler will just default to the base level cpu
4148      for the indicated isa.  */
4149   if (mips_cpu_string == 0)
4150     mips_cpu_string = MIPS_CPU_STRING_DEFAULT;
4151 #endif
4152
4153   /* Identify the processor type */
4154   if (mips_cpu_string == 0
4155       || !strcmp (mips_cpu_string, "default")
4156       || !strcmp (mips_cpu_string, "DEFAULT"))
4157     {
4158       switch (mips_isa)
4159         {
4160         default:
4161           mips_cpu_string = "3000";
4162           mips_cpu = PROCESSOR_R3000;
4163           break;
4164         case 2:
4165           mips_cpu_string = "6000";
4166           mips_cpu = PROCESSOR_R6000;
4167           break;
4168         case 3:
4169           mips_cpu_string = "4000";
4170           mips_cpu = PROCESSOR_R4000;
4171           break;
4172         case 4:
4173           mips_cpu_string = "8000";
4174           mips_cpu = PROCESSOR_R8000;
4175           break;
4176         }
4177     }
4178
4179   else
4180     {
4181       const char *p = mips_cpu_string;
4182       int seen_v = 0;
4183
4184       /* We need to cope with the various "vr" prefixes for the NEC 4300
4185          and 4100 processors.  */
4186       if (*p == 'v' || *p == 'V')
4187         seen_v = 1, p++;
4188
4189       if (*p == 'r' || *p == 'R')
4190         p++;
4191
4192       /* Since there is no difference between a R2000 and R3000 in
4193          terms of the scheduler, we collapse them into just an R3000. */
4194
4195       mips_cpu = PROCESSOR_DEFAULT;
4196       switch (*p)
4197         {
4198         case '2':
4199           if (!strcmp (p, "2000") || !strcmp (p, "2k") || !strcmp (p, "2K"))
4200             mips_cpu = PROCESSOR_R3000;
4201           break;
4202
4203         case '3':
4204           if (!strcmp (p, "3000") || !strcmp (p, "3k") || !strcmp (p, "3K"))
4205             mips_cpu = PROCESSOR_R3000;
4206           else if (!strcmp (p, "3900"))
4207             mips_cpu = PROCESSOR_R3900;
4208           break;
4209
4210         case '4':
4211           if (!strcmp (p, "4000") || !strcmp (p, "4k") || !strcmp (p, "4K"))
4212             mips_cpu = PROCESSOR_R4000;
4213           /* The vr4100 is a non-FP ISA III processor with some extra
4214              instructions.  */
4215           else if (!strcmp (p, "4100"))
4216             {
4217               mips_cpu = PROCESSOR_R4100;
4218               target_flags |= MASK_SOFT_FLOAT ;
4219             }
4220           /* The vr4300 is a standard ISA III processor, but with a different
4221              pipeline.  */
4222           else if (!strcmp (p, "4300"))
4223             mips_cpu = PROCESSOR_R4300;
4224           /* The r4400 is exactly the same as the r4000 from the compiler's
4225              viewpoint.  */
4226           else if (!strcmp (p, "4400"))
4227             mips_cpu = PROCESSOR_R4000;
4228           else if (!strcmp (p, "4600"))
4229             mips_cpu = PROCESSOR_R4600;
4230           else if (!strcmp (p, "4650"))
4231             mips_cpu = PROCESSOR_R4650;
4232           break;
4233
4234         case '5':
4235           if (!strcmp (p, "5000") || !strcmp (p, "5k") || !strcmp (p, "5K"))
4236             mips_cpu = PROCESSOR_R5000;
4237           break;
4238
4239         case '6':
4240           if (!strcmp (p, "6000") || !strcmp (p, "6k") || !strcmp (p, "6K"))
4241             mips_cpu = PROCESSOR_R6000;
4242           break;
4243
4244         case '8':
4245           if (!strcmp (p, "8000"))
4246             mips_cpu = PROCESSOR_R8000;
4247           break;
4248
4249         case 'o':
4250           if (!strcmp (p, "orion"))
4251             mips_cpu = PROCESSOR_R4600;
4252           break;
4253         }
4254
4255       if (seen_v
4256           && mips_cpu != PROCESSOR_R4300
4257           && mips_cpu != PROCESSOR_R4100
4258           && mips_cpu != PROCESSOR_R5000)
4259         mips_cpu = PROCESSOR_DEFAULT;
4260
4261       if (mips_cpu == PROCESSOR_DEFAULT)
4262         {
4263           error ("bad value (%s) for -mcpu= switch", mips_cpu_string);
4264           mips_cpu_string = "default";
4265         }
4266     }
4267
4268   if ((mips_cpu == PROCESSOR_R3000 && mips_isa > 1)
4269       || (mips_cpu == PROCESSOR_R6000 && mips_isa > 2)
4270       || ((mips_cpu == PROCESSOR_R4000
4271            || mips_cpu == PROCESSOR_R4100
4272            || mips_cpu == PROCESSOR_R4300
4273            || mips_cpu == PROCESSOR_R4600
4274            || mips_cpu == PROCESSOR_R4650)
4275           && mips_isa > 3))
4276     error ("-mcpu=%s does not support -mips%d", mips_cpu_string, mips_isa);
4277
4278   /* make sure sizes of ints/longs/etc. are ok */
4279   if (mips_isa < 3)
4280     {
4281       if (TARGET_FLOAT64)
4282         fatal ("Only MIPS-III or MIPS-IV CPUs can support 64 bit fp registers");
4283
4284       else if (TARGET_64BIT)
4285         fatal ("Only MIPS-III or MIPS-IV CPUs can support 64 bit gp registers");
4286     }
4287
4288   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
4289     flag_pcc_struct_return = 0;
4290
4291   /* Tell halfpic.c that we have half-pic code if we do.  */
4292   if (TARGET_HALF_PIC)
4293     HALF_PIC_INIT ();
4294
4295   /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined.  We need
4296      to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work.  */
4297   /* ??? -non_shared turns off pic code generation, but this is not
4298      implemented.  */
4299   if (TARGET_ABICALLS)
4300     {
4301       mips_abicalls = MIPS_ABICALLS_YES;
4302       flag_pic = 1;
4303       if (mips_section_threshold > 0)
4304         warning ("-G is incompatible with PIC code which is the default");
4305     }
4306   else
4307     mips_abicalls = MIPS_ABICALLS_NO;
4308
4309   /* -membedded-pic is a form of PIC code suitable for embedded
4310      systems.  All calls are made using PC relative addressing, and
4311      all data is addressed using the $gp register.  This requires gas,
4312      which does most of the work, and GNU ld, which automatically
4313      expands PC relative calls which are out of range into a longer
4314      instruction sequence.  All gcc really does differently is
4315      generate a different sequence for a switch.  */
4316   if (TARGET_EMBEDDED_PIC)
4317     {
4318       flag_pic = 1;
4319       if (TARGET_ABICALLS)
4320         warning ("-membedded-pic and -mabicalls are incompatible");
4321
4322       if (g_switch_set)
4323         warning ("-G and -membedded-pic are incompatible");
4324
4325       /* Setting mips_section_threshold is not required, because gas
4326          will force everything to be GP addressable anyhow, but
4327          setting it will cause gcc to make better estimates of the
4328          number of instructions required to access a particular data
4329          item.  */
4330       mips_section_threshold = 0x7fffffff;
4331     }
4332
4333   /* This optimization requires a linker that can support a R_MIPS_LO16
4334      relocation which is not immediately preceded by a R_MIPS_HI16 relocation.
4335      GNU ld has this support, but not all other MIPS linkers do, so we enable
4336      this optimization only if the user requests it, or if GNU ld is the
4337      standard linker for this configuration.  */
4338   /* ??? This does not work when target addresses are DImode.
4339      This is because we are missing DImode high/lo_sum patterns.  */
4340   if (TARGET_GAS && ! TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES && optimize && ! flag_pic
4341       && Pmode == SImode)
4342     mips_split_addresses = 1;
4343   else
4344     mips_split_addresses = 0;
4345
4346   /* -mrnames says to use the MIPS software convention for register
4347      names instead of the hardware names (ie, $a0 instead of $4).
4348      We do this by switching the names in mips_reg_names, which the
4349      reg_names points into via the REGISTER_NAMES macro.  */
4350
4351   if (TARGET_NAME_REGS)
4352     bcopy ((char *) mips_sw_reg_names, (char *) mips_reg_names,
4353            sizeof (mips_reg_names));
4354
4355   /* When compiling for the mips16, we can not use floating point.  We
4356      record the original hard float value in mips16_hard_float.  */
4357   if (TARGET_MIPS16)
4358     {
4359       if (TARGET_SOFT_FLOAT)
4360         mips16_hard_float = 0;
4361       else
4362         mips16_hard_float = 1;
4363       target_flags |= MASK_SOFT_FLOAT;
4364
4365       /* Don't run the scheduler before reload, since it tends to
4366          increase register pressure.  */
4367       flag_schedule_insns = 0;
4368     }
4369
4370   /* We put -mentry in TARGET_OPTIONS rather than TARGET_SWITCHES only
4371      to avoid using up another bit in target_flags.  */
4372   if (mips_entry_string != NULL)
4373     {
4374       if (*mips_entry_string != '\0')
4375         error ("Invalid option `entry%s'", mips_entry_string);
4376
4377       if (! TARGET_MIPS16)
4378         warning ("-mentry is only meaningful with -mips-16");
4379       else
4380         mips_entry = 1;
4381     }
4382
4383   /* We copy TARGET_MIPS16 into the mips16 global variable, so that
4384      attributes can access it.  */
4385   if (TARGET_MIPS16)
4386     mips16 = 1;
4387   else
4388     mips16 = 0;
4389
4390   /* Initialize the high and low values for legitimate floating point
4391      constants.  Rather than trying to get the accuracy down to the
4392      last bit, just use approximate ranges.  */
4393   dfhigh = REAL_VALUE_ATOF ("1.0e300", DFmode);
4394   dflow = REAL_VALUE_ATOF ("1.0e-300", DFmode);
4395   sfhigh = REAL_VALUE_ATOF ("1.0e38", SFmode);
4396   sflow = REAL_VALUE_ATOF ("1.0e-38", SFmode);
4397
4398   mips_print_operand_punct['?'] = 1;
4399   mips_print_operand_punct['#'] = 1;
4400   mips_print_operand_punct['&'] = 1;
4401   mips_print_operand_punct['!'] = 1;
4402   mips_print_operand_punct['*'] = 1;
4403   mips_print_operand_punct['@'] = 1;
4404   mips_print_operand_punct['.'] = 1;
4405   mips_print_operand_punct['('] = 1;
4406   mips_print_operand_punct[')'] = 1;
4407   mips_print_operand_punct['['] = 1;
4408   mips_print_operand_punct[']'] = 1;
4409   mips_print_operand_punct['<'] = 1;
4410   mips_print_operand_punct['>'] = 1;
4411   mips_print_operand_punct['{'] = 1;
4412   mips_print_operand_punct['}'] = 1;
4413   mips_print_operand_punct['^'] = 1;
4414   mips_print_operand_punct['$'] = 1;
4415   mips_print_operand_punct['+'] = 1;
4416   mips_print_operand_punct['~'] = 1;
4417
4418   mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
4419   mips_char_to_class['e'] = M16_NA_REGS;
4420   mips_char_to_class['t'] = T_REG;
4421   mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
4422   mips_char_to_class['h'] = HI_REG;
4423   mips_char_to_class['l'] = LO_REG;
4424   mips_char_to_class['a'] = HILO_REG;
4425   mips_char_to_class['x'] = MD_REGS;
4426   mips_char_to_class['b'] = ALL_REGS;
4427   mips_char_to_class['y'] = GR_REGS;
4428   mips_char_to_class['z'] = ST_REGS;
4429
4430   /* Set up array to map GCC register number to debug register number.
4431      Ignore the special purpose register numbers.  */
4432
4433   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4434     mips_dbx_regno[i] = -1;
4435
4436   start = GP_DBX_FIRST - GP_REG_FIRST;
4437   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
4438     mips_dbx_regno[i] = i + start;
4439
4440   start = FP_DBX_FIRST - FP_REG_FIRST;
4441   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
4442     mips_dbx_regno[i] = i + start;
4443
4444   /* Set up array giving whether a given register can hold a given mode.
4445      At present, restrict ints from being in FP registers, because reload
4446      is a little enthusiastic about storing extra values in FP registers,
4447      and this is not good for things like OS kernels.  Also, due to the
4448      mandatory delay, it is as fast to load from cached memory as to move
4449      from the FP register.  */
4450
4451   for (mode = VOIDmode;
4452        mode != MAX_MACHINE_MODE;
4453        mode = (enum machine_mode) ((int)mode + 1))
4454     {
4455       register int size              = GET_MODE_SIZE (mode);
4456       register enum mode_class class = GET_MODE_CLASS (mode);
4457
4458       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4459         {
4460           register int temp;
4461
4462           if (mode == CCmode)
4463             {
4464               if (mips_isa < 4)
4465                 temp = (regno == FPSW_REGNUM);
4466               else
4467                 temp = (ST_REG_P (regno) || GP_REG_P (regno)
4468                         || FP_REG_P (regno));
4469             }
4470
4471           else if (GP_REG_P (regno))
4472             temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
4473
4474           else if (FP_REG_P (regno))
4475             temp = ((TARGET_FLOAT64 || ((regno & 1) == 0))
4476                     && (class == MODE_FLOAT
4477                         || class == MODE_COMPLEX_FLOAT
4478                         || (TARGET_DEBUG_H_MODE && class == MODE_INT))
4479                     && (! TARGET_SINGLE_FLOAT || size <= 4));
4480
4481           else if (MD_REG_P (regno))
4482             temp = (class == MODE_INT
4483                     && (size <= UNITS_PER_WORD
4484                         || (regno == MD_REG_FIRST
4485                             && size == 2 * UNITS_PER_WORD)));
4486
4487           else
4488             temp = 0;
4489
4490           mips_hard_regno_mode_ok[(int)mode][regno] = temp;
4491         }
4492     }
4493
4494   /* Save GPR registers in word_mode sized hunks.  word_mode hasn't been
4495      initialized yet, so we can't use that here.  */
4496   gpr_mode = TARGET_64BIT ? DImode : SImode;
4497
4498   /* Provide default values for align_* for 64-bit targets.  */
4499   if (TARGET_64BIT)
4500     {
4501       if (align_loops == 0) 
4502         align_loops = 8;
4503       if (align_jumps == 0) 
4504         align_jumps = 8;
4505       if (align_functions == 0) 
4506         align_functions = 8;
4507     }
4508 }
4509
4510 /* On the mips16, we want to allocate $24 (T_REG) before other
4511    registers for instructions for which it is possible.  This helps
4512    avoid shuffling registers around in order to set up for an xor,
4513    encouraging the compiler to use a cmp instead.  */
4514
4515 void
4516 mips_order_regs_for_local_alloc ()
4517 {
4518   register int i;
4519
4520   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4521     reg_alloc_order[i] = i;
4522
4523   if (TARGET_MIPS16)
4524     {
4525       /* It really doesn't matter where we put register 0, since it is
4526          a fixed register anyhow.  */
4527       reg_alloc_order[0] = 24;
4528       reg_alloc_order[24] = 0;
4529     }
4530 }
4531
4532 \f
4533 /* The MIPS debug format wants all automatic variables and arguments
4534    to be in terms of the virtual frame pointer (stack pointer before
4535    any adjustment in the function), while the MIPS 3.0 linker wants
4536    the frame pointer to be the stack pointer after the initial
4537    adjustment.  So, we do the adjustment here.  The arg pointer (which
4538    is eliminated) points to the virtual frame pointer, while the frame
4539    pointer (which may be eliminated) points to the stack pointer after
4540    the initial adjustments.  */
4541
4542 HOST_WIDE_INT
4543 mips_debugger_offset (addr, offset)
4544      rtx addr;
4545      HOST_WIDE_INT offset;
4546 {
4547   rtx offset2 = const0_rtx;
4548   rtx reg = eliminate_constant_term (addr, &offset2);
4549
4550   if (offset == 0)
4551     offset = INTVAL (offset2);
4552
4553   if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
4554       || reg == hard_frame_pointer_rtx)
4555     {
4556       HOST_WIDE_INT frame_size = (!current_frame_info.initialized)
4557                                   ? compute_frame_size (get_frame_size ())
4558                                   : current_frame_info.total_size;
4559
4560       /* MIPS16 frame is smaller */
4561       if (frame_pointer_needed && TARGET_MIPS16)
4562         frame_size -= current_function_outgoing_args_size;
4563
4564       offset = offset - frame_size;
4565     }
4566
4567   /* sdbout_parms does not want this to crash for unrecognized cases.  */
4568 #if 0
4569   else if (reg != arg_pointer_rtx)
4570     abort_with_insn (addr, "mips_debugger_offset called with non stack/frame/arg pointer.");
4571 #endif
4572
4573   return offset;
4574 }
4575 \f
4576 /* A C compound statement to output to stdio stream STREAM the
4577    assembler syntax for an instruction operand X.  X is an RTL
4578    expression.
4579
4580    CODE is a value that can be used to specify one of several ways
4581    of printing the operand.  It is used when identical operands
4582    must be printed differently depending on the context.  CODE
4583    comes from the `%' specification that was used to request
4584    printing of the operand.  If the specification was just `%DIGIT'
4585    then CODE is 0; if the specification was `%LTR DIGIT' then CODE
4586    is the ASCII code for LTR.
4587
4588    If X is a register, this macro should print the register's name.
4589    The names can be found in an array `reg_names' whose type is
4590    `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.
4591
4592    When the machine description has a specification `%PUNCT' (a `%'
4593    followed by a punctuation character), this macro is called with
4594    a null pointer for X and the punctuation character for CODE.
4595
4596    The MIPS specific codes are:
4597
4598    'X'  X is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
4599    'x'  X is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
4600    'd'  output integer constant in decimal,
4601    'z'  if the operand is 0, use $0 instead of normal operand.
4602    'D'  print second register of double-word register operand.
4603    'L'  print low-order register of double-word register operand.
4604    'M'  print high-order register of double-word register operand.
4605    'C'  print part of opcode for a branch condition.
4606    'F'  print part of opcode for a floating-point branch condition.
4607    'N'  print part of opcode for a branch condition, inverted.
4608    'W'  print part of opcode for a floating-point branch condition, inverted.
4609    'S'  X is CODE_LABEL, print with prefix of "LS" (for embedded switch).
4610    'B'  print 'z' for EQ, 'n' for NE
4611    'b'  print 'n' for EQ, 'z' for NE
4612    'T'  print 'f' for EQ, 't' for NE
4613    't'  print 't' for EQ, 'f' for NE
4614    'Z'  print register and a comma, but print nothing for $fcc0
4615    '('  Turn on .set noreorder
4616    ')'  Turn on .set reorder
4617    '['  Turn on .set noat
4618    ']'  Turn on .set at
4619    '<'  Turn on .set nomacro
4620    '>'  Turn on .set macro
4621    '{'  Turn on .set volatile (not GAS)
4622    '}'  Turn on .set novolatile (not GAS)
4623    '&'  Turn on .set noreorder if filling delay slots
4624    '*'  Turn on both .set noreorder and .set nomacro if filling delay slots
4625    '!'  Turn on .set nomacro if filling delay slots
4626    '#'  Print nop if in a .set noreorder section.
4627    '?'  Print 'l' if we are to use a branch likely instead of normal branch.
4628    '@'  Print the name of the assembler temporary register (at or $1).
4629    '.'  Print the name of the register with a hard-wired zero (zero or $0).
4630    '^'  Print the name of the pic call-through register (t9 or $25).
4631    '$'  Print the name of the stack pointer register (sp or $29).
4632    '+'  Print the name of the gp register (gp or $28).
4633    '~'  Output an branch alignment to LABEL_ALIGN(NULL).  */
4634
4635 void
4636 print_operand (file, op, letter)
4637      FILE *file;                /* file to write to */
4638      rtx op;                    /* operand to print */
4639      int letter;                /* %<letter> or 0 */
4640 {
4641   register enum rtx_code code;
4642
4643   if (PRINT_OPERAND_PUNCT_VALID_P (letter))
4644     {
4645       switch (letter)
4646         {
4647         case '?':
4648           if (mips_branch_likely)
4649             putc ('l', file);
4650           break;
4651
4652         case '@':
4653           fputs (reg_names [GP_REG_FIRST + 1], file);
4654           break;
4655
4656         case '^':
4657           fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
4658           break;
4659
4660         case '.':
4661           fputs (reg_names [GP_REG_FIRST + 0], file);
4662           break;
4663
4664         case '$':
4665           fputs (reg_names[STACK_POINTER_REGNUM], file);
4666           break;
4667
4668         case '+':
4669           fputs (reg_names[GP_REG_FIRST + 28], file);
4670           break;
4671
4672         case '&':
4673           if (final_sequence != 0 && set_noreorder++ == 0)
4674             fputs (".set\tnoreorder\n\t", file);
4675           break;
4676
4677         case '*':
4678           if (final_sequence != 0)
4679             {
4680               if (set_noreorder++ == 0)
4681                 fputs (".set\tnoreorder\n\t", file);
4682
4683               if (set_nomacro++ == 0)
4684                 fputs (".set\tnomacro\n\t", file);
4685             }
4686           break;
4687
4688         case '!':
4689           if (final_sequence != 0 && set_nomacro++ == 0)
4690             fputs ("\n\t.set\tnomacro", file);
4691           break;
4692
4693         case '#':
4694           if (set_noreorder != 0)
4695             fputs ("\n\tnop", file);
4696           else if (TARGET_STATS)
4697             fputs ("\n\t#nop", file);
4698
4699           break;
4700
4701         case '(':
4702           if (set_noreorder++ == 0)
4703             fputs (".set\tnoreorder\n\t", file);
4704           break;
4705
4706         case ')':
4707           if (set_noreorder == 0)
4708             error ("internal error: %%) found without a %%( in assembler pattern");
4709
4710           else if (--set_noreorder == 0)
4711             fputs ("\n\t.set\treorder", file);
4712
4713           break;
4714
4715         case '[':
4716           if (set_noat++ == 0)
4717             fputs (".set\tnoat\n\t", file);
4718           break;
4719
4720         case ']': 
4721           if (set_noat == 0)
4722             error ("internal error: %%] found without a %%[ in assembler pattern");
4723           else if (--set_noat == 0)
4724             fputs ("\n\t.set\tat", file);
4725
4726           break;
4727
4728         case '<':
4729           if (set_nomacro++ == 0)
4730             fputs (".set\tnomacro\n\t", file);
4731           break;
4732
4733         case '>':
4734           if (set_nomacro == 0)
4735             error ("internal error: %%> found without a %%< in assembler pattern");
4736           else if (--set_nomacro == 0)
4737             fputs ("\n\t.set\tmacro", file);
4738
4739           break;
4740
4741         case '{':
4742           if (set_volatile++ == 0)
4743             fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
4744           break;
4745
4746         case '}':
4747           if (set_volatile == 0)
4748             error ("internal error: %%} found without a %%{ in assembler pattern");
4749           else if (--set_volatile == 0)
4750             fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
4751
4752           break;
4753
4754         case '~':
4755           {
4756             if (align_labels_log > 0)
4757               ASM_OUTPUT_ALIGN (file, align_labels_log);
4758           }
4759         break;
4760
4761         default:
4762           error ("PRINT_OPERAND: Unknown punctuation '%c'", letter);
4763           break;
4764         }
4765
4766       return;
4767     }
4768
4769   if (! op)
4770     {
4771       error ("PRINT_OPERAND null pointer");
4772       return;
4773     }
4774
4775   code = GET_CODE (op);
4776
4777   if (code == SIGN_EXTEND)
4778     op = XEXP (op, 0), code = GET_CODE (op);
4779
4780   if (letter == 'C')
4781     switch (code)
4782       {
4783       case EQ:  fputs ("eq",  file); break;
4784       case NE:  fputs ("ne",  file); break;
4785       case GT:  fputs ("gt",  file); break;
4786       case GE:  fputs ("ge",  file); break;
4787       case LT:  fputs ("lt",  file); break;
4788       case LE:  fputs ("le",  file); break;
4789       case GTU: fputs ("gtu", file); break;
4790       case GEU: fputs ("geu", file); break;
4791       case LTU: fputs ("ltu", file); break;
4792       case LEU: fputs ("leu", file); break;
4793       default:
4794         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
4795       }
4796
4797   else if (letter == 'N')
4798     switch (code)
4799       {
4800       case EQ:  fputs ("ne",  file); break;
4801       case NE:  fputs ("eq",  file); break;
4802       case GT:  fputs ("le",  file); break;
4803       case GE:  fputs ("lt",  file); break;
4804       case LT:  fputs ("ge",  file); break;
4805       case LE:  fputs ("gt",  file); break;
4806       case GTU: fputs ("leu", file); break;
4807       case GEU: fputs ("ltu", file); break;
4808       case LTU: fputs ("geu", file); break;
4809       case LEU: fputs ("gtu", file); break;
4810       default:
4811         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
4812       }
4813
4814   else if (letter == 'F')
4815     switch (code)
4816       {
4817       case EQ: fputs ("c1f", file); break;
4818       case NE: fputs ("c1t", file); break;
4819       default:
4820         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
4821       }
4822
4823   else if (letter == 'W')
4824     switch (code)
4825       {
4826       case EQ: fputs ("c1t", file); break;
4827       case NE: fputs ("c1f", file); break;
4828       default:
4829         abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
4830       }
4831
4832   else if (letter == 'S')
4833     {
4834       char buffer[100];
4835
4836       ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
4837       assemble_name (file, buffer);
4838     }
4839
4840   else if (letter == 'Z')
4841     {
4842       register int regnum;
4843
4844       if (code != REG)
4845         abort ();
4846
4847       regnum = REGNO (op);
4848       if (! ST_REG_P (regnum))
4849         abort ();
4850
4851       if (regnum != ST_REG_FIRST)
4852         fprintf (file, "%s,", reg_names[regnum]);
4853     }
4854
4855   else if (code == REG || code == SUBREG)
4856     {
4857       register int regnum;
4858
4859       if (code == REG)
4860         regnum = REGNO (op);
4861       else
4862         regnum = true_regnum (op);
4863
4864       if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
4865           || (letter == 'L' && WORDS_BIG_ENDIAN)
4866           || letter == 'D')
4867         regnum++;
4868
4869       fprintf (file, "%s", reg_names[regnum]);
4870     }
4871
4872   else if (code == MEM)
4873     output_address (XEXP (op, 0));
4874
4875   else if (code == CONST_DOUBLE
4876            && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
4877     {
4878       REAL_VALUE_TYPE d;
4879       char s[30];
4880
4881       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
4882       REAL_VALUE_TO_DECIMAL (d, "%.20e", s);
4883       fprintf (file, s);
4884     }
4885
4886   else if (letter == 'x' && GET_CODE (op) == CONST_INT)
4887     fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
4888
4889   else if (letter == 'X' && GET_CODE(op) == CONST_INT)
4890     fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
4891
4892   else if (letter == 'd' && GET_CODE(op) == CONST_INT)
4893     fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
4894
4895   else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
4896     fputs (reg_names[GP_REG_FIRST], file);
4897
4898   else if (letter == 'd' || letter == 'x' || letter == 'X')
4899     fatal ("PRINT_OPERAND: letter %c was found & insn was not CONST_INT",
4900            letter);
4901
4902   else if (letter == 'B')
4903     fputs (code == EQ ? "z" : "n", file);
4904   else if (letter == 'b')
4905     fputs (code == EQ ? "n" : "z", file);
4906   else if (letter == 'T')
4907     fputs (code == EQ ? "f" : "t", file);
4908   else if (letter == 't')
4909     fputs (code == EQ ? "t" : "f", file);
4910
4911   else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
4912     {
4913       /* This case arises on the mips16; see mips16_gp_pseudo_reg.  */
4914       print_operand (file, XEXP (op, 0), letter);
4915     }
4916
4917   else if (TARGET_MIPS16 && code == CONST && mips16_gp_offset_p (op))
4918     {
4919       fputs ("%gprel(", file);
4920       mips16_output_gp_offset (file, op);
4921       fputs (")", file);
4922     }
4923
4924   else
4925     output_addr_const (file, op);
4926 }
4927 \f
4928 /* A C compound statement to output to stdio stream STREAM the
4929    assembler syntax for an instruction operand that is a memory
4930    reference whose address is ADDR.  ADDR is an RTL expression.
4931
4932    On some machines, the syntax for a symbolic address depends on
4933    the section that the address refers to.  On these machines,
4934    define the macro `ENCODE_SECTION_INFO' to store the information
4935    into the `symbol_ref', and then check for it here.  */
4936
4937 void
4938 print_operand_address (file, addr)
4939      FILE *file;
4940      rtx addr;
4941 {
4942   if (!addr)
4943     error ("PRINT_OPERAND_ADDRESS, null pointer");
4944
4945   else
4946     switch (GET_CODE (addr))
4947       {
4948       case REG:
4949         if (! TARGET_MIPS16 && REGNO (addr) == ARG_POINTER_REGNUM)
4950           abort_with_insn (addr, "Arg pointer not eliminated.");
4951
4952         fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
4953         break;
4954
4955       case LO_SUM:
4956         {
4957           register rtx arg0 = XEXP (addr, 0);
4958           register rtx arg1 = XEXP (addr, 1);
4959
4960           if (! mips_split_addresses)
4961             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, Spurious LO_SUM.");
4962
4963           if (GET_CODE (arg0) != REG)
4964             abort_with_insn (addr,
4965                              "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG.");
4966
4967           fprintf (file, "%%lo(");
4968           print_operand_address (file, arg1);
4969           fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
4970         }
4971         break;
4972
4973       case PLUS:
4974         {
4975           register rtx reg = 0;
4976           register rtx offset = 0;
4977           register rtx arg0 = XEXP (addr, 0);
4978           register rtx arg1 = XEXP (addr, 1);
4979
4980           if (GET_CODE (arg0) == REG)
4981             {
4982               reg = arg0;
4983               offset = arg1;
4984               if (GET_CODE (offset) == REG)
4985                 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
4986             }
4987
4988           else if (GET_CODE (arg1) == REG)
4989               reg = arg1, offset = arg0;
4990           else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
4991             {
4992               output_addr_const (file, addr);
4993               break;
4994             }
4995           else
4996             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
4997
4998           if (! CONSTANT_P (offset))
4999             abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
5000
5001           if (REGNO (reg) == ARG_POINTER_REGNUM)
5002             abort_with_insn (addr, "Arg pointer not eliminated.");
5003
5004           if (TARGET_MIPS16
5005               && GET_CODE (offset) == CONST
5006               && mips16_gp_offset_p (offset))
5007             {
5008               fputs ("%gprel(", file);
5009               mips16_output_gp_offset (file, offset);
5010               fputs (")", file);
5011             }
5012           else
5013             output_addr_const (file, offset);
5014           fprintf (file, "(%s)", reg_names [REGNO (reg)]);
5015         }
5016         break;
5017
5018       case LABEL_REF:
5019       case SYMBOL_REF:
5020       case CONST_INT:
5021       case CONST:
5022         output_addr_const (file, addr);
5023         break;
5024
5025       default:
5026         abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
5027         break;
5028     }
5029 }
5030
5031 \f
5032 /* If optimizing for the global pointer, keep track of all of the externs, so
5033    that at the end of the file, we can emit the appropriate .extern
5034    declaration for them, before writing out the text section.  We assume all
5035    names passed to us are in the permanent obstack, so they will be valid at
5036    the end of the compilation.
5037
5038    If we have -G 0, or the extern size is unknown, or the object is in a user
5039    specified section that is not .sbss/.sdata, don't bother emitting the
5040    .externs.  In the case of user specified sections this behaviour is
5041    required as otherwise GAS will think the object lives in .sbss/.sdata.  */
5042
5043 int
5044 mips_output_external (file, decl, name)
5045      FILE *file ATTRIBUTE_UNUSED;
5046      tree decl;
5047      char *name;
5048 {
5049   register struct extern_list *p;
5050   int len;
5051   tree section_name;
5052
5053   if (TARGET_GP_OPT
5054       && TREE_CODE (decl) != FUNCTION_DECL
5055       && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
5056       && ((section_name = DECL_SECTION_NAME (decl)) == NULL
5057           || strcmp (TREE_STRING_POINTER (section_name), ".sbss") == 0
5058           || strcmp (TREE_STRING_POINTER (section_name), ".sdata") == 0))
5059     {
5060       p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5061       p->next = extern_head;
5062       p->name = name;
5063       p->size = len;
5064       extern_head = p;
5065     }
5066
5067 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5068   if (TREE_CODE (decl) == FUNCTION_DECL
5069       /* ??? Don't include alloca, since gcc will always expand it
5070          inline.  If we don't do this, the C++ library fails to build.  */
5071       && strcmp (name, "alloca")
5072       /* ??? Don't include __builtin_next_arg, because then gcc will not
5073          bootstrap under Irix 5.1.  */
5074       && strcmp (name, "__builtin_next_arg"))
5075     {
5076       p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5077       p->next = extern_head;
5078       p->name = name;
5079       p->size = -1;
5080       extern_head = p;
5081     }
5082 #endif
5083
5084   return 0;
5085 }
5086
5087 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5088 int
5089 mips_output_external_libcall (file, name)
5090      FILE *file;
5091      char *name;
5092 {
5093   register struct extern_list *p;
5094
5095   p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5096   p->next = extern_head;
5097   p->name = name;
5098   p->size = -1;
5099   extern_head = p;
5100
5101   return 0;
5102 }
5103 #endif
5104 \f
5105 /* Compute a string to use as a temporary file name.  */
5106
5107 /* On MSDOS, write temp files in current dir
5108    because there's no place else we can expect to use.  */
5109 #if __MSDOS__
5110 #ifndef P_tmpdir
5111 #define P_tmpdir "./"
5112 #endif
5113 #endif
5114
5115 static FILE *
5116 make_temp_file ()
5117 {
5118   FILE *stream;
5119   const char *base = getenv ("TMPDIR");
5120   int len;
5121
5122   if (base == 0)
5123     {
5124 #ifdef P_tmpdir
5125       if (access (P_tmpdir, R_OK | W_OK) == 0)
5126         base = P_tmpdir;
5127       else
5128 #endif
5129         if (access ("/usr/tmp", R_OK | W_OK) == 0)
5130           base = "/usr/tmp/";
5131         else
5132           base = "/tmp/";
5133     }
5134
5135   len = strlen (base);
5136   /* temp_filename is global, so we must use malloc, not alloca.  */
5137   temp_filename = (char *) xmalloc (len + sizeof("/ctXXXXXX"));
5138   strcpy (temp_filename, base);
5139   if (len > 0 && temp_filename[len-1] != '/')
5140     temp_filename[len++] = '/';
5141
5142   strcpy (temp_filename + len, "ctXXXXXX");
5143   mktemp (temp_filename);
5144
5145   stream = fopen (temp_filename, "w+");
5146   if (!stream)
5147     pfatal_with_name (temp_filename);
5148
5149 #ifndef __MSDOS__
5150   /* In MSDOS, we cannot unlink the temporary file until we are finished using
5151      it.  Otherwise, we delete it now, so that it will be gone even if the
5152      compiler happens to crash.  */
5153   unlink (temp_filename);
5154 #endif
5155   return stream;
5156 }
5157 \f
5158 /* Emit a new filename to a stream.  If this is MIPS ECOFF, watch out
5159    for .file's that start within a function.  If we are smuggling stabs, try to
5160    put out a MIPS ECOFF file and a stab.  */
5161
5162 void
5163 mips_output_filename (stream, name)
5164      FILE *stream;
5165      char *name;
5166 {
5167   static int first_time = 1;
5168   char ltext_label_name[100];
5169
5170   if (first_time)
5171     {
5172       first_time = 0;
5173       SET_FILE_NUMBER ();
5174       current_function_file = name;
5175       ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5176       /* This tells mips-tfile that stabs will follow.  */
5177       if (!TARGET_GAS && write_symbols == DBX_DEBUG)
5178         fprintf (stream, "\t#@stabs\n");
5179     }
5180
5181   else if (write_symbols == DBX_DEBUG)
5182     {
5183       ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
5184       fprintf (stream, "%s ", ASM_STABS_OP);
5185       output_quoted_string (stream, name);
5186       fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
5187     }
5188
5189   else if (name != current_function_file
5190       && strcmp (name, current_function_file) != 0)
5191     {
5192       if (inside_function && !TARGET_GAS)
5193         {
5194           if (!file_in_function_warning)
5195             {
5196               file_in_function_warning = 1;
5197               ignore_line_number = 1;
5198               warning ("MIPS ECOFF format does not allow changing filenames within functions with #line");
5199             }
5200         }
5201       else
5202         {
5203           SET_FILE_NUMBER ();
5204           current_function_file = name;
5205           ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5206         }
5207     }
5208 }
5209 \f
5210 /* Emit a linenumber.  For encapsulated stabs, we need to put out a stab
5211    as well as a .loc, since it is possible that MIPS ECOFF might not be
5212    able to represent the location for inlines that come from a different
5213    file.  */
5214
5215 void
5216 mips_output_lineno (stream, line)
5217      FILE *stream;
5218      int line;
5219 {
5220   if (write_symbols == DBX_DEBUG)
5221     {
5222       ++sym_lineno;
5223       fprintf (stream, "%sLM%d:\n\t%s %d,0,%d,%sLM%d\n",
5224                LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
5225                LOCAL_LABEL_PREFIX, sym_lineno);
5226     }
5227
5228   else
5229     {
5230       fprintf (stream, "\n\t%s.loc\t%d %d\n",
5231                (ignore_line_number) ? "#" : "",
5232                num_source_filenames, line);
5233   
5234       LABEL_AFTER_LOC (stream);
5235     }
5236 }
5237 \f
5238 /* If defined, a C statement to be executed just prior to the output of
5239    assembler code for INSN, to modify the extracted operands so they will be
5240    output differently.
5241
5242    Here the argument OPVEC is the vector containing the operands extracted
5243    from INSN, and NOPERANDS is the number of elements of the vector which
5244    contain meaningful data for this insn.  The contents of this vector are
5245    what will be used to convert the insn template into assembler code, so you
5246    can change the assembler output by changing the contents of the vector.
5247
5248    We use it to check if the current insn needs a nop in front of it because
5249    of load delays, and also to update the delay slot statistics.  */
5250
5251 /* ??? There is no real need for this function, because it never actually
5252    emits a NOP anymore.  */
5253
5254 void
5255 final_prescan_insn (insn, opvec, noperands)
5256      rtx insn;
5257      rtx opvec[] ATTRIBUTE_UNUSED;
5258      int noperands ATTRIBUTE_UNUSED;
5259 {
5260   if (dslots_number_nops > 0)
5261     {
5262       rtx pattern = PATTERN (insn);
5263       int length = get_attr_length (insn);
5264
5265       /* Do we need to emit a NOP? */
5266       if (length == 0
5267           || (mips_load_reg != 0 && reg_mentioned_p (mips_load_reg,  pattern))
5268           || (mips_load_reg2 != 0 && reg_mentioned_p (mips_load_reg2, pattern))
5269           || (mips_load_reg3 != 0 && reg_mentioned_p (mips_load_reg3, pattern))
5270           || (mips_load_reg4 != 0
5271               && reg_mentioned_p (mips_load_reg4, pattern)))
5272         fputs ("\t#nop\n", asm_out_file);
5273
5274       else
5275         dslots_load_filled++;
5276
5277       while (--dslots_number_nops > 0)
5278         fputs ("\t#nop\n", asm_out_file);
5279
5280       mips_load_reg = 0;
5281       mips_load_reg2 = 0;
5282       mips_load_reg3 = 0;
5283       mips_load_reg4 = 0;
5284     }
5285
5286   if (TARGET_STATS
5287       && (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CALL_INSN))
5288     dslots_jump_total++;
5289 }
5290 \f
5291 /* Output at beginning of assembler file.
5292
5293    If we are optimizing to use the global pointer, create a temporary file to
5294    hold all of the text stuff, and write it out to the end. This is needed
5295    because the MIPS assembler is evidently one pass, and if it hasn't seen the
5296    relevant .comm/.lcomm/.extern/.sdata declaration when the code is
5297    processed, it generates a two instruction sequence.  */
5298
5299 void
5300 mips_asm_file_start (stream)
5301      FILE *stream;
5302 {
5303   ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
5304
5305   /* Versions of the MIPS assembler before 2.20 generate errors if a branch
5306      inside of a .set noreorder section jumps to a label outside of the .set
5307      noreorder section.  Revision 2.20 just set nobopt silently rather than
5308      fixing the bug.  */
5309
5310   if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
5311     fprintf (stream, "\t.set\tnobopt\n");
5312
5313   /* Generate the pseudo ops that System V.4 wants.  */
5314 #ifndef ABICALLS_ASM_OP
5315 #define ABICALLS_ASM_OP ".abicalls"
5316 #endif
5317   if (TARGET_ABICALLS)
5318     /* ??? but do not want this (or want pic0) if -non-shared? */
5319     fprintf (stream, "\t%s\n", ABICALLS_ASM_OP);
5320
5321   if (TARGET_MIPS16)
5322     fprintf (stream, "\t.set\tmips16\n");
5323
5324   /* Start a section, so that the first .popsection directive is guaranteed
5325      to have a previously defined section to pop back to.  */
5326   if (mips_abi != ABI_32 && mips_abi != ABI_O64 && mips_abi != ABI_EABI)
5327     fprintf (stream, "\t.section\t.text\n");
5328
5329   /* This code exists so that we can put all externs before all symbol
5330      references.  This is necessary for the MIPS assembler's global pointer
5331      optimizations to work.  */
5332   if (TARGET_FILE_SWITCHING && ! TARGET_MIPS16)
5333     {
5334       asm_out_data_file = stream;
5335       asm_out_text_file = make_temp_file ();
5336     }
5337
5338   else
5339     asm_out_data_file = asm_out_text_file = stream;
5340
5341   if (flag_verbose_asm)
5342     fprintf (stream, "\n%s -G value = %d, Cpu = %s, ISA = %d\n",
5343              ASM_COMMENT_START,
5344              mips_section_threshold, mips_cpu_string, mips_isa);
5345 }
5346 \f
5347 /* If we are optimizing the global pointer, emit the text section now and any
5348    small externs which did not have .comm, etc that are needed.  Also, give a
5349    warning if the data area is more than 32K and -pic because 3 instructions
5350    are needed to reference the data pointers.  */
5351
5352 void
5353 mips_asm_file_end (file)
5354      FILE *file;
5355 {
5356   char buffer[8192];
5357   tree name_tree;
5358   struct extern_list *p;
5359   int len;
5360
5361   if (HALF_PIC_P ())
5362     {
5363       HALF_PIC_FINISH (file);
5364     }
5365
5366   if (extern_head)
5367     {
5368       fputs ("\n", file);
5369
5370       for (p = extern_head; p != 0; p = p->next)
5371         {
5372           name_tree = get_identifier (p->name);
5373
5374           /* Positively ensure only one .extern for any given symbol.  */
5375           if (! TREE_ASM_WRITTEN (name_tree))
5376             {
5377               TREE_ASM_WRITTEN (name_tree) = 1;
5378 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5379               if (p->size == -1)
5380                 ASM_OUTPUT_UNDEF_FUNCTION (file, p->name);
5381               else
5382 #endif
5383                 {
5384                   fputs ("\t.extern\t", file);
5385                   assemble_name (file, p->name);
5386                   fprintf (file, ", %d\n", p->size);
5387                 }
5388             }
5389         }
5390     }
5391       
5392   if (TARGET_FILE_SWITCHING && ! TARGET_MIPS16)
5393     {
5394       fprintf (file, "\n\t.text\n");
5395       rewind (asm_out_text_file);
5396       if (ferror (asm_out_text_file))
5397         fatal_io_error (temp_filename);
5398
5399       while ((len = fread (buffer, 1, sizeof (buffer), asm_out_text_file)) > 0)
5400         if ((int) fwrite (buffer, 1, len, file) != len)
5401           pfatal_with_name (asm_file_name);
5402
5403       if (len < 0)
5404         pfatal_with_name (temp_filename);
5405
5406       if (fclose (asm_out_text_file) != 0)
5407         pfatal_with_name (temp_filename);
5408
5409 #ifdef __MSDOS__
5410       unlink (temp_filename);
5411 #endif
5412     }
5413 }
5414
5415 /* Emit either a label, .comm, or .lcomm directive, and mark that the symbol
5416    is used, so that we don't emit an .extern for it in mips_asm_file_end.  */
5417
5418 void
5419 mips_declare_object (stream, name, init_string, final_string, size)
5420      FILE *stream;
5421      char *name;
5422      char *init_string;
5423      char *final_string;
5424      int size;
5425 {
5426   fputs (init_string, stream);          /* "", "\t.comm\t", or "\t.lcomm\t" */
5427   assemble_name (stream, name);
5428   fprintf (stream, final_string, size); /* ":\n", ",%u\n", ",%u\n" */
5429
5430   if (TARGET_GP_OPT)
5431     {
5432       tree name_tree = get_identifier (name);
5433       TREE_ASM_WRITTEN (name_tree) = 1;
5434     }
5435 }
5436 \f
5437 /* Output a double precision value to the assembler.  If both the
5438    host and target are IEEE, emit the values in hex.  */
5439
5440 void
5441 mips_output_double (stream, value)
5442      FILE *stream;
5443      REAL_VALUE_TYPE value;
5444 {
5445 #ifdef REAL_VALUE_TO_TARGET_DOUBLE
5446   long value_long[2];
5447   REAL_VALUE_TO_TARGET_DOUBLE (value, value_long);
5448
5449   fprintf (stream, "\t.word\t0x%08lx\t\t# %.20g\n\t.word\t0x%08lx\n",
5450            value_long[0], value, value_long[1]);
5451 #else
5452   fprintf (stream, "\t.double\t%.20g\n", value);
5453 #endif
5454 }
5455
5456 /* Output a single precision value to the assembler.  If both the
5457    host and target are IEEE, emit the values in hex.  */
5458
5459 void
5460 mips_output_float (stream, value)
5461      FILE *stream;
5462      REAL_VALUE_TYPE value;
5463 {
5464 #ifdef REAL_VALUE_TO_TARGET_SINGLE
5465   long value_long;
5466   REAL_VALUE_TO_TARGET_SINGLE (value, value_long);
5467
5468   fprintf (stream, "\t.word\t0x%08lx\t\t# %.12g (float)\n", value_long, value);
5469 #else
5470   fprintf (stream, "\t.float\t%.12g\n", value);
5471 #endif
5472 }
5473 \f
5474 /* Return the bytes needed to compute the frame pointer from the current
5475    stack pointer.
5476
5477    Mips stack frames look like:
5478
5479              Before call                        After call
5480         +-----------------------+       +-----------------------+
5481    high |                       |       |                       |
5482    mem. |                       |       |                       |
5483         |  caller's temps.      |       |  caller's temps.      |
5484         |                       |       |                       |
5485         +-----------------------+       +-----------------------+
5486         |                       |       |                       |
5487         |  arguments on stack.  |       |  arguments on stack.  |
5488         |                       |       |                       |
5489         +-----------------------+       +-----------------------+
5490         |  4 words to save      |       |  4 words to save      |
5491         |  arguments passed     |       |  arguments passed     |
5492         |  in registers, even   |       |  in registers, even   |
5493     SP->|  if not passed.       |  VFP->|  if not passed.       |
5494         +-----------------------+       +-----------------------+
5495                                         |                       |
5496                                         |  fp register save     |
5497                                         |                       |
5498                                         +-----------------------+
5499                                         |                       |
5500                                         |  gp register save     |
5501                                         |                       |
5502                                         +-----------------------+
5503                                         |                       |
5504                                         |  local variables      |
5505                                         |                       |
5506                                         +-----------------------+
5507                                         |                       |
5508                                         |  alloca allocations   |
5509                                         |                       |
5510                                         +-----------------------+
5511                                         |                       |
5512                                         |  GP save for V.4 abi  |
5513                                         |                       |
5514                                         +-----------------------+
5515                                         |                       |
5516                                         |  arguments on stack   |
5517                                         |                       |
5518                                         +-----------------------+
5519                                         |  4 words to save      |
5520                                         |  arguments passed     |
5521                                         |  in registers, even   |
5522    low                              SP->|  if not passed.       |
5523    memory                               +-----------------------+
5524
5525 */
5526
5527 HOST_WIDE_INT
5528 compute_frame_size (size)
5529      HOST_WIDE_INT size;        /* # of var. bytes allocated */
5530 {
5531   int regno;
5532   HOST_WIDE_INT total_size;     /* # bytes that the entire frame takes up */
5533   HOST_WIDE_INT var_size;       /* # bytes that variables take up */
5534   HOST_WIDE_INT args_size;      /* # bytes that outgoing arguments take up */
5535   HOST_WIDE_INT extra_size;     /* # extra bytes */
5536   HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
5537   HOST_WIDE_INT gp_reg_size;    /* # bytes needed to store gp regs */
5538   HOST_WIDE_INT fp_reg_size;    /* # bytes needed to store fp regs */
5539   long mask;                    /* mask of saved gp registers */
5540   long fmask;                   /* mask of saved fp registers */
5541   int  fp_inc;                  /* 1 or 2 depending on the size of fp regs */
5542   long fp_bits;                 /* bitmask to use for each fp register */
5543
5544   gp_reg_size = 0;
5545   fp_reg_size = 0;
5546   mask = 0;
5547   fmask = 0;
5548   extra_size = MIPS_STACK_ALIGN (((TARGET_ABICALLS) ? UNITS_PER_WORD : 0));
5549   var_size = MIPS_STACK_ALIGN (size);
5550   args_size = MIPS_STACK_ALIGN (current_function_outgoing_args_size);
5551
5552   /* The MIPS 3.0 linker does not like functions that dynamically
5553      allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
5554      looks like we are trying to create a second frame pointer to the
5555      function, so allocate some stack space to make it happy.  */
5556
5557   if (args_size == 0 && current_function_calls_alloca)
5558     args_size = 4 * UNITS_PER_WORD;
5559
5560   total_size = var_size + args_size + extra_size;
5561
5562   /* Calculate space needed for gp registers.  */
5563   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
5564     {
5565       /* $18 is a special case on the mips16.  It may be used to call
5566          a function which returns a floating point value, but it is
5567          marked in call_used_regs.  $31 is also a special case.  When
5568          not using -mentry, it will be used to copy a return value
5569          into the floating point registers if the return value is
5570          floating point.  */
5571       if (MUST_SAVE_REGISTER (regno)
5572           || (TARGET_MIPS16
5573               && regno == GP_REG_FIRST + 18
5574               && regs_ever_live[regno])
5575           || (TARGET_MIPS16
5576               && regno == GP_REG_FIRST + 31
5577               && mips16_hard_float
5578               && ! mips_entry
5579               && ! aggregate_value_p (DECL_RESULT (current_function_decl))
5580               && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
5581                   == MODE_FLOAT)
5582               && (! TARGET_SINGLE_FLOAT
5583                   || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
5584                       <= 4))))
5585         {
5586           gp_reg_size += GET_MODE_SIZE (gpr_mode);
5587           mask |= 1L << (regno - GP_REG_FIRST);
5588
5589           /* The entry and exit pseudo instructions can not save $17
5590              without also saving $16.  */
5591           if (mips_entry
5592               && regno == GP_REG_FIRST + 17
5593               && ! MUST_SAVE_REGISTER (GP_REG_FIRST + 16))
5594             {
5595               gp_reg_size += UNITS_PER_WORD;
5596               mask |= 1L << 16;
5597             }
5598         }
5599     }
5600
5601   /* Calculate space needed for fp registers.  */
5602   if (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT)
5603     {
5604       fp_inc = 1;
5605       fp_bits = 1;
5606     }
5607   else
5608     {
5609       fp_inc = 2;
5610       fp_bits = 3;
5611     }
5612
5613   /* This loop must iterate over the same space as its companion in
5614      save_restore_regs.  */
5615   for (regno = (FP_REG_LAST - fp_inc + 1);
5616        regno >= FP_REG_FIRST;
5617        regno -= fp_inc)
5618     {
5619       if (regs_ever_live[regno] && !call_used_regs[regno])
5620         {
5621           fp_reg_size += fp_inc * UNITS_PER_FPREG;
5622           fmask |= fp_bits << (regno - FP_REG_FIRST);
5623         }
5624     }
5625
5626   gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
5627   total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
5628
5629   /* The gp reg is caller saved in the 32 bit ABI, so there is no need
5630      for leaf routines (total_size == extra_size) to save the gp reg.
5631      The gp reg is callee saved in the 64 bit ABI, so all routines must
5632      save the gp reg.  This is not a leaf routine if -p, because of the
5633      call to mcount.  */
5634   if (total_size == extra_size 
5635       && (mips_abi == ABI_32 || mips_abi == ABI_O64 || mips_abi == ABI_EABI)
5636       && ! profile_flag)
5637     total_size = extra_size = 0;
5638   else if (TARGET_ABICALLS)
5639     {
5640       /* Add the context-pointer to the saved registers.  */
5641       gp_reg_size += UNITS_PER_WORD;
5642       mask |= 1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST);
5643       total_size -= gp_reg_rounded;
5644       gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
5645       total_size += gp_reg_rounded;
5646     }
5647
5648   /* Add in space reserved on the stack by the callee for storing arguments
5649      passed in registers.  */
5650   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
5651     total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
5652
5653   /* The entry pseudo instruction will allocate 32 bytes on the stack.  */
5654   if (mips_entry && total_size > 0 && total_size < 32)
5655     total_size = 32;
5656
5657   /* Save other computed information.  */
5658   current_frame_info.total_size = total_size;
5659   current_frame_info.var_size = var_size;
5660   current_frame_info.args_size = args_size;
5661   current_frame_info.extra_size = extra_size;
5662   current_frame_info.gp_reg_size = gp_reg_size;
5663   current_frame_info.fp_reg_size = fp_reg_size;
5664   current_frame_info.mask = mask;
5665   current_frame_info.fmask = fmask;
5666   current_frame_info.initialized = reload_completed;
5667   current_frame_info.num_gp = gp_reg_size / UNITS_PER_WORD;
5668   current_frame_info.num_fp = fp_reg_size / (fp_inc * UNITS_PER_FPREG);
5669
5670   if (mask)
5671     {
5672       unsigned long offset;
5673
5674       /* When using mips_entry, the registers are always saved at the
5675          top of the stack.  */
5676       if (! mips_entry)
5677         offset = (args_size + extra_size + var_size
5678                   + gp_reg_size - GET_MODE_SIZE (gpr_mode));
5679       else
5680         offset = total_size - GET_MODE_SIZE (gpr_mode);
5681
5682       current_frame_info.gp_sp_offset = offset;
5683       current_frame_info.gp_save_offset = offset - total_size;
5684     }
5685   else
5686     {
5687       current_frame_info.gp_sp_offset = 0;
5688       current_frame_info.gp_save_offset = 0;
5689     }
5690
5691   if (fmask)
5692     {
5693       unsigned long offset = (args_size + extra_size + var_size
5694                               + gp_reg_rounded + fp_reg_size
5695                               - fp_inc * UNITS_PER_FPREG);
5696       current_frame_info.fp_sp_offset = offset;
5697       current_frame_info.fp_save_offset = offset - total_size;
5698     }
5699   else
5700     {
5701       current_frame_info.fp_sp_offset = 0;
5702       current_frame_info.fp_save_offset = 0;
5703     }
5704
5705   /* Ok, we're done.  */
5706   return total_size;
5707 }
5708 \f
5709 /* Common code to emit the insns (or to write the instructions to a file)
5710    to save/restore registers.
5711
5712    Other parts of the code assume that MIPS_TEMP1_REGNUM (aka large_reg)
5713    is not modified within save_restore_insns.  */
5714
5715 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
5716
5717 static void
5718 save_restore_insns (store_p, large_reg, large_offset, file)
5719      int store_p;       /* true if this is prologue */
5720      rtx large_reg;     /* register holding large offset constant or NULL */
5721      long large_offset; /* large constant offset value */
5722      FILE *file;        /* file to write instructions instead of making RTL */
5723 {
5724   long mask = current_frame_info.mask;
5725   long fmask = current_frame_info.fmask;
5726   int regno;
5727   rtx base_reg_rtx;
5728   HOST_WIDE_INT base_offset;
5729   HOST_WIDE_INT gp_offset;
5730   HOST_WIDE_INT fp_offset;
5731   HOST_WIDE_INT end_offset;
5732   rtx insn;
5733
5734   if (frame_pointer_needed
5735       && ! BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST))
5736     abort ();
5737
5738   if (mask == 0 && fmask == 0)
5739     return;
5740
5741   /* Save registers starting from high to low.  The debuggers prefer at least
5742      the return register be stored at func+4, and also it allows us not to
5743      need a nop in the epilog if at least one register is reloaded in
5744      addition to return address.  */
5745
5746   /* Save GP registers if needed.  */
5747   if (mask)
5748     {
5749       /* Pick which pointer to use as a base register.  For small frames, just
5750          use the stack pointer.  Otherwise, use a temporary register.  Save 2
5751          cycles if the save area is near the end of a large frame, by reusing
5752          the constant created in the prologue/epilogue to adjust the stack
5753          frame.  */
5754
5755       gp_offset = current_frame_info.gp_sp_offset;
5756       end_offset
5757         = gp_offset - (current_frame_info.gp_reg_size
5758                        - GET_MODE_SIZE (gpr_mode));
5759
5760       if (gp_offset < 0 || end_offset < 0)
5761         fatal ("gp_offset (%ld) or end_offset (%ld) is less than zero.",
5762                (long) gp_offset, (long) end_offset);
5763
5764       /* If we see a large frame in mips16 mode, we save the registers
5765          before adjusting the stack pointer, and load them afterward.  */
5766       else if (TARGET_MIPS16 && large_offset > 32767)
5767         base_reg_rtx = stack_pointer_rtx, base_offset = large_offset;
5768
5769       else if (gp_offset < 32768)
5770         base_reg_rtx = stack_pointer_rtx, base_offset  = 0;
5771
5772       else if (large_reg != 0
5773                && (unsigned HOST_WIDE_INT) (large_offset - gp_offset) < 32768
5774                && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
5775         {
5776           base_reg_rtx = gen_rtx (REG, Pmode, MIPS_TEMP2_REGNUM);
5777           base_offset = large_offset;
5778           if (file == 0)
5779             {
5780               if (Pmode == DImode)
5781                 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
5782                                               stack_pointer_rtx));
5783               else
5784                 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
5785                                               stack_pointer_rtx));
5786               if (store_p)
5787                 RTX_FRAME_RELATED_P (insn) = 1;
5788             }
5789           else
5790             fprintf (file, "\t%s\t%s,%s,%s\n",
5791                      Pmode == DImode ? "daddu" : "addu",
5792                      reg_names[MIPS_TEMP2_REGNUM],
5793                      reg_names[REGNO (large_reg)],
5794                      reg_names[STACK_POINTER_REGNUM]);
5795         }
5796
5797       else
5798         {
5799           base_reg_rtx = gen_rtx (REG, Pmode, MIPS_TEMP2_REGNUM);
5800           base_offset = gp_offset;
5801           if (file == 0)
5802             {
5803               rtx gp_offset_rtx = GEN_INT (gp_offset);
5804
5805               /* Instruction splitting doesn't preserve the RTX_FRAME_RELATED_P
5806                  bit, so make sure that we don't emit anything that can be
5807                  split.  */
5808               /* ??? There is no DImode ori immediate pattern, so we can only
5809                  do this for 32 bit code.  */
5810               if (large_int (gp_offset_rtx)
5811                   && GET_MODE (base_reg_rtx) == SImode)
5812                 {
5813                   insn = emit_move_insn (base_reg_rtx,
5814                                          GEN_INT (gp_offset & 0xffff0000));
5815                   if (store_p)
5816                     RTX_FRAME_RELATED_P (insn) = 1;
5817                   insn
5818                     = emit_insn (gen_iorsi3 (base_reg_rtx, base_reg_rtx,
5819                                              GEN_INT (gp_offset & 0x0000ffff)));
5820                   if (store_p)
5821                     RTX_FRAME_RELATED_P (insn) = 1;
5822                 }
5823               else
5824                 {
5825                   insn = emit_move_insn (base_reg_rtx, gp_offset_rtx);
5826                   if (store_p)
5827                     RTX_FRAME_RELATED_P (insn) = 1;
5828                 }
5829
5830               if (Pmode == DImode)
5831                 insn = emit_insn (gen_adddi3 (base_reg_rtx, base_reg_rtx,
5832                                               stack_pointer_rtx));
5833               else
5834                 insn = emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx,
5835                                               stack_pointer_rtx));
5836               if (store_p)
5837                 RTX_FRAME_RELATED_P (insn) = 1;
5838             }
5839           else
5840             {
5841               fprintf (file, "\tli\t%s,0x%.08lx\t# ",
5842                        reg_names[MIPS_TEMP2_REGNUM], (long) base_offset);
5843               fprintf (file, HOST_WIDE_INT_PRINT_DEC, base_offset);
5844               fprintf (file, "\n\t%s\t%s,%s,%s\n",
5845                        Pmode == DImode ? "daddu" : "addu",
5846                        reg_names[MIPS_TEMP2_REGNUM],
5847                        reg_names[MIPS_TEMP2_REGNUM],
5848                        reg_names[STACK_POINTER_REGNUM]);
5849             }
5850         }
5851
5852       /* When we restore the registers in MIPS16 mode, then if we are
5853          using a frame pointer, and this is not a large frame, the
5854          current stack pointer will be offset by
5855          current_function_outgoing_args_size.  Doing it this way lets
5856          us avoid offsetting the frame pointer before copying it into
5857          the stack pointer; there is no instruction to set the stack
5858          pointer to the sum of a register and a constant.  */
5859       if (TARGET_MIPS16
5860           && ! store_p
5861           && frame_pointer_needed
5862           && large_offset <= 32767)
5863         base_offset += current_function_outgoing_args_size;
5864
5865       for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
5866         if (BITSET_P (mask, regno - GP_REG_FIRST))
5867           {
5868             if (file == 0)
5869               {
5870                 rtx reg_rtx;
5871                 rtx mem_rtx
5872                   = gen_rtx (MEM, gpr_mode,
5873                              gen_rtx (PLUS, Pmode, base_reg_rtx,
5874                                       GEN_INT (gp_offset - base_offset)));
5875
5876                 RTX_UNCHANGING_P (mem_rtx) = 1;
5877
5878                 /* The mips16 does not have an instruction to load
5879                    $31, so we load $7 instead, and work things out
5880                    in the caller.  */
5881                 if (TARGET_MIPS16 && ! store_p && regno == GP_REG_FIRST + 31)
5882                   reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 7);
5883                 /* The mips16 sometimes needs to save $18.  */
5884                 else if (TARGET_MIPS16
5885                          && regno != GP_REG_FIRST + 31
5886                          && ! M16_REG_P (regno))
5887                   {
5888                     if (! store_p)
5889                       reg_rtx = gen_rtx (REG, gpr_mode, 6);
5890                     else
5891                       {
5892                         reg_rtx = gen_rtx (REG, gpr_mode, 3);
5893                         emit_move_insn (reg_rtx,
5894                                         gen_rtx (REG, gpr_mode, regno));
5895                       }
5896                   }
5897                 else
5898                   reg_rtx = gen_rtx (REG, gpr_mode, regno);
5899
5900                 if (store_p)
5901                   {
5902                     insn = emit_move_insn (mem_rtx, reg_rtx);
5903                     RTX_FRAME_RELATED_P (insn) = 1;
5904                   }
5905                 else if (!TARGET_ABICALLS 
5906                          || (mips_abi != ABI_32 && mips_abi != ABI_O64)
5907                          || regno != (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
5908                   {
5909                     emit_move_insn (reg_rtx, mem_rtx);
5910                     if (TARGET_MIPS16
5911                         && regno != GP_REG_FIRST + 31
5912                         && ! M16_REG_P (regno))
5913                       emit_move_insn (gen_rtx (REG, gpr_mode, regno),
5914                                       reg_rtx);
5915                   }
5916               }
5917             else
5918               {
5919                 if (store_p || !TARGET_ABICALLS 
5920                     || (mips_abi != ABI_32 && mips_abi != ABI_O64)
5921                     || regno != (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
5922                   {
5923                     int r = regno;
5924
5925                     /* The mips16 does not have an instruction to
5926                        load $31, so we load $7 instead, and work
5927                        things out in the caller.  */
5928                     if (TARGET_MIPS16 && ! store_p && r == GP_REG_FIRST + 31)
5929                       r = GP_REG_FIRST + 7;
5930                       /* The mips16 sometimes needs to save $18.  */
5931                     if (TARGET_MIPS16
5932                         && regno != GP_REG_FIRST + 31
5933                         && ! M16_REG_P (regno))
5934                       {
5935                         if (! store_p)
5936                           r = GP_REG_FIRST + 6;
5937                         else
5938                           {
5939                             r = GP_REG_FIRST + 3;
5940                             fprintf (file, "\tmove\t%s,%s\n",
5941                                      reg_names[r], reg_names[regno]);
5942                           }
5943                       }
5944                     fprintf (file, "\t%s\t%s,",
5945                              (TARGET_64BIT
5946                               ? (store_p) ? "sd" : "ld"
5947                               : (store_p) ? "sw" : "lw"),
5948                              reg_names[r]);
5949                     fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
5950                              gp_offset - base_offset);
5951                     fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
5952                     if (! store_p
5953                         && TARGET_MIPS16
5954                         && regno != GP_REG_FIRST + 31
5955                         && ! M16_REG_P (regno))
5956                       fprintf (file, "\tmove\t%s,%s\n",
5957                                reg_names[regno], reg_names[r]);
5958                   }
5959
5960               }
5961             gp_offset -= GET_MODE_SIZE (gpr_mode);
5962           }
5963     }
5964   else
5965     base_reg_rtx = 0, base_offset  = 0;
5966
5967   /* Save floating point registers if needed.  */
5968   if (fmask)
5969     {
5970       int fp_inc = (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT) ? 1 : 2;
5971       int fp_size = fp_inc * UNITS_PER_FPREG;
5972
5973       /* Pick which pointer to use as a base register.  */
5974       fp_offset = current_frame_info.fp_sp_offset;
5975       end_offset = fp_offset - (current_frame_info.fp_reg_size - fp_size);
5976
5977       if (fp_offset < 0 || end_offset < 0)
5978         fatal ("fp_offset (%ld) or end_offset (%ld) is less than zero.",
5979                (long) fp_offset, (long) end_offset);
5980
5981       else if (fp_offset < 32768)
5982         base_reg_rtx = stack_pointer_rtx, base_offset  = 0;
5983
5984       else if (base_reg_rtx != 0
5985                && (unsigned HOST_WIDE_INT) (base_offset - fp_offset) < 32768
5986                && (unsigned HOST_WIDE_INT) (base_offset - end_offset) < 32768)
5987         ;                       /* already set up for gp registers above */
5988
5989       else if (large_reg != 0
5990                && (unsigned HOST_WIDE_INT) (large_offset - fp_offset) < 32768
5991                && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
5992         {
5993           base_reg_rtx = gen_rtx (REG, Pmode, MIPS_TEMP2_REGNUM);
5994           base_offset = large_offset;
5995           if (file == 0)
5996             {
5997               if (Pmode == DImode)
5998                 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
5999                                               stack_pointer_rtx));
6000               else
6001                 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
6002                                               stack_pointer_rtx));
6003               if (store_p)
6004                 RTX_FRAME_RELATED_P (insn) = 1;
6005             }
6006
6007           else
6008             fprintf (file, "\t%s\t%s,%s,%s\n",
6009                      Pmode == DImode ? "daddu" : "addu",
6010                      reg_names[MIPS_TEMP2_REGNUM],
6011                      reg_names[REGNO (large_reg)],
6012                      reg_names[STACK_POINTER_REGNUM]);
6013         }
6014
6015       else
6016         {
6017           base_reg_rtx = gen_rtx (REG, Pmode, MIPS_TEMP2_REGNUM);
6018           base_offset = fp_offset;
6019           if (file == 0)
6020             {
6021               rtx fp_offset_rtx = GEN_INT (fp_offset);
6022
6023               /* Instruction splitting doesn't preserve the RTX_FRAME_RELATED_P
6024                  bit, so make sure that we don't emit anything that can be
6025                  split.  */
6026               /* ??? There is no DImode ori immediate pattern, so we can only
6027                  do this for 32 bit code.  */
6028               if (large_int (fp_offset_rtx)
6029                   && GET_MODE (base_reg_rtx) == SImode)
6030                 {
6031                   insn = emit_move_insn (base_reg_rtx,
6032                                          GEN_INT (fp_offset & 0xffff0000));
6033                   if (store_p)
6034                     RTX_FRAME_RELATED_P (insn) = 1;
6035                   insn = emit_insn (gen_iorsi3 (base_reg_rtx, base_reg_rtx,
6036                                                 GEN_INT (fp_offset & 0x0000ffff)));
6037                   if (store_p)
6038                     RTX_FRAME_RELATED_P (insn) = 1;
6039                 }
6040               else
6041                 {
6042                   insn = emit_move_insn (base_reg_rtx, fp_offset_rtx);
6043                   if (store_p)
6044                     RTX_FRAME_RELATED_P (insn) = 1;
6045                 }
6046
6047               if (store_p)
6048                 RTX_FRAME_RELATED_P (insn) = 1;
6049               if (Pmode == DImode)
6050                 insn = emit_insn (gen_adddi3 (base_reg_rtx, base_reg_rtx,
6051                                               stack_pointer_rtx));
6052               else
6053                 insn = emit_insn (gen_addsi3 (base_reg_rtx, base_reg_rtx,
6054                                               stack_pointer_rtx));
6055               if (store_p)
6056                 RTX_FRAME_RELATED_P (insn) = 1;
6057             }
6058           else
6059             {
6060               fprintf (file, "\tli\t%s,0x%.08lx\t# ",
6061                        reg_names[MIPS_TEMP2_REGNUM], (long) base_offset);
6062               fprintf (file, HOST_WIDE_INT_PRINT_DEC, base_offset);
6063               fprintf (file, "\n\t%s\t%s,%s,%s\n",
6064                        Pmode == DImode ? "daddu" : "addu",
6065                        reg_names[MIPS_TEMP2_REGNUM],
6066                        reg_names[MIPS_TEMP2_REGNUM],
6067                        reg_names[STACK_POINTER_REGNUM]);
6068             }
6069         }
6070
6071       /* This loop must iterate over the same space as its companion in
6072          compute_frame_size.  */
6073       for (regno = (FP_REG_LAST - fp_inc + 1);
6074            regno >= FP_REG_FIRST;
6075            regno -= fp_inc)
6076         if (BITSET_P (fmask, regno - FP_REG_FIRST))
6077           {
6078             if (file == 0)
6079               {
6080                 enum machine_mode sz
6081                   = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
6082                 rtx reg_rtx = gen_rtx (REG, sz, regno);
6083                 rtx mem_rtx = gen_rtx (MEM, sz,
6084                                        gen_rtx (PLUS, Pmode, base_reg_rtx,
6085                                                 GEN_INT (fp_offset
6086                                                          - base_offset)));
6087                 RTX_UNCHANGING_P (mem_rtx) = 1;
6088
6089                 if (store_p)
6090                   {
6091                     insn = emit_move_insn (mem_rtx, reg_rtx);
6092                     RTX_FRAME_RELATED_P (insn) = 1;
6093                   }
6094                 else
6095                   emit_move_insn (reg_rtx, mem_rtx);
6096               }
6097             else
6098               {
6099                 fprintf (file, "\t%s\t%s,",
6100                          (TARGET_SINGLE_FLOAT
6101                           ? (store_p ? "s.s" : "l.s")
6102                           : (store_p ? "s.d" : "l.d")),
6103                          reg_names[regno]);
6104                 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
6105                          fp_offset - base_offset);
6106                 fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
6107               }
6108
6109             fp_offset -= fp_size;
6110           }
6111     }
6112 }
6113 \f
6114 /* Set up the stack and frame (if desired) for the function.  */
6115
6116 void
6117 function_prologue (file, size)
6118      FILE *file;
6119      int size ATTRIBUTE_UNUSED;
6120 {
6121 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6122   char *fnname;
6123 #endif
6124   long tsize = current_frame_info.total_size;
6125
6126   ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
6127
6128 #ifdef SDB_DEBUGGING_INFO
6129   if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6130     ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
6131 #endif
6132
6133   /* In mips16 mode, we may need to generate a 32 bit to handle
6134      floating point arguments.  The linker will arrange for any 32 bit
6135      functions to call this stub, which will then jump to the 16 bit
6136      function proper.  */
6137   if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6138       && current_function_args_info.fp_code != 0)
6139     build_mips16_function_stub (file);
6140
6141   inside_function = 1;
6142
6143 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6144   /* Get the function name the same way that toplev.c does before calling
6145      assemble_start_function.  This is needed so that the name used here
6146      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
6147   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6148
6149   if (!flag_inhibit_size_directive)
6150     {
6151       fputs ("\t.ent\t", file);
6152       assemble_name (file, fnname);
6153       fputs ("\n", file);
6154     }
6155
6156   assemble_name (file, fnname);
6157   fputs (":\n", file);
6158 #endif
6159
6160   if (!flag_inhibit_size_directive)
6161     {
6162       /* .frame FRAMEREG, FRAMESIZE, RETREG */
6163       fprintf (file,
6164                "\t.frame\t%s,%ld,%s\t\t# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
6165                (reg_names[(frame_pointer_needed)
6166                           ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
6167                ((frame_pointer_needed && TARGET_MIPS16)
6168                 ? (tsize - current_function_outgoing_args_size)
6169                 : tsize),
6170                reg_names[31 + GP_REG_FIRST],
6171                current_frame_info.var_size,
6172                current_frame_info.num_gp,
6173                current_frame_info.num_fp,
6174                current_function_outgoing_args_size,
6175                current_frame_info.extra_size);
6176
6177       /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
6178       fprintf (file, "\t.mask\t0x%08lx,%ld\n\t.fmask\t0x%08lx,%ld\n",
6179                current_frame_info.mask,
6180                current_frame_info.gp_save_offset,
6181                current_frame_info.fmask,
6182                current_frame_info.fp_save_offset);
6183
6184       /* Require:
6185          OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
6186          HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
6187     }
6188
6189   if (mips_entry && ! mips_can_use_return_insn ())
6190     {
6191       int save16 = BITSET_P (current_frame_info.mask, 16);
6192       int save17 = BITSET_P (current_frame_info.mask, 17);
6193       int save31 = BITSET_P (current_frame_info.mask, 31);
6194       int savearg = 0;
6195       rtx insn;
6196
6197       /* Look through the initial insns to see if any of them store
6198          the function parameters into the incoming parameter storage
6199          area.  If they do, we delete the insn, and save the register
6200          using the entry pseudo-instruction instead.  We don't try to
6201          look past a label, jump, or call.  */
6202       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
6203         {
6204           rtx note, set, src, dest, base, offset;
6205           int hireg;
6206
6207           if (GET_CODE (insn) == CODE_LABEL
6208               || GET_CODE (insn) == JUMP_INSN
6209               || GET_CODE (insn) == CALL_INSN)
6210             break;
6211           if (GET_CODE (insn) != INSN)
6212             continue;
6213           set = PATTERN (insn);
6214           if (GET_CODE (set) != SET)
6215             continue;
6216
6217           /* An insn storing a function parameter will still have a
6218              REG_EQUIV note on it mentioning the argument pointer.  */
6219           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
6220           if (note == NULL_RTX)
6221             continue;
6222           if (! reg_mentioned_p (arg_pointer_rtx, XEXP (note, 0)))
6223             continue;
6224
6225           src = SET_SRC (set);
6226           if (GET_CODE (src) != REG
6227               || REGNO (src) < GP_REG_FIRST + 4
6228               || REGNO (src) > GP_REG_FIRST + 7)
6229             continue;
6230
6231           dest = SET_DEST (set);
6232           if (GET_CODE (dest) != MEM)
6233             continue;
6234           if (GET_MODE_SIZE (GET_MODE (dest)) == UNITS_PER_WORD)
6235             ;
6236           else if (GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
6237                    && REGNO (src) < GP_REG_FIRST + 7)
6238             ;
6239           else
6240             continue;
6241           offset = const0_rtx;
6242           base = eliminate_constant_term (XEXP (dest, 0), &offset);
6243           if (GET_CODE (base) != REG
6244               || GET_CODE (offset) != CONST_INT)
6245             continue;
6246           if (REGNO (base) == STACK_POINTER_REGNUM
6247               && INTVAL (offset) == tsize + (REGNO (src) - 4) * UNITS_PER_WORD)
6248             ;
6249           else if (REGNO (base) == HARD_FRAME_POINTER_REGNUM
6250                    && (INTVAL (offset)
6251                        == (tsize
6252                            + (REGNO (src) - 4) * UNITS_PER_WORD
6253                            - current_function_outgoing_args_size)))
6254             ;
6255           else
6256             continue;
6257
6258           /* This insn stores a parameter onto the stack, in the same
6259              location where the entry pseudo-instruction will put it.
6260              Delete the insn, and arrange to tell the entry
6261              instruction to save the register.  */
6262           PUT_CODE (insn, NOTE);
6263           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
6264           NOTE_SOURCE_FILE (insn) = 0;
6265
6266           hireg = (REGNO (src)
6267                    + HARD_REGNO_NREGS (REGNO (src), GET_MODE (dest))
6268                    - 1);
6269           if (hireg > savearg)
6270             savearg = hireg;
6271         }
6272
6273       /* If this is a varargs function, we need to save all the
6274          registers onto the stack anyhow.  */
6275       if (current_function_stdarg || current_function_varargs)
6276         savearg = GP_REG_FIRST + 7;
6277
6278       fprintf (file, "\tentry\t");
6279       if (savearg > 0)
6280         {
6281           if (savearg == GP_REG_FIRST + 4)
6282             fprintf (file, "%s", reg_names[savearg]);
6283           else
6284             fprintf (file, "%s-%s", reg_names[GP_REG_FIRST + 4],
6285                      reg_names[savearg]);
6286         }
6287       if (save16 || save17)
6288         {
6289           if (savearg > 0)
6290             fprintf (file, ",");
6291           fprintf (file, "%s", reg_names[GP_REG_FIRST + 16]);
6292           if (save17)
6293             fprintf (file, "-%s", reg_names[GP_REG_FIRST + 17]);
6294         }
6295       if (save31)
6296         {
6297           if (savearg > 0 || save16 || save17)
6298             fprintf (file, ",");
6299           fprintf (file, "%s", reg_names[GP_REG_FIRST + 31]);
6300         }
6301       fprintf (file, "\n");
6302     }
6303
6304   if (TARGET_ABICALLS && (mips_abi == ABI_32 || mips_abi == ABI_O64))
6305     {
6306       char *sp_str = reg_names[STACK_POINTER_REGNUM];
6307
6308       fprintf (file, "\t.set\tnoreorder\n\t.cpload\t%s\n\t.set\treorder\n",
6309                reg_names[PIC_FUNCTION_ADDR_REGNUM]);
6310       if (tsize > 0)
6311         {
6312           fprintf (file, "\t%s\t%s,%s,%ld\n",
6313                    (Pmode == DImode ? "dsubu" : "subu"),
6314                    sp_str, sp_str, tsize);
6315           fprintf (file, "\t.cprestore %ld\n", current_frame_info.args_size);
6316         }
6317
6318       if (dwarf2out_do_frame ())
6319         dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, tsize);
6320     }
6321 }
6322 \f
6323 /* Expand the prologue into a bunch of separate insns.  */
6324
6325 void
6326 mips_expand_prologue ()
6327 {
6328   int regno;
6329   HOST_WIDE_INT tsize;
6330   rtx tmp_rtx = 0;
6331   char *arg_name = 0;
6332   tree fndecl = current_function_decl;
6333   tree fntype = TREE_TYPE (fndecl);
6334   tree fnargs = DECL_ARGUMENTS (fndecl);
6335   rtx next_arg_reg;
6336   int i;
6337   tree next_arg;
6338   tree cur_arg;
6339   CUMULATIVE_ARGS args_so_far;
6340   rtx reg_18_save = NULL_RTX;
6341
6342   /* If struct value address is treated as the first argument, make it so.  */
6343   if (aggregate_value_p (DECL_RESULT (fndecl))
6344       && ! current_function_returns_pcc_struct
6345       && struct_value_incoming_rtx == 0)
6346     {
6347       tree type = build_pointer_type (fntype);
6348       tree function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
6349
6350       DECL_ARG_TYPE (function_result_decl) = type;
6351       TREE_CHAIN (function_result_decl) = fnargs;
6352       fnargs = function_result_decl;
6353     }
6354
6355   /* Determine the last argument, and get its name.  */
6356
6357   INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
6358   regno = GP_ARG_FIRST;
6359
6360   for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
6361     {
6362       tree passed_type = DECL_ARG_TYPE (cur_arg);
6363       enum machine_mode passed_mode = TYPE_MODE (passed_type);
6364       rtx entry_parm;
6365
6366       if (TREE_ADDRESSABLE (passed_type))
6367         {
6368           passed_type = build_pointer_type (passed_type);
6369           passed_mode = Pmode;
6370         }
6371
6372       entry_parm = FUNCTION_ARG (args_so_far, passed_mode, passed_type, 1);
6373
6374       if (entry_parm)
6375         {
6376           int words;
6377
6378           /* passed in a register, so will get homed automatically */
6379           if (GET_MODE (entry_parm) == BLKmode)
6380             words = (int_size_in_bytes (passed_type) + 3) / 4;
6381           else
6382             words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
6383
6384           regno = REGNO (entry_parm) + words - 1;
6385         }
6386       else
6387         {
6388           regno = GP_ARG_LAST+1;
6389           break;
6390         }
6391
6392       FUNCTION_ARG_ADVANCE (args_so_far, passed_mode, passed_type, 1);
6393
6394       next_arg = TREE_CHAIN (cur_arg);
6395       if (next_arg == 0)
6396         {
6397           if (DECL_NAME (cur_arg))
6398             arg_name = IDENTIFIER_POINTER (DECL_NAME (cur_arg));
6399
6400           break;
6401         }
6402     }
6403
6404   /* In order to pass small structures by value in registers compatibly with
6405      the MIPS compiler, we need to shift the value into the high part of the
6406      register.  Function_arg has encoded a PARALLEL rtx, holding a vector of
6407      adjustments to be made as the next_arg_reg variable, so we split up the
6408      insns, and emit them separately.  */
6409
6410   next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1);
6411   if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
6412     {
6413       rtvec adjust = XVEC (next_arg_reg, 0);
6414       int num = GET_NUM_ELEM (adjust);
6415
6416       for (i = 0; i < num; i++)
6417         {
6418           rtx pattern = RTVEC_ELT (adjust, i);
6419           if (GET_CODE (pattern) != SET
6420               || GET_CODE (SET_SRC (pattern)) != ASHIFT)
6421             abort_with_insn (pattern, "Insn is not a shift");
6422
6423           PUT_CODE (SET_SRC (pattern), ASHIFTRT);
6424           emit_insn (pattern);
6425         }
6426     }
6427
6428   tsize = compute_frame_size (get_frame_size ());
6429
6430   /* If this function is a varargs function, store any registers that
6431      would normally hold arguments ($4 - $7) on the stack.  */
6432   if ((mips_abi == ABI_32 || mips_abi == ABI_O64)
6433       && (! mips_entry || mips_can_use_return_insn ())
6434       && ((TYPE_ARG_TYPES (fntype) != 0
6435            && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
6436                != void_type_node))
6437           || (arg_name != 0
6438               && ((arg_name[0] == '_'
6439                    && strcmp (arg_name, "__builtin_va_alist") == 0)
6440                   || (arg_name[0] == 'v'
6441                       && strcmp (arg_name, "va_alist") == 0)))))
6442     {
6443       int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
6444       rtx ptr = stack_pointer_rtx;
6445
6446       /* If we are doing svr4-abi, sp has already been decremented by tsize. */
6447       if (TARGET_ABICALLS)
6448         offset += tsize;
6449
6450       for (; regno <= GP_ARG_LAST; regno++)
6451         {
6452           if (offset != 0)
6453             ptr = gen_rtx (PLUS, Pmode, stack_pointer_rtx, GEN_INT (offset));
6454           emit_move_insn (gen_rtx (MEM, gpr_mode, ptr),
6455                           gen_rtx (REG, gpr_mode, regno));
6456
6457           offset += GET_MODE_SIZE (gpr_mode);
6458         }
6459     }
6460
6461   /* If we are using the entry pseudo instruction, it will
6462      automatically subtract 32 from the stack pointer, so we don't
6463      need to.  The entry pseudo instruction is emitted by
6464      function_prologue.  */
6465   if (mips_entry && ! mips_can_use_return_insn ())
6466     {
6467       if (tsize > 0 && tsize <= 32 && frame_pointer_needed)
6468         {
6469           rtx insn;
6470
6471           /* If we are using a frame pointer with a small stack frame,
6472              we need to initialize it here since it won't be done
6473              below.  */
6474           if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
6475             {
6476               rtx incr = GEN_INT (current_function_outgoing_args_size);
6477               if (Pmode == DImode)
6478                 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
6479                                               stack_pointer_rtx,
6480                                               incr));
6481               else
6482                 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
6483                                               stack_pointer_rtx,
6484                                               incr));
6485             }
6486           else if (Pmode == DImode)
6487             insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
6488                                          stack_pointer_rtx));
6489           else
6490             insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
6491                                          stack_pointer_rtx));
6492
6493           RTX_FRAME_RELATED_P (insn) = 1;
6494         }
6495
6496       /* We may need to save $18, if it is used to call a function
6497          which may return a floating point value.  Set up a sequence
6498          of instructions to do so.  Later on we emit them at the right
6499          moment.  */
6500       if (TARGET_MIPS16 && BITSET_P (current_frame_info.mask, 18))
6501         {
6502           rtx reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 3);
6503           long gp_offset, base_offset;
6504
6505           gp_offset = current_frame_info.gp_sp_offset;
6506           if (BITSET_P (current_frame_info.mask, 16))
6507             gp_offset -= UNITS_PER_WORD;
6508           if (BITSET_P (current_frame_info.mask, 17))
6509             gp_offset -= UNITS_PER_WORD;
6510           if (BITSET_P (current_frame_info.mask, 31))
6511             gp_offset -= UNITS_PER_WORD;
6512           if (tsize > 32767)
6513             base_offset = tsize;
6514           else
6515             base_offset = 0;
6516           start_sequence ();
6517           emit_move_insn (reg_rtx,
6518                           gen_rtx (REG, gpr_mode, GP_REG_FIRST + 18));
6519           emit_move_insn (gen_rtx (MEM, gpr_mode,
6520                                    gen_rtx (PLUS, Pmode, stack_pointer_rtx,
6521                                             GEN_INT (gp_offset
6522                                                      - base_offset))),
6523                           reg_rtx);
6524           reg_18_save = gen_sequence ();
6525           end_sequence ();
6526         }
6527
6528       if (tsize > 32)
6529         tsize -= 32;
6530       else
6531         {
6532           tsize = 0;
6533           if (reg_18_save != NULL_RTX)
6534             emit_insn (reg_18_save);
6535         }
6536     }
6537
6538   if (tsize > 0)
6539     {
6540       rtx tsize_rtx = GEN_INT (tsize);
6541
6542       /* If we are doing svr4-abi, sp move is done by
6543          function_prologue.  In mips16 mode with a large frame, we
6544          save the registers before adjusting the stack.  */
6545       if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
6546           && (!TARGET_MIPS16 || tsize <= 32767))
6547         {
6548           rtx insn;
6549
6550           if (tsize > 32767)
6551             {
6552               tmp_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
6553
6554               /* Instruction splitting doesn't preserve the RTX_FRAME_RELATED_P
6555                  bit, so make sure that we don't emit anything that can be
6556                  split.  */
6557               /* ??? There is no DImode ori immediate pattern, so we can only
6558                  do this for 32 bit code.  */
6559               if (large_int (tsize_rtx) && GET_MODE (tmp_rtx) == SImode)
6560                 {
6561                   insn = emit_move_insn (tmp_rtx,
6562                                          GEN_INT (tsize & 0xffff0000));
6563                   RTX_FRAME_RELATED_P (insn) = 1;
6564                   insn = emit_insn (gen_iorsi3 (tmp_rtx, tmp_rtx,
6565                                                 GEN_INT (tsize & 0x0000ffff)));
6566                   RTX_FRAME_RELATED_P (insn) = 1;
6567                 }
6568               else
6569                 {
6570                   insn = emit_move_insn (tmp_rtx, tsize_rtx);
6571                   RTX_FRAME_RELATED_P (insn) = 1;
6572                 }
6573
6574               tsize_rtx = tmp_rtx;
6575             }
6576
6577           if (Pmode == DImode)
6578             insn = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
6579                                           tsize_rtx));
6580           else
6581             insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
6582                                           tsize_rtx));
6583
6584           RTX_FRAME_RELATED_P (insn) = 1;
6585         }
6586
6587       if (! mips_entry)
6588         save_restore_insns (1, tmp_rtx, tsize, (FILE *)0);
6589       else if (reg_18_save != NULL_RTX)
6590         emit_insn (reg_18_save);
6591
6592       if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
6593           && TARGET_MIPS16
6594           && tsize > 32767)
6595         {
6596           rtx reg_rtx;
6597
6598           if (!frame_pointer_needed)
6599             abort ();
6600
6601           reg_rtx = gen_rtx (REG, Pmode, 3);
6602           emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6603           emit_move_insn (reg_rtx, tsize_rtx);
6604           if (Pmode == DImode)
6605             emit_insn (gen_subdi3 (hard_frame_pointer_rtx,
6606                                    hard_frame_pointer_rtx,
6607                                    reg_rtx));
6608           else
6609             emit_insn (gen_subsi3 (hard_frame_pointer_rtx,
6610                                    hard_frame_pointer_rtx,
6611                                    reg_rtx));
6612           emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
6613         }
6614
6615       if (frame_pointer_needed)
6616         {
6617           rtx insn = 0;
6618
6619           /* On the mips16, we encourage the use of unextended
6620              instructions when using the frame pointer by pointing the
6621              frame pointer ahead of the argument space allocated on
6622              the stack.  */
6623           if ((! TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
6624               && TARGET_MIPS16
6625               && tsize > 32767)
6626             {
6627               /* In this case, we have already copied the stack
6628                  pointer into the frame pointer, above.  We need only
6629                  adjust for the outgoing argument size.  */
6630               if (current_function_outgoing_args_size != 0)
6631                 {
6632                   rtx incr = GEN_INT (current_function_outgoing_args_size);
6633                   if (Pmode == DImode)
6634                     insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
6635                                                   hard_frame_pointer_rtx,
6636                                                   incr));
6637                   else
6638                     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
6639                                                   hard_frame_pointer_rtx,
6640                                                   incr));
6641                 }
6642             }
6643           else if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
6644             {
6645               rtx incr = GEN_INT (current_function_outgoing_args_size);
6646               if (Pmode == DImode)
6647                 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
6648                                               stack_pointer_rtx,
6649                                               incr));
6650               else
6651                 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
6652                                               stack_pointer_rtx,
6653                                               incr));
6654             }
6655           else if (Pmode == DImode)
6656             insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
6657                                          stack_pointer_rtx));
6658           else
6659             insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
6660                                          stack_pointer_rtx));
6661
6662           if (insn)
6663             RTX_FRAME_RELATED_P (insn) = 1;
6664         }
6665
6666       if (TARGET_ABICALLS && (mips_abi != ABI_32 && mips_abi != ABI_O64))
6667         emit_insn (gen_loadgp (XEXP (DECL_RTL (current_function_decl), 0),
6668                                gen_rtx (REG, DImode, 25)));
6669     }
6670
6671   /* If we are profiling, make sure no instructions are scheduled before
6672      the call to mcount.  */
6673
6674   if (profile_flag || profile_block_flag)
6675     emit_insn (gen_blockage ());
6676 }
6677 \f
6678 /* Do any necessary cleanup after a function to restore stack, frame,
6679    and regs. */
6680
6681 #define RA_MASK ((long) 0x80000000)     /* 1 << 31 */
6682 #define PIC_OFFSET_TABLE_MASK (1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
6683
6684 void
6685 function_epilogue (file, size)
6686      FILE *file ATTRIBUTE_UNUSED;
6687      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
6688 {
6689   char *fnname;
6690
6691 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6692   /* Get the function name the same way that toplev.c does before calling
6693      assemble_start_function.  This is needed so that the name used here
6694      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
6695   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6696
6697   if (!flag_inhibit_size_directive)
6698     {
6699       fputs ("\t.end\t", file);
6700       assemble_name (file, fnname);
6701       fputs ("\n", file);
6702     }
6703 #endif
6704
6705   if (TARGET_STATS)
6706     {
6707       int num_gp_regs = current_frame_info.gp_reg_size / 4;
6708       int num_fp_regs = current_frame_info.fp_reg_size / 8;
6709       int num_regs = num_gp_regs + num_fp_regs;
6710       char *name = fnname;
6711
6712       if (name[0] == '*')
6713         name++;
6714
6715       dslots_load_total += num_regs;
6716
6717       fprintf (stderr,
6718                "%-20s fp=%c leaf=%c alloca=%c setjmp=%c stack=%4ld arg=%3d reg=%2d/%d delay=%3d/%3dL %3d/%3dJ refs=%3d/%3d/%3d",
6719                name, frame_pointer_needed ? 'y' : 'n',
6720                (current_frame_info.mask & RA_MASK) != 0 ? 'n' : 'y',
6721                current_function_calls_alloca ? 'y' : 'n',
6722                current_function_calls_setjmp ? 'y' : 'n',
6723                current_frame_info.total_size,
6724                current_function_outgoing_args_size, num_gp_regs, num_fp_regs,
6725                dslots_load_total, dslots_load_filled,
6726                dslots_jump_total, dslots_jump_filled,
6727                num_refs[0], num_refs[1], num_refs[2]);
6728
6729       if (HALF_PIC_NUMBER_PTRS > prev_half_pic_ptrs)
6730         {
6731           fprintf (stderr,
6732                    " half-pic=%3d", HALF_PIC_NUMBER_PTRS - prev_half_pic_ptrs);
6733           prev_half_pic_ptrs = HALF_PIC_NUMBER_PTRS;
6734         }
6735
6736       if (HALF_PIC_NUMBER_REFS > prev_half_pic_refs)
6737         {
6738           fprintf (stderr,
6739                    " pic-ref=%3d", HALF_PIC_NUMBER_REFS - prev_half_pic_refs);
6740           prev_half_pic_refs = HALF_PIC_NUMBER_REFS;
6741         }
6742
6743       fputc ('\n', stderr);
6744     }
6745
6746   /* Reset state info for each function.  */
6747   inside_function = 0;
6748   ignore_line_number = 0;
6749   dslots_load_total = 0;
6750   dslots_jump_total = 0;
6751   dslots_load_filled = 0;
6752   dslots_jump_filled = 0;
6753   num_refs[0] = 0;
6754   num_refs[1] = 0;
6755   num_refs[2] = 0;
6756   mips_load_reg = 0;
6757   mips_load_reg2 = 0;
6758   current_frame_info = zero_frame_info;
6759
6760   while (string_constants != NULL)
6761     {
6762       struct string_constant *next;
6763
6764       next = string_constants->next;
6765       free (string_constants);
6766       string_constants = next;
6767     }
6768
6769   /* Restore the output file if optimizing the GP (optimizing the GP causes
6770      the text to be diverted to a tempfile, so that data decls come before
6771      references to the data).  */
6772
6773   if (TARGET_GP_OPT && ! TARGET_MIPS16 && ! TARGET_GAS)
6774     asm_out_file = asm_out_data_file;
6775 }
6776 \f
6777 /* Expand the epilogue into a bunch of separate insns.  */
6778
6779 void
6780 mips_expand_epilogue ()
6781 {
6782   HOST_WIDE_INT tsize = current_frame_info.total_size;
6783   rtx tsize_rtx = GEN_INT (tsize);
6784   rtx tmp_rtx = (rtx)0;
6785
6786   if (mips_can_use_return_insn ())
6787     {
6788       emit_insn (gen_return ());
6789       return;
6790     }
6791
6792   if (mips_entry && ! mips_can_use_return_insn ())
6793     tsize -= 32;
6794
6795   if (tsize > 32767 && ! TARGET_MIPS16)
6796     {
6797       tmp_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
6798       emit_move_insn (tmp_rtx, tsize_rtx);
6799       tsize_rtx = tmp_rtx;
6800     }
6801
6802   if (tsize > 0)
6803     {
6804       long orig_tsize = tsize;
6805
6806       if (frame_pointer_needed)
6807         {
6808           emit_insn (gen_blockage ());
6809
6810           /* On the mips16, the frame pointer is offset from the stack
6811              pointer by current_function_outgoing_args_size.  We
6812              account for that by changing tsize.  Note that this can
6813              actually make tsize negative.  */
6814           if (TARGET_MIPS16)
6815             {
6816               tsize -= current_function_outgoing_args_size;
6817
6818               /* If we have a large frame, it's easier to add to $17
6819                  than to $sp, since the mips16 has no instruction to
6820                  add a register to $sp.  */
6821               if (orig_tsize > 32767)
6822                 {
6823                   rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
6824
6825                   emit_move_insn (g6_rtx, GEN_INT (tsize));
6826                   if (Pmode == DImode)
6827                     emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
6828                                            hard_frame_pointer_rtx,
6829                                            g6_rtx));
6830                   else
6831                     emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
6832                                            hard_frame_pointer_rtx,
6833                                            g6_rtx));
6834                   tsize = 0;
6835                 }
6836               
6837               if (tsize && tsize != orig_tsize)
6838                 tsize_rtx = GEN_INT (tsize);
6839             }
6840
6841           if (Pmode == DImode)
6842             emit_insn (gen_movdi (stack_pointer_rtx, hard_frame_pointer_rtx));
6843           else
6844             emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
6845         }
6846
6847       /* The GP/PIC register is implicitly used by all SYMBOL_REFs, so if we
6848          are going to restore it, then we must emit a blockage insn to
6849          prevent the scheduler from moving the restore out of the epilogue.  */
6850       else if (TARGET_ABICALLS && mips_abi != ABI_32 && mips_abi != ABI_O64
6851                && (current_frame_info.mask
6852                    & (1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))))
6853         emit_insn (gen_blockage ());
6854
6855       save_restore_insns (0, tmp_rtx, orig_tsize, (FILE *)0);
6856
6857       /* In mips16 mode with a large frame, we adjust the stack
6858          pointer before restoring the registers.  In this case, we
6859          should always be using a frame pointer, so everything should
6860          have been handled above.  */
6861       if (tsize > 32767 && TARGET_MIPS16)
6862         abort ();
6863
6864       emit_insn (gen_blockage ());
6865       if (Pmode == DImode && tsize != 0)
6866         emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
6867                                tsize_rtx));
6868       else if (tsize != 0)
6869         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
6870                                tsize_rtx));
6871     }
6872
6873   /* The mips16 loads the return address into $7, not $31.  */
6874   if (TARGET_MIPS16 && (current_frame_info.mask & RA_MASK) != 0)
6875     emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
6876                                                   GP_REG_FIRST + 7)));
6877   else
6878     emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
6879                                                   GP_REG_FIRST + 31)));
6880 }
6881 \f
6882 /* Return nonzero if this function is known to have a null epilogue.
6883    This allows the optimizer to omit jumps to jumps if no stack
6884    was created.  */
6885
6886 int
6887 mips_can_use_return_insn ()
6888 {
6889   if (! reload_completed)
6890     return 0;
6891
6892   if (regs_ever_live[31] || profile_flag)
6893     return 0;
6894
6895   /* In mips16 mode, a function which returns a floating point value
6896      needs to arrange to copy the return value into the floating point
6897      registers.  */
6898   if (TARGET_MIPS16
6899       && mips16_hard_float
6900       && ! aggregate_value_p (DECL_RESULT (current_function_decl))
6901       && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
6902           == MODE_FLOAT)
6903       && (! TARGET_SINGLE_FLOAT
6904           || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
6905               <= 4)))
6906     return 0;
6907
6908   if (current_frame_info.initialized)
6909     return current_frame_info.total_size == 0;
6910
6911   return compute_frame_size (get_frame_size ()) == 0;
6912 }
6913 \f
6914 /* Returns non-zero if X contains a SYMBOL_REF.  */
6915
6916 static int
6917 symbolic_expression_p (x)
6918      rtx x;
6919 {
6920   if (GET_CODE (x) == SYMBOL_REF)
6921     return 1;
6922
6923   if (GET_CODE (x) == CONST)
6924     return symbolic_expression_p (XEXP (x, 0));
6925   
6926   if (GET_RTX_CLASS (GET_CODE (x)) == '1')
6927     return symbolic_expression_p (XEXP (x, 0));
6928
6929   if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
6930       || GET_RTX_CLASS (GET_CODE (x)) == '2')
6931     return (symbolic_expression_p (XEXP (x, 0))
6932             || symbolic_expression_p (XEXP (x, 1)));
6933
6934   return 0;
6935 }
6936
6937 /* Choose the section to use for the constant rtx expression X that has
6938    mode MODE.  */
6939
6940 void
6941 mips_select_rtx_section (mode, x)
6942      enum machine_mode mode;
6943      rtx x ATTRIBUTE_UNUSED;
6944 {
6945   if (TARGET_MIPS16)
6946     {
6947       /* In mips16 mode, the constant table always goes in the same section
6948          as the function, so that constants can be loaded using PC relative
6949          addressing.  */
6950       function_section (current_function_decl);
6951     }
6952   else if (TARGET_EMBEDDED_DATA)
6953     {
6954       /* For embedded applications, always put constants in read-only data,
6955          in order to reduce RAM usage.  */
6956       READONLY_DATA_SECTION ();
6957     }
6958   else
6959     {
6960       /* For hosted applications, always put constants in small data if
6961          possible, as this gives the best performance.  */
6962      
6963       if (GET_MODE_SIZE (mode) <= mips_section_threshold
6964           && mips_section_threshold > 0)
6965         SMALL_DATA_SECTION ();
6966       else if (flag_pic && symbolic_expression_p (x))
6967         /* Any expression involving a SYMBOL_REF might need a run-time
6968            relocation.  (The symbol might be defined in a shared
6969            library loaded at an unexpected base address.)  So, we must
6970            put such expressions in the data segment (which is
6971            writable), rather than the text segment (which is
6972            read-only).  */
6973         data_section ();
6974       else
6975         READONLY_DATA_SECTION ();
6976     }
6977 }
6978
6979 /* Choose the section to use for DECL.  RELOC is true if its value contains
6980    any relocatable expression.
6981
6982    Some of the logic used here needs to be replicated in
6983    ENCODE_SECTION_INFO in mips.h so that references to these symbols
6984    are done correctly.  Specifically, at least all symbols assigned
6985    here to rom (.text and/or .rodata) must not be referenced via
6986    ENCODE_SECTION_INFO with %gprel, as the rom might be too far away.
6987
6988    If you need to make a change here, you probably should check
6989    ENCODE_SECTION_INFO to see if it needs a similar change.  */
6990
6991 void
6992 mips_select_section (decl, reloc)
6993      tree decl;
6994      int reloc;
6995 {
6996   int size = int_size_in_bytes (TREE_TYPE (decl));
6997
6998   if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
6999       && TREE_CODE (decl) == STRING_CST
7000       && !flag_writable_strings)
7001     /* For embedded position independent code, put constant strings in the
7002        text section, because the data section is limited to 64K in size.
7003        For mips16 code, put strings in the text section so that a PC
7004        relative load instruction can be used to get their address.  */
7005     text_section ();
7006   else if (TARGET_EMBEDDED_DATA)
7007     {
7008       /* For embedded applications, always put an object in read-only data
7009          if possible, in order to reduce RAM usage.  */
7010
7011       if (((TREE_CODE (decl) == VAR_DECL
7012             && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7013             && DECL_INITIAL (decl)
7014             && (DECL_INITIAL (decl) == error_mark_node
7015                 || TREE_CONSTANT (DECL_INITIAL (decl))))
7016            /* Deal with calls from output_constant_def_contents.  */
7017            || (TREE_CODE (decl) != VAR_DECL
7018                && (TREE_CODE (decl) != STRING_CST
7019                    || !flag_writable_strings)))
7020           && ! (flag_pic && reloc))
7021         READONLY_DATA_SECTION ();
7022       else if (size > 0 && size <= mips_section_threshold)
7023         SMALL_DATA_SECTION ();
7024       else
7025         data_section ();
7026     }
7027   else
7028     {
7029       /* For hosted applications, always put an object in small data if
7030          possible, as this gives the best performance.  */
7031
7032       if (size > 0 && size <= mips_section_threshold)
7033         SMALL_DATA_SECTION ();
7034       else if (((TREE_CODE (decl) == VAR_DECL
7035                  && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7036                  && DECL_INITIAL (decl)
7037                  && (DECL_INITIAL (decl) == error_mark_node
7038                      || TREE_CONSTANT (DECL_INITIAL (decl))))
7039                 /* Deal with calls from output_constant_def_contents.  */
7040                 || (TREE_CODE (decl) != VAR_DECL
7041                     && (TREE_CODE (decl) != STRING_CST
7042                         || !flag_writable_strings)))
7043                && ! (flag_pic && reloc))
7044         READONLY_DATA_SECTION ();
7045       else
7046         data_section ();
7047     }
7048 }
7049 \f
7050 #ifdef MIPS_ABI_DEFAULT
7051
7052 /* Support functions for the 64 bit ABI.  */
7053
7054 /* Return register to use for a function return value with VALTYPE for function
7055    FUNC.  */
7056
7057 rtx
7058 mips_function_value (valtype, func)
7059      tree valtype;
7060      tree func ATTRIBUTE_UNUSED;
7061 {
7062   int reg = GP_RETURN;
7063   enum machine_mode mode = TYPE_MODE (valtype);
7064   enum mode_class mclass = GET_MODE_CLASS (mode);
7065   int unsignedp = TREE_UNSIGNED (valtype);
7066
7067   /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
7068      just as PROMOTE_MODE does.  */
7069   mode = promote_mode (valtype, mode, &unsignedp, 1);
7070
7071   /* ??? How should we return complex float?  */
7072   if (mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
7073     {
7074       if (TARGET_SINGLE_FLOAT
7075           && (mclass == MODE_FLOAT
7076               ? GET_MODE_SIZE (mode) > 4 : GET_MODE_SIZE (mode) / 2 > 4))
7077         reg = GP_RETURN;
7078       else
7079         reg = FP_RETURN;
7080     }
7081
7082   else if (TREE_CODE (valtype) == RECORD_TYPE
7083            && mips_abi != ABI_32 
7084            && mips_abi != ABI_O64 
7085            && mips_abi != ABI_EABI)
7086     {
7087       /* A struct with only one or two floating point fields is returned in
7088          the floating point registers.  */
7089       tree field, fields[2];
7090       int i;
7091
7092       for (i = 0, field = TYPE_FIELDS (valtype); field;
7093            field = TREE_CHAIN (field))
7094         {
7095           if (TREE_CODE (field) != FIELD_DECL)
7096             continue;
7097
7098           if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE || i >= 2)
7099             break;
7100
7101           fields[i++] = field;
7102         }
7103           
7104       /* Must check i, so that we reject structures with no elements.  */
7105       if (! field)
7106         {
7107           if (i == 1)
7108             {
7109               /* The structure has DImode, but we don't allow DImode values
7110                  in FP registers, so we use a PARALLEL even though it isn't
7111                  strictly necessary.  */
7112               enum machine_mode field_mode = TYPE_MODE (TREE_TYPE (fields[0]));
7113
7114               return gen_rtx (PARALLEL, mode,
7115                               gen_rtvec (1,
7116                                          gen_rtx (EXPR_LIST, VOIDmode,
7117                                                   gen_rtx (REG, field_mode,
7118                                                            FP_RETURN),
7119                                                   const0_rtx)));
7120             }
7121
7122           else if (i == 2)
7123             {
7124               enum machine_mode first_mode
7125                 = TYPE_MODE (TREE_TYPE (fields[0]));
7126               enum machine_mode second_mode
7127                 = TYPE_MODE (TREE_TYPE (fields[1]));
7128               int first_offset
7129                 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (fields[0]));
7130               int second_offset
7131                 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (fields[1]));
7132
7133               return gen_rtx (PARALLEL, mode,
7134                               gen_rtvec (2,
7135                                          gen_rtx (EXPR_LIST, VOIDmode,
7136                                                   gen_rtx (REG, first_mode,
7137                                                            FP_RETURN),
7138                                                   GEN_INT (first_offset
7139                                                            / BITS_PER_UNIT)),
7140                                          gen_rtx (EXPR_LIST, VOIDmode,
7141                                                   gen_rtx (REG, second_mode,
7142                                                            FP_RETURN + 2),
7143                                                   GEN_INT (second_offset
7144                                                            / BITS_PER_UNIT))));
7145             }
7146         }
7147     }
7148
7149   return gen_rtx (REG, mode, reg);
7150 }
7151
7152 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE.  Return
7153    nonzero when an argument must be passed by reference.  */
7154
7155 int
7156 function_arg_pass_by_reference (cum, mode, type, named)
7157      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
7158      enum machine_mode mode;
7159      tree type;
7160      int named ATTRIBUTE_UNUSED;
7161 {
7162   int size;
7163
7164   if (mips_abi != ABI_EABI)
7165     return 0;
7166
7167   /* ??? How should SCmode be handled?  */
7168   if (type == NULL_TREE || mode == DImode || mode == DFmode)
7169     return 0;
7170
7171   size = int_size_in_bytes (type);
7172   return size == -1 || size > UNITS_PER_WORD;
7173 }
7174 #endif
7175
7176 /* This function returns the register class required for a secondary
7177    register when copying between one of the registers in CLASS, and X,
7178    using MODE.  If IN_P is nonzero, the copy is going from X to the
7179    register, otherwise the register is the source.  A return value of
7180    NO_REGS means that no secondary register is required.  */
7181
7182 enum reg_class
7183 mips_secondary_reload_class (class, mode, x, in_p)
7184      enum reg_class class;
7185      enum machine_mode mode;
7186      rtx x;
7187      int in_p;
7188 {
7189   enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
7190   int regno = -1;
7191   int gp_reg_p;
7192
7193   if (GET_CODE (x) == SIGN_EXTEND)
7194     {
7195       int off = 0;
7196
7197       x = XEXP (x, 0);
7198
7199       /* We may be called with reg_renumber NULL from regclass.
7200          ??? This is probably a bug.  */
7201       if (reg_renumber)
7202         regno = true_regnum (x);
7203       else
7204         {
7205           while (GET_CODE (x) == SUBREG)
7206             {
7207               off += SUBREG_WORD (x);
7208               x = SUBREG_REG (x);
7209             }
7210
7211           if (GET_CODE (x) == REG)
7212             regno = REGNO (x) + off;
7213         }
7214     }
7215
7216   else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
7217     regno = true_regnum (x);
7218
7219   gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
7220
7221   /* We always require a general register when copying anything to
7222      HILO_REGNUM, except when copying an SImode value from HILO_REGNUM
7223      to a general register, or when copying from register 0.  */
7224   if (class == HILO_REG && regno != GP_REG_FIRST + 0)
7225     return ((! in_p
7226              && gp_reg_p
7227              && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
7228             ? NO_REGS : gr_regs);
7229   else if (regno == HILO_REGNUM)
7230     return ((in_p
7231              && class == gr_regs
7232              && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
7233             ? NO_REGS : gr_regs);
7234
7235   /* Copying from HI or LO to anywhere other than a general register
7236      requires a general register.  */
7237   if (class == HI_REG || class == LO_REG || class == MD_REGS)
7238     {
7239       if (TARGET_MIPS16 && in_p)
7240         {
7241           /* We can't really copy to HI or LO at all in mips16 mode.  */
7242           return M16_REGS;
7243         }
7244       return gp_reg_p ? NO_REGS : gr_regs;
7245     }
7246   if (MD_REG_P (regno))
7247     {
7248       if (TARGET_MIPS16 && ! in_p)
7249         {
7250           /* We can't really copy to HI or LO at all in mips16 mode.  */
7251           return M16_REGS;
7252         }
7253       return class == gr_regs ? NO_REGS : gr_regs;
7254     }
7255
7256   /* We can only copy a value to a condition code register from a
7257      floating point register, and even then we require a scratch
7258      floating point register.  We can only copy a value out of a
7259      condition code register into a general register.  */
7260   if (class == ST_REGS)
7261     {
7262       if (in_p)
7263         return FP_REGS;
7264       return GP_REG_P (regno) ? NO_REGS : GR_REGS;
7265     }
7266   if (ST_REG_P (regno))
7267     {
7268       if (! in_p)
7269         return FP_REGS;
7270       return class == GR_REGS ? NO_REGS : GR_REGS;
7271     }
7272
7273   /* In mips16 mode, going between memory and anything but M16_REGS
7274      requires an M16_REG.  */
7275   if (TARGET_MIPS16)
7276     {
7277       if (class != M16_REGS && class != M16_NA_REGS)
7278         {
7279           if (gp_reg_p)
7280             return NO_REGS;
7281           return M16_REGS;
7282         }
7283       if (! gp_reg_p)
7284         {
7285           /* The stack pointer isn't a valid operand to an add instruction,
7286              so we need to load it into M16_REGS first.  This can happen as
7287              a result of register elimination and form_sum converting
7288              (plus reg (plus SP CONST)) to (plus (plus reg SP) CONST).  We
7289              need an extra register if the dest is the same as the other
7290              register.  In that case, we can't fix the problem by loading SP
7291              into the dest first.  */
7292           if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
7293               && GET_CODE (XEXP (x, 1)) == REG
7294               && (XEXP (x, 0) == stack_pointer_rtx
7295                   || XEXP (x, 1) == stack_pointer_rtx))
7296             return (class == M16_REGS ? M16_NA_REGS : M16_REGS);
7297
7298           if (class == M16_REGS || class == M16_NA_REGS)
7299             return NO_REGS;
7300           return M16_REGS;
7301         }
7302     }
7303
7304   return NO_REGS;
7305 }
7306 \f
7307 /* For each mips16 function which refers to GP relative symbols, we
7308    use a pseudo register, initialized at the start of the function, to
7309    hold the $gp value.  */
7310
7311 rtx
7312 mips16_gp_pseudo_reg ()
7313 {
7314   if (mips16_gp_pseudo_rtx == NULL_RTX)
7315     {
7316       rtx const_gp;
7317       rtx insn, scan;
7318
7319       mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
7320       RTX_UNCHANGING_P (mips16_gp_pseudo_rtx) = 1;
7321
7322       /* We want to initialize this to a value which gcc will believe
7323          is constant.  */
7324       const_gp = gen_rtx (CONST, Pmode,
7325                           gen_rtx (REG, Pmode, GP_REG_FIRST + 28));
7326
7327       start_sequence ();
7328       emit_move_insn (mips16_gp_pseudo_rtx, const_gp);
7329       insn = gen_sequence ();
7330       end_sequence ();
7331
7332       push_topmost_sequence ();
7333       /* We need to emit the initialization after the FUNCTION_BEG
7334          note, so that it will be integrated.  */
7335       for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
7336         if (GET_CODE (scan) == NOTE
7337             && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
7338           break;
7339       if (scan == NULL_RTX)
7340         scan = get_insns ();
7341       insn = emit_insn_after (insn, scan);
7342       pop_topmost_sequence ();
7343     }
7344
7345   return mips16_gp_pseudo_rtx;
7346 }
7347
7348 /* Return an RTX which represents the signed 16 bit offset from the
7349    $gp register for the given symbol.  This is only used on the
7350    mips16.  */
7351
7352 rtx
7353 mips16_gp_offset (sym)
7354      rtx sym;
7355 {
7356   tree gp;
7357
7358   if (GET_CODE (sym) != SYMBOL_REF
7359       || ! SYMBOL_REF_FLAG (sym))
7360     abort ();
7361
7362   /* We use a special identifier to represent the value of the gp
7363      register.  */
7364   gp = get_identifier ("__mips16_gp_value");
7365
7366   return gen_rtx (CONST, Pmode,
7367                   gen_rtx (MINUS, Pmode, sym,
7368                            gen_rtx (SYMBOL_REF, Pmode,
7369                                     IDENTIFIER_POINTER (gp))));
7370 }
7371
7372 /* Return nonzero if the given RTX represents a signed 16 bit offset
7373    from the $gp register.  */
7374
7375 int
7376 mips16_gp_offset_p (x)
7377      rtx x;
7378 {
7379   if (GET_CODE (x) == CONST)
7380     x = XEXP (x, 0);
7381
7382   /* It's OK to add a small integer value to a gp offset.  */
7383   if (GET_CODE (x) == PLUS)
7384     {
7385       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7386           && SMALL_INT (XEXP (x, 1)))
7387         return mips16_gp_offset_p (XEXP (x, 0));
7388       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7389           && SMALL_INT (XEXP (x, 0)))
7390         return mips16_gp_offset_p (XEXP (x, 1));
7391       return 0;
7392     }
7393
7394   /* Make sure it is in the form SYM - __mips16_gp_value.  */
7395   return (GET_CODE (x) == MINUS
7396           && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
7397           && SYMBOL_REF_FLAG (XEXP (x, 0))
7398           && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
7399           && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0);
7400 }
7401
7402 /* Output a GP offset.  We don't want to print the subtraction of
7403    __mips16_gp_value; it is implicitly represented by the %gprel which
7404    should have been printed by the caller.  */
7405
7406 static void
7407 mips16_output_gp_offset (file, x)
7408      FILE *file;
7409      rtx x;
7410 {
7411   if (GET_CODE (x) == CONST)
7412     x = XEXP (x, 0);
7413
7414   if (GET_CODE (x) == PLUS)
7415     {
7416       mips16_output_gp_offset (file, XEXP (x, 0));
7417       fputs ("+", file);
7418       mips16_output_gp_offset (file, XEXP (x, 1));
7419       return;
7420     }
7421
7422   if (GET_CODE (x) == MINUS
7423       && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
7424       && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0)
7425     {
7426       mips16_output_gp_offset (file, XEXP (x, 0));
7427       return;
7428     }
7429
7430   output_addr_const (file, x);
7431 }
7432
7433 /* Return nonzero if a constant should not be output until after the
7434    function.  This is true of most string constants, so that we can
7435    use a more efficient PC relative reference.  However, a static
7436    inline function may never call assemble_function_end to write out
7437    the constant pool, so don't try to postpone the constant in that
7438    case.
7439
7440    ??? It's really a bug that a static inline function can put stuff
7441    in the constant pool even if the function itself is not output.
7442
7443    We record which string constants we've seen, so that we know which
7444    ones might use the more efficient reference.  */
7445
7446 int
7447 mips16_constant_after_function_p (x)
7448      tree x;
7449 {
7450   if (TREE_CODE (x) == STRING_CST
7451       && ! flag_writable_strings
7452       && current_function_decl != 0
7453       && ! DECL_DEFER_OUTPUT (current_function_decl)
7454       && ! (DECL_INLINE (current_function_decl)
7455             && ((! TREE_PUBLIC (current_function_decl)
7456                  && ! TREE_ADDRESSABLE (current_function_decl)
7457                  && ! flag_keep_inline_functions)
7458                 || DECL_EXTERNAL (current_function_decl))))
7459     {
7460       struct string_constant *n;
7461
7462       n = (struct string_constant *) xmalloc (sizeof *n);
7463       n->label = XSTR (XEXP (TREE_CST_RTL (x), 0), 0);
7464       n->next = string_constants;
7465       string_constants = n;
7466
7467       return 1;
7468     }
7469
7470   return 0;
7471 }
7472
7473 /* Validate a constant for the mips16.  This rejects general symbolic
7474    addresses, which must be loaded from memory.  If ADDR is nonzero,
7475    this should reject anything which is not a legal address.  If
7476    ADDEND is nonzero, this is being added to something else.  */
7477
7478 int
7479 mips16_constant (x, mode, addr, addend)
7480      rtx x;
7481      enum machine_mode mode;
7482      int addr;
7483      int addend;
7484 {
7485   while (GET_CODE (x) == CONST)
7486     x = XEXP (x, 0);
7487
7488   switch (GET_CODE (x))
7489     {
7490     default:
7491       return 0;
7492
7493     case PLUS:
7494       return (mips16_constant (XEXP (x, 0), mode, addr, 1)
7495               && mips16_constant (XEXP (x, 1), mode, addr, 1));
7496
7497     case SYMBOL_REF:
7498       if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
7499         return 0;
7500       if (CONSTANT_POOL_ADDRESS_P (x))
7501         return 1;
7502
7503       /* If we aren't looking for a memory address, we can accept a GP
7504          relative symbol, which will have SYMBOL_REF_FLAG set; movsi
7505          knows how to handle this.  We can always accept a string
7506          constant, which is the other case in which SYMBOL_REF_FLAG
7507          will be set.  */
7508       if (! addr && ! addend && SYMBOL_REF_FLAG (x) && mode == Pmode)
7509         return 1;
7510
7511       /* We can accept a string constant, which will have
7512          SYMBOL_REF_FLAG set but must be recognized by name to
7513          distinguish from a GP accessible symbol.  The name of a
7514          string constant will have been generated by
7515          ASM_GENERATE_INTERNAL_LABEL as called by output_constant_def.  */
7516       if (SYMBOL_REF_FLAG (x))
7517         {
7518           char *name = XSTR (x, 0);
7519
7520           return (name[0] == '*'
7521                   && strncmp (name + 1, LOCAL_LABEL_PREFIX,
7522                               sizeof LOCAL_LABEL_PREFIX - 1) == 0);
7523         }
7524
7525       return 0;
7526
7527     case LABEL_REF:
7528       if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
7529         return 0;
7530       return 1;
7531
7532     case CONST_INT:
7533       if (addr && ! addend)
7534         return 0;
7535       return INTVAL (x) > - 0x10000 && INTVAL (x) <= 0xffff;
7536
7537     case REG:
7538       /* We need to treat $gp as a legitimate constant, because
7539          mips16_gp_pseudo_reg assumes that.  */
7540       return REGNO (x) == GP_REG_FIRST + 28;
7541     }
7542 }
7543
7544 /* Write out code to move floating point arguments in or out of
7545    general registers.  Output the instructions to FILE.  FP_CODE is
7546    the code describing which arguments are present (see the comment at
7547    the definition of CUMULATIVE_ARGS in mips.h).  FROM_FP_P is non-zero if
7548    we are copying from the floating point registers.  */
7549
7550 static void
7551 mips16_fp_args (file, fp_code, from_fp_p)
7552      FILE *file;
7553      int fp_code;
7554      int from_fp_p;
7555 {
7556   const char *s;
7557   int gparg, fparg;
7558   unsigned int f;
7559
7560   /* This code only works for the original 32 bit ABI and the O64 ABI.  */
7561   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
7562     abort ();
7563
7564   if (from_fp_p)
7565     s = "mfc1";
7566   else
7567     s = "mtc1";
7568   gparg = GP_ARG_FIRST;
7569   fparg = FP_ARG_FIRST;
7570   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7571     {
7572       if ((f & 3) == 1)
7573         {
7574           if ((fparg & 1) != 0)
7575             ++fparg;
7576           fprintf (file, "\t%s\t%s,%s\n", s,
7577                    reg_names[gparg], reg_names[fparg]);
7578         }
7579       else if ((f & 3) == 2)
7580         {
7581           if (TARGET_64BIT)
7582             fprintf (file, "\td%s\t%s,%s\n", s,
7583                      reg_names[gparg], reg_names[fparg]);
7584           else
7585             {
7586               if ((fparg & 1) != 0)
7587                 ++fparg;
7588               if (TARGET_BIG_ENDIAN)
7589                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7590                          reg_names[gparg], reg_names[fparg + 1], s,
7591                          reg_names[gparg + 1], reg_names[fparg]);
7592               else
7593                 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7594                          reg_names[gparg], reg_names[fparg], s,
7595                          reg_names[gparg + 1], reg_names[fparg + 1]);
7596               ++gparg;
7597               ++fparg;
7598             }
7599         }
7600       else
7601         abort ();
7602
7603       ++gparg;
7604       ++fparg;
7605     }
7606 }
7607
7608 /* Build a mips16 function stub.  This is used for functions which
7609    take aruments in the floating point registers.  It is 32 bit code
7610    that moves the floating point args into the general registers, and
7611    then jumps to the 16 bit code.  */
7612
7613 static void
7614 build_mips16_function_stub (file)
7615      FILE *file;
7616 {
7617   char *fnname;
7618   char *secname, *stubname;
7619   tree stubid, stubdecl;
7620   int need_comma;
7621   unsigned int f;
7622
7623   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7624   secname = (char *) alloca (strlen (fnname) + 20);
7625   sprintf (secname, ".mips16.fn.%s", fnname);
7626   stubname = (char *) alloca (strlen (fnname) + 20);
7627   sprintf (stubname, "__fn_stub_%s", fnname);
7628   stubid = get_identifier (stubname);
7629   stubdecl = build_decl (FUNCTION_DECL, stubid,
7630                          build_function_type (void_type_node, NULL_TREE));
7631   DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
7632
7633   fprintf (file, "\t# Stub function for %s (", current_function_name);
7634   need_comma = 0;
7635   for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
7636     {
7637       fprintf (file, "%s%s",
7638                need_comma ? ", " : "",
7639                (f & 3) == 1 ? "float" : "double");
7640       need_comma = 1;
7641     }
7642   fprintf (file, ")\n");
7643
7644   fprintf (file, "\t.set\tnomips16\n");
7645   function_section (stubdecl);
7646   ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
7647
7648   /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
7649      within a .ent, and we can not emit another .ent.  */
7650 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7651   fputs ("\t.ent\t", file);
7652   assemble_name (file, stubname);
7653   fputs ("\n", file);
7654 #endif
7655
7656   assemble_name (file, stubname);
7657   fputs (":\n", file);
7658
7659   /* We don't want the assembler to insert any nops here.  */
7660   fprintf (file, "\t.set\tnoreorder\n");
7661
7662   mips16_fp_args (file, current_function_args_info.fp_code, 1);
7663
7664   fprintf (asm_out_file, "\t.set\tnoat\n");
7665   fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
7666   assemble_name (file, fnname);
7667   fprintf (file, "\n");
7668   fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
7669   fprintf (asm_out_file, "\t.set\tat\n");
7670
7671   /* Unfortunately, we can't fill the jump delay slot.  We can't fill
7672      with one of the mfc1 instructions, because the result is not
7673      available for one instruction, so if the very first instruction
7674      in the function refers to the register, it will see the wrong
7675      value.  */
7676   fprintf (file, "\tnop\n");
7677
7678   fprintf (file, "\t.set\treorder\n");
7679
7680 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7681   fputs ("\t.end\t", file);
7682   assemble_name (file, stubname);
7683   fputs ("\n", file);
7684 #endif
7685
7686   fprintf (file, "\t.set\tmips16\n");
7687
7688   function_section (current_function_decl);
7689 }
7690
7691 /* We keep a list of functions for which we have already built stubs
7692    in build_mips16_call_stub.  */
7693
7694 struct mips16_stub
7695 {
7696   struct mips16_stub *next;
7697   char *name;
7698   int fpret;
7699 };
7700
7701 static struct mips16_stub *mips16_stubs;
7702
7703 /* Build a call stub for a mips16 call.  A stub is needed if we are
7704    passing any floating point values which should go into the floating
7705    point registers.  If we are, and the call turns out to be to a 32
7706    bit function, the stub will be used to move the values into the
7707    floating point registers before calling the 32 bit function.  The
7708    linker will magically adjust the function call to either the 16 bit
7709    function or the 32 bit stub, depending upon where the function call
7710    is actually defined.
7711
7712    Similarly, we need a stub if the return value might come back in a
7713    floating point register.
7714
7715    RETVAL, FNMEM, and ARG_SIZE are the values passed to the call insn
7716    (RETVAL is NULL if this is call rather than call_value).  FP_CODE
7717    is the code built by function_arg.  This function returns a nonzero
7718    value if it builds the call instruction itself.  */
7719
7720 int
7721 build_mips16_call_stub (retval, fnmem, arg_size, fp_code)
7722      rtx retval;
7723      rtx fnmem;
7724      rtx arg_size;
7725      int fp_code;
7726 {
7727   int fpret;
7728   rtx fn;
7729   char *fnname, *secname, *stubname;
7730   struct mips16_stub *l;
7731   tree stubid, stubdecl;
7732   int need_comma;
7733   unsigned int f;
7734
7735   /* We don't need to do anything if we aren't in mips16 mode, or if
7736      we were invoked with the -msoft-float option.  */
7737   if (! TARGET_MIPS16 || ! mips16_hard_float)
7738     return 0;
7739
7740   /* Figure out whether the value might come back in a floating point
7741      register.  */
7742   fpret = (retval != 0
7743            && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
7744            && (! TARGET_SINGLE_FLOAT
7745                || GET_MODE_SIZE (GET_MODE (retval)) <= 4));
7746
7747   /* We don't need to do anything if there were no floating point
7748      arguments and the value will not be returned in a floating point
7749      register.  */
7750   if (fp_code == 0 && ! fpret)
7751     return 0;
7752
7753   if (GET_CODE (fnmem) != MEM)
7754     abort ();
7755   fn = XEXP (fnmem, 0);
7756
7757   /* We don't need to do anything if this is a call to a special
7758      mips16 support function.  */
7759   if (GET_CODE (fn) == SYMBOL_REF
7760       && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
7761     return 0;
7762
7763   /* This code will only work for o32 and o64 abis.  The other ABI's 
7764      require more sophisticated support.  */
7765   if (mips_abi != ABI_32 && mips_abi != ABI_O64)
7766     abort ();
7767
7768   /* We can only handle SFmode and DFmode floating point return
7769      values.  */
7770   if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
7771     abort ();
7772
7773   /* If we're calling via a function pointer, then we must always call
7774      via a stub.  There are magic stubs provided in libgcc.a for each
7775      of the required cases.  Each of them expects the function address
7776      to arrive in register $2.  */
7777
7778   if (GET_CODE (fn) != SYMBOL_REF)
7779     {
7780       char buf[30];
7781       tree id;
7782       rtx stub_fn, stub_mem, insn;
7783
7784       /* ??? If this code is modified to support other ABI's, we need
7785          to handle PARALLEL return values here.  */
7786
7787       sprintf (buf, "__mips16_call_stub_%s%d",
7788                (fpret
7789                 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
7790                 : ""),
7791                fp_code);
7792       id = get_identifier (buf);
7793       stub_fn = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (id));
7794       stub_mem = gen_rtx (MEM, Pmode, stub_fn);
7795
7796       emit_move_insn (gen_rtx (REG, Pmode, 2), fn);
7797
7798       if (retval == NULL_RTX)
7799         insn = gen_call_internal0 (stub_mem, arg_size,
7800                                    gen_rtx (REG, SImode,
7801                                             GP_REG_FIRST + 31));
7802       else
7803         insn = gen_call_value_internal0 (retval, stub_mem, arg_size,
7804                                          gen_rtx (REG, SImode,
7805                                                   GP_REG_FIRST + 31));
7806       insn = emit_call_insn (insn);
7807
7808       /* Put the register usage information on the CALL.  */
7809       if (GET_CODE (insn) != CALL_INSN)
7810         abort ();
7811       CALL_INSN_FUNCTION_USAGE (insn) =
7812         gen_rtx (EXPR_LIST, VOIDmode,
7813                  gen_rtx (USE, VOIDmode, gen_rtx (REG, Pmode, 2)),
7814                  CALL_INSN_FUNCTION_USAGE (insn));
7815
7816       /* If we are handling a floating point return value, we need to
7817          save $18 in the function prologue.  Putting a note on the
7818          call will mean that regs_ever_live[$18] will be true if the
7819          call is not eliminated, and we can check that in the prologue
7820          code.  */
7821       if (fpret)
7822         CALL_INSN_FUNCTION_USAGE (insn) =
7823           gen_rtx (EXPR_LIST, VOIDmode,
7824                    gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
7825                    CALL_INSN_FUNCTION_USAGE (insn));
7826
7827       /* Return 1 to tell the caller that we've generated the call
7828          insn.  */
7829       return 1;
7830     }
7831
7832   /* We know the function we are going to call.  If we have already
7833      built a stub, we don't need to do anything further.  */
7834
7835   fnname = XSTR (fn, 0);
7836   for (l = mips16_stubs; l != NULL; l = l->next)
7837     if (strcmp (l->name, fnname) == 0)
7838       break;
7839
7840   if (l == NULL)
7841     {
7842       /* Build a special purpose stub.  When the linker sees a
7843          function call in mips16 code, it will check where the target
7844          is defined.  If the target is a 32 bit call, the linker will
7845          search for the section defined here.  It can tell which
7846          symbol this section is associated with by looking at the
7847          relocation information (the name is unreliable, since this
7848          might be a static function).  If such a section is found, the
7849          linker will redirect the call to the start of the magic
7850          section.
7851
7852          If the function does not return a floating point value, the
7853          special stub section is named
7854              .mips16.call.FNNAME
7855
7856          If the function does return a floating point value, the stub
7857          section is named
7858              .mips16.call.fp.FNNAME
7859          */
7860
7861       secname = (char *) alloca (strlen (fnname) + 40);
7862       sprintf (secname, ".mips16.call.%s%s",
7863                fpret ? "fp." : "",
7864                fnname);
7865       stubname = (char *) alloca (strlen (fnname) + 20);
7866       sprintf (stubname, "__call_stub_%s%s",
7867                fpret ? "fp_" : "",
7868                fnname);
7869       stubid = get_identifier (stubname);
7870       stubdecl = build_decl (FUNCTION_DECL, stubid,
7871                              build_function_type (void_type_node, NULL_TREE));
7872       DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
7873
7874       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
7875                (fpret
7876                 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
7877                 : ""),
7878                fnname);
7879       need_comma = 0;
7880       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7881         {
7882           fprintf (asm_out_file, "%s%s",
7883                    need_comma ? ", " : "",
7884                    (f & 3) == 1 ? "float" : "double");
7885           need_comma = 1;
7886         }
7887       fprintf (asm_out_file, ")\n");
7888
7889       fprintf (asm_out_file, "\t.set\tnomips16\n");
7890       assemble_start_function (stubdecl, stubname);
7891
7892 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7893       fputs ("\t.ent\t", asm_out_file);
7894       assemble_name (asm_out_file, stubname);
7895       fputs ("\n", asm_out_file);
7896
7897       assemble_name (asm_out_file, stubname);
7898       fputs (":\n", asm_out_file);
7899 #endif
7900
7901       /* We build the stub code by hand.  That's the only way we can
7902          do it, since we can't generate 32 bit code during a 16 bit
7903          compilation. */
7904
7905       /* We don't want the assembler to insert any nops here.  */
7906       fprintf (asm_out_file, "\t.set\tnoreorder\n");
7907
7908       mips16_fp_args (asm_out_file, fp_code, 0);
7909
7910       if (! fpret)
7911         {
7912           fprintf (asm_out_file, "\t.set\tnoat\n");
7913           fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
7914                    fnname);
7915           fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
7916           fprintf (asm_out_file, "\t.set\tat\n");
7917           /* Unfortunately, we can't fill the jump delay slot.  We
7918              can't fill with one of the mtc1 instructions, because the
7919              result is not available for one instruction, so if the
7920              very first instruction in the function refers to the
7921              register, it will see the wrong value.  */
7922           fprintf (asm_out_file, "\tnop\n");
7923         }
7924       else
7925         {
7926           fprintf (asm_out_file, "\tmove\t%s,%s\n",
7927                    reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
7928           fprintf (asm_out_file, "\tjal\t%s\n", fnname);
7929           /* As above, we can't fill the delay slot.  */
7930           fprintf (asm_out_file, "\tnop\n");
7931           if (GET_MODE (retval) == SFmode)
7932             fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
7933                      reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
7934           else
7935             {
7936               if (TARGET_BIG_ENDIAN)
7937                 {
7938                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
7939                            reg_names[GP_REG_FIRST + 2],
7940                            reg_names[FP_REG_FIRST + 1]);
7941                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
7942                            reg_names[GP_REG_FIRST + 3],
7943                            reg_names[FP_REG_FIRST + 0]);
7944                 }
7945               else
7946                 {
7947                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
7948                            reg_names[GP_REG_FIRST + 2],
7949                            reg_names[FP_REG_FIRST + 0]);
7950                   fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
7951                            reg_names[GP_REG_FIRST + 3],
7952                            reg_names[FP_REG_FIRST + 1]);
7953                 }
7954             }
7955           fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
7956           /* As above, we can't fill the delay slot.  */
7957           fprintf (asm_out_file, "\tnop\n");
7958         }
7959
7960       fprintf (asm_out_file, "\t.set\treorder\n");
7961
7962 #ifdef ASM_DECLARE_FUNCTION_SIZE
7963       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
7964 #endif
7965
7966 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7967       fputs ("\t.end\t", asm_out_file);
7968       assemble_name (asm_out_file, stubname);
7969       fputs ("\n", asm_out_file);
7970 #endif
7971
7972       fprintf (asm_out_file, "\t.set\tmips16\n");
7973
7974       /* Record this stub.  */
7975       l = (struct mips16_stub *) xmalloc (sizeof *l);
7976       l->name = xstrdup (fnname);
7977       l->fpret = fpret;
7978       l->next = mips16_stubs;
7979       mips16_stubs = l;
7980     }
7981
7982   /* If we expect a floating point return value, but we've built a
7983      stub which does not expect one, then we're in trouble.  We can't
7984      use the existing stub, because it won't handle the floating point
7985      value.  We can't build a new stub, because the linker won't know
7986      which stub to use for the various calls in this object file.
7987      Fortunately, this case is illegal, since it means that a function
7988      was declared in two different ways in a single compilation.  */
7989   if (fpret && ! l->fpret)
7990     error ("can not handle inconsistent calls to `%s'", fnname);
7991
7992   /* If we are calling a stub which handles a floating point return
7993      value, we need to arrange to save $18 in the prologue.  We do
7994      this by marking the function call as using the register.  The
7995      prologue will later see that it is used, and emit code to save
7996      it.  */
7997
7998   if (l->fpret)
7999     {
8000       rtx insn;
8001
8002       if (retval == NULL_RTX)
8003         insn = gen_call_internal0 (fnmem, arg_size,
8004                                    gen_rtx (REG, SImode,
8005                                             GP_REG_FIRST + 31));
8006       else
8007         insn = gen_call_value_internal0 (retval, fnmem, arg_size,
8008                                          gen_rtx (REG, SImode,
8009                                                   GP_REG_FIRST + 31));
8010       insn = emit_call_insn (insn);
8011
8012       if (GET_CODE (insn) != CALL_INSN)
8013         abort ();
8014
8015       CALL_INSN_FUNCTION_USAGE (insn) =
8016         gen_rtx (EXPR_LIST, VOIDmode,
8017                  gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
8018                  CALL_INSN_FUNCTION_USAGE (insn));
8019
8020       /* Return 1 to tell the caller that we've generated the call
8021          insn.  */
8022       return 1;
8023     }
8024
8025   /* Return 0 to let the caller generate the call insn.  */
8026   return 0;
8027 }
8028
8029 /* This function looks through the code for a function, and tries to
8030    optimize the usage of the $gp register.  We arrange to copy $gp
8031    into a pseudo-register, and then let gcc's normal reload handling
8032    deal with the pseudo-register.  Unfortunately, if reload choose to
8033    put the pseudo-register into a call-clobbered register, it will
8034    emit saves and restores for that register around any function
8035    calls.  We don't need the saves, and it's faster to copy $gp than
8036    to do an actual restore.  ??? This still means that we waste a
8037    stack slot.
8038
8039    This is an optimization, and the code which gcc has actually
8040    generated is correct, so we do not need to catch all cases.  */
8041
8042 static void
8043 mips16_optimize_gp (first)
8044      rtx first;
8045 {
8046   rtx gpcopy, slot, insn;
8047
8048   /* Look through the instructions.  Set GPCOPY to the register which
8049      holds a copy of $gp.  Set SLOT to the stack slot where it is
8050      saved.  If we find an instruction which sets GPCOPY to anything
8051      other than $gp or SLOT, then we can't use it.  If we find an
8052      instruction which sets SLOT to anything other than GPCOPY, we
8053      can't use it.  */
8054
8055   gpcopy = NULL_RTX;
8056   slot = NULL_RTX;
8057   for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
8058     {
8059       rtx set;
8060
8061       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8062         continue;
8063
8064       set = PATTERN (insn);
8065
8066       /* We know that all references to memory will be inside a SET,
8067          because there is no other way to access memory on the mips16.
8068          We don't have to worry about a PARALLEL here, because the
8069          mips.md file will never generate them for memory references.  */
8070       if (GET_CODE (set) != SET)
8071         continue;
8072
8073       if (gpcopy == NULL_RTX
8074           && GET_CODE (SET_SRC (set)) == CONST
8075           && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
8076           && REGNO (XEXP (SET_SRC (set), 0)) == GP_REG_FIRST + 28
8077           && GET_CODE (SET_DEST (set)) == REG
8078           && GET_MODE (SET_DEST (set)) == Pmode)
8079         gpcopy = SET_DEST (set);
8080       else if (slot == NULL_RTX
8081                && gpcopy != NULL_RTX
8082                && GET_CODE (SET_DEST (set)) == MEM
8083                && GET_CODE (SET_SRC (set)) == REG
8084                && REGNO (SET_SRC (set)) == REGNO (gpcopy)
8085                && GET_MODE (SET_DEST (set)) == Pmode)
8086         {
8087           rtx base, offset;
8088
8089           offset = const0_rtx;
8090           base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
8091           if (GET_CODE (base) == REG
8092               && (REGNO (base) == STACK_POINTER_REGNUM
8093                   || REGNO (base) == FRAME_POINTER_REGNUM))
8094             slot = SET_DEST (set);
8095         }
8096       else if (gpcopy != NULL_RTX
8097                && (GET_CODE (SET_DEST (set)) == REG
8098                    || GET_CODE (SET_DEST (set)) == SUBREG)
8099                && reg_overlap_mentioned_p (SET_DEST (set), gpcopy)
8100                && (GET_CODE (SET_DEST (set)) != REG
8101                    || REGNO (SET_DEST (set)) != REGNO (gpcopy)
8102                    || GET_MODE (SET_DEST (set)) != Pmode
8103                    || ((GET_CODE (SET_SRC (set)) != CONST
8104                         || GET_CODE (XEXP (SET_SRC (set), 0)) != REG
8105                         || (REGNO (XEXP (SET_SRC (set), 0))
8106                             != GP_REG_FIRST + 28))
8107                        && ! rtx_equal_p (SET_SRC (set), slot))))
8108         break;
8109       else if (slot != NULL_RTX
8110                && GET_CODE (SET_DEST (set)) == MEM
8111                && rtx_equal_p (SET_DEST (set), slot)
8112                && (GET_CODE (SET_SRC (set)) != REG
8113                    || REGNO (SET_SRC (set)) != REGNO (gpcopy)))
8114         break;
8115     }
8116
8117   /* If we couldn't find a unique value for GPCOPY or SLOT, then try a
8118      different optimization.  Any time we find a copy of $28 into a
8119      register, followed by an add of a symbol_ref to that register, we
8120      convert it to load the value from the constant table instead.
8121      The copy and add will take six bytes, just as the load and
8122      constant table entry will take six bytes.  However, it is
8123      possible that the constant table entry will be shared.
8124
8125      This could be a peephole optimization, but I don't know if the
8126      peephole code can call force_const_mem.
8127
8128      Using the same register for the copy of $28 and the add of the
8129      symbol_ref is actually pretty likely, since the add instruction
8130      requires the destination and the first addend to be the same
8131      register.  */
8132
8133   if (insn != NULL_RTX || gpcopy == NULL_RTX || slot == NULL_RTX)
8134     {
8135       rtx next;
8136
8137       /* This optimization is only reasonable if the constant table
8138          entries are only 4 bytes.  */
8139       if (Pmode != SImode)
8140         return;
8141
8142       for (insn = first; insn != NULL_RTX; insn = next)
8143         {
8144           rtx set1, set2;
8145
8146           next = insn;
8147           do
8148             {
8149               next = NEXT_INSN (next);
8150             }
8151           while (next != NULL_RTX
8152                  && (GET_CODE (next) == NOTE
8153                      || (GET_CODE (next) == INSN
8154                          && (GET_CODE (PATTERN (next)) == USE
8155                              || GET_CODE (PATTERN (next)) == CLOBBER))));
8156
8157           if (next == NULL_RTX)
8158             break;
8159
8160           if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8161             continue;
8162
8163           if (GET_RTX_CLASS (GET_CODE (next)) != 'i')
8164             continue;
8165
8166           set1 = PATTERN (insn);
8167           if (GET_CODE (set1) != SET)
8168             continue;
8169           set2 = PATTERN (next);
8170           if (GET_CODE (set2) != SET)
8171             continue;
8172
8173           if (GET_CODE (SET_DEST (set1)) == REG
8174               && GET_CODE (SET_SRC (set1)) == CONST
8175               && GET_CODE (XEXP (SET_SRC (set1), 0)) == REG
8176               && REGNO (XEXP (SET_SRC (set1), 0)) == GP_REG_FIRST + 28
8177               && rtx_equal_p (SET_DEST (set1), SET_DEST (set2))
8178               && GET_CODE (SET_SRC (set2)) == PLUS
8179               && rtx_equal_p (SET_DEST (set1), XEXP (SET_SRC (set2), 0))
8180               && mips16_gp_offset_p (XEXP (SET_SRC (set2), 1))
8181               && GET_CODE (XEXP (XEXP (SET_SRC (set2), 1), 0)) == MINUS)
8182             {
8183               rtx sym;
8184
8185               /* We've found a case we can change to load from the
8186                  constant table.  */
8187
8188               sym = XEXP (XEXP (XEXP (SET_SRC (set2), 1), 0), 0);
8189               if (GET_CODE (sym) != SYMBOL_REF)
8190                 abort ();
8191               emit_insn_after (gen_rtx (SET, VOIDmode, SET_DEST (set1),
8192                                         force_const_mem (Pmode, sym)),
8193                                next);
8194               
8195               PUT_CODE (insn, NOTE);
8196               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8197               NOTE_SOURCE_FILE (insn) = 0;
8198
8199               PUT_CODE (next, NOTE);
8200               NOTE_LINE_NUMBER (next) = NOTE_INSN_DELETED;
8201               NOTE_SOURCE_FILE (next) = 0;
8202             }
8203         }
8204
8205       return;
8206     }
8207
8208   /* We can safely remove all assignments to SLOT from GPCOPY, and
8209      replace all assignments from SLOT to GPCOPY with assignments from
8210      $28.  */
8211
8212   for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
8213     {
8214       rtx set;
8215
8216       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8217         continue;
8218
8219       set = PATTERN (insn);
8220       if (GET_CODE (set) != SET
8221           || GET_MODE (SET_DEST (set)) != Pmode)
8222         continue;
8223
8224       if (GET_CODE (SET_DEST (set)) == MEM
8225           && rtx_equal_p (SET_DEST (set), slot)
8226           && GET_CODE (SET_SRC (set)) == REG
8227           && REGNO (SET_SRC (set)) == REGNO (gpcopy))
8228         {
8229           PUT_CODE (insn, NOTE);
8230           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8231           NOTE_SOURCE_FILE (insn) = 0;
8232         }
8233       else if (GET_CODE (SET_DEST (set)) == REG
8234                && REGNO (SET_DEST (set)) == REGNO (gpcopy)
8235                && GET_CODE (SET_SRC (set)) == MEM
8236                && rtx_equal_p (SET_SRC (set), slot))
8237         {
8238           emit_insn_after (gen_rtx (SET, Pmode, SET_DEST (set),
8239                                     gen_rtx (CONST, Pmode,
8240                                              gen_rtx (REG, Pmode,
8241                                                       GP_REG_FIRST + 28))),
8242                            insn);
8243           PUT_CODE (insn, NOTE);
8244           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
8245           NOTE_SOURCE_FILE (insn) = 0;
8246         }
8247     }
8248 }
8249
8250 /* We keep a list of constants we which we have to add to internal
8251    constant tables in the middle of large functions.  */
8252
8253 struct constant
8254 {
8255   struct constant *next;
8256   rtx value;
8257   rtx label;
8258   enum machine_mode mode;
8259 };
8260
8261 /* Add a constant to the list in *PCONSTANTS.  */
8262
8263 static rtx
8264 add_constant (pconstants, val, mode)
8265      struct constant **pconstants;
8266      rtx val;
8267      enum machine_mode mode;
8268 {
8269   struct constant *c;
8270
8271   for (c = *pconstants; c != NULL; c = c->next)
8272     if (mode == c->mode && rtx_equal_p (val, c->value))
8273       return c->label;
8274
8275   c = (struct constant *) xmalloc (sizeof *c);
8276   c->value = val;
8277   c->mode = mode;
8278   c->label = gen_label_rtx ();
8279   c->next = *pconstants;
8280   *pconstants = c;
8281   return c->label;
8282 }
8283
8284 /* Dump out the constants in CONSTANTS after INSN.  */
8285
8286 static void
8287 dump_constants (constants, insn)
8288      struct constant *constants;
8289      rtx insn;
8290 {
8291   struct constant *c;
8292   int align;
8293
8294   c = constants;
8295   align = 0;
8296   while (c != NULL)
8297     {
8298       rtx r;
8299       struct constant *next;
8300
8301       switch (GET_MODE_SIZE (c->mode))
8302         {
8303         case 1:
8304           align = 0;
8305           break;
8306         case 2:
8307           if (align < 1)
8308             insn = emit_insn_after (gen_align_2 (), insn);
8309           align = 1;
8310           break;
8311         case 4:
8312           if (align < 2)
8313             insn = emit_insn_after (gen_align_4 (), insn);
8314           align = 2;
8315           break;
8316         default:
8317           if (align < 3)
8318             insn = emit_insn_after (gen_align_8 (), insn);
8319           align = 3;
8320           break;
8321         }
8322
8323       insn = emit_label_after (c->label, insn);
8324
8325       switch (c->mode)
8326         {
8327         case QImode:
8328           r = gen_consttable_qi (c->value);
8329           break;
8330         case HImode:
8331           r = gen_consttable_hi (c->value);
8332           break;
8333         case SImode:
8334           r = gen_consttable_si (c->value);
8335           break;
8336         case SFmode:
8337           r = gen_consttable_sf (c->value);
8338           break;
8339         case DImode:
8340           r = gen_consttable_di (c->value);
8341           break;
8342         case DFmode:
8343           r = gen_consttable_df (c->value);
8344           break;
8345         default:
8346           abort ();
8347         }
8348
8349       insn = emit_insn_after (r, insn);
8350
8351       next = c->next;
8352       free (c);
8353       c = next;
8354     }
8355
8356   emit_barrier_after (insn);
8357 }
8358
8359 /* Find the symbol in an address expression.  */
8360
8361 static rtx
8362 mips_find_symbol (addr)
8363      rtx addr;
8364 {
8365   if (GET_CODE (addr) == MEM)
8366     addr = XEXP (addr, 0);
8367   while (GET_CODE (addr) == CONST)
8368     addr = XEXP (addr, 0);
8369   if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
8370     return addr;
8371   if (GET_CODE (addr) == PLUS)
8372     {
8373       rtx l1, l2;
8374
8375       l1 = mips_find_symbol (XEXP (addr, 0));
8376       l2 = mips_find_symbol (XEXP (addr, 1));
8377       if (l1 != NULL_RTX && l2 == NULL_RTX)
8378         return l1;
8379       else if (l1 == NULL_RTX && l2 != NULL_RTX)
8380         return l2;
8381     }
8382   return NULL_RTX;
8383 }
8384
8385 /* Exported to toplev.c.
8386
8387    Do a final pass over the function, just before delayed branch
8388    scheduling.  */
8389
8390 void
8391 machine_dependent_reorg (first)
8392      rtx first;
8393 {
8394   int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
8395   rtx insn;
8396   struct constant *constants;
8397
8398   if (! TARGET_MIPS16)
8399     return;
8400
8401   /* If $gp is used, try to remove stores, and replace loads with
8402      copies from $gp.  */
8403   if (optimize)
8404     mips16_optimize_gp (first);
8405
8406   /* Scan the function looking for PC relative loads which may be out
8407      of range.  All such loads will either be from the constant table,
8408      or be getting the address of a constant string.  If the size of
8409      the function plus the size of the constant table is less than
8410      0x8000, then all loads are in range.  */
8411
8412   insns_len = 0;
8413   for (insn = first; insn; insn = NEXT_INSN (insn))
8414     {
8415       insns_len += get_attr_length (insn);
8416
8417       /* ??? We put switch tables in .text, but we don't define
8418          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
8419          compute their lengths correctly.  */
8420       if (GET_CODE (insn) == JUMP_INSN)
8421         {
8422           rtx body;
8423
8424           body = PATTERN (insn);
8425           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
8426             insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
8427                           * GET_MODE_SIZE (GET_MODE (body)));
8428           insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
8429         }
8430     }
8431
8432   /* Store the original value of insns_len in current_frame_info, so
8433      that simple_memory_operand can look at it.  */
8434   current_frame_info.insns_len = insns_len;
8435
8436   pool_size = get_pool_size ();
8437   if (insns_len + pool_size + mips_string_length < 0x8000)
8438     return;
8439
8440   /* Loop over the insns and figure out what the maximum internal pool
8441      size could be.  */
8442   max_internal_pool_size = 0;
8443   for (insn = first; insn; insn = NEXT_INSN (insn))
8444     {
8445       if (GET_CODE (insn) == INSN
8446           && GET_CODE (PATTERN (insn)) == SET)
8447         {
8448           rtx src;
8449
8450           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
8451           if (src == NULL_RTX)
8452             continue;
8453           if (CONSTANT_POOL_ADDRESS_P (src))
8454             max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
8455           else if (SYMBOL_REF_FLAG (src))
8456             max_internal_pool_size += GET_MODE_SIZE (Pmode);
8457         }
8458     }
8459
8460   constants = NULL;
8461   addr = 0;
8462   first_constant_ref = -1;
8463
8464   for (insn = first; insn; insn = NEXT_INSN (insn))
8465     {
8466       if (GET_CODE (insn) == INSN
8467           && GET_CODE (PATTERN (insn)) == SET)
8468         {
8469           rtx val, src;
8470           enum machine_mode mode;
8471
8472           val = NULL_RTX;
8473           src = mips_find_symbol (SET_SRC (PATTERN (insn)));
8474           if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
8475             {
8476               /* ??? This is very conservative, which means that we
8477                  will generate too many copies of the constant table.
8478                  The only solution would seem to be some form of
8479                  relaxing.  */
8480               if (((insns_len - addr)
8481                    + max_internal_pool_size
8482                    + get_pool_offset (src))
8483                   >= 0x8000)
8484                 {
8485                   val = get_pool_constant (src);
8486                   mode = get_pool_mode (src);
8487                 }
8488               max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
8489             }
8490           else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
8491             {
8492               /* Including all of mips_string_length is conservative,
8493                  and so is including all of max_internal_pool_size.  */
8494               if (((insns_len - addr)
8495                    + max_internal_pool_size
8496                    + pool_size
8497                    + mips_string_length)
8498                   >= 0x8000)
8499               val = src;
8500               mode = Pmode;
8501               max_internal_pool_size -= Pmode;
8502             }
8503
8504           if (val != NULL_RTX)
8505             {
8506               rtx lab, newsrc;
8507
8508               /* This PC relative load is out of range.  ??? In the
8509                  case of a string constant, we are only guessing that
8510                  it is range, since we don't know the offset of a
8511                  particular string constant.  */
8512
8513               lab = add_constant (&constants, val, mode);
8514               newsrc = gen_rtx (MEM, mode,
8515                                 gen_rtx (LABEL_REF, VOIDmode, lab));
8516               RTX_UNCHANGING_P (newsrc) = 1;
8517               PATTERN (insn) = gen_rtx (SET, VOIDmode,
8518                                         SET_DEST (PATTERN (insn)),
8519                                         newsrc);
8520               INSN_CODE (insn) = -1;
8521
8522               if (first_constant_ref < 0)
8523                 first_constant_ref = addr;
8524             }
8525         }
8526
8527       addr += get_attr_length (insn);
8528
8529       /* ??? We put switch tables in .text, but we don't define
8530          JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
8531          compute their lengths correctly.  */
8532       if (GET_CODE (insn) == JUMP_INSN)
8533         {
8534           rtx body;
8535
8536           body = PATTERN (insn);
8537           if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
8538             addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
8539                           * GET_MODE_SIZE (GET_MODE (body)));
8540           addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
8541         }
8542
8543       if (GET_CODE (insn) == BARRIER)
8544         {
8545           /* Output any constants we have accumulated.  Note that we
8546              don't need to change ADDR, since its only use is
8547              subtraction from INSNS_LEN, and both would be changed by
8548              the same amount.
8549              ??? If the instructions up to the next barrier reuse a
8550              constant, it would often be better to continue
8551              accumulating.  */
8552           if (constants != NULL)
8553             dump_constants (constants, insn);
8554           constants = NULL;
8555           first_constant_ref = -1;
8556         }
8557       
8558       if (constants != NULL
8559                && (NEXT_INSN (insn) == NULL 
8560                    || (first_constant_ref >= 0 
8561                        && (((addr - first_constant_ref)
8562                             + 2 /* for alignment */
8563                             + 2 /* for a short jump insn */
8564                             + pool_size)
8565                            >= 0x8000))))
8566         {
8567           /* If we haven't had a barrier within 0x8000 bytes of a
8568              constant reference or we are at the end of the function,
8569              emit a barrier now. */
8570
8571           rtx label, jump, barrier;
8572               
8573           label = gen_label_rtx ();
8574           jump = emit_jump_insn_after (gen_jump (label), insn);
8575           JUMP_LABEL (jump) = label;
8576           LABEL_NUSES (label) = 1;
8577           barrier = emit_barrier_after (jump);
8578           emit_label_after (label, barrier);
8579           first_constant_ref = -1;
8580         }
8581      }
8582
8583   /* ??? If we output all references to a constant in internal
8584      constants table, we don't need to output the constant in the real
8585      constant table, but we have no way to prevent that.  */
8586 }
8587
8588 /* Return nonzero if X is a SIGN or ZERO extend operator.  */
8589 int
8590 extend_operator (x, mode)
8591      rtx x;
8592      enum machine_mode mode ATTRIBUTE_UNUSED;
8593 {
8594   enum rtx_code code = GET_CODE (x);
8595   return code == SIGN_EXTEND || code == ZERO_EXTEND;
8596 }
8597
8598 /* Accept any operator that can be used to shift the high half of the
8599    input value to the lower half, suitable for truncation.  The
8600    remainder (the lower half of the input, and the upper half of the
8601    output) will be discarded.  */
8602 int
8603 highpart_shift_operator (x, mode)
8604      rtx x;
8605      enum machine_mode mode ATTRIBUTE_UNUSED;
8606 {
8607   enum rtx_code code = GET_CODE (x);
8608   return (code == LSHIFTRT
8609           || code == ASHIFTRT
8610           || code == ROTATERT
8611           || code == ROTATE);
8612 }
8613
8614 /* Return the length of INSN.  LENGTH is the initial length computed by 
8615    attributes in the machine-description file.  */
8616
8617 int
8618 mips_adjust_insn_length (insn, length)
8619      rtx insn;
8620      int length;
8621 {
8622   /* A unconditional jump has an unfilled delay slot if it is not part
8623      of a sequence.  A conditional jump normally has a delay slot, but
8624      does not on MIPS16.  */
8625   if (simplejump_p (insn)
8626       || (!TARGET_MIPS16  && (GET_CODE (insn) == JUMP_INSN 
8627                               || GET_CODE (insn) == CALL_INSN)))
8628     length += 4;
8629
8630   /* All MIPS16 instructions are a measly two bytes.  */
8631   if (TARGET_MIPS16)
8632     length /= 2;
8633
8634   return length;
8635 }
8636
8637 /* Output assembly instructions to peform a conditional branch.  
8638
8639    INSN is the branch instruction.  OPERANDS[0] is the condition.
8640    OPERANDS[1] is the target of the branch.  OPERANDS[2] is the target
8641    of the first operand to the condition.  If TWO_OPERANDS_P is
8642    non-zero the comparison takes two operands; OPERANDS[3] will be the
8643    second operand.
8644
8645    If INVERTED_P is non-zero we are to branch if the condition does
8646    not hold.  If FLOAT_P is non-zero this is a floating-point comparison.
8647
8648    LENGTH is the length (in bytes) of the sequence we are to generate.
8649    That tells us whether to generate a simple conditional branch, or a
8650    reversed conditional branch around a `jr' instruction.  */
8651 char *
8652 mips_output_conditional_branch (insn, 
8653                                 operands, 
8654                                 two_operands_p,
8655                                 float_p,
8656                                 inverted_p,
8657                                 length)
8658      rtx insn;
8659      rtx *operands;
8660      int two_operands_p;
8661      int float_p;
8662      int inverted_p;
8663      int length;
8664 {
8665   static char buffer[200];
8666   /* The kind of comparison we are doing.  */
8667   enum rtx_code code = GET_CODE (operands[0]);
8668   /* Non-zero if the opcode for the comparison needs a `z' indicating
8669      that it is a comparision against zero.  */
8670   int need_z_p;
8671   /* A string to use in the assembly output to represent the first
8672      operand.  */
8673   char *op1 = "%z2";
8674   /* A string to use in the assembly output to represent the second
8675      operand.  Use the hard-wired zero register if there's no second
8676      operand.  */
8677   char *op2 = (two_operands_p ? ",%z3" : ",%.");
8678   /* The operand-printing string for the comparison.  */
8679   char *comp = (float_p ? "%F0" : "%C0");
8680   /* The operand-printing string for the inverted comparison.  */
8681   char *inverted_comp = (float_p ? "%W0" : "%N0");
8682
8683   /* The MIPS processors (for levels of the ISA at least two), have
8684      "likely" variants of each branch instruction.  These instructions
8685      annul the instruction in the delay slot if the branch is not
8686      taken.  */
8687   mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
8688
8689   if (!two_operands_p)
8690     {
8691       /* To compute whether than A > B, for example, we normally
8692          subtract B from A and then look at the sign bit.  But, if we
8693          are doing an unsigned comparison, and B is zero, we don't
8694          have to do the subtraction.  Instead, we can just check to
8695          see if A is non-zero.  Thus, we change the CODE here to
8696          reflect the simpler comparison operation.  */
8697       switch (code)
8698         {
8699         case GTU:
8700           code = NE;
8701           break;
8702
8703         case LEU:
8704           code = EQ;
8705           break;
8706
8707         case GEU:
8708           /* A condition which will always be true.  */
8709           code = EQ;
8710           op1 = "%.";
8711           break;
8712
8713         case LTU:
8714           /* A condition which will always be false. */
8715           code = NE;
8716           op1 = "%.";
8717           break;
8718
8719         default:
8720           /* Not a special case.  */
8721           break;
8722         }
8723     }
8724
8725   /* Relative comparisons are always done against zero.  But
8726      equality comparisons are done between two operands, and therefore
8727      do not require a `z' in the assembly language output.  */
8728   need_z_p = (!float_p && code != EQ && code != NE);
8729   /* For comparisons against zero, the zero is not provided 
8730      explicitly.  */
8731   if (need_z_p)
8732     op2 = "";
8733
8734   /* Begin by terminating the buffer.  That way we can always use
8735      strcat to add to it.  */
8736   buffer[0] = '\0';
8737
8738   switch (length) 
8739     {
8740     case 4:
8741     case 8:
8742       /* Just a simple conditional branch.  */
8743       if (float_p)
8744         sprintf (buffer, "%%*b%s%%?\t%%Z2%%1",
8745                  inverted_p ? inverted_comp : comp);
8746       else
8747         sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1",
8748                  inverted_p ? inverted_comp : comp,
8749                  need_z_p ? "z" : "",
8750                  op1,
8751                  op2);
8752       return buffer;
8753
8754     case 12:
8755     case 16:
8756       {
8757         /* Generate a reversed conditional branch around ` j'
8758            instruction:
8759
8760                 .set noreorder
8761                 .set nomacro
8762                 bc    l
8763                 nop
8764                 j     target
8765                 .set macro
8766                 .set reorder
8767              l:
8768
8769            Because we have to jump four bytes *past* the following
8770            instruction if this branch was annulled, we can't just use
8771            a label, as in the picture above; there's no way to put the
8772            label after the next instruction, as the assembler does not
8773            accept `.L+4' as the target of a branch.  (We can't just
8774            wait until the next instruction is output; it might be a
8775            macro and take up more than four bytes.  Once again, we see
8776            why we want to eliminate macros.)
8777            
8778            If the branch is annulled, we jump four more bytes that we
8779            would otherwise; that way we skip the annulled instruction
8780            in the delay slot.  */
8781
8782         char *target 
8783           = ((mips_branch_likely || length == 16) ? ".+16" : ".+12");
8784         char *c;
8785
8786         strcpy (buffer, "%(%<");
8787         c = strchr (buffer, '\0');
8788         /* Generate the reversed comparision.  This takes four 
8789            bytes.  */
8790         if (float_p)
8791           sprintf (c, "%%*b%s\t%%Z2%s",
8792                    inverted_p ? comp : inverted_comp,
8793                    target);
8794         else
8795           sprintf (c, "%%*b%s%s\t%s%s,%s",
8796                    inverted_p ? comp : inverted_comp,
8797                    need_z_p ? "z" : "",
8798                    op1,
8799                    op2,
8800                    target);
8801         strcat (c, "\n\tnop\n\tj\t%1");
8802         if (length == 16)
8803           /* The delay slot was unfilled.  Since we're inside
8804              .noreorder, the assembler will not fill in the NOP for
8805              us, so we must do it ourselves.  */
8806           strcat (buffer, "\n\tnop");
8807         strcat (buffer, "%>%)");
8808         return buffer;
8809       }
8810
8811     /* We do not currently use this code.  It handles jumps to
8812        arbitrary locations, using `jr', even across a 256MB boundary.
8813        We could add a -mhuge switch, and then use this code instead of
8814        the `j' alternative above when -mhuge was used.  */
8815 #if 0
8816     case 16:
8817     case 20:
8818       {
8819         /* Generate a reversed conditional branch around a `jr'
8820            instruction:
8821
8822                  .set noreorder
8823                  .set nomacro
8824                  .set noat
8825                  bc    l
8826                  la    $at, target
8827                  jr    $at
8828                  .set at
8829                  .set macro
8830                  .set reorder
8831               l:
8832
8833            Not pretty, but allows a conditional branch anywhere in the
8834            32-bit address space.  If the original branch is annulled,
8835            then the instruction in the delay slot should be executed
8836            only if the branch is taken.  The la instruction is really
8837            a macro which will usually take eight bytes, but sometimes
8838            takes only four, if the instruction to which we're jumping
8839            gets its own entry in the global pointer table, which will
8840            happen if its a case label.  The assembler will then
8841            generate only a four-byte sequence, rather than eight, and
8842            there seems to be no way to tell it not to.  Thus, we can't
8843            just use a `.+x' addressing form; we don't know what value
8844            to give for `x'.  
8845
8846            So, we resort to using the explicit relocation syntax
8847            available in the assembler and do:
8848
8849               lw $at,%got_page(target)($gp)
8850               daddiu $at,$at,%got_ofst(target)
8851
8852            That way, this always takes up eight bytes, and we can use
8853            the `.+x' form.  Of course, these explicit machinations
8854            with relocation will not work with old assemblers.  Then
8855            again, neither do out-of-range branches, so we haven't lost
8856            anything.  */
8857
8858         /* The target of the reversed branch.  */
8859         char *target 
8860           = ((mips_branch_likely || length == 20) ? ".+20" : ".+16");
8861         char *at_register = mips_reg_names[ASSEMBLER_SCRATCH_REGNUM];
8862         char *gp_register = mips_reg_names[PIC_OFFSET_TABLE_REGNUM];
8863         char *c;
8864
8865         strcpy (buffer, "%(%<%[");
8866         c = strchr (buffer, '\0');
8867         /* Generate the reversed comparision.  This takes four 
8868            bytes.  */
8869         if (float_p)
8870           sprintf (c, "%%*b%s\t%%Z2%s",
8871                    inverted_p ? comp : inverted_comp,
8872                    target);
8873         else
8874           sprintf (c, "%%*b%s%s\t%s%s,%s",
8875                    inverted_p ? comp : inverted_comp,
8876                    need_z_p ? "z" : "",
8877                    op1,
8878                    op2,
8879                    target);
8880         c = strchr (buffer, '\0');
8881         /* Generate the load-address, and jump.  This takes twelve
8882            bytes, for a total of 16.  */
8883         sprintf (c,
8884                  "\n\tlw\t%s,%%%%got_page(%%1)(%s)\n\tdaddiu\t%s,%s,%%%%got_ofst(%%1)\n\tjr\t%s",
8885                  at_register,
8886                  gp_register,
8887                  at_register,
8888                  at_register,
8889                  at_register);
8890         if (length == 20)
8891           /* The delay slot was unfilled.  Since we're inside
8892              .noreorder, the assembler will not fill in the NOP for
8893              us, so we must do it ourselves.  */
8894           strcat (buffer, "\n\tnop");
8895         strcat (buffer, "%]%>%)");
8896         return buffer;
8897       }
8898 #endif
8899
8900     default:
8901       abort ();
8902     }
8903
8904   /* NOTREACHED */
8905   return 0;
8906 }