]> gitweb.factorcode.org Git - factor.git/blob - basis/bootstrap/assembler/x86.64.factor
bootstrap.image: attempt to remove the SPECIAL-OBJECT: table
[factor.git] / basis / bootstrap / assembler / x86.64.factor
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 ;
7 IN: bootstrap.x86
8
9 8 \ cell set
10
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 ;
30
31 : jit-call ( name -- )
32     RAX 0 MOV f rc-absolute-cell rel-dlsym
33     RAX CALL ;
34
35 :: jit-call-1arg ( arg1s name -- )
36     arg1 arg1s MOV
37     name jit-call ;
38
39 :: jit-call-2arg ( arg1s arg2s name -- )
40     arg1 arg1s MOV
41     arg2 arg2s MOV
42     name jit-call ;
43
44 [
45     pic-tail-reg 5 [RIP+] LEA
46     0 JMP f rc-relative rel-word-pic-tail
47 ] JIT-WORD-JUMP jit-define
48
49 : jit-load-vm ( -- )
50     ! no-op on x86-64. in factor contexts vm-reg always contains the
51     ! vm pointer.
52     ;
53
54 : jit-load-context ( -- )
55     ctx-reg vm-reg vm-context-offset [+] MOV ;
56
57 : jit-save-context ( -- )
58     jit-load-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.
62     R11 RSP -8 [+] LEA
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 ;
66
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 ;
71
72 [
73     ! ctx-reg is preserved across the call because it is non-volatile
74     ! in the C ABI
75     jit-save-context
76     ! call the primitive
77     arg1 vm-reg MOV
78     RAX 0 MOV f f rc-absolute-cell rel-dlsym
79     RAX CALL
80     jit-restore-context
81 ] JIT-PRIMITIVE jit-define
82
83 : jit-jump-quot ( -- )
84     arg1 quot-entry-point-offset [+] JMP ;
85
86 : jit-call-quot ( -- ) arg1 quot-entry-point-offset [+] CALL ;
87
88 [
89     arg2 arg1 MOV
90     vm-reg "begin_callback" jit-call-1arg
91
92     ! call the quotation
93     arg1 return-reg MOV
94     jit-call-quot
95
96     vm-reg "end_callback" jit-call-1arg
97 ] \ c-to-factor define-sub-primitive
98
99 : signal-handler-save-regs ( -- regs )
100     { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15 } ;
101
102 [
103     arg1 ds-reg [] MOV
104     ds-reg bootstrap-cell SUB
105 ]
106 [ jit-call-quot ]
107 [ jit-jump-quot ]
108 \ (call) define-combinator-primitive
109
110 [
111     ! Unwind stack frames
112     RSP arg2 MOV
113
114     ! Load VM pointer into vm-reg, since we're entering from
115     ! C code
116     vm-reg 0 MOV 0 rc-absolute-cell rel-vm
117
118     ! Load ds and rs registers
119     jit-load-context
120     jit-restore-context
121
122     ! Clear the fault flag
123     vm-reg vm-fault-flag-offset [+] 0 MOV
124
125     ! Call quotation
126     jit-jump-quot
127 ] \ unwind-native-frames define-sub-primitive
128
129 [
130     RSP 2 SUB
131     RSP [] FNSTCW
132     FNINIT
133     AX RSP [] MOV
134     RSP 2 ADD
135 ] \ fpu-state define-sub-primitive
136
137 [
138     RSP 2 SUB
139     RSP [] arg1 16-bit-version-of MOV
140     RSP [] FLDCW
141     RSP 2 ADD
142 ] \ set-fpu-state define-sub-primitive
143
144 [
145     ! Load callstack object
146     arg4 ds-reg [] MOV
147     ds-reg bootstrap-cell SUB
148     ! Get ctx->callstack_bottom
149     jit-load-context
150     arg1 ctx-reg context-callstack-bottom-offset [+] MOV
151     ! Get top of callstack object -- 'src' for memcpy
152     arg2 arg4 callstack-top-offset [+] LEA
153     ! Get callstack length, in bytes --- 'len' for memcpy
154     arg3 arg4 callstack-length-offset [+] MOV
155     arg3 tag-bits get SHR
156     ! Compute new stack pointer -- 'dst' for memcpy
157     arg1 arg3 SUB
158     ! Install new stack pointer
159     RSP arg1 MOV
160     ! Call memcpy; arguments are now in the correct registers
161     ! Create register shadow area for Win64
162     RSP 32 SUB
163     "factor_memcpy" jit-call
164     ! Tear down register shadow area
165     RSP 32 ADD
166     ! Return with new callstack
167     0 RET
168 ] \ set-callstack define-sub-primitive
169
170 [
171     jit-save-context
172     arg2 vm-reg MOV
173     "lazy_jit_compile" jit-call
174     arg1 return-reg MOV
175 ]
176 [ return-reg quot-entry-point-offset [+] CALL ]
177 [ jit-jump-quot ]
178 \ lazy-jit-compile define-combinator-primitive
179
180 [
181     temp2 0xffffffff MOV f rc-absolute-cell rel-literal
182     temp1 temp2 CMP
183 ] PIC-CHECK-TUPLE jit-define
184
185 ! Inline cache miss entry points
186 : jit-load-return-address ( -- )
187     RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
188
189 ! These are always in tail position with an existing stack
190 ! frame, and the stack. The frame setup takes this into account.
191 : jit-inline-cache-miss ( -- )
192     jit-save-context
193     arg1 RBX MOV
194     arg2 vm-reg MOV
195     RAX 0 MOV rc-absolute-cell rel-inline-cache-miss
196     RAX CALL
197     jit-load-context
198     jit-restore-context ;
199
200 [ jit-load-return-address jit-inline-cache-miss ]
201 [ RAX CALL ]
202 [ RAX JMP ]
203 \ inline-cache-miss define-combinator-primitive
204
205 [ jit-inline-cache-miss ]
206 [ RAX CALL ]
207 [ RAX JMP ]
208 \ inline-cache-miss-tail define-combinator-primitive
209
210 ! Overflowing fixnum arithmetic
211 : jit-overflow ( insn func -- )
212     ds-reg 8 SUB
213     jit-save-context
214     arg1 ds-reg [] MOV
215     arg2 ds-reg 8 [+] MOV
216     arg3 arg1 MOV
217     [ [ arg3 arg2 ] dip call ] dip
218     ds-reg [] arg3 MOV
219     [ JNO ]
220     [ arg3 vm-reg MOV jit-call ]
221     jit-conditional ; inline
222
223 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
224
225 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
226
227 [
228     ds-reg 8 SUB
229     jit-save-context
230     RCX ds-reg [] MOV
231     RBX ds-reg 8 [+] MOV
232     RBX tag-bits get SAR
233     RAX RCX MOV
234     RBX IMUL
235     ds-reg [] RAX MOV
236     [ JNO ]
237     [
238         arg1 RCX MOV
239         arg1 tag-bits get SAR
240         arg2 RBX MOV
241         arg3 vm-reg MOV
242         "overflow_fixnum_multiply" jit-call
243     ]
244     jit-conditional
245 ] \ fixnum* define-sub-primitive
246
247 ! Contexts
248 : jit-switch-context ( reg -- )
249     ! Push a bogus return address so the GC can track this frame back
250     ! to the owner
251     0 CALL
252
253     ! Make the new context the current one
254     ctx-reg swap MOV
255     vm-reg vm-context-offset [+] ctx-reg MOV
256
257     ! Load new stack pointer
258     RSP ctx-reg context-callstack-top-offset [+] MOV
259
260     ! Load new ds, rs registers
261     jit-restore-context
262
263     ctx-reg jit-update-tib ;
264
265 : jit-pop-context-and-param ( -- )
266     arg1 ds-reg [] MOV
267     arg1 arg1 alien-offset [+] MOV
268     arg2 ds-reg -8 [+] MOV
269     ds-reg 16 SUB ;
270
271 : jit-push-param ( -- )
272     ds-reg 8 ADD
273     ds-reg [] arg2 MOV ;
274
275 : jit-set-context ( -- )
276     jit-pop-context-and-param
277     jit-save-context
278     arg1 jit-switch-context
279     RSP 8 ADD
280     jit-push-param ;
281
282 [ jit-set-context ] \ (set-context) define-sub-primitive
283
284 : jit-pop-quot-and-param ( -- )
285     arg1 ds-reg [] MOV
286     arg2 ds-reg -8 [+] MOV
287     ds-reg 16 SUB ;
288
289 : jit-start-context ( -- )
290     ! Create the new context in return-reg. Have to save context
291     ! twice, first before calling new_context() which may GC,
292     ! and again after popping the two parameters from the stack.
293     jit-save-context
294     vm-reg "new_context" jit-call-1arg
295
296     jit-pop-quot-and-param
297     jit-save-context
298     return-reg jit-switch-context
299     jit-push-param
300     jit-jump-quot ;
301
302 [ jit-start-context ] \ (start-context) define-sub-primitive
303
304 : jit-delete-current-context ( -- )
305     vm-reg "delete_context" jit-call-1arg ;
306
307 [
308     jit-delete-current-context
309     jit-set-context
310 ] \ (set-context-and-delete) define-sub-primitive
311
312 ! Resets the active context and instead the passed in quotation
313 ! becomes the new code that it executes.
314 : jit-start-context-and-delete ( -- )
315     ! Updates the context to match the values in the data and retain
316     ! stack registers. reset_context can GC.
317     jit-save-context
318
319     ! Resets the context. The top two ds items are preserved.
320     vm-reg "reset_context" jit-call-1arg
321
322     ! Switches to the same context I think.
323     ctx-reg jit-switch-context
324
325     ! Pops the quotation from the stack and puts it in arg1.
326     arg1 ds-reg [] MOV
327     ds-reg 8 SUB
328
329     ! Jump to quotation arg1
330     jit-jump-quot ;
331
332 [
333     0 [RIP+] EAX MOV rc-relative rel-safepoint
334 ] JIT-SAFEPOINT jit-define
335
336 [
337     jit-start-context-and-delete
338 ] \ (start-context-and-delete) define-sub-primitive