1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.javascript.jscomp.ControlFlowGraph.Branch;
import com.google.javascript.jscomp.DataFlowAnalysis.BranchedFlowState;
import com.google.javascript.jscomp.DataFlowAnalysis.BranchedForwardDataFlowAnalysis;
import com.google.javascript.jscomp.DataFlowAnalysis.FlowState;
import com.google.javascript.jscomp.DataFlowAnalysis.MaxIterationsExceededException;
import com.google.javascript.jscomp.JoinOp.BinaryJoinOp;
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
import com.google.javascript.jscomp.graph.GraphNode;
import com.google.javascript.jscomp.graph.LatticeElement;
import junit.framework.TestCase;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* A test suite with a very small programming language that has two types of
* instructions: {@link BranchInstruction} and {@link ArithmeticInstruction}.
* Test cases must construct a small program with these instructions and
* manually put each instruction in a {@code ControlFlowGraph}.
*
*/
public class DataFlowAnalysisTest extends TestCase {
/**
* Operations supported by ArithmeticInstruction.
*/
enum Operation {
ADD("+"), SUB("-"), DIV("/"), MUL("*");
private final String stringRep;
private Operation(String stringRep) {
this.stringRep = stringRep;
}
@Override
public String toString() {
return stringRep;
}
}
/**
* A simple value.
*/
abstract static class Value {
boolean isNumber() {
return this instanceof Number;
}
boolean isVariable() {
return this instanceof Variable;
}
}
/**
* A variable.
*/
static class Variable extends Value {
private String name;
/**
* Constructor.
*
* @param n Name of the variable.
*/
Variable(String n) {
name = n;
}
String getName() {
return name;
}
@Override
public boolean equals(Object other) {
// Use the String's .equals()
if (!(other instanceof Variable)) {
return false;
}
return ((Variable) other).name.equals(name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return this.name;
}
}
/**
* A number constant.
*/
static class Number extends Value {
private int value;
/**
* Constructor
*
* @param v Value
*/
Number(int v) {
value = v;
}
int getValue() {
return value;
}
@Override
public String toString() {
return "" + value;
}
@Override
public int hashCode() {
return value;
}
}
/**
* An instruction of the dummy program.
*/
abstract static class Instruction {
int order = 0;
/**
* Check whether this is an arithmetic instruction.
*
* @return {@code true} if it is an arithmetic instruction.
*/
boolean isArithmetic() {
return this instanceof ArithmeticInstruction;
}
/**
* Check whether this is a branch instruction.
*
* @return {@code true} if it is a branch instruction.
*/
boolean isBranch() {
return this instanceof BranchInstruction;
}
}
/**
* Basic arithmetic instruction that only takes the form of:
*
* <pre>
* Result = Operand1 operator Operand2
* </pre>
*/
static class ArithmeticInstruction extends Instruction {
private Operation operation;
private Value operand1;
private Value operand2;
private Variable result;
/**
* Constructor
*
* @param res Result.
* @param op1 First Operand.
* @param o Operator.
* @param op2 Second Operand.
*/
ArithmeticInstruction(Variable res, int op1, Operation o, int op2) {
this(res, new Number(op1), o, new Number(op2));
}
/**
* Constructor
*
* @param res Result.
* @param op1 First Operand.
* @param o Operator.
* @param op2 Second Operand.
*/
ArithmeticInstruction(Variable res, Value op1, Operation o, int op2) {
this(res, op1, o, new Number(op2));
}
/**
* Constructor
*
* @param res Result.
* @param op1 First Operand.
* @param o Operator.
* @param op2 Second Operand.
*/
ArithmeticInstruction(Variable res, int op1, Operation o, Value op2) {
this(res, new Number(op1), o, op2);
}
/**
* Constructor
*
* @param res Result.
* @param op1 First Operand.
* @param o Operator.
* @param op2 Second Operand.
*/
ArithmeticInstruction(Variable res, Value op1, Operation o, Value op2) {
result = res;
operand1 = op1;
operand2 = op2;
operation = o;
}
Operation getOperator() {
return operation;
}
void setOperator(Operation op) {
this.operation = op;
}
Value getOperand1() {
return operand1;
}
void setOperand1(Value operand1) {
this.operand1 = operand1;
}
Value getOperand2() {
return operand2;
}
void setOperand2(Value operand2) {
this.operand2 = operand2;
}
Variable getResult() {
return result;
}
void setResult(Variable result) {
this.result = result;
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
out.append(result);
out.append(" = ");
out.append(operand1);
out.append(operation);
out.append(operand2);
return out.toString();
}
@Override
public int hashCode() {
return toString().hashCode();
}
}
public static ArithmeticInstruction
newAssignNumberToVariableInstruction(Variable res, int num) {
return new ArithmeticInstruction(res, num, Operation.ADD, 0);
}
public static ArithmeticInstruction
newAssignVariableToVariableInstruction(Variable lhs, Variable rhs) {
return new ArithmeticInstruction(lhs, rhs, Operation.ADD, 0);
}
/**
* Branch instruction based on a {@link Value} as a condition.
*/
static class BranchInstruction extends Instruction {
private Value condition;
BranchInstruction(Value cond) {
condition = cond;
}
Value getCondition() {
return condition;
}
void setCondition(Value condition) {
this.condition = condition;
}
}
/**
* A lattice to represent constant states. Each variable of the program will
* have a lattice defined as:
*
* <pre>
* TOP
* / / | \
* 0 1 2 3 ..... MAX_VALUE
* \ \ | /
* BOTTOM
* </pre>
*
* Where BOTTOM represents the variable is not a constant.
* <p>
* This class will represent a product lattice of each variable's lattice. The
* whole lattice is store in a {@code HashMap}. If variable {@code x} is
* defined to be constant 10. The map will contain the value 10 with the
* variable {@code x} as key. Otherwise, {@code x} is not a constant.
*/
private static class ConstPropLatticeElement implements LatticeElement {
private final Map<Variable, Integer> constMap;
private final boolean isTop;
/**
* Constructor.
*
* @param isTop To define if the lattice is top.
*/
ConstPropLatticeElement(boolean isTop) {
this.isTop = isTop;
this.constMap = Maps.newHashMap();
}
/**
* Create a lattice where every variable is defined to be not constant.
*/
ConstPropLatticeElement() {
this(false);
}
ConstPropLatticeElement(ConstPropLatticeElement other) {
this.isTop = other.isTop;
this.constMap = Maps.newHashMap(other.constMap);
}
@Override
public String toString() {
if (isTop) {
return "TOP";
}
StringBuilder out = new StringBuilder();
out.append("{");
for (Variable var : constMap.keySet()) {
out.append(var);
out.append("=");
out.append(constMap.get(var));
out.append(" ");
}
out.append("}");
return out.toString();
}
@Override
public boolean equals(Object other) {
if (other instanceof ConstPropLatticeElement) {
ConstPropLatticeElement otherLattice = (ConstPropLatticeElement) other;
return (this.isTop == otherLattice.isTop) &&
this.constMap.equals(otherLattice.constMap);
}
return false;
}
}
private static class ConstPropJoinOp
extends BinaryJoinOp<ConstPropLatticeElement> {
@Override
public ConstPropLatticeElement apply(ConstPropLatticeElement a,
ConstPropLatticeElement b) {
ConstPropLatticeElement result = new ConstPropLatticeElement();
// By the definition of TOP of the lattice.
if (a.isTop) {
return new ConstPropLatticeElement(a);
}
if (b.isTop) {
return new ConstPropLatticeElement(b);
}
// Do the join for each variable's lattice.
for (Variable var : a.constMap.keySet()) {
if (b.constMap.containsKey(var)) {
Integer number = b.constMap.get(var);
// The result will contain that variable as a known constant
// if both lattice has that variable the same constant.
if (a.constMap.get(var).equals(number)) {
result.constMap.put(var, number);
}
}
}
return result;
}
}
/**
* A simple forward constant propagation.
*/
static class DummyConstPropagation extends
DataFlowAnalysis<Instruction, ConstPropLatticeElement> {
/**
* Constructor.
*
* @param targetCfg Control Flow Graph.
*/
DummyConstPropagation(ControlFlowGraph<Instruction> targetCfg) {
super(targetCfg, new ConstPropJoinOp());
}
@Override
boolean isForward() {
return true;
}
@Override
ConstPropLatticeElement flowThrough(Instruction node,
ConstPropLatticeElement input) {
if (node.isBranch()) {
return new ConstPropLatticeElement(input);
} else {
return flowThroughArithmeticInstruction((ArithmeticInstruction) node,
input);
}
}
@Override
ConstPropLatticeElement createEntryLattice() {
return new ConstPropLatticeElement();
}
@Override
ConstPropLatticeElement createInitialEstimateLattice() {
return new ConstPropLatticeElement(true);
}
}
static ConstPropLatticeElement flowThroughArithmeticInstruction(
ArithmeticInstruction aInst, ConstPropLatticeElement input) {
ConstPropLatticeElement out = new ConstPropLatticeElement(input);
// Try to see if left is a number. If it is a variable, it might already
// be a constant coming in.
Integer leftConst = null;
if (aInst.operand1.isNumber()) {
leftConst = ((Number) aInst.operand1).value;
} else {
if (input.constMap.containsKey(aInst.operand1)) {
leftConst = input.constMap.get(aInst.operand1);
}
}
// Do the same thing to the right.
Integer rightConst = null;
if (aInst.operand2.isNumber()) {
rightConst = ((Number) aInst.operand2).value;
} else {
if (input.constMap.containsKey(aInst.operand2)) {
rightConst = input.constMap.get(aInst.operand2);
}
}
// If both are known constant we can perform the operation.
if (leftConst != null && rightConst != null) {
Integer constResult = null;
if (aInst.operation == Operation.ADD) {
constResult = leftConst.intValue() + rightConst.intValue();
} else if (aInst.operation == Operation.SUB) {
constResult = leftConst.intValue() - rightConst.intValue();
} else if (aInst.operation == Operation.MUL) {
constResult = leftConst.intValue() * rightConst.intValue();
} else if (aInst.operation == Operation.DIV) {
constResult = leftConst.intValue() / rightConst.intValue();
}
// Put it in the map. (Possibly replacing the existing constant value)
out.constMap.put(aInst.result, constResult);
} else {
// If we cannot find a constant for it
out.constMap.remove(aInst.result);
}
return out;
}
public void testSimpleIf() {
// if (a) { b = 1; } else { b = 1; } c = b;
Variable a = new Variable("a");
Variable b = new Variable("b");
Variable c = new Variable("c");
Instruction inst1 = new BranchInstruction(a);
Instruction inst2 = newAssignNumberToVariableInstruction(b, 1);
Instruction inst3 = newAssignNumberToVariableInstruction(b, 1);
Instruction inst4 = newAssignVariableToVariableInstruction(c, b);
ControlFlowGraph<Instruction> cfg =
new ControlFlowGraph<Instruction>(inst1, true, true);
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1);
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2);
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3);
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4);
cfg.connect(inst1, ControlFlowGraph.Branch.ON_FALSE, inst2);
cfg.connect(inst1, ControlFlowGraph.Branch.ON_TRUE, inst3);
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst4);
cfg.connect(inst3, ControlFlowGraph.Branch.UNCOND, inst4);
DummyConstPropagation constProp = new DummyConstPropagation(cfg);
constProp.analyze();
// We cannot conclude anything from if (a).
verifyInHas(n1, a, null);
verifyInHas(n1, b, null);
verifyInHas(n1, c, null);
verifyOutHas(n1, a, null);
verifyOutHas(n1, b, null);
verifyOutHas(n1, c, null);
// We can conclude b = 1 after the instruction.
verifyInHas(n2, a, null);
verifyInHas(n2, b, null);
verifyInHas(n2, c, null);
verifyOutHas(n2, a, null);
verifyOutHas(n2, b, 1);
verifyOutHas(n2, c, null);
// Same as above.
verifyInHas(n3, a, null);
verifyInHas(n3, b, null);
verifyInHas(n3, c, null);
verifyOutHas(n3, a, null);
verifyOutHas(n3, b, 1);
verifyOutHas(n3, c, null);
// After the merge we should still have b = 1.
verifyInHas(n4, a, null);
verifyInHas(n4, b, 1);
verifyInHas(n4, c, null);
verifyOutHas(n4, a, null);
// After the instruction both b and c are 1.
verifyOutHas(n4, b, 1);
verifyOutHas(n4, c, 1);
}
public void testSimpleLoop() {
// a = 0; do { a = a + 1 } while (b); c = a;
Variable a = new Variable("a");
Variable b = new Variable("b");
Variable c = new Variable("c");
Instruction inst1 = newAssignNumberToVariableInstruction(a, 0);
Instruction inst2 = new ArithmeticInstruction(a, a, Operation.ADD, 1);
Instruction inst3 = new BranchInstruction(b);
Instruction inst4 = newAssignVariableToVariableInstruction(c, a);
ControlFlowGraph<Instruction> cfg =
new ControlFlowGraph<Instruction>(inst1, true, true);
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1);
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2);
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3);
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4);
cfg.connect(inst1, ControlFlowGraph.Branch.UNCOND, inst2);
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst3);
cfg.connect(inst3, ControlFlowGraph.Branch.ON_TRUE, inst2);
cfg.connect(inst3, ControlFlowGraph.Branch.ON_FALSE, inst4);
DummyConstPropagation constProp = new DummyConstPropagation(cfg);
// This will also show that the framework terminates properly.
constProp.analyze();
// a = 0 is the only thing we know.
verifyInHas(n1, a, null);
verifyInHas(n1, b, null);
verifyInHas(n1, c, null);
verifyOutHas(n1, a, 0);
verifyOutHas(n1, b, null);
verifyOutHas(n1, c, null);
// Nothing is provable in this program, so confirm that we haven't
// erroneously "proven" something.
verifyInHas(n2, a, null);
verifyInHas(n2, b, null);
verifyInHas(n2, c, null);
verifyOutHas(n2, a, null);
verifyOutHas(n2, b, null);
verifyOutHas(n2, c, null);
verifyInHas(n3, a, null);
verifyInHas(n3, b, null);
verifyInHas(n3, c, null);
verifyOutHas(n3, a, null);
verifyOutHas(n3, b, null);
verifyOutHas(n3, c, null);
verifyInHas(n4, a, null);
verifyInHas(n4, b, null);
verifyInHas(n4, c, null);
verifyOutHas(n4, a, null);
verifyOutHas(n4, b, null);
verifyOutHas(n4, c, null);
}
public void testLatticeArrayMinimizationWhenMidpointIsEven() {
assertEquals(6, JoinOp.BinaryJoinOp.computeMidPoint(12));
}
public void testLatticeArrayMinimizationWhenMidpointRoundsDown() {
assertEquals(8, JoinOp.BinaryJoinOp.computeMidPoint(18));
}
public void testLatticeArrayMinimizationWithTwoElements() {
assertEquals(1, JoinOp.BinaryJoinOp.computeMidPoint(2));
}
/**
* A simple forward constant propagation.
*/
static class BranchedDummyConstPropagation extends
BranchedForwardDataFlowAnalysis<Instruction, ConstPropLatticeElement> {
BranchedDummyConstPropagation(ControlFlowGraph<Instruction> targetCfg) {
super(targetCfg, new ConstPropJoinOp());
}
@Override
ConstPropLatticeElement flowThrough(Instruction node,
ConstPropLatticeElement input) {
if (node.isArithmetic()) {
return flowThroughArithmeticInstruction(
(ArithmeticInstruction) node, input);
} else {
return new ConstPropLatticeElement(input);
}
}
@Override
List<ConstPropLatticeElement> branchedFlowThrough(Instruction node,
ConstPropLatticeElement input) {
List<ConstPropLatticeElement> result = Lists.newArrayList();
List<DiGraphEdge<Instruction, Branch>> outEdges =
getCfg().getOutEdges(node);
if (node.isArithmetic()) {
assertTrue(outEdges.size() < 2);
ConstPropLatticeElement aResult = flowThroughArithmeticInstruction(
(ArithmeticInstruction) node, input);
for (int i = 0; i < outEdges.size(); i++) {
result.add(aResult);
}
} else {
BranchInstruction branchInst = (BranchInstruction) node;
for (DiGraphEdge<Instruction, Branch> branch : outEdges) {
ConstPropLatticeElement edgeResult =
new ConstPropLatticeElement(input);
if (branch.getValue() == Branch.ON_FALSE &&
branchInst.getCondition().isVariable()) {
edgeResult.constMap.put((Variable) branchInst.getCondition(), 0);
}
result.add(edgeResult);
}
}
return result;
}
@Override
ConstPropLatticeElement createEntryLattice() {
return new ConstPropLatticeElement();
}
@Override
ConstPropLatticeElement createInitialEstimateLattice() {
return new ConstPropLatticeElement(true);
}
}
public void testBranchedSimpleIf() {
// if (a) { a = 0; } else { b = 0; } c = b;
Variable a = new Variable("a");
Variable b = new Variable("b");
Variable c = new Variable("c");
Instruction inst1 = new BranchInstruction(a);
Instruction inst2 = newAssignNumberToVariableInstruction(a, 0);
Instruction inst3 = newAssignNumberToVariableInstruction(b, 0);
Instruction inst4 = newAssignVariableToVariableInstruction(c, b);
ControlFlowGraph<Instruction> cfg =
new ControlFlowGraph<Instruction>(inst1, true, true);
GraphNode<Instruction, Branch> n1 = cfg.createNode(inst1);
GraphNode<Instruction, Branch> n2 = cfg.createNode(inst2);
GraphNode<Instruction, Branch> n3 = cfg.createNode(inst3);
GraphNode<Instruction, Branch> n4 = cfg.createNode(inst4);
cfg.connect(inst1, ControlFlowGraph.Branch.ON_TRUE, inst2);
cfg.connect(inst1, ControlFlowGraph.Branch.ON_FALSE, inst3);
cfg.connect(inst2, ControlFlowGraph.Branch.UNCOND, inst4);
cfg.connect(inst3, ControlFlowGraph.Branch.UNCOND, inst4);
BranchedDummyConstPropagation constProp =
new BranchedDummyConstPropagation(cfg);
constProp.analyze();
// We cannot conclude anything from if (a).
verifyBranchedInHas(n1, a, null);
verifyBranchedInHas(n1, b, null);
verifyBranchedInHas(n1, c, null);
// Nothing is known on the true branch.
verifyBranchedInHas(n2, a, null);
verifyBranchedInHas(n2, b, null);
verifyBranchedInHas(n2, c, null);
// Verify that we have a = 0 on the false branch.
verifyBranchedInHas(n3, a, 0);
verifyBranchedInHas(n3, b, null);
verifyBranchedInHas(n3, c, null);
// After the merge we should still have a = 0.
verifyBranchedInHas(n4, a, 0);
}
public void testMaxIterationsExceededException() {
final int MAX_STEP = 10;
Variable a = new Variable("a");
Instruction inst1 = new ArithmeticInstruction(a, a, Operation.ADD, a);
ControlFlowGraph<Instruction> cfg =
new ControlFlowGraph<Instruction>(inst1, true, true) {
@Override
public Comparator<DiGraphNode<Instruction, Branch>>
getOptionalNodeComparator(boolean isForward) {
return new Comparator<DiGraphNode<Instruction, Branch>>() {
@Override
public int compare(DiGraphNode<Instruction, Branch> o1,
DiGraphNode<Instruction, Branch> o2) {
return o1.getValue().order - o2.getValue().order;
}
};
}
};
cfg.createNode(inst1);
// We have MAX_STEP + 1 nodes, it is impossible to finish the analysis with
// MAX_STEP number of steps.
for (int i = 0; i < MAX_STEP + 1; i++) {
Instruction inst2 = new ArithmeticInstruction(a, a, Operation.ADD, a);
cfg.createNode(inst2);
inst2.order = i + 1;
cfg.connect(inst1, ControlFlowGraph.Branch.UNCOND, inst2);
inst1 = inst2;
}
DummyConstPropagation constProp = new DummyConstPropagation(cfg);
try {
constProp.analyze(MAX_STEP);
fail("Expected MaxIterationsExceededException to be thrown.");
} catch (MaxIterationsExceededException e) {
assertEquals(e.getMessage(), "Analysis did not terminate after "
+ MAX_STEP + " iterations");
}
}
static void verifyInHas(GraphNode<Instruction, Branch> node, Variable var,
Integer constant) {
FlowState<ConstPropLatticeElement> fState = node.getAnnotation();
assertEquals(constant, fState.getIn().constMap.get(var));
}
static void verifyOutHas(GraphNode<Instruction, Branch> node, Variable var,
Integer constant) {
FlowState<ConstPropLatticeElement> fState = node.getAnnotation();
assertEquals(constant, fState.getOut().constMap.get(var));
}
static void verifyBranchedInHas(GraphNode<Instruction, Branch> node,
Variable var, Integer constant) {
BranchedFlowState<ConstPropLatticeElement> fState = node.getAnnotation();
assertEquals(constant, fState.getIn().constMap.get(var));
}
}
|