Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 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; |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 35 | import com.google.common.collect.RangeMap; |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 36 | |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 37 | import javax.annotation.Nonnull; |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 38 | import javax.annotation.Nullable; |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 39 | import java.util.EnumMap; |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
| 41 | |
| 42 | public class Opcodes { |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 43 | |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Either the api level for dalvik opcodes, or the art version for art opcodes |
| 46 | */ |
| 47 | public final int api; |
| 48 | public final int artVersion; |
| 49 | @Nonnull private final Opcode[] opcodesByValue = new Opcode[255]; |
| 50 | @Nonnull private final EnumMap<Opcode, Short> opcodeValues; |
| 51 | @Nonnull private final HashMap<String, Opcode> opcodesByName; |
| 52 | |
| 53 | @Nonnull |
| 54 | public static Opcodes forApi(int api) { |
| 55 | return new Opcodes(api, VersionMap.mapApiToArtVersion(api), false); |
| 56 | } |
| 57 | |
| 58 | @Nonnull |
| 59 | public static Opcodes forApi(int api, boolean experimental) { |
| 60 | return new Opcodes(api, VersionMap.mapApiToArtVersion(api), experimental); |
| 61 | } |
| 62 | |
| 63 | @Nonnull |
| 64 | public static Opcodes forArtVersion(int artVersion) { |
| 65 | return forArtVersion(artVersion, false); |
| 66 | } |
| 67 | |
| 68 | @Nonnull |
| 69 | public static Opcodes forArtVersion(int artVersion, boolean experimental) { |
| 70 | return new Opcodes(VersionMap.mapArtVersionToApi(artVersion), artVersion, experimental); |
| 71 | } |
| 72 | |
| 73 | @Deprecated |
Ben Gruver | 3065d6b | 2015-06-18 17:44:30 -0700 | [diff] [blame] | 74 | public Opcodes(int api) { |
| 75 | this(api, false); |
| 76 | } |
| 77 | |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 78 | @Deprecated |
Igor Murashkin | 144951a | 2015-02-18 17:38:30 -0800 | [diff] [blame] | 79 | public Opcodes(int api, boolean experimental) { |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 80 | this(api, VersionMap.mapApiToArtVersion(api), experimental); |
| 81 | } |
| 82 | |
| 83 | private Opcodes(int api, int artVersion, boolean experimental) { |
| 84 | this.api = api; |
| 85 | this.artVersion = artVersion; |
| 86 | |
| 87 | opcodeValues = new EnumMap<Opcode, Short>(Opcode.class); |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 88 | opcodesByName = Maps.newHashMap(); |
| 89 | |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 90 | int version; |
| 91 | if (isArt()) { |
| 92 | version = artVersion; |
| 93 | } else { |
| 94 | version = api; |
| 95 | } |
| 96 | |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 97 | for (Opcode opcode: Opcode.values()) { |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 98 | RangeMap<Integer, Short> versionToValueMap; |
| 99 | |
| 100 | if (isArt()) { |
| 101 | versionToValueMap = opcode.artVersionToValueMap; |
| 102 | } else { |
| 103 | versionToValueMap = opcode.apiToValueMap; |
| 104 | } |
| 105 | |
| 106 | Short opcodeValue = versionToValueMap.get(version); |
| 107 | if (opcodeValue != null && (!opcode.isExperimental() || experimental)) { |
| 108 | if (!opcode.format.isPayloadFormat) { |
| 109 | opcodesByValue[opcodeValue] = opcode; |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 110 | } |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 111 | opcodeValues.put(opcode, opcodeValue); |
| 112 | opcodesByName.put(opcode.name.toLowerCase(), opcode); |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Nullable |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 118 | public Opcode getOpcodeByName(@Nonnull String opcodeName) { |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 119 | return opcodesByName.get(opcodeName.toLowerCase()); |
| 120 | } |
| 121 | |
| 122 | @Nullable |
| 123 | public Opcode getOpcodeByValue(int opcodeValue) { |
| 124 | switch (opcodeValue) { |
| 125 | case 0x100: |
| 126 | return Opcode.PACKED_SWITCH_PAYLOAD; |
| 127 | case 0x200: |
| 128 | return Opcode.SPARSE_SWITCH_PAYLOAD; |
| 129 | case 0x300: |
| 130 | return Opcode.ARRAY_PAYLOAD; |
| 131 | default: |
Ben Gruver | d7cd523 | 2013-11-26 17:34:52 -0800 | [diff] [blame] | 132 | if (opcodeValue >= 0 && opcodeValue < opcodesByValue.length) { |
| 133 | return opcodesByValue[opcodeValue]; |
| 134 | } |
| 135 | return null; |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
Ben Gruver | 3ff884b | 2015-07-12 12:52:57 -0700 | [diff] [blame] | 138 | |
| 139 | @Nullable |
| 140 | public Short getOpcodeValue(@Nonnull Opcode opcode) { |
| 141 | return opcodeValues.get(opcode); |
| 142 | } |
| 143 | |
| 144 | public boolean isArt() { |
| 145 | return artVersion != VersionMap.NO_VERSION; |
| 146 | } |
Ben Gruver | 83f77f5 | 2013-04-17 22:32:08 -0700 | [diff] [blame] | 147 | } |