Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [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 | */ |
| 16 | |
| 17 | #include "stack.h" |
| 18 | |
| 19 | #include "compiler.h" |
| 20 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "object_utils.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 22 | #include "thread_list.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 26 | bool Frame::HasMethod() const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 27 | return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod()); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void Frame::Next() { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 31 | #if defined(ART_USE_LLVM_COMPILER) |
| 32 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 33 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 34 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 35 | DCHECK_NE(frame_size, 0u); |
| 36 | DCHECK_LT(frame_size, 1024u); |
| 37 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
| 38 | sp_ = reinterpret_cast<Method**>(next_sp); |
| 39 | if (*sp_ != NULL) { |
| 40 | DCHECK((*sp_)->GetClass() == Method::GetMethodClass() || |
| 41 | (*sp_)->GetClass() == Method::GetConstructorClass()); |
| 42 | } |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 43 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | uintptr_t Frame::GetReturnPC() const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 47 | #if defined(ART_USE_LLVM_COMPILER) |
| 48 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 49 | return 0; |
| 50 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 51 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 52 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 53 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 54 | } |
| 55 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 56 | void Frame::SetReturnPC(uintptr_t pc) { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 57 | #if defined(ART_USE_LLVM_COMPILER) |
| 58 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 59 | #else |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 60 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 61 | *reinterpret_cast<uintptr_t*>(pc_addr) = pc; |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 62 | #endif |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 67 | * spill or Method* in bytes using Method*. |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 68 | * Note that (reg >= 0) refers to a Dalvik register, (reg == -2) |
| 69 | * denotes Method* and (reg <= -3) denotes a compiler temp. |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 70 | * |
| 71 | * +------------------------+ |
| 72 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 73 | * | . | |
| 74 | * | IN[0] | |
| 75 | * | caller's Method* | |
| 76 | * +========================+ {Note: start of callee's frame} |
| 77 | * | core callee-save spill | {variable sized} |
| 78 | * +------------------------+ |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 79 | * | fp callee-save spill | |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 80 | * +------------------------+ |
buzbee | 70c96d4 | 2012-03-15 15:27:56 -0700 | [diff] [blame] | 81 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 82 | * +------------------------+ |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 83 | * | V[locals-1] | |
| 84 | * | V[locals-2] | |
| 85 | * | . | |
| 86 | * | . | ... (reg == 2) |
| 87 | * | V[1] | ... (reg == 1) |
| 88 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 89 | * +------------------------+ |
| 90 | * | Compiler temps | ... (reg == -2) |
| 91 | * | | ... (reg == -3) |
| 92 | * | | ... (reg == -4) |
| 93 | * +------------------------+ |
| 94 | * | stack alignment padding| {0 to (kStackAlignWords-1) of padding} |
| 95 | * +------------------------+ |
| 96 | * | OUT[outs-1] | |
| 97 | * | OUT[outs-2] | |
| 98 | * | . | |
| 99 | * | OUT[0] | |
| 100 | * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned |
| 101 | * +========================+ |
| 102 | */ |
| 103 | int Frame::GetVRegOffset(const DexFile::CodeItem* code_item, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 104 | uint32_t core_spills, uint32_t fp_spills, |
Elliott Hughes | 5ddbe0b | 2012-06-01 12:58:24 -0700 | [diff] [blame] | 105 | size_t frame_size, int reg) { |
| 106 | DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U); |
buzbee | 70c96d4 | 2012-03-15 15:27:56 -0700 | [diff] [blame] | 107 | int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1 /* filler */; |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 108 | int num_ins = code_item->ins_size_; |
| 109 | int num_regs = code_item->registers_size_ - num_ins; |
| 110 | int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t)); |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 111 | if (reg == -2) { |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 112 | return 0; // Method* |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 113 | } else if (reg <= -3) { |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 114 | return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp |
| 115 | } else if (reg < num_regs) { |
| 116 | return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg |
| 117 | } else { |
| 118 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in |
| 119 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 122 | uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills, |
| 123 | uint32_t fp_spills, size_t frame_size, int vreg) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 124 | #if defined(ART_USE_LLVM_COMPILER) |
| 125 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 126 | return 0; |
| 127 | #else |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 128 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 129 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 130 | return *reinterpret_cast<uint32_t*>(vreg_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 131 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 134 | uint32_t Frame::GetVReg(Method* m, int vreg) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 135 | #if defined(ART_USE_LLVM_COMPILER) |
| 136 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 137 | return 0; |
| 138 | #else |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 139 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 140 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 141 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 142 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 143 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 144 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 145 | return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 146 | #endif |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 150 | #if defined(ART_USE_LLVM_COMPILER) |
| 151 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 152 | #else |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 153 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 154 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 155 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 156 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 157 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 158 | size_t frame_size = m->GetFrameSizeInBytes(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 159 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 160 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
| 161 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 162 | #endif |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 165 | uintptr_t Frame::LoadCalleeSave(int num) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 166 | #if defined(ART_USE_LLVM_COMPILER) |
| 167 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 168 | return 0; |
| 169 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 170 | // Callee saves are held at the top of the frame |
| 171 | Method* method = GetMethod(); |
| 172 | DCHECK(method != NULL); |
| 173 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 174 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
| 175 | #if defined(__i386__) |
| 176 | save_addr -= kPointerSize; // account for return address |
| 177 | #endif |
| 178 | return *reinterpret_cast<uintptr_t*>(save_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 179 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | Method* Frame::NextMethod() const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 183 | #if defined(ART_USE_LLVM_COMPILER) |
| 184 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 185 | return NULL; |
| 186 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 187 | byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes(); |
| 188 | return *reinterpret_cast<Method**>(next_sp); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 189 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 192 | class StackGetter { |
| 193 | public: |
| 194 | StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) { |
| 195 | } |
| 196 | |
| 197 | static void Callback(void* arg) { |
| 198 | reinterpret_cast<StackGetter*>(arg)->Callback(); |
| 199 | } |
| 200 | |
| 201 | jobject GetTrace() { |
| 202 | return trace_; |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | void Callback() { |
| 207 | trace_ = thread_->CreateInternalStackTrace(env_); |
| 208 | } |
| 209 | |
| 210 | JNIEnv* env_; |
| 211 | Thread* thread_; |
| 212 | jobject trace_; |
| 213 | }; |
| 214 | |
| 215 | jobject GetThreadStack(JNIEnv* env, Thread* thread) { |
| 216 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 217 | StackGetter stack_getter(env, thread); |
| 218 | thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter); |
| 219 | return stack_getter.GetTrace(); |
| 220 | } |
| 221 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 222 | } // namespace art |