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)
52 void context::scrub_stacks(gc_info* info, cell index) {
53 u8* bitmap = info->gc_info_bitmap();
56 cell base = info->callsite_scrub_d(index);
58 for (cell loc = 0; loc < info->scrub_d_count; loc++) {
59 if (bitmap_p(bitmap, base + loc)) {
61 std::cout << "scrubbing datastack location " << loc << std::endl;
63 *((cell*)datastack - loc) = 0;
69 cell base = info->callsite_scrub_r(index);
71 for (cell loc = 0; loc < info->scrub_r_count; loc++) {
72 if (bitmap_p(bitmap, base + loc)) {
74 std::cout << "scrubbing retainstack location " << loc << std::endl;
76 *((cell*)retainstack - loc) = 0;
84 delete retainstack_seg;
88 /* called on startup */
89 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_,
90 cell callstack_size_) {
91 datastack_size = datastack_size_;
92 retainstack_size = retainstack_size_;
93 callstack_size = callstack_size_;
96 spare_ctx = new_context();
99 void factor_vm::delete_contexts() {
101 std::list<context*>::const_iterator iter = unused_contexts.begin();
102 std::list<context*>::const_iterator end = unused_contexts.end();
103 while (iter != end) {
109 context* factor_vm::new_context() {
110 context* new_context;
112 if (unused_contexts.empty()) {
113 new_context = new context(datastack_size, retainstack_size, callstack_size);
115 new_context = unused_contexts.back();
116 unused_contexts.pop_back();
119 new_context->reset();
121 active_contexts.insert(new_context);
126 /* Allocates memory */
127 void factor_vm::init_context(context* ctx) {
128 ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
131 /* Allocates memory */
132 context* new_context(factor_vm* parent) {
133 context* new_context = parent->new_context();
134 parent->init_context(new_context);
138 void factor_vm::delete_context(context* old_context) {
139 unused_contexts.push_back(old_context);
140 active_contexts.erase(old_context);
142 while (unused_contexts.size() > 10) {
143 context* stale_context = unused_contexts.front();
144 unused_contexts.pop_front();
145 delete stale_context;
149 VM_C_API void delete_context(factor_vm* parent, context* old_context) {
150 parent->delete_context(old_context);
153 /* Allocates memory */
154 VM_C_API void reset_context(factor_vm* parent, context* ctx) {
156 parent->init_context(ctx);
159 /* Allocates memory */
160 cell factor_vm::begin_callback(cell quot_) {
161 data_root<object> quot(quot_, this);
164 spare_ctx = new_context();
165 callback_ids.push_back(callback_id++);
172 cell begin_callback(factor_vm* parent, cell quot) {
173 return parent->begin_callback(quot);
176 void factor_vm::end_callback() {
177 callback_ids.pop_back();
181 void end_callback(factor_vm* parent) { parent->end_callback(); }
183 void factor_vm::primitive_current_callback() {
184 ctx->push(tag_fixnum(callback_ids.back()));
187 void factor_vm::primitive_context_object() {
188 fixnum n = untag_fixnum(ctx->peek());
189 ctx->replace(ctx->context_objects[n]);
192 void factor_vm::primitive_set_context_object() {
193 fixnum n = untag_fixnum(ctx->pop());
194 cell value = ctx->pop();
195 ctx->context_objects[n] = value;
198 void factor_vm::primitive_context_object_for() {
199 context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
200 fixnum n = untag_fixnum(ctx->peek());
201 ctx->replace(other_ctx->context_objects[n]);
204 /* Allocates memory */
205 cell factor_vm::stack_to_array(cell bottom, cell top) {
206 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
211 array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
212 memcpy(a + 1, (void*)bottom, depth);
213 return tag<array>(a);
217 cell factor_vm::datastack_to_array(context* ctx) {
218 cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
219 if (array == false_object) {
220 general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
226 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
228 void factor_vm::primitive_datastack_for() {
229 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
230 ctx->replace(datastack_to_array(other_ctx));
233 cell factor_vm::retainstack_to_array(context* ctx) {
234 cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
235 if (array == false_object) {
236 general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
242 void factor_vm::primitive_retainstack() {
243 ctx->push(retainstack_to_array(ctx));
246 void factor_vm::primitive_retainstack_for() {
247 context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
248 ctx->replace(retainstack_to_array(other_ctx));
251 /* returns pointer to top of stack */
252 cell factor_vm::array_to_stack(array* array, cell bottom) {
253 cell depth = array_capacity(array) * sizeof(cell);
254 memcpy((void*)bottom, array + 1, depth);
255 return bottom + depth - sizeof(cell);
258 void factor_vm::set_datastack(context* ctx, array* array) {
259 ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
262 void factor_vm::primitive_set_datastack() {
263 set_datastack(ctx, untag_check<array>(ctx->pop()));
266 void factor_vm::set_retainstack(context* ctx, array* array) {
267 ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
270 void factor_vm::primitive_set_retainstack() {
271 set_retainstack(ctx, untag_check<array>(ctx->pop()));
274 /* Used to implement call( */
275 void factor_vm::primitive_check_datastack() {
276 fixnum out = to_fixnum(ctx->pop());
277 fixnum in = to_fixnum(ctx->pop());
278 fixnum height = out - in;
279 array* saved_datastack = untag_check<array>(ctx->pop());
280 fixnum saved_height = array_capacity(saved_datastack);
281 fixnum current_height =
282 (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
284 if (current_height - height != saved_height)
285 ctx->push(false_object);
287 cell* ds_bot = (cell*)ctx->datastack_seg->start;
288 for (fixnum i = 0; i < saved_height - in; i++) {
289 if (ds_bot[i] != array_nth(saved_datastack, i)) {
290 ctx->push(false_object);
294 ctx->push(true_object);
298 void factor_vm::primitive_load_locals() {
299 fixnum count = untag_fixnum(ctx->pop());
300 memcpy((cell*)(ctx->retainstack + sizeof(cell)),
301 (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
302 sizeof(cell) * count);
303 ctx->datastack -= sizeof(cell) * count;
304 ctx->retainstack += sizeof(cell) * count;