blob: e66749d4a3f533c89b4e2b68f051221cd0c6cf91 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
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 "runtime_support.h"
19
20namespace art {
21
Ian Rogers137e88f2012-10-08 17:46:47 -070022// Determine target of interface dispatch. This object is known non-null.
23extern "C" uint64_t artInvokeInterfaceTrampoline(AbstractMethod* interface_method,
24 Object* this_object, AbstractMethod* caller_method,
25 Thread* self, AbstractMethod** sp)
26 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
27 AbstractMethod* method;
28 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex16)) {
29 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogersa6389412012-10-11 21:35:03 -070030 if (UNLIKELY(method == NULL)) {
31 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
32 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
33 caller_method);
34 return 0; // Failure.
35 }
Ian Rogers137e88f2012-10-08 17:46:47 -070036 } else {
37 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
38 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
39 // Determine method index from calling dex instruction.
40#if defined(__arm__)
41 // On entry the stack pointed by sp is:
42 // | argN | |
43 // | ... | |
44 // | arg4 | |
45 // | arg3 spill | | Caller's frame
46 // | arg2 spill | |
47 // | arg1 spill | |
48 // | Method* | ---
49 // | LR |
50 // | ... | callee saves
51 // | R3 | arg3
52 // | R2 | arg2
53 // | R1 | arg1
54 // | R0 |
55 // | Method* | <- sp
56 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
57 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
58 uintptr_t caller_pc = regs[10];
59#elif defined(__i386__)
60 // On entry the stack pointed by sp is:
61 // | argN | |
62 // | ... | |
63 // | arg4 | |
64 // | arg3 spill | | Caller's frame
65 // | arg2 spill | |
66 // | arg1 spill | |
67 // | Method* | ---
68 // | Return |
69 // | EBP,ESI,EDI | callee saves
70 // | EBX | arg3
71 // | EDX | arg2
72 // | ECX | arg1
73 // | EAX/Method* | <- sp
74 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
75 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
76 uintptr_t caller_pc = regs[7];
77#else
78 UNIMPLEMENTED(FATAL);
79 uintptr_t caller_pc = 0;
80#endif
81 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
82 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
83 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
84 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
85 Instruction::Code instr_code = instr->Opcode();
86 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
87 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
88 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
89 DecodedInstruction dec_insn(instr);
90 uint32_t dex_method_idx = dec_insn.vB;
91 method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self,
92 false, kInterface);
93 if (UNLIKELY(method == NULL)) {
94 CHECK(self->IsExceptionPending());
Ian Rogersa6389412012-10-11 21:35:03 -070095 return 0; // Failure.
Ian Rogers137e88f2012-10-08 17:46:47 -070096 }
97 }
98 const void* code = method->GetCode();
99
100#ifndef NDEBUG
101 // When we return, the caller will branch to this address, so it had better not be 0!
102 if (UNLIKELY(code == NULL)) {
103 MethodHelper mh(method);
104 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
105 << " location: " << mh.GetDexFile().GetLocation();
106 }
107#endif
108
109 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
110 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
111 uint64_t result = ((code_uint << 32) | method_uint);
112 return result;
113}
114
115
Mathieu Chartier66f19252012-09-18 08:57:04 -0700116static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, AbstractMethod* caller_method,
117 Thread* self, AbstractMethod** sp, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700119 AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700120 if (UNLIKELY(method == NULL)) {
121 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
122 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700123 ThrowNullPointerExceptionForMethodAccess(caller_method, method_idx, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700124 return 0; // failure
125 }
126 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
127 if (UNLIKELY(method == NULL)) {
128 CHECK(self->IsExceptionPending());
129 return 0; // failure
130 }
131 }
132 DCHECK(!self->IsExceptionPending());
133 const void* code = method->GetCode();
134
Ian Rogers137e88f2012-10-08 17:46:47 -0700135#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700136 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700137 if (UNLIKELY(code == NULL)) {
138 MethodHelper mh(method);
139 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
140 << " location: " << mh.GetDexFile().GetLocation();
141 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700142#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700143
144 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
145 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
146 uint64_t result = ((code_uint << 32) | method_uint);
147 return result;
148}
149
150// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700151extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
152 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700153 AbstractMethod* caller_method,
154 Thread* self,
155 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700157 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
158}
159
160
161extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
162 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700163 AbstractMethod* caller_method,
164 Thread* self,
165 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700167 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
168}
169
170extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
171 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 AbstractMethod* caller_method,
173 Thread* self,
174 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700176 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
177}
178
179extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
180 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700181 AbstractMethod* caller_method,
182 Thread* self,
183 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700185 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
186}
187
188extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
189 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700190 AbstractMethod* caller_method,
191 Thread* self,
192 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700194 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
195}
196
197} // namespace art