6 void factor_vm::primitive_special_object()
8 fixnum e = untag_fixnum(dpeek());
9 drepl(special_objects[e]);
12 void factor_vm::primitive_set_special_object()
14 fixnum e = untag_fixnum(dpop());
16 special_objects[e] = value;
19 void factor_vm::primitive_set_slot()
21 fixnum slot = untag_fixnum(dpop());
22 object *obj = untag<object>(dpop());
25 cell *slot_ptr = &obj->slots()[slot];
27 write_barrier(slot_ptr);
30 cell factor_vm::clone_object(cell obj_)
32 data_root<object> obj(obj_,this);
34 if(immediate_p(obj.value()))
38 cell size = object_size(obj.value());
39 object *new_obj = allot_object(header(obj.type()),size);
40 memcpy(new_obj,obj.untagged(),size);
41 return tag_dynamic(new_obj);
45 void factor_vm::primitive_clone()
47 drepl(clone_object(dpeek()));
50 /* Size of the object pointed to by a tagged pointer */
51 cell factor_vm::object_size(cell tagged)
53 if(immediate_p(tagged))
56 return untag<object>(tagged)->size();
59 void factor_vm::primitive_size()
61 box_unsigned_cell(object_size(dpop()));
64 struct slot_become_visitor {
65 std::map<object *,object *> *become_map;
67 explicit slot_become_visitor(std::map<object *,object *> *become_map_) :
68 become_map(become_map_) {}
70 object *operator()(object *old)
72 std::map<object *,object *>::const_iterator iter = become_map->find(old);
73 if(iter != become_map->end())
80 struct object_become_visitor {
81 slot_visitor<slot_become_visitor> *workhorse;
83 explicit object_become_visitor(slot_visitor<slot_become_visitor> *workhorse_) :
84 workhorse(workhorse_) {}
86 void operator()(object *obj)
88 workhorse->visit_slots(obj);
92 /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
93 to coalesce equal but distinct quotations and wrappers. */
94 void factor_vm::primitive_become()
96 array *new_objects = untag_check<array>(dpop());
97 array *old_objects = untag_check<array>(dpop());
99 cell capacity = array_capacity(new_objects);
100 if(capacity != array_capacity(old_objects))
101 critical_error("bad parameters to become",0);
103 /* Build the forwarding map */
104 std::map<object *,object *> become_map;
106 for(cell i = 0; i < capacity; i++)
108 tagged<object> old_obj(array_nth(old_objects,i));
109 tagged<object> new_obj(array_nth(new_objects,i));
111 if(old_obj != new_obj)
112 become_map[old_obj.untagged()] = new_obj.untagged();
115 /* Update all references to old objects to point to new objects */
116 slot_visitor<slot_become_visitor> workhorse(this,slot_become_visitor(&become_map));
117 workhorse.visit_roots();
118 workhorse.visit_contexts();
120 object_become_visitor object_visitor(&workhorse);
121 each_object(object_visitor);
123 /* Since we may have introduced old->new references, need to revisit
124 all objects on a minor GC. */
125 data->mark_all_cards();
126 primitive_minor_gc();
128 /* If a word's definition quotation was in old_objects and the
129 quotation in new_objects is not compiled, we might leak memory
130 by referencing the old quotation unless we recompile all
131 unoptimized words. */
134 /* Update references to old objects in the code heap */
135 update_code_heap_words_and_literals();