blob: 656df8de5b3ad257e270e617bb586725806dd4dc [file] [log] [blame]
Ian Rogers7db619b2013-01-16 18:35:48 -08001/*
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
Ian Rogers166db042013-07-26 12:05:57 -070017#include "quick_argument_visitor.h"
Ian Rogers7db619b2013-01-16 18:35:48 -080018#include "callee_save_frame.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070019#include "dex_file-inl.h"
Ian Rogers7db619b2013-01-16 18:35:48 -080020#include "interpreter/interpreter.h"
Jeff Hao16743632013-05-08 10:59:04 -070021#include "invoke_arg_array_builder.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/abstract_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "mirror/class-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/object-inl.h"
Ian Rogersf3e98552013-03-20 15:49:49 -070025#include "mirror/object_array-inl.h"
Ian Rogers7db619b2013-01-16 18:35:48 -080026#include "object_utils.h"
27
28namespace art {
29
30// Visits arguments on the stack placing them into the shadow frame.
Jeff Hao5fa60c32013-04-04 17:57:01 -070031class BuildShadowFrameVisitor : public QuickArgumentVisitor {
Ian Rogers7db619b2013-01-16 18:35:48 -080032 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 BuildShadowFrameVisitor(MethodHelper& caller_mh, mirror::AbstractMethod** sp,
Ian Rogers7db619b2013-01-16 18:35:48 -080034 ShadowFrame& sf, size_t first_arg_reg) :
Jeff Hao5fa60c32013-04-04 17:57:01 -070035 QuickArgumentVisitor(caller_mh, sp), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers7db619b2013-01-16 18:35:48 -080036
37 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
38 Primitive::Type type = GetParamPrimitiveType();
39 switch (type) {
40 case Primitive::kPrimLong: // Fall-through.
41 case Primitive::kPrimDouble:
42 if (IsSplitLongOrDouble()) {
43 sf_.SetVRegLong(cur_reg_, ReadSplitLongParam());
44 } else {
45 sf_.SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
46 }
Ian Rogersf3e98552013-03-20 15:49:49 -070047 ++cur_reg_;
Ian Rogers7db619b2013-01-16 18:35:48 -080048 break;
49 case Primitive::kPrimNot:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 sf_.SetVRegReference(cur_reg_, *reinterpret_cast<mirror::Object**>(GetParamAddress()));
Ian Rogers7db619b2013-01-16 18:35:48 -080051 break;
52 case Primitive::kPrimBoolean: // Fall-through.
53 case Primitive::kPrimByte: // Fall-through.
54 case Primitive::kPrimChar: // Fall-through.
55 case Primitive::kPrimShort: // Fall-through.
56 case Primitive::kPrimInt: // Fall-through.
57 case Primitive::kPrimFloat:
58 sf_.SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
59 break;
60 case Primitive::kPrimVoid:
61 LOG(FATAL) << "UNREACHABLE";
62 break;
63 }
64 ++cur_reg_;
65 }
66
67 private:
68 ShadowFrame& sf_;
69 size_t cur_reg_;
70
71 DISALLOW_COPY_AND_ASSIGN(BuildShadowFrameVisitor);
72};
73
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074extern "C" uint64_t artInterpreterEntry(mirror::AbstractMethod* method, Thread* self,
75 mirror::AbstractMethod** sp)
Ian Rogers7db619b2013-01-16 18:35:48 -080076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
78 // frame.
79 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
80 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
81
82 MethodHelper mh(method);
83 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao66135192013-05-14 11:02:41 -070084 uint16_t num_regs = code_item->registers_size_;
85 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Brian Carlstrom7934ac22013-07-26 10:54:15 -070086 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
Jeff Hao66135192013-05-14 11:02:41 -070087 method, 0, memory));
Ian Rogers7db619b2013-01-16 18:35:48 -080088 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Jeff Hao66135192013-05-14 11:02:41 -070089 BuildShadowFrameVisitor shadow_frame_builder(mh, sp, *shadow_frame, first_arg_reg);
Ian Rogers7db619b2013-01-16 18:35:48 -080090 shadow_frame_builder.VisitArguments();
91 // Push a transition back into managed code onto the linked list in thread.
92 ManagedStack fragment;
93 self->PushManagedStackFragment(&fragment);
Jeff Hao66135192013-05-14 11:02:41 -070094 self->PushShadowFrame(shadow_frame);
Ian Rogers7db619b2013-01-16 18:35:48 -080095 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogersf3e98552013-03-20 15:49:49 -070096
97 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
98 // Ensure static method's class is initialized.
99 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),
100 true, true)) {
101 DCHECK(Thread::Current()->IsExceptionPending());
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200102 self->PopManagedStackFragment(fragment);
Ian Rogersf3e98552013-03-20 15:49:49 -0700103 return 0;
104 }
105 }
106
Jeff Hao66135192013-05-14 11:02:41 -0700107 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
Ian Rogers7db619b2013-01-16 18:35:48 -0800108 // Pop transition.
109 self->PopManagedStackFragment(fragment);
110 return result.GetJ();
111}
112
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700113extern "C" void artInterpreterToQuickEntry(Thread* self, MethodHelper& mh,
114 const DexFile::CodeItem* code_item,
115 ShadowFrame* shadow_frame, JValue* result)
Jeff Hao16743632013-05-08 10:59:04 -0700116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 mirror::AbstractMethod* method = shadow_frame->GetMethod();
Jeff Haofb2802d2013-07-24 13:53:05 -0700118 // Ensure static methods are initialized.
119 if (method->IsStatic()) {
120 Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(), true, true);
121 }
Jeff Hao16743632013-05-08 10:59:04 -0700122 uint16_t arg_offset = (code_item == NULL) ? 0 : code_item->registers_size_ - code_item->ins_size_;
123 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
124 arg_array.BuildArgArray(shadow_frame, arg_offset);
Jeff Hao69510672013-05-21 17:34:55 -0700125 method->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result, mh.GetShorty()[0]);
Jeff Hao16743632013-05-08 10:59:04 -0700126}
127
Ian Rogers7db619b2013-01-16 18:35:48 -0800128} // namespace art