Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include "callee_save_frame.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 18 | #include "common_throws.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 19 | #include "dex_file-inl.h" |
| 20 | #include "dex_instruction-inl.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 21 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 23 | #include "interpreter/interpreter.h" |
| 24 | #include "invoke_arg_array_builder.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 25 | #include "mirror/art_method-inl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/object-inl.h" |
| 28 | #include "mirror/object_array-inl.h" |
| 29 | #include "object_utils.h" |
| 30 | #include "runtime.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | // Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame. |
| 35 | class QuickArgumentVisitor { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 36 | // Size of each spilled GPR. |
| 37 | #ifdef __LP64__ |
| 38 | static constexpr size_t kBytesPerGprSpillLocation = 8; |
| 39 | #else |
| 40 | static constexpr size_t kBytesPerGprSpillLocation = 4; |
| 41 | #endif |
| 42 | // Number of bytes for each out register in the caller method's frame. |
| 43 | static constexpr size_t kBytesStackArgLocation = 4; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 44 | #if defined(__arm__) |
| 45 | // The callee save frame is pointed to by SP. |
| 46 | // | argN | | |
| 47 | // | ... | | |
| 48 | // | arg4 | | |
| 49 | // | arg3 spill | | Caller's frame |
| 50 | // | arg2 spill | | |
| 51 | // | arg1 spill | | |
| 52 | // | Method* | --- |
| 53 | // | LR | |
| 54 | // | ... | callee saves |
| 55 | // | R3 | arg3 |
| 56 | // | R2 | arg2 |
| 57 | // | R1 | arg1 |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 58 | // | R0 | padding |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 59 | // | Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 60 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 61 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 62 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 63 | static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes. |
| 64 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 65 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg. |
| 66 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address. |
| 67 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size. |
| 68 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 69 | return gpr_index * kBytesPerGprSpillLocation; |
| 70 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 71 | #elif defined(__mips__) |
| 72 | // The callee save frame is pointed to by SP. |
| 73 | // | argN | | |
| 74 | // | ... | | |
| 75 | // | arg4 | | |
| 76 | // | arg3 spill | | Caller's frame |
| 77 | // | arg2 spill | | |
| 78 | // | arg1 spill | | |
| 79 | // | Method* | --- |
| 80 | // | RA | |
| 81 | // | ... | callee saves |
| 82 | // | A3 | arg3 |
| 83 | // | A2 | arg2 |
| 84 | // | A1 | arg1 |
| 85 | // | A0/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 86 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 87 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 88 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 89 | static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes. |
| 90 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 91 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. |
| 92 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address. |
| 93 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size. |
| 94 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 95 | return gpr_index * kBytesPerGprSpillLocation; |
| 96 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 97 | #elif defined(__i386__) |
| 98 | // The callee save frame is pointed to by SP. |
| 99 | // | argN | | |
| 100 | // | ... | | |
| 101 | // | arg4 | | |
| 102 | // | arg3 spill | | Caller's frame |
| 103 | // | arg2 spill | | |
| 104 | // | arg1 spill | | |
| 105 | // | Method* | --- |
| 106 | // | Return | |
| 107 | // | EBP,ESI,EDI | callee saves |
| 108 | // | EBX | arg3 |
| 109 | // | EDX | arg2 |
| 110 | // | ECX | arg1 |
| 111 | // | EAX/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 112 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 113 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 114 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 115 | static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes. |
| 116 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 117 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. |
| 118 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address. |
| 119 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size. |
| 120 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 121 | return gpr_index * kBytesPerGprSpillLocation; |
| 122 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 123 | #elif defined(__x86_64__) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 124 | // The callee save frame is pointed to by SP. |
| 125 | // | argN | | |
| 126 | // | ... | | |
| 127 | // | reg. arg spills | | Caller's frame |
| 128 | // | Method* | --- |
| 129 | // | Return | |
| 130 | // | R15 | callee save |
| 131 | // | R14 | callee save |
| 132 | // | R13 | callee save |
| 133 | // | R12 | callee save |
| 134 | // | R9 | arg5 |
| 135 | // | R8 | arg4 |
| 136 | // | RSI/R6 | arg1 |
| 137 | // | RBP/R5 | callee save |
| 138 | // | RBX/R3 | callee save |
| 139 | // | RDX/R2 | arg2 |
| 140 | // | RCX/R1 | arg3 |
| 141 | // | XMM7 | float arg 8 |
| 142 | // | XMM6 | float arg 7 |
| 143 | // | XMM5 | float arg 6 |
| 144 | // | XMM4 | float arg 5 |
| 145 | // | XMM3 | float arg 4 |
| 146 | // | XMM2 | float arg 3 |
| 147 | // | XMM1 | float arg 2 |
| 148 | // | XMM0 | float arg 1 |
| 149 | // | Padding | |
| 150 | // | RDI/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 151 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
| 152 | static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs. |
| 153 | static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 154 | static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes. |
| 155 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg. |
| 156 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg. |
| 157 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address. |
| 158 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size. |
| 159 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 160 | switch (gpr_index) { |
| 161 | case 0: return (4 * kBytesPerGprSpillLocation); |
| 162 | case 1: return (1 * kBytesPerGprSpillLocation); |
| 163 | case 2: return (0 * kBytesPerGprSpillLocation); |
| 164 | case 3: return (5 * kBytesPerGprSpillLocation); |
| 165 | case 4: return (6 * kBytesPerGprSpillLocation); |
| 166 | default: |
| 167 | LOG(FATAL) << "Unexpected GPR index: " << gpr_index; |
| 168 | return 0; |
| 169 | } |
| 170 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 171 | #else |
| 172 | #error "Unsupported architecture" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 173 | #endif |
| 174 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 175 | public: |
| 176 | static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp) |
| 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 178 | DCHECK((*sp)->IsCalleeSaveMethod()); |
| 179 | byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 180 | return *reinterpret_cast<mirror::ArtMethod**>(previous_sp); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 183 | // For the given quick ref and args quick frame, return the caller's PC. |
| 184 | static uintptr_t GetCallingPc(mirror::ArtMethod** sp) |
| 185 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 186 | DCHECK((*sp)->IsCalleeSaveMethod()); |
| 187 | byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 188 | return *reinterpret_cast<uintptr_t*>(lr); |
| 189 | } |
| 190 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 191 | QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 192 | const char* shorty, uint32_t shorty_len) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 194 | is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), |
| 195 | gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), |
| 196 | fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), |
| 197 | stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize |
| 198 | + StackArgumentStartFromShorty(is_static, shorty, shorty_len)), |
| 199 | gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid), |
| 200 | is_split_long_or_double_(false) { |
| 201 | DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 202 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 203 | } |
| 204 | |
| 205 | virtual ~QuickArgumentVisitor() {} |
| 206 | |
| 207 | virtual void Visit() = 0; |
| 208 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 209 | Primitive::Type GetParamPrimitiveType() const { |
| 210 | return cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | byte* GetParamAddress() const { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 214 | if (!kQuickSoftFloatAbi) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 215 | Primitive::Type type = GetParamPrimitiveType(); |
| 216 | if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 217 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 218 | return fpr_args_ + (fpr_index_ * kBytesPerFprSpillLocation); |
| 219 | } |
| 220 | } |
| 221 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 222 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 223 | return gpr_args_ + GprIndexToGprOffset(gpr_index_); |
| 224 | } |
| 225 | return stack_args_ + (stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool IsSplitLongOrDouble() const { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 229 | if ((kBytesPerGprSpillLocation == 4) || (kBytesPerFprSpillLocation == 4)) { |
| 230 | return is_split_long_or_double_; |
| 231 | } else { |
| 232 | return false; // An optimization for when GPR and FPRs are 64bit. |
| 233 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 236 | bool IsParamAReference() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 237 | return GetParamPrimitiveType() == Primitive::kPrimNot; |
| 238 | } |
| 239 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 240 | bool IsParamALongOrDouble() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 241 | Primitive::Type type = GetParamPrimitiveType(); |
| 242 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 243 | } |
| 244 | |
| 245 | uint64_t ReadSplitLongParam() const { |
| 246 | DCHECK(IsSplitLongOrDouble()); |
| 247 | uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress()); |
| 248 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_); |
| 249 | return (low_half & 0xffffffffULL) | (high_half << 32); |
| 250 | } |
| 251 | |
| 252 | void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 253 | gpr_index_ = 0; |
| 254 | fpr_index_ = 0; |
| 255 | stack_index_ = 0; |
| 256 | if (!is_static_) { // Handle this. |
| 257 | cur_type_ = Primitive::kPrimNot; |
| 258 | is_split_long_or_double_ = false; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 259 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 260 | if (kNumQuickGprArgs > 0) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 261 | gpr_index_++; |
| 262 | } else { |
| 263 | stack_index_++; |
| 264 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 265 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 266 | for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { |
| 267 | cur_type_ = Primitive::GetType(shorty_[shorty_index]); |
| 268 | switch (cur_type_) { |
| 269 | case Primitive::kPrimNot: |
| 270 | case Primitive::kPrimBoolean: |
| 271 | case Primitive::kPrimByte: |
| 272 | case Primitive::kPrimChar: |
| 273 | case Primitive::kPrimShort: |
| 274 | case Primitive::kPrimInt: |
| 275 | is_split_long_or_double_ = false; |
| 276 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 277 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 278 | gpr_index_++; |
| 279 | } else { |
| 280 | stack_index_++; |
| 281 | } |
| 282 | break; |
| 283 | case Primitive::kPrimFloat: |
| 284 | is_split_long_or_double_ = false; |
| 285 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 286 | if (kQuickSoftFloatAbi) { |
| 287 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 288 | gpr_index_++; |
| 289 | } else { |
| 290 | stack_index_++; |
| 291 | } |
| 292 | } else { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 293 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 294 | fpr_index_++; |
| 295 | } else { |
| 296 | stack_index_++; |
| 297 | } |
| 298 | } |
| 299 | break; |
| 300 | case Primitive::kPrimDouble: |
| 301 | case Primitive::kPrimLong: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 302 | if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 303 | is_split_long_or_double_ = (kBytesPerGprSpillLocation == 4) && |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 304 | ((gpr_index_ + 1) == kNumQuickGprArgs); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 305 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 306 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 307 | gpr_index_++; |
| 308 | if (kBytesPerGprSpillLocation == 4) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 309 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 310 | gpr_index_++; |
| 311 | } else { |
| 312 | stack_index_++; |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | if (kBytesStackArgLocation == 4) { |
| 317 | stack_index_+= 2; |
| 318 | } else { |
| 319 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 320 | stack_index_++; |
| 321 | } |
| 322 | } |
| 323 | } else { |
| 324 | is_split_long_or_double_ = (kBytesPerFprSpillLocation == 4) && |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 325 | ((fpr_index_ + 1) == kNumQuickFprArgs); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 326 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 327 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 328 | fpr_index_++; |
| 329 | if (kBytesPerFprSpillLocation == 4) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 330 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 331 | fpr_index_++; |
| 332 | } else { |
| 333 | stack_index_++; |
| 334 | } |
| 335 | } |
| 336 | } else { |
| 337 | if (kBytesStackArgLocation == 4) { |
| 338 | stack_index_+= 2; |
| 339 | } else { |
| 340 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 341 | stack_index_++; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | break; |
| 346 | default: |
| 347 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; |
| 348 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | private: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 353 | static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty, |
| 354 | uint32_t shorty_len) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 355 | if (kQuickSoftFloatAbi) { |
| 356 | CHECK_EQ(kNumQuickFprArgs, 0U); |
| 357 | return (kNumQuickGprArgs * kBytesPerGprSpillLocation) + kBytesPerGprSpillLocation /* ArtMethod* */; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 358 | } else { |
| 359 | size_t offset = kBytesPerGprSpillLocation; // Skip Method*. |
| 360 | size_t gprs_seen = 0; |
| 361 | size_t fprs_seen = 0; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 362 | if (!is_static && (gprs_seen < kNumQuickGprArgs)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 363 | gprs_seen++; |
| 364 | offset += kBytesStackArgLocation; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 365 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 366 | for (uint32_t i = 1; i < shorty_len; ++i) { |
| 367 | switch (shorty[i]) { |
| 368 | case 'Z': |
| 369 | case 'B': |
| 370 | case 'C': |
| 371 | case 'S': |
| 372 | case 'I': |
| 373 | case 'L': |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 374 | if (gprs_seen < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 375 | gprs_seen++; |
| 376 | offset += kBytesStackArgLocation; |
| 377 | } |
| 378 | break; |
| 379 | case 'J': |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 380 | if (gprs_seen < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 381 | gprs_seen++; |
| 382 | offset += 2 * kBytesStackArgLocation; |
| 383 | if (kBytesPerGprSpillLocation == 4) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 384 | if (gprs_seen < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 385 | gprs_seen++; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | break; |
| 390 | case 'F': |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 391 | if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 392 | fprs_seen++; |
| 393 | offset += kBytesStackArgLocation; |
| 394 | } |
| 395 | break; |
| 396 | case 'D': |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 397 | if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 398 | fprs_seen++; |
| 399 | offset += 2 * kBytesStackArgLocation; |
| 400 | if (kBytesPerFprSpillLocation == 4) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 401 | if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 402 | fprs_seen++; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | break; |
| 407 | default: |
| 408 | LOG(FATAL) << "Unexpected shorty character: " << shorty[i] << " in " << shorty; |
| 409 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 410 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 411 | return offset; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 412 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | const bool is_static_; |
| 416 | const char* const shorty_; |
| 417 | const uint32_t shorty_len_; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 418 | byte* const gpr_args_; // Address of GPR arguments in callee save frame. |
| 419 | byte* const fpr_args_; // Address of FPR arguments in callee save frame. |
| 420 | byte* const stack_args_; // Address of stack arguments in caller's frame. |
| 421 | uint32_t gpr_index_; // Index into spilled GPRs. |
| 422 | uint32_t fpr_index_; // Index into spilled FPRs. |
| 423 | uint32_t stack_index_; // Index into arguments on the stack. |
| 424 | // The current type of argument during VisitArguments. |
| 425 | Primitive::Type cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 426 | // Does a 64bit parameter straddle the register and stack arguments? |
| 427 | bool is_split_long_or_double_; |
| 428 | }; |
| 429 | |
| 430 | // Visits arguments on the stack placing them into the shadow frame. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 431 | class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 432 | public: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 433 | BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, |
| 434 | uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) : |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 435 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} |
| 436 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 437 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 438 | |
| 439 | private: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 440 | ShadowFrame* const sf_; |
| 441 | uint32_t cur_reg_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 442 | |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 443 | DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 444 | }; |
| 445 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 446 | void BuildQuickShadowFrameVisitor::Visit() { |
| 447 | Primitive::Type type = GetParamPrimitiveType(); |
| 448 | switch (type) { |
| 449 | case Primitive::kPrimLong: // Fall-through. |
| 450 | case Primitive::kPrimDouble: |
| 451 | if (IsSplitLongOrDouble()) { |
| 452 | sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); |
| 453 | } else { |
| 454 | sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); |
| 455 | } |
| 456 | ++cur_reg_; |
| 457 | break; |
| 458 | case Primitive::kPrimNot: { |
| 459 | StackReference<mirror::Object>* stack_ref = |
| 460 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 461 | sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); |
| 462 | } |
| 463 | break; |
| 464 | case Primitive::kPrimBoolean: // Fall-through. |
| 465 | case Primitive::kPrimByte: // Fall-through. |
| 466 | case Primitive::kPrimChar: // Fall-through. |
| 467 | case Primitive::kPrimShort: // Fall-through. |
| 468 | case Primitive::kPrimInt: // Fall-through. |
| 469 | case Primitive::kPrimFloat: |
| 470 | sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); |
| 471 | break; |
| 472 | case Primitive::kPrimVoid: |
| 473 | LOG(FATAL) << "UNREACHABLE"; |
| 474 | break; |
| 475 | } |
| 476 | ++cur_reg_; |
| 477 | } |
| 478 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 479 | extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self, |
| 480 | mirror::ArtMethod** sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 481 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 482 | // Ensure we don't get thread suspension until the object arguments are safely in the shadow |
| 483 | // frame. |
| 484 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 485 | |
| 486 | if (method->IsAbstract()) { |
| 487 | ThrowAbstractMethodError(method); |
| 488 | return 0; |
| 489 | } else { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 490 | DCHECK(!method->IsNative()) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 491 | const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame"); |
| 492 | MethodHelper mh(method); |
| 493 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 494 | DCHECK(code_item != nullptr) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 495 | uint16_t num_regs = code_item->registers_size_; |
| 496 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 497 | ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick. |
| 498 | method, 0, memory)); |
| 499 | size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_; |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 500 | BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(), |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 501 | mh.GetShortyLength(), |
| 502 | shadow_frame, first_arg_reg); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 503 | shadow_frame_builder.VisitArguments(); |
| 504 | // Push a transition back into managed code onto the linked list in thread. |
| 505 | ManagedStack fragment; |
| 506 | self->PushManagedStackFragment(&fragment); |
| 507 | self->PushShadowFrame(shadow_frame); |
| 508 | self->EndAssertNoThreadSuspension(old_cause); |
| 509 | |
| 510 | if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) { |
| 511 | // Ensure static method's class is initialized. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 512 | SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass()); |
| 513 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 514 | DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 515 | self->PopManagedStackFragment(fragment); |
| 516 | return 0; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame); |
| 521 | // Pop transition. |
| 522 | self->PopManagedStackFragment(fragment); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 523 | // No need to restore the args since the method has already been run by the interpreter. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 524 | return result.GetJ(); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Visits arguments on the stack placing them into the args vector, Object* arguments are converted |
| 529 | // to jobjects. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 530 | class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 531 | public: |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 532 | BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 533 | uint32_t shorty_len, ScopedObjectAccessUnchecked* soa, |
| 534 | std::vector<jvalue>* args) : |
| 535 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} |
| 536 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 537 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 538 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 539 | void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 540 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 541 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 542 | ScopedObjectAccessUnchecked* const soa_; |
| 543 | std::vector<jvalue>* const args_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 544 | // References which we must update when exiting in case the GC moved the objects. |
| 545 | std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 546 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 547 | DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); |
| 548 | }; |
| 549 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 550 | void BuildQuickArgumentVisitor::Visit() { |
| 551 | jvalue val; |
| 552 | Primitive::Type type = GetParamPrimitiveType(); |
| 553 | switch (type) { |
| 554 | case Primitive::kPrimNot: { |
| 555 | StackReference<mirror::Object>* stack_ref = |
| 556 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 557 | val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 558 | references_.push_back(std::make_pair(val.l, stack_ref)); |
| 559 | break; |
| 560 | } |
| 561 | case Primitive::kPrimLong: // Fall-through. |
| 562 | case Primitive::kPrimDouble: |
| 563 | if (IsSplitLongOrDouble()) { |
| 564 | val.j = ReadSplitLongParam(); |
| 565 | } else { |
| 566 | val.j = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 567 | } |
| 568 | break; |
| 569 | case Primitive::kPrimBoolean: // Fall-through. |
| 570 | case Primitive::kPrimByte: // Fall-through. |
| 571 | case Primitive::kPrimChar: // Fall-through. |
| 572 | case Primitive::kPrimShort: // Fall-through. |
| 573 | case Primitive::kPrimInt: // Fall-through. |
| 574 | case Primitive::kPrimFloat: |
| 575 | val.i = *reinterpret_cast<jint*>(GetParamAddress()); |
| 576 | break; |
| 577 | case Primitive::kPrimVoid: |
| 578 | LOG(FATAL) << "UNREACHABLE"; |
| 579 | val.j = 0; |
| 580 | break; |
| 581 | } |
| 582 | args_->push_back(val); |
| 583 | } |
| 584 | |
| 585 | void BuildQuickArgumentVisitor::FixupReferences() { |
| 586 | // Fixup any references which may have changed. |
| 587 | for (const auto& pair : references_) { |
| 588 | pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); |
| 589 | } |
| 590 | } |
| 591 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 592 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 593 | // which is responsible for recording callee save registers. We explicitly place into jobjects the |
| 594 | // incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a |
| 595 | // field within the proxy object, which will box the primitive arguments and deal with error cases. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 596 | extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 597 | mirror::Object* receiver, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 598 | Thread* self, mirror::ArtMethod** sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 599 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 600 | DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method); |
| 601 | DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 602 | // Ensure we don't get thread suspension until the object arguments are safely in jobjects. |
| 603 | const char* old_cause = |
| 604 | self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); |
| 605 | // Register the top of the managed stack, making stack crawlable. |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 606 | DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 607 | self->SetTopOfStack(sp, 0); |
| 608 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 609 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()) |
| 610 | << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 611 | self->VerifyStack(); |
| 612 | // Start new JNI local reference state. |
| 613 | JNIEnvExt* env = self->GetJniEnv(); |
| 614 | ScopedObjectAccessUnchecked soa(env); |
| 615 | ScopedJniEnvLocalRefState env_state(env); |
| 616 | // Create local ref. copies of proxy method and the receiver. |
| 617 | jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); |
| 618 | |
| 619 | // Placing arguments into args vector and remove the receiver. |
| 620 | MethodHelper proxy_mh(proxy_method); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 621 | DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 622 | std::vector<jvalue> args; |
| 623 | BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(), |
| 624 | proxy_mh.GetShortyLength(), &soa, &args); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 625 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 626 | local_ref_visitor.VisitArguments(); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 627 | DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 628 | args.erase(args.begin()); |
| 629 | |
| 630 | // Convert proxy method into expected interface method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 631 | mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod(); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 632 | DCHECK(interface_method != NULL) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 633 | DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); |
| 634 | jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method); |
| 635 | |
| 636 | // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code |
| 637 | // that performs allocations. |
| 638 | self->EndAssertNoThreadSuspension(old_cause); |
| 639 | JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(), |
| 640 | rcvr_jobj, interface_method_jobj, args); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 641 | // Restore references which might have moved. |
| 642 | local_ref_visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 643 | return result.GetJ(); |
| 644 | } |
| 645 | |
| 646 | // Read object references held in arguments from quick frames and place in a JNI local references, |
| 647 | // so they don't get garbage collected. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 648 | class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 649 | public: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 650 | RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, |
| 651 | uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 652 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} |
| 653 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 654 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 655 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 656 | void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 657 | |
| 658 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 659 | ScopedObjectAccessUnchecked* const soa_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 660 | // References which we must update when exiting in case the GC moved the objects. |
| 661 | std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 662 | DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 663 | }; |
| 664 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 665 | void RememberForGcArgumentVisitor::Visit() { |
| 666 | if (IsParamAReference()) { |
| 667 | StackReference<mirror::Object>* stack_ref = |
| 668 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 669 | jobject reference = |
| 670 | soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 671 | references_.push_back(std::make_pair(reference, stack_ref)); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | void RememberForGcArgumentVisitor::FixupReferences() { |
| 676 | // Fixup any references which may have changed. |
| 677 | for (const auto& pair : references_) { |
| 678 | pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 683 | // Lazily resolve a method for quick. Called by stub code. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 684 | extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 685 | mirror::Object* receiver, |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 686 | Thread* self, mirror::ArtMethod** sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 687 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 688 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 689 | // Start new JNI local reference state |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 690 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 691 | ScopedObjectAccessUnchecked soa(env); |
| 692 | ScopedJniEnvLocalRefState env_state(env); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 693 | const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 694 | |
| 695 | // Compute details about the called method (avoid GCs) |
| 696 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 697 | mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 698 | InvokeType invoke_type; |
| 699 | const DexFile* dex_file; |
| 700 | uint32_t dex_method_idx; |
| 701 | if (called->IsRuntimeMethod()) { |
| 702 | uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp)); |
| 703 | const DexFile::CodeItem* code; |
| 704 | { |
| 705 | MethodHelper mh(caller); |
| 706 | dex_file = &mh.GetDexFile(); |
| 707 | code = mh.GetCodeItem(); |
| 708 | } |
| 709 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 710 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 711 | Instruction::Code instr_code = instr->Opcode(); |
| 712 | bool is_range; |
| 713 | switch (instr_code) { |
| 714 | case Instruction::INVOKE_DIRECT: |
| 715 | invoke_type = kDirect; |
| 716 | is_range = false; |
| 717 | break; |
| 718 | case Instruction::INVOKE_DIRECT_RANGE: |
| 719 | invoke_type = kDirect; |
| 720 | is_range = true; |
| 721 | break; |
| 722 | case Instruction::INVOKE_STATIC: |
| 723 | invoke_type = kStatic; |
| 724 | is_range = false; |
| 725 | break; |
| 726 | case Instruction::INVOKE_STATIC_RANGE: |
| 727 | invoke_type = kStatic; |
| 728 | is_range = true; |
| 729 | break; |
| 730 | case Instruction::INVOKE_SUPER: |
| 731 | invoke_type = kSuper; |
| 732 | is_range = false; |
| 733 | break; |
| 734 | case Instruction::INVOKE_SUPER_RANGE: |
| 735 | invoke_type = kSuper; |
| 736 | is_range = true; |
| 737 | break; |
| 738 | case Instruction::INVOKE_VIRTUAL: |
| 739 | invoke_type = kVirtual; |
| 740 | is_range = false; |
| 741 | break; |
| 742 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 743 | invoke_type = kVirtual; |
| 744 | is_range = true; |
| 745 | break; |
| 746 | case Instruction::INVOKE_INTERFACE: |
| 747 | invoke_type = kInterface; |
| 748 | is_range = false; |
| 749 | break; |
| 750 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 751 | invoke_type = kInterface; |
| 752 | is_range = true; |
| 753 | break; |
| 754 | default: |
| 755 | LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL); |
| 756 | // Avoid used uninitialized warnings. |
| 757 | invoke_type = kDirect; |
| 758 | is_range = false; |
| 759 | } |
| 760 | dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c(); |
| 761 | |
| 762 | } else { |
| 763 | invoke_type = kStatic; |
| 764 | dex_file = &MethodHelper(called).GetDexFile(); |
| 765 | dex_method_idx = called->GetDexMethodIndex(); |
| 766 | } |
| 767 | uint32_t shorty_len; |
| 768 | const char* shorty = |
| 769 | dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 770 | RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 771 | visitor.VisitArguments(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 772 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 773 | bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 774 | // Resolve method filling in dex cache. |
| 775 | if (called->IsRuntimeMethod()) { |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 776 | SirtRef<mirror::Object> sirt_receiver(soa.Self(), virtual_or_interface ? receiver : nullptr); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 777 | called = linker->ResolveMethod(dex_method_idx, caller, invoke_type); |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 778 | receiver = sirt_receiver.get(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 779 | } |
| 780 | const void* code = NULL; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 781 | if (LIKELY(!self->IsExceptionPending())) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 782 | // Incompatible class change should have been handled in resolve method. |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 783 | CHECK(!called->CheckIncompatibleClassChange(invoke_type)) |
| 784 | << PrettyMethod(called) << " " << invoke_type; |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 785 | if (virtual_or_interface) { |
| 786 | // Refine called method based on receiver. |
| 787 | CHECK(receiver != nullptr) << invoke_type; |
| 788 | if (invoke_type == kVirtual) { |
| 789 | called = receiver->GetClass()->FindVirtualMethodForVirtual(called); |
| 790 | } else { |
| 791 | called = receiver->GetClass()->FindVirtualMethodForInterface(called); |
| 792 | } |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 793 | // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index |
| 794 | // of the sharpened method. |
| 795 | if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 796 | caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 797 | } else { |
| 798 | // Calling from one dex file to another, need to compute the method index appropriate to |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 799 | // the caller's dex file. Since we get here only if the original called was a runtime |
| 800 | // method, we've got the correct dex_file and a dex_method_idx from above. |
| 801 | DCHECK(&MethodHelper(caller).GetDexFile() == dex_file); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 802 | uint32_t method_index = |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 803 | MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 804 | if (method_index != DexFile::kDexNoIndex) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 805 | caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 809 | // Ensure that the called method's class is initialized. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 810 | SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 811 | linker->EnsureInitialized(called_class, true, true); |
| 812 | if (LIKELY(called_class->IsInitialized())) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 813 | code = called->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 814 | } else if (called_class->IsInitializing()) { |
| 815 | if (invoke_type == kStatic) { |
| 816 | // Class is still initializing, go to oat and grab code (trampoline must be left in place |
| 817 | // until class is initialized to stop races between threads). |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 818 | code = linker->GetQuickOatCodeFor(called); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 819 | } else { |
| 820 | // No trampoline for non-static methods. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 821 | code = called->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 822 | } |
| 823 | } else { |
| 824 | DCHECK(called_class->IsErroneous()); |
| 825 | } |
| 826 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 827 | CHECK_EQ(code == NULL, self->IsExceptionPending()); |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 828 | // Fixup any locally saved objects may have moved during a GC. |
| 829 | visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 830 | // Place called method in callee-save frame to be placed as first argument to quick method. |
| 831 | *sp = called; |
| 832 | return code; |
| 833 | } |
| 834 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 835 | |
| 836 | |
| 837 | /* |
| 838 | * This class uses a couple of observations to unite the different calling conventions through |
| 839 | * a few constants. |
| 840 | * |
| 841 | * 1) Number of registers used for passing is normally even, so counting down has no penalty for |
| 842 | * possible alignment. |
| 843 | * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point |
| 844 | * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote |
| 845 | * when we have to split things |
| 846 | * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats |
| 847 | * and we can use Int handling directly. |
| 848 | * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code |
| 849 | * necessary when widening. Also, widening of Ints will take place implicitly, and the |
| 850 | * extension should be compatible with Aarch64, which mandates copying the available bits |
| 851 | * into LSB and leaving the rest unspecified. |
| 852 | * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on |
| 853 | * the stack. |
| 854 | * 6) There is only little endian. |
| 855 | * |
| 856 | * |
| 857 | * Actual work is supposed to be done in a delegate of the template type. The interface is as |
| 858 | * follows: |
| 859 | * |
| 860 | * void PushGpr(uintptr_t): Add a value for the next GPR |
| 861 | * |
| 862 | * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need |
| 863 | * padding, that is, think the architecture is 32b and aligns 64b. |
| 864 | * |
| 865 | * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to |
| 866 | * split this if necessary. The current state will have aligned, if |
| 867 | * necessary. |
| 868 | * |
| 869 | * void PushStack(uintptr_t): Push a value to the stack. |
| 870 | * |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 871 | * uintptr_t PushSirt(mirror::Object* ref): Add a reference to the Sirt. This _will_ have nullptr, |
| 872 | * as this might be important for null initialization. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 873 | * Must return the jobject, that is, the reference to the |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 874 | * entry in the Sirt (nullptr if necessary). |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 875 | * |
| 876 | */ |
| 877 | template <class T> class BuildGenericJniFrameStateMachine { |
| 878 | public: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 879 | #if defined(__arm__) |
| 880 | // TODO: These are all dummy values! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 881 | static constexpr bool kNativeSoftFloatAbi = true; |
| 882 | static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 883 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
| 884 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 885 | static constexpr size_t kRegistersNeededForLong = 2; |
| 886 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 887 | static constexpr bool kMultiRegistersAligned = true; |
| 888 | static constexpr bool kMultiRegistersWidened = false; |
| 889 | static constexpr bool kAlignLongOnStack = true; |
| 890 | static constexpr bool kAlignDoubleOnStack = true; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 891 | #elif defined(__mips__) |
| 892 | // TODO: These are all dummy values! |
| 893 | static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. |
| 894 | static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. |
| 895 | static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. |
| 896 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 897 | static constexpr size_t kRegistersNeededForLong = 2; |
| 898 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 899 | static constexpr bool kMultiRegistersAligned = true; |
| 900 | static constexpr bool kMultiRegistersWidened = true; |
| 901 | static constexpr bool kAlignLongOnStack = false; |
| 902 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 903 | #elif defined(__i386__) |
| 904 | // TODO: Check these! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 905 | static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 906 | static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. |
| 907 | static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. |
| 908 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 909 | static constexpr size_t kRegistersNeededForLong = 2; |
| 910 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 911 | static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways |
| 912 | static constexpr bool kMultiRegistersWidened = false; |
| 913 | static constexpr bool kAlignLongOnStack = false; |
| 914 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 915 | #elif defined(__x86_64__) |
| 916 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 917 | static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. |
| 918 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 919 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 920 | static constexpr size_t kRegistersNeededForLong = 1; |
| 921 | static constexpr size_t kRegistersNeededForDouble = 1; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 922 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 7a0e504 | 2014-03-07 13:03:19 -0800 | [diff] [blame] | 923 | static constexpr bool kMultiRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 924 | static constexpr bool kAlignLongOnStack = false; |
| 925 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 926 | #else |
| 927 | #error "Unsupported architecture" |
| 928 | #endif |
| 929 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 930 | public: |
| 931 | explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs), |
| 932 | fpr_index_(kNumNativeFprArgs), |
| 933 | stack_entries_(0), |
| 934 | delegate_(delegate) { |
| 935 | // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff |
| 936 | // the next register is even; counting down is just to make the compiler happy... |
| 937 | CHECK_EQ(kNumNativeGprArgs % 2, 0U); |
| 938 | CHECK_EQ(kNumNativeFprArgs % 2, 0U); |
| 939 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 940 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 941 | virtual ~BuildGenericJniFrameStateMachine() {} |
| 942 | |
| 943 | bool HavePointerGpr() { |
| 944 | return gpr_index_ > 0; |
| 945 | } |
| 946 | |
| 947 | void AdvancePointer(void* val) { |
| 948 | if (HavePointerGpr()) { |
| 949 | gpr_index_--; |
| 950 | PushGpr(reinterpret_cast<uintptr_t>(val)); |
| 951 | } else { |
| 952 | stack_entries_++; // TODO: have a field for pointer length as multiple of 32b |
| 953 | PushStack(reinterpret_cast<uintptr_t>(val)); |
| 954 | gpr_index_ = 0; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | |
| 959 | bool HaveSirtGpr() { |
| 960 | return gpr_index_ > 0; |
| 961 | } |
| 962 | |
| 963 | void AdvanceSirt(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 964 | uintptr_t sirtRef = PushSirt(ptr); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 965 | if (HaveSirtGpr()) { |
| 966 | gpr_index_--; |
| 967 | PushGpr(sirtRef); |
| 968 | } else { |
| 969 | stack_entries_++; |
| 970 | PushStack(sirtRef); |
| 971 | gpr_index_ = 0; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | |
| 976 | bool HaveIntGpr() { |
| 977 | return gpr_index_ > 0; |
| 978 | } |
| 979 | |
| 980 | void AdvanceInt(uint32_t val) { |
| 981 | if (HaveIntGpr()) { |
| 982 | gpr_index_--; |
| 983 | PushGpr(val); |
| 984 | } else { |
| 985 | stack_entries_++; |
| 986 | PushStack(val); |
| 987 | gpr_index_ = 0; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | |
| 992 | bool HaveLongGpr() { |
| 993 | return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); |
| 994 | } |
| 995 | |
| 996 | bool LongGprNeedsPadding() { |
| 997 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 998 | kAlignLongOnStack && // and when it needs alignment |
| 999 | (gpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1000 | } |
| 1001 | |
| 1002 | bool LongStackNeedsPadding() { |
| 1003 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1004 | kAlignLongOnStack && // and when it needs 8B alignment |
| 1005 | (stack_entries_ & 1) == 1; // counter is odd |
| 1006 | } |
| 1007 | |
| 1008 | void AdvanceLong(uint64_t val) { |
| 1009 | if (HaveLongGpr()) { |
| 1010 | if (LongGprNeedsPadding()) { |
| 1011 | PushGpr(0); |
| 1012 | gpr_index_--; |
| 1013 | } |
| 1014 | if (kRegistersNeededForLong == 1) { |
| 1015 | PushGpr(static_cast<uintptr_t>(val)); |
| 1016 | } else { |
| 1017 | PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1018 | PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1019 | } |
| 1020 | gpr_index_ -= kRegistersNeededForLong; |
| 1021 | } else { |
| 1022 | if (LongStackNeedsPadding()) { |
| 1023 | PushStack(0); |
| 1024 | stack_entries_++; |
| 1025 | } |
| 1026 | if (kRegistersNeededForLong == 1) { |
| 1027 | PushStack(static_cast<uintptr_t>(val)); |
| 1028 | stack_entries_++; |
| 1029 | } else { |
| 1030 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1031 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1032 | stack_entries_ += 2; |
| 1033 | } |
| 1034 | gpr_index_ = 0; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | bool HaveFloatFpr() { |
| 1040 | return fpr_index_ > 0; |
| 1041 | } |
| 1042 | |
| 1043 | // TODO: please review this bit representation retrieving. |
| 1044 | template <typename U, typename V> V convert(U in) { |
| 1045 | CHECK_LE(sizeof(U), sizeof(V)); |
| 1046 | union { U u; V v; } tmp; |
| 1047 | tmp.u = in; |
| 1048 | return tmp.v; |
| 1049 | } |
| 1050 | |
| 1051 | void AdvanceFloat(float val) { |
| 1052 | if (kNativeSoftFloatAbi) { |
| 1053 | AdvanceInt(convert<float, uint32_t>(val)); |
| 1054 | } else { |
| 1055 | if (HaveFloatFpr()) { |
| 1056 | fpr_index_--; |
| 1057 | if (kRegistersNeededForDouble == 1) { |
| 1058 | if (kMultiRegistersWidened) { |
| 1059 | PushFpr8(convert<double, uint64_t>(val)); |
| 1060 | } else { |
| 1061 | // No widening, just use the bits. |
| 1062 | PushFpr8(convert<float, uint64_t>(val)); |
| 1063 | } |
| 1064 | } else { |
| 1065 | PushFpr4(val); |
| 1066 | } |
| 1067 | } else { |
| 1068 | stack_entries_++; |
| 1069 | if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) { |
| 1070 | // Need to widen before storing: Note the "double" in the template instantiation. |
| 1071 | PushStack(convert<double, uintptr_t>(val)); |
| 1072 | } else { |
| 1073 | PushStack(convert<float, uintptr_t>(val)); |
| 1074 | } |
| 1075 | fpr_index_ = 0; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | bool HaveDoubleFpr() { |
| 1082 | return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); |
| 1083 | } |
| 1084 | |
| 1085 | bool DoubleFprNeedsPadding() { |
| 1086 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1087 | kAlignDoubleOnStack && // and when it needs alignment |
| 1088 | (fpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1089 | } |
| 1090 | |
| 1091 | bool DoubleStackNeedsPadding() { |
| 1092 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1093 | kAlignDoubleOnStack && // and when it needs 8B alignment |
| 1094 | (stack_entries_ & 1) == 1; // counter is odd |
| 1095 | } |
| 1096 | |
| 1097 | void AdvanceDouble(uint64_t val) { |
| 1098 | if (kNativeSoftFloatAbi) { |
| 1099 | AdvanceLong(val); |
| 1100 | } else { |
| 1101 | if (HaveDoubleFpr()) { |
| 1102 | if (DoubleFprNeedsPadding()) { |
| 1103 | PushFpr4(0); |
| 1104 | fpr_index_--; |
| 1105 | } |
| 1106 | PushFpr8(val); |
| 1107 | fpr_index_ -= kRegistersNeededForDouble; |
| 1108 | } else { |
| 1109 | if (DoubleStackNeedsPadding()) { |
| 1110 | PushStack(0); |
| 1111 | stack_entries_++; |
| 1112 | } |
| 1113 | if (kRegistersNeededForDouble == 1) { |
| 1114 | PushStack(static_cast<uintptr_t>(val)); |
| 1115 | stack_entries_++; |
| 1116 | } else { |
| 1117 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1118 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1119 | stack_entries_ += 2; |
| 1120 | } |
| 1121 | fpr_index_ = 0; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | uint32_t getStackEntries() { |
| 1127 | return stack_entries_; |
| 1128 | } |
| 1129 | |
| 1130 | uint32_t getNumberOfUsedGprs() { |
| 1131 | return kNumNativeGprArgs - gpr_index_; |
| 1132 | } |
| 1133 | |
| 1134 | uint32_t getNumberOfUsedFprs() { |
| 1135 | return kNumNativeFprArgs - fpr_index_; |
| 1136 | } |
| 1137 | |
| 1138 | private: |
| 1139 | void PushGpr(uintptr_t val) { |
| 1140 | delegate_->PushGpr(val); |
| 1141 | } |
| 1142 | void PushFpr4(float val) { |
| 1143 | delegate_->PushFpr4(val); |
| 1144 | } |
| 1145 | void PushFpr8(uint64_t val) { |
| 1146 | delegate_->PushFpr8(val); |
| 1147 | } |
| 1148 | void PushStack(uintptr_t val) { |
| 1149 | delegate_->PushStack(val); |
| 1150 | } |
| 1151 | uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1152 | return delegate_->PushSirt(ref); |
| 1153 | } |
| 1154 | |
| 1155 | uint32_t gpr_index_; // Number of free GPRs |
| 1156 | uint32_t fpr_index_; // Number of free FPRs |
| 1157 | uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not |
| 1158 | // extended |
| 1159 | T* delegate_; // What Push implementation gets called |
| 1160 | }; |
| 1161 | |
| 1162 | class ComputeGenericJniFrameSize FINAL { |
| 1163 | public: |
| 1164 | ComputeGenericJniFrameSize() : num_sirt_references_(0), num_stack_entries_(0) {} |
| 1165 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1166 | uint32_t GetStackSize() { |
| 1167 | return num_stack_entries_ * sizeof(uintptr_t); |
| 1168 | } |
| 1169 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1170 | // WARNING: After this, *sp won't be pointing to the method anymore! |
| 1171 | void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len, |
| 1172 | void* sp, StackIndirectReferenceTable** table, uint32_t* sirt_entries, |
| 1173 | uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr, |
| 1174 | void** code_return, size_t* overall_size) |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1175 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1176 | ComputeAll(is_static, shorty, shorty_len); |
| 1177 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1178 | mirror::ArtMethod* method = **m; |
| 1179 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1180 | uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1181 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1182 | // First, fix up the layout of the callee-save frame. |
| 1183 | // We have to squeeze in the Sirt, and relocate the method pointer. |
| 1184 | |
| 1185 | // "Free" the slot for the method. |
| 1186 | sp8 += kPointerSize; |
| 1187 | |
| 1188 | // Add the Sirt. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1189 | *sirt_entries = num_sirt_references_; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1190 | size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSize(num_sirt_references_); |
| 1191 | sp8 -= sirt_size; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1192 | *table = reinterpret_cast<StackIndirectReferenceTable*>(sp8); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1193 | (*table)->SetNumberOfReferences(num_sirt_references_); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1194 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1195 | // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. |
| 1196 | sp8 -= kPointerSize; |
| 1197 | uint8_t* method_pointer = sp8; |
| 1198 | *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method; |
| 1199 | *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer); |
| 1200 | |
| 1201 | // Reference cookie and padding |
| 1202 | sp8 -= 8; |
| 1203 | // Store Sirt size |
| 1204 | *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(sirt_size & 0xFFFFFFFF); |
| 1205 | |
| 1206 | // Next comes the native call stack. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1207 | sp8 -= GetStackSize(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1208 | // Now align the call stack below. This aligns by 16, as AArch64 seems to require. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1209 | uintptr_t mask = ~0x0F; |
| 1210 | sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask); |
| 1211 | *start_stack = reinterpret_cast<uintptr_t*>(sp8); |
| 1212 | |
| 1213 | // put fprs and gprs below |
| 1214 | // Assumption is OK right now, as we have soft-float arm |
| 1215 | size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs; |
| 1216 | sp8 -= fregs * sizeof(uintptr_t); |
| 1217 | *start_fpr = reinterpret_cast<uint32_t*>(sp8); |
| 1218 | size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs; |
| 1219 | sp8 -= iregs * sizeof(uintptr_t); |
| 1220 | *start_gpr = reinterpret_cast<uintptr_t*>(sp8); |
| 1221 | |
| 1222 | // reserve space for the code pointer |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1223 | sp8 -= kPointerSize; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1224 | *code_return = reinterpret_cast<void*>(sp8); |
| 1225 | |
| 1226 | *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1227 | |
| 1228 | // The new SP is stored at the end of the alloca, so it can be immediately popped |
| 1229 | sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB; |
| 1230 | *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | void ComputeSirtOffset() { } // nothing to do, static right now |
| 1234 | |
| 1235 | void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len) |
| 1236 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1237 | BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this); |
| 1238 | |
| 1239 | // JNIEnv |
| 1240 | sm.AdvancePointer(nullptr); |
| 1241 | |
| 1242 | // Class object or this as first argument |
| 1243 | sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); |
| 1244 | |
| 1245 | for (uint32_t i = 1; i < shorty_len; ++i) { |
| 1246 | Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); |
| 1247 | switch (cur_type_) { |
| 1248 | case Primitive::kPrimNot: |
| 1249 | sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); |
| 1250 | break; |
| 1251 | |
| 1252 | case Primitive::kPrimBoolean: |
| 1253 | case Primitive::kPrimByte: |
| 1254 | case Primitive::kPrimChar: |
| 1255 | case Primitive::kPrimShort: |
| 1256 | case Primitive::kPrimInt: |
| 1257 | sm.AdvanceInt(0); |
| 1258 | break; |
| 1259 | case Primitive::kPrimFloat: |
| 1260 | sm.AdvanceFloat(0); |
| 1261 | break; |
| 1262 | case Primitive::kPrimDouble: |
| 1263 | sm.AdvanceDouble(0); |
| 1264 | break; |
| 1265 | case Primitive::kPrimLong: |
| 1266 | sm.AdvanceLong(0); |
| 1267 | break; |
| 1268 | default: |
| 1269 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | num_stack_entries_ = sm.getStackEntries(); |
| 1274 | } |
| 1275 | |
| 1276 | void PushGpr(uintptr_t /* val */) { |
| 1277 | // not optimizing registers, yet |
| 1278 | } |
| 1279 | |
| 1280 | void PushFpr4(float /* val */) { |
| 1281 | // not optimizing registers, yet |
| 1282 | } |
| 1283 | |
| 1284 | void PushFpr8(uint64_t /* val */) { |
| 1285 | // not optimizing registers, yet |
| 1286 | } |
| 1287 | |
| 1288 | void PushStack(uintptr_t /* val */) { |
| 1289 | // counting is already done in the superclass |
| 1290 | } |
| 1291 | |
| 1292 | uintptr_t PushSirt(mirror::Object* /* ptr */) { |
| 1293 | num_sirt_references_++; |
| 1294 | return reinterpret_cast<uintptr_t>(nullptr); |
| 1295 | } |
| 1296 | |
| 1297 | private: |
| 1298 | uint32_t num_sirt_references_; |
| 1299 | uint32_t num_stack_entries_; |
| 1300 | }; |
| 1301 | |
| 1302 | // Visits arguments on the stack placing them into a region lower down the stack for the benefit |
| 1303 | // of transitioning into native code. |
| 1304 | class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1305 | public: |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1306 | BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty, |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1307 | uint32_t shorty_len, Thread* self) : |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1308 | QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1309 | ComputeGenericJniFrameSize fsc; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1310 | fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &sirt_, &sirt_expected_refs_, |
| 1311 | &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_, |
| 1312 | &alloca_used_size_); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1313 | sirt_number_of_references_ = 0; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1314 | cur_sirt_entry_ = reinterpret_cast<StackReference<mirror::Object>*>(GetFirstSirtEntry()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1315 | |
| 1316 | // jni environment is always first argument |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1317 | sm_.AdvancePointer(self->GetJniEnv()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1318 | |
| 1319 | if (is_static) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1320 | sm_.AdvanceSirt((**sp)->GetDeclaringClass()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1324 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1325 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1326 | void FinalizeSirt(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1327 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1328 | jobject GetFirstSirtEntry() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1329 | return reinterpret_cast<jobject>(sirt_->GetStackReference(0)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | void PushGpr(uintptr_t val) { |
| 1333 | *cur_gpr_reg_ = val; |
| 1334 | cur_gpr_reg_++; |
| 1335 | } |
| 1336 | |
| 1337 | void PushFpr4(float val) { |
| 1338 | *cur_fpr_reg_ = val; |
| 1339 | cur_fpr_reg_++; |
| 1340 | } |
| 1341 | |
| 1342 | void PushFpr8(uint64_t val) { |
| 1343 | uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); |
| 1344 | *tmp = val; |
| 1345 | cur_fpr_reg_ += 2; |
| 1346 | } |
| 1347 | |
| 1348 | void PushStack(uintptr_t val) { |
| 1349 | *cur_stack_arg_ = val; |
| 1350 | cur_stack_arg_++; |
| 1351 | } |
| 1352 | |
| 1353 | uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1354 | uintptr_t tmp; |
| 1355 | if (ref == nullptr) { |
| 1356 | *cur_sirt_entry_ = StackReference<mirror::Object>(); |
| 1357 | tmp = reinterpret_cast<uintptr_t>(nullptr); |
| 1358 | } else { |
| 1359 | *cur_sirt_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref); |
| 1360 | tmp = reinterpret_cast<uintptr_t>(cur_sirt_entry_); |
| 1361 | } |
| 1362 | cur_sirt_entry_++; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1363 | sirt_number_of_references_++; |
| 1364 | return tmp; |
| 1365 | } |
| 1366 | |
| 1367 | // Size of the part of the alloca that we actually need. |
| 1368 | size_t GetAllocaUsedSize() { |
| 1369 | return alloca_used_size_; |
| 1370 | } |
| 1371 | |
| 1372 | void* GetCodeReturn() { |
| 1373 | return code_return_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | private: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1377 | uint32_t sirt_number_of_references_; |
| 1378 | StackReference<mirror::Object>* cur_sirt_entry_; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1379 | StackIndirectReferenceTable* sirt_; |
| 1380 | uint32_t sirt_expected_refs_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1381 | uintptr_t* cur_gpr_reg_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1382 | uint32_t* cur_fpr_reg_; |
| 1383 | uintptr_t* cur_stack_arg_; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1384 | // StackReference<mirror::Object>* top_of_sirt_; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1385 | void* code_return_; |
| 1386 | size_t alloca_used_size_; |
| 1387 | |
| 1388 | BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1389 | |
| 1390 | DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); |
| 1391 | }; |
| 1392 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1393 | void BuildGenericJniFrameVisitor::Visit() { |
| 1394 | Primitive::Type type = GetParamPrimitiveType(); |
| 1395 | switch (type) { |
| 1396 | case Primitive::kPrimLong: { |
| 1397 | jlong long_arg; |
| 1398 | if (IsSplitLongOrDouble()) { |
| 1399 | long_arg = ReadSplitLongParam(); |
| 1400 | } else { |
| 1401 | long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 1402 | } |
| 1403 | sm_.AdvanceLong(long_arg); |
| 1404 | break; |
| 1405 | } |
| 1406 | case Primitive::kPrimDouble: { |
| 1407 | uint64_t double_arg; |
| 1408 | if (IsSplitLongOrDouble()) { |
| 1409 | // Read into union so that we don't case to a double. |
| 1410 | double_arg = ReadSplitLongParam(); |
| 1411 | } else { |
| 1412 | double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); |
| 1413 | } |
| 1414 | sm_.AdvanceDouble(double_arg); |
| 1415 | break; |
| 1416 | } |
| 1417 | case Primitive::kPrimNot: { |
| 1418 | StackReference<mirror::Object>* stack_ref = |
| 1419 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 1420 | sm_.AdvanceSirt(stack_ref->AsMirrorPtr()); |
| 1421 | break; |
| 1422 | } |
| 1423 | case Primitive::kPrimFloat: |
| 1424 | sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); |
| 1425 | break; |
| 1426 | case Primitive::kPrimBoolean: // Fall-through. |
| 1427 | case Primitive::kPrimByte: // Fall-through. |
| 1428 | case Primitive::kPrimChar: // Fall-through. |
| 1429 | case Primitive::kPrimShort: // Fall-through. |
| 1430 | case Primitive::kPrimInt: // Fall-through. |
| 1431 | sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); |
| 1432 | break; |
| 1433 | case Primitive::kPrimVoid: |
| 1434 | LOG(FATAL) << "UNREACHABLE"; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | void BuildGenericJniFrameVisitor::FinalizeSirt(Thread* self) { |
| 1440 | // Initialize padding entries. |
| 1441 | while (sirt_number_of_references_ < sirt_expected_refs_) { |
| 1442 | *cur_sirt_entry_ = StackReference<mirror::Object>(); |
| 1443 | cur_sirt_entry_++; |
| 1444 | sirt_number_of_references_++; |
| 1445 | } |
| 1446 | sirt_->SetNumberOfReferences(sirt_expected_refs_); |
| 1447 | DCHECK_NE(sirt_expected_refs_, 0U); |
| 1448 | // Install Sirt. |
| 1449 | self->PushSirt(sirt_); |
| 1450 | } |
| 1451 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1452 | /* |
| 1453 | * Initializes an alloca region assumed to be directly below sp for a native call: |
| 1454 | * Create a Sirt and call stack and fill a mini stack with values to be pushed to registers. |
| 1455 | * The final element on the stack is a pointer to the native code. |
| 1456 | * |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1457 | * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. |
| 1458 | * We need to fix this, as the Sirt needs to go into the callee-save frame. |
| 1459 | * |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1460 | * The return of this function denotes: |
| 1461 | * 1) How many bytes of the alloca can be released, if the value is non-negative. |
| 1462 | * 2) An error, if the value is negative. |
| 1463 | */ |
| 1464 | extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp) |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1465 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1466 | mirror::ArtMethod* called = *sp; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1467 | DCHECK(called->IsNative()) << PrettyMethod(called, true); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1468 | |
| 1469 | // run the visitor |
| 1470 | MethodHelper mh(called); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1471 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1472 | BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(), |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1473 | self); |
| 1474 | visitor.VisitArguments(); |
| 1475 | visitor.FinalizeSirt(self); |
| 1476 | |
| 1477 | // fix up managed-stack things in Thread |
| 1478 | self->SetTopOfStack(sp, 0); |
| 1479 | |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1480 | self->VerifyStack(); |
| 1481 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1482 | // start JNI, save the cookie |
| 1483 | uint32_t cookie; |
| 1484 | if (called->IsSynchronized()) { |
| 1485 | cookie = JniMethodStartSynchronized(visitor.GetFirstSirtEntry(), self); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1486 | if (self->IsExceptionPending()) { |
| 1487 | self->PopSirt(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1488 | // A negative value denotes an error. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1489 | // TODO: Do we still need to fix the stack pointer? I think so. Then it's necessary to push |
| 1490 | // that value! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1491 | return -1; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } else { |
| 1494 | cookie = JniMethodStart(self); |
| 1495 | } |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1496 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1497 | *(sp32 - 1) = cookie; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1498 | |
| 1499 | // retrieve native code |
| 1500 | const void* nativeCode = called->GetNativeMethod(); |
| 1501 | if (nativeCode == nullptr) { |
| 1502 | // TODO: is this really an error, or do we need to try to find native code? |
| 1503 | LOG(FATAL) << "Finding native code not implemented yet."; |
| 1504 | } |
| 1505 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1506 | uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn()); |
| 1507 | size_t window_size = visitor.GetAllocaUsedSize(); |
| 1508 | *code_pointer = reinterpret_cast<uintptr_t>(nativeCode); |
| 1509 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1510 | // 5K reserved, window_size + frame pointer used. |
| 1511 | return (5 * KB) - window_size - kPointerSize; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Is called after the native JNI code. Responsible for cleanup (SIRT, saved state) and |
| 1516 | * unlocking. |
| 1517 | */ |
| 1518 | extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp, |
| 1519 | jvalue result, uint64_t result_f) |
| 1520 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1521 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
| 1522 | mirror::ArtMethod* called = *sp; |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1523 | uint32_t cookie = *(sp32 - 1); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1524 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1525 | MethodHelper mh(called); |
| 1526 | char return_shorty_char = mh.GetShorty()[0]; |
| 1527 | |
| 1528 | if (return_shorty_char == 'L') { |
| 1529 | // the only special ending call |
| 1530 | if (called->IsSynchronized()) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1531 | StackIndirectReferenceTable* table = |
| 1532 | reinterpret_cast<StackIndirectReferenceTable*>( |
| 1533 | reinterpret_cast<uint8_t*>(sp) + kPointerSize); |
| 1534 | jobject tmp = reinterpret_cast<jobject>(table->GetStackReference(0)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1535 | |
| 1536 | return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(result.l, cookie, tmp, |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1537 | self)); |
| 1538 | } else { |
| 1539 | return reinterpret_cast<uint64_t>(JniMethodEndWithReference(result.l, cookie, self)); |
| 1540 | } |
| 1541 | } else { |
| 1542 | if (called->IsSynchronized()) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1543 | StackIndirectReferenceTable* table = |
| 1544 | reinterpret_cast<StackIndirectReferenceTable*>( |
| 1545 | reinterpret_cast<uint8_t*>(sp) + kPointerSize); |
| 1546 | jobject tmp = reinterpret_cast<jobject>(table->GetStackReference(0)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1547 | |
| 1548 | JniMethodEndSynchronized(cookie, tmp, self); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1549 | } else { |
| 1550 | JniMethodEnd(cookie, self); |
| 1551 | } |
| 1552 | |
| 1553 | switch (return_shorty_char) { |
| 1554 | case 'F': // Fall-through. |
| 1555 | case 'D': |
| 1556 | return result_f; |
| 1557 | case 'Z': |
| 1558 | return result.z; |
| 1559 | case 'B': |
| 1560 | return result.b; |
| 1561 | case 'C': |
| 1562 | return result.c; |
| 1563 | case 'S': |
| 1564 | return result.s; |
| 1565 | case 'I': |
| 1566 | return result.i; |
| 1567 | case 'J': |
| 1568 | return result.j; |
| 1569 | case 'V': |
| 1570 | return 0; |
| 1571 | default: |
| 1572 | LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char; |
| 1573 | return 0; |
| 1574 | } |
| 1575 | } |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1578 | } // namespace art |