1 /*
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 #ifndef ART_RUNTIME_INTERPRETER_INTERPRETER_SWITCH_IMPL_H_
18 #define ART_RUNTIME_INTERPRETER_INTERPRETER_SWITCH_IMPL_H_
19
20 #include "base/locks.h"
21 #include "base/macros.h"
22 #include "dex/dex_file.h"
23 #include "dex/code_item_accessors.h"
24 #include "jvalue.h"
25 #include "obj_ptr.h"
26
27 namespace art HIDDEN {
28
29 class ShadowFrame;
30 class Thread;
31
32 namespace interpreter {
33
34 // Group all the data that is needed in the switch interpreter.
35 // We need to pass it to the hand-written assembly and back,
36 // so it is easier to pass it through a single pointer.
37 // Similarly, returning the JValue type would be non-trivial.
38 struct SwitchImplContext {
39 Thread* self;
40 const CodeItemDataAccessor& accessor;
41 ShadowFrame& shadow_frame;
42 JValue& result_register;
43 bool interpret_one_instruction;
44 JValue result;
45 };
46
47 // The actual internal implementation of the switch interpreter.
48 template<bool transaction_active>
49 void ExecuteSwitchImplCpp(SwitchImplContext* ctx)
50 REQUIRES_SHARED(Locks::mutator_lock_);
51
52 // Hand-written assembly method which wraps the C++ implementation,
53 // while defining the DEX PC in the CFI so that libunwind can resolve it.
54 extern "C" void ExecuteSwitchImplAsm(SwitchImplContext* ctx, void* impl, const uint16_t* dexpc)
55 REQUIRES_SHARED(Locks::mutator_lock_);
56
57 // Wrapper around the switch interpreter which ensures we can unwind through it.
58 template<bool transaction_active>
ExecuteSwitchImpl(Thread * self,const CodeItemDataAccessor & accessor,ShadowFrame & shadow_frame,JValue result_register,bool interpret_one_instruction)59 ALWAYS_INLINE JValue ExecuteSwitchImpl(Thread* self,
60 const CodeItemDataAccessor& accessor,
61 ShadowFrame& shadow_frame,
62 JValue result_register,
63 bool interpret_one_instruction)
64 REQUIRES_SHARED(Locks::mutator_lock_) {
65 SwitchImplContext ctx {
66 .self = self,
67 .accessor = accessor,
68 .shadow_frame = shadow_frame,
69 .result_register = result_register,
70 .interpret_one_instruction = interpret_one_instruction,
71 .result = JValue(),
72 };
73 void* impl = reinterpret_cast<void*>(&ExecuteSwitchImplCpp<transaction_active>);
74 const uint16_t* dex_pc = ctx.accessor.Insns();
75 ExecuteSwitchImplAsm(&ctx, impl, dex_pc);
76 return ctx.result;
77 }
78
79 } // namespace interpreter
80 } // namespace art
81
82 #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_SWITCH_IMPL_H_
83