https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68370

            Bug ID: 68370
           Summary: Pointer arithmetic in libgccjit seems to require an
                    extra cast
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: jit
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: roger.ferrer at bsc dot es
  Target Milestone: ---

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,

Reply via email to