1/*
2 * Copyright (C) 2015 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_COMPILER_LINKER_ARM_RELATIVE_PATCHER_THUMB2_H_
18#define ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_THUMB2_H_
19
20#include "arch/arm/registers_arm.h"
21#include "base/array_ref.h"
22#include "base/bit_field.h"
23#include "base/bit_utils.h"
24#include "linker/arm/relative_patcher_arm_base.h"
25
26namespace art {
27
28namespace arm {
29class ArmVIXLAssembler;
30}  // namespace arm
31
32namespace linker {
33
34class Thumb2RelativePatcher FINAL : public ArmBaseRelativePatcher {
35 public:
36  static constexpr uint32_t kBakerCcEntrypointRegister = 4u;
37
38  static uint32_t EncodeBakerReadBarrierFieldData(uint32_t base_reg,
39                                                  uint32_t holder_reg,
40                                                  bool narrow) {
41    CheckValidReg(base_reg);
42    CheckValidReg(holder_reg);
43    DCHECK(!narrow || base_reg < 8u) << base_reg;
44    BakerReadBarrierWidth width =
45        narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide;
46    return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kField) |
47           BakerReadBarrierFirstRegField::Encode(base_reg) |
48           BakerReadBarrierSecondRegField::Encode(holder_reg) |
49           BakerReadBarrierWidthField::Encode(width);
50  }
51
52  static uint32_t EncodeBakerReadBarrierArrayData(uint32_t base_reg) {
53    CheckValidReg(base_reg);
54    return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kArray) |
55           BakerReadBarrierFirstRegField::Encode(base_reg) |
56           BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg) |
57           BakerReadBarrierWidthField::Encode(BakerReadBarrierWidth::kWide);
58  }
59
60  static uint32_t EncodeBakerReadBarrierGcRootData(uint32_t root_reg, bool narrow) {
61    CheckValidReg(root_reg);
62    DCHECK(!narrow || root_reg < 8u) << root_reg;
63    BakerReadBarrierWidth width =
64        narrow ? BakerReadBarrierWidth::kNarrow : BakerReadBarrierWidth::kWide;
65    return BakerReadBarrierKindField::Encode(BakerReadBarrierKind::kGcRoot) |
66           BakerReadBarrierFirstRegField::Encode(root_reg) |
67           BakerReadBarrierSecondRegField::Encode(kInvalidEncodedReg) |
68           BakerReadBarrierWidthField::Encode(width);
69  }
70
71  explicit Thumb2RelativePatcher(RelativePatcherTargetProvider* provider);
72
73  void PatchCall(std::vector<uint8_t>* code,
74                 uint32_t literal_offset,
75                 uint32_t patch_offset,
76                 uint32_t target_offset) OVERRIDE;
77  void PatchPcRelativeReference(std::vector<uint8_t>* code,
78                                const LinkerPatch& patch,
79                                uint32_t patch_offset,
80                                uint32_t target_offset) OVERRIDE;
81  void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code,
82                                   const LinkerPatch& patch,
83                                   uint32_t patch_offset) OVERRIDE;
84
85 protected:
86  std::vector<uint8_t> CompileThunk(const ThunkKey& key) OVERRIDE;
87  std::string GetThunkDebugName(const ThunkKey& key) OVERRIDE;
88  uint32_t MaxPositiveDisplacement(const ThunkKey& key) OVERRIDE;
89  uint32_t MaxNegativeDisplacement(const ThunkKey& key) OVERRIDE;
90
91 private:
92  static constexpr uint32_t kInvalidEncodedReg = /* pc is invalid */ 15u;
93
94  enum class BakerReadBarrierKind : uint8_t {
95    kField,   // Field get or array get with constant offset (i.e. constant index).
96    kArray,   // Array get with index in register.
97    kGcRoot,  // GC root load.
98    kLast = kGcRoot
99  };
100
101  enum class BakerReadBarrierWidth : uint8_t {
102    kWide,          // 32-bit LDR (and 32-bit NEG if heap poisoning is enabled).
103    kNarrow,        // 16-bit LDR (and 16-bit NEG if heap poisoning is enabled).
104    kLast = kNarrow
105  };
106
107  static constexpr size_t kBitsForBakerReadBarrierKind =
108      MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierKind::kLast));
109  static constexpr size_t kBitsForRegister = 4u;
110  using BakerReadBarrierKindField =
111      BitField<BakerReadBarrierKind, 0, kBitsForBakerReadBarrierKind>;
112  using BakerReadBarrierFirstRegField =
113      BitField<uint32_t, kBitsForBakerReadBarrierKind, kBitsForRegister>;
114  using BakerReadBarrierSecondRegField =
115      BitField<uint32_t, kBitsForBakerReadBarrierKind + kBitsForRegister, kBitsForRegister>;
116  static constexpr size_t kBitsForBakerReadBarrierWidth =
117      MinimumBitsToStore(static_cast<size_t>(BakerReadBarrierWidth::kLast));
118  using BakerReadBarrierWidthField = BitField<BakerReadBarrierWidth,
119                                              kBitsForBakerReadBarrierKind + 2 * kBitsForRegister,
120                                              kBitsForBakerReadBarrierWidth>;
121
122  static void CheckValidReg(uint32_t reg) {
123    DCHECK(reg < 12u && reg != kBakerCcEntrypointRegister) << reg;
124  }
125
126  void CompileBakerReadBarrierThunk(arm::ArmVIXLAssembler& assembler, uint32_t encoded_data);
127
128  void SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value);
129  static uint32_t GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset);
130
131  template <typename Vector>
132  static uint32_t GetInsn32(Vector* code, uint32_t offset);
133
134  static uint32_t GetInsn16(ArrayRef<const uint8_t> code, uint32_t offset);
135
136  template <typename Vector>
137  static uint32_t GetInsn16(Vector* code, uint32_t offset);
138
139  friend class Thumb2RelativePatcherTest;
140
141  DISALLOW_COPY_AND_ASSIGN(Thumb2RelativePatcher);
142};
143
144}  // namespace linker
145}  // namespace art
146
147#endif  // ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_THUMB2_H_
148