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 locals math
6 math.private namespaces threads.private ;
11 : signal-handler-stack-frame-size ( -- n ) 12 bootstrap-cells ;
12 : stack-frame-size ( -- n ) 8 bootstrap-cells ;
13 : shift-arg ( -- reg ) ECX ;
14 : div-arg ( -- reg ) EAX ;
15 : mod-arg ( -- reg ) EDX ;
16 : temp0 ( -- reg ) EAX ;
17 : temp1 ( -- reg ) ECX ;
18 : temp2 ( -- reg ) EBX ;
19 : temp3 ( -- reg ) EDX ;
20 : pic-tail-reg ( -- reg ) EDX ;
21 : stack-reg ( -- reg ) ESP ;
22 : frame-reg ( -- reg ) EBP ;
23 : vm-reg ( -- reg ) EBX ;
24 : ctx-reg ( -- reg ) EBP ;
25 : nv-regs ( -- seq ) { ESI EDI EBX } ;
26 : volatile-regs ( -- seq ) { EAX ECX EDX } ;
27 : nv-reg ( -- reg ) ESI ;
28 : ds-reg ( -- reg ) ESI ;
29 : rs-reg ( -- reg ) EDI ;
30 : link-reg ( -- reg ) EBX ;
31 : fixnum>slot@ ( -- ) temp0 2 SAR ;
32 : rex-length ( -- n ) 0 ;
33 : red-zone-size ( -- n ) 0 ;
35 : jit-call ( name -- )
36 0 CALL f rc-relative rel-dlsym ;
38 :: jit-call-1arg ( arg1s name -- )
42 :: jit-call-2arg ( arg1s arg2s name -- )
47 :: jit-call-3arg ( arg1s arg2s arg3s name -- )
54 pic-tail-reg 0 MOV 0 rc-absolute-cell rel-here
55 0 JMP f rc-relative rel-word-pic-tail
56 ] JIT-WORD-JUMP jit-define
59 vm-reg 0 MOV 0 rc-absolute-cell rel-vm ;
61 : jit-load-context ( -- )
62 ! VM pointer must be in vm-reg already
63 ctx-reg vm-reg vm-context-offset [+] MOV ;
65 : jit-save-context ( -- )
68 ctx-reg context-callstack-top-offset [+] ECX MOV
69 ctx-reg context-datastack-offset [+] ds-reg MOV
70 ctx-reg context-retainstack-offset [+] rs-reg MOV ;
72 : jit-restore-context ( -- )
73 ds-reg ctx-reg context-datastack-offset [+] MOV
74 rs-reg ctx-reg context-retainstack-offset [+] MOV ;
77 ! ctx-reg is preserved across the call because it is
78 ! non-volatile in the C ABI
83 0 CALL f f rc-relative rel-dlsym
85 ] JIT-PRIMITIVE jit-define
87 : jit-jump-quot ( -- )
88 EAX quot-entry-point-offset [+] JMP ;
90 : jit-call-quot ( -- )
91 EAX quot-entry-point-offset [+] CALL ;
96 vm-reg EAX "begin_callback" jit-call-2arg
101 vm-reg "end_callback" jit-call-1arg
102 ] \ c-to-factor define-sub-primitive
104 : signal-handler-save-regs ( -- regs )
105 { EAX EBX ECX EDX EBP EDI ESI } ;
109 ds-reg bootstrap-cell SUB
113 \ (call) define-combinator-primitive
115 ! unwind-native-frames is marked as "special" in vm/quotations.cpp
116 ! so it does not have a standard prolog
118 ! Load ds and rs registers
123 ! clear the fault flag
124 vm-reg vm-fault-flag-offset [+] 0 MOV
126 ! Windows-specific setup
127 ctx-reg jit-update-seh
130 EAX ESP bootstrap-cell [+] MOV
131 EDX ESP 2 bootstrap-cells [+] MOV
133 ! Unwind stack frames
137 ] \ unwind-native-frames define-sub-primitive
145 ] \ fpu-state define-sub-primitive
148 ESP stack-frame-size [+] FLDCW
149 ] \ set-fpu-state define-sub-primitive
152 ! Load callstack object
154 ds-reg bootstrap-cell SUB
155 ! Get ctx->callstack_bottom
158 temp0 ctx-reg context-callstack-bottom-offset [+] MOV
159 ! Get top of callstack object -- 'src' for memcpy
160 temp1 temp3 callstack-top-offset [+] LEA
161 ! Get callstack length, in bytes --- 'len' for memcpy
162 temp2 temp3 callstack-length-offset [+] MOV
163 temp2 tag-bits get SHR
164 ! Compute new stack pointer -- 'dst' for memcpy
166 ! Install new stack pointer
172 "factor_memcpy" jit-call
174 ! Return with new callstack
176 ] \ set-callstack define-sub-primitive
182 ! Call VM, quotation reference is in EAX
183 EAX vm-reg "lazy_jit_compile" jit-call-2arg
187 \ lazy-jit-compile define-combinator-primitive
190 temp1 0xffffffff CMP f rc-absolute-cell rel-literal
191 ] PIC-CHECK-TUPLE jit-define
193 ! Inline cache miss entry points
194 : jit-load-return-address ( -- )
195 pic-tail-reg ESP stack-frame-size bootstrap-cell - [+] MOV ;
197 ! These are always in tail position with an existing stack
198 ! frame, and the stack. The frame setup takes this into account.
199 : jit-inline-cache-miss ( -- )
203 ESP [] pic-tail-reg MOV
204 0 CALL rc-relative rel-inline-cache-miss
205 jit-restore-context ;
207 [ jit-load-return-address jit-inline-cache-miss ]
210 \ inline-cache-miss define-combinator-primitive
212 [ jit-inline-cache-miss ]
215 \ inline-cache-miss-tail define-combinator-primitive
217 ! Overflowing fixnum arithmetic
218 : jit-overflow ( insn func -- )
225 [ [ EBX EDX ] dip call( dst src -- ) ] dip
237 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
239 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
255 ECX EBP vm-reg "overflow_fixnum_multiply" jit-call-3arg
258 ] \ fixnum* define-sub-primitive
261 : jit-switch-context ( reg -- )
262 ! Push a bogus return address so the GC can track this frame back
266 ! Make the new context the current one
268 vm-reg vm-context-offset [+] ctx-reg MOV
270 ! Load new stack pointer
271 ESP ctx-reg context-callstack-top-offset [+] MOV
273 ! Windows-specific setup
274 ctx-reg jit-update-tib
276 ! Load new ds, rs registers
277 jit-restore-context ;
279 : jit-set-context ( -- )
280 ! Load context and parameter from datastack
282 EAX EAX alien-offset [+] MOV
283 EDX ds-reg -4 [+] MOV
286 ! Save ds, rs registers
290 ! Make the new context active
291 EAX jit-switch-context
293 ! Windows-specific setup
294 ctx-reg jit-update-seh
296 ! Twiddle stack for return
299 ! Store parameter to datastack
303 [ jit-set-context ] \ (set-context) define-sub-primitive
305 : jit-save-quot-and-param ( -- )
309 : jit-push-param ( -- )
314 : jit-start-context ( -- )
315 ! Create the new context in return-reg
318 vm-reg "new_context" jit-call-1arg
320 jit-save-quot-and-param
322 ! Make the new context active
325 EAX jit-switch-context
329 ! Windows-specific setup
332 ! Push a fake return address
335 ! Jump to initial quotation
339 [ jit-start-context ] \ (start-context) define-sub-primitive
341 : jit-delete-current-context ( -- )
344 vm-reg "delete_context" jit-call-1arg ;
347 jit-delete-current-context
349 ] \ (set-context-and-delete) define-sub-primitive
351 : jit-start-context-and-delete ( -- )
354 ! Updates the context to match the values in the data and retain
355 ! stack registers. reset_context can GC.
358 ! Resets the context. The top two ds item are preserved.
359 vm-reg "reset_context" jit-call-1arg
361 ! Switches to the same context I think, uses ctx-reg
362 ctx-reg jit-switch-context
364 ! Pops the quotation from the stack and puts it in EAX.
368 ! Jump to the quotation in EAX.
372 0 EAX MOVABS rc-absolute rel-safepoint
373 ] JIT-SAFEPOINT jit-define
376 jit-start-context-and-delete
377 ] \ (start-context-and-delete) define-sub-primitive