2009-11-05 20:03:51 -05:00
|
|
|
#include "master.hpp"
|
|
|
|
|
|
|
|
namespace factor
|
|
|
|
{
|
|
|
|
|
|
|
|
void factor_vm::primitive_special_object()
|
|
|
|
{
|
|
|
|
fixnum e = untag_fixnum(dpeek());
|
|
|
|
drepl(special_objects[e]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void factor_vm::primitive_set_special_object()
|
|
|
|
{
|
|
|
|
fixnum e = untag_fixnum(dpop());
|
|
|
|
cell value = dpop();
|
|
|
|
special_objects[e] = value;
|
|
|
|
}
|
|
|
|
|
2009-11-10 22:06:36 -05:00
|
|
|
void factor_vm::primitive_identity_hashcode()
|
|
|
|
{
|
|
|
|
cell tagged = dpeek();
|
|
|
|
if(immediate_p(tagged))
|
|
|
|
drepl(tagged & ~TAG_MASK);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
object *obj = untag<object>(tagged);
|
|
|
|
if(obj->hashcode() == 0)
|
|
|
|
{
|
|
|
|
/* Use megamorphic_cache_misses as a random source of randomness */
|
|
|
|
obj->set_hashcode(((cell)obj / block_granularity) ^ dispatch_stats.megamorphic_cache_hits);
|
|
|
|
}
|
|
|
|
drepl(tag_fixnum(obj->hashcode()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-05 20:03:51 -05:00
|
|
|
void factor_vm::primitive_set_slot()
|
|
|
|
{
|
|
|
|
fixnum slot = untag_fixnum(dpop());
|
|
|
|
object *obj = untag<object>(dpop());
|
|
|
|
cell value = dpop();
|
|
|
|
|
|
|
|
cell *slot_ptr = &obj->slots()[slot];
|
|
|
|
*slot_ptr = value;
|
|
|
|
write_barrier(slot_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cell factor_vm::clone_object(cell obj_)
|
|
|
|
{
|
|
|
|
data_root<object> obj(obj_,this);
|
|
|
|
|
|
|
|
if(immediate_p(obj.value()))
|
|
|
|
return obj.value();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell size = object_size(obj.value());
|
2009-11-10 22:06:36 -05:00
|
|
|
object *new_obj = allot_object(obj.type(),size);
|
2009-11-05 20:03:51 -05:00
|
|
|
memcpy(new_obj,obj.untagged(),size);
|
2009-11-10 22:06:36 -05:00
|
|
|
new_obj->set_hashcode(0);
|
2009-11-05 20:03:51 -05:00
|
|
|
return tag_dynamic(new_obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void factor_vm::primitive_clone()
|
|
|
|
{
|
|
|
|
drepl(clone_object(dpeek()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Size of the object pointed to by a tagged pointer */
|
|
|
|
cell factor_vm::object_size(cell tagged)
|
|
|
|
{
|
|
|
|
if(immediate_p(tagged))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return untag<object>(tagged)->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void factor_vm::primitive_size()
|
|
|
|
{
|
|
|
|
box_unsigned_cell(object_size(dpop()));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct slot_become_visitor {
|
|
|
|
std::map<object *,object *> *become_map;
|
|
|
|
|
|
|
|
explicit slot_become_visitor(std::map<object *,object *> *become_map_) :
|
|
|
|
become_map(become_map_) {}
|
|
|
|
|
|
|
|
object *operator()(object *old)
|
|
|
|
{
|
|
|
|
std::map<object *,object *>::const_iterator iter = become_map->find(old);
|
|
|
|
if(iter != become_map->end())
|
|
|
|
return iter->second;
|
|
|
|
else
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct object_become_visitor {
|
|
|
|
slot_visitor<slot_become_visitor> *workhorse;
|
|
|
|
|
|
|
|
explicit object_become_visitor(slot_visitor<slot_become_visitor> *workhorse_) :
|
|
|
|
workhorse(workhorse_) {}
|
|
|
|
|
2009-11-05 22:49:03 -05:00
|
|
|
void operator()(object *obj)
|
2009-11-05 20:03:51 -05:00
|
|
|
{
|
2009-11-05 22:49:03 -05:00
|
|
|
workhorse->visit_slots(obj);
|
2009-11-05 20:03:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
|
|
|
|
to coalesce equal but distinct quotations and wrappers. */
|
|
|
|
void factor_vm::primitive_become()
|
|
|
|
{
|
|
|
|
array *new_objects = untag_check<array>(dpop());
|
|
|
|
array *old_objects = untag_check<array>(dpop());
|
|
|
|
|
|
|
|
cell capacity = array_capacity(new_objects);
|
|
|
|
if(capacity != array_capacity(old_objects))
|
|
|
|
critical_error("bad parameters to become",0);
|
|
|
|
|
|
|
|
/* Build the forwarding map */
|
|
|
|
std::map<object *,object *> become_map;
|
|
|
|
|
|
|
|
for(cell i = 0; i < capacity; i++)
|
|
|
|
{
|
|
|
|
tagged<object> old_obj(array_nth(old_objects,i));
|
|
|
|
tagged<object> new_obj(array_nth(new_objects,i));
|
|
|
|
|
|
|
|
if(old_obj != new_obj)
|
|
|
|
become_map[old_obj.untagged()] = new_obj.untagged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update all references to old objects to point to new objects */
|
|
|
|
slot_visitor<slot_become_visitor> workhorse(this,slot_become_visitor(&become_map));
|
|
|
|
workhorse.visit_roots();
|
|
|
|
workhorse.visit_contexts();
|
|
|
|
|
|
|
|
object_become_visitor object_visitor(&workhorse);
|
|
|
|
each_object(object_visitor);
|
|
|
|
|
|
|
|
/* Since we may have introduced old->new references, need to revisit
|
|
|
|
all objects on a minor GC. */
|
|
|
|
data->mark_all_cards();
|
2009-11-05 22:49:03 -05:00
|
|
|
primitive_minor_gc();
|
2009-11-05 20:03:51 -05:00
|
|
|
|
|
|
|
/* If a word's definition quotation was in old_objects and the
|
|
|
|
quotation in new_objects is not compiled, we might leak memory
|
|
|
|
by referencing the old quotation unless we recompile all
|
|
|
|
unoptimized words. */
|
|
|
|
compile_all_words();
|
|
|
|
|
|
|
|
/* Update references to old objects in the code heap */
|
|
|
|
update_code_heap_words_and_literals();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|