test-volatile.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. volatile int the_volatile;
  6. void
  7. create_code (gcc_jit_context *ctxt, void *user_data)
  8. {
  9. /* Let's try to inject the equivalent of:
  10. extern volatile int the_volatile;
  11. int
  12. test_using_volatile (void)
  13. {
  14. return the_volatile;
  15. }
  16. */
  17. gcc_jit_type *int_type =
  18. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  19. gcc_jit_type *volatile_int_type =
  20. gcc_jit_type_get_volatile (int_type);
  21. /* Build the test_fn. */
  22. gcc_jit_function *test_fn =
  23. gcc_jit_context_new_function (ctxt, NULL,
  24. GCC_JIT_FUNCTION_EXPORTED,
  25. int_type,
  26. "test_using_volatile",
  27. 0, NULL,
  28. 0);
  29. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  30. gcc_jit_lvalue *read_of_the_volatile =
  31. gcc_jit_rvalue_dereference (
  32. gcc_jit_context_new_rvalue_from_ptr (
  33. ctxt,
  34. gcc_jit_type_get_pointer (volatile_int_type),
  35. (void *)&the_volatile),
  36. NULL);
  37. gcc_jit_block_end_with_return (
  38. block, NULL,
  39. gcc_jit_lvalue_as_rvalue (read_of_the_volatile));
  40. }
  41. void
  42. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  43. {
  44. typedef int (*fn_type) (void);
  45. CHECK_NON_NULL (result);
  46. fn_type test_using_volatile =
  47. (fn_type)gcc_jit_result_get_code (result, "test_using_volatile");
  48. CHECK_NON_NULL (test_using_volatile);
  49. the_volatile = 42;
  50. /* Call the JIT-generated function. */
  51. test_using_volatile ();
  52. CHECK_VALUE (test_using_volatile (), 42);
  53. }