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