Home | History | Annotate | Line # | Download | only in msp430
msp430.cc revision 1.1.1.1
      1 /* Subroutines used for code generation on TI MSP430 processors.
      2    Copyright (C) 2012-2022 Free Software Foundation, Inc.
      3    Contributed by Red Hat.
      4 
      5    This file is part of GCC.
      6 
      7    GCC is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3, or (at your option)
     10    any later version.
     11 
     12    GCC is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with GCC; see the file COPYING3.  If not see
     19    <http://www.gnu.org/licenses/>.  */
     20 
     21 #define IN_TARGET_CODE 1
     22 
     23 #include "config.h"
     24 #include "system.h"
     25 #include "coretypes.h"
     26 #include "backend.h"
     27 #include "target.h"
     28 #include "rtl.h"
     29 #include "tree.h"
     30 #include "stringpool.h"
     31 #include "attribs.h"
     32 #include "gimple-expr.h"
     33 #include "df.h"
     34 #include "memmodel.h"
     35 #include "tm_p.h"
     36 #include "regs.h"
     37 #include "emit-rtl.h"
     38 #include "varasm.h"
     39 #include "diagnostic-core.h"
     40 #include "fold-const.h"
     41 #include "stor-layout.h"
     42 #include "calls.h"
     43 #include "output.h"
     44 #include "explow.h"
     45 #include "expr.h"
     46 #include "langhooks.h"
     47 #include "builtins.h"
     48 #include "intl.h"
     49 #include "msp430-devices.h"
     50 #include "incpath.h"
     51 #include "prefix.h"
     52 #include "insn-config.h"
     53 #include "insn-attr.h"
     54 #include "recog.h"
     55 
     56 /* This file should be included last.  */
     57 #include "target-def.h"
     58 
     59 
     61 static void msp430_compute_frame_info (void);
     62 static bool msp430_use_16bit_hwmult (void);
     63 static bool msp430_use_32bit_hwmult (void);
     64 static bool use_helper_for_const_shift (machine_mode mode, HOST_WIDE_INT amt);
     65 
     66 
     67 
     69 /* Run-time Target Specification.  */
     70 
     71 bool msp430x = true;
     72 
     73 struct GTY(()) machine_function
     74 {
     75   /* If set, the rest of the fields have been computed.  */
     76   int computed;
     77   /* Which registers need to be saved in the pro/epilogue.  */
     78   int need_to_save[FIRST_PSEUDO_REGISTER];
     79 
     80   /* These fields describe the frame layout...  */
     81   /* arg pointer */
     82   /* 2/4 bytes for saved PC */
     83   int framesize_regs;
     84   /* frame pointer */
     85   int framesize_locals;
     86   int framesize_outgoing;
     87   /* stack pointer */
     88   int framesize;
     89 
     90   /* How much we adjust the stack when returning from an exception
     91      handler.  */
     92   rtx eh_stack_adjust;
     93 };
     94 
     95 /* This is our init_machine_status, as set in
     96    msp430_option_override.  */
     97 static struct machine_function *
     98 msp430_init_machine_status (void)
     99 {
    100   struct machine_function *m;
    101 
    102   m = ggc_cleared_alloc<machine_function> ();
    103 
    104   return m;
    105 }
    106 
    107 #undef  TARGET_OPTION_OVERRIDE
    108 #define TARGET_OPTION_OVERRIDE		msp430_option_override
    109 
    110 /* Generate a C preprocessor symbol based upon the MCU selected by the user.
    111    If a specific MCU has not been selected then return a generic symbol
    112    instead.  */
    113 
    114 const char *
    115 msp430_mcu_name (void)
    116 {
    117   if (target_mcu)
    118     {
    119       msp430_extract_mcu_data (target_mcu);
    120       unsigned int i;
    121       unsigned int start_upper;
    122       unsigned int end_upper;
    123       static char mcu_name[64];
    124 
    125       /* The 'i' in the device name symbol for msp430i* devices must be lower
    126 	 case, to match the expected symbol in msp430.h.  */
    127       if (startswith (target_mcu, "msp430i"))
    128 	{
    129 	  snprintf (mcu_name, sizeof (mcu_name) - 1, "__MSP430i%s__",
    130 		    target_mcu + 7);
    131 	  start_upper = 9;
    132 	}
    133       else
    134 	{
    135 	  snprintf (mcu_name, sizeof (mcu_name) - 1, "__%s__", target_mcu);
    136 	  start_upper = 2;
    137 	}
    138       end_upper = strlen (mcu_name) - 2;
    139       for (i = start_upper; i < end_upper; i++)
    140 	mcu_name[i] = TOUPPER (mcu_name[i]);
    141       return mcu_name;
    142     }
    143 
    144   return msp430x ? "__MSP430XGENERIC__" : "__MSP430GENERIC__";
    145 }
    146 
    147 static const char *
    148 hwmult_name (unsigned int val)
    149 {
    150   switch (val)
    151     {
    152     case 0: return "none";
    153     case 1: return "16-bit";
    154     case 2: return "16-bit";
    155     case 4: return "32-bit";
    156     case 8: return "32-bit (5xx)";
    157     default: gcc_unreachable ();
    158     }
    159 }
    160 
    161 static void
    162 msp430_option_override (void)
    163 {
    164   /* The MSP430 architecture can safely dereference a NULL pointer.  In fact,
    165      there are memory mapped registers there.  */
    166   flag_delete_null_pointer_checks = 0;
    167 
    168   init_machine_status = msp430_init_machine_status;
    169 
    170   msp430x = target_cpu >= MSP430_CPU_MSP430X_DEFAULT;
    171 
    172   if (target_mcu)
    173     {
    174       msp430_extract_mcu_data (target_mcu);
    175 
    176       if (extracted_mcu_data.name != NULL)
    177 	{
    178 	  bool xisa = extracted_mcu_data.revision >= 1;
    179 
    180 	  if (msp430_warn_mcu)
    181 	    {
    182 	      if (target_cpu != MSP430_CPU_MSP430X_DEFAULT && msp430x != xisa)
    183 		warning (0, "MCU %qs supports %s ISA but %<-mcpu%> option "
    184 			 "is set to %s",
    185 			 target_mcu, xisa ? "430X" : "430",
    186 			 msp430x ? "430X" : "430");
    187 
    188 	      if (extracted_mcu_data.hwmpy == 0
    189 		  && msp430_hwmult_type != MSP430_HWMULT_AUTO
    190 		  && msp430_hwmult_type != MSP430_HWMULT_NONE)
    191 		warning (0, "MCU %qs does not have hardware multiply "
    192 			 "support, but %<-mhwmult%> is set to %s",
    193 			 target_mcu,
    194 			 msp430_hwmult_type == MSP430_HWMULT_SMALL ? "16-bit"
    195 			 : msp430_hwmult_type == MSP430_HWMULT_LARGE
    196 			 ? "32-bit" : "f5series");
    197 	      else if (msp430_hwmult_type == MSP430_HWMULT_SMALL
    198 		       && extracted_mcu_data.hwmpy != 1
    199 		       && extracted_mcu_data.hwmpy != 2)
    200 		warning (0, "MCU %qs supports %s hardware multiply, "
    201 			 "but %<-mhwmult%> is set to 16-bit",
    202 			 target_mcu, hwmult_name (extracted_mcu_data.hwmpy));
    203 	      else if (msp430_hwmult_type == MSP430_HWMULT_LARGE
    204 		       && extracted_mcu_data.hwmpy != 4)
    205 		warning (0, "MCU %qs supports %s hardware multiply, "
    206 			 "but %<-mhwmult%> is set to 32-bit",
    207 			 target_mcu, hwmult_name (extracted_mcu_data.hwmpy));
    208 	      else if (msp430_hwmult_type == MSP430_HWMULT_F5SERIES
    209 		       && extracted_mcu_data.hwmpy != 8)
    210 		warning (0, "MCU %qs supports %s hardware multiply, "
    211 			 "but %<-mhwmult%> is set to f5series",
    212 			 target_mcu, hwmult_name (extracted_mcu_data.hwmpy));
    213 	    }
    214 	  /* Only override the default setting with the extracted ISA value if
    215 	     the user has not passed -mcpu=.  */
    216 	  if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
    217 	    msp430x = xisa;
    218 	}
    219       else
    220 	{
    221 	  if (msp430_hwmult_type == MSP430_HWMULT_AUTO)
    222 	    {
    223 	      if (msp430_warn_mcu)
    224 		{
    225 		  if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
    226 		    warning (0,
    227 			     "unrecognized MCU name %qs, assuming that it is "
    228 			     "just a MSP430X with no hardware multiply; "
    229 			     "use the %<-mcpu%> and %<-mhwmult%> options to "
    230 			     "set these explicitly",
    231 			     target_mcu);
    232 		  else
    233 		    warning (0,
    234 			     "unrecognized MCU name %qs, assuming that it "
    235 			     "has no hardware multiply; use the %<-mhwmult%> "
    236 			     "option to set this explicitly",
    237 			     target_mcu);
    238 		}
    239 
    240 	      msp430_hwmult_type = MSP430_HWMULT_NONE;
    241 	    }
    242 	  else if (target_cpu == MSP430_CPU_MSP430X_DEFAULT)
    243 	    {
    244 	      if (msp430_warn_mcu)
    245 		warning (0,
    246 			 "unrecognized MCU name %qs, assuming that it just "
    247 			 "supports the MSP430X ISA; use the %<-mcpu%> option "
    248 			 "to set the ISA explicitly",
    249 			 target_mcu);
    250 	    }
    251 	  else if (msp430_warn_mcu)
    252 	    warning (0, "Unrecognized MCU name %qs.", target_mcu);
    253 	}
    254     }
    255 
    256   if (TARGET_LARGE && !msp430x)
    257     error ("%<-mlarge%> requires a 430X-compatible %<-mmcu=%>");
    258 
    259   if (!TARGET_LARGE && msp430_code_region == MSP430_REGION_EITHER)
    260     error ("%<-mcode-region=either%> requires the large memory model "
    261 	   "(%<-mlarge%>)");
    262   else if (!TARGET_LARGE && msp430_code_region == MSP430_REGION_UPPER)
    263     error ("%<-mcode-region=upper%> requires the large memory model "
    264 	   "(%<-mlarge%>)");
    265 
    266   if (!TARGET_LARGE && msp430_data_region == MSP430_REGION_EITHER)
    267     error ("%<-mdata-region=either%> requires the large memory model "
    268 	   "(%<-mlarge%>)");
    269   else if (!TARGET_LARGE && msp430_data_region == MSP430_REGION_UPPER)
    270     error ("%<-mdata-region=upper%> requires the large memory model "
    271 	   "(%<-mlarge%>)");
    272 
    273   if (flag_exceptions || flag_non_call_exceptions
    274       || flag_unwind_tables || flag_asynchronous_unwind_tables)
    275     flag_omit_frame_pointer = false;
    276   else
    277     flag_omit_frame_pointer = true;
    278 
    279   /* This is a hack to work around a problem with the newlib build
    280      mechanism.  Newlib always appends CFLAGS to the end of the GCC
    281      command line and always sets -O2 in CFLAGS.  Thus it is not
    282      possible to build newlib with -Os enabled.  Until now...  */
    283   if (TARGET_OPT_SPACE && optimize < 3)
    284     optimize_size = 1;
    285 
    286 #if !DEFAULT_USE_CXA_ATEXIT
    287   /* For some configurations, we use atexit () instead of __cxa_atexit () by
    288      default to save on code size and remove the declaration of __dso_handle
    289      from the CRT library.
    290      Configuring GCC with --enable-__cxa-atexit re-enables it by defining
    291      DEFAULT_USE_CXA_ATEXIT to 1.  */
    292   if (flag_use_cxa_atexit)
    293     error ("%<-fuse-cxa-atexit%> is not supported for msp430-elf");
    294 #endif
    295 
    296 #ifndef HAVE_NEWLIB_NANO_FORMATTED_IO
    297   if (TARGET_TINY_PRINTF)
    298     error ("GCC must be configured with %<--enable-newlib-nano-formatted-io%> "
    299 	   "to use %<-mtiny-printf%>");
    300 #endif
    301 }
    302 
    303 #undef  TARGET_SCALAR_MODE_SUPPORTED_P
    304 #define TARGET_SCALAR_MODE_SUPPORTED_P msp430_scalar_mode_supported_p
    305 
    306 static bool
    307 msp430_scalar_mode_supported_p (scalar_mode m)
    308 {
    309   if (m == PSImode && msp430x)
    310     return true;
    311 #if 0
    312   if (m == TImode)
    313     return true;
    314 #endif
    315   return default_scalar_mode_supported_p (m);
    316 }
    317 
    318 
    319 
    321 /* Storage Layout */
    322 
    323 #undef  TARGET_MS_BITFIELD_LAYOUT_P
    324 #define TARGET_MS_BITFIELD_LAYOUT_P msp430_ms_bitfield_layout_p
    325 
    326 bool
    327 msp430_ms_bitfield_layout_p (const_tree record_type ATTRIBUTE_UNUSED)
    328 {
    329   return false;
    330 }
    331 
    332 
    333 
    335 /* Register Usage */
    336 
    337 #undef TARGET_HARD_REGNO_NREGS
    338 #define TARGET_HARD_REGNO_NREGS msp430_hard_regno_nregs
    339 
    340 static unsigned int
    341 msp430_hard_regno_nregs (unsigned int, machine_mode mode)
    342 {
    343   if (mode == PSImode && msp430x)
    344     return 1;
    345   if (mode == CPSImode && msp430x)
    346     return 2;
    347   return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
    348 	  / UNITS_PER_WORD);
    349 }
    350 
    351 /* subreg_get_info correctly handles PSImode registers, so defining
    352    HARD_REGNO_NREGS_HAS_PADDING and HARD_REGNO_NREGS_WITH_PADDING
    353    has no effect.  */
    354 
    355 #undef TARGET_HARD_REGNO_MODE_OK
    356 #define TARGET_HARD_REGNO_MODE_OK msp430_hard_regno_mode_ok
    357 
    358 static bool
    359 msp430_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
    360 {
    361   return regno <= (ARG_POINTER_REGNUM
    362 		   - (unsigned int) msp430_hard_regno_nregs (regno, mode));
    363 }
    364 
    365 #undef TARGET_MODES_TIEABLE_P
    366 #define TARGET_MODES_TIEABLE_P msp430_modes_tieable_p
    367 
    368 static bool
    369 msp430_modes_tieable_p (machine_mode mode1, machine_mode mode2)
    370 {
    371   if ((mode1 == PSImode || mode2 == SImode)
    372       || (mode1 == SImode || mode2 == PSImode))
    373     return false;
    374 
    375   return ((GET_MODE_CLASS (mode1) == MODE_FLOAT
    376 	   || GET_MODE_CLASS (mode1) == MODE_COMPLEX_FLOAT)
    377 	  == (GET_MODE_CLASS (mode2) == MODE_FLOAT
    378 	      || GET_MODE_CLASS (mode2) == MODE_COMPLEX_FLOAT));
    379 }
    380 
    381 #undef  TARGET_FRAME_POINTER_REQUIRED
    382 #define TARGET_FRAME_POINTER_REQUIRED msp430_frame_pointer_required
    383 
    384 static bool
    385 msp430_frame_pointer_required (void)
    386 {
    387   return false;
    388 }
    389 
    390 #undef  TARGET_CAN_ELIMINATE
    391 #define TARGET_CAN_ELIMINATE		msp430_can_eliminate
    392 
    393 static bool
    394 msp430_can_eliminate (const int from_reg ATTRIBUTE_UNUSED,
    395 		      const int to_reg ATTRIBUTE_UNUSED)
    396 {
    397   return true;
    398 }
    399 
    400 /* Implements INITIAL_ELIMINATION_OFFSET.  */
    401 int
    402 msp430_initial_elimination_offset (int from, int to)
    403 {
    404   int rv = 0; /* As if arg to arg.  */
    405 
    406   msp430_compute_frame_info ();
    407 
    408   switch (to)
    409     {
    410     case STACK_POINTER_REGNUM:
    411       rv += cfun->machine->framesize_outgoing;
    412       rv += cfun->machine->framesize_locals;
    413       /* Fall through.  */
    414     case FRAME_POINTER_REGNUM:
    415       rv += cfun->machine->framesize_regs;
    416       /* Allow for the saved return address.  */
    417       rv += (TARGET_LARGE ? 4 : 2);
    418       /* NB/ No need to allow for crtl->args.pretend_args_size.
    419 	 GCC does that for us.  */
    420       break;
    421     default:
    422       gcc_unreachable ();
    423     }
    424 
    425   switch (from)
    426     {
    427     case FRAME_POINTER_REGNUM:
    428       /* Allow for the fall through above.  */
    429       rv -= (TARGET_LARGE ? 4 : 2);
    430       rv -= cfun->machine->framesize_regs;
    431     case ARG_POINTER_REGNUM:
    432       break;
    433     default:
    434       gcc_unreachable ();
    435     }
    436 
    437   return rv;
    438 }
    439 
    440 /* Named Address Space support */
    442 
    443 
    444 /* Return the appropriate mode for a named address pointer.  */
    445 #undef  TARGET_ADDR_SPACE_POINTER_MODE
    446 #define TARGET_ADDR_SPACE_POINTER_MODE msp430_addr_space_pointer_mode
    447 #undef  TARGET_ADDR_SPACE_ADDRESS_MODE
    448 #define TARGET_ADDR_SPACE_ADDRESS_MODE msp430_addr_space_pointer_mode
    449 
    450 static scalar_int_mode
    451 msp430_addr_space_pointer_mode (addr_space_t addrspace)
    452 {
    453   switch (addrspace)
    454     {
    455     default:
    456     case ADDR_SPACE_GENERIC:
    457       return Pmode;
    458     case ADDR_SPACE_NEAR:
    459       return HImode;
    460     case ADDR_SPACE_FAR:
    461       return PSImode;
    462     }
    463 }
    464 
    465 /* Function pointers are stored in unwind_word sized
    466    variables, so make sure that unwind_word is big enough.  */
    467 #undef  TARGET_UNWIND_WORD_MODE
    468 #define TARGET_UNWIND_WORD_MODE msp430_unwind_word_mode
    469 
    470 static scalar_int_mode
    471 msp430_unwind_word_mode (void)
    472 {
    473   /* This needs to match msp430_init_dwarf_reg_sizes_extra (below).  */
    474   return msp430x ? PSImode : HImode;
    475 }
    476 
    477 /* Determine if one named address space is a subset of another.  */
    478 #undef  TARGET_ADDR_SPACE_SUBSET_P
    479 #define TARGET_ADDR_SPACE_SUBSET_P msp430_addr_space_subset_p
    480 static bool
    481 msp430_addr_space_subset_p (addr_space_t subset, addr_space_t superset)
    482 {
    483   if (subset == superset)
    484     return true;
    485   else
    486     return (subset != ADDR_SPACE_FAR && superset == ADDR_SPACE_FAR);
    487 }
    488 
    489 #undef  TARGET_ADDR_SPACE_CONVERT
    490 #define TARGET_ADDR_SPACE_CONVERT msp430_addr_space_convert
    491 /* Convert from one address space to another.  */
    492 static rtx
    493 msp430_addr_space_convert (rtx op, tree from_type, tree to_type)
    494 {
    495   addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (from_type));
    496   addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (to_type));
    497   rtx result;
    498 
    499   if (to_as != ADDR_SPACE_FAR && from_as == ADDR_SPACE_FAR)
    500     {
    501       /* This is unpredictable, as we're truncating off usable address
    502 	 bits.  */
    503 
    504       if (CONSTANT_P (op))
    505 	return gen_rtx_CONST (HImode, op);
    506 
    507       result = gen_reg_rtx (HImode);
    508       emit_insn (gen_truncpsihi2 (result, op));
    509       return result;
    510     }
    511   else if (to_as == ADDR_SPACE_FAR && from_as != ADDR_SPACE_FAR)
    512     {
    513       /* This always works.  */
    514 
    515       if (CONSTANT_P (op))
    516 	return gen_rtx_CONST (PSImode, op);
    517 
    518       result = gen_reg_rtx (PSImode);
    519       emit_insn (gen_zero_extendhipsi2 (result, op));
    520       return result;
    521     }
    522   else
    523     gcc_unreachable ();
    524 }
    525 
    526 /* Stack Layout and Calling Conventions.  */
    528 
    529 /* For each function, we list the gcc version and the TI version on
    530    each line, where we're converting the function names.  */
    531 static char const * const special_convention_function_names[] =
    532 {
    533   "__muldi3", "__mspabi_mpyll",
    534   "__udivdi3", "__mspabi_divull",
    535   "__umoddi3", "__mspabi_remull",
    536   "__divdi3", "__mspabi_divlli",
    537   "__moddi3", "__mspabi_remlli",
    538   "__mspabi_srall",
    539   "__mspabi_srlll",
    540   "__mspabi_sllll",
    541   "__adddf3", "__mspabi_addd",
    542   "__subdf3", "__mspabi_subd",
    543   "__muldf3", "__mspabi_mpyd",
    544   "__divdf3", "__mspabi_divd",
    545   "__mspabi_cmpd",
    546   NULL
    547 };
    548 
    549 /* TRUE if the function passed is a "speical" function.  Special
    550    functions pass two DImode parameters in registers.  */
    551 static bool
    552 msp430_special_register_convention_p (const char *name)
    553 {
    554   int i;
    555 
    556   for (i = 0; special_convention_function_names[i]; i++)
    557     if (!strcmp (name, special_convention_function_names[i]))
    558       return true;
    559 
    560   return false;
    561 }
    562 
    563 #undef  TARGET_FUNCTION_VALUE_REGNO_P
    564 #define TARGET_FUNCTION_VALUE_REGNO_P msp430_function_value_regno_p
    565 
    566 bool
    567 msp430_function_value_regno_p (unsigned int regno)
    568 {
    569   return regno == 12;
    570 }
    571 
    572 
    573 #undef  TARGET_FUNCTION_VALUE
    574 #define TARGET_FUNCTION_VALUE msp430_function_value
    575 
    576 rtx
    577 msp430_function_value (const_tree ret_type,
    578 		       const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
    579 		       bool outgoing ATTRIBUTE_UNUSED)
    580 {
    581   return gen_rtx_REG (TYPE_MODE (ret_type), 12);
    582 }
    583 
    584 #undef  TARGET_LIBCALL_VALUE
    585 #define TARGET_LIBCALL_VALUE msp430_libcall_value
    586 
    587 rtx
    588 msp430_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
    589 {
    590   return gen_rtx_REG (mode, 12);
    591 }
    592 
    593 /* Implements INIT_CUMULATIVE_ARGS.  */
    594 void
    595 msp430_init_cumulative_args (CUMULATIVE_ARGS *ca,
    596 			     tree fntype ATTRIBUTE_UNUSED,
    597 			     rtx libname ATTRIBUTE_UNUSED,
    598 			     tree fndecl ATTRIBUTE_UNUSED,
    599 			     int n_named_args ATTRIBUTE_UNUSED)
    600 {
    601   const char *fname;
    602   memset (ca, 0, sizeof(*ca));
    603 
    604   ca->can_split = 1;
    605 
    606   if (fndecl)
    607     fname = IDENTIFIER_POINTER (DECL_NAME (fndecl));
    608   else if (libname)
    609     fname = XSTR (libname, 0);
    610   else
    611     fname = NULL;
    612 
    613   if (fname && msp430_special_register_convention_p (fname))
    614     ca->special_p = 1;
    615 }
    616 
    617 /* Helper function for argument passing; this function is the common
    618    code that determines where an argument will be passed.  */
    619 static void
    620 msp430_evaluate_arg (cumulative_args_t cap,
    621 		     machine_mode mode,
    622 		     const_tree type ATTRIBUTE_UNUSED,
    623 		     bool named)
    624 {
    625   CUMULATIVE_ARGS *ca = get_cumulative_args (cap);
    626   int nregs = GET_MODE_SIZE (mode);
    627   int i;
    628 
    629   ca->reg_count = 0;
    630   ca->mem_count = 0;
    631 
    632   if (!named)
    633     return;
    634 
    635   if (mode == PSImode)
    636     nregs = 1;
    637   else
    638     nregs = (nregs + 1) / 2;
    639 
    640   if (ca->special_p)
    641     {
    642       /* Function is passed two DImode operands, in R8:R11 and
    643 	 R12:15.  */
    644       ca->start_reg = 8;
    645       ca->reg_count = 4;
    646       return;
    647     }
    648 
    649   switch (nregs)
    650     {
    651     case 1:
    652       for (i = 0; i < 4; i++)
    653 	if (!ca->reg_used[i])
    654 	  {
    655 	    ca->reg_count = 1;
    656 	    ca->start_reg = CA_FIRST_REG + i;
    657 	    return;
    658 	  }
    659       break;
    660     case 2:
    661       for (i = 0; i < 3; i++)
    662 	if (!ca->reg_used[i] && !ca->reg_used[i + 1])
    663 	  {
    664 	    ca->reg_count = 2;
    665 	    ca->start_reg = CA_FIRST_REG + i;
    666 	    return;
    667 	  }
    668       if (!ca->reg_used[3] && ca->can_split)
    669 	{
    670 	  ca->reg_count = 1;
    671 	  ca->mem_count = 2;
    672 	  ca->start_reg = CA_FIRST_REG + 3;
    673 	  return;
    674 	}
    675       break;
    676     case 3:
    677     case 4:
    678       ca->can_split = 0;
    679       if (!ca->reg_used[0]
    680 	  && !ca->reg_used[1]
    681 	  && !ca->reg_used[2]
    682 	  && !ca->reg_used[3])
    683 	{
    684 	  ca->reg_count = 4;
    685 	  ca->start_reg = CA_FIRST_REG;
    686 	  return;
    687 	}
    688       break;
    689     }
    690 }
    691 
    692 #undef  TARGET_PROMOTE_PROTOTYPES
    693 #define TARGET_PROMOTE_PROTOTYPES msp430_promote_prototypes
    694 
    695 bool
    696 msp430_promote_prototypes (const_tree fntype ATTRIBUTE_UNUSED)
    697 {
    698   return false;
    699 }
    700 
    701 #undef  TARGET_FUNCTION_ARG
    702 #define TARGET_FUNCTION_ARG msp430_function_arg
    703 
    704 rtx
    705 msp430_function_arg (cumulative_args_t cap,
    706 		     const function_arg_info &arg)
    707 {
    708   CUMULATIVE_ARGS *ca = get_cumulative_args (cap);
    709 
    710   msp430_evaluate_arg (cap, arg.mode, arg.type, arg.named);
    711 
    712   if (ca->reg_count)
    713     return gen_rtx_REG (arg.mode, ca->start_reg);
    714 
    715   return 0;
    716 }
    717 
    718 #undef  TARGET_ARG_PARTIAL_BYTES
    719 #define TARGET_ARG_PARTIAL_BYTES msp430_arg_partial_bytes
    720 
    721 int
    722 msp430_arg_partial_bytes (cumulative_args_t cap, const function_arg_info &arg)
    723 {
    724   CUMULATIVE_ARGS *ca = get_cumulative_args (cap);
    725 
    726   msp430_evaluate_arg (cap, arg.mode, arg.type, arg.named);
    727 
    728   if (ca->reg_count && ca->mem_count)
    729     return ca->reg_count * UNITS_PER_WORD;
    730 
    731   return 0;
    732 }
    733 
    734 #undef  TARGET_PASS_BY_REFERENCE
    735 #define TARGET_PASS_BY_REFERENCE msp430_pass_by_reference
    736 
    737 static bool
    738 msp430_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
    739 {
    740   return (arg.mode == BLKmode
    741 	  || (arg.type && TREE_CODE (arg.type) == RECORD_TYPE)
    742 	  || (arg.type && TREE_CODE (arg.type) == UNION_TYPE));
    743 }
    744 
    745 #undef  TARGET_CALLEE_COPIES
    746 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_arg_info_true
    747 
    748 #undef  TARGET_FUNCTION_ARG_ADVANCE
    749 #define TARGET_FUNCTION_ARG_ADVANCE msp430_function_arg_advance
    750 
    751 void
    752 msp430_function_arg_advance (cumulative_args_t cap,
    753 			     const function_arg_info &arg)
    754 {
    755   CUMULATIVE_ARGS *ca = get_cumulative_args (cap);
    756   int i;
    757 
    758   msp430_evaluate_arg (cap, arg.mode, arg.type, arg.named);
    759 
    760   if (ca->start_reg >= CA_FIRST_REG)
    761     for (i = 0; i < ca->reg_count; i ++)
    762       ca->reg_used[i + ca->start_reg - CA_FIRST_REG] = 1;
    763 
    764   ca->special_p = 0;
    765 }
    766 
    767 #undef  TARGET_FUNCTION_ARG_BOUNDARY
    768 #define TARGET_FUNCTION_ARG_BOUNDARY msp430_function_arg_boundary
    769 
    770 static unsigned int
    771 msp430_function_arg_boundary (machine_mode mode, const_tree type)
    772 {
    773   if (mode == BLKmode
    774       && int_size_in_bytes (type) > 1)
    775     return 16;
    776   if (GET_MODE_BITSIZE (mode) > 8)
    777     return 16;
    778   return 8;
    779 }
    780 
    781 #undef  TARGET_RETURN_IN_MEMORY
    782 #define TARGET_RETURN_IN_MEMORY msp430_return_in_memory
    783 
    784 static bool
    785 msp430_return_in_memory (const_tree ret_type,
    786 			 const_tree fntype ATTRIBUTE_UNUSED)
    787 {
    788   machine_mode mode = TYPE_MODE (ret_type);
    789 
    790   if (mode == BLKmode
    791       || (fntype && TREE_CODE (TREE_TYPE (fntype)) == RECORD_TYPE)
    792       || (fntype && TREE_CODE (TREE_TYPE (fntype)) == UNION_TYPE))
    793     return true;
    794 
    795   if (GET_MODE_SIZE (mode) > 8)
    796     return true;
    797 
    798   return false;
    799 }
    800 
    801 #undef  TARGET_GET_RAW_ARG_MODE
    802 #define TARGET_GET_RAW_ARG_MODE msp430_get_raw_arg_mode
    803 
    804 static fixed_size_mode
    805 msp430_get_raw_arg_mode (int regno)
    806 {
    807   return as_a <fixed_size_mode> (regno == ARG_POINTER_REGNUM
    808 				 ? VOIDmode : Pmode);
    809 }
    810 
    811 #undef  TARGET_GET_RAW_RESULT_MODE
    812 #define TARGET_GET_RAW_RESULT_MODE msp430_get_raw_result_mode
    813 
    814 static fixed_size_mode
    815 msp430_get_raw_result_mode (int regno ATTRIBUTE_UNUSED)
    816 {
    817   return Pmode;
    818 }
    819 
    820 #undef  TARGET_GIMPLIFY_VA_ARG_EXPR
    821 #define TARGET_GIMPLIFY_VA_ARG_EXPR msp430_gimplify_va_arg_expr
    822 
    823 #include "gimplify.h"
    824 
    825 static tree
    826 msp430_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
    827 			     gimple_seq *post_p)
    828 {
    829   tree addr, t, type_size, rounded_size, valist_tmp;
    830   unsigned HOST_WIDE_INT align, boundary;
    831   bool indirect;
    832 
    833   indirect = pass_va_arg_by_reference (type);
    834   if (indirect)
    835     type = build_pointer_type (type);
    836 
    837   align = PARM_BOUNDARY / BITS_PER_UNIT;
    838   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
    839 
    840   /* When we align parameter on stack for caller, if the parameter
    841      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
    842      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
    843      here with caller.  */
    844   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
    845     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
    846 
    847   boundary /= BITS_PER_UNIT;
    848 
    849   /* Hoist the valist value into a temporary for the moment.  */
    850   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
    851 
    852   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
    853      requires greater alignment, we must perform dynamic alignment.  */
    854   if (boundary > align
    855       && !integer_zerop (TYPE_SIZE (type)))
    856     {
    857       /* FIXME: This is where this function diverts from targhooks.cc:
    858 	 std_gimplify_va_arg_expr().  It works, but I do not know why...  */
    859       if (! POINTER_TYPE_P (type))
    860 	{
    861 	  t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
    862 		      fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
    863 	  gimplify_and_add (t, pre_p);
    864 
    865 	  t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
    866 		      fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
    867 				   valist_tmp,
    868 				   build_int_cst (TREE_TYPE (valist),
    869 						  -boundary)));
    870 	  gimplify_and_add (t, pre_p);
    871 	}
    872     }
    873   else
    874     boundary = align;
    875 
    876   /* If the actual alignment is less than the alignment of the type,
    877      adjust the type accordingly so that we don't assume strict alignment
    878      when dereferencing the pointer.  */
    879   boundary *= BITS_PER_UNIT;
    880   if (boundary < TYPE_ALIGN (type))
    881     {
    882       type = build_variant_type_copy (type);
    883       SET_TYPE_ALIGN (type, boundary);
    884     }
    885 
    886   /* Compute the rounded size of the type.  */
    887   type_size = size_in_bytes (type);
    888   rounded_size = round_up (type_size, align);
    889 
    890   /* Reduce rounded_size so it's sharable with the postqueue.  */
    891   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
    892 
    893   /* Get AP.  */
    894   addr = valist_tmp;
    895 
    896   /* Compute new value for AP.  */
    897   t = fold_build_pointer_plus (valist_tmp, rounded_size);
    898   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
    899   gimplify_and_add (t, pre_p);
    900 
    901   addr = fold_convert (build_pointer_type (type), addr);
    902 
    903   if (indirect)
    904     addr = build_va_arg_indirect_ref (addr);
    905 
    906   addr = build_va_arg_indirect_ref (addr);
    907 
    908   return addr;
    909 }
    910 
    911 #undef TARGET_LRA_P
    913 #define TARGET_LRA_P hook_bool_void_false
    914 
    915 /* Addressing Modes */
    916 
    917 #undef  TARGET_LEGITIMATE_ADDRESS_P
    918 #define TARGET_LEGITIMATE_ADDRESS_P msp430_legitimate_address_p
    919 
    920 static bool
    921 reg_ok_for_addr (rtx r, bool strict)
    922 {
    923   int rn = REGNO (r);
    924 
    925   if (strict && rn >= FIRST_PSEUDO_REGISTER)
    926     rn = reg_renumber[rn];
    927   if (strict && 0 <= rn && rn < FIRST_PSEUDO_REGISTER)
    928     return true;
    929   if (!strict)
    930     return true;
    931   return false;
    932 }
    933 
    934 bool
    935 msp430_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED,
    936 			     rtx x ATTRIBUTE_UNUSED,
    937 			     bool strict ATTRIBUTE_UNUSED)
    938 {
    939   switch (GET_CODE (x))
    940     {
    941     case MEM:
    942       return false;
    943 
    944     case PLUS:
    945     case POST_INC:
    946       if (REG_P (XEXP (x, 0)))
    947 	{
    948 	  if (GET_MODE (x) != GET_MODE (XEXP (x, 0)))
    949 	    return false;
    950 	  if (!reg_ok_for_addr (XEXP (x, 0), strict))
    951 	    return false;
    952 	  if (GET_CODE (x) == POST_INC)
    953 	    /* At this point, if the original rtx was a post_inc, we don't have
    954 	       anything further to check.  */
    955 	    return true;
    956 	  switch (GET_CODE (XEXP (x, 1)))
    957 	    {
    958 	    case CONST:
    959 	    case SYMBOL_REF:
    960 	    case CONST_INT:
    961 	      return true;
    962 	    default:
    963 	      return false;
    964 	    }
    965 	}
    966       return false;
    967 
    968     case REG:
    969       if (!reg_ok_for_addr (x, strict))
    970 	return false;
    971       /* FALLTHRU */
    972     case CONST:
    973     case SYMBOL_REF:
    974     case CONST_INT:
    975       return true;
    976 
    977     default:
    978       return false;
    979     }
    980 }
    981 
    982 #undef  TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
    983 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P \
    984   msp430_addr_space_legitimate_address_p
    985 
    986 bool
    987 msp430_addr_space_legitimate_address_p (machine_mode mode,
    988 					rtx x,
    989 					bool strict,
    990 					addr_space_t as ATTRIBUTE_UNUSED)
    991 {
    992   return msp430_legitimate_address_p (mode, x, strict);
    993 }
    994 
    995 #undef  TARGET_ASM_INTEGER
    996 #define TARGET_ASM_INTEGER msp430_asm_integer
    997 static bool
    998 msp430_asm_integer (rtx x, unsigned int size, int aligned_p)
    999 {
   1000   int c = GET_CODE (x);
   1001 
   1002   if (size == 3 && GET_MODE (x) == PSImode)
   1003     size = 4;
   1004 
   1005   switch (size)
   1006     {
   1007     case 4:
   1008       if (c == SYMBOL_REF || c == CONST || c == LABEL_REF || c == CONST_INT
   1009 	  || c == PLUS || c == MINUS)
   1010 	{
   1011 	  fprintf (asm_out_file, "\t.long\t");
   1012 	  output_addr_const (asm_out_file, x);
   1013 	  fputc ('\n', asm_out_file);
   1014 	  return true;
   1015 	}
   1016       break;
   1017     }
   1018   return default_assemble_integer (x, size, aligned_p);
   1019 }
   1020 
   1021 #undef  TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
   1022 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA msp430_asm_output_addr_const_extra
   1023 static bool
   1024 msp430_asm_output_addr_const_extra (FILE *file ATTRIBUTE_UNUSED, rtx x)
   1025 {
   1026   debug_rtx (x);
   1027   return false;
   1028 }
   1029 
   1030 #undef  TARGET_LEGITIMATE_CONSTANT_P
   1031 #define TARGET_LEGITIMATE_CONSTANT_P msp430_legitimate_constant
   1032 
   1033 static bool
   1034 msp430_legitimate_constant (machine_mode mode, rtx x)
   1035 {
   1036   return ! CONST_INT_P (x)
   1037     || mode != PSImode
   1038     /* GCC does not know the width of the PSImode, so make
   1039        sure that it does not try to use a constant value that
   1040        is out of range.  */
   1041     || (INTVAL (x) < (1 << 20)
   1042 	&& INTVAL (x) >= (HOST_WIDE_INT)(HOST_WIDE_INT_M1U << 20));
   1043 }
   1044 
   1045 
   1046 /* Describing Relative Costs of Operations
   1048    To model the cost of an instruction, use the number of cycles when
   1049    optimizing for speed, and the number of words when optimizing for size.
   1050    The cheapest instruction will execute in one cycle and cost one word.
   1051    The cycle and size costs correspond to 430 ISA instructions, not 430X
   1052    instructions or 430X "address" instructions.  The relative costs of 430X
   1053    instructions is accurately modeled with the 430 costs.  The relative costs
   1054    of some "address" instructions can differ, but these are not yet handled.
   1055    Adding support for this could improve performance/code size.  */
   1056 
   1057 struct single_op_cost
   1058 {
   1059   const int reg;
   1060   /* Indirect register (@Rn) or indirect autoincrement (@Rn+).  */
   1061   const int ind;
   1062   const int mem;
   1063 };
   1064 
   1065 static const struct single_op_cost cycle_cost_single_op =
   1066 {
   1067   1, 3, 4
   1068 };
   1069 
   1070 static const struct single_op_cost size_cost_single_op =
   1071 {
   1072   1, 1, 2
   1073 };
   1074 
   1075 /* When the destination of an insn is memory, the cost is always the same
   1076    regardless of whether that memory is accessed using indirect register,
   1077    indexed or absolute addressing.
   1078    When the source operand is memory, indirect register and post-increment have
   1079    the same cost, which is lower than indexed and absolute, which also have
   1080    the same cost.  */
   1081 struct double_op_cost
   1082 {
   1083   /* Source operand is a register.  */
   1084   const int r2r;
   1085   const int r2pc;
   1086   const int r2m;
   1087 
   1088   /* Source operand is memory, using indirect register (@Rn) or indirect
   1089      autoincrement (@Rn+) addressing modes.  */
   1090   const int ind2r;
   1091   const int ind2pc;
   1092   const int ind2m;
   1093 
   1094   /* Source operand is an immediate.  */
   1095   const int imm2r;
   1096   const int imm2pc;
   1097   const int imm2m;
   1098 
   1099   /* Source operand is memory, using indexed (x(Rn)) or absolute (&ADDR)
   1100      addressing modes.  */
   1101   const int mem2r;
   1102   const int mem2pc;
   1103   const int mem2m;
   1104 };
   1105 
   1106 /* These structures describe the cost of MOV, BIT and CMP instructions, in terms
   1107    of clock cycles or words.  */
   1108 static const struct double_op_cost cycle_cost_double_op_mov =
   1109 {
   1110   1, 3, 3,
   1111   2, 4, 4,
   1112   2, 3, 4,
   1113   3, 5, 5
   1114 };
   1115 
   1116 /* Cycle count when memory is the destination operand is one larger than above
   1117    for instructions that aren't MOV, BIT or CMP.  */
   1118 static const struct double_op_cost cycle_cost_double_op =
   1119 {
   1120   1, 3, 4,
   1121   2, 4, 5,
   1122   2, 3, 5,
   1123   3, 5, 6
   1124 };
   1125 
   1126 static const struct double_op_cost size_cost_double_op =
   1127 {
   1128   1, 1, 2,
   1129   1, 1, 2,
   1130   2, 2, 3,
   1131   2, 2, 3
   1132 };
   1133 
   1134 struct msp430_multlib_costs
   1135 {
   1136   const int mulhi;
   1137   const int mulsi;
   1138   const int muldi;
   1139 };
   1140 
   1141 /* There is no precise size cost when using libcalls, instead it is disparaged
   1142    relative to other instructions.
   1143    The cycle costs are from the CALL to the RET, inclusive.
   1144    FIXME muldi cost is not accurate.  */
   1145 static const struct msp430_multlib_costs cycle_cost_multlib_32bit =
   1146 {
   1147   27, 33, 66
   1148 };
   1149 
   1150 /* 32bit multiply takes a few more instructions on 16bit hwmult.  */
   1151 static const struct msp430_multlib_costs cycle_cost_multlib_16bit =
   1152 {
   1153   27, 42, 66
   1154 };
   1155 
   1156 /* TARGET_REGISTER_MOVE_COST
   1157    There is only one class of general-purpose, non-fixed registers, and the
   1158    relative cost of moving data between them is always the same.
   1159    Therefore, the default of 2 is optimal.  */
   1160 
   1161 #undef TARGET_MEMORY_MOVE_COST
   1162 #define TARGET_MEMORY_MOVE_COST msp430_memory_move_cost
   1163 
   1164 /* Return the cost of moving data between registers and memory.
   1165    The returned cost must be relative to the default TARGET_REGISTER_MOVE_COST
   1166    of 2.
   1167    IN is false if the value is to be written to memory.  */
   1168 static int
   1169 msp430_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
   1170 			 reg_class_t rclass ATTRIBUTE_UNUSED,
   1171 			 bool in)
   1172 {
   1173   int cost;
   1174   const struct double_op_cost *cost_p;
   1175   /* Optimize with a code size focus by default, unless -O2 or above is
   1176      specified.  */
   1177   bool speed = (!optimize_size && optimize >= 2);
   1178 
   1179   cost_p = (speed ? &cycle_cost_double_op_mov : &size_cost_double_op);
   1180 
   1181   if (in)
   1182     /* Reading from memory using indirect addressing is assumed to be the more
   1183        common case.  */
   1184     cost = cost_p->ind2r;
   1185   else
   1186     cost = cost_p->r2m;
   1187 
   1188   /* All register to register moves cost 1 cycle or 1 word, so multiply by 2
   1189      to get the costs relative to TARGET_REGISTER_MOVE_COST of 2.  */
   1190   return 2 * cost;
   1191 }
   1192 
   1193 /* For X, which must be a MEM RTX, return TRUE if it is an indirect memory
   1194    reference, @Rn or @Rn+.  */
   1195 static bool
   1196 msp430_is_mem_indirect (rtx x)
   1197 {
   1198   gcc_assert (GET_CODE (x) == MEM);
   1199   rtx op0 = XEXP (x, 0);
   1200   return (GET_CODE (op0) == REG || GET_CODE (op0) == POST_INC);
   1201 }
   1202 
   1203 /* Costs of MSP430 instructions are generally based on the addressing mode
   1204    combination of the source and destination operands.
   1205    Given source operand SRC (which may be NULL to indicate a single-operand
   1206    instruction) and destination operand DST return the cost of this
   1207    expression.  */
   1208 static int
   1209 msp430_costs (rtx src, rtx dst, bool speed, rtx outer_rtx)
   1210 {
   1211   enum rtx_code src_code = GET_CODE (src);
   1212   enum rtx_code dst_code = GET_CODE (dst);
   1213   enum rtx_code outer_code = GET_CODE (outer_rtx);
   1214   machine_mode outer_mode = GET_MODE (outer_rtx);
   1215   const struct double_op_cost *cost_p;
   1216   cost_p = (speed ? &cycle_cost_double_op : &size_cost_double_op);
   1217 
   1218   if (outer_code == TRUNCATE
   1219       && (outer_mode == QImode
   1220 	  || outer_mode == HImode
   1221 	  || outer_mode == PSImode))
   1222     /* Truncation to these modes is normally free as a side effect of the
   1223        instructions themselves.  */
   1224     return 0;
   1225 
   1226   if (dst_code == SYMBOL_REF
   1227       || dst_code == LABEL_REF
   1228       || dst_code == CONST_INT)
   1229     /* Catch RTX like (minus (const_int 0) (reg)) but don't add any cost.  */
   1230     return 0;
   1231 
   1232   switch (src_code)
   1233     {
   1234     case REG:
   1235       return (dst_code == REG ? cost_p->r2r
   1236 	      : (dst_code == PC ? cost_p->r2pc : cost_p->r2m));
   1237 
   1238     case CONST_INT:
   1239     case SYMBOL_REF:
   1240     case LABEL_REF:
   1241     case CONST:
   1242       return (dst_code == REG ? cost_p->imm2r
   1243 	      : (dst_code == PC ? cost_p->imm2pc : cost_p->imm2m));
   1244 
   1245 
   1246     case MEM:
   1247       if (msp430_is_mem_indirect (src))
   1248 	return (dst_code == REG ? cost_p->ind2r : (dst_code == PC
   1249 						   ? cost_p->ind2pc
   1250 						   : cost_p->ind2m));
   1251       else
   1252 	return (dst_code == REG ? cost_p->mem2r	: (dst_code == PC
   1253 						   ? cost_p->mem2pc
   1254 						   : cost_p->mem2m));
   1255     default:
   1256       return cost_p->mem2m;
   1257     }
   1258 }
   1259 
   1260 /* Given source operand SRC and destination operand DST from the shift or
   1261    rotate RTX OUTER_RTX, return the cost of performing that shift, assuming
   1262    optimization for speed when SPEED is true.  */
   1263 static int
   1264 msp430_shift_costs (rtx src, rtx dst, bool speed, rtx outer_rtx)
   1265 {
   1266   int amt;
   1267   enum rtx_code src_code = GET_CODE (src);
   1268   enum rtx_code dst_code = GET_CODE (dst);
   1269   const struct single_op_cost *cost_p;
   1270 
   1271   cost_p = (speed ? &cycle_cost_single_op : &size_cost_single_op);
   1272 
   1273   if (src_code != CONST_INT)
   1274     /* The size or speed cost when the shift amount is unknown cannot be
   1275        accurately calculated, so just disparage it slightly.  */
   1276     return 2 * msp430_costs (src, dst, speed, outer_rtx);
   1277 
   1278   if (use_helper_for_const_shift (GET_MODE (outer_rtx), amt = INTVAL (src)))
   1279     {
   1280       /* GCC sometimes tries to perform shifts in some very inventive ways,
   1281 	 resulting in much larger code size usage than necessary, if
   1282 	 they are disparaged too much here.  So in general, if
   1283 	 use_helper_for_const_shift thinks a helper should be used, obey
   1284 	 that and don't disparage the shift any more than a regular
   1285 	 instruction, even though the shift may actually cost more.
   1286 	 This ensures that the RTL generated at the initial expand pass has the
   1287 	 expected shift instructions, which can be mapped to the helper
   1288 	 functions.  */
   1289       return msp430_costs (src, dst, speed, outer_rtx);
   1290     }
   1291 
   1292   if (!msp430x)
   1293     {
   1294       /* Each shift by one place will be emitted individually.  */
   1295       switch (dst_code)
   1296 	{
   1297 	case REG:
   1298 	case CONST_INT:
   1299 	  return amt * cost_p->reg;
   1300 	case MEM:
   1301 	  if (msp430_is_mem_indirect (dst))
   1302 	    return amt * cost_p->ind;
   1303 	  else
   1304 	    return amt * cost_p->mem;
   1305 	default:
   1306 	  return amt * cost_p->mem;
   1307 	}
   1308     }
   1309 
   1310   /* RRAM, RRCM, RRUM, RLAM are used for shift counts <= 4, otherwise, the 'X'
   1311      versions are used.
   1312      Instructions which shift a MEM operand will never actually be output.  It
   1313      will always be copied into a register to allow for efficient shifting.  So
   1314      the cost just takes into account the cost of an additional copy in that
   1315      case.  */
   1316   return (amt <= 4 ? (speed ? amt : 1) : (speed ? amt + 1 : 2)
   1317 	  + (dst_code == REG ? 0
   1318 	     : msp430_costs (dst, gen_rtx_REG (HImode, 10), speed, outer_rtx)));
   1319 }
   1320 
   1321 /* Given source operand SRC and destination operand DST from the MULT/DIV/MOD
   1322    RTX OUTER_RTX, return the cost of performing that operation, assuming
   1323    optimization for speed when SPEED is true.  */
   1324 static int
   1325 msp430_muldiv_costs (rtx src, rtx dst, bool speed, rtx outer_rtx,
   1326 		     machine_mode outer_mode)
   1327 {
   1328   enum rtx_code outer_code = GET_CODE (outer_rtx);
   1329   const struct msp430_multlib_costs *cost_p;
   1330   cost_p = (msp430_use_16bit_hwmult ()
   1331 	    ? &cycle_cost_multlib_32bit
   1332 	    : &cycle_cost_multlib_16bit);
   1333 
   1334   int factor = 1;
   1335   /* Only used in some calculations.  */
   1336   int mode_factor = 1;
   1337   if (outer_mode == SImode)
   1338     mode_factor = 2;
   1339   else if (outer_mode == PSImode)
   1340     /* PSImode multiplication is performed using SImode operands, so has extra
   1341        cost to factor in the conversions necessary before/after the
   1342        operation.  */
   1343     mode_factor = 3;
   1344   else if (outer_mode == DImode)
   1345     mode_factor = 4;
   1346 
   1347   if (!speed)
   1348     {
   1349       /* The codesize cost of using a helper function to perform the
   1350 	 multiplication or division cannot be accurately calculated, since the
   1351 	 cost depends on how many times the operation is performed in the
   1352 	 entire program.  */
   1353       if (outer_code != MULT)
   1354 	/* Division is always expensive.  */
   1355 	factor = 7;
   1356       else if (((msp430_use_16bit_hwmult () && outer_mode != DImode)
   1357 		|| msp430_use_32bit_hwmult ()
   1358 		|| msp430_use_f5_series_hwmult ()))
   1359 	/* When the hardware multiplier is available, only disparage
   1360 	   slightly.  */
   1361 	factor = 2;
   1362       else
   1363 	factor = 5;
   1364       return factor * mode_factor * msp430_costs (src, dst, speed, outer_rtx);
   1365     }
   1366 
   1367   /* When there is hardware multiply support, there is a relatively low, fixed
   1368      cycle cost to performing any multiplication, but when there is no hardware
   1369      multiply support it is very costly.  That precise cycle cost has not been
   1370      calculated here.
   1371      Division is extra slow since it always uses a software library.
   1372      The 16-bit hardware multiply library cannot be used to produce 64-bit
   1373      results.  */
   1374   if (outer_code != MULT || !msp430_has_hwmult ()
   1375       || (outer_mode == DImode && msp430_use_16bit_hwmult ()))
   1376     {
   1377       factor = (outer_code == MULT ? 50 : 70);
   1378       return factor * mode_factor * msp430_costs (src, dst, speed, outer_rtx);
   1379     }
   1380 
   1381   switch (outer_mode)
   1382     {
   1383     case E_QImode:
   1384     case E_HImode:
   1385       /* Include the cost of copying the operands into and out of the hardware
   1386 	 multiply routine.  */
   1387       return cost_p->mulhi + (3 * msp430_costs (src, dst, speed, outer_rtx));
   1388 
   1389     case E_PSImode:
   1390       /* Extra factor for the conversions necessary to do PSI->SI before the
   1391 	 operation.  */
   1392       factor = 2;
   1393       /* fallthru.  */
   1394     case E_SImode:
   1395       return factor * (cost_p->mulsi
   1396 		       + (6 * msp430_costs (src, dst, speed, outer_rtx)));
   1397 
   1398     case E_DImode:
   1399     default:
   1400       return cost_p->muldi + (12 * msp430_costs (src, dst, speed, outer_rtx));
   1401     }
   1402 }
   1403 
   1404 /* Recurse within X to find the actual destination operand of the expression.
   1405    For example:
   1406    (plus (ashift (minus (ashift (reg)
   1407    (const_int) ......
   1408    should return the reg RTX.  */
   1409 static rtx
   1410 msp430_get_inner_dest_code (rtx x)
   1411 {
   1412   enum rtx_code code = GET_CODE (x);
   1413   rtx op0 = XEXP (x, 0);
   1414   switch (code)
   1415     {
   1416     case REG:
   1417     case SYMBOL_REF:
   1418     case CONST_INT:
   1419     case CONST:
   1420     case LABEL_REF:
   1421       return x;
   1422 
   1423     case MEM:
   1424       /* Return the MEM expr not the inner REG for these cases.  */
   1425       switch (GET_CODE (op0))
   1426 	{
   1427 	case REG:
   1428 	case SYMBOL_REF:
   1429 	case LABEL_REF:
   1430 	case CONST:
   1431 	case POST_INC:
   1432 	  return x;
   1433 
   1434 	case PLUS:
   1435 	  /* return MEM (PLUS (REG) (CONST)) */
   1436 	  if (GET_CODE (XEXP (op0, 0)) == REG)
   1437 	    {
   1438 	      if (GET_CODE (XEXP (op0, 1)) == CONST_INT
   1439 		  || GET_CODE (XEXP (op0, 1)) == CONST
   1440 		  || GET_CODE (XEXP (op0, 1)) == LABEL_REF
   1441 		  || GET_CODE (XEXP (op0, 1)) == SYMBOL_REF)
   1442 		return x;
   1443 	      else
   1444 		return msp430_get_inner_dest_code (op0);
   1445 	    }
   1446 	  return msp430_get_inner_dest_code (op0);
   1447 
   1448 	default:
   1449 	  if (GET_RTX_FORMAT (code)[0] != 'e')
   1450 	    return x;
   1451 	  return msp430_get_inner_dest_code (op0);
   1452 	}
   1453       break;
   1454 
   1455     default:
   1456       if (op0 == NULL_RTX)
   1457 	gcc_unreachable ();
   1458       else
   1459 	{
   1460 	  if (GET_RTX_FORMAT (code)[0] != 'e'
   1461 	      && code != ENTRY_VALUE)
   1462 	    return x;
   1463 	  return msp430_get_inner_dest_code (op0);
   1464 	}
   1465     }
   1466 }
   1467 
   1468 /* Calculate the cost of an MSP430 single-operand instruction, for operand DST
   1469    within the RTX OUTER_RTX, optimizing for speed if SPEED is true.  */
   1470 static int
   1471 msp430_single_op_cost (rtx dst, bool speed, rtx outer_rtx)
   1472 {
   1473   enum rtx_code dst_code = GET_CODE (dst);
   1474   const struct single_op_cost *cost_p;
   1475   const struct double_op_cost *double_op_cost_p;
   1476 
   1477   cost_p = (speed ? &cycle_cost_single_op : &size_cost_single_op);
   1478   double_op_cost_p = (speed ? &cycle_cost_double_op : &size_cost_double_op);
   1479 
   1480   switch (dst_code)
   1481     {
   1482     case REG:
   1483       return cost_p->reg;
   1484     case MEM:
   1485       if (msp430_is_mem_indirect (dst))
   1486 	return cost_p->ind;
   1487       else
   1488 	return cost_p->mem;
   1489 
   1490     case CONST_INT:
   1491     case CONST_FIXED:
   1492     case CONST_DOUBLE:
   1493     case SYMBOL_REF:
   1494     case CONST:
   1495       /* A constant value would need to be copied into a register first.  */
   1496       return double_op_cost_p->imm2r + cost_p->reg;
   1497 
   1498     default:
   1499       return cost_p->mem;
   1500     }
   1501 }
   1502 
   1503 #undef  TARGET_RTX_COSTS
   1504 #define TARGET_RTX_COSTS msp430_rtx_costs
   1505 
   1506 /* This target hook describes the relative costs of RTL expressions.
   1507    The function recurses to just before the lowest level of the expression,
   1508    when both of the operands of the expression can be examined at the same time.
   1509    This is because the cost of the expression depends on the specific
   1510    addressing mode combination of the operands.
   1511    The hook returns true when all subexpressions of X have been processed, and
   1512    false when rtx_cost should recurse.  */
   1513 static bool
   1514 msp430_rtx_costs (rtx x,
   1515 		  machine_mode mode,
   1516 		  int	   outer_code ATTRIBUTE_UNUSED,
   1517 		  int	   opno ATTRIBUTE_UNUSED,
   1518 		  int *	   total,
   1519 		  bool	   speed)
   1520 {
   1521   enum rtx_code code = GET_CODE (x);
   1522   rtx dst, src;
   1523   rtx dst_inner, src_inner;
   1524 
   1525   *total = 0;
   1526   dst = XEXP (x, 0);
   1527   if (GET_RTX_LENGTH (code) == 1)
   1528     /* Some RTX that are single-op in GCC are double-op when translated to
   1529        MSP430 instructions e.g NOT, NEG, ZERO_EXTEND.  */
   1530     src = dst;
   1531   else
   1532     src = XEXP (x, 1);
   1533 
   1534 
   1535   switch (code)
   1536     {
   1537     case SET:
   1538       /* Ignoring SET improves codesize.  */
   1539       if (!speed)
   1540 	return true;
   1541       /* fallthru.  */
   1542     case PLUS:
   1543       if (outer_code == MEM)
   1544 	/* Do not add any cost for the plus itself, but recurse in case there
   1545 	   are more complicated RTX inside.  */
   1546 	return false;
   1547       /* fallthru.  */
   1548     case MINUS:
   1549     case AND:
   1550     case IOR:
   1551     case XOR:
   1552     case NOT:
   1553     case ZERO_EXTEND:
   1554     case TRUNCATE:
   1555     case NEG:
   1556     case ZERO_EXTRACT:
   1557     case SIGN_EXTRACT:
   1558     case IF_THEN_ELSE:
   1559       dst_inner = msp430_get_inner_dest_code (dst);
   1560       src_inner = msp430_get_inner_dest_code (src);
   1561       *total = COSTS_N_INSNS (msp430_costs (src_inner, dst_inner, speed, x));
   1562       if (mode == SImode)
   1563 	*total *= 2;
   1564       if (mode == DImode)
   1565 	*total *= 4;
   1566       return false;
   1567 
   1568     case ROTATE:
   1569     case ASHIFT:
   1570     case ASHIFTRT:
   1571     case LSHIFTRT:
   1572       dst_inner = msp430_get_inner_dest_code (dst);
   1573       src_inner = msp430_get_inner_dest_code (src);
   1574       *total = COSTS_N_INSNS (msp430_shift_costs (src_inner, dst_inner,
   1575 						  speed, x));
   1576       if (mode == SImode)
   1577 	*total *= 2;
   1578       if (mode == DImode)
   1579 	*total *= 4;
   1580       return false;
   1581 
   1582     case MULT:
   1583     case DIV:
   1584     case MOD:
   1585     case UDIV:
   1586     case UMOD:
   1587       dst_inner = msp430_get_inner_dest_code (dst);
   1588       src_inner = msp430_get_inner_dest_code (src);
   1589       *total = COSTS_N_INSNS (msp430_muldiv_costs (src_inner, dst_inner, speed,
   1590 						   x, mode));
   1591       return false;
   1592 
   1593     case CALL:
   1594     case SIGN_EXTEND:
   1595       dst_inner = msp430_get_inner_dest_code (dst);
   1596       *total = COSTS_N_INSNS (msp430_single_op_cost (dst_inner, speed, x));
   1597       if (mode == SImode)
   1598 	*total *= 2;
   1599       if (mode == DImode)
   1600 	*total *= 4;
   1601       return false;
   1602 
   1603     case CONST_INT:
   1604     case CONST_FIXED:
   1605     case CONST_DOUBLE:
   1606     case SYMBOL_REF:
   1607     case CONST:
   1608     case LABEL_REF:
   1609     case REG:
   1610     case PC:
   1611     case POST_INC:
   1612       if (mode == SImode)
   1613 	*total = COSTS_N_INSNS (2);
   1614       else if (mode == DImode)
   1615 	*total = COSTS_N_INSNS (4);
   1616       return true;
   1617 
   1618     case MEM:
   1619       /* PSImode operands are expensive when in memory.  */
   1620       if (mode == PSImode)
   1621 	*total = COSTS_N_INSNS (1);
   1622       else if (mode == SImode)
   1623 	*total = COSTS_N_INSNS (2);
   1624       else if (mode == DImode)
   1625 	*total = COSTS_N_INSNS (4);
   1626       /* Recurse into the MEM.  */
   1627       return false;
   1628 
   1629     case EQ:
   1630     case NE:
   1631     case GT:
   1632     case GTU:
   1633     case GE:
   1634     case GEU:
   1635     case LT:
   1636     case LTU:
   1637     case LE:
   1638     case LEU:
   1639       /* Conditions are mostly equivalent, changing their relative
   1640 	 costs has no effect.  */
   1641       return false;
   1642 
   1643     case ASM_OPERANDS:
   1644     case ASM_INPUT:
   1645     case CLOBBER:
   1646     case COMPARE:
   1647     case CONCAT:
   1648     case ENTRY_VALUE:
   1649       /* Other unhandled expressions.  */
   1650       return false;
   1651 
   1652     default:
   1653       return false;
   1654     }
   1655 }
   1656 
   1657 #undef TARGET_INSN_COST
   1658 #define TARGET_INSN_COST msp430_insn_cost
   1659 
   1660 static int
   1661 msp430_insn_cost (rtx_insn *insn, bool speed ATTRIBUTE_UNUSED)
   1662 {
   1663   if (recog_memoized (insn) < 0)
   1664     return 0;
   1665 
   1666   /* The returned cost must be relative to COSTS_N_INSNS (1). An insn with a
   1667      length of 2 bytes is the smallest possible size and so must be equivalent
   1668      to COSTS_N_INSNS (1).  */
   1669   return COSTS_N_INSNS (get_attr_length (insn) / 2);
   1670 
   1671   /* FIXME Add more detailed costs when optimizing for speed.
   1672      For now the length of the instruction is a good approximiation and roughly
   1673      correlates with cycle cost.  */
   1674 }
   1675 
   1676 
   1677 /* Function Entry and Exit */
   1679 
   1680 /* The MSP430 call frame looks like this:
   1681 
   1682    <higher addresses>
   1683    +--------------------+
   1684    |                    |
   1685    | Stack Arguments    |
   1686    |                    |
   1687    +--------------------+ <-- "arg pointer"
   1688    |                    |
   1689    | PC from call       |  (2 bytes for 430, 4 for TARGET_LARGE)
   1690    |                    |
   1691    +--------------------+
   1692    | SR if this func has|
   1693    | been called via an |
   1694    | interrupt.         |
   1695    +--------------------+  <-- SP before prologue, also AP
   1696    |                    |
   1697    | Saved Regs         |  (2 bytes per reg for 430, 4 per for TARGET_LARGE)
   1698    |                    |
   1699    +--------------------+  <-- "frame pointer"
   1700    |                    |
   1701    | Locals             |
   1702    |                    |
   1703    +--------------------+
   1704    |                    |
   1705    | Outgoing Args      |
   1706    |                    |
   1707    +--------------------+  <-- SP during function
   1708    <lower addresses>
   1709 
   1710 */
   1711 
   1712 /* We use this to wrap all emitted insns in the prologue, so they get
   1713    the "frame-related" (/f) flag set.  */
   1714 static rtx
   1715 F (rtx x)
   1716 {
   1717   RTX_FRAME_RELATED_P (x) = 1;
   1718   return x;
   1719 }
   1720 
   1721 /* This is the one spot that decides if a register is to be saved and
   1722    restored in the prologue/epilogue.  */
   1723 static bool
   1724 msp430_preserve_reg_p (int regno)
   1725 {
   1726   /* PC, SP, SR, and the constant generator.  */
   1727   if (regno <= 3)
   1728     return false;
   1729 
   1730   /* FIXME: add interrupt, EH, etc.  */
   1731   if (crtl->calls_eh_return)
   1732     return true;
   1733 
   1734   /* Shouldn't be more than the above, but just in case...  */
   1735   if (fixed_regs[regno])
   1736     return false;
   1737 
   1738   /* For interrupt functions we must save and restore the used regs that
   1739      would normally be caller-saved (R11->R15).  */
   1740   if (msp430_is_interrupt_func () && regno >= 11 && regno <= 15)
   1741     {
   1742       if (crtl->is_leaf && df_regs_ever_live_p (regno))
   1743 	/* If the interrupt func is a leaf then we only need to restore the
   1744 	   caller-saved regs that are used.  */
   1745 	return true;
   1746       else if (!crtl->is_leaf)
   1747 	/* If the interrupt function is not a leaf we must save all
   1748 	   caller-saved regs in case the callee modifies them.  */
   1749 	return true;
   1750     }
   1751 
   1752   if (!call_used_or_fixed_reg_p (regno)
   1753       && df_regs_ever_live_p (regno))
   1754     return true;
   1755 
   1756   return false;
   1757 }
   1758 
   1759 /* Compute all the frame-related fields in our machine_function
   1760    structure.  */
   1761 static void
   1762 msp430_compute_frame_info (void)
   1763 {
   1764   int i;
   1765 
   1766   cfun->machine->computed = 1;
   1767   cfun->machine->framesize_regs = 0;
   1768   cfun->machine->framesize_locals = get_frame_size ();
   1769   cfun->machine->framesize_outgoing = crtl->outgoing_args_size;
   1770 
   1771   for (i = 0; i < ARG_POINTER_REGNUM; i ++)
   1772     if (msp430_preserve_reg_p (i))
   1773       {
   1774 	cfun->machine->need_to_save[i] = 1;
   1775 	cfun->machine->framesize_regs += (TARGET_LARGE ? 4 : 2);
   1776       }
   1777     else
   1778       cfun->machine->need_to_save[i] = 0;
   1779 
   1780   if ((cfun->machine->framesize_locals + cfun->machine->framesize_outgoing) & 1)
   1781     cfun->machine->framesize_locals ++;
   1782 
   1783   cfun->machine->framesize = (cfun->machine->framesize_regs
   1784 			      + cfun->machine->framesize_locals
   1785 			      + cfun->machine->framesize_outgoing);
   1786 }
   1787 
   1788 /* Attribute Handling.  */
   1789 
   1790 const char * const  ATTR_INTR   = "interrupt";
   1791 const char * const  ATTR_WAKEUP = "wakeup";
   1792 const char * const  ATTR_NAKED  = "naked";
   1793 const char * const  ATTR_REENT  = "reentrant";
   1794 const char * const  ATTR_CRIT   = "critical";
   1795 const char * const  ATTR_LOWER  = "lower";
   1796 const char * const  ATTR_UPPER  = "upper";
   1797 const char * const  ATTR_EITHER = "either";
   1798 const char * const  ATTR_NOINIT = "noinit";
   1799 const char * const  ATTR_PERSIST = "persistent";
   1800 
   1801 static inline bool
   1802 has_attr (const char * attr, tree decl)
   1803 {
   1804   if (decl == NULL_TREE)
   1805     return false;
   1806   return lookup_attribute (attr, DECL_ATTRIBUTES (decl)) != NULL_TREE;
   1807 }
   1808 
   1809 static bool
   1810 is_interrupt_func (tree decl = current_function_decl)
   1811 {
   1812   return has_attr (ATTR_INTR, decl);
   1813 }
   1814 
   1815 /* Returns true if the current function has the "interrupt" attribute.  */
   1816 
   1817 bool
   1818 msp430_is_interrupt_func (void)
   1819 {
   1820   return is_interrupt_func (current_function_decl);
   1821 }
   1822 
   1823 static bool
   1824 is_wakeup_func (tree decl = current_function_decl)
   1825 {
   1826   return is_interrupt_func (decl) && has_attr (ATTR_WAKEUP, decl);
   1827 }
   1828 
   1829 static inline bool
   1830 is_naked_func (tree decl = current_function_decl)
   1831 {
   1832   return has_attr (ATTR_NAKED, decl);
   1833 }
   1834 
   1835 static inline bool
   1836 is_reentrant_func (tree decl = current_function_decl)
   1837 {
   1838   return has_attr (ATTR_REENT, decl);
   1839 }
   1840 
   1841 static inline bool
   1842 is_critical_func (tree decl = current_function_decl)
   1843 {
   1844   return has_attr (ATTR_CRIT, decl);
   1845 }
   1846 
   1847 static bool
   1848 has_section_name (const char * name, tree decl = current_function_decl)
   1849 {
   1850   if (decl == NULL_TREE)
   1851     return false;
   1852   return (DECL_SECTION_NAME (decl)
   1853 	  && (strcmp (name, DECL_SECTION_NAME (decl)) == 0));
   1854 }
   1855 
   1856 #undef  TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
   1857 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS \
   1858   msp430_allocate_stack_slots_for_args
   1859 
   1860 static bool
   1861 msp430_allocate_stack_slots_for_args (void)
   1862 {
   1863   /* Naked functions should not allocate stack slots for arguments.  */
   1864   return ! is_naked_func ();
   1865 }
   1866 
   1867 #undef TARGET_WARN_FUNC_RETURN
   1868 #define TARGET_WARN_FUNC_RETURN msp430_warn_func_return
   1869 
   1870 static bool
   1871 msp430_warn_func_return (tree decl)
   1872 {
   1873   /* Naked functions are implemented entirely in assembly, including the
   1874      return sequence, so suppress warnings about this.  */
   1875   return !is_naked_func (decl);
   1876 }
   1877 
   1878 /* Verify MSP430 specific attributes.  */
   1879 #define TREE_NAME_EQ(NAME, STR) (strcmp (IDENTIFIER_POINTER (NAME), (STR)) == 0)
   1880 
   1881 static tree
   1882 msp430_attr (tree * node,
   1883 	     tree   name,
   1884 	     tree   args,
   1885 	     int    flags ATTRIBUTE_UNUSED,
   1886 	     bool * no_add_attrs)
   1887 {
   1888   gcc_assert (DECL_P (* node));
   1889 
   1890   /* Only the interrupt attribute takes an argument.  */
   1891   if (args != NULL)
   1892     {
   1893       tree value = TREE_VALUE (args);
   1894 
   1895       switch (TREE_CODE (value))
   1896 	{
   1897 	case STRING_CST:
   1898 	  if (   strcmp (TREE_STRING_POINTER (value), "reset")
   1899 	      && strcmp (TREE_STRING_POINTER (value), "nmi")
   1900 	      && strcmp (TREE_STRING_POINTER (value), "watchdog"))
   1901 	    /* Allow the attribute to be added - the linker script
   1902 	       being used may still recognise this name.  */
   1903 	    warning (OPT_Wattributes,
   1904 		     "unrecognized interrupt vector argument of %qE attribute",
   1905 		     name);
   1906 	  break;
   1907 
   1908 	case INTEGER_CST:
   1909 	  if (wi::gtu_p (wi::to_wide (value), 63))
   1910 	    /* Allow the attribute to be added - the linker script
   1911 	       being used may still recognise this value.  */
   1912 	    warning (OPT_Wattributes,
   1913 		     "numeric argument of %qE attribute must be in range [0-63]",
   1914 		     name);
   1915 	  break;
   1916 
   1917 	default:
   1918 	  warning (OPT_Wattributes,
   1919 		   "argument of %qE attribute is not a string constant "
   1920 		   "or number", name);
   1921 	  *no_add_attrs = true;
   1922 	  break;
   1923 	}
   1924     }
   1925 
   1926   const char * message = NULL;
   1927 
   1928   if (TREE_CODE (* node) != FUNCTION_DECL)
   1929     {
   1930       message = "%qE attribute only applies to functions";
   1931     }
   1932   else if (TREE_NAME_EQ (name, ATTR_INTR))
   1933     {
   1934       if (TREE_CODE (TREE_TYPE (* node)) == FUNCTION_TYPE
   1935 	  && ! VOID_TYPE_P (TREE_TYPE (TREE_TYPE (* node))))
   1936 	message = "interrupt handlers must be void";
   1937       else
   1938 	{
   1939 	  /* Ensure interrupt handlers never get optimised out.  */
   1940 	  TREE_USED (* node) = 1;
   1941 	  DECL_PRESERVE_P (* node) = 1;
   1942 	}
   1943       if (is_critical_func (* node))
   1944 	{
   1945 	  /* We always ignore the critical attribute when interrupt and
   1946 	     critical are used together.  */
   1947 	  warning (OPT_Wattributes,
   1948 		   "critical attribute has no effect on interrupt functions");
   1949 	  DECL_ATTRIBUTES (*node) = remove_attribute (ATTR_CRIT,
   1950 						      DECL_ATTRIBUTES (* node));
   1951 	}
   1952     }
   1953   else if (TREE_NAME_EQ (name, ATTR_CRIT))
   1954     {
   1955       if (is_interrupt_func ( *node))
   1956 	message = "critical attribute has no effect on interrupt functions";
   1957     }
   1958 
   1959   if (message)
   1960     {
   1961       warning (OPT_Wattributes, message, name);
   1962       * no_add_attrs = true;
   1963     }
   1964 
   1965   return NULL_TREE;
   1966 }
   1967 
   1968 static tree
   1969 msp430_section_attr (tree * node,
   1970 		     tree   name,
   1971 		     tree   args,
   1972 		     int    flags ATTRIBUTE_UNUSED,
   1973 		     bool * no_add_attrs ATTRIBUTE_UNUSED)
   1974 {
   1975   gcc_assert (DECL_P (* node));
   1976   gcc_assert (args == NULL);
   1977 
   1978   const char * message = NULL;
   1979 
   1980   /* The "noinit", "persistent", and "section" attributes are handled
   1981      generically, so we cannot set up additional target-specific attribute
   1982      exclusions using the existing mechanism.  */
   1983   if (has_attr (ATTR_NOINIT, *node) && !TREE_NAME_EQ (name, "lower"))
   1984     message = G_("ignoring attribute %qE because it conflicts with "
   1985 		 "attribute %<noinit%>");
   1986   else if (has_attr ("section", *node) && !TREE_NAME_EQ (name, "lower"))
   1987     message = G_("ignoring attribute %qE because it conflicts with "
   1988 		 "attribute %<section%>");
   1989   else if (has_attr (ATTR_PERSIST, *node) && !TREE_NAME_EQ (name, "lower"))
   1990     message = G_("ignoring attribute %qE because it conflicts with "
   1991 		 "attribute %<persistent%>");
   1992   /* It does not make sense to use upper/lower/either attributes without
   1993      -mlarge.
   1994      Without -mlarge, "lower" is the default and only region, so is redundant.
   1995      Without -mlarge, "upper" will (and "either" might) place code/data in the
   1996      upper region, which for data could result in relocation overflows, and for
   1997      code could result in stack mismanagement and incorrect call/return
   1998      instructions.  */
   1999   else if (!TARGET_LARGE)
   2000     message = G_("%qE attribute ignored.  Large memory model (%<-mlarge%>) "
   2001 		 "is required.");
   2002 
   2003   if (message)
   2004     {
   2005       warning (OPT_Wattributes, message, name);
   2006       * no_add_attrs = true;
   2007     }
   2008 
   2009   return NULL_TREE;
   2010 }
   2011 
   2012 /* Helper to define attribute exclusions.  */
   2013 #define ATTR_EXCL(name, function, type, variable)	\
   2014   { name, function, type, variable }
   2015 
   2016 /* "reentrant", "critical" and "naked" functions must conflict because
   2017    they all modify the prologue or epilogue of functions in mutually exclusive
   2018    ways.  */
   2019 static const struct attribute_spec::exclusions attr_reent_exclusions[] =
   2020 {
   2021   ATTR_EXCL (ATTR_NAKED, true, true, true),
   2022   ATTR_EXCL (ATTR_CRIT, true, true, true),
   2023   ATTR_EXCL (NULL, false, false, false)
   2024 };
   2025 
   2026 static const struct attribute_spec::exclusions attr_naked_exclusions[] =
   2027 {
   2028   ATTR_EXCL (ATTR_REENT, true, true, true),
   2029   ATTR_EXCL (ATTR_CRIT, true, true, true),
   2030   ATTR_EXCL (NULL, false, false, false)
   2031 };
   2032 
   2033 static const struct attribute_spec::exclusions attr_crit_exclusions[] =
   2034 {
   2035   ATTR_EXCL (ATTR_REENT, true, true, true),
   2036   ATTR_EXCL (ATTR_NAKED, true, true, true),
   2037   ATTR_EXCL (NULL, false, false, false)
   2038 };
   2039 
   2040 /* Attributes which put the given object in a specific section must conflict
   2041    with one another.  */
   2042 static const struct attribute_spec::exclusions attr_lower_exclusions[] =
   2043 {
   2044   ATTR_EXCL (ATTR_UPPER, true, true, true),
   2045   ATTR_EXCL (ATTR_EITHER, true, true, true),
   2046   ATTR_EXCL (NULL, false, false, false)
   2047 };
   2048 
   2049 static const struct attribute_spec::exclusions attr_upper_exclusions[] =
   2050 {
   2051   ATTR_EXCL (ATTR_LOWER, true, true, true),
   2052   ATTR_EXCL (ATTR_EITHER, true, true, true),
   2053   ATTR_EXCL (NULL, false, false, false)
   2054 };
   2055 
   2056 static const struct attribute_spec::exclusions attr_either_exclusions[] =
   2057 {
   2058   ATTR_EXCL (ATTR_LOWER, true, true, true),
   2059   ATTR_EXCL (ATTR_UPPER, true, true, true),
   2060   ATTR_EXCL (NULL, false, false, false)
   2061 };
   2062 
   2063 #undef  TARGET_ATTRIBUTE_TABLE
   2064 #define TARGET_ATTRIBUTE_TABLE		msp430_attribute_table
   2065 
   2066 /* Table of MSP430-specific attributes.  */
   2067 const struct attribute_spec msp430_attribute_table[] =
   2068   {
   2069     /* { name, min_num_args, max_num_args, decl_req, type_req, fn_type_req,
   2070 	 affects_type_identity, handler, exclude } */
   2071     { ATTR_INTR,	0, 1, true,  false, false, false, msp430_attr, NULL },
   2072     { ATTR_NAKED,       0, 0, true,  false, false, false, msp430_attr,
   2073       attr_naked_exclusions },
   2074     { ATTR_REENT,       0, 0, true,  false, false, false, msp430_attr,
   2075       attr_reent_exclusions },
   2076     { ATTR_CRIT,	0, 0, true,  false, false, false, msp430_attr,
   2077       attr_crit_exclusions },
   2078     { ATTR_WAKEUP,      0, 0, true,  false, false, false, msp430_attr, NULL },
   2079 
   2080     { ATTR_LOWER,       0, 0, true,  false, false, false, msp430_section_attr,
   2081       attr_lower_exclusions },
   2082     { ATTR_UPPER,       0, 0, true,  false, false, false, msp430_section_attr,
   2083       attr_upper_exclusions },
   2084     { ATTR_EITHER,      0, 0, true,  false, false, false, msp430_section_attr,
   2085       attr_either_exclusions },
   2086 
   2087     { NULL,		0, 0, false, false, false, false, NULL,  NULL }
   2088   };
   2089 
   2090 #undef TARGET_HANDLE_GENERIC_ATTRIBUTE
   2091 #define TARGET_HANDLE_GENERIC_ATTRIBUTE msp430_handle_generic_attribute
   2092 
   2093 tree
   2094 msp430_handle_generic_attribute (tree *node,
   2095 				 tree   name,
   2096 				 tree   args ATTRIBUTE_UNUSED,
   2097 				 int    flags ATTRIBUTE_UNUSED,
   2098 				 bool *no_add_attrs)
   2099 
   2100 {
   2101   const char *message = NULL;
   2102 
   2103   /* Permit the "lower" attribute to be set on variables with the "section",
   2104      "noinit" and "persistent" attributes.  This is used to indicate that the
   2105      corresponding output section will be in lower memory, so a 430X
   2106      instruction is not required to handle it.  */
   2107   if (has_attr (ATTR_LOWER, *node)
   2108       && !(TREE_NAME_EQ (name, "section") || TREE_NAME_EQ (name, ATTR_PERSIST)
   2109 	   || TREE_NAME_EQ (name, ATTR_NOINIT)))
   2110     message = G_("ignoring attribute %qE because it conflicts with "
   2111 		 "attribute %<lower%>");
   2112   else if (has_attr (ATTR_UPPER, *node))
   2113     message = G_("ignoring attribute %qE because it conflicts with "
   2114 		 "attribute %<upper%>");
   2115   else if (has_attr (ATTR_EITHER, *node))
   2116     message = G_("ignoring attribute %qE because it conflicts with "
   2117 		 "attribute %<either%>");
   2118 
   2119   if (message)
   2120     {
   2121       warning (OPT_Wattributes, message, name);
   2122       *no_add_attrs = true;
   2123     }
   2124 
   2125   return NULL_TREE;
   2126 }
   2127 
   2128 /* Given a non-automatic VAR_DECL which can possibly have a section, return
   2129    true if the variable will definitely be placed in the lower memory
   2130    region (below address 0x10000).  */
   2131 static bool
   2132 msp430_var_in_low_mem (tree decl)
   2133 {
   2134   gcc_assert (VAR_P (decl));
   2135 
   2136   /* "noinit" variables are always placed in the lower memory region.  */
   2137   if (has_attr (ATTR_UPPER, decl)
   2138       || has_attr (ATTR_EITHER, decl)
   2139       || has_attr (ATTR_PERSIST, decl)
   2140       /* Unless the variable is marked with the lower or noinit attribute, we
   2141 	 cannot assume that it is in the lower region if it is marked with the
   2142 	 section attribute or -mdata-region={upper,either,none} have been
   2143 	 passed.
   2144 	 The noinit and section attributes conflict.  */
   2145       || (!has_attr (ATTR_LOWER, decl) && !has_attr (ATTR_NOINIT, decl)
   2146 	  && (has_attr ("section", decl)
   2147 	      || msp430_data_region == MSP430_REGION_UPPER
   2148 	      || msp430_data_region == MSP430_REGION_EITHER
   2149 	      || msp430_data_region == MSP430_REGION_ANY)))
   2150     return false;
   2151   return true;
   2152 }
   2153 
   2154 #undef TARGET_ENCODE_SECTION_INFO
   2155 #define TARGET_ENCODE_SECTION_INFO msp430_encode_section_info
   2156 
   2157 /* Encode whether a SYMBOL_REF is definitely in the lower memory region.  */
   2158 static void
   2159 msp430_encode_section_info (tree decl, rtx rtl, int first)
   2160 {
   2161   rtx symbol;
   2162   default_encode_section_info (decl, rtl, first);
   2163 
   2164   /* Careful not to prod global register variables.  */
   2165   if (!MEM_P (rtl))
   2166     return;
   2167   symbol = XEXP (rtl, 0);
   2168   if (GET_CODE (symbol) != SYMBOL_REF)
   2169     return;
   2170 
   2171   if (VAR_P (decl)
   2172       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
   2173       && msp430_var_in_low_mem (decl))
   2174     SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOW_MEM;
   2175 }
   2176 
   2177 #undef  TARGET_ASM_FUNCTION_PROLOGUE
   2178 #define TARGET_ASM_FUNCTION_PROLOGUE	msp430_start_function
   2179 
   2180 static void
   2181 msp430_start_function (FILE *outfile)
   2182 {
   2183   int r, n;
   2184 
   2185   fprintf (outfile, "; start of function\n");
   2186 
   2187   if (DECL_ATTRIBUTES (current_function_decl) != NULL_TREE)
   2188     {
   2189       fprintf (outfile, "; attributes: ");
   2190       if (is_naked_func ())
   2191 	fprintf (outfile, "naked ");
   2192       if (msp430_is_interrupt_func ())
   2193 	fprintf (outfile, "interrupt ");
   2194       if (is_reentrant_func ())
   2195 	fprintf (outfile, "reentrant ");
   2196       if (is_critical_func ())
   2197 	fprintf (outfile, "critical ");
   2198       if (is_wakeup_func ())
   2199 	fprintf (outfile, "wakeup ");
   2200       fprintf (outfile, "\n");
   2201     }
   2202 
   2203   fprintf (outfile, "; framesize_regs:     %d\n",
   2204 	   cfun->machine->framesize_regs);
   2205   fprintf (outfile, "; framesize_locals:   %d\n",
   2206 	   cfun->machine->framesize_locals);
   2207   fprintf (outfile, "; framesize_outgoing: %d\n",
   2208 	   cfun->machine->framesize_outgoing);
   2209   fprintf (outfile, "; framesize:          %d\n", cfun->machine->framesize);
   2210   fprintf (outfile, "; elim ap -> fp       %d\n",
   2211 	   msp430_initial_elimination_offset (ARG_POINTER_REGNUM,
   2212 					      FRAME_POINTER_REGNUM));
   2213   fprintf (outfile, "; elim fp -> sp       %d\n",
   2214 	   msp430_initial_elimination_offset (FRAME_POINTER_REGNUM,
   2215 					      STACK_POINTER_REGNUM));
   2216 
   2217   n = 0;
   2218   fprintf (outfile, "; saved regs:");
   2219   for (r = 0; r < ARG_POINTER_REGNUM; r++)
   2220     if (cfun->machine->need_to_save[r])
   2221       {
   2222 	fprintf (outfile, " %s", reg_names[r]);
   2223 	n = 1;
   2224       }
   2225   if (n == 0)
   2226     fprintf (outfile, "(none)");
   2227   fprintf (outfile, "\n");
   2228 }
   2229 
   2230 /* Common code to change the stack pointer.  */
   2231 static void
   2232 increment_stack (HOST_WIDE_INT amount)
   2233 {
   2234   rtx inc;
   2235   rtx sp =  stack_pointer_rtx;
   2236 
   2237   if (amount == 0)
   2238     return;
   2239 
   2240   if (amount < 0)
   2241     {
   2242       inc = GEN_INT (- amount);
   2243       if (TARGET_LARGE)
   2244 	F (emit_insn (gen_subpsi3 (sp, sp, inc)));
   2245       else
   2246 	F (emit_insn (gen_subhi3 (sp, sp, inc)));
   2247     }
   2248   else
   2249     {
   2250       inc = GEN_INT (amount);
   2251       if (TARGET_LARGE)
   2252 	F (emit_insn (gen_addpsi3 (sp, sp, inc)));
   2253       else
   2254 	F (emit_insn (gen_addhi3 (sp, sp, inc)));
   2255     }
   2256 }
   2257 
   2258 void
   2259 msp430_start_function (FILE *file, const char *name, tree decl)
   2260 {
   2261   tree int_attr;
   2262 
   2263   int_attr = lookup_attribute ("interrupt", DECL_ATTRIBUTES (decl));
   2264   if (int_attr != NULL_TREE)
   2265     {
   2266       tree intr_vector = TREE_VALUE (int_attr);
   2267 
   2268       if (intr_vector != NULL_TREE)
   2269 	{
   2270 	  char buf[101];
   2271 
   2272 	  /* Interrupt vector sections should be unique, but use of weak
   2273 	     functions implies multiple definitions.  */
   2274 	  if (DECL_WEAK (decl))
   2275 	    {
   2276 	      error ("argument to interrupt attribute is unsupported for weak "
   2277 		     "functions");
   2278 	    }
   2279 
   2280 	  intr_vector = TREE_VALUE (intr_vector);
   2281 
   2282 	  /* The interrupt attribute has a vector value.  Turn this into a
   2283 	     section name, switch to that section and put the address of
   2284 	     the current function into that vector slot.  Note msp430_attr()
   2285 	     has already verified the vector name for us.  */
   2286 	  if (TREE_CODE (intr_vector) == STRING_CST)
   2287 	    sprintf (buf, "__interrupt_vector_%.80s",
   2288 		     TREE_STRING_POINTER (intr_vector));
   2289 	  else /* TREE_CODE (intr_vector) == INTEGER_CST */
   2290 	    sprintf (buf, "__interrupt_vector_%u",
   2291 		     (unsigned int) TREE_INT_CST_LOW (intr_vector));
   2292 
   2293 	  switch_to_section (get_section (buf, SECTION_CODE, decl));
   2294 	  fputs ("\t.word\t", file);
   2295 	  assemble_name (file, name);
   2296 	  fputc ('\n', file);
   2297 	  fputc ('\t', file);
   2298 	}
   2299     }
   2300 
   2301   switch_to_section (function_section (decl));
   2302   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
   2303   ASM_OUTPUT_FUNCTION_LABEL (file, name, decl);
   2304 }
   2305 
   2306 static const char * const lower_prefix = ".lower";
   2307 static const char * const upper_prefix = ".upper";
   2308 static const char * const either_prefix = ".either";
   2309 
   2310 /* Generate a prefix for a section name, based upon
   2311    the region into which the object should be placed.  */
   2312 
   2313 static const char *
   2314 gen_prefix (tree decl)
   2315 {
   2316   if (DECL_ONE_ONLY (decl))
   2317     return NULL;
   2318 
   2319   /* If the user has specified a particular section then do not use any
   2320      prefix.  */
   2321   if (has_attr ("section", decl))
   2322     return NULL;
   2323 
   2324   /* If the function has been put in the .lowtext section (because it is an
   2325      interrupt handler, and the large memory model is used), then do not add
   2326      any prefixes.  */
   2327   if (has_section_name (".lowtext", decl))
   2328     return NULL;
   2329 
   2330   /* Memory regions require the large memory model.  */
   2331   if (!TARGET_LARGE)
   2332     return NULL;
   2333 
   2334   /* Note that we always apply the lower prefix when the attribute has been
   2335      used.  But we only apply the lower prefix when the lower region has been
   2336      specified by a command line option if -muse-lower-region-prefix has also
   2337      been passed.  */
   2338   if (has_attr (ATTR_LOWER, decl))
   2339     return lower_prefix;
   2340 
   2341   if (has_attr (ATTR_UPPER, decl))
   2342     return upper_prefix;
   2343 
   2344   if (has_attr (ATTR_EITHER, decl))
   2345     return either_prefix;
   2346 
   2347   if (TREE_CODE (decl) == FUNCTION_DECL)
   2348     {
   2349       if ((msp430_code_region == MSP430_REGION_LOWER)
   2350 	  && TARGET_USE_LOWER_REGION_PREFIX)
   2351 	return lower_prefix;
   2352 
   2353       if (msp430_code_region == MSP430_REGION_UPPER)
   2354 	return upper_prefix;
   2355 
   2356       if (msp430_code_region == MSP430_REGION_EITHER)
   2357 	return either_prefix;
   2358     }
   2359   else
   2360     {
   2361       if ((msp430_data_region == MSP430_REGION_LOWER)
   2362 	  && TARGET_USE_LOWER_REGION_PREFIX)
   2363 	return lower_prefix;
   2364 
   2365       if (msp430_data_region == MSP430_REGION_UPPER)
   2366 	return upper_prefix;
   2367 
   2368       if (msp430_data_region == MSP430_REGION_EITHER)
   2369 	return either_prefix;
   2370     }
   2371 
   2372   return NULL;
   2373 }
   2374 
   2375 #undef  TARGET_ASM_SELECT_SECTION
   2376 #define TARGET_ASM_SELECT_SECTION msp430_select_section
   2377 
   2378 static section *
   2379 msp430_select_section (tree decl, int reloc, unsigned HOST_WIDE_INT align)
   2380 {
   2381   const char *prefix;
   2382   const char *sec_name;
   2383   const char *base_sec_name;
   2384 
   2385   gcc_assert (decl != NULL_TREE);
   2386 
   2387   if (TREE_CODE (decl) == STRING_CST
   2388       || TREE_CODE (decl) == CONSTRUCTOR
   2389       || TREE_CODE (decl) == INTEGER_CST
   2390       || TREE_CODE (decl) == VECTOR_CST
   2391       || TREE_CODE (decl) == COMPLEX_CST)
   2392     return default_select_section (decl, reloc, align);
   2393 
   2394   /* In large mode we must make sure that interrupt handlers are put into
   2395      low memory as the vector table only accepts 16-bit addresses.  */
   2396   if (TARGET_LARGE && TREE_CODE (decl) == FUNCTION_DECL
   2397       && is_interrupt_func (decl))
   2398     return get_section (".lowtext", SECTION_CODE | SECTION_WRITE , decl);
   2399 
   2400   /* The "noinit" and "persistent" attributes are handled generically.  */
   2401   if (has_attr (ATTR_NOINIT, decl) || has_attr (ATTR_PERSIST, decl))
   2402     return default_elf_select_section (decl, reloc, align);
   2403 
   2404   prefix = gen_prefix (decl);
   2405 
   2406   switch (categorize_decl_for_section (decl, reloc))
   2407     {
   2408     case SECCAT_TEXT:
   2409       if (!prefix)
   2410 	return text_section;
   2411       base_sec_name = ".text";
   2412       break;
   2413     case SECCAT_DATA:
   2414       if (!prefix)
   2415 	return data_section;
   2416       base_sec_name = ".data";
   2417       break;
   2418     case SECCAT_BSS:
   2419       if (!prefix)
   2420 	return bss_section;
   2421       base_sec_name = ".bss";
   2422       break;
   2423     case SECCAT_RODATA:
   2424       if (!prefix)
   2425 	return readonly_data_section;
   2426       base_sec_name = ".rodata";
   2427       break;
   2428 
   2429     /* Enable merging of constant data by the GNU linker using
   2430        default_elf_select_section and therefore enabling creation of
   2431        sections with the SHF_MERGE flag.  */
   2432     case SECCAT_RODATA_MERGE_STR:
   2433     case SECCAT_RODATA_MERGE_STR_INIT:
   2434     case SECCAT_RODATA_MERGE_CONST:
   2435       return default_elf_select_section (decl, reloc, align);
   2436 
   2437     /* The sections listed below are not supported for MSP430.
   2438        They should not be generated, but in case they are, we use
   2439        default_select_section so they get placed in sections
   2440        the msp430 assembler and linker understand.  */
   2441     /* "small data" sections are not supported.  */
   2442     case SECCAT_SRODATA:
   2443     case SECCAT_SDATA:
   2444     case SECCAT_SBSS:
   2445     /* Thread-local storage (TLS) is not supported.  */
   2446     case SECCAT_TDATA:
   2447     case SECCAT_TBSS:
   2448     /* Sections used by a dynamic linker are not supported.  */
   2449     case SECCAT_DATA_REL:
   2450     case SECCAT_DATA_REL_LOCAL:
   2451     case SECCAT_DATA_REL_RO:
   2452     case SECCAT_DATA_REL_RO_LOCAL:
   2453       return default_select_section (decl, reloc, align);
   2454 
   2455     default:
   2456       gcc_unreachable ();
   2457     }
   2458 
   2459   sec_name = ACONCAT ((prefix, base_sec_name, DECL_SECTION_NAME (decl), NULL));
   2460 
   2461   return get_named_section (decl, sec_name, 0);
   2462 }
   2463 
   2464 #undef  TARGET_ASM_FUNCTION_SECTION
   2465 #define TARGET_ASM_FUNCTION_SECTION msp430_function_section
   2466 
   2467 static section *
   2468 msp430_function_section (tree decl, enum node_frequency freq, bool startup,
   2469 			 bool exit)
   2470 {
   2471   const char * name;
   2472 
   2473   gcc_assert (DECL_SECTION_NAME (decl) != NULL);
   2474   name = DECL_SECTION_NAME (decl);
   2475 
   2476   const char * prefix = gen_prefix (decl);
   2477   if (prefix == NULL
   2478       || startswith (name, prefix))
   2479     return default_function_section (decl, freq, startup, exit);
   2480 
   2481   name = ACONCAT ((prefix, name, NULL));
   2482   return get_named_section (decl, name, 0);
   2483 }
   2484 
   2485 #undef  TARGET_SECTION_TYPE_FLAGS
   2486 #define TARGET_SECTION_TYPE_FLAGS msp430_section_type_flags
   2487 
   2488 unsigned int
   2489 msp430_section_type_flags (tree decl, const char * name, int reloc)
   2490 {
   2491   if (startswith (name, lower_prefix))
   2492     name += strlen (lower_prefix);
   2493   else if (startswith (name, upper_prefix))
   2494     name += strlen (upper_prefix);
   2495   else if (startswith (name, either_prefix))
   2496     name += strlen (either_prefix);
   2497 
   2498   return default_section_type_flags (decl, name, reloc);
   2499 }
   2500 
   2501 #undef  TARGET_ASM_UNIQUE_SECTION
   2502 #define TARGET_ASM_UNIQUE_SECTION msp430_unique_section
   2503 
   2504 static void
   2505 msp430_unique_section (tree decl, int reloc)
   2506 {
   2507   gcc_assert (decl != NULL_TREE);
   2508 
   2509   /* In large mode we must make sure that interrupt handlers are put into
   2510      low memory as the vector table only accepts 16-bit addresses.  */
   2511   if (TARGET_LARGE && TREE_CODE (decl) == FUNCTION_DECL
   2512       && is_interrupt_func (decl))
   2513     {
   2514       set_decl_section_name (decl, ".lowtext");
   2515       return;
   2516     }
   2517 
   2518   default_unique_section (decl, reloc);
   2519 
   2520   const char * prefix;
   2521 
   2522   if (   TREE_CODE (decl) == STRING_CST
   2523       || TREE_CODE (decl) == CONSTRUCTOR
   2524       || TREE_CODE (decl) == INTEGER_CST
   2525       || TREE_CODE (decl) == VECTOR_CST
   2526       || TREE_CODE (decl) == COMPLEX_CST
   2527       || (prefix = gen_prefix (decl)) == NULL)
   2528     return;
   2529 
   2530   const char * dec_name = DECL_SECTION_NAME (decl);
   2531   char * name = ACONCAT ((prefix, dec_name, NULL));
   2532 
   2533   set_decl_section_name (decl, name);
   2534 }
   2535 
   2536 /* Emit a declaration of a common symbol.
   2537    If a data region is in use then put the symbol into the
   2538    equivalent .bss section instead.
   2539    If LOCAL is 1, then DECL is for a local common variable.  */
   2540 void
   2541 msp430_output_aligned_decl_common (FILE *		  stream,
   2542 				   const tree		  decl,
   2543 				   const char *		  name,
   2544 				   unsigned HOST_WIDE_INT size,
   2545 				   unsigned int		  align,
   2546 				   int local)
   2547 {
   2548   /* Only emit a common symbol if the variable does not have a specific section
   2549      assigned.  */
   2550   if ((msp430_data_region == MSP430_REGION_ANY
   2551        || ((msp430_data_region == MSP430_REGION_LOWER)
   2552 	   && !TARGET_USE_LOWER_REGION_PREFIX))
   2553       && !(decl != NULL_TREE && DECL_SECTION_NAME (decl))
   2554       && !has_attr (ATTR_EITHER, decl)
   2555       && !has_attr (ATTR_LOWER, decl)
   2556       && !has_attr (ATTR_UPPER, decl)
   2557       && !has_attr (ATTR_PERSIST, decl)
   2558       && !has_attr (ATTR_NOINIT, decl))
   2559     {
   2560       if (local)
   2561 	{
   2562 	  fprintf (stream, LOCAL_ASM_OP);
   2563 	  assemble_name (stream, name);
   2564 	  fprintf (stream, "\n");
   2565 	}
   2566       fprintf (stream, COMMON_ASM_OP);
   2567       assemble_name (stream, name);
   2568       fprintf (stream, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
   2569 	       size, align / BITS_PER_UNIT);
   2570     }
   2571   else
   2572     {
   2573       section * sec;
   2574 
   2575       if (decl)
   2576 	sec = msp430_select_section (decl, 0, align);
   2577       else
   2578 	switch (msp430_data_region)
   2579 	  {
   2580 	  case MSP430_REGION_UPPER:
   2581 	    sec = get_named_section (NULL, ".upper.bss", 0);
   2582 	    break;
   2583 	  case MSP430_REGION_LOWER:
   2584 	    sec = get_named_section (NULL, ".lower.bss", 0);
   2585 	    break;
   2586 	  case MSP430_REGION_EITHER:
   2587 	    sec = get_named_section (NULL, ".either.bss", 0);
   2588 	    break;
   2589 	  default:
   2590 	    gcc_unreachable ();
   2591 	  }
   2592       gcc_assert (sec != NULL);
   2593 
   2594       switch_to_section (sec);
   2595       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
   2596       if (!local)
   2597 	{
   2598 	  targetm.asm_out.globalize_label (stream, name);
   2599 	  ASM_WEAKEN_LABEL (stream, name);
   2600 	}
   2601       ASM_OUTPUT_LABEL (stream, name);
   2602       ASM_OUTPUT_SKIP (stream, size ? size : 1);
   2603     }
   2604 }
   2605 
   2606 #undef TARGET_ASM_FILE_END
   2607 #define TARGET_ASM_FILE_END msp430_file_end
   2608 
   2609 /* Emit MSPABI and GNU object attributes.
   2610    Tags and values for MSPABI attributes are:
   2611    OFBA_MSPABI_Tag_ISA		4
   2612      MSP430	1
   2613      MSP430X	2
   2614    OFBA_MSPABI_Tag_Code_Model	6
   2615      Small 	1
   2616      Large	2
   2617    OFBA_MSPABI_Tag_Data_Model	8
   2618      Small 	1
   2619      Large	2
   2620      Restricted	3 (Unused by GNU)
   2621    OFBA_MSPABI_Tag_enum_size	10 (Unused by GNU)
   2622    Note that Code_Model and Data_Model are always equal for GNU.
   2623    We define a new .gnu_attribute to keep track of the data region used.
   2624    Tag_GNU_MSP430_Data_Region	4
   2625      LOWER	1
   2626      ANY	2
   2627    See binutils-gdb/include/elf/msp430.h for the full details.  */
   2628 static void
   2629 msp430_file_end (void)
   2630 {
   2631 #ifdef HAVE_AS_MSPABI_ATTRIBUTE
   2632   /* Enum for tag names.  */
   2633   enum
   2634     {
   2635       OFBA_MSPABI_Tag_ISA = 4,
   2636       OFBA_MSPABI_Tag_Code_Model = 6,
   2637       OFBA_MSPABI_Tag_Data_Model = 8,
   2638       Tag_GNU_MSP430_Data_Region = 4
   2639     };
   2640   /* Enum for tag values.  */
   2641   enum
   2642     {
   2643       OFBA_MSPABI_Val_ISA_MSP430 = 1,
   2644       OFBA_MSPABI_Val_ISA_MSP430X = 2,
   2645       OFBA_MSPABI_Val_Model_Small = 1,
   2646       OFBA_MSPABI_Val_Model_Large = 2,
   2647       Tag_GNU_MSP430_Data_Region_Lower = 1,
   2648       Tag_GNU_MSP430_Data_Region_Any = 2
   2649     };
   2650   /* .mspabi_attribute is a GNU assembler directive only.  The assembler will
   2651      construct a .MSP430.attributes section based on the options it is invoked
   2652      with.  The values it reads from these directives are used for validating
   2653      those options.  */
   2654   const char *msp430_attr = ".mspabi_attribute";
   2655   const char *gnu_attr = ".gnu_attribute";
   2656 
   2657   /* Emit .mspabi_attribute directive for OFBA_MSPABI_Tag_ISA.  */
   2658   fprintf (asm_out_file, "\t%s %d, %d\n", msp430_attr, OFBA_MSPABI_Tag_ISA,
   2659 	   msp430x ? OFBA_MSPABI_Val_ISA_MSP430X : OFBA_MSPABI_Val_ISA_MSP430);
   2660   /* Emit .mspabi_attribute directive for OFBA_MSPABI_Tag_Code_Model.  */
   2661   fprintf (asm_out_file, "\t%s %d, %d\n", msp430_attr,
   2662 	   OFBA_MSPABI_Tag_Code_Model,
   2663 	   TARGET_LARGE ? OFBA_MSPABI_Val_Model_Large
   2664 	   : OFBA_MSPABI_Val_Model_Small);
   2665   /* Emit .mspabi_attribute directive for OFBA_MSPABI_Tag_Data_Model.  */
   2666   fprintf (asm_out_file, "\t%s %d, %d\n", msp430_attr,
   2667 	   OFBA_MSPABI_Tag_Data_Model,
   2668 	   TARGET_LARGE ? OFBA_MSPABI_Val_Model_Large
   2669 	   : OFBA_MSPABI_Val_Model_Small);
   2670 #ifdef HAVE_AS_GNU_ATTRIBUTE
   2671   /* Emit .gnu_attribute directive for Tag_GNU_MSP430_Data_Region.  */
   2672   fprintf (asm_out_file, "\t%s %d, %d\n", gnu_attr, Tag_GNU_MSP430_Data_Region,
   2673 	   msp430_data_region == MSP430_REGION_LOWER
   2674 	   ? Tag_GNU_MSP430_Data_Region_Lower
   2675 	   : Tag_GNU_MSP430_Data_Region_Any);
   2676 #endif
   2677 #endif
   2678 }
   2679 
   2680 enum msp430_builtin
   2681 {
   2682   MSP430_BUILTIN_BIC_SR,
   2683   MSP430_BUILTIN_BIS_SR,
   2684   MSP430_BUILTIN_DELAY_CYCLES,
   2685   MSP430_BUILTIN_max
   2686 };
   2687 
   2688 static GTY(()) tree msp430_builtins[(int) MSP430_BUILTIN_max];
   2689 
   2690 static void
   2691 msp430_init_builtins (void)
   2692 {
   2693   tree void_ftype_int = build_function_type_list (void_type_node,
   2694 						  integer_type_node, NULL);
   2695   tree void_ftype_longlong
   2696     = build_function_type_list (void_type_node, long_long_integer_type_node,
   2697 				NULL);
   2698 
   2699   msp430_builtins[MSP430_BUILTIN_BIC_SR] =
   2700     add_builtin_function ( "__bic_SR_register_on_exit", void_ftype_int,
   2701 			  MSP430_BUILTIN_BIC_SR, BUILT_IN_MD, NULL, NULL_TREE);
   2702 
   2703   msp430_builtins[MSP430_BUILTIN_BIS_SR] =
   2704     add_builtin_function ( "__bis_SR_register_on_exit", void_ftype_int,
   2705 			  MSP430_BUILTIN_BIS_SR, BUILT_IN_MD, NULL, NULL_TREE);
   2706 
   2707   msp430_builtins[MSP430_BUILTIN_DELAY_CYCLES] =
   2708     add_builtin_function ( "__delay_cycles", void_ftype_longlong,
   2709 			  MSP430_BUILTIN_DELAY_CYCLES, BUILT_IN_MD, NULL,
   2710 			  NULL_TREE);
   2711 }
   2712 
   2713 static tree
   2714 msp430_builtin_decl (unsigned code, bool initialize ATTRIBUTE_UNUSED)
   2715 {
   2716   switch (code)
   2717     {
   2718     case MSP430_BUILTIN_BIC_SR:
   2719     case MSP430_BUILTIN_BIS_SR:
   2720     case MSP430_BUILTIN_DELAY_CYCLES:
   2721       return msp430_builtins[code];
   2722     default:
   2723       return error_mark_node;
   2724     }
   2725 }
   2726 
   2727 /* These constants are really register reads, which are faster than
   2728    regular constants.  */
   2729 static int
   2730 cg_magic_constant (HOST_WIDE_INT c)
   2731 {
   2732   switch (c)
   2733     {
   2734     case 0xffff:
   2735     case -1:
   2736     case 0:
   2737     case 1:
   2738     case 2:
   2739     case 4:
   2740     case 8:
   2741       return 1;
   2742     default:
   2743       return 0;
   2744     }
   2745 }
   2746 
   2747 static rtx
   2748 msp430_expand_delay_cycles (rtx arg)
   2749 {
   2750   HOST_WIDE_INT i, c, n;
   2751   /* extra cycles for MSP430X instructions */
   2752 #define CYCX(M,X) (msp430x ? (X) : (M))
   2753 
   2754   if (GET_CODE (arg) != CONST_INT)
   2755     {
   2756       error ("%<__delay_cycles%> only takes constant arguments");
   2757       return NULL_RTX;
   2758     }
   2759 
   2760   c = INTVAL (arg);
   2761 
   2762   if (HOST_BITS_PER_WIDE_INT > 32)
   2763     {
   2764       if (c < 0)
   2765 	{
   2766 	  error ("%<__delay_cycles%> only takes non-negative cycle counts");
   2767 	  return NULL_RTX;
   2768 	}
   2769     }
   2770 
   2771   emit_insn (gen_delay_cycles_start (arg));
   2772 
   2773   /* For 32-bit loops, there's 13(16) + 5(min(x,0x10000) + 6x cycles.  */
   2774   if (c > 3 * 0xffff + CYCX (7, 10))
   2775     {
   2776       n = c;
   2777       /* There's 4 cycles in the short (i>0xffff) loop and 7 in the long
   2778 	 (x<=0xffff) loop.  */
   2779       if (c >= 0x10000 * 7 + CYCX (14, 16))
   2780 	{
   2781 	  i = 0x10000;
   2782 	  c -= CYCX (14, 16) + 7 * 0x10000;
   2783 	  i += c / 4;
   2784 	  c %= 4;
   2785 	  if ((unsigned long long) i > 0xffffffffULL)
   2786 	    {
   2787 	      error ("%<__delay_cycles%> is limited to 32-bit loop counts");
   2788 	      return NULL_RTX;
   2789 	    }
   2790 	}
   2791       else
   2792 	{
   2793 	  i = (c - CYCX (14, 16)) / 7;
   2794 	  c -= CYCX (14, 16) + i * 7;
   2795 	}
   2796 
   2797       if (cg_magic_constant (i & 0xffff))
   2798 	c ++;
   2799       if (cg_magic_constant ((i >> 16) & 0xffff))
   2800 	c ++;
   2801 
   2802       if (msp430x)
   2803 	emit_insn (gen_delay_cycles_32x (GEN_INT (i), GEN_INT (n - c)));
   2804       else
   2805 	emit_insn (gen_delay_cycles_32 (GEN_INT (i), GEN_INT (n - c)));
   2806     }
   2807 
   2808   /* For 16-bit loops, there's 7(10) + 3x cycles - so the max cycles is
   2809      0x30004(7).  */
   2810   if (c > 12)
   2811     {
   2812       n = c;
   2813       i = (c - CYCX (7, 10)) / 3;
   2814       c -= CYCX (7, 10) + i * 3;
   2815 
   2816       if (cg_magic_constant (i))
   2817 	c ++;
   2818 
   2819       if (msp430x)
   2820 	emit_insn (gen_delay_cycles_16x (GEN_INT (i), GEN_INT (n - c)));
   2821       else
   2822 	emit_insn (gen_delay_cycles_16 (GEN_INT (i), GEN_INT (n - c)));
   2823     }
   2824 
   2825   while (c > 1)
   2826     {
   2827       emit_insn (gen_delay_cycles_2 ());
   2828       c -= 2;
   2829     }
   2830 
   2831   if (c)
   2832     {
   2833       emit_insn (gen_delay_cycles_1 ());
   2834       c -= 1;
   2835     }
   2836 
   2837   emit_insn (gen_delay_cycles_end (arg));
   2838 
   2839   return NULL_RTX;
   2840 }
   2841 
   2842 static rtx
   2843 msp430_expand_builtin (tree exp,
   2844 		       rtx target ATTRIBUTE_UNUSED,
   2845 		       rtx subtarget ATTRIBUTE_UNUSED,
   2846 		       machine_mode mode ATTRIBUTE_UNUSED,
   2847 		       int ignore ATTRIBUTE_UNUSED)
   2848 {
   2849   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
   2850   unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
   2851   rtx arg1 = expand_normal (CALL_EXPR_ARG (exp, 0));
   2852 
   2853   if (fcode == MSP430_BUILTIN_DELAY_CYCLES)
   2854     return msp430_expand_delay_cycles (arg1);
   2855 
   2856   if (! msp430_is_interrupt_func ())
   2857     {
   2858       error ("MSP430 built-in functions only work inside interrupt handlers");
   2859       return NULL_RTX;
   2860     }
   2861 
   2862   if (! REG_P (arg1) && ! CONSTANT_P (arg1))
   2863     arg1 = force_reg (mode, arg1);
   2864 
   2865   switch (fcode)
   2866     {
   2867     case MSP430_BUILTIN_BIC_SR:  emit_insn (gen_bic_SR (arg1)); break;
   2868     case MSP430_BUILTIN_BIS_SR:  emit_insn (gen_bis_SR (arg1)); break;
   2869     default:
   2870       internal_error ("bad builtin code");
   2871       break;
   2872     }
   2873   return NULL_RTX;
   2874 }
   2875 
   2876 #undef  TARGET_INIT_BUILTINS
   2877 #define TARGET_INIT_BUILTINS  msp430_init_builtins
   2878 
   2879 #undef  TARGET_EXPAND_BUILTIN
   2880 #define TARGET_EXPAND_BUILTIN msp430_expand_builtin
   2881 
   2882 #undef  TARGET_BUILTIN_DECL
   2883 #define TARGET_BUILTIN_DECL   msp430_builtin_decl
   2884 
   2885 void
   2886 msp430_expand_prologue (void)
   2887 {
   2888   int i, j;
   2889   int fs;
   2890   /* Always use stack_pointer_rtx instead of calling
   2891      rtx_gen_REG ourselves.  Code elsewhere in GCC assumes
   2892      that there is a single rtx representing the stack pointer,
   2893      namely stack_pointer_rtx, and uses == to recognize it.  */
   2894   rtx sp = stack_pointer_rtx;
   2895   rtx p;
   2896 
   2897   if (is_naked_func ())
   2898     {
   2899       /* We must generate some RTX as thread_prologue_and_epilogue_insns()
   2900 	 examines the output of the gen_prologue() function.  */
   2901       emit_insn (gen_rtx_CLOBBER (VOIDmode, GEN_INT (0)));
   2902       return;
   2903     }
   2904 
   2905   emit_insn (gen_prologue_start_marker ());
   2906 
   2907   if (is_critical_func ())
   2908     {
   2909       emit_insn (gen_push_intr_state ());
   2910       emit_insn (gen_disable_interrupts ());
   2911     }
   2912   else if (is_reentrant_func ())
   2913     emit_insn (gen_disable_interrupts ());
   2914 
   2915   if (!cfun->machine->computed)
   2916     msp430_compute_frame_info ();
   2917 
   2918   if (flag_stack_usage_info)
   2919     current_function_static_stack_size = cfun->machine->framesize;
   2920 
   2921   if (crtl->args.pretend_args_size)
   2922     {
   2923       rtx note;
   2924 
   2925       gcc_assert (crtl->args.pretend_args_size == 2);
   2926 
   2927       p = emit_insn (gen_grow_and_swap ());
   2928 
   2929       /* Document the stack decrement...  */
   2930       note = F (gen_rtx_SET (stack_pointer_rtx,
   2931 			     gen_rtx_MINUS (Pmode,
   2932 					    stack_pointer_rtx, GEN_INT (2))));
   2933       add_reg_note (p, REG_FRAME_RELATED_EXPR, note);
   2934 
   2935       /* ...and the establishment of a new location for the return address.  */
   2936       note = F (gen_rtx_SET (gen_rtx_MEM (Pmode,
   2937 					  gen_rtx_PLUS (Pmode,
   2938 							stack_pointer_rtx,
   2939 							GEN_INT (-2))),
   2940 			     pc_rtx));
   2941       add_reg_note (p, REG_CFA_OFFSET, note);
   2942       F (p);
   2943     }
   2944 
   2945   for (i = 15; i >= 4; i--)
   2946     if (cfun->machine->need_to_save[i])
   2947       {
   2948 	/* We need to save COUNT sequential registers starting from regnum
   2949 	   I.  */
   2950 	int seq, count;
   2951 	rtx note;
   2952 
   2953 	for (seq = i - 1; seq >= 4 && cfun->machine->need_to_save[seq]; seq --)
   2954 	  ;
   2955 	count = i - seq;
   2956 
   2957 	if (msp430x)
   2958 	  {
   2959 	    /* Note: with TARGET_LARGE we still use PUSHM as PUSHX.A is two
   2960 	       bytes bigger.  */
   2961 	    p = F (emit_insn (gen_pushm (gen_rtx_REG (Pmode, i),
   2962 					 GEN_INT (count))));
   2963 
   2964 	    /* Document the stack decrement as a result of PUSHM.  */
   2965 	    note = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
   2966 
   2967 	    XVECEXP (note, 0, 0)
   2968 	      = F (gen_rtx_SET (stack_pointer_rtx,
   2969 				gen_rtx_PLUS (Pmode,
   2970 					      stack_pointer_rtx,
   2971 					      GEN_INT (count * (TARGET_LARGE
   2972 								? -4 : -2)))));
   2973 
   2974 	    /* *sp-- = R[i-j] */
   2975 	    /* sp+N	R10
   2976 	       ...
   2977 	       sp	R4  */
   2978 	    for (j = 0; j < count; j ++)
   2979 	      {
   2980 		rtx addr;
   2981 		int ofs = (count - j - 1) * (TARGET_LARGE ? 4 : 2);
   2982 
   2983 		if (ofs)
   2984 		  addr = gen_rtx_PLUS (Pmode, sp, GEN_INT (ofs));
   2985 		else
   2986 		  addr = stack_pointer_rtx;
   2987 
   2988 		XVECEXP (note, 0, j + 1) =
   2989 		  F (gen_rtx_SET (gen_rtx_MEM (Pmode, addr),
   2990 				  gen_rtx_REG (Pmode, i - j)));
   2991 	      }
   2992 
   2993 	    add_reg_note (p, REG_FRAME_RELATED_EXPR, note);
   2994 	    i -= count - 1;
   2995 	  }
   2996 	else
   2997 	  F (emit_insn (gen_push (gen_rtx_REG (Pmode, i))));
   2998       }
   2999 
   3000   if (frame_pointer_needed)
   3001     F (emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), sp));
   3002 
   3003   fs = cfun->machine->framesize_locals + cfun->machine->framesize_outgoing;
   3004 
   3005   increment_stack (- fs);
   3006 
   3007   emit_insn (gen_prologue_end_marker ());
   3008 }
   3009 
   3010 void
   3011 msp430_expand_epilogue (int is_eh)
   3012 {
   3013   int i, j;
   3014   int fs;
   3015   rtx sp = stack_pointer_rtx;
   3016   rtx p;
   3017   int helper_n = 0;
   3018 
   3019   if (is_naked_func ())
   3020     {
   3021       /* We must generate some RTX as thread_prologue_and_epilogue_insns()
   3022 	 examines the output of the gen_epilogue() function.  */
   3023       emit_insn (gen_rtx_CLOBBER (VOIDmode, GEN_INT (0)));
   3024       return;
   3025     }
   3026 
   3027   if (cfun->machine->need_to_save[10])
   3028     {
   3029       /* Check for a helper function.  */
   3030       helper_n = 7; /* For when the loop below never sees a match.  */
   3031       for (i = 9; i >= 4; i--)
   3032 	if (!cfun->machine->need_to_save[i])
   3033 	  {
   3034 	    helper_n = 10 - i;
   3035 	    for (; i >= 4; i--)
   3036 	      if (cfun->machine->need_to_save[i])
   3037 		{
   3038 		  helper_n = 0;
   3039 		  break;
   3040 		}
   3041 	    break;
   3042 	  }
   3043     }
   3044 
   3045   emit_insn (gen_epilogue_start_marker ());
   3046 
   3047   if (cfun->decl && strcmp (IDENTIFIER_POINTER (DECL_NAME (cfun->decl)),
   3048 			    "main") == 0)
   3049     emit_insn (gen_msp430_refsym_need_exit ());
   3050 
   3051   if (is_wakeup_func ())
   3052     /* Clear the SCG1, SCG0, OSCOFF and CPUOFF bits in the saved copy of the
   3053        status register current residing on the stack.  When this function
   3054        executes its RETI instruction the SR will be updated with this saved
   3055        value, thus ensuring that the processor is woken up from any low power
   3056        state in which it may be residing.  */
   3057     emit_insn (gen_bic_SR (GEN_INT (0xf0)));
   3058 
   3059   fs = cfun->machine->framesize_locals + cfun->machine->framesize_outgoing;
   3060 
   3061   increment_stack (fs);
   3062 
   3063   if (is_eh)
   3064     {
   3065       /* We need to add the right "SP" register save just after the
   3066 	 regular ones, so that when we pop it off we're in the EH
   3067 	 return frame, not this one.  This overwrites our own return
   3068 	 address, but we're not going to be returning anyway.  */
   3069       rtx r12 = gen_rtx_REG (Pmode, 12);
   3070       rtx (*addPmode)(rtx, rtx, rtx) = TARGET_LARGE ? gen_addpsi3 : gen_addhi3;
   3071 
   3072       /* R12 will hold the new SP.  */
   3073       i = cfun->machine->framesize_regs;
   3074       emit_move_insn (r12, stack_pointer_rtx);
   3075       emit_insn (addPmode (r12, r12, EH_RETURN_STACKADJ_RTX));
   3076       emit_insn (addPmode (r12, r12, GEN_INT (i)));
   3077       emit_move_insn (gen_rtx_MEM (Pmode, plus_constant (Pmode,
   3078 							 stack_pointer_rtx,
   3079 							 i)), r12);
   3080     }
   3081 
   3082   for (i = 4; i <= 15; i++)
   3083     if (cfun->machine->need_to_save[i])
   3084       {
   3085 	/* We need to restore COUNT sequential registers starting from regnum
   3086 	   I.  */
   3087 	int seq;
   3088 	int count = 1;
   3089 	int helper_used = 0;
   3090 	rtx note, addr;
   3091 
   3092 	if (msp430x)
   3093 	  {
   3094 	    for (seq = i + 1; seq <= 15 && cfun->machine->need_to_save[seq];
   3095 		 seq++)
   3096 	      ;
   3097 	    count = seq - i;
   3098 	  }
   3099 
   3100 	if (msp430x)
   3101 	  {
   3102 	    /* Note: With TARGET_LARGE we still use
   3103 	       POPM as POPX.A is two bytes bigger.  */
   3104 	    p = F (emit_insn (gen_popm (stack_pointer_rtx, GEN_INT (seq - 1),
   3105 					GEN_INT (count))));
   3106 	  }
   3107 	else if (i == 11 - helper_n
   3108 		 && ! msp430_is_interrupt_func ()
   3109 		 && ! is_reentrant_func ()
   3110 		 && ! is_critical_func ()
   3111 		 && crtl->args.pretend_args_size == 0
   3112 		 /* Calling the helper takes as many bytes as the POP;RET
   3113 		    sequence.  */
   3114 		 && helper_n > 1
   3115 		 && !is_eh)
   3116 	  {
   3117 	    p = F (emit_jump_insn (gen_epilogue_helper (GEN_INT (helper_n))));
   3118 	    count = helper_n;
   3119 	    helper_used = 1;
   3120 	  }
   3121 	else
   3122 	  p = F (emit_insn (gen_pop (gen_rtx_REG (Pmode, i))));
   3123 
   3124 	/* Document the stack increment as a result of POPM.  */
   3125 	note = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
   3126 
   3127 	addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
   3128 			     GEN_INT (count * (TARGET_LARGE ? 4 : 2)));
   3129 
   3130 	XVECEXP (note, 0, 0) = F (gen_rtx_SET (stack_pointer_rtx, addr));
   3131 
   3132 
   3133 	/* *sp++ = R[i+j] */
   3134 	/* sp	R4
   3135 	   ...
   3136 	   sp+N	R10.  */
   3137 	for (j = 0; j < count; j++)
   3138 	  {
   3139 	    int ofs = j * (TARGET_LARGE ? 4 : 2);
   3140 
   3141 	    if (ofs)
   3142 	      addr = gen_rtx_PLUS (Pmode, sp, GEN_INT (ofs));
   3143 	    else
   3144 	      addr = stack_pointer_rtx;
   3145 
   3146 	    XVECEXP (note, 0, j + 1)
   3147 	      = F (gen_rtx_SET (gen_rtx_MEM (Pmode, addr),
   3148 				gen_rtx_REG (Pmode, i + j)));
   3149 	  }
   3150 	add_reg_note (p, REG_FRAME_RELATED_EXPR, note);
   3151 	i += count - 1;
   3152 
   3153 	if (helper_used)
   3154 	  return;
   3155       }
   3156 
   3157   if (is_eh)
   3158     {
   3159       /* Also pop SP, which puts us into the EH return frame.  Except
   3160 	 that you can't "pop" sp, you have to just load it off the
   3161 	 stack.  */
   3162       emit_move_insn (stack_pointer_rtx, gen_rtx_MEM (Pmode,
   3163 						      stack_pointer_rtx));
   3164     }
   3165 
   3166   if (crtl->args.pretend_args_size)
   3167     emit_insn (gen_swap_and_shrink ());
   3168 
   3169   if (is_critical_func ())
   3170     emit_insn (gen_pop_intr_state ());
   3171   else if (is_reentrant_func ())
   3172     emit_insn (gen_enable_interrupts ());
   3173 
   3174   emit_jump_insn (gen_msp430_return ());
   3175 }
   3176 
   3177 /* Implements EH_RETURN_STACKADJ_RTX.  Saved and used later in
   3178    m32c_emit_eh_epilogue.  */
   3179 rtx
   3180 msp430_eh_return_stackadj_rtx (void)
   3181 {
   3182   if (!cfun->machine->eh_stack_adjust)
   3183     {
   3184       rtx sa;
   3185 
   3186       sa = gen_rtx_REG (Pmode, 15);
   3187       cfun->machine->eh_stack_adjust = sa;
   3188     }
   3189   return cfun->machine->eh_stack_adjust;
   3190 }
   3191 
   3192 /* This function is called before reload, to "fix" the stack in
   3193    preparation for an EH return.  */
   3194 void
   3195 msp430_expand_eh_return (rtx eh_handler)
   3196 {
   3197   /* These are all Pmode */
   3198   rtx ap, sa, ra, tmp;
   3199 
   3200   ap = arg_pointer_rtx;
   3201   sa = msp430_eh_return_stackadj_rtx ();
   3202   ra = eh_handler;
   3203 
   3204   tmp = ap;
   3205   tmp = gen_rtx_PLUS (Pmode, ap, sa);
   3206   tmp = plus_constant (Pmode, tmp, TARGET_LARGE ? -4 : -2);
   3207   tmp = gen_rtx_MEM (Pmode, tmp);
   3208   emit_move_insn (tmp, ra);
   3209 }
   3210 
   3211 #undef  TARGET_INIT_DWARF_REG_SIZES_EXTRA
   3212 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA msp430_init_dwarf_reg_sizes_extra
   3213 void
   3214 msp430_init_dwarf_reg_sizes_extra (tree address)
   3215 {
   3216   int i;
   3217   rtx addr = expand_normal (address);
   3218   rtx mem = gen_rtx_MEM (BLKmode, addr);
   3219 
   3220   /* This needs to match msp430_unwind_word_mode (above).  */
   3221   if (!msp430x)
   3222     return;
   3223 
   3224   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
   3225     {
   3226       unsigned int dnum = DWARF_FRAME_REGNUM (i);
   3227       unsigned int rnum = DWARF2_FRAME_REG_OUT (dnum, 1);
   3228 
   3229       if (rnum < DWARF_FRAME_REGISTERS)
   3230 	{
   3231 	  HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (QImode);
   3232 
   3233 	  emit_move_insn (adjust_address (mem, QImode, offset),
   3234 			  gen_int_mode (4, QImode));
   3235 	}
   3236     }
   3237 }
   3238 
   3239 /* The MSP430 ABI defines a number of helper functions that should be
   3240    used for, for example, 32-bit shifts.  This function is called to
   3241    emit such a function, using the table above to optimize some
   3242    cases.  */
   3243 void
   3244 msp430_expand_helper (rtx *operands, const char *helper_name,
   3245 		      bool const_variants)
   3246 {
   3247   rtx c, fusage, fsym;
   3248   char *helper_const = NULL;
   3249   int arg1 = 12;
   3250   int arg2 = 13;
   3251   int arg1sz = 1;
   3252   machine_mode arg0mode = GET_MODE (operands[0]);
   3253   machine_mode arg1mode = GET_MODE (operands[1]);
   3254   machine_mode arg2mode = GET_MODE (operands[2]);
   3255   int expand_mpy = startswith (helper_name, "__mspabi_mpy");
   3256   /* This function has been used incorrectly if CONST_VARIANTS is TRUE for a
   3257      hwmpy function.  */
   3258   gcc_assert (!(expand_mpy && const_variants));
   3259 
   3260   if (arg1mode != VOIDmode && arg2mode != VOIDmode)
   3261     /* Modes of arguments must be equal if not constants.  */
   3262     gcc_assert (arg1mode == arg2mode);
   3263 
   3264   if (arg1mode == VOIDmode)
   3265     arg1mode = arg0mode;
   3266   if (arg2mode == VOIDmode)
   3267     arg2mode = arg0mode;
   3268 
   3269   if (arg1mode == SImode)
   3270     {
   3271       arg2 = 14;
   3272       arg1sz = 2;
   3273     }
   3274   else if (arg1mode == DImode)
   3275     {
   3276       arg1 = 8;
   3277       arg1sz = 4;
   3278       arg2 = 12;
   3279     }
   3280 
   3281   /* Use the "const_variant" of a shift library function if requested.
   3282      These are faster, but have larger code size.  */
   3283   if (const_variants
   3284       && CONST_INT_P (operands[2])
   3285       && INTVAL (operands[2]) >= 1
   3286       && INTVAL (operands[2]) <= 15)
   3287     {
   3288       /* Note that the INTVAL is limited in value and length by the conditional
   3289 	 above.  */
   3290       int len = strlen (helper_name) + 4;
   3291       helper_const = (char *) xmalloc (len);
   3292       snprintf (helper_const, len, "%s_%d", helper_name,
   3293 		(int) INTVAL (operands[2]));
   3294     }
   3295 
   3296   /* Setup the arguments to the helper function.  */
   3297   emit_move_insn (gen_rtx_REG (arg1mode, arg1),
   3298 		  operands[1]);
   3299   if (!helper_const)
   3300     emit_move_insn (gen_rtx_REG (arg2mode, arg2),
   3301 		    operands[2]);
   3302 
   3303   if (expand_mpy)
   3304     {
   3305       if (msp430_use_f5_series_hwmult ())
   3306 	fsym = gen_rtx_SYMBOL_REF (VOIDmode, concat (helper_name,
   3307 						     "_f5hw", NULL));
   3308       else if (msp430_use_32bit_hwmult ())
   3309 	{
   3310 	  /* When the arguments are 16-bits, the 16-bit hardware multiplier is
   3311 	     used.  */
   3312 	  if (arg1mode == HImode)
   3313 	    fsym = gen_rtx_SYMBOL_REF (VOIDmode, concat (helper_name,
   3314 							 "_hw", NULL));
   3315 	  else
   3316 	    fsym = gen_rtx_SYMBOL_REF (VOIDmode, concat (helper_name,
   3317 							 "_hw32", NULL));
   3318 	}
   3319       else if (msp430_use_16bit_hwmult ())
   3320 	fsym = gen_rtx_SYMBOL_REF (VOIDmode, concat (helper_name,
   3321 						     "_hw", NULL));
   3322       else
   3323 	fsym = gen_rtx_SYMBOL_REF (VOIDmode, helper_name);
   3324     }
   3325   else
   3326     fsym = gen_rtx_SYMBOL_REF (VOIDmode,
   3327 			       helper_const ? helper_const : helper_name);
   3328 
   3329   c = gen_call_value_internal (gen_rtx_REG (arg0mode, 12), fsym, GEN_INT (0));
   3330 
   3331   c = emit_call_insn (c);
   3332   RTL_CONST_CALL_P (c) = 1;
   3333 
   3334   /* Add register usage information for the arguments to the call.  */
   3335   fusage = NULL;
   3336   use_regs (&fusage, arg1, arg1sz);
   3337   if (!helper_const)
   3338     {
   3339       /* If we are expanding a shift, we only need to use the low register
   3340 	 for the shift amount.  */
   3341       if (!expand_mpy)
   3342 	use_regs (&fusage, arg2, 1);
   3343       else
   3344 	use_regs (&fusage, arg2, arg1sz);
   3345     }
   3346   add_function_usage_to (c, fusage);
   3347 
   3348   emit_move_insn (operands[0],
   3349 		  /* Return value will always start in R12.  */
   3350 		  gen_rtx_REG (arg0mode, 12));
   3351 }
   3352 
   3353 /* Return TRUE if the helper function should be used and FALSE if the shifts
   3354    insns should be emitted inline.  */
   3355 static bool
   3356 use_helper_for_const_shift (machine_mode mode, HOST_WIDE_INT amt)
   3357 {
   3358   const int default_inline_shift = 4;
   3359   /* We initialize the option to 65 so we know if the user set it or not.  */
   3360   int user_set_max_inline = (msp430_max_inline_shift == 65 ? 0 : 1);
   3361   int max_inline = (user_set_max_inline ? msp430_max_inline_shift
   3362 		    : default_inline_shift);
   3363   /* 32-bit shifts are roughly twice as costly as 16-bit shifts so we adjust
   3364      the heuristic accordingly.  */
   3365   int max_inline_32 = max_inline / 2;
   3366 
   3367   if (mode == E_DImode)
   3368     return true;
   3369 
   3370   /* Don't use helpers for these modes on 430X, when optimizing for speed, or
   3371      when emitting a small number of insns.  */
   3372   if ((mode == E_QImode || mode == E_HImode || mode == E_PSImode)
   3373       && (msp430x
   3374 	  /* If the user set max_inline then we always obey that number.
   3375 	     Otherwise we always emit the shifts inline at -O2 and above.  */
   3376 	  || amt <= max_inline
   3377 	  || (!user_set_max_inline
   3378 	      && (optimize >= 2 && !optimize_size))))
   3379     return false;
   3380 
   3381   /* 430 and 430X codegen for SImode shifts is the same.
   3382      Set a hard limit of 15 for the number of shifts that will be emitted
   3383      inline by default, even at -O2 and above, to prevent code size
   3384      explosion.  */
   3385   if (mode == E_SImode
   3386       && (amt <= max_inline_32
   3387 	  || (!user_set_max_inline
   3388 	      && (optimize >= 2 && !optimize_size)
   3389 	      && amt <= 15)))
   3390     return false;
   3391 
   3392   return true;
   3393 }
   3394 
   3395 /* For shift operations which will use an mspabi helper function, setup the
   3396    call to msp430_expand helper.  Return 1 to indicate we have finished with
   3397    this insn and invoke "DONE".
   3398    Otherwise return 0 to indicate the insn should fallthrough.
   3399    Never FAIL.  */
   3400 int
   3401 msp430_expand_shift (enum rtx_code code, machine_mode mode, rtx *operands)
   3402 {
   3403   /* Always use the helper function when the shift amount is not a
   3404      constant.  */
   3405   if (!CONST_INT_P (operands[2])
   3406       || mode == E_DImode
   3407       || use_helper_for_const_shift (mode, INTVAL (operands[2])))
   3408     {
   3409       const char *helper_name = NULL;
   3410       /* The const variants of mspabi shifts have significantly larger code
   3411 	 size than the generic version, so use the generic version if
   3412 	 optimizing for size.  */
   3413       bool const_variant = !optimize_size;
   3414       switch (mode)
   3415 	{
   3416 	case E_HImode:
   3417 	  helper_name = (code == ASHIFT ? "__mspabi_slli" :
   3418 			 (code == ASHIFTRT ? "__mspabi_srai" :
   3419 			  (code == LSHIFTRT ? "__mspabi_srli" :
   3420 			   NULL)));
   3421 	  break;
   3422 	case E_PSImode:
   3423 	  helper_name = (code == ASHIFT ? "__gnu_mspabi_sllp" :
   3424 			 (code == ASHIFTRT ? "__gnu_mspabi_srap" :
   3425 			  (code == LSHIFTRT ? "__gnu_mspabi_srlp" :
   3426 			   NULL)));
   3427 	  /* No const variant for PSImode shifts FIXME.  */
   3428 	  const_variant = false;
   3429 	  break;
   3430 	case E_SImode:
   3431 	  helper_name = (code == ASHIFT ? "__mspabi_slll" :
   3432 			 (code == ASHIFTRT ? "__mspabi_sral" :
   3433 			  (code == LSHIFTRT ? "__mspabi_srll" :
   3434 			   NULL)));
   3435 	  break;
   3436 	case E_DImode:
   3437 	  helper_name = (code == ASHIFT ? "__mspabi_sllll" :
   3438 			 (code == ASHIFTRT ? "__mspabi_srall" :
   3439 			  (code == LSHIFTRT ? "__mspabi_srlll" :
   3440 			   NULL)));
   3441 	  /* No const variant for DImode shifts.  */
   3442 	  const_variant = false;
   3443 	  break;
   3444 	default:
   3445 	  gcc_unreachable ();
   3446 	  break;
   3447 	}
   3448       gcc_assert (helper_name);
   3449       msp430_expand_helper (operands, helper_name, const_variant);
   3450       return 1;
   3451     }
   3452   /* When returning 0, there must be an insn to match the RTL pattern
   3453      otherwise there will be an unrecognizeable insn.  */
   3454   return 0;
   3455 }
   3456 
   3457 /* Helper function to emit a sequence of shift instructions.  The amount of
   3458    shift instructions to emit is in OPERANDS[2].
   3459    For 430 we output copies of identical inline shifts for all modes.
   3460    For 430X it is inneficient to do so for any modes except SI and DI, since we
   3461    can make use of R*M insns or RPT with 430X insns, so this function is only
   3462    used for SImode in that case.  */
   3463 int
   3464 msp430_output_asm_shift_insns (enum rtx_code code, machine_mode mode,
   3465 			       rtx *operands, bool return_length)
   3466 {
   3467   int i;
   3468   int amt;
   3469   int max_shift = GET_MODE_BITSIZE (mode) - 1;
   3470   int length = 0;
   3471 
   3472   gcc_assert (CONST_INT_P (operands[2]));
   3473   amt = INTVAL (operands[2]);
   3474 
   3475   if (amt == 0 || amt > max_shift)
   3476     {
   3477       if (return_length)
   3478 	return 0;
   3479       switch (code)
   3480 	{
   3481 	case ASHIFT:
   3482 	  output_asm_insn ("# ignored undefined behaviour left shift "
   3483 			   "of %1 by %2", operands);
   3484 	  break;
   3485 	case ASHIFTRT:
   3486 	  output_asm_insn ("# ignored undefined behaviour arithmetic right "
   3487 			   "shift of %1 by %2", operands);
   3488 	  break;
   3489 	case LSHIFTRT:
   3490 	  output_asm_insn ("# ignored undefined behaviour logical right shift "
   3491 			   "of %1 by %2", operands);
   3492 	  break;
   3493 	default:
   3494 	  gcc_unreachable ();
   3495 	}
   3496       return 0;
   3497     }
   3498 
   3499   if (code == ASHIFT)
   3500     {
   3501       if (!msp430x && mode == HImode)
   3502 	{
   3503 	  if (return_length)
   3504 	    length = 2 + (MEM_P (operands[0]) ? 2 : 0);
   3505 	  else
   3506 	    for (i = 0; i < amt; i++)
   3507 	      output_asm_insn ("RLA.W\t%0", operands);
   3508 	}
   3509       else if (mode == SImode)
   3510 	{
   3511 	  if (return_length)
   3512 	    length = 4 + (MEM_P (operands[0]) ? 4 : 0)
   3513 	      + (4 * msp430x_insn_required (operands[0]));
   3514 	  else
   3515 	    for (i = 0; i < amt; i++)
   3516 	      output_asm_insn ("RLA%X0.W\t%L0 { RLC%X0.W\t%H0", operands);
   3517 	}
   3518       else
   3519 	/* Catch unhandled cases.  */
   3520 	gcc_unreachable ();
   3521     }
   3522   else if (code == ASHIFTRT)
   3523     {
   3524       if (!msp430x && mode == HImode)
   3525 	{
   3526 	  if (return_length)
   3527 	    length = 2 + (MEM_P (operands[0]) ? 2 : 0);
   3528 	  else
   3529 	    for (i = 0; i < amt; i++)
   3530 	      output_asm_insn ("RRA.W\t%0", operands);
   3531 	}
   3532       else if (mode == SImode)
   3533 	{
   3534 	  if (return_length)
   3535 	    length = 4 + (MEM_P (operands[0]) ? 4 : 0)
   3536 	      + (4 * msp430x_insn_required (operands[0]));
   3537 	  else
   3538 	    for (i = 0; i < amt; i++)
   3539 	      output_asm_insn ("RRA%X0.W\t%H0 { RRC%X0.W\t%L0", operands);
   3540 	}
   3541       else
   3542 	gcc_unreachable ();
   3543     }
   3544   else if (code == LSHIFTRT)
   3545     {
   3546       if (!msp430x && mode == HImode)
   3547 	{
   3548 	  if (return_length)
   3549 	    length = 4 + (MEM_P (operands[0]) ? 2 : 0);
   3550 	  else
   3551 	    for (i = 0; i < amt; i++)
   3552 	      output_asm_insn ("CLRC { RRC.W\t%0", operands);
   3553 	}
   3554       else if (mode == SImode)
   3555 	{
   3556 	  if (return_length)
   3557 	    length = 6 + (MEM_P (operands[0]) ? 4 : 0)
   3558 	      + (4 * msp430x_insn_required (operands[0]));
   3559 	  else
   3560 	    for (i = 0; i < amt; i++)
   3561 	      output_asm_insn ("CLRC { RRC%X0.W\t%H0 { RRC%X0.W\t%L0",
   3562 			       operands);
   3563 	}
   3564       /* FIXME: Why doesn't "RRUX.W\t%H0 { RRC%X0.W\t%L0" work for msp430x?
   3565 	 It causes execution timeouts e.g. pr41963.c.  */
   3566 #if 0
   3567       else if (msp430x && mode == SImode)
   3568 	{
   3569 	  if (return_length)
   3570 	    length = 2;
   3571 	  else
   3572 	    for (i = 0; i < amt; i++)
   3573 	      output_asm_insn ("RRUX.W\t%H0 { RRC%X0.W\t%L0", operands);
   3574 	}
   3575 #endif
   3576       else
   3577 	gcc_unreachable ();
   3578     }
   3579   return length * amt;
   3580 }
   3581 
   3582 /* Called by cbranch<mode>4 to coerce operands into usable forms.  */
   3583 void
   3584 msp430_fixup_compare_operands (machine_mode my_mode, rtx * operands)
   3585 {
   3586   /* constants we're looking for, not constants which are allowed.  */
   3587   int const_op_idx = 1;
   3588 
   3589   if (msp430_reversible_cmp_operator (operands[0], VOIDmode))
   3590     const_op_idx = 2;
   3591 
   3592   if (GET_CODE (operands[const_op_idx]) != REG
   3593       && GET_CODE (operands[const_op_idx]) != MEM)
   3594     operands[const_op_idx] = copy_to_mode_reg (my_mode, operands[const_op_idx]);
   3595 }
   3596 
   3597 /* Simplify_gen_subreg() doesn't handle memory references the way we
   3598    need it to below, so we use this function for when we must get a
   3599    valid subreg in a "natural" state.  */
   3600 rtx
   3601 msp430_subreg (machine_mode mode, rtx r, machine_mode omode, int byte)
   3602 {
   3603   rtx rv;
   3604   gcc_assert (mode == HImode);
   3605 
   3606   if (GET_CODE (r) == SUBREG
   3607       && SUBREG_BYTE (r) == 0)
   3608     {
   3609       rtx ireg = SUBREG_REG (r);
   3610       machine_mode imode = GET_MODE (ireg);
   3611 
   3612       /* special case for (HI (SI (PSI ...), 0)) */
   3613       if (imode == PSImode
   3614 	  && mode == HImode
   3615 	  && byte == 0)
   3616 	rv = gen_rtx_SUBREG (mode, ireg, byte);
   3617       else
   3618 	rv = simplify_gen_subreg (mode, ireg, imode, byte);
   3619     }
   3620   else if (GET_CODE (r) == MEM)
   3621     {
   3622       /* When byte == 2, we can be certain that we were already called with an
   3623 	 identical rtx with byte == 0.  So we don't need to do anything to
   3624 	 get a 2 byte offset of a (mem (post_inc)) rtx, since the address has
   3625 	 already been offset by the post_inc itself.  */
   3626       if (GET_CODE (XEXP (r, 0)) == POST_INC && byte == 2)
   3627 	byte = 0;
   3628       rv = adjust_address (r, mode, byte);
   3629     }
   3630   else if (GET_CODE (r) == SYMBOL_REF
   3631 	   && (byte == 0 || byte == 2)
   3632 	   && mode == HImode)
   3633     {
   3634       rv = gen_rtx_ZERO_EXTRACT (HImode, r, GEN_INT (16), GEN_INT (8*byte));
   3635       rv = gen_rtx_CONST (HImode, r);
   3636     }
   3637   else
   3638     rv = simplify_gen_subreg (mode, r, omode, byte);
   3639 
   3640   if (!rv)
   3641     gcc_unreachable ();
   3642 
   3643   return rv;
   3644 }
   3645 
   3646 int
   3647 msp430_split_addsi (rtx *operands)
   3648 {
   3649   operands[3] = msp430_subreg (HImode, operands[0], SImode, 0);
   3650   operands[4] = msp430_subreg (HImode, operands[1], SImode, 0);
   3651   operands[5] = msp430_subreg (HImode, operands[2], SImode, 0);
   3652   operands[6] = msp430_subreg (HImode, operands[0], SImode, 2);
   3653   operands[7] = msp430_subreg (HImode, operands[1], SImode, 2);
   3654   operands[8] = msp430_subreg (HImode, operands[2], SImode, 2);
   3655 
   3656   /* BZ 64160: Do not use this splitter when the dest partially overlaps the
   3657      source.  */
   3658   if (reg_overlap_mentioned_p (operands[3], operands[7])
   3659       || reg_overlap_mentioned_p (operands[3], operands[8]))
   3660     return 1;
   3661 
   3662   if (GET_CODE (operands[5]) == CONST_INT)
   3663     operands[9] = GEN_INT (INTVAL (operands[5]) & 0xffff);
   3664   /* Handle post_inc, for example:
   3665      (set (reg:SI)
   3666 	  (plus:SI (reg:SI)
   3667 		   (mem:SI (post_inc:PSI (reg:PSI))))).  */
   3668   else if (MEM_P (operands[5]) && GET_CODE (XEXP (operands[5], 0)) == POST_INC)
   3669     {
   3670       /* Strip out the post_inc from (mem (post_inc (reg))).  */
   3671       operands[9] = XEXP (XEXP (operands[5], 0), 0);
   3672       operands[9] = gen_rtx_MEM (HImode, operands[9]);
   3673       /* Then zero extend as normal.  */
   3674       operands[9] = gen_rtx_ZERO_EXTEND (SImode, operands[9]);
   3675     }
   3676   else
   3677     operands[9] = gen_rtx_ZERO_EXTEND (SImode, operands[5]);
   3678   return 0;
   3679 }
   3680 
   3681 /* Called by movsi_x to generate the HImode operands.  */
   3682 void
   3683 msp430_split_movsi (rtx *operands)
   3684 {
   3685   rtx op00, op02, op10, op12;
   3686 
   3687   op00 = msp430_subreg (HImode, operands[0], SImode, 0);
   3688   op02 = msp430_subreg (HImode, operands[0], SImode, 2);
   3689 
   3690   if (GET_CODE (operands[1]) == CONST
   3691       || GET_CODE (operands[1]) == SYMBOL_REF)
   3692     {
   3693       op10 = gen_rtx_ZERO_EXTRACT (HImode, operands[1], GEN_INT (16),
   3694 				   GEN_INT (0));
   3695       op10 = gen_rtx_CONST (HImode, op10);
   3696       op12 = gen_rtx_ZERO_EXTRACT (HImode, operands[1], GEN_INT (16),
   3697 				   GEN_INT (16));
   3698       op12 = gen_rtx_CONST (HImode, op12);
   3699     }
   3700   else
   3701     {
   3702       op10 = msp430_subreg (HImode, operands[1], SImode, 0);
   3703       op12 = msp430_subreg (HImode, operands[1], SImode, 2);
   3704     }
   3705 
   3706   if (rtx_equal_p (operands[0], operands[1]))
   3707     {
   3708       operands[2] = op02;
   3709       operands[4] = op12;
   3710       operands[3] = op00;
   3711       operands[5] = op10;
   3712     }
   3713   else if (rtx_equal_p (op00, op12)
   3714 	   /* Catch the case where we are loading (rN, rN+1) from mem (rN).  */
   3715 	   || (REG_P (op00) && reg_mentioned_p (op00, op10))
   3716 	   /* Or storing (rN) into mem (rN).  */
   3717 	   || (REG_P (op10) && reg_mentioned_p (op10, op00)))
   3718     {
   3719       operands[2] = op02;
   3720       operands[4] = op12;
   3721       operands[3] = op00;
   3722       operands[5] = op10;
   3723     }
   3724   else
   3725     {
   3726       operands[2] = op00;
   3727       operands[4] = op10;
   3728       operands[3] = op02;
   3729       operands[5] = op12;
   3730     }
   3731 }
   3732 
   3733 
   3734 /* The MSPABI specifies the names of various helper functions, many of
   3736    which are compatible with GCC's helpers.  This table maps the GCC
   3737    name to the MSPABI name.  */
   3738 static const struct
   3739 {
   3740   char const * const gcc_name;
   3741   char const * const ti_name;
   3742 }
   3743 helper_function_name_mappings[] =
   3744   {
   3745     /* Floating point to/from integer conversions.  */
   3746     { "__truncdfsf2", "__mspabi_cvtdf" },
   3747     { "__extendsfdf2", "__mspabi_cvtfd" },
   3748     { "__fixdfhi", "__mspabi_fixdi" },
   3749     { "__fixdfsi", "__mspabi_fixdli" },
   3750     { "__fixdfdi", "__mspabi_fixdlli" },
   3751     { "__fixunsdfhi", "__mspabi_fixdu" },
   3752     { "__fixunsdfsi", "__mspabi_fixdul" },
   3753     { "__fixunsdfdi", "__mspabi_fixdull" },
   3754     { "__fixsfhi", "__mspabi_fixfi" },
   3755     { "__fixsfsi", "__mspabi_fixfli" },
   3756     { "__fixsfdi", "__mspabi_fixflli" },
   3757     { "__fixunsfhi", "__mspabi_fixfu" },
   3758     { "__fixunsfsi", "__mspabi_fixful" },
   3759     { "__fixunsfdi", "__mspabi_fixfull" },
   3760     { "__floathisf", "__mspabi_fltif" },
   3761     { "__floatsisf", "__mspabi_fltlif" },
   3762     { "__floatdisf", "__mspabi_fltllif" },
   3763     { "__floathidf", "__mspabi_fltid" },
   3764     { "__floatsidf", "__mspabi_fltlid" },
   3765     { "__floatdidf", "__mspabi_fltllid" },
   3766     { "__floatunhisf", "__mspabi_fltuf" },
   3767     { "__floatunsisf", "__mspabi_fltulf" },
   3768     { "__floatundisf", "__mspabi_fltullf" },
   3769     { "__floatunhidf", "__mspabi_fltud" },
   3770     { "__floatunsidf", "__mspabi_fltuld" },
   3771     { "__floatundidf", "__mspabi_fltulld" },
   3772 
   3773     /* Floating point comparisons.  */
   3774     /* GCC uses individual functions for each comparison, TI uses one
   3775        compare <=> function.  */
   3776 
   3777     /* Floating point arithmetic.  */
   3778     { "__adddf3", "__mspabi_addd" },
   3779     { "__addsf3", "__mspabi_addf" },
   3780     { "__divdf3", "__mspabi_divd" },
   3781     { "__divsf3", "__mspabi_divf" },
   3782     { "__muldf3", "__mspabi_mpyd" },
   3783     { "__mulsf3", "__mspabi_mpyf" },
   3784     { "__subdf3", "__mspabi_subd" },
   3785     { "__subsf3", "__mspabi_subf" },
   3786     /* GCC does not use helper functions for negation.  */
   3787 
   3788     /* Integer multiply, divide, remainder.  */
   3789     { "__mulhi3", "__mspabi_mpyi" },
   3790     { "__mulsi3", "__mspabi_mpyl" },
   3791     { "__muldi3", "__mspabi_mpyll" },
   3792 #if 0
   3793     /* Clarify signed vs unsigned first.  */
   3794     { "__mulhisi3", "__mspabi_mpysl" }, /* gcc doesn't use widening multiply
   3795 					   (yet?) */
   3796     { "__mulsidi3", "__mspabi_mpysll" }, /* gcc doesn't use widening multiply
   3797 					    (yet?) */
   3798 #endif
   3799 
   3800     { "__divhi3", "__mspabi_divi" },
   3801     { "__divsi3", "__mspabi_divli" },
   3802     { "__divdi3", "__mspabi_divlli" },
   3803     { "__udivhi3", "__mspabi_divu" },
   3804     { "__udivsi3", "__mspabi_divul" },
   3805     { "__udivdi3", "__mspabi_divull" },
   3806     { "__modhi3", "__mspabi_remi" },
   3807     { "__modsi3", "__mspabi_remli" },
   3808     { "__moddi3", "__mspabi_remlli" },
   3809     { "__umodhi3", "__mspabi_remu" },
   3810     { "__umodsi3", "__mspabi_remul" },
   3811     { "__umoddi3", "__mspabi_remull" },
   3812 
   3813     /* Bitwise operations.  */
   3814     /* Rotation - no rotation support yet.  */
   3815     /* Logical left shift - gcc already does these itself.  */
   3816     /* Arithmetic left shift - gcc already does these itself.  */
   3817     /* Arithmetic right shift - gcc already does these itself.  */
   3818 
   3819     { NULL, NULL }
   3820   };
   3821 
   3822 /* Returns true if the current MCU supports an F5xxx series
   3823    hardware multiper.  */
   3824 
   3825 bool
   3826 msp430_use_f5_series_hwmult (void)
   3827 {
   3828   static const char * cached_match = NULL;
   3829   static bool cached_result;
   3830 
   3831   if (msp430_hwmult_type == MSP430_HWMULT_F5SERIES)
   3832     return true;
   3833 
   3834   if (target_mcu == NULL || msp430_hwmult_type != MSP430_HWMULT_AUTO)
   3835     return false;
   3836 
   3837   if (target_mcu == cached_match)
   3838     return cached_result;
   3839 
   3840   cached_match = target_mcu;
   3841 
   3842   if (strncasecmp (target_mcu, "msp430f5", 8) == 0)
   3843     return cached_result = true;
   3844   if (strncasecmp (target_mcu, "msp430fr5", 9) == 0)
   3845     return cached_result = true;
   3846   if (strncasecmp (target_mcu, "msp430f6", 8) == 0)
   3847     return cached_result = true;
   3848 
   3849   msp430_extract_mcu_data (target_mcu);
   3850 
   3851   if (extracted_mcu_data.name != NULL)
   3852     return cached_result = extracted_mcu_data.hwmpy == 8;
   3853 
   3854   return cached_result = false;
   3855 }
   3856 
   3857 /* Returns true if the current MCU has a second generation
   3858    32-bit hardware multiplier.  */
   3859 
   3860 static bool
   3861 msp430_use_32bit_hwmult (void)
   3862 {
   3863   static const char * cached_match = NULL;
   3864   static bool cached_result;
   3865 
   3866   if (msp430_hwmult_type == MSP430_HWMULT_LARGE)
   3867     return true;
   3868 
   3869   if (target_mcu == NULL || msp430_hwmult_type != MSP430_HWMULT_AUTO)
   3870     return false;
   3871 
   3872   if (target_mcu == cached_match)
   3873     return cached_result;
   3874 
   3875   cached_match = target_mcu;
   3876 
   3877   msp430_extract_mcu_data (target_mcu);
   3878   if (extracted_mcu_data.name != NULL)
   3879     return cached_result = extracted_mcu_data.hwmpy == 4;
   3880 
   3881   return cached_result = false;
   3882 }
   3883 
   3884 /* Returns true if the current MCU has a first generation
   3885    16-bit hardware multiplier.  */
   3886 
   3887 static bool
   3888 msp430_use_16bit_hwmult (void)
   3889 {
   3890   static const char * cached_match = NULL;
   3891   static bool	      cached_result;
   3892 
   3893   if (msp430_hwmult_type == MSP430_HWMULT_SMALL)
   3894     return true;
   3895 
   3896   if (target_mcu == NULL || msp430_hwmult_type != MSP430_HWMULT_AUTO)
   3897     return false;
   3898 
   3899   if (target_mcu == cached_match)
   3900     return cached_result;
   3901 
   3902   cached_match = target_mcu;
   3903 
   3904   msp430_extract_mcu_data (target_mcu);
   3905   if (extracted_mcu_data.name != NULL)
   3906     return cached_result = (extracted_mcu_data.hwmpy == 1
   3907 			    || extracted_mcu_data.hwmpy == 2);
   3908 
   3909   return cached_result = false;
   3910 }
   3911 
   3912 /* Returns true if the current MCU does not have a
   3913    hardware multiplier of any kind.  */
   3914 
   3915 bool
   3916 msp430_has_hwmult (void)
   3917 {
   3918   static const char * cached_match = NULL;
   3919   static bool cached_result;
   3920 
   3921   if (msp430_hwmult_type == MSP430_HWMULT_NONE)
   3922     return false;
   3923 
   3924   /* TRUE for any other explicit hwmult specified.  */
   3925   if (msp430_hwmult_type != MSP430_HWMULT_AUTO)
   3926     return true;
   3927 
   3928   /* Now handle -mhwmult=auto.  */
   3929   if (target_mcu == NULL)
   3930     return false;
   3931 
   3932   if (target_mcu == cached_match)
   3933     return cached_result;
   3934 
   3935   cached_match = target_mcu;
   3936 
   3937   msp430_extract_mcu_data (target_mcu);
   3938   if (extracted_mcu_data.name != NULL)
   3939     return cached_result = extracted_mcu_data.hwmpy != 0;
   3940 
   3941   /* If we do not recognise the MCU name, we assume that it does not support
   3942      any kind of hardware multiply - this is the safest assumption to make.  */
   3943   return cached_result = false;
   3944 }
   3945 
   3946 /* This function does the same as the default, but it will replace GCC
   3947    function names with the MSPABI-specified ones.  */
   3948 
   3949 void
   3950 msp430_output_labelref (FILE *file, const char *name)
   3951 {
   3952   int i;
   3953 
   3954   for (i = 0; helper_function_name_mappings[i].gcc_name; i++)
   3955     if (strcmp (helper_function_name_mappings[i].gcc_name, name) == 0)
   3956       {
   3957 	name = helper_function_name_mappings[i].ti_name;
   3958 	break;
   3959       }
   3960 
   3961   if (user_label_prefix[0] != 0)
   3962     fputs (user_label_prefix, file);
   3963 
   3964   fputs (name, file);
   3965 }
   3966 
   3967 /* Common code for msp430_print_operand...  */
   3968 
   3969 static void
   3970 msp430_print_operand_raw (FILE * file, rtx op)
   3971 {
   3972   HOST_WIDE_INT i;
   3973 
   3974   switch (GET_CODE (op))
   3975     {
   3976     case REG:
   3977       fprintf (file, "%s", reg_names[REGNO (op)]);
   3978       break;
   3979 
   3980     case CONST_INT:
   3981       i = INTVAL (op);
   3982       if (TARGET_ASM_HEX)
   3983 	fprintf (file, "%#" HOST_WIDE_INT_PRINT "x", i);
   3984       else
   3985 	fprintf (file, "%" HOST_WIDE_INT_PRINT "d", i);
   3986       break;
   3987 
   3988     case CONST:
   3989     case PLUS:
   3990     case MINUS:
   3991     case SYMBOL_REF:
   3992     case LABEL_REF:
   3993       output_addr_const (file, op);
   3994       break;
   3995 
   3996     default:
   3997       print_rtl (file, op);
   3998       break;
   3999     }
   4000 }
   4001 
   4002 #undef  TARGET_ASM_ALIGNED_PSI_OP
   4003 #define TARGET_ASM_ALIGNED_PSI_OP "\t.long\t"
   4004 #undef  TARGET_ASM_UNALIGNED_PSI_OP
   4005 #define TARGET_ASM_UNALIGNED_PSI_OP TARGET_ASM_ALIGNED_PSI_OP
   4006 
   4007 #undef  TARGET_PRINT_OPERAND_ADDRESS
   4008 #define TARGET_PRINT_OPERAND_ADDRESS	msp430_print_operand_addr
   4009 
   4010 /* Output to stdio stream FILE the assembler syntax for an
   4011    instruction operand that is a memory reference whose address
   4012    is ADDR.  */
   4013 
   4014 static void
   4015 msp430_print_operand_addr (FILE * file, machine_mode /*mode*/, rtx addr)
   4016 {
   4017   switch (GET_CODE (addr))
   4018     {
   4019     case PLUS:
   4020       msp430_print_operand_raw (file, XEXP (addr, 1));
   4021       gcc_assert (REG_P (XEXP (addr, 0)));
   4022       fprintf (file, "(%s)", reg_names[REGNO (XEXP (addr, 0))]);
   4023       return;
   4024 
   4025     case REG:
   4026       fprintf (file, "@");
   4027       break;
   4028 
   4029     case POST_INC:
   4030       fprintf (file, "@%s+", reg_names[REGNO (XEXP (addr, 0))]);
   4031       return;
   4032 
   4033     case CONST:
   4034     case CONST_INT:
   4035     case SYMBOL_REF:
   4036     case LABEL_REF:
   4037       fprintf (file, "&");
   4038       break;
   4039 
   4040     default:
   4041       break;
   4042     }
   4043 
   4044   msp430_print_operand_raw (file, addr);
   4045 }
   4046 
   4047 /* We can only allow signed 15-bit indexes i.e. +/-32K.  */
   4048 static bool
   4049 msp430_check_index_not_high_mem (rtx op)
   4050 {
   4051   if (CONST_INT_P (op)
   4052       && IN_RANGE (INTVAL (op), HOST_WIDE_INT_M1U << 15, (1 << 15) - 1))
   4053     return true;
   4054   return false;
   4055 }
   4056 
   4057 /* If this returns true, we don't need a 430X insn.  */
   4058 static bool
   4059 msp430_check_plus_not_high_mem (rtx op)
   4060 {
   4061   if (GET_CODE (op) != PLUS)
   4062     return false;
   4063   rtx op0 = XEXP (op, 0);
   4064   rtx op1 = XEXP (op, 1);
   4065   if (SYMBOL_REF_P (op0)
   4066       && (SYMBOL_REF_FLAGS (op0) & SYMBOL_FLAG_LOW_MEM)
   4067       && msp430_check_index_not_high_mem (op1))
   4068     return true;
   4069   return false;
   4070 }
   4071 
   4072 /* Determine whether an RTX is definitely not a MEM referencing an address in
   4073    the upper memory region.  Returns true if we've decided the address will be
   4074    in the lower memory region, or the RTX is not a MEM.  Returns false
   4075    otherwise.
   4076    The Ys constraint will catch (mem (plus (const/reg)) but we catch cases
   4077    involving a symbol_ref here.  */
   4078 bool
   4079 msp430_op_not_in_high_mem (rtx op)
   4080 {
   4081   rtx op0;
   4082 
   4083   if (!TARGET_LARGE || !MEM_P (op))
   4084     return true;
   4085 
   4086   op0 = XEXP (op, 0);
   4087 
   4088   if (SYMBOL_REF_P (op0) && (SYMBOL_REF_FLAGS (op0) & SYMBOL_FLAG_LOW_MEM))
   4089     /* msp430_encode_section_info decided this mem will be in lower
   4090        memory.  */
   4091     return true;
   4092 
   4093   /* Check possibilites for (mem (plus)).
   4094      e.g. (mem (const (plus ((symbol_ref) (const_int))))) : &addr+2.  */
   4095   if (msp430_check_plus_not_high_mem (op0)
   4096       || ((GET_CODE (op0) == CONST)
   4097 	  && msp430_check_plus_not_high_mem (XEXP (op0, 0))))
   4098     return true;
   4099 
   4100   /* An absolute 16-bit address is allowed.  */
   4101   if ((CONST_INT_P (op0) && (IN_RANGE (INTVAL (op0), 0, (1 << 16) - 1))))
   4102     return true;
   4103 
   4104   /* Return false when undecided.  */
   4105   return false;
   4106 }
   4107 
   4108 /* Based on the operand OP, is a 430X insn required to handle it?
   4109    There are only 3 conditions for which a 430X insn is required:
   4110    - PSImode operand
   4111    - memory reference to a symbol which could be in upper memory
   4112      (so its address is > 0xFFFF)
   4113    - absolute address which has VOIDmode, i.e. (mem:HI (const_int))
   4114    Use a 430 insn if none of these conditions are true.  */
   4115 bool
   4116 msp430x_insn_required (rtx op)
   4117 {
   4118   return (GET_MODE (op) == PSImode
   4119 	  || !msp430_op_not_in_high_mem (op));
   4120 }
   4121 
   4122 #undef  TARGET_PRINT_OPERAND
   4123 #define TARGET_PRINT_OPERAND		msp430_print_operand
   4124 
   4125 /* A   Select low 16-bits of the constant/register/memory operand.
   4126    B   Select high 16-bits of the constant/register/memory
   4127        operand.
   4128    C   Select bits 32-47 of the constant/register/memory operand.
   4129    D   Select bits 48-63 of the constant/register/memory operand.
   4130    H   Equivalent to @code{B} (for backwards compatibility).
   4131    I   Print the inverse (logical @code{NOT}) of the constant
   4132        value.
   4133    J   Print an integer without a @code{#} prefix.
   4134    L   Equivalent to @code{A} (for backwards compatibility).
   4135    O   Offset of the current frame from the top of the stack.
   4136    Q   Use the @code{A} instruction postfix.
   4137    R   Inverse of condition code, for unsigned comparisons.
   4138    W   Subtract 16 from the constant value.
   4139    X   Use the @code{X} instruction postfix.
   4140    Y   Subtract 4 from the constant value.
   4141    Z   Subtract 1 from the constant value.
   4142    b   Append @code{.B}, @code{.W} or @code{.A} to the
   4143        instruction, depending on the mode.
   4144    d   Offset 1 byte of a memory reference or constant value.
   4145    e   Offset 3 bytes of a memory reference or constant value.
   4146    f   Offset 5 bytes of a memory reference or constant value.
   4147    g   Offset 7 bytes of a memory reference or constant value.
   4148    p   Print the value of 2, raised to the power of the given
   4149        constant.  Used to select the specified bit position.
   4150    r   Inverse of condition code, for signed comparisons.
   4151    x   Equivialent to @code{X}, but only for pointers.  */
   4152 
   4153 static void
   4154 msp430_print_operand (FILE * file, rtx op, int letter)
   4155 {
   4156   rtx addr;
   4157   /* These are used by the 'A', 'B', 'C', 'D', 'd', 'e', 'f' and 'g' modifiers
   4158      to describe how to process the operand to get the requested value.  */
   4159   int mem_off = 0;
   4160   int reg_off = 0;
   4161   int const_shift = 0;
   4162 
   4163   /* We can't use c, n, a, or l.  */
   4164   switch (letter)
   4165     {
   4166     case 'Z':
   4167       gcc_assert (CONST_INT_P (op));
   4168       /* Print the constant value, less one.  */
   4169       fprintf (file, "#%ld", (long) (INTVAL (op) - 1));
   4170       return;
   4171     case 'Y':
   4172       gcc_assert (CONST_INT_P (op));
   4173       /* Print the constant value, less four.  */
   4174       fprintf (file, "#%ld", (long) (INTVAL (op) - 4));
   4175       return;
   4176     case 'W':
   4177       gcc_assert (CONST_INT_P (op));
   4178       /* Print the constant value, less 16.  */
   4179       fprintf (file, "#%ld", (long) (INTVAL (op) - 16));
   4180       return;
   4181     case 'I':
   4182       if (GET_CODE (op) == CONST_INT)
   4183 	{
   4184 	  /* Inverse of constants */
   4185 	  int i = INTVAL (op);
   4186 	  fprintf (file, "%d", ~i);
   4187 	  return;
   4188 	}
   4189       op = XEXP (op, 0);
   4190       break;
   4191     case 'r': /* Conditional jump where the condition is reversed.  */
   4192       switch (GET_CODE (op))
   4193 	{
   4194 	case EQ: fprintf (file, "NE"); break;
   4195 	case NE: fprintf (file, "EQ"); break;
   4196 	case GEU: fprintf (file, "LO"); break;
   4197 	case LTU: fprintf (file, "HS"); break;
   4198 	case GE: fprintf (file, "L"); break;
   4199 	case LT: fprintf (file, "GE"); break;
   4200 	  /* Assume these have reversed operands.  */
   4201 	case GTU: fprintf (file, "HS"); break;
   4202 	case LEU: fprintf (file, "LO"); break;
   4203 	case GT: fprintf (file, "GE"); break;
   4204 	case LE: fprintf (file, "L"); break;
   4205 	default:
   4206 	  msp430_print_operand_raw (file, op);
   4207 	  break;
   4208 	}
   4209       return;
   4210     case 'R': /* Conditional jump where the operands are reversed.  */
   4211       switch (GET_CODE (op))
   4212 	{
   4213 	case GTU: fprintf (file, "LO"); break;
   4214 	case LEU: fprintf (file, "HS"); break;
   4215 	case GT: fprintf (file, "L"); break;
   4216 	case LE: fprintf (file, "GE"); break;
   4217 	default:
   4218 	  msp430_print_operand_raw (file, op);
   4219 	  break;
   4220 	}
   4221       return;
   4222     case 'p': /* Bit position.  0 == 0x01, 3 = 0x08 etc.  */
   4223       gcc_assert (CONST_INT_P (op));
   4224       fprintf (file, "#%d", 1 << INTVAL (op));
   4225       return;
   4226     case 'b':
   4227       switch (GET_MODE (op))
   4228 	{
   4229 	case E_QImode: fprintf (file, ".B"); return;
   4230 	case E_HImode: fprintf (file, ".W"); return;
   4231 	case E_PSImode: fprintf (file, ".A"); return;
   4232 	case E_SImode: fprintf (file, ".A"); return;
   4233 	default:
   4234 	  return;
   4235 	}
   4236     case 'd': case 'e': case 'f': case 'g':
   4237       if (REG_P (op))
   4238 	{
   4239 	  output_operand_lossage ("%%d, %%e, %%f, %%g operand modifiers are "
   4240 				  "for memory references or constant values "
   4241 				  "only");
   4242 	  return;
   4243 	}
   4244       /* fallthru */
   4245     case 'B': case 'H': /* high half */
   4246     case 'C':
   4247     case 'D':
   4248       switch (letter)
   4249 	{
   4250 	case 'd':
   4251 	  mem_off = 1;
   4252 	  const_shift = 8;
   4253 	  break;
   4254 	case 'B':
   4255 	case 'H':
   4256 	  mem_off = 2;
   4257 	  reg_off = 1;
   4258 	  const_shift = 16;
   4259 	  break;
   4260 	case 'e':
   4261 	  mem_off = 3;
   4262 	  const_shift = 24;
   4263 	  break;
   4264 	case 'C':
   4265 	  mem_off = 4;
   4266 	  reg_off = 2;
   4267 	  const_shift = 32;
   4268 	  break;
   4269 	case 'f':
   4270 	  mem_off = 5;
   4271 	  const_shift = 40;
   4272 	  break;
   4273 	case 'D':
   4274 	  mem_off = 6;
   4275 	  reg_off = 3;
   4276 	  const_shift = 48;
   4277 	  break;
   4278 	case 'g':
   4279 	  mem_off = 7;
   4280 	  const_shift = 56;
   4281 	  break;
   4282 	default:
   4283 	  gcc_unreachable ();
   4284 	  break;
   4285 	}
   4286       /* fallthru */
   4287     case 'A': case 'L': /* Low half.  */
   4288       switch (GET_CODE (op))
   4289 	{
   4290 	case MEM:
   4291 	  /* We don't need to adjust the address for post_inc.  */
   4292 	  op = adjust_address (op, Pmode,
   4293 			       (GET_CODE (XEXP (op, 0)) == POST_INC)
   4294 			       ? 0 : mem_off);
   4295 	  break;
   4296 	case REG:
   4297 	  op = gen_rtx_REG (Pmode, REGNO (op) + reg_off);
   4298 	  break;
   4299 	case CONST_INT:
   4300 	  op = GEN_INT (((long long) INTVAL (op) >> const_shift) & 0xffff);
   4301 	  letter = 0;
   4302 	  break;
   4303 	default:
   4304 	  /* If you get here, figure out a test case :-) */
   4305 	  gcc_unreachable ();
   4306 	}
   4307       break;
   4308 
   4309     case 'X':
   4310       /* This is used to turn, for example, an ADD opcode into an ADDX
   4311 	 opcode when we're using 20-bit addresses.
   4312 	 This can be used for insns which have only one operand which might be
   4313 	 a mem.
   4314 	 If an insn has two different operands which could be memory operands,
   4315 	 then the "Yx" constraint must be used to determine if the X suffix is
   4316 	 required by checking both operands.  */
   4317       if (GET_MODE (op) == PSImode
   4318 	  || !msp430_op_not_in_high_mem (op))
   4319 	fprintf (file, "X");
   4320       return;
   4321 
   4322     case 'x':
   4323       /* Similarly, but only for PSImodes.  BIC, and other insn patterns using
   4324 	 the QHI mode iterator (which includes, QI, HI, and PSImode) use
   4325 	 this.  */
   4326       if (GET_MODE (op) == PSImode)
   4327 	fprintf (file, "X");
   4328       return;
   4329 
   4330     case 'Q':
   4331       /* Likewise, for BR -> BRA.  */
   4332       if (TARGET_LARGE)
   4333 	fprintf (file, "A");
   4334       return;
   4335 
   4336     case 'O':
   4337       /* Computes the offset to the top of the stack for the current frame.
   4338 	 This has to be done here rather than in, say, msp430_expand_builtin()
   4339 	 because builtins are expanded before the frame layout is
   4340 	 determined.  */
   4341       fprintf (file, "%d",
   4342 	       msp430_initial_elimination_offset (ARG_POINTER_REGNUM,
   4343 						  STACK_POINTER_REGNUM)
   4344 	       - (TARGET_LARGE ? 4 : 2));
   4345       return;
   4346 
   4347     case 'J':
   4348       gcc_assert (GET_CODE (op) == CONST_INT);
   4349     case 0:
   4350       break;
   4351     default:
   4352       output_operand_lossage ("invalid operand prefix");
   4353       return;
   4354     }
   4355 
   4356   switch (GET_CODE (op))
   4357     {
   4358     case REG:
   4359       msp430_print_operand_raw (file, op);
   4360       break;
   4361 
   4362     case MEM:
   4363       addr = XEXP (op, 0);
   4364       msp430_print_operand_addr (file, GET_MODE (op), addr);
   4365       break;
   4366 
   4367     case CONST:
   4368       if (GET_CODE (XEXP (op, 0)) == ZERO_EXTRACT)
   4369 	{
   4370 	  op = XEXP (op, 0);
   4371 	  switch (INTVAL (XEXP (op, 2)))
   4372 	    {
   4373 	    case 0:
   4374 	      fprintf (file, "#lo (");
   4375 	      msp430_print_operand_raw (file, XEXP (op, 0));
   4376 	      fprintf (file, ")");
   4377 	      break;
   4378 
   4379 	    case 16:
   4380 	      fprintf (file, "#hi (");
   4381 	      msp430_print_operand_raw (file, XEXP (op, 0));
   4382 	      fprintf (file, ")");
   4383 	      break;
   4384 
   4385 	    default:
   4386 	      output_operand_lossage ("invalid zero extract");
   4387 	      break;
   4388 	    }
   4389 	  break;
   4390 	}
   4391       /* Fall through.  */
   4392     case CONST_INT:
   4393     case SYMBOL_REF:
   4394     case LABEL_REF:
   4395       if (letter == 0)
   4396 	fprintf (file, "#");
   4397       msp430_print_operand_raw (file, op);
   4398       break;
   4399 
   4400     case EQ: fprintf (file, "EQ"); break;
   4401     case NE: fprintf (file, "NE"); break;
   4402     case GEU: fprintf (file, "HS"); break;
   4403     case LTU: fprintf (file, "LO"); break;
   4404     case GE: fprintf (file, "GE"); break;
   4405     case LT: fprintf (file, "L"); break;
   4406 
   4407     default:
   4408       print_rtl (file, op);
   4409       break;
   4410     }
   4411 }
   4412 
   4413 
   4414 /* Frame stuff.  */
   4416 
   4417 rtx
   4418 msp430_return_addr_rtx (int count)
   4419 {
   4420   int ra_size;
   4421   if (count)
   4422     return NULL_RTX;
   4423 
   4424   ra_size = TARGET_LARGE ? 4 : 2;
   4425   if (crtl->args.pretend_args_size)
   4426     ra_size += 2;
   4427 
   4428   return gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, arg_pointer_rtx,
   4429 					   GEN_INT (- ra_size)));
   4430 }
   4431 
   4432 rtx
   4433 msp430_incoming_return_addr_rtx (void)
   4434 {
   4435   return gen_rtx_MEM (Pmode, stack_pointer_rtx);
   4436 }
   4437 
   4438 /* If the path to the MSP430-GCC support files has been found by examining
   4439    an environment variable (see msp430_check_env_var_for_devices in
   4440    msp430-devices.cc), or -mdevices-csv-loc=, register this path as an include
   4441    directory so the user can #include msp430.h without needing to specify the
   4442    path to the support files with -I.  */
   4443 void
   4444 msp430_register_pre_includes (const char *sysroot ATTRIBUTE_UNUSED,
   4445 			      const char *iprefix ATTRIBUTE_UNUSED,
   4446 			      int stdinc ATTRIBUTE_UNUSED)
   4447 {
   4448   char *include_dir;
   4449   if (msp430_devices_csv_loc)
   4450     include_dir = xstrdup (msp430_devices_csv_loc);
   4451   else if (msp430_check_env_var_for_devices (&include_dir))
   4452     return;
   4453   include_dir = msp430_dirname (include_dir);
   4454 
   4455   include_dir = update_path (include_dir, "");
   4456   add_path (include_dir, INC_SYSTEM, false, false);
   4457 }
   4458 
   4459 /* Instruction generation stuff.  */
   4461 
   4462 /* Generate a sequence of instructions to sign-extend an HI
   4463    value into an SI value.  Handles the tricky case where
   4464    we are overwriting the destination.
   4465    Return the number of bytes used by the emitted instructions.
   4466    If RETURN_LENGTH is true then do not emit the assembly instruction
   4467    sequence.  */
   4468 int
   4469 msp430x_extendhisi (rtx * operands, bool return_length)
   4470 {
   4471   if (REGNO (operands[0]) == REGNO (operands[1]))
   4472     {
   4473       /* Low word of dest == source word.  */
   4474       if (!return_length)
   4475 	output_asm_insn ("BIT.W\t#0x8000, %L0 { SUBC.W\t%H0, %H0 { INV.W\t%H0, %H0",
   4476 			 operands);
   4477       return 8;
   4478     }
   4479   else if (! msp430x)
   4480     {
   4481       /* Note: This sequence is approximately the same length as invoking a
   4482 	 helper function to perform the sign-extension, as in:
   4483 
   4484 	 MOV.W  %1, %L0
   4485 	 MOV.W  %1, r12
   4486 	 CALL   __mspabi_srai_15
   4487 	 MOV.W  r12, %H0
   4488 
   4489 	 but this version does not involve any function calls or using argument
   4490 	 registers, so it reduces register pressure.  */
   4491       if (!return_length)
   4492 	output_asm_insn ("MOV.W\t%1, %L0 { BIT.W\t#0x8000, %L0 { SUBC.W\t%H0, %H0 { INV.W\t%H0, %H0",
   4493 			 operands);
   4494       return 10;
   4495     }
   4496   else if (REGNO (operands[0]) + 1 == REGNO (operands[1]))
   4497     {
   4498       /* High word of dest == source word.  */
   4499       if (!return_length)
   4500 	output_asm_insn ("MOV.W\t%1, %L0 { RPT\t#15 { RRAX.W\t%H0",
   4501 			 operands);
   4502       return 6;
   4503     }
   4504 
   4505   /* No overlap between dest and source.  */
   4506   if (!return_length)
   4507     output_asm_insn ("MOV.W\t%1, %L0 { MOV.W\t%1, %H0 { RPT\t#15 { RRAX.W\t%H0",
   4508 		     operands);
   4509   return 8;
   4510 }
   4511 
   4512 /* Stop GCC from thinking that it can eliminate (SUBREG:PSI (SI)).  */
   4513 
   4514 #undef TARGET_CAN_CHANGE_MODE_CLASS
   4515 #define TARGET_CAN_CHANGE_MODE_CLASS msp430_can_change_mode_class
   4516 
   4517 static bool
   4518 msp430_can_change_mode_class (machine_mode from, machine_mode to, reg_class_t)
   4519 {
   4520   if ((to == PSImode && from == SImode)
   4521       || (to == SImode && from == PSImode)
   4522       || (to == DImode && from == PSImode)
   4523       || (to == PSImode && from == DImode))
   4524     return false;
   4525   return true;
   4526 }
   4527 
   4528 #undef  TARGET_HAVE_SPECULATION_SAFE_VALUE
   4530 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
   4531 
   4532 struct gcc_target targetm = TARGET_INITIALIZER;
   4533 
   4534 #include "gt-msp430.h"
   4535