Bug 68370

Summary: Pointer arithmetic in libgccjit seems to require an extra cast
Product: gcc Reporter: Roger Ferrer Ibanez <rofirrim>
Component: jitAssignee: David Malcolm <dmalcolm>
Status: UNCONFIRMED ---    
Severity: normal    
Priority: P3    
Version: 5.2.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: Small reproducer

Description Roger Ferrer Ibanez 2015-11-16 06:10:30 UTC
Hi,

maybe I'm doing something wrong, but libgccjit rejects an assignment of pointer arithmetic where the types (according to the diagnostic) are OK.

This is not a blocker as it can be worked around using an explicit cast.

I am using GCC 5.2.0.

I have a function "int test(const char* text)" with a single block.

  gcc_jit_type *int_type = gcc_jit_context_get_type (ctx, GCC_JIT_TYPE_INT);
  gcc_jit_type *const_char_ptr_type =
    gcc_jit_context_get_type (ctx, GCC_JIT_TYPE_CONST_CHAR_PTR);

  // param: const char* text;
  gcc_jit_param *param_text =
    gcc_jit_context_new_param (ctx, /* loc */ NULL, const_char_ptr_type,
			       "text");
  gcc_jit_rvalue *rval_text = gcc_jit_param_as_rvalue (param_text);

  // int test(const char* text);
  gcc_jit_param *params[] = { param_text };
  gcc_jit_function *test_fun =
    gcc_jit_context_new_function (ctx, /* loc */ NULL,
				  GCC_JIT_FUNCTION_EXPORTED, int_type, "test",
				  1, params, /* is_variadic */ 0);
  gcc_jit_block *block = gcc_jit_function_new_block (test_fun, "test-block");

and then I want to compute

  text++;

According to the documentation this has to be done like this.

  text = &text[1];

But if I write

  // text = &text[1]; // does not work
  gcc_jit_block_add_assignment (block, /* loc */ NULL,
        gcc_jit_param_as_lvalue (param_text),
        gcc_jit_lvalue_get_address(
          gcc_jit_context_new_array_access(ctx, /* loc */ NULL,
              rval_text,
              gcc_jit_context_one (ctx, int_type)),
         /* loc */ NULL));

libgccjit rejects it with 

libgccjit.so: error: gcc_jit_block_add_assignment: mismatching types: assignment to text (type: const char *) from &text[(int)1] (type: const char *)

This can be worked around using a cast:

  gcc_jit_block_add_assignment (block, /* loc */ NULL,
        gcc_jit_param_as_lvalue (param_text),
        gcc_jit_context_new_cast (ctx, /* loc */ NULL,
          gcc_jit_lvalue_get_address(
            gcc_jit_context_new_array_access(ctx, /* loc */ NULL,
                rval_text,
                gcc_jit_context_one (ctx, int_type)),
           /* loc */ NULL),
           const_char_type));

Kind regards,
Comment 1 Andrew Pinski 2015-11-16 06:41:57 UTC
//   text++;

Can't this be computed as:
text = text POINTER_PLUS 1;

?
Comment 2 David Malcolm 2015-11-16 11:50:46 UTC
Thanks for reporting this.

That error message:
 libgccjit.so: error: gcc_jit_block_add_assignment: mismatching types: assignment to text (type: const char *) from &text[(int)1] (type: const char *)

looks wrong.

Please can you attach the full reproducer as a .c file (perhaps using gcc_jit_context_dump_reproducer_to_file; 
see https://gcc.gnu.org/onlinedocs/jit/topics/contexts.html#gcc_jit_context_dump_reproducer_to_file ).
Comment 3 Roger Ferrer Ibanez 2015-11-16 18:29:59 UTC
Created attachment 36729 [details]
Small reproducer