]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/64/64.factor
moved %(un)nest-stacks out to cpu specific files to eliminate %vm-invoke from compile...
[factor.git] / basis / cpu / x86 / 64 / 64.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel math namespaces make sequences system
4 layouts alien alien.c-types alien.accessors slots
5 splitting assocs combinators locals compiler.constants
6 compiler.codegen compiler.codegen.fixup compiler.cfg.instructions
7 compiler.cfg.builder compiler.cfg.intrinsics compiler.cfg.stack-frame
8 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86 cpu.architecture ;
9 IN: cpu.x86.64
10
11 M: x86.64 machine-registers
12     {
13         { int-regs { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 } }
14         { float-regs {
15             XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7
16             XMM8 XMM9 XMM10 XMM11 XMM12 XMM13 XMM14 XMM15
17         } }
18     } ;
19
20 M: x86.64 ds-reg R14 ;
21 M: x86.64 rs-reg R15 ;
22 M: x86.64 stack-reg RSP ;
23
24 M:: x86.64 %dispatch ( src temp -- )
25     building get length :> start
26     ! Load jump table base.
27     temp HEX: ffffffff MOV
28     0 rc-absolute-cell rel-here
29     ! Add jump table base
30     temp src ADD
31     temp HEX: 7f [+] JMP
32     building get length :> end
33     ! Fix up the displacement above
34     cell code-alignment
35     [ end start - 2 - + building get dup pop* push ]
36     [ align-code ]
37     bi ;
38
39 : param-reg-1 ( -- reg ) int-regs param-regs first ; inline
40 : param-reg-2 ( -- reg ) int-regs param-regs second ; inline
41 : param-reg-3 ( -- reg ) int-regs param-regs third ; inline
42
43 M: x86.64 pic-tail-reg RBX ;
44
45 M: int-regs return-reg drop RAX ;
46 M: float-regs return-reg drop XMM0 ;
47
48 M: x86.64 %prologue ( n -- )
49     temp-reg 0 MOV rc-absolute-cell rel-this
50     dup PUSH
51     temp-reg PUSH
52     stack-reg swap 3 cells - SUB ;
53
54 M: stack-params copy-register*
55     drop
56     {
57         { [ dup  integer? ] [ R11 swap next-stack@ MOV  R11 MOV ] }
58         { [ over integer? ] [ R11 swap MOV              param@ R11 MOV ] }
59     } cond ;
60
61 M: x86 %save-param-reg [ param@ ] 2dip %copy ;
62
63 M: x86 %load-param-reg [ swap param@ ] dip %copy ;
64
65 : with-return-regs ( quot -- )
66     [
67         V{ RDX RAX } clone int-regs set
68         V{ XMM1 XMM0 } clone float-regs set
69         call
70     ] with-scope ; inline
71
72 M: x86.64 %prepare-unbox ( -- )
73     ! First parameter is top of stack
74     param-reg-1 R14 [] MOV
75     R14 cell SUB ;
76
77 : %vm-invoke-1st-arg ( function -- )
78     param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup
79     f %alien-invoke ;
80
81 : %vm-invoke-2nd-arg ( function -- )
82     param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup
83     f %alien-invoke ;
84
85 : %vm-invoke-3rd-arg ( function -- )
86     param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup
87     f %alien-invoke ;
88
89 : %vm-invoke-4th-arg ( function -- )
90     int-regs param-regs fourth 0 MOV rc-absolute-cell rt-vm rel-fixup
91     f %alien-invoke ;
92
93
94 M:: x86.64 %unbox ( n rep func -- )
95     ! Call the unboxer
96     func %vm-invoke-2nd-arg
97     ! Store the return value on the C stack if this is an
98     ! alien-invoke, otherwise leave it the return register if
99     ! this is the end of alien-callback
100     n [ n rep reg-class-of return-reg rep %save-param-reg ] when ;
101
102 M: x86.64 %unbox-long-long ( n func -- )
103     [ int-rep ] dip %unbox ;
104
105 : %unbox-struct-field ( c-type i -- )
106     ! Alien must be in param-reg-1.
107     R11 swap cells [+] swap rep>> reg-class-of {
108         { int-regs [ int-regs get pop swap MOV ] }
109         { float-regs [ float-regs get pop swap MOVSD ] }
110     } case ;
111
112
113 M: x86.64 %unbox-small-struct ( c-type -- )
114     ! Alien must be in param-reg-1.
115     "alien_offset" %vm-invoke-2nd-arg
116     ! Move alien_offset() return value to R11 so that we don't
117     ! clobber it.
118     R11 RAX MOV
119     [
120         flatten-value-type [ %unbox-struct-field ] each-index
121     ] with-return-regs ;
122
123 M:: x86.64 %unbox-large-struct ( n c-type -- )
124     ! Source is in param-reg-1
125     ! Load destination address into param-reg-2
126     param-reg-2 n param@ LEA
127     ! Load structure size into param-reg-3
128     param-reg-3 c-type heap-size MOV
129     ! Copy the struct to the C stack
130     "to_value_struct" %vm-invoke-4th-arg ;
131
132 : load-return-value ( rep -- )
133     [ [ 0 ] dip reg-class-of param-reg ]
134     [ reg-class-of return-reg ]
135     [ ]
136     tri %copy ;
137
138 M:: x86.64 %box ( n rep func -- )
139     n [
140         n
141         0 rep reg-class-of param-reg
142         rep %load-param-reg
143     ] [
144         rep load-return-value
145     ] if
146     rep int-rep? [ func %vm-invoke-2nd-arg ] [ func %vm-invoke-1st-arg ] if ;
147
148 M: x86.64 %box-long-long ( n func -- )
149     [ int-rep ] dip %box ;
150
151 : box-struct-field@ ( i -- operand ) 1 + cells param@ ;
152
153 : %box-struct-field ( c-type i -- )
154     box-struct-field@ swap c-type-rep reg-class-of {
155         { int-regs [ int-regs get pop MOV ] }
156         { float-regs [ float-regs get pop MOVSD ] }
157     } case ;
158
159 M: x86.64 %box-small-struct ( c-type -- )
160     #! Box a <= 16-byte struct.
161     [
162         [ flatten-value-type [ %box-struct-field ] each-index ]
163         [ param-reg-3 swap heap-size MOV ] bi
164         param-reg-1 0 box-struct-field@ MOV
165         param-reg-2 1 box-struct-field@ MOV
166         "box_small_struct" %vm-invoke-4th-arg
167     ] with-return-regs ;
168
169 : struct-return@ ( n -- operand )
170     [ stack-frame get params>> ] unless* param@ ;
171
172 M: x86.64 %box-large-struct ( n c-type -- )
173     ! Struct size is parameter 2
174     param-reg-2 swap heap-size MOV
175     ! Compute destination address
176     param-reg-1 swap struct-return@ LEA
177     ! Copy the struct from the C stack
178     "box_value_struct" %vm-invoke-3rd-arg ;
179
180 M: x86.64 %prepare-box-struct ( -- )
181     ! Compute target address for value struct return
182     RAX f struct-return@ LEA
183     ! Store it as the first parameter
184     0 param@ RAX MOV ;
185
186 M: x86.64 %prepare-var-args RAX RAX XOR ;
187
188 M: x86.64 %alien-invoke
189     R11 0 MOV
190     rc-absolute-cell rel-dlsym
191     R11 CALL ;
192
193 M: x86.64 %nest-stacks ( -- )
194     param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup
195     "nest_stacks" f %alien-invoke ;
196
197 M: x86.64 %unnest-stacks ( -- )
198     param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup
199     "unnest_stacks" f %alien-invoke ;
200
201 M: x86.64 %prepare-alien-indirect ( -- )
202     "unbox_alien" %vm-invoke-1st-arg
203     RBP RAX MOV ;
204
205 M: x86.64 %alien-indirect ( -- )
206     RBP CALL ;
207
208 M: x86.64 %alien-callback ( quot -- )
209     param-reg-1 swap %load-reference
210     "c_to_factor" %vm-invoke-2nd-arg ;
211
212 M: x86.64 %callback-value ( ctype -- )
213     ! Save top of data stack
214     %prepare-unbox
215     ! Save top of data stack
216     RSP 8 SUB
217     param-reg-1 PUSH
218     ! Restore data/call/retain stacks
219     "unnest_stacks" %vm-invoke-1st-arg
220     ! Put former top of data stack in param-reg-1
221     param-reg-1 POP
222     RSP 8 ADD
223     ! Unbox former top of data stack to return registers
224     unbox-return ;
225
226 : float-function-param ( i spill-slot -- )
227     [ float-regs param-regs nth ] [ n>> spill@ ] bi* MOVSD ;
228
229 : float-function-return ( reg -- )
230     float-regs return-reg double-rep %copy ;
231
232 M:: x86.64 %unary-float-function ( dst src func -- )
233     0 src float-function-param
234     func f %alien-invoke
235     dst float-function-return ;
236
237 M:: x86.64 %binary-float-function ( dst src1 src2 func -- )
238     0 src1 float-function-param
239     1 src2 float-function-param
240     func f %alien-invoke
241     dst float-function-return ;
242
243 M:: x86.64 %call-gc ( gc-root-count temp1 -- )
244     ! Pass pointer to start of GC roots as first parameter
245     param-reg-1 gc-root-base param@ LEA
246     ! Pass number of roots as second parameter
247     param-reg-2 gc-root-count MOV
248     ! Pass vm as third parameter
249     param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup
250     ! Call GC
251     "inline_gc" f %alien-invoke ;
252
253 ! The result of reading 4 bytes from memory is a fixnum on
254 ! x86-64.
255 enable-alien-4-intrinsics
256
257 ! Enable fast calling of libc math functions
258 enable-float-functions
259
260 USE: vocabs.loader
261
262 {
263     { [ os unix? ] [ "cpu.x86.64.unix" require ] }
264     { [ os winnt? ] [ "cpu.x86.64.winnt" require ] }
265 } cond
266
267 check-sse