factor/library/compiler/intrinsics.factor

236 lines
5.1 KiB
Factor
Raw Normal View History

2005-05-06 18:33:40 -04:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-05-09 02:34:15 -04:00
IN: compiler-frontend
USING: arrays assembler compiler-backend generic hashtables
inference kernel kernel-internals lists math math-internals
namespaces sequences words ;
2005-05-06 18:33:40 -04:00
: node-peek ( node -- value ) node-in-d peek ;
2005-08-19 21:46:12 -04:00
: type-tag ( type -- tag )
#! Given a type number, return the tag number.
dup 6 > [ drop 3 ] when ;
: value-tag ( value node -- n/f )
#! If the tag is known, output it, otherwise f.
2005-09-23 01:22:04 -04:00
node-classes ?hash dup [
2005-09-18 01:37:28 -04:00
types [ type-tag ] map dup all-equal?
2005-09-24 15:21:17 -04:00
[ first ] [ drop f ] if
] [
drop f
2005-09-24 15:21:17 -04:00
] if ;
2005-05-06 18:33:40 -04:00
: slot@ ( node -- n/f )
2005-05-06 18:33:40 -04:00
#! Compute slot offset.
dup node-in-d reverse-slice dup first dup value? [
value-literal cells swap second
2005-09-24 15:21:17 -04:00
rot value-tag dup [ - ] [ 2drop f ] if
] [
3drop f
2005-09-24 15:21:17 -04:00
] if ;
2005-05-09 22:34:47 -04:00
2005-05-06 18:33:40 -04:00
\ slot [
dup slot@ [
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
2005-05-06 18:33:40 -04:00
in-1
0 swap slot@ %fast-slot ,
] [
drop
in-2
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
0 %untag ,
2005-05-06 18:33:40 -04:00
1 0 %slot ,
2005-09-24 15:21:17 -04:00
] if out-1
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
\ set-slot [
dup slot@ [
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
in-2
2005-09-08 22:23:54 -04:00
-2 %inc-d ,
slot@ >r 0 1 r> %fast-set-slot ,
] [
drop
in-3
2005-09-08 22:23:54 -04:00
-3 %inc-d ,
1 %untag ,
0 1 2 %set-slot ,
2005-09-24 15:21:17 -04:00
] if
1 %write-barrier ,
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
! \ char-slot [
! drop
! in-2
! -1 %inc-d ,
! 0 1 %char-slot ,
! 1 <vreg> 0 %replace-d ,
! ] "intrinsic" set-word-prop
!
! \ set-char-slot [
! drop
! in-3
! -3 %inc-d ,
! 0 2 1 %set-char-slot ,
! ] "intrinsic" set-word-prop
\ type [
drop
in-1
0 %type ,
out-1
] "intrinsic" set-word-prop
\ tag [
drop
in-1
0 %tag ,
out-1
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
\ getenv [
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
node-peek value-literal 0 <vreg> swap %getenv ,
2005-09-08 22:23:54 -04:00
1 %inc-d ,
out-1
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
\ setenv [
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
in-1
node-peek value-literal 0 <vreg> swap %setenv ,
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
2005-06-07 23:29:47 -04:00
: value/vreg-list ( in -- list )
2005-09-16 22:47:28 -04:00
[ 0 swap length 1- ] keep
[ >r 2dup r> 3array >r 1- >r 1+ r> r> ] map 2nip ;
2005-06-07 23:29:47 -04:00
: values>vregs ( in -- in )
value/vreg-list
dup [ first3 load-value ] each
[ first <vreg> ] map ;
2005-06-07 23:29:47 -04:00
: binary-inputs ( node -- in1 in2 )
node-in-d values>vregs first2 swap ;
: binary-op-reg ( node op -- )
>r binary-inputs dup -1 %inc-d , r> execute , out-1 ; inline
: binary-imm ( node -- in1 in2 )
-1 %inc-d , in-1 node-peek value-literal 0 <vreg> ;
: binary-op-imm ( node op -- )
>r binary-imm dup r> execute , out-1 ; inline
: literal-immediate? ( value -- ? )
dup value? [ value-literal immediate? ] [ drop f ] if ;
: binary-op-imm? ( node -- ? )
fixnum-imm? >r node-peek literal-immediate? r> and ;
2005-06-07 23:29:47 -04:00
: binary-op ( node op -- )
#! out is a vreg where the vop stores the result.
over binary-op-imm?
2005-09-24 15:21:17 -04:00
[ binary-op-imm ] [ binary-op-reg ] if ;
{
{ fixnum+ %fixnum+ }
{ fixnum- %fixnum- }
{ fixnum-bitand %fixnum-bitand }
{ fixnum-bitor %fixnum-bitor }
{ fixnum-bitxor %fixnum-bitxor }
} [
2005-10-01 01:44:49 -04:00
first2 [ binary-op ] curry "intrinsic" set-word-prop
] each
: binary-jump-reg ( node label op -- )
>r >r binary-inputs -2 %inc-d , r> r> execute , ; inline
: binary-jump-imm ( node label op -- )
>r >r binary-imm -1 %inc-d , r> r> execute , ; inline
: binary-jump ( node label op -- )
pick binary-op-imm?
2005-09-24 15:21:17 -04:00
[ binary-jump-imm ] [ binary-jump-reg ] if ;
2005-10-04 03:16:50 -04:00
{
{ fixnum<= %jump-fixnum<= }
{ fixnum< %jump-fixnum< }
{ fixnum>= %jump-fixnum>= }
{ fixnum> %jump-fixnum> }
{ eq? %jump-eq? }
} [
2005-10-01 01:44:49 -04:00
first2 [ binary-jump ] curry "if-intrinsic" set-word-prop
] each
\ fixnum/i [
\ %fixnum/i binary-op-reg
] "intrinsic" set-word-prop
\ fixnum-mod [
! This is not clever. Because of x86, %fixnum-mod is
! hard-coded to put its output in vreg 2, which happends to
! be EDX there.
drop
in-2
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
1 <vreg> 0 <vreg> 2 <vreg> %fixnum-mod ,
T{ vreg f 2 } 0 %replace-d ,
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
\ fixnum/mod [
! See the remark on fixnum-mod for vreg usage
drop
in-2
{ T{ vreg f 1 } T{ vreg f 0 } }
{ T{ vreg f 2 } T{ vreg f 0 } }
%fixnum/mod ,
T{ vreg f 2 } 0 %replace-d ,
T{ vreg f 0 } 1 %replace-d ,
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
\ fixnum-bitnot [
drop
in-1
2005-06-05 02:43:05 -04:00
0 <vreg> 0 <vreg> %fixnum-bitnot ,
out-1
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop
2005-05-09 22:34:47 -04:00
\ fixnum* [
\ %fixnum* binary-op-reg
] "intrinsic" set-word-prop
2005-05-09 22:34:47 -04:00
: slow-shift ( -- ) \ fixnum-shift %call , ;
: negative-shift ( n -- )
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
2005-05-09 22:34:47 -04:00
in-1
2005-12-13 18:18:16 -05:00
dup cell-bits neg <= [
2005-06-05 02:43:05 -04:00
drop 0 <vreg> 2 <vreg> %fixnum-sgn ,
T{ vreg f 2 } 0 %replace-d ,
2005-05-09 22:34:47 -04:00
] [
neg 0 <vreg> 0 <vreg> %fixnum>> ,
2005-05-09 22:34:47 -04:00
out-1
2005-09-24 15:21:17 -04:00
] if ;
2005-05-09 22:34:47 -04:00
: fast-shift ( n -- )
2006-01-28 15:49:31 -05:00
dup zero? [
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
2005-05-09 22:34:47 -04:00
drop
] [
dup 0 < [
negative-shift
] [
drop slow-shift
2005-09-24 15:21:17 -04:00
] if
] if ;
2005-05-09 22:34:47 -04:00
\ fixnum-shift [
node-peek dup value? [
value-literal fast-shift
2005-05-09 22:34:47 -04:00
] [
drop slow-shift
2005-09-24 15:21:17 -04:00
] if
2005-05-17 16:13:08 -04:00
] "intrinsic" set-word-prop