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 threads.private ;
11 : shift-arg ( -- reg ) RCX ;
12 : div-arg ( -- reg ) RAX ;
13 : mod-arg ( -- reg ) RDX ;
14 : temp0 ( -- reg ) RAX ;
15 : temp1 ( -- reg ) RCX ;
16 : temp2 ( -- reg ) RDX ;
17 : temp3 ( -- reg ) RBX ;
18 : pic-tail-reg ( -- reg ) RBX ;
19 : return-reg ( -- reg ) RAX ;
20 : nv-reg ( -- reg ) RBX ;
21 : stack-reg ( -- reg ) RSP ;
22 : frame-reg ( -- reg ) RBP ;
23 : link-reg ( -- reg ) R11 ;
24 : ctx-reg ( -- reg ) R12 ;
25 : vm-reg ( -- reg ) R13 ;
26 : ds-reg ( -- reg ) R14 ;
27 : rs-reg ( -- reg ) R15 ;
28 : fixnum>slot@ ( -- ) temp0 1 SAR ;
29 : rex-length ( -- n ) 1 ;
31 : jit-call ( name -- )
32 RAX 0 MOV rc-absolute-cell jit-dlsym
37 RAX 0 MOV rc-absolute-cell rt-this jit-rel
38 ! save stack frame size
43 RSP stack-frame-size 3 bootstrap-cells - SUB
44 ] jit-prolog jit-define
47 pic-tail-reg 5 [RIP+] LEA
48 0 JMP rc-relative rt-entry-point-pic-tail jit-rel
49 ] jit-word-jump jit-define
51 : jit-load-context ( -- )
52 ctx-reg vm-reg vm-context-offset [+] MOV ;
54 : jit-save-context ( -- )
57 ctx-reg context-callstack-top-offset [+] R11 MOV
58 ctx-reg context-datastack-offset [+] ds-reg MOV
59 ctx-reg context-retainstack-offset [+] rs-reg MOV ;
61 : jit-restore-context ( -- )
62 ds-reg ctx-reg context-datastack-offset [+] MOV
63 rs-reg ctx-reg context-retainstack-offset [+] MOV ;
66 ! ctx-reg is preserved across the call because it is non-volatile
71 RAX 0 MOV rc-absolute-cell rt-dlsym jit-rel
74 ] jit-primitive jit-define
76 : jit-jump-quot ( -- ) arg1 quot-entry-point-offset [+] JMP ;
78 : jit-call-quot ( -- ) arg1 quot-entry-point-offset [+] CALL ;
83 "begin_callback" jit-call
90 "end_callback" jit-call
91 ] \ c-to-factor define-sub-primitive
95 ds-reg bootstrap-cell SUB
99 \ (call) define-combinator-primitive
102 ! Unwind stack frames
105 ! Load VM pointer into vm-reg, since we're entering from
107 vm-reg 0 MOV 0 rc-absolute-cell jit-vm
109 ! Load ds and rs registers
115 ] \ unwind-native-frames define-sub-primitive
123 ] \ get-fpu-state define-sub-primitive
127 RSP [] arg1 16-bit-version-of MOV
130 ] \ set-fpu-state define-sub-primitive
133 ! Load callstack object
135 ds-reg bootstrap-cell SUB
136 ! Get ctx->callstack_bottom
138 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
139 ! Get top of callstack object -- 'src' for memcpy
140 arg2 arg4 callstack-top-offset [+] LEA
141 ! Get callstack length, in bytes --- 'len' for memcpy
142 arg3 arg4 callstack-length-offset [+] MOV
143 arg3 tag-bits get SHR
144 ! Compute new stack pointer -- 'dst' for memcpy
146 ! Install new stack pointer
148 ! Call memcpy; arguments are now in the correct registers
149 ! Create register shadow area for Win64
151 "factor_memcpy" jit-call
152 ! Tear down register shadow area
154 ! Return with new callstack
156 ] \ set-callstack define-sub-primitive
161 "lazy_jit_compile" jit-call
164 [ return-reg quot-entry-point-offset [+] CALL ]
166 \ lazy-jit-compile define-combinator-primitive
169 temp2 HEX: ffffffff MOV rc-absolute-cell rt-literal jit-rel
171 ] pic-check-tuple jit-define
173 ! Inline cache miss entry points
174 : jit-load-return-address ( -- )
175 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
177 ! These are always in tail position with an existing stack
178 ! frame, and the stack. The frame setup takes this into account.
179 : jit-inline-cache-miss ( -- )
183 "inline_cache_miss" jit-call
185 jit-restore-context ;
187 [ jit-load-return-address jit-inline-cache-miss ]
190 \ inline-cache-miss define-combinator-primitive
192 [ jit-inline-cache-miss ]
195 \ inline-cache-miss-tail define-combinator-primitive
197 ! Overflowing fixnum arithmetic
198 : jit-overflow ( insn func -- )
202 arg2 ds-reg 8 [+] MOV
204 [ [ arg3 arg2 ] dip call ] dip
207 [ arg3 vm-reg MOV jit-call ]
208 jit-conditional ; inline
210 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
212 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
226 arg1 tag-bits get SAR
229 "overflow_fixnum_multiply" jit-call
232 ] \ fixnum* define-sub-primitive
235 : jit-switch-context ( reg -- )
236 ! Reset return value since its bogus right now, to avoid
240 ! Make the new context the current one
242 vm-reg vm-context-offset [+] ctx-reg MOV
244 ! Load new stack pointer
245 RSP ctx-reg context-callstack-top-offset [+] MOV
247 ! Load new ds, rs registers
250 ctx-reg jit-update-tib ;
252 : jit-pop-context-and-param ( -- )
254 arg1 arg1 alien-offset [+] MOV
255 arg2 ds-reg -8 [+] MOV
258 : jit-push-param ( -- )
262 : jit-set-context ( -- )
263 jit-pop-context-and-param
265 arg1 jit-switch-context
269 [ jit-set-context ] \ (set-context) define-sub-primitive
271 : jit-pop-quot-and-param ( -- )
273 arg2 ds-reg -8 [+] MOV
276 : jit-start-context ( -- )
277 ! Create the new context in return-reg. Have to save context
278 ! twice, first before calling new_context() which may GC,
279 ! and again after popping the two parameters from the stack.
282 "new_context" jit-call
284 jit-pop-quot-and-param
286 return-reg jit-switch-context
290 [ jit-start-context ] \ (start-context) define-sub-primitive
292 : jit-delete-current-context ( -- )
296 "delete_context" jit-call ;
299 jit-delete-current-context
301 ] \ (set-context-and-delete) define-sub-primitive
303 : jit-start-context-and-delete ( -- )
307 "reset_context" jit-call
309 jit-pop-quot-and-param
310 ctx-reg jit-switch-context
315 jit-start-context-and-delete
316 ] \ (start-context-and-delete) define-sub-primitive