vm: non-optimizing compiler now compiles word definition quotations with the owner set to the word object

db4
Slava Pestov 2009-11-24 22:38:15 -06:00
parent 73e105bfc4
commit 4ebaf956c3
5 changed files with 52 additions and 30 deletions

View File

@ -99,14 +99,17 @@ cell factor_vm::frame_scan(stack_frame *frame)
{ {
case code_block_unoptimized: case code_block_unoptimized:
{ {
cell quot = frame_executing(frame); tagged<object> obj(frame_executing(frame));
if(to_boolean(quot)) if(obj.type_p(WORD_TYPE))
obj = obj.as<word>()->def;
if(obj.type_p(QUOTATION_TYPE))
{ {
char *return_addr = (char *)FRAME_RETURN_ADDRESS(frame,this); char *return_addr = (char *)FRAME_RETURN_ADDRESS(frame,this);
char *quot_xt = (char *)(frame_code(frame) + 1); char *quot_xt = (char *)(frame_code(frame) + 1);
return tag_fixnum(quot_code_offset_to_scan( return tag_fixnum(quot_code_offset_to_scan(
quot,(cell)(return_addr - quot_xt))); obj.value(),(cell)(return_addr - quot_xt)));
} }
else else
return false_object; return false_object;
@ -190,7 +193,7 @@ void factor_vm::primitive_set_innermost_stack_frame_quot()
callstack.untag_check(this); callstack.untag_check(this);
quot.untag_check(this); quot.untag_check(this);
jit_compile(quot.value(),true); jit_compile_quot(quot.value(),true);
stack_frame *inner = innermost_stack_frame_quot(callstack.untagged()); stack_frame *inner = innermost_stack_frame_quot(callstack.untagged());
cell offset = (char *)FRAME_RETURN_ADDRESS(inner,this) - (char *)inner->xt; cell offset = (char *)FRAME_RETURN_ADDRESS(inner,this) - (char *)inner->xt;

View File

@ -70,20 +70,6 @@ bool factor_vm::in_code_heap_p(cell ptr)
return (ptr >= code->seg->start && ptr <= code->seg->end); return (ptr >= code->seg->start && ptr <= code->seg->end);
} }
/* Compile a word definition with the non-optimizing compiler. Allocates memory */
void factor_vm::jit_compile_word(cell word_, cell def_, bool relocate)
{
data_root<word> word(word_,this);
data_root<quotation> def(def_,this);
jit_compile(def.value(),relocate);
word->code = def->code;
if(to_boolean(word->pic_def)) jit_compile(word->pic_def,relocate);
if(to_boolean(word->pic_tail_def)) jit_compile(word->pic_tail_def,relocate);
}
struct word_updater { struct word_updater {
factor_vm *parent; factor_vm *parent;

View File

@ -36,6 +36,11 @@ includes stack shufflers, some fixnum arithmetic words, and words such as tag,
slot and eq?. A primitive call is relatively expensive (two subroutine calls) slot and eq?. A primitive call is relatively expensive (two subroutine calls)
so this results in a big speedup for relatively little effort. */ so this results in a big speedup for relatively little effort. */
void quotation_jit::init_quotation(cell quot)
{
elements = untag<quotation>(quot)->array;
}
bool quotation_jit::primitive_call_p(cell i, cell length) bool quotation_jit::primitive_call_p(cell i, cell length)
{ {
return (i + 2) == length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_PRIMITIVE_WORD]; return (i + 2) == length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_PRIMITIVE_WORD];
@ -120,7 +125,7 @@ void quotation_jit::emit_quot(cell quot_)
literal(array_nth(elements,0)); literal(array_nth(elements,0));
else else
{ {
if(compiling) parent->jit_compile(quot.value(),relocate); if(compiling) parent->jit_compile_quot(quot.value(),relocate);
literal(quot.value()); literal(quot.value());
} }
} }
@ -288,23 +293,35 @@ void factor_vm::set_quot_xt(quotation *quot, code_block *code)
} }
/* Allocates memory */ /* Allocates memory */
void factor_vm::jit_compile(cell quot_, bool relocating) code_block *factor_vm::jit_compile_quot(cell owner_, cell quot_, bool relocating)
{ {
data_root<word> owner(owner_,this);
data_root<quotation> quot(quot_,this); data_root<quotation> quot(quot_,this);
if(quot->code) return;
quotation_jit compiler(quot.value(),true,relocating,this); quotation_jit compiler(owner.value(),true,relocating,this);
compiler.init_quotation(quot.value());
compiler.iterate_quotation(); compiler.iterate_quotation();
code_block *compiled = compiler.to_code_block(); code_block *compiled = compiler.to_code_block();
set_quot_xt(quot.untagged(),compiled);
if(relocating) relocate_code_block(compiled); if(relocating) relocate_code_block(compiled);
return compiled;
}
void factor_vm::jit_compile_quot(cell quot_, bool relocating)
{
data_root<quotation> quot(quot_,this);
if(quot->code) return;
code_block *compiled = jit_compile_quot(quot.value(),quot.value(),relocating);
set_quot_xt(quot.untagged(),compiled);
} }
void factor_vm::primitive_jit_compile() void factor_vm::primitive_jit_compile()
{ {
jit_compile(dpop(),true); jit_compile_quot(dpop(),true);
} }
/* push a new quotation on the stack */ /* push a new quotation on the stack */
@ -325,6 +342,19 @@ void factor_vm::primitive_quotation_xt()
drepl(allot_cell((cell)quot->xt)); drepl(allot_cell((cell)quot->xt));
} }
/* Compile a word definition with the non-optimizing compiler. Allocates memory */
void factor_vm::jit_compile_word(cell word_, cell def_, bool relocating)
{
data_root<word> word(word_,this);
data_root<quotation> def(def_,this);
code_block *compiled = jit_compile_quot(word.value(),def.value(),relocating);
word->code = compiled;
if(to_boolean(word->pic_def)) jit_compile_quot(word->pic_def,relocating);
if(to_boolean(word->pic_tail_def)) jit_compile_quot(word->pic_tail_def,relocating);
}
void factor_vm::compile_all_words() void factor_vm::compile_all_words()
{ {
data_root<array> words(find_all_words(),this); data_root<array> words(find_all_words(),this);
@ -350,6 +380,7 @@ fixnum factor_vm::quot_code_offset_to_scan(cell quot_, cell offset)
data_root<array> array(quot->array,this); data_root<array> array(quot->array,this);
quotation_jit compiler(quot.value(),false,false,this); quotation_jit compiler(quot.value(),false,false,this);
compiler.init_quotation(quot.value());
compiler.compute_position(offset); compiler.compute_position(offset);
compiler.iterate_quotation(); compiler.iterate_quotation();
@ -360,7 +391,7 @@ cell factor_vm::lazy_jit_compile_impl(cell quot_, stack_frame *stack)
{ {
data_root<quotation> quot(quot_,this); data_root<quotation> quot(quot_,this);
ctx->callstack_top = stack; ctx->callstack_top = stack;
jit_compile(quot.value(),true); jit_compile_quot(quot.value(),true);
return quot.value(); return quot.value();
} }

View File

@ -5,12 +5,13 @@ struct quotation_jit : public jit {
data_root<array> elements; data_root<array> elements;
bool compiling, relocate; bool compiling, relocate;
explicit quotation_jit(cell quot, bool compiling_, bool relocate_, factor_vm *vm) explicit quotation_jit(cell owner, bool compiling_, bool relocate_, factor_vm *vm)
: jit(code_block_unoptimized,quot,vm), : jit(code_block_unoptimized,owner,vm),
elements(owner.as<quotation>().untagged()->array,vm), elements(false_object,vm),
compiling(compiling_), compiling(compiling_),
relocate(relocate_){}; relocate(relocate_){};
void init_quotation(cell quot);
void emit_mega_cache_lookup(cell methods, fixnum index, cell cache); void emit_mega_cache_lookup(cell methods, fixnum index, cell cache);
bool primitive_call_p(cell i, cell length); bool primitive_call_p(cell i, cell length);
bool trivial_quotation_p(array *elements); bool trivial_quotation_p(array *elements);

View File

@ -544,7 +544,6 @@ struct factor_vm
void init_code_heap(cell size); void init_code_heap(cell size);
bool in_code_heap_p(cell ptr); bool in_code_heap_p(cell ptr);
void jit_compile_word(cell word_, cell def_, bool relocate);
void update_code_heap_words(); void update_code_heap_words();
void primitive_modify_code_heap(); void primitive_modify_code_heap();
code_heap_room code_room(); code_heap_room code_room();
@ -626,7 +625,9 @@ struct factor_vm
void primitive_array_to_quotation(); void primitive_array_to_quotation();
void primitive_quotation_xt(); void primitive_quotation_xt();
void set_quot_xt(quotation *quot, code_block *code); void set_quot_xt(quotation *quot, code_block *code);
void jit_compile(cell quot_, bool relocating); code_block *jit_compile_quot(cell owner_, cell quot_, bool relocating);
void jit_compile_quot(cell quot_, bool relocating);
void jit_compile_word(cell word_, cell def_, bool relocating);
void compile_all_words(); void compile_all_words();
fixnum quot_code_offset_to_scan(cell quot_, cell offset); fixnum quot_code_offset_to_scan(cell quot_, cell offset);
cell lazy_jit_compile_impl(cell quot_, stack_frame *stack); cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);