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 1 + bootstrap-cells 16 align stack-frame-size + :> frame-size
100 ! minus a cell each for flags, return address
101 ! use LEA so we don't dirty flags
102 RSP RSP frame-size 2 bootstrap-cells - neg [+] LEA
103 save-regs [| r i | RSP i bootstrap-cells [+] r MOV ] each-index
105 ! Now that the registers are saved, we can make the stack frame
106 RAX 0 MOV rc-absolute-cell rel-this
107 RSP frame-size 3 bootstrap-cells - [+] RAX MOV
108 RSP frame-size 2 bootstrap-cells - [+] frame-size MOV
111 :: jit-signal-handler-epilog ( frame-size -- )
113 signal-handler-save-regs
114 [| r i | r RSP i bootstrap-cells [+] MOV ] each-index
115 RSP RSP frame-size 2 bootstrap-cells - [+] LEA ;
119 ds-reg bootstrap-cell SUB
123 \ (call) define-combinator-primitive
126 ! Unwind stack frames
129 ! Load VM pointer into vm-reg, since we're entering from
131 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
133 ! Load ds and rs registers
137 ! Clear the fault flag
138 vm-reg vm-fault-flag-offset [+] 0 MOV
142 ] \ unwind-native-frames define-sub-primitive
150 ] \ fpu-state define-sub-primitive
154 RSP [] arg1 16-bit-version-of MOV
157 ] \ set-fpu-state define-sub-primitive
160 ! Load callstack object
162 ds-reg bootstrap-cell SUB
163 ! Get ctx->callstack_bottom
165 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
166 ! Get top of callstack object -- 'src' for memcpy
167 arg2 arg4 callstack-top-offset [+] LEA
168 ! Get callstack length, in bytes --- 'len' for memcpy
169 arg3 arg4 callstack-length-offset [+] MOV
170 arg3 tag-bits get SHR
171 ! Compute new stack pointer -- 'dst' for memcpy
173 ! Install new stack pointer
175 ! Call memcpy; arguments are now in the correct registers
176 ! Create register shadow area for Win64
178 "factor_memcpy" jit-call
179 ! Tear down register shadow area
181 ! Return with new callstack
183 ] \ set-callstack define-sub-primitive
188 "lazy_jit_compile" jit-call
191 [ return-reg quot-entry-point-offset [+] CALL ]
193 \ lazy-jit-compile define-combinator-primitive
196 temp2 0xffffffff MOV f rc-absolute-cell rel-literal
198 ] pic-check-tuple jit-define
200 ! Inline cache miss entry points
201 : jit-load-return-address ( -- )
202 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
204 ! These are always in tail position with an existing stack
205 ! frame, and the stack. The frame setup takes this into account.
206 : jit-inline-cache-miss ( -- )
210 RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
213 jit-restore-context ;
215 [ jit-load-return-address jit-inline-cache-miss ]
218 \ inline-cache-miss define-combinator-primitive
220 [ jit-inline-cache-miss ]
223 \ inline-cache-miss-tail define-combinator-primitive
225 ! Overflowing fixnum arithmetic
226 : jit-overflow ( insn func -- )
230 arg2 ds-reg 8 [+] MOV
232 [ [ arg3 arg2 ] dip call ] dip
235 [ arg3 vm-reg MOV jit-call ]
236 jit-conditional ; inline
238 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
240 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
254 arg1 tag-bits get SAR
257 "overflow_fixnum_multiply" jit-call
260 ] \ fixnum* define-sub-primitive
263 : jit-switch-context ( reg -- )
264 ! Reset return value since its bogus right now, to avoid
268 ! Make the new context the current one
270 vm-reg vm-context-offset [+] ctx-reg MOV
272 ! Load new stack pointer
273 RSP ctx-reg context-callstack-top-offset [+] MOV
275 ! Load new ds, rs registers
278 ctx-reg jit-update-tib ;
280 : jit-pop-context-and-param ( -- )
282 arg1 arg1 alien-offset [+] MOV
283 arg2 ds-reg -8 [+] MOV
286 : jit-push-param ( -- )
290 : jit-set-context ( -- )
291 jit-pop-context-and-param
293 arg1 jit-switch-context
297 [ jit-set-context ] \ (set-context) define-sub-primitive
299 : jit-pop-quot-and-param ( -- )
301 arg2 ds-reg -8 [+] MOV
304 : jit-start-context ( -- )
305 ! Create the new context in return-reg. Have to save context
306 ! twice, first before calling new_context() which may GC,
307 ! and again after popping the two parameters from the stack.
310 "new_context" jit-call
312 jit-pop-quot-and-param
314 return-reg jit-switch-context
318 [ jit-start-context ] \ (start-context) define-sub-primitive
320 : jit-delete-current-context ( -- )
324 "delete_context" jit-call ;
327 jit-delete-current-context
329 ] \ (set-context-and-delete) define-sub-primitive
331 : jit-start-context-and-delete ( -- )
335 "reset_context" jit-call
337 jit-pop-quot-and-param
338 ctx-reg jit-switch-context
343 0 [RIP+] EAX MOV rc-relative rel-safepoint
344 ] \ jit-safepoint jit-define
347 jit-start-context-and-delete
348 ] \ (start-context-and-delete) define-sub-primitive