From 19835ab32e3b0773340a7a53ebbcc98a96c069e8 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 24 Nov 2009 22:38:15 -0600 Subject: [PATCH] vm: non-optimizing compiler now compiles word definition quotations with the owner set to the word object --- vm/callstack.cpp | 11 +++++++---- vm/code_heap.cpp | 14 -------------- vm/quotations.cpp | 45 ++++++++++++++++++++++++++++++++++++++------- vm/quotations.hpp | 7 ++++--- vm/vm.hpp | 5 +++-- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/vm/callstack.cpp b/vm/callstack.cpp index 85f392af0e..7165539a89 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -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 obj(frame_executing(frame)); + if(obj.type_p(WORD_TYPE)) + obj = obj.as()->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; @@ -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; diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index bad5a2fb67..d3a81cf70d 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -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_,this); - data_root 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; diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 8ccafc9d8f..2275b59833 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -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(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]; @@ -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()); } } @@ -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 owner(owner_,this); data_root 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 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 */ @@ -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_,this); + data_root 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 words(find_all_words(),this); @@ -350,6 +380,7 @@ fixnum factor_vm::quot_code_offset_to_scan(cell quot_, cell offset) data_root array(quot->array,this); quotation_jit compiler(quot.value(),false,false,this); + compiler.init_quotation(quot.value()); compiler.compute_position(offset); compiler.iterate_quotation(); @@ -360,7 +391,7 @@ cell factor_vm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) { data_root quot(quot_,this); ctx->callstack_top = stack; - jit_compile(quot.value(),true); + jit_compile_quot(quot.value(),true); return quot.value(); } diff --git a/vm/quotations.hpp b/vm/quotations.hpp index 6d04d80de3..0735b5a7b8 100755 --- a/vm/quotations.hpp +++ b/vm/quotations.hpp @@ -5,12 +5,13 @@ struct quotation_jit : public jit { data_root 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().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); diff --git a/vm/vm.hpp b/vm/vm.hpp index 82f8e39d67..986332eb1f 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -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(); @@ -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);