Debian Codegraph @

gcc/config/m32r/m32r.cc

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