Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 2 | |
| 3 | #ifndef ART_SRC_CALLING_CONVENTION_H_ |
| 4 | #define ART_SRC_CALLING_CONVENTION_H_ |
| 5 | |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 6 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | #include "managed_register.h" |
| 8 | #include "object.h" |
| 9 | #include "thread.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | // Top-level abstraction for different calling conventions |
| 14 | class CallingConvention { |
| 15 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | bool IsReturnAReference() const { return method_->IsReturnAReference(); } |
| 17 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 18 | size_t SizeOfReturnValue() const { return method_->ReturnSize(); } |
| 19 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 20 | // Register that holds result of this method |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 21 | virtual ManagedRegister ReturnRegister() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 22 | // Register reserved for scratch usage during procedure calls |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 23 | virtual ManagedRegister InterproceduralScratchRegister() = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 24 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 25 | // Offset of Method within the frame |
| 26 | FrameOffset MethodStackOffset(); |
| 27 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 28 | // Iterator interface |
| 29 | |
| 30 | // Place iterator at start of arguments. The displacement is applied to |
| 31 | // frame offset methods to account for frames which may be on the stack |
| 32 | // below the one being iterated over. |
| 33 | void ResetIterator(FrameOffset displacement) { |
| 34 | displacement_ = displacement; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 35 | itr_slots_ = 0; |
| 36 | itr_args_ = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 37 | itr_refs_ = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 38 | itr_longs_and_doubles_ = 0; |
| 39 | } |
| 40 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 41 | virtual ~CallingConvention() {} |
| 42 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 43 | protected: |
| 44 | explicit CallingConvention(Method* method) : displacement_(0), |
| 45 | method_(method) {} |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 46 | |
| 47 | Method* GetMethod() const { return method_; } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 48 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 49 | // The slot number for current calling_convention argument. |
| 50 | // Note that each slot is 32-bit. When the current argument is bigger |
| 51 | // than 32 bits, return the first slot number for this argument. |
| 52 | unsigned int itr_slots_; |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 53 | // The number of references iterated past |
| 54 | unsigned int itr_refs_; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 55 | // The argument number along argument list for current argument |
| 56 | unsigned int itr_args_; |
| 57 | // Number of longs and doubles seen along argument list |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 58 | unsigned int itr_longs_and_doubles_; |
| 59 | // Space for frames below this on the stack |
| 60 | FrameOffset displacement_; |
| 61 | |
| 62 | private: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 63 | Method* method_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Abstraction for managed code's calling conventions |
| 67 | class ManagedRuntimeCallingConvention : public CallingConvention { |
| 68 | public: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 69 | static ManagedRuntimeCallingConvention* Create(Method* native_method, |
| 70 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 71 | |
| 72 | size_t FrameSize(); |
| 73 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 74 | // Register that holds the incoming method argument |
| 75 | virtual ManagedRegister MethodRegister() = 0; |
| 76 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 77 | // Iterator interface |
| 78 | bool HasNext(); |
| 79 | void Next(); |
| 80 | bool IsCurrentParamAReference(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 81 | bool IsCurrentArgExplicit(); // ie a non-implict argument such as this |
| 82 | bool IsCurrentArgPossiblyNull(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 83 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 84 | virtual bool IsCurrentParamInRegister() = 0; |
| 85 | virtual bool IsCurrentParamOnStack() = 0; |
| 86 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 87 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 88 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 89 | virtual ~ManagedRuntimeCallingConvention() {} |
| 90 | |
| 91 | protected: |
| 92 | explicit ManagedRuntimeCallingConvention(Method* method) : |
| 93 | CallingConvention(method) {} |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // Abstraction for JNI calling conventions |
| 97 | // | incoming stack args | <-- Prior SP |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 98 | // | { Return address } | (x86) |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 99 | // | { Return value spill } | (live on return slow paths) |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 100 | // | { Stack Indirect Ref. | |
| 101 | // | Table... | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 102 | // | num. refs./link } | (here to prior SP is frame size) |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 103 | // | { Spill area } | (ARM) |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 104 | // | Method* | <-- Anchor SP written to thread |
| 105 | // | { Outgoing stack args | |
| 106 | // | ... } | <-- SP at point of call |
| 107 | // | Native frame | |
| 108 | class JniCallingConvention : public CallingConvention { |
| 109 | public: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 110 | static JniCallingConvention* Create(Method* native_method, |
| 111 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | |
| 113 | // Size of frame excluding space for outgoing args (its assumed Method* is |
| 114 | // always at the bottom of a frame, but this doesn't work for outgoing |
| 115 | // native args). Includes alignment. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 116 | virtual size_t FrameSize() = 0; |
Ian Rogers | 762400c | 2011-08-23 12:14:16 -0700 | [diff] [blame] | 117 | // Offset within the frame of the return pc |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 118 | virtual size_t ReturnPcOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 119 | // Size of outgoing arguments, including alignment |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 120 | virtual size_t OutArgSize() = 0; |
| 121 | // Size of area used to hold spilled registers |
| 122 | virtual size_t SpillAreaSize() = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 123 | // Number of references in stack indirect reference table |
| 124 | size_t ReferenceCount(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 125 | // Location where the return value of a call can be squirreled if another |
| 126 | // call is made following the native call |
| 127 | FrameOffset ReturnValueSaveLocation(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 128 | |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 129 | // Registers that must be spilled (due to clobbering) before the call into |
| 130 | // the native routine |
| 131 | const std::vector<ManagedRegister>& RegsToSpillPreCall() { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 132 | return spill_regs_; |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 135 | // Returns true if the register will be clobbered by an outgoing |
| 136 | // argument value. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 137 | virtual bool IsOutArgRegister(ManagedRegister reg) = 0; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 138 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 139 | // Iterator interface |
| 140 | bool HasNext(); |
| 141 | void Next(); |
| 142 | bool IsCurrentParamAReference(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 143 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 144 | virtual bool IsCurrentParamInRegister() = 0; |
| 145 | virtual bool IsCurrentParamOnStack() = 0; |
| 146 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 147 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 148 | |
| 149 | // Iterator interface extension for JNI |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 150 | FrameOffset CurrentParamSirtEntryOffset(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 151 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 152 | // Position of SIRT and interior fields |
| 153 | FrameOffset SirtOffset() { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 154 | return FrameOffset(displacement_.Int32Value() + |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 155 | SpillAreaSize() + |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 156 | kPointerSize); // above Method* |
| 157 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 158 | FrameOffset SirtNumRefsOffset() { |
| 159 | return FrameOffset(SirtOffset().Int32Value() + |
| 160 | StackIndirectReferenceTable::NumberOfReferencesOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 161 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 162 | FrameOffset SirtLinkOffset() { |
| 163 | return FrameOffset(SirtOffset().Int32Value() + |
| 164 | StackIndirectReferenceTable::LinkOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 167 | virtual ~JniCallingConvention() {} |
| 168 | |
| 169 | protected: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 170 | // Named iterator positions |
| 171 | enum IteratorPos { |
| 172 | kJniEnv = 0, |
| 173 | kObjectOrClass = 1 |
| 174 | }; |
| 175 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 176 | explicit JniCallingConvention(Method* native_method) : |
| 177 | CallingConvention(native_method) {} |
| 178 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 179 | // Number of stack slots for outgoing arguments, above which the SIRT is |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 180 | // located |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 181 | virtual size_t NumberOfOutgoingStackArgs() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 182 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 183 | protected: |
| 184 | static size_t NumberOfExtraArgumentsForJni(Method* method); |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 185 | |
| 186 | // Extra registers to spill before the call into native |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 187 | std::vector<ManagedRegister> spill_regs_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace art |
| 191 | |
| 192 | #endif // ART_SRC_CALLING_CONVENTION_H_ |