This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Where to find global var declaration
- From: David Malcolm <dmalcolm at redhat dot com>
- To: Cristina Georgiana Opriceana <cristina dot opriceana at gmail dot com>, gcc at gcc dot gnu dot org
- Date: Wed, 27 Apr 2016 09:54:51 -0400
- Subject: Re: Where to find global var declaration
- Authentication-results: sourceware.org; auth=none
- References: <CAFA9rWM1AvJ5VA7JpUdfURxZswL1aX57K_E2M5um9q=QDdycMg at mail dot gmail dot com>
On Wed, 2016-04-27 at 12:34 +0300, Cristina Georgiana Opriceana wrote:
> Hello,
>
> I tried to add a new global declaration of a pointer and I expected
> to
> see it in varpool nodes, but it does not appear there.
>
> ustackptr = build_decl (UNKNOWN_LOCATION,
> VAR_DECL, get_identifier ("ustackptr"),
> build_pointer_type(void_type_node));
> TREE_ADDRESSABLE (ustackptr) = 1;
> TREE_USED (ustackptr) = 1;
> rest_of_decl_compilation (ustackptr, 1, 0);
>
> and
>
> struct varpool_node *node;
> FOR_EACH_VARIABLE (node) {
> fprintf(stdout, "%s\n", get_name(node->decl));
> }
FWIW, in the the jit "frontend", I wasn't aware of
rest_of_decl_compilation. Instead I have the following code for
creating a global variable, which calls varpool_node::get_create and
varpool_node::finalize_decl directly on the VAR_DECL instance.
That said, maybe rest_of_decl_compilation is the best approach, but I'm
not sure why it isn't working for you. (I'm not an expert at this, I
copied from the C frontend and hacked it up till it worked).
This is from gcc/jit/jit-playback.c (which has a family of wrapper classes around "tree", but hopefully the idea is clear):
/* Construct a playback::lvalue instance (wrapping a tree). */
playback::lvalue *
playback::context::
new_global (location *loc,
enum gcc_jit_global_kind kind,
type *type,
const char *name)
{
gcc_assert (type);
gcc_assert (name);
tree inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (name),
type->as_tree ());
TREE_PUBLIC (inner) = (kind != GCC_JIT_GLOBAL_INTERNAL);
DECL_COMMON (inner) = 1;
switch (kind)
{
default:
gcc_unreachable ();
case GCC_JIT_GLOBAL_EXPORTED:
TREE_STATIC (inner) = 1;
break;
case GCC_JIT_GLOBAL_INTERNAL:
TREE_STATIC (inner) = 1;
break;
case GCC_JIT_GLOBAL_IMPORTED:
DECL_EXTERNAL (inner) = 1;
break;
}
if (loc)
set_tree_location (inner, loc);
varpool_node::get_create (inner);
varpool_node::finalize_decl (inner);
m_globals.safe_push (inner);
return new lvalue (this, inner);
}
Hope this is helpful
Dave