gccollect.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "py/mpstate.h"
  28. #include "py/gc.h"
  29. #if MICROPY_ENABLE_GC
  30. // Even if we have specific support for an architecture, it is
  31. // possible to force use of setjmp-based implementation.
  32. #if !MICROPY_GCREGS_SETJMP
  33. // We capture here callee-save registers, i.e. ones which may contain
  34. // interesting values held there by our callers. It doesn't make sense
  35. // to capture caller-saved registers, because they, well, put on the
  36. // stack already by the caller.
  37. #if defined(__x86_64__)
  38. typedef mp_uint_t regs_t[6];
  39. STATIC void gc_helper_get_regs(regs_t arr) {
  40. register long rbx asm ("rbx");
  41. register long rbp asm ("rbp");
  42. register long r12 asm ("r12");
  43. register long r13 asm ("r13");
  44. register long r14 asm ("r14");
  45. register long r15 asm ("r15");
  46. #ifdef __clang__
  47. // TODO:
  48. // This is dirty workaround for Clang. It tries to get around
  49. // uncompliant (wrt to GCC) behavior of handling register variables.
  50. // Application of this patch here is random, and done only to unbreak
  51. // MacOS build. Better, cross-arch ways to deal with Clang issues should
  52. // be found.
  53. asm("" : "=r"(rbx));
  54. asm("" : "=r"(rbp));
  55. asm("" : "=r"(r12));
  56. asm("" : "=r"(r13));
  57. asm("" : "=r"(r14));
  58. asm("" : "=r"(r15));
  59. #endif
  60. arr[0] = rbx;
  61. arr[1] = rbp;
  62. arr[2] = r12;
  63. arr[3] = r13;
  64. arr[4] = r14;
  65. arr[5] = r15;
  66. }
  67. #elif defined(__i386__)
  68. typedef mp_uint_t regs_t[4];
  69. STATIC void gc_helper_get_regs(regs_t arr) {
  70. register long ebx asm ("ebx");
  71. register long esi asm ("esi");
  72. register long edi asm ("edi");
  73. register long ebp asm ("ebp");
  74. arr[0] = ebx;
  75. arr[1] = esi;
  76. arr[2] = edi;
  77. arr[3] = ebp;
  78. }
  79. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  80. typedef mp_uint_t regs_t[10];
  81. STATIC void gc_helper_get_regs(regs_t arr) {
  82. register long r4 asm ("r4");
  83. register long r5 asm ("r5");
  84. register long r6 asm ("r6");
  85. register long r7 asm ("r7");
  86. register long r8 asm ("r8");
  87. register long r9 asm ("r9");
  88. register long r10 asm ("r10");
  89. register long r11 asm ("r11");
  90. register long r12 asm ("r12");
  91. register long r13 asm ("r13");
  92. arr[0] = r4;
  93. arr[1] = r5;
  94. arr[2] = r6;
  95. arr[3] = r7;
  96. arr[4] = r8;
  97. arr[5] = r9;
  98. arr[6] = r10;
  99. arr[7] = r11;
  100. arr[8] = r12;
  101. arr[9] = r13;
  102. }
  103. #else
  104. // If we don't have architecture-specific optimized support,
  105. // just fall back to setjmp-based implementation.
  106. #undef MICROPY_GCREGS_SETJMP
  107. #define MICROPY_GCREGS_SETJMP (1)
  108. #endif // Arch-specific selection
  109. #endif // !MICROPY_GCREGS_SETJMP
  110. // If MICROPY_GCREGS_SETJMP was requested explicitly, or if
  111. // we enabled it as a fallback above.
  112. #if MICROPY_GCREGS_SETJMP
  113. #include <setjmp.h>
  114. typedef jmp_buf regs_t;
  115. STATIC void gc_helper_get_regs(regs_t arr) {
  116. setjmp(arr);
  117. }
  118. #endif // MICROPY_GCREGS_SETJMP
  119. void gc_collect(void) {
  120. gc_collect_start();
  121. regs_t regs;
  122. gc_helper_get_regs(regs);
  123. // GC stack (and regs because we captured them)
  124. void **regs_ptr = (void**)(void*)&regs;
  125. gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
  126. #if MICROPY_EMIT_NATIVE
  127. mp_unix_mark_exec();
  128. #endif
  129. gc_collect_end();
  130. }
  131. #endif //MICROPY_ENABLE_GC