blob: 2c4b15ca3138d93ccb497fc0701556e20fc1dfa8 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Ian Rogersb033c752011-07-20 12:22:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_JNI_QUICK_CALLING_CONVENTION_H_
18#define ART_COMPILER_JNI_QUICK_CALLING_CONVENTION_H_
Ian Rogersb033c752011-07-20 12:22:35 -070019
Ian Rogers0d666d82011-08-14 16:03:46 -070020#include <vector>
Vladimir Marko93205e32016-04-13 11:59:46 +010021
22#include "base/arena_object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle_scope.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "primitive.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "thread.h"
Ian Rogers166db042013-07-26 12:05:57 -070026#include "utils/managed_register.h"
Ian Rogersb033c752011-07-20 12:22:35 -070027
28namespace art {
29
Ian Rogers790a6b72014-04-01 10:36:00 -070030// Top-level abstraction for different calling conventions.
Vladimir Marko93205e32016-04-13 11:59:46 +010031class CallingConvention : public DeletableArenaObject<kArenaAllocCallingConvention> {
Ian Rogersb033c752011-07-20 12:22:35 -070032 public:
Ian Rogers169c9a72011-11-13 20:13:17 -080033 bool IsReturnAReference() const { return shorty_[0] == 'L'; }
Ian Rogersb033c752011-07-20 12:22:35 -070034
jeffhao58136ca2012-05-24 13:40:11 -070035 Primitive::Type GetReturnType() const {
36 return Primitive::GetType(shorty_[0]);
37 }
38
Ian Rogers169c9a72011-11-13 20:13:17 -080039 size_t SizeOfReturnValue() const {
40 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[0]));
41 if (result >= 1 && result < 4) {
42 result = 4;
43 }
44 return result;
45 }
Ian Rogersdf20fe02011-07-20 20:34:16 -070046
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 // Register that holds result of this method invocation.
Ian Rogers2c8f6532011-09-02 17:16:34 -070048 virtual ManagedRegister ReturnRegister() = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 // Register reserved for scratch usage during procedure calls.
Ian Rogers2c8f6532011-09-02 17:16:34 -070050 virtual ManagedRegister InterproceduralScratchRegister() = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070051
Ian Rogers790a6b72014-04-01 10:36:00 -070052 // Offset of Method within the frame.
53 FrameOffset MethodStackOffset() {
54 return displacement_;
55 }
Carl Shapiroe2d373e2011-07-25 15:20:06 -070056
Ian Rogersb033c752011-07-20 12:22:35 -070057 // Iterator interface
58
59 // Place iterator at start of arguments. The displacement is applied to
60 // frame offset methods to account for frames which may be on the stack
61 // below the one being iterated over.
62 void ResetIterator(FrameOffset displacement) {
63 displacement_ = displacement;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070064 itr_slots_ = 0;
65 itr_args_ = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070066 itr_refs_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070067 itr_longs_and_doubles_ = 0;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070068 itr_float_and_doubles_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070069 }
70
Ian Rogers2c8f6532011-09-02 17:16:34 -070071 virtual ~CallingConvention() {}
72
Ian Rogersb033c752011-07-20 12:22:35 -070073 protected:
Ian Rogers790a6b72014-04-01 10:36:00 -070074 CallingConvention(bool is_static, bool is_synchronized, const char* shorty,
75 size_t frame_pointer_size)
76 : itr_slots_(0), itr_refs_(0), itr_args_(0), itr_longs_and_doubles_(0),
77 itr_float_and_doubles_(0), displacement_(0),
78 frame_pointer_size_(frame_pointer_size),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070079 handle_scope_pointer_size_(sizeof(StackReference<mirror::Object>)),
Ian Rogers790a6b72014-04-01 10:36:00 -070080 is_static_(is_static), is_synchronized_(is_synchronized),
Ian Rogers169c9a72011-11-13 20:13:17 -080081 shorty_(shorty) {
82 num_args_ = (is_static ? 0 : 1) + strlen(shorty) - 1;
83 num_ref_args_ = is_static ? 0 : 1; // The implicit this pointer.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070084 num_float_or_double_args_ = 0;
Ian Rogers169c9a72011-11-13 20:13:17 -080085 num_long_or_double_args_ = 0;
86 for (size_t i = 1; i < strlen(shorty); i++) {
87 char ch = shorty_[i];
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070088 switch (ch) {
89 case 'L':
Ian Rogers169c9a72011-11-13 20:13:17 -080090 num_ref_args_++;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070091 break;
92 case 'J':
Ian Rogers169c9a72011-11-13 20:13:17 -080093 num_long_or_double_args_++;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070094 break;
95 case 'D':
96 num_long_or_double_args_++;
97 num_float_or_double_args_++;
98 break;
99 case 'F':
100 num_float_or_double_args_++;
101 break;
Ian Rogers169c9a72011-11-13 20:13:17 -0800102 }
103 }
104 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700105
Ian Rogers169c9a72011-11-13 20:13:17 -0800106 bool IsStatic() const {
107 return is_static_;
108 }
109 bool IsSynchronized() const {
110 return is_synchronized_;
111 }
112 bool IsParamALongOrDouble(unsigned int param) const {
113 DCHECK_LT(param, NumArgs());
114 if (IsStatic()) {
115 param++; // 0th argument must skip return value at start of the shorty
116 } else if (param == 0) {
117 return false; // this argument
118 }
119 char ch = shorty_[param];
120 return (ch == 'J' || ch == 'D');
121 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700122 bool IsParamAFloatOrDouble(unsigned int param) const {
123 DCHECK_LT(param, NumArgs());
124 if (IsStatic()) {
125 param++; // 0th argument must skip return value at start of the shorty
126 } else if (param == 0) {
127 return false; // this argument
128 }
129 char ch = shorty_[param];
130 return (ch == 'F' || ch == 'D');
131 }
Serban Constantinescu75b91132014-04-09 18:39:10 +0100132 bool IsParamADouble(unsigned int param) const {
133 DCHECK_LT(param, NumArgs());
134 if (IsStatic()) {
135 param++; // 0th argument must skip return value at start of the shorty
136 } else if (param == 0) {
137 return false; // this argument
138 }
139 return shorty_[param] == 'D';
140 }
141 bool IsParamALong(unsigned int param) const {
142 DCHECK_LT(param, NumArgs());
143 if (IsStatic()) {
144 param++; // 0th argument must skip return value at start of the shorty
145 } else if (param == 0) {
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500146 return false; // this argument
Serban Constantinescu75b91132014-04-09 18:39:10 +0100147 }
148 return shorty_[param] == 'J';
149 }
Ian Rogers169c9a72011-11-13 20:13:17 -0800150 bool IsParamAReference(unsigned int param) const {
151 DCHECK_LT(param, NumArgs());
152 if (IsStatic()) {
153 param++; // 0th argument must skip return value at start of the shorty
154 } else if (param == 0) {
155 return true; // this argument
156 }
157 return shorty_[param] == 'L';
Ian Rogers169c9a72011-11-13 20:13:17 -0800158 }
159 size_t NumArgs() const {
160 return num_args_;
161 }
162 size_t NumLongOrDoubleArgs() const {
163 return num_long_or_double_args_;
164 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700165 size_t NumFloatOrDoubleArgs() const {
166 return num_float_or_double_args_;
167 }
Ian Rogers169c9a72011-11-13 20:13:17 -0800168 size_t NumReferenceArgs() const {
169 return num_ref_args_;
170 }
171 size_t ParamSize(unsigned int param) const {
172 DCHECK_LT(param, NumArgs());
173 if (IsStatic()) {
174 param++; // 0th argument must skip return value at start of the shorty
175 } else if (param == 0) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 return sizeof(mirror::HeapReference<mirror::Object>); // this argument
Ian Rogers169c9a72011-11-13 20:13:17 -0800177 }
178 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[param]));
179 if (result >= 1 && result < 4) {
180 result = 4;
181 }
182 return result;
183 }
184 const char* GetShorty() const {
185 return shorty_.c_str();
186 }
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700187 // The slot number for current calling_convention argument.
188 // Note that each slot is 32-bit. When the current argument is bigger
189 // than 32 bits, return the first slot number for this argument.
190 unsigned int itr_slots_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700191 // The number of references iterated past.
Ian Rogers7a99c112011-09-07 12:48:27 -0700192 unsigned int itr_refs_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700193 // The argument number along argument list for current argument.
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700194 unsigned int itr_args_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700195 // Number of longs and doubles seen along argument list.
Ian Rogersb033c752011-07-20 12:22:35 -0700196 unsigned int itr_longs_and_doubles_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700197 // Number of float and doubles seen along argument list.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700198 unsigned int itr_float_and_doubles_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700199 // Space for frames below this on the stack.
Ian Rogersb033c752011-07-20 12:22:35 -0700200 FrameOffset displacement_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 // The size of a pointer.
Ian Rogers790a6b72014-04-01 10:36:00 -0700202 const size_t frame_pointer_size_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 // The size of a reference entry within the handle scope.
204 const size_t handle_scope_pointer_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700205
206 private:
Ian Rogers169c9a72011-11-13 20:13:17 -0800207 const bool is_static_;
208 const bool is_synchronized_;
209 std::string shorty_;
210 size_t num_args_;
211 size_t num_ref_args_;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700212 size_t num_float_or_double_args_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800213 size_t num_long_or_double_args_;
Ian Rogersb033c752011-07-20 12:22:35 -0700214};
215
216// Abstraction for managed code's calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700217// | { Incoming stack args } |
218// | { Prior Method* } | <-- Prior SP
219// | { Return address } |
220// | { Callee saves } |
221// | { Spills ... } |
222// | { Outgoing stack args } |
223// | { Method* } | <-- SP
Ian Rogersb033c752011-07-20 12:22:35 -0700224class ManagedRuntimeCallingConvention : public CallingConvention {
225 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100226 static std::unique_ptr<ManagedRuntimeCallingConvention> Create(ArenaAllocator* arena,
227 bool is_static,
228 bool is_synchronized,
229 const char* shorty,
230 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700231
Ian Rogers2c8f6532011-09-02 17:16:34 -0700232 // Register that holds the incoming method argument
233 virtual ManagedRegister MethodRegister() = 0;
234
Ian Rogersb033c752011-07-20 12:22:35 -0700235 // Iterator interface
236 bool HasNext();
237 void Next();
238 bool IsCurrentParamAReference();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700239 bool IsCurrentParamAFloatOrDouble();
Serban Constantinescu75b91132014-04-09 18:39:10 +0100240 bool IsCurrentParamADouble();
241 bool IsCurrentParamALong();
Ian Rogers7a99c112011-09-07 12:48:27 -0700242 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
243 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700244 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700245 virtual bool IsCurrentParamInRegister() = 0;
246 virtual bool IsCurrentParamOnStack() = 0;
247 virtual ManagedRegister CurrentParamRegister() = 0;
248 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700249
Ian Rogers2c8f6532011-09-02 17:16:34 -0700250 virtual ~ManagedRuntimeCallingConvention() {}
251
Ian Rogersb5d09b22012-03-06 22:14:17 -0800252 // Registers to spill to caller's out registers on entry.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700253 virtual const ManagedRegisterEntrySpills& EntrySpills() = 0;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800254
Ian Rogers2c8f6532011-09-02 17:16:34 -0700255 protected:
Vladimir Marko93205e32016-04-13 11:59:46 +0100256 ManagedRuntimeCallingConvention(bool is_static,
257 bool is_synchronized,
258 const char* shorty,
Ian Rogers790a6b72014-04-01 10:36:00 -0700259 size_t frame_pointer_size)
260 : CallingConvention(is_static, is_synchronized, shorty, frame_pointer_size) {}
Ian Rogersb033c752011-07-20 12:22:35 -0700261};
262
263// Abstraction for JNI calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700264// | { Incoming stack args } | <-- Prior SP
265// | { Return address } |
266// | { Callee saves } | ([1])
267// | { Return value spill } | (live on return slow paths)
Ian Rogersdc51b792011-09-22 20:41:37 -0700268// | { Local Ref. Table State } |
Ian Rogersbdb03912011-09-14 00:55:44 -0700269// | { Stack Indirect Ref. Table |
270// | num. refs./link } | (here to prior SP is frame size)
271// | { Method* } | <-- Anchor SP written to thread
272// | { Outgoing stack args } | <-- SP at point of call
273// | Native frame |
274//
275// [1] We must save all callee saves here to enable any exception throws to restore
276// callee saves for frames above this one.
Ian Rogersb033c752011-07-20 12:22:35 -0700277class JniCallingConvention : public CallingConvention {
278 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100279 static std::unique_ptr<JniCallingConvention> Create(ArenaAllocator* arena,
280 bool is_static,
281 bool is_synchronized,
282 const char* shorty,
283 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700284
285 // Size of frame excluding space for outgoing args (its assumed Method* is
286 // always at the bottom of a frame, but this doesn't work for outgoing
287 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700288 virtual size_t FrameSize() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700289 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700290 virtual size_t OutArgSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700291 // Number of references in stack indirect reference table
Ian Rogersdc51b792011-09-22 20:41:37 -0700292 size_t ReferenceCount() const;
293 // Location where the segment state of the local indirect reference table is saved
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700294 FrameOffset SavedLocalReferenceCookieOffset() const;
Ian Rogersdf20fe02011-07-20 20:34:16 -0700295 // Location where the return value of a call can be squirreled if another
296 // call is made following the native call
Ian Rogersdc51b792011-09-22 20:41:37 -0700297 FrameOffset ReturnValueSaveLocation() const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 // Register that holds result if it is integer.
299 virtual ManagedRegister IntReturnRegister() = 0;
Andreas Gamped1104322014-05-01 14:38:56 -0700300 // Whether the compiler needs to ensure zero-/sign-extension of a small result type
301 virtual bool RequiresSmallResultTypeExtension() const = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700302
Ian Rogersbdb03912011-09-14 00:55:44 -0700303 // Callee save registers to spill prior to native code (which may clobber)
304 virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0;
305
306 // Spill mask values
307 virtual uint32_t CoreSpillMask() const = 0;
308 virtual uint32_t FpSpillMask() const = 0;
Ian Rogers0d666d82011-08-14 16:03:46 -0700309
Ian Rogersdc51b792011-09-22 20:41:37 -0700310 // An extra scratch register live after the call
311 virtual ManagedRegister ReturnScratchRegister() const = 0;
312
Ian Rogersb033c752011-07-20 12:22:35 -0700313 // Iterator interface
314 bool HasNext();
Ian Rogers67375ac2011-09-14 00:55:44 -0700315 virtual void Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700316 bool IsCurrentParamAReference();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700317 bool IsCurrentParamAFloatOrDouble();
Serban Constantinescu75b91132014-04-09 18:39:10 +0100318 bool IsCurrentParamADouble();
319 bool IsCurrentParamALong();
320 bool IsCurrentParamJniEnv();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700321 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700322 virtual bool IsCurrentParamInRegister() = 0;
323 virtual bool IsCurrentParamOnStack() = 0;
324 virtual ManagedRegister CurrentParamRegister() = 0;
325 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700326
327 // Iterator interface extension for JNI
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700328 FrameOffset CurrentParamHandleScopeEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700329
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700330 // Position of handle scope and interior fields
331 FrameOffset HandleScopeOffset() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 return FrameOffset(this->displacement_.Int32Value() + frame_pointer_size_);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700333 // above Method reference
Ian Rogersb033c752011-07-20 12:22:35 -0700334 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700335
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700336 FrameOffset HandleScopeLinkOffset() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 return FrameOffset(HandleScopeOffset().Int32Value() +
338 HandleScope::LinkOffset(frame_pointer_size_));
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700339 }
340
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700341 FrameOffset HandleScopeNumRefsOffset() const {
342 return FrameOffset(HandleScopeOffset().Int32Value() +
343 HandleScope::NumberOfReferencesOffset(frame_pointer_size_));
Ian Rogersb033c752011-07-20 12:22:35 -0700344 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700345
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 FrameOffset HandleReferencesOffset() const {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700347 return FrameOffset(HandleScopeOffset().Int32Value() +
348 HandleScope::ReferencesOffset(frame_pointer_size_));
Ian Rogersb033c752011-07-20 12:22:35 -0700349 }
350
Ian Rogers2c8f6532011-09-02 17:16:34 -0700351 virtual ~JniCallingConvention() {}
352
353 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700354 // Named iterator positions
355 enum IteratorPos {
356 kJniEnv = 0,
357 kObjectOrClass = 1
358 };
359
Roland Levillain3887c462015-08-12 18:15:42 +0100360 JniCallingConvention(bool is_static, bool is_synchronized, const char* shorty,
361 size_t frame_pointer_size)
Ian Rogers790a6b72014-04-01 10:36:00 -0700362 : CallingConvention(is_static, is_synchronized, shorty, frame_pointer_size) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700363
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700364 // Number of stack slots for outgoing arguments, above which the handle scope is
Ian Rogersb033c752011-07-20 12:22:35 -0700365 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700366 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700367
Ian Rogers2c8f6532011-09-02 17:16:34 -0700368 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800369 size_t NumberOfExtraArgumentsForJni();
Ian Rogersb033c752011-07-20 12:22:35 -0700370};
371
372} // namespace art
373
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700374#endif // ART_COMPILER_JNI_QUICK_CALLING_CONVENTION_H_