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