|
| 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 | + |
| 27 | +#include <stdio.h> |
| 28 | + |
| 29 | +#include "py/mpstate.h" |
| 30 | +#include "py/gc.h" |
| 31 | + |
| 32 | +#if MICROPY_ENABLE_GC |
| 33 | + |
| 34 | +// Even if we have specific support for an architecture, it is |
| 35 | +// possible to force use of setjmp-based implementation. |
| 36 | +#if !MICROPY_GCREGS_SETJMP |
| 37 | + |
| 38 | +// We capture here callee-save registers, i.e. ones which may contain |
| 39 | +// interesting values held there by our callers. It doesn't make sense |
| 40 | +// to capture caller-saved registers, because they, well, put on the |
| 41 | +// stack already by the caller. |
| 42 | +#if defined(__x86_64__) |
| 43 | +typedef mp_uint_t regs_t[6]; |
| 44 | + |
| 45 | +STATIC void gc_helper_get_regs(regs_t arr) { |
| 46 | + register long rbx asm ("rbx"); |
| 47 | + register long rbp asm ("rbp"); |
| 48 | + register long r12 asm ("r12"); |
| 49 | + register long r13 asm ("r13"); |
| 50 | + register long r14 asm ("r14"); |
| 51 | + register long r15 asm ("r15"); |
| 52 | +#ifdef __clang__ |
| 53 | + // TODO: |
| 54 | + // This is dirty workaround for Clang. It tries to get around |
| 55 | + // uncompliant (wrt to GCC) behavior of handling register variables. |
| 56 | + // Application of this patch here is random, and done only to unbreak |
| 57 | + // MacOS build. Better, cross-arch ways to deal with Clang issues should |
| 58 | + // be found. |
| 59 | + asm("" : "=r"(rbx)); |
| 60 | + asm("" : "=r"(rbp)); |
| 61 | + asm("" : "=r"(r12)); |
| 62 | + asm("" : "=r"(r13)); |
| 63 | + asm("" : "=r"(r14)); |
| 64 | + asm("" : "=r"(r15)); |
| 65 | +#endif |
| 66 | + arr[0] = rbx; |
| 67 | + arr[1] = rbp; |
| 68 | + arr[2] = r12; |
| 69 | + arr[3] = r13; |
| 70 | + arr[4] = r14; |
| 71 | + arr[5] = r15; |
| 72 | +} |
| 73 | + |
| 74 | +#elif defined(__i386__) |
| 75 | + |
| 76 | +typedef mp_uint_t regs_t[4]; |
| 77 | + |
| 78 | +STATIC void gc_helper_get_regs(regs_t arr) { |
| 79 | + register long ebx asm ("ebx"); |
| 80 | + register long esi asm ("esi"); |
| 81 | + register long edi asm ("edi"); |
| 82 | + register long ebp asm ("ebp"); |
| 83 | + arr[0] = ebx; |
| 84 | + arr[1] = esi; |
| 85 | + arr[2] = edi; |
| 86 | + arr[3] = ebp; |
| 87 | +} |
| 88 | + |
| 89 | +#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) |
| 90 | + |
| 91 | +typedef mp_uint_t regs_t[10]; |
| 92 | + |
| 93 | +STATIC void gc_helper_get_regs(regs_t arr) { |
| 94 | + register long r4 asm ("r4"); |
| 95 | + register long r5 asm ("r5"); |
| 96 | + register long r6 asm ("r6"); |
| 97 | + register long r7 asm ("r7"); |
| 98 | + register long r8 asm ("r8"); |
| 99 | + register long r9 asm ("r9"); |
| 100 | + register long r10 asm ("r10"); |
| 101 | + register long r11 asm ("r11"); |
| 102 | + register long r12 asm ("r12"); |
| 103 | + register long r13 asm ("r13"); |
| 104 | + arr[0] = r4; |
| 105 | + arr[1] = r5; |
| 106 | + arr[2] = r6; |
| 107 | + arr[3] = r7; |
| 108 | + arr[4] = r8; |
| 109 | + arr[5] = r9; |
| 110 | + arr[6] = r10; |
| 111 | + arr[7] = r11; |
| 112 | + arr[8] = r12; |
| 113 | + arr[9] = r13; |
| 114 | +} |
| 115 | + |
| 116 | +#else |
| 117 | + |
| 118 | +// If we don't have architecture-specific optimized support, |
| 119 | +// just fall back to setjmp-based implementation. |
| 120 | +#undef MICROPY_GCREGS_SETJMP |
| 121 | +#define MICROPY_GCREGS_SETJMP (1) |
| 122 | + |
| 123 | +#endif // Arch-specific selection |
| 124 | +#endif // !MICROPY_GCREGS_SETJMP |
| 125 | + |
| 126 | +// If MICROPY_GCREGS_SETJMP was requested explicitly, or if |
| 127 | +// we enabled it as a fallback above. |
| 128 | +#if MICROPY_GCREGS_SETJMP |
| 129 | +#include <setjmp.h> |
| 130 | + |
| 131 | +typedef jmp_buf regs_t; |
| 132 | + |
| 133 | +STATIC void gc_helper_get_regs(regs_t arr) { |
| 134 | + setjmp(arr); |
| 135 | +} |
| 136 | + |
| 137 | +#endif // MICROPY_GCREGS_SETJMP |
| 138 | + |
| 139 | +void gc_collect(void) { |
| 140 | + gc_collect_start(); |
| 141 | + regs_t regs; |
| 142 | + gc_helper_get_regs(regs); |
| 143 | + // GC stack (and regs because we captured them) |
| 144 | + void **regs_ptr = (void**)(void*)®s; |
| 145 | + gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)®s) / sizeof(mp_uint_t)); |
| 146 | + #if MICROPY_EMIT_NATIVE |
| 147 | + mp_unix_mark_exec(); |
| 148 | + #endif |
| 149 | + gc_collect_end(); |
| 150 | +} |
| 151 | + |
| 152 | +#endif //MICROPY_ENABLE_GC |
0 commit comments