From 0c6865a10b5c149fc29d3c0e1a2516e8f3fe6453 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 25 Sep 2007 21:23:20 -0400 Subject: [PATCH] Lazy JIT --- vm/cpu-ppc.S | 27 +++++++++++++++++++++------ vm/cpu-ppc.h | 1 + vm/data_gc.c | 2 +- vm/image.c | 7 +++---- vm/jit.c | 15 --------------- vm/jit.h | 4 ++-- vm/types.c | 7 +------ 7 files changed, 29 insertions(+), 34 deletions(-) diff --git a/vm/cpu-ppc.S b/vm/cpu-ppc.S index 295e16f04c..b1f8298eb0 100644 --- a/vm/cpu-ppc.S +++ b/vm/cpu-ppc.S @@ -40,11 +40,19 @@ in the public domain. */ #define RESTORE(register,offset) lwz register,SAVE_AT(offset)(r1) -DEF(void,c_to_factor,(CELL quot)): - mflr r0 /* get caller's return address */ - stwu r1,-FRAME(r1) /* create a stack frame to hold non-volatile registers */ +#define PROLOGUE \ + mflr r0 ; /* get caller's return address */ \ + stwu r1,-FRAME(r1) ; /* create a stack frame to hold non-volatile registers */ SAVE_LR(r0) +#define EPILOGUE \ + LOAD_LR(r0) ; \ + lwz r1,0(r1) ; /* destroy the stack frame */ \ + mtlr r0 /* get ready to return */ \ + +DEF(void,c_to_factor,(CELL quot)): + PROLOGUE + SAVE(r13,0) /* save GPRs */ /* don't save ds pointer */ /* don't save rs pointer */ @@ -92,9 +100,7 @@ DEF(void,c_to_factor,(CELL quot)): /* don't restore ds pointer */ RESTORE(r13,0) - LOAD_LR(r0) - lwz r1,0(r1) /* destroy the stack frame */ - mtlr r0 /* get ready to return */ + EPILOGUE blr /* The JIT compiles an 'mr r4,r1' in front of every primitive call, since a @@ -164,6 +170,15 @@ DEF(void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): mtlr r0 JUMP_QUOT /* call the quotation */ +DEF(void,lazy_jit_compile,(CELL quot)): + mr r4,r1 /* save stack pointer */ + PROLOGUE + SAVE(r3,19) /* save quotation since we're about to mangle it */ + b MANGLE(jit_compile) + RESTORE(r3,19) /* restore quotation */ + EPILOGUE + JUMP_QUOT /* call the quotation */ + /* Thanks to Joshua Grams for this code. On PowerPC processors, we must flush the instruction cache manually diff --git a/vm/cpu-ppc.h b/vm/cpu-ppc.h index e4ff35c15c..c74e13e68b 100644 --- a/vm/cpu-ppc.h +++ b/vm/cpu-ppc.h @@ -11,6 +11,7 @@ void docol(CELL word); void undefined(CELL word); void set_callstack(F_STACK_FRAME *to, F_STACK_FRAME *from, CELL length, void *memcpy); void throw_impl(CELL quot, F_STACK_FRAME *rewind); +void lazy_jit_compile(CELL quot); void flush_icache(CELL start, CELL len); #define FRAME_SUCCESSOR(frame) (frame)->previous diff --git a/vm/data_gc.c b/vm/data_gc.c index eaf88f31c5..a94dcadf81 100644 --- a/vm/data_gc.c +++ b/vm/data_gc.c @@ -553,7 +553,7 @@ CELL collect_next(CELL scan) break; case QUOTATION_TYPE: quot = (F_QUOTATION *)scan; - if(collecting_code && quot->xt != NULL) + if(collecting_code && quot->xt != lazy_jit_compile) recursive_mark(quot->xt); break; case CALLSTACK_TYPE: diff --git a/vm/image.c b/vm/image.c index 32e628f902..d0f4635f82 100644 --- a/vm/image.c +++ b/vm/image.c @@ -162,10 +162,9 @@ void fixup_word(F_WORD *word) void fixup_quotation(F_QUOTATION *quot) { - /* quot->xt is only ever NULL at the start of stage2 bootstrap, - in this case the JIT compiles all quotations */ - if(quot->xt) - code_fixup("->xt); + code_fixup("->xt); + if(!in_code_heap_p(quot->xt)) + quot->xt = lazy_jit_compile; } void fixup_alien(F_ALIEN *d) diff --git a/vm/jit.c b/vm/jit.c index 7cdd645683..c6904414b5 100644 --- a/vm/jit.c +++ b/vm/jit.c @@ -152,21 +152,6 @@ void jit_compile(F_QUOTATION *quot) quot->xt = xt; } -void jit_compile_all(void) -{ - begin_scan(); - - CELL obj; - while((obj = next_object()) != F) - { - if(type_of(obj) == QUOTATION_TYPE) - jit_compile(untag_quotation(obj)); - } - - /* End the scan */ - gc_off = false; -} - XT quot_offset_to_pc(F_QUOTATION *quot, F_FIXNUM offset) { if(offset != -1) diff --git a/vm/jit.h b/vm/jit.h index 1758a24ff1..b3a54d604f 100644 --- a/vm/jit.h +++ b/vm/jit.h @@ -1,3 +1,3 @@ -void jit_compile(F_QUOTATION *quot); -void jit_compile_all(void); +DLLEXPORT void jit_compile(F_QUOTATION *quot); +jit_compile_all(void); XT quot_offset_to_pc(F_QUOTATION *quot, F_FIXNUM offset); diff --git a/vm/types.c b/vm/types.c index 1b9b3513dc..a17999a98b 100644 --- a/vm/types.c +++ b/vm/types.c @@ -131,12 +131,7 @@ DEFINE_PRIMITIVE(array_to_quotation) { F_QUOTATION *quot = allot_object(QUOTATION_TYPE,sizeof(F_QUOTATION)); quot->array = dpeek(); - quot->xt = NULL; - - REGISTER_UNTAGGED(quot); - jit_compile(quot); - UNREGISTER_UNTAGGED(quot); - + quot->xt = lazy_jit_compile; drepl(tag_object(quot)); }