blob: 5370a641ceff357740c31b52a4ac85ce23599c03 [file] [log] [blame]
Paul Sokolovskye7db8172014-02-11 00:44:37 +02001#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
10extern 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__
17typedef machine_uint_t regs_t[6];
18
19void 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__
36typedef machine_uint_t regs_t[4];
37
38void 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
50void gc_collect(void) {
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +020051 //gc_dump_info();
52
Paul Sokolovskye7db8172014-02-11 00:44:37 +020053 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 Georgece1162a2014-02-26 22:55:59 +000057 gc_collect_root((void**)&__bss_start, ((machine_uint_t)&_end - (machine_uint_t)&__bss_start) / sizeof(machine_uint_t));
Paul Sokolovskye7db8172014-02-11 00:44:37 +020058 regs_t regs;
59 gc_helper_get_regs(regs);
60 // GC stack (and regs because we captured them)
Damien Georgece1162a2014-02-26 22:55:59 +000061 gc_collect_root((void**)&regs, ((machine_uint_t)stack_top - (machine_uint_t)&regs) / sizeof(machine_uint_t));
Paul Sokolovskye7db8172014-02-11 00:44:37 +020062 gc_collect_end();
63
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +020064 //printf("-----\n");
65 //gc_dump_info();
Paul Sokolovskye7db8172014-02-11 00:44:37 +020066}
67
68#endif //MICROPY_ENABLE_GC