* builtins.c (expand_builtin_setjmp_receiver): Const-ify.
[official-gcc.git] / gcc / config / mips / mips.c
blob9ff5be61106939ed0fbcefdae45729c918a502d4
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 __GNU_STAB__
58 #define STAB_CODE_TYPE enum __stab_debug_code
59 #else
60 #define STAB_CODE_TYPE int
61 #endif
63 extern tree lookup_name PARAMS ((tree));
65 /* Enumeration for all of the relational tests, so that we can build
66 arrays indexed by the test type, and not worry about the order
67 of EQ, NE, etc. */
69 enum internal_test {
70 ITEST_EQ,
71 ITEST_NE,
72 ITEST_GT,
73 ITEST_GE,
74 ITEST_LT,
75 ITEST_LE,
76 ITEST_GTU,
77 ITEST_GEU,
78 ITEST_LTU,
79 ITEST_LEU,
80 ITEST_MAX
84 struct constant;
85 static enum internal_test map_test_to_internal_test PARAMS ((enum rtx_code));
86 static int mips16_simple_memory_operand PARAMS ((rtx, rtx,
87 enum machine_mode));
88 static int m16_check_op PARAMS ((rtx, int, int, int));
89 static void block_move_loop PARAMS ((rtx, rtx,
90 unsigned int,
91 int,
92 rtx, rtx));
93 static void block_move_call PARAMS ((rtx, rtx, rtx));
94 static rtx mips_add_large_offset_to_sp PARAMS ((HOST_WIDE_INT,
95 FILE *));
96 static void mips_annotate_frame_insn PARAMS ((rtx, rtx));
97 static rtx mips_frame_set PARAMS ((enum machine_mode,
98 int, int));
99 static void mips_emit_frame_related_store PARAMS ((rtx, rtx,
100 HOST_WIDE_INT));
101 static void save_restore_insns PARAMS ((int, rtx,
102 long, FILE *));
103 static void mips16_output_gp_offset PARAMS ((FILE *, rtx));
104 static void mips16_fp_args PARAMS ((FILE *, int, int));
105 static void build_mips16_function_stub PARAMS ((FILE *));
106 static void mips16_optimize_gp PARAMS ((rtx));
107 static rtx add_constant PARAMS ((struct constant **,
108 rtx,
109 enum machine_mode));
110 static void dump_constants PARAMS ((struct constant *,
111 rtx));
112 static rtx mips_find_symbol PARAMS ((rtx));
113 static void abort_with_insn PARAMS ((rtx, const char *))
114 ATTRIBUTE_NORETURN;
115 static int symbolic_expression_p PARAMS ((rtx));
116 static void mips_add_gc_roots PARAMS ((void));
117 static void mips_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
118 static void mips_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
119 static enum processor_type mips_parse_cpu PARAMS ((const char *));
120 static void copy_file_data PARAMS ((FILE *, FILE *));
121 #ifdef TARGET_IRIX6
122 static void iris6_asm_named_section_1 PARAMS ((const char *,
123 unsigned int,
124 unsigned int));
125 static void iris6_asm_named_section PARAMS ((const char *,
126 unsigned int));
127 static int iris_section_align_entry_eq PARAMS ((const PTR, const PTR));
128 static hashval_t iris_section_align_entry_hash PARAMS ((const PTR));
129 static int iris6_section_align_1 PARAMS ((void **, void *));
130 #endif
131 static int mips_adjust_cost PARAMS ((rtx, rtx, rtx, int));
133 /* Global variables for machine-dependent things. */
135 /* Threshold for data being put into the small data/bss area, instead
136 of the normal data area (references to the small data/bss area take
137 1 instruction, and use the global pointer, references to the normal
138 data area takes 2 instructions). */
139 int mips_section_threshold = -1;
141 /* Count the number of .file directives, so that .loc is up to date. */
142 int num_source_filenames = 0;
144 /* Count the number of sdb related labels are generated (to find block
145 start and end boundaries). */
146 int sdb_label_count = 0;
148 /* Next label # for each statement for Silicon Graphics IRIS systems. */
149 int sym_lineno = 0;
151 /* Non-zero if inside of a function, because the stupid MIPS asm can't
152 handle .files inside of functions. */
153 int inside_function = 0;
155 /* Files to separate the text and the data output, so that all of the data
156 can be emitted before the text, which will mean that the assembler will
157 generate smaller code, based on the global pointer. */
158 FILE *asm_out_data_file;
159 FILE *asm_out_text_file;
161 /* Linked list of all externals that are to be emitted when optimizing
162 for the global pointer if they haven't been declared by the end of
163 the program with an appropriate .comm or initialization. */
165 struct extern_list
167 struct extern_list *next; /* next external */
168 const char *name; /* name of the external */
169 int size; /* size in bytes */
170 } *extern_head = 0;
172 /* Name of the file containing the current function. */
173 const char *current_function_file = "";
175 /* Warning given that Mips ECOFF can't support changing files
176 within a function. */
177 int file_in_function_warning = FALSE;
179 /* Whether to suppress issuing .loc's because the user attempted
180 to change the filename within a function. */
181 int ignore_line_number = FALSE;
183 /* Number of nested .set noreorder, noat, nomacro, and volatile requests. */
184 int set_noreorder;
185 int set_noat;
186 int set_nomacro;
187 int set_volatile;
189 /* The next branch instruction is a branch likely, not branch normal. */
190 int mips_branch_likely;
192 /* Count of delay slots and how many are filled. */
193 int dslots_load_total;
194 int dslots_load_filled;
195 int dslots_jump_total;
196 int dslots_jump_filled;
198 /* # of nops needed by previous insn */
199 int dslots_number_nops;
201 /* Number of 1/2/3 word references to data items (ie, not jal's). */
202 int num_refs[3];
204 /* registers to check for load delay */
205 rtx mips_load_reg, mips_load_reg2, mips_load_reg3, mips_load_reg4;
207 /* Cached operands, and operator to compare for use in set/branch/trap
208 on condition codes. */
209 rtx branch_cmp[2];
211 /* what type of branch to use */
212 enum cmp_type branch_type;
214 /* Number of previously seen half-pic pointers and references. */
215 static int prev_half_pic_ptrs = 0;
216 static int prev_half_pic_refs = 0;
218 /* The target cpu for code generation. */
219 enum processor_type mips_arch;
221 /* The target cpu for optimization and scheduling. */
222 enum processor_type mips_tune;
224 /* which instruction set architecture to use. */
225 int mips_isa;
227 #ifdef MIPS_ABI_DEFAULT
228 /* Which ABI to use. This is defined to a constant in mips.h if the target
229 doesn't support multiple ABIs. */
230 int mips_abi;
231 #endif
233 /* Strings to hold which cpu and instruction set architecture to use. */
234 const char *mips_cpu_string; /* for -mcpu=<xxx> */
235 const char *mips_arch_string; /* for -march=<xxx> */
236 const char *mips_tune_string; /* for -mtune=<xxx> */
237 const char *mips_isa_string; /* for -mips{1,2,3,4} */
238 const char *mips_abi_string; /* for -mabi={32,n32,64,eabi} */
240 /* Whether we are generating mips16 code. This is a synonym for
241 TARGET_MIPS16, and exists for use as an attribute. */
242 int mips16;
244 /* This variable is set by -mno-mips16. We only care whether
245 -mno-mips16 appears or not, and using a string in this fashion is
246 just a way to avoid using up another bit in target_flags. */
247 const char *mips_no_mips16_string;
249 /* This is only used to determine if an type size setting option was
250 explicitly specified (-mlong64, -mint64, -mlong32). The specs
251 set this option if such an option is used. */
252 const char *mips_explicit_type_size_string;
254 /* Whether we are generating mips16 hard float code. In mips16 mode
255 we always set TARGET_SOFT_FLOAT; this variable is nonzero if
256 -msoft-float was not specified by the user, which means that we
257 should arrange to call mips32 hard floating point code. */
258 int mips16_hard_float;
260 /* This variable is set by -mentry. We only care whether -mentry
261 appears or not, and using a string in this fashion is just a way to
262 avoid using up another bit in target_flags. */
263 const char *mips_entry_string;
265 /* Whether we should entry and exit pseudo-ops in mips16 mode. */
266 int mips_entry;
268 /* If TRUE, we split addresses into their high and low parts in the RTL. */
269 int mips_split_addresses;
271 /* Generating calls to position independent functions? */
272 enum mips_abicalls_type mips_abicalls;
274 /* High and low marks for floating point values which we will accept
275 as legitimate constants for LEGITIMATE_CONSTANT_P. These are
276 initialized in override_options. */
277 REAL_VALUE_TYPE dfhigh, dflow, sfhigh, sflow;
279 /* Mode used for saving/restoring general purpose registers. */
280 static enum machine_mode gpr_mode;
282 /* Array giving truth value on whether or not a given hard register
283 can support a given mode. */
284 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
286 /* Current frame information calculated by compute_frame_size. */
287 struct mips_frame_info current_frame_info;
289 /* Zero structure to initialize current_frame_info. */
290 struct mips_frame_info zero_frame_info;
292 /* Pseudo-reg holding the address of the current function when
293 generating embedded PIC code. Created by LEGITIMIZE_ADDRESS, used
294 by mips_finalize_pic if it was created. */
295 rtx embedded_pic_fnaddr_rtx;
297 /* The length of all strings seen when compiling for the mips16. This
298 is used to tell how many strings are in the constant pool, so that
299 we can see if we may have an overflow. This is reset each time the
300 constant pool is output. */
301 int mips_string_length;
303 /* Pseudo-reg holding the value of $28 in a mips16 function which
304 refers to GP relative global variables. */
305 rtx mips16_gp_pseudo_rtx;
307 /* In mips16 mode, we build a list of all the string constants we see
308 in a particular function. */
310 struct string_constant
312 struct string_constant *next;
313 const char *label;
316 static struct string_constant *string_constants;
318 /* List of all MIPS punctuation characters used by print_operand. */
319 char mips_print_operand_punct[256];
321 /* Map GCC register number to debugger register number. */
322 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
324 /* Buffer to use to enclose a load/store operation with %{ %} to
325 turn on .set volatile. */
326 static char volatile_buffer[60];
328 /* Hardware names for the registers. If -mrnames is used, this
329 will be overwritten with mips_sw_reg_names. */
331 char mips_reg_names[][8] =
333 "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7",
334 "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
335 "$16", "$17", "$18", "$19", "$20", "$21", "$22", "$23",
336 "$24", "$25", "$26", "$27", "$28", "$sp", "$fp", "$31",
337 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
338 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
339 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
340 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
341 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
342 "$fcc5","$fcc6","$fcc7","$rap"
345 /* Mips software names for the registers, used to overwrite the
346 mips_reg_names array. */
348 static const char mips_sw_reg_names[][8] =
350 "$zero","$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3",
351 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
352 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
353 "$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra",
354 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
355 "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
356 "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
357 "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
358 "hi", "lo", "accum","$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
359 "$fcc5","$fcc6","$fcc7","$rap"
362 /* Map hard register number to register class */
363 const enum reg_class mips_regno_to_class[] =
365 GR_REGS, GR_REGS, M16_NA_REGS, M16_NA_REGS,
366 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
367 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
368 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
369 M16_NA_REGS, M16_NA_REGS, GR_REGS, GR_REGS,
370 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
371 T_REG, GR_REGS, GR_REGS, GR_REGS,
372 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
373 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
374 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
375 FP_REGS, FP_REGS, FP_REGS, FP_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 HI_REG, LO_REG, HILO_REG, ST_REGS,
382 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
383 ST_REGS, ST_REGS, ST_REGS, GR_REGS
386 /* Map register constraint character to register class. */
387 enum reg_class mips_char_to_class[256] =
389 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
390 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
391 NO_REGS, NO_REGS, NO_REGS, NO_REGS,
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,
455 /* Initialize the GCC target structure. */
456 #undef TARGET_ASM_FUNCTION_PROLOGUE
457 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
458 #undef TARGET_ASM_FUNCTION_EPILOGUE
459 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
461 #undef TARGET_SCHED_ADJUST_COST
462 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
464 struct gcc_target targetm = TARGET_INITIALIZER;
466 /* Return truth value of whether OP can be used as an operands
467 where a register or 16 bit unsigned integer is needed. */
470 uns_arith_operand (op, mode)
471 rtx op;
472 enum machine_mode mode;
474 if (GET_CODE (op) == CONST_INT && SMALL_INT_UNSIGNED (op))
475 return 1;
477 return register_operand (op, mode);
480 /* Return truth value of whether OP can be used as an operands
481 where a 16 bit integer is needed */
484 arith_operand (op, mode)
485 rtx op;
486 enum machine_mode mode;
488 if (GET_CODE (op) == CONST_INT && SMALL_INT (op))
489 return 1;
491 /* On the mips16, a GP relative value is a signed 16 bit offset. */
492 if (TARGET_MIPS16 && GET_CODE (op) == CONST && mips16_gp_offset_p (op))
493 return 1;
495 return register_operand (op, mode);
498 /* Return truth value of whether OP can be used as an operand in a two
499 address arithmetic insn (such as set 123456,%o4) of mode MODE. */
502 arith32_operand (op, mode)
503 rtx op;
504 enum machine_mode mode;
506 if (GET_CODE (op) == CONST_INT)
507 return 1;
509 return register_operand (op, mode);
512 /* Return truth value of whether OP is a integer which fits in 16 bits */
515 small_int (op, mode)
516 rtx op;
517 enum machine_mode mode ATTRIBUTE_UNUSED;
519 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
522 /* Return truth value of whether OP is a 32 bit integer which is too big to
523 be loaded with one instruction. */
526 large_int (op, mode)
527 rtx op;
528 enum machine_mode mode ATTRIBUTE_UNUSED;
530 HOST_WIDE_INT value;
532 if (GET_CODE (op) != CONST_INT)
533 return 0;
535 value = INTVAL (op);
537 /* ior reg,$r0,value */
538 if ((value & ~ ((HOST_WIDE_INT) 0x0000ffff)) == 0)
539 return 0;
541 /* subu reg,$r0,value */
542 if (((unsigned HOST_WIDE_INT) (value + 32768)) <= 32767)
543 return 0;
545 /* lui reg,value>>16 */
546 if ((value & 0x0000ffff) == 0)
547 return 0;
549 return 1;
552 /* Return truth value of whether OP is a register or the constant 0.
553 In mips16 mode, we only accept a register, since the mips16 does
554 not have $0. */
557 reg_or_0_operand (op, mode)
558 rtx op;
559 enum machine_mode mode;
561 switch (GET_CODE (op))
563 case CONST_INT:
564 if (TARGET_MIPS16)
565 return 0;
566 return INTVAL (op) == 0;
568 case CONST_DOUBLE:
569 if (TARGET_MIPS16)
570 return 0;
571 return op == CONST0_RTX (mode);
573 case REG:
574 case SUBREG:
575 return register_operand (op, mode);
577 default:
578 break;
581 return 0;
584 /* Return truth value of whether OP is a register or the constant 0,
585 even in mips16 mode. */
588 true_reg_or_0_operand (op, mode)
589 rtx op;
590 enum machine_mode mode;
592 switch (GET_CODE (op))
594 case CONST_INT:
595 return INTVAL (op) == 0;
597 case CONST_DOUBLE:
598 return op == CONST0_RTX (mode);
600 case REG:
601 case SUBREG:
602 return register_operand (op, mode);
604 default:
605 break;
608 return 0;
611 /* Return truth value if a CONST_DOUBLE is ok to be a legitimate constant. */
614 mips_const_double_ok (op, mode)
615 rtx op;
616 enum machine_mode mode;
618 REAL_VALUE_TYPE d;
620 if (GET_CODE (op) != CONST_DOUBLE)
621 return 0;
623 if (mode == VOIDmode)
624 return 1;
626 if (mode != SFmode && mode != DFmode)
627 return 0;
629 if (op == CONST0_RTX (mode))
630 return 1;
632 /* ??? li.s does not work right with SGI's Irix 6 assembler. */
633 if (mips_abi != ABI_32 && mips_abi != ABI_O64 && mips_abi != ABI_EABI)
634 return 0;
636 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
638 if (REAL_VALUE_ISNAN (d))
639 return FALSE;
641 if (REAL_VALUE_NEGATIVE (d))
642 d = REAL_VALUE_NEGATE (d);
644 if (mode == DFmode)
646 if (REAL_VALUES_LESS (d, dfhigh)
647 && REAL_VALUES_LESS (dflow, d))
648 return 1;
650 else
652 if (REAL_VALUES_LESS (d, sfhigh)
653 && REAL_VALUES_LESS (sflow, d))
654 return 1;
657 return 0;
660 /* Accept the floating point constant 1 in the appropriate mode. */
663 const_float_1_operand (op, mode)
664 rtx op;
665 enum machine_mode mode;
667 REAL_VALUE_TYPE d;
668 static REAL_VALUE_TYPE onedf;
669 static REAL_VALUE_TYPE onesf;
670 static int one_initialized;
672 if (GET_CODE (op) != CONST_DOUBLE
673 || mode != GET_MODE (op)
674 || (mode != DFmode && mode != SFmode))
675 return 0;
677 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
679 /* We only initialize these values if we need them, since we will
680 never get called unless mips_isa >= 4. */
681 if (! one_initialized)
683 onedf = REAL_VALUE_ATOF ("1.0", DFmode);
684 onesf = REAL_VALUE_ATOF ("1.0", SFmode);
685 one_initialized = 1;
688 if (mode == DFmode)
689 return REAL_VALUES_EQUAL (d, onedf);
690 else
691 return REAL_VALUES_EQUAL (d, onesf);
694 /* Return true if a memory load or store of REG plus OFFSET in MODE
695 can be represented in a single word on the mips16. */
697 static int
698 mips16_simple_memory_operand (reg, offset, mode)
699 rtx reg;
700 rtx offset;
701 enum machine_mode mode;
703 unsigned int size;
704 int off;
706 if (mode == BLKmode)
708 /* We can't tell, because we don't know how the value will
709 eventually be accessed. Returning 0 here does no great
710 harm; it just prevents some possible instruction scheduling. */
711 return 0;
714 size = GET_MODE_SIZE (mode);
716 if (INTVAL (offset) % size != 0)
717 return 0;
718 if (REGNO (reg) == STACK_POINTER_REGNUM && GET_MODE_SIZE (mode) == 4)
719 off = 0x100;
720 else
721 off = 0x20;
722 if (INTVAL (offset) >= 0 && INTVAL (offset) < (HOST_WIDE_INT)(off * size))
723 return 1;
724 return 0;
727 /* Return truth value if a memory operand fits in a single instruction
728 (ie, register + small offset). */
731 simple_memory_operand (op, mode)
732 rtx op;
733 enum machine_mode mode;
735 rtx addr, plus0, plus1;
737 /* Eliminate non-memory operations */
738 if (GET_CODE (op) != MEM)
739 return 0;
741 /* dword operations really put out 2 instructions, so eliminate them. */
742 /* ??? This isn't strictly correct. It is OK to accept multiword modes
743 here, since the length attributes are being set correctly, but only
744 if the address is offsettable. LO_SUM is not offsettable. */
745 if (GET_MODE_SIZE (GET_MODE (op)) > (unsigned) UNITS_PER_WORD)
746 return 0;
748 /* Decode the address now. */
749 addr = XEXP (op, 0);
750 switch (GET_CODE (addr))
752 case REG:
753 case LO_SUM:
754 return 1;
756 case CONST_INT:
757 if (TARGET_MIPS16)
758 return 0;
759 return SMALL_INT (addr);
761 case PLUS:
762 plus0 = XEXP (addr, 0);
763 plus1 = XEXP (addr, 1);
764 if (GET_CODE (plus0) == REG
765 && GET_CODE (plus1) == CONST_INT && SMALL_INT (plus1)
766 && (! TARGET_MIPS16
767 || mips16_simple_memory_operand (plus0, plus1, mode)))
768 return 1;
770 else if (GET_CODE (plus1) == REG
771 && GET_CODE (plus0) == CONST_INT && SMALL_INT (plus0)
772 && (! TARGET_MIPS16
773 || mips16_simple_memory_operand (plus1, plus0, mode)))
774 return 1;
776 else
777 return 0;
779 #if 0
780 /* We used to allow small symbol refs here (ie, stuff in .sdata
781 or .sbss), but this causes some bugs in G++. Also, it won't
782 interfere if the MIPS linker rewrites the store instruction
783 because the function is PIC. */
785 case LABEL_REF: /* never gp relative */
786 break;
788 case CONST:
789 /* If -G 0, we can never have a GP relative memory operation.
790 Also, save some time if not optimizing. */
791 if (!TARGET_GP_OPT)
792 return 0;
795 rtx offset = const0_rtx;
796 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
797 if (GET_CODE (op) != SYMBOL_REF)
798 return 0;
800 /* let's be paranoid.... */
801 if (! SMALL_INT (offset))
802 return 0;
805 /* fall through */
807 case SYMBOL_REF:
808 return SYMBOL_REF_FLAG (addr);
809 #endif
811 /* This SYMBOL_REF case is for the mips16. If the above case is
812 reenabled, this one should be merged in. */
813 case SYMBOL_REF:
814 /* References to the constant pool on the mips16 use a small
815 offset if the function is small. The only time we care about
816 getting this right is during delayed branch scheduling, so
817 don't need to check until then. The machine_dependent_reorg
818 function will set the total length of the instructions used
819 in the function in current_frame_info. If that is small
820 enough, we know for sure that this is a small offset. It
821 would be better if we could take into account the location of
822 the instruction within the function, but we can't, because we
823 don't know where we are. */
824 if (TARGET_MIPS16
825 && CONSTANT_POOL_ADDRESS_P (addr)
826 && current_frame_info.insns_len > 0)
828 long size;
830 size = current_frame_info.insns_len + get_pool_size ();
831 if (GET_MODE_SIZE (mode) == 4)
832 return size < 4 * 0x100;
833 else if (GET_MODE_SIZE (mode) == 8)
834 return size < 8 * 0x20;
835 else
836 return 0;
839 return 0;
841 default:
842 break;
845 return 0;
848 /* Return nonzero for a memory address that can be used to load or store
849 a doubleword. */
852 double_memory_operand (op, mode)
853 rtx op;
854 enum machine_mode mode;
856 if (GET_CODE (op) != MEM
857 || ! memory_operand (op, mode))
859 /* During reload, we accept a pseudo register if it has an
860 appropriate memory address. If we don't do this, we will
861 wind up reloading into a register, and then reloading that
862 register from memory, when we could just reload directly from
863 memory. */
864 if (reload_in_progress
865 && GET_CODE (op) == REG
866 && REGNO (op) >= FIRST_PSEUDO_REGISTER
867 && reg_renumber[REGNO (op)] < 0
868 && reg_equiv_mem[REGNO (op)] != 0
869 && double_memory_operand (reg_equiv_mem[REGNO (op)], mode))
870 return 1;
872 /* All reloaded addresses are valid in TARGET_64BIT mode. This is
873 the same test performed for 'm' in find_reloads. */
875 if (reload_in_progress
876 && TARGET_64BIT
877 && (GET_CODE (op) == MEM
878 || (GET_CODE (op) == REG
879 && REGNO (op) >= FIRST_PSEUDO_REGISTER
880 && reg_renumber[REGNO (op)] < 0)))
881 return 1;
883 if (reload_in_progress
884 && TARGET_MIPS16
885 && GET_CODE (op) == MEM)
887 rtx addr;
889 addr = XEXP (op, 0);
891 /* During reload on the mips16, we accept a large offset
892 from the frame pointer or the stack pointer. This large
893 address will get reloaded anyhow. */
894 if (GET_CODE (addr) == PLUS
895 && GET_CODE (XEXP (addr, 0)) == REG
896 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
897 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
898 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
899 && ! SMALL_INT (XEXP (addr, 1)))
900 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
901 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
902 return 1;
904 /* Similarly, we accept a case where the memory address is
905 itself on the stack, and will be reloaded. */
906 if (GET_CODE (addr) == MEM)
908 rtx maddr;
910 maddr = XEXP (addr, 0);
911 if (GET_CODE (maddr) == PLUS
912 && GET_CODE (XEXP (maddr, 0)) == REG
913 && (REGNO (XEXP (maddr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
914 || REGNO (XEXP (maddr, 0)) == STACK_POINTER_REGNUM)
915 && ((GET_CODE (XEXP (maddr, 1)) == CONST_INT
916 && ! SMALL_INT (XEXP (maddr, 1)))
917 || (GET_CODE (XEXP (maddr, 1)) == SYMBOL_REF
918 && CONSTANT_POOL_ADDRESS_P (XEXP (maddr, 1)))))
919 return 1;
922 /* We also accept the same case when we have a 16 bit signed
923 offset mixed in as well. The large address will get
924 reloaded, and the 16 bit offset will be OK. */
925 if (GET_CODE (addr) == PLUS
926 && GET_CODE (XEXP (addr, 0)) == MEM
927 && GET_CODE (XEXP (addr, 1)) == CONST_INT
928 && SMALL_INT (XEXP (addr, 1)))
930 addr = XEXP (XEXP (addr, 0), 0);
931 if (GET_CODE (addr) == PLUS
932 && GET_CODE (XEXP (addr, 0)) == REG
933 && (REGNO (XEXP (addr, 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
934 || REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
935 && ((GET_CODE (XEXP (addr, 1)) == CONST_INT
936 && ! SMALL_INT (XEXP (addr, 1)))
937 || (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF
938 && CONSTANT_POOL_ADDRESS_P (XEXP (addr, 1)))))
939 return 1;
943 return 0;
946 if (TARGET_64BIT)
948 /* In this case we can use an instruction like sd. */
949 return 1;
952 /* Make sure that 4 added to the address is a valid memory address.
953 This essentially just checks for overflow in an added constant. */
955 if (CONSTANT_ADDRESS_P (XEXP (op, 0)))
956 return 1;
958 op = adjust_address_nv (op, GET_MODE_CLASS (mode) == MODE_INT
959 ? SImode : SFmode, 4);
960 return memory_address_p (GET_MODE (op), XEXP (op, 0));
963 /* Return nonzero if the code of this rtx pattern is EQ or NE. */
966 equality_op (op, mode)
967 rtx op;
968 enum machine_mode mode;
970 if (mode != GET_MODE (op))
971 return 0;
973 return GET_CODE (op) == EQ || GET_CODE (op) == NE;
976 /* Return nonzero if the code is a relational operations (EQ, LE, etc.) */
979 cmp_op (op, mode)
980 rtx op;
981 enum machine_mode mode;
983 if (mode != GET_MODE (op))
984 return 0;
986 return GET_RTX_CLASS (GET_CODE (op)) == '<';
989 /* Return nonzero if the code is a relational operation suitable for a
990 conditional trap instructuion (only EQ, NE, LT, LTU, GE, GEU).
991 We need this in the insn that expands `trap_if' in order to prevent
992 combine from erroneously altering the condition. */
995 trap_cmp_op (op, mode)
996 rtx op;
997 enum machine_mode mode;
999 if (mode != GET_MODE (op))
1000 return 0;
1002 switch (GET_CODE (op))
1004 case EQ:
1005 case NE:
1006 case LT:
1007 case LTU:
1008 case GE:
1009 case GEU:
1010 return 1;
1012 default:
1013 return 0;
1017 /* Return nonzero if the operand is either the PC or a label_ref. */
1020 pc_or_label_operand (op, mode)
1021 rtx op;
1022 enum machine_mode mode ATTRIBUTE_UNUSED;
1024 if (op == pc_rtx)
1025 return 1;
1027 if (GET_CODE (op) == LABEL_REF)
1028 return 1;
1030 return 0;
1033 /* Test for a valid operand for a call instruction.
1034 Don't allow the arg pointer register or virtual regs
1035 since they may change into reg + const, which the patterns
1036 can't handle yet. */
1039 call_insn_operand (op, mode)
1040 rtx op;
1041 enum machine_mode mode ATTRIBUTE_UNUSED;
1043 return (CONSTANT_ADDRESS_P (op)
1044 || (GET_CODE (op) == REG && op != arg_pointer_rtx
1045 && ! (REGNO (op) >= FIRST_PSEUDO_REGISTER
1046 && REGNO (op) <= LAST_VIRTUAL_REGISTER)));
1049 /* Return nonzero if OPERAND is valid as a source operand for a move
1050 instruction. */
1053 move_operand (op, mode)
1054 rtx op;
1055 enum machine_mode mode;
1057 /* Accept any general operand after reload has started; doing so
1058 avoids losing if reload does an in-place replacement of a register
1059 with a SYMBOL_REF or CONST. */
1060 return (general_operand (op, mode)
1061 && (! (mips_split_addresses && mips_check_split (op, mode))
1062 || reload_in_progress || reload_completed)
1063 && ! (TARGET_MIPS16
1064 && GET_CODE (op) == SYMBOL_REF
1065 && ! mips16_constant (op, mode, 1, 0)));
1068 /* Return nonzero if OPERAND is valid as a source operand for movdi.
1069 This accepts not only general_operand, but also sign extended
1070 constants and registers. We need to accept sign extended constants
1071 in case a sign extended register which is used in an expression,
1072 and is equivalent to a constant, is spilled. */
1075 movdi_operand (op, mode)
1076 rtx op;
1077 enum machine_mode mode;
1079 if (TARGET_64BIT
1080 && mode == DImode
1081 && GET_CODE (op) == SIGN_EXTEND
1082 && GET_MODE (op) == DImode
1083 && (GET_MODE (XEXP (op, 0)) == SImode
1084 || (GET_CODE (XEXP (op, 0)) == CONST_INT
1085 && GET_MODE (XEXP (op, 0)) == VOIDmode))
1086 && (register_operand (XEXP (op, 0), SImode)
1087 || immediate_operand (XEXP (op, 0), SImode)))
1088 return 1;
1090 return (general_operand (op, mode)
1091 && ! (TARGET_MIPS16
1092 && GET_CODE (op) == SYMBOL_REF
1093 && ! mips16_constant (op, mode, 1, 0)));
1096 /* Like register_operand, but when in 64 bit mode also accept a sign
1097 extend of a 32 bit register, since the value is known to be already
1098 sign extended. */
1101 se_register_operand (op, mode)
1102 rtx op;
1103 enum machine_mode mode;
1105 if (TARGET_64BIT
1106 && mode == DImode
1107 && GET_CODE (op) == SIGN_EXTEND
1108 && GET_MODE (op) == DImode
1109 && GET_MODE (XEXP (op, 0)) == SImode
1110 && register_operand (XEXP (op, 0), SImode))
1111 return 1;
1113 return register_operand (op, mode);
1116 /* Like reg_or_0_operand, but when in 64 bit mode also accept a sign
1117 extend of a 32 bit register, since the value is known to be already
1118 sign extended. */
1121 se_reg_or_0_operand (op, mode)
1122 rtx op;
1123 enum machine_mode mode;
1125 if (TARGET_64BIT
1126 && mode == DImode
1127 && GET_CODE (op) == SIGN_EXTEND
1128 && GET_MODE (op) == DImode
1129 && GET_MODE (XEXP (op, 0)) == SImode
1130 && register_operand (XEXP (op, 0), SImode))
1131 return 1;
1133 return reg_or_0_operand (op, mode);
1136 /* Like uns_arith_operand, but when in 64 bit mode also accept a sign
1137 extend of a 32 bit register, since the value is known to be already
1138 sign extended. */
1141 se_uns_arith_operand (op, mode)
1142 rtx op;
1143 enum machine_mode mode;
1145 if (TARGET_64BIT
1146 && mode == DImode
1147 && GET_CODE (op) == SIGN_EXTEND
1148 && GET_MODE (op) == DImode
1149 && GET_MODE (XEXP (op, 0)) == SImode
1150 && register_operand (XEXP (op, 0), SImode))
1151 return 1;
1153 return uns_arith_operand (op, mode);
1156 /* Like arith_operand, but when in 64 bit mode also accept a sign
1157 extend of a 32 bit register, since the value is known to be already
1158 sign extended. */
1161 se_arith_operand (op, mode)
1162 rtx op;
1163 enum machine_mode mode;
1165 if (TARGET_64BIT
1166 && mode == DImode
1167 && GET_CODE (op) == SIGN_EXTEND
1168 && GET_MODE (op) == DImode
1169 && GET_MODE (XEXP (op, 0)) == SImode
1170 && register_operand (XEXP (op, 0), SImode))
1171 return 1;
1173 return arith_operand (op, mode);
1176 /* Like nonmemory_operand, but when in 64 bit mode also accept a sign
1177 extend of a 32 bit register, since the value is known to be already
1178 sign extended. */
1181 se_nonmemory_operand (op, mode)
1182 rtx op;
1183 enum machine_mode mode;
1185 if (TARGET_64BIT
1186 && mode == DImode
1187 && GET_CODE (op) == SIGN_EXTEND
1188 && GET_MODE (op) == DImode
1189 && GET_MODE (XEXP (op, 0)) == SImode
1190 && register_operand (XEXP (op, 0), SImode))
1191 return 1;
1193 return nonmemory_operand (op, mode);
1196 /* Like nonimmediate_operand, but when in 64 bit mode also accept a
1197 sign extend of a 32 bit register, since the value is known to be
1198 already sign extended. */
1201 se_nonimmediate_operand (op, mode)
1202 rtx op;
1203 enum machine_mode mode;
1205 if (TARGET_64BIT
1206 && mode == DImode
1207 && GET_CODE (op) == SIGN_EXTEND
1208 && GET_MODE (op) == DImode
1209 && GET_MODE (XEXP (op, 0)) == SImode
1210 && register_operand (XEXP (op, 0), SImode))
1211 return 1;
1213 return nonimmediate_operand (op, mode);
1216 /* Accept any operand that can appear in a mips16 constant table
1217 instruction. We can't use any of the standard operand functions
1218 because for these instructions we accept values that are not
1219 accepted by LEGITIMATE_CONSTANT, such as arbitrary SYMBOL_REFs. */
1222 consttable_operand (op, mode)
1223 rtx op;
1224 enum machine_mode mode ATTRIBUTE_UNUSED;
1226 return CONSTANT_P (op);
1229 /* Return nonzero if we split the address into high and low parts. */
1231 /* ??? We should also handle reg+array somewhere. We get four
1232 instructions currently, lui %hi/addui %lo/addui reg/lw. Better is
1233 lui %hi/addui reg/lw %lo. Fixing GO_IF_LEGITIMATE_ADDRESS to accept
1234 (plus (reg) (symbol_ref)) doesn't work because the SYMBOL_REF is broken
1235 out of the address, then we have 4 instructions to combine. Perhaps
1236 add a 3->2 define_split for combine. */
1238 /* ??? We could also split a CONST_INT here if it is a large_int().
1239 However, it doesn't seem to be very useful to have %hi(constant).
1240 We would be better off by doing the masking ourselves and then putting
1241 the explicit high part of the constant in the RTL. This will give better
1242 optimization. Also, %hi(constant) needs assembler changes to work.
1243 There is already a define_split that does this. */
1246 mips_check_split (address, mode)
1247 rtx address;
1248 enum machine_mode mode;
1250 /* ??? This is the same check used in simple_memory_operand.
1251 We use it here because LO_SUM is not offsettable. */
1252 if (GET_MODE_SIZE (mode) > (unsigned) UNITS_PER_WORD)
1253 return 0;
1255 if ((GET_CODE (address) == SYMBOL_REF && ! SYMBOL_REF_FLAG (address))
1256 || (GET_CODE (address) == CONST
1257 && GET_CODE (XEXP (XEXP (address, 0), 0)) == SYMBOL_REF
1258 && ! SYMBOL_REF_FLAG (XEXP (XEXP (address, 0), 0)))
1259 || GET_CODE (address) == LABEL_REF)
1260 return 1;
1262 return 0;
1265 /* This function is used to implement REG_MODE_OK_FOR_BASE_P. */
1268 mips_reg_mode_ok_for_base_p (reg, mode, strict)
1269 rtx reg;
1270 enum machine_mode mode;
1271 int strict;
1273 return (strict
1274 ? REGNO_MODE_OK_FOR_BASE_P (REGNO (reg), mode)
1275 : GP_REG_OR_PSEUDO_NONSTRICT_P (REGNO (reg), mode));
1278 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS. It
1279 returns a nonzero value if XINSN is a legitimate address for a
1280 memory operand of the indicated MODE. STRICT is non-zero if this
1281 function is called during reload. */
1284 mips_legitimate_address_p (mode, xinsn, strict)
1285 enum machine_mode mode;
1286 rtx xinsn;
1287 int strict;
1289 if (TARGET_DEBUG_B_MODE)
1291 GO_PRINTF2 ("\n========== GO_IF_LEGITIMATE_ADDRESS, %sstrict\n",
1292 strict ? "" : "not ");
1293 GO_DEBUG_RTX (xinsn);
1296 /* Check for constant before stripping off SUBREG, so that we don't
1297 accept (subreg (const_int)) which will fail to reload. */
1298 if (CONSTANT_ADDRESS_P (xinsn)
1299 && ! (mips_split_addresses && mips_check_split (xinsn, mode))
1300 && (! TARGET_MIPS16 || mips16_constant (xinsn, mode, 1, 0)))
1301 return 1;
1303 while (GET_CODE (xinsn) == SUBREG)
1304 xinsn = SUBREG_REG (xinsn);
1306 /* The mips16 can only use the stack pointer as a base register when
1307 loading SImode or DImode values. */
1308 if (GET_CODE (xinsn) == REG
1309 && mips_reg_mode_ok_for_base_p (xinsn, mode, strict))
1310 return 1;
1312 if (GET_CODE (xinsn) == LO_SUM && mips_split_addresses)
1314 register rtx xlow0 = XEXP (xinsn, 0);
1315 register rtx xlow1 = XEXP (xinsn, 1);
1317 while (GET_CODE (xlow0) == SUBREG)
1318 xlow0 = SUBREG_REG (xlow0);
1319 if (GET_CODE (xlow0) == REG
1320 && mips_reg_mode_ok_for_base_p (xlow0, mode, strict)
1321 && mips_check_split (xlow1, mode))
1322 return 1;
1325 if (GET_CODE (xinsn) == PLUS)
1327 register rtx xplus0 = XEXP (xinsn, 0);
1328 register rtx xplus1 = XEXP (xinsn, 1);
1329 register enum rtx_code code0;
1330 register enum rtx_code code1;
1332 while (GET_CODE (xplus0) == SUBREG)
1333 xplus0 = SUBREG_REG (xplus0);
1334 code0 = GET_CODE (xplus0);
1336 while (GET_CODE (xplus1) == SUBREG)
1337 xplus1 = SUBREG_REG (xplus1);
1338 code1 = GET_CODE (xplus1);
1340 /* The mips16 can only use the stack pointer as a base register
1341 when loading SImode or DImode values. */
1342 if (code0 == REG
1343 && mips_reg_mode_ok_for_base_p (xplus0, mode, strict))
1345 if (code1 == CONST_INT && SMALL_INT (xplus1))
1346 return 1;
1348 /* On the mips16, we represent GP relative offsets in RTL.
1349 These are 16 bit signed values, and can serve as register
1350 offsets. */
1351 if (TARGET_MIPS16
1352 && mips16_gp_offset_p (xplus1))
1353 return 1;
1355 /* For some code sequences, you actually get better code by
1356 pretending that the MIPS supports an address mode of a
1357 constant address + a register, even though the real
1358 machine doesn't support it. This is because the
1359 assembler can use $r1 to load just the high 16 bits, add
1360 in the register, and fold the low 16 bits into the memory
1361 reference, whereas the compiler generates a 4 instruction
1362 sequence. On the other hand, CSE is not as effective.
1363 It would be a win to generate the lui directly, but the
1364 MIPS assembler does not have syntax to generate the
1365 appropriate relocation. */
1367 /* Also accept CONST_INT addresses here, so no else. */
1368 /* Reject combining an embedded PIC text segment reference
1369 with a register. That requires an additional
1370 instruction. */
1371 /* ??? Reject combining an address with a register for the MIPS
1372 64 bit ABI, because the SGI assembler can not handle this. */
1373 if (!TARGET_DEBUG_A_MODE
1374 && (mips_abi == ABI_32
1375 || mips_abi == ABI_O64
1376 || mips_abi == ABI_EABI)
1377 && CONSTANT_ADDRESS_P (xplus1)
1378 && ! mips_split_addresses
1379 && (!TARGET_EMBEDDED_PIC
1380 || code1 != CONST
1381 || GET_CODE (XEXP (xplus1, 0)) != MINUS)
1382 /* When assembling for machines with 64 bit registers,
1383 the assembler will sign-extend the constant "foo"
1384 in "la x, foo(x)" yielding the wrong result for:
1385 (set (blah:DI) (plus x y)). */
1386 && (!TARGET_64BIT
1387 || (code1 == CONST_INT
1388 && trunc_int_for_mode (INTVAL (xplus1),
1389 SImode) == INTVAL (xplus1)))
1390 && !TARGET_MIPS16)
1391 return 1;
1395 if (TARGET_DEBUG_B_MODE)
1396 GO_PRINTF ("Not a legitimate address\n");
1398 /* The address was not legitimate. */
1399 return 0;
1403 /* We need a lot of little routines to check constant values on the
1404 mips16. These are used to figure out how long the instruction will
1405 be. It would be much better to do this using constraints, but
1406 there aren't nearly enough letters available. */
1408 static int
1409 m16_check_op (op, low, high, mask)
1410 rtx op;
1411 int low;
1412 int high;
1413 int mask;
1415 return (GET_CODE (op) == CONST_INT
1416 && INTVAL (op) >= low
1417 && INTVAL (op) <= high
1418 && (INTVAL (op) & mask) == 0);
1422 m16_uimm3_b (op, mode)
1423 rtx op;
1424 enum machine_mode mode ATTRIBUTE_UNUSED;
1426 return m16_check_op (op, 0x1, 0x8, 0);
1430 m16_simm4_1 (op, mode)
1431 rtx op;
1432 enum machine_mode mode ATTRIBUTE_UNUSED;
1434 return m16_check_op (op, - 0x8, 0x7, 0);
1438 m16_nsimm4_1 (op, mode)
1439 rtx op;
1440 enum machine_mode mode ATTRIBUTE_UNUSED;
1442 return m16_check_op (op, - 0x7, 0x8, 0);
1446 m16_simm5_1 (op, mode)
1447 rtx op;
1448 enum machine_mode mode ATTRIBUTE_UNUSED;
1450 return m16_check_op (op, - 0x10, 0xf, 0);
1454 m16_nsimm5_1 (op, mode)
1455 rtx op;
1456 enum machine_mode mode ATTRIBUTE_UNUSED;
1458 return m16_check_op (op, - 0xf, 0x10, 0);
1462 m16_uimm5_4 (op, mode)
1463 rtx op;
1464 enum machine_mode mode ATTRIBUTE_UNUSED;
1466 return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
1470 m16_nuimm5_4 (op, mode)
1471 rtx op;
1472 enum machine_mode mode ATTRIBUTE_UNUSED;
1474 return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
1478 m16_simm8_1 (op, mode)
1479 rtx op;
1480 enum machine_mode mode ATTRIBUTE_UNUSED;
1482 return m16_check_op (op, - 0x80, 0x7f, 0);
1486 m16_nsimm8_1 (op, mode)
1487 rtx op;
1488 enum machine_mode mode ATTRIBUTE_UNUSED;
1490 return m16_check_op (op, - 0x7f, 0x80, 0);
1494 m16_uimm8_1 (op, mode)
1495 rtx op;
1496 enum machine_mode mode ATTRIBUTE_UNUSED;
1498 return m16_check_op (op, 0x0, 0xff, 0);
1502 m16_nuimm8_1 (op, mode)
1503 rtx op;
1504 enum machine_mode mode ATTRIBUTE_UNUSED;
1506 return m16_check_op (op, - 0xff, 0x0, 0);
1510 m16_uimm8_m1_1 (op, mode)
1511 rtx op;
1512 enum machine_mode mode ATTRIBUTE_UNUSED;
1514 return m16_check_op (op, - 0x1, 0xfe, 0);
1518 m16_uimm8_4 (op, mode)
1519 rtx op;
1520 enum machine_mode mode ATTRIBUTE_UNUSED;
1522 return m16_check_op (op, 0x0, 0xff << 2, 3);
1526 m16_nuimm8_4 (op, mode)
1527 rtx op;
1528 enum machine_mode mode ATTRIBUTE_UNUSED;
1530 return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
1534 m16_simm8_8 (op, mode)
1535 rtx op;
1536 enum machine_mode mode ATTRIBUTE_UNUSED;
1538 return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
1542 m16_nsimm8_8 (op, mode)
1543 rtx op;
1544 enum machine_mode mode ATTRIBUTE_UNUSED;
1546 return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
1549 /* References to the string table on the mips16 only use a small
1550 offset if the function is small. See the comment in the SYMBOL_REF
1551 case in simple_memory_operand. We can't check for LABEL_REF here,
1552 because the offset is always large if the label is before the
1553 referencing instruction. */
1556 m16_usym8_4 (op, mode)
1557 rtx op;
1558 enum machine_mode mode ATTRIBUTE_UNUSED;
1560 if (GET_CODE (op) == SYMBOL_REF
1561 && SYMBOL_REF_FLAG (op)
1562 && current_frame_info.insns_len > 0
1563 && XSTR (op, 0)[0] == '*'
1564 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1565 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1566 && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1567 < 4 * 0x100))
1569 struct string_constant *l;
1571 /* Make sure this symbol is on thelist of string constants to be
1572 output for this function. It is possible that it has already
1573 been output, in which case this requires a large offset. */
1574 for (l = string_constants; l != NULL; l = l->next)
1575 if (strcmp (l->label, XSTR (op, 0)) == 0)
1576 return 1;
1579 return 0;
1583 m16_usym5_4 (op, mode)
1584 rtx op;
1585 enum machine_mode mode ATTRIBUTE_UNUSED;
1587 if (GET_CODE (op) == SYMBOL_REF
1588 && SYMBOL_REF_FLAG (op)
1589 && current_frame_info.insns_len > 0
1590 && XSTR (op, 0)[0] == '*'
1591 && strncmp (XSTR (op, 0) + 1, LOCAL_LABEL_PREFIX,
1592 sizeof LOCAL_LABEL_PREFIX - 1) == 0
1593 && (current_frame_info.insns_len + get_pool_size () + mips_string_length
1594 < 4 * 0x20))
1596 struct string_constant *l;
1598 /* Make sure this symbol is on thelist of string constants to be
1599 output for this function. It is possible that it has already
1600 been output, in which case this requires a large offset. */
1601 for (l = string_constants; l != NULL; l = l->next)
1602 if (strcmp (l->label, XSTR (op, 0)) == 0)
1603 return 1;
1606 return 0;
1609 /* Returns an operand string for the given instruction's delay slot,
1610 after updating filled delay slot statistics.
1612 We assume that operands[0] is the target register that is set.
1614 In order to check the next insn, most of this functionality is moved
1615 to FINAL_PRESCAN_INSN, and we just set the global variables that
1616 it needs. */
1618 /* ??? This function no longer does anything useful, because final_prescan_insn
1619 now will never emit a nop. */
1621 const char *
1622 mips_fill_delay_slot (ret, type, operands, cur_insn)
1623 const char *ret; /* normal string to return */
1624 enum delay_type type; /* type of delay */
1625 rtx operands[]; /* operands to use */
1626 rtx cur_insn; /* current insn */
1628 register rtx set_reg;
1629 register enum machine_mode mode;
1630 register rtx next_insn = cur_insn ? NEXT_INSN (cur_insn) : NULL_RTX;
1631 register int num_nops;
1633 if (type == DELAY_LOAD || type == DELAY_FCMP)
1634 num_nops = 1;
1636 else if (type == DELAY_HILO)
1637 num_nops = 2;
1639 else
1640 num_nops = 0;
1642 /* Make sure that we don't put nop's after labels. */
1643 next_insn = NEXT_INSN (cur_insn);
1644 while (next_insn != 0 && GET_CODE (next_insn) == NOTE)
1645 next_insn = NEXT_INSN (next_insn);
1647 dslots_load_total += num_nops;
1648 if (TARGET_DEBUG_F_MODE
1649 || !optimize
1650 || type == DELAY_NONE
1651 || operands == 0
1652 || cur_insn == 0
1653 || next_insn == 0
1654 || GET_CODE (next_insn) == CODE_LABEL
1655 || (set_reg = operands[0]) == 0)
1657 dslots_number_nops = 0;
1658 mips_load_reg = 0;
1659 mips_load_reg2 = 0;
1660 mips_load_reg3 = 0;
1661 mips_load_reg4 = 0;
1662 return ret;
1665 set_reg = operands[0];
1666 if (set_reg == 0)
1667 return ret;
1669 while (GET_CODE (set_reg) == SUBREG)
1670 set_reg = SUBREG_REG (set_reg);
1672 mode = GET_MODE (set_reg);
1673 dslots_number_nops = num_nops;
1674 mips_load_reg = set_reg;
1675 if (GET_MODE_SIZE (mode)
1676 > (unsigned) (FP_REG_P (REGNO (set_reg)) ? UNITS_PER_FPREG : UNITS_PER_WORD))
1677 mips_load_reg2 = gen_rtx_REG (SImode, REGNO (set_reg) + 1);
1678 else
1679 mips_load_reg2 = 0;
1681 if (type == DELAY_HILO)
1683 mips_load_reg3 = gen_rtx_REG (SImode, MD_REG_FIRST);
1684 mips_load_reg4 = gen_rtx_REG (SImode, MD_REG_FIRST+1);
1686 else
1688 mips_load_reg3 = 0;
1689 mips_load_reg4 = 0;
1692 return ret;
1696 /* Determine whether a memory reference takes one (based off of the GP
1697 pointer), two (normal), or three (label + reg) instructions, and bump the
1698 appropriate counter for -mstats. */
1700 void
1701 mips_count_memory_refs (op, num)
1702 rtx op;
1703 int num;
1705 int additional = 0;
1706 int n_words = 0;
1707 rtx addr, plus0, plus1;
1708 enum rtx_code code0, code1;
1709 int looping;
1711 if (TARGET_DEBUG_B_MODE)
1713 fprintf (stderr, "\n========== mips_count_memory_refs:\n");
1714 debug_rtx (op);
1717 /* Skip MEM if passed, otherwise handle movsi of address. */
1718 addr = (GET_CODE (op) != MEM) ? op : XEXP (op, 0);
1720 /* Loop, going through the address RTL. */
1723 looping = FALSE;
1724 switch (GET_CODE (addr))
1726 case REG:
1727 case CONST_INT:
1728 case LO_SUM:
1729 break;
1731 case PLUS:
1732 plus0 = XEXP (addr, 0);
1733 plus1 = XEXP (addr, 1);
1734 code0 = GET_CODE (plus0);
1735 code1 = GET_CODE (plus1);
1737 if (code0 == REG)
1739 additional++;
1740 addr = plus1;
1741 looping = 1;
1742 continue;
1745 if (code0 == CONST_INT)
1747 addr = plus1;
1748 looping = 1;
1749 continue;
1752 if (code1 == REG)
1754 additional++;
1755 addr = plus0;
1756 looping = 1;
1757 continue;
1760 if (code1 == CONST_INT)
1762 addr = plus0;
1763 looping = 1;
1764 continue;
1767 if (code0 == SYMBOL_REF || code0 == LABEL_REF || code0 == CONST)
1769 addr = plus0;
1770 looping = 1;
1771 continue;
1774 if (code1 == SYMBOL_REF || code1 == LABEL_REF || code1 == CONST)
1776 addr = plus1;
1777 looping = 1;
1778 continue;
1781 break;
1783 case LABEL_REF:
1784 n_words = 2; /* always 2 words */
1785 break;
1787 case CONST:
1788 addr = XEXP (addr, 0);
1789 looping = 1;
1790 continue;
1792 case SYMBOL_REF:
1793 n_words = SYMBOL_REF_FLAG (addr) ? 1 : 2;
1794 break;
1796 default:
1797 break;
1800 while (looping);
1802 if (n_words == 0)
1803 return;
1805 n_words += additional;
1806 if (n_words > 3)
1807 n_words = 3;
1809 num_refs[n_words-1] += num;
1813 /* Return RTL for the offset from the current function to the argument.
1815 ??? Which argument is this? */
1818 embedded_pic_offset (x)
1819 rtx x;
1821 if (embedded_pic_fnaddr_rtx == NULL)
1823 rtx seq;
1825 embedded_pic_fnaddr_rtx = gen_reg_rtx (Pmode);
1827 /* Output code at function start to initialize the pseudo-reg. */
1828 /* ??? We used to do this in FINALIZE_PIC, but that does not work for
1829 inline functions, because it is called after RTL for the function
1830 has been copied. The pseudo-reg in embedded_pic_fnaddr_rtx however
1831 does not get copied, and ends up not matching the rest of the RTL.
1832 This solution works, but means that we get unnecessary code to
1833 initialize this value every time a function is inlined into another
1834 function. */
1835 start_sequence ();
1836 emit_insn (gen_get_fnaddr (embedded_pic_fnaddr_rtx,
1837 XEXP (DECL_RTL (current_function_decl), 0)));
1838 seq = gen_sequence ();
1839 end_sequence ();
1840 push_topmost_sequence ();
1841 emit_insn_after (seq, get_insns ());
1842 pop_topmost_sequence ();
1845 return
1846 gen_rtx_CONST (Pmode,
1847 gen_rtx_MINUS (Pmode, x,
1848 XEXP (DECL_RTL (current_function_decl), 0)));
1851 /* Return the appropriate instructions to move one operand to another. */
1853 const char *
1854 mips_move_1word (operands, insn, unsignedp)
1855 rtx operands[];
1856 rtx insn;
1857 int unsignedp;
1859 const char *ret = 0;
1860 rtx op0 = operands[0];
1861 rtx op1 = operands[1];
1862 enum rtx_code code0 = GET_CODE (op0);
1863 enum rtx_code code1 = GET_CODE (op1);
1864 enum machine_mode mode = GET_MODE (op0);
1865 int subreg_offset0 = 0;
1866 int subreg_offset1 = 0;
1867 enum delay_type delay = DELAY_NONE;
1869 while (code0 == SUBREG)
1871 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
1872 GET_MODE (SUBREG_REG (op0)),
1873 SUBREG_BYTE (op0),
1874 GET_MODE (op0));
1875 op0 = SUBREG_REG (op0);
1876 code0 = GET_CODE (op0);
1879 while (code1 == SUBREG)
1881 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
1882 GET_MODE (SUBREG_REG (op1)),
1883 SUBREG_BYTE (op1),
1884 GET_MODE (op1));
1885 op1 = SUBREG_REG (op1);
1886 code1 = GET_CODE (op1);
1889 /* For our purposes, a condition code mode is the same as SImode. */
1890 if (mode == CCmode)
1891 mode = SImode;
1893 if (code0 == REG)
1895 int regno0 = REGNO (op0) + subreg_offset0;
1897 if (code1 == REG)
1899 int regno1 = REGNO (op1) + subreg_offset1;
1901 /* Just in case, don't do anything for assigning a register
1902 to itself, unless we are filling a delay slot. */
1903 if (regno0 == regno1 && set_nomacro == 0)
1904 ret = "";
1906 else if (GP_REG_P (regno0))
1908 if (GP_REG_P (regno1))
1909 ret = "move\t%0,%1";
1911 else if (MD_REG_P (regno1))
1913 delay = DELAY_HILO;
1914 if (regno1 != HILO_REGNUM)
1915 ret = "mf%1\t%0";
1916 else
1917 ret = "mflo\t%0";
1920 else if (ST_REG_P (regno1) && ISA_HAS_8CC)
1921 ret = "li\t%0,1\n\tmovf\t%0,%.,%1";
1923 else
1925 delay = DELAY_LOAD;
1926 if (FP_REG_P (regno1))
1927 ret = "mfc1\t%0,%1";
1929 else if (regno1 == FPSW_REGNUM && ! ISA_HAS_8CC)
1930 ret = "cfc1\t%0,$31";
1934 else if (FP_REG_P (regno0))
1936 if (GP_REG_P (regno1))
1938 delay = DELAY_LOAD;
1939 ret = "mtc1\t%1,%0";
1942 if (FP_REG_P (regno1))
1943 ret = "mov.s\t%0,%1";
1946 else if (MD_REG_P (regno0))
1948 if (GP_REG_P (regno1))
1950 delay = DELAY_HILO;
1951 if (regno0 != HILO_REGNUM && ! TARGET_MIPS16)
1952 ret = "mt%0\t%1";
1956 else if (regno0 == FPSW_REGNUM && ! ISA_HAS_8CC)
1958 if (GP_REG_P (regno1))
1960 delay = DELAY_LOAD;
1961 ret = "ctc1\t%0,$31";
1966 else if (code1 == MEM)
1968 delay = DELAY_LOAD;
1970 if (TARGET_STATS)
1971 mips_count_memory_refs (op1, 1);
1973 if (GP_REG_P (regno0))
1975 /* For loads, use the mode of the memory item, instead of the
1976 target, so zero/sign extend can use this code as well. */
1977 switch (GET_MODE (op1))
1979 default:
1980 break;
1981 case SFmode:
1982 ret = "lw\t%0,%1";
1983 break;
1984 case SImode:
1985 case CCmode:
1986 ret = ((unsignedp && TARGET_64BIT)
1987 ? "lwu\t%0,%1"
1988 : "lw\t%0,%1");
1989 break;
1990 case HImode:
1991 ret = (unsignedp) ? "lhu\t%0,%1" : "lh\t%0,%1";
1992 break;
1993 case QImode:
1994 ret = (unsignedp) ? "lbu\t%0,%1" : "lb\t%0,%1";
1995 break;
1999 else if (FP_REG_P (regno0) && (mode == SImode || mode == SFmode))
2000 ret = "l.s\t%0,%1";
2002 if (ret != (char *)0 && MEM_VOLATILE_P (op1))
2004 size_t i = strlen (ret);
2005 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2006 abort ();
2008 sprintf (volatile_buffer, "%%{%s%%}", ret);
2009 ret = volatile_buffer;
2013 else if (code1 == CONST_INT
2014 || (code1 == CONST_DOUBLE
2015 && GET_MODE (op1) == VOIDmode))
2017 if (code1 == CONST_DOUBLE)
2019 /* This can happen when storing constants into long long
2020 bitfields. Just store the least significant word of
2021 the value. */
2022 operands[1] = op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2025 if (INTVAL (op1) == 0 && ! TARGET_MIPS16)
2027 if (GP_REG_P (regno0))
2028 ret = "move\t%0,%z1";
2030 else if (FP_REG_P (regno0))
2032 delay = DELAY_LOAD;
2033 ret = "mtc1\t%z1,%0";
2036 else if (MD_REG_P (regno0))
2038 delay = DELAY_HILO;
2039 ret = "mt%0\t%.";
2043 else if (GP_REG_P (regno0))
2045 /* Don't use X format, because that will give out of
2046 range numbers for 64 bit host and 32 bit target. */
2047 if (! TARGET_MIPS16)
2048 ret = "li\t%0,%1\t\t\t# %X1";
2049 else
2051 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2052 ret = "li\t%0,%1";
2053 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2054 ret = "li\t%0,%n1\n\tneg\t%0";
2059 else if (code1 == CONST_DOUBLE && mode == SFmode)
2061 if (op1 == CONST0_RTX (SFmode))
2063 if (GP_REG_P (regno0))
2064 ret = "move\t%0,%.";
2066 else if (FP_REG_P (regno0))
2068 delay = DELAY_LOAD;
2069 ret = "mtc1\t%.,%0";
2073 else
2075 delay = DELAY_LOAD;
2076 ret = "li.s\t%0,%1";
2080 else if (code1 == LABEL_REF)
2082 if (TARGET_STATS)
2083 mips_count_memory_refs (op1, 1);
2085 ret = "la\t%0,%a1";
2088 else if (code1 == SYMBOL_REF || code1 == CONST)
2090 if (HALF_PIC_P () && CONSTANT_P (op1) && HALF_PIC_ADDRESS_P (op1))
2092 rtx offset = const0_rtx;
2094 if (GET_CODE (op1) == CONST)
2095 op1 = eliminate_constant_term (XEXP (op1, 0), &offset);
2097 if (GET_CODE (op1) == SYMBOL_REF)
2099 operands[2] = HALF_PIC_PTR (op1);
2101 if (TARGET_STATS)
2102 mips_count_memory_refs (operands[2], 1);
2104 if (INTVAL (offset) == 0)
2106 delay = DELAY_LOAD;
2107 ret = (unsignedp && TARGET_64BIT
2108 ? "lwu\t%0,%2"
2109 : "lw\t%0,%2");
2111 else
2113 dslots_load_total++;
2114 operands[3] = offset;
2115 if (unsignedp && TARGET_64BIT)
2116 ret = (SMALL_INT (offset)
2117 ? "lwu\t%0,%2%#\n\tadd\t%0,%0,%3"
2118 : "lwu\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
2119 else
2120 ret = (SMALL_INT (offset)
2121 ? "lw\t%0,%2%#\n\tadd\t%0,%0,%3"
2122 : "lw\t%0,%2%#\n\t%[li\t%@,%3\n\tadd\t%0,%0,%@%]");
2126 else if (TARGET_MIPS16
2127 && code1 == CONST
2128 && GET_CODE (XEXP (op1, 0)) == REG
2129 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2131 /* This case arises on the mips16; see
2132 mips16_gp_pseudo_reg. */
2133 ret = "move\t%0,%+";
2135 else if (TARGET_MIPS16
2136 && code1 == SYMBOL_REF
2137 && SYMBOL_REF_FLAG (op1)
2138 && (XSTR (op1, 0)[0] != '*'
2139 || strncmp (XSTR (op1, 0) + 1,
2140 LOCAL_LABEL_PREFIX,
2141 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2143 /* This can occur when reloading the address of a GP
2144 relative symbol on the mips16. */
2145 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2147 else
2149 if (TARGET_STATS)
2150 mips_count_memory_refs (op1, 1);
2152 ret = "la\t%0,%a1";
2156 else if (code1 == PLUS)
2158 rtx add_op0 = XEXP (op1, 0);
2159 rtx add_op1 = XEXP (op1, 1);
2161 if (GET_CODE (XEXP (op1, 1)) == REG
2162 && GET_CODE (XEXP (op1, 0)) == CONST_INT)
2163 add_op0 = XEXP (op1, 1), add_op1 = XEXP (op1, 0);
2165 operands[2] = add_op0;
2166 operands[3] = add_op1;
2167 ret = "add%:\t%0,%2,%3";
2170 else if (code1 == HIGH)
2172 operands[1] = XEXP (op1, 0);
2173 ret = "lui\t%0,%%hi(%1)";
2177 else if (code0 == MEM)
2179 if (TARGET_STATS)
2180 mips_count_memory_refs (op0, 1);
2182 if (code1 == REG)
2184 int regno1 = REGNO (op1) + subreg_offset1;
2186 if (GP_REG_P (regno1))
2188 switch (mode)
2190 case SFmode: ret = "sw\t%1,%0"; break;
2191 case SImode: ret = "sw\t%1,%0"; break;
2192 case HImode: ret = "sh\t%1,%0"; break;
2193 case QImode: ret = "sb\t%1,%0"; break;
2194 default: break;
2198 else if (FP_REG_P (regno1) && (mode == SImode || mode == SFmode))
2199 ret = "s.s\t%1,%0";
2202 else if (code1 == CONST_INT && INTVAL (op1) == 0)
2204 switch (mode)
2206 case SFmode: ret = "sw\t%z1,%0"; break;
2207 case SImode: ret = "sw\t%z1,%0"; break;
2208 case HImode: ret = "sh\t%z1,%0"; break;
2209 case QImode: ret = "sb\t%z1,%0"; break;
2210 default: break;
2214 else if (code1 == CONST_DOUBLE && op1 == CONST0_RTX (mode))
2216 switch (mode)
2218 case SFmode: ret = "sw\t%.,%0"; break;
2219 case SImode: ret = "sw\t%.,%0"; break;
2220 case HImode: ret = "sh\t%.,%0"; break;
2221 case QImode: ret = "sb\t%.,%0"; break;
2222 default: break;
2226 if (ret != 0 && MEM_VOLATILE_P (op0))
2228 size_t i = strlen (ret);
2230 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2231 abort ();
2233 sprintf (volatile_buffer, "%%{%s%%}", ret);
2234 ret = volatile_buffer;
2238 if (ret == 0)
2240 abort_with_insn (insn, "Bad move");
2241 return 0;
2244 if (delay != DELAY_NONE)
2245 return mips_fill_delay_slot (ret, delay, operands, insn);
2247 return ret;
2251 /* Return the appropriate instructions to move 2 words */
2253 const char *
2254 mips_move_2words (operands, insn)
2255 rtx operands[];
2256 rtx insn;
2258 const char *ret = 0;
2259 rtx op0 = operands[0];
2260 rtx op1 = operands[1];
2261 enum rtx_code code0 = GET_CODE (operands[0]);
2262 enum rtx_code code1 = GET_CODE (operands[1]);
2263 int subreg_offset0 = 0;
2264 int subreg_offset1 = 0;
2265 enum delay_type delay = DELAY_NONE;
2267 while (code0 == SUBREG)
2269 subreg_offset0 += subreg_regno_offset (REGNO (SUBREG_REG (op0)),
2270 GET_MODE (SUBREG_REG (op0)),
2271 SUBREG_BYTE (op0),
2272 GET_MODE (op0));
2273 op0 = SUBREG_REG (op0);
2274 code0 = GET_CODE (op0);
2277 if (code1 == SIGN_EXTEND)
2279 op1 = XEXP (op1, 0);
2280 code1 = GET_CODE (op1);
2283 while (code1 == SUBREG)
2285 subreg_offset1 += subreg_regno_offset (REGNO (SUBREG_REG (op1)),
2286 GET_MODE (SUBREG_REG (op1)),
2287 SUBREG_BYTE (op1),
2288 GET_MODE (op1));
2289 op1 = SUBREG_REG (op1);
2290 code1 = GET_CODE (op1);
2293 /* Sanity check. */
2294 if (GET_CODE (operands[1]) == SIGN_EXTEND
2295 && code1 != REG
2296 && code1 != CONST_INT
2297 /* The following three can happen as the result of a questionable
2298 cast. */
2299 && code1 != LABEL_REF
2300 && code1 != SYMBOL_REF
2301 && code1 != CONST)
2302 abort ();
2304 if (code0 == REG)
2306 int regno0 = REGNO (op0) + subreg_offset0;
2308 if (code1 == REG)
2310 int regno1 = REGNO (op1) + subreg_offset1;
2312 /* Just in case, don't do anything for assigning a register
2313 to itself, unless we are filling a delay slot. */
2314 if (regno0 == regno1 && set_nomacro == 0)
2315 ret = "";
2317 else if (FP_REG_P (regno0))
2319 if (FP_REG_P (regno1))
2320 ret = "mov.d\t%0,%1";
2322 else
2324 delay = DELAY_LOAD;
2325 if (TARGET_FLOAT64)
2327 if (!TARGET_64BIT)
2328 abort_with_insn (insn, "Bad move");
2330 #ifdef TARGET_FP_CALL_32
2331 if (FP_CALL_GP_REG_P (regno1))
2332 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tdmtc1\t%1,%0";
2333 else
2334 #endif
2335 ret = "dmtc1\t%1,%0";
2337 else
2338 ret = "mtc1\t%L1,%0\n\tmtc1\t%M1,%D0";
2342 else if (FP_REG_P (regno1))
2344 delay = DELAY_LOAD;
2345 if (TARGET_FLOAT64)
2347 if (!TARGET_64BIT)
2348 abort_with_insn (insn, "Bad move");
2350 #ifdef TARGET_FP_CALL_32
2351 if (FP_CALL_GP_REG_P (regno0))
2352 ret = "dmfc1\t%0,%1\n\tmfc1\t%D0,%1\n\tdsrl\t%0,32";
2353 else
2354 #endif
2355 ret = "dmfc1\t%0,%1";
2357 else
2358 ret = "mfc1\t%L0,%1\n\tmfc1\t%M0,%D1";
2361 else if (MD_REG_P (regno0) && GP_REG_P (regno1) && !TARGET_MIPS16)
2363 delay = DELAY_HILO;
2364 if (TARGET_64BIT)
2366 if (regno0 != HILO_REGNUM)
2367 ret = "mt%0\t%1";
2368 else if (regno1 == 0)
2369 ret = "mtlo\t%.\n\tmthi\t%.";
2371 else
2372 ret = "mthi\t%M1\n\tmtlo\t%L1";
2375 else if (GP_REG_P (regno0) && MD_REG_P (regno1))
2377 delay = DELAY_HILO;
2378 if (TARGET_64BIT)
2380 if (regno1 != HILO_REGNUM)
2381 ret = "mf%1\t%0";
2383 else
2384 ret = "mfhi\t%M0\n\tmflo\t%L0";
2387 else if (TARGET_64BIT)
2388 ret = "move\t%0,%1";
2390 else if (regno0 != (regno1+1))
2391 ret = "move\t%0,%1\n\tmove\t%D0,%D1";
2393 else
2394 ret = "move\t%D0,%D1\n\tmove\t%0,%1";
2397 else if (code1 == CONST_DOUBLE)
2399 /* Move zero from $0 unless !TARGET_64BIT and recipient
2400 is 64-bit fp reg, in which case generate a constant. */
2401 if (op1 != CONST0_RTX (GET_MODE (op1))
2402 || (TARGET_FLOAT64 && !TARGET_64BIT && FP_REG_P (regno0)))
2404 if (GET_MODE (op1) == DFmode)
2406 delay = DELAY_LOAD;
2408 #ifdef TARGET_FP_CALL_32
2409 if (FP_CALL_GP_REG_P (regno0))
2411 if (TARGET_FLOAT64 && !TARGET_64BIT)
2413 split_double (op1, operands + 2, operands + 3);
2414 ret = "li\t%0,%2\n\tli\t%D0,%3";
2416 else
2417 ret = "li.d\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32";
2419 else
2420 #endif
2421 /* GNU as emits 64-bit code for li.d if the ISA is 3
2422 or higher. For !TARGET_64BIT && gp registers we
2423 need to avoid this by using two li instructions
2424 instead. */
2425 if (ISA_HAS_64BIT_REGS
2426 && ! TARGET_64BIT
2427 && ! FP_REG_P (regno0))
2429 split_double (op1, operands + 2, operands + 3);
2430 ret = "li\t%0,%2\n\tli\t%D0,%3";
2432 else
2433 ret = "li.d\t%0,%1";
2436 else if (TARGET_64BIT)
2438 if (! TARGET_MIPS16)
2439 ret = "dli\t%0,%1";
2442 else
2444 split_double (op1, operands + 2, operands + 3);
2445 ret = "li\t%0,%2\n\tli\t%D0,%3";
2449 else
2451 if (GP_REG_P (regno0))
2452 ret = (TARGET_64BIT
2453 #ifdef TARGET_FP_CALL_32
2454 && ! FP_CALL_GP_REG_P (regno0)
2455 #endif
2456 ? "move\t%0,%."
2457 : "move\t%0,%.\n\tmove\t%D0,%.");
2459 else if (FP_REG_P (regno0))
2461 delay = DELAY_LOAD;
2462 ret = (TARGET_64BIT
2463 ? "dmtc1\t%.,%0"
2464 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0");
2469 else if (code1 == CONST_INT && INTVAL (op1) == 0 && ! TARGET_MIPS16)
2471 if (GP_REG_P (regno0))
2472 ret = (TARGET_64BIT
2473 ? "move\t%0,%."
2474 : "move\t%0,%.\n\tmove\t%D0,%.");
2476 else if (FP_REG_P (regno0))
2478 delay = DELAY_LOAD;
2479 ret = (TARGET_64BIT
2480 ? "dmtc1\t%.,%0"
2481 : (TARGET_FLOAT64
2482 ? "li.d\t%0,%1"
2483 : "mtc1\t%.,%0\n\tmtc1\t%.,%D0"));
2485 else if (MD_REG_P (regno0))
2487 delay = DELAY_HILO;
2488 ret = (regno0 == HILO_REGNUM
2489 ? "mtlo\t%.\n\tmthi\t%."
2490 : "mt%0\t%.\n");
2494 else if (code1 == CONST_INT && GET_MODE (op0) == DImode
2495 && GP_REG_P (regno0))
2497 if (TARGET_64BIT)
2499 if (TARGET_MIPS16)
2501 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2502 ret = "li\t%0,%1";
2503 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2504 ret = "li\t%0,%n1\n\tneg\t%0";
2506 else if (GET_CODE (operands[1]) == SIGN_EXTEND)
2507 ret = "li\t%0,%1\t\t# %X1";
2508 else if (HOST_BITS_PER_WIDE_INT < 64)
2509 /* We can't use 'X' for negative numbers, because then we won't
2510 get the right value for the upper 32 bits. */
2511 ret = (INTVAL (op1) < 0
2512 ? "dli\t%0,%1\t\t\t# %X1"
2513 : "dli\t%0,%X1\t\t# %1");
2514 else
2515 /* We must use 'X', because otherwise LONG_MIN will print as
2516 a number that the assembler won't accept. */
2517 ret = "dli\t%0,%X1\t\t# %1";
2519 else if (HOST_BITS_PER_WIDE_INT < 64)
2521 operands[2] = GEN_INT (INTVAL (operands[1]) >= 0 ? 0 : -1);
2522 if (TARGET_MIPS16)
2524 if (INTVAL (op1) >= 0 && INTVAL (op1) <= 0xffff)
2525 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2526 else if (INTVAL (op1) < 0 && INTVAL (op1) >= -0xffff)
2528 operands[2] = GEN_INT (1);
2529 ret = "li\t%M0,%2\n\tneg\t%M0\n\tli\t%L0,%n1\n\tneg\t%L0";
2532 else
2533 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2535 else
2537 /* We use multiple shifts here, to avoid warnings about out
2538 of range shifts on 32 bit hosts. */
2539 operands[2] = GEN_INT (INTVAL (operands[1]) >> 16 >> 16);
2540 operands[1]
2541 = GEN_INT (INTVAL (operands[1]) << 16 << 16 >> 16 >> 16);
2542 ret = "li\t%M0,%2\n\tli\t%L0,%1";
2546 else if (code1 == MEM)
2548 delay = DELAY_LOAD;
2550 if (TARGET_STATS)
2551 mips_count_memory_refs (op1, 2);
2553 if (FP_REG_P (regno0))
2554 ret = "l.d\t%0,%1";
2556 else if (TARGET_64BIT)
2559 #ifdef TARGET_FP_CALL_32
2560 if (FP_CALL_GP_REG_P (regno0))
2561 ret = (double_memory_operand (op1, GET_MODE (op1))
2562 ? "lwu\t%0,%1\n\tlwu\t%D0,4+%1"
2563 : "ld\t%0,%1\n\tdsll\t%D0,%0,32\n\tdsrl\t%D0,32\n\tdsrl\t%0,32");
2564 else
2565 #endif
2566 ret = "ld\t%0,%1";
2569 else if (double_memory_operand (op1, GET_MODE (op1)))
2570 ret = (reg_mentioned_p (op0, op1)
2571 ? "lw\t%D0,%D1\n\tlw\t%0,%1"
2572 : "lw\t%0,%1\n\tlw\t%D0,%D1");
2574 if (ret != 0 && MEM_VOLATILE_P (op1))
2576 size_t i = strlen (ret);
2578 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2579 abort ();
2581 sprintf (volatile_buffer, "%%{%s%%}", ret);
2582 ret = volatile_buffer;
2586 else if (code1 == LABEL_REF)
2588 if (TARGET_STATS)
2589 mips_count_memory_refs (op1, 2);
2591 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2592 /* We deliberately remove the 'a' from '%1', so that we don't
2593 have to add SIGN_EXTEND support to print_operand_address.
2594 print_operand will just call print_operand_address in this
2595 case, so there is no problem. */
2596 ret = "la\t%0,%1";
2597 else
2598 ret = "dla\t%0,%a1";
2600 else if (code1 == SYMBOL_REF || code1 == CONST)
2602 if (TARGET_MIPS16
2603 && code1 == CONST
2604 && GET_CODE (XEXP (op1, 0)) == REG
2605 && REGNO (XEXP (op1, 0)) == GP_REG_FIRST + 28)
2607 /* This case arises on the mips16; see
2608 mips16_gp_pseudo_reg. */
2609 ret = "move\t%0,%+";
2611 else if (TARGET_MIPS16
2612 && code1 == SYMBOL_REF
2613 && SYMBOL_REF_FLAG (op1)
2614 && (XSTR (op1, 0)[0] != '*'
2615 || strncmp (XSTR (op1, 0) + 1,
2616 LOCAL_LABEL_PREFIX,
2617 sizeof LOCAL_LABEL_PREFIX - 1) != 0))
2619 /* This can occur when reloading the address of a GP
2620 relative symbol on the mips16. */
2621 ret = "move\t%0,%+\n\taddu\t%0,%%gprel(%a1)";
2623 else
2625 if (TARGET_STATS)
2626 mips_count_memory_refs (op1, 2);
2628 if (GET_CODE (operands[1]) == SIGN_EXTEND)
2629 /* We deliberately remove the 'a' from '%1', so that we don't
2630 have to add SIGN_EXTEND support to print_operand_address.
2631 print_operand will just call print_operand_address in this
2632 case, so there is no problem. */
2633 ret = "la\t%0,%1";
2634 else
2635 ret = "dla\t%0,%a1";
2640 else if (code0 == MEM)
2642 if (code1 == REG)
2644 int regno1 = REGNO (op1) + subreg_offset1;
2646 if (FP_REG_P (regno1))
2647 ret = "s.d\t%1,%0";
2649 else if (TARGET_64BIT)
2652 #ifdef TARGET_FP_CALL_32
2653 if (FP_CALL_GP_REG_P (regno1))
2654 ret = "dsll\t%1,32\n\tor\t%1,%D1\n\tsd\t%1,%0";
2655 else
2656 #endif
2657 ret = "sd\t%1,%0";
2660 else if (double_memory_operand (op0, GET_MODE (op0)))
2661 ret = "sw\t%1,%0\n\tsw\t%D1,%D0";
2664 else if (((code1 == CONST_INT && INTVAL (op1) == 0)
2665 || (code1 == CONST_DOUBLE
2666 && op1 == CONST0_RTX (GET_MODE (op1))))
2667 && (TARGET_64BIT
2668 || double_memory_operand (op0, GET_MODE (op0))))
2670 if (TARGET_64BIT)
2671 ret = "sd\t%.,%0";
2672 else
2673 ret = "sw\t%.,%0\n\tsw\t%.,%D0";
2676 if (TARGET_STATS)
2677 mips_count_memory_refs (op0, 2);
2679 if (ret != 0 && MEM_VOLATILE_P (op0))
2681 size_t i = strlen (ret);
2683 if (i > sizeof (volatile_buffer) - sizeof ("%{%}"))
2684 abort ();
2686 sprintf (volatile_buffer, "%%{%s%%}", ret);
2687 ret = volatile_buffer;
2691 if (ret == 0)
2693 abort_with_insn (insn, "Bad move");
2694 return 0;
2697 if (delay != DELAY_NONE)
2698 return mips_fill_delay_slot (ret, delay, operands, insn);
2700 return ret;
2703 /* Provide the costs of an addressing mode that contains ADDR.
2704 If ADDR is not a valid address, its cost is irrelevant. */
2707 mips_address_cost (addr)
2708 rtx addr;
2710 switch (GET_CODE (addr))
2712 case LO_SUM:
2713 return 1;
2715 case LABEL_REF:
2716 return 2;
2718 case CONST:
2720 rtx offset = const0_rtx;
2721 addr = eliminate_constant_term (XEXP (addr, 0), &offset);
2722 if (GET_CODE (addr) == LABEL_REF)
2723 return 2;
2725 if (GET_CODE (addr) != SYMBOL_REF)
2726 return 4;
2728 if (! SMALL_INT (offset))
2729 return 2;
2732 /* ... fall through ... */
2734 case SYMBOL_REF:
2735 return SYMBOL_REF_FLAG (addr) ? 1 : 2;
2737 case PLUS:
2739 register rtx plus0 = XEXP (addr, 0);
2740 register rtx plus1 = XEXP (addr, 1);
2742 if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
2743 plus0 = XEXP (addr, 1), plus1 = XEXP (addr, 0);
2745 if (GET_CODE (plus0) != REG)
2746 break;
2748 switch (GET_CODE (plus1))
2750 case CONST_INT:
2751 return SMALL_INT (plus1) ? 1 : 2;
2753 case CONST:
2754 case SYMBOL_REF:
2755 case LABEL_REF:
2756 case HIGH:
2757 case LO_SUM:
2758 return mips_address_cost (plus1) + 1;
2760 default:
2761 break;
2765 default:
2766 break;
2769 return 4;
2772 /* Return nonzero if X is an address which needs a temporary register when
2773 reloaded while generating PIC code. */
2776 pic_address_needs_scratch (x)
2777 rtx x;
2779 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
2780 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
2781 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
2782 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2783 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
2784 return 1;
2786 return 0;
2789 /* Make normal rtx_code into something we can index from an array */
2791 static enum internal_test
2792 map_test_to_internal_test (test_code)
2793 enum rtx_code test_code;
2795 enum internal_test test = ITEST_MAX;
2797 switch (test_code)
2799 case EQ: test = ITEST_EQ; break;
2800 case NE: test = ITEST_NE; break;
2801 case GT: test = ITEST_GT; break;
2802 case GE: test = ITEST_GE; break;
2803 case LT: test = ITEST_LT; break;
2804 case LE: test = ITEST_LE; break;
2805 case GTU: test = ITEST_GTU; break;
2806 case GEU: test = ITEST_GEU; break;
2807 case LTU: test = ITEST_LTU; break;
2808 case LEU: test = ITEST_LEU; break;
2809 default: break;
2812 return test;
2816 /* Generate the code to compare two integer values. The return value is:
2817 (reg:SI xx) The pseudo register the comparison is in
2818 0 No register, generate a simple branch.
2820 ??? This is called with result nonzero by the Scond patterns in
2821 mips.md. These patterns are called with a target in the mode of
2822 the Scond instruction pattern. Since this must be a constant, we
2823 must use SImode. This means that if RESULT is non-zero, it will
2824 always be an SImode register, even if TARGET_64BIT is true. We
2825 cope with this by calling convert_move rather than emit_move_insn.
2826 This will sometimes lead to an unnecessary extension of the result;
2827 for example:
2829 long long
2830 foo (long long i)
2832 return i < 5;
2838 gen_int_relational (test_code, result, cmp0, cmp1, p_invert)
2839 enum rtx_code test_code; /* relational test (EQ, etc) */
2840 rtx result; /* result to store comp. or 0 if branch */
2841 rtx cmp0; /* first operand to compare */
2842 rtx cmp1; /* second operand to compare */
2843 int *p_invert; /* NULL or ptr to hold whether branch needs */
2844 /* to reverse its test */
2846 struct cmp_info
2848 enum rtx_code test_code; /* code to use in instruction (LT vs. LTU) */
2849 int const_low; /* low bound of constant we can accept */
2850 int const_high; /* high bound of constant we can accept */
2851 int const_add; /* constant to add (convert LE -> LT) */
2852 int reverse_regs; /* reverse registers in test */
2853 int invert_const; /* != 0 if invert value if cmp1 is constant */
2854 int invert_reg; /* != 0 if invert value if cmp1 is register */
2855 int unsignedp; /* != 0 for unsigned comparisons. */
2858 static struct cmp_info info[ (int)ITEST_MAX ] = {
2860 { XOR, 0, 65535, 0, 0, 0, 0, 0 }, /* EQ */
2861 { XOR, 0, 65535, 0, 0, 1, 1, 0 }, /* NE */
2862 { LT, -32769, 32766, 1, 1, 1, 0, 0 }, /* GT */
2863 { LT, -32768, 32767, 0, 0, 1, 1, 0 }, /* GE */
2864 { LT, -32768, 32767, 0, 0, 0, 0, 0 }, /* LT */
2865 { LT, -32769, 32766, 1, 1, 0, 1, 0 }, /* LE */
2866 { LTU, -32769, 32766, 1, 1, 1, 0, 1 }, /* GTU */
2867 { LTU, -32768, 32767, 0, 0, 1, 1, 1 }, /* GEU */
2868 { LTU, -32768, 32767, 0, 0, 0, 0, 1 }, /* LTU */
2869 { LTU, -32769, 32766, 1, 1, 0, 1, 1 }, /* LEU */
2872 enum internal_test test;
2873 enum machine_mode mode;
2874 struct cmp_info *p_info;
2875 int branch_p;
2876 int eqne_p;
2877 int invert;
2878 rtx reg;
2879 rtx reg2;
2881 test = map_test_to_internal_test (test_code);
2882 if (test == ITEST_MAX)
2883 abort ();
2885 p_info = &info[(int) test];
2886 eqne_p = (p_info->test_code == XOR);
2888 mode = GET_MODE (cmp0);
2889 if (mode == VOIDmode)
2890 mode = GET_MODE (cmp1);
2892 /* Eliminate simple branches */
2893 branch_p = (result == 0);
2894 if (branch_p)
2896 if (GET_CODE (cmp0) == REG || GET_CODE (cmp0) == SUBREG)
2898 /* Comparisons against zero are simple branches */
2899 if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0
2900 && (! TARGET_MIPS16 || eqne_p))
2901 return 0;
2903 /* Test for beq/bne. */
2904 if (eqne_p && ! TARGET_MIPS16)
2905 return 0;
2908 /* allocate a pseudo to calculate the value in. */
2909 result = gen_reg_rtx (mode);
2912 /* Make sure we can handle any constants given to us. */
2913 if (GET_CODE (cmp0) == CONST_INT)
2914 cmp0 = force_reg (mode, cmp0);
2916 if (GET_CODE (cmp1) == CONST_INT)
2918 HOST_WIDE_INT value = INTVAL (cmp1);
2920 if (value < p_info->const_low
2921 || value > p_info->const_high
2922 /* ??? Why? And why wasn't the similar code below modified too? */
2923 || (TARGET_64BIT
2924 && HOST_BITS_PER_WIDE_INT < 64
2925 && p_info->const_add != 0
2926 && ((p_info->unsignedp
2927 ? ((unsigned HOST_WIDE_INT) (value + p_info->const_add)
2928 > (unsigned HOST_WIDE_INT) INTVAL (cmp1))
2929 : (value + p_info->const_add) > INTVAL (cmp1))
2930 != (p_info->const_add > 0))))
2931 cmp1 = force_reg (mode, cmp1);
2934 /* See if we need to invert the result. */
2935 invert = (GET_CODE (cmp1) == CONST_INT
2936 ? p_info->invert_const : p_info->invert_reg);
2938 if (p_invert != (int *)0)
2940 *p_invert = invert;
2941 invert = 0;
2944 /* Comparison to constants, may involve adding 1 to change a LT into LE.
2945 Comparison between two registers, may involve switching operands. */
2946 if (GET_CODE (cmp1) == CONST_INT)
2948 if (p_info->const_add != 0)
2950 HOST_WIDE_INT new = INTVAL (cmp1) + p_info->const_add;
2952 /* If modification of cmp1 caused overflow,
2953 we would get the wrong answer if we follow the usual path;
2954 thus, x > 0xffffffffU would turn into x > 0U. */
2955 if ((p_info->unsignedp
2956 ? (unsigned HOST_WIDE_INT) new >
2957 (unsigned HOST_WIDE_INT) INTVAL (cmp1)
2958 : new > INTVAL (cmp1))
2959 != (p_info->const_add > 0))
2961 /* This test is always true, but if INVERT is true then
2962 the result of the test needs to be inverted so 0 should
2963 be returned instead. */
2964 emit_move_insn (result, invert ? const0_rtx : const_true_rtx);
2965 return result;
2967 else
2968 cmp1 = GEN_INT (new);
2972 else if (p_info->reverse_regs)
2974 rtx temp = cmp0;
2975 cmp0 = cmp1;
2976 cmp1 = temp;
2979 if (test == ITEST_NE && GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) == 0)
2980 reg = cmp0;
2981 else
2983 reg = (invert || eqne_p) ? gen_reg_rtx (mode) : result;
2984 convert_move (reg, gen_rtx (p_info->test_code, mode, cmp0, cmp1), 0);
2987 if (test == ITEST_NE)
2989 if (! TARGET_MIPS16)
2991 convert_move (result, gen_rtx (GTU, mode, reg, const0_rtx), 0);
2992 if (p_invert != NULL)
2993 *p_invert = 0;
2994 invert = 0;
2996 else
2998 reg2 = invert ? gen_reg_rtx (mode) : result;
2999 convert_move (reg2, gen_rtx (LTU, mode, reg, const1_rtx), 0);
3000 reg = reg2;
3004 else if (test == ITEST_EQ)
3006 reg2 = invert ? gen_reg_rtx (mode) : result;
3007 convert_move (reg2, gen_rtx_LTU (mode, reg, const1_rtx), 0);
3008 reg = reg2;
3011 if (invert)
3013 rtx one;
3015 if (! TARGET_MIPS16)
3016 one = const1_rtx;
3017 else
3019 /* The value is in $24. Copy it to another register, so
3020 that reload doesn't think it needs to store the $24 and
3021 the input to the XOR in the same location. */
3022 reg2 = gen_reg_rtx (mode);
3023 emit_move_insn (reg2, reg);
3024 reg = reg2;
3025 one = force_reg (mode, const1_rtx);
3027 convert_move (result, gen_rtx (XOR, mode, reg, one), 0);
3030 return result;
3033 /* Emit the common code for doing conditional branches.
3034 operand[0] is the label to jump to.
3035 The comparison operands are saved away by cmp{si,di,sf,df}. */
3037 void
3038 gen_conditional_branch (operands, test_code)
3039 rtx operands[];
3040 enum rtx_code test_code;
3042 enum cmp_type type = branch_type;
3043 rtx cmp0 = branch_cmp[0];
3044 rtx cmp1 = branch_cmp[1];
3045 enum machine_mode mode;
3046 rtx reg;
3047 int invert;
3048 rtx label1, label2;
3050 switch (type)
3052 case CMP_SI:
3053 case CMP_DI:
3054 mode = type == CMP_SI ? SImode : DImode;
3055 invert = 0;
3056 reg = gen_int_relational (test_code, NULL_RTX, cmp0, cmp1, &invert);
3058 if (reg)
3060 cmp0 = reg;
3061 cmp1 = const0_rtx;
3062 test_code = NE;
3064 else if (GET_CODE (cmp1) == CONST_INT && INTVAL (cmp1) != 0)
3065 /* We don't want to build a comparison against a non-zero
3066 constant. */
3067 cmp1 = force_reg (mode, cmp1);
3069 break;
3071 case CMP_SF:
3072 case CMP_DF:
3073 if (! ISA_HAS_8CC)
3074 reg = gen_rtx_REG (CCmode, FPSW_REGNUM);
3075 else
3076 reg = gen_reg_rtx (CCmode);
3078 /* For cmp0 != cmp1, build cmp0 == cmp1, and test for result ==
3079 0 in the instruction built below. The MIPS FPU handles
3080 inequality testing by testing for equality and looking for a
3081 false result. */
3082 emit_insn (gen_rtx_SET (VOIDmode, reg,
3083 gen_rtx (test_code == NE ? EQ : test_code,
3084 CCmode, cmp0, cmp1)));
3086 test_code = test_code == NE ? EQ : NE;
3087 mode = CCmode;
3088 cmp0 = reg;
3089 cmp1 = const0_rtx;
3090 invert = 0;
3091 break;
3093 default:
3094 abort_with_insn (gen_rtx (test_code, VOIDmode, cmp0, cmp1), "bad test");
3097 /* Generate the branch. */
3099 label1 = gen_rtx_LABEL_REF (VOIDmode, operands[0]);
3100 label2 = pc_rtx;
3102 if (invert)
3104 label2 = label1;
3105 label1 = pc_rtx;
3108 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
3109 gen_rtx_IF_THEN_ELSE (VOIDmode,
3110 gen_rtx (test_code, mode,
3111 cmp0, cmp1),
3112 label1, label2)));
3115 /* Emit the common code for conditional moves. OPERANDS is the array
3116 of operands passed to the conditional move defined_expand. */
3118 void
3119 gen_conditional_move (operands)
3120 rtx *operands;
3122 rtx op0 = branch_cmp[0];
3123 rtx op1 = branch_cmp[1];
3124 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3125 enum rtx_code cmp_code = GET_CODE (operands[1]);
3126 enum rtx_code move_code = NE;
3127 enum machine_mode op_mode = GET_MODE (operands[0]);
3128 enum machine_mode cmp_mode;
3129 rtx cmp_reg;
3131 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
3133 switch (cmp_code)
3135 case EQ:
3136 cmp_code = XOR;
3137 move_code = EQ;
3138 break;
3139 case NE:
3140 cmp_code = XOR;
3141 break;
3142 case LT:
3143 break;
3144 case GE:
3145 cmp_code = LT;
3146 move_code = EQ;
3147 break;
3148 case GT:
3149 cmp_code = LT;
3150 op0 = force_reg (mode, branch_cmp[1]);
3151 op1 = branch_cmp[0];
3152 break;
3153 case LE:
3154 cmp_code = LT;
3155 op0 = force_reg (mode, branch_cmp[1]);
3156 op1 = branch_cmp[0];
3157 move_code = EQ;
3158 break;
3159 case LTU:
3160 break;
3161 case GEU:
3162 cmp_code = LTU;
3163 move_code = EQ;
3164 break;
3165 case GTU:
3166 cmp_code = LTU;
3167 op0 = force_reg (mode, branch_cmp[1]);
3168 op1 = branch_cmp[0];
3169 break;
3170 case LEU:
3171 cmp_code = LTU;
3172 op0 = force_reg (mode, branch_cmp[1]);
3173 op1 = branch_cmp[0];
3174 move_code = EQ;
3175 break;
3176 default:
3177 abort ();
3180 else if (cmp_code == NE)
3181 cmp_code = EQ, move_code = EQ;
3183 if (mode == SImode || mode == DImode)
3184 cmp_mode = mode;
3185 else if (mode == SFmode || mode == DFmode)
3186 cmp_mode = CCmode;
3187 else
3188 abort ();
3190 cmp_reg = gen_reg_rtx (cmp_mode);
3191 emit_insn (gen_rtx_SET (cmp_mode, cmp_reg,
3192 gen_rtx (cmp_code, cmp_mode, op0, op1)));
3194 emit_insn (gen_rtx_SET (op_mode, operands[0],
3195 gen_rtx_IF_THEN_ELSE (op_mode,
3196 gen_rtx (move_code, VOIDmode,
3197 cmp_reg,
3198 CONST0_RTX (SImode)),
3199 operands[2], operands[3])));
3202 /* Emit the common code for conditional moves. OPERANDS is the array
3203 of operands passed to the conditional move defined_expand. */
3205 void
3206 mips_gen_conditional_trap (operands)
3207 rtx operands[];
3209 rtx op0, op1;
3210 enum rtx_code cmp_code = GET_CODE (operands[0]);
3211 enum machine_mode mode = GET_MODE (branch_cmp[0]);
3213 /* MIPS conditional trap machine instructions don't have GT or LE
3214 flavors, so we must invert the comparison and convert to LT and
3215 GE, respectively. */
3216 switch (cmp_code)
3218 case GT: cmp_code = LT; break;
3219 case LE: cmp_code = GE; break;
3220 case GTU: cmp_code = LTU; break;
3221 case LEU: cmp_code = GEU; break;
3222 default: break;
3224 if (cmp_code == GET_CODE (operands[0]))
3226 op0 = force_reg (mode, branch_cmp[0]);
3227 op1 = branch_cmp[1];
3229 else
3231 op0 = force_reg (mode, branch_cmp[1]);
3232 op1 = branch_cmp[0];
3234 if (GET_CODE (op1) == CONST_INT && ! SMALL_INT (op1))
3235 op1 = force_reg (mode, op1);
3237 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3238 gen_rtx (cmp_code, GET_MODE (operands[0]), op0, op1),
3239 operands[1]));
3242 /* Write a loop to move a constant number of bytes.
3243 Generate load/stores as follows:
3245 do {
3246 temp1 = src[0];
3247 temp2 = src[1];
3249 temp<last> = src[MAX_MOVE_REGS-1];
3250 dest[0] = temp1;
3251 dest[1] = temp2;
3253 dest[MAX_MOVE_REGS-1] = temp<last>;
3254 src += MAX_MOVE_REGS;
3255 dest += MAX_MOVE_REGS;
3256 } while (src != final);
3258 This way, no NOP's are needed, and only MAX_MOVE_REGS+3 temp
3259 registers are needed.
3261 Aligned moves move MAX_MOVE_REGS*4 bytes every (2*MAX_MOVE_REGS)+3
3262 cycles, unaligned moves move MAX_MOVE_REGS*4 bytes every
3263 (4*MAX_MOVE_REGS)+3 cycles, assuming no cache misses. */
3265 #define MAX_MOVE_REGS 4
3266 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3268 static void
3269 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src)
3270 rtx dest_reg; /* register holding destination address */
3271 rtx src_reg; /* register holding source address */
3272 unsigned int bytes; /* # bytes to move */
3273 int align; /* alignment */
3274 rtx orig_dest; /* original dest */
3275 rtx orig_src; /* original source for making a reg note */
3277 rtx dest_mem = replace_equiv_address (orig_dest, dest_reg);
3278 rtx src_mem = replace_equiv_address (orig_src, src_reg);
3279 rtx align_rtx = GEN_INT (align);
3280 rtx label;
3281 rtx final_src;
3282 rtx bytes_rtx;
3283 int leftover;
3285 if (bytes < (unsigned)2 * MAX_MOVE_BYTES)
3286 abort ();
3288 leftover = bytes % MAX_MOVE_BYTES;
3289 bytes -= leftover;
3291 label = gen_label_rtx ();
3292 final_src = gen_reg_rtx (Pmode);
3293 bytes_rtx = GEN_INT (bytes);
3295 if (bytes > 0x7fff)
3297 if (Pmode == DImode)
3299 emit_insn (gen_movdi (final_src, bytes_rtx));
3300 emit_insn (gen_adddi3 (final_src, final_src, src_reg));
3302 else
3304 emit_insn (gen_movsi (final_src, bytes_rtx));
3305 emit_insn (gen_addsi3 (final_src, final_src, src_reg));
3308 else
3310 if (Pmode == DImode)
3311 emit_insn (gen_adddi3 (final_src, src_reg, bytes_rtx));
3312 else
3313 emit_insn (gen_addsi3 (final_src, src_reg, bytes_rtx));
3316 emit_label (label);
3318 bytes_rtx = GEN_INT (MAX_MOVE_BYTES);
3319 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, bytes_rtx, align_rtx));
3321 if (Pmode == DImode)
3323 emit_insn (gen_adddi3 (src_reg, src_reg, bytes_rtx));
3324 emit_insn (gen_adddi3 (dest_reg, dest_reg, bytes_rtx));
3325 emit_insn (gen_cmpdi (src_reg, final_src));
3327 else
3329 emit_insn (gen_addsi3 (src_reg, src_reg, bytes_rtx));
3330 emit_insn (gen_addsi3 (dest_reg, dest_reg, bytes_rtx));
3331 emit_insn (gen_cmpsi (src_reg, final_src));
3334 emit_jump_insn (gen_bne (label));
3336 if (leftover)
3337 emit_insn (gen_movstrsi_internal (dest_mem, src_mem, GEN_INT (leftover),
3338 align_rtx));
3341 /* Use a library function to move some bytes. */
3343 static void
3344 block_move_call (dest_reg, src_reg, bytes_rtx)
3345 rtx dest_reg;
3346 rtx src_reg;
3347 rtx bytes_rtx;
3349 /* We want to pass the size as Pmode, which will normally be SImode
3350 but will be DImode if we are using 64 bit longs and pointers. */
3351 if (GET_MODE (bytes_rtx) != VOIDmode
3352 && GET_MODE (bytes_rtx) != (unsigned) Pmode)
3353 bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
3355 #ifdef TARGET_MEM_FUNCTIONS
3356 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memcpy"), 0,
3357 VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
3358 convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
3359 TREE_UNSIGNED (sizetype)),
3360 TYPE_MODE (sizetype));
3361 #else
3362 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "bcopy"), 0,
3363 VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
3364 convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
3365 TREE_UNSIGNED (integer_type_node)),
3366 TYPE_MODE (integer_type_node));
3367 #endif
3370 /* Expand string/block move operations.
3372 operands[0] is the pointer to the destination.
3373 operands[1] is the pointer to the source.
3374 operands[2] is the number of bytes to move.
3375 operands[3] is the alignment. */
3377 void
3378 expand_block_move (operands)
3379 rtx operands[];
3381 rtx bytes_rtx = operands[2];
3382 rtx align_rtx = operands[3];
3383 int constp = GET_CODE (bytes_rtx) == CONST_INT;
3384 unsigned HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
3385 unsigned int align = INTVAL (align_rtx);
3386 rtx orig_src = operands[1];
3387 rtx orig_dest = operands[0];
3388 rtx src_reg;
3389 rtx dest_reg;
3391 if (constp && bytes == 0)
3392 return;
3394 if (align > (unsigned) UNITS_PER_WORD)
3395 align = UNITS_PER_WORD;
3397 /* Move the address into scratch registers. */
3398 dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
3399 src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
3401 if (TARGET_MEMCPY)
3402 block_move_call (dest_reg, src_reg, bytes_rtx);
3404 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES
3405 && align == (unsigned) UNITS_PER_WORD)
3406 move_by_pieces (orig_dest, orig_src, bytes, align * BITS_PER_WORD);
3408 else if (constp && bytes <= (unsigned)2 * MAX_MOVE_BYTES)
3409 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
3410 dest_reg),
3411 replace_equiv_address (orig_src,
3412 src_reg),
3413 bytes_rtx, align_rtx));
3415 else if (constp && align >= (unsigned) UNITS_PER_WORD && optimize)
3416 block_move_loop (dest_reg, src_reg, bytes, align, orig_dest, orig_src);
3418 else if (constp && optimize)
3420 /* If the alignment is not word aligned, generate a test at
3421 runtime, to see whether things wound up aligned, and we
3422 can use the faster lw/sw instead ulw/usw. */
3424 rtx temp = gen_reg_rtx (Pmode);
3425 rtx aligned_label = gen_label_rtx ();
3426 rtx join_label = gen_label_rtx ();
3427 int leftover = bytes % MAX_MOVE_BYTES;
3429 bytes -= leftover;
3431 if (Pmode == DImode)
3433 emit_insn (gen_iordi3 (temp, src_reg, dest_reg));
3434 emit_insn (gen_anddi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3435 emit_insn (gen_cmpdi (temp, const0_rtx));
3437 else
3439 emit_insn (gen_iorsi3 (temp, src_reg, dest_reg));
3440 emit_insn (gen_andsi3 (temp, temp, GEN_INT (UNITS_PER_WORD - 1)));
3441 emit_insn (gen_cmpsi (temp, const0_rtx));
3444 emit_jump_insn (gen_beq (aligned_label));
3446 /* Unaligned loop. */
3447 block_move_loop (dest_reg, src_reg, bytes, 1, orig_dest, orig_src);
3448 emit_jump_insn (gen_jump (join_label));
3449 emit_barrier ();
3451 /* Aligned loop. */
3452 emit_label (aligned_label);
3453 block_move_loop (dest_reg, src_reg, bytes, UNITS_PER_WORD, orig_dest,
3454 orig_src);
3455 emit_label (join_label);
3457 /* Bytes at the end of the loop. */
3458 if (leftover)
3459 emit_insn (gen_movstrsi_internal (replace_equiv_address (orig_dest,
3460 dest_reg),
3461 replace_equiv_address (orig_src,
3462 src_reg),
3463 GEN_INT (leftover),
3464 GEN_INT (align)));
3467 else
3468 block_move_call (dest_reg, src_reg, bytes_rtx);
3471 /* Emit load/stores for a small constant block_move.
3473 operands[0] is the memory address of the destination.
3474 operands[1] is the memory address of the source.
3475 operands[2] is the number of bytes to move.
3476 operands[3] is the alignment.
3477 operands[4] is a temp register.
3478 operands[5] is a temp register.
3480 operands[3+num_regs] is the last temp register.
3482 The block move type can be one of the following:
3483 BLOCK_MOVE_NORMAL Do all of the block move.
3484 BLOCK_MOVE_NOT_LAST Do all but the last store.
3485 BLOCK_MOVE_LAST Do just the last store. */
3487 const char *
3488 output_block_move (insn, operands, num_regs, move_type)
3489 rtx insn;
3490 rtx operands[];
3491 int num_regs;
3492 enum block_move_type move_type;
3494 rtx dest_reg = XEXP (operands[0], 0);
3495 rtx src_reg = XEXP (operands[1], 0);
3496 HOST_WIDE_INT bytes = INTVAL (operands[2]);
3497 int align = INTVAL (operands[3]);
3498 int num = 0;
3499 int offset = 0;
3500 int use_lwl_lwr = 0;
3501 int last_operand = num_regs + 4;
3502 int safe_regs = 4;
3503 int i;
3504 rtx xoperands[10];
3506 struct {
3507 const char *load; /* load insn without nop */
3508 const char *load_nop; /* load insn with trailing nop */
3509 const char *store; /* store insn */
3510 const char *final; /* if last_store used: NULL or swr */
3511 const char *last_store; /* last store instruction */
3512 int offset; /* current offset */
3513 enum machine_mode mode; /* mode to use on (MEM) */
3514 } load_store[4];
3516 /* ??? Detect a bug in GCC, where it can give us a register
3517 the same as one of the addressing registers and reduce
3518 the number of registers available. */
3519 for (i = 4; i < last_operand && safe_regs < (int) ARRAY_SIZE (xoperands); i++)
3520 if (! reg_mentioned_p (operands[i], operands[0])
3521 && ! reg_mentioned_p (operands[i], operands[1]))
3522 xoperands[safe_regs++] = operands[i];
3524 if (safe_regs < last_operand)
3526 xoperands[0] = operands[0];
3527 xoperands[1] = operands[1];
3528 xoperands[2] = operands[2];
3529 xoperands[3] = operands[3];
3530 return output_block_move (insn, xoperands, safe_regs - 4, move_type);
3533 /* If we are given global or static addresses, and we would be
3534 emitting a few instructions, try to save time by using a
3535 temporary register for the pointer. */
3536 /* ??? The SGI Irix6 assembler fails when a SYMBOL_REF is used in
3537 an ldl/ldr instruction pair. We play it safe, and always move
3538 constant addresses into registers when generating N32/N64 code, just
3539 in case we might emit an unaligned load instruction. */
3540 if (num_regs > 2 && (bytes > 2 * align || move_type != BLOCK_MOVE_NORMAL
3541 || mips_abi == ABI_MEABI
3542 || mips_abi == ABI_N32
3543 || mips_abi == ABI_64))
3545 if (CONSTANT_P (src_reg))
3547 if (TARGET_STATS)
3548 mips_count_memory_refs (operands[1], 1);
3550 src_reg = operands[3 + num_regs--];
3551 if (move_type != BLOCK_MOVE_LAST)
3553 xoperands[1] = operands[1];
3554 xoperands[0] = src_reg;
3555 if (Pmode == DImode)
3556 output_asm_insn ("dla\t%0,%1", xoperands);
3557 else
3558 output_asm_insn ("la\t%0,%1", xoperands);
3562 if (CONSTANT_P (dest_reg))
3564 if (TARGET_STATS)
3565 mips_count_memory_refs (operands[0], 1);
3567 dest_reg = operands[3 + num_regs--];
3568 if (move_type != BLOCK_MOVE_LAST)
3570 xoperands[1] = operands[0];
3571 xoperands[0] = dest_reg;
3572 if (Pmode == DImode)
3573 output_asm_insn ("dla\t%0,%1", xoperands);
3574 else
3575 output_asm_insn ("la\t%0,%1", xoperands);
3580 /* ??? We really shouldn't get any LO_SUM addresses here, because they
3581 are not offsettable, however, offsettable_address_p says they are
3582 offsettable. I think this is a bug in offsettable_address_p.
3583 For expediency, we fix this by just loading the address into a register
3584 if we happen to get one. */
3586 if (GET_CODE (src_reg) == LO_SUM)
3588 src_reg = operands[3 + num_regs--];
3589 if (move_type != BLOCK_MOVE_LAST)
3591 xoperands[2] = XEXP (XEXP (operands[1], 0), 1);
3592 xoperands[1] = XEXP (XEXP (operands[1], 0), 0);
3593 xoperands[0] = src_reg;
3594 if (Pmode == DImode)
3595 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3596 else
3597 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3601 if (GET_CODE (dest_reg) == LO_SUM)
3603 dest_reg = operands[3 + num_regs--];
3604 if (move_type != BLOCK_MOVE_LAST)
3606 xoperands[2] = XEXP (XEXP (operands[0], 0), 1);
3607 xoperands[1] = XEXP (XEXP (operands[0], 0), 0);
3608 xoperands[0] = dest_reg;
3609 if (Pmode == DImode)
3610 output_asm_insn ("daddiu\t%0,%1,%%lo(%2)", xoperands);
3611 else
3612 output_asm_insn ("addiu\t%0,%1,%%lo(%2)", xoperands);
3616 if (num_regs > (int) ARRAY_SIZE (load_store))
3617 num_regs = ARRAY_SIZE (load_store);
3619 else if (num_regs < 1)
3620 abort_with_insn (insn,
3621 "Cannot do block move, not enough scratch registers");
3623 while (bytes > 0)
3625 load_store[num].offset = offset;
3627 if (TARGET_64BIT && bytes >= 8 && align >= 8)
3629 load_store[num].load = "ld\t%0,%1";
3630 load_store[num].load_nop = "ld\t%0,%1%#";
3631 load_store[num].store = "sd\t%0,%1";
3632 load_store[num].last_store = "sd\t%0,%1";
3633 load_store[num].final = 0;
3634 load_store[num].mode = DImode;
3635 offset += 8;
3636 bytes -= 8;
3639 /* ??? Fails because of a MIPS assembler bug? */
3640 else if (TARGET_64BIT && bytes >= 8 && ! TARGET_MIPS16)
3642 if (BYTES_BIG_ENDIAN)
3644 load_store[num].load = "ldl\t%0,%1\n\tldr\t%0,%2";
3645 load_store[num].load_nop = "ldl\t%0,%1\n\tldr\t%0,%2%#";
3646 load_store[num].store = "sdl\t%0,%1\n\tsdr\t%0,%2";
3647 load_store[num].last_store = "sdr\t%0,%2";
3648 load_store[num].final = "sdl\t%0,%1";
3650 else
3652 load_store[num].load = "ldl\t%0,%2\n\tldr\t%0,%1";
3653 load_store[num].load_nop = "ldl\t%0,%2\n\tldr\t%0,%1%#";
3654 load_store[num].store = "sdl\t%0,%2\n\tsdr\t%0,%1";
3655 load_store[num].last_store = "sdr\t%0,%1";
3656 load_store[num].final = "sdl\t%0,%2";
3659 load_store[num].mode = DImode;
3660 offset += 8;
3661 bytes -= 8;
3662 use_lwl_lwr = 1;
3665 else if (bytes >= 4 && align >= 4)
3667 load_store[num].load = "lw\t%0,%1";
3668 load_store[num].load_nop = "lw\t%0,%1%#";
3669 load_store[num].store = "sw\t%0,%1";
3670 load_store[num].last_store = "sw\t%0,%1";
3671 load_store[num].final = 0;
3672 load_store[num].mode = SImode;
3673 offset += 4;
3674 bytes -= 4;
3677 else if (bytes >= 4 && ! TARGET_MIPS16)
3679 if (BYTES_BIG_ENDIAN)
3681 load_store[num].load = "lwl\t%0,%1\n\tlwr\t%0,%2";
3682 load_store[num].load_nop = "lwl\t%0,%1\n\tlwr\t%0,%2%#";
3683 load_store[num].store = "swl\t%0,%1\n\tswr\t%0,%2";
3684 load_store[num].last_store = "swr\t%0,%2";
3685 load_store[num].final = "swl\t%0,%1";
3687 else
3689 load_store[num].load = "lwl\t%0,%2\n\tlwr\t%0,%1";
3690 load_store[num].load_nop = "lwl\t%0,%2\n\tlwr\t%0,%1%#";
3691 load_store[num].store = "swl\t%0,%2\n\tswr\t%0,%1";
3692 load_store[num].last_store = "swr\t%0,%1";
3693 load_store[num].final = "swl\t%0,%2";
3696 load_store[num].mode = SImode;
3697 offset += 4;
3698 bytes -= 4;
3699 use_lwl_lwr = 1;
3702 else if (bytes >= 2 && align >= 2)
3704 load_store[num].load = "lh\t%0,%1";
3705 load_store[num].load_nop = "lh\t%0,%1%#";
3706 load_store[num].store = "sh\t%0,%1";
3707 load_store[num].last_store = "sh\t%0,%1";
3708 load_store[num].final = 0;
3709 load_store[num].mode = HImode;
3710 offset += 2;
3711 bytes -= 2;
3713 else
3715 load_store[num].load = "lb\t%0,%1";
3716 load_store[num].load_nop = "lb\t%0,%1%#";
3717 load_store[num].store = "sb\t%0,%1";
3718 load_store[num].last_store = "sb\t%0,%1";
3719 load_store[num].final = 0;
3720 load_store[num].mode = QImode;
3721 offset++;
3722 bytes--;
3725 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3727 dslots_load_total++;
3728 dslots_load_filled++;
3730 if (CONSTANT_P (src_reg))
3731 mips_count_memory_refs (src_reg, 1);
3733 if (CONSTANT_P (dest_reg))
3734 mips_count_memory_refs (dest_reg, 1);
3737 /* Emit load/stores now if we have run out of registers or are
3738 at the end of the move. */
3740 if (++num == num_regs || bytes == 0)
3742 /* If only load/store, we need a NOP after the load. */
3743 if (num == 1)
3745 load_store[0].load = load_store[0].load_nop;
3746 if (TARGET_STATS && move_type != BLOCK_MOVE_LAST)
3747 dslots_load_filled--;
3750 if (move_type != BLOCK_MOVE_LAST)
3752 for (i = 0; i < num; i++)
3754 int offset;
3756 if (!operands[i + 4])
3757 abort ();
3759 if (GET_MODE (operands[i + 4]) != load_store[i].mode)
3760 operands[i + 4] = gen_rtx_REG (load_store[i].mode,
3761 REGNO (operands[i + 4]));
3763 offset = load_store[i].offset;
3764 xoperands[0] = operands[i + 4];
3765 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
3766 plus_constant (src_reg, offset));
3768 if (use_lwl_lwr)
3770 int extra_offset
3771 = GET_MODE_SIZE (load_store[i].mode) - 1;
3773 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
3774 plus_constant (src_reg,
3775 extra_offset
3776 + offset));
3779 output_asm_insn (load_store[i].load, xoperands);
3783 for (i = 0; i < num; i++)
3785 int last_p = (i == num-1 && bytes == 0);
3786 int offset = load_store[i].offset;
3788 xoperands[0] = operands[i + 4];
3789 xoperands[1] = gen_rtx_MEM (load_store[i].mode,
3790 plus_constant (dest_reg, offset));
3793 if (use_lwl_lwr)
3795 int extra_offset = GET_MODE_SIZE (load_store[i].mode) - 1;
3796 xoperands[2] = gen_rtx_MEM (load_store[i].mode,
3797 plus_constant (dest_reg,
3798 extra_offset
3799 + offset));
3802 if (move_type == BLOCK_MOVE_NORMAL)
3803 output_asm_insn (load_store[i].store, xoperands);
3805 else if (move_type == BLOCK_MOVE_NOT_LAST)
3807 if (!last_p)
3808 output_asm_insn (load_store[i].store, xoperands);
3810 else if (load_store[i].final != 0)
3811 output_asm_insn (load_store[i].final, xoperands);
3814 else if (last_p)
3815 output_asm_insn (load_store[i].last_store, xoperands);
3818 num = 0; /* reset load_store */
3819 use_lwl_lwr = 0;
3823 return "";
3826 /* Argument support functions. */
3828 /* Initialize CUMULATIVE_ARGS for a function. */
3830 void
3831 init_cumulative_args (cum, fntype, libname)
3832 CUMULATIVE_ARGS *cum; /* argument info to initialize */
3833 tree fntype; /* tree ptr for function decl */
3834 rtx libname ATTRIBUTE_UNUSED; /* SYMBOL_REF of library name or 0 */
3836 static CUMULATIVE_ARGS zero_cum;
3837 tree param, next_param;
3839 if (TARGET_DEBUG_E_MODE)
3841 fprintf (stderr,
3842 "\ninit_cumulative_args, fntype = 0x%.8lx", (long)fntype);
3844 if (!fntype)
3845 fputc ('\n', stderr);
3847 else
3849 tree ret_type = TREE_TYPE (fntype);
3850 fprintf (stderr, ", fntype code = %s, ret code = %s\n",
3851 tree_code_name[(int)TREE_CODE (fntype)],
3852 tree_code_name[(int)TREE_CODE (ret_type)]);
3856 *cum = zero_cum;
3857 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3859 /* Determine if this function has variable arguments. This is
3860 indicated by the last argument being 'void_type_mode' if there
3861 are no variable arguments. The standard MIPS calling sequence
3862 passes all arguments in the general purpose registers in this case. */
3864 for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3865 param != 0; param = next_param)
3867 next_param = TREE_CHAIN (param);
3868 if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3869 cum->gp_reg_found = 1;
3873 /* Advance the argument to the next argument position. */
3875 void
3876 function_arg_advance (cum, mode, type, named)
3877 CUMULATIVE_ARGS *cum; /* current arg information */
3878 enum machine_mode mode; /* current arg mode */
3879 tree type; /* type of the argument or 0 if lib support */
3880 int named; /* whether or not the argument was named */
3882 if (TARGET_DEBUG_E_MODE)
3884 fprintf (stderr,
3885 "function_adv({gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3886 cum->gp_reg_found, cum->arg_number, cum->arg_words,
3887 GET_MODE_NAME (mode));
3888 fprintf (stderr, HOST_PTR_PRINTF, (const PTR) type);
3889 fprintf (stderr, ", %d )\n\n", named);
3892 cum->arg_number++;
3893 switch (mode)
3895 case VOIDmode:
3896 break;
3898 default:
3899 if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
3900 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3901 abort ();
3903 cum->gp_reg_found = 1;
3904 cum->arg_words += ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
3905 / UNITS_PER_WORD);
3906 break;
3908 case BLKmode:
3909 cum->gp_reg_found = 1;
3910 cum->arg_words += ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
3911 / UNITS_PER_WORD);
3912 break;
3914 case SFmode:
3915 if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3916 cum->fp_arg_words++;
3917 else
3918 cum->arg_words++;
3919 if (! cum->gp_reg_found && cum->arg_number <= 2)
3920 cum->fp_code += 1 << ((cum->arg_number - 1) * 2);
3921 break;
3923 case DFmode:
3924 if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
3925 cum->fp_arg_words += (TARGET_64BIT ? 1 : 2);
3926 else
3927 cum->arg_words += (TARGET_64BIT ? 1 : 2);
3928 if (! cum->gp_reg_found && ! TARGET_SINGLE_FLOAT && cum->arg_number <= 2)
3929 cum->fp_code += 2 << ((cum->arg_number - 1) * 2);
3930 break;
3932 case DImode:
3933 cum->gp_reg_found = 1;
3934 cum->arg_words += (TARGET_64BIT ? 1 : 2);
3935 break;
3937 case QImode:
3938 case HImode:
3939 case SImode:
3940 cum->gp_reg_found = 1;
3941 cum->arg_words++;
3942 break;
3946 /* Return an RTL expression containing the register for the given mode,
3947 or 0 if the argument is to be passed on the stack. */
3949 struct rtx_def *
3950 function_arg (cum, mode, type, named)
3951 CUMULATIVE_ARGS *cum; /* current arg information */
3952 enum machine_mode mode; /* current arg mode */
3953 tree type; /* type of the argument or 0 if lib support */
3954 int named; /* != 0 for normal args, == 0 for ... args */
3956 rtx ret;
3957 int regbase = -1;
3958 int bias = 0;
3959 unsigned int *arg_words = &cum->arg_words;
3960 int struct_p = (type != 0
3961 && (TREE_CODE (type) == RECORD_TYPE
3962 || TREE_CODE (type) == UNION_TYPE
3963 || TREE_CODE (type) == QUAL_UNION_TYPE));
3965 if (TARGET_DEBUG_E_MODE)
3967 fprintf (stderr,
3968 "function_arg( {gp reg found = %d, arg # = %2d, words = %2d}, %4s, ",
3969 cum->gp_reg_found, cum->arg_number, cum->arg_words,
3970 GET_MODE_NAME (mode));
3971 fprintf (stderr, HOST_PTR_PRINTF, (const PTR) type);
3972 fprintf (stderr, ", %d ) = ", named);
3976 cum->last_arg_fp = 0;
3977 switch (mode)
3979 case SFmode:
3980 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
3982 if (cum->gp_reg_found || cum->arg_number >= 2 || TARGET_SOFT_FLOAT)
3983 regbase = GP_ARG_FIRST;
3984 else
3986 regbase = FP_ARG_FIRST;
3988 /* If the first arg was a float in a floating point register,
3989 then set bias to align this float arg properly. */
3990 if (cum->arg_words == 1)
3991 bias = 1;
3994 else if (mips_abi == ABI_EABI && ! TARGET_SOFT_FLOAT)
3996 if (! TARGET_64BIT)
3997 cum->fp_arg_words += cum->fp_arg_words & 1;
3998 cum->last_arg_fp = 1;
3999 arg_words = &cum->fp_arg_words;
4000 regbase = FP_ARG_FIRST;
4002 /* The MIPS eabi says only structures containing doubles get passed in a
4003 fp register, so force a structure containing a float to be passed in
4004 the integer registers. */
4005 else if (mips_abi == ABI_MEABI && struct_p)
4006 regbase = GP_ARG_FIRST;
4007 else
4008 regbase = (TARGET_SOFT_FLOAT || ! named ? GP_ARG_FIRST : FP_ARG_FIRST);
4009 break;
4011 case DFmode:
4012 if (! TARGET_64BIT)
4014 if (mips_abi == ABI_EABI
4015 && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
4016 cum->fp_arg_words += cum->fp_arg_words & 1;
4017 else
4018 cum->arg_words += cum->arg_words & 1;
4021 if (mips_abi == ABI_32 || mips_abi == ABI_O64)
4022 regbase = ((cum->gp_reg_found
4023 || TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT
4024 || cum->arg_number >= 2)
4025 ? GP_ARG_FIRST : FP_ARG_FIRST);
4026 else if (mips_abi == ABI_EABI
4027 && ! TARGET_SOFT_FLOAT && ! TARGET_SINGLE_FLOAT)
4029 cum->last_arg_fp = 1;
4030 arg_words = &cum->fp_arg_words;
4031 regbase = FP_ARG_FIRST;
4033 else
4034 regbase = (TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT || ! named
4035 ? GP_ARG_FIRST : FP_ARG_FIRST);
4036 break;
4038 default:
4039 if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
4040 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4041 abort ();
4043 /* Drops through. */
4044 case BLKmode:
4045 if (type != NULL_TREE && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD
4046 && ! TARGET_64BIT && mips_abi != ABI_EABI)
4047 cum->arg_words += (cum->arg_words & 1);
4048 regbase = GP_ARG_FIRST;
4049 break;
4051 case VOIDmode:
4052 case QImode:
4053 case HImode:
4054 case SImode:
4055 regbase = GP_ARG_FIRST;
4056 break;
4058 case DImode:
4059 if (! TARGET_64BIT)
4060 cum->arg_words += (cum->arg_words & 1);
4061 regbase = GP_ARG_FIRST;
4064 if (*arg_words >= (unsigned) MAX_ARGS_IN_REGISTERS)
4066 if (TARGET_DEBUG_E_MODE)
4067 fprintf (stderr, "<stack>%s\n", struct_p ? ", [struct]" : "");
4069 ret = 0;
4071 else
4073 if (regbase == -1)
4074 abort ();
4076 if (! type || TREE_CODE (type) != RECORD_TYPE
4077 || mips_abi == ABI_32 || mips_abi == ABI_EABI
4078 || mips_abi == ABI_O64 || mips_abi == ABI_MEABI
4079 || ! named
4080 || ! TYPE_SIZE_UNIT (type)
4081 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
4084 unsigned int arg_reg = (regbase + *arg_words + bias);
4085 ret = gen_rtx_REG (mode, arg_reg);
4086 if (mips_abi == ABI_MEABI
4087 && regbase == FP_ARG_FIRST
4088 && ! cum->prototype)
4090 /* To make K&R varargs work we need to pass floating
4091 point arguments in both integer and FP registers. */
4092 ret = gen_rtx_PARALLEL (mode,
4093 gen_rtvec (2,
4094 gen_rtx_EXPR_LIST (VOIDmode,
4095 gen_rtx_REG (mode,
4096 arg_reg + GP_ARG_FIRST - FP_ARG_FIRST),
4097 const0_rtx), gen_rtx_EXPR_LIST (VOIDmode, ret, const0_rtx)));
4100 else
4102 /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
4103 structure contains a double in its entirety, then that 64 bit
4104 chunk is passed in a floating point register. */
4105 tree field;
4107 /* First check to see if there is any such field. */
4108 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4109 if (TREE_CODE (field) == FIELD_DECL
4110 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4111 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4112 && host_integerp (bit_position (field), 0)
4113 && int_bit_position (field) % BITS_PER_WORD == 0)
4114 break;
4116 /* If the whole struct fits a DFmode register,
4117 we don't need the PARALLEL. */
4118 if (! field || mode == DFmode)
4119 ret = gen_rtx_REG (mode, regbase + *arg_words + bias);
4120 else
4122 /* Now handle the special case by returning a PARALLEL
4123 indicating where each 64 bit chunk goes. */
4124 unsigned int chunks;
4125 HOST_WIDE_INT bitpos;
4126 unsigned int regno;
4127 unsigned int i;
4129 /* ??? If this is a packed structure, then the last hunk won't
4130 be 64 bits. */
4132 chunks
4133 = tree_low_cst (TYPE_SIZE_UNIT (type), 1) / UNITS_PER_WORD;
4134 if (chunks + *arg_words + bias > (unsigned) MAX_ARGS_IN_REGISTERS)
4135 chunks = MAX_ARGS_IN_REGISTERS - *arg_words - bias;
4137 /* assign_parms checks the mode of ENTRY_PARM, so we must
4138 use the actual mode here. */
4139 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (chunks));
4141 bitpos = 0;
4142 regno = regbase + *arg_words + bias;
4143 field = TYPE_FIELDS (type);
4144 for (i = 0; i < chunks; i++)
4146 rtx reg;
4148 for (; field; field = TREE_CHAIN (field))
4149 if (TREE_CODE (field) == FIELD_DECL
4150 && int_bit_position (field) >= bitpos)
4151 break;
4153 if (field
4154 && int_bit_position (field) == bitpos
4155 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4156 && !TARGET_SOFT_FLOAT
4157 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4158 reg = gen_rtx_REG (DFmode,
4159 regno + FP_ARG_FIRST - GP_ARG_FIRST);
4160 else
4161 reg = gen_rtx_REG (word_mode, regno);
4163 XVECEXP (ret, 0, i)
4164 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4165 GEN_INT (bitpos / BITS_PER_UNIT));
4167 bitpos += 64;
4168 regno++;
4173 if (TARGET_DEBUG_E_MODE)
4174 fprintf (stderr, "%s%s\n", reg_names[regbase + *arg_words + bias],
4175 struct_p ? ", [struct]" : "");
4177 /* The following is a hack in order to pass 1 byte structures
4178 the same way that the MIPS compiler does (namely by passing
4179 the structure in the high byte or half word of the register).
4180 This also makes varargs work. If we have such a structure,
4181 we save the adjustment RTL, and the call define expands will
4182 emit them. For the VOIDmode argument (argument after the
4183 last real argument), pass back a parallel vector holding each
4184 of the adjustments. */
4186 /* ??? function_arg can be called more than once for each argument.
4187 As a result, we compute more adjustments than we need here.
4188 See the CUMULATIVE_ARGS definition in mips.h. */
4190 /* ??? This scheme requires everything smaller than the word size to
4191 shifted to the left, but when TARGET_64BIT and ! TARGET_INT64,
4192 that would mean every int needs to be shifted left, which is very
4193 inefficient. Let's not carry this compatibility to the 64 bit
4194 calling convention for now. */
4196 if (struct_p && int_size_in_bytes (type) < UNITS_PER_WORD
4197 && ! TARGET_64BIT
4198 && mips_abi != ABI_EABI
4199 && mips_abi != ABI_MEABI)
4201 rtx amount = GEN_INT (BITS_PER_WORD
4202 - int_size_in_bytes (type) * BITS_PER_UNIT);
4203 rtx reg = gen_rtx_REG (word_mode, regbase + *arg_words + bias);
4205 if (TARGET_64BIT)
4206 cum->adjust[cum->num_adjusts++] = gen_ashldi3 (reg, reg, amount);
4207 else
4208 cum->adjust[cum->num_adjusts++] = gen_ashlsi3 (reg, reg, amount);
4212 /* We will be called with a mode of VOIDmode after the last argument
4213 has been seen. Whatever we return will be passed to the call
4214 insn. If we need any shifts for small structures, return them in
4215 a PARALLEL; in that case, stuff the mips16 fp_code in as the
4216 mode. Otherwise, if we have need a mips16 fp_code, return a REG
4217 with the code stored as the mode. */
4218 if (mode == VOIDmode)
4220 if (cum->num_adjusts > 0)
4221 ret = gen_rtx (PARALLEL, (enum machine_mode) cum->fp_code,
4222 gen_rtvec_v (cum->num_adjusts, cum->adjust));
4223 else if (TARGET_MIPS16 && cum->fp_code != 0)
4224 ret = gen_rtx (REG, (enum machine_mode) cum->fp_code, 0);
4227 return ret;
4231 function_arg_partial_nregs (cum, mode, type, named)
4232 CUMULATIVE_ARGS *cum; /* current arg information */
4233 enum machine_mode mode; /* current arg mode */
4234 tree type; /* type of the argument or 0 if lib support */
4235 int named ATTRIBUTE_UNUSED;/* != 0 for normal args, == 0 for ... args */
4237 if ((mode == BLKmode
4238 || GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
4239 || GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4240 && cum->arg_words < (unsigned) MAX_ARGS_IN_REGISTERS
4241 && mips_abi != ABI_EABI)
4243 int words;
4244 if (mode == BLKmode)
4245 words = ((int_size_in_bytes (type) + UNITS_PER_WORD - 1)
4246 / UNITS_PER_WORD);
4247 else
4248 words = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4250 if (words + cum->arg_words <= (unsigned) MAX_ARGS_IN_REGISTERS)
4251 return 0; /* structure fits in registers */
4253 if (TARGET_DEBUG_E_MODE)
4254 fprintf (stderr, "function_arg_partial_nregs = %d\n",
4255 MAX_ARGS_IN_REGISTERS - cum->arg_words);
4257 return MAX_ARGS_IN_REGISTERS - cum->arg_words;
4260 else if (mode == DImode
4261 && cum->arg_words == MAX_ARGS_IN_REGISTERS - (unsigned)1
4262 && ! TARGET_64BIT && mips_abi != ABI_EABI)
4264 if (TARGET_DEBUG_E_MODE)
4265 fprintf (stderr, "function_arg_partial_nregs = 1\n");
4267 return 1;
4270 return 0;
4273 /* Create the va_list data type.
4274 We keep 3 pointers, and two offsets.
4275 Two pointers are to the overflow area, which starts at the CFA.
4276 One of these is constant, for addressing into the GPR save area below it.
4277 The other is advanced up the stack through the overflow region.
4278 The third pointer is to the GPR save area. Since the FPR save area
4279 is just below it, we can address FPR slots off this pointer.
4280 We also keep two one-byte offsets, which are to be subtracted from the
4281 constant pointers to yield addresses in the GPR and FPR save areas.
4282 These are downcounted as float or non-float arguments are used,
4283 and when they get to zero, the argument must be obtained from the
4284 overflow region.
4285 If TARGET_SOFT_FLOAT or TARGET_SINGLE_FLOAT, then no FPR save area exists,
4286 and a single pointer is enough. It's started at the GPR save area,
4287 and is advanced, period.
4288 Note that the GPR save area is not constant size, due to optimization
4289 in the prologue. Hence, we can't use a design with two pointers
4290 and two offsets, although we could have designed this with two pointers
4291 and three offsets. */
4294 tree
4295 mips_build_va_list ()
4297 if (mips_abi == ABI_EABI && !TARGET_SOFT_FLOAT && !TARGET_SINGLE_FLOAT)
4299 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, record;
4301 record = make_node (RECORD_TYPE);
4303 f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
4304 ptr_type_node);
4305 f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
4306 ptr_type_node);
4307 f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
4308 ptr_type_node);
4309 f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
4310 unsigned_char_type_node);
4311 f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
4312 unsigned_char_type_node);
4315 DECL_FIELD_CONTEXT (f_ovfl) = record;
4316 DECL_FIELD_CONTEXT (f_gtop) = record;
4317 DECL_FIELD_CONTEXT (f_ftop) = record;
4318 DECL_FIELD_CONTEXT (f_goff) = record;
4319 DECL_FIELD_CONTEXT (f_foff) = record;
4321 TYPE_FIELDS (record) = f_ovfl;
4322 TREE_CHAIN (f_ovfl) = f_gtop;
4323 TREE_CHAIN (f_gtop) = f_ftop;
4324 TREE_CHAIN (f_ftop) = f_goff;
4325 TREE_CHAIN (f_goff) = f_foff;
4327 layout_type (record);
4328 return record;
4330 else
4331 return ptr_type_node;
4334 /* Implement va_start. stdarg_p is 0 if implementing
4335 __builtin_varargs_va_start, 1 if implementing __builtin_stdarg_va_start.
4336 Note that this routine isn't called when compiling e.g. "_vfprintf_r".
4337 (It doesn't have "...", so it inherits the pointers of its caller.) */
4339 void
4340 mips_va_start (stdarg_p, valist, nextarg)
4341 int stdarg_p;
4342 tree valist;
4343 rtx nextarg;
4345 int int_arg_words;
4346 tree t;
4348 /* Find out how many non-float named formals */
4349 int_arg_words = current_function_args_info.arg_words;
4351 if (mips_abi == ABI_EABI)
4353 int gpr_save_area_size;
4354 /* Note UNITS_PER_WORD is 4 bytes or 8, depending on TARGET_64BIT. */
4355 if (int_arg_words < 8 )
4356 /* Adjust for the prologue's economy measure */
4357 gpr_save_area_size = (8 - int_arg_words) * UNITS_PER_WORD;
4358 else
4359 gpr_save_area_size = 0;
4361 if (!TARGET_SOFT_FLOAT && !TARGET_SINGLE_FLOAT)
4363 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4364 tree ovfl, gtop, ftop, goff, foff;
4365 tree gprv;
4366 int float_formals, fpr_offset, size_excess, floats_passed_in_regs;
4367 int fpr_save_offset;
4369 float_formals = current_function_args_info.fp_arg_words;
4370 /* If mips2, the number of formals is half the reported # of words */
4371 if (!TARGET_64BIT)
4372 float_formals /= 2;
4373 floats_passed_in_regs = (TARGET_64BIT ? 8 : 4);
4375 f_ovfl = TYPE_FIELDS (va_list_type_node);
4376 f_gtop = TREE_CHAIN (f_ovfl);
4377 f_ftop = TREE_CHAIN (f_gtop);
4378 f_goff = TREE_CHAIN (f_ftop);
4379 f_foff = TREE_CHAIN (f_goff);
4381 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4382 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4383 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4384 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4385 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4387 /* Emit code setting a pointer into the overflow (shared-stack) area.
4388 If there were more than 8 non-float formals, or more than 8
4389 float formals, then this pointer isn't to the base of the area.
4390 In that case, it must point to where the first vararg is. */
4391 size_excess = 0;
4392 if (float_formals > floats_passed_in_regs)
4393 size_excess += (float_formals-floats_passed_in_regs) * 8;
4394 if (int_arg_words > 8)
4395 size_excess += (int_arg_words-8) * UNITS_PER_WORD;
4397 /* FIXME: for mips2, the above size_excess can be wrong. Because the
4398 overflow stack holds mixed size items, there can be alignments,
4399 so that an 8 byte double following a 4 byte int will be on an
4400 8 byte boundary. This means that the above calculation should
4401 take into account the exact sequence of floats and non-floats
4402 which make up the excess. That calculation should be rolled
4403 into the code which sets the current_function_args_info struct.
4404 The above then reduces to a fetch from that struct. */
4407 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
4408 if (size_excess)
4409 t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
4410 build_int_2 (size_excess, 0));
4411 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4412 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4414 /* Emit code setting a ptr to the base of the overflow area. */
4415 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
4416 t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
4417 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4419 /* Emit code setting a pointer to the GPR save area.
4420 More precisely, a pointer to off-the-end of the FPR save area.
4421 If mips4, this is gpr_save_area_size below the overflow area.
4422 If mips2, also round down to an 8-byte boundary, since the FPR
4423 save area is 8-byte aligned, and GPR is 4-byte-aligned.
4424 Therefore there can be a 4-byte gap between the save areas. */
4425 gprv = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
4426 fpr_save_offset = gpr_save_area_size;
4427 if (!TARGET_64BIT)
4429 if (fpr_save_offset & 7)
4430 fpr_save_offset += 4;
4432 if (fpr_save_offset)
4433 gprv = build (PLUS_EXPR, TREE_TYPE (ftop), gprv,
4434 build_int_2 (-fpr_save_offset,-1));
4435 t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, gprv);
4436 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4438 /* Emit code initting an offset to the size of the GPR save area */
4439 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
4440 build_int_2 (gpr_save_area_size,0));
4441 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4443 /* Emit code initting an offset from ftop to the first float
4444 vararg. This varies in size, since any float
4445 varargs are put in the FPR save area after the formals.
4446 Note it's 8 bytes/formal regardless of TARGET_64BIT.
4447 However, mips2 stores 4 GPRs, mips4 stores 8 GPRs.
4448 If there are 8 or more float formals, init to zero.
4449 (In fact, the formals aren't stored in the bottom of the
4450 FPR save area: they are elsewhere, and the size of the FPR
4451 save area is economized by the prologue. But this code doesn't
4452 care. This design is unaffected by that fact.) */
4453 if (float_formals >= floats_passed_in_regs)
4454 fpr_offset = 0;
4455 else
4456 fpr_offset = (floats_passed_in_regs - float_formals) * 8;
4457 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
4458 build_int_2 (fpr_offset,0));
4459 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4461 else
4463 /* TARGET_SOFT_FLOAT or TARGET_SINGLE_FLOAT */
4465 /* Everything is in the GPR save area, or in the overflow
4466 area which is contiguous with it. */
4468 int offset = -gpr_save_area_size;
4469 if (gpr_save_area_size == 0)
4470 offset = (stdarg_p ? 0 : -UNITS_PER_WORD);
4471 nextarg = plus_constant (nextarg, offset);
4472 std_expand_builtin_va_start (1, valist, nextarg);
4475 else
4477 /* not EABI */
4478 int ofs;
4480 if (stdarg_p)
4481 ofs = 0;
4482 else
4484 /* ??? This had been conditional on
4485 _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32
4486 and both iris5.h and iris6.h define _MIPS_SIM. */
4487 if (mips_abi == ABI_N32 || mips_abi == ABI_64)
4488 ofs = (int_arg_words >= 8 ? -UNITS_PER_WORD : 0);
4489 else if (mips_abi == ABI_MEABI)
4490 ofs = (int_arg_words >= 8 ? -UNITS_PER_WORD : 0);
4491 else
4492 ofs = -UNITS_PER_WORD;
4495 nextarg = plus_constant (nextarg, ofs);
4496 std_expand_builtin_va_start (1, valist, nextarg);
4500 /* Implement va_arg. */
4503 mips_va_arg (valist, type)
4504 tree valist, type;
4506 HOST_WIDE_INT size, rsize;
4507 rtx addr_rtx;
4508 tree t;
4510 size = int_size_in_bytes (type);
4511 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4513 if (mips_abi == ABI_EABI)
4515 int indirect;
4516 rtx r, lab_over = NULL_RTX, lab_false;
4517 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4518 tree ovfl, gtop, ftop, goff, foff;
4520 indirect
4521 = function_arg_pass_by_reference (NULL, TYPE_MODE (type), type, 0);
4522 if (indirect)
4524 size = POINTER_SIZE / BITS_PER_UNIT;
4525 rsize = UNITS_PER_WORD;
4528 addr_rtx = gen_reg_rtx (Pmode);
4530 if (TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT)
4532 /* Case of all args in a merged stack. No need to check bounds,
4533 just advance valist along the stack. */
4535 tree gpr = valist;
4536 if (! indirect
4537 && ! TARGET_64BIT
4538 && TYPE_ALIGN (type) > (unsigned) BITS_PER_WORD)
4540 t = build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4541 build_int_2 (2*UNITS_PER_WORD - 1, 0));
4542 t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
4543 build_int_2 (-2*UNITS_PER_WORD, -1));
4544 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4545 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4548 t = build (POSTINCREMENT_EXPR, TREE_TYPE (gpr), gpr,
4549 size_int (rsize));
4550 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4551 if (r != addr_rtx)
4552 emit_move_insn (addr_rtx, r);
4554 /* flush the POSTINCREMENT */
4555 emit_queue();
4557 if (indirect)
4559 r = gen_rtx_MEM (Pmode, addr_rtx);
4560 set_mem_alias_set (r, get_varargs_alias_set ());
4561 emit_move_insn (addr_rtx, r);
4563 else
4565 if (BYTES_BIG_ENDIAN && rsize != size)
4566 addr_rtx = plus_constant (addr_rtx, rsize - size);
4568 return addr_rtx;
4571 /* Not a simple merged stack. Need ptrs and indexes left by va_start. */
4573 f_ovfl = TYPE_FIELDS (va_list_type_node);
4574 f_gtop = TREE_CHAIN (f_ovfl);
4575 f_ftop = TREE_CHAIN (f_gtop);
4576 f_goff = TREE_CHAIN (f_ftop);
4577 f_foff = TREE_CHAIN (f_goff);
4579 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl);
4580 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop);
4581 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop);
4582 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff);
4583 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff);
4585 lab_false = gen_label_rtx ();
4586 lab_over = gen_label_rtx ();
4588 if (TREE_CODE (type) == REAL_TYPE)
4591 /* Emit code to branch if foff == 0. */
4592 r = expand_expr (foff, NULL_RTX, TYPE_MODE (TREE_TYPE (foff)),
4593 EXPAND_NORMAL);
4594 emit_cmp_and_jump_insns (r, const0_rtx, EQ,
4595 const1_rtx, GET_MODE (r), 1, 1, lab_false);
4597 /* Emit code for addr_rtx = ftop - foff */
4598 t = build (MINUS_EXPR, TREE_TYPE (ftop), ftop, foff );
4599 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4600 if (r != addr_rtx)
4601 emit_move_insn (addr_rtx, r);
4603 /* Emit code for foff-=8.
4604 Advances the offset up FPR save area by one double */
4605 t = build (MINUS_EXPR, TREE_TYPE (foff), foff, build_int_2 (8, 0));
4606 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff, t);
4607 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4609 emit_queue();
4610 emit_jump (lab_over);
4611 emit_barrier ();
4612 emit_label (lab_false);
4614 if (!TARGET_64BIT)
4616 /* For mips2, the overflow area contains mixed size items.
4617 If a 4-byte int is followed by an 8-byte float, then
4618 natural alignment causes a 4 byte gap.
4619 So, dynamically adjust ovfl up to a multiple of 8. */
4620 t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), ovfl,
4621 build_int_2 (7, 0));
4622 t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl, t);
4623 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4624 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4627 /* Emit code for addr_rtx = the ovfl pointer into overflow area.
4628 Regardless of mips2, postincrement the ovfl pointer by 8. */
4629 t = build (POSTINCREMENT_EXPR, TREE_TYPE(ovfl), ovfl,
4630 size_int (8));
4631 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4632 if (r != addr_rtx)
4633 emit_move_insn (addr_rtx, r);
4635 emit_queue();
4636 emit_label (lab_over);
4637 return addr_rtx;
4639 else
4641 /* not REAL_TYPE */
4642 int step_size;
4644 if (! TARGET_64BIT
4645 && TREE_CODE (type) == INTEGER_TYPE
4646 && TYPE_PRECISION (type) == 64)
4648 /* In mips2, int takes 32 bits of the GPR save area, but
4649 longlong takes an aligned 64 bits. So, emit code
4650 to zero the low order bits of goff, thus aligning
4651 the later calculation of (gtop-goff) upwards. */
4652 t = build (BIT_AND_EXPR, TREE_TYPE (goff), goff,
4653 build_int_2 (-8, -1));
4654 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff, t);
4655 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4658 /* Emit code to branch if goff == 0. */
4659 r = expand_expr (goff, NULL_RTX, TYPE_MODE (TREE_TYPE (goff)),
4660 EXPAND_NORMAL);
4661 emit_cmp_and_jump_insns (r, const0_rtx, EQ,
4662 const1_rtx, GET_MODE (r), 1, 1, lab_false);
4664 /* Emit code for addr_rtx = gtop - goff. */
4665 t = build (MINUS_EXPR, TREE_TYPE (gtop), gtop, goff);
4666 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4667 if (r != addr_rtx)
4668 emit_move_insn (addr_rtx, r);
4670 /* Note that mips2 int is 32 bit, but mips2 longlong is 64. */
4671 if (! TARGET_64BIT && TYPE_PRECISION (type) == 64)
4672 step_size = 8;
4673 else
4674 step_size = UNITS_PER_WORD;
4676 /* Emit code for goff = goff - step_size.
4677 Advances the offset up GPR save area over the item. */
4678 t = build (MINUS_EXPR, TREE_TYPE (goff), goff,
4679 build_int_2 (step_size, 0));
4680 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff, t);
4681 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4683 emit_queue();
4684 emit_jump (lab_over);
4685 emit_barrier ();
4686 emit_label (lab_false);
4688 /* Emit code for addr_rtx -> overflow area, postinc by step_size */
4689 t = build (POSTINCREMENT_EXPR, TREE_TYPE(ovfl), ovfl,
4690 size_int (step_size));
4691 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4692 if (r != addr_rtx)
4693 emit_move_insn (addr_rtx, r);
4695 emit_queue();
4696 emit_label (lab_over);
4698 if (indirect)
4700 r = gen_rtx_MEM (Pmode, addr_rtx);
4701 set_mem_alias_set (r, get_varargs_alias_set ());
4702 emit_move_insn (addr_rtx, r);
4704 else
4706 if (BYTES_BIG_ENDIAN && rsize != size)
4707 addr_rtx = plus_constant (addr_rtx, rsize - size);
4709 return addr_rtx;
4712 else
4714 /* Not EABI. */
4715 int align;
4717 /* ??? The original va-mips.h did always align, despite the fact
4718 that alignments <= UNITS_PER_WORD are preserved by the va_arg
4719 increment mechanism. */
4721 if (TARGET_64BIT)
4722 align = 8;
4723 else if (TYPE_ALIGN (type) > 32)
4724 align = 8;
4725 else
4726 align = 4;
4728 t = build (PLUS_EXPR, TREE_TYPE (valist), valist,
4729 build_int_2 (align - 1, 0));
4730 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
4731 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4732 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4734 /* Everything past the alignment is standard. */
4735 return std_expand_builtin_va_arg (valist, type);
4739 /* Abort after printing out a specific insn. */
4741 static void
4742 abort_with_insn (insn, reason)
4743 rtx insn;
4744 const char *reason;
4746 error (reason);
4747 debug_rtx (insn);
4748 abort ();
4751 /* Set up the threshold for data to go into the small data area, instead
4752 of the normal data area, and detect any conflicts in the switches. */
4754 void
4755 override_options ()
4757 register int i, start;
4758 register int regno;
4759 register enum machine_mode mode;
4760 register enum processor_type mips_cpu;
4762 mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4764 if (mips_section_threshold <= 0)
4765 target_flags &= ~MASK_GPOPT;
4766 else if (optimize)
4767 target_flags |= MASK_GPOPT;
4769 /* If both single-float and soft-float are set, then clear the one that
4770 was set by TARGET_DEFAULT, leaving the one that was set by the
4771 user. We assume here that the specs prevent both being set by the
4772 user. */
4773 #ifdef TARGET_DEFAULT
4774 if (TARGET_SINGLE_FLOAT && TARGET_SOFT_FLOAT)
4775 target_flags &= ~((TARGET_DEFAULT) & (MASK_SOFT_FLOAT | MASK_SINGLE_FLOAT));
4776 #endif
4778 /* Get the architectural level. */
4779 if (mips_isa_string == 0)
4780 mips_isa = MIPS_ISA_DEFAULT;
4782 else if (ISDIGIT (*mips_isa_string))
4784 mips_isa = atoi (mips_isa_string);
4785 if (mips_isa == 16)
4787 /* -mno-mips16 overrides -mips16. */
4788 if (mips_no_mips16_string == NULL)
4790 target_flags |= MASK_MIPS16;
4791 if (TARGET_64BIT)
4792 mips_isa = 3;
4793 else
4794 mips_isa = MIPS_ISA_DEFAULT;
4796 else
4798 mips_isa = MIPS_ISA_DEFAULT;
4801 else if (mips_isa < 1
4802 || (mips_isa > 4
4803 && mips_isa != 32
4804 && mips_isa != 64))
4806 error ("-mips%d not supported", mips_isa);
4807 mips_isa = 1;
4811 else
4813 error ("bad value (%s) for -mips switch", mips_isa_string);
4814 mips_isa = 1;
4817 #ifdef MIPS_ABI_DEFAULT
4818 /* Get the ABI to use. */
4819 if (mips_abi_string == (char *) 0)
4820 mips_abi = MIPS_ABI_DEFAULT;
4821 else if (! strcmp (mips_abi_string, "32"))
4822 mips_abi = ABI_32;
4823 else if (! strcmp (mips_abi_string, "o64"))
4824 mips_abi = ABI_O64;
4825 else if (! strcmp (mips_abi_string, "n32"))
4826 mips_abi = ABI_N32;
4827 else if (! strcmp (mips_abi_string, "64"))
4828 mips_abi = ABI_64;
4829 else if (! strcmp (mips_abi_string, "eabi"))
4830 mips_abi = ABI_EABI;
4831 else if (! strcmp (mips_abi_string, "meabi"))
4832 mips_abi = ABI_MEABI;
4833 else
4834 error ("bad value (%s) for -mabi= switch", mips_abi_string);
4836 /* A specified ISA defaults the ABI if it was not specified. */
4837 if (mips_abi_string == 0 && mips_isa_string
4838 && mips_abi != ABI_EABI
4839 && mips_abi != ABI_O64
4840 && mips_abi != ABI_MEABI)
4842 if (mips_isa == 64)
4843 mips_abi = ABI_O64;
4844 else
4846 if (! ISA_HAS_64BIT_REGS)
4847 mips_abi = ABI_32;
4848 else
4849 mips_abi = ABI_64;
4853 #ifdef MIPS_CPU_STRING_DEFAULT
4854 /* A specified ABI defaults the ISA if it was not specified. */
4855 else if (mips_isa_string == 0 && mips_abi_string
4856 && mips_abi != ABI_EABI && mips_abi != ABI_O64)
4858 if (mips_abi == ABI_32)
4859 mips_isa = 1;
4860 else if (mips_abi == ABI_N32)
4861 mips_isa = 3;
4862 else
4863 mips_isa = 4;
4865 #endif
4867 /* If both ABI and ISA were specified, check for conflicts. */
4868 else if (mips_isa_string && mips_abi_string)
4870 if (! ISA_HAS_64BIT_REGS && (mips_abi == ABI_N32 || mips_abi == ABI_64
4871 || mips_abi == ABI_O64))
4872 error ("-mabi=%s does not support -mips%d", mips_abi_string, mips_isa);
4875 /* Override TARGET_DEFAULT if necessary. */
4876 if (mips_abi == ABI_32)
4877 target_flags &= ~ (MASK_FLOAT64|MASK_64BIT);
4879 /* If no type size setting options (-mlong64,-mint64,-mlong32) were used
4880 then set the type sizes. In the EABI in 64 bit mode, longs and
4881 pointers are 64 bits. Likewise for the SGI Irix6 N64 ABI. */
4882 if (mips_explicit_type_size_string == NULL
4883 && ((mips_abi == ABI_EABI && TARGET_64BIT)
4884 || mips_abi == ABI_64))
4885 target_flags |= MASK_LONG64;
4887 /* ??? This doesn't work yet, so don't let people try to use it. */
4888 if (mips_abi == ABI_32)
4889 error ("The -mabi=32 support does not work yet.");
4891 #else
4892 if (mips_abi_string)
4893 error ("This target does not support the -mabi switch.");
4894 #endif
4896 #ifdef MIPS_CPU_STRING_DEFAULT
4897 /* ??? There is a minor inconsistency here. If the user specifies an ISA
4898 greater than that supported by the default processor, then the user gets
4899 an error. Normally, the compiler will just default to the base level cpu
4900 for the indicated isa. */
4901 if (mips_arch_string == 0)
4902 mips_arch_string = MIPS_CPU_STRING_DEFAULT;
4903 if (mips_tune_string == 0)
4904 mips_tune_string = MIPS_CPU_STRING_DEFAULT;
4905 #endif
4907 /* Identify the processor type. */
4909 if (mips_cpu_string != 0)
4911 mips_cpu = mips_parse_cpu (mips_cpu_string);
4912 if (mips_cpu == PROCESSOR_DEFAULT)
4914 error ("bad value (%s) for -mcpu= switch", mips_arch_string);
4915 mips_cpu_string = "default";
4917 mips_arch = mips_cpu;
4918 mips_tune = mips_cpu;
4921 if (mips_arch_string == 0
4922 || ! strcmp (mips_arch_string, "default")
4923 || ! strcmp (mips_arch_string, "DEFAULT"))
4925 switch (mips_isa)
4927 default:
4928 mips_arch_string = "3000";
4929 mips_arch = PROCESSOR_R3000;
4930 break;
4931 case 2:
4932 mips_arch_string = "6000";
4933 mips_arch = PROCESSOR_R6000;
4934 break;
4935 case 3:
4936 mips_arch_string = "4000";
4937 mips_arch = PROCESSOR_R4000;
4938 break;
4939 case 4:
4940 mips_arch_string = "8000";
4941 mips_arch = PROCESSOR_R8000;
4942 break;
4943 case 32:
4944 mips_arch_string = "4kc";
4945 mips_arch = PROCESSOR_R4KC;
4946 break;
4947 case 64:
4948 mips_arch_string = "5kc";
4949 mips_arch = PROCESSOR_R5KC;
4950 break;
4953 else
4955 mips_arch = mips_parse_cpu (mips_arch_string);
4956 if (mips_arch == PROCESSOR_DEFAULT)
4958 error ("bad value (%s) for -march= switch", mips_arch_string);
4959 mips_arch_string = "default";
4962 if (mips_tune_string == 0
4963 || ! strcmp (mips_tune_string, "default")
4964 || ! strcmp (mips_tune_string, "DEFAULT"))
4966 if (mips_arch != PROCESSOR_DEFAULT)
4967 mips_tune = mips_arch;
4968 else
4969 switch (mips_isa)
4971 default:
4972 mips_tune_string = "3000";
4973 mips_tune = PROCESSOR_R3000;
4974 break;
4975 case 2:
4976 mips_tune_string = "6000";
4977 mips_tune = PROCESSOR_R6000;
4978 break;
4979 case 3:
4980 mips_tune_string = "4000";
4981 mips_tune = PROCESSOR_R4000;
4982 break;
4983 case 4:
4984 mips_tune_string = "8000";
4985 mips_tune = PROCESSOR_R8000;
4986 break;
4987 case 32:
4988 mips_arch_string = "4kc";
4989 mips_arch = PROCESSOR_R4KC;
4990 break;
4991 case 64:
4992 mips_arch_string = "5kc";
4993 mips_arch = PROCESSOR_R5KC;
4994 break;
4998 else
5000 mips_tune = mips_parse_cpu (mips_tune_string);
5001 if (mips_tune == PROCESSOR_DEFAULT)
5003 error ("bad value (%s) for -mtune= switch", mips_tune_string);
5004 mips_tune_string = "default";
5008 /* Handle processor configuration based on architecture. */
5009 if (TARGET_MIPS4100
5010 || TARGET_MIPS3900
5011 || TARGET_MIPS4KC
5012 || TARGET_MIPS5KC)
5013 target_flags |= MASK_SOFT_FLOAT;
5016 if ((mips_arch == PROCESSOR_R3000 && (mips_isa != 1))
5017 || (mips_arch == PROCESSOR_R4KC && mips_isa != 32)
5018 || ((mips_arch == PROCESSOR_R5KC
5019 || mips_arch == PROCESSOR_R20KC) && mips_isa != 64)
5020 || (mips_arch == PROCESSOR_R6000 && mips_isa != 1 && mips_isa != 2)
5021 || ((mips_arch == PROCESSOR_R4000
5022 || mips_arch == PROCESSOR_R4100
5023 || mips_arch == PROCESSOR_R4300
5024 || mips_arch == PROCESSOR_R4600
5025 || mips_arch == PROCESSOR_R4650)
5026 && mips_isa != 1 && mips_isa != 2 && mips_isa != 3))
5027 error ("-march=%s does not support -mips%d", mips_arch_string, mips_isa);
5029 /* make sure sizes of ints/longs/etc. are ok */
5030 if (! ISA_HAS_64BIT_REGS)
5032 if (TARGET_FLOAT64)
5034 error ("-mips%d does not support 64 bit fp registers", mips_isa);
5035 target_flags &= ~ MASK_FLOAT64;
5038 else if (TARGET_64BIT)
5040 error ("-mips%d does not support 64 bit gp registers", mips_isa);
5041 target_flags &= ~MASK_64BIT;
5045 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
5046 flag_pcc_struct_return = 0;
5048 /* Tell halfpic.c that we have half-pic code if we do. */
5049 if (TARGET_HALF_PIC)
5050 HALF_PIC_INIT ();
5052 /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined. We need
5053 to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work. */
5054 /* ??? -non_shared turns off pic code generation, but this is not
5055 implemented. */
5056 if (TARGET_ABICALLS)
5058 mips_abicalls = MIPS_ABICALLS_YES;
5059 flag_pic = 1;
5060 if (mips_section_threshold > 0)
5061 warning ("-G is incompatible with PIC code which is the default");
5063 else
5064 mips_abicalls = MIPS_ABICALLS_NO;
5066 /* -membedded-pic is a form of PIC code suitable for embedded
5067 systems. All calls are made using PC relative addressing, and
5068 all data is addressed using the $gp register. This requires gas,
5069 which does most of the work, and GNU ld, which automatically
5070 expands PC relative calls which are out of range into a longer
5071 instruction sequence. All gcc really does differently is
5072 generate a different sequence for a switch. */
5073 if (TARGET_EMBEDDED_PIC)
5075 flag_pic = 1;
5076 if (TARGET_ABICALLS)
5077 warning ("-membedded-pic and -mabicalls are incompatible");
5079 if (g_switch_set)
5080 warning ("-G and -membedded-pic are incompatible");
5082 /* Setting mips_section_threshold is not required, because gas
5083 will force everything to be GP addressable anyhow, but
5084 setting it will cause gcc to make better estimates of the
5085 number of instructions required to access a particular data
5086 item. */
5087 mips_section_threshold = 0x7fffffff;
5090 /* This optimization requires a linker that can support a R_MIPS_LO16
5091 relocation which is not immediately preceded by a R_MIPS_HI16 relocation.
5092 GNU ld has this support, but not all other MIPS linkers do, so we enable
5093 this optimization only if the user requests it, or if GNU ld is the
5094 standard linker for this configuration. */
5095 /* ??? This does not work when target addresses are DImode.
5096 This is because we are missing DImode high/lo_sum patterns. */
5097 if (TARGET_GAS && ! TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES && optimize && ! flag_pic
5098 && Pmode == SImode)
5099 mips_split_addresses = 1;
5100 else
5101 mips_split_addresses = 0;
5103 /* -mrnames says to use the MIPS software convention for register
5104 names instead of the hardware names (ie, $a0 instead of $4).
5105 We do this by switching the names in mips_reg_names, which the
5106 reg_names points into via the REGISTER_NAMES macro. */
5108 if (TARGET_NAME_REGS)
5109 memcpy (mips_reg_names, mips_sw_reg_names, sizeof (mips_reg_names));
5111 /* When compiling for the mips16, we can not use floating point. We
5112 record the original hard float value in mips16_hard_float. */
5113 if (TARGET_MIPS16)
5115 if (TARGET_SOFT_FLOAT)
5116 mips16_hard_float = 0;
5117 else
5118 mips16_hard_float = 1;
5119 target_flags |= MASK_SOFT_FLOAT;
5121 /* Don't run the scheduler before reload, since it tends to
5122 increase register pressure. */
5123 flag_schedule_insns = 0;
5126 /* We put -mentry in TARGET_OPTIONS rather than TARGET_SWITCHES only
5127 to avoid using up another bit in target_flags. */
5128 if (mips_entry_string != NULL)
5130 if (*mips_entry_string != '\0')
5131 error ("Invalid option `entry%s'", mips_entry_string);
5133 if (! TARGET_MIPS16)
5134 warning ("-mentry is only meaningful with -mips-16");
5135 else
5136 mips_entry = 1;
5139 /* We copy TARGET_MIPS16 into the mips16 global variable, so that
5140 attributes can access it. */
5141 if (TARGET_MIPS16)
5142 mips16 = 1;
5143 else
5144 mips16 = 0;
5146 /* Initialize the high and low values for legitimate floating point
5147 constants. Rather than trying to get the accuracy down to the
5148 last bit, just use approximate ranges. */
5149 dfhigh = REAL_VALUE_ATOF ("1.0e300", DFmode);
5150 dflow = REAL_VALUE_ATOF ("1.0e-300", DFmode);
5151 sfhigh = REAL_VALUE_ATOF ("1.0e38", SFmode);
5152 sflow = REAL_VALUE_ATOF ("1.0e-38", SFmode);
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;
5164 mips_print_operand_punct[']'] = 1;
5165 mips_print_operand_punct['<'] = 1;
5166 mips_print_operand_punct['>'] = 1;
5167 mips_print_operand_punct['{'] = 1;
5168 mips_print_operand_punct['}'] = 1;
5169 mips_print_operand_punct['^'] = 1;
5170 mips_print_operand_punct['$'] = 1;
5171 mips_print_operand_punct['+'] = 1;
5172 mips_print_operand_punct['~'] = 1;
5174 mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
5175 mips_char_to_class['e'] = M16_NA_REGS;
5176 mips_char_to_class['t'] = T_REG;
5177 mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
5178 mips_char_to_class['h'] = HI_REG;
5179 mips_char_to_class['l'] = LO_REG;
5180 mips_char_to_class['a'] = HILO_REG;
5181 mips_char_to_class['x'] = MD_REGS;
5182 mips_char_to_class['b'] = ALL_REGS;
5183 mips_char_to_class['y'] = GR_REGS;
5184 mips_char_to_class['z'] = ST_REGS;
5186 /* Set up array to map GCC register number to debug register number.
5187 Ignore the special purpose register numbers. */
5189 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5190 mips_dbx_regno[i] = -1;
5192 start = GP_DBX_FIRST - GP_REG_FIRST;
5193 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
5194 mips_dbx_regno[i] = i + start;
5196 start = FP_DBX_FIRST - FP_REG_FIRST;
5197 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
5198 mips_dbx_regno[i] = i + start;
5200 /* Set up array giving whether a given register can hold a given mode.
5201 At present, restrict ints from being in FP registers, because reload
5202 is a little enthusiastic about storing extra values in FP registers,
5203 and this is not good for things like OS kernels. Also, due to the
5204 mandatory delay, it is as fast to load from cached memory as to move
5205 from the FP register. */
5207 for (mode = VOIDmode;
5208 mode != MAX_MACHINE_MODE;
5209 mode = (enum machine_mode) ((int)mode + 1))
5211 register int size = GET_MODE_SIZE (mode);
5212 register enum mode_class class = GET_MODE_CLASS (mode);
5214 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5216 register int temp;
5218 if (mode == CCmode)
5220 if (! ISA_HAS_8CC)
5221 temp = (regno == FPSW_REGNUM);
5222 else
5223 temp = (ST_REG_P (regno) || GP_REG_P (regno)
5224 || FP_REG_P (regno));
5227 else if (GP_REG_P (regno))
5228 temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
5230 else if (FP_REG_P (regno))
5231 temp = ((TARGET_FLOAT64 || ((regno & 1) == 0)
5232 /* I think this change is OK regardless of abi, but
5233 I'm being cautions untill I can test this more.
5234 HARD_REGNO_MODE_OK is about whether or not you
5235 can move to and from a register without changing
5236 the value, not about whether math works on the
5237 register. */
5238 || (mips_abi == ABI_MEABI && size <= 4))
5239 && (class == MODE_FLOAT
5240 || class == MODE_COMPLEX_FLOAT
5241 || (TARGET_DEBUG_H_MODE && class == MODE_INT))
5242 && (! TARGET_SINGLE_FLOAT || size <= 4));
5244 else if (MD_REG_P (regno))
5245 temp = (class == MODE_INT
5246 && (size <= UNITS_PER_WORD
5247 || (regno == MD_REG_FIRST
5248 && size == 2 * UNITS_PER_WORD)));
5250 else
5251 temp = 0;
5253 mips_hard_regno_mode_ok[(int)mode][regno] = temp;
5257 /* Save GPR registers in word_mode sized hunks. word_mode hasn't been
5258 initialized yet, so we can't use that here. */
5259 gpr_mode = TARGET_64BIT ? DImode : SImode;
5261 /* Provide default values for align_* for 64-bit targets. */
5262 if (TARGET_64BIT && !TARGET_MIPS16)
5264 if (align_loops == 0)
5265 align_loops = 8;
5266 if (align_jumps == 0)
5267 align_jumps = 8;
5268 if (align_functions == 0)
5269 align_functions = 8;
5272 /* Register global variables with the garbage collector. */
5273 mips_add_gc_roots ();
5276 /* On the mips16, we want to allocate $24 (T_REG) before other
5277 registers for instructions for which it is possible. This helps
5278 avoid shuffling registers around in order to set up for an xor,
5279 encouraging the compiler to use a cmp instead. */
5281 void
5282 mips_order_regs_for_local_alloc ()
5284 register int i;
5286 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5287 reg_alloc_order[i] = i;
5289 if (TARGET_MIPS16)
5291 /* It really doesn't matter where we put register 0, since it is
5292 a fixed register anyhow. */
5293 reg_alloc_order[0] = 24;
5294 reg_alloc_order[24] = 0;
5299 /* The MIPS debug format wants all automatic variables and arguments
5300 to be in terms of the virtual frame pointer (stack pointer before
5301 any adjustment in the function), while the MIPS 3.0 linker wants
5302 the frame pointer to be the stack pointer after the initial
5303 adjustment. So, we do the adjustment here. The arg pointer (which
5304 is eliminated) points to the virtual frame pointer, while the frame
5305 pointer (which may be eliminated) points to the stack pointer after
5306 the initial adjustments. */
5308 HOST_WIDE_INT
5309 mips_debugger_offset (addr, offset)
5310 rtx addr;
5311 HOST_WIDE_INT offset;
5313 rtx offset2 = const0_rtx;
5314 rtx reg = eliminate_constant_term (addr, &offset2);
5316 if (offset == 0)
5317 offset = INTVAL (offset2);
5319 if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5320 || reg == hard_frame_pointer_rtx)
5322 HOST_WIDE_INT frame_size = (!current_frame_info.initialized)
5323 ? compute_frame_size (get_frame_size ())
5324 : current_frame_info.total_size;
5326 /* MIPS16 frame is smaller */
5327 if (frame_pointer_needed && TARGET_MIPS16)
5328 frame_size -= current_function_outgoing_args_size;
5330 offset = offset - frame_size;
5333 /* sdbout_parms does not want this to crash for unrecognized cases. */
5334 #if 0
5335 else if (reg != arg_pointer_rtx)
5336 abort_with_insn (addr, "mips_debugger_offset called with non stack/frame/arg pointer.");
5337 #endif
5339 return offset;
5342 /* A C compound statement to output to stdio stream STREAM the
5343 assembler syntax for an instruction operand X. X is an RTL
5344 expression.
5346 CODE is a value that can be used to specify one of several ways
5347 of printing the operand. It is used when identical operands
5348 must be printed differently depending on the context. CODE
5349 comes from the `%' specification that was used to request
5350 printing of the operand. If the specification was just `%DIGIT'
5351 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
5352 is the ASCII code for LTR.
5354 If X is a register, this macro should print the register's name.
5355 The names can be found in an array `reg_names' whose type is
5356 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
5358 When the machine description has a specification `%PUNCT' (a `%'
5359 followed by a punctuation character), this macro is called with
5360 a null pointer for X and the punctuation character for CODE.
5362 The MIPS specific codes are:
5364 'X' X is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5365 'x' X is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
5366 'd' output integer constant in decimal,
5367 'z' if the operand is 0, use $0 instead of normal operand.
5368 'D' print second part of double-word register or memory operand.
5369 'L' print low-order register of double-word register operand.
5370 'M' print high-order register of double-word register operand.
5371 'C' print part of opcode for a branch condition.
5372 'F' print part of opcode for a floating-point branch condition.
5373 'N' print part of opcode for a branch condition, inverted.
5374 'W' print part of opcode for a floating-point branch condition, inverted.
5375 'S' X is CODE_LABEL, print with prefix of "LS" (for embedded switch).
5376 'B' print 'z' for EQ, 'n' for NE
5377 'b' print 'n' for EQ, 'z' for NE
5378 'T' print 'f' for EQ, 't' for NE
5379 't' print 't' for EQ, 'f' for NE
5380 'Z' print register and a comma, but print nothing for $fcc0
5381 '(' Turn on .set noreorder
5382 ')' Turn on .set reorder
5383 '[' Turn on .set noat
5384 ']' Turn on .set at
5385 '<' Turn on .set nomacro
5386 '>' Turn on .set macro
5387 '{' Turn on .set volatile (not GAS)
5388 '}' Turn on .set novolatile (not GAS)
5389 '&' Turn on .set noreorder if filling delay slots
5390 '*' Turn on both .set noreorder and .set nomacro if filling delay slots
5391 '!' Turn on .set nomacro if filling delay slots
5392 '#' Print nop if in a .set noreorder section.
5393 '?' Print 'l' if we are to use a branch likely instead of normal branch.
5394 '@' Print the name of the assembler temporary register (at or $1).
5395 '.' Print the name of the register with a hard-wired zero (zero or $0).
5396 '^' Print the name of the pic call-through register (t9 or $25).
5397 '$' Print the name of the stack pointer register (sp or $29).
5398 '+' Print the name of the gp register (gp or $28).
5399 '~' Output an branch alignment to LABEL_ALIGN(NULL). */
5401 void
5402 print_operand (file, op, letter)
5403 FILE *file; /* file to write to */
5404 rtx op; /* operand to print */
5405 int letter; /* %<letter> or 0 */
5407 register enum rtx_code code;
5409 if (PRINT_OPERAND_PUNCT_VALID_P (letter))
5411 switch (letter)
5413 case '?':
5414 if (mips_branch_likely)
5415 putc ('l', file);
5416 break;
5418 case '@':
5419 fputs (reg_names [GP_REG_FIRST + 1], file);
5420 break;
5422 case '^':
5423 fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
5424 break;
5426 case '.':
5427 fputs (reg_names [GP_REG_FIRST + 0], file);
5428 break;
5430 case '$':
5431 fputs (reg_names[STACK_POINTER_REGNUM], file);
5432 break;
5434 case '+':
5435 fputs (reg_names[GP_REG_FIRST + 28], file);
5436 break;
5438 case '&':
5439 if (final_sequence != 0 && set_noreorder++ == 0)
5440 fputs (".set\tnoreorder\n\t", file);
5441 break;
5443 case '*':
5444 if (final_sequence != 0)
5446 if (set_noreorder++ == 0)
5447 fputs (".set\tnoreorder\n\t", file);
5449 if (set_nomacro++ == 0)
5450 fputs (".set\tnomacro\n\t", file);
5452 break;
5454 case '!':
5455 if (final_sequence != 0 && set_nomacro++ == 0)
5456 fputs ("\n\t.set\tnomacro", file);
5457 break;
5459 case '#':
5460 if (set_noreorder != 0)
5461 fputs ("\n\tnop", file);
5462 else if (TARGET_STATS)
5463 fputs ("\n\t#nop", file);
5465 break;
5467 case '(':
5468 if (set_noreorder++ == 0)
5469 fputs (".set\tnoreorder\n\t", file);
5470 break;
5472 case ')':
5473 if (set_noreorder == 0)
5474 error ("internal error: %%) found without a %%( in assembler pattern");
5476 else if (--set_noreorder == 0)
5477 fputs ("\n\t.set\treorder", file);
5479 break;
5481 case '[':
5482 if (set_noat++ == 0)
5483 fputs (".set\tnoat\n\t", file);
5484 break;
5486 case ']':
5487 if (set_noat == 0)
5488 error ("internal error: %%] found without a %%[ in assembler pattern");
5489 else if (--set_noat == 0)
5490 fputs ("\n\t.set\tat", file);
5492 break;
5494 case '<':
5495 if (set_nomacro++ == 0)
5496 fputs (".set\tnomacro\n\t", file);
5497 break;
5499 case '>':
5500 if (set_nomacro == 0)
5501 error ("internal error: %%> found without a %%< in assembler pattern");
5502 else if (--set_nomacro == 0)
5503 fputs ("\n\t.set\tmacro", file);
5505 break;
5507 case '{':
5508 if (set_volatile++ == 0)
5509 fprintf (file, "%s.set\tvolatile\n\t", TARGET_MIPS_AS ? "" : "#");
5510 break;
5512 case '}':
5513 if (set_volatile == 0)
5514 error ("internal error: %%} found without a %%{ in assembler pattern");
5515 else if (--set_volatile == 0)
5516 fprintf (file, "\n\t%s.set\tnovolatile", (TARGET_MIPS_AS) ? "" : "#");
5518 break;
5520 case '~':
5522 if (align_labels_log > 0)
5523 ASM_OUTPUT_ALIGN (file, align_labels_log);
5525 break;
5527 default:
5528 error ("PRINT_OPERAND: Unknown punctuation '%c'", letter);
5529 break;
5532 return;
5535 if (! op)
5537 error ("PRINT_OPERAND null pointer");
5538 return;
5541 code = GET_CODE (op);
5543 if (code == SIGN_EXTEND)
5544 op = XEXP (op, 0), code = GET_CODE (op);
5546 if (letter == 'C')
5547 switch (code)
5549 case EQ: fputs ("eq", file); break;
5550 case NE: fputs ("ne", file); break;
5551 case GT: fputs ("gt", file); break;
5552 case GE: fputs ("ge", file); break;
5553 case LT: fputs ("lt", file); break;
5554 case LE: fputs ("le", file); break;
5555 case GTU: fputs ("gtu", file); break;
5556 case GEU: fputs ("geu", file); break;
5557 case LTU: fputs ("ltu", file); break;
5558 case LEU: fputs ("leu", file); break;
5559 default:
5560 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%C");
5563 else if (letter == 'N')
5564 switch (code)
5566 case EQ: fputs ("ne", file); break;
5567 case NE: fputs ("eq", file); break;
5568 case GT: fputs ("le", file); break;
5569 case GE: fputs ("lt", file); break;
5570 case LT: fputs ("ge", file); break;
5571 case LE: fputs ("gt", file); break;
5572 case GTU: fputs ("leu", file); break;
5573 case GEU: fputs ("ltu", file); break;
5574 case LTU: fputs ("geu", file); break;
5575 case LEU: fputs ("gtu", file); break;
5576 default:
5577 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%N");
5580 else if (letter == 'F')
5581 switch (code)
5583 case EQ: fputs ("c1f", file); break;
5584 case NE: fputs ("c1t", file); break;
5585 default:
5586 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%F");
5589 else if (letter == 'W')
5590 switch (code)
5592 case EQ: fputs ("c1t", file); break;
5593 case NE: fputs ("c1f", file); break;
5594 default:
5595 abort_with_insn (op, "PRINT_OPERAND, invalid insn for %%W");
5598 else if (letter == 'S')
5600 char buffer[100];
5602 ASM_GENERATE_INTERNAL_LABEL (buffer, "LS", CODE_LABEL_NUMBER (op));
5603 assemble_name (file, buffer);
5606 else if (letter == 'Z')
5608 register int regnum;
5610 if (code != REG)
5611 abort ();
5613 regnum = REGNO (op);
5614 if (! ST_REG_P (regnum))
5615 abort ();
5617 if (regnum != ST_REG_FIRST)
5618 fprintf (file, "%s,", reg_names[regnum]);
5621 else if (code == REG || code == SUBREG)
5623 register int regnum;
5625 if (code == REG)
5626 regnum = REGNO (op);
5627 else
5628 regnum = true_regnum (op);
5630 if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
5631 || (letter == 'L' && WORDS_BIG_ENDIAN)
5632 || letter == 'D')
5633 regnum++;
5635 fprintf (file, "%s", reg_names[regnum]);
5638 else if (code == MEM)
5640 if (letter == 'D')
5641 output_address (plus_constant (XEXP (op, 0), 4));
5642 else
5643 output_address (XEXP (op, 0));
5646 else if (code == CONST_DOUBLE
5647 && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT)
5649 REAL_VALUE_TYPE d;
5650 char s[30];
5652 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
5653 REAL_VALUE_TO_DECIMAL (d, "%.20e", s);
5654 fprintf (file, s);
5657 else if (letter == 'x' && GET_CODE (op) == CONST_INT)
5658 fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
5660 else if (letter == 'X' && GET_CODE(op) == CONST_INT)
5661 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
5663 else if (letter == 'd' && GET_CODE(op) == CONST_INT)
5664 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
5666 else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
5667 fputs (reg_names[GP_REG_FIRST], file);
5669 else if (letter == 'd' || letter == 'x' || letter == 'X')
5670 output_operand_lossage ("invalid use of %%d, %%x, or %%X");
5672 else if (letter == 'B')
5673 fputs (code == EQ ? "z" : "n", file);
5674 else if (letter == 'b')
5675 fputs (code == EQ ? "n" : "z", file);
5676 else if (letter == 'T')
5677 fputs (code == EQ ? "f" : "t", file);
5678 else if (letter == 't')
5679 fputs (code == EQ ? "t" : "f", file);
5681 else if (code == CONST && GET_CODE (XEXP (op, 0)) == REG)
5683 /* This case arises on the mips16; see mips16_gp_pseudo_reg. */
5684 print_operand (file, XEXP (op, 0), letter);
5687 else if (TARGET_MIPS16 && code == CONST && mips16_gp_offset_p (op))
5689 fputs ("%gprel(", file);
5690 mips16_output_gp_offset (file, op);
5691 fputs (")", file);
5694 else
5695 output_addr_const (file, op);
5698 /* A C compound statement to output to stdio stream STREAM the
5699 assembler syntax for an instruction operand that is a memory
5700 reference whose address is ADDR. ADDR is an RTL expression.
5702 On some machines, the syntax for a symbolic address depends on
5703 the section that the address refers to. On these machines,
5704 define the macro `ENCODE_SECTION_INFO' to store the information
5705 into the `symbol_ref', and then check for it here. */
5707 void
5708 print_operand_address (file, addr)
5709 FILE *file;
5710 rtx addr;
5712 if (!addr)
5713 error ("PRINT_OPERAND_ADDRESS, null pointer");
5715 else
5716 switch (GET_CODE (addr))
5718 case REG:
5719 if (! TARGET_MIPS16 && REGNO (addr) == ARG_POINTER_REGNUM)
5720 abort_with_insn (addr, "Arg pointer not eliminated.");
5722 fprintf (file, "0(%s)", reg_names [REGNO (addr)]);
5723 break;
5725 case LO_SUM:
5727 register rtx arg0 = XEXP (addr, 0);
5728 register rtx arg1 = XEXP (addr, 1);
5730 if (! mips_split_addresses)
5731 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, Spurious LO_SUM.");
5733 if (GET_CODE (arg0) != REG)
5734 abort_with_insn (addr,
5735 "PRINT_OPERAND_ADDRESS, LO_SUM with #1 not REG.");
5737 fprintf (file, "%%lo(");
5738 print_operand_address (file, arg1);
5739 fprintf (file, ")(%s)", reg_names [REGNO (arg0)]);
5741 break;
5743 case PLUS:
5745 register rtx reg = 0;
5746 register rtx offset = 0;
5747 register rtx arg0 = XEXP (addr, 0);
5748 register rtx arg1 = XEXP (addr, 1);
5750 if (GET_CODE (arg0) == REG)
5752 reg = arg0;
5753 offset = arg1;
5754 if (GET_CODE (offset) == REG)
5755 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, 2 regs");
5758 else if (GET_CODE (arg1) == REG)
5759 reg = arg1, offset = arg0;
5760 else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
5762 output_addr_const (file, addr);
5763 break;
5765 else
5766 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, no regs");
5768 if (! CONSTANT_P (offset))
5769 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #2");
5771 if (REGNO (reg) == ARG_POINTER_REGNUM)
5772 abort_with_insn (addr, "Arg pointer not eliminated.");
5774 if (TARGET_MIPS16
5775 && GET_CODE (offset) == CONST
5776 && mips16_gp_offset_p (offset))
5778 fputs ("%gprel(", file);
5779 mips16_output_gp_offset (file, offset);
5780 fputs (")", file);
5782 else
5783 output_addr_const (file, offset);
5784 fprintf (file, "(%s)", reg_names [REGNO (reg)]);
5786 break;
5788 case LABEL_REF:
5789 case SYMBOL_REF:
5790 case CONST_INT:
5791 case CONST:
5792 output_addr_const (file, addr);
5793 break;
5795 default:
5796 abort_with_insn (addr, "PRINT_OPERAND_ADDRESS, invalid insn #1");
5797 break;
5802 /* If optimizing for the global pointer, keep track of all of the externs, so
5803 that at the end of the file, we can emit the appropriate .extern
5804 declaration for them, before writing out the text section. We assume all
5805 names passed to us are in the permanent obstack, so they will be valid at
5806 the end of the compilation.
5808 If we have -G 0, or the extern size is unknown, or the object is in a user
5809 specified section that is not .sbss/.sdata, don't bother emitting the
5810 .externs. In the case of user specified sections this behaviour is
5811 required as otherwise GAS will think the object lives in .sbss/.sdata. */
5814 mips_output_external (file, decl, name)
5815 FILE *file ATTRIBUTE_UNUSED;
5816 tree decl;
5817 const char *name;
5819 register struct extern_list *p;
5820 int len;
5821 tree section_name;
5823 if (TARGET_GP_OPT
5824 && TREE_CODE (decl) != FUNCTION_DECL
5825 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
5826 && ((section_name = DECL_SECTION_NAME (decl)) == NULL
5827 || strcmp (TREE_STRING_POINTER (section_name), ".sbss") == 0
5828 || strcmp (TREE_STRING_POINTER (section_name), ".sdata") == 0))
5830 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5831 p->next = extern_head;
5832 p->name = name;
5833 p->size = len;
5834 extern_head = p;
5837 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5838 if (TREE_CODE (decl) == FUNCTION_DECL
5839 /* ??? Don't include alloca, since gcc will always expand it
5840 inline. If we don't do this, the C++ library fails to build. */
5841 && strcmp (name, "alloca")
5842 /* ??? Don't include __builtin_next_arg, because then gcc will not
5843 bootstrap under Irix 5.1. */
5844 && strcmp (name, "__builtin_next_arg"))
5846 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5847 p->next = extern_head;
5848 p->name = name;
5849 p->size = -1;
5850 extern_head = p;
5852 #endif
5854 return 0;
5857 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
5859 mips_output_external_libcall (file, name)
5860 FILE *file;
5861 const char *name;
5863 register struct extern_list *p;
5865 p = (struct extern_list *) permalloc (sizeof (struct extern_list));
5866 p->next = extern_head;
5867 p->name = name;
5868 p->size = -1;
5869 extern_head = p;
5871 return 0;
5873 #endif
5875 /* Emit a new filename to a stream. If this is MIPS ECOFF, watch out
5876 for .file's that start within a function. If we are smuggling stabs, try to
5877 put out a MIPS ECOFF file and a stab. */
5879 void
5880 mips_output_filename (stream, name)
5881 FILE *stream;
5882 const char *name;
5884 static int first_time = 1;
5885 char ltext_label_name[100];
5887 if (first_time)
5889 first_time = 0;
5890 SET_FILE_NUMBER ();
5891 current_function_file = name;
5892 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5893 /* This tells mips-tfile that stabs will follow. */
5894 if (!TARGET_GAS && write_symbols == DBX_DEBUG)
5895 fprintf (stream, "\t#@stabs\n");
5898 else if (write_symbols == DBX_DEBUG)
5900 ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
5901 fprintf (stream, "%s", ASM_STABS_OP);
5902 output_quoted_string (stream, name);
5903 fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
5906 else if (name != current_function_file
5907 && strcmp (name, current_function_file) != 0)
5909 if (inside_function && !TARGET_GAS)
5911 if (!file_in_function_warning)
5913 file_in_function_warning = 1;
5914 ignore_line_number = 1;
5915 warning ("MIPS ECOFF format does not allow changing filenames within functions with #line");
5918 else
5920 SET_FILE_NUMBER ();
5921 current_function_file = name;
5922 ASM_OUTPUT_FILENAME (stream, num_source_filenames, name);
5927 /* Emit a linenumber. For encapsulated stabs, we need to put out a stab
5928 as well as a .loc, since it is possible that MIPS ECOFF might not be
5929 able to represent the location for inlines that come from a different
5930 file. */
5932 void
5933 mips_output_lineno (stream, line)
5934 FILE *stream;
5935 int line;
5937 if (write_symbols == DBX_DEBUG)
5939 ++sym_lineno;
5940 fprintf (stream, "%sLM%d:\n%s%d,0,%d,%sLM%d\n",
5941 LOCAL_LABEL_PREFIX, sym_lineno, ASM_STABN_OP, N_SLINE, line,
5942 LOCAL_LABEL_PREFIX, sym_lineno);
5944 else
5946 fprintf (stream, "\n\t%s.loc\t%d %d\n",
5947 (ignore_line_number) ? "#" : "",
5948 num_source_filenames, line);
5950 LABEL_AFTER_LOC (stream);
5954 /* Output an ASCII string, in a space-saving way. */
5956 void
5957 mips_output_ascii (stream, string_param, len)
5958 FILE *stream;
5959 const char *string_param;
5960 size_t len;
5962 size_t i;
5963 int cur_pos = 17;
5964 register const unsigned char *string =
5965 (const unsigned char *)string_param;
5967 fprintf (stream, "\t.ascii\t\"");
5968 for (i = 0; i < len; i++)
5970 register int c = string[i];
5972 switch (c)
5974 case '\"':
5975 case '\\':
5976 putc ('\\', stream);
5977 putc (c, stream);
5978 cur_pos += 2;
5979 break;
5981 case TARGET_NEWLINE:
5982 fputs ("\\n", stream);
5983 if (i+1 < len
5984 && (((c = string[i+1]) >= '\040' && c <= '~')
5985 || c == TARGET_TAB))
5986 cur_pos = 32767; /* break right here */
5987 else
5988 cur_pos += 2;
5989 break;
5991 case TARGET_TAB:
5992 fputs ("\\t", stream);
5993 cur_pos += 2;
5994 break;
5996 case TARGET_FF:
5997 fputs ("\\f", stream);
5998 cur_pos += 2;
5999 break;
6001 case TARGET_BS:
6002 fputs ("\\b", stream);
6003 cur_pos += 2;
6004 break;
6006 case TARGET_CR:
6007 fputs ("\\r", stream);
6008 cur_pos += 2;
6009 break;
6011 default:
6012 if (c >= ' ' && c < 0177)
6014 putc (c, stream);
6015 cur_pos++;
6017 else
6019 fprintf (stream, "\\%03o", c);
6020 cur_pos += 4;
6024 if (cur_pos > 72 && i+1 < len)
6026 cur_pos = 17;
6027 fprintf (stream, "\"\n\t.ascii\t\"");
6030 fprintf (stream, "\"\n");
6033 /* If defined, a C statement to be executed just prior to the output of
6034 assembler code for INSN, to modify the extracted operands so they will be
6035 output differently.
6037 Here the argument OPVEC is the vector containing the operands extracted
6038 from INSN, and NOPERANDS is the number of elements of the vector which
6039 contain meaningful data for this insn. The contents of this vector are
6040 what will be used to convert the insn template into assembler code, so you
6041 can change the assembler output by changing the contents of the vector.
6043 We use it to check if the current insn needs a nop in front of it because
6044 of load delays, and also to update the delay slot statistics. */
6046 /* ??? There is no real need for this function, because it never actually
6047 emits a NOP anymore. */
6049 void
6050 final_prescan_insn (insn, opvec, noperands)
6051 rtx insn;
6052 rtx opvec[] ATTRIBUTE_UNUSED;
6053 int noperands ATTRIBUTE_UNUSED;
6055 if (dslots_number_nops > 0)
6057 rtx pattern = PATTERN (insn);
6058 int length = get_attr_length (insn);
6060 /* Do we need to emit a NOP? */
6061 if (length == 0
6062 || (mips_load_reg != 0 && reg_mentioned_p (mips_load_reg, pattern))
6063 || (mips_load_reg2 != 0 && reg_mentioned_p (mips_load_reg2, pattern))
6064 || (mips_load_reg3 != 0 && reg_mentioned_p (mips_load_reg3, pattern))
6065 || (mips_load_reg4 != 0
6066 && reg_mentioned_p (mips_load_reg4, pattern)))
6067 fputs ("\t#nop\n", asm_out_file);
6069 else
6070 dslots_load_filled++;
6072 while (--dslots_number_nops > 0)
6073 fputs ("\t#nop\n", asm_out_file);
6075 mips_load_reg = 0;
6076 mips_load_reg2 = 0;
6077 mips_load_reg3 = 0;
6078 mips_load_reg4 = 0;
6081 if (TARGET_STATS
6082 && (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == CALL_INSN))
6083 dslots_jump_total++;
6086 /* Output at beginning of assembler file.
6088 If we are optimizing to use the global pointer, create a temporary file to
6089 hold all of the text stuff, and write it out to the end. This is needed
6090 because the MIPS assembler is evidently one pass, and if it hasn't seen the
6091 relevant .comm/.lcomm/.extern/.sdata declaration when the code is
6092 processed, it generates a two instruction sequence. */
6094 void
6095 mips_asm_file_start (stream)
6096 FILE *stream;
6098 const char * abi_string = NULL;
6100 ASM_OUTPUT_SOURCE_FILENAME (stream, main_input_filename);
6102 /* Versions of the MIPS assembler before 2.20 generate errors if a branch
6103 inside of a .set noreorder section jumps to a label outside of the .set
6104 noreorder section. Revision 2.20 just set nobopt silently rather than
6105 fixing the bug. */
6107 if (TARGET_MIPS_AS && optimize && flag_delayed_branch)
6108 fprintf (stream, "\t.set\tnobopt\n");
6110 if (TARGET_GAS)
6112 #if defined(OBJECT_FORMAT_ELF)
6113 /* Generate a special section to describe the ABI switches used to
6114 produce the resultant binary. This used to be done by the assembler
6115 setting bits in the ELF header's flags field, but we have run out of
6116 bits. GDB needs this information in order to be able to correctly
6117 debug these binaries. See the function mips_gdbarch_init() in
6118 gdb/mips-tdep.c. */
6120 switch (mips_abi)
6122 case ABI_32: abi_string = "abi32"; break;
6123 case ABI_N32: abi_string = "abiN32"; break;
6124 case ABI_64: abi_string = "abi64"; break;
6125 case ABI_O64: abi_string = "abiO64"; break;
6126 case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
6127 case ABI_MEABI:abi_string = TARGET_64BIT ? "meabi64" : "meabi32"; break;
6128 default:
6129 abort ();
6131 /* Note - we use fprintf directly rather than called named_section()
6132 because in this way we can avoid creating an allocated section. We
6133 do not want this section to take up any space in the running
6134 executable. */
6135 fprintf (stream, "\t.section .mdebug.%s\n", abi_string);
6137 /* Restore the default section. */
6138 fprintf (stream, "\t.previous\n");
6139 #endif
6144 /* Generate the pseudo ops that System V.4 wants. */
6145 #ifndef ABICALLS_ASM_OP
6146 #define ABICALLS_ASM_OP "\t.abicalls"
6147 #endif
6148 if (TARGET_ABICALLS)
6149 /* ??? but do not want this (or want pic0) if -non-shared? */
6150 fprintf (stream, "%s\n", ABICALLS_ASM_OP);
6152 if (TARGET_MIPS16)
6153 fprintf (stream, "\t.set\tmips16\n");
6155 /* This code exists so that we can put all externs before all symbol
6156 references. This is necessary for the MIPS assembler's global pointer
6157 optimizations to work. */
6158 if (TARGET_FILE_SWITCHING)
6160 asm_out_data_file = stream;
6161 asm_out_text_file = tmpfile ();
6163 else
6164 asm_out_data_file = asm_out_text_file = stream;
6166 if (flag_verbose_asm)
6167 fprintf (stream, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
6168 ASM_COMMENT_START,
6169 mips_section_threshold, mips_arch_string, mips_isa);
6172 /* If we are optimizing the global pointer, emit the text section now and any
6173 small externs which did not have .comm, etc that are needed. Also, give a
6174 warning if the data area is more than 32K and -pic because 3 instructions
6175 are needed to reference the data pointers. */
6177 void
6178 mips_asm_file_end (file)
6179 FILE *file;
6181 tree name_tree;
6182 struct extern_list *p;
6184 if (HALF_PIC_P ())
6186 HALF_PIC_FINISH (file);
6189 if (extern_head)
6191 fputs ("\n", file);
6193 for (p = extern_head; p != 0; p = p->next)
6195 name_tree = get_identifier (p->name);
6197 /* Positively ensure only one .extern for any given symbol. */
6198 if (! TREE_ASM_WRITTEN (name_tree))
6200 TREE_ASM_WRITTEN (name_tree) = 1;
6201 #ifdef ASM_OUTPUT_UNDEF_FUNCTION
6202 if (p->size == -1)
6203 ASM_OUTPUT_UNDEF_FUNCTION (file, p->name);
6204 else
6205 #endif
6207 fputs ("\t.extern\t", file);
6208 assemble_name (file, p->name);
6209 fprintf (file, ", %d\n", p->size);
6215 if (TARGET_FILE_SWITCHING)
6217 fprintf (file, "\n\t.text\n");
6218 copy_file_data (file, asm_out_text_file);
6222 static void
6223 copy_file_data (to, from)
6224 FILE *to, *from;
6226 char buffer[8192];
6227 size_t len;
6228 rewind (from);
6229 if (ferror (from))
6230 fatal_io_error ("can't rewind temp file");
6232 while ((len = fread (buffer, 1, sizeof (buffer), from)) > 0)
6233 if (fwrite (buffer, 1, len, to) != len)
6234 fatal_io_error ("can't write to output file");
6236 if (ferror (from))
6237 fatal_io_error ("can't read from temp file");
6239 if (fclose (from))
6240 fatal_io_error ("can't close temp file");
6243 /* Emit either a label, .comm, or .lcomm directive, and mark that the symbol
6244 is used, so that we don't emit an .extern for it in mips_asm_file_end. */
6246 void
6247 mips_declare_object (stream, name, init_string, final_string, size)
6248 FILE *stream;
6249 const char *name;
6250 const char *init_string;
6251 const char *final_string;
6252 int size;
6254 fputs (init_string, stream); /* "", "\t.comm\t", or "\t.lcomm\t" */
6255 assemble_name (stream, name);
6256 fprintf (stream, final_string, size); /* ":\n", ",%u\n", ",%u\n" */
6258 if (TARGET_GP_OPT)
6260 tree name_tree = get_identifier (name);
6261 TREE_ASM_WRITTEN (name_tree) = 1;
6265 /* Output a double precision value to the assembler. If both the
6266 host and target are IEEE, emit the values in hex. */
6268 void
6269 mips_output_double (stream, value)
6270 FILE *stream;
6271 REAL_VALUE_TYPE value;
6273 #ifdef REAL_VALUE_TO_TARGET_DOUBLE
6274 long value_long[2];
6275 REAL_VALUE_TO_TARGET_DOUBLE (value, value_long);
6277 fprintf (stream, "\t.word\t0x%08lx\t\t# %.20g\n\t.word\t0x%08lx\n",
6278 value_long[0], value, value_long[1]);
6279 #else
6280 fprintf (stream, "\t.double\t%.20g\n", value);
6281 #endif
6284 /* Output a single precision value to the assembler. If both the
6285 host and target are IEEE, emit the values in hex. */
6287 void
6288 mips_output_float (stream, value)
6289 FILE *stream;
6290 REAL_VALUE_TYPE value;
6292 #ifdef REAL_VALUE_TO_TARGET_SINGLE
6293 long value_long;
6294 REAL_VALUE_TO_TARGET_SINGLE (value, value_long);
6296 fprintf (stream, "\t.word\t0x%08lx\t\t# %.12g (float)\n", value_long, value);
6297 #else
6298 fprintf (stream, "\t.float\t%.12g\n", value);
6299 #endif
6302 /* Return the bytes needed to compute the frame pointer from the current
6303 stack pointer.
6305 Mips stack frames look like:
6307 Before call After call
6308 +-----------------------+ +-----------------------+
6309 high | | | |
6310 mem. | | | |
6311 | caller's temps. | | caller's temps. |
6312 | | | |
6313 +-----------------------+ +-----------------------+
6314 | | | |
6315 | arguments on stack. | | arguments on stack. |
6316 | | | |
6317 +-----------------------+ +-----------------------+
6318 | 4 words to save | | 4 words to save |
6319 | arguments passed | | arguments passed |
6320 | in registers, even | | in registers, even |
6321 SP->| if not passed. | VFP->| if not passed. |
6322 +-----------------------+ +-----------------------+
6324 | fp register save |
6326 +-----------------------+
6328 | gp register save |
6330 +-----------------------+
6332 | local variables |
6334 +-----------------------+
6336 | alloca allocations |
6338 +-----------------------+
6340 | GP save for V.4 abi |
6342 +-----------------------+
6344 | arguments on stack |
6346 +-----------------------+
6347 | 4 words to save |
6348 | arguments passed |
6349 | in registers, even |
6350 low SP->| if not passed. |
6351 memory +-----------------------+
6355 HOST_WIDE_INT
6356 compute_frame_size (size)
6357 HOST_WIDE_INT size; /* # of var. bytes allocated */
6359 unsigned int regno;
6360 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
6361 HOST_WIDE_INT var_size; /* # bytes that variables take up */
6362 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
6363 HOST_WIDE_INT extra_size; /* # extra bytes */
6364 HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6365 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
6366 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
6367 long mask; /* mask of saved gp registers */
6368 long fmask; /* mask of saved fp registers */
6369 int fp_inc; /* 1 or 2 depending on the size of fp regs */
6370 long fp_bits; /* bitmask to use for each fp register */
6372 gp_reg_size = 0;
6373 fp_reg_size = 0;
6374 mask = 0;
6375 fmask = 0;
6376 extra_size = MIPS_STACK_ALIGN (((TARGET_ABICALLS) ? UNITS_PER_WORD : 0));
6377 var_size = MIPS_STACK_ALIGN (size);
6378 args_size = MIPS_STACK_ALIGN (current_function_outgoing_args_size);
6380 /* The MIPS 3.0 linker does not like functions that dynamically
6381 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6382 looks like we are trying to create a second frame pointer to the
6383 function, so allocate some stack space to make it happy. */
6385 if (args_size == 0 && current_function_calls_alloca)
6386 args_size = 4 * UNITS_PER_WORD;
6388 total_size = var_size + args_size + extra_size;
6390 /* Calculate space needed for gp registers. */
6391 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6393 /* $18 is a special case on the mips16. It may be used to call
6394 a function which returns a floating point value, but it is
6395 marked in call_used_regs. $31 is also a special case. When
6396 not using -mentry, it will be used to copy a return value
6397 into the floating point registers if the return value is
6398 floating point. */
6399 if (MUST_SAVE_REGISTER (regno)
6400 || (TARGET_MIPS16
6401 && regno == GP_REG_FIRST + 18
6402 && regs_ever_live[regno])
6403 || (TARGET_MIPS16
6404 && regno == GP_REG_FIRST + 31
6405 && mips16_hard_float
6406 && ! mips_entry
6407 && ! aggregate_value_p (DECL_RESULT (current_function_decl))
6408 && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
6409 == MODE_FLOAT)
6410 && (! TARGET_SINGLE_FLOAT
6411 || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
6412 <= 4))))
6414 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6415 mask |= 1L << (regno - GP_REG_FIRST);
6417 /* The entry and exit pseudo instructions can not save $17
6418 without also saving $16. */
6419 if (mips_entry
6420 && regno == GP_REG_FIRST + 17
6421 && ! MUST_SAVE_REGISTER (GP_REG_FIRST + 16))
6423 gp_reg_size += UNITS_PER_WORD;
6424 mask |= 1L << 16;
6429 /* We need to restore these for the handler. */
6430 if (current_function_calls_eh_return)
6432 unsigned int i;
6433 for (i = 0; ; ++i)
6435 regno = EH_RETURN_DATA_REGNO (i);
6436 if (regno == INVALID_REGNUM)
6437 break;
6438 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6439 mask |= 1L << (regno - GP_REG_FIRST);
6443 /* Calculate space needed for fp registers. */
6444 if (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT)
6446 fp_inc = 1;
6447 fp_bits = 1;
6449 else
6451 fp_inc = 2;
6452 fp_bits = 3;
6455 /* This loop must iterate over the same space as its companion in
6456 save_restore_regs. */
6457 for (regno = (FP_REG_LAST - fp_inc + 1);
6458 regno >= FP_REG_FIRST;
6459 regno -= fp_inc)
6461 if (regs_ever_live[regno] && !call_used_regs[regno])
6463 fp_reg_size += fp_inc * UNITS_PER_FPREG;
6464 fmask |= fp_bits << (regno - FP_REG_FIRST);
6468 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6469 total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
6471 /* The gp reg is caller saved in the 32 bit ABI, so there is no need
6472 for leaf routines (total_size == extra_size) to save the gp reg.
6473 The gp reg is callee saved in the 64 bit ABI, so all routines must
6474 save the gp reg. This is not a leaf routine if -p, because of the
6475 call to mcount. */
6476 if (total_size == extra_size
6477 && (mips_abi == ABI_32 || mips_abi == ABI_O64 || mips_abi == ABI_EABI)
6478 && ! profile_flag)
6479 total_size = extra_size = 0;
6480 else if (TARGET_ABICALLS)
6482 /* Add the context-pointer to the saved registers. */
6483 gp_reg_size += UNITS_PER_WORD;
6484 mask |= 1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST);
6485 total_size -= gp_reg_rounded;
6486 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6487 total_size += gp_reg_rounded;
6490 /* Add in space reserved on the stack by the callee for storing arguments
6491 passed in registers. */
6492 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
6493 total_size += MIPS_STACK_ALIGN (current_function_pretend_args_size);
6495 /* The entry pseudo instruction will allocate 32 bytes on the stack. */
6496 if (mips_entry && total_size > 0 && total_size < 32)
6497 total_size = 32;
6499 /* Save other computed information. */
6500 current_frame_info.total_size = total_size;
6501 current_frame_info.var_size = var_size;
6502 current_frame_info.args_size = args_size;
6503 current_frame_info.extra_size = extra_size;
6504 current_frame_info.gp_reg_size = gp_reg_size;
6505 current_frame_info.fp_reg_size = fp_reg_size;
6506 current_frame_info.mask = mask;
6507 current_frame_info.fmask = fmask;
6508 current_frame_info.initialized = reload_completed;
6509 current_frame_info.num_gp = gp_reg_size / UNITS_PER_WORD;
6510 current_frame_info.num_fp = fp_reg_size / (fp_inc * UNITS_PER_FPREG);
6512 if (mask)
6514 unsigned long offset;
6516 /* When using mips_entry, the registers are always saved at the
6517 top of the stack. */
6518 if (! mips_entry)
6519 offset = (args_size + extra_size + var_size
6520 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
6521 else
6522 offset = total_size - GET_MODE_SIZE (gpr_mode);
6524 current_frame_info.gp_sp_offset = offset;
6525 current_frame_info.gp_save_offset = offset - total_size;
6527 else
6529 current_frame_info.gp_sp_offset = 0;
6530 current_frame_info.gp_save_offset = 0;
6533 if (fmask)
6535 unsigned long offset = (args_size + extra_size + var_size
6536 + gp_reg_rounded + fp_reg_size
6537 - fp_inc * UNITS_PER_FPREG);
6538 current_frame_info.fp_sp_offset = offset;
6539 current_frame_info.fp_save_offset = offset - total_size;
6541 else
6543 current_frame_info.fp_sp_offset = 0;
6544 current_frame_info.fp_save_offset = 0;
6547 /* Ok, we're done. */
6548 return total_size;
6551 /* Common code to emit the insns (or to write the instructions to a file)
6552 to save/restore registers.
6554 Other parts of the code assume that MIPS_TEMP1_REGNUM (aka large_reg)
6555 is not modified within save_restore_insns. */
6557 #define BITSET_P(VALUE,BIT) (((VALUE) & (1L << (BIT))) != 0)
6559 /* Emit instructions to load the value (SP + OFFSET) into MIPS_TEMP2_REGNUM
6560 and return an rtl expression for the register. Write the assembly
6561 instructions directly to FILE if it is not null, otherwise emit them as
6562 rtl.
6564 This function is a subroutine of save_restore_insns. It is used when
6565 OFFSET is too large to add in a single instruction. */
6567 static rtx
6568 mips_add_large_offset_to_sp (offset, file)
6569 HOST_WIDE_INT offset;
6570 FILE *file;
6572 rtx reg = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6573 if (file == 0)
6575 rtx offset_rtx = GEN_INT (offset);
6577 emit_move_insn (reg, offset_rtx);
6578 if (Pmode == DImode)
6579 emit_insn (gen_adddi3 (reg, reg, stack_pointer_rtx));
6580 else
6581 emit_insn (gen_addsi3 (reg, reg, stack_pointer_rtx));
6583 else
6585 fprintf (file, "\tli\t%s,0x%.08lx\t# ",
6586 reg_names[MIPS_TEMP2_REGNUM], (long) offset);
6587 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
6588 fprintf (file, "\n\t%s\t%s,%s,%s\n",
6589 Pmode == DImode ? "daddu" : "addu",
6590 reg_names[MIPS_TEMP2_REGNUM],
6591 reg_names[MIPS_TEMP2_REGNUM],
6592 reg_names[STACK_POINTER_REGNUM]);
6594 return reg;
6597 /* Make INSN frame related and note that it performs the frame-related
6598 operation DWARF_PATTERN. */
6600 static void
6601 mips_annotate_frame_insn (insn, dwarf_pattern)
6602 rtx insn, dwarf_pattern;
6604 RTX_FRAME_RELATED_P (insn) = 1;
6605 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6606 dwarf_pattern,
6607 REG_NOTES (insn));
6610 /* Return a frame-related rtx that stores register REGNO at (SP + OFFSET).
6611 The expression should only be used to store single registers. */
6613 static rtx
6614 mips_frame_set (mode, regno, offset)
6615 enum machine_mode mode;
6616 int regno;
6617 int offset;
6619 rtx address = plus_constant (stack_pointer_rtx, offset);
6620 rtx set = gen_rtx_SET (mode,
6621 gen_rtx_MEM (mode, address),
6622 gen_rtx_REG (mode, regno));
6623 RTX_FRAME_RELATED_P (set) = 1;
6624 return set;
6628 /* Emit a move instruction that stores REG in MEM. Make the instruction
6629 frame related and note that it stores REG at (SP + OFFSET). This
6630 function may be asked to store an FPR pair. */
6632 static void
6633 mips_emit_frame_related_store (mem, reg, offset)
6634 rtx mem;
6635 rtx reg;
6636 HOST_WIDE_INT offset;
6638 rtx dwarf_expr;
6640 if (GET_MODE (reg) == DFmode && ! TARGET_FLOAT64)
6642 /* Two registers are being stored, so the frame-related expression
6643 must be a PARALLEL rtx with one SET for each register. The
6644 higher numbered register is stored in the lower address on
6645 big-endian targets. */
6646 int regno1 = TARGET_BIG_ENDIAN ? REGNO (reg) + 1 : REGNO (reg);
6647 int regno2 = TARGET_BIG_ENDIAN ? REGNO (reg) : REGNO (reg) + 1;
6648 rtx set1 = mips_frame_set (SFmode, regno1, offset);
6649 rtx set2 = mips_frame_set (SFmode, regno2, offset + UNITS_PER_FPREG);
6650 dwarf_expr = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set1, set2));
6652 else
6653 dwarf_expr = mips_frame_set (GET_MODE (reg), REGNO (reg), offset);
6655 mips_annotate_frame_insn (emit_move_insn (mem, reg), dwarf_expr);
6658 static void
6659 save_restore_insns (store_p, large_reg, large_offset, file)
6660 int store_p; /* true if this is prologue */
6661 rtx large_reg; /* register holding large offset constant or NULL */
6662 long large_offset; /* large constant offset value */
6663 FILE *file; /* file to write instructions instead of making RTL */
6665 long mask = current_frame_info.mask;
6666 long fmask = current_frame_info.fmask;
6667 int regno;
6668 rtx base_reg_rtx;
6669 HOST_WIDE_INT base_offset;
6670 HOST_WIDE_INT gp_offset;
6671 HOST_WIDE_INT fp_offset;
6672 HOST_WIDE_INT end_offset;
6673 rtx insn;
6675 if (frame_pointer_needed
6676 && ! BITSET_P (mask, HARD_FRAME_POINTER_REGNUM - GP_REG_FIRST))
6677 abort ();
6679 if (mask == 0 && fmask == 0)
6680 return;
6682 /* Save registers starting from high to low. The debuggers prefer at least
6683 the return register be stored at func+4, and also it allows us not to
6684 need a nop in the epilog if at least one register is reloaded in
6685 addition to return address. */
6687 /* Save GP registers if needed. */
6688 if (mask)
6690 /* Pick which pointer to use as a base register. For small frames, just
6691 use the stack pointer. Otherwise, use a temporary register. Save 2
6692 cycles if the save area is near the end of a large frame, by reusing
6693 the constant created in the prologue/epilogue to adjust the stack
6694 frame. */
6696 gp_offset = current_frame_info.gp_sp_offset;
6697 end_offset
6698 = gp_offset - (current_frame_info.gp_reg_size
6699 - GET_MODE_SIZE (gpr_mode));
6701 if (gp_offset < 0 || end_offset < 0)
6702 internal_error
6703 ("gp_offset (%ld) or end_offset (%ld) is less than zero.",
6704 (long) gp_offset, (long) end_offset);
6706 /* If we see a large frame in mips16 mode, we save the registers
6707 before adjusting the stack pointer, and load them afterward. */
6708 else if (TARGET_MIPS16 && large_offset > 32767)
6709 base_reg_rtx = stack_pointer_rtx, base_offset = large_offset;
6711 else if (gp_offset < 32768)
6712 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
6714 else if (large_reg != 0
6715 && (unsigned HOST_WIDE_INT) (large_offset - gp_offset) < 32768
6716 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
6718 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6719 base_offset = large_offset;
6720 if (file == 0)
6722 if (Pmode == DImode)
6723 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
6724 stack_pointer_rtx));
6725 else
6726 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
6727 stack_pointer_rtx));
6729 else
6730 fprintf (file, "\t%s\t%s,%s,%s\n",
6731 Pmode == DImode ? "daddu" : "addu",
6732 reg_names[MIPS_TEMP2_REGNUM],
6733 reg_names[REGNO (large_reg)],
6734 reg_names[STACK_POINTER_REGNUM]);
6736 else
6738 base_offset = gp_offset;
6739 base_reg_rtx = mips_add_large_offset_to_sp (base_offset, file);
6742 /* When we restore the registers in MIPS16 mode, then if we are
6743 using a frame pointer, and this is not a large frame, the
6744 current stack pointer will be offset by
6745 current_function_outgoing_args_size. Doing it this way lets
6746 us avoid offsetting the frame pointer before copying it into
6747 the stack pointer; there is no instruction to set the stack
6748 pointer to the sum of a register and a constant. */
6749 if (TARGET_MIPS16
6750 && ! store_p
6751 && frame_pointer_needed
6752 && large_offset <= 32767)
6753 base_offset += current_function_outgoing_args_size;
6755 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
6756 if (BITSET_P (mask, regno - GP_REG_FIRST))
6758 if (file == 0)
6760 rtx reg_rtx;
6761 rtx mem_rtx
6762 = gen_rtx (MEM, gpr_mode,
6763 gen_rtx (PLUS, Pmode, base_reg_rtx,
6764 GEN_INT (gp_offset - base_offset)));
6766 if (! current_function_calls_eh_return)
6767 RTX_UNCHANGING_P (mem_rtx) = 1;
6769 /* The mips16 does not have an instruction to load
6770 $31, so we load $7 instead, and work things out
6771 in the caller. */
6772 if (TARGET_MIPS16 && ! store_p && regno == GP_REG_FIRST + 31)
6773 reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 7);
6774 /* The mips16 sometimes needs to save $18. */
6775 else if (TARGET_MIPS16
6776 && regno != GP_REG_FIRST + 31
6777 && ! M16_REG_P (regno))
6779 if (! store_p)
6780 reg_rtx = gen_rtx (REG, gpr_mode, 6);
6781 else
6783 reg_rtx = gen_rtx (REG, gpr_mode, 3);
6784 emit_move_insn (reg_rtx,
6785 gen_rtx (REG, gpr_mode, regno));
6788 else
6789 reg_rtx = gen_rtx (REG, gpr_mode, regno);
6791 if (store_p)
6792 mips_emit_frame_related_store (mem_rtx, reg_rtx, gp_offset);
6793 else if (!TARGET_ABICALLS
6794 || (mips_abi != ABI_32 && mips_abi != ABI_O64)
6795 || regno != (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
6797 emit_move_insn (reg_rtx, mem_rtx);
6798 if (TARGET_MIPS16
6799 && regno != GP_REG_FIRST + 31
6800 && ! M16_REG_P (regno))
6801 emit_move_insn (gen_rtx (REG, gpr_mode, regno),
6802 reg_rtx);
6805 else
6807 if (store_p || !TARGET_ABICALLS
6808 || (mips_abi != ABI_32 && mips_abi != ABI_O64)
6809 || regno != (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
6811 int r = regno;
6813 /* The mips16 does not have an instruction to
6814 load $31, so we load $7 instead, and work
6815 things out in the caller. */
6816 if (TARGET_MIPS16 && ! store_p && r == GP_REG_FIRST + 31)
6817 r = GP_REG_FIRST + 7;
6818 /* The mips16 sometimes needs to save $18. */
6819 if (TARGET_MIPS16
6820 && regno != GP_REG_FIRST + 31
6821 && ! M16_REG_P (regno))
6823 if (! store_p)
6824 r = GP_REG_FIRST + 6;
6825 else
6827 r = GP_REG_FIRST + 3;
6828 fprintf (file, "\tmove\t%s,%s\n",
6829 reg_names[r], reg_names[regno]);
6832 fprintf (file, "\t%s\t%s,",
6833 (TARGET_64BIT
6834 ? (store_p) ? "sd" : "ld"
6835 : (store_p) ? "sw" : "lw"),
6836 reg_names[r]);
6837 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
6838 gp_offset - base_offset);
6839 fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
6840 if (! store_p
6841 && TARGET_MIPS16
6842 && regno != GP_REG_FIRST + 31
6843 && ! M16_REG_P (regno))
6844 fprintf (file, "\tmove\t%s,%s\n",
6845 reg_names[regno], reg_names[r]);
6849 gp_offset -= GET_MODE_SIZE (gpr_mode);
6852 else
6853 base_reg_rtx = 0, base_offset = 0;
6855 /* Save floating point registers if needed. */
6856 if (fmask)
6858 int fp_inc = (TARGET_FLOAT64 || TARGET_SINGLE_FLOAT) ? 1 : 2;
6859 int fp_size = fp_inc * UNITS_PER_FPREG;
6861 /* Pick which pointer to use as a base register. */
6862 fp_offset = current_frame_info.fp_sp_offset;
6863 end_offset = fp_offset - (current_frame_info.fp_reg_size - fp_size);
6865 if (fp_offset < 0 || end_offset < 0)
6866 internal_error
6867 ("fp_offset (%ld) or end_offset (%ld) is less than zero.",
6868 (long) fp_offset, (long) end_offset);
6870 else if (fp_offset < 32768)
6871 base_reg_rtx = stack_pointer_rtx, base_offset = 0;
6873 else if (base_reg_rtx != 0
6874 && (unsigned HOST_WIDE_INT) (base_offset - fp_offset) < 32768
6875 && (unsigned HOST_WIDE_INT) (base_offset - end_offset) < 32768)
6876 ; /* already set up for gp registers above */
6878 else if (large_reg != 0
6879 && (unsigned HOST_WIDE_INT) (large_offset - fp_offset) < 32768
6880 && (unsigned HOST_WIDE_INT) (large_offset - end_offset) < 32768)
6882 base_reg_rtx = gen_rtx_REG (Pmode, MIPS_TEMP2_REGNUM);
6883 base_offset = large_offset;
6884 if (file == 0)
6886 if (Pmode == DImode)
6887 insn = emit_insn (gen_adddi3 (base_reg_rtx, large_reg,
6888 stack_pointer_rtx));
6889 else
6890 insn = emit_insn (gen_addsi3 (base_reg_rtx, large_reg,
6891 stack_pointer_rtx));
6894 else
6895 fprintf (file, "\t%s\t%s,%s,%s\n",
6896 Pmode == DImode ? "daddu" : "addu",
6897 reg_names[MIPS_TEMP2_REGNUM],
6898 reg_names[REGNO (large_reg)],
6899 reg_names[STACK_POINTER_REGNUM]);
6901 else
6903 base_offset = fp_offset;
6904 base_reg_rtx = mips_add_large_offset_to_sp (fp_offset, file);
6907 /* This loop must iterate over the same space as its companion in
6908 compute_frame_size. */
6909 for (regno = (FP_REG_LAST - fp_inc + 1);
6910 regno >= FP_REG_FIRST;
6911 regno -= fp_inc)
6912 if (BITSET_P (fmask, regno - FP_REG_FIRST))
6914 if (file == 0)
6916 enum machine_mode sz
6917 = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
6918 rtx reg_rtx = gen_rtx (REG, sz, regno);
6919 rtx mem_rtx = gen_rtx (MEM, sz,
6920 gen_rtx (PLUS, Pmode, base_reg_rtx,
6921 GEN_INT (fp_offset
6922 - base_offset)));
6923 if (! current_function_calls_eh_return)
6924 RTX_UNCHANGING_P (mem_rtx) = 1;
6926 if (store_p)
6927 mips_emit_frame_related_store (mem_rtx, reg_rtx, fp_offset);
6928 else
6929 emit_move_insn (reg_rtx, mem_rtx);
6931 else
6933 fprintf (file, "\t%s\t%s,",
6934 (TARGET_SINGLE_FLOAT
6935 ? (store_p ? "s.s" : "l.s")
6936 : (store_p ? "s.d" : "l.d")),
6937 reg_names[regno]);
6938 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
6939 fp_offset - base_offset);
6940 fprintf (file, "(%s)\n", reg_names[REGNO(base_reg_rtx)]);
6943 fp_offset -= fp_size;
6948 /* Set up the stack and frame (if desired) for the function. */
6950 static void
6951 mips_output_function_prologue (file, size)
6952 FILE *file;
6953 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
6955 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6956 const char *fnname;
6957 #endif
6958 long tsize = current_frame_info.total_size;
6960 ASM_OUTPUT_SOURCE_FILENAME (file, DECL_SOURCE_FILE (current_function_decl));
6962 #ifdef SDB_DEBUGGING_INFO
6963 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6964 ASM_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
6965 #endif
6967 /* In mips16 mode, we may need to generate a 32 bit to handle
6968 floating point arguments. The linker will arrange for any 32 bit
6969 functions to call this stub, which will then jump to the 16 bit
6970 function proper. */
6971 if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6972 && current_function_args_info.fp_code != 0)
6973 build_mips16_function_stub (file);
6975 inside_function = 1;
6977 #ifndef FUNCTION_NAME_ALREADY_DECLARED
6978 /* Get the function name the same way that toplev.c does before calling
6979 assemble_start_function. This is needed so that the name used here
6980 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6981 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6983 if (!flag_inhibit_size_directive)
6985 fputs ("\t.ent\t", file);
6986 assemble_name (file, fnname);
6987 fputs ("\n", file);
6990 assemble_name (file, fnname);
6991 fputs (":\n", file);
6992 #endif
6994 if (!flag_inhibit_size_directive)
6996 /* .frame FRAMEREG, FRAMESIZE, RETREG */
6997 fprintf (file,
6998 "\t.frame\t%s,%ld,%s\t\t# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
6999 (reg_names[(frame_pointer_needed)
7000 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
7001 ((frame_pointer_needed && TARGET_MIPS16)
7002 ? (tsize - current_function_outgoing_args_size)
7003 : tsize),
7004 reg_names[31 + GP_REG_FIRST],
7005 current_frame_info.var_size,
7006 current_frame_info.num_gp,
7007 current_frame_info.num_fp,
7008 current_function_outgoing_args_size,
7009 current_frame_info.extra_size);
7011 /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
7012 fprintf (file, "\t.mask\t0x%08lx,%ld\n\t.fmask\t0x%08lx,%ld\n",
7013 current_frame_info.mask,
7014 current_frame_info.gp_save_offset,
7015 current_frame_info.fmask,
7016 current_frame_info.fp_save_offset);
7018 /* Require:
7019 OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
7020 HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
7023 if (mips_entry && ! mips_can_use_return_insn ())
7025 int save16 = BITSET_P (current_frame_info.mask, 16);
7026 int save17 = BITSET_P (current_frame_info.mask, 17);
7027 int save31 = BITSET_P (current_frame_info.mask, 31);
7028 int savearg = 0;
7029 rtx insn;
7031 /* Look through the initial insns to see if any of them store
7032 the function parameters into the incoming parameter storage
7033 area. If they do, we delete the insn, and save the register
7034 using the entry pseudo-instruction instead. We don't try to
7035 look past a label, jump, or call. */
7036 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
7038 rtx note, set, src, dest, base, offset;
7039 int hireg;
7041 if (GET_CODE (insn) == CODE_LABEL
7042 || GET_CODE (insn) == JUMP_INSN
7043 || GET_CODE (insn) == CALL_INSN)
7044 break;
7045 if (GET_CODE (insn) != INSN)
7046 continue;
7047 set = PATTERN (insn);
7048 if (GET_CODE (set) != SET)
7049 continue;
7051 /* An insn storing a function parameter will still have a
7052 REG_EQUIV note on it mentioning the argument pointer. */
7053 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
7054 if (note == NULL_RTX)
7055 continue;
7056 if (! reg_mentioned_p (arg_pointer_rtx, XEXP (note, 0)))
7057 continue;
7059 src = SET_SRC (set);
7060 if (GET_CODE (src) != REG
7061 || REGNO (src) < GP_REG_FIRST + 4
7062 || REGNO (src) > GP_REG_FIRST + 7)
7063 continue;
7065 dest = SET_DEST (set);
7066 if (GET_CODE (dest) != MEM)
7067 continue;
7068 if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned) UNITS_PER_WORD)
7070 else if (GET_MODE_SIZE (GET_MODE (dest)) == (unsigned)2 * UNITS_PER_WORD
7071 && REGNO (src) < GP_REG_FIRST + 7)
7073 else
7074 continue;
7075 offset = const0_rtx;
7076 base = eliminate_constant_term (XEXP (dest, 0), &offset);
7077 if (GET_CODE (base) != REG
7078 || GET_CODE (offset) != CONST_INT)
7079 continue;
7080 if (REGNO (base) == (unsigned) STACK_POINTER_REGNUM
7081 && (unsigned HOST_WIDE_INT) INTVAL (offset)
7082 == tsize + (REGNO (src) - 4) * UNITS_PER_WORD)
7084 else if (REGNO (base) == (unsigned) HARD_FRAME_POINTER_REGNUM
7085 && ((unsigned HOST_WIDE_INT) INTVAL (offset)
7086 == (tsize
7087 + (REGNO (src) - 4) * UNITS_PER_WORD
7088 - current_function_outgoing_args_size)))
7090 else
7091 continue;
7093 /* This insn stores a parameter onto the stack, in the same
7094 location where the entry pseudo-instruction will put it.
7095 Delete the insn, and arrange to tell the entry
7096 instruction to save the register. */
7097 PUT_CODE (insn, NOTE);
7098 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7099 NOTE_SOURCE_FILE (insn) = 0;
7101 hireg = (REGNO (src)
7102 + HARD_REGNO_NREGS (REGNO (src), GET_MODE (dest))
7103 - 1);
7104 if (hireg > savearg)
7105 savearg = hireg;
7108 /* If this is a varargs function, we need to save all the
7109 registers onto the stack anyhow. */
7110 if (current_function_stdarg || current_function_varargs)
7111 savearg = GP_REG_FIRST + 7;
7113 fprintf (file, "\tentry\t");
7114 if (savearg > 0)
7116 if (savearg == GP_REG_FIRST + 4)
7117 fprintf (file, "%s", reg_names[savearg]);
7118 else
7119 fprintf (file, "%s-%s", reg_names[GP_REG_FIRST + 4],
7120 reg_names[savearg]);
7122 if (save16 || save17)
7124 if (savearg > 0)
7125 fprintf (file, ",");
7126 fprintf (file, "%s", reg_names[GP_REG_FIRST + 16]);
7127 if (save17)
7128 fprintf (file, "-%s", reg_names[GP_REG_FIRST + 17]);
7130 if (save31)
7132 if (savearg > 0 || save16 || save17)
7133 fprintf (file, ",");
7134 fprintf (file, "%s", reg_names[GP_REG_FIRST + 31]);
7136 fprintf (file, "\n");
7139 if (TARGET_ABICALLS && (mips_abi == ABI_32 || mips_abi == ABI_O64))
7141 const char *const sp_str = reg_names[STACK_POINTER_REGNUM];
7143 fprintf (file, "\t.set\tnoreorder\n\t.cpload\t%s\n\t.set\treorder\n",
7144 reg_names[PIC_FUNCTION_ADDR_REGNUM]);
7145 if (tsize > 0)
7147 fprintf (file, "\t%s\t%s,%s,%ld\n",
7148 (Pmode == DImode ? "dsubu" : "subu"),
7149 sp_str, sp_str, tsize);
7150 fprintf (file, "\t.cprestore %ld\n", current_frame_info.args_size);
7153 if (dwarf2out_do_frame ())
7154 dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, tsize);
7158 /* Expand the prologue into a bunch of separate insns. */
7160 void
7161 mips_expand_prologue ()
7163 int regno;
7164 HOST_WIDE_INT tsize;
7165 rtx tmp_rtx = 0;
7166 int last_arg_is_vararg_marker = 0;
7167 tree fndecl = current_function_decl;
7168 tree fntype = TREE_TYPE (fndecl);
7169 tree fnargs = DECL_ARGUMENTS (fndecl);
7170 rtx next_arg_reg;
7171 int i;
7172 tree next_arg;
7173 tree cur_arg;
7174 CUMULATIVE_ARGS args_so_far;
7175 rtx reg_18_save = NULL_RTX;
7176 int store_args_on_stack = (mips_abi == ABI_32 || mips_abi == ABI_O64)
7177 && (! mips_entry || mips_can_use_return_insn ());
7179 /* If struct value address is treated as the first argument, make it so. */
7180 if (aggregate_value_p (DECL_RESULT (fndecl))
7181 && ! current_function_returns_pcc_struct
7182 && struct_value_incoming_rtx == 0)
7184 tree type = build_pointer_type (fntype);
7185 tree function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
7187 DECL_ARG_TYPE (function_result_decl) = type;
7188 TREE_CHAIN (function_result_decl) = fnargs;
7189 fnargs = function_result_decl;
7192 /* For arguments passed in registers, find the register number
7193 of the first argument in the variable part of the argument list,
7194 otherwise GP_ARG_LAST+1. Note also if the last argument is
7195 the varargs special argument, and treat it as part of the
7196 variable arguments.
7198 This is only needed if store_args_on_stack is true. */
7200 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
7201 regno = GP_ARG_FIRST;
7203 for (cur_arg = fnargs; cur_arg != 0; cur_arg = next_arg)
7205 tree passed_type = DECL_ARG_TYPE (cur_arg);
7206 enum machine_mode passed_mode = TYPE_MODE (passed_type);
7207 rtx entry_parm;
7209 if (TREE_ADDRESSABLE (passed_type))
7211 passed_type = build_pointer_type (passed_type);
7212 passed_mode = Pmode;
7215 entry_parm = FUNCTION_ARG (args_so_far, passed_mode, passed_type, 1);
7217 FUNCTION_ARG_ADVANCE (args_so_far, passed_mode, passed_type, 1);
7218 next_arg = TREE_CHAIN (cur_arg);
7220 if (entry_parm && store_args_on_stack)
7222 if (next_arg == 0
7223 && DECL_NAME (cur_arg)
7224 && ((0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7225 "__builtin_va_alist"))
7226 || (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)),
7227 "va_alist"))))
7229 last_arg_is_vararg_marker = 1;
7230 break;
7232 else
7234 int words;
7236 if (GET_CODE (entry_parm) != REG)
7237 abort ();
7239 /* passed in a register, so will get homed automatically */
7240 if (GET_MODE (entry_parm) == BLKmode)
7241 words = (int_size_in_bytes (passed_type) + 3) / 4;
7242 else
7243 words = (GET_MODE_SIZE (GET_MODE (entry_parm)) + 3) / 4;
7245 regno = REGNO (entry_parm) + words - 1;
7248 else
7250 regno = GP_ARG_LAST+1;
7251 break;
7255 /* In order to pass small structures by value in registers compatibly with
7256 the MIPS compiler, we need to shift the value into the high part of the
7257 register. Function_arg has encoded a PARALLEL rtx, holding a vector of
7258 adjustments to be made as the next_arg_reg variable, so we split up the
7259 insns, and emit them separately. */
7261 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1);
7262 if (next_arg_reg != 0 && GET_CODE (next_arg_reg) == PARALLEL)
7264 rtvec adjust = XVEC (next_arg_reg, 0);
7265 int num = GET_NUM_ELEM (adjust);
7267 for (i = 0; i < num; i++)
7269 rtx insn, pattern;
7271 pattern = RTVEC_ELT (adjust, i);
7272 if (GET_CODE (pattern) != SET
7273 || GET_CODE (SET_SRC (pattern)) != ASHIFT)
7274 abort_with_insn (pattern, "Insn is not a shift");
7275 PUT_CODE (SET_SRC (pattern), ASHIFTRT);
7277 insn = emit_insn (pattern);
7279 /* Global life information isn't valid at this point, so we
7280 can't check whether these shifts are actually used. Mark
7281 them MAYBE_DEAD so that flow2 will remove them, and not
7282 complain about dead code in the prologue. */
7283 REG_NOTES(insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, NULL_RTX,
7284 REG_NOTES (insn));
7288 tsize = compute_frame_size (get_frame_size ());
7290 /* If this function is a varargs function, store any registers that
7291 would normally hold arguments ($4 - $7) on the stack. */
7292 if (store_args_on_stack
7293 && ((TYPE_ARG_TYPES (fntype) != 0
7294 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
7295 != void_type_node))
7296 || last_arg_is_vararg_marker))
7298 int offset = (regno - GP_ARG_FIRST) * UNITS_PER_WORD;
7299 rtx ptr = stack_pointer_rtx;
7301 /* If we are doing svr4-abi, sp has already been decremented by tsize. */
7302 if (TARGET_ABICALLS)
7303 offset += tsize;
7305 for (; regno <= GP_ARG_LAST; regno++)
7307 if (offset != 0)
7308 ptr = gen_rtx (PLUS, Pmode, stack_pointer_rtx, GEN_INT (offset));
7309 emit_move_insn (gen_rtx (MEM, gpr_mode, ptr),
7310 gen_rtx (REG, gpr_mode, regno));
7312 offset += GET_MODE_SIZE (gpr_mode);
7316 /* If we are using the entry pseudo instruction, it will
7317 automatically subtract 32 from the stack pointer, so we don't
7318 need to. The entry pseudo instruction is emitted by
7319 function_prologue. */
7320 if (mips_entry && ! mips_can_use_return_insn ())
7322 if (tsize > 0 && tsize <= 32 && frame_pointer_needed)
7324 rtx insn;
7326 /* If we are using a frame pointer with a small stack frame,
7327 we need to initialize it here since it won't be done
7328 below. */
7329 if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7331 rtx incr = GEN_INT (current_function_outgoing_args_size);
7332 if (Pmode == DImode)
7333 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7334 stack_pointer_rtx,
7335 incr));
7336 else
7337 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7338 stack_pointer_rtx,
7339 incr));
7341 else if (Pmode == DImode)
7342 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7343 stack_pointer_rtx));
7344 else
7345 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7346 stack_pointer_rtx));
7348 RTX_FRAME_RELATED_P (insn) = 1;
7351 /* We may need to save $18, if it is used to call a function
7352 which may return a floating point value. Set up a sequence
7353 of instructions to do so. Later on we emit them at the right
7354 moment. */
7355 if (TARGET_MIPS16 && BITSET_P (current_frame_info.mask, 18))
7357 rtx reg_rtx = gen_rtx (REG, gpr_mode, GP_REG_FIRST + 3);
7358 long gp_offset, base_offset;
7360 gp_offset = current_frame_info.gp_sp_offset;
7361 if (BITSET_P (current_frame_info.mask, 16))
7362 gp_offset -= UNITS_PER_WORD;
7363 if (BITSET_P (current_frame_info.mask, 17))
7364 gp_offset -= UNITS_PER_WORD;
7365 if (BITSET_P (current_frame_info.mask, 31))
7366 gp_offset -= UNITS_PER_WORD;
7367 if (tsize > 32767)
7368 base_offset = tsize;
7369 else
7370 base_offset = 0;
7371 start_sequence ();
7372 emit_move_insn (reg_rtx,
7373 gen_rtx (REG, gpr_mode, GP_REG_FIRST + 18));
7374 emit_move_insn (gen_rtx (MEM, gpr_mode,
7375 gen_rtx (PLUS, Pmode, stack_pointer_rtx,
7376 GEN_INT (gp_offset
7377 - base_offset))),
7378 reg_rtx);
7379 reg_18_save = gen_sequence ();
7380 end_sequence ();
7383 if (tsize > 32)
7384 tsize -= 32;
7385 else
7387 tsize = 0;
7388 if (reg_18_save != NULL_RTX)
7389 emit_insn (reg_18_save);
7393 if (tsize > 0)
7395 rtx tsize_rtx = GEN_INT (tsize);
7397 /* If we are doing svr4-abi, sp move is done by
7398 function_prologue. In mips16 mode with a large frame, we
7399 save the registers before adjusting the stack. */
7400 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7401 && (!TARGET_MIPS16 || tsize <= 32767))
7403 rtx adjustment_rtx, insn, dwarf_pattern;
7405 if (tsize > 32767)
7407 adjustment_rtx = gen_rtx (REG, Pmode, MIPS_TEMP1_REGNUM);
7408 emit_move_insn (adjustment_rtx, tsize_rtx);
7410 else
7411 adjustment_rtx = tsize_rtx;
7413 if (Pmode == DImode)
7414 insn = emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx,
7415 adjustment_rtx));
7416 else
7417 insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
7418 adjustment_rtx));
7420 dwarf_pattern = gen_rtx_SET (Pmode, stack_pointer_rtx,
7421 plus_constant (stack_pointer_rtx,
7422 -tsize));
7424 mips_annotate_frame_insn (insn, dwarf_pattern);
7427 if (! mips_entry)
7428 save_restore_insns (1, tmp_rtx, tsize, (FILE *)0);
7429 else if (reg_18_save != NULL_RTX)
7430 emit_insn (reg_18_save);
7432 if ((!TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7433 && TARGET_MIPS16
7434 && tsize > 32767)
7436 rtx reg_rtx;
7438 if (!frame_pointer_needed)
7439 abort ();
7441 reg_rtx = gen_rtx (REG, Pmode, 3);
7442 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
7443 emit_move_insn (reg_rtx, tsize_rtx);
7444 if (Pmode == DImode)
7445 emit_insn (gen_subdi3 (hard_frame_pointer_rtx,
7446 hard_frame_pointer_rtx,
7447 reg_rtx));
7448 else
7449 emit_insn (gen_subsi3 (hard_frame_pointer_rtx,
7450 hard_frame_pointer_rtx,
7451 reg_rtx));
7452 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
7455 if (frame_pointer_needed)
7457 rtx insn = 0;
7459 /* On the mips16, we encourage the use of unextended
7460 instructions when using the frame pointer by pointing the
7461 frame pointer ahead of the argument space allocated on
7462 the stack. */
7463 if ((! TARGET_ABICALLS || (mips_abi != ABI_32 && mips_abi != ABI_O64))
7464 && TARGET_MIPS16
7465 && tsize > 32767)
7467 /* In this case, we have already copied the stack
7468 pointer into the frame pointer, above. We need only
7469 adjust for the outgoing argument size. */
7470 if (current_function_outgoing_args_size != 0)
7472 rtx incr = GEN_INT (current_function_outgoing_args_size);
7473 if (Pmode == DImode)
7474 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7475 hard_frame_pointer_rtx,
7476 incr));
7477 else
7478 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7479 hard_frame_pointer_rtx,
7480 incr));
7483 else if (TARGET_MIPS16 && current_function_outgoing_args_size != 0)
7485 rtx incr = GEN_INT (current_function_outgoing_args_size);
7486 if (Pmode == DImode)
7487 insn = emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7488 stack_pointer_rtx,
7489 incr));
7490 else
7491 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7492 stack_pointer_rtx,
7493 incr));
7495 else if (Pmode == DImode)
7496 insn = emit_insn (gen_movdi (hard_frame_pointer_rtx,
7497 stack_pointer_rtx));
7498 else
7499 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
7500 stack_pointer_rtx));
7502 if (insn)
7503 RTX_FRAME_RELATED_P (insn) = 1;
7506 if (TARGET_ABICALLS && (mips_abi != ABI_32 && mips_abi != ABI_O64))
7507 emit_insn (gen_loadgp (XEXP (DECL_RTL (current_function_decl), 0),
7508 gen_rtx_REG (DImode, 25)));
7511 /* If we are profiling, make sure no instructions are scheduled before
7512 the call to mcount. */
7514 if (profile_flag || profile_block_flag)
7515 emit_insn (gen_blockage ());
7518 /* Do any necessary cleanup after a function to restore stack, frame,
7519 and regs. */
7521 #define RA_MASK BITMASK_HIGH /* 1 << 31 */
7522 #define PIC_OFFSET_TABLE_MASK (1 << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))
7524 static void
7525 mips_output_function_epilogue (file, size)
7526 FILE *file ATTRIBUTE_UNUSED;
7527 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
7529 const char *fnname = ""; /* FIXME: Correct initialisation? */
7531 #ifndef FUNCTION_NAME_ALREADY_DECLARED
7532 /* Get the function name the same way that toplev.c does before calling
7533 assemble_start_function. This is needed so that the name used here
7534 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
7535 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7537 if (!flag_inhibit_size_directive)
7539 fputs ("\t.end\t", file);
7540 assemble_name (file, fnname);
7541 fputs ("\n", file);
7543 #endif
7545 if (TARGET_STATS)
7547 int num_gp_regs = current_frame_info.gp_reg_size / 4;
7548 int num_fp_regs = current_frame_info.fp_reg_size / 8;
7549 int num_regs = num_gp_regs + num_fp_regs;
7550 const char *name = fnname;
7552 if (name[0] == '*')
7553 name++;
7555 dslots_load_total += num_regs;
7557 fprintf (stderr,
7558 "%-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",
7559 name, frame_pointer_needed ? 'y' : 'n',
7560 (current_frame_info.mask & RA_MASK) != 0 ? 'n' : 'y',
7561 current_function_calls_alloca ? 'y' : 'n',
7562 current_function_calls_setjmp ? 'y' : 'n',
7563 current_frame_info.total_size,
7564 current_function_outgoing_args_size, num_gp_regs, num_fp_regs,
7565 dslots_load_total, dslots_load_filled,
7566 dslots_jump_total, dslots_jump_filled,
7567 num_refs[0], num_refs[1], num_refs[2]);
7569 if (HALF_PIC_NUMBER_PTRS > prev_half_pic_ptrs)
7571 fprintf (stderr,
7572 " half-pic=%3d", HALF_PIC_NUMBER_PTRS - prev_half_pic_ptrs);
7573 prev_half_pic_ptrs = HALF_PIC_NUMBER_PTRS;
7576 if (HALF_PIC_NUMBER_REFS > prev_half_pic_refs)
7578 fprintf (stderr,
7579 " pic-ref=%3d", HALF_PIC_NUMBER_REFS - prev_half_pic_refs);
7580 prev_half_pic_refs = HALF_PIC_NUMBER_REFS;
7583 fputc ('\n', stderr);
7586 /* Reset state info for each function. */
7587 inside_function = 0;
7588 ignore_line_number = 0;
7589 dslots_load_total = 0;
7590 dslots_jump_total = 0;
7591 dslots_load_filled = 0;
7592 dslots_jump_filled = 0;
7593 num_refs[0] = 0;
7594 num_refs[1] = 0;
7595 num_refs[2] = 0;
7596 mips_load_reg = 0;
7597 mips_load_reg2 = 0;
7598 current_frame_info = zero_frame_info;
7600 while (string_constants != NULL)
7602 struct string_constant *next;
7604 next = string_constants->next;
7605 free (string_constants);
7606 string_constants = next;
7609 /* Restore the output file if optimizing the GP (optimizing the GP causes
7610 the text to be diverted to a tempfile, so that data decls come before
7611 references to the data). */
7612 if (TARGET_FILE_SWITCHING)
7614 asm_out_file = asm_out_data_file;
7615 data_section ();
7619 /* Expand the epilogue into a bunch of separate insns. */
7621 void
7622 mips_expand_epilogue ()
7624 HOST_WIDE_INT tsize = current_frame_info.total_size;
7625 rtx tsize_rtx = GEN_INT (tsize);
7626 rtx tmp_rtx = (rtx)0;
7628 if (mips_can_use_return_insn ())
7630 emit_insn (gen_return ());
7631 return;
7634 if (mips_entry && ! mips_can_use_return_insn ())
7635 tsize -= 32;
7637 if (tsize > 32767 && ! TARGET_MIPS16)
7639 tmp_rtx = gen_rtx_REG (Pmode, MIPS_TEMP1_REGNUM);
7640 emit_move_insn (tmp_rtx, tsize_rtx);
7641 tsize_rtx = tmp_rtx;
7644 if (tsize > 0)
7646 long orig_tsize = tsize;
7648 if (frame_pointer_needed)
7650 emit_insn (gen_blockage ());
7652 /* On the mips16, the frame pointer is offset from the stack
7653 pointer by current_function_outgoing_args_size. We
7654 account for that by changing tsize. Note that this can
7655 actually make tsize negative. */
7656 if (TARGET_MIPS16)
7658 tsize -= current_function_outgoing_args_size;
7660 /* If we have a large frame, it's easier to add to $17
7661 than to $sp, since the mips16 has no instruction to
7662 add a register to $sp. */
7663 if (orig_tsize > 32767)
7665 rtx g6_rtx = gen_rtx (REG, Pmode, GP_REG_FIRST + 6);
7667 emit_move_insn (g6_rtx, GEN_INT (tsize));
7668 if (Pmode == DImode)
7669 emit_insn (gen_adddi3 (hard_frame_pointer_rtx,
7670 hard_frame_pointer_rtx,
7671 g6_rtx));
7672 else
7673 emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7674 hard_frame_pointer_rtx,
7675 g6_rtx));
7676 tsize = 0;
7679 if (tsize && tsize != orig_tsize)
7680 tsize_rtx = GEN_INT (tsize);
7683 if (Pmode == DImode)
7684 emit_insn (gen_movdi (stack_pointer_rtx, hard_frame_pointer_rtx));
7685 else
7686 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
7689 /* The GP/PIC register is implicitly used by all SYMBOL_REFs, so if we
7690 are going to restore it, then we must emit a blockage insn to
7691 prevent the scheduler from moving the restore out of the epilogue. */
7692 else if (TARGET_ABICALLS && mips_abi != ABI_32 && mips_abi != ABI_O64
7693 && (current_frame_info.mask
7694 & (1L << (PIC_OFFSET_TABLE_REGNUM - GP_REG_FIRST))))
7695 emit_insn (gen_blockage ());
7697 save_restore_insns (0, tmp_rtx, orig_tsize, (FILE *)0);
7699 /* In mips16 mode with a large frame, we adjust the stack
7700 pointer before restoring the registers. In this case, we
7701 should always be using a frame pointer, so everything should
7702 have been handled above. */
7703 if (tsize > 32767 && TARGET_MIPS16)
7704 abort ();
7706 if (current_function_calls_eh_return)
7708 rtx eh_ofs = EH_RETURN_STACKADJ_RTX;
7709 if (Pmode == DImode)
7710 emit_insn (gen_adddi3 (eh_ofs, eh_ofs, tsize_rtx));
7711 else
7712 emit_insn (gen_addsi3 (eh_ofs, eh_ofs, tsize_rtx));
7713 tsize_rtx = eh_ofs;
7716 emit_insn (gen_blockage ());
7718 if (tsize != 0 || current_function_calls_eh_return)
7720 if (Pmode == DImode)
7721 emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx,
7722 tsize_rtx));
7723 else
7724 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
7725 tsize_rtx));
7729 /* The mips16 loads the return address into $7, not $31. */
7730 if (TARGET_MIPS16 && (current_frame_info.mask & RA_MASK) != 0)
7731 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
7732 GP_REG_FIRST + 7)));
7733 else
7734 emit_jump_insn (gen_return_internal (gen_rtx (REG, Pmode,
7735 GP_REG_FIRST + 31)));
7738 /* Return nonzero if this function is known to have a null epilogue.
7739 This allows the optimizer to omit jumps to jumps if no stack
7740 was created. */
7743 mips_can_use_return_insn ()
7745 if (! reload_completed)
7746 return 0;
7748 if (regs_ever_live[31] || profile_flag)
7749 return 0;
7751 /* In mips16 mode, a function which returns a floating point value
7752 needs to arrange to copy the return value into the floating point
7753 registers. */
7754 if (TARGET_MIPS16
7755 && mips16_hard_float
7756 && ! aggregate_value_p (DECL_RESULT (current_function_decl))
7757 && (GET_MODE_CLASS (DECL_MODE (DECL_RESULT (current_function_decl)))
7758 == MODE_FLOAT)
7759 && (! TARGET_SINGLE_FLOAT
7760 || (GET_MODE_SIZE (DECL_MODE (DECL_RESULT (current_function_decl)))
7761 <= 4)))
7762 return 0;
7764 if (current_frame_info.initialized)
7765 return current_frame_info.total_size == 0;
7767 return compute_frame_size (get_frame_size ()) == 0;
7770 /* Returns non-zero if X contains a SYMBOL_REF. */
7772 static int
7773 symbolic_expression_p (x)
7774 rtx x;
7776 if (GET_CODE (x) == SYMBOL_REF)
7777 return 1;
7779 if (GET_CODE (x) == CONST)
7780 return symbolic_expression_p (XEXP (x, 0));
7782 if (GET_RTX_CLASS (GET_CODE (x)) == '1')
7783 return symbolic_expression_p (XEXP (x, 0));
7785 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
7786 || GET_RTX_CLASS (GET_CODE (x)) == '2')
7787 return (symbolic_expression_p (XEXP (x, 0))
7788 || symbolic_expression_p (XEXP (x, 1)));
7790 return 0;
7793 /* Choose the section to use for the constant rtx expression X that has
7794 mode MODE. */
7796 void
7797 mips_select_rtx_section (mode, x)
7798 enum machine_mode mode;
7799 rtx x ATTRIBUTE_UNUSED;
7801 if (TARGET_MIPS16)
7803 /* In mips16 mode, the constant table always goes in the same section
7804 as the function, so that constants can be loaded using PC relative
7805 addressing. */
7806 function_section (current_function_decl);
7808 else if (TARGET_EMBEDDED_DATA)
7810 /* For embedded applications, always put constants in read-only data,
7811 in order to reduce RAM usage. */
7812 READONLY_DATA_SECTION ();
7814 else
7816 /* For hosted applications, always put constants in small data if
7817 possible, as this gives the best performance. */
7819 if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
7820 && mips_section_threshold > 0)
7821 SMALL_DATA_SECTION ();
7822 else if (flag_pic && symbolic_expression_p (x))
7823 /* Any expression involving a SYMBOL_REF might need a run-time
7824 relocation. (The symbol might be defined in a shared
7825 library loaded at an unexpected base address.) So, we must
7826 put such expressions in the data segment (which is
7827 writable), rather than the text segment (which is
7828 read-only). */
7829 data_section ();
7830 else
7831 READONLY_DATA_SECTION ();
7835 /* Choose the section to use for DECL. RELOC is true if its value contains
7836 any relocatable expression.
7838 Some of the logic used here needs to be replicated in
7839 ENCODE_SECTION_INFO in mips.h so that references to these symbols
7840 are done correctly. Specifically, at least all symbols assigned
7841 here to rom (.text and/or .rodata) must not be referenced via
7842 ENCODE_SECTION_INFO with %gprel, as the rom might be too far away.
7844 If you need to make a change here, you probably should check
7845 ENCODE_SECTION_INFO to see if it needs a similar change. */
7847 void
7848 mips_select_section (decl, reloc)
7849 tree decl;
7850 int reloc;
7852 int size = int_size_in_bytes (TREE_TYPE (decl));
7854 if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
7855 && TREE_CODE (decl) == STRING_CST
7856 && !flag_writable_strings)
7857 /* For embedded position independent code, put constant strings in the
7858 text section, because the data section is limited to 64K in size.
7859 For mips16 code, put strings in the text section so that a PC
7860 relative load instruction can be used to get their address. */
7861 text_section ();
7862 else if (TARGET_EMBEDDED_DATA)
7864 /* For embedded applications, always put an object in read-only data
7865 if possible, in order to reduce RAM usage. */
7867 if (((TREE_CODE (decl) == VAR_DECL
7868 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7869 && DECL_INITIAL (decl)
7870 && (DECL_INITIAL (decl) == error_mark_node
7871 || TREE_CONSTANT (DECL_INITIAL (decl))))
7872 /* Deal with calls from output_constant_def_contents. */
7873 || (TREE_CODE (decl) != VAR_DECL
7874 && (TREE_CODE (decl) != STRING_CST
7875 || !flag_writable_strings)))
7876 && ! (flag_pic && reloc))
7877 READONLY_DATA_SECTION ();
7878 else if (size > 0 && size <= mips_section_threshold)
7879 SMALL_DATA_SECTION ();
7880 else
7881 data_section ();
7883 else
7885 /* For hosted applications, always put an object in small data if
7886 possible, as this gives the best performance. */
7888 if (size > 0 && size <= mips_section_threshold)
7889 SMALL_DATA_SECTION ();
7890 else if (((TREE_CODE (decl) == VAR_DECL
7891 && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl)
7892 && DECL_INITIAL (decl)
7893 && (DECL_INITIAL (decl) == error_mark_node
7894 || TREE_CONSTANT (DECL_INITIAL (decl))))
7895 /* Deal with calls from output_constant_def_contents. */
7896 || (TREE_CODE (decl) != VAR_DECL
7897 && (TREE_CODE (decl) != STRING_CST
7898 || !flag_writable_strings)))
7899 && ! (flag_pic && reloc))
7900 READONLY_DATA_SECTION ();
7901 else
7902 data_section ();
7906 #ifdef MIPS_ABI_DEFAULT
7908 /* Support functions for the 64 bit ABI. */
7910 /* Return register to use for a function return value with VALTYPE for function
7911 FUNC. */
7914 mips_function_value (valtype, func)
7915 tree valtype;
7916 tree func ATTRIBUTE_UNUSED;
7918 int reg = GP_RETURN;
7919 enum machine_mode mode = TYPE_MODE (valtype);
7920 enum mode_class mclass = GET_MODE_CLASS (mode);
7921 int unsignedp = TREE_UNSIGNED (valtype);
7923 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
7924 just as PROMOTE_MODE does. */
7925 mode = promote_mode (valtype, mode, &unsignedp, 1);
7927 if (mclass == MODE_FLOAT)
7929 if (TARGET_SINGLE_FLOAT
7930 && (mclass == MODE_FLOAT
7931 ? GET_MODE_SIZE (mode) > 4 : GET_MODE_SIZE (mode) / 2 > 4))
7932 reg = GP_RETURN;
7933 else
7934 reg = FP_RETURN;
7937 else if (mclass == MODE_COMPLEX_FLOAT)
7939 if (TARGET_FLOAT64)
7940 reg = FP_RETURN;
7941 else if (mode == SCmode)
7943 /* When FP registers are 32 bits, we can't directly reference
7944 the odd numbered ones, so let's make a pair of evens. */
7946 enum machine_mode cmode = TYPE_MODE (TREE_TYPE (valtype));
7948 return gen_rtx_PARALLEL
7949 (VOIDmode,
7950 gen_rtvec (2,
7951 gen_rtx_EXPR_LIST (VOIDmode,
7952 gen_rtx_REG (cmode,
7953 FP_RETURN),
7954 GEN_INT (0)),
7955 gen_rtx_EXPR_LIST (VOIDmode,
7956 gen_rtx_REG (cmode,
7957 FP_RETURN + 2),
7958 GEN_INT (4))));
7960 else
7961 reg = FP_RETURN;
7964 else if (TREE_CODE (valtype) == RECORD_TYPE
7965 && mips_abi != ABI_32
7966 && mips_abi != ABI_O64
7967 && mips_abi != ABI_EABI)
7969 /* A struct with only one or two floating point fields is returned in
7970 the floating point registers. */
7971 tree field, fields[2];
7972 int i;
7974 for (i = 0, field = TYPE_FIELDS (valtype); field;
7975 field = TREE_CHAIN (field))
7977 if (TREE_CODE (field) != FIELD_DECL)
7978 continue;
7980 if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE || i >= 2)
7981 break;
7983 fields[i++] = field;
7986 /* Must check i, so that we reject structures with no elements. */
7987 if (! field)
7989 if (i == 1)
7991 /* The structure has DImode, but we don't allow DImode values
7992 in FP registers, so we use a PARALLEL even though it isn't
7993 strictly necessary. */
7994 enum machine_mode field_mode = TYPE_MODE (TREE_TYPE (fields[0]));
7996 return gen_rtx_PARALLEL
7997 (mode,
7998 gen_rtvec (1,
7999 gen_rtx_EXPR_LIST (VOIDmode,
8000 gen_rtx_REG (field_mode,
8001 FP_RETURN),
8002 const0_rtx)));
8005 else if (i == 2)
8007 enum machine_mode first_mode
8008 = TYPE_MODE (TREE_TYPE (fields[0]));
8009 enum machine_mode second_mode
8010 = TYPE_MODE (TREE_TYPE (fields[1]));
8011 HOST_WIDE_INT first_offset = int_byte_position (fields[0]);
8012 HOST_WIDE_INT second_offset = int_byte_position (fields[1]);
8014 return gen_rtx_PARALLEL
8015 (mode,
8016 gen_rtvec (2,
8017 gen_rtx_EXPR_LIST (VOIDmode,
8018 gen_rtx_REG (first_mode,
8019 FP_RETURN),
8020 GEN_INT (first_offset)),
8021 gen_rtx_EXPR_LIST (VOIDmode,
8022 gen_rtx_REG (second_mode,
8023 FP_RETURN + 2),
8024 GEN_INT (second_offset))));
8029 return gen_rtx_REG (mode, reg);
8031 #endif
8033 /* The implementation of FUNCTION_ARG_PASS_BY_REFERENCE. Return
8034 nonzero when an argument must be passed by reference. */
8037 function_arg_pass_by_reference (cum, mode, type, named)
8038 CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
8039 enum machine_mode mode;
8040 tree type;
8041 int named ATTRIBUTE_UNUSED;
8043 int size;
8045 /* We must pass by reference if we would be both passing in registers
8046 and the stack. This is because any subsequent partial arg would be
8047 handled incorrectly in this case.
8049 ??? This is really a kludge. We should either fix GCC so that such
8050 a situation causes an abort and then do something in the MIPS port
8051 to prevent it, or add code to function.c to properly handle the case. */
8052 /* ??? cum can be NULL when called from mips_va_arg. The problem handled
8053 here hopefully is not relevant to mips_va_arg. */
8054 if (cum && MUST_PASS_IN_STACK (mode, type)
8055 && mips_abi != ABI_MEABI)
8057 /* Don't pass the actual CUM to FUNCTION_ARG, because we would
8058 get double copies of any offsets generated for small structs
8059 passed in registers. */
8060 CUMULATIVE_ARGS temp;
8061 temp = *cum;
8062 if (FUNCTION_ARG (temp, mode, type, named) != 0)
8063 return 1;
8066 /* Otherwise, we only do this if EABI is selected. */
8067 if (mips_abi != ABI_EABI)
8068 return 0;
8070 /* ??? How should SCmode be handled? */
8071 if (type == NULL_TREE || mode == DImode || mode == DFmode)
8072 return 0;
8074 size = int_size_in_bytes (type);
8075 return size == -1 || size > UNITS_PER_WORD;
8078 /* This function returns the register class required for a secondary
8079 register when copying between one of the registers in CLASS, and X,
8080 using MODE. If IN_P is nonzero, the copy is going from X to the
8081 register, otherwise the register is the source. A return value of
8082 NO_REGS means that no secondary register is required. */
8084 enum reg_class
8085 mips_secondary_reload_class (class, mode, x, in_p)
8086 enum reg_class class;
8087 enum machine_mode mode;
8088 rtx x;
8089 int in_p;
8091 enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
8092 int regno = -1;
8093 int gp_reg_p;
8095 if (GET_CODE (x) == SIGN_EXTEND)
8097 int off = 0;
8099 x = XEXP (x, 0);
8101 /* We may be called with reg_renumber NULL from regclass.
8102 ??? This is probably a bug. */
8103 if (reg_renumber)
8104 regno = true_regnum (x);
8105 else
8107 while (GET_CODE (x) == SUBREG)
8109 off += subreg_regno_offset (REGNO (SUBREG_REG (x)),
8110 GET_MODE (SUBREG_REG (x)),
8111 SUBREG_BYTE (x),
8112 GET_MODE (x));
8113 x = SUBREG_REG (x);
8116 if (GET_CODE (x) == REG)
8117 regno = REGNO (x) + off;
8121 else if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
8122 regno = true_regnum (x);
8124 gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
8126 /* We always require a general register when copying anything to
8127 HILO_REGNUM, except when copying an SImode value from HILO_REGNUM
8128 to a general register, or when copying from register 0. */
8129 if (class == HILO_REG && regno != GP_REG_FIRST + 0)
8130 return ((! in_p
8131 && gp_reg_p
8132 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8133 ? NO_REGS : gr_regs);
8134 else if (regno == HILO_REGNUM)
8135 return ((in_p
8136 && class == gr_regs
8137 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode))
8138 ? NO_REGS : gr_regs);
8140 /* Copying from HI or LO to anywhere other than a general register
8141 requires a general register. */
8142 if (class == HI_REG || class == LO_REG || class == MD_REGS)
8144 if (TARGET_MIPS16 && in_p)
8146 /* We can't really copy to HI or LO at all in mips16 mode. */
8147 return M16_REGS;
8149 return gp_reg_p ? NO_REGS : gr_regs;
8151 if (MD_REG_P (regno))
8153 if (TARGET_MIPS16 && ! in_p)
8155 /* We can't really copy to HI or LO at all in mips16 mode. */
8156 return M16_REGS;
8158 return class == gr_regs ? NO_REGS : gr_regs;
8161 /* We can only copy a value to a condition code register from a
8162 floating point register, and even then we require a scratch
8163 floating point register. We can only copy a value out of a
8164 condition code register into a general register. */
8165 if (class == ST_REGS)
8167 if (in_p)
8168 return FP_REGS;
8169 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
8171 if (ST_REG_P (regno))
8173 if (! in_p)
8174 return FP_REGS;
8175 return class == GR_REGS ? NO_REGS : GR_REGS;
8178 /* In mips16 mode, going between memory and anything but M16_REGS
8179 requires an M16_REG. */
8180 if (TARGET_MIPS16)
8182 if (class != M16_REGS && class != M16_NA_REGS)
8184 if (gp_reg_p)
8185 return NO_REGS;
8186 return M16_REGS;
8188 if (! gp_reg_p)
8190 /* The stack pointer isn't a valid operand to an add instruction,
8191 so we need to load it into M16_REGS first. This can happen as
8192 a result of register elimination and form_sum converting
8193 (plus reg (plus SP CONST)) to (plus (plus reg SP) CONST). We
8194 need an extra register if the dest is the same as the other
8195 register. In that case, we can't fix the problem by loading SP
8196 into the dest first. */
8197 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
8198 && GET_CODE (XEXP (x, 1)) == REG
8199 && (XEXP (x, 0) == stack_pointer_rtx
8200 || XEXP (x, 1) == stack_pointer_rtx))
8201 return (class == M16_REGS ? M16_NA_REGS : M16_REGS);
8203 if (class == M16_REGS || class == M16_NA_REGS)
8204 return NO_REGS;
8205 return M16_REGS;
8209 return NO_REGS;
8212 /* For each mips16 function which refers to GP relative symbols, we
8213 use a pseudo register, initialized at the start of the function, to
8214 hold the $gp value. */
8217 mips16_gp_pseudo_reg ()
8219 if (mips16_gp_pseudo_rtx == NULL_RTX)
8221 rtx const_gp;
8222 rtx insn, scan;
8224 mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
8225 RTX_UNCHANGING_P (mips16_gp_pseudo_rtx) = 1;
8227 /* We want to initialize this to a value which gcc will believe
8228 is constant. */
8229 const_gp = gen_rtx (CONST, Pmode,
8230 gen_rtx (REG, Pmode, GP_REG_FIRST + 28));
8232 start_sequence ();
8233 emit_move_insn (mips16_gp_pseudo_rtx, const_gp);
8234 insn = gen_sequence ();
8235 end_sequence ();
8237 push_topmost_sequence ();
8238 /* We need to emit the initialization after the FUNCTION_BEG
8239 note, so that it will be integrated. */
8240 for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
8241 if (GET_CODE (scan) == NOTE
8242 && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
8243 break;
8244 if (scan == NULL_RTX)
8245 scan = get_insns ();
8246 insn = emit_insn_after (insn, scan);
8247 pop_topmost_sequence ();
8250 return mips16_gp_pseudo_rtx;
8253 /* Return an RTX which represents the signed 16 bit offset from the
8254 $gp register for the given symbol. This is only used on the
8255 mips16. */
8258 mips16_gp_offset (sym)
8259 rtx sym;
8261 tree gp;
8263 if (GET_CODE (sym) != SYMBOL_REF
8264 || ! SYMBOL_REF_FLAG (sym))
8265 abort ();
8267 /* We use a special identifier to represent the value of the gp
8268 register. */
8269 gp = get_identifier ("__mips16_gp_value");
8271 return gen_rtx (CONST, Pmode,
8272 gen_rtx (MINUS, Pmode, sym,
8273 gen_rtx (SYMBOL_REF, Pmode,
8274 IDENTIFIER_POINTER (gp))));
8277 /* Return nonzero if the given RTX represents a signed 16 bit offset
8278 from the $gp register. */
8281 mips16_gp_offset_p (x)
8282 rtx x;
8284 if (GET_CODE (x) == CONST)
8285 x = XEXP (x, 0);
8287 /* It's OK to add a small integer value to a gp offset. */
8288 if (GET_CODE (x) == PLUS)
8290 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8291 && SMALL_INT (XEXP (x, 1)))
8292 return mips16_gp_offset_p (XEXP (x, 0));
8293 if (GET_CODE (XEXP (x, 0)) == CONST_INT
8294 && SMALL_INT (XEXP (x, 0)))
8295 return mips16_gp_offset_p (XEXP (x, 1));
8296 return 0;
8299 /* Make sure it is in the form SYM - __mips16_gp_value. */
8300 return (GET_CODE (x) == MINUS
8301 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
8302 && SYMBOL_REF_FLAG (XEXP (x, 0))
8303 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
8304 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0);
8307 /* Output a GP offset. We don't want to print the subtraction of
8308 __mips16_gp_value; it is implicitly represented by the %gprel which
8309 should have been printed by the caller. */
8311 static void
8312 mips16_output_gp_offset (file, x)
8313 FILE *file;
8314 rtx x;
8316 if (GET_CODE (x) == CONST)
8317 x = XEXP (x, 0);
8319 if (GET_CODE (x) == PLUS)
8321 mips16_output_gp_offset (file, XEXP (x, 0));
8322 fputs ("+", file);
8323 mips16_output_gp_offset (file, XEXP (x, 1));
8324 return;
8327 if (GET_CODE (x) == MINUS
8328 && GET_CODE (XEXP (x, 1)) == SYMBOL_REF
8329 && strcmp (XSTR (XEXP (x, 1), 0), "__mips16_gp_value") == 0)
8331 mips16_output_gp_offset (file, XEXP (x, 0));
8332 return;
8335 output_addr_const (file, x);
8338 /* Return nonzero if a constant should not be output until after the
8339 function. This is true of most string constants, so that we can
8340 use a more efficient PC relative reference. However, a static
8341 inline function may never call assemble_function_end to write out
8342 the constant pool, so don't try to postpone the constant in that
8343 case.
8345 ??? It's really a bug that a static inline function can put stuff
8346 in the constant pool even if the function itself is not output.
8348 We record which string constants we've seen, so that we know which
8349 ones might use the more efficient reference. */
8352 mips16_constant_after_function_p (x)
8353 tree x;
8355 if (TREE_CODE (x) == STRING_CST
8356 && ! flag_writable_strings
8357 && current_function_decl != 0
8358 && ! DECL_DEFER_OUTPUT (current_function_decl)
8359 && ! (DECL_INLINE (current_function_decl)
8360 && ((! TREE_PUBLIC (current_function_decl)
8361 && ! TREE_ADDRESSABLE (current_function_decl)
8362 && ! flag_keep_inline_functions)
8363 || DECL_EXTERNAL (current_function_decl))))
8365 struct string_constant *n;
8367 n = (struct string_constant *) xmalloc (sizeof *n);
8368 n->label = XSTR (XEXP (TREE_CST_RTL (x), 0), 0);
8369 n->next = string_constants;
8370 string_constants = n;
8372 return 1;
8375 return 0;
8378 /* Validate a constant for the mips16. This rejects general symbolic
8379 addresses, which must be loaded from memory. If ADDR is nonzero,
8380 this should reject anything which is not a legal address. If
8381 ADDEND is nonzero, this is being added to something else. */
8384 mips16_constant (x, mode, addr, addend)
8385 rtx x;
8386 enum machine_mode mode;
8387 int addr;
8388 int addend;
8390 while (GET_CODE (x) == CONST)
8391 x = XEXP (x, 0);
8393 switch (GET_CODE (x))
8395 default:
8396 return 0;
8398 case PLUS:
8399 return (mips16_constant (XEXP (x, 0), mode, addr, 1)
8400 && mips16_constant (XEXP (x, 1), mode, addr, 1));
8402 case SYMBOL_REF:
8403 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
8404 return 0;
8405 if (CONSTANT_POOL_ADDRESS_P (x))
8406 return 1;
8408 /* If we aren't looking for a memory address, we can accept a GP
8409 relative symbol, which will have SYMBOL_REF_FLAG set; movsi
8410 knows how to handle this. We can always accept a string
8411 constant, which is the other case in which SYMBOL_REF_FLAG
8412 will be set. */
8413 if (! addr
8414 && ! addend
8415 && SYMBOL_REF_FLAG (x)
8416 && mode == (enum machine_mode) Pmode)
8417 return 1;
8419 /* We can accept a string constant, which will have
8420 SYMBOL_REF_FLAG set but must be recognized by name to
8421 distinguish from a GP accessible symbol. The name of a
8422 string constant will have been generated by
8423 ASM_GENERATE_INTERNAL_LABEL as called by output_constant_def. */
8424 if (SYMBOL_REF_FLAG (x))
8426 const char *name = XSTR (x, 0);
8428 return (name[0] == '*'
8429 && strncmp (name + 1, LOCAL_LABEL_PREFIX,
8430 sizeof LOCAL_LABEL_PREFIX - 1) == 0);
8433 return 0;
8435 case LABEL_REF:
8436 if (addr && GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)
8437 return 0;
8438 return 1;
8440 case CONST_INT:
8441 if (addr && ! addend)
8442 return 0;
8443 return INTVAL (x) > - 0x10000 && INTVAL (x) <= 0xffff;
8445 case REG:
8446 /* We need to treat $gp as a legitimate constant, because
8447 mips16_gp_pseudo_reg assumes that. */
8448 return REGNO (x) == GP_REG_FIRST + 28;
8452 /* Write out code to move floating point arguments in or out of
8453 general registers. Output the instructions to FILE. FP_CODE is
8454 the code describing which arguments are present (see the comment at
8455 the definition of CUMULATIVE_ARGS in mips.h). FROM_FP_P is non-zero if
8456 we are copying from the floating point registers. */
8458 static void
8459 mips16_fp_args (file, fp_code, from_fp_p)
8460 FILE *file;
8461 int fp_code;
8462 int from_fp_p;
8464 const char *s;
8465 int gparg, fparg;
8466 unsigned int f;
8468 /* This code only works for the original 32 bit ABI and the O64 ABI. */
8469 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
8470 abort ();
8472 if (from_fp_p)
8473 s = "mfc1";
8474 else
8475 s = "mtc1";
8476 gparg = GP_ARG_FIRST;
8477 fparg = FP_ARG_FIRST;
8478 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8480 if ((f & 3) == 1)
8482 if ((fparg & 1) != 0)
8483 ++fparg;
8484 fprintf (file, "\t%s\t%s,%s\n", s,
8485 reg_names[gparg], reg_names[fparg]);
8487 else if ((f & 3) == 2)
8489 if (TARGET_64BIT)
8490 fprintf (file, "\td%s\t%s,%s\n", s,
8491 reg_names[gparg], reg_names[fparg]);
8492 else
8494 if ((fparg & 1) != 0)
8495 ++fparg;
8496 if (TARGET_BIG_ENDIAN)
8497 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
8498 reg_names[gparg], reg_names[fparg + 1], s,
8499 reg_names[gparg + 1], reg_names[fparg]);
8500 else
8501 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
8502 reg_names[gparg], reg_names[fparg], s,
8503 reg_names[gparg + 1], reg_names[fparg + 1]);
8504 ++gparg;
8505 ++fparg;
8508 else
8509 abort ();
8511 ++gparg;
8512 ++fparg;
8516 /* Build a mips16 function stub. This is used for functions which
8517 take aruments in the floating point registers. It is 32 bit code
8518 that moves the floating point args into the general registers, and
8519 then jumps to the 16 bit code. */
8521 static void
8522 build_mips16_function_stub (file)
8523 FILE *file;
8525 const char *fnname;
8526 char *secname, *stubname;
8527 tree stubid, stubdecl;
8528 int need_comma;
8529 unsigned int f;
8531 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
8532 secname = (char *) alloca (strlen (fnname) + 20);
8533 sprintf (secname, ".mips16.fn.%s", fnname);
8534 stubname = (char *) alloca (strlen (fnname) + 20);
8535 sprintf (stubname, "__fn_stub_%s", fnname);
8536 stubid = get_identifier (stubname);
8537 stubdecl = build_decl (FUNCTION_DECL, stubid,
8538 build_function_type (void_type_node, NULL_TREE));
8539 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8541 fprintf (file, "\t# Stub function for %s (", current_function_name);
8542 need_comma = 0;
8543 for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
8545 fprintf (file, "%s%s",
8546 need_comma ? ", " : "",
8547 (f & 3) == 1 ? "float" : "double");
8548 need_comma = 1;
8550 fprintf (file, ")\n");
8552 fprintf (file, "\t.set\tnomips16\n");
8553 function_section (stubdecl);
8554 ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
8556 /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
8557 within a .ent, and we can not emit another .ent. */
8558 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8559 fputs ("\t.ent\t", file);
8560 assemble_name (file, stubname);
8561 fputs ("\n", file);
8562 #endif
8564 assemble_name (file, stubname);
8565 fputs (":\n", file);
8567 /* We don't want the assembler to insert any nops here. */
8568 fprintf (file, "\t.set\tnoreorder\n");
8570 mips16_fp_args (file, current_function_args_info.fp_code, 1);
8572 fprintf (asm_out_file, "\t.set\tnoat\n");
8573 fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
8574 assemble_name (file, fnname);
8575 fprintf (file, "\n");
8576 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8577 fprintf (asm_out_file, "\t.set\tat\n");
8579 /* Unfortunately, we can't fill the jump delay slot. We can't fill
8580 with one of the mfc1 instructions, because the result is not
8581 available for one instruction, so if the very first instruction
8582 in the function refers to the register, it will see the wrong
8583 value. */
8584 fprintf (file, "\tnop\n");
8586 fprintf (file, "\t.set\treorder\n");
8588 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8589 fputs ("\t.end\t", file);
8590 assemble_name (file, stubname);
8591 fputs ("\n", file);
8592 #endif
8594 fprintf (file, "\t.set\tmips16\n");
8596 function_section (current_function_decl);
8599 /* We keep a list of functions for which we have already built stubs
8600 in build_mips16_call_stub. */
8602 struct mips16_stub
8604 struct mips16_stub *next;
8605 char *name;
8606 int fpret;
8609 static struct mips16_stub *mips16_stubs;
8611 /* Build a call stub for a mips16 call. A stub is needed if we are
8612 passing any floating point values which should go into the floating
8613 point registers. If we are, and the call turns out to be to a 32
8614 bit function, the stub will be used to move the values into the
8615 floating point registers before calling the 32 bit function. The
8616 linker will magically adjust the function call to either the 16 bit
8617 function or the 32 bit stub, depending upon where the function call
8618 is actually defined.
8620 Similarly, we need a stub if the return value might come back in a
8621 floating point register.
8623 RETVAL, FNMEM, and ARG_SIZE are the values passed to the call insn
8624 (RETVAL is NULL if this is call rather than call_value). FP_CODE
8625 is the code built by function_arg. This function returns a nonzero
8626 value if it builds the call instruction itself. */
8629 build_mips16_call_stub (retval, fnmem, arg_size, fp_code)
8630 rtx retval;
8631 rtx fnmem;
8632 rtx arg_size;
8633 int fp_code;
8635 int fpret;
8636 rtx fn;
8637 const char *fnname;
8638 char *secname, *stubname;
8639 struct mips16_stub *l;
8640 tree stubid, stubdecl;
8641 int need_comma;
8642 unsigned int f;
8644 /* We don't need to do anything if we aren't in mips16 mode, or if
8645 we were invoked with the -msoft-float option. */
8646 if (! TARGET_MIPS16 || ! mips16_hard_float)
8647 return 0;
8649 /* Figure out whether the value might come back in a floating point
8650 register. */
8651 fpret = (retval != 0
8652 && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
8653 && (! TARGET_SINGLE_FLOAT
8654 || GET_MODE_SIZE (GET_MODE (retval)) <= 4));
8656 /* We don't need to do anything if there were no floating point
8657 arguments and the value will not be returned in a floating point
8658 register. */
8659 if (fp_code == 0 && ! fpret)
8660 return 0;
8662 if (GET_CODE (fnmem) != MEM)
8663 abort ();
8664 fn = XEXP (fnmem, 0);
8666 /* We don't need to do anything if this is a call to a special
8667 mips16 support function. */
8668 if (GET_CODE (fn) == SYMBOL_REF
8669 && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
8670 return 0;
8672 /* This code will only work for o32 and o64 abis. The other ABI's
8673 require more sophisticated support. */
8674 if (mips_abi != ABI_32 && mips_abi != ABI_O64)
8675 abort ();
8677 /* We can only handle SFmode and DFmode floating point return
8678 values. */
8679 if (fpret && GET_MODE (retval) != SFmode && GET_MODE (retval) != DFmode)
8680 abort ();
8682 /* If we're calling via a function pointer, then we must always call
8683 via a stub. There are magic stubs provided in libgcc.a for each
8684 of the required cases. Each of them expects the function address
8685 to arrive in register $2. */
8687 if (GET_CODE (fn) != SYMBOL_REF)
8689 char buf[30];
8690 tree id;
8691 rtx stub_fn, stub_mem, insn;
8693 /* ??? If this code is modified to support other ABI's, we need
8694 to handle PARALLEL return values here. */
8696 sprintf (buf, "__mips16_call_stub_%s%d",
8697 (fpret
8698 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
8699 : ""),
8700 fp_code);
8701 id = get_identifier (buf);
8702 stub_fn = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (id));
8703 stub_mem = gen_rtx (MEM, Pmode, stub_fn);
8705 emit_move_insn (gen_rtx (REG, Pmode, 2), fn);
8707 if (retval == NULL_RTX)
8708 insn = gen_call_internal0 (stub_mem, arg_size,
8709 gen_rtx (REG, SImode,
8710 GP_REG_FIRST + 31));
8711 else
8712 insn = gen_call_value_internal0 (retval, stub_mem, arg_size,
8713 gen_rtx (REG, SImode,
8714 GP_REG_FIRST + 31));
8715 insn = emit_call_insn (insn);
8717 /* Put the register usage information on the CALL. */
8718 if (GET_CODE (insn) != CALL_INSN)
8719 abort ();
8720 CALL_INSN_FUNCTION_USAGE (insn) =
8721 gen_rtx (EXPR_LIST, VOIDmode,
8722 gen_rtx (USE, VOIDmode, gen_rtx (REG, Pmode, 2)),
8723 CALL_INSN_FUNCTION_USAGE (insn));
8725 /* If we are handling a floating point return value, we need to
8726 save $18 in the function prologue. Putting a note on the
8727 call will mean that regs_ever_live[$18] will be true if the
8728 call is not eliminated, and we can check that in the prologue
8729 code. */
8730 if (fpret)
8731 CALL_INSN_FUNCTION_USAGE (insn) =
8732 gen_rtx (EXPR_LIST, VOIDmode,
8733 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
8734 CALL_INSN_FUNCTION_USAGE (insn));
8736 /* Return 1 to tell the caller that we've generated the call
8737 insn. */
8738 return 1;
8741 /* We know the function we are going to call. If we have already
8742 built a stub, we don't need to do anything further. */
8744 fnname = XSTR (fn, 0);
8745 for (l = mips16_stubs; l != NULL; l = l->next)
8746 if (strcmp (l->name, fnname) == 0)
8747 break;
8749 if (l == NULL)
8751 /* Build a special purpose stub. When the linker sees a
8752 function call in mips16 code, it will check where the target
8753 is defined. If the target is a 32 bit call, the linker will
8754 search for the section defined here. It can tell which
8755 symbol this section is associated with by looking at the
8756 relocation information (the name is unreliable, since this
8757 might be a static function). If such a section is found, the
8758 linker will redirect the call to the start of the magic
8759 section.
8761 If the function does not return a floating point value, the
8762 special stub section is named
8763 .mips16.call.FNNAME
8765 If the function does return a floating point value, the stub
8766 section is named
8767 .mips16.call.fp.FNNAME
8770 secname = (char *) alloca (strlen (fnname) + 40);
8771 sprintf (secname, ".mips16.call.%s%s",
8772 fpret ? "fp." : "",
8773 fnname);
8774 stubname = (char *) alloca (strlen (fnname) + 20);
8775 sprintf (stubname, "__call_stub_%s%s",
8776 fpret ? "fp_" : "",
8777 fnname);
8778 stubid = get_identifier (stubname);
8779 stubdecl = build_decl (FUNCTION_DECL, stubid,
8780 build_function_type (void_type_node, NULL_TREE));
8781 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8783 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8784 (fpret
8785 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8786 : ""),
8787 fnname);
8788 need_comma = 0;
8789 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8791 fprintf (asm_out_file, "%s%s",
8792 need_comma ? ", " : "",
8793 (f & 3) == 1 ? "float" : "double");
8794 need_comma = 1;
8796 fprintf (asm_out_file, ")\n");
8798 fprintf (asm_out_file, "\t.set\tnomips16\n");
8799 assemble_start_function (stubdecl, stubname);
8801 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8802 fputs ("\t.ent\t", asm_out_file);
8803 assemble_name (asm_out_file, stubname);
8804 fputs ("\n", asm_out_file);
8806 assemble_name (asm_out_file, stubname);
8807 fputs (":\n", asm_out_file);
8808 #endif
8810 /* We build the stub code by hand. That's the only way we can
8811 do it, since we can't generate 32 bit code during a 16 bit
8812 compilation. */
8814 /* We don't want the assembler to insert any nops here. */
8815 fprintf (asm_out_file, "\t.set\tnoreorder\n");
8817 mips16_fp_args (asm_out_file, fp_code, 0);
8819 if (! fpret)
8821 fprintf (asm_out_file, "\t.set\tnoat\n");
8822 fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
8823 fnname);
8824 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8825 fprintf (asm_out_file, "\t.set\tat\n");
8826 /* Unfortunately, we can't fill the jump delay slot. We
8827 can't fill with one of the mtc1 instructions, because the
8828 result is not available for one instruction, so if the
8829 very first instruction in the function refers to the
8830 register, it will see the wrong value. */
8831 fprintf (asm_out_file, "\tnop\n");
8833 else
8835 fprintf (asm_out_file, "\tmove\t%s,%s\n",
8836 reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
8837 fprintf (asm_out_file, "\tjal\t%s\n", fnname);
8838 /* As above, we can't fill the delay slot. */
8839 fprintf (asm_out_file, "\tnop\n");
8840 if (GET_MODE (retval) == SFmode)
8841 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8842 reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
8843 else
8845 if (TARGET_BIG_ENDIAN)
8847 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8848 reg_names[GP_REG_FIRST + 2],
8849 reg_names[FP_REG_FIRST + 1]);
8850 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8851 reg_names[GP_REG_FIRST + 3],
8852 reg_names[FP_REG_FIRST + 0]);
8854 else
8856 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8857 reg_names[GP_REG_FIRST + 2],
8858 reg_names[FP_REG_FIRST + 0]);
8859 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8860 reg_names[GP_REG_FIRST + 3],
8861 reg_names[FP_REG_FIRST + 1]);
8864 fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
8865 /* As above, we can't fill the delay slot. */
8866 fprintf (asm_out_file, "\tnop\n");
8869 fprintf (asm_out_file, "\t.set\treorder\n");
8871 #ifdef ASM_DECLARE_FUNCTION_SIZE
8872 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8873 #endif
8875 #ifndef FUNCTION_NAME_ALREADY_DECLARED
8876 fputs ("\t.end\t", asm_out_file);
8877 assemble_name (asm_out_file, stubname);
8878 fputs ("\n", asm_out_file);
8879 #endif
8881 fprintf (asm_out_file, "\t.set\tmips16\n");
8883 /* Record this stub. */
8884 l = (struct mips16_stub *) xmalloc (sizeof *l);
8885 l->name = xstrdup (fnname);
8886 l->fpret = fpret;
8887 l->next = mips16_stubs;
8888 mips16_stubs = l;
8891 /* If we expect a floating point return value, but we've built a
8892 stub which does not expect one, then we're in trouble. We can't
8893 use the existing stub, because it won't handle the floating point
8894 value. We can't build a new stub, because the linker won't know
8895 which stub to use for the various calls in this object file.
8896 Fortunately, this case is illegal, since it means that a function
8897 was declared in two different ways in a single compilation. */
8898 if (fpret && ! l->fpret)
8899 error ("can not handle inconsistent calls to `%s'", fnname);
8901 /* If we are calling a stub which handles a floating point return
8902 value, we need to arrange to save $18 in the prologue. We do
8903 this by marking the function call as using the register. The
8904 prologue will later see that it is used, and emit code to save
8905 it. */
8907 if (l->fpret)
8909 rtx insn;
8911 if (retval == NULL_RTX)
8912 insn = gen_call_internal0 (fnmem, arg_size,
8913 gen_rtx (REG, SImode,
8914 GP_REG_FIRST + 31));
8915 else
8916 insn = gen_call_value_internal0 (retval, fnmem, arg_size,
8917 gen_rtx (REG, SImode,
8918 GP_REG_FIRST + 31));
8919 insn = emit_call_insn (insn);
8921 if (GET_CODE (insn) != CALL_INSN)
8922 abort ();
8924 CALL_INSN_FUNCTION_USAGE (insn) =
8925 gen_rtx (EXPR_LIST, VOIDmode,
8926 gen_rtx (USE, VOIDmode, gen_rtx (REG, word_mode, 18)),
8927 CALL_INSN_FUNCTION_USAGE (insn));
8929 /* Return 1 to tell the caller that we've generated the call
8930 insn. */
8931 return 1;
8934 /* Return 0 to let the caller generate the call insn. */
8935 return 0;
8938 /* This function looks through the code for a function, and tries to
8939 optimize the usage of the $gp register. We arrange to copy $gp
8940 into a pseudo-register, and then let gcc's normal reload handling
8941 deal with the pseudo-register. Unfortunately, if reload choose to
8942 put the pseudo-register into a call-clobbered register, it will
8943 emit saves and restores for that register around any function
8944 calls. We don't need the saves, and it's faster to copy $gp than
8945 to do an actual restore. ??? This still means that we waste a
8946 stack slot.
8948 This is an optimization, and the code which gcc has actually
8949 generated is correct, so we do not need to catch all cases. */
8951 static void
8952 mips16_optimize_gp (first)
8953 rtx first;
8955 rtx gpcopy, slot, insn;
8957 /* Look through the instructions. Set GPCOPY to the register which
8958 holds a copy of $gp. Set SLOT to the stack slot where it is
8959 saved. If we find an instruction which sets GPCOPY to anything
8960 other than $gp or SLOT, then we can't use it. If we find an
8961 instruction which sets SLOT to anything other than GPCOPY, we
8962 can't use it. */
8964 gpcopy = NULL_RTX;
8965 slot = NULL_RTX;
8966 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
8968 rtx set;
8970 if (! INSN_P (insn))
8971 continue;
8973 set = PATTERN (insn);
8975 /* We know that all references to memory will be inside a SET,
8976 because there is no other way to access memory on the mips16.
8977 We don't have to worry about a PARALLEL here, because the
8978 mips.md file will never generate them for memory references. */
8979 if (GET_CODE (set) != SET)
8980 continue;
8982 if (gpcopy == NULL_RTX
8983 && GET_CODE (SET_SRC (set)) == CONST
8984 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
8985 && REGNO (XEXP (SET_SRC (set), 0)) == GP_REG_FIRST + 28
8986 && GET_CODE (SET_DEST (set)) == REG
8987 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
8988 gpcopy = SET_DEST (set);
8989 else if (slot == NULL_RTX
8990 && gpcopy != NULL_RTX
8991 && GET_CODE (SET_DEST (set)) == MEM
8992 && GET_CODE (SET_SRC (set)) == REG
8993 && REGNO (SET_SRC (set)) == REGNO (gpcopy)
8994 && GET_MODE (SET_DEST (set)) == (unsigned) Pmode)
8996 rtx base, offset;
8998 offset = const0_rtx;
8999 base = eliminate_constant_term (XEXP (SET_DEST (set), 0), &offset);
9000 if (GET_CODE (base) == REG
9001 && (REGNO (base) == STACK_POINTER_REGNUM
9002 || REGNO (base) == FRAME_POINTER_REGNUM))
9003 slot = SET_DEST (set);
9005 else if (gpcopy != NULL_RTX
9006 && (GET_CODE (SET_DEST (set)) == REG
9007 || GET_CODE (SET_DEST (set)) == SUBREG)
9008 && reg_overlap_mentioned_p (SET_DEST (set), gpcopy)
9009 && (GET_CODE (SET_DEST (set)) != REG
9010 || REGNO (SET_DEST (set)) != REGNO (gpcopy)
9011 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode
9012 || ((GET_CODE (SET_SRC (set)) != CONST
9013 || GET_CODE (XEXP (SET_SRC (set), 0)) != REG
9014 || (REGNO (XEXP (SET_SRC (set), 0))
9015 != GP_REG_FIRST + 28))
9016 && ! rtx_equal_p (SET_SRC (set), slot))))
9017 break;
9018 else if (slot != NULL_RTX
9019 && GET_CODE (SET_DEST (set)) == MEM
9020 && rtx_equal_p (SET_DEST (set), slot)
9021 && (GET_CODE (SET_SRC (set)) != REG
9022 || REGNO (SET_SRC (set)) != REGNO (gpcopy)))
9023 break;
9026 /* If we couldn't find a unique value for GPCOPY or SLOT, then try a
9027 different optimization. Any time we find a copy of $28 into a
9028 register, followed by an add of a symbol_ref to that register, we
9029 convert it to load the value from the constant table instead.
9030 The copy and add will take six bytes, just as the load and
9031 constant table entry will take six bytes. However, it is
9032 possible that the constant table entry will be shared.
9034 This could be a peephole optimization, but I don't know if the
9035 peephole code can call force_const_mem.
9037 Using the same register for the copy of $28 and the add of the
9038 symbol_ref is actually pretty likely, since the add instruction
9039 requires the destination and the first addend to be the same
9040 register. */
9042 if (insn != NULL_RTX || gpcopy == NULL_RTX || slot == NULL_RTX)
9044 rtx next;
9046 /* This optimization is only reasonable if the constant table
9047 entries are only 4 bytes. */
9048 if (Pmode != SImode)
9049 return;
9051 for (insn = first; insn != NULL_RTX; insn = next)
9053 rtx set1, set2;
9055 next = insn;
9058 next = NEXT_INSN (next);
9060 while (next != NULL_RTX
9061 && (GET_CODE (next) == NOTE
9062 || (GET_CODE (next) == INSN
9063 && (GET_CODE (PATTERN (next)) == USE
9064 || GET_CODE (PATTERN (next)) == CLOBBER))));
9066 if (next == NULL_RTX)
9067 break;
9069 if (! INSN_P (insn))
9070 continue;
9072 if (! INSN_P (next))
9073 continue;
9075 set1 = PATTERN (insn);
9076 if (GET_CODE (set1) != SET)
9077 continue;
9078 set2 = PATTERN (next);
9079 if (GET_CODE (set2) != SET)
9080 continue;
9082 if (GET_CODE (SET_DEST (set1)) == REG
9083 && GET_CODE (SET_SRC (set1)) == CONST
9084 && GET_CODE (XEXP (SET_SRC (set1), 0)) == REG
9085 && REGNO (XEXP (SET_SRC (set1), 0)) == GP_REG_FIRST + 28
9086 && rtx_equal_p (SET_DEST (set1), SET_DEST (set2))
9087 && GET_CODE (SET_SRC (set2)) == PLUS
9088 && rtx_equal_p (SET_DEST (set1), XEXP (SET_SRC (set2), 0))
9089 && mips16_gp_offset_p (XEXP (SET_SRC (set2), 1))
9090 && GET_CODE (XEXP (XEXP (SET_SRC (set2), 1), 0)) == MINUS)
9092 rtx sym;
9094 /* We've found a case we can change to load from the
9095 constant table. */
9097 sym = XEXP (XEXP (XEXP (SET_SRC (set2), 1), 0), 0);
9098 if (GET_CODE (sym) != SYMBOL_REF)
9099 abort ();
9100 emit_insn_after (gen_rtx (SET, VOIDmode, SET_DEST (set1),
9101 force_const_mem (Pmode, sym)),
9102 next);
9104 PUT_CODE (insn, NOTE);
9105 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9106 NOTE_SOURCE_FILE (insn) = 0;
9108 PUT_CODE (next, NOTE);
9109 NOTE_LINE_NUMBER (next) = NOTE_INSN_DELETED;
9110 NOTE_SOURCE_FILE (next) = 0;
9114 return;
9117 /* We can safely remove all assignments to SLOT from GPCOPY, and
9118 replace all assignments from SLOT to GPCOPY with assignments from
9119 $28. */
9121 for (insn = first; insn != NULL_RTX; insn = next_active_insn (insn))
9123 rtx set;
9125 if (! INSN_P (insn))
9126 continue;
9128 set = PATTERN (insn);
9129 if (GET_CODE (set) != SET
9130 || GET_MODE (SET_DEST (set)) != (unsigned) Pmode)
9131 continue;
9133 if (GET_CODE (SET_DEST (set)) == MEM
9134 && rtx_equal_p (SET_DEST (set), slot)
9135 && GET_CODE (SET_SRC (set)) == REG
9136 && REGNO (SET_SRC (set)) == REGNO (gpcopy))
9138 PUT_CODE (insn, NOTE);
9139 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9140 NOTE_SOURCE_FILE (insn) = 0;
9142 else if (GET_CODE (SET_DEST (set)) == REG
9143 && REGNO (SET_DEST (set)) == REGNO (gpcopy)
9144 && GET_CODE (SET_SRC (set)) == MEM
9145 && rtx_equal_p (SET_SRC (set), slot))
9147 emit_insn_after (gen_rtx (SET, Pmode, SET_DEST (set),
9148 gen_rtx (CONST, Pmode,
9149 gen_rtx (REG, Pmode,
9150 GP_REG_FIRST + 28))),
9151 insn);
9152 PUT_CODE (insn, NOTE);
9153 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
9154 NOTE_SOURCE_FILE (insn) = 0;
9159 /* We keep a list of constants we which we have to add to internal
9160 constant tables in the middle of large functions. */
9162 struct constant
9164 struct constant *next;
9165 rtx value;
9166 rtx label;
9167 enum machine_mode mode;
9170 /* Add a constant to the list in *PCONSTANTS. */
9172 static rtx
9173 add_constant (pconstants, val, mode)
9174 struct constant **pconstants;
9175 rtx val;
9176 enum machine_mode mode;
9178 struct constant *c;
9180 for (c = *pconstants; c != NULL; c = c->next)
9181 if (mode == c->mode && rtx_equal_p (val, c->value))
9182 return c->label;
9184 c = (struct constant *) xmalloc (sizeof *c);
9185 c->value = val;
9186 c->mode = mode;
9187 c->label = gen_label_rtx ();
9188 c->next = *pconstants;
9189 *pconstants = c;
9190 return c->label;
9193 /* Dump out the constants in CONSTANTS after INSN. */
9195 static void
9196 dump_constants (constants, insn)
9197 struct constant *constants;
9198 rtx insn;
9200 struct constant *c;
9201 int align;
9203 c = constants;
9204 align = 0;
9205 while (c != NULL)
9207 rtx r;
9208 struct constant *next;
9210 switch (GET_MODE_SIZE (c->mode))
9212 case 1:
9213 align = 0;
9214 break;
9215 case 2:
9216 if (align < 1)
9217 insn = emit_insn_after (gen_align_2 (), insn);
9218 align = 1;
9219 break;
9220 case 4:
9221 if (align < 2)
9222 insn = emit_insn_after (gen_align_4 (), insn);
9223 align = 2;
9224 break;
9225 default:
9226 if (align < 3)
9227 insn = emit_insn_after (gen_align_8 (), insn);
9228 align = 3;
9229 break;
9232 insn = emit_label_after (c->label, insn);
9234 switch (c->mode)
9236 case QImode:
9237 r = gen_consttable_qi (c->value);
9238 break;
9239 case HImode:
9240 r = gen_consttable_hi (c->value);
9241 break;
9242 case SImode:
9243 r = gen_consttable_si (c->value);
9244 break;
9245 case SFmode:
9246 r = gen_consttable_sf (c->value);
9247 break;
9248 case DImode:
9249 r = gen_consttable_di (c->value);
9250 break;
9251 case DFmode:
9252 r = gen_consttable_df (c->value);
9253 break;
9254 default:
9255 abort ();
9258 insn = emit_insn_after (r, insn);
9260 next = c->next;
9261 free (c);
9262 c = next;
9265 emit_barrier_after (insn);
9268 /* Find the symbol in an address expression. */
9270 static rtx
9271 mips_find_symbol (addr)
9272 rtx addr;
9274 if (GET_CODE (addr) == MEM)
9275 addr = XEXP (addr, 0);
9276 while (GET_CODE (addr) == CONST)
9277 addr = XEXP (addr, 0);
9278 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
9279 return addr;
9280 if (GET_CODE (addr) == PLUS)
9282 rtx l1, l2;
9284 l1 = mips_find_symbol (XEXP (addr, 0));
9285 l2 = mips_find_symbol (XEXP (addr, 1));
9286 if (l1 != NULL_RTX && l2 == NULL_RTX)
9287 return l1;
9288 else if (l1 == NULL_RTX && l2 != NULL_RTX)
9289 return l2;
9291 return NULL_RTX;
9294 /* Exported to toplev.c.
9296 Do a final pass over the function, just before delayed branch
9297 scheduling. */
9299 void
9300 machine_dependent_reorg (first)
9301 rtx first;
9303 int insns_len, max_internal_pool_size, pool_size, addr, first_constant_ref;
9304 rtx insn;
9305 struct constant *constants;
9307 if (! TARGET_MIPS16)
9308 return;
9310 /* If $gp is used, try to remove stores, and replace loads with
9311 copies from $gp. */
9312 if (optimize)
9313 mips16_optimize_gp (first);
9315 /* Scan the function looking for PC relative loads which may be out
9316 of range. All such loads will either be from the constant table,
9317 or be getting the address of a constant string. If the size of
9318 the function plus the size of the constant table is less than
9319 0x8000, then all loads are in range. */
9321 insns_len = 0;
9322 for (insn = first; insn; insn = NEXT_INSN (insn))
9324 insns_len += get_attr_length (insn);
9326 /* ??? We put switch tables in .text, but we don't define
9327 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
9328 compute their lengths correctly. */
9329 if (GET_CODE (insn) == JUMP_INSN)
9331 rtx body;
9333 body = PATTERN (insn);
9334 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
9335 insns_len += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
9336 * GET_MODE_SIZE (GET_MODE (body)));
9337 insns_len += GET_MODE_SIZE (GET_MODE (body)) - 1;
9341 /* Store the original value of insns_len in current_frame_info, so
9342 that simple_memory_operand can look at it. */
9343 current_frame_info.insns_len = insns_len;
9345 pool_size = get_pool_size ();
9346 if (insns_len + pool_size + mips_string_length < 0x8000)
9347 return;
9349 /* Loop over the insns and figure out what the maximum internal pool
9350 size could be. */
9351 max_internal_pool_size = 0;
9352 for (insn = first; insn; insn = NEXT_INSN (insn))
9354 if (GET_CODE (insn) == INSN
9355 && GET_CODE (PATTERN (insn)) == SET)
9357 rtx src;
9359 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
9360 if (src == NULL_RTX)
9361 continue;
9362 if (CONSTANT_POOL_ADDRESS_P (src))
9363 max_internal_pool_size += GET_MODE_SIZE (get_pool_mode (src));
9364 else if (SYMBOL_REF_FLAG (src))
9365 max_internal_pool_size += GET_MODE_SIZE (Pmode);
9369 constants = NULL;
9370 addr = 0;
9371 first_constant_ref = -1;
9373 for (insn = first; insn; insn = NEXT_INSN (insn))
9375 if (GET_CODE (insn) == INSN
9376 && GET_CODE (PATTERN (insn)) == SET)
9378 rtx val, src;
9379 enum machine_mode mode = VOIDmode;
9381 val = NULL_RTX;
9382 src = mips_find_symbol (SET_SRC (PATTERN (insn)));
9383 if (src != NULL_RTX && CONSTANT_POOL_ADDRESS_P (src))
9385 /* ??? This is very conservative, which means that we
9386 will generate too many copies of the constant table.
9387 The only solution would seem to be some form of
9388 relaxing. */
9389 if (((insns_len - addr)
9390 + max_internal_pool_size
9391 + get_pool_offset (src))
9392 >= 0x8000)
9394 val = get_pool_constant (src);
9395 mode = get_pool_mode (src);
9397 max_internal_pool_size -= GET_MODE_SIZE (get_pool_mode (src));
9399 else if (src != NULL_RTX && SYMBOL_REF_FLAG (src))
9401 /* Including all of mips_string_length is conservative,
9402 and so is including all of max_internal_pool_size. */
9403 if (((insns_len - addr)
9404 + max_internal_pool_size
9405 + pool_size
9406 + mips_string_length)
9407 >= 0x8000)
9409 val = src;
9410 mode = Pmode;
9412 max_internal_pool_size -= Pmode;
9415 if (val != NULL_RTX)
9417 rtx lab, newsrc;
9419 /* This PC relative load is out of range. ??? In the
9420 case of a string constant, we are only guessing that
9421 it is range, since we don't know the offset of a
9422 particular string constant. */
9424 lab = add_constant (&constants, val, mode);
9425 newsrc = gen_rtx (MEM, mode,
9426 gen_rtx (LABEL_REF, VOIDmode, lab));
9427 RTX_UNCHANGING_P (newsrc) = 1;
9428 PATTERN (insn) = gen_rtx (SET, VOIDmode,
9429 SET_DEST (PATTERN (insn)),
9430 newsrc);
9431 INSN_CODE (insn) = -1;
9433 if (first_constant_ref < 0)
9434 first_constant_ref = addr;
9438 addr += get_attr_length (insn);
9440 /* ??? We put switch tables in .text, but we don't define
9441 JUMP_TABLES_IN_TEXT_SECTION, so get_attr_length will not
9442 compute their lengths correctly. */
9443 if (GET_CODE (insn) == JUMP_INSN)
9445 rtx body;
9447 body = PATTERN (insn);
9448 if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
9449 addr += (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
9450 * GET_MODE_SIZE (GET_MODE (body)));
9451 addr += GET_MODE_SIZE (GET_MODE (body)) - 1;
9454 if (GET_CODE (insn) == BARRIER)
9456 /* Output any constants we have accumulated. Note that we
9457 don't need to change ADDR, since its only use is
9458 subtraction from INSNS_LEN, and both would be changed by
9459 the same amount.
9460 ??? If the instructions up to the next barrier reuse a
9461 constant, it would often be better to continue
9462 accumulating. */
9463 if (constants != NULL)
9464 dump_constants (constants, insn);
9465 constants = NULL;
9466 first_constant_ref = -1;
9469 if (constants != NULL
9470 && (NEXT_INSN (insn) == NULL
9471 || (first_constant_ref >= 0
9472 && (((addr - first_constant_ref)
9473 + 2 /* for alignment */
9474 + 2 /* for a short jump insn */
9475 + pool_size)
9476 >= 0x8000))))
9478 /* If we haven't had a barrier within 0x8000 bytes of a
9479 constant reference or we are at the end of the function,
9480 emit a barrier now. */
9482 rtx label, jump, barrier;
9484 label = gen_label_rtx ();
9485 jump = emit_jump_insn_after (gen_jump (label), insn);
9486 JUMP_LABEL (jump) = label;
9487 LABEL_NUSES (label) = 1;
9488 barrier = emit_barrier_after (jump);
9489 emit_label_after (label, barrier);
9490 first_constant_ref = -1;
9494 /* ??? If we output all references to a constant in internal
9495 constants table, we don't need to output the constant in the real
9496 constant table, but we have no way to prevent that. */
9499 /* Return nonzero if X is a SIGN or ZERO extend operator. */
9501 extend_operator (x, mode)
9502 rtx x;
9503 enum machine_mode mode ATTRIBUTE_UNUSED;
9505 enum rtx_code code = GET_CODE (x);
9506 return code == SIGN_EXTEND || code == ZERO_EXTEND;
9509 /* Accept any operator that can be used to shift the high half of the
9510 input value to the lower half, suitable for truncation. The
9511 remainder (the lower half of the input, and the upper half of the
9512 output) will be discarded. */
9514 highpart_shift_operator (x, mode)
9515 rtx x;
9516 enum machine_mode mode ATTRIBUTE_UNUSED;
9518 enum rtx_code code = GET_CODE (x);
9519 return (code == LSHIFTRT
9520 || code == ASHIFTRT
9521 || code == ROTATERT
9522 || code == ROTATE);
9525 /* Return the length of INSN. LENGTH is the initial length computed by
9526 attributes in the machine-description file. */
9529 mips_adjust_insn_length (insn, length)
9530 rtx insn;
9531 int length;
9533 /* A unconditional jump has an unfilled delay slot if it is not part
9534 of a sequence. A conditional jump normally has a delay slot, but
9535 does not on MIPS16. */
9536 if (simplejump_p (insn)
9537 || (!TARGET_MIPS16 && (GET_CODE (insn) == JUMP_INSN
9538 || GET_CODE (insn) == CALL_INSN)))
9539 length += 4;
9541 /* All MIPS16 instructions are a measly two bytes. */
9542 if (TARGET_MIPS16)
9543 length /= 2;
9545 return length;
9548 /* Output assembly instructions to peform a conditional branch.
9550 INSN is the branch instruction. OPERANDS[0] is the condition.
9551 OPERANDS[1] is the target of the branch. OPERANDS[2] is the target
9552 of the first operand to the condition. If TWO_OPERANDS_P is
9553 non-zero the comparison takes two operands; OPERANDS[3] will be the
9554 second operand.
9556 If INVERTED_P is non-zero we are to branch if the condition does
9557 not hold. If FLOAT_P is non-zero this is a floating-point comparison.
9559 LENGTH is the length (in bytes) of the sequence we are to generate.
9560 That tells us whether to generate a simple conditional branch, or a
9561 reversed conditional branch around a `jr' instruction. */
9562 char *
9563 mips_output_conditional_branch (insn,
9564 operands,
9565 two_operands_p,
9566 float_p,
9567 inverted_p,
9568 length)
9569 rtx insn;
9570 rtx *operands;
9571 int two_operands_p;
9572 int float_p;
9573 int inverted_p;
9574 int length;
9576 static char buffer[200];
9577 /* The kind of comparison we are doing. */
9578 enum rtx_code code = GET_CODE (operands[0]);
9579 /* Non-zero if the opcode for the comparison needs a `z' indicating
9580 that it is a comparision against zero. */
9581 int need_z_p;
9582 /* A string to use in the assembly output to represent the first
9583 operand. */
9584 const char *op1 = "%z2";
9585 /* A string to use in the assembly output to represent the second
9586 operand. Use the hard-wired zero register if there's no second
9587 operand. */
9588 const char *op2 = (two_operands_p ? ",%z3" : ",%.");
9589 /* The operand-printing string for the comparison. */
9590 const char *const comp = (float_p ? "%F0" : "%C0");
9591 /* The operand-printing string for the inverted comparison. */
9592 const char *const inverted_comp = (float_p ? "%W0" : "%N0");
9594 /* The MIPS processors (for levels of the ISA at least two), have
9595 "likely" variants of each branch instruction. These instructions
9596 annul the instruction in the delay slot if the branch is not
9597 taken. */
9598 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
9600 if (!two_operands_p)
9602 /* To compute whether than A > B, for example, we normally
9603 subtract B from A and then look at the sign bit. But, if we
9604 are doing an unsigned comparison, and B is zero, we don't
9605 have to do the subtraction. Instead, we can just check to
9606 see if A is non-zero. Thus, we change the CODE here to
9607 reflect the simpler comparison operation. */
9608 switch (code)
9610 case GTU:
9611 code = NE;
9612 break;
9614 case LEU:
9615 code = EQ;
9616 break;
9618 case GEU:
9619 /* A condition which will always be true. */
9620 code = EQ;
9621 op1 = "%.";
9622 break;
9624 case LTU:
9625 /* A condition which will always be false. */
9626 code = NE;
9627 op1 = "%.";
9628 break;
9630 default:
9631 /* Not a special case. */
9632 break;
9636 /* Relative comparisons are always done against zero. But
9637 equality comparisons are done between two operands, and therefore
9638 do not require a `z' in the assembly language output. */
9639 need_z_p = (!float_p && code != EQ && code != NE);
9640 /* For comparisons against zero, the zero is not provided
9641 explicitly. */
9642 if (need_z_p)
9643 op2 = "";
9645 /* Begin by terminating the buffer. That way we can always use
9646 strcat to add to it. */
9647 buffer[0] = '\0';
9649 switch (length)
9651 case 4:
9652 case 8:
9653 /* Just a simple conditional branch. */
9654 if (float_p)
9655 sprintf (buffer, "%%*b%s%%?\t%%Z2%%1",
9656 inverted_p ? inverted_comp : comp);
9657 else
9658 sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1",
9659 inverted_p ? inverted_comp : comp,
9660 need_z_p ? "z" : "",
9661 op1,
9662 op2);
9663 return buffer;
9665 case 12:
9666 case 16:
9668 /* Generate a reversed conditional branch around ` j'
9669 instruction:
9671 .set noreorder
9672 .set nomacro
9673 bc l
9675 j target
9676 .set macro
9677 .set reorder
9680 Because we have to jump four bytes *past* the following
9681 instruction if this branch was annulled, we can't just use
9682 a label, as in the picture above; there's no way to put the
9683 label after the next instruction, as the assembler does not
9684 accept `.L+4' as the target of a branch. (We can't just
9685 wait until the next instruction is output; it might be a
9686 macro and take up more than four bytes. Once again, we see
9687 why we want to eliminate macros.)
9689 If the branch is annulled, we jump four more bytes that we
9690 would otherwise; that way we skip the annulled instruction
9691 in the delay slot. */
9693 const char *const target
9694 = ((mips_branch_likely || length == 16) ? ".+16" : ".+12");
9695 char *c;
9697 strcpy (buffer, "%(%<");
9698 c = strchr (buffer, '\0');
9699 /* Generate the reversed comparision. This takes four
9700 bytes. */
9701 if (float_p)
9702 sprintf (c, "%%*b%s\t%%Z2%s",
9703 inverted_p ? comp : inverted_comp,
9704 target);
9705 else
9706 sprintf (c, "%%*b%s%s\t%s%s,%s",
9707 inverted_p ? comp : inverted_comp,
9708 need_z_p ? "z" : "",
9709 op1,
9710 op2,
9711 target);
9712 strcat (c, "\n\tnop\n\tj\t%1");
9713 if (length == 16)
9714 /* The delay slot was unfilled. Since we're inside
9715 .noreorder, the assembler will not fill in the NOP for
9716 us, so we must do it ourselves. */
9717 strcat (buffer, "\n\tnop");
9718 strcat (buffer, "%>%)");
9719 return buffer;
9722 /* We do not currently use this code. It handles jumps to
9723 arbitrary locations, using `jr', even across a 256MB boundary.
9724 We could add a -mhuge switch, and then use this code instead of
9725 the `j' alternative above when -mhuge was used. */
9726 #if 0
9727 case 16:
9728 case 20:
9730 /* Generate a reversed conditional branch around a `jr'
9731 instruction:
9733 .set noreorder
9734 .set nomacro
9735 .set noat
9736 bc l
9737 la $at, target
9738 jr $at
9739 .set at
9740 .set macro
9741 .set reorder
9744 Not pretty, but allows a conditional branch anywhere in the
9745 32-bit address space. If the original branch is annulled,
9746 then the instruction in the delay slot should be executed
9747 only if the branch is taken. The la instruction is really
9748 a macro which will usually take eight bytes, but sometimes
9749 takes only four, if the instruction to which we're jumping
9750 gets its own entry in the global pointer table, which will
9751 happen if its a case label. The assembler will then
9752 generate only a four-byte sequence, rather than eight, and
9753 there seems to be no way to tell it not to. Thus, we can't
9754 just use a `.+x' addressing form; we don't know what value
9755 to give for `x'.
9757 So, we resort to using the explicit relocation syntax
9758 available in the assembler and do:
9760 lw $at,%got_page(target)($gp)
9761 daddiu $at,$at,%got_ofst(target)
9763 That way, this always takes up eight bytes, and we can use
9764 the `.+x' form. Of course, these explicit machinations
9765 with relocation will not work with old assemblers. Then
9766 again, neither do out-of-range branches, so we haven't lost
9767 anything. */
9769 /* The target of the reversed branch. */
9770 const char *const target
9771 = ((mips_branch_likely || length == 20) ? ".+20" : ".+16");
9772 const char *at_register = mips_reg_names[ASSEMBLER_SCRATCH_REGNUM];
9773 const char *gp_register = mips_reg_names[PIC_OFFSET_TABLE_REGNUM];
9774 char *c;
9776 strcpy (buffer, "%(%<%[");
9777 c = strchr (buffer, '\0');
9778 /* Generate the reversed comparision. This takes four
9779 bytes. */
9780 if (float_p)
9781 sprintf (c, "%%*b%s\t%%Z2%s",
9782 inverted_p ? comp : inverted_comp,
9783 target);
9784 else
9785 sprintf (c, "%%*b%s%s\t%s%s,%s",
9786 inverted_p ? comp : inverted_comp,
9787 need_z_p ? "z" : "",
9788 op1,
9789 op2,
9790 target);
9791 c = strchr (buffer, '\0');
9792 /* Generate the load-address, and jump. This takes twelve
9793 bytes, for a total of 16. */
9794 sprintf (c,
9795 "\n\tlw\t%s,%%%%got_page(%%1)(%s)\n\tdaddiu\t%s,%s,%%%%got_ofst(%%1)\n\tjr\t%s",
9796 at_register,
9797 gp_register,
9798 at_register,
9799 at_register,
9800 at_register);
9801 if (length == 20)
9802 /* The delay slot was unfilled. Since we're inside
9803 .noreorder, the assembler will not fill in the NOP for
9804 us, so we must do it ourselves. */
9805 strcat (buffer, "\n\tnop");
9806 strcat (buffer, "%]%>%)");
9807 return buffer;
9809 #endif
9811 default:
9812 abort ();
9815 /* NOTREACHED */
9816 return 0;
9819 /* Called to register all of our global variables with the garbage
9820 collector. */
9822 static void
9823 mips_add_gc_roots ()
9825 ggc_add_rtx_root (&mips_load_reg, 1);
9826 ggc_add_rtx_root (&mips_load_reg2, 1);
9827 ggc_add_rtx_root (&mips_load_reg3, 1);
9828 ggc_add_rtx_root (&mips_load_reg4, 1);
9829 ggc_add_rtx_root (branch_cmp, sizeof (branch_cmp) / sizeof (rtx));
9830 ggc_add_rtx_root (&embedded_pic_fnaddr_rtx, 1);
9831 ggc_add_rtx_root (&mips16_gp_pseudo_rtx, 1);
9834 static enum processor_type
9835 mips_parse_cpu (cpu_string)
9836 const char *cpu_string;
9838 const char *p = cpu_string;
9839 int seen_v = 0;
9840 enum processor_type cpu;
9842 /* We need to cope with the various "vr" prefixes for the NEC 4300
9843 and 4100 processors. */
9844 if (*p == 'v' || *p == 'V')
9845 seen_v = 1, p++;
9847 if (*p == 'r' || *p == 'R')
9848 p++;
9850 /* Since there is no difference between a R2000 and R3000 in
9851 terms of the scheduler, we collapse them into just an R3000. */
9853 cpu = PROCESSOR_DEFAULT;
9854 switch (*p)
9856 case '2':
9857 if (!strcmp (p, "2000") || !strcmp (p, "2k") || !strcmp (p, "2K"))
9858 cpu = PROCESSOR_R3000;
9859 else if (!strcmp (p, "20kc") || !strcmp (p, "20Kc") )
9860 cpu = PROCESSOR_R20KC;
9861 break;
9863 case '3':
9864 if (!strcmp (p, "3000") || !strcmp (p, "3k") || !strcmp (p, "3K"))
9865 cpu = PROCESSOR_R3000;
9866 else if (!strcmp (p, "3900"))
9867 cpu = PROCESSOR_R3900;
9868 break;
9870 case '4':
9871 if (!strcmp (p, "4000") || !strcmp (p, "4k") || !strcmp (p, "4K"))
9872 cpu = PROCESSOR_R4000;
9873 /* The vr4100 is a non-FP ISA III processor with some extra
9874 instructions. */
9875 else if (!strcmp (p, "4100"))
9876 cpu = PROCESSOR_R4100;
9877 /* The vr4300 is a standard ISA III processor, but with a different
9878 pipeline. */
9879 else if (!strcmp (p, "4300"))
9880 cpu = PROCESSOR_R4300;
9881 /* The r4400 is exactly the same as the r4000 from the compiler's
9882 viewpoint. */
9883 else if (!strcmp (p, "4400"))
9884 cpu = PROCESSOR_R4000;
9885 else if (!strcmp (p, "4600"))
9886 cpu = PROCESSOR_R4600;
9887 else if (!strcmp (p, "4650"))
9888 cpu = PROCESSOR_R4650;
9889 /* The 4kc and 4kp processor cores are the same for
9890 scheduling purposes; they both implement the MIPS32
9891 ISA and only differ in their memory management
9892 methods. */
9893 else if (!strcmp (p, "4kc") || !strcmp (p, "4Kc")
9894 || !strcmp (p, "4kp") || !strcmp (p, "4Kp") )
9895 cpu = PROCESSOR_R4KC;
9896 break;
9898 case '5':
9899 if (!strcmp (p, "5000") || !strcmp (p, "5k") || !strcmp (p, "5K"))
9900 cpu = PROCESSOR_R5000;
9901 else if (!strcmp (p, "5kc") || !strcmp (p, "5Kc") )
9902 cpu = PROCESSOR_R5KC;
9903 break;
9905 case '6':
9906 if (!strcmp (p, "6000") || !strcmp (p, "6k") || !strcmp (p, "6K"))
9907 cpu = PROCESSOR_R6000;
9908 break;
9910 case '8':
9911 if (!strcmp (p, "8000"))
9912 cpu = PROCESSOR_R8000;
9913 break;
9915 case 'o':
9916 if (!strcmp (p, "orion"))
9917 cpu = PROCESSOR_R4600;
9918 break;
9921 if (seen_v
9922 && cpu != PROCESSOR_R4300
9923 && cpu != PROCESSOR_R4100
9924 && cpu != PROCESSOR_R5000)
9925 cpu = PROCESSOR_DEFAULT;
9927 return cpu;
9930 /* Adjust the cost of INSN based on the relationship between INSN that
9931 is dependent on DEP_INSN through the dependence LINK. The default
9932 is to make no adjustment to COST.
9934 On the MIPS, ignore the cost of anti- and output-dependencies. */
9935 static int
9936 mips_adjust_cost (insn, link, dep, cost)
9937 rtx insn ATTRIBUTE_UNUSED;
9938 rtx link;
9939 rtx dep ATTRIBUTE_UNUSED;
9940 int cost;
9942 if (REG_NOTE_KIND (link) != 0)
9943 return 0; /* Anti or output dependence. */
9944 return cost;
9947 /* Cover function for UNIQUE_SECTION. */
9949 void
9950 mips_unique_section (decl, reloc)
9951 tree decl;
9952 int reloc;
9954 int len, size, sec;
9955 const char *name, *prefix;
9956 char *string;
9957 static const char *const prefixes[4][2] = {
9958 { ".text.", ".gnu.linkonce.t." },
9959 { ".rodata.", ".gnu.linkonce.r." },
9960 { ".data.", ".gnu.linkonce.d." },
9961 { ".sdata.", ".gnu.linkonce.s." }
9964 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
9965 size = int_size_in_bytes (TREE_TYPE (decl));
9967 /* Determine the base section we are interested in:
9968 0=text, 1=rodata, 2=data, 3=sdata, [4=bss]. */
9969 if (TREE_CODE (decl) == FUNCTION_DECL)
9970 sec = 0;
9971 else if (DECL_INITIAL (decl) == 0
9972 || DECL_INITIAL (decl) == error_mark_node)
9973 sec = 2;
9974 else if ((TARGET_EMBEDDED_PIC || TARGET_MIPS16)
9975 && TREE_CODE (decl) == STRING_CST
9976 && !flag_writable_strings)
9978 /* For embedded position independent code, put constant
9979 strings in the text section, because the data section
9980 is limited to 64K in size. For mips16 code, put
9981 strings in the text section so that a PC relative load
9982 instruction can be used to get their address. */
9983 sec = 0;
9985 else if (TARGET_EMBEDDED_DATA)
9987 /* For embedded applications, always put an object in
9988 read-only data if possible, in order to reduce RAM
9989 usage. */
9991 if (DECL_READONLY_SECTION (decl, reloc))
9992 sec = 1;
9993 else if (size > 0 && size <= mips_section_threshold)
9994 sec = 3;
9995 else
9996 sec = 2;
9998 else
10000 /* For hosted applications, always put an object in
10001 small data if possible, as this gives the best
10002 performance. */
10004 if (size > 0 && size <= mips_section_threshold)
10005 sec = 3;
10006 else if (DECL_READONLY_SECTION (decl, reloc))
10007 sec = 1;
10008 else
10009 sec = 2;
10012 prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
10013 len = strlen (name) + strlen (prefix);
10014 string = alloca (len + 1);
10015 sprintf (string, "%s%s", prefix, name);
10017 DECL_SECTION_NAME (decl) = build_string (len, string);
10020 unsigned int
10021 mips_hard_regno_nregs (regno, mode)
10022 int regno;
10023 enum machine_mode mode;
10025 if (! FP_REG_P (regno))
10026 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
10027 else
10028 return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
10032 #ifdef TARGET_IRIX6
10033 /* Output assembly to switch to section NAME with attribute FLAGS. */
10035 static void
10036 iris6_asm_named_section_1 (name, flags, align)
10037 const char *name;
10038 unsigned int flags;
10039 unsigned int align;
10041 unsigned int sh_type, sh_flags, sh_entsize;
10043 sh_flags = 0;
10044 if (!(flags & SECTION_DEBUG))
10045 sh_flags |= 2; /* SHF_ALLOC */
10046 if (flags & SECTION_WRITE)
10047 sh_flags |= 1; /* SHF_WRITE */
10048 if (flags & SECTION_CODE)
10049 sh_flags |= 4; /* SHF_EXECINSTR */
10050 if (flags & SECTION_SMALL)
10051 sh_flags |= 0x10000000; /* SHF_MIPS_GPREL */
10052 if (strcmp (name, ".debug_frame") == 0)
10053 sh_flags |= 0x08000000; /* SHF_MIPS_NOSTRIP */
10054 if (flags & SECTION_DEBUG)
10055 sh_type = 0x7000001e; /* SHT_MIPS_DWARF */
10056 else if (flags & SECTION_BSS)
10057 sh_type = 8; /* SHT_NOBITS */
10058 else
10059 sh_type = 1; /* SHT_PROGBITS */
10061 if (flags & SECTION_CODE)
10062 sh_entsize = 4;
10063 else
10064 sh_entsize = 0;
10066 fprintf (asm_out_file, "\t.section %s,%#x,%#x,%u,%u\n",
10067 name, sh_type, sh_flags, sh_entsize, align);
10070 static void
10071 iris6_asm_named_section (name, flags)
10072 const char *name;
10073 unsigned int flags;
10075 if (TARGET_FILE_SWITCHING && (flags & SECTION_CODE))
10076 asm_out_file = asm_out_text_file;
10077 iris6_asm_named_section_1 (name, flags, 0);
10080 /* In addition to emitting a .align directive, record the maximum
10081 alignment requested for the current section. */
10083 struct iris_section_align_entry
10085 const char *name;
10086 unsigned int log;
10087 unsigned int flags;
10090 static htab_t iris_section_align_htab;
10091 static FILE *iris_orig_asm_out_file;
10093 static int
10094 iris_section_align_entry_eq (p1, p2)
10095 const PTR p1;
10096 const PTR p2;
10098 const struct iris_section_align_entry *old = p1;
10099 const char *new = p2;
10101 return strcmp (old->name, new) == 0;
10104 static hashval_t
10105 iris_section_align_entry_hash (p)
10106 const PTR p;
10108 const struct iris_section_align_entry *old = p;
10109 return htab_hash_string (old->name);
10112 void
10113 iris6_asm_output_align (file, log)
10114 FILE *file;
10115 unsigned int log;
10117 const char *section = current_section_name ();
10118 struct iris_section_align_entry **slot, *entry;
10120 if (! section)
10121 abort ();
10123 slot = (struct iris_section_align_entry **)
10124 htab_find_slot_with_hash (iris_section_align_htab, section,
10125 htab_hash_string (section), INSERT);
10126 entry = *slot;
10127 if (! entry)
10129 entry = (struct iris_section_align_entry *)
10130 xmalloc (sizeof (struct iris_section_align_entry));
10131 *slot = entry;
10132 entry->name = section;
10133 entry->log = log;
10134 entry->flags = current_section_flags ();
10136 else if (entry->log < log)
10137 entry->log = log;
10139 fprintf (file, "\t.align\t%u\n", log);
10142 /* The Iris assembler does not record alignment from .align directives,
10143 but takes it from the first .section directive seen. Play yet more
10144 file switching games so that we can emit a .section directive at the
10145 beginning of the file with the proper alignment attached. */
10147 void
10148 iris6_asm_file_start (stream)
10149 FILE *stream;
10151 mips_asm_file_start (stream);
10153 iris_orig_asm_out_file = asm_out_file;
10154 stream = tmpfile ();
10155 asm_out_file = stream;
10156 asm_out_data_file = stream;
10157 if (! TARGET_FILE_SWITCHING)
10158 asm_out_text_file = stream;
10160 iris_section_align_htab = htab_create (31, iris_section_align_entry_hash,
10161 iris_section_align_entry_eq, NULL);
10164 static int
10165 iris6_section_align_1 (slot, data)
10166 void **slot;
10167 void *data ATTRIBUTE_UNUSED;
10169 const struct iris_section_align_entry *entry
10170 = *(const struct iris_section_align_entry **) slot;
10172 iris6_asm_named_section_1 (entry->name, entry->flags, 1 << entry->log);
10173 return 1;
10176 void
10177 iris6_asm_file_end (stream)
10178 FILE *stream;
10180 /* Emit section directives with the proper alignment at the top of the
10181 real output file. */
10182 asm_out_file = iris_orig_asm_out_file;
10183 htab_traverse (iris_section_align_htab, iris6_section_align_1, NULL);
10185 /* Copy the data emitted to the temp file to the real output file. */
10186 copy_file_data (asm_out_file, stream);
10188 mips_asm_file_end (stream);
10190 #endif /* TARGET_IRIX6 */