On Wed, Feb 17, 2016 at 5:27 PM, Cristina Georgiana Opriceana
<cristina.oprice...@gmail.com> wrote:
> Hello,
>
> I inserted a new local var decl in gimple, a pointer which is
> malloc'ed and now I am trying to read/write in that memory.
>
> int *mumu;
> mumu = malloc ( 40 * sizeof (int));
> mumu[1] = 10;
>
> The following statement: mumu[1] = 10; which should look like this
> MEM[(int *)mumu_10 + 4B] = 10;
>
> for me, looks like:
> MEM[(int * *)mumu_10 + 4B] = 10;
>
> This is the variable insertion:
> tree vardecl = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
>                                 VAR_DECL, get_identifier ("mumu"),
> integer_ptr_type_node);
> TREE_ADDRESSABLE (vardecl) = 1;
> DECL_CONTEXT (vardecl) = current_function_decl;
> TREE_USED (vardecl) = 1;
> add_local_decl (cfun, vardecl);
>
> Then I insert the malloc call:
>
> tree fn_ptr = make_ssa_name (vardecl);
> fn = builtin_decl_explicit (BUILT_IN_MALLOC);
> size = build_int_cst (integer_type_node, 40 * sizeof(int)); // testing purpose
> gimple *malloc_stmt = gimple_build_call (fn, 1, size);
> gimple_call_set_lhs (malloc_stmt, fn_ptr);
> gsi_insert_before (&gsi, malloc_stmt, GSI_SAME_STMT);
>
> And then I try to build a mem_ref to the pointer ssa:
>
> tree mem_ref = fold_build2 (MEM_REF, TREE_TYPE (fn_ptr), fn_ptr,
>                    build_int_cst (build_pointer_type (TREE_TYPE (fn_ptr)), 
> 4));

as you want to dereference the pointer you want TREE_TYPE (TREE_TYPE
(fn_ptr)) here (int).
And for the last operand do not indirect fn_ptr again.

> gimple *stmt = gimple_build_assign (mem_ref, build_int_cst
>                       (integer_type_node,10));
> gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
>
>
> If i use build2() instead of fold_build2() compilation fails with
> error: non-trivial conversion at assignment
> void *
> int
> # .MEM_12 = VDEF <.MEM_2>
> MEM[(int * *)mumu_10 + 4B] = 10;

As expected.

> I am a newbie to gcc development, any info is appreciated.
> Thanks,
> Cristina

Reply via email to