checkpoint
[sbp.git] / src / edu / berkeley / sbp / GSS.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import java.io.*;
7 import java.util.*;
8 import java.lang.reflect.*;
9
10 //////////////////////////////////////////////////////////////////////////////
11 // TODO:
12 //
13 //  - fix public/package/private status
14 //
15
16 //////////////////////////////////////////////////////////////////////////////
17 // Optimizations to add
18 //
19 // ** NOTE: not all of these are appropriate for this class -- it is
20 //          simply a list of optimizations not implemented.  This
21 //          class is meant to remain simple and easy to understand;
22 //          optimizations which obscure that do not belong here (they
23 //          should go into the compiled version instead)
24
25 /** implements Tomita's Graph Structured Stack */
26 class GSS {
27
28     public GSS() { }
29
30     private Phase.Node[] reducing_list = null;
31
32     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
33     public class Phase {
34
35         /** the token immediately after this phase */
36         public  final Token token;
37
38         boolean reducing = false;
39
40         /** currently this is necessary only for the code() hack -- it doesn't actually correspond to the input */
41         private final int pos;
42
43         /** FIXME */
44         public  Forest.Ref finalResult = null;
45
46         /** all nodes, keyed by the value returned by code() */
47         private HashMap<Long,Phase.Node> hash    = new HashMap<Long,Phase.Node>();  /* ALLOC */
48
49         /** the number of nodes in this phase */
50         private int numNodes = 0;
51
52         boolean closed = false;
53
54         private Token.Location location;
55         public Phase(Phase previous, Token token, Token.Location location) {
56             this.pos = previous==null ? 0 : previous.pos+1;
57             this.token = token;
58             this.location = location;
59         }
60
61         public boolean isDone() { return token == null; }
62
63         private String error = "generic syntax error";
64         public void checkFailure() throws Parser.Failed {
65             if (numNodes <= 0)
66                 throw new Parser.Failed(error, getLocation());
67         }
68
69         public Token.Location getLocation() { return location; }
70
71         /** add a new node (merging with existing nodes if possible)
72          *  @param parent             the parent of the new node
73          *  @param result             the SPPF result corresponding to the new node
74          *  @param state              the state that the new node is in
75          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
76          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
77          */
78         public void newNode(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
79             Node p = hash.get(code(state, start));
80             if (p != null)  newNode2(p, parent, pending, state, fromEmptyReduction, start);
81             else            newNode3(parent, pending, state, fromEmptyReduction, start);
82         }
83         private void newNode2(Node p, Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
84             p.holder.merge(pending);
85             if (p.parents().contains(parent)) return;
86             p.addParent(parent, fromEmptyReduction);
87         }
88         private void newNode3(Node parent, Forest pending, Parser.Table.State state, boolean fromEmptyReduction, Phase start) {
89             do {
90                 if (token != null && state.canShift(token)) break;
91                 if (state.isAccepting()) break;
92                 if (token==null) break;
93                 int count = 0;
94                 Parser.Table.Reduction r = null;
95                 for(Parser.Table.Reduction red : token==null ? state.getEofReductions() : state.getReductions(token)) { r = red; count++; }
96                 if (count==0) return;     // BEWARE! this optimization is suspected to cause really nasty heisenbugs
97                 //if (count > 1) break;
98                 //if (r.numPop == 0) break;
99                 //r.reduce(pending, parent, null, Phase.this, null);
100                 //return;
101             } while(false);
102
103             Node n = new Node(parent, pending, state, start);  // ALLOC
104             n.queueEmptyReductions();
105             if (!fromEmptyReduction) n.queueReductions(parent);
106         }
107
108         
109         /** perform all reduction operations */
110         public void reduce() {
111             reducing = true;
112             if (reducing_list==null || reducing_list.length < hash.size())
113                 reducing_list = new Phase.Node[hash.size() * 4];
114             Collection<Node> hv = hash.values();
115             hv.toArray(reducing_list);
116             int num = hv.size();
117             for(int i=0; i<num; i++) {
118                 Node n = reducing_list[i];
119                 n.queueEmptyReductions();
120                 // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
121             }
122             for(int i=0; i<num; i++) {
123                 Node n = reducing_list[i];
124                 reducing_list[i] = null;
125                 n.queueEmptyReductions();
126                 n.queueReductions();
127             }
128         }
129
130         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
131         public void shift(Phase next, Forest result) {
132             closed = true;
133             Forest res = null;
134             boolean ok = false;
135             for(Phase.Node n : hash.values()) {
136                 if (n.holder==null) continue;
137                 n.holder.resolve();
138                 if (token == null && n.state.isAccepting()) {
139                     ok = true;
140                     if (finalResult==null) finalResult = new Forest.Ref();
141                     finalResult.merge(n.holder);
142                 }
143                 if (!n.holder.valid()) continue;
144                 if (token == null) continue;
145                 for(Parser.Table.State st : n.state.getShifts(token)) {
146                     if (res == null) res = result;
147                     next.newNode(n, res, st, true, this);
148                     ok = true;
149                 }
150             }
151
152             if (!ok && token != null) {
153                 StringBuffer error = new StringBuffer();
154                 error.append("error: unable to shift token \"" + token + "\"\n");
155                 //error.append("  before: " +pendingReductions+ "\n");
156                 //error.append("  before: " +totalReductions+ "\n");
157                 //for(Phase.Node n : hash.values()) {
158                 //n.queueReductions();
159                 //n.queueEmptyReductions();
160                 //}
161                 //error.append("  after: " +pendingReductions+ "\n");
162                 //error.append("  candidate states:\n");
163                 //for(Phase.Node n : hash.values()) {
164                     //for(Sequence.Position p : n.state) error.append("        " + p + "\n");
165                     //error.append("        --\n");
166                 //for(Parser.Table.Reduction r : n.state.getReductions(token)) error.append("        " + r + "\n");
167                     //error.append("        ==\n");
168                 //}
169                 next.error = error.toString();
170             }
171
172             // this massively improves GC performance
173             hash = null;
174         }
175
176        
177         // GSS Nodes //////////////////////////////////////////////////////////////////////////////
178
179         /** a node in the GSS */
180         public final class Node extends FastSet<Node> {
181
182             public void addParent(Node parent, boolean fromEmptyReduction) {
183                 parents().add(parent, true);
184                 if (this!=parent && !fromEmptyReduction) queueReductions(parent);
185             }
186
187             private Forest.Ref holder = null;
188             private boolean allqueued = false;
189
190             private HashMap<Parser.Table.Reduction,Forest> cache = null;
191
192             /** the set of nodes to which there is an edge starting at this node */
193             //public final FastSet<Node> parents = new FastSet<Node>();  /* ALLOC */
194
195             /** what state this node is in */
196             public final Parser.Table.State state;
197
198             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
199             public Phase phase() { return Phase.this; }
200
201             public  HashMap<Parser.Table.Reduction,Forest> cache() {
202                 return cache==null ? (cache = new HashMap<Parser.Table.Reduction,Forest>()) : cache;
203             }
204             public  Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
205             public  Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
206             public  FastSet<Node> parents() { return this; }
207
208             /** FIXME */
209             public void queueReductions() {
210                 if (allqueued) return;
211                 allqueued = true;
212                 int where = parents().size();
213                 for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token))
214                     if (r.numPop > 1)
215                         r.reduce(this, null, null);
216                 for(int i=0; i<where; i++)
217                     queueReductions(get(i), false);
218             }
219
220             /** FIXME */
221             public void queueReductions(Node n2) { queueReductions(n2, true); }
222             public void queueReductions(Node n2, boolean includeLongs) {
223                 if (!allqueued) { queueReductions(); return; }
224                 Node n = this;
225                 for(Parser.Table.Reduction r : token==null ? n.state.getEofReductions() : n.state.getReductions(token)) {
226                     
227                     // UGLY HACK
228                     // The problem here is that a "reduction of length 1"
229                     // performed twice with different values of n2 needs
230                     // to only create a *single* new result, but must add
231                     // multiple parents to the node holding that result.
232                     // The current reducer doesn't differentiate between
233                     // the next node of an n-pop reduction and the
234                     // ultimate parent of the last pop, so we need to
235                     // cache instances here as a way of avoiding
236                     // recreating them.
237                     if (r.numPop <= 0) continue;
238                     if (r.numPop == 1) {
239                         Forest ret = n.cache().get(r);
240                         if (ret != null) r.reduce(this, n2, ret);
241                         else n.cache().put(r, r.reduce(this, n2, null));
242                     } else {
243                         if (includeLongs) r.reduce(this, n2, null);
244                     }
245                 }
246             }
247
248
249             /** FIXME */
250             public void queueEmptyReductions() {
251                 if (reducing)
252                     for(Parser.Table.Reduction r : token==null ? state.getEofReductions() : state.getReductions(token))
253                         if (r.numPop==0)
254                             r.reduce(this, null, r.zero());
255             }
256
257             private Node(Node parent, Forest pending, Parser.Table.State state, Phase start) {
258                 this.state = state;
259                 if (pending != null) this.holder().merge(pending);
260                 if (parent != null) parents().add(parent, true);
261                 if (Phase.this.hash.get(code(state, start)) != null) throw new Error("severe problem!");
262                 Phase.this.hash.put(code(state, start), this);
263                 Phase.this.numNodes++;
264                 if (parent==null) holder().valid = true; // hack to make sure that the "base" node is always considered valid
265             }
266         }
267
268     }
269
270     /** helper method */
271     private static boolean equal(Object a, Object b) {
272         if (a==null && b==null) return true;
273         if (a==null || b==null) return false;
274         return a.equals(b);
275     }
276
277     /** this is something of a hack right now */
278     private static long code(Parser.Table.State state, Phase start) {
279         return (((long)state.idx) << 32) | (start==null ? 0 : start.pos);
280     }
281 }