1 /*
2  * Copyright (C) 2022 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_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
18 #define ART_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
19 
20 #include <string>
21 
22 #include "base/array_ref.h"
23 #include "base/macros.h"
24 
25 namespace art HIDDEN {
26 
27 namespace linker {
28 class LinkerPatch;
29 }  // namespace linker
30 
31 class CompiledMethod;
32 enum class InstructionSet;
33 
34 // Interface for storing AOT-compiled artifacts.
35 // These artifacts include compiled method code and related stack maps and
36 // linker patches as well as the compiled thunk code required for some kinds
37 // of linker patches.
38 //
39 // This interface is used for passing AOT-compiled code and metadata produced
40 // by the `libart-compiler` to `dex2oat`. The `CompiledMethod` created by
41 // `dex2oat` is completely opaque to the `libart-compiler`.
42 class CompiledCodeStorage {
43  public:
44   virtual CompiledMethod* CreateCompiledMethod(InstructionSet instruction_set,
45                                                ArrayRef<const uint8_t> code,
46                                                ArrayRef<const uint8_t> stack_map,
47                                                ArrayRef<const uint8_t> cfi,
48                                                ArrayRef<const linker::LinkerPatch> patches,
49                                                bool is_intrinsic) = 0;
50 
51   // TODO: Rewrite the interface for passing thunks to the `dex2oat` to reduce
52   // locking. The `OptimizingCompiler` is currently calling `GetThunkCode()`
53   // and locking a mutex there for every `LinkerPatch` that needs a thunk to
54   // check whether we need to compile it. Using a thunk compiler interface,
55   // we could drive this from the `dex2oat` side and lock the mutex at most
56   // once per `CreateCompiledMethod()` for any number of patches.
57   virtual ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& patch,
58                                                /*out*/ std::string* debug_name = nullptr) = 0;
59   virtual void SetThunkCode(const linker::LinkerPatch& patch,
60                             ArrayRef<const uint8_t> code,
61                             const std::string& debug_name) = 0;
62 
63  protected:
CompiledCodeStorage()64   CompiledCodeStorage() {}
~CompiledCodeStorage()65   ~CompiledCodeStorage() {}
66 
67  private:
68   DISALLOW_COPY_AND_ASSIGN(CompiledCodeStorage);
69 };
70 
71 }  // namespace art
72 
73 #endif  // ART_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
74