]> 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 math.intervals
4 namespaces combinators fry arrays
5 cpu.architecture
6 compiler.tree.propagation.info
7 compiler.cfg.hats
8 compiler.cfg.stacks
9 compiler.cfg.instructions
10 compiler.cfg.utilities
11 compiler.cfg.builder.blocks
12 compiler.cfg.registers
13 compiler.cfg.comparisons ;
14 IN: compiler.cfg.intrinsics.fixnum
15
16 : emit-both-fixnums? ( -- )
17     2inputs
18     ^^or
19     tag-mask get ^^and-imm
20     0 cc= ^^compare-imm
21     ds-push ;
22
23 : tag-literal ( n -- tagged )
24     literal>> [ tag-fixnum ] [ \ f tag-number ] if* ;
25
26 : emit-fixnum-op ( insn -- )
27     [ 2inputs ] dip call ds-push ; inline
28
29 : emit-fixnum-left-shift ( -- )
30     [ ^^untag-fixnum ^^shl ] emit-fixnum-op ;
31
32 : emit-fixnum-right-shift ( -- )
33     [ ^^untag-fixnum ^^neg ^^sar dup tag-mask get ^^and-imm ^^xor ] emit-fixnum-op ;
34
35 : emit-fixnum-shift-general ( -- )
36     ds-peek 0 cc> ##compare-imm-branch
37     [ emit-fixnum-left-shift ] with-branch
38     [ emit-fixnum-right-shift ] with-branch
39     2array emit-conditional ;
40
41 : emit-fixnum-shift-fast ( node -- )
42     node-input-infos second interval>> {
43         { [ dup 0 [a,inf] interval-subset? ] [ drop emit-fixnum-left-shift ] }
44         { [ dup 0 [-inf,a] interval-subset? ] [ drop emit-fixnum-right-shift ] }
45         [ drop emit-fixnum-shift-general ]
46     } cond ;
47     
48 : emit-fixnum-bitnot ( -- )
49     ds-pop ^^not tag-mask get ^^xor-imm ds-push ;
50
51 : emit-fixnum-log2 ( -- )
52     ds-pop ^^log2 tag-bits get ^^sub-imm ^^tag-fixnum ds-push ;
53
54 : emit-fixnum*fast ( -- )
55     2inputs ^^untag-fixnum ^^mul ds-push ;
56
57 : emit-fixnum-comparison ( cc -- )
58     '[ _ ^^compare ] emit-fixnum-op ;
59
60 : emit-no-overflow-case ( dst -- final-bb )
61     [ ds-drop ds-drop ds-push ] with-branch ;
62
63 : emit-overflow-case ( word -- final-bb )
64     [ ##call -1 adjust-d ] with-branch ;
65
66 : emit-fixnum-overflow-op ( quot word -- )
67     ! Inputs to the final instruction need to be copied because
68     ! of loc>vreg sync
69     [ [ (2inputs) [ any-rep ^^copy ] bi@ ] dip call ] dip
70     [ emit-no-overflow-case ] [ emit-overflow-case ] bi* 2array
71     emit-conditional ; inline
72
73 : fixnum+overflow ( x y -- z ) [ >bignum ] bi@ + ;
74
75 : fixnum-overflow ( x y -- z ) [ >bignum ] bi@ - ;
76
77 : fixnum*overflow ( x y -- z ) [ >bignum ] bi@ * ;
78
79 : emit-fixnum+ ( -- )
80     [ ^^fixnum-add ] \ fixnum+overflow emit-fixnum-overflow-op ;
81
82 : emit-fixnum- ( -- )
83     [ ^^fixnum-sub ] \ fixnum-overflow emit-fixnum-overflow-op ;
84
85 : emit-fixnum* ( -- )
86     [ ^^untag-fixnum ^^fixnum-mul ] \ fixnum*overflow emit-fixnum-overflow-op ;