Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "deoptimize_stack_visitor.h" |
| 18 | |
| 19 | #include "mirror/art_method-inl.h" |
| 20 | #include "object_utils.h" |
| 21 | #include "quick_exception_handler.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 22 | #include "handle_scope-inl.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 23 | #include "verifier/method_verifier.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | bool DeoptimizeStackVisitor::VisitFrame() { |
| 28 | exception_handler_->SetHandlerFrameId(GetFrameId()); |
| 29 | mirror::ArtMethod* method = GetMethod(); |
| 30 | if (method == nullptr) { |
| 31 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 32 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 33 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 34 | return false; // End stack walk. |
| 35 | } else if (method->IsRuntimeMethod()) { |
| 36 | // Ignore callee save method. |
| 37 | DCHECK(method->IsCalleeSaveMethod()); |
| 38 | return true; |
| 39 | } else { |
| 40 | return HandleDeoptimization(method); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool DeoptimizeStackVisitor::HandleDeoptimization(mirror::ArtMethod* m) { |
| 45 | MethodHelper mh(m); |
| 46 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 47 | CHECK(code_item != nullptr); |
| 48 | uint16_t num_regs = code_item->registers_size_; |
| 49 | uint32_t dex_pc = GetDexPc(); |
| 50 | const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc); |
| 51 | uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits(); |
| 52 | ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 53 | StackHandleScope<2> hs(self_); |
| 54 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache())); |
| 55 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader())); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 56 | verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, |
| 57 | &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
| 58 | m->GetAccessFlags(), false, true); |
| 59 | verifier.Verify(); |
| 60 | std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc); |
| 61 | for (uint16_t reg = 0; reg < num_regs; ++reg) { |
| 62 | VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); |
| 63 | switch (kind) { |
| 64 | case kUndefined: |
| 65 | new_frame->SetVReg(reg, 0xEBADDE09); |
| 66 | break; |
| 67 | case kConstant: |
| 68 | new_frame->SetVReg(reg, kinds.at((reg * 2) + 1)); |
| 69 | break; |
| 70 | case kReferenceVReg: |
| 71 | new_frame->SetVRegReference(reg, |
| 72 | reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind))); |
| 73 | break; |
| 74 | default: |
| 75 | new_frame->SetVReg(reg, GetVReg(m, reg, kind)); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | if (prev_shadow_frame_ != nullptr) { |
| 80 | prev_shadow_frame_->SetLink(new_frame); |
| 81 | } else { |
Sebastien Hertz | 714f175 | 2014-04-28 15:03:08 +0200 | [diff] [blame] | 82 | self_->SetDeoptimizationShadowFrame(new_frame); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 83 | } |
| 84 | prev_shadow_frame_ = new_frame; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | } // namespace art |