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 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "calling_convention.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 4 | |
| 5 | #include "calling_convention_arm.h" |
| 6 | #include "calling_convention_x86.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | #include "logging.h" |
| 8 | #include "utils.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 9 | |
| 10 | namespace art { |
| 11 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 12 | // Offset of Method within the frame |
| 13 | FrameOffset CallingConvention::MethodStackOffset() { |
| 14 | return displacement_; |
| 15 | } |
| 16 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 17 | // Managed runtime calling convention |
| 18 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 19 | ManagedRuntimeCallingConvention* ManagedRuntimeCallingConvention::Create( |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 20 | bool is_static, bool is_synchronized, const char* shorty, InstructionSet instruction_set) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 21 | if (instruction_set == kX86) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 22 | return new x86::X86ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 23 | } else { |
| 24 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 25 | return new arm::ArmManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 29 | bool ManagedRuntimeCallingConvention::HasNext() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 30 | return itr_args_ < NumArgs(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void ManagedRuntimeCallingConvention::Next() { |
| 34 | CHECK(HasNext()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 35 | if (IsCurrentArgExplicit() && // don't query parameter type of implicit args |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 36 | IsParamALongOrDouble(itr_args_)) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 37 | itr_longs_and_doubles_++; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 38 | itr_slots_++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 39 | } |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 40 | if (IsCurrentParamAReference()) { |
| 41 | itr_refs_++; |
| 42 | } |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 43 | itr_args_++; |
| 44 | itr_slots_++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 47 | bool ManagedRuntimeCallingConvention::IsCurrentArgExplicit() { |
| 48 | // Static methods have no implicit arguments, others implicitly pass this |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 49 | return IsStatic() || (itr_args_ != 0); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool ManagedRuntimeCallingConvention::IsCurrentArgPossiblyNull() { |
| 53 | return IsCurrentArgExplicit(); // any user parameter may be null |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 56 | size_t ManagedRuntimeCallingConvention::CurrentParamSize() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 57 | return ParamSize(itr_args_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool ManagedRuntimeCallingConvention::IsCurrentParamAReference() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 61 | return IsParamAReference(itr_args_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 64 | // JNI calling convention |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 65 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 66 | JniCallingConvention* JniCallingConvention::Create(bool is_static, bool is_synchronized, |
| 67 | const char* shorty, |
| 68 | InstructionSet instruction_set) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 69 | if (instruction_set == kX86) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 70 | return new x86::X86JniCallingConvention(is_static, is_synchronized, shorty); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 71 | } else { |
| 72 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 73 | return new arm::ArmJniCallingConvention(is_static, is_synchronized, shorty); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 77 | size_t JniCallingConvention::ReferenceCount() const { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 78 | return NumReferenceArgs() + (IsStatic() ? 1 : 0); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 81 | FrameOffset JniCallingConvention::SavedLocalReferenceCookieOffset() const { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 82 | size_t start_of_sirt = SirtLinkOffset().Int32Value() + kPointerSize; |
| 83 | size_t references_size = kPointerSize * ReferenceCount(); // size excluding header |
| 84 | return FrameOffset(start_of_sirt + references_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 87 | FrameOffset JniCallingConvention::ReturnValueSaveLocation() const { |
| 88 | // Segment state is 4 bytes long |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 89 | return FrameOffset(SavedLocalReferenceCookieOffset().Int32Value() + 4); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 92 | bool JniCallingConvention::HasNext() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 93 | if (itr_args_ <= kObjectOrClass) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } else { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 96 | unsigned int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 97 | return arg_pos < NumArgs(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | void JniCallingConvention::Next() { |
| 102 | CHECK(HasNext()); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 103 | if (itr_args_ > kObjectOrClass) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 104 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 105 | if (IsParamALongOrDouble(arg_pos)) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 106 | itr_longs_and_doubles_++; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 107 | itr_slots_++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 110 | if (IsCurrentParamAReference()) { |
| 111 | itr_refs_++; |
| 112 | } |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 113 | itr_args_++; |
| 114 | itr_slots_++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool JniCallingConvention::IsCurrentParamAReference() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 118 | switch (itr_args_) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 119 | case kJniEnv: |
| 120 | return false; // JNIEnv* |
| 121 | case kObjectOrClass: |
| 122 | return true; // jobject or jclass |
| 123 | default: { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 124 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 125 | return IsParamAReference(arg_pos); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 130 | // Return position of SIRT entry holding reference at the current iterator |
| 131 | // position |
| 132 | FrameOffset JniCallingConvention::CurrentParamSirtEntryOffset() { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 133 | CHECK(IsCurrentParamAReference()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 134 | CHECK_GT(SirtLinkOffset(), SirtNumRefsOffset()); |
| 135 | // Address of 1st SIRT entry |
| 136 | int result = SirtLinkOffset().Int32Value() + kPointerSize; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 137 | result += itr_refs_ * kPointerSize; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 138 | CHECK_GT(result, SirtLinkOffset().Int32Value()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 139 | return FrameOffset(result); |
| 140 | } |
| 141 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 142 | size_t JniCallingConvention::CurrentParamSize() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 143 | if (itr_args_ <= kObjectOrClass) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 144 | return kPointerSize; // JNIEnv or jobject/jclass |
| 145 | } else { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 146 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 147 | return ParamSize(arg_pos); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 151 | size_t JniCallingConvention::NumberOfExtraArgumentsForJni() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 152 | // The first argument is the JNIEnv*. |
| 153 | // Static methods have an extra argument which is the jclass. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 154 | return IsStatic() ? 2 : 1; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 157 | } // namespace art |