+ cond.getEnd().addNext(fcb);
+ fcb.addTrueNext(true_np.getBegin());
+ fcb.addFalseNext(false_np.getBegin());
+ if (true_np.getEnd()!=null)
+ true_np.getEnd().addNext(nopend);
+ if (false_np.getEnd()!=null)
+ false_np.getEnd().addNext(nopend);
+ if (nopend.numPrev()==0)
+ return new NodePair(cond.getBegin(), null);
+
+ return new NodePair(cond.getBegin(), nopend);
+ }
+
+ private NodePair flattenSwitchStatementNode(SwitchStatementNode ssn) {
+ TempDescriptor cond_temp=TempDescriptor.tempFactory("condition",new TypeDescriptor(TypeDescriptor.INT));
+ NodePair cond=flattenExpressionNode(ssn.getCondition(),cond_temp);
+ FlatNop nopend=new FlatNop();
+ NodePair sbody = flattenSwitchBodyNode(ssn.getSwitchBody(), cond_temp, nopend);
+
+ cond.getEnd().addNext(sbody.getBegin());
+
+ return new NodePair(cond.getBegin(), sbody.getEnd());
+ }
+
+ private NodePair flattenSwitchBodyNode(BlockNode bn, TempDescriptor cond_temp, FlatNode endnode) {
+ FlatNode begin=null;
+ FlatNode end=endnode;
+ NodePair prev_true_branch = null;
+ NodePair prev_false_branch = null;
+ for(int i=0; i<bn.size(); i++) {
+ SwitchBlockNode sbn = (SwitchBlockNode)bn.get(i);
+ HashSet oldbs=breakset;
+ breakset=new HashSet();
+
+ NodePair body=flattenBlockNode(sbn.getSwitchBlockStatement());
+ Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
+ FlatNode cond_begin = null;
+ NodePair prev_fnp = null;
+ for(int j = 0; j < slnv.size(); j++) {
+ SwitchLabelNode sln = slnv.elementAt(j);
+ NodePair left = null;
+ NodePair false_np = null;
+ if(sln.isDefault()) {
+ left = body;
+ } else {
+ TempDescriptor cond_tmp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
+ TempDescriptor temp_left=TempDescriptor.tempFactory("leftop", sln.getCondition().getType());
+ Operation op=new Operation(Operation.EQUAL);
+ left=flattenExpressionNode(sln.getCondition(), temp_left);
+ FlatOpNode fon=new FlatOpNode(cond_tmp, temp_left, cond_temp, op);
+ fon.setNumLine(sln.getNumLine());
+ left.getEnd().addNext(fon);
+
+ FlatCondBranch fcb=new FlatCondBranch(cond_tmp);
+ fcb.setNumLine(bn.getNumLine());
+ fcb.setTrueProb(State.TRUEPROB);
+
+ FlatNop nop=new FlatNop();
+ false_np=new NodePair(nop,nop);
+
+ fon.addNext(fcb);
+ fcb.addTrueNext(body.getBegin());
+ fcb.addFalseNext(false_np.getBegin());
+ }
+ if((prev_fnp != null) && (prev_fnp.getEnd() != null)) {
+ prev_fnp.getEnd().addNext(left.getBegin());
+ }
+ prev_fnp = false_np;
+
+ if (begin==null) {
+ begin = left.getBegin();
+ }
+ if(cond_begin == null) {
+ cond_begin = left.getBegin();
+ }
+ }
+ if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
+ prev_false_branch.getEnd().addNext(cond_begin);
+ }
+ prev_false_branch = prev_fnp;
+ if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
+ prev_true_branch.getEnd().addNext(body.getBegin());
+ }
+ prev_true_branch = body;
+ for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
+ FlatNode fn=(FlatNode)breakit.next();
+ breakit.remove();
+ fn.addNext(endnode);
+ }
+ breakset=oldbs;
+ }
+ if((prev_true_branch != null) && (prev_true_branch.getEnd() != null)) {
+ prev_true_branch.getEnd().addNext(endnode);
+ }
+ if((prev_false_branch != null) && (prev_false_branch.getEnd() != null)) {
+ prev_false_branch.getEnd().addNext(endnode);
+ }
+ if(begin == null) {
+ end=begin=new FlatNop();
+ }
+ return new NodePair(begin,end);
+ }
+
+ private NodePair flattenLoopNode(LoopNode ln) {
+ HashSet oldbs=breakset;
+ HashSet oldcs=continueset;
+ breakset=new HashSet();
+ continueset=new HashSet();
+
+ if (ln.getType()==LoopNode.FORLOOP) {
+ NodePair initializer=flattenBlockNode(ln.getInitializer());
+ TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
+ NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
+ NodePair update=flattenBlockNode(ln.getUpdate());
+ NodePair body=flattenBlockNode(ln.getBody());
+ FlatNode begin=initializer.getBegin();
+ FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+ fcb.setNumLine(ln.getNumLine());
+ fcb.setTrueProb(State.TRUEPROB);
+ fcb.setLoop();
+ FlatNop nopend=new FlatNop();
+ FlatBackEdge backedge=new FlatBackEdge();
+
+ FlatNop nop2=new FlatNop();
+ initializer.getEnd().addNext(nop2);
+ nop2.addNext(condition.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);
+ }
+ breakset=oldbs;
+ continueset=oldcs;
+ return new NodePair(begin,nopend);
+ } else if (ln.getType()==LoopNode.WHILELOOP) {
+ TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
+ NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
+ NodePair body=flattenBlockNode(ln.getBody());
+ FlatNode begin=condition.getBegin();
+ FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+ fcb.setNumLine(ln.getNumLine());
+ fcb.setTrueProb(State.TRUEPROB);
+ fcb.setLoop();
+ FlatNop nopend=new FlatNop();
+ FlatBackEdge backedge=new FlatBackEdge();
+
+ if (body.getEnd()!=null)
+ body.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(backedge);
+ }
+ for(Iterator breakit=breakset.iterator();breakit.hasNext();) {
+ FlatNode fn=(FlatNode)breakit.next();
+ breakit.remove();
+ fn.addNext(nopend);
+ }
+ breakset=oldbs;
+ continueset=oldcs;
+ return new NodePair(begin,nopend);
+ } else if (ln.getType()==LoopNode.DOWHILELOOP) {
+ TempDescriptor cond_temp=TempDescriptor.tempFactory("condition", new TypeDescriptor(TypeDescriptor.BOOLEAN));
+ NodePair condition=flattenExpressionNode(ln.getCondition(),cond_temp);
+ NodePair body=flattenBlockNode(ln.getBody());
+ FlatNode begin=body.getBegin();
+ FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+ fcb.setNumLine(ln.getNumLine());
+ fcb.setTrueProb(State.TRUEPROB);
+ fcb.setLoop();
+ FlatNop nopend=new FlatNop();
+ FlatBackEdge backedge=new FlatBackEdge();
+
+ if (body.getEnd()!=null)
+ body.getEnd().addNext(condition.getBegin());
+ condition.getEnd().addNext(fcb);
+ 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);
+ }
+ breakset=oldbs;
+ continueset=oldcs;
+ return new NodePair(begin,nopend);
+ } else throw new Error();
+ }
+
+ private NodePair flattenReturnNode(ReturnNode rntree) {
+ TempDescriptor retval=null;
+ NodePair cond=null;
+ if (rntree.getReturnExpression()!=null) {
+ retval=TempDescriptor.tempFactory("ret_value", rntree.getReturnExpression().getType());
+ cond=flattenExpressionNode(rntree.getReturnExpression(),retval);