1 /* 2 * Copyright (C) 2016 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_X86_64_JNI_MACRO_ASSEMBLER_X86_64_H_ 18 #define ART_COMPILER_UTILS_X86_64_JNI_MACRO_ASSEMBLER_X86_64_H_ 19 20 #include <vector> 21 22 #include "assembler_x86_64.h" 23 #include "base/arena_containers.h" 24 #include "base/array_ref.h" 25 #include "base/macros.h" 26 #include "base/pointer_size.h" 27 #include "offsets.h" 28 #include "utils/assembler.h" 29 #include "utils/jni_macro_assembler.h" 30 31 namespace art HIDDEN { 32 namespace x86_64 { 33 34 class X86_64JNIMacroAssembler final : public JNIMacroAssemblerFwd<X86_64Assembler, 35 PointerSize::k64> { 36 public: X86_64JNIMacroAssembler(ArenaAllocator * allocator)37 explicit X86_64JNIMacroAssembler(ArenaAllocator* allocator) 38 : JNIMacroAssemblerFwd<X86_64Assembler, PointerSize::k64>(allocator) {} ~X86_64JNIMacroAssembler()39 virtual ~X86_64JNIMacroAssembler() {} 40 41 // 42 // Overridden common assembler high-level functionality 43 // 44 45 // Emit code that will create an activation on the stack 46 void BuildFrame(size_t frame_size, 47 ManagedRegister method_reg, 48 ArrayRef<const ManagedRegister> callee_save_regs) override; 49 50 // Emit code that will remove an activation from the stack 51 void RemoveFrame(size_t frame_size, 52 ArrayRef<const ManagedRegister> callee_save_regs, 53 bool may_suspend) override; 54 55 void IncreaseFrameSize(size_t adjust) override; 56 void DecreaseFrameSize(size_t adjust) override; 57 58 ManagedRegister CoreRegisterWithSize(ManagedRegister src, size_t size) override; 59 60 // Store routines 61 void Store(FrameOffset offs, ManagedRegister src, size_t size) override; 62 void Store(ManagedRegister base, MemberOffset offs, ManagedRegister src, size_t size) override; 63 void StoreRawPtr(FrameOffset dest, ManagedRegister src) override; 64 65 void StoreStackPointerToThread(ThreadOffset64 thr_offs, bool tag_sp) override; 66 67 // Load routines 68 void Load(ManagedRegister dest, FrameOffset src, size_t size) override; 69 void Load(ManagedRegister dest, ManagedRegister base, MemberOffset offs, size_t size) override; 70 71 void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset64 offs) override; 72 73 // Copying routines 74 void MoveArguments(ArrayRef<ArgumentLocation> dests, 75 ArrayRef<ArgumentLocation> srcs, 76 ArrayRef<FrameOffset> refs) override; 77 78 void Move(ManagedRegister dest, ManagedRegister src, size_t size) override; 79 80 void Move(ManagedRegister dest, size_t value) override; 81 82 // Sign extension 83 void SignExtend(ManagedRegister mreg, size_t size) override; 84 85 // Zero extension 86 void ZeroExtend(ManagedRegister mreg, size_t size) override; 87 88 // Exploit fast access in managed code to Thread::Current() 89 void GetCurrentThread(ManagedRegister dest) override; 90 void GetCurrentThread(FrameOffset dest_offset) override; 91 92 // Decode JNI transition or local `jobject`. For (weak) global `jobject`, jump to slow path. 93 void DecodeJNITransitionOrLocalJObject(ManagedRegister reg, 94 JNIMacroLabel* slow_path, 95 JNIMacroLabel* resume) override; 96 97 // Heap::VerifyObject on src. In some cases (such as a reference to this) we 98 // know that src may not be null. 99 void VerifyObject(ManagedRegister src, bool could_be_null) override; 100 void VerifyObject(FrameOffset src, bool could_be_null) override; 101 102 // Jump to address held at [base+offset] (used for tail calls). 103 void Jump(ManagedRegister base, Offset offset) override; 104 105 // Call to address held at [base+offset] 106 void Call(ManagedRegister base, Offset offset) override; 107 void CallFromThread(ThreadOffset64 offset) override; 108 109 // Generate fast-path for transition to Native. Go to `label` if any thread flag is set. 110 // The implementation can use `scratch_regs` which should be callee save core registers 111 // (already saved before this call) and must preserve all argument registers. 112 void TryToTransitionFromRunnableToNative( 113 JNIMacroLabel* label, ArrayRef<const ManagedRegister> scratch_regs) override; 114 115 // Generate fast-path for transition to Runnable. Go to `label` if any thread flag is set. 116 // The implementation can use `scratch_regs` which should be core argument registers 117 // not used as return registers and it must preserve the `return_reg` if any. 118 void TryToTransitionFromNativeToRunnable(JNIMacroLabel* label, 119 ArrayRef<const ManagedRegister> scratch_regs, 120 ManagedRegister return_reg) override; 121 122 // Generate suspend check and branch to `label` if there is a pending suspend request. 123 void SuspendCheck(JNIMacroLabel* label) override; 124 125 // Generate code to check if Thread::Current()->exception_ is non-null 126 // and branch to the `label` if it is. 127 void ExceptionPoll(JNIMacroLabel* label) override; 128 // Deliver pending exception. 129 void DeliverPendingException() override; 130 131 // Create a new label that can be used with Jump/Bind calls. 132 std::unique_ptr<JNIMacroLabel> CreateLabel() override; 133 // Emit an unconditional jump to the label. 134 void Jump(JNIMacroLabel* label) override; 135 // Emit a conditional jump to the label by applying a unary condition test to the GC marking flag. 136 void TestGcMarking(JNIMacroLabel* label, JNIMacroUnaryCondition cond) override; 137 // Emit a conditional jump to the label by applying a unary condition test to object's mark bit. 138 void TestMarkBit(ManagedRegister ref, JNIMacroLabel* label, JNIMacroUnaryCondition cond) override; 139 // Emit a conditional jump to label if the loaded value from specified locations is not zero. 140 void TestByteAndJumpIfNotZero(uintptr_t address, JNIMacroLabel* label) override; 141 // Code at this offset will serve as the target for the Jump call. 142 void Bind(JNIMacroLabel* label) override; 143 144 private: 145 void Copy(FrameOffset dest, FrameOffset src, size_t size); 146 147 // Set up `out_reg` to hold a `jobject` (`StackReference<Object>*` to a spilled value), 148 // or to be null if the value is null and `null_allowed`. `in_reg` holds a possibly 149 // stale reference that can be used to avoid loading the spilled value to 150 // see if the value is null. 151 void CreateJObject(ManagedRegister out_reg, 152 FrameOffset spilled_reference_offset, 153 ManagedRegister in_reg, 154 bool null_allowed); 155 156 // Set up `out_off` to hold a `jobject` (`StackReference<Object>*` to a spilled value), 157 // or to be null if the value is null and `null_allowed`. 158 void CreateJObject(FrameOffset out_off, 159 FrameOffset spilled_reference_offset, 160 bool null_allowed); 161 162 DISALLOW_COPY_AND_ASSIGN(X86_64JNIMacroAssembler); 163 }; 164 165 class X86_64JNIMacroLabel final 166 : public JNIMacroLabelCommon<X86_64JNIMacroLabel, 167 art::Label, 168 InstructionSet::kX86_64> { 169 public: AsX86_64()170 art::Label* AsX86_64() { 171 return AsPlatformLabel(); 172 } 173 }; 174 175 } // namespace x86_64 176 } // namespace art 177 178 #endif // ART_COMPILER_UTILS_X86_64_JNI_MACRO_ASSEMBLER_X86_64_H_ 179