1 19 20 package jode.flow; 21 import jode.GlobalOptions; 22 import jode.type.Type; 23 import jode.expr.*; 24 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 28 public class CreateIfThenElseOperator { 29 30 48 private static boolean createFunnyHelper(FlowBlock trueDest, 49 FlowBlock falseDest, 50 StructuredBlock block) { 51 52 if (block instanceof InstructionBlock 53 && !((InstructionBlock)block).getInstruction().isVoid()) 54 return true; 55 56 if (block instanceof IfThenElseBlock) { 57 IfThenElseBlock ifBlock = (IfThenElseBlock) block; 58 Expression expr1, expr2; 59 if (ifBlock.elseBlock == null) 60 return false; 61 62 63 if (!createFunnyHelper(trueDest, falseDest, ifBlock.thenBlock) 64 | !createFunnyHelper(trueDest, falseDest, ifBlock.elseBlock)) 65 return false; 66 67 if (GlobalOptions.verboseLevel > 0) 68 GlobalOptions.err.print('?'); 69 70 Expression iteo = new IfThenElseOperator(Type.tBoolean) 71 .addOperand(((InstructionBlock) ifBlock.elseBlock) 72 .getInstruction()) 73 .addOperand(((InstructionBlock) ifBlock.thenBlock) 74 .getInstruction()) 75 .addOperand(ifBlock.cond); 76 ((InstructionBlock)ifBlock.thenBlock).setInstruction(iteo); 77 78 ifBlock.thenBlock.moveDefinitions(ifBlock, null); 79 ifBlock.thenBlock.replace(ifBlock); 80 return true; 81 } 82 83 if (block instanceof SequentialBlock 84 && block.getSubBlocks()[0] instanceof ConditionalBlock 85 && block.getSubBlocks()[1] instanceof InstructionBlock) { 86 87 ConditionalBlock condBlock = 88 (ConditionalBlock) block.getSubBlocks()[0]; 89 InstructionBlock pushBlock = 90 (InstructionBlock) block.getSubBlocks()[1]; 91 92 if (!(pushBlock.getInstruction() instanceof ConstOperator)) 93 return false; 94 95 ConstOperator constOp = 96 (ConstOperator) pushBlock.getInstruction(); 97 98 if (condBlock.trueBlock.jump.destination == trueDest 99 && constOp.getValue().equals(new Integer (0))) { 100 101 Expression cond = condBlock.getInstruction(); 102 condBlock.flowBlock.removeSuccessor(condBlock.trueBlock.jump); 103 condBlock.trueBlock.removeJump(); 104 105 pushBlock.setInstruction(cond); 106 pushBlock.moveDefinitions(block, null); 107 pushBlock.replace(block); 108 return true; 109 } 110 } 111 return false; 112 } 113 114 146 public static boolean createFunny(ConditionalBlock cb, 147 StructuredBlock last) { 148 149 if (cb.jump == null 150 || !(cb.getInstruction() instanceof CompareUnaryOperator) 151 || !(last.outer instanceof SequentialBlock) 152 || !(last.outer.getSubBlocks()[0] instanceof IfThenElseBlock)) 153 return false; 154 155 CompareUnaryOperator compare = 156 (CompareUnaryOperator) cb.getInstruction(); 157 158 FlowBlock trueDestination; 159 FlowBlock falseDestination; 160 if (compare.getOperatorIndex() == compare.EQUALS_OP) { 161 trueDestination = cb.jump.destination; 162 falseDestination = cb.trueBlock.jump.destination; 163 } else if (compare.getOperatorIndex() == compare.NOTEQUALS_OP) { 164 falseDestination = cb.jump.destination; 165 trueDestination = cb.trueBlock.jump.destination; 166 } else 167 return false; 168 169 Expression[] e = new Expression[3]; 170 IfThenElseBlock ifBlock; 171 172 SequentialBlock sequBlock = (SequentialBlock) last.outer; 173 return createFunnyHelper(trueDestination, falseDestination, 174 sequBlock.subBlocks[0]); 175 } 176 177 193 public static boolean create(InstructionContainer ic, 194 StructuredBlock last) { 195 Expression cond, thenExpr, elseExpr; 196 InstructionBlock thenBlock; 197 if (ic.jump == null 198 || !(last.outer instanceof SequentialBlock)) 199 return false; 200 SequentialBlock sequBlock = (SequentialBlock)last.outer; 201 if (!(sequBlock.subBlocks[0] instanceof IfThenElseBlock)) 202 return false; 203 204 IfThenElseBlock ifBlock = (IfThenElseBlock)sequBlock.subBlocks[0]; 205 if (!(ifBlock.thenBlock instanceof InstructionBlock) 206 || ifBlock.thenBlock.jump == null 207 || ifBlock.thenBlock.jump.destination != ic.jump.destination 208 || ifBlock.elseBlock != null) 209 return false; 210 211 thenBlock = (InstructionBlock) ifBlock.thenBlock; 212 213 thenExpr = thenBlock.getInstruction(); 214 if (thenExpr.isVoid() || thenExpr.getFreeOperandCount() > 0) 215 return false; 216 elseExpr = ic.getInstruction(); 217 if (elseExpr.isVoid() || elseExpr.getFreeOperandCount() > 0) 218 return false; 219 cond = ifBlock.cond; 220 221 if (GlobalOptions.verboseLevel > 0) 222 GlobalOptions.err.print('?'); 223 224 thenBlock.flowBlock.removeSuccessor(thenBlock.jump); 225 thenBlock.removeJump(); 226 227 IfThenElseOperator iteo = new IfThenElseOperator 228 (Type.tSuperType(thenExpr.getType()) 229 .intersection(Type.tSuperType(elseExpr.getType()))); 230 iteo.addOperand(elseExpr); 231 iteo.addOperand(thenExpr); 232 iteo.addOperand(cond); 233 ic.setInstruction(iteo); 234 ic.moveDefinitions(last.outer, last); 235 last.replace(last.outer); 236 return true; 237 } 238 } 239 | Popular Tags |