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