public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC Plugin: Initialize an Array Global Variable
@ 2014-08-01 19:14 Stephan Friedl
  2014-08-01 23:14 ` Stephan Friedl
  0 siblings, 1 reply; 2+ messages in thread
From: Stephan Friedl @ 2014-08-01 19:14 UTC (permalink / raw)
  To: gcc-help

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

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

* Re: GCC Plugin: Initialize an Array Global Variable
  2014-08-01 19:14 GCC Plugin: Initialize an Array Global Variable Stephan Friedl
@ 2014-08-01 23:14 ` Stephan Friedl
  0 siblings, 0 replies; 2+ messages in thread
From: Stephan Friedl @ 2014-08-01 23:14 UTC (permalink / raw)
  To: gcc-help

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@yahoo.com> 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

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

end of thread, other threads:[~2014-08-01 23:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-01 19:14 GCC Plugin: Initialize an Array Global Variable Stephan Friedl
2014-08-01 23:14 ` Stephan Friedl

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 read-only IMAP folder(s) and NNTP newsgroup(s).