]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.cpp
VM: refactoring to remove redundancy in retainstack|datastack_to_array
[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 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, vm_error_type error) {
178   fixnum depth = (fixnum)(top - bottom + sizeof(cell));
179
180   if (depth < 0) {
181     general_error(error, false_object, false_object);
182   }
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 /* Allocates memory */
189 cell factor_vm::datastack_to_array(context* ctx) {
190   return stack_to_array(ctx->datastack_seg->start,
191                         ctx->datastack,
192                         ERROR_DATASTACK_UNDERFLOW);
193 }
194
195 /* Allocates memory */
196 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
197
198 /* Allocates memory */
199 void factor_vm::primitive_datastack_for() {
200   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
201   ctx->replace(datastack_to_array(other_ctx));
202 }
203
204 /* Allocates memory */
205 cell factor_vm::retainstack_to_array(context* ctx) {
206   return stack_to_array(ctx->retainstack_seg->start,
207                         ctx->retainstack,
208                         ERROR_RETAINSTACK_UNDERFLOW);
209 }
210
211 /* Allocates memory */
212 void factor_vm::primitive_retainstack() {
213   ctx->push(retainstack_to_array(ctx));
214 }
215
216 /* Allocates memory */
217 void factor_vm::primitive_retainstack_for() {
218   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
219   ctx->replace(retainstack_to_array(other_ctx));
220 }
221
222 /* returns pointer to top of stack */
223 cell factor_vm::array_to_stack(array* array, cell bottom) {
224   cell depth = array_capacity(array) * sizeof(cell);
225   memcpy((void*)bottom, array + 1, depth);
226   return bottom + depth - sizeof(cell);
227 }
228
229 void factor_vm::set_datastack(context* ctx, array* array) {
230   ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
231 }
232
233 void factor_vm::primitive_set_datastack() {
234   set_datastack(ctx, untag_check<array>(ctx->pop()));
235 }
236
237 void factor_vm::set_retainstack(context* ctx, array* array) {
238   ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
239 }
240
241 void factor_vm::primitive_set_retainstack() {
242   set_retainstack(ctx, untag_check<array>(ctx->pop()));
243 }
244
245 /* Used to implement call( */
246 void factor_vm::primitive_check_datastack() {
247   fixnum out = to_fixnum(ctx->pop());
248   fixnum in = to_fixnum(ctx->pop());
249   fixnum height = out - in;
250   array* saved_datastack = untag_check<array>(ctx->pop());
251   fixnum saved_height = array_capacity(saved_datastack);
252   fixnum current_height =
253       (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
254       sizeof(cell);
255   if (current_height - height != saved_height)
256     ctx->push(false_object);
257   else {
258     cell* ds_bot = (cell*)ctx->datastack_seg->start;
259     for (fixnum i = 0; i < saved_height - in; i++) {
260       if (ds_bot[i] != array_nth(saved_datastack, i)) {
261         ctx->push(false_object);
262         return;
263       }
264     }
265     ctx->push(true_object);
266   }
267 }
268
269 void factor_vm::primitive_load_locals() {
270   fixnum count = untag_fixnum(ctx->pop());
271   memcpy((cell*)(ctx->retainstack + sizeof(cell)),
272          (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
273          sizeof(cell) * count);
274   ctx->datastack -= sizeof(cell) * count;
275   ctx->retainstack += sizeof(cell) * count;
276 }
277
278 }