blob: 76d237e943c267fc969cced65525dd2ef22b2365 [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>
Elliott Hughes68e76522011-10-05 13:22:16 -070021#include "stack_indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "thread.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "utils/managed_register.h"
Ian Rogersb033c752011-07-20 12:22:35 -070024
25namespace art {
26
Ian Rogers790a6b72014-04-01 10:36:00 -070027// Top-level abstraction for different calling conventions.
Ian Rogersb033c752011-07-20 12:22:35 -070028class CallingConvention {
29 public:
Ian Rogers169c9a72011-11-13 20:13:17 -080030 bool IsReturnAReference() const { return shorty_[0] == 'L'; }
Ian Rogersb033c752011-07-20 12:22:35 -070031
jeffhao58136ca2012-05-24 13:40:11 -070032 Primitive::Type GetReturnType() const {
33 return Primitive::GetType(shorty_[0]);
34 }
35
Ian Rogers169c9a72011-11-13 20:13:17 -080036 size_t SizeOfReturnValue() const {
37 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[0]));
38 if (result >= 1 && result < 4) {
39 result = 4;
40 }
41 return result;
42 }
Ian Rogersdf20fe02011-07-20 20:34:16 -070043
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 // Register that holds result of this method invocation.
Ian Rogers2c8f6532011-09-02 17:16:34 -070045 virtual ManagedRegister ReturnRegister() = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 // Register reserved for scratch usage during procedure calls.
Ian Rogers2c8f6532011-09-02 17:16:34 -070047 virtual ManagedRegister InterproceduralScratchRegister() = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070048
Ian Rogers790a6b72014-04-01 10:36:00 -070049 // Offset of Method within the frame.
50 FrameOffset MethodStackOffset() {
51 return displacement_;
52 }
Carl Shapiroe2d373e2011-07-25 15:20:06 -070053
Ian Rogersb033c752011-07-20 12:22:35 -070054 // Iterator interface
55
56 // Place iterator at start of arguments. The displacement is applied to
57 // frame offset methods to account for frames which may be on the stack
58 // below the one being iterated over.
59 void ResetIterator(FrameOffset displacement) {
60 displacement_ = displacement;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070061 itr_slots_ = 0;
62 itr_args_ = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070063 itr_refs_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070064 itr_longs_and_doubles_ = 0;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070065 itr_float_and_doubles_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070066 }
67
Ian Rogers2c8f6532011-09-02 17:16:34 -070068 virtual ~CallingConvention() {}
69
Ian Rogersb033c752011-07-20 12:22:35 -070070 protected:
Ian Rogers790a6b72014-04-01 10:36:00 -070071 CallingConvention(bool is_static, bool is_synchronized, const char* shorty,
72 size_t frame_pointer_size)
73 : itr_slots_(0), itr_refs_(0), itr_args_(0), itr_longs_and_doubles_(0),
74 itr_float_and_doubles_(0), displacement_(0),
75 frame_pointer_size_(frame_pointer_size),
76 sirt_pointer_size_(sizeof(StackReference<mirror::Object>)),
77 is_static_(is_static), is_synchronized_(is_synchronized),
Ian Rogers169c9a72011-11-13 20:13:17 -080078 shorty_(shorty) {
79 num_args_ = (is_static ? 0 : 1) + strlen(shorty) - 1;
80 num_ref_args_ = is_static ? 0 : 1; // The implicit this pointer.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070081 num_float_or_double_args_ = 0;
Ian Rogers169c9a72011-11-13 20:13:17 -080082 num_long_or_double_args_ = 0;
83 for (size_t i = 1; i < strlen(shorty); i++) {
84 char ch = shorty_[i];
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070085 switch (ch) {
86 case 'L':
Ian Rogers169c9a72011-11-13 20:13:17 -080087 num_ref_args_++;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070088 break;
89 case 'J':
Ian Rogers169c9a72011-11-13 20:13:17 -080090 num_long_or_double_args_++;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070091 break;
92 case 'D':
93 num_long_or_double_args_++;
94 num_float_or_double_args_++;
95 break;
96 case 'F':
97 num_float_or_double_args_++;
98 break;
Ian Rogers169c9a72011-11-13 20:13:17 -080099 }
100 }
101 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700102
Ian Rogers169c9a72011-11-13 20:13:17 -0800103 bool IsStatic() const {
104 return is_static_;
105 }
106 bool IsSynchronized() const {
107 return is_synchronized_;
108 }
109 bool IsParamALongOrDouble(unsigned int param) const {
110 DCHECK_LT(param, NumArgs());
111 if (IsStatic()) {
112 param++; // 0th argument must skip return value at start of the shorty
113 } else if (param == 0) {
114 return false; // this argument
115 }
116 char ch = shorty_[param];
117 return (ch == 'J' || ch == 'D');
118 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700119 bool IsParamAFloatOrDouble(unsigned int param) const {
120 DCHECK_LT(param, NumArgs());
121 if (IsStatic()) {
122 param++; // 0th argument must skip return value at start of the shorty
123 } else if (param == 0) {
124 return false; // this argument
125 }
126 char ch = shorty_[param];
127 return (ch == 'F' || ch == 'D');
128 }
Ian Rogers169c9a72011-11-13 20:13:17 -0800129 bool IsParamAReference(unsigned int param) const {
130 DCHECK_LT(param, NumArgs());
131 if (IsStatic()) {
132 param++; // 0th argument must skip return value at start of the shorty
133 } else if (param == 0) {
134 return true; // this argument
135 }
136 return shorty_[param] == 'L';
Ian Rogers169c9a72011-11-13 20:13:17 -0800137 }
138 size_t NumArgs() const {
139 return num_args_;
140 }
141 size_t NumLongOrDoubleArgs() const {
142 return num_long_or_double_args_;
143 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700144 size_t NumFloatOrDoubleArgs() const {
145 return num_float_or_double_args_;
146 }
Ian Rogers169c9a72011-11-13 20:13:17 -0800147 size_t NumReferenceArgs() const {
148 return num_ref_args_;
149 }
150 size_t ParamSize(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) {
Ian Rogers790a6b72014-04-01 10:36:00 -0700155 return frame_pointer_size_; // this argument
Ian Rogers169c9a72011-11-13 20:13:17 -0800156 }
157 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[param]));
158 if (result >= 1 && result < 4) {
159 result = 4;
160 }
161 return result;
162 }
163 const char* GetShorty() const {
164 return shorty_.c_str();
165 }
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700166 // The slot number for current calling_convention argument.
167 // Note that each slot is 32-bit. When the current argument is bigger
168 // than 32 bits, return the first slot number for this argument.
169 unsigned int itr_slots_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700170 // The number of references iterated past.
Ian Rogers7a99c112011-09-07 12:48:27 -0700171 unsigned int itr_refs_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700172 // The argument number along argument list for current argument.
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700173 unsigned int itr_args_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700174 // Number of longs and doubles seen along argument list.
Ian Rogersb033c752011-07-20 12:22:35 -0700175 unsigned int itr_longs_and_doubles_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700176 // Number of float and doubles seen along argument list.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700177 unsigned int itr_float_and_doubles_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700178 // Space for frames below this on the stack.
Ian Rogersb033c752011-07-20 12:22:35 -0700179 FrameOffset displacement_;
Ian Rogers790a6b72014-04-01 10:36:00 -0700180 // The size of a reference.
181 const size_t frame_pointer_size_;
182 // The size of a reference entry within the SIRT.
183 const size_t sirt_pointer_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700184
185 private:
Ian Rogers169c9a72011-11-13 20:13:17 -0800186 const bool is_static_;
187 const bool is_synchronized_;
188 std::string shorty_;
189 size_t num_args_;
190 size_t num_ref_args_;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700191 size_t num_float_or_double_args_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800192 size_t num_long_or_double_args_;
Ian Rogersb033c752011-07-20 12:22:35 -0700193};
194
195// Abstraction for managed code's calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700196// | { Incoming stack args } |
197// | { Prior Method* } | <-- Prior SP
198// | { Return address } |
199// | { Callee saves } |
200// | { Spills ... } |
201// | { Outgoing stack args } |
202// | { Method* } | <-- SP
Ian Rogersb033c752011-07-20 12:22:35 -0700203class ManagedRuntimeCallingConvention : public CallingConvention {
204 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800205 static ManagedRuntimeCallingConvention* Create(bool is_static, bool is_synchronized,
206 const char* shorty,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700207 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700208
Ian Rogers2c8f6532011-09-02 17:16:34 -0700209 // Register that holds the incoming method argument
210 virtual ManagedRegister MethodRegister() = 0;
211
Ian Rogersb033c752011-07-20 12:22:35 -0700212 // Iterator interface
213 bool HasNext();
214 void Next();
215 bool IsCurrentParamAReference();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700216 bool IsCurrentParamAFloatOrDouble();
Ian Rogers7a99c112011-09-07 12:48:27 -0700217 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
218 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700219 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700220 virtual bool IsCurrentParamInRegister() = 0;
221 virtual bool IsCurrentParamOnStack() = 0;
222 virtual ManagedRegister CurrentParamRegister() = 0;
223 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700224
Ian Rogers2c8f6532011-09-02 17:16:34 -0700225 virtual ~ManagedRuntimeCallingConvention() {}
226
Ian Rogersb5d09b22012-03-06 22:14:17 -0800227 // Registers to spill to caller's out registers on entry.
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700228 virtual const ManagedRegisterEntrySpills& EntrySpills() = 0;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800229
Ian Rogers2c8f6532011-09-02 17:16:34 -0700230 protected:
Ian Rogers790a6b72014-04-01 10:36:00 -0700231 ManagedRuntimeCallingConvention(bool is_static, bool is_synchronized, const char* shorty,
232 size_t frame_pointer_size)
233 : CallingConvention(is_static, is_synchronized, shorty, frame_pointer_size) {}
Ian Rogersb033c752011-07-20 12:22:35 -0700234};
235
236// Abstraction for JNI calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700237// | { Incoming stack args } | <-- Prior SP
238// | { Return address } |
239// | { Callee saves } | ([1])
240// | { Return value spill } | (live on return slow paths)
Ian Rogersdc51b792011-09-22 20:41:37 -0700241// | { Local Ref. Table State } |
Ian Rogersbdb03912011-09-14 00:55:44 -0700242// | { Stack Indirect Ref. Table |
243// | num. refs./link } | (here to prior SP is frame size)
244// | { Method* } | <-- Anchor SP written to thread
245// | { Outgoing stack args } | <-- SP at point of call
246// | Native frame |
247//
248// [1] We must save all callee saves here to enable any exception throws to restore
249// callee saves for frames above this one.
Ian Rogersb033c752011-07-20 12:22:35 -0700250class JniCallingConvention : public CallingConvention {
251 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800252 static JniCallingConvention* Create(bool is_static, bool is_synchronized, const char* shorty,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700253 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700254
255 // Size of frame excluding space for outgoing args (its assumed Method* is
256 // always at the bottom of a frame, but this doesn't work for outgoing
257 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700258 virtual size_t FrameSize() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700259 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700260 virtual size_t OutArgSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700261 // Number of references in stack indirect reference table
Ian Rogersdc51b792011-09-22 20:41:37 -0700262 size_t ReferenceCount() const;
263 // Location where the segment state of the local indirect reference table is saved
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700264 FrameOffset SavedLocalReferenceCookieOffset() const;
Ian Rogersdf20fe02011-07-20 20:34:16 -0700265 // Location where the return value of a call can be squirreled if another
266 // call is made following the native call
Ian Rogersdc51b792011-09-22 20:41:37 -0700267 FrameOffset ReturnValueSaveLocation() const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 // Register that holds result if it is integer.
269 virtual ManagedRegister IntReturnRegister() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700270
Ian Rogersbdb03912011-09-14 00:55:44 -0700271 // Callee save registers to spill prior to native code (which may clobber)
272 virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0;
273
274 // Spill mask values
275 virtual uint32_t CoreSpillMask() const = 0;
276 virtual uint32_t FpSpillMask() const = 0;
Ian Rogers0d666d82011-08-14 16:03:46 -0700277
Ian Rogersdc51b792011-09-22 20:41:37 -0700278 // An extra scratch register live after the call
279 virtual ManagedRegister ReturnScratchRegister() const = 0;
280
Ian Rogersb033c752011-07-20 12:22:35 -0700281 // Iterator interface
282 bool HasNext();
Ian Rogers67375ac2011-09-14 00:55:44 -0700283 virtual void Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700284 bool IsCurrentParamAReference();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700285 bool IsCurrentParamAFloatOrDouble();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700286 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700287 virtual bool IsCurrentParamInRegister() = 0;
288 virtual bool IsCurrentParamOnStack() = 0;
289 virtual ManagedRegister CurrentParamRegister() = 0;
290 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700291
292 // Iterator interface extension for JNI
Ian Rogers408f79a2011-08-23 18:22:33 -0700293 FrameOffset CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700294
Ian Rogers408f79a2011-08-23 18:22:33 -0700295 // Position of SIRT and interior fields
Ian Rogersdc51b792011-09-22 20:41:37 -0700296 FrameOffset SirtOffset() const {
Ian Rogers790a6b72014-04-01 10:36:00 -0700297 return FrameOffset(this->displacement_.Int32Value() + frame_pointer_size_); // above Method*
Ian Rogersb033c752011-07-20 12:22:35 -0700298 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700299
300 FrameOffset SirtLinkOffset() const {
301 return FrameOffset(SirtOffset().Int32Value() +
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700302 StackIndirectReferenceTable::LinkOffset(frame_pointer_size_));
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700303 }
304
Ian Rogersdc51b792011-09-22 20:41:37 -0700305 FrameOffset SirtNumRefsOffset() const {
Ian Rogers408f79a2011-08-23 18:22:33 -0700306 return FrameOffset(SirtOffset().Int32Value() +
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700307 StackIndirectReferenceTable::NumberOfReferencesOffset(frame_pointer_size_));
Ian Rogersb033c752011-07-20 12:22:35 -0700308 }
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700309
310 FrameOffset SirtReferencesOffset() const {
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700311 return FrameOffset(SirtOffset().Int32Value() +
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700312 StackIndirectReferenceTable::ReferencesOffset(frame_pointer_size_));
Ian Rogersb033c752011-07-20 12:22:35 -0700313 }
314
Ian Rogers2c8f6532011-09-02 17:16:34 -0700315 virtual ~JniCallingConvention() {}
316
317 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700318 // Named iterator positions
319 enum IteratorPos {
320 kJniEnv = 0,
321 kObjectOrClass = 1
322 };
323
Ian Rogers790a6b72014-04-01 10:36:00 -0700324 explicit JniCallingConvention(bool is_static, bool is_synchronized, const char* shorty,
325 size_t frame_pointer_size)
326 : CallingConvention(is_static, is_synchronized, shorty, frame_pointer_size) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700327
Ian Rogers408f79a2011-08-23 18:22:33 -0700328 // Number of stack slots for outgoing arguments, above which the SIRT is
Ian Rogersb033c752011-07-20 12:22:35 -0700329 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700330 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700331
Ian Rogers2c8f6532011-09-02 17:16:34 -0700332 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800333 size_t NumberOfExtraArgumentsForJni();
Ian Rogersb033c752011-07-20 12:22:35 -0700334};
335
336} // namespace art
337
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700338#endif // ART_COMPILER_JNI_QUICK_CALLING_CONVENTION_H_