]> gitweb.factorcode.org Git - factor.git/blob - vm/callstack.hpp
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / vm / callstack.hpp
1 namespace factor
2 {
3
4 inline static cell callstack_size(cell size)
5 {
6         return sizeof(callstack) + size;
7 }
8
9 stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom);
10 stack_frame *frame_successor(stack_frame *frame);
11 code_block *frame_code(stack_frame *frame);
12 cell frame_executing(stack_frame *frame);
13 cell frame_scan(stack_frame *frame);
14 cell frame_type(stack_frame *frame);
15
16 PRIMITIVE(callstack);
17 PRIMITIVE(set_callstack);
18 PRIMITIVE(callstack_to_array);
19 PRIMITIVE(innermost_stack_frame_executing);
20 PRIMITIVE(innermost_stack_frame_scan);
21 PRIMITIVE(set_innermost_stack_frame_quot);
22
23 VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom);
24
25 template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator)
26 {
27         stack_frame *frame = (stack_frame *)bottom - 1;
28
29         while((cell)frame >= top)
30         {
31                 iterator(frame);
32                 frame = frame_successor(frame);
33         }
34 }
35
36 /* This is a little tricky. The iterator may allocate memory, so we
37 keep the callstack in a GC root and use relative offsets */
38 template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator)
39 {
40         gc_root<callstack> stack(stack_);
41         fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame);
42
43         while(frame_offset >= 0)
44         {
45                 stack_frame *frame = stack->frame_at(frame_offset);
46                 frame_offset -= frame->size;
47                 iterator(frame);
48         }
49 }
50
51 }