import java.util.Iterator;
import java.util.Set;
+import Analysis.SSJava.SSJavaAnalysis;
import IR.Operation;
+import IR.State;
import IR.Flat.FKind;
import IR.Flat.FlatCondBranch;
import IR.Flat.FlatMethod;
Set<FlatNode> computed;
+ State state;
+ SSJavaAnalysis ssjava;
+
/**
* Constructor for Loop Termination Analysis
*/
- public LoopTerminate() {
+ public LoopTerminate(SSJavaAnalysis ssjava, State state) {
+ this.ssjava = ssjava;
+ this.state = state;
this.inductionSet = new HashSet<TempDescriptor>();
this.inductionVar2DefNode = new HashMap<TempDescriptor, FlatNode>();
this.derivedVar2basicInduction = new HashMap<TempDescriptor, TempDescriptor>();
assert loopEntrances.size() == 1;
FlatNode loopEntrance = (FlatNode) loopEntrances.iterator().next();
- init();
- detectBasicInductionVar(loopElements);
- detectDerivedInductionVar(loopElements);
- checkConditionBranch(loopEntrance, loopElements);
+ String loopLabel = (String) state.fn2labelMap.get(loopEntrance);
+
+ if (loopLabel == null || !loopLabel.startsWith(ssjava.TERMINATE)) {
+ init();
+ detectBasicInductionVar(loopElements);
+ detectDerivedInductionVar(loopElements);
+ checkConditionBranch(loopEntrance, loopElements);
+ }
}
private void checkBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn, boolean flag) {
bn.getVarTable().setParent(nametable);
- String label = bn.getLabel();
- boolean isSSJavaLoop = flag;
- if (label != null && label.equals(ssjava.SSJAVA)) {
- if (isSSJavaLoop) {
- throw new Error("Only outermost loop can be the self-stabilizing loop.");
- } else {
- annotatedMDSet.add(md);
- isSSJavaLoop = true;
- }
- }
for (int i = 0; i < bn.size(); i++) {
BlockStatementNode bsn = bn.get(i);
- checkBlockStatementNode(md, bn.getVarTable(), bsn, isSSJavaLoop);
+ checkBlockStatementNode(md, bn.getVarTable(), bsn, flag);
}
}
}
private void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln, boolean flag) {
+
+ String label = ln.getLabel();
+ boolean isSSJavaLoop = flag;
+ if (label != null && label.equals(ssjava.SSJAVA)) {
+ if (isSSJavaLoop) {
+ throw new Error("Only outermost loop can be the self-stabilizing loop.");
+ } else {
+ annotatedMDSet.add(md);
+ isSSJavaLoop = true;
+ }
+ }
+
if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
- checkExpressionNode(md, nametable, ln.getCondition(), flag);
- checkBlockNode(md, nametable, ln.getBody(), flag);
+ checkExpressionNode(md, nametable, ln.getCondition(), isSSJavaLoop);
+ checkBlockNode(md, nametable, ln.getBody(), isSSJavaLoop);
} else {
// For loop case
/* Link in the initializer naming environment */
bn.getVarTable().setParent(nametable);
for (int i = 0; i < bn.size(); i++) {
BlockStatementNode bsn = bn.get(i);
- checkBlockStatementNode(md, bn.getVarTable(), bsn, flag);
+ checkBlockStatementNode(md, bn.getVarTable(), bsn, isSSJavaLoop);
}
// check the condition
- checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), flag);
- checkBlockNode(md, bn.getVarTable(), ln.getBody(), flag);
- checkBlockNode(md, bn.getVarTable(), ln.getUpdate(), flag);
+ checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), isSSJavaLoop);
+ checkBlockNode(md, bn.getVarTable(), ln.getBody(), isSSJavaLoop);
+ checkBlockNode(md, bn.getVarTable(), ln.getUpdate(), isSSJavaLoop);
}
}
}
public void doCheck() {
- doMethodAnnotationCheck();
- computeLinearTypeCheckMethodSet();
- doLinearTypeCheck();
- if (state.SSJAVADEBUG) {
- debugPrint();
- }
- parseLocationAnnotation();
- doFlowDownCheck();
- doDefinitelyWrittenCheck();
+ doMethodAnnotationCheck();
+ // computeLinearTypeCheckMethodSet();
+ // doLinearTypeCheck();
+ // if (state.SSJAVADEBUG) {
+ // debugPrint();
+ // }
+ // parseLocationAnnotation();
+ // doFlowDownCheck();
+ // doDefinitelyWrittenCheck();
}
public void addTrustMethod(MethodDescriptor md) {
}
public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
- LoopTerminate lt = new LoopTerminate();
+ LoopTerminate lt = new LoopTerminate(this,state);
if (needTobeAnnotated(fm.getMethod())) {
lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
}
}
- public void doLoopTerminationCheck(LoopOptimize lo) {
- LoopTerminate lt = new LoopTerminate();
- for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
- MethodDescriptor md = (MethodDescriptor) iterator.next();
- if (!skipLoopTerminate.containsKey(md)) {
- FlatMethod fm = state.getMethodFlat(md);
- lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
- }
- }
-
- }
-
public CallGraph getCallGraph() {
return callgraph;
}
NodePair np=flattenBlockStatementNode(bn.get(i));
FlatNode np_begin=np.getBegin();
FlatNode np_end=np.getEnd();
- if(bn.getLabel()!=null) {
- // interim implementation to have the labeled statement
- state.fn2labelMap.put(np_begin, bn.getLabel());
- }
if (begin==null) {
begin=np_begin;
}
}
private NodePair flattenLoopNode(LoopNode ln) {
+
HashSet oldbs=breakset;
HashSet oldcs=continueset;
breakset=new HashSet();
}
breakset=oldbs;
continueset=oldcs;
+ if(ln.getLabel()!=null){
+ state.fn2labelMap.put(condition.getBegin(), ln.getLabel());
+ }
return new NodePair(begin,nopend);
} else if (ln.getType()==LoopNode.WHILELOOP) {
TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
}
breakset=oldbs;
continueset=oldcs;
+ if(ln.getLabel()!=null){
+ state.fn2labelMap.put(begin, ln.getLabel());
+ }
return new NodePair(begin,nopend);
} else if (ln.getType()==LoopNode.DOWHILELOOP) {
TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
}
breakset=oldbs;
continueset=oldcs;
+ if(ln.getLabel()!=null){
+ state.fn2labelMap.put(condition.getBegin(), ln.getLabel());
+ }
return new NodePair(begin,nopend);
} else throw new Error();
}
public final static int NOBRACES=1;
public final static int EXPRLIST=2;
- String label=null;
-
public BlockNode() {
blockstatements=new Vector();
table=new SymbolTable();
return Kind.BlockNode;
}
- public void setLabel(String l) {
- label=l;
- }
-
- public String getLabel() {
- return label;
- }
-
}
return bn;
}
- public BlockNode parseSingleBlock(ParseNode pn) {
+ public BlockNode parseSingleBlock(ParseNode pn, String label){
BlockNode bn=new BlockNode();
- Vector bsv=parseBlockStatement(pn);
+ Vector bsv=parseBlockStatement(pn,label);
for(int j=0; j<bsv.size(); j++) {
bn.addBlockStatement((BlockStatementNode)bsv.get(j));
}
bn.setStyle(BlockNode.NOBRACES);
return bn;
}
+
+ public BlockNode parseSingleBlock(ParseNode pn) {
+ return parseSingleBlock(pn,null);
+ }
public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
ParseNodeVector pnv=pn.getChildren();
}
return bv;
}
+
+ public Vector parseBlockStatement(ParseNode pn){
+ return parseBlockStatement(pn,null);
+ }
- public Vector parseBlockStatement(ParseNode pn) {
+ public Vector parseBlockStatement(ParseNode pn, String label) {
Vector blockstatements=new Vector();
if (isNode(pn,"tag_declaration")) {
String name=pn.getChild("single").getTerminal();
// no condition clause, make a 'true' expression as the condition
condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
}
- LoopNode ln=new LoopNode(init,condition,update,body);
+ LoopNode ln=new LoopNode(init,condition,update,body,label);
ln.setNumLine(pn.getLine());
blockstatements.add(ln);
} else if (isNode(pn,"whilestatement")) {
// no condition clause, make a 'true' expression as the condition
condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
}
- blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
+ blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP,label));
} else if (isNode(pn,"dowhilestatement")) {
ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
// no condition clause, make a 'true' expression as the condition
condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
}
- blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
+ blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP,label));
} else if (isNode(pn,"sese")) {
ParseNode pnID=pn.getChild("identifier");
String stID=null;
blockstatements.add(new GenReachNode(graphName) );
} else if(isNode(pn,"labeledstatement")) {
- String label = pn.getChild("name").getTerminal();
- BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
- bn.setLabel(label);
+ String labeledstatement = pn.getChild("name").getTerminal();
+ BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild(),labeledstatement);
blockstatements.add(new SubBlockNode(bn));
} else {
System.out.println("---------------");
public static int FORLOOP=1;
public static int WHILELOOP=2;
public static int DOWHILELOOP=3;
+ String label=null;
- public LoopNode(BlockNode initializer,ExpressionNode condition, BlockNode update, BlockNode body) {
+ public LoopNode(BlockNode initializer,ExpressionNode condition, BlockNode update, BlockNode body, String label) {
this.initializer=initializer;
this.condition=condition;
this.update=update;
initializer.setStyle(BlockNode.EXPRLIST);
update.setStyle(BlockNode.EXPRLIST);
type=FORLOOP;
+ this.label=label;
}
- public LoopNode(ExpressionNode condition, BlockNode body, int type) {
+ public LoopNode(ExpressionNode condition, BlockNode body, int type, String label) {
this.condition=condition;
this.body=body;
this.type=type;
+ this.label=label;
}
public BlockNode getInitializer() {
public int kind() {
return Kind.LoopNode;
}
+
+ public void setLabel(String l) {
+ label=l;
+ }
+
+ public String getLabel() {
+ return label;
+ }
}