Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 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" |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 23 | #include "base/malloc_arena_pool.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 24 | #include "common_runtime_test.h" // For ScratchFile |
| 25 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 28 | #include <cstdio> |
| 29 | #include <cstdlib> |
| 30 | #include <fstream> |
| 31 | #include <iterator> |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | template<typename Ass> |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 36 | class JNIMacroAssemblerTest : public AssemblerTestBase { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 37 | public: |
| 38 | Ass* GetAssembler() { |
| 39 | return assembler_.get(); |
| 40 | } |
| 41 | |
| 42 | typedef std::string (*TestFn)(JNIMacroAssemblerTest* assembler_test, Ass* assembler); |
| 43 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 44 | void DriverFn(TestFn f, const std::string& test_name) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 45 | DriverWrapper(f(this, assembler_.get()), test_name); |
| 46 | } |
| 47 | |
| 48 | // This driver assumes the assembler has already been called. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 49 | void DriverStr(const std::string& assembly_string, const std::string& test_name) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 50 | DriverWrapper(assembly_string, test_name); |
| 51 | } |
| 52 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 53 | protected: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 54 | JNIMacroAssemblerTest() {} |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 55 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 56 | void SetUp() override { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 57 | AssemblerTestBase::SetUp(); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 58 | allocator_.reset(new ArenaAllocator(&pool_)); |
| 59 | assembler_.reset(CreateAssembler(allocator_.get())); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 60 | SetUpHelpers(); |
| 61 | } |
| 62 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 63 | void TearDown() override { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 64 | AssemblerTestBase::TearDown(); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 65 | assembler_.reset(); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 66 | allocator_.reset(); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Override this to set up any architecture-specific things, e.g., CPU revision. |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 70 | virtual Ass* CreateAssembler(ArenaAllocator* allocator) { |
| 71 | return new (allocator) Ass(allocator); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Override this to set up any architecture-specific things, e.g., register vectors. |
| 75 | virtual void SetUpHelpers() {} |
| 76 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 77 | private: |
| 78 | // Override this to pad the code with NOPs to a certain size if needed. |
| 79 | virtual void Pad(std::vector<uint8_t>& data ATTRIBUTE_UNUSED) { |
| 80 | } |
| 81 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 82 | void DriverWrapper(const std::string& assembly_text, const std::string& test_name) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 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_->FinalizeInstructions(code); |
| 88 | Pad(*data); |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 89 | Driver(*data, assembly_text, test_name); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 92 | MallocArenaPool pool_; |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 93 | std::unique_ptr<ArenaAllocator> allocator_; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 94 | std::unique_ptr<Ass> assembler_; |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(JNIMacroAssemblerTest); |
| 97 | }; |
| 98 | |
| 99 | } // namespace art |
| 100 | |
| 101 | #endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_ |