From 3d784c7927e05cf294da7313704ca823d47c6ec8 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 25 Dec 2007 23:40:36 -0500 Subject: [PATCH] Simplify quotation calling convention: no need to pass XT --- core/bootstrap/image/image.factor | 3 ++ core/cpu/x86/32/bootstrap.factor | 1 - core/cpu/x86/bootstrap.factor | 82 +++++++++++++++---------------- core/cpu/x86/bootstrap.factor.new | 68 +++++++++++++++++++++++++ vm/code_heap.c | 16 +++--- vm/cpu-x86.S | 22 ++++----- vm/quotations.c | 21 +++++--- 7 files changed, 142 insertions(+), 71 deletions(-) mode change 100644 => 100755 core/cpu/x86/32/bootstrap.factor create mode 100755 core/cpu/x86/bootstrap.factor.new diff --git a/core/bootstrap/image/image.factor b/core/bootstrap/image/image.factor index 20aa3af0be..5d74f5d9c6 100755 --- a/core/bootstrap/image/image.factor +++ b/core/bootstrap/image/image.factor @@ -38,6 +38,9 @@ IN: bootstrap.image : quot-array@ bootstrap-cell object tag-number - ; : quot-xt@ 3 bootstrap-cells object tag-number - ; +: jit-define ( quot rc rt offset name -- ) + >r >r >r >r { } make r> r> r> 4array r> set ; + ! The image being constructed; a vector of word-size integers SYMBOL: image diff --git a/core/cpu/x86/32/bootstrap.factor b/core/cpu/x86/32/bootstrap.factor old mode 100644 new mode 100755 index 32d07797e7..88a7028929 --- a/core/cpu/x86/32/bootstrap.factor +++ b/core/cpu/x86/32/bootstrap.factor @@ -11,7 +11,6 @@ IN: bootstrap.x86 : stack-reg ESP ; : ds-reg ESI ; : scan-reg EBX ; -: xt-reg ECX ; : fixnum>slot@ arg0 1 SAR ; "resource:core/cpu/x86/bootstrap.factor" run-file diff --git a/core/cpu/x86/bootstrap.factor b/core/cpu/x86/bootstrap.factor index 935ca1bad0..3c42814bcf 100755 --- a/core/cpu/x86/bootstrap.factor +++ b/core/cpu/x86/bootstrap.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: bootstrap.image.private kernel namespaces system -cpu.x86.assembler layouts vocabs math ; +cpu.x86.assembler layouts vocabs math generator.fixup ; IN: bootstrap.x86 big-endian off @@ -13,77 +13,78 @@ big-endian off : scan-save stack-reg 3 bootstrap-cells [+] ; [ + ! arg0 0 MOV ! load quotation + arg1 arg0 quot-xt@ [+] MOV ! load XT arg0 arg0 quot-array@ [+] MOV ! load array scan-reg arg0 scan@ [+] LEA ! initialize scan pointer -] { } make jit-setup set +] rc-absolute-cell rt-literal 1 jit-setup jit-define -[ - stack-frame-size PUSH ! save stack frame size - xt-reg PUSH ! save XT +[ + stack-frame-size PUSH ! save stack frame size + arg1 PUSH ! save XT arg0 PUSH ! save array scan-reg PUSH ! initial scan stack-reg 3 bootstrap-cells SUB ! reserved -] { } make jit-prolog set - -: advance-scan scan-reg bootstrap-cell ADD ; - -[ - advance-scan +] f f f jit-prolog jit-define + +: advance-scan scan-reg bootstrap-cell ADD ; + +[ + advance-scan ds-reg bootstrap-cell ADD ! increment datastack pointer arg0 scan-reg [] MOV ! load literal ds-reg [] arg0 MOV ! store literal on datastack -] { } make jit-push-literal set +] f f f jit-push-literal jit-define -[ - advance-scan +[ + advance-scan ds-reg bootstrap-cell ADD ! increment datastack pointer arg0 scan-reg [] MOV ! load wrapper arg0 dup wrapper@ [+] MOV ! load wrapper-obj slot ds-reg [] arg0 MOV ! store literal on datastack -] { } make jit-push-wrapper set - -[ +] f f f jit-push-wrapper jit-define + +[ arg1 stack-reg MOV ! pass callstack pointer as arg 2 -] { } make jit-word-primitive-jump set - -[ +] f f f jit-word-primitive-jump jit-define + +[ arg1 stack-reg bootstrap-cell neg [+] LEA ! pass callstack pointer as arg 2 -] { } make jit-word-primitive-call set - -[ +] f f f jit-word-primitive-call jit-define + +[ arg0 scan-reg bootstrap-cell [+] MOV ! load word arg0 word-xt@ [+] JMP ! jump to word XT -] { } make jit-word-jump set - -[ - advance-scan +] f f f jit-word-jump jit-define + +[ + advance-scan scan-save scan-reg MOV ! save scan pointer arg0 scan-reg [] MOV ! load word arg0 word-xt@ [+] CALL ! call word XT scan-reg scan-save MOV ! restore scan pointer -] { } make jit-word-call set - -: load-branch +] f f f jit-word-call jit-define + +: load-branch arg0 ds-reg [] MOV ! load boolean ds-reg bootstrap-cell SUB ! pop boolean arg0 \ f tag-number CMP ! compare it with f arg0 scan-reg 2 bootstrap-cells [+] CMOVE ! load false branch if equal arg0 scan-reg 1 bootstrap-cells [+] CMOVNE ! load true branch if not equal scan-reg 3 bootstrap-cells ADD ! advance scan pointer - xt-reg arg0 quot-xt@ [+] MOV ! load quotation-xt + arg0 quot-xt@ [+] ! load quotation-xt ; [ - load-branch - xt-reg JMP -] { } make jit-if-jump set + load-branch JMP +] f f f jit-if-jump jit-define [ load-branch scan-save scan-reg MOV ! save scan pointer - xt-reg CALL ! call quotation + CALL ! call quotation scan-reg scan-save MOV ! restore scan pointer -] { } make jit-if-call set +] f f f jit-if-call jit-define [ arg0 ds-reg [] MOV ! load index @@ -91,14 +92,13 @@ big-endian off ds-reg bootstrap-cell SUB ! pop index arg0 scan-reg bootstrap-cell [+] ADD ! compute quotation location arg0 arg0 array-start [+] MOV ! load quotation - xt-reg arg0 quot-xt@ [+] MOV ! load quotation-xt - xt-reg JMP ! execute quotation -] { } make jit-dispatch set + arg0 quot-xt@ [+] JMP ! jump to quotation-xt +] f f f jit-dispatch jit-define [ stack-reg stack-frame-size bootstrap-cell - ADD ! unwind stack frame -] { } make jit-epilog set +] f f f jit-epilog jit-define -[ 0 RET ] { } make jit-return set +[ 0 RET ] f f f jit-return jit-define "bootstrap.x86" forget-vocab diff --git a/core/cpu/x86/bootstrap.factor.new b/core/cpu/x86/bootstrap.factor.new new file mode 100755 index 0000000000..35aa34563d --- /dev/null +++ b/core/cpu/x86/bootstrap.factor.new @@ -0,0 +1,68 @@ +[! Copyright (C) 2007 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: bootstrap.image.private kernel namespaces system +cpu.x86.assembler layouts vocabs math ; +IN: bootstrap.x86 + +big-endian off + +1 jit-code-format set + +: stack-frame-size 4 bootstrap-cells ; + +[ + arg0 0 MOV + stack-frame-size PUSH ! save stack frame size + arg0 PUSH ! save XT + 0 PUSH ! reserved +] { } make jit-prolog set + +[ + arg0 0 [] MOV ! load literal + ds-reg bootstrap-cell ADD ! increment datastack pointer + ds-reg [] arg0 MOV ! store literal on datastack +] { } make jit-push-literal set + +[ + arg1 stack-reg MOV ! pass callstack pointer as arg 2 +] { } make jit-word-primitive-jump set + +[ + arg1 stack-reg bootstrap-cell neg [+] LEA ! pass callstack pointer as arg 2 +] { } make jit-word-primitive-call set + +[ + 0 JMP +] { } make jit-word-jump set + +[ + 0 CALL +] { } make jit-word-call set + +[ + arg1 0 MOV ! load addr of true quotation + arg0 ds-reg [] MOV ! load boolean + ds-reg bootstrap-cell SUB ! pop boolean + arg0 \ f tag-number CMP ! compare it with f + arg0 arg1 [] CMOVE ! load false branch if equal + arg0 arg1 bootstrap-cell [+] CMOVNE ! load true branch if not equal + arg0 quot-xt@ [+] JMP ! execute branch +] { } make jit-if set + +[ + arg1 0 [] MOV ! load dispatch table + arg0 ds-reg [] MOV ! load index + fixnum>slot@ ! turn it into an array offset + ds-reg bootstrap-cell SUB ! pop index + arg0 arg1 ADD ! compute quotation location + arg0 arg0 array-start [+] MOV ! load quotation + arg0 quot-xt@ [+] JMP ! execute branch +] { } make jit-dispatch set + +[ + stack-reg stack-frame-size bootstrap-cell - ADD ! unwind stack frame +] { } make jit-epilog set + +[ 0 RET ] { } make jit-return set + +"bootstrap.x86" forget-vocab diff --git a/vm/code_heap.c b/vm/code_heap.c index 2c125cd345..3234a0c0bf 100755 --- a/vm/code_heap.c +++ b/vm/code_heap.c @@ -251,20 +251,20 @@ F_COMPILED *add_compiled_block( CELL profiler_prologue, F_ARRAY *code, F_ARRAY *labels, - F_ARRAY *rel, + F_ARRAY *relocation, F_ARRAY *words, F_ARRAY *literals) { CELL code_format = compiled_code_format(); CELL code_length = align8(array_capacity(code) * code_format); - CELL rel_length = (rel ? array_capacity(rel) * sizeof(unsigned int) : 0); + CELL rel_length = (relocation ? array_capacity(relocation) * sizeof(unsigned int) : 0); CELL words_length = (words ? array_capacity(words) * CELLS : 0); CELL literals_length = (literals ? array_capacity(literals) * CELLS : 0); REGISTER_UNTAGGED(code); REGISTER_UNTAGGED(labels); - REGISTER_UNTAGGED(rel); + REGISTER_UNTAGGED(relocation); REGISTER_UNTAGGED(words); REGISTER_UNTAGGED(literals); @@ -273,7 +273,7 @@ F_COMPILED *add_compiled_block( UNREGISTER_UNTAGGED(literals); UNREGISTER_UNTAGGED(words); - UNREGISTER_UNTAGGED(rel); + UNREGISTER_UNTAGGED(relocation); UNREGISTER_UNTAGGED(labels); UNREGISTER_UNTAGGED(code); @@ -295,9 +295,9 @@ F_COMPILED *add_compiled_block( here += code_length; /* relation info */ - if(rel) + if(relocation) { - deposit_integers(here,rel,sizeof(unsigned int)); + deposit_integers(here,relocation,sizeof(unsigned int)); here += rel_length; } @@ -365,7 +365,7 @@ DEFINE_PRIMITIVE(modify_code_heap) CELL profiler_prologue = to_cell(array_nth(compiled_code,0)); F_ARRAY *literals = untag_array(array_nth(compiled_code,1)); F_ARRAY *words = untag_array(array_nth(compiled_code,2)); - F_ARRAY *rel = untag_array(array_nth(compiled_code,3)); + F_ARRAY *relocation = untag_array(array_nth(compiled_code,3)); F_ARRAY *labels = untag_array(array_nth(compiled_code,4)); F_ARRAY *code = untag_array(array_nth(compiled_code,5)); @@ -377,7 +377,7 @@ DEFINE_PRIMITIVE(modify_code_heap) profiler_prologue, code, labels, - rel, + relocation, words, literals); diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index f4fb5f5d31..96489954f7 100755 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -1,6 +1,4 @@ -#define JUMP_QUOT \ - mov QUOT_XT_OFFSET(ARG0),XT_REG ; /* Load quot-xt */ \ - jmp *XT_REG /* Jump to quot-xt */ +#define JUMP_QUOT jmp *QUOT_XT_OFFSET(ARG0) DEF(F_FASTCALL void,c_to_factor,(CELL quot)): PUSH_NONVOLATILE @@ -10,8 +8,7 @@ DEF(F_FASTCALL void,c_to_factor,(CELL quot)): call MANGLE(save_callstack_bottom) mov (STACK_REG),ARG0 /* Pass quot as arg 1 */ - mov QUOT_XT_OFFSET(ARG0),XT_REG - call *XT_REG /* Call quot-xt */ + call *QUOT_XT_OFFSET(ARG0) /* Call quot-xt */ POP ARG0 POP_NONVOLATILE @@ -40,8 +37,7 @@ callstack top parameter to primitives. */ DEF(F_FASTCALL void,primitive_execute,(void)): mov (DS_REG),ARG0 /* Load word from data stack */ sub $CELL_SIZE,DS_REG /* Pop data stack */ - mov WORD_XT_OFFSET(ARG0),XT_REG /* Load word-xt slot */ - jmp *XT_REG /* Go */ + jmp *WORD_XT_OFFSET(ARG0) /* Load word-xt slot */ DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): mov ARG1,STACK_REG /* rewind_to */ @@ -49,14 +45,14 @@ DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): DEF(FASTCALL void,lazy_jit_compile,(CELL quot)): mov STACK_REG,ARG1 /* Save stack pointer */ - push XT_REG /* Alignment */ - push XT_REG - push XT_REG + push ARG1 /* Alignment */ + push ARG1 + push ARG1 call MANGLE(primitive_jit_compile) mov RETURN_REG,ARG0 /* No-op on 32-bit */ - pop XT_REG /* OK to clobber XT_REG here */ - pop XT_REG - pop XT_REG + pop ARG1 /* OK to clobber ARG1 here */ + pop ARG1 + pop ARG1 JUMP_QUOT /* Call the quotation */ #ifdef WINDOWS diff --git a/vm/quotations.c b/vm/quotations.c index 97baf2afe9..60c6d729d7 100755 --- a/vm/quotations.c +++ b/vm/quotations.c @@ -17,9 +17,14 @@ bool jit_fast_dispatch_p(F_ARRAY *array, CELL i) && array_nth(array,i + 1) == userenv[JIT_DISPATCH_WORD]; } +F_ARRAY *code_to_emit(CELL name) +{ + return untag_object(array_nth(untag_object(userenv[name]),0)); +} + #define EMIT(name) { \ REGISTER_UNTAGGED(array); \ - GROWABLE_APPEND(result,untag_object(userenv[name])); \ + GROWABLE_APPEND(code,code_to_emit(name)); \ UNREGISTER_UNTAGGED(array); \ } @@ -54,7 +59,7 @@ void jit_compile(F_QUOTATION *quot) REGISTER_UNTAGGED(quot); REGISTER_UNTAGGED(array); - GROWABLE_ARRAY(result); + GROWABLE_ARRAY(code); UNREGISTER_UNTAGGED(array); bool stack_frame = jit_stack_frame_p(array); @@ -149,16 +154,16 @@ void jit_compile(F_QUOTATION *quot) EMIT(JIT_RETURN); } - GROWABLE_TRIM(result); + GROWABLE_TRIM(code); UNREGISTER_UNTAGGED(quot); REGISTER_UNTAGGED(quot); - REGISTER_UNTAGGED(result); + REGISTER_UNTAGGED(code); F_ARRAY *literals = allot_array(ARRAY_TYPE,1,tag_object(quot)); - UNREGISTER_UNTAGGED(result); + UNREGISTER_UNTAGGED(code); - F_COMPILED *compiled = add_compiled_block(QUOTATION_TYPE,0,result,NULL,NULL,NULL,literals); + F_COMPILED *compiled = add_compiled_block(QUOTATION_TYPE,0,code,NULL,NULL,NULL,literals); iterate_code_heap_step(compiled,finalize_code_block); UNREGISTER_UNTAGGED(quot); @@ -181,11 +186,11 @@ XT quot_offset_to_pc(F_QUOTATION *quot, F_FIXNUM offset) CELL xt = 0; - xt += array_capacity(untag_array(userenv[JIT_SETUP])); + xt += array_capacity(code_to_emit(JIT_SETUP)); bool stack_frame = jit_stack_frame_p(untag_array(quot->array)); if(stack_frame) - xt += array_capacity(untag_array(userenv[JIT_PROLOG])); + xt += array_capacity(code_to_emit(JIT_PROLOG)); xt *= compiled_code_format();