(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-types.c
       1  #include <stdint.h>
       2  #include <stdlib.h>
       3  #include <stdio.h>
       4  #include <stddef.h>
       5  #include <stdbool.h>
       6  
       7  #include "libgccjit.h"
       8  
       9  #include "harness.h"
      10  
      11  struct zoo
      12  {
      13    void *m_void_ptr;
      14  
      15    bool m_bool;
      16  
      17    char m_char;
      18    signed char m_signed_char;
      19    unsigned char m_unsigned_char;
      20  
      21    short m_short;
      22    unsigned short m_unsigned_short;
      23  
      24    int m_int;
      25    unsigned int m_unsigned_int;
      26  
      27    long m_long;
      28    unsigned long m_unsigned_long;
      29  
      30    long long m_long_long;
      31    unsigned long long m_unsigned_long_long;
      32  
      33    uint8_t m_u8;
      34    uint16_t m_u16;
      35    uint32_t m_u32;
      36    uint64_t m_u64;
      37  
      38    int8_t m_i8;
      39    int16_t m_i16;
      40    int32_t m_i32;
      41    int64_t m_i64;
      42  
      43  #ifdef __SIZEOF_INT128__
      44    __uint128_t m_u128;
      45    __int128_t m_i128;
      46  #endif
      47  
      48    int m_sized_int_type;
      49  
      50    float m_float;
      51    double m_double;
      52    long double m_long_double;
      53  
      54    const char *m_const_char_ptr;
      55  
      56    size_t m_size_t;
      57  
      58    FILE *m_FILE_ptr;
      59  };
      60  
      61  int test_int = 42;
      62  int *test_ptr = &test_int;
      63  
      64  const char *test_string = "test_string";
      65  
      66  void
      67  create_code (gcc_jit_context *ctxt, void *user_data)
      68  {
      69    /* Let's try to inject the equivalent of:
      70  
      71       void
      72       test_caller (struct zoo *z)
      73       {
      74  	for each fields "m_field":
      75  	  z->m_field = ...some data;
      76       }
      77    */
      78    gcc_jit_type *void_type =
      79      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
      80  
      81  #define CREATE_FIELD(TYPE, NAME) \
      82    gcc_jit_context_new_field ( \
      83  	ctxt, NULL, \
      84  	gcc_jit_context_get_type (ctxt, TYPE), \
      85  	NAME)
      86  
      87    gcc_jit_field *field_m_void_ptr =
      88      CREATE_FIELD (GCC_JIT_TYPE_VOID_PTR, "m_void_ptr");
      89  
      90    gcc_jit_field *field_m_bool =
      91      CREATE_FIELD (GCC_JIT_TYPE_BOOL, "m_bool");
      92  
      93    gcc_jit_field *field_m_char =
      94      CREATE_FIELD (GCC_JIT_TYPE_CHAR, "m_char");
      95    gcc_jit_field *field_m_signed_char =
      96      CREATE_FIELD (GCC_JIT_TYPE_SIGNED_CHAR, "m_signed_char");
      97    gcc_jit_field *field_m_unsigned_char =
      98      CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_CHAR, "m_unsigned_char");
      99  
     100    gcc_jit_field *field_m_short =
     101      CREATE_FIELD (GCC_JIT_TYPE_SHORT, "m_short");
     102    gcc_jit_field *field_m_unsigned_short =
     103      CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_SHORT, "m_unsigned_short");
     104  
     105    gcc_jit_field *field_m_int =
     106      CREATE_FIELD (GCC_JIT_TYPE_INT, "m_int");
     107    gcc_jit_field *field_m_unsigned_int =
     108      CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_INT, "m_unsigned_int");
     109  
     110    gcc_jit_field *field_m_long =
     111      CREATE_FIELD (GCC_JIT_TYPE_LONG, "m_long");
     112    gcc_jit_field *field_m_unsigned_long =
     113      CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG, "m_unsigned_long");
     114  
     115    gcc_jit_field *field_m_long_long =
     116      CREATE_FIELD (GCC_JIT_TYPE_LONG_LONG, "m_long_long");
     117    gcc_jit_field *field_m_unsigned_long_long =
     118      CREATE_FIELD (GCC_JIT_TYPE_UNSIGNED_LONG_LONG, "m_unsigned_long_long");
     119  
     120    gcc_jit_field *field_m_u8 =
     121      CREATE_FIELD (GCC_JIT_TYPE_UINT8_T, "m_u8");
     122    gcc_jit_field *field_m_u16 =
     123      CREATE_FIELD (GCC_JIT_TYPE_UINT16_T, "m_u16");
     124    gcc_jit_field *field_m_u32 =
     125      CREATE_FIELD (GCC_JIT_TYPE_UINT32_T, "m_u32");
     126    gcc_jit_field *field_m_u64 =
     127      CREATE_FIELD (GCC_JIT_TYPE_UINT64_T, "m_u64");
     128  
     129    gcc_jit_field *field_m_i8 =
     130      CREATE_FIELD (GCC_JIT_TYPE_INT8_T, "m_i8");
     131    gcc_jit_field *field_m_i16 =
     132      CREATE_FIELD (GCC_JIT_TYPE_INT16_T, "m_i16");
     133    gcc_jit_field *field_m_i32 =
     134      CREATE_FIELD (GCC_JIT_TYPE_INT32_T, "m_i32");
     135    gcc_jit_field *field_m_i64 =
     136      CREATE_FIELD (GCC_JIT_TYPE_INT64_T, "m_i64");
     137  
     138  #ifdef __SIZEOF_INT128__
     139    gcc_jit_field *field_m_u128 =
     140      CREATE_FIELD (GCC_JIT_TYPE_UINT128_T, "m_u128");
     141    gcc_jit_field *field_m_i128 =
     142      CREATE_FIELD (GCC_JIT_TYPE_INT128_T, "m_i128");
     143  #endif
     144  
     145    /* Signed int type with sizeof (int): */
     146    gcc_jit_type *sized_int_type =
     147      gcc_jit_context_get_int_type (ctxt, sizeof (int), 1);
     148    gcc_jit_field *field_m_sized_int_type =
     149      gcc_jit_context_new_field (
     150        ctxt, NULL, sized_int_type, "m_sized_int_type");
     151  
     152    gcc_jit_field *field_m_float =
     153      CREATE_FIELD (GCC_JIT_TYPE_FLOAT, "m_float");
     154    gcc_jit_field *field_m_double =
     155      CREATE_FIELD (GCC_JIT_TYPE_DOUBLE, "m_double");
     156    gcc_jit_field *field_m_long_double =
     157      CREATE_FIELD (GCC_JIT_TYPE_LONG_DOUBLE, "m_long_double");
     158  
     159    gcc_jit_field *field_m_const_char_ptr =
     160      CREATE_FIELD (GCC_JIT_TYPE_CONST_CHAR_PTR, "m_const_char_ptr");
     161  
     162    gcc_jit_field *field_m_size_t =
     163      CREATE_FIELD (GCC_JIT_TYPE_SIZE_T, "m_size_t");
     164  
     165    gcc_jit_field *field_m_FILE_ptr =
     166      CREATE_FIELD (GCC_JIT_TYPE_FILE_PTR, "m_FILE_ptr");
     167  
     168  #undef CREATE_FIELD
     169  
     170    gcc_jit_field *zoo_fields[] = {
     171      field_m_void_ptr,
     172  
     173      field_m_bool,
     174  
     175      field_m_char,
     176      field_m_signed_char,
     177      field_m_unsigned_char,
     178  
     179      field_m_short,
     180      field_m_unsigned_short,
     181  
     182      field_m_int,
     183      field_m_unsigned_int,
     184  
     185      field_m_long,
     186      field_m_unsigned_long,
     187  
     188      field_m_long_long,
     189      field_m_unsigned_long_long,
     190  
     191      field_m_u8,
     192      field_m_u16,
     193      field_m_u32,
     194      field_m_u64,
     195  
     196      field_m_i8,
     197      field_m_i16,
     198      field_m_i32,
     199      field_m_i64,
     200  
     201  #ifdef __SIZEOF_INT128__
     202      field_m_u128,
     203      field_m_i128,
     204  #endif
     205  
     206      field_m_sized_int_type,
     207  
     208      field_m_float,
     209      field_m_double,
     210      field_m_long_double,
     211  
     212      field_m_const_char_ptr,
     213  
     214      field_m_size_t,
     215  
     216      field_m_FILE_ptr
     217    };
     218  
     219    gcc_jit_type *zoo_type =
     220      gcc_jit_struct_as_type (
     221        gcc_jit_context_new_struct_type (
     222          ctxt,
     223  	NULL,
     224  	"zoo",
     225  	sizeof (zoo_fields) / sizeof (zoo_fields[0]),
     226  	zoo_fields));
     227  
     228    gcc_jit_type *zoo_ptr_type =
     229      gcc_jit_type_get_pointer (zoo_type);
     230  
     231    /* Build the test_fn.	 */
     232    gcc_jit_param *param_z =
     233      gcc_jit_context_new_param (ctxt, NULL, zoo_ptr_type, "z");
     234    gcc_jit_function *test_fn =
     235      gcc_jit_context_new_function (ctxt, NULL,
     236  				  GCC_JIT_FUNCTION_EXPORTED,
     237  				  void_type,
     238  				  "test_types",
     239  				  1, &param_z,
     240  				  0);
     241    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
     242  
     243    /* Write to the various fields of param "z".	*/
     244  #define ASSIGN(FIELD, EXPR) \
     245    gcc_jit_block_add_assignment (		\
     246      block, NULL,				\
     247      gcc_jit_rvalue_dereference_field (		\
     248        gcc_jit_param_as_rvalue (param_z),	\
     249        NULL,					\
     250        (FIELD)),				\
     251      (EXPR));
     252  
     253    ASSIGN(
     254      field_m_void_ptr,
     255      gcc_jit_context_new_rvalue_from_ptr (
     256        ctxt,
     257        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR),
     258        test_ptr))
     259  
     260    ASSIGN(field_m_bool,
     261      gcc_jit_context_new_rvalue_from_int (
     262        ctxt,
     263        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL), 1))
     264  
     265    ASSIGN(field_m_char,
     266      gcc_jit_context_new_rvalue_from_int (
     267        ctxt,
     268        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR),
     269        'V'))
     270    ASSIGN(field_m_signed_char,
     271      gcc_jit_context_new_rvalue_from_int (
     272        ctxt,
     273        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR),
     274        -37))
     275    ASSIGN(field_m_unsigned_char,
     276      gcc_jit_context_new_rvalue_from_int (
     277        ctxt,
     278        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR),
     279        200))
     280  
     281    ASSIGN(field_m_short,
     282      gcc_jit_context_new_rvalue_from_int (
     283        ctxt,
     284        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SHORT),
     285        -900))
     286    ASSIGN(field_m_unsigned_short,
     287      gcc_jit_context_new_rvalue_from_int (
     288        ctxt,
     289        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_SHORT),
     290        0x3000))
     291  
     292    ASSIGN(field_m_int,
     293      gcc_jit_context_new_rvalue_from_int (
     294        ctxt,
     295        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT),
     296        -0x2000))
     297    ASSIGN(field_m_unsigned_int,
     298      gcc_jit_context_new_rvalue_from_int (
     299        ctxt,
     300        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT),
     301        1234567))
     302  
     303    ASSIGN(field_m_long,
     304      gcc_jit_context_new_rvalue_from_int (
     305        ctxt,
     306        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG),
     307        -5))
     308    ASSIGN(field_m_unsigned_long,
     309      gcc_jit_context_new_rvalue_from_int (
     310        ctxt,
     311        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG),
     312        12345678))
     313  
     314    ASSIGN(field_m_long_long,
     315      gcc_jit_context_new_rvalue_from_int (
     316        ctxt,
     317        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG),
     318        -42))
     319    ASSIGN(field_m_unsigned_long_long,
     320      gcc_jit_context_new_rvalue_from_int (
     321        ctxt,
     322        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_LONG_LONG),
     323        123456789))
     324  
     325    ASSIGN(field_m_u8,
     326      gcc_jit_context_new_rvalue_from_int (
     327        ctxt,
     328        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT8_T),
     329        123))
     330    ASSIGN(field_m_u16,
     331      gcc_jit_context_new_rvalue_from_int (
     332        ctxt,
     333        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT16_T),
     334        12345))
     335    ASSIGN(field_m_u32,
     336      gcc_jit_context_new_rvalue_from_int (
     337        ctxt,
     338        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT32_T),
     339        123456789))
     340    ASSIGN(field_m_u64,
     341      gcc_jit_context_new_rvalue_from_int (
     342        ctxt,
     343        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT64_T),
     344        123456789))
     345  
     346    ASSIGN(field_m_i8,
     347      gcc_jit_context_new_rvalue_from_int (
     348        ctxt,
     349        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT8_T),
     350        -1))
     351    ASSIGN(field_m_i16,
     352      gcc_jit_context_new_rvalue_from_int (
     353        ctxt,
     354        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT16_T),
     355        -2))
     356    ASSIGN(field_m_i32,
     357      gcc_jit_context_new_rvalue_from_int (
     358        ctxt,
     359        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT32_T),
     360        -3))
     361    ASSIGN(field_m_i64,
     362      gcc_jit_context_new_rvalue_from_int (
     363        ctxt,
     364        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT64_T),
     365        -4))
     366  
     367  #ifdef __SIZEOF_INT128__
     368    ASSIGN(field_m_u128,
     369      gcc_jit_context_new_rvalue_from_int (
     370        ctxt,
     371        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT128_T),
     372        123456789))
     373    ASSIGN(field_m_i128,
     374      gcc_jit_context_new_rvalue_from_int (
     375        ctxt,
     376        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT128_T),
     377        -5))
     378  #endif
     379  
     380    ASSIGN(field_m_sized_int_type,
     381      gcc_jit_context_new_rvalue_from_int (
     382        ctxt,
     383        sized_int_type, 500))
     384  
     385    ASSIGN(field_m_float,
     386      gcc_jit_context_new_rvalue_from_double (
     387        ctxt,
     388        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT),
     389        3.141))
     390    ASSIGN(field_m_double,
     391      gcc_jit_context_new_rvalue_from_double (
     392        ctxt,
     393        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE),
     394        3.141))
     395    ASSIGN(field_m_long_double,
     396      gcc_jit_context_new_rvalue_from_double (
     397        ctxt,
     398        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_DOUBLE),
     399        3.141))
     400  
     401    ASSIGN(field_m_const_char_ptr,
     402      gcc_jit_context_new_rvalue_from_ptr (
     403        ctxt,
     404        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR),
     405        (char *)test_string))
     406  
     407    ASSIGN(field_m_size_t,
     408      gcc_jit_context_new_rvalue_from_int (
     409        ctxt,
     410        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T),
     411        sizeof (struct zoo)))
     412  
     413    ASSIGN(field_m_FILE_ptr,
     414      gcc_jit_context_new_rvalue_from_ptr (
     415        ctxt,
     416        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FILE_PTR),
     417        stderr))
     418  
     419  #undef ASSIGN
     420  
     421    gcc_jit_block_end_with_void_return (block, NULL);
     422  }
     423  
     424  void
     425  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
     426  {
     427    typedef void (*fn_type) (struct zoo *);
     428    CHECK_NON_NULL (result);
     429  
     430    fn_type test_types =
     431      (fn_type)gcc_jit_result_get_code (result, "test_types");
     432    CHECK_NON_NULL (test_types);
     433  
     434    struct zoo z;
     435    memset (&z, 0xf0, sizeof (z));
     436  
     437    /* Call the JIT-generated function.  */
     438    test_types (&z);
     439  
     440    /* Verify that it correctly wrote to the various fields.  */
     441    CHECK_VALUE (z.m_void_ptr, test_ptr);
     442  
     443    CHECK_VALUE (z.m_bool, true);
     444  
     445    CHECK_VALUE (z.m_char, 'V');
     446    CHECK_VALUE (z.m_signed_char, -37);
     447    CHECK_VALUE (z.m_unsigned_char, 200);
     448  
     449    CHECK_VALUE (z.m_short, -900);
     450    CHECK_VALUE (z.m_unsigned_short, 0x3000);
     451  
     452    CHECK_VALUE (z.m_int, -0x2000);
     453    CHECK_VALUE (z.m_unsigned_int, 1234567);
     454  
     455    CHECK_VALUE (z.m_long, -5);
     456    CHECK_VALUE (z.m_unsigned_long, 12345678);
     457  
     458    CHECK_VALUE (z.m_long_long, -42);
     459    CHECK_VALUE (z.m_unsigned_long_long, 123456789);
     460  
     461    CHECK_VALUE (z.m_u8, 123);
     462    CHECK_VALUE (z.m_u16, 12345);
     463    CHECK_VALUE (z.m_u32, 123456789);
     464    CHECK_VALUE (z.m_u64, 123456789);
     465  
     466    CHECK_VALUE (z.m_i8, -1);
     467    CHECK_VALUE (z.m_i16, -2);
     468    CHECK_VALUE (z.m_i32, -3);
     469    CHECK_VALUE (z.m_i64, -4);
     470  
     471  #ifdef __SIZEOF_INT128__
     472    CHECK_VALUE (z.m_u128, 123456789);
     473    CHECK_VALUE (z.m_i128, -5);
     474  #endif
     475  
     476    CHECK_VALUE (z.m_sized_int_type, 500);
     477  
     478    CHECK_VALUE (z.m_float, 3.141f);
     479    CHECK_VALUE (z.m_double, 3.141);
     480    CHECK_VALUE (z.m_long_double, 3.141);
     481  
     482    CHECK_VALUE (z.m_const_char_ptr, test_string);
     483  
     484    CHECK_VALUE (z.m_size_t, sizeof (struct zoo));
     485  
     486    CHECK_VALUE (z.m_FILE_ptr, stderr);
     487  
     488    if (sizeof(long) == 8)
     489      CHECK (gcc_jit_compatible_types (
     490        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG),
     491        gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT64_T)));
     492  
     493    CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT)), sizeof (float));
     494    CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE)), sizeof (double));
     495  }