blob: 13fd3ae4ee45ea362392c560b6ad6e40aca2c735 [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"
19#include "object.h" // for JValue
20#include "object_utils.h"
21#include "stack.h"
22#include "thread.h"
23#include "verifier/method_verifier.h"
24
25namespace art {
26
27extern "C" uint64_t artDeoptimize(JValue ret_val, Thread* self, AbstractMethod** sp)
28 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
30 // Return value may hold Object* so avoid suspension.
31 const char* old_cause = self->StartAssertNoThreadSuspension("Deoptimizing stack frame");
32 CHECK(old_cause == NULL);
33 class DeoptimizationVisitor : public StackVisitor {
34 public:
35 DeoptimizationVisitor(const ManagedStack* stack,
36 const std::deque<InstrumentationStackFrame>* instrumentation_stack,
37 Context* context)
38 : StackVisitor(stack, instrumentation_stack, context), shadow_frame_(NULL),
39 runtime_frames_(0) { }
40
41 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
42 AbstractMethod* m = GetMethod();
43 if (m->IsRuntimeMethod()) {
44 if (runtime_frames_ == 0) {
45 runtime_frames_++;
46 return true; // Skip the callee save frame.
47 } else {
48 return false; // Caller was an upcall.
49 }
50 }
51 MethodHelper mh(m);
52 const DexFile::CodeItem* code_item = mh.GetCodeItem();
53 CHECK(code_item != NULL);
54 uint16_t num_regs = code_item->registers_size_;
55 shadow_frame_ = ShadowFrame::Create(num_regs, NULL, m, GetDexPc());
56 std::vector<int32_t> kinds =
57 verifier::MethodVerifier::DescribeVRegs(m->GetDexMethodIndex(), &mh.GetDexFile(),
58 mh.GetDexCache(), mh.GetClassLoader(),
59 mh.GetClassDefIndex(), code_item, m,
60 m->GetAccessFlags(), GetDexPc());
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 shadow_frame_->SetVReg(reg, 0xEBADDE09);
66 break;
67 case kConstant:
68 shadow_frame_->SetVReg(reg, kinds.at((reg * 2) + 1));
69 break;
70 default:
71 shadow_frame_->SetVReg(reg, GetVReg(m, reg, kind));
72 break;
73 }
74 }
75 return false; // Stop now we have built the shadow frame.
76 }
77 ShadowFrame* shadow_frame_;
78 uint32_t runtime_frames_;
79 } visitor(self->GetManagedStack(), self->GetInstrumentationStack(), self->GetLongJumpContext());
80 visitor.WalkStack(false);
81 if (visitor.shadow_frame_ != NULL) {
82 self->SetDeoptimizationShadowFrame(visitor.shadow_frame_, ret_val);
83 return (*sp)->GetFrameSizeInBytes();
84 } else {
85 return 0; // Caller was an upcall.
86 }
87}
88
89
90extern "C" JValue artEnterInterpreterFromDeoptimize(Thread* self, AbstractMethod** sp)
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
92 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
93 JValue return_value;
94 UniquePtr<ShadowFrame> shadow_frame(self->GetAndClearDeoptimizationShadowFrame(&return_value));
95 self->EndAssertNoThreadSuspension(NULL);
96 return interpreter::EnterInterpreterFromDeoptimize(self, *shadow_frame.get(), return_value);
97}
98
99} // namespace art