blob: a96555d8eadcfedbb6402b3af5f2784d2f63b765 [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"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020018#include "dex_instruction-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#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
Jeff Hao1f3bc2f2013-04-30 15:17:19 -070098 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
jeffhaofa147e22012-10-12 17:03:32 -070099 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
Jeff Hao1f3bc2f2013-04-30 15:17:19 -0700100 uintptr_t caller_pc = regs[15];
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);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200113 uint32_t dex_method_idx;
114 if (instr_code == Instruction::INVOKE_INTERFACE) {
115 dex_method_idx = instr->VRegB_35c();
116 } else {
117 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
118 dex_method_idx = instr->VRegB_3rc();
119 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700120 method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self,
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200121 false, kInterface);
Ian Rogers137e88f2012-10-08 17:46:47 -0700122 if (UNLIKELY(method == NULL)) {
123 CHECK(self->IsExceptionPending());
Ian Rogersa6389412012-10-11 21:35:03 -0700124 return 0; // Failure.
Ian Rogers137e88f2012-10-08 17:46:47 -0700125 }
126 }
Jeff Haoaa4a7932013-05-13 11:28:27 -0700127 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers137e88f2012-10-08 17:46:47 -0700128
129#ifndef NDEBUG
130 // When we return, the caller will branch to this address, so it had better not be 0!
131 if (UNLIKELY(code == NULL)) {
132 MethodHelper mh(method);
133 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
134 << " location: " << mh.GetDexFile().GetLocation();
135 }
136#endif
137
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
146 mirror::AbstractMethod* caller_method,
147 Thread* self, mirror::AbstractMethod** sp, bool access_check,
148 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method,
151 access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700152 if (UNLIKELY(method == NULL)) {
153 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers57b86d42012-03-27 16:05:41 -0700154 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
155 if (UNLIKELY(method == NULL)) {
156 CHECK(self->IsExceptionPending());
157 return 0; // failure
158 }
159 }
160 DCHECK(!self->IsExceptionPending());
Jeff Haoaa4a7932013-05-13 11:28:27 -0700161 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers57b86d42012-03-27 16:05:41 -0700162
Ian Rogers137e88f2012-10-08 17:46:47 -0700163#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700165 if (UNLIKELY(code == NULL)) {
166 MethodHelper mh(method);
167 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
168 << " location: " << mh.GetDexFile().GetLocation();
169 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700170#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700171
172 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
173 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
174 uint64_t result = ((code_uint << 32) | method_uint);
175 return result;
176}
177
178// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700179extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 mirror::Object* this_object,
181 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700182 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 mirror::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, kInterface);
186}
187
188
189extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 mirror::Object* this_object,
191 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700195 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
196}
197
198extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 mirror::Object* this_object,
200 mirror::AbstractMethod* caller_method,
201 Thread* self,
202 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700204 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
205}
206
207extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800208 mirror::Object* this_object,
209 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700210 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700213 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
214}
215
216extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Object* this_object,
218 mirror::AbstractMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700219 Thread* self,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700222 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
223}
224
225} // namespace art