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);
22 void context::reset_retainstack() {
23 retainstack = retainstack_seg->start - sizeof(cell);
26 void context::reset_callstack() {
27 callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
30 void context::reset_context_objects() {
31 memset_cell(context_objects, false_object,
32 context_object_count * sizeof(cell));
35 void context::reset() {
39 reset_context_objects();
42 void context::fix_stacks() {
43 if (datastack + sizeof(cell) < datastack_seg->start ||
44 datastack + stack_reserved >= datastack_seg->end)
47 if (retainstack + sizeof(cell) < retainstack_seg->start ||
48 retainstack + stack_reserved >= retainstack_seg->end)
54 delete retainstack_seg;
58 /* called on startup */
59 /* Allocates memory (new_context()) */
60 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_,
61 cell callstack_size_) {
62 datastack_size = datastack_size_;
63 retainstack_size = retainstack_size_;
64 callstack_size = callstack_size_;
67 spare_ctx = new_context();
70 void factor_vm::delete_contexts() {
72 std::list<context*>::const_iterator iter = unused_contexts.begin();
73 std::list<context*>::const_iterator end = unused_contexts.end();
80 context* factor_vm::new_context() {
83 if (unused_contexts.empty()) {
84 new_context = new context(datastack_size, retainstack_size, callstack_size);
86 new_context = unused_contexts.back();
87 unused_contexts.pop_back();
92 active_contexts.insert(new_context);
97 /* Allocates memory */
98 void factor_vm::init_context(context* ctx) {
99 ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
102 /* Allocates memory (init_context(), but not parent->new_context() */
103 VM_C_API context* new_context(factor_vm* parent) {
104 context* new_context = parent->new_context();
105 parent->init_context(new_context);
109 void factor_vm::delete_context() {
110 unused_contexts.push_back(ctx);
111 active_contexts.erase(ctx);
113 while (unused_contexts.size() > 10) {
114 context* stale_context = unused_contexts.front();
115 unused_contexts.pop_front();
116 delete stale_context;
120 VM_C_API void delete_context(factor_vm* parent) {
121 parent->delete_context();
124 /* Allocates memory (init_context()) */
125 VM_C_API void reset_context(factor_vm* parent) {
127 // The function is used by (start-context-and-delete) which expects
128 // the top two datastack items to be preserved after the context has
131 context* ctx = parent->ctx;
132 cell arg1 = ctx->pop();
133 cell arg2 = ctx->pop();
137 parent->init_context(ctx);
140 /* Allocates memory */
141 cell factor_vm::begin_callback(cell quot_) {
142 data_root<object> quot(quot_, this);
145 spare_ctx = new_context();
146 callback_ids.push_back(callback_id++);
153 /* Allocates memory */
154 cell begin_callback(factor_vm* parent, cell quot) {
155 return parent->begin_callback(quot);
158 void factor_vm::end_callback() {
159 callback_ids.pop_back();
163 void end_callback(factor_vm* parent) { parent->end_callback(); }
165 void factor_vm::primitive_current_callback() {
166 ctx->push(tag_fixnum(callback_ids.back()));
169 void factor_vm::primitive_context_object() {
170 fixnum n = untag_fixnum(ctx->peek());
171 ctx->replace(ctx->context_objects[n]);
174 void factor_vm::primitive_set_context_object() {
175 fixnum n = untag_fixnum(ctx->pop());
176 cell value = ctx->pop();
177 ctx->context_objects[n] = value;
180 void factor_vm::primitive_context_object_for() {
181 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
182 fixnum n = untag_fixnum(ctx->peek());
183 ctx->replace(other_ctx->context_objects[n]);
186 /* Allocates memory */
187 cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
188 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
191 general_error(error, false_object, false_object);
193 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
194 memcpy(a + 1, (void*)bottom, depth);
195 return tag<array>(a);
198 /* Allocates memory */
199 cell factor_vm::datastack_to_array(context* ctx) {
200 return stack_to_array(ctx->datastack_seg->start,
202 ERROR_DATASTACK_UNDERFLOW);
205 /* Allocates memory */
206 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
208 /* Allocates memory */
209 void factor_vm::primitive_datastack_for() {
210 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
211 ctx->replace(datastack_to_array(other_ctx));
214 /* Allocates memory */
215 cell factor_vm::retainstack_to_array(context* ctx) {
216 return stack_to_array(ctx->retainstack_seg->start,
218 ERROR_RETAINSTACK_UNDERFLOW);
221 /* Allocates memory */
222 void factor_vm::primitive_retainstack() {
223 ctx->push(retainstack_to_array(ctx));
226 /* Allocates memory */
227 void factor_vm::primitive_retainstack_for() {
228 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
229 ctx->replace(retainstack_to_array(other_ctx));
232 /* returns pointer to top of stack */
233 cell factor_vm::array_to_stack(array* array, cell bottom) {
234 cell depth = array_capacity(array) * sizeof(cell);
235 memcpy((void*)bottom, array + 1, depth);
236 return bottom + depth - sizeof(cell);
239 void factor_vm::set_datastack(context* ctx, array* array) {
240 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
243 void factor_vm::primitive_set_datastack() {
244 set_datastack(ctx, untag_check<array>(ctx->pop()));
247 void factor_vm::set_retainstack(context* ctx, array* array) {
248 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
251 void factor_vm::primitive_set_retainstack() {
252 set_retainstack(ctx, untag_check<array>(ctx->pop()));
255 /* Used to implement call( */
256 void factor_vm::primitive_check_datastack() {
257 fixnum out = to_fixnum(ctx->pop());
258 fixnum in = to_fixnum(ctx->pop());
259 fixnum height = out - in;
260 array* saved_datastack = untag_check<array>(ctx->pop());
261 fixnum saved_height = array_capacity(saved_datastack);
262 fixnum current_height =
263 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
265 if (current_height - height != saved_height)
266 ctx->push(false_object);
268 cell* ds_bot = (cell*)ctx->datastack_seg->start;
269 for (fixnum i = 0; i < saved_height - in; i++) {
270 if (ds_bot[i] != array_nth(saved_datastack, i)) {
271 ctx->push(false_object);
275 ctx->push(true_object);
279 void factor_vm::primitive_load_locals() {
280 fixnum count = untag_fixnum(ctx->pop());
281 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
282 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
283 sizeof(cell) * count);
284 ctx->datastack -= sizeof(cell) * count;
285 ctx->retainstack += sizeof(cell) * count;