]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.cpp
Revert "VM: always clear the data and retainstack in general_error because they might...
[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(NULL),
8       callstack_bottom(NULL),
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 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(context* old_context) {
110   unused_contexts.push_back(old_context);
111   active_contexts.erase(old_context);
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, context* old_context) {
121   parent->delete_context(old_context);
122 }
123
124 /* Allocates memory (init_context()) */
125 VM_C_API void reset_context(factor_vm* parent, context* ctx) {
126   ctx->reset();
127   parent->init_context(ctx);
128 }
129
130 /* Allocates memory */
131 cell factor_vm::begin_callback(cell quot_) {
132   data_root<object> quot(quot_, this);
133
134   ctx->reset();
135   spare_ctx = new_context();
136   callback_ids.push_back(callback_id++);
137
138   init_context(ctx);
139
140   return quot.value();
141 }
142
143 /* Allocates memory */
144 cell begin_callback(factor_vm* parent, cell quot) {
145   return parent->begin_callback(quot);
146 }
147
148 void factor_vm::end_callback() {
149   callback_ids.pop_back();
150   delete_context(ctx);
151 }
152
153 void end_callback(factor_vm* parent) { parent->end_callback(); }
154
155 void factor_vm::primitive_current_callback() {
156   ctx->push(tag_fixnum(callback_ids.back()));
157 }
158
159 void factor_vm::primitive_context_object() {
160   fixnum n = untag_fixnum(ctx->peek());
161   ctx->replace(ctx->context_objects[n]);
162 }
163
164 void factor_vm::primitive_set_context_object() {
165   fixnum n = untag_fixnum(ctx->pop());
166   cell value = ctx->pop();
167   ctx->context_objects[n] = value;
168 }
169
170 void factor_vm::primitive_context_object_for() {
171   context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
172   fixnum n = untag_fixnum(ctx->peek());
173   ctx->replace(other_ctx->context_objects[n]);
174 }
175
176 /* Allocates memory */
177 cell factor_vm::stack_to_array(cell bottom, cell top) {
178   fixnum depth = (fixnum)(top - bottom + sizeof(cell));
179
180   if (depth < 0)
181     return false_object;
182   else {
183     array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
184     memcpy(a + 1, (void*)bottom, depth);
185     return tag<array>(a);
186   }
187 }
188
189 /* Allocates memory */
190 cell factor_vm::datastack_to_array(context* ctx) {
191   cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
192   if (array == false_object) {
193     general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
194     return false_object;
195   } else
196     return array;
197 }
198
199 /* Allocates memory */
200 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
201
202 /* Allocates memory */
203 void factor_vm::primitive_datastack_for() {
204   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
205   ctx->replace(datastack_to_array(other_ctx));
206 }
207
208 /* Allocates memory */
209 cell factor_vm::retainstack_to_array(context* ctx) {
210   cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
211   if (array == false_object) {
212     general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
213     return false_object;
214   } else
215     return array;
216 }
217
218 /* Allocates memory */
219 void factor_vm::primitive_retainstack() {
220   ctx->push(retainstack_to_array(ctx));
221 }
222
223 /* Allocates memory */
224 void factor_vm::primitive_retainstack_for() {
225   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
226   ctx->replace(retainstack_to_array(other_ctx));
227 }
228
229 /* returns pointer to top of stack */
230 cell factor_vm::array_to_stack(array* array, cell bottom) {
231   cell depth = array_capacity(array) * sizeof(cell);
232   memcpy((void*)bottom, array + 1, depth);
233   return bottom + depth - sizeof(cell);
234 }
235
236 void factor_vm::set_datastack(context* ctx, array* array) {
237   ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
238 }
239
240 void factor_vm::primitive_set_datastack() {
241   set_datastack(ctx, untag_check<array>(ctx->pop()));
242 }
243
244 void factor_vm::set_retainstack(context* ctx, array* array) {
245   ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
246 }
247
248 void factor_vm::primitive_set_retainstack() {
249   set_retainstack(ctx, untag_check<array>(ctx->pop()));
250 }
251
252 /* Used to implement call( */
253 void factor_vm::primitive_check_datastack() {
254   fixnum out = to_fixnum(ctx->pop());
255   fixnum in = to_fixnum(ctx->pop());
256   fixnum height = out - in;
257   array* saved_datastack = untag_check<array>(ctx->pop());
258   fixnum saved_height = array_capacity(saved_datastack);
259   fixnum current_height =
260       (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
261       sizeof(cell);
262   if (current_height - height != saved_height)
263     ctx->push(false_object);
264   else {
265     cell* ds_bot = (cell*)ctx->datastack_seg->start;
266     for (fixnum i = 0; i < saved_height - in; i++) {
267       if (ds_bot[i] != array_nth(saved_datastack, i)) {
268         ctx->push(false_object);
269         return;
270       }
271     }
272     ctx->push(true_object);
273   }
274 }
275
276 void factor_vm::primitive_load_locals() {
277   fixnum count = untag_fixnum(ctx->pop());
278   memcpy((cell*)(ctx->retainstack + sizeof(cell)),
279          (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
280          sizeof(cell) * count);
281   ctx->datastack -= sizeof(cell) * count;
282   ctx->retainstack += sizeof(cell) * count;
283 }
284
285 }