5 context::context(cell datastack_size, cell retainstack_size,
8 callstack_bottom(NULL),
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 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(context* old_context) {
110 unused_contexts.push_back(old_context);
111 active_contexts.erase(old_context);
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, context* old_context) {
121 parent->delete_context(old_context);
124 /* Allocates memory (init_context()) */
125 VM_C_API void reset_context(factor_vm* parent, context* ctx) {
127 parent->init_context(ctx);
130 /* Allocates memory */
131 cell factor_vm::begin_callback(cell quot_) {
132 data_root<object> quot(quot_, this);
135 spare_ctx = new_context();
136 callback_ids.push_back(callback_id++);
143 /* Allocates memory */
144 cell begin_callback(factor_vm* parent, cell quot) {
145 return parent->begin_callback(quot);
148 void factor_vm::end_callback() {
149 callback_ids.pop_back();
153 void end_callback(factor_vm* parent) { parent->end_callback(); }
155 void factor_vm::primitive_current_callback() {
156 ctx->push(tag_fixnum(callback_ids.back()));
159 void factor_vm::primitive_context_object() {
160 fixnum n = untag_fixnum(ctx->peek());
161 ctx->replace(ctx->context_objects[n]);
164 void factor_vm::primitive_set_context_object() {
165 fixnum n = untag_fixnum(ctx->pop());
166 cell value = ctx->pop();
167 ctx->context_objects[n] = value;
170 void factor_vm::primitive_context_object_for() {
171 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
172 fixnum n = untag_fixnum(ctx->peek());
173 ctx->replace(other_ctx->context_objects[n]);
176 /* Allocates memory */
177 cell factor_vm::stack_to_array(cell bottom, cell top) {
178 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
183 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
184 memcpy(a + 1, (void*)bottom, depth);
185 return tag<array>(a);
189 /* Allocates memory */
190 cell factor_vm::datastack_to_array(context* ctx) {
191 cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
192 if (array == false_object) {
193 general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
199 /* Allocates memory */
200 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
202 /* Allocates memory */
203 void factor_vm::primitive_datastack_for() {
204 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
205 ctx->replace(datastack_to_array(other_ctx));
208 /* Allocates memory */
209 cell factor_vm::retainstack_to_array(context* ctx) {
210 cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
211 if (array == false_object) {
212 general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
218 /* Allocates memory */
219 void factor_vm::primitive_retainstack() {
220 ctx->push(retainstack_to_array(ctx));
223 /* Allocates memory */
224 void factor_vm::primitive_retainstack_for() {
225 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
226 ctx->replace(retainstack_to_array(other_ctx));
229 /* returns pointer to top of stack */
230 cell factor_vm::array_to_stack(array* array, cell bottom) {
231 cell depth = array_capacity(array) * sizeof(cell);
232 memcpy((void*)bottom, array + 1, depth);
233 return bottom + depth - sizeof(cell);
236 void factor_vm::set_datastack(context* ctx, array* array) {
237 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
240 void factor_vm::primitive_set_datastack() {
241 set_datastack(ctx, untag_check<array>(ctx->pop()));
244 void factor_vm::set_retainstack(context* ctx, array* array) {
245 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
248 void factor_vm::primitive_set_retainstack() {
249 set_retainstack(ctx, untag_check<array>(ctx->pop()));
252 /* Used to implement call( */
253 void factor_vm::primitive_check_datastack() {
254 fixnum out = to_fixnum(ctx->pop());
255 fixnum in = to_fixnum(ctx->pop());
256 fixnum height = out - in;
257 array* saved_datastack = untag_check<array>(ctx->pop());
258 fixnum saved_height = array_capacity(saved_datastack);
259 fixnum current_height =
260 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
262 if (current_height - height != saved_height)
263 ctx->push(false_object);
265 cell* ds_bot = (cell*)ctx->datastack_seg->start;
266 for (fixnum i = 0; i < saved_height - in; i++) {
267 if (ds_bot[i] != array_nth(saved_datastack, i)) {
268 ctx->push(false_object);
272 ctx->push(true_object);
276 void factor_vm::primitive_load_locals() {
277 fixnum count = untag_fixnum(ctx->pop());
278 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
279 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
280 sizeof(cell) * count);
281 ctx->datastack -= sizeof(cell) * count;
282 ctx->retainstack += sizeof(cell) * count;