Skip to content

Commit

Permalink
vm: non-optimizing compiler now compiles word definition quotations w…
Browse files Browse the repository at this point in the history
…ith the owner set to the word object
  • Loading branch information
Slava Pestov committed Nov 25, 2009
1 parent 0521890 commit 19835ab
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
11 changes: 7 additions & 4 deletions vm/callstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ cell factor_vm::frame_scan(stack_frame *frame)
{
case code_block_unoptimized:
{
cell quot = frame_executing(frame);
if(to_boolean(quot))
tagged<object> obj(frame_executing(frame));
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 *quot_xt = (char *)(frame_code(frame) + 1);

return tag_fixnum(quot_code_offset_to_scan(
quot,(cell)(return_addr - quot_xt)));
obj.value(),(cell)(return_addr - quot_xt)));
}
else
return false_object;
Expand Down Expand Up @@ -190,7 +193,7 @@ void factor_vm::primitive_set_innermost_stack_frame_quot()
callstack.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());
cell offset = (char *)FRAME_RETURN_ADDRESS(inner,this) - (char *)inner->xt;
Expand Down
14 changes: 0 additions & 14 deletions vm/code_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ bool factor_vm::in_code_heap_p(cell ptr)
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 {
factor_vm *parent;

Expand Down
45 changes: 38 additions & 7 deletions vm/quotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
{
return (i + 2) == length && array_nth(elements.untagged(),i + 1) == parent->special_objects[JIT_PRIMITIVE_WORD];
Expand Down Expand Up @@ -120,7 +125,7 @@ void quotation_jit::emit_quot(cell quot_)
literal(array_nth(elements,0));
else
{
if(compiling) parent->jit_compile(quot.value(),relocate);
if(compiling) parent->jit_compile_quot(quot.value(),relocate);
literal(quot.value());
}
}
Expand Down Expand Up @@ -288,23 +293,35 @@ void factor_vm::set_quot_xt(quotation *quot, code_block *code)
}

/* 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);
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();

code_block *compiled = compiler.to_code_block();
set_quot_xt(quot.untagged(),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()
{
jit_compile(dpop(),true);
jit_compile_quot(dpop(),true);
}

/* push a new quotation on the stack */
Expand All @@ -325,6 +342,19 @@ void factor_vm::primitive_quotation_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()
{
data_root<array> words(find_all_words(),this);
Expand All @@ -350,6 +380,7 @@ fixnum factor_vm::quot_code_offset_to_scan(cell quot_, cell offset)
data_root<array> array(quot->array,this);

quotation_jit compiler(quot.value(),false,false,this);
compiler.init_quotation(quot.value());
compiler.compute_position(offset);
compiler.iterate_quotation();

Expand All @@ -360,7 +391,7 @@ cell factor_vm::lazy_jit_compile_impl(cell quot_, stack_frame *stack)
{
data_root<quotation> quot(quot_,this);
ctx->callstack_top = stack;
jit_compile(quot.value(),true);
jit_compile_quot(quot.value(),true);
return quot.value();
}

Expand Down
7 changes: 4 additions & 3 deletions vm/quotations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ struct quotation_jit : public jit {
data_root<array> elements;
bool compiling, relocate;

explicit quotation_jit(cell quot, bool compiling_, bool relocate_, factor_vm *vm)
: jit(code_block_unoptimized,quot,vm),
elements(owner.as<quotation>().untagged()->array,vm),
explicit quotation_jit(cell owner, bool compiling_, bool relocate_, factor_vm *vm)
: jit(code_block_unoptimized,owner,vm),
elements(false_object,vm),
compiling(compiling_),
relocate(relocate_){};

void init_quotation(cell quot);
void emit_mega_cache_lookup(cell methods, fixnum index, cell cache);
bool primitive_call_p(cell i, cell length);
bool trivial_quotation_p(array *elements);
Expand Down
5 changes: 3 additions & 2 deletions vm/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ struct factor_vm

void init_code_heap(cell size);
bool in_code_heap_p(cell ptr);
void jit_compile_word(cell word_, cell def_, bool relocate);
void update_code_heap_words();
void primitive_modify_code_heap();
code_heap_room code_room();
Expand Down Expand Up @@ -626,7 +625,9 @@ struct factor_vm
void primitive_array_to_quotation();
void primitive_quotation_xt();
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();
fixnum quot_code_offset_to_scan(cell quot_, cell offset);
cell lazy_jit_compile_impl(cell quot_, stack_frame *stack);
Expand Down

0 comments on commit 19835ab

Please sign in to comment.