]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add new assembler macro pseudo-variable \+ which counts the number of times a macro...
authorNick Clifton <nickc@redhat.com>
Mon, 13 May 2024 08:56:09 +0000 (09:56 +0100)
committerNick Clifton <nickc@redhat.com>
Mon, 13 May 2024 08:56:09 +0000 (09:56 +0100)
gas/NEWS
gas/doc/as.texi
gas/macro.c
gas/macro.h
gas/testsuite/gas/macros/altmacro.d
gas/testsuite/gas/macros/altmacro.l [new file with mode: 0644]
gas/testsuite/gas/macros/count.d [new file with mode: 0644]
gas/testsuite/gas/macros/count.l [new file with mode: 0644]
gas/testsuite/gas/macros/count.s [new file with mode: 0644]
gas/testsuite/gas/macros/macros.exp

index cb58ca8fb8d40b7b34a23452ec0b37bdb6049570..0fbd244c518cfcca598011ffcd840cd98c458d70 100644 (file)
--- a/gas/NEWS
+++ b/gas/NEWS
@@ -1,5 +1,9 @@
 -*- text -*-
 
+* Assembler macros can now use the syntax \+ to access the number of times a
+  given macro has been executed.  This is similar to the already existing \@
+  syntax, except that the count is maintained on a per-macro basis.
+  
 * Support the NF feature in Intel APX.
 
 * Remove KEYLOCKER and SHA promotions from EVEX MAP4.
index 42db11e91daf10f33d0f4302abb28de46cdda79e..1fb97c4cd9f80e9ee27f3b36be2075bdb16a29cd 100644 (file)
@@ -6362,6 +6362,14 @@ Exit early from the current macro definition.
 executed in this pseudo-variable; you can copy that number to your
 output with @samp{\@@}, but @emph{only within a macro definition}.
 
+@cindex number of times a macro has been executed
+@cindex macro, execution count
+@item \+
+Similar to the @code{\@@} pseudo-variable, @command{@value{AS}} also maintains
+a per-macro count of the number of times that that macro has been executed.
+You can copy that number to your output with @samp{\+}, but
+@emph{only within a macro definition}.
+
 @item LOCAL @var{name} [ , @dots{} ]
 @emph{Warning: @code{LOCAL} is only available if you select ``alternate
 macro syntax'' with @samp{--alternate} or @code{.altmacro}.}
index e2ee39bc20927ceb7612587200aba80c667e7cc6..72d869d317fd6f9f687acc0501914dddb0c3c222 100644 (file)
@@ -56,7 +56,7 @@ int macro_defined;
 
 /* Number of macro expansions that have been done.  */
 
-static int macro_number;
+static unsigned int macro_number;
 
 static void free_macro (macro_entry *);
 
@@ -668,6 +668,7 @@ define_macro (sb *in, sb *label, size_t (*get_line) (sb *))
   macro->formal_count = 0;
   macro->formals = 0;
   macro->formal_hash = str_htab_create ();
+  macro->count = 0;
 
   idx = sb_skip_white (0, in);
   if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
@@ -846,11 +847,20 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
            }
          else if (src < in->len && in->ptr[src] == '@')
            {
-             /* Sub in the macro invocation number.  */
+             /* Sub in the total macro invocation number.  */
 
              char buffer[12];
              src++;
-             sprintf (buffer, "%d", macro_number);
+             sprintf (buffer, "%u", macro_number);
+             sb_add_string (out, buffer);
+           }
+         else if (src < in->len && in->ptr[src] == '+')
+           {
+             /* Sub in the current macro invocation number.  */
+
+             char buffer[12];
+             src++;
+             sprintf (buffer, "%d", macro->count);
              sb_add_string (out, buffer);
            }
          else if (src < in->len && in->ptr[src] == '&')
@@ -1227,7 +1237,10 @@ macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
 
   sb_kill (&t);
   if (!err)
-    macro_number++;
+    {
+      macro_number++;
+      m->count++;
+    }
 
   return err;
 }
index a12fb894bec9952262b5cdf95f976e78e595e0d3..e87f64e70ca225fe23716e5c99a95319377f6e8a 100644 (file)
@@ -60,13 +60,14 @@ typedef struct formal_struct {
 
 typedef struct macro_struct
 {
-  sb sub;                              /* Substitution text.  */
-  int formal_count;                    /* Number of formal args.  */
-  formal_entry *formals;               /* List of formal_structs.  */
-  htab_t formal_hash;                  /* Hash table of formals.  */
-  const char *name;                    /* Macro name.  */
-  const char *file;                    /* File the macro was defined in.  */
-  unsigned int line;                   /* Line number of definition.  */
+  sb              sub;                 /* Substitution text.  */
+  int             formal_count;                /* Number of formal args.  */
+  formal_entry *  formals;             /* List of formal_structs.  */
+  htab_t          formal_hash;         /* Hash table of formals.  */
+  const char *    name;                        /* Macro name.  */
+  const char *    file;                        /* File the macro was defined in.  */
+  unsigned int    line;                        /* Line number of definition.  */
+  unsigned int    count;                /* Invocation count.  */
 } macro_entry;
 
 /* Whether any macros have been defined.  */
index 49c04aa3f79ac8574dbf1a6e080ddd78b377b38e..e9ca349e1c7fdbd4e7fbef87d23a87143729dccc 100644 (file)
@@ -1,3 +1,2 @@
-local
-.LL0001
-.LL0002
+#name: Local label generation in alternate macros (altmacro)
+
diff --git a/gas/testsuite/gas/macros/altmacro.l b/gas/testsuite/gas/macros/altmacro.l
new file mode 100644 (file)
index 0000000..e2ce9c0
--- /dev/null
@@ -0,0 +1,3 @@
+local.*
+.*LL0001
+.*LL0002
diff --git a/gas/testsuite/gas/macros/count.d b/gas/testsuite/gas/macros/count.d
new file mode 100644 (file)
index 0000000..196e052
--- /dev/null
@@ -0,0 +1,3 @@
+#name: Macro counters (count.d)
+# Tests that \@ counts total macro invocation count
+# and that \+ counts individual macro invocation count.
diff --git a/gas/testsuite/gas/macros/count.l b/gas/testsuite/gas/macros/count.l
new file mode 100644 (file)
index 0000000..3418b0b
--- /dev/null
@@ -0,0 +1,10 @@
+0
+0
+1
+1
+2
+0
+3
+1
+4
+2
diff --git a/gas/testsuite/gas/macros/count.s b/gas/testsuite/gas/macros/count.s
new file mode 100644 (file)
index 0000000..650a416
--- /dev/null
@@ -0,0 +1,19 @@
+
+.macro mac1 count=10
+       .print "\@"
+       .print "\+"
+.if \count > 1
+       mac1 \count-1
+.endif
+.endm
+
+.macro mac2 count=100
+       .print "\@"
+       .print "\+"
+.if \count > 1
+       mac2 \count-1
+.endif
+.endm
+
+mac1 2
+mac2 3
index 38e613f088ae354207b21e55427bbdd50d230f6b..3108f3fcd566cf556034246de03b72fa29360959 100644 (file)
@@ -100,3 +100,6 @@ if [string match "" [lindex [gas_run ../all/excl.s "-o /dev/null" ""] 0]] {
 #  prevented the assembler from parsing the rest of the file,
 #  and hence catching an erroroneous instruction.
 gas_test_error "exit.s" "" ".exitm outside of a macro"
+
+run_list_test altmacro
+run_list_test count