2009-06-02 19:23:47 -04:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-07-29 22:44:08 -04:00
|
|
|
USING: accessors arrays kernel math namespaces make sequences system
|
|
|
|
layouts alien alien.c-types alien.accessors alien.structs slots
|
|
|
|
splitting assocs combinators locals compiler.constants
|
|
|
|
compiler.codegen compiler.codegen.fixup compiler.cfg.instructions
|
|
|
|
compiler.cfg.builder compiler.cfg.intrinsics compiler.cfg.stack-frame
|
|
|
|
cpu.x86.assembler cpu.x86.assembler.operands cpu.x86 cpu.architecture ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: cpu.x86.64
|
|
|
|
|
2008-10-07 21:00:38 -04:00
|
|
|
M: x86.64 machine-registers
|
|
|
|
{
|
2008-10-13 15:03:21 -04:00
|
|
|
{ int-regs { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 } }
|
2009-08-07 18:44:50 -04:00
|
|
|
{ float-regs {
|
2008-10-07 21:00:38 -04:00
|
|
|
XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7
|
|
|
|
XMM8 XMM9 XMM10 XMM11 XMM12 XMM13 XMM14 XMM15
|
|
|
|
} }
|
|
|
|
} ;
|
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 ds-reg R14 ;
|
|
|
|
M: x86.64 rs-reg R15 ;
|
|
|
|
M: x86.64 stack-reg RSP ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-05-29 02:39:14 -04:00
|
|
|
M:: x86.64 %dispatch ( src temp -- )
|
2009-06-30 19:11:15 -04:00
|
|
|
building get length :> start
|
2008-11-13 05:16:08 -05:00
|
|
|
! Load jump table base.
|
|
|
|
temp HEX: ffffffff MOV
|
2009-05-29 02:39:14 -04:00
|
|
|
0 rc-absolute-cell rel-here
|
2008-11-13 05:16:08 -05:00
|
|
|
! Add jump table base
|
2009-06-30 17:47:22 -04:00
|
|
|
temp src ADD
|
|
|
|
temp HEX: 7f [+] JMP
|
2009-06-30 19:11:15 -04:00
|
|
|
building get length :> end
|
2008-11-13 05:16:08 -05:00
|
|
|
! Fix up the displacement above
|
|
|
|
cell code-alignment
|
2009-06-30 19:11:15 -04:00
|
|
|
[ end start - 2 - + building get dup pop* push ]
|
2008-11-13 05:16:08 -05:00
|
|
|
[ align-code ]
|
|
|
|
bi ;
|
|
|
|
|
2008-11-28 06:33:58 -05:00
|
|
|
M: x86.64 param-reg-1 int-regs param-regs first ;
|
|
|
|
M: x86.64 param-reg-2 int-regs param-regs second ;
|
2008-12-15 23:21:56 -05:00
|
|
|
: param-reg-3 ( -- reg ) int-regs param-regs third ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-05-06 20:22:22 -04:00
|
|
|
M: x86.64 pic-tail-reg RBX ;
|
|
|
|
|
2008-11-07 21:33:32 -05:00
|
|
|
M: int-regs return-reg drop RAX ;
|
2007-09-20 18:09:08 -04:00
|
|
|
M: float-regs return-reg drop XMM0 ;
|
|
|
|
|
2008-10-21 04:21:29 -04:00
|
|
|
M: x86.64 %prologue ( n -- )
|
2009-08-07 18:44:50 -04:00
|
|
|
temp-reg 0 MOV rc-absolute-cell rel-this
|
2008-10-21 04:21:29 -04:00
|
|
|
dup PUSH
|
2009-08-07 18:44:50 -04:00
|
|
|
temp-reg PUSH
|
2008-10-21 04:21:29 -04:00
|
|
|
stack-reg swap 3 cells - SUB ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
M: stack-params copy-register*
|
2007-09-20 18:09:08 -04:00
|
|
|
drop
|
2009-08-07 18:44:50 -04:00
|
|
|
{
|
|
|
|
{ [ dup integer? ] [ R11 swap next-stack@ MOV R11 MOV ] }
|
|
|
|
{ [ over integer? ] [ R11 swap MOV param@ R11 MOV ] }
|
|
|
|
} cond ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
M: x86 %save-param-reg [ param@ ] 2dip copy-register ;
|
|
|
|
|
|
|
|
M: x86 %load-param-reg [ swap param@ ] dip copy-register ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
: with-return-regs ( quot -- )
|
|
|
|
[
|
|
|
|
V{ RDX RAX } clone int-regs set
|
|
|
|
V{ XMM1 XMM0 } clone float-regs set
|
|
|
|
call
|
|
|
|
] with-scope ; inline
|
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %prepare-unbox ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
! First parameter is top of stack
|
2008-11-07 21:33:32 -05:00
|
|
|
param-reg-1 R14 [] MOV
|
2007-09-20 18:09:08 -04:00
|
|
|
R14 cell SUB ;
|
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
M:: x86.64 %unbox ( n rep func -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
! Call the unboxer
|
2009-08-07 18:44:50 -04:00
|
|
|
func f %alien-invoke
|
|
|
|
! Store the return value on the C stack if this is an
|
|
|
|
! alien-invoke, otherwise leave it the return register if
|
|
|
|
! this is the end of alien-callback
|
|
|
|
n [ n rep reg-class-of return-reg rep %save-param-reg ] when ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %unbox-long-long ( n func -- )
|
2009-08-07 18:44:50 -04:00
|
|
|
[ int-rep ] dip %unbox ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
: %unbox-struct-field ( c-type i -- )
|
2008-11-07 21:33:32 -05:00
|
|
|
! Alien must be in param-reg-1.
|
2009-08-07 18:44:50 -04:00
|
|
|
R11 swap cells [+] swap rep>> reg-class-of {
|
2008-09-13 21:28:13 -04:00
|
|
|
{ int-regs [ int-regs get pop swap MOV ] }
|
2009-08-07 18:44:50 -04:00
|
|
|
{ float-regs [ float-regs get pop swap MOVSD ] }
|
2008-09-13 21:28:13 -04:00
|
|
|
} case ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
M: x86.64 %unbox-small-struct ( c-type -- )
|
2008-11-07 21:33:32 -05:00
|
|
|
! Alien must be in param-reg-1.
|
2007-10-30 01:46:41 -04:00
|
|
|
"alien_offset" f %alien-invoke
|
2008-11-08 22:40:47 -05:00
|
|
|
! Move alien_offset() return value to R11 so that we don't
|
2008-09-13 21:28:13 -04:00
|
|
|
! clobber it.
|
2008-11-08 22:40:47 -05:00
|
|
|
R11 RAX MOV
|
2008-09-13 21:28:13 -04:00
|
|
|
[
|
2008-11-08 22:40:47 -05:00
|
|
|
flatten-value-type [ %unbox-struct-field ] each-index
|
2008-09-13 21:28:13 -04:00
|
|
|
] with-return-regs ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-25 20:38:48 -04:00
|
|
|
M:: x86.64 %unbox-large-struct ( n c-type -- )
|
2008-11-07 21:33:32 -05:00
|
|
|
! Source is in param-reg-1
|
2009-08-25 20:38:48 -04:00
|
|
|
! Load destination address into param-reg-2
|
|
|
|
param-reg-2 n param@ LEA
|
|
|
|
! Load structure size into param-reg-3
|
|
|
|
param-reg-3 c-type heap-size MOV
|
2007-09-20 18:09:08 -04:00
|
|
|
! Copy the struct to the C stack
|
2007-10-30 01:46:41 -04:00
|
|
|
"to_value_struct" f %alien-invoke ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
: load-return-value ( rep -- )
|
|
|
|
[ [ 0 ] dip reg-class-of param-reg ]
|
|
|
|
[ reg-class-of return-reg ]
|
|
|
|
[ ]
|
|
|
|
tri copy-register ;
|
|
|
|
|
|
|
|
M:: x86.64 %box ( n rep func -- )
|
|
|
|
n [
|
|
|
|
n
|
|
|
|
0 rep reg-class-of param-reg
|
|
|
|
rep %load-param-reg
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
2009-08-07 18:44:50 -04:00
|
|
|
rep load-return-value
|
|
|
|
] if
|
|
|
|
func f %alien-invoke ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %box-long-long ( n func -- )
|
2009-08-07 18:44:50 -04:00
|
|
|
[ int-rep ] dip %box ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-13 20:21:44 -04:00
|
|
|
: box-struct-field@ ( i -- operand ) 1 + cells param@ ;
|
2008-09-13 21:28:13 -04:00
|
|
|
|
|
|
|
: %box-struct-field ( c-type i -- )
|
2009-08-07 18:44:50 -04:00
|
|
|
box-struct-field@ swap c-type-rep reg-class-of {
|
2008-09-13 21:28:13 -04:00
|
|
|
{ int-regs [ int-regs get pop MOV ] }
|
2009-08-07 18:44:50 -04:00
|
|
|
{ float-regs [ float-regs get pop MOVSD ] }
|
2008-09-13 21:28:13 -04:00
|
|
|
} case ;
|
|
|
|
|
|
|
|
M: x86.64 %box-small-struct ( c-type -- )
|
|
|
|
#! Box a <= 16-byte struct.
|
|
|
|
[
|
2008-11-08 22:40:47 -05:00
|
|
|
[ flatten-value-type [ %box-struct-field ] each-index ]
|
|
|
|
[ param-reg-3 swap heap-size MOV ] bi
|
2008-11-07 21:33:32 -05:00
|
|
|
param-reg-1 0 box-struct-field@ MOV
|
|
|
|
param-reg-2 1 box-struct-field@ MOV
|
2008-09-13 21:28:13 -04:00
|
|
|
"box_small_struct" f %alien-invoke
|
|
|
|
] with-return-regs ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-06 01:20:00 -04:00
|
|
|
: struct-return@ ( n -- operand )
|
2008-11-08 22:40:47 -05:00
|
|
|
[ stack-frame get params>> ] unless* param@ ;
|
2008-09-12 05:02:32 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
M: x86.64 %box-large-struct ( n c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
! Struct size is parameter 2
|
2008-11-07 21:33:32 -05:00
|
|
|
param-reg-2 swap heap-size MOV
|
2007-09-20 18:09:08 -04:00
|
|
|
! Compute destination address
|
2008-11-07 21:33:32 -05:00
|
|
|
param-reg-1 swap struct-return@ LEA
|
2007-09-20 18:09:08 -04:00
|
|
|
! Copy the struct from the C stack
|
2007-10-30 01:46:41 -04:00
|
|
|
"box_value_struct" f %alien-invoke ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-06 01:20:00 -04:00
|
|
|
M: x86.64 %prepare-box-struct ( -- )
|
|
|
|
! Compute target address for value struct return
|
|
|
|
RAX f struct-return@ LEA
|
|
|
|
! Store it as the first parameter
|
2008-11-08 22:40:47 -05:00
|
|
|
0 param@ RAX MOV ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %prepare-var-args RAX RAX XOR ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-09 00:21:46 -04:00
|
|
|
M: x86.64 %alien-invoke
|
2008-09-13 15:25:06 -04:00
|
|
|
R11 0 MOV
|
|
|
|
rc-absolute-cell rel-dlsym
|
|
|
|
R11 CALL ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %prepare-alien-indirect ( -- )
|
2007-10-30 01:46:41 -04:00
|
|
|
"unbox_alien" f %alien-invoke
|
2008-10-05 22:30:29 -04:00
|
|
|
RBP RAX MOV ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %alien-indirect ( -- )
|
2008-10-05 22:30:29 -04:00
|
|
|
RBP CALL ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %alien-callback ( quot -- )
|
2009-01-29 02:44:58 -05:00
|
|
|
param-reg-1 swap %load-reference
|
2008-10-22 00:17:32 -04:00
|
|
|
"c_to_factor" f %alien-invoke ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %callback-value ( ctype -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
! Save top of data stack
|
|
|
|
%prepare-unbox
|
2008-10-05 22:30:29 -04:00
|
|
|
! Save top of data stack
|
|
|
|
RSP 8 SUB
|
2008-11-07 21:33:32 -05:00
|
|
|
param-reg-1 PUSH
|
2007-09-20 18:09:08 -04:00
|
|
|
! Restore data/call/retain stacks
|
|
|
|
"unnest_stacks" f %alien-invoke
|
2008-11-07 21:33:32 -05:00
|
|
|
! Put former top of data stack in param-reg-1
|
|
|
|
param-reg-1 POP
|
2008-10-05 22:30:29 -04:00
|
|
|
RSP 8 ADD
|
2007-09-20 18:09:08 -04:00
|
|
|
! Unbox former top of data stack to return registers
|
|
|
|
unbox-return ;
|
|
|
|
|
2009-08-30 05:52:01 -04:00
|
|
|
: float-function-param ( i spill-slot -- )
|
|
|
|
[ float-regs param-regs nth ] [ n>> spill@ ] bi* MOVSD ;
|
|
|
|
|
|
|
|
: float-function-return ( reg -- )
|
|
|
|
float-regs return-reg double-float-rep copy-register ;
|
|
|
|
|
|
|
|
M:: x86.64 %unary-float-function ( dst src func -- )
|
|
|
|
0 src float-function-param
|
|
|
|
func f %alien-invoke
|
|
|
|
dst float-function-return ;
|
|
|
|
|
|
|
|
M:: x86.64 %binary-float-function ( dst src1 src2 func -- )
|
|
|
|
0 src1 float-function-param
|
|
|
|
1 src2 float-function-param
|
|
|
|
func f %alien-invoke
|
|
|
|
dst float-function-return ;
|
|
|
|
|
2008-10-20 06:55:57 -04:00
|
|
|
! The result of reading 4 bytes from memory is a fixnum on
|
|
|
|
! x86-64.
|
|
|
|
enable-alien-4-intrinsics
|
2008-10-13 23:43:32 -04:00
|
|
|
|
2009-08-30 05:52:01 -04:00
|
|
|
! Enable fast calling of libc math functions
|
|
|
|
enable-float-functions
|
|
|
|
|
2009-09-01 16:19:26 -04:00
|
|
|
! SSE2 is always available on x86-64.
|
|
|
|
enable-sse2
|
|
|
|
|
2008-11-07 21:33:32 -05:00
|
|
|
USE: vocabs.loader
|
|
|
|
|
|
|
|
{
|
|
|
|
{ [ os unix? ] [ "cpu.x86.64.unix" require ] }
|
|
|
|
{ [ os winnt? ] [ "cpu.x86.64.winnt" require ] }
|
|
|
|
} cond
|