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