]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.hpp
vm: bottom_frame method for contexts
[factor.git] / vm / contexts.hpp
1 namespace factor
2 {
3
4 // Context object count and identifiers must be kept in sync with:
5 //   core/kernel/kernel.factor
6
7 static const cell context_object_count = 10;
8
9 enum context_object {
10         OBJ_NAMESTACK,
11         OBJ_CATCHSTACK,
12         OBJ_CONTEXT,
13         OBJ_IN_CALLBACK_P,
14 };
15
16 static const cell stack_reserved = 1024;
17
18 struct context {
19
20         // First 4 fields accessed directly by compiler. See basis/vm/vm.factor
21
22         /* Factor callstack pointers */
23         stack_frame *callstack_top;
24         stack_frame *callstack_bottom;
25
26         /* current datastack top pointer */
27         cell datastack;
28
29         /* current retain stack top pointer */
30         cell retainstack;
31
32         /* C callstack pointer */
33         cell callstack_save;
34
35         segment *datastack_seg;
36         segment *retainstack_seg;
37         segment *callstack_seg;
38
39         /* context-specific special objects, accessed by context-object and
40         set-context-object primitives */
41         cell context_objects[context_object_count];
42
43         context(cell datastack_size, cell retainstack_size, cell callstack_size);
44         ~context();
45
46         void reset_datastack();
47         void reset_retainstack();
48         void reset_callstack();
49         void reset_context_objects();
50         void reset();
51         void fix_stacks();
52         void scrub_stacks(gc_info *info, cell index);
53
54         cell peek()
55         {
56                 return *(cell *)datastack;
57         }
58
59         void replace(cell tagged)
60         {
61                 *(cell *)datastack = tagged;
62         }
63
64         cell pop()
65         {
66                 cell value = peek();
67                 datastack -= sizeof(cell);
68                 return value;
69         }
70
71         void push(cell tagged)
72         {
73                 datastack += sizeof(cell);
74                 replace(tagged);
75         }
76
77         stack_frame *bottom_frame()
78         {
79                 return callstack_bottom - 1;
80         }
81 };
82
83 VM_C_API context *new_context(factor_vm *parent);
84 VM_C_API void delete_context(factor_vm *parent, context *old_context);
85 VM_C_API void reset_context(factor_vm *parent, context *ctx);
86 VM_C_API cell begin_callback(factor_vm *parent, cell quot);
87 VM_C_API void end_callback(factor_vm *parent);
88
89 }