xref: /aosp_15_r20/art/compiler/optimizing/jit_patches_arm64.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2023 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 "code_generation_data.h"
18 #include "gc_root.h"
19 #include "jit_patches_arm64.h"
20 
21 namespace art HIDDEN {
22 
23 namespace arm64 {
24 
DeduplicateUint32Literal(uint32_t value)25 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateUint32Literal(
26     uint32_t value) {
27   return uint32_literals_.GetOrCreate(
28       value,
29       [this, value]() {
30         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(value);
31       });
32 }
33 
DeduplicateUint64Literal(uint64_t value)34 vixl::aarch64::Literal<uint64_t>* JitPatchesARM64::DeduplicateUint64Literal(
35     uint64_t value) {
36   return uint64_literals_.GetOrCreate(
37       value,
38       [this, value]() {
39         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint64_t>(value);
40       });
41 }
42 
PatchJitRootUse(uint8_t * code,const uint8_t * roots_data,vixl::aarch64::Literal<uint32_t> * literal,uint64_t index_in_table)43 static void PatchJitRootUse(uint8_t* code,
44                             const uint8_t* roots_data,
45                             vixl::aarch64::Literal<uint32_t>* literal,
46                             uint64_t index_in_table) {
47   uint32_t literal_offset = literal->GetOffset();
48   uintptr_t address =
49       reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
50   uint8_t* data = code + literal_offset;
51   reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
52 }
53 
EmitJitRootPatches(uint8_t * code,const uint8_t * roots_data,const CodeGenerationData & code_generation_data) const54 void JitPatchesARM64::EmitJitRootPatches(
55     uint8_t* code,
56     const uint8_t* roots_data,
57     const CodeGenerationData& code_generation_data) const {
58   for (const auto& entry : jit_string_patches_) {
59     const StringReference& string_reference = entry.first;
60     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
61     uint64_t index_in_table = code_generation_data.GetJitStringRootIndex(string_reference);
62     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
63   }
64   for (const auto& entry : jit_class_patches_) {
65     const TypeReference& type_reference = entry.first;
66     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
67     uint64_t index_in_table = code_generation_data.GetJitClassRootIndex(type_reference);
68     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
69   }
70   for (const auto& entry : jit_method_type_patches_) {
71     const ProtoReference& proto_reference = entry.first;
72     vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
73     uint64_t index_in_table = code_generation_data.GetJitMethodTypeRootIndex(proto_reference);
74     PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
75   }
76 }
77 
DeduplicateBootImageAddressLiteral(uint64_t address)78 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateBootImageAddressLiteral(
79     uint64_t address) {
80   return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
81 }
82 
DeduplicateJitStringLiteral(const DexFile & dex_file,dex::StringIndex string_index,Handle<mirror::String> handle,CodeGenerationData * code_generation_data)83 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitStringLiteral(
84     const DexFile& dex_file,
85     dex::StringIndex string_index,
86     Handle<mirror::String> handle,
87     CodeGenerationData* code_generation_data) {
88   code_generation_data->ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
89   return jit_string_patches_.GetOrCreate(
90       StringReference(&dex_file, string_index),
91       [this]() {
92         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
93       });
94 }
95 
DeduplicateJitClassLiteral(const DexFile & dex_file,dex::TypeIndex type_index,Handle<mirror::Class> handle,CodeGenerationData * code_generation_data)96 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitClassLiteral(
97     const DexFile& dex_file,
98     dex::TypeIndex type_index,
99     Handle<mirror::Class> handle,
100     CodeGenerationData* code_generation_data) {
101   code_generation_data->ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
102   return jit_class_patches_.GetOrCreate(
103       TypeReference(&dex_file, type_index),
104       [this]() {
105         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
106       });
107 }
108 
DeduplicateJitMethodTypeLiteral(const DexFile & dex_file,dex::ProtoIndex proto_index,Handle<mirror::MethodType> handle,CodeGenerationData * code_generation_data)109 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitMethodTypeLiteral(
110     const DexFile& dex_file,
111     dex::ProtoIndex proto_index,
112     Handle<mirror::MethodType> handle,
113     CodeGenerationData* code_generation_data) {
114   code_generation_data->ReserveJitMethodTypeRoot(ProtoReference(&dex_file, proto_index), handle);
115   return jit_method_type_patches_.GetOrCreate(
116       ProtoReference(&dex_file, proto_index),
117       [this]() {
118         return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
119       });
120 }
121 
122 }  // namespace arm64
123 }  // namespace art
124