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