testapp.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (C) 2024 Midokura Japan KK. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. uint32_t
  8. host_consume_stack_and_call_indirect(int (*)(int), uint32_t, uint32_t);
  9. uint32_t host_consume_stack(uint32_t);
  10. int
  11. cb(int x)
  12. {
  13. return x * x;
  14. }
  15. int
  16. consume_stack_cb(int x)
  17. {
  18. /*
  19. * intentions:
  20. *
  21. * - consume native stack by making recursive calls
  22. *
  23. * - avoid tail-call optimization (either by the C compiler or
  24. * aot-compiler)
  25. */
  26. if (x == 0) {
  27. return 0;
  28. }
  29. return consume_stack_cb(x - 1) + 1;
  30. }
  31. int
  32. host_consume_stack_cb(int x)
  33. {
  34. return host_consume_stack(x);
  35. }
  36. __attribute__((export_name("test"))) uint32_t
  37. test(uint32_t native_stack, uint32_t recurse_count)
  38. {
  39. uint32_t ret;
  40. ret = host_consume_stack_and_call_indirect(cb, 321, native_stack);
  41. ret = host_consume_stack_and_call_indirect(consume_stack_cb, recurse_count,
  42. native_stack);
  43. #if 0 /* notyet */
  44. ret = host_consume_stack_and_call_indirect(host_consume_stack_cb, 1000000,
  45. native_stack);
  46. #endif
  47. return 42;
  48. }