Gimple Statement

Nicolas BENOIT nicolas.benoit@cea.fr
Mon Sep 27 11:12:00 GMT 2010


Hi,

I am not sure if that's the best way to do it, but here is how it could 
be achieved :

While browsing the Gimple statements
get the tree rhs1 with gimple_op(g,1)
the tree rhs2 with gimple_op(g,2) if gimple_num_ops(g) > 2
the tree lhs with gimple_op(g,0)

Then, explore recursively each tree according to the TREE_CODE.
Below is an example where a function fills a structure named 'operand'.
It uses a 'datacess' structure to describe the type of access (indirect, 
load address, array ref, etc.); operand and dataccess structures are 
nested, for example, for an array reference :

OPERAND
+ DATACCESS(ARRAY REF)
   + OPERAND(ARRAY)
     + DATACCESS(ARRAY DATA)
   + OPERAND(INDEX)
     + DATACCESS(INDEX DATA) or INTEGER CONSTANT

The root operand points to a dataccess of type ARRAY REF.
The ARRAY REF contains itself two operands, each linked to a dataccess.
This is build by recursively browsing the tree, use the print_node() 
function or the tree browser to understand how all this is arranged, and 
have a look at tree.h

You will need a structure to describe the data as well. Here, all the 
'data' structures build are added to a global database (DATA_DB).

Note that 'operand', 'dataccess', 'data' and 'datadb' are not part of 
GCC, this is just an illustration of a tree abstraction.
According to your goal, you may not need all of this.


operand
operand_new_from_tree ( tree optree )
{
   operand o;
   enum tree_code tc;

   tc = TREE_CODE ( optree );

   if ( tc == SSA_NAME )
     {
       o = operand_new_from_tree ( SSA_NAME_VAR(optree) );
       o->ssa = optree;
       return o;
     }
   else if ( ( tc == VAR_DECL ) || ( tc == PARM_DECL ) ||
             ( tc == FUNCTION_DECL ) || ( tc == CONST_DECL ) )
     {
       data d = datadb_find_with_tuid ( DATA_DB, DECL_UID(optree) );
       if ( d == NULL )
         d = data_new_from_tree ( optree );
       o = operand_new_with_data ( d );
     }
   else if ( tc == INTEGER_CST )
     {
       o = operand_new_with_integer_value ( TREE_INT_CST_LOW(optree) );
     }
   else if ( tc == REAL_CST )
     {
       o = operand_new_with_real_value ( &TREE_REAL_CST(optree) );
     }
#if BUILDING_GCC_MINOR>5
   else if ( tc == MEM_REF )
     {
       /* operand 0 is the pointer, operand 1 is the offset */
       o = operand_new_with_dataccess ( 
dataccess_new_indirect_ref_from_tree(TREE_OPERAND(optree,0)) );
       o->da->offset = TREE_INT_CST_LOW ( TREE_OPERAND(optree,1) );
     }
#endif
   else if ( tc == ARRAY_REF )
     {
       /* operand 0 is the array, operand 1 is the index */
       o = operand_new_with_dataccess ( 
dataccess_new_array_ref_from_trees(TREE_OPERAND(optree,0),TREE_OPERAND(optree,1)) 
);
     }
   else if ( tc == ADDR_EXPR )
     {
       o = operand_new_with_dataccess ( 
dataccess_new_address_load_from_tree(TREE_OPERAND(optree,0)) );
     }
   else if ( tc == INDIRECT_REF )
     {
       /* operand 0 is the pointer being dereferenced */
       o = operand_new_with_dataccess ( 
dataccess_new_indirect_ref_from_tree(TREE_OPERAND(optree,0)) );
     }
   else
     {
       fprintf ( stderr, "operand_new_from_tree(): unhandled \'%s\'\n", 
tree_code_name[TREE_CODE(optree)] );
       print_node ( stderr, "", optree, 0 );
       fprintf ( stderr, "\n" );
       gcc_unreachable ( );
       return NULL;
     }

   return o;
}


Regards,
Nicolas.


On 09/26/2010 07:46 AM, Mradul Maheshwari wrote:
>> "Mradul Maheshwari"<mradul@cse.iitb.ac.in>  writes:
>>
>>> I am trying to deduce the original statement from the set of gimple
>>> statements. For eg.
>>> These statement are in gimple
>>>
>>> x.0_1 = x;
>>>
>>> a.1_2 = (int)&a;
>>>
>>> *x.0_1 = a.1_2;
>>>
>>> their equivalent statement in c in *x =&a;
>>>
>>> how can I deduce this statement from these gimple statement?
>>
>> In what sense do you want to deduce the statement?  If you want to
>> deduce precisely "*x =&a", you can't.  GIMPLE does not preserve all the
>> information which is in the source code.  There could have been
>> temporary variables which have been discarded, or this assignment could
>> be part of a conditional or other statement, etc.  But you can see
>> pretty clearly that the effect of the three GIMPLE statements you list
>> above is indeed *x =&a.
>>
>> Ian
>>
> I want to deduce that the effect of these 3 statement is - that in the lhs
> we are having x with 1 DEREFERENCING involved and the rhs is 'a' with
> ADDRESOF involved. I want to store this information in some data structure
> and then print it in a file.
> This information precisely means that
> -----lhs = x, dereferencing = 1, addressof = 0-----
> -----rhs = a, dereferencing = 0, addressof = 1-----
>
> I am trying to use some macros SSA_NAME_DEF_STMT and
> SSA_NAME_IMM_USE_NODE. But still not able to figure out how to use them.
>
> Mradul Maheshwari
>
>



More information about the Gcc-help mailing list