]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.hpp
VM: always clear the data and retainstack in general_error because they might contain...
[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 static const cell stack_reserved = 1024;
16
17 struct context {
18
19   // First 4 fields accessed directly by compiler. See basis/vm/vm.factor
20
21   /* Factor callstack pointers */
22   void* callstack_top;
23   void* callstack_bottom;
24
25   /* current datastack top pointer */
26   cell datastack;
27
28   /* current retain stack top pointer */
29   cell retainstack;
30
31   /* C callstack pointer */
32   cell callstack_save;
33
34   segment* datastack_seg;
35   segment* retainstack_seg;
36   segment* callstack_seg;
37
38   /* context-specific special objects, accessed by context-object and
39      set-context-object primitives */
40   cell context_objects[context_object_count];
41
42   context(cell datastack_size, cell retainstack_size, cell callstack_size);
43   ~context();
44
45   void reset_datastack();
46   void reset_retainstack();
47   void reset_callstack();
48   void reset_context_objects();
49   void reset();
50   void scrub_stacks(gc_info* info, cell index);
51
52   cell peek() { return *(cell*)datastack; }
53
54   void replace(cell tagged) { *(cell*)datastack = tagged; }
55
56   cell pop() {
57     cell value = peek();
58     datastack -= sizeof(cell);
59     return value;
60   }
61
62   void push(cell tagged) {
63     datastack += sizeof(cell);
64     replace(tagged);
65   }
66 };
67
68 VM_C_API context* new_context(factor_vm* parent);
69 VM_C_API void delete_context(factor_vm* parent, context* old_context);
70 VM_C_API void reset_context(factor_vm* parent, context* ctx);
71 VM_C_API cell begin_callback(factor_vm* parent, cell quot);
72 VM_C_API void end_callback(factor_vm* parent);
73
74 }