* c-decl.c (grokdeclarator): Use ISO word.
[official-gcc.git] / gcc / config / mcore / mcore.c
blobe4901085f78f0a1e9ad5b35a774102921c77bef1
1 /* Output routines for Motorola MCore processor
2 Copyright (C) 1993, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "tm_p.h"
26 #include "assert.h"
27 #include "mcore.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "obstack.h"
37 #include "expr.h"
38 #include "reload.h"
39 #include "recog.h"
40 #include "function.h"
41 #include "ggc.h"
42 #include "toplev.h"
43 #include "target.h"
44 #include "target-def.h"
46 /* Maximum size we are allowed to grow the stack in a single operation.
47 If we want more, we must do it in increments of at most this size.
48 If this value is 0, we don't check at all. */
49 const char * mcore_stack_increment_string = 0;
50 int mcore_stack_increment = STACK_UNITS_MAXSTEP;
52 /* For dumping information about frame sizes. */
53 char * mcore_current_function_name = 0;
54 long mcore_current_compilation_timestamp = 0;
56 /* Global variables for machine-dependent things. */
58 /* Saved operands from the last compare to use when we generate an scc
59 or bcc insn. */
60 rtx arch_compare_op0;
61 rtx arch_compare_op1;
63 /* Provides the class number of the smallest class containing
64 reg number. */
65 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
67 GENERAL_REGS, ONLYR1_REGS, LRW_REGS, LRW_REGS,
68 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
69 LRW_REGS, LRW_REGS, LRW_REGS, LRW_REGS,
70 LRW_REGS, LRW_REGS, LRW_REGS, GENERAL_REGS,
71 GENERAL_REGS, C_REGS, NO_REGS, NO_REGS,
74 /* Provide reg_class from a letter such as appears in the machine
75 description. */
76 enum reg_class reg_class_from_letter[] =
78 /* a */ LRW_REGS, /* b */ ONLYR1_REGS, /* c */ C_REGS, /* d */ NO_REGS,
79 /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
80 /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ NO_REGS,
81 /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
82 /* q */ NO_REGS, /* r */ GENERAL_REGS, /* s */ NO_REGS, /* t */ NO_REGS,
83 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ ALL_REGS,
84 /* y */ NO_REGS, /* z */ NO_REGS
87 struct mcore_frame
89 int arg_size; /* stdarg spills (bytes) */
90 int reg_size; /* non-volatile reg saves (bytes) */
91 int reg_mask; /* non-volatile reg saves */
92 int local_size; /* locals */
93 int outbound_size; /* arg overflow on calls out */
94 int pad_outbound;
95 int pad_local;
96 int pad_reg;
97 /* Describe the steps we'll use to grow it. */
98 #define MAX_STACK_GROWS 4 /* gives us some spare space */
99 int growth[MAX_STACK_GROWS];
100 int arg_offset;
101 int reg_offset;
102 int reg_growth;
103 int local_growth;
106 typedef enum
108 COND_NO,
109 COND_MOV_INSN,
110 COND_CLR_INSN,
111 COND_INC_INSN,
112 COND_DEC_INSN,
113 COND_BRANCH_INSN
115 cond_type;
117 static void output_stack_adjust PARAMS ((int, int));
118 static int calc_live_regs PARAMS ((int *));
119 static int const_ok_for_mcore PARAMS ((int));
120 static int try_constant_tricks PARAMS ((long, int *, int *));
121 static const char * output_inline_const PARAMS ((enum machine_mode, rtx *));
122 static void block_move_sequence PARAMS ((rtx, rtx, rtx, rtx, int, int, int));
123 static void layout_mcore_frame PARAMS ((struct mcore_frame *));
124 static cond_type is_cond_candidate PARAMS ((rtx));
125 static rtx emit_new_cond_insn PARAMS ((rtx, int));
126 static rtx conditionalize_block PARAMS ((rtx));
127 static void conditionalize_optimization PARAMS ((rtx));
128 static void mcore_add_gc_roots PARAMS ((void));
129 static rtx handle_structs_in_regs PARAMS ((enum machine_mode, tree, int));
130 static void mcore_mark_dllexport PARAMS ((tree));
131 static void mcore_mark_dllimport PARAMS ((tree));
132 static int mcore_dllexport_p PARAMS ((tree));
133 static int mcore_dllimport_p PARAMS ((tree));
134 const struct attribute_spec mcore_attribute_table[];
135 static tree mcore_handle_naked_attribute PARAMS ((tree *, tree, tree, int, bool *));
136 #ifdef OBJECT_FORMAT_ELF
137 static void mcore_asm_named_section PARAMS ((const char *,
138 unsigned int));
139 #endif
141 /* Initialize the GCC target structure. */
142 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
143 #undef TARGET_MERGE_DECL_ATTRIBUTES
144 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
145 #endif
147 #undef TARGET_ATTRIBUTE_TABLE
148 #define TARGET_ATTRIBUTE_TABLE mcore_attribute_table
150 struct gcc_target targetm = TARGET_INITIALIZER;
152 /* Adjust the stack and return the number of bytes taken to do it. */
153 static void
154 output_stack_adjust (direction, size)
155 int direction;
156 int size;
158 /* If extending stack a lot, we do it incrementally. */
159 if (direction < 0 && size > mcore_stack_increment && mcore_stack_increment > 0)
161 rtx tmp = gen_rtx (REG, SImode, 1);
162 rtx memref;
163 emit_insn (gen_movsi (tmp, GEN_INT (mcore_stack_increment)));
166 emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
167 memref = gen_rtx (MEM, SImode, stack_pointer_rtx);
168 MEM_VOLATILE_P (memref) = 1;
169 emit_insn (gen_movsi (memref, stack_pointer_rtx));
170 size -= mcore_stack_increment;
172 while (size > mcore_stack_increment);
174 /* SIZE is now the residual for the last adjustment,
175 which doesn't require a probe. */
178 if (size)
180 rtx insn;
181 rtx val = GEN_INT (size);
183 if (size > 32)
185 rtx nval = gen_rtx (REG, SImode, 1);
186 emit_insn (gen_movsi (nval, val));
187 val = nval;
190 if (direction > 0)
191 insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
192 else
193 insn = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
195 emit_insn (insn);
199 /* Work out the registers which need to be saved,
200 both as a mask and a count. */
202 static int
203 calc_live_regs (count)
204 int * count;
206 int reg;
207 int live_regs_mask = 0;
209 * count = 0;
211 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
213 if (regs_ever_live[reg] && !call_used_regs[reg])
215 (*count)++;
216 live_regs_mask |= (1 << reg);
220 return live_regs_mask;
223 /* Print the operand address in x to the stream. */
225 void
226 mcore_print_operand_address (stream, x)
227 FILE * stream;
228 rtx x;
230 switch (GET_CODE (x))
232 case REG:
233 fprintf (stream, "(%s)", reg_names[REGNO (x)]);
234 break;
236 case PLUS:
238 rtx base = XEXP (x, 0);
239 rtx index = XEXP (x, 1);
241 if (GET_CODE (base) != REG)
243 /* Ensure that BASE is a register (one of them must be). */
244 rtx temp = base;
245 base = index;
246 index = temp;
249 switch (GET_CODE (index))
251 case CONST_INT:
252 fprintf (stream, "(%s,%d)", reg_names[REGNO(base)],
253 INTVAL (index));
254 break;
256 default:
257 debug_rtx (x);
259 abort ();
263 break;
265 default:
266 output_addr_const (stream, x);
267 break;
271 /* Print operand x (an rtx) in assembler syntax to file stream
272 according to modifier code.
274 'R' print the next register or memory location along, ie the lsw in
275 a double word value
276 'O' print a constant without the #
277 'M' print a constant as its negative
278 'P' print log2 of a power of two
279 'Q' print log2 of an inverse of a power of two
280 'U' print register for ldm/stm instruction
281 'X' print byte number for xtrbN instruction. */
283 void
284 mcore_print_operand (stream, x, code)
285 FILE * stream;
286 rtx x;
287 int code;
289 switch (code)
291 case 'N':
292 if (INTVAL(x) == -1)
293 fprintf (asm_out_file, "32");
294 else
295 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) + 1));
296 break;
297 case 'P':
298 fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x)));
299 break;
300 case 'Q':
301 fprintf (asm_out_file, "%d", exact_log2 (~INTVAL (x)));
302 break;
303 case 'O':
304 fprintf (asm_out_file, "%d", INTVAL (x));
305 break;
306 case 'M':
307 fprintf (asm_out_file, "%d", - INTVAL (x));
308 break;
309 case 'R':
310 /* Next location along in memory or register. */
311 switch (GET_CODE (x))
313 case REG:
314 fputs (reg_names[REGNO (x) + 1], (stream));
315 break;
316 case MEM:
317 mcore_print_operand_address
318 (stream, XEXP (adjust_address (x, SImode, 4), 0));
319 break;
320 default:
321 abort ();
323 break;
324 case 'U':
325 fprintf (asm_out_file, "%s-%s", reg_names[REGNO (x)],
326 reg_names[REGNO (x) + 3]);
327 break;
328 case 'x':
329 fprintf (asm_out_file, "0x%x", INTVAL (x));
330 break;
331 case 'X':
332 fprintf (asm_out_file, "%d", 3 - INTVAL (x) / 8);
333 break;
335 default:
336 switch (GET_CODE (x))
338 case REG:
339 fputs (reg_names[REGNO (x)], (stream));
340 break;
341 case MEM:
342 output_address (XEXP (x, 0));
343 break;
344 default:
345 output_addr_const (stream, x);
346 break;
348 break;
352 /* What does a constant cost ? */
355 mcore_const_costs (exp, code)
356 rtx exp;
357 enum rtx_code code;
360 int val = INTVAL (exp);
362 /* Easy constants. */
363 if ( CONST_OK_FOR_I (val)
364 || CONST_OK_FOR_M (val)
365 || CONST_OK_FOR_N (val)
366 || (code == PLUS && CONST_OK_FOR_L (val)))
367 return 1;
368 else if (code == AND
369 && ( CONST_OK_FOR_M (~val)
370 || CONST_OK_FOR_N (~val)))
371 return 2;
372 else if (code == PLUS
373 && ( CONST_OK_FOR_I (-val)
374 || CONST_OK_FOR_M (-val)
375 || CONST_OK_FOR_N (-val)))
376 return 2;
378 return 5;
381 /* What does an and instruction cost - we do this b/c immediates may
382 have been relaxed. We want to ensure that cse will cse relaxed immeds
383 out. Otherwise we'll get bad code (multiple reloads of the same const). */
386 mcore_and_cost (x)
387 rtx x;
389 int val;
391 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
392 return 2;
394 val = INTVAL (XEXP (x, 1));
396 /* Do it directly. */
397 if (CONST_OK_FOR_K (val) || CONST_OK_FOR_M (~val))
398 return 2;
399 /* Takes one instruction to load. */
400 else if (const_ok_for_mcore (val))
401 return 3;
402 /* Takes two instructions to load. */
403 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
404 return 4;
406 /* Takes a lrw to load. */
407 return 5;
410 /* What does an or cost - see and_cost(). */
413 mcore_ior_cost (x)
414 rtx x;
416 int val;
418 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
419 return 2;
421 val = INTVAL (XEXP (x, 1));
423 /* Do it directly with bclri. */
424 if (CONST_OK_FOR_M (val))
425 return 2;
426 /* Takes one instruction to load. */
427 else if (const_ok_for_mcore (val))
428 return 3;
429 /* Takes two instructions to load. */
430 else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
431 return 4;
433 /* Takes a lrw to load. */
434 return 5;
437 /* Check to see if a comparison against a constant can be made more efficient
438 by incrementing/decrementing the constant to get one that is more efficient
439 to load. */
442 mcore_modify_comparison (code)
443 enum rtx_code code;
445 rtx op1 = arch_compare_op1;
447 if (GET_CODE (op1) == CONST_INT)
449 int val = INTVAL (op1);
451 switch (code)
453 case LE:
454 if (CONST_OK_FOR_J (val + 1))
456 arch_compare_op1 = GEN_INT (val + 1);
457 return 1;
459 break;
461 default:
462 break;
466 return 0;
469 /* Prepare the operands for a comparison. */
472 mcore_gen_compare_reg (code)
473 enum rtx_code code;
475 rtx op0 = arch_compare_op0;
476 rtx op1 = arch_compare_op1;
477 rtx cc_reg = gen_rtx (REG, CCmode, CC_REG);
479 if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
480 op1 = force_reg (SImode, op1);
482 /* cmpnei: 0-31 (K immediate)
483 cmplti: 1-32 (J immediate, 0 using btsti x,31). */
484 switch (code)
486 case EQ: /* Use inverted condition, cmpne. */
487 code = NE;
488 /* drop through */
490 case NE: /* Use normal condition, cmpne. */
491 if (GET_CODE (op1) == CONST_INT && ! CONST_OK_FOR_K (INTVAL (op1)))
492 op1 = force_reg (SImode, op1);
493 break;
495 case LE: /* Use inverted condition, reversed cmplt. */
496 code = GT;
497 /* drop through */
499 case GT: /* Use normal condition, reversed cmplt. */
500 if (GET_CODE (op1) == CONST_INT)
501 op1 = force_reg (SImode, op1);
502 break;
504 case GE: /* Use inverted condition, cmplt. */
505 code = LT;
506 /* drop through */
508 case LT: /* Use normal condition, cmplt. */
509 if (GET_CODE (op1) == CONST_INT &&
510 /* covered by btsti x,31 */
511 INTVAL (op1) != 0 &&
512 ! CONST_OK_FOR_J (INTVAL (op1)))
513 op1 = force_reg (SImode, op1);
514 break;
516 case GTU: /* Use inverted condition, cmple. */
517 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) == 0)
519 /* Unsigned > 0 is the same as != 0, but we need
520 to invert the condition, so we want to set
521 code = EQ. This cannot be done however, as the
522 mcore does not support such a test. Instead we
523 cope with this case in the "bgtu" pattern itself
524 so we should never reach this point. */
525 /* code = EQ; */
526 abort ();
527 break;
529 code = LEU;
530 /* drop through */
532 case LEU: /* Use normal condition, reversed cmphs. */
533 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
534 op1 = force_reg (SImode, op1);
535 break;
537 case LTU: /* Use inverted condition, cmphs. */
538 code = GEU;
539 /* drop through */
541 case GEU: /* Use normal condition, cmphs. */
542 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
543 op1 = force_reg (SImode, op1);
544 break;
546 default:
547 break;
550 emit_insn (gen_rtx (SET, VOIDmode, cc_reg, gen_rtx (code, CCmode, op0, op1)));
552 return cc_reg;
557 mcore_symbolic_address_p (x)
558 rtx x;
560 switch (GET_CODE (x))
562 case SYMBOL_REF:
563 case LABEL_REF:
564 return 1;
565 case CONST:
566 x = XEXP (x, 0);
567 return ( (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
568 || GET_CODE (XEXP (x, 0)) == LABEL_REF)
569 && GET_CODE (XEXP (x, 1)) == CONST_INT);
570 default:
571 return 0;
576 mcore_call_address_operand (x, mode)
577 rtx x;
578 enum machine_mode mode;
580 return register_operand (x, mode) || CONSTANT_P (x);
583 /* Functions to output assembly code for a function call. */
585 char *
586 mcore_output_call (operands, index)
587 rtx operands[];
588 int index;
590 static char buffer[20];
591 rtx addr = operands [index];
593 if (REG_P (addr))
595 if (TARGET_CG_DATA)
597 if (mcore_current_function_name == 0)
598 abort ();
600 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
601 "unknown", 1);
604 sprintf (buffer, "jsr\t%%%d", index);
606 else
608 if (TARGET_CG_DATA)
610 if (mcore_current_function_name == 0)
611 abort ();
613 if (GET_CODE (addr) != SYMBOL_REF)
614 abort ();
616 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, XSTR (addr, 0), 0);
619 sprintf (buffer, "jbsr\t%%%d", index);
622 return buffer;
625 /* Can we load a constant with a single instruction ? */
627 static int
628 const_ok_for_mcore (value)
629 int value;
631 if (value >= 0 && value <= 127)
632 return 1;
634 /* Try exact power of two. */
635 if ((value & (value - 1)) == 0)
636 return 1;
638 /* Try exact power of two - 1. */
639 if ((value & (value + 1)) == 0)
640 return 1;
642 return 0;
645 /* Can we load a constant inline with up to 2 instructions ? */
648 mcore_const_ok_for_inline (value)
649 long value;
651 int x, y;
653 return try_constant_tricks (value, & x, & y) > 0;
656 /* Are we loading the constant using a not ? */
659 mcore_const_trick_uses_not (value)
660 long value;
662 int x, y;
664 return try_constant_tricks (value, & x, & y) == 2;
667 /* Try tricks to load a constant inline and return the trick number if
668 success (0 is non-inlinable).
670 0: not inlinable
671 1: single instruction (do the usual thing)
672 2: single insn followed by a 'not'
673 3: single insn followed by a subi
674 4: single insn followed by an addi
675 5: single insn followed by rsubi
676 6: single insn followed by bseti
677 7: single insn followed by bclri
678 8: single insn followed by rotli
679 9: single insn followed by lsli
680 10: single insn followed by ixh
681 11: single insn followed by ixw. */
683 static int
684 try_constant_tricks (value, x, y)
685 long value;
686 int * x;
687 int * y;
689 int i;
690 unsigned bit, shf, rot;
692 if (const_ok_for_mcore (value))
693 return 1; /* Do the usual thing. */
695 if (TARGET_HARDLIT)
697 if (const_ok_for_mcore (~value))
699 *x = ~value;
700 return 2;
703 for (i = 1; i <= 32; i++)
705 if (const_ok_for_mcore (value - i))
707 *x = value - i;
708 *y = i;
710 return 3;
713 if (const_ok_for_mcore (value + i))
715 *x = value + i;
716 *y = i;
718 return 4;
722 bit = 0x80000000UL;
724 for (i = 0; i <= 31; i++)
726 if (const_ok_for_mcore (i - value))
728 *x = i - value;
729 *y = i;
731 return 5;
734 if (const_ok_for_mcore (value & ~bit))
736 *y = bit;
737 *x = value & ~bit;
739 return 6;
742 if (const_ok_for_mcore (value | bit))
744 *y = ~bit;
745 *x = value | bit;
747 return 7;
750 bit >>= 1;
753 shf = value;
754 rot = value;
756 for (i = 1; i < 31; i++)
758 int c;
760 /* MCore has rotate left. */
761 c = rot << 31;
762 rot >>= 1;
763 rot &= 0x7FFFFFFF;
764 rot |= c; /* Simulate rotate. */
766 if (const_ok_for_mcore (rot))
768 *y = i;
769 *x = rot;
771 return 8;
774 if (shf & 1)
775 shf = 0; /* Can't use logical shift, low order bit is one. */
777 shf >>= 1;
779 if (shf != 0 && const_ok_for_mcore (shf))
781 *y = i;
782 *x = shf;
784 return 9;
788 if ((value % 3) == 0 && const_ok_for_mcore (value / 3))
790 *x = value / 3;
792 return 10;
795 if ((value % 5) == 0 && const_ok_for_mcore (value / 5))
797 *x = value / 5;
799 return 11;
803 return 0;
807 /* Check whether reg is dead at first. This is done by searching ahead
808 for either the next use (i.e., reg is live), a death note, or a set of
809 reg. Don't just use dead_or_set_p() since reload does not always mark
810 deaths (especially if PRESERVE_DEATH_NOTES_REGNO_P is not defined). We
811 can ignore subregs by extracting the actual register. BRC */
814 mcore_is_dead (first, reg)
815 rtx first;
816 rtx reg;
818 rtx insn;
820 /* For mcore, subregs can't live independently of their parent regs. */
821 if (GET_CODE (reg) == SUBREG)
822 reg = SUBREG_REG (reg);
824 /* Dies immediately. */
825 if (dead_or_set_p (first, reg))
826 return 1;
828 /* Look for conclusive evidence of live/death, otherwise we have
829 to assume that it is live. */
830 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
832 if (GET_CODE (insn) == JUMP_INSN)
833 return 0; /* We lose track, assume it is alive. */
835 else if (GET_CODE(insn) == CALL_INSN)
837 /* Call's might use it for target or register parms. */
838 if (reg_referenced_p (reg, PATTERN (insn))
839 || find_reg_fusage (insn, USE, reg))
840 return 0;
841 else if (dead_or_set_p (insn, reg))
842 return 1;
844 else if (GET_CODE (insn) == INSN)
846 if (reg_referenced_p (reg, PATTERN (insn)))
847 return 0;
848 else if (dead_or_set_p (insn, reg))
849 return 1;
853 /* No conclusive evidence either way, we can not take the chance
854 that control flow hid the use from us -- "I'm not dead yet". */
855 return 0;
859 /* Count the number of ones in mask. */
862 mcore_num_ones (mask)
863 int mask;
865 /* A trick to count set bits recently posted on comp.compilers. */
866 mask = (mask >> 1 & 0x55555555) + (mask & 0x55555555);
867 mask = ((mask >> 2) & 0x33333333) + (mask & 0x33333333);
868 mask = ((mask >> 4) + mask) & 0x0f0f0f0f;
869 mask = ((mask >> 8) + mask);
871 return (mask + (mask >> 16)) & 0xff;
874 /* Count the number of zeros in mask. */
877 mcore_num_zeros (mask)
878 int mask;
880 return 32 - mcore_num_ones (mask);
883 /* Determine byte being masked. */
886 mcore_byte_offset (mask)
887 unsigned int mask;
889 if (mask == 0x00ffffffUL)
890 return 0;
891 else if (mask == 0xff00ffffUL)
892 return 1;
893 else if (mask == 0xffff00ffUL)
894 return 2;
895 else if (mask == 0xffffff00UL)
896 return 3;
898 return -1;
901 /* Determine halfword being masked. */
904 mcore_halfword_offset (mask)
905 unsigned int mask;
907 if (mask == 0x0000ffffL)
908 return 0;
909 else if (mask == 0xffff0000UL)
910 return 1;
912 return -1;
915 /* Output a series of bseti's corresponding to mask. */
917 const char *
918 mcore_output_bseti (dst, mask)
919 rtx dst;
920 int mask;
922 rtx out_operands[2];
923 int bit;
925 out_operands[0] = dst;
927 for (bit = 0; bit < 32; bit++)
929 if ((mask & 0x1) == 0x1)
931 out_operands[1] = GEN_INT (bit);
933 output_asm_insn ("bseti\t%0,%1", out_operands);
935 mask >>= 1;
938 return "";
941 /* Output a series of bclri's corresponding to mask. */
943 const char *
944 mcore_output_bclri (dst, mask)
945 rtx dst;
946 int mask;
948 rtx out_operands[2];
949 int bit;
951 out_operands[0] = dst;
953 for (bit = 0; bit < 32; bit++)
955 if ((mask & 0x1) == 0x0)
957 out_operands[1] = GEN_INT (bit);
959 output_asm_insn ("bclri\t%0,%1", out_operands);
962 mask >>= 1;
965 return "";
968 /* Output a conditional move of two constants that are +/- 1 within each
969 other. See the "movtK" patterns in mcore.md. I'm not sure this is
970 really worth the effort. */
972 const char *
973 mcore_output_cmov (operands, cmp_t, test)
974 rtx operands[];
975 int cmp_t;
976 const char * test;
978 int load_value;
979 int adjust_value;
980 rtx out_operands[4];
982 out_operands[0] = operands[0];
984 /* Check to see which constant is loadable. */
985 if (const_ok_for_mcore (INTVAL (operands[1])))
987 out_operands[1] = operands[1];
988 out_operands[2] = operands[2];
990 else if (const_ok_for_mcore (INTVAL (operands[2])))
992 out_operands[1] = operands[2];
993 out_operands[2] = operands[1];
995 /* Complement test since constants are swapped. */
996 cmp_t = (cmp_t == 0);
998 load_value = INTVAL (out_operands[1]);
999 adjust_value = INTVAL (out_operands[2]);
1001 /* First output the test if folded into the pattern. */
1003 if (test)
1004 output_asm_insn (test, operands);
1006 /* Load the constant - for now, only support constants that can be
1007 generated with a single instruction. maybe add general inlinable
1008 constants later (this will increase the # of patterns since the
1009 instruction sequence has a different length attribute). */
1010 if (load_value >= 0 && load_value <= 127)
1011 output_asm_insn ("movi\t%0,%1", out_operands);
1012 else if ((load_value & (load_value - 1)) == 0)
1013 output_asm_insn ("bgeni\t%0,%P1", out_operands);
1014 else if ((load_value & (load_value + 1)) == 0)
1015 output_asm_insn ("bmaski\t%0,%N1", out_operands);
1017 /* Output the constant adjustment. */
1018 if (load_value > adjust_value)
1020 if (cmp_t)
1021 output_asm_insn ("decf\t%0", out_operands);
1022 else
1023 output_asm_insn ("dect\t%0", out_operands);
1025 else
1027 if (cmp_t)
1028 output_asm_insn ("incf\t%0", out_operands);
1029 else
1030 output_asm_insn ("inct\t%0", out_operands);
1033 return "";
1036 /* Outputs the peephole for moving a constant that gets not'ed followed
1037 by an and (i.e. combine the not and the and into andn). BRC */
1039 const char *
1040 mcore_output_andn (insn, operands)
1041 rtx insn ATTRIBUTE_UNUSED;
1042 rtx operands[];
1044 int x, y;
1045 rtx out_operands[3];
1046 const char * load_op;
1047 char buf[256];
1049 if (try_constant_tricks (INTVAL (operands[1]), &x, &y) != 2)
1050 abort ();
1052 out_operands[0] = operands[0];
1053 out_operands[1] = GEN_INT(x);
1054 out_operands[2] = operands[2];
1056 if (x >= 0 && x <= 127)
1057 load_op = "movi\t%0,%1";
1059 /* Try exact power of two. */
1060 else if ((x & (x - 1)) == 0)
1061 load_op = "bgeni\t%0,%P1";
1063 /* Try exact power of two - 1. */
1064 else if ((x & (x + 1)) == 0)
1065 load_op = "bmaski\t%0,%N1";
1067 else
1068 load_op = "BADMOVI\t%0,%1";
1070 sprintf (buf, "%s\n\tandn\t%%2,%%0", load_op);
1071 output_asm_insn (buf, out_operands);
1073 return "";
1076 /* Output an inline constant. */
1078 static const char *
1079 output_inline_const (mode, operands)
1080 enum machine_mode mode;
1081 rtx operands[];
1083 int x = 0, y = 0;
1084 int trick_no;
1085 rtx out_operands[3];
1086 char buf[256];
1087 char load_op[256];
1088 const char *dst_fmt;
1089 int value;
1091 value = INTVAL (operands[1]);
1093 if ((trick_no = try_constant_tricks (value, &x, &y)) == 0)
1095 /* lrw's are handled separately: Large inlinable constants
1096 never get turned into lrw's. Our caller uses try_constant_tricks
1097 to back off to an lrw rather than calling this routine. */
1098 abort ();
1101 if (trick_no == 1)
1102 x = value;
1104 /* operands: 0 = dst, 1 = load immed., 2 = immed. adjustment. */
1105 out_operands[0] = operands[0];
1106 out_operands[1] = GEN_INT (x);
1108 if (trick_no > 2)
1109 out_operands[2] = GEN_INT (y);
1111 /* Select dst format based on mode. */
1112 if (mode == DImode && (! TARGET_LITTLE_END))
1113 dst_fmt = "%R0";
1114 else
1115 dst_fmt = "%0";
1117 if (x >= 0 && x <= 127)
1118 sprintf (load_op, "movi\t%s,%%1", dst_fmt);
1120 /* Try exact power of two. */
1121 else if ((x & (x - 1)) == 0)
1122 sprintf (load_op, "bgeni\t%s,%%P1", dst_fmt);
1124 /* Try exact power of two - 1. */
1125 else if ((x & (x + 1)) == 0)
1126 sprintf (load_op, "bmaski\t%s,%%N1", dst_fmt);
1128 else
1129 sprintf (load_op, "BADMOVI\t%s,%%1", dst_fmt);
1131 switch (trick_no)
1133 case 1:
1134 strcpy (buf, load_op);
1135 break;
1136 case 2: /* not */
1137 sprintf (buf, "%s\n\tnot\t%s\t// %d 0x%x", load_op, dst_fmt, value, value);
1138 break;
1139 case 3: /* add */
1140 sprintf (buf, "%s\n\taddi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1141 break;
1142 case 4: /* sub */
1143 sprintf (buf, "%s\n\tsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1144 break;
1145 case 5: /* rsub */
1146 /* Never happens unless -mrsubi, see try_constant_tricks(). */
1147 sprintf (buf, "%s\n\trsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1148 break;
1149 case 6: /* bset */
1150 sprintf (buf, "%s\n\tbseti\t%s,%%P2\t// %d 0x%x", load_op, dst_fmt, value, value);
1151 break;
1152 case 7: /* bclr */
1153 sprintf (buf, "%s\n\tbclri\t%s,%%Q2\t// %d 0x%x", load_op, dst_fmt, value, value);
1154 break;
1155 case 8: /* rotl */
1156 sprintf (buf, "%s\n\trotli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1157 break;
1158 case 9: /* lsl */
1159 sprintf (buf, "%s\n\tlsli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1160 break;
1161 case 10: /* ixh */
1162 sprintf (buf, "%s\n\tixh\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1163 break;
1164 case 11: /* ixw */
1165 sprintf (buf, "%s\n\tixw\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1166 break;
1167 default:
1168 return "";
1171 output_asm_insn (buf, out_operands);
1173 return "";
1176 /* Output a move of a word or less value. */
1178 const char *
1179 mcore_output_move (insn, operands, mode)
1180 rtx insn ATTRIBUTE_UNUSED;
1181 rtx operands[];
1182 enum machine_mode mode ATTRIBUTE_UNUSED;
1184 rtx dst = operands[0];
1185 rtx src = operands[1];
1187 if (GET_CODE (dst) == REG)
1189 if (GET_CODE (src) == REG)
1191 if (REGNO (src) == CC_REG) /* r-c */
1192 return "mvc\t%0";
1193 else
1194 return "mov\t%0,%1"; /* r-r*/
1196 else if (GET_CODE (src) == MEM)
1198 if (GET_CODE (XEXP (src, 0)) == LABEL_REF)
1199 return "lrw\t%0,[%1]"; /* a-R */
1200 else
1201 return "ldw\t%0,%1"; /* r-m */
1203 else if (GET_CODE (src) == CONST_INT)
1205 int x, y;
1207 if (CONST_OK_FOR_I (INTVAL (src))) /* r-I */
1208 return "movi\t%0,%1";
1209 else if (CONST_OK_FOR_M (INTVAL (src))) /* r-M */
1210 return "bgeni\t%0,%P1\t// %1 %x1";
1211 else if (CONST_OK_FOR_N (INTVAL (src))) /* r-N */
1212 return "bmaski\t%0,%N1\t// %1 %x1";
1213 else if (try_constant_tricks (INTVAL (src), &x, &y)) /* R-P */
1214 return output_inline_const (SImode, operands); /* 1-2 insns */
1215 else
1216 return "lrw\t%0,%x1\t// %1"; /* Get it from literal pool. */
1218 else
1219 return "lrw\t%0, %1"; /* Into the literal pool. */
1221 else if (GET_CODE (dst) == MEM) /* m-r */
1222 return "stw\t%1,%0";
1224 abort ();
1227 /* Outputs a constant inline -- regardless of the cost.
1228 Useful for things where we've gotten into trouble and think we'd
1229 be doing an lrw into r15 (forbidden). This lets us get out of
1230 that pickle even after register allocation. */
1232 const char *
1233 mcore_output_inline_const_forced (insn, operands, mode)
1234 rtx insn ATTRIBUTE_UNUSED;
1235 rtx operands[];
1236 enum machine_mode mode ATTRIBUTE_UNUSED;
1238 unsigned long value = INTVAL (operands[1]);
1239 unsigned long ovalue = value;
1240 struct piece
1242 int low;
1243 int shift;
1245 part[6];
1246 int i;
1248 if (mcore_const_ok_for_inline (value))
1249 return output_inline_const (SImode, operands);
1251 for (i = 0; (unsigned) i < ARRAY_SIZE (part); i++)
1253 part[i].shift = 0;
1254 part[i].low = (value & 0x1F);
1255 value -= part[i].low;
1257 if (mcore_const_ok_for_inline (value))
1258 break;
1259 else
1261 value >>= 5;
1262 part[i].shift = 5;
1264 while ((value & 1) == 0)
1266 part[i].shift++;
1267 value >>= 1;
1270 if (mcore_const_ok_for_inline (value))
1271 break;
1275 /* 5 bits per iteration, a maximum of 5 times == 25 bits and leaves
1276 7 bits left in the constant -- which we know we can cover with
1277 a movi. The final value can't be zero otherwise we'd have stopped
1278 in the previous iteration. */
1279 if (value == 0 || ! mcore_const_ok_for_inline (value))
1280 abort ();
1282 /* Now, work our way backwards emitting the constant. */
1284 /* Emit the value that remains -- it will be non-zero. */
1285 operands[1] = GEN_INT (value);
1286 output_asm_insn (output_inline_const (SImode, operands), operands);
1288 while (i >= 0)
1290 /* Shift anything we've already loaded. */
1291 if (part[i].shift)
1293 operands[2] = GEN_INT (part[i].shift);
1294 output_asm_insn ("lsli %0,%2", operands);
1295 value <<= part[i].shift;
1298 /* Add anything we need into the low 5 bits. */
1299 if (part[i].low != 0)
1301 operands[2] = GEN_INT (part[i].low);
1302 output_asm_insn ("addi %0,%2", operands);
1303 value += part[i].low;
1306 i--;
1309 if (value != ovalue) /* sanity */
1310 abort ();
1312 /* We've output all the instructions. */
1313 return "";
1316 /* Return a sequence of instructions to perform DI or DF move.
1317 Since the MCORE cannot move a DI or DF in one instruction, we have
1318 to take care when we see overlapping source and dest registers. */
1320 const char *
1321 mcore_output_movedouble (operands, mode)
1322 rtx operands[];
1323 enum machine_mode mode ATTRIBUTE_UNUSED;
1325 rtx dst = operands[0];
1326 rtx src = operands[1];
1328 if (GET_CODE (dst) == REG)
1330 if (GET_CODE (src) == REG)
1332 int dstreg = REGNO (dst);
1333 int srcreg = REGNO (src);
1335 /* Ensure the second source not overwritten. */
1336 if (srcreg + 1 == dstreg)
1337 return "mov %R0,%R1\n\tmov %0,%1";
1338 else
1339 return "mov %0,%1\n\tmov %R0,%R1";
1341 else if (GET_CODE (src) == MEM)
1343 rtx memexp = memexp = XEXP (src, 0);
1344 int dstreg = REGNO (dst);
1345 int basereg = -1;
1347 if (GET_CODE (memexp) == LABEL_REF)
1348 return "lrw\t%0,[%1]\n\tlrw\t%R0,[%R1]";
1349 else if (GET_CODE (memexp) == REG)
1350 basereg = REGNO (memexp);
1351 else if (GET_CODE (memexp) == PLUS)
1353 if (GET_CODE (XEXP (memexp, 0)) == REG)
1354 basereg = REGNO (XEXP (memexp, 0));
1355 else if (GET_CODE (XEXP (memexp, 1)) == REG)
1356 basereg = REGNO (XEXP (memexp, 1));
1357 else
1358 abort ();
1360 else
1361 abort ();
1363 /* ??? length attribute is wrong here. */
1364 if (dstreg == basereg)
1366 /* Just load them in reverse order. */
1367 return "ldw\t%R0,%R1\n\tldw\t%0,%1";
1369 /* XXX: alternative: move basereg to basereg+1
1370 and then fall through. */
1372 else
1373 return "ldw\t%0,%1\n\tldw\t%R0,%R1";
1375 else if (GET_CODE (src) == CONST_INT)
1377 if (TARGET_LITTLE_END)
1379 if (CONST_OK_FOR_I (INTVAL (src)))
1380 output_asm_insn ("movi %0,%1", operands);
1381 else if (CONST_OK_FOR_M (INTVAL (src)))
1382 output_asm_insn ("bgeni %0,%P1", operands);
1383 else if (INTVAL (src) == -1)
1384 output_asm_insn ("bmaski %0,32", operands);
1385 else if (CONST_OK_FOR_N (INTVAL (src)))
1386 output_asm_insn ("bmaski %0,%N1", operands);
1387 else
1388 abort ();
1390 if (INTVAL (src) < 0)
1391 return "bmaski %R0,32";
1392 else
1393 return "movi %R0,0";
1395 else
1397 if (CONST_OK_FOR_I (INTVAL (src)))
1398 output_asm_insn ("movi %R0,%1", operands);
1399 else if (CONST_OK_FOR_M (INTVAL (src)))
1400 output_asm_insn ("bgeni %R0,%P1", operands);
1401 else if (INTVAL (src) == -1)
1402 output_asm_insn ("bmaski %R0,32", operands);
1403 else if (CONST_OK_FOR_N (INTVAL (src)))
1404 output_asm_insn ("bmaski %R0,%N1", operands);
1405 else
1406 abort ();
1408 if (INTVAL (src) < 0)
1409 return "bmaski %0,32";
1410 else
1411 return "movi %0,0";
1414 else
1415 abort ();
1417 else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
1418 return "stw\t%1,%0\n\tstw\t%R1,%R0";
1419 else
1420 abort ();
1423 /* Predicates used by the templates. */
1425 /* Non zero if OP can be source of a simple move operation. */
1428 mcore_general_movsrc_operand (op, mode)
1429 rtx op;
1430 enum machine_mode mode;
1432 /* Any (MEM LABEL_REF) is OK. That is a pc-relative load. */
1433 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == LABEL_REF)
1434 return 1;
1436 return general_operand (op, mode);
1439 /* Non zero if OP can be destination of a simple move operation. */
1442 mcore_general_movdst_operand (op, mode)
1443 rtx op;
1444 enum machine_mode mode;
1446 if (GET_CODE (op) == REG && REGNO (op) == CC_REG)
1447 return 0;
1449 return general_operand (op, mode);
1452 /* Nonzero if OP is a normal arithmetic register. */
1455 mcore_arith_reg_operand (op, mode)
1456 rtx op;
1457 enum machine_mode mode;
1459 if (! register_operand (op, mode))
1460 return 0;
1462 if (GET_CODE (op) == SUBREG)
1463 op = SUBREG_REG (op);
1465 if (GET_CODE (op) == REG)
1466 return REGNO (op) != CC_REG;
1468 return 1;
1471 /* Non zero if OP should be recognized during reload for an ixh/ixw
1472 operand. See the ixh/ixw patterns. */
1475 mcore_reload_operand (op, mode)
1476 rtx op;
1477 enum machine_mode mode;
1479 if (mcore_arith_reg_operand (op, mode))
1480 return 1;
1482 if (! reload_in_progress)
1483 return 0;
1485 return GET_CODE (op) == MEM;
1488 /* Nonzero if OP is a valid source operand for an arithmetic insn. */
1491 mcore_arith_J_operand (op, mode)
1492 rtx op;
1493 enum machine_mode mode;
1495 if (register_operand (op, mode))
1496 return 1;
1498 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
1499 return 1;
1501 return 0;
1504 /* Nonzero if OP is a valid source operand for an arithmetic insn. */
1507 mcore_arith_K_operand (op, mode)
1508 rtx op;
1509 enum machine_mode mode;
1511 if (register_operand (op, mode))
1512 return 1;
1514 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1515 return 1;
1517 return 0;
1520 /* Nonzero if OP is a valid source operand for a shift or rotate insn. */
1523 mcore_arith_K_operand_not_0 (op, mode)
1524 rtx op;
1525 enum machine_mode mode;
1527 if (register_operand (op, mode))
1528 return 1;
1530 if ( GET_CODE (op) == CONST_INT
1531 && CONST_OK_FOR_K (INTVAL (op))
1532 && INTVAL (op) != 0)
1533 return 1;
1535 return 0;
1539 mcore_arith_K_S_operand (op, mode)
1540 rtx op;
1541 enum machine_mode mode;
1543 if (register_operand (op, mode))
1544 return 1;
1546 if (GET_CODE (op) == CONST_INT)
1548 if (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_M (~INTVAL (op)))
1549 return 1;
1552 return 0;
1556 mcore_arith_S_operand (op)
1557 rtx op;
1559 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (~INTVAL (op)))
1560 return 1;
1562 return 0;
1566 mcore_arith_M_operand (op, mode)
1567 rtx op;
1568 enum machine_mode mode;
1570 if (register_operand (op, mode))
1571 return 1;
1573 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
1574 return 1;
1576 return 0;
1579 /* Nonzero if OP is a valid source operand for loading. */
1582 mcore_arith_imm_operand (op, mode)
1583 rtx op;
1584 enum machine_mode mode;
1586 if (register_operand (op, mode))
1587 return 1;
1589 if (GET_CODE (op) == CONST_INT && const_ok_for_mcore (INTVAL (op)))
1590 return 1;
1592 return 0;
1596 mcore_arith_any_imm_operand (op, mode)
1597 rtx op;
1598 enum machine_mode mode;
1600 if (register_operand (op, mode))
1601 return 1;
1603 if (GET_CODE (op) == CONST_INT)
1604 return 1;
1606 return 0;
1609 /* Nonzero if OP is a valid source operand for a cmov with two consts +/- 1. */
1612 mcore_arith_O_operand (op, mode)
1613 rtx op;
1614 enum machine_mode mode;
1616 if (register_operand (op, mode))
1617 return 1;
1619 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_O (INTVAL (op)))
1620 return 1;
1622 return 0;
1625 /* Nonzero if OP is a valid source operand for a btsti. */
1628 mcore_literal_K_operand (op, mode)
1629 rtx op;
1630 enum machine_mode mode ATTRIBUTE_UNUSED;
1632 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1633 return 1;
1635 return 0;
1638 /* Nonzero if OP is a valid source operand for an add/sub insn. */
1641 mcore_addsub_operand (op, mode)
1642 rtx op;
1643 enum machine_mode mode;
1645 if (register_operand (op, mode))
1646 return 1;
1648 if (GET_CODE (op) == CONST_INT)
1650 return 1;
1652 /* The following is removed because it precludes large constants from being
1653 returned as valid source operands for and add/sub insn. While large
1654 constants may not directly be used in an add/sub, they may if first loaded
1655 into a register. Thus, this predicate should indicate that they are valid,
1656 and the constraint in mcore.md should control whether an additional load to
1657 register is needed. (see mcore.md, addsi). -- DAC 4/2/1998 */
1659 if (CONST_OK_FOR_J(INTVAL(op)) || CONST_OK_FOR_L(INTVAL(op)))
1660 return 1;
1664 return 0;
1667 /* Nonzero if OP is a valid source operand for a compare operation. */
1670 mcore_compare_operand (op, mode)
1671 rtx op;
1672 enum machine_mode mode;
1674 if (register_operand (op, mode))
1675 return 1;
1677 if (GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
1678 return 1;
1680 return 0;
1683 /* Expand insert bit field. BRC */
1686 mcore_expand_insv (operands)
1687 rtx operands[];
1689 int width = INTVAL (operands[1]);
1690 int posn = INTVAL (operands[2]);
1691 int mask;
1692 rtx mreg, sreg, ereg;
1694 /* To get width 1 insv, the test in store_bit_field() (expmed.c, line 191)
1695 for width==1 must be removed. Look around line 368. This is something
1696 we really want the md part to do. */
1697 if (width == 1 && GET_CODE (operands[3]) == CONST_INT)
1699 /* Do directly with bseti or bclri. */
1700 /* RBE: 2/97 consider only low bit of constant. */
1701 if ((INTVAL(operands[3])&1) == 0)
1703 mask = ~(1 << posn);
1704 emit_insn (gen_rtx (SET, SImode, operands[0],
1705 gen_rtx (AND, SImode, operands[0], GEN_INT (mask))));
1707 else
1709 mask = 1 << posn;
1710 emit_insn (gen_rtx (SET, SImode, operands[0],
1711 gen_rtx (IOR, SImode, operands[0], GEN_INT (mask))));
1714 return 1;
1717 /* Look at some bitfield placements that we aren't interested
1718 in handling ourselves, unless specifically directed to do so. */
1719 if (! TARGET_W_FIELD)
1720 return 0; /* Generally, give up about now. */
1722 if (width == 8 && posn % 8 == 0)
1723 /* Byte sized and aligned; let caller break it up. */
1724 return 0;
1726 if (width == 16 && posn % 16 == 0)
1727 /* Short sized and aligned; let caller break it up. */
1728 return 0;
1730 /* The general case - we can do this a little bit better than what the
1731 machine independent part tries. This will get rid of all the subregs
1732 that mess up constant folding in combine when working with relaxed
1733 immediates. */
1735 /* If setting the entire field, do it directly. */
1736 if (GET_CODE (operands[3]) == CONST_INT &&
1737 INTVAL (operands[3]) == ((1 << width) - 1))
1739 mreg = force_reg (SImode, GEN_INT (INTVAL (operands[3]) << posn));
1740 emit_insn (gen_rtx (SET, SImode, operands[0],
1741 gen_rtx (IOR, SImode, operands[0], mreg)));
1742 return 1;
1745 /* Generate the clear mask. */
1746 mreg = force_reg (SImode, GEN_INT (~(((1 << width) - 1) << posn)));
1748 /* Clear the field, to overlay it later with the source. */
1749 emit_insn (gen_rtx (SET, SImode, operands[0],
1750 gen_rtx (AND, SImode, operands[0], mreg)));
1752 /* If the source is constant 0, we've nothing to add back. */
1753 if (GET_CODE (operands[3]) == CONST_INT && INTVAL (operands[3]) == 0)
1754 return 1;
1756 /* XXX: Should we worry about more games with constant values?
1757 We've covered the high profile: set/clear single-bit and many-bit
1758 fields. How often do we see "arbitrary bit pattern" constants? */
1759 sreg = copy_to_mode_reg (SImode, operands[3]);
1761 /* Extract src as same width as dst (needed for signed values). We
1762 always have to do this since we widen everything to SImode.
1763 We don't have to mask if we're shifting this up against the
1764 MSB of the register (e.g., the shift will push out any hi-order
1765 bits. */
1766 if (width + posn != (int) GET_MODE_SIZE (SImode))
1768 ereg = force_reg (SImode, GEN_INT ((1 << width) - 1));
1769 emit_insn (gen_rtx (SET, SImode, sreg,
1770 gen_rtx (AND, SImode, sreg, ereg)));
1773 /* Insert source value in dest. */
1774 if (posn != 0)
1775 emit_insn (gen_rtx (SET, SImode, sreg,
1776 gen_rtx (ASHIFT, SImode, sreg, GEN_INT (posn))));
1778 emit_insn (gen_rtx (SET, SImode, operands[0],
1779 gen_rtx (IOR, SImode, operands[0], sreg)));
1781 return 1;
1784 /* Return 1 if OP is a load multiple operation. It is known to be a
1785 PARALLEL and the first section will be tested. */
1787 mcore_load_multiple_operation (op, mode)
1788 rtx op;
1789 enum machine_mode mode ATTRIBUTE_UNUSED;
1791 int count = XVECLEN (op, 0);
1792 int dest_regno;
1793 rtx src_addr;
1794 int i;
1796 /* Perform a quick check so we don't blow up below. */
1797 if (count <= 1
1798 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1799 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
1800 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
1801 return 0;
1803 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
1804 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
1806 for (i = 1; i < count; i++)
1808 rtx elt = XVECEXP (op, 0, i);
1810 if (GET_CODE (elt) != SET
1811 || GET_CODE (SET_DEST (elt)) != REG
1812 || GET_MODE (SET_DEST (elt)) != SImode
1813 || REGNO (SET_DEST (elt)) != (unsigned) (dest_regno + i)
1814 || GET_CODE (SET_SRC (elt)) != MEM
1815 || GET_MODE (SET_SRC (elt)) != SImode
1816 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
1817 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
1818 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
1819 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
1820 return 0;
1823 return 1;
1826 /* Similar, but tests for store multiple. */
1829 mcore_store_multiple_operation (op, mode)
1830 rtx op;
1831 enum machine_mode mode ATTRIBUTE_UNUSED;
1833 int count = XVECLEN (op, 0);
1834 int src_regno;
1835 rtx dest_addr;
1836 int i;
1838 /* Perform a quick check so we don't blow up below. */
1839 if (count <= 1
1840 || GET_CODE (XVECEXP (op, 0, 0)) != SET
1841 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
1842 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
1843 return 0;
1845 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
1846 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
1848 for (i = 1; i < count; i++)
1850 rtx elt = XVECEXP (op, 0, i);
1852 if (GET_CODE (elt) != SET
1853 || GET_CODE (SET_SRC (elt)) != REG
1854 || GET_MODE (SET_SRC (elt)) != SImode
1855 || REGNO (SET_SRC (elt)) != (unsigned) (src_regno + i)
1856 || GET_CODE (SET_DEST (elt)) != MEM
1857 || GET_MODE (SET_DEST (elt)) != SImode
1858 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
1859 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
1860 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
1861 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
1862 return 0;
1865 return 1;
1868 /* ??? Block move stuff stolen from m88k. This code has not been
1869 verified for correctness. */
1871 /* Emit code to perform a block move. Choose the best method.
1873 OPERANDS[0] is the destination.
1874 OPERANDS[1] is the source.
1875 OPERANDS[2] is the size.
1876 OPERANDS[3] is the alignment safe to use. */
1878 /* Emit code to perform a block move with an offset sequence of ldw/st
1879 instructions (..., ldw 0, stw 1, ldw 1, stw 0, ...). SIZE and ALIGN are
1880 known constants. DEST and SRC are registers. OFFSET is the known
1881 starting point for the output pattern. */
1883 static const enum machine_mode mode_from_align[] =
1885 VOIDmode, QImode, HImode, VOIDmode, SImode,
1886 VOIDmode, VOIDmode, VOIDmode, DImode
1889 static void
1890 block_move_sequence (dest, dst_mem, src, src_mem, size, align, offset)
1891 rtx dest, dst_mem;
1892 rtx src, src_mem;
1893 int size;
1894 int align;
1895 int offset;
1897 rtx temp[2];
1898 enum machine_mode mode[2];
1899 int amount[2];
1900 int active[2];
1901 int phase = 0;
1902 int next;
1903 int offset_ld = offset;
1904 int offset_st = offset;
1906 active[0] = active[1] = FALSE;
1908 /* Establish parameters for the first load and for the second load if
1909 it is known to be the same mode as the first. */
1910 amount[0] = amount[1] = align;
1912 mode[0] = mode_from_align[align];
1914 temp[0] = gen_reg_rtx (mode[0]);
1916 if (size >= 2 * align)
1918 mode[1] = mode[0];
1919 temp[1] = gen_reg_rtx (mode[1]);
1924 rtx srcp, dstp;
1926 next = phase;
1927 phase = !phase;
1929 if (size > 0)
1931 /* Change modes as the sequence tails off. */
1932 if (size < amount[next])
1934 amount[next] = (size >= 4 ? 4 : (size >= 2 ? 2 : 1));
1935 mode[next] = mode_from_align[amount[next]];
1936 temp[next] = gen_reg_rtx (mode[next]);
1939 size -= amount[next];
1940 srcp = gen_rtx (MEM,
1941 #if 0
1942 MEM_IN_STRUCT_P (src_mem) ? mode[next] : BLKmode,
1943 #else
1944 mode[next],
1945 #endif
1946 gen_rtx (PLUS, Pmode, src,
1947 gen_rtx (CONST_INT, SImode, offset_ld)));
1949 RTX_UNCHANGING_P (srcp) = RTX_UNCHANGING_P (src_mem);
1950 MEM_VOLATILE_P (srcp) = MEM_VOLATILE_P (src_mem);
1951 MEM_IN_STRUCT_P (srcp) = 1;
1952 emit_insn (gen_rtx (SET, VOIDmode, temp[next], srcp));
1953 offset_ld += amount[next];
1954 active[next] = TRUE;
1957 if (active[phase])
1959 active[phase] = FALSE;
1961 dstp = gen_rtx (MEM,
1962 #if 0
1963 MEM_IN_STRUCT_P (dst_mem) ? mode[phase] : BLKmode,
1964 #else
1965 mode[phase],
1966 #endif
1967 gen_rtx (PLUS, Pmode, dest,
1968 gen_rtx (CONST_INT, SImode, offset_st)));
1970 RTX_UNCHANGING_P (dstp) = RTX_UNCHANGING_P (dst_mem);
1971 MEM_VOLATILE_P (dstp) = MEM_VOLATILE_P (dst_mem);
1972 MEM_IN_STRUCT_P (dstp) = 1;
1973 emit_insn (gen_rtx (SET, VOIDmode, dstp, temp[phase]));
1974 offset_st += amount[phase];
1977 while (active[next]);
1980 void
1981 mcore_expand_block_move (dst_mem, src_mem, operands)
1982 rtx dst_mem;
1983 rtx src_mem;
1984 rtx * operands;
1986 int align = INTVAL (operands[3]);
1987 int bytes;
1989 if (GET_CODE (operands[2]) == CONST_INT)
1991 bytes = INTVAL (operands[2]);
1993 if (bytes <= 0)
1994 return;
1995 if (align > 4)
1996 align = 4;
1998 /* RBE: bumped 1 and 2 byte align from 1 and 2 to 4 and 8 bytes before
1999 we give up and go to memcpy. */
2000 if ((align == 4 && (bytes <= 4*4
2001 || ((bytes & 01) == 0 && bytes <= 8*4)
2002 || ((bytes & 03) == 0 && bytes <= 16*4)))
2003 || (align == 2 && bytes <= 4*2)
2004 || (align == 1 && bytes <= 4*1))
2006 block_move_sequence (operands[0], dst_mem, operands[1], src_mem,
2007 bytes, align, 0);
2008 return;
2012 /* If we get here, just use the library routine. */
2013 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "memcpy"), 0, VOIDmode, 3,
2014 operands[0], Pmode, operands[1], Pmode, operands[2],
2015 SImode);
2019 /* Code to generate prologue and epilogue sequences. */
2020 static int number_of_regs_before_varargs;
2022 /* Set by SETUP_INCOMING_VARARGS to indicate to prolog that this is
2023 for a varargs function. */
2024 static int current_function_anonymous_args;
2026 #define STACK_BYTES (STACK_BOUNDARY/BITS_PER_UNIT)
2027 #define STORE_REACH (64) /* Maximum displace of word store + 4. */
2028 #define ADDI_REACH (32) /* Maximum addi operand. */
2030 static void
2031 layout_mcore_frame (infp)
2032 struct mcore_frame * infp;
2034 int n;
2035 unsigned int i;
2036 int nbytes;
2037 int regarg;
2038 int localregarg;
2039 int localreg;
2040 int outbounds;
2041 unsigned int growths;
2042 int step;
2044 /* Might have to spill bytes to re-assemble a big argument that
2045 was passed partially in registers and partially on the stack. */
2046 nbytes = current_function_pretend_args_size;
2048 /* Determine how much space for spilled anonymous args (e.g., stdarg). */
2049 if (current_function_anonymous_args)
2050 nbytes += (NPARM_REGS - number_of_regs_before_varargs) * UNITS_PER_WORD;
2052 infp->arg_size = nbytes;
2054 /* How much space to save non-volatile registers we stomp. */
2055 infp->reg_mask = calc_live_regs (& n);
2056 infp->reg_size = n * 4;
2058 /* And the rest of it... locals and space for overflowed outbounds. */
2059 infp->local_size = get_frame_size ();
2060 infp->outbound_size = current_function_outgoing_args_size;
2062 /* Make sure we have a whole number of words for the locals. */
2063 if (infp->local_size % STACK_BYTES)
2064 infp->local_size = (infp->local_size + STACK_BYTES - 1) & ~ (STACK_BYTES -1);
2066 /* Only thing we know we have to pad is the outbound space, since
2067 we've aligned our locals assuming that base of locals is aligned. */
2068 infp->pad_local = 0;
2069 infp->pad_reg = 0;
2070 infp->pad_outbound = 0;
2071 if (infp->outbound_size % STACK_BYTES)
2072 infp->pad_outbound = STACK_BYTES - (infp->outbound_size % STACK_BYTES);
2074 /* Now we see how we want to stage the prologue so that it does
2075 the most appropriate stack growth and register saves to either:
2076 (1) run fast,
2077 (2) reduce instruction space, or
2078 (3) reduce stack space. */
2079 for (i = 0; i < ARRAY_SIZE (infp->growth); i++)
2080 infp->growth[i] = 0;
2082 regarg = infp->reg_size + infp->arg_size;
2083 localregarg = infp->local_size + regarg;
2084 localreg = infp->local_size + infp->reg_size;
2085 outbounds = infp->outbound_size + infp->pad_outbound;
2086 growths = 0;
2088 /* XXX: Consider one where we consider localregarg + outbound too! */
2090 /* Frame of <= 32 bytes and using stm would get <= 2 registers.
2091 use stw's with offsets and buy the frame in one shot. */
2092 if (localregarg <= ADDI_REACH
2093 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2095 /* Make sure we'll be aligned. */
2096 if (localregarg % STACK_BYTES)
2097 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2099 step = localregarg + infp->pad_reg;
2100 infp->reg_offset = infp->local_size;
2102 if (outbounds + step <= ADDI_REACH && !frame_pointer_needed)
2104 step += outbounds;
2105 infp->reg_offset += outbounds;
2106 outbounds = 0;
2109 infp->arg_offset = step - 4;
2110 infp->growth[growths++] = step;
2111 infp->reg_growth = growths;
2112 infp->local_growth = growths;
2114 /* If we haven't already folded it in. */
2115 if (outbounds)
2116 infp->growth[growths++] = outbounds;
2118 goto finish;
2121 /* Frame can't be done with a single subi, but can be done with 2
2122 insns. If the 'stm' is getting <= 2 registers, we use stw's and
2123 shift some of the stack purchase into the first subi, so both are
2124 single instructions. */
2125 if (localregarg <= STORE_REACH
2126 && (infp->local_size > ADDI_REACH)
2127 && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2129 int all;
2131 /* Make sure we'll be aligned; use either pad_reg or pad_local. */
2132 if (localregarg % STACK_BYTES)
2133 infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2135 all = localregarg + infp->pad_reg + infp->pad_local;
2136 step = ADDI_REACH; /* As much up front as we can. */
2137 if (step > all)
2138 step = all;
2140 /* XXX: Consider whether step will still be aligned; we believe so. */
2141 infp->arg_offset = step - 4;
2142 infp->growth[growths++] = step;
2143 infp->reg_growth = growths;
2144 infp->reg_offset = step - infp->pad_reg - infp->reg_size;
2145 all -= step;
2147 /* Can we fold in any space required for outbounds? */
2148 if (outbounds + all <= ADDI_REACH && !frame_pointer_needed)
2150 all += outbounds;
2151 outbounds = 0;
2154 /* Get the rest of the locals in place. */
2155 step = all;
2156 infp->growth[growths++] = step;
2157 infp->local_growth = growths;
2158 all -= step;
2160 assert (all == 0);
2162 /* Finish off if we need to do so. */
2163 if (outbounds)
2164 infp->growth[growths++] = outbounds;
2166 goto finish;
2169 /* Registers + args is nicely aligned, so we'll buy that in one shot.
2170 Then we buy the rest of the frame in 1 or 2 steps depending on
2171 whether we need a frame pointer. */
2172 if ((regarg % STACK_BYTES) == 0)
2174 infp->growth[growths++] = regarg;
2175 infp->reg_growth = growths;
2176 infp->arg_offset = regarg - 4;
2177 infp->reg_offset = 0;
2179 if (infp->local_size % STACK_BYTES)
2180 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2182 step = infp->local_size + infp->pad_local;
2184 if (!frame_pointer_needed)
2186 step += outbounds;
2187 outbounds = 0;
2190 infp->growth[growths++] = step;
2191 infp->local_growth = growths;
2193 /* If there's any left to be done. */
2194 if (outbounds)
2195 infp->growth[growths++] = outbounds;
2197 goto finish;
2200 /* XXX: optimizations that we'll want to play with....
2201 -- regarg is not aligned, but it's a small number of registers;
2202 use some of localsize so that regarg is aligned and then
2203 save the registers. */
2205 /* Simple encoding; plods down the stack buying the pieces as it goes.
2206 -- does not optimize space consumption.
2207 -- does not attempt to optimize instruction counts.
2208 -- but it is safe for all alignments. */
2209 if (regarg % STACK_BYTES != 0)
2210 infp->pad_reg = STACK_BYTES - (regarg % STACK_BYTES);
2212 infp->growth[growths++] = infp->arg_size + infp->reg_size + infp->pad_reg;
2213 infp->reg_growth = growths;
2214 infp->arg_offset = infp->growth[0] - 4;
2215 infp->reg_offset = 0;
2217 if (frame_pointer_needed)
2219 if (infp->local_size % STACK_BYTES != 0)
2220 infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2222 infp->growth[growths++] = infp->local_size + infp->pad_local;
2223 infp->local_growth = growths;
2225 infp->growth[growths++] = outbounds;
2227 else
2229 if ((infp->local_size + outbounds) % STACK_BYTES != 0)
2230 infp->pad_local = STACK_BYTES - ((infp->local_size + outbounds) % STACK_BYTES);
2232 infp->growth[growths++] = infp->local_size + infp->pad_local + outbounds;
2233 infp->local_growth = growths;
2236 /* Anything else that we've forgotten?, plus a few consistency checks. */
2237 finish:
2238 assert (infp->reg_offset >= 0);
2239 assert (growths <= MAX_STACK_GROWS);
2241 for (i = 0; i < growths; i++)
2243 if (infp->growth[i] % STACK_BYTES)
2245 fprintf (stderr,"stack growth of %d is not %d aligned\n",
2246 infp->growth[i], STACK_BYTES);
2247 abort ();
2252 /* Define the offset between two registers, one to be eliminated, and
2253 the other its replacement, at the start of a routine. */
2256 mcore_initial_elimination_offset (from, to)
2257 int from;
2258 int to;
2260 int above_frame;
2261 int below_frame;
2262 struct mcore_frame fi;
2264 layout_mcore_frame (& fi);
2266 /* fp to ap */
2267 above_frame = fi.local_size + fi.pad_local + fi.reg_size + fi.pad_reg;
2268 /* sp to fp */
2269 below_frame = fi.outbound_size + fi.pad_outbound;
2271 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
2272 return above_frame;
2274 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2275 return above_frame + below_frame;
2277 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2278 return below_frame;
2280 abort ();
2282 return 0;
2285 /* Keep track of some information about varargs for the prolog. */
2287 void
2288 mcore_setup_incoming_varargs (args_so_far, mode, type, ptr_pretend_size)
2289 CUMULATIVE_ARGS args_so_far;
2290 enum machine_mode mode;
2291 tree type;
2292 int * ptr_pretend_size ATTRIBUTE_UNUSED;
2294 current_function_anonymous_args = 1;
2296 /* We need to know how many argument registers are used before
2297 the varargs start, so that we can push the remaining argument
2298 registers during the prologue. */
2299 number_of_regs_before_varargs = args_so_far + mcore_num_arg_regs (mode, type);
2301 /* There is a bug somwehere in the arg handling code.
2302 Until I can find it this workaround always pushes the
2303 last named argument onto the stack. */
2304 number_of_regs_before_varargs = args_so_far;
2306 /* The last named argument may be split between argument registers
2307 and the stack. Allow for this here. */
2308 if (number_of_regs_before_varargs > NPARM_REGS)
2309 number_of_regs_before_varargs = NPARM_REGS;
2312 void
2313 mcore_expand_prolog ()
2315 struct mcore_frame fi;
2316 int space_allocated = 0;
2317 int growth = 0;
2319 /* Find out what we're doing. */
2320 layout_mcore_frame (&fi);
2322 space_allocated = fi.arg_size + fi.reg_size + fi.local_size +
2323 fi.outbound_size + fi.pad_outbound + fi.pad_local + fi.pad_reg;
2325 if (TARGET_CG_DATA)
2327 /* Emit a symbol for this routine's frame size. */
2328 rtx x;
2329 int len;
2331 x = DECL_RTL (current_function_decl);
2333 if (GET_CODE (x) != MEM)
2334 abort ();
2336 x = XEXP (x, 0);
2338 if (GET_CODE (x) != SYMBOL_REF)
2339 abort ();
2341 if (mcore_current_function_name)
2342 free (mcore_current_function_name);
2344 len = strlen (XSTR (x, 0)) + 1;
2345 mcore_current_function_name = (char *) xmalloc (len);
2347 memcpy (mcore_current_function_name, XSTR (x, 0), len);
2349 ASM_OUTPUT_CG_NODE (asm_out_file, mcore_current_function_name, space_allocated);
2351 if (current_function_calls_alloca)
2352 ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, "alloca", 1);
2354 /* 970425: RBE:
2355 We're looking at how the 8byte alignment affects stack layout
2356 and where we had to pad things. This emits information we can
2357 extract which tells us about frame sizes and the like. */
2358 fprintf (asm_out_file,
2359 "\t.equ\t__$frame$info$_%s_$_%d_%d_x%x_%d_%d_%d,0\n",
2360 mcore_current_function_name,
2361 fi.arg_size, fi.reg_size, fi.reg_mask,
2362 fi.local_size, fi.outbound_size,
2363 frame_pointer_needed);
2366 if (mcore_naked_function_p ())
2367 return;
2369 /* Handle stdarg+regsaves in one shot: can't be more than 64 bytes. */
2370 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2372 /* If we have a parameter passed partially in regs and partially in memory,
2373 the registers will have been stored to memory already in function.c. So
2374 we only need to do something here for varargs functions. */
2375 if (fi.arg_size != 0 && current_function_pretend_args_size == 0)
2377 int offset;
2378 int rn = FIRST_PARM_REG + NPARM_REGS - 1;
2379 int remaining = fi.arg_size;
2381 for (offset = fi.arg_offset; remaining >= 4; offset -= 4, rn--, remaining -= 4)
2383 emit_insn (gen_movsi
2384 (gen_rtx (MEM, SImode,
2385 plus_constant (stack_pointer_rtx, offset)),
2386 gen_rtx (REG, SImode, rn)));
2390 /* Do we need another stack adjustment before we do the register saves? */
2391 if (growth < fi.reg_growth)
2392 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2394 if (fi.reg_size != 0)
2396 int i;
2397 int offs = fi.reg_offset;
2399 for (i = 15; i >= 0; i--)
2401 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2403 int first_reg = 15;
2405 while (fi.reg_mask & (1 << first_reg))
2406 first_reg--;
2407 first_reg++;
2409 emit_insn (gen_store_multiple (gen_rtx (MEM, SImode, stack_pointer_rtx),
2410 gen_rtx (REG, SImode, first_reg),
2411 GEN_INT (16 - first_reg)));
2413 i -= (15 - first_reg);
2414 offs += (16 - first_reg) * 4;
2416 else if (fi.reg_mask & (1 << i))
2418 emit_insn (gen_movsi
2419 (gen_rtx (MEM, SImode,
2420 plus_constant (stack_pointer_rtx, offs)),
2421 gen_rtx (REG, SImode, i)));
2422 offs += 4;
2427 /* Figure the locals + outbounds. */
2428 if (frame_pointer_needed)
2430 /* If we haven't already purchased to 'fp'. */
2431 if (growth < fi.local_growth)
2432 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2434 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2436 /* ... and then go any remaining distance for outbounds, etc. */
2437 if (fi.growth[growth])
2438 output_stack_adjust (-1, fi.growth[growth++]);
2440 else
2442 if (growth < fi.local_growth)
2443 output_stack_adjust (-1, fi.growth[growth++]); /* grows it */
2444 if (fi.growth[growth])
2445 output_stack_adjust (-1, fi.growth[growth++]);
2449 void
2450 mcore_expand_epilog ()
2452 struct mcore_frame fi;
2453 int i;
2454 int offs;
2455 int growth = MAX_STACK_GROWS - 1 ;
2458 /* Find out what we're doing. */
2459 layout_mcore_frame(&fi);
2461 if (mcore_naked_function_p ())
2462 return;
2464 /* If we had a frame pointer, restore the sp from that. */
2465 if (frame_pointer_needed)
2467 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2468 growth = fi.local_growth - 1;
2470 else
2472 /* XXX: while loop should accumulate and do a single sell. */
2473 while (growth >= fi.local_growth)
2475 if (fi.growth[growth] != 0)
2476 output_stack_adjust (1, fi.growth[growth]);
2477 growth--;
2481 /* Make sure we've shrunk stack back to the point where the registers
2482 were laid down. This is typically 0/1 iterations. Then pull the
2483 register save information back off the stack. */
2484 while (growth >= fi.reg_growth)
2485 output_stack_adjust ( 1, fi.growth[growth--]);
2487 offs = fi.reg_offset;
2489 for (i = 15; i >= 0; i--)
2491 if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2493 int first_reg;
2495 /* Find the starting register. */
2496 first_reg = 15;
2498 while (fi.reg_mask & (1 << first_reg))
2499 first_reg--;
2501 first_reg++;
2503 emit_insn (gen_load_multiple (gen_rtx (REG, SImode, first_reg),
2504 gen_rtx (MEM, SImode, stack_pointer_rtx),
2505 GEN_INT (16 - first_reg)));
2507 i -= (15 - first_reg);
2508 offs += (16 - first_reg) * 4;
2510 else if (fi.reg_mask & (1 << i))
2512 emit_insn (gen_movsi
2513 (gen_rtx (REG, SImode, i),
2514 gen_rtx (MEM, SImode,
2515 plus_constant (stack_pointer_rtx, offs))));
2516 offs += 4;
2520 /* Give back anything else. */
2521 /* XXX: Should accumuate total and then give it back. */
2522 while (growth >= 0)
2523 output_stack_adjust ( 1, fi.growth[growth--]);
2526 /* This code is borrowed from the SH port. */
2528 /* The MCORE cannot load a large constant into a register, constants have to
2529 come from a pc relative load. The reference of a pc relative load
2530 instruction must be less than 1k infront of the instruction. This
2531 means that we often have to dump a constant inside a function, and
2532 generate code to branch around it.
2534 It is important to minimize this, since the branches will slow things
2535 down and make things bigger.
2537 Worst case code looks like:
2539 lrw L1,r0
2540 br L2
2541 align
2542 L1: .long value
2546 lrw L3,r0
2547 br L4
2548 align
2549 L3: .long value
2553 We fix this by performing a scan before scheduling, which notices which
2554 instructions need to have their operands fetched from the constant table
2555 and builds the table.
2557 The algorithm is:
2559 scan, find an instruction which needs a pcrel move. Look forward, find the
2560 last barrier which is within MAX_COUNT bytes of the requirement.
2561 If there isn't one, make one. Process all the instructions between
2562 the find and the barrier.
2564 In the above example, we can tell that L3 is within 1k of L1, so
2565 the first move can be shrunk from the 2 insn+constant sequence into
2566 just 1 insn, and the constant moved to L3 to make:
2568 lrw L1,r0
2570 lrw L3,r0
2571 bra L4
2572 align
2573 L3:.long value
2574 L4:.long value
2576 Then the second move becomes the target for the shortening process. */
2578 typedef struct
2580 rtx value; /* Value in table. */
2581 rtx label; /* Label of value. */
2582 } pool_node;
2584 /* The maximum number of constants that can fit into one pool, since
2585 the pc relative range is 0...1020 bytes and constants are at least 4
2586 bytes long. We subtact 4 from the range to allow for the case where
2587 we need to add a branch/align before the constant pool. */
2589 #define MAX_COUNT 1016
2590 #define MAX_POOL_SIZE (MAX_COUNT/4)
2591 static pool_node pool_vector[MAX_POOL_SIZE];
2592 static int pool_size;
2594 /* Dump out any constants accumulated in the final pass. These
2595 will only be labels. */
2597 const char *
2598 mcore_output_jump_label_table ()
2600 int i;
2602 if (pool_size)
2604 fprintf (asm_out_file, "\t.align 2\n");
2606 for (i = 0; i < pool_size; i++)
2608 pool_node * p = pool_vector + i;
2610 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (p->label));
2612 output_asm_insn (".long %0", &p->value);
2615 pool_size = 0;
2618 return "";
2621 /* Check whether insn is a candidate for a conditional. */
2623 static cond_type
2624 is_cond_candidate (insn)
2625 rtx insn;
2627 /* The only things we conditionalize are those that can be directly
2628 changed into a conditional. Only bother with SImode items. If
2629 we wanted to be a little more aggressive, we could also do other
2630 modes such as DImode with reg-reg move or load 0. */
2631 if (GET_CODE (insn) == INSN)
2633 rtx pat = PATTERN (insn);
2634 rtx src, dst;
2636 if (GET_CODE (pat) != SET)
2637 return COND_NO;
2639 dst = XEXP (pat, 0);
2641 if ((GET_CODE (dst) != REG &&
2642 GET_CODE (dst) != SUBREG) ||
2643 GET_MODE (dst) != SImode)
2644 return COND_NO;
2646 src = XEXP (pat, 1);
2648 if ((GET_CODE (src) == REG ||
2649 (GET_CODE (src) == SUBREG &&
2650 GET_CODE (SUBREG_REG (src)) == REG)) &&
2651 GET_MODE (src) == SImode)
2652 return COND_MOV_INSN;
2653 else if (GET_CODE (src) == CONST_INT &&
2654 INTVAL (src) == 0)
2655 return COND_CLR_INSN;
2656 else if (GET_CODE (src) == PLUS &&
2657 (GET_CODE (XEXP (src, 0)) == REG ||
2658 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2659 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2660 GET_MODE (XEXP (src, 0)) == SImode &&
2661 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2662 INTVAL (XEXP (src, 1)) == 1)
2663 return COND_INC_INSN;
2664 else if (((GET_CODE (src) == MINUS &&
2665 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2666 INTVAL( XEXP (src, 1)) == 1) ||
2667 (GET_CODE (src) == PLUS &&
2668 GET_CODE (XEXP (src, 1)) == CONST_INT &&
2669 INTVAL (XEXP (src, 1)) == -1)) &&
2670 (GET_CODE (XEXP (src, 0)) == REG ||
2671 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2672 GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2673 GET_MODE (XEXP (src, 0)) == SImode)
2674 return COND_DEC_INSN;
2676 /* some insns that we don't bother with:
2677 (set (rx:DI) (ry:DI))
2678 (set (rx:DI) (const_int 0))
2682 else if (GET_CODE (insn) == JUMP_INSN &&
2683 GET_CODE (PATTERN (insn)) == SET &&
2684 GET_CODE (XEXP (PATTERN (insn), 1)) == LABEL_REF)
2685 return COND_BRANCH_INSN;
2687 return COND_NO;
2690 /* Emit a conditional version of insn and replace the old insn with the
2691 new one. Return the new insn if emitted. */
2693 static rtx
2694 emit_new_cond_insn (insn, cond)
2695 rtx insn;
2696 int cond;
2698 rtx c_insn = 0;
2699 rtx pat, dst, src;
2700 cond_type num;
2702 if ((num = is_cond_candidate (insn)) == COND_NO)
2703 return NULL;
2705 pat = PATTERN (insn);
2707 if (GET_CODE (insn) == INSN)
2709 dst = SET_DEST (pat);
2710 src = SET_SRC (pat);
2712 else
2714 dst = JUMP_LABEL (insn);
2715 src = NULL_RTX;
2718 switch (num)
2720 case COND_MOV_INSN:
2721 case COND_CLR_INSN:
2722 if (cond)
2723 c_insn = gen_movt0 (dst, src, dst);
2724 else
2725 c_insn = gen_movt0 (dst, dst, src);
2726 break;
2728 case COND_INC_INSN:
2729 if (cond)
2730 c_insn = gen_incscc (dst, dst);
2731 else
2732 c_insn = gen_incscc_false (dst, dst);
2733 break;
2735 case COND_DEC_INSN:
2736 if (cond)
2737 c_insn = gen_decscc (dst, dst);
2738 else
2739 c_insn = gen_decscc_false (dst, dst);
2740 break;
2742 case COND_BRANCH_INSN:
2743 if (cond)
2744 c_insn = gen_branch_true (dst);
2745 else
2746 c_insn = gen_branch_false (dst);
2747 break;
2749 default:
2750 return NULL;
2753 /* Only copy the notes if they exist. */
2754 if (rtx_length [GET_CODE (c_insn)] >= 7 && rtx_length [GET_CODE (insn)] >= 7)
2756 /* We really don't need to bother with the notes and links at this
2757 point, but go ahead and save the notes. This will help is_dead()
2758 when applying peepholes (links don't matter since they are not
2759 used any more beyond this point for the mcore). */
2760 REG_NOTES (c_insn) = REG_NOTES (insn);
2763 if (num == COND_BRANCH_INSN)
2765 /* For jumps, we need to be a little bit careful and emit the new jump
2766 before the old one and to update the use count for the target label.
2767 This way, the barrier following the old (uncond) jump will get
2768 deleted, but the label won't. */
2769 c_insn = emit_jump_insn_before (c_insn, insn);
2771 ++ LABEL_NUSES (dst);
2773 JUMP_LABEL (c_insn) = dst;
2775 else
2776 c_insn = emit_insn_after (c_insn, insn);
2778 delete_insn (insn);
2780 return c_insn;
2783 /* Attempt to change a basic block into a series of conditional insns. This
2784 works by taking the branch at the end of the 1st block and scanning for the
2785 end of the 2nd block. If all instructions in the 2nd block have cond.
2786 versions and the label at the start of block 3 is the same as the target
2787 from the branch at block 1, then conditionalize all insn in block 2 using
2788 the inverse condition of the branch at block 1. (Note I'm bending the
2789 definition of basic block here.)
2791 e.g., change:
2793 bt L2 <-- end of block 1 (delete)
2794 mov r7,r8
2795 addu r7,1
2796 br L3 <-- end of block 2
2798 L2: ... <-- start of block 3 (NUSES==1)
2799 L3: ...
2803 movf r7,r8
2804 incf r7
2805 bf L3
2807 L3: ...
2809 we can delete the L2 label if NUSES==1 and re-apply the optimization
2810 starting at the last instruction of block 2. This may allow an entire
2811 if-then-else statement to be conditionalized. BRC */
2812 static rtx
2813 conditionalize_block (first)
2814 rtx first;
2816 rtx insn;
2817 rtx br_pat;
2818 rtx end_blk_1_br = 0;
2819 rtx end_blk_2_insn = 0;
2820 rtx start_blk_3_lab = 0;
2821 int cond;
2822 int br_lab_num;
2823 int blk_size = 0;
2826 /* Check that the first insn is a candidate conditional jump. This is
2827 the one that we'll eliminate. If not, advance to the next insn to
2828 try. */
2829 if (GET_CODE (first) != JUMP_INSN ||
2830 GET_CODE (PATTERN (first)) != SET ||
2831 GET_CODE (XEXP (PATTERN (first), 1)) != IF_THEN_ELSE)
2832 return NEXT_INSN (first);
2834 /* Extract some information we need. */
2835 end_blk_1_br = first;
2836 br_pat = PATTERN (end_blk_1_br);
2838 /* Complement the condition since we use the reverse cond. for the insns. */
2839 cond = (GET_CODE (XEXP (XEXP (br_pat, 1), 0)) == EQ);
2841 /* Determine what kind of branch we have. */
2842 if (GET_CODE (XEXP (XEXP (br_pat, 1), 1)) == LABEL_REF)
2844 /* A normal branch, so extract label out of first arm. */
2845 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 1), 0));
2847 else
2849 /* An inverse branch, so extract the label out of the 2nd arm
2850 and complement the condition. */
2851 cond = (cond == 0);
2852 br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 2), 0));
2855 /* Scan forward for the start of block 2: it must start with a
2856 label and that label must be the same as the branch target
2857 label from block 1. We don't care about whether block 2 actually
2858 ends with a branch or a label (an uncond. branch is
2859 conditionalizable). */
2860 for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
2862 enum rtx_code code;
2864 code = GET_CODE (insn);
2866 /* Look for the label at the start of block 3. */
2867 if (code == CODE_LABEL && CODE_LABEL_NUMBER (insn) == br_lab_num)
2868 break;
2870 /* Skip barriers, notes, and conditionalizable insns. If the
2871 insn is not conditionalizable or makes this optimization fail,
2872 just return the next insn so we can start over from that point. */
2873 if (code != BARRIER && code != NOTE && !is_cond_candidate (insn))
2874 return NEXT_INSN (insn);
2876 /* Remember the last real insn before the label (ie end of block 2). */
2877 if (code == JUMP_INSN || code == INSN)
2879 blk_size ++;
2880 end_blk_2_insn = insn;
2884 if (!insn)
2885 return insn;
2887 /* It is possible for this optimization to slow performance if the blocks
2888 are long. This really depends upon whether the branch is likely taken
2889 or not. If the branch is taken, we slow performance in many cases. But,
2890 if the branch is not taken, we always help performance (for a single
2891 block, but for a double block (i.e. when the optimization is re-applied)
2892 this is not true since the 'right thing' depends on the overall length of
2893 the collapsed block). As a compromise, don't apply this optimization on
2894 blocks larger than size 2 (unlikely for the mcore) when speed is important.
2895 the best threshold depends on the latencies of the instructions (i.e.,
2896 the branch penalty). */
2897 if (optimize > 1 && blk_size > 2)
2898 return insn;
2900 /* At this point, we've found the start of block 3 and we know that
2901 it is the destination of the branch from block 1. Also, all
2902 instructions in the block 2 are conditionalizable. So, apply the
2903 conditionalization and delete the branch. */
2904 start_blk_3_lab = insn;
2906 for (insn = NEXT_INSN (end_blk_1_br); insn != start_blk_3_lab;
2907 insn = NEXT_INSN (insn))
2909 rtx newinsn;
2911 if (INSN_DELETED_P (insn))
2912 continue;
2914 /* Try to form a conditional variant of the instruction and emit it. */
2915 if ((newinsn = emit_new_cond_insn (insn, cond)))
2917 if (end_blk_2_insn == insn)
2918 end_blk_2_insn = newinsn;
2920 insn = newinsn;
2924 /* Note whether we will delete the label starting blk 3 when the jump
2925 gets deleted. If so, we want to re-apply this optimization at the
2926 last real instruction right before the label. */
2927 if (LABEL_NUSES (start_blk_3_lab) == 1)
2929 start_blk_3_lab = 0;
2932 /* ??? we probably should redistribute the death notes for this insn, esp.
2933 the death of cc, but it doesn't really matter this late in the game.
2934 The peepholes all use is_dead() which will find the correct death
2935 regardless of whether there is a note. */
2936 delete_insn (end_blk_1_br);
2938 if (! start_blk_3_lab)
2939 return end_blk_2_insn;
2941 /* Return the insn right after the label at the start of block 3. */
2942 return NEXT_INSN (start_blk_3_lab);
2945 /* Apply the conditionalization of blocks optimization. This is the
2946 outer loop that traverses through the insns scanning for a branch
2947 that signifies an opportunity to apply the optimization. Note that
2948 this optimization is applied late. If we could apply it earlier,
2949 say before cse 2, it may expose more optimization opportunities.
2950 but, the pay back probably isn't really worth the effort (we'd have
2951 to update all reg/flow/notes/links/etc to make it work - and stick it
2952 in before cse 2). */
2954 static void
2955 conditionalize_optimization (first)
2956 rtx first;
2958 rtx insn;
2960 for (insn = first; insn; insn = conditionalize_block (insn))
2961 continue;
2964 static int saved_warn_return_type = -1;
2965 static int saved_warn_return_type_count = 0;
2967 /* This function is called from toplev.c before reorg. */
2969 void
2970 mcore_dependent_reorg (first)
2971 rtx first;
2973 /* Reset this variable. */
2974 current_function_anonymous_args = 0;
2976 /* Restore the warn_return_type if it has been altered. */
2977 if (saved_warn_return_type != -1)
2979 /* Only restore the value if we have reached another function.
2980 The test of warn_return_type occurs in final_function () in
2981 c-decl.c a long time after the code for the function is generated,
2982 so we need a counter to tell us when we have finished parsing that
2983 function and can restore the flag. */
2984 if (--saved_warn_return_type_count == 0)
2986 warn_return_type = saved_warn_return_type;
2987 saved_warn_return_type = -1;
2991 if (optimize == 0)
2992 return;
2994 /* Conditionalize blocks where we can. */
2995 conditionalize_optimization (first);
2997 /* Literal pool generation is now pushed off until the assembler. */
3001 /* Return the reg_class to use when reloading the rtx X into the class
3002 CLASS. */
3004 /* If the input is (PLUS REG CONSTANT) representing a stack slot address,
3005 then we want to restrict the class to LRW_REGS since that ensures that
3006 will be able to safely load the constant.
3008 If the input is a constant that should be loaded with mvir1, then use
3009 ONLYR1_REGS.
3011 ??? We don't handle the case where we have (PLUS REG CONSTANT) and
3012 the constant should be loaded with mvir1, because that can lead to cases
3013 where an instruction needs two ONLYR1_REGS reloads. */
3014 enum reg_class
3015 mcore_reload_class (x, class)
3016 rtx x;
3017 enum reg_class class;
3019 enum reg_class new_class;
3021 if (class == GENERAL_REGS && CONSTANT_P (x)
3022 && (GET_CODE (x) != CONST_INT
3023 || ( ! CONST_OK_FOR_I (INTVAL (x))
3024 && ! CONST_OK_FOR_M (INTVAL (x))
3025 && ! CONST_OK_FOR_N (INTVAL (x)))))
3026 new_class = LRW_REGS;
3027 else
3028 new_class = class;
3030 return new_class;
3033 /* Tell me if a pair of reg/subreg rtx's actually refer to the same
3034 register. Note that the current version doesn't worry about whether
3035 they are the same mode or note (e.g., a QImode in r2 matches an HImode
3036 in r2 matches an SImode in r2. Might think in the future about whether
3037 we want to be able to say something about modes. */
3039 mcore_is_same_reg (x, y)
3040 rtx x;
3041 rtx y;
3043 /* Strip any and all of the subreg wrappers. */
3044 while (GET_CODE (x) == SUBREG)
3045 x = SUBREG_REG (x);
3047 while (GET_CODE (y) == SUBREG)
3048 y = SUBREG_REG (y);
3050 if (GET_CODE(x) == REG && GET_CODE(y) == REG && REGNO(x) == REGNO(y))
3051 return 1;
3053 return 0;
3056 /* Called to register all of our global variables with the garbage
3057 collector. */
3058 static void
3059 mcore_add_gc_roots ()
3061 ggc_add_rtx_root (&arch_compare_op0, 1);
3062 ggc_add_rtx_root (&arch_compare_op1, 1);
3065 void
3066 mcore_override_options ()
3068 if (mcore_stack_increment_string)
3070 mcore_stack_increment = atoi (mcore_stack_increment_string);
3072 if (mcore_stack_increment < 0
3073 || (mcore_stack_increment == 0
3074 && (mcore_stack_increment_string[0] != '0'
3075 || mcore_stack_increment_string[1] != 0)))
3076 error ("invalid option `-mstack-increment=%s'",
3077 mcore_stack_increment_string);
3080 /* Only the m340 supports little endian code. */
3081 if (TARGET_LITTLE_END && ! TARGET_M340)
3082 target_flags |= M340_BIT;
3084 mcore_add_gc_roots ();
3088 mcore_must_pass_on_stack (mode, type)
3089 enum machine_mode mode ATTRIBUTE_UNUSED;
3090 tree type;
3092 if (type == NULL)
3093 return 0;
3095 /* If the argugment can have its address taken, it must
3096 be placed on the stack. */
3097 if (TREE_ADDRESSABLE (type))
3098 return 1;
3100 return 0;
3103 /* Compute the number of word sized registers needed to
3104 hold a function argument of mode MODE and type TYPE. */
3106 mcore_num_arg_regs (mode, type)
3107 enum machine_mode mode;
3108 tree type;
3110 int size;
3112 if (MUST_PASS_IN_STACK (mode, type))
3113 return 0;
3115 if (type && mode == BLKmode)
3116 size = int_size_in_bytes (type);
3117 else
3118 size = GET_MODE_SIZE (mode);
3120 return ROUND_ADVANCE (size);
3123 static rtx
3124 handle_structs_in_regs (mode, type, reg)
3125 enum machine_mode mode;
3126 tree type;
3127 int reg;
3129 int size;
3131 /* The MCore ABI defines that a structure whoes size is not a whole multiple
3132 of bytes is passed packed into registers (or spilled onto the stack if
3133 not enough registers are available) with the last few bytes of the
3134 structure being packed, left-justified, into the last register/stack slot.
3135 GCC handles this correctly if the last word is in a stack slot, but we
3136 have to generate a special, PARALLEL RTX if the last word is in an
3137 argument register. */
3138 if (type
3139 && TYPE_MODE (type) == BLKmode
3140 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3141 && (size = int_size_in_bytes (type)) > UNITS_PER_WORD
3142 && (size % UNITS_PER_WORD != 0)
3143 && (reg + mcore_num_arg_regs (mode, type) <= (FIRST_PARM_REG + NPARM_REGS)))
3145 rtx arg_regs [NPARM_REGS];
3146 int nregs;
3147 rtx result;
3148 rtvec rtvec;
3150 for (nregs = 0; size > 0; size -= UNITS_PER_WORD)
3152 arg_regs [nregs] =
3153 gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, reg ++),
3154 GEN_INT (nregs * UNITS_PER_WORD));
3155 nregs ++;
3158 /* We assume here that NPARM_REGS == 6. The assert checks this. */
3159 assert (ARRAY_SIZE (arg_regs) == 6);
3160 rtvec = gen_rtvec (nregs, arg_regs[0], arg_regs[1], arg_regs[2],
3161 arg_regs[3], arg_regs[4], arg_regs[5]);
3163 result = gen_rtx_PARALLEL (mode, rtvec);
3164 return result;
3167 return gen_rtx_REG (mode, reg);
3171 mcore_function_value (valtype, func)
3172 tree valtype;
3173 tree func ATTRIBUTE_UNUSED;
3175 enum machine_mode mode;
3176 int unsigned_p;
3178 mode = TYPE_MODE (valtype);
3180 PROMOTE_MODE (mode, unsigned_p, NULL);
3182 return handle_structs_in_regs (mode, valtype, FIRST_RET_REG);
3185 /* Define where to put the arguments to a function.
3186 Value is zero to push the argument on the stack,
3187 or a hard register in which to store the argument.
3189 MODE is the argument's machine mode.
3190 TYPE is the data type of the argument (as a tree).
3191 This is null for libcalls where that information may
3192 not be available.
3193 CUM is a variable of type CUMULATIVE_ARGS which gives info about
3194 the preceding args and about the function being called.
3195 NAMED is nonzero if this argument is a named parameter
3196 (otherwise it is an extra parameter matching an ellipsis).
3198 On MCore the first args are normally in registers
3199 and the rest are pushed. Any arg that starts within the first
3200 NPARM_REGS words is at least partially passed in a register unless
3201 its data type forbids. */
3203 mcore_function_arg (cum, mode, type, named)
3204 CUMULATIVE_ARGS cum;
3205 enum machine_mode mode;
3206 tree type;
3207 int named;
3209 int arg_reg;
3211 if (! named)
3212 return 0;
3214 if (MUST_PASS_IN_STACK (mode, type))
3215 return 0;
3217 arg_reg = ROUND_REG (cum, mode);
3219 if (arg_reg < NPARM_REGS)
3220 return handle_structs_in_regs (mode, type, FIRST_PARM_REG + arg_reg);
3222 return 0;
3225 /* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.
3226 Returns the number of argument registers required to hold *part* of
3227 a parameter of machine mode MODE and type TYPE (which may be NULL if
3228 the type is not known). If the argument fits entirly in the argument
3229 registers, or entirely on the stack, then 0 is returned. CUM is the
3230 number of argument registers already used by earlier parameters to
3231 the function. */
3233 mcore_function_arg_partial_nregs (cum, mode, type, named)
3234 CUMULATIVE_ARGS cum;
3235 enum machine_mode mode;
3236 tree type;
3237 int named;
3239 int reg = ROUND_REG (cum, mode);
3241 if (named == 0)
3242 return 0;
3244 if (MUST_PASS_IN_STACK (mode, type))
3245 return 0;
3247 /* REG is not the *hardware* register number of the register that holds
3248 the argument, it is the *argument* register number. So for example,
3249 the first argument to a function goes in argument register 0, which
3250 translates (for the MCore) into hardware register 2. The second
3251 argument goes into argument register 1, which translates into hardware
3252 register 3, and so on. NPARM_REGS is the number of argument registers
3253 supported by the target, not the maximum hardware register number of
3254 the target. */
3255 if (reg >= NPARM_REGS)
3256 return 0;
3258 /* If the argument fits entirely in registers, return 0. */
3259 if (reg + mcore_num_arg_regs (mode, type) <= NPARM_REGS)
3260 return 0;
3262 /* The argument overflows the number of available argument registers.
3263 Compute how many argument registers have not yet been assigned to
3264 hold an argument. */
3265 reg = NPARM_REGS - reg;
3267 /* Return partially in registers and partially on the stack. */
3268 return reg;
3271 /* Return non-zero if SYMBOL is marked as being dllexport'd. */
3273 mcore_dllexport_name_p (symbol)
3274 const char * symbol;
3276 return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
3279 /* Return non-zero if SYMBOL is marked as being dllimport'd. */
3281 mcore_dllimport_name_p (symbol)
3282 const char * symbol;
3284 return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
3287 /* Mark a DECL as being dllexport'd. */
3288 static void
3289 mcore_mark_dllexport (decl)
3290 tree decl;
3292 const char * oldname;
3293 char * newname;
3294 rtx rtlname;
3295 tree idp;
3297 rtlname = XEXP (DECL_RTL (decl), 0);
3299 if (GET_CODE (rtlname) == SYMBOL_REF)
3300 oldname = XSTR (rtlname, 0);
3301 else if ( GET_CODE (rtlname) == MEM
3302 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3303 oldname = XSTR (XEXP (rtlname, 0), 0);
3304 else
3305 abort ();
3307 if (mcore_dllexport_name_p (oldname))
3308 return; /* Already done. */
3310 newname = alloca (strlen (oldname) + 4);
3311 sprintf (newname, "@e.%s", oldname);
3313 /* We pass newname through get_identifier to ensure it has a unique
3314 address. RTL processing can sometimes peek inside the symbol ref
3315 and compare the string's addresses to see if two symbols are
3316 identical. */
3317 /* ??? At least I think that's why we do this. */
3318 idp = get_identifier (newname);
3320 XEXP (DECL_RTL (decl), 0) =
3321 gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
3324 /* Mark a DECL as being dllimport'd. */
3325 static void
3326 mcore_mark_dllimport (decl)
3327 tree decl;
3329 const char * oldname;
3330 char * newname;
3331 tree idp;
3332 rtx rtlname;
3333 rtx newrtl;
3335 rtlname = XEXP (DECL_RTL (decl), 0);
3337 if (GET_CODE (rtlname) == SYMBOL_REF)
3338 oldname = XSTR (rtlname, 0);
3339 else if ( GET_CODE (rtlname) == MEM
3340 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3341 oldname = XSTR (XEXP (rtlname, 0), 0);
3342 else
3343 abort ();
3345 if (mcore_dllexport_name_p (oldname))
3346 abort (); /* This shouldn't happen. */
3347 else if (mcore_dllimport_name_p (oldname))
3348 return; /* Already done. */
3350 /* ??? One can well ask why we're making these checks here,
3351 and that would be a good question. */
3353 /* Imported variables can't be initialized. */
3354 if (TREE_CODE (decl) == VAR_DECL
3355 && !DECL_VIRTUAL_P (decl)
3356 && DECL_INITIAL (decl))
3358 error_with_decl (decl, "initialized variable `%s' is marked dllimport");
3359 return;
3362 /* `extern' needn't be specified with dllimport.
3363 Specify `extern' now and hope for the best. Sigh. */
3364 if (TREE_CODE (decl) == VAR_DECL
3365 /* ??? Is this test for vtables needed? */
3366 && !DECL_VIRTUAL_P (decl))
3368 DECL_EXTERNAL (decl) = 1;
3369 TREE_PUBLIC (decl) = 1;
3372 newname = alloca (strlen (oldname) + 11);
3373 sprintf (newname, "@i.__imp_%s", oldname);
3375 /* We pass newname through get_identifier to ensure it has a unique
3376 address. RTL processing can sometimes peek inside the symbol ref
3377 and compare the string's addresses to see if two symbols are
3378 identical. */
3379 /* ??? At least I think that's why we do this. */
3380 idp = get_identifier (newname);
3382 newrtl = gen_rtx (MEM, Pmode,
3383 gen_rtx (SYMBOL_REF, Pmode,
3384 IDENTIFIER_POINTER (idp)));
3385 XEXP (DECL_RTL (decl), 0) = newrtl;
3388 static int
3389 mcore_dllexport_p (decl)
3390 tree decl;
3392 if ( TREE_CODE (decl) != VAR_DECL
3393 && TREE_CODE (decl) != FUNCTION_DECL)
3394 return 0;
3396 return lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) != 0;
3399 static int
3400 mcore_dllimport_p (decl)
3401 tree decl;
3403 if ( TREE_CODE (decl) != VAR_DECL
3404 && TREE_CODE (decl) != FUNCTION_DECL)
3405 return 0;
3407 return lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) != 0;
3410 /* Cover function to implement ENCODE_SECTION_INFO. */
3411 void
3412 mcore_encode_section_info (decl)
3413 tree decl;
3415 /* This bit is copied from arm.h. */
3416 if (optimize > 0
3417 && TREE_CONSTANT (decl)
3418 && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
3420 rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
3421 ? TREE_CST_RTL (decl) : DECL_RTL (decl));
3422 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
3425 /* Mark the decl so we can tell from the rtl whether the object is
3426 dllexport'd or dllimport'd. */
3427 if (mcore_dllexport_p (decl))
3428 mcore_mark_dllexport (decl);
3429 else if (mcore_dllimport_p (decl))
3430 mcore_mark_dllimport (decl);
3432 /* It might be that DECL has already been marked as dllimport, but
3433 a subsequent definition nullified that. The attribute is gone
3434 but DECL_RTL still has @i.__imp_foo. We need to remove that. */
3435 else if ((TREE_CODE (decl) == FUNCTION_DECL
3436 || TREE_CODE (decl) == VAR_DECL)
3437 && DECL_RTL (decl) != NULL_RTX
3438 && GET_CODE (DECL_RTL (decl)) == MEM
3439 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
3440 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
3441 && mcore_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
3443 const char * oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
3444 tree idp = get_identifier (oldname + 9);
3445 rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
3447 XEXP (DECL_RTL (decl), 0) = newrtl;
3449 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
3450 ??? We leave these alone for now. */
3454 /* MCore specific attribute support.
3455 dllexport - for exporting a function/variable that will live in a dll
3456 dllimport - for importing a function/variable from a dll
3457 naked - do not create a function prologue/epilogue. */
3459 const struct attribute_spec mcore_attribute_table[] =
3461 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3462 { "dllexport", 0, 0, true, false, false, NULL },
3463 { "dllimport", 0, 0, true, false, false, NULL },
3464 { "naked", 0, 0, true, false, false, mcore_handle_naked_attribute },
3465 { NULL, 0, 0, false, false, false, NULL }
3468 /* Handle a "naked" attribute; arguments as in
3469 struct attribute_spec.handler. */
3470 static tree
3471 mcore_handle_naked_attribute (node, name, args, flags, no_add_attrs)
3472 tree *node;
3473 tree name;
3474 tree args ATTRIBUTE_UNUSED;
3475 int flags ATTRIBUTE_UNUSED;
3476 bool *no_add_attrs;
3478 if (TREE_CODE (*node) == FUNCTION_DECL)
3480 /* PR14310 - don't complain about lack of return statement
3481 in naked functions. The solution here is a gross hack
3482 but this is the only way to solve the problem without
3483 adding a new feature to GCC. I did try submitting a patch
3484 that would add such a new feature, but it was (rightfully)
3485 rejected on the grounds that it was creeping featurism,
3486 so hence this code. */
3487 if (warn_return_type)
3489 saved_warn_return_type = warn_return_type;
3490 warn_return_type = 0;
3491 saved_warn_return_type_count = 2;
3493 else if (saved_warn_return_type_count)
3494 saved_warn_return_type_count = 2;
3496 else
3498 warning ("`%s' attribute only applies to functions",
3499 IDENTIFIER_POINTER (name));
3500 *no_add_attrs = true;
3503 return NULL_TREE;
3506 /* Cover function for UNIQUE_SECTION. */
3508 void
3509 mcore_unique_section (decl, reloc)
3510 tree decl;
3511 int reloc ATTRIBUTE_UNUSED;
3513 int len;
3514 char * name;
3515 char * string;
3516 const char * prefix;
3518 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3520 /* Strip off any encoding in name. */
3521 STRIP_NAME_ENCODING (name, name);
3523 /* The object is put in, for example, section .text$foo.
3524 The linker will then ultimately place them in .text
3525 (everything from the $ on is stripped). */
3526 if (TREE_CODE (decl) == FUNCTION_DECL)
3527 prefix = ".text$";
3528 /* For compatibility with EPOC, we ignore the fact that the
3529 section might have relocs against it. */
3530 else if (DECL_READONLY_SECTION (decl, 0))
3531 prefix = ".rdata$";
3532 else
3533 prefix = ".data$";
3535 len = strlen (name) + strlen (prefix);
3536 string = alloca (len + 1);
3538 sprintf (string, "%s%s", prefix, name);
3540 DECL_SECTION_NAME (decl) = build_string (len, string);
3544 mcore_naked_function_p ()
3546 return lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE;
3549 #ifdef OBJECT_FORMAT_ELF
3550 static void
3551 mcore_asm_named_section (name, flags)
3552 const char *name;
3553 unsigned int flags ATTRIBUTE_UNUSED;
3555 fprintf (asm_out_file, "\t.section %s\n", name);
3557 #endif /* OBJECT_FORMAT_ELF */