qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] decodetree: Allow use of hex/bin format for argument field values
@ 2020-11-30 12:26 Philippe Mathieu-Daudé
  2020-11-30 12:32 ` Philippe Mathieu-Daudé
  2020-11-30 18:22 ` Richard Henderson
  0 siblings, 2 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-11-30 12:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: Paolo Bonzini, Philippe Mathieu-Daudé,
	Richard Henderson, Eduardo Habkost, Cleber Rosa

ISA datasheets often use binary or hexadecimal constant values.
By doing base conversion, we might introduce bugs. Safer is to
copy/paste the datasheet value.
Add support for bin/hex constants in argument field token.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
Is there a more pythonic way to write this if/elif/else
loop without re.fullmatch?
---
 scripts/decodetree.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/decodetree.py b/scripts/decodetree.py
index 47aa9caf6d1..d2ecc61813f 100644
--- a/scripts/decodetree.py
+++ b/scripts/decodetree.py
@@ -849,9 +849,15 @@ def parse_generic(lineno, parent_pat, name, toks):
             continue
 
         # 'Foo=number' sets an argument field to a constant value
-        if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
+        if re.fullmatch(re_C_ident + '=[+-]?(0[bx])?[0-9]+', t):
             (fname, value) = t.split('=')
-            value = int(value)
+            if re.fullmatch('[+-]?0b[0-9]+', value):
+                base = 2
+            elif re.fullmatch('[+-]?0x[0-9]+', value):
+                base = 16
+            else:
+                base = 10
+            value = int(value, base)
             flds = add_field(lineno, flds, fname, ConstField(value))
             continue
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] decodetree: Allow use of hex/bin format for argument field values
  2020-11-30 12:26 [PATCH] decodetree: Allow use of hex/bin format for argument field values Philippe Mathieu-Daudé
@ 2020-11-30 12:32 ` Philippe Mathieu-Daudé
  2020-11-30 18:22 ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-11-30 12:32 UTC (permalink / raw)
  To: qemu-devel@nongnu.org Developers
  Cc: Paolo Bonzini, Richard Henderson, Eduardo Habkost, Cleber Rosa

On Mon, Nov 30, 2020 at 1:26 PM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
> ISA datasheets often use binary or hexadecimal constant values.
> By doing base conversion, we might introduce bugs. Safer is to
> copy/paste the datasheet value.
> Add support for bin/hex constants in argument field token.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> Is there a more pythonic way to write this if/elif/else
> loop without re.fullmatch?

BTW I discarded the simple "int(value, 0)" to have strict checks,
but if this is enough it is certainly simpler.

> ---
>  scripts/decodetree.py | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/decodetree.py b/scripts/decodetree.py
> index 47aa9caf6d1..d2ecc61813f 100644
> --- a/scripts/decodetree.py
> +++ b/scripts/decodetree.py
> @@ -849,9 +849,15 @@ def parse_generic(lineno, parent_pat, name, toks):
>              continue
>
>          # 'Foo=number' sets an argument field to a constant value
> -        if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
> +        if re.fullmatch(re_C_ident + '=[+-]?(0[bx])?[0-9]+', t):
>              (fname, value) = t.split('=')
> -            value = int(value)
> +            if re.fullmatch('[+-]?0b[0-9]+', value):
> +                base = 2
> +            elif re.fullmatch('[+-]?0x[0-9]+', value):
> +                base = 16
> +            else:
> +                base = 10
> +            value = int(value, base)
>              flds = add_field(lineno, flds, fname, ConstField(value))
>              continue
>
> --
> 2.26.2
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] decodetree: Allow use of hex/bin format for argument field values
  2020-11-30 12:26 [PATCH] decodetree: Allow use of hex/bin format for argument field values Philippe Mathieu-Daudé
  2020-11-30 12:32 ` Philippe Mathieu-Daudé
@ 2020-11-30 18:22 ` Richard Henderson
  2020-12-01  8:47   ` Philippe Mathieu-Daudé
  1 sibling, 1 reply; 4+ messages in thread
From: Richard Henderson @ 2020-11-30 18:22 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Paolo Bonzini, Eduardo Habkost, Cleber Rosa

On 11/30/20 6:26 AM, Philippe Mathieu-Daudé wrote:
>          # 'Foo=number' sets an argument field to a constant value
> -        if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
> +        if re.fullmatch(re_C_ident + '=[+-]?(0[bx])?[0-9]+', t):
>              (fname, value) = t.split('=')
> -            value = int(value)
> +            if re.fullmatch('[+-]?0b[0-9]+', value):
> +                base = 2
> +            elif re.fullmatch('[+-]?0x[0-9]+', value):
> +                base = 16
> +            else:
> +                base = 10
> +            value = int(value, base)
>              flds = add_field(lineno, flds, fname, ConstField(value))
>              continue

Well, the regxps are off.  No letters for the hex, and 9 accepted for binary.
I think with the right regexps, just trusting int(value, 0) is good enough.

So maybe something like

  re_C_ident + "=[+-]?([0-9]+|0x[0-9a-fA-F]+|0b[01]+)"


r~


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] decodetree: Allow use of hex/bin format for argument field values
  2020-11-30 18:22 ` Richard Henderson
@ 2020-12-01  8:47   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-12-01  8:47 UTC (permalink / raw)
  To: Richard Henderson, qemu-devel; +Cc: Paolo Bonzini, Eduardo Habkost, Cleber Rosa

On 11/30/20 7:22 PM, Richard Henderson wrote:
> On 11/30/20 6:26 AM, Philippe Mathieu-Daudé wrote:
>>          # 'Foo=number' sets an argument field to a constant value
>> -        if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
>> +        if re.fullmatch(re_C_ident + '=[+-]?(0[bx])?[0-9]+', t):
>>              (fname, value) = t.split('=')
>> -            value = int(value)
>> +            if re.fullmatch('[+-]?0b[0-9]+', value):
>> +                base = 2
>> +            elif re.fullmatch('[+-]?0x[0-9]+', value):
>> +                base = 16
>> +            else:
>> +                base = 10
>> +            value = int(value, base)
>>              flds = add_field(lineno, flds, fname, ConstField(value))
>>              continue
> 
> Well, the regxps are off.  No letters for the hex, and 9 accepted for binary.
> I think with the right regexps, just trusting int(value, 0) is good enough.

OK!

> 
> So maybe something like
> 
>   re_C_ident + "=[+-]?([0-9]+|0x[0-9a-fA-F]+|0b[01]+)"

Better indeed :)

Thanks,

Phil.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-01  8:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-30 12:26 [PATCH] decodetree: Allow use of hex/bin format for argument field values Philippe Mathieu-Daudé
2020-11-30 12:32 ` Philippe Mathieu-Daudé
2020-11-30 18:22 ` Richard Henderson
2020-12-01  8:47   ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).