Re: GCC Plugin: Initialize an Array Global Variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think I figured out the right way to do this...

For initializing an array, it appears as if creating a constructor that takes the initial values as a vector of constructor_elt elements of { index, value } pairs does the trick.  For example:


const tree	globalType = build_array_type( integer_type_node, build_index_type( build_int_cst( NULL_TREE, 12 )) );

tree globalDeclaration = build_decl( UNKNOWN_LOCATION, VAR_DECL, "test_array", globalType ); 
TREE_STATIC( globalDeclaration ) = 1; 

TREE_PUBLIC( globalDeclaration ) = 1; 
TREE_ADDRESSABLE( globalDeclaration ) = 1;
DECL_EXTERNAL( globalDeclaration ) = 0; 

vec<constructor_elt, va_gc> *v; 
vec_alloc ( v, 12 ); 
for( int i = 0; i < 12; ++i ) 
{ 
    constructor_elt elt = { build_int_cst( integer_type_node, i ), build_int_cst( integer_type_node, 100 + i ) }; 
    v->quick_push (elt); 
} 
DECL_INITIAL( globalDeclaration ) = build_constructor( globalType, v ); 

vec_free( v ); 


I had to modify the above code so it makes sense out of the context of my method so it may not be spot-on, but the above should create an integer array of 12 elements and populate that array with the values 100 -> 111 inclusive.

If I am wrong in how I am approaching this, I'd appreciate any correction.

Thanks,

Stephan






On Friday, August 1, 2014 1:14 PM, Stephan Friedl <stephanf01@xxxxxxxxx> wrote:
I am implementing a GCC Plugin to perform some simple operations, one of which is to create and initialize global variables.  I have this working for fundamental values and for creating class instances and then invoking their constructor with appropriate arguments during static initialization.

What currently has me stalled is initializing global array variables.  I can create the variables but have not yet uncovered how to initialize them.  I have tried calling DECL_INITIAL with references to array locations but that does not appear to work.

Can anybody give me some insight into how to initialize a global array and/or point me to some example code?

Thanks,

Stephan






[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux