added vm member to gc_root and growable arrays

db4
Phil Dawes 2009-08-17 21:37:09 +01:00
parent 54b3c1ea88
commit a2f14b5a6d
27 changed files with 119 additions and 116 deletions

View File

@ -32,8 +32,8 @@ char *pinned_alien_offset(cell obj)
/* make an alien */ /* make an alien */
cell factorvm::allot_alien(cell delegate_, cell displacement) cell factorvm::allot_alien(cell delegate_, cell displacement)
{ {
gc_root<object> delegate(delegate_); gc_root<object> delegate(delegate_,this);
gc_root<alien> new_alien(allot<alien>(sizeof(alien))); gc_root<alien> new_alien(allot<alien>(sizeof(alien)),this);
if(delegate.type_p(ALIEN_TYPE)) if(delegate.type_p(ALIEN_TYPE))
{ {
@ -138,9 +138,9 @@ DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset)
/* open a native library and push a handle */ /* open a native library and push a handle */
inline void factorvm::vmprim_dlopen() inline void factorvm::vmprim_dlopen()
{ {
gc_root<byte_array> path(dpop()); gc_root<byte_array> path(dpop(),this);
path.untag_check(); path.untag_check();
gc_root<dll> library(allot<dll>(sizeof(dll))); gc_root<dll> library(allot<dll>(sizeof(dll)),this);
library->path = path.value(); library->path = path.value();
ffi_dlopen(library.untagged()); ffi_dlopen(library.untagged());
dpush(library.value()); dpush(library.value());
@ -154,8 +154,8 @@ PRIMITIVE(dlopen)
/* look up a symbol in a native library */ /* look up a symbol in a native library */
inline void factorvm::vmprim_dlsym() inline void factorvm::vmprim_dlsym()
{ {
gc_root<object> library(dpop()); gc_root<object> library(dpop(),this);
gc_root<byte_array> name(dpop()); gc_root<byte_array> name(dpop(),this);
name.untag_check(); name.untag_check();
symbol_char *sym = name->data<symbol_char>(); symbol_char *sym = name->data<symbol_char>();

View File

@ -6,8 +6,8 @@ namespace factor
/* make a new array with an initial element */ /* make a new array with an initial element */
array *factorvm::allot_array(cell capacity, cell fill_) array *factorvm::allot_array(cell capacity, cell fill_)
{ {
gc_root<object> fill(fill_); gc_root<object> fill(fill_,this);
gc_root<array> new_array(allot_array_internal<array>(capacity)); gc_root<array> new_array(allot_array_internal<array>(capacity),this);
if(fill.value() == tag_fixnum(0)) if(fill.value() == tag_fixnum(0))
memset(new_array->data(),'\0',capacity * sizeof(cell)); memset(new_array->data(),'\0',capacity * sizeof(cell));
@ -43,8 +43,8 @@ PRIMITIVE(array)
cell factorvm::allot_array_1(cell obj_) cell factorvm::allot_array_1(cell obj_)
{ {
gc_root<object> obj(obj_); gc_root<object> obj(obj_,this);
gc_root<array> a(allot_array_internal<array>(1)); gc_root<array> a(allot_array_internal<array>(1),this);
set_array_nth(a.untagged(),0,obj.value()); set_array_nth(a.untagged(),0,obj.value());
return a.value(); return a.value();
} }
@ -56,9 +56,9 @@ cell allot_array_1(cell obj_)
cell factorvm::allot_array_2(cell v1_, cell v2_) cell factorvm::allot_array_2(cell v1_, cell v2_)
{ {
gc_root<object> v1(v1_); gc_root<object> v1(v1_,this);
gc_root<object> v2(v2_); gc_root<object> v2(v2_,this);
gc_root<array> a(allot_array_internal<array>(2)); gc_root<array> a(allot_array_internal<array>(2),this);
set_array_nth(a.untagged(),0,v1.value()); set_array_nth(a.untagged(),0,v1.value());
set_array_nth(a.untagged(),1,v2.value()); set_array_nth(a.untagged(),1,v2.value());
return a.value(); return a.value();
@ -71,11 +71,11 @@ cell allot_array_2(cell v1_, cell v2_)
cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_)
{ {
gc_root<object> v1(v1_); gc_root<object> v1(v1_,this);
gc_root<object> v2(v2_); gc_root<object> v2(v2_,this);
gc_root<object> v3(v3_); gc_root<object> v3(v3_,this);
gc_root<object> v4(v4_); gc_root<object> v4(v4_,this);
gc_root<array> a(allot_array_internal<array>(4)); gc_root<array> a(allot_array_internal<array>(4),this);
set_array_nth(a.untagged(),0,v1.value()); set_array_nth(a.untagged(),0,v1.value());
set_array_nth(a.untagged(),1,v2.value()); set_array_nth(a.untagged(),1,v2.value());
set_array_nth(a.untagged(),2,v3.value()); set_array_nth(a.untagged(),2,v3.value());
@ -102,7 +102,7 @@ PRIMITIVE(resize_array)
void growable_array::add(cell elt_) void growable_array::add(cell elt_)
{ {
gc_root<object> elt(elt_); gc_root<object> elt(elt_,elements.myvm);
if(count == array_capacity(elements.untagged())) if(count == array_capacity(elements.untagged()))
elements = reallot_array(elements.untagged(),count * 2); elements = reallot_array(elements.untagged(),count * 2);

3
vm/arrays.hpp Normal file → Executable file
View File

@ -1,6 +1,5 @@
namespace factor namespace factor
{ {
inline static cell array_nth(array *array, cell slot) inline static cell array_nth(array *array, cell slot)
{ {
#ifdef FACTOR_DEBUG #ifdef FACTOR_DEBUG
@ -34,7 +33,7 @@ struct growable_array {
cell count; cell count;
gc_root<array> elements; gc_root<array> elements;
growable_array(cell capacity = 10) : count(0), elements(allot_array(capacity,F)) {} growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {}
void add(cell elt); void add(cell elt);
void trim(); void trim();

View File

@ -63,7 +63,7 @@ void growable_byte_array::append_bytes(void *elts, cell len)
void growable_byte_array::append_byte_array(cell byte_array_) void growable_byte_array::append_byte_array(cell byte_array_)
{ {
gc_root<byte_array> byte_array(byte_array_); gc_root<byte_array> byte_array(byte_array_,elements.myvm);
cell len = array_capacity(byte_array.untagged()); cell len = array_capacity(byte_array.untagged());
cell new_size = count + len; cell new_size = count + len;

2
vm/byte_arrays.hpp Normal file → Executable file
View File

@ -11,7 +11,7 @@ struct growable_byte_array {
cell count; cell count;
gc_root<byte_array> elements; gc_root<byte_array> elements;
growable_byte_array(cell capacity = 40) : count(0), elements(allot_byte_array(capacity)) { } growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { }
void append_bytes(void *elts, cell len); void append_bytes(void *elts, cell len);
void append_byte_array(cell elts); void append_byte_array(cell elts);

View File

@ -192,10 +192,12 @@ namespace
struct stack_frame_accumulator { struct stack_frame_accumulator {
growable_array frames; growable_array frames;
stack_frame_accumulator(factorvm *vm) : frames(vm) {}
void operator()(stack_frame *frame) void operator()(stack_frame *frame)
{ {
gc_root<object> executing(frame_executing(frame)); gc_root<object> executing(frame_executing(frame),frames.elements.myvm);
gc_root<object> scan(frame_scan(frame)); gc_root<object> scan(frame_scan(frame),frames.elements.myvm);
frames.add(executing.value()); frames.add(executing.value());
frames.add(scan.value()); frames.add(scan.value());
@ -206,9 +208,9 @@ struct stack_frame_accumulator {
inline void factorvm::vmprim_callstack_to_array() inline void factorvm::vmprim_callstack_to_array()
{ {
gc_root<callstack> callstack(dpop()); gc_root<callstack> callstack(dpop(),this);
stack_frame_accumulator accum; stack_frame_accumulator accum(this);
iterate_callstack_object(callstack.untagged(),accum); iterate_callstack_object(callstack.untagged(),accum);
accum.frames.trim(); accum.frames.trim();
@ -273,8 +275,8 @@ PRIMITIVE(innermost_stack_frame_scan)
inline void factorvm::vmprim_set_innermost_stack_frame_quot() inline void factorvm::vmprim_set_innermost_stack_frame_quot()
{ {
gc_root<callstack> callstack(dpop()); gc_root<callstack> callstack(dpop(),this);
gc_root<quotation> quot(dpop()); gc_root<quotation> quot(dpop(),this);
callstack.untag_check(); callstack.untag_check();
quot.untag_check(); quot.untag_check();

2
vm/callstack.hpp Normal file → Executable file
View File

@ -37,7 +37,7 @@ template<typename T> void iterate_callstack(cell top, cell bottom, T &iterator)
keep the callstack in a GC root and use relative offsets */ keep the callstack in a GC root and use relative offsets */
template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator) template<typename T> void iterate_callstack_object(callstack *stack_, T &iterator)
{ {
gc_root<callstack> stack(stack_); gc_root<callstack> stack(stack_,vm);
fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame);
while(frame_offset >= 0) while(frame_offset >= 0)

View File

@ -638,10 +638,10 @@ code_block *allot_code_block(cell size)
/* Might GC */ /* Might GC */
code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_)
{ {
gc_root<byte_array> code(code_); gc_root<byte_array> code(code_,this);
gc_root<object> labels(labels_); gc_root<object> labels(labels_,this);
gc_root<byte_array> relocation(relocation_); gc_root<byte_array> relocation(relocation_,this);
gc_root<array> literals(literals_); gc_root<array> literals(literals_,this);
cell code_length = align8(array_capacity(code.untagged())); cell code_length = align8(array_capacity(code.untagged()));
code_block *compiled = allot_code_block(code_length); code_block *compiled = allot_code_block(code_length);

View File

@ -29,8 +29,8 @@ bool in_code_heap_p(cell ptr)
/* Compile a word definition with the non-optimizing compiler. Allocates memory */ /* Compile a word definition with the non-optimizing compiler. Allocates memory */
void factorvm::jit_compile_word(cell word_, cell def_, bool relocate) void factorvm::jit_compile_word(cell word_, cell def_, bool relocate)
{ {
gc_root<word> word(word_); gc_root<word> word(word_,this);
gc_root<quotation> def(def_); gc_root<quotation> def(def_,this);
jit_compile(def.value(),relocate); jit_compile(def.value(),relocate);
@ -89,7 +89,7 @@ void update_code_heap_words()
inline void factorvm::vmprim_modify_code_heap() inline void factorvm::vmprim_modify_code_heap()
{ {
gc_root<array> alist(dpop()); gc_root<array> alist(dpop(),this);
cell count = array_capacity(alist.untagged()); cell count = array_capacity(alist.untagged());
@ -99,10 +99,10 @@ inline void factorvm::vmprim_modify_code_heap()
cell i; cell i;
for(i = 0; i < count; i++) for(i = 0; i < count; i++)
{ {
gc_root<array> pair(array_nth(alist.untagged(),i)); gc_root<array> pair(array_nth(alist.untagged(),i),this);
gc_root<word> word(array_nth(pair.untagged(),0)); gc_root<word> word(array_nth(pair.untagged(),0),this);
gc_root<object> data(array_nth(pair.untagged(),1)); gc_root<object> data(array_nth(pair.untagged(),1),this);
switch(data.type()) switch(data.type())
{ {

View File

@ -730,7 +730,7 @@ PRIMITIVE(gc)
inline void factorvm::vmprim_gc_stats() inline void factorvm::vmprim_gc_stats()
{ {
growable_array result; growable_array result(this);
cell i; cell i;
u64 total_gc_time = 0; u64 total_gc_time = 0;

View File

@ -358,7 +358,7 @@ inline void factorvm::vmprim_data_room()
dpush(tag_fixnum((data->cards_end - data->cards) >> 10)); dpush(tag_fixnum((data->cards_end - data->cards) >> 10));
dpush(tag_fixnum((data->decks_end - data->decks) >> 10)); dpush(tag_fixnum((data->decks_end - data->decks) >> 10));
growable_array a; growable_array a(this);
cell gen; cell gen;
for(gen = 0; gen < data->gen_count; gen++) for(gen = 0; gen < data->gen_count; gen++)
@ -478,7 +478,7 @@ struct word_counter {
struct word_accumulator { struct word_accumulator {
growable_array words; growable_array words;
word_accumulator(int count) : words(count) {} word_accumulator(int count,factorvm *vm) : words(vm,count) {}
void operator()(tagged<object> obj) { if(obj.type_p(WORD_TYPE)) words.add(obj.value()); } void operator()(tagged<object> obj) { if(obj.type_p(WORD_TYPE)) words.add(obj.value()); }
}; };
@ -488,7 +488,7 @@ cell factorvm::find_all_words()
{ {
word_counter counter; word_counter counter;
each_object(counter); each_object(counter);
word_accumulator accum(counter.count); word_accumulator accum(counter.count,this);
each_object(accum); each_object(accum);
accum.words.trim(); accum.words.trim();
return accum.words.elements.value(); return accum.words.elements.value();

View File

@ -244,7 +244,7 @@ PRIMITIVE(reset_dispatch_stats)
inline void factorvm::vmprim_dispatch_stats() inline void factorvm::vmprim_dispatch_stats()
{ {
growable_array stats; growable_array stats(this);
stats.add(allot_cell(megamorphic_cache_hits)); stats.add(allot_cell(megamorphic_cache_hits));
stats.add(allot_cell(megamorphic_cache_misses)); stats.add(allot_cell(megamorphic_cache_misses));
stats.trim(); stats.trim();
@ -258,8 +258,8 @@ PRIMITIVE(dispatch_stats)
void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_) void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
{ {
gc_root<array> methods(methods_); gc_root<array> methods(methods_,myvm);
gc_root<array> cache(cache_); gc_root<array> cache(cache_,myvm);
/* Generate machine code to determine the object's class. */ /* Generate machine code to determine the object's class. */
emit_class_lookup(index,PIC_HI_TAG_TUPLE); emit_class_lookup(index,PIC_HI_TAG_TUPLE);

View File

@ -179,7 +179,7 @@ VM_C_API void init_factor(vm_parameters *p)
/* May allocate memory */ /* May allocate memory */
void factorvm::pass_args_to_factor(int argc, vm_char **argv) void factorvm::pass_args_to_factor(int argc, vm_char **argv)
{ {
growable_array args; growable_array args(this);
int i; int i;
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++)

View File

@ -33,7 +33,7 @@ template <typename T> bool reallot_array_in_place_p(T *array, cell capacity)
template <typename T> T *reallot_array(T *array_, cell capacity) template <typename T> T *reallot_array(T *array_, cell capacity)
{ {
gc_root<T> array(array_); gc_root<T> array(array_,vm);
if(reallot_array_in_place_p(array.untagged(),capacity)) if(reallot_array_in_place_p(array.untagged(),capacity))
{ {

4
vm/image.cpp Normal file → Executable file
View File

@ -147,7 +147,7 @@ inline void factorvm::vmprim_save_image()
/* do a full GC to push everything into tenured space */ /* do a full GC to push everything into tenured space */
gc(); gc();
gc_root<byte_array> path(dpop()); gc_root<byte_array> path(dpop(),this);
path.untag_check(); path.untag_check();
save_image((vm_char *)(path.untagged() + 1)); save_image((vm_char *)(path.untagged() + 1));
} }
@ -162,7 +162,7 @@ inline void factorvm::vmprim_save_image_and_exit()
/* We unbox this before doing anything else. This is the only point /* We unbox this before doing anything else. This is the only point
where we might throw an error, so we have to throw an error here since where we might throw an error, so we have to throw an error here since
later steps destroy the current image. */ later steps destroy the current image. */
gc_root<byte_array> path(dpop()); gc_root<byte_array> path(dpop(),this);
path.untag_check(); path.untag_check();
/* strip out userenv data which is set on startup anyway */ /* strip out userenv data which is set on startup anyway */

View File

@ -132,9 +132,9 @@ void inline_cache_jit::compile_inline_cache(fixnum index,
cell cache_entries_, cell cache_entries_,
bool tail_call_p) bool tail_call_p)
{ {
gc_root<word> generic_word(generic_word_); gc_root<word> generic_word(generic_word_,myvm);
gc_root<array> methods(methods_); gc_root<array> methods(methods_,myvm);
gc_root<array> cache_entries(cache_entries_); gc_root<array> cache_entries(cache_entries_,myvm);
cell inline_cache_type = determine_inline_cache_type(cache_entries.untagged()); cell inline_cache_type = determine_inline_cache_type(cache_entries.untagged());
update_pic_count(inline_cache_type); update_pic_count(inline_cache_type);
@ -169,9 +169,9 @@ void inline_cache_jit::compile_inline_cache(fixnum index,
code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p)
{ {
gc_root<word> generic_word(generic_word_); gc_root<word> generic_word(generic_word_,this);
gc_root<array> methods(methods_); gc_root<array> methods(methods_,this);
gc_root<array> cache_entries(cache_entries_); gc_root<array> cache_entries(cache_entries_,this);
inline_cache_jit jit(generic_word.value(),this); inline_cache_jit jit(generic_word.value(),this);
jit.compile_inline_cache(index, jit.compile_inline_cache(index,
@ -213,12 +213,12 @@ cell inline_cache_size(cell cache_entries)
/* Allocates memory */ /* Allocates memory */
cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_)
{ {
gc_root<array> cache_entries(cache_entries_); gc_root<array> cache_entries(cache_entries_,this);
gc_root<object> klass(klass_); gc_root<object> klass(klass_,this);
gc_root<word> method(method_); gc_root<word> method(method_,this);
cell pic_size = array_capacity(cache_entries.untagged()); cell pic_size = array_capacity(cache_entries.untagged());
gc_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2)); gc_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2),this);
set_array_nth(new_cache_entries.untagged(),pic_size,klass.value()); set_array_nth(new_cache_entries.untagged(),pic_size,klass.value());
set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value()); set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value());
return new_cache_entries.value(); return new_cache_entries.value();
@ -255,11 +255,11 @@ void *factorvm::inline_cache_miss(cell return_address)
instead of leaving dead PICs around until the next GC. */ instead of leaving dead PICs around until the next GC. */
deallocate_inline_cache(return_address); deallocate_inline_cache(return_address);
gc_root<array> cache_entries(dpop()); gc_root<array> cache_entries(dpop(),this);
fixnum index = untag_fixnum(dpop()); fixnum index = untag_fixnum(dpop());
gc_root<array> methods(dpop()); gc_root<array> methods(dpop(),this);
gc_root<word> generic_word(dpop()); gc_root<word> generic_word(dpop(),this);
gc_root<object> object(((cell *)ds)[-index]); gc_root<object> object(((cell *)ds)[-index],this);
void *xt; void *xt;
@ -277,7 +277,7 @@ void *factorvm::inline_cache_miss(cell return_address)
gc_root<array> new_cache_entries(add_inline_cache_entry( gc_root<array> new_cache_entries(add_inline_cache_entry(
cache_entries.value(), cache_entries.value(),
klass, klass,
method)); method),this);
xt = compile_inline_cache(index, xt = compile_inline_cache(index,
generic_word.value(), generic_word.value(),
methods.value(), methods.value(),
@ -317,7 +317,7 @@ PRIMITIVE(reset_inline_cache_stats)
inline void factorvm::vmprim_inline_cache_stats() inline void factorvm::vmprim_inline_cache_stats()
{ {
growable_array stats; growable_array stats(this);
stats.add(allot_cell(cold_call_to_ic_transitions)); stats.add(allot_cell(cold_call_to_ic_transitions));
stats.add(allot_cell(ic_to_pic_transitions)); stats.add(allot_cell(ic_to_pic_transitions));
stats.add(allot_cell(pic_to_mega_transitions)); stats.add(allot_cell(pic_to_mega_transitions));

View File

@ -43,8 +43,8 @@ void io_error()
inline void factorvm::vmprim_fopen() inline void factorvm::vmprim_fopen()
{ {
gc_root<byte_array> mode(dpop()); gc_root<byte_array> mode(dpop(),this);
gc_root<byte_array> path(dpop()); gc_root<byte_array> path(dpop(),this);
mode.untag_check(); mode.untag_check();
path.untag_check(); path.untag_check();
@ -108,7 +108,7 @@ inline void factorvm::vmprim_fread()
return; return;
} }
gc_root<byte_array> buf(allot_array_internal<byte_array>(size)); gc_root<byte_array> buf(allot_array_internal<byte_array>(size),this);
for(;;) for(;;)
{ {

View File

@ -12,10 +12,10 @@ namespace factor
/* Allocates memory */ /* Allocates memory */
jit::jit(cell type_, cell owner_, factorvm *vm) jit::jit(cell type_, cell owner_, factorvm *vm)
: type(type_), : type(type_),
owner(owner_), owner(owner_,vm),
code(), code(vm),
relocation(), relocation(vm),
literals(), literals(vm),
computing_offset_p(false), computing_offset_p(false),
position(0), position(0),
offset(0), offset(0),
@ -26,7 +26,7 @@ jit::jit(cell type_, cell owner_, factorvm *vm)
void jit::emit_relocation(cell code_template_) void jit::emit_relocation(cell code_template_)
{ {
gc_root<array> code_template(code_template_); gc_root<array> code_template(code_template_,myvm);
cell capacity = array_capacity(code_template.untagged()); cell capacity = array_capacity(code_template.untagged());
for(cell i = 1; i < capacity; i += 3) for(cell i = 1; i < capacity; i += 3)
{ {
@ -45,11 +45,11 @@ void jit::emit_relocation(cell code_template_)
/* Allocates memory */ /* Allocates memory */
void jit::emit(cell code_template_) void jit::emit(cell code_template_)
{ {
gc_root<array> code_template(code_template_); gc_root<array> code_template(code_template_,myvm);
emit_relocation(code_template.value()); emit_relocation(code_template.value());
gc_root<byte_array> insns(array_nth(code_template.untagged(),0)); gc_root<byte_array> insns(array_nth(code_template.untagged(),0),myvm);
if(computing_offset_p) if(computing_offset_p)
{ {
@ -73,8 +73,8 @@ void jit::emit(cell code_template_)
} }
void jit::emit_with(cell code_template_, cell argument_) { void jit::emit_with(cell code_template_, cell argument_) {
gc_root<array> code_template(code_template_); gc_root<array> code_template(code_template_,myvm);
gc_root<object> argument(argument_); gc_root<object> argument(argument_,myvm);
literal(argument.value()); literal(argument.value());
emit(code_template.value()); emit(code_template.value());
} }

View File

@ -40,8 +40,8 @@ struct jit {
} }
void emit_subprimitive(cell word_) { void emit_subprimitive(cell word_) {
gc_root<word> word(word_); gc_root<word> word(word_,myvm);
gc_root<array> code_template(word->subprimitive); gc_root<array> code_template(word->subprimitive,myvm);
if(array_capacity(code_template.untagged()) > 1) literal(T); if(array_capacity(code_template.untagged()) > 1) literal(T);
emit(code_template.value()); emit(code_template.value());
} }

View File

@ -3,17 +3,19 @@ namespace factor
struct factorvm; struct factorvm;
template <typename T> template <typename TYPE>
struct gc_root : public tagged<T> struct gc_root : public tagged<TYPE>
{ {
void push() { check_tagged_pointer(tagged<T>::value()); vm->gc_locals.push_back((cell)this); } factorvm *myvm;
//explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged<T>(value_) { push(); } void push() { check_tagged_pointer(tagged<TYPE>::value()); myvm->gc_locals.push_back((cell)this); }
explicit gc_root(cell value_) : tagged<T>(value_) { push(); }
explicit gc_root(T *value_) : tagged<T>(value_) { push(); }
const gc_root<T>& operator=(const T *x) { tagged<T>::operator=(x); return *this; } //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged<TYPE>(value_) { push(); }
const gc_root<T>& operator=(const cell &x) { tagged<T>::operator=(x); return *this; } explicit gc_root(cell value_,factorvm *vm) : tagged<TYPE>(value_),myvm(vm) { push(); }
explicit gc_root(TYPE *value_, factorvm *vm) : tagged<TYPE>(value_),myvm(vm) { push(); }
const gc_root<TYPE>& operator=(const TYPE *x) { tagged<TYPE>::operator=(x); return *this; }
const gc_root<TYPE>& operator=(const cell &x) { tagged<TYPE>::operator=(x); return *this; }
~gc_root() { ~gc_root() {
#ifdef FACTOR_DEBUG #ifdef FACTOR_DEBUG

View File

@ -18,7 +18,7 @@ void init_profiler()
/* Allocates memory */ /* Allocates memory */
code_block *factorvm::compile_profiling_stub(cell word_) code_block *factorvm::compile_profiling_stub(cell word_)
{ {
gc_root<word> word(word_); gc_root<word> word(word_,this);
jit jit(WORD_TYPE,word.value(),this); jit jit(WORD_TYPE,word.value(),this);
jit.emit_with(userenv[JIT_PROFILING],word.value()); jit.emit_with(userenv[JIT_PROFILING],word.value());
@ -43,7 +43,7 @@ void factorvm::set_profiling(bool profiling)
and allocate profiling blocks if necessary */ and allocate profiling blocks if necessary */
gc(); gc();
gc_root<array> words(find_all_words()); gc_root<array> words(find_all_words(),this);
cell i; cell i;
cell length = array_capacity(words.untagged()); cell length = array_capacity(words.untagged());

View File

@ -125,7 +125,7 @@ void quotation_jit::iterate_quotation()
{ {
set_position(i); set_position(i);
gc_root<object> obj(array_nth(elements.untagged(),i)); gc_root<object> obj(array_nth(elements.untagged(),i),myvm);
switch(obj.type()) switch(obj.type())
{ {
@ -282,7 +282,7 @@ void set_quot_xt(quotation *quot, code_block *code)
/* Allocates memory */ /* Allocates memory */
void factorvm::jit_compile(cell quot_, bool relocating) void factorvm::jit_compile(cell quot_, bool relocating)
{ {
gc_root<quotation> quot(quot_); gc_root<quotation> quot(quot_,this);
if(quot->code) return; if(quot->code) return;
quotation_jit compiler(quot.value(),true,relocating,this); quotation_jit compiler(quot.value(),true,relocating,this);
@ -339,13 +339,13 @@ PRIMITIVE(quotation_xt)
void factorvm::compile_all_words() void factorvm::compile_all_words()
{ {
gc_root<array> words(find_all_words()); gc_root<array> words(find_all_words(),this);
cell i; cell i;
cell length = array_capacity(words.untagged()); cell length = array_capacity(words.untagged());
for(i = 0; i < length; i++) for(i = 0; i < length; i++)
{ {
gc_root<word> word(array_nth(words.untagged(),i)); gc_root<word> word(array_nth(words.untagged(),i),this);
if(!word->code || !word_optimized_p(word.untagged())) if(!word->code || !word_optimized_p(word.untagged()))
jit_compile_word(word.value(),word->def,false); jit_compile_word(word.value(),word->def,false);
@ -365,8 +365,8 @@ void compile_all_words()
/* Allocates memory */ /* Allocates memory */
fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset)
{ {
gc_root<quotation> quot(quot_); gc_root<quotation> quot(quot_,this);
gc_root<array> array(quot->array); gc_root<array> array(quot->array,this);
quotation_jit compiler(quot.value(),false,false,this); quotation_jit compiler(quot.value(),false,false,this);
compiler.compute_position(offset); compiler.compute_position(offset);
@ -382,7 +382,7 @@ fixnum quot_code_offset_to_scan(cell quot_, cell offset)
cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack)
{ {
gc_root<quotation> quot(quot_); gc_root<quotation> quot(quot_,this);
stack_chain->callstack_top = stack; stack_chain->callstack_top = stack;
jit_compile(quot.value(),true); jit_compile(quot.value(),true);
return quot.value(); return quot.value();

View File

@ -7,7 +7,7 @@ struct quotation_jit : public jit {
quotation_jit(cell quot, bool compiling_, bool relocate_, factorvm *vm) quotation_jit(cell quot, bool compiling_, bool relocate_, factorvm *vm)
: jit(QUOTATION_TYPE,quot,vm), : jit(QUOTATION_TYPE,quot,vm),
elements(owner.as<quotation>().untagged()->array), elements(owner.as<quotation>().untagged()->array,vm),
compiling(compiling_), compiling(compiling_),
relocate(relocate_){}; relocate(relocate_){};

View File

@ -90,7 +90,7 @@ PRIMITIVE(load_locals)
cell factorvm::clone_object(cell obj_) cell factorvm::clone_object(cell obj_)
{ {
gc_root<object> obj(obj_); gc_root<object> obj(obj_,this);
if(immediate_p(obj.value())) if(immediate_p(obj.value()))
return obj.value(); return obj.value();

View File

@ -39,7 +39,7 @@ void set_string_nth_fast(string *str, cell index, cell ch)
void factorvm::set_string_nth_slow(string *str_, cell index, cell ch) void factorvm::set_string_nth_slow(string *str_, cell index, cell ch)
{ {
gc_root<string> str(str_); gc_root<string> str(str_,this);
byte_array *aux; byte_array *aux;
@ -103,7 +103,7 @@ string *allot_string_internal(cell capacity)
/* Allocates memory */ /* Allocates memory */
void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill) void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill)
{ {
gc_root<string> str(str_); gc_root<string> str(str_,this);
if(fill <= 0x7f) if(fill <= 0x7f)
memset(&str->data()[start],fill,capacity - start); memset(&str->data()[start],fill,capacity - start);
@ -124,7 +124,7 @@ void fill_string(string *str_, cell start, cell capacity, cell fill)
/* Allocates memory */ /* Allocates memory */
string *factorvm::allot_string(cell capacity, cell fill) string *factorvm::allot_string(cell capacity, cell fill)
{ {
gc_root<string> str(allot_string_internal(capacity)); gc_root<string> str(allot_string_internal(capacity),this);
fill_string(str.untagged(),0,capacity,fill); fill_string(str.untagged(),0,capacity,fill);
return str.untagged(); return str.untagged();
} }
@ -160,7 +160,7 @@ bool reallot_string_in_place_p(string *str, cell capacity)
string* factorvm::reallot_string(string *str_, cell capacity) string* factorvm::reallot_string(string *str_, cell capacity)
{ {
gc_root<string> str(str_); gc_root<string> str(str_,this);
if(reallot_string_in_place_p(str.untagged(),capacity)) if(reallot_string_in_place_p(str.untagged(),capacity))
{ {
@ -180,7 +180,7 @@ string* factorvm::reallot_string(string *str_, cell capacity)
if(capacity < to_copy) if(capacity < to_copy)
to_copy = capacity; to_copy = capacity;
gc_root<string> new_str(allot_string_internal(capacity)); gc_root<string> new_str(allot_string_internal(capacity),this);
memcpy(new_str->data(),str->data(),to_copy); memcpy(new_str->data(),str->data(),to_copy);

View File

@ -6,8 +6,8 @@ namespace factor
/* push a new tuple on the stack */ /* push a new tuple on the stack */
tuple *factorvm::allot_tuple(cell layout_) tuple *factorvm::allot_tuple(cell layout_)
{ {
gc_root<tuple_layout> layout(layout_); gc_root<tuple_layout> layout(layout_,this);
gc_root<tuple> t(allot<tuple>(tuple_size(layout.untagged()))); gc_root<tuple> t(allot<tuple>(tuple_size(layout.untagged())),this);
t->layout = layout.value(); t->layout = layout.value();
return t.untagged(); return t.untagged();
} }
@ -19,7 +19,7 @@ tuple *allot_tuple(cell layout_)
inline void factorvm::vmprim_tuple() inline void factorvm::vmprim_tuple()
{ {
gc_root<tuple_layout> layout(dpop()); gc_root<tuple_layout> layout(dpop(),this);
tuple *t = allot_tuple(layout.value()); tuple *t = allot_tuple(layout.value());
fixnum i; fixnum i;
for(i = tuple_size(layout.untagged()) - 1; i >= 0; i--) for(i = tuple_size(layout.untagged()) - 1; i >= 0; i--)
@ -36,8 +36,8 @@ PRIMITIVE(tuple)
/* push a new tuple on the stack, filling its slots from the stack */ /* push a new tuple on the stack, filling its slots from the stack */
inline void factorvm::vmprim_tuple_boa() inline void factorvm::vmprim_tuple_boa()
{ {
gc_root<tuple_layout> layout(dpop()); gc_root<tuple_layout> layout(dpop(),this);
gc_root<tuple> t(allot_tuple(layout.value())); gc_root<tuple> t(allot_tuple(layout.value()),this);
cell size = untag_fixnum(layout.untagged()->size) * sizeof(cell); cell size = untag_fixnum(layout.untagged()->size) * sizeof(cell);
memcpy(t->data(),(cell *)(ds - (size - sizeof(cell))),size); memcpy(t->data(),(cell *)(ds - (size - sizeof(cell))),size);
ds -= size; ds -= size;

View File

@ -5,10 +5,10 @@ namespace factor
word *factorvm::allot_word(cell vocab_, cell name_) word *factorvm::allot_word(cell vocab_, cell name_)
{ {
gc_root<object> vocab(vocab_); gc_root<object> vocab(vocab_,this);
gc_root<object> name(name_); gc_root<object> name(name_,this);
gc_root<word> new_word(allot<word>(sizeof(word))); gc_root<word> new_word(allot<word>(sizeof(word)),this);
new_word->hashcode = tag_fixnum((rand() << 16) ^ rand()); new_word->hashcode = tag_fixnum((rand() << 16) ^ rand());
new_word->vocabulary = vocab.value(); new_word->vocabulary = vocab.value();
@ -66,7 +66,7 @@ PRIMITIVE(word_xt)
/* Allocates memory */ /* Allocates memory */
void factorvm::update_word_xt(cell w_) void factorvm::update_word_xt(cell w_)
{ {
gc_root<word> w(w_); gc_root<word> w(w_,this);
if(profiling_p) if(profiling_p)
{ {