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