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