void
create_code (gcc_jit_context *ctxt, void *user_data)
{
    /* Let's try to inject the equivalent of:

         struct foo;

         extern void called_function (struct foo *ptr);

         void
         test_fn ()
         {
           struct foo f;
     called_function (f);
       }

       and verify that we get a type error (foo * vs foo).
    */
    gcc_jit_type *void_type =
        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
    gcc_jit_struct *struct_foo =
        gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 0, NULL);
    gcc_jit_type *foo_ptr =
        gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_foo));
    gcc_jit_param *param =
        gcc_jit_context_new_param (ctxt, NULL, foo_ptr, "ptr");

    gcc_jit_function *called_function =
        gcc_jit_context_new_function (ctxt, NULL,
                                      GCC_JIT_FUNCTION_IMPORTED,
                                      void_type,
                                      "called_function",
                                      1, &param,
                                      0);

    gcc_jit_function *test_fn =
        gcc_jit_context_new_function (ctxt, NULL,
                                      GCC_JIT_FUNCTION_EXPORTED,
                                      void_type,
                                      "test_fn",
                                      0, NULL,
                                      0);
    gcc_jit_lvalue *f =
        gcc_jit_function_new_local (
            test_fn, NULL, gcc_jit_struct_as_type (struct_foo), "f");

    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);

    gcc_jit_rvalue *arg = gcc_jit_lvalue_as_rvalue (f);

    gcc_jit_block_add_eval (
        block, NULL,
        gcc_jit_context_new_call (
            ctxt, NULL,
            called_function,
            1, &arg));
    gcc_jit_block_end_with_void_return (block, NULL);
}
示例#2
0
static void
make_types (struct top_level *top_level)
{
  top_level->numeric_type =
    gcc_jit_context_get_type (top_level->ctxt, GCC_JIT_TYPE_DOUBLE);
  top_level->numeric_type_ptr =
    gcc_jit_type_get_pointer (top_level->numeric_type);
  top_level->zero =
    gcc_jit_context_zero (top_level->ctxt, top_level->numeric_type);

  top_level->int_type =
    gcc_jit_context_get_type (top_level->ctxt, GCC_JIT_TYPE_INT);
  top_level->void_type =
    gcc_jit_context_get_type (top_level->ctxt, GCC_JIT_TYPE_VOID);

  top_level->a =
    gcc_jit_context_new_field (top_level->ctxt,
			       NULL,
			       top_level->numeric_type,
			       "a");
  top_level->b =
    gcc_jit_context_new_field (top_level->ctxt,
			       NULL,
			       top_level->numeric_type,
			       "b");
  top_level->c =
    gcc_jit_context_new_field (top_level->ctxt,
			       NULL,
			       top_level->numeric_type,
			       "c");
  top_level->discriminant =
    gcc_jit_context_new_field (top_level->ctxt,
			       NULL,
			       top_level->numeric_type,
			       "discriminant");
  gcc_jit_field *fields[] = {top_level->a,
			     top_level->b,
			     top_level->c,
			     top_level->discriminant};
  top_level->struct_quadratic =
    gcc_jit_struct_as_type (
      gcc_jit_context_new_struct_type (top_level->ctxt, NULL,
				       "quadratic", 4, fields));
  top_level->quadratic_ptr =
    gcc_jit_type_get_pointer (top_level->struct_quadratic);
}
void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  /* Let's try to inject the equivalent of:
     void
     test_bogus_access (struct foo f)
     {
       f.x = f.p;
     }
     i.e. using the wrong struct for the RHS.
  */
  gcc_jit_type *void_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);

  /* Map "struct foo".  */
  gcc_jit_field *x =
    gcc_jit_context_new_field (ctxt,
			       NULL,
			       int_type,
			       "x");
  gcc_jit_field *y =
    gcc_jit_context_new_field (ctxt,
			       NULL,
			       int_type,
			       "y");
  gcc_jit_field *foo_fields[] = {x, y};
  gcc_jit_struct *struct_foo =
    gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);

  /* Map "struct bar".  */
  gcc_jit_field *p =
    gcc_jit_context_new_field (ctxt,
			       NULL,
			       int_type,
			       "p");
  gcc_jit_field *q =
    gcc_jit_context_new_field (ctxt,
			       NULL,
			       int_type,
			       "q");
  /* We don't actually need a gcc_jit_type for "struct bar" for the test.  */
  gcc_jit_field *bar_fields[] = {p, q};
  (void)gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, bar_fields);

  /* Build the test function.  */
  gcc_jit_param *param_f =
    gcc_jit_context_new_param (ctxt, NULL,
			       gcc_jit_struct_as_type (struct_foo), "f");
  gcc_jit_function *test_fn =
    gcc_jit_context_new_function (ctxt, NULL,
				  GCC_JIT_FUNCTION_EXPORTED,
				  void_type,
				  "test_bogus_access",
				  1, &param_f,
				  0);

  /* OK: f.x = ... */
  gcc_jit_lvalue *lvalue =
    gcc_jit_lvalue_access_field (
      gcc_jit_param_as_lvalue (param_f),
      NULL,
      x);

  /* Erroneous: ... = f.p; */
  gcc_jit_rvalue *rvalue =
      gcc_jit_rvalue_access_field (
	gcc_jit_param_as_rvalue (param_f),
	NULL,
	p);

  gcc_jit_block *block =
    gcc_jit_function_new_block (test_fn, NULL);
  gcc_jit_block_add_assignment (
    block,
    NULL,
    lvalue, rvalue);
  gcc_jit_block_end_with_void_return (block, NULL);
}