* Makefile.in (cse.o): Depend on TARGET_H.
[official-gcc.git] / gcc / config / mips / mips.c
blob4dcf5248ab57f4938f146dab924b6a99931f9014
1 /* Subroutines for insn-output.c for MIPS
2 Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by A. Lichnewsky, lich@inria.inria.fr.
5 Changes by Michael Meissner, meissner@osf.org.
6 64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
7 Brendan Eich, brendan@microunity.com.
9 This file is part of GNU CC.
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.
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.
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. */
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. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include <signal.h>
35 #include "rtl.h"
36 #include "regs.h"
37 #include "hard-reg-set.h"
38 #include "real.h"
39 #include "insn-config.h"
40 #include "conditions.h"
41 #include "insn-attr.h"
42 #include "recog.h"
43 #include "toplev.h"
44 #include "output.h"
45 #include "tree.h"
46 #include "function.h"
47 #include "expr.h"
48 #include "flags.h"
49 #include "reload.h"
50 #include "output.h"
51 #include "tm_p.h"
52 #include "ggc.h"
53 #include "gstab.h"
54 #include "hashtab.h"
55 #include "debug.h"
56 #include "target.h"
57 #include "target-def.h"
59 #ifdef __GNU_STAB__
60 #define STAB_CODE_TYPE enum __stab_debug_code
61 #else
62 #define STAB_CODE_TYPE int
63 #endif
65 extern tree lookup_name PARAMS ((tree));
67 /* Enumeration for all of the relational tests, so that we can build
68 arrays indexed by the test type, and not worry about the order
69 of EQ, NE, etc. */
71 enum internal_test {
72 ITEST_EQ,
73 ITEST_NE,
74 ITEST_GT,
75 ITEST_GE,
76 ITEST_LT,
77 ITEST_LE,
78 ITEST_GTU,
79 ITEST_GEU,
80 ITEST_LTU,
81 ITEST_LEU,
82 ITEST_MAX
86 struct constant;
87 struct mips_arg_info;
88 static enum internal_test map_test_to_internal_test PARAMS ((enum rtx_code));
89 static void get_float_compare_codes PARAMS ((enum rtx_code, enum rtx_code *,
90 enum rtx_code *));
91 static int mips16_simple_memory_operand PARAMS ((rtx, rtx,
92 enum machine_mode));
93 static int m16_check_op PARAMS ((rtx, int, int, int));
94 static void block_move_loop PARAMS ((rtx, rtx,
95 unsigned int,
96 int,
97 rtx, rtx));
98 static void block_move_call PARAMS ((rtx, rtx, rtx));
99 static void mips_arg_info PARAMS ((const CUMULATIVE_ARGS *,
100 enum machine_mode,
101 tree, int,
102 struct mips_arg_info *));
103 static rtx mips_add_large_offset_to_sp PARAMS ((HOST_WIDE_INT));
104 static void mips_annotate_frame_insn PARAMS ((rtx, rtx));
105 static rtx mips_frame_set PARAMS ((enum machine_mode,
106 int, int));
107 static void mips_emit_frame_related_store PARAMS ((rtx, rtx,
108 HOST_WIDE_INT));
109 static void save_restore_insns PARAMS ((int, rtx, long));
110 static void mips16_output_gp_offset PARAMS ((FILE *, rtx));
111 static void mips16_fp_args PARAMS ((FILE *, int, int));
112 static void build_mips16_function_stub PARAMS ((FILE *));
113 static void mips16_optimize_gp PARAMS ((rtx));
114 static rtx add_constant PARAMS ((struct constant **,
115 rtx,
116 enum machine_mode));
117 static void dump_constants PARAMS ((struct constant *,
118 rtx));
119 static rtx mips_find_symbol PARAMS ((rtx));
120 static void abort_with_insn PARAMS ((rtx, const char *))
121 ATTRIBUTE_NORETURN;
122 static int symbolic_expression_p PARAMS ((rtx));
123 static bool mips_assemble_integer PARAMS ((rtx, unsigned int, int));
124 static void mips_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
125 static void mips_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
126 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
127 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
128 static bool mips_strict_matching_cpu_name_p PARAMS ((const char *,
129 const char *));
130 static bool mips_matching_cpu_name_p PARAMS ((const char *,
131 const char *));
132 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
133 const char *));
134 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
135 static void copy_file_data PARAMS ((FILE *, FILE *));
136 #ifdef TARGET_IRIX6
137 static void iris6_asm_named_section_1 PARAMS ((const char *,
138 unsigned int,
139 unsigned int));
140 static void iris6_asm_named_section PARAMS ((const char *,
141 unsigned int));
142 static int iris_section_align_entry_eq PARAMS ((const PTR, const PTR));
143 static hashval_t iris_section_align_entry_hash PARAMS ((const PTR));
144 static int iris6_section_align_1 PARAMS ((void **, void *));
145 #endif
146 static int mips_adjust_cost PARAMS ((rtx, rtx, rtx, int));
147 static int mips_issue_rate PARAMS ((void));
149 static struct machine_function * mips_init_machine_status PARAMS ((void));
150 static void mips_select_section PARAMS ((tree, int, unsigned HOST_WIDE_INT))
151 ATTRIBUTE_UNUSED;
152 static void mips_unique_section PARAMS ((tree, int))
153 ATTRIBUTE_UNUSED;
154 static void mips_select_rtx_section PARAMS ((enum machine_mode, rtx,
155 unsigned HOST_WIDE_INT));
156 static int mips_use_dfa_pipeline_interface PARAMS ((void));
157 static void mips_encode_section_info PARAMS ((tree, int));
158 static bool mips_rtx_costs PARAMS ((rtx, int, int, int *));
161 /* Structure to be filled in by compute_frame_size with register
162 save masks, and offsets for the current function. */
164 struct mips_frame_info GTY(())
166 long total_size; /* # bytes that the entire frame takes up */
167 long var_size; /* # bytes that variables take up */
168 long args_size; /* # bytes that outgoing arguments take up */
169 long extra_size; /* # bytes of extra gunk */
170 int gp_reg_size; /* # bytes needed to store gp regs */
171 int fp_reg_size; /* # bytes needed to store fp regs */
172 long mask; /* mask of saved gp registers */
173 long fmask; /* mask of saved fp registers */
174 long gp_save_offset; /* offset from vfp to store gp registers */
175 long fp_save_offset; /* offset from vfp to store fp registers */
176 long gp_sp_offset; /* offset from new sp to store gp registers */
177 long fp_sp_offset; /* offset from new sp to store fp registers */
178 int initialized; /* != 0 if frame size already calculated */
179 int num_gp; /* number of gp registers saved */
180 int num_fp; /* number of fp registers saved */
183 struct machine_function GTY(()) {
184 /* Pseudo-reg holding the address of the current function when
185 generating embedded PIC code. Created by LEGITIMIZE_ADDRESS,
186 used by mips_finalize_pic if it was created. */
187 rtx embedded_pic_fnaddr_rtx;
189 /* Pseudo-reg holding the value of $28 in a mips16 function which
190 refers to GP relative global variables. */
191 rtx mips16_gp_pseudo_rtx;
193 /* Current frame information, calculated by compute_frame_size. */
194 struct mips_frame_info frame;
196 /* Length of instructions in function; mips16 only. */
197 long insns_len;
200 /* Information about a single argument. */
201 struct mips_arg_info
203 /* True if the argument is a record or union type. */
204 bool struct_p;
206 /* True if the argument is passed in a floating-point register, or
207 would have been if we hadn't run out of registers. */
208 bool fpr_p;
210 /* The argument's size, in bytes. */
211 unsigned int num_bytes;
213 /* The number of words passed in registers, rounded up. */
214 unsigned int reg_words;
216 /* The offset of the first register from GP_ARG_FIRST or FP_ARG_FIRST,
217 or MAX_ARGS_IN_REGISTERS if the argument is passed entirely
218 on the stack. */
219 unsigned int reg_offset;
221 /* The number of words that must be passed on the stack, rounded up. */
222 unsigned int stack_words;
224 /* The offset from the start of the stack overflow area of the argument's
225 first stack word. Only meaningful when STACK_WORDS is nonzero. */
226 unsigned int stack_offset;
229 /* Global variables for machine-dependent things. */
231 /* Threshold for data being put into the small data/bss area, instead
232 of the normal data area (references to the small data/bss area take
233 1 instruction, and use the global pointer, references to the normal
234 data area takes 2 instructions). */
235 int mips_section_threshold = -1;
237 /* Count the number of .file directives, so that .loc is up to date. */
238 int num_source_filenames = 0;
240 /* Count the number of sdb related labels are generated (to find block
241 start and end boundaries). */
242 int sdb_label_count = 0;
244 /* Next label # for each statement for Silicon Graphics IRIS systems. */
245 int sym_lineno = 0;
247 /* Nonzero if inside of a function, because the stupid MIPS asm can't
248 handle .files inside of functions. */
249 int inside_function = 0;
251 /* Files to separate the text and the data output, so that all of the data
252 can be emitted before the text, which will mean that the assembler will
253 generate smaller code, based on the global pointer. */
254 FILE *asm_out_data_file;
255 FILE *asm_out_text_file;
257 /* Linked list of all externals that are to be emitted when optimizing
258 for the global pointer if they haven't been declared by the end of
259 the program with an appropriate .comm or initialization. */
261 struct extern_list
263 struct extern_list *next; /* next external */
264 const char *name; /* name of the external */
265 int size; /* size in bytes */
266 } *extern_head = 0;
268 /* Name of the file containing the current function. */
269 const char *current_function_file = "";
271 /* Warning given that Mips ECOFF can't support changing files
272 within a function. */
273 int file_in_function_warning = FALSE;
275 /* Whether to suppress issuing .loc's because the user attempted
276 to change the filename within a function. */
277 int ignore_line_number = FALSE;
279 /* Number of nested .set noreorder, noat, nomacro, and volatile requests. */
280 int set_noreorder;
281 int set_noat;
282 int set_nomacro;
283 int set_volatile;
285 /* The next branch instruction is a branch likely, not branch normal. */
286 int mips_branch_likely;
288 /* Count of delay slots and how many are filled. */
289 int dslots_load_total;
290 int dslots_load_filled;
291 int dslots_jump_total;
292 int dslots_jump_filled;
294 /* # of nops needed by previous insn */
295 int dslots_number_nops;
297 /* Number of 1/2/3 word references to data items (ie, not jal's). */
298 int num_refs[3];
300 /* registers to check for load delay */
301 rtx mips_load_reg, mips_load_reg2, mips_load_reg3, mips_load_reg4;
303 /* Cached operands, and operator to compare for use in set/branch/trap
304 on condition codes. */
305 rtx branch_cmp[2];
307 /* what type of branch to use */
308 enum cmp_type branch_type;
310 /* The target cpu for code generation. */
311 enum processor_type mips_arch;
312 const struct mips_cpu_info *mips_arch_info;
314 /* The target cpu for optimization and scheduling. */
315 enum processor_type mips_tune;
316 const struct mips_cpu_info *mips_tune_info;
318 /* which instruction set architecture to use. */
319 int mips_isa;
321 /* which abi to use. */
322 int mips_abi;
324 /* Strings to hold which cpu and instruction set architecture to use. */
325 const char *mips_arch_string; /* for -march=<xxx> */
326 const char *mips_tune_string; /* for -mtune=<xxx> */
327 const char *mips_isa_string; /* for -mips{1,2,3,4} */
328 const char *mips_abi_string; /* for -mabi={32,n32,64,eabi} */
330 /* Whether we are generating mips16 code. This is a synonym for
331 TARGET_MIPS16, and exists for use as an attribute. */
332 int mips16;
334 /* This variable is set by -mno-mips16. We only care whether
335 -mno-mips16 appears or not, and using a string in this fashion is
336 just a way to avoid using up another bit in target_flags. */
337 const char *mips_no_mips16_string;
339 /* Whether we are generating mips16 hard float code. In mips16 mode
340 we always set TARGET_SOFT_FLOAT; this variable is nonzero if
341 -msoft-float was not specified by the user, which means that we
342 should arrange to call mips32 hard floating point code. */
343 int mips16_hard_float;
345 /* This variable is set by -mentry. We only care whether -mentry
346 appears or not, and using a string in this fashion is just a way to
347 avoid using up another bit in target_flags. */
348 const char *mips_entry_string;
350 const char *mips_cache_flush_func = CACHE_FLUSH_FUNC;
352 /* Whether we should entry and exit pseudo-ops in mips16 mode. */
353 int mips_entry;
355 /* If TRUE, we split addresses into their high and low parts in the RTL. */
356 int mips_split_addresses;
358 /* Generating calls to position independent functions? */
359 enum mips_abicalls_type mips_abicalls;
361 /* Mode used for saving/restoring general purpose registers. */
362 static enum machine_mode gpr_mode;
364 /* Array giving truth value on whether or not a given hard register
365 can support a given mode. */
366 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
368 /* The length of all strings seen when compiling for the mips16. This
369 is used to tell how many strings are in the constant pool, so that
370 we can see if we may have an overflow. This is reset each time the
371 constant pool is output. */
372 int mips_string_length;
374 /* When generating mips16 code, a list of all strings that are to be
375 output after the current function. */
377 static GTY(()) rtx mips16_strings;
379 /* In mips16 mode, we build a list of all the string constants we see
380 in a particular function. */
382 struct string_constant
384 struct string_constant *next;
385 const char *label;
388 static struct string_constant *string_constants;
390 /* List of all MIPS punctuation characters used by print_operand. */
391 char mips_print_operand_punct[256];
393 /* Map GCC register number to debugger register number. */
394 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
396 /* Buffer to use to enclose a load/store operation with %{ %} to
397 turn on .set volatile. */
398 static char volatile_buffer[60];
400 /* Hardware names for the registers. If -mrnames is used, this
401 will be overwritten with mips_sw_reg_names. */
403 char mips_reg_names[][8] =
405 "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7",
406 "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
407 "$16", "$17", "$18", "$19", "$20", "$21", "$22", "$23",
408 "$24", "$25", "$26", "$27", "$28", "$sp", "$fp", "$31",
409 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
410 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
411 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
412 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
413 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
414 "$fcc5","$fcc6","$fcc7","$rap", "", "", "", "",
415 "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
416 "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
417 "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
418 "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
419 "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
420 "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
421 "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
422 "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
423 "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
424 "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
425 "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
426 "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
429 /* Mips software names for the registers, used to overwrite the
430 mips_reg_names array. */
432 char mips_sw_reg_names[][8] =
434 "$zero","$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3",
435 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
436 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
437 "$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra",
438 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
439 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
440 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
441 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
442 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
443 "$fcc5","$fcc6","$fcc7","$rap", "", "", "", "",
444 "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
445 "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
446 "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
447 "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
448 "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
449 "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
450 "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
451 "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
452 "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
453 "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
454 "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
455 "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
458 /* Map hard register number to register class */
459 const enum reg_class mips_regno_to_class[] =
461 GR_REGS, GR_REGS, M16_NA_REGS, M16_NA_REGS,
462 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
463 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
464 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
465 M16_NA_REGS, M16_NA_REGS, GR_REGS, GR_REGS,
466 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
467 T_REG, GR_REGS, GR_REGS, GR_REGS,
468 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
469 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
470 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
471 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
472 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
473 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
474 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
475 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
476 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
477 HI_REG, LO_REG, HILO_REG, ST_REGS,
478 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
479 ST_REGS, ST_REGS, ST_REGS, GR_REGS,
480 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
481 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
482 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
483 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
484 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
485 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
486 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
487 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
488 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
489 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
490 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
491 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
492 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
493 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
494 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
495 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
496 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
497 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
498 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
499 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
500 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
501 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
502 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
503 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
504 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS
507 /* Map register constraint character to register class. */
508 enum reg_class mips_char_to_class[256] =
510 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
511 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
512 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
513 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
514 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
515 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
516 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
517 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
518 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
519 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
520 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
521 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
522 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
523 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
524 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
525 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
526 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
527 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
528 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
529 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
530 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
531 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
532 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
533 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
534 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
535 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
536 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
537 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
538 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
539 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
540 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
541 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
542 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
543 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
544 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
545 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
546 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
547 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
548 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
549 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
550 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
551 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
552 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
553 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
554 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
555 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
556 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
557 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
558 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
559 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
560 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
561 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
562 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
563 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
564 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
565 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
566 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
567 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
568 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
569 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
570 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
571 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
572 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
573 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
576 /* A table describing all the processors gcc knows about. Names are
577 matched in the order listed. The first mention of an ISA level is
578 taken as the canonical name for that ISA.
580 To ease comparison, please keep this table in the same order as
581 gas's mips_cpu_info_table[]. */
582 const struct mips_cpu_info mips_cpu_info_table[] = {
583 /* Entries for generic ISAs */
584 { "mips1", PROCESSOR_R3000, 1 },
585 { "mips2", PROCESSOR_R6000, 2 },
586 { "mips3", PROCESSOR_R4000, 3 },
587 { "mips4", PROCESSOR_R8000, 4 },
588 { "mips32", PROCESSOR_4KC, 32 },
589 { "mips32r2", PROCESSOR_M4K, 33 },
590 { "mips64", PROCESSOR_5KC, 64 },
592 /* MIPS I */
593 { "r3000", PROCESSOR_R3000, 1 },
594 { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
595 { "r3900", PROCESSOR_R3900, 1 },
597 /* MIPS II */
598 { "r6000", PROCESSOR_R6000, 2 },
600 /* MIPS III */
601 { "r4000", PROCESSOR_R4000, 3 },
602 { "vr4100", PROCESSOR_R4100, 3 },
603 { "vr4111", PROCESSOR_R4111, 3 },
604 { "vr4120", PROCESSOR_R4120, 3 },
605 { "vr4300", PROCESSOR_R4300, 3 },
606 { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
607 { "r4600", PROCESSOR_R4600, 3 },
608 { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
609 { "r4650", PROCESSOR_R4650, 3 },
611 /* MIPS IV */
612 { "r8000", PROCESSOR_R8000, 4 },
613 { "vr5000", PROCESSOR_R5000, 4 },
614 { "vr5400", PROCESSOR_R5400, 4 },
615 { "vr5500", PROCESSOR_R5500, 4 },
617 /* MIPS32 */
618 { "4kc", PROCESSOR_4KC, 32 },
619 { "4kp", PROCESSOR_4KC, 32 }, /* = 4kc */
621 /* MIPS32 Release 2 */
622 { "m4k", PROCESSOR_M4K, 33 },
624 /* MIPS64 */
625 { "5kc", PROCESSOR_5KC, 64 },
626 { "20kc", PROCESSOR_20KC, 64 },
627 { "sb1", PROCESSOR_SB1, 64 },
628 { "sr71000", PROCESSOR_SR71000, 64 },
630 /* End marker */
631 { 0, 0, 0 }
634 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT. */
635 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
636 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
637 #endif
639 /* Initialize the GCC target structure. */
640 #undef TARGET_ASM_ALIGNED_HI_OP
641 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
642 #undef TARGET_ASM_ALIGNED_SI_OP
643 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
644 #undef TARGET_ASM_INTEGER
645 #define TARGET_ASM_INTEGER mips_assemble_integer
647 #if TARGET_IRIX5 && !TARGET_IRIX6
648 #undef TARGET_ASM_UNALIGNED_HI_OP
649 #define TARGET_ASM_UNALIGNED_HI_OP "\t.align 0\n\t.half\t"
650 #undef TARGET_ASM_UNALIGNED_SI_OP
651 #define TARGET_ASM_UNALIGNED_SI_OP "\t.align 0\n\t.word\t"
652 #undef TARGET_ASM_UNALIGNED_DI_OP
653 #define TARGET_ASM_UNALIGNED_DI_OP "\t.align 0\n\t.dword\t"
654 #endif
656 #undef TARGET_ASM_FUNCTION_PROLOGUE
657 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
658 #undef TARGET_ASM_FUNCTION_EPILOGUE
659 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
660 #undef TARGET_ASM_SELECT_RTX_SECTION
661 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
663 #undef TARGET_SCHED_ADJUST_COST
664 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
665 #undef TARGET_SCHED_ISSUE_RATE
666 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
667 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
668 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE mips_use_dfa_pipeline_interface
670 #undef TARGET_ENCODE_SECTION_INFO
671 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
673 #undef TARGET_VALID_POINTER_MODE
674 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
675 #undef TARGET_RTX_COSTS
676 #define TARGET_RTX_COSTS mips_rtx_costs
678 struct gcc_target targetm = TARGET_INITIALIZER;
680 /* Return truth value of whether OP can be used as an operands
681 where a register or 16 bit unsigned integer is needed. */
684 uns_arith_operand (op, mode)
685 rtx op;
686 enum machine_mode mode;
688 if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
689 return 1;
691 return register_operand (op, mode);
694 /* Return truth value of whether OP can be used as an operands
695 where a 16 bit integer is needed */
698 arith_operand (op, mode)
699 rtx op;
700 enum machine_mode mode;
702 if (GET_CODE (op) == CONST_INT && SMALL_INT (op))
703 return 1;
705 /* On the mips16, a GP relative value is a signed 16 bit offset. */
706 if (TARGET_MIPS16 && GET_CODE (op) == CONST && mips16_gp_offset_p (op))
707 return 1;
709 return register_operand (op, mode);
712 /* Return truth value of whether OP can be used as an operand in a two
713 address arithmetic insn (such as set 123456,%o4) of mode MODE. */
716 arith32_operand (op, mode)
717 rtx op;
718 enum machine_mode mode;
720 if (GET_CODE (op) == CONST_INT)
721 return 1;
723 return register_operand (op, mode);
726 /* Return truth value of whether OP is an integer which fits in 16 bits. */
729 small_int (op, mode)
730 rtx op;
731 enum machine_mode mode ATTRIBUTE_UNUSED;
733 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
736 /* Return truth value of whether OP is a 32 bit integer which is too big to
737 be loaded with one instruction. */
740 large_int (op, mode)
741 rtx op;
742 enum machine_mode mode ATTRIBUTE_UNUSED;
744 HOST_WIDE_INT value;
746 if (GET_CODE (op) != CONST_INT)
747 return 0;
749 value = INTVAL (op);
751 /* ior reg,$r0,value */
752 if ((value & ~ ((HOST_WIDE_INT) 0x0000ffff)) == 0)
753 return 0;
755 /* subu reg,$r0,value */
756 if (((unsigned HOST_WIDE_INT) (value + 32768)) <= 32767)
757 return 0;
759 /* lui reg,value>>16 */
760 if ((value & 0x0000ffff) == 0)
761 return 0;
763 return 1;
766 /* Return truth value of whether OP is a register or the constant 0.
767 In mips16 mode, we only accept a register, since the mips16 does
768 not have $0. */
771 reg_or_0_operand (op, mode)
772 rtx op;
773 enum machine_mode mode;
775 switch (GET_CODE (op))
777 case CONST_INT:
778 if (TARGET_MIPS16)
779 return 0;
780 return INTVAL (op) == 0;
782 case CONST_DOUBLE:
783 if (TARGET_MIPS16)
784 return 0;
785 return op == CONST0_RTX (mode);
787 default:
788 return register_operand (op, mode);
792 /* Return truth value of whether OP is a register or the constant 0,
793 even in mips16 mode. */
796 true_reg_or_0_operand (op, mode)
797 rtx op;
798 enum machine_mode mode;
800 switch (GET_CODE (op))
802 case CONST_INT:
803 return INTVAL (op) == 0;
805 case CONST_DOUBLE:
806 return op == CONST0_RTX (mode);
808 default:
809 return register_operand (op, mode);
813 /* Return truth value if a CONST_DOUBLE is ok to be a legitimate constant. */
816 mips_const_double_ok (op, mode)
817 rtx op;
818 enum machine_mode mode;
820 if (GET_CODE (op) != CONST_DOUBLE)
821 return 0;
823 if (mode == VOIDmode)
824 return 1;
826 /* We've no zero register in mips16 mode. */
827 if (TARGET_MIPS16)
828 return 0;
830 if (mode != SFmode && mode != DFmode)
831 return 0;
833 if (op == CONST0_RTX (mode))
834 return 1;
836 return 0;
839 /* Accept the floating point constant 1 in the appropriate mode. */
842 const_float_1_operand (op, mode)
843 rtx op;
844 enum machine_mode mode;
846 REAL_VALUE_TYPE d;
848 if (GET_CODE (op) != CONST_DOUBLE
849 || mode != GET_MODE (op)
850 || (mode != DFmode && mode != SFmode))
851 return 0;
853 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
855 return REAL_VALUES_EQUAL (d, dconst1);
858 /* Return true if a memory load or store of REG plus OFFSET in MODE
859 can be represented in a single word on the mips16. */
861 static int
862 mips16_simple_memory_operand (reg, offset, mode)
863 rtx reg;
864 rtx offset;
865 enum machine_mode mode;
867 unsigned int size;
868 int off;
870 if (mode == BLKmode)
872 /* We can't tell, because we don't know how the value will
873 eventually be accessed. Returning 0 here does no great
874 harm; it just prevents some possible instruction scheduling. */
875 return 0;
878 size = GET_MODE_SIZE (mode);
880 if (INTVAL (offset) % size != 0)
881 return 0;
882 if (REGNO (reg) == STACK_POINTER_REGNUM && GET_MODE_SIZE (mode) == 4)
883 off = 0x100;
884 else
885 off = 0x20;
886 if (INTVAL (offset) >= 0 && INTVAL (offset) < (HOST_WIDE_INT)(off * size))
887 return 1;
888 return 0;
891 /* Return truth value if a memory operand fits in a single instruction
892 (ie, register + small offset). */
895 simple_memory_operand (op, mode)
896 rtx op;
897 enum machine_mode mode;
899 rtx addr, plus0, plus1;
901 /* Eliminate non-memory operations */
902 if (GET_CODE (op) != MEM)
903 return 0;
905 /* dword operations really put out 2 instructions, so eliminate them. */
906 /* ??? This isn't strictly correct. It is OK to accept multiword modes
907 here, since the length attributes are being set correctly, but only
908 if the address is offsettable. LO_SUM is not offsettable. */
909 if (GET_MODE_SIZE (GET_MODE (op)) > (unsigned) UNITS_PER_WORD)
910 return 0;
912 /* Decode the address now. */
913 addr = XEXP (op, 0);
914 switch (GET_CODE (addr))
916 case REG:
917 case LO_SUM:
918 return 1;
920 case CONST_INT:
921 if (TARGET_MIPS16)
922 return 0;
923 return SMALL_INT (addr);
925 case PLUS:
926 plus0 = XEXP (addr, 0);
927 plus1 = XEXP (addr, 1);
928 if (GET_CODE (plus0) == REG
929 && GET_CODE (plus1) == CONST_INT && SMALL_INT (plus1)
930 && (! TARGET_MIPS16
931 || mips16_simple_memory_operand (plus0, plus1, mode)))
932 return 1;
934 else if (GET_CODE (plus1) == REG
935 && GET_CODE (plus0) == CONST_INT && SMALL_INT (plus0)
936 && (! TARGET_MIPS16
937 || mips16_simple_memory_operand (plus1, plus0, mode)))
938 return 1;
940 else
941 return 0;
943 #if 0
944 /* We used to allow small symbol refs here (ie, stuff in .sdata
945 or .sbss), but this causes some bugs in G++. Also, it won't
946 interfere if the MIPS linker rewrites the store instruction
947 because the function is PIC. */
949 case LABEL_REF: /* never gp relative */
950 break;
952 case CONST:
953 /* If -G 0, we can never have a GP relative memory operation.
954 Also, save some time if not optimizing. */
955 if (!TARGET_GP_OPT)
956 return 0;
959 rtx offset = const0_rtx;
960 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
961 if (GET_CODE (op) != SYMBOL_REF)
962 return 0;
964 /* let's be paranoid.... */
965 if (! SMALL_INT (offset))
966 return 0;
969 /* fall through */
971 case SYMBOL_REF:
972 return SYMBOL_REF_FLAG (addr);
973 #endif
975 /* This SYMBOL_REF case is for the mips16. If the above case is
976 reenabled, this one should be merged in. */
977 case SYMBOL_REF:
978 /* References to the constant pool on the mips16 use a small
979 offset if the function is small. The only time we care about
980 getting this right is during delayed branch scheduling, so
981 don't need to check until then. The machine_dependent_reorg
982 function will set the total length of the instructions used
983 in the function (cfun->machine->insns_len). If that is small
984 enough, we know for sure that this is a small offset. It
985 would be better if we could take into account the location of
986 the instruction within the function, but we can't, because we
987 don't know where we are. */
988 if (TARGET_MIPS16
989 && CONSTANT_POOL_ADDRESS_P (addr)
990 && cfun->machine->insns_len > 0)
992 long size;
994 size = cfun->machine->insns_len + get_pool_size ();
995 if (GET_MODE_SIZE (mode) == 4)
996 return size < 4 * 0x100;
997 else if (GET_MODE_SIZE (mode) == 8)
998 return size < 8 * 0x20;
999 else
1000 return 0;
1003 return 0;
1005 default:
1006 break;
1009 return 0;
1012 /* Return nonzero for a memory address that can be used to load or store
1013 a doubleword. */
1016 double_memory_operand (op, mode)
1017 rtx op;
1018 enum machine_mode mode;
1020 if (GET_CODE (op) != MEM
1021 || ! memory_operand (op, mode))
1023 /* During reload, we accept a pseudo register if it has an
1024 appropriate memory address. If we don't do this, we will
1025 wind up reloading into a register, and then reloading that
1026 register from memory, when we could just reload directly from
1027 memory. */
1028 if (reload_in_progress
1029 && GET_CODE (op) == REG
1030 && REGNO (op) >= FIRST_PSEUDO_REGISTER
1031 && reg_renumber[REGNO (op)] < 0
1032 && reg_equiv_mem[REGNO (op)] != 0
1033 && double_memory_operand (reg_equiv_mem[REGNO (op)], mode))
1034 return 1;
1036 /* All reloaded addresses are valid in TARGET_64BIT mode. This is
1037 the same test performed for 'm' in find_reloads. */
1039 if (reload_in_progress
1040 && TARGET_64BIT
1041 && (GET_CODE (op) == MEM
1042 || (GET_CODE (op) == REG
1043 && REGNO (op) >= FIRST_PSEUDO_REGISTER
1044 && reg_renumber[REGNO (op)] < 0)))
1045 return 1;
1047 if (reload_in_progress
1048 && TARGET_MIPS16
1049 && GET_CODE (op) == MEM)
1051 rtx addr;
1053 addr = XEXP (op, 0);
1055 /* During reload on the mips16, we accept a large offset
1056 from the frame pointer or the stack pointer. This large
1057 address will get reloaded anyhow. */
1058 if (GET_CODE (addr) == PLUS
1059 && GET_CODE (XEXP (addr, 0)) == REG
1060 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1061 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
1062 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
1063 && ! SMALL_INT (XEXP (addr, 1)))
1064 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
1065 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
1066 return 1;
1068 /* Similarly, we accept a case where the memory address is
1069 itself on the stack, and will be reloaded. */
1070 if (GET_CODE (addr) == MEM)
1072 rtx maddr;
1074 maddr = XEXP (addr, 0);
1075 if (GET_CODE (maddr) == PLUS
1076 && GET_CODE (XEXP (maddr, 0)) == REG
1077 && (REGNO (XEXP (maddr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1078 || REGNO (XEXP (maddr, 0)) == STACK_POINTER_REGNUM)
1079 && ((GET_CODE (XEXP (maddr, 1)) == CONST_INT
1080 && ! SMALL_INT (XEXP (maddr, 1)))
1081 || (GET_CODE (XEXP (maddr, 1)) == SYMBOL_REF
1082 && CONSTANT_POOL_ADDRESS_P (XEXP (maddr, 1)))))
1083 return 1;
1086 /* We also accept the same case when we have a 16 bit signed
1087 offset mixed in as well. The large address will get
1088 reloaded, and the 16 bit offset will be OK. */
1089 if (GET_CODE (addr) == PLUS
1090 && GET_CODE (XEXP (addr, 0)) == MEM
1091 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1092 && SMALL_INT (XEXP (addr, 1)))
1094 addr = XEXP (XEXP (addr, 0), 0);
1095 if (GET_CODE (addr) == PLUS
1096 && GET_CODE (XEXP (addr, 0)) == REG
1097 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
1098 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
1099 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
1100 && ! SMALL_INT (XEXP (addr, 1)))
1101 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
1102 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
1103 return 1;
1107 return 0;
1110 if (TARGET_64BIT)
1112 /* In this case we can use an instruction like sd. */
1113 return 1;
1116 /* Make sure that 4 added to the address is a valid memory address.
1117 This essentially just checks for overflow in an added constant. */
1119 if (CONSTANT_ADDRESS_P (XEXP (op, 0)))
1120 return 1;
1122 op = adjust_address_nv (op, GET_MODE_CLASS (mode) == MODE_INT
1123 ? SImode : SFmode, 4);
1124 return memory_address_p (GET_MODE (op), XEXP (op, 0));
1127 /* Return nonzero if the code of this rtx pattern is EQ or NE. */
1130 equality_op (op, mode)
1131 rtx op;
1132 enum machine_mode mode;
1134 if (mode != GET_MODE (op))
1135 return 0;
1137 return GET_CODE (op) == EQ || GET_CODE (op) == NE;
1140 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
1143 cmp_op (op, mode)
1144 rtx op;
1145 enum machine_mode mode;
1147 if (mode != GET_MODE (op))
1148 return 0;
1150 return GET_RTX_CLASS (GET_CODE (op)) == '<';
1153 /* Return nonzero if the code is a relational operation suitable for a
1154 conditional trap instructuion (only EQ, NE, LT, LTU, GE, GEU).
1155 We need this in the insn that expands `trap_if' in order to prevent
1156 combine from erroneously altering the condition. */
1159 trap_cmp_op (op, mode)
1160 rtx op;
1161 enum machine_mode mode;
1163 if (mode != GET_MODE (op))
1164 return 0;
1166 switch (GET_CODE (op))
1168 case EQ:
1169 case NE:
1170 case LT:
1171 case LTU:
1172 case GE:
1173 case GEU:
1174 return 1;
1176 default:
1177 return 0;
1181 /* Return nonzero if the operand is either the PC or a label_ref. */
1184 pc_or_label_operand (op, mode)
1185 rtx op;
1186 enum machine_mode mode ATTRIBUTE_UNUSED;
1188 if (op == pc_rtx)
1189 return 1;
1191 if (GET_CODE (op) == LABEL_REF)
1192 return 1;
1194 return 0;
1197 /* Test for a valid operand for a call instruction.
1198 Don't allow the arg pointer register or virtual regs
1199 since they may change into reg + const, which the patterns
1200 can't handle yet. */
1203 call_insn_operand (op, mode)
1204 rtx op;
1205 enum machine_mode mode ATTRIBUTE_UNUSED;
1207 return (CONSTANT_ADDRESS_P (op)
1208 || (GET_CODE (op) == REG && op != arg_pointer_rtx
1209 && ! (REGNO (op) >= FIRST_PSEUDO_REGISTER
1210 && REGNO (op) <= LAST_VIRTUAL_REGISTER)));
1213 /* Return nonzero if OPERAND is valid as a source operand for a move
1214 instruction. */
1217 move_operand (op, mode)
1218 rtx op;
1219 enum machine_mode mode;
1221 /* Accept any general operand after reload has started; doing so
1222 avoids losing if reload does an in-place replacement of a register
1223 with a SYMBOL_REF or CONST. */
1224 return (general_operand (op, mode)
1225 && (! (mips_split_addresses && mips_check_split (op, mode))
1226 || reload_in_progress || reload_completed)
1227 && ! (TARGET_MIPS16
1228 && GET_CODE (op) == SYMBOL_REF
1229 && ! mips16_constant (op, mode, 1, 0)));
1232 /* Return nonzero if OPERAND is valid as a source operand for movdi.
1233 This accepts not only general_operand, but also sign extended
1234 move_operands. Note that we need to accept sign extended constants
1235 in case a sign extended register which is used in an expression,
1236 and is equivalent to a constant, is spilled. We need to accept
1237 sign-extended memory in order to reload registers from stack slots,
1238 and so that we generate efficient code for extendsidi2. */
1241 movdi_operand (op, mode)
1242 rtx op;
1243 enum machine_mode mode;
1245 if (TARGET_64BIT
1246 && mode == DImode
1247 && GET_CODE (op) == SIGN_EXTEND
1248 && GET_MODE (op) == DImode
1249 && move_operand (XEXP (op, 0), SImode))
1250 return 1;
1252 return (general_operand (op, mode)
1253 && ! (TARGET_MIPS16
1254 && GET_CODE (op) == SYMBOL_REF
1255 && ! mips16_constant (op, mode, 1, 0)));
1258 /* Like register_operand, but when in 64 bit mode also accept a sign
1259 extend of a 32 bit register, since the value is known to be already
1260 sign extended. */
1263 se_register_operand (op, mode)
1264 rtx op;
1265 enum machine_mode mode;
1267 if (TARGET_64BIT
1268 && mode == DImode
1269 && GET_CODE (op) == SIGN_EXTEND
1270 && GET_MODE (op) == DImode
1271 && GET_MODE (XEXP (op, 0)) == SImode
1272 && register_operand (XEXP (op, 0), SImode))
1273 return 1;
1275 return register_operand (op, mode);
1278 /* Like reg_or_0_operand, but when in 64 bit mode also accept a sign
1279 extend of a 32 bit register, since the value is known to be already
1280 sign extended. */
1283 se_reg_or_0_operand (op, mode)
1284 rtx op;
1285 enum machine_mode mode;
1287 if (TARGET_64BIT
1288 && mode == DImode
1289 && GET_CODE (op) == SIGN_EXTEND
1290 && GET_MODE (op) == DImode
1291 && GET_MODE (XEXP (op, 0)) == SImode
1292 && register_operand (XEXP (op, 0), SImode))
1293 return 1;
1295 return reg_or_0_operand (op, mode);
1298 /* Like uns_arith_operand, but when in 64 bit mode also accept a sign
1299 extend of a 32 bit register, since the value is known to be already
1300 sign extended. */
1303 se_uns_arith_operand (op, mode)
1304 rtx op;
1305 enum machine_mode mode;
1307 if (TARGET_64BIT
1308 && mode == DImode
1309 && GET_CODE (op) == SIGN_EXTEND
1310 && GET_MODE (op) == DImode
1311 && GET_MODE (XEXP (op, 0)) == SImode
1312 && register_operand (XEXP (op, 0), SImode))
1313 return 1;
1315 return uns_arith_operand (op, mode);
1318 /* Like arith_operand, but when in 64 bit mode also accept a sign
1319 extend of a 32 bit register, since the value is known to be already
1320 sign extended. */
1323 se_arith_operand (op, mode)
1324 rtx op;
1325 enum machine_mode mode;
1327 if (TARGET_64BIT
1328 && mode == DImode
1329 && GET_CODE (op) == SIGN_EXTEND
1330 && GET_MODE (op) == DImode
1331 && GET_MODE (XEXP (op, 0)) == SImode
1332 && register_operand (XEXP (op, 0), SImode))
1333 return 1;
1335 return arith_operand (op, mode);
1338 /* Like nonmemory_operand, but when in 64 bit mode also accept a sign
1339 extend of a 32 bit register, since the value is known to be already
1340 sign extended. */
1343 se_nonmemory_operand (op, mode)
1344 rtx op;
1345 enum machine_mode mode;
1347 if (TARGET_64BIT
1348 && mode == DImode
1349 && GET_CODE (op) == SIGN_EXTEND
1350 && GET_MODE (op) == DImode
1351 && GET_MODE (XEXP (op, 0)) == SImode
1352 && register_operand (XEXP (op, 0), SImode))
1353 return 1;
1355 return nonmemory_operand (op, mode);
1358 /* Accept any operand that can appear in a mips16 constant table
1359 instruction. We can't use any of the standard operand functions
1360 because for these instructions we accept values that are not
1361 accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs. */
1364 consttable_operand (op, mode)
1365 rtx op;
1366 enum machine_mode mode ATTRIBUTE_UNUSED;
1368 return CONSTANT_P (op);
1371 /* Coprocessor operand; return true if rtx is a REG and refers to a
1372 coprocessor. */
1375 coprocessor_operand (op, mode)
1376 rtx op;
1377 enum machine_mode mode ATTRIBUTE_UNUSED;
1379 return (GET_CODE (op) == REG
1380 && COP0_REG_FIRST <= REGNO (op)
1381 && REGNO (op) <= COP3_REG_LAST);
1385 coprocessor2_operand (op, mode)
1386 rtx op;
1387 enum machine_mode mode ATTRIBUTE_UNUSED;
1389 return (GET_CODE (op) == REG
1390 && COP2_REG_FIRST <= REGNO (op)
1391 && REGNO (op) <= COP2_REG_LAST);
1394 /* Returns 1 if OP is a symbolic operand, i.e. a symbol_ref or a label_ref,
1395 possibly with an offset. */
1398 symbolic_operand (op, mode)
1399 register rtx op;
1400 enum machine_mode mode;
1402 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && mode != GET_MODE (op))
1403 return 0;
1404 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
1405 return 1;
1406 if (GET_CODE (op) == CONST
1407 && GET_CODE (XEXP (op,0)) == PLUS
1408 && GET_CODE (XEXP (XEXP (op,0), 0)) == SYMBOL_REF
1409 && GET_CODE (XEXP (XEXP (op,0), 1)) == CONST_INT)
1410 return 1;
1411 return 0;
1414 /* Return nonzero if we split the address into high and low parts. */
1416 /* ??? We should also handle reg+array somewhere. We get four
1417 instructions currently, lui %hi/addui %lo/addui reg/lw. Better is
1418 lui %hi/addui reg/lw %lo. Fixing GO_IF_LEGITIMATE_ADDRESS to accept
1419 (plus (reg) (symbol_ref)) doesn't work because the SYMBOL_REF is broken
1420 out of the address, then we have 4 instructions to combine. Perhaps
1421 add a 3->2 define_split for combine. */
1423 /* ??? We could also split a CONST_INT here if it is a large_int().
1424 However, it doesn't seem to be very useful to have %hi(constant).
1425 We would be better off by doing the masking ourselves and then putting
1426 the explicit high part of the constant in the RTL. This will give better
1427 optimization. Also, %hi(constant) needs assembler changes to work.
1428 There is already a define_split that does this. */
1431 mips_check_split (address, mode)
1432 rtx address;
1433 enum machine_mode mode;
1435 /* ??? This is the same check used in simple_memory_operand.
1436 We use it here because LO_SUM is not offsettable. */
1437 if (GET_MODE_SIZE (mode) > (unsigned) UNITS_PER_WORD)
1438 return 0;
1440 if ((GET_CODE (address) == SYMBOL_REF && ! SYMBOL_REF_FLAG (address))
1441 || (GET_CODE (address) == CONST
1442 && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF
1443 && ! SYMBOL_REF_FLAG (XEXP (XEXP (address, 0), 0)))
1444 || GET_CODE (address) == LABEL_REF)
1445 return 1;
1447 return 0;
1450 /* This function is used to implement REG_MODE_OK_FOR_BASE_P. */
1453 mips_reg_mode_ok_for_base_p (reg, mode, strict)
1454 rtx reg;
1455 enum machine_mode mode;
1456 int strict;
1458 return (strict
1459 ? REGNO_MODE_OK_FOR_BASE_P (REGNO (reg), mode)
1460 : GP_REG_OR_PSEUDO_NONSTRICT_P (REGNO (reg), mode));
1463 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS. It
1464 returns a nonzero value if XINSN is a legitimate address for a
1465 memory operand of the indicated MODE. STRICT is nonzero if this
1466 function is called during reload. */
1469 mips_legitimate_address_p (mode, xinsn, strict)
1470 enum machine_mode mode;
1471 rtx xinsn;
1472 int strict;
1474 if (TARGET_DEBUG_B_MODE)
1476 GO_PRINTF2 ("\n========== GO_IF_LEGITIMATE_ADDRESS, %sstrict\n",
1477 strict ? "" : "not ");
1478 GO_DEBUG_RTX (xinsn);
1481 /* Check for constant before stripping off SUBREG, so that we don't
1482 accept (subreg (const_int)) which will fail to reload. */
1483 if (CONSTANT_ADDRESS_P (xinsn)
1484 && ! (mips_split_addresses && mips_check_split (xinsn, mode))
1485 && (! TARGET_MIPS16 || mips16_constant (xinsn, mode, 1, 0)))
1486 return 1;
1488 while (GET_CODE (xinsn) == SUBREG)
1489 xinsn = SUBREG_REG (xinsn);
1491 /* The mips16 can only use the stack pointer as a base register when
1492 loading SImode or DImode values. */
1493 if (GET_CODE (xinsn) == REG
1494 && mips_reg_mode_ok_for_base_p (xinsn, mode, strict))
1495 return 1;
1497 if (GET_CODE (xinsn) == LO_SUM && mips_split_addresses)
1499 register rtx xlow0 = XEXP (xinsn, 0);
1500 register rtx xlow1 = XEXP (xinsn, 1);
1502 while (GET_CODE (xlow0) == SUBREG)
1503 xlow0 = SUBREG_REG (xlow0);
1504 if (GET_CODE (xlow0) == REG
1505 && mips_reg_mode_ok_for_base_p (xlow0, mode, strict)
1506 && mips_check_split (xlow1, mode))
1507 return 1;
1510 if (GET_CODE (xinsn) == PLUS)
1512 register rtx xplus0 = XEXP (xinsn, 0);
1513 register rtx xplus1 = XEXP (xinsn, 1);
1514 register enum rtx_code code0;
1515 register enum rtx_code code1;
1517 while (GET_CODE (xplus0) == SUBREG)
1518 xplus0 = SUBREG_REG (xplus0);
1519 code0 = GET_CODE (xplus0);
1521 while (GET_CODE (xplus1) == SUBREG)
1522 xplus1 = SUBREG_REG (xplus1);
1523 code1 = GET_CODE (xplus1);
1525 /* The mips16 can only use the stack pointer as a base register
1526 when loading SImode or DImode values. */
1527 if (code0 == REG
1528 && mips_reg_mode_ok_for_base_p (xplus0, mode, strict))
1530 if (code1 == CONST_INT && SMALL_INT (xplus1))
1531 return 1;
1533 /* On the mips16, we represent GP relative offsets in RTL.
1534 These are 16 bit signed values, and can serve as register
1535 offsets. */
1536 if (TARGET_MIPS16
1537 && mips16_gp_offset_p (xplus1))
1538 return 1;
1540 /* For some code sequences, you actually get better code by
1541 pretending that the MIPS supports an address mode of a
1542 constant address + a register, even though the real
1543 machine doesn't support it. This is because the
1544 assembler can use $r1 to load just the high 16 bits, add
1545 in the register, and fold the low 16 bits into the memory
1546 reference, whereas the compiler generates a 4 instruction
1547 sequence. On the other hand, CSE is not as effective.
1548 It would be a win to generate the lui directly, but the
1549 MIPS assembler does not have syntax to generate the
1550 appropriate relocation. */
1552 /* Also accept CONST_INT addresses here, so no else. */
1553 /* Reject combining an embedded PIC text segment reference
1554 with a register. That requires an additional
1555 instruction. */
1556 /* ??? Reject combining an address with a register for the MIPS
1557 64 bit ABI, because the SGI assembler can not handle this. */
1558 if (!TARGET_DEBUG_A_MODE
1559 && (mips_abi == ABI_32
1560 || mips_abi == ABI_O64
1561 || mips_abi == ABI_EABI)
1562 && CONSTANT_ADDRESS_P (xplus1)
1563 && ! mips_split_addresses
1564 && (!TARGET_EMBEDDED_PIC
1565 || code1 != CONST
1566 || GET_CODE (XEXP (xplus1, 0)) != MINUS)
1567 /* When assembling for machines with 64 bit registers,
1568 the assembler will sign-extend the constant "foo"
1569 in "la x, foo(x)" yielding the wrong result for:
1570 (set (blah:DI) (plus x y)). */
1571 && (!TARGET_64BIT
1572 || (code1 == CONST_INT
1573 && trunc_int_for_mode (INTVAL (xplus1),
1574 SImode) == INTVAL (xplus1)))
1575 && !TARGET_MIPS16)
1576 return 1;
1580 if (TARGET_DEBUG_B_MODE)
1581 GO_PRINTF ("Not a legitimate address\n");
1583 /* The address was not legitimate. */
1584 return 0;
1588 /* We need a lot of little routines to check constant values on the
1589 mips16. These are used to figure out how long the instruction will
1590 be. It would be much better to do this using constraints, but
1591 there aren't nearly enough letters available. */
1593 static int
1594 m16_check_op (op, low, high, mask)
1595 rtx op;
1596 int low;
1597 int high;
1598 int mask;
1600 return (GET_CODE (op) == CONST_INT
1601 && INTVAL (op) >= low
1602 && INTVAL (op) <= high
1603 && (INTVAL (op) & mask) == 0);
1607 m16_uimm3_b (op, mode)
1608 rtx op;
1609 enum machine_mode mode ATTRIBUTE_UNUSED;
1611 return m16_check_op (op, 0x1, 0x8, 0);
1615 m16_simm4_1 (op, mode)
1616 rtx op;
1617 enum machine_mode mode ATTRIBUTE_UNUSED;
1619 return m16_check_op (op, - 0x8, 0x7, 0);
1623 m16_nsimm4_1 (op, mode)
1624 rtx op;
1625 enum machine_mode mode ATTRIBUTE_UNUSED;
1627 return m16_check_op (op, - 0x7, 0x8, 0);
1631 m16_simm5_1 (op, mode)
1632 rtx op;
1633 enum machine_mode mode ATTRIBUTE_UNUSED;
1635 return m16_check_op (op, - 0x10, 0xf, 0);
1639 m16_nsimm5_1 (op, mode)
1640 rtx op;
1641 enum machine_mode mode ATTRIBUTE_UNUSED;
1643 return m16_check_op (op, - 0xf, 0x10, 0);
1647 m16_uimm5_4 (op, mode)
1648 rtx op;
1649 enum machine_mode mode ATTRIBUTE_UNUSED;
1651 return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
1655 m16_nuimm5_4 (op, mode)
1656 rtx op;
1657 enum machine_mode mode ATTRIBUTE_UNUSED;
1659 return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
1663 m16_simm8_1 (op, mode)
1664 rtx op;
1665 enum machine_mode mode ATTRIBUTE_UNUSED;
1667 return m16_check_op (op, - 0x80, 0x7f, 0);
1671 m16_nsimm8_1 (op, mode)
1672 rtx op;
1673 enum machine_mode mode ATTRIBUTE_UNUSED;
1675 return m16_check_op (op, - 0x7f, 0x80, 0);
1679 m16_uimm8_1 (op, mode)
1680 rtx op;
1681 enum machine_mode mode ATTRIBUTE_UNUSED;
1683 return m16_check_op (op, 0x0, 0xff, 0);
1687 m16_nuimm8_1 (op, mode)
1688 rtx op;
1689 enum machine_mode mode ATTRIBUTE_UNUSED;
1691 return m16_check_op (op, - 0xff, 0x0, 0);
1695 m16_uimm8_m1_1 (op, mode)
1696 rtx op;
1697 enum machine_mode mode ATTRIBUTE_UNUSED;
1699 return m16_check_op (op, - 0x1, 0xfe, 0);
1703 m16_uimm8_4 (op, mode)
1704 rtx op;
1705 enum machine_mode mode ATTRIBUTE_UNUSED;
1707 return m16_check_op (op, 0x0, 0xff << 2, 3);
1711 m16_nuimm8_4 (op, mode)
1712 rtx op;
1713 enum machine_mode mode ATTRIBUTE_UNUSED;
1715 return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
1719 m16_simm8_8 (op, mode)
1720 rtx op;
1721 enum machine_mode mode ATTRIBUTE_UNUSED;
1723 return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
1727 m16_nsimm8_8 (op, mode)
1728 rtx op;
1729 enum machine_mode mode ATTRIBUTE_UNUSED;
1731 return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
1734 /* References to the string table on the mips16 only use a small
1735 offset if the function is small. See the comment in the SYMBOL_REF
1736 case in simple_memory_operand. We can't check for LABEL_REF here,
1737 because the offset is always large if the label is before the
1738 referencing instruction. */
1741 m16_usym8_4 (op, mode)
1742 rtx op;
1743 enum machine_mode mode ATTRIBUTE_UNUSED;
1745 if (GET_CODE (op) == SYMBOL_REF
1746 && SYMBOL_REF_FLAG (op)
1747 && cfun->machine->insns_len > 0
1748 && XSTR (op, 0)[0] == '*'
1749 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1750 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1751 && (cfun->machine->insns_len + get_pool_size () + mips_string_length
1752 < 4 * 0x100))
1754 struct string_constant *l;
1756 /* Make sure this symbol is on thelist of string constants to be
1757 output for this function. It is possible that it has already
1758 been output, in which case this requires a large offset. */
1759 for (l = string_constants; l != NULL; l = l->next)
1760 if (strcmp (l->label, XSTR (op, 0)) == 0)
1761 return 1;
1764 return 0;
1768 m16_usym5_4 (op, mode)
1769 rtx op;
1770 enum machine_mode mode ATTRIBUTE_UNUSED;
1772 if (GET_CODE (op) == SYMBOL_REF
1773 && SYMBOL_REF_FLAG (op)
1774 && cfun->machine->insns_len > 0
1775 && XSTR (op, 0)[0] == '*'
1776 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1777 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1778 && (cfun->machine->insns_len + get_pool_size () + mips_string_length
1779 < 4 * 0x20))
1781 struct string_constant *l;
1783 /* Make sure this symbol is on thelist of string constants to be
1784 output for this function. It is possible that it has already
1785 been output, in which case this requires a large offset. */
1786 for (l = string_constants; l != NULL; l = l->next)
1787 if (strcmp (l->label, XSTR (op, 0)) == 0)
1788 return 1;
1791 return 0;
1794 /* Returns an operand string for the given instruction's delay slot,
1795 after updating filled delay slot statistics.
1797 We assume that operands[0] is the target register that is set.
1799 In order to check the next insn, most of this functionality is moved
1800 to FINAL_PRESCAN_INSN, and we just set the global variables that
1801 it needs. */
1803 /* ??? This function no longer does anything useful, because final_prescan_insn
1804 now will never emit a nop. */
1806 const char *
1807 mips_fill_delay_slot (ret, type, operands, cur_insn)
1808 const char *ret; /* normal string to return */
1809 enum delay_type type; /* type of delay */
1810 rtx operands[]; /* operands to use */
1811 rtx cur_insn; /* current insn */
1813 register rtx set_reg;
1814 register enum machine_mode mode;
1815 register rtx next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL_RTX;
1816 register int num_nops;
1818 if (type == DELAY_LOAD || type == DELAY_FCMP)
1819 num_nops = 1;
1821 else if (type == DELAY_HILO)
1822 num_nops = 2;
1824 else
1825 num_nops = 0;
1827 /* Make sure that we don't put nop's after labels. */
1828 next_insn = NEXT_INSN (cur_insn);
1829 while (next_insn != 0 && GET_CODE (next_insn) == NOTE)
1830 next_insn = NEXT_INSN (next_insn);
1832 dslots_load_total += num_nops;
1833 if (TARGET_DEBUG_F_MODE
1834 || !optimize
1835 || type == DELAY_NONE
1836 || operands == 0
1837 || cur_insn == 0
1838 || next_insn == 0
1839 || GET_CODE (next_insn) == CODE_LABEL
1840 || (set_reg = operands[0]) == 0)
1842 dslots_number_nops = 0;
1843 mips_load_reg = 0;
1844 mips_load_reg2 = 0;
1845 mips_load_reg3 = 0;
1846 mips_load_reg4 = 0;
1847 return ret;
1850 set_reg = operands[0];
1851 if (set_reg == 0)
1852 return ret;
1854 while (GET_CODE (set_reg) == SUBREG)
1855 set_reg = SUBREG_REG (set_reg);
1857 mode = GET_MODE (set_reg);
1858 dslots_number_nops = num_nops;
1859 mips_load_reg = set_reg;
1860 if (GET_MODE_SIZE (mode)
1861 > (unsigned) (FP_REG_P (REGNO (set_reg)) ? UNITS_PER_FPREG : UNITS_PER_WORD))
1862 mips_load_reg2 = gen_rtx_REG (SImode, REGNO (set_reg) + 1);
1863 else
1864 mips_load_reg2 = 0;
1866 if (type == DELAY_HILO)
1868 mips_load_reg3 = gen_rtx_REG (SImode, MD_REG_FIRST);
1869 mips_load_reg4 = gen_rtx_REG (SImode, MD_REG_FIRST+1);
1871 else
1873 mips_load_reg3 = 0;
1874 mips_load_reg4 = 0;
1877 return ret;
1881 /* Determine whether a memory reference takes one (based off of the GP
1882 pointer), two (normal), or three (label + reg) instructions, and bump the
1883 appropriate counter for -mstats. */
1885 void
1886 mips_count_memory_refs (op, num)
1887 rtx op;
1888 int num;
1890 int additional = 0;
1891 int n_words = 0;
1892 rtx addr, plus0, plus1;
1893 enum rtx_code code0, code1;
1894 int looping;
1896 if (TARGET_DEBUG_B_MODE)
1898 fprintf (stderr, "\n========== mips_count_memory_refs:\n");
1899 debug_rtx (op);
1902 /* Skip MEM if passed, otherwise handle movsi of address. */
1903 addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
1905 /* Loop, going through the address RTL. */
1908 looping = FALSE;
1909 switch (GET_CODE (addr))
1911 case REG:
1912 case CONST_INT:
1913 case LO_SUM:
1914 break;
1916 case PLUS:
1917 plus0 = XEXP (addr, 0);
1918 plus1 = XEXP (addr, 1);
1919 code0 = GET_CODE (plus0);
1920 code1 = GET_CODE (plus1);
1922 if (code0 == REG)
1924 additional++;
1925 addr = plus1;
1926 looping = 1;
1927 continue;
1930 if (code0 == CONST_INT)
1932 addr = plus1;
1933 looping = 1;
1934 continue;
1937 if (code1 == REG)
1939 additional++;
1940 addr = plus0;
1941 looping = 1;
1942 continue;
1945 if (code1 == CONST_INT)
1947 addr = plus0;
1948 looping = 1;
1949 continue;
1952 if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
1954 addr = plus0;
1955 looping = 1;
1956 continue;
1959 if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
1961 addr = plus1;
1962 looping = 1;
1963 continue;
1966 break;
1968 case LABEL_REF:
1969 n_words = 2; /* always 2 words */
1970 break;
1972 case CONST:
1973 addr = XEXP (addr, 0);
1974 looping = 1;
1975 continue;
1977 case SYMBOL_REF:
1978 n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
1979 break;
1981 default:
1982 break;
1985 while (looping);
1987 if (n_words == 0)
1988 return;
1990 n_words += additional;
1991 if (n_words > 3)
1992 n_words = 3;
1994 num_refs[n_words-1] += num;
1998 /* Return a pseudo that points to the address of the current function.
1999 The first time it is called for a function, an initializer for the
2000 pseudo is emitted in the beginning of the function. */
2003 embedded_pic_fnaddr_reg ()
2005 if (cfun->machine->embedded_pic_fnaddr_rtx == NULL)
2007 rtx seq;
2009 cfun->machine->embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
2011 /* Output code at function start to initialize the pseudo-reg. */
2012 /* ??? We used to do this in FINALIZE_PIC, but that does not work for
2013 inline functions, because it is called after RTL for the function
2014 has been copied. The pseudo-reg in embedded_pic_fnaddr_rtx however
2015 does not get copied, and ends up not matching the rest of the RTL.
2016 This solution works, but means that we get unnecessary code to
2017 initialize this value every time a function is inlined into another
2018 function. */
2019 start_sequence ();
2020 emit_insn (gen_get_fnaddr (cfun->machine->embedded_pic_fnaddr_rtx,
2021 XEXP (DECL_RTL (current_function_decl), 0)));
2022 seq = get_insns ();
2023 end_sequence ();
2024 push_topmost_sequence ();
2025 emit_insn_after (seq, get_insns ());
2026 pop_topmost_sequence ();
2029 return cfun->machine->embedded_pic_fnaddr_rtx;
2032 /* Return RTL for the offset from the current function to the argument.
2033 X is the symbol whose offset from the current function we want. */
2036 embedded_pic_offset (x)
2037 rtx x;
2039 /* Make sure it is emitted. */
2040 embedded_pic_fnaddr_reg ();
2042 return
2043 gen_rtx_CONST (Pmode,
2044 gen_rtx_MINUS (Pmode, x,
2045 XEXP (DECL_RTL (current_function_decl), 0)));
2048 /* Return the appropriate instructions to move one operand to another. */
2050 const char *
2051 mips_move_1word (operands, insn, unsignedp)
2052 rtx operands[];
2053 rtx insn;
2054 int unsignedp;
2056 const char *ret = 0;
2057 rtx op0 = operands[0];
2058 rtx op1 = operands[1];
2059 enum rtx_code code0 = GET_CODE (op0);
2060 enum rtx_code code1 = GET_CODE (op1);
2061 enum machine_mode mode = GET_MODE (op0);
2062 int subreg_offset0 = 0;
2063 int subreg_offset1 = 0;
2064 enum delay_type delay = DELAY_NONE;
2066 while (code0 == SUBREG)
2068 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2069 GET_MODE (SUBREG_REG (op0)),
2070 SUBREG_BYTE (op0),
2071 GET_MODE (op0));
2072 op0 = SUBREG_REG (op0);
2073 code0 = GET_CODE (op0);
2076 while (code1 == SUBREG)
2078 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2079 GET_MODE (SUBREG_REG (op1)),
2080 SUBREG_BYTE (op1),
2081 GET_MODE (op1));
2082 op1 = SUBREG_REG (op1);
2083 code1 = GET_CODE (op1);
2086 /* For our purposes, a condition code mode is the same as SImode. */
2087 if (mode == CCmode)
2088 mode = SImode;
2090 if (code0 == REG)
2092 int regno0 = REGNO (op0) + subreg_offset0;
2094 if (code1 == REG)
2096 int regno1 = REGNO (op1) + subreg_offset1;
2098 /* Just in case, don't do anything for assigning a register
2099 to itself, unless we are filling a delay slot. */
2100 if (regno0 == regno1 && set_nomacro == 0)
2101 ret = "";
2103 else if (GP_REG_P (regno0))
2105 if (GP_REG_P (regno1))
2106 ret = "move\t%0,%1";
2108 else if (MD_REG_P (regno1))
2110 delay = DELAY_HILO;
2111 if (regno1 != HILO_REGNUM)
2112 ret = "mf%1\t%0";
2113 else
2114 ret = "mflo\t%0";
2117 else if (ST_REG_P (regno1) && ISA_HAS_8CC)
2118 ret = "li\t%0,1\n\tmovf\t%0,%.,%1";
2120 else
2122 delay = DELAY_LOAD;
2123 if (FP_REG_P (regno1))
2124 ret = "mfc1\t%0,%1";
2125 else if (ALL_COP_REG_P (regno1))
2127 static char retval[] = "mfc_\t%0,%1";
2129 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2130 ret = retval;
2132 else if (regno1 == FPSW_REGNUM && ! ISA_HAS_8CC)
2133 ret = "cfc1\t%0,$31";
2137 else if (FP_REG_P (regno0))
2139 if (GP_REG_P (regno1))
2141 delay = DELAY_LOAD;
2142 ret = "mtc1\t%1,%0";
2145 if (FP_REG_P (regno1))
2146 ret = "mov.s\t%0,%1";
2149 else if (MD_REG_P (regno0))
2151 if (GP_REG_P (regno1))
2153 delay = DELAY_HILO;
2154 if (regno0 != HILO_REGNUM && ! TARGET_MIPS16)
2155 ret = "mt%0\t%1";
2159 else if (regno0 == FPSW_REGNUM && ! ISA_HAS_8CC)
2161 if (GP_REG_P (regno1))
2163 delay = DELAY_LOAD;
2164 ret = "ctc1\t%0,$31";
2167 else if (ALL_COP_REG_P (regno0))
2169 if (GP_REG_P (regno1))
2171 static char retval[] = "mtc_\t%1,%0";
2172 char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2174 if (cop == '0')
2175 abort_with_insn (insn,
2176 "mtc0 not supported; it disturbs virtual address translation");
2177 delay = DELAY_LOAD;
2178 retval[3] = cop;
2179 ret = retval;
2184 else if (code1 == MEM)
2186 delay = DELAY_LOAD;
2188 if (TARGET_STATS)
2189 mips_count_memory_refs (op1, 1);
2191 if (GP_REG_P (regno0))
2193 /* For loads, use the mode of the memory item, instead of the
2194 target, so zero/sign extend can use this code as well. */
2195 switch (GET_MODE (op1))
2197 default:
2198 break;
2199 case SFmode:
2200 ret = "lw\t%0,%1";
2201 break;
2202 case SImode:
2203 case CCmode:
2204 ret = ((unsignedp && TARGET_64BIT)
2205 ? "lwu\t%0,%1"
2206 : "lw\t%0,%1");
2207 break;
2208 case HImode:
2209 ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
2210 break;
2211 case QImode:
2212 ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
2213 break;
2217 else if (FP_REG_P (regno0) && (mode == SImode || mode == SFmode))
2218 ret = "l.s\t%0,%1";
2220 else if (ALL_COP_REG_P (regno0))
2222 static char retval[] = "lwc_\t%0,%1";
2223 char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2225 if (cop == '0')
2226 abort_with_insn (insn,
2227 "loads from memory to COP0 are illegal");
2228 delay = DELAY_LOAD;
2229 retval[3] = cop;
2230 ret = retval;
2233 if (ret != (char *)0 && MEM_VOLATILE_P (op1))
2235 size_t i = strlen (ret);
2236 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2237 abort ();
2239 sprintf (volatile_buffer, "%%{%s%%}", ret);
2240 ret = volatile_buffer;
2244 else if (code1 == CONST_INT
2245 || (code1 == CONST_DOUBLE
2246 && GET_MODE (op1) == VOIDmode))
2248 if (code1 == CONST_DOUBLE)
2250 /* This can happen when storing constants into long long
2251 bitfields. Just store the least significant word of
2252 the value. */
2253 operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2256 if (INTVAL (op1) == 0 && ! TARGET_MIPS16)
2258 if (GP_REG_P (regno0))
2259 ret = "move\t%0,%z1";
2261 else if (FP_REG_P (regno0))
2263 delay = DELAY_LOAD;
2264 ret = "mtc1\t%z1,%0";
2267 else if (MD_REG_P (regno0))
2269 delay = DELAY_HILO;
2270 ret = "mt%0\t%.";
2274 else if (GP_REG_P (regno0))
2276 /* Don't use X format, because that will give out of
2277 range numbers for 64 bit host and 32 bit target. */
2278 if (! TARGET_MIPS16)
2279 ret = "li\t%0,%1\t\t\t# %X1";
2280 else
2282 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2283 ret = "li\t%0,%1";
2284 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2285 ret = "li\t%0,%n1\n\tneg\t%0";
2290 else if (code1 == CONST_DOUBLE && mode == SFmode)
2292 if (op1 == CONST0_RTX (SFmode))
2294 if (GP_REG_P (regno0))
2295 ret = "move\t%0,%.";
2297 else if (FP_REG_P (regno0))
2299 delay = DELAY_LOAD;
2300 ret = "mtc1\t%.,%0";
2304 else
2306 delay = DELAY_LOAD;
2307 ret = "li.s\t%0,%1";
2311 else if (code1 == LABEL_REF)
2313 if (TARGET_STATS)
2314 mips_count_memory_refs (op1, 1);
2316 ret = "la\t%0,%a1";
2319 else if (code1 == SYMBOL_REF || code1 == CONST)
2321 if (TARGET_MIPS16
2322 && code1 == CONST
2323 && GET_CODE (XEXP (op1, 0)) == REG
2324 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2326 /* This case arises on the mips16; see
2327 mips16_gp_pseudo_reg. */
2328 ret = "move\t%0,%+";
2330 else if (TARGET_MIPS16
2331 && code1 == SYMBOL_REF
2332 && SYMBOL_REF_FLAG (op1)
2333 && (XSTR (op1, 0)[0] != '*'
2334 || strncmp (XSTR (op1, 0) + 1,
2335 LOCAL_LABEL_PREFIX,
2336 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2338 /* This can occur when reloading the address of a GP
2339 relative symbol on the mips16. */
2340 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2342 else
2344 if (TARGET_STATS)
2345 mips_count_memory_refs (op1, 1);
2347 ret = "la\t%0,%a1";
2351 else if (code1 == PLUS)
2353 rtx add_op0 = XEXP (op1, 0);
2354 rtx add_op1 = XEXP (op1, 1);
2356 if (GET_CODE (XEXP (op1, 1)) == REG
2357 && GET_CODE (XEXP (op1, 0)) == CONST_INT)
2358 add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
2360 operands[2] = add_op0;
2361 operands[3] = add_op1;
2362 ret = "add%:\t%0,%2,%3";
2365 else if (code1 == HIGH)
2367 operands[1] = XEXP (op1, 0);
2368 ret = "lui\t%0,%%hi(%1)";
2372 else if (code0 == MEM)
2374 if (TARGET_STATS)
2375 mips_count_memory_refs (op0, 1);
2377 if (code1 == REG)
2379 int regno1 = REGNO (op1) + subreg_offset1;
2381 if (GP_REG_P (regno1))
2383 switch (mode)
2385 case SFmode: ret = "sw\t%1,%0"; break;
2386 case SImode: ret = "sw\t%1,%0"; break;
2387 case HImode: ret = "sh\t%1,%0"; break;
2388 case QImode: ret = "sb\t%1,%0"; break;
2389 default: break;
2393 else if (FP_REG_P (regno1) && (mode == SImode || mode == SFmode))
2394 ret = "s.s\t%1,%0";
2395 else if (ALL_COP_REG_P (regno1))
2397 static char retval[] = "swc_\t%1,%0";
2399 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2400 ret = retval;
2404 else if (code1 == CONST_INT && INTVAL (op1) == 0)
2406 switch (mode)
2408 case SFmode: ret = "sw\t%z1,%0"; break;
2409 case SImode: ret = "sw\t%z1,%0"; break;
2410 case HImode: ret = "sh\t%z1,%0"; break;
2411 case QImode: ret = "sb\t%z1,%0"; break;
2412 default: break;
2416 else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
2418 switch (mode)
2420 case SFmode: ret = "sw\t%.,%0"; break;
2421 case SImode: ret = "sw\t%.,%0"; break;
2422 case HImode: ret = "sh\t%.,%0"; break;
2423 case QImode: ret = "sb\t%.,%0"; break;
2424 default: break;
2428 if (ret != 0 && MEM_VOLATILE_P (op0))
2430 size_t i = strlen (ret);
2432 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2433 abort ();
2435 sprintf (volatile_buffer, "%%{%s%%}", ret);
2436 ret = volatile_buffer;
2440 if (ret == 0)
2442 abort_with_insn (insn, "bad move");
2443 return 0;
2446 if (delay != DELAY_NONE)
2447 return mips_fill_delay_slot (ret, delay, operands, insn);
2449 return ret;
2452 /* Return instructions to restore the global pointer from the stack,
2453 assuming TARGET_ABICALLS. Used by exception_receiver to set up
2454 the GP for exception handlers.
2456 OPERANDS is an array of operands whose contents are undefined
2457 on entry. INSN is the exception_handler instruction. */
2459 const char *
2460 mips_restore_gp (operands, insn)
2461 rtx *operands, insn;
2463 rtx loc;
2465 operands[0] = pic_offset_table_rtx;
2466 if (frame_pointer_needed)
2467 loc = hard_frame_pointer_rtx;
2468 else
2469 loc = stack_pointer_rtx;
2470 loc = plus_constant (loc, cfun->machine->frame.args_size);
2471 operands[1] = gen_rtx_MEM (Pmode, loc);
2473 return mips_move_1word (operands, insn, 0);
2476 /* Return an instruction to sign-extend SImode value SRC and store it
2477 in DImode value DEST. INSN is the original extendsidi2-type insn. */
2479 const char *
2480 mips_sign_extend (insn, dest, src)
2481 rtx insn, dest, src;
2483 rtx operands[MAX_RECOG_OPERANDS];
2485 if ((register_operand (src, SImode) && FP_REG_P (true_regnum (src)))
2486 || memory_operand (src, SImode))
2488 /* If the source is a floating-point register, we need to use a
2489 32-bit move, since the float register is not kept sign-extended.
2490 If the source is in memory, we need a 32-bit load. */
2491 operands[0] = gen_lowpart_SUBREG (SImode, dest);
2492 operands[1] = src;
2493 return mips_move_1word (operands, insn, false);
2495 else
2497 operands[0] = dest;
2498 operands[1] = src;
2499 return mips_move_2words (operands, insn);
2503 /* Return the appropriate instructions to move 2 words */
2505 const char *
2506 mips_move_2words (operands, insn)
2507 rtx operands[];
2508 rtx insn;
2510 const char *ret = 0;
2511 rtx op0 = operands[0];
2512 rtx op1 = operands[1];
2513 enum rtx_code code0 = GET_CODE (operands[0]);
2514 enum rtx_code code1 = GET_CODE (operands[1]);
2515 int subreg_offset0 = 0;
2516 int subreg_offset1 = 0;
2517 enum delay_type delay = DELAY_NONE;
2519 if (code1 == SIGN_EXTEND)
2520 return mips_sign_extend (insn, op0, XEXP (op1, 0));
2522 while (code0 == SUBREG)
2524 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2525 GET_MODE (SUBREG_REG (op0)),
2526 SUBREG_BYTE (op0),
2527 GET_MODE (op0));
2528 op0 = SUBREG_REG (op0);
2529 code0 = GET_CODE (op0);
2532 while (code1 == SUBREG)
2534 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2535 GET_MODE (SUBREG_REG (op1)),
2536 SUBREG_BYTE (op1),
2537 GET_MODE (op1));
2538 op1 = SUBREG_REG (op1);
2539 code1 = GET_CODE (op1);
2542 if (code0 == REG)
2544 int regno0 = REGNO (op0) + subreg_offset0;
2546 if (code1 == REG)
2548 int regno1 = REGNO (op1) + subreg_offset1;
2550 /* Just in case, don't do anything for assigning a register
2551 to itself, unless we are filling a delay slot. */
2552 if (regno0 == regno1 && set_nomacro == 0)
2553 ret = "";
2555 else if (FP_REG_P (regno0))
2557 if (FP_REG_P (regno1))
2558 ret = "mov.d\t%0,%1";
2560 else
2562 delay = DELAY_LOAD;
2563 if (TARGET_FLOAT64)
2565 if (!TARGET_64BIT)
2566 abort_with_insn (insn, "bad move");
2568 #ifdef TARGET_FP_CALL_32
2569 if (FP_CALL_GP_REG_P (regno1))
2570 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tdmtc1\t%1,%0";
2571 else
2572 #endif
2573 ret = "dmtc1\t%1,%0";
2575 else
2576 ret = "mtc1\t%L1,%0\n\tmtc1\t%M1,%D0";
2580 else if (FP_REG_P (regno1))
2582 delay = DELAY_LOAD;
2583 if (TARGET_FLOAT64)
2585 if (!TARGET_64BIT)
2586 abort_with_insn (insn, "bad move");
2588 #ifdef TARGET_FP_CALL_32
2589 if (FP_CALL_GP_REG_P (regno0))
2590 ret = "dmfc1\t%0,%1\n\tmfc1\t%D0,%1\n\tdsrl\t%0,32";
2591 else
2592 #endif
2593 ret = "dmfc1\t%0,%1";
2595 else
2596 ret = "mfc1\t%L0,%1\n\tmfc1\t%M0,%D1";
2599 else if (MD_REG_P (regno0) && GP_REG_P (regno1) && !TARGET_MIPS16)
2601 delay = DELAY_HILO;
2602 if (TARGET_64BIT)
2604 if (regno0 != HILO_REGNUM)
2605 ret = "mt%0\t%1";
2606 else if (regno1 == 0)
2607 ret = "mtlo\t%.\n\tmthi\t%.";
2609 else
2610 ret = "mthi\t%M1\n\tmtlo\t%L1";
2613 else if (GP_REG_P (regno0) && MD_REG_P (regno1))
2615 delay = DELAY_HILO;
2616 if (TARGET_64BIT)
2618 if (regno1 != HILO_REGNUM)
2619 ret = "mf%1\t%0";
2621 else
2622 ret = "mfhi\t%M0\n\tmflo\t%L0";
2624 else if (GP_REG_P (regno0) && ALL_COP_REG_P (regno1)
2625 && TARGET_64BIT)
2627 static char retval[] = "dmfc_\t%0,%1";
2629 delay = DELAY_LOAD;
2630 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2631 ret = retval;
2633 else if (ALL_COP_REG_P (regno0) && GP_REG_P (regno1)
2634 && TARGET_64BIT)
2636 static char retval[] = "dmtc_\t%1,%0";
2637 char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2639 if (cop == '0')
2640 abort_with_insn (insn,
2641 "dmtc0 not supported; it disturbs virtual address translation");
2642 delay = DELAY_LOAD;
2643 retval[4] = cop;
2644 ret = retval;
2646 else if (TARGET_64BIT)
2647 ret = "move\t%0,%1";
2649 else if (regno0 != (regno1+1))
2650 ret = "move\t%0,%1\n\tmove\t%D0,%D1";
2652 else
2653 ret = "move\t%D0,%D1\n\tmove\t%0,%1";
2656 else if (code1 == CONST_DOUBLE)
2658 /* Move zero from $0 unless !TARGET_64BIT and recipient
2659 is 64-bit fp reg, in which case generate a constant. */
2660 if (op1 != CONST0_RTX (GET_MODE (op1))
2661 || (TARGET_FLOAT64 && !TARGET_64BIT && FP_REG_P (regno0)))
2663 if (GET_MODE (op1) == DFmode)
2665 delay = DELAY_LOAD;
2667 #ifdef TARGET_FP_CALL_32
2668 if (FP_CALL_GP_REG_P (regno0))
2670 if (TARGET_FLOAT64 && !TARGET_64BIT)
2672 split_double (op1, operands + 2, operands + 3);
2673 ret = "li\t%0,%2\n\tli\t%D0,%3";
2675 else
2676 ret = "li.d\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32";
2678 else
2679 #endif
2680 /* GNU as emits 64-bit code for li.d if the ISA is 3
2681 or higher. For !TARGET_64BIT && gp registers we
2682 need to avoid this by using two li instructions
2683 instead. */
2684 if (ISA_HAS_64BIT_REGS
2685 && ! TARGET_64BIT
2686 && ! FP_REG_P (regno0))
2688 split_double (op1, operands + 2, operands + 3);
2689 ret = "li\t%0,%2\n\tli\t%D0,%3";
2691 else
2692 ret = "li.d\t%0,%1";
2695 else if (TARGET_64BIT)
2697 if (! TARGET_MIPS16)
2698 ret = "dli\t%0,%1";
2701 else
2703 split_double (op1, operands + 2, operands + 3);
2704 ret = "li\t%0,%2\n\tli\t%D0,%3";
2708 else
2710 if (GP_REG_P (regno0))
2711 ret = (TARGET_64BIT
2712 #ifdef TARGET_FP_CALL_32
2713 && ! FP_CALL_GP_REG_P (regno0)
2714 #endif
2715 ? "move\t%0,%."
2716 : "move\t%0,%.\n\tmove\t%D0,%.");
2718 else if (FP_REG_P (regno0))
2720 delay = DELAY_LOAD;
2721 ret = (TARGET_64BIT
2722 ? "dmtc1\t%.,%0"
2723 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0");
2728 else if (code1 == CONST_INT && INTVAL (op1) == 0 && ! TARGET_MIPS16)
2730 if (GP_REG_P (regno0))
2731 ret = (TARGET_64BIT
2732 ? "move\t%0,%."
2733 : "move\t%0,%.\n\tmove\t%D0,%.");
2735 else if (FP_REG_P (regno0))
2737 delay = DELAY_LOAD;
2738 ret = (TARGET_64BIT
2739 ? "dmtc1\t%.,%0"
2740 : (TARGET_FLOAT64
2741 ? "li.d\t%0,%1"
2742 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0"));
2744 else if (MD_REG_P (regno0))
2746 delay = DELAY_HILO;
2747 ret = (regno0 == HILO_REGNUM
2748 ? "mtlo\t%.\n\tmthi\t%."
2749 : "mt%0\t%.\n");
2753 else if (code1 == CONST_INT && GET_MODE (op0) == DImode
2754 && GP_REG_P (regno0))
2756 if (TARGET_64BIT)
2758 if (TARGET_MIPS16)
2760 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2761 ret = "li\t%0,%1";
2762 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2763 ret = "li\t%0,%n1\n\tneg\t%0";
2765 else if (GET_CODE (operands[1]) == SIGN_EXTEND)
2766 ret = "li\t%0,%1\t\t# %X1";
2767 else if (HOST_BITS_PER_WIDE_INT < 64)
2768 /* We can't use 'X' for negative numbers, because then we won't
2769 get the right value for the upper 32 bits. */
2770 ret = (INTVAL (op1) < 0
2771 ? "dli\t%0,%1\t\t\t# %X1"
2772 : "dli\t%0,%X1\t\t# %1");
2773 else
2774 /* We must use 'X', because otherwise LONG_MIN will print as
2775 a number that the assembler won't accept. */
2776 ret = "dli\t%0,%X1\t\t# %1";
2778 else if (HOST_BITS_PER_WIDE_INT < 64)
2780 operands[2] = GEN_INT (INTVAL (operands[1]) >= 0 ? 0 : -1);
2781 if (TARGET_MIPS16)
2783 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2784 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2785 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2787 operands[2] = GEN_INT (1);
2788 ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2791 else
2792 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2794 else
2796 /* We use multiple shifts here, to avoid warnings about out
2797 of range shifts on 32 bit hosts. */
2798 operands[2] = GEN_INT (INTVAL (operands[1]) >> 16 >> 16);
2799 operands[1]
2800 = GEN_INT (INTVAL (operands[1]) << 16 << 16 >> 16 >> 16);
2801 if (TARGET_MIPS16)
2803 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2804 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2805 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2807 operands[2] = GEN_INT (1);
2808 ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2811 else
2812 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2816 else if (code1 == MEM)
2818 delay = DELAY_LOAD;
2820 if (TARGET_STATS)
2821 mips_count_memory_refs (op1, 2);
2823 if (FP_REG_P (regno0))
2824 ret = "l.d\t%0,%1";
2826 else if (ALL_COP_REG_P (regno0) && TARGET_64BIT)
2828 static char retval[] = "ldc_\t%0,%1";
2829 char cop = COPNUM_AS_CHAR_FROM_REGNUM (regno0);
2831 if (cop == '0')
2832 abort_with_insn (insn,
2833 "loads from memory to COP0 are illegal");
2834 delay = DELAY_LOAD;
2835 retval[3] = cop;
2836 ret = retval;
2839 else if (TARGET_64BIT)
2842 #ifdef TARGET_FP_CALL_32
2843 if (FP_CALL_GP_REG_P (regno0))
2844 ret = (double_memory_operand (op1, GET_MODE (op1))
2845 ? "lwu\t%0,%1\n\tlwu\t%D0,4+%1"
2846 : "ld\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32");
2847 else
2848 #endif
2849 ret = "ld\t%0,%1";
2852 else if (double_memory_operand (op1, GET_MODE (op1)))
2853 ret = (reg_mentioned_p (op0, op1)
2854 ? "lw\t%D0,%D1\n\tlw\t%0,%1"
2855 : "lw\t%0,%1\n\tlw\t%D0,%D1");
2857 if (ret != 0 && MEM_VOLATILE_P (op1))
2859 size_t i = strlen (ret);
2861 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2862 abort ();
2864 sprintf (volatile_buffer, "%%{%s%%}", ret);
2865 ret = volatile_buffer;
2869 else if (code1 == LABEL_REF)
2871 if (TARGET_STATS)
2872 mips_count_memory_refs (op1, 2);
2874 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2875 /* We deliberately remove the 'a' from '%1', so that we don't
2876 have to add SIGN_EXTEND support to print_operand_address.
2877 print_operand will just call print_operand_address in this
2878 case, so there is no problem. */
2879 ret = "la\t%0,%1";
2880 else
2881 ret = "dla\t%0,%a1";
2883 else if (code1 == SYMBOL_REF || code1 == CONST)
2885 if (TARGET_MIPS16
2886 && code1 == CONST
2887 && GET_CODE (XEXP (op1, 0)) == REG
2888 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2890 /* This case arises on the mips16; see
2891 mips16_gp_pseudo_reg. */
2892 ret = "move\t%0,%+";
2894 else if (TARGET_MIPS16
2895 && code1 == SYMBOL_REF
2896 && SYMBOL_REF_FLAG (op1)
2897 && (XSTR (op1, 0)[0] != '*'
2898 || strncmp (XSTR (op1, 0) + 1,
2899 LOCAL_LABEL_PREFIX,
2900 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2902 /* This can occur when reloading the address of a GP
2903 relative symbol on the mips16. */
2904 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2906 else
2908 if (TARGET_STATS)
2909 mips_count_memory_refs (op1, 2);
2911 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2912 /* We deliberately remove the 'a' from '%1', so that we don't
2913 have to add SIGN_EXTEND support to print_operand_address.
2914 print_operand will just call print_operand_address in this
2915 case, so there is no problem. */
2916 ret = "la\t%0,%1";
2917 else
2918 ret = "dla\t%0,%a1";
2923 else if (code0 == MEM)
2925 if (code1 == REG)
2927 int regno1 = REGNO (op1) + subreg_offset1;
2929 if (FP_REG_P (regno1))
2930 ret = "s.d\t%1,%0";
2932 else if (ALL_COP_REG_P (regno1) && TARGET_64BIT)
2934 static char retval[] = "sdc_\t%1,%0";
2936 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (regno1);
2937 ret = retval;
2939 else if (TARGET_64BIT)
2942 #ifdef TARGET_FP_CALL_32
2943 if (FP_CALL_GP_REG_P (regno1))
2944 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tsd\t%1,%0";
2945 else
2946 #endif
2947 ret = "sd\t%1,%0";
2950 else if (double_memory_operand (op0, GET_MODE (op0)))
2951 ret = "sw\t%1,%0\n\tsw\t%D1,%D0";
2954 else if (((code1 == CONST_INT && INTVAL (op1) == 0)
2955 || (code1 == CONST_DOUBLE
2956 && op1 == CONST0_RTX (GET_MODE (op1))))
2957 && (TARGET_64BIT
2958 || double_memory_operand (op0, GET_MODE (op0))))
2960 if (TARGET_64BIT)
2961 ret = "sd\t%.,%0";
2962 else
2963 ret = "sw\t%.,%0\n\tsw\t%.,%D0";
2966 if (TARGET_STATS)
2967 mips_count_memory_refs (op0, 2);
2969 if (ret != 0 && MEM_VOLATILE_P (op0))
2971 size_t i = strlen (ret);
2973 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2974 abort ();
2976 sprintf (volatile_buffer, "%%{%s%%}", ret);
2977 ret = volatile_buffer;
2981 if (ret == 0)
2983 abort_with_insn (insn, "bad move");
2984 return 0;
2987 if (delay != DELAY_NONE)
2988 return mips_fill_delay_slot (ret, delay, operands, insn);
2990 return ret;
2993 static bool
2994 mips_rtx_costs (x, code, outer_code, total)
2995 rtx x;
2996 int code, outer_code;
2997 int *total;
2999 enum machine_mode mode = GET_MODE (x);
3001 switch (code)
3003 case CONST_INT:
3004 if (! TARGET_MIPS16)
3006 /* Always return 0, since we don't have different sized insns,
3007 hence different costs according to Richard Kenner. */
3008 *total = 0;
3009 return true;
3012 if (outer_code == SET)
3014 if (INTVAL (x) >= 0 && INTVAL (x) < 0x100)
3015 *total = 0;
3016 else if ((INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3017 || (INTVAL (x) < 0 && INTVAL (x) > -0x100))
3018 *total = COSTS_N_INSNS (1);
3019 else
3020 *total = COSTS_N_INSNS (2);
3021 return true;
3024 /* A PLUS could be an address. We don't want to force an address
3025 to use a register, so accept any signed 16 bit value without
3026 complaint. */
3027 if (outer_code == PLUS
3028 && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
3030 *total = 0;
3031 return true;
3034 /* A number between 1 and 8 inclusive is efficient for a shift.
3035 Otherwise, we will need an extended instruction. */
3036 if (outer_code == ASHIFT || outer_code == ASHIFTRT
3037 || outer_code == LSHIFTRT)
3039 if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
3040 *total = 0;
3041 else
3042 *total = COSTS_N_INSNS (1);
3043 return true;
3046 /* We can use cmpi for an xor with an unsigned 16 bit value. */
3047 if (outer_code == XOR
3048 && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3050 *total = 0;
3051 return true;
3054 /* We may be able to use slt or sltu for a comparison with a
3055 signed 16 bit value. (The boundary conditions aren't quite
3056 right, but this is just a heuristic anyhow.) */
3057 if ((outer_code == LT || outer_code == LE
3058 || outer_code == GE || outer_code == GT
3059 || outer_code == LTU || outer_code == LEU
3060 || outer_code == GEU || outer_code == GTU)
3061 && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
3063 *total = 0;
3064 return true;
3067 /* Equality comparisons with 0 are cheap. */
3068 if ((outer_code == EQ || outer_code == NE)
3069 && INTVAL (x) == 0)
3070 return 0;
3072 /* Otherwise, work out the cost to load the value into a
3073 register. */
3074 if (INTVAL (x) >= 0 && INTVAL (x) < 0x100)
3075 *total = COSTS_N_INSNS (1);
3076 else if ((INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
3077 || (INTVAL (x) < 0 && INTVAL (x) > -0x100))
3078 *total = COSTS_N_INSNS (2);
3079 else
3080 *total = COSTS_N_INSNS (3);
3081 return true;
3083 case LABEL_REF:
3084 *total = COSTS_N_INSNS (2);
3085 return true;
3087 case CONST:
3089 rtx offset = const0_rtx;
3090 rtx symref = eliminate_constant_term (XEXP (x, 0), &offset);
3092 if (TARGET_MIPS16 && mips16_gp_offset_p (x))
3094 /* Treat this like a signed 16 bit CONST_INT. */
3095 if (outer_code == PLUS)
3096 *total = 0;
3097 else if (outer_code == SET)
3098 *total = COSTS_N_INSNS (1);
3099 else
3100 *total = COSTS_N_INSNS (2);
3101 return true;
3104 if (GET_CODE (symref) == LABEL_REF)
3105 *total = COSTS_N_INSNS (2);
3106 else if (GET_CODE (symref) != SYMBOL_REF)
3107 *total = COSTS_N_INSNS (4);
3108 else if (INTVAL (offset) < -32768 || INTVAL (offset) > 32767)
3109 *total = COSTS_N_INSNS (2);
3110 else
3111 *total = COSTS_N_INSNS (SYMBOL_REF_FLAG (symref) ? 1 : 2);
3113 return true;
3116 case SYMBOL_REF:
3117 *total = COSTS_N_INSNS (SYMBOL_REF_FLAG (x) ? 1 : 2);
3118 return true;
3120 case CONST_DOUBLE:
3122 rtx high, low;
3123 if (TARGET_MIPS16)
3125 *total = COSTS_N_INSNS (4);
3126 return true;
3129 split_double (x, &high, &low);
3130 *total = COSTS_N_INSNS ((high == CONST0_RTX (GET_MODE (high))
3131 || low == CONST0_RTX (GET_MODE (low)))
3132 ? 2 : 4);
3133 return true;
3136 case MEM:
3138 int num_words = (GET_MODE_SIZE (mode) > UNITS_PER_WORD) ? 2 : 1;
3139 if (simple_memory_operand (x, mode))
3140 *total = COSTS_N_INSNS (num_words);
3141 else
3142 *total = COSTS_N_INSNS (2*num_words);
3143 return true;
3146 case FFS:
3147 *total = COSTS_N_INSNS (6);
3148 return true;
3150 case NOT:
3151 *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
3152 return true;
3154 case AND:
3155 case IOR:
3156 case XOR:
3157 if (mode == DImode && !TARGET_64BIT)
3159 *total = COSTS_N_INSNS (2);
3160 return true;
3162 return false;
3164 case ASHIFT:
3165 case ASHIFTRT:
3166 case LSHIFTRT:
3167 if (mode == DImode && !TARGET_64BIT)
3169 *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
3170 ? 4 : 12);
3171 return true;
3173 return false;
3175 case ABS:
3176 if (mode == SFmode || mode == DFmode)
3177 *total = COSTS_N_INSNS (1);
3178 else
3179 *total = COSTS_N_INSNS (4);
3180 return true;
3182 case PLUS:
3183 case MINUS:
3184 if (mode == SFmode || mode == DFmode)
3186 if (TUNE_MIPS3000 || TUNE_MIPS3900)
3187 *total = COSTS_N_INSNS (2);
3188 else if (TUNE_MIPS6000)
3189 *total = COSTS_N_INSNS (3);
3190 else
3191 *total = COSTS_N_INSNS (6);
3192 return true;
3194 if (mode == DImode && !TARGET_64BIT)
3196 *total = COSTS_N_INSNS (4);
3197 return true;
3199 return false;
3201 case NEG:
3202 if (mode == DImode && !TARGET_64BIT)
3204 *total = 4;
3205 return true;
3207 return false;
3209 case MULT:
3210 if (mode == SFmode)
3212 if (TUNE_MIPS3000
3213 || TUNE_MIPS3900
3214 || TUNE_MIPS5000)
3215 *total = COSTS_N_INSNS (4);
3216 else if (TUNE_MIPS6000
3217 || TUNE_MIPS5400
3218 || TUNE_MIPS5500)
3219 *total = COSTS_N_INSNS (5);
3220 else
3221 *total = COSTS_N_INSNS (7);
3222 return true;
3225 if (mode == DFmode)
3227 if (TUNE_MIPS3000
3228 || TUNE_MIPS3900
3229 || TUNE_MIPS5000)
3230 *total = COSTS_N_INSNS (5);
3231 else if (TUNE_MIPS6000
3232 || TUNE_MIPS5400
3233 || TUNE_MIPS5500)
3234 *total = COSTS_N_INSNS (6);
3235 else
3236 *total = COSTS_N_INSNS (8);
3237 return true;
3240 if (TUNE_MIPS3000)
3241 *total = COSTS_N_INSNS (12);
3242 else if (TUNE_MIPS3900)
3243 *total = COSTS_N_INSNS (2);
3244 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3245 *total = COSTS_N_INSNS ((mode == DImode) ? 4 : 3);
3246 else if (TUNE_MIPS6000)
3247 *total = COSTS_N_INSNS (17);
3248 else if (TUNE_MIPS5000)
3249 *total = COSTS_N_INSNS (5);
3250 else
3251 *total = COSTS_N_INSNS (10);
3252 return true;
3254 case DIV:
3255 case MOD:
3256 if (mode == SFmode)
3258 if (TUNE_MIPS3000
3259 || TUNE_MIPS3900)
3260 *total = COSTS_N_INSNS (12);
3261 else if (TUNE_MIPS6000)
3262 *total = COSTS_N_INSNS (15);
3263 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3264 *total = COSTS_N_INSNS (30);
3265 else
3266 *total = COSTS_N_INSNS (23);
3267 return true;
3270 if (mode == DFmode)
3272 if (TUNE_MIPS3000
3273 || TUNE_MIPS3900)
3274 *total = COSTS_N_INSNS (19);
3275 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3276 *total = COSTS_N_INSNS (59);
3277 else if (TUNE_MIPS6000)
3278 *total = COSTS_N_INSNS (16);
3279 else
3280 *total = COSTS_N_INSNS (36);
3281 return true;
3283 /* FALLTHRU */
3285 case UDIV:
3286 case UMOD:
3287 if (TUNE_MIPS3000
3288 || TUNE_MIPS3900)
3289 *total = COSTS_N_INSNS (35);
3290 else if (TUNE_MIPS6000)
3291 *total = COSTS_N_INSNS (38);
3292 else if (TUNE_MIPS5000)
3293 *total = COSTS_N_INSNS (36);
3294 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
3295 *total = COSTS_N_INSNS ((mode == SImode) ? 42 : 74);
3296 else
3297 *total = COSTS_N_INSNS (69);
3298 return true;
3300 case SIGN_EXTEND:
3301 /* A sign extend from SImode to DImode in 64 bit mode is often
3302 zero instructions, because the result can often be used
3303 directly by another instruction; we'll call it one. */
3304 if (TARGET_64BIT && mode == DImode
3305 && GET_MODE (XEXP (x, 0)) == SImode)
3306 *total = COSTS_N_INSNS (1);
3307 else
3308 *total = COSTS_N_INSNS (2);
3309 return true;
3311 case ZERO_EXTEND:
3312 if (TARGET_64BIT && mode == DImode
3313 && GET_MODE (XEXP (x, 0)) == SImode)
3314 *total = COSTS_N_INSNS (2);
3315 else
3316 *total = COSTS_N_INSNS (1);
3317 return true;
3319 default:
3320 return false;
3324 /* Provide the costs of an addressing mode that contains ADDR.
3325 If ADDR is not a valid address, its cost is irrelevant. */
3328 mips_address_cost (addr)
3329 rtx addr;
3331 switch (GET_CODE (addr))
3333 case LO_SUM:
3334 return 1;
3336 case LABEL_REF:
3337 return 2;
3339 case CONST:
3341 rtx offset = const0_rtx;
3342 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
3343 if (GET_CODE (addr) == LABEL_REF)
3344 return 2;
3346 if (GET_CODE (addr) != SYMBOL_REF)
3347 return 4;
3349 if (! SMALL_INT (offset))
3350 return 2;
3353 /* ... fall through ... */
3355 case SYMBOL_REF:
3356 return SYMBOL_REF_FLAG (addr) ? 1 : 2;
3358 case PLUS:
3360 register rtx plus0 = XEXP (addr, 0);
3361 register rtx plus1 = XEXP (addr, 1);
3363 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
3364 plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
3366 if (GET_CODE (plus0) != REG)
3367 break;
3369 switch (GET_CODE (plus1))
3371 case CONST_INT:
3372 return SMALL_INT (plus1) ? 1 : 2;
3374 case CONST:
3375 case SYMBOL_REF:
3376 case LABEL_REF:
3377 case HIGH:
3378 case LO_SUM:
3379 return mips_address_cost (plus1) + 1;
3381 default:
3382 break;
3386 default:
3387 break;
3390 return 4;
3393 /* Return nonzero if X is an address which needs a temporary register when
3394 reloaded while generating PIC code. */
3397 pic_address_needs_scratch (x)
3398 rtx x;
3400 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
3401 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
3402 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
3403 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3404 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
3405 return 1;
3407 return 0;
3410 /* Make normal rtx_code into something we can index from an array */
3412 static enum internal_test
3413 map_test_to_internal_test (test_code)
3414 enum rtx_code test_code;
3416 enum internal_test test = ITEST_MAX;
3418 switch (test_code)
3420 case EQ: test = ITEST_EQ; break;
3421 case NE: test = ITEST_NE; break;
3422 case GT: test = ITEST_GT; break;
3423 case GE: test = ITEST_GE; break;
3424 case LT: test = ITEST_LT; break;
3425 case LE: test = ITEST_LE; break;
3426 case GTU: test = ITEST_GTU; break;
3427 case GEU: test = ITEST_GEU; break;
3428 case LTU: test = ITEST_LTU; break;
3429 case LEU: test = ITEST_LEU; break;
3430 default: break;
3433 return test;
3437 /* Generate the code to compare two integer values. The return value is:
3438 (reg:SI xx) The pseudo register the comparison is in
3439 0 No register, generate a simple branch.
3441 ??? This is called with result nonzero by the Scond patterns in
3442 mips.md. These patterns are called with a target in the mode of
3443 the Scond instruction pattern. Since this must be a constant, we
3444 must use SImode. This means that if RESULT is nonzero, it will
3445 always be an SImode register, even if TARGET_64BIT is true. We
3446 cope with this by calling convert_move rather than emit_move_insn.
3447 This will sometimes lead to an unnecessary extension of the result;
3448 for example:
3450 long long
3451 foo (long long i)
3453 return i < 5;
3459 gen_int_relational (test_code, result, cmp0, cmp1, p_invert)
3460 enum rtx_code test_code; /* relational test (EQ, etc) */
3461 rtx result; /* result to store comp. or 0 if branch */
3462 rtx cmp0; /* first operand to compare */
3463 rtx cmp1; /* second operand to compare */
3464 int *p_invert; /* NULL or ptr to hold whether branch needs */
3465 /* to reverse its test */
3467 struct cmp_info
3469 enum rtx_code test_code; /* code to use in instruction (LT vs. LTU) */
3470 int const_low; /* low bound of constant we can accept */
3471 int const_high; /* high bound of constant we can accept */
3472 int const_add; /* constant to add (convert LE -> LT) */
3473 int reverse_regs; /* reverse registers in test */
3474 int invert_const; /* != 0 if invert value if cmp1 is constant */
3475 int invert_reg; /* != 0 if invert value if cmp1 is register */
3476 int unsignedp; /* != 0 for unsigned comparisons. */
3479 static const struct cmp_info info[ (int)ITEST_MAX ] = {
3481 { XOR, 0, 65535, 0, 0, 0, 0, 0 }, /* EQ */
3482 { XOR, 0, 65535, 0, 0, 1, 1, 0 }, /* NE */
3483 { LT, -32769, 32766, 1, 1, 1, 0, 0 }, /* GT */
3484 { LT, -32768, 32767, 0, 0, 1, 1, 0 }, /* GE */
3485 { LT, -32768, 32767, 0, 0, 0, 0, 0 }, /* LT */
3486 { LT, -32769, 32766, 1, 1, 0, 1, 0 }, /* LE */
3487 { LTU, -32769, 32766, 1, 1, 1, 0, 1 }, /* GTU */
3488 { LTU, -32768, 32767, 0, 0, 1, 1, 1 }, /* GEU */
3489 { LTU, -32768, 32767, 0, 0, 0, 0, 1 }, /* LTU */
3490 { LTU, -32769, 32766, 1, 1, 0, 1, 1 }, /* LEU */
3493 enum internal_test test;
3494 enum machine_mode mode;
3495 const struct cmp_info *p_info;
3496 int branch_p;
3497 int eqne_p;
3498 int invert;
3499 rtx reg;
3500 rtx reg2;
3502 test = map_test_to_internal_test (test_code);
3503 if (test == ITEST_MAX)
3504 abort ();
3506 p_info = &info[(int) test];
3507 eqne_p = (p_info->test_code == XOR);
3509 mode = GET_MODE (cmp0);
3510 if (mode == VOIDmode)
3511 mode = GET_MODE (cmp1);
3513 /* Eliminate simple branches */
3514 branch_p = (result == 0);
3515 if (branch_p)
3517 if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
3519 /* Comparisons against zero are simple branches */
3520 if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
3521 && (! TARGET_MIPS16 || eqne_p))
3522 return 0;
3524 /* Test for beq/bne. */
3525 if (eqne_p && ! TARGET_MIPS16)
3526 return 0;
3529 /* allocate a pseudo to calculate the value in. */
3530 result = gen_reg_rtx (mode);
3533 /* Make sure we can handle any constants given to us. */
3534 if (GET_CODE (cmp0) == CONST_INT)
3535 cmp0 = force_reg (mode, cmp0);
3537 if (GET_CODE (cmp1) == CONST_INT)
3539 HOST_WIDE_INT value = INTVAL (cmp1);
3541 if (value < p_info->const_low
3542 || value > p_info->const_high
3543 /* ??? Why? And why wasn't the similar code below modified too? */
3544 || (TARGET_64BIT
3545 && HOST_BITS_PER_WIDE_INT < 64
3546 && p_info->const_add != 0
3547 && ((p_info->unsignedp
3548 ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
3549 > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
3550 : (value + p_info->const_add) > INTVAL (cmp1))
3551 != (p_info->const_add > 0))))
3552 cmp1 = force_reg (mode, cmp1);
3555 /* See if we need to invert the result. */
3556 invert = (GET_CODE (cmp1) == CONST_INT
3557 ? p_info->invert_const : p_info->invert_reg);
3559 if (p_invert != (int *)0)
3561 *p_invert = invert;
3562 invert = 0;
3565 /* Comparison to constants, may involve adding 1 to change a LT into LE.
3566 Comparison between two registers, may involve switching operands. */
3567 if (GET_CODE (cmp1) == CONST_INT)
3569 if (p_info->const_add != 0)
3571 HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
3573 /* If modification of cmp1 caused overflow,
3574 we would get the wrong answer if we follow the usual path;
3575 thus, x > 0xffffffffU would turn into x > 0U. */
3576 if ((p_info->unsignedp
3577 ? (unsigned HOST_WIDE_INT) new >
3578 (unsigned HOST_WIDE_INT) INTVAL (cmp1)
3579 : new > INTVAL (cmp1))
3580 != (p_info->const_add > 0))
3582 /* This test is always true, but if INVERT is true then
3583 the result of the test needs to be inverted so 0 should
3584 be returned instead. */
3585 emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
3586 return result;
3588 else
3589 cmp1 = GEN_INT (new);
3593 else if (p_info->reverse_regs)
3595 rtx temp = cmp0;
3596 cmp0 = cmp1;
3597 cmp1 = temp;
3600 if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
3601 reg = cmp0;
3602 else
3604 reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
3605 convert_move (reg, gen_rtx (p_info->test_code, mode, cmp0, cmp1), 0);
3608 if (test == ITEST_NE)
3610 if (! TARGET_MIPS16)
3612 convert_move (result, gen_rtx (GTU, mode, reg, const0_rtx), 0);
3613 if (p_invert != NULL)
3614 *p_invert = 0;
3615 invert = 0;
3617 else
3619 reg2 = invert ? gen_reg_rtx (mode) : result;
3620 convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
3621 reg = reg2;
3625 else if (test == ITEST_EQ)
3627 reg2 = invert ? gen_reg_rtx (mode) : result;
3628 convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3629 reg = reg2;
3632 if (invert)
3634 rtx one;
3636 if (! TARGET_MIPS16)
3637 one = const1_rtx;
3638 else
3640 /* The value is in $24. Copy it to another register, so
3641 that reload doesn't think it needs to store the $24 and
3642 the input to the XOR in the same location. */
3643 reg2 = gen_reg_rtx (mode);
3644 emit_move_insn (reg2, reg);
3645 reg = reg2;
3646 one = force_reg (mode, const1_rtx);
3648 convert_move (result, gen_rtx (XOR, mode, reg, one), 0);
3651 return result;
3654 /* Work out how to check a floating-point condition. We need a
3655 separate comparison instruction (C.cond.fmt), followed by a
3656 branch or conditional move. Given that IN_CODE is the
3657 required condition, set *CMP_CODE to the C.cond.fmt code
3658 and *action_code to the branch or move code. */
3660 static void
3661 get_float_compare_codes (in_code, cmp_code, action_code)
3662 enum rtx_code in_code, *cmp_code, *action_code;
3664 switch (in_code)
3666 case NE:
3667 case UNGE:
3668 case UNGT:
3669 case LTGT:
3670 case ORDERED:
3671 *cmp_code = reverse_condition_maybe_unordered (in_code);
3672 *action_code = EQ;
3673 break;
3675 default:
3676 *cmp_code = in_code;
3677 *action_code = NE;
3678 break;
3682 /* Emit the common code for doing conditional branches.
3683 operand[0] is the label to jump to.
3684 The comparison operands are saved away by cmp{si,di,sf,df}. */
3686 void
3687 gen_conditional_branch (operands, test_code)
3688 rtx operands[];
3689 enum rtx_code test_code;
3691 enum cmp_type type = branch_type;
3692 rtx cmp0 = branch_cmp[0];
3693 rtx cmp1 = branch_cmp[1];
3694 enum machine_mode mode;
3695 enum rtx_code cmp_code;
3696 rtx reg;
3697 int invert;
3698 rtx label1, label2;
3700 switch (type)
3702 case CMP_SI:
3703 case CMP_DI:
3704 mode = type == CMP_SI ? SImode : DImode;
3705 invert = 0;
3706 reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3708 if (reg)
3710 cmp0 = reg;
3711 cmp1 = const0_rtx;
3712 test_code = NE;
3714 else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3715 /* We don't want to build a comparison against a nonzero
3716 constant. */
3717 cmp1 = force_reg (mode, cmp1);
3719 break;
3721 case CMP_SF:
3722 case CMP_DF:
3723 if (! ISA_HAS_8CC)
3724 reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3725 else
3726 reg = gen_reg_rtx (CCmode);
3728 get_float_compare_codes (test_code, &cmp_code, &test_code);
3729 emit_insn (gen_rtx_SET (VOIDmode, reg,
3730 gen_rtx (cmp_code, CCmode, cmp0, cmp1)));
3732 mode = CCmode;
3733 cmp0 = reg;
3734 cmp1 = const0_rtx;
3735 invert = 0;
3736 break;
3738 default:
3739 abort_with_insn (gen_rtx (test_code, VOIDmode, cmp0, cmp1), "bad test");
3742 /* Generate the branch. */
3744 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3745 label2 = pc_rtx;
3747 if (invert)
3749 label2 = label1;
3750 label1 = pc_rtx;
3753 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
3754 gen_rtx_IF_THEN_ELSE (VOIDmode,
3755 gen_rtx (test_code, mode,
3756 cmp0, cmp1),
3757 label1, label2)));
3760 /* Emit the common code for conditional moves. OPERANDS is the array
3761 of operands passed to the conditional move defined_expand. */
3763 void
3764 gen_conditional_move (operands)
3765 rtx *operands;
3767 rtx op0 = branch_cmp[0];
3768 rtx op1 = branch_cmp[1];
3769 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3770 enum rtx_code cmp_code = GET_CODE (operands[1]);
3771 enum rtx_code move_code = NE;
3772 enum machine_mode op_mode = GET_MODE (operands[0]);
3773 enum machine_mode cmp_mode;
3774 rtx cmp_reg;
3776 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3778 switch (cmp_code)
3780 case EQ:
3781 cmp_code = XOR;
3782 move_code = EQ;
3783 break;
3784 case NE:
3785 cmp_code = XOR;
3786 break;
3787 case LT:
3788 break;
3789 case GE:
3790 cmp_code = LT;
3791 move_code = EQ;
3792 break;
3793 case GT:
3794 cmp_code = LT;
3795 op0 = force_reg (mode, branch_cmp[1]);
3796 op1 = branch_cmp[0];
3797 break;
3798 case LE:
3799 cmp_code = LT;
3800 op0 = force_reg (mode, branch_cmp[1]);
3801 op1 = branch_cmp[0];
3802 move_code = EQ;
3803 break;
3804 case LTU:
3805 break;
3806 case GEU:
3807 cmp_code = LTU;
3808 move_code = EQ;
3809 break;
3810 case GTU:
3811 cmp_code = LTU;
3812 op0 = force_reg (mode, branch_cmp[1]);
3813 op1 = branch_cmp[0];
3814 break;
3815 case LEU:
3816 cmp_code = LTU;
3817 op0 = force_reg (mode, branch_cmp[1]);
3818 op1 = branch_cmp[0];
3819 move_code = EQ;
3820 break;
3821 default:
3822 abort ();
3825 else
3826 get_float_compare_codes (cmp_code, &cmp_code, &move_code);
3828 if (mode == SImode || mode == DImode)
3829 cmp_mode = mode;
3830 else if (mode == SFmode || mode == DFmode)
3831 cmp_mode = CCmode;
3832 else
3833 abort ();
3835 cmp_reg = gen_reg_rtx (cmp_mode);
3836 emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3837 gen_rtx (cmp_code, cmp_mode, op0, op1)));
3839 emit_insn (gen_rtx_SET (op_mode, operands[0],
3840 gen_rtx_IF_THEN_ELSE (op_mode,
3841 gen_rtx (move_code, VOIDmode,
3842 cmp_reg,
3843 CONST0_RTX (SImode)),
3844 operands[2], operands[3])));
3847 /* Emit the common code for conditional moves. OPERANDS is the array
3848 of operands passed to the conditional move defined_expand. */
3850 void
3851 mips_gen_conditional_trap (operands)
3852 rtx operands[];
3854 rtx op0, op1;
3855 enum rtx_code cmp_code = GET_CODE (operands[0]);
3856 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3858 /* MIPS conditional trap machine instructions don't have GT or LE
3859 flavors, so we must invert the comparison and convert to LT and
3860 GE, respectively. */
3861 switch (cmp_code)
3863 case GT: cmp_code = LT; break;
3864 case LE: cmp_code = GE; break;
3865 case GTU: cmp_code = LTU; break;
3866 case LEU: cmp_code = GEU; break;
3867 default: break;
3869 if (cmp_code == GET_CODE (operands[0]))
3871 op0 = force_reg (mode, branch_cmp[0]);
3872 op1 = branch_cmp[1];
3874 else
3876 op0 = force_reg (mode, branch_cmp[1]);
3877 op1 = branch_cmp[0];
3879 if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3880 op1 = force_reg (mode, op1);
3882 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3883 gen_rtx (cmp_code, GET_MODE (operands[0]), op0, op1),
3884 operands[1]));
3887 /* Return true if operand OP is a condition code register.
3888 Only for use during or after reload. */
3891 fcc_register_operand (op, mode)
3892 rtx op;
3893 enum machine_mode mode;
3895 return ((mode == VOIDmode || mode == GET_MODE (op))
3896 && (reload_in_progress || reload_completed)
3897 && (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
3898 && ST_REG_P (true_regnum (op)));
3901 /* Emit code to move general operand SRC into condition-code
3902 register DEST. SCRATCH is a scratch TFmode float register.
3903 The sequence is:
3905 FP1 = SRC
3906 FP2 = 0.0f
3907 DEST = FP2 < FP1
3909 where FP1 and FP2 are single-precision float registers
3910 taken from SCRATCH. */
3912 void
3913 mips_emit_fcc_reload (dest, src, scratch)
3914 rtx dest, src, scratch;
3916 rtx fp1, fp2;
3918 /* Change the source to SFmode. */
3919 if (GET_CODE (src) == MEM)
3920 src = adjust_address (src, SFmode, 0);
3921 else if (GET_CODE (src) == REG || GET_CODE (src) == SUBREG)
3922 src = gen_rtx_REG (SFmode, true_regnum (src));
3924 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3925 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3927 emit_move_insn (copy_rtx (fp1), src);
3928 emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3929 emit_insn (gen_slt_sf (dest, fp2, fp1));
3932 /* Emit code to change the current function's return address to
3933 ADDRESS. SCRATCH is available as a scratch register, if needed.
3934 ADDRESS and SCRATCH are both word-mode GPRs. */
3936 void
3937 mips_set_return_address (address, scratch)
3938 rtx address, scratch;
3940 HOST_WIDE_INT gp_offset;
3942 compute_frame_size (get_frame_size ());
3943 if (((cfun->machine->frame.mask >> 31) & 1) == 0)
3944 abort ();
3945 gp_offset = cfun->machine->frame.gp_sp_offset;
3947 /* Reduce SP + GP_OFSET to a legitimate address and put it in SCRATCH. */
3948 if (gp_offset < 32768)
3949 scratch = plus_constant (stack_pointer_rtx, gp_offset);
3950 else
3952 emit_move_insn (scratch, GEN_INT (gp_offset));
3953 if (Pmode == DImode)
3954 emit_insn (gen_adddi3 (scratch, scratch, stack_pointer_rtx));
3955 else
3956 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
3959 emit_move_insn (gen_rtx_MEM (GET_MODE (address), scratch), address);
3962 /* Write a loop to move a constant number of bytes.
3963 Generate load/stores as follows:
3965 do {
3966 temp1 = src[0];
3967 temp2 = src[1];
3969 temp<last> = src[MAX_MOVE_REGS-1];
3970 dest[0] = temp1;
3971 dest[1] = temp2;
3973 dest[MAX_MOVE_REGS-1] = temp<last>;
3974 src += MAX_MOVE_REGS;
3975 dest += MAX_MOVE_REGS;
3976 } while (src != final);
3978 This way, no NOP's are needed, and only MAX_MOVE_REGS+3 temp
3979 registers are needed.
3981 Aligned moves move MAX_MOVE_REGS*4 bytes every (2*MAX_MOVE_REGS)+3
3982 cycles, unaligned moves move MAX_MOVE_REGS*4 bytes every
3983 (4*MAX_MOVE_REGS)+3 cycles, assuming no cache misses. */
3985 #define MAX_MOVE_REGS 4
3986 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3988 static void
3989 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src)
3990 rtx dest_reg; /* register holding destination address */
3991 rtx src_reg; /* register holding source address */
3992 unsigned int bytes; /* # bytes to move */
3993 int align; /* alignment */
3994 rtx orig_dest; /* original dest */
3995 rtx orig_src; /* original source for making a reg note */
3997 rtx dest_mem = replace_equiv_address (orig_dest, dest_reg);
3998 rtx src_mem = replace_equiv_address (orig_src, src_reg);
3999 rtx align_rtx = GEN_INT (align);
4000 rtx label;
4001 rtx final_src;
4002 rtx bytes_rtx;
4003 int leftover;
4005 if (bytes < (unsigned)2 * MAX_MOVE_BYTES)
4006 abort ();
4008 leftover = bytes % MAX_MOVE_BYTES;
4009 bytes -= leftover;
4011 label = gen_label_rtx ();
4012 final_src = gen_reg_rtx (Pmode);
4013 bytes_rtx = GEN_INT (bytes);
4015 if (bytes > 0x7fff)
4017 if (Pmode == DImode)
4019 emit_insn (gen_movdi (final_src, bytes_rtx));
4020 emit_insn (gen_adddi3 (final_src, final_src, src_reg));
4022 else
4024 emit_insn (gen_movsi (final_src, bytes_rtx));
4025 emit_insn (gen_addsi3 (final_src, final_src, src_reg));
4028 else
4030 if (Pmode == DImode)
4031 emit_insn (gen_adddi3 (final_src, src_reg, bytes_rtx));
4032 else
4033 emit_insn (gen_addsi3 (final_src, src_reg, bytes_rtx));
4036 emit_label (label);
4038 bytes_rtx = GEN_INT (MAX_MOVE_BYTES);
4039 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, bytes_rtx, align_rtx));
4041 if (Pmode == DImode)
4043 emit_insn (gen_adddi3 (src_reg, src_reg, bytes_rtx));
4044 emit_insn (gen_adddi3 (dest_reg, dest_reg, bytes_rtx));
4045 emit_insn (gen_cmpdi (src_reg, final_src));
4047 else
4049 emit_insn (gen_addsi3 (src_reg, src_reg, bytes_rtx));
4050 emit_insn (gen_addsi3 (dest_reg, dest_reg, bytes_rtx));
4051 emit_insn (gen_cmpsi (src_reg, final_src));
4054 emit_jump_insn (gen_bne (label));
4056 if (leftover)
4057 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, GEN_INT (leftover),
4058 align_rtx));
4061 /* Use a library function to move some bytes. */
4063 static void
4064 block_move_call (dest_reg, src_reg, bytes_rtx)
4065 rtx dest_reg;
4066 rtx src_reg;
4067 rtx bytes_rtx;
4069 /* We want to pass the size as Pmode, which will normally be SImode
4070 but will be DImode if we are using 64 bit longs and pointers. */
4071 if (GET_MODE (bytes_rtx) != VOIDmode
4072 && GET_MODE (bytes_rtx) != (unsigned) Pmode)
4073 bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
4075 #ifdef TARGET_MEM_FUNCTIONS
4076 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memcpy"), 0,
4077 VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
4078 convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
4079 TREE_UNSIGNED (sizetype)),
4080 TYPE_MODE (sizetype));
4081 #else
4082 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "bcopy"), 0,
4083 VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
4084 convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
4085 TREE_UNSIGNED (integer_type_node)),
4086 TYPE_MODE (integer_type_node));
4087 #endif
4090 /* Expand string/block move operations.
4092 operands[0] is the pointer to the destination.
4093 operands[1] is the pointer to the source.
4094 operands[2] is the number of bytes to move.
4095 operands[3] is the alignment. */
4097 void
4098 expand_block_move (operands)
4099 rtx operands[];
4101 rtx bytes_rtx = operands[2];
4102 rtx align_rtx = operands[3];
4103 int constp = GET_CODE (bytes_rtx) == CONST_INT;
4104 unsigned HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
4105 unsigned int align = INTVAL (align_rtx);
4106 rtx orig_src = operands[1];
4107 rtx orig_dest = operands[0];
4108 rtx src_reg;
4109 rtx dest_reg;
4111 if (constp && bytes == 0)
4112 return;
4114 if (align > (unsigned) UNITS_PER_WORD)
4115 align = UNITS_PER_WORD;
4117 /* Move the address into scratch registers. */
4118 dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
4119 src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
4121 if (TARGET_MEMCPY)
4122 block_move_call (dest_reg, src_reg, bytes_rtx);
4124 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES
4125 && align == (unsigned) UNITS_PER_WORD)
4126 move_by_pieces (orig_dest, orig_src, bytes, align * BITS_PER_WORD);
4128 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES)
4129 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
4130 dest_reg),
4131 replace_equiv_address (orig_src,
4132 src_reg),
4133 bytes_rtx, align_rtx));
4135 else if (constp && align >= (unsigned) UNITS_PER_WORD && optimize)
4136 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src);
4138 else if (constp && optimize)
4140 /* If the alignment is not word aligned, generate a test at
4141 runtime, to see whether things wound up aligned, and we
4142 can use the faster lw/sw instead ulw/usw. */
4144 rtx temp = gen_reg_rtx (Pmode);
4145 rtx aligned_label = gen_label_rtx ();
4146 rtx join_label = gen_label_rtx ();
4147 int leftover = bytes % MAX_MOVE_BYTES;
4149 bytes -= leftover;
4151 if (Pmode == DImode)
4153 emit_insn (gen_iordi3 (temp, src_reg, dest_reg));
4154 emit_insn (gen_anddi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
4155 emit_insn (gen_cmpdi (temp, const0_rtx));
4157 else
4159 emit_insn (gen_iorsi3 (temp, src_reg, dest_reg));
4160 emit_insn (gen_andsi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
4161 emit_insn (gen_cmpsi (temp, const0_rtx));
4164 emit_jump_insn (gen_beq (aligned_label));
4166 /* Unaligned loop. */
4167 block_move_loop (dest_reg, src_reg, bytes, 1, orig_dest, orig_src);
4168 emit_jump_insn (gen_jump (join_label));
4169 emit_barrier ();
4171 /* Aligned loop. */
4172 emit_label (aligned_label);
4173 block_move_loop (dest_reg, src_reg, bytes, UNITS_PER_WORD, orig_dest,
4174 orig_src);
4175 emit_label (join_label);
4177 /* Bytes at the end of the loop. */
4178 if (leftover)
4179 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
4180 dest_reg),
4181 replace_equiv_address (orig_src,
4182 src_reg),
4183 GEN_INT (leftover),
4184 GEN_INT (align)));
4187 else
4188 block_move_call (dest_reg, src_reg, bytes_rtx);
4191 /* Emit load/stores for a small constant block_move.
4193 operands[0] is the memory address of the destination.
4194 operands[1] is the memory address of the source.
4195 operands[2] is the number of bytes to move.
4196 operands[3] is the alignment.
4197 operands[4] is a temp register.
4198 operands[5] is a temp register.
4200 operands[3+num_regs] is the last temp register.
4202 The block move type can be one of the following:
4203 BLOCK_MOVE_NORMAL Do all of the block move.
4204 BLOCK_MOVE_NOT_LAST Do all but the last store.
4205 BLOCK_MOVE_LAST Do just the last store. */
4207 const char *
4208 output_block_move (insn, operands, num_regs, move_type)
4209 rtx insn;
4210 rtx operands[];
4211 int num_regs;
4212 enum block_move_type move_type;
4214 rtx dest_reg = XEXP (operands[0], 0);
4215 rtx src_reg = XEXP (operands[1], 0);
4216 HOST_WIDE_INT bytes = INTVAL (operands[2]);
4217 int align = INTVAL (operands[3]);
4218 int num = 0;
4219 int offset = 0;
4220 int use_lwl_lwr = 0;
4221 int last_operand = num_regs + 4;
4222 int safe_regs = 4;
4223 int i;
4224 rtx xoperands[10];
4226 struct {
4227 const char *load; /* load insn without nop */
4228 const char *load_nop; /* load insn with trailing nop */
4229 const char *store; /* store insn */
4230 const char *final; /* if last_store used: NULL or swr */
4231 const char *last_store; /* last store instruction */
4232 int offset; /* current offset */
4233 enum machine_mode mode; /* mode to use on (MEM) */
4234 } load_store[4];
4236 /* ??? Detect a bug in GCC, where it can give us a register
4237 the same as one of the addressing registers and reduce
4238 the number of registers available. */
4239 for (i = 4; i < last_operand && safe_regs < (int) ARRAY_SIZE (xoperands); i++)
4240 if (! reg_mentioned_p (operands[i], operands[0])
4241 && ! reg_mentioned_p (operands[i], operands[1]))
4242 xoperands[safe_regs++] = operands[i];
4244 if (safe_regs < last_operand)
4246 xoperands[0] = operands[0];
4247 xoperands[1] = operands[1];
4248 xoperands[2] = operands[2];
4249 xoperands[3] = operands[3];
4250 return output_block_move (insn, xoperands, safe_regs - 4, move_type);
4253 /* If we are given global or static addresses, and we would be
4254 emitting a few instructions, try to save time by using a
4255 temporary register for the pointer. */
4256 /* ??? The SGI Irix6 assembler fails when a SYMBOL_REF is used in
4257 an ldl/ldr instruction pair. We play it safe, and always move
4258 constant addresses into registers when generating N32/N64 code, just
4259 in case we might emit an unaligned load instruction. */
4260 if (num_regs > 2 && (bytes > 2 * align || move_type != BLOCK_MOVE_NORMAL
4261 || mips_abi == ABI_MEABI
4262 || mips_abi == ABI_N32
4263 || mips_abi == ABI_64))
4265 if (CONSTANT_P (src_reg))
4267 if (TARGET_STATS)
4268 mips_count_memory_refs (operands[1], 1);
4270 src_reg = operands[3 + num_regs--];
4271 if (move_type != BLOCK_MOVE_LAST)
4273 xoperands[1] = operands[1];
4274 xoperands[0] = src_reg;
4275 if (Pmode == DImode)
4276 output_asm_insn ("dla\t%0,%1", xoperands);
4277 else
4278 output_asm_insn ("la\t%0,%1", xoperands);
4282 if (CONSTANT_P (dest_reg))
4284 if (TARGET_STATS)
4285 mips_count_memory_refs (operands[0], 1);
4287 dest_reg = operands[3 + num_regs--];
4288 if (move_type != BLOCK_MOVE_LAST)
4290 xoperands[1] = operands[0];
4291 xoperands[0] = dest_reg;
4292 if (Pmode == DImode)
4293 output_asm_insn ("dla\t%0,%1", xoperands);
4294 else
4295 output_asm_insn ("la\t%0,%1", xoperands);
4300 /* ??? We really shouldn't get any LO_SUM addresses here, because they
4301 are not offsettable, however, offsettable_address_p says they are
4302 offsettable. I think this is a bug in offsettable_address_p.
4303 For expediency, we fix this by just loading the address into a register
4304 if we happen to get one. */
4306 if (GET_CODE (src_reg) == LO_SUM)
4308 src_reg = operands[3 + num_regs--];
4309 if (move_type != BLOCK_MOVE_LAST)
4311 xoperands[2] = XEXP (XEXP (operands[1], 0), 1);
4312 xoperands[1] = XEXP (XEXP (operands[1], 0), 0);
4313 xoperands[0] = src_reg;
4314 if (Pmode == DImode)
4315 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
4316 else
4317 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
4321 if (GET_CODE (dest_reg) == LO_SUM)
4323 dest_reg = operands[3 + num_regs--];
4324 if (move_type != BLOCK_MOVE_LAST)
4326 xoperands[2] = XEXP (XEXP (operands[0], 0), 1);
4327 xoperands[1] = XEXP (XEXP (operands[0], 0), 0);
4328 xoperands[0] = dest_reg;
4329 if (Pmode == DImode)
4330 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
4331 else
4332 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
4336 if (num_regs > (int) ARRAY_SIZE (load_store))
4337 num_regs = ARRAY_SIZE (load_store);
4339 else if (num_regs < 1)
4340 abort_with_insn (insn,
4341 "cannot do block move, not enough scratch registers");
4343 while (bytes > 0)
4345 load_store[num].offset = offset;
4347 if (TARGET_64BIT && bytes >= 8 && align >= 8)
4349 load_store[num].load = "ld\t%0,%1";
4350 load_store[num].load_nop = "ld\t%0,%1%#";
4351 load_store[num].store = "sd\t%0,%1";
4352 load_store[num].last_store = "sd\t%0,%1";
4353 load_store[num].final = 0;
4354 load_store[num].mode = DImode;
4355 offset += 8;
4356 bytes -= 8;
4359 /* ??? Fails because of a MIPS assembler bug? */
4360 else if (TARGET_64BIT && bytes >= 8
4361 && ! TARGET_SR71K
4362 && ! TARGET_MIPS16)
4364 if (BYTES_BIG_ENDIAN)
4366 load_store[num].load = "ldl\t%0,%1\n\tldr\t%0,%2";
4367 load_store[num].load_nop = "ldl\t%0,%1\n\tldr\t%0,%2%#";
4368 load_store[num].store = "sdl\t%0,%1\n\tsdr\t%0,%2";
4369 load_store[num].last_store = "sdr\t%0,%2";
4370 load_store[num].final = "sdl\t%0,%1";
4372 else
4374 load_store[num].load = "ldl\t%0,%2\n\tldr\t%0,%1";
4375 load_store[num].load_nop = "ldl\t%0,%2\n\tldr\t%0,%1%#";
4376 load_store[num].store = "sdl\t%0,%2\n\tsdr\t%0,%1";
4377 load_store[num].last_store = "sdr\t%0,%1";
4378 load_store[num].final = "sdl\t%0,%2";
4381 load_store[num].mode = DImode;
4382 offset += 8;
4383 bytes -= 8;
4384 use_lwl_lwr = 1;
4387 else if (bytes >= 4 && align >= 4)
4389 load_store[num].load = "lw\t%0,%1";
4390 load_store[num].load_nop = "lw\t%0,%1%#";
4391 load_store[num].store = "sw\t%0,%1";
4392 load_store[num].last_store = "sw\t%0,%1";
4393 load_store[num].final = 0;
4394 load_store[num].mode = SImode;
4395 offset += 4;
4396 bytes -= 4;
4399 else if (bytes >= 4
4400 && ! TARGET_SR71K
4401 && ! TARGET_MIPS16)
4403 if (BYTES_BIG_ENDIAN)
4405 load_store[num].load = "lwl\t%0,%1\n\tlwr\t%0,%2";
4406 load_store[num].load_nop = "lwl\t%0,%1\n\tlwr\t%0,%2%#";
4407 load_store[num].store = "swl\t%0,%1\n\tswr\t%0,%2";
4408 load_store[num].last_store = "swr\t%0,%2";
4409 load_store[num].final = "swl\t%0,%1";
4411 else
4413 load_store[num].load = "lwl\t%0,%2\n\tlwr\t%0,%1";
4414 load_store[num].load_nop = "lwl\t%0,%2\n\tlwr\t%0,%1%#";
4415 load_store[num].store = "swl\t%0,%2\n\tswr\t%0,%1";
4416 load_store[num].last_store = "swr\t%0,%1";
4417 load_store[num].final = "swl\t%0,%2";
4420 load_store[num].mode = SImode;
4421 offset += 4;
4422 bytes -= 4;
4423 use_lwl_lwr = 1;
4426 else if (bytes >= 2 && align >= 2)
4428 load_store[num].load = "lh\t%0,%1";
4429 load_store[num].load_nop = "lh\t%0,%1%#";
4430 load_store[num].store = "sh\t%0,%1";
4431 load_store[num].last_store = "sh\t%0,%1";
4432 load_store[num].final = 0;
4433 load_store[num].mode = HImode;
4434 offset += 2;
4435 bytes -= 2;
4437 else
4439 load_store[num].load = "lb\t%0,%1";
4440 load_store[num].load_nop = "lb\t%0,%1%#";
4441 load_store[num].store = "sb\t%0,%1";
4442 load_store[num].last_store = "sb\t%0,%1";
4443 load_store[num].final = 0;
4444 load_store[num].mode = QImode;
4445 offset++;
4446 bytes--;
4449 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
4451 dslots_load_total++;
4452 dslots_load_filled++;
4454 if (CONSTANT_P (src_reg))
4455 mips_count_memory_refs (src_reg, 1);
4457 if (CONSTANT_P (dest_reg))
4458 mips_count_memory_refs (dest_reg, 1);
4461 /* Emit load/stores now if we have run out of registers or are
4462 at the end of the move. */
4464 if (++num == num_regs || bytes == 0)
4466 /* If only load/store, we need a NOP after the load. */
4467 if (num == 1)
4469 load_store[0].load = load_store[0].load_nop;
4470 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
4471 dslots_load_filled--;
4474 if (move_type != BLOCK_MOVE_LAST)
4476 for (i = 0; i < num; i++)
4478 int offset;
4480 if (!operands[i + 4])
4481 abort ();
4483 if (GET_MODE (operands[i + 4]) != load_store[i].mode)
4484 operands[i + 4] = gen_rtx_REG (load_store[i].mode,
4485 REGNO (operands[i + 4]));
4487 offset = load_store[i].offset;
4488 xoperands[0] = operands[i + 4];
4489 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
4490 plus_constant (src_reg, offset));
4492 if (use_lwl_lwr)
4494 int extra_offset
4495 = GET_MODE_SIZE (load_store[i].mode) - 1;
4497 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
4498 plus_constant (src_reg,
4499 extra_offset
4500 + offset));
4503 output_asm_insn (load_store[i].load, xoperands);
4507 for (i = 0; i < num; i++)
4509 int last_p = (i == num-1 && bytes == 0);
4510 int offset = load_store[i].offset;
4512 xoperands[0] = operands[i + 4];
4513 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
4514 plus_constant (dest_reg, offset));
4517 if (use_lwl_lwr)
4519 int extra_offset = GET_MODE_SIZE (load_store[i].mode) - 1;
4520 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
4521 plus_constant (dest_reg,
4522 extra_offset
4523 + offset));
4526 if (move_type == BLOCK_MOVE_NORMAL)
4527 output_asm_insn (load_store[i].store, xoperands);
4529 else if (move_type == BLOCK_MOVE_NOT_LAST)
4531 if (!last_p)
4532 output_asm_insn (load_store[i].store, xoperands);
4534 else if (load_store[i].final != 0)
4535 output_asm_insn (load_store[i].final, xoperands);
4538 else if (last_p)
4539 output_asm_insn (load_store[i].last_store, xoperands);
4542 num = 0; /* reset load_store */
4543 use_lwl_lwr = 0;
4547 return "";
4550 /* Argument support functions. */
4552 /* Initialize CUMULATIVE_ARGS for a function. */
4554 void
4555 init_cumulative_args (cum, fntype, libname)
4556 CUMULATIVE_ARGS *cum; /* argument info to initialize */
4557 tree fntype; /* tree ptr for function decl */
4558 rtx libname ATTRIBUTE_UNUSED; /* SYMBOL_REF of library name or 0 */
4560 static CUMULATIVE_ARGS zero_cum;
4561 tree param, next_param;
4563 if (TARGET_DEBUG_E_MODE)
4565 fprintf (stderr,
4566 "\ninit_cumulative_args, fntype = 0x%.8lx", (long)fntype);
4568 if (!fntype)
4569 fputc ('\n', stderr);
4571 else
4573 tree ret_type = TREE_TYPE (fntype);
4574 fprintf (stderr, ", fntype code = %s, ret code = %s\n",
4575 tree_code_name[(int)TREE_CODE (fntype)],
4576 tree_code_name[(int)TREE_CODE (ret_type)]);
4580 *cum = zero_cum;
4581 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
4583 /* Determine if this function has variable arguments. This is
4584 indicated by the last argument being 'void_type_mode' if there
4585 are no variable arguments. The standard MIPS calling sequence
4586 passes all arguments in the general purpose registers in this case. */
4588 for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
4589 param != 0; param = next_param)
4591 next_param = TREE_CHAIN (param);
4592 if (next_param == 0 && TREE_VALUE (param) != void_type_node)
4593 cum->gp_reg_found = 1;
4597 static void
4598 mips_arg_info (cum, mode, type, named, info)
4599 const CUMULATIVE_ARGS *cum;
4600 enum machine_mode mode;
4601 tree type;
4602 int named;
4603 struct mips_arg_info *info;
4605 bool even_reg_p;
4606 unsigned int num_words, max_regs;
4608 info->struct_p = (type != 0
4609 && (TREE_CODE (type) == RECORD_TYPE
4610 || TREE_CODE (type) == UNION_TYPE
4611 || TREE_CODE (type) == QUAL_UNION_TYPE));
4613 /* Decide whether this argument should go in a floating-point register,
4614 assuming one is free. Later code checks for availablity. */
4616 info->fpr_p = false;
4617 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4618 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE)
4620 switch (mips_abi)
4622 case ABI_32:
4623 case ABI_O64:
4624 info->fpr_p = (!cum->gp_reg_found && cum->arg_number < 2);
4625 break;
4627 case ABI_EABI:
4628 info->fpr_p = true;
4629 break;
4631 case ABI_MEABI:
4632 /* The MIPS eabi says only structures containing doubles get
4633 passed in a fp register, so force a structure containing
4634 a float to be passed in the integer registers. */
4635 info->fpr_p = (named && !(mode == SFmode && info->struct_p));
4636 break;
4638 default:
4639 info->fpr_p = named;
4640 break;
4644 /* Now decide whether the argument must go in an even-numbered register. */
4646 even_reg_p = false;
4647 if (info->fpr_p)
4649 /* Under the O64 ABI, the second float argument goes in $f13 if it
4650 is a double, but $f14 if it is a single. Otherwise, on a
4651 32-bit double-float machine, each FP argument must start in a
4652 new register pair. */
4653 even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_HWFPVALUE
4654 || (mips_abi == ABI_O64 && mode == SFmode)
4655 || FP_INC > 1);
4657 else if (!TARGET_64BIT || LONG_DOUBLE_TYPE_SIZE == 128)
4659 if (GET_MODE_CLASS (mode) == MODE_INT
4660 || GET_MODE_CLASS (mode) == MODE_FLOAT)
4661 even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_WORD);
4663 else if (type != NULL_TREE && TYPE_ALIGN (type) > BITS_PER_WORD)
4664 even_reg_p = true;
4667 /* Set REG_OFFSET to the register count we're interested in.
4668 The EABI allocates the floating-point registers separately,
4669 but the other ABIs allocate them like integer registers. */
4670 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
4671 ? cum->num_fprs
4672 : cum->num_gprs);
4674 if (even_reg_p)
4675 info->reg_offset += info->reg_offset & 1;
4677 /* The alignment applied to registers is also applied to stack arguments. */
4678 info->stack_offset = cum->stack_words;
4679 if (even_reg_p)
4680 info->stack_offset += info->stack_offset & 1;
4682 if (mode == BLKmode)
4683 info->num_bytes = int_size_in_bytes (type);
4684 else
4685 info->num_bytes = GET_MODE_SIZE (mode);
4687 num_words = (info->num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4688 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
4690 /* Partition the argument between registers and stack. */
4691 info->reg_words = MIN (num_words, max_regs);
4692 info->stack_words = num_words - info->reg_words;
4696 /* Advance the argument to the next argument position. */
4698 void
4699 function_arg_advance (cum, mode, type, named)
4700 CUMULATIVE_ARGS *cum; /* current arg information */
4701 enum machine_mode mode; /* current arg mode */
4702 tree type; /* type of the argument or 0 if lib support */
4703 int named; /* whether or not the argument was named */
4705 struct mips_arg_info info;
4707 mips_arg_info (cum, mode, type, named, &info);
4709 /* The following is a hack in order to pass 1 byte structures
4710 the same way that the MIPS compiler does (namely by passing
4711 the structure in the high byte or half word of the register).
4712 This also makes varargs work. If we have such a structure,
4713 we save the adjustment RTL, and the call define expands will
4714 emit them. For the VOIDmode argument (argument after the
4715 last real argument), pass back a parallel vector holding each
4716 of the adjustments. */
4718 /* ??? This scheme requires everything smaller than the word size to
4719 shifted to the left, but when TARGET_64BIT and ! TARGET_INT64,
4720 that would mean every int needs to be shifted left, which is very
4721 inefficient. Let's not carry this compatibility to the 64 bit
4722 calling convention for now. */
4724 if (info.struct_p
4725 && info.reg_words == 1
4726 && info.num_bytes < UNITS_PER_WORD
4727 && !TARGET_64BIT
4728 && mips_abi != ABI_EABI
4729 && mips_abi != ABI_MEABI)
4731 rtx amount = GEN_INT (BITS_PER_WORD - info.num_bytes * BITS_PER_UNIT);
4732 rtx reg = gen_rtx_REG (word_mode, GP_ARG_FIRST + info.reg_offset);
4734 if (TARGET_64BIT)
4735 cum->adjust[cum->num_adjusts++] = PATTERN (gen_ashldi3 (reg, reg, amount));
4736 else
4737 cum->adjust[cum->num_adjusts++] = PATTERN (gen_ashlsi3 (reg, reg, amount));
4740 if (!info.fpr_p)
4741 cum->gp_reg_found = true;
4743 /* See the comment above the cumulative args structure in mips.h
4744 for an explanation of what this code does. It assumes the O32
4745 ABI, which passes at most 2 arguments in float registers. */
4746 if (cum->arg_number < 2 && info.fpr_p)
4747 cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
4749 if (mips_abi != ABI_EABI || !info.fpr_p)
4750 cum->num_gprs = info.reg_offset + info.reg_words;
4751 else if (info.reg_words > 0)
4752 cum->num_fprs += FP_INC;
4754 if (info.stack_words > 0)
4755 cum->stack_words = info.stack_offset + info.stack_words;
4757 cum->arg_number++;
4760 /* Return an RTL expression containing the register for the given mode,
4761 or 0 if the argument is to be passed on the stack. */
4763 struct rtx_def *
4764 function_arg (cum, mode, type, named)
4765 const CUMULATIVE_ARGS *cum; /* current arg information */
4766 enum machine_mode mode; /* current arg mode */
4767 tree type; /* type of the argument or 0 if lib support */
4768 int named; /* != 0 for normal args, == 0 for ... args */
4770 struct mips_arg_info info;
4772 /* We will be called with a mode of VOIDmode after the last argument
4773 has been seen. Whatever we return will be passed to the call
4774 insn. If we need any shifts for small structures, return them in
4775 a PARALLEL; in that case, stuff the mips16 fp_code in as the
4776 mode. Otherwise, if we need a mips16 fp_code, return a REG
4777 with the code stored as the mode. */
4778 if (mode == VOIDmode)
4780 if (cum->num_adjusts > 0)
4781 return gen_rtx_PARALLEL ((enum machine_mode) cum->fp_code,
4782 gen_rtvec_v (cum->num_adjusts,
4783 (rtx *) cum->adjust));
4785 else if (TARGET_MIPS16 && cum->fp_code != 0)
4786 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
4788 else
4789 return 0;
4792 mips_arg_info (cum, mode, type, named, &info);
4794 /* Return straight away if the whole argument is passed on the stack. */
4795 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
4796 return 0;
4798 if (type != 0
4799 && TREE_CODE (type) == RECORD_TYPE
4800 && (mips_abi == ABI_N32 || mips_abi == ABI_64)
4801 && TYPE_SIZE_UNIT (type)
4802 && host_integerp (TYPE_SIZE_UNIT (type), 1)
4803 && named
4804 && mode != DFmode)
4806 /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
4807 structure contains a double in its entirety, then that 64 bit
4808 chunk is passed in a floating point register. */
4809 tree field;
4811 /* First check to see if there is any such field. */
4812 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4813 if (TREE_CODE (field) == FIELD_DECL
4814 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4815 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4816 && host_integerp (bit_position (field), 0)
4817 && int_bit_position (field) % BITS_PER_WORD == 0)
4818 break;
4820 if (field != 0)
4822 /* Now handle the special case by returning a PARALLEL
4823 indicating where each 64 bit chunk goes. INFO.REG_WORDS
4824 chunks are passed in registers. */
4825 unsigned int i;
4826 HOST_WIDE_INT bitpos;
4827 rtx ret;
4829 /* assign_parms checks the mode of ENTRY_PARM, so we must
4830 use the actual mode here. */
4831 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
4833 bitpos = 0;
4834 field = TYPE_FIELDS (type);
4835 for (i = 0; i < info.reg_words; i++)
4837 rtx reg;
4839 for (; field; field = TREE_CHAIN (field))
4840 if (TREE_CODE (field) == FIELD_DECL
4841 && int_bit_position (field) >= bitpos)
4842 break;
4844 if (field
4845 && int_bit_position (field) == bitpos
4846 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4847 && !TARGET_SOFT_FLOAT
4848 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4849 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
4850 else
4851 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
4853 XVECEXP (ret, 0, i)
4854 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4855 GEN_INT (bitpos / BITS_PER_UNIT));
4857 bitpos += BITS_PER_WORD;
4859 return ret;
4863 if (mips_abi == ABI_MEABI && info.fpr_p && !cum->prototype)
4865 /* To make K&R varargs work we need to pass floating
4866 point arguments in both integer and FP registers. */
4867 return gen_rtx_PARALLEL
4868 (mode,
4869 gen_rtvec (2,
4870 gen_rtx_EXPR_LIST (VOIDmode,
4871 gen_rtx_REG (mode,
4872 GP_ARG_FIRST
4873 + info.reg_offset),
4874 const0_rtx),
4875 gen_rtx_EXPR_LIST (VOIDmode,
4876 gen_rtx_REG (mode,
4877 FP_ARG_FIRST
4878 + info.reg_offset),
4879 const0_rtx)));
4882 if (info.fpr_p)
4883 return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
4884 else
4885 return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
4889 function_arg_partial_nregs (cum, mode, type, named)
4890 const CUMULATIVE_ARGS *cum; /* current arg information */
4891 enum machine_mode mode; /* current arg mode */
4892 tree type; /* type of the argument or 0 if lib support */
4893 int named; /* != 0 for normal args, == 0 for ... args */
4895 struct mips_arg_info info;
4897 mips_arg_info (cum, mode, type, named, &info);
4898 return info.stack_words > 0 ? info.reg_words : 0;
4902 mips_setup_incoming_varargs (cum, mode, type, no_rtl)
4903 const CUMULATIVE_ARGS *cum;
4904 enum machine_mode mode;
4905 tree type;
4906 int no_rtl;
4908 CUMULATIVE_ARGS local_cum;
4909 int gp_saved, fp_saved;
4911 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
4912 return 0;
4914 /* The caller has advanced CUM up to, but not beyond, the last named
4915 argument. Advance a local copy of CUM past the last "real" named
4916 argument, to find out how many registers are left over. */
4918 local_cum = *cum;
4919 FUNCTION_ARG_ADVANCE (local_cum, mode, type, 1);
4921 /* Found out how many registers we need to save. */
4922 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
4923 fp_saved = (EABI_FLOAT_VARARGS_P
4924 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
4925 : 0);
4927 if (!no_rtl)
4929 if (gp_saved > 0)
4931 rtx ptr, mem;
4933 ptr = virtual_incoming_args_rtx;
4934 if (mips_abi == ABI_EABI)
4935 ptr = plus_constant (ptr, -gp_saved * UNITS_PER_WORD);
4936 mem = gen_rtx_MEM (BLKmode, ptr);
4938 /* va_arg is an array access in this case, which causes
4939 it to get MEM_IN_STRUCT_P set. We must set it here
4940 so that the insn scheduler won't assume that these
4941 stores can't possibly overlap with the va_arg loads. */
4942 if (mips_abi != ABI_EABI && BYTES_BIG_ENDIAN)
4943 MEM_SET_IN_STRUCT_P (mem, 1);
4945 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST, mem,
4946 gp_saved, gp_saved * UNITS_PER_WORD);
4948 if (fp_saved > 0)
4950 /* We can't use move_block_from_reg, because it will use
4951 the wrong mode. */
4952 enum machine_mode mode;
4953 int off, i;
4955 /* Set OFF to the offset from virtual_incoming_args_rtx of
4956 the first float register. The FP save area lies below
4957 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
4958 off = -gp_saved * UNITS_PER_WORD;
4959 off &= ~(UNITS_PER_FPVALUE - 1);
4960 off -= fp_saved * UNITS_PER_FPREG;
4962 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
4964 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS; i += FP_INC)
4966 rtx ptr = plus_constant (virtual_incoming_args_rtx, off);
4967 emit_move_insn (gen_rtx_MEM (mode, ptr),
4968 gen_rtx_REG (mode, FP_ARG_FIRST + i));
4969 off += UNITS_PER_HWFPVALUE;
4973 return (gp_saved * UNITS_PER_WORD) + (fp_saved * UNITS_PER_FPREG);
4976 /* Create the va_list data type.
4977 We keep 3 pointers, and two offsets.
4978 Two pointers are to the overflow area, which starts at the CFA.
4979 One of these is constant, for addressing into the GPR save area below it.
4980 The other is advanced up the stack through the overflow region.
4981 The third pointer is to the GPR save area. Since the FPR save area
4982 is just below it, we can address FPR slots off this pointer.
4983 We also keep two one-byte offsets, which are to be subtracted from the
4984 constant pointers to yield addresses in the GPR and FPR save areas.
4985 These are downcounted as float or non-float arguments are used,
4986 and when they get to zero, the argument must be obtained from the
4987 overflow region.
4988 If !EABI_FLOAT_VARARGS_P, then no FPR save area exists, and a single
4989 pointer is enough. It's started at the GPR save area, and is
4990 advanced, period.
4991 Note that the GPR save area is not constant size, due to optimization
4992 in the prologue. Hence, we can't use a design with two pointers
4993 and two offsets, although we could have designed this with two pointers
4994 and three offsets. */
4997 tree
4998 mips_build_va_list ()
5000 if (EABI_FLOAT_VARARGS_P)
5002 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, record;
5004 record = make_node (RECORD_TYPE);
5006 f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
5007 ptr_type_node);
5008 f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
5009 ptr_type_node);
5010 f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
5011 ptr_type_node);
5012 f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
5013 unsigned_char_type_node);
5014 f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
5015 unsigned_char_type_node);
5018 DECL_FIELD_CONTEXT (f_ovfl) = record;
5019 DECL_FIELD_CONTEXT (f_gtop) = record;
5020 DECL_FIELD_CONTEXT (f_ftop) = record;
5021 DECL_FIELD_CONTEXT (f_goff) = record;
5022 DECL_FIELD_CONTEXT (f_foff) = record;
5024 TYPE_FIELDS (record) = f_ovfl;
5025 TREE_CHAIN (f_ovfl) = f_gtop;
5026 TREE_CHAIN (f_gtop) = f_ftop;
5027 TREE_CHAIN (f_ftop) = f_goff;
5028 TREE_CHAIN (f_goff) = f_foff;
5030 layout_type (record);
5031 return record;
5033 else
5034 return ptr_type_node;
5037 /* Implement va_start. stdarg_p is always 1. */
5039 void
5040 mips_va_start (valist, nextarg)
5041 tree valist;
5042 rtx nextarg;
5044 const CUMULATIVE_ARGS *cum = &current_function_args_info;
5046 /* ARG_POINTER_REGNUM is initialized to STACK_POINTER_BOUNDARY, but
5047 since the stack is aligned for a pair of argument-passing slots,
5048 and the beginning of a variable argument list may be an odd slot,
5049 we have to decrease its alignment. */
5050 if (cfun && cfun->emit->regno_pointer_align)
5051 while (((current_function_pretend_args_size * BITS_PER_UNIT)
5052 & (REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) - 1)) != 0)
5053 REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) /= 2;
5055 if (mips_abi == ABI_EABI)
5057 int gpr_save_area_size;
5059 gpr_save_area_size
5060 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5062 if (EABI_FLOAT_VARARGS_P)
5064 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5065 tree ovfl, gtop, ftop, goff, foff;
5066 tree t;
5067 int fpr_offset;
5068 int fpr_save_area_size;
5070 f_ovfl = TYPE_FIELDS (va_list_type_node);
5071 f_gtop = TREE_CHAIN (f_ovfl);
5072 f_ftop = TREE_CHAIN (f_gtop);
5073 f_goff = TREE_CHAIN (f_ftop);
5074 f_foff = TREE_CHAIN (f_goff);
5076 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
5077 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
5078 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
5079 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
5080 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
5082 /* Emit code to initialize OVFL, which points to the next varargs
5083 stack argument. CUM->STACK_WORDS gives the number of stack
5084 words used by named arguments. */
5085 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5086 if (cum->stack_words > 0)
5087 t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
5088 build_int_2 (cum->stack_words * UNITS_PER_WORD, 0));
5089 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5090 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5092 /* Emit code to initialize GTOP, the top of the GPR save area. */
5093 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5094 t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5095 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5097 /* Emit code to initialize FTOP, the top of the FPR save area.
5098 This address is gpr_save_area_bytes below GTOP, rounded
5099 down to the next fp-aligned boundary. */
5100 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5101 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5102 fpr_offset &= ~(UNITS_PER_FPVALUE - 1);
5103 if (fpr_offset)
5104 t = build (PLUS_EXPR, TREE_TYPE (ftop), t,
5105 build_int_2 (-fpr_offset, -1));
5106 t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5107 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5109 /* Emit code to initialize GOFF, the offset from GTOP of the
5110 next GPR argument. */
5111 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
5112 build_int_2 (gpr_save_area_size, 0));
5113 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5115 /* Likewise emit code to initialize FOFF, the offset from FTOP
5116 of the next FPR argument. */
5117 fpr_save_area_size
5118 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5119 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
5120 build_int_2 (fpr_save_area_size, 0));
5121 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5123 else
5125 /* Everything is in the GPR save area, or in the overflow
5126 area which is contiguous with it. */
5127 nextarg = plus_constant (nextarg, -gpr_save_area_size);
5128 std_expand_builtin_va_start (valist, nextarg);
5131 else
5132 std_expand_builtin_va_start (valist, nextarg);
5135 /* Implement va_arg. */
5138 mips_va_arg (valist, type)
5139 tree valist, type;
5141 HOST_WIDE_INT size, rsize;
5142 rtx addr_rtx;
5143 tree t;
5145 size = int_size_in_bytes (type);
5146 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5148 if (mips_abi == ABI_EABI)
5150 bool indirect;
5151 rtx r;
5153 indirect
5154 = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5156 if (indirect)
5158 size = POINTER_SIZE / BITS_PER_UNIT;
5159 rsize = UNITS_PER_WORD;
5162 addr_rtx = gen_reg_rtx (Pmode);
5164 if (!EABI_FLOAT_VARARGS_P)
5166 /* Case of all args in a merged stack. No need to check bounds,
5167 just advance valist along the stack. */
5169 tree gpr = valist;
5170 if (!indirect
5171 && !TARGET_64BIT
5172 && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
5174 /* Align the pointer using: ap = (ap + align - 1) & -align,
5175 where align is 2 * UNITS_PER_WORD. */
5176 t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
5177 build_int_2 (2 * UNITS_PER_WORD - 1, 0));
5178 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
5179 build_int_2 (-2 * UNITS_PER_WORD, -1));
5180 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
5181 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5184 /* Emit code to set addr_rtx to the valist, and postincrement
5185 the valist by the size of the argument, rounded up to the
5186 next word. */
5187 t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
5188 size_int (rsize));
5189 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5190 if (r != addr_rtx)
5191 emit_move_insn (addr_rtx, r);
5193 /* Flush the POSTINCREMENT. */
5194 emit_queue();
5196 else
5198 /* Not a simple merged stack. */
5200 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5201 tree ovfl, top, off;
5202 rtx lab_over = NULL_RTX, lab_false;
5203 HOST_WIDE_INT osize;
5205 f_ovfl = TYPE_FIELDS (va_list_type_node);
5206 f_gtop = TREE_CHAIN (f_ovfl);
5207 f_ftop = TREE_CHAIN (f_gtop);
5208 f_goff = TREE_CHAIN (f_ftop);
5209 f_foff = TREE_CHAIN (f_goff);
5211 /* We maintain separate pointers and offsets for floating-point
5212 and integer arguments, but we need similar code in both cases.
5213 Let:
5215 TOP be the top of the register save area;
5216 OFF be the offset from TOP of the next register;
5217 ADDR_RTX be the address of the argument; and
5218 RSIZE be the number of bytes used to store the argument
5219 when it's in the register save area
5220 OSIZE be the number of bytes used to store it when it's
5221 in the stack overflow area
5222 PADDING be (BYTES_BIG_ENDIAN ? OSIZE - RSIZE : 0)
5224 The code we want is:
5226 1: off &= -rsize; // round down
5227 2: if (off != 0)
5228 3: {
5229 4: addr_rtx = top - off;
5230 5: off -= rsize;
5231 6: }
5232 7: else
5233 8: {
5234 9: ovfl += ((intptr_t) ovfl + osize - 1) & -osize;
5235 10: addr_rtx = ovfl + PADDING;
5236 11: ovfl += osize;
5237 14: }
5239 [1] and [9] can sometimes be optimized away. */
5241 lab_false = gen_label_rtx ();
5242 lab_over = gen_label_rtx ();
5244 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
5246 if (TREE_CODE (type) == REAL_TYPE)
5248 top = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
5249 off = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
5251 /* When floating-point registers are saved to the stack,
5252 each one will take up UNITS_PER_HWFPVALUE bytes, regardless
5253 of the float's precision. */
5254 rsize = UNITS_PER_HWFPVALUE;
5256 else
5258 top = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
5259 off = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
5260 if (rsize > UNITS_PER_WORD)
5262 /* [1] Emit code for: off &= -rsize. */
5263 t = build (BIT_AND_EXPR, TREE_TYPE (off), off,
5264 build_int_2 (-rsize, -1));
5265 t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
5266 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5269 /* Every overflow argument must take up at least UNITS_PER_WORD
5270 bytes (= PARM_BOUNDARY bits). RSIZE can sometimes be smaller
5271 than that, such as in the combination -mgp64 -msingle-float
5272 -fshort-double. Doubles passed in registers will then take
5273 up UNITS_PER_HWFPVALUE bytes, but those passed on the stack
5274 take up UNITS_PER_WORD bytes. */
5275 osize = MAX (rsize, UNITS_PER_WORD);
5277 /* [2] Emit code to branch if off == 0. */
5278 r = expand_expr (off, NULL_RTX, TYPE_MODE (TREE_TYPE (off)),
5279 EXPAND_NORMAL);
5280 emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
5281 1, lab_false);
5283 /* [4] Emit code for: addr_rtx = top - off. */
5284 t = build (MINUS_EXPR, TREE_TYPE (top), top, off);
5285 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5286 if (r != addr_rtx)
5287 emit_move_insn (addr_rtx, r);
5289 /* [5] Emit code for: off -= rsize. */
5290 t = build (MINUS_EXPR, TREE_TYPE (off), off, build_int_2 (rsize, 0));
5291 t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
5292 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5294 /* [7] Emit code to jump over the else clause, then the label
5295 that starts it. */
5296 emit_queue();
5297 emit_jump (lab_over);
5298 emit_barrier ();
5299 emit_label (lab_false);
5301 if (osize > UNITS_PER_WORD)
5303 /* [9] Emit: ovfl += ((intptr_t) ovfl + osize - 1) & -osize. */
5304 t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl,
5305 build_int_2 (osize - 1, 0));
5306 t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), t,
5307 build_int_2 (-osize, -1));
5308 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5309 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5312 /* [10, 11]. Emit code to store ovfl in addr_rtx, then
5313 post-increment ovfl by osize. On big-endian machines,
5314 the argument has OSIZE - RSIZE bytes of leading padding. */
5315 t = build (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl,
5316 size_int (osize));
5317 if (BYTES_BIG_ENDIAN && osize > rsize)
5318 t = build (PLUS_EXPR, TREE_TYPE (t), t,
5319 build_int_2 (osize - rsize, 0));
5320 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5321 if (r != addr_rtx)
5322 emit_move_insn (addr_rtx, r);
5324 emit_queue();
5325 emit_label (lab_over);
5327 if (BYTES_BIG_ENDIAN && rsize != size)
5328 addr_rtx = plus_constant (addr_rtx, rsize - size);
5329 if (indirect)
5331 addr_rtx = force_reg (Pmode, addr_rtx);
5332 r = gen_rtx_MEM (Pmode, addr_rtx);
5333 set_mem_alias_set (r, get_varargs_alias_set ());
5334 emit_move_insn (addr_rtx, r);
5336 return addr_rtx;
5338 else
5340 /* Not EABI. */
5341 int align;
5343 /* ??? The original va-mips.h did always align, despite the fact
5344 that alignments <= UNITS_PER_WORD are preserved by the va_arg
5345 increment mechanism. */
5347 if ((mips_abi == ABI_N32 || mips_abi == ABI_64)
5348 && TYPE_ALIGN (type) > 64)
5349 align = 16;
5350 else if (TARGET_64BIT)
5351 align = 8;
5352 else if (TYPE_ALIGN (type) > 32)
5353 align = 8;
5354 else
5355 align = 4;
5357 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
5358 build_int_2 (align - 1, 0));
5359 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
5360 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5361 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5363 /* Everything past the alignment is standard. */
5364 return std_expand_builtin_va_arg (valist, type);
5368 /* Abort after printing out a specific insn. */
5370 static void
5371 abort_with_insn (insn, reason)
5372 rtx insn;
5373 const char *reason;
5375 error (reason);
5376 debug_rtx (insn);
5377 abort ();
5380 /* Set up globals to generate code for the ISA or processor
5381 described by INFO. */
5383 static void
5384 mips_set_architecture (info)
5385 const struct mips_cpu_info *info;
5387 if (info != 0)
5389 mips_arch_info = info;
5390 mips_arch = info->cpu;
5391 mips_isa = info->isa;
5396 /* Likewise for tuning. */
5398 static void
5399 mips_set_tune (info)
5400 const struct mips_cpu_info *info;
5402 if (info != 0)
5404 mips_tune_info = info;
5405 mips_tune = info->cpu;
5410 /* Set up the threshold for data to go into the small data area, instead
5411 of the normal data area, and detect any conflicts in the switches. */
5413 void
5414 override_options ()
5416 int i, start, regno;
5417 enum machine_mode mode;
5419 mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
5421 if (mips_section_threshold <= 0)
5422 target_flags &= ~MASK_GPOPT;
5423 else if (optimize)
5424 target_flags |= MASK_GPOPT;
5426 /* If both single-float and soft-float are set, then clear the one that
5427 was set by TARGET_DEFAULT, leaving the one that was set by the
5428 user. We assume here that the specs prevent both being set by the
5429 user. */
5430 #ifdef TARGET_DEFAULT
5431 if (TARGET_SINGLE_FLOAT && TARGET_SOFT_FLOAT)
5432 target_flags &= ~((TARGET_DEFAULT) & (MASK_SOFT_FLOAT | MASK_SINGLE_FLOAT));
5433 #endif
5435 /* Interpret -mabi. */
5436 mips_abi = MIPS_ABI_DEFAULT;
5437 if (mips_abi_string != 0)
5439 if (strcmp (mips_abi_string, "32") == 0)
5440 mips_abi = ABI_32;
5441 else if (strcmp (mips_abi_string, "o64") == 0)
5442 mips_abi = ABI_O64;
5443 else if (strcmp (mips_abi_string, "n32") == 0)
5444 mips_abi = ABI_N32;
5445 else if (strcmp (mips_abi_string, "64") == 0)
5446 mips_abi = ABI_64;
5447 else if (strcmp (mips_abi_string, "eabi") == 0)
5448 mips_abi = ABI_EABI;
5449 else if (strcmp (mips_abi_string, "meabi") == 0)
5450 mips_abi = ABI_MEABI;
5451 else
5452 fatal_error ("bad value (%s) for -mabi= switch", mips_abi_string);
5455 /* The following code determines the architecture and register size.
5456 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
5457 The GAS and GCC code should be kept in sync as much as possible. */
5459 if (mips_arch_string != 0)
5460 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
5462 if (mips_tune_string != 0)
5463 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
5465 if (mips_isa_string != 0)
5467 /* Handle -mipsN. */
5469 if (strcmp (mips_isa_string, "16") == 0)
5471 /* -mips16 specifies an ASE rather than a processor, so don't
5472 change mips_arch here. -mno-mips16 overrides -mips16. */
5473 if (mips_no_mips16_string == NULL)
5474 target_flags |= MASK_MIPS16;
5476 else
5478 char *whole_isa_str = concat ("mips", mips_isa_string, NULL);
5479 const struct mips_cpu_info *isa_info;
5481 isa_info = mips_parse_cpu ("-mips option", whole_isa_str);
5482 free (whole_isa_str);
5484 /* -march takes precedence over -mipsN, since it is more descriptive.
5485 There's no harm in specifying both as long as the ISA levels
5486 are the same. */
5487 if (mips_arch_info != 0 && mips_isa != isa_info->isa)
5488 error ("-mips%s conflicts with the other architecture options, which specify a MIPS%d processor",
5489 mips_isa_string, mips_isa);
5491 /* Set architecture based on the given option. */
5492 mips_set_architecture (isa_info);
5496 if (mips_arch_info == 0)
5498 #ifdef MIPS_CPU_STRING_DEFAULT
5499 mips_set_architecture (mips_parse_cpu ("default CPU",
5500 MIPS_CPU_STRING_DEFAULT));
5501 #else
5502 mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
5503 #endif
5506 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
5507 error ("-march=%s is not compatible with the selected ABI",
5508 mips_arch_info->name);
5510 /* Optimize for mips_arch, unless -mtune selects a different processor. */
5511 if (mips_tune_info == 0)
5512 mips_set_tune (mips_arch_info);
5514 if ((target_flags_explicit & MASK_64BIT) != 0)
5516 /* The user specified the size of the integer registers. Make sure
5517 it agrees with the ABI and ISA. */
5518 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
5519 error ("-mgp64 used with a 32-bit processor");
5520 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
5521 error ("-mgp32 used with a 64-bit ABI");
5522 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
5523 error ("-mgp64 used with a 32-bit ABI");
5525 else
5527 /* Infer the integer register size from the ABI and processor.
5528 Restrict ourselves to 32-bit registers if that's all the
5529 processor has, or if the ABI cannot handle 64-bit registers. */
5530 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
5531 target_flags &= ~MASK_64BIT;
5532 else
5533 target_flags |= MASK_64BIT;
5536 if ((target_flags_explicit & MASK_FLOAT64) != 0)
5538 /* Really, -mfp32 and -mfp64 are ornamental options. There's
5539 only one right answer here. */
5540 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
5541 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
5542 else if (!TARGET_64BIT && TARGET_FLOAT64)
5543 error ("unsupported combination: %s", "-mgp32 -mfp64");
5544 else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
5545 error ("unsupported combination: %s", "-mfp64 -msingle-float");
5547 else
5549 /* -msingle-float selects 32-bit float registers. Otherwise the
5550 float registers should be the same size as the integer ones. */
5551 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
5552 target_flags |= MASK_FLOAT64;
5553 else
5554 target_flags &= ~MASK_FLOAT64;
5557 /* End of code shared with GAS. */
5559 if ((target_flags_explicit & MASK_LONG64) == 0)
5561 /* If no type size setting options (-mlong64,-mint64,-mlong32)
5562 were used, then set the type sizes. In the EABI in 64 bit mode,
5563 longs and pointers are 64 bits. Likewise for the SGI Irix6 N64
5564 ABI. */
5565 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
5566 target_flags |= MASK_LONG64;
5567 else
5568 target_flags &= ~MASK_LONG64;
5571 if (MIPS_MARCH_CONTROLS_SOFT_FLOAT
5572 && (target_flags_explicit & MASK_SOFT_FLOAT) == 0)
5574 /* For some configurations, it is useful to have -march control
5575 the default setting of MASK_SOFT_FLOAT. */
5576 switch ((int) mips_arch)
5578 case PROCESSOR_R4100:
5579 case PROCESSOR_R4120:
5580 target_flags |= MASK_SOFT_FLOAT;
5581 break;
5583 default:
5584 target_flags &= ~MASK_SOFT_FLOAT;
5585 break;
5589 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
5590 flag_pcc_struct_return = 0;
5592 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
5594 /* If neither -mbranch-likely nor -mno-branch-likely was given
5595 on the command line, set MASK_BRANCHLIKELY based on the target
5596 architecture.
5598 By default, we enable use of Branch Likely instructions on
5599 all architectures which support them except for MIPS32 and MIPS64
5600 (i.e., the generic MIPS32 and MIPS64 ISAs, and processors which
5601 implement them).
5603 The MIPS32 and MIPS64 architecture specifications say "Software
5604 is strongly encouraged to avoid use of Branch Likely
5605 instructions, as they will be removed from a future revision
5606 of the [MIPS32 and MIPS64] architecture." Therefore, we do not
5607 issue those instructions unless instructed to do so by
5608 -mbranch-likely. */
5609 if (ISA_HAS_BRANCHLIKELY && !(ISA_MIPS32 || ISA_MIPS32R2 || ISA_MIPS64))
5610 target_flags |= MASK_BRANCHLIKELY;
5611 else
5612 target_flags &= ~MASK_BRANCHLIKELY;
5614 if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
5615 warning ("generation of Branch Likely instructions enabled, but not supported by architecture");
5617 /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined. We need
5618 to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work. */
5619 /* ??? -non_shared turns off pic code generation, but this is not
5620 implemented. */
5621 if (TARGET_ABICALLS)
5623 mips_abicalls = MIPS_ABICALLS_YES;
5624 flag_pic = 1;
5625 if (mips_section_threshold > 0)
5626 warning ("-G is incompatible with PIC code which is the default");
5628 else
5629 mips_abicalls = MIPS_ABICALLS_NO;
5631 /* -membedded-pic is a form of PIC code suitable for embedded
5632 systems. All calls are made using PC relative addressing, and
5633 all data is addressed using the $gp register. This requires gas,
5634 which does most of the work, and GNU ld, which automatically
5635 expands PC relative calls which are out of range into a longer
5636 instruction sequence. All gcc really does differently is
5637 generate a different sequence for a switch. */
5638 if (TARGET_EMBEDDED_PIC)
5640 flag_pic = 1;
5641 if (TARGET_ABICALLS)
5642 warning ("-membedded-pic and -mabicalls are incompatible");
5644 if (g_switch_set)
5645 warning ("-G and -membedded-pic are incompatible");
5647 /* Setting mips_section_threshold is not required, because gas
5648 will force everything to be GP addressable anyhow, but
5649 setting it will cause gcc to make better estimates of the
5650 number of instructions required to access a particular data
5651 item. */
5652 mips_section_threshold = 0x7fffffff;
5655 /* This optimization requires a linker that can support a R_MIPS_LO16
5656 relocation which is not immediately preceded by a R_MIPS_HI16 relocation.
5657 GNU ld has this support, but not all other MIPS linkers do, so we enable
5658 this optimization only if the user requests it, or if GNU ld is the
5659 standard linker for this configuration. */
5660 /* ??? This does not work when target addresses are DImode.
5661 This is because we are missing DImode high/lo_sum patterns. */
5662 if (TARGET_GAS && ! TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES && optimize && ! flag_pic
5663 && Pmode == SImode)
5664 mips_split_addresses = 1;
5665 else
5666 mips_split_addresses = 0;
5668 /* -mrnames says to use the MIPS software convention for register
5669 names instead of the hardware names (ie, $a0 instead of $4).
5670 We do this by switching the names in mips_reg_names, which the
5671 reg_names points into via the REGISTER_NAMES macro. */
5673 if (TARGET_NAME_REGS)
5674 memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
5676 /* When compiling for the mips16, we can not use floating point. We
5677 record the original hard float value in mips16_hard_float. */
5678 if (TARGET_MIPS16)
5680 if (TARGET_SOFT_FLOAT)
5681 mips16_hard_float = 0;
5682 else
5683 mips16_hard_float = 1;
5684 target_flags |= MASK_SOFT_FLOAT;
5686 /* Don't run the scheduler before reload, since it tends to
5687 increase register pressure. */
5688 flag_schedule_insns = 0;
5691 /* We put -mentry in TARGET_OPTIONS rather than TARGET_SWITCHES only
5692 to avoid using up another bit in target_flags. */
5693 if (mips_entry_string != NULL)
5695 if (*mips_entry_string != '\0')
5696 error ("invalid option `entry%s'", mips_entry_string);
5698 if (! TARGET_MIPS16)
5699 warning ("-mentry is only meaningful with -mips-16");
5700 else
5701 mips_entry = 1;
5704 /* We copy TARGET_MIPS16 into the mips16 global variable, so that
5705 attributes can access it. */
5706 if (TARGET_MIPS16)
5707 mips16 = 1;
5708 else
5709 mips16 = 0;
5711 #ifdef MIPS_TFMODE_FORMAT
5712 real_format_for_mode[TFmode - QFmode] = &MIPS_TFMODE_FORMAT;
5713 #endif
5715 mips_print_operand_punct['?'] = 1;
5716 mips_print_operand_punct['#'] = 1;
5717 mips_print_operand_punct['&'] = 1;
5718 mips_print_operand_punct['!'] = 1;
5719 mips_print_operand_punct['*'] = 1;
5720 mips_print_operand_punct['@'] = 1;
5721 mips_print_operand_punct['.'] = 1;
5722 mips_print_operand_punct['('] = 1;
5723 mips_print_operand_punct[')'] = 1;
5724 mips_print_operand_punct['['] = 1;
5725 mips_print_operand_punct[']'] = 1;
5726 mips_print_operand_punct['<'] = 1;
5727 mips_print_operand_punct['>'] = 1;
5728 mips_print_operand_punct['{'] = 1;
5729 mips_print_operand_punct['}'] = 1;
5730 mips_print_operand_punct['^'] = 1;
5731 mips_print_operand_punct['$'] = 1;
5732 mips_print_operand_punct['+'] = 1;
5733 mips_print_operand_punct['~'] = 1;
5735 mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
5736 mips_char_to_class['e'] = M16_NA_REGS;
5737 mips_char_to_class['t'] = T_REG;
5738 mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
5739 mips_char_to_class['h'] = HI_REG;
5740 mips_char_to_class['l'] = LO_REG;
5741 mips_char_to_class['a'] = HILO_REG;
5742 mips_char_to_class['x'] = MD_REGS;
5743 mips_char_to_class['b'] = ALL_REGS;
5744 mips_char_to_class['y'] = GR_REGS;
5745 mips_char_to_class['z'] = ST_REGS;
5746 mips_char_to_class['B'] = COP0_REGS;
5747 mips_char_to_class['C'] = COP2_REGS;
5748 mips_char_to_class['D'] = COP3_REGS;
5750 /* Set up array to map GCC register number to debug register number.
5751 Ignore the special purpose register numbers. */
5753 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5754 mips_dbx_regno[i] = -1;
5756 start = GP_DBX_FIRST - GP_REG_FIRST;
5757 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
5758 mips_dbx_regno[i] = i + start;
5760 start = FP_DBX_FIRST - FP_REG_FIRST;
5761 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
5762 mips_dbx_regno[i] = i + start;
5764 /* Set up array giving whether a given register can hold a given mode.
5765 At present, restrict ints from being in FP registers, because reload
5766 is a little enthusiastic about storing extra values in FP registers,
5767 and this is not good for things like OS kernels. Also, due to the
5768 mandatory delay, it is as fast to load from cached memory as to move
5769 from the FP register. */
5771 for (mode = VOIDmode;
5772 mode != MAX_MACHINE_MODE;
5773 mode = (enum machine_mode) ((int)mode + 1))
5775 register int size = GET_MODE_SIZE (mode);
5776 register enum mode_class class = GET_MODE_CLASS (mode);
5778 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5780 register int temp;
5782 if (mode == CCmode)
5784 if (! ISA_HAS_8CC)
5785 temp = (regno == FPSW_REGNUM);
5786 else
5787 temp = (ST_REG_P (regno) || GP_REG_P (regno)
5788 || FP_REG_P (regno));
5791 else if (GP_REG_P (regno))
5792 temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
5794 else if (FP_REG_P (regno))
5795 temp = (((regno % FP_INC) == 0
5796 /* I think this change is OK regardless of abi, but
5797 I'm being cautions untill I can test this more.
5798 HARD_REGNO_MODE_OK is about whether or not you
5799 can move to and from a register without changing
5800 the value, not about whether math works on the
5801 register. */
5802 || (mips_abi == ABI_MEABI && size <= 4))
5803 && (((class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
5804 && size <= UNITS_PER_FPVALUE)
5805 /* Allow integer modes that fit into a single
5806 register. We need to put integers into FPRs
5807 when using instructions like cvt and trunc. */
5808 || (class == MODE_INT && size <= UNITS_PER_FPREG)
5809 /* Allow TFmode for CCmode reloads. */
5810 || (ISA_HAS_8CC && mode == TFmode)));
5812 else if (MD_REG_P (regno))
5813 temp = (class == MODE_INT
5814 && (size <= UNITS_PER_WORD
5815 || (regno == MD_REG_FIRST
5816 && size == 2 * UNITS_PER_WORD)));
5818 else if (ALL_COP_REG_P (regno))
5819 temp = (class == MODE_INT && size <= UNITS_PER_WORD);
5820 else
5821 temp = 0;
5823 mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5827 /* Save GPR registers in word_mode sized hunks. word_mode hasn't been
5828 initialized yet, so we can't use that here. */
5829 gpr_mode = TARGET_64BIT ? DImode : SImode;
5831 /* Provide default values for align_* for 64-bit targets. */
5832 if (TARGET_64BIT && !TARGET_MIPS16)
5834 if (align_loops == 0)
5835 align_loops = 8;
5836 if (align_jumps == 0)
5837 align_jumps = 8;
5838 if (align_functions == 0)
5839 align_functions = 8;
5842 /* Function to allocate machine-dependent function status. */
5843 init_machine_status = &mips_init_machine_status;
5846 /* Implement CONDITIONAL_REGISTER_USAGE. */
5848 void
5849 mips_conditional_register_usage ()
5851 if (!TARGET_HARD_FLOAT)
5853 int regno;
5855 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5856 fixed_regs[regno] = call_used_regs[regno] = 1;
5857 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5858 fixed_regs[regno] = call_used_regs[regno] = 1;
5860 else if (! ISA_HAS_8CC)
5862 int regno;
5864 /* We only have a single condition code register. We
5865 implement this by hiding all the condition code registers,
5866 and generating RTL that refers directly to ST_REG_FIRST. */
5867 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5868 fixed_regs[regno] = call_used_regs[regno] = 1;
5870 /* In mips16 mode, we permit the $t temporary registers to be used
5871 for reload. We prohibit the unused $s registers, since they
5872 are caller saved, and saving them via a mips16 register would
5873 probably waste more time than just reloading the value. */
5874 if (TARGET_MIPS16)
5876 fixed_regs[18] = call_used_regs[18] = 1;
5877 fixed_regs[19] = call_used_regs[19] = 1;
5878 fixed_regs[20] = call_used_regs[20] = 1;
5879 fixed_regs[21] = call_used_regs[21] = 1;
5880 fixed_regs[22] = call_used_regs[22] = 1;
5881 fixed_regs[23] = call_used_regs[23] = 1;
5882 fixed_regs[26] = call_used_regs[26] = 1;
5883 fixed_regs[27] = call_used_regs[27] = 1;
5884 fixed_regs[30] = call_used_regs[30] = 1;
5886 /* fp20-23 are now caller saved. */
5887 if (mips_abi == ABI_64)
5889 int regno;
5890 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
5891 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5893 /* odd registers from fp21 to fp31 are now caller saved. */
5894 if (mips_abi == ABI_N32 || mips_abi == ABI_MEABI)
5896 int regno;
5897 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
5898 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5902 /* Allocate a chunk of memory for per-function machine-dependent data. */
5903 static struct machine_function *
5904 mips_init_machine_status ()
5906 return ((struct machine_function *)
5907 ggc_alloc_cleared (sizeof (struct machine_function)));
5910 /* On the mips16, we want to allocate $24 (T_REG) before other
5911 registers for instructions for which it is possible. This helps
5912 avoid shuffling registers around in order to set up for an xor,
5913 encouraging the compiler to use a cmp instead. */
5915 void
5916 mips_order_regs_for_local_alloc ()
5918 register int i;
5920 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5921 reg_alloc_order[i] = i;
5923 if (TARGET_MIPS16)
5925 /* It really doesn't matter where we put register 0, since it is
5926 a fixed register anyhow. */
5927 reg_alloc_order[0] = 24;
5928 reg_alloc_order[24] = 0;
5933 /* The MIPS debug format wants all automatic variables and arguments
5934 to be in terms of the virtual frame pointer (stack pointer before
5935 any adjustment in the function), while the MIPS 3.0 linker wants
5936 the frame pointer to be the stack pointer after the initial
5937 adjustment. So, we do the adjustment here. The arg pointer (which
5938 is eliminated) points to the virtual frame pointer, while the frame
5939 pointer (which may be eliminated) points to the stack pointer after
5940 the initial adjustments. */
5942 HOST_WIDE_INT
5943 mips_debugger_offset (addr, offset)
5944 rtx addr;
5945 HOST_WIDE_INT offset;
5947 rtx offset2 = const0_rtx;
5948 rtx reg = eliminate_constant_term (addr, &offset2);
5950 if (offset == 0)
5951 offset = INTVAL (offset2);
5953 if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5954 || reg == hard_frame_pointer_rtx)
5956 HOST_WIDE_INT frame_size = (!cfun->machine->frame.initialized)
5957 ? compute_frame_size (get_frame_size ())
5958 : cfun->machine->frame.total_size;
5960 /* MIPS16 frame is smaller */
5961 if (frame_pointer_needed && TARGET_MIPS16)
5962 frame_size -= current_function_outgoing_args_size;
5964 offset = offset - frame_size;
5967 /* sdbout_parms does not want this to crash for unrecognized cases. */
5968 #if 0
5969 else if (reg != arg_pointer_rtx)
5970 abort_with_insn (addr, "mips_debugger_offset called with non stack/frame/arg pointer");
5971 #endif
5973 return offset;
5976 /* A C compound statement to output to stdio stream STREAM the
5977 assembler syntax for an instruction operand X. X is an RTL
5978 expression.
5980 CODE is a value that can be used to specify one of several ways
5981 of printing the operand. It is used when identical operands
5982 must be printed differently depending on the context. CODE
5983 comes from the `%' specification that was used to request
5984 printing of the operand. If the specification was just `%DIGIT'
5985 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
5986 is the ASCII code for LTR.
5988 If X is a register, this macro should print the register's name.
5989 The names can be found in an array `reg_names' whose type is
5990 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
5992 When the machine description has a specification `%PUNCT' (a `%'
5993 followed by a punctuation character), this macro is called with
5994 a null pointer for X and the punctuation character for CODE.
5996 The MIPS specific codes are:
5998 'X' X is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5999 'x' X is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
6000 'd' output integer constant in decimal,
6001 'z' if the operand is 0, use $0 instead of normal operand.
6002 'D' print second part of double-word register or memory operand.
6003 'L' print low-order register of double-word register operand.
6004 'M' print high-order register of double-word register operand.
6005 'C' print part of opcode for a branch condition.
6006 'F' print part of opcode for a floating-point branch condition.
6007 'N' print part of opcode for a branch condition, inverted.
6008 'W' print part of opcode for a floating-point branch condition, inverted.
6009 'S' X is CODE_LABEL, print with prefix of "LS" (for embedded switch).
6010 'B' print 'z' for EQ, 'n' for NE
6011 'b' print 'n' for EQ, 'z' for NE
6012 'T' print 'f' for EQ, 't' for NE
6013 't' print 't' for EQ, 'f' for NE
6014 'Z' print register and a comma, but print nothing for $fcc0
6015 '(' Turn on .set noreorder
6016 ')' Turn on .set reorder
6017 '[' Turn on .set noat
6018 ']' Turn on .set at
6019 '<' Turn on .set nomacro
6020 '>' Turn on .set macro
6021 '{' Turn on .set volatile (not GAS)
6022 '}' Turn on .set novolatile (not GAS)
6023 '&' Turn on .set noreorder if filling delay slots
6024 '*' Turn on both .set noreorder and .set nomacro if filling delay slots
6025 '!' Turn on .set nomacro if filling delay slots
6026 '#' Print nop if in a .set noreorder section.
6027 '?' Print 'l' if we are to use a branch likely instead of normal branch.
6028 '@' Print the name of the assembler temporary register (at or $1).
6029 '.' Print the name of the register with a hard-wired zero (zero or $0).
6030 '^' Print the name of the pic call-through register (t9 or $25).
6031 '$' Print the name of the stack pointer register (sp or $29).
6032 '+' Print the name of the gp register (gp or $28).
6033 '~' Output a branch alignment to LABEL_ALIGN(NULL). */
6035 void
6036 print_operand (file, op, letter)
6037 FILE *file; /* file to write to */
6038 rtx op; /* operand to print */
6039 int letter; /* %<letter> or 0 */
6041 register enum rtx_code code;
6043 if (PRINT_OPERAND_PUNCT_VALID_P (letter))
6045 switch (letter)
6047 case '?':
6048 if (mips_branch_likely)
6049 putc ('l', file);
6050 break;
6052 case '@':
6053 fputs (reg_names [GP_REG_FIRST + 1], file);
6054 break;
6056 case '^':
6057 fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
6058 break;
6060 case '.':
6061 fputs (reg_names [GP_REG_FIRST + 0], file);
6062 break;
6064 case '$':
6065 fputs (reg_names[STACK_POINTER_REGNUM], file);
6066 break;
6068 case '+':
6069 fputs (reg_names[GP_REG_FIRST + 28], file);
6070 break;
6072 case '&':
6073 if (final_sequence != 0 && set_noreorder++ == 0)
6074 fputs (".set\tnoreorder\n\t", file);
6075 break;
6077 case '*':
6078 if (final_sequence != 0)
6080 if (set_noreorder++ == 0)
6081 fputs (".set\tnoreorder\n\t", file);
6083 if (set_nomacro++ == 0)
6084 fputs (".set\tnomacro\n\t", file);
6086 break;
6088 case '!':
6089 if (final_sequence != 0 && set_nomacro++ == 0)
6090 fputs ("\n\t.set\tnomacro", file);
6091 break;
6093 case '#':
6094 if (set_noreorder != 0)
6095 fputs ("\n\tnop", file);
6096 else if (TARGET_STATS)
6097 fputs ("\n\t#nop", file);
6099 break;
6101 case '(':
6102 if (set_noreorder++ == 0)
6103 fputs (".set\tnoreorder\n\t", file);
6104 break;
6106 case ')':
6107 if (set_noreorder == 0)
6108 error ("internal error: %%) found without a %%( in assembler pattern");
6110 else if (--set_noreorder == 0)
6111 fputs ("\n\t.set\treorder", file);
6113 break;
6115 case '[':
6116 if (set_noat++ == 0)
6117 fputs (".set\tnoat\n\t", file);
6118 break;
6120 case ']':
6121 if (set_noat == 0)
6122 error ("internal error: %%] found without a %%[ in assembler pattern");
6123 else if (--set_noat == 0)
6124 fputs ("\n\t.set\tat", file);
6126 break;
6128 case '<':
6129 if (set_nomacro++ == 0)
6130 fputs (".set\tnomacro\n\t", file);
6131 break;
6133 case '>':
6134 if (set_nomacro == 0)
6135 error ("internal error: %%> found without a %%< in assembler pattern");
6136 else if (--set_nomacro == 0)
6137 fputs ("\n\t.set\tmacro", file);
6139 break;
6141 case '{':
6142 if (set_volatile++ == 0)
6143 fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
6144 break;
6146 case '}':
6147 if (set_volatile == 0)
6148 error ("internal error: %%} found without a %%{ in assembler pattern");
6149 else if (--set_volatile == 0)
6150 fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
6152 break;
6154 case '~':
6156 if (align_labels_log > 0)
6157 ASM_OUTPUT_ALIGN (file, align_labels_log);
6159 break;
6161 default:
6162 error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
6163 break;
6166 return;
6169 if (! op)
6171 error ("PRINT_OPERAND null pointer");
6172 return;
6175 code = GET_CODE (op);
6177 if (code == SIGN_EXTEND)
6178 op = XEXP (op, 0), code = GET_CODE (op);
6180 if (letter == 'C')
6181 switch (code)
6183 case EQ: fputs ("eq", file); break;
6184 case NE: fputs ("ne", file); break;
6185 case GT: fputs ("gt", file); break;
6186 case GE: fputs ("ge", file); break;
6187 case LT: fputs ("lt", file); break;
6188 case LE: fputs ("le", file); break;
6189 case GTU: fputs ("gtu", file); break;
6190 case GEU: fputs ("geu", file); break;
6191 case LTU: fputs ("ltu", file); break;
6192 case LEU: fputs ("leu", file); break;
6193 default:
6194 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
6197 else if (letter == 'N')
6198 switch (code)
6200 case EQ: fputs ("ne", file); break;
6201 case NE: fputs ("eq", file); break;
6202 case GT: fputs ("le", file); break;
6203 case GE: fputs ("lt", file); break;
6204 case LT: fputs ("ge", file); break;
6205 case LE: fputs ("gt", file); break;
6206 case GTU: fputs ("leu", file); break;
6207 case GEU: fputs ("ltu", file); break;
6208 case LTU: fputs ("geu", file); break;
6209 case LEU: fputs ("gtu", file); break;
6210 default:
6211 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
6214 else if (letter == 'F')
6215 switch (code)
6217 case EQ: fputs ("c1f", file); break;
6218 case NE: fputs ("c1t", file); break;
6219 default:
6220 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
6223 else if (letter == 'W')
6224 switch (code)
6226 case EQ: fputs ("c1t", file); break;
6227 case NE: fputs ("c1f", file); break;
6228 default:
6229 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
6232 else if (letter == 'S')
6234 char buffer[100];
6236 ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
6237 assemble_name (file, buffer);
6240 else if (letter == 'Z')
6242 register int regnum;
6244 if (code != REG)
6245 abort ();
6247 regnum = REGNO (op);
6248 if (! ST_REG_P (regnum))
6249 abort ();
6251 if (regnum != ST_REG_FIRST)
6252 fprintf (file, "%s,", reg_names[regnum]);
6255 else if (code == REG || code == SUBREG)
6257 register int regnum;
6259 if (code == REG)
6260 regnum = REGNO (op);
6261 else
6262 regnum = true_regnum (op);
6264 if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
6265 || (letter == 'L' && WORDS_BIG_ENDIAN)
6266 || letter == 'D')
6267 regnum++;
6269 fprintf (file, "%s", reg_names[regnum]);
6272 else if (code == MEM)
6274 if (letter == 'D')
6275 output_address (plus_constant (XEXP (op, 0), 4));
6276 else
6277 output_address (XEXP (op, 0));
6280 else if (code == CONST_DOUBLE
6281 && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
6283 char s[60];
6285 real_to_decimal (s, CONST_DOUBLE_REAL_VALUE (op), sizeof (s), 0, 1);
6286 fputs (s, file);
6289 else if (letter == 'x' && GET_CODE (op) == CONST_INT)
6290 fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
6292 else if (letter == 'X' && GET_CODE(op) == CONST_INT)
6293 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
6295 else if (letter == 'd' && GET_CODE(op) == CONST_INT)
6296 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
6298 else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
6299 fputs (reg_names[GP_REG_FIRST], file);
6301 else if (letter == 'd' || letter == 'x' || letter == 'X')
6302 output_operand_lossage ("invalid use of %%d, %%x, or %%X");
6304 else if (letter == 'B')
6305 fputs (code == EQ ? "z" : "n", file);
6306 else if (letter == 'b')
6307 fputs (code == EQ ? "n" : "z", file);
6308 else if (letter == 'T')
6309 fputs (code == EQ ? "f" : "t", file);
6310 else if (letter == 't')
6311 fputs (code == EQ ? "t" : "f", file);
6313 else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
6315 /* This case arises on the mips16; see mips16_gp_pseudo_reg. */
6316 print_operand (file, XEXP (op, 0), letter);
6319 else if (TARGET_MIPS16 && code == CONST && mips16_gp_offset_p (op))
6321 fputs ("%gprel(", file);
6322 mips16_output_gp_offset (file, op);
6323 fputs (")", file);
6326 else
6327 output_addr_const (file, op);
6330 /* A C compound statement to output to stdio stream STREAM the
6331 assembler syntax for an instruction operand that is a memory
6332 reference whose address is ADDR. ADDR is an RTL expression. */
6334 void
6335 print_operand_address (file, addr)
6336 FILE *file;
6337 rtx addr;
6339 if (!addr)
6340 error ("PRINT_OPERAND_ADDRESS, null pointer");
6342 else
6343 switch (GET_CODE (addr))
6345 case REG:
6346 if (! TARGET_MIPS16 && REGNO (addr) == ARG_POINTER_REGNUM)
6347 abort_with_insn (addr, "arg pointer not eliminated");
6349 fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
6350 break;
6352 case LO_SUM:
6354 register rtx arg0 = XEXP (addr, 0);
6355 register rtx arg1 = XEXP (addr, 1);
6357 if (! mips_split_addresses)
6358 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, Spurious LO_SUM");
6360 if (GET_CODE (arg0) != REG)
6361 abort_with_insn (addr,
6362 "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG");
6364 fprintf (file, "%%lo(");
6365 print_operand_address (file, arg1);
6366 fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
6368 break;
6370 case PLUS:
6372 register rtx reg = 0;
6373 register rtx offset = 0;
6374 register rtx arg0 = XEXP (addr, 0);
6375 register rtx arg1 = XEXP (addr, 1);
6377 if (GET_CODE (arg0) == REG)
6379 reg = arg0;
6380 offset = arg1;
6381 if (GET_CODE (offset) == REG)
6382 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
6385 else if (GET_CODE (arg1) == REG)
6386 reg = arg1, offset = arg0;
6387 else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
6389 output_addr_const (file, addr);
6390 break;
6392 else
6393 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
6395 if (! CONSTANT_P (offset))
6396 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
6398 if (REGNO (reg) == ARG_POINTER_REGNUM)
6399 abort_with_insn (addr, "arg pointer not eliminated");
6401 if (TARGET_MIPS16
6402 && GET_CODE (offset) == CONST
6403 && mips16_gp_offset_p (offset))
6405 fputs ("%gprel(", file);
6406 mips16_output_gp_offset (file, offset);
6407 fputs (")", file);
6409 else
6410 output_addr_const (file, offset);
6411 fprintf (file, "(%s)", reg_names [REGNO (reg)]);
6413 break;
6415 case LABEL_REF:
6416 case SYMBOL_REF:
6417 case CONST_INT:
6418 case CONST:
6419 output_addr_const (file, addr);
6420 break;
6422 default:
6423 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
6424 break;
6428 /* Target hook for assembling integer objects. It appears that the Irix
6429 6 assembler can't handle 64-bit decimal integers, so avoid printing
6430 such an integer here. */
6432 static bool
6433 mips_assemble_integer (x, size, aligned_p)
6434 rtx x;
6435 unsigned int size;
6436 int aligned_p;
6438 if ((TARGET_64BIT || TARGET_GAS) && size == 8 && aligned_p)
6440 fputs ("\t.dword\t", asm_out_file);
6441 if (HOST_BITS_PER_WIDE_INT < 64 || GET_CODE (x) != CONST_INT)
6442 output_addr_const (asm_out_file, x);
6443 else
6444 print_operand (asm_out_file, x, 'X');
6445 fputc ('\n', asm_out_file);
6446 return true;
6448 return default_assemble_integer (x, size, aligned_p);
6451 /* If optimizing for the global pointer, keep track of all of the externs, so
6452 that at the end of the file, we can emit the appropriate .extern
6453 declaration for them, before writing out the text section. We assume all
6454 names passed to us are in the permanent obstack, so they will be valid at
6455 the end of the compilation.
6457 If we have -G 0, or the extern size is unknown, or the object is in a user
6458 specified section that is not .sbss/.sdata, don't bother emitting the
6459 .externs. In the case of user specified sections this behavior is
6460 required as otherwise GAS will think the object lives in .sbss/.sdata. */
6463 mips_output_external (file, decl, name)
6464 FILE *file ATTRIBUTE_UNUSED;
6465 tree decl;
6466 const char *name;
6468 register struct extern_list *p;
6469 int len;
6470 tree section_name;
6472 if (TARGET_GP_OPT
6473 && TREE_CODE (decl) != FUNCTION_DECL
6474 && !DECL_COMDAT (decl)
6475 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
6476 && ((section_name = DECL_SECTION_NAME (decl)) == NULL
6477 || strcmp (TREE_STRING_POINTER (section_name), ".sbss") == 0
6478 || strcmp (TREE_STRING_POINTER (section_name), ".sdata") == 0))
6480 p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6481 p->next = extern_head;
6482 p->name = name;
6483 p->size = len;
6484 extern_head = p;
6487 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6488 if (TREE_CODE (decl) == FUNCTION_DECL
6489 /* ??? Don't include alloca, since gcc will always expand it
6490 inline. If we don't do this, the C++ library fails to build. */
6491 && strcmp (name, "alloca")
6492 /* ??? Don't include __builtin_next_arg, because then gcc will not
6493 bootstrap under Irix 5.1. */
6494 && strcmp (name, "__builtin_next_arg"))
6496 p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6497 p->next = extern_head;
6498 p->name = name;
6499 p->size = -1;
6500 extern_head = p;
6502 #endif
6504 return 0;
6507 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6509 mips_output_external_libcall (file, name)
6510 FILE *file ATTRIBUTE_UNUSED;
6511 const char *name;
6513 register struct extern_list *p;
6515 p = (struct extern_list *) xmalloc (sizeof (struct extern_list));
6516 p->next = extern_head;
6517 p->name = name;
6518 p->size = -1;
6519 extern_head = p;
6521 return 0;
6523 #endif
6525 /* Emit a new filename to a stream. If this is MIPS ECOFF, watch out
6526 for .file's that start within a function. If we are smuggling stabs, try to
6527 put out a MIPS ECOFF file and a stab. */
6529 void
6530 mips_output_filename (stream, name)
6531 FILE *stream;
6532 const char *name;
6534 static int first_time = 1;
6535 char ltext_label_name[100];
6537 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
6538 directives. */
6539 if (write_symbols == DWARF2_DEBUG)
6540 return;
6541 else if (first_time)
6543 first_time = 0;
6544 SET_FILE_NUMBER ();
6545 current_function_file = name;
6546 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
6547 /* This tells mips-tfile that stabs will follow. */
6548 if (!TARGET_GAS && write_symbols == DBX_DEBUG)
6549 fprintf (stream, "\t#@stabs\n");
6552 else if (write_symbols == DBX_DEBUG)
6554 ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
6555 fprintf (stream, "%s", ASM_STABS_OP);
6556 output_quoted_string (stream, name);
6557 fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
6560 else if (name != current_function_file
6561 && strcmp (name, current_function_file) != 0)
6563 if (inside_function && !TARGET_GAS)
6565 if (!file_in_function_warning)
6567 file_in_function_warning = 1;
6568 ignore_line_number = 1;
6569 warning ("MIPS ECOFF format does not allow changing filenames within functions with #line");
6572 else
6574 SET_FILE_NUMBER ();
6575 current_function_file = name;
6576 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
6581 /* Emit a linenumber. For encapsulated stabs, we need to put out a stab
6582 as well as a .loc, since it is possible that MIPS ECOFF might not be
6583 able to represent the location for inlines that come from a different
6584 file. */
6586 void
6587 mips_output_lineno (stream, line)
6588 FILE *stream;
6589 int line;
6591 if (write_symbols == DBX_DEBUG)
6593 ++sym_lineno;
6594 fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
6595 LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
6596 LOCAL_LABEL_PREFIX, sym_lineno);
6598 else
6600 fprintf (stream, "\n\t%s.loc\t%d %d\n",
6601 (ignore_line_number) ? "#" : "",
6602 num_source_filenames, line);
6604 LABEL_AFTER_LOC (stream);
6608 /* Output an ASCII string, in a space-saving way. */
6610 void
6611 mips_output_ascii (stream, string_param, len)
6612 FILE *stream;
6613 const char *string_param;
6614 size_t len;
6616 size_t i;
6617 int cur_pos = 17;
6618 register const unsigned char *string =
6619 (const unsigned char *)string_param;
6621 fprintf (stream, "\t.ascii\t\"");
6622 for (i = 0; i < len; i++)
6624 register int c = string[i];
6626 switch (c)
6628 case '\"':
6629 case '\\':
6630 putc ('\\', stream);
6631 putc (c, stream);
6632 cur_pos += 2;
6633 break;
6635 case TARGET_NEWLINE:
6636 fputs ("\\n", stream);
6637 if (i+1 < len
6638 && (((c = string[i+1]) >= '\040' && c <= '~')
6639 || c == TARGET_TAB))
6640 cur_pos = 32767; /* break right here */
6641 else
6642 cur_pos += 2;
6643 break;
6645 case TARGET_TAB:
6646 fputs ("\\t", stream);
6647 cur_pos += 2;
6648 break;
6650 case TARGET_FF:
6651 fputs ("\\f", stream);
6652 cur_pos += 2;
6653 break;
6655 case TARGET_BS:
6656 fputs ("\\b", stream);
6657 cur_pos += 2;
6658 break;
6660 case TARGET_CR:
6661 fputs ("\\r", stream);
6662 cur_pos += 2;
6663 break;
6665 default:
6666 if (c >= ' ' && c < 0177)
6668 putc (c, stream);
6669 cur_pos++;
6671 else
6673 fprintf (stream, "\\%03o", c);
6674 cur_pos += 4;
6678 if (cur_pos > 72 && i+1 < len)
6680 cur_pos = 17;
6681 fprintf (stream, "\"\n\t.ascii\t\"");
6684 fprintf (stream, "\"\n");
6687 /* If defined, a C statement to be executed just prior to the output of
6688 assembler code for INSN, to modify the extracted operands so they will be
6689 output differently.
6691 Here the argument OPVEC is the vector containing the operands extracted
6692 from INSN, and NOPERANDS is the number of elements of the vector which
6693 contain meaningful data for this insn. The contents of this vector are
6694 what will be used to convert the insn template into assembler code, so you
6695 can change the assembler output by changing the contents of the vector.
6697 We use it to check if the current insn needs a nop in front of it because
6698 of load delays, and also to update the delay slot statistics. */
6700 /* ??? There is no real need for this function, because it never actually
6701 emits a NOP anymore. */
6703 void
6704 final_prescan_insn (insn, opvec, noperands)
6705 rtx insn;
6706 rtx opvec[] ATTRIBUTE_UNUSED;
6707 int noperands ATTRIBUTE_UNUSED;
6709 if (dslots_number_nops > 0)
6711 rtx pattern = PATTERN (insn);
6712 int length = get_attr_length (insn);
6714 /* Do we need to emit a NOP? */
6715 if (length == 0
6716 || (mips_load_reg != 0 && reg_mentioned_p (mips_load_reg, pattern))
6717 || (mips_load_reg2 != 0 && reg_mentioned_p (mips_load_reg2, pattern))
6718 || (mips_load_reg3 != 0 && reg_mentioned_p (mips_load_reg3, pattern))
6719 || (mips_load_reg4 != 0
6720 && reg_mentioned_p (mips_load_reg4, pattern)))
6721 fputs ("\t#nop\n", asm_out_file);
6723 else
6724 dslots_load_filled++;
6726 while (--dslots_number_nops > 0)
6727 fputs ("\t#nop\n", asm_out_file);
6729 mips_load_reg = 0;
6730 mips_load_reg2 = 0;
6731 mips_load_reg3 = 0;
6732 mips_load_reg4 = 0;
6735 if (TARGET_STATS
6736 && (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CALL_INSN))
6737 dslots_jump_total++;
6740 /* Output at beginning of assembler file.
6742 If we are optimizing to use the global pointer, create a temporary file to
6743 hold all of the text stuff, and write it out to the end. This is needed
6744 because the MIPS assembler is evidently one pass, and if it hasn't seen the
6745 relevant .comm/.lcomm/.extern/.sdata declaration when the code is
6746 processed, it generates a two instruction sequence. */
6748 void
6749 mips_asm_file_start (stream)
6750 FILE *stream;
6752 ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
6754 /* Versions of the MIPS assembler before 2.20 generate errors if a branch
6755 inside of a .set noreorder section jumps to a label outside of the .set
6756 noreorder section. Revision 2.20 just set nobopt silently rather than
6757 fixing the bug. */
6759 if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
6760 fprintf (stream, "\t.set\tnobopt\n");
6762 if (TARGET_GAS)
6764 #if defined(OBJECT_FORMAT_ELF) && !(TARGET_IRIX5 || TARGET_IRIX6)
6765 /* Generate a special section to describe the ABI switches used to
6766 produce the resultant binary. This used to be done by the assembler
6767 setting bits in the ELF header's flags field, but we have run out of
6768 bits. GDB needs this information in order to be able to correctly
6769 debug these binaries. See the function mips_gdbarch_init() in
6770 gdb/mips-tdep.c. This is unnecessary for the IRIX 5/6 ABIs and
6771 causes unnecessary IRIX 6 ld warnings. */
6772 const char * abi_string = NULL;
6774 switch (mips_abi)
6776 case ABI_32: abi_string = "abi32"; break;
6777 case ABI_N32: abi_string = "abiN32"; break;
6778 case ABI_64: abi_string = "abi64"; break;
6779 case ABI_O64: abi_string = "abiO64"; break;
6780 case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
6781 case ABI_MEABI:abi_string = TARGET_64BIT ? "meabi64" : "meabi32"; break;
6782 default:
6783 abort ();
6785 /* Note - we use fprintf directly rather than called named_section()
6786 because in this way we can avoid creating an allocated section. We
6787 do not want this section to take up any space in the running
6788 executable. */
6789 fprintf (stream, "\t.section .mdebug.%s\n", abi_string);
6791 /* Restore the default section. */
6792 fprintf (stream, "\t.previous\n");
6793 #endif
6798 /* Generate the pseudo ops that System V.4 wants. */
6799 #ifndef ABICALLS_ASM_OP
6800 #define ABICALLS_ASM_OP "\t.abicalls"
6801 #endif
6802 if (TARGET_ABICALLS)
6803 /* ??? but do not want this (or want pic0) if -non-shared? */
6804 fprintf (stream, "%s\n", ABICALLS_ASM_OP);
6806 if (TARGET_MIPS16)
6807 fprintf (stream, "\t.set\tmips16\n");
6809 /* This code exists so that we can put all externs before all symbol
6810 references. This is necessary for the MIPS assembler's global pointer
6811 optimizations to work. */
6812 if (TARGET_FILE_SWITCHING)
6814 asm_out_data_file = stream;
6815 asm_out_text_file = tmpfile ();
6817 else
6818 asm_out_data_file = asm_out_text_file = stream;
6820 if (flag_verbose_asm)
6821 fprintf (stream, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
6822 ASM_COMMENT_START,
6823 mips_section_threshold, mips_arch_info->name, mips_isa);
6826 /* If we are optimizing the global pointer, emit the text section now and any
6827 small externs which did not have .comm, etc that are needed. Also, give a
6828 warning if the data area is more than 32K and -pic because 3 instructions
6829 are needed to reference the data pointers. */
6831 void
6832 mips_asm_file_end (file)
6833 FILE *file;
6835 tree name_tree;
6836 struct extern_list *p;
6838 if (extern_head)
6840 fputs ("\n", file);
6842 for (p = extern_head; p != 0; p = p->next)
6844 name_tree = get_identifier (p->name);
6846 /* Positively ensure only one .extern for any given symbol. */
6847 if (! TREE_ASM_WRITTEN (name_tree))
6849 TREE_ASM_WRITTEN (name_tree) = 1;
6850 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6851 if (p->size == -1)
6852 ASM_OUTPUT_UNDEF_FUNCTION (file, p->name);
6853 else
6854 #endif
6856 fputs ("\t.extern\t", file);
6857 assemble_name (file, p->name);
6858 fprintf (file, ", %d\n", p->size);
6864 if (TARGET_FILE_SWITCHING)
6866 fprintf (file, "\n\t.text\n");
6867 copy_file_data (file, asm_out_text_file);
6871 static void
6872 copy_file_data (to, from)
6873 FILE *to, *from;
6875 char buffer[8192];
6876 size_t len;
6877 rewind (from);
6878 if (ferror (from))
6879 fatal_io_error ("can't rewind temp file");
6881 while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
6882 if (fwrite (buffer, 1, len, to) != len)
6883 fatal_io_error ("can't write to output file");
6885 if (ferror (from))
6886 fatal_io_error ("can't read from temp file");
6888 if (fclose (from))
6889 fatal_io_error ("can't close temp file");
6892 /* Emit either a label, .comm, or .lcomm directive, and mark that the symbol
6893 is used, so that we don't emit an .extern for it in mips_asm_file_end. */
6895 void
6896 mips_declare_object (stream, name, init_string, final_string, size)
6897 FILE *stream;
6898 const char *name;
6899 const char *init_string;
6900 const char *final_string;
6901 int size;
6903 fputs (init_string, stream); /* "", "\t.comm\t", or "\t.lcomm\t" */
6904 assemble_name (stream, name);
6905 fprintf (stream, final_string, size); /* ":\n", ",%u\n", ",%u\n" */
6907 if (TARGET_GP_OPT)
6909 tree name_tree = get_identifier (name);
6910 TREE_ASM_WRITTEN (name_tree) = 1;
6914 /* Return the bytes needed to compute the frame pointer from the current
6915 stack pointer.
6917 Mips stack frames look like:
6919 Before call After call
6920 +-----------------------+ +-----------------------+
6921 high | | | |
6922 mem. | | | |
6923 | caller's temps. | | caller's temps. |
6924 | | | |
6925 +-----------------------+ +-----------------------+
6926 | | | |
6927 | arguments on stack. | | arguments on stack. |
6928 | | | |
6929 +-----------------------+ +-----------------------+
6930 | 4 words to save | | 4 words to save |
6931 | arguments passed | | arguments passed |
6932 | in registers, even | | in registers, even |
6933 SP->| if not passed. | VFP->| if not passed. |
6934 +-----------------------+ +-----------------------+
6936 | fp register save |
6938 +-----------------------+
6940 | gp register save |
6942 +-----------------------+
6944 | local variables |
6946 +-----------------------+
6948 | alloca allocations |
6950 +-----------------------+
6952 | GP save for V.4 abi |
6954 +-----------------------+
6956 | arguments on stack |
6958 +-----------------------+
6959 | 4 words to save |
6960 | arguments passed |
6961 | in registers, even |
6962 low SP->| if not passed. |
6963 memory +-----------------------+
6967 HOST_WIDE_INT
6968 compute_frame_size (size)
6969 HOST_WIDE_INT size; /* # of var. bytes allocated */
6971 unsigned int regno;
6972 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
6973 HOST_WIDE_INT var_size; /* # bytes that variables take up */
6974 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
6975 HOST_WIDE_INT extra_size; /* # extra bytes */
6976 HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6977 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
6978 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
6979 long mask; /* mask of saved gp registers */
6980 long fmask; /* mask of saved fp registers */
6981 tree return_type;
6983 gp_reg_size = 0;
6984 fp_reg_size = 0;
6985 mask = 0;
6986 fmask = 0;
6987 extra_size = MIPS_STACK_ALIGN (((TARGET_ABICALLS) ? UNITS_PER_WORD : 0));
6988 var_size = MIPS_STACK_ALIGN (size);
6989 args_size = MIPS_STACK_ALIGN (current_function_outgoing_args_size);
6991 /* The MIPS 3.0 linker does not like functions that dynamically
6992 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6993 looks like we are trying to create a second frame pointer to the
6994 function, so allocate some stack space to make it happy. */
6996 if (args_size == 0 && current_function_calls_alloca)
6997 args_size = 4 * UNITS_PER_WORD;
6999 total_size = var_size + args_size + extra_size;
7000 return_type = DECL_RESULT (current_function_decl);
7002 /* Calculate space needed for gp registers. */
7003 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
7005 /* $18 is a special case on the mips16. It may be used to call
7006 a function which returns a floating point value, but it is
7007 marked in call_used_regs. $31 is also a special case. When
7008 not using -mentry, it will be used to copy a return value
7009 into the floating point registers if the return value is
7010 floating point. */
7011 if (MUST_SAVE_REGISTER (regno)
7012 || (TARGET_MIPS16
7013 && regno == GP_REG_FIRST + 18
7014 && regs_ever_live[regno])
7015 || (TARGET_MIPS16
7016 && regno == GP_REG_FIRST + 31
7017 && mips16_hard_float
7018 && ! mips_entry
7019 && ! aggregate_value_p (return_type)
7020 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
7021 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE))
7023 gp_reg_size += GET_MODE_SIZE (gpr_mode);
7024 mask |= 1L << (regno - GP_REG_FIRST);
7026 /* The entry and exit pseudo instructions can not save $17
7027 without also saving $16. */
7028 if (mips_entry
7029 && regno == GP_REG_FIRST + 17
7030 && ! MUST_SAVE_REGISTER (GP_REG_FIRST + 16))
7032 gp_reg_size += UNITS_PER_WORD;
7033 mask |= 1L << 16;
7038 /* We need to restore these for the handler. */
7039 if (current_function_calls_eh_return)
7041 unsigned int i;
7042 for (i = 0; ; ++i)
7044 regno = EH_RETURN_DATA_REGNO (i);
7045 if (regno == INVALID_REGNUM)
7046 break;
7047 gp_reg_size += GET_MODE_SIZE (gpr_mode);
7048 mask |= 1L << (regno - GP_REG_FIRST);
7052 /* This loop must iterate over the same space as its companion in
7053 save_restore_insns. */
7054 for (regno = (FP_REG_LAST - FP_INC + 1);
7055 regno >= FP_REG_FIRST;
7056 regno -= FP_INC)
7058 if (regs_ever_live[regno] && !call_used_regs[regno])
7060 fp_reg_size += FP_INC * UNITS_PER_FPREG;
7061 fmask |= ((1 << FP_INC) - 1) << (regno - FP_REG_FIRST);
7065 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
7066 total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
7068 /* The gp reg is caller saved in the 32 bit ABI, so there is no need
7069 for leaf routines (total_size == extra_size) to save the gp reg.
7070 The gp reg is callee saved in the 64 bit ABI, so all routines must
7071 save the gp reg. This is not a leaf routine if -p, because of the
7072 call to mcount. */
7073 if (total_size == extra_size
7074 && (mips_abi == ABI_32 || mips_abi == ABI_O64 || mips_abi == ABI_EABI)
7075 && ! current_function_profile)
7076 total_size = extra_size = 0;
7077 else if (TARGET_ABICALLS)
7079 /* Add the context-pointer to the saved registers. */
7080 gp_reg_size += UNITS_PER_WORD;
7081 mask |= 1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST);
7082 total_size -= gp_reg_rounded;
7083 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
7084 total_size += gp_reg_rounded;
7087 /* Add in space reserved on the stack by the callee for storing arguments
7088 passed in registers. */
7089 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
7090 total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
7092 /* The entry pseudo instruction will allocate 32 bytes on the stack. */
7093 if (mips_entry && total_size > 0 && total_size < 32)
7094 total_size = 32;
7096 /* Save other computed information. */
7097 cfun->machine->frame.total_size = total_size;
7098 cfun->machine->frame.var_size = var_size;
7099 cfun->machine->frame.args_size = args_size;
7100 cfun->machine->frame.extra_size = extra_size;
7101 cfun->machine->frame.gp_reg_size = gp_reg_size;
7102 cfun->machine->frame.fp_reg_size = fp_reg_size;
7103 cfun->machine->frame.mask = mask;
7104 cfun->machine->frame.fmask = fmask;
7105 cfun->machine->frame.initialized = reload_completed;
7106 cfun->machine->frame.num_gp = gp_reg_size / UNITS_PER_WORD;
7107 cfun->machine->frame.num_fp = fp_reg_size / (FP_INC * UNITS_PER_FPREG);
7109 if (mask)
7111 unsigned long offset;
7113 /* When using mips_entry, the registers are always saved at the
7114 top of the stack. */
7115 if (! mips_entry)
7116 offset = (args_size + extra_size + var_size
7117 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
7118 else
7119 offset = total_size - GET_MODE_SIZE (gpr_mode);
7121 cfun->machine->frame.gp_sp_offset = offset;
7122 cfun->machine->frame.gp_save_offset = offset - total_size;
7124 else
7126 cfun->machine->frame.gp_sp_offset = 0;
7127 cfun->machine->frame.gp_save_offset = 0;
7130 if (fmask)
7132 unsigned long offset = (args_size + extra_size + var_size
7133 + gp_reg_rounded + fp_reg_size
7134 - FP_INC * UNITS_PER_FPREG);
7135 cfun->machine->frame.fp_sp_offset = offset;
7136 cfun->machine->frame.fp_save_offset = offset - total_size;
7138 else
7140 cfun->machine->frame.fp_sp_offset = 0;
7141 cfun->machine->frame.fp_save_offset = 0;
7144 /* Ok, we're done. */
7145 return total_size;
7148 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame
7149 pointer, argument pointer, or return address pointer. TO is either
7150 the stack pointer or hard frame pointer. */
7153 mips_initial_elimination_offset (from, to)
7154 int from, to;
7156 int offset;
7158 /* Set OFFSET to the offset from the stack pointer. */
7159 switch (from)
7161 case FRAME_POINTER_REGNUM:
7162 offset = 0;
7163 break;
7165 case ARG_POINTER_REGNUM:
7166 compute_frame_size (get_frame_size ());
7167 offset = cfun->machine->frame.total_size;
7168 if (mips_abi == ABI_N32 || mips_abi == ABI_64 || mips_abi == ABI_MEABI)
7169 offset -= current_function_pretend_args_size;
7170 break;
7172 case RETURN_ADDRESS_POINTER_REGNUM:
7173 compute_frame_size (get_frame_size ());
7174 offset = cfun->machine->frame.gp_sp_offset;
7175 if (BYTES_BIG_ENDIAN)
7176 offset += UNITS_PER_WORD - (POINTER_SIZE / BITS_PER_UNIT);
7177 break;
7179 default:
7180 abort ();
7183 if (TARGET_MIPS16 && to == HARD_FRAME_POINTER_REGNUM)
7184 offset -= current_function_outgoing_args_size;
7186 return offset;
7189 /* Common code to emit the insns (or to write the instructions to a file)
7190 to save/restore registers.
7192 Other parts of the code assume that MIPS_TEMP1_REGNUM (aka large_reg)
7193 is not modified within save_restore_insns. */
7195 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
7197 /* Emit instructions to load the value (SP + OFFSET) into MIPS_TEMP2_REGNUM
7198 and return an rtl expression for the register.
7200 This function is a subroutine of save_restore_insns. It is used when
7201 OFFSET is too large to add in a single instruction. */
7203 static rtx
7204 mips_add_large_offset_to_sp (offset)
7205 HOST_WIDE_INT offset;
7207 rtx reg = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7208 rtx offset_rtx = GEN_INT (offset);
7210 emit_move_insn (reg, offset_rtx);
7211 if (Pmode == DImode)
7212 emit_insn (gen_adddi3 (reg, reg, stack_pointer_rtx));
7213 else
7214 emit_insn (gen_addsi3 (reg, reg, stack_pointer_rtx));
7215 return reg;
7218 /* Make INSN frame related and note that it performs the frame-related
7219 operation DWARF_PATTERN. */
7221 static void
7222 mips_annotate_frame_insn (insn, dwarf_pattern)
7223 rtx insn, dwarf_pattern;
7225 RTX_FRAME_RELATED_P (insn) = 1;
7226 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
7227 dwarf_pattern,
7228 REG_NOTES (insn));
7231 /* Return a frame-related rtx that stores register REGNO at (SP + OFFSET).
7232 The expression should only be used to store single registers. */
7234 static rtx
7235 mips_frame_set (mode, regno, offset)
7236 enum machine_mode mode;
7237 int regno;
7238 int offset;
7240 rtx address = plus_constant (stack_pointer_rtx, offset);
7241 rtx set = gen_rtx_SET (mode,
7242 gen_rtx_MEM (mode, address),
7243 gen_rtx_REG (mode, regno));
7244 RTX_FRAME_RELATED_P (set) = 1;
7245 return set;
7249 /* Emit a move instruction that stores REG in MEM. Make the instruction
7250 frame related and note that it stores REG at (SP + OFFSET). This
7251 function may be asked to store an FPR pair. */
7253 static void
7254 mips_emit_frame_related_store (mem, reg, offset)
7255 rtx mem;
7256 rtx reg;
7257 HOST_WIDE_INT offset;
7259 rtx dwarf_expr;
7261 if (GET_MODE (reg) == DFmode && ! TARGET_FLOAT64)
7263 /* Two registers are being stored, so the frame-related expression
7264 must be a PARALLEL rtx with one SET for each register. The
7265 higher numbered register is stored in the lower address on
7266 big-endian targets. */
7267 int regno1 = TARGET_BIG_ENDIAN ? REGNO (reg) + 1 : REGNO (reg);
7268 int regno2 = TARGET_BIG_ENDIAN ? REGNO (reg) : REGNO (reg) + 1;
7269 rtx set1 = mips_frame_set (SFmode, regno1, offset);
7270 rtx set2 = mips_frame_set (SFmode, regno2, offset + UNITS_PER_FPREG);
7271 dwarf_expr = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set1, set2));
7273 else
7274 dwarf_expr = mips_frame_set (GET_MODE (reg), REGNO (reg), offset);
7276 mips_annotate_frame_insn (emit_move_insn (mem, reg), dwarf_expr);
7279 static void
7280 save_restore_insns (store_p, large_reg, large_offset)
7281 int store_p; /* true if this is prologue */
7282 rtx large_reg; /* register holding large offset constant or NULL */
7283 long large_offset; /* large constant offset value */
7285 long mask = cfun->machine->frame.mask;
7286 long fmask = cfun->machine->frame.fmask;
7287 long real_mask = mask;
7288 int regno;
7289 rtx base_reg_rtx;
7290 HOST_WIDE_INT base_offset;
7291 HOST_WIDE_INT gp_offset;
7292 HOST_WIDE_INT fp_offset;
7293 HOST_WIDE_INT end_offset;
7295 if (frame_pointer_needed
7296 && ! BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST))
7297 abort ();
7299 /* Do not restore GP under certain conditions. */
7300 if (! store_p
7301 && TARGET_ABICALLS
7302 && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7303 mask &= ~(1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST));
7305 if (mask == 0 && fmask == 0)
7306 return;
7308 /* Save registers starting from high to low. The debuggers prefer at least
7309 the return register be stored at func+4, and also it allows us not to
7310 need a nop in the epilog if at least one register is reloaded in
7311 addition to return address. */
7313 /* Save GP registers if needed. */
7314 if (mask)
7316 /* Pick which pointer to use as a base register. For small frames, just
7317 use the stack pointer. Otherwise, use a temporary register. Save 2
7318 cycles if the save area is near the end of a large frame, by reusing
7319 the constant created in the prologue/epilogue to adjust the stack
7320 frame. */
7322 gp_offset = cfun->machine->frame.gp_sp_offset;
7323 end_offset
7324 = gp_offset - (cfun->machine->frame.gp_reg_size
7325 - GET_MODE_SIZE (gpr_mode));
7327 if (gp_offset < 0 || end_offset < 0)
7328 internal_error
7329 ("gp_offset (%ld) or end_offset (%ld) is less than zero",
7330 (long) gp_offset, (long) end_offset);
7332 /* If we see a large frame in mips16 mode, we save the registers
7333 before adjusting the stack pointer, and load them afterward. */
7334 else if (TARGET_MIPS16 && large_offset > 32767)
7335 base_reg_rtx = stack_pointer_rtx, base_offset = large_offset;
7337 else if (gp_offset < 32768)
7338 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
7340 else if (large_reg != 0
7341 && (unsigned HOST_WIDE_INT) (large_offset - gp_offset) < 32768
7342 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
7344 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7345 base_offset = large_offset;
7346 if (Pmode == DImode)
7347 emit_insn (gen_adddi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7348 else
7349 emit_insn (gen_addsi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7351 else
7353 base_offset = gp_offset;
7354 base_reg_rtx = mips_add_large_offset_to_sp (base_offset);
7357 /* When we restore the registers in MIPS16 mode, then if we are
7358 using a frame pointer, and this is not a large frame, the
7359 current stack pointer will be offset by
7360 current_function_outgoing_args_size. Doing it this way lets
7361 us avoid offsetting the frame pointer before copying it into
7362 the stack pointer; there is no instruction to set the stack
7363 pointer to the sum of a register and a constant. */
7364 if (TARGET_MIPS16
7365 && ! store_p
7366 && frame_pointer_needed
7367 && large_offset <= 32767)
7368 base_offset += current_function_outgoing_args_size;
7370 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
7372 if (BITSET_P (mask, regno - GP_REG_FIRST))
7374 rtx reg_rtx;
7375 rtx mem_rtx
7376 = gen_rtx (MEM, gpr_mode,
7377 gen_rtx (PLUS, Pmode, base_reg_rtx,
7378 GEN_INT (gp_offset - base_offset)));
7380 if (! current_function_calls_eh_return)
7381 RTX_UNCHANGING_P (mem_rtx) = 1;
7383 /* The mips16 does not have an instruction to load
7384 $31, so we load $7 instead, and work things out
7385 in mips_expand_epilogue. */
7386 if (TARGET_MIPS16 && ! store_p && regno == GP_REG_FIRST + 31)
7387 reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 7);
7388 /* The mips16 sometimes needs to save $18. */
7389 else if (TARGET_MIPS16
7390 && regno != GP_REG_FIRST + 31
7391 && ! M16_REG_P (regno))
7393 if (! store_p)
7394 reg_rtx = gen_rtx (REG, gpr_mode, 6);
7395 else
7397 reg_rtx = gen_rtx (REG, gpr_mode, 3);
7398 emit_move_insn (reg_rtx,
7399 gen_rtx (REG, gpr_mode, regno));
7402 else
7403 reg_rtx = gen_rtx (REG, gpr_mode, regno);
7405 if (store_p)
7406 mips_emit_frame_related_store (mem_rtx, reg_rtx, gp_offset);
7407 else
7409 emit_move_insn (reg_rtx, mem_rtx);
7410 if (TARGET_MIPS16
7411 && regno != GP_REG_FIRST + 31
7412 && ! M16_REG_P (regno))
7413 emit_move_insn (gen_rtx (REG, gpr_mode, regno),
7414 reg_rtx);
7417 /* If the restore is being supressed, still take into account
7418 the offset at which it is stored. */
7419 if (BITSET_P (real_mask, regno - GP_REG_FIRST))
7420 gp_offset -= GET_MODE_SIZE (gpr_mode);
7423 else
7424 base_reg_rtx = 0, base_offset = 0;
7426 /* Save floating point registers if needed. */
7427 if (fmask)
7429 /* Pick which pointer to use as a base register. */
7430 fp_offset = cfun->machine->frame.fp_sp_offset;
7431 end_offset = fp_offset - (cfun->machine->frame.fp_reg_size
7432 - UNITS_PER_HWFPVALUE);
7434 if (fp_offset < 0 || end_offset < 0)
7435 internal_error
7436 ("fp_offset (%ld) or end_offset (%ld) is less than zero",
7437 (long) fp_offset, (long) end_offset);
7439 else if (fp_offset < 32768)
7440 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
7442 else if (base_reg_rtx != 0
7443 && (unsigned HOST_WIDE_INT) (base_offset - fp_offset) < 32768
7444 && (unsigned HOST_WIDE_INT) (base_offset - end_offset) < 32768)
7445 ; /* already set up for gp registers above */
7447 else if (large_reg != 0
7448 && (unsigned HOST_WIDE_INT) (large_offset - fp_offset) < 32768
7449 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
7451 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
7452 base_offset = large_offset;
7453 if (Pmode == DImode)
7454 emit_insn (gen_adddi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7455 else
7456 emit_insn (gen_addsi3 (base_reg_rtx, large_reg, stack_pointer_rtx));
7458 else
7460 base_offset = fp_offset;
7461 base_reg_rtx = mips_add_large_offset_to_sp (fp_offset);
7464 /* This loop must iterate over the same space as its companion in
7465 compute_frame_size. */
7466 for (regno = (FP_REG_LAST - FP_INC + 1);
7467 regno >= FP_REG_FIRST;
7468 regno -= FP_INC)
7469 if (BITSET_P (fmask, regno - FP_REG_FIRST))
7471 enum machine_mode sz = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
7472 rtx reg_rtx = gen_rtx (REG, sz, regno);
7473 rtx mem_rtx = gen_rtx (MEM, sz,
7474 gen_rtx (PLUS, Pmode, base_reg_rtx,
7475 GEN_INT (fp_offset
7476 - base_offset)));
7477 if (! current_function_calls_eh_return)
7478 RTX_UNCHANGING_P (mem_rtx) = 1;
7480 if (store_p)
7481 mips_emit_frame_related_store (mem_rtx, reg_rtx, fp_offset);
7482 else
7483 emit_move_insn (reg_rtx, mem_rtx);
7485 fp_offset -= UNITS_PER_HWFPVALUE;
7490 /* Set up the stack and frame (if desired) for the function. */
7492 static void
7493 mips_output_function_prologue (file, size)
7494 FILE *file;
7495 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
7497 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7498 const char *fnname;
7499 #endif
7500 HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
7502 /* ??? When is this really needed? At least the GNU assembler does not
7503 need the source filename more than once in the file, beyond what is
7504 emitted by the debug information. */
7505 if (!TARGET_GAS)
7506 ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
7508 #ifdef SDB_DEBUGGING_INFO
7509 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
7510 ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
7511 #endif
7513 /* In mips16 mode, we may need to generate a 32 bit to handle
7514 floating point arguments. The linker will arrange for any 32 bit
7515 functions to call this stub, which will then jump to the 16 bit
7516 function proper. */
7517 if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
7518 && current_function_args_info.fp_code != 0)
7519 build_mips16_function_stub (file);
7521 inside_function = 1;
7523 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7524 /* Get the function name the same way that toplev.c does before calling
7525 assemble_start_function. This is needed so that the name used here
7526 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
7527 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7529 if (!flag_inhibit_size_directive)
7531 fputs ("\t.ent\t", file);
7532 assemble_name (file, fnname);
7533 fputs ("\n", file);
7536 assemble_name (file, fnname);
7537 fputs (":\n", file);
7538 #endif
7540 if (!flag_inhibit_size_directive)
7542 /* .frame FRAMEREG, FRAMESIZE, RETREG */
7543 fprintf (file,
7544 "\t.frame\t%s,%ld,%s\t\t# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
7545 (reg_names[(frame_pointer_needed)
7546 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
7547 ((frame_pointer_needed && TARGET_MIPS16)
7548 ? ((long) tsize - current_function_outgoing_args_size)
7549 : (long) tsize),
7550 reg_names[GP_REG_FIRST + 31],
7551 cfun->machine->frame.var_size,
7552 cfun->machine->frame.num_gp,
7553 cfun->machine->frame.num_fp,
7554 current_function_outgoing_args_size,
7555 cfun->machine->frame.extra_size);
7557 /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
7558 fprintf (file, "\t.mask\t0x%08lx,%ld\n\t.fmask\t0x%08lx,%ld\n",
7559 cfun->machine->frame.mask,
7560 cfun->machine->frame.gp_save_offset,
7561 cfun->machine->frame.fmask,
7562 cfun->machine->frame.fp_save_offset);
7564 /* Require:
7565 OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
7566 HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
7569 if (mips_entry && ! mips_can_use_return_insn ())
7571 int save16 = BITSET_P (cfun->machine->frame.mask, 16);
7572 int save17 = BITSET_P (cfun->machine->frame.mask, 17);
7573 int save31 = BITSET_P (cfun->machine->frame.mask, 31);
7574 int savearg = 0;
7575 rtx insn;
7577 /* Look through the initial insns to see if any of them store
7578 the function parameters into the incoming parameter storage
7579 area. If they do, we delete the insn, and save the register
7580 using the entry pseudo-instruction instead. We don't try to
7581 look past a label, jump, or call. */
7582 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
7584 rtx note, set, src, dest, base, offset;
7585 int hireg;
7587 if (GET_CODE (insn) == CODE_LABEL
7588 || GET_CODE (insn) == JUMP_INSN
7589 || GET_CODE (insn) == CALL_INSN)
7590 break;
7591 if (GET_CODE (insn) != INSN)
7592 continue;
7593 set = PATTERN (insn);
7594 if (GET_CODE (set) != SET)
7595 continue;
7597 /* An insn storing a function parameter will still have a
7598 REG_EQUIV note on it mentioning the argument pointer. */
7599 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
7600 if (note == NULL_RTX)
7601 continue;
7602 if (! reg_mentioned_p (arg_pointer_rtx, XEXP (note, 0)))
7603 continue;
7605 src = SET_SRC (set);
7606 if (GET_CODE (src) != REG
7607 || REGNO (src) < GP_REG_FIRST + 4
7608 || REGNO (src) > GP_REG_FIRST + 7)
7609 continue;
7611 dest = SET_DEST (set);
7612 if (GET_CODE (dest) != MEM)
7613 continue;
7614 if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned) UNITS_PER_WORD)
7616 else if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned)2 * UNITS_PER_WORD
7617 && REGNO (src) < GP_REG_FIRST + 7)
7619 else
7620 continue;
7621 offset = const0_rtx;
7622 base = eliminate_constant_term (XEXP (dest, 0), &offset);
7623 if (GET_CODE (base) != REG
7624 || GET_CODE (offset) != CONST_INT)
7625 continue;
7626 if (REGNO (base) == (unsigned) STACK_POINTER_REGNUM
7627 && INTVAL (offset) == tsize + (REGNO (src) - 4) * UNITS_PER_WORD)
7629 else if (REGNO (base) == (unsigned) HARD_FRAME_POINTER_REGNUM
7630 && (INTVAL (offset)
7631 == (tsize
7632 + (REGNO (src) - 4) * UNITS_PER_WORD
7633 - current_function_outgoing_args_size)))
7635 else
7636 continue;
7638 /* This insn stores a parameter onto the stack, in the same
7639 location where the entry pseudo-instruction will put it.
7640 Delete the insn, and arrange to tell the entry
7641 instruction to save the register. */
7642 PUT_CODE (insn, NOTE);
7643 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7644 NOTE_SOURCE_FILE (insn) = 0;
7646 hireg = (REGNO (src)
7647 + HARD_REGNO_NREGS (REGNO (src), GET_MODE (dest))
7648 - 1);
7649 if (hireg > savearg)
7650 savearg = hireg;
7653 /* If this is a varargs function, we need to save all the
7654 registers onto the stack anyhow. */
7655 if (current_function_stdarg)
7656 savearg = GP_REG_FIRST + 7;
7658 fprintf (file, "\tentry\t");
7659 if (savearg > 0)
7661 if (savearg == GP_REG_FIRST + 4)
7662 fprintf (file, "%s", reg_names[savearg]);
7663 else
7664 fprintf (file, "%s-%s", reg_names[GP_REG_FIRST + 4],
7665 reg_names[savearg]);
7667 if (save16 || save17)
7669 if (savearg > 0)
7670 fprintf (file, ",");
7671 fprintf (file, "%s", reg_names[GP_REG_FIRST + 16]);
7672 if (save17)
7673 fprintf (file, "-%s", reg_names[GP_REG_FIRST + 17]);
7675 if (save31)
7677 if (savearg > 0 || save16 || save17)
7678 fprintf (file, ",");
7679 fprintf (file, "%s", reg_names[GP_REG_FIRST + 31]);
7681 fprintf (file, "\n");
7684 if (TARGET_ABICALLS && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7686 const char *const sp_str = reg_names[STACK_POINTER_REGNUM];
7688 fprintf (file, "\t.set\tnoreorder\n\t.cpload\t%s\n\t.set\treorder\n",
7689 reg_names[PIC_FUNCTION_ADDR_REGNUM]);
7690 if (tsize > 0)
7692 fprintf (file, "\t%s\t%s,%s,%ld\n",
7693 (Pmode == DImode ? "dsubu" : "subu"),
7694 sp_str, sp_str, (long) tsize);
7695 fprintf (file, "\t.cprestore %ld\n", cfun->machine->frame.args_size);
7698 if (dwarf2out_do_frame ())
7699 dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, tsize);
7703 /* Expand the prologue into a bunch of separate insns. */
7705 void
7706 mips_expand_prologue ()
7708 int regno;
7709 HOST_WIDE_INT tsize;
7710 rtx tmp_rtx = 0;
7711 int last_arg_is_vararg_marker = 0;
7712 tree fndecl = current_function_decl;
7713 tree fntype = TREE_TYPE (fndecl);
7714 tree fnargs = DECL_ARGUMENTS (fndecl);
7715 rtx next_arg_reg;
7716 int i;
7717 tree next_arg;
7718 tree cur_arg;
7719 CUMULATIVE_ARGS args_so_far;
7720 rtx reg_18_save = NULL_RTX;
7721 int store_args_on_stack = (mips_abi == ABI_32 || mips_abi == ABI_O64)
7722 && (! mips_entry || mips_can_use_return_insn ());
7724 /* If struct value address is treated as the first argument, make it so. */
7725 if (aggregate_value_p (DECL_RESULT (fndecl))
7726 && ! current_function_returns_pcc_struct
7727 && struct_value_incoming_rtx == 0)
7729 tree type = build_pointer_type (fntype);
7730 tree function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
7732 DECL_ARG_TYPE (function_result_decl) = type;
7733 TREE_CHAIN (function_result_decl) = fnargs;
7734 fnargs = function_result_decl;
7737 /* For arguments passed in registers, find the register number
7738 of the first argument in the variable part of the argument list,
7739 otherwise GP_ARG_LAST+1. Note also if the last argument is
7740 the varargs special argument, and treat it as part of the
7741 variable arguments.
7743 This is only needed if store_args_on_stack is true. */
7745 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
7746 regno = GP_ARG_FIRST;
7748 for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
7750 tree passed_type = DECL_ARG_TYPE (cur_arg);
7751 enum machine_mode passed_mode = TYPE_MODE (passed_type);
7752 rtx entry_parm;
7754 if (TREE_ADDRESSABLE (passed_type))
7756 passed_type = build_pointer_type (passed_type);
7757 passed_mode = Pmode;
7760 entry_parm = FUNCTION_ARG (args_so_far, passed_mode, passed_type, 1);
7762 FUNCTION_ARG_ADVANCE (args_so_far, passed_mode, passed_type, 1);
7763 next_arg = TREE_CHAIN (cur_arg);
7765 if (entry_parm && store_args_on_stack)
7767 if (next_arg == 0
7768 && DECL_NAME (cur_arg)
7769 && ((0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7770 "__builtin_va_alist"))
7771 || (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7772 "va_alist"))))
7774 last_arg_is_vararg_marker = 1;
7775 if (GET_CODE (entry_parm) == REG)
7776 regno = REGNO (entry_parm);
7777 else
7778 regno = GP_ARG_LAST + 1;
7779 break;
7781 else
7783 int words;
7785 if (GET_CODE (entry_parm) != REG)
7786 abort ();
7788 /* passed in a register, so will get homed automatically */
7789 if (GET_MODE (entry_parm) == BLKmode)
7790 words = (int_size_in_bytes (passed_type) + 3) / 4;
7791 else
7792 words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
7794 regno = REGNO (entry_parm) + words - 1;
7797 else
7799 regno = GP_ARG_LAST+1;
7800 break;
7804 /* In order to pass small structures by value in registers compatibly with
7805 the MIPS compiler, we need to shift the value into the high part of the
7806 register. Function_arg has encoded a PARALLEL rtx, holding a vector of
7807 adjustments to be made as the next_arg_reg variable, so we split up the
7808 insns, and emit them separately. */
7810 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1);
7811 if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
7813 rtvec adjust = XVEC (next_arg_reg, 0);
7814 int num = GET_NUM_ELEM (adjust);
7816 for (i = 0; i < num; i++)
7818 rtx insn, pattern;
7820 pattern = RTVEC_ELT (adjust, i);
7821 if (GET_CODE (pattern) != SET
7822 || GET_CODE (SET_SRC (pattern)) != ASHIFT)
7823 abort_with_insn (pattern, "insn is not a shift");
7824 PUT_CODE (SET_SRC (pattern), ASHIFTRT);
7826 insn = emit_insn (pattern);
7828 /* Global life information isn't valid at this point, so we
7829 can't check whether these shifts are actually used. Mark
7830 them MAYBE_DEAD so that flow2 will remove them, and not
7831 complain about dead code in the prologue. */
7832 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
7833 REG_NOTES (insn));
7837 tsize = compute_frame_size (get_frame_size ());
7839 /* If this function is a varargs function, store any registers that
7840 would normally hold arguments ($4 - $7) on the stack. */
7841 if (store_args_on_stack
7842 && ((TYPE_ARG_TYPES (fntype) != 0
7843 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
7844 != void_type_node))
7845 || last_arg_is_vararg_marker))
7847 int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
7848 rtx ptr = stack_pointer_rtx;
7850 /* If we are doing svr4-abi, sp has already been decremented by tsize. */
7851 if (TARGET_ABICALLS)
7852 offset += tsize;
7854 for (; regno <= GP_ARG_LAST; regno++)
7856 if (offset != 0)
7857 ptr = gen_rtx (PLUS, Pmode, stack_pointer_rtx, GEN_INT (offset));
7858 emit_move_insn (gen_rtx (MEM, gpr_mode, ptr),
7859 gen_rtx (REG, gpr_mode, regno));
7861 offset += GET_MODE_SIZE (gpr_mode);
7865 /* If we are using the entry pseudo instruction, it will
7866 automatically subtract 32 from the stack pointer, so we don't
7867 need to. The entry pseudo instruction is emitted by
7868 function_prologue. */
7869 if (mips_entry && ! mips_can_use_return_insn ())
7871 if (tsize > 0 && tsize <= 32 && frame_pointer_needed)
7873 rtx insn;
7875 /* If we are using a frame pointer with a small stack frame,
7876 we need to initialize it here since it won't be done
7877 below. */
7878 if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7880 rtx incr = GEN_INT (current_function_outgoing_args_size);
7881 if (Pmode == DImode)
7882 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7883 stack_pointer_rtx,
7884 incr));
7885 else
7886 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7887 stack_pointer_rtx,
7888 incr));
7890 else if (Pmode == DImode)
7891 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7892 stack_pointer_rtx));
7893 else
7894 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7895 stack_pointer_rtx));
7897 RTX_FRAME_RELATED_P (insn) = 1;
7900 /* We may need to save $18, if it is used to call a function
7901 which may return a floating point value. Set up a sequence
7902 of instructions to do so. Later on we emit them at the right
7903 moment. */
7904 if (TARGET_MIPS16 && BITSET_P (cfun->machine->frame.mask, 18))
7906 rtx reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 3);
7907 long gp_offset, base_offset;
7909 gp_offset = cfun->machine->frame.gp_sp_offset;
7910 if (BITSET_P (cfun->machine->frame.mask, 16))
7911 gp_offset -= UNITS_PER_WORD;
7912 if (BITSET_P (cfun->machine->frame.mask, 17))
7913 gp_offset -= UNITS_PER_WORD;
7914 if (BITSET_P (cfun->machine->frame.mask, 31))
7915 gp_offset -= UNITS_PER_WORD;
7916 if (tsize > 32767)
7917 base_offset = tsize;
7918 else
7919 base_offset = 0;
7920 start_sequence ();
7921 emit_move_insn (reg_rtx,
7922 gen_rtx (REG, gpr_mode, GP_REG_FIRST + 18));
7923 emit_move_insn (gen_rtx (MEM, gpr_mode,
7924 gen_rtx (PLUS, Pmode, stack_pointer_rtx,
7925 GEN_INT (gp_offset
7926 - base_offset))),
7927 reg_rtx);
7928 reg_18_save = get_insns ();
7929 end_sequence ();
7932 if (tsize > 32)
7933 tsize -= 32;
7934 else
7936 tsize = 0;
7937 if (reg_18_save != NULL_RTX)
7938 emit_insn (reg_18_save);
7942 if (tsize > 0)
7944 rtx tsize_rtx = GEN_INT (tsize);
7946 /* If we are doing svr4-abi, sp move is done by
7947 function_prologue. In mips16 mode with a large frame, we
7948 save the registers before adjusting the stack. */
7949 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7950 && (!TARGET_MIPS16 || tsize <= 32767))
7952 rtx adjustment_rtx, insn, dwarf_pattern;
7954 if (tsize > 32767)
7956 adjustment_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
7957 emit_move_insn (adjustment_rtx, tsize_rtx);
7959 else
7960 adjustment_rtx = tsize_rtx;
7962 if (Pmode == DImode)
7963 insn = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
7964 adjustment_rtx));
7965 else
7966 insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
7967 adjustment_rtx));
7969 dwarf_pattern = gen_rtx_SET (Pmode, stack_pointer_rtx,
7970 plus_constant (stack_pointer_rtx,
7971 -tsize));
7973 mips_annotate_frame_insn (insn, dwarf_pattern);
7976 if (! mips_entry)
7977 save_restore_insns (1, tmp_rtx, tsize);
7978 else if (reg_18_save != NULL_RTX)
7979 emit_insn (reg_18_save);
7981 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7982 && TARGET_MIPS16
7983 && tsize > 32767)
7985 rtx reg_rtx;
7987 if (!frame_pointer_needed)
7988 abort ();
7990 reg_rtx = gen_rtx (REG, Pmode, 3);
7991 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
7992 emit_move_insn (reg_rtx, tsize_rtx);
7993 if (Pmode == DImode)
7994 emit_insn (gen_subdi3 (hard_frame_pointer_rtx,
7995 hard_frame_pointer_rtx,
7996 reg_rtx));
7997 else
7998 emit_insn (gen_subsi3 (hard_frame_pointer_rtx,
7999 hard_frame_pointer_rtx,
8000 reg_rtx));
8001 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
8004 if (frame_pointer_needed)
8006 rtx insn = 0;
8008 /* On the mips16, we encourage the use of unextended
8009 instructions when using the frame pointer by pointing the
8010 frame pointer ahead of the argument space allocated on
8011 the stack. */
8012 if ((! TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
8013 && TARGET_MIPS16
8014 && tsize > 32767)
8016 /* In this case, we have already copied the stack
8017 pointer into the frame pointer, above. We need only
8018 adjust for the outgoing argument size. */
8019 if (current_function_outgoing_args_size != 0)
8021 rtx incr = GEN_INT (current_function_outgoing_args_size);
8022 if (Pmode == DImode)
8023 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8024 hard_frame_pointer_rtx,
8025 incr));
8026 else
8027 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8028 hard_frame_pointer_rtx,
8029 incr));
8032 else if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
8034 rtx incr = GEN_INT (current_function_outgoing_args_size);
8035 if (Pmode == DImode)
8036 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8037 stack_pointer_rtx,
8038 incr));
8039 else
8040 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8041 stack_pointer_rtx,
8042 incr));
8044 else if (Pmode == DImode)
8045 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
8046 stack_pointer_rtx));
8047 else
8048 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
8049 stack_pointer_rtx));
8051 if (insn)
8052 RTX_FRAME_RELATED_P (insn) = 1;
8055 if (TARGET_ABICALLS && (mips_abi != ABI_32 && mips_abi != ABI_O64))
8056 emit_insn (gen_loadgp (XEXP (DECL_RTL (current_function_decl), 0),
8057 gen_rtx_REG (DImode, 25)));
8060 /* If we are profiling, make sure no instructions are scheduled before
8061 the call to mcount. */
8063 if (current_function_profile)
8064 emit_insn (gen_blockage ());
8067 /* Do any necessary cleanup after a function to restore stack, frame,
8068 and regs. */
8070 #define RA_MASK BITMASK_HIGH /* 1 << 31 */
8071 #define PIC_OFFSET_TABLE_MASK (1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
8073 static void
8074 mips_output_function_epilogue (file, size)
8075 FILE *file ATTRIBUTE_UNUSED;
8076 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
8078 const char *fnname = ""; /* FIXME: Correct initialisation? */
8079 rtx string;
8081 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8082 /* Get the function name the same way that toplev.c does before calling
8083 assemble_start_function. This is needed so that the name used here
8084 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
8085 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
8087 if (!flag_inhibit_size_directive)
8089 fputs ("\t.end\t", file);
8090 assemble_name (file, fnname);
8091 fputs ("\n", file);
8093 #endif
8095 if (TARGET_STATS)
8097 int num_gp_regs = cfun->machine->frame.gp_reg_size / 4;
8098 int num_fp_regs = cfun->machine->frame.fp_reg_size / 8;
8099 int num_regs = num_gp_regs + num_fp_regs;
8100 const char *name = fnname;
8102 if (name[0] == '*')
8103 name++;
8105 dslots_load_total += num_regs;
8107 fprintf (stderr,
8108 "%-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",
8109 name, frame_pointer_needed ? 'y' : 'n',
8110 (cfun->machine->frame.mask & RA_MASK) != 0 ? 'n' : 'y',
8111 current_function_calls_alloca ? 'y' : 'n',
8112 current_function_calls_setjmp ? 'y' : 'n',
8113 cfun->machine->frame.total_size,
8114 current_function_outgoing_args_size, num_gp_regs, num_fp_regs,
8115 dslots_load_total, dslots_load_filled,
8116 dslots_jump_total, dslots_jump_filled,
8117 num_refs[0], num_refs[1], num_refs[2]);
8119 fputc ('\n', stderr);
8122 /* Reset state info for each function. */
8123 inside_function = 0;
8124 ignore_line_number = 0;
8125 dslots_load_total = 0;
8126 dslots_jump_total = 0;
8127 dslots_load_filled = 0;
8128 dslots_jump_filled = 0;
8129 num_refs[0] = 0;
8130 num_refs[1] = 0;
8131 num_refs[2] = 0;
8132 mips_load_reg = 0;
8133 mips_load_reg2 = 0;
8135 while (string_constants != NULL)
8137 struct string_constant *next;
8139 next = string_constants->next;
8140 free (string_constants);
8141 string_constants = next;
8144 /* If any following function uses the same strings as this one, force
8145 them to refer those strings indirectly. Nearby functions could
8146 refer them using pc-relative addressing, but it isn't safe in
8147 general. For instance, some functions may be placed in sections
8148 other than .text, and we don't know whether they be close enough
8149 to this one. In large files, even other .text functions can be
8150 too far away. */
8151 for (string = mips16_strings; string != 0; string = XEXP (string, 1))
8152 SYMBOL_REF_FLAG (XEXP (string, 0)) = 0;
8153 free_EXPR_LIST_list (&mips16_strings);
8155 /* Restore the output file if optimizing the GP (optimizing the GP causes
8156 the text to be diverted to a tempfile, so that data decls come before
8157 references to the data). */
8158 if (TARGET_FILE_SWITCHING)
8160 asm_out_file = asm_out_data_file;
8161 data_section ();
8165 /* Expand the epilogue into a bunch of separate insns. */
8167 void
8168 mips_expand_epilogue ()
8170 HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
8171 rtx tsize_rtx = GEN_INT (tsize);
8172 rtx tmp_rtx = (rtx)0;
8174 if (mips_can_use_return_insn ())
8176 emit_jump_insn (gen_return ());
8177 return;
8180 if (mips_entry && ! mips_can_use_return_insn ())
8181 tsize -= 32;
8183 if (tsize > 32767 && ! TARGET_MIPS16)
8185 tmp_rtx = gen_rtx_REG (Pmode, MIPS_TEMP1_REGNUM);
8186 emit_move_insn (tmp_rtx, tsize_rtx);
8187 tsize_rtx = tmp_rtx;
8190 if (tsize > 0)
8192 long orig_tsize = tsize;
8194 if (frame_pointer_needed)
8196 emit_insn (gen_blockage ());
8198 /* On the mips16, the frame pointer is offset from the stack
8199 pointer by current_function_outgoing_args_size. We
8200 account for that by changing tsize. Note that this can
8201 actually make tsize negative. */
8202 if (TARGET_MIPS16)
8204 tsize -= current_function_outgoing_args_size;
8206 /* If we have a large frame, it's easier to add to $6
8207 than to $sp, since the mips16 has no instruction to
8208 add a register to $sp. */
8209 if (orig_tsize > 32767)
8211 rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
8213 emit_move_insn (g6_rtx, GEN_INT (tsize));
8214 if (Pmode == DImode)
8215 emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
8216 hard_frame_pointer_rtx,
8217 g6_rtx));
8218 else
8219 emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
8220 hard_frame_pointer_rtx,
8221 g6_rtx));
8222 tsize = 0;
8225 if (tsize && tsize != orig_tsize)
8226 tsize_rtx = GEN_INT (tsize);
8229 if (Pmode == DImode)
8230 emit_insn (gen_movdi (stack_pointer_rtx, hard_frame_pointer_rtx));
8231 else
8232 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
8235 /* The GP/PIC register is implicitly used by all SYMBOL_REFs, so if we
8236 are going to restore it, then we must emit a blockage insn to
8237 prevent the scheduler from moving the restore out of the epilogue. */
8238 else if (TARGET_ABICALLS && mips_abi != ABI_32 && mips_abi != ABI_O64
8239 && (cfun->machine->frame.mask
8240 & (1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))))
8241 emit_insn (gen_blockage ());
8243 save_restore_insns (0, tmp_rtx, orig_tsize);
8245 /* In mips16 mode with a large frame, we adjust the stack
8246 pointer before restoring the registers. In this case, we
8247 should always be using a frame pointer, so everything should
8248 have been handled above. */
8249 if (tsize > 32767 && TARGET_MIPS16)
8250 abort ();
8252 if (current_function_calls_eh_return)
8254 rtx eh_ofs = EH_RETURN_STACKADJ_RTX;
8255 if (Pmode == DImode)
8256 emit_insn (gen_adddi3 (eh_ofs, eh_ofs, tsize_rtx));
8257 else
8258 emit_insn (gen_addsi3 (eh_ofs, eh_ofs, tsize_rtx));
8259 tsize_rtx = eh_ofs;
8262 emit_insn (gen_blockage ());
8264 if (tsize != 0 || current_function_calls_eh_return)
8266 if (!TARGET_MIPS16)
8268 if (Pmode == DImode)
8269 emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
8270 tsize_rtx));
8271 else
8272 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8273 tsize_rtx));
8275 else
8277 /* We need to work around not being able to add a register
8278 to the stack pointer directly. Use register $6 as an
8279 intermediate step. */
8281 rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
8283 if (Pmode == DImode)
8285 emit_insn (gen_movdi (g6_rtx, stack_pointer_rtx));
8286 emit_insn (gen_adddi3 (g6_rtx, g6_rtx, tsize_rtx));
8287 emit_insn (gen_movdi (stack_pointer_rtx, g6_rtx));
8289 else
8291 emit_insn (gen_movsi (g6_rtx, stack_pointer_rtx));
8292 emit_insn (gen_addsi3 (g6_rtx, g6_rtx, tsize_rtx));
8293 emit_insn (gen_movsi (stack_pointer_rtx, g6_rtx));
8300 /* The mips16 loads the return address into $7, not $31. */
8301 if (TARGET_MIPS16 && (cfun->machine->frame.mask & RA_MASK) != 0)
8302 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
8303 GP_REG_FIRST + 7)));
8304 else
8305 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
8306 GP_REG_FIRST + 31)));
8309 /* Return nonzero if this function is known to have a null epilogue.
8310 This allows the optimizer to omit jumps to jumps if no stack
8311 was created. */
8314 mips_can_use_return_insn ()
8316 tree return_type;
8318 if (! reload_completed)
8319 return 0;
8321 if (regs_ever_live[31] || current_function_profile)
8322 return 0;
8324 return_type = DECL_RESULT (current_function_decl);
8326 /* In mips16 mode, a function which returns a floating point value
8327 needs to arrange to copy the return value into the floating point
8328 registers. */
8329 if (TARGET_MIPS16
8330 && mips16_hard_float
8331 && ! aggregate_value_p (return_type)
8332 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
8333 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
8334 return 0;
8336 if (cfun->machine->frame.initialized)
8337 return cfun->machine->frame.total_size == 0;
8339 return compute_frame_size (get_frame_size ()) == 0;
8342 /* Returns nonzero if X contains a SYMBOL_REF. */
8344 static int
8345 symbolic_expression_p (x)
8346 rtx x;
8348 if (GET_CODE (x) == SYMBOL_REF)
8349 return 1;
8351 if (GET_CODE (x) == CONST)
8352 return symbolic_expression_p (XEXP (x, 0));
8354 if (GET_RTX_CLASS (GET_CODE (x)) == '1')
8355 return symbolic_expression_p (XEXP (x, 0));
8357 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
8358 || GET_RTX_CLASS (GET_CODE (x)) == '2')
8359 return (symbolic_expression_p (XEXP (x, 0))
8360 || symbolic_expression_p (XEXP (x, 1)));
8362 return 0;
8365 /* Choose the section to use for the constant rtx expression X that has
8366 mode MODE. */
8368 static void
8369 mips_select_rtx_section (mode, x, align)
8370 enum machine_mode mode;
8371 rtx x;
8372 unsigned HOST_WIDE_INT align;
8374 if (TARGET_MIPS16)
8376 /* In mips16 mode, the constant table always goes in the same section
8377 as the function, so that constants can be loaded using PC relative
8378 addressing. */
8379 function_section (current_function_decl);
8381 else if (TARGET_EMBEDDED_DATA)
8383 /* For embedded applications, always put constants in read-only data,
8384 in order to reduce RAM usage. */
8385 mergeable_constant_section (mode, align, 0);
8387 else
8389 /* For hosted applications, always put constants in small data if
8390 possible, as this gives the best performance. */
8391 /* ??? Consider using mergable small data sections. */
8393 if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
8394 && mips_section_threshold > 0)
8395 SMALL_DATA_SECTION ();
8396 else if (flag_pic && symbolic_expression_p (x))
8398 if (targetm.have_named_sections)
8399 named_section (NULL_TREE, ".data.rel.ro", 3);
8400 else
8401 data_section ();
8403 else
8404 mergeable_constant_section (mode, align, 0);
8408 /* Choose the section to use for DECL. RELOC is true if its value contains
8409 any relocatable expression.
8411 Some of the logic used here needs to be replicated in
8412 mips_encode_section_info so that references to these symbols are
8413 done correctly. Specifically, at least all symbols assigned here
8414 to rom (.text and/or .rodata) must not be referenced via
8415 mips_encode_section_info with %gprel, as the rom might be too far
8416 away.
8418 If you need to make a change here, you probably should check
8419 mips_encode_section_info to see if it needs a similar change.
8421 ??? This would be fixed by implementing targetm.is_small_data_p. */
8423 static void
8424 mips_select_section (decl, reloc, align)
8425 tree decl;
8426 int reloc;
8427 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
8429 int size = int_size_in_bytes (TREE_TYPE (decl));
8431 if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
8432 && TREE_CODE (decl) == STRING_CST
8433 && !flag_writable_strings)
8434 /* For embedded position independent code, put constant strings in the
8435 text section, because the data section is limited to 64K in size.
8436 For mips16 code, put strings in the text section so that a PC
8437 relative load instruction can be used to get their address. */
8438 text_section ();
8439 else if (TARGET_EMBEDDED_DATA)
8441 /* For embedded applications, always put an object in read-only data
8442 if possible, in order to reduce RAM usage. */
8444 if (((TREE_CODE (decl) == VAR_DECL
8445 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
8446 && DECL_INITIAL (decl)
8447 && (DECL_INITIAL (decl) == error_mark_node
8448 || TREE_CONSTANT (DECL_INITIAL (decl))))
8449 /* Deal with calls from output_constant_def_contents. */
8450 || (TREE_CODE (decl) != VAR_DECL
8451 && (TREE_CODE (decl) != STRING_CST
8452 || !flag_writable_strings)))
8453 && ! (flag_pic && reloc))
8454 readonly_data_section ();
8455 else if (size > 0 && size <= mips_section_threshold)
8456 SMALL_DATA_SECTION ();
8457 else
8458 data_section ();
8460 else
8462 /* For hosted applications, always put an object in small data if
8463 possible, as this gives the best performance. */
8465 if (size > 0 && size <= mips_section_threshold)
8466 SMALL_DATA_SECTION ();
8467 else if (((TREE_CODE (decl) == VAR_DECL
8468 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
8469 && DECL_INITIAL (decl)
8470 && (DECL_INITIAL (decl) == error_mark_node
8471 || TREE_CONSTANT (DECL_INITIAL (decl))))
8472 /* Deal with calls from output_constant_def_contents. */
8473 || (TREE_CODE (decl) != VAR_DECL
8474 && (TREE_CODE (decl) != STRING_CST
8475 || !flag_writable_strings)))
8476 && ! (flag_pic && reloc))
8477 readonly_data_section ();
8478 else
8479 data_section ();
8483 /* When optimizing for the $gp pointer, SYMBOL_REF_FLAG is set for all
8484 small objects.
8486 When generating embedded PIC code, SYMBOL_REF_FLAG is set for
8487 symbols which are not in the .text section.
8489 When generating mips16 code, SYMBOL_REF_FLAG is set for string
8490 constants which are put in the .text section. We also record the
8491 total length of all such strings; this total is used to decide
8492 whether we need to split the constant table, and need not be
8493 precisely correct.
8495 When not mips16 code nor embedded PIC, if a symbol is in a
8496 gp addressable section, SYMBOL_REF_FLAG is set prevent gcc from
8497 splitting the reference so that gas can generate a gp relative
8498 reference.
8500 When TARGET_EMBEDDED_DATA is set, we assume that all const
8501 variables will be stored in ROM, which is too far from %gp to use
8502 %gprel addressing. Note that (1) we include "extern const"
8503 variables in this, which mips_select_section doesn't, and (2) we
8504 can't always tell if they're really const (they might be const C++
8505 objects with non-const constructors), so we err on the side of
8506 caution and won't use %gprel anyway (otherwise we'd have to defer
8507 this decision to the linker/loader). The handling of extern consts
8508 is why the DECL_INITIAL macros differ from mips_select_section. */
8510 static void
8511 mips_encode_section_info (decl, first)
8512 tree decl;
8513 int first;
8515 if (TARGET_MIPS16)
8517 if (first && TREE_CODE (decl) == STRING_CST
8518 && ! flag_writable_strings
8519 /* If this string is from a function, and the function will
8520 go in a gnu linkonce section, then we can't directly
8521 access the string. This gets an assembler error
8522 "unsupported PC relative reference to different section".
8523 If we modify SELECT_SECTION to put it in function_section
8524 instead of text_section, it still fails because
8525 DECL_SECTION_NAME isn't set until assemble_start_function.
8526 If we fix that, it still fails because strings are shared
8527 among multiple functions, and we have cross section
8528 references again. We force it to work by putting string
8529 addresses in the constant pool and indirecting. */
8530 && (! current_function_decl
8531 || ! DECL_ONE_ONLY (current_function_decl)))
8533 rtx symref;
8535 symref = XEXP (TREE_CST_RTL (decl), 0);
8536 mips16_strings = alloc_EXPR_LIST (0, symref, mips16_strings);
8537 SYMBOL_REF_FLAG (symref) = 1;
8538 mips_string_length += TREE_STRING_LENGTH (decl);
8542 if (TARGET_EMBEDDED_DATA
8543 && (TREE_CODE (decl) == VAR_DECL
8544 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl))
8545 && (!DECL_INITIAL (decl)
8546 || TREE_CONSTANT (DECL_INITIAL (decl))))
8548 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 0;
8551 else if (TARGET_EMBEDDED_PIC)
8553 if (TREE_CODE (decl) == VAR_DECL)
8554 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8555 else if (TREE_CODE (decl) == FUNCTION_DECL)
8556 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 0;
8557 else if (TREE_CODE (decl) == STRING_CST
8558 && ! flag_writable_strings)
8559 SYMBOL_REF_FLAG (XEXP (TREE_CST_RTL (decl), 0)) = 0;
8560 else
8561 SYMBOL_REF_FLAG (XEXP (TREE_CST_RTL (decl), 0)) = 1;
8564 else if (TREE_CODE (decl) == VAR_DECL
8565 && DECL_SECTION_NAME (decl) != NULL_TREE
8566 && (0 == strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
8567 ".sdata")
8568 || 0 == strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
8569 ".sbss")))
8571 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8574 /* We can not perform GP optimizations on variables which are in
8575 specific sections, except for .sdata and .sbss which are
8576 handled above. */
8577 else if (TARGET_GP_OPT && TREE_CODE (decl) == VAR_DECL
8578 && DECL_SECTION_NAME (decl) == NULL_TREE
8579 && ! (TARGET_MIPS16 && TREE_PUBLIC (decl)
8580 && (DECL_COMMON (decl)
8581 || DECL_ONE_ONLY (decl)
8582 || DECL_WEAK (decl))))
8584 int size = int_size_in_bytes (TREE_TYPE (decl));
8586 if (size > 0 && size <= mips_section_threshold)
8587 SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
8592 /* Return register to use for a function return value with VALTYPE for
8593 function FUNC. MODE is used instead of VALTYPE for LIBCALLs. */
8596 mips_function_value (valtype, func, mode)
8597 tree valtype;
8598 tree func ATTRIBUTE_UNUSED;
8599 enum machine_mode mode;
8601 int reg = GP_RETURN;
8602 enum mode_class mclass;
8603 int unsignedp = 1;
8605 if (valtype)
8607 mode = TYPE_MODE (valtype);
8608 unsignedp = TREE_UNSIGNED (valtype);
8610 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote
8611 the mode just as PROMOTE_MODE does. */
8612 mode = promote_mode (valtype, mode, &unsignedp, 1);
8614 mclass = GET_MODE_CLASS (mode);
8616 if (mclass == MODE_FLOAT && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
8617 reg = FP_RETURN;
8619 else if (mclass == MODE_FLOAT && mode == TFmode)
8620 /* long doubles are really split between f0 and f2, not f1. Eek.
8621 Use DImode for each component, since GCC wants integer modes
8622 for subregs. */
8623 return gen_rtx_PARALLEL
8624 (VOIDmode,
8625 gen_rtvec (2,
8626 gen_rtx_EXPR_LIST (VOIDmode,
8627 gen_rtx_REG (DImode, FP_RETURN),
8628 GEN_INT (0)),
8629 gen_rtx_EXPR_LIST (VOIDmode,
8630 gen_rtx_REG (DImode, FP_RETURN + 2),
8631 GEN_INT (GET_MODE_SIZE (mode) / 2))));
8634 else if (mclass == MODE_COMPLEX_FLOAT
8635 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)
8637 enum machine_mode cmode = GET_MODE_INNER (mode);
8639 return gen_rtx_PARALLEL
8640 (VOIDmode,
8641 gen_rtvec (2,
8642 gen_rtx_EXPR_LIST (VOIDmode,
8643 gen_rtx_REG (cmode, FP_RETURN),
8644 GEN_INT (0)),
8645 gen_rtx_EXPR_LIST (VOIDmode,
8646 gen_rtx_REG (cmode, FP_RETURN + FP_INC),
8647 GEN_INT (GET_MODE_SIZE (cmode)))));
8650 else if (valtype && TREE_CODE (valtype) == RECORD_TYPE
8651 && mips_abi != ABI_32
8652 && mips_abi != ABI_O64
8653 && mips_abi != ABI_EABI)
8655 /* A struct with only one or two floating point fields is returned in
8656 the floating point registers. */
8657 tree field, fields[2];
8658 int i;
8660 for (i = 0, field = TYPE_FIELDS (valtype); field;
8661 field = TREE_CHAIN (field))
8663 if (TREE_CODE (field) != FIELD_DECL)
8664 continue;
8666 if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE || i >= 2)
8667 break;
8669 fields[i++] = field;
8672 /* Must check i, so that we reject structures with no elements. */
8673 if (! field)
8675 if (i == 1)
8677 /* The structure has DImode, but we don't allow DImode values
8678 in FP registers, so we use a PARALLEL even though it isn't
8679 strictly necessary. */
8680 enum machine_mode field_mode = TYPE_MODE (TREE_TYPE (fields[0]));
8682 return gen_rtx_PARALLEL
8683 (mode,
8684 gen_rtvec (1,
8685 gen_rtx_EXPR_LIST (VOIDmode,
8686 gen_rtx_REG (field_mode,
8687 FP_RETURN),
8688 const0_rtx)));
8691 else if (i == 2)
8693 enum machine_mode first_mode
8694 = TYPE_MODE (TREE_TYPE (fields[0]));
8695 enum machine_mode second_mode
8696 = TYPE_MODE (TREE_TYPE (fields[1]));
8697 HOST_WIDE_INT first_offset = int_byte_position (fields[0]);
8698 HOST_WIDE_INT second_offset = int_byte_position (fields[1]);
8700 return gen_rtx_PARALLEL
8701 (mode,
8702 gen_rtvec (2,
8703 gen_rtx_EXPR_LIST (VOIDmode,
8704 gen_rtx_REG (first_mode,
8705 FP_RETURN),
8706 GEN_INT (first_offset)),
8707 gen_rtx_EXPR_LIST (VOIDmode,
8708 gen_rtx_REG (second_mode,
8709 FP_RETURN + 2),
8710 GEN_INT (second_offset))));
8715 return gen_rtx_REG (mode, reg);
8718 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE. Return
8719 nonzero when an argument must be passed by reference. */
8722 function_arg_pass_by_reference (cum, mode, type, named)
8723 const CUMULATIVE_ARGS *cum;
8724 enum machine_mode mode;
8725 tree type;
8726 int named ATTRIBUTE_UNUSED;
8728 int size;
8730 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
8731 return 0;
8733 /* We must pass by reference if we would be both passing in registers
8734 and the stack. This is because any subsequent partial arg would be
8735 handled incorrectly in this case.
8737 ??? This is really a kludge. We should either fix GCC so that such
8738 a situation causes an abort and then do something in the MIPS port
8739 to prevent it, or add code to function.c to properly handle the case. */
8740 /* ??? cum can be NULL when called from mips_va_arg. The problem handled
8741 here hopefully is not relevant to mips_va_arg. */
8742 if (cum && MUST_PASS_IN_STACK (mode, type)
8743 && mips_abi != ABI_MEABI
8744 && FUNCTION_ARG (*cum, mode, type, named) != 0)
8745 return 1;
8747 /* Otherwise, we only do this if EABI is selected. */
8748 if (mips_abi != ABI_EABI)
8749 return 0;
8751 /* ??? How should SCmode be handled? */
8752 if (type == NULL_TREE || mode == DImode || mode == DFmode)
8753 return 0;
8755 size = int_size_in_bytes (type);
8756 return size == -1 || size > UNITS_PER_WORD;
8759 /* Return the class of registers for which a mode change from FROM to TO
8760 is invalid.
8762 In little-endian mode, the hi-lo registers are numbered backwards,
8763 so (subreg:SI (reg:DI hi) 0) gets the high word instead of the low
8764 word as intended.
8766 Similarly, when using paired floating-point registers, the first
8767 register holds the low word, regardless of endianness. So in big
8768 endian mode, (subreg:SI (reg:DF $f0) 0) does not get the high word
8769 as intended.
8771 Also, loading a 32-bit value into a 64-bit floating-point register
8772 will not sign-extend the value, despite what LOAD_EXTEND_OP says.
8773 We can't allow 64-bit float registers to change from a 32-bit
8774 mode to a 64-bit mode. */
8776 enum reg_class
8777 mips_cannot_change_mode_class (from, to)
8778 enum machine_mode from, to;
8780 if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
8782 if (TARGET_BIG_ENDIAN)
8783 return FP_REGS;
8784 if (TARGET_FLOAT64)
8785 return HI_AND_FP_REGS;
8786 return HI_REG;
8788 return NO_REGS;
8791 /* This function returns the register class required for a secondary
8792 register when copying between one of the registers in CLASS, and X,
8793 using MODE. If IN_P is nonzero, the copy is going from X to the
8794 register, otherwise the register is the source. A return value of
8795 NO_REGS means that no secondary register is required. */
8797 enum reg_class
8798 mips_secondary_reload_class (class, mode, x, in_p)
8799 enum reg_class class;
8800 enum machine_mode mode;
8801 rtx x;
8802 int in_p;
8804 enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
8805 int regno = -1;
8806 int gp_reg_p;
8808 if (GET_CODE (x) == SIGN_EXTEND)
8810 int off = 0;
8812 x = XEXP (x, 0);
8814 /* We may be called with reg_renumber NULL from regclass.
8815 ??? This is probably a bug. */
8816 if (reg_renumber)
8817 regno = true_regnum (x);
8818 else
8820 while (GET_CODE (x) == SUBREG)
8822 off += subreg_regno_offset (REGNO (SUBREG_REG (x)),
8823 GET_MODE (SUBREG_REG (x)),
8824 SUBREG_BYTE (x),
8825 GET_MODE (x));
8826 x = SUBREG_REG (x);
8829 if (GET_CODE (x) == REG)
8830 regno = REGNO (x) + off;
8833 /* 64-bit floating-point registers don't store 32-bit values
8834 in sign-extended form. The only way we can reload
8835 (sign_extend:DI (reg:SI $f0)) is by moving $f0 into
8836 an integer register using a 32-bit move. */
8837 if (FP_REG_P (regno))
8838 return (class == GR_REGS ? NO_REGS : GR_REGS);
8840 /* For the same reason, we can only reload (sign_extend:DI FOO) into
8841 a floating-point register when FOO is an integer register. */
8842 if (class == FP_REGS)
8843 return (GP_REG_P (regno) ? NO_REGS : GR_REGS);
8846 else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
8847 regno = true_regnum (x);
8849 gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
8851 /* We always require a general register when copying anything to
8852 HILO_REGNUM, except when copying an SImode value from HILO_REGNUM
8853 to a general register, or when copying from register 0. */
8854 if (class == HILO_REG && regno != GP_REG_FIRST + 0)
8855 return ((! in_p
8856 && gp_reg_p
8857 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8858 ? NO_REGS : gr_regs);
8859 else if (regno == HILO_REGNUM)
8860 return ((in_p
8861 && class == gr_regs
8862 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8863 ? NO_REGS : gr_regs);
8865 /* Copying from HI or LO to anywhere other than a general register
8866 requires a general register. */
8867 if (class == HI_REG || class == LO_REG || class == MD_REGS)
8869 if (TARGET_MIPS16 && in_p)
8871 /* We can't really copy to HI or LO at all in mips16 mode. */
8872 return M16_REGS;
8874 return gp_reg_p ? NO_REGS : gr_regs;
8876 if (MD_REG_P (regno))
8878 if (TARGET_MIPS16 && ! in_p)
8880 /* We can't really copy to HI or LO at all in mips16 mode. */
8881 return M16_REGS;
8883 return class == gr_regs ? NO_REGS : gr_regs;
8886 /* We can only copy a value to a condition code register from a
8887 floating point register, and even then we require a scratch
8888 floating point register. We can only copy a value out of a
8889 condition code register into a general register. */
8890 if (class == ST_REGS)
8892 if (in_p)
8893 return FP_REGS;
8894 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
8896 if (ST_REG_P (regno))
8898 if (! in_p)
8899 return FP_REGS;
8900 return class == GR_REGS ? NO_REGS : GR_REGS;
8903 if (class == FP_REGS)
8905 if (GET_CODE (x) == MEM)
8907 /* In this case we can use lwc1, swc1, ldc1 or sdc1. */
8908 return NO_REGS;
8910 else if (CONSTANT_P (x) && GET_MODE_CLASS (mode) == MODE_FLOAT)
8912 /* We can use the l.s and l.d macros to load floating-point
8913 constants. ??? For l.s, we could probably get better
8914 code by returning GR_REGS here. */
8915 return NO_REGS;
8917 else if (GP_REG_P (regno) || x == CONST0_RTX (mode))
8919 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
8920 return NO_REGS;
8922 else if (FP_REG_P (regno))
8924 /* In this case we can use mov.s or mov.d. */
8925 return NO_REGS;
8927 else
8929 /* Otherwise, we need to reload through an integer register. */
8930 return GR_REGS;
8934 /* In mips16 mode, going between memory and anything but M16_REGS
8935 requires an M16_REG. */
8936 if (TARGET_MIPS16)
8938 if (class != M16_REGS && class != M16_NA_REGS)
8940 if (gp_reg_p)
8941 return NO_REGS;
8942 return M16_REGS;
8944 if (! gp_reg_p)
8946 /* The stack pointer isn't a valid operand to an add instruction,
8947 so we need to load it into M16_REGS first. This can happen as
8948 a result of register elimination and form_sum converting
8949 (plus reg (plus SP CONST)) to (plus (plus reg SP) CONST). We
8950 need an extra register if the dest is the same as the other
8951 register. In that case, we can't fix the problem by loading SP
8952 into the dest first. */
8953 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
8954 && GET_CODE (XEXP (x, 1)) == REG
8955 && (XEXP (x, 0) == stack_pointer_rtx
8956 || XEXP (x, 1) == stack_pointer_rtx))
8957 return (class == M16_REGS ? M16_NA_REGS : M16_REGS);
8959 if (class == M16_REGS || class == M16_NA_REGS)
8960 return NO_REGS;
8961 return M16_REGS;
8965 return NO_REGS;
8968 /* This function returns the maximum number of consecutive registers
8969 needed to represent mode MODE in registers of class CLASS. */
8972 mips_class_max_nregs (class, mode)
8973 enum reg_class class;
8974 enum machine_mode mode;
8976 if (class == FP_REGS)
8977 return FP_INC;
8978 else
8979 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
8982 bool
8983 mips_valid_pointer_mode (mode)
8984 enum machine_mode mode;
8986 return (mode == SImode || (TARGET_64BIT && mode == DImode));
8990 /* For each mips16 function which refers to GP relative symbols, we
8991 use a pseudo register, initialized at the start of the function, to
8992 hold the $gp value. */
8995 mips16_gp_pseudo_reg ()
8997 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
8999 rtx const_gp;
9000 rtx insn, scan;
9002 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
9003 RTX_UNCHANGING_P (cfun->machine->mips16_gp_pseudo_rtx) = 1;
9005 /* We want to initialize this to a value which gcc will believe
9006 is constant. */
9007 const_gp = gen_rtx (CONST, Pmode,
9008 gen_rtx (REG, Pmode, GP_REG_FIRST + 28));
9010 start_sequence ();
9011 emit_move_insn (cfun->machine->mips16_gp_pseudo_rtx,
9012 const_gp);
9013 insn = get_insns ();
9014 end_sequence ();
9016 push_topmost_sequence ();
9017 /* We need to emit the initialization after the FUNCTION_BEG
9018 note, so that it will be integrated. */
9019 for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
9020 if (GET_CODE (scan) == NOTE
9021 && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
9022 break;
9023 if (scan == NULL_RTX)
9024 scan = get_insns ();
9025 insn = emit_insn_after (insn, scan);
9026 pop_topmost_sequence ();
9029 return cfun->machine->mips16_gp_pseudo_rtx;
9032 /* Return an RTX which represents the signed 16 bit offset from the
9033 $gp register for the given symbol. This is only used on the
9034 mips16. */
9037 mips16_gp_offset (sym)
9038 rtx sym;
9040 tree gp;
9042 if (GET_CODE (sym) != SYMBOL_REF
9043 || ! SYMBOL_REF_FLAG (sym))
9044 abort ();
9046 /* We use a special identifier to represent the value of the gp
9047 register. */
9048 gp = get_identifier ("__mips16_gp_value");
9050 return gen_rtx (CONST, Pmode,
9051 gen_rtx (MINUS, Pmode, sym,
9052 gen_rtx (SYMBOL_REF, Pmode,
9053 IDENTIFIER_POINTER (gp))));
9056 /* Return nonzero if the given RTX represents a signed 16 bit offset
9057 from the $gp register. */
9060 mips16_gp_offset_p (x)
9061 rtx x;
9063 if (GET_CODE (x) == CONST)
9064 x = XEXP (x, 0);
9066 /* It's OK to add a small integer value to a gp offset. */
9067 if (GET_CODE (x) == PLUS)
9069 if (GET_CODE (XEXP (x, 1)) == CONST_INT
9070 && SMALL_INT (XEXP (x, 1)))
9071 return mips16_gp_offset_p (XEXP (x, 0));
9072 if (GET_CODE (XEXP (x, 0)) == CONST_INT
9073 && SMALL_INT (XEXP (x, 0)))
9074 return mips16_gp_offset_p (XEXP (x, 1));
9075 return 0;
9078 /* Make sure it is in the form SYM - __mips16_gp_value. */
9079 return (GET_CODE (x) == MINUS
9080 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
9081 && SYMBOL_REF_FLAG (XEXP (x, 0))
9082 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
9083 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0);
9086 /* Output a GP offset. We don't want to print the subtraction of
9087 __mips16_gp_value; it is implicitly represented by the %gprel which
9088 should have been printed by the caller. */
9090 static void
9091 mips16_output_gp_offset (file, x)
9092 FILE *file;
9093 rtx x;
9095 if (GET_CODE (x) == CONST)
9096 x = XEXP (x, 0);
9098 if (GET_CODE (x) == PLUS)
9100 mips16_output_gp_offset (file, XEXP (x, 0));
9101 fputs ("+", file);
9102 mips16_output_gp_offset (file, XEXP (x, 1));
9103 return;
9106 if (GET_CODE (x) == MINUS
9107 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
9108 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0)
9110 mips16_output_gp_offset (file, XEXP (x, 0));
9111 return;
9114 output_addr_const (file, x);
9117 /* Return nonzero if a constant should not be output until after the
9118 function. This is true of most string constants, so that we can
9119 use a more efficient PC relative reference. However, a static
9120 inline function may never call assemble_function_end to write out
9121 the constant pool, so don't try to postpone the constant in that
9122 case.
9124 ??? It's really a bug that a static inline function can put stuff
9125 in the constant pool even if the function itself is not output.
9127 We record which string constants we've seen, so that we know which
9128 ones might use the more efficient reference. */
9131 mips16_constant_after_function_p (x)
9132 tree x;
9134 if (TREE_CODE (x) == STRING_CST
9135 && ! flag_writable_strings
9136 && current_function_decl != 0
9137 && ! DECL_DEFER_OUTPUT (current_function_decl)
9138 && ! (DECL_INLINE (current_function_decl)
9139 && ((! TREE_PUBLIC (current_function_decl)
9140 && ! TREE_ADDRESSABLE (current_function_decl)
9141 && ! flag_keep_inline_functions)
9142 || DECL_EXTERNAL (current_function_decl))))
9144 struct string_constant *n;
9146 n = (struct string_constant *) xmalloc (sizeof *n);
9147 n->label = XSTR (XEXP (TREE_CST_RTL (x), 0), 0);
9148 n->next = string_constants;
9149 string_constants = n;
9151 return 1;
9154 return 0;
9157 /* Validate a constant for the mips16. This rejects general symbolic
9158 addresses, which must be loaded from memory. If ADDR is nonzero,
9159 this should reject anything which is not a legal address. If
9160 ADDEND is nonzero, this is being added to something else. */
9163 mips16_constant (x, mode, addr, addend)
9164 rtx x;
9165 enum machine_mode mode;
9166 int addr;
9167 int addend;
9169 while (GET_CODE (x) == CONST)
9170 x = XEXP (x, 0);
9172 switch (GET_CODE (x))
9174 default:
9175 return 0;
9177 case PLUS:
9178 return (mips16_constant (XEXP (x, 0), mode, addr, 1)
9179 && mips16_constant (XEXP (x, 1), mode, addr, 1));
9181 case SYMBOL_REF:
9182 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
9183 return 0;
9184 if (CONSTANT_POOL_ADDRESS_P (x))
9185 return 1;
9187 /* If we aren't looking for a memory address, we can accept a GP
9188 relative symbol, which will have SYMBOL_REF_FLAG set; movsi
9189 knows how to handle this. We can always accept a string
9190 constant, which is the other case in which SYMBOL_REF_FLAG
9191 will be set. */
9192 if (! addr
9193 && ! addend
9194 && SYMBOL_REF_FLAG (x)
9195 && mode == (enum machine_mode) Pmode)
9196 return 1;
9198 /* We can accept a string constant, which will have
9199 SYMBOL_REF_FLAG set but must be recognized by name to
9200 distinguish from a GP accessible symbol. The name of a
9201 string constant will have been generated by
9202 ASM_GENERATE_INTERNAL_LABEL as called by output_constant_def. */
9203 if (SYMBOL_REF_FLAG (x))
9205 const char *name = XSTR (x, 0);
9207 return (name[0] == '*'
9208 && strncmp (name + 1, LOCAL_LABEL_PREFIX,
9209 sizeof LOCAL_LABEL_PREFIX - 1) == 0);
9212 return 0;
9214 case LABEL_REF:
9215 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
9216 return 0;
9217 return 1;
9219 case CONST_INT:
9220 if (addr && ! addend)
9221 return 0;
9222 return INTVAL (x) > - 0x10000 && INTVAL (x) <= 0xffff;
9224 case REG:
9225 /* We need to treat $gp as a legitimate constant, because
9226 mips16_gp_pseudo_reg assumes that. */
9227 return REGNO (x) == GP_REG_FIRST + 28;
9231 /* Write out code to move floating point arguments in or out of
9232 general registers. Output the instructions to FILE. FP_CODE is
9233 the code describing which arguments are present (see the comment at
9234 the definition of CUMULATIVE_ARGS in mips.h). FROM_FP_P is nonzero if
9235 we are copying from the floating point registers. */
9237 static void
9238 mips16_fp_args (file, fp_code, from_fp_p)
9239 FILE *file;
9240 int fp_code;
9241 int from_fp_p;
9243 const char *s;
9244 int gparg, fparg;
9245 unsigned int f;
9247 /* This code only works for the original 32 bit ABI and the O64 ABI. */
9248 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
9249 abort ();
9251 if (from_fp_p)
9252 s = "mfc1";
9253 else
9254 s = "mtc1";
9255 gparg = GP_ARG_FIRST;
9256 fparg = FP_ARG_FIRST;
9257 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
9259 if ((f & 3) == 1)
9261 if ((fparg & 1) != 0)
9262 ++fparg;
9263 fprintf (file, "\t%s\t%s,%s\n", s,
9264 reg_names[gparg], reg_names[fparg]);
9266 else if ((f & 3) == 2)
9268 if (TARGET_64BIT)
9269 fprintf (file, "\td%s\t%s,%s\n", s,
9270 reg_names[gparg], reg_names[fparg]);
9271 else
9273 if ((fparg & 1) != 0)
9274 ++fparg;
9275 if (TARGET_BIG_ENDIAN)
9276 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
9277 reg_names[gparg], reg_names[fparg + 1], s,
9278 reg_names[gparg + 1], reg_names[fparg]);
9279 else
9280 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
9281 reg_names[gparg], reg_names[fparg], s,
9282 reg_names[gparg + 1], reg_names[fparg + 1]);
9283 ++gparg;
9284 ++fparg;
9287 else
9288 abort ();
9290 ++gparg;
9291 ++fparg;
9295 /* Build a mips16 function stub. This is used for functions which
9296 take aruments in the floating point registers. It is 32 bit code
9297 that moves the floating point args into the general registers, and
9298 then jumps to the 16 bit code. */
9300 static void
9301 build_mips16_function_stub (file)
9302 FILE *file;
9304 const char *fnname;
9305 char *secname, *stubname;
9306 tree stubid, stubdecl;
9307 int need_comma;
9308 unsigned int f;
9310 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9311 secname = (char *) alloca (strlen (fnname) + 20);
9312 sprintf (secname, ".mips16.fn.%s", fnname);
9313 stubname = (char *) alloca (strlen (fnname) + 20);
9314 sprintf (stubname, "__fn_stub_%s", fnname);
9315 stubid = get_identifier (stubname);
9316 stubdecl = build_decl (FUNCTION_DECL, stubid,
9317 build_function_type (void_type_node, NULL_TREE));
9318 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
9320 fprintf (file, "\t# Stub function for %s (", current_function_name);
9321 need_comma = 0;
9322 for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
9324 fprintf (file, "%s%s",
9325 need_comma ? ", " : "",
9326 (f & 3) == 1 ? "float" : "double");
9327 need_comma = 1;
9329 fprintf (file, ")\n");
9331 fprintf (file, "\t.set\tnomips16\n");
9332 function_section (stubdecl);
9333 ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
9335 /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
9336 within a .ent, and we can not emit another .ent. */
9337 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9338 fputs ("\t.ent\t", file);
9339 assemble_name (file, stubname);
9340 fputs ("\n", file);
9341 #endif
9343 assemble_name (file, stubname);
9344 fputs (":\n", file);
9346 /* We don't want the assembler to insert any nops here. */
9347 fprintf (file, "\t.set\tnoreorder\n");
9349 mips16_fp_args (file, current_function_args_info.fp_code, 1);
9351 fprintf (asm_out_file, "\t.set\tnoat\n");
9352 fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
9353 assemble_name (file, fnname);
9354 fprintf (file, "\n");
9355 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
9356 fprintf (asm_out_file, "\t.set\tat\n");
9358 /* Unfortunately, we can't fill the jump delay slot. We can't fill
9359 with one of the mfc1 instructions, because the result is not
9360 available for one instruction, so if the very first instruction
9361 in the function refers to the register, it will see the wrong
9362 value. */
9363 fprintf (file, "\tnop\n");
9365 fprintf (file, "\t.set\treorder\n");
9367 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9368 fputs ("\t.end\t", file);
9369 assemble_name (file, stubname);
9370 fputs ("\n", file);
9371 #endif
9373 fprintf (file, "\t.set\tmips16\n");
9375 function_section (current_function_decl);
9378 /* We keep a list of functions for which we have already built stubs
9379 in build_mips16_call_stub. */
9381 struct mips16_stub
9383 struct mips16_stub *next;
9384 char *name;
9385 int fpret;
9388 static struct mips16_stub *mips16_stubs;
9390 /* Build a call stub for a mips16 call. A stub is needed if we are
9391 passing any floating point values which should go into the floating
9392 point registers. If we are, and the call turns out to be to a 32
9393 bit function, the stub will be used to move the values into the
9394 floating point registers before calling the 32 bit function. The
9395 linker will magically adjust the function call to either the 16 bit
9396 function or the 32 bit stub, depending upon where the function call
9397 is actually defined.
9399 Similarly, we need a stub if the return value might come back in a
9400 floating point register.
9402 RETVAL, FNMEM, and ARG_SIZE are the values passed to the call insn
9403 (RETVAL is NULL if this is call rather than call_value). FP_CODE
9404 is the code built by function_arg. This function returns a nonzero
9405 value if it builds the call instruction itself. */
9408 build_mips16_call_stub (retval, fnmem, arg_size, fp_code)
9409 rtx retval;
9410 rtx fnmem;
9411 rtx arg_size;
9412 int fp_code;
9414 int fpret;
9415 rtx fn;
9416 const char *fnname;
9417 char *secname, *stubname;
9418 struct mips16_stub *l;
9419 tree stubid, stubdecl;
9420 int need_comma;
9421 unsigned int f;
9423 /* We don't need to do anything if we aren't in mips16 mode, or if
9424 we were invoked with the -msoft-float option. */
9425 if (! TARGET_MIPS16 || ! mips16_hard_float)
9426 return 0;
9428 /* Figure out whether the value might come back in a floating point
9429 register. */
9430 fpret = (retval != 0
9431 && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
9432 && GET_MODE_SIZE (GET_MODE (retval)) <= UNITS_PER_FPVALUE);
9434 /* We don't need to do anything if there were no floating point
9435 arguments and the value will not be returned in a floating point
9436 register. */
9437 if (fp_code == 0 && ! fpret)
9438 return 0;
9440 if (GET_CODE (fnmem) != MEM)
9441 abort ();
9442 fn = XEXP (fnmem, 0);
9444 /* We don't need to do anything if this is a call to a special
9445 mips16 support function. */
9446 if (GET_CODE (fn) == SYMBOL_REF
9447 && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
9448 return 0;
9450 /* This code will only work for o32 and o64 abis. The other ABI's
9451 require more sophisticated support. */
9452 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
9453 abort ();
9455 /* We can only handle SFmode and DFmode floating point return
9456 values. */
9457 if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
9458 abort ();
9460 /* If we're calling via a function pointer, then we must always call
9461 via a stub. There are magic stubs provided in libgcc.a for each
9462 of the required cases. Each of them expects the function address
9463 to arrive in register $2. */
9465 if (GET_CODE (fn) != SYMBOL_REF)
9467 char buf[30];
9468 tree id;
9469 rtx stub_fn, stub_mem, insn;
9471 /* ??? If this code is modified to support other ABI's, we need
9472 to handle PARALLEL return values here. */
9474 sprintf (buf, "__mips16_call_stub_%s%d",
9475 (fpret
9476 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
9477 : ""),
9478 fp_code);
9479 id = get_identifier (buf);
9480 stub_fn = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (id));
9481 stub_mem = gen_rtx (MEM, Pmode, stub_fn);
9483 emit_move_insn (gen_rtx (REG, Pmode, 2), fn);
9485 if (retval == NULL_RTX)
9486 insn = gen_call_internal0 (stub_mem, arg_size,
9487 gen_rtx (REG, SImode,
9488 GP_REG_FIRST + 31));
9489 else
9490 insn = gen_call_value_internal0 (retval, stub_mem, arg_size,
9491 gen_rtx (REG, SImode,
9492 GP_REG_FIRST + 31));
9493 insn = emit_call_insn (insn);
9495 /* Put the register usage information on the CALL. */
9496 if (GET_CODE (insn) != CALL_INSN)
9497 abort ();
9498 CALL_INSN_FUNCTION_USAGE (insn) =
9499 gen_rtx (EXPR_LIST, VOIDmode,
9500 gen_rtx (USE, VOIDmode, gen_rtx (REG, Pmode, 2)),
9501 CALL_INSN_FUNCTION_USAGE (insn));
9503 /* If we are handling a floating point return value, we need to
9504 save $18 in the function prologue. Putting a note on the
9505 call will mean that regs_ever_live[$18] will be true if the
9506 call is not eliminated, and we can check that in the prologue
9507 code. */
9508 if (fpret)
9509 CALL_INSN_FUNCTION_USAGE (insn) =
9510 gen_rtx (EXPR_LIST, VOIDmode,
9511 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
9512 CALL_INSN_FUNCTION_USAGE (insn));
9514 /* Return 1 to tell the caller that we've generated the call
9515 insn. */
9516 return 1;
9519 /* We know the function we are going to call. If we have already
9520 built a stub, we don't need to do anything further. */
9522 fnname = XSTR (fn, 0);
9523 for (l = mips16_stubs; l != NULL; l = l->next)
9524 if (strcmp (l->name, fnname) == 0)
9525 break;
9527 if (l == NULL)
9529 /* Build a special purpose stub. When the linker sees a
9530 function call in mips16 code, it will check where the target
9531 is defined. If the target is a 32 bit call, the linker will
9532 search for the section defined here. It can tell which
9533 symbol this section is associated with by looking at the
9534 relocation information (the name is unreliable, since this
9535 might be a static function). If such a section is found, the
9536 linker will redirect the call to the start of the magic
9537 section.
9539 If the function does not return a floating point value, the
9540 special stub section is named
9541 .mips16.call.FNNAME
9543 If the function does return a floating point value, the stub
9544 section is named
9545 .mips16.call.fp.FNNAME
9548 secname = (char *) alloca (strlen (fnname) + 40);
9549 sprintf (secname, ".mips16.call.%s%s",
9550 fpret ? "fp." : "",
9551 fnname);
9552 stubname = (char *) alloca (strlen (fnname) + 20);
9553 sprintf (stubname, "__call_stub_%s%s",
9554 fpret ? "fp_" : "",
9555 fnname);
9556 stubid = get_identifier (stubname);
9557 stubdecl = build_decl (FUNCTION_DECL, stubid,
9558 build_function_type (void_type_node, NULL_TREE));
9559 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
9561 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
9562 (fpret
9563 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
9564 : ""),
9565 fnname);
9566 need_comma = 0;
9567 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
9569 fprintf (asm_out_file, "%s%s",
9570 need_comma ? ", " : "",
9571 (f & 3) == 1 ? "float" : "double");
9572 need_comma = 1;
9574 fprintf (asm_out_file, ")\n");
9576 fprintf (asm_out_file, "\t.set\tnomips16\n");
9577 assemble_start_function (stubdecl, stubname);
9579 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9580 fputs ("\t.ent\t", asm_out_file);
9581 assemble_name (asm_out_file, stubname);
9582 fputs ("\n", asm_out_file);
9584 assemble_name (asm_out_file, stubname);
9585 fputs (":\n", asm_out_file);
9586 #endif
9588 /* We build the stub code by hand. That's the only way we can
9589 do it, since we can't generate 32 bit code during a 16 bit
9590 compilation. */
9592 /* We don't want the assembler to insert any nops here. */
9593 fprintf (asm_out_file, "\t.set\tnoreorder\n");
9595 mips16_fp_args (asm_out_file, fp_code, 0);
9597 if (! fpret)
9599 fprintf (asm_out_file, "\t.set\tnoat\n");
9600 fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
9601 fnname);
9602 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
9603 fprintf (asm_out_file, "\t.set\tat\n");
9604 /* Unfortunately, we can't fill the jump delay slot. We
9605 can't fill with one of the mtc1 instructions, because the
9606 result is not available for one instruction, so if the
9607 very first instruction in the function refers to the
9608 register, it will see the wrong value. */
9609 fprintf (asm_out_file, "\tnop\n");
9611 else
9613 fprintf (asm_out_file, "\tmove\t%s,%s\n",
9614 reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
9615 fprintf (asm_out_file, "\tjal\t%s\n", fnname);
9616 /* As above, we can't fill the delay slot. */
9617 fprintf (asm_out_file, "\tnop\n");
9618 if (GET_MODE (retval) == SFmode)
9619 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9620 reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
9621 else
9623 if (TARGET_BIG_ENDIAN)
9625 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9626 reg_names[GP_REG_FIRST + 2],
9627 reg_names[FP_REG_FIRST + 1]);
9628 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9629 reg_names[GP_REG_FIRST + 3],
9630 reg_names[FP_REG_FIRST + 0]);
9632 else
9634 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9635 reg_names[GP_REG_FIRST + 2],
9636 reg_names[FP_REG_FIRST + 0]);
9637 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
9638 reg_names[GP_REG_FIRST + 3],
9639 reg_names[FP_REG_FIRST + 1]);
9642 fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
9643 /* As above, we can't fill the delay slot. */
9644 fprintf (asm_out_file, "\tnop\n");
9647 fprintf (asm_out_file, "\t.set\treorder\n");
9649 #ifdef ASM_DECLARE_FUNCTION_SIZE
9650 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
9651 #endif
9653 #ifndef FUNCTION_NAME_ALREADY_DECLARED
9654 fputs ("\t.end\t", asm_out_file);
9655 assemble_name (asm_out_file, stubname);
9656 fputs ("\n", asm_out_file);
9657 #endif
9659 fprintf (asm_out_file, "\t.set\tmips16\n");
9661 /* Record this stub. */
9662 l = (struct mips16_stub *) xmalloc (sizeof *l);
9663 l->name = xstrdup (fnname);
9664 l->fpret = fpret;
9665 l->next = mips16_stubs;
9666 mips16_stubs = l;
9669 /* If we expect a floating point return value, but we've built a
9670 stub which does not expect one, then we're in trouble. We can't
9671 use the existing stub, because it won't handle the floating point
9672 value. We can't build a new stub, because the linker won't know
9673 which stub to use for the various calls in this object file.
9674 Fortunately, this case is illegal, since it means that a function
9675 was declared in two different ways in a single compilation. */
9676 if (fpret && ! l->fpret)
9677 error ("can not handle inconsistent calls to `%s'", fnname);
9679 /* If we are calling a stub which handles a floating point return
9680 value, we need to arrange to save $18 in the prologue. We do
9681 this by marking the function call as using the register. The
9682 prologue will later see that it is used, and emit code to save
9683 it. */
9685 if (l->fpret)
9687 rtx insn;
9689 if (retval == NULL_RTX)
9690 insn = gen_call_internal0 (fnmem, arg_size,
9691 gen_rtx (REG, SImode,
9692 GP_REG_FIRST + 31));
9693 else
9694 insn = gen_call_value_internal0 (retval, fnmem, arg_size,
9695 gen_rtx (REG, SImode,
9696 GP_REG_FIRST + 31));
9697 insn = emit_call_insn (insn);
9699 if (GET_CODE (insn) != CALL_INSN)
9700 abort ();
9702 CALL_INSN_FUNCTION_USAGE (insn) =
9703 gen_rtx (EXPR_LIST, VOIDmode,
9704 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
9705 CALL_INSN_FUNCTION_USAGE (insn));
9707 /* Return 1 to tell the caller that we've generated the call
9708 insn. */
9709 return 1;
9712 /* Return 0 to let the caller generate the call insn. */
9713 return 0;
9716 /* This function looks through the code for a function, and tries to
9717 optimize the usage of the $gp register. We arrange to copy $gp
9718 into a pseudo-register, and then let gcc's normal reload handling
9719 deal with the pseudo-register. Unfortunately, if reload choose to
9720 put the pseudo-register into a call-clobbered register, it will
9721 emit saves and restores for that register around any function
9722 calls. We don't need the saves, and it's faster to copy $gp than
9723 to do an actual restore. ??? This still means that we waste a
9724 stack slot.
9726 This is an optimization, and the code which gcc has actually
9727 generated is correct, so we do not need to catch all cases. */
9729 static void
9730 mips16_optimize_gp (first)
9731 rtx first;
9733 rtx gpcopy, slot, insn;
9735 /* Look through the instructions. Set GPCOPY to the register which
9736 holds a copy of $gp. Set SLOT to the stack slot where it is
9737 saved. If we find an instruction which sets GPCOPY to anything
9738 other than $gp or SLOT, then we can't use it. If we find an
9739 instruction which sets SLOT to anything other than GPCOPY, we
9740 can't use it. */
9742 gpcopy = NULL_RTX;
9743 slot = NULL_RTX;
9744 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9746 rtx set;
9748 if (! INSN_P (insn))
9749 continue;
9751 set = PATTERN (insn);
9753 /* We know that all references to memory will be inside a SET,
9754 because there is no other way to access memory on the mips16.
9755 We don't have to worry about a PARALLEL here, because the
9756 mips.md file will never generate them for memory references. */
9757 if (GET_CODE (set) != SET)
9758 continue;
9760 if (gpcopy == NULL_RTX
9761 && GET_CODE (SET_SRC (set)) == CONST
9762 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
9763 && REGNO (XEXP (SET_SRC (set), 0)) == GP_REG_FIRST + 28
9764 && GET_CODE (SET_DEST (set)) == REG
9765 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
9766 gpcopy = SET_DEST (set);
9767 else if (slot == NULL_RTX
9768 && gpcopy != NULL_RTX
9769 && GET_CODE (SET_DEST (set)) == MEM
9770 && GET_CODE (SET_SRC (set)) == REG
9771 && REGNO (SET_SRC (set)) == REGNO (gpcopy)
9772 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
9774 rtx base, offset;
9776 offset = const0_rtx;
9777 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
9778 if (GET_CODE (base) == REG
9779 && (REGNO (base) == STACK_POINTER_REGNUM
9780 || REGNO (base) == FRAME_POINTER_REGNUM))
9781 slot = SET_DEST (set);
9783 else if (gpcopy != NULL_RTX
9784 && (GET_CODE (SET_DEST (set)) == REG
9785 || GET_CODE (SET_DEST (set)) == SUBREG)
9786 && reg_overlap_mentioned_p (SET_DEST (set), gpcopy)
9787 && (GET_CODE (SET_DEST (set)) != REG
9788 || REGNO (SET_DEST (set)) != REGNO (gpcopy)
9789 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode
9790 || ((GET_CODE (SET_SRC (set)) != CONST
9791 || GET_CODE (XEXP (SET_SRC (set), 0)) != REG
9792 || (REGNO (XEXP (SET_SRC (set), 0))
9793 != GP_REG_FIRST + 28))
9794 && ! rtx_equal_p (SET_SRC (set), slot))))
9795 break;
9796 else if (slot != NULL_RTX
9797 && GET_CODE (SET_DEST (set)) == MEM
9798 && rtx_equal_p (SET_DEST (set), slot)
9799 && (GET_CODE (SET_SRC (set)) != REG
9800 || REGNO (SET_SRC (set)) != REGNO (gpcopy)))
9801 break;
9804 /* If we couldn't find a unique value for GPCOPY or SLOT, then try a
9805 different optimization. Any time we find a copy of $28 into a
9806 register, followed by an add of a symbol_ref to that register, we
9807 convert it to load the value from the constant table instead.
9808 The copy and add will take six bytes, just as the load and
9809 constant table entry will take six bytes. However, it is
9810 possible that the constant table entry will be shared.
9812 This could be a peephole optimization, but I don't know if the
9813 peephole code can call force_const_mem.
9815 Using the same register for the copy of $28 and the add of the
9816 symbol_ref is actually pretty likely, since the add instruction
9817 requires the destination and the first addend to be the same
9818 register. */
9820 if (insn != NULL_RTX || gpcopy == NULL_RTX || slot == NULL_RTX)
9822 rtx next;
9824 /* This optimization is only reasonable if the constant table
9825 entries are only 4 bytes. */
9826 if (Pmode != SImode)
9827 return;
9829 for (insn = first; insn != NULL_RTX; insn = next)
9831 rtx set1, set2;
9833 next = insn;
9836 next = NEXT_INSN (next);
9838 while (next != NULL_RTX
9839 && (GET_CODE (next) == NOTE
9840 || (GET_CODE (next) == INSN
9841 && (GET_CODE (PATTERN (next)) == USE
9842 || GET_CODE (PATTERN (next)) == CLOBBER))));
9844 if (next == NULL_RTX)
9845 break;
9847 if (! INSN_P (insn))
9848 continue;
9850 if (! INSN_P (next))
9851 continue;
9853 set1 = PATTERN (insn);
9854 if (GET_CODE (set1) != SET)
9855 continue;
9856 set2 = PATTERN (next);
9857 if (GET_CODE (set2) != SET)
9858 continue;
9860 if (GET_CODE (SET_DEST (set1)) == REG
9861 && GET_CODE (SET_SRC (set1)) == CONST
9862 && GET_CODE (XEXP (SET_SRC (set1), 0)) == REG
9863 && REGNO (XEXP (SET_SRC (set1), 0)) == GP_REG_FIRST + 28
9864 && rtx_equal_p (SET_DEST (set1), SET_DEST (set2))
9865 && GET_CODE (SET_SRC (set2)) == PLUS
9866 && rtx_equal_p (SET_DEST (set1), XEXP (SET_SRC (set2), 0))
9867 && mips16_gp_offset_p (XEXP (SET_SRC (set2), 1))
9868 && GET_CODE (XEXP (XEXP (SET_SRC (set2), 1), 0)) == MINUS)
9870 rtx sym;
9872 /* We've found a case we can change to load from the
9873 constant table. */
9875 sym = XEXP (XEXP (XEXP (SET_SRC (set2), 1), 0), 0);
9876 if (GET_CODE (sym) != SYMBOL_REF)
9877 abort ();
9878 emit_insn_after (gen_rtx (SET, VOIDmode, SET_DEST (set1),
9879 force_const_mem (Pmode, sym)),
9880 next);
9882 PUT_CODE (insn, NOTE);
9883 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9884 NOTE_SOURCE_FILE (insn) = 0;
9886 PUT_CODE (next, NOTE);
9887 NOTE_LINE_NUMBER (next) = NOTE_INSN_DELETED;
9888 NOTE_SOURCE_FILE (next) = 0;
9892 return;
9895 /* We can safely remove all assignments to SLOT from GPCOPY, and
9896 replace all assignments from SLOT to GPCOPY with assignments from
9897 $28. */
9899 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9901 rtx set;
9903 if (! INSN_P (insn))
9904 continue;
9906 set = PATTERN (insn);
9907 if (GET_CODE (set) != SET
9908 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode)
9909 continue;
9911 if (GET_CODE (SET_DEST (set)) == MEM
9912 && rtx_equal_p (SET_DEST (set), slot)
9913 && GET_CODE (SET_SRC (set)) == REG
9914 && REGNO (SET_SRC (set)) == REGNO (gpcopy))
9916 PUT_CODE (insn, NOTE);
9917 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9918 NOTE_SOURCE_FILE (insn) = 0;
9920 else if (GET_CODE (SET_DEST (set)) == REG
9921 && REGNO (SET_DEST (set)) == REGNO (gpcopy)
9922 && GET_CODE (SET_SRC (set)) == MEM
9923 && rtx_equal_p (SET_SRC (set), slot))
9925 emit_insn_after (gen_rtx (SET, Pmode, SET_DEST (set),
9926 gen_rtx (CONST, Pmode,
9927 gen_rtx (REG, Pmode,
9928 GP_REG_FIRST + 28))),
9929 insn);
9930 PUT_CODE (insn, NOTE);
9931 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9932 NOTE_SOURCE_FILE (insn) = 0;
9937 /* We keep a list of constants we which we have to add to internal
9938 constant tables in the middle of large functions. */
9940 struct constant
9942 struct constant *next;
9943 rtx value;
9944 rtx label;
9945 enum machine_mode mode;
9948 /* Add a constant to the list in *PCONSTANTS. */
9950 static rtx
9951 add_constant (pconstants, val, mode)
9952 struct constant **pconstants;
9953 rtx val;
9954 enum machine_mode mode;
9956 struct constant *c;
9958 for (c = *pconstants; c != NULL; c = c->next)
9959 if (mode == c->mode && rtx_equal_p (val, c->value))
9960 return c->label;
9962 c = (struct constant *) xmalloc (sizeof *c);
9963 c->value = val;
9964 c->mode = mode;
9965 c->label = gen_label_rtx ();
9966 c->next = *pconstants;
9967 *pconstants = c;
9968 return c->label;
9971 /* Dump out the constants in CONSTANTS after INSN. */
9973 static void
9974 dump_constants (constants, insn)
9975 struct constant *constants;
9976 rtx insn;
9978 struct constant *c;
9979 int align;
9981 c = constants;
9982 align = 0;
9983 while (c != NULL)
9985 rtx r;
9986 struct constant *next;
9988 switch (GET_MODE_SIZE (c->mode))
9990 case 1:
9991 align = 0;
9992 break;
9993 case 2:
9994 if (align < 1)
9995 insn = emit_insn_after (gen_align_2 (), insn);
9996 align = 1;
9997 break;
9998 case 4:
9999 if (align < 2)
10000 insn = emit_insn_after (gen_align_4 (), insn);
10001 align = 2;
10002 break;
10003 default:
10004 if (align < 3)
10005 insn = emit_insn_after (gen_align_8 (), insn);
10006 align = 3;
10007 break;
10010 insn = emit_label_after (c->label, insn);
10012 switch (c->mode)
10014 case QImode:
10015 r = gen_consttable_qi (c->value);
10016 break;
10017 case HImode:
10018 r = gen_consttable_hi (c->value);
10019 break;
10020 case SImode:
10021 r = gen_consttable_si (c->value);
10022 break;
10023 case SFmode:
10024 r = gen_consttable_sf (c->value);
10025 break;
10026 case DImode:
10027 r = gen_consttable_di (c->value);
10028 break;
10029 case DFmode:
10030 r = gen_consttable_df (c->value);
10031 break;
10032 default:
10033 abort ();
10036 insn = emit_insn_after (r, insn);
10038 next = c->next;
10039 free (c);
10040 c = next;
10043 emit_barrier_after (insn);
10046 /* Find the symbol in an address expression. */
10048 static rtx
10049 mips_find_symbol (addr)
10050 rtx addr;
10052 if (GET_CODE (addr) == MEM)
10053 addr = XEXP (addr, 0);
10054 while (GET_CODE (addr) == CONST)
10055 addr = XEXP (addr, 0);
10056 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
10057 return addr;
10058 if (GET_CODE (addr) == PLUS)
10060 rtx l1, l2;
10062 l1 = mips_find_symbol (XEXP (addr, 0));
10063 l2 = mips_find_symbol (XEXP (addr, 1));
10064 if (l1 != NULL_RTX && l2 == NULL_RTX)
10065 return l1;
10066 else if (l1 == NULL_RTX && l2 != NULL_RTX)
10067 return l2;
10069 return NULL_RTX;
10072 /* Exported to toplev.c.
10074 Do a final pass over the function, just before delayed branch
10075 scheduling. */
10077 void
10078 machine_dependent_reorg (first)
10079 rtx first;
10081 int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
10082 rtx insn;
10083 struct constant *constants;
10085 if (! TARGET_MIPS16)
10086 return;
10088 /* If $gp is used, try to remove stores, and replace loads with
10089 copies from $gp. */
10090 if (optimize)
10091 mips16_optimize_gp (first);
10093 /* Scan the function looking for PC relative loads which may be out
10094 of range. All such loads will either be from the constant table,
10095 or be getting the address of a constant string. If the size of
10096 the function plus the size of the constant table is less than
10097 0x8000, then all loads are in range. */
10099 insns_len = 0;
10100 for (insn = first; insn; insn = NEXT_INSN (insn))
10102 insns_len += get_attr_length (insn);
10104 /* ??? We put switch tables in .text, but we don't define
10105 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
10106 compute their lengths correctly. */
10107 if (GET_CODE (insn) == JUMP_INSN)
10109 rtx body;
10111 body = PATTERN (insn);
10112 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
10113 insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
10114 * GET_MODE_SIZE (GET_MODE (body)));
10115 insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
10119 /* Store the original value of insns_len in cfun->machine, so
10120 that simple_memory_operand can look at it. */
10121 cfun->machine->insns_len = insns_len;
10123 pool_size = get_pool_size ();
10124 if (insns_len + pool_size + mips_string_length < 0x8000)
10125 return;
10127 /* Loop over the insns and figure out what the maximum internal pool
10128 size could be. */
10129 max_internal_pool_size = 0;
10130 for (insn = first; insn; insn = NEXT_INSN (insn))
10132 if (GET_CODE (insn) == INSN
10133 && GET_CODE (PATTERN (insn)) == SET)
10135 rtx src;
10137 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
10138 if (src == NULL_RTX)
10139 continue;
10140 if (CONSTANT_POOL_ADDRESS_P (src))
10141 max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
10142 else if (SYMBOL_REF_FLAG (src))
10143 max_internal_pool_size += GET_MODE_SIZE (Pmode);
10147 constants = NULL;
10148 addr = 0;
10149 first_constant_ref = -1;
10151 for (insn = first; insn; insn = NEXT_INSN (insn))
10153 if (GET_CODE (insn) == INSN
10154 && GET_CODE (PATTERN (insn)) == SET)
10156 rtx val, src;
10157 enum machine_mode mode = VOIDmode;
10159 val = NULL_RTX;
10160 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
10161 if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
10163 /* ??? This is very conservative, which means that we
10164 will generate too many copies of the constant table.
10165 The only solution would seem to be some form of
10166 relaxing. */
10167 if (((insns_len - addr)
10168 + max_internal_pool_size
10169 + get_pool_offset (src))
10170 >= 0x8000)
10172 val = get_pool_constant (src);
10173 mode = get_pool_mode (src);
10175 max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
10177 else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
10179 /* Including all of mips_string_length is conservative,
10180 and so is including all of max_internal_pool_size. */
10181 if (((insns_len - addr)
10182 + max_internal_pool_size
10183 + pool_size
10184 + mips_string_length)
10185 >= 0x8000)
10187 val = src;
10188 mode = Pmode;
10190 max_internal_pool_size -= Pmode;
10193 if (val != NULL_RTX)
10195 rtx lab, newsrc;
10197 /* This PC relative load is out of range. ??? In the
10198 case of a string constant, we are only guessing that
10199 it is range, since we don't know the offset of a
10200 particular string constant. */
10202 lab = add_constant (&constants, val, mode);
10203 newsrc = gen_rtx (MEM, mode,
10204 gen_rtx (LABEL_REF, VOIDmode, lab));
10205 RTX_UNCHANGING_P (newsrc) = 1;
10206 PATTERN (insn) = gen_rtx (SET, VOIDmode,
10207 SET_DEST (PATTERN (insn)),
10208 newsrc);
10209 INSN_CODE (insn) = -1;
10211 if (first_constant_ref < 0)
10212 first_constant_ref = addr;
10216 addr += get_attr_length (insn);
10218 /* ??? We put switch tables in .text, but we don't define
10219 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
10220 compute their lengths correctly. */
10221 if (GET_CODE (insn) == JUMP_INSN)
10223 rtx body;
10225 body = PATTERN (insn);
10226 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
10227 addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
10228 * GET_MODE_SIZE (GET_MODE (body)));
10229 addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
10232 if (GET_CODE (insn) == BARRIER)
10234 /* Output any constants we have accumulated. Note that we
10235 don't need to change ADDR, since its only use is
10236 subtraction from INSNS_LEN, and both would be changed by
10237 the same amount.
10238 ??? If the instructions up to the next barrier reuse a
10239 constant, it would often be better to continue
10240 accumulating. */
10241 if (constants != NULL)
10242 dump_constants (constants, insn);
10243 constants = NULL;
10244 first_constant_ref = -1;
10247 if (constants != NULL
10248 && (NEXT_INSN (insn) == NULL
10249 || (first_constant_ref >= 0
10250 && (((addr - first_constant_ref)
10251 + 2 /* for alignment */
10252 + 2 /* for a short jump insn */
10253 + pool_size)
10254 >= 0x8000))))
10256 /* If we haven't had a barrier within 0x8000 bytes of a
10257 constant reference or we are at the end of the function,
10258 emit a barrier now. */
10260 rtx label, jump, barrier;
10262 label = gen_label_rtx ();
10263 jump = emit_jump_insn_after (gen_jump (label), insn);
10264 JUMP_LABEL (jump) = label;
10265 LABEL_NUSES (label) = 1;
10266 barrier = emit_barrier_after (jump);
10267 emit_label_after (label, barrier);
10268 first_constant_ref = -1;
10272 /* ??? If we output all references to a constant in internal
10273 constants table, we don't need to output the constant in the real
10274 constant table, but we have no way to prevent that. */
10277 /* Return nonzero if X is a SIGN or ZERO extend operator. */
10279 extend_operator (x, mode)
10280 rtx x;
10281 enum machine_mode mode ATTRIBUTE_UNUSED;
10283 enum rtx_code code = GET_CODE (x);
10284 return code == SIGN_EXTEND || code == ZERO_EXTEND;
10287 /* Accept any operator that can be used to shift the high half of the
10288 input value to the lower half, suitable for truncation. The
10289 remainder (the lower half of the input, and the upper half of the
10290 output) will be discarded. */
10292 highpart_shift_operator (x, mode)
10293 rtx x;
10294 enum machine_mode mode ATTRIBUTE_UNUSED;
10296 enum rtx_code code = GET_CODE (x);
10297 return (code == LSHIFTRT
10298 || code == ASHIFTRT
10299 || code == ROTATERT
10300 || code == ROTATE);
10303 /* Return a number assessing the cost of moving a register in class
10304 FROM to class TO. The classes are expressed using the enumeration
10305 values such as `GENERAL_REGS'. A value of 2 is the default; other
10306 values are interpreted relative to that.
10308 It is not required that the cost always equal 2 when FROM is the
10309 same as TO; on some machines it is expensive to move between
10310 registers if they are not general registers.
10312 If reload sees an insn consisting of a single `set' between two
10313 hard registers, and if `REGISTER_MOVE_COST' applied to their
10314 classes returns a value of 2, reload does not check to ensure that
10315 the constraints of the insn are met. Setting a cost of other than
10316 2 will allow reload to verify that the constraints are met. You
10317 should do this if the `movM' pattern's constraints do not allow
10318 such copying.
10320 ??? We make make the cost of moving from HI/LO/HILO/MD into general
10321 registers the same as for one of moving general registers to
10322 HI/LO/HILO/MD for TARGET_MIPS16 in order to prevent allocating a
10323 pseudo to HI/LO/HILO/MD. This might hurt optimizations though, it
10324 isn't clear if it is wise. And it might not work in all cases. We
10325 could solve the DImode LO reg problem by using a multiply, just
10326 like reload_{in,out}si. We could solve the SImode/HImode HI reg
10327 problem by using divide instructions. divu puts the remainder in
10328 the HI reg, so doing a divide by -1 will move the value in the HI
10329 reg for all values except -1. We could handle that case by using a
10330 signed divide, e.g. -1 / 2 (or maybe 1 / -2?). We'd have to emit
10331 a compare/branch to test the input value to see which instruction
10332 we need to use. This gets pretty messy, but it is feasible. */
10335 mips_register_move_cost (mode, to, from)
10336 enum machine_mode mode ATTRIBUTE_UNUSED;
10337 enum reg_class to, from;
10339 if (from == M16_REGS && GR_REG_CLASS_P (to))
10340 return 2;
10341 else if (from == M16_NA_REGS && GR_REG_CLASS_P (to))
10342 return 2;
10343 else if (GR_REG_CLASS_P (from))
10345 if (to == M16_REGS)
10346 return 2;
10347 else if (to == M16_NA_REGS)
10348 return 2;
10349 else if (GR_REG_CLASS_P (to))
10351 if (TARGET_MIPS16)
10352 return 4;
10353 else
10354 return 2;
10356 else if (to == FP_REGS)
10357 return 4;
10358 else if (to == HI_REG || to == LO_REG || to == MD_REGS
10359 || to == HILO_REG)
10361 if (TARGET_MIPS16)
10362 return 12;
10363 else
10364 return 6;
10366 else if (COP_REG_CLASS_P (to))
10368 return 5;
10370 } /* GR_REG_CLASS_P (from) */
10371 else if (from == FP_REGS)
10373 if (GR_REG_CLASS_P (to))
10374 return 4;
10375 else if (to == FP_REGS)
10376 return 2;
10377 else if (to == ST_REGS)
10378 return 8;
10379 } /* from == FP_REGS */
10380 else if (from == HI_REG || from == LO_REG || from == MD_REGS
10381 || from == HILO_REG)
10383 if (GR_REG_CLASS_P (to))
10385 if (TARGET_MIPS16)
10386 return 12;
10387 else
10388 return 6;
10390 } /* from == HI_REG, etc. */
10391 else if (from == ST_REGS && GR_REG_CLASS_P (to))
10392 return 4;
10393 else if (COP_REG_CLASS_P (from))
10395 return 5;
10396 } /* COP_REG_CLASS_P (from) */
10398 /* fallthru */
10400 return 12;
10403 /* Return the length of INSN. LENGTH is the initial length computed by
10404 attributes in the machine-description file. */
10407 mips_adjust_insn_length (insn, length)
10408 rtx insn;
10409 int length;
10411 /* A unconditional jump has an unfilled delay slot if it is not part
10412 of a sequence. A conditional jump normally has a delay slot, but
10413 does not on MIPS16. */
10414 if (simplejump_p (insn)
10415 || (!TARGET_MIPS16 && (GET_CODE (insn) == JUMP_INSN
10416 || GET_CODE (insn) == CALL_INSN)))
10417 length += 4;
10419 /* All MIPS16 instructions are a measly two bytes. */
10420 if (TARGET_MIPS16)
10421 length /= 2;
10423 return length;
10426 /* Output assembly instructions to peform a conditional branch.
10428 INSN is the branch instruction. OPERANDS[0] is the condition.
10429 OPERANDS[1] is the target of the branch. OPERANDS[2] is the target
10430 of the first operand to the condition. If TWO_OPERANDS_P is
10431 nonzero the comparison takes two operands; OPERANDS[3] will be the
10432 second operand.
10434 If INVERTED_P is nonzero we are to branch if the condition does
10435 not hold. If FLOAT_P is nonzero this is a floating-point comparison.
10437 LENGTH is the length (in bytes) of the sequence we are to generate.
10438 That tells us whether to generate a simple conditional branch, or a
10439 reversed conditional branch around a `jr' instruction. */
10440 const char *
10441 mips_output_conditional_branch (insn,
10442 operands,
10443 two_operands_p,
10444 float_p,
10445 inverted_p,
10446 length)
10447 rtx insn;
10448 rtx *operands;
10449 int two_operands_p;
10450 int float_p;
10451 int inverted_p;
10452 int length;
10454 static char buffer[200];
10455 /* The kind of comparison we are doing. */
10456 enum rtx_code code = GET_CODE (operands[0]);
10457 /* Nonzero if the opcode for the comparison needs a `z' indicating
10458 that it is a comparision against zero. */
10459 int need_z_p;
10460 /* A string to use in the assembly output to represent the first
10461 operand. */
10462 const char *op1 = "%z2";
10463 /* A string to use in the assembly output to represent the second
10464 operand. Use the hard-wired zero register if there's no second
10465 operand. */
10466 const char *op2 = (two_operands_p ? ",%z3" : ",%.");
10467 /* The operand-printing string for the comparison. */
10468 const char *const comp = (float_p ? "%F0" : "%C0");
10469 /* The operand-printing string for the inverted comparison. */
10470 const char *const inverted_comp = (float_p ? "%W0" : "%N0");
10472 /* The MIPS processors (for levels of the ISA at least two), have
10473 "likely" variants of each branch instruction. These instructions
10474 annul the instruction in the delay slot if the branch is not
10475 taken. */
10476 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
10478 if (!two_operands_p)
10480 /* To compute whether than A > B, for example, we normally
10481 subtract B from A and then look at the sign bit. But, if we
10482 are doing an unsigned comparison, and B is zero, we don't
10483 have to do the subtraction. Instead, we can just check to
10484 see if A is nonzero. Thus, we change the CODE here to
10485 reflect the simpler comparison operation. */
10486 switch (code)
10488 case GTU:
10489 code = NE;
10490 break;
10492 case LEU:
10493 code = EQ;
10494 break;
10496 case GEU:
10497 /* A condition which will always be true. */
10498 code = EQ;
10499 op1 = "%.";
10500 break;
10502 case LTU:
10503 /* A condition which will always be false. */
10504 code = NE;
10505 op1 = "%.";
10506 break;
10508 default:
10509 /* Not a special case. */
10510 break;
10514 /* Relative comparisons are always done against zero. But
10515 equality comparisons are done between two operands, and therefore
10516 do not require a `z' in the assembly language output. */
10517 need_z_p = (!float_p && code != EQ && code != NE);
10518 /* For comparisons against zero, the zero is not provided
10519 explicitly. */
10520 if (need_z_p)
10521 op2 = "";
10523 /* Begin by terminating the buffer. That way we can always use
10524 strcat to add to it. */
10525 buffer[0] = '\0';
10527 switch (length)
10529 case 4:
10530 case 8:
10531 /* Just a simple conditional branch. */
10532 if (float_p)
10533 sprintf (buffer, "%%*b%s%%?\t%%Z2%%1",
10534 inverted_p ? inverted_comp : comp);
10535 else
10536 sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1",
10537 inverted_p ? inverted_comp : comp,
10538 need_z_p ? "z" : "",
10539 op1,
10540 op2);
10541 return buffer;
10543 case 12:
10544 case 16:
10545 case 24:
10546 case 28:
10548 /* Generate a reversed conditional branch around ` j'
10549 instruction:
10551 .set noreorder
10552 .set nomacro
10553 bc l
10554 delay_slot or #nop
10555 j target
10556 #nop
10558 .set macro
10559 .set reorder
10561 If the original branch was a likely branch, the delay slot
10562 must be executed only if the branch is taken, so generate:
10564 .set noreorder
10565 .set nomacro
10566 bc l
10567 #nop
10568 j target
10569 delay slot or #nop
10571 .set macro
10572 .set reorder
10574 When generating non-embedded PIC, instead of:
10576 j target
10578 we emit:
10580 .set noat
10581 la $at, target
10582 jr $at
10583 .set at
10586 rtx orig_target;
10587 rtx target = gen_label_rtx ();
10589 orig_target = operands[1];
10590 operands[1] = target;
10591 /* Generate the reversed comparison. This takes four
10592 bytes. */
10593 if (float_p)
10594 sprintf (buffer, "%%*b%s\t%%Z2%%1",
10595 inverted_p ? comp : inverted_comp);
10596 else
10597 sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
10598 inverted_p ? comp : inverted_comp,
10599 need_z_p ? "z" : "",
10600 op1,
10601 op2);
10602 output_asm_insn (buffer, operands);
10604 if (length != 16 && length != 28 && ! mips_branch_likely)
10606 /* Output delay slot instruction. */
10607 rtx insn = final_sequence;
10608 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
10609 optimize, 0, 1);
10610 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
10612 else
10613 output_asm_insn ("%#", 0);
10615 if (length <= 16)
10616 output_asm_insn ("j\t%0", &orig_target);
10617 else
10619 if (Pmode == DImode)
10620 output_asm_insn ("%[dla\t%@,%0\n\tjr\t%@%]", &orig_target);
10621 else
10622 output_asm_insn ("%[la\t%@,%0\n\tjr\t%@%]", &orig_target);
10625 if (length != 16 && length != 28 && mips_branch_likely)
10627 /* Output delay slot instruction. */
10628 rtx insn = final_sequence;
10629 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
10630 optimize, 0, 1);
10631 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
10633 else
10634 output_asm_insn ("%#", 0);
10636 (*targetm.asm_out.internal_label) (asm_out_file, "L",
10637 CODE_LABEL_NUMBER (target));
10639 return "";
10642 /* We do not currently use this code. It handles jumps to
10643 arbitrary locations, using `jr', even across a 256MB boundary.
10644 We could add a -mhuge switch, and then use this code instead of
10645 the `j' alternative above when -mhuge was used. */
10646 #if 0
10647 case 16:
10648 case 20:
10650 /* Generate a reversed conditional branch around a `jr'
10651 instruction:
10653 .set noreorder
10654 .set nomacro
10655 .set noat
10656 bc l
10657 la $at, target
10658 jr $at
10659 .set at
10660 .set macro
10661 .set reorder
10664 Not pretty, but allows a conditional branch anywhere in the
10665 32-bit address space. If the original branch is annulled,
10666 then the instruction in the delay slot should be executed
10667 only if the branch is taken. The la instruction is really
10668 a macro which will usually take eight bytes, but sometimes
10669 takes only four, if the instruction to which we're jumping
10670 gets its own entry in the global pointer table, which will
10671 happen if its a case label. The assembler will then
10672 generate only a four-byte sequence, rather than eight, and
10673 there seems to be no way to tell it not to. Thus, we can't
10674 just use a `.+x' addressing form; we don't know what value
10675 to give for `x'.
10677 So, we resort to using the explicit relocation syntax
10678 available in the assembler and do:
10680 lw $at,%got_page(target)($gp)
10681 daddiu $at,$at,%got_ofst(target)
10683 That way, this always takes up eight bytes, and we can use
10684 the `.+x' form. Of course, these explicit machinations
10685 with relocation will not work with old assemblers. Then
10686 again, neither do out-of-range branches, so we haven't lost
10687 anything. */
10689 /* The target of the reversed branch. */
10690 const char *const target
10691 = ((mips_branch_likely || length == 20) ? ".+20" : ".+16");
10692 const char *at_register = mips_reg_names[ASSEMBLER_SCRATCH_REGNUM];
10693 const char *gp_register = mips_reg_names[PIC_OFFSET_TABLE_REGNUM];
10694 char *c;
10696 strcpy (buffer, "%(%<%[");
10697 c = strchr (buffer, '\0');
10698 /* Generate the reversed comparision. This takes four
10699 bytes. */
10700 if (float_p)
10701 sprintf (c, "%%*b%s\t%%Z2%s",
10702 inverted_p ? comp : inverted_comp,
10703 target);
10704 else
10705 sprintf (c, "%%*b%s%s\t%s%s,%s",
10706 inverted_p ? comp : inverted_comp,
10707 need_z_p ? "z" : "",
10708 op1,
10709 op2,
10710 target);
10711 c = strchr (buffer, '\0');
10712 /* Generate the load-address, and jump. This takes twelve
10713 bytes, for a total of 16. */
10714 sprintf (c,
10715 "\n\tlw\t%s,%%%%got_page(%%1)(%s)\n\tdaddiu\t%s,%s,%%%%got_ofst(%%1)\n\tjr\t%s",
10716 at_register,
10717 gp_register,
10718 at_register,
10719 at_register,
10720 at_register);
10721 if (length == 20)
10722 /* The delay slot was unfilled. Since we're inside
10723 .noreorder, the assembler will not fill in the NOP for
10724 us, so we must do it ourselves. */
10725 strcat (buffer, "\n\tnop");
10726 strcat (buffer, "%]%>%)");
10727 return buffer;
10729 #endif
10731 default:
10732 abort ();
10735 /* NOTREACHED */
10736 return 0;
10739 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
10740 with a final "000" replaced by "k". Ignore case.
10742 Note: this function is shared between GCC and GAS. */
10744 static bool
10745 mips_strict_matching_cpu_name_p (canonical, given)
10746 const char *canonical, *given;
10748 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
10749 given++, canonical++;
10751 return ((*given == 0 && *canonical == 0)
10752 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
10756 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
10757 CPU name. We've traditionally allowed a lot of variation here.
10759 Note: this function is shared between GCC and GAS. */
10761 static bool
10762 mips_matching_cpu_name_p (canonical, given)
10763 const char *canonical, *given;
10765 /* First see if the name matches exactly, or with a final "000"
10766 turned into "k". */
10767 if (mips_strict_matching_cpu_name_p (canonical, given))
10768 return true;
10770 /* If not, try comparing based on numerical designation alone.
10771 See if GIVEN is an unadorned number, or 'r' followed by a number. */
10772 if (TOLOWER (*given) == 'r')
10773 given++;
10774 if (!ISDIGIT (*given))
10775 return false;
10777 /* Skip over some well-known prefixes in the canonical name,
10778 hoping to find a number there too. */
10779 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
10780 canonical += 2;
10781 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
10782 canonical += 2;
10783 else if (TOLOWER (canonical[0]) == 'r')
10784 canonical += 1;
10786 return mips_strict_matching_cpu_name_p (canonical, given);
10790 /* Parse an option that takes the name of a processor as its argument.
10791 OPTION is the name of the option and CPU_STRING is the argument.
10792 Return the corresponding processor enumeration if the CPU_STRING is
10793 recognized, otherwise report an error and return null.
10795 A similar function exists in GAS. */
10797 static const struct mips_cpu_info *
10798 mips_parse_cpu (option, cpu_string)
10799 const char *option, *cpu_string;
10801 const struct mips_cpu_info *p;
10802 const char *s;
10804 /* In the past, we allowed upper-case CPU names, but it doesn't
10805 work well with the multilib machinery. */
10806 for (s = cpu_string; *s != 0; s++)
10807 if (ISUPPER (*s))
10809 warning ("the cpu name must be lower case");
10810 break;
10813 /* 'from-abi' selects the most compatible architecture for the given
10814 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
10815 EABIs, we have to decide whether we're using the 32-bit or 64-bit
10816 version. Look first at the -mgp options, if given, otherwise base
10817 the choice on MASK_64BIT in TARGET_DEFAULT. */
10818 if (strcasecmp (cpu_string, "from-abi") == 0)
10819 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
10820 : ABI_NEEDS_64BIT_REGS ? 3
10821 : (TARGET_64BIT ? 3 : 1));
10823 /* 'default' has traditionally been a no-op. Probably not very useful. */
10824 if (strcasecmp (cpu_string, "default") == 0)
10825 return 0;
10827 for (p = mips_cpu_info_table; p->name != 0; p++)
10828 if (mips_matching_cpu_name_p (p->name, cpu_string))
10829 return p;
10831 error ("bad value (%s) for %s", cpu_string, option);
10832 return 0;
10836 /* Return the processor associated with the given ISA level, or null
10837 if the ISA isn't valid. */
10839 static const struct mips_cpu_info *
10840 mips_cpu_info_from_isa (isa)
10841 int isa;
10843 const struct mips_cpu_info *p;
10845 for (p = mips_cpu_info_table; p->name != 0; p++)
10846 if (p->isa == isa)
10847 return p;
10849 return 0;
10852 /* Adjust the cost of INSN based on the relationship between INSN that
10853 is dependent on DEP_INSN through the dependence LINK. The default
10854 is to make no adjustment to COST.
10856 On the MIPS, ignore the cost of anti- and output-dependencies. */
10857 static int
10858 mips_adjust_cost (insn, link, dep, cost)
10859 rtx insn ATTRIBUTE_UNUSED;
10860 rtx link;
10861 rtx dep ATTRIBUTE_UNUSED;
10862 int cost;
10864 if (REG_NOTE_KIND (link) != 0)
10865 return 0; /* Anti or output dependence. */
10866 return cost;
10869 /* ??? This could be replaced with the default elf version if
10870 TARGET_IS_SMALL_DATA_P is set properly. */
10872 static void
10873 mips_unique_section (decl, reloc)
10874 tree decl;
10875 int reloc;
10877 int len, size, sec;
10878 const char *name, *prefix;
10879 char *string;
10880 static const char *const prefixes[4][2] = {
10881 { ".text.", ".gnu.linkonce.t." },
10882 { ".rodata.", ".gnu.linkonce.r." },
10883 { ".data.", ".gnu.linkonce.d." },
10884 { ".sdata.", ".gnu.linkonce.s." }
10887 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10888 name = (* targetm.strip_name_encoding) (name);
10889 size = int_size_in_bytes (TREE_TYPE (decl));
10891 /* Determine the base section we are interested in:
10892 0=text, 1=rodata, 2=data, 3=sdata, [4=bss]. */
10893 if (TREE_CODE (decl) == FUNCTION_DECL)
10894 sec = 0;
10895 else if (DECL_INITIAL (decl) == 0
10896 || DECL_INITIAL (decl) == error_mark_node)
10897 sec = 2;
10898 else if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
10899 && TREE_CODE (decl) == STRING_CST
10900 && !flag_writable_strings)
10902 /* For embedded position independent code, put constant
10903 strings in the text section, because the data section
10904 is limited to 64K in size. For mips16 code, put
10905 strings in the text section so that a PC relative load
10906 instruction can be used to get their address. */
10907 sec = 0;
10909 else if (TARGET_EMBEDDED_DATA)
10911 /* For embedded applications, always put an object in
10912 read-only data if possible, in order to reduce RAM
10913 usage. */
10915 if (decl_readonly_section (decl, reloc))
10916 sec = 1;
10917 else if (size > 0 && size <= mips_section_threshold)
10918 sec = 3;
10919 else
10920 sec = 2;
10922 else
10924 /* For hosted applications, always put an object in
10925 small data if possible, as this gives the best
10926 performance. */
10928 if (size > 0 && size <= mips_section_threshold)
10929 sec = 3;
10930 else if (decl_readonly_section (decl, reloc))
10931 sec = 1;
10932 else
10933 sec = 2;
10936 prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
10937 len = strlen (name) + strlen (prefix);
10938 string = alloca (len + 1);
10939 sprintf (string, "%s%s", prefix, name);
10941 DECL_SECTION_NAME (decl) = build_string (len, string);
10944 unsigned int
10945 mips_hard_regno_nregs (regno, mode)
10946 int regno;
10947 enum machine_mode mode;
10949 if (! FP_REG_P (regno))
10950 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
10951 else
10952 return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
10956 mips_return_in_memory (type)
10957 tree type;
10959 /* Under the old (i.e., 32 and O64 ABIs) all BLKmode objects are
10960 returned in memory. Under the new (N32 and 64-bit MIPS ABIs) small
10961 structures are returned in a register. Objects with varying size
10962 must still be returned in memory, of course. */
10964 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
10965 return (TYPE_MODE (type) == BLKmode);
10966 else
10967 return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
10968 || (int_size_in_bytes (type) == -1));
10971 static int
10972 mips_issue_rate ()
10974 switch (mips_tune)
10976 case PROCESSOR_R3000: return 1;
10977 case PROCESSOR_R5400: return 2;
10978 case PROCESSOR_R5500: return 2;
10980 default:
10981 return 1;
10984 abort ();
10988 /* Implements TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE. Return true for
10989 processors that have a DFA pipeline description. */
10991 static int
10992 mips_use_dfa_pipeline_interface ()
10994 switch (mips_tune)
10996 case PROCESSOR_R5400:
10997 case PROCESSOR_R5500:
10998 case PROCESSOR_SR71000:
10999 return true;
11001 default:
11002 return false;
11007 const char *
11008 mips_emit_prefetch (operands)
11009 rtx operands[];
11011 /* For the mips32/64 architectures the hint fields are arranged
11012 by operation (load/store) and locality (normal/streamed/retained).
11013 Irritatingly, numbers 2 and 3 are reserved leaving no simple
11014 algorithm for figuring the hint. */
11016 int write = INTVAL (operands[1]);
11017 int locality = INTVAL (operands[2]);
11019 static const char * const alt[2][4] = {
11021 "pref\t4,%a0",
11022 "pref\t0,%a0",
11023 "pref\t0,%a0",
11024 "pref\t6,%a0"
11027 "pref\t5,%a0",
11028 "pref\t1,%a0",
11029 "pref\t1,%a0",
11030 "pref\t7,%a0"
11034 return alt[write][locality];
11039 #ifdef TARGET_IRIX6
11040 /* Output assembly to switch to section NAME with attribute FLAGS. */
11042 static void
11043 iris6_asm_named_section_1 (name, flags, align)
11044 const char *name;
11045 unsigned int flags;
11046 unsigned int align;
11048 unsigned int sh_type, sh_flags, sh_entsize;
11050 sh_flags = 0;
11051 if (!(flags & SECTION_DEBUG))
11052 sh_flags |= 2; /* SHF_ALLOC */
11053 if (flags & SECTION_WRITE)
11054 sh_flags |= 1; /* SHF_WRITE */
11055 if (flags & SECTION_CODE)
11056 sh_flags |= 4; /* SHF_EXECINSTR */
11057 if (flags & SECTION_SMALL)
11058 sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
11059 if (strcmp (name, ".debug_frame") == 0)
11060 sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
11061 if (flags & SECTION_DEBUG)
11062 sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
11063 else if (flags & SECTION_BSS)
11064 sh_type = 8; /* SHT_NOBITS */
11065 else
11066 sh_type = 1; /* SHT_PROGBITS */
11068 if (flags & SECTION_CODE)
11069 sh_entsize = 4;
11070 else
11071 sh_entsize = 0;
11073 fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
11074 name, sh_type, sh_flags, sh_entsize, align);
11077 static void
11078 iris6_asm_named_section (name, flags)
11079 const char *name;
11080 unsigned int flags;
11082 if (TARGET_FILE_SWITCHING && (flags & SECTION_CODE))
11083 asm_out_file = asm_out_text_file;
11084 iris6_asm_named_section_1 (name, flags, 0);
11087 /* In addition to emitting a .align directive, record the maximum
11088 alignment requested for the current section. */
11090 struct iris_section_align_entry
11092 const char *name;
11093 unsigned int log;
11094 unsigned int flags;
11097 static htab_t iris_section_align_htab;
11098 static FILE *iris_orig_asm_out_file;
11100 static int
11101 iris_section_align_entry_eq (p1, p2)
11102 const PTR p1;
11103 const PTR p2;
11105 const struct iris_section_align_entry *old = p1;
11106 const char *new = p2;
11108 return strcmp (old->name, new) == 0;
11111 static hashval_t
11112 iris_section_align_entry_hash (p)
11113 const PTR p;
11115 const struct iris_section_align_entry *old = p;
11116 return htab_hash_string (old->name);
11119 void
11120 iris6_asm_output_align (file, log)
11121 FILE *file;
11122 unsigned int log;
11124 const char *section = current_section_name ();
11125 struct iris_section_align_entry **slot, *entry;
11127 if (! section)
11128 abort ();
11130 slot = (struct iris_section_align_entry **)
11131 htab_find_slot_with_hash (iris_section_align_htab, section,
11132 htab_hash_string (section), INSERT);
11133 entry = *slot;
11134 if (! entry)
11136 entry = (struct iris_section_align_entry *)
11137 xmalloc (sizeof (struct iris_section_align_entry));
11138 *slot = entry;
11139 entry->name = section;
11140 entry->log = log;
11141 entry->flags = current_section_flags ();
11143 else if (entry->log < log)
11144 entry->log = log;
11146 fprintf (file, "\t.align\t%u\n", log);
11149 /* The Iris assembler does not record alignment from .align directives,
11150 but takes it from the first .section directive seen. Play yet more
11151 file switching games so that we can emit a .section directive at the
11152 beginning of the file with the proper alignment attached. */
11154 void
11155 iris6_asm_file_start (stream)
11156 FILE *stream;
11158 mips_asm_file_start (stream);
11160 iris_orig_asm_out_file = asm_out_file;
11161 stream = tmpfile ();
11162 asm_out_file = stream;
11163 asm_out_data_file = stream;
11164 if (! TARGET_FILE_SWITCHING)
11165 asm_out_text_file = stream;
11167 iris_section_align_htab = htab_create (31, iris_section_align_entry_hash,
11168 iris_section_align_entry_eq, NULL);
11171 static int
11172 iris6_section_align_1 (slot, data)
11173 void **slot;
11174 void *data ATTRIBUTE_UNUSED;
11176 const struct iris_section_align_entry *entry
11177 = *(const struct iris_section_align_entry **) slot;
11179 iris6_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
11180 return 1;
11183 void
11184 iris6_asm_file_end (stream)
11185 FILE *stream;
11187 /* Emit section directives with the proper alignment at the top of the
11188 real output file. */
11189 asm_out_file = iris_orig_asm_out_file;
11190 htab_traverse (iris_section_align_htab, iris6_section_align_1, NULL);
11192 /* Copy the data emitted to the temp file to the real output file. */
11193 copy_file_data (asm_out_file, stream);
11195 mips_asm_file_end (stream);
11197 #endif /* TARGET_IRIX6 */
11199 #include "gt-mips.h"