| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Sets; |
| 21 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 22 |
import com.google.javascript.jscomp.Scope.Var; |
| 23 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 24 |
import com.google.javascript.jscomp.graph.LatticeElement; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
import com.google.javascript.rhino.Token; |
| 27 |
|
| 28 |
import java.util.BitSet; |
| 29 |
import java.util.List; |
| 30 |
import java.util.Set; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class LiveVariablesAnalysis extends |
| 49 |
DataFlowAnalysis<Node, LiveVariablesAnalysis.LiveVariableLattice> { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static final int MAX_VARIABLES_TO_ANALYZE = 100; |
| 55 |
|
| 56 |
public static final String ARGUMENT_ARRAY_ALIAS = "arguments"; |
| 57 |
|
| 58 |
private static class LiveVariableJoinOp |
| 59 |
implements JoinOp<LiveVariableLattice> { |
| 60 |
@Override |
| 61 |
public LiveVariableLattice apply(List<LiveVariableLattice> in) { |
| 62 |
LiveVariableLattice result = new LiveVariableLattice(in.get(0)); |
| 63 |
for (int i = 1; i < in.size(); i++) { |
| 64 |
result.liveSet.or(in.get(i).liveSet); |
| 65 |
} |
| 66 |
return result; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static class LiveVariableLattice implements LatticeElement { |
| 76 |
private final BitSet liveSet; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private LiveVariableLattice(int numVars) { |
| 82 |
this.liveSet = new BitSet(numVars); |
| 83 |
} |
| 84 |
|
| 85 |
private LiveVariableLattice(LiveVariableLattice other) { |
| 86 |
Preconditions.checkNotNull(other); |
| 87 |
this.liveSet = (BitSet) other.liveSet.clone(); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public boolean equals(Object other) { |
| 92 |
Preconditions.checkNotNull(other); |
| 93 |
return (other instanceof LiveVariableLattice) && |
| 94 |
this.liveSet.equals(((LiveVariableLattice) other).liveSet); |
| 95 |
} |
| 96 |
|
| 97 |
public boolean isLive(Var v) { |
| 98 |
Preconditions.checkNotNull(v); |
| 99 |
return liveSet.get(v.index); |
| 100 |
} |
| 101 |
|
| 102 |
public boolean isLive(int index) { |
| 103 |
return liveSet.get(index); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public String toString() { |
| 108 |
return liveSet.toString(); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public int hashCode() { |
| 113 |
return liveSet.hashCode(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
private final Scope jsScope; |
| 119 |
private final Set<Var> escaped; |
| 120 |
|
| 121 |
LiveVariablesAnalysis(ControlFlowGraph<Node> cfg, Scope jsScope, |
| 122 |
AbstractCompiler compiler) { |
| 123 |
super(cfg, new LiveVariableJoinOp()); |
| 124 |
this.jsScope = jsScope; |
| 125 |
this.escaped = Sets.newHashSet(); |
| 126 |
computeEscaped(jsScope, escaped, compiler); |
| 127 |
} |
| 128 |
|
| 129 |
public Set<Var> getEscapedLocals() { |
| 130 |
return escaped; |
| 131 |
} |
| 132 |
|
| 133 |
public int getVarIndex(String var) { |
| 134 |
return jsScope.getVar(var).index; |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
boolean isForward() { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
LiveVariableLattice createEntryLattice() { |
| 144 |
return new LiveVariableLattice(jsScope.getVarCount()); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
LiveVariableLattice createInitialEstimateLattice() { |
| 149 |
return new LiveVariableLattice(jsScope.getVarCount()); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
LiveVariableLattice flowThrough(Node node, LiveVariableLattice input) { |
| 154 |
final BitSet gen = new BitSet(input.liveSet.size()); |
| 155 |
final BitSet kill = new BitSet(input.liveSet.size()); |
| 156 |
|
| 157 |
|
| 158 |
boolean conditional = false; |
| 159 |
List<DiGraphEdge<Node, Branch>> edgeList = getCfg().getOutEdges(node); |
| 160 |
for (DiGraphEdge<Node, Branch> edge : edgeList) { |
| 161 |
if (Branch.ON_EX.equals(edge.getValue())) { |
| 162 |
conditional = true; |
| 163 |
} |
| 164 |
} |
| 165 |
computeGenKill(node, gen, kill, conditional); |
| 166 |
LiveVariableLattice result = new LiveVariableLattice(input); |
| 167 |
|
| 168 |
result.liveSet.andNot(kill); |
| 169 |
result.liveSet.or(gen); |
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private void computeGenKill(Node n, BitSet gen, BitSet kill, |
| 185 |
boolean conditional) { |
| 186 |
|
| 187 |
switch (n.getType()) { |
| 188 |
case Token.SCRIPT: |
| 189 |
case Token.BLOCK: |
| 190 |
case Token.FUNCTION: |
| 191 |
return; |
| 192 |
|
| 193 |
case Token.WHILE: |
| 194 |
case Token.DO: |
| 195 |
case Token.IF: |
| 196 |
computeGenKill(NodeUtil.getConditionExpression(n), gen, kill, |
| 197 |
conditional); |
| 198 |
return; |
| 199 |
|
| 200 |
case Token.FOR: |
| 201 |
if (!NodeUtil.isForIn(n)) { |
| 202 |
computeGenKill(NodeUtil.getConditionExpression(n), gen, kill, |
| 203 |
conditional); |
| 204 |
} else { |
| 205 |
|
| 206 |
Node lhs = n.getFirstChild(); |
| 207 |
if (lhs.isVar()) { |
| 208 |
|
| 209 |
lhs = lhs.getLastChild(); |
| 210 |
} |
| 211 |
|
| 212 |
if (lhs.isName()) { |
| 213 |
addToSetIfLocal(lhs, kill); |
| 214 |
addToSetIfLocal(lhs, gen); |
| 215 |
} else { |
| 216 |
computeGenKill(lhs, gen, kill, conditional); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
} |
| 221 |
return; |
| 222 |
|
| 223 |
case Token.VAR: |
| 224 |
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { |
| 225 |
if (c.hasChildren()) { |
| 226 |
computeGenKill(c.getFirstChild(), gen, kill, conditional); |
| 227 |
if (!conditional) { |
| 228 |
addToSetIfLocal(c, kill); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return; |
| 233 |
|
| 234 |
case Token.AND: |
| 235 |
case Token.OR: |
| 236 |
computeGenKill(n.getFirstChild(), gen, kill, conditional); |
| 237 |
|
| 238 |
computeGenKill(n.getLastChild(), gen, kill, true); |
| 239 |
return; |
| 240 |
|
| 241 |
case Token.HOOK: |
| 242 |
computeGenKill(n.getFirstChild(), gen, kill, conditional); |
| 243 |
|
| 244 |
computeGenKill(n.getFirstChild().getNext(), gen, kill, true); |
| 245 |
computeGenKill(n.getLastChild(), gen, kill, true); |
| 246 |
return; |
| 247 |
|
| 248 |
case Token.NAME: |
| 249 |
if (isArgumentsName(n)) { |
| 250 |
markAllParametersEscaped(); |
| 251 |
} else { |
| 252 |
addToSetIfLocal(n, gen); |
| 253 |
} |
| 254 |
return; |
| 255 |
|
| 256 |
default: |
| 257 |
if (NodeUtil.isAssignmentOp(n) && n.getFirstChild().isName()) { |
| 258 |
Node lhs = n.getFirstChild(); |
| 259 |
if (!conditional) { |
| 260 |
addToSetIfLocal(lhs, kill); |
| 261 |
} |
| 262 |
if (!n.isAssign()) { |
| 263 |
|
| 264 |
addToSetIfLocal(lhs, gen); |
| 265 |
} |
| 266 |
computeGenKill(lhs.getNext(), gen, kill, conditional); |
| 267 |
} else { |
| 268 |
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { |
| 269 |
computeGenKill(c, gen, kill, conditional); |
| 270 |
} |
| 271 |
} |
| 272 |
return; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
private void addToSetIfLocal(Node node, BitSet set) { |
| 277 |
Preconditions.checkState(node.isName()); |
| 278 |
String name = node.getString(); |
| 279 |
if (!jsScope.isDeclared(name, false)) { |
| 280 |
return; |
| 281 |
} |
| 282 |
Var var = jsScope.getVar(name); |
| 283 |
if (!escaped.contains(var)) { |
| 284 |
set.set(var.index); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
void markAllParametersEscaped() { |
| 293 |
Node lp = jsScope.getRootNode().getFirstChild().getNext(); |
| 294 |
for(Node arg = lp.getFirstChild(); arg != null; arg = arg.getNext()) { |
| 295 |
escaped.add(jsScope.getVar(arg.getString())); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
private boolean isArgumentsName(Node n) { |
| 300 |
if (!n.isName()|| |
| 301 |
!n.getString().equals(ARGUMENT_ARRAY_ALIAS) || |
| 302 |
jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false)) { |
| 303 |
return false; |
| 304 |
} else { |
| 305 |
return true; |
| 306 |
} |
| 307 |
} |
| 308 |
} |