5 context::context(cell datastack_size, cell retainstack_size,
12 datastack_seg(new segment(datastack_size, false)),
13 retainstack_seg(new segment(retainstack_size, false)),
14 callstack_seg(new segment(callstack_size, false)) {
18 void context::reset_datastack() {
19 datastack = datastack_seg->start - sizeof(cell);
20 fill_stack_seg(datastack, datastack_seg, 0x11111111);
23 void context::reset_retainstack() {
24 retainstack = retainstack_seg->start - sizeof(cell);
25 fill_stack_seg(retainstack, retainstack_seg, 0x22222222);
28 void context::reset_callstack() {
29 callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
32 void context::reset_context_objects() {
33 memset_cell(context_objects, false_object,
34 context_object_count * sizeof(cell));
37 void context::fill_stack_seg(cell top_ptr, segment* seg, cell pattern) {
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);
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;
62 void context::reset() {
66 reset_context_objects();
69 void context::fix_stacks() {
70 if (datastack + sizeof(cell) < datastack_seg->start ||
71 datastack + stack_reserved >= datastack_seg->end)
74 if (retainstack + sizeof(cell) < retainstack_seg->start ||
75 retainstack + stack_reserved >= retainstack_seg->end)
81 delete retainstack_seg;
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_;
94 spare_ctx = new_context();
97 void factor_vm::delete_contexts() {
99 FACTOR_FOR_EACH(unused_contexts) {
104 context* factor_vm::new_context() {
105 context* new_context;
107 if (unused_contexts.empty()) {
108 new_context = new context(datastack_size, retainstack_size, callstack_size);
110 new_context = unused_contexts.back();
111 unused_contexts.pop_back();
114 new_context->reset();
116 active_contexts.insert(new_context);
121 /* Allocates memory */
122 void factor_vm::init_context(context* ctx) {
123 ctx->context_objects[OBJ_CONTEXT] = allot_alien((cell)ctx);
126 /* Allocates memory (init_context(), but not parent->new_context() */
127 VM_C_API context* new_context(factor_vm* parent) {
128 context* new_context = parent->new_context();
129 parent->init_context(new_context);
133 void factor_vm::delete_context() {
134 unused_contexts.push_back(ctx);
135 active_contexts.erase(ctx);
137 while (unused_contexts.size() > 10) {
138 context* stale_context = unused_contexts.front();
139 unused_contexts.pop_front();
140 delete stale_context;
144 VM_C_API void delete_context(factor_vm* parent) {
145 parent->delete_context();
148 /* Allocates memory (init_context()) */
149 VM_C_API void reset_context(factor_vm* parent) {
151 // The function is used by (start-context-and-delete) which expects
152 // the top two datastack items to be preserved after the context has
155 context* ctx = parent->ctx;
156 cell arg1 = ctx->pop();
157 cell arg2 = ctx->pop();
161 parent->init_context(ctx);
164 /* Allocates memory */
165 cell factor_vm::begin_callback(cell quot_) {
166 data_root<object> quot(quot_, this);
169 spare_ctx = new_context();
170 callback_ids.push_back(callback_id++);
177 /* Allocates memory */
178 cell begin_callback(factor_vm* parent, cell quot) {
179 return parent->begin_callback(quot);
182 void factor_vm::end_callback() {
183 callback_ids.pop_back();
187 void end_callback(factor_vm* parent) { parent->end_callback(); }
189 void factor_vm::primitive_current_callback() {
190 ctx->push(tag_fixnum(callback_ids.back()));
193 void factor_vm::primitive_context_object() {
194 fixnum n = untag_fixnum(ctx->peek());
195 ctx->replace(ctx->context_objects[n]);
198 void factor_vm::primitive_set_context_object() {
199 fixnum n = untag_fixnum(ctx->pop());
200 cell value = ctx->pop();
201 ctx->context_objects[n] = value;
204 void factor_vm::primitive_context_object_for() {
205 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
206 fixnum n = untag_fixnum(ctx->peek());
207 ctx->replace(other_ctx->context_objects[n]);
210 /* Allocates memory */
211 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
212 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
215 general_error(error, false_object, false_object);
217 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
218 memcpy(a + 1, (void*)bottom, depth);
219 return tag<array>(a);
222 /* Allocates memory */
223 cell factor_vm::datastack_to_array(context* ctx) {
224 return stack_to_array(ctx->datastack_seg->start,
226 ERROR_DATASTACK_UNDERFLOW);
229 /* Allocates memory */
230 void factor_vm::primitive_datastack_for() {
231 data_root<alien> alien_ctx(ctx->pop(), this);
232 context* other_ctx = (context*)pinned_alien_offset(alien_ctx.value());
233 cell array = datastack_to_array(other_ctx);
237 /* Allocates memory */
238 cell factor_vm::retainstack_to_array(context* ctx) {
239 return stack_to_array(ctx->retainstack_seg->start,
241 ERROR_RETAINSTACK_UNDERFLOW);
244 /* Allocates memory */
245 void factor_vm::primitive_retainstack_for() {
246 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
247 ctx->replace(retainstack_to_array(other_ctx));
250 /* returns pointer to top of stack */
251 cell factor_vm::array_to_stack(array* array, cell bottom) {
252 cell depth = array_capacity(array) * sizeof(cell);
253 memcpy((void*)bottom, array + 1, depth);
254 return bottom + depth - sizeof(cell);
257 void factor_vm::set_datastack(context* ctx, array* array) {
258 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
261 void factor_vm::primitive_set_datastack() {
262 set_datastack(ctx, untag_check<array>(ctx->pop()));
265 void factor_vm::set_retainstack(context* ctx, array* array) {
266 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
269 void factor_vm::primitive_set_retainstack() {
270 set_retainstack(ctx, untag_check<array>(ctx->pop()));
273 /* Used to implement call( */
274 void factor_vm::primitive_check_datastack() {
275 fixnum out = to_fixnum(ctx->pop());
276 fixnum in = to_fixnum(ctx->pop());
277 fixnum height = out - in;
278 array* saved_datastack = untag_check<array>(ctx->pop());
279 fixnum saved_height = array_capacity(saved_datastack);
280 fixnum current_height =
281 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
283 if (current_height - height != saved_height)
284 ctx->push(false_object);
286 cell* ds_bot = (cell*)ctx->datastack_seg->start;
287 for (fixnum i = 0; i < saved_height - in; i++) {
288 if (ds_bot[i] != array_nth(saved_datastack, i)) {
289 ctx->push(false_object);
293 ctx->push(special_objects[OBJ_CANONICAL_TRUE]);
297 void factor_vm::primitive_load_locals() {
298 fixnum count = untag_fixnum(ctx->pop());
299 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
300 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
301 sizeof(cell) * count);
302 ctx->datastack -= sizeof(cell) * count;
303 ctx->retainstack += sizeof(cell) * count;