aa
TypeAry.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 // A TypeObj where fields are indexed by dynamic integer.
7 public class TypeAry extends TypeObj<TypeAry> {
8  public TypeInt _size; // Count of elements
9  private Type _elem; // MEET over all elements.
10  private TypeObj _stor; // Storage class; widened over elements. Can be, e.g. bits or complex structs with embedded pointers
11 
12  private TypeAry init(String name, boolean any, TypeInt sz, Type elem, TypeObj stor ) {
13  super.init(TARY,name,any,any);
14  _size = sz;
15  _elem = elem;
16  _stor = stor;
17  return this;
18  }
19  @Override int compute_hash() { return super.compute_hash() + _size._hash + _elem._hash + _stor._hash; }
20  @Override public boolean equals( Object o ) {
21  if( this==o ) return true;
22  if( !(o instanceof TypeAry) || !super.equals(o) ) return false;
23  TypeAry ta = (TypeAry)o;
24  return _size == ta._size && _elem == ta._elem && _stor == ta._stor;
25  }
26  @Override public boolean cycle_equals( Type o ) { return equals(o); }
27  @Override public SB str( SB sb, VBitSet dups, TypeMem mem, boolean debug ) {
28  if( _any ) sb.p('~');
29  sb.p('[');
30  if( _size!=null && _size != TypeInt.INT64 ) sb.p(_size);
31  sb.p(']');
32  if( _elem !=null ) sb.p(_elem);
33  if( _elem != _stor && _stor!=null ) sb.p('/').p(_stor);
34  return sb;
35  }
36 
37  static { new Pool(TARY,new TypeAry()); }
38  public static TypeAry make( String name, boolean any, TypeInt sz, Type elem, TypeObj stor ) {
39  TypeAry t1 = POOLS[TARY].malloc();
40  return t1.init(name,any,sz,elem,stor).hashcons_free();
41  }
42 
43  public static TypeAry make( TypeInt sz, Type elem, TypeObj stor ) { return make("",false,sz,elem,stor); }
44  public static final TypeAry ARY = make("",false,TypeInt.INT64 ,Type.SCALAR ,TypeObj.OBJ );
45  public static final TypeAry ARY0 = make("",false,TypeInt.INT64 ,Type.XNIL ,TypeObj.OBJ );
46  public static final TypeAry BYTES = make("",false,TypeInt.con(3),TypeInt.INT8,TypeObj.OBJ ); // TODO: TypeObjBits2
47  static final TypeAry[] TYPES = new TypeAry[]{ARY,ARY0,BYTES};
48 
49  @Override protected TypeAry xdual() { return new TypeAry().init(_name, !_any,_size.dual(),_elem.dual(),(TypeObj)_stor.dual()); }
50  @Override
52  if( _dual != null ) return _dual;
53  TypeAry dual = _dual = xdual();
54  dual._dual = this;
55  dual._hash = dual.compute_hash();
56  return dual;
57  }
58  @Override protected Type xmeet( Type t ) {
59  switch( t._type ) {
60  case TARY: break;
61  case TSTR:
62  case TLIVE:
63  case TSTRUCT:return OBJ;
64  case TOBJ: return t.xmeet(this);
65  case TFUNSIG:
66  case TTUPLE:
67  case TFUNPTR:
68  case TMEMPTR:
69  case TFLT:
70  case TINT:
71  case TRPC:
72  case TMEM: return ALL;
73  default: throw typerr(t);
74  }
75  TypeAry ta = (TypeAry)t;
76  boolean any = _any&ta._any;
77  TypeInt size = (TypeInt)_size.meet(ta._size);
78  Type elem = _elem.meet(ta._elem);
79  TypeObj stor = (TypeObj)_stor.meet(ta._stor);
80  return make("",any,size,elem,stor);
81  }
82 
83  // Widen (lose info), to make it suitable as the default function memory.
84  // All elements widened to SCALAR.
85  @Override public TypeAry crush() {
86  if( _any ) return this; // No crush on high arrays
87  return make(_size,Type.SCALAR,_stor);
88  }
89 
90  public Type ld(TypeInt idx) {
91  return _elem;
92  }
93  @Override public TypeObj update(TypeInt idx, Type val) {
94  if( idx.above_center() ) return this; // Nothing updates
95  if( val.isa(_elem) ) return this; // No change
96  Type elem = _elem.meet(val); // Worse-case
97  TypeInt size = _size; // TypeInt size = (TypeInt)_size.meet(idx); // CNC - Not inferring array size yet
98  return make(size,elem,TypeObj.OBJ);
99  }
100  // Used during liveness propagation from Loads.
101  // Fields not-loaded are not-live.
102  @Override TypeAry remove_other_flds(String fld, Type live) {
103  return ARY;
104  }
105 }
com.cliffc.aa.type.TypeAry.compute_hash
int compute_hash()
Definition: TypeAry.java:19
com.cliffc.aa.type.TypeInt.INT8
static final TypeInt INT8
Definition: TypeInt.java:42
com.cliffc.aa.type.TypeInt.above_center
boolean above_center()
Definition: TypeInt.java:179
com.cliffc.aa.type.Type.isa
boolean isa(Type t)
Definition: Type.java:623
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.TypeObj< TypeAry >::_any
boolean _any
Definition: TypeObj.java:16
com.cliffc.aa.type.Type.SCALAR
static final Type SCALAR
Definition: Type.java:328
com.cliffc
com.cliffc.aa.type.Type._hash
int _hash
Definition: Type.java:97
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.type.TypeAry.ld
Type ld(TypeInt idx)
Definition: TypeAry.java:90
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.type.Type._type
byte _type
Definition: Type.java:98
com.cliffc.aa.type.TypeAry.make
static TypeAry make(TypeInt sz, Type elem, TypeObj stor)
Definition: TypeAry.java:43
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.type.TypeAry.str
SB str(SB sb, VBitSet dups, TypeMem mem, boolean debug)
Definition: TypeAry.java:27
com.cliffc.aa.type.TypeAry._elem
Type _elem
Definition: TypeAry.java:9
com.cliffc.aa.type.Type.meet
final Type meet(Type t)
Definition: Type.java:412
com.cliffc.aa.type.TypeAry
Definition: TypeAry.java:7
com.cliffc.aa.type.TypeAry.crush
TypeAry crush()
Definition: TypeAry.java:85
com.cliffc.aa.type.TypeAry.init
TypeAry init(String name, boolean any, TypeInt sz, Type elem, TypeObj stor)
Definition: TypeAry.java:12
com.cliffc.aa.type.TypeAry.cycle_equals
boolean cycle_equals(Type o)
Definition: TypeAry.java:26
com.cliffc.aa.type.TypeInt.INT64
static final TypeInt INT64
Definition: TypeInt.java:39
com.cliffc.aa.type.TypeAry.BYTES
static final TypeAry BYTES
Definition: TypeAry.java:46
com.cliffc.aa.type.TypeObj.OBJ
static final TypeObj OBJ
Definition: TypeObj.java:44
com.cliffc.aa.type.TypeObj
Definition: TypeObj.java:15
com.cliffc.aa.type.TypeAry.update
TypeObj update(TypeInt idx, Type val)
Definition: TypeAry.java:93
com.cliffc.aa.type.TypeAry.ARY
static final TypeAry ARY
Definition: TypeAry.java:44
com.cliffc.aa.type.TypeAry.TYPES
static final TypeAry[] TYPES
Definition: TypeAry.java:47
com.cliffc.aa.type.TypeAry._size
TypeInt _size
Definition: TypeAry.java:8
com.cliffc.aa.type.TypeAry._stor
TypeObj _stor
Definition: TypeAry.java:10
com.cliffc.aa.type.TypeAry.make
static TypeAry make(String name, boolean any, TypeInt sz, Type elem, TypeObj stor)
Definition: TypeAry.java:38
com.cliffc.aa.type.TypeAry.equals
boolean equals(Object o)
Definition: TypeAry.java:20
com.cliffc.aa.type.Type.xmeet
Type xmeet(Type t)
Definition: Type.java:461
com.cliffc.aa.type.TypeAry.ARY0
static final TypeAry ARY0
Definition: TypeAry.java:45
com.cliffc.aa.util.VBitSet
Definition: VBitSet.java:5
com.cliffc.aa.util.SB
Tight/tiny StringBuilder wrapper.
Definition: SB.java:8
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.type.TypeAry.xdual
TypeAry xdual()
Definition: TypeAry.java:49
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.type.Type.dual
final T dual()
Definition: Type.java:361
com.cliffc.aa.type.TypeAry.xmeet
Type xmeet(Type t)
Definition: TypeAry.java:58
com.cliffc.aa.type.Type.XNIL
static final Type XNIL
Definition: Type.java:333
com.cliffc.aa.type.TypeAry.remove_other_flds
TypeAry remove_other_flds(String fld, Type live)
Definition: TypeAry.java:102
com
com.cliffc.aa.type.TypeAry.rdual
TypeAry rdual()
Definition: TypeAry.java:51