5 context::context(cell ds_size, cell rs_size, cell cs_size)
11 datastack_seg(new segment(ds_size, false)),
12 retainstack_seg(new segment(rs_size, false)),
13 callstack_seg(new segment(cs_size, false)) {
17 void context::reset_datastack() {
18 datastack = datastack_seg->start - sizeof(cell);
19 fill_stack_seg(datastack, datastack_seg, 0x11111111);
22 void context::reset_retainstack() {
23 retainstack = retainstack_seg->start - sizeof(cell);
24 fill_stack_seg(retainstack, retainstack_seg, 0x22222222);
27 void context::reset_callstack() {
28 callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
31 void context::reset_context_objects() {
32 memset_cell(context_objects, false_object,
33 context_object_count * sizeof(cell));
36 void context::fill_stack_seg(cell top_ptr, segment* seg, cell pattern) {
38 cell clear_start = top_ptr + sizeof(cell);
39 cell clear_size = seg->end - clear_start;
40 memset_cell((void*)clear_start, pattern, clear_size);
48 vm_error_type context::address_to_error(cell addr) {
49 if (datastack_seg->underflow_p(addr))
50 return ERROR_DATASTACK_UNDERFLOW;
51 if (datastack_seg->overflow_p(addr))
52 return ERROR_DATASTACK_OVERFLOW;
53 if (retainstack_seg->underflow_p(addr))
54 return ERROR_RETAINSTACK_UNDERFLOW;
55 if (retainstack_seg->overflow_p(addr))
56 return ERROR_RETAINSTACK_OVERFLOW;
57 // These are flipped because the callstack grows downwards.
58 if (callstack_seg->underflow_p(addr))
59 return ERROR_CALLSTACK_OVERFLOW;
60 if (callstack_seg->overflow_p(addr))
61 return ERROR_CALLSTACK_UNDERFLOW;
65 void context::reset() {
69 reset_context_objects();
72 void context::fix_stacks() {
73 if (datastack + sizeof(cell) < datastack_seg->start ||
74 datastack + stack_reserved >= datastack_seg->end)
77 if (retainstack + sizeof(cell) < retainstack_seg->start ||
78 retainstack + stack_reserved >= retainstack_seg->end)
84 delete retainstack_seg;
88 context* factor_vm::new_context() {
91 if (unused_contexts.empty()) {
92 new_context = new context(datastack_size, retainstack_size, callstack_size);
94 new_context = unused_contexts.back();
96 unused_contexts.pop_back();
99 active_contexts.insert(new_context);
105 void factor_vm::init_context(context* ctx) {
106 ctx->context_objects[OBJ_CONTEXT] = allot_alien((cell)ctx);
109 // Allocates memory (init_context(), but not parent->new_context()
110 VM_C_API context* new_context(factor_vm* parent) {
111 context* new_context = parent->new_context();
112 parent->init_context(new_context);
116 void factor_vm::delete_context() {
117 unused_contexts.push_back(ctx);
118 active_contexts.erase(ctx);
120 while (unused_contexts.size() > 10) {
121 context* stale_context = unused_contexts.front();
122 unused_contexts.pop_front();
123 delete stale_context;
127 VM_C_API void delete_context(factor_vm* parent) {
128 parent->delete_context();
131 // Allocates memory (init_context())
132 VM_C_API void reset_context(factor_vm* parent) {
134 // The function is used by (start-context-and-delete) which expects
135 // the top two datastack items to be preserved after the context has
138 context* ctx = parent->ctx;
139 cell arg1 = ctx->pop();
140 cell arg2 = ctx->pop();
144 parent->init_context(ctx);
148 cell factor_vm::begin_callback(cell quot_) {
149 data_root<object> quot(quot_, this);
152 spare_ctx = new_context();
153 callback_ids.push_back(callback_id++);
161 cell begin_callback(factor_vm* parent, cell quot) {
162 return parent->begin_callback(quot);
165 void factor_vm::end_callback() {
166 callback_ids.pop_back();
170 void end_callback(factor_vm* parent) { parent->end_callback(); }
172 void factor_vm::primitive_current_callback() {
173 ctx->push(tag_fixnum(callback_ids.back()));
176 void factor_vm::primitive_context_object() {
177 fixnum n = untag_fixnum(ctx->peek());
178 ctx->replace(ctx->context_objects[n]);
181 void factor_vm::primitive_set_context_object() {
182 fixnum n = untag_fixnum(ctx->pop());
183 cell value = ctx->pop();
184 ctx->context_objects[n] = value;
187 void factor_vm::primitive_context_object_for() {
188 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
189 fixnum n = untag_fixnum(ctx->peek());
190 ctx->replace(other_ctx->context_objects[n]);
194 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
195 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
198 general_error(error, false_object, false_object);
200 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
201 memcpy(a + 1, (void*)bottom, depth);
202 return tag<array>(a);
206 cell factor_vm::datastack_to_array(context* ctx) {
207 return stack_to_array(ctx->datastack_seg->start,
209 ERROR_DATASTACK_UNDERFLOW);
213 void factor_vm::primitive_datastack_for() {
214 data_root<alien> alien_ctx(ctx->pop(), this);
215 context* other_ctx = (context*)pinned_alien_offset(alien_ctx.value());
216 cell array = datastack_to_array(other_ctx);
221 cell factor_vm::retainstack_to_array(context* ctx) {
222 return stack_to_array(ctx->retainstack_seg->start,
224 ERROR_RETAINSTACK_UNDERFLOW);
228 void factor_vm::primitive_retainstack_for() {
229 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
230 ctx->replace(retainstack_to_array(other_ctx));
233 // returns pointer to top of stack
234 static cell array_to_stack(array* array, cell bottom) {
235 cell depth = array_capacity(array) * sizeof(cell);
236 memcpy((void*)bottom, array + 1, depth);
237 return bottom + depth - sizeof(cell);
240 void factor_vm::primitive_set_datastack() {
241 array* arr = untag_check<array>(ctx->pop());
242 ctx->datastack = array_to_stack(arr, ctx->datastack_seg->start);
245 void factor_vm::primitive_set_retainstack() {
246 array* arr = untag_check<array>(ctx->pop());
247 ctx->retainstack = array_to_stack(arr, ctx->retainstack_seg->start);
250 // Used to implement call(
251 void factor_vm::primitive_check_datastack() {
252 fixnum out = to_fixnum(ctx->pop());
253 fixnum in = to_fixnum(ctx->pop());
254 fixnum height = out - in;
255 array* saved_datastack = untag_check<array>(ctx->pop());
256 fixnum saved_height = array_capacity(saved_datastack);
257 fixnum current_height =
258 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
260 if (current_height - height != saved_height)
261 ctx->push(false_object);
263 cell* ds_bot = (cell*)ctx->datastack_seg->start;
264 for (fixnum i = 0; i < saved_height - in; i++) {
265 if (ds_bot[i] != array_nth(saved_datastack, i)) {
266 ctx->push(false_object);
270 ctx->push(special_objects[OBJ_CANONICAL_TRUE]);
274 void factor_vm::primitive_load_locals() {
275 fixnum count = untag_fixnum(ctx->pop());
276 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
277 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
278 sizeof(cell) * count);
279 ctx->datastack -= sizeof(cell) * count;
280 ctx->retainstack += sizeof(cell) * count;