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