| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | public class IRPrinter { |
| 37 | |
| 38 | public static void print(IR ir, PrintStream out) { |
| 39 | // print method signature |
| 40 | out.println("---------- " + ir.getMethod() + " ----------"); |
| 41 | // print parameters |
| 42 | out.print("Parameters: "); |
| 43 | out.println(ir.getParams() |
| 44 | .stream() |
| 45 | .map(p -> p.getType() + " " + p) |
| 46 | .collect(Collectors.joining(", "))); |
| 47 | // print all variables |
| 48 | out.println("Variables:"); |
| 49 | ir.getVars().forEach(v -> out.println(v.getType() + " " + v)); |
| 50 | // print all statements |
| 51 | out.println("Statements:"); |
| 52 | ir.forEach(s -> out.println(toString(s))); |
| 53 | // print all try-catch blocks |
| 54 | if (!ir.getExceptionEntries().isEmpty()) { |
| 55 | out.println("Exception entries:"); |
| 56 | ir.getExceptionEntries().forEach(b -> out.println(" " + b)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public static String toString(Stmt stmt) { |
| 61 | if (stmt instanceof Invoke) { |
| 62 | return toString((Invoke) stmt); |
| 63 | } else { |
| 64 | return String.format("%s %s;", position(stmt), stmt); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public static String toString(Invoke invoke) { |
| 69 | Formatter formatter = new Formatter(); |
| 70 | formatter.format("%s ", position(invoke)); |
| 71 | if (invoke.getResult() != null) { |
| 72 | // some variable names contain '%', which will be treated as |
| 73 | // format specifier by formatter, thus we need to escape it |
| 74 | String lhs = invoke.getResult().toString().replace("%", "%%"); |
| 75 | formatter.format(lhs + " = "); |
| 76 | } |
| 77 | InvokeExp ie = invoke.getInvokeExp(); |
| 78 | formatter.format("%s ", ie.getInvokeString()); |
| 79 | if (ie instanceof InvokeDynamic indy) { |
| 80 | formatter.format("%s \"%s\" <%s>[%s]%s;", |
| 81 | indy.getBootstrapMethodRef(), |
| 82 | indy.getMethodName(), indy.getMethodType(), |
| 83 | indy.getBootstrapArgs() |
| 84 | .stream() |
| 85 | .map(Literal::toString) |
| 86 | .collect(Collectors.joining(", ")), |
| 87 | indy.getArgsString()); |
| 88 | } else { |
| 89 | if (ie instanceof InvokeInstanceExp) { |
| 90 | formatter.format("%s.", ((InvokeInstanceExp) ie).getBase().getName()); |
| 91 | } |
| 92 | formatter.format("%s%s;", ie.getMethodRef(), ie.getArgsString()); |
| 93 | } |
nothing calls this directly
no outgoing calls
no test coverage detected