Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_UTILS_JNI_MACRO_ASSEMBLER_H_ |
| 18 | #define ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
| 25 | #include "base/arena_allocator.h" |
| 26 | #include "base/arena_object.h" |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 27 | #include "base/array_ref.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 28 | #include "base/enums.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 29 | #include "base/macros.h" |
| 30 | #include "managed_register.h" |
| 31 | #include "offsets.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | class ArenaAllocator; |
| 36 | class DebugFrameOpCodeWriterForAssembler; |
| 37 | class InstructionSetFeatures; |
| 38 | class MemoryRegion; |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 39 | class JNIMacroLabel; |
| 40 | |
| 41 | enum class JNIMacroUnaryCondition { |
| 42 | kZero, |
| 43 | kNotZero |
| 44 | }; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 45 | |
Vladimir Marko | 0300822 | 2020-03-06 14:04:21 +0000 | [diff] [blame] | 46 | class ArgumentLocation { |
| 47 | public: |
| 48 | ArgumentLocation(ManagedRegister reg, size_t size) |
| 49 | : reg_(reg), frame_offset_(0u), size_(size) { |
| 50 | DCHECK(reg.IsRegister()); |
| 51 | } |
| 52 | |
| 53 | ArgumentLocation(FrameOffset frame_offset, size_t size) |
| 54 | : reg_(ManagedRegister::NoRegister()), frame_offset_(frame_offset), size_(size) {} |
| 55 | |
| 56 | bool IsRegister() const { |
| 57 | return reg_.IsRegister(); |
| 58 | } |
| 59 | |
| 60 | ManagedRegister GetRegister() const { |
| 61 | DCHECK(IsRegister()); |
| 62 | return reg_; |
| 63 | } |
| 64 | |
| 65 | FrameOffset GetFrameOffset() const { |
| 66 | DCHECK(!IsRegister()); |
| 67 | return frame_offset_; |
| 68 | } |
| 69 | |
| 70 | size_t GetSize() const { |
| 71 | return size_; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | ManagedRegister reg_; |
| 76 | FrameOffset frame_offset_; |
| 77 | size_t size_; |
| 78 | }; |
| 79 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 80 | template <PointerSize kPointerSize> |
| 81 | class JNIMacroAssembler : public DeletableArenaObject<kArenaAllocAssembler> { |
| 82 | public: |
| 83 | static std::unique_ptr<JNIMacroAssembler<kPointerSize>> Create( |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 84 | ArenaAllocator* allocator, |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 85 | InstructionSet instruction_set, |
| 86 | const InstructionSetFeatures* instruction_set_features = nullptr); |
| 87 | |
| 88 | // Finalize the code; emit slow paths, fixup branches, add literal pool, etc. |
| 89 | virtual void FinalizeCode() = 0; |
| 90 | |
| 91 | // Size of generated code |
| 92 | virtual size_t CodeSize() const = 0; |
| 93 | |
| 94 | // Copy instructions out of assembly buffer into the given region of memory |
| 95 | virtual void FinalizeInstructions(const MemoryRegion& region) = 0; |
| 96 | |
| 97 | // Emit code that will create an activation on the stack |
| 98 | virtual void BuildFrame(size_t frame_size, |
| 99 | ManagedRegister method_reg, |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 100 | ArrayRef<const ManagedRegister> callee_save_regs) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 101 | |
| 102 | // Emit code that will remove an activation from the stack |
Roland Levillain | 0d127e1 | 2017-07-05 17:01:11 +0100 | [diff] [blame] | 103 | // |
| 104 | // Argument `may_suspend` must be `true` if the compiled method may be |
| 105 | // suspended during its execution (otherwise `false`, if it is impossible |
| 106 | // to suspend during its execution). |
| 107 | virtual void RemoveFrame(size_t frame_size, |
| 108 | ArrayRef<const ManagedRegister> callee_save_regs, |
| 109 | bool may_suspend) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 110 | |
| 111 | virtual void IncreaseFrameSize(size_t adjust) = 0; |
| 112 | virtual void DecreaseFrameSize(size_t adjust) = 0; |
| 113 | |
| 114 | // Store routines |
| 115 | virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0; |
| 116 | virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0; |
| 117 | virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0; |
| 118 | |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 119 | virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 120 | |
| 121 | virtual void StoreStackOffsetToThread(ThreadOffset<kPointerSize> thr_offs, |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 122 | FrameOffset fr_offs) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 123 | |
| 124 | virtual void StoreStackPointerToThread(ThreadOffset<kPointerSize> thr_offs) = 0; |
| 125 | |
| 126 | virtual void StoreSpanning(FrameOffset dest, |
| 127 | ManagedRegister src, |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 128 | FrameOffset in_off) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 129 | |
| 130 | // Load routines |
| 131 | virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0; |
| 132 | |
| 133 | virtual void LoadFromThread(ManagedRegister dest, |
| 134 | ThreadOffset<kPointerSize> src, |
| 135 | size_t size) = 0; |
| 136 | |
| 137 | virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0; |
| 138 | // If unpoison_reference is true and kPoisonReference is true, then we negate the read reference. |
| 139 | virtual void LoadRef(ManagedRegister dest, |
| 140 | ManagedRegister base, |
| 141 | MemberOffset offs, |
| 142 | bool unpoison_reference) = 0; |
| 143 | |
| 144 | virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) = 0; |
| 145 | |
| 146 | virtual void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset<kPointerSize> offs) = 0; |
| 147 | |
| 148 | // Copying routines |
Vladimir Marko | 0300822 | 2020-03-06 14:04:21 +0000 | [diff] [blame] | 149 | virtual void MoveArguments(ArrayRef<ArgumentLocation> dests, ArrayRef<ArgumentLocation> srcs) = 0; |
| 150 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 151 | virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0; |
| 152 | |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 153 | virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset<kPointerSize> thr_offs) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 154 | |
| 155 | virtual void CopyRawPtrToThread(ThreadOffset<kPointerSize> thr_offs, |
| 156 | FrameOffset fr_offs, |
| 157 | ManagedRegister scratch) = 0; |
| 158 | |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 159 | virtual void CopyRef(FrameOffset dest, FrameOffset src) = 0; |
| 160 | virtual void CopyRef(FrameOffset dest, |
| 161 | ManagedRegister base, |
| 162 | MemberOffset offs, |
| 163 | bool unpoison_reference) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 164 | |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 165 | virtual void Copy(FrameOffset dest, FrameOffset src, size_t size) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 166 | |
| 167 | virtual void Copy(FrameOffset dest, |
| 168 | ManagedRegister src_base, |
| 169 | Offset src_offset, |
| 170 | ManagedRegister scratch, |
| 171 | size_t size) = 0; |
| 172 | |
| 173 | virtual void Copy(ManagedRegister dest_base, |
| 174 | Offset dest_offset, |
| 175 | FrameOffset src, |
| 176 | ManagedRegister scratch, |
| 177 | size_t size) = 0; |
| 178 | |
| 179 | virtual void Copy(FrameOffset dest, |
| 180 | FrameOffset src_base, |
| 181 | Offset src_offset, |
| 182 | ManagedRegister scratch, |
| 183 | size_t size) = 0; |
| 184 | |
| 185 | virtual void Copy(ManagedRegister dest, |
| 186 | Offset dest_offset, |
| 187 | ManagedRegister src, |
| 188 | Offset src_offset, |
| 189 | ManagedRegister scratch, |
| 190 | size_t size) = 0; |
| 191 | |
| 192 | virtual void Copy(FrameOffset dest, |
| 193 | Offset dest_offset, |
| 194 | FrameOffset src, |
| 195 | Offset src_offset, |
| 196 | ManagedRegister scratch, |
| 197 | size_t size) = 0; |
| 198 | |
| 199 | virtual void MemoryBarrier(ManagedRegister scratch) = 0; |
| 200 | |
| 201 | // Sign extension |
| 202 | virtual void SignExtend(ManagedRegister mreg, size_t size) = 0; |
| 203 | |
| 204 | // Zero extension |
| 205 | virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0; |
| 206 | |
| 207 | // Exploit fast access in managed code to Thread::Current() |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 208 | virtual void GetCurrentThread(ManagedRegister dest) = 0; |
| 209 | virtual void GetCurrentThread(FrameOffset dest_offset) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 210 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 211 | // Set up `out_reg` to hold a `jobject` (`StackReference<Object>*` to a spilled value), |
| 212 | // or to be null if the value is null and `null_allowed`. `in_reg` holds a possibly |
| 213 | // stale reference that can be used to avoid loading the spilled value to |
| 214 | // see if the value is null. |
| 215 | virtual void CreateJObject(ManagedRegister out_reg, |
| 216 | FrameOffset spilled_reference_offset, |
| 217 | ManagedRegister in_reg, |
| 218 | bool null_allowed) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 219 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 220 | // Set up `out_off` to hold a `jobject` (`StackReference<Object>*` to a spilled value), |
| 221 | // or to be null if the value is null and `null_allowed`. |
| 222 | virtual void CreateJObject(FrameOffset out_off, |
| 223 | FrameOffset spilled_reference_offset, |
| 224 | bool null_allowed) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 225 | |
| 226 | // Heap::VerifyObject on src. In some cases (such as a reference to this) we |
| 227 | // know that src may not be null. |
| 228 | virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0; |
| 229 | virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0; |
| 230 | |
Vladimir Marko | 1c3c106 | 2019-12-03 11:18:44 +0000 | [diff] [blame] | 231 | // Jump to address held at [base+offset] (used for tail calls). |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 232 | virtual void Jump(ManagedRegister base, Offset offset) = 0; |
Vladimir Marko | 1c3c106 | 2019-12-03 11:18:44 +0000 | [diff] [blame] | 233 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 234 | // Call to address held at [base+offset] |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 235 | virtual void Call(ManagedRegister base, Offset offset) = 0; |
| 236 | virtual void Call(FrameOffset base, Offset offset) = 0; |
| 237 | virtual void CallFromThread(ThreadOffset<kPointerSize> offset) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 238 | |
| 239 | // Generate code to check if Thread::Current()->exception_ is non-null |
| 240 | // and branch to a ExceptionSlowPath if it is. |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 241 | virtual void ExceptionPoll(size_t stack_adjust) = 0; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 242 | |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 243 | // Create a new label that can be used with Jump/Bind calls. |
| 244 | virtual std::unique_ptr<JNIMacroLabel> CreateLabel() = 0; |
| 245 | // Emit an unconditional jump to the label. |
| 246 | virtual void Jump(JNIMacroLabel* label) = 0; |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 247 | // Emit a conditional jump to the label by applying a unary condition test to the GC marking flag. |
| 248 | virtual void TestGcMarking(JNIMacroLabel* label, JNIMacroUnaryCondition cond) = 0; |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 249 | // Code at this offset will serve as the target for the Jump call. |
| 250 | virtual void Bind(JNIMacroLabel* label) = 0; |
| 251 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 252 | virtual ~JNIMacroAssembler() {} |
| 253 | |
| 254 | /** |
| 255 | * @brief Buffer of DWARF's Call Frame Information opcodes. |
| 256 | * @details It is used by debuggers and other tools to unwind the call stack. |
| 257 | */ |
| 258 | virtual DebugFrameOpCodeWriterForAssembler& cfi() = 0; |
| 259 | |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 260 | void SetEmitRunTimeChecksInDebugMode(bool value) { |
| 261 | emit_run_time_checks_in_debug_mode_ = value; |
| 262 | } |
| 263 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 264 | protected: |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 265 | JNIMacroAssembler() {} |
| 266 | |
| 267 | // Should run-time checks be emitted in debug mode? |
| 268 | bool emit_run_time_checks_in_debug_mode_ = false; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 271 | // A "Label" class used with the JNIMacroAssembler |
| 272 | // allowing one to use branches (jumping from one place to another). |
| 273 | // |
| 274 | // This is just an interface, so every platform must provide |
| 275 | // its own implementation of it. |
| 276 | // |
| 277 | // It is only safe to use a label created |
| 278 | // via JNIMacroAssembler::CreateLabel with that same macro assembler. |
| 279 | class JNIMacroLabel { |
| 280 | public: |
| 281 | virtual ~JNIMacroLabel() = 0; |
| 282 | |
| 283 | const InstructionSet isa_; |
| 284 | protected: |
| 285 | explicit JNIMacroLabel(InstructionSet isa) : isa_(isa) {} |
| 286 | }; |
| 287 | |
| 288 | inline JNIMacroLabel::~JNIMacroLabel() { |
| 289 | // Compulsory definition for a pure virtual destructor |
| 290 | // to avoid linking errors. |
| 291 | } |
| 292 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 293 | template <typename T, PointerSize kPointerSize> |
| 294 | class JNIMacroAssemblerFwd : public JNIMacroAssembler<kPointerSize> { |
| 295 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 296 | void FinalizeCode() override { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 297 | asm_.FinalizeCode(); |
| 298 | } |
| 299 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 300 | size_t CodeSize() const override { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 301 | return asm_.CodeSize(); |
| 302 | } |
| 303 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 304 | void FinalizeInstructions(const MemoryRegion& region) override { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 305 | asm_.FinalizeInstructions(region); |
| 306 | } |
| 307 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 308 | DebugFrameOpCodeWriterForAssembler& cfi() override { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 309 | return asm_.cfi(); |
| 310 | } |
| 311 | |
| 312 | protected: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 313 | explicit JNIMacroAssemblerFwd(ArenaAllocator* allocator) : asm_(allocator) {} |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 314 | |
| 315 | T asm_; |
| 316 | }; |
| 317 | |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 318 | template <typename Self, typename PlatformLabel, InstructionSet kIsa> |
| 319 | class JNIMacroLabelCommon : public JNIMacroLabel { |
| 320 | public: |
| 321 | static Self* Cast(JNIMacroLabel* label) { |
| 322 | CHECK(label != nullptr); |
| 323 | CHECK_EQ(kIsa, label->isa_); |
| 324 | |
| 325 | return reinterpret_cast<Self*>(label); |
| 326 | } |
| 327 | |
| 328 | protected: |
| 329 | PlatformLabel* AsPlatformLabel() { |
| 330 | return &label_; |
| 331 | } |
| 332 | |
| 333 | JNIMacroLabelCommon() : JNIMacroLabel(kIsa) { |
| 334 | } |
| 335 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 336 | ~JNIMacroLabelCommon() override {} |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 337 | |
| 338 | private: |
| 339 | PlatformLabel label_; |
| 340 | }; |
| 341 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 342 | } // namespace art |
| 343 | |
| 344 | #endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_ |