summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/transformations/enums/SwitchEnumSupport.java
blob: e4273d6c52b4749b601d2cfb06a47307137a9c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.transformations.enums;

import com.android.jack.Jack;
import com.android.jack.Options;
import com.android.jack.config.id.Private;
import com.android.jack.ir.ast.JArrayLength;
import com.android.jack.ir.ast.JArrayRef;
import com.android.jack.ir.ast.JArrayType;
import com.android.jack.ir.ast.JAsgOperation;
import com.android.jack.ir.ast.JBinaryOperation;
import com.android.jack.ir.ast.JBinaryOperator;
import com.android.jack.ir.ast.JBlock;
import com.android.jack.ir.ast.JCaseStatement;
import com.android.jack.ir.ast.JCatchBlock;
import com.android.jack.ir.ast.JClass;
import com.android.jack.ir.ast.JDefinedClass;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JDefinedEnum;
import com.android.jack.ir.ast.JEnumField;
import com.android.jack.ir.ast.JEnumLiteral;
import com.android.jack.ir.ast.JExpression;
import com.android.jack.ir.ast.JExpressionStatement;
import com.android.jack.ir.ast.JField;
import com.android.jack.ir.ast.JFieldId;
import com.android.jack.ir.ast.JFieldRef;
import com.android.jack.ir.ast.JIfStatement;
import com.android.jack.ir.ast.JIntLiteral;
import com.android.jack.ir.ast.JLiteral;
import com.android.jack.ir.ast.JLocal;
import com.android.jack.ir.ast.JLocalRef;
import com.android.jack.ir.ast.JMethod;
import com.android.jack.ir.ast.JMethodBody;
import com.android.jack.ir.ast.JMethodCall;
import com.android.jack.ir.ast.JMethodId;
import com.android.jack.ir.ast.JModifier;
import com.android.jack.ir.ast.JNeqOperation;
import com.android.jack.ir.ast.JNewArray;
import com.android.jack.ir.ast.JNullLiteral;
import com.android.jack.ir.ast.JPrimitiveType.JPrimitiveTypeEnum;
import com.android.jack.ir.ast.JReturnStatement;
import com.android.jack.ir.ast.JStatement;
import com.android.jack.ir.ast.JSwitchStatement;
import com.android.jack.ir.ast.JTryStatement;
import com.android.jack.ir.ast.JType;
import com.android.jack.ir.ast.JVisitor;
import com.android.jack.ir.ast.MethodKind;
import com.android.jack.ir.formatter.BinaryQualifiedNameFormatter;
import com.android.jack.ir.sourceinfo.SourceInfo;
import com.android.jack.lookup.JLookup;
import com.android.jack.lookup.JMethodLookupException;
import com.android.jack.shrob.obfuscation.OriginalNames;
import com.android.jack.transformations.LocalVarCreator;
import com.android.jack.transformations.exceptions.TryStatementSchedulingSeparator;
import com.android.jack.transformations.request.AppendField;
import com.android.jack.transformations.request.AppendMethod;
import com.android.jack.transformations.request.Replace;
import com.android.jack.transformations.request.TransformationRequest;
import com.android.jack.transformations.threeaddresscode.ThreeAddressCodeForm;
import com.android.jack.util.NamingTools;
import com.android.jack.util.filter.Filter;
import com.android.sched.item.Description;
import com.android.sched.item.Name;
import com.android.sched.item.Synchronized;
import com.android.sched.marker.Marker;
import com.android.sched.marker.ValidOn;
import com.android.sched.schedulable.Constraint;
import com.android.sched.schedulable.RunnableSchedulable;
import com.android.sched.schedulable.Transform;
import com.android.sched.schedulable.Use;
import com.android.sched.util.config.HasKeyId;
import com.android.sched.util.config.ThreadConfig;
import com.android.sched.util.config.id.BooleanPropertyId;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Add support for partial recompilation for enum used into switches.
 */
@Description("Add support for partial recompilation for enum used into switches.")
@Name("SwitchEnumSupport")
@Synchronized
@Constraint(need = {JSwitchStatement.class, JEnumField.class, JEnumLiteral.class,
    SwitchEnumSupport.UsedEnumField.class, OriginalNames.class})
@Transform(modify = JSwitchStatement.class, add = {EnumMappingMarker.class,
    JNewArray.class, JAsgOperation.NonReusedAsg.class, JMethodCall.class, JArrayRef.class,
    JArrayLength.class, JLocalRef.class, JField.class,
    JMethod.class, JMethodBody.class, JFieldRef.class, JNullLiteral.class, JLocal.class,
    JIfStatement.class, JReturnStatement.class, JBlock.class, JTryStatement.class,
    JIntLiteral.class, JExpressionStatement.class,
    JNeqOperation.class, TryStatementSchedulingSeparator.SeparatorTag.class,
    EnumMappingSchedulingSeparator.SeparatorTag.class},
    remove = {JSwitchStatement.SwitchWithEnum.class, ThreeAddressCodeForm.class})
@Use(value = {LocalVarCreator.class})
@HasKeyId
public class SwitchEnumSupport implements RunnableSchedulable<JMethod> {

  @Nonnull
  public static final BooleanPropertyId SORT_ENUM_FIELD = BooleanPropertyId.create(
      "jack.internal.switch-enumfield.sort",
      "Generate determinist code to initialize constant array indexed by ordinal "
      + "value of enum field")
      .addDefaultValue(Boolean.TRUE).withCategory(Private.get());

  private final JType noSuchFieldErrorType =
      Jack.getSession().getPhantomLookup().getType("Ljava/lang/NoSuchFieldError;");

  /**
   * Enum fields used into switch.
   */
  @Description("Enum fields used into switch")
  @ValidOn(JDefinedClass.class)
  public static class UsedEnumField implements Marker {
    @Nonnull
    private final Set<JFieldId> enumFields;

    public UsedEnumField(@Nonnull Set<JFieldId> enumFields) {
      this.enumFields = enumFields;
    }

    @Nonnull
    public Set<JFieldId> getEnumFields() {
      return enumFields;
    }

    @Override
    public Marker cloneIfNeeded() {
      return this;
    }
  }

  @Nonnull
  private final Filter<JMethod> filter = ThreadConfig.get(Options.METHOD_FILTER);

  private class Visitor extends JVisitor {

    private final boolean sortEnumField = ThreadConfig.get(SORT_ENUM_FIELD).booleanValue();

    private static final String ORDINAL = "ordinal";

    @Nonnull
    private final TransformationRequest tr;

    @Nonnull
    private final JDefinedClassOrInterface currentClass;

    @Nonnull
    private final JLookup lookup;

    @CheckForNull
    private Set<JFieldId> usedEnumFields;

    public Visitor(@Nonnull TransformationRequest tr,
        @Nonnull JDefinedClassOrInterface currentClass) {
      this.tr = tr;
      this.currentClass = currentClass;
      this.lookup = Jack.getSession().getPhantomLookup();
    }

    @Override
    public boolean visit(@Nonnull JMethod method) {
      if (method.getEnclosingType() instanceof JDefinedClass) {
        UsedEnumField uef = method.getEnclosingType().getMarker(UsedEnumField.class);
        assert uef != null;
        usedEnumFields = uef.getEnumFields();
      }
      return super.visit(method);
    }

    @Override
    public boolean visit(@Nonnull JSwitchStatement switchStmt) {
      JExpression expr = switchStmt.getExpr();

      JType exprType = expr.getType();

      if (exprType instanceof JDefinedEnum) {
          JDefinedEnum enumType = (JDefinedEnum) exprType;

          JMethod getEnumSwitchValues = getSwitchValuesMethod(enumType);

          // Replace enum access by $SwitchesValues[x.ordinal()]
          JMethodId methodId = getEnumSwitchValues.getMethodId();
          JExpression callSwitchValues = new JMethodCall(
              switchStmt.getSourceInfo(), null, getEnumSwitchValues.getEnclosingType(),
              methodId, getEnumSwitchValues.getType(),
              methodId.canBeVirtual());

        JMethodId ordinalMethodId = enumType.getOrCreateMethodId(
            ORDINAL, Collections.<JType>emptyList(), MethodKind.INSTANCE_VIRTUAL);

          tr.append(new Replace(expr, new JArrayRef(SourceInfo.UNKNOWN, callSwitchValues,
            new JMethodCall(SourceInfo.UNKNOWN, expr, enumType, ordinalMethodId,
                JPrimitiveTypeEnum.INT.getType(), ordinalMethodId.canBeVirtual()))));
      }
      return super.visit(switchStmt);
    }


    @Override
    public boolean visit(@Nonnull JCaseStatement caseStmt) {
      JLiteral caseExpr = caseStmt.getExpr();

      if (caseExpr != null && caseExpr instanceof JEnumLiteral) {
        JEnumLiteral literal = (JEnumLiteral) caseExpr;

        JMethod getEnumSwitchValues =
            getSwitchValuesMethod((JDefinedEnum) literal.getType());

        EnumMappingMarker emm = getEnumSwitchValues.getMarker(EnumMappingMarker.class);
        assert emm != null;

        Integer enumSwitchValue = emm.getMapping().get(literal.getFieldId());
        assert enumSwitchValue != null;

        tr.append(new Replace(caseExpr,
            new JIntLiteral(caseStmt.getSourceInfo(), enumSwitchValue.intValue())));
      }

      return super.visit(caseStmt);
    }

    @Nonnull
    private JMethod getSwitchValuesMethod(@Nonnull JDefinedEnum enumType) {
      SourceInfo dbgInfo = SourceInfo.UNKNOWN;
      String enumName =
          NamingTools.getValidName(BinaryQualifiedNameFormatter.getFormatter().getName(enumType));
      String methodName =
          NamingTools.getNonSourceConflictingName("get" + enumName + "SwitchesValues");
      String fieldName =
          NamingTools.getNonSourceConflictingName(enumName + "SwitchesValues");

      JArrayType switchValuesArrayType = JPrimitiveTypeEnum.INT.getType().getArray();
      JArrayType enumArrayType = enumType.getArray();

      JMethod getEnumSwitchValues;

      try {
        getEnumSwitchValues = currentClass.getMethod(methodName, switchValuesArrayType);
      } catch (JMethodLookupException e) {
        TransformationRequest localTr = new TransformationRequest(currentClass);

        // Create $[EnumName]switchesValues field
        JField enumSwitchValues =
            new JField(dbgInfo, fieldName, currentClass, switchValuesArrayType,
                JModifier.PRIVATE | JModifier.STATIC | JModifier.SYNTHETIC);
        localTr.append(new AppendField(currentClass, enumSwitchValues));

        // Create method $getEnumSwitchesValues
        getEnumSwitchValues =
            new JMethod(dbgInfo, new JMethodId(methodName, MethodKind.STATIC),
                currentClass, switchValuesArrayType,
                JModifier.PRIVATE | JModifier.STATIC | JModifier.SYNTHETIC);
        localTr.append(new AppendMethod(currentClass, getEnumSwitchValues));

        JBlock bodyBlock = new JBlock(dbgInfo);
        JMethodBody body = new JMethodBody(dbgInfo, bodyBlock);
        getEnumSwitchValues.setBody(body);
        LocalVarCreator lvc = new LocalVarCreator(getEnumSwitchValues, "es");

        // if ($switchesValues != null) return ($switchesValues);
        JFieldId enumSwitchValuesId = enumSwitchValues.getId();
        JExpression checkNull =
            JBinaryOperation.create(dbgInfo, JBinaryOperator.NEQ, new JFieldRef(dbgInfo,
                null /* instance */, enumSwitchValuesId, currentClass),
                new JNullLiteral(dbgInfo));
        JBlock thenBlock = new JBlock(dbgInfo);
        thenBlock.addStmt(new JReturnStatement(dbgInfo,
            new JFieldRef(dbgInfo, null /* instance */, enumSwitchValuesId, currentClass)));
        bodyBlock.addStmt(new JIfStatement(dbgInfo, checkNull, thenBlock, null /* elseStmt */));

        JLocal arrayVar = lvc.createTempLocal(switchValuesArrayType, dbgInfo, localTr);

        JMethod valuesMethod;
        try {
          valuesMethod = enumType.getMethod("values", enumArrayType);
        } catch (JMethodLookupException e1) {
          // A valid enum must have a values() method
          throw new AssertionError(e1);
        }

        // int[] array = new int[enum.values().length]
        JMethodId valuesId = valuesMethod.getMethodId();
        JExpression valuesLength = new JArrayLength(dbgInfo, new JMethodCall(dbgInfo,
            null /* instance */, enumType, valuesId, valuesMethod.getType(),
            valuesId.canBeVirtual()));
        List<JExpression> dimensions = new ArrayList<JExpression>();
        dimensions.add(valuesLength);
        bodyBlock.addStmt(new JAsgOperation(dbgInfo, new JLocalRef(dbgInfo, arrayVar),
            JNewArray.createWithDims(dbgInfo, switchValuesArrayType, dimensions)).makeStatement());


        // 0 is used to represent unknown value.
        int usedEnumFieldCstValue = 1;
        assert usedEnumFields != null;
        // +1 due to the fact that numbering of used fields does not start to 0 but 1, otherwise
        // the numbering of used fields and unused fields could be overlap.
        int unusedEnumFieldCstValue = usedEnumFields.size() + 1;
        EnumMappingMarker emm = new EnumMappingMarker();

        List<JField> enumFields = enumType.getFields();
        if (sortEnumField) {
          Collections.sort(enumFields, new Comparator<JField>() {
            @Override
            public int compare(JField o1, JField o2) {
              return o1.getName().compareTo(o2.getName());
            }
          });
        }
        for (JField enumField : enumFields) {

          if (!(enumField instanceof JEnumField)) {
            continue;
          }

          // try { array[enumField.ordinal()] = constant; } catch (NoSuchFieldError ex) {}
          JBlock tryBlock = new JBlock(dbgInfo);

          JLocal exVar = new JLocal(dbgInfo, "ex", noSuchFieldErrorType, JModifier.SYNTHETIC, body);

          List<JCatchBlock> catchBlock = new ArrayList<JCatchBlock>(1);
          catchBlock.add(new JCatchBlock(dbgInfo, Collections
              .singletonList((JClass) noSuchFieldErrorType), exVar));

          bodyBlock.addStmt(new JTryStatement(dbgInfo,
              Collections.<JStatement>emptyList(),
              tryBlock,
              catchBlock,
              null /* finallyBlock */));

          JFieldId enumFieldId = enumField.getId();
          JExpression enumFieldAccess =
              new JFieldRef(dbgInfo, null /* instance */, enumFieldId, enumType);
          JMethodId ordinalMethodId = enumType.getOrCreateMethodId(
              ORDINAL, Collections.<JType>emptyList(), MethodKind.INSTANCE_VIRTUAL);
          JExpression callOrdinal =
              new JMethodCall(dbgInfo, enumFieldAccess, enumType, ordinalMethodId,
                  JPrimitiveTypeEnum.INT.getType(),
                  ordinalMethodId.canBeVirtual());

          int constant;
          assert usedEnumFields != null;
          if (usedEnumFields.contains(enumFieldId)) {
            constant = usedEnumFieldCstValue;
            usedEnumFieldCstValue++;
          } else {
            constant = unusedEnumFieldCstValue;
            unusedEnumFieldCstValue++;
          }

          tryBlock.addStmt(new JAsgOperation(dbgInfo, new JArrayRef(dbgInfo, new JLocalRef(dbgInfo,
              arrayVar), callOrdinal), new JIntLiteral(dbgInfo, constant)).makeStatement());

          emm.addMapping(enumFieldId, constant);
        }

        getEnumSwitchValues.addMarker(emm);
        bodyBlock.addStmt(new JAsgOperation(dbgInfo, new JFieldRef(dbgInfo, null /* instance */,
            enumSwitchValuesId, currentClass), new JLocalRef(dbgInfo, arrayVar)).makeStatement());
        bodyBlock.addStmt(new JReturnStatement(dbgInfo, new JLocalRef(dbgInfo, arrayVar)));

        localTr.commit();
      }

      return (getEnumSwitchValues);
    }
  }

  @Override
  public synchronized void run(@Nonnull JMethod method) throws Exception {
    JDefinedClassOrInterface enclosingType = method.getEnclosingType();

    if (enclosingType.isExternal() || method.isNative() || method.isAbstract()
        || !filter.accept(this.getClass(), method)) {
      return;
    }

    TransformationRequest tr = new TransformationRequest(enclosingType);
    Visitor visitor = new Visitor(tr, enclosingType);
    visitor.accept(method);
    tr.commit();
  }

}