blob: 6feff0f56097aa6c6f8cc6e9f951b4d2222561ab [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
Ian Rogers57b86d42012-03-27 16:05:41 -070017#ifndef ART_SRC_OAT_JNI_CALLING_CONVENTION_H_
18#define ART_SRC_OAT_JNI_CALLING_CONVENTION_H_
Ian Rogersb033c752011-07-20 12:22:35 -070019
Ian Rogers0d666d82011-08-14 16:03:46 -070020#include <vector>
Ian Rogers57b86d42012-03-27 16:05:41 -070021#include "oat/utils/managed_register.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070022#include "stack_indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "thread.h"
Ian Rogersb033c752011-07-20 12:22:35 -070024
25namespace art {
26
27// Top-level abstraction for different calling conventions
28class 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 Rogersb033c752011-07-20 12:22:35 -070044 // Register that holds result of this method
Ian Rogers2c8f6532011-09-02 17:16:34 -070045 virtual ManagedRegister ReturnRegister() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -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
Carl Shapiroe2d373e2011-07-25 15:20:06 -070049 // Offset of Method within the frame
50 FrameOffset MethodStackOffset();
51
Ian Rogersb033c752011-07-20 12:22:35 -070052 // Iterator interface
53
54 // Place iterator at start of arguments. The displacement is applied to
55 // frame offset methods to account for frames which may be on the stack
56 // below the one being iterated over.
57 void ResetIterator(FrameOffset displacement) {
58 displacement_ = displacement;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070059 itr_slots_ = 0;
60 itr_args_ = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070061 itr_refs_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070062 itr_longs_and_doubles_ = 0;
63 }
64
Ian Rogers2c8f6532011-09-02 17:16:34 -070065 virtual ~CallingConvention() {}
66
Ian Rogersb033c752011-07-20 12:22:35 -070067 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -080068 CallingConvention(bool is_static, bool is_synchronized, const char* shorty)
69 : displacement_(0), is_static_(is_static), is_synchronized_(is_synchronized),
70 shorty_(shorty) {
71 num_args_ = (is_static ? 0 : 1) + strlen(shorty) - 1;
72 num_ref_args_ = is_static ? 0 : 1; // The implicit this pointer.
73 num_long_or_double_args_ = 0;
74 for (size_t i = 1; i < strlen(shorty); i++) {
75 char ch = shorty_[i];
76 if (ch == 'L') {
77 num_ref_args_++;
78 } else if ((ch == 'D') || (ch == 'J')) {
79 num_long_or_double_args_++;
80 }
81 }
82 }
Ian Rogers2c8f6532011-09-02 17:16:34 -070083
Ian Rogers169c9a72011-11-13 20:13:17 -080084 bool IsStatic() const {
85 return is_static_;
86 }
87 bool IsSynchronized() const {
88 return is_synchronized_;
89 }
90 bool IsParamALongOrDouble(unsigned int param) const {
91 DCHECK_LT(param, NumArgs());
92 if (IsStatic()) {
93 param++; // 0th argument must skip return value at start of the shorty
94 } else if (param == 0) {
95 return false; // this argument
96 }
97 char ch = shorty_[param];
98 return (ch == 'J' || ch == 'D');
99 }
100 bool IsParamAReference(unsigned int param) const {
101 DCHECK_LT(param, NumArgs());
102 if (IsStatic()) {
103 param++; // 0th argument must skip return value at start of the shorty
104 } else if (param == 0) {
105 return true; // this argument
106 }
107 return shorty_[param] == 'L';
Ian Rogers169c9a72011-11-13 20:13:17 -0800108 }
109 size_t NumArgs() const {
110 return num_args_;
111 }
112 size_t NumLongOrDoubleArgs() const {
113 return num_long_or_double_args_;
114 }
115 size_t NumReferenceArgs() const {
116 return num_ref_args_;
117 }
118 size_t ParamSize(unsigned int param) const {
119 DCHECK_LT(param, NumArgs());
120 if (IsStatic()) {
121 param++; // 0th argument must skip return value at start of the shorty
122 } else if (param == 0) {
123 return kPointerSize; // this argument
124 }
125 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[param]));
126 if (result >= 1 && result < 4) {
127 result = 4;
128 }
129 return result;
130 }
131 const char* GetShorty() const {
132 return shorty_.c_str();
133 }
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700134 // The slot number for current calling_convention argument.
135 // Note that each slot is 32-bit. When the current argument is bigger
136 // than 32 bits, return the first slot number for this argument.
137 unsigned int itr_slots_;
Ian Rogers7a99c112011-09-07 12:48:27 -0700138 // The number of references iterated past
139 unsigned int itr_refs_;
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700140 // The argument number along argument list for current argument
141 unsigned int itr_args_;
142 // Number of longs and doubles seen along argument list
Ian Rogersb033c752011-07-20 12:22:35 -0700143 unsigned int itr_longs_and_doubles_;
144 // Space for frames below this on the stack
145 FrameOffset displacement_;
146
147 private:
Ian Rogers169c9a72011-11-13 20:13:17 -0800148 const bool is_static_;
149 const bool is_synchronized_;
150 std::string shorty_;
151 size_t num_args_;
152 size_t num_ref_args_;
153 size_t num_long_or_double_args_;
Ian Rogersb033c752011-07-20 12:22:35 -0700154};
155
156// Abstraction for managed code's calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700157// | { Incoming stack args } |
158// | { Prior Method* } | <-- Prior SP
159// | { Return address } |
160// | { Callee saves } |
161// | { Spills ... } |
162// | { Outgoing stack args } |
163// | { Method* } | <-- SP
Ian Rogersb033c752011-07-20 12:22:35 -0700164class ManagedRuntimeCallingConvention : public CallingConvention {
165 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800166 static ManagedRuntimeCallingConvention* Create(bool is_static, bool is_synchronized,
167 const char* shorty,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700168 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700169
Ian Rogers2c8f6532011-09-02 17:16:34 -0700170 // Register that holds the incoming method argument
171 virtual ManagedRegister MethodRegister() = 0;
172
Ian Rogersb033c752011-07-20 12:22:35 -0700173 // Iterator interface
174 bool HasNext();
175 void Next();
176 bool IsCurrentParamAReference();
Ian Rogers7a99c112011-09-07 12:48:27 -0700177 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
178 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700179 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700180 virtual bool IsCurrentParamInRegister() = 0;
181 virtual bool IsCurrentParamOnStack() = 0;
182 virtual ManagedRegister CurrentParamRegister() = 0;
183 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700184
Ian Rogers2c8f6532011-09-02 17:16:34 -0700185 virtual ~ManagedRuntimeCallingConvention() {}
186
Ian Rogersb5d09b22012-03-06 22:14:17 -0800187 // Registers to spill to caller's out registers on entry.
188 virtual const std::vector<ManagedRegister>& EntrySpills() = 0;
189
Ian Rogers2c8f6532011-09-02 17:16:34 -0700190 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800191 ManagedRuntimeCallingConvention(bool is_static, bool is_synchronized, const char* shorty) :
192 CallingConvention(is_static, is_synchronized, shorty) {}
Ian Rogersb033c752011-07-20 12:22:35 -0700193};
194
195// Abstraction for JNI calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700196// | { Incoming stack args } | <-- Prior SP
197// | { Return address } |
198// | { Callee saves } | ([1])
199// | { Return value spill } | (live on return slow paths)
Ian Rogersdc51b792011-09-22 20:41:37 -0700200// | { Local Ref. Table State } |
Ian Rogersbdb03912011-09-14 00:55:44 -0700201// | { Stack Indirect Ref. Table |
202// | num. refs./link } | (here to prior SP is frame size)
203// | { Method* } | <-- Anchor SP written to thread
204// | { Outgoing stack args } | <-- SP at point of call
205// | Native frame |
206//
207// [1] We must save all callee saves here to enable any exception throws to restore
208// callee saves for frames above this one.
Ian Rogersb033c752011-07-20 12:22:35 -0700209class JniCallingConvention : public CallingConvention {
210 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800211 static JniCallingConvention* Create(bool is_static, bool is_synchronized, const char* shorty,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700212 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700213
214 // Size of frame excluding space for outgoing args (its assumed Method* is
215 // always at the bottom of a frame, but this doesn't work for outgoing
216 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700217 virtual size_t FrameSize() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700218 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700219 virtual size_t OutArgSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700220 // Number of references in stack indirect reference table
Ian Rogersdc51b792011-09-22 20:41:37 -0700221 size_t ReferenceCount() const;
222 // Location where the segment state of the local indirect reference table is saved
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700223 FrameOffset SavedLocalReferenceCookieOffset() const;
Ian Rogersdf20fe02011-07-20 20:34:16 -0700224 // Location where the return value of a call can be squirreled if another
225 // call is made following the native call
Ian Rogersdc51b792011-09-22 20:41:37 -0700226 FrameOffset ReturnValueSaveLocation() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700227
Ian Rogersbdb03912011-09-14 00:55:44 -0700228 // Callee save registers to spill prior to native code (which may clobber)
229 virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0;
230
231 // Spill mask values
232 virtual uint32_t CoreSpillMask() const = 0;
233 virtual uint32_t FpSpillMask() const = 0;
Ian Rogers0d666d82011-08-14 16:03:46 -0700234
Ian Rogers67375ac2011-09-14 00:55:44 -0700235 // Returns true if the method register will have been clobbered during argument
236 // set up
Ian Rogersad42e132011-09-17 20:23:33 -0700237 virtual bool IsMethodRegisterClobberedPreCall() = 0;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700238
Ian Rogersdc51b792011-09-22 20:41:37 -0700239 // An extra scratch register live after the call
240 virtual ManagedRegister ReturnScratchRegister() const = 0;
241
Ian Rogersb033c752011-07-20 12:22:35 -0700242 // Iterator interface
243 bool HasNext();
Ian Rogers67375ac2011-09-14 00:55:44 -0700244 virtual void Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700245 bool IsCurrentParamAReference();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700246 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700247 virtual bool IsCurrentParamInRegister() = 0;
248 virtual bool IsCurrentParamOnStack() = 0;
249 virtual ManagedRegister CurrentParamRegister() = 0;
250 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700251
252 // Iterator interface extension for JNI
Ian Rogers408f79a2011-08-23 18:22:33 -0700253 FrameOffset CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700254
Ian Rogers408f79a2011-08-23 18:22:33 -0700255 // Position of SIRT and interior fields
Ian Rogersdc51b792011-09-22 20:41:37 -0700256 FrameOffset SirtOffset() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700257 return FrameOffset(displacement_.Int32Value() +
258 kPointerSize); // above Method*
259 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700260 FrameOffset SirtNumRefsOffset() const {
Ian Rogers408f79a2011-08-23 18:22:33 -0700261 return FrameOffset(SirtOffset().Int32Value() +
262 StackIndirectReferenceTable::NumberOfReferencesOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700263 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700264 FrameOffset SirtLinkOffset() const {
Ian Rogers408f79a2011-08-23 18:22:33 -0700265 return FrameOffset(SirtOffset().Int32Value() +
266 StackIndirectReferenceTable::LinkOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700267 }
268
Ian Rogers2c8f6532011-09-02 17:16:34 -0700269 virtual ~JniCallingConvention() {}
270
271 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700272 // Named iterator positions
273 enum IteratorPos {
274 kJniEnv = 0,
275 kObjectOrClass = 1
276 };
277
Ian Rogers169c9a72011-11-13 20:13:17 -0800278 explicit JniCallingConvention(bool is_static, bool is_synchronized, const char* shorty) :
279 CallingConvention(is_static, is_synchronized, shorty) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700280
Ian Rogers408f79a2011-08-23 18:22:33 -0700281 // Number of stack slots for outgoing arguments, above which the SIRT is
Ian Rogersb033c752011-07-20 12:22:35 -0700282 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700283 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700284
Ian Rogers2c8f6532011-09-02 17:16:34 -0700285 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800286 size_t NumberOfExtraArgumentsForJni();
Ian Rogersb033c752011-07-20 12:22:35 -0700287};
288
289} // namespace art
290
Ian Rogers57b86d42012-03-27 16:05:41 -0700291#endif // ART_SRC_OAT_JNI_CALLING_CONVENTION_H_