test-using-global.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. extern int imported_global;
  9. #ifdef __cplusplus
  10. }
  11. #endif
  12. void
  13. create_code (gcc_jit_context *ctxt, void *user_data)
  14. {
  15. /* Let's try to inject the equivalent of:
  16. int exported_global;
  17. extern int imported_global;
  18. static int internal_global;
  19. int
  20. test_using_global (void)
  21. {
  22. exported_global += 1;
  23. imported_global += 1;
  24. internal_global += 1;
  25. return internal_global;
  26. }
  27. */
  28. gcc_jit_type *int_type =
  29. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  30. gcc_jit_lvalue *exported_global =
  31. gcc_jit_context_new_global (ctxt,
  32. NULL,
  33. GCC_JIT_GLOBAL_EXPORTED,
  34. int_type,
  35. "exported_global");
  36. gcc_jit_lvalue *imported_global =
  37. gcc_jit_context_new_global (ctxt,
  38. NULL,
  39. GCC_JIT_GLOBAL_IMPORTED,
  40. int_type,
  41. "imported_global");
  42. gcc_jit_lvalue *internal_global =
  43. gcc_jit_context_new_global (ctxt,
  44. NULL,
  45. GCC_JIT_GLOBAL_INTERNAL,
  46. int_type,
  47. "internal_global");
  48. /* Build the test_fn. */
  49. gcc_jit_function *test_fn =
  50. gcc_jit_context_new_function (ctxt, NULL,
  51. GCC_JIT_FUNCTION_EXPORTED,
  52. int_type,
  53. "test_using_global",
  54. 0, NULL,
  55. 0);
  56. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  57. gcc_jit_block_add_assignment_op (
  58. block, NULL,
  59. exported_global,
  60. GCC_JIT_BINARY_OP_PLUS,
  61. gcc_jit_context_one (ctxt, int_type));
  62. gcc_jit_block_add_assignment_op (
  63. block, NULL,
  64. imported_global,
  65. GCC_JIT_BINARY_OP_PLUS,
  66. gcc_jit_context_one (ctxt, int_type));
  67. gcc_jit_block_add_assignment_op (
  68. block, NULL,
  69. internal_global,
  70. GCC_JIT_BINARY_OP_PLUS,
  71. gcc_jit_context_one (ctxt, int_type));
  72. gcc_jit_block_end_with_return (block,
  73. NULL,
  74. gcc_jit_lvalue_as_rvalue (internal_global));
  75. }
  76. int imported_global;
  77. void
  78. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  79. {
  80. typedef int (*fn_type) (void);
  81. CHECK_NON_NULL (result);
  82. fn_type test_using_global =
  83. (fn_type)gcc_jit_result_get_code (result, "test_using_global");
  84. CHECK_NON_NULL (test_using_global);
  85. /* The exported global should be visible. */
  86. int *exported_global = (int *)gcc_jit_result_get_global (result, "exported_global");
  87. CHECK_NON_NULL (exported_global);
  88. /* ...and should be zero-initialized. */
  89. CHECK_VALUE (*exported_global, 0);
  90. /* Set some nonzero values. */
  91. *exported_global = 11;
  92. imported_global = 42;
  93. /* The internal global shouldn't be visible. */
  94. int *internal_global = (int *)gcc_jit_result_get_global (result, "internal_global");
  95. CHECK_VALUE (internal_global, NULL);
  96. /* Call the JIT-generated function. */
  97. int call_count = test_using_global ();
  98. /* Verify that it correctly modified imported_global and exported_global. */
  99. CHECK_VALUE (*exported_global, 12);
  100. CHECK_VALUE (imported_global, 43);
  101. CHECK_VALUE (call_count, 1);
  102. /* Try calling it again. */
  103. call_count = test_using_global ();
  104. /* Verify the new values. */
  105. CHECK_VALUE (*exported_global, 13);
  106. CHECK_VALUE (imported_global, 44);
  107. CHECK_VALUE (call_count, 2);
  108. }