@Override public void visit(InvokeInstruction insn) { Expr[] exprArgs = new Expr[insn.getMethod().getParameterTypes().length]; for (int i = 0; i < insn.getArguments().size(); ++i) { exprArgs[i] = Expr.var(insn.getArguments().get(i).getIndex()); } InvocationExpr invocationExpr; if (insn.getInstance() != null) { if (insn.getType() == InvocationType.VIRTUAL) { invocationExpr = Expr.invoke(insn.getMethod(), Expr.var(insn.getInstance().getIndex()), exprArgs); } else { invocationExpr = Expr.invokeSpecial(insn.getMethod(), Expr.var(insn.getInstance().getIndex()), exprArgs); } } else { invocationExpr = Expr.invokeStatic(insn.getMethod(), exprArgs); } AssignmentStatement stmt; if (insn.getReceiver() != null) { stmt = Statement.assign(Expr.var(insn.getReceiver().getIndex()), invocationExpr); stmt.getDebugNames().addAll(insn.getReceiver().getDebugNames()); } else { stmt = Statement.assign(null, invocationExpr); } stmt.setLocation(currentLocation); stmt.setAsync(async); async = false; statements.add(stmt); }
@Override public void visit(AssignInstruction insn) { AssignmentStatement stmt = Statement.assign( Expr.var(insn.getReceiver().getIndex()), Expr.var(insn.getAssignee().getIndex())); stmt.getDebugNames().addAll(insn.getReceiver().getDebugNames()); stmt.setLocation(currentLocation); statements.add(stmt); }
@Override public void visit(PutElementInstruction insn) { AssignmentStatement stmt = Statement.assign( Expr.subscript( Expr.var(insn.getArray().getIndex()), Expr.var(insn.getIndex().getIndex())), Expr.var(insn.getValue().getIndex())); stmt.setLocation(currentLocation); statements.add(stmt); }
@Override public void visit(PutFieldInstruction insn) { Expr right = Expr.var(insn.getValue().getIndex()); Expr left; if (insn.getInstance() != null) { left = Expr.qualify(Expr.var(insn.getInstance().getIndex()), insn.getField()); } else { left = Expr.qualify(null, insn.getField()); } AssignmentStatement stmt = Statement.assign(left, right); stmt.setLocation(currentLocation); statements.add(stmt); }
@Override public void visit(VariableExpr expr) { int index = expr.getIndex(); resultExpr = expr; if (readFrequencies[index] != 1 || preservedVars[index]) { return; } if (resultSequence.isEmpty()) { return; } Statement last = resultSequence.get(resultSequence.size() - 1); if (!(last instanceof AssignmentStatement)) { return; } AssignmentStatement assignment = (AssignmentStatement) last; if (assignment.isAsync()) { return; } if (!(assignment.getLeftValue() instanceof VariableExpr)) { return; } VariableExpr var = (VariableExpr) assignment.getLeftValue(); if (var.getLocation() != null && assignment.getLocation() != null && !assignment.getLocation().equals(var.getLocation())) { return; } if (var.getIndex() == index) { resultSequence.remove(resultSequence.size() - 1); assignment.getRightValue().setLocation(assignment.getLocation()); assignment.getRightValue().acceptVisitor(this); } }
@Override public void visit(GetFieldInstruction insn) { if (insn.getInstance() != null) { AssignmentStatement stmt = Statement.assign( Expr.var(insn.getReceiver().getIndex()), Expr.qualify(Expr.var(insn.getInstance().getIndex()), insn.getField())); stmt.setLocation(currentLocation); statements.add(stmt); } else { Expr fieldExpr = Expr.qualify(null, insn.getField()); AssignmentStatement stmt = Statement.assign(Expr.var(insn.getReceiver().getIndex()), fieldExpr); stmt.setLocation(currentLocation); statements.add(stmt); } }
@Override public void visit(AssignmentStatement statement) { if (statement.getLeftValue() == null) { statement.getRightValue().acceptVisitor(this); if (resultExpr instanceof InvocationExpr && tryApplyConstructor((InvocationExpr) resultExpr)) { resultStmt = new SequentialStatement(); } else { statement.setRightValue(resultExpr); resultStmt = statement; } } else { statement.getRightValue().acceptVisitor(this); Expr right = resultExpr; Expr left = statement.getLeftValue(); if (!(statement.getLeftValue() instanceof VariableExpr)) { statement.getLeftValue().acceptVisitor(this); left = resultExpr; } statement.setLeftValue(left); statement.setRightValue(right); resultStmt = statement; } }
private boolean tryApplyConstructor(InvocationExpr expr) { if (!expr.getMethod().getName().equals("<init>")) { return false; } if (resultSequence == null || resultSequence.isEmpty()) { return false; } Statement last = resultSequence.get(resultSequence.size() - 1); if (!(last instanceof AssignmentStatement)) { return false; } AssignmentStatement assignment = (AssignmentStatement) last; if (!(assignment.getLeftValue() instanceof VariableExpr)) { return false; } VariableExpr var = (VariableExpr) assignment.getLeftValue(); if (!(expr.getArguments().get(0) instanceof VariableExpr)) { return false; } VariableExpr target = (VariableExpr) expr.getArguments().get(0); if (target.getIndex() != var.getIndex()) { return false; } if (!(assignment.getRightValue() instanceof NewExpr)) { return false; } NewExpr constructed = (NewExpr) assignment.getRightValue(); if (!constructed.getConstructedClass().equals(expr.getMethod().getClassName())) { return false; } Expr[] args = expr.getArguments().toArray(new Expr[0]); args = Arrays.copyOfRange(args, 1, args.length); InvocationExpr constructrExpr = Expr.constructObject(expr.getMethod(), args); constructrExpr.setLocation(expr.getLocation()); assignment.setRightValue(constructrExpr); readFrequencies[var.getIndex()]--; return true; }
private void assign(Expr source, Variable target) { AssignmentStatement stmt = Statement.assign(Expr.var(target.getIndex()), source); stmt.setLocation(currentLocation); stmt.getDebugNames().addAll(target.getDebugNames()); statements.add(stmt); }