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 void context::reset() {
49 reset_context_objects();
52 void context::fix_stacks() {
53 if (datastack + sizeof(cell) < datastack_seg->start ||
54 datastack + stack_reserved >= datastack_seg->end)
57 if (retainstack + sizeof(cell) < retainstack_seg->start ||
58 retainstack + stack_reserved >= retainstack_seg->end)
64 delete retainstack_seg;
68 /* called on startup */
69 /* Allocates memory (new_context()) */
70 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_,
71 cell callstack_size_) {
72 datastack_size = datastack_size_;
73 retainstack_size = retainstack_size_;
74 callstack_size = callstack_size_;
77 spare_ctx = new_context();
80 void factor_vm::delete_contexts() {
82 std::list<context*>::const_iterator iter = unused_contexts.begin();
83 std::list<context*>::const_iterator end = unused_contexts.end();
90 context* factor_vm::new_context() {
93 if (unused_contexts.empty()) {
94 new_context = new context(datastack_size, retainstack_size, callstack_size);
96 new_context = unused_contexts.back();
97 unused_contexts.pop_back();
100 new_context->reset();
102 active_contexts.insert(new_context);
107 /* Allocates memory */
108 void factor_vm::init_context(context* ctx) {
109 ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
112 /* Allocates memory (init_context(), but not parent->new_context() */
113 VM_C_API context* new_context(factor_vm* parent) {
114 context* new_context = parent->new_context();
115 parent->init_context(new_context);
119 void factor_vm::delete_context() {
120 unused_contexts.push_back(ctx);
121 active_contexts.erase(ctx);
123 while (unused_contexts.size() > 10) {
124 context* stale_context = unused_contexts.front();
125 unused_contexts.pop_front();
126 delete stale_context;
130 VM_C_API void delete_context(factor_vm* parent) {
131 parent->delete_context();
134 /* Allocates memory (init_context()) */
135 VM_C_API void reset_context(factor_vm* parent) {
137 // The function is used by (start-context-and-delete) which expects
138 // the top two datastack items to be preserved after the context has
141 context* ctx = parent->ctx;
142 cell arg1 = ctx->pop();
143 cell arg2 = ctx->pop();
147 parent->init_context(ctx);
150 /* Allocates memory */
151 cell factor_vm::begin_callback(cell quot_) {
152 data_root<object> quot(quot_, this);
155 spare_ctx = new_context();
156 callback_ids.push_back(callback_id++);
163 /* Allocates memory */
164 cell begin_callback(factor_vm* parent, cell quot) {
165 return parent->begin_callback(quot);
168 void factor_vm::end_callback() {
169 callback_ids.pop_back();
173 void end_callback(factor_vm* parent) { parent->end_callback(); }
175 void factor_vm::primitive_current_callback() {
176 ctx->push(tag_fixnum(callback_ids.back()));
179 void factor_vm::primitive_context_object() {
180 fixnum n = untag_fixnum(ctx->peek());
181 ctx->replace(ctx->context_objects[n]);
184 void factor_vm::primitive_set_context_object() {
185 fixnum n = untag_fixnum(ctx->pop());
186 cell value = ctx->pop();
187 ctx->context_objects[n] = value;
190 void factor_vm::primitive_context_object_for() {
191 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
192 fixnum n = untag_fixnum(ctx->peek());
193 ctx->replace(other_ctx->context_objects[n]);
196 /* Allocates memory */
197 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
198 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
201 general_error(error, false_object, false_object);
203 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
204 memcpy(a + 1, (void*)bottom, depth);
205 return tag<array>(a);
208 /* Allocates memory */
209 cell factor_vm::datastack_to_array(context* ctx) {
210 return stack_to_array(ctx->datastack_seg->start,
212 ERROR_DATASTACK_UNDERFLOW);
215 /* Allocates memory */
216 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
218 /* Allocates memory */
219 void factor_vm::primitive_datastack_for() {
220 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
221 ctx->replace(datastack_to_array(other_ctx));
224 /* Allocates memory */
225 cell factor_vm::retainstack_to_array(context* ctx) {
226 return stack_to_array(ctx->retainstack_seg->start,
228 ERROR_RETAINSTACK_UNDERFLOW);
231 /* Allocates memory */
232 void factor_vm::primitive_retainstack() {
233 ctx->push(retainstack_to_array(ctx));
236 /* Allocates memory */
237 void factor_vm::primitive_retainstack_for() {
238 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
239 ctx->replace(retainstack_to_array(other_ctx));
242 /* returns pointer to top of stack */
243 cell factor_vm::array_to_stack(array* array, cell bottom) {
244 cell depth = array_capacity(array) * sizeof(cell);
245 memcpy((void*)bottom, array + 1, depth);
246 return bottom + depth - sizeof(cell);
249 void factor_vm::set_datastack(context* ctx, array* array) {
250 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
253 void factor_vm::primitive_set_datastack() {
254 set_datastack(ctx, untag_check<array>(ctx->pop()));
257 void factor_vm::set_retainstack(context* ctx, array* array) {
258 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
261 void factor_vm::primitive_set_retainstack() {
262 set_retainstack(ctx, untag_check<array>(ctx->pop()));
265 /* Used to implement call( */
266 void factor_vm::primitive_check_datastack() {
267 fixnum out = to_fixnum(ctx->pop());
268 fixnum in = to_fixnum(ctx->pop());
269 fixnum height = out - in;
270 array* saved_datastack = untag_check<array>(ctx->pop());
271 fixnum saved_height = array_capacity(saved_datastack);
272 fixnum current_height =
273 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
275 if (current_height - height != saved_height)
276 ctx->push(false_object);
278 cell* ds_bot = (cell*)ctx->datastack_seg->start;
279 for (fixnum i = 0; i < saved_height - in; i++) {
280 if (ds_bot[i] != array_nth(saved_datastack, i)) {
281 ctx->push(false_object);
285 ctx->push(true_object);
289 void factor_vm::primitive_load_locals() {
290 fixnum count = untag_fixnum(ctx->pop());
291 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
292 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
293 sizeof(cell) * count);
294 ctx->datastack -= sizeof(cell) * count;
295 ctx->retainstack += sizeof(cell) * count;