blob: ac8e7d3010eaf334f7fdd0e558e770470dd443c8 [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"
Vladimir Marko176362a2022-11-08 11:47:50 +000023#include "base/macros.h"
David Sehr3215fff2018-04-03 17:10:12 -070024#include "base/malloc_arena_pool.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070025#include "common_runtime_test.h" // For ScratchFile
26
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include <sys/stat.h>
28
Andreas Gampe3b165bc2016-08-01 22:07:04 -070029#include <cstdio>
30#include <cstdlib>
31#include <fstream>
32#include <iterator>
Andreas Gampe3b165bc2016-08-01 22:07:04 -070033
Vladimir Marko176362a2022-11-08 11:47:50 +000034namespace art HIDDEN {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070035
36template<typename Ass>
David Srbeckyd6e14e02020-07-01 13:19:17 +010037class JNIMacroAssemblerTest : public AssemblerTestBase {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070038 public:
39 Ass* GetAssembler() {
40 return assembler_.get();
41 }
42
Stefano Cianciulli86c4b392022-06-09 14:46:08 +000043 using TestFn = std::string (*)(JNIMacroAssemblerTest *, Ass *);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070044
Andreas Gampe2e965ac2016-11-03 17:24:15 -070045 void DriverFn(TestFn f, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070046 DriverWrapper(f(this, assembler_.get()), test_name);
47 }
48
49 // This driver assumes the assembler has already been called.
Andreas Gampe2e965ac2016-11-03 17:24:15 -070050 void DriverStr(const std::string& assembly_string, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070051 DriverWrapper(assembly_string, test_name);
52 }
53
Andreas Gampe3b165bc2016-08-01 22:07:04 -070054 protected:
Igor Murashkin2ffb7032017-11-08 13:35:21 -080055 JNIMacroAssemblerTest() {}
Andreas Gampe3b165bc2016-08-01 22:07:04 -070056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010057 void SetUp() override {
David Srbeckyd6e14e02020-07-01 13:19:17 +010058 AssemblerTestBase::SetUp();
Vladimir Marko69d310e2017-10-09 14:12:23 +010059 allocator_.reset(new ArenaAllocator(&pool_));
60 assembler_.reset(CreateAssembler(allocator_.get()));
Andreas Gampe3b165bc2016-08-01 22:07:04 -070061 SetUpHelpers();
62 }
63
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010064 void TearDown() override {
David Srbeckyd6e14e02020-07-01 13:19:17 +010065 AssemblerTestBase::TearDown();
Andreas Gampe3b165bc2016-08-01 22:07:04 -070066 assembler_.reset();
Vladimir Marko69d310e2017-10-09 14:12:23 +010067 allocator_.reset();
Andreas Gampe3b165bc2016-08-01 22:07:04 -070068 }
69
70 // Override this to set up any architecture-specific things, e.g., CPU revision.
Vladimir Markoe764d2e2017-10-05 14:35:55 +010071 virtual Ass* CreateAssembler(ArenaAllocator* allocator) {
72 return new (allocator) Ass(allocator);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070073 }
74
75 // Override this to set up any architecture-specific things, e.g., register vectors.
76 virtual void SetUpHelpers() {}
77
Andreas Gampe3b165bc2016-08-01 22:07:04 -070078 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 Gampe2e965ac2016-11-03 17:24:15 -070083 void DriverWrapper(const std::string& assembly_text, const std::string& test_name) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070084 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 Srbeckyd6e14e02020-07-01 13:19:17 +010090 Driver(*data, assembly_text, test_name);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070091 }
92
David Sehr3215fff2018-04-03 17:10:12 -070093 MallocArenaPool pool_;
Vladimir Marko69d310e2017-10-09 14:12:23 +010094 std::unique_ptr<ArenaAllocator> allocator_;
Andreas Gampe3b165bc2016-08-01 22:07:04 -070095 std::unique_ptr<Ass> assembler_;
Andreas Gampe3b165bc2016-08-01 22:07:04 -070096
97 DISALLOW_COPY_AND_ASSIGN(JNIMacroAssemblerTest);
98};
99
100} // namespace art
101
102#endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_TEST_H_