|
| 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) { |
| 51 | + gc_collect_start(); |
| 52 | + // this traces .data and .bss sections |
| 53 | + extern char __bss_start, _end; |
| 54 | + //printf(".bss: %p-%p\n", &__bss_start, &_end); |
| 55 | + gc_collect_root((void**)&__bss_start, ((uint32_t)&_end - (uint32_t)&__bss_start) / sizeof(uint32_t)); |
| 56 | + regs_t regs; |
| 57 | + gc_helper_get_regs(regs); |
| 58 | + // GC stack (and regs because we captured them) |
| 59 | + gc_collect_root((void**)®s, ((uint32_t)stack_top - (uint32_t)®s) / sizeof(uint32_t)); |
| 60 | + gc_collect_end(); |
| 61 | + |
| 62 | + if (0) { |
| 63 | + // print GC info |
| 64 | + gc_info_t info; |
| 65 | + gc_info(&info); |
| 66 | + printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free); |
| 67 | + printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", |
| 68 | + info.num_1block, info.num_2block, info.max_block); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#endif //MICROPY_ENABLE_GC |
0 commit comments