Paul Sokolovsky | e7db817 | 2014-02-11 00:44:37 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | #include "misc.h" |
| 5 | #include "mpconfig.h" |
| 6 | #include "gc.h" |
| 7 | |
| 8 | #if MICROPY_ENABLE_GC |
| 9 | |
| 10 | extern void *stack_top; |
| 11 | |
| 12 | // We capture here callee-save registers, i.e. ones which may contain |
| 13 | // interesting values held there by our callers. It doesn't make sense |
| 14 | // to capture caller-saved registers, because they, well, put on the |
| 15 | // stack already by the caller. |
| 16 | #ifdef __x86_64__ |
| 17 | typedef machine_uint_t regs_t[6]; |
| 18 | |
| 19 | void gc_helper_get_regs(regs_t arr) { |
| 20 | register long rbx asm ("rbx"); |
| 21 | register long rbp asm ("rbp"); |
| 22 | register long r12 asm ("r12"); |
| 23 | register long r13 asm ("r13"); |
| 24 | register long r14 asm ("r14"); |
| 25 | register long r15 asm ("r15"); |
| 26 | arr[0] = rbx; |
| 27 | arr[1] = rbp; |
| 28 | arr[2] = r12; |
| 29 | arr[3] = r13; |
| 30 | arr[4] = r14; |
| 31 | arr[5] = r15; |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | #ifdef __i386__ |
| 36 | typedef machine_uint_t regs_t[4]; |
| 37 | |
| 38 | void gc_helper_get_regs(regs_t arr) { |
| 39 | register long ebx asm ("ebx"); |
| 40 | register long esi asm ("esi"); |
| 41 | register long edi asm ("edi"); |
| 42 | register long ebp asm ("ebp"); |
| 43 | arr[0] = ebx; |
| 44 | arr[1] = esi; |
| 45 | arr[2] = edi; |
| 46 | arr[3] = ebp; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | void gc_collect(void) { |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 51 | //gc_dump_info(); |
| 52 | |
Paul Sokolovsky | e7db817 | 2014-02-11 00:44:37 +0200 | [diff] [blame] | 53 | gc_collect_start(); |
| 54 | // this traces .data and .bss sections |
| 55 | extern char __bss_start, _end; |
| 56 | //printf(".bss: %p-%p\n", &__bss_start, &_end); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame^] | 57 | gc_collect_root((void**)&__bss_start, ((machine_uint_t)&_end - (machine_uint_t)&__bss_start) / sizeof(machine_uint_t)); |
Paul Sokolovsky | e7db817 | 2014-02-11 00:44:37 +0200 | [diff] [blame] | 58 | regs_t regs; |
| 59 | gc_helper_get_regs(regs); |
| 60 | // GC stack (and regs because we captured them) |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame^] | 61 | gc_collect_root((void**)®s, ((machine_uint_t)stack_top - (machine_uint_t)®s) / sizeof(machine_uint_t)); |
Paul Sokolovsky | e7db817 | 2014-02-11 00:44:37 +0200 | [diff] [blame] | 62 | gc_collect_end(); |
| 63 | |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 64 | //printf("-----\n"); |
| 65 | //gc_dump_info(); |
Paul Sokolovsky | e7db817 | 2014-02-11 00:44:37 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | #endif //MICROPY_ENABLE_GC |