From 6fb134f1aae4d5425d01c8adc94b414b32b694f9 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 5 Oct 2007 04:17:38 -0400 Subject: [PATCH] Update non-optimizing compiler for new PowerPC calling convention --- core/cpu/ppc/bootstrap.factor | 100 +++++++++++++-------------- core/cpu/ppc/linux/bootstrap.factor | 2 +- core/cpu/ppc/macosx/bootstrap.factor | 2 +- vm/callstack.c | 38 +--------- vm/callstack.h | 1 + vm/cpu-ppc.S | 4 -- vm/cpu-ppc.h | 20 +++++- vm/cpu-x86.h | 4 +- vm/image.c | 7 +- vm/os-linux-ppc.h | 27 +------- vm/os-macosx-ppc.h | 32 +-------- 11 files changed, 78 insertions(+), 159 deletions(-) diff --git a/core/cpu/ppc/bootstrap.factor b/core/cpu/ppc/bootstrap.factor index 7caaabc16a..e3819dea91 100644 --- a/core/cpu/ppc/bootstrap.factor +++ b/core/cpu/ppc/bootstrap.factor @@ -17,85 +17,85 @@ big-endian on : temp-reg 6 ; : xt-reg 11 ; -: param-save-size 8 bootstrap-cells ; - -: local@ - bootstrap-cells reserved-area-size param-save-size + + ; - -: array-save 0 local@ ; -: scan-save 1 local@ ; -: xt-save 2 local@ ; +: factor-area-size 4 bootstrap-cells ; : stack-frame - 3 local@ 4 bootstrap-cells align ; + factor-area-size c-area-size + 4 bootstrap-cells align ; + +: next-save stack-frame bootstrap-cell - ; +: xt-save stack-frame 2 bootstrap-cells - ; +: scan-save stack-frame 3 bootstrap-cells - ; +: array-save stack-frame 4 bootstrap-cells - ; [ - temp-reg quot-reg quot-array@ LWZ ! load array - scan-reg temp-reg scan@ ADDI ! initialize scan pointer + temp-reg quot-reg quot-array@ LWZ ! load array + scan-reg temp-reg scan@ ADDI ! initialize scan pointer ] { } make jit-setup set [ - 1 1 stack-frame neg STWU ! store back link - 0 MFLR ! load return address into r0 - temp-reg 1 array-save STW ! save array - xt-reg 1 xt-save STW ! save XT - 0 1 lr-save stack-frame + STW ! save return address + 0 MFLR + 1 1 stack-frame neg ADDI + xt-reg 1 xt-save STW ! save XT + xt-reg 1 factor-area-size neg ADDI + xt-reg 1 next-save STW ! save forward chain pointer + temp-reg 1 array-save STW ! save array + 0 1 lr-save stack-frame + STW ! save return address ] { } make jit-prolog set [ - temp-reg scan-reg 4 LWZU ! load literal and advance - temp-reg ds-reg 4 STWU ! push literal + temp-reg scan-reg 4 LWZU ! load literal and advance + temp-reg ds-reg 4 STWU ! push literal ] { } make jit-push-literal set [ - temp-reg scan-reg 4 LWZU ! load wrapper and advance - temp-reg dup wrapper@ LWZ ! load wrapped object - temp-reg ds-reg 4 STWU ! push wrapped object + temp-reg scan-reg 4 LWZU ! load wrapper and advance + temp-reg dup wrapper@ LWZ ! load wrapped object + temp-reg ds-reg 4 STWU ! push wrapped object ] { } make jit-push-wrapper set [ - 4 1 MR ! pass stack pointer to primitive + 4 1 MR ! pass stack pointer to primitive ] { } make jit-word-primitive-jump set [ - 4 1 MR ! pass stack pointer to primitive + 4 1 MR ! pass stack pointer to primitive ] { } make jit-word-primitive-call set : load-xt ( -- ) xt-reg word-reg word-xt@ LWZ ; : jit-call - scan-reg 1 scan-save STW ! save scan pointer - xt-reg MTLR ! pass XT to callee - BLRL ! call - scan-reg 1 scan-save LWZ ! restore scan pointer + scan-reg 1 scan-save STW ! save scan pointer + xt-reg MTLR ! pass XT to callee + BLRL ! call + scan-reg 1 scan-save LWZ ! restore scan pointer ; : jit-jump xt-reg MTCTR BCTR ; [ - word-reg scan-reg 4 LWZU ! load word and advance + word-reg scan-reg 4 LWZU ! load word and advance load-xt jit-call ] { } make jit-word-call set [ - word-reg scan-reg 4 LWZ ! load word - load-xt ! jump to word XT + word-reg scan-reg 4 LWZ ! load word + load-xt ! jump to word XT jit-jump ] { } make jit-word-jump set : load-branch - temp-reg ds-reg 0 LWZ ! load boolean - 0 temp-reg \ f tag-number CMPI ! compare it with f - quot-reg scan-reg MR ! point quot-reg at false branch - 2 BNE ! skip next insn if its not f - quot-reg dup 4 ADDI ! point quot-reg at true branch - quot-reg dup 4 LWZ ! load the branch - ds-reg dup 4 SUBI ! pop boolean - scan-reg dup 12 ADDI ! advance scan pointer - xt-reg quot-reg quot-xt@ LWZ ! load quotation-xt + temp-reg ds-reg 0 LWZ ! load boolean + 0 temp-reg \ f tag-number CMPI ! compare it with f + quot-reg scan-reg MR ! point quot-reg at false branch + 2 BNE ! skip next insn if its not f + quot-reg dup 4 ADDI ! point quot-reg at true branch + quot-reg dup 4 LWZ ! load the branch + ds-reg dup 4 SUBI ! pop boolean + scan-reg dup 12 ADDI ! advance scan pointer + xt-reg quot-reg quot-xt@ LWZ ! load quotation-xt ; [ @@ -107,20 +107,20 @@ big-endian on ] { } make jit-if-call set [ - temp-reg ds-reg 0 LWZ ! load index - temp-reg dup 1 SRAWI ! turn it into an array offset - ds-reg dup 4 SUBI ! pop index - scan-reg dup 4 LWZ ! load array - temp-reg dup scan-reg ADD ! compute quotation location - quot-reg temp-reg array-start LWZ ! load quotation - xt-reg quot-reg quot-xt@ LWZ ! load quotation-xt - jit-jump ! execute quotation + temp-reg ds-reg 0 LWZ ! load index + temp-reg dup 1 SRAWI ! turn it into an array offset + ds-reg dup 4 SUBI ! pop index + scan-reg dup 4 LWZ ! load array + temp-reg dup scan-reg ADD ! compute quotation location + quot-reg temp-reg array-start LWZ ! load quotation + xt-reg quot-reg quot-xt@ LWZ ! load quotation-xt + jit-jump ! execute quotation ] { } make jit-dispatch set [ - 0 1 lr-save stack-frame + LWZ ! load return address - 1 1 stack-frame ADDI ! pop stack frame - 0 MTLR ! get ready to return + 0 1 lr-save stack-frame + LWZ ! load return address + 1 1 stack-frame ADDI ! pop stack frame + 0 MTLR ! get ready to return ] { } make jit-epilog set [ BLR ] { } make jit-return set diff --git a/core/cpu/ppc/linux/bootstrap.factor b/core/cpu/ppc/linux/bootstrap.factor index 32491f90a2..a84bff5141 100644 --- a/core/cpu/ppc/linux/bootstrap.factor +++ b/core/cpu/ppc/linux/bootstrap.factor @@ -3,7 +3,7 @@ USING: parser layouts system ; IN: bootstrap.ppc -: reserved-area-size 2 bootstrap-cells ; +: c-area-size 10 bootstrap-cells ; : lr-save bootstrap-cell ; "resource:core/cpu/ppc/bootstrap.factor" run-file diff --git a/core/cpu/ppc/macosx/bootstrap.factor b/core/cpu/ppc/macosx/bootstrap.factor index 4909c8649c..016e445522 100644 --- a/core/cpu/ppc/macosx/bootstrap.factor +++ b/core/cpu/ppc/macosx/bootstrap.factor @@ -3,7 +3,7 @@ USING: parser layouts system ; IN: bootstrap.ppc -: reserved-area-size 6 bootstrap-cells ; +: c-area-size 14 bootstrap-cells ; : lr-save 2 bootstrap-cells ; "resource:core/cpu/ppc/bootstrap.factor" run-file diff --git a/vm/callstack.c b/vm/callstack.c index a7cfe903c0..ae1fef0cad 100644 --- a/vm/callstack.c +++ b/vm/callstack.c @@ -12,15 +12,9 @@ void iterate_callstack(CELL top, CELL bottom, CELL base, CALLSTACK_ITER iterator { CELL delta = (bottom - base); -#ifdef CALLSTACK_UP_P F_STACK_FRAME *frame = (F_STACK_FRAME *)bottom - 1; - #define ITERATING_P (CELL)frame >= top -#else - F_STACK_FRAME *frame = (F_STACK_FRAME *)top; - #define ITERATING_P (CELL)frame < bottom -#endif - while(ITERATING_P) + while((CELL)frame >= top) { F_STACK_FRAME *next = REBASE_FRAME_SUCCESSOR(frame,delta); iterator(frame); @@ -55,7 +49,6 @@ called by continuation implementation, and user code shouldn't be calling it at all, so we leave it as it is for now. */ F_STACK_FRAME *capture_start(void) { -#ifdef CALLSTACK_UP_P F_STACK_FRAME *frame = stack_chain->callstack_bottom - 1; while(frame >= stack_chain->callstack_top && FRAME_SUCCESSOR(frame) >= stack_chain->callstack_top) @@ -63,9 +56,6 @@ F_STACK_FRAME *capture_start(void) frame = FRAME_SUCCESSOR(frame); } return frame + 1; -#else - return FRAME_SUCCESSOR(stack_chain->callstack_top); -#endif } DEFINE_PRIMITIVE(callstack) @@ -152,13 +142,8 @@ CELL frame_scan(F_STACK_FRAME *frame) void stack_frame_to_array(F_STACK_FRAME *frame) { -#ifdef CALLSTACK_UP_P set_array_nth(array,frame_index++,frame_executing(frame)); set_array_nth(array,frame_index++,frame_scan(frame)); -#else - set_array_nth(array,frame_index--,frame_scan(frame)); - set_array_nth(array,frame_index--,frame_executing(frame)); -#endif } DEFINE_PRIMITIVE(callstack_to_array) @@ -174,11 +159,7 @@ DEFINE_PRIMITIVE(callstack_to_array) /* frame_count is equal to the total length now */ -#ifdef CALLSTACK_UP_P frame_index = 0; -#else - frame_index = frame_count - 1; -#endif iterate_callstack_object(stack,stack_frame_to_array); @@ -187,7 +168,6 @@ DEFINE_PRIMITIVE(callstack_to_array) F_STACK_FRAME *innermost_stack_frame(F_CALLSTACK *callstack) { -#ifdef CALLSTACK_UP_P CELL top = (CELL)(callstack + 1); CELL bottom = top + untag_fixnum_fast(callstack->length); CELL base = callstack->bottom; @@ -200,9 +180,6 @@ F_STACK_FRAME *innermost_stack_frame(F_CALLSTACK *callstack) frame = REBASE_FRAME_SUCCESSOR(frame,delta); return frame; -#else - return FIRST_STACK_FRAME(callstack); -#endif } /* Some primitives implementing a limited form of callstack mutation. @@ -244,26 +221,17 @@ DEFINE_PRIMITIVE(set_innermost_stack_frame_quot) CELL scan = inner->scan - inner->array; -#ifdef CALLSTACK_UP_P CELL top = (CELL)(callstack + 1); CELL bottom = top + untag_fixnum_fast(callstack->length); CELL base = callstack->bottom; CELL delta = (bottom - base); - F_STACK_FRAME *next = REBASE_FRAME_SUCCESSOR(inner,delta); - CELL offset = *(XT *)(next + 1) - inner->xt; -#else - CELL offset = inner->return_address - inner->xt; -#endif + CELL offset = FRAME_RETURN_ADDRESS(inner,delta) - inner->xt; inner->array = quot->array; inner->scan = quot->array + scan; inner->xt = quot->xt; -#ifdef CALLSTACK_UP_P - *(XT *)(next + 1) = quot->xt + offset; -#else - inner->return_address = quot->xt + offset; -#endif + FRAME_RETURN_ADDRESS(inner,delta) = quot->xt + offset; } diff --git a/vm/callstack.h b/vm/callstack.h index 575c614342..fdff137604 100644 --- a/vm/callstack.h +++ b/vm/callstack.h @@ -2,6 +2,7 @@ F_FASTCALL void save_callstack_bottom(F_STACK_FRAME *callstack_bottom); #define FIRST_STACK_FRAME(stack) (F_STACK_FRAME *)((stack) + 1) +#define FRAME_SUCCESSOR(frame) (frame)->next #define REBASE_FRAME_SUCCESSOR(frame,delta) (F_STACK_FRAME *)((CELL)FRAME_SUCCESSOR(frame) + delta) typedef void (*CALLSTACK_ITER)(F_STACK_FRAME *frame); diff --git a/vm/cpu-ppc.S b/vm/cpu-ppc.S index 99be40a74e..3c90fabca2 100644 --- a/vm/cpu-ppc.S +++ b/vm/cpu-ppc.S @@ -146,8 +146,6 @@ DEF(void,primitive_execute,(void)): subi r14,r14,4 /* pop word from data stack */ bctr /* go */ -#define SCAN_SAVE (RESERVED_SIZE + PARAM_SIZE + 4) - /* We pass a function pointer to memcpy in r6 to work around a Mac OS X ABI limitation which would otherwise require us to do a bizzaro PC-relative trampoline to retrieve the function address */ @@ -159,8 +157,6 @@ DEF(void,set_callstack,(F_STACK_FRAME *to, F_STACK_FRAME *from, CELL length, voi blrl /* go */ lwz r1,0(r1) /* tear down fake stack frame */ lwz r0,LR_SAVE(r1) /* we have restored the stack; load return address */ - /* load quotation scan pointer */ - lwz r5,SCAN_SAVE(r1) mtlr r0 /* prepare to return to restored callstack */ blr /* go */ diff --git a/vm/cpu-ppc.h b/vm/cpu-ppc.h index ac4a0a92ee..ee651f70fb 100644 --- a/vm/cpu-ppc.h +++ b/vm/cpu-ppc.h @@ -1,3 +1,21 @@ +typedef struct _F_STACK_FRAME +{ + /* In compiled quotation frames, the quot->array slot. + In compiled word frames, unused. */ + CELL array; + + /* In compiled quotation frames, position within the array. + In compiled word frames, unused. */ + CELL scan; + + /* In all compiled frames, the XT on entry. */ + XT xt; + + /* Pointer to the next stack frame; frames are chained from + the bottom on up */ + struct _F_STACK_FRAME *next; +} F_STACK_FRAME; + #define FACTOR_CPU_STRING "ppc" #define F_FASTCALL @@ -13,5 +31,3 @@ void set_callstack(F_STACK_FRAME *to, F_STACK_FRAME *from, CELL length, void *me 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/cpu-x86.h b/vm/cpu-x86.h index 91f1f8236a..cedf861a63 100644 --- a/vm/cpu-x86.h +++ b/vm/cpu-x86.h @@ -16,9 +16,7 @@ typedef struct _F_STACK_FRAME XT xt; } F_STACK_FRAME; -#define CALLSTACK_UP_P - -#define FRAME_SUCCESSOR(frame) (frame)->next +#define FRAME_RETURN_ADDRESS(frame,delta) *(XT *)(REBASE_FRAME_SUCCESSOR(frame,delta) + 1) INLINE void flush_icache(CELL start, CELL len) {} diff --git a/vm/image.c b/vm/image.c index a4fd08af00..9fb13930a9 100644 --- a/vm/image.c +++ b/vm/image.c @@ -186,12 +186,7 @@ void fixup_stack_frame(F_STACK_FRAME *frame) frame->scan = scan + frame->array; } -#ifdef CALLSTACK_UP_P - F_STACK_FRAME *next = REBASE_FRAME_SUCCESSOR(frame,delta); - code_fixup((XT *)(next + 1)); -#else - code_fixup(&frame->return_address); -#endif + code_fixup(&FRAME_RETURN_ADDRESS(frame,delta)); } void fixup_callstack_object(F_CALLSTACK *stack) diff --git a/vm/os-linux-ppc.h b/vm/os-linux-ppc.h index d90524095b..ba047d22b1 100644 --- a/vm/os-linux-ppc.h +++ b/vm/os-linux-ppc.h @@ -1,29 +1,4 @@ -typedef struct _F_STACK_FRAME -{ - struct _F_STACK_FRAME *previous; - - /* Callee stores our LR here */ - XT return_address; - - /* ===== 32 bytes saved register area ===== */ - CELL padding5[8]; - - /* ===== 16 byte local variable area ===== */ - - /* In compiled quotation frames, the quot->array slot. - In compiled word frames, unused. */ - CELL array; - - /* In compiled quotation frames, position within the array. - In compiled word frames, unused. */ - CELL scan; - - /* In all compiled frames, the XT on entry. */ - XT xt; - - /* ===== 12 byte padding to make it 16 byte aligned ===== */ - CELL padding6[3]; -} F_STACK_FRAME; +#define FRAME_RETURN_ADDRESS(frame,delta) *((XT *)(REBASE_FRAME_SUCCESSOR(frame,delta) + 1) + 1) #define UAP_PROGRAM_COUNTER(ucontext) \ (((ucontext_t *)(ucontext))->uc_mcontext.uc_regs->gregs[PT_NIP]) diff --git a/vm/os-macosx-ppc.h b/vm/os-macosx-ppc.h index 6cc9bd5dac..2e9fcd0110 100644 --- a/vm/os-macosx-ppc.h +++ b/vm/os-macosx-ppc.h @@ -1,34 +1,4 @@ -typedef struct _F_STACK_FRAME { - /* ===== 24 bytes reserved ===== */ - struct _F_STACK_FRAME *previous; - - CELL padding1; - - /* Callee stores our LR here */ - XT return_address; - - CELL padding2; - CELL padding3; - CELL padding4; - /* ===== 32 bytes saved register area ===== */ - CELL padding5[8]; - - /* ===== 16 byte local variable area ===== */ - - /* In compiled quotation frames, the quot->array slot. - In compiled word frames, unused. */ - CELL array; - - /* In compiled quotation frames, position within the array. - In compiled word frames, unused. */ - CELL scan; - - /* In all compiled frames, the XT on entry. */ - XT xt; - - /* ===== 12 byte padding to make it 16 byte aligned ===== */ - CELL padding6[3]; -} F_STACK_FRAME; +#define FRAME_RETURN_ADDRESS(frame,delta) *((XT *)(REBASE_FRAME_SUCCESSOR(frame,delta) + 1) + 2) #define MACH_EXC_STATE_TYPE ppc_exception_state_t #define MACH_EXC_STATE_FLAVOR PPC_EXCEPTION_STATE