factor/library/compiler/x86/architecture.factor

156 lines
4.4 KiB
Factor
Raw Normal View History

2006-04-28 19:23:50 -04:00
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: alien arrays assembler generic kernel kernel-internals
2006-04-29 17:28:51 -04:00
math memory namespaces sequences words ;
2006-04-29 18:33:05 -04:00
IN: compiler
: code-format 1 ; inline
2005-09-05 17:14:15 -04:00
! x86 register assignments
2006-05-04 18:08:52 -04:00
! EAX, ECX, EDX integer vregs
! XMM0 - XMM7 float vregs
2005-09-05 17:14:15 -04:00
! ESI datastack
2006-07-02 23:41:13 -04:00
! EDI callstack
2005-09-05 17:14:15 -04:00
2006-05-04 18:08:52 -04:00
! AMD64 redefines a lot of words in this file
2005-12-07 00:14:24 -05:00
: ds-reg ESI ; inline
2006-07-02 23:41:13 -04:00
: cs-reg EDI ; inline
2006-04-29 17:28:51 -04:00
: remainder-reg EDX ; inline
2006-07-02 23:41:13 -04:00
: alloc-tmp-reg EBX ; inline
2006-07-03 02:52:44 -04:00
: stack-reg ESP ; inline
: stack@ stack-reg swap [+] ;
2006-04-29 17:28:51 -04:00
2006-04-28 19:23:50 -04:00
: reg-stack ( n reg -- op ) swap cells neg [+] ;
M: ds-loc v>operand ds-loc-n ds-reg reg-stack ;
M: cs-loc v>operand cs-loc-n cs-reg reg-stack ;
2006-08-09 18:43:08 -04:00
: %alien-invoke ( symbol dll -- ) (CALL) rel-dlsym ;
2005-12-10 01:02:13 -05:00
: with-aligned-stack ( n quot -- )
#! On Linux, there is no requirement to align stack frames,
#! so this is mostly a no-op.
swap slip stack-reg swap ADD ; inline
2005-12-10 01:02:13 -05:00
: compile-c-call* ( symbol dll args -- operands )
dup length cells [
<reversed> [ PUSH ] each %alien-invoke
] with-aligned-stack ;
2005-12-10 01:02:13 -05:00
2006-07-03 02:52:44 -04:00
GENERIC: push-return-reg ( reg-class -- )
GENERIC: pop-return-reg ( reg-class -- )
GENERIC: load-return-reg ( stack@ reg-class -- )
2006-07-04 02:04:33 -04:00
GENERIC: store-return-reg ( stack@ reg-class -- )
2006-07-03 02:52:44 -04:00
! On x86, parameters are never passed in registers.
2005-12-24 16:08:15 -05:00
M: int-regs return-reg drop EAX ;
2006-01-24 20:20:20 -05:00
M: int-regs fastcall-regs drop { } ;
2006-05-04 18:08:52 -04:00
M: int-regs vregs drop { EAX ECX EDX } ;
2006-07-03 02:52:44 -04:00
M: int-regs %freg>stack drop >r stack@ r> MOV ;
M: int-regs %stack>freg drop swap stack@ MOV ;
M: int-regs push-return-reg return-reg PUSH ;
M: int-regs pop-return-reg return-reg POP ;
2006-07-04 02:04:33 -04:00
: load/store-int-return return-reg stack-reg rot [+] ;
M: int-regs load-return-reg load/store-int-return MOV ;
M: int-regs store-return-reg load/store-int-return swap MOV ;
2006-07-03 02:52:44 -04:00
: MOVSS/D float-regs-size 4 = [ MOVSS ] [ MOVSD ] if ;
2005-12-24 16:08:15 -05:00
2006-01-24 20:20:20 -05:00
M: float-regs fastcall-regs drop { } ;
2006-05-04 18:08:52 -04:00
M: float-regs vregs drop { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } ;
2006-07-03 02:52:44 -04:00
M: float-regs %freg>stack >r >r stack@ r> r> MOVSS/D ;
M: float-regs %stack>freg >r swap stack@ r> MOVSS/D ;
: FSTP 4 = [ FSTPS ] [ FSTPL ] if ;
M: float-regs push-return-reg
stack-reg swap reg-size [ SUB stack-reg [] ] keep FSTP ;
: FLD 4 = [ FLDS ] [ FLDL ] if ;
2006-07-03 03:30:11 -04:00
: drop-return-reg stack-reg swap reg-size ADD ;
2006-07-03 02:52:44 -04:00
M: float-regs pop-return-reg
stack-reg [] over reg-size FLD drop-return-reg ;
2006-07-04 02:04:33 -04:00
: load/store-float-return reg-size >r stack-reg swap [+] r> ;
M: float-regs load-return-reg load/store-float-return FLD ;
M: float-regs store-return-reg load/store-float-return FSTP ;
2005-12-04 22:55:02 -05:00
: address-operand ( address -- operand )
#! On x86, we can always use an address as an operand
#! directly.
; inline
: fixnum>slot@ 1 SHR ; inline
2005-12-07 03:37:05 -05:00
: prepare-division CDQ ; inline
M: immediate load-literal
v>operand swap v>operand MOV ;
2006-07-04 02:04:33 -04:00
: load-indirect ( literal reg -- )
0 [] MOV rel-absolute-cell rel-literal ;
2006-07-04 02:04:33 -04:00
M: object load-literal
2006-07-04 02:04:33 -04:00
v>operand load-indirect ;
2006-04-28 19:23:50 -04:00
: (%call) ( label -- label )
2006-08-10 14:39:12 -04:00
dup (compile) dup primitive? [ address-operand ] when ;
2006-04-28 19:23:50 -04:00
: %call ( label -- ) (%call) CALL ;
: %jump ( label -- ) %epilogue (%call) JMP ;
: %jump-label ( label -- ) JMP ;
2006-04-29 17:28:51 -04:00
: %jump-t ( label -- ) "flag" operand f v>operand CMP JNE ;
2006-04-28 19:23:50 -04:00
: compile-aligned ( -- )
compiled-offset [ 8 align ] keep - 0 <array> % ;
: %dispatch ( -- )
2006-04-28 19:23:50 -04:00
#! Compile a piece of code that jumps to an offset in a
#! jump table indexed by the fixnum at the top of the stack.
#! The jump table must immediately follow this macro.
! Untag and multiply to get a jump table offset
2006-08-10 00:39:13 -04:00
"end" define-label
"n" operand fixnum>slot@
2006-04-28 19:23:50 -04:00
! Add to jump table base. We use a temporary register since
2006-07-11 00:48:35 -04:00
! on AMD64 we have to load a 64-bit immediate. On x86, this
2006-04-28 19:23:50 -04:00
! is redundant.
"scratch" operand HEX: ffffffff MOV
2006-08-09 18:43:08 -04:00
"end" get rel-absolute-cell rel-label
2006-04-29 18:33:05 -04:00
"n" operand "scratch" operand ADD
2006-04-28 19:23:50 -04:00
! Jump to jump table entry
"n" operand [] JMP
2006-04-28 19:23:50 -04:00
! Align for better performance
compile-aligned
! Fix up jump table pointer
"end" get resolve-label ;
2006-04-28 19:23:50 -04:00
: %target ( label -- ) 0 cell, rel-absolute-cell rel-label ;
2006-04-28 19:23:50 -04:00
: %return ( -- ) %epilogue RET ;
2006-05-09 11:31:10 -04:00
: %move-int>int ( dst src -- )
[ v>operand ] 2apply MOV ;
: %move-int>float ( dst src -- )
[ v>operand ] 2apply float-offset [+] MOVSD ;
2006-05-09 11:31:10 -04:00
M: int-regs (%peek) drop %move-int>int ;
M: int-regs (%replace) drop swap %move-int>int ;
2006-05-05 20:06:57 -04:00
2006-04-29 18:33:05 -04:00
: (%inc) swap cells dup 0 > [ ADD ] [ neg SUB ] if ;
2006-04-28 19:23:50 -04:00
: %inc-d ( n -- ) ds-reg (%inc) ;
2006-04-28 19:23:50 -04:00
: %inc-r ( n -- ) cs-reg (%inc) ;
2006-05-09 11:31:10 -04:00
M: object %stack>freg 3drop ;
2006-05-09 11:31:10 -04:00
M: object %freg>stack 3drop ;