aa
TypeInt.java
Go to the documentation of this file.
1 package com.cliffc.aa.type;
2 
3 import com.cliffc.aa.util.SB;
4 import com.cliffc.aa.util.VBitSet;
5 
6 import java.util.HashMap;
7 import java.util.function.Predicate;
8 
9 public class TypeInt extends Type<TypeInt> {
10  private byte _x; // -2 bot, -1 not-null, 0 con, +1 not-null-top +2 top
11  public byte _z; // bitsiZe, one of: 1,8,16,32,64
12  private long _con; // hi or lo according to _x
13  private TypeInt init(int x, int z, long con ) { super.init(TINT,""); _x=(byte)x; _z=(byte)z; _con = con; return this; }
14  // Hash does not depend on other types
15  @Override int compute_hash() { return super.compute_hash()+_x+_z+(int)_con; }
16  @Override public boolean equals( Object o ) {
17  if( this==o ) return true;
18  if( !(o instanceof TypeInt) ) return false;
19  TypeInt t2 = (TypeInt)o;
20  return super.equals(o) && _x==t2._x && _z==t2._z && _con==t2._con;
21  }
22  @Override public boolean cycle_equals( Type o ) { return equals(o); }
23  @Override public SB str( SB sb, VBitSet dups, TypeMem mem, boolean debug ) {
24  sb.p(_name);
25  if( _con != 0 ) return sb.p(_x<0 ? "&" : (_x>0 ? "+" : "")).p(_con);
26  if( _x==0 ) return sb.p(_con);
27  return sb.p(_x>0?"~":"").p(Math.abs(_x)==1?"n":"").p("int").p(_z);
28  }
29 
30  static { new Pool(TINT,new TypeInt()); }
31  public static TypeInt make( int x, int z, long con ) {
32  if( Math.abs(x)==1 && z==1 && con==0) con=1; // not-null-bool is just a 1
33  TypeInt t1 = POOLS[TINT].malloc();
34  return t1.init(x,z,con).hashcons_free();
35  }
36 
37  public static TypeInt con(long con) { return make(0,log(con),con); }
38 
39  static public final TypeInt INT64 = make(-2,64,0);
40  static public final TypeInt INT32 = make(-2,32,0);
41  static final TypeInt INT16 = make(-2,16,0);
42  static public final TypeInt INT8 = make(-2, 8,0);
43  static public final TypeInt BOOL = make(-2, 1,0);
44  static public final TypeInt TRUE = make( 0, 1,1);
45  static public final Type FALSE = make( 0, 1,0);
46  static public final TypeInt XINT1 = make( 2, 1,0);
47  static public final TypeInt NINT8 = make(-1, 8,0);
48  static public final TypeInt NINT64 = make(-1,64,0);
49  static public final TypeInt ZERO = new TypeInt().init(0,1,0).hashcons_free();
50  static final TypeInt[] TYPES = new TypeInt[]{INT64,INT32,INT16,BOOL,TRUE,NINT64};
51  static void init1( HashMap<String,Type> types ) {
52  types.put("bool" ,BOOL);
53  types.put("int1" ,BOOL);
54  types.put("int8" ,INT8);
55  types.put("int16",INT16);
56  types.put("int32",INT32);
57  types.put("int64",INT64);
58  types.put("int" ,INT64);
59  }
60  // Return a long from a TypeInt constant; assert otherwise.
61  @Override public long getl() { assert is_con(); return _con; }
62  @Override public double getd() { assert is_con() && (long)((double)_con)==_con; return _con; }
63 
64  @Override protected TypeInt xdual() { return _x==0 ? this : new TypeInt().init(-_x,_z,_con); }
65  @Override protected Type xmeet( Type t ) {
66  assert t != this;
67  switch( t._type ) {
68  case TINT: break;
69  case TFLT: return xmeetf((TypeFlt)t);
70  case TFUNPTR:
71  case TMEMPTR:
72  case TRPC: return cross_nil(t);
73  case TFUNSIG:
74  case TARY:
75  case TLIVE:
76  case TOBJ:
77  case TSTR:
78  case TSTRUCT:
79  case TTUPLE:
80  case TMEM: return ALL;
81  default: throw typerr(t);
82  }
83  TypeInt tt = (TypeInt)t;
84  assert !equals(tt); // Already covered by interning
85  int maxz = Math.max(_z,tt._z);
86  int minz = Math.min(_z,tt._z);
87  if( _x== 0 && tt._x== 0 && _con==tt._con ) return make(0,maxz,_con);
88  if( _x<= 0 && tt._x<= 0 ) return make(Math.min(nn(),tt.nn()),maxz,0); // Both bottom, widen size
89  if( _x > 0 && tt._x > 0 ) return make(Math.min( _x,tt._x ),minz,0); // Both top, narrow size
90  if( _x==-2 && tt._x== 2 ) return this; // Top loses to other guy
91  if( _x== 2 && tt._x==-2 ) return tt ; // Top loses to other guy
92 
93  // ttop==+1,+2 and that is 0,-1,-2
94  TypeInt that = _x>0 ? tt : this;
95  TypeInt ttop = _x>0 ? this : tt;
96  if( that._x<0 ) return that; // Return low value
97  if( log(that._con) <= ttop._z && (that._con!=0 || ttop._x==2) )
98  return that; // Keep a fitting constant
99  return make(that.nn(),that._z,0); // No longer a constant
100  }
101 
102  private int nn() { assert _x <=0; return _con!=0 || _x== -1 ? -1 : -2; }
103  private static int log( long con ) {
104  if( 0 <= con && con <= 1 ) return 1;
105  if( -128 <= con && con <= 127 ) return 8;
106  if(-32768 <= con && con <= 32767 ) return 16;
107  if(Integer.MIN_VALUE <= con && con <= Integer.MAX_VALUE ) return 32;
108  return 64;
109  }
110 
112  int tx = _x;
113  if( tx > 0 ) { // Top Int, size 1 to 64
114  if( tf._x < 0 ) return tf; // (~Int | Flt) = Flt // choice includes 1 which is in all flts
115  if( tf._x > 0 ) // ~Int | ~Flt = ~Int of some type; defaults to Int tossing all the non-integral Flt choices
116  return make(Math.min(_x,tf._x),Math.min(tf._z>>1,_z),0); // Choices limited to smaller-ints (to fit in float of same range)
117  // Float constant: cast "for free" to Int constant if possible, else fall to same as Flt-bottom
118  long con = (long)tf._con;
119  // Fits in the int choices, just keep float, but could return the int constant just as well
120  if( con == tf._con && log(con) <= _z ) return tf;
121  return TypeFlt.make(con==0 ? -2 : -1, tf._z,0);
122  }
123 
124  if( tx == 0 ) { // Constant int
125  int lg = log(_con);
126  if( tf._x< 0) { // Bottom float/double
127  // Wider of (ints-wider-by-1) and floats
128  if( (lg<<1) <= tf._z ) return TypeFlt.make((_con!=0 && tf._x==-1) ? -1 : -2, tf._z,0); // Fits in a float
129  return REAL;
130  }
131  if( tf._x== 0 ) { // Int constant vs Float constant
132  if( _con==tf._con ) return this; // Matching int constant wins
133  if( ((long)tf._con) == tf._con ) // Float is a integer
134  return xmeet(TypeInt.con((long)tf._con)); // Return as-if meeting 2 integers
135  return TypeFlt.make(_con==0 || tf._con==0 ? -2 : -1,Math.max(TypeFlt.log(_con),tf._z),0);
136  }
137  // tf._x > 0 // Can a high float fall to the int constant?
138  double dcon = tf._z==32 ? (float)_con : (double)_con;
139  if( (long)dcon == _con && (_con!=0 || tf._x == 2) )
140  return this;
141  tx = _con==0 ? -2 : -1; // Fall from constant
142  } // Fall into the bottom-int case
143 
144  // Bottom Int or constant did not fit, size 1 to 64
145  if( tf._x > 0 ) return make(tx,_z,0); // ( Int | ~Flt) = Int, since can choose 1.0
146  // Float constant: cast "for free" to Int if possible, else fall to same as Flt-bottom
147  long icon = (long)tf._con;
148  if( tf._x== 0 && icon == tf._con )
149  return make(-2,Math.max(_z,log(icon)),0);
150  if( (_z<<1) <= tf._z ) return TypeFlt.make(Math.min(tx,tf._x),tf._z,0);
151  if( (_z<<1) <= 64 ) return TypeFlt.FLT64; // Fits in the float
152  return must_nil() || tf.must_nil() ? REAL : NREAL;
153  }
154 
155  // Lattice of conversions:
156  // -1 unknown; top; might fail, might be free (Scalar->Int); Scalar might lift
157  // to e.g. Float and require a user-provided rounding conversion from F64->Int.
158  // 0 requires no/free conversion (Int8->Int64, F32->F64)
159  // +1 requires a bit-changing conversion (Int->Flt)
160  // 99 Bottom; No free converts; e.g. Flt->Int requires explicit rounding
161  @Override public byte isBitShape(Type t) {
162  // TODO: Allow loss-less conversions (e.g. small float integer constants convert to ints just fine)
163  if( t._type == TXSCALAR ) return 0;
164  if( t._type == TINT ) return (byte)(_z<=((TypeInt)t)._z ? 0 : 99);
165  if( t._type == TFLT ) return 2; // Int->Flt ignores large int overflow issues
166  if( t._type == Type.TMEMPTR ) return 99; // No flt->ptr conversion
167  if( t._type == Type.TFUNPTR ) return 99; // No flt->ptr conversion
168  if( t._type == Type.TALL ) return 99;
169  if( t._type == TREAL ) return 1;
170  if( t._type == TSCALAR ) return 9; // Might have to autobox
171  if( t._type == TSTR ) return 99;
172  if( t == NIL || t == XNIL ) return 99; // Cannot not-nil to nil
173  throw com.cliffc.aa.AA.unimpl();
174  }
175  @Override public Type widen() {
176  assert _x <= 0;
177  return INT64;
178  }
179  @Override public boolean above_center() { return _x>0; }
180  @Override public boolean may_be_con() { return _x>=0; }
181  @Override public boolean is_con() { return _x==0; }
182  @Override public boolean must_nil() { return _x==-2 || (_x==0 && _con==0); }
183  @Override public boolean may_nil() { return _x>0 || (_x==0 && _con==0); }
184  @Override Type not_nil() {
185  // Choice {+0+1} ==> {+1}
186  if( this==BOOL.dual() ) return make(1,1,1);
187  // {0} ==> {0,1}
188  if( this==FALSE ) return BOOL;
189  // Choice any-int ==> any-not-nil-int
190  if( _x==2 ) return make(1,_z,_con);
191  // Never had a nil choice
192  return this;
193  }
194  @Override public Type meet_nil(Type nil) {
195  if( _x==2 ) return nil;
196  if( _x==0 && _con==0 ) return nil==Type.XNIL ? this : Type.NIL;
197  return TypeInt.make(-2,_z,0);
198  }
199  @Override public void walk( Predicate<Type> p ) { p.test(this); }
200  public TypeInt minsize(TypeInt ti) { return make(-2,Math.min(_z,ti._z),0); }
201  public TypeInt maxsize(TypeInt ti) { return make(-2,Math.max(_z,ti._z),0); }
202 }
com.cliffc.aa.type.Type< TypeInt >::TLIVE
static final byte TLIVE
Definition: Type.java:276
com.cliffc.aa.type.TypeFlt._x
byte _x
Definition: TypeFlt.java:10
com.cliffc.aa.type.TypeInt.make
static TypeInt make(int x, int z, long con)
Definition: TypeInt.java:31
com.cliffc.aa.type.TypeInt.INT8
static final TypeInt INT8
Definition: TypeInt.java:42
com.cliffc.aa.type.Type< TypeInt >::TMEMPTR
static final byte TMEMPTR
Definition: Type.java:273
com.cliffc.aa.type.TypeInt.above_center
boolean above_center()
Definition: TypeInt.java:179
com.cliffc.aa.type.Type< TypeInt >::NREAL
static final Type NREAL
Definition: Type.java:336
com.cliffc.aa.type.TypeInt.equals
boolean equals(Object o)
Definition: TypeInt.java:16
com.cliffc.aa.type.TypeInt.may_nil
boolean may_nil()
Definition: TypeInt.java:183
com.cliffc.aa.type.Type< TypeInt >::TREAL
static final byte TREAL
Definition: Type.java:255
com.cliffc.aa.type.Type< TypeInt >::typerr
RuntimeException typerr(Type t)
Definition: Type.java:947
com.cliffc.aa.type.TypeMem
Memory type; the state of all of memory; memory edges order memory ops.
Definition: TypeMem.java:53
com.cliffc.aa.type.TypeInt.widen
Type widen()
Definition: TypeInt.java:175
com.cliffc
com.cliffc.aa.type.TypeInt._con
long _con
Definition: TypeInt.java:12
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.type.TypeFlt._con
double _con
Definition: TypeFlt.java:12
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.type.TypeInt.not_nil
Type not_nil()
Definition: TypeInt.java:184
com.cliffc.aa.type.TypeFlt
Definition: TypeFlt.java:9
com.cliffc.aa.type.Type< TypeInt >::TSTR
static final byte TSTR
Definition: Type.java:270
com.cliffc.aa.type.TypeInt._z
byte _z
Definition: TypeInt.java:11
com.cliffc.aa.type.Type._type
byte _type
Definition: Type.java:98
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.type.TypeInt.getd
double getd()
Definition: TypeInt.java:62
com.cliffc.aa.type.TypeInt.init
TypeInt init(int x, int z, long con)
Definition: TypeInt.java:13
com.cliffc.aa.type.TypeInt.must_nil
boolean must_nil()
Definition: TypeInt.java:182
com.cliffc.aa.AA.unimpl
static RuntimeException unimpl()
Definition: AA.java:10
com.cliffc.aa.type.Type< TypeInt >::TFUNPTR
static final byte TFUNPTR
Definition: Type.java:274
com.cliffc.aa.type.Type< TypeInt >::TINT
static final byte TINT
Definition: Type.java:263
com.cliffc.aa.type.TypeInt.cycle_equals
boolean cycle_equals(Type o)
Definition: TypeInt.java:22
com.cliffc.aa.type.Type< TypeInt >::ALL
static final Type ALL
Definition: Type.java:324
com.cliffc.aa.type.Type< TypeInt >::TMEM
static final byte TMEM
Definition: Type.java:272
com.cliffc.aa.type.TypeInt.walk
void walk(Predicate< Type > p)
Definition: TypeInt.java:199
com.cliffc.aa.type.TypeInt.nn
int nn()
Definition: TypeInt.java:102
com.cliffc.aa.type.Type< TypeInt >::TTUPLE
static final byte TTUPLE
Definition: Type.java:266
com.cliffc.aa.type.TypeInt.INT64
static final TypeInt INT64
Definition: TypeInt.java:39
com.cliffc.aa.type.Type< TypeInt >::TSTRUCT
static final byte TSTRUCT
Definition: Type.java:268
com.cliffc.aa.type.TypeInt.compute_hash
int compute_hash()
Definition: TypeInt.java:15
com.cliffc.aa.type.Type.TALL
static final byte TALL
Definition: Type.java:239
com.cliffc.aa.type.TypeInt.maxsize
TypeInt maxsize(TypeInt ti)
Definition: TypeInt.java:201
com.cliffc.aa.type.TypeInt.str
SB str(SB sb, VBitSet dups, TypeMem mem, boolean debug)
Definition: TypeInt.java:23
com.cliffc.aa.type.TypeFlt.make
static Type make(int x, int z, double con)
Definition: TypeFlt.java:30
com.cliffc.aa.type.TypeInt.is_con
boolean is_con()
Definition: TypeInt.java:181
com.cliffc.aa.type.Type< TypeInt >::cross_nil
final Type cross_nil(Type t)
Definition: Type.java:865
com.cliffc.aa.type.TypeInt.NINT8
static final TypeInt NINT8
Definition: TypeInt.java:47
com.cliffc.aa.type.TypeInt.INT32
static final TypeInt INT32
Definition: TypeInt.java:40
com.cliffc.aa.type.TypeInt.isBitShape
byte isBitShape(Type t)
Definition: TypeInt.java:161
com.cliffc.aa.type.TypeInt.may_be_con
boolean may_be_con()
Definition: TypeInt.java:180
com.cliffc.aa.type.TypeFlt.log
static int log(double con)
Definition: TypeFlt.java:90
com.cliffc.aa.type.Type< TypeInt >::TSCALAR
static final byte TSCALAR
Definition: Type.java:246
com.cliffc.aa.type.Type< TypeInt >::TXSCALAR
static final byte TXSCALAR
Definition: Type.java:247
com.cliffc.aa.type.Type< TypeInt >::TRPC
static final byte TRPC
Definition: Type.java:265
com.cliffc.aa.util.VBitSet
Definition: VBitSet.java:5
com.cliffc.aa.type.TypeInt.TYPES
static final TypeInt[] TYPES
Definition: TypeInt.java:50
com.cliffc.aa.type.TypeInt.xmeet
Type xmeet(Type t)
Definition: TypeInt.java:65
com.cliffc.aa.type.Type.hashcons_free
final T hashcons_free()
Definition: Type.java:153
com.cliffc.aa.util.SB
Tight/tiny StringBuilder wrapper.
Definition: SB.java:8
com.cliffc.aa.type.TypeInt._x
byte _x
Definition: TypeInt.java:10
com.cliffc.aa.type.Type< TypeInt >::NIL
static final Type NIL
Definition: Type.java:332
com.cliffc.aa.AA
an implementation of language AA
Definition: AA.java:9
com.cliffc.aa.type.TypeInt.NINT64
static final TypeInt NINT64
Definition: TypeInt.java:48
com.cliffc.aa.type.TypeInt.XINT1
static final TypeInt XINT1
Definition: TypeInt.java:46
com.cliffc.aa.type.TypeFlt._z
byte _z
Definition: TypeFlt.java:11
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.type.TypeInt.meet_nil
Type meet_nil(Type nil)
Definition: TypeInt.java:194
com.cliffc.aa.type.TypeInt.INT16
static final TypeInt INT16
Definition: TypeInt.java:41
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.type.TypeInt.FALSE
static final Type FALSE
Definition: TypeInt.java:45
com.cliffc.aa.type.Type< TypeInt >::_name
String _name
Definition: Type.java:99
com.cliffc.aa.type.Type.dual
final T dual()
Definition: Type.java:361
com.cliffc.aa.type.Type< TypeInt >::REAL
static final Type REAL
Definition: Type.java:334
com.cliffc.aa.type.TypeFlt.must_nil
boolean must_nil()
Definition: TypeFlt.java:95
com.cliffc.aa.type.TypeInt.getl
long getl()
Definition: TypeInt.java:61
com.cliffc.aa.type.Type< TypeInt >::XNIL
static final Type XNIL
Definition: Type.java:333
com.cliffc.aa.type.Type< TypeInt >::POOLS
static final Pool[] POOLS
Definition: Type.java:281
com.cliffc.aa.type.TypeInt.ZERO
static final TypeInt ZERO
Definition: TypeInt.java:49
com.cliffc.aa.type.Type< TypeInt >::TOBJ
static final byte TOBJ
Definition: Type.java:267
com.cliffc.aa.type.TypeInt.minsize
TypeInt minsize(TypeInt ti)
Definition: TypeInt.java:200
com.cliffc.aa.type.TypeInt.xdual
TypeInt xdual()
Definition: TypeInt.java:64
com.cliffc.aa.type.TypeInt.TRUE
static final TypeInt TRUE
Definition: TypeInt.java:44
com
com.cliffc.aa.type.TypeInt.xmeetf
Type xmeetf(TypeFlt tf)
Definition: TypeInt.java:111
com.cliffc.aa.type.TypeInt.init1
static void init1(HashMap< String, Type > types)
Definition: TypeInt.java:51
com.cliffc.aa.type.TypeInt.BOOL
static final TypeInt BOOL
Definition: TypeInt.java:43
com.cliffc.aa.type.TypeInt.log
static int log(long con)
Definition: TypeInt.java:103
com.cliffc.aa.type.Type< TypeInt >::TFUNSIG
static final byte TFUNSIG
Definition: Type.java:275
com.cliffc.aa.type.Type< TypeInt >::TARY
static final byte TARY
Definition: Type.java:269
com.cliffc.aa.type.TypeFlt.FLT64
static final TypeFlt FLT64
Definition: TypeFlt.java:38
com.cliffc.aa.type.Type< TypeInt >::TFLT
static final byte TFLT
Definition: Type.java:264