blob: e77177e43e9ab0a8e2039dee1d30690493052379 [file] [log] [blame]
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001/*
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 Sehr3215fff2018-04-03 17:10:12 -070023#include "base/malloc_arena_pool.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070024#include "common_runtime_test.h" // For ScratchFile
25
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include <sys/stat.h>
27
Andreas Gampe3b165bc2016-08-01 22:07:04 -070028#include <cstdio>
29#include <cstdlib>
30#include <fstream>
31#include <iterator>
Andreas Gampe3b165bc2016-08-01 22:07:04 -070032
33namespace art {
34
35template<typename Ass>
David Srbeckyd6e14e02020-07-01 13:19:17 +010036class JNIMacroAssemblerTest : public AssemblerTestBase {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070037 public:
38 Ass* GetAssembler() {
39 return assembler_.get();
40 }
41
42 typedef std::string (*TestFn)(JNIMacroAssemblerTest* assembler_test, Ass* assembler);
43
Andreas Gampe2e965ac2016-11-03 17:24:15 -070044 void DriverFn(TestFn f, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045 DriverWrapper(f(this, assembler_.get()), test_name);
46 }
47
48 // This driver assumes the assembler has already been called.
Andreas Gampe2e965ac2016-11-03 17:24:15 -070049 void DriverStr(const std::string& assembly_string, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070050 DriverWrapper(assembly_string, test_name);
51 }
52
Andreas Gampe3b165bc2016-08-01 22:07:04 -070053 protected:
Igor Murashkin2ffb7032017-11-08 13:35:21 -080054 JNIMacroAssemblerTest() {}
Andreas Gampe3b165bc2016-08-01 22:07:04 -070055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010056 void SetUp() override {
David Srbeckyd6e14e02020-07-01 13:19:17 +010057 AssemblerTestBase::SetUp();
Vladimir Marko69d310e2017-10-09 14:12:23 +010058 allocator_.reset(new ArenaAllocator(&pool_));
59 assembler_.reset(CreateAssembler(allocator_.get()));
Andreas Gampe3b165bc2016-08-01 22:07:04 -070060 SetUpHelpers();
61 }
62
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010063 void TearDown() override {
David Srbeckyd6e14e02020-07-01 13:19:17 +010064 AssemblerTestBase::TearDown();
Andreas Gampe3b165bc2016-08-01 22:07:04 -070065 assembler_.reset();
Vladimir Marko69d310e2017-10-09 14:12:23 +010066 allocator_.reset();
Andreas Gampe3b165bc2016-08-01 22:07:04 -070067 }
68
69 // Override this to set up any architecture-specific things, e.g., CPU revision.
Vladimir Markoe764d2e2017-10-05 14:35:55 +010070 virtual Ass* CreateAssembler(ArenaAllocator* allocator) {
71 return new (allocator) Ass(allocator);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070072 }
73
74 // Override this to set up any architecture-specific things, e.g., register vectors.
75 virtual void SetUpHelpers() {}
76
Andreas Gampe3b165bc2016-08-01 22:07:04 -070077 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 Gampe2e965ac2016-11-03 17:24:15 -070082 void DriverWrapper(const std::string& assembly_text, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070083 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 Srbeckyd6e14e02020-07-01 13:19:17 +010089 Driver(*data, assembly_text, test_name);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070090 }
91
David Sehr3215fff2018-04-03 17:10:12 -070092 MallocArenaPool pool_;
Vladimir Marko69d310e2017-10-09 14:12:23 +010093 std::unique_ptr<ArenaAllocator> allocator_;
Andreas Gampe3b165bc2016-08-01 22:07:04 -070094 std::unique_ptr<Ass> assembler_;
Andreas Gampe3b165bc2016-08-01 22:07:04 -070095
96 DISALLOW_COPY_AND_ASSIGN(JNIMacroAssemblerTest);
97};
98
99} // namespace art
100
101#endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_