blob: 46561980719589fbb6ede90a68294d27ab697cf2 [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);
30 } else {
31 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
32 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
33 // Determine method index from calling dex instruction.
34#if defined(__arm__)
35 // On entry the stack pointed by sp is:
36 // | argN | |
37 // | ... | |
38 // | arg4 | |
39 // | arg3 spill | | Caller's frame
40 // | arg2 spill | |
41 // | arg1 spill | |
42 // | Method* | ---
43 // | LR |
44 // | ... | callee saves
45 // | R3 | arg3
46 // | R2 | arg2
47 // | R1 | arg1
48 // | R0 |
49 // | Method* | <- sp
50 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
51 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
52 uintptr_t caller_pc = regs[10];
53#elif defined(__i386__)
54 // On entry the stack pointed by sp is:
55 // | argN | |
56 // | ... | |
57 // | arg4 | |
58 // | arg3 spill | | Caller's frame
59 // | arg2 spill | |
60 // | arg1 spill | |
61 // | Method* | ---
62 // | Return |
63 // | EBP,ESI,EDI | callee saves
64 // | EBX | arg3
65 // | EDX | arg2
66 // | ECX | arg1
67 // | EAX/Method* | <- sp
68 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
69 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
70 uintptr_t caller_pc = regs[7];
71#else
72 UNIMPLEMENTED(FATAL);
73 uintptr_t caller_pc = 0;
74#endif
75 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
76 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
77 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
78 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
79 Instruction::Code instr_code = instr->Opcode();
80 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
81 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
82 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
83 DecodedInstruction dec_insn(instr);
84 uint32_t dex_method_idx = dec_insn.vB;
85 method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self,
86 false, kInterface);
87 if (UNLIKELY(method == NULL)) {
88 CHECK(self->IsExceptionPending());
89 return 0; // failure
90 }
91 }
92 const void* code = method->GetCode();
93
94#ifndef NDEBUG
95 // When we return, the caller will branch to this address, so it had better not be 0!
96 if (UNLIKELY(code == NULL)) {
97 MethodHelper mh(method);
98 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
99 << " location: " << mh.GetDexFile().GetLocation();
100 }
101#endif
102
103 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
104 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
105 uint64_t result = ((code_uint << 32) | method_uint);
106 return result;
107}
108
109
Mathieu Chartier66f19252012-09-18 08:57:04 -0700110static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, AbstractMethod* caller_method,
111 Thread* self, AbstractMethod** sp, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700113 AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114 if (UNLIKELY(method == NULL)) {
115 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
116 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700117 ThrowNullPointerExceptionForMethodAccess(caller_method, method_idx, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700118 return 0; // failure
119 }
120 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
121 if (UNLIKELY(method == NULL)) {
122 CHECK(self->IsExceptionPending());
123 return 0; // failure
124 }
125 }
126 DCHECK(!self->IsExceptionPending());
127 const void* code = method->GetCode();
128
Ian Rogers137e88f2012-10-08 17:46:47 -0700129#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700130 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700131 if (UNLIKELY(code == NULL)) {
132 MethodHelper mh(method);
133 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
134 << " location: " << mh.GetDexFile().GetLocation();
135 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700136#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700137
138 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
139 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
140 uint64_t result = ((code_uint << 32) | method_uint);
141 return result;
142}
143
144// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700145extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
146 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700147 AbstractMethod* caller_method,
148 Thread* self,
149 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700151 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
152}
153
154
155extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
156 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700157 AbstractMethod* caller_method,
158 Thread* self,
159 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700161 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
162}
163
164extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
165 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700166 AbstractMethod* caller_method,
167 Thread* self,
168 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700170 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
171}
172
173extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
174 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700175 AbstractMethod* caller_method,
176 Thread* self,
177 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700179 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
180}
181
182extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
183 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700184 AbstractMethod* caller_method,
185 Thread* self,
186 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700188 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
189}
190
191} // namespace art