2009-06-30 21:16:09 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
|
2008-10-20 21:40:15 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: sequences accessors layouts kernel math namespaces
|
2009-07-16 19:29:40 -04:00
|
|
|
combinators fry arrays
|
2008-10-20 21:40:15 -04:00
|
|
|
compiler.tree.propagation.info
|
2008-11-28 07:36:30 -05:00
|
|
|
compiler.cfg.hats
|
|
|
|
compiler.cfg.stacks
|
|
|
|
compiler.cfg.instructions
|
2008-11-28 09:35:02 -05:00
|
|
|
compiler.cfg.utilities
|
2009-07-13 15:42:52 -04:00
|
|
|
compiler.cfg.registers
|
|
|
|
compiler.cfg.comparisons ;
|
2008-10-20 21:40:15 -04:00
|
|
|
IN: compiler.cfg.intrinsics.fixnum
|
|
|
|
|
2008-11-28 09:35:02 -05:00
|
|
|
: emit-both-fixnums? ( -- )
|
2008-12-06 10:16:29 -05:00
|
|
|
2inputs
|
2008-11-28 09:35:02 -05:00
|
|
|
^^or
|
|
|
|
tag-mask get ^^and-imm
|
|
|
|
0 cc= ^^compare-imm
|
|
|
|
ds-push ;
|
|
|
|
|
2009-06-30 19:47:24 -04:00
|
|
|
: tag-literal ( n -- tagged )
|
|
|
|
literal>> [ tag-fixnum ] [ \ f tag-number ] if* ;
|
|
|
|
|
2009-07-14 20:18:57 -04:00
|
|
|
: emit-fixnum-op ( insn -- dst )
|
|
|
|
[ 2inputs ] dip call ds-push ; inline
|
2008-10-20 21:40:15 -04:00
|
|
|
|
|
|
|
: emit-fixnum-shift-fast ( node -- )
|
2009-07-02 01:51:06 -04:00
|
|
|
dup node-input-infos dup second value-info-small-fixnum? [
|
2008-10-20 21:40:15 -04:00
|
|
|
nip
|
2009-07-02 01:51:06 -04:00
|
|
|
[ ds-drop ds-pop ] dip
|
|
|
|
second literal>> dup sgn {
|
|
|
|
{ -1 [ neg tag-bits get + ^^sar-imm ^^tag-fixnum ] }
|
|
|
|
{ 0 [ drop ] }
|
|
|
|
{ 1 [ ^^shl-imm ] }
|
|
|
|
} case
|
|
|
|
ds-push
|
|
|
|
] [ drop emit-primitive ] if ;
|
2009-07-01 00:01:44 -04:00
|
|
|
|
2008-10-20 21:40:15 -04:00
|
|
|
: emit-fixnum-bitnot ( -- )
|
2008-10-21 04:20:48 -04:00
|
|
|
ds-pop ^^not tag-mask get ^^xor-imm ds-push ;
|
2008-10-20 21:40:15 -04:00
|
|
|
|
2008-12-06 16:31:17 -05:00
|
|
|
: emit-fixnum-log2 ( -- )
|
|
|
|
ds-pop ^^log2 tag-bits get ^^sub-imm ^^tag-fixnum ds-push ;
|
|
|
|
|
2009-07-14 20:18:57 -04:00
|
|
|
: emit-fixnum*fast ( -- )
|
|
|
|
2inputs ^^untag-fixnum ^^mul ds-push ;
|
2009-06-30 19:47:24 -04:00
|
|
|
|
2009-07-14 20:18:57 -04:00
|
|
|
: emit-fixnum-comparison ( cc -- )
|
|
|
|
'[ _ ^^compare ] emit-fixnum-op ;
|
2008-10-20 21:40:15 -04:00
|
|
|
|
|
|
|
: emit-bignum>fixnum ( -- )
|
2008-10-21 04:20:48 -04:00
|
|
|
ds-pop ^^bignum>integer ^^tag-fixnum ds-push ;
|
2008-10-20 21:40:15 -04:00
|
|
|
|
|
|
|
: emit-fixnum>bignum ( -- )
|
2008-11-02 02:50:48 -05:00
|
|
|
ds-pop ^^untag-fixnum ^^integer>bignum ds-push ;
|
2008-11-28 06:33:58 -05:00
|
|
|
|
2009-07-16 19:29:40 -04:00
|
|
|
: emit-no-overflow-case ( dst -- final-bb )
|
|
|
|
[ -2 ##inc-d ds-push ] with-branch ;
|
|
|
|
|
|
|
|
: emit-overflow-case ( word -- final-bb )
|
|
|
|
[ ##call ] with-branch ;
|
|
|
|
|
|
|
|
: emit-fixnum-overflow-op ( quot word -- )
|
|
|
|
[ [ D 1 ^^peek D 0 ^^peek ] dip call ] dip
|
|
|
|
[ emit-no-overflow-case ] [ emit-overflow-case ] bi* 2array
|
|
|
|
emit-conditional ; inline
|
|
|
|
|
|
|
|
: fixnum+overflow ( x y -- z ) [ >bignum ] bi@ + ;
|
|
|
|
|
|
|
|
: fixnum-overflow ( x y -- z ) [ >bignum ] bi@ - ;
|
|
|
|
|
|
|
|
: fixnum*overflow ( x y -- z ) [ >bignum ] bi@ * ;
|
|
|
|
|
|
|
|
: emit-fixnum+ ( -- )
|
|
|
|
[ ^^fixnum-add ] \ fixnum+overflow emit-fixnum-overflow-op ;
|
|
|
|
|
|
|
|
: emit-fixnum- ( -- )
|
|
|
|
[ ^^fixnum-sub ] \ fixnum-overflow emit-fixnum-overflow-op ;
|
|
|
|
|
|
|
|
: emit-fixnum* ( -- )
|
|
|
|
[ ^^untag-fixnum ^^fixnum-mul ] \ fixnum*overflow emit-fixnum-overflow-op ;
|