blob: 4f16afee4e6ac650092a798d1c7287ed582cdc4d [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];
jeffhaofa147e22012-10-12 17:03:32 -070077#elif defined(__mips__)
78 // On entry the stack pointed by sp is:
79 // | argN | |
80 // | ... | |
81 // | arg4 | |
82 // | arg3 spill | | Caller's frame
83 // | arg2 spill | |
84 // | arg1 spill | |
85 // | Method* | ---
86 // | RA |
87 // | ... | callee saves
88 // | A3 | arg3
89 // | A2 | arg2
90 // | A1 | arg1
91 // | A0/Method* | <- sp
92 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
93 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
94 uintptr_t caller_pc = regs[11];
Ian Rogers137e88f2012-10-08 17:46:47 -070095#else
96 UNIMPLEMENTED(FATAL);
97 uintptr_t caller_pc = 0;
98#endif
99 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
100 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
101 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
102 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
103 Instruction::Code instr_code = instr->Opcode();
104 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
105 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
106 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
107 DecodedInstruction dec_insn(instr);
108 uint32_t dex_method_idx = dec_insn.vB;
109 method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self,
110 false, kInterface);
111 if (UNLIKELY(method == NULL)) {
112 CHECK(self->IsExceptionPending());
Ian Rogersa6389412012-10-11 21:35:03 -0700113 return 0; // Failure.
Ian Rogers137e88f2012-10-08 17:46:47 -0700114 }
115 }
116 const void* code = method->GetCode();
117
118#ifndef NDEBUG
119 // When we return, the caller will branch to this address, so it had better not be 0!
120 if (UNLIKELY(code == NULL)) {
121 MethodHelper mh(method);
122 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
123 << " location: " << mh.GetDexFile().GetLocation();
124 }
125#endif
126
127 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
128 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
129 uint64_t result = ((code_uint << 32) | method_uint);
130 return result;
131}
132
133
Mathieu Chartier66f19252012-09-18 08:57:04 -0700134static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, AbstractMethod* caller_method,
135 Thread* self, AbstractMethod** sp, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700137 AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700138 if (UNLIKELY(method == NULL)) {
139 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
140 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700141 ThrowNullPointerExceptionForMethodAccess(caller_method, method_idx, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700142 return 0; // failure
143 }
144 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
145 if (UNLIKELY(method == NULL)) {
146 CHECK(self->IsExceptionPending());
147 return 0; // failure
148 }
149 }
150 DCHECK(!self->IsExceptionPending());
151 const void* code = method->GetCode();
152
Ian Rogers137e88f2012-10-08 17:46:47 -0700153#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700154 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700155 if (UNLIKELY(code == NULL)) {
156 MethodHelper mh(method);
157 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
158 << " location: " << mh.GetDexFile().GetLocation();
159 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700160#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700161
162 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
163 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
164 uint64_t result = ((code_uint << 32) | method_uint);
165 return result;
166}
167
168// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700169extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
170 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700171 AbstractMethod* caller_method,
172 Thread* self,
173 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700175 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
176}
177
178
179extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(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, kDirect);
186}
187
188extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(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, kStatic);
195}
196
197extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
198 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700199 AbstractMethod* caller_method,
200 Thread* self,
201 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700203 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
204}
205
206extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
207 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700208 AbstractMethod* caller_method,
209 Thread* self,
210 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700212 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
213}
214
215} // namespace art