Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_INVOKE_ARG_ARRAY_BUILDER_H_ |
| 18 | #define ART_RUNTIME_INVOKE_ARG_ARRAY_BUILDER_H_ |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "mirror/art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/object.h" |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | static inline size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) { |
| 27 | size_t num_bytes = 0; |
| 28 | for (size_t i = 1; i < shorty_len; ++i) { |
| 29 | char ch = shorty[i]; |
| 30 | if (ch == 'D' || ch == 'J') { |
| 31 | num_bytes += 8; |
| 32 | } else if (ch == 'L') { |
| 33 | // Argument is a reference or an array. The shorty descriptor |
| 34 | // does not distinguish between these types. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | num_bytes += sizeof(mirror::Object*); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 36 | } else { |
| 37 | num_bytes += 4; |
| 38 | } |
| 39 | } |
| 40 | return num_bytes; |
| 41 | } |
| 42 | |
| 43 | class ArgArray { |
| 44 | public: |
| 45 | explicit ArgArray(const char* shorty, uint32_t shorty_len) |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 46 | : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | size_t num_slots = shorty_len + 1; // +1 in case of receiver. |
| 48 | if (LIKELY((num_slots * 2) < kSmallArgArraySize)) { |
| 49 | // We can trivially use the small arg array. |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 50 | arg_array_ = small_arg_array_; |
| 51 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 52 | // Analyze shorty to see if we need the large arg array. |
| 53 | for (size_t i = 1; i < shorty_len; ++i) { |
| 54 | char c = shorty[i]; |
| 55 | if (c == 'J' || c == 'D') { |
| 56 | num_slots++; |
| 57 | } |
| 58 | } |
| 59 | if (num_slots <= kSmallArgArraySize) { |
| 60 | arg_array_ = small_arg_array_; |
| 61 | } else { |
| 62 | large_arg_array_.reset(new uint32_t[num_slots]); |
| 63 | arg_array_ = large_arg_array_.get(); |
| 64 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 68 | uint32_t* GetArray() { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 69 | return arg_array_; |
| 70 | } |
| 71 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 72 | uint32_t GetNumBytes() { |
| 73 | return num_bytes_; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 76 | void Append(uint32_t value) { |
| 77 | arg_array_[num_bytes_ / 4] = value; |
| 78 | num_bytes_ += 4; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 81 | void AppendWide(uint64_t value) { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 82 | // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4). |
Jeff Hao | 10efeec | 2013-03-29 14:12:59 -0700 | [diff] [blame] | 83 | #if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__)) |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 84 | if (num_bytes_ % 8 == 0) { |
| 85 | num_bytes_ += 4; |
| 86 | } |
| 87 | #endif |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 88 | arg_array_[num_bytes_ / 4] = value; |
| 89 | arg_array_[(num_bytes_ / 4) + 1] = value >> 32; |
| 90 | num_bytes_ += 8; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 93 | void BuildArgArray(const ScopedObjectAccess& soa, mirror::Object* receiver, va_list ap) |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 95 | // Set receiver if non-null (method is not static) |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 96 | if (receiver != NULL) { |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 97 | Append(reinterpret_cast<int32_t>(receiver)); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 98 | } |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 99 | for (size_t i = 1; i < shorty_len_; ++i) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 100 | switch (shorty_[i]) { |
| 101 | case 'Z': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 102 | case 'B': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 103 | case 'C': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 104 | case 'S': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 105 | case 'I': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 106 | Append(va_arg(ap, jint)); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 107 | break; |
| 108 | case 'F': { |
| 109 | JValue value; |
| 110 | value.SetF(va_arg(ap, jdouble)); |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 111 | Append(value.GetI()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | case 'L': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 115 | Append(reinterpret_cast<int32_t>(soa.Decode<mirror::Object*>(va_arg(ap, jobject)))); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 116 | break; |
| 117 | case 'D': { |
| 118 | JValue value; |
| 119 | value.SetD(va_arg(ap, jdouble)); |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 120 | AppendWide(value.GetJ()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | case 'J': { |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 124 | AppendWide(va_arg(ap, jlong)); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void BuildArgArray(const ScopedObjectAccess& soa, mirror::Object* receiver, jvalue* args) |
| 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 133 | // Set receiver if non-null (method is not static) |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 134 | if (receiver != NULL) { |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 135 | Append(reinterpret_cast<int32_t>(receiver)); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 136 | } |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 137 | for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 138 | switch (shorty_[i]) { |
| 139 | case 'Z': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 140 | Append(args[args_offset].z); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 141 | break; |
| 142 | case 'B': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 143 | Append(args[args_offset].b); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 144 | break; |
| 145 | case 'C': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 146 | Append(args[args_offset].c); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 147 | break; |
| 148 | case 'S': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 149 | Append(args[args_offset].s); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 150 | break; |
| 151 | case 'I': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 152 | case 'F': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 153 | Append(args[args_offset].i); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 154 | break; |
| 155 | case 'L': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 156 | Append(reinterpret_cast<int32_t>(soa.Decode<mirror::Object*>(args[args_offset].l))); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 157 | break; |
| 158 | case 'D': |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 159 | case 'J': |
Jeff Hao | aabe8ad | 2013-03-18 12:28:09 -0700 | [diff] [blame] | 160 | AppendWide(args[args_offset].j); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 166 | |
| 167 | void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset) |
| 168 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 169 | // Set receiver if non-null (method is not static) |
| 170 | size_t cur_arg = arg_offset; |
| 171 | if (!shadow_frame->GetMethod()->IsStatic()) { |
| 172 | Append(shadow_frame->GetVReg(cur_arg)); |
| 173 | cur_arg++; |
| 174 | } |
| 175 | for (size_t i = 1; i < shorty_len_; ++i) { |
| 176 | switch (shorty_[i]) { |
| 177 | case 'Z': |
| 178 | case 'B': |
| 179 | case 'C': |
| 180 | case 'S': |
| 181 | case 'I': |
| 182 | case 'F': |
| 183 | case 'L': |
| 184 | Append(shadow_frame->GetVReg(cur_arg)); |
| 185 | cur_arg++; |
| 186 | break; |
| 187 | case 'D': |
| 188 | case 'J': |
| 189 | AppendWide(shadow_frame->GetVRegLong(cur_arg)); |
| 190 | cur_arg++; |
| 191 | cur_arg++; |
| 192 | break; |
| 193 | } |
| 194 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private: |
| 198 | enum { kSmallArgArraySize = 16 }; |
| 199 | const char* const shorty_; |
| 200 | const uint32_t shorty_len_; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 201 | uint32_t num_bytes_; |
| 202 | uint32_t* arg_array_; |
| 203 | uint32_t small_arg_array_[kSmallArgArraySize]; |
| 204 | UniquePtr<uint32_t[]> large_arg_array_; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace art |
| 208 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 209 | #endif // ART_RUNTIME_INVOKE_ARG_ARRAY_BUILDER_H_ |