aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/jit.dg/test-global-set-initializer.c
blob: d38aba7d73f5bafa378cd836fc75c0527b7f2dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "libgccjit.h"

#include "harness.h"

#define BIG_BLOB_SIZE (1 << 12) /* 4KB.  */

static signed char test_blob1[] = { 0xc, 0xa, 0xf, 0xf, 0xe };
static unsigned test_blob2[] = { 0x3, 0x2, 0x1, 0x0, 0x1, 0x2, 0x3 };
static unsigned char test_blob3[BIG_BLOB_SIZE];

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

     signed char bin_blob1[] = { 0xc, 0xa, 0xf, 0xf, 0xe };
     unsigned bin_blob2[] = { 0x3, 0x2, 0x1, 0x0, 0x1, 0x2, 0x3 };
     unsigned char bin_blob3[4096]...
  */
  gcc_jit_type *unsigned_char_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR);
  gcc_jit_type *signed_char_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR);
  gcc_jit_type *unsigned_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT);

  gcc_jit_lvalue *glob =
    gcc_jit_context_new_global (
      ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
      gcc_jit_context_new_array_type (ctxt, NULL, signed_char_type,
				      sizeof (test_blob1)),
      "bin_blob1");
  gcc_jit_global_set_initializer (glob, test_blob1, sizeof (test_blob1));

  glob =
    gcc_jit_context_new_global (
      ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
      gcc_jit_context_new_array_type (
	ctxt, NULL, unsigned_type,
	sizeof (test_blob2) / sizeof (*test_blob2)),
      "bin_blob2");
  gcc_jit_global_set_initializer (glob, test_blob2,
				  sizeof (test_blob2));

  for (size_t i = 0; i < BIG_BLOB_SIZE; i++)
    test_blob3[i] = i * i + i;
  glob =
    gcc_jit_context_new_global (
      ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
      gcc_jit_context_new_array_type (ctxt, NULL, unsigned_char_type,
				      sizeof (test_blob3)),
      "bin_blob3");
  gcc_jit_global_set_initializer (glob, test_blob3, sizeof (test_blob3));
}

void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
  CHECK_NON_NULL (result);
  void *glob = gcc_jit_result_get_global (result, "bin_blob1");
  CHECK_NON_NULL (glob);
  CHECK_VALUE (memcmp (test_blob1, glob, sizeof (test_blob1)), 0);

  glob = gcc_jit_result_get_global (result, "bin_blob2");
  CHECK_NON_NULL (glob);
  CHECK_VALUE (memcmp (test_blob2, glob,
		       sizeof (test_blob2)), 0);

  glob = gcc_jit_result_get_global (result, "bin_blob3");
  CHECK_NON_NULL (glob);
  CHECK_VALUE (memcmp (test_blob3, glob, sizeof (test_blob3)), 0);

}