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 stack-frame-size bootstrap-cell - SUB
42 RSP stack-frame-size bootstrap-cell 3 * - [+] RAX MOV
43 ! store stack frame size
44 RSP stack-frame-size bootstrap-cell 2 * - [+] 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
96 : jit-save-volatile-regs ( -- )
97 ! do we also need to save XMM?
98 RSP volatile-regs length bootstrap-cell * SUB
100 [| r i | RSP i bootstrap-cell * [+] r MOV ] each-index ;
102 :: jit-restore-volatile-regs ( additional-pop -- )
104 [| r i | r RSP i bootstrap-cell * [+] MOV ] each-index
105 RSP volatile-regs length bootstrap-cell * additional-pop + ADD ;
108 ! Stack at this point has the signal handler pointer followed by
109 ! the return address back into normal execution, then the 24 bytes
110 ! of stack frame + alignment inserted by the prolog.
111 ! After registers are saved, the stack looks like:
112 ! RSP saved volatile regs (`volatile-regs length` cells)
113 ! + subprimitive stack frame alignment (3 cells)
114 ! . signal handler address (1 cell)
115 ! . resume address (1 cell)
116 jit-save-volatile-regs
118 RAX RSP volatile-regs length 3 + bootstrap-cell * [+] MOV
120 bootstrap-cell jit-restore-volatile-regs
121 ] \ signal-handler define-sub-primitive
123 ! :: jit-push-leaf-stack-frame ( -- )
126 ! :: jit-pop-leaf-stack-frame ( -- )
130 ! ! Stack at this point has the signal handler pointer followed by
131 ! ! the word pointer and the return address back into normal execution,
132 ! ! then the 24 bytes of stack frame + alignment inserted by the prolog
133 ! ! After registers are saved and the leaf stack frame is constructed,
134 ! ! the stack looks like:
135 ! ! RSP fake leaf stack frame (4 cells)
136 ! ! + saved volatile regs (`volatile-regs length` cells)
137 ! ! . subprimitive stack frame alignment (3 cells)
138 ! ! . leaf word (1 cell)
139 ! ! . signal handler address (1 cell)
140 ! ! resume address (1 cell)
141 ! jit-save-volatile-regs
142 ! jit-push-leaf-stack-frame
144 ! "memory_signal_handler_impl" jit-call
145 ! jit-pop-leaf-stack-frame
146 ! bootstrap-cell jit-restore-volatile-regs
147 ! ] \ leaf-signal-handler define-sub-primitive
151 ds-reg bootstrap-cell SUB
155 \ (call) define-combinator-primitive
158 ! Unwind stack frames
161 ! Load VM pointer into vm-reg, since we're entering from
163 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
165 ! Load ds and rs registers
171 ] \ unwind-native-frames define-sub-primitive
179 ] \ fpu-state define-sub-primitive
183 RSP [] arg1 16-bit-version-of MOV
186 ] \ set-fpu-state define-sub-primitive
189 ! Load callstack object
191 ds-reg bootstrap-cell SUB
192 ! Get ctx->callstack_bottom
194 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
195 ! Get top of callstack object -- 'src' for memcpy
196 arg2 arg4 callstack-top-offset [+] LEA
197 ! Get callstack length, in bytes --- 'len' for memcpy
198 arg3 arg4 callstack-length-offset [+] MOV
199 arg3 tag-bits get SHR
200 ! Compute new stack pointer -- 'dst' for memcpy
202 ! Install new stack pointer
204 ! Call memcpy; arguments are now in the correct registers
205 ! Create register shadow area for Win64
207 "factor_memcpy" jit-call
208 ! Tear down register shadow area
210 ! Return with new callstack
212 ] \ set-callstack define-sub-primitive
217 "lazy_jit_compile" jit-call
220 [ return-reg quot-entry-point-offset [+] CALL ]
222 \ lazy-jit-compile define-combinator-primitive
225 temp2 HEX: ffffffff MOV f rc-absolute-cell rel-literal
227 ] pic-check-tuple jit-define
229 ! Inline cache miss entry points
230 : jit-load-return-address ( -- )
231 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
233 ! These are always in tail position with an existing stack
234 ! frame, and the stack. The frame setup takes this into account.
235 : jit-inline-cache-miss ( -- )
239 RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
242 jit-restore-context ;
244 [ jit-load-return-address jit-inline-cache-miss ]
247 \ inline-cache-miss define-combinator-primitive
249 [ jit-inline-cache-miss ]
252 \ inline-cache-miss-tail define-combinator-primitive
254 ! Overflowing fixnum arithmetic
255 : jit-overflow ( insn func -- )
259 arg2 ds-reg 8 [+] MOV
261 [ [ arg3 arg2 ] dip call ] dip
264 [ arg3 vm-reg MOV jit-call ]
265 jit-conditional ; inline
267 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
269 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
283 arg1 tag-bits get SAR
286 "overflow_fixnum_multiply" jit-call
289 ] \ fixnum* define-sub-primitive
292 : jit-switch-context ( reg -- )
293 ! Reset return value since its bogus right now, to avoid
297 ! Make the new context the current one
299 vm-reg vm-context-offset [+] ctx-reg MOV
301 ! Load new stack pointer
302 RSP ctx-reg context-callstack-top-offset [+] MOV
304 ! Load new ds, rs registers
307 ctx-reg jit-update-tib ;
309 : jit-pop-context-and-param ( -- )
311 arg1 arg1 alien-offset [+] MOV
312 arg2 ds-reg -8 [+] MOV
315 : jit-push-param ( -- )
319 : jit-set-context ( -- )
320 jit-pop-context-and-param
322 arg1 jit-switch-context
326 [ jit-set-context ] \ (set-context) define-sub-primitive
328 : jit-pop-quot-and-param ( -- )
330 arg2 ds-reg -8 [+] MOV
333 : jit-start-context ( -- )
334 ! Create the new context in return-reg. Have to save context
335 ! twice, first before calling new_context() which may GC,
336 ! and again after popping the two parameters from the stack.
339 "new_context" jit-call
341 jit-pop-quot-and-param
343 return-reg jit-switch-context
347 [ jit-start-context ] \ (start-context) define-sub-primitive
349 : jit-delete-current-context ( -- )
353 "delete_context" jit-call ;
356 jit-delete-current-context
358 ] \ (set-context-and-delete) define-sub-primitive
360 : jit-start-context-and-delete ( -- )
364 "reset_context" jit-call
366 jit-pop-quot-and-param
367 ctx-reg jit-switch-context
371 : jit-safepoint ( -- )
372 0 [RIP+] EAX MOV rc-relative rel-safepoint ;
375 jit-start-context-and-delete
376 ] \ (start-context-and-delete) define-sub-primitive