2004-06-23 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / config / mips / mips.c
blobf481c4ba338cc261eed3aa82061ca8277ea196c8
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by A. Lichnewsky, lich@inria.inria.fr.
5 Changes by Michael Meissner, meissner@osf.org.
6 64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
7 Brendan Eich, brendan@microunity.com.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to
23 the Free Software Foundation, 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.h>
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "insn-attr.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "tree.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "optabs.h"
45 #include "flags.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "ggc.h"
49 #include "gstab.h"
50 #include "hashtab.h"
51 #include "debug.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "integrate.h"
55 #include "langhooks.h"
56 #include "cfglayout.h"
57 #include "sched-int.h"
59 /* Enumeration for all of the relational tests, so that we can build
60 arrays indexed by the test type, and not worry about the order
61 of EQ, NE, etc. */
63 enum internal_test {
64 ITEST_EQ,
65 ITEST_NE,
66 ITEST_GT,
67 ITEST_GE,
68 ITEST_LT,
69 ITEST_LE,
70 ITEST_GTU,
71 ITEST_GEU,
72 ITEST_LTU,
73 ITEST_LEU,
74 ITEST_MAX
77 /* True if X is an unspec wrapper around a SYMBOL_REF or LABEL_REF. */
78 #define UNSPEC_ADDRESS_P(X) \
79 (GET_CODE (X) == UNSPEC \
80 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
81 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
83 /* Extract the symbol or label from UNSPEC wrapper X. */
84 #define UNSPEC_ADDRESS(X) \
85 XVECEXP (X, 0, 0)
87 /* Extract the symbol type from UNSPEC wrapper X. */
88 #define UNSPEC_ADDRESS_TYPE(X) \
89 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
91 /* True if X is (const (unspec [(const_int 0)] UNSPEC_GP)). This is used
92 to initialize the mips16 gp pseudo register. */
93 #define CONST_GP_P(X) \
94 (GET_CODE (X) == CONST \
95 && GET_CODE (XEXP (X, 0)) == UNSPEC \
96 && XINT (XEXP (X, 0), 1) == UNSPEC_GP)
98 /* The maximum distance between the top of the stack frame and the
99 value $sp has when we save & restore registers.
101 Use a maximum gap of 0x100 in the mips16 case. We can then use
102 unextended instructions to save and restore registers, and to
103 allocate and deallocate the top part of the frame.
105 The value in the !mips16 case must be a SMALL_OPERAND and must
106 preserve the maximum stack alignment. It could really be 0x7ff0,
107 but SGI's assemblers implement daddiu $sp,$sp,-0x7ff0 as a
108 multi-instruction addu sequence. Use 0x7fe0 to work around this. */
109 #define MIPS_MAX_FIRST_STACK_STEP (TARGET_MIPS16 ? 0x100 : 0x7fe0)
111 /* True if INSN is a mips.md pattern or asm statement. */
112 #define USEFUL_INSN_P(INSN) \
113 (INSN_P (INSN) \
114 && GET_CODE (PATTERN (INSN)) != USE \
115 && GET_CODE (PATTERN (INSN)) != CLOBBER \
116 && GET_CODE (PATTERN (INSN)) != ADDR_VEC \
117 && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
119 /* If INSN is a delayed branch sequence, return the first instruction
120 in the sequence, otherwise return INSN itself. */
121 #define SEQ_BEGIN(INSN) \
122 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
123 ? XVECEXP (PATTERN (INSN), 0, 0) \
124 : (INSN))
126 /* Likewise for the last instruction in a delayed branch sequence. */
127 #define SEQ_END(INSN) \
128 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
129 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
130 : (INSN))
132 /* Execute the following loop body with SUBINSN set to each instruction
133 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
134 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
135 for ((SUBINSN) = SEQ_BEGIN (INSN); \
136 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
137 (SUBINSN) = NEXT_INSN (SUBINSN))
139 /* Classifies an address.
141 ADDRESS_REG
142 A natural register + offset address. The register satisfies
143 mips_valid_base_register_p and the offset is a const_arith_operand.
145 ADDRESS_LO_SUM
146 A LO_SUM rtx. The first operand is a valid base register and
147 the second operand is a symbolic address.
149 ADDRESS_CONST_INT
150 A signed 16-bit constant address.
152 ADDRESS_SYMBOLIC:
153 A constant symbolic address (equivalent to CONSTANT_SYMBOLIC). */
154 enum mips_address_type {
155 ADDRESS_REG,
156 ADDRESS_LO_SUM,
157 ADDRESS_CONST_INT,
158 ADDRESS_SYMBOLIC
161 /* A function to save or store a register. The first argument is the
162 register and the second is the stack slot. */
163 typedef void (*mips_save_restore_fn) (rtx, rtx);
165 struct mips16_constant;
166 struct mips_arg_info;
167 struct mips_address_info;
168 struct mips_integer_op;
169 struct mips_sim;
171 static enum mips_symbol_type mips_classify_symbol (rtx);
172 static void mips_split_const (rtx, rtx *, HOST_WIDE_INT *);
173 static bool mips_offset_within_object_p (rtx, HOST_WIDE_INT);
174 static bool mips_symbolic_constant_p (rtx, enum mips_symbol_type *);
175 static bool mips_valid_base_register_p (rtx, enum machine_mode, int);
176 static bool mips_symbolic_address_p (enum mips_symbol_type, enum machine_mode);
177 static bool mips_classify_address (struct mips_address_info *, rtx,
178 enum machine_mode, int);
179 static int mips_symbol_insns (enum mips_symbol_type);
180 static bool mips16_unextended_reference_p (enum machine_mode mode, rtx, rtx);
181 static rtx mips_force_temporary (rtx, rtx);
182 static rtx mips_split_symbol (rtx, rtx);
183 static rtx mips_unspec_offset_high (rtx, rtx, rtx, enum mips_symbol_type);
184 static rtx mips_add_offset (rtx, HOST_WIDE_INT);
185 static unsigned int mips_build_shift (struct mips_integer_op *, HOST_WIDE_INT);
186 static unsigned int mips_build_lower (struct mips_integer_op *,
187 unsigned HOST_WIDE_INT);
188 static unsigned int mips_build_integer (struct mips_integer_op *,
189 unsigned HOST_WIDE_INT);
190 static void mips_move_integer (rtx, unsigned HOST_WIDE_INT);
191 static void mips_legitimize_const_move (enum machine_mode, rtx, rtx);
192 static int m16_check_op (rtx, int, int, int);
193 static bool mips_rtx_costs (rtx, int, int, int *);
194 static int mips_address_cost (rtx);
195 static enum internal_test map_test_to_internal_test (enum rtx_code);
196 static void get_float_compare_codes (enum rtx_code, enum rtx_code *,
197 enum rtx_code *);
198 static void mips_load_call_address (rtx, rtx, int);
199 static bool mips_function_ok_for_sibcall (tree, tree);
200 static void mips_block_move_straight (rtx, rtx, HOST_WIDE_INT);
201 static void mips_adjust_block_mem (rtx, HOST_WIDE_INT, rtx *, rtx *);
202 static void mips_block_move_loop (rtx, rtx, HOST_WIDE_INT);
203 static void mips_arg_info (const CUMULATIVE_ARGS *, enum machine_mode,
204 tree, int, struct mips_arg_info *);
205 static bool mips_get_unaligned_mem (rtx *, unsigned int, int, rtx *, rtx *);
206 static void mips_set_architecture (const struct mips_cpu_info *);
207 static void mips_set_tune (const struct mips_cpu_info *);
208 static struct machine_function *mips_init_machine_status (void);
209 static void print_operand_reloc (FILE *, rtx, const char **);
210 static bool mips_assemble_integer (rtx, unsigned int, int);
211 static void mips_file_start (void);
212 static void mips_file_end (void);
213 static bool mips_rewrite_small_data_p (rtx);
214 static int small_data_pattern_1 (rtx *, void *);
215 static int mips_rewrite_small_data_1 (rtx *, void *);
216 static bool mips_function_has_gp_insn (void);
217 static unsigned int mips_global_pointer (void);
218 static bool mips_save_reg_p (unsigned int);
219 static void mips_save_restore_reg (enum machine_mode, int, HOST_WIDE_INT,
220 mips_save_restore_fn);
221 static void mips_for_each_saved_reg (HOST_WIDE_INT, mips_save_restore_fn);
222 static void mips_output_cplocal (void);
223 static void mips_emit_loadgp (void);
224 static void mips_output_function_prologue (FILE *, HOST_WIDE_INT);
225 static void mips_set_frame_expr (rtx);
226 static rtx mips_frame_set (rtx, rtx);
227 static void mips_save_reg (rtx, rtx);
228 static void mips_output_function_epilogue (FILE *, HOST_WIDE_INT);
229 static void mips_restore_reg (rtx, rtx);
230 static void mips_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
231 HOST_WIDE_INT, tree);
232 static int symbolic_expression_p (rtx);
233 static void mips_select_rtx_section (enum machine_mode, rtx,
234 unsigned HOST_WIDE_INT);
235 static void mips_select_section (tree, int, unsigned HOST_WIDE_INT)
236 ATTRIBUTE_UNUSED;
237 static bool mips_in_small_data_p (tree);
238 static int mips_fpr_return_fields (tree, tree *);
239 static bool mips_return_in_msb (tree);
240 static rtx mips_return_fpr_pair (enum machine_mode mode,
241 enum machine_mode mode1, HOST_WIDE_INT,
242 enum machine_mode mode2, HOST_WIDE_INT);
243 static rtx mips16_gp_pseudo_reg (void);
244 static void mips16_fp_args (FILE *, int, int);
245 static void build_mips16_function_stub (FILE *);
246 static rtx dump_constants_1 (enum machine_mode, rtx, rtx);
247 static void dump_constants (struct mips16_constant *, rtx);
248 static int mips16_insn_length (rtx);
249 static int mips16_rewrite_pool_refs (rtx *, void *);
250 static void mips16_lay_out_constants (void);
251 static void mips_sim_reset (struct mips_sim *);
252 static void mips_sim_init (struct mips_sim *, state_t);
253 static void mips_sim_next_cycle (struct mips_sim *);
254 static void mips_sim_wait_reg (struct mips_sim *, rtx, rtx);
255 static int mips_sim_wait_regs_2 (rtx *, void *);
256 static void mips_sim_wait_regs_1 (rtx *, void *);
257 static void mips_sim_wait_regs (struct mips_sim *, rtx);
258 static void mips_sim_wait_units (struct mips_sim *, rtx);
259 static void mips_sim_wait_insn (struct mips_sim *, rtx);
260 static void mips_sim_record_set (rtx, rtx, void *);
261 static void mips_sim_issue_insn (struct mips_sim *, rtx);
262 static void mips_sim_issue_nop (struct mips_sim *);
263 static void mips_sim_finish_insn (struct mips_sim *, rtx);
264 static void vr4130_avoid_branch_rt_conflict (rtx);
265 static void vr4130_align_insns (void);
266 static void mips_avoid_hazard (rtx, rtx, int *, rtx *, rtx);
267 static void mips_avoid_hazards (void);
268 static void mips_reorg (void);
269 static bool mips_strict_matching_cpu_name_p (const char *, const char *);
270 static bool mips_matching_cpu_name_p (const char *, const char *);
271 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
272 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
273 static bool mips_return_in_memory (tree, tree);
274 static bool mips_strict_argument_naming (CUMULATIVE_ARGS *);
275 static void mips_macc_chains_record (rtx);
276 static void mips_macc_chains_reorder (rtx *, int);
277 static void vr4130_true_reg_dependence_p_1 (rtx, rtx, void *);
278 static bool vr4130_true_reg_dependence_p (rtx);
279 static bool vr4130_swap_insns_p (rtx, rtx);
280 static void vr4130_reorder (rtx *, int);
281 static void mips_promote_ready (rtx *, int, int);
282 static int mips_sched_reorder (FILE *, int, rtx *, int *, int);
283 static int mips_variable_issue (FILE *, int, rtx, int);
284 static int mips_adjust_cost (rtx, rtx, rtx, int);
285 static int mips_issue_rate (void);
286 static int mips_use_dfa_pipeline_interface (void);
287 static int mips_multipass_dfa_lookahead (void);
288 static void mips_init_libfuncs (void);
289 static void mips_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
290 tree, int *, int);
291 static tree mips_build_builtin_va_list (void);
293 #if TARGET_IRIX
294 static void irix_asm_named_section_1 (const char *, unsigned int,
295 unsigned int);
296 static void irix_asm_named_section (const char *, unsigned int);
297 static int irix_section_align_entry_eq (const void *, const void *);
298 static hashval_t irix_section_align_entry_hash (const void *);
299 static void irix_file_start (void);
300 static int irix_section_align_1 (void **, void *);
301 static void copy_file_data (FILE *, FILE *);
302 static void irix_file_end (void);
303 static unsigned int irix_section_type_flags (tree, const char *, int);
304 #endif
306 /* Structure to be filled in by compute_frame_size with register
307 save masks, and offsets for the current function. */
309 struct mips_frame_info GTY(())
311 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
312 HOST_WIDE_INT var_size; /* # bytes that variables take up */
313 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
314 HOST_WIDE_INT cprestore_size; /* # bytes that the .cprestore slot takes up */
315 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
316 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
317 unsigned int mask; /* mask of saved gp registers */
318 unsigned int fmask; /* mask of saved fp registers */
319 HOST_WIDE_INT gp_save_offset; /* offset from vfp to store gp registers */
320 HOST_WIDE_INT fp_save_offset; /* offset from vfp to store fp registers */
321 HOST_WIDE_INT gp_sp_offset; /* offset from new sp to store gp registers */
322 HOST_WIDE_INT fp_sp_offset; /* offset from new sp to store fp registers */
323 bool initialized; /* true if frame size already calculated */
324 int num_gp; /* number of gp registers saved */
325 int num_fp; /* number of fp registers saved */
328 struct machine_function GTY(()) {
329 /* Pseudo-reg holding the value of $28 in a mips16 function which
330 refers to GP relative global variables. */
331 rtx mips16_gp_pseudo_rtx;
333 /* Current frame information, calculated by compute_frame_size. */
334 struct mips_frame_info frame;
336 /* The register to use as the global pointer within this function. */
337 unsigned int global_pointer;
339 /* True if mips_adjust_insn_length should ignore an instruction's
340 hazard attribute. */
341 bool ignore_hazard_length_p;
343 /* True if the whole function is suitable for .set noreorder and
344 .set nomacro. */
345 bool all_noreorder_p;
347 /* True if the function is known to have an instruction that needs $gp. */
348 bool has_gp_insn_p;
351 /* Information about a single argument. */
352 struct mips_arg_info
354 /* True if the argument is passed in a floating-point register, or
355 would have been if we hadn't run out of registers. */
356 bool fpr_p;
358 /* The argument's size, in bytes. */
359 unsigned int num_bytes;
361 /* The number of words passed in registers, rounded up. */
362 unsigned int reg_words;
364 /* The offset of the first register from GP_ARG_FIRST or FP_ARG_FIRST,
365 or MAX_ARGS_IN_REGISTERS if the argument is passed entirely
366 on the stack. */
367 unsigned int reg_offset;
369 /* The number of words that must be passed on the stack, rounded up. */
370 unsigned int stack_words;
372 /* The offset from the start of the stack overflow area of the argument's
373 first stack word. Only meaningful when STACK_WORDS is nonzero. */
374 unsigned int stack_offset;
378 /* Information about an address described by mips_address_type.
380 ADDRESS_CONST_INT
381 No fields are used.
383 ADDRESS_REG
384 REG is the base register and OFFSET is the constant offset.
386 ADDRESS_LO_SUM
387 REG is the register that contains the high part of the address,
388 OFFSET is the symbolic address being referenced and SYMBOL_TYPE
389 is the type of OFFSET's symbol.
391 ADDRESS_SYMBOLIC
392 SYMBOL_TYPE is the type of symbol being referenced. */
394 struct mips_address_info
396 enum mips_address_type type;
397 rtx reg;
398 rtx offset;
399 enum mips_symbol_type symbol_type;
403 /* One stage in a constant building sequence. These sequences have
404 the form:
406 A = VALUE[0]
407 A = A CODE[1] VALUE[1]
408 A = A CODE[2] VALUE[2]
411 where A is an accumulator, each CODE[i] is a binary rtl operation
412 and each VALUE[i] is a constant integer. */
413 struct mips_integer_op {
414 enum rtx_code code;
415 unsigned HOST_WIDE_INT value;
419 /* The largest number of operations needed to load an integer constant.
420 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
421 When the lowest bit is clear, we can try, but reject a sequence with
422 an extra SLL at the end. */
423 #define MIPS_MAX_INTEGER_OPS 7
426 /* Global variables for machine-dependent things. */
428 /* Threshold for data being put into the small data/bss area, instead
429 of the normal data area. */
430 int mips_section_threshold = -1;
432 /* Count the number of .file directives, so that .loc is up to date. */
433 int num_source_filenames = 0;
435 /* Count the number of sdb related labels are generated (to find block
436 start and end boundaries). */
437 int sdb_label_count = 0;
439 /* Next label # for each statement for Silicon Graphics IRIS systems. */
440 int sym_lineno = 0;
442 /* Linked list of all externals that are to be emitted when optimizing
443 for the global pointer if they haven't been declared by the end of
444 the program with an appropriate .comm or initialization. */
446 struct extern_list GTY (())
448 struct extern_list *next; /* next external */
449 const char *name; /* name of the external */
450 int size; /* size in bytes */
453 static GTY (()) struct extern_list *extern_head = 0;
455 /* Name of the file containing the current function. */
456 const char *current_function_file = "";
458 /* Number of nested .set noreorder, noat, nomacro, and volatile requests. */
459 int set_noreorder;
460 int set_noat;
461 int set_nomacro;
462 int set_volatile;
464 /* The next branch instruction is a branch likely, not branch normal. */
465 int mips_branch_likely;
467 /* Cached operands, and operator to compare for use in set/branch/trap
468 on condition codes. */
469 rtx branch_cmp[2];
471 /* what type of branch to use */
472 enum cmp_type branch_type;
474 /* The target cpu for code generation. */
475 enum processor_type mips_arch;
476 const struct mips_cpu_info *mips_arch_info;
478 /* The target cpu for optimization and scheduling. */
479 enum processor_type mips_tune;
480 const struct mips_cpu_info *mips_tune_info;
482 /* Which instruction set architecture to use. */
483 int mips_isa;
485 /* Which ABI to use. */
486 int mips_abi;
488 /* Strings to hold which cpu and instruction set architecture to use. */
489 const char *mips_arch_string; /* for -march=<xxx> */
490 const char *mips_tune_string; /* for -mtune=<xxx> */
491 const char *mips_isa_string; /* for -mips{1,2,3,4} */
492 const char *mips_abi_string; /* for -mabi={32,n32,64,eabi} */
494 /* Whether we are generating mips16 hard float code. In mips16 mode
495 we always set TARGET_SOFT_FLOAT; this variable is nonzero if
496 -msoft-float was not specified by the user, which means that we
497 should arrange to call mips32 hard floating point code. */
498 int mips16_hard_float;
500 const char *mips_cache_flush_func = CACHE_FLUSH_FUNC;
502 /* If TRUE, we split addresses into their high and low parts in the RTL. */
503 int mips_split_addresses;
505 /* Mode used for saving/restoring general purpose registers. */
506 static enum machine_mode gpr_mode;
508 /* Array giving truth value on whether or not a given hard register
509 can support a given mode. */
510 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
512 /* List of all MIPS punctuation characters used by print_operand. */
513 char mips_print_operand_punct[256];
515 /* Map GCC register number to debugger register number. */
516 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
518 /* A copy of the original flag_delayed_branch: see override_options. */
519 static int mips_flag_delayed_branch;
521 static GTY (()) int mips_output_filename_first_time = 1;
523 /* mips_split_p[X] is true if symbols of type X can be split by
524 mips_split_symbol(). */
525 static bool mips_split_p[NUM_SYMBOL_TYPES];
527 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
528 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
529 if they are matched by a special .md file pattern. */
530 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
532 /* Likewise for HIGHs. */
533 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
535 /* Hardware names for the registers. If -mrnames is used, this
536 will be overwritten with mips_sw_reg_names. */
538 char mips_reg_names[][8] =
540 "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7",
541 "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
542 "$16", "$17", "$18", "$19", "$20", "$21", "$22", "$23",
543 "$24", "$25", "$26", "$27", "$28", "$sp", "$fp", "$31",
544 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
545 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
546 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
547 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
548 "hi", "lo", "", "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
549 "$fcc5","$fcc6","$fcc7","", "", "$arg", "$frame", "$fakec",
550 "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
551 "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
552 "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
553 "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
554 "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
555 "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
556 "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
557 "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
558 "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
559 "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
560 "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
561 "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
564 /* Mips software names for the registers, used to overwrite the
565 mips_reg_names array. */
567 char mips_sw_reg_names[][8] =
569 "$zero","$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3",
570 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
571 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
572 "$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra",
573 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
574 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
575 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
576 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
577 "hi", "lo", "", "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
578 "$fcc5","$fcc6","$fcc7","$rap", "", "$arg", "$frame", "$fakec",
579 "$c0r0", "$c0r1", "$c0r2", "$c0r3", "$c0r4", "$c0r5", "$c0r6", "$c0r7",
580 "$c0r8", "$c0r9", "$c0r10","$c0r11","$c0r12","$c0r13","$c0r14","$c0r15",
581 "$c0r16","$c0r17","$c0r18","$c0r19","$c0r20","$c0r21","$c0r22","$c0r23",
582 "$c0r24","$c0r25","$c0r26","$c0r27","$c0r28","$c0r29","$c0r30","$c0r31",
583 "$c2r0", "$c2r1", "$c2r2", "$c2r3", "$c2r4", "$c2r5", "$c2r6", "$c2r7",
584 "$c2r8", "$c2r9", "$c2r10","$c2r11","$c2r12","$c2r13","$c2r14","$c2r15",
585 "$c2r16","$c2r17","$c2r18","$c2r19","$c2r20","$c2r21","$c2r22","$c2r23",
586 "$c2r24","$c2r25","$c2r26","$c2r27","$c2r28","$c2r29","$c2r30","$c2r31",
587 "$c3r0", "$c3r1", "$c3r2", "$c3r3", "$c3r4", "$c3r5", "$c3r6", "$c3r7",
588 "$c3r8", "$c3r9", "$c3r10","$c3r11","$c3r12","$c3r13","$c3r14","$c3r15",
589 "$c3r16","$c3r17","$c3r18","$c3r19","$c3r20","$c3r21","$c3r22","$c3r23",
590 "$c3r24","$c3r25","$c3r26","$c3r27","$c3r28","$c3r29","$c3r30","$c3r31"
593 /* Map hard register number to register class */
594 const enum reg_class mips_regno_to_class[] =
596 LEA_REGS, LEA_REGS, M16_NA_REGS, M16_NA_REGS,
597 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
598 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
599 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
600 M16_NA_REGS, M16_NA_REGS, LEA_REGS, LEA_REGS,
601 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
602 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
603 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
604 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
605 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
606 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
607 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
608 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
609 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
610 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
611 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
612 HI_REG, LO_REG, NO_REGS, ST_REGS,
613 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
614 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
615 NO_REGS, ALL_REGS, ALL_REGS, NO_REGS,
616 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
617 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
618 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
619 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
620 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
621 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
622 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
623 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
624 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
625 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
626 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
627 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
628 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
629 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
630 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
631 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
632 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
633 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
634 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
635 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
636 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
637 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
638 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
639 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS
642 /* Map register constraint character to register class. */
643 enum reg_class mips_char_to_class[256];
645 /* A table describing all the processors gcc knows about. Names are
646 matched in the order listed. The first mention of an ISA level is
647 taken as the canonical name for that ISA.
649 To ease comparison, please keep this table in the same order as
650 gas's mips_cpu_info_table[]. */
651 const struct mips_cpu_info mips_cpu_info_table[] = {
652 /* Entries for generic ISAs */
653 { "mips1", PROCESSOR_R3000, 1 },
654 { "mips2", PROCESSOR_R6000, 2 },
655 { "mips3", PROCESSOR_R4000, 3 },
656 { "mips4", PROCESSOR_R8000, 4 },
657 { "mips32", PROCESSOR_4KC, 32 },
658 { "mips32r2", PROCESSOR_M4K, 33 },
659 { "mips64", PROCESSOR_5KC, 64 },
661 /* MIPS I */
662 { "r3000", PROCESSOR_R3000, 1 },
663 { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
664 { "r3900", PROCESSOR_R3900, 1 },
666 /* MIPS II */
667 { "r6000", PROCESSOR_R6000, 2 },
669 /* MIPS III */
670 { "r4000", PROCESSOR_R4000, 3 },
671 { "vr4100", PROCESSOR_R4100, 3 },
672 { "vr4111", PROCESSOR_R4111, 3 },
673 { "vr4120", PROCESSOR_R4120, 3 },
674 { "vr4130", PROCESSOR_R4130, 3 },
675 { "vr4300", PROCESSOR_R4300, 3 },
676 { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
677 { "r4600", PROCESSOR_R4600, 3 },
678 { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
679 { "r4650", PROCESSOR_R4650, 3 },
681 /* MIPS IV */
682 { "r8000", PROCESSOR_R8000, 4 },
683 { "vr5000", PROCESSOR_R5000, 4 },
684 { "vr5400", PROCESSOR_R5400, 4 },
685 { "vr5500", PROCESSOR_R5500, 4 },
686 { "rm7000", PROCESSOR_R7000, 4 },
687 { "rm9000", PROCESSOR_R9000, 4 },
689 /* MIPS32 */
690 { "4kc", PROCESSOR_4KC, 32 },
691 { "4kp", PROCESSOR_4KC, 32 }, /* = 4kc */
693 /* MIPS32 Release 2 */
694 { "m4k", PROCESSOR_M4K, 33 },
696 /* MIPS64 */
697 { "5kc", PROCESSOR_5KC, 64 },
698 { "20kc", PROCESSOR_20KC, 64 },
699 { "sb1", PROCESSOR_SB1, 64 },
700 { "sr71000", PROCESSOR_SR71000, 64 },
702 /* End marker */
703 { 0, 0, 0 }
706 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT. */
707 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
708 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
709 #endif
711 /* Initialize the GCC target structure. */
712 #undef TARGET_ASM_ALIGNED_HI_OP
713 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
714 #undef TARGET_ASM_ALIGNED_SI_OP
715 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
716 #undef TARGET_ASM_INTEGER
717 #define TARGET_ASM_INTEGER mips_assemble_integer
719 #undef TARGET_ASM_FUNCTION_PROLOGUE
720 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
721 #undef TARGET_ASM_FUNCTION_EPILOGUE
722 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
723 #undef TARGET_ASM_SELECT_RTX_SECTION
724 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
726 #undef TARGET_SCHED_REORDER
727 #define TARGET_SCHED_REORDER mips_sched_reorder
728 #undef TARGET_SCHED_VARIABLE_ISSUE
729 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
730 #undef TARGET_SCHED_ADJUST_COST
731 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
732 #undef TARGET_SCHED_ISSUE_RATE
733 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
734 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
735 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE mips_use_dfa_pipeline_interface
736 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
737 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
738 mips_multipass_dfa_lookahead
740 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
741 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
743 #undef TARGET_VALID_POINTER_MODE
744 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
745 #undef TARGET_RTX_COSTS
746 #define TARGET_RTX_COSTS mips_rtx_costs
747 #undef TARGET_ADDRESS_COST
748 #define TARGET_ADDRESS_COST mips_address_cost
750 #undef TARGET_IN_SMALL_DATA_P
751 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
753 #undef TARGET_MACHINE_DEPENDENT_REORG
754 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
756 #undef TARGET_ASM_FILE_START
757 #undef TARGET_ASM_FILE_END
758 #if TARGET_IRIX
759 #define TARGET_ASM_FILE_START irix_file_start
760 #define TARGET_ASM_FILE_END irix_file_end
761 #else
762 #define TARGET_ASM_FILE_START mips_file_start
763 #define TARGET_ASM_FILE_END mips_file_end
764 #endif
765 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
766 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
768 #if TARGET_IRIX
769 #undef TARGET_SECTION_TYPE_FLAGS
770 #define TARGET_SECTION_TYPE_FLAGS irix_section_type_flags
771 #endif
773 #undef TARGET_INIT_LIBFUNCS
774 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
776 #undef TARGET_BUILD_BUILTIN_VA_LIST
777 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
779 #undef TARGET_PROMOTE_FUNCTION_ARGS
780 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
781 #undef TARGET_PROMOTE_FUNCTION_RETURN
782 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
783 #undef TARGET_PROMOTE_PROTOTYPES
784 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
786 #undef TARGET_RETURN_IN_MEMORY
787 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
788 #undef TARGET_RETURN_IN_MSB
789 #define TARGET_RETURN_IN_MSB mips_return_in_msb
791 #undef TARGET_ASM_OUTPUT_MI_THUNK
792 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
793 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
794 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
796 #undef TARGET_SETUP_INCOMING_VARARGS
797 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
798 #undef TARGET_STRICT_ARGUMENT_NAMING
799 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
801 struct gcc_target targetm = TARGET_INITIALIZER;
803 /* Classify symbol X, which must be a SYMBOL_REF or a LABEL_REF. */
805 static enum mips_symbol_type
806 mips_classify_symbol (rtx x)
808 if (GET_CODE (x) == LABEL_REF)
810 if (TARGET_MIPS16)
811 return SYMBOL_CONSTANT_POOL;
812 if (TARGET_ABICALLS)
813 return SYMBOL_GOT_LOCAL;
814 return SYMBOL_GENERAL;
817 if (GET_CODE (x) != SYMBOL_REF)
818 abort ();
820 if (CONSTANT_POOL_ADDRESS_P (x))
822 if (TARGET_MIPS16)
823 return SYMBOL_CONSTANT_POOL;
825 if (TARGET_ABICALLS)
826 return SYMBOL_GOT_LOCAL;
828 if (GET_MODE_SIZE (get_pool_mode (x)) <= mips_section_threshold)
829 return SYMBOL_SMALL_DATA;
831 return SYMBOL_GENERAL;
834 if (SYMBOL_REF_SMALL_P (x))
835 return SYMBOL_SMALL_DATA;
837 if (TARGET_ABICALLS)
839 if (SYMBOL_REF_DECL (x) == 0)
840 return SYMBOL_REF_LOCAL_P (x) ? SYMBOL_GOT_LOCAL : SYMBOL_GOT_GLOBAL;
842 /* There are three cases to consider:
844 - o32 PIC (either with or without explicit relocs)
845 - n32/n64 PIC without explicit relocs
846 - n32/n64 PIC with explicit relocs
848 In the first case, both local and global accesses will use an
849 R_MIPS_GOT16 relocation. We must correctly predict which of
850 the two semantics (local or global) the assembler and linker
851 will apply. The choice doesn't depend on the symbol's
852 visibility, so we deliberately ignore decl_visibility and
853 binds_local_p here.
855 In the second case, the assembler will not use R_MIPS_GOT16
856 relocations, but it chooses between local and global accesses
857 in the same way as for o32 PIC.
859 In the third case we have more freedom since both forms of
860 access will work for any kind of symbol. However, there seems
861 little point in doing things differently. */
862 if (DECL_P (SYMBOL_REF_DECL (x)) && TREE_PUBLIC (SYMBOL_REF_DECL (x)))
863 return SYMBOL_GOT_GLOBAL;
865 return SYMBOL_GOT_LOCAL;
868 return SYMBOL_GENERAL;
872 /* Split X into a base and a constant offset, storing them in *BASE
873 and *OFFSET respectively. */
875 static void
876 mips_split_const (rtx x, rtx *base, HOST_WIDE_INT *offset)
878 *offset = 0;
880 if (GET_CODE (x) == CONST)
881 x = XEXP (x, 0);
883 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
885 *offset += INTVAL (XEXP (x, 1));
886 x = XEXP (x, 0);
888 *base = x;
892 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
893 to the same object as SYMBOL. */
895 static bool
896 mips_offset_within_object_p (rtx symbol, HOST_WIDE_INT offset)
898 if (GET_CODE (symbol) != SYMBOL_REF)
899 return false;
901 if (CONSTANT_POOL_ADDRESS_P (symbol)
902 && offset >= 0
903 && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
904 return true;
906 if (SYMBOL_REF_DECL (symbol) != 0
907 && offset >= 0
908 && offset < int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (symbol))))
909 return true;
911 return false;
915 /* Return true if X is a symbolic constant that can be calculated in
916 the same way as a bare symbol. If it is, store the type of the
917 symbol in *SYMBOL_TYPE. */
919 static bool
920 mips_symbolic_constant_p (rtx x, enum mips_symbol_type *symbol_type)
922 HOST_WIDE_INT offset;
924 mips_split_const (x, &x, &offset);
925 if (UNSPEC_ADDRESS_P (x))
926 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
927 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
928 *symbol_type = mips_classify_symbol (x);
929 else
930 return false;
932 if (offset == 0)
933 return true;
935 /* Check whether a nonzero offset is valid for the underlying
936 relocations. */
937 switch (*symbol_type)
939 case SYMBOL_GENERAL:
940 case SYMBOL_64_HIGH:
941 case SYMBOL_64_MID:
942 case SYMBOL_64_LOW:
943 /* If the target has 64-bit pointers and the object file only
944 supports 32-bit symbols, the values of those symbols will be
945 sign-extended. In this case we can't allow an arbitrary offset
946 in case the 32-bit value X + OFFSET has a different sign from X. */
947 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
948 return mips_offset_within_object_p (x, offset);
950 /* In other cases the relocations can handle any offset. */
951 return true;
953 case SYMBOL_CONSTANT_POOL:
954 /* Allow constant pool references to be converted to LABEL+CONSTANT.
955 In this case, we no longer have access to the underlying constant,
956 but the original symbol-based access was known to be valid. */
957 if (GET_CODE (x) == LABEL_REF)
958 return true;
960 /* Fall through. */
962 case SYMBOL_SMALL_DATA:
963 /* Make sure that the offset refers to something within the
964 underlying object. This should guarantee that the final
965 PC- or GP-relative offset is within the 16-bit limit. */
966 return mips_offset_within_object_p (x, offset);
968 case SYMBOL_GOT_LOCAL:
969 case SYMBOL_GOTOFF_PAGE:
970 /* The linker should provide enough local GOT entries for a
971 16-bit offset. Larger offsets may lead to GOT overflow. */
972 return SMALL_OPERAND (offset);
974 case SYMBOL_GOT_GLOBAL:
975 case SYMBOL_GOTOFF_GLOBAL:
976 case SYMBOL_GOTOFF_CALL:
977 case SYMBOL_GOTOFF_LOADGP:
978 return false;
980 abort ();
984 /* This function is used to implement REG_MODE_OK_FOR_BASE_P. */
987 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode, int strict)
989 if (regno >= FIRST_PSEUDO_REGISTER)
991 if (!strict)
992 return true;
993 regno = reg_renumber[regno];
996 /* These fake registers will be eliminated to either the stack or
997 hard frame pointer, both of which are usually valid base registers.
998 Reload deals with the cases where the eliminated form isn't valid. */
999 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1000 return true;
1002 /* In mips16 mode, the stack pointer can only address word and doubleword
1003 values, nothing smaller. There are two problems here:
1005 (a) Instantiating virtual registers can introduce new uses of the
1006 stack pointer. If these virtual registers are valid addresses,
1007 the stack pointer should be too.
1009 (b) Most uses of the stack pointer are not made explicit until
1010 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1011 We don't know until that stage whether we'll be eliminating to the
1012 stack pointer (which needs the restriction) or the hard frame
1013 pointer (which doesn't).
1015 All in all, it seems more consistent to only enforce this restriction
1016 during and after reload. */
1017 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1018 return !strict || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1020 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1024 /* Return true if X is a valid base register for the given mode.
1025 Allow only hard registers if STRICT. */
1027 static bool
1028 mips_valid_base_register_p (rtx x, enum machine_mode mode, int strict)
1030 if (!strict && GET_CODE (x) == SUBREG)
1031 x = SUBREG_REG (x);
1033 return (GET_CODE (x) == REG
1034 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict));
1038 /* Return true if symbols of type SYMBOL_TYPE can directly address a value
1039 with mode MODE. This is used for both symbolic and LO_SUM addresses. */
1041 static bool
1042 mips_symbolic_address_p (enum mips_symbol_type symbol_type,
1043 enum machine_mode mode)
1045 switch (symbol_type)
1047 case SYMBOL_GENERAL:
1048 return !TARGET_MIPS16;
1050 case SYMBOL_SMALL_DATA:
1051 return true;
1053 case SYMBOL_CONSTANT_POOL:
1054 /* PC-relative addressing is only available for lw and ld. */
1055 return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1057 case SYMBOL_GOT_LOCAL:
1058 return true;
1060 case SYMBOL_GOT_GLOBAL:
1061 /* The address will have to be loaded from the GOT first. */
1062 return false;
1064 case SYMBOL_GOTOFF_PAGE:
1065 case SYMBOL_GOTOFF_GLOBAL:
1066 case SYMBOL_GOTOFF_CALL:
1067 case SYMBOL_GOTOFF_LOADGP:
1068 case SYMBOL_64_HIGH:
1069 case SYMBOL_64_MID:
1070 case SYMBOL_64_LOW:
1071 return true;
1073 abort ();
1077 /* Return true if X is a valid address for machine mode MODE. If it is,
1078 fill in INFO appropriately. STRICT is true if we should only accept
1079 hard base registers. */
1081 static bool
1082 mips_classify_address (struct mips_address_info *info, rtx x,
1083 enum machine_mode mode, int strict)
1085 switch (GET_CODE (x))
1087 case REG:
1088 case SUBREG:
1089 info->type = ADDRESS_REG;
1090 info->reg = x;
1091 info->offset = const0_rtx;
1092 return mips_valid_base_register_p (info->reg, mode, strict);
1094 case PLUS:
1095 info->type = ADDRESS_REG;
1096 info->reg = XEXP (x, 0);
1097 info->offset = XEXP (x, 1);
1098 return (mips_valid_base_register_p (info->reg, mode, strict)
1099 && const_arith_operand (info->offset, VOIDmode));
1101 case LO_SUM:
1102 info->type = ADDRESS_LO_SUM;
1103 info->reg = XEXP (x, 0);
1104 info->offset = XEXP (x, 1);
1105 return (mips_valid_base_register_p (info->reg, mode, strict)
1106 && mips_symbolic_constant_p (info->offset, &info->symbol_type)
1107 && mips_symbolic_address_p (info->symbol_type, mode)
1108 && mips_lo_relocs[info->symbol_type] != 0);
1110 case CONST_INT:
1111 /* Small-integer addresses don't occur very often, but they
1112 are legitimate if $0 is a valid base register. */
1113 info->type = ADDRESS_CONST_INT;
1114 return !TARGET_MIPS16 && SMALL_INT (x);
1116 case CONST:
1117 case LABEL_REF:
1118 case SYMBOL_REF:
1119 info->type = ADDRESS_SYMBOLIC;
1120 return (mips_symbolic_constant_p (x, &info->symbol_type)
1121 && mips_symbolic_address_p (info->symbol_type, mode)
1122 && !mips_split_p[info->symbol_type]);
1124 default:
1125 return false;
1129 /* Return the number of instructions needed to load a symbol of the
1130 given type into a register. If valid in an address, the same number
1131 of instructions are needed for loads and stores. Treat extended
1132 mips16 instructions as two instructions. */
1134 static int
1135 mips_symbol_insns (enum mips_symbol_type type)
1137 switch (type)
1139 case SYMBOL_GENERAL:
1140 /* In mips16 code, general symbols must be fetched from the
1141 constant pool. */
1142 if (TARGET_MIPS16)
1143 return 0;
1145 /* When using 64-bit symbols, we need 5 preparatory instructions,
1146 such as:
1148 lui $at,%highest(symbol)
1149 daddiu $at,$at,%higher(symbol)
1150 dsll $at,$at,16
1151 daddiu $at,$at,%hi(symbol)
1152 dsll $at,$at,16
1154 The final address is then $at + %lo(symbol). With 32-bit
1155 symbols we just need a preparatory lui. */
1156 return (ABI_HAS_64BIT_SYMBOLS ? 6 : 2);
1158 case SYMBOL_SMALL_DATA:
1159 return 1;
1161 case SYMBOL_CONSTANT_POOL:
1162 /* This case is for mips16 only. Assume we'll need an
1163 extended instruction. */
1164 return 2;
1166 case SYMBOL_GOT_LOCAL:
1167 case SYMBOL_GOT_GLOBAL:
1168 /* Unless -funit-at-a-time is in effect, we can't be sure whether
1169 the local/global classification is accurate. See override_options
1170 for details.
1172 The worst cases are:
1174 (1) For local symbols when generating o32 or o64 code. The assembler
1175 will use:
1177 lw $at,%got(symbol)
1180 ...and the final address will be $at + %lo(symbol).
1182 (2) For global symbols when -mxgot. The assembler will use:
1184 lui $at,%got_hi(symbol)
1185 (d)addu $at,$at,$gp
1187 ...and the final address will be $at + %got_lo(symbol). */
1188 return 3;
1190 case SYMBOL_GOTOFF_PAGE:
1191 case SYMBOL_GOTOFF_GLOBAL:
1192 case SYMBOL_GOTOFF_CALL:
1193 case SYMBOL_GOTOFF_LOADGP:
1194 case SYMBOL_64_HIGH:
1195 case SYMBOL_64_MID:
1196 case SYMBOL_64_LOW:
1197 /* Check whether the offset is a 16- or 32-bit value. */
1198 return mips_split_p[type] ? 2 : 1;
1200 abort ();
1204 /* Return true if a value at OFFSET bytes from BASE can be accessed
1205 using an unextended mips16 instruction. MODE is the mode of the
1206 value.
1208 Usually the offset in an unextended instruction is a 5-bit field.
1209 The offset is unsigned and shifted left once for HIs, twice
1210 for SIs, and so on. An exception is SImode accesses off the
1211 stack pointer, which have an 8-bit immediate field. */
1213 static bool
1214 mips16_unextended_reference_p (enum machine_mode mode, rtx base, rtx offset)
1216 if (TARGET_MIPS16
1217 && GET_CODE (offset) == CONST_INT
1218 && INTVAL (offset) >= 0
1219 && (INTVAL (offset) & (GET_MODE_SIZE (mode) - 1)) == 0)
1221 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1222 return INTVAL (offset) < 256 * GET_MODE_SIZE (mode);
1223 return INTVAL (offset) < 32 * GET_MODE_SIZE (mode);
1225 return false;
1229 /* Return the number of instructions needed to load or store a value
1230 of mode MODE at X. Return 0 if X isn't valid for MODE.
1232 For mips16 code, count extended instructions as two instructions. */
1235 mips_address_insns (rtx x, enum machine_mode mode)
1237 struct mips_address_info addr;
1238 int factor;
1240 if (mode == BLKmode)
1241 /* BLKmode is used for single unaligned loads and stores. */
1242 factor = 1;
1243 else
1244 /* Each word of a multi-word value will be accessed individually. */
1245 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1247 if (mips_classify_address (&addr, x, mode, false))
1248 switch (addr.type)
1250 case ADDRESS_REG:
1251 if (TARGET_MIPS16
1252 && !mips16_unextended_reference_p (mode, addr.reg, addr.offset))
1253 return factor * 2;
1254 return factor;
1256 case ADDRESS_LO_SUM:
1257 return (TARGET_MIPS16 ? factor * 2 : factor);
1259 case ADDRESS_CONST_INT:
1260 return factor;
1262 case ADDRESS_SYMBOLIC:
1263 return factor * mips_symbol_insns (addr.symbol_type);
1265 return 0;
1269 /* Likewise for constant X. */
1272 mips_const_insns (rtx x)
1274 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1275 enum mips_symbol_type symbol_type;
1276 HOST_WIDE_INT offset;
1278 switch (GET_CODE (x))
1280 case HIGH:
1281 if (TARGET_MIPS16
1282 || !mips_symbolic_constant_p (XEXP (x, 0), &symbol_type)
1283 || !mips_split_p[symbol_type])
1284 return 0;
1286 return 1;
1288 case CONST_INT:
1289 if (TARGET_MIPS16)
1290 /* Unsigned 8-bit constants can be loaded using an unextended
1291 LI instruction. Unsigned 16-bit constants can be loaded
1292 using an extended LI. Negative constants must be loaded
1293 using LI and then negated. */
1294 return (INTVAL (x) >= 0 && INTVAL (x) < 256 ? 1
1295 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
1296 : INTVAL (x) > -256 && INTVAL (x) < 0 ? 2
1297 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
1298 : 0);
1300 return mips_build_integer (codes, INTVAL (x));
1302 case CONST_DOUBLE:
1303 return (!TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0);
1305 case CONST:
1306 if (CONST_GP_P (x))
1307 return 1;
1309 /* See if we can refer to X directly. */
1310 if (mips_symbolic_constant_p (x, &symbol_type))
1311 return mips_symbol_insns (symbol_type);
1313 /* Otherwise try splitting the constant into a base and offset.
1314 16-bit offsets can be added using an extra addiu. Larger offsets
1315 must be calculated separately and then added to the base. */
1316 mips_split_const (x, &x, &offset);
1317 if (offset != 0)
1319 int n = mips_const_insns (x);
1320 if (n != 0)
1322 if (SMALL_OPERAND (offset))
1323 return n + 1;
1324 else
1325 return n + 1 + mips_build_integer (codes, offset);
1328 return 0;
1330 case SYMBOL_REF:
1331 case LABEL_REF:
1332 return mips_symbol_insns (mips_classify_symbol (x));
1334 default:
1335 return 0;
1340 /* Return the number of instructions needed for memory reference X.
1341 Count extended mips16 instructions as two instructions. */
1344 mips_fetch_insns (rtx x)
1346 if (GET_CODE (x) != MEM)
1347 abort ();
1349 return mips_address_insns (XEXP (x, 0), GET_MODE (x));
1353 /* Return the number of instructions needed for an integer division. */
1356 mips_idiv_insns (void)
1358 int count;
1360 count = 1;
1361 if (TARGET_CHECK_ZERO_DIV)
1362 count += 2;
1363 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
1364 count++;
1365 return count;
1369 /* Return truth value of whether OP can be used as an operands
1370 where a register or 16 bit unsigned integer is needed. */
1373 uns_arith_operand (rtx op, enum machine_mode mode)
1375 if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
1376 return 1;
1378 return register_operand (op, mode);
1382 /* True if OP can be treated as a signed 16-bit constant. */
1385 const_arith_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1387 return GET_CODE (op) == CONST_INT && SMALL_INT (op);
1391 /* Return true if OP is a register operand or a signed 16-bit constant. */
1394 arith_operand (rtx op, enum machine_mode mode)
1396 return const_arith_operand (op, mode) || register_operand (op, mode);
1399 /* Return truth value of whether OP is an integer which fits in 16 bits. */
1402 small_int (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1404 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
1407 /* Return truth value of whether OP is a register or the constant 0.
1408 Do not accept 0 in mips16 mode since $0 is not one of the core 8
1409 registers. */
1412 reg_or_0_operand (rtx op, enum machine_mode mode)
1414 switch (GET_CODE (op))
1416 case CONST_INT:
1417 if (TARGET_MIPS16)
1418 return 0;
1419 return INTVAL (op) == 0;
1421 case CONST_DOUBLE:
1422 if (TARGET_MIPS16)
1423 return 0;
1424 return op == CONST0_RTX (mode);
1426 default:
1427 return register_operand (op, mode);
1431 /* Accept a register or the floating point constant 1 in the appropriate mode. */
1434 reg_or_const_float_1_operand (rtx op, enum machine_mode mode)
1436 REAL_VALUE_TYPE d;
1438 switch (GET_CODE (op))
1440 case CONST_DOUBLE:
1441 if (mode != GET_MODE (op)
1442 || (mode != DFmode && mode != SFmode))
1443 return 0;
1445 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1446 return REAL_VALUES_EQUAL (d, dconst1);
1448 default:
1449 return register_operand (op, mode);
1453 /* Accept the floating point constant 1 in the appropriate mode. */
1456 const_float_1_operand (rtx op, enum machine_mode mode)
1458 REAL_VALUE_TYPE d;
1460 if (GET_CODE (op) != CONST_DOUBLE
1461 || mode != GET_MODE (op)
1462 || (mode != DFmode && mode != SFmode))
1463 return 0;
1465 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1467 return REAL_VALUES_EQUAL (d, dconst1);
1470 /* Return true if OP is either the HI or LO register. */
1473 hilo_operand (rtx op, enum machine_mode mode)
1475 return ((mode == VOIDmode || mode == GET_MODE (op))
1476 && REG_P (op) && MD_REG_P (REGNO (op)));
1479 /* Return true if OP is an extension operator. */
1482 extend_operator (rtx op, enum machine_mode mode)
1484 return ((mode == VOIDmode || mode == GET_MODE (op))
1485 && (GET_CODE (op) == ZERO_EXTEND || GET_CODE (op) == SIGN_EXTEND));
1488 /* Return true if X is the right hand side of a "macc" or "msac" instruction.
1489 This predicate is intended for use in peephole optimizations. */
1492 macc_msac_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1494 if (ISA_HAS_MACC && GET_CODE (x) == PLUS && REG_P (XEXP (x, 1)))
1495 x = XEXP (x, 0);
1496 else if (ISA_HAS_MSAC && GET_CODE (x) == MINUS && REG_P (XEXP (x, 0)))
1497 x = XEXP (x, 1);
1498 else
1499 return false;
1501 return GET_CODE (x) == MULT && REG_P (XEXP (x, 0)) && REG_P (XEXP (x, 1));
1504 /* Return nonzero if the code of this rtx pattern is EQ or NE. */
1507 equality_op (rtx op, enum machine_mode mode)
1509 if (mode != GET_MODE (op))
1510 return 0;
1512 return GET_CODE (op) == EQ || GET_CODE (op) == NE;
1515 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
1518 cmp_op (rtx op, enum machine_mode mode)
1520 if (mode != GET_MODE (op))
1521 return 0;
1523 return COMPARISON_P (op);
1526 /* Return nonzero if the code is a relational operation suitable for a
1527 conditional trap instruction (only EQ, NE, LT, LTU, GE, GEU).
1528 We need this in the insn that expands `trap_if' in order to prevent
1529 combine from erroneously altering the condition. */
1532 trap_cmp_op (rtx op, enum machine_mode mode)
1534 if (mode != GET_MODE (op))
1535 return 0;
1537 switch (GET_CODE (op))
1539 case EQ:
1540 case NE:
1541 case LT:
1542 case LTU:
1543 case GE:
1544 case GEU:
1545 return 1;
1547 default:
1548 return 0;
1552 /* Return nonzero if the operand is either the PC or a label_ref. */
1555 pc_or_label_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1557 if (op == pc_rtx)
1558 return 1;
1560 if (GET_CODE (op) == LABEL_REF)
1561 return 1;
1563 return 0;
1566 /* Test for a valid call address. */
1569 call_insn_operand (rtx op, enum machine_mode mode)
1571 enum mips_symbol_type symbol_type;
1573 if (mips_symbolic_constant_p (op, &symbol_type))
1574 switch (symbol_type)
1576 case SYMBOL_GENERAL:
1577 /* If -mlong-calls, force all calls to use register addressing. */
1578 return !TARGET_LONG_CALLS;
1580 case SYMBOL_GOT_GLOBAL:
1581 /* Without explicit relocs, there is no special syntax for
1582 loading the address of a call destination into a register.
1583 Using "la $25,foo; jal $25" would prevent the lazy binding
1584 of "foo", so keep the address of global symbols with the
1585 jal macro. */
1586 return !TARGET_EXPLICIT_RELOCS;
1588 default:
1589 return false;
1591 return register_operand (op, mode);
1595 /* Return nonzero if OP is valid as a source operand for a move
1596 instruction. */
1599 move_operand (rtx op, enum machine_mode mode)
1601 enum mips_symbol_type symbol_type;
1603 if (!general_operand (op, mode))
1604 return false;
1606 switch (GET_CODE (op))
1608 case CONST_INT:
1609 /* When generating mips16 code, LEGITIMATE_CONSTANT_P rejects
1610 CONST_INTs that can't be loaded using simple insns. */
1611 if (TARGET_MIPS16)
1612 return true;
1614 /* When generating 32-bit code, allow DImode move_operands to
1615 match arbitrary constants. We split them after reload. */
1616 if (!TARGET_64BIT && mode == DImode)
1617 return true;
1619 /* Otherwise check whether the constant can be loaded in a single
1620 instruction. */
1621 return LUI_INT (op) || SMALL_INT (op) || SMALL_INT_UNSIGNED (op);
1623 case CONST:
1624 case SYMBOL_REF:
1625 case LABEL_REF:
1626 if (CONST_GP_P (op))
1627 return true;
1629 return (mips_symbolic_constant_p (op, &symbol_type)
1630 && !mips_split_p[symbol_type]);
1632 default:
1633 return true;
1638 /* Accept any operand that can appear in a mips16 constant table
1639 instruction. We can't use any of the standard operand functions
1640 because for these instructions we accept values that are not
1641 accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs. */
1644 consttable_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1646 return CONSTANT_P (op);
1649 /* Return 1 if OP is a symbolic operand, i.e. a symbol_ref or a label_ref,
1650 possibly with an offset. */
1653 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1655 enum mips_symbol_type symbol_type;
1657 return mips_symbolic_constant_p (op, &symbol_type);
1661 /* Return true if OP is a symbolic constant of type SYMBOL_GENERAL. */
1664 general_symbolic_operand (rtx op, enum machine_mode mode)
1666 enum mips_symbol_type symbol_type;
1668 return ((mode == VOIDmode || mode == GET_MODE (op))
1669 && mips_symbolic_constant_p (op, &symbol_type)
1670 && symbol_type == SYMBOL_GENERAL);
1674 /* Return true if we're generating PIC and OP is a global symbol. */
1677 global_got_operand (rtx op, enum machine_mode mode)
1679 enum mips_symbol_type symbol_type;
1681 return ((mode == VOIDmode || mode == GET_MODE (op))
1682 && mips_symbolic_constant_p (op, &symbol_type)
1683 && symbol_type == SYMBOL_GOT_GLOBAL);
1687 /* Likewise for local symbols. */
1690 local_got_operand (rtx op, enum machine_mode mode)
1692 enum mips_symbol_type symbol_type;
1694 return ((mode == VOIDmode || mode == GET_MODE (op))
1695 && mips_symbolic_constant_p (op, &symbol_type)
1696 && symbol_type == SYMBOL_GOT_LOCAL);
1700 /* Return true if OP is a memory reference that uses the stack pointer
1701 as a base register. */
1704 stack_operand (rtx op, enum machine_mode mode)
1706 struct mips_address_info addr;
1708 return ((mode == VOIDmode || mode == GET_MODE (op))
1709 && GET_CODE (op) == MEM
1710 && mips_classify_address (&addr, XEXP (op, 0), GET_MODE (op), false)
1711 && addr.type == ADDRESS_REG
1712 && addr.reg == stack_pointer_rtx);
1715 /* Helper function for DFA schedulers. Return true if OP is a floating
1716 point register. */
1719 fp_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1721 return REG_P (op) && FP_REG_P (REGNO (op));
1724 /* Helper function for DFA schedulers. Return true if OP is a LO reg. */
1727 lo_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1729 return REG_P (op) && REGNO (op) == LO_REGNUM;
1733 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS. It
1734 returns a nonzero value if X is a legitimate address for a memory
1735 operand of the indicated MODE. STRICT is nonzero if this function
1736 is called during reload. */
1738 bool
1739 mips_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1741 struct mips_address_info addr;
1743 return mips_classify_address (&addr, x, mode, strict);
1747 /* Copy VALUE to a register and return that register. If new psuedos
1748 are allowed, copy it into a new register, otherwise use DEST. */
1750 static rtx
1751 mips_force_temporary (rtx dest, rtx value)
1753 if (!no_new_pseudos)
1754 return force_reg (Pmode, value);
1755 else
1757 emit_move_insn (copy_rtx (dest), value);
1758 return dest;
1763 /* Return a LO_SUM expression for ADDR. TEMP is as for mips_force_temporary
1764 and is used to load the high part into a register. */
1766 static rtx
1767 mips_split_symbol (rtx temp, rtx addr)
1769 rtx high;
1771 if (TARGET_MIPS16)
1772 high = mips16_gp_pseudo_reg ();
1773 else
1774 high = mips_force_temporary (temp, gen_rtx_HIGH (Pmode, copy_rtx (addr)));
1775 return gen_rtx_LO_SUM (Pmode, high, addr);
1779 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1780 type SYMBOL_TYPE. */
1783 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
1785 rtx base;
1786 HOST_WIDE_INT offset;
1788 mips_split_const (address, &base, &offset);
1789 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1790 UNSPEC_ADDRESS_FIRST + symbol_type);
1791 return plus_constant (gen_rtx_CONST (Pmode, base), offset);
1795 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1796 high part to BASE and return the result. Just return BASE otherwise.
1797 TEMP is available as a temporary register if needed.
1799 The returned expression can be used as the first operand to a LO_SUM. */
1801 static rtx
1802 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
1803 enum mips_symbol_type symbol_type)
1805 if (mips_split_p[symbol_type])
1807 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
1808 addr = mips_force_temporary (temp, addr);
1809 return mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
1811 return base;
1815 /* Return a legitimate address for REG + OFFSET. This function will
1816 create a temporary register if OFFSET is not a SMALL_OPERAND. */
1818 static rtx
1819 mips_add_offset (rtx reg, HOST_WIDE_INT offset)
1821 if (!SMALL_OPERAND (offset))
1822 reg = expand_simple_binop (GET_MODE (reg), PLUS,
1823 GEN_INT (CONST_HIGH_PART (offset)),
1824 reg, NULL, 0, OPTAB_WIDEN);
1826 return plus_constant (reg, CONST_LOW_PART (offset));
1830 /* This function is used to implement LEGITIMIZE_ADDRESS. If *XLOC can
1831 be legitimized in a way that the generic machinery might not expect,
1832 put the new address in *XLOC and return true. MODE is the mode of
1833 the memory being accessed. */
1835 bool
1836 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
1838 enum mips_symbol_type symbol_type;
1840 /* See if the address can split into a high part and a LO_SUM. */
1841 if (mips_symbolic_constant_p (*xloc, &symbol_type)
1842 && mips_symbolic_address_p (symbol_type, mode)
1843 && mips_split_p[symbol_type])
1845 *xloc = mips_split_symbol (0, *xloc);
1846 return true;
1849 if (GET_CODE (*xloc) == PLUS && GET_CODE (XEXP (*xloc, 1)) == CONST_INT)
1851 /* Handle REG + CONSTANT using mips_add_offset. */
1852 rtx reg;
1854 reg = XEXP (*xloc, 0);
1855 if (!mips_valid_base_register_p (reg, mode, 0))
1856 reg = copy_to_mode_reg (Pmode, reg);
1857 *xloc = mips_add_offset (reg, INTVAL (XEXP (*xloc, 1)));
1858 return true;
1861 return false;
1865 /* Subroutine of mips_build_integer (with the same interface).
1866 Assume that the final action in the sequence should be a left shift. */
1868 static unsigned int
1869 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1871 unsigned int i, shift;
1873 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1874 since signed numbers are easier to load than unsigned ones. */
1875 shift = 0;
1876 while ((value & 1) == 0)
1877 value /= 2, shift++;
1879 i = mips_build_integer (codes, value);
1880 codes[i].code = ASHIFT;
1881 codes[i].value = shift;
1882 return i + 1;
1886 /* As for mips_build_shift, but assume that the final action will be
1887 an IOR or PLUS operation. */
1889 static unsigned int
1890 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1892 unsigned HOST_WIDE_INT high;
1893 unsigned int i;
1895 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1896 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1898 /* The constant is too complex to load with a simple lui/ori pair
1899 so our goal is to clear as many trailing zeros as possible.
1900 In this case, we know bit 16 is set and that the low 16 bits
1901 form a negative number. If we subtract that number from VALUE,
1902 we will clear at least the lowest 17 bits, maybe more. */
1903 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1904 codes[i].code = PLUS;
1905 codes[i].value = CONST_LOW_PART (value);
1907 else
1909 i = mips_build_integer (codes, high);
1910 codes[i].code = IOR;
1911 codes[i].value = value & 0xffff;
1913 return i + 1;
1917 /* Fill CODES with a sequence of rtl operations to load VALUE.
1918 Return the number of operations needed. */
1920 static unsigned int
1921 mips_build_integer (struct mips_integer_op *codes,
1922 unsigned HOST_WIDE_INT value)
1924 if (SMALL_OPERAND (value)
1925 || SMALL_OPERAND_UNSIGNED (value)
1926 || LUI_OPERAND (value))
1928 /* The value can be loaded with a single instruction. */
1929 codes[0].code = NIL;
1930 codes[0].value = value;
1931 return 1;
1933 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1935 /* Either the constant is a simple LUI/ORI combination or its
1936 lowest bit is set. We don't want to shift in this case. */
1937 return mips_build_lower (codes, value);
1939 else if ((value & 0xffff) == 0)
1941 /* The constant will need at least three actions. The lowest
1942 16 bits are clear, so the final action will be a shift. */
1943 return mips_build_shift (codes, value);
1945 else
1947 /* The final action could be a shift, add or inclusive OR.
1948 Rather than use a complex condition to select the best
1949 approach, try both mips_build_shift and mips_build_lower
1950 and pick the one that gives the shortest sequence.
1951 Note that this case is only used once per constant. */
1952 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1953 unsigned int cost, alt_cost;
1955 cost = mips_build_shift (codes, value);
1956 alt_cost = mips_build_lower (alt_codes, value);
1957 if (alt_cost < cost)
1959 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1960 cost = alt_cost;
1962 return cost;
1967 /* Move VALUE into register DEST. */
1969 static void
1970 mips_move_integer (rtx dest, unsigned HOST_WIDE_INT value)
1972 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1973 enum machine_mode mode;
1974 unsigned int i, cost;
1975 rtx x;
1977 mode = GET_MODE (dest);
1978 cost = mips_build_integer (codes, value);
1980 /* Apply each binary operation to X. Invariant: X is a legitimate
1981 source operand for a SET pattern. */
1982 x = GEN_INT (codes[0].value);
1983 for (i = 1; i < cost; i++)
1985 if (no_new_pseudos)
1986 emit_move_insn (dest, x), x = dest;
1987 else
1988 x = force_reg (mode, x);
1989 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1992 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1996 /* Subroutine of mips_legitimize_move. Move constant SRC into register
1997 DEST given that SRC satisfies immediate_operand but doesn't satisfy
1998 move_operand. */
2000 static void
2001 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
2003 rtx base;
2004 HOST_WIDE_INT offset;
2005 enum mips_symbol_type symbol_type;
2007 /* Split moves of big integers into smaller pieces. In mips16 code,
2008 it's better to force the constant into memory instead. */
2009 if (GET_CODE (src) == CONST_INT && !TARGET_MIPS16)
2011 mips_move_integer (dest, INTVAL (src));
2012 return;
2015 /* See if the symbol can be split. For mips16, this is often worse than
2016 forcing it in the constant pool since it needs the single-register form
2017 of addiu or daddiu. */
2018 if (!TARGET_MIPS16
2019 && mips_symbolic_constant_p (src, &symbol_type)
2020 && mips_split_p[symbol_type])
2022 emit_move_insn (dest, mips_split_symbol (dest, src));
2023 return;
2026 /* If we have (const (plus symbol offset)), load the symbol first
2027 and then add in the offset. This is usually better than forcing
2028 the constant into memory, at least in non-mips16 code. */
2029 mips_split_const (src, &base, &offset);
2030 if (!TARGET_MIPS16
2031 && offset != 0
2032 && (!no_new_pseudos || SMALL_OPERAND (offset)))
2034 base = mips_force_temporary (dest, base);
2035 emit_move_insn (dest, mips_add_offset (base, offset));
2036 return;
2039 src = force_const_mem (mode, src);
2041 /* When using explicit relocs, constant pool references are sometimes
2042 not legitimate addresses. */
2043 if (!memory_operand (src, VOIDmode))
2044 src = replace_equiv_address (src, mips_split_symbol (dest, XEXP (src, 0)));
2045 emit_move_insn (dest, src);
2049 /* If (set DEST SRC) is not a valid instruction, emit an equivalent
2050 sequence that is valid. */
2052 bool
2053 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2055 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2057 emit_move_insn (dest, force_reg (mode, src));
2058 return true;
2061 /* Check for individual, fully-reloaded mflo and mfhi instructions. */
2062 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2063 && REG_P (src) && MD_REG_P (REGNO (src))
2064 && REG_P (dest) && GP_REG_P (REGNO (dest)))
2066 int other_regno = REGNO (src) == HI_REGNUM ? LO_REGNUM : HI_REGNUM;
2067 if (GET_MODE_SIZE (mode) <= 4)
2068 emit_insn (gen_mfhilo_si (gen_rtx_REG (SImode, REGNO (dest)),
2069 gen_rtx_REG (SImode, REGNO (src)),
2070 gen_rtx_REG (SImode, other_regno)));
2071 else
2072 emit_insn (gen_mfhilo_di (gen_rtx_REG (DImode, REGNO (dest)),
2073 gen_rtx_REG (DImode, REGNO (src)),
2074 gen_rtx_REG (DImode, other_regno)));
2075 return true;
2078 /* We need to deal with constants that would be legitimate
2079 immediate_operands but not legitimate move_operands. */
2080 if (CONSTANT_P (src) && !move_operand (src, mode))
2082 mips_legitimize_const_move (mode, dest, src);
2083 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2084 return true;
2086 return false;
2089 /* We need a lot of little routines to check constant values on the
2090 mips16. These are used to figure out how long the instruction will
2091 be. It would be much better to do this using constraints, but
2092 there aren't nearly enough letters available. */
2094 static int
2095 m16_check_op (rtx op, int low, int high, int mask)
2097 return (GET_CODE (op) == CONST_INT
2098 && INTVAL (op) >= low
2099 && INTVAL (op) <= high
2100 && (INTVAL (op) & mask) == 0);
2104 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2106 return m16_check_op (op, 0x1, 0x8, 0);
2110 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2112 return m16_check_op (op, - 0x8, 0x7, 0);
2116 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2118 return m16_check_op (op, - 0x7, 0x8, 0);
2122 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2124 return m16_check_op (op, - 0x10, 0xf, 0);
2128 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2130 return m16_check_op (op, - 0xf, 0x10, 0);
2134 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2136 return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
2140 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2142 return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
2146 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2148 return m16_check_op (op, - 0x80, 0x7f, 0);
2152 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2154 return m16_check_op (op, - 0x7f, 0x80, 0);
2158 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2160 return m16_check_op (op, 0x0, 0xff, 0);
2164 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2166 return m16_check_op (op, - 0xff, 0x0, 0);
2170 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2172 return m16_check_op (op, - 0x1, 0xfe, 0);
2176 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2178 return m16_check_op (op, 0x0, 0xff << 2, 3);
2182 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2184 return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
2188 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2190 return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
2194 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2196 return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
2199 static bool
2200 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
2202 enum machine_mode mode = GET_MODE (x);
2204 switch (code)
2206 case CONST_INT:
2207 if (!TARGET_MIPS16)
2209 /* Always return 0, since we don't have different sized
2210 instructions, hence different costs according to Richard
2211 Kenner */
2212 *total = 0;
2213 return true;
2216 /* A number between 1 and 8 inclusive is efficient for a shift.
2217 Otherwise, we will need an extended instruction. */
2218 if ((outer_code) == ASHIFT || (outer_code) == ASHIFTRT
2219 || (outer_code) == LSHIFTRT)
2221 if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
2222 *total = 0;
2223 else
2224 *total = COSTS_N_INSNS (1);
2225 return true;
2228 /* We can use cmpi for an xor with an unsigned 16 bit value. */
2229 if ((outer_code) == XOR
2230 && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
2232 *total = 0;
2233 return true;
2236 /* We may be able to use slt or sltu for a comparison with a
2237 signed 16 bit value. (The boundary conditions aren't quite
2238 right, but this is just a heuristic anyhow.) */
2239 if (((outer_code) == LT || (outer_code) == LE
2240 || (outer_code) == GE || (outer_code) == GT
2241 || (outer_code) == LTU || (outer_code) == LEU
2242 || (outer_code) == GEU || (outer_code) == GTU)
2243 && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
2245 *total = 0;
2246 return true;
2249 /* Equality comparisons with 0 are cheap. */
2250 if (((outer_code) == EQ || (outer_code) == NE)
2251 && INTVAL (x) == 0)
2253 *total = 0;
2254 return true;
2257 /* Otherwise fall through to the handling below. */
2259 case CONST:
2260 case SYMBOL_REF:
2261 case LABEL_REF:
2262 case CONST_DOUBLE:
2263 if (LEGITIMATE_CONSTANT_P (x))
2265 *total = COSTS_N_INSNS (1);
2266 return true;
2268 else
2270 /* The value will need to be fetched from the constant pool. */
2271 *total = CONSTANT_POOL_COST;
2272 return true;
2275 case MEM:
2277 /* If the address is legitimate, return the number of
2278 instructions it needs, otherwise use the default handling. */
2279 int n = mips_address_insns (XEXP (x, 0), GET_MODE (x));
2280 if (n > 0)
2282 *total = COSTS_N_INSNS (1 + n);
2283 return true;
2285 return false;
2288 case FFS:
2289 *total = COSTS_N_INSNS (6);
2290 return true;
2292 case NOT:
2293 *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
2294 return true;
2296 case AND:
2297 case IOR:
2298 case XOR:
2299 if (mode == DImode && !TARGET_64BIT)
2301 *total = COSTS_N_INSNS (2);
2302 return true;
2304 return false;
2306 case ASHIFT:
2307 case ASHIFTRT:
2308 case LSHIFTRT:
2309 if (mode == DImode && !TARGET_64BIT)
2311 *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
2312 ? 4 : 12);
2313 return true;
2315 return false;
2317 case ABS:
2318 if (mode == SFmode || mode == DFmode)
2319 *total = COSTS_N_INSNS (1);
2320 else
2321 *total = COSTS_N_INSNS (4);
2322 return true;
2324 case LO_SUM:
2325 *total = COSTS_N_INSNS (1);
2326 return true;
2328 case PLUS:
2329 case MINUS:
2330 if (mode == SFmode || mode == DFmode)
2332 if (TUNE_MIPS3000 || TUNE_MIPS3900)
2333 *total = COSTS_N_INSNS (2);
2334 else if (TUNE_MIPS6000)
2335 *total = COSTS_N_INSNS (3);
2336 else if (TUNE_SB1)
2337 *total = COSTS_N_INSNS (4);
2338 else
2339 *total = COSTS_N_INSNS (6);
2340 return true;
2342 if (mode == DImode && !TARGET_64BIT)
2344 *total = COSTS_N_INSNS (4);
2345 return true;
2347 return false;
2349 case NEG:
2350 if (mode == DImode && !TARGET_64BIT)
2352 *total = 4;
2353 return true;
2355 return false;
2357 case MULT:
2358 if (mode == SFmode)
2360 if (TUNE_MIPS3000
2361 || TUNE_MIPS3900
2362 || TUNE_MIPS5000
2363 || TUNE_SB1)
2364 *total = COSTS_N_INSNS (4);
2365 else if (TUNE_MIPS6000
2366 || TUNE_MIPS5400
2367 || TUNE_MIPS5500)
2368 *total = COSTS_N_INSNS (5);
2369 else
2370 *total = COSTS_N_INSNS (7);
2371 return true;
2374 if (mode == DFmode)
2376 if (TUNE_SB1)
2377 *total = COSTS_N_INSNS (4);
2378 else if (TUNE_MIPS3000
2379 || TUNE_MIPS3900
2380 || TUNE_MIPS5000)
2381 *total = COSTS_N_INSNS (5);
2382 else if (TUNE_MIPS6000
2383 || TUNE_MIPS5400
2384 || TUNE_MIPS5500)
2385 *total = COSTS_N_INSNS (6);
2386 else
2387 *total = COSTS_N_INSNS (8);
2388 return true;
2391 if (TUNE_MIPS3000)
2392 *total = COSTS_N_INSNS (12);
2393 else if (TUNE_MIPS3900)
2394 *total = COSTS_N_INSNS (2);
2395 else if (TUNE_MIPS4130)
2396 *total = COSTS_N_INSNS (mode == DImode ? 6 : 4);
2397 else if (TUNE_MIPS5400 || TUNE_SB1)
2398 *total = COSTS_N_INSNS (mode == DImode ? 4 : 3);
2399 else if (TUNE_MIPS5500 || TUNE_MIPS7000)
2400 *total = COSTS_N_INSNS (mode == DImode ? 9 : 5);
2401 else if (TUNE_MIPS9000)
2402 *total = COSTS_N_INSNS (mode == DImode ? 8 : 3);
2403 else if (TUNE_MIPS6000)
2404 *total = COSTS_N_INSNS (17);
2405 else if (TUNE_MIPS5000)
2406 *total = COSTS_N_INSNS (5);
2407 else
2408 *total = COSTS_N_INSNS (10);
2409 return true;
2411 case DIV:
2412 case MOD:
2413 if (mode == SFmode)
2415 if (TUNE_MIPS3000
2416 || TUNE_MIPS3900)
2417 *total = COSTS_N_INSNS (12);
2418 else if (TUNE_MIPS6000)
2419 *total = COSTS_N_INSNS (15);
2420 else if (TUNE_SB1)
2421 *total = COSTS_N_INSNS (24);
2422 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2423 *total = COSTS_N_INSNS (30);
2424 else
2425 *total = COSTS_N_INSNS (23);
2426 return true;
2429 if (mode == DFmode)
2431 if (TUNE_MIPS3000
2432 || TUNE_MIPS3900)
2433 *total = COSTS_N_INSNS (19);
2434 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2435 *total = COSTS_N_INSNS (59);
2436 else if (TUNE_MIPS6000)
2437 *total = COSTS_N_INSNS (16);
2438 else if (TUNE_SB1)
2439 *total = COSTS_N_INSNS (32);
2440 else
2441 *total = COSTS_N_INSNS (36);
2442 return true;
2444 /* Fall through. */
2446 case UDIV:
2447 case UMOD:
2448 if (TUNE_MIPS3000
2449 || TUNE_MIPS3900)
2450 *total = COSTS_N_INSNS (35);
2451 else if (TUNE_MIPS6000)
2452 *total = COSTS_N_INSNS (38);
2453 else if (TUNE_MIPS5000)
2454 *total = COSTS_N_INSNS (36);
2455 else if (TUNE_SB1)
2456 *total = COSTS_N_INSNS ((mode == SImode) ? 36 : 68);
2457 else if (TUNE_MIPS5400 || TUNE_MIPS5500)
2458 *total = COSTS_N_INSNS ((mode == SImode) ? 42 : 74);
2459 else
2460 *total = COSTS_N_INSNS (69);
2461 return true;
2463 case SIGN_EXTEND:
2464 /* A sign extend from SImode to DImode in 64 bit mode is often
2465 zero instructions, because the result can often be used
2466 directly by another instruction; we'll call it one. */
2467 if (TARGET_64BIT && mode == DImode
2468 && GET_MODE (XEXP (x, 0)) == SImode)
2469 *total = COSTS_N_INSNS (1);
2470 else
2471 *total = COSTS_N_INSNS (2);
2472 return true;
2474 case ZERO_EXTEND:
2475 if (TARGET_64BIT && mode == DImode
2476 && GET_MODE (XEXP (x, 0)) == SImode)
2477 *total = COSTS_N_INSNS (2);
2478 else
2479 *total = COSTS_N_INSNS (1);
2480 return true;
2482 default:
2483 return false;
2487 /* Provide the costs of an addressing mode that contains ADDR.
2488 If ADDR is not a valid address, its cost is irrelevant. */
2490 static int
2491 mips_address_cost (rtx addr)
2493 return mips_address_insns (addr, SImode);
2496 /* Return one word of double-word value OP, taking into account the fixed
2497 endianness of certain registers. HIGH_P is true to select the high part,
2498 false to select the low part. */
2501 mips_subword (rtx op, int high_p)
2503 unsigned int byte;
2504 enum machine_mode mode;
2506 mode = GET_MODE (op);
2507 if (mode == VOIDmode)
2508 mode = DImode;
2510 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
2511 byte = UNITS_PER_WORD;
2512 else
2513 byte = 0;
2515 if (GET_CODE (op) == REG)
2517 if (FP_REG_P (REGNO (op)))
2518 return gen_rtx_REG (word_mode, high_p ? REGNO (op) + 1 : REGNO (op));
2519 if (REGNO (op) == HI_REGNUM)
2520 return gen_rtx_REG (word_mode, high_p ? HI_REGNUM : LO_REGNUM);
2523 if (GET_CODE (op) == MEM)
2524 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2526 return simplify_gen_subreg (word_mode, op, mode, byte);
2530 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
2532 bool
2533 mips_split_64bit_move_p (rtx dest, rtx src)
2535 if (TARGET_64BIT)
2536 return false;
2538 /* FP->FP moves can be done in a single instruction. */
2539 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2540 return false;
2542 /* Check for floating-point loads and stores. They can be done using
2543 ldc1 and sdc1 on MIPS II and above. */
2544 if (mips_isa > 1)
2546 if (FP_REG_RTX_P (dest) && GET_CODE (src) == MEM)
2547 return false;
2548 if (FP_REG_RTX_P (src) && GET_CODE (dest) == MEM)
2549 return false;
2551 return true;
2555 /* Split a 64-bit move from SRC to DEST assuming that
2556 mips_split_64bit_move_p holds.
2558 Moves into and out of FPRs cause some difficulty here. Such moves
2559 will always be DFmode, since paired FPRs are not allowed to store
2560 DImode values. The most natural representation would be two separate
2561 32-bit moves, such as:
2563 (set (reg:SI $f0) (mem:SI ...))
2564 (set (reg:SI $f1) (mem:SI ...))
2566 However, the second insn is invalid because odd-numbered FPRs are
2567 not allowed to store independent values. Use the patterns load_df_low,
2568 load_df_high and store_df_high instead. */
2570 void
2571 mips_split_64bit_move (rtx dest, rtx src)
2573 if (FP_REG_RTX_P (dest))
2575 /* Loading an FPR from memory or from GPRs. */
2576 emit_insn (gen_load_df_low (copy_rtx (dest), mips_subword (src, 0)));
2577 emit_insn (gen_load_df_high (dest, mips_subword (src, 1),
2578 copy_rtx (dest)));
2580 else if (FP_REG_RTX_P (src))
2582 /* Storing an FPR into memory or GPRs. */
2583 emit_move_insn (mips_subword (dest, 0), mips_subword (src, 0));
2584 emit_insn (gen_store_df_high (mips_subword (dest, 1), src));
2586 else
2588 /* The operation can be split into two normal moves. Decide in
2589 which order to do them. */
2590 rtx low_dest;
2592 low_dest = mips_subword (dest, 0);
2593 if (GET_CODE (low_dest) == REG
2594 && reg_overlap_mentioned_p (low_dest, src))
2596 emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2597 emit_move_insn (low_dest, mips_subword (src, 0));
2599 else
2601 emit_move_insn (low_dest, mips_subword (src, 0));
2602 emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2607 /* Return the appropriate instructions to move SRC into DEST. Assume
2608 that SRC is operand 1 and DEST is operand 0. */
2610 const char *
2611 mips_output_move (rtx dest, rtx src)
2613 enum rtx_code dest_code, src_code;
2614 bool dbl_p;
2616 dest_code = GET_CODE (dest);
2617 src_code = GET_CODE (src);
2618 dbl_p = (GET_MODE_SIZE (GET_MODE (dest)) == 8);
2620 if (dbl_p && mips_split_64bit_move_p (dest, src))
2621 return "#";
2623 if ((src_code == REG && GP_REG_P (REGNO (src)))
2624 || (!TARGET_MIPS16 && src == CONST0_RTX (GET_MODE (dest))))
2626 if (dest_code == REG)
2628 if (GP_REG_P (REGNO (dest)))
2629 return "move\t%0,%z1";
2631 if (MD_REG_P (REGNO (dest)))
2632 return "mt%0\t%z1";
2634 if (FP_REG_P (REGNO (dest)))
2635 return (dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0");
2637 if (ALL_COP_REG_P (REGNO (dest)))
2639 static char retval[] = "dmtc_\t%z1,%0";
2641 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2642 return (dbl_p ? retval : retval + 1);
2645 if (dest_code == MEM)
2646 return (dbl_p ? "sd\t%z1,%0" : "sw\t%z1,%0");
2648 if (dest_code == REG && GP_REG_P (REGNO (dest)))
2650 if (src_code == REG)
2652 if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
2653 return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
2655 if (FP_REG_P (REGNO (src)))
2656 return (dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1");
2658 if (ALL_COP_REG_P (REGNO (src)))
2660 static char retval[] = "dmfc_\t%0,%1";
2662 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2663 return (dbl_p ? retval : retval + 1);
2667 if (src_code == MEM)
2668 return (dbl_p ? "ld\t%0,%1" : "lw\t%0,%1");
2670 if (src_code == CONST_INT)
2672 /* Don't use the X format, because that will give out of
2673 range numbers for 64 bit hosts and 32 bit targets. */
2674 if (!TARGET_MIPS16)
2675 return "li\t%0,%1\t\t\t# %X1";
2677 if (INTVAL (src) >= 0 && INTVAL (src) <= 0xffff)
2678 return "li\t%0,%1";
2680 if (INTVAL (src) < 0 && INTVAL (src) >= -0xffff)
2681 return "li\t%0,%n1\n\tneg\t%0";
2684 if (src_code == HIGH)
2685 return "lui\t%0,%h1";
2687 if (CONST_GP_P (src))
2688 return "move\t%0,%1";
2690 if (symbolic_operand (src, VOIDmode))
2691 return (dbl_p ? "dla\t%0,%1" : "la\t%0,%1");
2693 if (src_code == REG && FP_REG_P (REGNO (src)))
2695 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2696 return (dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1");
2698 if (dest_code == MEM)
2699 return (dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0");
2701 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2703 if (src_code == MEM)
2704 return (dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1");
2706 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
2708 static char retval[] = "l_c_\t%0,%1";
2710 retval[1] = (dbl_p ? 'd' : 'w');
2711 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2712 return retval;
2714 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
2716 static char retval[] = "s_c_\t%1,%0";
2718 retval[1] = (dbl_p ? 'd' : 'w');
2719 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2720 return retval;
2722 abort ();
2725 /* Return an rtx for the gp save slot. Valid only when using o32 or
2726 o64 abicalls. */
2729 mips_gp_save_slot (void)
2731 rtx loc;
2733 if (!TARGET_ABICALLS || TARGET_NEWABI)
2734 abort ();
2736 if (frame_pointer_needed)
2737 loc = hard_frame_pointer_rtx;
2738 else
2739 loc = stack_pointer_rtx;
2740 loc = plus_constant (loc, current_function_outgoing_args_size);
2741 loc = gen_rtx_MEM (Pmode, loc);
2742 RTX_UNCHANGING_P (loc) = 1;
2743 return loc;
2746 /* Make normal rtx_code into something we can index from an array */
2748 static enum internal_test
2749 map_test_to_internal_test (enum rtx_code test_code)
2751 enum internal_test test = ITEST_MAX;
2753 switch (test_code)
2755 case EQ: test = ITEST_EQ; break;
2756 case NE: test = ITEST_NE; break;
2757 case GT: test = ITEST_GT; break;
2758 case GE: test = ITEST_GE; break;
2759 case LT: test = ITEST_LT; break;
2760 case LE: test = ITEST_LE; break;
2761 case GTU: test = ITEST_GTU; break;
2762 case GEU: test = ITEST_GEU; break;
2763 case LTU: test = ITEST_LTU; break;
2764 case LEU: test = ITEST_LEU; break;
2765 default: break;
2768 return test;
2772 /* Generate the code to compare two integer values. The return value is:
2773 (reg:SI xx) The pseudo register the comparison is in
2774 0 No register, generate a simple branch.
2776 ??? This is called with result nonzero by the Scond patterns in
2777 mips.md. These patterns are called with a target in the mode of
2778 the Scond instruction pattern. Since this must be a constant, we
2779 must use SImode. This means that if RESULT is nonzero, it will
2780 always be an SImode register, even if TARGET_64BIT is true. We
2781 cope with this by calling convert_move rather than emit_move_insn.
2782 This will sometimes lead to an unnecessary extension of the result;
2783 for example:
2785 long long
2786 foo (long long i)
2788 return i < 5;
2791 TEST_CODE is the rtx code for the comparison.
2792 CMP0 and CMP1 are the two operands to compare.
2793 RESULT is the register in which the result should be stored (null for
2794 branches).
2795 For branches, P_INVERT points to an integer that is nonzero on return
2796 if the branch should be inverted. */
2799 gen_int_relational (enum rtx_code test_code, rtx result, rtx cmp0,
2800 rtx cmp1, int *p_invert)
2802 struct cmp_info
2804 enum rtx_code test_code; /* code to use in instruction (LT vs. LTU) */
2805 int const_low; /* low bound of constant we can accept */
2806 int const_high; /* high bound of constant we can accept */
2807 int const_add; /* constant to add (convert LE -> LT) */
2808 int reverse_regs; /* reverse registers in test */
2809 int invert_const; /* != 0 if invert value if cmp1 is constant */
2810 int invert_reg; /* != 0 if invert value if cmp1 is register */
2811 int unsignedp; /* != 0 for unsigned comparisons. */
2814 static const struct cmp_info info[ (int)ITEST_MAX ] = {
2816 { XOR, 0, 65535, 0, 0, 0, 0, 0 }, /* EQ */
2817 { XOR, 0, 65535, 0, 0, 1, 1, 0 }, /* NE */
2818 { LT, -32769, 32766, 1, 1, 1, 0, 0 }, /* GT */
2819 { LT, -32768, 32767, 0, 0, 1, 1, 0 }, /* GE */
2820 { LT, -32768, 32767, 0, 0, 0, 0, 0 }, /* LT */
2821 { LT, -32769, 32766, 1, 1, 0, 1, 0 }, /* LE */
2822 { LTU, -32769, 32766, 1, 1, 1, 0, 1 }, /* GTU */
2823 { LTU, -32768, 32767, 0, 0, 1, 1, 1 }, /* GEU */
2824 { LTU, -32768, 32767, 0, 0, 0, 0, 1 }, /* LTU */
2825 { LTU, -32769, 32766, 1, 1, 0, 1, 1 }, /* LEU */
2828 enum internal_test test;
2829 enum machine_mode mode;
2830 const struct cmp_info *p_info;
2831 int branch_p;
2832 int eqne_p;
2833 int invert;
2834 rtx reg;
2835 rtx reg2;
2837 test = map_test_to_internal_test (test_code);
2838 if (test == ITEST_MAX)
2839 abort ();
2841 p_info = &info[(int) test];
2842 eqne_p = (p_info->test_code == XOR);
2844 mode = GET_MODE (cmp0);
2845 if (mode == VOIDmode)
2846 mode = GET_MODE (cmp1);
2848 /* Eliminate simple branches. */
2849 branch_p = (result == 0);
2850 if (branch_p)
2852 if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
2854 /* Comparisons against zero are simple branches. */
2855 if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
2856 && (! TARGET_MIPS16 || eqne_p))
2857 return 0;
2859 /* Test for beq/bne. */
2860 if (eqne_p && ! TARGET_MIPS16)
2861 return 0;
2864 /* Allocate a pseudo to calculate the value in. */
2865 result = gen_reg_rtx (mode);
2868 /* Make sure we can handle any constants given to us. */
2869 if (GET_CODE (cmp0) == CONST_INT)
2870 cmp0 = force_reg (mode, cmp0);
2872 if (GET_CODE (cmp1) == CONST_INT)
2874 HOST_WIDE_INT value = INTVAL (cmp1);
2876 if (value < p_info->const_low
2877 || value > p_info->const_high
2878 /* ??? Why? And why wasn't the similar code below modified too? */
2879 || (TARGET_64BIT
2880 && HOST_BITS_PER_WIDE_INT < 64
2881 && p_info->const_add != 0
2882 && ((p_info->unsignedp
2883 ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
2884 > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
2885 : (value + p_info->const_add) > INTVAL (cmp1))
2886 != (p_info->const_add > 0))))
2887 cmp1 = force_reg (mode, cmp1);
2890 /* See if we need to invert the result. */
2891 invert = (GET_CODE (cmp1) == CONST_INT
2892 ? p_info->invert_const : p_info->invert_reg);
2894 if (p_invert != (int *)0)
2896 *p_invert = invert;
2897 invert = 0;
2900 /* Comparison to constants, may involve adding 1 to change a LT into LE.
2901 Comparison between two registers, may involve switching operands. */
2902 if (GET_CODE (cmp1) == CONST_INT)
2904 if (p_info->const_add != 0)
2906 HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
2908 /* If modification of cmp1 caused overflow,
2909 we would get the wrong answer if we follow the usual path;
2910 thus, x > 0xffffffffU would turn into x > 0U. */
2911 if ((p_info->unsignedp
2912 ? (unsigned HOST_WIDE_INT) new >
2913 (unsigned HOST_WIDE_INT) INTVAL (cmp1)
2914 : new > INTVAL (cmp1))
2915 != (p_info->const_add > 0))
2917 /* This test is always true, but if INVERT is true then
2918 the result of the test needs to be inverted so 0 should
2919 be returned instead. */
2920 emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
2921 return result;
2923 else
2924 cmp1 = GEN_INT (new);
2928 else if (p_info->reverse_regs)
2930 rtx temp = cmp0;
2931 cmp0 = cmp1;
2932 cmp1 = temp;
2935 if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
2936 reg = cmp0;
2937 else
2939 reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
2940 convert_move (reg, gen_rtx_fmt_ee (p_info->test_code,
2941 mode, cmp0, cmp1), 0);
2944 if (test == ITEST_NE)
2946 if (! TARGET_MIPS16)
2948 convert_move (result, gen_rtx_GTU (mode, reg, const0_rtx), 0);
2949 if (p_invert != NULL)
2950 *p_invert = 0;
2951 invert = 0;
2953 else
2955 reg2 = invert ? gen_reg_rtx (mode) : result;
2956 convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
2957 reg = reg2;
2961 else if (test == ITEST_EQ)
2963 reg2 = invert ? gen_reg_rtx (mode) : result;
2964 convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
2965 reg = reg2;
2968 if (invert)
2970 rtx one;
2972 if (! TARGET_MIPS16)
2973 one = const1_rtx;
2974 else
2976 /* The value is in $24. Copy it to another register, so
2977 that reload doesn't think it needs to store the $24 and
2978 the input to the XOR in the same location. */
2979 reg2 = gen_reg_rtx (mode);
2980 emit_move_insn (reg2, reg);
2981 reg = reg2;
2982 one = force_reg (mode, const1_rtx);
2984 convert_move (result, gen_rtx_XOR (mode, reg, one), 0);
2987 return result;
2990 /* Work out how to check a floating-point condition. We need a
2991 separate comparison instruction (C.cond.fmt), followed by a
2992 branch or conditional move. Given that IN_CODE is the
2993 required condition, set *CMP_CODE to the C.cond.fmt code
2994 and *action_code to the branch or move code. */
2996 static void
2997 get_float_compare_codes (enum rtx_code in_code, enum rtx_code *cmp_code,
2998 enum rtx_code *action_code)
3000 switch (in_code)
3002 case NE:
3003 case UNGE:
3004 case UNGT:
3005 case LTGT:
3006 case ORDERED:
3007 *cmp_code = reverse_condition_maybe_unordered (in_code);
3008 *action_code = EQ;
3009 break;
3011 default:
3012 *cmp_code = in_code;
3013 *action_code = NE;
3014 break;
3018 /* Emit the common code for doing conditional branches.
3019 operand[0] is the label to jump to.
3020 The comparison operands are saved away by cmp{si,di,sf,df}. */
3022 void
3023 gen_conditional_branch (rtx *operands, enum rtx_code test_code)
3025 enum cmp_type type = branch_type;
3026 rtx cmp0 = branch_cmp[0];
3027 rtx cmp1 = branch_cmp[1];
3028 enum machine_mode mode;
3029 enum rtx_code cmp_code;
3030 rtx reg;
3031 int invert;
3032 rtx label1, label2;
3034 switch (type)
3036 case CMP_SI:
3037 case CMP_DI:
3038 mode = type == CMP_SI ? SImode : DImode;
3039 invert = 0;
3040 reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3042 if (reg)
3044 cmp0 = reg;
3045 cmp1 = const0_rtx;
3046 test_code = NE;
3048 else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3049 /* We don't want to build a comparison against a nonzero
3050 constant. */
3051 cmp1 = force_reg (mode, cmp1);
3053 break;
3055 case CMP_SF:
3056 case CMP_DF:
3057 if (! ISA_HAS_8CC)
3058 reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3059 else
3060 reg = gen_reg_rtx (CCmode);
3062 get_float_compare_codes (test_code, &cmp_code, &test_code);
3063 emit_insn (gen_rtx_SET (VOIDmode, reg,
3064 gen_rtx_fmt_ee (cmp_code, CCmode, cmp0, cmp1)));
3066 mode = CCmode;
3067 cmp0 = reg;
3068 cmp1 = const0_rtx;
3069 invert = 0;
3070 break;
3072 default:
3073 fatal_insn ("bad test",
3074 gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
3077 /* Generate the branch. */
3079 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3080 label2 = pc_rtx;
3082 if (invert)
3084 label2 = label1;
3085 label1 = pc_rtx;
3088 emit_jump_insn
3089 (gen_rtx_SET (VOIDmode, pc_rtx,
3090 gen_rtx_IF_THEN_ELSE (VOIDmode,
3091 gen_rtx_fmt_ee (test_code, mode,
3092 cmp0, cmp1),
3093 label1, label2)));
3096 /* Emit the common code for conditional moves. OPERANDS is the array
3097 of operands passed to the conditional move define_expand. */
3099 void
3100 gen_conditional_move (rtx *operands)
3102 rtx op0 = branch_cmp[0];
3103 rtx op1 = branch_cmp[1];
3104 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3105 enum rtx_code cmp_code = GET_CODE (operands[1]);
3106 enum rtx_code move_code = NE;
3107 enum machine_mode op_mode = GET_MODE (operands[0]);
3108 enum machine_mode cmp_mode;
3109 rtx cmp_reg;
3111 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3113 switch (cmp_code)
3115 case EQ:
3116 cmp_code = XOR;
3117 move_code = EQ;
3118 break;
3119 case NE:
3120 cmp_code = XOR;
3121 break;
3122 case LT:
3123 break;
3124 case GE:
3125 cmp_code = LT;
3126 move_code = EQ;
3127 break;
3128 case GT:
3129 cmp_code = LT;
3130 op0 = force_reg (mode, branch_cmp[1]);
3131 op1 = branch_cmp[0];
3132 break;
3133 case LE:
3134 cmp_code = LT;
3135 op0 = force_reg (mode, branch_cmp[1]);
3136 op1 = branch_cmp[0];
3137 move_code = EQ;
3138 break;
3139 case LTU:
3140 break;
3141 case GEU:
3142 cmp_code = LTU;
3143 move_code = EQ;
3144 break;
3145 case GTU:
3146 cmp_code = LTU;
3147 op0 = force_reg (mode, branch_cmp[1]);
3148 op1 = branch_cmp[0];
3149 break;
3150 case LEU:
3151 cmp_code = LTU;
3152 op0 = force_reg (mode, branch_cmp[1]);
3153 op1 = branch_cmp[0];
3154 move_code = EQ;
3155 break;
3156 default:
3157 abort ();
3160 else
3161 get_float_compare_codes (cmp_code, &cmp_code, &move_code);
3163 if (mode == SImode || mode == DImode)
3164 cmp_mode = mode;
3165 else if (mode == SFmode || mode == DFmode)
3166 cmp_mode = CCmode;
3167 else
3168 abort ();
3170 cmp_reg = gen_reg_rtx (cmp_mode);
3171 emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3172 gen_rtx_fmt_ee (cmp_code, cmp_mode, op0, op1)));
3174 emit_insn (gen_rtx_SET (op_mode, operands[0],
3175 gen_rtx_IF_THEN_ELSE (op_mode,
3176 gen_rtx_fmt_ee (move_code,
3177 VOIDmode,
3178 cmp_reg,
3179 const0_rtx),
3180 operands[2], operands[3])));
3183 /* Emit a conditional trap. OPERANDS is the array of operands passed to
3184 the conditional_trap expander. */
3186 void
3187 mips_gen_conditional_trap (rtx *operands)
3189 rtx op0, op1;
3190 enum rtx_code cmp_code = GET_CODE (operands[0]);
3191 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3193 /* MIPS conditional trap machine instructions don't have GT or LE
3194 flavors, so we must invert the comparison and convert to LT and
3195 GE, respectively. */
3196 switch (cmp_code)
3198 case GT: cmp_code = LT; break;
3199 case LE: cmp_code = GE; break;
3200 case GTU: cmp_code = LTU; break;
3201 case LEU: cmp_code = GEU; break;
3202 default: break;
3204 if (cmp_code == GET_CODE (operands[0]))
3206 op0 = force_reg (mode, branch_cmp[0]);
3207 op1 = branch_cmp[1];
3209 else
3211 op0 = force_reg (mode, branch_cmp[1]);
3212 op1 = branch_cmp[0];
3214 if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3215 op1 = force_reg (mode, op1);
3217 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3218 gen_rtx_fmt_ee (cmp_code, GET_MODE (operands[0]),
3219 op0, op1),
3220 operands[1]));
3223 /* Load function address ADDR into register DEST. SIBCALL_P is true
3224 if the address is needed for a sibling call. */
3226 static void
3227 mips_load_call_address (rtx dest, rtx addr, int sibcall_p)
3229 /* If we're generating PIC, and this call is to a global function,
3230 try to allow its address to be resolved lazily. This isn't
3231 possible for NewABI sibcalls since the value of $gp on entry
3232 to the stub would be our caller's gp, not ours. */
3233 if (TARGET_EXPLICIT_RELOCS
3234 && !(sibcall_p && TARGET_NEWABI)
3235 && global_got_operand (addr, VOIDmode))
3237 rtx high, lo_sum_symbol;
3239 high = mips_unspec_offset_high (dest, pic_offset_table_rtx,
3240 addr, SYMBOL_GOTOFF_CALL);
3241 lo_sum_symbol = mips_unspec_address (addr, SYMBOL_GOTOFF_CALL);
3242 if (Pmode == SImode)
3243 emit_insn (gen_load_callsi (dest, high, lo_sum_symbol));
3244 else
3245 emit_insn (gen_load_calldi (dest, high, lo_sum_symbol));
3247 else
3248 emit_move_insn (dest, addr);
3252 /* Expand a call or call_value instruction. RESULT is where the
3253 result will go (null for calls), ADDR is the address of the
3254 function, ARGS_SIZE is the size of the arguments and AUX is
3255 the value passed to us by mips_function_arg. SIBCALL_P is true
3256 if we are expanding a sibling call, false if we're expanding
3257 a normal call. */
3259 void
3260 mips_expand_call (rtx result, rtx addr, rtx args_size, rtx aux, int sibcall_p)
3262 rtx orig_addr, pattern, insn;
3264 orig_addr = addr;
3265 if (!call_insn_operand (addr, VOIDmode))
3267 addr = gen_reg_rtx (Pmode);
3268 mips_load_call_address (addr, orig_addr, sibcall_p);
3271 if (TARGET_MIPS16
3272 && mips16_hard_float
3273 && build_mips16_call_stub (result, addr, args_size,
3274 aux == 0 ? 0 : (int) GET_MODE (aux)))
3275 return;
3277 if (result == 0)
3278 pattern = (sibcall_p
3279 ? gen_sibcall_internal (addr, args_size)
3280 : gen_call_internal (addr, args_size));
3281 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
3283 rtx reg1, reg2;
3285 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
3286 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
3287 pattern =
3288 (sibcall_p
3289 ? gen_sibcall_value_multiple_internal (reg1, addr, args_size, reg2)
3290 : gen_call_value_multiple_internal (reg1, addr, args_size, reg2));
3292 else
3293 pattern = (sibcall_p
3294 ? gen_sibcall_value_internal (result, addr, args_size)
3295 : gen_call_value_internal (result, addr, args_size));
3297 insn = emit_call_insn (pattern);
3299 /* Lazy-binding stubs require $gp to be valid on entry. */
3300 if (global_got_operand (orig_addr, VOIDmode))
3301 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3305 /* We can handle any sibcall when TARGET_SIBCALLS is true. */
3307 static bool
3308 mips_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
3309 tree exp ATTRIBUTE_UNUSED)
3311 return TARGET_SIBCALLS;
3314 /* Return true if operand OP is a condition code register.
3315 Only for use during or after reload. */
3318 fcc_register_operand (rtx op, enum machine_mode mode)
3320 return ((mode == VOIDmode || mode == GET_MODE (op))
3321 && (reload_in_progress || reload_completed)
3322 && (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
3323 && ST_REG_P (true_regnum (op)));
3326 /* Emit code to move general operand SRC into condition-code
3327 register DEST. SCRATCH is a scratch TFmode float register.
3328 The sequence is:
3330 FP1 = SRC
3331 FP2 = 0.0f
3332 DEST = FP2 < FP1
3334 where FP1 and FP2 are single-precision float registers
3335 taken from SCRATCH. */
3337 void
3338 mips_emit_fcc_reload (rtx dest, rtx src, rtx scratch)
3340 rtx fp1, fp2;
3342 /* Change the source to SFmode. */
3343 if (GET_CODE (src) == MEM)
3344 src = adjust_address (src, SFmode, 0);
3345 else if (GET_CODE (src) == REG || GET_CODE (src) == SUBREG)
3346 src = gen_rtx_REG (SFmode, true_regnum (src));
3348 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3349 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3351 emit_move_insn (copy_rtx (fp1), src);
3352 emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3353 emit_insn (gen_slt_sf (dest, fp2, fp1));
3356 /* Emit code to change the current function's return address to
3357 ADDRESS. SCRATCH is available as a scratch register, if needed.
3358 ADDRESS and SCRATCH are both word-mode GPRs. */
3360 void
3361 mips_set_return_address (rtx address, rtx scratch)
3363 HOST_WIDE_INT gp_offset;
3365 compute_frame_size (get_frame_size ());
3366 if (((cfun->machine->frame.mask >> 31) & 1) == 0)
3367 abort ();
3368 gp_offset = cfun->machine->frame.gp_sp_offset;
3370 /* Reduce SP + GP_OFSET to a legitimate address and put it in SCRATCH. */
3371 if (gp_offset < 32768)
3372 scratch = plus_constant (stack_pointer_rtx, gp_offset);
3373 else
3375 emit_move_insn (scratch, GEN_INT (gp_offset));
3376 if (Pmode == DImode)
3377 emit_insn (gen_adddi3 (scratch, scratch, stack_pointer_rtx));
3378 else
3379 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
3382 emit_move_insn (gen_rtx_MEM (GET_MODE (address), scratch), address);
3385 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
3386 Assume that the areas do not overlap. */
3388 static void
3389 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
3391 HOST_WIDE_INT offset, delta;
3392 unsigned HOST_WIDE_INT bits;
3393 int i;
3394 enum machine_mode mode;
3395 rtx *regs;
3397 /* Work out how many bits to move at a time. If both operands have
3398 half-word alignment, it is usually better to move in half words.
3399 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
3400 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
3401 Otherwise move word-sized chunks. */
3402 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
3403 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
3404 bits = BITS_PER_WORD / 2;
3405 else
3406 bits = BITS_PER_WORD;
3408 mode = mode_for_size (bits, MODE_INT, 0);
3409 delta = bits / BITS_PER_UNIT;
3411 /* Allocate a buffer for the temporary registers. */
3412 regs = alloca (sizeof (rtx) * length / delta);
3414 /* Load as many BITS-sized chunks as possible. Use a normal load if
3415 the source has enough alignment, otherwise use left/right pairs. */
3416 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3418 rtx part;
3420 regs[i] = gen_reg_rtx (mode);
3421 part = adjust_address (src, mode, offset);
3422 if (MEM_ALIGN (part) >= bits)
3423 emit_move_insn (regs[i], part);
3424 else if (!mips_expand_unaligned_load (regs[i], part, bits, 0))
3425 abort ();
3428 /* Copy the chunks to the destination. */
3429 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3431 rtx part;
3433 part = adjust_address (dest, mode, offset);
3434 if (MEM_ALIGN (part) >= bits)
3435 emit_move_insn (part, regs[i]);
3436 else if (!mips_expand_unaligned_store (part, regs[i], bits, 0))
3437 abort ();
3440 /* Mop up any left-over bytes. */
3441 if (offset < length)
3443 src = adjust_address (src, mode, offset);
3444 dest = adjust_address (dest, mode, offset);
3445 move_by_pieces (dest, src, length - offset,
3446 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
3450 #define MAX_MOVE_REGS 4
3451 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3454 /* Helper function for doing a loop-based block operation on memory
3455 reference MEM. Each iteration of the loop will operate on LENGTH
3456 bytes of MEM.
3458 Create a new base register for use within the loop and point it to
3459 the start of MEM. Create a new memory reference that uses this
3460 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
3462 static void
3463 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
3464 rtx *loop_reg, rtx *loop_mem)
3466 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
3468 /* Although the new mem does not refer to a known location,
3469 it does keep up to LENGTH bytes of alignment. */
3470 *loop_mem = change_address (mem, BLKmode, *loop_reg);
3471 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
3475 /* Move LENGTH bytes from SRC to DEST using a loop that moves MAX_MOVE_BYTES
3476 per iteration. LENGTH must be at least MAX_MOVE_BYTES. Assume that the
3477 memory regions do not overlap. */
3479 static void
3480 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length)
3482 rtx label, src_reg, dest_reg, final_src;
3483 HOST_WIDE_INT leftover;
3485 leftover = length % MAX_MOVE_BYTES;
3486 length -= leftover;
3488 /* Create registers and memory references for use within the loop. */
3489 mips_adjust_block_mem (src, MAX_MOVE_BYTES, &src_reg, &src);
3490 mips_adjust_block_mem (dest, MAX_MOVE_BYTES, &dest_reg, &dest);
3492 /* Calculate the value that SRC_REG should have after the last iteration
3493 of the loop. */
3494 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
3495 0, 0, OPTAB_WIDEN);
3497 /* Emit the start of the loop. */
3498 label = gen_label_rtx ();
3499 emit_label (label);
3501 /* Emit the loop body. */
3502 mips_block_move_straight (dest, src, MAX_MOVE_BYTES);
3504 /* Move on to the next block. */
3505 emit_move_insn (src_reg, plus_constant (src_reg, MAX_MOVE_BYTES));
3506 emit_move_insn (dest_reg, plus_constant (dest_reg, MAX_MOVE_BYTES));
3508 /* Emit the loop condition. */
3509 if (Pmode == DImode)
3510 emit_insn (gen_cmpdi (src_reg, final_src));
3511 else
3512 emit_insn (gen_cmpsi (src_reg, final_src));
3513 emit_jump_insn (gen_bne (label));
3515 /* Mop up any left-over bytes. */
3516 if (leftover)
3517 mips_block_move_straight (dest, src, leftover);
3520 /* Expand a movstrsi instruction. */
3522 bool
3523 mips_expand_block_move (rtx dest, rtx src, rtx length)
3525 if (GET_CODE (length) == CONST_INT)
3527 if (INTVAL (length) <= 2 * MAX_MOVE_BYTES)
3529 mips_block_move_straight (dest, src, INTVAL (length));
3530 return true;
3532 else if (optimize)
3534 mips_block_move_loop (dest, src, INTVAL (length));
3535 return true;
3538 return false;
3541 /* Argument support functions. */
3543 /* Initialize CUMULATIVE_ARGS for a function. */
3545 void
3546 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
3547 rtx libname ATTRIBUTE_UNUSED)
3549 static CUMULATIVE_ARGS zero_cum;
3550 tree param, next_param;
3552 *cum = zero_cum;
3553 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3555 /* Determine if this function has variable arguments. This is
3556 indicated by the last argument being 'void_type_mode' if there
3557 are no variable arguments. The standard MIPS calling sequence
3558 passes all arguments in the general purpose registers in this case. */
3560 for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3561 param != 0; param = next_param)
3563 next_param = TREE_CHAIN (param);
3564 if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3565 cum->gp_reg_found = 1;
3570 /* Fill INFO with information about a single argument. CUM is the
3571 cumulative state for earlier arguments. MODE is the mode of this
3572 argument and TYPE is its type (if known). NAMED is true if this
3573 is a named (fixed) argument rather than a variable one. */
3575 static void
3576 mips_arg_info (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3577 tree type, int named, struct mips_arg_info *info)
3579 bool even_reg_p;
3580 unsigned int num_words, max_regs;
3582 /* Decide whether this argument should go in a floating-point register,
3583 assuming one is free. Later code checks for availability. */
3585 info->fpr_p = (GET_MODE_CLASS (mode) == MODE_FLOAT
3586 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3588 if (info->fpr_p)
3589 switch (mips_abi)
3591 case ABI_32:
3592 case ABI_O64:
3593 info->fpr_p = (!cum->gp_reg_found
3594 && cum->arg_number < 2
3595 && (type == 0 || FLOAT_TYPE_P (type)));
3596 break;
3598 case ABI_N32:
3599 case ABI_64:
3600 info->fpr_p = (named && (type == 0 || FLOAT_TYPE_P (type)));
3601 break;
3604 /* Now decide whether the argument must go in an even-numbered register. */
3606 even_reg_p = false;
3607 if (info->fpr_p)
3609 /* Under the O64 ABI, the second float argument goes in $f13 if it
3610 is a double, but $f14 if it is a single. Otherwise, on a
3611 32-bit double-float machine, each FP argument must start in a
3612 new register pair. */
3613 even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_HWFPVALUE
3614 || (mips_abi == ABI_O64 && mode == SFmode)
3615 || FP_INC > 1);
3617 else if (!TARGET_64BIT || LONG_DOUBLE_TYPE_SIZE == 128)
3619 if (GET_MODE_CLASS (mode) == MODE_INT
3620 || GET_MODE_CLASS (mode) == MODE_FLOAT)
3621 even_reg_p = (GET_MODE_SIZE (mode) > UNITS_PER_WORD);
3623 else if (type != NULL_TREE && TYPE_ALIGN (type) > BITS_PER_WORD)
3624 even_reg_p = true;
3627 if (mips_abi != ABI_EABI && MUST_PASS_IN_STACK (mode, type))
3628 /* This argument must be passed on the stack. Eat up all the
3629 remaining registers. */
3630 info->reg_offset = MAX_ARGS_IN_REGISTERS;
3631 else
3633 /* Set REG_OFFSET to the register count we're interested in.
3634 The EABI allocates the floating-point registers separately,
3635 but the other ABIs allocate them like integer registers. */
3636 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
3637 ? cum->num_fprs
3638 : cum->num_gprs);
3640 if (even_reg_p)
3641 info->reg_offset += info->reg_offset & 1;
3644 /* The alignment applied to registers is also applied to stack arguments. */
3645 info->stack_offset = cum->stack_words;
3646 if (even_reg_p)
3647 info->stack_offset += info->stack_offset & 1;
3649 if (mode == BLKmode)
3650 info->num_bytes = int_size_in_bytes (type);
3651 else
3652 info->num_bytes = GET_MODE_SIZE (mode);
3654 num_words = (info->num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3655 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
3657 /* Partition the argument between registers and stack. */
3658 info->reg_words = MIN (num_words, max_regs);
3659 info->stack_words = num_words - info->reg_words;
3663 /* Implement FUNCTION_ARG_ADVANCE. */
3665 void
3666 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3667 tree type, int named)
3669 struct mips_arg_info info;
3671 mips_arg_info (cum, mode, type, named, &info);
3673 if (!info.fpr_p)
3674 cum->gp_reg_found = true;
3676 /* See the comment above the cumulative args structure in mips.h
3677 for an explanation of what this code does. It assumes the O32
3678 ABI, which passes at most 2 arguments in float registers. */
3679 if (cum->arg_number < 2 && info.fpr_p)
3680 cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
3682 if (mips_abi != ABI_EABI || !info.fpr_p)
3683 cum->num_gprs = info.reg_offset + info.reg_words;
3684 else if (info.reg_words > 0)
3685 cum->num_fprs += FP_INC;
3687 if (info.stack_words > 0)
3688 cum->stack_words = info.stack_offset + info.stack_words;
3690 cum->arg_number++;
3693 /* Implement FUNCTION_ARG. */
3695 struct rtx_def *
3696 function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3697 tree type, int named)
3699 struct mips_arg_info info;
3701 /* We will be called with a mode of VOIDmode after the last argument
3702 has been seen. Whatever we return will be passed to the call
3703 insn. If we need a mips16 fp_code, return a REG with the code
3704 stored as the mode. */
3705 if (mode == VOIDmode)
3707 if (TARGET_MIPS16 && cum->fp_code != 0)
3708 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
3710 else
3711 return 0;
3714 mips_arg_info (cum, mode, type, named, &info);
3716 /* Return straight away if the whole argument is passed on the stack. */
3717 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
3718 return 0;
3720 if (type != 0
3721 && TREE_CODE (type) == RECORD_TYPE
3722 && TARGET_NEWABI
3723 && TYPE_SIZE_UNIT (type)
3724 && host_integerp (TYPE_SIZE_UNIT (type), 1)
3725 && named)
3727 /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
3728 structure contains a double in its entirety, then that 64 bit
3729 chunk is passed in a floating point register. */
3730 tree field;
3732 /* First check to see if there is any such field. */
3733 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3734 if (TREE_CODE (field) == FIELD_DECL
3735 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3736 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
3737 && host_integerp (bit_position (field), 0)
3738 && int_bit_position (field) % BITS_PER_WORD == 0)
3739 break;
3741 if (field != 0)
3743 /* Now handle the special case by returning a PARALLEL
3744 indicating where each 64 bit chunk goes. INFO.REG_WORDS
3745 chunks are passed in registers. */
3746 unsigned int i;
3747 HOST_WIDE_INT bitpos;
3748 rtx ret;
3750 /* assign_parms checks the mode of ENTRY_PARM, so we must
3751 use the actual mode here. */
3752 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
3754 bitpos = 0;
3755 field = TYPE_FIELDS (type);
3756 for (i = 0; i < info.reg_words; i++)
3758 rtx reg;
3760 for (; field; field = TREE_CHAIN (field))
3761 if (TREE_CODE (field) == FIELD_DECL
3762 && int_bit_position (field) >= bitpos)
3763 break;
3765 if (field
3766 && int_bit_position (field) == bitpos
3767 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3768 && !TARGET_SOFT_FLOAT
3769 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
3770 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
3771 else
3772 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
3774 XVECEXP (ret, 0, i)
3775 = gen_rtx_EXPR_LIST (VOIDmode, reg,
3776 GEN_INT (bitpos / BITS_PER_UNIT));
3778 bitpos += BITS_PER_WORD;
3780 return ret;
3784 if (info.fpr_p)
3785 return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
3786 else
3787 return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
3791 /* Implement FUNCTION_ARG_PARTIAL_NREGS. */
3794 function_arg_partial_nregs (const CUMULATIVE_ARGS *cum,
3795 enum machine_mode mode, tree type, int named)
3797 struct mips_arg_info info;
3799 mips_arg_info (cum, mode, type, named, &info);
3800 return info.stack_words > 0 ? info.reg_words : 0;
3804 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
3805 upward rather than downward. In other words, return true if the
3806 first byte of the stack slot has useful data, false if the last
3807 byte does. */
3809 bool
3810 mips_pad_arg_upward (enum machine_mode mode, tree type)
3812 /* On little-endian targets, the first byte of every stack argument
3813 is passed in the first byte of the stack slot. */
3814 if (!BYTES_BIG_ENDIAN)
3815 return true;
3817 /* Otherwise, integral types are padded downward: the last byte of a
3818 stack argument is passed in the last byte of the stack slot. */
3819 if (type != 0
3820 ? INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
3821 : GET_MODE_CLASS (mode) == MODE_INT)
3822 return false;
3824 /* Big-endian o64 pads floating-point arguments downward. */
3825 if (mips_abi == ABI_O64)
3826 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3827 return false;
3829 /* Other types are padded upward for o32, o64, n32 and n64. */
3830 if (mips_abi != ABI_EABI)
3831 return true;
3833 /* Arguments smaller than a stack slot are padded downward. */
3834 if (mode != BLKmode)
3835 return (GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY);
3836 else
3837 return (int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT));
3841 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
3842 if the least significant byte of the register has useful data. Return
3843 the opposite if the most significant byte does. */
3845 bool
3846 mips_pad_reg_upward (enum machine_mode mode, tree type)
3848 /* No shifting is required for floating-point arguments. */
3849 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3850 return !BYTES_BIG_ENDIAN;
3852 /* Otherwise, apply the same padding to register arguments as we do
3853 to stack arguments. */
3854 return mips_pad_arg_upward (mode, type);
3857 static void
3858 mips_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3859 tree type, int *pretend_size, int no_rtl)
3861 CUMULATIVE_ARGS local_cum;
3862 int gp_saved, fp_saved;
3864 /* The caller has advanced CUM up to, but not beyond, the last named
3865 argument. Advance a local copy of CUM past the last "real" named
3866 argument, to find out how many registers are left over. */
3868 local_cum = *cum;
3869 FUNCTION_ARG_ADVANCE (local_cum, mode, type, 1);
3871 /* Found out how many registers we need to save. */
3872 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
3873 fp_saved = (EABI_FLOAT_VARARGS_P
3874 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
3875 : 0);
3877 if (!no_rtl)
3879 if (gp_saved > 0)
3881 rtx ptr, mem;
3883 ptr = virtual_incoming_args_rtx;
3884 switch (mips_abi)
3886 case ABI_32:
3887 case ABI_O64:
3888 ptr = plus_constant (ptr, local_cum.num_gprs * UNITS_PER_WORD);
3889 break;
3891 case ABI_EABI:
3892 ptr = plus_constant (ptr, -gp_saved * UNITS_PER_WORD);
3893 break;
3895 mem = gen_rtx_MEM (BLKmode, ptr);
3896 set_mem_alias_set (mem, get_varargs_alias_set ());
3898 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
3899 mem, gp_saved);
3901 if (fp_saved > 0)
3903 /* We can't use move_block_from_reg, because it will use
3904 the wrong mode. */
3905 enum machine_mode mode;
3906 int off, i;
3908 /* Set OFF to the offset from virtual_incoming_args_rtx of
3909 the first float register. The FP save area lies below
3910 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
3911 off = -gp_saved * UNITS_PER_WORD;
3912 off &= ~(UNITS_PER_FPVALUE - 1);
3913 off -= fp_saved * UNITS_PER_FPREG;
3915 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
3917 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS; i += FP_INC)
3919 rtx ptr, mem;
3921 ptr = plus_constant (virtual_incoming_args_rtx, off);
3922 mem = gen_rtx_MEM (mode, ptr);
3923 set_mem_alias_set (mem, get_varargs_alias_set ());
3924 emit_move_insn (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
3925 off += UNITS_PER_HWFPVALUE;
3929 if (TARGET_OLDABI)
3931 /* No need for pretend arguments: the register parameter area was
3932 allocated by the caller. */
3933 *pretend_size = 0;
3934 return;
3936 *pretend_size = (gp_saved * UNITS_PER_WORD) + (fp_saved * UNITS_PER_FPREG);
3939 /* Create the va_list data type.
3940 We keep 3 pointers, and two offsets.
3941 Two pointers are to the overflow area, which starts at the CFA.
3942 One of these is constant, for addressing into the GPR save area below it.
3943 The other is advanced up the stack through the overflow region.
3944 The third pointer is to the GPR save area. Since the FPR save area
3945 is just below it, we can address FPR slots off this pointer.
3946 We also keep two one-byte offsets, which are to be subtracted from the
3947 constant pointers to yield addresses in the GPR and FPR save areas.
3948 These are downcounted as float or non-float arguments are used,
3949 and when they get to zero, the argument must be obtained from the
3950 overflow region.
3951 If !EABI_FLOAT_VARARGS_P, then no FPR save area exists, and a single
3952 pointer is enough. It's started at the GPR save area, and is
3953 advanced, period.
3954 Note that the GPR save area is not constant size, due to optimization
3955 in the prologue. Hence, we can't use a design with two pointers
3956 and two offsets, although we could have designed this with two pointers
3957 and three offsets. */
3959 static tree
3960 mips_build_builtin_va_list (void)
3962 if (EABI_FLOAT_VARARGS_P)
3964 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
3965 tree array, index;
3967 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3969 f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
3970 ptr_type_node);
3971 f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
3972 ptr_type_node);
3973 f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
3974 ptr_type_node);
3975 f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
3976 unsigned_char_type_node);
3977 f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
3978 unsigned_char_type_node);
3979 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
3980 warn on every user file. */
3981 index = build_int_2 (GET_MODE_SIZE (ptr_mode) - 2 - 1, 0);
3982 array = build_array_type (unsigned_char_type_node,
3983 build_index_type (index));
3984 f_res = build_decl (FIELD_DECL, get_identifier ("__reserved"), array);
3986 DECL_FIELD_CONTEXT (f_ovfl) = record;
3987 DECL_FIELD_CONTEXT (f_gtop) = record;
3988 DECL_FIELD_CONTEXT (f_ftop) = record;
3989 DECL_FIELD_CONTEXT (f_goff) = record;
3990 DECL_FIELD_CONTEXT (f_foff) = record;
3991 DECL_FIELD_CONTEXT (f_res) = record;
3993 TYPE_FIELDS (record) = f_ovfl;
3994 TREE_CHAIN (f_ovfl) = f_gtop;
3995 TREE_CHAIN (f_gtop) = f_ftop;
3996 TREE_CHAIN (f_ftop) = f_goff;
3997 TREE_CHAIN (f_goff) = f_foff;
3998 TREE_CHAIN (f_foff) = f_res;
4000 layout_type (record);
4001 return record;
4003 else if (TARGET_IRIX && !TARGET_IRIX5)
4004 /* On IRIX 6, this type is 'char *'. */
4005 return build_pointer_type (char_type_node);
4006 else
4007 /* Otherwise, we use 'void *'. */
4008 return ptr_type_node;
4011 /* Implement va_start. */
4013 void
4014 mips_va_start (tree valist, rtx nextarg)
4016 const CUMULATIVE_ARGS *cum = &current_function_args_info;
4018 /* ARG_POINTER_REGNUM is initialized to STACK_POINTER_BOUNDARY, but
4019 since the stack is aligned for a pair of argument-passing slots,
4020 and the beginning of a variable argument list may be an odd slot,
4021 we have to decrease its alignment. */
4022 if (cfun && cfun->emit->regno_pointer_align)
4023 while (((current_function_pretend_args_size * BITS_PER_UNIT)
4024 & (REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) - 1)) != 0)
4025 REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) /= 2;
4027 if (mips_abi == ABI_EABI)
4029 int gpr_save_area_size;
4031 gpr_save_area_size
4032 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
4034 if (EABI_FLOAT_VARARGS_P)
4036 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4037 tree ovfl, gtop, ftop, goff, foff;
4038 tree t;
4039 int fpr_offset;
4040 int fpr_save_area_size;
4042 f_ovfl = TYPE_FIELDS (va_list_type_node);
4043 f_gtop = TREE_CHAIN (f_ovfl);
4044 f_ftop = TREE_CHAIN (f_gtop);
4045 f_goff = TREE_CHAIN (f_ftop);
4046 f_foff = TREE_CHAIN (f_goff);
4048 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
4049 NULL_TREE);
4050 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
4051 NULL_TREE);
4052 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
4053 NULL_TREE);
4054 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
4055 NULL_TREE);
4056 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
4057 NULL_TREE);
4059 /* Emit code to initialize OVFL, which points to the next varargs
4060 stack argument. CUM->STACK_WORDS gives the number of stack
4061 words used by named arguments. */
4062 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
4063 if (cum->stack_words > 0)
4064 t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
4065 build_int_2 (cum->stack_words * UNITS_PER_WORD, 0));
4066 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4067 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4069 /* Emit code to initialize GTOP, the top of the GPR save area. */
4070 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
4071 t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
4072 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4074 /* Emit code to initialize FTOP, the top of the FPR save area.
4075 This address is gpr_save_area_bytes below GTOP, rounded
4076 down to the next fp-aligned boundary. */
4077 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
4078 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
4079 fpr_offset &= ~(UNITS_PER_FPVALUE - 1);
4080 if (fpr_offset)
4081 t = build (PLUS_EXPR, TREE_TYPE (ftop), t,
4082 build_int_2 (-fpr_offset, -1));
4083 t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
4084 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4086 /* Emit code to initialize GOFF, the offset from GTOP of the
4087 next GPR argument. */
4088 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
4089 build_int_2 (gpr_save_area_size, 0));
4090 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4092 /* Likewise emit code to initialize FOFF, the offset from FTOP
4093 of the next FPR argument. */
4094 fpr_save_area_size
4095 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
4096 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
4097 build_int_2 (fpr_save_area_size, 0));
4098 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4100 else
4102 /* Everything is in the GPR save area, or in the overflow
4103 area which is contiguous with it. */
4104 nextarg = plus_constant (nextarg, -gpr_save_area_size);
4105 std_expand_builtin_va_start (valist, nextarg);
4108 else
4109 std_expand_builtin_va_start (valist, nextarg);
4112 /* Implement va_arg. */
4115 mips_va_arg (tree valist, tree type)
4117 HOST_WIDE_INT size, rsize;
4118 rtx addr_rtx;
4119 tree t;
4121 size = int_size_in_bytes (type);
4122 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4124 if (mips_abi == ABI_EABI)
4126 bool indirect;
4127 rtx r;
4129 indirect
4130 = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
4132 if (indirect)
4134 size = POINTER_SIZE / BITS_PER_UNIT;
4135 rsize = UNITS_PER_WORD;
4138 if (!EABI_FLOAT_VARARGS_P)
4140 /* Case of all args in a merged stack. No need to check bounds,
4141 just advance valist along the stack. */
4143 tree gpr = valist;
4144 if (!indirect
4145 && !TARGET_64BIT
4146 && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
4148 /* Align the pointer using: ap = (ap + align - 1) & -align,
4149 where align is 2 * UNITS_PER_WORD. */
4150 t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4151 build_int_2 (2 * UNITS_PER_WORD - 1, 0));
4152 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
4153 build_int_2 (-2 * UNITS_PER_WORD, -1));
4154 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4155 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4158 /* Emit code to set addr_rtx to the valist, and postincrement
4159 the valist by the size of the argument, rounded up to the
4160 next word. Account for padding on big-endian targets. */
4161 t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
4162 size_int (rsize));
4163 addr_rtx = expand_expr (t, 0, Pmode, EXPAND_NORMAL);
4164 if (BYTES_BIG_ENDIAN)
4165 addr_rtx = plus_constant (addr_rtx, rsize - size);
4167 /* Flush the POSTINCREMENT. */
4168 emit_queue();
4170 else
4172 /* Not a simple merged stack. */
4174 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4175 tree ovfl, top, off;
4176 rtx lab_over = NULL_RTX, lab_false;
4177 HOST_WIDE_INT osize;
4179 addr_rtx = gen_reg_rtx (Pmode);
4181 f_ovfl = TYPE_FIELDS (va_list_type_node);
4182 f_gtop = TREE_CHAIN (f_ovfl);
4183 f_ftop = TREE_CHAIN (f_gtop);
4184 f_goff = TREE_CHAIN (f_ftop);
4185 f_foff = TREE_CHAIN (f_goff);
4187 /* We maintain separate pointers and offsets for floating-point
4188 and integer arguments, but we need similar code in both cases.
4189 Let:
4191 TOP be the top of the register save area;
4192 OFF be the offset from TOP of the next register;
4193 ADDR_RTX be the address of the argument;
4194 RSIZE be the number of bytes used to store the argument
4195 when it's in the register save area;
4196 OSIZE be the number of bytes used to store it when it's
4197 in the stack overflow area; and
4198 PADDING be (BYTES_BIG_ENDIAN ? OSIZE - RSIZE : 0)
4200 The code we want is:
4202 1: off &= -rsize; // round down
4203 2: if (off != 0)
4204 3: {
4205 4: addr_rtx = top - off;
4206 5: off -= rsize;
4207 6: }
4208 7: else
4209 8: {
4210 9: ovfl += ((intptr_t) ovfl + osize - 1) & -osize;
4211 10: addr_rtx = ovfl + PADDING;
4212 11: ovfl += osize;
4213 14: }
4215 [1] and [9] can sometimes be optimized away. */
4217 lab_false = gen_label_rtx ();
4218 lab_over = gen_label_rtx ();
4220 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
4221 NULL_TREE);
4222 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
4223 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
4225 top = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
4226 NULL_TREE);
4227 off = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
4228 NULL_TREE);
4230 /* When floating-point registers are saved to the stack,
4231 each one will take up UNITS_PER_HWFPVALUE bytes, regardless
4232 of the float's precision. */
4233 rsize = UNITS_PER_HWFPVALUE;
4235 /* Overflow arguments are padded to UNITS_PER_WORD bytes
4236 (= PARM_BOUNDARY bits). This can be different from RSIZE
4237 in two cases:
4239 (1) On 32-bit targets when TYPE is a structure such as:
4241 struct s { float f; };
4243 Such structures are passed in paired FPRs, so RSIZE
4244 will be 8 bytes. However, the structure only takes
4245 up 4 bytes of memory, so OSIZE will only be 4.
4247 (2) In combinations such as -mgp64 -msingle-float
4248 -fshort-double. Doubles passed in registers
4249 will then take up 4 (UNITS_PER_HWFPVALUE) bytes,
4250 but those passed on the stack take up
4251 UNITS_PER_WORD bytes. */
4252 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
4254 else
4256 top = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
4257 NULL_TREE);
4258 off = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
4259 NULL_TREE);
4260 if (rsize > UNITS_PER_WORD)
4262 /* [1] Emit code for: off &= -rsize. */
4263 t = build (BIT_AND_EXPR, TREE_TYPE (off), off,
4264 build_int_2 (-rsize, -1));
4265 t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
4266 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4268 osize = rsize;
4271 /* [2] Emit code to branch if off == 0. */
4272 r = expand_expr (off, NULL_RTX, TYPE_MODE (TREE_TYPE (off)),
4273 EXPAND_NORMAL);
4274 emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
4275 1, lab_false);
4277 /* [4] Emit code for: addr_rtx = top - off. On big endian machines,
4278 the argument has RSIZE - SIZE bytes of leading padding. */
4279 t = build (MINUS_EXPR, TREE_TYPE (top), top, off);
4280 if (BYTES_BIG_ENDIAN && rsize > size)
4281 t = build (PLUS_EXPR, TREE_TYPE (t), t,
4282 build_int_2 (rsize - size, 0));
4283 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4284 if (r != addr_rtx)
4285 emit_move_insn (addr_rtx, r);
4287 /* [5] Emit code for: off -= rsize. */
4288 t = build (MINUS_EXPR, TREE_TYPE (off), off, build_int_2 (rsize, 0));
4289 t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
4290 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4292 /* [7] Emit code to jump over the else clause, then the label
4293 that starts it. */
4294 emit_queue();
4295 emit_jump (lab_over);
4296 emit_barrier ();
4297 emit_label (lab_false);
4299 if (osize > UNITS_PER_WORD)
4301 /* [9] Emit: ovfl += ((intptr_t) ovfl + osize - 1) & -osize. */
4302 t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl,
4303 build_int_2 (osize - 1, 0));
4304 t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), t,
4305 build_int_2 (-osize, -1));
4306 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4307 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4310 /* [10, 11]. Emit code to store ovfl in addr_rtx, then
4311 post-increment ovfl by osize. On big-endian machines,
4312 the argument has OSIZE - SIZE bytes of leading padding. */
4313 t = build (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl,
4314 size_int (osize));
4315 if (BYTES_BIG_ENDIAN && osize > size)
4316 t = build (PLUS_EXPR, TREE_TYPE (t), t,
4317 build_int_2 (osize - size, 0));
4318 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4319 if (r != addr_rtx)
4320 emit_move_insn (addr_rtx, r);
4322 emit_queue();
4323 emit_label (lab_over);
4325 if (indirect)
4327 addr_rtx = force_reg (Pmode, addr_rtx);
4328 r = gen_rtx_MEM (Pmode, addr_rtx);
4329 set_mem_alias_set (r, get_varargs_alias_set ());
4330 emit_move_insn (addr_rtx, r);
4332 return addr_rtx;
4334 else
4336 /* Not EABI. */
4337 int align;
4338 HOST_WIDE_INT min_offset;
4340 /* ??? The original va-mips.h did always align, despite the fact
4341 that alignments <= UNITS_PER_WORD are preserved by the va_arg
4342 increment mechanism. */
4344 if (TARGET_NEWABI && TYPE_ALIGN (type) > 64)
4345 align = 16;
4346 else if (TARGET_64BIT)
4347 align = 8;
4348 else if (TYPE_ALIGN (type) > 32)
4349 align = 8;
4350 else
4351 align = 4;
4353 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4354 build_int_2 (align - 1, 0));
4355 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
4357 /* If arguments of type TYPE must be passed on the stack,
4358 set MIN_OFFSET to the offset of the first stack parameter. */
4359 if (!MUST_PASS_IN_STACK (TYPE_MODE (type), type))
4360 min_offset = 0;
4361 else if (TARGET_NEWABI)
4362 min_offset = current_function_pretend_args_size;
4363 else
4364 min_offset = REG_PARM_STACK_SPACE (current_function_decl);
4366 /* Make sure the new address is at least MIN_OFFSET bytes from
4367 the incoming argument pointer. */
4368 if (min_offset > 0)
4369 t = build (MAX_EXPR, TREE_TYPE (valist), t,
4370 make_tree (TREE_TYPE (valist),
4371 plus_constant (virtual_incoming_args_rtx,
4372 min_offset)));
4374 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4375 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4377 /* Everything past the alignment is standard. */
4378 return std_expand_builtin_va_arg (valist, type);
4382 /* Return true if it is possible to use left/right accesses for a
4383 bitfield of WIDTH bits starting BITPOS bits into *OP. When
4384 returning true, update *OP, *LEFT and *RIGHT as follows:
4386 *OP is a BLKmode reference to the whole field.
4388 *LEFT is a QImode reference to the first byte if big endian or
4389 the last byte if little endian. This address can be used in the
4390 left-side instructions (lwl, swl, ldl, sdl).
4392 *RIGHT is a QImode reference to the opposite end of the field and
4393 can be used in the parterning right-side instruction. */
4395 static bool
4396 mips_get_unaligned_mem (rtx *op, unsigned int width, int bitpos,
4397 rtx *left, rtx *right)
4399 rtx first, last;
4401 /* Check that the operand really is a MEM. Not all the extv and
4402 extzv predicates are checked. */
4403 if (GET_CODE (*op) != MEM)
4404 return false;
4406 /* Check that the size is valid. */
4407 if (width != 32 && (!TARGET_64BIT || width != 64))
4408 return false;
4410 /* We can only access byte-aligned values. Since we are always passed
4411 a reference to the first byte of the field, it is not necessary to
4412 do anything with BITPOS after this check. */
4413 if (bitpos % BITS_PER_UNIT != 0)
4414 return false;
4416 /* Reject aligned bitfields: we want to use a normal load or store
4417 instead of a left/right pair. */
4418 if (MEM_ALIGN (*op) >= width)
4419 return false;
4421 /* Adjust *OP to refer to the whole field. This also has the effect
4422 of legitimizing *OP's address for BLKmode, possibly simplifying it. */
4423 *op = adjust_address (*op, BLKmode, 0);
4424 set_mem_size (*op, GEN_INT (width / BITS_PER_UNIT));
4426 /* Get references to both ends of the field. We deliberately don't
4427 use the original QImode *OP for FIRST since the new BLKmode one
4428 might have a simpler address. */
4429 first = adjust_address (*op, QImode, 0);
4430 last = adjust_address (*op, QImode, width / BITS_PER_UNIT - 1);
4432 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
4433 be the upper word and RIGHT the lower word. */
4434 if (TARGET_BIG_ENDIAN)
4435 *left = first, *right = last;
4436 else
4437 *left = last, *right = first;
4439 return true;
4443 /* Try to emit the equivalent of (set DEST (zero_extract SRC WIDTH BITPOS)).
4444 Return true on success. We only handle cases where zero_extract is
4445 equivalent to sign_extract. */
4447 bool
4448 mips_expand_unaligned_load (rtx dest, rtx src, unsigned int width, int bitpos)
4450 rtx left, right;
4452 /* If TARGET_64BIT, the destination of a 32-bit load will be a
4453 paradoxical word_mode subreg. This is the only case in which
4454 we allow the destination to be larger than the source. */
4455 if (GET_CODE (dest) == SUBREG
4456 && GET_MODE (dest) == DImode
4457 && SUBREG_BYTE (dest) == 0
4458 && GET_MODE (SUBREG_REG (dest)) == SImode)
4459 dest = SUBREG_REG (dest);
4461 /* After the above adjustment, the destination must be the same
4462 width as the source. */
4463 if (GET_MODE_BITSIZE (GET_MODE (dest)) != width)
4464 return false;
4466 if (!mips_get_unaligned_mem (&src, width, bitpos, &left, &right))
4467 return false;
4469 if (GET_MODE (dest) == DImode)
4471 emit_insn (gen_mov_ldl (dest, src, left));
4472 emit_insn (gen_mov_ldr (copy_rtx (dest), copy_rtx (src),
4473 right, copy_rtx (dest)));
4475 else
4477 emit_insn (gen_mov_lwl (dest, src, left));
4478 emit_insn (gen_mov_lwr (copy_rtx (dest), copy_rtx (src),
4479 right, copy_rtx (dest)));
4481 return true;
4485 /* Try to expand (set (zero_extract DEST WIDTH BITPOS) SRC). Return
4486 true on success. */
4488 bool
4489 mips_expand_unaligned_store (rtx dest, rtx src, unsigned int width, int bitpos)
4491 rtx left, right;
4493 if (!mips_get_unaligned_mem (&dest, width, bitpos, &left, &right))
4494 return false;
4496 src = gen_lowpart (mode_for_size (width, MODE_INT, 0), src);
4498 if (GET_MODE (src) == DImode)
4500 emit_insn (gen_mov_sdl (dest, src, left));
4501 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
4503 else
4505 emit_insn (gen_mov_swl (dest, src, left));
4506 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
4508 return true;
4511 /* Set up globals to generate code for the ISA or processor
4512 described by INFO. */
4514 static void
4515 mips_set_architecture (const struct mips_cpu_info *info)
4517 if (info != 0)
4519 mips_arch_info = info;
4520 mips_arch = info->cpu;
4521 mips_isa = info->isa;
4526 /* Likewise for tuning. */
4528 static void
4529 mips_set_tune (const struct mips_cpu_info *info)
4531 if (info != 0)
4533 mips_tune_info = info;
4534 mips_tune = info->cpu;
4539 /* Set up the threshold for data to go into the small data area, instead
4540 of the normal data area, and detect any conflicts in the switches. */
4542 void
4543 override_options (void)
4545 int i, start, regno;
4546 enum machine_mode mode;
4548 mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4550 /* Interpret -mabi. */
4551 mips_abi = MIPS_ABI_DEFAULT;
4552 if (mips_abi_string != 0)
4554 if (strcmp (mips_abi_string, "32") == 0)
4555 mips_abi = ABI_32;
4556 else if (strcmp (mips_abi_string, "o64") == 0)
4557 mips_abi = ABI_O64;
4558 else if (strcmp (mips_abi_string, "n32") == 0)
4559 mips_abi = ABI_N32;
4560 else if (strcmp (mips_abi_string, "64") == 0)
4561 mips_abi = ABI_64;
4562 else if (strcmp (mips_abi_string, "eabi") == 0)
4563 mips_abi = ABI_EABI;
4564 else
4565 fatal_error ("bad value (%s) for -mabi= switch", mips_abi_string);
4568 /* The following code determines the architecture and register size.
4569 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
4570 The GAS and GCC code should be kept in sync as much as possible. */
4572 if (mips_arch_string != 0)
4573 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
4575 if (mips_isa_string != 0)
4577 /* Handle -mipsN. */
4578 char *whole_isa_str = concat ("mips", mips_isa_string, NULL);
4579 const struct mips_cpu_info *isa_info;
4581 isa_info = mips_parse_cpu ("-mips option", whole_isa_str);
4582 free (whole_isa_str);
4584 /* -march takes precedence over -mipsN, since it is more descriptive.
4585 There's no harm in specifying both as long as the ISA levels
4586 are the same. */
4587 if (mips_arch_info != 0 && mips_isa != isa_info->isa)
4588 error ("-mips%s conflicts with the other architecture options, "
4589 "which specify a MIPS%d processor",
4590 mips_isa_string, mips_isa);
4592 /* Set architecture based on the given option. */
4593 mips_set_architecture (isa_info);
4596 if (mips_arch_info == 0)
4598 #ifdef MIPS_CPU_STRING_DEFAULT
4599 mips_set_architecture (mips_parse_cpu ("default CPU",
4600 MIPS_CPU_STRING_DEFAULT));
4601 #else
4602 mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
4603 #endif
4606 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
4607 error ("-march=%s is not compatible with the selected ABI",
4608 mips_arch_info->name);
4610 /* Optimize for mips_arch, unless -mtune selects a different processor. */
4611 if (mips_tune_string != 0)
4612 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
4614 if (mips_tune_info == 0)
4615 mips_set_tune (mips_arch_info);
4617 if ((target_flags_explicit & MASK_64BIT) != 0)
4619 /* The user specified the size of the integer registers. Make sure
4620 it agrees with the ABI and ISA. */
4621 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
4622 error ("-mgp64 used with a 32-bit processor");
4623 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
4624 error ("-mgp32 used with a 64-bit ABI");
4625 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
4626 error ("-mgp64 used with a 32-bit ABI");
4628 else
4630 /* Infer the integer register size from the ABI and processor.
4631 Restrict ourselves to 32-bit registers if that's all the
4632 processor has, or if the ABI cannot handle 64-bit registers. */
4633 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
4634 target_flags &= ~MASK_64BIT;
4635 else
4636 target_flags |= MASK_64BIT;
4639 if ((target_flags_explicit & MASK_FLOAT64) != 0)
4641 /* Really, -mfp32 and -mfp64 are ornamental options. There's
4642 only one right answer here. */
4643 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
4644 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
4645 else if (!TARGET_64BIT && TARGET_FLOAT64)
4646 error ("unsupported combination: %s", "-mgp32 -mfp64");
4647 else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
4648 error ("unsupported combination: %s", "-mfp64 -msingle-float");
4650 else
4652 /* -msingle-float selects 32-bit float registers. Otherwise the
4653 float registers should be the same size as the integer ones. */
4654 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
4655 target_flags |= MASK_FLOAT64;
4656 else
4657 target_flags &= ~MASK_FLOAT64;
4660 /* End of code shared with GAS. */
4662 if ((target_flags_explicit & MASK_LONG64) == 0)
4664 /* If no type size setting options (-mlong64,-mint64,-mlong32)
4665 were used, then set the type sizes. In the EABI in 64 bit mode,
4666 longs and pointers are 64 bits. Likewise for the SGI Irix6 N64
4667 ABI. */
4668 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
4669 target_flags |= MASK_LONG64;
4670 else
4671 target_flags &= ~MASK_LONG64;
4674 if (MIPS_MARCH_CONTROLS_SOFT_FLOAT
4675 && (target_flags_explicit & MASK_SOFT_FLOAT) == 0)
4677 /* For some configurations, it is useful to have -march control
4678 the default setting of MASK_SOFT_FLOAT. */
4679 switch ((int) mips_arch)
4681 case PROCESSOR_R4100:
4682 case PROCESSOR_R4111:
4683 case PROCESSOR_R4120:
4684 case PROCESSOR_R4130:
4685 target_flags |= MASK_SOFT_FLOAT;
4686 break;
4688 default:
4689 target_flags &= ~MASK_SOFT_FLOAT;
4690 break;
4694 if (!TARGET_OLDABI)
4695 flag_pcc_struct_return = 0;
4697 #if defined(USE_COLLECT2)
4698 /* For IRIX 5 or IRIX 6 with integrated O32 ABI support, USE_COLLECT2 is
4699 always defined when GNU as is not in use, but collect2 is only used
4700 for the O32 ABI, so override the toplev.c and target-def.h defaults
4701 for flag_gnu_linker, TARGET_ASM_{CONSTRUCTOR, DESTRUCTOR} and
4702 TARGET_HAVE_CTORS_DTORS.
4704 Since the IRIX 5 and IRIX 6 O32 assemblers cannot handle named
4705 sections, constructor/destructor handling depends on the ABI in use.
4707 Since USE_COLLECT2 is defined, we only need to restore the non-collect2
4708 defaults for the N32/N64 ABIs. */
4709 if (TARGET_IRIX && !TARGET_SGI_O32_AS)
4711 targetm.have_ctors_dtors = true;
4712 targetm.asm_out.constructor = default_named_section_asm_out_constructor;
4713 targetm.asm_out.destructor = default_named_section_asm_out_destructor;
4715 #endif
4717 /* Handle some quirks of the IRIX 5 and IRIX 6 O32 assemblers. */
4719 if (TARGET_SGI_O32_AS)
4721 /* They don't recognize `.[248]byte'. */
4722 targetm.asm_out.unaligned_op.hi = "\t.align 0\n\t.half\t";
4723 targetm.asm_out.unaligned_op.si = "\t.align 0\n\t.word\t";
4724 /* The IRIX 6 O32 assembler gives an error for `align 0; .dword',
4725 contrary to the documentation, so disable it. */
4726 targetm.asm_out.unaligned_op.di = NULL;
4728 /* They cannot handle named sections. */
4729 targetm.have_named_sections = false;
4730 /* Therefore, EH_FRAME_SECTION_NAME isn't defined and we must use
4731 collect2. */
4732 targetm.terminate_dw2_eh_frame_info = true;
4733 targetm.asm_out.eh_frame_section = collect2_eh_frame_section;
4735 /* They cannot handle debug information. */
4736 if (write_symbols != NO_DEBUG)
4738 /* Adapt wording to IRIX version: IRIX 5 only had a single ABI,
4739 so -mabi=32 isn't usually specified. */
4740 if (TARGET_IRIX5)
4741 inform ("-g is only supported using GNU as,");
4742 else
4743 inform ("-g is only supported using GNU as with -mabi=32,");
4744 inform ("-g option disabled");
4745 write_symbols = NO_DEBUG;
4749 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
4751 /* If neither -mbranch-likely nor -mno-branch-likely was given
4752 on the command line, set MASK_BRANCHLIKELY based on the target
4753 architecture.
4755 By default, we enable use of Branch Likely instructions on
4756 all architectures which support them with the following
4757 exceptions: when creating MIPS32 or MIPS64 code, and when
4758 tuning for architectures where their use tends to hurt
4759 performance.
4761 The MIPS32 and MIPS64 architecture specifications say "Software
4762 is strongly encouraged to avoid use of Branch Likely
4763 instructions, as they will be removed from a future revision
4764 of the [MIPS32 and MIPS64] architecture." Therefore, we do not
4765 issue those instructions unless instructed to do so by
4766 -mbranch-likely. */
4767 if (ISA_HAS_BRANCHLIKELY
4768 && !(ISA_MIPS32 || ISA_MIPS32R2 || ISA_MIPS64)
4769 && !(TUNE_MIPS5500 || TUNE_SB1))
4770 target_flags |= MASK_BRANCHLIKELY;
4771 else
4772 target_flags &= ~MASK_BRANCHLIKELY;
4774 if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
4775 warning ("generation of Branch Likely instructions enabled, but not supported by architecture");
4777 /* The effect of -mabicalls isn't defined for the EABI. */
4778 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
4780 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
4781 target_flags &= ~MASK_ABICALLS;
4784 /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined. We need
4785 to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work. */
4786 /* ??? -non_shared turns off pic code generation, but this is not
4787 implemented. */
4788 if (TARGET_ABICALLS)
4790 flag_pic = 1;
4791 if (mips_section_threshold > 0)
4792 warning ("-G is incompatible with PIC code which is the default");
4795 /* The MIPS and SGI o32 assemblers expect small-data variables to
4796 be declared before they are used. Although we once had code to
4797 do this, it was very invasive and fragile. It no longer seems
4798 worth the effort. */
4799 if (!TARGET_EXPLICIT_RELOCS && !TARGET_GAS)
4800 mips_section_threshold = 0;
4802 /* We switch to small data sections using ".section", which the native
4803 o32 irix assemblers don't understand. Disable -G accordingly.
4804 We must do this regardless of command-line options since otherwise
4805 the compiler would abort. */
4806 if (!targetm.have_named_sections)
4807 mips_section_threshold = 0;
4809 /* mips_split_addresses is a half-way house between explicit
4810 relocations and the traditional assembler macros. It can
4811 split absolute 32-bit symbolic constants into a high/lo_sum
4812 pair but uses macros for other sorts of access.
4814 Like explicit relocation support for REL targets, it relies
4815 on GNU extensions in the assembler and the linker.
4817 Although this code should work for -O0, it has traditionally
4818 been treated as an optimization. */
4819 if (TARGET_GAS && !TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES
4820 && optimize && !flag_pic
4821 && !ABI_HAS_64BIT_SYMBOLS)
4822 mips_split_addresses = 1;
4823 else
4824 mips_split_addresses = 0;
4826 /* Explicit relocations for "old" ABIs are a GNU extension. Unless
4827 the user has said otherwise, assume that they are not available
4828 with assemblers other than gas. */
4829 if (!TARGET_NEWABI && !TARGET_GAS
4830 && (target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4831 target_flags &= ~MASK_EXPLICIT_RELOCS;
4833 /* Make -mabicalls -fno-unit-at-a-time imply -mno-explicit-relocs
4834 unless the user says otherwise.
4836 There are two problems here:
4838 (1) The value of an R_MIPS_GOT16 relocation depends on whether
4839 the symbol is local or global. We therefore need to know
4840 a symbol's binding before referring to it using %got().
4842 (2) R_MIPS_CALL16 can only be applied to global symbols.
4844 When not using -funit-at-a-time, a symbol's binding may change
4845 after it has been used. For example, the C++ front-end will
4846 initially assume that the typeinfo for an incomplete type will be
4847 comdat, on the basis that the type could be completed later in the
4848 file. But if the type never is completed, the typeinfo will become
4849 local instead. */
4850 if (!flag_unit_at_a_time
4851 && TARGET_ABICALLS
4852 && (target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4853 target_flags &= ~MASK_EXPLICIT_RELOCS;
4855 /* -mrnames says to use the MIPS software convention for register
4856 names instead of the hardware names (ie, $a0 instead of $4).
4857 We do this by switching the names in mips_reg_names, which the
4858 reg_names points into via the REGISTER_NAMES macro. */
4860 if (TARGET_NAME_REGS)
4861 memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
4863 /* -mvr4130-align is a "speed over size" optimization: it usually produces
4864 faster code, but at the expense of more nops. Enable it at -O3 and
4865 above. */
4866 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
4867 target_flags |= MASK_VR4130_ALIGN;
4869 /* When compiling for the mips16, we can not use floating point. We
4870 record the original hard float value in mips16_hard_float. */
4871 if (TARGET_MIPS16)
4873 if (TARGET_SOFT_FLOAT)
4874 mips16_hard_float = 0;
4875 else
4876 mips16_hard_float = 1;
4877 target_flags |= MASK_SOFT_FLOAT;
4879 /* Don't run the scheduler before reload, since it tends to
4880 increase register pressure. */
4881 flag_schedule_insns = 0;
4883 /* Silently disable -mexplicit-relocs since it doesn't apply
4884 to mips16 code. Even so, it would overly pedantic to warn
4885 about "-mips16 -mexplicit-relocs", especially given that
4886 we use a %gprel() operator. */
4887 target_flags &= ~MASK_EXPLICIT_RELOCS;
4890 /* When using explicit relocs, we call dbr_schedule from within
4891 mips_reorg. */
4892 if (TARGET_EXPLICIT_RELOCS)
4894 mips_flag_delayed_branch = flag_delayed_branch;
4895 flag_delayed_branch = 0;
4898 #ifdef MIPS_TFMODE_FORMAT
4899 REAL_MODE_FORMAT (TFmode) = &MIPS_TFMODE_FORMAT;
4900 #endif
4902 mips_print_operand_punct['?'] = 1;
4903 mips_print_operand_punct['#'] = 1;
4904 mips_print_operand_punct['/'] = 1;
4905 mips_print_operand_punct['&'] = 1;
4906 mips_print_operand_punct['!'] = 1;
4907 mips_print_operand_punct['*'] = 1;
4908 mips_print_operand_punct['@'] = 1;
4909 mips_print_operand_punct['.'] = 1;
4910 mips_print_operand_punct['('] = 1;
4911 mips_print_operand_punct[')'] = 1;
4912 mips_print_operand_punct['['] = 1;
4913 mips_print_operand_punct[']'] = 1;
4914 mips_print_operand_punct['<'] = 1;
4915 mips_print_operand_punct['>'] = 1;
4916 mips_print_operand_punct['{'] = 1;
4917 mips_print_operand_punct['}'] = 1;
4918 mips_print_operand_punct['^'] = 1;
4919 mips_print_operand_punct['$'] = 1;
4920 mips_print_operand_punct['+'] = 1;
4921 mips_print_operand_punct['~'] = 1;
4923 mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
4924 mips_char_to_class['t'] = T_REG;
4925 mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
4926 mips_char_to_class['h'] = HI_REG;
4927 mips_char_to_class['l'] = LO_REG;
4928 mips_char_to_class['x'] = MD_REGS;
4929 mips_char_to_class['b'] = ALL_REGS;
4930 mips_char_to_class['c'] = (TARGET_ABICALLS ? PIC_FN_ADDR_REG :
4931 TARGET_MIPS16 ? M16_NA_REGS :
4932 GR_REGS);
4933 mips_char_to_class['e'] = LEA_REGS;
4934 mips_char_to_class['j'] = PIC_FN_ADDR_REG;
4935 mips_char_to_class['y'] = GR_REGS;
4936 mips_char_to_class['z'] = ST_REGS;
4937 mips_char_to_class['B'] = COP0_REGS;
4938 mips_char_to_class['C'] = COP2_REGS;
4939 mips_char_to_class['D'] = COP3_REGS;
4941 /* Set up array to map GCC register number to debug register number.
4942 Ignore the special purpose register numbers. */
4944 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4945 mips_dbx_regno[i] = -1;
4947 start = GP_DBX_FIRST - GP_REG_FIRST;
4948 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
4949 mips_dbx_regno[i] = i + start;
4951 start = FP_DBX_FIRST - FP_REG_FIRST;
4952 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
4953 mips_dbx_regno[i] = i + start;
4955 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
4956 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
4958 /* Set up array giving whether a given register can hold a given mode. */
4960 for (mode = VOIDmode;
4961 mode != MAX_MACHINE_MODE;
4962 mode = (enum machine_mode) ((int)mode + 1))
4964 register int size = GET_MODE_SIZE (mode);
4965 register enum mode_class class = GET_MODE_CLASS (mode);
4967 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4969 register int temp;
4971 if (mode == CCmode)
4973 if (! ISA_HAS_8CC)
4974 temp = (regno == FPSW_REGNUM);
4975 else
4976 temp = (ST_REG_P (regno) || GP_REG_P (regno)
4977 || FP_REG_P (regno));
4980 else if (GP_REG_P (regno))
4981 temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
4983 else if (FP_REG_P (regno))
4984 temp = ((regno % FP_INC) == 0)
4985 && (((class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
4986 && size <= UNITS_PER_FPVALUE)
4987 /* Allow integer modes that fit into a single
4988 register. We need to put integers into FPRs
4989 when using instructions like cvt and trunc. */
4990 || (class == MODE_INT && size <= UNITS_PER_FPREG)
4991 /* Allow TFmode for CCmode reloads. */
4992 || (ISA_HAS_8CC && mode == TFmode));
4994 else if (MD_REG_P (regno))
4995 temp = (INTEGRAL_MODE_P (mode)
4996 && (size <= UNITS_PER_WORD
4997 || (regno == MD_REG_FIRST
4998 && size == 2 * UNITS_PER_WORD)));
5000 else if (ALL_COP_REG_P (regno))
5001 temp = (class == MODE_INT && size <= UNITS_PER_WORD);
5002 else
5003 temp = 0;
5005 mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5009 /* Save GPR registers in word_mode sized hunks. word_mode hasn't been
5010 initialized yet, so we can't use that here. */
5011 gpr_mode = TARGET_64BIT ? DImode : SImode;
5013 /* Provide default values for align_* for 64-bit targets. */
5014 if (TARGET_64BIT && !TARGET_MIPS16)
5016 if (align_loops == 0)
5017 align_loops = 8;
5018 if (align_jumps == 0)
5019 align_jumps = 8;
5020 if (align_functions == 0)
5021 align_functions = 8;
5024 /* Function to allocate machine-dependent function status. */
5025 init_machine_status = &mips_init_machine_status;
5027 if (ABI_HAS_64BIT_SYMBOLS)
5029 if (TARGET_EXPLICIT_RELOCS)
5031 mips_split_p[SYMBOL_64_HIGH] = true;
5032 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
5033 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
5035 mips_split_p[SYMBOL_64_MID] = true;
5036 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
5037 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
5039 mips_split_p[SYMBOL_64_LOW] = true;
5040 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
5041 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
5043 mips_split_p[SYMBOL_GENERAL] = true;
5044 mips_lo_relocs[SYMBOL_GENERAL] = "%lo(";
5047 else
5049 if (TARGET_EXPLICIT_RELOCS || mips_split_addresses)
5051 mips_split_p[SYMBOL_GENERAL] = true;
5052 mips_hi_relocs[SYMBOL_GENERAL] = "%hi(";
5053 mips_lo_relocs[SYMBOL_GENERAL] = "%lo(";
5057 if (TARGET_MIPS16)
5059 /* The high part is provided by a pseudo copy of $gp. */
5060 mips_split_p[SYMBOL_SMALL_DATA] = true;
5061 mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gprel(";
5064 if (TARGET_EXPLICIT_RELOCS)
5066 /* Small data constants are kept whole until after reload,
5067 then lowered by mips_rewrite_small_data. */
5068 mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gp_rel(";
5070 mips_split_p[SYMBOL_GOT_LOCAL] = true;
5071 if (TARGET_NEWABI)
5073 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
5074 mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%got_ofst(";
5076 else
5078 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
5079 mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%lo(";
5082 if (TARGET_XGOT)
5084 /* The HIGH and LO_SUM are matched by special .md patterns. */
5085 mips_split_p[SYMBOL_GOT_GLOBAL] = true;
5087 mips_split_p[SYMBOL_GOTOFF_GLOBAL] = true;
5088 mips_hi_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_hi(";
5089 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_lo(";
5091 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
5092 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
5093 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
5095 else
5097 if (TARGET_NEWABI)
5098 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_disp(";
5099 else
5100 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got(";
5101 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
5105 if (TARGET_NEWABI)
5107 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
5108 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
5109 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
5112 /* Default to working around R4000 errata only if the processor
5113 was selected explicitly. */
5114 if ((target_flags_explicit & MASK_FIX_R4000) == 0
5115 && mips_matching_cpu_name_p (mips_arch_info->name, "r4000"))
5116 target_flags |= MASK_FIX_R4000;
5118 /* Default to working around R4400 errata only if the processor
5119 was selected explicitly. */
5120 if ((target_flags_explicit & MASK_FIX_R4400) == 0
5121 && mips_matching_cpu_name_p (mips_arch_info->name, "r4400"))
5122 target_flags |= MASK_FIX_R4400;
5125 /* Implement CONDITIONAL_REGISTER_USAGE. */
5127 void
5128 mips_conditional_register_usage (void)
5130 if (!TARGET_HARD_FLOAT)
5132 int regno;
5134 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5135 fixed_regs[regno] = call_used_regs[regno] = 1;
5136 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5137 fixed_regs[regno] = call_used_regs[regno] = 1;
5139 else if (! ISA_HAS_8CC)
5141 int regno;
5143 /* We only have a single condition code register. We
5144 implement this by hiding all the condition code registers,
5145 and generating RTL that refers directly to ST_REG_FIRST. */
5146 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5147 fixed_regs[regno] = call_used_regs[regno] = 1;
5149 /* In mips16 mode, we permit the $t temporary registers to be used
5150 for reload. We prohibit the unused $s registers, since they
5151 are caller saved, and saving them via a mips16 register would
5152 probably waste more time than just reloading the value. */
5153 if (TARGET_MIPS16)
5155 fixed_regs[18] = call_used_regs[18] = 1;
5156 fixed_regs[19] = call_used_regs[19] = 1;
5157 fixed_regs[20] = call_used_regs[20] = 1;
5158 fixed_regs[21] = call_used_regs[21] = 1;
5159 fixed_regs[22] = call_used_regs[22] = 1;
5160 fixed_regs[23] = call_used_regs[23] = 1;
5161 fixed_regs[26] = call_used_regs[26] = 1;
5162 fixed_regs[27] = call_used_regs[27] = 1;
5163 fixed_regs[30] = call_used_regs[30] = 1;
5165 /* fp20-23 are now caller saved. */
5166 if (mips_abi == ABI_64)
5168 int regno;
5169 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
5170 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5172 /* Odd registers from fp21 to fp31 are now caller saved. */
5173 if (mips_abi == ABI_N32)
5175 int regno;
5176 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
5177 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5181 /* Allocate a chunk of memory for per-function machine-dependent data. */
5182 static struct machine_function *
5183 mips_init_machine_status (void)
5185 return ((struct machine_function *)
5186 ggc_alloc_cleared (sizeof (struct machine_function)));
5189 /* On the mips16, we want to allocate $24 (T_REG) before other
5190 registers for instructions for which it is possible. This helps
5191 avoid shuffling registers around in order to set up for an xor,
5192 encouraging the compiler to use a cmp instead. */
5194 void
5195 mips_order_regs_for_local_alloc (void)
5197 register int i;
5199 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5200 reg_alloc_order[i] = i;
5202 if (TARGET_MIPS16)
5204 /* It really doesn't matter where we put register 0, since it is
5205 a fixed register anyhow. */
5206 reg_alloc_order[0] = 24;
5207 reg_alloc_order[24] = 0;
5212 /* The MIPS debug format wants all automatic variables and arguments
5213 to be in terms of the virtual frame pointer (stack pointer before
5214 any adjustment in the function), while the MIPS 3.0 linker wants
5215 the frame pointer to be the stack pointer after the initial
5216 adjustment. So, we do the adjustment here. The arg pointer (which
5217 is eliminated) points to the virtual frame pointer, while the frame
5218 pointer (which may be eliminated) points to the stack pointer after
5219 the initial adjustments. */
5221 HOST_WIDE_INT
5222 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
5224 rtx offset2 = const0_rtx;
5225 rtx reg = eliminate_constant_term (addr, &offset2);
5227 if (offset == 0)
5228 offset = INTVAL (offset2);
5230 if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5231 || reg == hard_frame_pointer_rtx)
5233 HOST_WIDE_INT frame_size = (!cfun->machine->frame.initialized)
5234 ? compute_frame_size (get_frame_size ())
5235 : cfun->machine->frame.total_size;
5237 /* MIPS16 frame is smaller */
5238 if (frame_pointer_needed && TARGET_MIPS16)
5239 frame_size -= cfun->machine->frame.args_size;
5241 offset = offset - frame_size;
5244 /* sdbout_parms does not want this to crash for unrecognized cases. */
5245 #if 0
5246 else if (reg != arg_pointer_rtx)
5247 fatal_insn ("mips_debugger_offset called with non stack/frame/arg pointer",
5248 addr);
5249 #endif
5251 return offset;
5254 /* Implement the PRINT_OPERAND macro. The MIPS-specific operand codes are:
5256 'X' OP is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5257 'x' OP is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
5258 'h' OP is HIGH, prints %hi(X),
5259 'd' output integer constant in decimal,
5260 'z' if the operand is 0, use $0 instead of normal operand.
5261 'D' print second part of double-word register or memory operand.
5262 'L' print low-order register of double-word register operand.
5263 'M' print high-order register of double-word register operand.
5264 'C' print part of opcode for a branch condition.
5265 'F' print part of opcode for a floating-point branch condition.
5266 'N' print part of opcode for a branch condition, inverted.
5267 'W' print part of opcode for a floating-point branch condition, inverted.
5268 'B' print 'z' for EQ, 'n' for NE
5269 'b' print 'n' for EQ, 'z' for NE
5270 'T' print 'f' for EQ, 't' for NE
5271 't' print 't' for EQ, 'f' for NE
5272 'Z' print register and a comma, but print nothing for $fcc0
5273 'R' print the reloc associated with LO_SUM
5275 The punctuation characters are:
5277 '(' Turn on .set noreorder
5278 ')' Turn on .set reorder
5279 '[' Turn on .set noat
5280 ']' Turn on .set at
5281 '<' Turn on .set nomacro
5282 '>' Turn on .set macro
5283 '{' Turn on .set volatile (not GAS)
5284 '}' Turn on .set novolatile (not GAS)
5285 '&' Turn on .set noreorder if filling delay slots
5286 '*' Turn on both .set noreorder and .set nomacro if filling delay slots
5287 '!' Turn on .set nomacro if filling delay slots
5288 '#' Print nop if in a .set noreorder section.
5289 '/' Like '#', but does nothing within a delayed branch sequence
5290 '?' Print 'l' if we are to use a branch likely instead of normal branch.
5291 '@' Print the name of the assembler temporary register (at or $1).
5292 '.' Print the name of the register with a hard-wired zero (zero or $0).
5293 '^' Print the name of the pic call-through register (t9 or $25).
5294 '$' Print the name of the stack pointer register (sp or $29).
5295 '+' Print the name of the gp register (usually gp or $28).
5296 '~' Output a branch alignment to LABEL_ALIGN(NULL). */
5298 void
5299 print_operand (FILE *file, rtx op, int letter)
5301 register enum rtx_code code;
5303 if (PRINT_OPERAND_PUNCT_VALID_P (letter))
5305 switch (letter)
5307 case '?':
5308 if (mips_branch_likely)
5309 putc ('l', file);
5310 break;
5312 case '@':
5313 fputs (reg_names [GP_REG_FIRST + 1], file);
5314 break;
5316 case '^':
5317 fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
5318 break;
5320 case '.':
5321 fputs (reg_names [GP_REG_FIRST + 0], file);
5322 break;
5324 case '$':
5325 fputs (reg_names[STACK_POINTER_REGNUM], file);
5326 break;
5328 case '+':
5329 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
5330 break;
5332 case '&':
5333 if (final_sequence != 0 && set_noreorder++ == 0)
5334 fputs (".set\tnoreorder\n\t", file);
5335 break;
5337 case '*':
5338 if (final_sequence != 0)
5340 if (set_noreorder++ == 0)
5341 fputs (".set\tnoreorder\n\t", file);
5343 if (set_nomacro++ == 0)
5344 fputs (".set\tnomacro\n\t", file);
5346 break;
5348 case '!':
5349 if (final_sequence != 0 && set_nomacro++ == 0)
5350 fputs ("\n\t.set\tnomacro", file);
5351 break;
5353 case '#':
5354 if (set_noreorder != 0)
5355 fputs ("\n\tnop", file);
5356 break;
5358 case '/':
5359 /* Print an extra newline so that the delayed insn is separated
5360 from the following ones. This looks neater and is consistent
5361 with non-nop delayed sequences. */
5362 if (set_noreorder != 0 && final_sequence == 0)
5363 fputs ("\n\tnop\n", file);
5364 break;
5366 case '(':
5367 if (set_noreorder++ == 0)
5368 fputs (".set\tnoreorder\n\t", file);
5369 break;
5371 case ')':
5372 if (set_noreorder == 0)
5373 error ("internal error: %%) found without a %%( in assembler pattern");
5375 else if (--set_noreorder == 0)
5376 fputs ("\n\t.set\treorder", file);
5378 break;
5380 case '[':
5381 if (set_noat++ == 0)
5382 fputs (".set\tnoat\n\t", file);
5383 break;
5385 case ']':
5386 if (set_noat == 0)
5387 error ("internal error: %%] found without a %%[ in assembler pattern");
5388 else if (--set_noat == 0)
5389 fputs ("\n\t.set\tat", file);
5391 break;
5393 case '<':
5394 if (set_nomacro++ == 0)
5395 fputs (".set\tnomacro\n\t", file);
5396 break;
5398 case '>':
5399 if (set_nomacro == 0)
5400 error ("internal error: %%> found without a %%< in assembler pattern");
5401 else if (--set_nomacro == 0)
5402 fputs ("\n\t.set\tmacro", file);
5404 break;
5406 case '{':
5407 if (set_volatile++ == 0)
5408 fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
5409 break;
5411 case '}':
5412 if (set_volatile == 0)
5413 error ("internal error: %%} found without a %%{ in assembler pattern");
5414 else if (--set_volatile == 0)
5415 fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
5417 break;
5419 case '~':
5421 if (align_labels_log > 0)
5422 ASM_OUTPUT_ALIGN (file, align_labels_log);
5424 break;
5426 default:
5427 error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
5428 break;
5431 return;
5434 if (! op)
5436 error ("PRINT_OPERAND null pointer");
5437 return;
5440 code = GET_CODE (op);
5442 if (letter == 'C')
5443 switch (code)
5445 case EQ: fputs ("eq", file); break;
5446 case NE: fputs ("ne", file); break;
5447 case GT: fputs ("gt", file); break;
5448 case GE: fputs ("ge", file); break;
5449 case LT: fputs ("lt", file); break;
5450 case LE: fputs ("le", file); break;
5451 case GTU: fputs ("gtu", file); break;
5452 case GEU: fputs ("geu", file); break;
5453 case LTU: fputs ("ltu", file); break;
5454 case LEU: fputs ("leu", file); break;
5455 default:
5456 fatal_insn ("PRINT_OPERAND, invalid insn for %%C", op);
5459 else if (letter == 'N')
5460 switch (code)
5462 case EQ: fputs ("ne", file); break;
5463 case NE: fputs ("eq", file); break;
5464 case GT: fputs ("le", file); break;
5465 case GE: fputs ("lt", file); break;
5466 case LT: fputs ("ge", file); break;
5467 case LE: fputs ("gt", file); break;
5468 case GTU: fputs ("leu", file); break;
5469 case GEU: fputs ("ltu", file); break;
5470 case LTU: fputs ("geu", file); break;
5471 case LEU: fputs ("gtu", file); break;
5472 default:
5473 fatal_insn ("PRINT_OPERAND, invalid insn for %%N", op);
5476 else if (letter == 'F')
5477 switch (code)
5479 case EQ: fputs ("c1f", file); break;
5480 case NE: fputs ("c1t", file); break;
5481 default:
5482 fatal_insn ("PRINT_OPERAND, invalid insn for %%F", op);
5485 else if (letter == 'W')
5486 switch (code)
5488 case EQ: fputs ("c1t", file); break;
5489 case NE: fputs ("c1f", file); break;
5490 default:
5491 fatal_insn ("PRINT_OPERAND, invalid insn for %%W", op);
5494 else if (letter == 'h')
5496 if (GET_CODE (op) == HIGH)
5497 op = XEXP (op, 0);
5499 print_operand_reloc (file, op, mips_hi_relocs);
5502 else if (letter == 'R')
5503 print_operand_reloc (file, op, mips_lo_relocs);
5505 else if (letter == 'Z')
5507 register int regnum;
5509 if (code != REG)
5510 abort ();
5512 regnum = REGNO (op);
5513 if (! ST_REG_P (regnum))
5514 abort ();
5516 if (regnum != ST_REG_FIRST)
5517 fprintf (file, "%s,", reg_names[regnum]);
5520 else if (code == REG || code == SUBREG)
5522 register int regnum;
5524 if (code == REG)
5525 regnum = REGNO (op);
5526 else
5527 regnum = true_regnum (op);
5529 if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
5530 || (letter == 'L' && WORDS_BIG_ENDIAN)
5531 || letter == 'D')
5532 regnum++;
5534 fprintf (file, "%s", reg_names[regnum]);
5537 else if (code == MEM)
5539 if (letter == 'D')
5540 output_address (plus_constant (XEXP (op, 0), 4));
5541 else
5542 output_address (XEXP (op, 0));
5545 else if (letter == 'x' && GET_CODE (op) == CONST_INT)
5546 fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
5548 else if (letter == 'X' && GET_CODE(op) == CONST_INT)
5549 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
5551 else if (letter == 'd' && GET_CODE(op) == CONST_INT)
5552 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
5554 else if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
5555 fputs (reg_names[GP_REG_FIRST], file);
5557 else if (letter == 'd' || letter == 'x' || letter == 'X')
5558 output_operand_lossage ("invalid use of %%d, %%x, or %%X");
5560 else if (letter == 'B')
5561 fputs (code == EQ ? "z" : "n", file);
5562 else if (letter == 'b')
5563 fputs (code == EQ ? "n" : "z", file);
5564 else if (letter == 'T')
5565 fputs (code == EQ ? "f" : "t", file);
5566 else if (letter == 't')
5567 fputs (code == EQ ? "t" : "f", file);
5569 else if (CONST_GP_P (op))
5570 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
5572 else
5573 output_addr_const (file, op);
5577 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM.
5578 RELOCS is the array of relocations to use. */
5580 static void
5581 print_operand_reloc (FILE *file, rtx op, const char **relocs)
5583 enum mips_symbol_type symbol_type;
5584 const char *p;
5585 rtx base;
5586 HOST_WIDE_INT offset;
5588 if (!mips_symbolic_constant_p (op, &symbol_type) || relocs[symbol_type] == 0)
5589 fatal_insn ("PRINT_OPERAND, invalid operand for relocation", op);
5591 /* If OP uses an UNSPEC address, we want to print the inner symbol. */
5592 mips_split_const (op, &base, &offset);
5593 if (UNSPEC_ADDRESS_P (base))
5594 op = plus_constant (UNSPEC_ADDRESS (base), offset);
5596 fputs (relocs[symbol_type], file);
5597 output_addr_const (file, op);
5598 for (p = relocs[symbol_type]; *p != 0; p++)
5599 if (*p == '(')
5600 fputc (')', file);
5603 /* Output address operand X to FILE. */
5605 void
5606 print_operand_address (FILE *file, rtx x)
5608 struct mips_address_info addr;
5610 if (mips_classify_address (&addr, x, word_mode, true))
5611 switch (addr.type)
5613 case ADDRESS_REG:
5614 print_operand (file, addr.offset, 0);
5615 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5616 return;
5618 case ADDRESS_LO_SUM:
5619 print_operand (file, addr.offset, 'R');
5620 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5621 return;
5623 case ADDRESS_CONST_INT:
5624 case ADDRESS_SYMBOLIC:
5625 output_addr_const (file, x);
5626 return;
5628 abort ();
5631 /* Target hook for assembling integer objects. It appears that the Irix
5632 6 assembler can't handle 64-bit decimal integers, so avoid printing
5633 such an integer here. */
5635 static bool
5636 mips_assemble_integer (rtx x, unsigned int size, int aligned_p)
5638 if ((TARGET_64BIT || TARGET_GAS) && size == 8 && aligned_p)
5640 fputs ("\t.dword\t", asm_out_file);
5641 if (HOST_BITS_PER_WIDE_INT < 64 || GET_CODE (x) != CONST_INT)
5642 output_addr_const (asm_out_file, x);
5643 else
5644 print_operand (asm_out_file, x, 'X');
5645 fputc ('\n', asm_out_file);
5646 return true;
5648 return default_assemble_integer (x, size, aligned_p);
5651 /* When using assembler macros, keep track of all of small-data externs
5652 so that mips_file_end can emit the appropriate declarations for them.
5654 In most cases it would be safe (though pointless) to emit .externs
5655 for other symbols too. One exception is when an object is within
5656 the -G limit but declared by the user to be in a section other
5657 than .sbss or .sdata. */
5660 mips_output_external (FILE *file ATTRIBUTE_UNUSED, tree decl, const char *name)
5662 register struct extern_list *p;
5664 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
5666 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5667 p->next = extern_head;
5668 p->name = name;
5669 p->size = int_size_in_bytes (TREE_TYPE (decl));
5670 extern_head = p;
5673 if (TARGET_IRIX && mips_abi == ABI_32 && TREE_CODE (decl) == FUNCTION_DECL)
5675 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5676 p->next = extern_head;
5677 p->name = name;
5678 p->size = -1;
5679 extern_head = p;
5682 return 0;
5685 #if TARGET_IRIX
5686 void
5687 irix_output_external_libcall (rtx fun)
5689 register struct extern_list *p;
5691 if (mips_abi == ABI_32)
5693 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5694 p->next = extern_head;
5695 p->name = XSTR (fun, 0);
5696 p->size = -1;
5697 extern_head = p;
5700 #endif
5702 /* Emit a new filename to a stream. If we are smuggling stabs, try to
5703 put out a MIPS ECOFF file and a stab. */
5705 void
5706 mips_output_filename (FILE *stream, const char *name)
5708 char ltext_label_name[100];
5710 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
5711 directives. */
5712 if (write_symbols == DWARF2_DEBUG)
5713 return;
5714 else if (mips_output_filename_first_time)
5716 mips_output_filename_first_time = 0;
5717 SET_FILE_NUMBER ();
5718 current_function_file = name;
5719 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5720 /* This tells mips-tfile that stabs will follow. */
5721 if (!TARGET_GAS && write_symbols == DBX_DEBUG)
5722 fprintf (stream, "\t#@stabs\n");
5725 else if (write_symbols == DBX_DEBUG)
5727 ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
5728 fprintf (stream, "%s", ASM_STABS_OP);
5729 output_quoted_string (stream, name);
5730 fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
5733 else if (name != current_function_file
5734 && strcmp (name, current_function_file) != 0)
5736 SET_FILE_NUMBER ();
5737 current_function_file = name;
5738 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5742 /* Emit a linenumber. For encapsulated stabs, we need to put out a stab
5743 as well as a .loc, since it is possible that MIPS ECOFF might not be
5744 able to represent the location for inlines that come from a different
5745 file. */
5747 void
5748 mips_output_lineno (FILE *stream, int line)
5750 if (write_symbols == DBX_DEBUG)
5752 ++sym_lineno;
5753 fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
5754 LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
5755 LOCAL_LABEL_PREFIX, sym_lineno);
5757 else
5759 fprintf (stream, "\n\t.loc\t%d %d\n", num_source_filenames, line);
5760 LABEL_AFTER_LOC (stream);
5764 /* Output an ASCII string, in a space-saving way. PREFIX is the string
5765 that should be written before the opening quote, such as "\t.ascii\t"
5766 for real string data or "\t# " for a comment. */
5768 void
5769 mips_output_ascii (FILE *stream, const char *string_param, size_t len,
5770 const char *prefix)
5772 size_t i;
5773 int cur_pos = 17;
5774 register const unsigned char *string =
5775 (const unsigned char *)string_param;
5777 fprintf (stream, "%s\"", prefix);
5778 for (i = 0; i < len; i++)
5780 register int c = string[i];
5782 switch (c)
5784 case '\"':
5785 case '\\':
5786 putc ('\\', stream);
5787 putc (c, stream);
5788 cur_pos += 2;
5789 break;
5791 case TARGET_NEWLINE:
5792 fputs ("\\n", stream);
5793 if (i+1 < len
5794 && (((c = string[i+1]) >= '\040' && c <= '~')
5795 || c == TARGET_TAB))
5796 cur_pos = 32767; /* break right here */
5797 else
5798 cur_pos += 2;
5799 break;
5801 case TARGET_TAB:
5802 fputs ("\\t", stream);
5803 cur_pos += 2;
5804 break;
5806 case TARGET_FF:
5807 fputs ("\\f", stream);
5808 cur_pos += 2;
5809 break;
5811 case TARGET_BS:
5812 fputs ("\\b", stream);
5813 cur_pos += 2;
5814 break;
5816 case TARGET_CR:
5817 fputs ("\\r", stream);
5818 cur_pos += 2;
5819 break;
5821 default:
5822 if (c >= ' ' && c < 0177)
5824 putc (c, stream);
5825 cur_pos++;
5827 else
5829 fprintf (stream, "\\%03o", c);
5830 cur_pos += 4;
5834 if (cur_pos > 72 && i+1 < len)
5836 cur_pos = 17;
5837 fprintf (stream, "\"\n%s\"", prefix);
5840 fprintf (stream, "\"\n");
5843 /* Implement TARGET_ASM_FILE_START. */
5845 static void
5846 mips_file_start (void)
5848 default_file_start ();
5850 /* Versions of the MIPS assembler before 2.20 generate errors if a branch
5851 inside of a .set noreorder section jumps to a label outside of the .set
5852 noreorder section. Revision 2.20 just set nobopt silently rather than
5853 fixing the bug. */
5855 if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
5856 fprintf (asm_out_file, "\t.set\tnobopt\n");
5858 if (TARGET_GAS)
5860 #if defined(OBJECT_FORMAT_ELF) && !TARGET_IRIX
5861 /* Generate a special section to describe the ABI switches used to
5862 produce the resultant binary. This used to be done by the assembler
5863 setting bits in the ELF header's flags field, but we have run out of
5864 bits. GDB needs this information in order to be able to correctly
5865 debug these binaries. See the function mips_gdbarch_init() in
5866 gdb/mips-tdep.c. This is unnecessary for the IRIX 5/6 ABIs and
5867 causes unnecessary IRIX 6 ld warnings. */
5868 const char * abi_string = NULL;
5870 switch (mips_abi)
5872 case ABI_32: abi_string = "abi32"; break;
5873 case ABI_N32: abi_string = "abiN32"; break;
5874 case ABI_64: abi_string = "abi64"; break;
5875 case ABI_O64: abi_string = "abiO64"; break;
5876 case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
5877 default:
5878 abort ();
5880 /* Note - we use fprintf directly rather than called named_section()
5881 because in this way we can avoid creating an allocated section. We
5882 do not want this section to take up any space in the running
5883 executable. */
5884 fprintf (asm_out_file, "\t.section .mdebug.%s\n", abi_string);
5886 /* There is no ELF header flag to distinguish long32 forms of the
5887 EABI from long64 forms. Emit a special section to help tools
5888 such as GDB. */
5889 if (mips_abi == ABI_EABI)
5890 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n",
5891 TARGET_LONG64 ? 64 : 32);
5893 /* Restore the default section. */
5894 fprintf (asm_out_file, "\t.previous\n");
5895 #endif
5898 /* Generate the pseudo ops that System V.4 wants. */
5899 #ifndef ABICALLS_ASM_OP
5900 #define ABICALLS_ASM_OP "\t.abicalls"
5901 #endif
5902 if (TARGET_ABICALLS)
5903 /* ??? but do not want this (or want pic0) if -non-shared? */
5904 fprintf (asm_out_file, "%s\n", ABICALLS_ASM_OP);
5906 if (TARGET_MIPS16)
5907 fprintf (asm_out_file, "\t.set\tmips16\n");
5909 if (flag_verbose_asm)
5910 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
5911 ASM_COMMENT_START,
5912 mips_section_threshold, mips_arch_info->name, mips_isa);
5915 #ifdef BSS_SECTION_ASM_OP
5916 /* Implement ASM_OUTPUT_ALIGNED_BSS. This differs from the default only
5917 in the use of sbss. */
5919 void
5920 mips_output_aligned_bss (FILE *stream, tree decl, const char *name,
5921 unsigned HOST_WIDE_INT size, int align)
5923 extern tree last_assemble_variable_decl;
5925 if (mips_in_small_data_p (decl))
5926 named_section (0, ".sbss", 0);
5927 else
5928 bss_section ();
5929 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
5930 last_assemble_variable_decl = decl;
5931 ASM_DECLARE_OBJECT_NAME (stream, name, decl);
5932 ASM_OUTPUT_SKIP (stream, size != 0 ? size : 1);
5934 #endif
5936 /* Implement TARGET_ASM_FILE_END. When using assembler macros, emit
5937 .externs for any small-data variables that turned out to be external. */
5939 static void
5940 mips_file_end (void)
5942 tree name_tree;
5943 struct extern_list *p;
5945 if (extern_head)
5947 fputs ("\n", asm_out_file);
5949 for (p = extern_head; p != 0; p = p->next)
5951 name_tree = get_identifier (p->name);
5953 /* Positively ensure only one .extern for any given symbol. */
5954 if (!TREE_ASM_WRITTEN (name_tree)
5955 && TREE_SYMBOL_REFERENCED (name_tree))
5957 TREE_ASM_WRITTEN (name_tree) = 1;
5958 /* In IRIX 5 or IRIX 6 for the O32 ABI, we must output a
5959 `.global name .text' directive for every used but
5960 undefined function. If we don't, the linker may perform
5961 an optimization (skipping over the insns that set $gp)
5962 when it is unsafe. */
5963 if (TARGET_IRIX && mips_abi == ABI_32 && p->size == -1)
5965 fputs ("\t.globl ", asm_out_file);
5966 assemble_name (asm_out_file, p->name);
5967 fputs (" .text\n", asm_out_file);
5969 else
5971 fputs ("\t.extern\t", asm_out_file);
5972 assemble_name (asm_out_file, p->name);
5973 fprintf (asm_out_file, ", %d\n", p->size);
5980 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as
5981 the elfos.h version, but we also need to handle -muninit-const-in-rodata
5982 and the limitations of the SGI o32 assembler. */
5984 void
5985 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
5986 unsigned HOST_WIDE_INT size,
5987 unsigned int align)
5989 /* If the target wants uninitialized const declarations in
5990 .rdata then don't put them in .comm. */
5991 if (TARGET_EMBEDDED_DATA && TARGET_UNINIT_CONST_IN_RODATA
5992 && TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl)
5993 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5995 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
5996 targetm.asm_out.globalize_label (stream, name);
5998 readonly_data_section ();
5999 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
6000 mips_declare_object (stream, name, "",
6001 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
6002 size);
6004 else if (TARGET_SGI_O32_AS)
6006 /* The SGI o32 assembler doesn't accept an alignment, so round up
6007 the size instead. */
6008 size += (align / BITS_PER_UNIT) - 1;
6009 size -= size % (align / BITS_PER_UNIT);
6010 mips_declare_object (stream, name, "\n\t.comm\t",
6011 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
6013 else
6014 mips_declare_object (stream, name, "\n\t.comm\t",
6015 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
6016 size, align / BITS_PER_UNIT);
6019 /* Emit either a label, .comm, or .lcomm directive. When using assembler
6020 macros, mark the symbol as written so that mips_file_end won't emit an
6021 .extern for it. STREAM is the output file, NAME is the name of the
6022 symbol, INIT_STRING is the string that should be written before the
6023 symbol and FINAL_STRING is the string that should be written after it.
6024 FINAL_STRING is a printf() format that consumes the remaining arguments. */
6026 void
6027 mips_declare_object (FILE *stream, const char *name, const char *init_string,
6028 const char *final_string, ...)
6030 va_list ap;
6032 fputs (init_string, stream);
6033 assemble_name (stream, name);
6034 va_start (ap, final_string);
6035 vfprintf (stream, final_string, ap);
6036 va_end (ap);
6038 if (!TARGET_EXPLICIT_RELOCS)
6040 tree name_tree = get_identifier (name);
6041 TREE_ASM_WRITTEN (name_tree) = 1;
6045 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
6046 extern int size_directive_output;
6048 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
6049 definitions except that it uses mips_declare_object() to emit the label. */
6051 void
6052 mips_declare_object_name (FILE *stream, const char *name,
6053 tree decl ATTRIBUTE_UNUSED)
6055 if (!TARGET_SGI_O32_AS)
6057 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
6058 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
6059 #endif
6061 size_directive_output = 0;
6062 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
6064 HOST_WIDE_INT size;
6066 size_directive_output = 1;
6067 size = int_size_in_bytes (TREE_TYPE (decl));
6068 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6072 mips_declare_object (stream, name, "", ":\n", 0);
6075 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
6077 void
6078 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
6080 const char *name;
6082 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6083 if (!TARGET_SGI_O32_AS
6084 && !flag_inhibit_size_directive
6085 && DECL_SIZE (decl) != 0
6086 && !at_end && top_level
6087 && DECL_INITIAL (decl) == error_mark_node
6088 && !size_directive_output)
6090 HOST_WIDE_INT size;
6092 size_directive_output = 1;
6093 size = int_size_in_bytes (TREE_TYPE (decl));
6094 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6097 #endif
6099 /* Return true if X is a small data address that can be rewritten
6100 as a LO_SUM. */
6102 static bool
6103 mips_rewrite_small_data_p (rtx x)
6105 enum mips_symbol_type symbol_type;
6107 return (TARGET_EXPLICIT_RELOCS
6108 && mips_symbolic_constant_p (x, &symbol_type)
6109 && symbol_type == SYMBOL_SMALL_DATA);
6113 /* A for_each_rtx callback for small_data_pattern. */
6115 static int
6116 small_data_pattern_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6118 if (GET_CODE (*loc) == LO_SUM)
6119 return -1;
6121 return mips_rewrite_small_data_p (*loc);
6124 /* Return true if OP refers to small data symbols directly, not through
6125 a LO_SUM. */
6128 small_data_pattern (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
6130 return (GET_CODE (op) != SEQUENCE
6131 && for_each_rtx (&op, small_data_pattern_1, 0));
6134 /* A for_each_rtx callback, used by mips_rewrite_small_data. */
6136 static int
6137 mips_rewrite_small_data_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6139 if (mips_rewrite_small_data_p (*loc))
6140 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
6142 if (GET_CODE (*loc) == LO_SUM)
6143 return -1;
6145 return 0;
6148 /* If possible, rewrite OP so that it refers to small data using
6149 explicit relocations. */
6152 mips_rewrite_small_data (rtx op)
6154 op = copy_insn (op);
6155 for_each_rtx (&op, mips_rewrite_small_data_1, 0);
6156 return op;
6159 /* Return true if the current function has an insn that implicitly
6160 refers to $gp. */
6162 static bool
6163 mips_function_has_gp_insn (void)
6165 /* Don't bother rechecking if we found one last time. */
6166 if (!cfun->machine->has_gp_insn_p)
6168 rtx insn;
6170 push_topmost_sequence ();
6171 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6172 if (INSN_P (insn)
6173 && GET_CODE (PATTERN (insn)) != USE
6174 && GET_CODE (PATTERN (insn)) != CLOBBER
6175 && (get_attr_got (insn) != GOT_UNSET
6176 || small_data_pattern (PATTERN (insn), VOIDmode)))
6177 break;
6178 pop_topmost_sequence ();
6180 cfun->machine->has_gp_insn_p = (insn != 0);
6182 return cfun->machine->has_gp_insn_p;
6186 /* Return the register that should be used as the global pointer
6187 within this function. Return 0 if the function doesn't need
6188 a global pointer. */
6190 static unsigned int
6191 mips_global_pointer (void)
6193 unsigned int regno;
6195 /* $gp is always available in non-abicalls code. */
6196 if (!TARGET_ABICALLS)
6197 return GLOBAL_POINTER_REGNUM;
6199 /* We must always provide $gp when it is used implicitly. */
6200 if (!TARGET_EXPLICIT_RELOCS)
6201 return GLOBAL_POINTER_REGNUM;
6203 /* FUNCTION_PROFILER includes a jal macro, so we need to give it
6204 a valid gp. */
6205 if (current_function_profile)
6206 return GLOBAL_POINTER_REGNUM;
6208 /* If the function has a nonlocal goto, $gp must hold the correct
6209 global pointer for the target function. */
6210 if (current_function_has_nonlocal_goto)
6211 return GLOBAL_POINTER_REGNUM;
6213 /* If the gp is never referenced, there's no need to initialize it.
6214 Note that reload can sometimes introduce constant pool references
6215 into a function that otherwise didn't need them. For example,
6216 suppose we have an instruction like:
6218 (set (reg:DF R1) (float:DF (reg:SI R2)))
6220 If R2 turns out to be constant such as 1, the instruction may have a
6221 REG_EQUAL note saying that R1 == 1.0. Reload then has the option of
6222 using this constant if R2 doesn't get allocated to a register.
6224 In cases like these, reload will have added the constant to the pool
6225 but no instruction will yet refer to it. */
6226 if (!regs_ever_live[GLOBAL_POINTER_REGNUM]
6227 && !current_function_uses_const_pool
6228 && !mips_function_has_gp_insn ())
6229 return 0;
6231 /* We need a global pointer, but perhaps we can use a call-clobbered
6232 register instead of $gp. */
6233 if (TARGET_NEWABI && current_function_is_leaf)
6234 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6235 if (!regs_ever_live[regno]
6236 && call_used_regs[regno]
6237 && !fixed_regs[regno]
6238 && regno != PIC_FUNCTION_ADDR_REGNUM)
6239 return regno;
6241 return GLOBAL_POINTER_REGNUM;
6245 /* Return true if the current function must save REGNO. */
6247 static bool
6248 mips_save_reg_p (unsigned int regno)
6250 /* We only need to save $gp for NewABI PIC. */
6251 if (regno == GLOBAL_POINTER_REGNUM)
6252 return (TARGET_ABICALLS && TARGET_NEWABI
6253 && cfun->machine->global_pointer == regno);
6255 /* Check call-saved registers. */
6256 if (regs_ever_live[regno] && !call_used_regs[regno])
6257 return true;
6259 /* We need to save the old frame pointer before setting up a new one. */
6260 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
6261 return true;
6263 /* We need to save the incoming return address if it is ever clobbered
6264 within the function. */
6265 if (regno == GP_REG_FIRST + 31 && regs_ever_live[regno])
6266 return true;
6268 if (TARGET_MIPS16)
6270 tree return_type;
6272 return_type = DECL_RESULT (current_function_decl);
6274 /* $18 is a special case in mips16 code. It may be used to call
6275 a function which returns a floating point value, but it is
6276 marked in call_used_regs. */
6277 if (regno == GP_REG_FIRST + 18 && regs_ever_live[regno])
6278 return true;
6280 /* $31 is also a special case. It will be used to copy a return
6281 value into the floating point registers if the return value is
6282 floating point. */
6283 if (regno == GP_REG_FIRST + 31
6284 && mips16_hard_float
6285 && !aggregate_value_p (return_type, current_function_decl)
6286 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
6287 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
6288 return true;
6291 return false;
6295 /* Return the bytes needed to compute the frame pointer from the current
6296 stack pointer. SIZE is the size (in bytes) of the local variables.
6298 Mips stack frames look like:
6300 Before call After call
6301 +-----------------------+ +-----------------------+
6302 high | | | |
6303 mem. | | | |
6304 | caller's temps. | | caller's temps. |
6305 | | | |
6306 +-----------------------+ +-----------------------+
6307 | | | |
6308 | arguments on stack. | | arguments on stack. |
6309 | | | |
6310 +-----------------------+ +-----------------------+
6311 | 4 words to save | | 4 words to save |
6312 | arguments passed | | arguments passed |
6313 | in registers, even | | in registers, even |
6314 SP->| if not passed. | VFP->| if not passed. |
6315 +-----------------------+ +-----------------------+
6317 | fp register save |
6319 +-----------------------+
6321 | gp register save |
6323 +-----------------------+
6325 | local variables |
6327 +-----------------------+
6329 | alloca allocations |
6331 +-----------------------+
6333 | GP save for V.4 abi |
6335 +-----------------------+
6337 | arguments on stack |
6339 +-----------------------+
6340 | 4 words to save |
6341 | arguments passed |
6342 | in registers, even |
6343 low SP->| if not passed. |
6344 memory +-----------------------+
6348 HOST_WIDE_INT
6349 compute_frame_size (HOST_WIDE_INT size)
6351 unsigned int regno;
6352 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
6353 HOST_WIDE_INT var_size; /* # bytes that variables take up */
6354 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
6355 HOST_WIDE_INT cprestore_size; /* # bytes that the cprestore slot takes up */
6356 HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6357 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
6358 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
6359 unsigned int mask; /* mask of saved gp registers */
6360 unsigned int fmask; /* mask of saved fp registers */
6362 cfun->machine->global_pointer = mips_global_pointer ();
6364 gp_reg_size = 0;
6365 fp_reg_size = 0;
6366 mask = 0;
6367 fmask = 0;
6368 var_size = MIPS_STACK_ALIGN (size);
6369 args_size = current_function_outgoing_args_size;
6370 cprestore_size = MIPS_STACK_ALIGN (STARTING_FRAME_OFFSET) - args_size;
6372 /* The space set aside by STARTING_FRAME_OFFSET isn't needed in leaf
6373 functions. If the function has local variables, we're committed
6374 to allocating it anyway. Otherwise reclaim it here. */
6375 if (var_size == 0 && current_function_is_leaf)
6376 cprestore_size = args_size = 0;
6378 /* The MIPS 3.0 linker does not like functions that dynamically
6379 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6380 looks like we are trying to create a second frame pointer to the
6381 function, so allocate some stack space to make it happy. */
6383 if (args_size == 0 && current_function_calls_alloca)
6384 args_size = 4 * UNITS_PER_WORD;
6386 total_size = var_size + args_size + cprestore_size;
6388 /* Calculate space needed for gp registers. */
6389 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6390 if (mips_save_reg_p (regno))
6392 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6393 mask |= 1 << (regno - GP_REG_FIRST);
6396 /* We need to restore these for the handler. */
6397 if (current_function_calls_eh_return)
6399 unsigned int i;
6400 for (i = 0; ; ++i)
6402 regno = EH_RETURN_DATA_REGNO (i);
6403 if (regno == INVALID_REGNUM)
6404 break;
6405 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6406 mask |= 1 << (regno - GP_REG_FIRST);
6410 /* This loop must iterate over the same space as its companion in
6411 save_restore_insns. */
6412 for (regno = (FP_REG_LAST - FP_INC + 1);
6413 regno >= FP_REG_FIRST;
6414 regno -= FP_INC)
6416 if (mips_save_reg_p (regno))
6418 fp_reg_size += FP_INC * UNITS_PER_FPREG;
6419 fmask |= ((1 << FP_INC) - 1) << (regno - FP_REG_FIRST);
6423 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6424 total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
6426 /* Add in space reserved on the stack by the callee for storing arguments
6427 passed in registers. */
6428 if (!TARGET_OLDABI)
6429 total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
6431 /* Save other computed information. */
6432 cfun->machine->frame.total_size = total_size;
6433 cfun->machine->frame.var_size = var_size;
6434 cfun->machine->frame.args_size = args_size;
6435 cfun->machine->frame.cprestore_size = cprestore_size;
6436 cfun->machine->frame.gp_reg_size = gp_reg_size;
6437 cfun->machine->frame.fp_reg_size = fp_reg_size;
6438 cfun->machine->frame.mask = mask;
6439 cfun->machine->frame.fmask = fmask;
6440 cfun->machine->frame.initialized = reload_completed;
6441 cfun->machine->frame.num_gp = gp_reg_size / UNITS_PER_WORD;
6442 cfun->machine->frame.num_fp = fp_reg_size / (FP_INC * UNITS_PER_FPREG);
6444 if (mask)
6446 HOST_WIDE_INT offset;
6448 offset = (args_size + cprestore_size + var_size
6449 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
6450 cfun->machine->frame.gp_sp_offset = offset;
6451 cfun->machine->frame.gp_save_offset = offset - total_size;
6453 else
6455 cfun->machine->frame.gp_sp_offset = 0;
6456 cfun->machine->frame.gp_save_offset = 0;
6459 if (fmask)
6461 HOST_WIDE_INT offset;
6463 offset = (args_size + cprestore_size + var_size
6464 + gp_reg_rounded + fp_reg_size
6465 - FP_INC * UNITS_PER_FPREG);
6466 cfun->machine->frame.fp_sp_offset = offset;
6467 cfun->machine->frame.fp_save_offset = offset - total_size;
6469 else
6471 cfun->machine->frame.fp_sp_offset = 0;
6472 cfun->machine->frame.fp_save_offset = 0;
6475 /* Ok, we're done. */
6476 return total_size;
6479 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame
6480 pointer or argument pointer. TO is either the stack pointer or
6481 hard frame pointer. */
6483 HOST_WIDE_INT
6484 mips_initial_elimination_offset (int from, int to)
6486 HOST_WIDE_INT offset;
6488 compute_frame_size (get_frame_size ());
6490 /* Set OFFSET to the offset from the stack pointer. */
6491 switch (from)
6493 case FRAME_POINTER_REGNUM:
6494 offset = 0;
6495 break;
6497 case ARG_POINTER_REGNUM:
6498 offset = cfun->machine->frame.total_size;
6499 if (TARGET_NEWABI)
6500 offset -= current_function_pretend_args_size;
6501 break;
6503 default:
6504 abort ();
6507 if (TARGET_MIPS16 && to == HARD_FRAME_POINTER_REGNUM)
6508 offset -= cfun->machine->frame.args_size;
6510 return offset;
6513 /* Implement RETURN_ADDR_RTX. Note, we do not support moving
6514 back to a previous frame. */
6516 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
6518 if (count != 0)
6519 return const0_rtx;
6521 return get_hard_reg_initial_val (Pmode, GP_REG_FIRST + 31);
6524 /* Use FN to save or restore register REGNO. MODE is the register's
6525 mode and OFFSET is the offset of its save slot from the current
6526 stack pointer. */
6528 static void
6529 mips_save_restore_reg (enum machine_mode mode, int regno,
6530 HOST_WIDE_INT offset, mips_save_restore_fn fn)
6532 rtx mem;
6534 mem = gen_rtx_MEM (mode, plus_constant (stack_pointer_rtx, offset));
6535 if (!current_function_calls_eh_return)
6536 RTX_UNCHANGING_P (mem) = 1;
6538 fn (gen_rtx_REG (mode, regno), mem);
6542 /* Call FN for each register that is saved by the current function.
6543 SP_OFFSET is the offset of the current stack pointer from the start
6544 of the frame. */
6546 static void
6547 mips_for_each_saved_reg (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
6549 #define BITSET_P(VALUE, BIT) (((VALUE) & (1L << (BIT))) != 0)
6551 enum machine_mode fpr_mode;
6552 HOST_WIDE_INT offset;
6553 int regno;
6555 /* Save registers starting from high to low. The debuggers prefer at least
6556 the return register be stored at func+4, and also it allows us not to
6557 need a nop in the epilog if at least one register is reloaded in
6558 addition to return address. */
6559 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
6560 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
6561 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
6563 mips_save_restore_reg (gpr_mode, regno, offset, fn);
6564 offset -= GET_MODE_SIZE (gpr_mode);
6567 /* This loop must iterate over the same space as its companion in
6568 compute_frame_size. */
6569 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
6570 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
6571 for (regno = (FP_REG_LAST - FP_INC + 1);
6572 regno >= FP_REG_FIRST;
6573 regno -= FP_INC)
6574 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
6576 mips_save_restore_reg (fpr_mode, regno, offset, fn);
6577 offset -= GET_MODE_SIZE (fpr_mode);
6579 #undef BITSET_P
6582 /* If we're generating n32 or n64 abicalls, and the current function
6583 does not use $28 as its global pointer, emit a cplocal directive.
6584 Use pic_offset_table_rtx as the argument to the directive. */
6586 static void
6587 mips_output_cplocal (void)
6589 if (!TARGET_EXPLICIT_RELOCS
6590 && cfun->machine->global_pointer > 0
6591 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
6592 output_asm_insn (".cplocal %+", 0);
6595 /* If we're generating n32 or n64 abicalls, emit instructions
6596 to set up the global pointer. */
6598 static void
6599 mips_emit_loadgp (void)
6601 if (TARGET_ABICALLS && TARGET_NEWABI && cfun->machine->global_pointer > 0)
6603 rtx addr, offset, incoming_address;
6605 addr = XEXP (DECL_RTL (current_function_decl), 0);
6606 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
6607 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
6608 emit_insn (gen_loadgp (offset, incoming_address));
6609 if (!TARGET_EXPLICIT_RELOCS)
6610 emit_insn (gen_loadgp_blockage ());
6614 /* Set up the stack and frame (if desired) for the function. */
6616 static void
6617 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6619 const char *fnname;
6620 HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
6622 /* ??? When is this really needed? At least the GNU assembler does not
6623 need the source filename more than once in the file, beyond what is
6624 emitted by the debug information. */
6625 if (!TARGET_GAS)
6626 ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
6628 #ifdef SDB_DEBUGGING_INFO
6629 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6630 ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl), 0);
6631 #endif
6633 /* In mips16 mode, we may need to generate a 32 bit to handle
6634 floating point arguments. The linker will arrange for any 32 bit
6635 functions to call this stub, which will then jump to the 16 bit
6636 function proper. */
6637 if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6638 && current_function_args_info.fp_code != 0)
6639 build_mips16_function_stub (file);
6641 if (!FUNCTION_NAME_ALREADY_DECLARED)
6643 /* Get the function name the same way that toplev.c does before calling
6644 assemble_start_function. This is needed so that the name used here
6645 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6646 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6648 if (!flag_inhibit_size_directive)
6650 fputs ("\t.ent\t", file);
6651 assemble_name (file, fnname);
6652 fputs ("\n", file);
6655 assemble_name (file, fnname);
6656 fputs (":\n", file);
6659 if (!flag_inhibit_size_directive)
6661 /* .frame FRAMEREG, FRAMESIZE, RETREG */
6662 fprintf (file,
6663 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
6664 "# vars= " HOST_WIDE_INT_PRINT_DEC ", regs= %d/%d"
6665 ", args= " HOST_WIDE_INT_PRINT_DEC
6666 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
6667 (reg_names[(frame_pointer_needed)
6668 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
6669 ((frame_pointer_needed && TARGET_MIPS16)
6670 ? tsize - cfun->machine->frame.args_size
6671 : tsize),
6672 reg_names[GP_REG_FIRST + 31],
6673 cfun->machine->frame.var_size,
6674 cfun->machine->frame.num_gp,
6675 cfun->machine->frame.num_fp,
6676 cfun->machine->frame.args_size,
6677 cfun->machine->frame.cprestore_size);
6679 /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
6680 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6681 cfun->machine->frame.mask,
6682 cfun->machine->frame.gp_save_offset);
6683 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6684 cfun->machine->frame.fmask,
6685 cfun->machine->frame.fp_save_offset);
6687 /* Require:
6688 OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
6689 HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
6692 if (TARGET_ABICALLS && !TARGET_NEWABI && cfun->machine->global_pointer > 0)
6694 /* Handle the initialization of $gp for SVR4 PIC. */
6695 if (!cfun->machine->all_noreorder_p)
6696 output_asm_insn ("%(.cpload\t%^%)", 0);
6697 else
6698 output_asm_insn ("%(.cpload\t%^\n\t%<", 0);
6700 else if (cfun->machine->all_noreorder_p)
6701 output_asm_insn ("%(%<", 0);
6703 /* Tell the assembler which register we're using as the global
6704 pointer. This is needed for thunks, since they can use either
6705 explicit relocs or assembler macros. */
6706 mips_output_cplocal ();
6709 /* Make the last instruction frame related and note that it performs
6710 the operation described by FRAME_PATTERN. */
6712 static void
6713 mips_set_frame_expr (rtx frame_pattern)
6715 rtx insn;
6717 insn = get_last_insn ();
6718 RTX_FRAME_RELATED_P (insn) = 1;
6719 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6720 frame_pattern,
6721 REG_NOTES (insn));
6725 /* Return a frame-related rtx that stores REG at MEM.
6726 REG must be a single register. */
6728 static rtx
6729 mips_frame_set (rtx mem, rtx reg)
6731 rtx set = gen_rtx_SET (VOIDmode, mem, reg);
6732 RTX_FRAME_RELATED_P (set) = 1;
6733 return set;
6737 /* Save register REG to MEM. Make the instruction frame-related. */
6739 static void
6740 mips_save_reg (rtx reg, rtx mem)
6742 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
6744 rtx x1, x2;
6746 if (mips_split_64bit_move_p (mem, reg))
6747 mips_split_64bit_move (mem, reg);
6748 else
6749 emit_move_insn (mem, reg);
6751 x1 = mips_frame_set (mips_subword (mem, 0), mips_subword (reg, 0));
6752 x2 = mips_frame_set (mips_subword (mem, 1), mips_subword (reg, 1));
6753 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
6755 else
6757 if (TARGET_MIPS16
6758 && REGNO (reg) != GP_REG_FIRST + 31
6759 && !M16_REG_P (REGNO (reg)))
6761 /* Save a non-mips16 register by moving it through a temporary.
6762 We don't need to do this for $31 since there's a special
6763 instruction for it. */
6764 emit_move_insn (MIPS_PROLOGUE_TEMP (GET_MODE (reg)), reg);
6765 emit_move_insn (mem, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
6767 else
6768 emit_move_insn (mem, reg);
6770 mips_set_frame_expr (mips_frame_set (mem, reg));
6775 /* Expand the prologue into a bunch of separate insns. */
6777 void
6778 mips_expand_prologue (void)
6780 HOST_WIDE_INT size;
6782 if (cfun->machine->global_pointer > 0)
6783 REGNO (pic_offset_table_rtx) = cfun->machine->global_pointer;
6785 size = compute_frame_size (get_frame_size ());
6787 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
6788 bytes beforehand; this is enough to cover the register save area
6789 without going out of range. */
6790 if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
6792 HOST_WIDE_INT step1;
6794 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
6795 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6796 stack_pointer_rtx,
6797 GEN_INT (-step1)))) = 1;
6798 size -= step1;
6799 mips_for_each_saved_reg (size, mips_save_reg);
6802 /* Allocate the rest of the frame. */
6803 if (size > 0)
6805 if (SMALL_OPERAND (-size))
6806 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6807 stack_pointer_rtx,
6808 GEN_INT (-size)))) = 1;
6809 else
6811 emit_move_insn (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
6812 if (TARGET_MIPS16)
6814 /* There are no instructions to add or subtract registers
6815 from the stack pointer, so use the frame pointer as a
6816 temporary. We should always be using a frame pointer
6817 in this case anyway. */
6818 if (!frame_pointer_needed)
6819 abort ();
6821 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6822 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
6823 hard_frame_pointer_rtx,
6824 MIPS_PROLOGUE_TEMP (Pmode)));
6825 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
6827 else
6828 emit_insn (gen_sub3_insn (stack_pointer_rtx,
6829 stack_pointer_rtx,
6830 MIPS_PROLOGUE_TEMP (Pmode)));
6832 /* Describe the combined effect of the previous instructions. */
6833 mips_set_frame_expr
6834 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6835 plus_constant (stack_pointer_rtx, -size)));
6839 /* Set up the frame pointer, if we're using one. In mips16 code,
6840 we point the frame pointer ahead of the outgoing argument area.
6841 This should allow more variables & incoming arguments to be
6842 accessed with unextended instructions. */
6843 if (frame_pointer_needed)
6845 if (TARGET_MIPS16 && cfun->machine->frame.args_size != 0)
6847 rtx offset = GEN_INT (cfun->machine->frame.args_size);
6848 RTX_FRAME_RELATED_P
6849 (emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
6850 stack_pointer_rtx,
6851 offset))) = 1;
6853 else
6854 RTX_FRAME_RELATED_P (emit_move_insn (hard_frame_pointer_rtx,
6855 stack_pointer_rtx)) = 1;
6858 /* If generating o32/o64 abicalls, save $gp on the stack. */
6859 if (TARGET_ABICALLS && !TARGET_NEWABI && !current_function_is_leaf)
6860 emit_insn (gen_cprestore (GEN_INT (current_function_outgoing_args_size)));
6862 mips_emit_loadgp ();
6864 /* If we are profiling, make sure no instructions are scheduled before
6865 the call to mcount. */
6867 if (current_function_profile)
6868 emit_insn (gen_blockage ());
6871 /* Do any necessary cleanup after a function to restore stack, frame,
6872 and regs. */
6874 #define RA_MASK BITMASK_HIGH /* 1 << 31 */
6876 static void
6877 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6878 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6880 /* Reinstate the normal $gp. */
6881 REGNO (pic_offset_table_rtx) = GLOBAL_POINTER_REGNUM;
6882 mips_output_cplocal ();
6884 if (cfun->machine->all_noreorder_p)
6886 /* Avoid using %>%) since it adds excess whitespace. */
6887 output_asm_insn (".set\tmacro", 0);
6888 output_asm_insn (".set\treorder", 0);
6889 set_noreorder = set_nomacro = 0;
6892 if (!FUNCTION_NAME_ALREADY_DECLARED && !flag_inhibit_size_directive)
6894 const char *fnname;
6896 /* Get the function name the same way that toplev.c does before calling
6897 assemble_start_function. This is needed so that the name used here
6898 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6899 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6900 fputs ("\t.end\t", file);
6901 assemble_name (file, fnname);
6902 fputs ("\n", file);
6906 /* Emit instructions to restore register REG from slot MEM. */
6908 static void
6909 mips_restore_reg (rtx reg, rtx mem)
6911 /* There's no mips16 instruction to load $31 directly. Load into
6912 $7 instead and adjust the return insn appropriately. */
6913 if (TARGET_MIPS16 && REGNO (reg) == GP_REG_FIRST + 31)
6914 reg = gen_rtx_REG (GET_MODE (reg), 7);
6916 if (TARGET_MIPS16 && !M16_REG_P (REGNO (reg)))
6918 /* Can't restore directly; move through a temporary. */
6919 emit_move_insn (MIPS_EPILOGUE_TEMP (GET_MODE (reg)), mem);
6920 emit_move_insn (reg, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
6922 else
6923 emit_move_insn (reg, mem);
6927 /* Expand the epilogue into a bunch of separate insns. SIBCALL_P is true
6928 if this epilogue precedes a sibling call, false if it is for a normal
6929 "epilogue" pattern. */
6931 void
6932 mips_expand_epilogue (int sibcall_p)
6934 HOST_WIDE_INT step1, step2;
6935 rtx base, target;
6937 if (!sibcall_p && mips_can_use_return_insn ())
6939 emit_jump_insn (gen_return ());
6940 return;
6943 /* Split the frame into two. STEP1 is the amount of stack we should
6944 deallocate before restoring the registers. STEP2 is the amount we
6945 should deallocate afterwards.
6947 Start off by assuming that no registers need to be restored. */
6948 step1 = cfun->machine->frame.total_size;
6949 step2 = 0;
6951 /* Work out which register holds the frame address. Account for the
6952 frame pointer offset used by mips16 code. */
6953 if (!frame_pointer_needed)
6954 base = stack_pointer_rtx;
6955 else
6957 base = hard_frame_pointer_rtx;
6958 if (TARGET_MIPS16)
6959 step1 -= cfun->machine->frame.args_size;
6962 /* If we need to restore registers, deallocate as much stack as
6963 possible in the second step without going out of range. */
6964 if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
6966 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
6967 step1 -= step2;
6970 /* Set TARGET to BASE + STEP1. */
6971 target = base;
6972 if (step1 > 0)
6974 rtx adjust;
6976 /* Get an rtx for STEP1 that we can add to BASE. */
6977 adjust = GEN_INT (step1);
6978 if (!SMALL_OPERAND (step1))
6980 emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), adjust);
6981 adjust = MIPS_EPILOGUE_TEMP (Pmode);
6984 /* Normal mode code can copy the result straight into $sp. */
6985 if (!TARGET_MIPS16)
6986 target = stack_pointer_rtx;
6988 emit_insn (gen_add3_insn (target, base, adjust));
6991 /* Copy TARGET into the stack pointer. */
6992 if (target != stack_pointer_rtx)
6993 emit_move_insn (stack_pointer_rtx, target);
6995 /* If we're using addressing macros for n32/n64 abicalls, $gp is
6996 implicitly used by all SYMBOL_REFs. We must emit a blockage
6997 insn before restoring it. */
6998 if (TARGET_ABICALLS && TARGET_NEWABI && !TARGET_EXPLICIT_RELOCS)
6999 emit_insn (gen_blockage ());
7001 /* Restore the registers. */
7002 mips_for_each_saved_reg (cfun->machine->frame.total_size - step2,
7003 mips_restore_reg);
7005 /* Deallocate the final bit of the frame. */
7006 if (step2 > 0)
7007 emit_insn (gen_add3_insn (stack_pointer_rtx,
7008 stack_pointer_rtx,
7009 GEN_INT (step2)));
7011 /* Add in the __builtin_eh_return stack adjustment. We need to
7012 use a temporary in mips16 code. */
7013 if (current_function_calls_eh_return)
7015 if (TARGET_MIPS16)
7017 emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
7018 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
7019 MIPS_EPILOGUE_TEMP (Pmode),
7020 EH_RETURN_STACKADJ_RTX));
7021 emit_move_insn (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
7023 else
7024 emit_insn (gen_add3_insn (stack_pointer_rtx,
7025 stack_pointer_rtx,
7026 EH_RETURN_STACKADJ_RTX));
7029 if (!sibcall_p)
7031 /* The mips16 loads the return address into $7, not $31. */
7032 if (TARGET_MIPS16 && (cfun->machine->frame.mask & RA_MASK) != 0)
7033 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
7034 GP_REG_FIRST + 7)));
7035 else
7036 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
7037 GP_REG_FIRST + 31)));
7041 /* Return nonzero if this function is known to have a null epilogue.
7042 This allows the optimizer to omit jumps to jumps if no stack
7043 was created. */
7046 mips_can_use_return_insn (void)
7048 tree return_type;
7050 if (! reload_completed)
7051 return 0;
7053 if (regs_ever_live[31] || current_function_profile)
7054 return 0;
7056 return_type = DECL_RESULT (current_function_decl);
7058 /* In mips16 mode, a function which returns a floating point value
7059 needs to arrange to copy the return value into the floating point
7060 registers. */
7061 if (TARGET_MIPS16
7062 && mips16_hard_float
7063 && ! aggregate_value_p (return_type, current_function_decl)
7064 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
7065 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
7066 return 0;
7068 if (cfun->machine->frame.initialized)
7069 return cfun->machine->frame.total_size == 0;
7071 return compute_frame_size (get_frame_size ()) == 0;
7074 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
7075 in order to avoid duplicating too much logic from elsewhere. */
7077 static void
7078 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
7079 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7080 tree function)
7082 rtx this, temp1, temp2, insn, fnaddr;
7084 /* Pretend to be a post-reload pass while generating rtl. */
7085 no_new_pseudos = 1;
7086 reload_completed = 1;
7088 /* Pick a global pointer for -mabicalls. Use $15 rather than $28
7089 for TARGET_NEWABI since the latter is a call-saved register. */
7090 if (TARGET_ABICALLS)
7091 cfun->machine->global_pointer
7092 = REGNO (pic_offset_table_rtx)
7093 = TARGET_NEWABI ? 15 : GLOBAL_POINTER_REGNUM;
7095 /* Set up the global pointer for n32 or n64 abicalls. */
7096 mips_emit_loadgp ();
7098 /* We need two temporary registers in some cases. */
7099 temp1 = gen_rtx_REG (Pmode, 2);
7100 temp2 = gen_rtx_REG (Pmode, 3);
7102 /* Find out which register contains the "this" pointer. */
7103 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
7104 this = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
7105 else
7106 this = gen_rtx_REG (Pmode, GP_ARG_FIRST);
7108 /* Add DELTA to THIS. */
7109 if (delta != 0)
7111 rtx offset = GEN_INT (delta);
7112 if (!SMALL_OPERAND (delta))
7114 emit_move_insn (temp1, offset);
7115 offset = temp1;
7117 emit_insn (gen_add3_insn (this, this, offset));
7120 /* If needed, add *(*THIS + VCALL_OFFSET) to THIS. */
7121 if (vcall_offset != 0)
7123 rtx addr;
7125 /* Set TEMP1 to *THIS. */
7126 emit_move_insn (temp1, gen_rtx_MEM (Pmode, this));
7128 /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET. */
7129 if (SMALL_OPERAND (vcall_offset))
7130 addr = gen_rtx_PLUS (Pmode, temp1, GEN_INT (vcall_offset));
7131 else if (TARGET_MIPS16)
7133 /* Load the full offset into a register so that we can use
7134 an unextended instruction for the load itself. */
7135 emit_move_insn (temp2, GEN_INT (vcall_offset));
7136 emit_insn (gen_add3_insn (temp1, temp1, temp2));
7137 addr = temp1;
7139 else
7141 /* Load the high part of the offset into a register and
7142 leave the low part for the address. */
7143 emit_move_insn (temp2, GEN_INT (CONST_HIGH_PART (vcall_offset)));
7144 emit_insn (gen_add3_insn (temp1, temp1, temp2));
7145 addr = gen_rtx_PLUS (Pmode, temp1,
7146 GEN_INT (CONST_LOW_PART (vcall_offset)));
7149 /* Load the offset and add it to THIS. */
7150 emit_move_insn (temp1, gen_rtx_MEM (Pmode, addr));
7151 emit_insn (gen_add3_insn (this, this, temp1));
7154 /* Jump to the target function. Use a sibcall if direct jumps are
7155 allowed, otherwise load the address into a register first. */
7156 fnaddr = XEXP (DECL_RTL (function), 0);
7157 if (TARGET_MIPS16 || TARGET_ABICALLS || TARGET_LONG_CALLS)
7159 /* This is messy. gas treats "la $25,foo" as part of a call
7160 sequence and may allow a global "foo" to be lazily bound.
7161 The general move patterns therefore reject this combination.
7163 In this context, lazy binding would actually be OK for o32 and o64,
7164 but it's still wrong for n32 and n64; see mips_load_call_address.
7165 We must therefore load the address via a temporary register if
7166 mips_dangerous_for_la25_p.
7168 If we jump to the temporary register rather than $25, the assembler
7169 can use the move insn to fill the jump's delay slot. */
7170 if (TARGET_ABICALLS && !mips_dangerous_for_la25_p (fnaddr))
7171 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
7172 mips_load_call_address (temp1, fnaddr, true);
7174 if (TARGET_ABICALLS && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
7175 emit_move_insn (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
7176 emit_jump_insn (gen_indirect_jump (temp1));
7178 else
7180 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
7181 SIBLING_CALL_P (insn) = 1;
7184 /* Run just enough of rest_of_compilation. This sequence was
7185 "borrowed" from alpha.c. */
7186 insn = get_insns ();
7187 insn_locators_initialize ();
7188 split_all_insns_noflow ();
7189 if (TARGET_MIPS16)
7190 mips16_lay_out_constants ();
7191 shorten_branches (insn);
7192 final_start_function (insn, file, 1);
7193 final (insn, file, 1, 0);
7194 final_end_function ();
7196 /* Clean up the vars set above. Note that final_end_function resets
7197 the global pointer for us. */
7198 reload_completed = 0;
7199 no_new_pseudos = 0;
7202 /* Returns nonzero if X contains a SYMBOL_REF. */
7204 static int
7205 symbolic_expression_p (rtx x)
7207 if (GET_CODE (x) == SYMBOL_REF)
7208 return 1;
7210 if (GET_CODE (x) == CONST)
7211 return symbolic_expression_p (XEXP (x, 0));
7213 if (UNARY_P (x))
7214 return symbolic_expression_p (XEXP (x, 0));
7216 if (ARITHMETIC_P (x))
7217 return (symbolic_expression_p (XEXP (x, 0))
7218 || symbolic_expression_p (XEXP (x, 1)));
7220 return 0;
7223 /* Choose the section to use for the constant rtx expression X that has
7224 mode MODE. */
7226 static void
7227 mips_select_rtx_section (enum machine_mode mode, rtx x,
7228 unsigned HOST_WIDE_INT align)
7230 if (TARGET_MIPS16)
7232 /* In mips16 mode, the constant table always goes in the same section
7233 as the function, so that constants can be loaded using PC relative
7234 addressing. */
7235 function_section (current_function_decl);
7237 else if (TARGET_EMBEDDED_DATA)
7239 /* For embedded applications, always put constants in read-only data,
7240 in order to reduce RAM usage. */
7241 mergeable_constant_section (mode, align, 0);
7243 else
7245 /* For hosted applications, always put constants in small data if
7246 possible, as this gives the best performance. */
7247 /* ??? Consider using mergeable small data sections. */
7249 if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
7250 && mips_section_threshold > 0)
7251 named_section (0, ".sdata", 0);
7252 else if (flag_pic && symbolic_expression_p (x))
7254 if (targetm.have_named_sections)
7255 named_section (0, ".data.rel.ro", 3);
7256 else
7257 data_section ();
7259 else
7260 mergeable_constant_section (mode, align, 0);
7264 /* Choose the section to use for DECL. RELOC is true if its value contains
7265 any relocatable expression. */
7267 static void
7268 mips_select_section (tree decl, int reloc,
7269 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7271 if (targetm.have_named_sections)
7272 default_elf_select_section (decl, reloc, align);
7273 else
7274 /* The native irix o32 assembler doesn't support named sections. */
7275 default_select_section (decl, reloc, align);
7279 /* Implement TARGET_IN_SMALL_DATA_P. Return true if it would be safe to
7280 access DECL using %gp_rel(...)($gp). */
7282 static bool
7283 mips_in_small_data_p (tree decl)
7285 HOST_WIDE_INT size;
7287 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
7288 return false;
7290 /* We don't yet generate small-data references for -mabicalls. See related
7291 -G handling in override_options. */
7292 if (TARGET_ABICALLS)
7293 return false;
7295 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
7297 const char *name;
7299 /* Reject anything that isn't in a known small-data section. */
7300 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7301 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
7302 return false;
7304 /* If a symbol is defined externally, the assembler will use the
7305 usual -G rules when deciding how to implement macros. */
7306 if (TARGET_EXPLICIT_RELOCS || !DECL_EXTERNAL (decl))
7307 return true;
7309 else if (TARGET_EMBEDDED_DATA)
7311 /* Don't put constants into the small data section: we want them
7312 to be in ROM rather than RAM. */
7313 if (TREE_CODE (decl) != VAR_DECL)
7314 return false;
7316 if (TREE_READONLY (decl)
7317 && !TREE_SIDE_EFFECTS (decl)
7318 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
7319 return false;
7322 size = int_size_in_bytes (TREE_TYPE (decl));
7323 return (size > 0 && size <= mips_section_threshold);
7326 /* See whether VALTYPE is a record whose fields should be returned in
7327 floating-point registers. If so, return the number of fields and
7328 list them in FIELDS (which should have two elements). Return 0
7329 otherwise.
7331 For n32 & n64, a structure with one or two fields is returned in
7332 floating-point registers as long as every field has a floating-point
7333 type. */
7335 static int
7336 mips_fpr_return_fields (tree valtype, tree *fields)
7338 tree field;
7339 int i;
7341 if (!TARGET_NEWABI)
7342 return 0;
7344 if (TREE_CODE (valtype) != RECORD_TYPE)
7345 return 0;
7347 i = 0;
7348 for (field = TYPE_FIELDS (valtype); field != 0; field = TREE_CHAIN (field))
7350 if (TREE_CODE (field) != FIELD_DECL)
7351 continue;
7353 if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE)
7354 return 0;
7356 if (i == 2)
7357 return 0;
7359 fields[i++] = field;
7361 return i;
7365 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
7366 a value in the most significant part of $2/$3 if:
7368 - the target is big-endian;
7370 - the value has a structure or union type (we generalize this to
7371 cover aggregates from other languages too); and
7373 - the structure is not returned in floating-point registers. */
7375 static bool
7376 mips_return_in_msb (tree valtype)
7378 tree fields[2];
7380 return (TARGET_NEWABI
7381 && TARGET_BIG_ENDIAN
7382 && AGGREGATE_TYPE_P (valtype)
7383 && mips_fpr_return_fields (valtype, fields) == 0);
7387 /* Return a composite value in a pair of floating-point registers.
7388 MODE1 and OFFSET1 are the mode and byte offset for the first value,
7389 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
7390 complete value.
7392 For n32 & n64, $f0 always holds the first value and $f2 the second.
7393 Otherwise the values are packed together as closely as possible. */
7395 static rtx
7396 mips_return_fpr_pair (enum machine_mode mode,
7397 enum machine_mode mode1, HOST_WIDE_INT offset1,
7398 enum machine_mode mode2, HOST_WIDE_INT offset2)
7400 int inc;
7402 inc = (TARGET_NEWABI ? 2 : FP_INC);
7403 return gen_rtx_PARALLEL
7404 (mode,
7405 gen_rtvec (2,
7406 gen_rtx_EXPR_LIST (VOIDmode,
7407 gen_rtx_REG (mode1, FP_RETURN),
7408 GEN_INT (offset1)),
7409 gen_rtx_EXPR_LIST (VOIDmode,
7410 gen_rtx_REG (mode2, FP_RETURN + inc),
7411 GEN_INT (offset2))));
7416 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
7417 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
7418 VALTYPE is null and MODE is the mode of the return value. */
7421 mips_function_value (tree valtype, tree func ATTRIBUTE_UNUSED,
7422 enum machine_mode mode)
7424 if (valtype)
7426 tree fields[2];
7427 int unsignedp;
7429 mode = TYPE_MODE (valtype);
7430 unsignedp = TYPE_UNSIGNED (valtype);
7432 /* Since we define TARGET_PROMOTE_FUNCTION_RETURN that returns
7433 true, we must promote the mode just as PROMOTE_MODE does. */
7434 mode = promote_mode (valtype, mode, &unsignedp, 1);
7436 /* Handle structures whose fields are returned in $f0/$f2. */
7437 switch (mips_fpr_return_fields (valtype, fields))
7439 case 1:
7440 return gen_rtx_REG (mode, FP_RETURN);
7442 case 2:
7443 return mips_return_fpr_pair (mode,
7444 TYPE_MODE (TREE_TYPE (fields[0])),
7445 int_byte_position (fields[0]),
7446 TYPE_MODE (TREE_TYPE (fields[1])),
7447 int_byte_position (fields[1]));
7450 /* If a value is passed in the most significant part of a register, see
7451 whether we have to round the mode up to a whole number of words. */
7452 if (mips_return_in_msb (valtype))
7454 HOST_WIDE_INT size = int_size_in_bytes (valtype);
7455 if (size % UNITS_PER_WORD != 0)
7457 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
7458 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
7463 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7464 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
7465 return gen_rtx_REG (mode, FP_RETURN);
7467 /* Handle long doubles for n32 & n64. */
7468 if (mode == TFmode)
7469 return mips_return_fpr_pair (mode,
7470 DImode, 0,
7471 DImode, GET_MODE_SIZE (mode) / 2);
7473 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
7474 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)
7475 return mips_return_fpr_pair (mode,
7476 GET_MODE_INNER (mode), 0,
7477 GET_MODE_INNER (mode),
7478 GET_MODE_SIZE (mode) / 2);
7480 return gen_rtx_REG (mode, GP_RETURN);
7483 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE. Return
7484 nonzero when an argument must be passed by reference. */
7487 function_arg_pass_by_reference (const CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
7488 enum machine_mode mode, tree type,
7489 int named ATTRIBUTE_UNUSED)
7491 int size;
7493 /* The EABI is the only one to pass args by reference. */
7494 if (mips_abi != ABI_EABI)
7495 return 0;
7497 /* ??? How should SCmode be handled? */
7498 if (type == NULL_TREE || mode == DImode || mode == DFmode)
7499 return 0;
7501 size = int_size_in_bytes (type);
7502 return size == -1 || size > UNITS_PER_WORD;
7505 /* Return the class of registers for which a mode change from FROM to TO
7506 is invalid.
7508 In little-endian mode, the hi-lo registers are numbered backwards,
7509 so (subreg:SI (reg:DI hi) 0) gets the high word instead of the low
7510 word as intended.
7512 Similarly, when using paired floating-point registers, the first
7513 register holds the low word, regardless of endianness. So in big
7514 endian mode, (subreg:SI (reg:DF $f0) 0) does not get the high word
7515 as intended.
7517 Also, loading a 32-bit value into a 64-bit floating-point register
7518 will not sign-extend the value, despite what LOAD_EXTEND_OP says.
7519 We can't allow 64-bit float registers to change from a 32-bit
7520 mode to a 64-bit mode. */
7522 bool
7523 mips_cannot_change_mode_class (enum machine_mode from,
7524 enum machine_mode to, enum reg_class class)
7526 if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
7528 if (TARGET_BIG_ENDIAN)
7529 return reg_classes_intersect_p (FP_REGS, class);
7530 if (TARGET_FLOAT64)
7531 return reg_classes_intersect_p (HI_AND_FP_REGS, class);
7532 return reg_classes_intersect_p (HI_REG, class);
7534 return false;
7537 /* Return true if X should not be moved directly into register $25.
7538 We need this because many versions of GAS will treat "la $25,foo" as
7539 part of a call sequence and so allow a global "foo" to be lazily bound. */
7541 bool
7542 mips_dangerous_for_la25_p (rtx x)
7544 HOST_WIDE_INT offset;
7546 if (TARGET_EXPLICIT_RELOCS)
7547 return false;
7549 mips_split_const (x, &x, &offset);
7550 return global_got_operand (x, VOIDmode);
7553 /* Implement PREFERRED_RELOAD_CLASS. */
7555 enum reg_class
7556 mips_preferred_reload_class (rtx x, enum reg_class class)
7558 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, class))
7559 return LEA_REGS;
7561 if (TARGET_HARD_FLOAT
7562 && FLOAT_MODE_P (GET_MODE (x))
7563 && reg_class_subset_p (FP_REGS, class))
7564 return FP_REGS;
7566 if (reg_class_subset_p (GR_REGS, class))
7567 class = GR_REGS;
7569 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, class))
7570 class = M16_REGS;
7572 return class;
7575 /* This function returns the register class required for a secondary
7576 register when copying between one of the registers in CLASS, and X,
7577 using MODE. If IN_P is nonzero, the copy is going from X to the
7578 register, otherwise the register is the source. A return value of
7579 NO_REGS means that no secondary register is required. */
7581 enum reg_class
7582 mips_secondary_reload_class (enum reg_class class,
7583 enum machine_mode mode, rtx x, int in_p)
7585 enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
7586 int regno = -1;
7587 int gp_reg_p;
7589 if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
7590 regno = true_regnum (x);
7592 gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
7594 if (mips_dangerous_for_la25_p (x))
7596 gr_regs = LEA_REGS;
7597 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], 25))
7598 return gr_regs;
7601 /* Copying from HI or LO to anywhere other than a general register
7602 requires a general register. */
7603 if (class == HI_REG || class == LO_REG || class == MD_REGS)
7605 if (TARGET_MIPS16 && in_p)
7607 /* We can't really copy to HI or LO at all in mips16 mode. */
7608 return M16_REGS;
7610 return gp_reg_p ? NO_REGS : gr_regs;
7612 if (MD_REG_P (regno))
7614 if (TARGET_MIPS16 && ! in_p)
7616 /* We can't really copy to HI or LO at all in mips16 mode. */
7617 return M16_REGS;
7619 return class == gr_regs ? NO_REGS : gr_regs;
7622 /* We can only copy a value to a condition code register from a
7623 floating point register, and even then we require a scratch
7624 floating point register. We can only copy a value out of a
7625 condition code register into a general register. */
7626 if (class == ST_REGS)
7628 if (in_p)
7629 return FP_REGS;
7630 return gp_reg_p ? NO_REGS : gr_regs;
7632 if (ST_REG_P (regno))
7634 if (! in_p)
7635 return FP_REGS;
7636 return class == gr_regs ? NO_REGS : gr_regs;
7639 if (class == FP_REGS)
7641 if (GET_CODE (x) == MEM)
7643 /* In this case we can use lwc1, swc1, ldc1 or sdc1. */
7644 return NO_REGS;
7646 else if (CONSTANT_P (x) && GET_MODE_CLASS (mode) == MODE_FLOAT)
7648 /* We can use the l.s and l.d macros to load floating-point
7649 constants. ??? For l.s, we could probably get better
7650 code by returning GR_REGS here. */
7651 return NO_REGS;
7653 else if (gp_reg_p || x == CONST0_RTX (mode))
7655 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
7656 return NO_REGS;
7658 else if (FP_REG_P (regno))
7660 /* In this case we can use mov.s or mov.d. */
7661 return NO_REGS;
7663 else
7665 /* Otherwise, we need to reload through an integer register. */
7666 return gr_regs;
7670 /* In mips16 mode, going between memory and anything but M16_REGS
7671 requires an M16_REG. */
7672 if (TARGET_MIPS16)
7674 if (class != M16_REGS && class != M16_NA_REGS)
7676 if (gp_reg_p)
7677 return NO_REGS;
7678 return M16_REGS;
7680 if (! gp_reg_p)
7682 if (class == M16_REGS || class == M16_NA_REGS)
7683 return NO_REGS;
7684 return M16_REGS;
7688 return NO_REGS;
7691 /* Implement CLASS_MAX_NREGS.
7693 Usually all registers are word-sized. The only supported exception
7694 is -mgp64 -msingle-float, which has 64-bit words but 32-bit float
7695 registers. A word-based calculation is correct even in that case,
7696 since -msingle-float disallows multi-FPR values. */
7699 mips_class_max_nregs (enum reg_class class ATTRIBUTE_UNUSED,
7700 enum machine_mode mode)
7702 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7705 bool
7706 mips_valid_pointer_mode (enum machine_mode mode)
7708 return (mode == SImode || (TARGET_64BIT && mode == DImode));
7712 /* If we can access small data directly (using gp-relative relocation
7713 operators) return the small data pointer, otherwise return null.
7715 For each mips16 function which refers to GP relative symbols, we
7716 use a pseudo register, initialized at the start of the function, to
7717 hold the $gp value. */
7719 static rtx
7720 mips16_gp_pseudo_reg (void)
7722 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
7724 rtx unspec;
7725 rtx insn, scan;
7727 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
7728 RTX_UNCHANGING_P (cfun->machine->mips16_gp_pseudo_rtx) = 1;
7730 /* We want to initialize this to a value which gcc will believe
7731 is constant. */
7732 start_sequence ();
7733 unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_GP);
7734 emit_move_insn (cfun->machine->mips16_gp_pseudo_rtx,
7735 gen_rtx_CONST (Pmode, unspec));
7736 insn = get_insns ();
7737 end_sequence ();
7739 push_topmost_sequence ();
7740 /* We need to emit the initialization after the FUNCTION_BEG
7741 note, so that it will be integrated. */
7742 for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
7743 if (GET_CODE (scan) == NOTE
7744 && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
7745 break;
7746 if (scan == NULL_RTX)
7747 scan = get_insns ();
7748 insn = emit_insn_after (insn, scan);
7749 pop_topmost_sequence ();
7752 return cfun->machine->mips16_gp_pseudo_rtx;
7755 /* Write out code to move floating point arguments in or out of
7756 general registers. Output the instructions to FILE. FP_CODE is
7757 the code describing which arguments are present (see the comment at
7758 the definition of CUMULATIVE_ARGS in mips.h). FROM_FP_P is nonzero if
7759 we are copying from the floating point registers. */
7761 static void
7762 mips16_fp_args (FILE *file, int fp_code, int from_fp_p)
7764 const char *s;
7765 int gparg, fparg;
7766 unsigned int f;
7768 /* This code only works for the original 32 bit ABI and the O64 ABI. */
7769 if (!TARGET_OLDABI)
7770 abort ();
7772 if (from_fp_p)
7773 s = "mfc1";
7774 else
7775 s = "mtc1";
7776 gparg = GP_ARG_FIRST;
7777 fparg = FP_ARG_FIRST;
7778 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7780 if ((f & 3) == 1)
7782 if ((fparg & 1) != 0)
7783 ++fparg;
7784 fprintf (file, "\t%s\t%s,%s\n", s,
7785 reg_names[gparg], reg_names[fparg]);
7787 else if ((f & 3) == 2)
7789 if (TARGET_64BIT)
7790 fprintf (file, "\td%s\t%s,%s\n", s,
7791 reg_names[gparg], reg_names[fparg]);
7792 else
7794 if ((fparg & 1) != 0)
7795 ++fparg;
7796 if (TARGET_BIG_ENDIAN)
7797 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7798 reg_names[gparg], reg_names[fparg + 1], s,
7799 reg_names[gparg + 1], reg_names[fparg]);
7800 else
7801 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7802 reg_names[gparg], reg_names[fparg], s,
7803 reg_names[gparg + 1], reg_names[fparg + 1]);
7804 ++gparg;
7805 ++fparg;
7808 else
7809 abort ();
7811 ++gparg;
7812 ++fparg;
7816 /* Build a mips16 function stub. This is used for functions which
7817 take arguments in the floating point registers. It is 32 bit code
7818 that moves the floating point args into the general registers, and
7819 then jumps to the 16 bit code. */
7821 static void
7822 build_mips16_function_stub (FILE *file)
7824 const char *fnname;
7825 char *secname, *stubname;
7826 tree stubid, stubdecl;
7827 int need_comma;
7828 unsigned int f;
7830 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7831 secname = (char *) alloca (strlen (fnname) + 20);
7832 sprintf (secname, ".mips16.fn.%s", fnname);
7833 stubname = (char *) alloca (strlen (fnname) + 20);
7834 sprintf (stubname, "__fn_stub_%s", fnname);
7835 stubid = get_identifier (stubname);
7836 stubdecl = build_decl (FUNCTION_DECL, stubid,
7837 build_function_type (void_type_node, NULL_TREE));
7838 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
7840 fprintf (file, "\t# Stub function for %s (", current_function_name ());
7841 need_comma = 0;
7842 for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
7844 fprintf (file, "%s%s",
7845 need_comma ? ", " : "",
7846 (f & 3) == 1 ? "float" : "double");
7847 need_comma = 1;
7849 fprintf (file, ")\n");
7851 fprintf (file, "\t.set\tnomips16\n");
7852 function_section (stubdecl);
7853 ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
7855 /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
7856 within a .ent, and we can not emit another .ent. */
7857 if (!FUNCTION_NAME_ALREADY_DECLARED)
7859 fputs ("\t.ent\t", file);
7860 assemble_name (file, stubname);
7861 fputs ("\n", file);
7864 assemble_name (file, stubname);
7865 fputs (":\n", file);
7867 /* We don't want the assembler to insert any nops here. */
7868 fprintf (file, "\t.set\tnoreorder\n");
7870 mips16_fp_args (file, current_function_args_info.fp_code, 1);
7872 fprintf (asm_out_file, "\t.set\tnoat\n");
7873 fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
7874 assemble_name (file, fnname);
7875 fprintf (file, "\n");
7876 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
7877 fprintf (asm_out_file, "\t.set\tat\n");
7879 /* Unfortunately, we can't fill the jump delay slot. We can't fill
7880 with one of the mfc1 instructions, because the result is not
7881 available for one instruction, so if the very first instruction
7882 in the function refers to the register, it will see the wrong
7883 value. */
7884 fprintf (file, "\tnop\n");
7886 fprintf (file, "\t.set\treorder\n");
7888 if (!FUNCTION_NAME_ALREADY_DECLARED)
7890 fputs ("\t.end\t", file);
7891 assemble_name (file, stubname);
7892 fputs ("\n", file);
7895 fprintf (file, "\t.set\tmips16\n");
7897 function_section (current_function_decl);
7900 /* We keep a list of functions for which we have already built stubs
7901 in build_mips16_call_stub. */
7903 struct mips16_stub
7905 struct mips16_stub *next;
7906 char *name;
7907 int fpret;
7910 static struct mips16_stub *mips16_stubs;
7912 /* Build a call stub for a mips16 call. A stub is needed if we are
7913 passing any floating point values which should go into the floating
7914 point registers. If we are, and the call turns out to be to a 32
7915 bit function, the stub will be used to move the values into the
7916 floating point registers before calling the 32 bit function. The
7917 linker will magically adjust the function call to either the 16 bit
7918 function or the 32 bit stub, depending upon where the function call
7919 is actually defined.
7921 Similarly, we need a stub if the return value might come back in a
7922 floating point register.
7924 RETVAL is the location of the return value, or null if this is
7925 a call rather than a call_value. FN is the address of the
7926 function and ARG_SIZE is the size of the arguments. FP_CODE
7927 is the code built by function_arg. This function returns a nonzero
7928 value if it builds the call instruction itself. */
7931 build_mips16_call_stub (rtx retval, rtx fn, rtx arg_size, int fp_code)
7933 int fpret;
7934 const char *fnname;
7935 char *secname, *stubname;
7936 struct mips16_stub *l;
7937 tree stubid, stubdecl;
7938 int need_comma;
7939 unsigned int f;
7941 /* We don't need to do anything if we aren't in mips16 mode, or if
7942 we were invoked with the -msoft-float option. */
7943 if (! TARGET_MIPS16 || ! mips16_hard_float)
7944 return 0;
7946 /* Figure out whether the value might come back in a floating point
7947 register. */
7948 fpret = (retval != 0
7949 && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
7950 && GET_MODE_SIZE (GET_MODE (retval)) <= UNITS_PER_FPVALUE);
7952 /* We don't need to do anything if there were no floating point
7953 arguments and the value will not be returned in a floating point
7954 register. */
7955 if (fp_code == 0 && ! fpret)
7956 return 0;
7958 /* We don't need to do anything if this is a call to a special
7959 mips16 support function. */
7960 if (GET_CODE (fn) == SYMBOL_REF
7961 && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
7962 return 0;
7964 /* This code will only work for o32 and o64 abis. The other ABI's
7965 require more sophisticated support. */
7966 if (!TARGET_OLDABI)
7967 abort ();
7969 /* We can only handle SFmode and DFmode floating point return
7970 values. */
7971 if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
7972 abort ();
7974 /* If we're calling via a function pointer, then we must always call
7975 via a stub. There are magic stubs provided in libgcc.a for each
7976 of the required cases. Each of them expects the function address
7977 to arrive in register $2. */
7979 if (GET_CODE (fn) != SYMBOL_REF)
7981 char buf[30];
7982 tree id;
7983 rtx stub_fn, insn;
7985 /* ??? If this code is modified to support other ABI's, we need
7986 to handle PARALLEL return values here. */
7988 sprintf (buf, "__mips16_call_stub_%s%d",
7989 (fpret
7990 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
7991 : ""),
7992 fp_code);
7993 id = get_identifier (buf);
7994 stub_fn = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (id));
7996 emit_move_insn (gen_rtx_REG (Pmode, 2), fn);
7998 if (retval == NULL_RTX)
7999 insn = gen_call_internal (stub_fn, arg_size);
8000 else
8001 insn = gen_call_value_internal (retval, stub_fn, arg_size);
8002 insn = emit_call_insn (insn);
8004 /* Put the register usage information on the CALL. */
8005 if (GET_CODE (insn) != CALL_INSN)
8006 abort ();
8007 CALL_INSN_FUNCTION_USAGE (insn) =
8008 gen_rtx_EXPR_LIST (VOIDmode,
8009 gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 2)),
8010 CALL_INSN_FUNCTION_USAGE (insn));
8012 /* If we are handling a floating point return value, we need to
8013 save $18 in the function prologue. Putting a note on the
8014 call will mean that regs_ever_live[$18] will be true if the
8015 call is not eliminated, and we can check that in the prologue
8016 code. */
8017 if (fpret)
8018 CALL_INSN_FUNCTION_USAGE (insn) =
8019 gen_rtx_EXPR_LIST (VOIDmode,
8020 gen_rtx_USE (VOIDmode,
8021 gen_rtx_REG (word_mode, 18)),
8022 CALL_INSN_FUNCTION_USAGE (insn));
8024 /* Return 1 to tell the caller that we've generated the call
8025 insn. */
8026 return 1;
8029 /* We know the function we are going to call. If we have already
8030 built a stub, we don't need to do anything further. */
8032 fnname = XSTR (fn, 0);
8033 for (l = mips16_stubs; l != NULL; l = l->next)
8034 if (strcmp (l->name, fnname) == 0)
8035 break;
8037 if (l == NULL)
8039 /* Build a special purpose stub. When the linker sees a
8040 function call in mips16 code, it will check where the target
8041 is defined. If the target is a 32 bit call, the linker will
8042 search for the section defined here. It can tell which
8043 symbol this section is associated with by looking at the
8044 relocation information (the name is unreliable, since this
8045 might be a static function). If such a section is found, the
8046 linker will redirect the call to the start of the magic
8047 section.
8049 If the function does not return a floating point value, the
8050 special stub section is named
8051 .mips16.call.FNNAME
8053 If the function does return a floating point value, the stub
8054 section is named
8055 .mips16.call.fp.FNNAME
8058 secname = (char *) alloca (strlen (fnname) + 40);
8059 sprintf (secname, ".mips16.call.%s%s",
8060 fpret ? "fp." : "",
8061 fnname);
8062 stubname = (char *) alloca (strlen (fnname) + 20);
8063 sprintf (stubname, "__call_stub_%s%s",
8064 fpret ? "fp_" : "",
8065 fnname);
8066 stubid = get_identifier (stubname);
8067 stubdecl = build_decl (FUNCTION_DECL, stubid,
8068 build_function_type (void_type_node, NULL_TREE));
8069 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8071 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8072 (fpret
8073 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8074 : ""),
8075 fnname);
8076 need_comma = 0;
8077 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8079 fprintf (asm_out_file, "%s%s",
8080 need_comma ? ", " : "",
8081 (f & 3) == 1 ? "float" : "double");
8082 need_comma = 1;
8084 fprintf (asm_out_file, ")\n");
8086 fprintf (asm_out_file, "\t.set\tnomips16\n");
8087 assemble_start_function (stubdecl, stubname);
8089 if (!FUNCTION_NAME_ALREADY_DECLARED)
8091 fputs ("\t.ent\t", asm_out_file);
8092 assemble_name (asm_out_file, stubname);
8093 fputs ("\n", asm_out_file);
8095 assemble_name (asm_out_file, stubname);
8096 fputs (":\n", asm_out_file);
8099 /* We build the stub code by hand. That's the only way we can
8100 do it, since we can't generate 32 bit code during a 16 bit
8101 compilation. */
8103 /* We don't want the assembler to insert any nops here. */
8104 fprintf (asm_out_file, "\t.set\tnoreorder\n");
8106 mips16_fp_args (asm_out_file, fp_code, 0);
8108 if (! fpret)
8110 fprintf (asm_out_file, "\t.set\tnoat\n");
8111 fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
8112 fnname);
8113 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8114 fprintf (asm_out_file, "\t.set\tat\n");
8115 /* Unfortunately, we can't fill the jump delay slot. We
8116 can't fill with one of the mtc1 instructions, because the
8117 result is not available for one instruction, so if the
8118 very first instruction in the function refers to the
8119 register, it will see the wrong value. */
8120 fprintf (asm_out_file, "\tnop\n");
8122 else
8124 fprintf (asm_out_file, "\tmove\t%s,%s\n",
8125 reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
8126 fprintf (asm_out_file, "\tjal\t%s\n", fnname);
8127 /* As above, we can't fill the delay slot. */
8128 fprintf (asm_out_file, "\tnop\n");
8129 if (GET_MODE (retval) == SFmode)
8130 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8131 reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
8132 else
8134 if (TARGET_BIG_ENDIAN)
8136 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8137 reg_names[GP_REG_FIRST + 2],
8138 reg_names[FP_REG_FIRST + 1]);
8139 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8140 reg_names[GP_REG_FIRST + 3],
8141 reg_names[FP_REG_FIRST + 0]);
8143 else
8145 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8146 reg_names[GP_REG_FIRST + 2],
8147 reg_names[FP_REG_FIRST + 0]);
8148 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8149 reg_names[GP_REG_FIRST + 3],
8150 reg_names[FP_REG_FIRST + 1]);
8153 fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
8154 /* As above, we can't fill the delay slot. */
8155 fprintf (asm_out_file, "\tnop\n");
8158 fprintf (asm_out_file, "\t.set\treorder\n");
8160 #ifdef ASM_DECLARE_FUNCTION_SIZE
8161 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8162 #endif
8164 if (!FUNCTION_NAME_ALREADY_DECLARED)
8166 fputs ("\t.end\t", asm_out_file);
8167 assemble_name (asm_out_file, stubname);
8168 fputs ("\n", asm_out_file);
8171 fprintf (asm_out_file, "\t.set\tmips16\n");
8173 /* Record this stub. */
8174 l = (struct mips16_stub *) xmalloc (sizeof *l);
8175 l->name = xstrdup (fnname);
8176 l->fpret = fpret;
8177 l->next = mips16_stubs;
8178 mips16_stubs = l;
8181 /* If we expect a floating point return value, but we've built a
8182 stub which does not expect one, then we're in trouble. We can't
8183 use the existing stub, because it won't handle the floating point
8184 value. We can't build a new stub, because the linker won't know
8185 which stub to use for the various calls in this object file.
8186 Fortunately, this case is illegal, since it means that a function
8187 was declared in two different ways in a single compilation. */
8188 if (fpret && ! l->fpret)
8189 error ("can not handle inconsistent calls to `%s'", fnname);
8191 /* If we are calling a stub which handles a floating point return
8192 value, we need to arrange to save $18 in the prologue. We do
8193 this by marking the function call as using the register. The
8194 prologue will later see that it is used, and emit code to save
8195 it. */
8197 if (l->fpret)
8199 rtx insn;
8201 if (retval == NULL_RTX)
8202 insn = gen_call_internal (fn, arg_size);
8203 else
8204 insn = gen_call_value_internal (retval, fn, arg_size);
8205 insn = emit_call_insn (insn);
8207 if (GET_CODE (insn) != CALL_INSN)
8208 abort ();
8210 CALL_INSN_FUNCTION_USAGE (insn) =
8211 gen_rtx_EXPR_LIST (VOIDmode,
8212 gen_rtx_USE (VOIDmode, gen_rtx_REG (word_mode, 18)),
8213 CALL_INSN_FUNCTION_USAGE (insn));
8215 /* Return 1 to tell the caller that we've generated the call
8216 insn. */
8217 return 1;
8220 /* Return 0 to let the caller generate the call insn. */
8221 return 0;
8224 /* An entry in the mips16 constant pool. VALUE is the pool constant,
8225 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
8227 struct mips16_constant {
8228 struct mips16_constant *next;
8229 rtx value;
8230 rtx label;
8231 enum machine_mode mode;
8234 /* Information about an incomplete mips16 constant pool. FIRST is the
8235 first constant, HIGHEST_ADDRESS is the highest address that the first
8236 byte of the pool can have, and INSN_ADDRESS is the current instruction
8237 address. */
8239 struct mips16_constant_pool {
8240 struct mips16_constant *first;
8241 int highest_address;
8242 int insn_address;
8245 /* Add constant VALUE to POOL and return its label. MODE is the
8246 value's mode (used for CONST_INTs, etc.). */
8248 static rtx
8249 add_constant (struct mips16_constant_pool *pool,
8250 rtx value, enum machine_mode mode)
8252 struct mips16_constant **p, *c;
8253 bool first_of_size_p;
8255 /* See whether the constant is already in the pool. If so, return the
8256 existing label, otherwise leave P pointing to the place where the
8257 constant should be added.
8259 Keep the pool sorted in increasing order of mode size so that we can
8260 reduce the number of alignments needed. */
8261 first_of_size_p = true;
8262 for (p = &pool->first; *p != 0; p = &(*p)->next)
8264 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
8265 return (*p)->label;
8266 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
8267 break;
8268 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
8269 first_of_size_p = false;
8272 /* In the worst case, the constant needed by the earliest instruction
8273 will end up at the end of the pool. The entire pool must then be
8274 accessible from that instruction.
8276 When adding the first constant, set the pool's highest address to
8277 the address of the first out-of-range byte. Adjust this address
8278 downwards each time a new constant is added. */
8279 if (pool->first == 0)
8280 /* For pc-relative lw, addiu and daddiu instructions, the base PC value
8281 is the address of the instruction with the lowest two bits clear.
8282 The base PC value for ld has the lowest three bits clear. Assume
8283 the worst case here. */
8284 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
8285 pool->highest_address -= GET_MODE_SIZE (mode);
8286 if (first_of_size_p)
8287 /* Take into account the worst possible padding due to alignment. */
8288 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
8290 /* Create a new entry. */
8291 c = (struct mips16_constant *) xmalloc (sizeof *c);
8292 c->value = value;
8293 c->mode = mode;
8294 c->label = gen_label_rtx ();
8295 c->next = *p;
8296 *p = c;
8298 return c->label;
8301 /* Output constant VALUE after instruction INSN and return the last
8302 instruction emitted. MODE is the mode of the constant. */
8304 static rtx
8305 dump_constants_1 (enum machine_mode mode, rtx value, rtx insn)
8307 switch (GET_MODE_CLASS (mode))
8309 case MODE_INT:
8311 rtx size = GEN_INT (GET_MODE_SIZE (mode));
8312 return emit_insn_after (gen_consttable_int (value, size), insn);
8315 case MODE_FLOAT:
8316 return emit_insn_after (gen_consttable_float (value), insn);
8318 case MODE_VECTOR_FLOAT:
8319 case MODE_VECTOR_INT:
8321 int i;
8322 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
8323 insn = dump_constants_1 (GET_MODE_INNER (mode),
8324 CONST_VECTOR_ELT (value, i), insn);
8325 return insn;
8328 default:
8329 abort ();
8334 /* Dump out the constants in CONSTANTS after INSN. */
8336 static void
8337 dump_constants (struct mips16_constant *constants, rtx insn)
8339 struct mips16_constant *c, *next;
8340 int align;
8342 align = 0;
8343 for (c = constants; c != NULL; c = next)
8345 /* If necessary, increase the alignment of PC. */
8346 if (align < GET_MODE_SIZE (c->mode))
8348 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
8349 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
8351 align = GET_MODE_SIZE (c->mode);
8353 insn = emit_label_after (c->label, insn);
8354 insn = dump_constants_1 (c->mode, c->value, insn);
8356 next = c->next;
8357 free (c);
8360 emit_barrier_after (insn);
8363 /* Return the length of instruction INSN.
8365 ??? MIPS16 switch tables go in .text, but we don't define
8366 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
8367 compute their lengths correctly. */
8369 static int
8370 mips16_insn_length (rtx insn)
8372 if (GET_CODE (insn) == JUMP_INSN)
8374 rtx body = PATTERN (insn);
8375 if (GET_CODE (body) == ADDR_VEC)
8376 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
8377 if (GET_CODE (body) == ADDR_DIFF_VEC)
8378 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
8380 return get_attr_length (insn);
8383 /* Rewrite *X so that constant pool references refer to the constant's
8384 label instead. DATA points to the constant pool structure. */
8386 static int
8387 mips16_rewrite_pool_refs (rtx *x, void *data)
8389 struct mips16_constant_pool *pool = data;
8390 if (GET_CODE (*x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (*x))
8391 *x = gen_rtx_LABEL_REF (Pmode, add_constant (pool,
8392 get_pool_constant (*x),
8393 get_pool_mode (*x)));
8394 return 0;
8397 /* Build MIPS16 constant pools. */
8399 static void
8400 mips16_lay_out_constants (void)
8402 struct mips16_constant_pool pool;
8403 rtx insn, barrier;
8405 barrier = 0;
8406 memset (&pool, 0, sizeof (pool));
8407 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8409 /* Rewrite constant pool references in INSN. */
8410 if (INSN_P (insn))
8411 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &pool);
8413 pool.insn_address += mips16_insn_length (insn);
8415 if (pool.first != NULL)
8417 /* If there are no natural barriers between the first user of
8418 the pool and the highest acceptable address, we'll need to
8419 create a new instruction to jump around the constant pool.
8420 In the worst case, this instruction will be 4 bytes long.
8422 If it's too late to do this transformation after INSN,
8423 do it immediately before INSN. */
8424 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
8426 rtx label, jump;
8428 label = gen_label_rtx ();
8430 jump = emit_jump_insn_before (gen_jump (label), insn);
8431 JUMP_LABEL (jump) = label;
8432 LABEL_NUSES (label) = 1;
8433 barrier = emit_barrier_after (jump);
8435 emit_label_after (label, barrier);
8436 pool.insn_address += 4;
8439 /* See whether the constant pool is now out of range of the first
8440 user. If so, output the constants after the previous barrier.
8441 Note that any instructions between BARRIER and INSN (inclusive)
8442 will use negative offsets to refer to the pool. */
8443 if (pool.insn_address > pool.highest_address)
8445 dump_constants (pool.first, barrier);
8446 pool.first = NULL;
8447 barrier = 0;
8449 else if (BARRIER_P (insn))
8450 barrier = insn;
8453 dump_constants (pool.first, get_last_insn ());
8456 /* A temporary variable used by for_each_rtx callbacks, etc. */
8457 static rtx mips_sim_insn;
8459 /* A structure representing the state of the processor pipeline.
8460 Used by the mips_sim_* family of functions. */
8461 struct mips_sim {
8462 /* The maximum number of instructions that can be issued in a cycle.
8463 (Caches mips_issue_rate.) */
8464 unsigned int issue_rate;
8466 /* The current simulation time. */
8467 unsigned int time;
8469 /* How many more instructions can be issued in the current cycle. */
8470 unsigned int insns_left;
8472 /* LAST_SET[X].INSN is the last instruction to set register X.
8473 LAST_SET[X].TIME is the time at which that instruction was issued.
8474 INSN is null if no instruction has yet set register X. */
8475 struct {
8476 rtx insn;
8477 unsigned int time;
8478 } last_set[FIRST_PSEUDO_REGISTER];
8480 /* The pipeline's current DFA state. */
8481 state_t dfa_state;
8484 /* Reset STATE to the initial simulation state. */
8486 static void
8487 mips_sim_reset (struct mips_sim *state)
8489 state->time = 0;
8490 state->insns_left = state->issue_rate;
8491 memset (&state->last_set, 0, sizeof (state->last_set));
8492 state_reset (state->dfa_state);
8495 /* Initialize STATE before its first use. DFA_STATE points to an
8496 allocated but uninitialized DFA state. */
8498 static void
8499 mips_sim_init (struct mips_sim *state, state_t dfa_state)
8501 state->issue_rate = mips_issue_rate ();
8502 state->dfa_state = dfa_state;
8503 mips_sim_reset (state);
8506 /* Advance STATE by one clock cycle. */
8508 static void
8509 mips_sim_next_cycle (struct mips_sim *state)
8511 state->time++;
8512 state->insns_left = state->issue_rate;
8513 state_transition (state->dfa_state, 0);
8516 /* Advance simulation state STATE until instruction INSN can read
8517 register REG. */
8519 static void
8520 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
8522 unsigned int i;
8524 for (i = 0; i < HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)); i++)
8525 if (state->last_set[REGNO (reg) + i].insn != 0)
8527 unsigned int t;
8529 t = state->last_set[REGNO (reg) + i].time;
8530 t += insn_latency (state->last_set[REGNO (reg) + i].insn, insn);
8531 while (state->time < t)
8532 mips_sim_next_cycle (state);
8536 /* A for_each_rtx callback. If *X is a register, advance simulation state
8537 DATA until mips_sim_insn can read the register's value. */
8539 static int
8540 mips_sim_wait_regs_2 (rtx *x, void *data)
8542 if (REG_P (*x))
8543 mips_sim_wait_reg (data, mips_sim_insn, *x);
8544 return 0;
8547 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
8549 static void
8550 mips_sim_wait_regs_1 (rtx *x, void *data)
8552 for_each_rtx (x, mips_sim_wait_regs_2, data);
8555 /* Advance simulation state STATE until all of INSN's register
8556 dependencies are satisfied. */
8558 static void
8559 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
8561 mips_sim_insn = insn;
8562 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
8565 /* Advance simulation state STATE until the units required by
8566 instruction INSN are available. */
8568 static void
8569 mips_sim_wait_units (struct mips_sim *state, rtx insn)
8571 state_t tmp_state;
8573 tmp_state = alloca (state_size ());
8574 while (state->insns_left == 0
8575 || (memcpy (tmp_state, state->dfa_state, state_size ()),
8576 state_transition (tmp_state, insn) >= 0))
8577 mips_sim_next_cycle (state);
8580 /* Advance simulation state STATE until INSN is ready to issue. */
8582 static void
8583 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
8585 mips_sim_wait_regs (state, insn);
8586 mips_sim_wait_units (state, insn);
8589 /* mips_sim_insn has just set X. Update the LAST_SET array
8590 in simulation state DATA. */
8592 static void
8593 mips_sim_record_set (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
8595 struct mips_sim *state;
8596 unsigned int i;
8598 state = data;
8599 if (REG_P (x))
8600 for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
8602 state->last_set[REGNO (x) + i].insn = mips_sim_insn;
8603 state->last_set[REGNO (x) + i].time = state->time;
8607 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
8608 can issue immediately (i.e., that mips_sim_wait_insn has already
8609 been called). */
8611 static void
8612 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
8614 state_transition (state->dfa_state, insn);
8615 state->insns_left--;
8617 mips_sim_insn = insn;
8618 note_stores (PATTERN (insn), mips_sim_record_set, state);
8621 /* Simulate issuing a NOP in state STATE. */
8623 static void
8624 mips_sim_issue_nop (struct mips_sim *state)
8626 if (state->insns_left == 0)
8627 mips_sim_next_cycle (state);
8628 state->insns_left--;
8631 /* Update simulation state STATE so that it's ready to accept the instruction
8632 after INSN. INSN should be part of the main rtl chain, not a member of a
8633 SEQUENCE. */
8635 static void
8636 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
8638 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
8639 if (JUMP_P (insn))
8640 mips_sim_issue_nop (state);
8642 switch (GET_CODE (SEQ_BEGIN (insn)))
8644 case CODE_LABEL:
8645 case CALL_INSN:
8646 /* We can't predict the processor state after a call or label. */
8647 mips_sim_reset (state);
8648 break;
8650 case JUMP_INSN:
8651 /* The delay slots of branch likely instructions are only executed
8652 when the branch is taken. Therefore, if the caller has simulated
8653 the delay slot instruction, STATE does not really reflect the state
8654 of the pipeline for the instruction after the delay slot. Also,
8655 branch likely instructions tend to incur a penalty when not taken,
8656 so there will probably be an extra delay between the branch and
8657 the instruction after the delay slot. */
8658 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
8659 mips_sim_reset (state);
8660 break;
8662 default:
8663 break;
8667 /* The VR4130 pipeline issues aligned pairs of instructions together,
8668 but it stalls the second instruction if it depends on the first.
8669 In order to cut down the amount of logic required, this dependence
8670 check is not based on a full instruction decode. Instead, any non-SPECIAL
8671 instruction is assumed to modify the register specified by bits 20-16
8672 (which is usually the "rt" field).
8674 In beq, beql, bne and bnel instructions, the rt field is actually an
8675 input, so we can end up with a false dependence between the branch
8676 and its delay slot. If this situation occurs in instruction INSN,
8677 try to avoid it by swapping rs and rt. */
8679 static void
8680 vr4130_avoid_branch_rt_conflict (rtx insn)
8682 rtx first, second;
8684 first = SEQ_BEGIN (insn);
8685 second = SEQ_END (insn);
8686 if (GET_CODE (first) == JUMP_INSN
8687 && GET_CODE (second) == INSN
8688 && GET_CODE (PATTERN (first)) == SET
8689 && GET_CODE (SET_DEST (PATTERN (first))) == PC
8690 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
8692 /* Check for the right kind of condition. */
8693 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
8694 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
8695 && REG_P (XEXP (cond, 0))
8696 && REG_P (XEXP (cond, 1))
8697 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
8698 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
8700 /* SECOND mentions the rt register but not the rs register. */
8701 rtx tmp = XEXP (cond, 0);
8702 XEXP (cond, 0) = XEXP (cond, 1);
8703 XEXP (cond, 1) = tmp;
8708 /* Implement -mvr4130-align. Go through each basic block and simulate the
8709 processor pipeline. If we find that a pair of instructions could execute
8710 in parallel, and the first of those instruction is not 8-byte aligned,
8711 insert a nop to make it aligned. */
8713 static void
8714 vr4130_align_insns (void)
8716 struct mips_sim state;
8717 rtx insn, subinsn, last, last2, next;
8718 bool aligned_p;
8720 dfa_start ();
8722 /* LAST is the last instruction before INSN to have a nonzero length.
8723 LAST2 is the last such instruction before LAST. */
8724 last = 0;
8725 last2 = 0;
8727 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
8728 aligned_p = true;
8730 mips_sim_init (&state, alloca (state_size ()));
8731 for (insn = get_insns (); insn != 0; insn = next)
8733 unsigned int length;
8735 next = NEXT_INSN (insn);
8737 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
8738 This isn't really related to the alignment pass, but we do it on
8739 the fly to avoid a separate instruction walk. */
8740 vr4130_avoid_branch_rt_conflict (insn);
8742 if (USEFUL_INSN_P (insn))
8743 FOR_EACH_SUBINSN (subinsn, insn)
8745 mips_sim_wait_insn (&state, subinsn);
8747 /* If we want this instruction to issue in parallel with the
8748 previous one, make sure that the previous instruction is
8749 aligned. There are several reasons why this isn't worthwhile
8750 when the second instruction is a call:
8752 - Calls are less likely to be performance critical,
8753 - There's a good chance that the delay slot can execute
8754 in parallel with the call.
8755 - The return address would then be unaligned.
8757 In general, if we're going to insert a nop between instructions
8758 X and Y, it's better to insert it immediately after X. That
8759 way, if the nop makes Y aligned, it will also align any labels
8760 between X and Y. */
8761 if (state.insns_left != state.issue_rate
8762 && GET_CODE (subinsn) != CALL_INSN)
8764 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
8766 /* SUBINSN is the first instruction in INSN and INSN is
8767 aligned. We want to align the previous instruction
8768 instead, so insert a nop between LAST2 and LAST.
8770 Note that LAST could be either a single instruction
8771 or a branch with a delay slot. In the latter case,
8772 LAST, like INSN, is already aligned, but the delay
8773 slot must have some extra delay that stops it from
8774 issuing at the same time as the branch. We therefore
8775 insert a nop before the branch in order to align its
8776 delay slot. */
8777 emit_insn_after (gen_nop (), last2);
8778 aligned_p = false;
8780 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
8782 /* SUBINSN is the delay slot of INSN, but INSN is
8783 currently unaligned. Insert a nop between
8784 LAST and INSN to align it. */
8785 emit_insn_after (gen_nop (), last);
8786 aligned_p = true;
8789 mips_sim_issue_insn (&state, subinsn);
8791 mips_sim_finish_insn (&state, insn);
8793 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
8794 length = get_attr_length (insn);
8795 if (length > 0)
8797 /* If the instruction is an asm statement or multi-instruction
8798 mips.md patern, the length is only an estimate. Insert an
8799 8 byte alignment after it so that the following instructions
8800 can be handled correctly. */
8801 if (GET_CODE (SEQ_BEGIN (insn)) == INSN
8802 && (recog_memoized (insn) < 0 || length >= 8))
8804 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
8805 next = NEXT_INSN (next);
8806 mips_sim_next_cycle (&state);
8807 aligned_p = true;
8809 else if (length & 4)
8810 aligned_p = !aligned_p;
8811 last2 = last;
8812 last = insn;
8815 /* See whether INSN is an aligned label. */
8816 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
8817 aligned_p = true;
8819 dfa_finish ();
8822 /* Subroutine of mips_reorg. If there is a hazard between INSN
8823 and a previous instruction, avoid it by inserting nops after
8824 instruction AFTER.
8826 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
8827 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
8828 before using the value of that register. *HILO_DELAY counts the
8829 number of instructions since the last hilo hazard (that is,
8830 the number of instructions since the last mflo or mfhi).
8832 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
8833 for the next instruction.
8835 LO_REG is an rtx for the LO register, used in dependence checking. */
8837 static void
8838 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
8839 rtx *delayed_reg, rtx lo_reg)
8841 rtx pattern, set;
8842 int nops, ninsns;
8844 if (!INSN_P (insn))
8845 return;
8847 pattern = PATTERN (insn);
8849 /* Do not put the whole function in .set noreorder if it contains
8850 an asm statement. We don't know whether there will be hazards
8851 between the asm statement and the gcc-generated code. */
8852 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
8853 cfun->machine->all_noreorder_p = false;
8855 /* Ignore zero-length instructions (barriers and the like). */
8856 ninsns = get_attr_length (insn) / 4;
8857 if (ninsns == 0)
8858 return;
8860 /* Work out how many nops are needed. Note that we only care about
8861 registers that are explicitly mentioned in the instruction's pattern.
8862 It doesn't matter that calls use the argument registers or that they
8863 clobber hi and lo. */
8864 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
8865 nops = 2 - *hilo_delay;
8866 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
8867 nops = 1;
8868 else
8869 nops = 0;
8871 /* Insert the nops between this instruction and the previous one.
8872 Each new nop takes us further from the last hilo hazard. */
8873 *hilo_delay += nops;
8874 while (nops-- > 0)
8875 emit_insn_after (gen_hazard_nop (), after);
8877 /* Set up the state for the next instruction. */
8878 *hilo_delay += ninsns;
8879 *delayed_reg = 0;
8880 if (INSN_CODE (insn) >= 0)
8881 switch (get_attr_hazard (insn))
8883 case HAZARD_NONE:
8884 break;
8886 case HAZARD_HILO:
8887 *hilo_delay = 0;
8888 break;
8890 case HAZARD_DELAY:
8891 set = single_set (insn);
8892 if (set == 0)
8893 abort ();
8894 *delayed_reg = SET_DEST (set);
8895 break;
8900 /* Go through the instruction stream and insert nops where necessary.
8901 See if the whole function can then be put into .set noreorder &
8902 .set nomacro. */
8904 static void
8905 mips_avoid_hazards (void)
8907 rtx insn, last_insn, lo_reg, delayed_reg;
8908 int hilo_delay, i;
8910 /* Force all instructions to be split into their final form. */
8911 split_all_insns_noflow ();
8913 /* Recalculate instruction lengths without taking nops into account. */
8914 cfun->machine->ignore_hazard_length_p = true;
8915 shorten_branches (get_insns ());
8917 /* The profiler code uses assembler macros. -mfix-vr4120 relies on
8918 assembler nop insertion. */
8919 cfun->machine->all_noreorder_p = (!current_function_profile
8920 && !TARGET_FIX_VR4120);
8922 last_insn = 0;
8923 hilo_delay = 2;
8924 delayed_reg = 0;
8925 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
8927 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
8928 if (INSN_P (insn))
8930 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
8931 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8932 mips_avoid_hazard (last_insn, XVECEXP (PATTERN (insn), 0, i),
8933 &hilo_delay, &delayed_reg, lo_reg);
8934 else
8935 mips_avoid_hazard (last_insn, insn, &hilo_delay,
8936 &delayed_reg, lo_reg);
8938 last_insn = insn;
8943 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
8945 static void
8946 mips_reorg (void)
8948 if (TARGET_MIPS16)
8949 mips16_lay_out_constants ();
8950 else if (TARGET_EXPLICIT_RELOCS)
8952 if (mips_flag_delayed_branch)
8953 dbr_schedule (get_insns (), dump_file);
8954 mips_avoid_hazards ();
8955 if (TUNE_MIPS4130 && TARGET_VR4130_ALIGN)
8956 vr4130_align_insns ();
8960 /* This function does three things:
8962 - Register the special divsi3 and modsi3 functions if -mfix-vr4120.
8963 - Register the mips16 hardware floating point stubs.
8964 - Register the gofast functions if selected using --enable-gofast. */
8966 #include "config/gofast.h"
8968 static void
8969 mips_init_libfuncs (void)
8971 if (TARGET_FIX_VR4120)
8973 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
8974 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
8977 if (TARGET_MIPS16 && mips16_hard_float)
8979 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
8980 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
8981 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
8982 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
8984 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
8985 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
8986 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
8987 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
8988 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
8989 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
8991 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
8992 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
8994 if (TARGET_DOUBLE_FLOAT)
8996 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
8997 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
8998 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
8999 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
9001 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
9002 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
9003 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
9004 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
9005 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
9006 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
9008 set_conv_libfunc (sext_optab, DFmode, SFmode, "__mips16_extendsfdf2");
9009 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__mips16_truncdfsf2");
9011 set_conv_libfunc (sfix_optab, SImode, DFmode, "__mips16_fix_truncdfsi");
9012 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__mips16_floatsidf");
9015 else
9016 gofast_maybe_init_libfuncs ();
9019 /* Return a number assessing the cost of moving a register in class
9020 FROM to class TO. The classes are expressed using the enumeration
9021 values such as `GENERAL_REGS'. A value of 2 is the default; other
9022 values are interpreted relative to that.
9024 It is not required that the cost always equal 2 when FROM is the
9025 same as TO; on some machines it is expensive to move between
9026 registers if they are not general registers.
9028 If reload sees an insn consisting of a single `set' between two
9029 hard registers, and if `REGISTER_MOVE_COST' applied to their
9030 classes returns a value of 2, reload does not check to ensure that
9031 the constraints of the insn are met. Setting a cost of other than
9032 2 will allow reload to verify that the constraints are met. You
9033 should do this if the `movM' pattern's constraints do not allow
9034 such copying.
9036 ??? We make the cost of moving from HI/LO into general
9037 registers the same as for one of moving general registers to
9038 HI/LO for TARGET_MIPS16 in order to prevent allocating a
9039 pseudo to HI/LO. This might hurt optimizations though, it
9040 isn't clear if it is wise. And it might not work in all cases. We
9041 could solve the DImode LO reg problem by using a multiply, just
9042 like reload_{in,out}si. We could solve the SImode/HImode HI reg
9043 problem by using divide instructions. divu puts the remainder in
9044 the HI reg, so doing a divide by -1 will move the value in the HI
9045 reg for all values except -1. We could handle that case by using a
9046 signed divide, e.g. -1 / 2 (or maybe 1 / -2?). We'd have to emit
9047 a compare/branch to test the input value to see which instruction
9048 we need to use. This gets pretty messy, but it is feasible. */
9051 mips_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
9052 enum reg_class to, enum reg_class from)
9054 if (from == M16_REGS && GR_REG_CLASS_P (to))
9055 return 2;
9056 else if (from == M16_NA_REGS && GR_REG_CLASS_P (to))
9057 return 2;
9058 else if (GR_REG_CLASS_P (from))
9060 if (to == M16_REGS)
9061 return 2;
9062 else if (to == M16_NA_REGS)
9063 return 2;
9064 else if (GR_REG_CLASS_P (to))
9066 if (TARGET_MIPS16)
9067 return 4;
9068 else
9069 return 2;
9071 else if (to == FP_REGS)
9072 return 4;
9073 else if (to == HI_REG || to == LO_REG || to == MD_REGS)
9075 if (TARGET_MIPS16)
9076 return 12;
9077 else
9078 return 6;
9080 else if (COP_REG_CLASS_P (to))
9082 return 5;
9084 } /* GR_REG_CLASS_P (from) */
9085 else if (from == FP_REGS)
9087 if (GR_REG_CLASS_P (to))
9088 return 4;
9089 else if (to == FP_REGS)
9090 return 2;
9091 else if (to == ST_REGS)
9092 return 8;
9093 } /* from == FP_REGS */
9094 else if (from == HI_REG || from == LO_REG || from == MD_REGS)
9096 if (GR_REG_CLASS_P (to))
9098 if (TARGET_MIPS16)
9099 return 12;
9100 else
9101 return 6;
9103 } /* from == HI_REG, etc. */
9104 else if (from == ST_REGS && GR_REG_CLASS_P (to))
9105 return 4;
9106 else if (COP_REG_CLASS_P (from))
9108 return 5;
9109 } /* COP_REG_CLASS_P (from) */
9111 /* Fall through. */
9113 return 12;
9116 /* Return the length of INSN. LENGTH is the initial length computed by
9117 attributes in the machine-description file. */
9120 mips_adjust_insn_length (rtx insn, int length)
9122 /* A unconditional jump has an unfilled delay slot if it is not part
9123 of a sequence. A conditional jump normally has a delay slot, but
9124 does not on MIPS16. */
9125 if (simplejump_p (insn)
9126 || (!TARGET_MIPS16 && (GET_CODE (insn) == JUMP_INSN
9127 || GET_CODE (insn) == CALL_INSN)))
9128 length += 4;
9130 /* See how many nops might be needed to avoid hardware hazards. */
9131 if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
9132 switch (get_attr_hazard (insn))
9134 case HAZARD_NONE:
9135 break;
9137 case HAZARD_DELAY:
9138 length += 4;
9139 break;
9141 case HAZARD_HILO:
9142 length += 8;
9143 break;
9146 /* All MIPS16 instructions are a measly two bytes. */
9147 if (TARGET_MIPS16)
9148 length /= 2;
9150 return length;
9154 /* Return an asm sequence to start a noat block and load the address
9155 of a label into $1. */
9157 const char *
9158 mips_output_load_label (void)
9160 if (TARGET_EXPLICIT_RELOCS)
9161 switch (mips_abi)
9163 case ABI_N32:
9164 return "%[lw\t%@,%%got_page(%0)(%+)\n\taddiu\t%@,%@,%%got_ofst(%0)";
9166 case ABI_64:
9167 return "%[ld\t%@,%%got_page(%0)(%+)\n\tdaddiu\t%@,%@,%%got_ofst(%0)";
9169 default:
9170 if (ISA_HAS_LOAD_DELAY)
9171 return "%[lw\t%@,%%got(%0)(%+)%#\n\taddiu\t%@,%@,%%lo(%0)";
9172 return "%[lw\t%@,%%got(%0)(%+)\n\taddiu\t%@,%@,%%lo(%0)";
9174 else
9176 if (Pmode == DImode)
9177 return "%[dla\t%@,%0";
9178 else
9179 return "%[la\t%@,%0";
9184 /* Output assembly instructions to peform a conditional branch.
9186 INSN is the branch instruction. OPERANDS[0] is the condition.
9187 OPERANDS[1] is the target of the branch. OPERANDS[2] is the target
9188 of the first operand to the condition. If TWO_OPERANDS_P is
9189 nonzero the comparison takes two operands; OPERANDS[3] will be the
9190 second operand.
9192 If INVERTED_P is nonzero we are to branch if the condition does
9193 not hold. If FLOAT_P is nonzero this is a floating-point comparison.
9195 LENGTH is the length (in bytes) of the sequence we are to generate.
9196 That tells us whether to generate a simple conditional branch, or a
9197 reversed conditional branch around a `jr' instruction. */
9198 const char *
9199 mips_output_conditional_branch (rtx insn, rtx *operands, int two_operands_p,
9200 int float_p, int inverted_p, int length)
9202 static char buffer[200];
9203 /* The kind of comparison we are doing. */
9204 enum rtx_code code = GET_CODE (operands[0]);
9205 /* Nonzero if the opcode for the comparison needs a `z' indicating
9206 that it is a comparison against zero. */
9207 int need_z_p;
9208 /* A string to use in the assembly output to represent the first
9209 operand. */
9210 const char *op1 = "%z2";
9211 /* A string to use in the assembly output to represent the second
9212 operand. Use the hard-wired zero register if there's no second
9213 operand. */
9214 const char *op2 = (two_operands_p ? ",%z3" : ",%.");
9215 /* The operand-printing string for the comparison. */
9216 const char *const comp = (float_p ? "%F0" : "%C0");
9217 /* The operand-printing string for the inverted comparison. */
9218 const char *const inverted_comp = (float_p ? "%W0" : "%N0");
9220 /* The MIPS processors (for levels of the ISA at least two), have
9221 "likely" variants of each branch instruction. These instructions
9222 annul the instruction in the delay slot if the branch is not
9223 taken. */
9224 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
9226 if (!two_operands_p)
9228 /* To compute whether than A > B, for example, we normally
9229 subtract B from A and then look at the sign bit. But, if we
9230 are doing an unsigned comparison, and B is zero, we don't
9231 have to do the subtraction. Instead, we can just check to
9232 see if A is nonzero. Thus, we change the CODE here to
9233 reflect the simpler comparison operation. */
9234 switch (code)
9236 case GTU:
9237 code = NE;
9238 break;
9240 case LEU:
9241 code = EQ;
9242 break;
9244 case GEU:
9245 /* A condition which will always be true. */
9246 code = EQ;
9247 op1 = "%.";
9248 break;
9250 case LTU:
9251 /* A condition which will always be false. */
9252 code = NE;
9253 op1 = "%.";
9254 break;
9256 default:
9257 /* Not a special case. */
9258 break;
9262 /* Relative comparisons are always done against zero. But
9263 equality comparisons are done between two operands, and therefore
9264 do not require a `z' in the assembly language output. */
9265 need_z_p = (!float_p && code != EQ && code != NE);
9266 /* For comparisons against zero, the zero is not provided
9267 explicitly. */
9268 if (need_z_p)
9269 op2 = "";
9271 /* Begin by terminating the buffer. That way we can always use
9272 strcat to add to it. */
9273 buffer[0] = '\0';
9275 switch (length)
9277 case 4:
9278 case 8:
9279 /* Just a simple conditional branch. */
9280 if (float_p)
9281 sprintf (buffer, "%%*b%s%%?\t%%Z2%%1%%/",
9282 inverted_p ? inverted_comp : comp);
9283 else
9284 sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1%%/",
9285 inverted_p ? inverted_comp : comp,
9286 need_z_p ? "z" : "",
9287 op1,
9288 op2);
9289 return buffer;
9291 case 12:
9292 case 16:
9293 case 24:
9294 case 28:
9296 /* Generate a reversed conditional branch around ` j'
9297 instruction:
9299 .set noreorder
9300 .set nomacro
9301 bc l
9302 delay_slot or #nop
9303 j target
9304 #nop
9306 .set macro
9307 .set reorder
9309 If the original branch was a likely branch, the delay slot
9310 must be executed only if the branch is taken, so generate:
9312 .set noreorder
9313 .set nomacro
9314 bc l
9315 #nop
9316 j target
9317 delay slot or #nop
9319 .set macro
9320 .set reorder
9322 When generating PIC, instead of:
9324 j target
9326 we emit:
9328 .set noat
9329 la $at, target
9330 jr $at
9331 .set at
9334 rtx orig_target;
9335 rtx target = gen_label_rtx ();
9337 orig_target = operands[1];
9338 operands[1] = target;
9339 /* Generate the reversed comparison. This takes four
9340 bytes. */
9341 if (float_p)
9342 sprintf (buffer, "%%*b%s\t%%Z2%%1",
9343 inverted_p ? comp : inverted_comp);
9344 else
9345 sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
9346 inverted_p ? comp : inverted_comp,
9347 need_z_p ? "z" : "",
9348 op1,
9349 op2);
9350 output_asm_insn (buffer, operands);
9352 if (length != 16 && length != 28 && ! mips_branch_likely)
9354 /* Output delay slot instruction. */
9355 rtx insn = final_sequence;
9356 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9357 optimize, 0, 1, NULL);
9358 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9360 else
9361 output_asm_insn ("%#", 0);
9363 if (length <= 16)
9364 output_asm_insn ("j\t%0", &orig_target);
9365 else
9367 output_asm_insn (mips_output_load_label (), &orig_target);
9368 output_asm_insn ("jr\t%@%]", 0);
9371 if (length != 16 && length != 28 && mips_branch_likely)
9373 /* Output delay slot instruction. */
9374 rtx insn = final_sequence;
9375 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9376 optimize, 0, 1, NULL);
9377 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9379 else
9380 output_asm_insn ("%#", 0);
9382 (*targetm.asm_out.internal_label) (asm_out_file, "L",
9383 CODE_LABEL_NUMBER (target));
9385 return "";
9388 default:
9389 abort ();
9392 /* NOTREACHED */
9393 return 0;
9396 /* Used to output div or ddiv instruction DIVISION, which has the operands
9397 given by OPERANDS. Add in a divide-by-zero check if needed.
9399 When working around R4000 and R4400 errata, we need to make sure that
9400 the division is not immediately followed by a shift[1][2]. We also
9401 need to stop the division from being put into a branch delay slot[3].
9402 The easiest way to avoid both problems is to add a nop after the
9403 division. When a divide-by-zero check is needed, this nop can be
9404 used to fill the branch delay slot.
9406 [1] If a double-word or a variable shift executes immediately
9407 after starting an integer division, the shift may give an
9408 incorrect result. See quotations of errata #16 and #28 from
9409 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
9410 in mips.md for details.
9412 [2] A similar bug to [1] exists for all revisions of the
9413 R4000 and the R4400 when run in an MC configuration.
9414 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
9416 "19. In this following sequence:
9418 ddiv (or ddivu or div or divu)
9419 dsll32 (or dsrl32, dsra32)
9421 if an MPT stall occurs, while the divide is slipping the cpu
9422 pipeline, then the following double shift would end up with an
9423 incorrect result.
9425 Workaround: The compiler needs to avoid generating any
9426 sequence with divide followed by extended double shift."
9428 This erratum is also present in "MIPS R4400MC Errata, Processor
9429 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
9430 & 3.0" as errata #10 and #4, respectively.
9432 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
9433 (also valid for MIPS R4000MC processors):
9435 "52. R4000SC: This bug does not apply for the R4000PC.
9437 There are two flavors of this bug:
9439 1) If the instruction just after divide takes an RF exception
9440 (tlb-refill, tlb-invalid) and gets an instruction cache
9441 miss (both primary and secondary) and the line which is
9442 currently in secondary cache at this index had the first
9443 data word, where the bits 5..2 are set, then R4000 would
9444 get a wrong result for the div.
9448 div r8, r9
9449 ------------------- # end-of page. -tlb-refill
9453 div r8, r9
9454 ------------------- # end-of page. -tlb-invalid
9457 2) If the divide is in the taken branch delay slot, where the
9458 target takes RF exception and gets an I-cache miss for the
9459 exception vector or where I-cache miss occurs for the
9460 target address, under the above mentioned scenarios, the
9461 div would get wrong results.
9464 j r2 # to next page mapped or unmapped
9465 div r8,r9 # this bug would be there as long
9466 # as there is an ICache miss and
9467 nop # the "data pattern" is present
9470 beq r0, r0, NextPage # to Next page
9471 div r8,r9
9474 This bug is present for div, divu, ddiv, and ddivu
9475 instructions.
9477 Workaround: For item 1), OS could make sure that the next page
9478 after the divide instruction is also mapped. For item 2), the
9479 compiler could make sure that the divide instruction is not in
9480 the branch delay slot."
9482 These processors have PRId values of 0x00004220 and 0x00004300 for
9483 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
9485 const char *
9486 mips_output_division (const char *division, rtx *operands)
9488 const char *s;
9490 s = division;
9491 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
9493 output_asm_insn (s, operands);
9494 s = "nop";
9496 if (TARGET_CHECK_ZERO_DIV)
9498 if (TARGET_MIPS16)
9500 output_asm_insn (s, operands);
9501 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
9503 else
9505 output_asm_insn ("%(bne\t%2,%.,1f", operands);
9506 output_asm_insn (s, operands);
9507 s = "break\t7%)\n1:";
9510 return s;
9513 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
9514 with a final "000" replaced by "k". Ignore case.
9516 Note: this function is shared between GCC and GAS. */
9518 static bool
9519 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
9521 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
9522 given++, canonical++;
9524 return ((*given == 0 && *canonical == 0)
9525 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
9529 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
9530 CPU name. We've traditionally allowed a lot of variation here.
9532 Note: this function is shared between GCC and GAS. */
9534 static bool
9535 mips_matching_cpu_name_p (const char *canonical, const char *given)
9537 /* First see if the name matches exactly, or with a final "000"
9538 turned into "k". */
9539 if (mips_strict_matching_cpu_name_p (canonical, given))
9540 return true;
9542 /* If not, try comparing based on numerical designation alone.
9543 See if GIVEN is an unadorned number, or 'r' followed by a number. */
9544 if (TOLOWER (*given) == 'r')
9545 given++;
9546 if (!ISDIGIT (*given))
9547 return false;
9549 /* Skip over some well-known prefixes in the canonical name,
9550 hoping to find a number there too. */
9551 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
9552 canonical += 2;
9553 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
9554 canonical += 2;
9555 else if (TOLOWER (canonical[0]) == 'r')
9556 canonical += 1;
9558 return mips_strict_matching_cpu_name_p (canonical, given);
9562 /* Parse an option that takes the name of a processor as its argument.
9563 OPTION is the name of the option and CPU_STRING is the argument.
9564 Return the corresponding processor enumeration if the CPU_STRING is
9565 recognized, otherwise report an error and return null.
9567 A similar function exists in GAS. */
9569 static const struct mips_cpu_info *
9570 mips_parse_cpu (const char *option, const char *cpu_string)
9572 const struct mips_cpu_info *p;
9573 const char *s;
9575 /* In the past, we allowed upper-case CPU names, but it doesn't
9576 work well with the multilib machinery. */
9577 for (s = cpu_string; *s != 0; s++)
9578 if (ISUPPER (*s))
9580 warning ("the cpu name must be lower case");
9581 break;
9584 /* 'from-abi' selects the most compatible architecture for the given
9585 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
9586 EABIs, we have to decide whether we're using the 32-bit or 64-bit
9587 version. Look first at the -mgp options, if given, otherwise base
9588 the choice on MASK_64BIT in TARGET_DEFAULT. */
9589 if (strcasecmp (cpu_string, "from-abi") == 0)
9590 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
9591 : ABI_NEEDS_64BIT_REGS ? 3
9592 : (TARGET_64BIT ? 3 : 1));
9594 /* 'default' has traditionally been a no-op. Probably not very useful. */
9595 if (strcasecmp (cpu_string, "default") == 0)
9596 return 0;
9598 for (p = mips_cpu_info_table; p->name != 0; p++)
9599 if (mips_matching_cpu_name_p (p->name, cpu_string))
9600 return p;
9602 error ("bad value (%s) for %s", cpu_string, option);
9603 return 0;
9607 /* Return the processor associated with the given ISA level, or null
9608 if the ISA isn't valid. */
9610 static const struct mips_cpu_info *
9611 mips_cpu_info_from_isa (int isa)
9613 const struct mips_cpu_info *p;
9615 for (p = mips_cpu_info_table; p->name != 0; p++)
9616 if (p->isa == isa)
9617 return p;
9619 return 0;
9622 /* Implement HARD_REGNO_NREGS. The size of FP registers are controlled
9623 by UNITS_PER_FPREG. All other registers are word sized. */
9625 unsigned int
9626 mips_hard_regno_nregs (int regno, enum machine_mode mode)
9628 if (! FP_REG_P (regno))
9629 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
9630 else
9631 return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
9634 /* Implement TARGET_RETURN_IN_MEMORY. Under the old (i.e., 32 and O64 ABIs)
9635 all BLKmode objects are returned in memory. Under the new (N32 and
9636 64-bit MIPS ABIs) small structures are returned in a register.
9637 Objects with varying size must still be returned in memory, of
9638 course. */
9640 static bool
9641 mips_return_in_memory (tree type, tree fndecl ATTRIBUTE_UNUSED)
9643 if (TARGET_OLDABI)
9644 return (TYPE_MODE (type) == BLKmode);
9645 else
9646 return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
9647 || (int_size_in_bytes (type) == -1));
9650 static bool
9651 mips_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
9653 return !TARGET_OLDABI;
9656 /* Return true if INSN is a multiply-add or multiply-subtract
9657 instruction and PREV assigns to the accumulator operand. */
9659 bool
9660 mips_linked_madd_p (rtx prev, rtx insn)
9662 rtx x;
9664 x = single_set (insn);
9665 if (x == 0)
9666 return false;
9668 x = SET_SRC (x);
9670 if (GET_CODE (x) == PLUS
9671 && GET_CODE (XEXP (x, 0)) == MULT
9672 && reg_set_p (XEXP (x, 1), prev))
9673 return true;
9675 if (GET_CODE (x) == MINUS
9676 && GET_CODE (XEXP (x, 1)) == MULT
9677 && reg_set_p (XEXP (x, 0), prev))
9678 return true;
9680 return false;
9683 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
9684 that may clobber hi or lo. */
9686 static rtx mips_macc_chains_last_hilo;
9688 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
9689 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
9691 static void
9692 mips_macc_chains_record (rtx insn)
9694 if (get_attr_may_clobber_hilo (insn))
9695 mips_macc_chains_last_hilo = insn;
9698 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
9699 has NREADY elements, looking for a multiply-add or multiply-subtract
9700 instruction that is cumulative with mips_macc_chains_last_hilo.
9701 If there is one, promote it ahead of anything else that might
9702 clobber hi or lo. */
9704 static void
9705 mips_macc_chains_reorder (rtx *ready, int nready)
9707 int i, j;
9709 if (mips_macc_chains_last_hilo != 0)
9710 for (i = nready - 1; i >= 0; i--)
9711 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
9713 for (j = nready - 1; j > i; j--)
9714 if (recog_memoized (ready[j]) >= 0
9715 && get_attr_may_clobber_hilo (ready[j]))
9717 mips_promote_ready (ready, i, j);
9718 break;
9720 break;
9724 /* The last instruction to be scheduled. */
9726 static rtx vr4130_last_insn;
9728 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
9729 points to an rtx that is initially an instruction. Nullify the rtx
9730 if the instruction uses the value of register X. */
9732 static void
9733 vr4130_true_reg_dependence_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
9735 rtx *insn_ptr = data;
9736 if (REG_P (x)
9737 && *insn_ptr != 0
9738 && reg_referenced_p (x, PATTERN (*insn_ptr)))
9739 *insn_ptr = 0;
9742 /* Return true if there is true register dependence between vr4130_last_insn
9743 and INSN. */
9745 static bool
9746 vr4130_true_reg_dependence_p (rtx insn)
9748 note_stores (PATTERN (vr4130_last_insn),
9749 vr4130_true_reg_dependence_p_1, &insn);
9750 return insn == 0;
9753 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
9754 the ready queue and that INSN2 is the instruction after it, return
9755 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
9756 in which INSN1 and INSN2 can probably issue in parallel, but for
9757 which (INSN2, INSN1) should be less sensitive to instruction
9758 alignment than (INSN1, INSN2). See 4130.md for more details. */
9760 static bool
9761 vr4130_swap_insns_p (rtx insn1, rtx insn2)
9763 rtx dep;
9765 /* Check for the following case:
9767 1) there is some other instruction X with an anti dependence on INSN1;
9768 2) X has a higher priority than INSN2; and
9769 3) X is an arithmetic instruction (and thus has no unit restrictions).
9771 If INSN1 is the last instruction blocking X, it would better to
9772 choose (INSN1, X) over (INSN2, INSN1). */
9773 for (dep = INSN_DEPEND (insn1); dep != 0; dep = XEXP (dep, 1))
9774 if (REG_NOTE_KIND (dep) == REG_DEP_ANTI
9775 && INSN_PRIORITY (XEXP (dep, 0)) > INSN_PRIORITY (insn2)
9776 && recog_memoized (XEXP (dep, 0)) >= 0
9777 && get_attr_vr4130_class (XEXP (dep, 0)) == VR4130_CLASS_ALU)
9778 return false;
9780 if (vr4130_last_insn != 0
9781 && recog_memoized (insn1) >= 0
9782 && recog_memoized (insn2) >= 0)
9784 /* See whether INSN1 and INSN2 use different execution units,
9785 or if they are both ALU-type instructions. If so, they can
9786 probably execute in parallel. */
9787 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
9788 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
9789 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
9791 /* If only one of the instructions has a dependence on
9792 vr4130_last_insn, prefer to schedule the other one first. */
9793 bool dep1 = vr4130_true_reg_dependence_p (insn1);
9794 bool dep2 = vr4130_true_reg_dependence_p (insn2);
9795 if (dep1 != dep2)
9796 return dep1;
9798 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
9799 is not an ALU-type instruction and if INSN1 uses the same
9800 execution unit. (Note that if this condition holds, we already
9801 know that INSN2 uses a different execution unit.) */
9802 if (class1 != VR4130_CLASS_ALU
9803 && recog_memoized (vr4130_last_insn) >= 0
9804 && class1 == get_attr_vr4130_class (vr4130_last_insn))
9805 return true;
9808 return false;
9811 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
9812 queue with at least two instructions. Swap the first two if
9813 vr4130_swap_insns_p says that it could be worthwhile. */
9815 static void
9816 vr4130_reorder (rtx *ready, int nready)
9818 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
9819 mips_promote_ready (ready, nready - 2, nready - 1);
9822 /* Remove the instruction at index LOWER from ready queue READY and
9823 reinsert it in front of the instruction at index HIGHER. LOWER must
9824 be <= HIGHER. */
9826 static void
9827 mips_promote_ready (rtx *ready, int lower, int higher)
9829 rtx new_head;
9830 int i;
9832 new_head = ready[lower];
9833 for (i = lower; i < higher; i++)
9834 ready[i] = ready[i + 1];
9835 ready[i] = new_head;
9838 /* Implement TARGET_SCHED_REORDER. */
9840 static int
9841 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
9842 rtx *ready, int *nreadyp, int cycle)
9844 if (!reload_completed && TUNE_MACC_CHAINS)
9846 if (cycle == 0)
9847 mips_macc_chains_last_hilo = 0;
9848 if (*nreadyp > 0)
9849 mips_macc_chains_reorder (ready, *nreadyp);
9851 if (reload_completed && TUNE_MIPS4130 && !TARGET_VR4130_ALIGN)
9853 if (cycle == 0)
9854 vr4130_last_insn = 0;
9855 if (*nreadyp > 1)
9856 vr4130_reorder (ready, *nreadyp);
9858 return mips_issue_rate ();
9861 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
9863 static int
9864 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
9865 rtx insn, int more)
9867 switch (GET_CODE (PATTERN (insn)))
9869 case USE:
9870 case CLOBBER:
9871 /* Don't count USEs and CLOBBERs against the issue rate. */
9872 break;
9874 default:
9875 more--;
9876 if (!reload_completed && TUNE_MACC_CHAINS)
9877 mips_macc_chains_record (insn);
9878 vr4130_last_insn = insn;
9879 break;
9881 return more;
9884 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
9885 dependencies have no cost. */
9887 static int
9888 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
9889 rtx dep ATTRIBUTE_UNUSED, int cost)
9891 if (REG_NOTE_KIND (link) != 0)
9892 return 0;
9893 return cost;
9896 /* Return the number of instructions that can be issued per cycle. */
9898 static int
9899 mips_issue_rate (void)
9901 switch (mips_tune)
9903 case PROCESSOR_R4130:
9904 case PROCESSOR_R5400:
9905 case PROCESSOR_R5500:
9906 case PROCESSOR_R7000:
9907 case PROCESSOR_R9000:
9908 return 2;
9910 case PROCESSOR_SB1:
9911 /* This is actually 4, but we get better performance if we claim 3.
9912 This is partly because of unwanted speculative code motion with the
9913 larger number, and partly because in most common cases we can't
9914 reach the theoretical max of 4. */
9915 return 3;
9917 default:
9918 return 1;
9921 abort ();
9925 /* Implements TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE. Return true for
9926 processors that have a DFA pipeline description. */
9928 static int
9929 mips_use_dfa_pipeline_interface (void)
9931 switch (mips_tune)
9933 case PROCESSOR_R3000:
9934 case PROCESSOR_R4130:
9935 case PROCESSOR_R5400:
9936 case PROCESSOR_R5500:
9937 case PROCESSOR_R7000:
9938 case PROCESSOR_R9000:
9939 case PROCESSOR_SB1:
9940 case PROCESSOR_SR71000:
9941 return true;
9943 default:
9944 return false;
9948 /* Implements TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
9949 be as wide as the scheduling freedom in the DFA. */
9951 static int
9952 mips_multipass_dfa_lookahead (void)
9954 /* Can schedule up to 4 of the 6 function units in any one cycle. */
9955 if (mips_tune == PROCESSOR_SB1)
9956 return 4;
9958 return 0;
9962 const char *
9963 mips_emit_prefetch (rtx *operands)
9965 int write = INTVAL (operands[1]);
9966 int locality = INTVAL (operands[2]);
9967 int indexed = GET_CODE (operands[3]) == REG;
9968 int code;
9969 char buffer[30];
9971 if (locality <= 0)
9972 code = (write ? 5 : 4); /* store_streamed / load_streamed. */
9973 else if (locality <= 2)
9974 code = (write ? 1 : 0); /* store / load. */
9975 else
9976 code = (write ? 7 : 6); /* store_retained / load_retained. */
9978 sprintf (buffer, "%s\t%d,%%3(%%0)", indexed ? "prefx" : "pref", code);
9979 output_asm_insn (buffer, operands);
9980 return "";
9985 #if TARGET_IRIX
9986 /* Output assembly to switch to section NAME with attribute FLAGS. */
9988 static void
9989 irix_asm_named_section_1 (const char *name, unsigned int flags,
9990 unsigned int align)
9992 unsigned int sh_type, sh_flags, sh_entsize;
9994 sh_flags = 0;
9995 if (!(flags & SECTION_DEBUG))
9996 sh_flags |= 2; /* SHF_ALLOC */
9997 if (flags & SECTION_WRITE)
9998 sh_flags |= 1; /* SHF_WRITE */
9999 if (flags & SECTION_CODE)
10000 sh_flags |= 4; /* SHF_EXECINSTR */
10001 if (flags & SECTION_SMALL)
10002 sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
10003 if (strcmp (name, ".debug_frame") == 0)
10004 sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
10005 if (flags & SECTION_DEBUG)
10006 sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
10007 else if (flags & SECTION_BSS)
10008 sh_type = 8; /* SHT_NOBITS */
10009 else
10010 sh_type = 1; /* SHT_PROGBITS */
10012 if (flags & SECTION_CODE)
10013 sh_entsize = 4;
10014 else
10015 sh_entsize = 0;
10017 fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
10018 name, sh_type, sh_flags, sh_entsize, align);
10021 static void
10022 irix_asm_named_section (const char *name, unsigned int flags)
10024 if (TARGET_SGI_O32_AS)
10025 default_no_named_section (name, flags);
10026 else if (mips_abi == ABI_32 && TARGET_GAS)
10027 default_elf_asm_named_section (name, flags);
10028 else
10029 irix_asm_named_section_1 (name, flags, 0);
10032 /* In addition to emitting a .align directive, record the maximum
10033 alignment requested for the current section. */
10035 struct irix_section_align_entry GTY (())
10037 const char *name;
10038 unsigned int log;
10039 unsigned int flags;
10042 static htab_t irix_section_align_htab;
10043 static FILE *irix_orig_asm_out_file;
10045 static int
10046 irix_section_align_entry_eq (const void *p1, const void *p2)
10048 const struct irix_section_align_entry *old = p1;
10049 const char *new = p2;
10051 return strcmp (old->name, new) == 0;
10054 static hashval_t
10055 irix_section_align_entry_hash (const void *p)
10057 const struct irix_section_align_entry *old = p;
10058 return htab_hash_string (old->name);
10061 void
10062 irix_asm_output_align (FILE *file, unsigned int log)
10064 const char *section = current_section_name ();
10065 struct irix_section_align_entry **slot, *entry;
10067 if (mips_abi != ABI_32)
10069 if (! section)
10070 abort ();
10072 slot = (struct irix_section_align_entry **)
10073 htab_find_slot_with_hash (irix_section_align_htab, section,
10074 htab_hash_string (section), INSERT);
10075 entry = *slot;
10076 if (! entry)
10078 entry = (struct irix_section_align_entry *)
10079 xmalloc (sizeof (struct irix_section_align_entry));
10080 *slot = entry;
10081 entry->name = section;
10082 entry->log = log;
10083 entry->flags = current_section_flags ();
10085 else if (entry->log < log)
10086 entry->log = log;
10089 fprintf (file, "\t.align\t%u\n", log);
10092 /* The IRIX assembler does not record alignment from .align directives,
10093 but takes it from the first .section directive seen. Play file
10094 switching games so that we can emit a .section directive at the
10095 beginning of the file with the proper alignment attached. */
10097 static void
10098 irix_file_start (void)
10100 mips_file_start ();
10102 if (mips_abi == ABI_32)
10103 return;
10105 irix_orig_asm_out_file = asm_out_file;
10106 asm_out_file = tmpfile ();
10108 irix_section_align_htab = htab_create (31, irix_section_align_entry_hash,
10109 irix_section_align_entry_eq, NULL);
10112 static int
10113 irix_section_align_1 (void **slot, void *data ATTRIBUTE_UNUSED)
10115 const struct irix_section_align_entry *entry
10116 = *(const struct irix_section_align_entry **) slot;
10118 irix_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
10119 return 1;
10122 static void
10123 copy_file_data (FILE *to, FILE *from)
10125 char buffer[8192];
10126 size_t len;
10127 rewind (from);
10128 if (ferror (from))
10129 fatal_error ("can't rewind temp file: %m");
10131 while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
10132 if (fwrite (buffer, 1, len, to) != len)
10133 fatal_error ("can't write to output file: %m");
10135 if (ferror (from))
10136 fatal_error ("can't read from temp file: %m");
10138 if (fclose (from))
10139 fatal_error ("can't close temp file: %m");
10142 static void
10143 irix_file_end (void)
10145 if (mips_abi != ABI_32)
10147 /* Emit section directives with the proper alignment at the top of the
10148 real output file. */
10149 FILE *temp = asm_out_file;
10150 asm_out_file = irix_orig_asm_out_file;
10151 htab_traverse (irix_section_align_htab, irix_section_align_1, NULL);
10153 /* Copy the data emitted to the temp file to the real output file. */
10154 copy_file_data (asm_out_file, temp);
10157 mips_file_end ();
10161 /* Implement TARGET_SECTION_TYPE_FLAGS. Make sure that .sdata and
10162 .sbss sections get the SECTION_SMALL flag: this isn't set by the
10163 default code. */
10165 static unsigned int
10166 irix_section_type_flags (tree decl, const char *section, int relocs_p)
10168 unsigned int flags;
10170 flags = default_section_type_flags (decl, section, relocs_p);
10172 if (strcmp (section, ".sdata") == 0
10173 || strcmp (section, ".sbss") == 0
10174 || strncmp (section, ".gnu.linkonce.s.", 16) == 0
10175 || strncmp (section, ".gnu.linkonce.sb.", 17) == 0)
10176 flags |= SECTION_SMALL;
10178 return flags;
10181 #endif /* TARGET_IRIX */
10183 #include "gt-mips.h"