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