From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id CBECC3858D33 for ; Wed, 1 Mar 2023 03:09:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBECC3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [10.0.0.170] (unknown [217.28.27.60]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id F25D01E110; Tue, 28 Feb 2023 22:09:03 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1677640144; bh=cVZEs3a4L8Yev9COG49OuU+TSLALl1gwUqEYKtTvLY4=; h=Date:From:Subject:To:References:In-Reply-To:From; b=b1rviCl4SRheJt28Mx4lHmJTuOpsUT+l46ru5/N0ykvnHKYyUuewqgMZ/u+u124OK pHSd8p4IVt9rlV+LroOmaq3OYfF+Pmvz3TA2fgeZM75Vx30lQ3kUhhsNMArMlFLAyV w0wgSuylNxSPlxH+rbg1ScMMRzFEGnIDo+ISXi2Q= Message-ID: Date: Tue, 28 Feb 2023 22:09:03 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 From: Simon Marchi Subject: Re: [PATCH 1/2] gdb: updates to gdbarch.py algorithm To: Andrew Burgess , gdb-patches@sourceware.org References: Content-Language: fr In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,SPF_HELO_PASS,SPF_PASS,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 List-Id: On 2/28/23 11:51, Andrew Burgess via Gdb-patches wrote: > Restructure how gdbarch.py generates the verify_gdbarch function. > Previously the postdefault handling was bundled together with the > validation. This means that a field can't have both a postdefault, > and set its invalid attribute to a string. > > This doesn't seem reasonable to me, I see no reason why a field can't > have both a postdefault (used when the tdep doesn't set the field), > and an invalid expression, which can be used to validate the value > that a tdep might set. > > In this commit I restructure the verify_gdbarch generation code to > allow the above, there is no change in the actual generated code in > this commit, that will come in later commit. > > I did end up having to remove the "invalid" attribute (where the > attribute was set to True) from a number of fields in this commit. > This invalid attribute was never having an effect as these components > all have a postdefault. Consider; the "postdefault" is applied if the > field still has its initial value, while an "invalid" attribute set to > True means error if the field still has its default value. But the > field never will have its default value, it will always have its > postdefault value. > --- > gdb/gdbarch.py | 31 ++++++++++++++++--------- > gdb/gdbarch_components.py | 49 ++++++++++++++------------------------- > 2 files changed, 37 insertions(+), 43 deletions(-) > > diff --git a/gdb/gdbarch.py b/gdb/gdbarch.py > index 93b1e8bf84e..7fea41c9572 100755 > --- a/gdb/gdbarch.py > +++ b/gdb/gdbarch.py > @@ -203,35 +203,44 @@ with open("gdbarch.c", "w") as f: > file=f, > ) > for c in filter(not_info, components): > - if c.invalid is False: > - print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f) > - elif c.predicate: > - print(f" /* Skip verify of {c.name}, has predicate. */", file=f) > - elif isinstance(c.invalid, str) and c.postdefault is not None: > - print(f" if ({c.invalid})", file=f) > - print(f" gdbarch->{c.name} = {c.postdefault};", file=f) > - elif c.predefault is not None and c.postdefault is not None: > + # An opportunity to write in the 'postdefault' value. > + if c.postdefault is not None and c.predefault is not None: > print(f" if (gdbarch->{c.name} == {c.predefault})", file=f) > print(f" gdbarch->{c.name} = {c.postdefault};", file=f) > elif c.postdefault is not None: > print(f" if (gdbarch->{c.name} == 0)", file=f) > print(f" gdbarch->{c.name} = {c.postdefault};", file=f) I would find this postdefault snippet easier to read like this, with a single "if c.postdefault is not None", and then another condition inside to decide what we should compare against: if c.postdefault is not None: if c.predefault is not None: print(f" if (gdbarch->{c.name} == {c.predefault})", file=f) print(f" gdbarch->{c.name} = {c.postdefault};", file=f) else: print(f" if (gdbarch->{c.name} == 0)", file=f) print(f" gdbarch->{c.name} = {c.postdefault};", file=f) or even if c.postdefault is not None: predefault = c.predefault or "0" print(f" if (gdbarch->{c.name} == {predefault})", file=f) print(f" gdbarch->{c.name} = {c.postdefault};", file=f) > + > + # Now validate the value. > + if c.invalid is False: > + print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f) > + elif c.predicate: > + print(f" /* Skip verify of {c.name}, has predicate. */", file=f) > + elif c.invalid is None: I think it's confusing for the "invalid" parameter to be able to be None, that it's one to many state versus what we need to be able to represent. I think we can get by with string, True and False, where True means "auto", where the validity check is generated if it makes sense to. Having one less state would help simplify things. I hacked this locally and it seems to work. I can post this as a cleanup before or on top of your patch, as you prefer. Another cleanup that would help me understand what is going on would be to change this long list of if/elif to something that looks more like a decision tree. On top of your patch, and on top of my suggestion to get rid of the invalid=None state, this is what I made looks like: predefault = c.predefault or "0" # Now validate the value. if type(c.invalid) is str: print(f" if ({c.invalid})", file=f) print(f""" log.puts ("\\n\\t{c.name}");""", file=f) elif c.invalid: if c.predicate: print(f" /* Skip verify of {c.name}, has predicate. */", file=f) elif c.postdefault: # We currently don't print anything, but we could print: # print(f" /* Skip verify of {c.name}, has predicate. */", file=f) pass else: print(f" if (gdbarch->{c.name} == {predefault})", file=f) print(f""" log.puts ("\\n\\t{c.name}");""", file=f) else: print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f) That structure is clearer to me. We see clearly the portions handling the three states of "invalid" (str, True and False). Inside invalid == True (which really means "auto"), we see that we skip generating the check when either predicate or postdefault is set, the two situations where generating the check doesn't make sense. Another nice thing about this is that there isn't the "unhandled case when generating gdbarch validation" case at the end. Each branch of the decision tree has an outcome. Again, if you agree with this cleanup, we could do it before or after your patch, as you wish. > diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py > index caa65c334ec..1d420a513f9 100644 > --- a/gdb/gdbarch_components.py > +++ b/gdb/gdbarch_components.py > @@ -63,34 +63,28 @@ > # * "predefault", "postdefault", and "invalid" - These are used for > # the initialization and verification steps: > # > -# A gdbarch is zero-initialized. Then, if a field has a pre-default, > -# the field is set to that value. After initialization is complete > -# (that is, after the tdep code has a chance to change the settings), > -# the post-initialization step is done. > +# A gdbarch is zero-initialized. Then, if a field has a "predefault", > +# the field is set to that value. This becomes the fields initial Are you missing an apostrophe after fields? > +# value. > # > -# There is a generic algorithm to generate a "validation function" for > -# all fields. If the field has an "invalid" attribute with a string > -# value, then this string is the expression (note that a string-valued > -# "invalid" and "predicate" are mutually exclusive; and the case where > -# invalid is True means to ignore this field and instead use the > -# default checking that is about to be described). Otherwise, if > -# there is a "predefault", then the field is valid if it differs from > -# the predefault. Otherwise, the check is done against 0 (really NULL > -# for function pointers, but same idea). > -# > -# In post-initialization / validation, there are several cases. > +# After initialization is complete (that is, after the tdep code has a > +# chance to change the settings), the post-initialization step is > +# done. > # > -# * If "invalid" is False, or if the field specifies "predicate", > -# validation is skipped. Otherwise, a validation step is emitted. > +# If the field still has its initial value (see above), and the field > +# has a "postdefault", then the post field is set to this value. Do you really mean to say "the post field", and not just "the field"? > # > -# * Otherwise, the validity is checked using the usual validation > -# function (see above). If the field is considered valid, nothing is > -# done. > +# After the possible "postdefault" assignment, validation is > +# performed for fields that don't have a "predicate". > # > -# * Otherwise, the field's value is invalid. If there is a > -# "postdefault", then the field is assigned that value. > +# If the field has an "invalid" attribute with a string value, then > +# this string is the expression that should evaluate to true when the > +# field is invalid. > # > -# * Otherwise, the gdbarch will fail validation and gdb will crash. > +# Otherwise, if "invalid" is True, then the generic validation > +# function is used: the field is considered invalid it it still double "it" Simon