]> gitweb.factorcode.org Git - factor.git/blob - vm/callstack.hpp
VM: Fixup cast formatting after clang-format
[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 template <typename Iterator, typename Fixup>
10 inline void factor_vm::iterate_callstack_object(callstack* stack_,
11                                                 Iterator& iterator,
12                                                 Fixup& fixup) {
13   data_root<callstack> stack(stack_, this);
14   fixnum frame_length = factor::untag_fixnum(stack->length);
15   fixnum frame_offset = 0;
16
17   while (frame_offset < frame_length) {
18     void* frame_top = stack->frame_top_at(frame_offset);
19     void* addr = frame_return_address(frame_top);
20
21     void* fixed_addr = Fixup::translated_code_block_map
22                            ? (void*)fixup.translate_code((code_block*)addr)
23                            : addr;
24     code_block* owner = code->code_block_for_address((cell)fixed_addr);
25     cell frame_size = owner->stack_frame_size_for_address((cell)fixed_addr);
26
27     iterator(frame_top, frame_size, owner, fixed_addr);
28     frame_offset += frame_size;
29   }
30 }
31
32 template <typename Iterator>
33 inline void factor_vm::iterate_callstack_object(callstack* stack,
34                                                 Iterator& iterator) {
35   no_fixup none;
36   iterate_callstack_object(stack, iterator, none);
37 }
38
39 template <typename Iterator, typename Fixup>
40 inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator,
41                                          Fixup& fixup) {
42   if (ctx->callstack_top == ctx->callstack_bottom)
43     return;
44
45   char* frame_top = (char*)ctx->callstack_top;
46
47   while (frame_top < (char*)ctx->callstack_bottom) {
48     void* addr = frame_return_address((void*)frame_top);
49     FACTOR_ASSERT(addr != 0);
50     void* fixed_addr = Fixup::translated_code_block_map
51                            ? (void*)fixup.translate_code((code_block*)addr)
52                            : addr;
53
54     code_block* owner = code->code_block_for_address((cell)fixed_addr);
55     code_block* fixed_owner =
56         Fixup::translated_code_block_map ? owner : fixup.translate_code(owner);
57
58     cell frame_size =
59         fixed_owner->stack_frame_size_for_address((cell)fixed_addr);
60
61     void* fixed_addr_for_iter =
62         Fixup::translated_code_block_map ? fixed_addr : addr;
63
64     iterator(frame_top, frame_size, owner, fixed_addr_for_iter);
65     frame_top += frame_size;
66   }
67 }
68
69 template <typename Iterator>
70 inline void factor_vm::iterate_callstack(context* ctx, Iterator& iterator) {
71   no_fixup none;
72   iterate_callstack(ctx, iterator, none);
73 }
74
75 }