blob: 95c2d402b30b8cb5b2d3a692651dc28391442c90 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 "calling_convention.h"
18
19#include "base/logging.h"
20#include "jni/quick/arm/calling_convention_arm.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000021#include "jni/quick/arm64/calling_convention_arm64.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "jni/quick/mips/calling_convention_mips.h"
23#include "jni/quick/x86/calling_convention_x86.h"
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070024#include "jni/quick/x86_64/calling_convention_x86_64.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "utils.h"
26
27namespace art {
28
Brian Carlstrom7940e442013-07-12 13:46:57 -070029// Managed runtime calling convention
30
31ManagedRuntimeCallingConvention* ManagedRuntimeCallingConvention::Create(
32 bool is_static, bool is_synchronized, const char* shorty, InstructionSet instruction_set) {
33 switch (instruction_set) {
34 case kArm:
35 case kThumb2:
36 return new arm::ArmManagedRuntimeCallingConvention(is_static, is_synchronized, shorty);
Stuart Monteithb95a5342014-03-12 13:32:32 +000037 case kArm64:
38 return new arm64::Arm64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty);
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 case kMips:
40 return new mips::MipsManagedRuntimeCallingConvention(is_static, is_synchronized, shorty);
41 case kX86:
42 return new x86::X86ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty);
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070043 case kX86_64:
44 return new x86_64::X86_64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty);
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 default:
46 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
47 return NULL;
48 }
49}
50
51bool ManagedRuntimeCallingConvention::HasNext() {
52 return itr_args_ < NumArgs();
53}
54
55void ManagedRuntimeCallingConvention::Next() {
56 CHECK(HasNext());
57 if (IsCurrentArgExplicit() && // don't query parameter type of implicit args
58 IsParamALongOrDouble(itr_args_)) {
59 itr_longs_and_doubles_++;
60 itr_slots_++;
61 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070062 if (IsParamAFloatOrDouble(itr_args_)) {
63 itr_float_and_doubles_++;
64 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 if (IsCurrentParamAReference()) {
66 itr_refs_++;
67 }
68 itr_args_++;
69 itr_slots_++;
70}
71
72bool ManagedRuntimeCallingConvention::IsCurrentArgExplicit() {
73 // Static methods have no implicit arguments, others implicitly pass this
74 return IsStatic() || (itr_args_ != 0);
75}
76
77bool ManagedRuntimeCallingConvention::IsCurrentArgPossiblyNull() {
78 return IsCurrentArgExplicit(); // any user parameter may be null
79}
80
81size_t ManagedRuntimeCallingConvention::CurrentParamSize() {
82 return ParamSize(itr_args_);
83}
84
85bool ManagedRuntimeCallingConvention::IsCurrentParamAReference() {
86 return IsParamAReference(itr_args_);
87}
88
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070089bool ManagedRuntimeCallingConvention::IsCurrentParamAFloatOrDouble() {
90 return IsParamAFloatOrDouble(itr_args_);
91}
92
Serban Constantinescu75b91132014-04-09 18:39:10 +010093bool ManagedRuntimeCallingConvention::IsCurrentParamADouble() {
94 return IsParamADouble(itr_args_);
95}
96
97bool ManagedRuntimeCallingConvention::IsCurrentParamALong() {
98 return IsParamALong(itr_args_);
99}
100
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101// JNI calling convention
102
103JniCallingConvention* JniCallingConvention::Create(bool is_static, bool is_synchronized,
104 const char* shorty,
105 InstructionSet instruction_set) {
106 switch (instruction_set) {
107 case kArm:
108 case kThumb2:
109 return new arm::ArmJniCallingConvention(is_static, is_synchronized, shorty);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 case kArm64:
111 return new arm64::Arm64JniCallingConvention(is_static, is_synchronized, shorty);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 case kMips:
113 return new mips::MipsJniCallingConvention(is_static, is_synchronized, shorty);
114 case kX86:
115 return new x86::X86JniCallingConvention(is_static, is_synchronized, shorty);
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700116 case kX86_64:
117 return new x86_64::X86_64JniCallingConvention(is_static, is_synchronized, shorty);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 default:
119 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
120 return NULL;
121 }
122}
123
124size_t JniCallingConvention::ReferenceCount() const {
125 return NumReferenceArgs() + (IsStatic() ? 1 : 0);
126}
127
128FrameOffset JniCallingConvention::SavedLocalReferenceCookieOffset() const {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 size_t references_size = handle_scope_pointer_size_ * ReferenceCount(); // size excluding header
130 return FrameOffset(HandleerencesOffset().Int32Value() + references_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131}
132
133FrameOffset JniCallingConvention::ReturnValueSaveLocation() const {
134 // Segment state is 4 bytes long
135 return FrameOffset(SavedLocalReferenceCookieOffset().Int32Value() + 4);
136}
137
138bool JniCallingConvention::HasNext() {
139 if (itr_args_ <= kObjectOrClass) {
140 return true;
141 } else {
142 unsigned int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
143 return arg_pos < NumArgs();
144 }
145}
146
147void JniCallingConvention::Next() {
148 CHECK(HasNext());
149 if (itr_args_ > kObjectOrClass) {
150 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
151 if (IsParamALongOrDouble(arg_pos)) {
152 itr_longs_and_doubles_++;
153 itr_slots_++;
154 }
155 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700156 if (IsCurrentParamAFloatOrDouble()) {
157 itr_float_and_doubles_++;
158 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 if (IsCurrentParamAReference()) {
160 itr_refs_++;
161 }
162 itr_args_++;
163 itr_slots_++;
164}
165
166bool JniCallingConvention::IsCurrentParamAReference() {
167 switch (itr_args_) {
168 case kJniEnv:
169 return false; // JNIEnv*
170 case kObjectOrClass:
171 return true; // jobject or jclass
172 default: {
173 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
174 return IsParamAReference(arg_pos);
175 }
176 }
177}
178
Serban Constantinescu75b91132014-04-09 18:39:10 +0100179bool JniCallingConvention::IsCurrentParamJniEnv() {
180 return (itr_args_ == kJniEnv);
181}
182
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700183bool JniCallingConvention::IsCurrentParamAFloatOrDouble() {
184 switch (itr_args_) {
185 case kJniEnv:
186 return false; // JNIEnv*
187 case kObjectOrClass:
188 return false; // jobject or jclass
189 default: {
190 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
191 return IsParamAFloatOrDouble(arg_pos);
192 }
193 }
194}
195
Serban Constantinescu75b91132014-04-09 18:39:10 +0100196bool JniCallingConvention::IsCurrentParamADouble() {
197 switch (itr_args_) {
198 case kJniEnv:
199 return false; // JNIEnv*
200 case kObjectOrClass:
201 return false; // jobject or jclass
202 default: {
203 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
204 return IsParamADouble(arg_pos);
205 }
206 }
207}
208
209bool JniCallingConvention::IsCurrentParamALong() {
210 switch (itr_args_) {
211 case kJniEnv:
212 return false; // JNIEnv*
213 case kObjectOrClass:
214 return false; // jobject or jclass
215 default: {
216 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
217 return IsParamALong(arg_pos);
218 }
219 }
220}
221
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700222// Return position of handle scope entry holding reference at the current iterator
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223// position
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224FrameOffset JniCallingConvention::CurrentParamHandleScopeEntryOffset() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 CHECK(IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 CHECK_LT(HandleScopeLinkOffset(), HandleScopeNumRefsOffset());
227 int result = HandleerencesOffset().Int32Value() + itr_refs_ * handle_scope_pointer_size_;
228 CHECK_GT(result, HandleScopeNumRefsOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 return FrameOffset(result);
230}
231
232size_t JniCallingConvention::CurrentParamSize() {
233 if (itr_args_ <= kObjectOrClass) {
Ian Rogers790a6b72014-04-01 10:36:00 -0700234 return frame_pointer_size_; // JNIEnv or jobject/jclass
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 } else {
236 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
237 return ParamSize(arg_pos);
238 }
239}
240
241size_t JniCallingConvention::NumberOfExtraArgumentsForJni() {
242 // The first argument is the JNIEnv*.
243 // Static methods have an extra argument which is the jclass.
244 return IsStatic() ? 2 : 1;
245}
246
247} // namespace art