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);
44 vm_error_type context::address_to_error(cell addr) {
45 if (datastack_seg->underflow_p(addr))
46 return ERROR_DATASTACK_UNDERFLOW;
47 if (datastack_seg->overflow_p(addr))
48 return ERROR_DATASTACK_OVERFLOW;
49 if (retainstack_seg->underflow_p(addr))
50 return ERROR_RETAINSTACK_UNDERFLOW;
51 if (retainstack_seg->overflow_p(addr))
52 return ERROR_RETAINSTACK_OVERFLOW;
53 // These are flipped because the callstack grows downwards.
54 if (callstack_seg->underflow_p(addr))
55 return ERROR_CALLSTACK_OVERFLOW;
56 if (callstack_seg->overflow_p(addr))
57 return ERROR_CALLSTACK_UNDERFLOW;
61 void context::reset() {
65 reset_context_objects();
68 void context::fix_stacks() {
69 if (datastack + sizeof(cell) < datastack_seg->start ||
70 datastack + stack_reserved >= datastack_seg->end)
73 if (retainstack + sizeof(cell) < retainstack_seg->start ||
74 retainstack + stack_reserved >= retainstack_seg->end)
80 delete retainstack_seg;
84 context* factor_vm::new_context() {
87 if (unused_contexts.empty()) {
88 new_context = new context(datastack_size, retainstack_size, callstack_size);
90 new_context = unused_contexts.back();
91 unused_contexts.pop_back();
96 active_contexts.insert(new_context);
102 void factor_vm::init_context(context* ctx) {
103 ctx->context_objects[OBJ_CONTEXT] = allot_alien((cell)ctx);
106 // Allocates memory (init_context(), but not parent->new_context()
107 VM_C_API context* new_context(factor_vm* parent) {
108 context* new_context = parent->new_context();
109 parent->init_context(new_context);
113 void factor_vm::delete_context() {
114 unused_contexts.push_back(ctx);
115 active_contexts.erase(ctx);
117 while (unused_contexts.size() > 10) {
118 context* stale_context = unused_contexts.front();
119 unused_contexts.pop_front();
120 delete stale_context;
124 VM_C_API void delete_context(factor_vm* parent) {
125 parent->delete_context();
128 // Allocates memory (init_context())
129 VM_C_API void reset_context(factor_vm* parent) {
131 // The function is used by (start-context-and-delete) which expects
132 // the top two datastack items to be preserved after the context has
135 context* ctx = parent->ctx;
136 cell arg1 = ctx->pop();
137 cell arg2 = ctx->pop();
141 parent->init_context(ctx);
145 cell factor_vm::begin_callback(cell quot_) {
146 data_root<object> quot(quot_, this);
149 spare_ctx = new_context();
150 callback_ids.push_back(callback_id++);
158 cell begin_callback(factor_vm* parent, cell quot) {
159 return parent->begin_callback(quot);
162 void factor_vm::end_callback() {
163 callback_ids.pop_back();
167 void end_callback(factor_vm* parent) { parent->end_callback(); }
169 void factor_vm::primitive_current_callback() {
170 ctx->push(tag_fixnum(callback_ids.back()));
173 void factor_vm::primitive_context_object() {
174 fixnum n = untag_fixnum(ctx->peek());
175 ctx->replace(ctx->context_objects[n]);
178 void factor_vm::primitive_set_context_object() {
179 fixnum n = untag_fixnum(ctx->pop());
180 cell value = ctx->pop();
181 ctx->context_objects[n] = value;
184 void factor_vm::primitive_context_object_for() {
185 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
186 fixnum n = untag_fixnum(ctx->peek());
187 ctx->replace(other_ctx->context_objects[n]);
191 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
192 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
195 general_error(error, false_object, false_object);
197 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
198 memcpy(a + 1, (void*)bottom, depth);
199 return tag<array>(a);
203 cell factor_vm::datastack_to_array(context* ctx) {
204 return stack_to_array(ctx->datastack_seg->start,
206 ERROR_DATASTACK_UNDERFLOW);
210 void factor_vm::primitive_datastack_for() {
211 data_root<alien> alien_ctx(ctx->pop(), this);
212 context* other_ctx = (context*)pinned_alien_offset(alien_ctx.value());
213 cell array = datastack_to_array(other_ctx);
218 cell factor_vm::retainstack_to_array(context* ctx) {
219 return stack_to_array(ctx->retainstack_seg->start,
221 ERROR_RETAINSTACK_UNDERFLOW);
225 void factor_vm::primitive_retainstack_for() {
226 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
227 ctx->replace(retainstack_to_array(other_ctx));
230 // returns pointer to top of stack
231 static cell array_to_stack(array* array, cell bottom) {
232 cell depth = array_capacity(array) * sizeof(cell);
233 memcpy((void*)bottom, array + 1, depth);
234 return bottom + depth - sizeof(cell);
237 void factor_vm::primitive_set_datastack() {
238 array* arr = untag_check<array>(ctx->pop());
239 ctx->datastack = array_to_stack(arr, ctx->datastack_seg->start);
242 void factor_vm::primitive_set_retainstack() {
243 array* arr = untag_check<array>(ctx->pop());
244 ctx->retainstack = array_to_stack(arr, ctx->retainstack_seg->start);
247 // Used to implement call(
248 void factor_vm::primitive_check_datastack() {
249 fixnum out = to_fixnum(ctx->pop());
250 fixnum in = to_fixnum(ctx->pop());
251 fixnum height = out - in;
252 array* saved_datastack = untag_check<array>(ctx->pop());
253 fixnum saved_height = array_capacity(saved_datastack);
254 fixnum current_height =
255 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
257 if (current_height - height != saved_height)
258 ctx->push(false_object);
260 cell* ds_bot = (cell*)ctx->datastack_seg->start;
261 for (fixnum i = 0; i < saved_height - in; i++) {
262 if (ds_bot[i] != array_nth(saved_datastack, i)) {
263 ctx->push(false_object);
267 ctx->push(special_objects[OBJ_CANONICAL_TRUE]);
271 void factor_vm::primitive_load_locals() {
272 fixnum count = untag_fixnum(ctx->pop());
273 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
274 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
275 sizeof(cell) * count);
276 ctx->datastack -= sizeof(cell) * count;
277 ctx->retainstack += sizeof(cell) * count;