From: bdemsky Date: Mon, 9 Feb 2009 23:14:25 +0000 (+0000) Subject: loop code X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=4709a7687d982a20f67a140a9dfd8f2e5431812a;p=IRC.git loop code --- diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index 05646863..7fee5bab 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -1331,6 +1331,8 @@ public class BuildCode { if (current_node==null) { current_node=(FlatNode)tovisit.iterator().next(); tovisit.remove(current_node); + } else if (tovisit.contains(current_node)){ + tovisit.remove(current_node); } visited.add(current_node); if (nodetolabel.containsKey(current_node)) diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index 9eec4e65..7ba03259 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -9,10 +9,15 @@ public class BuildFlat { MethodDescriptor currmd; TypeUtil typeutil; + HashSet breakset; + HashSet continueset; + public BuildFlat(State st, TypeUtil typeutil) { state=st; temptovar=new Hashtable(); this.typeutil=typeutil; + this.breakset=new HashSet(); + this.continueset=new HashSet(); } public Hashtable getMap() { @@ -168,7 +173,10 @@ public class BuildFlat { end=np_end; } else { end.addNext(np_begin); - end=np_end; + if (np_end==null) { + return new NodePair(begin, null); + } else + end=np_end; } } if (begin==null) { @@ -861,8 +869,10 @@ public class BuildFlat { cond.getEnd().addNext(fcb); fcb.addTrueNext(true_np.getBegin()); fcb.addFalseNext(false_np.getBegin()); - true_np.getEnd().addNext(nopend); - false_np.getEnd().addNext(nopend); + if (true_np.getEnd()!=null) + true_np.getEnd().addNext(nopend); + if (false_np.getEnd()!=null) + false_np.getEnd().addNext(nopend); return new NodePair(cond.getBegin(), nopend); } @@ -883,12 +893,24 @@ public class BuildFlat { FlatNop nop2=new FlatNop(); initializer.getEnd().addNext(nop2); nop2.addNext(condition.getBegin()); - body.getEnd().addNext(update.getBegin()); + if (body.getEnd()!=null) + body.getEnd().addNext(update.getBegin()); update.getEnd().addNext(backedge); backedge.addNext(condition.getBegin()); condition.getEnd().addNext(fcb); fcb.addFalseNext(nopend); fcb.addTrueNext(body.getBegin()); + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(update.getBegin()); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } + return new NodePair(begin,nopend); } else if (ln.getType()==LoopNode.WHILELOOP) { TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); @@ -907,6 +929,17 @@ public class BuildFlat { condition.getEnd().addNext(fcb); fcb.addFalseNext(nopend); fcb.addTrueNext(body.getBegin()); + + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(backedge); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } return new NodePair(begin,nopend); } else if (ln.getType()==LoopNode.DOWHILELOOP) { TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN)); @@ -924,6 +957,17 @@ public class BuildFlat { fcb.addFalseNext(nopend); fcb.addTrueNext(backedge); backedge.addNext(body.getBegin()); + + for(Iterator contit=continueset.iterator();contit.hasNext();) { + FlatNode fn=(FlatNode)contit.next(); + contit.remove(); + fn.addNext(condition.getBegin()); + } + for(Iterator breakit=breakset.iterator();breakit.hasNext();) { + FlatNode fn=(FlatNode)breakit.next(); + breakit.remove(); + fn.addNext(nopend); + } return new NodePair(begin,nopend); } else throw new Error(); } @@ -1023,6 +1067,15 @@ public class BuildFlat { return new NodePair(fsexn, fsexn); } + private NodePair flattenContinueBreakNode(ContinueBreakNode cbn) { + FlatNop fn=new FlatNop(); + if (cbn.isBreak()) + breakset.add(fn); + else + continueset.add(fn); + return new NodePair(fn,null); + } + private NodePair flattenBlockStatementNode(BlockStatementNode bsn) { switch(bsn.kind()) { case Kind.BlockExpressionNode: @@ -1054,6 +1107,9 @@ public class BuildFlat { case Kind.SESENode: return flattenSESENode((SESENode)bsn); + + case Kind.ContinueBreakNode: + return flattenContinueBreakNode((ContinueBreakNode)bsn); } throw new Error(); } diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 8e70258c..6b663166 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -710,6 +710,11 @@ public class BuildIR { blockstatements.add(start); blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild())); blockstatements.add(end); + } else if (isNode(pn,"continue")) { + blockstatements.add(new ContinueBreakNode(false)); + } else if (isNode(pn,"break")) { + blockstatements.add(new ContinueBreakNode(true)); + } else { System.out.println("---------------"); System.out.println(pn.PPrint(3,true)); diff --git a/Robust/src/IR/Tree/Kind.java b/Robust/src/IR/Tree/Kind.java index 82afcf5f..6d46fa85 100644 --- a/Robust/src/IR/Tree/Kind.java +++ b/Robust/src/IR/Tree/Kind.java @@ -24,4 +24,5 @@ public class Kind { public final static int AtomicNode=21; public final static int OffsetNode=22; public final static int SESENode=23; + public final static int ContinueBreakNode=24; } diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 7aa5e3e9..82c732ed 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -6,10 +6,13 @@ import IR.*; public class SemanticCheck { State state; TypeUtil typeutil; + Stack loopstack; + public SemanticCheck(State state, TypeUtil tu) { this.state=state; this.typeutil=tu; + this.loopstack=new Stack(); } public void semanticCheck() { @@ -267,6 +270,10 @@ public class SemanticCheck { checkAtomicNode(md, nametable, (AtomicNode)bsn); return; + case Kind.ContinueBreakNode: + checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn); + return; + case Kind.SESENode: // do nothing, no semantic check for SESEs return; @@ -310,6 +317,13 @@ public class SemanticCheck { checkBlockNode(md, nametable, sbn.getBlockNode()); } + void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) { + if (loopstack.empty()) + throw new Error("continue/break outside of loop"); + LoopNode ln=(LoopNode)loopstack.peek(); + cbn.setLoop(ln); + } + void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) { if (d instanceof TaskDescriptor) throw new Error("Illegal return appears in Task: "+d.getSymbol()); @@ -580,6 +594,7 @@ public class SemanticCheck { } void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) { + loopstack.push(ln); if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) { checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN)); checkBlockNode(md, nametable, ln.getBody()); @@ -597,6 +612,7 @@ public class SemanticCheck { checkBlockNode(md, bn.getVarTable(), ln.getBody()); checkBlockNode(md, bn.getVarTable(), ln.getUpdate()); } + loopstack.pop(); } diff --git a/Robust/src/IR/Virtual.java b/Robust/src/IR/Virtual.java index b5af490c..5e91cde6 100644 --- a/Robust/src/IR/Virtual.java +++ b/Robust/src/IR/Virtual.java @@ -3,6 +3,7 @@ import java.util.*; import Analysis.Locality.LocalityBinding; import Analysis.Locality.LocalityAnalysis; + public class Virtual { State state; LocalityAnalysis locality;