]> gitweb.factorcode.org Git - factor.git/blob - vm/callstack.hpp
vm: remove iterate_callstack_object
[factor.git] / vm / callstack.hpp
1 namespace factor
2 {
3
4 inline static cell callstack_object_size(cell size)
5 {
6         return sizeof(callstack) + size;
7 }
8
9 /* This is a little tricky. The iterator may allocate memory, so we
10 keep the callstack in a GC root and use relative offsets */
11 template<typename Iterator, typename Fixup>
12 inline void factor_vm::iterate_callstack_object_reversed(callstack *stack_,
13         Iterator &iterator, Fixup &fixup)
14 {
15         data_root<callstack> stack(stack_,this);
16         fixnum frame_length = factor::untag_fixnum(stack->length);
17         fixnum frame_offset = 0;
18
19         while(frame_offset < frame_length)
20         {
21                 void *frame_top = stack->frame_top_at(frame_offset);
22                 void *addr = frame_return_address(frame_top);
23
24                 void *fixed_addr = Fixup::translated_code_block_map
25                         ? (void*)fixup.translate_code((code_block*)addr)
26                         : addr;
27                 code_block *owner = code->code_block_for_address((cell)fixed_addr);
28                 cell frame_size = owner->stack_frame_size_for_address((cell)fixed_addr);
29
30 #ifdef FACTOR_DEBUG
31                 // check our derived owner and frame size against the ones stored in the frame
32                 // by the function prolog
33                 stack_frame *frame = (stack_frame*)((char*)frame_top + frame_size) - 1;
34                 void *fixed_entry_point =
35                         (void*)fixup.translate_code((code_block*)frame->entry_point);
36                 FACTOR_ASSERT(owner->entry_point() == fixed_entry_point);
37                 FACTOR_ASSERT(frame_size == frame->size);
38 #endif
39
40                 iterator(frame_top, frame_size, owner, fixed_addr);
41                 frame_offset += frame_size;
42         }
43 }
44
45 template<typename Iterator>
46 inline void factor_vm::iterate_callstack_object_reversed(callstack *stack_, Iterator &iterator)
47 {
48         no_fixup none;
49         iterate_callstack_object_reversed(stack_, iterator, none);
50 }
51
52 template<typename Iterator, typename Fixup>
53 inline void factor_vm::iterate_callstack_reversed(context *ctx, Iterator &iterator, Fixup &fixup)
54 {
55         if (ctx->callstack_top == ctx->callstack_bottom)
56                 return;
57
58         char *frame_top = (char*)ctx->callstack_top;
59
60         while (frame_top < (char*)ctx->callstack_bottom)
61         {
62                 void *addr = frame_return_address((void*)frame_top);
63                 FACTOR_ASSERT(addr != 0);
64                 void *fixed_addr = Fixup::translated_code_block_map
65                         ? (void*)fixup.translate_code((code_block*)addr)
66                         : addr;
67
68                 code_block *owner = code->code_block_for_address((cell)fixed_addr);
69                 code_block *fixed_owner = Fixup::translated_code_block_map
70                         ? owner
71                         : fixup.translate_code(owner);
72
73                 cell frame_size = fixed_owner->stack_frame_size_for_address((cell)fixed_addr);
74
75 #ifdef FACTOR_DEBUG
76                 // check our derived owner and frame size against the ones stored in the frame
77                 // by the function prolog
78                 stack_frame *frame = (stack_frame*)(frame_top + frame_size) - 1;
79                 void *fixed_entry_point = Fixup::translated_code_block_map
80                         ? (void*)fixup.translate_code((code_block*)frame->entry_point)
81                         : frame->entry_point;
82                 FACTOR_ASSERT(owner->entry_point() == fixed_entry_point);
83                 FACTOR_ASSERT(frame_size == frame->size);
84 #endif
85                 void *fixed_addr_for_iter = Fixup::translated_code_block_map
86                         ? fixed_addr
87                         : addr;
88
89                 iterator(frame_top, frame_size, owner, fixed_addr_for_iter);
90                 frame_top += frame_size;
91         }
92 }
93
94 template<typename Iterator>
95 inline void factor_vm::iterate_callstack_reversed(context *ctx, Iterator &iterator)
96 {
97         no_fixup none;
98         iterate_callstack_reversed(ctx, iterator, none);
99 }
100
101
102 }