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 #ifndef ART_COMPILER_COMMON_COMPILER_TEST_H_
18 #define ART_COMPILER_COMMON_COMPILER_TEST_H_
19 
20 #include <list>
21 #include <vector>
22 
23 #include <jni.h>
24 
25 #include "arch/instruction_set.h"
26 #include "arch/instruction_set_features.h"
27 #include "base/macros.h"
28 #include "common_runtime_test.h"
29 #include "compiler.h"
30 #include "oat/oat_file.h"
31 
32 namespace art HIDDEN {
33 namespace mirror {
34 class ClassLoader;
35 }  // namespace mirror
36 
37 class CompilerOptions;
38 class CumulativeLogger;
39 class DexFile;
40 class TimingLogger;
41 
42 template<class T> class Handle;
43 
44 // Export all symbols in `CommonCompilerTestImpl` for dex2oat tests.
45 class EXPORT CommonCompilerTestImpl {
46  public:
47   static std::unique_ptr<CompilerOptions> CreateCompilerOptions(InstructionSet instruction_set,
48                                                                 const std::string& variant);
49 
50   CommonCompilerTestImpl();
51   virtual ~CommonCompilerTestImpl();
52 
53   // Create an executable copy of the code with given metadata.
54   const void* MakeExecutable(ArrayRef<const uint8_t> code,
55                              ArrayRef<const uint8_t> vmap_table,
56                              InstructionSet instruction_set);
57 
58  protected:
59   void SetUp();
60 
61   void SetUpRuntimeOptionsImpl();
62 
GetCompilerFilter()63   virtual CompilerFilter::Filter GetCompilerFilter() const {
64     return CompilerFilter::kDefaultCompilerFilter;
65   }
66 
67   void TearDown();
68 
69   void CompileMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
70 
71   void ApplyInstructionSet();
72   void OverrideInstructionSetFeatures(InstructionSet instruction_set, const std::string& variant);
73 
74   void ClearBootImageOption();
75 
76   InstructionSet instruction_set_ =
77       (kRuntimeISA == InstructionSet::kArm) ? InstructionSet::kThumb2 : kRuntimeISA;
78   // Take the default set of instruction features from the build.
79   std::unique_ptr<const InstructionSetFeatures> instruction_set_features_
80       = InstructionSetFeatures::FromCppDefines();
81 
82   std::unique_ptr<CompilerOptions> compiler_options_;
83 
84  protected:
85   virtual ClassLinker* GetClassLinker() = 0;
86   virtual Runtime* GetRuntime() = 0;
87   class OneCompiledMethodStorage;
88 
89  private:
90   class CodeAndMetadata;
91 
92   std::vector<CodeAndMetadata> code_and_metadata_;
93 };
94 
95 template <typename RuntimeBase>
96 class CommonCompilerTestBase : public CommonCompilerTestImpl, public RuntimeBase {
97  public:
SetUp()98   void SetUp() override {
99     RuntimeBase::SetUp();
100     CommonCompilerTestImpl::SetUp();
101   }
SetUpRuntimeOptions(RuntimeOptions * options)102   void SetUpRuntimeOptions(RuntimeOptions* options) override {
103     RuntimeBase::SetUpRuntimeOptions(options);
104     CommonCompilerTestImpl::SetUpRuntimeOptionsImpl();
105   }
TearDown()106   void TearDown() override {
107     CommonCompilerTestImpl::TearDown();
108     RuntimeBase::TearDown();
109   }
110 
111  protected:
GetClassLinker()112   ClassLinker* GetClassLinker() override {
113     return RuntimeBase::class_linker_;
114   }
GetRuntime()115   Runtime* GetRuntime() override {
116     return RuntimeBase::runtime_.get();
117   }
118 };
119 
120 class CommonCompilerTest : public CommonCompilerTestBase<CommonRuntimeTest> {};
121 
122 template <typename Param>
123 class CommonCompilerTestWithParam
124     : public CommonCompilerTestBase<CommonRuntimeTestWithParam<Param>> {};
125 
126 }  // namespace art
127 
128 #endif  // ART_COMPILER_COMMON_COMPILER_TEST_H_
129