2010-01-02 07:03:30 -05:00
|
|
|
! Copyright (C) 2005, 2010 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-01-02 07:03:30 -05:00
|
|
|
USING: accessors arrays kernel math namespaces make sequences
|
2010-01-24 08:17:18 -05:00
|
|
|
system layouts alien alien.c-types alien.accessors alien.libraries
|
|
|
|
slots splitting assocs combinators locals compiler.constants
|
2010-01-02 07:03:30 -05:00
|
|
|
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
|
2010-01-10 07:20:32 -05:00
|
|
|
cpu.architecture vm ;
|
|
|
|
FROM: layouts => cell cells ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: cpu.x86.64
|
|
|
|
|
2010-04-01 05:53:40 -04:00
|
|
|
: param-reg-0 ( -- reg ) 0 int-regs cdecl param-reg ; inline
|
|
|
|
: param-reg-1 ( -- reg ) 1 int-regs cdecl param-reg ; inline
|
|
|
|
: param-reg-2 ( -- reg ) 2 int-regs cdecl param-reg ; inline
|
|
|
|
: param-reg-3 ( -- reg ) 3 int-regs cdecl param-reg ; inline
|
2009-10-20 06:02:42 -04:00
|
|
|
|
|
|
|
M: x86.64 pic-tail-reg RBX ;
|
|
|
|
|
|
|
|
M: int-regs return-reg drop RAX ;
|
|
|
|
M: float-regs return-reg drop XMM0 ;
|
|
|
|
|
|
|
|
M: x86.64 ds-reg R14 ;
|
|
|
|
M: x86.64 rs-reg R15 ;
|
|
|
|
M: x86.64 stack-reg RSP ;
|
2010-01-02 07:03:30 -05:00
|
|
|
M: x86.64 frame-reg RBP ;
|
2009-10-20 06:02:42 -04:00
|
|
|
|
|
|
|
M: x86.64 extra-stack-space drop 0 ;
|
|
|
|
|
2008-10-07 21:00:38 -04:00
|
|
|
M: x86.64 machine-registers
|
|
|
|
{
|
2010-01-10 07:20:32 -05:00
|
|
|
{ int-regs { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 } }
|
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
|
|
|
|
} }
|
|
|
|
} ;
|
|
|
|
|
2010-01-10 07:20:32 -05:00
|
|
|
: vm-reg ( -- reg ) R13 ; inline
|
2010-03-26 22:44:43 -04:00
|
|
|
: nv-reg ( -- reg ) RBX ; inline
|
2010-01-10 07:20:32 -05:00
|
|
|
|
|
|
|
M: x86.64 %mov-vm-ptr ( reg -- )
|
|
|
|
vm-reg MOV ;
|
|
|
|
|
2010-02-03 03:27:18 -05:00
|
|
|
M: x86.64 %vm-field ( dst field -- )
|
|
|
|
[ vm-reg ] dip vm-field-offset [+] MOV ;
|
|
|
|
|
2010-01-10 07:20:32 -05:00
|
|
|
M: x86.64 %vm-field-ptr ( dst field -- )
|
|
|
|
[ vm-reg ] dip vm-field-offset [+] LEA ;
|
|
|
|
|
2009-10-20 06:02:42 -04:00
|
|
|
M: x86.64 %prologue ( n -- )
|
2010-02-03 03:27:18 -05:00
|
|
|
temp-reg -7 [] LEA
|
2009-10-20 06:02:42 -04:00
|
|
|
dup PUSH
|
|
|
|
temp-reg PUSH
|
|
|
|
stack-reg swap 3 cells - SUB ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-02-03 03:27:18 -05:00
|
|
|
M: x86.64 %prepare-jump
|
|
|
|
pic-tail-reg xt-tail-pic-offset [] LEA ;
|
|
|
|
|
2009-10-15 23:07:03 -04:00
|
|
|
: load-cards-offset ( dst -- )
|
|
|
|
0 MOV rc-absolute-cell rel-cards-offset ;
|
|
|
|
|
|
|
|
M: x86.64 %mark-card
|
|
|
|
dup load-cards-offset
|
|
|
|
[+] card-mark <byte> MOV ;
|
|
|
|
|
|
|
|
: load-decks-offset ( dst -- )
|
|
|
|
0 MOV rc-absolute-cell rel-decks-offset ;
|
|
|
|
|
|
|
|
M: x86.64 %mark-deck
|
2009-10-16 00:29:56 -04:00
|
|
|
dup load-decks-offset
|
2009-10-15 23:07:03 -04:00
|
|
|
[+] card-mark <byte> MOV ;
|
|
|
|
|
2009-05-29 02:39:14 -04:00
|
|
|
M:: x86.64 %dispatch ( src temp -- )
|
2008-11-13 05:16:08 -05:00
|
|
|
! Load jump table base.
|
|
|
|
temp HEX: ffffffff MOV
|
2010-01-02 07:03:30 -05:00
|
|
|
building get length :> start
|
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
|
2010-01-02 07:03:30 -05:00
|
|
|
[ end start - + building get dup pop* push ]
|
2008-11-13 05:16:08 -05:00
|
|
|
[ align-code ]
|
|
|
|
bi ;
|
|
|
|
|
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-12-26 07:17:42 -05:00
|
|
|
M: x86.64 %save-param-reg [ param@ ] 2dip %copy ;
|
2009-08-07 18:44:50 -04:00
|
|
|
|
2009-12-26 07:17:42 -05:00
|
|
|
M: x86.64 %load-param-reg [ swap param@ ] dip %copy ;
|
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
|
|
|
|
|
2009-12-21 21:42:49 -05:00
|
|
|
M: x86.64 %pop-stack ( n -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 swap ds-reg reg-stack MOV ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-12-26 07:17:42 -05:00
|
|
|
M: x86.64 %pop-context-stack ( -- )
|
2010-02-03 03:27:18 -05:00
|
|
|
temp-reg "ctx" %vm-field
|
|
|
|
param-reg-0 temp-reg "datastack" context-field-offset [+] MOV
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 param-reg-0 [] MOV
|
2010-02-03 03:27:18 -05:00
|
|
|
temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
|
2009-12-26 07:17:42 -05:00
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
M:: x86.64 %unbox ( n rep func -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-1 %mov-vm-ptr
|
2007-09-20 18:09:08 -04:00
|
|
|
! Call the unboxer
|
2009-09-25 15:03:03 -04:00
|
|
|
func f %alien-invoke
|
2009-08-07 18:44:50 -04:00
|
|
|
! 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-09-13 21:28:13 -04:00
|
|
|
: %unbox-struct-field ( c-type i -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
! Alien must be in param-reg-0.
|
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 -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
! Alien must be in param-reg-0.
|
|
|
|
param-reg-1 %mov-vm-ptr
|
2009-09-25 15:03:03 -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 -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
! Source is in param-reg-0
|
|
|
|
! Load destination address into param-reg-1
|
|
|
|
param-reg-1 n param@ LEA
|
|
|
|
! Load structure size into param-reg-2
|
|
|
|
param-reg-2 c-type heap-size MOV
|
|
|
|
param-reg-3 %mov-vm-ptr
|
2007-09-20 18:09:08 -04:00
|
|
|
! Copy the struct to the C stack
|
2009-09-25 15:03:03 -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 -- )
|
2010-04-01 05:53:40 -04:00
|
|
|
[ [ 0 ] dip reg-class-of cdecl param-reg ]
|
2009-08-07 18:44:50 -04:00
|
|
|
[ reg-class-of return-reg ]
|
|
|
|
[ ]
|
2009-09-23 23:49:54 -04:00
|
|
|
tri %copy ;
|
2009-08-07 18:44:50 -04:00
|
|
|
|
|
|
|
M:: x86.64 %box ( n rep func -- )
|
|
|
|
n [
|
|
|
|
n
|
2010-04-01 05:53:40 -04:00
|
|
|
0 rep reg-class-of cdecl param-reg
|
2009-08-07 18:44:50 -04:00
|
|
|
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
|
2010-01-24 08:17:18 -05:00
|
|
|
rep int-rep?
|
|
|
|
cpu x86.64? os windows? and or
|
|
|
|
param-reg-1 param-reg-0 ? %mov-vm-ptr
|
2009-09-25 15:03:03 -04:00
|
|
|
func f %alien-invoke ;
|
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 ]
|
2010-01-02 07:03:30 -05:00
|
|
|
[ param-reg-2 swap heap-size MOV ] bi
|
|
|
|
param-reg-0 0 box-struct-field@ MOV
|
|
|
|
param-reg-1 1 box-struct-field@ MOV
|
|
|
|
param-reg-3 %mov-vm-ptr
|
2009-12-21 21:42:49 -05:00
|
|
|
"from_small_struct" f %alien-invoke
|
2008-09-13 21:28:13 -04:00
|
|
|
] 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
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-1 swap heap-size MOV
|
2007-09-20 18:09:08 -04:00
|
|
|
! Compute destination address
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 swap struct-return@ LEA
|
|
|
|
param-reg-2 %mov-vm-ptr
|
2007-09-20 18:09:08 -04:00
|
|
|
! Copy the struct from the C stack
|
2009-12-21 21:42:49 -05:00
|
|
|
"from_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 ( -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 ds-reg [] MOV
|
2009-12-26 07:17:42 -05:00
|
|
|
ds-reg 8 SUB
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-1 %mov-vm-ptr
|
2009-12-26 07:17:42 -05:00
|
|
|
"pinned_alien_offset" f %alien-invoke
|
2010-03-26 22:44:43 -04:00
|
|
|
nv-reg RAX MOV ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %alien-indirect ( -- )
|
2010-03-26 22:44:43 -04:00
|
|
|
nv-reg CALL ;
|
|
|
|
|
|
|
|
M: x86.64 %begin-callback ( -- )
|
|
|
|
param-reg-0 %mov-vm-ptr
|
|
|
|
"begin_callback" f %alien-invoke ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
M: x86.64 %alien-callback ( quot -- )
|
2010-01-05 21:47:36 -05:00
|
|
|
param-reg-0 param-reg-1 %restore-context
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 swap %load-reference
|
2010-01-18 02:51:27 -05:00
|
|
|
param-reg-0 quot-entry-point-offset [+] CALL
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 param-reg-1 %save-context ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-03-26 22:44:43 -04:00
|
|
|
M: x86.64 %end-callback ( -- )
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 %mov-vm-ptr
|
2010-03-26 22:44:43 -04:00
|
|
|
"end_callback" f %alien-invoke ;
|
|
|
|
|
|
|
|
M: x86.64 %end-callback-value ( ctype -- )
|
|
|
|
%pop-context-stack
|
|
|
|
nv-reg param-reg-0 MOV
|
|
|
|
%end-callback
|
|
|
|
param-reg-0 nv-reg MOV
|
2007-09-20 18:09:08 -04:00
|
|
|
! Unbox former top of data stack to return registers
|
|
|
|
unbox-return ;
|
|
|
|
|
2009-09-27 20:28:20 -04:00
|
|
|
: float-function-param ( i src -- )
|
2010-04-01 05:53:40 -04:00
|
|
|
[ float-regs cdecl param-regs nth ] dip double-rep %copy ;
|
2009-08-30 05:52:01 -04:00
|
|
|
|
|
|
|
: float-function-return ( reg -- )
|
2009-09-23 23:49:54 -04:00
|
|
|
float-regs return-reg double-rep %copy ;
|
2009-08-30 05:52:01 -04:00
|
|
|
|
|
|
|
M:: x86.64 %unary-float-function ( dst src func -- )
|
|
|
|
0 src float-function-param
|
2010-01-24 08:17:18 -05:00
|
|
|
func "libm" load-library %alien-invoke
|
2009-08-30 05:52:01 -04:00
|
|
|
dst float-function-return ;
|
|
|
|
|
|
|
|
M:: x86.64 %binary-float-function ( dst src1 src2 func -- )
|
2009-09-27 20:28:20 -04:00
|
|
|
! src1 might equal dst; otherwise it will be a spill slot
|
|
|
|
! src2 is always a spill slot
|
2009-08-30 05:52:01 -04:00
|
|
|
0 src1 float-function-param
|
|
|
|
1 src2 float-function-param
|
2010-01-24 08:17:18 -05:00
|
|
|
func "libm" load-library %alien-invoke
|
2009-08-30 05:52:01 -04:00
|
|
|
dst float-function-return ;
|
|
|
|
|
2009-09-25 22:47:05 -04:00
|
|
|
M:: x86.64 %call-gc ( gc-root-count temp -- )
|
2009-09-25 13:29:07 -04:00
|
|
|
! Pass pointer to start of GC roots as first parameter
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-0 gc-root-base param@ LEA
|
2009-09-25 13:29:07 -04:00
|
|
|
! Pass number of roots as second parameter
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-1 gc-root-count MOV
|
2009-09-25 22:47:05 -04:00
|
|
|
! Pass VM ptr as third parameter
|
2010-01-02 07:03:30 -05:00
|
|
|
param-reg-2 %mov-vm-ptr
|
2009-09-25 13:29:07 -04:00
|
|
|
! Call GC
|
|
|
|
"inline_gc" f %alien-invoke ;
|
|
|
|
|
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
|
|
|
|
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
|
2009-09-03 04:28:38 -04:00
|
|
|
|
2009-09-20 18:43:16 -04:00
|
|
|
check-sse
|