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();
44 delete retainstack_seg;
48 /* called on startup */
49 /* Allocates memory (new_context()) */
50 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_,
51 cell callstack_size_) {
52 datastack_size = datastack_size_;
53 retainstack_size = retainstack_size_;
54 callstack_size = callstack_size_;
57 spare_ctx = new_context();
60 void factor_vm::delete_contexts() {
62 std::list<context*>::const_iterator iter = unused_contexts.begin();
63 std::list<context*>::const_iterator end = unused_contexts.end();
70 context* factor_vm::new_context() {
73 if (unused_contexts.empty()) {
74 new_context = new context(datastack_size, retainstack_size, callstack_size);
76 new_context = unused_contexts.back();
77 unused_contexts.pop_back();
82 active_contexts.insert(new_context);
87 /* Allocates memory */
88 void factor_vm::init_context(context* ctx) {
89 ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
92 /* Allocates memory (init_context(), but not parent->new_context() */
93 context* new_context(factor_vm* parent) {
94 context* new_context = parent->new_context();
95 parent->init_context(new_context);
99 void factor_vm::delete_context(context* old_context) {
100 unused_contexts.push_back(old_context);
101 active_contexts.erase(old_context);
103 while (unused_contexts.size() > 10) {
104 context* stale_context = unused_contexts.front();
105 unused_contexts.pop_front();
106 delete stale_context;
110 VM_C_API void delete_context(factor_vm* parent, context* old_context) {
111 parent->delete_context(old_context);
114 /* Allocates memory (init_context()) */
115 VM_C_API void reset_context(factor_vm* parent, context* ctx) {
117 parent->init_context(ctx);
120 /* Allocates memory */
121 cell factor_vm::begin_callback(cell quot_) {
122 data_root<object> quot(quot_, this);
125 spare_ctx = new_context();
126 callback_ids.push_back(callback_id++);
133 /* Allocates memory */
134 cell begin_callback(factor_vm* parent, cell quot) {
135 return parent->begin_callback(quot);
138 void factor_vm::end_callback() {
139 callback_ids.pop_back();
143 void end_callback(factor_vm* parent) { parent->end_callback(); }
145 void factor_vm::primitive_current_callback() {
146 ctx->push(tag_fixnum(callback_ids.back()));
149 void factor_vm::primitive_context_object() {
150 fixnum n = untag_fixnum(ctx->peek());
151 ctx->replace(ctx->context_objects[n]);
154 void factor_vm::primitive_set_context_object() {
155 fixnum n = untag_fixnum(ctx->pop());
156 cell value = ctx->pop();
157 ctx->context_objects[n] = value;
160 void factor_vm::primitive_context_object_for() {
161 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
162 fixnum n = untag_fixnum(ctx->peek());
163 ctx->replace(other_ctx->context_objects[n]);
166 /* Allocates memory */
167 cell factor_vm::stack_to_array(cell bottom, cell top) {
168 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
173 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
174 memcpy(a + 1, (void*)bottom, depth);
175 return tag<array>(a);
179 /* Allocates memory */
180 cell factor_vm::datastack_to_array(context* ctx) {
181 cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
182 if (array == false_object) {
183 general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
189 /* Allocates memory */
190 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
192 /* Allocates memory */
193 void factor_vm::primitive_datastack_for() {
194 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
195 ctx->replace(datastack_to_array(other_ctx));
198 /* Allocates memory */
199 cell factor_vm::retainstack_to_array(context* ctx) {
200 cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
201 if (array == false_object) {
202 general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
208 /* Allocates memory */
209 void factor_vm::primitive_retainstack() {
210 ctx->push(retainstack_to_array(ctx));
213 /* Allocates memory */
214 void factor_vm::primitive_retainstack_for() {
215 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
216 ctx->replace(retainstack_to_array(other_ctx));
219 /* returns pointer to top of stack */
220 cell factor_vm::array_to_stack(array* array, cell bottom) {
221 cell depth = array_capacity(array) * sizeof(cell);
222 memcpy((void*)bottom, array + 1, depth);
223 return bottom + depth - sizeof(cell);
226 void factor_vm::set_datastack(context* ctx, array* array) {
227 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
230 void factor_vm::primitive_set_datastack() {
231 set_datastack(ctx, untag_check<array>(ctx->pop()));
234 void factor_vm::set_retainstack(context* ctx, array* array) {
235 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
238 void factor_vm::primitive_set_retainstack() {
239 set_retainstack(ctx, untag_check<array>(ctx->pop()));
242 /* Used to implement call( */
243 void factor_vm::primitive_check_datastack() {
244 fixnum out = to_fixnum(ctx->pop());
245 fixnum in = to_fixnum(ctx->pop());
246 fixnum height = out - in;
247 array* saved_datastack = untag_check<array>(ctx->pop());
248 fixnum saved_height = array_capacity(saved_datastack);
249 fixnum current_height =
250 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
252 if (current_height - height != saved_height)
253 ctx->push(false_object);
255 cell* ds_bot = (cell*)ctx->datastack_seg->start;
256 for (fixnum i = 0; i < saved_height - in; i++) {
257 if (ds_bot[i] != array_nth(saved_datastack, i)) {
258 ctx->push(false_object);
262 ctx->push(true_object);
266 void factor_vm::primitive_load_locals() {
267 fixnum count = untag_fixnum(ctx->pop());
268 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
269 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
270 sizeof(cell) * count);
271 ctx->datastack -= sizeof(cell) * count;
272 ctx->retainstack += sizeof(cell) * count;