Move some remaining dex utilities

There were several utilities related to building/walking/testing dex
files that were not in libdexfile.  This change consolidates these.

Bug: 22322814
Test: make -j 50 test-art-host
Change-Id: Id76e9179d03b8ec7d67f7e0f267121f54f0ec2e0
diff --git a/runtime/aot_class_linker.cc b/runtime/aot_class_linker.cc
index 93e02ef..c810b41 100644
--- a/runtime/aot_class_linker.cc
+++ b/runtime/aot_class_linker.cc
@@ -16,9 +16,9 @@
 
 #include "aot_class_linker.h"
 
-#include "class_reference.h"
 #include "class_status.h"
 #include "compiler_callbacks.h"
+#include "dex/class_reference.h"
 #include "handle_scope-inl.h"
 #include "mirror/class-inl.h"
 #include "runtime.h"
diff --git a/runtime/bytecode_utils.h b/runtime/bytecode_utils.h
deleted file mode 100644
index a7e0abf..0000000
--- a/runtime/bytecode_utils.h
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#ifndef ART_RUNTIME_BYTECODE_UTILS_H_
-#define ART_RUNTIME_BYTECODE_UTILS_H_
-
-#include "base/arena_object.h"
-#include "dex/dex_file-inl.h"
-#include "dex/dex_file.h"
-#include "dex/dex_instruction-inl.h"
-
-namespace art {
-
-class DexSwitchTable : public ValueObject {
- public:
-  DexSwitchTable(const Instruction& instruction, uint32_t dex_pc)
-      : instruction_(instruction),
-        dex_pc_(dex_pc),
-        sparse_(instruction.Opcode() == Instruction::SPARSE_SWITCH) {
-    int32_t table_offset = instruction.VRegB_31t();
-    const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
-    DCHECK_EQ(table[0], sparse_ ? static_cast<uint16_t>(Instruction::kSparseSwitchSignature)
-                                : static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
-    num_entries_ = table[1];
-    values_ = reinterpret_cast<const int32_t*>(&table[2]);
-  }
-
-  uint16_t GetNumEntries() const {
-    return num_entries_;
-  }
-
-  void CheckIndex(size_t index) const {
-    if (sparse_) {
-      // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
-      DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
-    } else {
-      // In a packed table, we have the starting key and num_entries_ values.
-      DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
-    }
-  }
-
-  int32_t GetEntryAt(size_t index) const {
-    CheckIndex(index);
-    return values_[index];
-  }
-
-  uint32_t GetDexPcForIndex(size_t index) const {
-    CheckIndex(index);
-    return dex_pc_ +
-        (reinterpret_cast<const int16_t*>(values_ + index) -
-         reinterpret_cast<const int16_t*>(&instruction_));
-  }
-
-  // Index of the first value in the table.
-  size_t GetFirstValueIndex() const {
-    if (sparse_) {
-      // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
-      return num_entries_;
-    } else {
-      // In a packed table, we have the starting key and num_entries_ values.
-      return 1;
-    }
-  }
-
-  bool IsSparse() const { return sparse_; }
-
-  bool ShouldBuildDecisionTree() {
-    return IsSparse() || GetNumEntries() <= kSmallSwitchThreshold;
-  }
-
- private:
-  const Instruction& instruction_;
-  const uint32_t dex_pc_;
-
-  // Whether this is a sparse-switch table (or a packed-switch one).
-  const bool sparse_;
-
-  // This can't be const as it needs to be computed off of the given instruction, and complicated
-  // expressions in the initializer list seemed very ugly.
-  uint16_t num_entries_;
-
-  const int32_t* values_;
-
-  // The number of entries in a packed switch before we use a jump table or specified
-  // compare/jump series.
-  static constexpr uint16_t kSmallSwitchThreshold = 3;
-
-  DISALLOW_COPY_AND_ASSIGN(DexSwitchTable);
-};
-
-class DexSwitchTableIterator {
- public:
-  explicit DexSwitchTableIterator(const DexSwitchTable& table)
-      : table_(table),
-        num_entries_(static_cast<size_t>(table_.GetNumEntries())),
-        first_target_offset_(table_.GetFirstValueIndex()),
-        index_(0u) {}
-
-  bool Done() const { return index_ >= num_entries_; }
-  bool IsLast() const { return index_ == num_entries_ - 1; }
-
-  void Advance() {
-    DCHECK(!Done());
-    index_++;
-  }
-
-  int32_t CurrentKey() const {
-    return table_.IsSparse() ? table_.GetEntryAt(index_) : table_.GetEntryAt(0) + index_;
-  }
-
-  int32_t CurrentTargetOffset() const {
-    return table_.GetEntryAt(index_ + first_target_offset_);
-  }
-
-  uint32_t GetDexPcForCurrentIndex() const { return table_.GetDexPcForIndex(index_); }
-
- private:
-  const DexSwitchTable& table_;
-  const size_t num_entries_;
-  const size_t first_target_offset_;
-
-  size_t index_;
-};
-
-inline bool IsThrowingDexInstruction(const Instruction& instruction) {
-  // Special-case MONITOR_EXIT which is a throwing instruction but the verifier
-  // guarantees that it will never throw. This is necessary to avoid rejecting
-  // 'synchronized' blocks/methods.
-  return instruction.IsThrow() && instruction.Opcode() != Instruction::MONITOR_EXIT;
-}
-
-}  // namespace art
-
-#endif  // ART_RUNTIME_BYTECODE_UTILS_H_
diff --git a/runtime/class_reference.h b/runtime/class_reference.h
deleted file mode 100644
index e8e668e..0000000
--- a/runtime/class_reference.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#ifndef ART_RUNTIME_CLASS_REFERENCE_H_
-#define ART_RUNTIME_CLASS_REFERENCE_H_
-
-#include <stdint.h>
-#include <utility>
-
-#include "dex/dex_file_reference.h"
-
-namespace art {
-
-class DexFile;
-
-// A class is uniquely located by its DexFile and the class_defs_ table index into that DexFile
-class ClassReference : public DexFileReference {
- public:
-  ClassReference(const DexFile* file, uint32_t class_def_idx)
-     : DexFileReference(file, class_def_idx) {}
-
-  uint32_t ClassDefIdx() const {
-    return index;
-  }
-};
-
-}  // namespace art
-
-#endif  // ART_RUNTIME_CLASS_REFERENCE_H_
diff --git a/runtime/compiler_callbacks.h b/runtime/compiler_callbacks.h
index 8395966..6855dcd 100644
--- a/runtime/compiler_callbacks.h
+++ b/runtime/compiler_callbacks.h
@@ -18,7 +18,7 @@
 #define ART_RUNTIME_COMPILER_CALLBACKS_H_
 
 #include "base/mutex.h"
-#include "class_reference.h"
+#include "dex/class_reference.h"
 #include "class_status.h"
 
 namespace art {
diff --git a/runtime/dex_to_dex_decompiler.cc b/runtime/dex_to_dex_decompiler.cc
index 7887191..a5248e6 100644
--- a/runtime/dex_to_dex_decompiler.cc
+++ b/runtime/dex_to_dex_decompiler.cc
@@ -20,7 +20,7 @@
 
 #include "base/macros.h"
 #include "base/mutex.h"
-#include "bytecode_utils.h"
+#include "dex/bytecode_utils.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_file-inl.h"
 #include "dex/dex_instruction-inl.h"
diff --git a/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc b/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
index b335872..7a0850d 100644
--- a/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
+++ b/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
@@ -23,6 +23,7 @@
 #include "dex/dex_file-inl.h"
 #include "dex/dex_file_types.h"
 #include "dex/dex_instruction-inl.h"
+#include "dex/method_reference.h"
 #include "entrypoints/entrypoint_utils-inl.h"
 #include "entrypoints/runtime_asm_entrypoints.h"
 #include "gc/accounting/card_table-inl.h"
@@ -34,7 +35,6 @@
 #include "jit/jit.h"
 #include "linear_alloc.h"
 #include "method_handles.h"
-#include "method_reference.h"
 #include "mirror/class-inl.h"
 #include "mirror/dex_cache-inl.h"
 #include "mirror/method.h"
diff --git a/runtime/jit/jit_code_cache.h b/runtime/jit/jit_code_cache.h
index 16335c6..dfa7ac0 100644
--- a/runtime/jit/jit_code_cache.h
+++ b/runtime/jit/jit_code_cache.h
@@ -25,8 +25,8 @@
 #include "base/macros.h"
 #include "base/mutex.h"
 #include "base/safe_map.h"
+#include "dex/method_reference.h"
 #include "gc_root.h"
-#include "method_reference.h"
 
 namespace art {
 
diff --git a/runtime/jit/profile_compilation_info.h b/runtime/jit/profile_compilation_info.h
index a0f6bf8..6c56db9 100644
--- a/runtime/jit/profile_compilation_info.h
+++ b/runtime/jit/profile_compilation_info.h
@@ -28,9 +28,9 @@
 #include "dex/dex_cache_resolved_classes.h"
 #include "dex/dex_file.h"
 #include "dex/dex_file_types.h"
-#include "method_reference.h"
+#include "dex/method_reference.h"
+#include "dex/type_reference.h"
 #include "mem_map.h"
-#include "type_reference.h"
 
 namespace art {
 
diff --git a/runtime/jit/profile_compilation_info_test.cc b/runtime/jit/profile_compilation_info_test.cc
index e691795..4e3774e 100644
--- a/runtime/jit/profile_compilation_info_test.cc
+++ b/runtime/jit/profile_compilation_info_test.cc
@@ -23,14 +23,14 @@
 #include "common_runtime_test.h"
 #include "dex/dex_file.h"
 #include "dex/dex_file_loader.h"
+#include "dex/method_reference.h"
+#include "dex/type_reference.h"
 #include "handle_scope-inl.h"
 #include "jit/profile_compilation_info.h"
 #include "linear_alloc.h"
-#include "method_reference.h"
 #include "mirror/class-inl.h"
 #include "mirror/class_loader.h"
 #include "scoped_thread_state_change-inl.h"
-#include "type_reference.h"
 #include "ziparchive/zip_writer.h"
 
 namespace art {
diff --git a/runtime/jit/profile_saver.h b/runtime/jit/profile_saver.h
index e5cd11b..afbb3c1 100644
--- a/runtime/jit/profile_saver.h
+++ b/runtime/jit/profile_saver.h
@@ -19,8 +19,8 @@
 
 #include "base/mutex.h"
 #include "base/safe_map.h"
+#include "dex/method_reference.h"
 #include "jit_code_cache.h"
-#include "method_reference.h"
 #include "profile_compilation_info.h"
 #include "profile_saver_options.h"
 
diff --git a/runtime/method_reference.h b/runtime/method_reference.h
deleted file mode 100644
index 50b6d6e..0000000
--- a/runtime/method_reference.h
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#ifndef ART_RUNTIME_METHOD_REFERENCE_H_
-#define ART_RUNTIME_METHOD_REFERENCE_H_
-
-#include <stdint.h>
-#include <string>
-#include "dex/dex_file.h"
-#include "dex/dex_file_reference.h"
-
-namespace art {
-
-// A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
-class MethodReference : public DexFileReference {
- public:
-  MethodReference(const DexFile* file, uint32_t index) : DexFileReference(file, index) {}
-  std::string PrettyMethod(bool with_signature = true) const {
-    return dex_file->PrettyMethod(index, with_signature);
-  }
-  const DexFile::MethodId& GetMethodId() const {
-    return dex_file->GetMethodId(index);
-  }
-};
-
-// Compare the actual referenced method signatures. Used for method reference deduplication.
-struct MethodReferenceValueComparator {
-  bool operator()(MethodReference mr1, MethodReference mr2) const {
-    if (mr1.dex_file == mr2.dex_file) {
-      DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2));
-      return mr1.index < mr2.index;
-    } else {
-      return SlowCompare(mr1, mr2);
-    }
-  }
-
-  bool SlowCompare(MethodReference mr1, MethodReference mr2) const {
-    // The order is the same as for method ids in a single dex file.
-    // Compare the class descriptors first.
-    const DexFile::MethodId& mid1 = mr1.GetMethodId();
-    const DexFile::MethodId& mid2 = mr2.GetMethodId();
-    int descriptor_diff = strcmp(mr1.dex_file->StringByTypeIdx(mid1.class_idx_),
-                                 mr2.dex_file->StringByTypeIdx(mid2.class_idx_));
-    if (descriptor_diff != 0) {
-      return descriptor_diff < 0;
-    }
-    // Compare names second.
-    int name_diff = strcmp(mr1.dex_file->GetMethodName(mid1), mr2.dex_file->GetMethodName(mid2));
-    if (name_diff != 0) {
-      return name_diff < 0;
-    }
-    // And then compare proto ids, starting with return type comparison.
-    const DexFile::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_);
-    const DexFile::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_);
-    int return_type_diff = strcmp(mr1.dex_file->StringByTypeIdx(prid1.return_type_idx_),
-                                  mr2.dex_file->StringByTypeIdx(prid2.return_type_idx_));
-    if (return_type_diff != 0) {
-      return return_type_diff < 0;
-    }
-    // And finishing with lexicographical parameter comparison.
-    const DexFile::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1);
-    size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
-    const DexFile::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2);
-    size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u;
-    for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) {
-      int param_diff = strcmp(mr1.dex_file->StringByTypeIdx(params1->GetTypeItem(i).type_idx_),
-                              mr2.dex_file->StringByTypeIdx(params2->GetTypeItem(i).type_idx_));
-      if (param_diff != 0) {
-        return param_diff < 0;
-      }
-    }
-    return param1_size < param2_size;
-  }
-};
-
-}  // namespace art
-
-#endif  // ART_RUNTIME_METHOD_REFERENCE_H_
diff --git a/runtime/runtime_callbacks_test.cc b/runtime/runtime_callbacks_test.cc
index 0b69851..5603526 100644
--- a/runtime/runtime_callbacks_test.cc
+++ b/runtime/runtime_callbacks_test.cc
@@ -30,8 +30,8 @@
 #include "art_method-inl.h"
 #include "base/mutex.h"
 #include "class_linker.h"
-#include "class_reference.h"
 #include "common_runtime_test.h"
+#include "dex/class_reference.h"
 #include "handle.h"
 #include "handle_scope-inl.h"
 #include "mem_map.h"
diff --git a/runtime/string_reference.h b/runtime/string_reference.h
deleted file mode 100644
index 1ee5d6d..0000000
--- a/runtime/string_reference.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#ifndef ART_RUNTIME_STRING_REFERENCE_H_
-#define ART_RUNTIME_STRING_REFERENCE_H_
-
-#include <stdint.h>
-
-#include <android-base/logging.h>
-
-#include "dex/dex_file-inl.h"
-#include "dex/dex_file_reference.h"
-#include "dex/dex_file_types.h"
-#include "dex/utf-inl.h"
-
-namespace art {
-
-// A string is located by its DexFile and the string_ids_ table index into that DexFile.
-class StringReference : public DexFileReference {
- public:
-  StringReference(const DexFile* file, dex::StringIndex index)
-     : DexFileReference(file, index.index_) {}
-
-  dex::StringIndex StringIndex() const {
-    return dex::StringIndex(index);
-  }
-
-  const char* GetStringData() const {
-    return dex_file->GetStringData(dex_file->GetStringId(StringIndex()));
-  }
-};
-
-// Compare the actual referenced string values. Used for string reference deduplication.
-struct StringReferenceValueComparator {
-  bool operator()(const StringReference& sr1, const StringReference& sr2) const {
-    // Note that we want to deduplicate identical strings even if they are referenced
-    // by different dex files, so we need some (any) total ordering of strings, rather
-    // than references. However, the references should usually be from the same dex file,
-    // so we choose the dex file string ordering so that we can simply compare indexes
-    // and avoid the costly string comparison in the most common case.
-    if (sr1.dex_file == sr2.dex_file) {
-      // Use the string order enforced by the dex file verifier.
-      DCHECK_EQ(
-          sr1.index < sr2.index,
-          CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
-                                                                  sr2.GetStringData()) < 0);
-      return sr1.index < sr2.index;
-    } else {
-      // Cannot compare indexes, so do the string comparison.
-      return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(sr1.GetStringData(),
-                                                                     sr2.GetStringData()) < 0;
-    }
-  }
-};
-
-}  // namespace art
-
-#endif  // ART_RUNTIME_STRING_REFERENCE_H_
diff --git a/runtime/type_reference.h b/runtime/type_reference.h
deleted file mode 100644
index 2b0b99f..0000000
--- a/runtime/type_reference.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#ifndef ART_RUNTIME_TYPE_REFERENCE_H_
-#define ART_RUNTIME_TYPE_REFERENCE_H_
-
-#include <stdint.h>
-
-#include <android-base/logging.h>
-
-#include "dex/dex_file_types.h"
-#include "string_reference.h"
-
-namespace art {
-
-class DexFile;
-
-// A type is located by its DexFile and the string_ids_ table index into that DexFile.
-class TypeReference : public DexFileReference {
- public:
-  TypeReference(const DexFile* file, dex::TypeIndex index)
-      : DexFileReference(file, index.index_) {}
-
-  dex::TypeIndex TypeIndex() const {
-    return dex::TypeIndex(index);
-  }
-};
-
-// Compare the actual referenced type names. Used for type reference deduplication.
-struct TypeReferenceValueComparator {
-  bool operator()(const TypeReference& tr1, const TypeReference& tr2) const {
-    // Note that we want to deduplicate identical boot image types even if they are
-    // referenced by different dex files, so we simply compare the descriptors.
-    StringReference sr1(tr1.dex_file, tr1.dex_file->GetTypeId(tr1.TypeIndex()).descriptor_idx_);
-    StringReference sr2(tr2.dex_file, tr2.dex_file->GetTypeId(tr2.TypeIndex()).descriptor_idx_);
-    return StringReferenceValueComparator()(sr1, sr2);
-  }
-};
-
-}  // namespace art
-
-#endif  // ART_RUNTIME_TYPE_REFERENCE_H_
diff --git a/runtime/verifier/method_verifier.h b/runtime/verifier/method_verifier.h
index 4c9518b..9237a8b 100644
--- a/runtime/verifier/method_verifier.h
+++ b/runtime/verifier/method_verifier.h
@@ -28,9 +28,9 @@
 #include "dex/code_item_accessors.h"
 #include "dex/dex_file.h"
 #include "dex/dex_file_types.h"
+#include "dex/method_reference.h"
 #include "handle.h"
 #include "instruction_flags.h"
-#include "method_reference.h"
 #include "reg_type_cache.h"
 #include "register_line.h"
 #include "verifier_enums.h"