]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.cpp
alien.syntax: clarify that we can dispatch off ENUM: members
[factor.git] / vm / contexts.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 context::context(cell ds_size, cell rs_size, cell cs_size)
6     : callstack_top(0),
7       callstack_bottom(0),
8       datastack(0),
9       retainstack(0),
10       callstack_save(0),
11       datastack_seg(new segment(ds_size, false)),
12       retainstack_seg(new segment(rs_size, false)),
13       callstack_seg(new segment(cs_size, false)) {
14   reset();
15 }
16
17 void context::reset_datastack() {
18   datastack = datastack_seg->start - sizeof(cell);
19   fill_stack_seg(datastack, datastack_seg, 0x11111111);
20 }
21
22 void context::reset_retainstack() {
23   retainstack = retainstack_seg->start - sizeof(cell);
24   fill_stack_seg(retainstack, retainstack_seg, 0x22222222);
25 }
26
27 void context::reset_callstack() {
28   callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
29 }
30
31 void context::reset_context_objects() {
32   memset_cell(context_objects, false_object,
33               context_object_count * sizeof(cell));
34 }
35
36 void context::fill_stack_seg(cell top_ptr, segment* seg, cell pattern) {
37 #ifdef FACTOR_DEBUG
38   cell clear_start = top_ptr + sizeof(cell);
39   cell clear_size = seg->end - clear_start;
40   memset_cell((void*)clear_start, pattern, clear_size);
41 #else
42   (void)top_ptr;
43   (void)seg;
44   (void)pattern;
45 #endif
46 }
47
48 vm_error_type context::address_to_error(cell addr) {
49   if (datastack_seg->underflow_p(addr))
50     return ERROR_DATASTACK_UNDERFLOW;
51   if (datastack_seg->overflow_p(addr))
52     return ERROR_DATASTACK_OVERFLOW;
53   if (retainstack_seg->underflow_p(addr))
54     return ERROR_RETAINSTACK_UNDERFLOW;
55   if (retainstack_seg->overflow_p(addr))
56     return ERROR_RETAINSTACK_OVERFLOW;
57   // These are flipped because the callstack grows downwards.
58   if (callstack_seg->underflow_p(addr))
59     return ERROR_CALLSTACK_OVERFLOW;
60   if (callstack_seg->overflow_p(addr))
61     return ERROR_CALLSTACK_UNDERFLOW;
62   return ERROR_MEMORY;
63 }
64
65 void context::reset() {
66   reset_datastack();
67   reset_retainstack();
68   reset_callstack();
69   reset_context_objects();
70 }
71
72 void context::fix_stacks() {
73   if (datastack + sizeof(cell) < datastack_seg->start ||
74       datastack + stack_reserved >= datastack_seg->end)
75     reset_datastack();
76
77   if (retainstack + sizeof(cell) < retainstack_seg->start ||
78       retainstack + stack_reserved >= retainstack_seg->end)
79     reset_retainstack();
80 }
81
82 context::~context() {
83   delete datastack_seg;
84   delete retainstack_seg;
85   delete callstack_seg;
86 }
87
88 context* factor_vm::new_context() {
89   context* new_context;
90
91   if (unused_contexts.empty()) {
92     new_context = new context(datastack_size, retainstack_size, callstack_size);
93   } else {
94     new_context = unused_contexts.back();
95     unused_contexts.pop_back();
96   }
97
98   new_context->reset();
99
100   active_contexts.insert(new_context);
101
102   return new_context;
103 }
104
105 // Allocates memory
106 void factor_vm::init_context(context* ctx) {
107   ctx->context_objects[OBJ_CONTEXT] = allot_alien((cell)ctx);
108 }
109
110 // Allocates memory (init_context(), but not parent->new_context()
111 VM_C_API context* new_context(factor_vm* parent) {
112   context* new_context = parent->new_context();
113   parent->init_context(new_context);
114   return new_context;
115 }
116
117 void factor_vm::delete_context() {
118   unused_contexts.push_back(ctx);
119   active_contexts.erase(ctx);
120
121   while (unused_contexts.size() > 10) {
122     context* stale_context = unused_contexts.front();
123     unused_contexts.pop_front();
124     delete stale_context;
125   }
126 }
127
128 VM_C_API void delete_context(factor_vm* parent) {
129   parent->delete_context();
130 }
131
132 // Allocates memory (init_context())
133 VM_C_API void reset_context(factor_vm* parent) {
134
135   // The function is used by (start-context-and-delete) which expects
136   // the top two datastack items to be preserved after the context has
137   // been resetted.
138
139   context* ctx = parent->ctx;
140   cell arg1 = ctx->pop();
141   cell arg2 = ctx->pop();
142   ctx->reset();
143   ctx->push(arg2);
144   ctx->push(arg1);
145   parent->init_context(ctx);
146 }
147
148 // Allocates memory
149 cell factor_vm::begin_callback(cell quot_) {
150   data_root<object> quot(quot_, this);
151
152   ctx->reset();
153   spare_ctx = new_context();
154   callback_ids.push_back(callback_id++);
155
156   init_context(ctx);
157
158   return quot.value();
159 }
160
161 // Allocates memory
162 cell begin_callback(factor_vm* parent, cell quot) {
163   return parent->begin_callback(quot);
164 }
165
166 void factor_vm::end_callback() {
167   callback_ids.pop_back();
168   delete_context();
169 }
170
171 void end_callback(factor_vm* parent) { parent->end_callback(); }
172
173 void factor_vm::primitive_current_callback() {
174   ctx->push(tag_fixnum(callback_ids.back()));
175 }
176
177 void factor_vm::primitive_context_object() {
178   fixnum n = untag_fixnum(ctx->peek());
179   ctx->replace(ctx->context_objects[n]);
180 }
181
182 void factor_vm::primitive_set_context_object() {
183   fixnum n = untag_fixnum(ctx->pop());
184   cell value = ctx->pop();
185   ctx->context_objects[n] = value;
186 }
187
188 void factor_vm::primitive_context_object_for() {
189   context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
190   fixnum n = untag_fixnum(ctx->peek());
191   ctx->replace(other_ctx->context_objects[n]);
192 }
193
194 // Allocates memory
195 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
196   fixnum depth = (fixnum)(top - bottom + sizeof(cell));
197
198   if (depth < 0) {
199     general_error(error, false_object, false_object);
200   }
201   array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
202   memcpy(a + 1, (void*)bottom, depth);
203   return tag<array>(a);
204 }
205
206 // Allocates memory
207 cell factor_vm::datastack_to_array(context* ctx) {
208   return stack_to_array(ctx->datastack_seg->start,
209                         ctx->datastack,
210                         ERROR_DATASTACK_UNDERFLOW);
211 }
212
213 // Allocates memory
214 void factor_vm::primitive_datastack_for() {
215   data_root<alien> alien_ctx(ctx->pop(), this);
216   context* other_ctx = (context*)pinned_alien_offset(alien_ctx.value());
217   cell array = datastack_to_array(other_ctx);
218   ctx->push(array);
219 }
220
221 // Allocates memory
222 cell factor_vm::retainstack_to_array(context* ctx) {
223   return stack_to_array(ctx->retainstack_seg->start,
224                         ctx->retainstack,
225                         ERROR_RETAINSTACK_UNDERFLOW);
226 }
227
228 // Allocates memory
229 void factor_vm::primitive_retainstack_for() {
230   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
231   ctx->replace(retainstack_to_array(other_ctx));
232 }
233
234 // returns pointer to top of stack
235 static cell array_to_stack(array* array, cell bottom) {
236   cell depth = array_capacity(array) * sizeof(cell);
237   memcpy((void*)bottom, array + 1, depth);
238   return bottom + depth - sizeof(cell);
239 }
240
241 void factor_vm::primitive_set_datastack() {
242   array* arr = untag_check<array>(ctx->pop());
243   ctx->datastack = array_to_stack(arr, ctx->datastack_seg->start);
244 }
245
246 void factor_vm::primitive_set_retainstack() {
247   array* arr = untag_check<array>(ctx->pop());
248   ctx->retainstack = array_to_stack(arr, ctx->retainstack_seg->start);
249 }
250
251 // Used to implement call(
252 void factor_vm::primitive_check_datastack() {
253   fixnum out = to_fixnum(ctx->pop());
254   fixnum in = to_fixnum(ctx->pop());
255   fixnum height = out - in;
256   array* saved_datastack = untag_check<array>(ctx->pop());
257   fixnum saved_height = array_capacity(saved_datastack);
258   fixnum current_height =
259       (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
260       sizeof(cell);
261   if (current_height - height != saved_height)
262     ctx->push(false_object);
263   else {
264     cell* ds_bot = (cell*)ctx->datastack_seg->start;
265     for (fixnum i = 0; i < saved_height - in; i++) {
266       if (ds_bot[i] != array_nth(saved_datastack, i)) {
267         ctx->push(false_object);
268         return;
269       }
270     }
271     ctx->push(special_objects[OBJ_CANONICAL_TRUE]);
272   }
273 }
274
275 void factor_vm::primitive_load_locals() {
276   fixnum count = untag_fixnum(ctx->pop());
277   memcpy((cell*)(ctx->retainstack + sizeof(cell)),
278          (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
279          sizeof(cell) * count);
280   ctx->datastack -= sizeof(cell) * count;
281   ctx->retainstack += sizeof(cell) * count;
282 }
283
284 }