]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.hpp
vm: minor fixes.
[factor.git] / vm / contexts.hpp
1 namespace factor {
2
3 // Context object count and identifiers must be kept in sync with:
4 //   core/kernel/kernel.factor
5
6 static const cell context_object_count = 10;
7
8 enum context_object {
9   OBJ_NAMESTACK,
10   OBJ_CATCHSTACK,
11   OBJ_CONTEXT,
12   OBJ_IN_CALLBACK_P,
13 };
14
15 /* When the callstack fills up (e.g by to deep recursion), a callstack
16    overflow error is triggered. So before continuing executing on it
17    in general_error(), we chop off this many bytes to have some space
18    to work with. */
19 static const cell stack_reserved = 4096;
20
21 struct context {
22
23   // First 4 fields accessed directly by compiler. See basis/vm/vm.factor
24
25   /* Factor callstack pointers */
26   cell callstack_top;
27   cell callstack_bottom;
28
29   /* current datastack top pointer */
30   cell datastack;
31
32   /* current retain stack top pointer */
33   cell retainstack;
34
35   /* C callstack pointer */
36   cell callstack_save;
37
38   segment* datastack_seg;
39   segment* retainstack_seg;
40   segment* callstack_seg;
41
42   /* context-specific special objects, accessed by context-object and
43      set-context-object primitives */
44   cell context_objects[context_object_count];
45
46   context(cell datastack_size, cell retainstack_size, cell callstack_size);
47   ~context();
48
49   void reset_datastack();
50   void reset_retainstack();
51   void reset_callstack();
52   void reset_context_objects();
53   void reset();
54   void fix_stacks();
55   void fill_stack_seg(cell top_ptr, segment* seg, cell pattern);
56   vm_error_type address_to_error(cell addr);
57
58   cell peek() { return *(cell*)datastack; }
59
60   void replace(cell tagged) { *(cell*)datastack = tagged; }
61
62   cell pop() {
63     cell value = peek();
64     datastack -= sizeof(cell);
65     return value;
66   }
67
68   void push(cell tagged) {
69     datastack += sizeof(cell);
70     replace(tagged);
71   }
72 };
73
74 VM_C_API context* new_context(factor_vm* parent);
75 VM_C_API void delete_context(factor_vm* parent);
76 VM_C_API void reset_context(factor_vm* parent);
77 VM_C_API cell begin_callback(factor_vm* parent, cell quot);
78 VM_C_API void end_callback(factor_vm* parent);
79
80 }