Bug 68370 - Pointer arithmetic in libgccjit seems to require an extra cast
Summary: Pointer arithmetic in libgccjit seems to require an extra cast
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: jit (show other bugs)
Version: 5.2.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-16 06:10 UTC by Roger Ferrer Ibanez
Modified: 2015-11-16 18:29 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Small reproducer (564 bytes, text/x-csrc)
2015-11-16 18:29 UTC, Roger Ferrer Ibanez
Details

Note You need to log in before you can comment on or make changes to this bug.
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