blob: f2eaf008562e25c652d133329328a40653b9f59e [file] [log] [blame]
Sebastien Hertzfd3077e2014-04-23 10:32:43 +02001/*
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"
22#include "sirt_ref-inl.h"
23#include "verifier/method_verifier.h"
24
25namespace art {
26
27bool 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
44bool 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);
53 SirtRef<mirror::DexCache> dex_cache(self_, mh.GetDexCache());
54 SirtRef<mirror::ClassLoader> class_loader(self_, mh.GetClassLoader());
55 verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
56 &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
57 m->GetAccessFlags(), false, true);
58 verifier.Verify();
59 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
60 for (uint16_t reg = 0; reg < num_regs; ++reg) {
61 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
62 switch (kind) {
63 case kUndefined:
64 new_frame->SetVReg(reg, 0xEBADDE09);
65 break;
66 case kConstant:
67 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
68 break;
69 case kReferenceVReg:
70 new_frame->SetVRegReference(reg,
71 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
72 break;
73 default:
74 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
75 break;
76 }
77 }
78 if (prev_shadow_frame_ != nullptr) {
79 prev_shadow_frame_->SetLink(new_frame);
80 } else {
81 exception_handler_->SetTopShadowFrame(new_frame);
82 }
83 prev_shadow_frame_ = new_frame;
84 return true;
85}
86
87} // namespace art