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