swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 1 | #include "gc.h" |
| 2 | #include "lisp.h" |
| 3 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 4 | value_t *gc_base; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 5 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 6 | void __attribute__((noinline)) gc_set_base_here() |
| 7 | { |
| 8 | // Move the current stack top address to gc_base. This works nicely because |
| 9 | // it means that when another (presumably lisp) function is called right |
| 10 | // after this, the stack top for it will be the same as for this function. |
| 11 | asm("movl %%esp, %0" : "=g"(gc_base)); |
| 12 | } |
| 13 | |
| 14 | void _mark(value_t value) |
| 15 | { |
| 16 | if (heapp(value)) |
| 17 | { |
| 18 | |
| 19 | } |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void _sweep() |
| 23 | { |
| 24 | |
| 25 | } |
| 26 | |
| 27 | void _do_gc(unsigned int esp, unsigned int ebp) |
| 28 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 29 | value_t *esp_p = (value_t *)esp, |
| 30 | *ebp_p = (value_t *)ebp; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 31 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 32 | int num_marked = 0; |
| 33 | |
| 34 | // For every stack frame until the base of the stack |
| 35 | while (esp_p < gc_base) |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 36 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 37 | // Walk up the stack until we reach either the frame pointer or the base |
| 38 | // of the stack. Basically walk to the top of this function's stack |
| 39 | // frame. |
| 40 | for (; esp_p < ebp_p && esp_p < gc_base; esp_p++) |
| 41 | { |
| 42 | _mark(*esp_p); |
| 43 | num_marked++; |
| 44 | } |
| 45 | |
| 46 | // Set the frame pointer to the frame pointer on the stack |
| 47 | ebp_p = (value_t *)*esp_p; |
| 48 | |
| 49 | // Step up two stack slots, one for the frame pointer and one for the |
| 50 | // return address. |
| 51 | esp_p += 2; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 52 | } |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 53 | |
| 54 | fprintf(stderr, "Marked %d\n", num_marked); |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 55 | } |