Helper for building IR for a method from scratch. Given a JMethod, this helper automatically creates this variable (for instance method), parameters, and return variable (if method return type is not void) for the method.
| 40 | * return type is not {@code void}) for the method. |
| 41 | */ |
| 42 | public class IRBuildHelper { |
| 43 | |
| 44 | private static final String THIS = "%this"; |
| 45 | |
| 46 | private static final String PARAM = "%param"; |
| 47 | |
| 48 | private static final String TEMP = "%temp"; |
| 49 | |
| 50 | private static final String RETURN = "%return"; |
| 51 | |
| 52 | private final JMethod method; |
| 53 | |
| 54 | private final Var thisVar; |
| 55 | |
| 56 | private final List<Var> params; |
| 57 | |
| 58 | private final Var returnVar; |
| 59 | |
| 60 | private final Set<Var> returnVars; |
| 61 | |
| 62 | private final List<Var> vars; |
| 63 | |
| 64 | /** |
| 65 | * Counter for indexing all variables. |
| 66 | */ |
| 67 | private int varCounter = 0; |
| 68 | |
| 69 | /** |
| 70 | * Counter for naming temporary variables. |
| 71 | */ |
| 72 | private int tempCounter = 0; |
| 73 | |
| 74 | public IRBuildHelper(JMethod method) { |
| 75 | this.method = method; |
| 76 | // build this variable |
| 77 | vars = new ArrayList<>(); |
| 78 | thisVar = method.isStatic() ? null : |
| 79 | newVar(THIS, method.getDeclaringClass().getType()); |
| 80 | // build parameters |
| 81 | params = new ArrayList<>(method.getParamCount()); |
| 82 | for (int i = 0; i < method.getParamCount(); ++i) { |
| 83 | params.add(newVar(PARAM + i, method.getParamType(i))); |
| 84 | } |
| 85 | // build return variable |
| 86 | Type retType = method.getReturnType(); |
| 87 | if (!retType.equals(VoidType.VOID)) { |
| 88 | returnVar = newVar(RETURN, retType); |
| 89 | returnVars = Set.of(returnVar); |
| 90 | } else { |
| 91 | returnVar = null; |
| 92 | returnVars = Set.of(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return {@code this} variable of the IR being built. |
| 98 | * @throws AssertionError if the method is not an instance method. |
| 99 | */ |
nothing calls this directly
no outgoing calls
no test coverage detected