]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/v850/v850.c
Merge basic-improvements-branch to trunk
[thirdparty/gcc.git] / gcc / config / v850 / v850.c
1 /* Subroutines for insn-output.c for NEC V850 series
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Contributed by Jeff Law (law@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "toplev.h"
41 #include "ggc.h"
42 #include "integrate.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "target-def.h"
46
47 #ifndef streq
48 #define streq(a,b) (strcmp (a, b) == 0)
49 #endif
50
51 /* Function prototypes for stupid compilers: */
52 static void const_double_split PARAMS ((rtx, HOST_WIDE_INT *, HOST_WIDE_INT *));
53 static int const_costs_int PARAMS ((HOST_WIDE_INT, int));
54 static void substitute_ep_register PARAMS ((rtx, rtx, int, int, rtx *, rtx *));
55 static int ep_memory_offset PARAMS ((enum machine_mode, int));
56 static void v850_set_data_area PARAMS ((tree, v850_data_area));
57 const struct attribute_spec v850_attribute_table[];
58 static tree v850_handle_interrupt_attribute PARAMS ((tree *, tree, tree, int, bool *));
59 static tree v850_handle_data_area_attribute PARAMS ((tree *, tree, tree, int, bool *));
60 static void v850_insert_attributes PARAMS ((tree, tree *));
61 static void v850_select_section PARAMS ((tree, int, unsigned HOST_WIDE_INT));
62 static void v850_encode_data_area PARAMS ((tree));
63 static void v850_encode_section_info PARAMS ((tree, int));
64 static const char *v850_strip_name_encoding PARAMS ((const char *));
65
66 /* Information about the various small memory areas. */
67 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
68 {
69 /* name value max physical max */
70 { "tda", (char *)0, 0, 256 },
71 { "sda", (char *)0, 0, 65536 },
72 { "zda", (char *)0, 0, 32768 },
73 };
74
75 /* Names of the various data areas used on the v850. */
76 tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
77 tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
78
79 /* Track the current data area set by the data area pragma (which
80 can be nested). Tested by check_default_data_area. */
81 data_area_stack_element * data_area_stack = NULL;
82
83 /* True if we don't need to check any more if the current
84 function is an interrupt handler. */
85 static int v850_interrupt_cache_p = FALSE;
86
87 /* Whether current function is an interrupt handler. */
88 static int v850_interrupt_p = FALSE;
89 \f
90 /* Initialize the GCC target structure. */
91 #undef TARGET_ASM_ALIGNED_HI_OP
92 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
93
94 #undef TARGET_ATTRIBUTE_TABLE
95 #define TARGET_ATTRIBUTE_TABLE v850_attribute_table
96
97 #undef TARGET_INSERT_ATTRIBUTES
98 #define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
99
100 #undef TARGET_ASM_SELECT_SECTION
101 #define TARGET_ASM_SELECT_SECTION v850_select_section
102
103 #undef TARGET_ENCODE_SECTION_INFO
104 #define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
105 #undef TARGET_STRIP_NAME_ENCODING
106 #define TARGET_STRIP_NAME_ENCODING v850_strip_name_encoding
107
108 struct gcc_target targetm = TARGET_INITIALIZER;
109 \f
110 /* Sometimes certain combinations of command options do not make
111 sense on a particular target machine. You can define a macro
112 `OVERRIDE_OPTIONS' to take account of this. This macro, if
113 defined, is executed once just after all the command options have
114 been parsed.
115
116 Don't use this macro to turn on various extra optimizations for
117 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
118
119 void
120 override_options ()
121 {
122 int i;
123 extern int atoi PARAMS ((const char *));
124
125 /* Parse -m{s,t,z}da=nnn switches */
126 for (i = 0; i < (int)SMALL_MEMORY_max; i++)
127 {
128 if (small_memory[i].value)
129 {
130 if (!ISDIGIT (*small_memory[i].value))
131 error ("%s=%s is not numeric",
132 small_memory[i].name,
133 small_memory[i].value);
134 else
135 {
136 small_memory[i].max = atoi (small_memory[i].value);
137 if (small_memory[i].max > small_memory[i].physical_max)
138 error ("%s=%s is too large",
139 small_memory[i].name,
140 small_memory[i].value);
141 }
142 }
143 }
144
145 /* Make sure that the US_BIT_SET mask has been correctly initialized. */
146 if ((target_flags & MASK_US_MASK_SET) == 0)
147 {
148 target_flags |= MASK_US_MASK_SET;
149 target_flags &= ~MASK_US_BIT_SET;
150 }
151 }
152
153 \f
154 /* Output assembly code for the start of the file. */
155
156 void
157 asm_file_start (file)
158 FILE *file;
159 {
160 output_file_directive (file, main_input_filename);
161 }
162 \f
163
164 /* Return an RTX to represent where a value with mode MODE will be returned
165 from a function. If the result is 0, the argument is pushed. */
166
167 rtx
168 function_arg (cum, mode, type, named)
169 CUMULATIVE_ARGS *cum;
170 enum machine_mode mode;
171 tree type;
172 int named;
173 {
174 rtx result = 0;
175 int size, align;
176
177 if (TARGET_GHS && !named)
178 return NULL_RTX;
179
180 if (mode == BLKmode)
181 size = int_size_in_bytes (type);
182 else
183 size = GET_MODE_SIZE (mode);
184
185 if (size < 1)
186 return 0;
187
188 if (type)
189 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
190 else
191 align = size;
192
193 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
194
195 if (cum->nbytes > 4 * UNITS_PER_WORD)
196 return 0;
197
198 if (type == NULL_TREE
199 && cum->nbytes + size > 4 * UNITS_PER_WORD)
200 return 0;
201
202 switch (cum->nbytes / UNITS_PER_WORD)
203 {
204 case 0:
205 result = gen_rtx_REG (mode, 6);
206 break;
207 case 1:
208 result = gen_rtx_REG (mode, 7);
209 break;
210 case 2:
211 result = gen_rtx_REG (mode, 8);
212 break;
213 case 3:
214 result = gen_rtx_REG (mode, 9);
215 break;
216 default:
217 result = 0;
218 }
219
220 return result;
221 }
222
223 \f
224 /* Return the number of words which must be put into registers
225 for values which are part in registers and part in memory. */
226
227 int
228 function_arg_partial_nregs (cum, mode, type, named)
229 CUMULATIVE_ARGS *cum;
230 enum machine_mode mode;
231 tree type;
232 int named;
233 {
234 int size, align;
235
236 if (TARGET_GHS && !named)
237 return 0;
238
239 if (mode == BLKmode)
240 size = int_size_in_bytes (type);
241 else
242 size = GET_MODE_SIZE (mode);
243
244 if (type)
245 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
246 else
247 align = size;
248
249 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
250
251 if (cum->nbytes > 4 * UNITS_PER_WORD)
252 return 0;
253
254 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
255 return 0;
256
257 if (type == NULL_TREE
258 && cum->nbytes + size > 4 * UNITS_PER_WORD)
259 return 0;
260
261 return (4 * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
262 }
263
264 \f
265 /* Return the high and low words of a CONST_DOUBLE */
266
267 static void
268 const_double_split (x, p_high, p_low)
269 rtx x;
270 HOST_WIDE_INT *p_high;
271 HOST_WIDE_INT *p_low;
272 {
273 if (GET_CODE (x) == CONST_DOUBLE)
274 {
275 long t[2];
276 REAL_VALUE_TYPE rv;
277
278 switch (GET_MODE (x))
279 {
280 case DFmode:
281 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
282 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
283 *p_high = t[1]; /* since v850 is little endian */
284 *p_low = t[0]; /* high is second word */
285 return;
286
287 case SFmode:
288 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
289 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
290 *p_low = 0;
291 return;
292
293 case VOIDmode:
294 case DImode:
295 *p_high = CONST_DOUBLE_HIGH (x);
296 *p_low = CONST_DOUBLE_LOW (x);
297 return;
298
299 default:
300 break;
301 }
302 }
303
304 fatal_insn ("const_double_split got a bad insn:", x);
305 }
306
307 \f
308 /* Return the cost of the rtx R with code CODE. */
309
310 static int
311 const_costs_int (value, zero_cost)
312 HOST_WIDE_INT value;
313 int zero_cost;
314 {
315 if (CONST_OK_FOR_I (value))
316 return zero_cost;
317 else if (CONST_OK_FOR_J (value))
318 return 1;
319 else if (CONST_OK_FOR_K (value))
320 return 2;
321 else
322 return 4;
323 }
324
325 int
326 const_costs (r, c)
327 rtx r;
328 enum rtx_code c;
329 {
330 HOST_WIDE_INT high, low;
331
332 switch (c)
333 {
334 case CONST_INT:
335 return const_costs_int (INTVAL (r), 0);
336
337 case CONST_DOUBLE:
338 const_double_split (r, &high, &low);
339 if (GET_MODE (r) == SFmode)
340 return const_costs_int (high, 1);
341 else
342 return const_costs_int (high, 1) + const_costs_int (low, 1);
343
344 case SYMBOL_REF:
345 case LABEL_REF:
346 case CONST:
347 return 2;
348
349 case HIGH:
350 return 1;
351
352 default:
353 return 4;
354 }
355 }
356
357 \f
358 /* Print operand X using operand code CODE to assembly language output file
359 FILE. */
360
361 void
362 print_operand (file, x, code)
363 FILE *file;
364 rtx x;
365 int code;
366 {
367 HOST_WIDE_INT high, low;
368
369 switch (code)
370 {
371 case 'c':
372 /* We use 'c' operands with symbols for .vtinherit */
373 if (GET_CODE (x) == SYMBOL_REF)
374 {
375 output_addr_const(file, x);
376 break;
377 }
378 /* fall through */
379 case 'b':
380 case 'B':
381 case 'C':
382 switch ((code == 'B' || code == 'C')
383 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
384 {
385 case NE:
386 if (code == 'c' || code == 'C')
387 fprintf (file, "nz");
388 else
389 fprintf (file, "ne");
390 break;
391 case EQ:
392 if (code == 'c' || code == 'C')
393 fprintf (file, "z");
394 else
395 fprintf (file, "e");
396 break;
397 case GE:
398 fprintf (file, "ge");
399 break;
400 case GT:
401 fprintf (file, "gt");
402 break;
403 case LE:
404 fprintf (file, "le");
405 break;
406 case LT:
407 fprintf (file, "lt");
408 break;
409 case GEU:
410 fprintf (file, "nl");
411 break;
412 case GTU:
413 fprintf (file, "h");
414 break;
415 case LEU:
416 fprintf (file, "nh");
417 break;
418 case LTU:
419 fprintf (file, "l");
420 break;
421 default:
422 abort ();
423 }
424 break;
425 case 'F': /* high word of CONST_DOUBLE */
426 if (GET_CODE (x) == CONST_INT)
427 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
428 else if (GET_CODE (x) == CONST_DOUBLE)
429 {
430 const_double_split (x, &high, &low);
431 fprintf (file, "%ld", (long) high);
432 }
433 else
434 abort ();
435 break;
436 case 'G': /* low word of CONST_DOUBLE */
437 if (GET_CODE (x) == CONST_INT)
438 fprintf (file, "%ld", (long) INTVAL (x));
439 else if (GET_CODE (x) == CONST_DOUBLE)
440 {
441 const_double_split (x, &high, &low);
442 fprintf (file, "%ld", (long) low);
443 }
444 else
445 abort ();
446 break;
447 case 'L':
448 fprintf (file, "%d\n", INTVAL (x) & 0xffff);
449 break;
450 case 'M':
451 fprintf (file, "%d", exact_log2 (INTVAL (x)));
452 break;
453 case 'O':
454 if (special_symbolref_operand (x, VOIDmode))
455 {
456 const char *name;
457
458 if (GET_CODE (x) == SYMBOL_REF)
459 name = XSTR (x, 0);
460 else if (GET_CODE (x) == CONST)
461 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
462 else
463 abort ();
464
465 if (ZDA_NAME_P (name))
466 fprintf (file, "zdaoff");
467 else if (SDA_NAME_P (name))
468 fprintf (file, "sdaoff");
469 else if (TDA_NAME_P (name))
470 fprintf (file, "tdaoff");
471 else
472 abort ();
473 }
474 else
475 abort ();
476 break;
477 case 'P':
478 if (special_symbolref_operand (x, VOIDmode))
479 output_addr_const (file, x);
480 else
481 abort ();
482 break;
483 case 'Q':
484 if (special_symbolref_operand (x, VOIDmode))
485 {
486 const char *name;
487
488 if (GET_CODE (x) == SYMBOL_REF)
489 name = XSTR (x, 0);
490 else if (GET_CODE (x) == CONST)
491 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
492 else
493 abort ();
494
495 if (ZDA_NAME_P (name))
496 fprintf (file, "r0");
497 else if (SDA_NAME_P (name))
498 fprintf (file, "gp");
499 else if (TDA_NAME_P (name))
500 fprintf (file, "ep");
501 else
502 abort ();
503 }
504 else
505 abort ();
506 break;
507 case 'R': /* 2nd word of a double. */
508 switch (GET_CODE (x))
509 {
510 case REG:
511 fprintf (file, reg_names[REGNO (x) + 1]);
512 break;
513 case MEM:
514 x = XEXP (adjust_address (x, SImode, 4), 0);
515 print_operand_address (file, x);
516 if (GET_CODE (x) == CONST_INT)
517 fprintf (file, "[r0]");
518 break;
519
520 default:
521 break;
522 }
523 break;
524 case 'S':
525 {
526 /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
527 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
528 fputs ("s", file);
529
530 break;
531 }
532 case 'T':
533 {
534 /* Like an 'S' operand above, but for unsigned loads only. */
535 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
536 fputs ("s", file);
537
538 break;
539 }
540 case 'W': /* print the instruction suffix */
541 switch (GET_MODE (x))
542 {
543 default:
544 abort ();
545
546 case QImode: fputs (".b", file); break;
547 case HImode: fputs (".h", file); break;
548 case SImode: fputs (".w", file); break;
549 case SFmode: fputs (".w", file); break;
550 }
551 break;
552 case '.': /* register r0 */
553 fputs (reg_names[0], file);
554 break;
555 case 'z': /* reg or zero */
556 if (x == const0_rtx)
557 fputs (reg_names[0], file);
558 else if (GET_CODE (x) == REG)
559 fputs (reg_names[REGNO (x)], file);
560 else
561 abort ();
562 break;
563 default:
564 switch (GET_CODE (x))
565 {
566 case MEM:
567 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
568 output_address (gen_rtx_PLUS (SImode, gen_rtx (REG, SImode, 0),
569 XEXP (x, 0)));
570 else
571 output_address (XEXP (x, 0));
572 break;
573
574 case REG:
575 fputs (reg_names[REGNO (x)], file);
576 break;
577 case SUBREG:
578 fputs (reg_names[subreg_regno (x)], file);
579 break;
580 case CONST_INT:
581 case SYMBOL_REF:
582 case CONST:
583 case LABEL_REF:
584 case CODE_LABEL:
585 print_operand_address (file, x);
586 break;
587 default:
588 abort ();
589 }
590 break;
591
592 }
593 }
594
595 \f
596 /* Output assembly language output for the address ADDR to FILE. */
597
598 void
599 print_operand_address (file, addr)
600 FILE *file;
601 rtx addr;
602 {
603 switch (GET_CODE (addr))
604 {
605 case REG:
606 fprintf (file, "0[");
607 print_operand (file, addr, 0);
608 fprintf (file, "]");
609 break;
610 case LO_SUM:
611 if (GET_CODE (XEXP (addr, 0)) == REG)
612 {
613 /* reg,foo */
614 fprintf (file, "lo(");
615 print_operand (file, XEXP (addr, 1), 0);
616 fprintf (file, ")[");
617 print_operand (file, XEXP (addr, 0), 0);
618 fprintf (file, "]");
619 }
620 break;
621 case PLUS:
622 if (GET_CODE (XEXP (addr, 0)) == REG
623 || GET_CODE (XEXP (addr, 0)) == SUBREG)
624 {
625 /* reg,foo */
626 print_operand (file, XEXP (addr, 1), 0);
627 fprintf (file, "[");
628 print_operand (file, XEXP (addr, 0), 0);
629 fprintf (file, "]");
630 }
631 else
632 {
633 print_operand (file, XEXP (addr, 0), 0);
634 fprintf (file, "+");
635 print_operand (file, XEXP (addr, 1), 0);
636 }
637 break;
638 case SYMBOL_REF:
639 if (ENCODED_NAME_P (XSTR (addr, 0)))
640 {
641 const char *name = XSTR (addr, 0);
642 const char *off_name;
643 const char *reg_name;
644
645 if (ZDA_NAME_P (name))
646 {
647 off_name = "zdaoff";
648 reg_name = "r0";
649 }
650 else if (SDA_NAME_P (name))
651 {
652 off_name = "sdaoff";
653 reg_name = "gp";
654 }
655 else if (TDA_NAME_P (name))
656 {
657 off_name = "tdaoff";
658 reg_name = "ep";
659 }
660 else
661 abort ();
662
663 fprintf (file, "%s(", off_name);
664 output_addr_const (file, addr);
665 fprintf (file, ")[%s]", reg_name);
666 }
667 else
668 output_addr_const (file, addr);
669 break;
670 case CONST:
671 if (special_symbolref_operand (addr, VOIDmode))
672 {
673 const char *name = XSTR (XEXP (XEXP (addr, 0), 0), 0);
674 const char *off_name;
675 const char *reg_name;
676
677 if (ZDA_NAME_P (name))
678 {
679 off_name = "zdaoff";
680 reg_name = "r0";
681 }
682 else if (SDA_NAME_P (name))
683 {
684 off_name = "sdaoff";
685 reg_name = "gp";
686 }
687 else if (TDA_NAME_P (name))
688 {
689 off_name = "tdaoff";
690 reg_name = "ep";
691 }
692 else
693 abort ();
694
695 fprintf (file, "%s(", off_name);
696 output_addr_const (file, addr);
697 fprintf (file, ")[%s]", reg_name);
698 }
699 else
700 output_addr_const (file, addr);
701 break;
702 default:
703 output_addr_const (file, addr);
704 break;
705 }
706 }
707
708 /* When assemble_integer is used to emit the offsets for a switch
709 table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
710 output_addr_const will normally barf at this, but it is OK to omit
711 the truncate and just emit the difference of the two labels. The
712 .hword directive will automatically handle the truncation for us.
713
714 Returns 1 if rtx was handled, 0 otherwise. */
715
716 int
717 v850_output_addr_const_extra (file, x)
718 FILE * file;
719 rtx x;
720 {
721 if (GET_CODE (x) != TRUNCATE)
722 return 0;
723
724 x = XEXP (x, 0);
725
726 /* We must also handle the case where the switch table was passed a
727 constant value and so has been collapsed. In this case the first
728 label will have been deleted. In such a case it is OK to emit
729 nothing, since the table will not be used.
730 (cf gcc.c-torture/compile/990801-1.c). */
731 if (GET_CODE (x) == MINUS
732 && GET_CODE (XEXP (x, 0)) == LABEL_REF
733 && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
734 && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
735 return 1;
736
737 output_addr_const (file, x);
738 return 1;
739 }
740 \f
741 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
742 point value. */
743
744 const char *
745 output_move_single (operands)
746 rtx *operands;
747 {
748 rtx dst = operands[0];
749 rtx src = operands[1];
750
751 if (REG_P (dst))
752 {
753 if (REG_P (src))
754 return "mov %1,%0";
755
756 else if (GET_CODE (src) == CONST_INT)
757 {
758 HOST_WIDE_INT value = INTVAL (src);
759
760 if (CONST_OK_FOR_J (value)) /* Signed 5 bit immediate. */
761 return "mov %1,%0";
762
763 else if (CONST_OK_FOR_K (value)) /* Signed 16 bit immediate. */
764 return "movea lo(%1),%.,%0";
765
766 else if (CONST_OK_FOR_L (value)) /* Upper 16 bits were set. */
767 return "movhi hi(%1),%.,%0";
768
769 /* A random constant. */
770 else if (TARGET_V850E)
771 return "mov %1,%0";
772 else
773 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
774 }
775
776 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
777 {
778 HOST_WIDE_INT high, low;
779
780 const_double_split (src, &high, &low);
781
782 if (CONST_OK_FOR_J (high)) /* Signed 5 bit immediate. */
783 return "mov %F1,%0";
784
785 else if (CONST_OK_FOR_K (high)) /* Signed 16 bit immediate. */
786 return "movea lo(%F1),%.,%0";
787
788 else if (CONST_OK_FOR_L (high)) /* Upper 16 bits were set. */
789 return "movhi hi(%F1),%.,%0";
790
791 /* A random constant. */
792 else if (TARGET_V850E)
793 return "mov %F1,%0";
794
795 else
796 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
797 }
798
799 else if (GET_CODE (src) == MEM)
800 return "%S1ld%W1 %1,%0";
801
802 else if (special_symbolref_operand (src, VOIDmode))
803 return "movea %O1(%P1),%Q1,%0";
804
805 else if (GET_CODE (src) == LABEL_REF
806 || GET_CODE (src) == SYMBOL_REF
807 || GET_CODE (src) == CONST)
808 {
809 if (TARGET_V850E)
810 return "mov hilo(%1),%0";
811 else
812 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
813 }
814
815 else if (GET_CODE (src) == HIGH)
816 return "movhi hi(%1),%.,%0";
817
818 else if (GET_CODE (src) == LO_SUM)
819 {
820 operands[2] = XEXP (src, 0);
821 operands[3] = XEXP (src, 1);
822 return "movea lo(%3),%2,%0";
823 }
824 }
825
826 else if (GET_CODE (dst) == MEM)
827 {
828 if (REG_P (src))
829 return "%S0st%W0 %1,%0";
830
831 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
832 return "%S0st%W0 %.,%0";
833
834 else if (GET_CODE (src) == CONST_DOUBLE
835 && CONST0_RTX (GET_MODE (dst)) == src)
836 return "%S0st%W0 %.,%0";
837 }
838
839 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
840 return "";
841 }
842
843 \f
844 /* Return appropriate code to load up an 8 byte integer or
845 floating point value */
846
847 const char *
848 output_move_double (operands)
849 rtx *operands;
850 {
851 enum machine_mode mode = GET_MODE (operands[0]);
852 rtx dst = operands[0];
853 rtx src = operands[1];
854
855 if (register_operand (dst, mode)
856 && register_operand (src, mode))
857 {
858 if (REGNO (src) + 1 == REGNO (dst))
859 return "mov %R1,%R0\n\tmov %1,%0";
860 else
861 return "mov %1,%0\n\tmov %R1,%R0";
862 }
863
864 /* Storing 0 */
865 if (GET_CODE (dst) == MEM
866 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
867 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
868 return "st.w %.,%0\n\tst.w %.,%R0";
869
870 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
871 {
872 HOST_WIDE_INT high_low[2];
873 int i;
874 rtx xop[10];
875
876 if (GET_CODE (src) == CONST_DOUBLE)
877 const_double_split (src, &high_low[1], &high_low[0]);
878 else
879 {
880 high_low[0] = INTVAL (src);
881 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
882 }
883
884 for (i = 0; i < 2; i++)
885 {
886 xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
887 xop[1] = GEN_INT (high_low[i]);
888 output_asm_insn (output_move_single (xop), xop);
889 }
890
891 return "";
892 }
893
894 if (GET_CODE (src) == MEM)
895 {
896 int ptrreg = -1;
897 int dreg = REGNO (dst);
898 rtx inside = XEXP (src, 0);
899
900 if (GET_CODE (inside) == REG)
901 ptrreg = REGNO (inside);
902 else if (GET_CODE (inside) == SUBREG)
903 ptrreg = subreg_regno (inside);
904 else if (GET_CODE (inside) == PLUS)
905 ptrreg = REGNO (XEXP (inside, 0));
906 else if (GET_CODE (inside) == LO_SUM)
907 ptrreg = REGNO (XEXP (inside, 0));
908
909 if (dreg == ptrreg)
910 return "ld.w %R1,%R0\n\tld.w %1,%0";
911 }
912
913 if (GET_CODE (src) == MEM)
914 return "ld.w %1,%0\n\tld.w %R1,%R0";
915
916 if (GET_CODE (dst) == MEM)
917 return "st.w %1,%0\n\tst.w %R1,%R0";
918
919 return "mov %1,%0\n\tmov %R1,%R0";
920 }
921
922 \f
923 /* Return maximum offset supported for a short EP memory reference of mode
924 MODE and signedness UNSIGNEDP. */
925
926 static int
927 ep_memory_offset (mode, unsignedp)
928 enum machine_mode mode;
929 int ATTRIBUTE_UNUSED unsignedp;
930 {
931 int max_offset = 0;
932
933 switch (mode)
934 {
935 case QImode:
936 if (TARGET_SMALL_SLD)
937 max_offset = (1 << 4);
938 else if (TARGET_V850E
939 && ( ( unsignedp && ! TARGET_US_BIT_SET)
940 || (! unsignedp && TARGET_US_BIT_SET)))
941 max_offset = (1 << 4);
942 else
943 max_offset = (1 << 7);
944 break;
945
946 case HImode:
947 if (TARGET_SMALL_SLD)
948 max_offset = (1 << 5);
949 else if (TARGET_V850E
950 && ( ( unsignedp && ! TARGET_US_BIT_SET)
951 || (! unsignedp && TARGET_US_BIT_SET)))
952 max_offset = (1 << 5);
953 else
954 max_offset = (1 << 8);
955 break;
956
957 case SImode:
958 case SFmode:
959 max_offset = (1 << 8);
960 break;
961
962 default:
963 break;
964 }
965
966 return max_offset;
967 }
968
969 /* Return true if OP is a valid short EP memory reference */
970
971 int
972 ep_memory_operand (op, mode, unsigned_load)
973 rtx op;
974 enum machine_mode mode;
975 int unsigned_load;
976 {
977 rtx addr, op0, op1;
978 int max_offset;
979 int mask;
980
981 if (GET_CODE (op) != MEM)
982 return FALSE;
983
984 max_offset = ep_memory_offset (mode, unsigned_load);
985
986 mask = GET_MODE_SIZE (mode) - 1;
987
988 addr = XEXP (op, 0);
989 if (GET_CODE (addr) == CONST)
990 addr = XEXP (addr, 0);
991
992 switch (GET_CODE (addr))
993 {
994 default:
995 break;
996
997 case SYMBOL_REF:
998 return TDA_NAME_P (XSTR (addr, 0));
999
1000 case REG:
1001 return REGNO (addr) == EP_REGNUM;
1002
1003 case PLUS:
1004 op0 = XEXP (addr, 0);
1005 op1 = XEXP (addr, 1);
1006 if (GET_CODE (op1) == CONST_INT
1007 && INTVAL (op1) < max_offset
1008 && INTVAL (op1) >= 0
1009 && (INTVAL (op1) & mask) == 0)
1010 {
1011 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1012 return TRUE;
1013
1014 if (GET_CODE (op0) == SYMBOL_REF && TDA_NAME_P (XSTR (op0, 0)))
1015 return TRUE;
1016 }
1017 break;
1018 }
1019
1020 return FALSE;
1021 }
1022
1023 /* Return true if OP is either a register or 0 */
1024
1025 int
1026 reg_or_0_operand (op, mode)
1027 rtx op;
1028 enum machine_mode mode;
1029 {
1030 if (GET_CODE (op) == CONST_INT)
1031 return INTVAL (op) == 0;
1032
1033 else if (GET_CODE (op) == CONST_DOUBLE)
1034 return CONST_DOUBLE_OK_FOR_G (op);
1035
1036 else
1037 return register_operand (op, mode);
1038 }
1039
1040 /* Return true if OP is either a register or a signed five bit integer */
1041
1042 int
1043 reg_or_int5_operand (op, mode)
1044 rtx op;
1045 enum machine_mode mode;
1046 {
1047 if (GET_CODE (op) == CONST_INT)
1048 return CONST_OK_FOR_J (INTVAL (op));
1049
1050 else
1051 return register_operand (op, mode);
1052 }
1053
1054 /* Return true if OP is either a register or a signed nine bit integer. */
1055
1056 int
1057 reg_or_int9_operand (op, mode)
1058 rtx op;
1059 enum machine_mode mode;
1060 {
1061 if (GET_CODE (op) == CONST_INT)
1062 return CONST_OK_FOR_O (INTVAL (op));
1063
1064 return register_operand (op, mode);
1065 }
1066
1067 /* Return true if OP is either a register or a const integer. */
1068
1069 int
1070 reg_or_const_operand (op, mode)
1071 rtx op;
1072 enum machine_mode mode;
1073 {
1074 if (GET_CODE (op) == CONST_INT)
1075 return TRUE;
1076
1077 return register_operand (op, mode);
1078 }
1079
1080 /* Return true if OP is a valid call operand. */
1081
1082 int
1083 call_address_operand (op, mode)
1084 rtx op;
1085 enum machine_mode ATTRIBUTE_UNUSED mode;
1086 {
1087 /* Only registers are valid call operands if TARGET_LONG_CALLS. */
1088 if (TARGET_LONG_CALLS)
1089 return GET_CODE (op) == REG;
1090 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
1091 }
1092
1093 int
1094 special_symbolref_operand (op, mode)
1095 rtx op;
1096 enum machine_mode ATTRIBUTE_UNUSED mode;
1097 {
1098 if (GET_CODE (op) == SYMBOL_REF)
1099 return ENCODED_NAME_P (XSTR (op, 0));
1100
1101 else if (GET_CODE (op) == CONST)
1102 return (GET_CODE (XEXP (op, 0)) == PLUS
1103 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
1104 && ENCODED_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0))
1105 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
1106 && CONST_OK_FOR_K (INTVAL (XEXP (XEXP (op, 0), 1))));
1107
1108 return FALSE;
1109 }
1110
1111 int
1112 movsi_source_operand (op, mode)
1113 rtx op;
1114 enum machine_mode mode;
1115 {
1116 /* Some constants, as well as symbolic operands
1117 must be done with HIGH & LO_SUM patterns. */
1118 if (CONSTANT_P (op)
1119 && GET_CODE (op) != HIGH
1120 && GET_CODE (op) != CONSTANT_P_RTX
1121 && !(GET_CODE (op) == CONST_INT
1122 && (CONST_OK_FOR_J (INTVAL (op))
1123 || CONST_OK_FOR_K (INTVAL (op))
1124 || CONST_OK_FOR_L (INTVAL (op)))))
1125 return special_symbolref_operand (op, mode);
1126 else
1127 return general_operand (op, mode);
1128 }
1129
1130 int
1131 power_of_two_operand (op, mode)
1132 rtx op;
1133 enum machine_mode ATTRIBUTE_UNUSED mode;
1134 {
1135 if (GET_CODE (op) != CONST_INT)
1136 return 0;
1137
1138 if (exact_log2 (INTVAL (op)) == -1)
1139 return 0;
1140 return 1;
1141 }
1142
1143 int
1144 not_power_of_two_operand (op, mode)
1145 rtx op;
1146 enum machine_mode mode;
1147 {
1148 unsigned int mask;
1149
1150 if (mode == QImode)
1151 mask = 0xff;
1152 else if (mode == HImode)
1153 mask = 0xffff;
1154 else if (mode == SImode)
1155 mask = 0xffffffff;
1156 else
1157 return 0;
1158
1159 if (GET_CODE (op) != CONST_INT)
1160 return 0;
1161
1162 if (exact_log2 (~INTVAL (op) & mask) == -1)
1163 return 0;
1164 return 1;
1165 }
1166
1167 \f
1168 /* Substitute memory references involving a pointer, to use the ep pointer,
1169 taking care to save and preserve the ep. */
1170
1171 static void
1172 substitute_ep_register (first_insn, last_insn, uses, regno, p_r1, p_ep)
1173 rtx first_insn;
1174 rtx last_insn;
1175 int uses;
1176 int regno;
1177 rtx *p_r1;
1178 rtx *p_ep;
1179 {
1180 rtx reg = gen_rtx_REG (Pmode, regno);
1181 rtx insn;
1182
1183 if (!*p_r1)
1184 {
1185 regs_ever_live[1] = 1;
1186 *p_r1 = gen_rtx_REG (Pmode, 1);
1187 *p_ep = gen_rtx_REG (Pmode, 30);
1188 }
1189
1190 if (TARGET_DEBUG)
1191 fprintf (stderr, "\
1192 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1193 2 * (uses - 3), uses, reg_names[regno],
1194 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1195 INSN_UID (first_insn), INSN_UID (last_insn));
1196
1197 if (GET_CODE (first_insn) == NOTE)
1198 first_insn = next_nonnote_insn (first_insn);
1199
1200 last_insn = next_nonnote_insn (last_insn);
1201 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1202 {
1203 if (GET_CODE (insn) == INSN)
1204 {
1205 rtx pattern = single_set (insn);
1206
1207 /* Replace the memory references. */
1208 if (pattern)
1209 {
1210 rtx *p_mem;
1211 /* Memory operands are signed by default. */
1212 int unsignedp = FALSE;
1213
1214 if (GET_CODE (SET_DEST (pattern)) == MEM
1215 && GET_CODE (SET_SRC (pattern)) == MEM)
1216 p_mem = (rtx *)0;
1217
1218 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1219 p_mem = &SET_DEST (pattern);
1220
1221 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1222 p_mem = &SET_SRC (pattern);
1223
1224 else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1225 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1226 p_mem = &XEXP (SET_SRC (pattern), 0);
1227
1228 else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1229 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1230 {
1231 p_mem = &XEXP (SET_SRC (pattern), 0);
1232 unsignedp = TRUE;
1233 }
1234 else
1235 p_mem = (rtx *)0;
1236
1237 if (p_mem)
1238 {
1239 rtx addr = XEXP (*p_mem, 0);
1240
1241 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1242 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1243
1244 else if (GET_CODE (addr) == PLUS
1245 && GET_CODE (XEXP (addr, 0)) == REG
1246 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1247 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1248 && ((INTVAL (XEXP (addr, 1)))
1249 < ep_memory_offset (GET_MODE (*p_mem),
1250 unsignedp))
1251 && ((INTVAL (XEXP (addr, 1))) >= 0))
1252 *p_mem = change_address (*p_mem, VOIDmode,
1253 gen_rtx_PLUS (Pmode,
1254 *p_ep,
1255 XEXP (addr, 1)));
1256 }
1257 }
1258 }
1259 }
1260
1261 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1262 insn = prev_nonnote_insn (first_insn);
1263 if (insn && GET_CODE (insn) == INSN
1264 && GET_CODE (PATTERN (insn)) == SET
1265 && SET_DEST (PATTERN (insn)) == *p_ep
1266 && SET_SRC (PATTERN (insn)) == *p_r1)
1267 delete_insn (insn);
1268 else
1269 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1270
1271 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1272 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1273 }
1274
1275 \f
1276 /* In rare cases, correct code generation requires extra machine
1277 dependent processing between the second jump optimization pass and
1278 delayed branch scheduling. On those machines, define this macro
1279 as a C statement to act on the code starting at INSN.
1280
1281 On the 850, we use it to implement the -mep mode to copy heavily used
1282 pointers to ep to use the implicit addressing. */
1283
1284 void v850_reorg (start_insn)
1285 rtx start_insn;
1286 {
1287 struct
1288 {
1289 int uses;
1290 rtx first_insn;
1291 rtx last_insn;
1292 }
1293 regs[FIRST_PSEUDO_REGISTER];
1294
1295 int i;
1296 int use_ep = FALSE;
1297 rtx r1 = NULL_RTX;
1298 rtx ep = NULL_RTX;
1299 rtx insn;
1300 rtx pattern;
1301
1302 /* If not ep mode, just return now. */
1303 if (!TARGET_EP)
1304 return;
1305
1306 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1307 {
1308 regs[i].uses = 0;
1309 regs[i].first_insn = NULL_RTX;
1310 regs[i].last_insn = NULL_RTX;
1311 }
1312
1313 for (insn = start_insn; insn != NULL_RTX; insn = NEXT_INSN (insn))
1314 {
1315 switch (GET_CODE (insn))
1316 {
1317 /* End of basic block */
1318 default:
1319 if (!use_ep)
1320 {
1321 int max_uses = -1;
1322 int max_regno = -1;
1323
1324 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1325 {
1326 if (max_uses < regs[i].uses)
1327 {
1328 max_uses = regs[i].uses;
1329 max_regno = i;
1330 }
1331 }
1332
1333 if (max_uses > 3)
1334 substitute_ep_register (regs[max_regno].first_insn,
1335 regs[max_regno].last_insn,
1336 max_uses, max_regno, &r1, &ep);
1337 }
1338
1339 use_ep = FALSE;
1340 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1341 {
1342 regs[i].uses = 0;
1343 regs[i].first_insn = NULL_RTX;
1344 regs[i].last_insn = NULL_RTX;
1345 }
1346 break;
1347
1348 case NOTE:
1349 break;
1350
1351 case INSN:
1352 pattern = single_set (insn);
1353
1354 /* See if there are any memory references we can shorten */
1355 if (pattern)
1356 {
1357 rtx src = SET_SRC (pattern);
1358 rtx dest = SET_DEST (pattern);
1359 rtx mem;
1360 /* Memory operands are signed by default. */
1361 int unsignedp = FALSE;
1362
1363 /* We might have (SUBREG (MEM)) here, so just get rid of the
1364 subregs to make this code simpler. */
1365 if (GET_CODE (dest) == SUBREG
1366 && (GET_CODE (SUBREG_REG (dest)) == MEM
1367 || GET_CODE (SUBREG_REG (dest)) == REG))
1368 alter_subreg (&dest);
1369 if (GET_CODE (src) == SUBREG
1370 && (GET_CODE (SUBREG_REG (src)) == MEM
1371 || GET_CODE (SUBREG_REG (src)) == REG))
1372 alter_subreg (&src);
1373
1374 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1375 mem = NULL_RTX;
1376
1377 else if (GET_CODE (dest) == MEM)
1378 mem = dest;
1379
1380 else if (GET_CODE (src) == MEM)
1381 mem = src;
1382
1383 else if (GET_CODE (src) == SIGN_EXTEND
1384 && GET_CODE (XEXP (src, 0)) == MEM)
1385 mem = XEXP (src, 0);
1386
1387 else if (GET_CODE (src) == ZERO_EXTEND
1388 && GET_CODE (XEXP (src, 0)) == MEM)
1389 {
1390 mem = XEXP (src, 0);
1391 unsignedp = TRUE;
1392 }
1393 else
1394 mem = NULL_RTX;
1395
1396 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1397 use_ep = TRUE;
1398
1399 else if (!use_ep && mem
1400 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1401 {
1402 rtx addr = XEXP (mem, 0);
1403 int regno = -1;
1404 int short_p;
1405
1406 if (GET_CODE (addr) == REG)
1407 {
1408 short_p = TRUE;
1409 regno = REGNO (addr);
1410 }
1411
1412 else if (GET_CODE (addr) == PLUS
1413 && GET_CODE (XEXP (addr, 0)) == REG
1414 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1415 && ((INTVAL (XEXP (addr, 1)))
1416 < ep_memory_offset (GET_MODE (mem), unsignedp))
1417 && ((INTVAL (XEXP (addr, 1))) >= 0))
1418 {
1419 short_p = TRUE;
1420 regno = REGNO (XEXP (addr, 0));
1421 }
1422
1423 else
1424 short_p = FALSE;
1425
1426 if (short_p)
1427 {
1428 regs[regno].uses++;
1429 regs[regno].last_insn = insn;
1430 if (!regs[regno].first_insn)
1431 regs[regno].first_insn = insn;
1432 }
1433 }
1434
1435 /* Loading up a register in the basic block zaps any savings
1436 for the register */
1437 if (GET_CODE (dest) == REG)
1438 {
1439 enum machine_mode mode = GET_MODE (dest);
1440 int regno;
1441 int endregno;
1442
1443 regno = REGNO (dest);
1444 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1445
1446 if (!use_ep)
1447 {
1448 /* See if we can use the pointer before this
1449 modification. */
1450 int max_uses = -1;
1451 int max_regno = -1;
1452
1453 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1454 {
1455 if (max_uses < regs[i].uses)
1456 {
1457 max_uses = regs[i].uses;
1458 max_regno = i;
1459 }
1460 }
1461
1462 if (max_uses > 3
1463 && max_regno >= regno
1464 && max_regno < endregno)
1465 {
1466 substitute_ep_register (regs[max_regno].first_insn,
1467 regs[max_regno].last_insn,
1468 max_uses, max_regno, &r1,
1469 &ep);
1470
1471 /* Since we made a substitution, zap all remembered
1472 registers. */
1473 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1474 {
1475 regs[i].uses = 0;
1476 regs[i].first_insn = NULL_RTX;
1477 regs[i].last_insn = NULL_RTX;
1478 }
1479 }
1480 }
1481
1482 for (i = regno; i < endregno; i++)
1483 {
1484 regs[i].uses = 0;
1485 regs[i].first_insn = NULL_RTX;
1486 regs[i].last_insn = NULL_RTX;
1487 }
1488 }
1489 }
1490 }
1491 }
1492 }
1493
1494 \f
1495 /* # of registers saved by the interrupt handler. */
1496 #define INTERRUPT_FIXED_NUM 4
1497
1498 /* # of bytes for registers saved by the interrupt handler. */
1499 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1500
1501 /* # of registers saved in register parameter area. */
1502 #define INTERRUPT_REGPARM_NUM 4
1503 /* # of words saved for other registers. */
1504 #define INTERRUPT_ALL_SAVE_NUM \
1505 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1506
1507 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1508
1509 int
1510 compute_register_save_size (p_reg_saved)
1511 long *p_reg_saved;
1512 {
1513 int size = 0;
1514 int i;
1515 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1516 int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1517 long reg_saved = 0;
1518
1519 /* Count the return pointer if we need to save it. */
1520 if (current_function_profile && !call_p)
1521 regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1522
1523 /* Count space for the register saves. */
1524 if (interrupt_handler)
1525 {
1526 for (i = 0; i <= 31; i++)
1527 switch (i)
1528 {
1529 default:
1530 if (regs_ever_live[i] || call_p)
1531 {
1532 size += 4;
1533 reg_saved |= 1L << i;
1534 }
1535 break;
1536
1537 /* We don't save/restore r0 or the stack pointer */
1538 case 0:
1539 case STACK_POINTER_REGNUM:
1540 break;
1541
1542 /* For registers with fixed use, we save them, set them to the
1543 appropriate value, and then restore them.
1544 These registers are handled specially, so don't list them
1545 on the list of registers to save in the prologue. */
1546 case 1: /* temp used to hold ep */
1547 case 4: /* gp */
1548 case 10: /* temp used to call interrupt save/restore */
1549 case EP_REGNUM: /* ep */
1550 size += 4;
1551 break;
1552 }
1553 }
1554 else
1555 {
1556 /* Find the first register that needs to be saved. */
1557 for (i = 0; i <= 31; i++)
1558 if (regs_ever_live[i] && ((! call_used_regs[i])
1559 || i == LINK_POINTER_REGNUM))
1560 break;
1561
1562 /* If it is possible that an out-of-line helper function might be
1563 used to generate the prologue for the current function, then we
1564 need to cover the possibility that such a helper function will
1565 be used, despite the fact that there might be gaps in the list of
1566 registers that need to be saved. To detect this we note that the
1567 helper functions always push at least register r29 (provided
1568 that the function is not an interrupt handler). */
1569
1570 if (TARGET_PROLOG_FUNCTION
1571 && (i == 2 || ((i >= 20) && (i < 30))))
1572 {
1573 if (i == 2)
1574 {
1575 size += 4;
1576 reg_saved |= 1L << i;
1577
1578 i = 20;
1579 }
1580
1581 /* Helper functions save all registers between the starting
1582 register and the last register, regardless of whether they
1583 are actually used by the function or not. */
1584 for (; i <= 29; i++)
1585 {
1586 size += 4;
1587 reg_saved |= 1L << i;
1588 }
1589
1590 if (regs_ever_live [LINK_POINTER_REGNUM])
1591 {
1592 size += 4;
1593 reg_saved |= 1L << LINK_POINTER_REGNUM;
1594 }
1595 }
1596 else
1597 {
1598 for (; i <= 31; i++)
1599 if (regs_ever_live[i] && ((! call_used_regs[i])
1600 || i == LINK_POINTER_REGNUM))
1601 {
1602 size += 4;
1603 reg_saved |= 1L << i;
1604 }
1605 }
1606 }
1607
1608 if (p_reg_saved)
1609 *p_reg_saved = reg_saved;
1610
1611 return size;
1612 }
1613
1614 int
1615 compute_frame_size (size, p_reg_saved)
1616 int size;
1617 long *p_reg_saved;
1618 {
1619 return (size
1620 + compute_register_save_size (p_reg_saved)
1621 + current_function_outgoing_args_size);
1622 }
1623
1624 \f
1625 void
1626 expand_prologue ()
1627 {
1628 unsigned int i;
1629 int offset;
1630 unsigned int size = get_frame_size ();
1631 unsigned int actual_fsize;
1632 unsigned int init_stack_alloc = 0;
1633 rtx save_regs[32];
1634 rtx save_all;
1635 unsigned int num_save;
1636 unsigned int default_stack;
1637 int code;
1638 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1639 long reg_saved = 0;
1640
1641 actual_fsize = compute_frame_size (size, &reg_saved);
1642
1643 /* Save/setup global registers for interrupt functions right now. */
1644 if (interrupt_handler)
1645 {
1646 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1647 emit_insn (gen_callt_save_interrupt ());
1648 else
1649 emit_insn (gen_save_interrupt ());
1650
1651 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1652
1653 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1654 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1655 }
1656
1657 /* Save arg registers to the stack if necessary. */
1658 else if (current_function_args_info.anonymous_args)
1659 {
1660 if (TARGET_PROLOG_FUNCTION)
1661 {
1662 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1663 emit_insn (gen_save_r6_r9_v850e ());
1664 else
1665 emit_insn (gen_save_r6_r9 ());
1666 }
1667 else
1668 {
1669 offset = 0;
1670 for (i = 6; i < 10; i++)
1671 {
1672 emit_move_insn (gen_rtx_MEM (SImode,
1673 plus_constant (stack_pointer_rtx,
1674 offset)),
1675 gen_rtx_REG (SImode, i));
1676 offset += 4;
1677 }
1678 }
1679 }
1680
1681 /* Identify all of the saved registers. */
1682 num_save = 0;
1683 default_stack = 0;
1684 for (i = 1; i < 31; i++)
1685 {
1686 if (((1L << i) & reg_saved) != 0)
1687 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1688 }
1689
1690 /* If the return pointer is saved, the helper functions also allocate
1691 16 bytes of stack for arguments to be saved in. */
1692 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1693 {
1694 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1695 default_stack = 16;
1696 }
1697
1698 /* See if we have an insn that allocates stack space and saves the particular
1699 registers we want to. */
1700 save_all = NULL_RTX;
1701 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1702 {
1703 int alloc_stack = (4 * num_save) + default_stack;
1704 int unalloc_stack = actual_fsize - alloc_stack;
1705 int save_func_len = 4;
1706 int save_normal_len;
1707
1708 if (unalloc_stack)
1709 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1710
1711 /* see if we would have used ep to save the stack */
1712 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1713 save_normal_len = (3 * 2) + (2 * num_save);
1714 else
1715 save_normal_len = 4 * num_save;
1716
1717 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1718
1719 /* Don't bother checking if we don't actually save any space.
1720 This happens for instance if one register is saved and additional
1721 stack space is allocated. */
1722 if (save_func_len < save_normal_len)
1723 {
1724 save_all = gen_rtx_PARALLEL
1725 (VOIDmode,
1726 rtvec_alloc (num_save + (TARGET_V850 ? 2 : 1)));
1727
1728 XVECEXP (save_all, 0, 0)
1729 = gen_rtx_SET (VOIDmode,
1730 stack_pointer_rtx,
1731 plus_constant (stack_pointer_rtx, -alloc_stack));
1732
1733 if (TARGET_V850)
1734 {
1735 XVECEXP (save_all, 0, num_save+1)
1736 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1737 }
1738
1739 offset = - default_stack;
1740 for (i = 0; i < num_save; i++)
1741 {
1742 XVECEXP (save_all, 0, i+1)
1743 = gen_rtx_SET (VOIDmode,
1744 gen_rtx_MEM (Pmode,
1745 plus_constant (stack_pointer_rtx,
1746 offset)),
1747 save_regs[i]);
1748 offset -= 4;
1749 }
1750
1751 code = recog (save_all, NULL_RTX, NULL);
1752 if (code >= 0)
1753 {
1754 rtx insn = emit_insn (save_all);
1755 INSN_CODE (insn) = code;
1756 actual_fsize -= alloc_stack;
1757
1758 if (TARGET_DEBUG)
1759 fprintf (stderr, "\
1760 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1761 save_normal_len - save_func_len,
1762 save_normal_len, save_func_len,
1763 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1764 }
1765 else
1766 save_all = NULL_RTX;
1767 }
1768 }
1769
1770 /* If no prolog save function is available, store the registers the old
1771 fashioned way (one by one). */
1772 if (!save_all)
1773 {
1774 /* Special case interrupt functions that save all registers for a call. */
1775 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1776 {
1777 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1778 emit_insn (gen_callt_save_all_interrupt ());
1779 else
1780 emit_insn (gen_save_all_interrupt ());
1781 }
1782 else
1783 {
1784 /* If the stack is too big, allocate it in chunks so we can do the
1785 register saves. We use the register save size so we use the ep
1786 register. */
1787 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1788 init_stack_alloc = compute_register_save_size (NULL);
1789 else
1790 init_stack_alloc = actual_fsize;
1791
1792 /* Save registers at the beginning of the stack frame */
1793 offset = init_stack_alloc - 4;
1794
1795 if (init_stack_alloc)
1796 emit_insn (gen_addsi3 (stack_pointer_rtx,
1797 stack_pointer_rtx,
1798 GEN_INT (-init_stack_alloc)));
1799
1800 /* Save the return pointer first. */
1801 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1802 {
1803 emit_move_insn (gen_rtx_MEM (SImode,
1804 plus_constant (stack_pointer_rtx,
1805 offset)),
1806 save_regs[--num_save]);
1807 offset -= 4;
1808 }
1809
1810 for (i = 0; i < num_save; i++)
1811 {
1812 emit_move_insn (gen_rtx_MEM (SImode,
1813 plus_constant (stack_pointer_rtx,
1814 offset)),
1815 save_regs[i]);
1816 offset -= 4;
1817 }
1818 }
1819 }
1820
1821 /* Allocate the rest of the stack that was not allocated above (either it is
1822 > 32K or we just called a function to save the registers and needed more
1823 stack. */
1824 if (actual_fsize > init_stack_alloc)
1825 {
1826 int diff = actual_fsize - init_stack_alloc;
1827 if (CONST_OK_FOR_K (diff))
1828 emit_insn (gen_addsi3 (stack_pointer_rtx,
1829 stack_pointer_rtx,
1830 GEN_INT (-diff)));
1831 else
1832 {
1833 rtx reg = gen_rtx_REG (Pmode, 12);
1834 emit_move_insn (reg, GEN_INT (-diff));
1835 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1836 }
1837 }
1838
1839 /* If we need a frame pointer, set it up now. */
1840 if (frame_pointer_needed)
1841 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1842 }
1843 \f
1844
1845 void
1846 expand_epilogue ()
1847 {
1848 unsigned int i;
1849 int offset;
1850 unsigned int size = get_frame_size ();
1851 long reg_saved = 0;
1852 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1853 unsigned int init_stack_free = 0;
1854 rtx restore_regs[32];
1855 rtx restore_all;
1856 unsigned int num_restore;
1857 unsigned int default_stack;
1858 int code;
1859 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1860
1861 /* Eliminate the initial stack stored by interrupt functions. */
1862 if (interrupt_handler)
1863 {
1864 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1865 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1866 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1867 }
1868
1869 /* Cut off any dynamic stack created. */
1870 if (frame_pointer_needed)
1871 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1872
1873 /* Identify all of the saved registers. */
1874 num_restore = 0;
1875 default_stack = 0;
1876 for (i = 1; i < 31; i++)
1877 {
1878 if (((1L << i) & reg_saved) != 0)
1879 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1880 }
1881
1882 /* If the return pointer is saved, the helper functions also allocate
1883 16 bytes of stack for arguments to be saved in. */
1884 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1885 {
1886 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1887 default_stack = 16;
1888 }
1889
1890 /* See if we have an insn that restores the particular registers we
1891 want to. */
1892 restore_all = NULL_RTX;
1893
1894 if (TARGET_PROLOG_FUNCTION
1895 && num_restore > 0
1896 && actual_fsize >= default_stack
1897 && !interrupt_handler)
1898 {
1899 int alloc_stack = (4 * num_restore) + default_stack;
1900 int unalloc_stack = actual_fsize - alloc_stack;
1901 int restore_func_len = 4;
1902 int restore_normal_len;
1903
1904 if (unalloc_stack)
1905 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1906
1907 /* See if we would have used ep to restore the registers. */
1908 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1909 restore_normal_len = (3 * 2) + (2 * num_restore);
1910 else
1911 restore_normal_len = 4 * num_restore;
1912
1913 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1914
1915 /* Don't bother checking if we don't actually save any space. */
1916 if (restore_func_len < restore_normal_len)
1917 {
1918 restore_all = gen_rtx_PARALLEL (VOIDmode,
1919 rtvec_alloc (num_restore + 2));
1920 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1921 XVECEXP (restore_all, 0, 1)
1922 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1923 gen_rtx_PLUS (Pmode,
1924 stack_pointer_rtx,
1925 GEN_INT (alloc_stack)));
1926
1927 offset = alloc_stack - 4;
1928 for (i = 0; i < num_restore; i++)
1929 {
1930 XVECEXP (restore_all, 0, i+2)
1931 = gen_rtx_SET (VOIDmode,
1932 restore_regs[i],
1933 gen_rtx_MEM (Pmode,
1934 plus_constant (stack_pointer_rtx,
1935 offset)));
1936 offset -= 4;
1937 }
1938
1939 code = recog (restore_all, NULL_RTX, NULL);
1940
1941 if (code >= 0)
1942 {
1943 rtx insn;
1944
1945 actual_fsize -= alloc_stack;
1946 if (actual_fsize)
1947 {
1948 if (CONST_OK_FOR_K (actual_fsize))
1949 emit_insn (gen_addsi3 (stack_pointer_rtx,
1950 stack_pointer_rtx,
1951 GEN_INT (actual_fsize)));
1952 else
1953 {
1954 rtx reg = gen_rtx_REG (Pmode, 12);
1955 emit_move_insn (reg, GEN_INT (actual_fsize));
1956 emit_insn (gen_addsi3 (stack_pointer_rtx,
1957 stack_pointer_rtx,
1958 reg));
1959 }
1960 }
1961
1962 insn = emit_jump_insn (restore_all);
1963 INSN_CODE (insn) = code;
1964
1965 if (TARGET_DEBUG)
1966 fprintf (stderr, "\
1967 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1968 restore_normal_len - restore_func_len,
1969 restore_normal_len, restore_func_len,
1970 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1971 }
1972 else
1973 restore_all = NULL_RTX;
1974 }
1975 }
1976
1977 /* If no epilog save function is available, restore the registers the
1978 old fashioned way (one by one). */
1979 if (!restore_all)
1980 {
1981 /* If the stack is large, we need to cut it down in 2 pieces. */
1982 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1983 init_stack_free = 4 * num_restore;
1984 else
1985 init_stack_free = actual_fsize;
1986
1987 /* Deallocate the rest of the stack if it is > 32K. */
1988 if (actual_fsize > init_stack_free)
1989 {
1990 int diff;
1991
1992 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1993
1994 if (CONST_OK_FOR_K (diff))
1995 emit_insn (gen_addsi3 (stack_pointer_rtx,
1996 stack_pointer_rtx,
1997 GEN_INT (diff)));
1998 else
1999 {
2000 rtx reg = gen_rtx_REG (Pmode, 12);
2001 emit_move_insn (reg, GEN_INT (diff));
2002 emit_insn (gen_addsi3 (stack_pointer_rtx,
2003 stack_pointer_rtx,
2004 reg));
2005 }
2006 }
2007
2008 /* Special case interrupt functions that save all registers
2009 for a call. */
2010 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
2011 {
2012 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
2013 emit_insn (gen_callt_restore_all_interrupt ());
2014 else
2015 emit_insn (gen_restore_all_interrupt ());
2016 }
2017 else
2018 {
2019 /* Restore registers from the beginning of the stack frame. */
2020 offset = init_stack_free - 4;
2021
2022 /* Restore the return pointer first. */
2023 if (num_restore > 0
2024 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
2025 {
2026 emit_move_insn (restore_regs[--num_restore],
2027 gen_rtx_MEM (SImode,
2028 plus_constant (stack_pointer_rtx,
2029 offset)));
2030 offset -= 4;
2031 }
2032
2033 for (i = 0; i < num_restore; i++)
2034 {
2035 emit_move_insn (restore_regs[i],
2036 gen_rtx_MEM (SImode,
2037 plus_constant (stack_pointer_rtx,
2038 offset)));
2039
2040 emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
2041 offset -= 4;
2042 }
2043
2044 /* Cut back the remainder of the stack. */
2045 if (init_stack_free)
2046 emit_insn (gen_addsi3 (stack_pointer_rtx,
2047 stack_pointer_rtx,
2048 GEN_INT (init_stack_free)));
2049 }
2050
2051 /* And return or use reti for interrupt handlers. */
2052 if (interrupt_handler)
2053 {
2054 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
2055 emit_insn (gen_callt_return_interrupt ());
2056 else
2057 emit_jump_insn (gen_return_interrupt ());
2058 }
2059 else if (actual_fsize)
2060 emit_jump_insn (gen_return_internal ());
2061 else
2062 emit_jump_insn (gen_return ());
2063 }
2064
2065 v850_interrupt_cache_p = FALSE;
2066 v850_interrupt_p = FALSE;
2067 }
2068
2069 \f
2070 /* Update the condition code from the insn. */
2071
2072 void
2073 notice_update_cc (body, insn)
2074 rtx body;
2075 rtx insn;
2076 {
2077 switch (get_attr_cc (insn))
2078 {
2079 case CC_NONE:
2080 /* Insn does not affect CC at all. */
2081 break;
2082
2083 case CC_NONE_0HIT:
2084 /* Insn does not change CC, but the 0'th operand has been changed. */
2085 if (cc_status.value1 != 0
2086 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2087 cc_status.value1 = 0;
2088 break;
2089
2090 case CC_SET_ZN:
2091 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2092 V,C is in an unusable state. */
2093 CC_STATUS_INIT;
2094 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2095 cc_status.value1 = recog_data.operand[0];
2096 break;
2097
2098 case CC_SET_ZNV:
2099 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2100 C is in an unusable state. */
2101 CC_STATUS_INIT;
2102 cc_status.flags |= CC_NO_CARRY;
2103 cc_status.value1 = recog_data.operand[0];
2104 break;
2105
2106 case CC_COMPARE:
2107 /* The insn is a compare instruction. */
2108 CC_STATUS_INIT;
2109 cc_status.value1 = SET_SRC (body);
2110 break;
2111
2112 case CC_CLOBBER:
2113 /* Insn doesn't leave CC in a usable state. */
2114 CC_STATUS_INIT;
2115 break;
2116 }
2117 }
2118 \f
2119 /* Retrieve the data area that has been chosen for the given decl. */
2120
2121 v850_data_area
2122 v850_get_data_area (decl)
2123 tree decl;
2124 {
2125 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2126 return DATA_AREA_SDA;
2127
2128 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2129 return DATA_AREA_TDA;
2130
2131 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2132 return DATA_AREA_ZDA;
2133
2134 return DATA_AREA_NORMAL;
2135 }
2136
2137 /* Store the indicated data area in the decl's attributes. */
2138
2139 static void
2140 v850_set_data_area (decl, data_area)
2141 tree decl;
2142 v850_data_area data_area;
2143 {
2144 tree name;
2145
2146 switch (data_area)
2147 {
2148 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2149 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2150 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2151 default:
2152 return;
2153 }
2154
2155 DECL_ATTRIBUTES (decl) = tree_cons
2156 (name, NULL, DECL_ATTRIBUTES (decl));
2157 }
2158 \f
2159 const struct attribute_spec v850_attribute_table[] =
2160 {
2161 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2162 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2163 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2164 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2165 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2166 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2167 { NULL, 0, 0, false, false, false, NULL }
2168 };
2169
2170 /* Handle an "interrupt" attribute; arguments as in
2171 struct attribute_spec.handler. */
2172 static tree
2173 v850_handle_interrupt_attribute (node, name, args, flags, no_add_attrs)
2174 tree *node;
2175 tree name;
2176 tree args ATTRIBUTE_UNUSED;
2177 int flags ATTRIBUTE_UNUSED;
2178 bool *no_add_attrs;
2179 {
2180 if (TREE_CODE (*node) != FUNCTION_DECL)
2181 {
2182 warning ("`%s' attribute only applies to functions",
2183 IDENTIFIER_POINTER (name));
2184 *no_add_attrs = true;
2185 }
2186
2187 return NULL_TREE;
2188 }
2189
2190 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2191 struct attribute_spec.handler. */
2192 static tree
2193 v850_handle_data_area_attribute (node, name, args, flags, no_add_attrs)
2194 tree *node;
2195 tree name;
2196 tree args ATTRIBUTE_UNUSED;
2197 int flags ATTRIBUTE_UNUSED;
2198 bool *no_add_attrs;
2199 {
2200 v850_data_area data_area;
2201 v850_data_area area;
2202 tree decl = *node;
2203
2204 /* Implement data area attribute. */
2205 if (is_attribute_p ("sda", name))
2206 data_area = DATA_AREA_SDA;
2207 else if (is_attribute_p ("tda", name))
2208 data_area = DATA_AREA_TDA;
2209 else if (is_attribute_p ("zda", name))
2210 data_area = DATA_AREA_ZDA;
2211 else
2212 abort ();
2213
2214 switch (TREE_CODE (decl))
2215 {
2216 case VAR_DECL:
2217 if (current_function_decl != NULL_TREE)
2218 {
2219 error_with_decl (decl, "\
2220 a data area attribute cannot be specified for local variables");
2221 *no_add_attrs = true;
2222 }
2223
2224 /* Drop through. */
2225
2226 case FUNCTION_DECL:
2227 area = v850_get_data_area (decl);
2228 if (area != DATA_AREA_NORMAL && data_area != area)
2229 {
2230 error_with_decl (decl, "\
2231 data area of '%s' conflicts with previous declaration");
2232 *no_add_attrs = true;
2233 }
2234 break;
2235
2236 default:
2237 break;
2238 }
2239
2240 return NULL_TREE;
2241 }
2242
2243 \f
2244 /* Return nonzero if FUNC is an interrupt function as specified
2245 by the "interrupt" attribute. */
2246
2247 int
2248 v850_interrupt_function_p (func)
2249 tree func;
2250 {
2251 tree a;
2252 int ret = 0;
2253
2254 if (v850_interrupt_cache_p)
2255 return v850_interrupt_p;
2256
2257 if (TREE_CODE (func) != FUNCTION_DECL)
2258 return 0;
2259
2260 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2261 if (a != NULL_TREE)
2262 ret = 1;
2263
2264 else
2265 {
2266 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2267 ret = a != NULL_TREE;
2268 }
2269
2270 /* Its not safe to trust global variables until after function inlining has
2271 been done. */
2272 if (reload_completed | reload_in_progress)
2273 v850_interrupt_p = ret;
2274
2275 return ret;
2276 }
2277
2278 \f
2279 static void
2280 v850_encode_data_area (decl)
2281 tree decl;
2282 {
2283 const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2284 int len = strlen (str);
2285 char * newstr;
2286
2287 /* Map explict sections into the appropriate attribute */
2288 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2289 {
2290 if (DECL_SECTION_NAME (decl))
2291 {
2292 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2293
2294 if (streq (name, ".zdata") || streq (name, ".zbss"))
2295 v850_set_data_area (decl, DATA_AREA_ZDA);
2296
2297 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2298 v850_set_data_area (decl, DATA_AREA_SDA);
2299
2300 else if (streq (name, ".tdata"))
2301 v850_set_data_area (decl, DATA_AREA_TDA);
2302 }
2303
2304 /* If no attribute, support -m{zda,sda,tda}=n */
2305 else
2306 {
2307 int size = int_size_in_bytes (TREE_TYPE (decl));
2308 if (size <= 0)
2309 ;
2310
2311 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2312 v850_set_data_area (decl, DATA_AREA_TDA);
2313
2314 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2315 v850_set_data_area (decl, DATA_AREA_SDA);
2316
2317 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2318 v850_set_data_area (decl, DATA_AREA_ZDA);
2319 }
2320
2321 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2322 return;
2323 }
2324
2325 newstr = alloca (len + 2);
2326
2327 strcpy (newstr + 1, str);
2328
2329 switch (v850_get_data_area (decl))
2330 {
2331 case DATA_AREA_ZDA: *newstr = ZDA_NAME_FLAG_CHAR; break;
2332 case DATA_AREA_TDA: *newstr = TDA_NAME_FLAG_CHAR; break;
2333 case DATA_AREA_SDA: *newstr = SDA_NAME_FLAG_CHAR; break;
2334 default: abort ();
2335 }
2336
2337 XSTR (XEXP (DECL_RTL (decl), 0), 0) = ggc_alloc_string (newstr, len + 2);
2338 }
2339
2340 static void
2341 v850_encode_section_info (decl, first)
2342 tree decl;
2343 int first;
2344 {
2345 if (first && TREE_CODE (decl) == VAR_DECL
2346 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2347 v850_encode_data_area (decl);
2348 }
2349
2350 static const char *
2351 v850_strip_name_encoding (str)
2352 const char *str;
2353 {
2354 return str + (ENCODED_NAME_P (str) || *str == '*');
2355 }
2356
2357 /* Return true if the given RTX is a register which can be restored
2358 by a function epilogue. */
2359 int
2360 register_is_ok_for_epilogue (op, mode)
2361 rtx op;
2362 enum machine_mode ATTRIBUTE_UNUSED mode;
2363 {
2364 /* The save/restore routines can only cope with registers 20 - 31. */
2365 return ((GET_CODE (op) == REG)
2366 && (((REGNO (op) >= 20) && REGNO (op) <= 31)));
2367 }
2368
2369 /* Return nonzero if the given RTX is suitable for collapsing into
2370 jump to a function epilogue. */
2371 int
2372 pattern_is_ok_for_epilogue (op, mode)
2373 rtx op;
2374 enum machine_mode ATTRIBUTE_UNUSED mode;
2375 {
2376 int count = XVECLEN (op, 0);
2377 int i;
2378
2379 /* If there are no registers to restore then the function epilogue
2380 is not suitable. */
2381 if (count <= 2)
2382 return 0;
2383
2384 /* The pattern matching has already established that we are performing a
2385 function epilogue and that we are popping at least one register. We must
2386 now check the remaining entries in the vector to make sure that they are
2387 also register pops. There is no good reason why there should ever be
2388 anything else in this vector, but being paranoid always helps...
2389
2390 The test below performs the C equivalent of this machine description
2391 pattern match:
2392
2393 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
2394 (mem:SI (plus:SI (reg:SI 3) (match_operand:SI n "immediate_operand" "i"))))
2395 */
2396
2397 for (i = 3; i < count; i++)
2398 {
2399 rtx vector_element = XVECEXP (op, 0, i);
2400 rtx dest;
2401 rtx src;
2402 rtx plus;
2403
2404 if (GET_CODE (vector_element) != SET)
2405 return 0;
2406
2407 dest = SET_DEST (vector_element);
2408 src = SET_SRC (vector_element);
2409
2410 if (GET_CODE (dest) != REG
2411 || GET_MODE (dest) != SImode
2412 || ! register_is_ok_for_epilogue (dest, SImode)
2413 || GET_CODE (src) != MEM
2414 || GET_MODE (src) != SImode)
2415 return 0;
2416
2417 plus = XEXP (src, 0);
2418
2419 if (GET_CODE (plus) != PLUS
2420 || GET_CODE (XEXP (plus, 0)) != REG
2421 || GET_MODE (XEXP (plus, 0)) != SImode
2422 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2423 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2424 return 0;
2425 }
2426
2427 return 1;
2428 }
2429
2430 /* Construct a JR instruction to a routine that will perform the equivalent of
2431 the RTL passed in as an argument. This RTL is a function epilogue that
2432 pops registers off the stack and possibly releases some extra stack space
2433 as well. The code has already verified that the RTL matches these
2434 requirements. */
2435 char *
2436 construct_restore_jr (op)
2437 rtx op;
2438 {
2439 int count = XVECLEN (op, 0);
2440 int stack_bytes;
2441 unsigned long int mask;
2442 unsigned long int first;
2443 unsigned long int last;
2444 int i;
2445 static char buff [100]; /* XXX */
2446
2447 if (count <= 2)
2448 {
2449 error ("bogus JR construction: %d\n", count);
2450 return NULL;
2451 }
2452
2453 /* Work out how many bytes to pop off the stack before retrieving
2454 registers. */
2455 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2456 abort ();
2457 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2458 abort ();
2459 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2460 abort ();
2461
2462 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2463
2464 /* Each pop will remove 4 bytes from the stack... */
2465 stack_bytes -= (count - 2) * 4;
2466
2467 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2468 if (stack_bytes != 0 && stack_bytes != 16)
2469 {
2470 error ("bad amount of stack space removal: %d", stack_bytes);
2471 return NULL;
2472 }
2473
2474 /* Now compute the bit mask of registers to push. */
2475 mask = 0;
2476 for (i = 2; i < count; i++)
2477 {
2478 rtx vector_element = XVECEXP (op, 0, i);
2479
2480 if (GET_CODE (vector_element) != SET)
2481 abort ();
2482 if (GET_CODE (SET_DEST (vector_element)) != REG)
2483 abort ();
2484 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2485 abort ();
2486
2487 mask |= 1 << REGNO (SET_DEST (vector_element));
2488 }
2489
2490 /* Scan for the first register to pop. */
2491 for (first = 0; first < 32; first++)
2492 {
2493 if (mask & (1 << first))
2494 break;
2495 }
2496
2497 if (first >= 32)
2498 abort ();
2499
2500 /* Discover the last register to pop. */
2501 if (mask & (1 << LINK_POINTER_REGNUM))
2502 {
2503 if (stack_bytes != 16)
2504 abort ();
2505
2506 last = LINK_POINTER_REGNUM;
2507 }
2508 else
2509 {
2510 if (stack_bytes != 0)
2511 abort ();
2512
2513 if ((mask & (1 << 29)) == 0)
2514 abort ();
2515
2516 last = 29;
2517 }
2518
2519 /* Note, it is possible to have gaps in the register mask.
2520 We ignore this here, and generate a JR anyway. We will
2521 be popping more registers than is strictly necessary, but
2522 it does save code space. */
2523
2524 if (TARGET_LONG_CALLS)
2525 {
2526 char name[40];
2527
2528 if (first == last)
2529 sprintf (name, "__return_%s", reg_names [first]);
2530 else
2531 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2532
2533 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2534 name, name);
2535 }
2536 else
2537 {
2538 if (first == last)
2539 sprintf (buff, "jr __return_%s", reg_names [first]);
2540 else
2541 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2542 }
2543
2544 return buff;
2545 }
2546
2547
2548 /* Return nonzero if the given RTX is suitable for collapsing into
2549 a jump to a function prologue. */
2550 int
2551 pattern_is_ok_for_prologue (op, mode)
2552 rtx op;
2553 enum machine_mode ATTRIBUTE_UNUSED mode;
2554 {
2555 int count = XVECLEN (op, 0);
2556 int i;
2557 rtx vector_element;
2558
2559 /* If there are no registers to save then the function prologue
2560 is not suitable. */
2561 if (count <= 2)
2562 return 0;
2563
2564 /* The pattern matching has already established that we are adjusting the
2565 stack and pushing at least one register. We must now check that the
2566 remaining entries in the vector to make sure that they are also register
2567 pushes, except for the last entry which should be a CLOBBER of r10.
2568
2569 The test below performs the C equivalent of this machine description
2570 pattern match:
2571
2572 (set (mem:SI (plus:SI (reg:SI 3)
2573 (match_operand:SI 2 "immediate_operand" "i")))
2574 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
2575
2576 */
2577
2578 for (i = 2; i < count - 1; i++)
2579 {
2580 rtx dest;
2581 rtx src;
2582 rtx plus;
2583
2584 vector_element = XVECEXP (op, 0, i);
2585
2586 if (GET_CODE (vector_element) != SET)
2587 return 0;
2588
2589 dest = SET_DEST (vector_element);
2590 src = SET_SRC (vector_element);
2591
2592 if (GET_CODE (dest) != MEM
2593 || GET_MODE (dest) != SImode
2594 || GET_CODE (src) != REG
2595 || GET_MODE (src) != SImode
2596 || ! register_is_ok_for_epilogue (src, SImode))
2597 return 0;
2598
2599 plus = XEXP (dest, 0);
2600
2601 if ( GET_CODE (plus) != PLUS
2602 || GET_CODE (XEXP (plus, 0)) != REG
2603 || GET_MODE (XEXP (plus, 0)) != SImode
2604 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2605 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2606 return 0;
2607
2608 /* If the register is being pushed somewhere other than the stack
2609 space just acquired by the first operand then abandon this quest.
2610 Note: the test is <= because both values are negative. */
2611 if (INTVAL (XEXP (plus, 1))
2612 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
2613 {
2614 return 0;
2615 }
2616 }
2617
2618 /* Make sure that the last entry in the vector is a clobber. */
2619 vector_element = XVECEXP (op, 0, i);
2620
2621 if (GET_CODE (vector_element) != CLOBBER
2622 || GET_CODE (XEXP (vector_element, 0)) != REG
2623 || REGNO (XEXP (vector_element, 0)) != 10)
2624 return 0;
2625
2626 return 1;
2627 }
2628
2629 /* Construct a JARL instruction to a routine that will perform the equivalent
2630 of the RTL passed as a parameter. This RTL is a function prologue that
2631 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2632 some stack space as well. The code has already verified that the RTL
2633 matches these requirements. */
2634 char *
2635 construct_save_jarl (op)
2636 rtx op;
2637 {
2638 int count = XVECLEN (op, 0);
2639 int stack_bytes;
2640 unsigned long int mask;
2641 unsigned long int first;
2642 unsigned long int last;
2643 int i;
2644 static char buff [100]; /* XXX */
2645
2646 if (count <= 2)
2647 {
2648 error ("bogus JARL construction: %d\n", count);
2649 return NULL;
2650 }
2651
2652 /* Paranoia. */
2653 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2654 abort ();
2655 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2656 abort ();
2657 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2658 abort ();
2659 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2660 abort ();
2661
2662 /* Work out how many bytes to push onto the stack after storing the
2663 registers. */
2664 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2665
2666 /* Each push will put 4 bytes from the stack... */
2667 stack_bytes += (count - 2) * 4;
2668
2669 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2670 if (stack_bytes != 0 && stack_bytes != -16)
2671 {
2672 error ("bad amount of stack space removal: %d", stack_bytes);
2673 return NULL;
2674 }
2675
2676 /* Now compute the bit mask of registers to push. */
2677 mask = 0;
2678 for (i = 1; i < count - 1; i++)
2679 {
2680 rtx vector_element = XVECEXP (op, 0, i);
2681
2682 if (GET_CODE (vector_element) != SET)
2683 abort ();
2684 if (GET_CODE (SET_SRC (vector_element)) != REG)
2685 abort ();
2686 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2687 abort ();
2688
2689 mask |= 1 << REGNO (SET_SRC (vector_element));
2690 }
2691
2692 /* Scan for the first register to push. */
2693 for (first = 0; first < 32; first++)
2694 {
2695 if (mask & (1 << first))
2696 break;
2697 }
2698
2699 if (first >= 32)
2700 abort ();
2701
2702 /* Discover the last register to push. */
2703 if (mask & (1 << LINK_POINTER_REGNUM))
2704 {
2705 if (stack_bytes != -16)
2706 abort ();
2707
2708 last = LINK_POINTER_REGNUM;
2709 }
2710 else
2711 {
2712 if (stack_bytes != 0)
2713 abort ();
2714 if ((mask & (1 << 29)) == 0)
2715 abort ();
2716
2717 last = 29;
2718 }
2719
2720 /* Note, it is possible to have gaps in the register mask.
2721 We ignore this here, and generate a JARL anyway. We will
2722 be pushing more registers than is strictly necessary, but
2723 it does save code space. */
2724
2725 if (TARGET_LONG_CALLS)
2726 {
2727 char name[40];
2728
2729 if (first == last)
2730 sprintf (name, "__save_%s", reg_names [first]);
2731 else
2732 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2733
2734 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2735 name, name);
2736 }
2737 else
2738 {
2739 if (first == last)
2740 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2741 else
2742 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2743 reg_names [last]);
2744 }
2745
2746 return buff;
2747 }
2748
2749 extern tree last_assemble_variable_decl;
2750 extern int size_directive_output;
2751
2752 /* A version of asm_output_aligned_bss() that copes with the special
2753 data areas of the v850. */
2754 void
2755 v850_output_aligned_bss (file, decl, name, size, align)
2756 FILE * file;
2757 tree decl;
2758 const char * name;
2759 int size;
2760 int align;
2761 {
2762 (*targetm.asm_out.globalize_label) (file, name);
2763
2764 switch (v850_get_data_area (decl))
2765 {
2766 case DATA_AREA_ZDA:
2767 zbss_section ();
2768 break;
2769
2770 case DATA_AREA_SDA:
2771 sbss_section ();
2772 break;
2773
2774 case DATA_AREA_TDA:
2775 tdata_section ();
2776
2777 default:
2778 bss_section ();
2779 break;
2780 }
2781
2782 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2783 #ifdef ASM_DECLARE_OBJECT_NAME
2784 last_assemble_variable_decl = decl;
2785 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2786 #else
2787 /* Standard thing is just output label for the object. */
2788 ASM_OUTPUT_LABEL (file, name);
2789 #endif /* ASM_DECLARE_OBJECT_NAME */
2790 ASM_OUTPUT_SKIP (file, size ? size : 1);
2791 }
2792
2793 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2794 void
2795 v850_output_common (file, decl, name, size, align)
2796 FILE * file;
2797 tree decl;
2798 const char * name;
2799 int size;
2800 int align;
2801 {
2802 if (decl == NULL_TREE)
2803 {
2804 fprintf (file, "%s", COMMON_ASM_OP);
2805 }
2806 else
2807 {
2808 switch (v850_get_data_area (decl))
2809 {
2810 case DATA_AREA_ZDA:
2811 fprintf (file, "%s", ZCOMMON_ASM_OP);
2812 break;
2813
2814 case DATA_AREA_SDA:
2815 fprintf (file, "%s", SCOMMON_ASM_OP);
2816 break;
2817
2818 case DATA_AREA_TDA:
2819 fprintf (file, "%s", TCOMMON_ASM_OP);
2820 break;
2821
2822 default:
2823 fprintf (file, "%s", COMMON_ASM_OP);
2824 break;
2825 }
2826 }
2827
2828 assemble_name (file, name);
2829 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2830 }
2831
2832 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2833 void
2834 v850_output_local (file, decl, name, size, align)
2835 FILE * file;
2836 tree decl;
2837 const char * name;
2838 int size;
2839 int align;
2840 {
2841 fprintf (file, "%s", LOCAL_ASM_OP);
2842 assemble_name (file, name);
2843 fprintf (file, "\n");
2844
2845 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2846 }
2847
2848 /* Add data area to the given declaration if a ghs data area pragma is
2849 currently in effect (#pragma ghs startXXX/endXXX). */
2850 static void
2851 v850_insert_attributes (decl, attr_ptr)
2852 tree decl;
2853 tree *attr_ptr ATTRIBUTE_UNUSED;
2854 {
2855 if (data_area_stack
2856 && data_area_stack->data_area
2857 && current_function_decl == NULL_TREE
2858 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2859 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2860 v850_set_data_area (decl, data_area_stack->data_area);
2861
2862 /* Initialize the default names of the v850 specific sections,
2863 if this has not been done before. */
2864
2865 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2866 {
2867 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2868 = build_string (sizeof (".sdata")-1, ".sdata");
2869
2870 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2871 = build_string (sizeof (".rosdata")-1, ".rosdata");
2872
2873 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2874 = build_string (sizeof (".tdata")-1, ".tdata");
2875
2876 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2877 = build_string (sizeof (".zdata")-1, ".zdata");
2878
2879 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2880 = build_string (sizeof (".rozdata")-1, ".rozdata");
2881 }
2882
2883 if (current_function_decl == NULL_TREE
2884 && (TREE_CODE (decl) == VAR_DECL
2885 || TREE_CODE (decl) == CONST_DECL
2886 || TREE_CODE (decl) == FUNCTION_DECL)
2887 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2888 && !DECL_SECTION_NAME (decl))
2889 {
2890 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2891 tree chosen_section;
2892
2893 if (TREE_CODE (decl) == FUNCTION_DECL)
2894 kind = GHS_SECTION_KIND_TEXT;
2895 else
2896 {
2897 /* First choose a section kind based on the data area of the decl. */
2898 switch (v850_get_data_area (decl))
2899 {
2900 default:
2901 abort ();
2902
2903 case DATA_AREA_SDA:
2904 kind = ((TREE_READONLY (decl))
2905 ? GHS_SECTION_KIND_ROSDATA
2906 : GHS_SECTION_KIND_SDATA);
2907 break;
2908
2909 case DATA_AREA_TDA:
2910 kind = GHS_SECTION_KIND_TDATA;
2911 break;
2912
2913 case DATA_AREA_ZDA:
2914 kind = ((TREE_READONLY (decl))
2915 ? GHS_SECTION_KIND_ROZDATA
2916 : GHS_SECTION_KIND_ZDATA);
2917 break;
2918
2919 case DATA_AREA_NORMAL: /* default data area */
2920 if (TREE_READONLY (decl))
2921 kind = GHS_SECTION_KIND_RODATA;
2922 else if (DECL_INITIAL (decl))
2923 kind = GHS_SECTION_KIND_DATA;
2924 else
2925 kind = GHS_SECTION_KIND_BSS;
2926 }
2927 }
2928
2929 /* Now, if the section kind has been explicitly renamed,
2930 then attach a section attribute. */
2931 chosen_section = GHS_current_section_names [(int) kind];
2932
2933 /* Otherwise, if this kind of section needs an explicit section
2934 attribute, then also attach one. */
2935 if (chosen_section == NULL)
2936 chosen_section = GHS_default_section_names [(int) kind];
2937
2938 if (chosen_section)
2939 {
2940 /* Only set the section name if specified by a pragma, because
2941 otherwise it will force those variables to get allocated storage
2942 in this module, rather than by the linker. */
2943 DECL_SECTION_NAME (decl) = chosen_section;
2944 }
2945 }
2946 }
2947
2948 /* Return nonzero if the given RTX is suitable
2949 for collapsing into a DISPOSE instruction. */
2950
2951 int
2952 pattern_is_ok_for_dispose (op, mode)
2953 rtx op;
2954 enum machine_mode mode ATTRIBUTE_UNUSED;
2955 {
2956 int count = XVECLEN (op, 0);
2957 int i;
2958
2959 /* If there are no registers to restore then
2960 the dispose instruction is not suitable. */
2961 if (count <= 2)
2962 return 0;
2963
2964 /* The pattern matching has already established that we are performing a
2965 function epilogue and that we are popping at least one register. We must
2966 now check the remaining entries in the vector to make sure that they are
2967 also register pops. There is no good reason why there should ever be
2968 anything else in this vector, but being paranoid always helps...
2969
2970 The test below performs the C equivalent of this machine description
2971 pattern match:
2972
2973 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
2974 (mem:SI (plus:SI (reg:SI 3)
2975 (match_operand:SI n "immediate_operand" "i"))))
2976 */
2977
2978 for (i = 3; i < count; i++)
2979 {
2980 rtx vector_element = XVECEXP (op, 0, i);
2981 rtx dest;
2982 rtx src;
2983 rtx plus;
2984
2985 if (GET_CODE (vector_element) != SET)
2986 return 0;
2987
2988 dest = SET_DEST (vector_element);
2989 src = SET_SRC (vector_element);
2990
2991 if ( GET_CODE (dest) != REG
2992 || GET_MODE (dest) != SImode
2993 || ! register_is_ok_for_epilogue (dest, SImode)
2994 || GET_CODE (src) != MEM
2995 || GET_MODE (src) != SImode)
2996 return 0;
2997
2998 plus = XEXP (src, 0);
2999
3000 if ( GET_CODE (plus) != PLUS
3001 || GET_CODE (XEXP (plus, 0)) != REG
3002 || GET_MODE (XEXP (plus, 0)) != SImode
3003 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
3004 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
3005 return 0;
3006 }
3007
3008 return 1;
3009 }
3010
3011 /* Construct a DISPOSE instruction that is the equivalent of
3012 the given RTX. We have already verified that this should
3013 be possible. */
3014
3015 char *
3016 construct_dispose_instruction (op)
3017 rtx op;
3018 {
3019 int count = XVECLEN (op, 0);
3020 int stack_bytes;
3021 unsigned long int mask;
3022 int i;
3023 static char buff[ 100 ]; /* XXX */
3024 int use_callt = 0;
3025
3026 if (count <= 2)
3027 {
3028 error ("Bogus DISPOSE construction: %d\n", count);
3029 return NULL;
3030 }
3031
3032 /* Work out how many bytes to pop off the
3033 stack before retrieving registers. */
3034 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
3035 abort ();
3036 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
3037 abort ();
3038 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
3039 abort ();
3040
3041 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
3042
3043 /* Each pop will remove 4 bytes from the stack... */
3044 stack_bytes -= (count - 2) * 4;
3045
3046 /* Make sure that the amount we are popping
3047 will fit into the DISPOSE instruction. */
3048 if (stack_bytes > 128)
3049 {
3050 error ("Too much stack space to dispose of: %d", stack_bytes);
3051 return NULL;
3052 }
3053
3054 /* Now compute the bit mask of registers to push. */
3055 mask = 0;
3056
3057 for (i = 2; i < count; i++)
3058 {
3059 rtx vector_element = XVECEXP (op, 0, i);
3060
3061 if (GET_CODE (vector_element) != SET)
3062 abort ();
3063 if (GET_CODE (SET_DEST (vector_element)) != REG)
3064 abort ();
3065 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
3066 abort ();
3067
3068 if (REGNO (SET_DEST (vector_element)) == 2)
3069 use_callt = 1;
3070 else
3071 mask |= 1 << REGNO (SET_DEST (vector_element));
3072 }
3073
3074 if (! TARGET_DISABLE_CALLT
3075 && (use_callt || stack_bytes == 0 || stack_bytes == 16))
3076 {
3077 if (use_callt)
3078 {
3079 sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
3080 return buff;
3081 }
3082 else
3083 {
3084 for (i = 20; i < 32; i++)
3085 if (mask & (1 << i))
3086 break;
3087
3088 if (i == 31)
3089 sprintf (buff, "callt ctoff(__callt_return_r31c)");
3090 else
3091 sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
3092 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
3093 }
3094 }
3095 else
3096 {
3097 static char regs [100]; /* XXX */
3098 int done_one;
3099
3100 /* Generate the DISPOSE instruction. Note we could just issue the
3101 bit mask as a number as the assembler can cope with this, but for
3102 the sake of our readers we turn it into a textual description. */
3103 regs[0] = 0;
3104 done_one = 0;
3105
3106 for (i = 20; i < 32; i++)
3107 {
3108 if (mask & (1 << i))
3109 {
3110 int first;
3111
3112 if (done_one)
3113 strcat (regs, ", ");
3114 else
3115 done_one = 1;
3116
3117 first = i;
3118 strcat (regs, reg_names[ first ]);
3119
3120 for (i++; i < 32; i++)
3121 if ((mask & (1 << i)) == 0)
3122 break;
3123
3124 if (i > first + 1)
3125 {
3126 strcat (regs, " - ");
3127 strcat (regs, reg_names[ i - 1 ] );
3128 }
3129 }
3130 }
3131
3132 sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
3133 }
3134
3135 return buff;
3136 }
3137
3138 /* Return nonzero if the given RTX is suitable
3139 for collapsing into a PREPARE instruction. */
3140
3141 int
3142 pattern_is_ok_for_prepare (op, mode)
3143 rtx op;
3144 enum machine_mode mode ATTRIBUTE_UNUSED;
3145 {
3146 int count = XVECLEN (op, 0);
3147 int i;
3148
3149 /* If there are no registers to restore then the prepare instruction
3150 is not suitable. */
3151 if (count <= 1)
3152 return 0;
3153
3154 /* The pattern matching has already established that we are adjusting the
3155 stack and pushing at least one register. We must now check that the
3156 remaining entries in the vector to make sure that they are also register
3157 pushes.
3158
3159 The test below performs the C equivalent of this machine description
3160 pattern match:
3161
3162 (set (mem:SI (plus:SI (reg:SI 3)
3163 (match_operand:SI 2 "immediate_operand" "i")))
3164 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
3165
3166 */
3167
3168 for (i = 2; i < count; i++)
3169 {
3170 rtx vector_element = XVECEXP (op, 0, i);
3171 rtx dest;
3172 rtx src;
3173 rtx plus;
3174
3175 if (GET_CODE (vector_element) != SET)
3176 return 0;
3177
3178 dest = SET_DEST (vector_element);
3179 src = SET_SRC (vector_element);
3180
3181 if ( GET_CODE (dest) != MEM
3182 || GET_MODE (dest) != SImode
3183 || GET_CODE (src) != REG
3184 || GET_MODE (src) != SImode
3185 || ! register_is_ok_for_epilogue (src, SImode)
3186 )
3187 return 0;
3188
3189 plus = XEXP (dest, 0);
3190
3191 if ( GET_CODE (plus) != PLUS
3192 || GET_CODE (XEXP (plus, 0)) != REG
3193 || GET_MODE (XEXP (plus, 0)) != SImode
3194 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
3195 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
3196 return 0;
3197
3198 /* If the register is being pushed somewhere other than the stack
3199 space just aquired by the first operand then abandon this quest.
3200 Note: the test is <= becuase both values are negative. */
3201 if (INTVAL (XEXP (plus, 1))
3202 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
3203 return 0;
3204 }
3205
3206 return 1;
3207 }
3208
3209 /* Construct a PREPARE instruction that is the equivalent of
3210 the given RTL. We have already verified that this should
3211 be possible. */
3212
3213 char *
3214 construct_prepare_instruction (op)
3215 rtx op;
3216 {
3217 int count = XVECLEN (op, 0);
3218 int stack_bytes;
3219 unsigned long int mask;
3220 int i;
3221 static char buff[ 100 ]; /* XXX */
3222 int use_callt = 0;
3223
3224 if (count <= 1)
3225 {
3226 error ("Bogus PREPEARE construction: %d\n", count);
3227 return NULL;
3228 }
3229
3230 /* Work out how many bytes to push onto
3231 the stack after storing the registers. */
3232 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
3233 abort ();
3234 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
3235 abort ();
3236 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
3237 abort ();
3238
3239 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
3240
3241 /* Each push will put 4 bytes from the stack. */
3242 stack_bytes += (count - 1) * 4;
3243
3244 /* Make sure that the amount we are popping
3245 will fit into the DISPOSE instruction. */
3246 if (stack_bytes < -128)
3247 {
3248 error ("Too much stack space to prepare: %d", stack_bytes);
3249 return NULL;
3250 }
3251
3252 /* Now compute the bit mask of registers to push. */
3253 mask = 0;
3254 for (i = 1; i < count; i++)
3255 {
3256 rtx vector_element = XVECEXP (op, 0, i);
3257
3258 if (GET_CODE (vector_element) != SET)
3259 abort ();
3260 if (GET_CODE (SET_SRC (vector_element)) != REG)
3261 abort ();
3262 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
3263 abort ();
3264
3265 if (REGNO (SET_SRC (vector_element)) == 2)
3266 use_callt = 1;
3267 else
3268 mask |= 1 << REGNO (SET_SRC (vector_element));
3269 }
3270
3271 if ((! TARGET_DISABLE_CALLT)
3272 && (use_callt || stack_bytes == 0 || stack_bytes == -16))
3273 {
3274 if (use_callt)
3275 {
3276 sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
3277 return buff;
3278 }
3279
3280 for (i = 20; i < 32; i++)
3281 if (mask & (1 << i))
3282 break;
3283
3284 if (i == 31)
3285 sprintf (buff, "callt ctoff(__callt_save_r31c)");
3286 else
3287 sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
3288 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
3289 }
3290 else
3291 {
3292 static char regs [100]; /* XXX */
3293 int done_one;
3294
3295
3296 /* Generate the PREPARE instruction. Note we could just issue the
3297 bit mask as a number as the assembler can cope with this, but for
3298 the sake of our readers we turn it into a textual description. */
3299 regs[0] = 0;
3300 done_one = 0;
3301
3302 for (i = 20; i < 32; i++)
3303 {
3304 if (mask & (1 << i))
3305 {
3306 int first;
3307
3308 if (done_one)
3309 strcat (regs, ", ");
3310 else
3311 done_one = 1;
3312
3313 first = i;
3314 strcat (regs, reg_names[ first ]);
3315
3316 for (i++; i < 32; i++)
3317 if ((mask & (1 << i)) == 0)
3318 break;
3319
3320 if (i > first + 1)
3321 {
3322 strcat (regs, " - ");
3323 strcat (regs, reg_names[ i - 1 ] );
3324 }
3325 }
3326 }
3327
3328 sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
3329 }
3330
3331 return buff;
3332 }
3333 \f
3334 /* Implement `va_arg'. */
3335
3336 rtx
3337 v850_va_arg (valist, type)
3338 tree valist, type;
3339 {
3340 HOST_WIDE_INT size, rsize;
3341 tree addr, incr;
3342 rtx addr_rtx;
3343 int indirect;
3344
3345 /* Round up sizeof(type) to a word. */
3346 size = int_size_in_bytes (type);
3347 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
3348 indirect = 0;
3349
3350 if (size > 8)
3351 {
3352 size = rsize = UNITS_PER_WORD;
3353 indirect = 1;
3354 }
3355
3356 addr = save_expr (valist);
3357 incr = fold (build (PLUS_EXPR, ptr_type_node, addr,
3358 build_int_2 (rsize, 0)));
3359
3360 incr = build (MODIFY_EXPR, ptr_type_node, valist, incr);
3361 TREE_SIDE_EFFECTS (incr) = 1;
3362 expand_expr (incr, const0_rtx, VOIDmode, EXPAND_NORMAL);
3363
3364 addr_rtx = expand_expr (addr, NULL, Pmode, EXPAND_NORMAL);
3365
3366 if (indirect)
3367 {
3368 addr_rtx = force_reg (Pmode, addr_rtx);
3369 addr_rtx = gen_rtx_MEM (Pmode, addr_rtx);
3370 set_mem_alias_set (addr_rtx, get_varargs_alias_set ());
3371 }
3372
3373 return addr_rtx;
3374 }
3375 \f
3376 /* Return an RTX indicating where the return address to the
3377 calling function can be found. */
3378
3379 rtx
3380 v850_return_addr (count)
3381 int count;
3382 {
3383 if (count != 0)
3384 return const0_rtx;
3385
3386 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
3387 }
3388 \f
3389 static void
3390 v850_select_section (exp, reloc, align)
3391 tree exp;
3392 int reloc ATTRIBUTE_UNUSED;
3393 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
3394 {
3395 if (TREE_CODE (exp) == VAR_DECL)
3396 {
3397 int is_const;
3398 if (!TREE_READONLY (exp)
3399 || TREE_SIDE_EFFECTS (exp)
3400 || !DECL_INITIAL (exp)
3401 || (DECL_INITIAL (exp) != error_mark_node
3402 && !TREE_CONSTANT (DECL_INITIAL (exp))))
3403 is_const = FALSE;
3404 else
3405 is_const = TRUE;
3406
3407 switch (v850_get_data_area (exp))
3408 {
3409 case DATA_AREA_ZDA:
3410 if (is_const)
3411 rozdata_section ();
3412 else
3413 zdata_section ();
3414 break;
3415
3416 case DATA_AREA_TDA:
3417 tdata_section ();
3418 break;
3419
3420 case DATA_AREA_SDA:
3421 if (is_const)
3422 rosdata_section ();
3423 else
3424 sdata_section ();
3425 break;
3426
3427 default:
3428 if (is_const)
3429 readonly_data_section ();
3430 else
3431 data_section ();
3432 break;
3433 }
3434 }
3435 else if (TREE_CODE (exp) == STRING_CST)
3436 {
3437 if (! flag_writable_strings)
3438 readonly_data_section ();
3439 else
3440 data_section ();
3441 }
3442 else
3443 readonly_data_section ();
3444 }