remove some duplicate #include's.
[deliverable/binutils-gdb.git] / gas / config / tc-msp430.c
index e75347df5d5fe42f5b5dcd8407cc982f6e4f43d0..72c73da2f4a73c7b87ca803377c8133a53ffa3fe 100644 (file)
@@ -1,6 +1,6 @@
 /* tc-msp430.c -- Assembler code for the Texas Instruments MSP430
 
-  Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+  Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
   Contributed by Dmitry Diky <diwil@mail.ru>
 
   This file is part of GAS, the GNU Assembler.
 
   You should have received a copy of the GNU General Public License
   along with GAS; see the file COPYING.  If not, write to
-  the Free Software Foundation, 59 Temple Place - Suite 330,
-  Boston, MA 02111-1307, USA.  */
+  the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+  Boston, MA 02110-1301, USA.  */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
 #include <limits.h>
 
 #define PUSH_1X_WORKAROUND
 #include "subsegs.h"
 #include "opcode/msp430.h"
 #include "safe-ctype.h"
+#include "dwarf2dbg.h"
+
+/*
+   We will disable polymorphs by default because it is dangerous.
+   The potential problem here is the following: assume we got the
+   following code:
+
+       jump .l1
+       nop
+       jump  subroutine        ; external symbol
+      .l1:
+       nop
+       ret
+   
+   In case of assembly time relaxation we'll get:
+       0: jmp .l1 <.text +0x08> (reloc deleted)
+       2: nop
+       4: br subroutine
+    .l1:
+       8: nop
+       10: ret
+
+   If the 'subroutine' wiys thin +-1024 bytes range then linker
+   will produce
+       0: jmp .text +0x08
+       2: nop
+       4: jmp subroutine
+       .l1:
+       6: nop
+       8: ret  ; 'jmp .text +0x08' will land here. WRONG!!!
+
+
+   The workaround is the following:
+   1. Declare global var enable_polymorphs which set to 1 via option -mP.
+   2. Declare global var enable_relax  which set to 1 via option -mQ.
+
+   If polymorphs are enabled, and relax isn't, treat all jumps as long jumps,
+   do not delete any relocs and leave them for linker.
+   
+   If relax is enabled, relax at assembly time and kill relocs as necessary.
+ */
+
+int msp430_enable_relax;
+int msp430_enable_polys;
 
 /* GCC uses the some condition codes which we'll
    implement as new polymorph instructions.
@@ -124,7 +165,7 @@ static struct hcodes_s msp430_hcodes[] =
 
 const char comment_chars[] = ";";
 const char line_comment_chars[] = "#";
-const char line_separator_chars[] = "";
+const char line_separator_chars[] = "|";
 const char EXP_CHARS[] = "eE";
 const char FLT_CHARS[] = "dD";
 
@@ -205,6 +246,7 @@ struct mcu_type_s
 #define MSP430_ISA_14   14
 #define MSP430_ISA_15   15
 #define MSP430_ISA_16   16
+#define MSP430_ISA_21   21
 #define MSP430_ISA_31   31
 #define MSP430_ISA_32   32
 #define MSP430_ISA_33   33
@@ -251,6 +293,11 @@ static struct mcu_type_s mcu_types[] =
   {"msp430x1611", MSP430_ISA_16, bfd_mach_msp16},
   {"msp430x1612", MSP430_ISA_16, bfd_mach_msp16},
 
+  {"msp430x2101", MSP430_ISA_21, bfd_mach_msp21},
+  {"msp430x2111", MSP430_ISA_21, bfd_mach_msp21},
+  {"msp430x2121", MSP430_ISA_21, bfd_mach_msp21},
+  {"msp430x2131", MSP430_ISA_21, bfd_mach_msp21},
+  
   {"msp430x311",  MSP430_ISA_31, bfd_mach_msp31},
   {"msp430x312",  MSP430_ISA_31, bfd_mach_msp31},
   {"msp430x313",  MSP430_ISA_31, bfd_mach_msp31},
@@ -437,7 +484,7 @@ skip_space (char * s)
   return s;
 }
 
-/* Extract one word from FROM and copy it to TO. Delimeters are ",;\n"  */
+/* Extract one word from FROM and copy it to TO. Delimiters are ",;\n"  */
 
 static char *
 extract_operand (char * from, char * to, int limit)
@@ -654,6 +701,8 @@ extract_word (char * from, char * to, int limit)
 }
 
 #define OPTION_MMCU 'm'
+#define OPTION_RELAX 'Q'
+#define OPTION_POLYMORPHS 'P'
 
 static void
 msp430_set_arch (int dummy ATTRIBUTE_UNUSED)
@@ -703,6 +752,17 @@ md_parse_option (int c, char * arg)
        as_fatal (_("redefinition of mcu type %s' to %s'"),
                  msp430_mcu->name, mcu_types[i].name);
       return 1;
+      break;
+      
+    case OPTION_RELAX:
+      msp430_enable_relax = 1; 
+      return 1;
+      break;
+      
+    case OPTION_POLYMORPHS:
+      msp430_enable_polys = 1;
+      return 1;
+      break;
     }
 
   return 0;
@@ -721,6 +781,8 @@ const char *md_shortopts = "m:";
 struct option md_longopts[] =
 {
   {"mmcu", required_argument, NULL, OPTION_MMCU},
+  {"mP", no_argument, NULL, OPTION_POLYMORPHS},
+  {"mQ", no_argument, NULL, OPTION_RELAX},
   {NULL, no_argument, NULL, 0}
 };
 
@@ -752,6 +814,9 @@ md_show_usage (FILE * stream)
             "                  msp430xG437 msp430xG438 msp430G439\n"
             "                  msp430x435  msp430x436  msp430x437\n"
             "                  msp430x447  msp430x448  msp430x449\n"));
+  fprintf (stream,
+          _("  -mQ - enable relaxation at assembly time. DANGEROUS!\n"
+            "  -mP - enable polymorph instructions\n"));
 
   show_mcu_list (stream);
 }
@@ -1355,7 +1420,7 @@ static unsigned int
 msp430_operands (struct msp430_opcode_s * opcode, char * line)
 {
   int bin = opcode->bin_opcode;        /* Opcode mask.  */
-  int __is;
+  int __is = 0;
   char l1[MAX_OP_LEN], l2[MAX_OP_LEN];
   char *frag;
   int where;
@@ -1407,6 +1472,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
          __is = 2;
          frag = frag_more (__is);
          bfd_putl16 ((bfd_vma) bin, frag);
+         dwarf2_emit_insn (__is);
          break;
        case 1:
          /* Something which works with destination operand.  */
@@ -1420,6 +1486,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
          frag = frag_more (2 * __is);
          where = frag - frag_now->fr_literal;
          bfd_putl16 ((bfd_vma) bin, frag);
+         dwarf2_emit_insn (2 * __is);
 
          if (op1.mode == OP_EXP)
            {
@@ -1453,7 +1520,8 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
            frag = frag_more (2 * __is);
            where = frag - frag_now->fr_literal;
            bfd_putl16 ((bfd_vma) bin, frag);
-
+           dwarf2_emit_insn (2 * __is);
+           
            if (op1.mode == OP_EXP)
              {
                where += 2;     /* Advance 'where' as we do not know _where_.  */
@@ -1497,6 +1565,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
          frag = frag_more (2 * __is);
          where = frag - frag_now->fr_literal;
          bfd_putl16 ((bfd_vma) bin, frag);
+         dwarf2_emit_insn (2 * __is);
 
          if (op1.mode == OP_EXP)
            {
@@ -1529,6 +1598,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
       frag = frag_more (2 * __is);
       where = frag - frag_now->fr_literal;
       bfd_putl16 ((bfd_vma) bin, frag);
+      dwarf2_emit_insn (2 * __is);
 
       if (op1.mode == OP_EXP)
        {
@@ -1563,6 +1633,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
          /* reti instruction.  */
          frag = frag_more (2);
          bfd_putl16 ((bfd_vma) bin, frag);
+         dwarf2_emit_insn (2);
          break;
        }
 
@@ -1576,6 +1647,7 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
       frag = frag_more (2 * __is);
       where = frag - frag_now->fr_literal;
       bfd_putl16 ((bfd_vma) bin, frag);
+      dwarf2_emit_insn (2 * __is);
 
       if (op1.mode == OP_EXP)
        {
@@ -1660,14 +1732,14 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
          else if (*l1 == '$')
            {
              as_bad (_("instruction requires label sans '$'"));
-             break;
            }
          else
            {
              as_bad (_
                      ("instruction requires label or value in range -511:512"));
-             break;
            }
+         dwarf2_emit_insn (2 * __is);
+         break;
        }
       else
        {
@@ -1677,6 +1749,12 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
       break;
 
     case 4:    /* Extended jumps.  */
+      if (!msp430_enable_polys)
+       {
+         as_bad(_("polymorphs are not enabled. Use -mP option to enable."));
+         break;
+       }
+       
       line = extract_operand (line, l1, sizeof (l1));
       if (l1[0])
        {
@@ -1693,7 +1771,12 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
              /* Relaxation required.  */
              struct rcodes_s rc = msp430_rcodes[opcode->insn_opnumb];
 
+             /* The parameter to dwarf2_emit_insn is actually the offset to the start
+                of the insn from the fix piece of instruction that was emitted.
+                Since next fragments may have variable size we tie debug info
+                to the beginning of the instruction. */
              frag = frag_more (8);
+             dwarf2_emit_insn (0);
              bfd_putl16 ((bfd_vma) rc.sop, frag);
              frag = frag_variant (rs_machine_dependent, 8, 2,
                                   ENCODE_RELAX (rc.lpos, STATE_BITS10), /* Wild guess.  */
@@ -1708,6 +1791,11 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
       break;
 
     case 5:    /* Emulated extended branches.  */
+      if (!msp430_enable_polys)
+       {
+         as_bad(_("polymorphs are not enabled. Use -mP option to enable."));
+         break;
+       }
       line = extract_operand (line, l1, sizeof (l1));
       if (l1[0])
        {
@@ -1725,8 +1813,10 @@ msp430_operands (struct msp430_opcode_s * opcode, char * line)
              struct hcodes_s hc = msp430_hcodes[opcode->insn_opnumb];
 
              frag = frag_more (8);
+             dwarf2_emit_insn (0);
              bfd_putl16 ((bfd_vma) hc.op0, frag);
              bfd_putl16 ((bfd_vma) hc.op1, frag+2);
+
              frag = frag_variant (rs_machine_dependent, 8, 2,
                                   ENCODE_RELAX (STATE_EMUL_BRANCH, STATE_BITS10), /* Wild guess.  */
                                   exp.X_add_symbol,
@@ -1814,11 +1904,26 @@ md_pcrel_from_section (fixS * fixp, segT sec)
   return fixp->fx_frag->fr_address + fixp->fx_where;
 }
 
+/* Replaces standard TC_FORCE_RELOCATION_LOCAL.
+   Now it handles the situation when relocations
+   have to be passed to linker. */
+int
+msp430_force_relocation_local(fixS *fixp)
+{
+  if (msp430_enable_polys
+        && !msp430_enable_relax)
+    return 1;
+  else
+    return (!fixp->fx_pcrel
+           || fixp->fx_plt
+           || generic_force_reloc(fixp));
+}
+
+
 /* GAS will call this for each fixup.  It should store the correct
    value in the object file.  */
-
 void
-md_apply_fix3 (fixS * fixp, valueT * valuep, segT seg)
+md_apply_fix (fixS * fixp, valueT * valuep, segT seg)
 {
   unsigned char * where;
   unsigned long insn;
@@ -1873,13 +1978,18 @@ md_apply_fix3 (fixS * fixp, valueT * valuep, segT seg)
        }
     }
 
-  switch (fixp->fx_r_type)
+  fixp->fx_no_overflow = 1;
+
+  /* if polymorphs are enabled and relax disabled. 
+     do not kill any relocs and pass them to linker. */
+  if (msp430_enable_polys 
+      && !msp430_enable_relax)
     {
-    default:
-      fixp->fx_no_overflow = 1;
-      break;
-    case BFD_RELOC_MSP430_10_PCREL:
-      break;
+      if (!fixp->fx_addsy || (fixp->fx_addsy 
+         && S_GET_SEGMENT (fixp->fx_addsy) == absolute_section))
+       fixp->fx_done = 1;      /* it is ok to kill 'abs' reloc */
+      else
+       fixp->fx_done = 0;
     }
 
   if (fixp->fx_done)
@@ -1958,12 +2068,11 @@ md_apply_fix3 (fixS * fixp, valueT * valuep, segT seg)
     }
 }
 
-/* A `BFD_ASSEMBLER' GAS will call this to generate a reloc.  GAS
-   will pass the resulting reloc to `bfd_install_relocation'.  This
-   currently works poorly, as `bfd_install_relocation' often does the
-   wrong thing, and instances of `tc_gen_reloc' have been written to
-   work around the problems, which in turns makes it difficult to fix
-   `bfd_install_relocation'.  */
+/* GAS will call this to generate a reloc, passing the resulting reloc
+   to `bfd_install_relocation'.  This currently works poorly, as
+   `bfd_install_relocation' often does the wrong thing, and instances of
+   `tc_gen_reloc' have been written to work around the problems, which
+   in turns makes it difficult to fix `bfd_install_relocation'.  */
 
 /* If while processing a fixup, a reloc really needs to be created
    then it is done here.  */
@@ -2180,6 +2289,13 @@ msp430_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS * fragP,
       aim = S_GET_VALUE (symbolP) - fragP->fr_address - fragP->fr_fix;
     }
 
+  if (!msp430_enable_relax)
+    {
+      /* Relaxation is not enabled. So, make all jump as long ones
+         by setting 'aim' to quite high value. */
+      aim = 0x7fff;
+    }
+  
   this_state = fragP->fr_subtype;
   start_type = this_type = table + this_state;
 
@@ -2187,7 +2303,7 @@ msp430_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS * fragP,
     {
       /* Look backwards.  */
       for (next_state = this_type->rlx_more; next_state;)
-       if (aim >= this_type->rlx_backward)
+       if (aim >= this_type->rlx_backward || !this_type->rlx_backward)
          next_state = 0;
        else
          {
@@ -2201,7 +2317,7 @@ msp430_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS * fragP,
     {
       /* Look forwards.  */
       for (next_state = this_type->rlx_more; next_state;)
-       if (aim <= this_type->rlx_forward)
+       if (aim <= this_type->rlx_forward || !this_type->rlx_forward)
          next_state = 0;
        else
          {
This page took 0.028951 seconds and 4 git commands to generate.