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