]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/mcore/mcore.c
expr.h: Remove prototypes of functions defined in builtins.c.
[thirdparty/gcc.git] / gcc / config / mcore / mcore.c
1 /* Output routines for Motorola MCore processor
2 Copyright (C) 1993-2014 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "stor-layout.h"
27 #include "varasm.h"
28 #include "stringpool.h"
29 #include "calls.h"
30 #include "tm_p.h"
31 #include "mcore.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "obstack.h"
40 #include "expr.h"
41 #include "reload.h"
42 #include "recog.h"
43 #include "function.h"
44 #include "ggc.h"
45 #include "diagnostic-core.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "df.h"
49 #include "builtins.h"
50
51 /* For dumping information about frame sizes. */
52 char * mcore_current_function_name = 0;
53 long mcore_current_compilation_timestamp = 0;
54
55 /* Global variables for machine-dependent things. */
56
57 /* Provides the class number of the smallest class containing
58 reg number. */
59 const enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER] =
60 {
61 GENERAL_REGS, ONLYR1_REGS, LRW_REGS, LRW_REGS,
62 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
63 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
64 LRW_REGS, LRW_REGS, LRW_REGS, GENERAL_REGS,
65 GENERAL_REGS, C_REGS, NO_REGS, NO_REGS,
66 };
67
68 struct mcore_frame
69 {
70 int arg_size; /* Stdarg spills (bytes). */
71 int reg_size; /* Non-volatile reg saves (bytes). */
72 int reg_mask; /* Non-volatile reg saves. */
73 int local_size; /* Locals. */
74 int outbound_size; /* Arg overflow on calls out. */
75 int pad_outbound;
76 int pad_local;
77 int pad_reg;
78 /* Describe the steps we'll use to grow it. */
79 #define MAX_STACK_GROWS 4 /* Gives us some spare space. */
80 int growth[MAX_STACK_GROWS];
81 int arg_offset;
82 int reg_offset;
83 int reg_growth;
84 int local_growth;
85 };
86
87 typedef enum
88 {
89 COND_NO,
90 COND_MOV_INSN,
91 COND_CLR_INSN,
92 COND_INC_INSN,
93 COND_DEC_INSN,
94 COND_BRANCH_INSN
95 }
96 cond_type;
97
98 static void output_stack_adjust (int, int);
99 static int calc_live_regs (int *);
100 static int try_constant_tricks (long, HOST_WIDE_INT *, HOST_WIDE_INT *);
101 static const char * output_inline_const (enum machine_mode, rtx *);
102 static void layout_mcore_frame (struct mcore_frame *);
103 static void mcore_setup_incoming_varargs (cumulative_args_t, enum machine_mode, tree, int *, int);
104 static cond_type is_cond_candidate (rtx);
105 static rtx emit_new_cond_insn (rtx, int);
106 static rtx conditionalize_block (rtx);
107 static void conditionalize_optimization (void);
108 static void mcore_reorg (void);
109 static rtx handle_structs_in_regs (enum machine_mode, const_tree, int);
110 static void mcore_mark_dllexport (tree);
111 static void mcore_mark_dllimport (tree);
112 static int mcore_dllexport_p (tree);
113 static int mcore_dllimport_p (tree);
114 static tree mcore_handle_naked_attribute (tree *, tree, tree, int, bool *);
115 #ifdef OBJECT_FORMAT_ELF
116 static void mcore_asm_named_section (const char *,
117 unsigned int, tree);
118 #endif
119 static void mcore_print_operand (FILE *, rtx, int);
120 static void mcore_print_operand_address (FILE *, rtx);
121 static bool mcore_print_operand_punct_valid_p (unsigned char code);
122 static void mcore_unique_section (tree, int);
123 static void mcore_encode_section_info (tree, rtx, int);
124 static const char *mcore_strip_name_encoding (const char *);
125 static int mcore_const_costs (rtx, RTX_CODE);
126 static int mcore_and_cost (rtx);
127 static int mcore_ior_cost (rtx);
128 static bool mcore_rtx_costs (rtx, int, int, int,
129 int *, bool);
130 static void mcore_external_libcall (rtx);
131 static bool mcore_return_in_memory (const_tree, const_tree);
132 static int mcore_arg_partial_bytes (cumulative_args_t,
133 enum machine_mode,
134 tree, bool);
135 static rtx mcore_function_arg (cumulative_args_t,
136 enum machine_mode,
137 const_tree, bool);
138 static void mcore_function_arg_advance (cumulative_args_t,
139 enum machine_mode,
140 const_tree, bool);
141 static unsigned int mcore_function_arg_boundary (enum machine_mode,
142 const_tree);
143 static void mcore_asm_trampoline_template (FILE *);
144 static void mcore_trampoline_init (rtx, tree, rtx);
145 static bool mcore_warn_func_return (tree);
146 static void mcore_option_override (void);
147 static bool mcore_legitimate_constant_p (enum machine_mode, rtx);
148 \f
149 /* MCore specific attributes. */
150
151 static const struct attribute_spec mcore_attribute_table[] =
152 {
153 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
154 affects_type_identity } */
155 { "dllexport", 0, 0, true, false, false, NULL, false },
156 { "dllimport", 0, 0, true, false, false, NULL, false },
157 { "naked", 0, 0, true, false, false, mcore_handle_naked_attribute,
158 false },
159 { NULL, 0, 0, false, false, false, NULL, false }
160 };
161 \f
162 /* Initialize the GCC target structure. */
163 #undef TARGET_ASM_EXTERNAL_LIBCALL
164 #define TARGET_ASM_EXTERNAL_LIBCALL mcore_external_libcall
165
166 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
167 #undef TARGET_MERGE_DECL_ATTRIBUTES
168 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
169 #endif
170
171 #ifdef OBJECT_FORMAT_ELF
172 #undef TARGET_ASM_UNALIGNED_HI_OP
173 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
174 #undef TARGET_ASM_UNALIGNED_SI_OP
175 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
176 #endif
177
178 #undef TARGET_PRINT_OPERAND
179 #define TARGET_PRINT_OPERAND mcore_print_operand
180 #undef TARGET_PRINT_OPERAND_ADDRESS
181 #define TARGET_PRINT_OPERAND_ADDRESS mcore_print_operand_address
182 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
183 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mcore_print_operand_punct_valid_p
184
185 #undef TARGET_ATTRIBUTE_TABLE
186 #define TARGET_ATTRIBUTE_TABLE mcore_attribute_table
187 #undef TARGET_ASM_UNIQUE_SECTION
188 #define TARGET_ASM_UNIQUE_SECTION mcore_unique_section
189 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
190 #define TARGET_ASM_FUNCTION_RODATA_SECTION default_no_function_rodata_section
191 #undef TARGET_ENCODE_SECTION_INFO
192 #define TARGET_ENCODE_SECTION_INFO mcore_encode_section_info
193 #undef TARGET_STRIP_NAME_ENCODING
194 #define TARGET_STRIP_NAME_ENCODING mcore_strip_name_encoding
195 #undef TARGET_RTX_COSTS
196 #define TARGET_RTX_COSTS mcore_rtx_costs
197 #undef TARGET_ADDRESS_COST
198 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
199 #undef TARGET_MACHINE_DEPENDENT_REORG
200 #define TARGET_MACHINE_DEPENDENT_REORG mcore_reorg
201
202 #undef TARGET_PROMOTE_FUNCTION_MODE
203 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
204 #undef TARGET_PROMOTE_PROTOTYPES
205 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
206
207 #undef TARGET_RETURN_IN_MEMORY
208 #define TARGET_RETURN_IN_MEMORY mcore_return_in_memory
209 #undef TARGET_MUST_PASS_IN_STACK
210 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
211 #undef TARGET_PASS_BY_REFERENCE
212 #define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
213 #undef TARGET_ARG_PARTIAL_BYTES
214 #define TARGET_ARG_PARTIAL_BYTES mcore_arg_partial_bytes
215 #undef TARGET_FUNCTION_ARG
216 #define TARGET_FUNCTION_ARG mcore_function_arg
217 #undef TARGET_FUNCTION_ARG_ADVANCE
218 #define TARGET_FUNCTION_ARG_ADVANCE mcore_function_arg_advance
219 #undef TARGET_FUNCTION_ARG_BOUNDARY
220 #define TARGET_FUNCTION_ARG_BOUNDARY mcore_function_arg_boundary
221
222 #undef TARGET_SETUP_INCOMING_VARARGS
223 #define TARGET_SETUP_INCOMING_VARARGS mcore_setup_incoming_varargs
224
225 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
226 #define TARGET_ASM_TRAMPOLINE_TEMPLATE mcore_asm_trampoline_template
227 #undef TARGET_TRAMPOLINE_INIT
228 #define TARGET_TRAMPOLINE_INIT mcore_trampoline_init
229
230 #undef TARGET_OPTION_OVERRIDE
231 #define TARGET_OPTION_OVERRIDE mcore_option_override
232
233 #undef TARGET_LEGITIMATE_CONSTANT_P
234 #define TARGET_LEGITIMATE_CONSTANT_P mcore_legitimate_constant_p
235
236 #undef TARGET_WARN_FUNC_RETURN
237 #define TARGET_WARN_FUNC_RETURN mcore_warn_func_return
238
239 struct gcc_target targetm = TARGET_INITIALIZER;
240 \f
241 /* Adjust the stack and return the number of bytes taken to do it. */
242 static void
243 output_stack_adjust (int direction, int size)
244 {
245 /* If extending stack a lot, we do it incrementally. */
246 if (direction < 0 && size > mcore_stack_increment && mcore_stack_increment > 0)
247 {
248 rtx tmp = gen_rtx_REG (SImode, 1);
249 rtx memref;
250
251 emit_insn (gen_movsi (tmp, GEN_INT (mcore_stack_increment)));
252 do
253 {
254 emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
255 memref = gen_rtx_MEM (SImode, stack_pointer_rtx);
256 MEM_VOLATILE_P (memref) = 1;
257 emit_insn (gen_movsi (memref, stack_pointer_rtx));
258 size -= mcore_stack_increment;
259 }
260 while (size > mcore_stack_increment);
261
262 /* SIZE is now the residual for the last adjustment,
263 which doesn't require a probe. */
264 }
265
266 if (size)
267 {
268 rtx insn;
269 rtx val = GEN_INT (size);
270
271 if (size > 32)
272 {
273 rtx nval = gen_rtx_REG (SImode, 1);
274 emit_insn (gen_movsi (nval, val));
275 val = nval;
276 }
277
278 if (direction > 0)
279 insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
280 else
281 insn = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
282
283 emit_insn (insn);
284 }
285 }
286
287 /* Work out the registers which need to be saved,
288 both as a mask and a count. */
289
290 static int
291 calc_live_regs (int * count)
292 {
293 int reg;
294 int live_regs_mask = 0;
295
296 * count = 0;
297
298 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
299 {
300 if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
301 {
302 (*count)++;
303 live_regs_mask |= (1 << reg);
304 }
305 }
306
307 return live_regs_mask;
308 }
309
310 /* Print the operand address in x to the stream. */
311
312 static void
313 mcore_print_operand_address (FILE * stream, rtx x)
314 {
315 switch (GET_CODE (x))
316 {
317 case REG:
318 fprintf (stream, "(%s)", reg_names[REGNO (x)]);
319 break;
320
321 case PLUS:
322 {
323 rtx base = XEXP (x, 0);
324 rtx index = XEXP (x, 1);
325
326 if (GET_CODE (base) != REG)
327 {
328 /* Ensure that BASE is a register (one of them must be). */
329 rtx temp = base;
330 base = index;
331 index = temp;
332 }
333
334 switch (GET_CODE (index))
335 {
336 case CONST_INT:
337 fprintf (stream, "(%s," HOST_WIDE_INT_PRINT_DEC ")",
338 reg_names[REGNO(base)], INTVAL (index));
339 break;
340
341 default:
342 gcc_unreachable ();
343 }
344 }
345
346 break;
347
348 default:
349 output_addr_const (stream, x);
350 break;
351 }
352 }
353
354 static bool
355 mcore_print_operand_punct_valid_p (unsigned char code)
356 {
357 return (code == '.' || code == '#' || code == '*' || code == '^'
358 || code == '!');
359 }
360
361 /* Print operand x (an rtx) in assembler syntax to file stream
362 according to modifier code.
363
364 'R' print the next register or memory location along, i.e. the lsw in
365 a double word value
366 'O' print a constant without the #
367 'M' print a constant as its negative
368 'P' print log2 of a power of two
369 'Q' print log2 of an inverse of a power of two
370 'U' print register for ldm/stm instruction
371 'X' print byte number for xtrbN instruction. */
372
373 static void
374 mcore_print_operand (FILE * stream, rtx x, int code)
375 {
376 switch (code)
377 {
378 case 'N':
379 if (INTVAL(x) == -1)
380 fprintf (asm_out_file, "32");
381 else
382 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) + 1));
383 break;
384 case 'P':
385 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) & 0xffffffff));
386 break;
387 case 'Q':
388 fprintf (asm_out_file, "%d", exact_log2 (~INTVAL (x)));
389 break;
390 case 'O':
391 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
392 break;
393 case 'M':
394 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, - INTVAL (x));
395 break;
396 case 'R':
397 /* Next location along in memory or register. */
398 switch (GET_CODE (x))
399 {
400 case REG:
401 fputs (reg_names[REGNO (x) + 1], (stream));
402 break;
403 case MEM:
404 mcore_print_operand_address
405 (stream, XEXP (adjust_address (x, SImode, 4), 0));
406 break;
407 default:
408 gcc_unreachable ();
409 }
410 break;
411 case 'U':
412 fprintf (asm_out_file, "%s-%s", reg_names[REGNO (x)],
413 reg_names[REGNO (x) + 3]);
414 break;
415 case 'x':
416 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
417 break;
418 case 'X':
419 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, 3 - INTVAL (x) / 8);
420 break;
421
422 default:
423 switch (GET_CODE (x))
424 {
425 case REG:
426 fputs (reg_names[REGNO (x)], (stream));
427 break;
428 case MEM:
429 output_address (XEXP (x, 0));
430 break;
431 default:
432 output_addr_const (stream, x);
433 break;
434 }
435 break;
436 }
437 }
438
439 /* What does a constant cost ? */
440
441 static int
442 mcore_const_costs (rtx exp, enum rtx_code code)
443 {
444 HOST_WIDE_INT val = INTVAL (exp);
445
446 /* Easy constants. */
447 if ( CONST_OK_FOR_I (val)
448 || CONST_OK_FOR_M (val)
449 || CONST_OK_FOR_N (val)
450 || (code == PLUS && CONST_OK_FOR_L (val)))
451 return 1;
452 else if (code == AND
453 && ( CONST_OK_FOR_M (~val)
454 || CONST_OK_FOR_N (~val)))
455 return 2;
456 else if (code == PLUS
457 && ( CONST_OK_FOR_I (-val)
458 || CONST_OK_FOR_M (-val)
459 || CONST_OK_FOR_N (-val)))
460 return 2;
461
462 return 5;
463 }
464
465 /* What does an and instruction cost - we do this b/c immediates may
466 have been relaxed. We want to ensure that cse will cse relaxed immeds
467 out. Otherwise we'll get bad code (multiple reloads of the same const). */
468
469 static int
470 mcore_and_cost (rtx x)
471 {
472 HOST_WIDE_INT val;
473
474 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
475 return 2;
476
477 val = INTVAL (XEXP (x, 1));
478
479 /* Do it directly. */
480 if (CONST_OK_FOR_K (val) || CONST_OK_FOR_M (~val))
481 return 2;
482 /* Takes one instruction to load. */
483 else if (const_ok_for_mcore (val))
484 return 3;
485 /* Takes two instructions to load. */
486 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
487 return 4;
488
489 /* Takes a lrw to load. */
490 return 5;
491 }
492
493 /* What does an or cost - see and_cost(). */
494
495 static int
496 mcore_ior_cost (rtx x)
497 {
498 HOST_WIDE_INT val;
499
500 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
501 return 2;
502
503 val = INTVAL (XEXP (x, 1));
504
505 /* Do it directly with bclri. */
506 if (CONST_OK_FOR_M (val))
507 return 2;
508 /* Takes one instruction to load. */
509 else if (const_ok_for_mcore (val))
510 return 3;
511 /* Takes two instructions to load. */
512 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
513 return 4;
514
515 /* Takes a lrw to load. */
516 return 5;
517 }
518
519 static bool
520 mcore_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
521 int * total, bool speed ATTRIBUTE_UNUSED)
522 {
523 switch (code)
524 {
525 case CONST_INT:
526 *total = mcore_const_costs (x, (enum rtx_code) outer_code);
527 return true;
528 case CONST:
529 case LABEL_REF:
530 case SYMBOL_REF:
531 *total = 5;
532 return true;
533 case CONST_DOUBLE:
534 *total = 10;
535 return true;
536
537 case AND:
538 *total = COSTS_N_INSNS (mcore_and_cost (x));
539 return true;
540
541 case IOR:
542 *total = COSTS_N_INSNS (mcore_ior_cost (x));
543 return true;
544
545 case DIV:
546 case UDIV:
547 case MOD:
548 case UMOD:
549 case FLOAT:
550 case FIX:
551 *total = COSTS_N_INSNS (100);
552 return true;
553
554 default:
555 return false;
556 }
557 }
558
559 /* Prepare the operands for a comparison. Return whether the branch/setcc
560 should reverse the operands. */
561
562 bool
563 mcore_gen_compare (enum rtx_code code, rtx op0, rtx op1)
564 {
565 rtx cc_reg = gen_rtx_REG (CCmode, CC_REG);
566 bool invert;
567
568 if (GET_CODE (op1) == CONST_INT)
569 {
570 HOST_WIDE_INT val = INTVAL (op1);
571
572 switch (code)
573 {
574 case GTU:
575 /* Unsigned > 0 is the same as != 0; everything else is converted
576 below to LEU (reversed cmphs). */
577 if (val == 0)
578 code = NE;
579 break;
580
581 /* Check whether (LE A imm) can become (LT A imm + 1),
582 or (GT A imm) can become (GE A imm + 1). */
583 case GT:
584 case LE:
585 if (CONST_OK_FOR_J (val + 1))
586 {
587 op1 = GEN_INT (val + 1);
588 code = code == LE ? LT : GE;
589 }
590 break;
591
592 default:
593 break;
594 }
595 }
596
597 if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
598 op1 = force_reg (SImode, op1);
599
600 /* cmpnei: 0-31 (K immediate)
601 cmplti: 1-32 (J immediate, 0 using btsti x,31). */
602 invert = false;
603 switch (code)
604 {
605 case EQ: /* Use inverted condition, cmpne. */
606 code = NE;
607 invert = true;
608 /* Drop through. */
609
610 case NE: /* Use normal condition, cmpne. */
611 if (GET_CODE (op1) == CONST_INT && ! CONST_OK_FOR_K (INTVAL (op1)))
612 op1 = force_reg (SImode, op1);
613 break;
614
615 case LE: /* Use inverted condition, reversed cmplt. */
616 code = GT;
617 invert = true;
618 /* Drop through. */
619
620 case GT: /* Use normal condition, reversed cmplt. */
621 if (GET_CODE (op1) == CONST_INT)
622 op1 = force_reg (SImode, op1);
623 break;
624
625 case GE: /* Use inverted condition, cmplt. */
626 code = LT;
627 invert = true;
628 /* Drop through. */
629
630 case LT: /* Use normal condition, cmplt. */
631 if (GET_CODE (op1) == CONST_INT &&
632 /* covered by btsti x,31. */
633 INTVAL (op1) != 0 &&
634 ! CONST_OK_FOR_J (INTVAL (op1)))
635 op1 = force_reg (SImode, op1);
636 break;
637
638 case GTU: /* Use inverted condition, cmple. */
639 /* We coped with unsigned > 0 above. */
640 gcc_assert (GET_CODE (op1) != CONST_INT || INTVAL (op1) != 0);
641 code = LEU;
642 invert = true;
643 /* Drop through. */
644
645 case LEU: /* Use normal condition, reversed cmphs. */
646 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
647 op1 = force_reg (SImode, op1);
648 break;
649
650 case LTU: /* Use inverted condition, cmphs. */
651 code = GEU;
652 invert = true;
653 /* Drop through. */
654
655 case GEU: /* Use normal condition, cmphs. */
656 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
657 op1 = force_reg (SImode, op1);
658 break;
659
660 default:
661 break;
662 }
663
664 emit_insn (gen_rtx_SET (VOIDmode,
665 cc_reg,
666 gen_rtx_fmt_ee (code, CCmode, op0, op1)));
667 return invert;
668 }
669
670 int
671 mcore_symbolic_address_p (rtx x)
672 {
673 switch (GET_CODE (x))
674 {
675 case SYMBOL_REF:
676 case LABEL_REF:
677 return 1;
678 case CONST:
679 x = XEXP (x, 0);
680 return ( (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
681 || GET_CODE (XEXP (x, 0)) == LABEL_REF)
682 && GET_CODE (XEXP (x, 1)) == CONST_INT);
683 default:
684 return 0;
685 }
686 }
687
688 /* Functions to output assembly code for a function call. */
689
690 char *
691 mcore_output_call (rtx operands[], int index)
692 {
693 static char buffer[20];
694 rtx addr = operands [index];
695
696 if (REG_P (addr))
697 {
698 if (TARGET_CG_DATA)
699 {
700 gcc_assert (mcore_current_function_name);
701
702 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
703 "unknown", 1);
704 }
705
706 sprintf (buffer, "jsr\t%%%d", index);
707 }
708 else
709 {
710 if (TARGET_CG_DATA)
711 {
712 gcc_assert (mcore_current_function_name);
713 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
714
715 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
716 XSTR (addr, 0), 0);
717 }
718
719 sprintf (buffer, "jbsr\t%%%d", index);
720 }
721
722 return buffer;
723 }
724
725 /* Can we load a constant with a single instruction ? */
726
727 int
728 const_ok_for_mcore (HOST_WIDE_INT value)
729 {
730 if (value >= 0 && value <= 127)
731 return 1;
732
733 /* Try exact power of two. */
734 if (CONST_OK_FOR_M (value))
735 return 1;
736
737 /* Try exact power of two - 1. */
738 if (CONST_OK_FOR_N (value) && value != -1)
739 return 1;
740
741 return 0;
742 }
743
744 /* Can we load a constant inline with up to 2 instructions ? */
745
746 int
747 mcore_const_ok_for_inline (HOST_WIDE_INT value)
748 {
749 HOST_WIDE_INT x, y;
750
751 return try_constant_tricks (value, & x, & y) > 0;
752 }
753
754 /* Are we loading the constant using a not ? */
755
756 int
757 mcore_const_trick_uses_not (HOST_WIDE_INT value)
758 {
759 HOST_WIDE_INT x, y;
760
761 return try_constant_tricks (value, & x, & y) == 2;
762 }
763
764 /* Try tricks to load a constant inline and return the trick number if
765 success (0 is non-inlinable).
766
767 0: not inlinable
768 1: single instruction (do the usual thing)
769 2: single insn followed by a 'not'
770 3: single insn followed by a subi
771 4: single insn followed by an addi
772 5: single insn followed by rsubi
773 6: single insn followed by bseti
774 7: single insn followed by bclri
775 8: single insn followed by rotli
776 9: single insn followed by lsli
777 10: single insn followed by ixh
778 11: single insn followed by ixw. */
779
780 static int
781 try_constant_tricks (HOST_WIDE_INT value, HOST_WIDE_INT * x, HOST_WIDE_INT * y)
782 {
783 HOST_WIDE_INT i;
784 unsigned HOST_WIDE_INT bit, shf, rot;
785
786 if (const_ok_for_mcore (value))
787 return 1; /* Do the usual thing. */
788
789 if (! TARGET_HARDLIT)
790 return 0;
791
792 if (const_ok_for_mcore (~value))
793 {
794 *x = ~value;
795 return 2;
796 }
797
798 for (i = 1; i <= 32; i++)
799 {
800 if (const_ok_for_mcore (value - i))
801 {
802 *x = value - i;
803 *y = i;
804
805 return 3;
806 }
807
808 if (const_ok_for_mcore (value + i))
809 {
810 *x = value + i;
811 *y = i;
812
813 return 4;
814 }
815 }
816
817 bit = 0x80000000ULL;
818
819 for (i = 0; i <= 31; i++)
820 {
821 if (const_ok_for_mcore (i - value))
822 {
823 *x = i - value;
824 *y = i;
825
826 return 5;
827 }
828
829 if (const_ok_for_mcore (value & ~bit))
830 {
831 *y = bit;
832 *x = value & ~bit;
833 return 6;
834 }
835
836 if (const_ok_for_mcore (value | bit))
837 {
838 *y = ~bit;
839 *x = value | bit;
840
841 return 7;
842 }
843
844 bit >>= 1;
845 }
846
847 shf = value;
848 rot = value;
849
850 for (i = 1; i < 31; i++)
851 {
852 int c;
853
854 /* MCore has rotate left. */
855 c = rot << 31;
856 rot >>= 1;
857 rot &= 0x7FFFFFFF;
858 rot |= c; /* Simulate rotate. */
859
860 if (const_ok_for_mcore (rot))
861 {
862 *y = i;
863 *x = rot;
864
865 return 8;
866 }
867
868 if (shf & 1)
869 shf = 0; /* Can't use logical shift, low order bit is one. */
870
871 shf >>= 1;
872
873 if (shf != 0 && const_ok_for_mcore (shf))
874 {
875 *y = i;
876 *x = shf;
877
878 return 9;
879 }
880 }
881
882 if ((value % 3) == 0 && const_ok_for_mcore (value / 3))
883 {
884 *x = value / 3;
885
886 return 10;
887 }
888
889 if ((value % 5) == 0 && const_ok_for_mcore (value / 5))
890 {
891 *x = value / 5;
892
893 return 11;
894 }
895
896 return 0;
897 }
898
899 /* Check whether reg is dead at first. This is done by searching ahead
900 for either the next use (i.e., reg is live), a death note, or a set of
901 reg. Don't just use dead_or_set_p() since reload does not always mark
902 deaths (especially if PRESERVE_DEATH_NOTES_REGNO_P is not defined). We
903 can ignore subregs by extracting the actual register. BRC */
904
905 int
906 mcore_is_dead (rtx first, rtx reg)
907 {
908 rtx insn;
909
910 /* For mcore, subregs can't live independently of their parent regs. */
911 if (GET_CODE (reg) == SUBREG)
912 reg = SUBREG_REG (reg);
913
914 /* Dies immediately. */
915 if (dead_or_set_p (first, reg))
916 return 1;
917
918 /* Look for conclusive evidence of live/death, otherwise we have
919 to assume that it is live. */
920 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
921 {
922 if (JUMP_P (insn))
923 return 0; /* We lose track, assume it is alive. */
924
925 else if (CALL_P (insn))
926 {
927 /* Call's might use it for target or register parms. */
928 if (reg_referenced_p (reg, PATTERN (insn))
929 || find_reg_fusage (insn, USE, reg))
930 return 0;
931 else if (dead_or_set_p (insn, reg))
932 return 1;
933 }
934 else if (NONJUMP_INSN_P (insn))
935 {
936 if (reg_referenced_p (reg, PATTERN (insn)))
937 return 0;
938 else if (dead_or_set_p (insn, reg))
939 return 1;
940 }
941 }
942
943 /* No conclusive evidence either way, we cannot take the chance
944 that control flow hid the use from us -- "I'm not dead yet". */
945 return 0;
946 }
947
948 /* Count the number of ones in mask. */
949
950 int
951 mcore_num_ones (HOST_WIDE_INT mask)
952 {
953 /* A trick to count set bits recently posted on comp.compilers. */
954 mask = (mask >> 1 & 0x55555555) + (mask & 0x55555555);
955 mask = ((mask >> 2) & 0x33333333) + (mask & 0x33333333);
956 mask = ((mask >> 4) + mask) & 0x0f0f0f0f;
957 mask = ((mask >> 8) + mask);
958
959 return (mask + (mask >> 16)) & 0xff;
960 }
961
962 /* Count the number of zeros in mask. */
963
964 int
965 mcore_num_zeros (HOST_WIDE_INT mask)
966 {
967 return 32 - mcore_num_ones (mask);
968 }
969
970 /* Determine byte being masked. */
971
972 int
973 mcore_byte_offset (unsigned int mask)
974 {
975 if (mask == 0x00ffffffL)
976 return 0;
977 else if (mask == 0xff00ffffL)
978 return 1;
979 else if (mask == 0xffff00ffL)
980 return 2;
981 else if (mask == 0xffffff00L)
982 return 3;
983
984 return -1;
985 }
986
987 /* Determine halfword being masked. */
988
989 int
990 mcore_halfword_offset (unsigned int mask)
991 {
992 if (mask == 0x0000ffffL)
993 return 0;
994 else if (mask == 0xffff0000L)
995 return 1;
996
997 return -1;
998 }
999
1000 /* Output a series of bseti's corresponding to mask. */
1001
1002 const char *
1003 mcore_output_bseti (rtx dst, int mask)
1004 {
1005 rtx out_operands[2];
1006 int bit;
1007
1008 out_operands[0] = dst;
1009
1010 for (bit = 0; bit < 32; bit++)
1011 {
1012 if ((mask & 0x1) == 0x1)
1013 {
1014 out_operands[1] = GEN_INT (bit);
1015
1016 output_asm_insn ("bseti\t%0,%1", out_operands);
1017 }
1018 mask >>= 1;
1019 }
1020
1021 return "";
1022 }
1023
1024 /* Output a series of bclri's corresponding to mask. */
1025
1026 const char *
1027 mcore_output_bclri (rtx dst, int mask)
1028 {
1029 rtx out_operands[2];
1030 int bit;
1031
1032 out_operands[0] = dst;
1033
1034 for (bit = 0; bit < 32; bit++)
1035 {
1036 if ((mask & 0x1) == 0x0)
1037 {
1038 out_operands[1] = GEN_INT (bit);
1039
1040 output_asm_insn ("bclri\t%0,%1", out_operands);
1041 }
1042
1043 mask >>= 1;
1044 }
1045
1046 return "";
1047 }
1048
1049 /* Output a conditional move of two constants that are +/- 1 within each
1050 other. See the "movtK" patterns in mcore.md. I'm not sure this is
1051 really worth the effort. */
1052
1053 const char *
1054 mcore_output_cmov (rtx operands[], int cmp_t, const char * test)
1055 {
1056 HOST_WIDE_INT load_value;
1057 HOST_WIDE_INT adjust_value;
1058 rtx out_operands[4];
1059
1060 out_operands[0] = operands[0];
1061
1062 /* Check to see which constant is loadable. */
1063 if (const_ok_for_mcore (INTVAL (operands[1])))
1064 {
1065 out_operands[1] = operands[1];
1066 out_operands[2] = operands[2];
1067 }
1068 else if (const_ok_for_mcore (INTVAL (operands[2])))
1069 {
1070 out_operands[1] = operands[2];
1071 out_operands[2] = operands[1];
1072
1073 /* Complement test since constants are swapped. */
1074 cmp_t = (cmp_t == 0);
1075 }
1076 load_value = INTVAL (out_operands[1]);
1077 adjust_value = INTVAL (out_operands[2]);
1078
1079 /* First output the test if folded into the pattern. */
1080
1081 if (test)
1082 output_asm_insn (test, operands);
1083
1084 /* Load the constant - for now, only support constants that can be
1085 generated with a single instruction. maybe add general inlinable
1086 constants later (this will increase the # of patterns since the
1087 instruction sequence has a different length attribute). */
1088 if (load_value >= 0 && load_value <= 127)
1089 output_asm_insn ("movi\t%0,%1", out_operands);
1090 else if (CONST_OK_FOR_M (load_value))
1091 output_asm_insn ("bgeni\t%0,%P1", out_operands);
1092 else if (CONST_OK_FOR_N (load_value))
1093 output_asm_insn ("bmaski\t%0,%N1", out_operands);
1094
1095 /* Output the constant adjustment. */
1096 if (load_value > adjust_value)
1097 {
1098 if (cmp_t)
1099 output_asm_insn ("decf\t%0", out_operands);
1100 else
1101 output_asm_insn ("dect\t%0", out_operands);
1102 }
1103 else
1104 {
1105 if (cmp_t)
1106 output_asm_insn ("incf\t%0", out_operands);
1107 else
1108 output_asm_insn ("inct\t%0", out_operands);
1109 }
1110
1111 return "";
1112 }
1113
1114 /* Outputs the peephole for moving a constant that gets not'ed followed
1115 by an and (i.e. combine the not and the and into andn). BRC */
1116
1117 const char *
1118 mcore_output_andn (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
1119 {
1120 HOST_WIDE_INT x, y;
1121 rtx out_operands[3];
1122 const char * load_op;
1123 char buf[256];
1124 int trick_no;
1125
1126 trick_no = try_constant_tricks (INTVAL (operands[1]), &x, &y);
1127 gcc_assert (trick_no == 2);
1128
1129 out_operands[0] = operands[0];
1130 out_operands[1] = GEN_INT (x);
1131 out_operands[2] = operands[2];
1132
1133 if (x >= 0 && x <= 127)
1134 load_op = "movi\t%0,%1";
1135
1136 /* Try exact power of two. */
1137 else if (CONST_OK_FOR_M (x))
1138 load_op = "bgeni\t%0,%P1";
1139
1140 /* Try exact power of two - 1. */
1141 else if (CONST_OK_FOR_N (x))
1142 load_op = "bmaski\t%0,%N1";
1143
1144 else
1145 {
1146 load_op = "BADMOVI-andn\t%0, %1";
1147 gcc_unreachable ();
1148 }
1149
1150 sprintf (buf, "%s\n\tandn\t%%2,%%0", load_op);
1151 output_asm_insn (buf, out_operands);
1152
1153 return "";
1154 }
1155
1156 /* Output an inline constant. */
1157
1158 static const char *
1159 output_inline_const (enum machine_mode mode, rtx operands[])
1160 {
1161 HOST_WIDE_INT x = 0, y = 0;
1162 int trick_no;
1163 rtx out_operands[3];
1164 char buf[256];
1165 char load_op[256];
1166 const char *dst_fmt;
1167 HOST_WIDE_INT value;
1168
1169 value = INTVAL (operands[1]);
1170
1171 trick_no = try_constant_tricks (value, &x, &y);
1172 /* lrw's are handled separately: Large inlinable constants never get
1173 turned into lrw's. Our caller uses try_constant_tricks to back
1174 off to an lrw rather than calling this routine. */
1175 gcc_assert (trick_no != 0);
1176
1177 if (trick_no == 1)
1178 x = value;
1179
1180 /* operands: 0 = dst, 1 = load immed., 2 = immed. adjustment. */
1181 out_operands[0] = operands[0];
1182 out_operands[1] = GEN_INT (x);
1183
1184 if (trick_no > 2)
1185 out_operands[2] = GEN_INT (y);
1186
1187 /* Select dst format based on mode. */
1188 if (mode == DImode && (! TARGET_LITTLE_END))
1189 dst_fmt = "%R0";
1190 else
1191 dst_fmt = "%0";
1192
1193 if (x >= 0 && x <= 127)
1194 sprintf (load_op, "movi\t%s,%%1", dst_fmt);
1195
1196 /* Try exact power of two. */
1197 else if (CONST_OK_FOR_M (x))
1198 sprintf (load_op, "bgeni\t%s,%%P1", dst_fmt);
1199
1200 /* Try exact power of two - 1. */
1201 else if (CONST_OK_FOR_N (x))
1202 sprintf (load_op, "bmaski\t%s,%%N1", dst_fmt);
1203
1204 else
1205 {
1206 sprintf (load_op, "BADMOVI-inline_const %s, %%1", dst_fmt);
1207 gcc_unreachable ();
1208 }
1209
1210 switch (trick_no)
1211 {
1212 case 1:
1213 strcpy (buf, load_op);
1214 break;
1215 case 2: /* not */
1216 sprintf (buf, "%s\n\tnot\t%s\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1217 break;
1218 case 3: /* add */
1219 sprintf (buf, "%s\n\taddi\t%s,%%2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1220 break;
1221 case 4: /* sub */
1222 sprintf (buf, "%s\n\tsubi\t%s,%%2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1223 break;
1224 case 5: /* rsub */
1225 /* Never happens unless -mrsubi, see try_constant_tricks(). */
1226 sprintf (buf, "%s\n\trsubi\t%s,%%2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1227 break;
1228 case 6: /* bseti */
1229 sprintf (buf, "%s\n\tbseti\t%s,%%P2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1230 break;
1231 case 7: /* bclr */
1232 sprintf (buf, "%s\n\tbclri\t%s,%%Q2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1233 break;
1234 case 8: /* rotl */
1235 sprintf (buf, "%s\n\trotli\t%s,%%2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1236 break;
1237 case 9: /* lsl */
1238 sprintf (buf, "%s\n\tlsli\t%s,%%2\t// %ld 0x%lx", load_op, dst_fmt, value, value);
1239 break;
1240 case 10: /* ixh */
1241 sprintf (buf, "%s\n\tixh\t%s,%s\t// %ld 0x%lx", load_op, dst_fmt, dst_fmt, value, value);
1242 break;
1243 case 11: /* ixw */
1244 sprintf (buf, "%s\n\tixw\t%s,%s\t// %ld 0x%lx", load_op, dst_fmt, dst_fmt, value, value);
1245 break;
1246 default:
1247 return "";
1248 }
1249
1250 output_asm_insn (buf, out_operands);
1251
1252 return "";
1253 }
1254
1255 /* Output a move of a word or less value. */
1256
1257 const char *
1258 mcore_output_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
1259 enum machine_mode mode ATTRIBUTE_UNUSED)
1260 {
1261 rtx dst = operands[0];
1262 rtx src = operands[1];
1263
1264 if (GET_CODE (dst) == REG)
1265 {
1266 if (GET_CODE (src) == REG)
1267 {
1268 if (REGNO (src) == CC_REG) /* r-c */
1269 return "mvc\t%0";
1270 else
1271 return "mov\t%0,%1"; /* r-r*/
1272 }
1273 else if (GET_CODE (src) == MEM)
1274 {
1275 if (GET_CODE (XEXP (src, 0)) == LABEL_REF)
1276 return "lrw\t%0,[%1]"; /* a-R */
1277 else
1278 switch (GET_MODE (src)) /* r-m */
1279 {
1280 case SImode:
1281 return "ldw\t%0,%1";
1282 case HImode:
1283 return "ld.h\t%0,%1";
1284 case QImode:
1285 return "ld.b\t%0,%1";
1286 default:
1287 gcc_unreachable ();
1288 }
1289 }
1290 else if (GET_CODE (src) == CONST_INT)
1291 {
1292 HOST_WIDE_INT x, y;
1293
1294 if (CONST_OK_FOR_I (INTVAL (src))) /* r-I */
1295 return "movi\t%0,%1";
1296 else if (CONST_OK_FOR_M (INTVAL (src))) /* r-M */
1297 return "bgeni\t%0,%P1\t// %1 %x1";
1298 else if (CONST_OK_FOR_N (INTVAL (src))) /* r-N */
1299 return "bmaski\t%0,%N1\t// %1 %x1";
1300 else if (try_constant_tricks (INTVAL (src), &x, &y)) /* R-P */
1301 return output_inline_const (SImode, operands); /* 1-2 insns */
1302 else
1303 return "lrw\t%0,%x1\t// %1"; /* Get it from literal pool. */
1304 }
1305 else
1306 return "lrw\t%0, %1"; /* Into the literal pool. */
1307 }
1308 else if (GET_CODE (dst) == MEM) /* m-r */
1309 switch (GET_MODE (dst))
1310 {
1311 case SImode:
1312 return "stw\t%1,%0";
1313 case HImode:
1314 return "st.h\t%1,%0";
1315 case QImode:
1316 return "st.b\t%1,%0";
1317 default:
1318 gcc_unreachable ();
1319 }
1320
1321 gcc_unreachable ();
1322 }
1323
1324 /* Return a sequence of instructions to perform DI or DF move.
1325 Since the MCORE cannot move a DI or DF in one instruction, we have
1326 to take care when we see overlapping source and dest registers. */
1327
1328 const char *
1329 mcore_output_movedouble (rtx operands[], enum machine_mode mode ATTRIBUTE_UNUSED)
1330 {
1331 rtx dst = operands[0];
1332 rtx src = operands[1];
1333
1334 if (GET_CODE (dst) == REG)
1335 {
1336 if (GET_CODE (src) == REG)
1337 {
1338 int dstreg = REGNO (dst);
1339 int srcreg = REGNO (src);
1340
1341 /* Ensure the second source not overwritten. */
1342 if (srcreg + 1 == dstreg)
1343 return "mov %R0,%R1\n\tmov %0,%1";
1344 else
1345 return "mov %0,%1\n\tmov %R0,%R1";
1346 }
1347 else if (GET_CODE (src) == MEM)
1348 {
1349 rtx memexp = XEXP (src, 0);
1350 int dstreg = REGNO (dst);
1351 int basereg = -1;
1352
1353 if (GET_CODE (memexp) == LABEL_REF)
1354 return "lrw\t%0,[%1]\n\tlrw\t%R0,[%R1]";
1355 else if (GET_CODE (memexp) == REG)
1356 basereg = REGNO (memexp);
1357 else if (GET_CODE (memexp) == PLUS)
1358 {
1359 if (GET_CODE (XEXP (memexp, 0)) == REG)
1360 basereg = REGNO (XEXP (memexp, 0));
1361 else if (GET_CODE (XEXP (memexp, 1)) == REG)
1362 basereg = REGNO (XEXP (memexp, 1));
1363 else
1364 gcc_unreachable ();
1365 }
1366 else
1367 gcc_unreachable ();
1368
1369 /* ??? length attribute is wrong here. */
1370 if (dstreg == basereg)
1371 {
1372 /* Just load them in reverse order. */
1373 return "ldw\t%R0,%R1\n\tldw\t%0,%1";
1374
1375 /* XXX: alternative: move basereg to basereg+1
1376 and then fall through. */
1377 }
1378 else
1379 return "ldw\t%0,%1\n\tldw\t%R0,%R1";
1380 }
1381 else if (GET_CODE (src) == CONST_INT)
1382 {
1383 if (TARGET_LITTLE_END)
1384 {
1385 if (CONST_OK_FOR_I (INTVAL (src)))
1386 output_asm_insn ("movi %0,%1", operands);
1387 else if (CONST_OK_FOR_M (INTVAL (src)))
1388 output_asm_insn ("bgeni %0,%P1", operands);
1389 else if (CONST_OK_FOR_N (INTVAL (src)))
1390 output_asm_insn ("bmaski %0,%N1", operands);
1391 else
1392 gcc_unreachable ();
1393
1394 if (INTVAL (src) < 0)
1395 return "bmaski %R0,32";
1396 else
1397 return "movi %R0,0";
1398 }
1399 else
1400 {
1401 if (CONST_OK_FOR_I (INTVAL (src)))
1402 output_asm_insn ("movi %R0,%1", operands);
1403 else if (CONST_OK_FOR_M (INTVAL (src)))
1404 output_asm_insn ("bgeni %R0,%P1", operands);
1405 else if (CONST_OK_FOR_N (INTVAL (src)))
1406 output_asm_insn ("bmaski %R0,%N1", operands);
1407 else
1408 gcc_unreachable ();
1409
1410 if (INTVAL (src) < 0)
1411 return "bmaski %0,32";
1412 else
1413 return "movi %0,0";
1414 }
1415 }
1416 else
1417 gcc_unreachable ();
1418 }
1419 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
1420 return "stw\t%1,%0\n\tstw\t%R1,%R0";
1421 else
1422 gcc_unreachable ();
1423 }
1424
1425 /* Predicates used by the templates. */
1426
1427 int
1428 mcore_arith_S_operand (rtx op)
1429 {
1430 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (~INTVAL (op)))
1431 return 1;
1432
1433 return 0;
1434 }
1435
1436 /* Expand insert bit field. BRC */
1437
1438 int
1439 mcore_expand_insv (rtx operands[])
1440 {
1441 int width = INTVAL (operands[1]);
1442 int posn = INTVAL (operands[2]);
1443 int mask;
1444 rtx mreg, sreg, ereg;
1445
1446 /* To get width 1 insv, the test in store_bit_field() (expmed.c, line 191)
1447 for width==1 must be removed. Look around line 368. This is something
1448 we really want the md part to do. */
1449 if (width == 1 && GET_CODE (operands[3]) == CONST_INT)
1450 {
1451 /* Do directly with bseti or bclri. */
1452 /* RBE: 2/97 consider only low bit of constant. */
1453 if ((INTVAL (operands[3]) & 1) == 0)
1454 {
1455 mask = ~(1 << posn);
1456 emit_insn (gen_rtx_SET (SImode, operands[0],
1457 gen_rtx_AND (SImode, operands[0], GEN_INT (mask))));
1458 }
1459 else
1460 {
1461 mask = 1 << posn;
1462 emit_insn (gen_rtx_SET (SImode, operands[0],
1463 gen_rtx_IOR (SImode, operands[0], GEN_INT (mask))));
1464 }
1465
1466 return 1;
1467 }
1468
1469 /* Look at some bit-field placements that we aren't interested
1470 in handling ourselves, unless specifically directed to do so. */
1471 if (! TARGET_W_FIELD)
1472 return 0; /* Generally, give up about now. */
1473
1474 if (width == 8 && posn % 8 == 0)
1475 /* Byte sized and aligned; let caller break it up. */
1476 return 0;
1477
1478 if (width == 16 && posn % 16 == 0)
1479 /* Short sized and aligned; let caller break it up. */
1480 return 0;
1481
1482 /* The general case - we can do this a little bit better than what the
1483 machine independent part tries. This will get rid of all the subregs
1484 that mess up constant folding in combine when working with relaxed
1485 immediates. */
1486
1487 /* If setting the entire field, do it directly. */
1488 if (GET_CODE (operands[3]) == CONST_INT
1489 && INTVAL (operands[3]) == ((1 << width) - 1))
1490 {
1491 mreg = force_reg (SImode, GEN_INT (INTVAL (operands[3]) << posn));
1492 emit_insn (gen_rtx_SET (SImode, operands[0],
1493 gen_rtx_IOR (SImode, operands[0], mreg)));
1494 return 1;
1495 }
1496
1497 /* Generate the clear mask. */
1498 mreg = force_reg (SImode, GEN_INT (~(((1 << width) - 1) << posn)));
1499
1500 /* Clear the field, to overlay it later with the source. */
1501 emit_insn (gen_rtx_SET (SImode, operands[0],
1502 gen_rtx_AND (SImode, operands[0], mreg)));
1503
1504 /* If the source is constant 0, we've nothing to add back. */
1505 if (GET_CODE (operands[3]) == CONST_INT && INTVAL (operands[3]) == 0)
1506 return 1;
1507
1508 /* XXX: Should we worry about more games with constant values?
1509 We've covered the high profile: set/clear single-bit and many-bit
1510 fields. How often do we see "arbitrary bit pattern" constants? */
1511 sreg = copy_to_mode_reg (SImode, operands[3]);
1512
1513 /* Extract src as same width as dst (needed for signed values). We
1514 always have to do this since we widen everything to SImode.
1515 We don't have to mask if we're shifting this up against the
1516 MSB of the register (e.g., the shift will push out any hi-order
1517 bits. */
1518 if (width + posn != (int) GET_MODE_SIZE (SImode))
1519 {
1520 ereg = force_reg (SImode, GEN_INT ((1 << width) - 1));
1521 emit_insn (gen_rtx_SET (SImode, sreg,
1522 gen_rtx_AND (SImode, sreg, ereg)));
1523 }
1524
1525 /* Insert source value in dest. */
1526 if (posn != 0)
1527 emit_insn (gen_rtx_SET (SImode, sreg,
1528 gen_rtx_ASHIFT (SImode, sreg, GEN_INT (posn))));
1529
1530 emit_insn (gen_rtx_SET (SImode, operands[0],
1531 gen_rtx_IOR (SImode, operands[0], sreg)));
1532
1533 return 1;
1534 }
1535 \f
1536 /* ??? Block move stuff stolen from m88k. This code has not been
1537 verified for correctness. */
1538
1539 /* Emit code to perform a block move. Choose the best method.
1540
1541 OPERANDS[0] is the destination.
1542 OPERANDS[1] is the source.
1543 OPERANDS[2] is the size.
1544 OPERANDS[3] is the alignment safe to use. */
1545
1546 /* Emit code to perform a block move with an offset sequence of ldw/st
1547 instructions (..., ldw 0, stw 1, ldw 1, stw 0, ...). SIZE and ALIGN are
1548 known constants. DEST and SRC are registers. OFFSET is the known
1549 starting point for the output pattern. */
1550
1551 static const enum machine_mode mode_from_align[] =
1552 {
1553 VOIDmode, QImode, HImode, VOIDmode, SImode,
1554 };
1555
1556 static void
1557 block_move_sequence (rtx dst_mem, rtx src_mem, int size, int align)
1558 {
1559 rtx temp[2];
1560 enum machine_mode mode[2];
1561 int amount[2];
1562 bool active[2];
1563 int phase = 0;
1564 int next;
1565 int offset_ld = 0;
1566 int offset_st = 0;
1567 rtx x;
1568
1569 x = XEXP (dst_mem, 0);
1570 if (!REG_P (x))
1571 {
1572 x = force_reg (Pmode, x);
1573 dst_mem = replace_equiv_address (dst_mem, x);
1574 }
1575
1576 x = XEXP (src_mem, 0);
1577 if (!REG_P (x))
1578 {
1579 x = force_reg (Pmode, x);
1580 src_mem = replace_equiv_address (src_mem, x);
1581 }
1582
1583 active[0] = active[1] = false;
1584
1585 do
1586 {
1587 next = phase;
1588 phase ^= 1;
1589
1590 if (size > 0)
1591 {
1592 int next_amount;
1593
1594 next_amount = (size >= 4 ? 4 : (size >= 2 ? 2 : 1));
1595 next_amount = MIN (next_amount, align);
1596
1597 amount[next] = next_amount;
1598 mode[next] = mode_from_align[next_amount];
1599 temp[next] = gen_reg_rtx (mode[next]);
1600
1601 x = adjust_address (src_mem, mode[next], offset_ld);
1602 emit_insn (gen_rtx_SET (VOIDmode, temp[next], x));
1603
1604 offset_ld += next_amount;
1605 size -= next_amount;
1606 active[next] = true;
1607 }
1608
1609 if (active[phase])
1610 {
1611 active[phase] = false;
1612
1613 x = adjust_address (dst_mem, mode[phase], offset_st);
1614 emit_insn (gen_rtx_SET (VOIDmode, x, temp[phase]));
1615
1616 offset_st += amount[phase];
1617 }
1618 }
1619 while (active[next]);
1620 }
1621
1622 bool
1623 mcore_expand_block_move (rtx *operands)
1624 {
1625 HOST_WIDE_INT align, bytes, max;
1626
1627 if (GET_CODE (operands[2]) != CONST_INT)
1628 return false;
1629
1630 bytes = INTVAL (operands[2]);
1631 align = INTVAL (operands[3]);
1632
1633 if (bytes <= 0)
1634 return false;
1635 if (align > 4)
1636 align = 4;
1637
1638 switch (align)
1639 {
1640 case 4:
1641 if (bytes & 1)
1642 max = 4*4;
1643 else if (bytes & 3)
1644 max = 8*4;
1645 else
1646 max = 16*4;
1647 break;
1648 case 2:
1649 max = 4*2;
1650 break;
1651 case 1:
1652 max = 4*1;
1653 break;
1654 default:
1655 gcc_unreachable ();
1656 }
1657
1658 if (bytes <= max)
1659 {
1660 block_move_sequence (operands[0], operands[1], bytes, align);
1661 return true;
1662 }
1663
1664 return false;
1665 }
1666 \f
1667
1668 /* Code to generate prologue and epilogue sequences. */
1669 static int number_of_regs_before_varargs;
1670
1671 /* Set by TARGET_SETUP_INCOMING_VARARGS to indicate to prolog that this is
1672 for a varargs function. */
1673 static int current_function_anonymous_args;
1674
1675 #define STACK_BYTES (STACK_BOUNDARY/BITS_PER_UNIT)
1676 #define STORE_REACH (64) /* Maximum displace of word store + 4. */
1677 #define ADDI_REACH (32) /* Maximum addi operand. */
1678
1679 static void
1680 layout_mcore_frame (struct mcore_frame * infp)
1681 {
1682 int n;
1683 unsigned int i;
1684 int nbytes;
1685 int regarg;
1686 int localregarg;
1687 int outbounds;
1688 unsigned int growths;
1689 int step;
1690
1691 /* Might have to spill bytes to re-assemble a big argument that
1692 was passed partially in registers and partially on the stack. */
1693 nbytes = crtl->args.pretend_args_size;
1694
1695 /* Determine how much space for spilled anonymous args (e.g., stdarg). */
1696 if (current_function_anonymous_args)
1697 nbytes += (NPARM_REGS - number_of_regs_before_varargs) * UNITS_PER_WORD;
1698
1699 infp->arg_size = nbytes;
1700
1701 /* How much space to save non-volatile registers we stomp. */
1702 infp->reg_mask = calc_live_regs (& n);
1703 infp->reg_size = n * 4;
1704
1705 /* And the rest of it... locals and space for overflowed outbounds. */
1706 infp->local_size = get_frame_size ();
1707 infp->outbound_size = crtl->outgoing_args_size;
1708
1709 /* Make sure we have a whole number of words for the locals. */
1710 if (infp->local_size % STACK_BYTES)
1711 infp->local_size = (infp->local_size + STACK_BYTES - 1) & ~ (STACK_BYTES -1);
1712
1713 /* Only thing we know we have to pad is the outbound space, since
1714 we've aligned our locals assuming that base of locals is aligned. */
1715 infp->pad_local = 0;
1716 infp->pad_reg = 0;
1717 infp->pad_outbound = 0;
1718 if (infp->outbound_size % STACK_BYTES)
1719 infp->pad_outbound = STACK_BYTES - (infp->outbound_size % STACK_BYTES);
1720
1721 /* Now we see how we want to stage the prologue so that it does
1722 the most appropriate stack growth and register saves to either:
1723 (1) run fast,
1724 (2) reduce instruction space, or
1725 (3) reduce stack space. */
1726 for (i = 0; i < ARRAY_SIZE (infp->growth); i++)
1727 infp->growth[i] = 0;
1728
1729 regarg = infp->reg_size + infp->arg_size;
1730 localregarg = infp->local_size + regarg;
1731 outbounds = infp->outbound_size + infp->pad_outbound;
1732 growths = 0;
1733
1734 /* XXX: Consider one where we consider localregarg + outbound too! */
1735
1736 /* Frame of <= 32 bytes and using stm would get <= 2 registers.
1737 use stw's with offsets and buy the frame in one shot. */
1738 if (localregarg <= ADDI_REACH
1739 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
1740 {
1741 /* Make sure we'll be aligned. */
1742 if (localregarg % STACK_BYTES)
1743 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
1744
1745 step = localregarg + infp->pad_reg;
1746 infp->reg_offset = infp->local_size;
1747
1748 if (outbounds + step <= ADDI_REACH && !frame_pointer_needed)
1749 {
1750 step += outbounds;
1751 infp->reg_offset += outbounds;
1752 outbounds = 0;
1753 }
1754
1755 infp->arg_offset = step - 4;
1756 infp->growth[growths++] = step;
1757 infp->reg_growth = growths;
1758 infp->local_growth = growths;
1759
1760 /* If we haven't already folded it in. */
1761 if (outbounds)
1762 infp->growth[growths++] = outbounds;
1763
1764 goto finish;
1765 }
1766
1767 /* Frame can't be done with a single subi, but can be done with 2
1768 insns. If the 'stm' is getting <= 2 registers, we use stw's and
1769 shift some of the stack purchase into the first subi, so both are
1770 single instructions. */
1771 if (localregarg <= STORE_REACH
1772 && (infp->local_size > ADDI_REACH)
1773 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
1774 {
1775 int all;
1776
1777 /* Make sure we'll be aligned; use either pad_reg or pad_local. */
1778 if (localregarg % STACK_BYTES)
1779 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
1780
1781 all = localregarg + infp->pad_reg + infp->pad_local;
1782 step = ADDI_REACH; /* As much up front as we can. */
1783 if (step > all)
1784 step = all;
1785
1786 /* XXX: Consider whether step will still be aligned; we believe so. */
1787 infp->arg_offset = step - 4;
1788 infp->growth[growths++] = step;
1789 infp->reg_growth = growths;
1790 infp->reg_offset = step - infp->pad_reg - infp->reg_size;
1791 all -= step;
1792
1793 /* Can we fold in any space required for outbounds? */
1794 if (outbounds + all <= ADDI_REACH && !frame_pointer_needed)
1795 {
1796 all += outbounds;
1797 outbounds = 0;
1798 }
1799
1800 /* Get the rest of the locals in place. */
1801 step = all;
1802 infp->growth[growths++] = step;
1803 infp->local_growth = growths;
1804 all -= step;
1805
1806 gcc_assert (all == 0);
1807
1808 /* Finish off if we need to do so. */
1809 if (outbounds)
1810 infp->growth[growths++] = outbounds;
1811
1812 goto finish;
1813 }
1814
1815 /* Registers + args is nicely aligned, so we'll buy that in one shot.
1816 Then we buy the rest of the frame in 1 or 2 steps depending on
1817 whether we need a frame pointer. */
1818 if ((regarg % STACK_BYTES) == 0)
1819 {
1820 infp->growth[growths++] = regarg;
1821 infp->reg_growth = growths;
1822 infp->arg_offset = regarg - 4;
1823 infp->reg_offset = 0;
1824
1825 if (infp->local_size % STACK_BYTES)
1826 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
1827
1828 step = infp->local_size + infp->pad_local;
1829
1830 if (!frame_pointer_needed)
1831 {
1832 step += outbounds;
1833 outbounds = 0;
1834 }
1835
1836 infp->growth[growths++] = step;
1837 infp->local_growth = growths;
1838
1839 /* If there's any left to be done. */
1840 if (outbounds)
1841 infp->growth[growths++] = outbounds;
1842
1843 goto finish;
1844 }
1845
1846 /* XXX: optimizations that we'll want to play with....
1847 -- regarg is not aligned, but it's a small number of registers;
1848 use some of localsize so that regarg is aligned and then
1849 save the registers. */
1850
1851 /* Simple encoding; plods down the stack buying the pieces as it goes.
1852 -- does not optimize space consumption.
1853 -- does not attempt to optimize instruction counts.
1854 -- but it is safe for all alignments. */
1855 if (regarg % STACK_BYTES != 0)
1856 infp->pad_reg = STACK_BYTES - (regarg % STACK_BYTES);
1857
1858 infp->growth[growths++] = infp->arg_size + infp->reg_size + infp->pad_reg;
1859 infp->reg_growth = growths;
1860 infp->arg_offset = infp->growth[0] - 4;
1861 infp->reg_offset = 0;
1862
1863 if (frame_pointer_needed)
1864 {
1865 if (infp->local_size % STACK_BYTES != 0)
1866 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
1867
1868 infp->growth[growths++] = infp->local_size + infp->pad_local;
1869 infp->local_growth = growths;
1870
1871 infp->growth[growths++] = outbounds;
1872 }
1873 else
1874 {
1875 if ((infp->local_size + outbounds) % STACK_BYTES != 0)
1876 infp->pad_local = STACK_BYTES - ((infp->local_size + outbounds) % STACK_BYTES);
1877
1878 infp->growth[growths++] = infp->local_size + infp->pad_local + outbounds;
1879 infp->local_growth = growths;
1880 }
1881
1882 /* Anything else that we've forgotten?, plus a few consistency checks. */
1883 finish:
1884 gcc_assert (infp->reg_offset >= 0);
1885 gcc_assert (growths <= MAX_STACK_GROWS);
1886
1887 for (i = 0; i < growths; i++)
1888 gcc_assert (!(infp->growth[i] % STACK_BYTES));
1889 }
1890
1891 /* Define the offset between two registers, one to be eliminated, and
1892 the other its replacement, at the start of a routine. */
1893
1894 int
1895 mcore_initial_elimination_offset (int from, int to)
1896 {
1897 int above_frame;
1898 int below_frame;
1899 struct mcore_frame fi;
1900
1901 layout_mcore_frame (& fi);
1902
1903 /* fp to ap */
1904 above_frame = fi.local_size + fi.pad_local + fi.reg_size + fi.pad_reg;
1905 /* sp to fp */
1906 below_frame = fi.outbound_size + fi.pad_outbound;
1907
1908 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1909 return above_frame;
1910
1911 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1912 return above_frame + below_frame;
1913
1914 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1915 return below_frame;
1916
1917 gcc_unreachable ();
1918 }
1919
1920 /* Keep track of some information about varargs for the prolog. */
1921
1922 static void
1923 mcore_setup_incoming_varargs (cumulative_args_t args_so_far_v,
1924 enum machine_mode mode, tree type,
1925 int * ptr_pretend_size ATTRIBUTE_UNUSED,
1926 int second_time ATTRIBUTE_UNUSED)
1927 {
1928 CUMULATIVE_ARGS *args_so_far = get_cumulative_args (args_so_far_v);
1929
1930 current_function_anonymous_args = 1;
1931
1932 /* We need to know how many argument registers are used before
1933 the varargs start, so that we can push the remaining argument
1934 registers during the prologue. */
1935 number_of_regs_before_varargs = *args_so_far + mcore_num_arg_regs (mode, type);
1936
1937 /* There is a bug somewhere in the arg handling code.
1938 Until I can find it this workaround always pushes the
1939 last named argument onto the stack. */
1940 number_of_regs_before_varargs = *args_so_far;
1941
1942 /* The last named argument may be split between argument registers
1943 and the stack. Allow for this here. */
1944 if (number_of_regs_before_varargs > NPARM_REGS)
1945 number_of_regs_before_varargs = NPARM_REGS;
1946 }
1947
1948 void
1949 mcore_expand_prolog (void)
1950 {
1951 struct mcore_frame fi;
1952 int space_allocated = 0;
1953 int growth = 0;
1954
1955 /* Find out what we're doing. */
1956 layout_mcore_frame (&fi);
1957
1958 space_allocated = fi.arg_size + fi.reg_size + fi.local_size +
1959 fi.outbound_size + fi.pad_outbound + fi.pad_local + fi.pad_reg;
1960
1961 if (TARGET_CG_DATA)
1962 {
1963 /* Emit a symbol for this routine's frame size. */
1964 rtx x;
1965
1966 x = DECL_RTL (current_function_decl);
1967
1968 gcc_assert (GET_CODE (x) == MEM);
1969
1970 x = XEXP (x, 0);
1971
1972 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1973
1974 free (mcore_current_function_name);
1975
1976 mcore_current_function_name = xstrdup (XSTR (x, 0));
1977
1978 ASM_OUTPUT_CG_NODE (asm_out_file, mcore_current_function_name, space_allocated);
1979
1980 if (cfun->calls_alloca)
1981 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, "alloca", 1);
1982
1983 /* 970425: RBE:
1984 We're looking at how the 8byte alignment affects stack layout
1985 and where we had to pad things. This emits information we can
1986 extract which tells us about frame sizes and the like. */
1987 fprintf (asm_out_file,
1988 "\t.equ\t__$frame$info$_%s_$_%d_%d_x%x_%d_%d_%d,0\n",
1989 mcore_current_function_name,
1990 fi.arg_size, fi.reg_size, fi.reg_mask,
1991 fi.local_size, fi.outbound_size,
1992 frame_pointer_needed);
1993 }
1994
1995 if (mcore_naked_function_p ())
1996 return;
1997
1998 /* Handle stdarg+regsaves in one shot: can't be more than 64 bytes. */
1999 output_stack_adjust (-1, fi.growth[growth++]); /* Grows it. */
2000
2001 /* If we have a parameter passed partially in regs and partially in memory,
2002 the registers will have been stored to memory already in function.c. So
2003 we only need to do something here for varargs functions. */
2004 if (fi.arg_size != 0 && crtl->args.pretend_args_size == 0)
2005 {
2006 int offset;
2007 int rn = FIRST_PARM_REG + NPARM_REGS - 1;
2008 int remaining = fi.arg_size;
2009
2010 for (offset = fi.arg_offset; remaining >= 4; offset -= 4, rn--, remaining -= 4)
2011 {
2012 emit_insn (gen_movsi
2013 (gen_rtx_MEM (SImode,
2014 plus_constant (Pmode, stack_pointer_rtx,
2015 offset)),
2016 gen_rtx_REG (SImode, rn)));
2017 }
2018 }
2019
2020 /* Do we need another stack adjustment before we do the register saves? */
2021 if (growth < fi.reg_growth)
2022 output_stack_adjust (-1, fi.growth[growth++]); /* Grows it. */
2023
2024 if (fi.reg_size != 0)
2025 {
2026 int i;
2027 int offs = fi.reg_offset;
2028
2029 for (i = 15; i >= 0; i--)
2030 {
2031 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2032 {
2033 int first_reg = 15;
2034
2035 while (fi.reg_mask & (1 << first_reg))
2036 first_reg--;
2037 first_reg++;
2038
2039 emit_insn (gen_store_multiple (gen_rtx_MEM (SImode, stack_pointer_rtx),
2040 gen_rtx_REG (SImode, first_reg),
2041 GEN_INT (16 - first_reg)));
2042
2043 i -= (15 - first_reg);
2044 offs += (16 - first_reg) * 4;
2045 }
2046 else if (fi.reg_mask & (1 << i))
2047 {
2048 emit_insn (gen_movsi
2049 (gen_rtx_MEM (SImode,
2050 plus_constant (Pmode, stack_pointer_rtx,
2051 offs)),
2052 gen_rtx_REG (SImode, i)));
2053 offs += 4;
2054 }
2055 }
2056 }
2057
2058 /* Figure the locals + outbounds. */
2059 if (frame_pointer_needed)
2060 {
2061 /* If we haven't already purchased to 'fp'. */
2062 if (growth < fi.local_growth)
2063 output_stack_adjust (-1, fi.growth[growth++]); /* Grows it. */
2064
2065 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2066
2067 /* ... and then go any remaining distance for outbounds, etc. */
2068 if (fi.growth[growth])
2069 output_stack_adjust (-1, fi.growth[growth++]);
2070 }
2071 else
2072 {
2073 if (growth < fi.local_growth)
2074 output_stack_adjust (-1, fi.growth[growth++]); /* Grows it. */
2075 if (fi.growth[growth])
2076 output_stack_adjust (-1, fi.growth[growth++]);
2077 }
2078 }
2079
2080 void
2081 mcore_expand_epilog (void)
2082 {
2083 struct mcore_frame fi;
2084 int i;
2085 int offs;
2086 int growth = MAX_STACK_GROWS - 1 ;
2087
2088
2089 /* Find out what we're doing. */
2090 layout_mcore_frame(&fi);
2091
2092 if (mcore_naked_function_p ())
2093 return;
2094
2095 /* If we had a frame pointer, restore the sp from that. */
2096 if (frame_pointer_needed)
2097 {
2098 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2099 growth = fi.local_growth - 1;
2100 }
2101 else
2102 {
2103 /* XXX: while loop should accumulate and do a single sell. */
2104 while (growth >= fi.local_growth)
2105 {
2106 if (fi.growth[growth] != 0)
2107 output_stack_adjust (1, fi.growth[growth]);
2108 growth--;
2109 }
2110 }
2111
2112 /* Make sure we've shrunk stack back to the point where the registers
2113 were laid down. This is typically 0/1 iterations. Then pull the
2114 register save information back off the stack. */
2115 while (growth >= fi.reg_growth)
2116 output_stack_adjust ( 1, fi.growth[growth--]);
2117
2118 offs = fi.reg_offset;
2119
2120 for (i = 15; i >= 0; i--)
2121 {
2122 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2123 {
2124 int first_reg;
2125
2126 /* Find the starting register. */
2127 first_reg = 15;
2128
2129 while (fi.reg_mask & (1 << first_reg))
2130 first_reg--;
2131
2132 first_reg++;
2133
2134 emit_insn (gen_load_multiple (gen_rtx_REG (SImode, first_reg),
2135 gen_rtx_MEM (SImode, stack_pointer_rtx),
2136 GEN_INT (16 - first_reg)));
2137
2138 i -= (15 - first_reg);
2139 offs += (16 - first_reg) * 4;
2140 }
2141 else if (fi.reg_mask & (1 << i))
2142 {
2143 emit_insn (gen_movsi
2144 (gen_rtx_REG (SImode, i),
2145 gen_rtx_MEM (SImode,
2146 plus_constant (Pmode, stack_pointer_rtx,
2147 offs))));
2148 offs += 4;
2149 }
2150 }
2151
2152 /* Give back anything else. */
2153 /* XXX: Should accumulate total and then give it back. */
2154 while (growth >= 0)
2155 output_stack_adjust ( 1, fi.growth[growth--]);
2156 }
2157 \f
2158 /* This code is borrowed from the SH port. */
2159
2160 /* The MCORE cannot load a large constant into a register, constants have to
2161 come from a pc relative load. The reference of a pc relative load
2162 instruction must be less than 1k in front of the instruction. This
2163 means that we often have to dump a constant inside a function, and
2164 generate code to branch around it.
2165
2166 It is important to minimize this, since the branches will slow things
2167 down and make things bigger.
2168
2169 Worst case code looks like:
2170
2171 lrw L1,r0
2172 br L2
2173 align
2174 L1: .long value
2175 L2:
2176 ..
2177
2178 lrw L3,r0
2179 br L4
2180 align
2181 L3: .long value
2182 L4:
2183 ..
2184
2185 We fix this by performing a scan before scheduling, which notices which
2186 instructions need to have their operands fetched from the constant table
2187 and builds the table.
2188
2189 The algorithm is:
2190
2191 scan, find an instruction which needs a pcrel move. Look forward, find the
2192 last barrier which is within MAX_COUNT bytes of the requirement.
2193 If there isn't one, make one. Process all the instructions between
2194 the find and the barrier.
2195
2196 In the above example, we can tell that L3 is within 1k of L1, so
2197 the first move can be shrunk from the 2 insn+constant sequence into
2198 just 1 insn, and the constant moved to L3 to make:
2199
2200 lrw L1,r0
2201 ..
2202 lrw L3,r0
2203 bra L4
2204 align
2205 L3:.long value
2206 L4:.long value
2207
2208 Then the second move becomes the target for the shortening process. */
2209
2210 typedef struct
2211 {
2212 rtx value; /* Value in table. */
2213 rtx label; /* Label of value. */
2214 } pool_node;
2215
2216 /* The maximum number of constants that can fit into one pool, since
2217 the pc relative range is 0...1020 bytes and constants are at least 4
2218 bytes long. We subtract 4 from the range to allow for the case where
2219 we need to add a branch/align before the constant pool. */
2220
2221 #define MAX_COUNT 1016
2222 #define MAX_POOL_SIZE (MAX_COUNT/4)
2223 static pool_node pool_vector[MAX_POOL_SIZE];
2224 static int pool_size;
2225
2226 /* Dump out any constants accumulated in the final pass. These
2227 will only be labels. */
2228
2229 const char *
2230 mcore_output_jump_label_table (void)
2231 {
2232 int i;
2233
2234 if (pool_size)
2235 {
2236 fprintf (asm_out_file, "\t.align 2\n");
2237
2238 for (i = 0; i < pool_size; i++)
2239 {
2240 pool_node * p = pool_vector + i;
2241
2242 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (p->label));
2243
2244 output_asm_insn (".long %0", &p->value);
2245 }
2246
2247 pool_size = 0;
2248 }
2249
2250 return "";
2251 }
2252
2253 /* Check whether insn is a candidate for a conditional. */
2254
2255 static cond_type
2256 is_cond_candidate (rtx insn)
2257 {
2258 /* The only things we conditionalize are those that can be directly
2259 changed into a conditional. Only bother with SImode items. If
2260 we wanted to be a little more aggressive, we could also do other
2261 modes such as DImode with reg-reg move or load 0. */
2262 if (NONJUMP_INSN_P (insn))
2263 {
2264 rtx pat = PATTERN (insn);
2265 rtx src, dst;
2266
2267 if (GET_CODE (pat) != SET)
2268 return COND_NO;
2269
2270 dst = XEXP (pat, 0);
2271
2272 if ((GET_CODE (dst) != REG &&
2273 GET_CODE (dst) != SUBREG) ||
2274 GET_MODE (dst) != SImode)
2275 return COND_NO;
2276
2277 src = XEXP (pat, 1);
2278
2279 if ((GET_CODE (src) == REG ||
2280 (GET_CODE (src) == SUBREG &&
2281 GET_CODE (SUBREG_REG (src)) == REG)) &&
2282 GET_MODE (src) == SImode)
2283 return COND_MOV_INSN;
2284 else if (GET_CODE (src) == CONST_INT &&
2285 INTVAL (src) == 0)
2286 return COND_CLR_INSN;
2287 else if (GET_CODE (src) == PLUS &&
2288 (GET_CODE (XEXP (src, 0)) == REG ||
2289 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2290 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2291 GET_MODE (XEXP (src, 0)) == SImode &&
2292 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2293 INTVAL (XEXP (src, 1)) == 1)
2294 return COND_INC_INSN;
2295 else if (((GET_CODE (src) == MINUS &&
2296 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2297 INTVAL( XEXP (src, 1)) == 1) ||
2298 (GET_CODE (src) == PLUS &&
2299 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2300 INTVAL (XEXP (src, 1)) == -1)) &&
2301 (GET_CODE (XEXP (src, 0)) == REG ||
2302 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2303 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2304 GET_MODE (XEXP (src, 0)) == SImode)
2305 return COND_DEC_INSN;
2306
2307 /* Some insns that we don't bother with:
2308 (set (rx:DI) (ry:DI))
2309 (set (rx:DI) (const_int 0))
2310 */
2311
2312 }
2313 else if (JUMP_P (insn)
2314 && GET_CODE (PATTERN (insn)) == SET
2315 && GET_CODE (XEXP (PATTERN (insn), 1)) == LABEL_REF)
2316 return COND_BRANCH_INSN;
2317
2318 return COND_NO;
2319 }
2320
2321 /* Emit a conditional version of insn and replace the old insn with the
2322 new one. Return the new insn if emitted. */
2323
2324 static rtx
2325 emit_new_cond_insn (rtx insn, int cond)
2326 {
2327 rtx c_insn = 0;
2328 rtx pat, dst, src;
2329 cond_type num;
2330
2331 if ((num = is_cond_candidate (insn)) == COND_NO)
2332 return NULL;
2333
2334 pat = PATTERN (insn);
2335
2336 if (NONJUMP_INSN_P (insn))
2337 {
2338 dst = SET_DEST (pat);
2339 src = SET_SRC (pat);
2340 }
2341 else
2342 {
2343 dst = JUMP_LABEL (insn);
2344 src = NULL_RTX;
2345 }
2346
2347 switch (num)
2348 {
2349 case COND_MOV_INSN:
2350 case COND_CLR_INSN:
2351 if (cond)
2352 c_insn = gen_movt0 (dst, src, dst);
2353 else
2354 c_insn = gen_movt0 (dst, dst, src);
2355 break;
2356
2357 case COND_INC_INSN:
2358 if (cond)
2359 c_insn = gen_incscc (dst, dst);
2360 else
2361 c_insn = gen_incscc_false (dst, dst);
2362 break;
2363
2364 case COND_DEC_INSN:
2365 if (cond)
2366 c_insn = gen_decscc (dst, dst);
2367 else
2368 c_insn = gen_decscc_false (dst, dst);
2369 break;
2370
2371 case COND_BRANCH_INSN:
2372 if (cond)
2373 c_insn = gen_branch_true (dst);
2374 else
2375 c_insn = gen_branch_false (dst);
2376 break;
2377
2378 default:
2379 return NULL;
2380 }
2381
2382 /* Only copy the notes if they exist. */
2383 if (rtx_length [GET_CODE (c_insn)] >= 7 && rtx_length [GET_CODE (insn)] >= 7)
2384 {
2385 /* We really don't need to bother with the notes and links at this
2386 point, but go ahead and save the notes. This will help is_dead()
2387 when applying peepholes (links don't matter since they are not
2388 used any more beyond this point for the mcore). */
2389 REG_NOTES (c_insn) = REG_NOTES (insn);
2390 }
2391
2392 if (num == COND_BRANCH_INSN)
2393 {
2394 /* For jumps, we need to be a little bit careful and emit the new jump
2395 before the old one and to update the use count for the target label.
2396 This way, the barrier following the old (uncond) jump will get
2397 deleted, but the label won't. */
2398 c_insn = emit_jump_insn_before (c_insn, insn);
2399
2400 ++ LABEL_NUSES (dst);
2401
2402 JUMP_LABEL (c_insn) = dst;
2403 }
2404 else
2405 c_insn = emit_insn_after (c_insn, insn);
2406
2407 delete_insn (insn);
2408
2409 return c_insn;
2410 }
2411
2412 /* Attempt to change a basic block into a series of conditional insns. This
2413 works by taking the branch at the end of the 1st block and scanning for the
2414 end of the 2nd block. If all instructions in the 2nd block have cond.
2415 versions and the label at the start of block 3 is the same as the target
2416 from the branch at block 1, then conditionalize all insn in block 2 using
2417 the inverse condition of the branch at block 1. (Note I'm bending the
2418 definition of basic block here.)
2419
2420 e.g., change:
2421
2422 bt L2 <-- end of block 1 (delete)
2423 mov r7,r8
2424 addu r7,1
2425 br L3 <-- end of block 2
2426
2427 L2: ... <-- start of block 3 (NUSES==1)
2428 L3: ...
2429
2430 to:
2431
2432 movf r7,r8
2433 incf r7
2434 bf L3
2435
2436 L3: ...
2437
2438 we can delete the L2 label if NUSES==1 and re-apply the optimization
2439 starting at the last instruction of block 2. This may allow an entire
2440 if-then-else statement to be conditionalized. BRC */
2441 static rtx
2442 conditionalize_block (rtx first)
2443 {
2444 rtx insn;
2445 rtx br_pat;
2446 rtx end_blk_1_br = 0;
2447 rtx end_blk_2_insn = 0;
2448 rtx start_blk_3_lab = 0;
2449 int cond;
2450 int br_lab_num;
2451 int blk_size = 0;
2452
2453
2454 /* Check that the first insn is a candidate conditional jump. This is
2455 the one that we'll eliminate. If not, advance to the next insn to
2456 try. */
2457 if (! JUMP_P (first)
2458 || GET_CODE (PATTERN (first)) != SET
2459 || GET_CODE (XEXP (PATTERN (first), 1)) != IF_THEN_ELSE)
2460 return NEXT_INSN (first);
2461
2462 /* Extract some information we need. */
2463 end_blk_1_br = first;
2464 br_pat = PATTERN (end_blk_1_br);
2465
2466 /* Complement the condition since we use the reverse cond. for the insns. */
2467 cond = (GET_CODE (XEXP (XEXP (br_pat, 1), 0)) == EQ);
2468
2469 /* Determine what kind of branch we have. */
2470 if (GET_CODE (XEXP (XEXP (br_pat, 1), 1)) == LABEL_REF)
2471 {
2472 /* A normal branch, so extract label out of first arm. */
2473 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 1), 0));
2474 }
2475 else
2476 {
2477 /* An inverse branch, so extract the label out of the 2nd arm
2478 and complement the condition. */
2479 cond = (cond == 0);
2480 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 2), 0));
2481 }
2482
2483 /* Scan forward for the start of block 2: it must start with a
2484 label and that label must be the same as the branch target
2485 label from block 1. We don't care about whether block 2 actually
2486 ends with a branch or a label (an uncond. branch is
2487 conditionalizable). */
2488 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
2489 {
2490 enum rtx_code code;
2491
2492 code = GET_CODE (insn);
2493
2494 /* Look for the label at the start of block 3. */
2495 if (code == CODE_LABEL && CODE_LABEL_NUMBER (insn) == br_lab_num)
2496 break;
2497
2498 /* Skip barriers, notes, and conditionalizable insns. If the
2499 insn is not conditionalizable or makes this optimization fail,
2500 just return the next insn so we can start over from that point. */
2501 if (code != BARRIER && code != NOTE && !is_cond_candidate (insn))
2502 return NEXT_INSN (insn);
2503
2504 /* Remember the last real insn before the label (i.e. end of block 2). */
2505 if (code == JUMP_INSN || code == INSN)
2506 {
2507 blk_size ++;
2508 end_blk_2_insn = insn;
2509 }
2510 }
2511
2512 if (!insn)
2513 return insn;
2514
2515 /* It is possible for this optimization to slow performance if the blocks
2516 are long. This really depends upon whether the branch is likely taken
2517 or not. If the branch is taken, we slow performance in many cases. But,
2518 if the branch is not taken, we always help performance (for a single
2519 block, but for a double block (i.e. when the optimization is re-applied)
2520 this is not true since the 'right thing' depends on the overall length of
2521 the collapsed block). As a compromise, don't apply this optimization on
2522 blocks larger than size 2 (unlikely for the mcore) when speed is important.
2523 the best threshold depends on the latencies of the instructions (i.e.,
2524 the branch penalty). */
2525 if (optimize > 1 && blk_size > 2)
2526 return insn;
2527
2528 /* At this point, we've found the start of block 3 and we know that
2529 it is the destination of the branch from block 1. Also, all
2530 instructions in the block 2 are conditionalizable. So, apply the
2531 conditionalization and delete the branch. */
2532 start_blk_3_lab = insn;
2533
2534 for (insn = NEXT_INSN (end_blk_1_br); insn != start_blk_3_lab;
2535 insn = NEXT_INSN (insn))
2536 {
2537 rtx newinsn;
2538
2539 if (INSN_DELETED_P (insn))
2540 continue;
2541
2542 /* Try to form a conditional variant of the instruction and emit it. */
2543 if ((newinsn = emit_new_cond_insn (insn, cond)))
2544 {
2545 if (end_blk_2_insn == insn)
2546 end_blk_2_insn = newinsn;
2547
2548 insn = newinsn;
2549 }
2550 }
2551
2552 /* Note whether we will delete the label starting blk 3 when the jump
2553 gets deleted. If so, we want to re-apply this optimization at the
2554 last real instruction right before the label. */
2555 if (LABEL_NUSES (start_blk_3_lab) == 1)
2556 {
2557 start_blk_3_lab = 0;
2558 }
2559
2560 /* ??? we probably should redistribute the death notes for this insn, esp.
2561 the death of cc, but it doesn't really matter this late in the game.
2562 The peepholes all use is_dead() which will find the correct death
2563 regardless of whether there is a note. */
2564 delete_insn (end_blk_1_br);
2565
2566 if (! start_blk_3_lab)
2567 return end_blk_2_insn;
2568
2569 /* Return the insn right after the label at the start of block 3. */
2570 return NEXT_INSN (start_blk_3_lab);
2571 }
2572
2573 /* Apply the conditionalization of blocks optimization. This is the
2574 outer loop that traverses through the insns scanning for a branch
2575 that signifies an opportunity to apply the optimization. Note that
2576 this optimization is applied late. If we could apply it earlier,
2577 say before cse 2, it may expose more optimization opportunities.
2578 but, the pay back probably isn't really worth the effort (we'd have
2579 to update all reg/flow/notes/links/etc to make it work - and stick it
2580 in before cse 2). */
2581
2582 static void
2583 conditionalize_optimization (void)
2584 {
2585 rtx insn;
2586
2587 for (insn = get_insns (); insn; insn = conditionalize_block (insn))
2588 continue;
2589 }
2590
2591 /* This is to handle loads from the constant pool. */
2592
2593 static void
2594 mcore_reorg (void)
2595 {
2596 /* Reset this variable. */
2597 current_function_anonymous_args = 0;
2598
2599 if (optimize == 0)
2600 return;
2601
2602 /* Conditionalize blocks where we can. */
2603 conditionalize_optimization ();
2604
2605 /* Literal pool generation is now pushed off until the assembler. */
2606 }
2607
2608 \f
2609 /* Return true if X is something that can be moved directly into r15. */
2610
2611 bool
2612 mcore_r15_operand_p (rtx x)
2613 {
2614 switch (GET_CODE (x))
2615 {
2616 case CONST_INT:
2617 return mcore_const_ok_for_inline (INTVAL (x));
2618
2619 case REG:
2620 case SUBREG:
2621 case MEM:
2622 return 1;
2623
2624 default:
2625 return 0;
2626 }
2627 }
2628
2629 /* Implement SECONDARY_RELOAD_CLASS. If RCLASS contains r15, and we can't
2630 directly move X into it, use r1-r14 as a temporary. */
2631
2632 enum reg_class
2633 mcore_secondary_reload_class (enum reg_class rclass,
2634 enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2635 {
2636 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], 15)
2637 && !mcore_r15_operand_p (x))
2638 return LRW_REGS;
2639 return NO_REGS;
2640 }
2641
2642 /* Return the reg_class to use when reloading the rtx X into the class
2643 RCLASS. If X is too complex to move directly into r15, prefer to
2644 use LRW_REGS instead. */
2645
2646 enum reg_class
2647 mcore_reload_class (rtx x, enum reg_class rclass)
2648 {
2649 if (reg_class_subset_p (LRW_REGS, rclass) && !mcore_r15_operand_p (x))
2650 return LRW_REGS;
2651
2652 return rclass;
2653 }
2654
2655 /* Tell me if a pair of reg/subreg rtx's actually refer to the same
2656 register. Note that the current version doesn't worry about whether
2657 they are the same mode or note (e.g., a QImode in r2 matches an HImode
2658 in r2 matches an SImode in r2. Might think in the future about whether
2659 we want to be able to say something about modes. */
2660
2661 int
2662 mcore_is_same_reg (rtx x, rtx y)
2663 {
2664 /* Strip any and all of the subreg wrappers. */
2665 while (GET_CODE (x) == SUBREG)
2666 x = SUBREG_REG (x);
2667
2668 while (GET_CODE (y) == SUBREG)
2669 y = SUBREG_REG (y);
2670
2671 if (GET_CODE(x) == REG && GET_CODE(y) == REG && REGNO(x) == REGNO(y))
2672 return 1;
2673
2674 return 0;
2675 }
2676
2677 static void
2678 mcore_option_override (void)
2679 {
2680 /* Only the m340 supports little endian code. */
2681 if (TARGET_LITTLE_END && ! TARGET_M340)
2682 target_flags |= MASK_M340;
2683 }
2684
2685 \f
2686 /* Compute the number of word sized registers needed to
2687 hold a function argument of mode MODE and type TYPE. */
2688
2689 int
2690 mcore_num_arg_regs (enum machine_mode mode, const_tree type)
2691 {
2692 int size;
2693
2694 if (targetm.calls.must_pass_in_stack (mode, type))
2695 return 0;
2696
2697 if (type && mode == BLKmode)
2698 size = int_size_in_bytes (type);
2699 else
2700 size = GET_MODE_SIZE (mode);
2701
2702 return ROUND_ADVANCE (size);
2703 }
2704
2705 static rtx
2706 handle_structs_in_regs (enum machine_mode mode, const_tree type, int reg)
2707 {
2708 int size;
2709
2710 /* The MCore ABI defines that a structure whose size is not a whole multiple
2711 of bytes is passed packed into registers (or spilled onto the stack if
2712 not enough registers are available) with the last few bytes of the
2713 structure being packed, left-justified, into the last register/stack slot.
2714 GCC handles this correctly if the last word is in a stack slot, but we
2715 have to generate a special, PARALLEL RTX if the last word is in an
2716 argument register. */
2717 if (type
2718 && TYPE_MODE (type) == BLKmode
2719 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2720 && (size = int_size_in_bytes (type)) > UNITS_PER_WORD
2721 && (size % UNITS_PER_WORD != 0)
2722 && (reg + mcore_num_arg_regs (mode, type) <= (FIRST_PARM_REG + NPARM_REGS)))
2723 {
2724 rtx arg_regs [NPARM_REGS];
2725 int nregs;
2726 rtx result;
2727 rtvec rtvec;
2728
2729 for (nregs = 0; size > 0; size -= UNITS_PER_WORD)
2730 {
2731 arg_regs [nregs] =
2732 gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, reg ++),
2733 GEN_INT (nregs * UNITS_PER_WORD));
2734 nregs ++;
2735 }
2736
2737 /* We assume here that NPARM_REGS == 6. The assert checks this. */
2738 gcc_assert (ARRAY_SIZE (arg_regs) == 6);
2739 rtvec = gen_rtvec (nregs, arg_regs[0], arg_regs[1], arg_regs[2],
2740 arg_regs[3], arg_regs[4], arg_regs[5]);
2741
2742 result = gen_rtx_PARALLEL (mode, rtvec);
2743 return result;
2744 }
2745
2746 return gen_rtx_REG (mode, reg);
2747 }
2748
2749 rtx
2750 mcore_function_value (const_tree valtype, const_tree func)
2751 {
2752 enum machine_mode mode;
2753 int unsigned_p;
2754
2755 mode = TYPE_MODE (valtype);
2756
2757 /* Since we promote return types, we must promote the mode here too. */
2758 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
2759
2760 return handle_structs_in_regs (mode, valtype, FIRST_RET_REG);
2761 }
2762
2763 /* Define where to put the arguments to a function.
2764 Value is zero to push the argument on the stack,
2765 or a hard register in which to store the argument.
2766
2767 MODE is the argument's machine mode.
2768 TYPE is the data type of the argument (as a tree).
2769 This is null for libcalls where that information may
2770 not be available.
2771 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2772 the preceding args and about the function being called.
2773 NAMED is nonzero if this argument is a named parameter
2774 (otherwise it is an extra parameter matching an ellipsis).
2775
2776 On MCore the first args are normally in registers
2777 and the rest are pushed. Any arg that starts within the first
2778 NPARM_REGS words is at least partially passed in a register unless
2779 its data type forbids. */
2780
2781 static rtx
2782 mcore_function_arg (cumulative_args_t cum, enum machine_mode mode,
2783 const_tree type, bool named)
2784 {
2785 int arg_reg;
2786
2787 if (! named || mode == VOIDmode)
2788 return 0;
2789
2790 if (targetm.calls.must_pass_in_stack (mode, type))
2791 return 0;
2792
2793 arg_reg = ROUND_REG (*get_cumulative_args (cum), mode);
2794
2795 if (arg_reg < NPARM_REGS)
2796 return handle_structs_in_regs (mode, type, FIRST_PARM_REG + arg_reg);
2797
2798 return 0;
2799 }
2800
2801 static void
2802 mcore_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
2803 const_tree type, bool named ATTRIBUTE_UNUSED)
2804 {
2805 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2806
2807 *cum = (ROUND_REG (*cum, mode)
2808 + (int)named * mcore_num_arg_regs (mode, type));
2809 }
2810
2811 static unsigned int
2812 mcore_function_arg_boundary (enum machine_mode mode,
2813 const_tree type ATTRIBUTE_UNUSED)
2814 {
2815 /* Doubles must be aligned to an 8 byte boundary. */
2816 return (mode != BLKmode && GET_MODE_SIZE (mode) == 8
2817 ? BIGGEST_ALIGNMENT
2818 : PARM_BOUNDARY);
2819 }
2820
2821 /* Returns the number of bytes of argument registers required to hold *part*
2822 of a parameter of machine mode MODE and type TYPE (which may be NULL if
2823 the type is not known). If the argument fits entirely in the argument
2824 registers, or entirely on the stack, then 0 is returned. CUM is the
2825 number of argument registers already used by earlier parameters to
2826 the function. */
2827
2828 static int
2829 mcore_arg_partial_bytes (cumulative_args_t cum, enum machine_mode mode,
2830 tree type, bool named)
2831 {
2832 int reg = ROUND_REG (*get_cumulative_args (cum), mode);
2833
2834 if (named == 0)
2835 return 0;
2836
2837 if (targetm.calls.must_pass_in_stack (mode, type))
2838 return 0;
2839
2840 /* REG is not the *hardware* register number of the register that holds
2841 the argument, it is the *argument* register number. So for example,
2842 the first argument to a function goes in argument register 0, which
2843 translates (for the MCore) into hardware register 2. The second
2844 argument goes into argument register 1, which translates into hardware
2845 register 3, and so on. NPARM_REGS is the number of argument registers
2846 supported by the target, not the maximum hardware register number of
2847 the target. */
2848 if (reg >= NPARM_REGS)
2849 return 0;
2850
2851 /* If the argument fits entirely in registers, return 0. */
2852 if (reg + mcore_num_arg_regs (mode, type) <= NPARM_REGS)
2853 return 0;
2854
2855 /* The argument overflows the number of available argument registers.
2856 Compute how many argument registers have not yet been assigned to
2857 hold an argument. */
2858 reg = NPARM_REGS - reg;
2859
2860 /* Return partially in registers and partially on the stack. */
2861 return reg * UNITS_PER_WORD;
2862 }
2863 \f
2864 /* Return nonzero if SYMBOL is marked as being dllexport'd. */
2865
2866 int
2867 mcore_dllexport_name_p (const char * symbol)
2868 {
2869 return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
2870 }
2871
2872 /* Return nonzero if SYMBOL is marked as being dllimport'd. */
2873
2874 int
2875 mcore_dllimport_name_p (const char * symbol)
2876 {
2877 return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
2878 }
2879
2880 /* Mark a DECL as being dllexport'd. */
2881
2882 static void
2883 mcore_mark_dllexport (tree decl)
2884 {
2885 const char * oldname;
2886 char * newname;
2887 rtx rtlname;
2888 tree idp;
2889
2890 rtlname = XEXP (DECL_RTL (decl), 0);
2891
2892 if (GET_CODE (rtlname) == MEM)
2893 rtlname = XEXP (rtlname, 0);
2894 gcc_assert (GET_CODE (rtlname) == SYMBOL_REF);
2895 oldname = XSTR (rtlname, 0);
2896
2897 if (mcore_dllexport_name_p (oldname))
2898 return; /* Already done. */
2899
2900 newname = XALLOCAVEC (char, strlen (oldname) + 4);
2901 sprintf (newname, "@e.%s", oldname);
2902
2903 /* We pass newname through get_identifier to ensure it has a unique
2904 address. RTL processing can sometimes peek inside the symbol ref
2905 and compare the string's addresses to see if two symbols are
2906 identical. */
2907 /* ??? At least I think that's why we do this. */
2908 idp = get_identifier (newname);
2909
2910 XEXP (DECL_RTL (decl), 0) =
2911 gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
2912 }
2913
2914 /* Mark a DECL as being dllimport'd. */
2915
2916 static void
2917 mcore_mark_dllimport (tree decl)
2918 {
2919 const char * oldname;
2920 char * newname;
2921 tree idp;
2922 rtx rtlname;
2923 rtx newrtl;
2924
2925 rtlname = XEXP (DECL_RTL (decl), 0);
2926
2927 if (GET_CODE (rtlname) == MEM)
2928 rtlname = XEXP (rtlname, 0);
2929 gcc_assert (GET_CODE (rtlname) == SYMBOL_REF);
2930 oldname = XSTR (rtlname, 0);
2931
2932 gcc_assert (!mcore_dllexport_name_p (oldname));
2933 if (mcore_dllimport_name_p (oldname))
2934 return; /* Already done. */
2935
2936 /* ??? One can well ask why we're making these checks here,
2937 and that would be a good question. */
2938
2939 /* Imported variables can't be initialized. */
2940 if (TREE_CODE (decl) == VAR_DECL
2941 && !DECL_VIRTUAL_P (decl)
2942 && DECL_INITIAL (decl))
2943 {
2944 error ("initialized variable %q+D is marked dllimport", decl);
2945 return;
2946 }
2947
2948 /* `extern' needn't be specified with dllimport.
2949 Specify `extern' now and hope for the best. Sigh. */
2950 if (TREE_CODE (decl) == VAR_DECL
2951 /* ??? Is this test for vtables needed? */
2952 && !DECL_VIRTUAL_P (decl))
2953 {
2954 DECL_EXTERNAL (decl) = 1;
2955 TREE_PUBLIC (decl) = 1;
2956 }
2957
2958 newname = XALLOCAVEC (char, strlen (oldname) + 11);
2959 sprintf (newname, "@i.__imp_%s", oldname);
2960
2961 /* We pass newname through get_identifier to ensure it has a unique
2962 address. RTL processing can sometimes peek inside the symbol ref
2963 and compare the string's addresses to see if two symbols are
2964 identical. */
2965 /* ??? At least I think that's why we do this. */
2966 idp = get_identifier (newname);
2967
2968 newrtl = gen_rtx_MEM (Pmode,
2969 gen_rtx_SYMBOL_REF (Pmode,
2970 IDENTIFIER_POINTER (idp)));
2971 XEXP (DECL_RTL (decl), 0) = newrtl;
2972 }
2973
2974 static int
2975 mcore_dllexport_p (tree decl)
2976 {
2977 if ( TREE_CODE (decl) != VAR_DECL
2978 && TREE_CODE (decl) != FUNCTION_DECL)
2979 return 0;
2980
2981 return lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) != 0;
2982 }
2983
2984 static int
2985 mcore_dllimport_p (tree decl)
2986 {
2987 if ( TREE_CODE (decl) != VAR_DECL
2988 && TREE_CODE (decl) != FUNCTION_DECL)
2989 return 0;
2990
2991 return lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) != 0;
2992 }
2993
2994 /* We must mark dll symbols specially. Definitions of dllexport'd objects
2995 install some info in the .drective (PE) or .exports (ELF) sections. */
2996
2997 static void
2998 mcore_encode_section_info (tree decl, rtx rtl ATTRIBUTE_UNUSED, int first ATTRIBUTE_UNUSED)
2999 {
3000 /* Mark the decl so we can tell from the rtl whether the object is
3001 dllexport'd or dllimport'd. */
3002 if (mcore_dllexport_p (decl))
3003 mcore_mark_dllexport (decl);
3004 else if (mcore_dllimport_p (decl))
3005 mcore_mark_dllimport (decl);
3006
3007 /* It might be that DECL has already been marked as dllimport, but
3008 a subsequent definition nullified that. The attribute is gone
3009 but DECL_RTL still has @i.__imp_foo. We need to remove that. */
3010 else if ((TREE_CODE (decl) == FUNCTION_DECL
3011 || TREE_CODE (decl) == VAR_DECL)
3012 && DECL_RTL (decl) != NULL_RTX
3013 && GET_CODE (DECL_RTL (decl)) == MEM
3014 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
3015 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
3016 && mcore_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
3017 {
3018 const char * oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
3019 tree idp = get_identifier (oldname + 9);
3020 rtx newrtl = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
3021
3022 XEXP (DECL_RTL (decl), 0) = newrtl;
3023
3024 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
3025 ??? We leave these alone for now. */
3026 }
3027 }
3028
3029 /* Undo the effects of the above. */
3030
3031 static const char *
3032 mcore_strip_name_encoding (const char * str)
3033 {
3034 return str + (str[0] == '@' ? 3 : 0);
3035 }
3036
3037 /* MCore specific attribute support.
3038 dllexport - for exporting a function/variable that will live in a dll
3039 dllimport - for importing a function/variable from a dll
3040 naked - do not create a function prologue/epilogue. */
3041
3042 /* Handle a "naked" attribute; arguments as in
3043 struct attribute_spec.handler. */
3044
3045 static tree
3046 mcore_handle_naked_attribute (tree * node, tree name, tree args ATTRIBUTE_UNUSED,
3047 int flags ATTRIBUTE_UNUSED, bool * no_add_attrs)
3048 {
3049 if (TREE_CODE (*node) != FUNCTION_DECL)
3050 {
3051 warning (OPT_Wattributes, "%qE attribute only applies to functions",
3052 name);
3053 *no_add_attrs = true;
3054 }
3055
3056 return NULL_TREE;
3057 }
3058
3059 /* ??? It looks like this is PE specific? Oh well, this is what the
3060 old code did as well. */
3061
3062 static void
3063 mcore_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
3064 {
3065 int len;
3066 const char * name;
3067 char * string;
3068 const char * prefix;
3069
3070 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3071
3072 /* Strip off any encoding in name. */
3073 name = (* targetm.strip_name_encoding) (name);
3074
3075 /* The object is put in, for example, section .text$foo.
3076 The linker will then ultimately place them in .text
3077 (everything from the $ on is stripped). */
3078 if (TREE_CODE (decl) == FUNCTION_DECL)
3079 prefix = ".text$";
3080 /* For compatibility with EPOC, we ignore the fact that the
3081 section might have relocs against it. */
3082 else if (decl_readonly_section (decl, 0))
3083 prefix = ".rdata$";
3084 else
3085 prefix = ".data$";
3086
3087 len = strlen (name) + strlen (prefix);
3088 string = XALLOCAVEC (char, len + 1);
3089
3090 sprintf (string, "%s%s", prefix, name);
3091
3092 DECL_SECTION_NAME (decl) = build_string (len, string);
3093 }
3094
3095 int
3096 mcore_naked_function_p (void)
3097 {
3098 return lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE;
3099 }
3100
3101 static bool
3102 mcore_warn_func_return (tree decl)
3103 {
3104 /* Naked functions are implemented entirely in assembly, including the
3105 return sequence, so suppress warnings about this. */
3106 return lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) == NULL_TREE;
3107 }
3108
3109 #ifdef OBJECT_FORMAT_ELF
3110 static void
3111 mcore_asm_named_section (const char *name,
3112 unsigned int flags ATTRIBUTE_UNUSED,
3113 tree decl ATTRIBUTE_UNUSED)
3114 {
3115 fprintf (asm_out_file, "\t.section %s\n", name);
3116 }
3117 #endif /* OBJECT_FORMAT_ELF */
3118
3119 /* Worker function for TARGET_ASM_EXTERNAL_LIBCALL. */
3120
3121 static void
3122 mcore_external_libcall (rtx fun)
3123 {
3124 fprintf (asm_out_file, "\t.import\t");
3125 assemble_name (asm_out_file, XSTR (fun, 0));
3126 fprintf (asm_out_file, "\n");
3127 }
3128
3129 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3130
3131 static bool
3132 mcore_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3133 {
3134 const HOST_WIDE_INT size = int_size_in_bytes (type);
3135 return (size == -1 || size > 2 * UNITS_PER_WORD);
3136 }
3137
3138 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE.
3139 Output assembler code for a block containing the constant parts
3140 of a trampoline, leaving space for the variable parts.
3141
3142 On the MCore, the trampoline looks like:
3143 lrw r1, function
3144 lrw r13, area
3145 jmp r13
3146 or r0, r0
3147 .literals */
3148
3149 static void
3150 mcore_asm_trampoline_template (FILE *f)
3151 {
3152 fprintf (f, "\t.short 0x7102\n");
3153 fprintf (f, "\t.short 0x7d02\n");
3154 fprintf (f, "\t.short 0x00cd\n");
3155 fprintf (f, "\t.short 0x1e00\n");
3156 fprintf (f, "\t.long 0\n");
3157 fprintf (f, "\t.long 0\n");
3158 }
3159
3160 /* Worker function for TARGET_TRAMPOLINE_INIT. */
3161
3162 static void
3163 mcore_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
3164 {
3165 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
3166 rtx mem;
3167
3168 emit_block_move (m_tramp, assemble_trampoline_template (),
3169 GEN_INT (2*UNITS_PER_WORD), BLOCK_OP_NORMAL);
3170
3171 mem = adjust_address (m_tramp, SImode, 8);
3172 emit_move_insn (mem, chain_value);
3173 mem = adjust_address (m_tramp, SImode, 12);
3174 emit_move_insn (mem, fnaddr);
3175 }
3176
3177 /* Implement TARGET_LEGITIMATE_CONSTANT_P
3178
3179 On the MCore, allow anything but a double. */
3180
3181 static bool
3182 mcore_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
3183 {
3184 return GET_CODE (x) != CONST_DOUBLE;
3185 }