1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators compiler.cfg.builder.blocks
4 compiler.cfg.comparisons compiler.cfg.hats
5 compiler.cfg.instructions compiler.cfg.stacks.local compiler.cfg.registers
6 compiler.cfg.stacks compiler.tree.propagation.info cpu.architecture fry kernel
7 layouts math math.intervals namespaces sequences ;
8 IN: compiler.cfg.intrinsics.fixnum
10 : emit-both-fixnums? ( -- )
12 [ ^^tagged>integer ] bi@
13 ^^or tag-mask get ^^and-imm
14 0 cc= ^^compare-integer-imm
17 : emit-fixnum-left-shift ( -- )
20 : emit-fixnum-right-shift ( -- )
22 [ tag-bits get ^^shl-imm ] dip
24 tag-bits get ^^sar-imm
27 : emit-fixnum-shift-general ( -- )
28 ds-peek 0 cc> ##compare-integer-imm-branch,
29 [ emit-fixnum-left-shift ] with-branch
30 [ emit-fixnum-right-shift ] with-branch
31 2array emit-conditional ;
33 : emit-fixnum-shift-fast ( node -- )
34 node-input-infos second interval>> {
35 { [ dup 0 [a,inf] interval-subset? ] [ drop emit-fixnum-left-shift ] }
36 { [ dup 0 [-inf,a] interval-subset? ] [ drop emit-fixnum-right-shift ] }
37 [ drop emit-fixnum-shift-general ]
40 : emit-fixnum-comparison ( cc -- )
41 '[ _ ^^compare-integer ] binary-op ;
43 : emit-no-overflow-case ( dst -- final-bb )
44 [ D -2 inc-stack ds-push ] with-branch ;
46 : emit-overflow-case ( word -- final-bb )
51 : emit-fixnum-overflow-op ( quot word -- )
52 ! Inputs to the final instruction need to be copied because
54 [ [ (2inputs) [ any-rep ^^copy ] bi@ cc/o ] dip call ] dip
55 [ emit-no-overflow-case ] [ emit-overflow-case ] bi* 2array
56 emit-conditional ; inline
58 : fixnum+overflow ( x y -- z ) [ >bignum ] bi@ + ;
60 : fixnum-overflow ( x y -- z ) [ >bignum ] bi@ - ;
62 : fixnum*overflow ( x y -- z ) [ >bignum ] bi@ * ;
65 [ ^^fixnum-add ] \ fixnum+overflow emit-fixnum-overflow-op ;
68 [ ^^fixnum-sub ] \ fixnum-overflow emit-fixnum-overflow-op ;
71 [ ^^fixnum-mul ] \ fixnum*overflow emit-fixnum-overflow-op ;