blob: da4249d28dde324e0fc9159f8ce83c452c85254a [file] [log] [blame]
Mathieu Chartier12dd8a92018-06-12 11:17:22 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_
18#define ART_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_
19
Mathieu Chartier8a0ede92018-07-12 10:24:04 -070020#include <array>
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070021#include <vector>
22#include <map>
23
24#include "base/safe_map.h"
25#include "dexanalyze_experiments.h"
26#include "dex/code_item_accessors.h"
Andreas Gampe7458a7a2019-01-02 10:32:11 -080027#include "dex/dex_file_types.h"
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070028
29namespace art {
30namespace dexanalyze {
31
32enum BytecodeExperiment {
33 kExperimentInvoke,
34 kExperimentInstanceField,
35 kExperimentInstanceFieldSelf,
36 kExperimentStaticField,
37 kExperimentLocalType,
38 kExperimentReturn,
39 kExperimentSmallIf,
40 kExperimentString,
41 kExperimentSingleGetSet,
42};
43
44// Maps from global index to local index.
45struct TypeLinkage {
46 // Referenced types.
47 SafeMap<size_t, size_t> types_;
48 // Owned fields.
49 SafeMap<size_t, size_t> fields_;
50 // Owned methods.
51 SafeMap<size_t, size_t> methods_;
52 // Referenced strings.
53 SafeMap<size_t, size_t> strings_;
54};
55
Mathieu Chartierfbc80032018-07-11 11:20:08 -070056class NewRegisterInstructions : public Experiment {
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070057 public:
Mathieu Chartiercd2edfc2018-08-19 15:38:32 -070058 explicit NewRegisterInstructions(uint64_t experiments)
59 : experiments_(experiments),
60 move_result_reg_(256),
61 first_arg_reg_count_(256),
62 opcode_count_(256) {}
Mathieu Chartierfbc80032018-07-11 11:20:08 -070063 void ProcessDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
64 void Dump(std::ostream& os, uint64_t total_size) const;
65
66 void ProcessCodeItem(const DexFile& dex_file,
67 const CodeItemDataAccessor& code_item,
68 dex::TypeIndex current_class_type,
69 bool count_types,
70 std::map<size_t, TypeLinkage>& types);
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070071 void Add(Instruction::Code opcode, const Instruction& inst);
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070072 bool InstNibbles(uint8_t opcode, const std::vector<uint32_t>& args);
Mathieu Chartiercd2edfc2018-08-19 15:38:32 -070073 bool ExtendPrefix(uint32_t* value1, uint32_t* value2);
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070074 bool Enabled(BytecodeExperiment experiment) const {
75 return experiments_ & (1u << static_cast<uint64_t>(experiment));
76 }
77
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070078 private:
Mathieu Chartierfbc80032018-07-11 11:20:08 -070079 size_t alignment_ = 1u;
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070080 uint64_t output_size_ = 0u;
81 uint64_t deduped_size_ = 0u;
82 uint64_t dex_code_bytes_ = 0u;
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070083 uint64_t experiments_ = std::numeric_limits<uint64_t>::max();
Mathieu Chartiercd2edfc2018-08-19 15:38:32 -070084 uint64_t extended_field_ = 0u;
85 uint64_t extended_method_ = 0u;
86 std::vector<size_t> move_result_reg_;
87 std::vector<size_t> first_arg_reg_count_;
88 std::vector<size_t> opcode_count_;
89 std::map<std::pair<uint32_t, uint32_t>, size_t> method_linkage_counts_;
90 std::map<std::pair<uint32_t, uint32_t>, size_t> field_linkage_counts_;
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070091 std::map<std::vector<uint8_t>, size_t> instruction_freq_;
Mathieu Chartierfbc80032018-07-11 11:20:08 -070092 // Output instruction buffer.
93 std::vector<uint8_t> buffer_;
Mathieu Chartier12dd8a92018-06-12 11:17:22 -070094};
95
96} // namespace dexanalyze
97} // namespace art
98
99#endif // ART_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_