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