Constructing static variables in GIMPLE?
Sean Callanan
spyffe@cs.sunysb.edu
Sat Jun 14 20:53:00 GMT 2008
Dear mailing list:
I solved my own problem. This is how I construct a static variable:
--
static tree build_static(tree type, const char* name)
{
tree ret = NULL;
ret = build_decl(VAR_DECL, get_identifier(name), type);
TREE_STATIC(ret) = 1;
assemble_variable(ret, 0, 0, 0); // tree decl, int top_level, int
at_end, int dont_output_data
add_referenced_var(ret);
return ret;
}
--
By the way, this only works for static variables; for automatic
variables, this is how I do it:
--
static tree build_automatic(tree type, const char* name)
{
tree ret = NULL;
ret = build_decl(VAR_DECL, get_identifier(name), type);
DECL_ARTIFICIAL(ret) = 1; // declared by the compiler
DECL_IGNORED_P(ret) = 1; // no debug info
TREE_READONLY(ret) = 0; // writable
DECL_EXTERNAL(ret) = 0; // defined
TREE_STATIC(ret) = 0; // automatic
TREE_USED(ret) = 1; // used
DECL_CONTEXT(ret) = cfun->decl;
create_var_ann(ret);
add_referenced_var(ret);
HOST_WIDE_INT decl_size;
if(TYPE_SIZE(type) && TREE_CODE(TYPE_SIZE(type)) == INTEGER_CST)
{
decl_size = tree_low_cst(TYPE_SIZE(type), 0);
}
else
{
decl_size = 0;
}
rtx decl_rtl = assign_stack_local(DECL_MODE(ret), decl_size, 0);
SET_DECL_RTL(ret, decl_rtl);
return ret;
}
--
Sincerely,
Sean Callanan
On Jun 13, 2008, at 1:57 AM, Sean Callanan wrote:
> Dear mailing list:
>
> I am writing GCC code that constructs GIMPLE (after
> pass_apply_inline and before pass_all_optimizations) for a function-
> level static variable that gets assigned to and passed to another
> function. My problem is that I am getting undefined symbol errors
> for a renamed version of the symbol.
> I define the variable as below:
>
> --
> static tree build_static(tree type, const char* name)
> {
> tree ret = NULL;
>
> ret = build_decl(VAR_DECL, get_identifier(name), type);
>
> DECL_ARTIFICIAL(ret) = 1; // declared by the compiler
> DECL_IGNORED_P(ret) = 1; // no debug info
> TREE_READONLY(ret) = 0; // writable
> DECL_EXTERNAL(ret) = 0; // defined
> TREE_STATIC(ret) = 1; // static
> TREE_USED(ret) = 1; // used
> DECL_CONTEXT(ret) = cfun->decl;
>
> create_var_ann(ret);
> add_referenced_var(ret);
>
> return ret;
> }
> --
>
> Here's an example where I use this function, and demonstrating how I
> use the variable.
>
> --
> tree last_p = build_static(long_long_unsigned_type_node, "last_p");
> // ...
> tree subtraction = build2(MINUS_EXPR, long_long_unsigned_type_node,
> end_time, start_time);
> build_gimple_modify_stmt(last_p, subtraction);
> // ...
> tree last_p_temp = create_tmp_var(TREE_TYPE(last_p),
> get_name(last_p));
> tree last_p_name = make_ssa_name(last_p_temp, NULL);
> tree last_p_assignment = build_gimple_modify_stmt(last_p_name,
> last_p);
> SSA_NAME_DEF_STMT(last_p_name) = last_p_assignment;
> --
>
> As seen above, I (try to) respect SSA when reading from this
> variable, and when assigning to it (I create an SSA_NAME when using
> the value, and assign directly to the VAR_DECL). I'm passing an
> SSA_NAME to the function. However, when I compile, I'm getting
> linker errors of the form:
>
> --
> [...]/test.c:4: undefined reference to `last_p.2610'
> --
>
> Oddly, I don't get undefined references for `last_p'; I find it very
> odd that it's being renamed. Does anyone know what's going on
> here? As always, I welcome corrections to my (admittedly novice-
> level) GIMPLE- and SSA-fu.
> Thanks for looking at this.
>
> Sincerely,
> Sean Callanan
More information about the Gcc
mailing list