]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/64/bootstrap.factor
FFI rewrite part 6: deconcatenatize
[factor.git] / basis / cpu / x86 / 64 / bootstrap.factor
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 ;
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 rc-absolute-cell jit-dlsym
33     RAX CALL ;
34
35 [
36     ! load entry point
37     RAX 0 MOV rc-absolute-cell rt-this jit-rel
38     ! save stack frame size
39     stack-frame-size PUSH
40     ! push entry point
41     RAX PUSH
42     ! alignment
43     RSP stack-frame-size 3 bootstrap-cells - SUB
44 ] jit-prolog jit-define
45
46 [
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
50
51 : jit-load-context ( -- )
52     ctx-reg vm-reg vm-context-offset [+] MOV ;
53
54 : jit-save-context ( -- )
55     jit-load-context
56     R11 RSP -8 [+] LEA
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 ;
60
61 : jit-restore-context ( -- )
62     ds-reg ctx-reg context-datastack-offset [+] MOV
63     rs-reg ctx-reg context-retainstack-offset [+] MOV ;
64
65 : jit-scrub-return ( n -- )
66     RSP swap [+] 0 MOV ;
67
68 [
69     ! ctx-reg is preserved across the call because it is non-volatile
70     ! in the C ABI
71     jit-save-context
72     ! call the primitive
73     arg1 vm-reg MOV
74     RAX 0 MOV rc-absolute-cell rt-dlsym jit-rel
75     RAX CALL
76     jit-restore-context
77 ] jit-primitive jit-define
78
79 : jit-jump-quot ( -- ) arg1 quot-entry-point-offset [+] JMP ;
80
81 : jit-call-quot ( -- ) arg1 quot-entry-point-offset [+] CALL ;
82
83 [
84     arg2 arg1 MOV
85     arg1 vm-reg MOV
86     "begin_callback" jit-call
87
88     ! call the quotation
89     arg1 return-reg MOV
90     jit-call-quot
91
92     arg1 vm-reg MOV
93     "end_callback" jit-call
94 ] \ c-to-factor define-sub-primitive
95
96 [
97     arg1 ds-reg [] MOV
98     ds-reg bootstrap-cell SUB
99 ]
100 [ jit-call-quot ]
101 [ jit-jump-quot ]
102 \ (call) define-combinator-primitive
103
104 [
105     ! Clear x87 stack, but preserve rounding mode and exception flags
106     RSP 2 SUB
107     RSP [] FNSTCW
108     FNINIT
109     RSP [] FLDCW
110
111     ! Unwind stack frames
112     RSP arg2 MOV
113     0 jit-scrub-return
114
115     ! Load VM pointer into vm-reg, since we're entering from
116     ! C code
117     vm-reg 0 MOV 0 rc-absolute-cell jit-vm
118
119     ! Load ds and rs registers
120     jit-load-context
121     jit-restore-context
122
123     ! Call quotation
124     jit-jump-quot
125 ] \ unwind-native-frames define-sub-primitive
126
127 [
128     ! Load callstack object
129     arg4 ds-reg [] MOV
130     ds-reg bootstrap-cell SUB
131     ! Get ctx->callstack_bottom
132     jit-load-context
133     arg1 ctx-reg context-callstack-bottom-offset [+] MOV
134     ! Get top of callstack object -- 'src' for memcpy
135     arg2 arg4 callstack-top-offset [+] LEA
136     ! Get callstack length, in bytes --- 'len' for memcpy
137     arg3 arg4 callstack-length-offset [+] MOV
138     arg3 tag-bits get SHR
139     ! Compute new stack pointer -- 'dst' for memcpy
140     arg1 arg3 SUB
141     ! Install new stack pointer
142     RSP arg1 MOV
143     ! Call memcpy; arguments are now in the correct registers
144     ! Create register shadow area for Win64
145     RSP 32 SUB
146     "factor_memcpy" jit-call
147     ! Tear down register shadow area
148     RSP 32 ADD
149     ! Return with new callstack
150     0 RET
151 ] \ set-callstack define-sub-primitive
152
153 [
154     jit-save-context
155     arg2 vm-reg MOV
156     "lazy_jit_compile" jit-call
157     arg1 return-reg MOV
158 ]
159 [ return-reg quot-entry-point-offset [+] CALL ]
160 [ jit-jump-quot ]
161 \ lazy-jit-compile define-combinator-primitive
162
163 [
164     temp2 HEX: ffffffff MOV rc-absolute-cell rt-literal jit-rel
165     temp1 temp2 CMP
166 ] pic-check-tuple jit-define
167
168 ! Inline cache miss entry points
169 : jit-load-return-address ( -- )
170     RBX RSP stack-frame-size bootstrap-cell - [+] MOV ;
171
172 ! These are always in tail position with an existing stack
173 ! frame, and the stack. The frame setup takes this into account.
174 : jit-inline-cache-miss ( -- )
175     jit-save-context
176     arg1 RBX MOV
177     arg2 vm-reg MOV
178     "inline_cache_miss" jit-call
179     jit-load-context
180     jit-restore-context ;
181
182 [ jit-load-return-address jit-inline-cache-miss ]
183 [ RAX CALL ]
184 [ RAX JMP ]
185 \ inline-cache-miss define-combinator-primitive
186
187 [ jit-inline-cache-miss ]
188 [ RAX CALL ]
189 [ RAX JMP ]
190 \ inline-cache-miss-tail define-combinator-primitive
191
192 ! Overflowing fixnum arithmetic
193 : jit-overflow ( insn func -- )
194     ds-reg 8 SUB
195     jit-save-context
196     arg1 ds-reg [] MOV
197     arg2 ds-reg 8 [+] MOV
198     arg3 arg1 MOV
199     [ [ arg3 arg2 ] dip call ] dip
200     ds-reg [] arg3 MOV
201     [ JNO ]
202     [ arg3 vm-reg MOV jit-call ]
203     jit-conditional ; inline
204
205 [ [ ADD ] "overflow_fixnum_add" jit-overflow ] \ fixnum+ define-sub-primitive
206
207 [ [ SUB ] "overflow_fixnum_subtract" jit-overflow ] \ fixnum- define-sub-primitive
208
209 [
210     ds-reg 8 SUB
211     jit-save-context
212     RCX ds-reg [] MOV
213     RBX ds-reg 8 [+] MOV
214     RBX tag-bits get SAR
215     RAX RCX MOV
216     RBX IMUL
217     ds-reg [] RAX MOV
218     [ JNO ]
219     [
220         arg1 RCX MOV
221         arg1 tag-bits get SAR
222         arg2 RBX MOV
223         arg3 vm-reg MOV
224         "overflow_fixnum_multiply" jit-call
225     ]
226     jit-conditional
227 ] \ fixnum* define-sub-primitive
228
229 ! Contexts
230 : jit-switch-context ( reg -- )
231     -8 jit-scrub-return
232
233     ! Save ds, rs registers
234     jit-save-context
235
236     ! Make the new context the current one
237     ctx-reg swap MOV
238     vm-reg vm-context-offset [+] ctx-reg MOV
239
240     ! Load new stack pointer
241     RSP ctx-reg context-callstack-top-offset [+] MOV
242
243     ! Load new ds, rs registers
244     jit-restore-context
245
246     ctx-reg jit-update-tib ;
247
248 : jit-pop-context-and-param ( -- )
249     arg1 ds-reg [] MOV
250     arg1 arg1 alien-offset [+] MOV
251     arg2 ds-reg -8 [+] MOV
252     ds-reg 16 SUB ;
253
254 : jit-push-param ( -- )
255     ds-reg 8 ADD
256     ds-reg [] arg2 MOV ;
257
258 : jit-set-context ( -- )
259     jit-pop-context-and-param
260     arg1 jit-switch-context
261     RSP 8 ADD
262     jit-push-param ;
263
264 [ jit-set-context ] \ (set-context) define-sub-primitive
265
266 : jit-pop-quot-and-param ( -- )
267     arg1 ds-reg [] MOV
268     arg2 ds-reg -8 [+] MOV
269     ds-reg 16 SUB ;
270
271 : jit-start-context ( -- )
272     ! Create the new context in return-reg
273     arg1 vm-reg MOV
274     "new_context" jit-call
275
276     jit-pop-quot-and-param
277
278     return-reg jit-switch-context
279
280     jit-push-param
281
282     jit-jump-quot ;
283
284 [ jit-start-context ] \ (start-context) define-sub-primitive
285
286 : jit-delete-current-context ( -- )
287     jit-load-context
288     arg1 vm-reg MOV
289     arg2 ctx-reg MOV
290     "delete_context" jit-call ;
291
292 [
293     jit-delete-current-context
294     jit-set-context
295 ] \ (set-context-and-delete) define-sub-primitive
296
297 [
298     jit-delete-current-context
299     jit-start-context
300 ] \ (start-context-and-delete) define-sub-primitive