blob: c77d034c0ddf1fb4a02d60964eeffbb43b916f74 [file] [log] [blame]
Ian Rogers306057f2012-11-26 12:45:53 -08001/*
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
17#include "callee_save_frame.h"
18#include "interpreter/interpreter.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/abstract_method-inl.h"
20#include "mirror/object_array-inl.h"
Ian Rogers306057f2012-11-26 12:45:53 -080021#include "object_utils.h"
22#include "stack.h"
23#include "thread.h"
24#include "verifier/method_verifier.h"
25
26namespace art {
27
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028extern "C" uint64_t artDeoptimize(JValue ret_val, Thread* self, mirror::AbstractMethod** sp)
Ian Rogers306057f2012-11-26 12:45:53 -080029 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
30 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
31 // Return value may hold Object* so avoid suspension.
32 const char* old_cause = self->StartAssertNoThreadSuspension("Deoptimizing stack frame");
33 CHECK(old_cause == NULL);
34 class DeoptimizationVisitor : public StackVisitor {
35 public:
Ian Rogers7a22fa62013-01-23 12:16:16 -080036 DeoptimizationVisitor(Thread* thread, Context* context)
37 : StackVisitor(thread, context), shadow_frame_(NULL), runtime_frames_(0) { }
Ian Rogers306057f2012-11-26 12:45:53 -080038
39 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 mirror::AbstractMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -080041 if (m->IsRuntimeMethod()) {
42 if (runtime_frames_ == 0) {
43 runtime_frames_++;
44 return true; // Skip the callee save frame.
45 } else {
46 return false; // Caller was an upcall.
47 }
48 }
49 MethodHelper mh(m);
50 const DexFile::CodeItem* code_item = mh.GetCodeItem();
51 CHECK(code_item != NULL);
52 uint16_t num_regs = code_item->registers_size_;
53 shadow_frame_ = ShadowFrame::Create(num_regs, NULL, m, GetDexPc());
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054 std::vector<int32_t> kinds = DescribeVRegs(m->GetDexMethodIndex(), &mh.GetDexFile(),
55 mh.GetDexCache(), mh.GetClassLoader(),
56 mh.GetClassDefIndex(), code_item, m,
57 m->GetAccessFlags(), GetDexPc());
Ian Rogers306057f2012-11-26 12:45:53 -080058 for(uint16_t reg = 0; reg < num_regs; reg++) {
59 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
60 switch (kind) {
61 case kUndefined:
62 shadow_frame_->SetVReg(reg, 0xEBADDE09);
63 break;
64 case kConstant:
65 shadow_frame_->SetVReg(reg, kinds.at((reg * 2) + 1));
66 break;
67 default:
68 shadow_frame_->SetVReg(reg, GetVReg(m, reg, kind));
69 break;
70 }
71 }
72 return false; // Stop now we have built the shadow frame.
73 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -080074
75 std::vector<int32_t> DescribeVRegs(uint32_t dex_method_idx,
76 const DexFile* dex_file,
77 mirror::DexCache* dex_cache,
78 mirror::ClassLoader* class_loader,
79 uint32_t class_def_idx,
80 const DexFile::CodeItem* code_item,
81 mirror::AbstractMethod* method,
82 uint32_t method_access_flags, uint32_t dex_pc)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
84 verifier::MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
85 dex_method_idx, method, method_access_flags, true);
86 verifier.Verify();
87 return verifier.DescribeVRegs(dex_pc);
88 }
89
Ian Rogers306057f2012-11-26 12:45:53 -080090 ShadowFrame* shadow_frame_;
91 uint32_t runtime_frames_;
Ian Rogers7a22fa62013-01-23 12:16:16 -080092 } visitor(self, self->GetLongJumpContext());
Ian Rogers306057f2012-11-26 12:45:53 -080093 visitor.WalkStack(false);
94 if (visitor.shadow_frame_ != NULL) {
95 self->SetDeoptimizationShadowFrame(visitor.shadow_frame_, ret_val);
96 return (*sp)->GetFrameSizeInBytes();
97 } else {
98 return 0; // Caller was an upcall.
99 }
100}
101
102
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103extern "C" JValue artEnterInterpreterFromDeoptimize(Thread* self, mirror::AbstractMethod** sp)
Ian Rogers306057f2012-11-26 12:45:53 -0800104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
105 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
106 JValue return_value;
107 UniquePtr<ShadowFrame> shadow_frame(self->GetAndClearDeoptimizationShadowFrame(&return_value));
108 self->EndAssertNoThreadSuspension(NULL);
109 return interpreter::EnterInterpreterFromDeoptimize(self, *shadow_frame.get(), return_value);
110}
111
112} // namespace art