1 /*
2 * Copyright (C) 2011 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 "jni_macro_assembler.h"
18
19 #include <algorithm>
20 #include <vector>
21
22 #ifdef ART_ENABLE_CODEGEN_arm
23 #include "arm/jni_macro_assembler_arm_vixl.h"
24 #endif
25 #ifdef ART_ENABLE_CODEGEN_arm64
26 #include "arm64/jni_macro_assembler_arm64.h"
27 #endif
28 #ifdef ART_ENABLE_CODEGEN_riscv64
29 #include "riscv64/jni_macro_assembler_riscv64.h"
30 #endif
31 #ifdef ART_ENABLE_CODEGEN_x86
32 #include "x86/jni_macro_assembler_x86.h"
33 #endif
34 #ifdef ART_ENABLE_CODEGEN_x86_64
35 #include "x86_64/jni_macro_assembler_x86_64.h"
36 #endif
37 #include "base/casts.h"
38 #include "base/globals.h"
39 #include "base/memory_region.h"
40 #include "gc_root.h"
41 #include "jni/jni_env_ext.h"
42 #include "jni/local_reference_table.h"
43 #include "stack_reference.h"
44
45 namespace art HIDDEN {
46
47 using MacroAsm32UniquePtr = std::unique_ptr<JNIMacroAssembler<PointerSize::k32>>;
48
49 template <>
Create(ArenaAllocator * allocator,InstructionSet instruction_set,const InstructionSetFeatures * instruction_set_features)50 MacroAsm32UniquePtr JNIMacroAssembler<PointerSize::k32>::Create(
51 ArenaAllocator* allocator,
52 InstructionSet instruction_set,
53 const InstructionSetFeatures* instruction_set_features) {
54 // TODO: Remove the parameter from API (not needed after Mips target was removed).
55 UNUSED(instruction_set_features);
56
57 switch (instruction_set) {
58 #ifdef ART_ENABLE_CODEGEN_arm
59 case InstructionSet::kArm:
60 case InstructionSet::kThumb2:
61 return MacroAsm32UniquePtr(new (allocator) arm::ArmVIXLJNIMacroAssembler(allocator));
62 #endif
63 #ifdef ART_ENABLE_CODEGEN_x86
64 case InstructionSet::kX86:
65 return MacroAsm32UniquePtr(new (allocator) x86::X86JNIMacroAssembler(allocator));
66 #endif
67 default:
68 UNUSED(allocator);
69 LOG(FATAL) << "Unknown/unsupported 4B InstructionSet: " << instruction_set;
70 UNREACHABLE();
71 }
72 }
73
74 using MacroAsm64UniquePtr = std::unique_ptr<JNIMacroAssembler<PointerSize::k64>>;
75
76 template <>
Create(ArenaAllocator * allocator,InstructionSet instruction_set,const InstructionSetFeatures * instruction_set_features)77 MacroAsm64UniquePtr JNIMacroAssembler<PointerSize::k64>::Create(
78 ArenaAllocator* allocator,
79 InstructionSet instruction_set,
80 const InstructionSetFeatures* instruction_set_features) {
81 // TODO: Remove the parameter from API (not needed after Mips64 target was removed).
82 UNUSED(instruction_set_features);
83
84 switch (instruction_set) {
85 #ifdef ART_ENABLE_CODEGEN_arm64
86 case InstructionSet::kArm64:
87 return MacroAsm64UniquePtr(new (allocator) arm64::Arm64JNIMacroAssembler(allocator));
88 #endif
89 #ifdef ART_ENABLE_CODEGEN_riscv64
90 case InstructionSet::kRiscv64:
91 return MacroAsm64UniquePtr(new (allocator) riscv64::Riscv64JNIMacroAssembler(allocator));
92 #endif
93 #ifdef ART_ENABLE_CODEGEN_x86_64
94 case InstructionSet::kX86_64:
95 return MacroAsm64UniquePtr(new (allocator) x86_64::X86_64JNIMacroAssembler(allocator));
96 #endif
97 default:
98 UNUSED(allocator);
99 LOG(FATAL) << "Unknown/unsupported 8B InstructionSet: " << instruction_set;
100 UNREACHABLE();
101 }
102 }
103
104 template <PointerSize kPointerSize>
LoadGcRootWithoutReadBarrier(ManagedRegister dest,ManagedRegister base,MemberOffset offs)105 void JNIMacroAssembler<kPointerSize>::LoadGcRootWithoutReadBarrier(ManagedRegister dest,
106 ManagedRegister base,
107 MemberOffset offs) {
108 static_assert(sizeof(uint32_t) == sizeof(GcRoot<mirror::Object>));
109 Load(dest, base, offs, sizeof(uint32_t));
110 }
111
112 template
113 void JNIMacroAssembler<PointerSize::k32>::LoadGcRootWithoutReadBarrier(ManagedRegister dest,
114 ManagedRegister base,
115 MemberOffset offs);
116 template
117 void JNIMacroAssembler<PointerSize::k64>::LoadGcRootWithoutReadBarrier(ManagedRegister dest,
118 ManagedRegister base,
119 MemberOffset offs);
120
121 template <PointerSize kPointerSize>
LoadStackReference(ManagedRegister dest,FrameOffset offs)122 void JNIMacroAssembler<kPointerSize>::LoadStackReference(ManagedRegister dest, FrameOffset offs) {
123 static_assert(sizeof(uint32_t) == sizeof(StackReference<mirror::Object>));
124 Load(dest, offs, sizeof(uint32_t));
125 }
126
127 template
128 void JNIMacroAssembler<PointerSize::k32>::LoadStackReference(ManagedRegister dest,
129 FrameOffset offs);
130 template
131 void JNIMacroAssembler<PointerSize::k64>::LoadStackReference(ManagedRegister dest,
132 FrameOffset offs);
133
134 template <PointerSize kPointerSize>
LoadLocalReferenceTableStates(ManagedRegister jni_env_reg,ManagedRegister previous_state_reg,ManagedRegister current_state_reg)135 void JNIMacroAssembler<kPointerSize>::LoadLocalReferenceTableStates(
136 ManagedRegister jni_env_reg,
137 ManagedRegister previous_state_reg,
138 ManagedRegister current_state_reg) {
139 constexpr size_t kLRTSegmentStateSize = sizeof(jni::LRTSegmentState);
140 const MemberOffset previous_state_offset = JNIEnvExt::LrtPreviousStateOffset(kPointerSize);
141 const MemberOffset current_state_offset = JNIEnvExt::LrtSegmentStateOffset(kPointerSize);
142
143 Load(previous_state_reg, jni_env_reg, previous_state_offset, kLRTSegmentStateSize);
144 Load(current_state_reg, jni_env_reg, current_state_offset, kLRTSegmentStateSize);
145 }
146
147 template
148 void JNIMacroAssembler<PointerSize::k32>::LoadLocalReferenceTableStates(
149 ManagedRegister jni_env_reg,
150 ManagedRegister previous_state_reg,
151 ManagedRegister current_state_reg);
152 template
153 void JNIMacroAssembler<PointerSize::k64>::LoadLocalReferenceTableStates(
154 ManagedRegister jni_env_reg,
155 ManagedRegister previous_state_reg,
156 ManagedRegister current_state_reg);
157
158 template <PointerSize kPointerSize>
StoreLocalReferenceTableStates(ManagedRegister jni_env_reg,ManagedRegister previous_state_reg,ManagedRegister current_state_reg)159 void JNIMacroAssembler<kPointerSize>::StoreLocalReferenceTableStates(
160 ManagedRegister jni_env_reg,
161 ManagedRegister previous_state_reg,
162 ManagedRegister current_state_reg) {
163 constexpr size_t kLRTSegmentStateSize = sizeof(jni::LRTSegmentState);
164 const MemberOffset previous_state_offset = JNIEnvExt::LrtPreviousStateOffset(kPointerSize);
165 const MemberOffset segment_state_offset = JNIEnvExt::LrtSegmentStateOffset(kPointerSize);
166
167 Store(jni_env_reg, previous_state_offset, previous_state_reg, kLRTSegmentStateSize);
168 Store(jni_env_reg, segment_state_offset, current_state_reg, kLRTSegmentStateSize);
169 }
170
171 template
172 void JNIMacroAssembler<PointerSize::k32>::StoreLocalReferenceTableStates(
173 ManagedRegister jni_env_reg,
174 ManagedRegister previous_state_reg,
175 ManagedRegister current_state_reg);
176 template
177 void JNIMacroAssembler<PointerSize::k64>::StoreLocalReferenceTableStates(
178 ManagedRegister jni_env_reg,
179 ManagedRegister previous_state_reg,
180 ManagedRegister current_state_reg);
181
182 } // namespace art
183