1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See https://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.registers
6 compiler.cfg.stacks compiler.cfg.stacks.local
7 compiler.tree.propagation.info cpu.architecture kernel layouts
8 math math.intervals namespaces sequences ;
9 IN: compiler.cfg.intrinsics.fixnum
11 : emit-both-fixnums? ( -- )
13 [ ^^tagged>integer ] bi@
14 ^^or tag-mask get ^^and-imm
15 0 cc= ^^compare-integer-imm
18 : emit-fixnum-left-shift ( -- )
21 : emit-fixnum-right-shift ( -- )
23 [ tag-bits get ^^shl-imm ] dip
25 tag-bits get ^^sar-imm
28 : emit-fixnum-shift-general ( block -- block' )
29 ds-peek 0 cc> ##compare-integer-imm-branch, dup
30 [ [ emit-fixnum-left-shift ] with-branch ]
31 [ [ emit-fixnum-right-shift ] with-branch ] bi 2array
34 : emit-fixnum-shift-fast ( block #call -- block' )
35 node-input-infos second interval>> {
36 { [ dup 0 [a,inf] interval-subset? ] [ drop emit-fixnum-left-shift ] }
37 { [ dup 0 [-inf,b] interval-subset? ] [ drop emit-fixnum-right-shift ] }
38 [ drop emit-fixnum-shift-general ]
41 : emit-fixnum-comparison ( cc -- )
42 '[ _ ^^compare-integer ] binary-op ;
44 : emit-no-overflow-case ( dst block -- final-bb )
45 [ swap D: -2 inc-stack ds-push ] with-branch ;
47 : emit-overflow-case ( word block -- final-bb )
48 [ -1 swap [ emit-call-block ] keep ] with-branch ;
50 :: emit-fixnum-overflow-op ( block quot word -- block' )
51 (2inputs) [ any-rep ^^copy ] bi@ cc/o
52 quot call( vreg1 vreg2 cc -- vreg ) block emit-no-overflow-case
53 word block emit-overflow-case 2array
54 block swap emit-conditional ; inline
56 : fixnum+overflow ( x y -- z ) [ >bignum ] bi@ + ;
58 : fixnum-overflow ( x y -- z ) [ >bignum ] bi@ - ;
60 : fixnum*overflow ( x y -- z ) [ >bignum ] bi@ * ;
62 : emit-fixnum+ ( block -- block' )
63 [ ^^fixnum-add ] \ fixnum+overflow emit-fixnum-overflow-op ;
65 : emit-fixnum- ( block -- block' )
66 [ ^^fixnum-sub ] \ fixnum-overflow emit-fixnum-overflow-op ;
68 : emit-fixnum* ( block -- block' )
69 [ ^^fixnum-mul ] \ fixnum*overflow emit-fixnum-overflow-op ;