Repost of Gcc plugin for modification of source code ?

2014-06-15 Thread Mohsin Khan
Hi,
I'm working on a gcc plugin project. I had a doubt, can somebody help
me on this.
Can we write a plugin (which works after a pass like cfg or any other)
which can modify the source code of .C file, for example insert
comments before function definitions or insert a new IF Block in the
.C file. And after modification it writes the entire modified code to
a new .C file. Is this type of plugin possible in GCC.
 If yes, please tell me how and if No, please tell me why.
Any help is much appreciated.

Note : This question was also answered by Basile Starynkevitch, thanks to him.
I want thoughts from other developers also from GCC. So can a Gcc
developer help me with this, and share there point of view.
Any help is appreciated.
Thanks


Re: Gcc plugin for modification of source code ?

2014-06-15 Thread Mohsin Khan
Thanks Basile Starynkevitch for the useful reply.


On 6/15/14, Basile Starynkevitch  wrote:
> On Sun, 2014-06-15 at 02:40 +0530, Mohsin Khan wrote:
>> Hi,
>> I'm working on a gcc plugin project. I had a doubt, can somebody help
>> me on this.
>
> [no need to ask on gcc-help@; plugins are definitely gcc internals
> related and should better be discussed on gcc@gcc.gnu.org]
>
>> Can we write a plugin (which works after a pass like cfg or any other)
>> which can modify the source code of .C file, for example insert
>> comments before function definitions or insert a new IF Block in the
>> .C file. And after modification it writes the entire modified code to
>> a new .C file. Is this type of plugin possible in GCC. If yes, please
>> tell me how and if No, please tell me why.
>
> If your source file does not contain any #line directive, a GCC plugin
> (or a MELT extension, see http://gcc-melt.org/ for more) is able to get
> the source code position of every Gimple instruction. (But did you think
> about instruction obtained by some macro expansions?).
>
> With that information, you can indeed transform the C source as you
> wish. It might not be simple (you need to build an editor-like internal
> represention of each line of the source code).
>
> However, modifying the Gimple representation (without modifying any C++
> source file) is probably simpler.
>
> What exactly do you want to achieve? Are you sure that a source to
> source transformation is worthwhile -given that you can transform
> internal GCC representations like Gimples-?
>
> Regards
>
> --
> Basile STARYNKEVITCH http://starynkevitch.net/Basile/
> email: basilestarynkevitchnet mobile: +33 6 8501 2359
> 8, rue de la Faiencerie, 92340 Bourg La Reine, France
> *** opinions {are only mine, sont seulement les miennes} ***
>
>
>


Gcc plugin for modification of source code ?

2014-06-14 Thread Mohsin Khan
Hi,
I'm working on a gcc plugin project. I had a doubt, can somebody help
me on this.
Can we write a plugin (which works after a pass like cfg or any other)
which can modify the source code of .C file, for example insert
comments before function definitions or insert a new IF Block in the
.C file. And after modification it writes the entire modified code to
a new .C file. Is this type of plugin possible in GCC. If yes, please
tell me how and if No, please tell me why.
Any help is much appreciated.
Thanks


Re: Can Some one please help me on this gcc plugin..

2014-03-06 Thread Mohsin Khan
Hi David,

 I am very much thankful to you as your answer worked wonders for me.
The problem with the plugin is solved now. Thanks again.


On 3/5/14, David Malcolm  wrote:
> On Wed, 2014-03-05 at 21:58 +0530, Mohsin Khan wrote:
>> Hi,
>>
>>  I am developing plugins for the GCC-4.8.2. I am a newbie in plugins.
>> I wrote a plugin and tried to count and see the Goto Statements using
>> the gimple_stmt_iterator. I get gimple statements printed on my
>> stdout, but I am not able to find the line which has goto statements.
>> I only get other lines such as variable declaration and logic
>> statements, but no goto statements.
>>   When I open the Gimple/SSA/CFG file seperately using the vim editor
>> I find the goto statements are actually present.
>>   So, can anyone help me. How can I actually get the count of Goto
>> statements or atleast access these goto statements using some
>> iterator.
>>   I have used -fdump-tree-all, -fdump-tree-cfg as flags.
>>
>> Here is the pseudocode:
>>
>> struct register_pass_info pass_info = {
>> &(pass_plugin.pass), /* Address of new pass,
>> here, the 'struct
>>  opt_pass' field of
>> 'gimple_opt_pass'
>>  defined above */
>> "ssa",   /* Name of the reference
>> pass for hooking up
>>  the new pass.   ??? */
>> 0,   /* Insert the pass at the
>> specified instance
>>  number of the reference
>> pass. Do it for
>>  every instance if it is 0.
>> */
>> PASS_POS_INSERT_AFTER/* how to insert the new
>> pass: before,
>
> You're inserting your pass after the "ssa" pass, which converts the CFG
> to SSA form.  This is run *after* the function has been converted from a
> flat list of gimple statements into a CFG of basic blocks, and that CFG
> conversion eliminates the goto statements in favor of edges within the
> CFG.  If you see "goto" in the dump, that's presumably just a textual
> way of expressing an edge in the CFG.
>
> To see gimple goto statements, you need to run your pass *before* the
> convertion to CFG, which happens fairly early on, in the "cfg" pass
>
> FWIW there's a diagram showing the passes here:
> http://gcc-python-plugin.readthedocs.org/en/latest/tables-of-passes.html
>
>
> Hope this is helpful
> Dave
>
>

Regards
Mohsin Khan


Can Some one please help me on this gcc plugin..

2014-03-05 Thread Mohsin Khan
Hi,

 I am developing plugins for the GCC-4.8.2. I am a newbie in plugins.
I wrote a plugin and tried to count and see the Goto Statements using
the gimple_stmt_iterator. I get gimple statements printed on my
stdout, but I am not able to find the line which has goto statements.
I only get other lines such as variable declaration and logic
statements, but no goto statements.
  When I open the Gimple/SSA/CFG file seperately using the vim editor
I find the goto statements are actually present.
  So, can anyone help me. How can I actually get the count of Goto
statements or atleast access these goto statements using some
iterator.
  I have used -fdump-tree-all, -fdump-tree-cfg as flags.

Here is the pseudocode:

struct register_pass_info pass_info = {
&(pass_plugin.pass), /* Address of new pass,
here, the 'struct
 opt_pass' field of
'gimple_opt_pass'
 defined above */
"ssa",   /* Name of the reference
pass for hooking up
 the new pass.   ??? */
0,   /* Insert the pass at the
specified instance
 number of the reference
pass. Do it for
 every instance if it is 0. */
PASS_POS_INSERT_AFTER/* how to insert the new
pass: before,
 after, or replace. Here
we are inserting
 a pass names 'plug' after
the pass named
 'pta' */
};

.

static unsigned int dead_code_elimination (void)
{

   FOR_EACH_BB_FN (bb, cfun)
 {
  //  gimple_dump_bb(stdout,bb,0,0);
 //printf("\nIn New BB");

   gsi2= gsi_after_labels (bb);
  print_gimple_stmt(stdout,gsi_stmt(gsi2),0,0);
 /*Iterating over each gimple statement in a basic block*/
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
   g = gsi_stmt(gsi);

print_gimple_stmt(stdout,g,0,0);

  if (gimple_code(g)==GIMPLE_GOTO)
  printf("\nFound GOTO stmt\n");

print_gimple_stmt(stdout,gsi_stmt(gsi),0,0);
  //analyze_gimple_statement (gsi);
 }
   }
}


Re: Help Required on Missing GOTO statements in Gimple/SSA/CFG Pass ...

2014-03-05 Thread Mohsin Khan
Hi,
 I am extremely sorry as I couldn't reply from many days. Actually I
was busy with some personal work so I didn't work for many days.
 I didn't use MELT because, I didn't want learn a new language and
also my professor wanted me to code the plugin in C/C++ .

On 2/18/14, Basile Starynkevitch  wrote:
> On Tue, 2014-02-18 at 11:17 +0530, Mohsin Khan wrote:
>> Hi,
>>
>>  I am developing plugins for the GCC-4.8.2. I am a newbie in plugins.
>> I wrote a plugin and tried to count and see the Goto Statements using
>> the gimple_stmt_iterator. I get gimple statements printed on my
>> stdout, but I am not able to find the line which has goto statements.
>
> I guess that most GOTOs are just becoming implicit as the link to the
> next basic block.
>
> Probably
>
>if (!cond) goto end;
>something;
>   end:;
>
> has nearly the same Gimple representation than
>while (cond) {
>  something;
>}
>
> BTW, did you consider using MELT http://gcc-melt.org/ to code your GCC
> extension?
>
> --
> Basile STARYNKEVITCH http://starynkevitch.net/Basile/
> email: basilestarynkevitchnet mobile: +33 6 8501 2359
> 8, rue de la Faiencerie, 92340 Bourg La Reine, France
> *** opinions {are only mine, sont seulement les miennes} ***
>
>
>


Help Required on Missing GOTO statements in Gimple/SSA/CFG Pass ...

2014-02-17 Thread Mohsin Khan
Hi,

 I am developing plugins for the GCC-4.8.2. I am a newbie in plugins.
I wrote a plugin and tried to count and see the Goto Statements using
the gimple_stmt_iterator. I get gimple statements printed on my
stdout, but I am not able to find the line which has goto statements.
I only get other lines such as variable declaration and logic
statements, but no goto statements.
  When I open the Gimple/SSA/CFG file seperately using the vim editor
I find the goto statements are actually present.
  So, can anyone help me. How can I actually get the count of Goto
statements or atleast access these goto statements using some
iterator.
  I have used -fdump-tree-all, -fdump-tree-cfg as flags.

Here is the pseudocode:

struct register_pass_info pass_info = {
&(pass_plugin.pass), /* Address of new pass,
here, the 'struct
 opt_pass' field of
'gimple_opt_pass'
 defined above */
"ssa",   /* Name of the reference
pass for hooking up
 the new pass.   ??? */
0,   /* Insert the pass at the
specified instance
 number of the reference
pass. Do it for
 every instance if it is 0. */
PASS_POS_INSERT_AFTER/* how to insert the new
pass: before,
 after, or replace. Here
we are inserting
 a pass names 'plug' after
the pass named
 'pta' */
};

.

static unsigned int dead_code_elimination (void)
{

   FOR_EACH_BB_FN (bb, cfun)
 {
  //  gimple_dump_bb(stdout,bb,0,0);
 //printf("\nIn New BB");

   gsi2= gsi_after_labels (bb);
  print_gimple_stmt(stdout,gsi_stmt(gsi2),0,0);
 /*Iterating over each gimple statement in a basic block*/
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
   g = gsi_stmt(gsi);

print_gimple_stmt(stdout,g,0,0);

  if (gimple_code(g)==GIMPLE_GOTO)
  printf("\nFound GOTO stmt\n");

print_gimple_stmt(stdout,gsi_stmt(gsi),0,0);
  //analyze_gimple_statement (gsi);
 }
   }
}