* [Cocci] transform needing a calculation from a Python script
@ 2018-03-01 19:13 Allan, Bruce W
2018-03-01 19:46 ` Julia Lawall
0 siblings, 1 reply; 6+ messages in thread
From: Allan, Bruce W @ 2018-03-01 19:13 UTC (permalink / raw)
To: cocci
We have a macro that takes a right-justified contiguous set of bits and shifts it left to create a bitmask:
#define FOO(m, s) (m) << (s)
where m is in hex and s is an integer. I'd like to transform occurrences of it to the Linux kernel's GENMASK(high, low) macro (which creates a contiguous bitmask starting at bit position low and ending@position high), but am having problems trying to figure out how to do that.
For example, I'd like to transform:
#define BITMASK_A FOO(0x3F, 4) /* 0x3F0 */
to
#define BITMASK_A GENMASK(9, 4) /* 0x3F0 */
The value s in FOO() is the same as the value low in GENMASK(), but the value high in GENMASK() must be calculated from the values m and s in FOO(). One calculation would be to use the hamming weight (hweight) such as hweight(m) + s - 1. My Python-foo is non-existent but I have found online an example for calculating the hamming weight with the Python script 'def hweight(n): return bin(n).count("1")'. Is it possible to use this (or another) Python script in a Coccinelle script to do this transform, and if so, what would that look like? If the transform cannot use Python, is there another suggestion?
Thanks,
Bruce.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20180301/d114de3a/attachment.html>
^ permalink raw reply [flat|nested] 6+ messages in thread* [Cocci] transform needing a calculation from a Python script
2018-03-01 19:13 [Cocci] transform needing a calculation from a Python script Allan, Bruce W
@ 2018-03-01 19:46 ` Julia Lawall
2018-03-01 21:17 ` Allan, Bruce W
0 siblings, 1 reply; 6+ messages in thread
From: Julia Lawall @ 2018-03-01 19:46 UTC (permalink / raw)
To: cocci
On Thu, 1 Mar 2018, Allan, Bruce W wrote:
>
> We have a macro that takes a right-justified contiguous set of bits and
> shifts it left to create a bitmask:
>
> ?
>
> #define FOO(m, s)??????????? (m) << (s)
>
> ?
>
> where m is in hex and s is an integer.? I?d like to transform occurrences of
> it to the Linux kernel?s GENMASK(high, low) macro (which creates a
> contiguous bitmask starting at bit position low and ending at position
> high), but am having problems trying to figure out how to do that.
>
> ?
>
> For example, I?d like to transform:
>
> ?????????????? #define BITMASK_A???????? FOO(0x3F, 4)?????????????????????
> /* 0x3F0 */
>
> to
>
> ?????????????? #define BITMASK_A???????? GENMASK(9, 4)? ?????????????? /*
> 0x3F0 */
>
> ?
>
> The value s in FOO() is the same as the value low in GENMASK(), but the
> value high in GENMASK() must be calculated from the values m and s in
> FOO().? One calculation would be to use the hamming weight (hweight) such as
> hweight(m) + s ? 1.? My Python-foo is non-existent but I have found online
> an example for calculating the hamming weight with the Python script ?def
> hweight(n): return bin(n).count("1")?.? Is it possible to use this (or
> another) Python script in a Coccinelle script to do this transform, and if
> so, what would that look like?? If the transform cannot use Python, is there
> another suggestion?
I think that you can follow the model of the following code
(demos/pythontococci.cocci). Let me know if it is not clear.
julia
@a@
identifier x;
@@
foo(x);
@script:python b@
x << a.x;
y;
z;
@@
print y
coccinelle.y = x
coccinelle.z = "something"
print y
@c@
identifier b.y;
identifier b.z;
identifier a.x;
@@
- bar();
+ matched_bar(y,z,x);
^ permalink raw reply [flat|nested] 6+ messages in thread* [Cocci] transform needing a calculation from a Python script
2018-03-01 19:46 ` Julia Lawall
@ 2018-03-01 21:17 ` Allan, Bruce W
2018-03-02 6:39 ` Julia Lawall
2018-03-03 9:05 ` Robert Larice
0 siblings, 2 replies; 6+ messages in thread
From: Allan, Bruce W @ 2018-03-01 21:17 UTC (permalink / raw)
To: cocci
> -----Original Message-----
> From: Julia Lawall [mailto:julia.lawall at lip6.fr]
> Sent: Thursday, March 01, 2018 11:46 AM
> To: Allan, Bruce W <bruce.w.allan@intel.com>
> Cc: cocci at systeme.lip6.fr
> Subject: Re: [Cocci] transform needing a calculation from a Python script
>
>
>
> On Thu, 1 Mar 2018, Allan, Bruce W wrote:
>
> >
> > We have a macro that takes a right-justified contiguous set of bits and
> > shifts it left to create a bitmask:
> >
> >
> >
> > #define FOO(m, s)??????????? (m) << (s)
> >
> >
> >
> > where m is in hex and s is an integer.? I?d like to transform occurrences of
> > it to the Linux kernel?s GENMASK(high, low) macro (which creates a
> > contiguous bitmask starting at bit position low and ending at position
> > high), but am having problems trying to figure out how to do that.
> >
> >
> >
> > For example, I?d like to transform:
> >
> > ?????????????? #define BITMASK_A???????? FOO(0x3F, 4)
> > /* 0x3F0 */
> >
> > to
> >
> > ?????????????? #define BITMASK_A???????? GENMASK(9, 4)? ?????????????? /*
> > 0x3F0 */
> >
> >
> >
> > The value s in FOO() is the same as the value low in GENMASK(), but the
> > value high in GENMASK() must be calculated from the values m and s in
> > FOO().? One calculation would be to use the hamming weight (hweight) such
> as
> > hweight(m) + s ? 1.? My Python-foo is non-existent but I have found online
> > an example for calculating the hamming weight with the Python script ?def
> > hweight(n): return bin(n).count("1")?.? Is it possible to use this (or
> > another) Python script in a Coccinelle script to do this transform, and if
> > so, what would that look like?? If the transform cannot use Python, is there
> > another suggestion?
>
> I think that you can follow the model of the following code
> (demos/pythontococci.cocci). Let me know if it is not clear.
Sorry, no, it is not clear to me how this works.
>
> julia
>
> @a@
> identifier x;
> @@
>
> foo(x);
>
> @script:python b@
> x << a.x;
> y;
> z;
> @@
>
> print y
> coccinelle.y = x
> coccinelle.z = "something"
> print y
>
> @c@
> identifier b.y;
> identifier b.z;
> identifier a.x;
> @@
>
> - bar();
> + matched_bar(y,z,x);
^ permalink raw reply [flat|nested] 6+ messages in thread* [Cocci] transform needing a calculation from a Python script
2018-03-01 21:17 ` Allan, Bruce W
@ 2018-03-02 6:39 ` Julia Lawall
2018-03-03 9:05 ` Robert Larice
1 sibling, 0 replies; 6+ messages in thread
From: Julia Lawall @ 2018-03-02 6:39 UTC (permalink / raw)
To: cocci
On Thu, 1 Mar 2018, Allan, Bruce W wrote:
> > -----Original Message-----
> > From: Julia Lawall [mailto:julia.lawall at lip6.fr]
> > Sent: Thursday, March 01, 2018 11:46 AM
> > To: Allan, Bruce W <bruce.w.allan@intel.com>
> > Cc: cocci at systeme.lip6.fr
> > Subject: Re: [Cocci] transform needing a calculation from a Python script
> >
> >
> >
> > On Thu, 1 Mar 2018, Allan, Bruce W wrote:
> >
> > >
> > > We have a macro that takes a right-justified contiguous set of bits and
> > > shifts it left to create a bitmask:
> > >
> > >
> > >
> > > #define FOO(m, s)??????????? (m) << (s)
> > >
> > >
> > >
> > > where m is in hex and s is an integer.? I?d like to transform occurrences of
> > > it to the Linux kernel?s GENMASK(high, low) macro (which creates a
> > > contiguous bitmask starting at bit position low and ending at position
> > > high), but am having problems trying to figure out how to do that.
> > >
> > >
> > >
> > > For example, I?d like to transform:
> > >
> > > ?????????????? #define BITMASK_A???????? FOO(0x3F, 4)
> > > /* 0x3F0 */
> > >
> > > to
> > >
> > > ?????????????? #define BITMASK_A???????? GENMASK(9, 4)? ?????????????? /*
> > > 0x3F0 */
> > >
> > >
> > >
> > > The value s in FOO() is the same as the value low in GENMASK(), but the
> > > value high in GENMASK() must be calculated from the values m and s in
> > > FOO().? One calculation would be to use the hamming weight (hweight) such
> > as
> > > hweight(m) + s ? 1.? My Python-foo is non-existent but I have found online
> > > an example for calculating the hamming weight with the Python script ?def
> > > hweight(n): return bin(n).count("1")?.? Is it possible to use this (or
> > > another) Python script in a Coccinelle script to do this transform, and if
> > > so, what would that look like?? If the transform cannot use Python, is there
> > > another suggestion?
> >
> > I think that you can follow the model of the following code
> > (demos/pythontococci.cocci). Let me know if it is not clear.
>
> Sorry, no, it is not clear to me how this works.
The first rule matches whatever you want to change and established some
metavariables that contain information you will want to give to python.
The second rule declares some new variables y and z, as well as inheriting
information from the first rule. This is where you call your python code,
and you assign the new variables to whatever you will need to compute your
result. The last rule uses the information obtained from the first and
second rules to make the transformation. It declares the metavariables
inherited from python as identifiers, but they can contain any
more complex expression as well.
julia
>
> >
> > julia
> >
> > @a@
> > identifier x;
> > @@
> >
> > foo(x);
> >
> > @script:python b@
> > x << a.x;
> > y;
> > z;
> > @@
> >
> > print y
> > coccinelle.y = x
> > coccinelle.z = "something"
> > print y
> >
> > @c@
> > identifier b.y;
> > identifier b.z;
> > identifier a.x;
> > @@
> >
> > - bar();
> > + matched_bar(y,z,x);
>
^ permalink raw reply [flat|nested] 6+ messages in thread* [Cocci] transform needing a calculation from a Python script
2018-03-01 21:17 ` Allan, Bruce W
2018-03-02 6:39 ` Julia Lawall
@ 2018-03-03 9:05 ` Robert Larice
2018-03-03 9:40 ` Julia Lawall
1 sibling, 1 reply; 6+ messages in thread
From: Robert Larice @ 2018-03-03 9:05 UTC (permalink / raw)
To: cocci
Hello,
attached is a very rough piece of .cocci which might
be an aproximation of your wish and julias hint.
I assembled it as a sort of excersice for myself.
Regards,
Robert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ex9.c
Type: text/x-csrc
Size: 312 bytes
Desc: not available
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20180303/dcd2f79d/attachment.bin>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ex9.cocci
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20180303/dcd2f79d/attachment.ksh>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-03-03 9:40 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-01 19:13 [Cocci] transform needing a calculation from a Python script Allan, Bruce W
2018-03-01 19:46 ` Julia Lawall
2018-03-01 21:17 ` Allan, Bruce W
2018-03-02 6:39 ` Julia Lawall
2018-03-03 9:05 ` Robert Larice
2018-03-03 9:40 ` Julia Lawall
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.