1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private kernel kernel.private namespaces
4 system layouts vocabs parser compiler.constants math
5 math.private cpu.x86.assembler cpu.x86.assembler.operands
6 sequences generic.single.private ;
11 : shift-arg ( -- reg ) RCX ;
12 : div-arg ( -- reg ) RAX ;
13 : mod-arg ( -- reg ) RDX ;
14 : temp0 ( -- reg ) RDI ;
15 : temp1 ( -- reg ) RSI ;
16 : temp2 ( -- reg ) RDX ;
17 : temp3 ( -- reg ) RBX ;
18 : return-reg ( -- reg ) RAX ;
19 : nv-reg ( -- reg ) nv-regs first ;
20 : stack-reg ( -- reg ) RSP ;
21 : frame-reg ( -- reg ) RBP ;
22 : ctx-reg ( -- reg ) R12 ;
23 : vm-reg ( -- reg ) R13 ;
24 : ds-reg ( -- reg ) R14 ;
25 : rs-reg ( -- reg ) R15 ;
26 : fixnum>slot@ ( -- ) temp0 1 SAR ;
27 : rex-length ( -- n ) 1 ;
29 : jit-call ( name -- )
30 RAX 0 MOV rc-absolute-cell jit-dlsym
35 RAX 0 MOV rc-absolute-cell rt-this jit-rel
36 ! save stack frame size
41 RSP stack-frame-size 3 bootstrap-cells - SUB
42 ] jit-prolog jit-define
46 0 JMP rc-relative rt-entry-point-pic-tail jit-rel
47 ] jit-word-jump jit-define
49 : jit-load-context ( -- )
50 ctx-reg vm-reg vm-context-offset [+] MOV ;
52 : jit-save-context ( -- )
55 ctx-reg context-callstack-top-offset [+] RAX MOV
56 ctx-reg context-datastack-offset [+] ds-reg MOV
57 ctx-reg context-retainstack-offset [+] rs-reg MOV ;
59 : jit-restore-context ( -- )
61 ds-reg ctx-reg context-datastack-offset [+] MOV
62 rs-reg ctx-reg context-retainstack-offset [+] MOV ;
68 RAX 0 MOV rc-absolute-cell rt-dlsym jit-rel
71 ] jit-primitive jit-define
77 "begin_callback" jit-call
81 ! save C callstack pointer
82 ctx-reg context-callstack-save-offset [+] stack-reg MOV
84 ! load Factor callstack pointer
85 stack-reg ctx-reg context-callstack-bottom-offset [+] MOV
90 arg1 quot-entry-point-offset [+] CALL
94 ! load C callstack pointer
95 stack-reg ctx-reg context-callstack-save-offset [+] MOV
98 "end_callback" jit-call
99 ] \ c-to-factor define-sub-primitive
103 ds-reg bootstrap-cell SUB
105 [ arg1 quot-entry-point-offset [+] CALL ]
106 [ arg1 quot-entry-point-offset [+] JMP ]
107 \ (call) define-combinator-primitive
110 ! Clear x87 stack, but preserve rounding mode and exception flags
116 ! Unwind stack frames
119 ! Load VM pointer into vm-reg, since we're entering from
121 vm-reg 0 MOV 0 rc-absolute-cell jit-vm
123 ! Load ds and rs registers
127 arg1 quot-entry-point-offset [+] JMP
128 ] \ unwind-native-frames define-sub-primitive
131 ! Load callstack object
133 ds-reg bootstrap-cell SUB
134 ! Get ctx->callstack_bottom
136 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
137 ! Get top of callstack object -- 'src' for memcpy
138 arg2 arg4 callstack-top-offset [+] LEA
139 ! Get callstack length, in bytes --- 'len' for memcpy
140 arg3 arg4 callstack-length-offset [+] MOV
141 arg3 tag-bits get SHR
142 ! Compute new stack pointer -- 'dst' for memcpy
144 ! Install new stack pointer
146 ! Call memcpy; arguments are now in the correct registers
147 ! Create register shadow area for Win64
149 "factor_memcpy" jit-call
150 ! Tear down register shadow area
152 ! Return with new callstack
154 ] \ set-callstack define-sub-primitive
159 "lazy_jit_compile" jit-call
161 [ return-reg quot-entry-point-offset [+] CALL ]
162 [ return-reg quot-entry-point-offset [+] JMP ]
163 \ lazy-jit-compile define-combinator-primitive
165 ! Inline cache miss entry points
166 : jit-load-return-address ( -- )
167 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
169 ! These are always in tail position with an existing stack
170 ! frame, and the stack. The frame setup takes this into account.
171 : jit-inline-cache-miss ( -- )
175 "inline_cache_miss" jit-call
176 jit-restore-context ;
178 [ jit-load-return-address jit-inline-cache-miss ]
181 \ inline-cache-miss define-combinator-primitive
183 [ jit-inline-cache-miss ]
186 \ inline-cache-miss-tail define-combinator-primitive
188 ! Overflowing fixnum arithmetic
189 : jit-overflow ( insn func -- )
193 arg2 ds-reg 8 [+] MOV
195 [ [ arg3 arg2 ] dip call ] dip
198 [ arg3 vm-reg MOV jit-call ]
199 jit-conditional ; inline
201 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
203 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
217 arg1 tag-bits get SAR
220 "overflow_fixnum_multiply" jit-call
223 ] \ fixnum* define-sub-primitive
225 << "vocab:cpu/x86/bootstrap.factor" parse-file suffix! >>