blob: bdbf1a171272bd327df4ac726c02cd4c8ded027f [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Ian Rogersb033c752011-07-20 12:22:35 -07002
3#ifndef ART_SRC_CALLING_CONVENTION_H_
4#define ART_SRC_CALLING_CONVENTION_H_
5
Ian Rogers0d666d82011-08-14 16:03:46 -07006#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "managed_register.h"
Elliott Hughes68e76522011-10-05 13:22:16 -07008#include "stack_indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "thread.h"
Ian Rogersb033c752011-07-20 12:22:35 -070010
11namespace art {
12
13// Top-level abstraction for different calling conventions
14class CallingConvention {
15 public:
Ian Rogers169c9a72011-11-13 20:13:17 -080016 bool IsReturnAReference() const { return shorty_[0] == 'L'; }
Ian Rogersb033c752011-07-20 12:22:35 -070017
Ian Rogers169c9a72011-11-13 20:13:17 -080018 size_t SizeOfReturnValue() const {
19 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[0]));
20 if (result >= 1 && result < 4) {
21 result = 4;
22 }
23 return result;
24 }
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
Ian Rogersb033c752011-07-20 12:22:35 -070026 // Register that holds result of this method
Ian Rogers2c8f6532011-09-02 17:16:34 -070027 virtual ManagedRegister ReturnRegister() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070028 // Register reserved for scratch usage during procedure calls
Ian Rogers2c8f6532011-09-02 17:16:34 -070029 virtual ManagedRegister InterproceduralScratchRegister() = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070030
Carl Shapiroe2d373e2011-07-25 15:20:06 -070031 // Offset of Method within the frame
32 FrameOffset MethodStackOffset();
33
Ian Rogersb033c752011-07-20 12:22:35 -070034 // Iterator interface
35
36 // Place iterator at start of arguments. The displacement is applied to
37 // frame offset methods to account for frames which may be on the stack
38 // below the one being iterated over.
39 void ResetIterator(FrameOffset displacement) {
40 displacement_ = displacement;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070041 itr_slots_ = 0;
42 itr_args_ = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070043 itr_refs_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070044 itr_longs_and_doubles_ = 0;
45 }
46
Ian Rogers2c8f6532011-09-02 17:16:34 -070047 virtual ~CallingConvention() {}
48
Ian Rogersb033c752011-07-20 12:22:35 -070049 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -080050 CallingConvention(bool is_static, bool is_synchronized, const char* shorty)
51 : displacement_(0), is_static_(is_static), is_synchronized_(is_synchronized),
52 shorty_(shorty) {
53 num_args_ = (is_static ? 0 : 1) + strlen(shorty) - 1;
54 num_ref_args_ = is_static ? 0 : 1; // The implicit this pointer.
55 num_long_or_double_args_ = 0;
56 for (size_t i = 1; i < strlen(shorty); i++) {
57 char ch = shorty_[i];
58 if (ch == 'L') {
59 num_ref_args_++;
60 } else if ((ch == 'D') || (ch == 'J')) {
61 num_long_or_double_args_++;
62 }
63 }
64 }
Ian Rogers2c8f6532011-09-02 17:16:34 -070065
Ian Rogers169c9a72011-11-13 20:13:17 -080066 bool IsStatic() const {
67 return is_static_;
68 }
69 bool IsSynchronized() const {
70 return is_synchronized_;
71 }
72 bool IsParamALongOrDouble(unsigned int param) const {
73 DCHECK_LT(param, NumArgs());
74 if (IsStatic()) {
75 param++; // 0th argument must skip return value at start of the shorty
76 } else if (param == 0) {
77 return false; // this argument
78 }
79 char ch = shorty_[param];
80 return (ch == 'J' || ch == 'D');
81 }
82 bool IsParamAReference(unsigned int param) const {
83 DCHECK_LT(param, NumArgs());
84 if (IsStatic()) {
85 param++; // 0th argument must skip return value at start of the shorty
86 } else if (param == 0) {
87 return true; // this argument
88 }
89 return shorty_[param] == 'L';
Ian Rogers169c9a72011-11-13 20:13:17 -080090 }
91 size_t NumArgs() const {
92 return num_args_;
93 }
94 size_t NumLongOrDoubleArgs() const {
95 return num_long_or_double_args_;
96 }
97 size_t NumReferenceArgs() const {
98 return num_ref_args_;
99 }
100 size_t ParamSize(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 kPointerSize; // this argument
106 }
107 size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[param]));
108 if (result >= 1 && result < 4) {
109 result = 4;
110 }
111 return result;
112 }
113 const char* GetShorty() const {
114 return shorty_.c_str();
115 }
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700116 // The slot number for current calling_convention argument.
117 // Note that each slot is 32-bit. When the current argument is bigger
118 // than 32 bits, return the first slot number for this argument.
119 unsigned int itr_slots_;
Ian Rogers7a99c112011-09-07 12:48:27 -0700120 // The number of references iterated past
121 unsigned int itr_refs_;
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700122 // The argument number along argument list for current argument
123 unsigned int itr_args_;
124 // Number of longs and doubles seen along argument list
Ian Rogersb033c752011-07-20 12:22:35 -0700125 unsigned int itr_longs_and_doubles_;
126 // Space for frames below this on the stack
127 FrameOffset displacement_;
128
129 private:
Ian Rogers169c9a72011-11-13 20:13:17 -0800130 const bool is_static_;
131 const bool is_synchronized_;
132 std::string shorty_;
133 size_t num_args_;
134 size_t num_ref_args_;
135 size_t num_long_or_double_args_;
Ian Rogersb033c752011-07-20 12:22:35 -0700136};
137
138// Abstraction for managed code's calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700139// | { Incoming stack args } |
140// | { Prior Method* } | <-- Prior SP
141// | { Return address } |
142// | { Callee saves } |
143// | { Spills ... } |
144// | { Outgoing stack args } |
145// | { Method* } | <-- SP
Ian Rogersb033c752011-07-20 12:22:35 -0700146class ManagedRuntimeCallingConvention : public CallingConvention {
147 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800148 static ManagedRuntimeCallingConvention* Create(bool is_static, bool is_synchronized,
149 const char* shorty,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700151
Ian Rogers2c8f6532011-09-02 17:16:34 -0700152 // Register that holds the incoming method argument
153 virtual ManagedRegister MethodRegister() = 0;
154
Ian Rogersb033c752011-07-20 12:22:35 -0700155 // Iterator interface
156 bool HasNext();
157 void Next();
158 bool IsCurrentParamAReference();
Ian Rogers7a99c112011-09-07 12:48:27 -0700159 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
160 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700161 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700162 virtual bool IsCurrentParamInRegister() = 0;
163 virtual bool IsCurrentParamOnStack() = 0;
164 virtual ManagedRegister CurrentParamRegister() = 0;
165 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700166
Ian Rogers2c8f6532011-09-02 17:16:34 -0700167 virtual ~ManagedRuntimeCallingConvention() {}
168
169 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800170 ManagedRuntimeCallingConvention(bool is_static, bool is_synchronized, const char* shorty) :
171 CallingConvention(is_static, is_synchronized, shorty) {}
Ian Rogersb033c752011-07-20 12:22:35 -0700172};
173
174// Abstraction for JNI calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700175// | { Incoming stack args } | <-- Prior SP
176// | { Return address } |
177// | { Callee saves } | ([1])
178// | { Return value spill } | (live on return slow paths)
Ian Rogersdc51b792011-09-22 20:41:37 -0700179// | { Local Ref. Table State } |
Ian Rogersbdb03912011-09-14 00:55:44 -0700180// | { Stack Indirect Ref. Table |
181// | num. refs./link } | (here to prior SP is frame size)
182// | { Method* } | <-- Anchor SP written to thread
183// | { Outgoing stack args } | <-- SP at point of call
184// | Native frame |
185//
186// [1] We must save all callee saves here to enable any exception throws to restore
187// callee saves for frames above this one.
Ian Rogersb033c752011-07-20 12:22:35 -0700188class JniCallingConvention : public CallingConvention {
189 public:
Ian Rogers169c9a72011-11-13 20:13:17 -0800190 static JniCallingConvention* Create(bool is_static, bool is_synchronized, const char* shorty,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700191 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700192
193 // Size of frame excluding space for outgoing args (its assumed Method* is
194 // always at the bottom of a frame, but this doesn't work for outgoing
195 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700196 virtual size_t FrameSize() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700197 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700198 virtual size_t OutArgSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700199 // Number of references in stack indirect reference table
Ian Rogersdc51b792011-09-22 20:41:37 -0700200 size_t ReferenceCount() const;
201 // Location where the segment state of the local indirect reference table is saved
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700202 FrameOffset SavedLocalReferenceCookieOffset() const;
Ian Rogersdf20fe02011-07-20 20:34:16 -0700203 // Location where the return value of a call can be squirreled if another
204 // call is made following the native call
Ian Rogersdc51b792011-09-22 20:41:37 -0700205 FrameOffset ReturnValueSaveLocation() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700206
Ian Rogersbdb03912011-09-14 00:55:44 -0700207 // Callee save registers to spill prior to native code (which may clobber)
208 virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0;
209
210 // Spill mask values
211 virtual uint32_t CoreSpillMask() const = 0;
212 virtual uint32_t FpSpillMask() const = 0;
Ian Rogers0d666d82011-08-14 16:03:46 -0700213
Ian Rogers67375ac2011-09-14 00:55:44 -0700214 // Returns true if the method register will have been clobbered during argument
215 // set up
Ian Rogersad42e132011-09-17 20:23:33 -0700216 virtual bool IsMethodRegisterClobberedPreCall() = 0;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700217
Ian Rogersdc51b792011-09-22 20:41:37 -0700218 // An extra scratch register live after the call
219 virtual ManagedRegister ReturnScratchRegister() const = 0;
220
Ian Rogersb033c752011-07-20 12:22:35 -0700221 // Iterator interface
222 bool HasNext();
Ian Rogers67375ac2011-09-14 00:55:44 -0700223 virtual void Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700224 bool IsCurrentParamAReference();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700225 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700226 virtual bool IsCurrentParamInRegister() = 0;
227 virtual bool IsCurrentParamOnStack() = 0;
228 virtual ManagedRegister CurrentParamRegister() = 0;
229 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700230
231 // Iterator interface extension for JNI
Ian Rogers408f79a2011-08-23 18:22:33 -0700232 FrameOffset CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700233
Ian Rogers408f79a2011-08-23 18:22:33 -0700234 // Position of SIRT and interior fields
Ian Rogersdc51b792011-09-22 20:41:37 -0700235 FrameOffset SirtOffset() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700236 return FrameOffset(displacement_.Int32Value() +
237 kPointerSize); // above Method*
238 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700239 FrameOffset SirtNumRefsOffset() const {
Ian Rogers408f79a2011-08-23 18:22:33 -0700240 return FrameOffset(SirtOffset().Int32Value() +
241 StackIndirectReferenceTable::NumberOfReferencesOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700242 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700243 FrameOffset SirtLinkOffset() const {
Ian Rogers408f79a2011-08-23 18:22:33 -0700244 return FrameOffset(SirtOffset().Int32Value() +
245 StackIndirectReferenceTable::LinkOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700246 }
247
Ian Rogers2c8f6532011-09-02 17:16:34 -0700248 virtual ~JniCallingConvention() {}
249
250 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700251 // Named iterator positions
252 enum IteratorPos {
253 kJniEnv = 0,
254 kObjectOrClass = 1
255 };
256
Ian Rogers169c9a72011-11-13 20:13:17 -0800257 explicit JniCallingConvention(bool is_static, bool is_synchronized, const char* shorty) :
258 CallingConvention(is_static, is_synchronized, shorty) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700259
Ian Rogers408f79a2011-08-23 18:22:33 -0700260 // Number of stack slots for outgoing arguments, above which the SIRT is
Ian Rogersb033c752011-07-20 12:22:35 -0700261 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700262 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700263
Ian Rogers2c8f6532011-09-02 17:16:34 -0700264 protected:
Ian Rogers169c9a72011-11-13 20:13:17 -0800265 size_t NumberOfExtraArgumentsForJni();
Ian Rogersb033c752011-07-20 12:22:35 -0700266};
267
268} // namespace art
269
270#endif // ART_SRC_CALLING_CONVENTION_H_