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
7 threads.private locals ;
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 stack-frame-size bootstrap-cell - SUB
42 RSP stack-frame-size 3 bootstrap-cells - [+] RAX MOV
43 ! store stack frame size
44 RSP stack-frame-size 2 bootstrap-cells - [+] stack-frame-size MOV
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
94 : signal-handler-save-regs ( -- regs )
95 { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15 } ;
97 :: jit-signal-handler-prolog ( -- frame-size )
98 signal-handler-save-regs :> save-regs
99 save-regs length bootstrap-cells 16 align stack-frame-size + :> frame-size
100 RSP frame-size bootstrap-cell - SUB ! minus a cell for return address
102 [| r i | RSP i bootstrap-cells [+] r MOV ] each-index
103 ! Now that the registers are saved, we can make the stack frame
104 RAX 0 MOV rc-absolute-cell rel-this
105 RSP frame-size 3 bootstrap-cells - [+] RAX MOV
106 RSP frame-size 2 bootstrap-cells - [+] frame-size MOV
109 :: jit-signal-handler-epilog ( frame-size -- )
110 signal-handler-save-regs
111 [| r i | r RSP i bootstrap-cells [+] MOV ] each-index
112 RSP frame-size bootstrap-cell - ADD ;
116 ds-reg bootstrap-cell SUB
120 \ (call) define-combinator-primitive
123 ! Unwind stack frames
126 ! Load VM pointer into vm-reg, since we're entering from
128 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
130 ! Load ds and rs registers
136 ] \ unwind-native-frames define-sub-primitive
144 ] \ fpu-state define-sub-primitive
148 RSP [] arg1 16-bit-version-of MOV
151 ] \ set-fpu-state define-sub-primitive
154 ! Load callstack object
156 ds-reg bootstrap-cell SUB
157 ! Get ctx->callstack_bottom
159 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
160 ! Get top of callstack object -- 'src' for memcpy
161 arg2 arg4 callstack-top-offset [+] LEA
162 ! Get callstack length, in bytes --- 'len' for memcpy
163 arg3 arg4 callstack-length-offset [+] MOV
164 arg3 tag-bits get SHR
165 ! Compute new stack pointer -- 'dst' for memcpy
167 ! Install new stack pointer
169 ! Call memcpy; arguments are now in the correct registers
170 ! Create register shadow area for Win64
172 "factor_memcpy" jit-call
173 ! Tear down register shadow area
175 ! Return with new callstack
177 ] \ set-callstack define-sub-primitive
182 "lazy_jit_compile" jit-call
185 [ return-reg quot-entry-point-offset [+] CALL ]
187 \ lazy-jit-compile define-combinator-primitive
190 temp2 HEX: ffffffff MOV f rc-absolute-cell rel-literal
192 ] pic-check-tuple jit-define
194 ! Inline cache miss entry points
195 : jit-load-return-address ( -- )
196 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
198 ! These are always in tail position with an existing stack
199 ! frame, and the stack. The frame setup takes this into account.
200 : jit-inline-cache-miss ( -- )
204 RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
207 jit-restore-context ;
209 [ jit-load-return-address jit-inline-cache-miss ]
212 \ inline-cache-miss define-combinator-primitive
214 [ jit-inline-cache-miss ]
217 \ inline-cache-miss-tail define-combinator-primitive
219 ! Overflowing fixnum arithmetic
220 : jit-overflow ( insn func -- )
224 arg2 ds-reg 8 [+] MOV
226 [ [ arg3 arg2 ] dip call ] dip
229 [ arg3 vm-reg MOV jit-call ]
230 jit-conditional ; inline
232 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
234 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
248 arg1 tag-bits get SAR
251 "overflow_fixnum_multiply" jit-call
254 ] \ fixnum* define-sub-primitive
257 : jit-switch-context ( reg -- )
258 ! Reset return value since its bogus right now, to avoid
262 ! Make the new context the current one
264 vm-reg vm-context-offset [+] ctx-reg MOV
266 ! Load new stack pointer
267 RSP ctx-reg context-callstack-top-offset [+] MOV
269 ! Load new ds, rs registers
272 ctx-reg jit-update-tib ;
274 : jit-pop-context-and-param ( -- )
276 arg1 arg1 alien-offset [+] MOV
277 arg2 ds-reg -8 [+] MOV
280 : jit-push-param ( -- )
284 : jit-set-context ( -- )
285 jit-pop-context-and-param
287 arg1 jit-switch-context
291 [ jit-set-context ] \ (set-context) define-sub-primitive
293 : jit-pop-quot-and-param ( -- )
295 arg2 ds-reg -8 [+] MOV
298 : jit-start-context ( -- )
299 ! Create the new context in return-reg. Have to save context
300 ! twice, first before calling new_context() which may GC,
301 ! and again after popping the two parameters from the stack.
304 "new_context" jit-call
306 jit-pop-quot-and-param
308 return-reg jit-switch-context
312 [ jit-start-context ] \ (start-context) define-sub-primitive
314 : jit-delete-current-context ( -- )
318 "delete_context" jit-call ;
321 jit-delete-current-context
323 ] \ (set-context-and-delete) define-sub-primitive
325 : jit-start-context-and-delete ( -- )
329 "reset_context" jit-call
331 jit-pop-quot-and-param
332 ctx-reg jit-switch-context
337 0 [RIP+] EAX MOV rc-relative rel-safepoint
338 ] \ jit-safepoint jit-define
341 jit-start-context-and-delete
342 ] \ (start-context-and-delete) define-sub-primitive