public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Stephan Friedl <stephanf01@yahoo.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: GCC Plugin: Initialize an Array Global Variable
Date: Fri, 01 Aug 2014 23:14:00 -0000	[thread overview]
Message-ID: <1406934860.32794.YahooMailNeo@web125801.mail.ne1.yahoo.com> (raw)
In-Reply-To: <1406920452.70499.YahooMailNeo@web125804.mail.ne1.yahoo.com>

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

      reply	other threads:[~2014-08-01 23:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-01 19:14 Stephan Friedl
2014-08-01 23:14 ` Stephan Friedl [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1406934860.32794.YahooMailNeo@web125801.mail.ne1.yahoo.com \
    --to=stephanf01@yahoo.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).