]> gitweb.factorcode.org Git - factor.git/blob - vm/callstack.hpp
VM: change type of callstack_top and callstack_bottom from void* to cell
[factor.git] / vm / callstack.hpp
1 namespace factor {
2
3 inline static cell callstack_object_size(cell size) {
4   return sizeof(callstack) + size;
5 }
6
7 /* This is a little tricky. The iterator may allocate memory, so we
8 keep the callstack in a GC root and use relative offsets */
9 /* Allocates memory */
10 template <typename Iterator, typename Fixup>
11 inline void factor_vm::iterate_callstack_object(callstack* stack_,
12                                                 Iterator& iterator,
13                                                 Fixup& fixup) {
14   data_root<callstack> stack(stack_, this);
15   fixnum frame_length = factor::untag_fixnum(stack->length);
16   fixnum frame_offset = 0;
17
18   while (frame_offset < frame_length) {
19     void* frame_top = stack->frame_top_at(frame_offset);
20     void* addr = *(void**)frame_top;
21
22     void* fixed_addr = Fixup::translated_code_block_map
23                            ? (void*)fixup.translate_code((code_block*)addr)
24                            : addr;
25     code_block* owner = code->code_block_for_address((cell)fixed_addr);
26     cell frame_size = owner->stack_frame_size_for_address((cell)fixed_addr);
27
28     iterator(frame_top, frame_size, owner, fixed_addr);
29     frame_offset += frame_size;
30   }
31 }
32
33 /* Allocates memory */
34 template <typename Iterator>
35 inline void factor_vm::iterate_callstack_object(callstack* stack,
36                                                 Iterator& iterator) {
37   no_fixup none;
38   iterate_callstack_object(stack, iterator, none);
39 }
40
41 /* Allocates memory */
42 template <typename Iterator, typename Fixup>
43 inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator,
44                                          Fixup& fixup) {
45
46   cell frame_top = ctx->callstack_top;
47
48   while (frame_top < ctx->callstack_bottom) {
49     void* addr = *(void**)frame_top;
50     FACTOR_ASSERT(addr != 0);
51     void* fixed_addr = Fixup::translated_code_block_map
52                            ? (void*)fixup.translate_code((code_block*)addr)
53                            : addr;
54
55     code_block* owner = code->code_block_for_address((cell)fixed_addr);
56     code_block* fixed_owner =
57         Fixup::translated_code_block_map ? owner : fixup.translate_code(owner);
58
59     cell frame_size =
60         fixed_owner->stack_frame_size_for_address((cell)fixed_addr);
61
62     void* fixed_addr_for_iter =
63         Fixup::translated_code_block_map ? fixed_addr : addr;
64
65     iterator((void*)frame_top, frame_size, owner, fixed_addr_for_iter);
66     frame_top += frame_size;
67   }
68 }
69
70 /* Allocates memory */
71 template <typename Iterator>
72 inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator) {
73   no_fixup none;
74   iterate_callstack(ctx, iterator, none);
75 }
76
77 }