]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/m32r/m32r.c
m32r.h (REG_OK_FOR_BASE_P, [...]): Remove macros.
[thirdparty/gcc.git] / gcc / config / m32r / m32r.c
1 /* Subroutines used for code generation on the Renesas M32R cpu.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "recog.h"
37 #include "diagnostic-core.h"
38 #include "ggc.h"
39 #include "integrate.h"
40 #include "df.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "tm-constrs.h"
45
46 /* Array of valid operand punctuation characters. */
47 static char m32r_punct_chars[256];
48
49 /* Selected code model. */
50 enum m32r_model m32r_model = M32R_MODEL_DEFAULT;
51
52 /* Selected SDA support. */
53 enum m32r_sdata m32r_sdata = M32R_SDATA_DEFAULT;
54
55 /* Machine-specific symbol_ref flags. */
56 #define SYMBOL_FLAG_MODEL_SHIFT SYMBOL_FLAG_MACH_DEP_SHIFT
57 #define SYMBOL_REF_MODEL(X) \
58 ((enum m32r_model) ((SYMBOL_REF_FLAGS (X) >> SYMBOL_FLAG_MODEL_SHIFT) & 3))
59
60 /* For string literals, etc. */
61 #define LIT_NAME_P(NAME) ((NAME)[0] == '*' && (NAME)[1] == '.')
62
63 /* Forward declaration. */
64 static bool m32r_handle_option (size_t, const char *, int);
65 static void m32r_option_override (void);
66 static void init_reg_tables (void);
67 static void block_move_call (rtx, rtx, rtx);
68 static int m32r_is_insn (rtx);
69 static bool m32r_legitimate_address_p (enum machine_mode, rtx, bool);
70 static rtx m32r_legitimize_address (rtx, rtx, enum machine_mode);
71 static bool m32r_mode_dependent_address_p (const_rtx);
72 static tree m32r_handle_model_attribute (tree *, tree, tree, int, bool *);
73 static void m32r_print_operand (FILE *, rtx, int);
74 static void m32r_print_operand_address (FILE *, rtx);
75 static bool m32r_print_operand_punct_valid_p (unsigned char code);
76 static void m32r_output_function_prologue (FILE *, HOST_WIDE_INT);
77 static void m32r_output_function_epilogue (FILE *, HOST_WIDE_INT);
78
79 static void m32r_file_start (void);
80
81 static int m32r_adjust_priority (rtx, int);
82 static int m32r_issue_rate (void);
83
84 static void m32r_encode_section_info (tree, rtx, int);
85 static bool m32r_in_small_data_p (const_tree);
86 static bool m32r_return_in_memory (const_tree, const_tree);
87 static rtx m32r_function_value (const_tree, const_tree, bool);
88 static rtx m32r_libcall_value (enum machine_mode, const_rtx);
89 static bool m32r_function_value_regno_p (const unsigned int);
90 static void m32r_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
91 tree, int *, int);
92 static void init_idents (void);
93 static bool m32r_rtx_costs (rtx, int, int, int *, bool speed);
94 static int m32r_memory_move_cost (enum machine_mode, reg_class_t, bool);
95 static bool m32r_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
96 const_tree, bool);
97 static int m32r_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
98 tree, bool);
99 static rtx m32r_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
100 const_tree, bool);
101 static void m32r_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
102 const_tree, bool);
103 static bool m32r_can_eliminate (const int, const int);
104 static void m32r_conditional_register_usage (void);
105 static void m32r_trampoline_init (rtx, tree, rtx);
106 \f
107 /* M32R specific attributes. */
108
109 static const struct attribute_spec m32r_attribute_table[] =
110 {
111 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
112 { "interrupt", 0, 0, true, false, false, NULL },
113 { "model", 1, 1, true, false, false, m32r_handle_model_attribute },
114 { NULL, 0, 0, false, false, false, NULL }
115 };
116
117 static const struct default_options m32r_option_optimization_table[] =
118 {
119 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
120 { OPT_LEVELS_1_PLUS, OPT_fregmove, NULL, 1 },
121 { OPT_LEVELS_NONE, 0, NULL, 0 }
122 };
123 \f
124 /* Initialize the GCC target structure. */
125 #undef TARGET_ATTRIBUTE_TABLE
126 #define TARGET_ATTRIBUTE_TABLE m32r_attribute_table
127
128 #undef TARGET_LEGITIMATE_ADDRESS_P
129 #define TARGET_LEGITIMATE_ADDRESS_P m32r_legitimate_address_p
130 #undef TARGET_LEGITIMIZE_ADDRESS
131 #define TARGET_LEGITIMIZE_ADDRESS m32r_legitimize_address
132 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
133 #define TARGET_MODE_DEPENDENT_ADDRESS_P m32r_mode_dependent_address_p
134
135 #undef TARGET_ASM_ALIGNED_HI_OP
136 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
137 #undef TARGET_ASM_ALIGNED_SI_OP
138 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
139
140 #undef TARGET_PRINT_OPERAND
141 #define TARGET_PRINT_OPERAND m32r_print_operand
142 #undef TARGET_PRINT_OPERAND_ADDRESS
143 #define TARGET_PRINT_OPERAND_ADDRESS m32r_print_operand_address
144 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
145 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P m32r_print_operand_punct_valid_p
146
147 #undef TARGET_ASM_FUNCTION_PROLOGUE
148 #define TARGET_ASM_FUNCTION_PROLOGUE m32r_output_function_prologue
149 #undef TARGET_ASM_FUNCTION_EPILOGUE
150 #define TARGET_ASM_FUNCTION_EPILOGUE m32r_output_function_epilogue
151
152 #undef TARGET_ASM_FILE_START
153 #define TARGET_ASM_FILE_START m32r_file_start
154
155 #undef TARGET_SCHED_ADJUST_PRIORITY
156 #define TARGET_SCHED_ADJUST_PRIORITY m32r_adjust_priority
157 #undef TARGET_SCHED_ISSUE_RATE
158 #define TARGET_SCHED_ISSUE_RATE m32r_issue_rate
159
160 #undef TARGET_DEFAULT_TARGET_FLAGS
161 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_CPU_DEFAULT
162 #undef TARGET_HANDLE_OPTION
163 #define TARGET_HANDLE_OPTION m32r_handle_option
164 #undef TARGET_OPTION_OVERRIDE
165 #define TARGET_OPTION_OVERRIDE m32r_option_override
166 #undef TARGET_OPTION_OPTIMIZATION_TABLE
167 #define TARGET_OPTION_OPTIMIZATION_TABLE m32r_option_optimization_table
168
169 #undef TARGET_ENCODE_SECTION_INFO
170 #define TARGET_ENCODE_SECTION_INFO m32r_encode_section_info
171 #undef TARGET_IN_SMALL_DATA_P
172 #define TARGET_IN_SMALL_DATA_P m32r_in_small_data_p
173
174
175 #undef TARGET_MEMORY_MOVE_COST
176 #define TARGET_MEMORY_MOVE_COST m32r_memory_move_cost
177 #undef TARGET_RTX_COSTS
178 #define TARGET_RTX_COSTS m32r_rtx_costs
179 #undef TARGET_ADDRESS_COST
180 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
181
182 #undef TARGET_PROMOTE_PROTOTYPES
183 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
184 #undef TARGET_RETURN_IN_MEMORY
185 #define TARGET_RETURN_IN_MEMORY m32r_return_in_memory
186
187 #undef TARGET_FUNCTION_VALUE
188 #define TARGET_FUNCTION_VALUE m32r_function_value
189 #undef TARGET_LIBCALL_VALUE
190 #define TARGET_LIBCALL_VALUE m32r_libcall_value
191 #undef TARGET_FUNCTION_VALUE_REGNO_P
192 #define TARGET_FUNCTION_VALUE_REGNO_P m32r_function_value_regno_p
193
194 #undef TARGET_SETUP_INCOMING_VARARGS
195 #define TARGET_SETUP_INCOMING_VARARGS m32r_setup_incoming_varargs
196 #undef TARGET_MUST_PASS_IN_STACK
197 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
198 #undef TARGET_PASS_BY_REFERENCE
199 #define TARGET_PASS_BY_REFERENCE m32r_pass_by_reference
200 #undef TARGET_ARG_PARTIAL_BYTES
201 #define TARGET_ARG_PARTIAL_BYTES m32r_arg_partial_bytes
202 #undef TARGET_FUNCTION_ARG
203 #define TARGET_FUNCTION_ARG m32r_function_arg
204 #undef TARGET_FUNCTION_ARG_ADVANCE
205 #define TARGET_FUNCTION_ARG_ADVANCE m32r_function_arg_advance
206
207 #undef TARGET_CAN_ELIMINATE
208 #define TARGET_CAN_ELIMINATE m32r_can_eliminate
209
210 #undef TARGET_CONDITIONAL_REGISTER_USAGE
211 #define TARGET_CONDITIONAL_REGISTER_USAGE m32r_conditional_register_usage
212
213 #undef TARGET_TRAMPOLINE_INIT
214 #define TARGET_TRAMPOLINE_INIT m32r_trampoline_init
215
216 #undef TARGET_EXCEPT_UNWIND_INFO
217 #define TARGET_EXCEPT_UNWIND_INFO sjlj_except_unwind_info
218
219 struct gcc_target targetm = TARGET_INITIALIZER;
220 \f
221 /* Implement TARGET_HANDLE_OPTION. */
222
223 static bool
224 m32r_handle_option (size_t code, const char *arg, int value)
225 {
226 switch (code)
227 {
228 case OPT_m32r:
229 target_flags &= ~(MASK_M32R2 | MASK_M32RX);
230 return true;
231
232 case OPT_mmodel_:
233 if (strcmp (arg, "small") == 0)
234 m32r_model = M32R_MODEL_SMALL;
235 else if (strcmp (arg, "medium") == 0)
236 m32r_model = M32R_MODEL_MEDIUM;
237 else if (strcmp (arg, "large") == 0)
238 m32r_model = M32R_MODEL_LARGE;
239 else
240 return false;
241 return true;
242
243 case OPT_msdata_:
244 if (strcmp (arg, "none") == 0)
245 m32r_sdata = M32R_SDATA_NONE;
246 else if (strcmp (arg, "sdata") == 0)
247 m32r_sdata = M32R_SDATA_SDATA;
248 else if (strcmp (arg, "use") == 0)
249 m32r_sdata = M32R_SDATA_USE;
250 else
251 return false;
252 return true;
253
254 case OPT_mno_flush_func:
255 m32r_cache_flush_func = NULL;
256 return true;
257
258 case OPT_mflush_trap_:
259 return value <= 15;
260
261 case OPT_mno_flush_trap:
262 m32r_cache_flush_trap = -1;
263 return true;
264
265 default:
266 return true;
267 }
268 }
269
270 /* Called by m32r_option_override to initialize various things. */
271
272 void
273 m32r_init (void)
274 {
275 init_reg_tables ();
276
277 /* Initialize array for TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
278 memset (m32r_punct_chars, 0, sizeof (m32r_punct_chars));
279 m32r_punct_chars['#'] = 1;
280 m32r_punct_chars['@'] = 1; /* ??? no longer used */
281
282 /* Provide default value if not specified. */
283 if (!global_options_set.x_g_switch_value)
284 g_switch_value = SDATA_DEFAULT_SIZE;
285 }
286
287 static void
288 m32r_option_override (void)
289 {
290 /* These need to be done at start up.
291 It's convenient to do them here. */
292 m32r_init ();
293 SUBTARGET_OVERRIDE_OPTIONS;
294 }
295
296 /* Vectors to keep interesting information about registers where it can easily
297 be got. We use to use the actual mode value as the bit number, but there
298 is (or may be) more than 32 modes now. Instead we use two tables: one
299 indexed by hard register number, and one indexed by mode. */
300
301 /* The purpose of m32r_mode_class is to shrink the range of modes so that
302 they all fit (as bit numbers) in a 32-bit word (again). Each real mode is
303 mapped into one m32r_mode_class mode. */
304
305 enum m32r_mode_class
306 {
307 C_MODE,
308 S_MODE, D_MODE, T_MODE, O_MODE,
309 SF_MODE, DF_MODE, TF_MODE, OF_MODE, A_MODE
310 };
311
312 /* Modes for condition codes. */
313 #define C_MODES (1 << (int) C_MODE)
314
315 /* Modes for single-word and smaller quantities. */
316 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
317
318 /* Modes for double-word and smaller quantities. */
319 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
320
321 /* Modes for quad-word and smaller quantities. */
322 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
323
324 /* Modes for accumulators. */
325 #define A_MODES (1 << (int) A_MODE)
326
327 /* Value is 1 if register/mode pair is acceptable on arc. */
328
329 const unsigned int m32r_hard_regno_mode_ok[FIRST_PSEUDO_REGISTER] =
330 {
331 T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
332 T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, S_MODES, S_MODES, S_MODES,
333 S_MODES, C_MODES, A_MODES, A_MODES
334 };
335
336 unsigned int m32r_mode_class [NUM_MACHINE_MODES];
337
338 enum reg_class m32r_regno_reg_class[FIRST_PSEUDO_REGISTER];
339
340 static void
341 init_reg_tables (void)
342 {
343 int i;
344
345 for (i = 0; i < NUM_MACHINE_MODES; i++)
346 {
347 switch (GET_MODE_CLASS (i))
348 {
349 case MODE_INT:
350 case MODE_PARTIAL_INT:
351 case MODE_COMPLEX_INT:
352 if (GET_MODE_SIZE (i) <= 4)
353 m32r_mode_class[i] = 1 << (int) S_MODE;
354 else if (GET_MODE_SIZE (i) == 8)
355 m32r_mode_class[i] = 1 << (int) D_MODE;
356 else if (GET_MODE_SIZE (i) == 16)
357 m32r_mode_class[i] = 1 << (int) T_MODE;
358 else if (GET_MODE_SIZE (i) == 32)
359 m32r_mode_class[i] = 1 << (int) O_MODE;
360 else
361 m32r_mode_class[i] = 0;
362 break;
363 case MODE_FLOAT:
364 case MODE_COMPLEX_FLOAT:
365 if (GET_MODE_SIZE (i) <= 4)
366 m32r_mode_class[i] = 1 << (int) SF_MODE;
367 else if (GET_MODE_SIZE (i) == 8)
368 m32r_mode_class[i] = 1 << (int) DF_MODE;
369 else if (GET_MODE_SIZE (i) == 16)
370 m32r_mode_class[i] = 1 << (int) TF_MODE;
371 else if (GET_MODE_SIZE (i) == 32)
372 m32r_mode_class[i] = 1 << (int) OF_MODE;
373 else
374 m32r_mode_class[i] = 0;
375 break;
376 case MODE_CC:
377 m32r_mode_class[i] = 1 << (int) C_MODE;
378 break;
379 default:
380 m32r_mode_class[i] = 0;
381 break;
382 }
383 }
384
385 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
386 {
387 if (GPR_P (i))
388 m32r_regno_reg_class[i] = GENERAL_REGS;
389 else if (i == ARG_POINTER_REGNUM)
390 m32r_regno_reg_class[i] = GENERAL_REGS;
391 else
392 m32r_regno_reg_class[i] = NO_REGS;
393 }
394 }
395 \f
396 /* M32R specific attribute support.
397
398 interrupt - for interrupt functions
399
400 model - select code model used to access object
401
402 small: addresses use 24 bits, use bl to make calls
403 medium: addresses use 32 bits, use bl to make calls
404 large: addresses use 32 bits, use seth/add3/jl to make calls
405
406 Grep for MODEL in m32r.h for more info. */
407
408 static tree small_ident1;
409 static tree small_ident2;
410 static tree medium_ident1;
411 static tree medium_ident2;
412 static tree large_ident1;
413 static tree large_ident2;
414
415 static void
416 init_idents (void)
417 {
418 if (small_ident1 == 0)
419 {
420 small_ident1 = get_identifier ("small");
421 small_ident2 = get_identifier ("__small__");
422 medium_ident1 = get_identifier ("medium");
423 medium_ident2 = get_identifier ("__medium__");
424 large_ident1 = get_identifier ("large");
425 large_ident2 = get_identifier ("__large__");
426 }
427 }
428
429 /* Handle an "model" attribute; arguments as in
430 struct attribute_spec.handler. */
431 static tree
432 m32r_handle_model_attribute (tree *node ATTRIBUTE_UNUSED, tree name,
433 tree args, int flags ATTRIBUTE_UNUSED,
434 bool *no_add_attrs)
435 {
436 tree arg;
437
438 init_idents ();
439 arg = TREE_VALUE (args);
440
441 if (arg != small_ident1
442 && arg != small_ident2
443 && arg != medium_ident1
444 && arg != medium_ident2
445 && arg != large_ident1
446 && arg != large_ident2)
447 {
448 warning (OPT_Wattributes, "invalid argument of %qs attribute",
449 IDENTIFIER_POINTER (name));
450 *no_add_attrs = true;
451 }
452
453 return NULL_TREE;
454 }
455 \f
456 /* Encode section information of DECL, which is either a VAR_DECL,
457 FUNCTION_DECL, STRING_CST, CONSTRUCTOR, or ???.
458
459 For the M32R we want to record:
460
461 - whether the object lives in .sdata/.sbss.
462 - what code model should be used to access the object
463 */
464
465 static void
466 m32r_encode_section_info (tree decl, rtx rtl, int first)
467 {
468 int extra_flags = 0;
469 tree model_attr;
470 enum m32r_model model;
471
472 default_encode_section_info (decl, rtl, first);
473
474 if (!DECL_P (decl))
475 return;
476
477 model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
478 if (model_attr)
479 {
480 tree id;
481
482 init_idents ();
483
484 id = TREE_VALUE (TREE_VALUE (model_attr));
485
486 if (id == small_ident1 || id == small_ident2)
487 model = M32R_MODEL_SMALL;
488 else if (id == medium_ident1 || id == medium_ident2)
489 model = M32R_MODEL_MEDIUM;
490 else if (id == large_ident1 || id == large_ident2)
491 model = M32R_MODEL_LARGE;
492 else
493 gcc_unreachable (); /* shouldn't happen */
494 }
495 else
496 {
497 if (TARGET_MODEL_SMALL)
498 model = M32R_MODEL_SMALL;
499 else if (TARGET_MODEL_MEDIUM)
500 model = M32R_MODEL_MEDIUM;
501 else if (TARGET_MODEL_LARGE)
502 model = M32R_MODEL_LARGE;
503 else
504 gcc_unreachable (); /* shouldn't happen */
505 }
506 extra_flags |= model << SYMBOL_FLAG_MODEL_SHIFT;
507
508 if (extra_flags)
509 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
510 }
511
512 /* Only mark the object as being small data area addressable if
513 it hasn't been explicitly marked with a code model.
514
515 The user can explicitly put an object in the small data area with the
516 section attribute. If the object is in sdata/sbss and marked with a
517 code model do both [put the object in .sdata and mark it as being
518 addressed with a specific code model - don't mark it as being addressed
519 with an SDA reloc though]. This is ok and might be useful at times. If
520 the object doesn't fit the linker will give an error. */
521
522 static bool
523 m32r_in_small_data_p (const_tree decl)
524 {
525 const_tree section;
526
527 if (TREE_CODE (decl) != VAR_DECL)
528 return false;
529
530 if (lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
531 return false;
532
533 section = DECL_SECTION_NAME (decl);
534 if (section)
535 {
536 const char *const name = TREE_STRING_POINTER (section);
537 if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
538 return true;
539 }
540 else
541 {
542 if (! TREE_READONLY (decl) && ! TARGET_SDATA_NONE)
543 {
544 int size = int_size_in_bytes (TREE_TYPE (decl));
545
546 if (size > 0 && size <= g_switch_value)
547 return true;
548 }
549 }
550
551 return false;
552 }
553
554 /* Do anything needed before RTL is emitted for each function. */
555
556 void
557 m32r_init_expanders (void)
558 {
559 /* ??? At one point there was code here. The function is left in
560 to make it easy to experiment. */
561 }
562 \f
563 int
564 call_operand (rtx op, enum machine_mode mode)
565 {
566 if (!MEM_P (op))
567 return 0;
568 op = XEXP (op, 0);
569 return call_address_operand (op, mode);
570 }
571
572 /* Return 1 if OP is a reference to an object in .sdata/.sbss. */
573
574 int
575 small_data_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
576 {
577 if (! TARGET_SDATA_USE)
578 return 0;
579
580 if (GET_CODE (op) == SYMBOL_REF)
581 return SYMBOL_REF_SMALL_P (op);
582
583 if (GET_CODE (op) == CONST
584 && GET_CODE (XEXP (op, 0)) == PLUS
585 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
586 && satisfies_constraint_J (XEXP (XEXP (op, 0), 1)))
587 return SYMBOL_REF_SMALL_P (XEXP (XEXP (op, 0), 0));
588
589 return 0;
590 }
591
592 /* Return 1 if OP is a symbol that can use 24-bit addressing. */
593
594 int
595 addr24_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
596 {
597 rtx sym;
598
599 if (flag_pic)
600 return 0;
601
602 if (GET_CODE (op) == LABEL_REF)
603 return TARGET_ADDR24;
604
605 if (GET_CODE (op) == SYMBOL_REF)
606 sym = op;
607 else if (GET_CODE (op) == CONST
608 && GET_CODE (XEXP (op, 0)) == PLUS
609 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
610 && satisfies_constraint_M (XEXP (XEXP (op, 0), 1)))
611 sym = XEXP (XEXP (op, 0), 0);
612 else
613 return 0;
614
615 if (SYMBOL_REF_MODEL (sym) == M32R_MODEL_SMALL)
616 return 1;
617
618 if (TARGET_ADDR24
619 && (CONSTANT_POOL_ADDRESS_P (sym)
620 || LIT_NAME_P (XSTR (sym, 0))))
621 return 1;
622
623 return 0;
624 }
625
626 /* Return 1 if OP is a symbol that needs 32-bit addressing. */
627
628 int
629 addr32_operand (rtx op, enum machine_mode mode)
630 {
631 rtx sym;
632
633 if (GET_CODE (op) == LABEL_REF)
634 return TARGET_ADDR32;
635
636 if (GET_CODE (op) == SYMBOL_REF)
637 sym = op;
638 else if (GET_CODE (op) == CONST
639 && GET_CODE (XEXP (op, 0)) == PLUS
640 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
641 && CONST_INT_P (XEXP (XEXP (op, 0), 1))
642 && ! flag_pic)
643 sym = XEXP (XEXP (op, 0), 0);
644 else
645 return 0;
646
647 return (! addr24_operand (sym, mode)
648 && ! small_data_operand (sym, mode));
649 }
650
651 /* Return 1 if OP is a function that can be called with the `bl' insn. */
652
653 int
654 call26_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
655 {
656 if (flag_pic)
657 return 1;
658
659 if (GET_CODE (op) == SYMBOL_REF)
660 return SYMBOL_REF_MODEL (op) != M32R_MODEL_LARGE;
661
662 return TARGET_CALL26;
663 }
664
665 /* Return 1 if OP is a DImode const we want to handle inline.
666 This must match the code in the movdi pattern.
667 It is used by the 'G' CONST_DOUBLE_OK_FOR_LETTER. */
668
669 int
670 easy_di_const (rtx op)
671 {
672 rtx high_rtx, low_rtx;
673 HOST_WIDE_INT high, low;
674
675 split_double (op, &high_rtx, &low_rtx);
676 high = INTVAL (high_rtx);
677 low = INTVAL (low_rtx);
678 /* Pick constants loadable with 2 16-bit `ldi' insns. */
679 if (high >= -128 && high <= 127
680 && low >= -128 && low <= 127)
681 return 1;
682 return 0;
683 }
684
685 /* Return 1 if OP is a DFmode const we want to handle inline.
686 This must match the code in the movdf pattern.
687 It is used by the 'H' CONST_DOUBLE_OK_FOR_LETTER. */
688
689 int
690 easy_df_const (rtx op)
691 {
692 REAL_VALUE_TYPE r;
693 long l[2];
694
695 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
696 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
697 if (l[0] == 0 && l[1] == 0)
698 return 1;
699 if ((l[0] & 0xffff) == 0 && l[1] == 0)
700 return 1;
701 return 0;
702 }
703
704 /* Return 1 if OP is (mem (reg ...)).
705 This is used in insn length calcs. */
706
707 int
708 memreg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
709 {
710 return MEM_P (op) && REG_P (XEXP (op, 0));
711 }
712
713 /* Return nonzero if TYPE must be passed by indirect reference. */
714
715 static bool
716 m32r_pass_by_reference (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
717 enum machine_mode mode, const_tree type,
718 bool named ATTRIBUTE_UNUSED)
719 {
720 int size;
721
722 if (type)
723 size = int_size_in_bytes (type);
724 else
725 size = GET_MODE_SIZE (mode);
726
727 return (size < 0 || size > 8);
728 }
729 \f
730 /* Comparisons. */
731
732 /* X and Y are two things to compare using CODE. Emit the compare insn and
733 return the rtx for compare [arg0 of the if_then_else].
734 If need_compare is true then the comparison insn must be generated, rather
735 than being subsumed into the following branch instruction. */
736
737 rtx
738 gen_compare (enum rtx_code code, rtx x, rtx y, int need_compare)
739 {
740 enum rtx_code compare_code;
741 enum rtx_code branch_code;
742 rtx cc_reg = gen_rtx_REG (CCmode, CARRY_REGNUM);
743 int must_swap = 0;
744
745 switch (code)
746 {
747 case EQ: compare_code = EQ; branch_code = NE; break;
748 case NE: compare_code = EQ; branch_code = EQ; break;
749 case LT: compare_code = LT; branch_code = NE; break;
750 case LE: compare_code = LT; branch_code = EQ; must_swap = 1; break;
751 case GT: compare_code = LT; branch_code = NE; must_swap = 1; break;
752 case GE: compare_code = LT; branch_code = EQ; break;
753 case LTU: compare_code = LTU; branch_code = NE; break;
754 case LEU: compare_code = LTU; branch_code = EQ; must_swap = 1; break;
755 case GTU: compare_code = LTU; branch_code = NE; must_swap = 1; break;
756 case GEU: compare_code = LTU; branch_code = EQ; break;
757
758 default:
759 gcc_unreachable ();
760 }
761
762 if (need_compare)
763 {
764 switch (compare_code)
765 {
766 case EQ:
767 if (satisfies_constraint_P (y) /* Reg equal to small const. */
768 && y != const0_rtx)
769 {
770 rtx tmp = gen_reg_rtx (SImode);
771
772 emit_insn (gen_addsi3 (tmp, x, GEN_INT (-INTVAL (y))));
773 x = tmp;
774 y = const0_rtx;
775 }
776 else if (CONSTANT_P (y)) /* Reg equal to const. */
777 {
778 rtx tmp = force_reg (GET_MODE (x), y);
779 y = tmp;
780 }
781
782 if (register_operand (y, SImode) /* Reg equal to reg. */
783 || y == const0_rtx) /* Reg equal to zero. */
784 {
785 emit_insn (gen_cmp_eqsi_insn (x, y));
786
787 return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
788 }
789 break;
790
791 case LT:
792 if (register_operand (y, SImode)
793 || satisfies_constraint_P (y))
794 {
795 rtx tmp = gen_reg_rtx (SImode); /* Reg compared to reg. */
796
797 switch (code)
798 {
799 case LT:
800 emit_insn (gen_cmp_ltsi_insn (x, y));
801 code = EQ;
802 break;
803 case LE:
804 if (y == const0_rtx)
805 tmp = const1_rtx;
806 else
807 emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
808 emit_insn (gen_cmp_ltsi_insn (x, tmp));
809 code = EQ;
810 break;
811 case GT:
812 if (CONST_INT_P (y))
813 tmp = gen_rtx_PLUS (SImode, y, const1_rtx);
814 else
815 emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
816 emit_insn (gen_cmp_ltsi_insn (x, tmp));
817 code = NE;
818 break;
819 case GE:
820 emit_insn (gen_cmp_ltsi_insn (x, y));
821 code = NE;
822 break;
823 default:
824 gcc_unreachable ();
825 }
826
827 return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
828 }
829 break;
830
831 case LTU:
832 if (register_operand (y, SImode)
833 || satisfies_constraint_P (y))
834 {
835 rtx tmp = gen_reg_rtx (SImode); /* Reg (unsigned) compared to reg. */
836
837 switch (code)
838 {
839 case LTU:
840 emit_insn (gen_cmp_ltusi_insn (x, y));
841 code = EQ;
842 break;
843 case LEU:
844 if (y == const0_rtx)
845 tmp = const1_rtx;
846 else
847 emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
848 emit_insn (gen_cmp_ltusi_insn (x, tmp));
849 code = EQ;
850 break;
851 case GTU:
852 if (CONST_INT_P (y))
853 tmp = gen_rtx_PLUS (SImode, y, const1_rtx);
854 else
855 emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
856 emit_insn (gen_cmp_ltusi_insn (x, tmp));
857 code = NE;
858 break;
859 case GEU:
860 emit_insn (gen_cmp_ltusi_insn (x, y));
861 code = NE;
862 break;
863 default:
864 gcc_unreachable ();
865 }
866
867 return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
868 }
869 break;
870
871 default:
872 gcc_unreachable ();
873 }
874 }
875 else
876 {
877 /* Reg/reg equal comparison. */
878 if (compare_code == EQ
879 && register_operand (y, SImode))
880 return gen_rtx_fmt_ee (code, CCmode, x, y);
881
882 /* Reg/zero signed comparison. */
883 if ((compare_code == EQ || compare_code == LT)
884 && y == const0_rtx)
885 return gen_rtx_fmt_ee (code, CCmode, x, y);
886
887 /* Reg/smallconst equal comparison. */
888 if (compare_code == EQ
889 && satisfies_constraint_P (y))
890 {
891 rtx tmp = gen_reg_rtx (SImode);
892
893 emit_insn (gen_addsi3 (tmp, x, GEN_INT (-INTVAL (y))));
894 return gen_rtx_fmt_ee (code, CCmode, tmp, const0_rtx);
895 }
896
897 /* Reg/const equal comparison. */
898 if (compare_code == EQ
899 && CONSTANT_P (y))
900 {
901 rtx tmp = force_reg (GET_MODE (x), y);
902
903 return gen_rtx_fmt_ee (code, CCmode, x, tmp);
904 }
905 }
906
907 if (CONSTANT_P (y))
908 {
909 if (must_swap)
910 y = force_reg (GET_MODE (x), y);
911 else
912 {
913 int ok_const = reg_or_int16_operand (y, GET_MODE (y));
914
915 if (! ok_const)
916 y = force_reg (GET_MODE (x), y);
917 }
918 }
919
920 switch (compare_code)
921 {
922 case EQ :
923 emit_insn (gen_cmp_eqsi_insn (must_swap ? y : x, must_swap ? x : y));
924 break;
925 case LT :
926 emit_insn (gen_cmp_ltsi_insn (must_swap ? y : x, must_swap ? x : y));
927 break;
928 case LTU :
929 emit_insn (gen_cmp_ltusi_insn (must_swap ? y : x, must_swap ? x : y));
930 break;
931
932 default:
933 gcc_unreachable ();
934 }
935
936 return gen_rtx_fmt_ee (branch_code, VOIDmode, cc_reg, CONST0_RTX (CCmode));
937 }
938
939 bool
940 gen_cond_store (enum rtx_code code, rtx op0, rtx op1, rtx op2)
941 {
942 enum machine_mode mode = GET_MODE (op0);
943
944 gcc_assert (mode == SImode);
945 switch (code)
946 {
947 case EQ:
948 if (!register_operand (op1, mode))
949 op1 = force_reg (mode, op1);
950
951 if (TARGET_M32RX || TARGET_M32R2)
952 {
953 if (!reg_or_zero_operand (op2, mode))
954 op2 = force_reg (mode, op2);
955
956 emit_insn (gen_seq_insn_m32rx (op0, op1, op2));
957 return true;
958 }
959 if (CONST_INT_P (op2) && INTVAL (op2) == 0)
960 {
961 emit_insn (gen_seq_zero_insn (op0, op1));
962 return true;
963 }
964
965 if (!reg_or_eq_int16_operand (op2, mode))
966 op2 = force_reg (mode, op2);
967
968 emit_insn (gen_seq_insn (op0, op1, op2));
969 return true;
970
971 case NE:
972 if (!CONST_INT_P (op2)
973 || (INTVAL (op2) != 0 && satisfies_constraint_K (op2)))
974 {
975 rtx reg;
976
977 if (reload_completed || reload_in_progress)
978 return false;
979
980 reg = gen_reg_rtx (SImode);
981 emit_insn (gen_xorsi3 (reg, op1, op2));
982 op1 = reg;
983
984 if (!register_operand (op1, mode))
985 op1 = force_reg (mode, op1);
986
987 emit_insn (gen_sne_zero_insn (op0, op1));
988 return true;
989 }
990 return false;
991
992 case LT:
993 case GT:
994 if (code == GT)
995 {
996 rtx tmp = op2;
997 op2 = op1;
998 op1 = tmp;
999 code = LT;
1000 }
1001
1002 if (!register_operand (op1, mode))
1003 op1 = force_reg (mode, op1);
1004
1005 if (!reg_or_int16_operand (op2, mode))
1006 op2 = force_reg (mode, op2);
1007
1008 emit_insn (gen_slt_insn (op0, op1, op2));
1009 return true;
1010
1011 case LTU:
1012 case GTU:
1013 if (code == GTU)
1014 {
1015 rtx tmp = op2;
1016 op2 = op1;
1017 op1 = tmp;
1018 code = LTU;
1019 }
1020
1021 if (!register_operand (op1, mode))
1022 op1 = force_reg (mode, op1);
1023
1024 if (!reg_or_int16_operand (op2, mode))
1025 op2 = force_reg (mode, op2);
1026
1027 emit_insn (gen_sltu_insn (op0, op1, op2));
1028 return true;
1029
1030 case GE:
1031 case GEU:
1032 if (!register_operand (op1, mode))
1033 op1 = force_reg (mode, op1);
1034
1035 if (!reg_or_int16_operand (op2, mode))
1036 op2 = force_reg (mode, op2);
1037
1038 if (code == GE)
1039 emit_insn (gen_sge_insn (op0, op1, op2));
1040 else
1041 emit_insn (gen_sgeu_insn (op0, op1, op2));
1042 return true;
1043
1044 case LE:
1045 case LEU:
1046 if (!register_operand (op1, mode))
1047 op1 = force_reg (mode, op1);
1048
1049 if (CONST_INT_P (op2))
1050 {
1051 HOST_WIDE_INT value = INTVAL (op2);
1052 if (value >= 2147483647)
1053 {
1054 emit_move_insn (op0, const1_rtx);
1055 return true;
1056 }
1057
1058 op2 = GEN_INT (value + 1);
1059 if (value < -32768 || value >= 32767)
1060 op2 = force_reg (mode, op2);
1061
1062 if (code == LEU)
1063 emit_insn (gen_sltu_insn (op0, op1, op2));
1064 else
1065 emit_insn (gen_slt_insn (op0, op1, op2));
1066 return true;
1067 }
1068
1069 if (!register_operand (op2, mode))
1070 op2 = force_reg (mode, op2);
1071
1072 if (code == LEU)
1073 emit_insn (gen_sleu_insn (op0, op1, op2));
1074 else
1075 emit_insn (gen_sle_insn (op0, op1, op2));
1076 return true;
1077
1078 default:
1079 gcc_unreachable ();
1080 }
1081 }
1082
1083 \f
1084 /* Split a 2 word move (DI or DF) into component parts. */
1085
1086 rtx
1087 gen_split_move_double (rtx operands[])
1088 {
1089 enum machine_mode mode = GET_MODE (operands[0]);
1090 rtx dest = operands[0];
1091 rtx src = operands[1];
1092 rtx val;
1093
1094 /* We might have (SUBREG (MEM)) here, so just get rid of the
1095 subregs to make this code simpler. It is safe to call
1096 alter_subreg any time after reload. */
1097 if (GET_CODE (dest) == SUBREG)
1098 alter_subreg (&dest);
1099 if (GET_CODE (src) == SUBREG)
1100 alter_subreg (&src);
1101
1102 start_sequence ();
1103 if (REG_P (dest))
1104 {
1105 int dregno = REGNO (dest);
1106
1107 /* Reg = reg. */
1108 if (REG_P (src))
1109 {
1110 int sregno = REGNO (src);
1111
1112 int reverse = (dregno == sregno + 1);
1113
1114 /* We normally copy the low-numbered register first. However, if
1115 the first register operand 0 is the same as the second register of
1116 operand 1, we must copy in the opposite order. */
1117 emit_insn (gen_rtx_SET (VOIDmode,
1118 operand_subword (dest, reverse, TRUE, mode),
1119 operand_subword (src, reverse, TRUE, mode)));
1120
1121 emit_insn (gen_rtx_SET (VOIDmode,
1122 operand_subword (dest, !reverse, TRUE, mode),
1123 operand_subword (src, !reverse, TRUE, mode)));
1124 }
1125
1126 /* Reg = constant. */
1127 else if (CONST_INT_P (src) || GET_CODE (src) == CONST_DOUBLE)
1128 {
1129 rtx words[2];
1130 split_double (src, &words[0], &words[1]);
1131 emit_insn (gen_rtx_SET (VOIDmode,
1132 operand_subword (dest, 0, TRUE, mode),
1133 words[0]));
1134
1135 emit_insn (gen_rtx_SET (VOIDmode,
1136 operand_subword (dest, 1, TRUE, mode),
1137 words[1]));
1138 }
1139
1140 /* Reg = mem. */
1141 else if (MEM_P (src))
1142 {
1143 /* If the high-address word is used in the address, we must load it
1144 last. Otherwise, load it first. */
1145 int reverse
1146 = (refers_to_regno_p (dregno, dregno + 1, XEXP (src, 0), 0) != 0);
1147
1148 /* We used to optimize loads from single registers as
1149
1150 ld r1,r3+; ld r2,r3
1151
1152 if r3 were not used subsequently. However, the REG_NOTES aren't
1153 propagated correctly by the reload phase, and it can cause bad
1154 code to be generated. We could still try:
1155
1156 ld r1,r3+; ld r2,r3; addi r3,-4
1157
1158 which saves 2 bytes and doesn't force longword alignment. */
1159 emit_insn (gen_rtx_SET (VOIDmode,
1160 operand_subword (dest, reverse, TRUE, mode),
1161 adjust_address (src, SImode,
1162 reverse * UNITS_PER_WORD)));
1163
1164 emit_insn (gen_rtx_SET (VOIDmode,
1165 operand_subword (dest, !reverse, TRUE, mode),
1166 adjust_address (src, SImode,
1167 !reverse * UNITS_PER_WORD)));
1168 }
1169 else
1170 gcc_unreachable ();
1171 }
1172
1173 /* Mem = reg. */
1174 /* We used to optimize loads from single registers as
1175
1176 st r1,r3; st r2,+r3
1177
1178 if r3 were not used subsequently. However, the REG_NOTES aren't
1179 propagated correctly by the reload phase, and it can cause bad
1180 code to be generated. We could still try:
1181
1182 st r1,r3; st r2,+r3; addi r3,-4
1183
1184 which saves 2 bytes and doesn't force longword alignment. */
1185 else if (MEM_P (dest) && REG_P (src))
1186 {
1187 emit_insn (gen_rtx_SET (VOIDmode,
1188 adjust_address (dest, SImode, 0),
1189 operand_subword (src, 0, TRUE, mode)));
1190
1191 emit_insn (gen_rtx_SET (VOIDmode,
1192 adjust_address (dest, SImode, UNITS_PER_WORD),
1193 operand_subword (src, 1, TRUE, mode)));
1194 }
1195
1196 else
1197 gcc_unreachable ();
1198
1199 val = get_insns ();
1200 end_sequence ();
1201 return val;
1202 }
1203
1204 \f
1205 static int
1206 m32r_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1207 tree type, bool named ATTRIBUTE_UNUSED)
1208 {
1209 int words;
1210 unsigned int size =
1211 (((mode == BLKmode && type)
1212 ? (unsigned int) int_size_in_bytes (type)
1213 : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
1214 / UNITS_PER_WORD;
1215
1216 if (*cum >= M32R_MAX_PARM_REGS)
1217 words = 0;
1218 else if (*cum + size > M32R_MAX_PARM_REGS)
1219 words = (*cum + size) - M32R_MAX_PARM_REGS;
1220 else
1221 words = 0;
1222
1223 return words * UNITS_PER_WORD;
1224 }
1225
1226 /* The ROUND_ADVANCE* macros are local to this file. */
1227 /* Round SIZE up to a word boundary. */
1228 #define ROUND_ADVANCE(SIZE) \
1229 (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1230
1231 /* Round arg MODE/TYPE up to the next word boundary. */
1232 #define ROUND_ADVANCE_ARG(MODE, TYPE) \
1233 ((MODE) == BLKmode \
1234 ? ROUND_ADVANCE ((unsigned int) int_size_in_bytes (TYPE)) \
1235 : ROUND_ADVANCE ((unsigned int) GET_MODE_SIZE (MODE)))
1236
1237 /* Round CUM up to the necessary point for argument MODE/TYPE. */
1238 #define ROUND_ADVANCE_CUM(CUM, MODE, TYPE) (CUM)
1239
1240 /* Return boolean indicating arg of type TYPE and mode MODE will be passed in
1241 a reg. This includes arguments that have to be passed by reference as the
1242 pointer to them is passed in a reg if one is available (and that is what
1243 we're given).
1244 This macro is only used in this file. */
1245 #define PASS_IN_REG_P(CUM, MODE, TYPE) \
1246 (ROUND_ADVANCE_CUM ((CUM), (MODE), (TYPE)) < M32R_MAX_PARM_REGS)
1247
1248 /* Determine where to put an argument to a function.
1249 Value is zero to push the argument on the stack,
1250 or a hard register in which to store the argument.
1251
1252 MODE is the argument's machine mode.
1253 TYPE is the data type of the argument (as a tree).
1254 This is null for libcalls where that information may
1255 not be available.
1256 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1257 the preceding args and about the function being called.
1258 NAMED is nonzero if this argument is a named parameter
1259 (otherwise it is an extra parameter matching an ellipsis). */
1260 /* On the M32R the first M32R_MAX_PARM_REGS args are normally in registers
1261 and the rest are pushed. */
1262
1263 static rtx
1264 m32r_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1265 const_tree type ATTRIBUTE_UNUSED,
1266 bool named ATTRIBUTE_UNUSED)
1267 {
1268 return (PASS_IN_REG_P (*cum, mode, type)
1269 ? gen_rtx_REG (mode, ROUND_ADVANCE_CUM (*cum, mode, type))
1270 : NULL_RTX);
1271 }
1272
1273 /* Update the data in CUM to advance over an argument
1274 of mode MODE and data type TYPE.
1275 (TYPE is null for libcalls where that information may not be available.) */
1276
1277 static void
1278 m32r_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1279 const_tree type, bool named ATTRIBUTE_UNUSED)
1280 {
1281 *cum = (ROUND_ADVANCE_CUM (*cum, mode, type)
1282 + ROUND_ADVANCE_ARG (mode, type));
1283 }
1284
1285 /* Worker function for TARGET_RETURN_IN_MEMORY. */
1286
1287 static bool
1288 m32r_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1289 {
1290 return m32r_pass_by_reference (NULL, TYPE_MODE (type), type, false);
1291 }
1292
1293 /* Worker function for TARGET_FUNCTION_VALUE. */
1294
1295 static rtx
1296 m32r_function_value (const_tree valtype,
1297 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1298 bool outgoing ATTRIBUTE_UNUSED)
1299 {
1300 return gen_rtx_REG (TYPE_MODE (valtype), 0);
1301 }
1302
1303 /* Worker function for TARGET_LIBCALL_VALUE. */
1304
1305 static rtx
1306 m32r_libcall_value (enum machine_mode mode,
1307 const_rtx fun ATTRIBUTE_UNUSED)
1308 {
1309 return gen_rtx_REG (mode, 0);
1310 }
1311
1312 /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P.
1313
1314 ??? What about r1 in DI/DF values. */
1315
1316 static bool
1317 m32r_function_value_regno_p (const unsigned int regno)
1318 {
1319 return (regno == 0);
1320 }
1321
1322 /* Do any needed setup for a variadic function. For the M32R, we must
1323 create a register parameter block, and then copy any anonymous arguments
1324 in registers to memory.
1325
1326 CUM has not been updated for the last named argument which has type TYPE
1327 and mode MODE, and we rely on this fact. */
1328
1329 static void
1330 m32r_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1331 tree type, int *pretend_size, int no_rtl)
1332 {
1333 int first_anon_arg;
1334
1335 if (no_rtl)
1336 return;
1337
1338 /* All BLKmode values are passed by reference. */
1339 gcc_assert (mode != BLKmode);
1340
1341 first_anon_arg = (ROUND_ADVANCE_CUM (*cum, mode, type)
1342 + ROUND_ADVANCE_ARG (mode, type));
1343
1344 if (first_anon_arg < M32R_MAX_PARM_REGS)
1345 {
1346 /* Note that first_reg_offset < M32R_MAX_PARM_REGS. */
1347 int first_reg_offset = first_anon_arg;
1348 /* Size in words to "pretend" allocate. */
1349 int size = M32R_MAX_PARM_REGS - first_reg_offset;
1350 rtx regblock;
1351
1352 regblock = gen_frame_mem (BLKmode,
1353 plus_constant (arg_pointer_rtx,
1354 FIRST_PARM_OFFSET (0)));
1355 set_mem_alias_set (regblock, get_varargs_alias_set ());
1356 move_block_from_reg (first_reg_offset, regblock, size);
1357
1358 *pretend_size = (size * UNITS_PER_WORD);
1359 }
1360 }
1361
1362 \f
1363 /* Return true if INSN is real instruction bearing insn. */
1364
1365 static int
1366 m32r_is_insn (rtx insn)
1367 {
1368 return (NONDEBUG_INSN_P (insn)
1369 && GET_CODE (PATTERN (insn)) != USE
1370 && GET_CODE (PATTERN (insn)) != CLOBBER
1371 && GET_CODE (PATTERN (insn)) != ADDR_VEC);
1372 }
1373
1374 /* Increase the priority of long instructions so that the
1375 short instructions are scheduled ahead of the long ones. */
1376
1377 static int
1378 m32r_adjust_priority (rtx insn, int priority)
1379 {
1380 if (m32r_is_insn (insn)
1381 && get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1382 priority <<= 3;
1383
1384 return priority;
1385 }
1386
1387 \f
1388 /* Indicate how many instructions can be issued at the same time.
1389 This is sort of a lie. The m32r can issue only 1 long insn at
1390 once, but it can issue 2 short insns. The default therefore is
1391 set at 2, but this can be overridden by the command line option
1392 -missue-rate=1. */
1393
1394 static int
1395 m32r_issue_rate (void)
1396 {
1397 return ((TARGET_LOW_ISSUE_RATE) ? 1 : 2);
1398 }
1399 \f
1400 /* Cost functions. */
1401
1402 /* Implement TARGET_HANDLE_OPTION.
1403
1404 Memory is 3 times as expensive as registers.
1405 ??? Is that the right way to look at it? */
1406
1407 static int
1408 m32r_memory_move_cost (enum machine_mode mode,
1409 reg_class_t rclass ATTRIBUTE_UNUSED,
1410 bool in ATTRIBUTE_UNUSED)
1411 {
1412 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
1413 return 6;
1414 else
1415 return 12;
1416 }
1417
1418 static bool
1419 m32r_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, int *total,
1420 bool speed ATTRIBUTE_UNUSED)
1421 {
1422 switch (code)
1423 {
1424 /* Small integers are as cheap as registers. 4 byte values can be
1425 fetched as immediate constants - let's give that the cost of an
1426 extra insn. */
1427 case CONST_INT:
1428 if (INT16_P (INTVAL (x)))
1429 {
1430 *total = 0;
1431 return true;
1432 }
1433 /* FALLTHRU */
1434
1435 case CONST:
1436 case LABEL_REF:
1437 case SYMBOL_REF:
1438 *total = COSTS_N_INSNS (1);
1439 return true;
1440
1441 case CONST_DOUBLE:
1442 {
1443 rtx high, low;
1444
1445 split_double (x, &high, &low);
1446 *total = COSTS_N_INSNS (!INT16_P (INTVAL (high))
1447 + !INT16_P (INTVAL (low)));
1448 return true;
1449 }
1450
1451 case MULT:
1452 *total = COSTS_N_INSNS (3);
1453 return true;
1454
1455 case DIV:
1456 case UDIV:
1457 case MOD:
1458 case UMOD:
1459 *total = COSTS_N_INSNS (10);
1460 return true;
1461
1462 default:
1463 return false;
1464 }
1465 }
1466 \f
1467 /* Type of function DECL.
1468
1469 The result is cached. To reset the cache at the end of a function,
1470 call with DECL = NULL_TREE. */
1471
1472 enum m32r_function_type
1473 m32r_compute_function_type (tree decl)
1474 {
1475 /* Cached value. */
1476 static enum m32r_function_type fn_type = M32R_FUNCTION_UNKNOWN;
1477 /* Last function we were called for. */
1478 static tree last_fn = NULL_TREE;
1479
1480 /* Resetting the cached value? */
1481 if (decl == NULL_TREE)
1482 {
1483 fn_type = M32R_FUNCTION_UNKNOWN;
1484 last_fn = NULL_TREE;
1485 return fn_type;
1486 }
1487
1488 if (decl == last_fn && fn_type != M32R_FUNCTION_UNKNOWN)
1489 return fn_type;
1490
1491 /* Compute function type. */
1492 fn_type = (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE
1493 ? M32R_FUNCTION_INTERRUPT
1494 : M32R_FUNCTION_NORMAL);
1495
1496 last_fn = decl;
1497 return fn_type;
1498 }
1499 \f/* Function prologue/epilogue handlers. */
1500
1501 /* M32R stack frames look like:
1502
1503 Before call After call
1504 +-----------------------+ +-----------------------+
1505 | | | |
1506 high | local variables, | | local variables, |
1507 mem | reg save area, etc. | | reg save area, etc. |
1508 | | | |
1509 +-----------------------+ +-----------------------+
1510 | | | |
1511 | arguments on stack. | | arguments on stack. |
1512 | | | |
1513 SP+0->+-----------------------+ +-----------------------+
1514 | reg parm save area, |
1515 | only created for |
1516 | variable argument |
1517 | functions |
1518 +-----------------------+
1519 | previous frame ptr |
1520 +-----------------------+
1521 | |
1522 | register save area |
1523 | |
1524 +-----------------------+
1525 | return address |
1526 +-----------------------+
1527 | |
1528 | local variables |
1529 | |
1530 +-----------------------+
1531 | |
1532 | alloca allocations |
1533 | |
1534 +-----------------------+
1535 | |
1536 low | arguments on stack |
1537 memory | |
1538 SP+0->+-----------------------+
1539
1540 Notes:
1541 1) The "reg parm save area" does not exist for non variable argument fns.
1542 2) The "reg parm save area" can be eliminated completely if we saved regs
1543 containing anonymous args separately but that complicates things too
1544 much (so it's not done).
1545 3) The return address is saved after the register save area so as to have as
1546 many insns as possible between the restoration of `lr' and the `jmp lr'. */
1547
1548 /* Structure to be filled in by m32r_compute_frame_size with register
1549 save masks, and offsets for the current function. */
1550 struct m32r_frame_info
1551 {
1552 unsigned int total_size; /* # bytes that the entire frame takes up. */
1553 unsigned int extra_size; /* # bytes of extra stuff. */
1554 unsigned int pretend_size; /* # bytes we push and pretend caller did. */
1555 unsigned int args_size; /* # bytes that outgoing arguments take up. */
1556 unsigned int reg_size; /* # bytes needed to store regs. */
1557 unsigned int var_size; /* # bytes that variables take up. */
1558 unsigned int gmask; /* Mask of saved gp registers. */
1559 unsigned int save_fp; /* Nonzero if fp must be saved. */
1560 unsigned int save_lr; /* Nonzero if lr (return addr) must be saved. */
1561 int initialized; /* Nonzero if frame size already calculated. */
1562 };
1563
1564 /* Current frame information calculated by m32r_compute_frame_size. */
1565 static struct m32r_frame_info current_frame_info;
1566
1567 /* Zero structure to initialize current_frame_info. */
1568 static struct m32r_frame_info zero_frame_info;
1569
1570 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
1571 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
1572
1573 /* Tell prologue and epilogue if register REGNO should be saved / restored.
1574 The return address and frame pointer are treated separately.
1575 Don't consider them here. */
1576 #define MUST_SAVE_REGISTER(regno, interrupt_p) \
1577 ((regno) != RETURN_ADDR_REGNUM && (regno) != FRAME_POINTER_REGNUM \
1578 && (df_regs_ever_live_p (regno) && (!call_really_used_regs[regno] || interrupt_p)))
1579
1580 #define MUST_SAVE_FRAME_POINTER (df_regs_ever_live_p (FRAME_POINTER_REGNUM))
1581 #define MUST_SAVE_RETURN_ADDR (df_regs_ever_live_p (RETURN_ADDR_REGNUM) || crtl->profile)
1582
1583 #define SHORT_INSN_SIZE 2 /* Size of small instructions. */
1584 #define LONG_INSN_SIZE 4 /* Size of long instructions. */
1585
1586 /* Return the bytes needed to compute the frame pointer from the current
1587 stack pointer.
1588
1589 SIZE is the size needed for local variables. */
1590
1591 unsigned int
1592 m32r_compute_frame_size (int size) /* # of var. bytes allocated. */
1593 {
1594 unsigned int regno;
1595 unsigned int total_size, var_size, args_size, pretend_size, extra_size;
1596 unsigned int reg_size;
1597 unsigned int gmask;
1598 enum m32r_function_type fn_type;
1599 int interrupt_p;
1600 int pic_reg_used = flag_pic && (crtl->uses_pic_offset_table
1601 | crtl->profile);
1602
1603 var_size = M32R_STACK_ALIGN (size);
1604 args_size = M32R_STACK_ALIGN (crtl->outgoing_args_size);
1605 pretend_size = crtl->args.pretend_args_size;
1606 extra_size = FIRST_PARM_OFFSET (0);
1607 total_size = extra_size + pretend_size + args_size + var_size;
1608 reg_size = 0;
1609 gmask = 0;
1610
1611 /* See if this is an interrupt handler. Call used registers must be saved
1612 for them too. */
1613 fn_type = m32r_compute_function_type (current_function_decl);
1614 interrupt_p = M32R_INTERRUPT_P (fn_type);
1615
1616 /* Calculate space needed for registers. */
1617 for (regno = 0; regno < M32R_MAX_INT_REGS; regno++)
1618 {
1619 if (MUST_SAVE_REGISTER (regno, interrupt_p)
1620 || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1621 {
1622 reg_size += UNITS_PER_WORD;
1623 gmask |= 1 << regno;
1624 }
1625 }
1626
1627 current_frame_info.save_fp = MUST_SAVE_FRAME_POINTER;
1628 current_frame_info.save_lr = MUST_SAVE_RETURN_ADDR || pic_reg_used;
1629
1630 reg_size += ((current_frame_info.save_fp + current_frame_info.save_lr)
1631 * UNITS_PER_WORD);
1632 total_size += reg_size;
1633
1634 /* ??? Not sure this is necessary, and I don't think the epilogue
1635 handler will do the right thing if this changes total_size. */
1636 total_size = M32R_STACK_ALIGN (total_size);
1637
1638 /* frame_size = total_size - (pretend_size + reg_size); */
1639
1640 /* Save computed information. */
1641 current_frame_info.total_size = total_size;
1642 current_frame_info.extra_size = extra_size;
1643 current_frame_info.pretend_size = pretend_size;
1644 current_frame_info.var_size = var_size;
1645 current_frame_info.args_size = args_size;
1646 current_frame_info.reg_size = reg_size;
1647 current_frame_info.gmask = gmask;
1648 current_frame_info.initialized = reload_completed;
1649
1650 /* Ok, we're done. */
1651 return total_size;
1652 }
1653
1654 /* Worker function for TARGET_CAN_ELIMINATE. */
1655
1656 bool
1657 m32r_can_eliminate (const int from, const int to)
1658 {
1659 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
1660 ? ! frame_pointer_needed
1661 : true);
1662 }
1663
1664 \f
1665 /* The table we use to reference PIC data. */
1666 static rtx global_offset_table;
1667
1668 static void
1669 m32r_reload_lr (rtx sp, int size)
1670 {
1671 rtx lr = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
1672
1673 if (size == 0)
1674 emit_insn (gen_movsi (lr, gen_frame_mem (Pmode, sp)));
1675 else if (size < 32768)
1676 emit_insn (gen_movsi (lr, gen_frame_mem (Pmode,
1677 gen_rtx_PLUS (Pmode, sp,
1678 GEN_INT (size)))));
1679 else
1680 {
1681 rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1682
1683 emit_insn (gen_movsi (tmp, GEN_INT (size)));
1684 emit_insn (gen_addsi3 (tmp, tmp, sp));
1685 emit_insn (gen_movsi (lr, gen_frame_mem (Pmode, tmp)));
1686 }
1687
1688 emit_use (lr);
1689 }
1690
1691 void
1692 m32r_load_pic_register (void)
1693 {
1694 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1695 emit_insn (gen_get_pc (pic_offset_table_rtx, global_offset_table,
1696 GEN_INT (TARGET_MODEL_SMALL)));
1697
1698 /* Need to emit this whether or not we obey regdecls,
1699 since setjmp/longjmp can cause life info to screw up. */
1700 emit_use (pic_offset_table_rtx);
1701 }
1702
1703 /* Expand the m32r prologue as a series of insns. */
1704
1705 void
1706 m32r_expand_prologue (void)
1707 {
1708 int regno;
1709 int frame_size;
1710 unsigned int gmask;
1711 int pic_reg_used = flag_pic && (crtl->uses_pic_offset_table
1712 | crtl->profile);
1713
1714 if (! current_frame_info.initialized)
1715 m32r_compute_frame_size (get_frame_size ());
1716
1717 gmask = current_frame_info.gmask;
1718
1719 /* These cases shouldn't happen. Catch them now. */
1720 gcc_assert (current_frame_info.total_size || !gmask);
1721
1722 /* Allocate space for register arguments if this is a variadic function. */
1723 if (current_frame_info.pretend_size != 0)
1724 {
1725 /* Use a HOST_WIDE_INT temporary, since negating an unsigned int gives
1726 the wrong result on a 64-bit host. */
1727 HOST_WIDE_INT pretend_size = current_frame_info.pretend_size;
1728 emit_insn (gen_addsi3 (stack_pointer_rtx,
1729 stack_pointer_rtx,
1730 GEN_INT (-pretend_size)));
1731 }
1732
1733 /* Save any registers we need to and set up fp. */
1734 if (current_frame_info.save_fp)
1735 emit_insn (gen_movsi_push (stack_pointer_rtx, frame_pointer_rtx));
1736
1737 gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1738
1739 /* Save any needed call-saved regs (and call-used if this is an
1740 interrupt handler). */
1741 for (regno = 0; regno <= M32R_MAX_INT_REGS; ++regno)
1742 {
1743 if ((gmask & (1 << regno)) != 0)
1744 emit_insn (gen_movsi_push (stack_pointer_rtx,
1745 gen_rtx_REG (Pmode, regno)));
1746 }
1747
1748 if (current_frame_info.save_lr)
1749 emit_insn (gen_movsi_push (stack_pointer_rtx,
1750 gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
1751
1752 /* Allocate the stack frame. */
1753 frame_size = (current_frame_info.total_size
1754 - (current_frame_info.pretend_size
1755 + current_frame_info.reg_size));
1756
1757 if (frame_size == 0)
1758 ; /* Nothing to do. */
1759 else if (frame_size <= 32768)
1760 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1761 GEN_INT (-frame_size)));
1762 else
1763 {
1764 rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1765
1766 emit_insn (gen_movsi (tmp, GEN_INT (frame_size)));
1767 emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
1768 }
1769
1770 if (frame_pointer_needed)
1771 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
1772
1773 if (crtl->profile)
1774 /* Push lr for mcount (form_pc, x). */
1775 emit_insn (gen_movsi_push (stack_pointer_rtx,
1776 gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
1777
1778 if (pic_reg_used)
1779 {
1780 m32r_load_pic_register ();
1781 m32r_reload_lr (stack_pointer_rtx,
1782 (crtl->profile ? 0 : frame_size));
1783 }
1784
1785 if (crtl->profile && !pic_reg_used)
1786 emit_insn (gen_blockage ());
1787 }
1788
1789 \f
1790 /* Set up the stack and frame pointer (if desired) for the function.
1791 Note, if this is changed, you need to mirror the changes in
1792 m32r_compute_frame_size which calculates the prolog size. */
1793
1794 static void
1795 m32r_output_function_prologue (FILE * file, HOST_WIDE_INT size)
1796 {
1797 enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
1798
1799 /* If this is an interrupt handler, mark it as such. */
1800 if (M32R_INTERRUPT_P (fn_type))
1801 fprintf (file, "\t%s interrupt handler\n", ASM_COMMENT_START);
1802
1803 if (! current_frame_info.initialized)
1804 m32r_compute_frame_size (size);
1805
1806 /* This is only for the human reader. */
1807 fprintf (file,
1808 "\t%s PROLOGUE, vars= %d, regs= %d, args= %d, extra= %d\n",
1809 ASM_COMMENT_START,
1810 current_frame_info.var_size,
1811 current_frame_info.reg_size / 4,
1812 current_frame_info.args_size,
1813 current_frame_info.extra_size);
1814 }
1815 \f
1816 /* Output RTL to pop register REGNO from the stack. */
1817
1818 static void
1819 pop (int regno)
1820 {
1821 rtx x;
1822
1823 x = emit_insn (gen_movsi_pop (gen_rtx_REG (Pmode, regno),
1824 stack_pointer_rtx));
1825 add_reg_note (x, REG_INC, stack_pointer_rtx);
1826 }
1827
1828 /* Expand the m32r epilogue as a series of insns. */
1829
1830 void
1831 m32r_expand_epilogue (void)
1832 {
1833 int regno;
1834 int noepilogue = FALSE;
1835 int total_size;
1836
1837 gcc_assert (current_frame_info.initialized);
1838 total_size = current_frame_info.total_size;
1839
1840 if (total_size == 0)
1841 {
1842 rtx insn = get_last_insn ();
1843
1844 /* If the last insn was a BARRIER, we don't have to write any code
1845 because a jump (aka return) was put there. */
1846 if (insn && NOTE_P (insn))
1847 insn = prev_nonnote_insn (insn);
1848 if (insn && BARRIER_P (insn))
1849 noepilogue = TRUE;
1850 }
1851
1852 if (!noepilogue)
1853 {
1854 unsigned int var_size = current_frame_info.var_size;
1855 unsigned int args_size = current_frame_info.args_size;
1856 unsigned int gmask = current_frame_info.gmask;
1857 int can_trust_sp_p = !cfun->calls_alloca;
1858
1859 if (flag_exceptions)
1860 emit_insn (gen_blockage ());
1861
1862 /* The first thing to do is point the sp at the bottom of the register
1863 save area. */
1864 if (can_trust_sp_p)
1865 {
1866 unsigned int reg_offset = var_size + args_size;
1867
1868 if (reg_offset == 0)
1869 ; /* Nothing to do. */
1870 else if (reg_offset < 32768)
1871 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1872 GEN_INT (reg_offset)));
1873 else
1874 {
1875 rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1876
1877 emit_insn (gen_movsi (tmp, GEN_INT (reg_offset)));
1878 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1879 tmp));
1880 }
1881 }
1882 else if (frame_pointer_needed)
1883 {
1884 unsigned int reg_offset = var_size + args_size;
1885
1886 if (reg_offset == 0)
1887 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
1888 else if (reg_offset < 32768)
1889 emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_rtx,
1890 GEN_INT (reg_offset)));
1891 else
1892 {
1893 rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1894
1895 emit_insn (gen_movsi (tmp, GEN_INT (reg_offset)));
1896 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
1897 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1898 tmp));
1899 }
1900 }
1901 else
1902 gcc_unreachable ();
1903
1904 if (current_frame_info.save_lr)
1905 pop (RETURN_ADDR_REGNUM);
1906
1907 /* Restore any saved registers, in reverse order of course. */
1908 gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1909 for (regno = M32R_MAX_INT_REGS - 1; regno >= 0; --regno)
1910 {
1911 if ((gmask & (1L << regno)) != 0)
1912 pop (regno);
1913 }
1914
1915 if (current_frame_info.save_fp)
1916 pop (FRAME_POINTER_REGNUM);
1917
1918 /* Remove varargs area if present. */
1919 if (current_frame_info.pretend_size != 0)
1920 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1921 GEN_INT (current_frame_info.pretend_size)));
1922
1923 emit_insn (gen_blockage ());
1924 }
1925 }
1926
1927 /* Do any necessary cleanup after a function to restore stack, frame,
1928 and regs. */
1929
1930 static void
1931 m32r_output_function_epilogue (FILE * file ATTRIBUTE_UNUSED,
1932 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1933 {
1934 /* Reset state info for each function. */
1935 current_frame_info = zero_frame_info;
1936 m32r_compute_function_type (NULL_TREE);
1937 }
1938 \f
1939 /* Return nonzero if this function is known to have a null or 1 instruction
1940 epilogue. */
1941
1942 int
1943 direct_return (void)
1944 {
1945 if (!reload_completed)
1946 return FALSE;
1947
1948 if (M32R_INTERRUPT_P (m32r_compute_function_type (current_function_decl)))
1949 return FALSE;
1950
1951 if (! current_frame_info.initialized)
1952 m32r_compute_frame_size (get_frame_size ());
1953
1954 return current_frame_info.total_size == 0;
1955 }
1956
1957 \f
1958 /* PIC. */
1959
1960 int
1961 m32r_legitimate_pic_operand_p (rtx x)
1962 {
1963 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1964 return 0;
1965
1966 if (GET_CODE (x) == CONST
1967 && GET_CODE (XEXP (x, 0)) == PLUS
1968 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1969 || GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)
1970 && (CONST_INT_P (XEXP (XEXP (x, 0), 1))))
1971 return 0;
1972
1973 return 1;
1974 }
1975
1976 rtx
1977 m32r_legitimize_pic_address (rtx orig, rtx reg)
1978 {
1979 #ifdef DEBUG_PIC
1980 printf("m32r_legitimize_pic_address()\n");
1981 #endif
1982
1983 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
1984 {
1985 rtx pic_ref, address;
1986 int subregs = 0;
1987
1988 if (reg == 0)
1989 {
1990 gcc_assert (!reload_in_progress && !reload_completed);
1991 reg = gen_reg_rtx (Pmode);
1992
1993 subregs = 1;
1994 }
1995
1996 if (subregs)
1997 address = gen_reg_rtx (Pmode);
1998 else
1999 address = reg;
2000
2001 crtl->uses_pic_offset_table = 1;
2002
2003 if (GET_CODE (orig) == LABEL_REF
2004 || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
2005 {
2006 emit_insn (gen_gotoff_load_addr (reg, orig));
2007 emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
2008 return reg;
2009 }
2010
2011 emit_insn (gen_pic_load_addr (address, orig));
2012
2013 emit_insn (gen_addsi3 (address, address, pic_offset_table_rtx));
2014 pic_ref = gen_const_mem (Pmode, address);
2015 emit_move_insn (reg, pic_ref);
2016 return reg;
2017 }
2018 else if (GET_CODE (orig) == CONST)
2019 {
2020 rtx base, offset;
2021
2022 if (GET_CODE (XEXP (orig, 0)) == PLUS
2023 && XEXP (XEXP (orig, 0), 1) == pic_offset_table_rtx)
2024 return orig;
2025
2026 if (reg == 0)
2027 {
2028 gcc_assert (!reload_in_progress && !reload_completed);
2029 reg = gen_reg_rtx (Pmode);
2030 }
2031
2032 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2033 {
2034 base = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 0), reg);
2035 if (base == reg)
2036 offset = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 1), NULL_RTX);
2037 else
2038 offset = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 1), reg);
2039 }
2040 else
2041 return orig;
2042
2043 if (CONST_INT_P (offset))
2044 {
2045 if (INT16_P (INTVAL (offset)))
2046 return plus_constant (base, INTVAL (offset));
2047 else
2048 {
2049 gcc_assert (! reload_in_progress && ! reload_completed);
2050 offset = force_reg (Pmode, offset);
2051 }
2052 }
2053
2054 return gen_rtx_PLUS (Pmode, base, offset);
2055 }
2056
2057 return orig;
2058 }
2059
2060 static rtx
2061 m32r_legitimize_address (rtx x, rtx orig_x ATTRIBUTE_UNUSED,
2062 enum machine_mode mode ATTRIBUTE_UNUSED)
2063 {
2064 if (flag_pic)
2065 return m32r_legitimize_pic_address (x, NULL_RTX);
2066 else
2067 return x;
2068 }
2069
2070 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P. */
2071
2072 static bool
2073 m32r_mode_dependent_address_p (const_rtx addr)
2074 {
2075 if (GET_CODE (addr) == LO_SUM)
2076 return true;
2077
2078 return false;
2079 }
2080 \f
2081 /* Nested function support. */
2082
2083 /* Emit RTL insns to initialize the variable parts of a trampoline.
2084 FNADDR is an RTX for the address of the function's pure code.
2085 CXT is an RTX for the static chain value for the function. */
2086
2087 void
2088 m32r_initialize_trampoline (rtx tramp ATTRIBUTE_UNUSED,
2089 rtx fnaddr ATTRIBUTE_UNUSED,
2090 rtx cxt ATTRIBUTE_UNUSED)
2091 {
2092 }
2093 \f
2094 static void
2095 m32r_file_start (void)
2096 {
2097 default_file_start ();
2098
2099 if (flag_verbose_asm)
2100 fprintf (asm_out_file,
2101 "%s M32R/D special options: -G %d\n",
2102 ASM_COMMENT_START, g_switch_value);
2103
2104 if (TARGET_LITTLE_ENDIAN)
2105 fprintf (asm_out_file, "\t.little\n");
2106 }
2107 \f
2108 /* Print operand X (an rtx) in assembler syntax to file FILE.
2109 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
2110 For `%' followed by punctuation, CODE is the punctuation and X is null. */
2111
2112 static void
2113 m32r_print_operand (FILE * file, rtx x, int code)
2114 {
2115 rtx addr;
2116
2117 switch (code)
2118 {
2119 /* The 's' and 'p' codes are used by output_block_move() to
2120 indicate post-increment 's'tores and 'p're-increment loads. */
2121 case 's':
2122 if (REG_P (x))
2123 fprintf (file, "@+%s", reg_names [REGNO (x)]);
2124 else
2125 output_operand_lossage ("invalid operand to %%s code");
2126 return;
2127
2128 case 'p':
2129 if (REG_P (x))
2130 fprintf (file, "@%s+", reg_names [REGNO (x)]);
2131 else
2132 output_operand_lossage ("invalid operand to %%p code");
2133 return;
2134
2135 case 'R' :
2136 /* Write second word of DImode or DFmode reference,
2137 register or memory. */
2138 if (REG_P (x))
2139 fputs (reg_names[REGNO (x)+1], file);
2140 else if (MEM_P (x))
2141 {
2142 fprintf (file, "@(");
2143 /* Handle possible auto-increment. Since it is pre-increment and
2144 we have already done it, we can just use an offset of four. */
2145 /* ??? This is taken from rs6000.c I think. I don't think it is
2146 currently necessary, but keep it around. */
2147 if (GET_CODE (XEXP (x, 0)) == PRE_INC
2148 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2149 output_address (plus_constant (XEXP (XEXP (x, 0), 0), 4));
2150 else
2151 output_address (plus_constant (XEXP (x, 0), 4));
2152 fputc (')', file);
2153 }
2154 else
2155 output_operand_lossage ("invalid operand to %%R code");
2156 return;
2157
2158 case 'H' : /* High word. */
2159 case 'L' : /* Low word. */
2160 if (REG_P (x))
2161 {
2162 /* L = least significant word, H = most significant word. */
2163 if ((WORDS_BIG_ENDIAN != 0) ^ (code == 'L'))
2164 fputs (reg_names[REGNO (x)], file);
2165 else
2166 fputs (reg_names[REGNO (x)+1], file);
2167 }
2168 else if (CONST_INT_P (x)
2169 || GET_CODE (x) == CONST_DOUBLE)
2170 {
2171 rtx first, second;
2172
2173 split_double (x, &first, &second);
2174 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2175 code == 'L' ? INTVAL (first) : INTVAL (second));
2176 }
2177 else
2178 output_operand_lossage ("invalid operand to %%H/%%L code");
2179 return;
2180
2181 case 'A' :
2182 {
2183 char str[30];
2184
2185 if (GET_CODE (x) != CONST_DOUBLE
2186 || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
2187 fatal_insn ("bad insn for 'A'", x);
2188
2189 real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (x), sizeof (str), 0, 1);
2190 fprintf (file, "%s", str);
2191 return;
2192 }
2193
2194 case 'B' : /* Bottom half. */
2195 case 'T' : /* Top half. */
2196 /* Output the argument to a `seth' insn (sets the Top half-word).
2197 For constants output arguments to a seth/or3 pair to set Top and
2198 Bottom halves. For symbols output arguments to a seth/add3 pair to
2199 set Top and Bottom halves. The difference exists because for
2200 constants seth/or3 is more readable but for symbols we need to use
2201 the same scheme as `ld' and `st' insns (16-bit addend is signed). */
2202 switch (GET_CODE (x))
2203 {
2204 case CONST_INT :
2205 case CONST_DOUBLE :
2206 {
2207 rtx first, second;
2208
2209 split_double (x, &first, &second);
2210 x = WORDS_BIG_ENDIAN ? second : first;
2211 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2212 (code == 'B'
2213 ? INTVAL (x) & 0xffff
2214 : (INTVAL (x) >> 16) & 0xffff));
2215 }
2216 return;
2217 case CONST :
2218 case SYMBOL_REF :
2219 if (code == 'B'
2220 && small_data_operand (x, VOIDmode))
2221 {
2222 fputs ("sda(", file);
2223 output_addr_const (file, x);
2224 fputc (')', file);
2225 return;
2226 }
2227 /* fall through */
2228 case LABEL_REF :
2229 fputs (code == 'T' ? "shigh(" : "low(", file);
2230 output_addr_const (file, x);
2231 fputc (')', file);
2232 return;
2233 default :
2234 output_operand_lossage ("invalid operand to %%T/%%B code");
2235 return;
2236 }
2237 break;
2238
2239 case 'U' :
2240 /* ??? wip */
2241 /* Output a load/store with update indicator if appropriate. */
2242 if (MEM_P (x))
2243 {
2244 if (GET_CODE (XEXP (x, 0)) == PRE_INC
2245 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2246 fputs (".a", file);
2247 }
2248 else
2249 output_operand_lossage ("invalid operand to %%U code");
2250 return;
2251
2252 case 'N' :
2253 /* Print a constant value negated. */
2254 if (CONST_INT_P (x))
2255 output_addr_const (file, GEN_INT (- INTVAL (x)));
2256 else
2257 output_operand_lossage ("invalid operand to %%N code");
2258 return;
2259
2260 case 'X' :
2261 /* Print a const_int in hex. Used in comments. */
2262 if (CONST_INT_P (x))
2263 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
2264 return;
2265
2266 case '#' :
2267 fputs (IMMEDIATE_PREFIX, file);
2268 return;
2269
2270 case 0 :
2271 /* Do nothing special. */
2272 break;
2273
2274 default :
2275 /* Unknown flag. */
2276 output_operand_lossage ("invalid operand output code");
2277 }
2278
2279 switch (GET_CODE (x))
2280 {
2281 case REG :
2282 fputs (reg_names[REGNO (x)], file);
2283 break;
2284
2285 case MEM :
2286 addr = XEXP (x, 0);
2287 if (GET_CODE (addr) == PRE_INC)
2288 {
2289 if (!REG_P (XEXP (addr, 0)))
2290 fatal_insn ("pre-increment address is not a register", x);
2291
2292 fprintf (file, "@+%s", reg_names[REGNO (XEXP (addr, 0))]);
2293 }
2294 else if (GET_CODE (addr) == PRE_DEC)
2295 {
2296 if (!REG_P (XEXP (addr, 0)))
2297 fatal_insn ("pre-decrement address is not a register", x);
2298
2299 fprintf (file, "@-%s", reg_names[REGNO (XEXP (addr, 0))]);
2300 }
2301 else if (GET_CODE (addr) == POST_INC)
2302 {
2303 if (!REG_P (XEXP (addr, 0)))
2304 fatal_insn ("post-increment address is not a register", x);
2305
2306 fprintf (file, "@%s+", reg_names[REGNO (XEXP (addr, 0))]);
2307 }
2308 else
2309 {
2310 fputs ("@(", file);
2311 output_address (XEXP (x, 0));
2312 fputc (')', file);
2313 }
2314 break;
2315
2316 case CONST_DOUBLE :
2317 /* We handle SFmode constants here as output_addr_const doesn't. */
2318 if (GET_MODE (x) == SFmode)
2319 {
2320 REAL_VALUE_TYPE d;
2321 long l;
2322
2323 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
2324 REAL_VALUE_TO_TARGET_SINGLE (d, l);
2325 fprintf (file, "0x%08lx", l);
2326 break;
2327 }
2328
2329 /* Fall through. Let output_addr_const deal with it. */
2330
2331 default :
2332 output_addr_const (file, x);
2333 break;
2334 }
2335 }
2336
2337 /* Print a memory address as an operand to reference that memory location. */
2338
2339 static void
2340 m32r_print_operand_address (FILE * file, rtx addr)
2341 {
2342 rtx base;
2343 rtx index = 0;
2344 int offset = 0;
2345
2346 switch (GET_CODE (addr))
2347 {
2348 case REG :
2349 fputs (reg_names[REGNO (addr)], file);
2350 break;
2351
2352 case PLUS :
2353 if (CONST_INT_P (XEXP (addr, 0)))
2354 offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
2355 else if (CONST_INT_P (XEXP (addr, 1)))
2356 offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
2357 else
2358 base = XEXP (addr, 0), index = XEXP (addr, 1);
2359 if (REG_P (base))
2360 {
2361 /* Print the offset first (if present) to conform to the manual. */
2362 if (index == 0)
2363 {
2364 if (offset != 0)
2365 fprintf (file, "%d,", offset);
2366 fputs (reg_names[REGNO (base)], file);
2367 }
2368 /* The chip doesn't support this, but left in for generality. */
2369 else if (REG_P (index))
2370 fprintf (file, "%s,%s",
2371 reg_names[REGNO (base)], reg_names[REGNO (index)]);
2372 /* Not sure this can happen, but leave in for now. */
2373 else if (GET_CODE (index) == SYMBOL_REF)
2374 {
2375 output_addr_const (file, index);
2376 fputc (',', file);
2377 fputs (reg_names[REGNO (base)], file);
2378 }
2379 else
2380 fatal_insn ("bad address", addr);
2381 }
2382 else if (GET_CODE (base) == LO_SUM)
2383 {
2384 gcc_assert (!index && REG_P (XEXP (base, 0)));
2385 if (small_data_operand (XEXP (base, 1), VOIDmode))
2386 fputs ("sda(", file);
2387 else
2388 fputs ("low(", file);
2389 output_addr_const (file, plus_constant (XEXP (base, 1), offset));
2390 fputs ("),", file);
2391 fputs (reg_names[REGNO (XEXP (base, 0))], file);
2392 }
2393 else
2394 fatal_insn ("bad address", addr);
2395 break;
2396
2397 case LO_SUM :
2398 if (!REG_P (XEXP (addr, 0)))
2399 fatal_insn ("lo_sum not of register", addr);
2400 if (small_data_operand (XEXP (addr, 1), VOIDmode))
2401 fputs ("sda(", file);
2402 else
2403 fputs ("low(", file);
2404 output_addr_const (file, XEXP (addr, 1));
2405 fputs ("),", file);
2406 fputs (reg_names[REGNO (XEXP (addr, 0))], file);
2407 break;
2408
2409 case PRE_INC : /* Assume SImode. */
2410 fprintf (file, "+%s", reg_names[REGNO (XEXP (addr, 0))]);
2411 break;
2412
2413 case PRE_DEC : /* Assume SImode. */
2414 fprintf (file, "-%s", reg_names[REGNO (XEXP (addr, 0))]);
2415 break;
2416
2417 case POST_INC : /* Assume SImode. */
2418 fprintf (file, "%s+", reg_names[REGNO (XEXP (addr, 0))]);
2419 break;
2420
2421 default :
2422 output_addr_const (file, addr);
2423 break;
2424 }
2425 }
2426
2427 static bool
2428 m32r_print_operand_punct_valid_p (unsigned char code)
2429 {
2430 return m32r_punct_chars[code];
2431 }
2432
2433 /* Return true if the operands are the constants 0 and 1. */
2434
2435 int
2436 zero_and_one (rtx operand1, rtx operand2)
2437 {
2438 return
2439 CONST_INT_P (operand1)
2440 && CONST_INT_P (operand2)
2441 && ( ((INTVAL (operand1) == 0) && (INTVAL (operand2) == 1))
2442 ||((INTVAL (operand1) == 1) && (INTVAL (operand2) == 0)));
2443 }
2444
2445 /* Generate the correct assembler code to handle the conditional loading of a
2446 value into a register. It is known that the operands satisfy the
2447 conditional_move_operand() function above. The destination is operand[0].
2448 The condition is operand [1]. The 'true' value is operand [2] and the
2449 'false' value is operand [3]. */
2450
2451 char *
2452 emit_cond_move (rtx * operands, rtx insn ATTRIBUTE_UNUSED)
2453 {
2454 static char buffer [100];
2455 const char * dest = reg_names [REGNO (operands [0])];
2456
2457 buffer [0] = 0;
2458
2459 /* Destination must be a register. */
2460 gcc_assert (REG_P (operands [0]));
2461 gcc_assert (conditional_move_operand (operands [2], SImode));
2462 gcc_assert (conditional_move_operand (operands [3], SImode));
2463
2464 /* Check to see if the test is reversed. */
2465 if (GET_CODE (operands [1]) == NE)
2466 {
2467 rtx tmp = operands [2];
2468 operands [2] = operands [3];
2469 operands [3] = tmp;
2470 }
2471
2472 sprintf (buffer, "mvfc %s, cbr", dest);
2473
2474 /* If the true value was '0' then we need to invert the results of the move. */
2475 if (INTVAL (operands [2]) == 0)
2476 sprintf (buffer + strlen (buffer), "\n\txor3 %s, %s, #1",
2477 dest, dest);
2478
2479 return buffer;
2480 }
2481
2482 /* Returns true if the registers contained in the two
2483 rtl expressions are different. */
2484
2485 int
2486 m32r_not_same_reg (rtx a, rtx b)
2487 {
2488 int reg_a = -1;
2489 int reg_b = -2;
2490
2491 while (GET_CODE (a) == SUBREG)
2492 a = SUBREG_REG (a);
2493
2494 if (REG_P (a))
2495 reg_a = REGNO (a);
2496
2497 while (GET_CODE (b) == SUBREG)
2498 b = SUBREG_REG (b);
2499
2500 if (REG_P (b))
2501 reg_b = REGNO (b);
2502
2503 return reg_a != reg_b;
2504 }
2505
2506 \f
2507 rtx
2508 m32r_function_symbol (const char *name)
2509 {
2510 int extra_flags = 0;
2511 enum m32r_model model;
2512 rtx sym = gen_rtx_SYMBOL_REF (Pmode, name);
2513
2514 if (TARGET_MODEL_SMALL)
2515 model = M32R_MODEL_SMALL;
2516 else if (TARGET_MODEL_MEDIUM)
2517 model = M32R_MODEL_MEDIUM;
2518 else if (TARGET_MODEL_LARGE)
2519 model = M32R_MODEL_LARGE;
2520 else
2521 gcc_unreachable (); /* Shouldn't happen. */
2522 extra_flags |= model << SYMBOL_FLAG_MODEL_SHIFT;
2523
2524 if (extra_flags)
2525 SYMBOL_REF_FLAGS (sym) |= extra_flags;
2526
2527 return sym;
2528 }
2529
2530 /* Use a library function to move some bytes. */
2531
2532 static void
2533 block_move_call (rtx dest_reg, rtx src_reg, rtx bytes_rtx)
2534 {
2535 /* We want to pass the size as Pmode, which will normally be SImode
2536 but will be DImode if we are using 64-bit longs and pointers. */
2537 if (GET_MODE (bytes_rtx) != VOIDmode
2538 && GET_MODE (bytes_rtx) != Pmode)
2539 bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
2540
2541 emit_library_call (m32r_function_symbol ("memcpy"), LCT_NORMAL,
2542 VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
2543 convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
2544 TYPE_UNSIGNED (sizetype)),
2545 TYPE_MODE (sizetype));
2546 }
2547
2548 /* Expand string/block move operations.
2549
2550 operands[0] is the pointer to the destination.
2551 operands[1] is the pointer to the source.
2552 operands[2] is the number of bytes to move.
2553 operands[3] is the alignment.
2554
2555 Returns 1 upon success, 0 otherwise. */
2556
2557 int
2558 m32r_expand_block_move (rtx operands[])
2559 {
2560 rtx orig_dst = operands[0];
2561 rtx orig_src = operands[1];
2562 rtx bytes_rtx = operands[2];
2563 rtx align_rtx = operands[3];
2564 int constp = CONST_INT_P (bytes_rtx);
2565 HOST_WIDE_INT bytes = constp ? INTVAL (bytes_rtx) : 0;
2566 int align = INTVAL (align_rtx);
2567 int leftover;
2568 rtx src_reg;
2569 rtx dst_reg;
2570
2571 if (constp && bytes <= 0)
2572 return 1;
2573
2574 /* Move the address into scratch registers. */
2575 dst_reg = copy_addr_to_reg (XEXP (orig_dst, 0));
2576 src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
2577
2578 if (align > UNITS_PER_WORD)
2579 align = UNITS_PER_WORD;
2580
2581 /* If we prefer size over speed, always use a function call.
2582 If we do not know the size, use a function call.
2583 If the blocks are not word aligned, use a function call. */
2584 if (optimize_size || ! constp || align != UNITS_PER_WORD)
2585 {
2586 block_move_call (dst_reg, src_reg, bytes_rtx);
2587 return 0;
2588 }
2589
2590 leftover = bytes % MAX_MOVE_BYTES;
2591 bytes -= leftover;
2592
2593 /* If necessary, generate a loop to handle the bulk of the copy. */
2594 if (bytes)
2595 {
2596 rtx label = NULL_RTX;
2597 rtx final_src = NULL_RTX;
2598 rtx at_a_time = GEN_INT (MAX_MOVE_BYTES);
2599 rtx rounded_total = GEN_INT (bytes);
2600 rtx new_dst_reg = gen_reg_rtx (SImode);
2601 rtx new_src_reg = gen_reg_rtx (SImode);
2602
2603 /* If we are going to have to perform this loop more than
2604 once, then generate a label and compute the address the
2605 source register will contain upon completion of the final
2606 iteration. */
2607 if (bytes > MAX_MOVE_BYTES)
2608 {
2609 final_src = gen_reg_rtx (Pmode);
2610
2611 if (INT16_P(bytes))
2612 emit_insn (gen_addsi3 (final_src, src_reg, rounded_total));
2613 else
2614 {
2615 emit_insn (gen_movsi (final_src, rounded_total));
2616 emit_insn (gen_addsi3 (final_src, final_src, src_reg));
2617 }
2618
2619 label = gen_label_rtx ();
2620 emit_label (label);
2621 }
2622
2623 /* It is known that output_block_move() will update src_reg to point
2624 to the word after the end of the source block, and dst_reg to point
2625 to the last word of the destination block, provided that the block
2626 is MAX_MOVE_BYTES long. */
2627 emit_insn (gen_movmemsi_internal (dst_reg, src_reg, at_a_time,
2628 new_dst_reg, new_src_reg));
2629 emit_move_insn (dst_reg, new_dst_reg);
2630 emit_move_insn (src_reg, new_src_reg);
2631 emit_insn (gen_addsi3 (dst_reg, dst_reg, GEN_INT (4)));
2632
2633 if (bytes > MAX_MOVE_BYTES)
2634 {
2635 rtx test = gen_rtx_NE (VOIDmode, src_reg, final_src);
2636 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
2637 }
2638 }
2639
2640 if (leftover)
2641 emit_insn (gen_movmemsi_internal (dst_reg, src_reg, GEN_INT (leftover),
2642 gen_reg_rtx (SImode),
2643 gen_reg_rtx (SImode)));
2644 return 1;
2645 }
2646
2647 \f
2648 /* Emit load/stores for a small constant word aligned block_move.
2649
2650 operands[0] is the memory address of the destination.
2651 operands[1] is the memory address of the source.
2652 operands[2] is the number of bytes to move.
2653 operands[3] is a temp register.
2654 operands[4] is a temp register. */
2655
2656 void
2657 m32r_output_block_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
2658 {
2659 HOST_WIDE_INT bytes = INTVAL (operands[2]);
2660 int first_time;
2661 int got_extra = 0;
2662
2663 gcc_assert (bytes >= 1 && bytes <= MAX_MOVE_BYTES);
2664
2665 /* We do not have a post-increment store available, so the first set of
2666 stores are done without any increment, then the remaining ones can use
2667 the pre-increment addressing mode.
2668
2669 Note: expand_block_move() also relies upon this behavior when building
2670 loops to copy large blocks. */
2671 first_time = 1;
2672
2673 while (bytes > 0)
2674 {
2675 if (bytes >= 8)
2676 {
2677 if (first_time)
2678 {
2679 output_asm_insn ("ld\t%5, %p1", operands);
2680 output_asm_insn ("ld\t%6, %p1", operands);
2681 output_asm_insn ("st\t%5, @%0", operands);
2682 output_asm_insn ("st\t%6, %s0", operands);
2683 }
2684 else
2685 {
2686 output_asm_insn ("ld\t%5, %p1", operands);
2687 output_asm_insn ("ld\t%6, %p1", operands);
2688 output_asm_insn ("st\t%5, %s0", operands);
2689 output_asm_insn ("st\t%6, %s0", operands);
2690 }
2691
2692 bytes -= 8;
2693 }
2694 else if (bytes >= 4)
2695 {
2696 if (bytes > 4)
2697 got_extra = 1;
2698
2699 output_asm_insn ("ld\t%5, %p1", operands);
2700
2701 if (got_extra)
2702 output_asm_insn ("ld\t%6, %p1", operands);
2703
2704 if (first_time)
2705 output_asm_insn ("st\t%5, @%0", operands);
2706 else
2707 output_asm_insn ("st\t%5, %s0", operands);
2708
2709 bytes -= 4;
2710 }
2711 else
2712 {
2713 /* Get the entire next word, even though we do not want all of it.
2714 The saves us from doing several smaller loads, and we assume that
2715 we cannot cause a page fault when at least part of the word is in
2716 valid memory [since we don't get called if things aren't properly
2717 aligned]. */
2718 int dst_offset = first_time ? 0 : 4;
2719 /* The amount of increment we have to make to the
2720 destination pointer. */
2721 int dst_inc_amount = dst_offset + bytes - 4;
2722 /* The same for the source pointer. */
2723 int src_inc_amount = bytes;
2724 int last_shift;
2725 rtx my_operands[3];
2726
2727 /* If got_extra is true then we have already loaded
2728 the next word as part of loading and storing the previous word. */
2729 if (! got_extra)
2730 output_asm_insn ("ld\t%6, @%1", operands);
2731
2732 if (bytes >= 2)
2733 {
2734 bytes -= 2;
2735
2736 output_asm_insn ("sra3\t%5, %6, #16", operands);
2737 my_operands[0] = operands[5];
2738 my_operands[1] = GEN_INT (dst_offset);
2739 my_operands[2] = operands[0];
2740 output_asm_insn ("sth\t%0, @(%1,%2)", my_operands);
2741
2742 /* If there is a byte left to store then increment the
2743 destination address and shift the contents of the source
2744 register down by 8 bits. We could not do the address
2745 increment in the store half word instruction, because it does
2746 not have an auto increment mode. */
2747 if (bytes > 0) /* assert (bytes == 1) */
2748 {
2749 dst_offset += 2;
2750 last_shift = 8;
2751 }
2752 }
2753 else
2754 last_shift = 24;
2755
2756 if (bytes > 0)
2757 {
2758 my_operands[0] = operands[6];
2759 my_operands[1] = GEN_INT (last_shift);
2760 output_asm_insn ("srai\t%0, #%1", my_operands);
2761 my_operands[0] = operands[6];
2762 my_operands[1] = GEN_INT (dst_offset);
2763 my_operands[2] = operands[0];
2764 output_asm_insn ("stb\t%0, @(%1,%2)", my_operands);
2765 }
2766
2767 /* Update the destination pointer if needed. We have to do
2768 this so that the patterns matches what we output in this
2769 function. */
2770 if (dst_inc_amount
2771 && !find_reg_note (insn, REG_UNUSED, operands[0]))
2772 {
2773 my_operands[0] = operands[0];
2774 my_operands[1] = GEN_INT (dst_inc_amount);
2775 output_asm_insn ("addi\t%0, #%1", my_operands);
2776 }
2777
2778 /* Update the source pointer if needed. We have to do this
2779 so that the patterns matches what we output in this
2780 function. */
2781 if (src_inc_amount
2782 && !find_reg_note (insn, REG_UNUSED, operands[1]))
2783 {
2784 my_operands[0] = operands[1];
2785 my_operands[1] = GEN_INT (src_inc_amount);
2786 output_asm_insn ("addi\t%0, #%1", my_operands);
2787 }
2788
2789 bytes = 0;
2790 }
2791
2792 first_time = 0;
2793 }
2794 }
2795
2796 /* Return true if using NEW_REG in place of OLD_REG is ok. */
2797
2798 int
2799 m32r_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
2800 unsigned int new_reg)
2801 {
2802 /* Interrupt routines can't clobber any register that isn't already used. */
2803 if (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl))
2804 && !df_regs_ever_live_p (new_reg))
2805 return 0;
2806
2807 return 1;
2808 }
2809
2810 rtx
2811 m32r_return_addr (int count)
2812 {
2813 if (count != 0)
2814 return const0_rtx;
2815
2816 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
2817 }
2818
2819 static void
2820 m32r_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2821 {
2822 emit_move_insn (adjust_address (m_tramp, SImode, 0),
2823 gen_int_mode (TARGET_LITTLE_ENDIAN ?
2824 0x017e8e17 : 0x178e7e01, SImode));
2825 emit_move_insn (adjust_address (m_tramp, SImode, 4),
2826 gen_int_mode (TARGET_LITTLE_ENDIAN ?
2827 0x0c00ae86 : 0x86ae000c, SImode));
2828 emit_move_insn (adjust_address (m_tramp, SImode, 8),
2829 gen_int_mode (TARGET_LITTLE_ENDIAN ?
2830 0xe627871e : 0x1e8727e6, SImode));
2831 emit_move_insn (adjust_address (m_tramp, SImode, 12),
2832 gen_int_mode (TARGET_LITTLE_ENDIAN ?
2833 0xc616c626 : 0x26c61fc6, SImode));
2834 emit_move_insn (adjust_address (m_tramp, SImode, 16),
2835 chain_value);
2836 emit_move_insn (adjust_address (m_tramp, SImode, 20),
2837 XEXP (DECL_RTL (fndecl), 0));
2838
2839 if (m32r_cache_flush_trap >= 0)
2840 emit_insn (gen_flush_icache
2841 (validize_mem (adjust_address (m_tramp, SImode, 0)),
2842 gen_int_mode (m32r_cache_flush_trap, SImode)));
2843 else if (m32r_cache_flush_func && m32r_cache_flush_func[0])
2844 emit_library_call (m32r_function_symbol (m32r_cache_flush_func),
2845 LCT_NORMAL, VOIDmode, 3, XEXP (m_tramp, 0), Pmode,
2846 gen_int_mode (TRAMPOLINE_SIZE, SImode), SImode,
2847 GEN_INT (3), SImode);
2848 }
2849
2850 /* True if X is a reg that can be used as a base reg. */
2851
2852 static bool
2853 m32r_rtx_ok_for_base_p (const_rtx x, bool strict)
2854 {
2855 if (! REG_P (x))
2856 return false;
2857
2858 if (strict)
2859 {
2860 if (GPR_P (REGNO (x)))
2861 return true;
2862 }
2863 else
2864 {
2865 if (GPR_P (REGNO (x))
2866 || REGNO (x) == ARG_POINTER_REGNUM
2867 || ! HARD_REGISTER_P (x))
2868 return true;
2869 }
2870
2871 return false;
2872 }
2873
2874 static inline bool
2875 m32r_rtx_ok_for_offset_p (const_rtx x)
2876 {
2877 return (CONST_INT_P (x) && INT16_P (INTVAL (x)));
2878 }
2879
2880 static inline bool
2881 m32r_legitimate_offset_addres_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2882 const_rtx x, bool strict)
2883 {
2884 if (GET_CODE (x) == PLUS
2885 && m32r_rtx_ok_for_base_p (XEXP (x, 0), strict)
2886 && m32r_rtx_ok_for_offset_p (XEXP (x, 1)))
2887 return true;
2888
2889 return false;
2890 }
2891
2892 /* For LO_SUM addresses, do not allow them if the MODE is > 1 word,
2893 since more than one instruction will be required. */
2894
2895 static inline bool
2896 m32r_legitimate_lo_sum_addres_p (enum machine_mode mode, const_rtx x,
2897 bool strict)
2898 {
2899 if (GET_CODE (x) == LO_SUM
2900 && (mode != BLKmode && GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2901 && m32r_rtx_ok_for_base_p (XEXP (x, 0), strict)
2902 && CONSTANT_P (XEXP (x, 1)))
2903 return true;
2904
2905 return false;
2906 }
2907
2908 /* Is this a load and increment operation. */
2909
2910 static inline bool
2911 m32r_load_postinc_p (enum machine_mode mode, const_rtx x, bool strict)
2912 {
2913 if ((mode == SImode || mode == SFmode)
2914 && GET_CODE (x) == POST_INC
2915 && REG_P (XEXP (x, 0))
2916 && m32r_rtx_ok_for_base_p (XEXP (x, 0), strict))
2917 return true;
2918
2919 return false;
2920 }
2921
2922 /* Is this an increment/decrement and store operation. */
2923
2924 static inline bool
2925 m32r_store_preinc_predec_p (enum machine_mode mode, const_rtx x, bool strict)
2926 {
2927 if ((mode == SImode || mode == SFmode)
2928 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
2929 && REG_P (XEXP (x, 0)) \
2930 && m32r_rtx_ok_for_base_p (XEXP (x, 0), strict))
2931 return true;
2932
2933 return false;
2934 }
2935
2936 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2937
2938 static bool
2939 m32r_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
2940 {
2941 if (m32r_rtx_ok_for_base_p (x, strict)
2942 || m32r_legitimate_offset_addres_p (mode, x, strict)
2943 || m32r_legitimate_lo_sum_addres_p (mode, x, strict)
2944 || m32r_load_postinc_p (mode, x, strict)
2945 || m32r_store_preinc_predec_p (mode, x, strict))
2946 return true;
2947
2948 return false;
2949 }
2950
2951 static void
2952 m32r_conditional_register_usage (void)
2953 {
2954 if (flag_pic)
2955 {
2956 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2957 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2958 }
2959 }