factor/basis/cpu/x86/bootstrap.factor

306 lines
9.7 KiB
Factor
Raw Normal View History

2008-01-09 05:00:02 -05:00
! Copyright (C) 2007, 2008 Slava Pestov.
2007-09-21 23:29:38 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: bootstrap.image.private kernel kernel.private namespaces
2008-08-12 04:31:48 -04:00
system cpu.x86.assembler layouts compiler.units math
2008-10-07 21:00:38 -04:00
math.private compiler.constants vocabs
2008-08-12 04:31:48 -04:00
slots.private words words.private ;
2007-09-21 23:29:38 -04:00
IN: bootstrap.x86
big-endian off
1 jit-code-format set
2008-06-11 05:52:19 -04:00
: stack-frame-size ( -- n ) 4 bootstrap-cells ;
2007-10-06 18:54:46 -04:00
2007-09-21 23:29:38 -04:00
[
2008-01-02 19:36:36 -05:00
! Load word
2008-01-12 18:46:22 -05:00
temp-reg 0 MOV
temp-reg dup [] MOV
2008-01-02 19:36:36 -05:00
! Bump profiling counter
2008-01-02 22:07:25 -05:00
temp-reg profile-count-offset [+] 1 tag-fixnum ADD
2008-01-02 19:36:36 -05:00
! Load word->code
2008-01-02 22:07:25 -05:00
temp-reg temp-reg word-code-offset [+] MOV
2008-01-02 19:36:36 -05:00
! Compute word XT
2008-01-02 22:07:25 -05:00
temp-reg compiled-header-size ADD
2008-01-02 19:36:36 -05:00
! Jump to XT
2008-01-02 22:07:25 -05:00
temp-reg JMP
2008-01-12 21:38:57 -05:00
] rc-absolute-cell rt-literal 1 rex-length + jit-profiling jit-define
2008-01-02 19:36:36 -05:00
[
2008-01-12 21:38:57 -05:00
temp-reg 0 MOV ! load XT
stack-frame-size PUSH ! save stack frame size
2008-01-12 21:38:57 -05:00
temp-reg PUSH ! push XT
2007-12-26 20:02:41 -05:00
arg1 PUSH ! alignment
2008-01-12 21:38:57 -05:00
] rc-absolute-cell rt-label 1 rex-length + jit-prolog jit-define
[
2008-01-12 18:46:22 -05:00
arg0 0 MOV ! load literal
arg0 dup [] MOV
2007-09-21 23:29:38 -04:00
ds-reg bootstrap-cell ADD ! increment datastack pointer
ds-reg [] arg0 MOV ! store literal on datastack
2008-01-12 21:38:57 -05:00
] rc-absolute-cell rt-literal 1 rex-length + jit-push-literal jit-define
2008-07-12 23:27:28 -04:00
[
arg0 0 MOV ! load literal
ds-reg bootstrap-cell ADD ! increment datastack pointer
ds-reg [] arg0 MOV ! store literal on datastack
] rc-absolute-cell rt-immediate 1 rex-length + jit-push-immediate jit-define
[
2008-01-12 21:38:57 -05:00
arg0 0 MOV ! load XT
2007-09-21 23:29:38 -04:00
arg1 stack-reg MOV ! pass callstack pointer as arg 2
2008-01-12 21:38:57 -05:00
arg0 JMP ! go
] rc-absolute-cell rt-primitive 1 rex-length + jit-primitive jit-define
[
2007-12-26 02:33:49 -05:00
(JMP) drop
] rc-relative rt-xt 1 jit-word-jump jit-define
[
2007-12-26 02:33:49 -05:00
(CALL) drop
2007-12-26 20:02:41 -05:00
] rc-relative rt-xt 1 jit-word-call jit-define
[
arg1 0 MOV ! load addr of true quotation
2007-09-21 23:29:38 -04:00
arg0 ds-reg [] MOV ! load boolean
ds-reg bootstrap-cell SUB ! pop boolean
arg0 \ f tag-number CMP ! compare it with f
arg0 arg1 [] CMOVNE ! load true branch if not equal
arg0 arg1 bootstrap-cell [+] CMOVE ! load false branch if equal
2008-06-08 16:32:55 -04:00
arg0 quot-xt-offset [+] JMP ! jump to quotation-xt
2008-01-12 21:38:57 -05:00
] rc-absolute-cell rt-literal 1 rex-length + jit-if-jump jit-define
2007-09-21 23:29:38 -04:00
[
2008-01-12 18:46:22 -05:00
arg1 0 MOV ! load dispatch table
arg1 dup [] MOV
2007-09-21 23:29:38 -04:00
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
2008-06-08 16:32:55 -04:00
arg0 arg0 array-start-offset [+] MOV ! load quotation
arg0 quot-xt-offset [+] JMP ! execute branch
2008-01-12 21:38:57 -05:00
] rc-absolute-cell rt-literal 1 rex-length + jit-dispatch jit-define
2007-09-21 23:29:38 -04:00
[
stack-reg stack-frame-size bootstrap-cell - ADD ! unwind stack frame
] f f f jit-epilog jit-define
[ 0 RET ] f f f jit-return jit-define
! Sub-primitives
! Quotations and words
[
arg0 ds-reg [] MOV ! load from stack
ds-reg bootstrap-cell SUB ! pop stack
arg0 quot-xt-offset [+] JMP ! call quotation
] f f f \ (call) define-sub-primitive
[
arg0 ds-reg [] MOV ! load from stack
ds-reg bootstrap-cell SUB ! pop stack
arg0 word-xt-offset [+] JMP ! execute word
] f f f \ (execute) define-sub-primitive
! Objects
[
arg1 ds-reg [] MOV ! load from stack
arg1 tag-mask get AND ! compute tag
arg1 tag-bits get SHL ! tag the tag
ds-reg [] arg1 MOV ! push to stack
] f f f \ tag define-sub-primitive
[
arg0 ds-reg [] MOV ! load slot number
ds-reg bootstrap-cell SUB ! adjust stack pointer
arg1 ds-reg [] MOV ! load object
fixnum>slot@ ! turn slot number into offset
arg1 tag-bits get SHR ! mask off tag
arg1 tag-bits get SHL
arg0 arg1 arg0 [+] MOV ! load slot value
ds-reg [] arg0 MOV ! push to stack
] f f f \ slot define-sub-primitive
! Shufflers
[
ds-reg bootstrap-cell SUB
] f f f \ drop define-sub-primitive
[
ds-reg 2 bootstrap-cells SUB
] f f f \ 2drop define-sub-primitive
[
ds-reg 3 bootstrap-cells SUB
] f f f \ 3drop define-sub-primitive
[
arg0 ds-reg [] MOV
ds-reg bootstrap-cell ADD
ds-reg [] arg0 MOV
] f f f \ dup define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg bootstrap-cell neg [+] MOV
ds-reg 2 bootstrap-cells ADD
ds-reg [] arg0 MOV
ds-reg bootstrap-cell neg [+] arg1 MOV
] f f f \ 2dup define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg -1 bootstrap-cells [+] MOV
temp-reg ds-reg -2 bootstrap-cells [+] MOV
ds-reg 3 bootstrap-cells ADD
ds-reg [] arg0 MOV
ds-reg -1 bootstrap-cells [+] arg1 MOV
ds-reg -2 bootstrap-cells [+] temp-reg MOV
] f f f \ 3dup define-sub-primitive
[
arg0 ds-reg [] MOV
ds-reg bootstrap-cell SUB
ds-reg [] arg0 MOV
] f f f \ nip define-sub-primitive
[
arg0 ds-reg [] MOV
ds-reg 2 bootstrap-cells SUB
ds-reg [] arg0 MOV
] f f f \ 2nip define-sub-primitive
[
arg0 ds-reg -1 bootstrap-cells [+] MOV
ds-reg bootstrap-cell ADD
ds-reg [] arg0 MOV
] f f f \ over define-sub-primitive
[
arg0 ds-reg -2 bootstrap-cells [+] MOV
ds-reg bootstrap-cell ADD
ds-reg [] arg0 MOV
] f f f \ pick define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg -1 bootstrap-cells [+] MOV
ds-reg [] arg1 MOV
ds-reg bootstrap-cell ADD
ds-reg [] arg0 MOV
] f f f \ dupd define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg -1 bootstrap-cells [+] MOV
ds-reg bootstrap-cell ADD
ds-reg [] arg0 MOV
ds-reg -1 bootstrap-cells [+] arg1 MOV
ds-reg -2 bootstrap-cells [+] arg0 MOV
] f f f \ tuck define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg bootstrap-cell neg [+] MOV
ds-reg bootstrap-cell neg [+] arg0 MOV
ds-reg [] arg1 MOV
] f f f \ swap define-sub-primitive
[
arg0 ds-reg -1 bootstrap-cells [+] MOV
arg1 ds-reg -2 bootstrap-cells [+] MOV
ds-reg -2 bootstrap-cells [+] arg0 MOV
ds-reg -1 bootstrap-cells [+] arg1 MOV
] f f f \ swapd define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg -1 bootstrap-cells [+] MOV
temp-reg ds-reg -2 bootstrap-cells [+] MOV
ds-reg -2 bootstrap-cells [+] arg1 MOV
ds-reg -1 bootstrap-cells [+] arg0 MOV
ds-reg [] temp-reg MOV
] f f f \ rot define-sub-primitive
[
arg0 ds-reg [] MOV
arg1 ds-reg -1 bootstrap-cells [+] MOV
temp-reg ds-reg -2 bootstrap-cells [+] MOV
ds-reg -2 bootstrap-cells [+] arg0 MOV
ds-reg -1 bootstrap-cells [+] temp-reg MOV
ds-reg [] arg1 MOV
] f f f \ -rot define-sub-primitive
[
rs-reg bootstrap-cell ADD
arg0 ds-reg [] MOV
ds-reg bootstrap-cell SUB
rs-reg [] arg0 MOV
] f f f \ >r define-sub-primitive
[
ds-reg bootstrap-cell ADD
arg0 rs-reg [] MOV
rs-reg bootstrap-cell SUB
ds-reg [] arg0 MOV
] f f f \ r> define-sub-primitive
! Comparisons
: jit-compare ( insn -- )
arg1 0 MOV ! load t
arg1 dup [] MOV
temp-reg \ f tag-number MOV ! load f
arg0 ds-reg [] MOV ! load first value
ds-reg bootstrap-cell SUB ! adjust stack pointer
ds-reg [] arg0 CMP ! compare with second value
[ arg1 temp-reg ] dip execute ! move t if true
ds-reg [] arg1 MOV ! store
;
: define-jit-compare ( insn word -- )
[ [ jit-compare ] curry rc-absolute-cell rt-literal 1 rex-length + ] dip
define-sub-primitive ;
\ CMOVNE \ eq? define-jit-compare
\ CMOVL \ fixnum>= define-jit-compare
\ CMOVG \ fixnum<= define-jit-compare
\ CMOVLE \ fixnum> define-jit-compare
\ CMOVGE \ fixnum< define-jit-compare
! Math
: jit-math ( insn -- )
arg0 ds-reg [] MOV ! load second input
ds-reg bootstrap-cell SUB ! pop stack
arg1 ds-reg [] MOV ! load first input
[ arg1 arg0 ] dip execute ! compute result
ds-reg [] arg1 MOV ! push result
;
[ \ ADD jit-math ] f f f \ fixnum+fast define-sub-primitive
[ \ SUB jit-math ] f f f \ fixnum-fast define-sub-primitive
2007-09-21 23:29:38 -04:00
[
arg0 ds-reg [] MOV ! load second input
ds-reg bootstrap-cell SUB ! pop stack
arg1 ds-reg [] MOV ! load first input
arg0 tag-bits get SAR ! untag second input
arg0 arg1 IMUL2 ! multiply
ds-reg [] arg1 MOV ! push result
] f f f \ fixnum*fast define-sub-primitive
2007-09-21 23:29:38 -04:00
[ \ AND jit-math ] f f f \ fixnum-bitand define-sub-primitive
[ \ OR jit-math ] f f f \ fixnum-bitor define-sub-primitive
[ \ XOR jit-math ] f f f \ fixnum-bitxor define-sub-primitive
[
arg0 ds-reg [] MOV ! load input input
arg0 NOT ! complement
arg0 tag-mask get XOR ! clear tag bits
ds-reg [] arg0 MOV ! save
] f f f \ fixnum-bitnot define-sub-primitive
2007-09-21 23:29:38 -04:00
[ "bootstrap.x86" forget-vocab ] with-compilation-unit