test-reading-struct.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. struct bar
  6. {
  7. int x;
  8. int y;
  9. };
  10. void
  11. create_code (gcc_jit_context *ctxt, void *user_data)
  12. {
  13. /* Let's try to inject the equivalent of:
  14. int
  15. test_reading (const struct bar *f)
  16. {
  17. return f->x * f->y;
  18. }
  19. int
  20. test_writing ()
  21. {
  22. struct bar tmp;
  23. tmp.x = 5;
  24. tmp.y = 7;
  25. return test_reading (&tmp);
  26. }
  27. */
  28. gcc_jit_type *int_type =
  29. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  30. gcc_jit_field *x =
  31. gcc_jit_context_new_field (ctxt,
  32. NULL,
  33. int_type,
  34. "x");
  35. gcc_jit_field *y =
  36. gcc_jit_context_new_field (ctxt,
  37. NULL,
  38. int_type,
  39. "y");
  40. gcc_jit_field *fields[] = {x, y};
  41. gcc_jit_type *struct_type =
  42. gcc_jit_struct_as_type (
  43. gcc_jit_context_new_struct_type (ctxt, NULL, "bar", 2, fields));
  44. gcc_jit_type *const_struct_type = gcc_jit_type_get_const (struct_type);
  45. gcc_jit_type *ptr_type = gcc_jit_type_get_pointer (const_struct_type);
  46. /* Build "test_reading". */
  47. gcc_jit_param *param_f =
  48. gcc_jit_context_new_param (ctxt, NULL, ptr_type, "f");
  49. gcc_jit_function *fn_test_reading =
  50. gcc_jit_context_new_function (ctxt, NULL,
  51. GCC_JIT_FUNCTION_EXPORTED,
  52. int_type,
  53. "test_reading",
  54. 1, &param_f,
  55. 0);
  56. /* return f->x * f->y; */
  57. gcc_jit_block *reading_block = gcc_jit_function_new_block (fn_test_reading, NULL);
  58. gcc_jit_block_end_with_return (
  59. reading_block,
  60. NULL,
  61. gcc_jit_context_new_binary_op (
  62. ctxt, NULL,
  63. GCC_JIT_BINARY_OP_MULT,
  64. int_type,
  65. gcc_jit_lvalue_as_rvalue (
  66. gcc_jit_rvalue_dereference_field (
  67. gcc_jit_param_as_rvalue (param_f),
  68. NULL,
  69. x)),
  70. gcc_jit_lvalue_as_rvalue (
  71. gcc_jit_rvalue_dereference_field (
  72. gcc_jit_param_as_rvalue (param_f),
  73. NULL,
  74. y))));
  75. /* Build "test_writing". */
  76. gcc_jit_function *fn_test_writing =
  77. gcc_jit_context_new_function (ctxt, NULL,
  78. GCC_JIT_FUNCTION_EXPORTED,
  79. int_type,
  80. "test_writing",
  81. 0, NULL,
  82. 0);
  83. /* struct bar tmp; */
  84. gcc_jit_lvalue *local_tmp =
  85. gcc_jit_function_new_local (fn_test_writing, NULL,
  86. struct_type,
  87. "tmp");
  88. /* tmp.x = 5; */
  89. gcc_jit_block *writing_block = gcc_jit_function_new_block (fn_test_writing, NULL);
  90. gcc_jit_block_add_assignment (
  91. writing_block, NULL,
  92. gcc_jit_lvalue_access_field (local_tmp, NULL, x),
  93. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 5));
  94. /* tmp.y = 7; */
  95. gcc_jit_block_add_assignment (
  96. writing_block, NULL,
  97. gcc_jit_lvalue_access_field (local_tmp, NULL, y),
  98. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 7));
  99. /* return test_reading (&tmp); */
  100. gcc_jit_rvalue *arg = gcc_jit_lvalue_get_address (local_tmp, NULL);
  101. gcc_jit_block_end_with_return (
  102. writing_block,
  103. NULL,
  104. gcc_jit_context_new_call (
  105. ctxt, NULL,
  106. fn_test_reading,
  107. 1, &arg));
  108. }
  109. void
  110. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  111. {
  112. typedef int (*fn_type) (void);
  113. CHECK_NON_NULL (result);
  114. fn_type test_writing =
  115. (fn_type)gcc_jit_result_get_code (result, "test_writing");
  116. CHECK_NON_NULL (test_writing);
  117. /* Verify that the code correctly returns the product of the fields. */
  118. CHECK_VALUE (test_writing (), 35);
  119. }