Home | History | Annotate | Download | only in dexlib2
      1 /*
      2  * Copyright 2013, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.dexlib2;
     33 
     34 import com.google.common.collect.Maps;
     35 import com.google.common.collect.RangeMap;
     36 
     37 import javax.annotation.Nonnull;
     38 import javax.annotation.Nullable;
     39 import java.util.EnumMap;
     40 import java.util.HashMap;
     41 
     42 import static org.jf.dexlib2.VersionMap.NO_VERSION;
     43 import static org.jf.dexlib2.VersionMap.mapApiToArtVersion;
     44 import static org.jf.dexlib2.VersionMap.mapArtVersionToApi;
     45 
     46 public class Opcodes {
     47 
     48     /**
     49      * Either the api level for dalvik opcodes, or the art version for art opcodes
     50      */
     51     public final int api;
     52     public final int artVersion;
     53     @Nonnull private final Opcode[] opcodesByValue = new Opcode[255];
     54     @Nonnull private final EnumMap<Opcode, Short> opcodeValues;
     55     @Nonnull private final HashMap<String, Opcode> opcodesByName;
     56 
     57     @Nonnull
     58     public static Opcodes forApi(int api) {
     59         return new Opcodes(api, NO_VERSION);
     60     }
     61 
     62     @Nonnull
     63     public static Opcodes forArtVersion(int artVersion) {
     64         return new Opcodes(NO_VERSION, artVersion);
     65     }
     66 
     67     /**
     68      * @return a default Opcodes instance for when the exact Opcodes to use doesn't matter or isn't known
     69      */
     70     @Nonnull
     71     public static Opcodes getDefault() {
     72         // The last pre-art api
     73         return forApi(20);
     74     }
     75 
     76     private Opcodes(int api, int artVersion) {
     77 
     78 
     79         if (api >= 21) {
     80         this.api = api;
     81             this.artVersion = mapApiToArtVersion(api);
     82         } else if (artVersion >= 0 && artVersion < 39) {
     83             this.api = mapArtVersionToApi(artVersion);
     84         this.artVersion = artVersion;
     85         } else {
     86             this.api = api;
     87             this.artVersion = artVersion;
     88         }
     89 
     90         opcodeValues = new EnumMap<Opcode, Short>(Opcode.class);
     91         opcodesByName = Maps.newHashMap();
     92 
     93         int version;
     94         if (isArt()) {
     95             version = this.artVersion;
     96         } else {
     97             version = this.api;
     98         }
     99 
    100         for (Opcode opcode: Opcode.values()) {
    101             RangeMap<Integer, Short> versionToValueMap;
    102 
    103             if (isArt()) {
    104                 versionToValueMap = opcode.artVersionToValueMap;
    105             } else {
    106                 versionToValueMap = opcode.apiToValueMap;
    107             }
    108 
    109             Short opcodeValue = versionToValueMap.get(version);
    110             if (opcodeValue != null) {
    111                 if (!opcode.format.isPayloadFormat) {
    112                     opcodesByValue[opcodeValue] = opcode;
    113                 }
    114                 opcodeValues.put(opcode, opcodeValue);
    115                 opcodesByName.put(opcode.name.toLowerCase(), opcode);
    116             }
    117         }
    118     }
    119 
    120     @Nullable
    121     public Opcode getOpcodeByName(@Nonnull String opcodeName) {
    122         return opcodesByName.get(opcodeName.toLowerCase());
    123     }
    124 
    125     @Nullable
    126     public Opcode getOpcodeByValue(int opcodeValue) {
    127         switch (opcodeValue) {
    128             case 0x100:
    129                 return Opcode.PACKED_SWITCH_PAYLOAD;
    130             case 0x200:
    131                 return Opcode.SPARSE_SWITCH_PAYLOAD;
    132             case 0x300:
    133                 return Opcode.ARRAY_PAYLOAD;
    134             default:
    135                 if (opcodeValue >= 0 && opcodeValue < opcodesByValue.length) {
    136                     return opcodesByValue[opcodeValue];
    137                 }
    138                 return null;
    139         }
    140     }
    141 
    142     @Nullable
    143     public Short getOpcodeValue(@Nonnull Opcode opcode) {
    144         return opcodeValues.get(opcode);
    145     }
    146 
    147     public boolean isArt() {
    148         return artVersion != NO_VERSION;
    149     }
    150 }
    151