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