blob: 7a49489f8d7ac77fc8a39e6f36832f899b183a65 [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);
Ian Rogers57b86d42012-03-27 16:05:41 -0700140 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
141 if (UNLIKELY(method == NULL)) {
142 CHECK(self->IsExceptionPending());
143 return 0; // failure
144 }
145 }
146 DCHECK(!self->IsExceptionPending());
147 const void* code = method->GetCode();
148
Ian Rogers137e88f2012-10-08 17:46:47 -0700149#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700150 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700151 if (UNLIKELY(code == NULL)) {
152 MethodHelper mh(method);
153 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
154 << " location: " << mh.GetDexFile().GetLocation();
155 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700156#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700157
158 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
159 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
160 uint64_t result = ((code_uint << 32) | method_uint);
161 return result;
162}
163
164// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700165extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
166 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700167 AbstractMethod* caller_method,
168 Thread* self,
169 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700171 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
172}
173
174
175extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
176 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700177 AbstractMethod* caller_method,
178 Thread* self,
179 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700181 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
182}
183
184extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
185 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700186 AbstractMethod* caller_method,
187 Thread* self,
188 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700190 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
191}
192
193extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
194 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700195 AbstractMethod* caller_method,
196 Thread* self,
197 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700199 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
200}
201
202extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
203 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700204 AbstractMethod* caller_method,
205 Thread* self,
206 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700208 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
209}
210
211} // namespace art