VM: Refactor objects* to Factor style

db4
Erik Charlebois 2013-05-11 22:17:38 -04:00
parent 56ceeb582a
commit 228d813a7d
2 changed files with 197 additions and 221 deletions

View File

@ -1,187 +1,167 @@
#include "master.hpp" #include "master.hpp"
namespace factor namespace factor {
{
void factor_vm::primitive_special_object() void factor_vm::primitive_special_object() {
{ fixnum n = untag_fixnum(ctx->peek());
fixnum n = untag_fixnum(ctx->peek()); ctx->replace(special_objects[n]);
ctx->replace(special_objects[n]);
} }
void factor_vm::primitive_set_special_object() void factor_vm::primitive_set_special_object() {
{ fixnum n = untag_fixnum(ctx->pop());
fixnum n = untag_fixnum(ctx->pop()); cell value = ctx->pop();
cell value = ctx->pop(); special_objects[n] = value;
special_objects[n] = value;
} }
void factor_vm::primitive_identity_hashcode() void factor_vm::primitive_identity_hashcode() {
{ cell tagged = ctx->peek();
cell tagged = ctx->peek(); object* obj = untag<object>(tagged);
object *obj = untag<object>(tagged); ctx->replace(tag_fixnum(obj->hashcode()));
ctx->replace(tag_fixnum(obj->hashcode()));
} }
void factor_vm::compute_identity_hashcode(object *obj) void factor_vm::compute_identity_hashcode(object* obj) {
{ object_counter++;
object_counter++; if (object_counter == 0)
if(object_counter == 0) object_counter++; object_counter++;
obj->set_hashcode((cell)obj ^ object_counter); obj->set_hashcode((cell) obj ^ object_counter);
} }
void factor_vm::primitive_compute_identity_hashcode() void factor_vm::primitive_compute_identity_hashcode() {
{ object* obj = untag<object>(ctx->pop());
object *obj = untag<object>(ctx->pop()); compute_identity_hashcode(obj);
compute_identity_hashcode(obj);
} }
void factor_vm::primitive_set_slot() void factor_vm::primitive_set_slot() {
{ fixnum slot = untag_fixnum(ctx->pop());
fixnum slot = untag_fixnum(ctx->pop()); object* obj = untag<object>(ctx->pop());
object *obj = untag<object>(ctx->pop()); cell value = ctx->pop();
cell value = ctx->pop();
cell *slot_ptr = &obj->slots()[slot]; cell* slot_ptr = &obj->slots()[slot];
*slot_ptr = value; *slot_ptr = value;
write_barrier(slot_ptr); write_barrier(slot_ptr);
} }
/* Allocates memory */ /* Allocates memory */
cell factor_vm::clone_object(cell obj_) cell factor_vm::clone_object(cell obj_) {
{ data_root<object> obj(obj_, this);
data_root<object> obj(obj_,this);
if(immediate_p(obj.value())) if (immediate_p(obj.value()))
return obj.value(); return obj.value();
else else {
{ cell size = object_size(obj.value());
cell size = object_size(obj.value()); object* new_obj = allot_object(obj.type(), size);
object *new_obj = allot_object(obj.type(),size); memcpy(new_obj, obj.untagged(), size);
memcpy(new_obj,obj.untagged(),size); new_obj->set_hashcode(0);
new_obj->set_hashcode(0); return tag_dynamic(new_obj);
return tag_dynamic(new_obj); }
}
} }
/* Allocates memory */ /* Allocates memory */
void factor_vm::primitive_clone() void factor_vm::primitive_clone() { ctx->replace(clone_object(ctx->peek())); }
{
ctx->replace(clone_object(ctx->peek()));
}
/* Size of the object pointed to by a tagged pointer */ /* Size of the object pointed to by a tagged pointer */
cell factor_vm::object_size(cell tagged) cell factor_vm::object_size(cell tagged) {
{ if (immediate_p(tagged))
if(immediate_p(tagged)) return 0;
return 0; else
else return untag<object>(tagged)->size();
return untag<object>(tagged)->size();
} }
/* Allocates memory */ /* Allocates memory */
void factor_vm::primitive_size() void factor_vm::primitive_size() {
{ ctx->push(from_unsigned_cell(object_size(ctx->pop())));
ctx->push(from_unsigned_cell(object_size(ctx->pop())));
} }
struct slot_become_fixup : no_fixup { struct slot_become_fixup : no_fixup {
std::map<object *,object *> *become_map; std::map<object*, object*>* become_map;
explicit slot_become_fixup(std::map<object *,object *> *become_map_) : explicit slot_become_fixup(std::map<object*, object*>* become_map_)
become_map(become_map_) {} : become_map(become_map_) {}
object *fixup_data(object *old) object* fixup_data(object* old) {
{ std::map<object*, object*>::const_iterator iter = become_map->find(old);
std::map<object *,object *>::const_iterator iter = become_map->find(old); if (iter != become_map->end())
if(iter != become_map->end()) return iter->second;
return iter->second; else
else return old;
return old; }
}
}; };
struct object_become_visitor { struct object_become_visitor {
slot_visitor<slot_become_fixup> *workhorse; slot_visitor<slot_become_fixup>* workhorse;
explicit object_become_visitor(slot_visitor<slot_become_fixup> *workhorse_) : explicit object_become_visitor(slot_visitor<slot_become_fixup>* workhorse_)
workhorse(workhorse_) {} : workhorse(workhorse_) {}
void operator()(object *obj) void operator()(object* obj) { workhorse->visit_slots(obj); }
{
workhorse->visit_slots(obj);
}
}; };
struct code_block_become_visitor { struct code_block_become_visitor {
slot_visitor<slot_become_fixup> *workhorse; slot_visitor<slot_become_fixup>* workhorse;
explicit code_block_become_visitor(slot_visitor<slot_become_fixup> *workhorse_) : explicit code_block_become_visitor(
workhorse(workhorse_) {} slot_visitor<slot_become_fixup>* workhorse_)
: workhorse(workhorse_) {}
void operator()(code_block *compiled, cell size) void operator()(code_block* compiled, cell size) {
{ workhorse->visit_code_block_objects(compiled);
workhorse->visit_code_block_objects(compiled); workhorse->visit_embedded_literals(compiled);
workhorse->visit_embedded_literals(compiled); }
}
}; };
struct code_block_write_barrier_visitor { struct code_block_write_barrier_visitor {
code_heap *code; code_heap* code;
explicit code_block_write_barrier_visitor(code_heap *code_) : explicit code_block_write_barrier_visitor(code_heap* code_) : code(code_) {}
code(code_) {}
void operator()(code_block *compiled, cell size) void operator()(code_block* compiled, cell size) {
{ code->write_barrier(compiled);
code->write_barrier(compiled); }
}
}; };
/* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
to coalesce equal but distinct quotations and wrappers. */ to coalesce equal but distinct quotations and wrappers. */
void factor_vm::primitive_become() void factor_vm::primitive_become() {
{ array* new_objects = untag_check<array>(ctx->pop());
array *new_objects = untag_check<array>(ctx->pop()); array* old_objects = untag_check<array>(ctx->pop());
array *old_objects = untag_check<array>(ctx->pop());
cell capacity = array_capacity(new_objects); cell capacity = array_capacity(new_objects);
if(capacity != array_capacity(old_objects)) if (capacity != array_capacity(old_objects))
critical_error("bad parameters to become",0); critical_error("bad parameters to become", 0);
/* Build the forwarding map */ /* Build the forwarding map */
std::map<object *,object *> become_map; std::map<object*, object*> become_map;
for(cell i = 0; i < capacity; i++) for (cell i = 0; i < capacity; i++) {
{ tagged<object> old_obj(array_nth(old_objects, i));
tagged<object> old_obj(array_nth(old_objects,i)); tagged<object> new_obj(array_nth(new_objects, i));
tagged<object> new_obj(array_nth(new_objects,i));
if(old_obj != new_obj) if (old_obj != new_obj)
become_map[old_obj.untagged()] = new_obj.untagged(); become_map[old_obj.untagged()] = new_obj.untagged();
} }
/* Update all references to old objects to point to new objects */ /* Update all references to old objects to point to new objects */
{ {
slot_visitor<slot_become_fixup> workhorse(this,slot_become_fixup(&become_map)); slot_visitor<slot_become_fixup> workhorse(this,
workhorse.visit_roots(); slot_become_fixup(&become_map));
workhorse.visit_contexts(); workhorse.visit_roots();
workhorse.visit_contexts();
object_become_visitor object_visitor(&workhorse); object_become_visitor object_visitor(&workhorse);
each_object(object_visitor); each_object(object_visitor);
code_block_become_visitor code_block_visitor(&workhorse); code_block_become_visitor code_block_visitor(&workhorse);
each_code_block(code_block_visitor); each_code_block(code_block_visitor);
} }
/* Since we may have introduced old->new references, need to revisit /* Since we may have introduced old->new references, need to revisit
all objects and code blocks on a minor GC. */ all objects and code blocks on a minor GC. */
data->mark_all_cards(); data->mark_all_cards();
{ {
code_block_write_barrier_visitor code_block_visitor(code); code_block_write_barrier_visitor code_block_visitor(code);
each_code_block(code_block_visitor); each_code_block(code_block_visitor);
} }
} }
} }

View File

@ -1,5 +1,4 @@
namespace factor namespace factor {
{
// Special object count and identifiers must be kept in sync with: // Special object count and identifiers must be kept in sync with:
// core/kernel/kernel.factor // core/kernel/kernel.factor
@ -8,130 +7,127 @@ namespace factor
static const cell special_object_count = 80; static const cell special_object_count = 80;
enum special_object { enum special_object {
OBJ_WALKER_HOOK = 3, /* non-local exit hook, used by library only */ OBJ_WALKER_HOOK = 3, /* non-local exit hook, used by library only */
OBJ_CALLCC_1, /* used to pass the value in callcc1 */ OBJ_CALLCC_1, /* used to pass the value in callcc1 */
ERROR_HANDLER_QUOT = 5, /* quotation called when VM throws an error */ ERROR_HANDLER_QUOT = 5, /* quotation called when VM throws an error */
OBJ_ERROR, /* a marker consed onto kernel errors */ OBJ_ERROR, /* a marker consed onto kernel errors */
OBJ_CELL_SIZE = 7, /* sizeof(cell) */ OBJ_CELL_SIZE = 7, /* sizeof(cell) */
OBJ_CPU, /* CPU architecture */ OBJ_CPU, /* CPU architecture */
OBJ_OS, /* operating system name */ OBJ_OS, /* operating system name */
OBJ_ARGS = 10, /* command line arguments */ OBJ_ARGS = 10, /* command line arguments */
OBJ_STDIN, /* stdin FILE* handle */ OBJ_STDIN, /* stdin FILE* handle */
OBJ_STDOUT, /* stdout FILE* handle */ OBJ_STDOUT, /* stdout FILE* handle */
OBJ_IMAGE = 13, /* image path name */ OBJ_IMAGE = 13, /* image path name */
OBJ_EXECUTABLE, /* runtime executable path name */ OBJ_EXECUTABLE, /* runtime executable path name */
OBJ_EMBEDDED = 15, /* are we embedded in another app? */ OBJ_EMBEDDED = 15, /* are we embedded in another app? */
OBJ_EVAL_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_EVAL_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_YIELD_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_YIELD_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_SLEEP_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_SLEEP_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_STARTUP_QUOT = 20, /* startup quotation */ OBJ_STARTUP_QUOT = 20, /* startup quotation */
OBJ_GLOBAL, /* global namespace */ OBJ_GLOBAL, /* global namespace */
OBJ_SHUTDOWN_QUOT, /* shutdown quotation */ OBJ_SHUTDOWN_QUOT, /* shutdown quotation */
/* Quotation compilation in quotations.c */ /* Quotation compilation in quotations.c */
JIT_PROLOG = 23, JIT_PROLOG = 23,
JIT_PRIMITIVE_WORD, JIT_PRIMITIVE_WORD,
JIT_PRIMITIVE, JIT_PRIMITIVE,
JIT_WORD_JUMP, JIT_WORD_JUMP,
JIT_WORD_CALL, JIT_WORD_CALL,
JIT_IF_WORD, JIT_IF_WORD,
JIT_IF, JIT_IF,
JIT_SAFEPOINT, JIT_SAFEPOINT,
JIT_EPILOG, JIT_EPILOG,
JIT_RETURN, JIT_RETURN,
JIT_PROFILING, JIT_PROFILING,
JIT_PUSH_IMMEDIATE, JIT_PUSH_IMMEDIATE,
JIT_DIP_WORD, JIT_DIP_WORD,
JIT_DIP, JIT_DIP,
JIT_2DIP_WORD, JIT_2DIP_WORD,
JIT_2DIP, JIT_2DIP,
JIT_3DIP_WORD, JIT_3DIP_WORD,
JIT_3DIP, JIT_3DIP,
JIT_EXECUTE, JIT_EXECUTE,
JIT_DECLARE_WORD, JIT_DECLARE_WORD,
/* External entry points */ /* External entry points */
C_TO_FACTOR_WORD = 43, C_TO_FACTOR_WORD = 43,
LAZY_JIT_COMPILE_WORD, LAZY_JIT_COMPILE_WORD,
UNWIND_NATIVE_FRAMES_WORD, UNWIND_NATIVE_FRAMES_WORD,
GET_FPU_STATE_WORD, GET_FPU_STATE_WORD,
SET_FPU_STATE_WORD, SET_FPU_STATE_WORD,
SIGNAL_HANDLER_WORD, SIGNAL_HANDLER_WORD,
LEAF_SIGNAL_HANDLER_WORD, LEAF_SIGNAL_HANDLER_WORD,
FFI_SIGNAL_HANDLER_WORD, FFI_SIGNAL_HANDLER_WORD,
FFI_LEAF_SIGNAL_HANDLER_WORD, FFI_LEAF_SIGNAL_HANDLER_WORD,
/* Incremented on every modify-code-heap call; invalidates call( inline /* Incremented on every modify-code-heap call; invalidates call( inline
caching */ caching */
REDEFINITION_COUNTER = 52, REDEFINITION_COUNTER = 52,
/* Callback stub generation in callbacks.c */ /* Callback stub generation in callbacks.c */
CALLBACK_STUB = 53, CALLBACK_STUB = 53,
/* Polymorphic inline cache generation in inline_cache.c */ /* Polymorphic inline cache generation in inline_cache.c */
PIC_LOAD = 54, PIC_LOAD = 54,
PIC_TAG, PIC_TAG,
PIC_TUPLE, PIC_TUPLE,
PIC_CHECK_TAG, PIC_CHECK_TAG,
PIC_CHECK_TUPLE, PIC_CHECK_TUPLE,
PIC_HIT, PIC_HIT,
PIC_MISS_WORD, PIC_MISS_WORD,
PIC_MISS_TAIL_WORD, PIC_MISS_TAIL_WORD,
/* Megamorphic cache generation in dispatch.c */ /* Megamorphic cache generation in dispatch.c */
MEGA_LOOKUP = 62, MEGA_LOOKUP = 62,
MEGA_LOOKUP_WORD, MEGA_LOOKUP_WORD,
MEGA_MISS_WORD, MEGA_MISS_WORD,
OBJ_UNDEFINED = 65, /* default quotation for undefined words */ OBJ_UNDEFINED = 65, /* default quotation for undefined words */
OBJ_STDERR = 66, /* stderr FILE* handle */ OBJ_STDERR = 66, /* stderr FILE* handle */
OBJ_STAGE2 = 67, /* have we bootstrapped? */ OBJ_STAGE2 = 67, /* have we bootstrapped? */
OBJ_CURRENT_THREAD = 68, OBJ_CURRENT_THREAD = 68,
OBJ_THREADS = 69, OBJ_THREADS = 69,
OBJ_RUN_QUEUE = 70, OBJ_RUN_QUEUE = 70,
OBJ_SLEEP_QUEUE = 71, OBJ_SLEEP_QUEUE = 71,
OBJ_VM_COMPILER = 72, /* version string of the compiler we were built with */ OBJ_VM_COMPILER = 72, /* version string of the compiler we were built with */
OBJ_WAITING_CALLBACKS = 73, OBJ_WAITING_CALLBACKS = 73,
OBJ_SIGNAL_PIPE = 74, /* file descriptor for pipe used to communicate signals OBJ_SIGNAL_PIPE = 74, /* file descriptor for pipe used to communicate signals
only used on unix */ only used on unix */
}; };
/* save-image-and-exit discards special objects that are filled in on startup /* save-image-and-exit discards special objects that are filled in on startup
anyway, to reduce image size */ anyway, to reduce image size */
#define OBJ_FIRST_SAVE OBJ_STARTUP_QUOT #define OBJ_FIRST_SAVE OBJ_STARTUP_QUOT
#define OBJ_LAST_SAVE OBJ_STAGE2 #define OBJ_LAST_SAVE OBJ_STAGE2
inline static bool save_special_p(cell i) inline static bool save_special_p(cell i) {
{ return (i >= OBJ_FIRST_SAVE && i <= OBJ_LAST_SAVE);
return (i >= OBJ_FIRST_SAVE && i <= OBJ_LAST_SAVE);
} }
template<typename Iterator> void object::each_slot(Iterator &iter) template <typename Iterator> void object::each_slot(Iterator& iter) {
{ cell scan = (cell) this;
cell scan = (cell)this; cell payload_start = binary_payload_start();
cell payload_start = binary_payload_start(); cell end = scan + payload_start;
cell end = scan + payload_start;
scan += sizeof(cell); scan += sizeof(cell);
while(scan < end) while (scan < end) {
{ iter((cell*)scan);
iter((cell *)scan); scan += sizeof(cell);
scan += sizeof(cell); }
}
} }
} }