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