From patchwork Wed May 8 17:11:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Clifton X-Patchwork-Id: 89803 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5505A384387D for ; Wed, 8 May 2024 17:11:48 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E9107385E836 for ; Wed, 8 May 2024 17:11:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E9107385E836 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E9107385E836 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715188281; cv=none; b=cVr0wWJeCjTmaXncN7RjxpNG1epGYrpeIE9Ss4iCtdI9Yh436My2rpJYp6gmu2xmusabXW5dmCh8WCxlcQ3q2kF7+CVh4ZALJh0CtUsOr1sNPvmTheX11fYWsXjOQSRhveiuGbAMOV74S38WhqICeuhBzaRqOVP3VR3DNjozmiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715188281; c=relaxed/simple; bh=hipvNnXwkyUvbKYKPNIWg8T1czgZmD6Kk29D0C+T3bw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gDjHH2VSVfphH/OyByiT+Th9vDAskJvwrLtrwUpfAFgSHSmpPzR09v99xGyqBOgkXeLhNjU8njXZf6d+oLvAaqGCvyeJZqxim7npf/74U5qFu7CKg5htFBGRN1M8fypsnnBxeqHnTuAms38PZgHThOeSDq4NHeS6vCb96T8nThw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715188278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=vSFGhqcx4N1jrF7izGA8YiQmClldwEsjwaoSRgUgusM=; b=HYl29WqgfAKgBPtVeA2RxU2mVvUDCiAwOYlt0F+2LPxE3K4b3L/PgFBSHoozl//hyu2Zch Q1ckIdS0RUIL7md4wO5gNBljdKRnLhqNHIzkXxay9e8Q/fRPjJfNTmiZk9ryX2Zz5ppv8u E15hYHyhzOoJnSXmR4zWALXQCU3d4X0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-189-fio0XZjqNbqV5YHhlwhn4Q-1; Wed, 08 May 2024 13:11:16 -0400 X-MC-Unique: fio0XZjqNbqV5YHhlwhn4Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 345FB85A58C for ; Wed, 8 May 2024 17:11:16 +0000 (UTC) Received: from prancer.redhat.com (unknown [10.42.28.123]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6DBCF40C6EB7 for ; Wed, 8 May 2024 17:11:15 +0000 (UTC) From: Nick Clifton To: binutils@sourceware.org Subject: RFC: Maintaining a per-macro invocation count Date: Wed, 08 May 2024 18:11:13 +0100 Message-ID: <87pltw9qku.fsf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org Hi Guys, The attached patch implements a extension to the assembler's macro syntax, allowing a macro to access a count of the number of times that it has been executed via the \+ pseudo-variable. ie like this: .macro foo .print "I have been invoked \+ times" .endm This syntax is similar to the already supported \@ pseudo-variable, except that the count is maintained on a per-macro basis. The drive for this extension is that I want to be able to generate N references to unique symbols in file A and then N definitions of those symbols in file B. Using assembler macros makes this easy, providing that I can ensure that the names in file A will match those in file B. ie: fileA.s: .macro gen_refs count .dc.a ref_\+ .if \count > 1 gen_refs \count-1 .endif .endm gen_refs 1000 fileB.s: .macro gen_defs count .global ref_\+ ref_\+ : .if \count > 1 gen_defs \count-1 .endif .endm gen_defs 1000 (This is part of a larger exercise to generate some micro benchmarks for the assembler that exercise various different features. In this case I am looking into relocation processing). Comments, suggestions ? Cheers Nick PS. In the course of creating this patch I noticed that the altmacto test (gas/testsuite/gas/macros/altmacro.d) does not appear to be run. So I have fixed that as well. diff --git a/gas/NEWS b/gas/NEWS index cb58ca8fb8d..0fbd244c518 100644 --- 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. diff --git a/gas/doc/as.texi b/gas/doc/as.texi index 42db11e91da..1fb97c4cd9f 100644 --- a/gas/doc/as.texi +++ b/gas/doc/as.texi @@ -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}.} diff --git a/gas/macro.c b/gas/macro.c index e2ee39bc209..72d869d317f 100644 --- a/gas/macro.c +++ b/gas/macro.c @@ -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", ¯o->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; } diff --git a/gas/macro.h b/gas/macro.h index a12fb894bec..e87f64e70ca 100644 --- a/gas/macro.h +++ b/gas/macro.h @@ -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. */ diff --git a/gas/testsuite/gas/macros/altmacro.d b/gas/testsuite/gas/macros/altmacro.d index 49c04aa3f79..e9ca349e1c7 100644 --- a/gas/testsuite/gas/macros/altmacro.d +++ b/gas/testsuite/gas/macros/altmacro.d @@ -1,3 +1,2 @@ -local -.LL0001 -.LL0002 +#name: Local label generation in alternate macros (altmacro) + diff --git a/gas/testsuite/gas/macros/macros.exp b/gas/testsuite/gas/macros/macros.exp index 38e613f088a..3108f3fcd56 100644 --- a/gas/testsuite/gas/macros/macros.exp +++ b/gas/testsuite/gas/macros/macros.exp @@ -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 --- /dev/null 2024-05-08 09:06:03.744530866 +0100 +++ current/gas/testsuite/gas/macros/altmacro.l 2024-05-08 17:45:47.116122421 +0100 @@ -0,0 +1,3 @@ +local.* +.*LL0001 +.*LL0002 --- /dev/null 2024-05-08 09:06:03.744530866 +0100 +++ current/gas/testsuite/gas/macros/count.s 2024-05-08 17:35:31.770892104 +0100 @@ -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 --- /dev/null 2024-05-08 09:06:03.744530866 +0100 +++ current/gas/testsuite/gas/macros/count.d 2024-05-08 17:46:54.781255760 +0100 @@ -0,0 +1,3 @@ +#name: Macro counters (count.d) +# Tests that \@ counts total macro invocation count +# and that \+ counts individual macro invocation count. --- /dev/null 2024-05-08 09:06:03.744530866 +0100 +++ current/gas/testsuite/gas/macros/count.l 2024-05-08 17:45:35.497099524 +0100 @@ -0,0 +1,10 @@ +0 +0 +1 +1 +2 +0 +3 +1 +4 +2