2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-02-10 22:44:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-01 23:30:53 +00:00
|
|
|
#include "py/mpstate.h"
|
2015-01-01 20:40:19 +00:00
|
|
|
#include "py/gc.h"
|
2014-02-10 22:44:37 +00:00
|
|
|
|
|
|
|
#if MICROPY_ENABLE_GC
|
|
|
|
|
2015-11-07 20:04:42 +00:00
|
|
|
// Even if we have specific support for an architecture, it is
|
|
|
|
// possible to force use of setjmp-based implementation.
|
|
|
|
#if !MICROPY_GCREGS_SETJMP
|
2014-06-05 02:32:17 +01:00
|
|
|
|
2014-02-10 22:44:37 +00:00
|
|
|
// We capture here callee-save registers, i.e. ones which may contain
|
|
|
|
// interesting values held there by our callers. It doesn't make sense
|
|
|
|
// to capture caller-saved registers, because they, well, put on the
|
|
|
|
// stack already by the caller.
|
2015-11-07 20:04:42 +00:00
|
|
|
#if defined(__x86_64__)
|
2014-07-03 13:25:24 +01:00
|
|
|
typedef mp_uint_t regs_t[6];
|
2014-02-10 22:44:37 +00:00
|
|
|
|
2015-01-12 22:30:49 +00:00
|
|
|
STATIC void gc_helper_get_regs(regs_t arr) {
|
2014-02-10 22:44:37 +00:00
|
|
|
register long rbx asm ("rbx");
|
|
|
|
register long rbp asm ("rbp");
|
|
|
|
register long r12 asm ("r12");
|
|
|
|
register long r13 asm ("r13");
|
|
|
|
register long r14 asm ("r14");
|
|
|
|
register long r15 asm ("r15");
|
2014-05-28 13:37:22 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
// TODO:
|
|
|
|
// This is dirty workaround for Clang. It tries to get around
|
|
|
|
// uncompliant (wrt to GCC) behavior of handling register variables.
|
|
|
|
// Application of this patch here is random, and done only to unbreak
|
|
|
|
// MacOS build. Better, cross-arch ways to deal with Clang issues should
|
|
|
|
// be found.
|
|
|
|
asm("" : "=r"(rbx));
|
|
|
|
asm("" : "=r"(rbp));
|
|
|
|
asm("" : "=r"(r12));
|
|
|
|
asm("" : "=r"(r13));
|
|
|
|
asm("" : "=r"(r14));
|
2014-05-11 23:24:41 +01:00
|
|
|
asm("" : "=r"(r15));
|
2014-05-28 13:37:22 +01:00
|
|
|
#endif
|
2014-02-10 22:44:37 +00:00
|
|
|
arr[0] = rbx;
|
|
|
|
arr[1] = rbp;
|
|
|
|
arr[2] = r12;
|
|
|
|
arr[3] = r13;
|
|
|
|
arr[4] = r14;
|
|
|
|
arr[5] = r15;
|
|
|
|
}
|
|
|
|
|
2015-11-07 20:04:42 +00:00
|
|
|
#elif defined(__i386__)
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
typedef mp_uint_t regs_t[4];
|
2014-02-10 22:44:37 +00:00
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC void gc_helper_get_regs(regs_t arr) {
|
2014-02-10 22:44:37 +00:00
|
|
|
register long ebx asm ("ebx");
|
|
|
|
register long esi asm ("esi");
|
|
|
|
register long edi asm ("edi");
|
|
|
|
register long ebp asm ("ebp");
|
2016-06-02 18:10:39 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
// TODO:
|
|
|
|
// This is dirty workaround for Clang. It tries to get around
|
|
|
|
// uncompliant (wrt to GCC) behavior of handling register variables.
|
|
|
|
// Application of this patch here is random, and done only to unbreak
|
|
|
|
// MacOS build. Better, cross-arch ways to deal with Clang issues should
|
|
|
|
// be found.
|
|
|
|
asm("" : "=r"(ebx));
|
|
|
|
asm("" : "=r"(esi));
|
|
|
|
asm("" : "=r"(edi));
|
|
|
|
asm("" : "=r"(ebp));
|
|
|
|
#endif
|
2014-02-10 22:44:37 +00:00
|
|
|
arr[0] = ebx;
|
|
|
|
arr[1] = esi;
|
|
|
|
arr[2] = edi;
|
|
|
|
arr[3] = ebp;
|
|
|
|
}
|
|
|
|
|
2015-11-07 20:04:42 +00:00
|
|
|
#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
typedef mp_uint_t regs_t[10];
|
2014-03-01 10:22:55 +00:00
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC void gc_helper_get_regs(regs_t arr) {
|
2014-03-01 10:22:55 +00:00
|
|
|
register long r4 asm ("r4");
|
|
|
|
register long r5 asm ("r5");
|
|
|
|
register long r6 asm ("r6");
|
|
|
|
register long r7 asm ("r7");
|
|
|
|
register long r8 asm ("r8");
|
|
|
|
register long r9 asm ("r9");
|
|
|
|
register long r10 asm ("r10");
|
|
|
|
register long r11 asm ("r11");
|
|
|
|
register long r12 asm ("r12");
|
|
|
|
register long r13 asm ("r13");
|
|
|
|
arr[0] = r4;
|
|
|
|
arr[1] = r5;
|
|
|
|
arr[2] = r6;
|
|
|
|
arr[3] = r7;
|
|
|
|
arr[4] = r8;
|
|
|
|
arr[5] = r9;
|
|
|
|
arr[6] = r10;
|
|
|
|
arr[7] = r11;
|
|
|
|
arr[8] = r12;
|
|
|
|
arr[9] = r13;
|
|
|
|
}
|
2015-11-07 20:04:42 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// If we don't have architecture-specific optimized support,
|
|
|
|
// just fall back to setjmp-based implementation.
|
|
|
|
#undef MICROPY_GCREGS_SETJMP
|
|
|
|
#define MICROPY_GCREGS_SETJMP (1)
|
|
|
|
|
|
|
|
#endif // Arch-specific selection
|
2014-06-05 02:32:17 +01:00
|
|
|
#endif // !MICROPY_GCREGS_SETJMP
|
2014-03-01 10:22:55 +00:00
|
|
|
|
2015-11-07 20:04:42 +00:00
|
|
|
// If MICROPY_GCREGS_SETJMP was requested explicitly, or if
|
|
|
|
// we enabled it as a fallback above.
|
|
|
|
#if MICROPY_GCREGS_SETJMP
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
typedef jmp_buf regs_t;
|
|
|
|
|
|
|
|
STATIC void gc_helper_get_regs(regs_t arr) {
|
|
|
|
setjmp(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_GCREGS_SETJMP
|
|
|
|
|
2016-05-05 12:00:19 +01:00
|
|
|
// this function is used by mpthreadport.c
|
|
|
|
void gc_collect_regs_and_stack(void);
|
|
|
|
|
2016-05-04 11:23:21 +01:00
|
|
|
void gc_collect_regs_and_stack(void) {
|
2014-02-10 22:44:37 +00:00
|
|
|
regs_t regs;
|
|
|
|
gc_helper_get_regs(regs);
|
|
|
|
// GC stack (and regs because we captured them)
|
2014-06-19 12:53:15 +01:00
|
|
|
void **regs_ptr = (void**)(void*)®s;
|
2016-04-22 23:44:56 +01:00
|
|
|
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)®s) / sizeof(uintptr_t));
|
2016-05-04 11:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void gc_collect(void) {
|
|
|
|
//gc_dump_info();
|
|
|
|
|
|
|
|
gc_collect_start();
|
|
|
|
gc_collect_regs_and_stack();
|
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
mp_thread_gc_others();
|
|
|
|
#endif
|
2015-01-11 17:28:27 +00:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
2015-01-14 00:11:09 +00:00
|
|
|
mp_unix_mark_exec();
|
2015-01-14 11:43:51 +00:00
|
|
|
#endif
|
2014-02-10 22:44:37 +00:00
|
|
|
gc_collect_end();
|
|
|
|
|
2014-02-11 16:01:38 +00:00
|
|
|
//printf("-----\n");
|
|
|
|
//gc_dump_info();
|
2014-02-10 22:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //MICROPY_ENABLE_GC
|