2000-09-09 Philip Blundell <philb@gnu.org>
[deliverable/binutils-gdb.git] / gas / macro.c
index e03b0f710b3c623e7756ddd8bb83142d0b689714..c5b9b68e4ee494df35ecc4f56e4a347167ebb060 100644 (file)
@@ -1,5 +1,6 @@
 /* macro.c - macro support for gas and gasp
-   Copyright (C) 1994, 95, 96, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1994, 95, 96, 97, 98, 99, 2000
+   Free Software Foundation, Inc.
 
    Written by Steve and Judy Chamberlain of Cygnus Support,
       sac@cygnus.com
    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. */
+   02111-1307, USA.  */
 
 #include "config.h"
 
 /* AIX requires this to be the first thing in the file.  */
 #ifdef __GNUC__
-#ifdef __STDC__
+# ifndef alloca
+#  ifdef __STDC__
 extern void *alloca ();
-#else
+#  else
 extern char *alloca ();
-#endif
+#  endif
+# endif
 #else
 # if HAVE_ALLOCA_H
 #  include <alloca.h>
@@ -63,45 +66,11 @@ extern void *alloca ();
 #include "hash.h"
 #include "macro.h"
 
+#include "asintl.h"
+
 /* The routines in this file handle macro definition and expansion.
    They are called by both gasp and gas.  */
 
-/* Structures used to store macros. 
-
-   Each macro knows its name and included text.  It gets built with a
-   list of formal arguments, and also keeps a hash table which points
-   into the list to speed up formal search.  Each formal knows its
-   name and its default value.  Each time the macro is expanded, the
-   formals get the actual values attatched to them. */
-
-/* describe the formal arguments to a macro */
-
-typedef struct formal_struct
-  {
-    struct formal_struct *next;        /* next formal in list */
-    sb name;                   /* name of the formal */
-    sb def;                    /* the default value */
-    sb actual;                 /* the actual argument (changed on each expansion) */
-    int index;                 /* the index of the formal 0..formal_count-1 */
-  }
-formal_entry;
-
-/* Other values found in the index field of a formal_entry.  */
-#define QUAL_INDEX (-1)
-#define NARG_INDEX (-2)
-#define LOCAL_INDEX (-3)
-
-/* describe the macro. */
-
-typedef struct macro_struct
-  {
-    sb sub;                    /* substitution text. */
-    int formal_count;          /* number of formal args. */
-    formal_entry *formals;     /* pointer to list of formal_structs */
-    struct hash_control *formal_hash; /* hash table of formals. */
-  }
-macro_entry;
-
 /* Internal functions.  */
 
 static int get_token PARAMS ((int, sb *, sb *));
@@ -119,7 +88,8 @@ static const char *macro_expand PARAMS ((int, sb *, macro_entry *, sb *, int));
 
 #define ISSEP(x) \
  ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
-  || (x) == '<' || (x) == '>' || (x) == ')' || (x) == '(')
+  || (x) == ')' || (x) == '(' \
+  || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
 
 #define ISBASE(x) \
   ((x) == 'b' || (x) == 'B' \
@@ -172,6 +142,15 @@ macro_init (alternate, mri, strip_at, expr)
   macro_expr = expr;
 }
 
+/* Switch in and out of MRI mode on the fly.  */
+
+void
+macro_mri_mode (mri)
+     int mri;
+{
+  macro_mri = mri;
+}
+
 /* Read input lines till we get to a TO string.
    Increase nesting depth if we get a FROM string.
    Put the results into sb at PTR.
@@ -194,7 +173,7 @@ buffer_and_nest (from, to, ptr, get_line)
 
   while (more)
     {
-      /* Try and find the first pseudo op on the line */
+      /* Try and find the first pseudo op on the line */
       int i = line_start;
 
       if (! macro_alternate && ! macro_mri)
@@ -202,26 +181,26 @@ buffer_and_nest (from, to, ptr, get_line)
          /* With normal syntax we can suck what we want till we get
             to the dot.  With the alternate, labels have to start in
             the first column, since we cant tell what's a label and
-            whats a pseudoop */
+            whats a pseudoop */
 
-         /* Skip leading whitespace */
+         /* Skip leading whitespace */
          while (i < ptr->len && ISWHITE (ptr->ptr[i]))
            i++;
 
-         /* Skip over a label */
+         /* Skip over a label */
          while (i < ptr->len
                 && (isalnum ((unsigned char) ptr->ptr[i])
                     || ptr->ptr[i] == '_'
                     || ptr->ptr[i] == '$'))
            i++;
 
-         /* And a colon */
+         /* And a colon */
          if (i < ptr->len
              && ptr->ptr[i] == ':')
            i++;
 
        }
-      /* Skip trailing whitespace */
+      /* Skip trailing whitespace */
       while (i < ptr->len && ISWHITE (ptr->ptr[i]))
        i++;
 
@@ -230,22 +209,26 @@ buffer_and_nest (from, to, ptr, get_line)
                           || macro_mri))
        {
          if (ptr->ptr[i] == '.')
-             i++;
-         if (strncasecmp (ptr->ptr + i, from, from_len) == 0)
+           i++;
+         if (strncasecmp (ptr->ptr + i, from, from_len) == 0
+             && (ptr->len == (i + from_len)
+                 || ! isalnum (ptr->ptr[i + from_len])))
            depth++;
-         if (strncasecmp (ptr->ptr + i, to, to_len) == 0)
+         if (strncasecmp (ptr->ptr + i, to, to_len) == 0
+             && (ptr->len == (i + to_len)
+                 || ! isalnum (ptr->ptr[i + to_len])))
            {
              depth--;
              if (depth == 0)
                {
-                 /* Reset the string to not include the ending rune */
+                 /* Reset the string to not include the ending rune */
                  ptr->len = line_start;
                  break;
                }
            }
        }
 
-      /* Add a CR to the end and keep running */
+      /* Add a CR to the end and keep running */
       sb_add_char (ptr, '\n');
       line_start = ptr->len;
       more = get_line (ptr);
@@ -277,7 +260,7 @@ get_token (idx, in, name)
          sb_add_char (name, in->ptr[idx++]);
        }
     }
-  /* Ignore trailing & */
+  /* Ignore trailing & */
   if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
     idx++;
   return idx;
@@ -294,77 +277,77 @@ getstring (idx, in, acc)
   idx = sb_skip_white (idx, in);
 
   while (idx < in->len
-        && (in->ptr[idx] == '"' 
-            || in->ptr[idx] == '<' 
+        && (in->ptr[idx] == '"'
+            || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
             || (in->ptr[idx] == '\'' && macro_alternate)))
     {
       if (in->ptr[idx] == '<')
        {
-         if (macro_alternate || macro_mri)
+         int nest = 0;
+         idx++;
+         while ((in->ptr[idx] != '>' || nest)
+                && idx < in->len)
            {
-             int nest = 0;
-             idx++;
-             while ((in->ptr[idx] != '>' || nest)
-                    && idx < in->len)
+             if (in->ptr[idx] == '!')
                {
-                 if (in->ptr[idx] == '!')
-                   {
-                     idx++  ;
-                     sb_add_char (acc, in->ptr[idx++]);
-                   }
-                 else
-                   {
-                     if (in->ptr[idx] == '>')
-                       nest--;
-                     if (in->ptr[idx] == '<')
-                       nest++;
-                     sb_add_char (acc, in->ptr[idx++]);
-                   }
+                 idx++;
+                 sb_add_char (acc, in->ptr[idx++]);
+               }
+             else
+               {
+                 if (in->ptr[idx] == '>')
+                   nest--;
+                 if (in->ptr[idx] == '<')
+                   nest++;
+                 sb_add_char (acc, in->ptr[idx++]);
                }
-             idx++;
-           }
-         else
-           {
-             int code;
-             idx++;
-             idx = ((*macro_expr)
-                    ("character code in string must be absolute expression",
-                     idx, in, &code));
-             sb_add_char (acc, code);
-
-#if 0
-             if (in->ptr[idx] != '>')
-               ERROR ((stderr, "Missing > for character code.\n"));
-#endif
-             idx++;
            }
+         idx++;
        }
       else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
        {
          char tchar = in->ptr[idx];
+         int escaped = 0;
+
          idx++;
+
          while (idx < in->len)
            {
+             if (in->ptr[idx - 1] == '\\')
+               escaped ^= 1;
+             else
+               escaped = 0;
+
              if (macro_alternate && in->ptr[idx] == '!')
                {
-                 idx++  ;
-                 sb_add_char (acc, in->ptr[idx++]);
+                 idx++;
+
+                 sb_add_char (acc, in->ptr[idx]);
+
+                 idx++;
+               }
+             else if (escaped && in->ptr[idx] == tchar)
+               {
+                 sb_add_char (acc, tchar);
+                 idx++;
                }
              else
                {
                  if (in->ptr[idx] == tchar)
                    {
                      idx++;
+
                      if (idx >= in->len || in->ptr[idx] != tchar)
                        break;
                    }
+
                  sb_add_char (acc, in->ptr[idx]);
                  idx++;
                }
            }
        }
     }
-  
+
   return idx;
 }
 
@@ -389,7 +372,7 @@ get_any_string (idx, in, out, expand, pretend_quoted)
 
   if (idx < in->len)
     {
-      if (in->len > 2 && in->ptr[idx+1] == '\'' && ISBASE (in->ptr[idx]))
+      if (in->len > 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
        {
          while (!ISSEP (in->ptr[idx]))
            sb_add_char (out, in->ptr[idx++]);
@@ -400,8 +383,8 @@ get_any_string (idx, in, out, expand, pretend_quoted)
        {
          int val;
          char buf[20];
-         /* Turns the next expression into a string */
-         idx = (*macro_expr) ("% operator needs absolute expression",
+         /* Turns the next expression into a string */
+         idx = (*macro_expr) (_("% operator needs absolute expression"),
                               idx + 1,
                               in,
                               &val);
@@ -409,45 +392,46 @@ get_any_string (idx, in, out, expand, pretend_quoted)
          sb_add_string (out, buf);
        }
       else if (in->ptr[idx] == '"'
-              || in->ptr[idx] == '<'
+              || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
               || (macro_alternate && in->ptr[idx] == '\''))
        {
          if (macro_alternate
              && ! macro_strip_at
              && expand)
            {
-             /* Keep the quotes */
-             sb_add_char (out,  '\"');
+             /* Keep the quotes */
+             sb_add_char (out, '\"');
 
              idx = getstring (idx, in, out);
-             sb_add_char (out,  '\"');
+             sb_add_char (out, '\"');
            }
          else
            {
              idx = getstring (idx, in, out);
            }
        }
-      else 
+      else
        {
-         while (idx < in->len 
+         while (idx < in->len
                 && (in->ptr[idx] == '"'
                     || in->ptr[idx] == '\''
-                    || pretend_quoted 
+                    || pretend_quoted
                     || (in->ptr[idx] != ' '
                         && in->ptr[idx] != '\t'
                         && in->ptr[idx] != ','
-                        && in->ptr[idx] != '<')))
+                        && (in->ptr[idx] != '<'
+                            || (! macro_alternate && ! macro_mri)))))
            {
-             if (in->ptr[idx] == '"' 
+             if (in->ptr[idx] == '"'
                  || in->ptr[idx] == '\'')
                {
                  char tchar = in->ptr[idx];
                  sb_add_char (out, in->ptr[idx++]);
                  while (idx < in->len
                         && in->ptr[idx] != tchar)
-                   sb_add_char (out, in->ptr[idx++]);              
+                   sb_add_char (out, in->ptr[idx++]);
                  if (idx == in->len)
-                   return idx;       
+                   return idx;
                }
              sb_add_char (out, in->ptr[idx++]);
            }
@@ -486,15 +470,15 @@ do_formals (macro, idx, in)
       idx = sb_skip_white (idx, in);
       if (formal->name.len)
        {
-         /* This is a formal */
+         /* This is a formal */
          if (idx < in->len && in->ptr[idx] == '=')
            {
-             /* Got a default */
+             /* Got a default */
              idx = get_any_string (idx + 1, in, &formal->def, 1, 0);
            }
        }
 
-      /* Add to macro's hash table */
+      /* Add to macro's hash table */
       hash_jam (macro->formal_hash, sb_terminate (&formal->name), formal);
 
       formal->index = macro->formal_count;
@@ -527,7 +511,7 @@ do_formals (macro, idx, in)
 
       sb_add_string (&formal->name, name);
 
-      /* Add to macro's hash table */
+      /* Add to macro's hash table */
       hash_jam (macro->formal_hash, name, formal);
 
       formal->index = NARG_INDEX;
@@ -563,20 +547,20 @@ define_macro (idx, in, label, get_line, namep)
 
   idx = sb_skip_white (idx, in);
   if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
-    return "unexpected end of file in macro definition";
+    return _("unexpected end of file in macro definition");
   if (label != NULL && label->len != 0)
     {
       sb_add_sb (&name, label);
       if (idx < in->len && in->ptr[idx] == '(')
        {
-         /* It's the label: MACRO (formals,...)  sort */
+         /* It's the label: MACRO (formals,...)  sort  */
          idx = do_formals (macro, idx + 1, in);
          if (in->ptr[idx] != ')')
-           return "missing ) after formals";
+           return _("missing ) after formals");
        }
       else
        {
-         /* It's the label: MACRO formals,...  sort */
+         /* It's the label: MACRO formals,...  sort  */
          idx = do_formals (macro, idx, in);
        }
     }
@@ -587,9 +571,9 @@ define_macro (idx, in, label, get_line, namep)
       idx = do_formals (macro, idx, in);
     }
 
-  /* and stick it in the macro hash table */
+  /* And stick it in the macro hash table.  */
   for (idx = 0; idx < name.len; idx++)
-    if (isupper (name.ptr[idx]))
+    if (isupper ((unsigned char) name.ptr[idx]))
       name.ptr[idx] = tolower (name.ptr[idx]);
   namestr = sb_terminate (&name);
   hash_jam (macro_hash, namestr, (PTR) macro);
@@ -655,11 +639,16 @@ sub_actual (start, in, t, formal_hash, kind, out, copyifnotthere)
          sb_add_sb (out, &ptr->def);
        }
     }
+  else if (kind == '&')
+    {
+      /* Doing this permits people to use & in macro bodies.  */
+      sb_add_char (out, '&');
+    }
   else if (copyifnotthere)
     {
       sb_add_sb (out, t);
     }
-  else 
+  else
     {
       sb_add_char (out, '\\');
       sb_add_sb (out, t);
@@ -699,8 +688,7 @@ macro_expand_body (in, out, formals, formal_hash, comment_char, locals)
            }
          else
            {
-             /* FIXME: Why do we do this?  It prevents people from
-                 using the & operator in a macro.  */
+             /* FIXME: Why do we do this?  */
              src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
            }
        }
@@ -709,14 +697,14 @@ macro_expand_body (in, out, formals, formal_hash, comment_char, locals)
          src++;
          if (in->ptr[src] == comment_char && comment_char != '\0')
            {
-             /* This is a comment, just drop the rest of the line */
+             /* This is a comment, just drop the rest of the line */
              while (src < in->len
                     && in->ptr[src] != '\n')
                src++;
            }
          else if (in->ptr[src] == '(')
            {
-             /* Sub in till the next ')' literally */
+             /* Sub in till the next ')' literally */
              src++;
              while (src < in->len && in->ptr[src] != ')')
                {
@@ -725,21 +713,21 @@ macro_expand_body (in, out, formals, formal_hash, comment_char, locals)
              if (in->ptr[src] == ')')
                src++;
              else
-               return "missplaced )";
+               return _("missplaced )");
            }
          else if (in->ptr[src] == '@')
            {
-             /* Sub in the macro invocation number */
+             /* Sub in the macro invocation number */
 
-             char buffer[6];
+             char buffer[10];
              src++;
-             sprintf (buffer, "%05d", macro_number);
+             sprintf (buffer, "%d", macro_number);
              sb_add_string (out, buffer);
            }
          else if (in->ptr[src] == '&')
            {
              /* This is a preprocessor variable name, we don't do them
-                here */
+                here */
              sb_add_char (out, '\\');
              sb_add_char (out, '&');
              src++;
@@ -901,7 +889,9 @@ macro_expand_body (in, out, formals, formal_hash, comment_char, locals)
       formal_entry *f;
 
       f = loclist->next;
-      hash_delete (formal_hash, sb_terminate (&loclist->name));
+      /* Setting the value to NULL effectively deletes the entry.  We
+         avoid calling hash_delete because it doesn't reclaim memory.  */
+      hash_jam (formal_hash, sb_terminate (&loclist->name), NULL);
       sb_kill (&loclist->name);
       sb_kill (&loclist->def);
       sb_kill (&loclist->actual);
@@ -932,10 +922,10 @@ macro_expand (idx, in, m, out, comment_char)
   const char *err;
 
   sb_new (&t);
-  
-  /* Reset any old value the actuals may have */
+
+  /* Reset any old value the actuals may have */
   for (f = m->formals; f; f = f->next)
-      sb_reset (&f->actual);
+    sb_reset (&f->actual);
   f = m->formals;
   while (f != NULL && f->index < 0)
     f = f->next;
@@ -961,13 +951,13 @@ macro_expand (idx, in, m, out, comment_char)
        }
     }
 
-  /* Peel off the actuals and store them away in the hash tables' actuals */
+  /* Peel off the actuals and store them away in the hash tables' actuals */
   idx = sb_skip_white (idx, in);
   while (idx < in->len && in->ptr[idx] != comment_char)
     {
       int scan;
 
-      /* Look and see if it's a positional or keyword arg */
+      /* Look and see if it's a positional or keyword arg */
       scan = idx;
       while (scan < in->len
             && !ISSEP (in->ptr[scan])
@@ -977,23 +967,23 @@ macro_expand (idx, in, m, out, comment_char)
       if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
        {
          is_keyword = 1;
-         if (is_positional)
-           return "can't mix positional and keyword arguments";
+
+         /* It's OK to go from positional to keyword.  */
 
          /* This is a keyword arg, fetch the formal name and
-            then the actual stuff */
+            then the actual stuff */
          sb_reset (&t);
          idx = get_token (idx, in, &t);
          if (in->ptr[idx] != '=')
-           return "confusion in formal parameters";
+           return _("confusion in formal parameters");
 
-         /* Lookup the formal in the macro's list */
+         /* Lookup the formal in the macro's list */
          ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
          if (!ptr)
-           return "macro formal argument does not exist";
+           return _("macro formal argument does not exist");
          else
            {
-             /* Insert this value into the right place */
+             /* Insert this value into the right place */
              sb_reset (&ptr->actual);
              idx = get_any_string (idx + 1, in, &ptr->actual, 0, 0);
              if (ptr->actual.len > 0)
@@ -1002,10 +992,10 @@ macro_expand (idx, in, m, out, comment_char)
        }
       else
        {
-         /* This is a positional arg */
+         /* This is a positional arg */
          is_positional = 1;
          if (is_keyword)
-           return "can't mix positional and keyword arguments";
+           return _("can't mix positional and keyword arguments");
 
          if (!f)
            {
@@ -1013,7 +1003,7 @@ macro_expand (idx, in, m, out, comment_char)
              int c;
 
              if (!macro_mri)
-               return "too many positional arguments";
+               return _("too many positional arguments");
 
              f = (formal_entry *) xmalloc (sizeof (formal_entry));
              sb_new (&f->name);
@@ -1103,11 +1093,12 @@ macro_expand (idx, in, m, out, comment_char)
    gasp.  Return 1 if a macro is found, 0 otherwise.  */
 
 int
-check_macro (line, expand, comment_char, error)
+check_macro (line, expand, comment_char, error, info)
      const char *line;
      sb *expand;
      int comment_char;
      const char **error;
+     macro_entry **info;
 {
   const char *s;
   char *copy, *cs;
@@ -1130,7 +1121,7 @@ check_macro (line, expand, comment_char, error)
   memcpy (copy, line, s - line);
   copy[s - line] = '\0';
   for (cs = copy; *cs != '\0'; cs++)
-    if (isupper (*cs))
+    if (isupper ((unsigned char) *cs))
       *cs = tolower (*cs);
 
   macro = (macro_entry *) hash_find (macro_hash, copy);
@@ -1148,6 +1139,10 @@ check_macro (line, expand, comment_char, error)
 
   sb_kill (&line_sb);
 
+  /* Export the macro information if requested.  */
+  if (info)
+    *info = macro;
+
   return 1;
 }
 
@@ -1188,15 +1183,15 @@ expand_irp (irpc, idx, in, out, get_line, comment_char)
 
   sb_new (&sub);
   if (! buffer_and_nest (mn, "ENDR", &sub, get_line))
-    return "unexpected end of file in irp or irpc";
-  
+    return _("unexpected end of file in irp or irpc");
+
   sb_new (&f.name);
   sb_new (&f.def);
   sb_new (&f.actual);
 
   idx = get_token (idx, in, &f.name);
   if (f.name.len == 0)
-    return "missing model parameter";
+    return _("missing model parameter");
 
   h = hash_new ();
   err = hash_jam (h, sb_terminate (&f.name), &f);
This page took 0.03244 seconds and 4 git commands to generate.