Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: irogers@google.com (Ian Rogers) |
| 3 | |
| 4 | #ifndef ART_SRC_CALLING_CONVENTION_H_ |
| 5 | #define ART_SRC_CALLING_CONVENTION_H_ |
| 6 | |
| 7 | #include "src/managed_register.h" |
| 8 | #include "src/object.h" |
| 9 | #include "src/thread.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | // Top-level abstraction for different calling conventions |
| 14 | class CallingConvention { |
| 15 | public: |
| 16 | CallingConvention* GetCallingConvention(Method* method); |
| 17 | |
| 18 | bool IsReturnAReference() const { return method_->IsReturnAReference(); } |
| 19 | |
| 20 | // Register that holds the incoming method argument |
| 21 | ManagedRegister MethodRegister(); |
| 22 | // Register that holds result of this method |
| 23 | ManagedRegister ReturnRegister(); |
| 24 | // Register reserved for scratch usage during procedure calls |
| 25 | ManagedRegister InterproceduralScratchRegister(); |
| 26 | |
| 27 | // Iterator interface |
| 28 | |
| 29 | // Place iterator at start of arguments. The displacement is applied to |
| 30 | // frame offset methods to account for frames which may be on the stack |
| 31 | // below the one being iterated over. |
| 32 | void ResetIterator(FrameOffset displacement) { |
| 33 | displacement_ = displacement; |
| 34 | itr_position_ = 0; |
| 35 | itr_longs_and_doubles_ = 0; |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | explicit CallingConvention(Method* method) : displacement_(0), |
| 40 | method_(method) {} |
| 41 | const Method* GetMethod() const { return method_; } |
| 42 | |
| 43 | // position along argument list |
| 44 | unsigned int itr_position_; |
| 45 | // number of longs and doubles seen along argument list |
| 46 | unsigned int itr_longs_and_doubles_; |
| 47 | // Space for frames below this on the stack |
| 48 | FrameOffset displacement_; |
| 49 | |
| 50 | private: |
| 51 | const Method* method_; |
| 52 | }; |
| 53 | |
| 54 | // Abstraction for managed code's calling conventions |
| 55 | class ManagedRuntimeCallingConvention : public CallingConvention { |
| 56 | public: |
| 57 | explicit ManagedRuntimeCallingConvention(Method* method) : |
| 58 | CallingConvention(method) {} |
| 59 | |
| 60 | size_t FrameSize(); |
| 61 | |
| 62 | // Iterator interface |
| 63 | bool HasNext(); |
| 64 | void Next(); |
| 65 | bool IsCurrentParamAReference(); |
| 66 | bool IsCurrentParamInRegister(); |
| 67 | bool IsCurrentParamOnStack(); |
| 68 | bool IsCurrentParamPossiblyNull(); |
| 69 | size_t CurrentParamSizeInBytes(); |
| 70 | ManagedRegister CurrentParamRegister(); |
| 71 | FrameOffset CurrentParamStackOffset(); |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(ManagedRuntimeCallingConvention); |
| 74 | }; |
| 75 | |
| 76 | // Abstraction for JNI calling conventions |
| 77 | // | incoming stack args | <-- Prior SP |
| 78 | // | { Spilled registers | |
| 79 | // | & return address } | |
| 80 | // | { Saved JNI Env Data } | |
| 81 | // | { Stack Handle Block | |
| 82 | // | ... | |
| 83 | // | length/link } | (here to prior SP is frame size) |
| 84 | // | Method* | <-- Anchor SP written to thread |
| 85 | // | { Outgoing stack args | |
| 86 | // | ... } | <-- SP at point of call |
| 87 | // | Native frame | |
| 88 | class JniCallingConvention : public CallingConvention { |
| 89 | public: |
| 90 | explicit JniCallingConvention(Method* native_method) : |
| 91 | CallingConvention(native_method) {} |
| 92 | |
| 93 | // Size of frame excluding space for outgoing args (its assumed Method* is |
| 94 | // always at the bottom of a frame, but this doesn't work for outgoing |
| 95 | // native args). Includes alignment. |
| 96 | size_t FrameSize(); |
| 97 | // Size of outgoing arguments, including alignment |
| 98 | size_t OutArgSize(); |
| 99 | // Number of handles in stack handle block |
| 100 | size_t HandleCount(); |
| 101 | |
| 102 | // Iterator interface |
| 103 | bool HasNext(); |
| 104 | void Next(); |
| 105 | bool IsCurrentParamAReference(); |
| 106 | bool IsCurrentParamInRegister(); |
| 107 | bool IsCurrentParamOnStack(); |
| 108 | size_t CurrentParamSizeInBytes(); |
| 109 | ManagedRegister CurrentParamRegister(); |
| 110 | FrameOffset CurrentParamStackOffset(); |
| 111 | |
| 112 | // Iterator interface extension for JNI |
| 113 | FrameOffset CurrentParamHandleOffset(); |
| 114 | |
| 115 | // Position of stack handle block and interior fields |
| 116 | FrameOffset ShbOffset() { |
| 117 | return FrameOffset(displacement_.Int32Value() + |
| 118 | kPointerSize); // above Method* |
| 119 | } |
| 120 | FrameOffset ShbNumRefsOffset() { |
| 121 | return FrameOffset(ShbOffset().Int32Value() + |
| 122 | StackHandleBlock::NumberOfReferencesOffset()); |
| 123 | } |
| 124 | FrameOffset ShbLinkOffset() { |
| 125 | return FrameOffset(ShbOffset().Int32Value() + |
| 126 | StackHandleBlock::LinkOffset()); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | // Named iterator positions |
| 131 | enum IteratorPos { |
| 132 | kJniEnv = 0, |
| 133 | kObjectOrClass = 1 |
| 134 | }; |
| 135 | |
| 136 | // Number of stack slots for outgoing arguments, above which handles are |
| 137 | // located |
| 138 | size_t NumberOfOutgoingStackArgs(); |
| 139 | |
| 140 | DISALLOW_COPY_AND_ASSIGN(JniCallingConvention); |
| 141 | }; |
| 142 | |
| 143 | } // namespace art |
| 144 | |
| 145 | #endif // ART_SRC_CALLING_CONVENTION_H_ |