* c-decl.c (grokdeclarator): Use ISO word.
[official-gcc.git] / gcc / config / mips / mips.c
blobac29ac9dc5987e3724c1df402b42778388835791
1 /* Subroutines for insn-output.c for MIPS
2 Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Contributed by A. Lichnewsky, lich@inria.inria.fr.
5 Changes by Michael Meissner, meissner@osf.org.
6 64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
7 Brendan Eich, brendan@microunity.com.
9 This file is part of GNU CC.
11 GNU CC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 GNU CC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU CC; see the file COPYING. If not, write to
23 the Free Software Foundation, 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
26 /* ??? The TARGET_FP_CALL_32 macros are intended to simulate a 32 bit
27 calling convention in 64 bit mode. It doesn't work though, and should
28 be replaced with something better designed. */
30 #include "config.h"
31 #include "system.h"
32 #include <signal.h>
33 #include "rtl.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "real.h"
37 #include "insn-config.h"
38 #include "conditions.h"
39 #include "insn-attr.h"
40 #include "recog.h"
41 #include "toplev.h"
42 #include "output.h"
43 #include "tree.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "flags.h"
47 #include "reload.h"
48 #include "output.h"
49 #include "tm_p.h"
50 #include "ggc.h"
51 #include "gstab.h"
52 #include "hashtab.h"
53 #include "debug.h"
54 #include "target.h"
55 #include "target-def.h"
57 #ifdef HALF_PIC_DEBUG
58 #include "halfpic.h"
59 #endif
61 #ifdef __GNU_STAB__
62 #define STAB_CODE_TYPE enum __stab_debug_code
63 #else
64 #define STAB_CODE_TYPE int
65 #endif
67 extern tree lookup_name PARAMS ((tree));
69 /* Enumeration for all of the relational tests, so that we can build
70 arrays indexed by the test type, and not worry about the order
71 of EQ, NE, etc. */
73 enum internal_test {
74 ITEST_EQ,
75 ITEST_NE,
76 ITEST_GT,
77 ITEST_GE,
78 ITEST_LT,
79 ITEST_LE,
80 ITEST_GTU,
81 ITEST_GEU,
82 ITEST_LTU,
83 ITEST_LEU,
84 ITEST_MAX
88 struct constant;
89 static enum internal_test map_test_to_internal_test PARAMS ((enum rtx_code));
90 static int mips16_simple_memory_operand PARAMS ((rtx, rtx,
91 enum machine_mode));
92 static int m16_check_op PARAMS ((rtx, int, int, int));
93 static void block_move_loop PARAMS ((rtx, rtx,
94 unsigned int,
95 int,
96 rtx, rtx));
97 static void block_move_call PARAMS ((rtx, rtx, rtx));
98 static rtx mips_add_large_offset_to_sp PARAMS ((HOST_WIDE_INT,
99 FILE *));
100 static void mips_annotate_frame_insn PARAMS ((rtx, rtx));
101 static rtx mips_frame_set PARAMS ((enum machine_mode,
102 int, int));
103 static void mips_emit_frame_related_store PARAMS ((rtx, rtx,
104 HOST_WIDE_INT));
105 static void save_restore_insns PARAMS ((int, rtx,
106 long, FILE *));
107 static void mips16_output_gp_offset PARAMS ((FILE *, rtx));
108 static void mips16_fp_args PARAMS ((FILE *, int, int));
109 static void build_mips16_function_stub PARAMS ((FILE *));
110 static void mips16_optimize_gp PARAMS ((rtx));
111 static rtx add_constant PARAMS ((struct constant **,
112 rtx,
113 enum machine_mode));
114 static void dump_constants PARAMS ((struct constant *,
115 rtx));
116 static rtx mips_find_symbol PARAMS ((rtx));
117 static void abort_with_insn PARAMS ((rtx, const char *))
118 ATTRIBUTE_NORETURN;
119 static int symbolic_expression_p PARAMS ((rtx));
120 static void mips_add_gc_roots PARAMS ((void));
121 static void mips_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
122 static void mips_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
123 static enum processor_type mips_parse_cpu PARAMS ((const char *));
124 static void copy_file_data PARAMS ((FILE *, FILE *));
125 #ifdef TARGET_IRIX6
126 static void iris6_asm_named_section_1 PARAMS ((const char *,
127 unsigned int,
128 unsigned int));
129 static void iris6_asm_named_section PARAMS ((const char *,
130 unsigned int));
131 static int iris_section_align_entry_eq PARAMS ((const PTR, const PTR));
132 static hashval_t iris_section_align_entry_hash PARAMS ((const PTR));
133 static int iris6_section_align_1 PARAMS ((void **, void *));
134 #endif
135 static int mips_adjust_cost PARAMS ((rtx, rtx, rtx, int));
137 /* Global variables for machine-dependent things. */
139 /* Threshold for data being put into the small data/bss area, instead
140 of the normal data area (references to the small data/bss area take
141 1 instruction, and use the global pointer, references to the normal
142 data area takes 2 instructions). */
143 int mips_section_threshold = -1;
145 /* Count the number of .file directives, so that .loc is up to date. */
146 int num_source_filenames = 0;
148 /* Count the number of sdb related labels are generated (to find block
149 start and end boundaries). */
150 int sdb_label_count = 0;
152 /* Next label # for each statement for Silicon Graphics IRIS systems. */
153 int sym_lineno = 0;
155 /* Non-zero if inside of a function, because the stupid MIPS asm can't
156 handle .files inside of functions. */
157 int inside_function = 0;
159 /* Files to separate the text and the data output, so that all of the data
160 can be emitted before the text, which will mean that the assembler will
161 generate smaller code, based on the global pointer. */
162 FILE *asm_out_data_file;
163 FILE *asm_out_text_file;
165 /* Linked list of all externals that are to be emitted when optimizing
166 for the global pointer if they haven't been declared by the end of
167 the program with an appropriate .comm or initialization. */
169 struct extern_list
171 struct extern_list *next; /* next external */
172 const char *name; /* name of the external */
173 int size; /* size in bytes */
174 } *extern_head = 0;
176 /* Name of the file containing the current function. */
177 const char *current_function_file = "";
179 /* Warning given that Mips ECOFF can't support changing files
180 within a function. */
181 int file_in_function_warning = FALSE;
183 /* Whether to suppress issuing .loc's because the user attempted
184 to change the filename within a function. */
185 int ignore_line_number = FALSE;
187 /* Number of nested .set noreorder, noat, nomacro, and volatile requests. */
188 int set_noreorder;
189 int set_noat;
190 int set_nomacro;
191 int set_volatile;
193 /* The next branch instruction is a branch likely, not branch normal. */
194 int mips_branch_likely;
196 /* Count of delay slots and how many are filled. */
197 int dslots_load_total;
198 int dslots_load_filled;
199 int dslots_jump_total;
200 int dslots_jump_filled;
202 /* # of nops needed by previous insn */
203 int dslots_number_nops;
205 /* Number of 1/2/3 word references to data items (ie, not jal's). */
206 int num_refs[3];
208 /* registers to check for load delay */
209 rtx mips_load_reg, mips_load_reg2, mips_load_reg3, mips_load_reg4;
211 /* Cached operands, and operator to compare for use in set/branch/trap
212 on condition codes. */
213 rtx branch_cmp[2];
215 /* what type of branch to use */
216 enum cmp_type branch_type;
218 /* Number of previously seen half-pic pointers and references. */
219 static int prev_half_pic_ptrs = 0;
220 static int prev_half_pic_refs = 0;
222 /* The target cpu for code generation. */
223 enum processor_type mips_arch;
225 /* The target cpu for optimization and scheduling. */
226 enum processor_type mips_tune;
228 /* which instruction set architecture to use. */
229 int mips_isa;
231 /* which abi to use. */
232 int mips_abi;
234 /* Strings to hold which cpu and instruction set architecture to use. */
235 const char *mips_cpu_string; /* for -mcpu=<xxx> */
236 const char *mips_arch_string; /* for -march=<xxx> */
237 const char *mips_tune_string; /* for -mtune=<xxx> */
238 const char *mips_isa_string; /* for -mips{1,2,3,4} */
239 const char *mips_abi_string; /* for -mabi={32,n32,64,eabi} */
241 /* Whether we are generating mips16 code. This is a synonym for
242 TARGET_MIPS16, and exists for use as an attribute. */
243 int mips16;
245 /* This variable is set by -mno-mips16. We only care whether
246 -mno-mips16 appears or not, and using a string in this fashion is
247 just a way to avoid using up another bit in target_flags. */
248 const char *mips_no_mips16_string;
250 /* This is only used to determine if an type size setting option was
251 explicitly specified (-mlong64, -mint64, -mlong32). The specs
252 set this option if such an option is used. */
253 const char *mips_explicit_type_size_string;
255 /* Whether we are generating mips16 hard float code. In mips16 mode
256 we always set TARGET_SOFT_FLOAT; this variable is nonzero if
257 -msoft-float was not specified by the user, which means that we
258 should arrange to call mips32 hard floating point code. */
259 int mips16_hard_float;
261 /* This variable is set by -mentry. We only care whether -mentry
262 appears or not, and using a string in this fashion is just a way to
263 avoid using up another bit in target_flags. */
264 const char *mips_entry_string;
266 const char *mips_cache_flush_func = CACHE_FLUSH_FUNC;
268 /* Whether we should entry and exit pseudo-ops in mips16 mode. */
269 int mips_entry;
271 /* If TRUE, we split addresses into their high and low parts in the RTL. */
272 int mips_split_addresses;
274 /* Generating calls to position independent functions? */
275 enum mips_abicalls_type mips_abicalls;
277 /* High and low marks for floating point values which we will accept
278 as legitimate constants for LEGITIMATE_CONSTANT_P. These are
279 initialized in override_options. */
280 REAL_VALUE_TYPE dfhigh, dflow, sfhigh, sflow;
282 /* Mode used for saving/restoring general purpose registers. */
283 static enum machine_mode gpr_mode;
285 /* Array giving truth value on whether or not a given hard register
286 can support a given mode. */
287 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
289 /* Current frame information calculated by compute_frame_size. */
290 struct mips_frame_info current_frame_info;
292 /* Zero structure to initialize current_frame_info. */
293 struct mips_frame_info zero_frame_info;
295 /* Pseudo-reg holding the address of the current function when
296 generating embedded PIC code. Created by LEGITIMIZE_ADDRESS, used
297 by mips_finalize_pic if it was created. */
298 rtx embedded_pic_fnaddr_rtx;
300 /* The length of all strings seen when compiling for the mips16. This
301 is used to tell how many strings are in the constant pool, so that
302 we can see if we may have an overflow. This is reset each time the
303 constant pool is output. */
304 int mips_string_length;
306 /* Pseudo-reg holding the value of $28 in a mips16 function which
307 refers to GP relative global variables. */
308 rtx mips16_gp_pseudo_rtx;
310 /* In mips16 mode, we build a list of all the string constants we see
311 in a particular function. */
313 struct string_constant
315 struct string_constant *next;
316 const char *label;
319 static struct string_constant *string_constants;
321 /* List of all MIPS punctuation characters used by print_operand. */
322 char mips_print_operand_punct[256];
324 /* Map GCC register number to debugger register number. */
325 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
327 /* Buffer to use to enclose a load/store operation with %{ %} to
328 turn on .set volatile. */
329 static char volatile_buffer[60];
331 /* Hardware names for the registers. If -mrnames is used, this
332 will be overwritten with mips_sw_reg_names. */
334 char mips_reg_names[][8] =
336 "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7",
337 "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
338 "$16", "$17", "$18", "$19", "$20", "$21", "$22", "$23",
339 "$24", "$25", "$26", "$27", "$28", "$sp", "$fp", "$31",
340 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
341 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
342 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
343 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
344 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
345 "$fcc5","$fcc6","$fcc7","$rap"
348 /* Mips software names for the registers, used to overwrite the
349 mips_reg_names array. */
351 static const char mips_sw_reg_names[][8] =
353 "$zero","$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3",
354 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
355 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
356 "$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra",
357 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
358 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
359 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
360 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
361 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
362 "$fcc5","$fcc6","$fcc7","$rap"
365 /* Map hard register number to register class */
366 const enum reg_class mips_regno_to_class[] =
368 GR_REGS, GR_REGS, M16_NA_REGS, M16_NA_REGS,
369 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
370 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
371 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
372 M16_NA_REGS, M16_NA_REGS, GR_REGS, GR_REGS,
373 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
374 T_REG, GR_REGS, GR_REGS, GR_REGS,
375 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
376 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
377 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
378 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
379 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
380 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
381 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
382 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
383 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
384 HI_REG, LO_REG, HILO_REG, ST_REGS,
385 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
386 ST_REGS, ST_REGS, ST_REGS, GR_REGS
389 /* Map register constraint character to register class. */
390 enum reg_class mips_char_to_class[256] =
392 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
393 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
394 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
395 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
396 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
397 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
398 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
399 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
400 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
401 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
402 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
403 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
404 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
405 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
406 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
407 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
408 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
409 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
410 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
411 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
412 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
413 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
414 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
415 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
416 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
417 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
418 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
419 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
420 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
421 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
422 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
423 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
424 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
425 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
426 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
427 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
428 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
429 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
430 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
431 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
432 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
433 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
434 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
435 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
436 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
437 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
438 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
439 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
440 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
441 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
442 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
443 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
444 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
445 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
446 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
447 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
448 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
449 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
450 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
451 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
452 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
453 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
454 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
455 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
458 /* Initialize the GCC target structure. */
459 #undef TARGET_ASM_FUNCTION_PROLOGUE
460 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
461 #undef TARGET_ASM_FUNCTION_EPILOGUE
462 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
464 #undef TARGET_SCHED_ADJUST_COST
465 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
467 struct gcc_target targetm = TARGET_INITIALIZER;
469 /* Return truth value of whether OP can be used as an operands
470 where a register or 16 bit unsigned integer is needed. */
473 uns_arith_operand (op, mode)
474 rtx op;
475 enum machine_mode mode;
477 if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
478 return 1;
480 return register_operand (op, mode);
483 /* Return truth value of whether OP can be used as an operands
484 where a 16 bit integer is needed */
487 arith_operand (op, mode)
488 rtx op;
489 enum machine_mode mode;
491 if (GET_CODE (op) == CONST_INT && SMALL_INT (op))
492 return 1;
494 /* On the mips16, a GP relative value is a signed 16 bit offset. */
495 if (TARGET_MIPS16 && GET_CODE (op) == CONST && mips16_gp_offset_p (op))
496 return 1;
498 return register_operand (op, mode);
501 /* Return truth value of whether OP can be used as an operand in a two
502 address arithmetic insn (such as set 123456,%o4) of mode MODE. */
505 arith32_operand (op, mode)
506 rtx op;
507 enum machine_mode mode;
509 if (GET_CODE (op) == CONST_INT)
510 return 1;
512 return register_operand (op, mode);
515 /* Return truth value of whether OP is an integer which fits in 16 bits. */
518 small_int (op, mode)
519 rtx op;
520 enum machine_mode mode ATTRIBUTE_UNUSED;
522 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
525 /* Return truth value of whether OP is a 32 bit integer which is too big to
526 be loaded with one instruction. */
529 large_int (op, mode)
530 rtx op;
531 enum machine_mode mode ATTRIBUTE_UNUSED;
533 HOST_WIDE_INT value;
535 if (GET_CODE (op) != CONST_INT)
536 return 0;
538 value = INTVAL (op);
540 /* ior reg,$r0,value */
541 if ((value & ~ ((HOST_WIDE_INT) 0x0000ffff)) == 0)
542 return 0;
544 /* subu reg,$r0,value */
545 if (((unsigned HOST_WIDE_INT) (value + 32768)) <= 32767)
546 return 0;
548 /* lui reg,value>>16 */
549 if ((value & 0x0000ffff) == 0)
550 return 0;
552 return 1;
555 /* Return truth value of whether OP is a register or the constant 0.
556 In mips16 mode, we only accept a register, since the mips16 does
557 not have $0. */
560 reg_or_0_operand (op, mode)
561 rtx op;
562 enum machine_mode mode;
564 switch (GET_CODE (op))
566 case CONST_INT:
567 if (TARGET_MIPS16)
568 return 0;
569 return INTVAL (op) == 0;
571 case CONST_DOUBLE:
572 if (TARGET_MIPS16)
573 return 0;
574 return op == CONST0_RTX (mode);
576 case REG:
577 case SUBREG:
578 return register_operand (op, mode);
580 default:
581 break;
584 return 0;
587 /* Return truth value of whether OP is a register or the constant 0,
588 even in mips16 mode. */
591 true_reg_or_0_operand (op, mode)
592 rtx op;
593 enum machine_mode mode;
595 switch (GET_CODE (op))
597 case CONST_INT:
598 return INTVAL (op) == 0;
600 case CONST_DOUBLE:
601 return op == CONST0_RTX (mode);
603 case REG:
604 case SUBREG:
605 return register_operand (op, mode);
607 default:
608 break;
611 return 0;
614 /* Return truth value if a CONST_DOUBLE is ok to be a legitimate constant. */
617 mips_const_double_ok (op, mode)
618 rtx op;
619 enum machine_mode mode;
621 REAL_VALUE_TYPE d;
623 if (GET_CODE (op) != CONST_DOUBLE)
624 return 0;
626 if (mode == VOIDmode)
627 return 1;
629 if (mode != SFmode && mode != DFmode)
630 return 0;
632 if (op == CONST0_RTX (mode))
633 return 1;
635 /* ??? li.s does not work right with SGI's Irix 6 assembler. */
636 if (mips_abi != ABI_32 && mips_abi != ABI_O64 && mips_abi != ABI_EABI)
637 return 0;
639 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
641 if (REAL_VALUE_ISNAN (d))
642 return FALSE;
644 if (REAL_VALUE_NEGATIVE (d))
645 d = REAL_VALUE_NEGATE (d);
647 if (mode == DFmode)
649 if (REAL_VALUES_LESS (d, dfhigh)
650 && REAL_VALUES_LESS (dflow, d))
651 return 1;
653 else
655 if (REAL_VALUES_LESS (d, sfhigh)
656 && REAL_VALUES_LESS (sflow, d))
657 return 1;
660 return 0;
663 /* Accept the floating point constant 1 in the appropriate mode. */
666 const_float_1_operand (op, mode)
667 rtx op;
668 enum machine_mode mode;
670 REAL_VALUE_TYPE d;
671 static REAL_VALUE_TYPE onedf;
672 static REAL_VALUE_TYPE onesf;
673 static int one_initialized;
675 if (GET_CODE (op) != CONST_DOUBLE
676 || mode != GET_MODE (op)
677 || (mode != DFmode && mode != SFmode))
678 return 0;
680 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
682 /* We only initialize these values if we need them, since we will
683 never get called unless mips_isa >= 4. */
684 if (! one_initialized)
686 onedf = REAL_VALUE_ATOF ("1.0", DFmode);
687 onesf = REAL_VALUE_ATOF ("1.0", SFmode);
688 one_initialized = 1;
691 if (mode == DFmode)
692 return REAL_VALUES_EQUAL (d, onedf);
693 else
694 return REAL_VALUES_EQUAL (d, onesf);
697 /* Return true if a memory load or store of REG plus OFFSET in MODE
698 can be represented in a single word on the mips16. */
700 static int
701 mips16_simple_memory_operand (reg, offset, mode)
702 rtx reg;
703 rtx offset;
704 enum machine_mode mode;
706 unsigned int size;
707 int off;
709 if (mode == BLKmode)
711 /* We can't tell, because we don't know how the value will
712 eventually be accessed. Returning 0 here does no great
713 harm; it just prevents some possible instruction scheduling. */
714 return 0;
717 size = GET_MODE_SIZE (mode);
719 if (INTVAL (offset) % size != 0)
720 return 0;
721 if (REGNO (reg) == STACK_POINTER_REGNUM && GET_MODE_SIZE (mode) == 4)
722 off = 0x100;
723 else
724 off = 0x20;
725 if (INTVAL (offset) >= 0 && INTVAL (offset) < (HOST_WIDE_INT)(off * size))
726 return 1;
727 return 0;
730 /* Return truth value if a memory operand fits in a single instruction
731 (ie, register + small offset). */
734 simple_memory_operand (op, mode)
735 rtx op;
736 enum machine_mode mode;
738 rtx addr, plus0, plus1;
740 /* Eliminate non-memory operations */
741 if (GET_CODE (op) != MEM)
742 return 0;
744 /* dword operations really put out 2 instructions, so eliminate them. */
745 /* ??? This isn't strictly correct. It is OK to accept multiword modes
746 here, since the length attributes are being set correctly, but only
747 if the address is offsettable. LO_SUM is not offsettable. */
748 if (GET_MODE_SIZE (GET_MODE (op)) > (unsigned) UNITS_PER_WORD)
749 return 0;
751 /* Decode the address now. */
752 addr = XEXP (op, 0);
753 switch (GET_CODE (addr))
755 case REG:
756 case LO_SUM:
757 return 1;
759 case CONST_INT:
760 if (TARGET_MIPS16)
761 return 0;
762 return SMALL_INT (addr);
764 case PLUS:
765 plus0 = XEXP (addr, 0);
766 plus1 = XEXP (addr, 1);
767 if (GET_CODE (plus0) == REG
768 && GET_CODE (plus1) == CONST_INT && SMALL_INT (plus1)
769 && (! TARGET_MIPS16
770 || mips16_simple_memory_operand (plus0, plus1, mode)))
771 return 1;
773 else if (GET_CODE (plus1) == REG
774 && GET_CODE (plus0) == CONST_INT && SMALL_INT (plus0)
775 && (! TARGET_MIPS16
776 || mips16_simple_memory_operand (plus1, plus0, mode)))
777 return 1;
779 else
780 return 0;
782 #if 0
783 /* We used to allow small symbol refs here (ie, stuff in .sdata
784 or .sbss), but this causes some bugs in G++. Also, it won't
785 interfere if the MIPS linker rewrites the store instruction
786 because the function is PIC. */
788 case LABEL_REF: /* never gp relative */
789 break;
791 case CONST:
792 /* If -G 0, we can never have a GP relative memory operation.
793 Also, save some time if not optimizing. */
794 if (!TARGET_GP_OPT)
795 return 0;
798 rtx offset = const0_rtx;
799 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
800 if (GET_CODE (op) != SYMBOL_REF)
801 return 0;
803 /* let's be paranoid.... */
804 if (! SMALL_INT (offset))
805 return 0;
808 /* fall through */
810 case SYMBOL_REF:
811 return SYMBOL_REF_FLAG (addr);
812 #endif
814 /* This SYMBOL_REF case is for the mips16. If the above case is
815 reenabled, this one should be merged in. */
816 case SYMBOL_REF:
817 /* References to the constant pool on the mips16 use a small
818 offset if the function is small. The only time we care about
819 getting this right is during delayed branch scheduling, so
820 don't need to check until then. The machine_dependent_reorg
821 function will set the total length of the instructions used
822 in the function in current_frame_info. If that is small
823 enough, we know for sure that this is a small offset. It
824 would be better if we could take into account the location of
825 the instruction within the function, but we can't, because we
826 don't know where we are. */
827 if (TARGET_MIPS16
828 && CONSTANT_POOL_ADDRESS_P (addr)
829 && current_frame_info.insns_len > 0)
831 long size;
833 size = current_frame_info.insns_len + get_pool_size ();
834 if (GET_MODE_SIZE (mode) == 4)
835 return size < 4 * 0x100;
836 else if (GET_MODE_SIZE (mode) == 8)
837 return size < 8 * 0x20;
838 else
839 return 0;
842 return 0;
844 default:
845 break;
848 return 0;
851 /* Return nonzero for a memory address that can be used to load or store
852 a doubleword. */
855 double_memory_operand (op, mode)
856 rtx op;
857 enum machine_mode mode;
859 if (GET_CODE (op) != MEM
860 || ! memory_operand (op, mode))
862 /* During reload, we accept a pseudo register if it has an
863 appropriate memory address. If we don't do this, we will
864 wind up reloading into a register, and then reloading that
865 register from memory, when we could just reload directly from
866 memory. */
867 if (reload_in_progress
868 && GET_CODE (op) == REG
869 && REGNO (op) >= FIRST_PSEUDO_REGISTER
870 && reg_renumber[REGNO (op)] < 0
871 && reg_equiv_mem[REGNO (op)] != 0
872 && double_memory_operand (reg_equiv_mem[REGNO (op)], mode))
873 return 1;
875 /* All reloaded addresses are valid in TARGET_64BIT mode. This is
876 the same test performed for 'm' in find_reloads. */
878 if (reload_in_progress
879 && TARGET_64BIT
880 && (GET_CODE (op) == MEM
881 || (GET_CODE (op) == REG
882 && REGNO (op) >= FIRST_PSEUDO_REGISTER
883 && reg_renumber[REGNO (op)] < 0)))
884 return 1;
886 if (reload_in_progress
887 && TARGET_MIPS16
888 && GET_CODE (op) == MEM)
890 rtx addr;
892 addr = XEXP (op, 0);
894 /* During reload on the mips16, we accept a large offset
895 from the frame pointer or the stack pointer. This large
896 address will get reloaded anyhow. */
897 if (GET_CODE (addr) == PLUS
898 && GET_CODE (XEXP (addr, 0)) == REG
899 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
900 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
901 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
902 && ! SMALL_INT (XEXP (addr, 1)))
903 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
904 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
905 return 1;
907 /* Similarly, we accept a case where the memory address is
908 itself on the stack, and will be reloaded. */
909 if (GET_CODE (addr) == MEM)
911 rtx maddr;
913 maddr = XEXP (addr, 0);
914 if (GET_CODE (maddr) == PLUS
915 && GET_CODE (XEXP (maddr, 0)) == REG
916 && (REGNO (XEXP (maddr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
917 || REGNO (XEXP (maddr, 0)) == STACK_POINTER_REGNUM)
918 && ((GET_CODE (XEXP (maddr, 1)) == CONST_INT
919 && ! SMALL_INT (XEXP (maddr, 1)))
920 || (GET_CODE (XEXP (maddr, 1)) == SYMBOL_REF
921 && CONSTANT_POOL_ADDRESS_P (XEXP (maddr, 1)))))
922 return 1;
925 /* We also accept the same case when we have a 16 bit signed
926 offset mixed in as well. The large address will get
927 reloaded, and the 16 bit offset will be OK. */
928 if (GET_CODE (addr) == PLUS
929 && GET_CODE (XEXP (addr, 0)) == MEM
930 && GET_CODE (XEXP (addr, 1)) == CONST_INT
931 && SMALL_INT (XEXP (addr, 1)))
933 addr = XEXP (XEXP (addr, 0), 0);
934 if (GET_CODE (addr) == PLUS
935 && GET_CODE (XEXP (addr, 0)) == REG
936 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
937 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
938 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
939 && ! SMALL_INT (XEXP (addr, 1)))
940 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
941 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
942 return 1;
946 return 0;
949 if (TARGET_64BIT)
951 /* In this case we can use an instruction like sd. */
952 return 1;
955 /* Make sure that 4 added to the address is a valid memory address.
956 This essentially just checks for overflow in an added constant. */
958 if (CONSTANT_ADDRESS_P (XEXP (op, 0)))
959 return 1;
961 op = adjust_address_nv (op, GET_MODE_CLASS (mode) == MODE_INT
962 ? SImode : SFmode, 4);
963 return memory_address_p (GET_MODE (op), XEXP (op, 0));
966 /* Return nonzero if the code of this rtx pattern is EQ or NE. */
969 equality_op (op, mode)
970 rtx op;
971 enum machine_mode mode;
973 if (mode != GET_MODE (op))
974 return 0;
976 return GET_CODE (op) == EQ || GET_CODE (op) == NE;
979 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
982 cmp_op (op, mode)
983 rtx op;
984 enum machine_mode mode;
986 if (mode != GET_MODE (op))
987 return 0;
989 return GET_RTX_CLASS (GET_CODE (op)) == '<';
992 /* Return nonzero if the code is a relational operation suitable for a
993 conditional trap instructuion (only EQ, NE, LT, LTU, GE, GEU).
994 We need this in the insn that expands `trap_if' in order to prevent
995 combine from erroneously altering the condition. */
998 trap_cmp_op (op, mode)
999 rtx op;
1000 enum machine_mode mode;
1002 if (mode != GET_MODE (op))
1003 return 0;
1005 switch (GET_CODE (op))
1007 case EQ:
1008 case NE:
1009 case LT:
1010 case LTU:
1011 case GE:
1012 case GEU:
1013 return 1;
1015 default:
1016 return 0;
1020 /* Return nonzero if the operand is either the PC or a label_ref. */
1023 pc_or_label_operand (op, mode)
1024 rtx op;
1025 enum machine_mode mode ATTRIBUTE_UNUSED;
1027 if (op == pc_rtx)
1028 return 1;
1030 if (GET_CODE (op) == LABEL_REF)
1031 return 1;
1033 return 0;
1036 /* Test for a valid operand for a call instruction.
1037 Don't allow the arg pointer register or virtual regs
1038 since they may change into reg + const, which the patterns
1039 can't handle yet. */
1042 call_insn_operand (op, mode)
1043 rtx op;
1044 enum machine_mode mode ATTRIBUTE_UNUSED;
1046 return (CONSTANT_ADDRESS_P (op)
1047 || (GET_CODE (op) == REG && op != arg_pointer_rtx
1048 && ! (REGNO (op) >= FIRST_PSEUDO_REGISTER
1049 && REGNO (op) <= LAST_VIRTUAL_REGISTER)));
1052 /* Return nonzero if OPERAND is valid as a source operand for a move
1053 instruction. */
1056 move_operand (op, mode)
1057 rtx op;
1058 enum machine_mode mode;
1060 /* Accept any general operand after reload has started; doing so
1061 avoids losing if reload does an in-place replacement of a register
1062 with a SYMBOL_REF or CONST. */
1063 return (general_operand (op, mode)
1064 && (! (mips_split_addresses && mips_check_split (op, mode))
1065 || reload_in_progress || reload_completed)
1066 && ! (TARGET_MIPS16
1067 && GET_CODE (op) == SYMBOL_REF
1068 && ! mips16_constant (op, mode, 1, 0)));
1071 /* Return nonzero if OPERAND is valid as a source operand for movdi.
1072 This accepts not only general_operand, but also sign extended
1073 constants and registers. We need to accept sign extended constants
1074 in case a sign extended register which is used in an expression,
1075 and is equivalent to a constant, is spilled. */
1078 movdi_operand (op, mode)
1079 rtx op;
1080 enum machine_mode mode;
1082 if (TARGET_64BIT
1083 && mode == DImode
1084 && GET_CODE (op) == SIGN_EXTEND
1085 && GET_MODE (op) == DImode
1086 && (GET_MODE (XEXP (op, 0)) == SImode
1087 || (GET_CODE (XEXP (op, 0)) == CONST_INT
1088 && GET_MODE (XEXP (op, 0)) == VOIDmode))
1089 && (register_operand (XEXP (op, 0), SImode)
1090 || immediate_operand (XEXP (op, 0), SImode)))
1091 return 1;
1093 return (general_operand (op, mode)
1094 && ! (TARGET_MIPS16
1095 && GET_CODE (op) == SYMBOL_REF
1096 && ! mips16_constant (op, mode, 1, 0)));
1099 /* Like register_operand, but when in 64 bit mode also accept a sign
1100 extend of a 32 bit register, since the value is known to be already
1101 sign extended. */
1104 se_register_operand (op, mode)
1105 rtx op;
1106 enum machine_mode mode;
1108 if (TARGET_64BIT
1109 && mode == DImode
1110 && GET_CODE (op) == SIGN_EXTEND
1111 && GET_MODE (op) == DImode
1112 && GET_MODE (XEXP (op, 0)) == SImode
1113 && register_operand (XEXP (op, 0), SImode))
1114 return 1;
1116 return register_operand (op, mode);
1119 /* Like reg_or_0_operand, but when in 64 bit mode also accept a sign
1120 extend of a 32 bit register, since the value is known to be already
1121 sign extended. */
1124 se_reg_or_0_operand (op, mode)
1125 rtx op;
1126 enum machine_mode mode;
1128 if (TARGET_64BIT
1129 && mode == DImode
1130 && GET_CODE (op) == SIGN_EXTEND
1131 && GET_MODE (op) == DImode
1132 && GET_MODE (XEXP (op, 0)) == SImode
1133 && register_operand (XEXP (op, 0), SImode))
1134 return 1;
1136 return reg_or_0_operand (op, mode);
1139 /* Like uns_arith_operand, but when in 64 bit mode also accept a sign
1140 extend of a 32 bit register, since the value is known to be already
1141 sign extended. */
1144 se_uns_arith_operand (op, mode)
1145 rtx op;
1146 enum machine_mode mode;
1148 if (TARGET_64BIT
1149 && mode == DImode
1150 && GET_CODE (op) == SIGN_EXTEND
1151 && GET_MODE (op) == DImode
1152 && GET_MODE (XEXP (op, 0)) == SImode
1153 && register_operand (XEXP (op, 0), SImode))
1154 return 1;
1156 return uns_arith_operand (op, mode);
1159 /* Like arith_operand, but when in 64 bit mode also accept a sign
1160 extend of a 32 bit register, since the value is known to be already
1161 sign extended. */
1164 se_arith_operand (op, mode)
1165 rtx op;
1166 enum machine_mode mode;
1168 if (TARGET_64BIT
1169 && mode == DImode
1170 && GET_CODE (op) == SIGN_EXTEND
1171 && GET_MODE (op) == DImode
1172 && GET_MODE (XEXP (op, 0)) == SImode
1173 && register_operand (XEXP (op, 0), SImode))
1174 return 1;
1176 return arith_operand (op, mode);
1179 /* Like nonmemory_operand, but when in 64 bit mode also accept a sign
1180 extend of a 32 bit register, since the value is known to be already
1181 sign extended. */
1184 se_nonmemory_operand (op, mode)
1185 rtx op;
1186 enum machine_mode mode;
1188 if (TARGET_64BIT
1189 && mode == DImode
1190 && GET_CODE (op) == SIGN_EXTEND
1191 && GET_MODE (op) == DImode
1192 && GET_MODE (XEXP (op, 0)) == SImode
1193 && register_operand (XEXP (op, 0), SImode))
1194 return 1;
1196 return nonmemory_operand (op, mode);
1199 /* Like nonimmediate_operand, but when in 64 bit mode also accept a
1200 sign extend of a 32 bit register, since the value is known to be
1201 already sign extended. */
1204 se_nonimmediate_operand (op, mode)
1205 rtx op;
1206 enum machine_mode mode;
1208 if (TARGET_64BIT
1209 && mode == DImode
1210 && GET_CODE (op) == SIGN_EXTEND
1211 && GET_MODE (op) == DImode
1212 && GET_MODE (XEXP (op, 0)) == SImode
1213 && register_operand (XEXP (op, 0), SImode))
1214 return 1;
1216 return nonimmediate_operand (op, mode);
1219 /* Accept any operand that can appear in a mips16 constant table
1220 instruction. We can't use any of the standard operand functions
1221 because for these instructions we accept values that are not
1222 accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs. */
1225 consttable_operand (op, mode)
1226 rtx op;
1227 enum machine_mode mode ATTRIBUTE_UNUSED;
1229 return CONSTANT_P (op);
1232 /* Return nonzero if we split the address into high and low parts. */
1234 /* ??? We should also handle reg+array somewhere. We get four
1235 instructions currently, lui %hi/addui %lo/addui reg/lw. Better is
1236 lui %hi/addui reg/lw %lo. Fixing GO_IF_LEGITIMATE_ADDRESS to accept
1237 (plus (reg) (symbol_ref)) doesn't work because the SYMBOL_REF is broken
1238 out of the address, then we have 4 instructions to combine. Perhaps
1239 add a 3->2 define_split for combine. */
1241 /* ??? We could also split a CONST_INT here if it is a large_int().
1242 However, it doesn't seem to be very useful to have %hi(constant).
1243 We would be better off by doing the masking ourselves and then putting
1244 the explicit high part of the constant in the RTL. This will give better
1245 optimization. Also, %hi(constant) needs assembler changes to work.
1246 There is already a define_split that does this. */
1249 mips_check_split (address, mode)
1250 rtx address;
1251 enum machine_mode mode;
1253 /* ??? This is the same check used in simple_memory_operand.
1254 We use it here because LO_SUM is not offsettable. */
1255 if (GET_MODE_SIZE (mode) > (unsigned) UNITS_PER_WORD)
1256 return 0;
1258 if ((GET_CODE (address) == SYMBOL_REF && ! SYMBOL_REF_FLAG (address))
1259 || (GET_CODE (address) == CONST
1260 && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF
1261 && ! SYMBOL_REF_FLAG (XEXP (XEXP (address, 0), 0)))
1262 || GET_CODE (address) == LABEL_REF)
1263 return 1;
1265 return 0;
1268 /* This function is used to implement REG_MODE_OK_FOR_BASE_P. */
1271 mips_reg_mode_ok_for_base_p (reg, mode, strict)
1272 rtx reg;
1273 enum machine_mode mode;
1274 int strict;
1276 return (strict
1277 ? REGNO_MODE_OK_FOR_BASE_P (REGNO (reg), mode)
1278 : GP_REG_OR_PSEUDO_NONSTRICT_P (REGNO (reg), mode));
1281 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS. It
1282 returns a nonzero value if XINSN is a legitimate address for a
1283 memory operand of the indicated MODE. STRICT is non-zero if this
1284 function is called during reload. */
1287 mips_legitimate_address_p (mode, xinsn, strict)
1288 enum machine_mode mode;
1289 rtx xinsn;
1290 int strict;
1292 if (TARGET_DEBUG_B_MODE)
1294 GO_PRINTF2 ("\n========== GO_IF_LEGITIMATE_ADDRESS, %sstrict\n",
1295 strict ? "" : "not ");
1296 GO_DEBUG_RTX (xinsn);
1299 /* Check for constant before stripping off SUBREG, so that we don't
1300 accept (subreg (const_int)) which will fail to reload. */
1301 if (CONSTANT_ADDRESS_P (xinsn)
1302 && ! (mips_split_addresses && mips_check_split (xinsn, mode))
1303 && (! TARGET_MIPS16 || mips16_constant (xinsn, mode, 1, 0)))
1304 return 1;
1306 while (GET_CODE (xinsn) == SUBREG)
1307 xinsn = SUBREG_REG (xinsn);
1309 /* The mips16 can only use the stack pointer as a base register when
1310 loading SImode or DImode values. */
1311 if (GET_CODE (xinsn) == REG
1312 && mips_reg_mode_ok_for_base_p (xinsn, mode, strict))
1313 return 1;
1315 if (GET_CODE (xinsn) == LO_SUM && mips_split_addresses)
1317 register rtx xlow0 = XEXP (xinsn, 0);
1318 register rtx xlow1 = XEXP (xinsn, 1);
1320 while (GET_CODE (xlow0) == SUBREG)
1321 xlow0 = SUBREG_REG (xlow0);
1322 if (GET_CODE (xlow0) == REG
1323 && mips_reg_mode_ok_for_base_p (xlow0, mode, strict)
1324 && mips_check_split (xlow1, mode))
1325 return 1;
1328 if (GET_CODE (xinsn) == PLUS)
1330 register rtx xplus0 = XEXP (xinsn, 0);
1331 register rtx xplus1 = XEXP (xinsn, 1);
1332 register enum rtx_code code0;
1333 register enum rtx_code code1;
1335 while (GET_CODE (xplus0) == SUBREG)
1336 xplus0 = SUBREG_REG (xplus0);
1337 code0 = GET_CODE (xplus0);
1339 while (GET_CODE (xplus1) == SUBREG)
1340 xplus1 = SUBREG_REG (xplus1);
1341 code1 = GET_CODE (xplus1);
1343 /* The mips16 can only use the stack pointer as a base register
1344 when loading SImode or DImode values. */
1345 if (code0 == REG
1346 && mips_reg_mode_ok_for_base_p (xplus0, mode, strict))
1348 if (code1 == CONST_INT && SMALL_INT (xplus1))
1349 return 1;
1351 /* On the mips16, we represent GP relative offsets in RTL.
1352 These are 16 bit signed values, and can serve as register
1353 offsets. */
1354 if (TARGET_MIPS16
1355 && mips16_gp_offset_p (xplus1))
1356 return 1;
1358 /* For some code sequences, you actually get better code by
1359 pretending that the MIPS supports an address mode of a
1360 constant address + a register, even though the real
1361 machine doesn't support it. This is because the
1362 assembler can use $r1 to load just the high 16 bits, add
1363 in the register, and fold the low 16 bits into the memory
1364 reference, whereas the compiler generates a 4 instruction
1365 sequence. On the other hand, CSE is not as effective.
1366 It would be a win to generate the lui directly, but the
1367 MIPS assembler does not have syntax to generate the
1368 appropriate relocation. */
1370 /* Also accept CONST_INT addresses here, so no else. */
1371 /* Reject combining an embedded PIC text segment reference
1372 with a register. That requires an additional
1373 instruction. */
1374 /* ??? Reject combining an address with a register for the MIPS
1375 64 bit ABI, because the SGI assembler can not handle this. */
1376 if (!TARGET_DEBUG_A_MODE
1377 && (mips_abi == ABI_32
1378 || mips_abi == ABI_O64
1379 || mips_abi == ABI_EABI)
1380 && CONSTANT_ADDRESS_P (xplus1)
1381 && ! mips_split_addresses
1382 && (!TARGET_EMBEDDED_PIC
1383 || code1 != CONST
1384 || GET_CODE (XEXP (xplus1, 0)) != MINUS)
1385 /* When assembling for machines with 64 bit registers,
1386 the assembler will sign-extend the constant "foo"
1387 in "la x, foo(x)" yielding the wrong result for:
1388 (set (blah:DI) (plus x y)). */
1389 && (!TARGET_64BIT
1390 || (code1 == CONST_INT
1391 && trunc_int_for_mode (INTVAL (xplus1),
1392 SImode) == INTVAL (xplus1)))
1393 && !TARGET_MIPS16)
1394 return 1;
1398 if (TARGET_DEBUG_B_MODE)
1399 GO_PRINTF ("Not a legitimate address\n");
1401 /* The address was not legitimate. */
1402 return 0;
1406 /* We need a lot of little routines to check constant values on the
1407 mips16. These are used to figure out how long the instruction will
1408 be. It would be much better to do this using constraints, but
1409 there aren't nearly enough letters available. */
1411 static int
1412 m16_check_op (op, low, high, mask)
1413 rtx op;
1414 int low;
1415 int high;
1416 int mask;
1418 return (GET_CODE (op) == CONST_INT
1419 && INTVAL (op) >= low
1420 && INTVAL (op) <= high
1421 && (INTVAL (op) & mask) == 0);
1425 m16_uimm3_b (op, mode)
1426 rtx op;
1427 enum machine_mode mode ATTRIBUTE_UNUSED;
1429 return m16_check_op (op, 0x1, 0x8, 0);
1433 m16_simm4_1 (op, mode)
1434 rtx op;
1435 enum machine_mode mode ATTRIBUTE_UNUSED;
1437 return m16_check_op (op, - 0x8, 0x7, 0);
1441 m16_nsimm4_1 (op, mode)
1442 rtx op;
1443 enum machine_mode mode ATTRIBUTE_UNUSED;
1445 return m16_check_op (op, - 0x7, 0x8, 0);
1449 m16_simm5_1 (op, mode)
1450 rtx op;
1451 enum machine_mode mode ATTRIBUTE_UNUSED;
1453 return m16_check_op (op, - 0x10, 0xf, 0);
1457 m16_nsimm5_1 (op, mode)
1458 rtx op;
1459 enum machine_mode mode ATTRIBUTE_UNUSED;
1461 return m16_check_op (op, - 0xf, 0x10, 0);
1465 m16_uimm5_4 (op, mode)
1466 rtx op;
1467 enum machine_mode mode ATTRIBUTE_UNUSED;
1469 return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
1473 m16_nuimm5_4 (op, mode)
1474 rtx op;
1475 enum machine_mode mode ATTRIBUTE_UNUSED;
1477 return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
1481 m16_simm8_1 (op, mode)
1482 rtx op;
1483 enum machine_mode mode ATTRIBUTE_UNUSED;
1485 return m16_check_op (op, - 0x80, 0x7f, 0);
1489 m16_nsimm8_1 (op, mode)
1490 rtx op;
1491 enum machine_mode mode ATTRIBUTE_UNUSED;
1493 return m16_check_op (op, - 0x7f, 0x80, 0);
1497 m16_uimm8_1 (op, mode)
1498 rtx op;
1499 enum machine_mode mode ATTRIBUTE_UNUSED;
1501 return m16_check_op (op, 0x0, 0xff, 0);
1505 m16_nuimm8_1 (op, mode)
1506 rtx op;
1507 enum machine_mode mode ATTRIBUTE_UNUSED;
1509 return m16_check_op (op, - 0xff, 0x0, 0);
1513 m16_uimm8_m1_1 (op, mode)
1514 rtx op;
1515 enum machine_mode mode ATTRIBUTE_UNUSED;
1517 return m16_check_op (op, - 0x1, 0xfe, 0);
1521 m16_uimm8_4 (op, mode)
1522 rtx op;
1523 enum machine_mode mode ATTRIBUTE_UNUSED;
1525 return m16_check_op (op, 0x0, 0xff << 2, 3);
1529 m16_nuimm8_4 (op, mode)
1530 rtx op;
1531 enum machine_mode mode ATTRIBUTE_UNUSED;
1533 return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
1537 m16_simm8_8 (op, mode)
1538 rtx op;
1539 enum machine_mode mode ATTRIBUTE_UNUSED;
1541 return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
1545 m16_nsimm8_8 (op, mode)
1546 rtx op;
1547 enum machine_mode mode ATTRIBUTE_UNUSED;
1549 return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
1552 /* References to the string table on the mips16 only use a small
1553 offset if the function is small. See the comment in the SYMBOL_REF
1554 case in simple_memory_operand. We can't check for LABEL_REF here,
1555 because the offset is always large if the label is before the
1556 referencing instruction. */
1559 m16_usym8_4 (op, mode)
1560 rtx op;
1561 enum machine_mode mode ATTRIBUTE_UNUSED;
1563 if (GET_CODE (op) == SYMBOL_REF
1564 && SYMBOL_REF_FLAG (op)
1565 && current_frame_info.insns_len > 0
1566 && XSTR (op, 0)[0] == '*'
1567 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1568 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1569 && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1570 < 4 * 0x100))
1572 struct string_constant *l;
1574 /* Make sure this symbol is on thelist of string constants to be
1575 output for this function. It is possible that it has already
1576 been output, in which case this requires a large offset. */
1577 for (l = string_constants; l != NULL; l = l->next)
1578 if (strcmp (l->label, XSTR (op, 0)) == 0)
1579 return 1;
1582 return 0;
1586 m16_usym5_4 (op, mode)
1587 rtx op;
1588 enum machine_mode mode ATTRIBUTE_UNUSED;
1590 if (GET_CODE (op) == SYMBOL_REF
1591 && SYMBOL_REF_FLAG (op)
1592 && current_frame_info.insns_len > 0
1593 && XSTR (op, 0)[0] == '*'
1594 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1595 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1596 && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1597 < 4 * 0x20))
1599 struct string_constant *l;
1601 /* Make sure this symbol is on thelist of string constants to be
1602 output for this function. It is possible that it has already
1603 been output, in which case this requires a large offset. */
1604 for (l = string_constants; l != NULL; l = l->next)
1605 if (strcmp (l->label, XSTR (op, 0)) == 0)
1606 return 1;
1609 return 0;
1612 /* Returns an operand string for the given instruction's delay slot,
1613 after updating filled delay slot statistics.
1615 We assume that operands[0] is the target register that is set.
1617 In order to check the next insn, most of this functionality is moved
1618 to FINAL_PRESCAN_INSN, and we just set the global variables that
1619 it needs. */
1621 /* ??? This function no longer does anything useful, because final_prescan_insn
1622 now will never emit a nop. */
1624 const char *
1625 mips_fill_delay_slot (ret, type, operands, cur_insn)
1626 const char *ret; /* normal string to return */
1627 enum delay_type type; /* type of delay */
1628 rtx operands[]; /* operands to use */
1629 rtx cur_insn; /* current insn */
1631 register rtx set_reg;
1632 register enum machine_mode mode;
1633 register rtx next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL_RTX;
1634 register int num_nops;
1636 if (type == DELAY_LOAD || type == DELAY_FCMP)
1637 num_nops = 1;
1639 else if (type == DELAY_HILO)
1640 num_nops = 2;
1642 else
1643 num_nops = 0;
1645 /* Make sure that we don't put nop's after labels. */
1646 next_insn = NEXT_INSN (cur_insn);
1647 while (next_insn != 0 && GET_CODE (next_insn) == NOTE)
1648 next_insn = NEXT_INSN (next_insn);
1650 dslots_load_total += num_nops;
1651 if (TARGET_DEBUG_F_MODE
1652 || !optimize
1653 || type == DELAY_NONE
1654 || operands == 0
1655 || cur_insn == 0
1656 || next_insn == 0
1657 || GET_CODE (next_insn) == CODE_LABEL
1658 || (set_reg = operands[0]) == 0)
1660 dslots_number_nops = 0;
1661 mips_load_reg = 0;
1662 mips_load_reg2 = 0;
1663 mips_load_reg3 = 0;
1664 mips_load_reg4 = 0;
1665 return ret;
1668 set_reg = operands[0];
1669 if (set_reg == 0)
1670 return ret;
1672 while (GET_CODE (set_reg) == SUBREG)
1673 set_reg = SUBREG_REG (set_reg);
1675 mode = GET_MODE (set_reg);
1676 dslots_number_nops = num_nops;
1677 mips_load_reg = set_reg;
1678 if (GET_MODE_SIZE (mode)
1679 > (unsigned) (FP_REG_P (REGNO (set_reg)) ? UNITS_PER_FPREG : UNITS_PER_WORD))
1680 mips_load_reg2 = gen_rtx_REG (SImode, REGNO (set_reg) + 1);
1681 else
1682 mips_load_reg2 = 0;
1684 if (type == DELAY_HILO)
1686 mips_load_reg3 = gen_rtx_REG (SImode, MD_REG_FIRST);
1687 mips_load_reg4 = gen_rtx_REG (SImode, MD_REG_FIRST+1);
1689 else
1691 mips_load_reg3 = 0;
1692 mips_load_reg4 = 0;
1695 return ret;
1699 /* Determine whether a memory reference takes one (based off of the GP
1700 pointer), two (normal), or three (label + reg) instructions, and bump the
1701 appropriate counter for -mstats. */
1703 void
1704 mips_count_memory_refs (op, num)
1705 rtx op;
1706 int num;
1708 int additional = 0;
1709 int n_words = 0;
1710 rtx addr, plus0, plus1;
1711 enum rtx_code code0, code1;
1712 int looping;
1714 if (TARGET_DEBUG_B_MODE)
1716 fprintf (stderr, "\n========== mips_count_memory_refs:\n");
1717 debug_rtx (op);
1720 /* Skip MEM if passed, otherwise handle movsi of address. */
1721 addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
1723 /* Loop, going through the address RTL. */
1726 looping = FALSE;
1727 switch (GET_CODE (addr))
1729 case REG:
1730 case CONST_INT:
1731 case LO_SUM:
1732 break;
1734 case PLUS:
1735 plus0 = XEXP (addr, 0);
1736 plus1 = XEXP (addr, 1);
1737 code0 = GET_CODE (plus0);
1738 code1 = GET_CODE (plus1);
1740 if (code0 == REG)
1742 additional++;
1743 addr = plus1;
1744 looping = 1;
1745 continue;
1748 if (code0 == CONST_INT)
1750 addr = plus1;
1751 looping = 1;
1752 continue;
1755 if (code1 == REG)
1757 additional++;
1758 addr = plus0;
1759 looping = 1;
1760 continue;
1763 if (code1 == CONST_INT)
1765 addr = plus0;
1766 looping = 1;
1767 continue;
1770 if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
1772 addr = plus0;
1773 looping = 1;
1774 continue;
1777 if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
1779 addr = plus1;
1780 looping = 1;
1781 continue;
1784 break;
1786 case LABEL_REF:
1787 n_words = 2; /* always 2 words */
1788 break;
1790 case CONST:
1791 addr = XEXP (addr, 0);
1792 looping = 1;
1793 continue;
1795 case SYMBOL_REF:
1796 n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
1797 break;
1799 default:
1800 break;
1803 while (looping);
1805 if (n_words == 0)
1806 return;
1808 n_words += additional;
1809 if (n_words > 3)
1810 n_words = 3;
1812 num_refs[n_words-1] += num;
1816 /* Return RTL for the offset from the current function to the argument.
1818 ??? Which argument is this? */
1821 embedded_pic_offset (x)
1822 rtx x;
1824 if (embedded_pic_fnaddr_rtx == NULL)
1826 rtx seq;
1828 embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
1830 /* Output code at function start to initialize the pseudo-reg. */
1831 /* ??? We used to do this in FINALIZE_PIC, but that does not work for
1832 inline functions, because it is called after RTL for the function
1833 has been copied. The pseudo-reg in embedded_pic_fnaddr_rtx however
1834 does not get copied, and ends up not matching the rest of the RTL.
1835 This solution works, but means that we get unnecessary code to
1836 initialize this value every time a function is inlined into another
1837 function. */
1838 start_sequence ();
1839 emit_insn (gen_get_fnaddr (embedded_pic_fnaddr_rtx,
1840 XEXP (DECL_RTL (current_function_decl), 0)));
1841 seq = gen_sequence ();
1842 end_sequence ();
1843 push_topmost_sequence ();
1844 emit_insn_after (seq, get_insns ());
1845 pop_topmost_sequence ();
1848 return
1849 gen_rtx_CONST (Pmode,
1850 gen_rtx_MINUS (Pmode, x,
1851 XEXP (DECL_RTL (current_function_decl), 0)));
1854 /* Return the appropriate instructions to move one operand to another. */
1856 const char *
1857 mips_move_1word (operands, insn, unsignedp)
1858 rtx operands[];
1859 rtx insn;
1860 int unsignedp;
1862 const char *ret = 0;
1863 rtx op0 = operands[0];
1864 rtx op1 = operands[1];
1865 enum rtx_code code0 = GET_CODE (op0);
1866 enum rtx_code code1 = GET_CODE (op1);
1867 enum machine_mode mode = GET_MODE (op0);
1868 int subreg_offset0 = 0;
1869 int subreg_offset1 = 0;
1870 enum delay_type delay = DELAY_NONE;
1872 while (code0 == SUBREG)
1874 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
1875 GET_MODE (SUBREG_REG (op0)),
1876 SUBREG_BYTE (op0),
1877 GET_MODE (op0));
1878 op0 = SUBREG_REG (op0);
1879 code0 = GET_CODE (op0);
1882 while (code1 == SUBREG)
1884 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
1885 GET_MODE (SUBREG_REG (op1)),
1886 SUBREG_BYTE (op1),
1887 GET_MODE (op1));
1888 op1 = SUBREG_REG (op1);
1889 code1 = GET_CODE (op1);
1892 /* For our purposes, a condition code mode is the same as SImode. */
1893 if (mode == CCmode)
1894 mode = SImode;
1896 if (code0 == REG)
1898 int regno0 = REGNO (op0) + subreg_offset0;
1900 if (code1 == REG)
1902 int regno1 = REGNO (op1) + subreg_offset1;
1904 /* Just in case, don't do anything for assigning a register
1905 to itself, unless we are filling a delay slot. */
1906 if (regno0 == regno1 && set_nomacro == 0)
1907 ret = "";
1909 else if (GP_REG_P (regno0))
1911 if (GP_REG_P (regno1))
1912 ret = "move\t%0,%1";
1914 else if (MD_REG_P (regno1))
1916 delay = DELAY_HILO;
1917 if (regno1 != HILO_REGNUM)
1918 ret = "mf%1\t%0";
1919 else
1920 ret = "mflo\t%0";
1923 else if (ST_REG_P (regno1) && ISA_HAS_8CC)
1924 ret = "li\t%0,1\n\tmovf\t%0,%.,%1";
1926 else
1928 delay = DELAY_LOAD;
1929 if (FP_REG_P (regno1))
1930 ret = "mfc1\t%0,%1";
1932 else if (regno1 == FPSW_REGNUM && ! ISA_HAS_8CC)
1933 ret = "cfc1\t%0,$31";
1937 else if (FP_REG_P (regno0))
1939 if (GP_REG_P (regno1))
1941 delay = DELAY_LOAD;
1942 ret = "mtc1\t%1,%0";
1945 if (FP_REG_P (regno1))
1946 ret = "mov.s\t%0,%1";
1949 else if (MD_REG_P (regno0))
1951 if (GP_REG_P (regno1))
1953 delay = DELAY_HILO;
1954 if (regno0 != HILO_REGNUM && ! TARGET_MIPS16)
1955 ret = "mt%0\t%1";
1959 else if (regno0 == FPSW_REGNUM && ! ISA_HAS_8CC)
1961 if (GP_REG_P (regno1))
1963 delay = DELAY_LOAD;
1964 ret = "ctc1\t%0,$31";
1969 else if (code1 == MEM)
1971 delay = DELAY_LOAD;
1973 if (TARGET_STATS)
1974 mips_count_memory_refs (op1, 1);
1976 if (GP_REG_P (regno0))
1978 /* For loads, use the mode of the memory item, instead of the
1979 target, so zero/sign extend can use this code as well. */
1980 switch (GET_MODE (op1))
1982 default:
1983 break;
1984 case SFmode:
1985 ret = "lw\t%0,%1";
1986 break;
1987 case SImode:
1988 case CCmode:
1989 ret = ((unsignedp && TARGET_64BIT)
1990 ? "lwu\t%0,%1"
1991 : "lw\t%0,%1");
1992 break;
1993 case HImode:
1994 ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
1995 break;
1996 case QImode:
1997 ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
1998 break;
2002 else if (FP_REG_P (regno0) && (mode == SImode || mode == SFmode))
2003 ret = "l.s\t%0,%1";
2005 if (ret != (char *)0 && MEM_VOLATILE_P (op1))
2007 size_t i = strlen (ret);
2008 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2009 abort ();
2011 sprintf (volatile_buffer, "%%{%s%%}", ret);
2012 ret = volatile_buffer;
2016 else if (code1 == CONST_INT
2017 || (code1 == CONST_DOUBLE
2018 && GET_MODE (op1) == VOIDmode))
2020 if (code1 == CONST_DOUBLE)
2022 /* This can happen when storing constants into long long
2023 bitfields. Just store the least significant word of
2024 the value. */
2025 operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2028 if (INTVAL (op1) == 0 && ! TARGET_MIPS16)
2030 if (GP_REG_P (regno0))
2031 ret = "move\t%0,%z1";
2033 else if (FP_REG_P (regno0))
2035 delay = DELAY_LOAD;
2036 ret = "mtc1\t%z1,%0";
2039 else if (MD_REG_P (regno0))
2041 delay = DELAY_HILO;
2042 ret = "mt%0\t%.";
2046 else if (GP_REG_P (regno0))
2048 /* Don't use X format, because that will give out of
2049 range numbers for 64 bit host and 32 bit target. */
2050 if (! TARGET_MIPS16)
2051 ret = "li\t%0,%1\t\t\t# %X1";
2052 else
2054 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2055 ret = "li\t%0,%1";
2056 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2057 ret = "li\t%0,%n1\n\tneg\t%0";
2062 else if (code1 == CONST_DOUBLE && mode == SFmode)
2064 if (op1 == CONST0_RTX (SFmode))
2066 if (GP_REG_P (regno0))
2067 ret = "move\t%0,%.";
2069 else if (FP_REG_P (regno0))
2071 delay = DELAY_LOAD;
2072 ret = "mtc1\t%.,%0";
2076 else
2078 delay = DELAY_LOAD;
2079 ret = "li.s\t%0,%1";
2083 else if (code1 == LABEL_REF)
2085 if (TARGET_STATS)
2086 mips_count_memory_refs (op1, 1);
2088 ret = "la\t%0,%a1";
2091 else if (code1 == SYMBOL_REF || code1 == CONST)
2093 if (HALF_PIC_P () && CONSTANT_P (op1) && HALF_PIC_ADDRESS_P (op1))
2095 rtx offset = const0_rtx;
2097 if (GET_CODE (op1) == CONST)
2098 op1 = eliminate_constant_term (XEXP (op1, 0), &offset);
2100 if (GET_CODE (op1) == SYMBOL_REF)
2102 operands[2] = HALF_PIC_PTR (op1);
2104 if (TARGET_STATS)
2105 mips_count_memory_refs (operands[2], 1);
2107 if (INTVAL (offset) == 0)
2109 delay = DELAY_LOAD;
2110 ret = (unsignedp && TARGET_64BIT
2111 ? "lwu\t%0,%2"
2112 : "lw\t%0,%2");
2114 else
2116 dslots_load_total++;
2117 operands[3] = offset;
2118 if (unsignedp && TARGET_64BIT)
2119 ret = (SMALL_INT (offset)
2120 ? "lwu\t%0,%2%#\n\tadd\t%0,%0,%3"
2121 : "lwu\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
2122 else
2123 ret = (SMALL_INT (offset)
2124 ? "lw\t%0,%2%#\n\tadd\t%0,%0,%3"
2125 : "lw\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
2129 else if (TARGET_MIPS16
2130 && code1 == CONST
2131 && GET_CODE (XEXP (op1, 0)) == REG
2132 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2134 /* This case arises on the mips16; see
2135 mips16_gp_pseudo_reg. */
2136 ret = "move\t%0,%+";
2138 else if (TARGET_MIPS16
2139 && code1 == SYMBOL_REF
2140 && SYMBOL_REF_FLAG (op1)
2141 && (XSTR (op1, 0)[0] != '*'
2142 || strncmp (XSTR (op1, 0) + 1,
2143 LOCAL_LABEL_PREFIX,
2144 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2146 /* This can occur when reloading the address of a GP
2147 relative symbol on the mips16. */
2148 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2150 else
2152 if (TARGET_STATS)
2153 mips_count_memory_refs (op1, 1);
2155 ret = "la\t%0,%a1";
2159 else if (code1 == PLUS)
2161 rtx add_op0 = XEXP (op1, 0);
2162 rtx add_op1 = XEXP (op1, 1);
2164 if (GET_CODE (XEXP (op1, 1)) == REG
2165 && GET_CODE (XEXP (op1, 0)) == CONST_INT)
2166 add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
2168 operands[2] = add_op0;
2169 operands[3] = add_op1;
2170 ret = "add%:\t%0,%2,%3";
2173 else if (code1 == HIGH)
2175 operands[1] = XEXP (op1, 0);
2176 ret = "lui\t%0,%%hi(%1)";
2180 else if (code0 == MEM)
2182 if (TARGET_STATS)
2183 mips_count_memory_refs (op0, 1);
2185 if (code1 == REG)
2187 int regno1 = REGNO (op1) + subreg_offset1;
2189 if (GP_REG_P (regno1))
2191 switch (mode)
2193 case SFmode: ret = "sw\t%1,%0"; break;
2194 case SImode: ret = "sw\t%1,%0"; break;
2195 case HImode: ret = "sh\t%1,%0"; break;
2196 case QImode: ret = "sb\t%1,%0"; break;
2197 default: break;
2201 else if (FP_REG_P (regno1) && (mode == SImode || mode == SFmode))
2202 ret = "s.s\t%1,%0";
2205 else if (code1 == CONST_INT && INTVAL (op1) == 0)
2207 switch (mode)
2209 case SFmode: ret = "sw\t%z1,%0"; break;
2210 case SImode: ret = "sw\t%z1,%0"; break;
2211 case HImode: ret = "sh\t%z1,%0"; break;
2212 case QImode: ret = "sb\t%z1,%0"; break;
2213 default: break;
2217 else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
2219 switch (mode)
2221 case SFmode: ret = "sw\t%.,%0"; break;
2222 case SImode: ret = "sw\t%.,%0"; break;
2223 case HImode: ret = "sh\t%.,%0"; break;
2224 case QImode: ret = "sb\t%.,%0"; break;
2225 default: break;
2229 if (ret != 0 && MEM_VOLATILE_P (op0))
2231 size_t i = strlen (ret);
2233 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2234 abort ();
2236 sprintf (volatile_buffer, "%%{%s%%}", ret);
2237 ret = volatile_buffer;
2241 if (ret == 0)
2243 abort_with_insn (insn, "bad move");
2244 return 0;
2247 if (delay != DELAY_NONE)
2248 return mips_fill_delay_slot (ret, delay, operands, insn);
2250 return ret;
2254 /* Return the appropriate instructions to move 2 words */
2256 const char *
2257 mips_move_2words (operands, insn)
2258 rtx operands[];
2259 rtx insn;
2261 const char *ret = 0;
2262 rtx op0 = operands[0];
2263 rtx op1 = operands[1];
2264 enum rtx_code code0 = GET_CODE (operands[0]);
2265 enum rtx_code code1 = GET_CODE (operands[1]);
2266 int subreg_offset0 = 0;
2267 int subreg_offset1 = 0;
2268 enum delay_type delay = DELAY_NONE;
2270 while (code0 == SUBREG)
2272 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2273 GET_MODE (SUBREG_REG (op0)),
2274 SUBREG_BYTE (op0),
2275 GET_MODE (op0));
2276 op0 = SUBREG_REG (op0);
2277 code0 = GET_CODE (op0);
2280 if (code1 == SIGN_EXTEND)
2282 op1 = XEXP (op1, 0);
2283 code1 = GET_CODE (op1);
2286 while (code1 == SUBREG)
2288 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2289 GET_MODE (SUBREG_REG (op1)),
2290 SUBREG_BYTE (op1),
2291 GET_MODE (op1));
2292 op1 = SUBREG_REG (op1);
2293 code1 = GET_CODE (op1);
2296 /* Sanity check. */
2297 if (GET_CODE (operands[1]) == SIGN_EXTEND
2298 && code1 != REG
2299 && code1 != CONST_INT
2300 /* The following three can happen as the result of a questionable
2301 cast. */
2302 && code1 != LABEL_REF
2303 && code1 != SYMBOL_REF
2304 && code1 != CONST)
2305 abort ();
2307 if (code0 == REG)
2309 int regno0 = REGNO (op0) + subreg_offset0;
2311 if (code1 == REG)
2313 int regno1 = REGNO (op1) + subreg_offset1;
2315 /* Just in case, don't do anything for assigning a register
2316 to itself, unless we are filling a delay slot. */
2317 if (regno0 == regno1 && set_nomacro == 0)
2318 ret = "";
2320 else if (FP_REG_P (regno0))
2322 if (FP_REG_P (regno1))
2323 ret = "mov.d\t%0,%1";
2325 else
2327 delay = DELAY_LOAD;
2328 if (TARGET_FLOAT64)
2330 if (!TARGET_64BIT)
2331 abort_with_insn (insn, "bad move");
2333 #ifdef TARGET_FP_CALL_32
2334 if (FP_CALL_GP_REG_P (regno1))
2335 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tdmtc1\t%1,%0";
2336 else
2337 #endif
2338 ret = "dmtc1\t%1,%0";
2340 else
2341 ret = "mtc1\t%L1,%0\n\tmtc1\t%M1,%D0";
2345 else if (FP_REG_P (regno1))
2347 delay = DELAY_LOAD;
2348 if (TARGET_FLOAT64)
2350 if (!TARGET_64BIT)
2351 abort_with_insn (insn, "bad move");
2353 #ifdef TARGET_FP_CALL_32
2354 if (FP_CALL_GP_REG_P (regno0))
2355 ret = "dmfc1\t%0,%1\n\tmfc1\t%D0,%1\n\tdsrl\t%0,32";
2356 else
2357 #endif
2358 ret = "dmfc1\t%0,%1";
2360 else
2361 ret = "mfc1\t%L0,%1\n\tmfc1\t%M0,%D1";
2364 else if (MD_REG_P (regno0) && GP_REG_P (regno1) && !TARGET_MIPS16)
2366 delay = DELAY_HILO;
2367 if (TARGET_64BIT)
2369 if (regno0 != HILO_REGNUM)
2370 ret = "mt%0\t%1";
2371 else if (regno1 == 0)
2372 ret = "mtlo\t%.\n\tmthi\t%.";
2374 else
2375 ret = "mthi\t%M1\n\tmtlo\t%L1";
2378 else if (GP_REG_P (regno0) && MD_REG_P (regno1))
2380 delay = DELAY_HILO;
2381 if (TARGET_64BIT)
2383 if (regno1 != HILO_REGNUM)
2384 ret = "mf%1\t%0";
2386 else
2387 ret = "mfhi\t%M0\n\tmflo\t%L0";
2390 else if (TARGET_64BIT)
2391 ret = "move\t%0,%1";
2393 else if (regno0 != (regno1+1))
2394 ret = "move\t%0,%1\n\tmove\t%D0,%D1";
2396 else
2397 ret = "move\t%D0,%D1\n\tmove\t%0,%1";
2400 else if (code1 == CONST_DOUBLE)
2402 /* Move zero from $0 unless !TARGET_64BIT and recipient
2403 is 64-bit fp reg, in which case generate a constant. */
2404 if (op1 != CONST0_RTX (GET_MODE (op1))
2405 || (TARGET_FLOAT64 && !TARGET_64BIT && FP_REG_P (regno0)))
2407 if (GET_MODE (op1) == DFmode)
2409 delay = DELAY_LOAD;
2411 #ifdef TARGET_FP_CALL_32
2412 if (FP_CALL_GP_REG_P (regno0))
2414 if (TARGET_FLOAT64 && !TARGET_64BIT)
2416 split_double (op1, operands + 2, operands + 3);
2417 ret = "li\t%0,%2\n\tli\t%D0,%3";
2419 else
2420 ret = "li.d\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32";
2422 else
2423 #endif
2424 /* GNU as emits 64-bit code for li.d if the ISA is 3
2425 or higher. For !TARGET_64BIT && gp registers we
2426 need to avoid this by using two li instructions
2427 instead. */
2428 if (ISA_HAS_64BIT_REGS
2429 && ! TARGET_64BIT
2430 && ! FP_REG_P (regno0))
2432 split_double (op1, operands + 2, operands + 3);
2433 ret = "li\t%0,%2\n\tli\t%D0,%3";
2435 else
2436 ret = "li.d\t%0,%1";
2439 else if (TARGET_64BIT)
2441 if (! TARGET_MIPS16)
2442 ret = "dli\t%0,%1";
2445 else
2447 split_double (op1, operands + 2, operands + 3);
2448 ret = "li\t%0,%2\n\tli\t%D0,%3";
2452 else
2454 if (GP_REG_P (regno0))
2455 ret = (TARGET_64BIT
2456 #ifdef TARGET_FP_CALL_32
2457 && ! FP_CALL_GP_REG_P (regno0)
2458 #endif
2459 ? "move\t%0,%."
2460 : "move\t%0,%.\n\tmove\t%D0,%.");
2462 else if (FP_REG_P (regno0))
2464 delay = DELAY_LOAD;
2465 ret = (TARGET_64BIT
2466 ? "dmtc1\t%.,%0"
2467 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0");
2472 else if (code1 == CONST_INT && INTVAL (op1) == 0 && ! TARGET_MIPS16)
2474 if (GP_REG_P (regno0))
2475 ret = (TARGET_64BIT
2476 ? "move\t%0,%."
2477 : "move\t%0,%.\n\tmove\t%D0,%.");
2479 else if (FP_REG_P (regno0))
2481 delay = DELAY_LOAD;
2482 ret = (TARGET_64BIT
2483 ? "dmtc1\t%.,%0"
2484 : (TARGET_FLOAT64
2485 ? "li.d\t%0,%1"
2486 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0"));
2488 else if (MD_REG_P (regno0))
2490 delay = DELAY_HILO;
2491 ret = (regno0 == HILO_REGNUM
2492 ? "mtlo\t%.\n\tmthi\t%."
2493 : "mt%0\t%.\n");
2497 else if (code1 == CONST_INT && GET_MODE (op0) == DImode
2498 && GP_REG_P (regno0))
2500 if (TARGET_64BIT)
2502 if (TARGET_MIPS16)
2504 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2505 ret = "li\t%0,%1";
2506 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2507 ret = "li\t%0,%n1\n\tneg\t%0";
2509 else if (GET_CODE (operands[1]) == SIGN_EXTEND)
2510 ret = "li\t%0,%1\t\t# %X1";
2511 else if (HOST_BITS_PER_WIDE_INT < 64)
2512 /* We can't use 'X' for negative numbers, because then we won't
2513 get the right value for the upper 32 bits. */
2514 ret = (INTVAL (op1) < 0
2515 ? "dli\t%0,%1\t\t\t# %X1"
2516 : "dli\t%0,%X1\t\t# %1");
2517 else
2518 /* We must use 'X', because otherwise LONG_MIN will print as
2519 a number that the assembler won't accept. */
2520 ret = "dli\t%0,%X1\t\t# %1";
2522 else if (HOST_BITS_PER_WIDE_INT < 64)
2524 operands[2] = GEN_INT (INTVAL (operands[1]) >= 0 ? 0 : -1);
2525 if (TARGET_MIPS16)
2527 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2528 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2529 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2531 operands[2] = GEN_INT (1);
2532 ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2535 else
2536 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2538 else
2540 /* We use multiple shifts here, to avoid warnings about out
2541 of range shifts on 32 bit hosts. */
2542 operands[2] = GEN_INT (INTVAL (operands[1]) >> 16 >> 16);
2543 operands[1]
2544 = GEN_INT (INTVAL (operands[1]) << 16 << 16 >> 16 >> 16);
2545 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2549 else if (code1 == MEM)
2551 delay = DELAY_LOAD;
2553 if (TARGET_STATS)
2554 mips_count_memory_refs (op1, 2);
2556 if (FP_REG_P (regno0))
2557 ret = "l.d\t%0,%1";
2559 else if (TARGET_64BIT)
2562 #ifdef TARGET_FP_CALL_32
2563 if (FP_CALL_GP_REG_P (regno0))
2564 ret = (double_memory_operand (op1, GET_MODE (op1))
2565 ? "lwu\t%0,%1\n\tlwu\t%D0,4+%1"
2566 : "ld\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32");
2567 else
2568 #endif
2569 ret = "ld\t%0,%1";
2572 else if (double_memory_operand (op1, GET_MODE (op1)))
2573 ret = (reg_mentioned_p (op0, op1)
2574 ? "lw\t%D0,%D1\n\tlw\t%0,%1"
2575 : "lw\t%0,%1\n\tlw\t%D0,%D1");
2577 if (ret != 0 && MEM_VOLATILE_P (op1))
2579 size_t i = strlen (ret);
2581 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2582 abort ();
2584 sprintf (volatile_buffer, "%%{%s%%}", ret);
2585 ret = volatile_buffer;
2589 else if (code1 == LABEL_REF)
2591 if (TARGET_STATS)
2592 mips_count_memory_refs (op1, 2);
2594 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2595 /* We deliberately remove the 'a' from '%1', so that we don't
2596 have to add SIGN_EXTEND support to print_operand_address.
2597 print_operand will just call print_operand_address in this
2598 case, so there is no problem. */
2599 ret = "la\t%0,%1";
2600 else
2601 ret = "dla\t%0,%a1";
2603 else if (code1 == SYMBOL_REF || code1 == CONST)
2605 if (TARGET_MIPS16
2606 && code1 == CONST
2607 && GET_CODE (XEXP (op1, 0)) == REG
2608 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2610 /* This case arises on the mips16; see
2611 mips16_gp_pseudo_reg. */
2612 ret = "move\t%0,%+";
2614 else if (TARGET_MIPS16
2615 && code1 == SYMBOL_REF
2616 && SYMBOL_REF_FLAG (op1)
2617 && (XSTR (op1, 0)[0] != '*'
2618 || strncmp (XSTR (op1, 0) + 1,
2619 LOCAL_LABEL_PREFIX,
2620 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2622 /* This can occur when reloading the address of a GP
2623 relative symbol on the mips16. */
2624 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2626 else
2628 if (TARGET_STATS)
2629 mips_count_memory_refs (op1, 2);
2631 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2632 /* We deliberately remove the 'a' from '%1', so that we don't
2633 have to add SIGN_EXTEND support to print_operand_address.
2634 print_operand will just call print_operand_address in this
2635 case, so there is no problem. */
2636 ret = "la\t%0,%1";
2637 else
2638 ret = "dla\t%0,%a1";
2643 else if (code0 == MEM)
2645 if (code1 == REG)
2647 int regno1 = REGNO (op1) + subreg_offset1;
2649 if (FP_REG_P (regno1))
2650 ret = "s.d\t%1,%0";
2652 else if (TARGET_64BIT)
2655 #ifdef TARGET_FP_CALL_32
2656 if (FP_CALL_GP_REG_P (regno1))
2657 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tsd\t%1,%0";
2658 else
2659 #endif
2660 ret = "sd\t%1,%0";
2663 else if (double_memory_operand (op0, GET_MODE (op0)))
2664 ret = "sw\t%1,%0\n\tsw\t%D1,%D0";
2667 else if (((code1 == CONST_INT && INTVAL (op1) == 0)
2668 || (code1 == CONST_DOUBLE
2669 && op1 == CONST0_RTX (GET_MODE (op1))))
2670 && (TARGET_64BIT
2671 || double_memory_operand (op0, GET_MODE (op0))))
2673 if (TARGET_64BIT)
2674 ret = "sd\t%.,%0";
2675 else
2676 ret = "sw\t%.,%0\n\tsw\t%.,%D0";
2679 if (TARGET_STATS)
2680 mips_count_memory_refs (op0, 2);
2682 if (ret != 0 && MEM_VOLATILE_P (op0))
2684 size_t i = strlen (ret);
2686 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2687 abort ();
2689 sprintf (volatile_buffer, "%%{%s%%}", ret);
2690 ret = volatile_buffer;
2694 if (ret == 0)
2696 abort_with_insn (insn, "bad move");
2697 return 0;
2700 if (delay != DELAY_NONE)
2701 return mips_fill_delay_slot (ret, delay, operands, insn);
2703 return ret;
2706 /* Provide the costs of an addressing mode that contains ADDR.
2707 If ADDR is not a valid address, its cost is irrelevant. */
2710 mips_address_cost (addr)
2711 rtx addr;
2713 switch (GET_CODE (addr))
2715 case LO_SUM:
2716 return 1;
2718 case LABEL_REF:
2719 return 2;
2721 case CONST:
2723 rtx offset = const0_rtx;
2724 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
2725 if (GET_CODE (addr) == LABEL_REF)
2726 return 2;
2728 if (GET_CODE (addr) != SYMBOL_REF)
2729 return 4;
2731 if (! SMALL_INT (offset))
2732 return 2;
2735 /* ... fall through ... */
2737 case SYMBOL_REF:
2738 return SYMBOL_REF_FLAG (addr) ? 1 : 2;
2740 case PLUS:
2742 register rtx plus0 = XEXP (addr, 0);
2743 register rtx plus1 = XEXP (addr, 1);
2745 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
2746 plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
2748 if (GET_CODE (plus0) != REG)
2749 break;
2751 switch (GET_CODE (plus1))
2753 case CONST_INT:
2754 return SMALL_INT (plus1) ? 1 : 2;
2756 case CONST:
2757 case SYMBOL_REF:
2758 case LABEL_REF:
2759 case HIGH:
2760 case LO_SUM:
2761 return mips_address_cost (plus1) + 1;
2763 default:
2764 break;
2768 default:
2769 break;
2772 return 4;
2775 /* Return nonzero if X is an address which needs a temporary register when
2776 reloaded while generating PIC code. */
2779 pic_address_needs_scratch (x)
2780 rtx x;
2782 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
2783 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
2784 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
2785 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2786 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
2787 return 1;
2789 return 0;
2792 /* Make normal rtx_code into something we can index from an array */
2794 static enum internal_test
2795 map_test_to_internal_test (test_code)
2796 enum rtx_code test_code;
2798 enum internal_test test = ITEST_MAX;
2800 switch (test_code)
2802 case EQ: test = ITEST_EQ; break;
2803 case NE: test = ITEST_NE; break;
2804 case GT: test = ITEST_GT; break;
2805 case GE: test = ITEST_GE; break;
2806 case LT: test = ITEST_LT; break;
2807 case LE: test = ITEST_LE; break;
2808 case GTU: test = ITEST_GTU; break;
2809 case GEU: test = ITEST_GEU; break;
2810 case LTU: test = ITEST_LTU; break;
2811 case LEU: test = ITEST_LEU; break;
2812 default: break;
2815 return test;
2819 /* Generate the code to compare two integer values. The return value is:
2820 (reg:SI xx) The pseudo register the comparison is in
2821 0 No register, generate a simple branch.
2823 ??? This is called with result nonzero by the Scond patterns in
2824 mips.md. These patterns are called with a target in the mode of
2825 the Scond instruction pattern. Since this must be a constant, we
2826 must use SImode. This means that if RESULT is non-zero, it will
2827 always be an SImode register, even if TARGET_64BIT is true. We
2828 cope with this by calling convert_move rather than emit_move_insn.
2829 This will sometimes lead to an unnecessary extension of the result;
2830 for example:
2832 long long
2833 foo (long long i)
2835 return i < 5;
2841 gen_int_relational (test_code, result, cmp0, cmp1, p_invert)
2842 enum rtx_code test_code; /* relational test (EQ, etc) */
2843 rtx result; /* result to store comp. or 0 if branch */
2844 rtx cmp0; /* first operand to compare */
2845 rtx cmp1; /* second operand to compare */
2846 int *p_invert; /* NULL or ptr to hold whether branch needs */
2847 /* to reverse its test */
2849 struct cmp_info
2851 enum rtx_code test_code; /* code to use in instruction (LT vs. LTU) */
2852 int const_low; /* low bound of constant we can accept */
2853 int const_high; /* high bound of constant we can accept */
2854 int const_add; /* constant to add (convert LE -> LT) */
2855 int reverse_regs; /* reverse registers in test */
2856 int invert_const; /* != 0 if invert value if cmp1 is constant */
2857 int invert_reg; /* != 0 if invert value if cmp1 is register */
2858 int unsignedp; /* != 0 for unsigned comparisons. */
2861 static struct cmp_info info[ (int)ITEST_MAX ] = {
2863 { XOR, 0, 65535, 0, 0, 0, 0, 0 }, /* EQ */
2864 { XOR, 0, 65535, 0, 0, 1, 1, 0 }, /* NE */
2865 { LT, -32769, 32766, 1, 1, 1, 0, 0 }, /* GT */
2866 { LT, -32768, 32767, 0, 0, 1, 1, 0 }, /* GE */
2867 { LT, -32768, 32767, 0, 0, 0, 0, 0 }, /* LT */
2868 { LT, -32769, 32766, 1, 1, 0, 1, 0 }, /* LE */
2869 { LTU, -32769, 32766, 1, 1, 1, 0, 1 }, /* GTU */
2870 { LTU, -32768, 32767, 0, 0, 1, 1, 1 }, /* GEU */
2871 { LTU, -32768, 32767, 0, 0, 0, 0, 1 }, /* LTU */
2872 { LTU, -32769, 32766, 1, 1, 0, 1, 1 }, /* LEU */
2875 enum internal_test test;
2876 enum machine_mode mode;
2877 struct cmp_info *p_info;
2878 int branch_p;
2879 int eqne_p;
2880 int invert;
2881 rtx reg;
2882 rtx reg2;
2884 test = map_test_to_internal_test (test_code);
2885 if (test == ITEST_MAX)
2886 abort ();
2888 p_info = &info[(int) test];
2889 eqne_p = (p_info->test_code == XOR);
2891 mode = GET_MODE (cmp0);
2892 if (mode == VOIDmode)
2893 mode = GET_MODE (cmp1);
2895 /* Eliminate simple branches */
2896 branch_p = (result == 0);
2897 if (branch_p)
2899 if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
2901 /* Comparisons against zero are simple branches */
2902 if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
2903 && (! TARGET_MIPS16 || eqne_p))
2904 return 0;
2906 /* Test for beq/bne. */
2907 if (eqne_p && ! TARGET_MIPS16)
2908 return 0;
2911 /* allocate a pseudo to calculate the value in. */
2912 result = gen_reg_rtx (mode);
2915 /* Make sure we can handle any constants given to us. */
2916 if (GET_CODE (cmp0) == CONST_INT)
2917 cmp0 = force_reg (mode, cmp0);
2919 if (GET_CODE (cmp1) == CONST_INT)
2921 HOST_WIDE_INT value = INTVAL (cmp1);
2923 if (value < p_info->const_low
2924 || value > p_info->const_high
2925 /* ??? Why? And why wasn't the similar code below modified too? */
2926 || (TARGET_64BIT
2927 && HOST_BITS_PER_WIDE_INT < 64
2928 && p_info->const_add != 0
2929 && ((p_info->unsignedp
2930 ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
2931 > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
2932 : (value + p_info->const_add) > INTVAL (cmp1))
2933 != (p_info->const_add > 0))))
2934 cmp1 = force_reg (mode, cmp1);
2937 /* See if we need to invert the result. */
2938 invert = (GET_CODE (cmp1) == CONST_INT
2939 ? p_info->invert_const : p_info->invert_reg);
2941 if (p_invert != (int *)0)
2943 *p_invert = invert;
2944 invert = 0;
2947 /* Comparison to constants, may involve adding 1 to change a LT into LE.
2948 Comparison between two registers, may involve switching operands. */
2949 if (GET_CODE (cmp1) == CONST_INT)
2951 if (p_info->const_add != 0)
2953 HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
2955 /* If modification of cmp1 caused overflow,
2956 we would get the wrong answer if we follow the usual path;
2957 thus, x > 0xffffffffU would turn into x > 0U. */
2958 if ((p_info->unsignedp
2959 ? (unsigned HOST_WIDE_INT) new >
2960 (unsigned HOST_WIDE_INT) INTVAL (cmp1)
2961 : new > INTVAL (cmp1))
2962 != (p_info->const_add > 0))
2964 /* This test is always true, but if INVERT is true then
2965 the result of the test needs to be inverted so 0 should
2966 be returned instead. */
2967 emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
2968 return result;
2970 else
2971 cmp1 = GEN_INT (new);
2975 else if (p_info->reverse_regs)
2977 rtx temp = cmp0;
2978 cmp0 = cmp1;
2979 cmp1 = temp;
2982 if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
2983 reg = cmp0;
2984 else
2986 reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
2987 convert_move (reg, gen_rtx (p_info->test_code, mode, cmp0, cmp1), 0);
2990 if (test == ITEST_NE)
2992 if (! TARGET_MIPS16)
2994 convert_move (result, gen_rtx (GTU, mode, reg, const0_rtx), 0);
2995 if (p_invert != NULL)
2996 *p_invert = 0;
2997 invert = 0;
2999 else
3001 reg2 = invert ? gen_reg_rtx (mode) : result;
3002 convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
3003 reg = reg2;
3007 else if (test == ITEST_EQ)
3009 reg2 = invert ? gen_reg_rtx (mode) : result;
3010 convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3011 reg = reg2;
3014 if (invert)
3016 rtx one;
3018 if (! TARGET_MIPS16)
3019 one = const1_rtx;
3020 else
3022 /* The value is in $24. Copy it to another register, so
3023 that reload doesn't think it needs to store the $24 and
3024 the input to the XOR in the same location. */
3025 reg2 = gen_reg_rtx (mode);
3026 emit_move_insn (reg2, reg);
3027 reg = reg2;
3028 one = force_reg (mode, const1_rtx);
3030 convert_move (result, gen_rtx (XOR, mode, reg, one), 0);
3033 return result;
3036 /* Emit the common code for doing conditional branches.
3037 operand[0] is the label to jump to.
3038 The comparison operands are saved away by cmp{si,di,sf,df}. */
3040 void
3041 gen_conditional_branch (operands, test_code)
3042 rtx operands[];
3043 enum rtx_code test_code;
3045 enum cmp_type type = branch_type;
3046 rtx cmp0 = branch_cmp[0];
3047 rtx cmp1 = branch_cmp[1];
3048 enum machine_mode mode;
3049 rtx reg;
3050 int invert;
3051 rtx label1, label2;
3053 switch (type)
3055 case CMP_SI:
3056 case CMP_DI:
3057 mode = type == CMP_SI ? SImode : DImode;
3058 invert = 0;
3059 reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3061 if (reg)
3063 cmp0 = reg;
3064 cmp1 = const0_rtx;
3065 test_code = NE;
3067 else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3068 /* We don't want to build a comparison against a non-zero
3069 constant. */
3070 cmp1 = force_reg (mode, cmp1);
3072 break;
3074 case CMP_SF:
3075 case CMP_DF:
3076 if (! ISA_HAS_8CC)
3077 reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3078 else
3079 reg = gen_reg_rtx (CCmode);
3081 /* For cmp0 != cmp1, build cmp0 == cmp1, and test for result ==
3082 0 in the instruction built below. The MIPS FPU handles
3083 inequality testing by testing for equality and looking for a
3084 false result. */
3085 emit_insn (gen_rtx_SET (VOIDmode, reg,
3086 gen_rtx (test_code == NE ? EQ : test_code,
3087 CCmode, cmp0, cmp1)));
3089 test_code = test_code == NE ? EQ : NE;
3090 mode = CCmode;
3091 cmp0 = reg;
3092 cmp1 = const0_rtx;
3093 invert = 0;
3094 break;
3096 default:
3097 abort_with_insn (gen_rtx (test_code, VOIDmode, cmp0, cmp1), "bad test");
3100 /* Generate the branch. */
3102 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3103 label2 = pc_rtx;
3105 if (invert)
3107 label2 = label1;
3108 label1 = pc_rtx;
3111 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
3112 gen_rtx_IF_THEN_ELSE (VOIDmode,
3113 gen_rtx (test_code, mode,
3114 cmp0, cmp1),
3115 label1, label2)));
3118 /* Emit the common code for conditional moves. OPERANDS is the array
3119 of operands passed to the conditional move defined_expand. */
3121 void
3122 gen_conditional_move (operands)
3123 rtx *operands;
3125 rtx op0 = branch_cmp[0];
3126 rtx op1 = branch_cmp[1];
3127 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3128 enum rtx_code cmp_code = GET_CODE (operands[1]);
3129 enum rtx_code move_code = NE;
3130 enum machine_mode op_mode = GET_MODE (operands[0]);
3131 enum machine_mode cmp_mode;
3132 rtx cmp_reg;
3134 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3136 switch (cmp_code)
3138 case EQ:
3139 cmp_code = XOR;
3140 move_code = EQ;
3141 break;
3142 case NE:
3143 cmp_code = XOR;
3144 break;
3145 case LT:
3146 break;
3147 case GE:
3148 cmp_code = LT;
3149 move_code = EQ;
3150 break;
3151 case GT:
3152 cmp_code = LT;
3153 op0 = force_reg (mode, branch_cmp[1]);
3154 op1 = branch_cmp[0];
3155 break;
3156 case LE:
3157 cmp_code = LT;
3158 op0 = force_reg (mode, branch_cmp[1]);
3159 op1 = branch_cmp[0];
3160 move_code = EQ;
3161 break;
3162 case LTU:
3163 break;
3164 case GEU:
3165 cmp_code = LTU;
3166 move_code = EQ;
3167 break;
3168 case GTU:
3169 cmp_code = LTU;
3170 op0 = force_reg (mode, branch_cmp[1]);
3171 op1 = branch_cmp[0];
3172 break;
3173 case LEU:
3174 cmp_code = LTU;
3175 op0 = force_reg (mode, branch_cmp[1]);
3176 op1 = branch_cmp[0];
3177 move_code = EQ;
3178 break;
3179 default:
3180 abort ();
3183 else if (cmp_code == NE)
3184 cmp_code = EQ, move_code = EQ;
3186 if (mode == SImode || mode == DImode)
3187 cmp_mode = mode;
3188 else if (mode == SFmode || mode == DFmode)
3189 cmp_mode = CCmode;
3190 else
3191 abort ();
3193 cmp_reg = gen_reg_rtx (cmp_mode);
3194 emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3195 gen_rtx (cmp_code, cmp_mode, op0, op1)));
3197 emit_insn (gen_rtx_SET (op_mode, operands[0],
3198 gen_rtx_IF_THEN_ELSE (op_mode,
3199 gen_rtx (move_code, VOIDmode,
3200 cmp_reg,
3201 CONST0_RTX (SImode)),
3202 operands[2], operands[3])));
3205 /* Emit the common code for conditional moves. OPERANDS is the array
3206 of operands passed to the conditional move defined_expand. */
3208 void
3209 mips_gen_conditional_trap (operands)
3210 rtx operands[];
3212 rtx op0, op1;
3213 enum rtx_code cmp_code = GET_CODE (operands[0]);
3214 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3216 /* MIPS conditional trap machine instructions don't have GT or LE
3217 flavors, so we must invert the comparison and convert to LT and
3218 GE, respectively. */
3219 switch (cmp_code)
3221 case GT: cmp_code = LT; break;
3222 case LE: cmp_code = GE; break;
3223 case GTU: cmp_code = LTU; break;
3224 case LEU: cmp_code = GEU; break;
3225 default: break;
3227 if (cmp_code == GET_CODE (operands[0]))
3229 op0 = force_reg (mode, branch_cmp[0]);
3230 op1 = branch_cmp[1];
3232 else
3234 op0 = force_reg (mode, branch_cmp[1]);
3235 op1 = branch_cmp[0];
3237 if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3238 op1 = force_reg (mode, op1);
3240 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3241 gen_rtx (cmp_code, GET_MODE (operands[0]), op0, op1),
3242 operands[1]));
3245 /* Write a loop to move a constant number of bytes.
3246 Generate load/stores as follows:
3248 do {
3249 temp1 = src[0];
3250 temp2 = src[1];
3252 temp<last> = src[MAX_MOVE_REGS-1];
3253 dest[0] = temp1;
3254 dest[1] = temp2;
3256 dest[MAX_MOVE_REGS-1] = temp<last>;
3257 src += MAX_MOVE_REGS;
3258 dest += MAX_MOVE_REGS;
3259 } while (src != final);
3261 This way, no NOP's are needed, and only MAX_MOVE_REGS+3 temp
3262 registers are needed.
3264 Aligned moves move MAX_MOVE_REGS*4 bytes every (2*MAX_MOVE_REGS)+3
3265 cycles, unaligned moves move MAX_MOVE_REGS*4 bytes every
3266 (4*MAX_MOVE_REGS)+3 cycles, assuming no cache misses. */
3268 #define MAX_MOVE_REGS 4
3269 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3271 static void
3272 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src)
3273 rtx dest_reg; /* register holding destination address */
3274 rtx src_reg; /* register holding source address */
3275 unsigned int bytes; /* # bytes to move */
3276 int align; /* alignment */
3277 rtx orig_dest; /* original dest */
3278 rtx orig_src; /* original source for making a reg note */
3280 rtx dest_mem = replace_equiv_address (orig_dest, dest_reg);
3281 rtx src_mem = replace_equiv_address (orig_src, src_reg);
3282 rtx align_rtx = GEN_INT (align);
3283 rtx label;
3284 rtx final_src;
3285 rtx bytes_rtx;
3286 int leftover;
3288 if (bytes < (unsigned)2 * MAX_MOVE_BYTES)
3289 abort ();
3291 leftover = bytes % MAX_MOVE_BYTES;
3292 bytes -= leftover;
3294 label = gen_label_rtx ();
3295 final_src = gen_reg_rtx (Pmode);
3296 bytes_rtx = GEN_INT (bytes);
3298 if (bytes > 0x7fff)
3300 if (Pmode == DImode)
3302 emit_insn (gen_movdi (final_src, bytes_rtx));
3303 emit_insn (gen_adddi3 (final_src, final_src, src_reg));
3305 else
3307 emit_insn (gen_movsi (final_src, bytes_rtx));
3308 emit_insn (gen_addsi3 (final_src, final_src, src_reg));
3311 else
3313 if (Pmode == DImode)
3314 emit_insn (gen_adddi3 (final_src, src_reg, bytes_rtx));
3315 else
3316 emit_insn (gen_addsi3 (final_src, src_reg, bytes_rtx));
3319 emit_label (label);
3321 bytes_rtx = GEN_INT (MAX_MOVE_BYTES);
3322 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, bytes_rtx, align_rtx));
3324 if (Pmode == DImode)
3326 emit_insn (gen_adddi3 (src_reg, src_reg, bytes_rtx));
3327 emit_insn (gen_adddi3 (dest_reg, dest_reg, bytes_rtx));
3328 emit_insn (gen_cmpdi (src_reg, final_src));
3330 else
3332 emit_insn (gen_addsi3 (src_reg, src_reg, bytes_rtx));
3333 emit_insn (gen_addsi3 (dest_reg, dest_reg, bytes_rtx));
3334 emit_insn (gen_cmpsi (src_reg, final_src));
3337 emit_jump_insn (gen_bne (label));
3339 if (leftover)
3340 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, GEN_INT (leftover),
3341 align_rtx));
3344 /* Use a library function to move some bytes. */
3346 static void
3347 block_move_call (dest_reg, src_reg, bytes_rtx)
3348 rtx dest_reg;
3349 rtx src_reg;
3350 rtx bytes_rtx;
3352 /* We want to pass the size as Pmode, which will normally be SImode
3353 but will be DImode if we are using 64 bit longs and pointers. */
3354 if (GET_MODE (bytes_rtx) != VOIDmode
3355 && GET_MODE (bytes_rtx) != (unsigned) Pmode)
3356 bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
3358 #ifdef TARGET_MEM_FUNCTIONS
3359 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memcpy"), 0,
3360 VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
3361 convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
3362 TREE_UNSIGNED (sizetype)),
3363 TYPE_MODE (sizetype));
3364 #else
3365 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "bcopy"), 0,
3366 VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
3367 convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
3368 TREE_UNSIGNED (integer_type_node)),
3369 TYPE_MODE (integer_type_node));
3370 #endif
3373 /* Expand string/block move operations.
3375 operands[0] is the pointer to the destination.
3376 operands[1] is the pointer to the source.
3377 operands[2] is the number of bytes to move.
3378 operands[3] is the alignment. */
3380 void
3381 expand_block_move (operands)
3382 rtx operands[];
3384 rtx bytes_rtx = operands[2];
3385 rtx align_rtx = operands[3];
3386 int constp = GET_CODE (bytes_rtx) == CONST_INT;
3387 unsigned HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
3388 unsigned int align = INTVAL (align_rtx);
3389 rtx orig_src = operands[1];
3390 rtx orig_dest = operands[0];
3391 rtx src_reg;
3392 rtx dest_reg;
3394 if (constp && bytes == 0)
3395 return;
3397 if (align > (unsigned) UNITS_PER_WORD)
3398 align = UNITS_PER_WORD;
3400 /* Move the address into scratch registers. */
3401 dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
3402 src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
3404 if (TARGET_MEMCPY)
3405 block_move_call (dest_reg, src_reg, bytes_rtx);
3407 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES
3408 && align == (unsigned) UNITS_PER_WORD)
3409 move_by_pieces (orig_dest, orig_src, bytes, align * BITS_PER_WORD);
3411 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES)
3412 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
3413 dest_reg),
3414 replace_equiv_address (orig_src,
3415 src_reg),
3416 bytes_rtx, align_rtx));
3418 else if (constp && align >= (unsigned) UNITS_PER_WORD && optimize)
3419 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src);
3421 else if (constp && optimize)
3423 /* If the alignment is not word aligned, generate a test at
3424 runtime, to see whether things wound up aligned, and we
3425 can use the faster lw/sw instead ulw/usw. */
3427 rtx temp = gen_reg_rtx (Pmode);
3428 rtx aligned_label = gen_label_rtx ();
3429 rtx join_label = gen_label_rtx ();
3430 int leftover = bytes % MAX_MOVE_BYTES;
3432 bytes -= leftover;
3434 if (Pmode == DImode)
3436 emit_insn (gen_iordi3 (temp, src_reg, dest_reg));
3437 emit_insn (gen_anddi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3438 emit_insn (gen_cmpdi (temp, const0_rtx));
3440 else
3442 emit_insn (gen_iorsi3 (temp, src_reg, dest_reg));
3443 emit_insn (gen_andsi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3444 emit_insn (gen_cmpsi (temp, const0_rtx));
3447 emit_jump_insn (gen_beq (aligned_label));
3449 /* Unaligned loop. */
3450 block_move_loop (dest_reg, src_reg, bytes, 1, orig_dest, orig_src);
3451 emit_jump_insn (gen_jump (join_label));
3452 emit_barrier ();
3454 /* Aligned loop. */
3455 emit_label (aligned_label);
3456 block_move_loop (dest_reg, src_reg, bytes, UNITS_PER_WORD, orig_dest,
3457 orig_src);
3458 emit_label (join_label);
3460 /* Bytes at the end of the loop. */
3461 if (leftover)
3462 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
3463 dest_reg),
3464 replace_equiv_address (orig_src,
3465 src_reg),
3466 GEN_INT (leftover),
3467 GEN_INT (align)));
3470 else
3471 block_move_call (dest_reg, src_reg, bytes_rtx);
3474 /* Emit load/stores for a small constant block_move.
3476 operands[0] is the memory address of the destination.
3477 operands[1] is the memory address of the source.
3478 operands[2] is the number of bytes to move.
3479 operands[3] is the alignment.
3480 operands[4] is a temp register.
3481 operands[5] is a temp register.
3483 operands[3+num_regs] is the last temp register.
3485 The block move type can be one of the following:
3486 BLOCK_MOVE_NORMAL Do all of the block move.
3487 BLOCK_MOVE_NOT_LAST Do all but the last store.
3488 BLOCK_MOVE_LAST Do just the last store. */
3490 const char *
3491 output_block_move (insn, operands, num_regs, move_type)
3492 rtx insn;
3493 rtx operands[];
3494 int num_regs;
3495 enum block_move_type move_type;
3497 rtx dest_reg = XEXP (operands[0], 0);
3498 rtx src_reg = XEXP (operands[1], 0);
3499 HOST_WIDE_INT bytes = INTVAL (operands[2]);
3500 int align = INTVAL (operands[3]);
3501 int num = 0;
3502 int offset = 0;
3503 int use_lwl_lwr = 0;
3504 int last_operand = num_regs + 4;
3505 int safe_regs = 4;
3506 int i;
3507 rtx xoperands[10];
3509 struct {
3510 const char *load; /* load insn without nop */
3511 const char *load_nop; /* load insn with trailing nop */
3512 const char *store; /* store insn */
3513 const char *final; /* if last_store used: NULL or swr */
3514 const char *last_store; /* last store instruction */
3515 int offset; /* current offset */
3516 enum machine_mode mode; /* mode to use on (MEM) */
3517 } load_store[4];
3519 /* ??? Detect a bug in GCC, where it can give us a register
3520 the same as one of the addressing registers and reduce
3521 the number of registers available. */
3522 for (i = 4; i < last_operand && safe_regs < (int) ARRAY_SIZE (xoperands); i++)
3523 if (! reg_mentioned_p (operands[i], operands[0])
3524 && ! reg_mentioned_p (operands[i], operands[1]))
3525 xoperands[safe_regs++] = operands[i];
3527 if (safe_regs < last_operand)
3529 xoperands[0] = operands[0];
3530 xoperands[1] = operands[1];
3531 xoperands[2] = operands[2];
3532 xoperands[3] = operands[3];
3533 return output_block_move (insn, xoperands, safe_regs - 4, move_type);
3536 /* If we are given global or static addresses, and we would be
3537 emitting a few instructions, try to save time by using a
3538 temporary register for the pointer. */
3539 /* ??? The SGI Irix6 assembler fails when a SYMBOL_REF is used in
3540 an ldl/ldr instruction pair. We play it safe, and always move
3541 constant addresses into registers when generating N32/N64 code, just
3542 in case we might emit an unaligned load instruction. */
3543 if (num_regs > 2 && (bytes > 2 * align || move_type != BLOCK_MOVE_NORMAL
3544 || mips_abi == ABI_MEABI
3545 || mips_abi == ABI_N32
3546 || mips_abi == ABI_64))
3548 if (CONSTANT_P (src_reg))
3550 if (TARGET_STATS)
3551 mips_count_memory_refs (operands[1], 1);
3553 src_reg = operands[3 + num_regs--];
3554 if (move_type != BLOCK_MOVE_LAST)
3556 xoperands[1] = operands[1];
3557 xoperands[0] = src_reg;
3558 if (Pmode == DImode)
3559 output_asm_insn ("dla\t%0,%1", xoperands);
3560 else
3561 output_asm_insn ("la\t%0,%1", xoperands);
3565 if (CONSTANT_P (dest_reg))
3567 if (TARGET_STATS)
3568 mips_count_memory_refs (operands[0], 1);
3570 dest_reg = operands[3 + num_regs--];
3571 if (move_type != BLOCK_MOVE_LAST)
3573 xoperands[1] = operands[0];
3574 xoperands[0] = dest_reg;
3575 if (Pmode == DImode)
3576 output_asm_insn ("dla\t%0,%1", xoperands);
3577 else
3578 output_asm_insn ("la\t%0,%1", xoperands);
3583 /* ??? We really shouldn't get any LO_SUM addresses here, because they
3584 are not offsettable, however, offsettable_address_p says they are
3585 offsettable. I think this is a bug in offsettable_address_p.
3586 For expediency, we fix this by just loading the address into a register
3587 if we happen to get one. */
3589 if (GET_CODE (src_reg) == LO_SUM)
3591 src_reg = operands[3 + num_regs--];
3592 if (move_type != BLOCK_MOVE_LAST)
3594 xoperands[2] = XEXP (XEXP (operands[1], 0), 1);
3595 xoperands[1] = XEXP (XEXP (operands[1], 0), 0);
3596 xoperands[0] = src_reg;
3597 if (Pmode == DImode)
3598 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3599 else
3600 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3604 if (GET_CODE (dest_reg) == LO_SUM)
3606 dest_reg = operands[3 + num_regs--];
3607 if (move_type != BLOCK_MOVE_LAST)
3609 xoperands[2] = XEXP (XEXP (operands[0], 0), 1);
3610 xoperands[1] = XEXP (XEXP (operands[0], 0), 0);
3611 xoperands[0] = dest_reg;
3612 if (Pmode == DImode)
3613 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3614 else
3615 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3619 if (num_regs > (int) ARRAY_SIZE (load_store))
3620 num_regs = ARRAY_SIZE (load_store);
3622 else if (num_regs < 1)
3623 abort_with_insn (insn,
3624 "cannot do block move, not enough scratch registers");
3626 while (bytes > 0)
3628 load_store[num].offset = offset;
3630 if (TARGET_64BIT && bytes >= 8 && align >= 8)
3632 load_store[num].load = "ld\t%0,%1";
3633 load_store[num].load_nop = "ld\t%0,%1%#";
3634 load_store[num].store = "sd\t%0,%1";
3635 load_store[num].last_store = "sd\t%0,%1";
3636 load_store[num].final = 0;
3637 load_store[num].mode = DImode;
3638 offset += 8;
3639 bytes -= 8;
3642 /* ??? Fails because of a MIPS assembler bug? */
3643 else if (TARGET_64BIT && bytes >= 8 && ! TARGET_MIPS16)
3645 if (BYTES_BIG_ENDIAN)
3647 load_store[num].load = "ldl\t%0,%1\n\tldr\t%0,%2";
3648 load_store[num].load_nop = "ldl\t%0,%1\n\tldr\t%0,%2%#";
3649 load_store[num].store = "sdl\t%0,%1\n\tsdr\t%0,%2";
3650 load_store[num].last_store = "sdr\t%0,%2";
3651 load_store[num].final = "sdl\t%0,%1";
3653 else
3655 load_store[num].load = "ldl\t%0,%2\n\tldr\t%0,%1";
3656 load_store[num].load_nop = "ldl\t%0,%2\n\tldr\t%0,%1%#";
3657 load_store[num].store = "sdl\t%0,%2\n\tsdr\t%0,%1";
3658 load_store[num].last_store = "sdr\t%0,%1";
3659 load_store[num].final = "sdl\t%0,%2";
3662 load_store[num].mode = DImode;
3663 offset += 8;
3664 bytes -= 8;
3665 use_lwl_lwr = 1;
3668 else if (bytes >= 4 && align >= 4)
3670 load_store[num].load = "lw\t%0,%1";
3671 load_store[num].load_nop = "lw\t%0,%1%#";
3672 load_store[num].store = "sw\t%0,%1";
3673 load_store[num].last_store = "sw\t%0,%1";
3674 load_store[num].final = 0;
3675 load_store[num].mode = SImode;
3676 offset += 4;
3677 bytes -= 4;
3680 else if (bytes >= 4 && ! TARGET_MIPS16)
3682 if (BYTES_BIG_ENDIAN)
3684 load_store[num].load = "lwl\t%0,%1\n\tlwr\t%0,%2";
3685 load_store[num].load_nop = "lwl\t%0,%1\n\tlwr\t%0,%2%#";
3686 load_store[num].store = "swl\t%0,%1\n\tswr\t%0,%2";
3687 load_store[num].last_store = "swr\t%0,%2";
3688 load_store[num].final = "swl\t%0,%1";
3690 else
3692 load_store[num].load = "lwl\t%0,%2\n\tlwr\t%0,%1";
3693 load_store[num].load_nop = "lwl\t%0,%2\n\tlwr\t%0,%1%#";
3694 load_store[num].store = "swl\t%0,%2\n\tswr\t%0,%1";
3695 load_store[num].last_store = "swr\t%0,%1";
3696 load_store[num].final = "swl\t%0,%2";
3699 load_store[num].mode = SImode;
3700 offset += 4;
3701 bytes -= 4;
3702 use_lwl_lwr = 1;
3705 else if (bytes >= 2 && align >= 2)
3707 load_store[num].load = "lh\t%0,%1";
3708 load_store[num].load_nop = "lh\t%0,%1%#";
3709 load_store[num].store = "sh\t%0,%1";
3710 load_store[num].last_store = "sh\t%0,%1";
3711 load_store[num].final = 0;
3712 load_store[num].mode = HImode;
3713 offset += 2;
3714 bytes -= 2;
3716 else
3718 load_store[num].load = "lb\t%0,%1";
3719 load_store[num].load_nop = "lb\t%0,%1%#";
3720 load_store[num].store = "sb\t%0,%1";
3721 load_store[num].last_store = "sb\t%0,%1";
3722 load_store[num].final = 0;
3723 load_store[num].mode = QImode;
3724 offset++;
3725 bytes--;
3728 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3730 dslots_load_total++;
3731 dslots_load_filled++;
3733 if (CONSTANT_P (src_reg))
3734 mips_count_memory_refs (src_reg, 1);
3736 if (CONSTANT_P (dest_reg))
3737 mips_count_memory_refs (dest_reg, 1);
3740 /* Emit load/stores now if we have run out of registers or are
3741 at the end of the move. */
3743 if (++num == num_regs || bytes == 0)
3745 /* If only load/store, we need a NOP after the load. */
3746 if (num == 1)
3748 load_store[0].load = load_store[0].load_nop;
3749 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3750 dslots_load_filled--;
3753 if (move_type != BLOCK_MOVE_LAST)
3755 for (i = 0; i < num; i++)
3757 int offset;
3759 if (!operands[i + 4])
3760 abort ();
3762 if (GET_MODE (operands[i + 4]) != load_store[i].mode)
3763 operands[i + 4] = gen_rtx_REG (load_store[i].mode,
3764 REGNO (operands[i + 4]));
3766 offset = load_store[i].offset;
3767 xoperands[0] = operands[i + 4];
3768 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
3769 plus_constant (src_reg, offset));
3771 if (use_lwl_lwr)
3773 int extra_offset
3774 = GET_MODE_SIZE (load_store[i].mode) - 1;
3776 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
3777 plus_constant (src_reg,
3778 extra_offset
3779 + offset));
3782 output_asm_insn (load_store[i].load, xoperands);
3786 for (i = 0; i < num; i++)
3788 int last_p = (i == num-1 && bytes == 0);
3789 int offset = load_store[i].offset;
3791 xoperands[0] = operands[i + 4];
3792 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
3793 plus_constant (dest_reg, offset));
3796 if (use_lwl_lwr)
3798 int extra_offset = GET_MODE_SIZE (load_store[i].mode) - 1;
3799 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
3800 plus_constant (dest_reg,
3801 extra_offset
3802 + offset));
3805 if (move_type == BLOCK_MOVE_NORMAL)
3806 output_asm_insn (load_store[i].store, xoperands);
3808 else if (move_type == BLOCK_MOVE_NOT_LAST)
3810 if (!last_p)
3811 output_asm_insn (load_store[i].store, xoperands);
3813 else if (load_store[i].final != 0)
3814 output_asm_insn (load_store[i].final, xoperands);
3817 else if (last_p)
3818 output_asm_insn (load_store[i].last_store, xoperands);
3821 num = 0; /* reset load_store */
3822 use_lwl_lwr = 0;
3826 return "";
3829 /* Argument support functions. */
3831 /* Initialize CUMULATIVE_ARGS for a function. */
3833 void
3834 init_cumulative_args (cum, fntype, libname)
3835 CUMULATIVE_ARGS *cum; /* argument info to initialize */
3836 tree fntype; /* tree ptr for function decl */
3837 rtx libname ATTRIBUTE_UNUSED; /* SYMBOL_REF of library name or 0 */
3839 static CUMULATIVE_ARGS zero_cum;
3840 tree param, next_param;
3842 if (TARGET_DEBUG_E_MODE)
3844 fprintf (stderr,
3845 "\ninit_cumulative_args, fntype = 0x%.8lx", (long)fntype);
3847 if (!fntype)
3848 fputc ('\n', stderr);
3850 else
3852 tree ret_type = TREE_TYPE (fntype);
3853 fprintf (stderr, ", fntype code = %s, ret code = %s\n",
3854 tree_code_name[(int)TREE_CODE (fntype)],
3855 tree_code_name[(int)TREE_CODE (ret_type)]);
3859 *cum = zero_cum;
3860 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3862 /* Determine if this function has variable arguments. This is
3863 indicated by the last argument being 'void_type_mode' if there
3864 are no variable arguments. The standard MIPS calling sequence
3865 passes all arguments in the general purpose registers in this case. */
3867 for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3868 param != 0; param = next_param)
3870 next_param = TREE_CHAIN (param);
3871 if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3872 cum->gp_reg_found = 1;
3876 /* Advance the argument to the next argument position. */
3878 void
3879 function_arg_advance (cum, mode, type, named)
3880 CUMULATIVE_ARGS *cum; /* current arg information */
3881 enum machine_mode mode; /* current arg mode */
3882 tree type; /* type of the argument or 0 if lib support */
3883 int named; /* whether or not the argument was named */
3885 if (TARGET_DEBUG_E_MODE)
3887 fprintf (stderr,
3888 "function_adv({gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3889 cum->gp_reg_found, cum->arg_number, cum->arg_words,
3890 GET_MODE_NAME (mode));
3891 fprintf (stderr, HOST_PTR_PRINTF, (const PTR) type);
3892 fprintf (stderr, ", %d )\n\n", named);
3895 cum->arg_number++;
3896 switch (mode)
3898 case VOIDmode:
3899 break;
3901 default:
3902 if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
3903 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3904 abort ();
3906 cum->gp_reg_found = 1;
3907 cum->arg_words += ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
3908 / UNITS_PER_WORD);
3909 break;
3911 case BLKmode:
3912 cum->gp_reg_found = 1;
3913 cum->arg_words += ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
3914 / UNITS_PER_WORD);
3915 break;
3917 case SFmode:
3918 if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3919 cum->fp_arg_words++;
3920 else
3921 cum->arg_words++;
3922 if (! cum->gp_reg_found && cum->arg_number <= 2)
3923 cum->fp_code += 1 << ((cum->arg_number - 1) * 2);
3924 break;
3926 case DFmode:
3927 if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
3928 cum->fp_arg_words += (TARGET_64BIT ? 1 : 2);
3929 else
3930 cum->arg_words += (TARGET_64BIT ? 1 : 2);
3931 if (! cum->gp_reg_found && ! TARGET_SINGLE_FLOAT && cum->arg_number <= 2)
3932 cum->fp_code += 2 << ((cum->arg_number - 1) * 2);
3933 break;
3935 case DImode:
3936 cum->gp_reg_found = 1;
3937 cum->arg_words += (TARGET_64BIT ? 1 : 2);
3938 break;
3940 case QImode:
3941 case HImode:
3942 case SImode:
3943 cum->gp_reg_found = 1;
3944 cum->arg_words++;
3945 break;
3949 /* Return an RTL expression containing the register for the given mode,
3950 or 0 if the argument is to be passed on the stack. */
3952 struct rtx_def *
3953 function_arg (cum, mode, type, named)
3954 CUMULATIVE_ARGS *cum; /* current arg information */
3955 enum machine_mode mode; /* current arg mode */
3956 tree type; /* type of the argument or 0 if lib support */
3957 int named; /* != 0 for normal args, == 0 for ... args */
3959 rtx ret;
3960 int regbase = -1;
3961 int bias = 0;
3962 unsigned int *arg_words = &cum->arg_words;
3963 int struct_p = (type != 0
3964 && (TREE_CODE (type) == RECORD_TYPE
3965 || TREE_CODE (type) == UNION_TYPE
3966 || TREE_CODE (type) == QUAL_UNION_TYPE));
3968 if (TARGET_DEBUG_E_MODE)
3970 fprintf (stderr,
3971 "function_arg( {gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3972 cum->gp_reg_found, cum->arg_number, cum->arg_words,
3973 GET_MODE_NAME (mode));
3974 fprintf (stderr, HOST_PTR_PRINTF, (const PTR) type);
3975 fprintf (stderr, ", %d ) = ", named);
3979 cum->last_arg_fp = 0;
3980 switch (mode)
3982 case SFmode:
3983 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
3985 if (cum->gp_reg_found || cum->arg_number >= 2 || TARGET_SOFT_FLOAT)
3986 regbase = GP_ARG_FIRST;
3987 else
3989 regbase = FP_ARG_FIRST;
3991 /* If the first arg was a float in a floating point register,
3992 then set bias to align this float arg properly. */
3993 if (cum->arg_words == 1)
3994 bias = 1;
3997 else if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3999 if (! TARGET_64BIT)
4000 cum->fp_arg_words += cum->fp_arg_words & 1;
4001 cum->last_arg_fp = 1;
4002 arg_words = &cum->fp_arg_words;
4003 regbase = FP_ARG_FIRST;
4005 /* The MIPS eabi says only structures containing doubles get passed in a
4006 fp register, so force a structure containing a float to be passed in
4007 the integer registers. */
4008 else if (mips_abi == ABI_MEABI && struct_p)
4009 regbase = GP_ARG_FIRST;
4010 else
4011 regbase = (TARGET_SOFT_FLOAT || ! named ? GP_ARG_FIRST : FP_ARG_FIRST);
4012 break;
4014 case DFmode:
4015 if (! TARGET_64BIT)
4017 if (mips_abi == ABI_EABI
4018 && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
4019 cum->fp_arg_words += cum->fp_arg_words & 1;
4020 else
4021 cum->arg_words += cum->arg_words & 1;
4024 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
4025 regbase = ((cum->gp_reg_found
4026 || TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT
4027 || cum->arg_number >= 2)
4028 ? GP_ARG_FIRST : FP_ARG_FIRST);
4029 else if (mips_abi == ABI_EABI
4030 && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
4032 cum->last_arg_fp = 1;
4033 arg_words = &cum->fp_arg_words;
4034 regbase = FP_ARG_FIRST;
4036 else
4037 regbase = (TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT || ! named
4038 ? GP_ARG_FIRST : FP_ARG_FIRST);
4039 break;
4041 default:
4042 if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
4043 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4044 abort ();
4046 /* Drops through. */
4047 case BLKmode:
4048 if (type != NULL_TREE && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD
4049 && ! TARGET_64BIT && mips_abi != ABI_EABI)
4050 cum->arg_words += (cum->arg_words & 1);
4051 regbase = GP_ARG_FIRST;
4052 break;
4054 case VOIDmode:
4055 case QImode:
4056 case HImode:
4057 case SImode:
4058 regbase = GP_ARG_FIRST;
4059 break;
4061 case DImode:
4062 if (! TARGET_64BIT)
4063 cum->arg_words += (cum->arg_words & 1);
4064 regbase = GP_ARG_FIRST;
4067 if (*arg_words >= (unsigned) MAX_ARGS_IN_REGISTERS)
4069 if (TARGET_DEBUG_E_MODE)
4070 fprintf (stderr, "<stack>%s\n", struct_p ? ", [struct]" : "");
4072 ret = 0;
4074 else
4076 if (regbase == -1)
4077 abort ();
4079 if (! type || TREE_CODE (type) != RECORD_TYPE
4080 || mips_abi == ABI_32 || mips_abi == ABI_EABI
4081 || mips_abi == ABI_O64 || mips_abi == ABI_MEABI
4082 || ! named
4083 || ! TYPE_SIZE_UNIT (type)
4084 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
4087 unsigned int arg_reg = (regbase + *arg_words + bias);
4088 ret = gen_rtx_REG (mode, arg_reg);
4089 if (mips_abi == ABI_MEABI
4090 && regbase == FP_ARG_FIRST
4091 && ! cum->prototype)
4093 /* To make K&R varargs work we need to pass floating
4094 point arguments in both integer and FP registers. */
4095 ret = gen_rtx_PARALLEL (mode,
4096 gen_rtvec (2,
4097 gen_rtx_EXPR_LIST (VOIDmode,
4098 gen_rtx_REG (mode,
4099 arg_reg + GP_ARG_FIRST - FP_ARG_FIRST),
4100 const0_rtx), gen_rtx_EXPR_LIST (VOIDmode, ret, const0_rtx)));
4103 else
4105 /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
4106 structure contains a double in its entirety, then that 64 bit
4107 chunk is passed in a floating point register. */
4108 tree field;
4110 /* First check to see if there is any such field. */
4111 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4112 if (TREE_CODE (field) == FIELD_DECL
4113 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4114 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4115 && host_integerp (bit_position (field), 0)
4116 && int_bit_position (field) % BITS_PER_WORD == 0)
4117 break;
4119 /* If the whole struct fits a DFmode register,
4120 we don't need the PARALLEL. */
4121 if (! field || mode == DFmode)
4122 ret = gen_rtx_REG (mode, regbase + *arg_words + bias);
4123 else
4125 /* Now handle the special case by returning a PARALLEL
4126 indicating where each 64 bit chunk goes. */
4127 unsigned int chunks;
4128 HOST_WIDE_INT bitpos;
4129 unsigned int regno;
4130 unsigned int i;
4132 /* ??? If this is a packed structure, then the last hunk won't
4133 be 64 bits. */
4135 chunks
4136 = tree_low_cst (TYPE_SIZE_UNIT (type), 1) / UNITS_PER_WORD;
4137 if (chunks + *arg_words + bias > (unsigned) MAX_ARGS_IN_REGISTERS)
4138 chunks = MAX_ARGS_IN_REGISTERS - *arg_words - bias;
4140 /* assign_parms checks the mode of ENTRY_PARM, so we must
4141 use the actual mode here. */
4142 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (chunks));
4144 bitpos = 0;
4145 regno = regbase + *arg_words + bias;
4146 field = TYPE_FIELDS (type);
4147 for (i = 0; i < chunks; i++)
4149 rtx reg;
4151 for (; field; field = TREE_CHAIN (field))
4152 if (TREE_CODE (field) == FIELD_DECL
4153 && int_bit_position (field) >= bitpos)
4154 break;
4156 if (field
4157 && int_bit_position (field) == bitpos
4158 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4159 && !TARGET_SOFT_FLOAT
4160 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4161 reg = gen_rtx_REG (DFmode,
4162 regno + FP_ARG_FIRST - GP_ARG_FIRST);
4163 else
4164 reg = gen_rtx_REG (word_mode, regno);
4166 XVECEXP (ret, 0, i)
4167 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4168 GEN_INT (bitpos / BITS_PER_UNIT));
4170 bitpos += 64;
4171 regno++;
4176 if (TARGET_DEBUG_E_MODE)
4177 fprintf (stderr, "%s%s\n", reg_names[regbase + *arg_words + bias],
4178 struct_p ? ", [struct]" : "");
4180 /* The following is a hack in order to pass 1 byte structures
4181 the same way that the MIPS compiler does (namely by passing
4182 the structure in the high byte or half word of the register).
4183 This also makes varargs work. If we have such a structure,
4184 we save the adjustment RTL, and the call define expands will
4185 emit them. For the VOIDmode argument (argument after the
4186 last real argument), pass back a parallel vector holding each
4187 of the adjustments. */
4189 /* ??? function_arg can be called more than once for each argument.
4190 As a result, we compute more adjustments than we need here.
4191 See the CUMULATIVE_ARGS definition in mips.h. */
4193 /* ??? This scheme requires everything smaller than the word size to
4194 shifted to the left, but when TARGET_64BIT and ! TARGET_INT64,
4195 that would mean every int needs to be shifted left, which is very
4196 inefficient. Let's not carry this compatibility to the 64 bit
4197 calling convention for now. */
4199 if (struct_p && int_size_in_bytes (type) < UNITS_PER_WORD
4200 && ! TARGET_64BIT
4201 && mips_abi != ABI_EABI
4202 && mips_abi != ABI_MEABI)
4204 rtx amount = GEN_INT (BITS_PER_WORD
4205 - int_size_in_bytes (type) * BITS_PER_UNIT);
4206 rtx reg = gen_rtx_REG (word_mode, regbase + *arg_words + bias);
4208 if (TARGET_64BIT)
4209 cum->adjust[cum->num_adjusts++] = gen_ashldi3 (reg, reg, amount);
4210 else
4211 cum->adjust[cum->num_adjusts++] = gen_ashlsi3 (reg, reg, amount);
4215 /* We will be called with a mode of VOIDmode after the last argument
4216 has been seen. Whatever we return will be passed to the call
4217 insn. If we need any shifts for small structures, return them in
4218 a PARALLEL; in that case, stuff the mips16 fp_code in as the
4219 mode. Otherwise, if we have need a mips16 fp_code, return a REG
4220 with the code stored as the mode. */
4221 if (mode == VOIDmode)
4223 if (cum->num_adjusts > 0)
4224 ret = gen_rtx (PARALLEL, (enum machine_mode) cum->fp_code,
4225 gen_rtvec_v (cum->num_adjusts, cum->adjust));
4226 else if (TARGET_MIPS16 && cum->fp_code != 0)
4227 ret = gen_rtx (REG, (enum machine_mode) cum->fp_code, 0);
4230 return ret;
4234 function_arg_partial_nregs (cum, mode, type, named)
4235 CUMULATIVE_ARGS *cum; /* current arg information */
4236 enum machine_mode mode; /* current arg mode */
4237 tree type; /* type of the argument or 0 if lib support */
4238 int named ATTRIBUTE_UNUSED;/* != 0 for normal args, == 0 for ... args */
4240 if ((mode == BLKmode
4241 || GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
4242 || GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4243 && cum->arg_words < (unsigned) MAX_ARGS_IN_REGISTERS
4244 && mips_abi != ABI_EABI)
4246 int words;
4247 if (mode == BLKmode)
4248 words = ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
4249 / UNITS_PER_WORD);
4250 else
4251 words = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4253 if (words + cum->arg_words <= (unsigned) MAX_ARGS_IN_REGISTERS)
4254 return 0; /* structure fits in registers */
4256 if (TARGET_DEBUG_E_MODE)
4257 fprintf (stderr, "function_arg_partial_nregs = %d\n",
4258 MAX_ARGS_IN_REGISTERS - cum->arg_words);
4260 return MAX_ARGS_IN_REGISTERS - cum->arg_words;
4263 else if (mode == DImode
4264 && cum->arg_words == MAX_ARGS_IN_REGISTERS - (unsigned)1
4265 && ! TARGET_64BIT && mips_abi != ABI_EABI)
4267 if (TARGET_DEBUG_E_MODE)
4268 fprintf (stderr, "function_arg_partial_nregs = 1\n");
4270 return 1;
4273 return 0;
4276 /* Create the va_list data type.
4277 We keep 3 pointers, and two offsets.
4278 Two pointers are to the overflow area, which starts at the CFA.
4279 One of these is constant, for addressing into the GPR save area below it.
4280 The other is advanced up the stack through the overflow region.
4281 The third pointer is to the GPR save area. Since the FPR save area
4282 is just below it, we can address FPR slots off this pointer.
4283 We also keep two one-byte offsets, which are to be subtracted from the
4284 constant pointers to yield addresses in the GPR and FPR save areas.
4285 These are downcounted as float or non-float arguments are used,
4286 and when they get to zero, the argument must be obtained from the
4287 overflow region.
4288 If TARGET_SOFT_FLOAT or TARGET_SINGLE_FLOAT, then no FPR save area exists,
4289 and a single pointer is enough. It's started at the GPR save area,
4290 and is advanced, period.
4291 Note that the GPR save area is not constant size, due to optimization
4292 in the prologue. Hence, we can't use a design with two pointers
4293 and two offsets, although we could have designed this with two pointers
4294 and three offsets. */
4297 tree
4298 mips_build_va_list ()
4300 if (mips_abi == ABI_EABI && !TARGET_SOFT_FLOAT && !TARGET_SINGLE_FLOAT)
4302 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, record;
4304 record = make_node (RECORD_TYPE);
4306 f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
4307 ptr_type_node);
4308 f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
4309 ptr_type_node);
4310 f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
4311 ptr_type_node);
4312 f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
4313 unsigned_char_type_node);
4314 f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
4315 unsigned_char_type_node);
4318 DECL_FIELD_CONTEXT (f_ovfl) = record;
4319 DECL_FIELD_CONTEXT (f_gtop) = record;
4320 DECL_FIELD_CONTEXT (f_ftop) = record;
4321 DECL_FIELD_CONTEXT (f_goff) = record;
4322 DECL_FIELD_CONTEXT (f_foff) = record;
4324 TYPE_FIELDS (record) = f_ovfl;
4325 TREE_CHAIN (f_ovfl) = f_gtop;
4326 TREE_CHAIN (f_gtop) = f_ftop;
4327 TREE_CHAIN (f_ftop) = f_goff;
4328 TREE_CHAIN (f_goff) = f_foff;
4330 layout_type (record);
4331 return record;
4333 else
4334 return ptr_type_node;
4337 /* Implement va_start. stdarg_p is 0 if implementing
4338 __builtin_varargs_va_start, 1 if implementing __builtin_stdarg_va_start.
4339 Note that this routine isn't called when compiling e.g. "_vfprintf_r".
4340 (It doesn't have "...", so it inherits the pointers of its caller.) */
4342 void
4343 mips_va_start (stdarg_p, valist, nextarg)
4344 int stdarg_p;
4345 tree valist;
4346 rtx nextarg;
4348 int int_arg_words;
4349 tree t;
4351 /* Find out how many non-float named formals */
4352 int_arg_words = current_function_args_info.arg_words;
4354 if (mips_abi == ABI_EABI)
4356 int gpr_save_area_size;
4357 /* Note UNITS_PER_WORD is 4 bytes or 8, depending on TARGET_64BIT. */
4358 if (int_arg_words < 8 )
4359 /* Adjust for the prologue's economy measure */
4360 gpr_save_area_size = (8 - int_arg_words) * UNITS_PER_WORD;
4361 else
4362 gpr_save_area_size = 0;
4364 if (!TARGET_SOFT_FLOAT && !TARGET_SINGLE_FLOAT)
4366 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4367 tree ovfl, gtop, ftop, goff, foff;
4368 tree gprv;
4369 int float_formals, fpr_offset, size_excess, floats_passed_in_regs;
4370 int fpr_save_offset;
4372 float_formals = current_function_args_info.fp_arg_words;
4373 /* If mips2, the number of formals is half the reported # of words */
4374 if (!TARGET_64BIT)
4375 float_formals /= 2;
4376 floats_passed_in_regs = (TARGET_64BIT ? 8 : 4);
4378 f_ovfl = TYPE_FIELDS (va_list_type_node);
4379 f_gtop = TREE_CHAIN (f_ovfl);
4380 f_ftop = TREE_CHAIN (f_gtop);
4381 f_goff = TREE_CHAIN (f_ftop);
4382 f_foff = TREE_CHAIN (f_goff);
4384 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4385 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4386 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4387 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4388 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4390 /* Emit code setting a pointer into the overflow (shared-stack) area.
4391 If there were more than 8 non-float formals, or more than 8
4392 float formals, then this pointer isn't to the base of the area.
4393 In that case, it must point to where the first vararg is. */
4394 size_excess = 0;
4395 if (float_formals > floats_passed_in_regs)
4396 size_excess += (float_formals-floats_passed_in_regs) * 8;
4397 if (int_arg_words > 8)
4398 size_excess += (int_arg_words-8) * UNITS_PER_WORD;
4400 /* FIXME: for mips2, the above size_excess can be wrong. Because the
4401 overflow stack holds mixed size items, there can be alignments,
4402 so that an 8 byte double following a 4 byte int will be on an
4403 8 byte boundary. This means that the above calculation should
4404 take into account the exact sequence of floats and non-floats
4405 which make up the excess. That calculation should be rolled
4406 into the code which sets the current_function_args_info struct.
4407 The above then reduces to a fetch from that struct. */
4410 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
4411 if (size_excess)
4412 t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
4413 build_int_2 (size_excess, 0));
4414 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4415 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4417 /* Emit code setting a ptr to the base of the overflow area. */
4418 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
4419 t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
4420 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4422 /* Emit code setting a pointer to the GPR save area.
4423 More precisely, a pointer to off-the-end of the FPR save area.
4424 If mips4, this is gpr_save_area_size below the overflow area.
4425 If mips2, also round down to an 8-byte boundary, since the FPR
4426 save area is 8-byte aligned, and GPR is 4-byte-aligned.
4427 Therefore there can be a 4-byte gap between the save areas. */
4428 gprv = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
4429 fpr_save_offset = gpr_save_area_size;
4430 if (!TARGET_64BIT)
4432 if (fpr_save_offset & 7)
4433 fpr_save_offset += 4;
4435 if (fpr_save_offset)
4436 gprv = build (PLUS_EXPR, TREE_TYPE (ftop), gprv,
4437 build_int_2 (-fpr_save_offset,-1));
4438 t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, gprv);
4439 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4441 /* Emit code initting an offset to the size of the GPR save area */
4442 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
4443 build_int_2 (gpr_save_area_size,0));
4444 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4446 /* Emit code initting an offset from ftop to the first float
4447 vararg. This varies in size, since any float
4448 varargs are put in the FPR save area after the formals.
4449 Note it's 8 bytes/formal regardless of TARGET_64BIT.
4450 However, mips2 stores 4 GPRs, mips4 stores 8 GPRs.
4451 If there are 8 or more float formals, init to zero.
4452 (In fact, the formals aren't stored in the bottom of the
4453 FPR save area: they are elsewhere, and the size of the FPR
4454 save area is economized by the prologue. But this code doesn't
4455 care. This design is unaffected by that fact.) */
4456 if (float_formals >= floats_passed_in_regs)
4457 fpr_offset = 0;
4458 else
4459 fpr_offset = (floats_passed_in_regs - float_formals) * 8;
4460 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
4461 build_int_2 (fpr_offset,0));
4462 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4464 else
4466 /* TARGET_SOFT_FLOAT or TARGET_SINGLE_FLOAT */
4468 /* Everything is in the GPR save area, or in the overflow
4469 area which is contiguous with it. */
4471 int offset = -gpr_save_area_size;
4472 if (gpr_save_area_size == 0)
4473 offset = (stdarg_p ? 0 : -UNITS_PER_WORD);
4474 nextarg = plus_constant (nextarg, offset);
4475 std_expand_builtin_va_start (1, valist, nextarg);
4478 else
4480 /* not EABI */
4481 int ofs;
4483 if (stdarg_p)
4484 ofs = 0;
4485 else
4487 /* ??? This had been conditional on
4488 _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32
4489 and both iris5.h and iris6.h define _MIPS_SIM. */
4490 if (mips_abi == ABI_N32 || mips_abi == ABI_64)
4491 ofs = (int_arg_words >= 8 ? -UNITS_PER_WORD : 0);
4492 else if (mips_abi == ABI_MEABI)
4493 ofs = (int_arg_words >= 8 ? -UNITS_PER_WORD : 0);
4494 else
4495 ofs = -UNITS_PER_WORD;
4498 nextarg = plus_constant (nextarg, ofs);
4499 std_expand_builtin_va_start (1, valist, nextarg);
4503 /* Implement va_arg. */
4506 mips_va_arg (valist, type)
4507 tree valist, type;
4509 HOST_WIDE_INT size, rsize;
4510 rtx addr_rtx;
4511 tree t;
4513 size = int_size_in_bytes (type);
4514 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4516 if (mips_abi == ABI_EABI)
4518 int indirect;
4519 rtx r, lab_over = NULL_RTX, lab_false;
4520 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4521 tree ovfl, gtop, ftop, goff, foff;
4523 indirect
4524 = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
4525 if (indirect)
4527 size = POINTER_SIZE / BITS_PER_UNIT;
4528 rsize = UNITS_PER_WORD;
4531 addr_rtx = gen_reg_rtx (Pmode);
4533 if (TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT)
4535 /* Case of all args in a merged stack. No need to check bounds,
4536 just advance valist along the stack. */
4538 tree gpr = valist;
4539 if (! indirect
4540 && ! TARGET_64BIT
4541 && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
4543 t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4544 build_int_2 (2*UNITS_PER_WORD - 1, 0));
4545 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
4546 build_int_2 (-2*UNITS_PER_WORD, -1));
4547 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4548 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4551 t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
4552 size_int (rsize));
4553 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4554 if (r != addr_rtx)
4555 emit_move_insn (addr_rtx, r);
4557 /* flush the POSTINCREMENT */
4558 emit_queue();
4560 if (indirect)
4562 r = gen_rtx_MEM (Pmode, addr_rtx);
4563 set_mem_alias_set (r, get_varargs_alias_set ());
4564 emit_move_insn (addr_rtx, r);
4566 else
4568 if (BYTES_BIG_ENDIAN && rsize != size)
4569 addr_rtx = plus_constant (addr_rtx, rsize - size);
4571 return addr_rtx;
4574 /* Not a simple merged stack. Need ptrs and indexes left by va_start. */
4576 f_ovfl = TYPE_FIELDS (va_list_type_node);
4577 f_gtop = TREE_CHAIN (f_ovfl);
4578 f_ftop = TREE_CHAIN (f_gtop);
4579 f_goff = TREE_CHAIN (f_ftop);
4580 f_foff = TREE_CHAIN (f_goff);
4582 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4583 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4584 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4585 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4586 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4588 lab_false = gen_label_rtx ();
4589 lab_over = gen_label_rtx ();
4591 if (TREE_CODE (type) == REAL_TYPE)
4594 /* Emit code to branch if foff == 0. */
4595 r = expand_expr (foff, NULL_RTX, TYPE_MODE (TREE_TYPE (foff)),
4596 EXPAND_NORMAL);
4597 emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
4598 1, lab_false);
4600 /* Emit code for addr_rtx = ftop - foff */
4601 t = build (MINUS_EXPR, TREE_TYPE (ftop), ftop, foff );
4602 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4603 if (r != addr_rtx)
4604 emit_move_insn (addr_rtx, r);
4606 /* Emit code for foff-=8.
4607 Advances the offset up FPR save area by one double */
4608 t = build (MINUS_EXPR, TREE_TYPE (foff), foff, build_int_2 (8, 0));
4609 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff, t);
4610 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4612 emit_queue();
4613 emit_jump (lab_over);
4614 emit_barrier ();
4615 emit_label (lab_false);
4617 if (!TARGET_64BIT)
4619 /* For mips2, the overflow area contains mixed size items.
4620 If a 4-byte int is followed by an 8-byte float, then
4621 natural alignment causes a 4 byte gap.
4622 So, dynamically adjust ovfl up to a multiple of 8. */
4623 t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), ovfl,
4624 build_int_2 (7, 0));
4625 t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl, t);
4626 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4627 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4630 /* Emit code for addr_rtx = the ovfl pointer into overflow area.
4631 Regardless of mips2, postincrement the ovfl pointer by 8. */
4632 t = build (POSTINCREMENT_EXPR, TREE_TYPE(ovfl), ovfl,
4633 size_int (8));
4634 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4635 if (r != addr_rtx)
4636 emit_move_insn (addr_rtx, r);
4638 emit_queue();
4639 emit_label (lab_over);
4640 return addr_rtx;
4642 else
4644 /* not REAL_TYPE */
4645 int step_size;
4647 if (! TARGET_64BIT
4648 && TREE_CODE (type) == INTEGER_TYPE
4649 && TYPE_PRECISION (type) == 64)
4651 /* In mips2, int takes 32 bits of the GPR save area, but
4652 longlong takes an aligned 64 bits. So, emit code
4653 to zero the low order bits of goff, thus aligning
4654 the later calculation of (gtop-goff) upwards. */
4655 t = build (BIT_AND_EXPR, TREE_TYPE (goff), goff,
4656 build_int_2 (-8, -1));
4657 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff, t);
4658 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4661 /* Emit code to branch if goff == 0. */
4662 r = expand_expr (goff, NULL_RTX, TYPE_MODE (TREE_TYPE (goff)),
4663 EXPAND_NORMAL);
4664 emit_cmp_and_jump_insns (r, const0_rtx, EQ, const1_rtx, GET_MODE (r),
4665 1, lab_false);
4667 /* Emit code for addr_rtx = gtop - goff. */
4668 t = build (MINUS_EXPR, TREE_TYPE (gtop), gtop, goff);
4669 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4670 if (r != addr_rtx)
4671 emit_move_insn (addr_rtx, r);
4673 /* Note that mips2 int is 32 bit, but mips2 longlong is 64. */
4674 if (! TARGET_64BIT && TYPE_PRECISION (type) == 64)
4675 step_size = 8;
4676 else
4677 step_size = UNITS_PER_WORD;
4679 /* Emit code for goff = goff - step_size.
4680 Advances the offset up GPR save area over the item. */
4681 t = build (MINUS_EXPR, TREE_TYPE (goff), goff,
4682 build_int_2 (step_size, 0));
4683 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff, t);
4684 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4686 emit_queue();
4687 emit_jump (lab_over);
4688 emit_barrier ();
4689 emit_label (lab_false);
4691 /* Emit code for addr_rtx -> overflow area, postinc by step_size */
4692 t = build (POSTINCREMENT_EXPR, TREE_TYPE(ovfl), ovfl,
4693 size_int (step_size));
4694 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4695 if (r != addr_rtx)
4696 emit_move_insn (addr_rtx, r);
4698 emit_queue();
4699 emit_label (lab_over);
4701 if (indirect)
4703 r = gen_rtx_MEM (Pmode, addr_rtx);
4704 set_mem_alias_set (r, get_varargs_alias_set ());
4705 emit_move_insn (addr_rtx, r);
4707 else
4709 if (BYTES_BIG_ENDIAN && rsize != size)
4710 addr_rtx = plus_constant (addr_rtx, rsize - size);
4712 return addr_rtx;
4715 else
4717 /* Not EABI. */
4718 int align;
4720 /* ??? The original va-mips.h did always align, despite the fact
4721 that alignments <= UNITS_PER_WORD are preserved by the va_arg
4722 increment mechanism. */
4724 if (TARGET_64BIT)
4725 align = 8;
4726 else if (TYPE_ALIGN (type) > 32)
4727 align = 8;
4728 else
4729 align = 4;
4731 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4732 build_int_2 (align - 1, 0));
4733 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
4734 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4735 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4737 /* Everything past the alignment is standard. */
4738 return std_expand_builtin_va_arg (valist, type);
4742 /* Abort after printing out a specific insn. */
4744 static void
4745 abort_with_insn (insn, reason)
4746 rtx insn;
4747 const char *reason;
4749 error (reason);
4750 debug_rtx (insn);
4751 abort ();
4754 /* Set up the threshold for data to go into the small data area, instead
4755 of the normal data area, and detect any conflicts in the switches. */
4757 void
4758 override_options ()
4760 register int i, start;
4761 register int regno;
4762 register enum machine_mode mode;
4763 register enum processor_type mips_cpu;
4765 mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4767 if (mips_section_threshold <= 0)
4768 target_flags &= ~MASK_GPOPT;
4769 else if (optimize)
4770 target_flags |= MASK_GPOPT;
4772 /* If both single-float and soft-float are set, then clear the one that
4773 was set by TARGET_DEFAULT, leaving the one that was set by the
4774 user. We assume here that the specs prevent both being set by the
4775 user. */
4776 #ifdef TARGET_DEFAULT
4777 if (TARGET_SINGLE_FLOAT && TARGET_SOFT_FLOAT)
4778 target_flags &= ~((TARGET_DEFAULT) & (MASK_SOFT_FLOAT | MASK_SINGLE_FLOAT));
4779 #endif
4781 /* Get the architectural level. */
4782 if (mips_isa_string == 0)
4783 mips_isa = MIPS_ISA_DEFAULT;
4785 else if (ISDIGIT (*mips_isa_string))
4787 mips_isa = atoi (mips_isa_string);
4788 if (mips_isa == 16)
4790 /* -mno-mips16 overrides -mips16. */
4791 if (mips_no_mips16_string == NULL)
4793 target_flags |= MASK_MIPS16;
4794 if (TARGET_64BIT)
4795 mips_isa = 3;
4796 else
4797 mips_isa = MIPS_ISA_DEFAULT;
4799 else
4801 mips_isa = MIPS_ISA_DEFAULT;
4804 else if (mips_isa < 1
4805 || (mips_isa > 4
4806 && mips_isa != 32
4807 && mips_isa != 64))
4809 error ("-mips%d not supported", mips_isa);
4810 mips_isa = 1;
4814 else
4816 error ("bad value (%s) for -mips switch", mips_isa_string);
4817 mips_isa = 1;
4820 #ifdef MIPS_ABI_DEFAULT
4821 /* Get the ABI to use. */
4822 if (mips_abi_string == (char *) 0)
4823 mips_abi = MIPS_ABI_DEFAULT;
4824 else if (! strcmp (mips_abi_string, "32"))
4825 mips_abi = ABI_32;
4826 else if (! strcmp (mips_abi_string, "o64"))
4827 mips_abi = ABI_O64;
4828 else if (! strcmp (mips_abi_string, "n32"))
4829 mips_abi = ABI_N32;
4830 else if (! strcmp (mips_abi_string, "64"))
4831 mips_abi = ABI_64;
4832 else if (! strcmp (mips_abi_string, "eabi"))
4833 mips_abi = ABI_EABI;
4834 else if (! strcmp (mips_abi_string, "meabi"))
4835 mips_abi = ABI_MEABI;
4836 else
4837 error ("bad value (%s) for -mabi= switch", mips_abi_string);
4839 /* A specified ISA defaults the ABI if it was not specified. */
4840 if (mips_abi_string == 0 && mips_isa_string
4841 && mips_abi != ABI_EABI
4842 && mips_abi != ABI_O64
4843 && mips_abi != ABI_MEABI)
4845 if (mips_isa == 64)
4846 mips_abi = ABI_O64;
4847 else
4849 if (! ISA_HAS_64BIT_REGS)
4850 mips_abi = ABI_32;
4851 else
4852 mips_abi = ABI_64;
4856 #ifdef MIPS_CPU_STRING_DEFAULT
4857 /* A specified ABI defaults the ISA if it was not specified. */
4858 else if (mips_isa_string == 0 && mips_abi_string
4859 && mips_abi != ABI_EABI && mips_abi != ABI_O64)
4861 if (mips_abi == ABI_32)
4862 mips_isa = 1;
4863 else if (mips_abi == ABI_N32)
4864 mips_isa = 3;
4865 else
4866 mips_isa = 4;
4868 #endif
4870 /* If both ABI and ISA were specified, check for conflicts. */
4871 else if (mips_isa_string && mips_abi_string)
4873 if (! ISA_HAS_64BIT_REGS && (mips_abi == ABI_N32 || mips_abi == ABI_64
4874 || mips_abi == ABI_O64))
4875 error ("-mabi=%s does not support -mips%d", mips_abi_string, mips_isa);
4878 /* Override TARGET_DEFAULT if necessary. */
4879 if (mips_abi == ABI_32)
4880 target_flags &= ~ (MASK_FLOAT64|MASK_64BIT);
4882 /* If no type size setting options (-mlong64,-mint64,-mlong32) were used
4883 then set the type sizes. In the EABI in 64 bit mode, longs and
4884 pointers are 64 bits. Likewise for the SGI Irix6 N64 ABI. */
4885 if (mips_explicit_type_size_string == NULL
4886 && ((mips_abi == ABI_EABI && TARGET_64BIT)
4887 || mips_abi == ABI_64))
4888 target_flags |= MASK_LONG64;
4890 #else
4891 if (mips_abi_string)
4892 error ("this target does not support the -mabi switch");
4893 #endif
4895 #ifdef MIPS_CPU_STRING_DEFAULT
4896 /* ??? There is a minor inconsistency here. If the user specifies an ISA
4897 greater than that supported by the default processor, then the user gets
4898 an error. Normally, the compiler will just default to the base level cpu
4899 for the indicated isa. */
4900 if (mips_arch_string == 0)
4901 mips_arch_string = MIPS_CPU_STRING_DEFAULT;
4902 if (mips_tune_string == 0)
4903 mips_tune_string = MIPS_CPU_STRING_DEFAULT;
4904 #endif
4906 /* Identify the processor type. */
4908 if (mips_cpu_string != 0)
4910 mips_cpu = mips_parse_cpu (mips_cpu_string);
4911 if (mips_cpu == PROCESSOR_DEFAULT)
4913 error ("bad value (%s) for -mcpu= switch", mips_arch_string);
4914 mips_cpu_string = "default";
4916 mips_arch = mips_cpu;
4917 mips_tune = mips_cpu;
4920 if (mips_arch_string == 0
4921 || ! strcmp (mips_arch_string, "default")
4922 || ! strcmp (mips_arch_string, "DEFAULT"))
4924 switch (mips_isa)
4926 default:
4927 mips_arch_string = "3000";
4928 mips_arch = PROCESSOR_R3000;
4929 break;
4930 case 2:
4931 mips_arch_string = "6000";
4932 mips_arch = PROCESSOR_R6000;
4933 break;
4934 case 3:
4935 mips_arch_string = "4000";
4936 mips_arch = PROCESSOR_R4000;
4937 break;
4938 case 4:
4939 mips_arch_string = "8000";
4940 mips_arch = PROCESSOR_R8000;
4941 break;
4942 case 32:
4943 mips_arch_string = "4kc";
4944 mips_arch = PROCESSOR_R4KC;
4945 break;
4946 case 64:
4947 mips_arch_string = "5kc";
4948 mips_arch = PROCESSOR_R5KC;
4949 break;
4952 else
4954 mips_arch = mips_parse_cpu (mips_arch_string);
4955 if (mips_arch == PROCESSOR_DEFAULT)
4957 error ("bad value (%s) for -march= switch", mips_arch_string);
4958 mips_arch_string = "default";
4961 if (mips_tune_string == 0
4962 || ! strcmp (mips_tune_string, "default")
4963 || ! strcmp (mips_tune_string, "DEFAULT"))
4965 if (mips_arch != PROCESSOR_DEFAULT)
4966 mips_tune = mips_arch;
4967 else
4968 switch (mips_isa)
4970 default:
4971 mips_tune_string = "3000";
4972 mips_tune = PROCESSOR_R3000;
4973 break;
4974 case 2:
4975 mips_tune_string = "6000";
4976 mips_tune = PROCESSOR_R6000;
4977 break;
4978 case 3:
4979 mips_tune_string = "4000";
4980 mips_tune = PROCESSOR_R4000;
4981 break;
4982 case 4:
4983 mips_tune_string = "8000";
4984 mips_tune = PROCESSOR_R8000;
4985 break;
4986 case 32:
4987 mips_tune_string = "4kc";
4988 mips_tune = PROCESSOR_R4KC;
4989 break;
4990 case 64:
4991 mips_tune_string = "5kc";
4992 mips_tune = PROCESSOR_R5KC;
4993 break;
4997 else
4999 mips_tune = mips_parse_cpu (mips_tune_string);
5000 if (mips_tune == PROCESSOR_DEFAULT)
5002 error ("bad value (%s) for -mtune= switch", mips_tune_string);
5003 mips_tune_string = "default";
5007 if ((mips_arch == PROCESSOR_R3000 && (mips_isa != 1))
5008 || (mips_arch == PROCESSOR_R4KC && mips_isa != 32)
5009 || ((mips_arch == PROCESSOR_R5KC
5010 || mips_arch == PROCESSOR_R20KC) && mips_isa != 64)
5011 || (mips_arch == PROCESSOR_R6000 && mips_isa != 1 && mips_isa != 2)
5012 || ((mips_arch == PROCESSOR_R4000
5013 || mips_arch == PROCESSOR_R4100
5014 || mips_arch == PROCESSOR_R4300
5015 || mips_arch == PROCESSOR_R4600
5016 || mips_arch == PROCESSOR_R4650)
5017 && mips_isa != 1 && mips_isa != 2 && mips_isa != 3))
5018 error ("-march=%s does not support -mips%d", mips_arch_string, mips_isa);
5020 /* make sure sizes of ints/longs/etc. are ok */
5021 if (! ISA_HAS_64BIT_REGS)
5023 if (TARGET_FLOAT64)
5025 error ("-mips%d does not support 64 bit fp registers", mips_isa);
5026 target_flags &= ~ MASK_FLOAT64;
5029 else if (TARGET_64BIT)
5031 error ("-mips%d does not support 64 bit gp registers", mips_isa);
5032 target_flags &= ~MASK_64BIT;
5036 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
5037 flag_pcc_struct_return = 0;
5039 /* Tell halfpic.c that we have half-pic code if we do. */
5040 if (TARGET_HALF_PIC)
5041 HALF_PIC_INIT ();
5043 /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined. We need
5044 to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work. */
5045 /* ??? -non_shared turns off pic code generation, but this is not
5046 implemented. */
5047 if (TARGET_ABICALLS)
5049 mips_abicalls = MIPS_ABICALLS_YES;
5050 flag_pic = 1;
5051 if (mips_section_threshold > 0)
5052 warning ("-G is incompatible with PIC code which is the default");
5054 else
5055 mips_abicalls = MIPS_ABICALLS_NO;
5057 /* -membedded-pic is a form of PIC code suitable for embedded
5058 systems. All calls are made using PC relative addressing, and
5059 all data is addressed using the $gp register. This requires gas,
5060 which does most of the work, and GNU ld, which automatically
5061 expands PC relative calls which are out of range into a longer
5062 instruction sequence. All gcc really does differently is
5063 generate a different sequence for a switch. */
5064 if (TARGET_EMBEDDED_PIC)
5066 flag_pic = 1;
5067 if (TARGET_ABICALLS)
5068 warning ("-membedded-pic and -mabicalls are incompatible");
5070 if (g_switch_set)
5071 warning ("-G and -membedded-pic are incompatible");
5073 /* Setting mips_section_threshold is not required, because gas
5074 will force everything to be GP addressable anyhow, but
5075 setting it will cause gcc to make better estimates of the
5076 number of instructions required to access a particular data
5077 item. */
5078 mips_section_threshold = 0x7fffffff;
5081 /* This optimization requires a linker that can support a R_MIPS_LO16
5082 relocation which is not immediately preceded by a R_MIPS_HI16 relocation.
5083 GNU ld has this support, but not all other MIPS linkers do, so we enable
5084 this optimization only if the user requests it, or if GNU ld is the
5085 standard linker for this configuration. */
5086 /* ??? This does not work when target addresses are DImode.
5087 This is because we are missing DImode high/lo_sum patterns. */
5088 if (TARGET_GAS && ! TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES && optimize && ! flag_pic
5089 && Pmode == SImode)
5090 mips_split_addresses = 1;
5091 else
5092 mips_split_addresses = 0;
5094 /* -mrnames says to use the MIPS software convention for register
5095 names instead of the hardware names (ie, $a0 instead of $4).
5096 We do this by switching the names in mips_reg_names, which the
5097 reg_names points into via the REGISTER_NAMES macro. */
5099 if (TARGET_NAME_REGS)
5100 memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
5102 /* When compiling for the mips16, we can not use floating point. We
5103 record the original hard float value in mips16_hard_float. */
5104 if (TARGET_MIPS16)
5106 if (TARGET_SOFT_FLOAT)
5107 mips16_hard_float = 0;
5108 else
5109 mips16_hard_float = 1;
5110 target_flags |= MASK_SOFT_FLOAT;
5112 /* Don't run the scheduler before reload, since it tends to
5113 increase register pressure. */
5114 flag_schedule_insns = 0;
5117 /* We put -mentry in TARGET_OPTIONS rather than TARGET_SWITCHES only
5118 to avoid using up another bit in target_flags. */
5119 if (mips_entry_string != NULL)
5121 if (*mips_entry_string != '\0')
5122 error ("invalid option `entry%s'", mips_entry_string);
5124 if (! TARGET_MIPS16)
5125 warning ("-mentry is only meaningful with -mips-16");
5126 else
5127 mips_entry = 1;
5130 /* We copy TARGET_MIPS16 into the mips16 global variable, so that
5131 attributes can access it. */
5132 if (TARGET_MIPS16)
5133 mips16 = 1;
5134 else
5135 mips16 = 0;
5137 /* Initialize the high and low values for legitimate floating point
5138 constants. Rather than trying to get the accuracy down to the
5139 last bit, just use approximate ranges. */
5140 dfhigh = REAL_VALUE_ATOF ("1.0e300", DFmode);
5141 dflow = REAL_VALUE_ATOF ("1.0e-300", DFmode);
5142 sfhigh = REAL_VALUE_ATOF ("1.0e38", SFmode);
5143 sflow = REAL_VALUE_ATOF ("1.0e-38", SFmode);
5145 mips_print_operand_punct['?'] = 1;
5146 mips_print_operand_punct['#'] = 1;
5147 mips_print_operand_punct['&'] = 1;
5148 mips_print_operand_punct['!'] = 1;
5149 mips_print_operand_punct['*'] = 1;
5150 mips_print_operand_punct['@'] = 1;
5151 mips_print_operand_punct['.'] = 1;
5152 mips_print_operand_punct['('] = 1;
5153 mips_print_operand_punct[')'] = 1;
5154 mips_print_operand_punct['['] = 1;
5155 mips_print_operand_punct[']'] = 1;
5156 mips_print_operand_punct['<'] = 1;
5157 mips_print_operand_punct['>'] = 1;
5158 mips_print_operand_punct['{'] = 1;
5159 mips_print_operand_punct['}'] = 1;
5160 mips_print_operand_punct['^'] = 1;
5161 mips_print_operand_punct['$'] = 1;
5162 mips_print_operand_punct['+'] = 1;
5163 mips_print_operand_punct['~'] = 1;
5165 mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
5166 mips_char_to_class['e'] = M16_NA_REGS;
5167 mips_char_to_class['t'] = T_REG;
5168 mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
5169 mips_char_to_class['h'] = HI_REG;
5170 mips_char_to_class['l'] = LO_REG;
5171 mips_char_to_class['a'] = HILO_REG;
5172 mips_char_to_class['x'] = MD_REGS;
5173 mips_char_to_class['b'] = ALL_REGS;
5174 mips_char_to_class['y'] = GR_REGS;
5175 mips_char_to_class['z'] = ST_REGS;
5177 /* Set up array to map GCC register number to debug register number.
5178 Ignore the special purpose register numbers. */
5180 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5181 mips_dbx_regno[i] = -1;
5183 start = GP_DBX_FIRST - GP_REG_FIRST;
5184 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
5185 mips_dbx_regno[i] = i + start;
5187 start = FP_DBX_FIRST - FP_REG_FIRST;
5188 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
5189 mips_dbx_regno[i] = i + start;
5191 /* Set up array giving whether a given register can hold a given mode.
5192 At present, restrict ints from being in FP registers, because reload
5193 is a little enthusiastic about storing extra values in FP registers,
5194 and this is not good for things like OS kernels. Also, due to the
5195 mandatory delay, it is as fast to load from cached memory as to move
5196 from the FP register. */
5198 for (mode = VOIDmode;
5199 mode != MAX_MACHINE_MODE;
5200 mode = (enum machine_mode) ((int)mode + 1))
5202 register int size = GET_MODE_SIZE (mode);
5203 register enum mode_class class = GET_MODE_CLASS (mode);
5205 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5207 register int temp;
5209 if (mode == CCmode)
5211 if (! ISA_HAS_8CC)
5212 temp = (regno == FPSW_REGNUM);
5213 else
5214 temp = (ST_REG_P (regno) || GP_REG_P (regno)
5215 || FP_REG_P (regno));
5218 else if (GP_REG_P (regno))
5219 temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
5221 else if (FP_REG_P (regno))
5222 temp = ((TARGET_FLOAT64 || ((regno & 1) == 0)
5223 /* I think this change is OK regardless of abi, but
5224 I'm being cautions untill I can test this more.
5225 HARD_REGNO_MODE_OK is about whether or not you
5226 can move to and from a register without changing
5227 the value, not about whether math works on the
5228 register. */
5229 || (mips_abi == ABI_MEABI && size <= 4))
5230 && (class == MODE_FLOAT
5231 || class == MODE_COMPLEX_FLOAT
5232 || (TARGET_DEBUG_H_MODE && class == MODE_INT))
5233 && (! TARGET_SINGLE_FLOAT || size <= 4));
5235 else if (MD_REG_P (regno))
5236 temp = (class == MODE_INT
5237 && (size <= UNITS_PER_WORD
5238 || (regno == MD_REG_FIRST
5239 && size == 2 * UNITS_PER_WORD)));
5241 else
5242 temp = 0;
5244 mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5248 /* Save GPR registers in word_mode sized hunks. word_mode hasn't been
5249 initialized yet, so we can't use that here. */
5250 gpr_mode = TARGET_64BIT ? DImode : SImode;
5252 /* Provide default values for align_* for 64-bit targets. */
5253 if (TARGET_64BIT && !TARGET_MIPS16)
5255 if (align_loops == 0)
5256 align_loops = 8;
5257 if (align_jumps == 0)
5258 align_jumps = 8;
5259 if (align_functions == 0)
5260 align_functions = 8;
5263 /* Register global variables with the garbage collector. */
5264 mips_add_gc_roots ();
5267 /* On the mips16, we want to allocate $24 (T_REG) before other
5268 registers for instructions for which it is possible. This helps
5269 avoid shuffling registers around in order to set up for an xor,
5270 encouraging the compiler to use a cmp instead. */
5272 void
5273 mips_order_regs_for_local_alloc ()
5275 register int i;
5277 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5278 reg_alloc_order[i] = i;
5280 if (TARGET_MIPS16)
5282 /* It really doesn't matter where we put register 0, since it is
5283 a fixed register anyhow. */
5284 reg_alloc_order[0] = 24;
5285 reg_alloc_order[24] = 0;
5290 /* The MIPS debug format wants all automatic variables and arguments
5291 to be in terms of the virtual frame pointer (stack pointer before
5292 any adjustment in the function), while the MIPS 3.0 linker wants
5293 the frame pointer to be the stack pointer after the initial
5294 adjustment. So, we do the adjustment here. The arg pointer (which
5295 is eliminated) points to the virtual frame pointer, while the frame
5296 pointer (which may be eliminated) points to the stack pointer after
5297 the initial adjustments. */
5299 HOST_WIDE_INT
5300 mips_debugger_offset (addr, offset)
5301 rtx addr;
5302 HOST_WIDE_INT offset;
5304 rtx offset2 = const0_rtx;
5305 rtx reg = eliminate_constant_term (addr, &offset2);
5307 if (offset == 0)
5308 offset = INTVAL (offset2);
5310 if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5311 || reg == hard_frame_pointer_rtx)
5313 HOST_WIDE_INT frame_size = (!current_frame_info.initialized)
5314 ? compute_frame_size (get_frame_size ())
5315 : current_frame_info.total_size;
5317 /* MIPS16 frame is smaller */
5318 if (frame_pointer_needed && TARGET_MIPS16)
5319 frame_size -= current_function_outgoing_args_size;
5321 offset = offset - frame_size;
5324 /* sdbout_parms does not want this to crash for unrecognized cases. */
5325 #if 0
5326 else if (reg != arg_pointer_rtx)
5327 abort_with_insn (addr, "mips_debugger_offset called with non stack/frame/arg pointer");
5328 #endif
5330 return offset;
5333 /* A C compound statement to output to stdio stream STREAM the
5334 assembler syntax for an instruction operand X. X is an RTL
5335 expression.
5337 CODE is a value that can be used to specify one of several ways
5338 of printing the operand. It is used when identical operands
5339 must be printed differently depending on the context. CODE
5340 comes from the `%' specification that was used to request
5341 printing of the operand. If the specification was just `%DIGIT'
5342 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
5343 is the ASCII code for LTR.
5345 If X is a register, this macro should print the register's name.
5346 The names can be found in an array `reg_names' whose type is
5347 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
5349 When the machine description has a specification `%PUNCT' (a `%'
5350 followed by a punctuation character), this macro is called with
5351 a null pointer for X and the punctuation character for CODE.
5353 The MIPS specific codes are:
5355 'X' X is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5356 'x' X is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
5357 'd' output integer constant in decimal,
5358 'z' if the operand is 0, use $0 instead of normal operand.
5359 'D' print second part of double-word register or memory operand.
5360 'L' print low-order register of double-word register operand.
5361 'M' print high-order register of double-word register operand.
5362 'C' print part of opcode for a branch condition.
5363 'F' print part of opcode for a floating-point branch condition.
5364 'N' print part of opcode for a branch condition, inverted.
5365 'W' print part of opcode for a floating-point branch condition, inverted.
5366 'S' X is CODE_LABEL, print with prefix of "LS" (for embedded switch).
5367 'B' print 'z' for EQ, 'n' for NE
5368 'b' print 'n' for EQ, 'z' for NE
5369 'T' print 'f' for EQ, 't' for NE
5370 't' print 't' for EQ, 'f' for NE
5371 'Z' print register and a comma, but print nothing for $fcc0
5372 '(' Turn on .set noreorder
5373 ')' Turn on .set reorder
5374 '[' Turn on .set noat
5375 ']' Turn on .set at
5376 '<' Turn on .set nomacro
5377 '>' Turn on .set macro
5378 '{' Turn on .set volatile (not GAS)
5379 '}' Turn on .set novolatile (not GAS)
5380 '&' Turn on .set noreorder if filling delay slots
5381 '*' Turn on both .set noreorder and .set nomacro if filling delay slots
5382 '!' Turn on .set nomacro if filling delay slots
5383 '#' Print nop if in a .set noreorder section.
5384 '?' Print 'l' if we are to use a branch likely instead of normal branch.
5385 '@' Print the name of the assembler temporary register (at or $1).
5386 '.' Print the name of the register with a hard-wired zero (zero or $0).
5387 '^' Print the name of the pic call-through register (t9 or $25).
5388 '$' Print the name of the stack pointer register (sp or $29).
5389 '+' Print the name of the gp register (gp or $28).
5390 '~' Output an branch alignment to LABEL_ALIGN(NULL). */
5392 void
5393 print_operand (file, op, letter)
5394 FILE *file; /* file to write to */
5395 rtx op; /* operand to print */
5396 int letter; /* %<letter> or 0 */
5398 register enum rtx_code code;
5400 if (PRINT_OPERAND_PUNCT_VALID_P (letter))
5402 switch (letter)
5404 case '?':
5405 if (mips_branch_likely)
5406 putc ('l', file);
5407 break;
5409 case '@':
5410 fputs (reg_names [GP_REG_FIRST + 1], file);
5411 break;
5413 case '^':
5414 fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
5415 break;
5417 case '.':
5418 fputs (reg_names [GP_REG_FIRST + 0], file);
5419 break;
5421 case '$':
5422 fputs (reg_names[STACK_POINTER_REGNUM], file);
5423 break;
5425 case '+':
5426 fputs (reg_names[GP_REG_FIRST + 28], file);
5427 break;
5429 case '&':
5430 if (final_sequence != 0 && set_noreorder++ == 0)
5431 fputs (".set\tnoreorder\n\t", file);
5432 break;
5434 case '*':
5435 if (final_sequence != 0)
5437 if (set_noreorder++ == 0)
5438 fputs (".set\tnoreorder\n\t", file);
5440 if (set_nomacro++ == 0)
5441 fputs (".set\tnomacro\n\t", file);
5443 break;
5445 case '!':
5446 if (final_sequence != 0 && set_nomacro++ == 0)
5447 fputs ("\n\t.set\tnomacro", file);
5448 break;
5450 case '#':
5451 if (set_noreorder != 0)
5452 fputs ("\n\tnop", file);
5453 else if (TARGET_STATS)
5454 fputs ("\n\t#nop", file);
5456 break;
5458 case '(':
5459 if (set_noreorder++ == 0)
5460 fputs (".set\tnoreorder\n\t", file);
5461 break;
5463 case ')':
5464 if (set_noreorder == 0)
5465 error ("internal error: %%) found without a %%( in assembler pattern");
5467 else if (--set_noreorder == 0)
5468 fputs ("\n\t.set\treorder", file);
5470 break;
5472 case '[':
5473 if (set_noat++ == 0)
5474 fputs (".set\tnoat\n\t", file);
5475 break;
5477 case ']':
5478 if (set_noat == 0)
5479 error ("internal error: %%] found without a %%[ in assembler pattern");
5480 else if (--set_noat == 0)
5481 fputs ("\n\t.set\tat", file);
5483 break;
5485 case '<':
5486 if (set_nomacro++ == 0)
5487 fputs (".set\tnomacro\n\t", file);
5488 break;
5490 case '>':
5491 if (set_nomacro == 0)
5492 error ("internal error: %%> found without a %%< in assembler pattern");
5493 else if (--set_nomacro == 0)
5494 fputs ("\n\t.set\tmacro", file);
5496 break;
5498 case '{':
5499 if (set_volatile++ == 0)
5500 fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
5501 break;
5503 case '}':
5504 if (set_volatile == 0)
5505 error ("internal error: %%} found without a %%{ in assembler pattern");
5506 else if (--set_volatile == 0)
5507 fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
5509 break;
5511 case '~':
5513 if (align_labels_log > 0)
5514 ASM_OUTPUT_ALIGN (file, align_labels_log);
5516 break;
5518 default:
5519 error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
5520 break;
5523 return;
5526 if (! op)
5528 error ("PRINT_OPERAND null pointer");
5529 return;
5532 code = GET_CODE (op);
5534 if (code == SIGN_EXTEND)
5535 op = XEXP (op, 0), code = GET_CODE (op);
5537 if (letter == 'C')
5538 switch (code)
5540 case EQ: fputs ("eq", file); break;
5541 case NE: fputs ("ne", file); break;
5542 case GT: fputs ("gt", file); break;
5543 case GE: fputs ("ge", file); break;
5544 case LT: fputs ("lt", file); break;
5545 case LE: fputs ("le", file); break;
5546 case GTU: fputs ("gtu", file); break;
5547 case GEU: fputs ("geu", file); break;
5548 case LTU: fputs ("ltu", file); break;
5549 case LEU: fputs ("leu", file); break;
5550 default:
5551 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
5554 else if (letter == 'N')
5555 switch (code)
5557 case EQ: fputs ("ne", file); break;
5558 case NE: fputs ("eq", file); break;
5559 case GT: fputs ("le", file); break;
5560 case GE: fputs ("lt", file); break;
5561 case LT: fputs ("ge", file); break;
5562 case LE: fputs ("gt", file); break;
5563 case GTU: fputs ("leu", file); break;
5564 case GEU: fputs ("ltu", file); break;
5565 case LTU: fputs ("geu", file); break;
5566 case LEU: fputs ("gtu", file); break;
5567 default:
5568 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
5571 else if (letter == 'F')
5572 switch (code)
5574 case EQ: fputs ("c1f", file); break;
5575 case NE: fputs ("c1t", file); break;
5576 default:
5577 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
5580 else if (letter == 'W')
5581 switch (code)
5583 case EQ: fputs ("c1t", file); break;
5584 case NE: fputs ("c1f", file); break;
5585 default:
5586 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
5589 else if (letter == 'S')
5591 char buffer[100];
5593 ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
5594 assemble_name (file, buffer);
5597 else if (letter == 'Z')
5599 register int regnum;
5601 if (code != REG)
5602 abort ();
5604 regnum = REGNO (op);
5605 if (! ST_REG_P (regnum))
5606 abort ();
5608 if (regnum != ST_REG_FIRST)
5609 fprintf (file, "%s,", reg_names[regnum]);
5612 else if (code == REG || code == SUBREG)
5614 register int regnum;
5616 if (code == REG)
5617 regnum = REGNO (op);
5618 else
5619 regnum = true_regnum (op);
5621 if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
5622 || (letter == 'L' && WORDS_BIG_ENDIAN)
5623 || letter == 'D')
5624 regnum++;
5626 fprintf (file, "%s", reg_names[regnum]);
5629 else if (code == MEM)
5631 if (letter == 'D')
5632 output_address (plus_constant (XEXP (op, 0), 4));
5633 else
5634 output_address (XEXP (op, 0));
5637 else if (code == CONST_DOUBLE
5638 && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
5640 REAL_VALUE_TYPE d;
5641 char s[30];
5643 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
5644 REAL_VALUE_TO_DECIMAL (d, "%.20e", s);
5645 fprintf (file, s);
5648 else if (letter == 'x' && GET_CODE (op) == CONST_INT)
5649 fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
5651 else if (letter == 'X' && GET_CODE(op) == CONST_INT)
5652 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
5654 else if (letter == 'd' && GET_CODE(op) == CONST_INT)
5655 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
5657 else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
5658 fputs (reg_names[GP_REG_FIRST], file);
5660 else if (letter == 'd' || letter == 'x' || letter == 'X')
5661 output_operand_lossage ("invalid use of %%d, %%x, or %%X");
5663 else if (letter == 'B')
5664 fputs (code == EQ ? "z" : "n", file);
5665 else if (letter == 'b')
5666 fputs (code == EQ ? "n" : "z", file);
5667 else if (letter == 'T')
5668 fputs (code == EQ ? "f" : "t", file);
5669 else if (letter == 't')
5670 fputs (code == EQ ? "t" : "f", file);
5672 else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
5674 /* This case arises on the mips16; see mips16_gp_pseudo_reg. */
5675 print_operand (file, XEXP (op, 0), letter);
5678 else if (TARGET_MIPS16 && code == CONST && mips16_gp_offset_p (op))
5680 fputs ("%gprel(", file);
5681 mips16_output_gp_offset (file, op);
5682 fputs (")", file);
5685 else
5686 output_addr_const (file, op);
5689 /* A C compound statement to output to stdio stream STREAM the
5690 assembler syntax for an instruction operand that is a memory
5691 reference whose address is ADDR. ADDR is an RTL expression.
5693 On some machines, the syntax for a symbolic address depends on
5694 the section that the address refers to. On these machines,
5695 define the macro `ENCODE_SECTION_INFO' to store the information
5696 into the `symbol_ref', and then check for it here. */
5698 void
5699 print_operand_address (file, addr)
5700 FILE *file;
5701 rtx addr;
5703 if (!addr)
5704 error ("PRINT_OPERAND_ADDRESS, null pointer");
5706 else
5707 switch (GET_CODE (addr))
5709 case REG:
5710 if (! TARGET_MIPS16 && REGNO (addr) == ARG_POINTER_REGNUM)
5711 abort_with_insn (addr, "arg pointer not eliminated");
5713 fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
5714 break;
5716 case LO_SUM:
5718 register rtx arg0 = XEXP (addr, 0);
5719 register rtx arg1 = XEXP (addr, 1);
5721 if (! mips_split_addresses)
5722 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, Spurious LO_SUM");
5724 if (GET_CODE (arg0) != REG)
5725 abort_with_insn (addr,
5726 "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG");
5728 fprintf (file, "%%lo(");
5729 print_operand_address (file, arg1);
5730 fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
5732 break;
5734 case PLUS:
5736 register rtx reg = 0;
5737 register rtx offset = 0;
5738 register rtx arg0 = XEXP (addr, 0);
5739 register rtx arg1 = XEXP (addr, 1);
5741 if (GET_CODE (arg0) == REG)
5743 reg = arg0;
5744 offset = arg1;
5745 if (GET_CODE (offset) == REG)
5746 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
5749 else if (GET_CODE (arg1) == REG)
5750 reg = arg1, offset = arg0;
5751 else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
5753 output_addr_const (file, addr);
5754 break;
5756 else
5757 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
5759 if (! CONSTANT_P (offset))
5760 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
5762 if (REGNO (reg) == ARG_POINTER_REGNUM)
5763 abort_with_insn (addr, "arg pointer not eliminated");
5765 if (TARGET_MIPS16
5766 && GET_CODE (offset) == CONST
5767 && mips16_gp_offset_p (offset))
5769 fputs ("%gprel(", file);
5770 mips16_output_gp_offset (file, offset);
5771 fputs (")", file);
5773 else
5774 output_addr_const (file, offset);
5775 fprintf (file, "(%s)", reg_names [REGNO (reg)]);
5777 break;
5779 case LABEL_REF:
5780 case SYMBOL_REF:
5781 case CONST_INT:
5782 case CONST:
5783 output_addr_const (file, addr);
5784 break;
5786 default:
5787 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
5788 break;
5793 /* If optimizing for the global pointer, keep track of all of the externs, so
5794 that at the end of the file, we can emit the appropriate .extern
5795 declaration for them, before writing out the text section. We assume all
5796 names passed to us are in the permanent obstack, so they will be valid at
5797 the end of the compilation.
5799 If we have -G 0, or the extern size is unknown, or the object is in a user
5800 specified section that is not .sbss/.sdata, don't bother emitting the
5801 .externs. In the case of user specified sections this behaviour is
5802 required as otherwise GAS will think the object lives in .sbss/.sdata. */
5805 mips_output_external (file, decl, name)
5806 FILE *file ATTRIBUTE_UNUSED;
5807 tree decl;
5808 const char *name;
5810 register struct extern_list *p;
5811 int len;
5812 tree section_name;
5814 if (TARGET_GP_OPT
5815 && TREE_CODE (decl) != FUNCTION_DECL
5816 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
5817 && ((section_name = DECL_SECTION_NAME (decl)) == NULL
5818 || strcmp (TREE_STRING_POINTER (section_name), ".sbss") == 0
5819 || strcmp (TREE_STRING_POINTER (section_name), ".sdata") == 0))
5821 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5822 p->next = extern_head;
5823 p->name = name;
5824 p->size = len;
5825 extern_head = p;
5828 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5829 if (TREE_CODE (decl) == FUNCTION_DECL
5830 /* ??? Don't include alloca, since gcc will always expand it
5831 inline. If we don't do this, the C++ library fails to build. */
5832 && strcmp (name, "alloca")
5833 /* ??? Don't include __builtin_next_arg, because then gcc will not
5834 bootstrap under Irix 5.1. */
5835 && strcmp (name, "__builtin_next_arg"))
5837 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5838 p->next = extern_head;
5839 p->name = name;
5840 p->size = -1;
5841 extern_head = p;
5843 #endif
5845 return 0;
5848 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5850 mips_output_external_libcall (file, name)
5851 FILE *file ATTRIBUTE_UNUSED;
5852 const char *name;
5854 register struct extern_list *p;
5856 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5857 p->next = extern_head;
5858 p->name = name;
5859 p->size = -1;
5860 extern_head = p;
5862 return 0;
5864 #endif
5866 /* Emit a new filename to a stream. If this is MIPS ECOFF, watch out
5867 for .file's that start within a function. If we are smuggling stabs, try to
5868 put out a MIPS ECOFF file and a stab. */
5870 void
5871 mips_output_filename (stream, name)
5872 FILE *stream;
5873 const char *name;
5875 static int first_time = 1;
5876 char ltext_label_name[100];
5878 if (first_time)
5880 first_time = 0;
5881 SET_FILE_NUMBER ();
5882 current_function_file = name;
5883 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5884 /* This tells mips-tfile that stabs will follow. */
5885 if (!TARGET_GAS && write_symbols == DBX_DEBUG)
5886 fprintf (stream, "\t#@stabs\n");
5889 else if (write_symbols == DBX_DEBUG)
5891 ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
5892 fprintf (stream, "%s", ASM_STABS_OP);
5893 output_quoted_string (stream, name);
5894 fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
5897 else if (name != current_function_file
5898 && strcmp (name, current_function_file) != 0)
5900 if (inside_function && !TARGET_GAS)
5902 if (!file_in_function_warning)
5904 file_in_function_warning = 1;
5905 ignore_line_number = 1;
5906 warning ("MIPS ECOFF format does not allow changing filenames within functions with #line");
5909 else
5911 SET_FILE_NUMBER ();
5912 current_function_file = name;
5913 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5918 /* Emit a linenumber. For encapsulated stabs, we need to put out a stab
5919 as well as a .loc, since it is possible that MIPS ECOFF might not be
5920 able to represent the location for inlines that come from a different
5921 file. */
5923 void
5924 mips_output_lineno (stream, line)
5925 FILE *stream;
5926 int line;
5928 if (write_symbols == DBX_DEBUG)
5930 ++sym_lineno;
5931 fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
5932 LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
5933 LOCAL_LABEL_PREFIX, sym_lineno);
5935 else
5937 fprintf (stream, "\n\t%s.loc\t%d %d\n",
5938 (ignore_line_number) ? "#" : "",
5939 num_source_filenames, line);
5941 LABEL_AFTER_LOC (stream);
5945 /* Output an ASCII string, in a space-saving way. */
5947 void
5948 mips_output_ascii (stream, string_param, len)
5949 FILE *stream;
5950 const char *string_param;
5951 size_t len;
5953 size_t i;
5954 int cur_pos = 17;
5955 register const unsigned char *string =
5956 (const unsigned char *)string_param;
5958 fprintf (stream, "\t.ascii\t\"");
5959 for (i = 0; i < len; i++)
5961 register int c = string[i];
5963 switch (c)
5965 case '\"':
5966 case '\\':
5967 putc ('\\', stream);
5968 putc (c, stream);
5969 cur_pos += 2;
5970 break;
5972 case TARGET_NEWLINE:
5973 fputs ("\\n", stream);
5974 if (i+1 < len
5975 && (((c = string[i+1]) >= '\040' && c <= '~')
5976 || c == TARGET_TAB))
5977 cur_pos = 32767; /* break right here */
5978 else
5979 cur_pos += 2;
5980 break;
5982 case TARGET_TAB:
5983 fputs ("\\t", stream);
5984 cur_pos += 2;
5985 break;
5987 case TARGET_FF:
5988 fputs ("\\f", stream);
5989 cur_pos += 2;
5990 break;
5992 case TARGET_BS:
5993 fputs ("\\b", stream);
5994 cur_pos += 2;
5995 break;
5997 case TARGET_CR:
5998 fputs ("\\r", stream);
5999 cur_pos += 2;
6000 break;
6002 default:
6003 if (c >= ' ' && c < 0177)
6005 putc (c, stream);
6006 cur_pos++;
6008 else
6010 fprintf (stream, "\\%03o", c);
6011 cur_pos += 4;
6015 if (cur_pos > 72 && i+1 < len)
6017 cur_pos = 17;
6018 fprintf (stream, "\"\n\t.ascii\t\"");
6021 fprintf (stream, "\"\n");
6024 /* If defined, a C statement to be executed just prior to the output of
6025 assembler code for INSN, to modify the extracted operands so they will be
6026 output differently.
6028 Here the argument OPVEC is the vector containing the operands extracted
6029 from INSN, and NOPERANDS is the number of elements of the vector which
6030 contain meaningful data for this insn. The contents of this vector are
6031 what will be used to convert the insn template into assembler code, so you
6032 can change the assembler output by changing the contents of the vector.
6034 We use it to check if the current insn needs a nop in front of it because
6035 of load delays, and also to update the delay slot statistics. */
6037 /* ??? There is no real need for this function, because it never actually
6038 emits a NOP anymore. */
6040 void
6041 final_prescan_insn (insn, opvec, noperands)
6042 rtx insn;
6043 rtx opvec[] ATTRIBUTE_UNUSED;
6044 int noperands ATTRIBUTE_UNUSED;
6046 if (dslots_number_nops > 0)
6048 rtx pattern = PATTERN (insn);
6049 int length = get_attr_length (insn);
6051 /* Do we need to emit a NOP? */
6052 if (length == 0
6053 || (mips_load_reg != 0 && reg_mentioned_p (mips_load_reg, pattern))
6054 || (mips_load_reg2 != 0 && reg_mentioned_p (mips_load_reg2, pattern))
6055 || (mips_load_reg3 != 0 && reg_mentioned_p (mips_load_reg3, pattern))
6056 || (mips_load_reg4 != 0
6057 && reg_mentioned_p (mips_load_reg4, pattern)))
6058 fputs ("\t#nop\n", asm_out_file);
6060 else
6061 dslots_load_filled++;
6063 while (--dslots_number_nops > 0)
6064 fputs ("\t#nop\n", asm_out_file);
6066 mips_load_reg = 0;
6067 mips_load_reg2 = 0;
6068 mips_load_reg3 = 0;
6069 mips_load_reg4 = 0;
6072 if (TARGET_STATS
6073 && (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CALL_INSN))
6074 dslots_jump_total++;
6077 /* Output at beginning of assembler file.
6079 If we are optimizing to use the global pointer, create a temporary file to
6080 hold all of the text stuff, and write it out to the end. This is needed
6081 because the MIPS assembler is evidently one pass, and if it hasn't seen the
6082 relevant .comm/.lcomm/.extern/.sdata declaration when the code is
6083 processed, it generates a two instruction sequence. */
6085 void
6086 mips_asm_file_start (stream)
6087 FILE *stream;
6089 ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
6091 /* Versions of the MIPS assembler before 2.20 generate errors if a branch
6092 inside of a .set noreorder section jumps to a label outside of the .set
6093 noreorder section. Revision 2.20 just set nobopt silently rather than
6094 fixing the bug. */
6096 if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
6097 fprintf (stream, "\t.set\tnobopt\n");
6099 if (TARGET_GAS)
6101 #if defined(OBJECT_FORMAT_ELF)
6102 /* Generate a special section to describe the ABI switches used to
6103 produce the resultant binary. This used to be done by the assembler
6104 setting bits in the ELF header's flags field, but we have run out of
6105 bits. GDB needs this information in order to be able to correctly
6106 debug these binaries. See the function mips_gdbarch_init() in
6107 gdb/mips-tdep.c. */
6108 const char * abi_string = NULL;
6110 switch (mips_abi)
6112 case ABI_32: abi_string = "abi32"; break;
6113 case ABI_N32: abi_string = "abiN32"; break;
6114 case ABI_64: abi_string = "abi64"; break;
6115 case ABI_O64: abi_string = "abiO64"; break;
6116 case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
6117 case ABI_MEABI:abi_string = TARGET_64BIT ? "meabi64" : "meabi32"; break;
6118 default:
6119 abort ();
6121 /* Note - we use fprintf directly rather than called named_section()
6122 because in this way we can avoid creating an allocated section. We
6123 do not want this section to take up any space in the running
6124 executable. */
6125 fprintf (stream, "\t.section .mdebug.%s\n", abi_string);
6127 /* Restore the default section. */
6128 fprintf (stream, "\t.previous\n");
6129 #endif
6134 /* Generate the pseudo ops that System V.4 wants. */
6135 #ifndef ABICALLS_ASM_OP
6136 #define ABICALLS_ASM_OP "\t.abicalls"
6137 #endif
6138 if (TARGET_ABICALLS)
6139 /* ??? but do not want this (or want pic0) if -non-shared? */
6140 fprintf (stream, "%s\n", ABICALLS_ASM_OP);
6142 if (TARGET_MIPS16)
6143 fprintf (stream, "\t.set\tmips16\n");
6145 /* This code exists so that we can put all externs before all symbol
6146 references. This is necessary for the MIPS assembler's global pointer
6147 optimizations to work. */
6148 if (TARGET_FILE_SWITCHING)
6150 asm_out_data_file = stream;
6151 asm_out_text_file = tmpfile ();
6153 else
6154 asm_out_data_file = asm_out_text_file = stream;
6156 if (flag_verbose_asm)
6157 fprintf (stream, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
6158 ASM_COMMENT_START,
6159 mips_section_threshold, mips_arch_string, mips_isa);
6162 /* If we are optimizing the global pointer, emit the text section now and any
6163 small externs which did not have .comm, etc that are needed. Also, give a
6164 warning if the data area is more than 32K and -pic because 3 instructions
6165 are needed to reference the data pointers. */
6167 void
6168 mips_asm_file_end (file)
6169 FILE *file;
6171 tree name_tree;
6172 struct extern_list *p;
6174 if (HALF_PIC_P ())
6176 HALF_PIC_FINISH (file);
6179 if (extern_head)
6181 fputs ("\n", file);
6183 for (p = extern_head; p != 0; p = p->next)
6185 name_tree = get_identifier (p->name);
6187 /* Positively ensure only one .extern for any given symbol. */
6188 if (! TREE_ASM_WRITTEN (name_tree))
6190 TREE_ASM_WRITTEN (name_tree) = 1;
6191 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6192 if (p->size == -1)
6193 ASM_OUTPUT_UNDEF_FUNCTION (file, p->name);
6194 else
6195 #endif
6197 fputs ("\t.extern\t", file);
6198 assemble_name (file, p->name);
6199 fprintf (file, ", %d\n", p->size);
6205 if (TARGET_FILE_SWITCHING)
6207 fprintf (file, "\n\t.text\n");
6208 copy_file_data (file, asm_out_text_file);
6212 static void
6213 copy_file_data (to, from)
6214 FILE *to, *from;
6216 char buffer[8192];
6217 size_t len;
6218 rewind (from);
6219 if (ferror (from))
6220 fatal_io_error ("can't rewind temp file");
6222 while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
6223 if (fwrite (buffer, 1, len, to) != len)
6224 fatal_io_error ("can't write to output file");
6226 if (ferror (from))
6227 fatal_io_error ("can't read from temp file");
6229 if (fclose (from))
6230 fatal_io_error ("can't close temp file");
6233 /* Emit either a label, .comm, or .lcomm directive, and mark that the symbol
6234 is used, so that we don't emit an .extern for it in mips_asm_file_end. */
6236 void
6237 mips_declare_object (stream, name, init_string, final_string, size)
6238 FILE *stream;
6239 const char *name;
6240 const char *init_string;
6241 const char *final_string;
6242 int size;
6244 fputs (init_string, stream); /* "", "\t.comm\t", or "\t.lcomm\t" */
6245 assemble_name (stream, name);
6246 fprintf (stream, final_string, size); /* ":\n", ",%u\n", ",%u\n" */
6248 if (TARGET_GP_OPT)
6250 tree name_tree = get_identifier (name);
6251 TREE_ASM_WRITTEN (name_tree) = 1;
6255 /* Output a double precision value to the assembler. If both the
6256 host and target are IEEE, emit the values in hex. */
6258 void
6259 mips_output_double (stream, value)
6260 FILE *stream;
6261 REAL_VALUE_TYPE value;
6263 #ifdef REAL_VALUE_TO_TARGET_DOUBLE
6264 long value_long[2];
6265 REAL_VALUE_TO_TARGET_DOUBLE (value, value_long);
6267 fprintf (stream, "\t.word\t0x%08lx\t\t# %.20g\n\t.word\t0x%08lx\n",
6268 value_long[0], value, value_long[1]);
6269 #else
6270 fprintf (stream, "\t.double\t%.20g\n", value);
6271 #endif
6274 /* Output a single precision value to the assembler. If both the
6275 host and target are IEEE, emit the values in hex. */
6277 void
6278 mips_output_float (stream, value)
6279 FILE *stream;
6280 REAL_VALUE_TYPE value;
6282 #ifdef REAL_VALUE_TO_TARGET_SINGLE
6283 long value_long;
6284 REAL_VALUE_TO_TARGET_SINGLE (value, value_long);
6286 fprintf (stream, "\t.word\t0x%08lx\t\t# %.12g (float)\n", value_long, value);
6287 #else
6288 fprintf (stream, "\t.float\t%.12g\n", value);
6289 #endif
6292 /* Return the bytes needed to compute the frame pointer from the current
6293 stack pointer.
6295 Mips stack frames look like:
6297 Before call After call
6298 +-----------------------+ +-----------------------+
6299 high | | | |
6300 mem. | | | |
6301 | caller's temps. | | caller's temps. |
6302 | | | |
6303 +-----------------------+ +-----------------------+
6304 | | | |
6305 | arguments on stack. | | arguments on stack. |
6306 | | | |
6307 +-----------------------+ +-----------------------+
6308 | 4 words to save | | 4 words to save |
6309 | arguments passed | | arguments passed |
6310 | in registers, even | | in registers, even |
6311 SP->| if not passed. | VFP->| if not passed. |
6312 +-----------------------+ +-----------------------+
6314 | fp register save |
6316 +-----------------------+
6318 | gp register save |
6320 +-----------------------+
6322 | local variables |
6324 +-----------------------+
6326 | alloca allocations |
6328 +-----------------------+
6330 | GP save for V.4 abi |
6332 +-----------------------+
6334 | arguments on stack |
6336 +-----------------------+
6337 | 4 words to save |
6338 | arguments passed |
6339 | in registers, even |
6340 low SP->| if not passed. |
6341 memory +-----------------------+
6345 HOST_WIDE_INT
6346 compute_frame_size (size)
6347 HOST_WIDE_INT size; /* # of var. bytes allocated */
6349 unsigned int regno;
6350 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
6351 HOST_WIDE_INT var_size; /* # bytes that variables take up */
6352 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
6353 HOST_WIDE_INT extra_size; /* # extra bytes */
6354 HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6355 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
6356 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
6357 long mask; /* mask of saved gp registers */
6358 long fmask; /* mask of saved fp registers */
6359 int fp_inc; /* 1 or 2 depending on the size of fp regs */
6360 long fp_bits; /* bitmask to use for each fp register */
6362 gp_reg_size = 0;
6363 fp_reg_size = 0;
6364 mask = 0;
6365 fmask = 0;
6366 extra_size = MIPS_STACK_ALIGN (((TARGET_ABICALLS) ? UNITS_PER_WORD : 0));
6367 var_size = MIPS_STACK_ALIGN (size);
6368 args_size = MIPS_STACK_ALIGN (current_function_outgoing_args_size);
6370 /* The MIPS 3.0 linker does not like functions that dynamically
6371 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6372 looks like we are trying to create a second frame pointer to the
6373 function, so allocate some stack space to make it happy. */
6375 if (args_size == 0 && current_function_calls_alloca)
6376 args_size = 4 * UNITS_PER_WORD;
6378 total_size = var_size + args_size + extra_size;
6380 /* Calculate space needed for gp registers. */
6381 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6383 /* $18 is a special case on the mips16. It may be used to call
6384 a function which returns a floating point value, but it is
6385 marked in call_used_regs. $31 is also a special case. When
6386 not using -mentry, it will be used to copy a return value
6387 into the floating point registers if the return value is
6388 floating point. */
6389 if (MUST_SAVE_REGISTER (regno)
6390 || (TARGET_MIPS16
6391 && regno == GP_REG_FIRST + 18
6392 && regs_ever_live[regno])
6393 || (TARGET_MIPS16
6394 && regno == GP_REG_FIRST + 31
6395 && mips16_hard_float
6396 && ! mips_entry
6397 && ! aggregate_value_p (DECL_RESULT (current_function_decl))
6398 && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
6399 == MODE_FLOAT)
6400 && (! TARGET_SINGLE_FLOAT
6401 || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
6402 <= 4))))
6404 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6405 mask |= 1L << (regno - GP_REG_FIRST);
6407 /* The entry and exit pseudo instructions can not save $17
6408 without also saving $16. */
6409 if (mips_entry
6410 && regno == GP_REG_FIRST + 17
6411 && ! MUST_SAVE_REGISTER (GP_REG_FIRST + 16))
6413 gp_reg_size += UNITS_PER_WORD;
6414 mask |= 1L << 16;
6419 /* We need to restore these for the handler. */
6420 if (current_function_calls_eh_return)
6422 unsigned int i;
6423 for (i = 0; ; ++i)
6425 regno = EH_RETURN_DATA_REGNO (i);
6426 if (regno == INVALID_REGNUM)
6427 break;
6428 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6429 mask |= 1L << (regno - GP_REG_FIRST);
6433 /* Calculate space needed for fp registers. */
6434 if (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT)
6436 fp_inc = 1;
6437 fp_bits = 1;
6439 else
6441 fp_inc = 2;
6442 fp_bits = 3;
6445 /* This loop must iterate over the same space as its companion in
6446 save_restore_regs. */
6447 for (regno = (FP_REG_LAST - fp_inc + 1);
6448 regno >= FP_REG_FIRST;
6449 regno -= fp_inc)
6451 if (regs_ever_live[regno] && !call_used_regs[regno])
6453 fp_reg_size += fp_inc * UNITS_PER_FPREG;
6454 fmask |= fp_bits << (regno - FP_REG_FIRST);
6458 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6459 total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
6461 /* The gp reg is caller saved in the 32 bit ABI, so there is no need
6462 for leaf routines (total_size == extra_size) to save the gp reg.
6463 The gp reg is callee saved in the 64 bit ABI, so all routines must
6464 save the gp reg. This is not a leaf routine if -p, because of the
6465 call to mcount. */
6466 if (total_size == extra_size
6467 && (mips_abi == ABI_32 || mips_abi == ABI_O64 || mips_abi == ABI_EABI)
6468 && ! profile_flag)
6469 total_size = extra_size = 0;
6470 else if (TARGET_ABICALLS)
6472 /* Add the context-pointer to the saved registers. */
6473 gp_reg_size += UNITS_PER_WORD;
6474 mask |= 1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST);
6475 total_size -= gp_reg_rounded;
6476 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6477 total_size += gp_reg_rounded;
6480 /* Add in space reserved on the stack by the callee for storing arguments
6481 passed in registers. */
6482 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
6483 total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
6485 /* The entry pseudo instruction will allocate 32 bytes on the stack. */
6486 if (mips_entry && total_size > 0 && total_size < 32)
6487 total_size = 32;
6489 /* Save other computed information. */
6490 current_frame_info.total_size = total_size;
6491 current_frame_info.var_size = var_size;
6492 current_frame_info.args_size = args_size;
6493 current_frame_info.extra_size = extra_size;
6494 current_frame_info.gp_reg_size = gp_reg_size;
6495 current_frame_info.fp_reg_size = fp_reg_size;
6496 current_frame_info.mask = mask;
6497 current_frame_info.fmask = fmask;
6498 current_frame_info.initialized = reload_completed;
6499 current_frame_info.num_gp = gp_reg_size / UNITS_PER_WORD;
6500 current_frame_info.num_fp = fp_reg_size / (fp_inc * UNITS_PER_FPREG);
6502 if (mask)
6504 unsigned long offset;
6506 /* When using mips_entry, the registers are always saved at the
6507 top of the stack. */
6508 if (! mips_entry)
6509 offset = (args_size + extra_size + var_size
6510 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
6511 else
6512 offset = total_size - GET_MODE_SIZE (gpr_mode);
6514 current_frame_info.gp_sp_offset = offset;
6515 current_frame_info.gp_save_offset = offset - total_size;
6517 else
6519 current_frame_info.gp_sp_offset = 0;
6520 current_frame_info.gp_save_offset = 0;
6523 if (fmask)
6525 unsigned long offset = (args_size + extra_size + var_size
6526 + gp_reg_rounded + fp_reg_size
6527 - fp_inc * UNITS_PER_FPREG);
6528 current_frame_info.fp_sp_offset = offset;
6529 current_frame_info.fp_save_offset = offset - total_size;
6531 else
6533 current_frame_info.fp_sp_offset = 0;
6534 current_frame_info.fp_save_offset = 0;
6537 /* Ok, we're done. */
6538 return total_size;
6541 /* Common code to emit the insns (or to write the instructions to a file)
6542 to save/restore registers.
6544 Other parts of the code assume that MIPS_TEMP1_REGNUM (aka large_reg)
6545 is not modified within save_restore_insns. */
6547 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
6549 /* Emit instructions to load the value (SP + OFFSET) into MIPS_TEMP2_REGNUM
6550 and return an rtl expression for the register. Write the assembly
6551 instructions directly to FILE if it is not null, otherwise emit them as
6552 rtl.
6554 This function is a subroutine of save_restore_insns. It is used when
6555 OFFSET is too large to add in a single instruction. */
6557 static rtx
6558 mips_add_large_offset_to_sp (offset, file)
6559 HOST_WIDE_INT offset;
6560 FILE *file;
6562 rtx reg = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6563 if (file == 0)
6565 rtx offset_rtx = GEN_INT (offset);
6567 emit_move_insn (reg, offset_rtx);
6568 if (Pmode == DImode)
6569 emit_insn (gen_adddi3 (reg, reg, stack_pointer_rtx));
6570 else
6571 emit_insn (gen_addsi3 (reg, reg, stack_pointer_rtx));
6573 else
6575 fprintf (file, "\tli\t%s,0x%.08lx\t# ",
6576 reg_names[MIPS_TEMP2_REGNUM], (long) offset);
6577 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
6578 fprintf (file, "\n\t%s\t%s,%s,%s\n",
6579 Pmode == DImode ? "daddu" : "addu",
6580 reg_names[MIPS_TEMP2_REGNUM],
6581 reg_names[MIPS_TEMP2_REGNUM],
6582 reg_names[STACK_POINTER_REGNUM]);
6584 return reg;
6587 /* Make INSN frame related and note that it performs the frame-related
6588 operation DWARF_PATTERN. */
6590 static void
6591 mips_annotate_frame_insn (insn, dwarf_pattern)
6592 rtx insn, dwarf_pattern;
6594 RTX_FRAME_RELATED_P (insn) = 1;
6595 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6596 dwarf_pattern,
6597 REG_NOTES (insn));
6600 /* Return a frame-related rtx that stores register REGNO at (SP + OFFSET).
6601 The expression should only be used to store single registers. */
6603 static rtx
6604 mips_frame_set (mode, regno, offset)
6605 enum machine_mode mode;
6606 int regno;
6607 int offset;
6609 rtx address = plus_constant (stack_pointer_rtx, offset);
6610 rtx set = gen_rtx_SET (mode,
6611 gen_rtx_MEM (mode, address),
6612 gen_rtx_REG (mode, regno));
6613 RTX_FRAME_RELATED_P (set) = 1;
6614 return set;
6618 /* Emit a move instruction that stores REG in MEM. Make the instruction
6619 frame related and note that it stores REG at (SP + OFFSET). This
6620 function may be asked to store an FPR pair. */
6622 static void
6623 mips_emit_frame_related_store (mem, reg, offset)
6624 rtx mem;
6625 rtx reg;
6626 HOST_WIDE_INT offset;
6628 rtx dwarf_expr;
6630 if (GET_MODE (reg) == DFmode && ! TARGET_FLOAT64)
6632 /* Two registers are being stored, so the frame-related expression
6633 must be a PARALLEL rtx with one SET for each register. The
6634 higher numbered register is stored in the lower address on
6635 big-endian targets. */
6636 int regno1 = TARGET_BIG_ENDIAN ? REGNO (reg) + 1 : REGNO (reg);
6637 int regno2 = TARGET_BIG_ENDIAN ? REGNO (reg) : REGNO (reg) + 1;
6638 rtx set1 = mips_frame_set (SFmode, regno1, offset);
6639 rtx set2 = mips_frame_set (SFmode, regno2, offset + UNITS_PER_FPREG);
6640 dwarf_expr = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set1, set2));
6642 else
6643 dwarf_expr = mips_frame_set (GET_MODE (reg), REGNO (reg), offset);
6645 mips_annotate_frame_insn (emit_move_insn (mem, reg), dwarf_expr);
6648 static void
6649 save_restore_insns (store_p, large_reg, large_offset, file)
6650 int store_p; /* true if this is prologue */
6651 rtx large_reg; /* register holding large offset constant or NULL */
6652 long large_offset; /* large constant offset value */
6653 FILE *file; /* file to write instructions instead of making RTL */
6655 long mask = current_frame_info.mask;
6656 long fmask = current_frame_info.fmask;
6657 long real_mask = mask;
6658 int regno;
6659 rtx base_reg_rtx;
6660 HOST_WIDE_INT base_offset;
6661 HOST_WIDE_INT gp_offset;
6662 HOST_WIDE_INT fp_offset;
6663 HOST_WIDE_INT end_offset;
6664 rtx insn;
6666 if (frame_pointer_needed
6667 && ! BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST))
6668 abort ();
6670 /* Do not restore GP under certain conditions. */
6671 if (! store_p
6672 && TARGET_ABICALLS
6673 && (mips_abi == ABI_32 || mips_abi == ABI_O64))
6674 mask &= ~(1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST));
6676 if (mask == 0 && fmask == 0)
6677 return;
6679 /* Save registers starting from high to low. The debuggers prefer at least
6680 the return register be stored at func+4, and also it allows us not to
6681 need a nop in the epilog if at least one register is reloaded in
6682 addition to return address. */
6684 /* Save GP registers if needed. */
6685 if (mask)
6687 /* Pick which pointer to use as a base register. For small frames, just
6688 use the stack pointer. Otherwise, use a temporary register. Save 2
6689 cycles if the save area is near the end of a large frame, by reusing
6690 the constant created in the prologue/epilogue to adjust the stack
6691 frame. */
6693 gp_offset = current_frame_info.gp_sp_offset;
6694 end_offset
6695 = gp_offset - (current_frame_info.gp_reg_size
6696 - GET_MODE_SIZE (gpr_mode));
6698 if (gp_offset < 0 || end_offset < 0)
6699 internal_error
6700 ("gp_offset (%ld) or end_offset (%ld) is less than zero",
6701 (long) gp_offset, (long) end_offset);
6703 /* If we see a large frame in mips16 mode, we save the registers
6704 before adjusting the stack pointer, and load them afterward. */
6705 else if (TARGET_MIPS16 && large_offset > 32767)
6706 base_reg_rtx = stack_pointer_rtx, base_offset = large_offset;
6708 else if (gp_offset < 32768)
6709 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
6711 else if (large_reg != 0
6712 && (unsigned HOST_WIDE_INT) (large_offset - gp_offset) < 32768
6713 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
6715 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6716 base_offset = large_offset;
6717 if (file == 0)
6719 if (Pmode == DImode)
6720 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
6721 stack_pointer_rtx));
6722 else
6723 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
6724 stack_pointer_rtx));
6726 else
6727 fprintf (file, "\t%s\t%s,%s,%s\n",
6728 Pmode == DImode ? "daddu" : "addu",
6729 reg_names[MIPS_TEMP2_REGNUM],
6730 reg_names[REGNO (large_reg)],
6731 reg_names[STACK_POINTER_REGNUM]);
6733 else
6735 base_offset = gp_offset;
6736 base_reg_rtx = mips_add_large_offset_to_sp (base_offset, file);
6739 /* When we restore the registers in MIPS16 mode, then if we are
6740 using a frame pointer, and this is not a large frame, the
6741 current stack pointer will be offset by
6742 current_function_outgoing_args_size. Doing it this way lets
6743 us avoid offsetting the frame pointer before copying it into
6744 the stack pointer; there is no instruction to set the stack
6745 pointer to the sum of a register and a constant. */
6746 if (TARGET_MIPS16
6747 && ! store_p
6748 && frame_pointer_needed
6749 && large_offset <= 32767)
6750 base_offset += current_function_outgoing_args_size;
6752 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
6753 if (BITSET_P (mask, regno - GP_REG_FIRST))
6755 if (file == 0)
6757 rtx reg_rtx;
6758 rtx mem_rtx
6759 = gen_rtx (MEM, gpr_mode,
6760 gen_rtx (PLUS, Pmode, base_reg_rtx,
6761 GEN_INT (gp_offset - base_offset)));
6763 if (! current_function_calls_eh_return)
6764 RTX_UNCHANGING_P (mem_rtx) = 1;
6766 /* The mips16 does not have an instruction to load
6767 $31, so we load $7 instead, and work things out
6768 in the caller. */
6769 if (TARGET_MIPS16 && ! store_p && regno == GP_REG_FIRST + 31)
6770 reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 7);
6771 /* The mips16 sometimes needs to save $18. */
6772 else if (TARGET_MIPS16
6773 && regno != GP_REG_FIRST + 31
6774 && ! M16_REG_P (regno))
6776 if (! store_p)
6777 reg_rtx = gen_rtx (REG, gpr_mode, 6);
6778 else
6780 reg_rtx = gen_rtx (REG, gpr_mode, 3);
6781 emit_move_insn (reg_rtx,
6782 gen_rtx (REG, gpr_mode, regno));
6785 else
6786 reg_rtx = gen_rtx (REG, gpr_mode, regno);
6788 if (store_p)
6789 mips_emit_frame_related_store (mem_rtx, reg_rtx, gp_offset);
6790 else
6792 emit_move_insn (reg_rtx, mem_rtx);
6793 if (TARGET_MIPS16
6794 && regno != GP_REG_FIRST + 31
6795 && ! M16_REG_P (regno))
6796 emit_move_insn (gen_rtx (REG, gpr_mode, regno),
6797 reg_rtx);
6800 else
6802 int r = regno;
6804 /* The mips16 does not have an instruction to
6805 load $31, so we load $7 instead, and work
6806 things out in the caller. */
6807 if (TARGET_MIPS16 && ! store_p && r == GP_REG_FIRST + 31)
6808 r = GP_REG_FIRST + 7;
6809 /* The mips16 sometimes needs to save $18. */
6810 if (TARGET_MIPS16
6811 && regno != GP_REG_FIRST + 31
6812 && ! M16_REG_P (regno))
6814 if (! store_p)
6815 r = GP_REG_FIRST + 6;
6816 else
6818 r = GP_REG_FIRST + 3;
6819 fprintf (file, "\tmove\t%s,%s\n",
6820 reg_names[r], reg_names[regno]);
6823 fprintf (file, "\t%s\t%s,",
6824 (TARGET_64BIT
6825 ? (store_p) ? "sd" : "ld"
6826 : (store_p) ? "sw" : "lw"),
6827 reg_names[r]);
6828 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
6829 gp_offset - base_offset);
6830 fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
6831 if (! store_p
6832 && TARGET_MIPS16
6833 && regno != GP_REG_FIRST + 31
6834 && ! M16_REG_P (regno))
6835 fprintf (file, "\tmove\t%s,%s\n",
6836 reg_names[regno], reg_names[r]);
6838 gp_offset -= GET_MODE_SIZE (gpr_mode);
6840 /* If the restore is being supressed, still take into account
6841 the offset at which it is stored. */
6842 else if (BITSET_P (real_mask, regno - GP_REG_FIRST))
6844 gp_offset -= GET_MODE_SIZE (gpr_mode);
6847 else
6848 base_reg_rtx = 0, base_offset = 0;
6850 /* Save floating point registers if needed. */
6851 if (fmask)
6853 int fp_inc = (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT) ? 1 : 2;
6854 int fp_size = fp_inc * UNITS_PER_FPREG;
6856 /* Pick which pointer to use as a base register. */
6857 fp_offset = current_frame_info.fp_sp_offset;
6858 end_offset = fp_offset - (current_frame_info.fp_reg_size - fp_size);
6860 if (fp_offset < 0 || end_offset < 0)
6861 internal_error
6862 ("fp_offset (%ld) or end_offset (%ld) is less than zero",
6863 (long) fp_offset, (long) end_offset);
6865 else if (fp_offset < 32768)
6866 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
6868 else if (base_reg_rtx != 0
6869 && (unsigned HOST_WIDE_INT) (base_offset - fp_offset) < 32768
6870 && (unsigned HOST_WIDE_INT) (base_offset - end_offset) < 32768)
6871 ; /* already set up for gp registers above */
6873 else if (large_reg != 0
6874 && (unsigned HOST_WIDE_INT) (large_offset - fp_offset) < 32768
6875 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
6877 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6878 base_offset = large_offset;
6879 if (file == 0)
6881 if (Pmode == DImode)
6882 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
6883 stack_pointer_rtx));
6884 else
6885 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
6886 stack_pointer_rtx));
6889 else
6890 fprintf (file, "\t%s\t%s,%s,%s\n",
6891 Pmode == DImode ? "daddu" : "addu",
6892 reg_names[MIPS_TEMP2_REGNUM],
6893 reg_names[REGNO (large_reg)],
6894 reg_names[STACK_POINTER_REGNUM]);
6896 else
6898 base_offset = fp_offset;
6899 base_reg_rtx = mips_add_large_offset_to_sp (fp_offset, file);
6902 /* This loop must iterate over the same space as its companion in
6903 compute_frame_size. */
6904 for (regno = (FP_REG_LAST - fp_inc + 1);
6905 regno >= FP_REG_FIRST;
6906 regno -= fp_inc)
6907 if (BITSET_P (fmask, regno - FP_REG_FIRST))
6909 if (file == 0)
6911 enum machine_mode sz
6912 = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
6913 rtx reg_rtx = gen_rtx (REG, sz, regno);
6914 rtx mem_rtx = gen_rtx (MEM, sz,
6915 gen_rtx (PLUS, Pmode, base_reg_rtx,
6916 GEN_INT (fp_offset
6917 - base_offset)));
6918 if (! current_function_calls_eh_return)
6919 RTX_UNCHANGING_P (mem_rtx) = 1;
6921 if (store_p)
6922 mips_emit_frame_related_store (mem_rtx, reg_rtx, fp_offset);
6923 else
6924 emit_move_insn (reg_rtx, mem_rtx);
6926 else
6928 fprintf (file, "\t%s\t%s,",
6929 (TARGET_SINGLE_FLOAT
6930 ? (store_p ? "s.s" : "l.s")
6931 : (store_p ? "s.d" : "l.d")),
6932 reg_names[regno]);
6933 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
6934 fp_offset - base_offset);
6935 fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
6938 fp_offset -= fp_size;
6943 /* Set up the stack and frame (if desired) for the function. */
6945 static void
6946 mips_output_function_prologue (file, size)
6947 FILE *file;
6948 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
6950 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6951 const char *fnname;
6952 #endif
6953 HOST_WIDE_INT tsize = current_frame_info.total_size;
6955 ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
6957 #ifdef SDB_DEBUGGING_INFO
6958 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6959 ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
6960 #endif
6962 /* In mips16 mode, we may need to generate a 32 bit to handle
6963 floating point arguments. The linker will arrange for any 32 bit
6964 functions to call this stub, which will then jump to the 16 bit
6965 function proper. */
6966 if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6967 && current_function_args_info.fp_code != 0)
6968 build_mips16_function_stub (file);
6970 inside_function = 1;
6972 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6973 /* Get the function name the same way that toplev.c does before calling
6974 assemble_start_function. This is needed so that the name used here
6975 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6976 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6978 if (!flag_inhibit_size_directive)
6980 fputs ("\t.ent\t", file);
6981 assemble_name (file, fnname);
6982 fputs ("\n", file);
6985 assemble_name (file, fnname);
6986 fputs (":\n", file);
6987 #endif
6989 if (!flag_inhibit_size_directive)
6991 /* .frame FRAMEREG, FRAMESIZE, RETREG */
6992 fprintf (file,
6993 "\t.frame\t%s,%ld,%s\t\t# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
6994 (reg_names[(frame_pointer_needed)
6995 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
6996 ((frame_pointer_needed && TARGET_MIPS16)
6997 ? ((long) tsize - current_function_outgoing_args_size)
6998 : (long) tsize),
6999 reg_names[GP_REG_FIRST + 31],
7000 current_frame_info.var_size,
7001 current_frame_info.num_gp,
7002 current_frame_info.num_fp,
7003 current_function_outgoing_args_size,
7004 current_frame_info.extra_size);
7006 /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
7007 fprintf (file, "\t.mask\t0x%08lx,%ld\n\t.fmask\t0x%08lx,%ld\n",
7008 current_frame_info.mask,
7009 current_frame_info.gp_save_offset,
7010 current_frame_info.fmask,
7011 current_frame_info.fp_save_offset);
7013 /* Require:
7014 OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
7015 HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
7018 if (mips_entry && ! mips_can_use_return_insn ())
7020 int save16 = BITSET_P (current_frame_info.mask, 16);
7021 int save17 = BITSET_P (current_frame_info.mask, 17);
7022 int save31 = BITSET_P (current_frame_info.mask, 31);
7023 int savearg = 0;
7024 rtx insn;
7026 /* Look through the initial insns to see if any of them store
7027 the function parameters into the incoming parameter storage
7028 area. If they do, we delete the insn, and save the register
7029 using the entry pseudo-instruction instead. We don't try to
7030 look past a label, jump, or call. */
7031 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
7033 rtx note, set, src, dest, base, offset;
7034 int hireg;
7036 if (GET_CODE (insn) == CODE_LABEL
7037 || GET_CODE (insn) == JUMP_INSN
7038 || GET_CODE (insn) == CALL_INSN)
7039 break;
7040 if (GET_CODE (insn) != INSN)
7041 continue;
7042 set = PATTERN (insn);
7043 if (GET_CODE (set) != SET)
7044 continue;
7046 /* An insn storing a function parameter will still have a
7047 REG_EQUIV note on it mentioning the argument pointer. */
7048 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
7049 if (note == NULL_RTX)
7050 continue;
7051 if (! reg_mentioned_p (arg_pointer_rtx, XEXP (note, 0)))
7052 continue;
7054 src = SET_SRC (set);
7055 if (GET_CODE (src) != REG
7056 || REGNO (src) < GP_REG_FIRST + 4
7057 || REGNO (src) > GP_REG_FIRST + 7)
7058 continue;
7060 dest = SET_DEST (set);
7061 if (GET_CODE (dest) != MEM)
7062 continue;
7063 if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned) UNITS_PER_WORD)
7065 else if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned)2 * UNITS_PER_WORD
7066 && REGNO (src) < GP_REG_FIRST + 7)
7068 else
7069 continue;
7070 offset = const0_rtx;
7071 base = eliminate_constant_term (XEXP (dest, 0), &offset);
7072 if (GET_CODE (base) != REG
7073 || GET_CODE (offset) != CONST_INT)
7074 continue;
7075 if (REGNO (base) == (unsigned) STACK_POINTER_REGNUM
7076 && INTVAL (offset) == tsize + (REGNO (src) - 4) * UNITS_PER_WORD)
7078 else if (REGNO (base) == (unsigned) HARD_FRAME_POINTER_REGNUM
7079 && (INTVAL (offset)
7080 == (tsize
7081 + (REGNO (src) - 4) * UNITS_PER_WORD
7082 - current_function_outgoing_args_size)))
7084 else
7085 continue;
7087 /* This insn stores a parameter onto the stack, in the same
7088 location where the entry pseudo-instruction will put it.
7089 Delete the insn, and arrange to tell the entry
7090 instruction to save the register. */
7091 PUT_CODE (insn, NOTE);
7092 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7093 NOTE_SOURCE_FILE (insn) = 0;
7095 hireg = (REGNO (src)
7096 + HARD_REGNO_NREGS (REGNO (src), GET_MODE (dest))
7097 - 1);
7098 if (hireg > savearg)
7099 savearg = hireg;
7102 /* If this is a varargs function, we need to save all the
7103 registers onto the stack anyhow. */
7104 if (current_function_stdarg || current_function_varargs)
7105 savearg = GP_REG_FIRST + 7;
7107 fprintf (file, "\tentry\t");
7108 if (savearg > 0)
7110 if (savearg == GP_REG_FIRST + 4)
7111 fprintf (file, "%s", reg_names[savearg]);
7112 else
7113 fprintf (file, "%s-%s", reg_names[GP_REG_FIRST + 4],
7114 reg_names[savearg]);
7116 if (save16 || save17)
7118 if (savearg > 0)
7119 fprintf (file, ",");
7120 fprintf (file, "%s", reg_names[GP_REG_FIRST + 16]);
7121 if (save17)
7122 fprintf (file, "-%s", reg_names[GP_REG_FIRST + 17]);
7124 if (save31)
7126 if (savearg > 0 || save16 || save17)
7127 fprintf (file, ",");
7128 fprintf (file, "%s", reg_names[GP_REG_FIRST + 31]);
7130 fprintf (file, "\n");
7133 if (TARGET_ABICALLS && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7135 const char *const sp_str = reg_names[STACK_POINTER_REGNUM];
7137 fprintf (file, "\t.set\tnoreorder\n\t.cpload\t%s\n\t.set\treorder\n",
7138 reg_names[PIC_FUNCTION_ADDR_REGNUM]);
7139 if (tsize > 0)
7141 fprintf (file, "\t%s\t%s,%s,%ld\n",
7142 (Pmode == DImode ? "dsubu" : "subu"),
7143 sp_str, sp_str, (long) tsize);
7144 fprintf (file, "\t.cprestore %ld\n", current_frame_info.args_size);
7147 if (dwarf2out_do_frame ())
7148 dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, tsize);
7152 /* Expand the prologue into a bunch of separate insns. */
7154 void
7155 mips_expand_prologue ()
7157 int regno;
7158 HOST_WIDE_INT tsize;
7159 rtx tmp_rtx = 0;
7160 int last_arg_is_vararg_marker = 0;
7161 tree fndecl = current_function_decl;
7162 tree fntype = TREE_TYPE (fndecl);
7163 tree fnargs = DECL_ARGUMENTS (fndecl);
7164 rtx next_arg_reg;
7165 int i;
7166 tree next_arg;
7167 tree cur_arg;
7168 CUMULATIVE_ARGS args_so_far;
7169 rtx reg_18_save = NULL_RTX;
7170 int store_args_on_stack = (mips_abi == ABI_32 || mips_abi == ABI_O64)
7171 && (! mips_entry || mips_can_use_return_insn ());
7173 /* If struct value address is treated as the first argument, make it so. */
7174 if (aggregate_value_p (DECL_RESULT (fndecl))
7175 && ! current_function_returns_pcc_struct
7176 && struct_value_incoming_rtx == 0)
7178 tree type = build_pointer_type (fntype);
7179 tree function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
7181 DECL_ARG_TYPE (function_result_decl) = type;
7182 TREE_CHAIN (function_result_decl) = fnargs;
7183 fnargs = function_result_decl;
7186 /* For arguments passed in registers, find the register number
7187 of the first argument in the variable part of the argument list,
7188 otherwise GP_ARG_LAST+1. Note also if the last argument is
7189 the varargs special argument, and treat it as part of the
7190 variable arguments.
7192 This is only needed if store_args_on_stack is true. */
7194 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
7195 regno = GP_ARG_FIRST;
7197 for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
7199 tree passed_type = DECL_ARG_TYPE (cur_arg);
7200 enum machine_mode passed_mode = TYPE_MODE (passed_type);
7201 rtx entry_parm;
7203 if (TREE_ADDRESSABLE (passed_type))
7205 passed_type = build_pointer_type (passed_type);
7206 passed_mode = Pmode;
7209 entry_parm = FUNCTION_ARG (args_so_far, passed_mode, passed_type, 1);
7211 FUNCTION_ARG_ADVANCE (args_so_far, passed_mode, passed_type, 1);
7212 next_arg = TREE_CHAIN (cur_arg);
7214 if (entry_parm && store_args_on_stack)
7216 if (next_arg == 0
7217 && DECL_NAME (cur_arg)
7218 && ((0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7219 "__builtin_va_alist"))
7220 || (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7221 "va_alist"))))
7223 last_arg_is_vararg_marker = 1;
7224 break;
7226 else
7228 int words;
7230 if (GET_CODE (entry_parm) != REG)
7231 abort ();
7233 /* passed in a register, so will get homed automatically */
7234 if (GET_MODE (entry_parm) == BLKmode)
7235 words = (int_size_in_bytes (passed_type) + 3) / 4;
7236 else
7237 words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
7239 regno = REGNO (entry_parm) + words - 1;
7242 else
7244 regno = GP_ARG_LAST+1;
7245 break;
7249 /* In order to pass small structures by value in registers compatibly with
7250 the MIPS compiler, we need to shift the value into the high part of the
7251 register. Function_arg has encoded a PARALLEL rtx, holding a vector of
7252 adjustments to be made as the next_arg_reg variable, so we split up the
7253 insns, and emit them separately. */
7255 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1);
7256 if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
7258 rtvec adjust = XVEC (next_arg_reg, 0);
7259 int num = GET_NUM_ELEM (adjust);
7261 for (i = 0; i < num; i++)
7263 rtx insn, pattern;
7265 pattern = RTVEC_ELT (adjust, i);
7266 if (GET_CODE (pattern) != SET
7267 || GET_CODE (SET_SRC (pattern)) != ASHIFT)
7268 abort_with_insn (pattern, "insn is not a shift");
7269 PUT_CODE (SET_SRC (pattern), ASHIFTRT);
7271 insn = emit_insn (pattern);
7273 /* Global life information isn't valid at this point, so we
7274 can't check whether these shifts are actually used. Mark
7275 them MAYBE_DEAD so that flow2 will remove them, and not
7276 complain about dead code in the prologue. */
7277 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
7278 REG_NOTES (insn));
7282 tsize = compute_frame_size (get_frame_size ());
7284 /* If this function is a varargs function, store any registers that
7285 would normally hold arguments ($4 - $7) on the stack. */
7286 if (store_args_on_stack
7287 && ((TYPE_ARG_TYPES (fntype) != 0
7288 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
7289 != void_type_node))
7290 || last_arg_is_vararg_marker))
7292 int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
7293 rtx ptr = stack_pointer_rtx;
7295 /* If we are doing svr4-abi, sp has already been decremented by tsize. */
7296 if (TARGET_ABICALLS)
7297 offset += tsize;
7299 for (; regno <= GP_ARG_LAST; regno++)
7301 if (offset != 0)
7302 ptr = gen_rtx (PLUS, Pmode, stack_pointer_rtx, GEN_INT (offset));
7303 emit_move_insn (gen_rtx (MEM, gpr_mode, ptr),
7304 gen_rtx (REG, gpr_mode, regno));
7306 offset += GET_MODE_SIZE (gpr_mode);
7310 /* If we are using the entry pseudo instruction, it will
7311 automatically subtract 32 from the stack pointer, so we don't
7312 need to. The entry pseudo instruction is emitted by
7313 function_prologue. */
7314 if (mips_entry && ! mips_can_use_return_insn ())
7316 if (tsize > 0 && tsize <= 32 && frame_pointer_needed)
7318 rtx insn;
7320 /* If we are using a frame pointer with a small stack frame,
7321 we need to initialize it here since it won't be done
7322 below. */
7323 if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7325 rtx incr = GEN_INT (current_function_outgoing_args_size);
7326 if (Pmode == DImode)
7327 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7328 stack_pointer_rtx,
7329 incr));
7330 else
7331 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7332 stack_pointer_rtx,
7333 incr));
7335 else if (Pmode == DImode)
7336 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7337 stack_pointer_rtx));
7338 else
7339 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7340 stack_pointer_rtx));
7342 RTX_FRAME_RELATED_P (insn) = 1;
7345 /* We may need to save $18, if it is used to call a function
7346 which may return a floating point value. Set up a sequence
7347 of instructions to do so. Later on we emit them at the right
7348 moment. */
7349 if (TARGET_MIPS16 && BITSET_P (current_frame_info.mask, 18))
7351 rtx reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 3);
7352 long gp_offset, base_offset;
7354 gp_offset = current_frame_info.gp_sp_offset;
7355 if (BITSET_P (current_frame_info.mask, 16))
7356 gp_offset -= UNITS_PER_WORD;
7357 if (BITSET_P (current_frame_info.mask, 17))
7358 gp_offset -= UNITS_PER_WORD;
7359 if (BITSET_P (current_frame_info.mask, 31))
7360 gp_offset -= UNITS_PER_WORD;
7361 if (tsize > 32767)
7362 base_offset = tsize;
7363 else
7364 base_offset = 0;
7365 start_sequence ();
7366 emit_move_insn (reg_rtx,
7367 gen_rtx (REG, gpr_mode, GP_REG_FIRST + 18));
7368 emit_move_insn (gen_rtx (MEM, gpr_mode,
7369 gen_rtx (PLUS, Pmode, stack_pointer_rtx,
7370 GEN_INT (gp_offset
7371 - base_offset))),
7372 reg_rtx);
7373 reg_18_save = gen_sequence ();
7374 end_sequence ();
7377 if (tsize > 32)
7378 tsize -= 32;
7379 else
7381 tsize = 0;
7382 if (reg_18_save != NULL_RTX)
7383 emit_insn (reg_18_save);
7387 if (tsize > 0)
7389 rtx tsize_rtx = GEN_INT (tsize);
7391 /* If we are doing svr4-abi, sp move is done by
7392 function_prologue. In mips16 mode with a large frame, we
7393 save the registers before adjusting the stack. */
7394 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7395 && (!TARGET_MIPS16 || tsize <= 32767))
7397 rtx adjustment_rtx, insn, dwarf_pattern;
7399 if (tsize > 32767)
7401 adjustment_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
7402 emit_move_insn (adjustment_rtx, tsize_rtx);
7404 else
7405 adjustment_rtx = tsize_rtx;
7407 if (Pmode == DImode)
7408 insn = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
7409 adjustment_rtx));
7410 else
7411 insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
7412 adjustment_rtx));
7414 dwarf_pattern = gen_rtx_SET (Pmode, stack_pointer_rtx,
7415 plus_constant (stack_pointer_rtx,
7416 -tsize));
7418 mips_annotate_frame_insn (insn, dwarf_pattern);
7421 if (! mips_entry)
7422 save_restore_insns (1, tmp_rtx, tsize, (FILE *)0);
7423 else if (reg_18_save != NULL_RTX)
7424 emit_insn (reg_18_save);
7426 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7427 && TARGET_MIPS16
7428 && tsize > 32767)
7430 rtx reg_rtx;
7432 if (!frame_pointer_needed)
7433 abort ();
7435 reg_rtx = gen_rtx (REG, Pmode, 3);
7436 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
7437 emit_move_insn (reg_rtx, tsize_rtx);
7438 if (Pmode == DImode)
7439 emit_insn (gen_subdi3 (hard_frame_pointer_rtx,
7440 hard_frame_pointer_rtx,
7441 reg_rtx));
7442 else
7443 emit_insn (gen_subsi3 (hard_frame_pointer_rtx,
7444 hard_frame_pointer_rtx,
7445 reg_rtx));
7446 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
7449 if (frame_pointer_needed)
7451 rtx insn = 0;
7453 /* On the mips16, we encourage the use of unextended
7454 instructions when using the frame pointer by pointing the
7455 frame pointer ahead of the argument space allocated on
7456 the stack. */
7457 if ((! TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7458 && TARGET_MIPS16
7459 && tsize > 32767)
7461 /* In this case, we have already copied the stack
7462 pointer into the frame pointer, above. We need only
7463 adjust for the outgoing argument size. */
7464 if (current_function_outgoing_args_size != 0)
7466 rtx incr = GEN_INT (current_function_outgoing_args_size);
7467 if (Pmode == DImode)
7468 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7469 hard_frame_pointer_rtx,
7470 incr));
7471 else
7472 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7473 hard_frame_pointer_rtx,
7474 incr));
7477 else if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7479 rtx incr = GEN_INT (current_function_outgoing_args_size);
7480 if (Pmode == DImode)
7481 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7482 stack_pointer_rtx,
7483 incr));
7484 else
7485 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7486 stack_pointer_rtx,
7487 incr));
7489 else if (Pmode == DImode)
7490 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7491 stack_pointer_rtx));
7492 else
7493 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7494 stack_pointer_rtx));
7496 if (insn)
7497 RTX_FRAME_RELATED_P (insn) = 1;
7500 if (TARGET_ABICALLS && (mips_abi != ABI_32 && mips_abi != ABI_O64))
7501 emit_insn (gen_loadgp (XEXP (DECL_RTL (current_function_decl), 0),
7502 gen_rtx_REG (DImode, 25)));
7505 /* If we are profiling, make sure no instructions are scheduled before
7506 the call to mcount. */
7508 if (profile_flag || profile_block_flag)
7509 emit_insn (gen_blockage ());
7512 /* Do any necessary cleanup after a function to restore stack, frame,
7513 and regs. */
7515 #define RA_MASK BITMASK_HIGH /* 1 << 31 */
7516 #define PIC_OFFSET_TABLE_MASK (1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
7518 static void
7519 mips_output_function_epilogue (file, size)
7520 FILE *file ATTRIBUTE_UNUSED;
7521 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
7523 const char *fnname = ""; /* FIXME: Correct initialisation? */
7525 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7526 /* Get the function name the same way that toplev.c does before calling
7527 assemble_start_function. This is needed so that the name used here
7528 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
7529 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7531 if (!flag_inhibit_size_directive)
7533 fputs ("\t.end\t", file);
7534 assemble_name (file, fnname);
7535 fputs ("\n", file);
7537 #endif
7539 if (TARGET_STATS)
7541 int num_gp_regs = current_frame_info.gp_reg_size / 4;
7542 int num_fp_regs = current_frame_info.fp_reg_size / 8;
7543 int num_regs = num_gp_regs + num_fp_regs;
7544 const char *name = fnname;
7546 if (name[0] == '*')
7547 name++;
7549 dslots_load_total += num_regs;
7551 fprintf (stderr,
7552 "%-20s fp=%c leaf=%c alloca=%c setjmp=%c stack=%4ld arg=%3d reg=%2d/%d delay=%3d/%3dL %3d/%3dJ refs=%3d/%3d/%3d",
7553 name, frame_pointer_needed ? 'y' : 'n',
7554 (current_frame_info.mask & RA_MASK) != 0 ? 'n' : 'y',
7555 current_function_calls_alloca ? 'y' : 'n',
7556 current_function_calls_setjmp ? 'y' : 'n',
7557 current_frame_info.total_size,
7558 current_function_outgoing_args_size, num_gp_regs, num_fp_regs,
7559 dslots_load_total, dslots_load_filled,
7560 dslots_jump_total, dslots_jump_filled,
7561 num_refs[0], num_refs[1], num_refs[2]);
7563 if (HALF_PIC_NUMBER_PTRS > prev_half_pic_ptrs)
7565 fprintf (stderr,
7566 " half-pic=%3d", HALF_PIC_NUMBER_PTRS - prev_half_pic_ptrs);
7567 prev_half_pic_ptrs = HALF_PIC_NUMBER_PTRS;
7570 if (HALF_PIC_NUMBER_REFS > prev_half_pic_refs)
7572 fprintf (stderr,
7573 " pic-ref=%3d", HALF_PIC_NUMBER_REFS - prev_half_pic_refs);
7574 prev_half_pic_refs = HALF_PIC_NUMBER_REFS;
7577 fputc ('\n', stderr);
7580 /* Reset state info for each function. */
7581 inside_function = 0;
7582 ignore_line_number = 0;
7583 dslots_load_total = 0;
7584 dslots_jump_total = 0;
7585 dslots_load_filled = 0;
7586 dslots_jump_filled = 0;
7587 num_refs[0] = 0;
7588 num_refs[1] = 0;
7589 num_refs[2] = 0;
7590 mips_load_reg = 0;
7591 mips_load_reg2 = 0;
7592 current_frame_info = zero_frame_info;
7594 while (string_constants != NULL)
7596 struct string_constant *next;
7598 next = string_constants->next;
7599 free (string_constants);
7600 string_constants = next;
7603 /* Restore the output file if optimizing the GP (optimizing the GP causes
7604 the text to be diverted to a tempfile, so that data decls come before
7605 references to the data). */
7606 if (TARGET_FILE_SWITCHING)
7608 asm_out_file = asm_out_data_file;
7609 data_section ();
7613 /* Expand the epilogue into a bunch of separate insns. */
7615 void
7616 mips_expand_epilogue ()
7618 HOST_WIDE_INT tsize = current_frame_info.total_size;
7619 rtx tsize_rtx = GEN_INT (tsize);
7620 rtx tmp_rtx = (rtx)0;
7622 if (mips_can_use_return_insn ())
7624 emit_insn (gen_return ());
7625 return;
7628 if (mips_entry && ! mips_can_use_return_insn ())
7629 tsize -= 32;
7631 if (tsize > 32767 && ! TARGET_MIPS16)
7633 tmp_rtx = gen_rtx_REG (Pmode, MIPS_TEMP1_REGNUM);
7634 emit_move_insn (tmp_rtx, tsize_rtx);
7635 tsize_rtx = tmp_rtx;
7638 if (tsize > 0)
7640 long orig_tsize = tsize;
7642 if (frame_pointer_needed)
7644 emit_insn (gen_blockage ());
7646 /* On the mips16, the frame pointer is offset from the stack
7647 pointer by current_function_outgoing_args_size. We
7648 account for that by changing tsize. Note that this can
7649 actually make tsize negative. */
7650 if (TARGET_MIPS16)
7652 tsize -= current_function_outgoing_args_size;
7654 /* If we have a large frame, it's easier to add to $17
7655 than to $sp, since the mips16 has no instruction to
7656 add a register to $sp. */
7657 if (orig_tsize > 32767)
7659 rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
7661 emit_move_insn (g6_rtx, GEN_INT (tsize));
7662 if (Pmode == DImode)
7663 emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7664 hard_frame_pointer_rtx,
7665 g6_rtx));
7666 else
7667 emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7668 hard_frame_pointer_rtx,
7669 g6_rtx));
7670 tsize = 0;
7673 if (tsize && tsize != orig_tsize)
7674 tsize_rtx = GEN_INT (tsize);
7677 if (Pmode == DImode)
7678 emit_insn (gen_movdi (stack_pointer_rtx, hard_frame_pointer_rtx));
7679 else
7680 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
7683 /* The GP/PIC register is implicitly used by all SYMBOL_REFs, so if we
7684 are going to restore it, then we must emit a blockage insn to
7685 prevent the scheduler from moving the restore out of the epilogue. */
7686 else if (TARGET_ABICALLS && mips_abi != ABI_32 && mips_abi != ABI_O64
7687 && (current_frame_info.mask
7688 & (1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))))
7689 emit_insn (gen_blockage ());
7691 save_restore_insns (0, tmp_rtx, orig_tsize, (FILE *)0);
7693 /* In mips16 mode with a large frame, we adjust the stack
7694 pointer before restoring the registers. In this case, we
7695 should always be using a frame pointer, so everything should
7696 have been handled above. */
7697 if (tsize > 32767 && TARGET_MIPS16)
7698 abort ();
7700 if (current_function_calls_eh_return)
7702 rtx eh_ofs = EH_RETURN_STACKADJ_RTX;
7703 if (Pmode == DImode)
7704 emit_insn (gen_adddi3 (eh_ofs, eh_ofs, tsize_rtx));
7705 else
7706 emit_insn (gen_addsi3 (eh_ofs, eh_ofs, tsize_rtx));
7707 tsize_rtx = eh_ofs;
7710 emit_insn (gen_blockage ());
7712 if (tsize != 0 || current_function_calls_eh_return)
7714 if (Pmode == DImode)
7715 emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
7716 tsize_rtx));
7717 else
7718 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
7719 tsize_rtx));
7723 /* The mips16 loads the return address into $7, not $31. */
7724 if (TARGET_MIPS16 && (current_frame_info.mask & RA_MASK) != 0)
7725 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
7726 GP_REG_FIRST + 7)));
7727 else
7728 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
7729 GP_REG_FIRST + 31)));
7732 /* Return nonzero if this function is known to have a null epilogue.
7733 This allows the optimizer to omit jumps to jumps if no stack
7734 was created. */
7737 mips_can_use_return_insn ()
7739 if (! reload_completed)
7740 return 0;
7742 if (regs_ever_live[31] || profile_flag)
7743 return 0;
7745 /* In mips16 mode, a function which returns a floating point value
7746 needs to arrange to copy the return value into the floating point
7747 registers. */
7748 if (TARGET_MIPS16
7749 && mips16_hard_float
7750 && ! aggregate_value_p (DECL_RESULT (current_function_decl))
7751 && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
7752 == MODE_FLOAT)
7753 && (! TARGET_SINGLE_FLOAT
7754 || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
7755 <= 4)))
7756 return 0;
7758 if (current_frame_info.initialized)
7759 return current_frame_info.total_size == 0;
7761 return compute_frame_size (get_frame_size ()) == 0;
7764 /* Returns non-zero if X contains a SYMBOL_REF. */
7766 static int
7767 symbolic_expression_p (x)
7768 rtx x;
7770 if (GET_CODE (x) == SYMBOL_REF)
7771 return 1;
7773 if (GET_CODE (x) == CONST)
7774 return symbolic_expression_p (XEXP (x, 0));
7776 if (GET_RTX_CLASS (GET_CODE (x)) == '1')
7777 return symbolic_expression_p (XEXP (x, 0));
7779 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
7780 || GET_RTX_CLASS (GET_CODE (x)) == '2')
7781 return (symbolic_expression_p (XEXP (x, 0))
7782 || symbolic_expression_p (XEXP (x, 1)));
7784 return 0;
7787 /* Choose the section to use for the constant rtx expression X that has
7788 mode MODE. */
7790 void
7791 mips_select_rtx_section (mode, x)
7792 enum machine_mode mode;
7793 rtx x ATTRIBUTE_UNUSED;
7795 if (TARGET_MIPS16)
7797 /* In mips16 mode, the constant table always goes in the same section
7798 as the function, so that constants can be loaded using PC relative
7799 addressing. */
7800 function_section (current_function_decl);
7802 else if (TARGET_EMBEDDED_DATA)
7804 /* For embedded applications, always put constants in read-only data,
7805 in order to reduce RAM usage. */
7806 READONLY_DATA_SECTION ();
7808 else
7810 /* For hosted applications, always put constants in small data if
7811 possible, as this gives the best performance. */
7813 if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
7814 && mips_section_threshold > 0)
7815 SMALL_DATA_SECTION ();
7816 else if (flag_pic && symbolic_expression_p (x))
7817 /* Any expression involving a SYMBOL_REF might need a run-time
7818 relocation. (The symbol might be defined in a shared
7819 library loaded at an unexpected base address.) So, we must
7820 put such expressions in the data segment (which is
7821 writable), rather than the text segment (which is
7822 read-only). */
7823 data_section ();
7824 else
7825 READONLY_DATA_SECTION ();
7829 /* Choose the section to use for DECL. RELOC is true if its value contains
7830 any relocatable expression.
7832 Some of the logic used here needs to be replicated in
7833 ENCODE_SECTION_INFO in mips.h so that references to these symbols
7834 are done correctly. Specifically, at least all symbols assigned
7835 here to rom (.text and/or .rodata) must not be referenced via
7836 ENCODE_SECTION_INFO with %gprel, as the rom might be too far away.
7838 If you need to make a change here, you probably should check
7839 ENCODE_SECTION_INFO to see if it needs a similar change. */
7841 void
7842 mips_select_section (decl, reloc)
7843 tree decl;
7844 int reloc;
7846 int size = int_size_in_bytes (TREE_TYPE (decl));
7848 if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
7849 && TREE_CODE (decl) == STRING_CST
7850 && !flag_writable_strings)
7851 /* For embedded position independent code, put constant strings in the
7852 text section, because the data section is limited to 64K in size.
7853 For mips16 code, put strings in the text section so that a PC
7854 relative load instruction can be used to get their address. */
7855 text_section ();
7856 else if (TARGET_EMBEDDED_DATA)
7858 /* For embedded applications, always put an object in read-only data
7859 if possible, in order to reduce RAM usage. */
7861 if (((TREE_CODE (decl) == VAR_DECL
7862 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7863 && DECL_INITIAL (decl)
7864 && (DECL_INITIAL (decl) == error_mark_node
7865 || TREE_CONSTANT (DECL_INITIAL (decl))))
7866 /* Deal with calls from output_constant_def_contents. */
7867 || (TREE_CODE (decl) != VAR_DECL
7868 && (TREE_CODE (decl) != STRING_CST
7869 || !flag_writable_strings)))
7870 && ! (flag_pic && reloc))
7871 READONLY_DATA_SECTION ();
7872 else if (size > 0 && size <= mips_section_threshold)
7873 SMALL_DATA_SECTION ();
7874 else
7875 data_section ();
7877 else
7879 /* For hosted applications, always put an object in small data if
7880 possible, as this gives the best performance. */
7882 if (size > 0 && size <= mips_section_threshold)
7883 SMALL_DATA_SECTION ();
7884 else if (((TREE_CODE (decl) == VAR_DECL
7885 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7886 && DECL_INITIAL (decl)
7887 && (DECL_INITIAL (decl) == error_mark_node
7888 || TREE_CONSTANT (DECL_INITIAL (decl))))
7889 /* Deal with calls from output_constant_def_contents. */
7890 || (TREE_CODE (decl) != VAR_DECL
7891 && (TREE_CODE (decl) != STRING_CST
7892 || !flag_writable_strings)))
7893 && ! (flag_pic && reloc))
7894 READONLY_DATA_SECTION ();
7895 else
7896 data_section ();
7900 #ifdef MIPS_ABI_DEFAULT
7902 /* Support functions for the 64 bit ABI. */
7904 /* Return register to use for a function return value with VALTYPE for function
7905 FUNC. */
7908 mips_function_value (valtype, func)
7909 tree valtype;
7910 tree func ATTRIBUTE_UNUSED;
7912 int reg = GP_RETURN;
7913 enum machine_mode mode = TYPE_MODE (valtype);
7914 enum mode_class mclass = GET_MODE_CLASS (mode);
7915 int unsignedp = TREE_UNSIGNED (valtype);
7917 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
7918 just as PROMOTE_MODE does. */
7919 mode = promote_mode (valtype, mode, &unsignedp, 1);
7921 if (mclass == MODE_FLOAT)
7923 if (TARGET_SINGLE_FLOAT
7924 && (mclass == MODE_FLOAT
7925 ? GET_MODE_SIZE (mode) > 4 : GET_MODE_SIZE (mode) / 2 > 4))
7926 reg = GP_RETURN;
7927 else
7928 reg = FP_RETURN;
7931 else if (mclass == MODE_COMPLEX_FLOAT)
7933 if (TARGET_FLOAT64)
7934 reg = FP_RETURN;
7935 else if (mode == SCmode)
7937 /* When FP registers are 32 bits, we can't directly reference
7938 the odd numbered ones, so let's make a pair of evens. */
7940 enum machine_mode cmode = TYPE_MODE (TREE_TYPE (valtype));
7942 return gen_rtx_PARALLEL
7943 (VOIDmode,
7944 gen_rtvec (2,
7945 gen_rtx_EXPR_LIST (VOIDmode,
7946 gen_rtx_REG (cmode,
7947 FP_RETURN),
7948 GEN_INT (0)),
7949 gen_rtx_EXPR_LIST (VOIDmode,
7950 gen_rtx_REG (cmode,
7951 FP_RETURN + 2),
7952 GEN_INT (4))));
7954 else
7955 reg = FP_RETURN;
7958 else if (TREE_CODE (valtype) == RECORD_TYPE
7959 && mips_abi != ABI_32
7960 && mips_abi != ABI_O64
7961 && mips_abi != ABI_EABI)
7963 /* A struct with only one or two floating point fields is returned in
7964 the floating point registers. */
7965 tree field, fields[2];
7966 int i;
7968 for (i = 0, field = TYPE_FIELDS (valtype); field;
7969 field = TREE_CHAIN (field))
7971 if (TREE_CODE (field) != FIELD_DECL)
7972 continue;
7974 if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE || i >= 2)
7975 break;
7977 fields[i++] = field;
7980 /* Must check i, so that we reject structures with no elements. */
7981 if (! field)
7983 if (i == 1)
7985 /* The structure has DImode, but we don't allow DImode values
7986 in FP registers, so we use a PARALLEL even though it isn't
7987 strictly necessary. */
7988 enum machine_mode field_mode = TYPE_MODE (TREE_TYPE (fields[0]));
7990 return gen_rtx_PARALLEL
7991 (mode,
7992 gen_rtvec (1,
7993 gen_rtx_EXPR_LIST (VOIDmode,
7994 gen_rtx_REG (field_mode,
7995 FP_RETURN),
7996 const0_rtx)));
7999 else if (i == 2)
8001 enum machine_mode first_mode
8002 = TYPE_MODE (TREE_TYPE (fields[0]));
8003 enum machine_mode second_mode
8004 = TYPE_MODE (TREE_TYPE (fields[1]));
8005 HOST_WIDE_INT first_offset = int_byte_position (fields[0]);
8006 HOST_WIDE_INT second_offset = int_byte_position (fields[1]);
8008 return gen_rtx_PARALLEL
8009 (mode,
8010 gen_rtvec (2,
8011 gen_rtx_EXPR_LIST (VOIDmode,
8012 gen_rtx_REG (first_mode,
8013 FP_RETURN),
8014 GEN_INT (first_offset)),
8015 gen_rtx_EXPR_LIST (VOIDmode,
8016 gen_rtx_REG (second_mode,
8017 FP_RETURN + 2),
8018 GEN_INT (second_offset))));
8023 return gen_rtx_REG (mode, reg);
8025 #endif
8027 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE. Return
8028 nonzero when an argument must be passed by reference. */
8031 function_arg_pass_by_reference (cum, mode, type, named)
8032 CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
8033 enum machine_mode mode;
8034 tree type;
8035 int named ATTRIBUTE_UNUSED;
8037 int size;
8039 /* We must pass by reference if we would be both passing in registers
8040 and the stack. This is because any subsequent partial arg would be
8041 handled incorrectly in this case.
8043 ??? This is really a kludge. We should either fix GCC so that such
8044 a situation causes an abort and then do something in the MIPS port
8045 to prevent it, or add code to function.c to properly handle the case. */
8046 /* ??? cum can be NULL when called from mips_va_arg. The problem handled
8047 here hopefully is not relevant to mips_va_arg. */
8048 if (cum && MUST_PASS_IN_STACK (mode, type)
8049 && mips_abi != ABI_MEABI)
8051 /* Don't pass the actual CUM to FUNCTION_ARG, because we would
8052 get double copies of any offsets generated for small structs
8053 passed in registers. */
8054 CUMULATIVE_ARGS temp;
8055 temp = *cum;
8056 if (FUNCTION_ARG (temp, mode, type, named) != 0)
8057 return 1;
8060 /* Otherwise, we only do this if EABI is selected. */
8061 if (mips_abi != ABI_EABI)
8062 return 0;
8064 /* ??? How should SCmode be handled? */
8065 if (type == NULL_TREE || mode == DImode || mode == DFmode)
8066 return 0;
8068 size = int_size_in_bytes (type);
8069 return size == -1 || size > UNITS_PER_WORD;
8072 /* This function returns the register class required for a secondary
8073 register when copying between one of the registers in CLASS, and X,
8074 using MODE. If IN_P is nonzero, the copy is going from X to the
8075 register, otherwise the register is the source. A return value of
8076 NO_REGS means that no secondary register is required. */
8078 enum reg_class
8079 mips_secondary_reload_class (class, mode, x, in_p)
8080 enum reg_class class;
8081 enum machine_mode mode;
8082 rtx x;
8083 int in_p;
8085 enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
8086 int regno = -1;
8087 int gp_reg_p;
8089 if (GET_CODE (x) == SIGN_EXTEND)
8091 int off = 0;
8093 x = XEXP (x, 0);
8095 /* We may be called with reg_renumber NULL from regclass.
8096 ??? This is probably a bug. */
8097 if (reg_renumber)
8098 regno = true_regnum (x);
8099 else
8101 while (GET_CODE (x) == SUBREG)
8103 off += subreg_regno_offset (REGNO (SUBREG_REG (x)),
8104 GET_MODE (SUBREG_REG (x)),
8105 SUBREG_BYTE (x),
8106 GET_MODE (x));
8107 x = SUBREG_REG (x);
8110 if (GET_CODE (x) == REG)
8111 regno = REGNO (x) + off;
8115 else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
8116 regno = true_regnum (x);
8118 gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
8120 /* We always require a general register when copying anything to
8121 HILO_REGNUM, except when copying an SImode value from HILO_REGNUM
8122 to a general register, or when copying from register 0. */
8123 if (class == HILO_REG && regno != GP_REG_FIRST + 0)
8124 return ((! in_p
8125 && gp_reg_p
8126 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8127 ? NO_REGS : gr_regs);
8128 else if (regno == HILO_REGNUM)
8129 return ((in_p
8130 && class == gr_regs
8131 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8132 ? NO_REGS : gr_regs);
8134 /* Copying from HI or LO to anywhere other than a general register
8135 requires a general register. */
8136 if (class == HI_REG || class == LO_REG || class == MD_REGS)
8138 if (TARGET_MIPS16 && in_p)
8140 /* We can't really copy to HI or LO at all in mips16 mode. */
8141 return M16_REGS;
8143 return gp_reg_p ? NO_REGS : gr_regs;
8145 if (MD_REG_P (regno))
8147 if (TARGET_MIPS16 && ! in_p)
8149 /* We can't really copy to HI or LO at all in mips16 mode. */
8150 return M16_REGS;
8152 return class == gr_regs ? NO_REGS : gr_regs;
8155 /* We can only copy a value to a condition code register from a
8156 floating point register, and even then we require a scratch
8157 floating point register. We can only copy a value out of a
8158 condition code register into a general register. */
8159 if (class == ST_REGS)
8161 if (in_p)
8162 return FP_REGS;
8163 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
8165 if (ST_REG_P (regno))
8167 if (! in_p)
8168 return FP_REGS;
8169 return class == GR_REGS ? NO_REGS : GR_REGS;
8172 /* In mips16 mode, going between memory and anything but M16_REGS
8173 requires an M16_REG. */
8174 if (TARGET_MIPS16)
8176 if (class != M16_REGS && class != M16_NA_REGS)
8178 if (gp_reg_p)
8179 return NO_REGS;
8180 return M16_REGS;
8182 if (! gp_reg_p)
8184 /* The stack pointer isn't a valid operand to an add instruction,
8185 so we need to load it into M16_REGS first. This can happen as
8186 a result of register elimination and form_sum converting
8187 (plus reg (plus SP CONST)) to (plus (plus reg SP) CONST). We
8188 need an extra register if the dest is the same as the other
8189 register. In that case, we can't fix the problem by loading SP
8190 into the dest first. */
8191 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
8192 && GET_CODE (XEXP (x, 1)) == REG
8193 && (XEXP (x, 0) == stack_pointer_rtx
8194 || XEXP (x, 1) == stack_pointer_rtx))
8195 return (class == M16_REGS ? M16_NA_REGS : M16_REGS);
8197 if (class == M16_REGS || class == M16_NA_REGS)
8198 return NO_REGS;
8199 return M16_REGS;
8203 return NO_REGS;
8206 /* For each mips16 function which refers to GP relative symbols, we
8207 use a pseudo register, initialized at the start of the function, to
8208 hold the $gp value. */
8211 mips16_gp_pseudo_reg ()
8213 if (mips16_gp_pseudo_rtx == NULL_RTX)
8215 rtx const_gp;
8216 rtx insn, scan;
8218 mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
8219 RTX_UNCHANGING_P (mips16_gp_pseudo_rtx) = 1;
8221 /* We want to initialize this to a value which gcc will believe
8222 is constant. */
8223 const_gp = gen_rtx (CONST, Pmode,
8224 gen_rtx (REG, Pmode, GP_REG_FIRST + 28));
8226 start_sequence ();
8227 emit_move_insn (mips16_gp_pseudo_rtx, const_gp);
8228 insn = gen_sequence ();
8229 end_sequence ();
8231 push_topmost_sequence ();
8232 /* We need to emit the initialization after the FUNCTION_BEG
8233 note, so that it will be integrated. */
8234 for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
8235 if (GET_CODE (scan) == NOTE
8236 && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
8237 break;
8238 if (scan == NULL_RTX)
8239 scan = get_insns ();
8240 insn = emit_insn_after (insn, scan);
8241 pop_topmost_sequence ();
8244 return mips16_gp_pseudo_rtx;
8247 /* Return an RTX which represents the signed 16 bit offset from the
8248 $gp register for the given symbol. This is only used on the
8249 mips16. */
8252 mips16_gp_offset (sym)
8253 rtx sym;
8255 tree gp;
8257 if (GET_CODE (sym) != SYMBOL_REF
8258 || ! SYMBOL_REF_FLAG (sym))
8259 abort ();
8261 /* We use a special identifier to represent the value of the gp
8262 register. */
8263 gp = get_identifier ("__mips16_gp_value");
8265 return gen_rtx (CONST, Pmode,
8266 gen_rtx (MINUS, Pmode, sym,
8267 gen_rtx (SYMBOL_REF, Pmode,
8268 IDENTIFIER_POINTER (gp))));
8271 /* Return nonzero if the given RTX represents a signed 16 bit offset
8272 from the $gp register. */
8275 mips16_gp_offset_p (x)
8276 rtx x;
8278 if (GET_CODE (x) == CONST)
8279 x = XEXP (x, 0);
8281 /* It's OK to add a small integer value to a gp offset. */
8282 if (GET_CODE (x) == PLUS)
8284 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8285 && SMALL_INT (XEXP (x, 1)))
8286 return mips16_gp_offset_p (XEXP (x, 0));
8287 if (GET_CODE (XEXP (x, 0)) == CONST_INT
8288 && SMALL_INT (XEXP (x, 0)))
8289 return mips16_gp_offset_p (XEXP (x, 1));
8290 return 0;
8293 /* Make sure it is in the form SYM - __mips16_gp_value. */
8294 return (GET_CODE (x) == MINUS
8295 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
8296 && SYMBOL_REF_FLAG (XEXP (x, 0))
8297 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
8298 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0);
8301 /* Output a GP offset. We don't want to print the subtraction of
8302 __mips16_gp_value; it is implicitly represented by the %gprel which
8303 should have been printed by the caller. */
8305 static void
8306 mips16_output_gp_offset (file, x)
8307 FILE *file;
8308 rtx x;
8310 if (GET_CODE (x) == CONST)
8311 x = XEXP (x, 0);
8313 if (GET_CODE (x) == PLUS)
8315 mips16_output_gp_offset (file, XEXP (x, 0));
8316 fputs ("+", file);
8317 mips16_output_gp_offset (file, XEXP (x, 1));
8318 return;
8321 if (GET_CODE (x) == MINUS
8322 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
8323 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0)
8325 mips16_output_gp_offset (file, XEXP (x, 0));
8326 return;
8329 output_addr_const (file, x);
8332 /* Return nonzero if a constant should not be output until after the
8333 function. This is true of most string constants, so that we can
8334 use a more efficient PC relative reference. However, a static
8335 inline function may never call assemble_function_end to write out
8336 the constant pool, so don't try to postpone the constant in that
8337 case.
8339 ??? It's really a bug that a static inline function can put stuff
8340 in the constant pool even if the function itself is not output.
8342 We record which string constants we've seen, so that we know which
8343 ones might use the more efficient reference. */
8346 mips16_constant_after_function_p (x)
8347 tree x;
8349 if (TREE_CODE (x) == STRING_CST
8350 && ! flag_writable_strings
8351 && current_function_decl != 0
8352 && ! DECL_DEFER_OUTPUT (current_function_decl)
8353 && ! (DECL_INLINE (current_function_decl)
8354 && ((! TREE_PUBLIC (current_function_decl)
8355 && ! TREE_ADDRESSABLE (current_function_decl)
8356 && ! flag_keep_inline_functions)
8357 || DECL_EXTERNAL (current_function_decl))))
8359 struct string_constant *n;
8361 n = (struct string_constant *) xmalloc (sizeof *n);
8362 n->label = XSTR (XEXP (TREE_CST_RTL (x), 0), 0);
8363 n->next = string_constants;
8364 string_constants = n;
8366 return 1;
8369 return 0;
8372 /* Validate a constant for the mips16. This rejects general symbolic
8373 addresses, which must be loaded from memory. If ADDR is nonzero,
8374 this should reject anything which is not a legal address. If
8375 ADDEND is nonzero, this is being added to something else. */
8378 mips16_constant (x, mode, addr, addend)
8379 rtx x;
8380 enum machine_mode mode;
8381 int addr;
8382 int addend;
8384 while (GET_CODE (x) == CONST)
8385 x = XEXP (x, 0);
8387 switch (GET_CODE (x))
8389 default:
8390 return 0;
8392 case PLUS:
8393 return (mips16_constant (XEXP (x, 0), mode, addr, 1)
8394 && mips16_constant (XEXP (x, 1), mode, addr, 1));
8396 case SYMBOL_REF:
8397 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
8398 return 0;
8399 if (CONSTANT_POOL_ADDRESS_P (x))
8400 return 1;
8402 /* If we aren't looking for a memory address, we can accept a GP
8403 relative symbol, which will have SYMBOL_REF_FLAG set; movsi
8404 knows how to handle this. We can always accept a string
8405 constant, which is the other case in which SYMBOL_REF_FLAG
8406 will be set. */
8407 if (! addr
8408 && ! addend
8409 && SYMBOL_REF_FLAG (x)
8410 && mode == (enum machine_mode) Pmode)
8411 return 1;
8413 /* We can accept a string constant, which will have
8414 SYMBOL_REF_FLAG set but must be recognized by name to
8415 distinguish from a GP accessible symbol. The name of a
8416 string constant will have been generated by
8417 ASM_GENERATE_INTERNAL_LABEL as called by output_constant_def. */
8418 if (SYMBOL_REF_FLAG (x))
8420 const char *name = XSTR (x, 0);
8422 return (name[0] == '*'
8423 && strncmp (name + 1, LOCAL_LABEL_PREFIX,
8424 sizeof LOCAL_LABEL_PREFIX - 1) == 0);
8427 return 0;
8429 case LABEL_REF:
8430 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
8431 return 0;
8432 return 1;
8434 case CONST_INT:
8435 if (addr && ! addend)
8436 return 0;
8437 return INTVAL (x) > - 0x10000 && INTVAL (x) <= 0xffff;
8439 case REG:
8440 /* We need to treat $gp as a legitimate constant, because
8441 mips16_gp_pseudo_reg assumes that. */
8442 return REGNO (x) == GP_REG_FIRST + 28;
8446 /* Write out code to move floating point arguments in or out of
8447 general registers. Output the instructions to FILE. FP_CODE is
8448 the code describing which arguments are present (see the comment at
8449 the definition of CUMULATIVE_ARGS in mips.h). FROM_FP_P is non-zero if
8450 we are copying from the floating point registers. */
8452 static void
8453 mips16_fp_args (file, fp_code, from_fp_p)
8454 FILE *file;
8455 int fp_code;
8456 int from_fp_p;
8458 const char *s;
8459 int gparg, fparg;
8460 unsigned int f;
8462 /* This code only works for the original 32 bit ABI and the O64 ABI. */
8463 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
8464 abort ();
8466 if (from_fp_p)
8467 s = "mfc1";
8468 else
8469 s = "mtc1";
8470 gparg = GP_ARG_FIRST;
8471 fparg = FP_ARG_FIRST;
8472 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8474 if ((f & 3) == 1)
8476 if ((fparg & 1) != 0)
8477 ++fparg;
8478 fprintf (file, "\t%s\t%s,%s\n", s,
8479 reg_names[gparg], reg_names[fparg]);
8481 else if ((f & 3) == 2)
8483 if (TARGET_64BIT)
8484 fprintf (file, "\td%s\t%s,%s\n", s,
8485 reg_names[gparg], reg_names[fparg]);
8486 else
8488 if ((fparg & 1) != 0)
8489 ++fparg;
8490 if (TARGET_BIG_ENDIAN)
8491 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
8492 reg_names[gparg], reg_names[fparg + 1], s,
8493 reg_names[gparg + 1], reg_names[fparg]);
8494 else
8495 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
8496 reg_names[gparg], reg_names[fparg], s,
8497 reg_names[gparg + 1], reg_names[fparg + 1]);
8498 ++gparg;
8499 ++fparg;
8502 else
8503 abort ();
8505 ++gparg;
8506 ++fparg;
8510 /* Build a mips16 function stub. This is used for functions which
8511 take aruments in the floating point registers. It is 32 bit code
8512 that moves the floating point args into the general registers, and
8513 then jumps to the 16 bit code. */
8515 static void
8516 build_mips16_function_stub (file)
8517 FILE *file;
8519 const char *fnname;
8520 char *secname, *stubname;
8521 tree stubid, stubdecl;
8522 int need_comma;
8523 unsigned int f;
8525 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
8526 secname = (char *) alloca (strlen (fnname) + 20);
8527 sprintf (secname, ".mips16.fn.%s", fnname);
8528 stubname = (char *) alloca (strlen (fnname) + 20);
8529 sprintf (stubname, "__fn_stub_%s", fnname);
8530 stubid = get_identifier (stubname);
8531 stubdecl = build_decl (FUNCTION_DECL, stubid,
8532 build_function_type (void_type_node, NULL_TREE));
8533 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8535 fprintf (file, "\t# Stub function for %s (", current_function_name);
8536 need_comma = 0;
8537 for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
8539 fprintf (file, "%s%s",
8540 need_comma ? ", " : "",
8541 (f & 3) == 1 ? "float" : "double");
8542 need_comma = 1;
8544 fprintf (file, ")\n");
8546 fprintf (file, "\t.set\tnomips16\n");
8547 function_section (stubdecl);
8548 ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
8550 /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
8551 within a .ent, and we can not emit another .ent. */
8552 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8553 fputs ("\t.ent\t", file);
8554 assemble_name (file, stubname);
8555 fputs ("\n", file);
8556 #endif
8558 assemble_name (file, stubname);
8559 fputs (":\n", file);
8561 /* We don't want the assembler to insert any nops here. */
8562 fprintf (file, "\t.set\tnoreorder\n");
8564 mips16_fp_args (file, current_function_args_info.fp_code, 1);
8566 fprintf (asm_out_file, "\t.set\tnoat\n");
8567 fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
8568 assemble_name (file, fnname);
8569 fprintf (file, "\n");
8570 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8571 fprintf (asm_out_file, "\t.set\tat\n");
8573 /* Unfortunately, we can't fill the jump delay slot. We can't fill
8574 with one of the mfc1 instructions, because the result is not
8575 available for one instruction, so if the very first instruction
8576 in the function refers to the register, it will see the wrong
8577 value. */
8578 fprintf (file, "\tnop\n");
8580 fprintf (file, "\t.set\treorder\n");
8582 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8583 fputs ("\t.end\t", file);
8584 assemble_name (file, stubname);
8585 fputs ("\n", file);
8586 #endif
8588 fprintf (file, "\t.set\tmips16\n");
8590 function_section (current_function_decl);
8593 /* We keep a list of functions for which we have already built stubs
8594 in build_mips16_call_stub. */
8596 struct mips16_stub
8598 struct mips16_stub *next;
8599 char *name;
8600 int fpret;
8603 static struct mips16_stub *mips16_stubs;
8605 /* Build a call stub for a mips16 call. A stub is needed if we are
8606 passing any floating point values which should go into the floating
8607 point registers. If we are, and the call turns out to be to a 32
8608 bit function, the stub will be used to move the values into the
8609 floating point registers before calling the 32 bit function. The
8610 linker will magically adjust the function call to either the 16 bit
8611 function or the 32 bit stub, depending upon where the function call
8612 is actually defined.
8614 Similarly, we need a stub if the return value might come back in a
8615 floating point register.
8617 RETVAL, FNMEM, and ARG_SIZE are the values passed to the call insn
8618 (RETVAL is NULL if this is call rather than call_value). FP_CODE
8619 is the code built by function_arg. This function returns a nonzero
8620 value if it builds the call instruction itself. */
8623 build_mips16_call_stub (retval, fnmem, arg_size, fp_code)
8624 rtx retval;
8625 rtx fnmem;
8626 rtx arg_size;
8627 int fp_code;
8629 int fpret;
8630 rtx fn;
8631 const char *fnname;
8632 char *secname, *stubname;
8633 struct mips16_stub *l;
8634 tree stubid, stubdecl;
8635 int need_comma;
8636 unsigned int f;
8638 /* We don't need to do anything if we aren't in mips16 mode, or if
8639 we were invoked with the -msoft-float option. */
8640 if (! TARGET_MIPS16 || ! mips16_hard_float)
8641 return 0;
8643 /* Figure out whether the value might come back in a floating point
8644 register. */
8645 fpret = (retval != 0
8646 && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
8647 && (! TARGET_SINGLE_FLOAT
8648 || GET_MODE_SIZE (GET_MODE (retval)) <= 4));
8650 /* We don't need to do anything if there were no floating point
8651 arguments and the value will not be returned in a floating point
8652 register. */
8653 if (fp_code == 0 && ! fpret)
8654 return 0;
8656 if (GET_CODE (fnmem) != MEM)
8657 abort ();
8658 fn = XEXP (fnmem, 0);
8660 /* We don't need to do anything if this is a call to a special
8661 mips16 support function. */
8662 if (GET_CODE (fn) == SYMBOL_REF
8663 && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
8664 return 0;
8666 /* This code will only work for o32 and o64 abis. The other ABI's
8667 require more sophisticated support. */
8668 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
8669 abort ();
8671 /* We can only handle SFmode and DFmode floating point return
8672 values. */
8673 if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
8674 abort ();
8676 /* If we're calling via a function pointer, then we must always call
8677 via a stub. There are magic stubs provided in libgcc.a for each
8678 of the required cases. Each of them expects the function address
8679 to arrive in register $2. */
8681 if (GET_CODE (fn) != SYMBOL_REF)
8683 char buf[30];
8684 tree id;
8685 rtx stub_fn, stub_mem, insn;
8687 /* ??? If this code is modified to support other ABI's, we need
8688 to handle PARALLEL return values here. */
8690 sprintf (buf, "__mips16_call_stub_%s%d",
8691 (fpret
8692 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
8693 : ""),
8694 fp_code);
8695 id = get_identifier (buf);
8696 stub_fn = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (id));
8697 stub_mem = gen_rtx (MEM, Pmode, stub_fn);
8699 emit_move_insn (gen_rtx (REG, Pmode, 2), fn);
8701 if (retval == NULL_RTX)
8702 insn = gen_call_internal0 (stub_mem, arg_size,
8703 gen_rtx (REG, SImode,
8704 GP_REG_FIRST + 31));
8705 else
8706 insn = gen_call_value_internal0 (retval, stub_mem, arg_size,
8707 gen_rtx (REG, SImode,
8708 GP_REG_FIRST + 31));
8709 insn = emit_call_insn (insn);
8711 /* Put the register usage information on the CALL. */
8712 if (GET_CODE (insn) != CALL_INSN)
8713 abort ();
8714 CALL_INSN_FUNCTION_USAGE (insn) =
8715 gen_rtx (EXPR_LIST, VOIDmode,
8716 gen_rtx (USE, VOIDmode, gen_rtx (REG, Pmode, 2)),
8717 CALL_INSN_FUNCTION_USAGE (insn));
8719 /* If we are handling a floating point return value, we need to
8720 save $18 in the function prologue. Putting a note on the
8721 call will mean that regs_ever_live[$18] will be true if the
8722 call is not eliminated, and we can check that in the prologue
8723 code. */
8724 if (fpret)
8725 CALL_INSN_FUNCTION_USAGE (insn) =
8726 gen_rtx (EXPR_LIST, VOIDmode,
8727 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
8728 CALL_INSN_FUNCTION_USAGE (insn));
8730 /* Return 1 to tell the caller that we've generated the call
8731 insn. */
8732 return 1;
8735 /* We know the function we are going to call. If we have already
8736 built a stub, we don't need to do anything further. */
8738 fnname = XSTR (fn, 0);
8739 for (l = mips16_stubs; l != NULL; l = l->next)
8740 if (strcmp (l->name, fnname) == 0)
8741 break;
8743 if (l == NULL)
8745 /* Build a special purpose stub. When the linker sees a
8746 function call in mips16 code, it will check where the target
8747 is defined. If the target is a 32 bit call, the linker will
8748 search for the section defined here. It can tell which
8749 symbol this section is associated with by looking at the
8750 relocation information (the name is unreliable, since this
8751 might be a static function). If such a section is found, the
8752 linker will redirect the call to the start of the magic
8753 section.
8755 If the function does not return a floating point value, the
8756 special stub section is named
8757 .mips16.call.FNNAME
8759 If the function does return a floating point value, the stub
8760 section is named
8761 .mips16.call.fp.FNNAME
8764 secname = (char *) alloca (strlen (fnname) + 40);
8765 sprintf (secname, ".mips16.call.%s%s",
8766 fpret ? "fp." : "",
8767 fnname);
8768 stubname = (char *) alloca (strlen (fnname) + 20);
8769 sprintf (stubname, "__call_stub_%s%s",
8770 fpret ? "fp_" : "",
8771 fnname);
8772 stubid = get_identifier (stubname);
8773 stubdecl = build_decl (FUNCTION_DECL, stubid,
8774 build_function_type (void_type_node, NULL_TREE));
8775 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8777 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8778 (fpret
8779 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8780 : ""),
8781 fnname);
8782 need_comma = 0;
8783 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8785 fprintf (asm_out_file, "%s%s",
8786 need_comma ? ", " : "",
8787 (f & 3) == 1 ? "float" : "double");
8788 need_comma = 1;
8790 fprintf (asm_out_file, ")\n");
8792 fprintf (asm_out_file, "\t.set\tnomips16\n");
8793 assemble_start_function (stubdecl, stubname);
8795 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8796 fputs ("\t.ent\t", asm_out_file);
8797 assemble_name (asm_out_file, stubname);
8798 fputs ("\n", asm_out_file);
8800 assemble_name (asm_out_file, stubname);
8801 fputs (":\n", asm_out_file);
8802 #endif
8804 /* We build the stub code by hand. That's the only way we can
8805 do it, since we can't generate 32 bit code during a 16 bit
8806 compilation. */
8808 /* We don't want the assembler to insert any nops here. */
8809 fprintf (asm_out_file, "\t.set\tnoreorder\n");
8811 mips16_fp_args (asm_out_file, fp_code, 0);
8813 if (! fpret)
8815 fprintf (asm_out_file, "\t.set\tnoat\n");
8816 fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
8817 fnname);
8818 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8819 fprintf (asm_out_file, "\t.set\tat\n");
8820 /* Unfortunately, we can't fill the jump delay slot. We
8821 can't fill with one of the mtc1 instructions, because the
8822 result is not available for one instruction, so if the
8823 very first instruction in the function refers to the
8824 register, it will see the wrong value. */
8825 fprintf (asm_out_file, "\tnop\n");
8827 else
8829 fprintf (asm_out_file, "\tmove\t%s,%s\n",
8830 reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
8831 fprintf (asm_out_file, "\tjal\t%s\n", fnname);
8832 /* As above, we can't fill the delay slot. */
8833 fprintf (asm_out_file, "\tnop\n");
8834 if (GET_MODE (retval) == SFmode)
8835 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8836 reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
8837 else
8839 if (TARGET_BIG_ENDIAN)
8841 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8842 reg_names[GP_REG_FIRST + 2],
8843 reg_names[FP_REG_FIRST + 1]);
8844 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8845 reg_names[GP_REG_FIRST + 3],
8846 reg_names[FP_REG_FIRST + 0]);
8848 else
8850 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8851 reg_names[GP_REG_FIRST + 2],
8852 reg_names[FP_REG_FIRST + 0]);
8853 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8854 reg_names[GP_REG_FIRST + 3],
8855 reg_names[FP_REG_FIRST + 1]);
8858 fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
8859 /* As above, we can't fill the delay slot. */
8860 fprintf (asm_out_file, "\tnop\n");
8863 fprintf (asm_out_file, "\t.set\treorder\n");
8865 #ifdef ASM_DECLARE_FUNCTION_SIZE
8866 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8867 #endif
8869 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8870 fputs ("\t.end\t", asm_out_file);
8871 assemble_name (asm_out_file, stubname);
8872 fputs ("\n", asm_out_file);
8873 #endif
8875 fprintf (asm_out_file, "\t.set\tmips16\n");
8877 /* Record this stub. */
8878 l = (struct mips16_stub *) xmalloc (sizeof *l);
8879 l->name = xstrdup (fnname);
8880 l->fpret = fpret;
8881 l->next = mips16_stubs;
8882 mips16_stubs = l;
8885 /* If we expect a floating point return value, but we've built a
8886 stub which does not expect one, then we're in trouble. We can't
8887 use the existing stub, because it won't handle the floating point
8888 value. We can't build a new stub, because the linker won't know
8889 which stub to use for the various calls in this object file.
8890 Fortunately, this case is illegal, since it means that a function
8891 was declared in two different ways in a single compilation. */
8892 if (fpret && ! l->fpret)
8893 error ("can not handle inconsistent calls to `%s'", fnname);
8895 /* If we are calling a stub which handles a floating point return
8896 value, we need to arrange to save $18 in the prologue. We do
8897 this by marking the function call as using the register. The
8898 prologue will later see that it is used, and emit code to save
8899 it. */
8901 if (l->fpret)
8903 rtx insn;
8905 if (retval == NULL_RTX)
8906 insn = gen_call_internal0 (fnmem, arg_size,
8907 gen_rtx (REG, SImode,
8908 GP_REG_FIRST + 31));
8909 else
8910 insn = gen_call_value_internal0 (retval, fnmem, arg_size,
8911 gen_rtx (REG, SImode,
8912 GP_REG_FIRST + 31));
8913 insn = emit_call_insn (insn);
8915 if (GET_CODE (insn) != CALL_INSN)
8916 abort ();
8918 CALL_INSN_FUNCTION_USAGE (insn) =
8919 gen_rtx (EXPR_LIST, VOIDmode,
8920 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
8921 CALL_INSN_FUNCTION_USAGE (insn));
8923 /* Return 1 to tell the caller that we've generated the call
8924 insn. */
8925 return 1;
8928 /* Return 0 to let the caller generate the call insn. */
8929 return 0;
8932 /* This function looks through the code for a function, and tries to
8933 optimize the usage of the $gp register. We arrange to copy $gp
8934 into a pseudo-register, and then let gcc's normal reload handling
8935 deal with the pseudo-register. Unfortunately, if reload choose to
8936 put the pseudo-register into a call-clobbered register, it will
8937 emit saves and restores for that register around any function
8938 calls. We don't need the saves, and it's faster to copy $gp than
8939 to do an actual restore. ??? This still means that we waste a
8940 stack slot.
8942 This is an optimization, and the code which gcc has actually
8943 generated is correct, so we do not need to catch all cases. */
8945 static void
8946 mips16_optimize_gp (first)
8947 rtx first;
8949 rtx gpcopy, slot, insn;
8951 /* Look through the instructions. Set GPCOPY to the register which
8952 holds a copy of $gp. Set SLOT to the stack slot where it is
8953 saved. If we find an instruction which sets GPCOPY to anything
8954 other than $gp or SLOT, then we can't use it. If we find an
8955 instruction which sets SLOT to anything other than GPCOPY, we
8956 can't use it. */
8958 gpcopy = NULL_RTX;
8959 slot = NULL_RTX;
8960 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
8962 rtx set;
8964 if (! INSN_P (insn))
8965 continue;
8967 set = PATTERN (insn);
8969 /* We know that all references to memory will be inside a SET,
8970 because there is no other way to access memory on the mips16.
8971 We don't have to worry about a PARALLEL here, because the
8972 mips.md file will never generate them for memory references. */
8973 if (GET_CODE (set) != SET)
8974 continue;
8976 if (gpcopy == NULL_RTX
8977 && GET_CODE (SET_SRC (set)) == CONST
8978 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
8979 && REGNO (XEXP (SET_SRC (set), 0)) == GP_REG_FIRST + 28
8980 && GET_CODE (SET_DEST (set)) == REG
8981 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
8982 gpcopy = SET_DEST (set);
8983 else if (slot == NULL_RTX
8984 && gpcopy != NULL_RTX
8985 && GET_CODE (SET_DEST (set)) == MEM
8986 && GET_CODE (SET_SRC (set)) == REG
8987 && REGNO (SET_SRC (set)) == REGNO (gpcopy)
8988 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
8990 rtx base, offset;
8992 offset = const0_rtx;
8993 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
8994 if (GET_CODE (base) == REG
8995 && (REGNO (base) == STACK_POINTER_REGNUM
8996 || REGNO (base) == FRAME_POINTER_REGNUM))
8997 slot = SET_DEST (set);
8999 else if (gpcopy != NULL_RTX
9000 && (GET_CODE (SET_DEST (set)) == REG
9001 || GET_CODE (SET_DEST (set)) == SUBREG)
9002 && reg_overlap_mentioned_p (SET_DEST (set), gpcopy)
9003 && (GET_CODE (SET_DEST (set)) != REG
9004 || REGNO (SET_DEST (set)) != REGNO (gpcopy)
9005 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode
9006 || ((GET_CODE (SET_SRC (set)) != CONST
9007 || GET_CODE (XEXP (SET_SRC (set), 0)) != REG
9008 || (REGNO (XEXP (SET_SRC (set), 0))
9009 != GP_REG_FIRST + 28))
9010 && ! rtx_equal_p (SET_SRC (set), slot))))
9011 break;
9012 else if (slot != NULL_RTX
9013 && GET_CODE (SET_DEST (set)) == MEM
9014 && rtx_equal_p (SET_DEST (set), slot)
9015 && (GET_CODE (SET_SRC (set)) != REG
9016 || REGNO (SET_SRC (set)) != REGNO (gpcopy)))
9017 break;
9020 /* If we couldn't find a unique value for GPCOPY or SLOT, then try a
9021 different optimization. Any time we find a copy of $28 into a
9022 register, followed by an add of a symbol_ref to that register, we
9023 convert it to load the value from the constant table instead.
9024 The copy and add will take six bytes, just as the load and
9025 constant table entry will take six bytes. However, it is
9026 possible that the constant table entry will be shared.
9028 This could be a peephole optimization, but I don't know if the
9029 peephole code can call force_const_mem.
9031 Using the same register for the copy of $28 and the add of the
9032 symbol_ref is actually pretty likely, since the add instruction
9033 requires the destination and the first addend to be the same
9034 register. */
9036 if (insn != NULL_RTX || gpcopy == NULL_RTX || slot == NULL_RTX)
9038 rtx next;
9040 /* This optimization is only reasonable if the constant table
9041 entries are only 4 bytes. */
9042 if (Pmode != SImode)
9043 return;
9045 for (insn = first; insn != NULL_RTX; insn = next)
9047 rtx set1, set2;
9049 next = insn;
9052 next = NEXT_INSN (next);
9054 while (next != NULL_RTX
9055 && (GET_CODE (next) == NOTE
9056 || (GET_CODE (next) == INSN
9057 && (GET_CODE (PATTERN (next)) == USE
9058 || GET_CODE (PATTERN (next)) == CLOBBER))));
9060 if (next == NULL_RTX)
9061 break;
9063 if (! INSN_P (insn))
9064 continue;
9066 if (! INSN_P (next))
9067 continue;
9069 set1 = PATTERN (insn);
9070 if (GET_CODE (set1) != SET)
9071 continue;
9072 set2 = PATTERN (next);
9073 if (GET_CODE (set2) != SET)
9074 continue;
9076 if (GET_CODE (SET_DEST (set1)) == REG
9077 && GET_CODE (SET_SRC (set1)) == CONST
9078 && GET_CODE (XEXP (SET_SRC (set1), 0)) == REG
9079 && REGNO (XEXP (SET_SRC (set1), 0)) == GP_REG_FIRST + 28
9080 && rtx_equal_p (SET_DEST (set1), SET_DEST (set2))
9081 && GET_CODE (SET_SRC (set2)) == PLUS
9082 && rtx_equal_p (SET_DEST (set1), XEXP (SET_SRC (set2), 0))
9083 && mips16_gp_offset_p (XEXP (SET_SRC (set2), 1))
9084 && GET_CODE (XEXP (XEXP (SET_SRC (set2), 1), 0)) == MINUS)
9086 rtx sym;
9088 /* We've found a case we can change to load from the
9089 constant table. */
9091 sym = XEXP (XEXP (XEXP (SET_SRC (set2), 1), 0), 0);
9092 if (GET_CODE (sym) != SYMBOL_REF)
9093 abort ();
9094 emit_insn_after (gen_rtx (SET, VOIDmode, SET_DEST (set1),
9095 force_const_mem (Pmode, sym)),
9096 next);
9098 PUT_CODE (insn, NOTE);
9099 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9100 NOTE_SOURCE_FILE (insn) = 0;
9102 PUT_CODE (next, NOTE);
9103 NOTE_LINE_NUMBER (next) = NOTE_INSN_DELETED;
9104 NOTE_SOURCE_FILE (next) = 0;
9108 return;
9111 /* We can safely remove all assignments to SLOT from GPCOPY, and
9112 replace all assignments from SLOT to GPCOPY with assignments from
9113 $28. */
9115 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9117 rtx set;
9119 if (! INSN_P (insn))
9120 continue;
9122 set = PATTERN (insn);
9123 if (GET_CODE (set) != SET
9124 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode)
9125 continue;
9127 if (GET_CODE (SET_DEST (set)) == MEM
9128 && rtx_equal_p (SET_DEST (set), slot)
9129 && GET_CODE (SET_SRC (set)) == REG
9130 && REGNO (SET_SRC (set)) == REGNO (gpcopy))
9132 PUT_CODE (insn, NOTE);
9133 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9134 NOTE_SOURCE_FILE (insn) = 0;
9136 else if (GET_CODE (SET_DEST (set)) == REG
9137 && REGNO (SET_DEST (set)) == REGNO (gpcopy)
9138 && GET_CODE (SET_SRC (set)) == MEM
9139 && rtx_equal_p (SET_SRC (set), slot))
9141 emit_insn_after (gen_rtx (SET, Pmode, SET_DEST (set),
9142 gen_rtx (CONST, Pmode,
9143 gen_rtx (REG, Pmode,
9144 GP_REG_FIRST + 28))),
9145 insn);
9146 PUT_CODE (insn, NOTE);
9147 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9148 NOTE_SOURCE_FILE (insn) = 0;
9153 /* We keep a list of constants we which we have to add to internal
9154 constant tables in the middle of large functions. */
9156 struct constant
9158 struct constant *next;
9159 rtx value;
9160 rtx label;
9161 enum machine_mode mode;
9164 /* Add a constant to the list in *PCONSTANTS. */
9166 static rtx
9167 add_constant (pconstants, val, mode)
9168 struct constant **pconstants;
9169 rtx val;
9170 enum machine_mode mode;
9172 struct constant *c;
9174 for (c = *pconstants; c != NULL; c = c->next)
9175 if (mode == c->mode && rtx_equal_p (val, c->value))
9176 return c->label;
9178 c = (struct constant *) xmalloc (sizeof *c);
9179 c->value = val;
9180 c->mode = mode;
9181 c->label = gen_label_rtx ();
9182 c->next = *pconstants;
9183 *pconstants = c;
9184 return c->label;
9187 /* Dump out the constants in CONSTANTS after INSN. */
9189 static void
9190 dump_constants (constants, insn)
9191 struct constant *constants;
9192 rtx insn;
9194 struct constant *c;
9195 int align;
9197 c = constants;
9198 align = 0;
9199 while (c != NULL)
9201 rtx r;
9202 struct constant *next;
9204 switch (GET_MODE_SIZE (c->mode))
9206 case 1:
9207 align = 0;
9208 break;
9209 case 2:
9210 if (align < 1)
9211 insn = emit_insn_after (gen_align_2 (), insn);
9212 align = 1;
9213 break;
9214 case 4:
9215 if (align < 2)
9216 insn = emit_insn_after (gen_align_4 (), insn);
9217 align = 2;
9218 break;
9219 default:
9220 if (align < 3)
9221 insn = emit_insn_after (gen_align_8 (), insn);
9222 align = 3;
9223 break;
9226 insn = emit_label_after (c->label, insn);
9228 switch (c->mode)
9230 case QImode:
9231 r = gen_consttable_qi (c->value);
9232 break;
9233 case HImode:
9234 r = gen_consttable_hi (c->value);
9235 break;
9236 case SImode:
9237 r = gen_consttable_si (c->value);
9238 break;
9239 case SFmode:
9240 r = gen_consttable_sf (c->value);
9241 break;
9242 case DImode:
9243 r = gen_consttable_di (c->value);
9244 break;
9245 case DFmode:
9246 r = gen_consttable_df (c->value);
9247 break;
9248 default:
9249 abort ();
9252 insn = emit_insn_after (r, insn);
9254 next = c->next;
9255 free (c);
9256 c = next;
9259 emit_barrier_after (insn);
9262 /* Find the symbol in an address expression. */
9264 static rtx
9265 mips_find_symbol (addr)
9266 rtx addr;
9268 if (GET_CODE (addr) == MEM)
9269 addr = XEXP (addr, 0);
9270 while (GET_CODE (addr) == CONST)
9271 addr = XEXP (addr, 0);
9272 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
9273 return addr;
9274 if (GET_CODE (addr) == PLUS)
9276 rtx l1, l2;
9278 l1 = mips_find_symbol (XEXP (addr, 0));
9279 l2 = mips_find_symbol (XEXP (addr, 1));
9280 if (l1 != NULL_RTX && l2 == NULL_RTX)
9281 return l1;
9282 else if (l1 == NULL_RTX && l2 != NULL_RTX)
9283 return l2;
9285 return NULL_RTX;
9288 /* Exported to toplev.c.
9290 Do a final pass over the function, just before delayed branch
9291 scheduling. */
9293 void
9294 machine_dependent_reorg (first)
9295 rtx first;
9297 int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
9298 rtx insn;
9299 struct constant *constants;
9301 if (! TARGET_MIPS16)
9302 return;
9304 /* If $gp is used, try to remove stores, and replace loads with
9305 copies from $gp. */
9306 if (optimize)
9307 mips16_optimize_gp (first);
9309 /* Scan the function looking for PC relative loads which may be out
9310 of range. All such loads will either be from the constant table,
9311 or be getting the address of a constant string. If the size of
9312 the function plus the size of the constant table is less than
9313 0x8000, then all loads are in range. */
9315 insns_len = 0;
9316 for (insn = first; insn; insn = NEXT_INSN (insn))
9318 insns_len += get_attr_length (insn);
9320 /* ??? We put switch tables in .text, but we don't define
9321 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
9322 compute their lengths correctly. */
9323 if (GET_CODE (insn) == JUMP_INSN)
9325 rtx body;
9327 body = PATTERN (insn);
9328 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
9329 insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
9330 * GET_MODE_SIZE (GET_MODE (body)));
9331 insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
9335 /* Store the original value of insns_len in current_frame_info, so
9336 that simple_memory_operand can look at it. */
9337 current_frame_info.insns_len = insns_len;
9339 pool_size = get_pool_size ();
9340 if (insns_len + pool_size + mips_string_length < 0x8000)
9341 return;
9343 /* Loop over the insns and figure out what the maximum internal pool
9344 size could be. */
9345 max_internal_pool_size = 0;
9346 for (insn = first; insn; insn = NEXT_INSN (insn))
9348 if (GET_CODE (insn) == INSN
9349 && GET_CODE (PATTERN (insn)) == SET)
9351 rtx src;
9353 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
9354 if (src == NULL_RTX)
9355 continue;
9356 if (CONSTANT_POOL_ADDRESS_P (src))
9357 max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
9358 else if (SYMBOL_REF_FLAG (src))
9359 max_internal_pool_size += GET_MODE_SIZE (Pmode);
9363 constants = NULL;
9364 addr = 0;
9365 first_constant_ref = -1;
9367 for (insn = first; insn; insn = NEXT_INSN (insn))
9369 if (GET_CODE (insn) == INSN
9370 && GET_CODE (PATTERN (insn)) == SET)
9372 rtx val, src;
9373 enum machine_mode mode = VOIDmode;
9375 val = NULL_RTX;
9376 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
9377 if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
9379 /* ??? This is very conservative, which means that we
9380 will generate too many copies of the constant table.
9381 The only solution would seem to be some form of
9382 relaxing. */
9383 if (((insns_len - addr)
9384 + max_internal_pool_size
9385 + get_pool_offset (src))
9386 >= 0x8000)
9388 val = get_pool_constant (src);
9389 mode = get_pool_mode (src);
9391 max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
9393 else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
9395 /* Including all of mips_string_length is conservative,
9396 and so is including all of max_internal_pool_size. */
9397 if (((insns_len - addr)
9398 + max_internal_pool_size
9399 + pool_size
9400 + mips_string_length)
9401 >= 0x8000)
9403 val = src;
9404 mode = Pmode;
9406 max_internal_pool_size -= Pmode;
9409 if (val != NULL_RTX)
9411 rtx lab, newsrc;
9413 /* This PC relative load is out of range. ??? In the
9414 case of a string constant, we are only guessing that
9415 it is range, since we don't know the offset of a
9416 particular string constant. */
9418 lab = add_constant (&constants, val, mode);
9419 newsrc = gen_rtx (MEM, mode,
9420 gen_rtx (LABEL_REF, VOIDmode, lab));
9421 RTX_UNCHANGING_P (newsrc) = 1;
9422 PATTERN (insn) = gen_rtx (SET, VOIDmode,
9423 SET_DEST (PATTERN (insn)),
9424 newsrc);
9425 INSN_CODE (insn) = -1;
9427 if (first_constant_ref < 0)
9428 first_constant_ref = addr;
9432 addr += get_attr_length (insn);
9434 /* ??? We put switch tables in .text, but we don't define
9435 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
9436 compute their lengths correctly. */
9437 if (GET_CODE (insn) == JUMP_INSN)
9439 rtx body;
9441 body = PATTERN (insn);
9442 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
9443 addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
9444 * GET_MODE_SIZE (GET_MODE (body)));
9445 addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
9448 if (GET_CODE (insn) == BARRIER)
9450 /* Output any constants we have accumulated. Note that we
9451 don't need to change ADDR, since its only use is
9452 subtraction from INSNS_LEN, and both would be changed by
9453 the same amount.
9454 ??? If the instructions up to the next barrier reuse a
9455 constant, it would often be better to continue
9456 accumulating. */
9457 if (constants != NULL)
9458 dump_constants (constants, insn);
9459 constants = NULL;
9460 first_constant_ref = -1;
9463 if (constants != NULL
9464 && (NEXT_INSN (insn) == NULL
9465 || (first_constant_ref >= 0
9466 && (((addr - first_constant_ref)
9467 + 2 /* for alignment */
9468 + 2 /* for a short jump insn */
9469 + pool_size)
9470 >= 0x8000))))
9472 /* If we haven't had a barrier within 0x8000 bytes of a
9473 constant reference or we are at the end of the function,
9474 emit a barrier now. */
9476 rtx label, jump, barrier;
9478 label = gen_label_rtx ();
9479 jump = emit_jump_insn_after (gen_jump (label), insn);
9480 JUMP_LABEL (jump) = label;
9481 LABEL_NUSES (label) = 1;
9482 barrier = emit_barrier_after (jump);
9483 emit_label_after (label, barrier);
9484 first_constant_ref = -1;
9488 /* ??? If we output all references to a constant in internal
9489 constants table, we don't need to output the constant in the real
9490 constant table, but we have no way to prevent that. */
9493 /* Return nonzero if X is a SIGN or ZERO extend operator. */
9495 extend_operator (x, mode)
9496 rtx x;
9497 enum machine_mode mode ATTRIBUTE_UNUSED;
9499 enum rtx_code code = GET_CODE (x);
9500 return code == SIGN_EXTEND || code == ZERO_EXTEND;
9503 /* Accept any operator that can be used to shift the high half of the
9504 input value to the lower half, suitable for truncation. The
9505 remainder (the lower half of the input, and the upper half of the
9506 output) will be discarded. */
9508 highpart_shift_operator (x, mode)
9509 rtx x;
9510 enum machine_mode mode ATTRIBUTE_UNUSED;
9512 enum rtx_code code = GET_CODE (x);
9513 return (code == LSHIFTRT
9514 || code == ASHIFTRT
9515 || code == ROTATERT
9516 || code == ROTATE);
9519 /* Return the length of INSN. LENGTH is the initial length computed by
9520 attributes in the machine-description file. */
9523 mips_adjust_insn_length (insn, length)
9524 rtx insn;
9525 int length;
9527 /* A unconditional jump has an unfilled delay slot if it is not part
9528 of a sequence. A conditional jump normally has a delay slot, but
9529 does not on MIPS16. */
9530 if (simplejump_p (insn)
9531 || (!TARGET_MIPS16 && (GET_CODE (insn) == JUMP_INSN
9532 || GET_CODE (insn) == CALL_INSN)))
9533 length += 4;
9535 /* All MIPS16 instructions are a measly two bytes. */
9536 if (TARGET_MIPS16)
9537 length /= 2;
9539 return length;
9542 /* Output assembly instructions to peform a conditional branch.
9544 INSN is the branch instruction. OPERANDS[0] is the condition.
9545 OPERANDS[1] is the target of the branch. OPERANDS[2] is the target
9546 of the first operand to the condition. If TWO_OPERANDS_P is
9547 non-zero the comparison takes two operands; OPERANDS[3] will be the
9548 second operand.
9550 If INVERTED_P is non-zero we are to branch if the condition does
9551 not hold. If FLOAT_P is non-zero this is a floating-point comparison.
9553 LENGTH is the length (in bytes) of the sequence we are to generate.
9554 That tells us whether to generate a simple conditional branch, or a
9555 reversed conditional branch around a `jr' instruction. */
9556 const char *
9557 mips_output_conditional_branch (insn,
9558 operands,
9559 two_operands_p,
9560 float_p,
9561 inverted_p,
9562 length)
9563 rtx insn;
9564 rtx *operands;
9565 int two_operands_p;
9566 int float_p;
9567 int inverted_p;
9568 int length;
9570 static char buffer[200];
9571 /* The kind of comparison we are doing. */
9572 enum rtx_code code = GET_CODE (operands[0]);
9573 /* Non-zero if the opcode for the comparison needs a `z' indicating
9574 that it is a comparision against zero. */
9575 int need_z_p;
9576 /* A string to use in the assembly output to represent the first
9577 operand. */
9578 const char *op1 = "%z2";
9579 /* A string to use in the assembly output to represent the second
9580 operand. Use the hard-wired zero register if there's no second
9581 operand. */
9582 const char *op2 = (two_operands_p ? ",%z3" : ",%.");
9583 /* The operand-printing string for the comparison. */
9584 const char *const comp = (float_p ? "%F0" : "%C0");
9585 /* The operand-printing string for the inverted comparison. */
9586 const char *const inverted_comp = (float_p ? "%W0" : "%N0");
9588 /* The MIPS processors (for levels of the ISA at least two), have
9589 "likely" variants of each branch instruction. These instructions
9590 annul the instruction in the delay slot if the branch is not
9591 taken. */
9592 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
9594 if (!two_operands_p)
9596 /* To compute whether than A > B, for example, we normally
9597 subtract B from A and then look at the sign bit. But, if we
9598 are doing an unsigned comparison, and B is zero, we don't
9599 have to do the subtraction. Instead, we can just check to
9600 see if A is non-zero. Thus, we change the CODE here to
9601 reflect the simpler comparison operation. */
9602 switch (code)
9604 case GTU:
9605 code = NE;
9606 break;
9608 case LEU:
9609 code = EQ;
9610 break;
9612 case GEU:
9613 /* A condition which will always be true. */
9614 code = EQ;
9615 op1 = "%.";
9616 break;
9618 case LTU:
9619 /* A condition which will always be false. */
9620 code = NE;
9621 op1 = "%.";
9622 break;
9624 default:
9625 /* Not a special case. */
9626 break;
9630 /* Relative comparisons are always done against zero. But
9631 equality comparisons are done between two operands, and therefore
9632 do not require a `z' in the assembly language output. */
9633 need_z_p = (!float_p && code != EQ && code != NE);
9634 /* For comparisons against zero, the zero is not provided
9635 explicitly. */
9636 if (need_z_p)
9637 op2 = "";
9639 /* Begin by terminating the buffer. That way we can always use
9640 strcat to add to it. */
9641 buffer[0] = '\0';
9643 switch (length)
9645 case 4:
9646 case 8:
9647 /* Just a simple conditional branch. */
9648 if (float_p)
9649 sprintf (buffer, "%%*b%s%%?\t%%Z2%%1",
9650 inverted_p ? inverted_comp : comp);
9651 else
9652 sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1",
9653 inverted_p ? inverted_comp : comp,
9654 need_z_p ? "z" : "",
9655 op1,
9656 op2);
9657 return buffer;
9659 case 12:
9660 case 16:
9662 /* Generate a reversed conditional branch around ` j'
9663 instruction:
9665 .set noreorder
9666 .set nomacro
9667 bc l
9669 j target
9670 .set macro
9671 .set reorder
9676 rtx orig_target;
9677 rtx target = gen_label_rtx ();
9679 output_asm_insn ("%(%<", 0);
9680 orig_target = operands[1];
9681 operands[1] = target;
9682 /* Generate the reversed comparison. This takes four
9683 bytes. */
9684 if (float_p)
9685 sprintf (buffer, "%%*b%s\t%%Z2%%1",
9686 inverted_p ? comp : inverted_comp);
9687 else
9688 sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
9689 inverted_p ? comp : inverted_comp,
9690 need_z_p ? "z" : "",
9691 op1,
9692 op2);
9693 output_asm_insn (buffer, operands);
9694 operands[1] = orig_target;
9696 output_asm_insn ("nop\n\tj\t%1", operands);
9698 if (length == 16)
9699 output_asm_insn ("nop", 0);
9700 else
9702 /* Output delay slot instruction. */
9703 rtx insn = final_sequence;
9704 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9705 optimize, 0, 1);
9706 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9708 output_asm_insn ("%>%)", 0);
9709 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
9710 CODE_LABEL_NUMBER (target));
9711 return "";
9714 /* We do not currently use this code. It handles jumps to
9715 arbitrary locations, using `jr', even across a 256MB boundary.
9716 We could add a -mhuge switch, and then use this code instead of
9717 the `j' alternative above when -mhuge was used. */
9718 #if 0
9719 case 16:
9720 case 20:
9722 /* Generate a reversed conditional branch around a `jr'
9723 instruction:
9725 .set noreorder
9726 .set nomacro
9727 .set noat
9728 bc l
9729 la $at, target
9730 jr $at
9731 .set at
9732 .set macro
9733 .set reorder
9736 Not pretty, but allows a conditional branch anywhere in the
9737 32-bit address space. If the original branch is annulled,
9738 then the instruction in the delay slot should be executed
9739 only if the branch is taken. The la instruction is really
9740 a macro which will usually take eight bytes, but sometimes
9741 takes only four, if the instruction to which we're jumping
9742 gets its own entry in the global pointer table, which will
9743 happen if its a case label. The assembler will then
9744 generate only a four-byte sequence, rather than eight, and
9745 there seems to be no way to tell it not to. Thus, we can't
9746 just use a `.+x' addressing form; we don't know what value
9747 to give for `x'.
9749 So, we resort to using the explicit relocation syntax
9750 available in the assembler and do:
9752 lw $at,%got_page(target)($gp)
9753 daddiu $at,$at,%got_ofst(target)
9755 That way, this always takes up eight bytes, and we can use
9756 the `.+x' form. Of course, these explicit machinations
9757 with relocation will not work with old assemblers. Then
9758 again, neither do out-of-range branches, so we haven't lost
9759 anything. */
9761 /* The target of the reversed branch. */
9762 const char *const target
9763 = ((mips_branch_likely || length == 20) ? ".+20" : ".+16");
9764 const char *at_register = mips_reg_names[ASSEMBLER_SCRATCH_REGNUM];
9765 const char *gp_register = mips_reg_names[PIC_OFFSET_TABLE_REGNUM];
9766 char *c;
9768 strcpy (buffer, "%(%<%[");
9769 c = strchr (buffer, '\0');
9770 /* Generate the reversed comparision. This takes four
9771 bytes. */
9772 if (float_p)
9773 sprintf (c, "%%*b%s\t%%Z2%s",
9774 inverted_p ? comp : inverted_comp,
9775 target);
9776 else
9777 sprintf (c, "%%*b%s%s\t%s%s,%s",
9778 inverted_p ? comp : inverted_comp,
9779 need_z_p ? "z" : "",
9780 op1,
9781 op2,
9782 target);
9783 c = strchr (buffer, '\0');
9784 /* Generate the load-address, and jump. This takes twelve
9785 bytes, for a total of 16. */
9786 sprintf (c,
9787 "\n\tlw\t%s,%%%%got_page(%%1)(%s)\n\tdaddiu\t%s,%s,%%%%got_ofst(%%1)\n\tjr\t%s",
9788 at_register,
9789 gp_register,
9790 at_register,
9791 at_register,
9792 at_register);
9793 if (length == 20)
9794 /* The delay slot was unfilled. Since we're inside
9795 .noreorder, the assembler will not fill in the NOP for
9796 us, so we must do it ourselves. */
9797 strcat (buffer, "\n\tnop");
9798 strcat (buffer, "%]%>%)");
9799 return buffer;
9801 #endif
9803 default:
9804 abort ();
9807 /* NOTREACHED */
9808 return 0;
9811 /* Called to register all of our global variables with the garbage
9812 collector. */
9814 static void
9815 mips_add_gc_roots ()
9817 ggc_add_rtx_root (&mips_load_reg, 1);
9818 ggc_add_rtx_root (&mips_load_reg2, 1);
9819 ggc_add_rtx_root (&mips_load_reg3, 1);
9820 ggc_add_rtx_root (&mips_load_reg4, 1);
9821 ggc_add_rtx_root (branch_cmp, sizeof (branch_cmp) / sizeof (rtx));
9822 ggc_add_rtx_root (&embedded_pic_fnaddr_rtx, 1);
9823 ggc_add_rtx_root (&mips16_gp_pseudo_rtx, 1);
9826 static enum processor_type
9827 mips_parse_cpu (cpu_string)
9828 const char *cpu_string;
9830 const char *p = cpu_string;
9831 int seen_v = 0;
9832 enum processor_type cpu;
9834 /* We need to cope with the various "vr" prefixes for the NEC 4300
9835 and 4100 processors. */
9836 if (*p == 'v' || *p == 'V')
9837 seen_v = 1, p++;
9839 if (*p == 'r' || *p == 'R')
9840 p++;
9842 /* Since there is no difference between a R2000 and R3000 in
9843 terms of the scheduler, we collapse them into just an R3000. */
9845 cpu = PROCESSOR_DEFAULT;
9846 switch (*p)
9848 case '2':
9849 if (!strcmp (p, "2000") || !strcmp (p, "2k") || !strcmp (p, "2K"))
9850 cpu = PROCESSOR_R3000;
9851 else if (!strcmp (p, "20kc") || !strcmp (p, "20Kc") )
9852 cpu = PROCESSOR_R20KC;
9853 break;
9855 case '3':
9856 if (!strcmp (p, "3000") || !strcmp (p, "3k") || !strcmp (p, "3K"))
9857 cpu = PROCESSOR_R3000;
9858 else if (!strcmp (p, "3900"))
9859 cpu = PROCESSOR_R3900;
9860 break;
9862 case '4':
9863 if (!strcmp (p, "4000") || !strcmp (p, "4k") || !strcmp (p, "4K"))
9864 cpu = PROCESSOR_R4000;
9865 /* The vr4100 is a non-FP ISA III processor with some extra
9866 instructions. */
9867 else if (!strcmp (p, "4100"))
9868 cpu = PROCESSOR_R4100;
9869 /* The vr4300 is a standard ISA III processor, but with a different
9870 pipeline. */
9871 else if (!strcmp (p, "4300"))
9872 cpu = PROCESSOR_R4300;
9873 /* The r4400 is exactly the same as the r4000 from the compiler's
9874 viewpoint. */
9875 else if (!strcmp (p, "4400"))
9876 cpu = PROCESSOR_R4000;
9877 else if (!strcmp (p, "4600"))
9878 cpu = PROCESSOR_R4600;
9879 else if (!strcmp (p, "4650"))
9880 cpu = PROCESSOR_R4650;
9881 /* The 4kc and 4kp processor cores are the same for
9882 scheduling purposes; they both implement the MIPS32
9883 ISA and only differ in their memory management
9884 methods. */
9885 else if (!strcmp (p, "4kc") || !strcmp (p, "4Kc")
9886 || !strcmp (p, "4kp") || !strcmp (p, "4Kp") )
9887 cpu = PROCESSOR_R4KC;
9888 break;
9890 case '5':
9891 if (!strcmp (p, "5000") || !strcmp (p, "5k") || !strcmp (p, "5K"))
9892 cpu = PROCESSOR_R5000;
9893 else if (!strcmp (p, "5kc") || !strcmp (p, "5Kc") )
9894 cpu = PROCESSOR_R5KC;
9895 break;
9897 case '6':
9898 if (!strcmp (p, "6000") || !strcmp (p, "6k") || !strcmp (p, "6K"))
9899 cpu = PROCESSOR_R6000;
9900 break;
9902 case '8':
9903 if (!strcmp (p, "8000"))
9904 cpu = PROCESSOR_R8000;
9905 break;
9907 case 'o':
9908 if (!strcmp (p, "orion"))
9909 cpu = PROCESSOR_R4600;
9910 break;
9913 if (seen_v
9914 && cpu != PROCESSOR_R4300
9915 && cpu != PROCESSOR_R4100
9916 && cpu != PROCESSOR_R5000)
9917 cpu = PROCESSOR_DEFAULT;
9919 return cpu;
9922 /* Adjust the cost of INSN based on the relationship between INSN that
9923 is dependent on DEP_INSN through the dependence LINK. The default
9924 is to make no adjustment to COST.
9926 On the MIPS, ignore the cost of anti- and output-dependencies. */
9927 static int
9928 mips_adjust_cost (insn, link, dep, cost)
9929 rtx insn ATTRIBUTE_UNUSED;
9930 rtx link;
9931 rtx dep ATTRIBUTE_UNUSED;
9932 int cost;
9934 if (REG_NOTE_KIND (link) != 0)
9935 return 0; /* Anti or output dependence. */
9936 return cost;
9939 /* Cover function for UNIQUE_SECTION. */
9941 void
9942 mips_unique_section (decl, reloc)
9943 tree decl;
9944 int reloc;
9946 int len, size, sec;
9947 const char *name, *prefix;
9948 char *string;
9949 static const char *const prefixes[4][2] = {
9950 { ".text.", ".gnu.linkonce.t." },
9951 { ".rodata.", ".gnu.linkonce.r." },
9952 { ".data.", ".gnu.linkonce.d." },
9953 { ".sdata.", ".gnu.linkonce.s." }
9956 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
9957 size = int_size_in_bytes (TREE_TYPE (decl));
9959 /* Determine the base section we are interested in:
9960 0=text, 1=rodata, 2=data, 3=sdata, [4=bss]. */
9961 if (TREE_CODE (decl) == FUNCTION_DECL)
9962 sec = 0;
9963 else if (DECL_INITIAL (decl) == 0
9964 || DECL_INITIAL (decl) == error_mark_node)
9965 sec = 2;
9966 else if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
9967 && TREE_CODE (decl) == STRING_CST
9968 && !flag_writable_strings)
9970 /* For embedded position independent code, put constant
9971 strings in the text section, because the data section
9972 is limited to 64K in size. For mips16 code, put
9973 strings in the text section so that a PC relative load
9974 instruction can be used to get their address. */
9975 sec = 0;
9977 else if (TARGET_EMBEDDED_DATA)
9979 /* For embedded applications, always put an object in
9980 read-only data if possible, in order to reduce RAM
9981 usage. */
9983 if (DECL_READONLY_SECTION (decl, reloc))
9984 sec = 1;
9985 else if (size > 0 && size <= mips_section_threshold)
9986 sec = 3;
9987 else
9988 sec = 2;
9990 else
9992 /* For hosted applications, always put an object in
9993 small data if possible, as this gives the best
9994 performance. */
9996 if (size > 0 && size <= mips_section_threshold)
9997 sec = 3;
9998 else if (DECL_READONLY_SECTION (decl, reloc))
9999 sec = 1;
10000 else
10001 sec = 2;
10004 prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
10005 len = strlen (name) + strlen (prefix);
10006 string = alloca (len + 1);
10007 sprintf (string, "%s%s", prefix, name);
10009 DECL_SECTION_NAME (decl) = build_string (len, string);
10012 unsigned int
10013 mips_hard_regno_nregs (regno, mode)
10014 int regno;
10015 enum machine_mode mode;
10017 if (! FP_REG_P (regno))
10018 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
10019 else
10020 return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
10024 #ifdef TARGET_IRIX6
10025 /* Output assembly to switch to section NAME with attribute FLAGS. */
10027 static void
10028 iris6_asm_named_section_1 (name, flags, align)
10029 const char *name;
10030 unsigned int flags;
10031 unsigned int align;
10033 unsigned int sh_type, sh_flags, sh_entsize;
10035 sh_flags = 0;
10036 if (!(flags & SECTION_DEBUG))
10037 sh_flags |= 2; /* SHF_ALLOC */
10038 if (flags & SECTION_WRITE)
10039 sh_flags |= 1; /* SHF_WRITE */
10040 if (flags & SECTION_CODE)
10041 sh_flags |= 4; /* SHF_EXECINSTR */
10042 if (flags & SECTION_SMALL)
10043 sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
10044 if (strcmp (name, ".debug_frame") == 0)
10045 sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
10046 if (flags & SECTION_DEBUG)
10047 sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
10048 else if (flags & SECTION_BSS)
10049 sh_type = 8; /* SHT_NOBITS */
10050 else
10051 sh_type = 1; /* SHT_PROGBITS */
10053 if (flags & SECTION_CODE)
10054 sh_entsize = 4;
10055 else
10056 sh_entsize = 0;
10058 fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
10059 name, sh_type, sh_flags, sh_entsize, align);
10062 static void
10063 iris6_asm_named_section (name, flags)
10064 const char *name;
10065 unsigned int flags;
10067 if (TARGET_FILE_SWITCHING && (flags & SECTION_CODE))
10068 asm_out_file = asm_out_text_file;
10069 iris6_asm_named_section_1 (name, flags, 0);
10072 /* In addition to emitting a .align directive, record the maximum
10073 alignment requested for the current section. */
10075 struct iris_section_align_entry
10077 const char *name;
10078 unsigned int log;
10079 unsigned int flags;
10082 static htab_t iris_section_align_htab;
10083 static FILE *iris_orig_asm_out_file;
10085 static int
10086 iris_section_align_entry_eq (p1, p2)
10087 const PTR p1;
10088 const PTR p2;
10090 const struct iris_section_align_entry *old = p1;
10091 const char *new = p2;
10093 return strcmp (old->name, new) == 0;
10096 static hashval_t
10097 iris_section_align_entry_hash (p)
10098 const PTR p;
10100 const struct iris_section_align_entry *old = p;
10101 return htab_hash_string (old->name);
10104 void
10105 iris6_asm_output_align (file, log)
10106 FILE *file;
10107 unsigned int log;
10109 const char *section = current_section_name ();
10110 struct iris_section_align_entry **slot, *entry;
10112 if (! section)
10113 abort ();
10115 slot = (struct iris_section_align_entry **)
10116 htab_find_slot_with_hash (iris_section_align_htab, section,
10117 htab_hash_string (section), INSERT);
10118 entry = *slot;
10119 if (! entry)
10121 entry = (struct iris_section_align_entry *)
10122 xmalloc (sizeof (struct iris_section_align_entry));
10123 *slot = entry;
10124 entry->name = section;
10125 entry->log = log;
10126 entry->flags = current_section_flags ();
10128 else if (entry->log < log)
10129 entry->log = log;
10131 fprintf (file, "\t.align\t%u\n", log);
10134 /* The Iris assembler does not record alignment from .align directives,
10135 but takes it from the first .section directive seen. Play yet more
10136 file switching games so that we can emit a .section directive at the
10137 beginning of the file with the proper alignment attached. */
10139 void
10140 iris6_asm_file_start (stream)
10141 FILE *stream;
10143 mips_asm_file_start (stream);
10145 iris_orig_asm_out_file = asm_out_file;
10146 stream = tmpfile ();
10147 asm_out_file = stream;
10148 asm_out_data_file = stream;
10149 if (! TARGET_FILE_SWITCHING)
10150 asm_out_text_file = stream;
10152 iris_section_align_htab = htab_create (31, iris_section_align_entry_hash,
10153 iris_section_align_entry_eq, NULL);
10156 static int
10157 iris6_section_align_1 (slot, data)
10158 void **slot;
10159 void *data ATTRIBUTE_UNUSED;
10161 const struct iris_section_align_entry *entry
10162 = *(const struct iris_section_align_entry **) slot;
10164 iris6_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
10165 return 1;
10168 void
10169 iris6_asm_file_end (stream)
10170 FILE *stream;
10172 /* Emit section directives with the proper alignment at the top of the
10173 real output file. */
10174 asm_out_file = iris_orig_asm_out_file;
10175 htab_traverse (iris_section_align_htab, iris6_section_align_1, NULL);
10177 /* Copy the data emitted to the temp file to the real output file. */
10178 copy_file_data (asm_out_file, stream);
10180 mips_asm_file_end (stream);
10182 #endif /* TARGET_IRIX6 */