1 ! Copyright (C) 2007, 2011 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
5 compiler.codegen.relocation math math.private cpu.x86.assembler
6 cpu.x86.assembler.operands sequences generic.single.private
12 : shift-arg ( -- reg ) RCX ;
13 : div-arg ( -- reg ) RAX ;
14 : mod-arg ( -- reg ) RDX ;
15 : temp0 ( -- reg ) RAX ;
16 : temp1 ( -- reg ) RCX ;
17 : temp2 ( -- reg ) RDX ;
18 : temp3 ( -- reg ) RBX ;
19 : pic-tail-reg ( -- reg ) RBX ;
20 : return-reg ( -- reg ) RAX ;
21 : nv-reg ( -- reg ) RBX ;
22 : stack-reg ( -- reg ) RSP ;
23 : frame-reg ( -- reg ) RBP ;
24 : link-reg ( -- reg ) R11 ;
25 : ctx-reg ( -- reg ) R12 ;
26 : vm-reg ( -- reg ) R13 ;
27 : ds-reg ( -- reg ) R14 ;
28 : rs-reg ( -- reg ) R15 ;
29 : fixnum>slot@ ( -- ) temp0 1 SAR ;
30 : rex-length ( -- n ) 1 ;
32 : jit-call ( name -- )
33 RAX 0 MOV f rc-absolute-cell rel-dlsym
38 RAX 0 MOV rc-absolute-cell rel-this
40 RSP bootstrap-cell 2 * neg [+] RAX MOV
41 ! store stack frame size
42 RSP bootstrap-cell neg [+] stack-frame-size MOV
44 RSP stack-frame-size bootstrap-cell - SUB
45 ] jit-prolog jit-define
48 pic-tail-reg 5 [RIP+] LEA
49 0 JMP f rc-relative rel-word-pic-tail
50 ] jit-word-jump jit-define
52 : jit-load-context ( -- )
53 ctx-reg vm-reg vm-context-offset [+] MOV ;
55 : jit-save-context ( -- )
58 ctx-reg context-callstack-top-offset [+] R11 MOV
59 ctx-reg context-datastack-offset [+] ds-reg MOV
60 ctx-reg context-retainstack-offset [+] rs-reg MOV ;
62 : jit-restore-context ( -- )
63 ds-reg ctx-reg context-datastack-offset [+] MOV
64 rs-reg ctx-reg context-retainstack-offset [+] MOV ;
67 ! ctx-reg is preserved across the call because it is non-volatile
72 RAX 0 MOV f f rc-absolute-cell rel-dlsym
75 ] jit-primitive jit-define
77 : jit-jump-quot ( -- ) arg1 quot-entry-point-offset [+] JMP ;
79 : jit-call-quot ( -- ) arg1 quot-entry-point-offset [+] CALL ;
84 "begin_callback" jit-call
91 "end_callback" jit-call
92 ] \ c-to-factor define-sub-primitive
96 ds-reg bootstrap-cell SUB
100 \ (call) define-combinator-primitive
103 ! Unwind stack frames
106 ! Load VM pointer into vm-reg, since we're entering from
108 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
110 ! Load ds and rs registers
116 ] \ unwind-native-frames define-sub-primitive
124 ] \ fpu-state define-sub-primitive
128 RSP [] arg1 16-bit-version-of MOV
131 ] \ set-fpu-state define-sub-primitive
134 ! Load callstack object
136 ds-reg bootstrap-cell SUB
137 ! Get ctx->callstack_bottom
139 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
140 ! Get top of callstack object -- 'src' for memcpy
141 arg2 arg4 callstack-top-offset [+] LEA
142 ! Get callstack length, in bytes --- 'len' for memcpy
143 arg3 arg4 callstack-length-offset [+] MOV
144 arg3 tag-bits get SHR
145 ! Compute new stack pointer -- 'dst' for memcpy
147 ! Install new stack pointer
149 ! Call memcpy; arguments are now in the correct registers
150 ! Create register shadow area for Win64
152 "factor_memcpy" jit-call
153 ! Tear down register shadow area
155 ! Return with new callstack
157 ] \ set-callstack define-sub-primitive
162 "lazy_jit_compile" jit-call
165 [ return-reg quot-entry-point-offset [+] CALL ]
167 \ lazy-jit-compile define-combinator-primitive
170 temp2 HEX: ffffffff MOV f rc-absolute-cell rel-literal
172 ] pic-check-tuple jit-define
174 ! Inline cache miss entry points
175 : jit-load-return-address ( -- )
176 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
178 ! These are always in tail position with an existing stack
179 ! frame, and the stack. The frame setup takes this into account.
180 : jit-inline-cache-miss ( -- )
184 RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
187 jit-restore-context ;
189 [ jit-load-return-address jit-inline-cache-miss ]
192 \ inline-cache-miss define-combinator-primitive
194 [ jit-inline-cache-miss ]
197 \ inline-cache-miss-tail define-combinator-primitive
199 ! Overflowing fixnum arithmetic
200 : jit-overflow ( insn func -- )
204 arg2 ds-reg 8 [+] MOV
206 [ [ arg3 arg2 ] dip call ] dip
209 [ arg3 vm-reg MOV jit-call ]
210 jit-conditional ; inline
212 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
214 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
228 arg1 tag-bits get SAR
231 "overflow_fixnum_multiply" jit-call
234 ] \ fixnum* define-sub-primitive
237 : jit-switch-context ( reg -- )
238 ! Reset return value since its bogus right now, to avoid
242 ! Make the new context the current one
244 vm-reg vm-context-offset [+] ctx-reg MOV
246 ! Load new stack pointer
247 RSP ctx-reg context-callstack-top-offset [+] MOV
249 ! Load new ds, rs registers
252 ctx-reg jit-update-tib ;
254 : jit-pop-context-and-param ( -- )
256 arg1 arg1 alien-offset [+] MOV
257 arg2 ds-reg -8 [+] MOV
260 : jit-push-param ( -- )
264 : jit-set-context ( -- )
265 jit-pop-context-and-param
267 arg1 jit-switch-context
271 [ jit-set-context ] \ (set-context) define-sub-primitive
273 : jit-pop-quot-and-param ( -- )
275 arg2 ds-reg -8 [+] MOV
278 : jit-start-context ( -- )
279 ! Create the new context in return-reg. Have to save context
280 ! twice, first before calling new_context() which may GC,
281 ! and again after popping the two parameters from the stack.
284 "new_context" jit-call
286 jit-pop-quot-and-param
288 return-reg jit-switch-context
292 [ jit-start-context ] \ (start-context) define-sub-primitive
294 : jit-delete-current-context ( -- )
298 "delete_context" jit-call ;
301 jit-delete-current-context
303 ] \ (set-context-and-delete) define-sub-primitive
305 : jit-start-context-and-delete ( -- )
309 "reset_context" jit-call
311 jit-pop-quot-and-param
312 ctx-reg jit-switch-context
316 : jit-safepoint ( -- )
317 0 [RIP+] EAX MOV rc-relative rel-safepoint ;
320 jit-start-context-and-delete
321 ] \ (start-context-and-delete) define-sub-primitive