• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_
18 #define ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_
19 
20 #include "jni_macro_assembler.h"
21 
22 #include "assembler_test_base.h"
23 #include "base/macros.h"
24 #include "base/malloc_arena_pool.h"
25 #include "common_runtime_test.h"  // For ScratchFile
26 
27 #include <sys/stat.h>
28 
29 #include <cstdio>
30 #include <cstdlib>
31 #include <fstream>
32 #include <iterator>
33 
34 namespace art HIDDEN {
35 
36 template<typename Ass>
37 class JNIMacroAssemblerTest : public AssemblerTestBase {
38  public:
GetAssembler()39   Ass* GetAssembler() {
40     return assembler_.get();
41   }
42 
43   using TestFn = std::string (*)(JNIMacroAssemblerTest *, Ass *);
44 
DriverFn(TestFn f,const std::string & test_name)45   void DriverFn(TestFn f, const std::string& test_name) {
46     DriverWrapper(f(this, assembler_.get()), test_name);
47   }
48 
49   // This driver assumes the assembler has already been called.
DriverStr(const std::string & assembly_string,const std::string & test_name)50   void DriverStr(const std::string& assembly_string, const std::string& test_name) {
51     DriverWrapper(assembly_string, test_name);
52   }
53 
54  protected:
JNIMacroAssemblerTest()55   JNIMacroAssemblerTest() {}
56 
SetUp()57   void SetUp() override {
58     AssemblerTestBase::SetUp();
59     allocator_.reset(new ArenaAllocator(&pool_));
60     assembler_.reset(CreateAssembler(allocator_.get()));
61     SetUpHelpers();
62   }
63 
TearDown()64   void TearDown() override {
65     AssemblerTestBase::TearDown();
66     assembler_.reset();
67     allocator_.reset();
68   }
69 
70   // Override this to set up any architecture-specific things, e.g., CPU revision.
CreateAssembler(ArenaAllocator * allocator)71   virtual Ass* CreateAssembler(ArenaAllocator* allocator) {
72     return new (allocator) Ass(allocator);
73   }
74 
75   // Override this to set up any architecture-specific things, e.g., register vectors.
SetUpHelpers()76   virtual void SetUpHelpers() {}
77 
78  private:
79   // Override this to pad the code with NOPs to a certain size if needed.
Pad(std::vector<uint8_t> & data)80   virtual void Pad([[maybe_unused]] std::vector<uint8_t>& data) {}
81 
DriverWrapper(const std::string & assembly_text,const std::string & test_name)82   void DriverWrapper(const std::string& assembly_text, const std::string& test_name) {
83     assembler_->FinalizeCode();
84     size_t cs = assembler_->CodeSize();
85     std::unique_ptr<std::vector<uint8_t>> data(new std::vector<uint8_t>(cs));
86     MemoryRegion code(&(*data)[0], data->size());
87     assembler_->CopyInstructions(code);
88     Pad(*data);
89     Driver(*data, assembly_text, test_name);
90   }
91 
92   MallocArenaPool pool_;
93   std::unique_ptr<ArenaAllocator> allocator_;
94   std::unique_ptr<Ass> assembler_;
95 
96   DISALLOW_COPY_AND_ASSIGN(JNIMacroAssemblerTest);
97 };
98 
99 }  // namespace art
100 
101 #endif  // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_
102