]> gitweb.factorcode.org Git - factor.git/blob - vm/callstack.hpp
vm: jit::jit is a c++ constructor but it does not allocate objects to the Factor...
[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 = frame_return_address(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   if (ctx->callstack_top == ctx->callstack_bottom)
46     return;
47
48   char* frame_top = (char*)ctx->callstack_top;
49
50   while (frame_top < (char*)ctx->callstack_bottom) {
51     void* addr = frame_return_address((void*)frame_top);
52     FACTOR_ASSERT(addr != 0);
53     void* fixed_addr = Fixup::translated_code_block_map
54                            ? (void*)fixup.translate_code((code_block*)addr)
55                            : addr;
56
57     code_block* owner = code->code_block_for_address((cell)fixed_addr);
58     code_block* fixed_owner =
59         Fixup::translated_code_block_map ? owner : fixup.translate_code(owner);
60
61     cell frame_size =
62         fixed_owner->stack_frame_size_for_address((cell)fixed_addr);
63
64     void* fixed_addr_for_iter =
65         Fixup::translated_code_block_map ? fixed_addr : addr;
66
67     iterator(frame_top, frame_size, owner, fixed_addr_for_iter);
68     frame_top += frame_size;
69   }
70 }
71
72 /* Allocates memory */
73 template <typename Iterator>
74 inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator) {
75   no_fixup none;
76   iterate_callstack(ctx, iterator, none);
77 }
78
79 }