aa
com.cliffc.aa.HM.HM9 Class Reference
Collaboration diagram for com.cliffc.aa.HM.HM9:
[legend]

Classes

class  Apply
 
class  Con
 
class  Dec
 
class  EQ
 
class  EQ0
 
class  Factor
 
class  Field
 
class  Ident
 
class  If
 
class  IsEmpty
 
class  Lambda
 
class  Let
 
class  Mul
 
class  NotNil
 
class  Pair
 
class  Pair1
 
class  PrimSyn
 
class  Root
 
class  Str
 
class  Struct
 
class  Syntax
 
class  T2
 
class  Triple
 
class  VStack
 
class  Worklist
 

Public Member Functions

String toString ()
 

Static Public Member Functions

static Root hm (String sprog)
 

Static Package Functions

 [static initializer]
 
static Root parse (String s)
 
static void reset ()
 
static Syntax term ()
 

Static Package Attributes

static final boolean DEBUG_LEAKS =false
 
static final boolean DO_GCP = true
 
static final boolean DO_HM = true
 
static final HashMap< String, PrimSynPRIMSYNS = new HashMap<>()
 

Static Private Member Functions

static String id ()
 
static boolean isAlpha0 (byte c)
 
static boolean isAlpha1 (byte c)
 
static boolean isDigit (byte c)
 
static boolean isWS (byte c)
 
static Syntax number ()
 
static void require (char c)
 
static< T > T require (char c, T t)
 
static void require (String s)
 
static byte skipWS ()
 
static Syntax string ()
 

Static Private Attributes

static byte[] BUF
 
static final SB ID = new SB()
 
static int X
 

Detailed Description

Definition at line 71 of file HM9.java.

Member Function Documentation

◆ [static initializer]()

com.cliffc.aa.HM.HM9.[static initializer]
staticpackage

◆ hm()

static Root com.cliffc.aa.HM.HM9.hm ( String  sprog)
static

Definition at line 81 of file HM9.java.

81  {
82  Worklist work = new Worklist();
83  PrimSyn.WORK=work;
84 
85  for( PrimSyn prim : new PrimSyn[]{new If(), new Pair1(), new Pair(), new EQ(), new EQ0(), new Mul(), new Dec(), new Str(), new Triple(), new Factor(), new IsEmpty(), new NotNil()} )
86  PRIMSYNS.put(prim.name(),prim);
87 
88  // Parse
89  Root prog = parse( sprog );
90 
91  // Prep for SSA: pre-gather all the (unique) ids
92  int cnt_syns = prog.prep_tree(null,null,work);
93  int init_T2s = T2.CNT;
94 
95  while( work.len()>0 ) { // While work
96  int oldcnt = T2.CNT; // Used for cost-check when no-progress
97  assert work._cnt<1000;
98  Syntax syn = work.pop(); // Get work
99  if( DO_HM ) {
100  T2 old = syn._hmt; // Old value for progress assert
101  if( syn.hm(work) ) {
102  assert !syn.debug_find().unify(old.find(),null);// monotonic: unifying with the result is no-progress
103  syn.add_hm_work(work); // Push affected neighbors on worklist
104  } else {
105  assert !DEBUG_LEAKS || oldcnt==T2.CNT; // No-progress consumes no-new-T2s
106  }
107  }
108  if( DO_GCP ) {
109  Type old = syn._flow;
110  Type t = syn.val(work);
111  if( t!=old ) { // Progress
112  assert old.isa(t); // Monotonic falling
113  syn._flow = t; // Update type
114  if( syn._par!=null ) { // Generally, parent needs revisit
115  work.push(syn._par); // Assume parent needs revisit
116  syn._par.add_val_work(syn,work); // Push affected neighbors on worklist
117  }
118  }
119  }
120 
121  // VERY EXPENSIVE ASSERT: O(n^2). Every Syntax that makes progress is on the worklist
122  assert prog.more_work(work);
123  }
124  assert prog.more_work(work);
125 
126  System.out.println("Initial T2s: "+init_T2s+", Prog size: "+cnt_syns+", worklist iters: "+work._cnt+", T2s: "+T2.CNT);
127  return prog;
128  }

References com.cliffc.aa.HM.HM9.Worklist._cnt, com.cliffc.aa.HM.HM9.Syntax._flow, com.cliffc.aa.HM.HM9.Syntax._hmt, com.cliffc.aa.HM.HM9.Syntax._par, com.cliffc.aa.HM.HM9.Syntax.add_hm_work(), com.cliffc.aa.HM.HM9.Syntax.add_val_work(), com.cliffc.aa.HM.HM9.T2.CNT, com.cliffc.aa.HM.HM9.Syntax.debug_find(), com.cliffc.aa.HM.HM9.DEBUG_LEAKS, com.cliffc.aa.HM.HM9.DO_GCP, com.cliffc.aa.HM.HM9.DO_HM, com.cliffc.aa.HM.HM9.T2.find(), com.cliffc.aa.HM.HM9.Syntax.hm(), com.cliffc.aa.type.Type< T extends Type< T >.isa(), com.cliffc.aa.HM.HM9.Worklist.len(), com.cliffc.aa.HM.HM9.Apply.more_work(), com.cliffc.aa.HM.HM9.parse(), com.cliffc.aa.HM.HM9.Worklist.pop(), com.cliffc.aa.HM.HM9.Apply.prep_tree(), com.cliffc.aa.HM.HM9.PRIMSYNS, com.cliffc.aa.HM.HM9.Worklist.push(), com.cliffc.aa.HM.HM9.T2.unify(), com.cliffc.aa.HM.HM9.Syntax.val(), and com.cliffc.aa.HM.HM9.PrimSyn.WORK.

Referenced by com.cliffc.aa.HM.TestHM9.run(), com.cliffc.aa.HM.TestHM9.test05(), com.cliffc.aa.HM.TestHM9.test06(), com.cliffc.aa.HM.TestHM9.test14(), com.cliffc.aa.HM.TestHM9.test18(), com.cliffc.aa.HM.TestHM9.test20(), com.cliffc.aa.HM.TestHM9.test32(), com.cliffc.aa.HM.TestHM9.test33(), com.cliffc.aa.HM.TestHM9.test34(), com.cliffc.aa.HM.TestHM9.test36(), com.cliffc.aa.HM.TestHM9.test39(), com.cliffc.aa.HM.TestHM9.test40(), com.cliffc.aa.HM.TestHM9.test41(), com.cliffc.aa.HM.TestHM9.test42(), com.cliffc.aa.HM.TestHM9.test43(), com.cliffc.aa.HM.TestHM9.test44(), com.cliffc.aa.HM.TestHM9.test45(), com.cliffc.aa.HM.TestHM9.test49(), and com.cliffc.aa.HM.TestHM9.test50().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ id()

static String com.cliffc.aa.HM.HM9.id ( )
staticprivate

Definition at line 224 of file HM9.java.

224  {
225  ID.clear();
226  while( X<BUF.length && isAlpha1(BUF[X]) )
227  ID.p((char)BUF[X++]);
228  String s = ID.toString().intern();
229  if( s.length()==0 ) throw unimpl("Missing id");
230  return s;
231  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.util.SB.clear(), com.cliffc.aa.HM.HM9.ID, com.cliffc.aa.HM.HM9.isAlpha1(), com.cliffc.aa.util.SB.p(), com.cliffc.aa.util.SB.toString(), and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.T2.add_fld(), com.cliffc.aa.HM.HM9.Field.Field(), and com.cliffc.aa.HM.HM9.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isAlpha0()

static boolean com.cliffc.aa.HM.HM9.isAlpha0 ( byte  c)
staticprivate

Definition at line 255 of file HM9.java.

255 { return ('a'<=c && c <= 'z') || ('A'<=c && c <= 'Z') || (c=='_') || (c=='*') || (c=='?'); }

Referenced by com.cliffc.aa.HM.HM9.isAlpha1(), and com.cliffc.aa.HM.HM9.term().

Here is the caller graph for this function:

◆ isAlpha1()

static boolean com.cliffc.aa.HM.HM9.isAlpha1 ( byte  c)
staticprivate

Definition at line 256 of file HM9.java.

256 { return isAlpha0(c) || ('0'<=c && c <= '9') || (c=='/'); }

References com.cliffc.aa.HM.HM9.isAlpha0().

Referenced by com.cliffc.aa.HM.HM9.id().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDigit()

static boolean com.cliffc.aa.HM.HM9.isDigit ( byte  c)
staticprivate

Definition at line 254 of file HM9.java.

254 { return '0' <= c && c <= '9'; }

Referenced by com.cliffc.aa.HM.HM9.T2.is_tuple(), com.cliffc.aa.HM.HM9.number(), and com.cliffc.aa.HM.HM9.term().

Here is the caller graph for this function:

◆ isWS()

static boolean com.cliffc.aa.HM.HM9.isWS ( byte  c)
staticprivate

Definition at line 253 of file HM9.java.

253 { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; }

Referenced by com.cliffc.aa.HM.HM9.skipWS().

Here is the caller graph for this function:

◆ number()

static Syntax com.cliffc.aa.HM.HM9.number ( )
staticprivate

Definition at line 232 of file HM9.java.

232  {
233  if( BUF[X]=='0' ) { X++; return new Con(Type.XNIL); }
234  int sum=0;
235  while( X<BUF.length && isDigit(BUF[X]) )
236  sum = sum*10+BUF[X++]-'0';
237  if( X>= BUF.length || BUF[X]!='.' )
238  return new Con(TypeInt.con(sum));
239  X++;
240  float f = (float)sum;
241  f = f + (BUF[X++]-'0')/10.0f;
242  return new Con(TypeFlt.con(f));
243  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.type.TypeFlt.con(), com.cliffc.aa.type.TypeInt.con(), com.cliffc.aa.HM.HM9.isDigit(), com.cliffc.aa.HM.HM9.X, and com.cliffc.aa.type.Type< T extends Type< T >.XNIL.

Referenced by com.cliffc.aa.HM.HM9.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse()

static Root com.cliffc.aa.HM.HM9.parse ( String  s)
staticpackage

Definition at line 145 of file HM9.java.

145  {
146  X = 0;
147  BUF = s.getBytes();
148  Syntax prog = term();
149  if( skipWS() != -1 ) throw unimpl("Junk at end of program: "+new String(BUF,X,BUF.length-X));
150  // Inject IF at root
151  return new Root(prog);
152  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.HM.HM9.skipWS(), com.cliffc.aa.HM.HM9.term(), and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.hm().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ require() [1/3]

static void com.cliffc.aa.HM.HM9.require ( char  c)
staticprivate

Definition at line 257 of file HM9.java.

257 { if( skipWS()!=c ) throw unimpl("Missing '"+c+"'"); X++; }

References com.cliffc.aa.HM.HM9.skipWS(), and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.string(), and com.cliffc.aa.HM.HM9.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ require() [2/3]

static <T> T com.cliffc.aa.HM.HM9.require ( char  c,
t 
)
staticprivate

Definition at line 258 of file HM9.java.

258 { require(c); return t; }

References com.cliffc.aa.HM.HM9.require().

Referenced by com.cliffc.aa.HM.HM9.require().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ require() [3/3]

static void com.cliffc.aa.HM.HM9.require ( String  s)
staticprivate

Definition at line 259 of file HM9.java.

259  {
260  skipWS();
261  for( int i=0; i<s.length(); i++ )
262  if( X+i >= BUF.length || BUF[X+i]!=s.charAt(i) )
263  throw unimpl("Missing '"+s+"'");
264  X+=s.length();
265  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.HM.HM9.skipWS(), and com.cliffc.aa.HM.HM9.X.

Here is the call graph for this function:

◆ reset()

static void com.cliffc.aa.HM.HM9.reset ( )
staticpackage

Definition at line 130 of file HM9.java.

130  {
133  PRIMSYNS.clear();
134  Pair1.PAIR1S.clear();
135  Lambda.FUNS.clear();
136  T2.reset();
137  PrimSyn.reset();
138  }

References com.cliffc.aa.HM.HM9.Lambda.FUNS, com.cliffc.aa.HM.HM9.Pair1.PAIR1S, com.cliffc.aa.HM.HM9.PRIMSYNS, com.cliffc.aa.HM.HM9.PrimSyn.reset(), com.cliffc.aa.HM.HM9.T2.reset(), com.cliffc.aa.type.BitsFun.reset_to_init0(), and com.cliffc.aa.type.BitsAlias.reset_to_init0().

Here is the call graph for this function:

◆ skipWS()

static byte com.cliffc.aa.HM.HM9.skipWS ( )
staticprivate

Definition at line 249 of file HM9.java.

249  {
250  while( X<BUF.length && isWS(BUF[X]) ) X++;
251  return X==BUF.length ? -1 : BUF[X];
252  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.HM.HM9.isWS(), and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.parse(), com.cliffc.aa.HM.HM9.require(), and com.cliffc.aa.HM.HM9.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string()

static Syntax com.cliffc.aa.HM.HM9.string ( )
staticprivate

Definition at line 244 of file HM9.java.

244  {
245  int start = ++X;
246  while( X<BUF.length && BUF[X]!='"' ) X++;
247  return require('"', new Con(TypeMemPtr.make(BitsAlias.STRBITS,TypeStr.con(new String(BUF,start,X-start).intern()))));
248  }

References com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.type.TypeStr.con(), com.cliffc.aa.type.TypeMemPtr.make(), com.cliffc.aa.HM.HM9.require(), com.cliffc.aa.type.BitsAlias.STRBITS, and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ term()

static Syntax com.cliffc.aa.HM.HM9.term ( )
staticpackage

Definition at line 153 of file HM9.java.

153  {
154  if( skipWS()==-1 ) return null;
155  if( isDigit(BUF[X]) ) return number();
156  if( BUF[X]=='"' ) return string();
157 
158  if( BUF[X]=='(' ) { // Parse an Apply
159  X++; // Skip paren
160  Syntax fun = term();
161  Ary<Syntax> args = new Ary<>(new Syntax[1],0);
162  while( skipWS()!= ')' && X<BUF.length ) args.push(term());
163  require(')');
164  // Guarding if-nil test inserts an upcast. This is a syntatic transform only.
165  if( fun instanceof If &&
166  args.at(0) instanceof Ident ) {
167  Ident id = (Ident)args.at(0);
168  args.set(1,new Apply(new Lambda(args.at(1), id._name),
169  new Apply(new NotNil(),new Ident(id._name))));
170  }
171  return new Apply(fun,args.asAry());
172  }
173 
174  if( BUF[X]=='{' ) { // Lambda of 1 or 2 args
175  X++; // Skip paren
176  Ary<String> args = new Ary<>(new String[1],0);
177  while( skipWS()!='-' ) args.push(id());
178  require("->");
179  Syntax body = term();
180  require('}');
181  return new Lambda(body,args.asAry());
182  }
183  // Let or Id
184  if( isAlpha0(BUF[X]) ) {
185  String id = id();
186  if( skipWS()!='=' ) {
187  PrimSyn prim = PRIMSYNS.get(id); // No shadowing primitives or this lookup returns the prim instead of the shadow
188  return prim==null ? new Ident(id) : prim.make(); // Make a prim copy with fresh HM variables
189  }
190  // Let expression; "id = term(); term..."
191  X++; // Skip '='
192  Syntax def = term();
193  require(';');
194  return new Let(id,def,term());
195  }
196 
197  // Structure
198  if( BUF[X]=='@' ) {
199  X++;
200  require('{');
201  Ary<String> ids = new Ary<>(String.class);
202  Ary<Syntax> flds = new Ary<>(Syntax.class);
203  while( skipWS()!='}' && X < BUF.length ) {
204  String id = require('=',id());
205  Syntax fld = term();
206  if( fld==null ) throw unimpl("Missing term for field "+id);
207  ids .push( id);
208  flds.push(fld);
209  if( skipWS()==',' ) X++;
210  }
211  require('}');
212  return new Struct(ids.asAry(),flds.asAry());
213  }
214 
215  // Field lookup is prefix or backwards: ".x term" instead of "term.x"
216  if( BUF[X]=='.' ) {
217  X++;
218  return new Field(id(),term());
219  }
220 
221  throw unimpl("Unknown syntax");
222  }

References com.cliffc.aa.util.Ary< E >.asAry(), com.cliffc.aa.util.Ary< E >.at(), com.cliffc.aa.HM.HM9.BUF, com.cliffc.aa.HM.HM9.id(), com.cliffc.aa.HM.HM9.isAlpha0(), com.cliffc.aa.HM.HM9.isDigit(), com.cliffc.aa.HM.HM9.PrimSyn.make(), com.cliffc.aa.HM.HM9.number(), com.cliffc.aa.HM.HM9.PRIMSYNS, com.cliffc.aa.util.Ary< E >.push(), com.cliffc.aa.HM.HM9.require(), com.cliffc.aa.util.Ary< E >.set(), com.cliffc.aa.HM.HM9.skipWS(), com.cliffc.aa.HM.HM9.string(), and com.cliffc.aa.HM.HM9.X.

Referenced by com.cliffc.aa.HM.HM9.parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

String com.cliffc.aa.HM.HM9.toString ( )

Definition at line 144 of file HM9.java.

144 { return new String(BUF,X,BUF.length-X); }

References com.cliffc.aa.HM.HM9.BUF, and com.cliffc.aa.HM.HM9.X.

Member Data Documentation

◆ BUF

◆ DEBUG_LEAKS

final boolean com.cliffc.aa.HM.HM9.DEBUG_LEAKS =false
staticpackage

Definition at line 75 of file HM9.java.

Referenced by com.cliffc.aa.HM.HM9.T2.fresh_unify(), and com.cliffc.aa.HM.HM9.hm().

◆ DO_GCP

final boolean com.cliffc.aa.HM.HM9.DO_GCP = true
staticpackage

◆ DO_HM

final boolean com.cliffc.aa.HM.HM9.DO_HM = true
staticpackage

◆ ID

final SB com.cliffc.aa.HM.HM9.ID = new SB()
staticprivate

Definition at line 223 of file HM9.java.

Referenced by com.cliffc.aa.HM.HM9.id().

◆ PRIMSYNS

final HashMap<String,PrimSyn> com.cliffc.aa.HM.HM9.PRIMSYNS = new HashMap<>()
staticpackage

◆ X


The documentation for this class was generated from the following file:
com.cliffc.aa.util.Ary.at
E at(int i)
Definition: Ary.java:25
com.cliffc.aa.util.Ary.push
E push(E e)
Add element in amortized constant time.
Definition: Ary.java:58
com.cliffc.aa.type.Type.isa
boolean isa(Type t)
Definition: Type.java:623
com.cliffc.aa.HM.HM9.skipWS
static byte skipWS()
Definition: HM9.java:249
com.cliffc.aa.HM.HM9.DEBUG_LEAKS
static final boolean DEBUG_LEAKS
Definition: HM9.java:75
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.util.SB.clear
SB clear()
Definition: SB.java:61
com.cliffc.aa.type.TypeFlt
Definition: TypeFlt.java:9
com.cliffc.aa.util.Ary
Definition: Ary.java:11
com.cliffc.aa.type.BitsAlias
Definition: BitsAlias.java:8
com.cliffc.aa.HM.HM9.number
static Syntax number()
Definition: HM9.java:232
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.HM.HM9.term
static Syntax term()
Definition: HM9.java:153
com.cliffc.aa.HM.HM9.isDigit
static boolean isDigit(byte c)
Definition: HM9.java:254
com.cliffc.aa.HM.HM9.string
static Syntax string()
Definition: HM9.java:244
com.cliffc.aa.util.Ary.asAry
E[] asAry()
Definition: Ary.java:172
com.cliffc.aa.type.TypeFlt.con
static Type con(double con)
Definition: TypeFlt.java:36
com.cliffc.aa.HM.HM9.parse
static Root parse(String s)
Definition: HM9.java:145
com.cliffc.aa.type.TypeStr.con
static TypeStr con(String con)
Definition: TypeStr.java:42
com.cliffc.aa.HM.HM9.isAlpha1
static boolean isAlpha1(byte c)
Definition: HM9.java:256
com.cliffc.aa.type.TypeStr
Definition: TypeStr.java:14
com.cliffc.aa.HM.HM9.id
static String id()
Definition: HM9.java:224
com.cliffc.aa.type.BitsFun
Definition: BitsFun.java:7
com.cliffc.aa.type.BitsAlias.STRBITS
static BitsAlias STRBITS
Definition: BitsAlias.java:27
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.HM.HM9.require
static void require(char c)
Definition: HM9.java:257
com.cliffc.aa.HM.HM9.X
static int X
Definition: HM9.java:142
com.cliffc.aa.type.BitsFun.reset_to_init0
static void reset_to_init0()
Definition: BitsFun.java:29
com.cliffc.aa.HM.HM9.ID
static final SB ID
Definition: HM9.java:223
com.cliffc.aa.HM.HM9.isWS
static boolean isWS(byte c)
Definition: HM9.java:253
com.cliffc.aa.HM.HM9.PRIMSYNS
static final HashMap< String, PrimSyn > PRIMSYNS
Definition: HM9.java:73
com.cliffc.aa.type.Type.XNIL
static final Type XNIL
Definition: Type.java:333
com.cliffc.aa.util.Ary.set
E set(int i, E e)
Set existing element.
Definition: Ary.java:133
com.cliffc.aa.HM.HM9.isAlpha0
static boolean isAlpha0(byte c)
Definition: HM9.java:255
com.cliffc.aa.HM.HM9.DO_GCP
static final boolean DO_GCP
Definition: HM9.java:79
com.cliffc.aa.HM.HM9.BUF
static byte[] BUF
Definition: HM9.java:143
com.cliffc.aa.HM.HM9.DO_HM
static final boolean DO_HM
Definition: HM9.java:78
com.cliffc.aa.util.SB.toString
String toString()
Definition: SB.java:62
com.cliffc.aa.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.type.BitsAlias.reset_to_init0
static void reset_to_init0()
Definition: BitsAlias.java:64
com.cliffc.aa.type.TypeMemPtr.make
static TypeMemPtr make(BitsAlias aliases, TypeObj obj)
Definition: TypeMemPtr.java:66