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