]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/intrinsics/fixnum/fixnum.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / compiler / cfg / intrinsics / fixnum / fixnum.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences accessors layouts kernel math namespaces
4 combinators fry arrays
5 compiler.tree.propagation.info
6 compiler.cfg.hats
7 compiler.cfg.stacks
8 compiler.cfg.instructions
9 compiler.cfg.utilities
10 compiler.cfg.registers
11 compiler.cfg.comparisons ;
12 IN: compiler.cfg.intrinsics.fixnum
13
14 : emit-both-fixnums? ( -- )
15     2inputs
16     ^^or
17     tag-mask get ^^and-imm
18     0 cc= ^^compare-imm
19     ds-push ;
20
21 : tag-literal ( n -- tagged )
22     literal>> [ tag-fixnum ] [ \ f tag-number ] if* ;
23
24 : emit-fixnum-op ( insn -- dst )
25     [ 2inputs ] dip call ds-push ; inline
26
27 : emit-fixnum-shift-fast ( node -- )
28     dup node-input-infos dup second value-info-small-fixnum? [
29         nip
30         [ ds-drop ds-pop ] dip
31         second literal>> dup sgn {
32             { -1 [ neg tag-bits get + ^^sar-imm ^^tag-fixnum ] }
33             {  0 [ drop ] }
34             {  1 [ ^^shl-imm ] }
35         } case
36         ds-push
37     ] [ drop emit-primitive ] if ;
38     
39 : emit-fixnum-bitnot ( -- )
40     ds-pop ^^not tag-mask get ^^xor-imm ds-push ;
41
42 : emit-fixnum-log2 ( -- )
43     ds-pop ^^log2 tag-bits get ^^sub-imm ^^tag-fixnum ds-push ;
44
45 : emit-fixnum*fast ( -- )
46     2inputs ^^untag-fixnum ^^mul ds-push ;
47
48 : emit-fixnum-comparison ( cc -- )
49     '[ _ ^^compare ] emit-fixnum-op ;
50
51 : emit-bignum>fixnum ( -- )
52     ds-pop ^^bignum>integer ^^tag-fixnum ds-push ;
53
54 : emit-fixnum>bignum ( -- )
55     ds-pop ^^untag-fixnum ^^integer>bignum ds-push ;
56
57 : emit-no-overflow-case ( dst -- final-bb )
58     [ -2 ##inc-d ds-push ] with-branch ;
59
60 : emit-overflow-case ( word -- final-bb )
61     [ ##call ] with-branch ;
62
63 : emit-fixnum-overflow-op ( quot word -- )
64     [ [ D 1 ^^peek D 0 ^^peek ] dip call ] dip
65     [ emit-no-overflow-case ] [ emit-overflow-case ] bi* 2array
66     emit-conditional ; inline
67
68 : fixnum+overflow ( x y -- z ) [ >bignum ] bi@ + ;
69
70 : fixnum-overflow ( x y -- z ) [ >bignum ] bi@ - ;
71
72 : fixnum*overflow ( x y -- z ) [ >bignum ] bi@ * ;
73
74 : emit-fixnum+ ( -- )
75     [ ^^fixnum-add ] \ fixnum+overflow emit-fixnum-overflow-op ;
76
77 : emit-fixnum- ( -- )
78     [ ^^fixnum-sub ] \ fixnum-overflow emit-fixnum-overflow-op ;
79
80 : emit-fixnum* ( -- )
81     [ ^^untag-fixnum ^^fixnum-mul ] \ fixnum*overflow emit-fixnum-overflow-op ;