blob: 438ac8f4c3e23ae678cf31758a36bbc67e8aa314 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "dex_instruction.h"
19#include "mirror/class-inl.h"
20#include "mirror/abstract_method-inl.h"
21#include "mirror/object-inl.h"
22#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070023#include "runtime_support.h"
24
25namespace art {
26
Ian Rogers137e88f2012-10-08 17:46:47 -070027// Determine target of interface dispatch. This object is known non-null.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::AbstractMethod* interface_method,
29 mirror::Object* this_object,
30 mirror::AbstractMethod* caller_method,
31 Thread* self, mirror::AbstractMethod** sp)
Ian Rogers137e88f2012-10-08 17:46:47 -070032 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 mirror::AbstractMethod* method;
Ian Rogers137e88f2012-10-08 17:46:47 -070034 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex16)) {
35 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogersa6389412012-10-11 21:35:03 -070036 if (UNLIKELY(method == NULL)) {
37 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
38 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
39 caller_method);
40 return 0; // Failure.
41 }
Ian Rogers137e88f2012-10-08 17:46:47 -070042 } else {
43 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
44 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
45 // Determine method index from calling dex instruction.
46#if defined(__arm__)
47 // On entry the stack pointed by sp is:
48 // | argN | |
49 // | ... | |
50 // | arg4 | |
51 // | arg3 spill | | Caller's frame
52 // | arg2 spill | |
53 // | arg1 spill | |
54 // | Method* | ---
55 // | LR |
56 // | ... | callee saves
57 // | R3 | arg3
58 // | R2 | arg2
59 // | R1 | arg1
60 // | R0 |
61 // | Method* | <- sp
62 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
63 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
64 uintptr_t caller_pc = regs[10];
65#elif defined(__i386__)
66 // On entry the stack pointed by sp is:
67 // | argN | |
68 // | ... | |
69 // | arg4 | |
70 // | arg3 spill | | Caller's frame
71 // | arg2 spill | |
72 // | arg1 spill | |
73 // | Method* | ---
74 // | Return |
75 // | EBP,ESI,EDI | callee saves
76 // | EBX | arg3
77 // | EDX | arg2
78 // | ECX | arg1
79 // | EAX/Method* | <- sp
80 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
81 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
82 uintptr_t caller_pc = regs[7];
jeffhaofa147e22012-10-12 17:03:32 -070083#elif defined(__mips__)
84 // On entry the stack pointed by sp is:
85 // | argN | |
86 // | ... | |
87 // | arg4 | |
88 // | arg3 spill | | Caller's frame
89 // | arg2 spill | |
90 // | arg1 spill | |
91 // | Method* | ---
92 // | RA |
93 // | ... | callee saves
94 // | A3 | arg3
95 // | A2 | arg2
96 // | A1 | arg1
97 // | A0/Method* | <- sp
98 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
99 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
100 uintptr_t caller_pc = regs[11];
Ian Rogers137e88f2012-10-08 17:46:47 -0700101#else
102 UNIMPLEMENTED(FATAL);
103 uintptr_t caller_pc = 0;
104#endif
105 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
106 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
107 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
108 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
109 Instruction::Code instr_code = instr->Opcode();
110 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
111 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
112 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
113 DecodedInstruction dec_insn(instr);
114 uint32_t dex_method_idx = dec_insn.vB;
115 method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self,
116 false, kInterface);
117 if (UNLIKELY(method == NULL)) {
118 CHECK(self->IsExceptionPending());
Ian Rogersa6389412012-10-11 21:35:03 -0700119 return 0; // Failure.
Ian Rogers137e88f2012-10-08 17:46:47 -0700120 }
121 }
122 const void* code = method->GetCode();
123
124#ifndef NDEBUG
125 // When we return, the caller will branch to this address, so it had better not be 0!
126 if (UNLIKELY(code == NULL)) {
127 MethodHelper mh(method);
128 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
129 << " location: " << mh.GetDexFile().GetLocation();
130 }
131#endif
132
133 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
134 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
135 uint64_t result = ((code_uint << 32) | method_uint);
136 return result;
137}
138
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
141 mirror::AbstractMethod* caller_method,
142 Thread* self, mirror::AbstractMethod** sp, bool access_check,
143 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method,
146 access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700147 if (UNLIKELY(method == NULL)) {
148 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers57b86d42012-03-27 16:05:41 -0700149 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
150 if (UNLIKELY(method == NULL)) {
151 CHECK(self->IsExceptionPending());
152 return 0; // failure
153 }
154 }
155 DCHECK(!self->IsExceptionPending());
156 const void* code = method->GetCode();
157
Ian Rogers137e88f2012-10-08 17:46:47 -0700158#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700159 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700160 if (UNLIKELY(code == NULL)) {
161 MethodHelper mh(method);
162 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
163 << " location: " << mh.GetDexFile().GetLocation();
164 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700165#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700166
167 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
168 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
169 uint64_t result = ((code_uint << 32) | method_uint);
170 return result;
171}
172
173// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700174extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::Object* this_object,
176 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700177 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700180 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
181}
182
183
184extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 mirror::Object* this_object,
186 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700187 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::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, kDirect);
191}
192
193extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::Object* this_object,
195 mirror::AbstractMethod* caller_method,
196 Thread* self,
197 mirror::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, kStatic);
200}
201
202extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 mirror::Object* this_object,
204 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700205 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 mirror::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, kSuper);
209}
210
211extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 mirror::Object* this_object,
213 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700214 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700217 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
218}
219
220} // namespace art