Simplify quotation calling convention: no need to pass XT

db4
Slava Pestov 2007-12-25 23:40:36 -05:00
parent 423c22e4ef
commit 3d784c7927
7 changed files with 142 additions and 71 deletions

View File

@ -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

1
core/cpu/x86/32/bootstrap.factor Normal file → Executable file
View File

@ -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

View File

@ -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,17 +13,19 @@ 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
arg1 PUSH ! save XT
arg0 PUSH ! save array
scan-reg PUSH ! initial scan
stack-reg 3 bootstrap-cells SUB ! reserved
] { } make jit-prolog set
] f f f jit-prolog jit-define
: advance-scan scan-reg bootstrap-cell ADD ;
@ -32,7 +34,7 @@ big-endian off
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
@ -40,20 +42,20 @@ big-endian off
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
] f f f jit-word-jump jit-define
[
advance-scan
@ -61,7 +63,7 @@ big-endian off
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
] f f f jit-word-call jit-define
: load-branch
arg0 ds-reg [] MOV ! load boolean
@ -70,20 +72,19 @@ big-endian off
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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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();