test-compound-assignment.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. struct assignable_struct
  6. {
  7. int a;
  8. char b;
  9. float c;
  10. };
  11. union assignable_union
  12. {
  13. int a;
  14. char b;
  15. float c;
  16. };
  17. /* Verify that compound assignment works; let's try to inject the
  18. equivalent of:
  19. struct assignable_struct
  20. test_struct_assignment (struct assignable_struct x)
  21. {
  22. struct assignable_struct y, z;
  23. y = x;
  24. z = y;
  25. return z;
  26. }
  27. and the same, for "union assignable_union". */
  28. /* Make the type "struct assignable_struct" or "union assignable_union". */
  29. static gcc_jit_type *
  30. make_type (gcc_jit_context *ctxt, int make_union)
  31. {
  32. gcc_jit_type *t_int =
  33. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  34. gcc_jit_type *t_char =
  35. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
  36. gcc_jit_type *t_float =
  37. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
  38. gcc_jit_field *a =
  39. gcc_jit_context_new_field (ctxt,
  40. NULL,
  41. t_int,
  42. "a");
  43. gcc_jit_field *b =
  44. gcc_jit_context_new_field (ctxt,
  45. NULL,
  46. t_char,
  47. "b");
  48. gcc_jit_field *c =
  49. gcc_jit_context_new_field (ctxt,
  50. NULL,
  51. t_float,
  52. "c");
  53. gcc_jit_field *fields[] = {a, b, c};
  54. if (make_union)
  55. return gcc_jit_context_new_union_type (ctxt, NULL,
  56. "assignable_union",
  57. 3, fields);
  58. else
  59. return gcc_jit_struct_as_type (
  60. gcc_jit_context_new_struct_type (ctxt, NULL,
  61. "assignable_struct",
  62. 3, fields));
  63. }
  64. static void
  65. make_function (gcc_jit_context *ctxt, int make_union, const char *funcname)
  66. {
  67. gcc_jit_type *test_type = make_type (ctxt, make_union);
  68. gcc_jit_param *x =
  69. gcc_jit_context_new_param (ctxt, NULL,
  70. test_type, "x");
  71. gcc_jit_function *fn =
  72. gcc_jit_context_new_function (ctxt, NULL,
  73. GCC_JIT_FUNCTION_EXPORTED,
  74. test_type,
  75. funcname,
  76. 1, &x,
  77. 0);
  78. gcc_jit_lvalue *y =
  79. gcc_jit_function_new_local (fn, NULL, test_type, "y");
  80. gcc_jit_lvalue *z =
  81. gcc_jit_function_new_local (fn, NULL, test_type, "z");
  82. gcc_jit_block *block =
  83. gcc_jit_function_new_block (fn, NULL);
  84. gcc_jit_block_add_assignment (block, NULL,
  85. y, gcc_jit_param_as_rvalue (x));
  86. gcc_jit_block_add_assignment (block, NULL,
  87. z, gcc_jit_lvalue_as_rvalue (y));
  88. gcc_jit_block_end_with_return (block, NULL,
  89. gcc_jit_lvalue_as_rvalue (z));
  90. }
  91. void
  92. create_code (gcc_jit_context *ctxt, void *user_data)
  93. {
  94. make_function (ctxt, 0, "test_struct_assignment");
  95. make_function (ctxt, 1, "test_union_assignment");
  96. }
  97. static void
  98. verify_test_struct_assignment (gcc_jit_result *result)
  99. {
  100. typedef struct assignable_struct (*fn_type) (struct assignable_struct);
  101. fn_type test_struct_assignment =
  102. (fn_type)gcc_jit_result_get_code (result, "test_struct_assignment");
  103. CHECK_NON_NULL (test_struct_assignment);
  104. struct assignable_struct s, t;
  105. s.a = 500;
  106. s.b = 'A';
  107. s.c = 1.0;
  108. t = test_struct_assignment (s);
  109. CHECK_VALUE (t.a, 500);
  110. CHECK_VALUE (t.b, 'A');
  111. CHECK_VALUE (t.c, 1.0);
  112. }
  113. static void
  114. verify_test_union_assignment (gcc_jit_result *result)
  115. {
  116. typedef union assignable_union (*fn_type) (union assignable_union);
  117. fn_type test_union_assignment =
  118. (fn_type)gcc_jit_result_get_code (result, "test_union_assignment");
  119. CHECK_NON_NULL (test_union_assignment);
  120. union assignable_union p, q;
  121. p.a = 500;
  122. q = test_union_assignment (p);
  123. CHECK_VALUE (q.a, 500);
  124. p.b = 'A';
  125. q = test_union_assignment (p);
  126. CHECK_VALUE (q.b, 'A');
  127. p.c = 1.0;
  128. q = test_union_assignment (p);
  129. CHECK_VALUE (q.c, 1.0);
  130. }
  131. void
  132. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  133. {
  134. CHECK_NON_NULL (result);
  135. verify_test_struct_assignment (result);
  136. verify_test_union_assignment (result);
  137. }