]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.hpp
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / vm / contexts.hpp
1 namespace factor
2 {
3
4 /* Assembly code makes assumptions about the layout of this struct */
5 struct context {
6         /* C stack pointer on entry */
7         stack_frame *callstack_top;
8         stack_frame *callstack_bottom;
9
10         /* current datastack top pointer */
11         cell datastack;
12
13         /* current retain stack top pointer */
14         cell retainstack;
15
16         /* memory region holding current datastack */
17         segment *datastack_region;
18
19         /* memory region holding current retain stack */
20         segment *retainstack_region;
21
22         /* saved special_objects slots on entry to callback */
23         cell catchstack_save;
24         cell current_callback_save;
25
26         context *next;
27
28         context(cell ds_size, cell rs_size);
29
30         cell peek()
31         {
32                 return *(cell *)datastack;
33         }
34
35         void replace(cell tagged)
36         {
37                 *(cell *)datastack = tagged;
38         }
39
40         cell pop()
41         {
42                 cell value = peek();
43                 datastack -= sizeof(cell);
44                 return value;
45         }
46
47         void push(cell tagged)
48         {
49                 datastack += sizeof(cell);
50                 replace(tagged);
51         }
52
53         void reset_datastack()
54         {
55                 datastack = datastack_region->start - sizeof(cell);
56         }
57
58         void reset_retainstack()
59         {
60                 retainstack = retainstack_region->start - sizeof(cell);
61         }
62
63         static const cell stack_reserved = (64 * sizeof(cell));
64
65         void fix_stacks()
66         {
67                 if(datastack + sizeof(cell) < datastack_region->start
68                         || datastack + stack_reserved >= datastack_region->end)
69                         reset_datastack();
70
71                 if(retainstack + sizeof(cell) < retainstack_region->start
72                         || retainstack + stack_reserved >= retainstack_region->end)
73                         reset_retainstack();
74         }
75 };
76
77 VM_C_API void nest_stacks(factor_vm *vm);
78 VM_C_API void unnest_stacks(factor_vm *vm);
79
80 }