quick_trampoline_entrypoints.cc revision e94652f1e321b2c8b71acbe5b07d2ebf69fbdb99
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"
18#include "common_throws.h"
19#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
21#include "entrypoints/entrypoint_utils-inl.h"
22#include "entrypoints/runtime_asm_entrypoints.h"
23#include "gc/accounting/card_table-inl.h"
24#include "interpreter/interpreter.h"
25#include "method_helper.h"
26#include "mirror/art_method-inl.h"
27#include "mirror/class-inl.h"
28#include "mirror/dex_cache-inl.h"
29#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
31#include "runtime.h"
32#include "scoped_thread_state_change.h"
33
34namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
38  // Number of bytes for each out register in the caller method's frame.
39  static constexpr size_t kBytesStackArgLocation = 4;
40  // Frame size in bytes of a callee-save frame for RefsAndArgs.
41  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
42      GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
43#if defined(__arm__)
44  // The callee save frame is pointed to by SP.
45  // | argN       |  |
46  // | ...        |  |
47  // | arg4       |  |
48  // | arg3 spill |  |  Caller's frame
49  // | arg2 spill |  |
50  // | arg1 spill |  |
51  // | Method*    | ---
52  // | LR         |
53  // | ...        |    4x6 bytes callee saves
54  // | R3         |
55  // | R2         |
56  // | R1         |
57  // | S15        |
58  // | :          |
59  // | S0         |
60  // |            |    4x2 bytes padding
61  // | Method*    |  <- sp
62  static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
63  static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
64  static constexpr size_t kNumQuickGprArgs = 3;
65  static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
66  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
67      arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs);  // Offset of first FPR arg.
68  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
69      arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs);  // Offset of first GPR arg.
70  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
71      arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs);  // Offset of return address.
72  static size_t GprIndexToGprOffset(uint32_t gpr_index) {
73    return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
74  }
75#elif defined(__aarch64__)
76  // The callee save frame is pointed to by SP.
77  // | argN       |  |
78  // | ...        |  |
79  // | arg4       |  |
80  // | arg3 spill |  |  Caller's frame
81  // | arg2 spill |  |
82  // | arg1 spill |  |
83  // | Method*    | ---
84  // | LR         |
85  // | X29        |
86  // |  :         |
87  // | X20        |
88  // | X7         |
89  // | :          |
90  // | X1         |
91  // | D7         |
92  // |  :         |
93  // | D0         |
94  // |            |    padding
95  // | Method*    |  <- sp
96  static constexpr bool kQuickSoftFloatAbi = false;  // This is a hard float ABI.
97  static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
98  static constexpr size_t kNumQuickGprArgs = 7;  // 7 arguments passed in GPRs.
99  static constexpr size_t kNumQuickFprArgs = 8;  // 8 arguments passed in FPRs.
100  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
101      arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs);  // Offset of first FPR arg.
102  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
103      arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs);  // Offset of first GPR arg.
104  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
105      arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs);  // Offset of return address.
106  static size_t GprIndexToGprOffset(uint32_t gpr_index) {
107    return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
108  }
109#elif defined(__mips__)
110  // The callee save frame is pointed to by SP.
111  // | argN       |  |
112  // | ...        |  |
113  // | arg4       |  |
114  // | arg3 spill |  |  Caller's frame
115  // | arg2 spill |  |
116  // | arg1 spill |  |
117  // | Method*    | ---
118  // | RA         |
119  // | ...        |    callee saves
120  // | A3         |    arg3
121  // | A2         |    arg2
122  // | A1         |    arg1
123  // | A0/Method* |  <- sp
124  static constexpr bool kQuickSoftFloatAbi = true;  // This is a soft float ABI.
125  static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
126  static constexpr size_t kNumQuickGprArgs = 3;  // 3 arguments passed in GPRs.
127  static constexpr size_t kNumQuickFprArgs = 0;  // 0 arguments passed in FPRs.
128  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0;  // Offset of first FPR arg.
129  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4;  // Offset of first GPR arg.
130  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60;  // Offset of return address.
131  static size_t GprIndexToGprOffset(uint32_t gpr_index) {
132    return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
133  }
134#elif defined(__i386__)
135  // The callee save frame is pointed to by SP.
136  // | argN        |  |
137  // | ...         |  |
138  // | arg4        |  |
139  // | arg3 spill  |  |  Caller's frame
140  // | arg2 spill  |  |
141  // | arg1 spill  |  |
142  // | Method*     | ---
143  // | Return      |
144  // | EBP,ESI,EDI |    callee saves
145  // | EBX         |    arg3
146  // | EDX         |    arg2
147  // | ECX         |    arg1
148  // | EAX/Method* |  <- sp
149  static constexpr bool kQuickSoftFloatAbi = true;  // This is a soft float ABI.
150  static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
151  static constexpr size_t kNumQuickGprArgs = 3;  // 3 arguments passed in GPRs.
152  static constexpr size_t kNumQuickFprArgs = 0;  // 0 arguments passed in FPRs.
153  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0;  // Offset of first FPR arg.
154  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4;  // Offset of first GPR arg.
155  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28;  // Offset of return address.
156  static size_t GprIndexToGprOffset(uint32_t gpr_index) {
157    return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
158  }
159#elif defined(__x86_64__)
160  // The callee save frame is pointed to by SP.
161  // | argN            |  |
162  // | ...             |  |
163  // | reg. arg spills |  |  Caller's frame
164  // | Method*         | ---
165  // | Return          |
166  // | R15             |    callee save
167  // | R14             |    callee save
168  // | R13             |    callee save
169  // | R12             |    callee save
170  // | R9              |    arg5
171  // | R8              |    arg4
172  // | RSI/R6          |    arg1
173  // | RBP/R5          |    callee save
174  // | RBX/R3          |    callee save
175  // | RDX/R2          |    arg2
176  // | RCX/R1          |    arg3
177  // | XMM7            |    float arg 8
178  // | XMM6            |    float arg 7
179  // | XMM5            |    float arg 6
180  // | XMM4            |    float arg 5
181  // | XMM3            |    float arg 4
182  // | XMM2            |    float arg 3
183  // | XMM1            |    float arg 2
184  // | XMM0            |    float arg 1
185  // | Padding         |
186  // | RDI/Method*     |  <- sp
187  static constexpr bool kQuickSoftFloatAbi = false;  // This is a hard float ABI.
188  static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
189  static constexpr size_t kNumQuickGprArgs = 5;  // 5 arguments passed in GPRs.
190  static constexpr size_t kNumQuickFprArgs = 8;  // 8 arguments passed in FPRs.
191  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16;  // Offset of first FPR arg.
192  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8;  // Offset of first GPR arg.
193  static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8;  // Offset of return address.
194  static size_t GprIndexToGprOffset(uint32_t gpr_index) {
195    switch (gpr_index) {
196      case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
197      case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
198      case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
199      case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
200      case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
201      default:
202      LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
203      return 0;
204    }
205  }
206#else
207#error "Unsupported architecture"
208#endif
209
210 public:
211  static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
212      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
213    DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
214    uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
215    return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
216  }
217
218  // For the given quick ref and args quick frame, return the caller's PC.
219  static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
220      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
221    DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
222    uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
223    return *reinterpret_cast<uintptr_t*>(lr);
224  }
225
226  QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
227                       uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
228          is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
229          gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
230          fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
231          stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
232              + sizeof(StackReference<mirror::ArtMethod>)),  // Skip StackReference<ArtMethod>.
233          gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
234          cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
235    static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
236                  "Number of Quick FPR arguments unexpected");
237    static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
238                  "Double alignment unexpected");
239    // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
240    // next register is even.
241    static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
242                  "Number of Quick FPR arguments not even");
243  }
244
245  virtual ~QuickArgumentVisitor() {}
246
247  virtual void Visit() = 0;
248
249  Primitive::Type GetParamPrimitiveType() const {
250    return cur_type_;
251  }
252
253  uint8_t* GetParamAddress() const {
254    if (!kQuickSoftFloatAbi) {
255      Primitive::Type type = GetParamPrimitiveType();
256      if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
257        if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
258          if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
259            return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
260          }
261        } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
262          return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
263        }
264        return stack_args_ + (stack_index_ * kBytesStackArgLocation);
265      }
266    }
267    if (gpr_index_ < kNumQuickGprArgs) {
268      return gpr_args_ + GprIndexToGprOffset(gpr_index_);
269    }
270    return stack_args_ + (stack_index_ * kBytesStackArgLocation);
271  }
272
273  bool IsSplitLongOrDouble() const {
274    if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
275      return is_split_long_or_double_;
276    } else {
277      return false;  // An optimization for when GPR and FPRs are 64bit.
278    }
279  }
280
281  bool IsParamAReference() const {
282    return GetParamPrimitiveType() == Primitive::kPrimNot;
283  }
284
285  bool IsParamALongOrDouble() const {
286    Primitive::Type type = GetParamPrimitiveType();
287    return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
288  }
289
290  uint64_t ReadSplitLongParam() const {
291    DCHECK(IsSplitLongOrDouble());
292    // Read low half from register.
293    uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
294    // Read high half from the stack. As current stack_index_ indexes the argument, the high part
295    // index should be (stack_index_ + 1).
296    uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_
297        + (stack_index_ + 1) * kBytesStackArgLocation);
298    return (low_half & 0xffffffffULL) | (high_half << 32);
299  }
300
301  void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
302    // (a) 'stack_args_' should point to the first method's argument
303    // (b) whatever the argument type it is, the 'stack_index_' should
304    //     be moved forward along with every visiting.
305    gpr_index_ = 0;
306    fpr_index_ = 0;
307    if (kQuickDoubleRegAlignedFloatBackFilled) {
308      fpr_double_index_ = 0;
309    }
310    stack_index_ = 0;
311    if (!is_static_) {  // Handle this.
312      cur_type_ = Primitive::kPrimNot;
313      is_split_long_or_double_ = false;
314      Visit();
315      stack_index_++;
316      if (kNumQuickGprArgs > 0) {
317        gpr_index_++;
318      }
319    }
320    for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
321      cur_type_ = Primitive::GetType(shorty_[shorty_index]);
322      switch (cur_type_) {
323        case Primitive::kPrimNot:
324        case Primitive::kPrimBoolean:
325        case Primitive::kPrimByte:
326        case Primitive::kPrimChar:
327        case Primitive::kPrimShort:
328        case Primitive::kPrimInt:
329          is_split_long_or_double_ = false;
330          Visit();
331          stack_index_++;
332          if (gpr_index_ < kNumQuickGprArgs) {
333            gpr_index_++;
334          }
335          break;
336        case Primitive::kPrimFloat:
337          is_split_long_or_double_ = false;
338          Visit();
339          stack_index_++;
340          if (kQuickSoftFloatAbi) {
341            if (gpr_index_ < kNumQuickGprArgs) {
342              gpr_index_++;
343            }
344          } else {
345            if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
346              fpr_index_++;
347              if (kQuickDoubleRegAlignedFloatBackFilled) {
348                // Double should not overlap with float.
349                // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
350                fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
351                // Float should not overlap with double.
352                if (fpr_index_ % 2 == 0) {
353                  fpr_index_ = std::max(fpr_double_index_, fpr_index_);
354                }
355              }
356            }
357          }
358          break;
359        case Primitive::kPrimDouble:
360        case Primitive::kPrimLong:
361          if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
362            is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
363                ((gpr_index_ + 1) == kNumQuickGprArgs);
364            Visit();
365            if (kBytesStackArgLocation == 4) {
366              stack_index_+= 2;
367            } else {
368              CHECK_EQ(kBytesStackArgLocation, 8U);
369              stack_index_++;
370            }
371            if (gpr_index_ < kNumQuickGprArgs) {
372              gpr_index_++;
373              if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
374                if (gpr_index_ < kNumQuickGprArgs) {
375                  gpr_index_++;
376                }
377              }
378            }
379          } else {
380            is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
381                ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
382            Visit();
383            if (kBytesStackArgLocation == 4) {
384              stack_index_+= 2;
385            } else {
386              CHECK_EQ(kBytesStackArgLocation, 8U);
387              stack_index_++;
388            }
389            if (kQuickDoubleRegAlignedFloatBackFilled) {
390              if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
391                fpr_double_index_ += 2;
392                // Float should not overlap with double.
393                if (fpr_index_ % 2 == 0) {
394                  fpr_index_ = std::max(fpr_double_index_, fpr_index_);
395                }
396              }
397            } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
398              fpr_index_++;
399              if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
400                if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
401                  fpr_index_++;
402                }
403              }
404            }
405          }
406          break;
407        default:
408          LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
409      }
410    }
411  }
412
413 protected:
414  const bool is_static_;
415  const char* const shorty_;
416  const uint32_t shorty_len_;
417
418 private:
419  uint8_t* const gpr_args_;  // Address of GPR arguments in callee save frame.
420  uint8_t* const fpr_args_;  // Address of FPR arguments in callee save frame.
421  uint8_t* const stack_args_;  // Address of stack arguments in caller's frame.
422  uint32_t gpr_index_;  // Index into spilled GPRs.
423  // Index into spilled FPRs.
424  // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
425  // holds a higher register number.
426  uint32_t fpr_index_;
427  // Index into spilled FPRs for aligned double.
428  // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
429  // terms of singles, may be behind fpr_index.
430  uint32_t fpr_double_index_;
431  uint32_t stack_index_;  // Index into arguments on the stack.
432  // The current type of argument during VisitArguments.
433  Primitive::Type cur_type_;
434  // Does a 64bit parameter straddle the register and stack arguments?
435  bool is_split_long_or_double_;
436};
437
438// Visits arguments on the stack placing them into the shadow frame.
439class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
440 public:
441  BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
442                               const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
443                               size_t first_arg_reg) :
444      QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
445
446  void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
447
448 private:
449  ShadowFrame* const sf_;
450  uint32_t cur_reg_;
451
452  DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
453};
454
455void BuildQuickShadowFrameVisitor::Visit() {
456  Primitive::Type type = GetParamPrimitiveType();
457  switch (type) {
458    case Primitive::kPrimLong:  // Fall-through.
459    case Primitive::kPrimDouble:
460      if (IsSplitLongOrDouble()) {
461        sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
462      } else {
463        sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
464      }
465      ++cur_reg_;
466      break;
467    case Primitive::kPrimNot: {
468        StackReference<mirror::Object>* stack_ref =
469            reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
470        sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
471      }
472      break;
473    case Primitive::kPrimBoolean:  // Fall-through.
474    case Primitive::kPrimByte:     // Fall-through.
475    case Primitive::kPrimChar:     // Fall-through.
476    case Primitive::kPrimShort:    // Fall-through.
477    case Primitive::kPrimInt:      // Fall-through.
478    case Primitive::kPrimFloat:
479      sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
480      break;
481    case Primitive::kPrimVoid:
482      LOG(FATAL) << "UNREACHABLE";
483      UNREACHABLE();
484  }
485  ++cur_reg_;
486}
487
488extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
489                                                StackReference<mirror::ArtMethod>* sp)
490    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
491  // Ensure we don't get thread suspension until the object arguments are safely in the shadow
492  // frame.
493  ScopedQuickEntrypointChecks sqec(self);
494
495  if (method->IsAbstract()) {
496    ThrowAbstractMethodError(method);
497    return 0;
498  } else {
499    DCHECK(!method->IsNative()) << PrettyMethod(method);
500    const char* old_cause = self->StartAssertNoThreadSuspension(
501        "Building interpreter shadow frame");
502    const DexFile::CodeItem* code_item = method->GetCodeItem();
503    DCHECK(code_item != nullptr) << PrettyMethod(method);
504    uint16_t num_regs = code_item->registers_size_;
505    void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
506    // No last shadow coming from quick.
507    ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
508    size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
509    uint32_t shorty_len = 0;
510    const char* shorty = method->GetShorty(&shorty_len);
511    BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
512                                                      shadow_frame, first_arg_reg);
513    shadow_frame_builder.VisitArguments();
514    const bool needs_initialization =
515        method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
516    // Push a transition back into managed code onto the linked list in thread.
517    ManagedStack fragment;
518    self->PushManagedStackFragment(&fragment);
519    self->PushShadowFrame(shadow_frame);
520    self->EndAssertNoThreadSuspension(old_cause);
521
522    if (needs_initialization) {
523      // Ensure static method's class is initialized.
524      StackHandleScope<1> hs(self);
525      Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
526      if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
527        DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
528        self->PopManagedStackFragment(fragment);
529        return 0;
530      }
531    }
532    JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
533    // Pop transition.
534    self->PopManagedStackFragment(fragment);
535    // No need to restore the args since the method has already been run by the interpreter.
536    return result.GetJ();
537  }
538}
539
540// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
541// to jobjects.
542class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
543 public:
544  BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
545                            const char* shorty, uint32_t shorty_len,
546                            ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
547      QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
548
549  void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
550
551  void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
552
553 private:
554  ScopedObjectAccessUnchecked* const soa_;
555  std::vector<jvalue>* const args_;
556  // References which we must update when exiting in case the GC moved the objects.
557  std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
558
559  DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
560};
561
562void BuildQuickArgumentVisitor::Visit() {
563  jvalue val;
564  Primitive::Type type = GetParamPrimitiveType();
565  switch (type) {
566    case Primitive::kPrimNot: {
567      StackReference<mirror::Object>* stack_ref =
568          reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
569      val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
570      references_.push_back(std::make_pair(val.l, stack_ref));
571      break;
572    }
573    case Primitive::kPrimLong:  // Fall-through.
574    case Primitive::kPrimDouble:
575      if (IsSplitLongOrDouble()) {
576        val.j = ReadSplitLongParam();
577      } else {
578        val.j = *reinterpret_cast<jlong*>(GetParamAddress());
579      }
580      break;
581    case Primitive::kPrimBoolean:  // Fall-through.
582    case Primitive::kPrimByte:     // Fall-through.
583    case Primitive::kPrimChar:     // Fall-through.
584    case Primitive::kPrimShort:    // Fall-through.
585    case Primitive::kPrimInt:      // Fall-through.
586    case Primitive::kPrimFloat:
587      val.i = *reinterpret_cast<jint*>(GetParamAddress());
588      break;
589    case Primitive::kPrimVoid:
590      LOG(FATAL) << "UNREACHABLE";
591      UNREACHABLE();
592  }
593  args_->push_back(val);
594}
595
596void BuildQuickArgumentVisitor::FixupReferences() {
597  // Fixup any references which may have changed.
598  for (const auto& pair : references_) {
599    pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
600    soa_->Env()->DeleteLocalRef(pair.first);
601  }
602}
603
604// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
605// which is responsible for recording callee save registers. We explicitly place into jobjects the
606// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
607// field within the proxy object, which will box the primitive arguments and deal with error cases.
608extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
609                                               mirror::Object* receiver,
610                                               Thread* self, StackReference<mirror::ArtMethod>* sp)
611    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
612  DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
613  DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
614  // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
615  const char* old_cause =
616      self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
617  // Register the top of the managed stack, making stack crawlable.
618  DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
619  DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
620            Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
621      << PrettyMethod(proxy_method);
622  self->VerifyStack();
623  // Start new JNI local reference state.
624  JNIEnvExt* env = self->GetJniEnv();
625  ScopedObjectAccessUnchecked soa(env);
626  ScopedJniEnvLocalRefState env_state(env);
627  // Create local ref. copies of proxy method and the receiver.
628  jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
629
630  // Placing arguments into args vector and remove the receiver.
631  mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
632  CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
633                                       << PrettyMethod(non_proxy_method);
634  std::vector<jvalue> args;
635  uint32_t shorty_len = 0;
636  const char* shorty = proxy_method->GetShorty(&shorty_len);
637  BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
638
639  local_ref_visitor.VisitArguments();
640  DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
641  args.erase(args.begin());
642
643  // Convert proxy method into expected interface method.
644  mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
645  DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
646  DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
647  jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
648
649  // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
650  // that performs allocations.
651  self->EndAssertNoThreadSuspension(old_cause);
652  JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
653  // Restore references which might have moved.
654  local_ref_visitor.FixupReferences();
655  return result.GetJ();
656}
657
658// Read object references held in arguments from quick frames and place in a JNI local references,
659// so they don't get garbage collected.
660class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
661 public:
662  RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
663                               const char* shorty, uint32_t shorty_len,
664                               ScopedObjectAccessUnchecked* soa) :
665      QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
666
667  void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
668
669  void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
670
671 private:
672  ScopedObjectAccessUnchecked* const soa_;
673  // References which we must update when exiting in case the GC moved the objects.
674  std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
675
676  DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
677};
678
679void RememberForGcArgumentVisitor::Visit() {
680  if (IsParamAReference()) {
681    StackReference<mirror::Object>* stack_ref =
682        reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
683    jobject reference =
684        soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
685    references_.push_back(std::make_pair(reference, stack_ref));
686  }
687}
688
689void RememberForGcArgumentVisitor::FixupReferences() {
690  // Fixup any references which may have changed.
691  for (const auto& pair : references_) {
692    pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
693    soa_->Env()->DeleteLocalRef(pair.first);
694  }
695}
696
697// Lazily resolve a method for quick. Called by stub code.
698extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
699                                                    mirror::Object* receiver,
700                                                    Thread* self,
701                                                    StackReference<mirror::ArtMethod>* sp)
702    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
703  ScopedQuickEntrypointChecks sqec(self);
704  // Start new JNI local reference state
705  JNIEnvExt* env = self->GetJniEnv();
706  ScopedObjectAccessUnchecked soa(env);
707  ScopedJniEnvLocalRefState env_state(env);
708  const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
709
710  // Compute details about the called method (avoid GCs)
711  ClassLinker* linker = Runtime::Current()->GetClassLinker();
712  mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
713  InvokeType invoke_type;
714  const DexFile* dex_file;
715  uint32_t dex_method_idx;
716  if (called->IsRuntimeMethod()) {
717    uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
718    const DexFile::CodeItem* code;
719    dex_file = caller->GetDexFile();
720    code = caller->GetCodeItem();
721    CHECK_LT(dex_pc, code->insns_size_in_code_units_);
722    const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
723    Instruction::Code instr_code = instr->Opcode();
724    bool is_range;
725    switch (instr_code) {
726      case Instruction::INVOKE_DIRECT:
727        invoke_type = kDirect;
728        is_range = false;
729        break;
730      case Instruction::INVOKE_DIRECT_RANGE:
731        invoke_type = kDirect;
732        is_range = true;
733        break;
734      case Instruction::INVOKE_STATIC:
735        invoke_type = kStatic;
736        is_range = false;
737        break;
738      case Instruction::INVOKE_STATIC_RANGE:
739        invoke_type = kStatic;
740        is_range = true;
741        break;
742      case Instruction::INVOKE_SUPER:
743        invoke_type = kSuper;
744        is_range = false;
745        break;
746      case Instruction::INVOKE_SUPER_RANGE:
747        invoke_type = kSuper;
748        is_range = true;
749        break;
750      case Instruction::INVOKE_VIRTUAL:
751        invoke_type = kVirtual;
752        is_range = false;
753        break;
754      case Instruction::INVOKE_VIRTUAL_RANGE:
755        invoke_type = kVirtual;
756        is_range = true;
757        break;
758      case Instruction::INVOKE_INTERFACE:
759        invoke_type = kInterface;
760        is_range = false;
761        break;
762      case Instruction::INVOKE_INTERFACE_RANGE:
763        invoke_type = kInterface;
764        is_range = true;
765        break;
766      default:
767        LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
768        // Avoid used uninitialized warnings.
769        invoke_type = kDirect;
770        is_range = false;
771    }
772    dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
773  } else {
774    invoke_type = kStatic;
775    dex_file = called->GetDexFile();
776    dex_method_idx = called->GetDexMethodIndex();
777  }
778  uint32_t shorty_len;
779  const char* shorty =
780      dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
781  RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
782  visitor.VisitArguments();
783  self->EndAssertNoThreadSuspension(old_cause);
784  bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
785  // Resolve method filling in dex cache.
786  if (UNLIKELY(called->IsRuntimeMethod())) {
787    StackHandleScope<1> hs(self);
788    mirror::Object* dummy = nullptr;
789    HandleWrapper<mirror::Object> h_receiver(
790        hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
791    called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
792  }
793  const void* code = NULL;
794  if (LIKELY(!self->IsExceptionPending())) {
795    // Incompatible class change should have been handled in resolve method.
796    CHECK(!called->CheckIncompatibleClassChange(invoke_type))
797        << PrettyMethod(called) << " " << invoke_type;
798    if (virtual_or_interface) {
799      // Refine called method based on receiver.
800      CHECK(receiver != nullptr) << invoke_type;
801
802      mirror::ArtMethod* orig_called = called;
803      if (invoke_type == kVirtual) {
804        called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
805      } else {
806        called = receiver->GetClass()->FindVirtualMethodForInterface(called);
807      }
808
809      CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
810                               << PrettyTypeOf(receiver) << " "
811                               << invoke_type << " " << orig_called->GetVtableIndex();
812
813      // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
814      // of the sharpened method.
815      if (called->HasSameDexCacheResolvedMethods(caller)) {
816        caller->SetDexCacheResolvedMethod(called->GetDexMethodIndex(), called);
817      } else {
818        // Calling from one dex file to another, need to compute the method index appropriate to
819        // the caller's dex file. Since we get here only if the original called was a runtime
820        // method, we've got the correct dex_file and a dex_method_idx from above.
821        DCHECK_EQ(caller->GetDexFile(), dex_file);
822        StackHandleScope<1> hs(self);
823        MethodHelper mh(hs.NewHandle(called));
824        uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
825        if (method_index != DexFile::kDexNoIndex) {
826          caller->SetDexCacheResolvedMethod(method_index, called);
827        }
828      }
829    }
830    // Ensure that the called method's class is initialized.
831    StackHandleScope<1> hs(soa.Self());
832    Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
833    linker->EnsureInitialized(soa.Self(), called_class, true, true);
834    if (LIKELY(called_class->IsInitialized())) {
835      code = called->GetEntryPointFromQuickCompiledCode();
836    } else if (called_class->IsInitializing()) {
837      if (invoke_type == kStatic) {
838        // Class is still initializing, go to oat and grab code (trampoline must be left in place
839        // until class is initialized to stop races between threads).
840        code = linker->GetQuickOatCodeFor(called);
841      } else {
842        // No trampoline for non-static methods.
843        code = called->GetEntryPointFromQuickCompiledCode();
844      }
845    } else {
846      DCHECK(called_class->IsErroneous());
847    }
848  }
849  CHECK_EQ(code == NULL, self->IsExceptionPending());
850  // Fixup any locally saved objects may have moved during a GC.
851  visitor.FixupReferences();
852  // Place called method in callee-save frame to be placed as first argument to quick method.
853  sp->Assign(called);
854  return code;
855}
856
857/*
858 * This class uses a couple of observations to unite the different calling conventions through
859 * a few constants.
860 *
861 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
862 *    possible alignment.
863 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
864 *    types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
865 *    when we have to split things
866 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
867 *    and we can use Int handling directly.
868 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
869 *    necessary when widening. Also, widening of Ints will take place implicitly, and the
870 *    extension should be compatible with Aarch64, which mandates copying the available bits
871 *    into LSB and leaving the rest unspecified.
872 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
873 *    the stack.
874 * 6) There is only little endian.
875 *
876 *
877 * Actual work is supposed to be done in a delegate of the template type. The interface is as
878 * follows:
879 *
880 * void PushGpr(uintptr_t):   Add a value for the next GPR
881 *
882 * void PushFpr4(float):      Add a value for the next FPR of size 32b. Is only called if we need
883 *                            padding, that is, think the architecture is 32b and aligns 64b.
884 *
885 * void PushFpr8(uint64_t):   Push a double. We _will_ call this on 32b, it's the callee's job to
886 *                            split this if necessary. The current state will have aligned, if
887 *                            necessary.
888 *
889 * void PushStack(uintptr_t): Push a value to the stack.
890 *
891 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
892 *                                          as this might be important for null initialization.
893 *                                          Must return the jobject, that is, the reference to the
894 *                                          entry in the HandleScope (nullptr if necessary).
895 *
896 */
897template<class T> class BuildNativeCallFrameStateMachine {
898 public:
899#if defined(__arm__)
900  // TODO: These are all dummy values!
901  static constexpr bool kNativeSoftFloatAbi = true;
902  static constexpr size_t kNumNativeGprArgs = 4;  // 4 arguments passed in GPRs, r0-r3
903  static constexpr size_t kNumNativeFprArgs = 0;  // 0 arguments passed in FPRs.
904
905  static constexpr size_t kRegistersNeededForLong = 2;
906  static constexpr size_t kRegistersNeededForDouble = 2;
907  static constexpr bool kMultiRegistersAligned = true;
908  static constexpr bool kMultiRegistersWidened = false;
909  static constexpr bool kAlignLongOnStack = true;
910  static constexpr bool kAlignDoubleOnStack = true;
911#elif defined(__aarch64__)
912  static constexpr bool kNativeSoftFloatAbi = false;  // This is a hard float ABI.
913  static constexpr size_t kNumNativeGprArgs = 8;  // 6 arguments passed in GPRs.
914  static constexpr size_t kNumNativeFprArgs = 8;  // 8 arguments passed in FPRs.
915
916  static constexpr size_t kRegistersNeededForLong = 1;
917  static constexpr size_t kRegistersNeededForDouble = 1;
918  static constexpr bool kMultiRegistersAligned = false;
919  static constexpr bool kMultiRegistersWidened = false;
920  static constexpr bool kAlignLongOnStack = false;
921  static constexpr bool kAlignDoubleOnStack = false;
922#elif defined(__mips__)
923  static constexpr bool kNativeSoftFloatAbi = true;  // This is a hard float ABI.
924  static constexpr size_t kNumNativeGprArgs = 4;  // 4 arguments passed in GPRs.
925  static constexpr size_t kNumNativeFprArgs = 0;  // 0 arguments passed in FPRs.
926
927  static constexpr size_t kRegistersNeededForLong = 2;
928  static constexpr size_t kRegistersNeededForDouble = 2;
929  static constexpr bool kMultiRegistersAligned = true;
930  static constexpr bool kMultiRegistersWidened = true;
931  static constexpr bool kAlignLongOnStack = true;
932  static constexpr bool kAlignDoubleOnStack = true;
933#elif defined(__i386__)
934  // TODO: Check these!
935  static constexpr bool kNativeSoftFloatAbi = false;  // Not using int registers for fp
936  static constexpr size_t kNumNativeGprArgs = 0;  // 6 arguments passed in GPRs.
937  static constexpr size_t kNumNativeFprArgs = 0;  // 8 arguments passed in FPRs.
938
939  static constexpr size_t kRegistersNeededForLong = 2;
940  static constexpr size_t kRegistersNeededForDouble = 2;
941  static constexpr bool kMultiRegistersAligned = false;  // x86 not using regs, anyways
942  static constexpr bool kMultiRegistersWidened = false;
943  static constexpr bool kAlignLongOnStack = false;
944  static constexpr bool kAlignDoubleOnStack = false;
945#elif defined(__x86_64__)
946  static constexpr bool kNativeSoftFloatAbi = false;  // This is a hard float ABI.
947  static constexpr size_t kNumNativeGprArgs = 6;  // 6 arguments passed in GPRs.
948  static constexpr size_t kNumNativeFprArgs = 8;  // 8 arguments passed in FPRs.
949
950  static constexpr size_t kRegistersNeededForLong = 1;
951  static constexpr size_t kRegistersNeededForDouble = 1;
952  static constexpr bool kMultiRegistersAligned = false;
953  static constexpr bool kMultiRegistersWidened = false;
954  static constexpr bool kAlignLongOnStack = false;
955  static constexpr bool kAlignDoubleOnStack = false;
956#else
957#error "Unsupported architecture"
958#endif
959
960 public:
961  explicit BuildNativeCallFrameStateMachine(T* delegate)
962      : gpr_index_(kNumNativeGprArgs),
963        fpr_index_(kNumNativeFprArgs),
964        stack_entries_(0),
965        delegate_(delegate) {
966    // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
967    // the next register is even; counting down is just to make the compiler happy...
968    static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
969    static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
970  }
971
972  virtual ~BuildNativeCallFrameStateMachine() {}
973
974  bool HavePointerGpr() const {
975    return gpr_index_ > 0;
976  }
977
978  void AdvancePointer(const void* val) {
979    if (HavePointerGpr()) {
980      gpr_index_--;
981      PushGpr(reinterpret_cast<uintptr_t>(val));
982    } else {
983      stack_entries_++;  // TODO: have a field for pointer length as multiple of 32b
984      PushStack(reinterpret_cast<uintptr_t>(val));
985      gpr_index_ = 0;
986    }
987  }
988
989  bool HaveHandleScopeGpr() const {
990    return gpr_index_ > 0;
991  }
992
993  void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
994    uintptr_t handle = PushHandle(ptr);
995    if (HaveHandleScopeGpr()) {
996      gpr_index_--;
997      PushGpr(handle);
998    } else {
999      stack_entries_++;
1000      PushStack(handle);
1001      gpr_index_ = 0;
1002    }
1003  }
1004
1005  bool HaveIntGpr() const {
1006    return gpr_index_ > 0;
1007  }
1008
1009  void AdvanceInt(uint32_t val) {
1010    if (HaveIntGpr()) {
1011      gpr_index_--;
1012      PushGpr(val);
1013    } else {
1014      stack_entries_++;
1015      PushStack(val);
1016      gpr_index_ = 0;
1017    }
1018  }
1019
1020  bool HaveLongGpr() const {
1021    return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1022  }
1023
1024  bool LongGprNeedsPadding() const {
1025    return kRegistersNeededForLong > 1 &&     // only pad when using multiple registers
1026        kAlignLongOnStack &&                  // and when it needs alignment
1027        (gpr_index_ & 1) == 1;                // counter is odd, see constructor
1028  }
1029
1030  bool LongStackNeedsPadding() const {
1031    return kRegistersNeededForLong > 1 &&     // only pad when using multiple registers
1032        kAlignLongOnStack &&                  // and when it needs 8B alignment
1033        (stack_entries_ & 1) == 1;            // counter is odd
1034  }
1035
1036  void AdvanceLong(uint64_t val) {
1037    if (HaveLongGpr()) {
1038      if (LongGprNeedsPadding()) {
1039        PushGpr(0);
1040        gpr_index_--;
1041      }
1042      if (kRegistersNeededForLong == 1) {
1043        PushGpr(static_cast<uintptr_t>(val));
1044      } else {
1045        PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1046        PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1047      }
1048      gpr_index_ -= kRegistersNeededForLong;
1049    } else {
1050      if (LongStackNeedsPadding()) {
1051        PushStack(0);
1052        stack_entries_++;
1053      }
1054      if (kRegistersNeededForLong == 1) {
1055        PushStack(static_cast<uintptr_t>(val));
1056        stack_entries_++;
1057      } else {
1058        PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1059        PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1060        stack_entries_ += 2;
1061      }
1062      gpr_index_ = 0;
1063    }
1064  }
1065
1066  bool HaveFloatFpr() const {
1067    return fpr_index_ > 0;
1068  }
1069
1070  void AdvanceFloat(float val) {
1071    if (kNativeSoftFloatAbi) {
1072      AdvanceInt(bit_cast<float, uint32_t>(val));
1073    } else {
1074      if (HaveFloatFpr()) {
1075        fpr_index_--;
1076        if (kRegistersNeededForDouble == 1) {
1077          if (kMultiRegistersWidened) {
1078            PushFpr8(bit_cast<double, uint64_t>(val));
1079          } else {
1080            // No widening, just use the bits.
1081            PushFpr8(bit_cast<float, uint64_t>(val));
1082          }
1083        } else {
1084          PushFpr4(val);
1085        }
1086      } else {
1087        stack_entries_++;
1088        if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1089          // Need to widen before storing: Note the "double" in the template instantiation.
1090          // Note: We need to jump through those hoops to make the compiler happy.
1091          DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1092          PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
1093        } else {
1094          PushStack(bit_cast<float, uintptr_t>(val));
1095        }
1096        fpr_index_ = 0;
1097      }
1098    }
1099  }
1100
1101  bool HaveDoubleFpr() const {
1102    return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1103  }
1104
1105  bool DoubleFprNeedsPadding() const {
1106    return kRegistersNeededForDouble > 1 &&     // only pad when using multiple registers
1107        kAlignDoubleOnStack &&                  // and when it needs alignment
1108        (fpr_index_ & 1) == 1;                  // counter is odd, see constructor
1109  }
1110
1111  bool DoubleStackNeedsPadding() const {
1112    return kRegistersNeededForDouble > 1 &&     // only pad when using multiple registers
1113        kAlignDoubleOnStack &&                  // and when it needs 8B alignment
1114        (stack_entries_ & 1) == 1;              // counter is odd
1115  }
1116
1117  void AdvanceDouble(uint64_t val) {
1118    if (kNativeSoftFloatAbi) {
1119      AdvanceLong(val);
1120    } else {
1121      if (HaveDoubleFpr()) {
1122        if (DoubleFprNeedsPadding()) {
1123          PushFpr4(0);
1124          fpr_index_--;
1125        }
1126        PushFpr8(val);
1127        fpr_index_ -= kRegistersNeededForDouble;
1128      } else {
1129        if (DoubleStackNeedsPadding()) {
1130          PushStack(0);
1131          stack_entries_++;
1132        }
1133        if (kRegistersNeededForDouble == 1) {
1134          PushStack(static_cast<uintptr_t>(val));
1135          stack_entries_++;
1136        } else {
1137          PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1138          PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1139          stack_entries_ += 2;
1140        }
1141        fpr_index_ = 0;
1142      }
1143    }
1144  }
1145
1146  uint32_t GetStackEntries() const {
1147    return stack_entries_;
1148  }
1149
1150  uint32_t GetNumberOfUsedGprs() const {
1151    return kNumNativeGprArgs - gpr_index_;
1152  }
1153
1154  uint32_t GetNumberOfUsedFprs() const {
1155    return kNumNativeFprArgs - fpr_index_;
1156  }
1157
1158 private:
1159  void PushGpr(uintptr_t val) {
1160    delegate_->PushGpr(val);
1161  }
1162  void PushFpr4(float val) {
1163    delegate_->PushFpr4(val);
1164  }
1165  void PushFpr8(uint64_t val) {
1166    delegate_->PushFpr8(val);
1167  }
1168  void PushStack(uintptr_t val) {
1169    delegate_->PushStack(val);
1170  }
1171  uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1172    return delegate_->PushHandle(ref);
1173  }
1174
1175  uint32_t gpr_index_;      // Number of free GPRs
1176  uint32_t fpr_index_;      // Number of free FPRs
1177  uint32_t stack_entries_;  // Stack entries are in multiples of 32b, as floats are usually not
1178                            // extended
1179  T* const delegate_;             // What Push implementation gets called
1180};
1181
1182// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1183// in subclasses.
1184//
1185// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1186// them with handles.
1187class ComputeNativeCallFrameSize {
1188 public:
1189  ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1190
1191  virtual ~ComputeNativeCallFrameSize() {}
1192
1193  uint32_t GetStackSize() const {
1194    return num_stack_entries_ * sizeof(uintptr_t);
1195  }
1196
1197  uint8_t* LayoutCallStack(uint8_t* sp8) const {
1198    sp8 -= GetStackSize();
1199    // Align by kStackAlignment.
1200    sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1201    return sp8;
1202  }
1203
1204  uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1205      const {
1206    // Assumption is OK right now, as we have soft-float arm
1207    size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1208    sp8 -= fregs * sizeof(uintptr_t);
1209    *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1210    size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1211    sp8 -= iregs * sizeof(uintptr_t);
1212    *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1213    return sp8;
1214  }
1215
1216  uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
1217                            uint32_t** start_fpr) const {
1218    // Native call stack.
1219    sp8 = LayoutCallStack(sp8);
1220    *start_stack = reinterpret_cast<uintptr_t*>(sp8);
1221
1222    // Put fprs and gprs below.
1223    sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
1224
1225    // Return the new bottom.
1226    return sp8;
1227  }
1228
1229  virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1230      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1231    UNUSED(sm);
1232  }
1233
1234  void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1235    BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1236
1237    WalkHeader(&sm);
1238
1239    for (uint32_t i = 1; i < shorty_len; ++i) {
1240      Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1241      switch (cur_type_) {
1242        case Primitive::kPrimNot:
1243          // TODO: fix abuse of mirror types.
1244          sm.AdvanceHandleScope(
1245              reinterpret_cast<mirror::Object*>(0x12345678));
1246          break;
1247
1248        case Primitive::kPrimBoolean:
1249        case Primitive::kPrimByte:
1250        case Primitive::kPrimChar:
1251        case Primitive::kPrimShort:
1252        case Primitive::kPrimInt:
1253          sm.AdvanceInt(0);
1254          break;
1255        case Primitive::kPrimFloat:
1256          sm.AdvanceFloat(0);
1257          break;
1258        case Primitive::kPrimDouble:
1259          sm.AdvanceDouble(0);
1260          break;
1261        case Primitive::kPrimLong:
1262          sm.AdvanceLong(0);
1263          break;
1264        default:
1265          LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1266      }
1267    }
1268
1269    num_stack_entries_ = sm.GetStackEntries();
1270  }
1271
1272  void PushGpr(uintptr_t /* val */) {
1273    // not optimizing registers, yet
1274  }
1275
1276  void PushFpr4(float /* val */) {
1277    // not optimizing registers, yet
1278  }
1279
1280  void PushFpr8(uint64_t /* val */) {
1281    // not optimizing registers, yet
1282  }
1283
1284  void PushStack(uintptr_t /* val */) {
1285    // counting is already done in the superclass
1286  }
1287
1288  virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
1289    return reinterpret_cast<uintptr_t>(nullptr);
1290  }
1291
1292 protected:
1293  uint32_t num_stack_entries_;
1294};
1295
1296class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
1297 public:
1298  ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
1299
1300  // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1301  // is at *m = sp. Will update to point to the bottom of the save frame.
1302  //
1303  // Note: assumes ComputeAll() has been run before.
1304  void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1305                             HandleScope** handle_scope)
1306      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1307    mirror::ArtMethod* method = (*m)->AsMirrorPtr();
1308
1309    uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1310
1311    // First, fix up the layout of the callee-save frame.
1312    // We have to squeeze in the HandleScope, and relocate the method pointer.
1313
1314    // "Free" the slot for the method.
1315    sp8 += sizeof(void*);  // In the callee-save frame we use a full pointer.
1316
1317    // Under the callee saves put handle scope and new method stack reference.
1318    size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1319    size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1320
1321    sp8 -= scope_and_method;
1322    // Align by kStackAlignment.
1323    sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1324        reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1325
1326    uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1327    *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1328                                        num_handle_scope_references_);
1329
1330    // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1331    uint8_t* method_pointer = sp8;
1332    StackReference<mirror::ArtMethod>* new_method_ref =
1333        reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1334    new_method_ref->Assign(method);
1335    *m = new_method_ref;
1336  }
1337
1338  // Adds space for the cookie. Note: may leave stack unaligned.
1339  void LayoutCookie(uint8_t** sp) const {
1340    // Reference cookie and padding
1341    *sp -= 8;
1342  }
1343
1344  // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1345  // Returns the new bottom. Note: this may be unaligned.
1346  uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1347                              HandleScope** handle_scope)
1348      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1349    // First, fix up the layout of the callee-save frame.
1350    // We have to squeeze in the HandleScope, and relocate the method pointer.
1351    LayoutCalleeSaveFrame(self, m, sp, handle_scope);
1352
1353    // The bottom of the callee-save frame is now where the method is, *m.
1354    uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1355
1356    // Add space for cookie.
1357    LayoutCookie(&sp8);
1358
1359    return sp8;
1360  }
1361
1362  // WARNING: After this, *sp won't be pointing to the method anymore!
1363  uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
1364                         const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
1365                         uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1366      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1367    Walk(shorty, shorty_len);
1368
1369    // JNI part.
1370    uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
1371
1372    sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1373
1374    // Return the new bottom.
1375    return sp8;
1376  }
1377
1378  uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1379
1380  // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1381  void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1382      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1383
1384 private:
1385  uint32_t num_handle_scope_references_;
1386};
1387
1388uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1389  num_handle_scope_references_++;
1390  return reinterpret_cast<uintptr_t>(nullptr);
1391}
1392
1393void ComputeGenericJniFrameSize::WalkHeader(
1394    BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1395  // JNIEnv
1396  sm->AdvancePointer(nullptr);
1397
1398  // Class object or this as first argument
1399  sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1400}
1401
1402// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1403// the template requirements of BuildGenericJniFrameStateMachine.
1404class FillNativeCall {
1405 public:
1406  FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1407      cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1408
1409  virtual ~FillNativeCall() {}
1410
1411  void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1412    cur_gpr_reg_ = gpr_regs;
1413    cur_fpr_reg_ = fpr_regs;
1414    cur_stack_arg_ = stack_args;
1415  }
1416
1417  void PushGpr(uintptr_t val) {
1418    *cur_gpr_reg_ = val;
1419    cur_gpr_reg_++;
1420  }
1421
1422  void PushFpr4(float val) {
1423    *cur_fpr_reg_ = val;
1424    cur_fpr_reg_++;
1425  }
1426
1427  void PushFpr8(uint64_t val) {
1428    uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1429    *tmp = val;
1430    cur_fpr_reg_ += 2;
1431  }
1432
1433  void PushStack(uintptr_t val) {
1434    *cur_stack_arg_ = val;
1435    cur_stack_arg_++;
1436  }
1437
1438  virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1439    LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1440    UNREACHABLE();
1441  }
1442
1443 private:
1444  uintptr_t* cur_gpr_reg_;
1445  uint32_t* cur_fpr_reg_;
1446  uintptr_t* cur_stack_arg_;
1447};
1448
1449// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1450// of transitioning into native code.
1451class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1452 public:
1453  BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1454                              StackReference<mirror::ArtMethod>** sp)
1455     : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1456       jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1457    ComputeGenericJniFrameSize fsc;
1458    uintptr_t* start_gpr_reg;
1459    uint32_t* start_fpr_reg;
1460    uintptr_t* start_stack_arg;
1461    bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
1462                                             &handle_scope_,
1463                                             &start_stack_arg,
1464                                             &start_gpr_reg, &start_fpr_reg);
1465
1466    jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1467
1468    // jni environment is always first argument
1469    sm_.AdvancePointer(self->GetJniEnv());
1470
1471    if (is_static) {
1472      sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1473    }
1474  }
1475
1476  void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1477
1478  void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1479
1480  StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1481      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1482    return handle_scope_->GetHandle(0).GetReference();
1483  }
1484
1485  jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1486    return handle_scope_->GetHandle(0).ToJObject();
1487  }
1488
1489  void* GetBottomOfUsedArea() const {
1490    return bottom_of_used_area_;
1491  }
1492
1493 private:
1494  // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1495  class FillJniCall FINAL : public FillNativeCall {
1496   public:
1497    FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1498                HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1499                                             handle_scope_(handle_scope), cur_entry_(0) {}
1500
1501    uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1502
1503    void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1504      FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1505      handle_scope_ = scope;
1506      cur_entry_ = 0U;
1507    }
1508
1509    void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1510      // Initialize padding entries.
1511      size_t expected_slots = handle_scope_->NumberOfReferences();
1512      while (cur_entry_ < expected_slots) {
1513        handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
1514      }
1515      DCHECK_NE(cur_entry_, 0U);
1516    }
1517
1518   private:
1519    HandleScope* handle_scope_;
1520    size_t cur_entry_;
1521  };
1522
1523  HandleScope* handle_scope_;
1524  FillJniCall jni_call_;
1525  void* bottom_of_used_area_;
1526
1527  BuildNativeCallFrameStateMachine<FillJniCall> sm_;
1528
1529  DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1530};
1531
1532uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1533  uintptr_t tmp;
1534  MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
1535  h.Assign(ref);
1536  tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1537  cur_entry_++;
1538  return tmp;
1539}
1540
1541void BuildGenericJniFrameVisitor::Visit() {
1542  Primitive::Type type = GetParamPrimitiveType();
1543  switch (type) {
1544    case Primitive::kPrimLong: {
1545      jlong long_arg;
1546      if (IsSplitLongOrDouble()) {
1547        long_arg = ReadSplitLongParam();
1548      } else {
1549        long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1550      }
1551      sm_.AdvanceLong(long_arg);
1552      break;
1553    }
1554    case Primitive::kPrimDouble: {
1555      uint64_t double_arg;
1556      if (IsSplitLongOrDouble()) {
1557        // Read into union so that we don't case to a double.
1558        double_arg = ReadSplitLongParam();
1559      } else {
1560        double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1561      }
1562      sm_.AdvanceDouble(double_arg);
1563      break;
1564    }
1565    case Primitive::kPrimNot: {
1566      StackReference<mirror::Object>* stack_ref =
1567          reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1568      sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
1569      break;
1570    }
1571    case Primitive::kPrimFloat:
1572      sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1573      break;
1574    case Primitive::kPrimBoolean:  // Fall-through.
1575    case Primitive::kPrimByte:     // Fall-through.
1576    case Primitive::kPrimChar:     // Fall-through.
1577    case Primitive::kPrimShort:    // Fall-through.
1578    case Primitive::kPrimInt:      // Fall-through.
1579      sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1580      break;
1581    case Primitive::kPrimVoid:
1582      LOG(FATAL) << "UNREACHABLE";
1583      UNREACHABLE();
1584  }
1585}
1586
1587void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
1588  // Clear out rest of the scope.
1589  jni_call_.ResetRemainingScopeSlots();
1590  // Install HandleScope.
1591  self->PushHandleScope(handle_scope_);
1592}
1593
1594#if defined(__arm__) || defined(__aarch64__)
1595extern "C" void* artFindNativeMethod();
1596#else
1597extern "C" void* artFindNativeMethod(Thread* self);
1598#endif
1599
1600uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1601  if (lock != nullptr) {
1602    return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1603  } else {
1604    return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1605  }
1606}
1607
1608void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1609  if (lock != nullptr) {
1610    JniMethodEndSynchronized(cookie, lock, self);
1611  } else {
1612    JniMethodEnd(cookie, self);
1613  }
1614}
1615
1616/*
1617 * Initializes an alloca region assumed to be directly below sp for a native call:
1618 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers.
1619 * The final element on the stack is a pointer to the native code.
1620 *
1621 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
1622 * We need to fix this, as the handle scope needs to go into the callee-save frame.
1623 *
1624 * The return of this function denotes:
1625 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1626 * 2) An error, if the value is negative.
1627 */
1628extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1629                                                      StackReference<mirror::ArtMethod>* sp)
1630    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1631  mirror::ArtMethod* called = sp->AsMirrorPtr();
1632  DCHECK(called->IsNative()) << PrettyMethod(called, true);
1633  uint32_t shorty_len = 0;
1634  const char* shorty = called->GetShorty(&shorty_len);
1635
1636  // Run the visitor and update sp.
1637  BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
1638  visitor.VisitArguments();
1639  visitor.FinalizeHandleScope(self);
1640
1641  // Fix up managed-stack things in Thread.
1642  self->SetTopOfStack(sp);
1643
1644  self->VerifyStack();
1645
1646  // Start JNI, save the cookie.
1647  uint32_t cookie;
1648  if (called->IsSynchronized()) {
1649    cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
1650    if (self->IsExceptionPending()) {
1651      self->PopHandleScope();
1652      // A negative value denotes an error.
1653      return GetTwoWordFailureValue();
1654    }
1655  } else {
1656    cookie = JniMethodStart(self);
1657  }
1658  uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
1659  *(sp32 - 1) = cookie;
1660
1661  // Retrieve the stored native code.
1662  void* nativeCode = called->GetEntryPointFromJni();
1663
1664  // There are two cases for the content of nativeCode:
1665  // 1) Pointer to the native function.
1666  // 2) Pointer to the trampoline for native code binding.
1667  // In the second case, we need to execute the binding and continue with the actual native function
1668  // pointer.
1669  DCHECK(nativeCode != nullptr);
1670  if (nativeCode == GetJniDlsymLookupStub()) {
1671#if defined(__arm__) || defined(__aarch64__)
1672    nativeCode = artFindNativeMethod();
1673#else
1674    nativeCode = artFindNativeMethod(self);
1675#endif
1676
1677    if (nativeCode == nullptr) {
1678      DCHECK(self->IsExceptionPending());    // There should be an exception pending now.
1679
1680      // End JNI, as the assembly will move to deliver the exception.
1681      jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
1682      if (shorty[0] == 'L') {
1683        artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1684      } else {
1685        artQuickGenericJniEndJNINonRef(self, cookie, lock);
1686      }
1687
1688      return GetTwoWordFailureValue();
1689    }
1690    // Note that the native code pointer will be automatically set by artFindNativeMethod().
1691  }
1692
1693  // Return native code addr(lo) and bottom of alloca address(hi).
1694  return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1695                                reinterpret_cast<uintptr_t>(nativeCode));
1696}
1697
1698/*
1699 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
1700 * unlocking.
1701 */
1702extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
1703    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1704  StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
1705  uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
1706  mirror::ArtMethod* called = sp->AsMirrorPtr();
1707  uint32_t cookie = *(sp32 - 1);
1708
1709  jobject lock = nullptr;
1710  if (called->IsSynchronized()) {
1711    HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1712        + sizeof(StackReference<mirror::ArtMethod>));
1713    lock = table->GetHandle(0).ToJObject();
1714  }
1715
1716  char return_shorty_char = called->GetShorty()[0];
1717
1718  if (return_shorty_char == 'L') {
1719    return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
1720  } else {
1721    artQuickGenericJniEndJNINonRef(self, cookie, lock);
1722
1723    switch (return_shorty_char) {
1724      case 'F': {
1725        if (kRuntimeISA == kX86) {
1726          // Convert back the result to float.
1727          double d = bit_cast<uint64_t, double>(result_f);
1728          return bit_cast<float, uint32_t>(static_cast<float>(d));
1729        } else {
1730          return result_f;
1731        }
1732      }
1733      case 'D':
1734        return result_f;
1735      case 'Z':
1736        return result.z;
1737      case 'B':
1738        return result.b;
1739      case 'C':
1740        return result.c;
1741      case 'S':
1742        return result.s;
1743      case 'I':
1744        return result.i;
1745      case 'J':
1746        return result.j;
1747      case 'V':
1748        return 0;
1749      default:
1750        LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1751        return 0;
1752    }
1753  }
1754}
1755
1756// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1757// for the method pointer.
1758//
1759// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1760// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
1761
1762template<InvokeType type, bool access_check>
1763static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1764                                     mirror::ArtMethod* caller_method,
1765                                     Thread* self, StackReference<mirror::ArtMethod>* sp);
1766
1767template<InvokeType type, bool access_check>
1768static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1769                                     mirror::ArtMethod* caller_method,
1770                                     Thread* self, StackReference<mirror::ArtMethod>* sp) {
1771  ScopedQuickEntrypointChecks sqec(self);
1772  DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
1773  mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1774                                             type);
1775  if (UNLIKELY(method == nullptr)) {
1776    const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1777    uint32_t shorty_len;
1778    const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
1779    {
1780      // Remember the args in case a GC happens in FindMethodFromCode.
1781      ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1782      RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1783      visitor.VisitArguments();
1784      method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1785                                                      self);
1786      visitor.FixupReferences();
1787    }
1788
1789    if (UNLIKELY(method == NULL)) {
1790      CHECK(self->IsExceptionPending());
1791      return GetTwoWordFailureValue();  // Failure.
1792    }
1793  }
1794  DCHECK(!self->IsExceptionPending());
1795  const void* code = method->GetEntryPointFromQuickCompiledCode();
1796
1797  // When we return, the caller will branch to this address, so it had better not be 0!
1798  DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1799                          << " location: "
1800                          << method->GetDexFile()->GetLocation();
1801
1802  return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1803                                reinterpret_cast<uintptr_t>(method));
1804}
1805
1806// Explicit artInvokeCommon template function declarations to please analysis tool.
1807#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check)                                \
1808  template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)                                          \
1809  TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx,                        \
1810                                                    mirror::Object* this_object,                \
1811                                                    mirror::ArtMethod* caller_method,           \
1812                                                    Thread* self,                               \
1813                                                    StackReference<mirror::ArtMethod>* sp)      \
1814
1815EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1816EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1817EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1818EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1819EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1820EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1821EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1822EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1823EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1824EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1825#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1826
1827// See comments in runtime_support_asm.S
1828extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1829    uint32_t method_idx, mirror::Object* this_object,
1830    mirror::ArtMethod* caller_method, Thread* self,
1831    StackReference<mirror::ArtMethod>* sp)
1832        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1833  return artInvokeCommon<kInterface, true>(method_idx, this_object,
1834                                           caller_method, self, sp);
1835}
1836
1837extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1838    uint32_t method_idx, mirror::Object* this_object,
1839    mirror::ArtMethod* caller_method, Thread* self,
1840    StackReference<mirror::ArtMethod>* sp)
1841        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1842  return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1843                                        self, sp);
1844}
1845
1846extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1847    uint32_t method_idx, mirror::Object* this_object,
1848    mirror::ArtMethod* caller_method, Thread* self,
1849    StackReference<mirror::ArtMethod>* sp)
1850        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1851  return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1852                                        self, sp);
1853}
1854
1855extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1856    uint32_t method_idx, mirror::Object* this_object,
1857    mirror::ArtMethod* caller_method, Thread* self,
1858    StackReference<mirror::ArtMethod>* sp)
1859        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1860  return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1861                                       self, sp);
1862}
1863
1864extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1865    uint32_t method_idx, mirror::Object* this_object,
1866    mirror::ArtMethod* caller_method, Thread* self,
1867    StackReference<mirror::ArtMethod>* sp)
1868        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1869  return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1870                                         self, sp);
1871}
1872
1873// Determine target of interface dispatch. This object is known non-null.
1874extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
1875                                                      mirror::Object* this_object,
1876                                                      mirror::ArtMethod* caller_method,
1877                                                      Thread* self,
1878                                                      StackReference<mirror::ArtMethod>* sp)
1879    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1880  ScopedQuickEntrypointChecks sqec(self);
1881  mirror::ArtMethod* method;
1882  if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1883    method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1884    if (UNLIKELY(method == NULL)) {
1885      ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1886                                                                 caller_method);
1887      return GetTwoWordFailureValue();  // Failure.
1888    }
1889  } else {
1890    DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
1891
1892    // Find the caller PC.
1893    constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
1894    uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
1895
1896    // Map the caller PC to a dex PC.
1897    uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
1898    const DexFile::CodeItem* code = caller_method->GetCodeItem();
1899    CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1900    const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1901    Instruction::Code instr_code = instr->Opcode();
1902    CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1903          instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1904        << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1905    uint32_t dex_method_idx;
1906    if (instr_code == Instruction::INVOKE_INTERFACE) {
1907      dex_method_idx = instr->VRegB_35c();
1908    } else {
1909      DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1910      dex_method_idx = instr->VRegB_3rc();
1911    }
1912
1913    const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1914        ->GetDexFile();
1915    uint32_t shorty_len;
1916    const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1917                                                   &shorty_len);
1918    {
1919      // Remember the args in case a GC happens in FindMethodFromCode.
1920      ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1921      RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1922      visitor.VisitArguments();
1923      method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
1924                                                     self);
1925      visitor.FixupReferences();
1926    }
1927
1928    if (UNLIKELY(method == nullptr)) {
1929      CHECK(self->IsExceptionPending());
1930      return GetTwoWordFailureValue();  // Failure.
1931    }
1932  }
1933  const void* code = method->GetEntryPointFromQuickCompiledCode();
1934
1935  // When we return, the caller will branch to this address, so it had better not be 0!
1936  DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1937                          << " location: " << method->GetDexFile()->GetLocation();
1938
1939  return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1940                                reinterpret_cast<uintptr_t>(method));
1941}
1942
1943}  // namespace art
1944