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 ;
11 M: x86.64 machine-registers
13 { int-regs { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 } }
15 XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7
16 XMM8 XMM9 XMM10 XMM11 XMM12 XMM13 XMM14 XMM15
20 M: x86.64 ds-reg R14 ;
21 M: x86.64 rs-reg R15 ;
22 M: x86.64 stack-reg RSP ;
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
32 building get length :> end
33 ! Fix up the displacement above
35 [ end start - 2 - + building get dup pop* push ]
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 : param-reg-4 ( -- reg ) int-regs param-regs fourth ; inline
44 M: x86.64 pic-tail-reg RBX ;
46 M: int-regs return-reg drop RAX ;
47 M: float-regs return-reg drop XMM0 ;
49 M: x86.64 %prologue ( n -- )
50 temp-reg 0 MOV rc-absolute-cell rel-this
53 stack-reg swap 3 cells - SUB ;
55 M: stack-params copy-register*
58 { [ dup integer? ] [ R11 swap next-stack@ MOV R11 MOV ] }
59 { [ over integer? ] [ R11 swap MOV param@ R11 MOV ] }
62 M: x86 %save-param-reg [ param@ ] 2dip %copy ;
64 M: x86 %load-param-reg [ swap param@ ] dip %copy ;
66 : with-return-regs ( quot -- )
68 V{ RDX RAX } clone int-regs set
69 V{ XMM1 XMM0 } clone float-regs set
73 M: x86.64 %prepare-unbox ( -- )
74 ! First parameter is top of stack
75 param-reg-1 R14 [] MOV
78 : %mov-vm-ptr ( reg -- )
79 0 MOV rc-absolute-cell rt-vm rel-fixup ;
81 M:: x86.64 %unbox ( n rep func -- )
82 param-reg-2 %mov-vm-ptr
85 ! Store the return value on the C stack if this is an
86 ! alien-invoke, otherwise leave it the return register if
87 ! this is the end of alien-callback
88 n [ n rep reg-class-of return-reg rep %save-param-reg ] when ;
90 M: x86.64 %unbox-long-long ( n func -- )
91 [ int-rep ] dip %unbox ;
93 : %unbox-struct-field ( c-type i -- )
94 ! Alien must be in param-reg-1.
95 R11 swap cells [+] swap rep>> reg-class-of {
96 { int-regs [ int-regs get pop swap MOV ] }
97 { float-regs [ float-regs get pop swap MOVSD ] }
100 M: x86.64 %unbox-small-struct ( c-type -- )
101 ! Alien must be in param-reg-1.
102 param-reg-2 %mov-vm-ptr
103 "alien_offset" f %alien-invoke
104 ! Move alien_offset() return value to R11 so that we don't
108 flatten-value-type [ %unbox-struct-field ] each-index
111 M:: x86.64 %unbox-large-struct ( n c-type -- )
112 ! Source is in param-reg-1
113 ! Load destination address into param-reg-2
114 param-reg-2 n param@ LEA
115 ! Load structure size into param-reg-3
116 param-reg-3 c-type heap-size MOV
117 param-reg-4 %mov-vm-ptr
118 ! Copy the struct to the C stack
119 "to_value_struct" f %alien-invoke ;
121 : load-return-value ( rep -- )
122 [ [ 0 ] dip reg-class-of param-reg ]
123 [ reg-class-of return-reg ]
127 M:: x86.64 %box ( n rep func -- )
130 0 rep reg-class-of param-reg
133 rep load-return-value
135 rep int-rep? [ param-reg-2 ] [ param-reg-1 ] if %mov-vm-ptr
136 func f %alien-invoke ;
138 M: x86.64 %box-long-long ( n func -- )
139 [ int-rep ] dip %box ;
141 : box-struct-field@ ( i -- operand ) 1 + cells param@ ;
143 : %box-struct-field ( c-type i -- )
144 box-struct-field@ swap c-type-rep reg-class-of {
145 { int-regs [ int-regs get pop MOV ] }
146 { float-regs [ float-regs get pop MOVSD ] }
149 M: x86.64 %box-small-struct ( c-type -- )
150 #! Box a <= 16-byte struct.
152 [ flatten-value-type [ %box-struct-field ] each-index ]
153 [ param-reg-3 swap heap-size MOV ] bi
154 param-reg-1 0 box-struct-field@ MOV
155 param-reg-2 1 box-struct-field@ MOV
156 param-reg-4 %mov-vm-ptr
157 "box_small_struct" f %alien-invoke
160 : struct-return@ ( n -- operand )
161 [ stack-frame get params>> ] unless* param@ ;
163 M: x86.64 %box-large-struct ( n c-type -- )
164 ! Struct size is parameter 2
165 param-reg-2 swap heap-size MOV
166 ! Compute destination address
167 param-reg-1 swap struct-return@ LEA
168 param-reg-3 %mov-vm-ptr
169 ! Copy the struct from the C stack
170 "box_value_struct" f %alien-invoke ;
172 M: x86.64 %prepare-box-struct ( -- )
173 ! Compute target address for value struct return
174 RAX f struct-return@ LEA
175 ! Store it as the first parameter
178 M: x86.64 %prepare-var-args RAX RAX XOR ;
180 M: x86.64 %alien-invoke
182 rc-absolute-cell rel-dlsym
185 M: x86.64 %nest-stacks ( -- )
186 param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup
187 "nest_stacks" f %alien-invoke ;
189 M: x86.64 %unnest-stacks ( -- )
190 param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup
191 "unnest_stacks" f %alien-invoke ;
193 M: x86.64 %prepare-alien-indirect ( -- )
194 param-reg-1 %mov-vm-ptr
195 "unbox_alien" f %alien-invoke
198 M: x86.64 %alien-indirect ( -- )
201 M: x86.64 %alien-callback ( quot -- )
202 param-reg-1 swap %load-reference
203 param-reg-2 %mov-vm-ptr
204 "c_to_factor" f %alien-invoke ;
206 M: x86.64 %callback-value ( ctype -- )
207 ! Save top of data stack
209 ! Save top of data stack
212 param-reg-1 %mov-vm-ptr
213 ! Restore data/call/retain stacks
214 "unnest_stacks" f %alien-invoke
215 ! Put former top of data stack in param-reg-1
218 ! Unbox former top of data stack to return registers
221 : float-function-param ( i src -- )
222 [ float-regs param-regs nth ] dip double-rep %copy ;
224 : float-function-return ( reg -- )
225 float-regs return-reg double-rep %copy ;
227 M:: x86.64 %unary-float-function ( dst src func -- )
228 0 src float-function-param
230 dst float-function-return ;
232 M:: x86.64 %binary-float-function ( dst src1 src2 func -- )
233 ! src1 might equal dst; otherwise it will be a spill slot
234 ! src2 is always a spill slot
235 0 src1 float-function-param
236 1 src2 float-function-param
238 dst float-function-return ;
240 M:: x86.64 %call-gc ( gc-root-count temp -- )
241 ! Pass pointer to start of GC roots as first parameter
242 param-reg-1 gc-root-base param@ LEA
243 ! Pass number of roots as second parameter
244 param-reg-2 gc-root-count MOV
245 ! Pass VM ptr as third parameter
246 param-reg-3 %mov-vm-ptr
248 "inline_gc" f %alien-invoke ;
250 ! The result of reading 4 bytes from memory is a fixnum on
252 enable-alien-4-intrinsics
257 { [ os unix? ] [ "cpu.x86.64.unix" require ] }
258 { [ os winnt? ] [ "cpu.x86.64.winnt" require ] }