1 ! Copyright (C) 2007, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private compiler.codegen.relocation
4 compiler.constants cpu.x86.assembler cpu.x86.assembler.operands
5 generic.single.private kernel kernel.private layouts math
6 math.private namespaces 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 f rc-absolute-cell rel-dlsym
35 :: jit-call-1arg ( arg1s name -- )
39 :: jit-call-2arg ( arg1s arg2s name -- )
45 pic-tail-reg 5 [RIP+] LEA
46 0 JMP f rc-relative rel-word-pic-tail
47 ] JIT-WORD-JUMP jit-define
50 ! no-op on x86-64. in factor contexts vm-reg always contains the
54 : jit-load-context ( -- )
55 ctx-reg vm-reg vm-context-offset [+] MOV ;
57 : jit-save-context ( -- )
59 ! The reason for -8 I think is because we are anticipating a CALL
60 ! instruction. After the call instruction, the contexts frame_top
61 ! will point to the origin jump address.
63 ctx-reg context-callstack-top-offset [+] R11 MOV
64 ctx-reg context-datastack-offset [+] ds-reg MOV
65 ctx-reg context-retainstack-offset [+] rs-reg MOV ;
67 ! ctx-reg must already have been loaded
68 : jit-restore-context ( -- )
69 ds-reg ctx-reg context-datastack-offset [+] MOV
70 rs-reg ctx-reg context-retainstack-offset [+] MOV ;
73 ! ctx-reg is preserved across the call because it is non-volatile
78 RAX 0 MOV f f rc-absolute-cell rel-dlsym
81 ] JIT-PRIMITIVE jit-define
83 : jit-jump-quot ( -- )
84 arg1 quot-entry-point-offset [+] JMP ;
86 : jit-call-quot ( -- ) arg1 quot-entry-point-offset [+] CALL ;
88 : signal-handler-save-regs ( -- regs )
89 { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15 } ;
93 ds-reg bootstrap-cell SUB
97 \ (call) define-combinator-primitive
102 "lazy_jit_compile" jit-call
105 [ return-reg quot-entry-point-offset [+] CALL ]
107 \ lazy-jit-compile define-combinator-primitive
110 temp2 0 MOV f rc-absolute-cell rel-literal
112 ] PIC-CHECK-TUPLE jit-define
114 ! Inline cache miss entry points
115 : jit-load-return-address ( -- )
116 RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
118 ! These are always in tail position with an existing stack
119 ! frame, and the stack. The frame setup takes this into account.
120 : jit-inline-cache-miss ( -- )
124 RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
127 jit-restore-context ;
129 [ jit-load-return-address jit-inline-cache-miss ]
132 \ inline-cache-miss define-combinator-primitive
134 [ jit-inline-cache-miss ]
137 \ inline-cache-miss-tail define-combinator-primitive
139 ! Overflowing fixnum arithmetic
140 : jit-overflow ( insn func -- )
144 arg2 ds-reg 8 [+] MOV
146 [ [ arg3 arg2 ] dip call ] dip
149 [ arg3 vm-reg MOV jit-call ]
150 jit-conditional ; inline
153 : jit-switch-context ( reg -- )
154 ! Push a bogus return address so the GC can track this frame back
158 ! Make the new context the current one
160 vm-reg vm-context-offset [+] ctx-reg MOV
162 ! Load new stack pointer
163 RSP ctx-reg context-callstack-top-offset [+] MOV
165 ! Load new ds, rs registers
168 ctx-reg jit-update-tib ;
170 : jit-pop-context-and-param ( -- )
172 arg1 arg1 alien-offset [+] MOV
173 arg2 ds-reg -8 [+] MOV
176 : jit-push-param ( -- )
180 : jit-set-context ( -- )
181 jit-pop-context-and-param
183 arg1 jit-switch-context
187 : jit-pop-quot-and-param ( -- )
189 arg2 ds-reg -8 [+] MOV
192 : jit-start-context ( -- )
193 ! Create the new context in return-reg. Have to save context
194 ! twice, first before calling new_context() which may GC,
195 ! and again after popping the two parameters from the stack.
197 vm-reg "new_context" jit-call-1arg
199 jit-pop-quot-and-param
201 return-reg jit-switch-context
205 : jit-delete-current-context ( -- )
206 vm-reg "delete_context" jit-call-1arg ;
208 ! Resets the active context and instead the passed in quotation
209 ! becomes the new code that it executes.
210 : jit-start-context-and-delete ( -- )
211 ! Updates the context to match the values in the data and retain
212 ! stack registers. reset_context can GC.
215 ! Resets the context. The top two ds items are preserved.
216 vm-reg "reset_context" jit-call-1arg
218 ! Switches to the same context I think.
219 ctx-reg jit-switch-context
221 ! Pops the quotation from the stack and puts it in arg1.
225 ! Jump to quotation arg1
229 0 [RIP+] EAX MOV rc-relative rel-safepoint
230 ] JIT-SAFEPOINT jit-define
232 ! # All x86.64 subprimitives
235 { (set-context) [ jit-set-context ] }
236 { (set-context-and-delete) [
237 jit-delete-current-context
240 { (start-context) [ jit-start-context ] }
241 { (start-context-and-delete) [ jit-start-context-and-delete ] }
246 vm-reg "begin_callback" jit-call-1arg
252 vm-reg "end_callback" jit-call-1arg
254 { unwind-native-frames [
255 ! unwind-native-frames is marked as "special" in
256 ! vm/quotations.cpp so it does not have a standard prolog
257 ! Unwind stack frames
260 ! Load VM pointer into vm-reg, since we're entering from
262 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
264 ! Load ds and rs registers
268 ! Clear the fault flag
269 vm-reg vm-fault-flag-offset [+] 0 MOV
276 { fixnum+ [ [ ADD ] "overflow_fixnum_add" jit-overflow ] }
277 { fixnum- [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] }
290 arg1 tag-bits get SAR
293 "overflow_fixnum_multiply" jit-call
308 RSP [] arg1 16-bit-version-of MOV
313 ! Load callstack object
315 ds-reg bootstrap-cell SUB
316 ! Get ctx->callstack_bottom
318 arg1 ctx-reg context-callstack-bottom-offset [+] MOV
319 ! Get top of callstack object -- 'src' for memcpy
320 arg2 arg4 callstack-top-offset [+] LEA
321 ! Get callstack length, in bytes --- 'len' for memcpy
322 arg3 arg4 callstack-length-offset [+] MOV
323 arg3 tag-bits get SHR
324 ! Compute new stack pointer -- 'dst' for memcpy
326 ! Install new stack pointer
328 ! Call memcpy; arguments are now in the correct registers
329 ! Create register shadow area for Win64
331 "factor_memcpy" jit-call
332 ! Tear down register shadow area
334 ! Return with new callstack
337 } define-sub-primitives