blob: 0d88c520d2f11cc0651b8832e9f4f6721aaf27ad [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());
54 std::vector<int32_t> kinds =
55 verifier::MethodVerifier::DescribeVRegs(m->GetDexMethodIndex(), &mh.GetDexFile(),
56 mh.GetDexCache(), mh.GetClassLoader(),
57 mh.GetClassDefIndex(), code_item, m,
58 m->GetAccessFlags(), GetDexPc());
59 for(uint16_t reg = 0; reg < num_regs; reg++) {
60 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
61 switch (kind) {
62 case kUndefined:
63 shadow_frame_->SetVReg(reg, 0xEBADDE09);
64 break;
65 case kConstant:
66 shadow_frame_->SetVReg(reg, kinds.at((reg * 2) + 1));
67 break;
68 default:
69 shadow_frame_->SetVReg(reg, GetVReg(m, reg, kind));
70 break;
71 }
72 }
73 return false; // Stop now we have built the shadow frame.
74 }
75 ShadowFrame* shadow_frame_;
76 uint32_t runtime_frames_;
Ian Rogers7a22fa62013-01-23 12:16:16 -080077 } visitor(self, self->GetLongJumpContext());
Ian Rogers306057f2012-11-26 12:45:53 -080078 visitor.WalkStack(false);
79 if (visitor.shadow_frame_ != NULL) {
80 self->SetDeoptimizationShadowFrame(visitor.shadow_frame_, ret_val);
81 return (*sp)->GetFrameSizeInBytes();
82 } else {
83 return 0; // Caller was an upcall.
84 }
85}
86
87
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088extern "C" JValue artEnterInterpreterFromDeoptimize(Thread* self, mirror::AbstractMethod** sp)
Ian Rogers306057f2012-11-26 12:45:53 -080089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
90 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
91 JValue return_value;
92 UniquePtr<ShadowFrame> shadow_frame(self->GetAndClearDeoptimizationShadowFrame(&return_value));
93 self->EndAssertNoThreadSuspension(NULL);
94 return interpreter::EnterInterpreterFromDeoptimize(self, *shadow_frame.get(), return_value);
95}
96
97} // namespace art