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 "common_runtime_test.h"
28 #include "compiler.h"
29 #include "oat_file.h"
30 
31 namespace art {
32 namespace mirror {
33 class ClassLoader;
34 }  // namespace mirror
35 
36 class CompiledMethod;
37 class CompilerOptions;
38 class CumulativeLogger;
39 class DexFile;
40 class TimingLogger;
41 class VerificationResults;
42 
43 template<class T> class Handle;
44 
45 class 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   void MakeExecutable(ArtMethod* method, const CompiledMethod* compiled_method)
59       REQUIRES_SHARED(Locks::mutator_lock_);
60 
61  protected:
62   void SetUp();
63 
64   void SetUpRuntimeOptionsImpl();
65 
66   Compiler::Kind GetCompilerKind() const;
67   void SetCompilerKind(Compiler::Kind compiler_kind);
68 
GetCompilerFilter()69   virtual CompilerFilter::Filter GetCompilerFilter() const {
70     return CompilerFilter::kDefaultCompilerFilter;
71   }
72 
73   void TearDown();
74 
75   void CompileMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
76 
77   void CompileDirectMethod(Handle<mirror::ClassLoader> class_loader, const char* class_name,
78                            const char* method_name, const char* signature)
79       REQUIRES_SHARED(Locks::mutator_lock_);
80 
81   void CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader, const char* class_name,
82                             const char* method_name, const char* signature)
83       REQUIRES_SHARED(Locks::mutator_lock_);
84 
85   void ApplyInstructionSet();
86   void OverrideInstructionSetFeatures(InstructionSet instruction_set, const std::string& variant);
87 
88   void ClearBootImageOption();
89 
90   Compiler::Kind compiler_kind_ = Compiler::kOptimizing;
91 
92   InstructionSet instruction_set_ =
93       (kRuntimeISA == InstructionSet::kArm) ? InstructionSet::kThumb2 : kRuntimeISA;
94   // Take the default set of instruction features from the build.
95   std::unique_ptr<const InstructionSetFeatures> instruction_set_features_
96       = InstructionSetFeatures::FromCppDefines();
97 
98   std::unique_ptr<CompilerOptions> compiler_options_;
99   std::unique_ptr<VerificationResults> verification_results_;
100 
101  protected:
102   virtual ClassLinker* GetClassLinker() = 0;
103   virtual Runtime* GetRuntime() = 0;
104 
105  private:
106   class CodeAndMetadata;
107   std::vector<CodeAndMetadata> code_and_metadata_;
108 };
109 
110 template <typename RuntimeBase>
111 class CommonCompilerTestBase : public CommonCompilerTestImpl, public RuntimeBase {
112  public:
SetUp()113   void SetUp() override {
114     RuntimeBase::SetUp();
115     CommonCompilerTestImpl::SetUp();
116   }
SetUpRuntimeOptions(RuntimeOptions * options)117   void SetUpRuntimeOptions(RuntimeOptions* options) override {
118     RuntimeBase::SetUpRuntimeOptions(options);
119     CommonCompilerTestImpl::SetUpRuntimeOptionsImpl();
120   }
TearDown()121   void TearDown() override {
122     CommonCompilerTestImpl::TearDown();
123     RuntimeBase::TearDown();
124   }
125 
126  protected:
GetClassLinker()127   ClassLinker* GetClassLinker() override {
128     return RuntimeBase::class_linker_;
129   }
GetRuntime()130   Runtime* GetRuntime() override {
131     return RuntimeBase::runtime_.get();
132   }
133 };
134 
135 class CommonCompilerTest : public CommonCompilerTestBase<CommonRuntimeTest> {};
136 
137 template <typename Param>
138 class CommonCompilerTestWithParam
139     : public CommonCompilerTestBase<CommonRuntimeTestWithParam<Param>> {};
140 
141 }  // namespace art
142 
143 #endif  // ART_COMPILER_COMMON_COMPILER_TEST_H_
144