1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "callconv.h"
18 #include "encode.h"
19 #include "mem/pool_manager.h"
20 #include "operands.h"
21 #include "registers_description.h"
22
23 namespace panda::compiler {
24 class ConstructorTest : public ::testing::Test {
25 public:
ConstructorTest()26 ConstructorTest()
27 {
28 panda::mem::MemConfig::Initialize(64_MB, 64_MB, 64_MB, 32_MB);
29 PoolManager::Initialize();
30 allocator_ = new ArenaAllocator(SpaceType::SPACE_TYPE_COMPILER);
31 }
~ConstructorTest()32 ~ConstructorTest()
33 {
34 // For additional destroy MasmHolder:
35 // There are two main ways - make ref-counting in each object
36 // For this - need create additional Free-method for each of them.
37 // Or create and destroy main MasmHolder user - encoder.
38 // Here implemented second way.
39 // Also it is possible to create id for allocators or some another
40 // uniqueness technic. But it may do not solve this issue.
41 Encoder *encoder = nullptr;
42
43 #ifdef PANDA_COMPILER_TARGET_AARCH32
44 encoder = Encoder::Create(GetAllocator(), Arch::AARCH32, false);
45 ASSERT(encoder->InitMasm());
46 encoder->~Encoder();
47 #endif
48 #ifdef PANDA_COMPILER_TARGET_AARCH64
49 encoder = Encoder::Create(GetAllocator(), Arch::AARCH64, false);
50 ASSERT(encoder->InitMasm());
51 encoder->~Encoder();
52 #endif
53 #ifdef PANDA_COMPILER_TARGET_X86
54 encoder = Encoder::Create(GetAllocator(), Arch::X86, false);
55 // TODO! Enable when will supported
56 if (encoder != nullptr) {
57 encoder->~Encoder();
58 }
59 #endif
60 #ifdef PANDA_COMPILER_TARGET_X86_64
61 encoder = Encoder::Create(GetAllocator(), Arch::X86_64, false);
62 ASSERT(encoder->InitMasm());
63 encoder->~Encoder();
64 #endif
65 delete allocator_;
66 PoolManager::Finalize();
67 panda::mem::MemConfig::Finalize();
68 }
69
GetAllocator()70 ArenaAllocator *GetAllocator()
71 {
72 return allocator_;
73 }
74
75 private:
76 ArenaAllocator *allocator_;
77 };
78
TEST_F(ConstructorTest,BackendSupport)79 TEST_F(ConstructorTest, BackendSupport)
80 {
81 #ifdef PANDA_COMPILER_TARGET_AARCH32
82 ASSERT_TRUE(BackendSupport(Arch::AARCH32));
83 #else
84 ASSERT_FALSE(BackendSupport(Arch::AARCH32));
85 #endif
86 #ifdef PANDA_COMPILER_TARGET_AARCH64
87 ASSERT_TRUE(BackendSupport(Arch::AARCH64));
88 #else
89 ASSERT_FALSE(BackendSupport(Arch::AARCH64));
90 #endif
91 #ifdef PANDA_COMPILER_TARGET_X86
92 // TODO! Enable when will supported
93 #endif
94 #ifdef PANDA_COMPILER_TARGET_X86_64
95 ASSERT_TRUE(BackendSupport(Arch::X86_64));
96 #else
97 ASSERT_FALSE(BackendSupport(Arch::X86_64));
98 #endif
99
100 ASSERT_FALSE(BackendSupport(Arch::NONE));
101 }
102
TEST_F(ConstructorTest,Encoder)103 TEST_F(ConstructorTest, Encoder)
104 {
105 Encoder *encoder = nullptr;
106
107 encoder = Encoder::Create(GetAllocator(), Arch::AARCH32, false);
108 #ifdef PANDA_COMPILER_TARGET_AARCH32
109 ASSERT_TRUE(encoder->InitMasm());
110 ASSERT_TRUE(encoder->IsValid());
111 ASSERT_NE(encoder, nullptr);
112 encoder->~Encoder();
113 #else
114 ASSERT_EQ(encoder, nullptr);
115 #endif
116
117 encoder = Encoder::Create(GetAllocator(), Arch::AARCH64, false);
118 #ifdef PANDA_COMPILER_TARGET_AARCH64
119 ASSERT_TRUE(encoder->InitMasm());
120 ASSERT_TRUE(encoder->IsValid());
121 ASSERT_NE(encoder, nullptr);
122 encoder->~Encoder();
123 #else
124 ASSERT_EQ(encoder, nullptr);
125 #endif
126
127 encoder = Encoder::Create(GetAllocator(), Arch::X86, false);
128 #ifdef PANDA_COMPILER_TARGET_X86
129 // TODO! Enable when will supported
130 if (encoder != nullptr) {
131 encoder->~Encoder();
132 }
133 #else
134 ASSERT_EQ(encoder, nullptr);
135 #endif
136
137 encoder = Encoder::Create(GetAllocator(), Arch::X86_64, false);
138 #ifdef PANDA_COMPILER_TARGET_X86_64
139 ASSERT_TRUE(encoder->InitMasm());
140 ASSERT_TRUE(encoder->IsValid());
141 ASSERT_NE(encoder, nullptr);
142 encoder->~Encoder();
143 #else
144 ASSERT_EQ(encoder, nullptr);
145 #endif
146
147 encoder = Encoder::Create(GetAllocator(), Arch::NONE, false);
148 ASSERT_EQ(encoder, nullptr);
149 }
150
TEST_F(ConstructorTest,RegistersDescription)151 TEST_F(ConstructorTest, RegistersDescription)
152 {
153 RegistersDescription *regfile = nullptr;
154
155 regfile = RegistersDescription::Create(GetAllocator(), Arch::AARCH32);
156 #ifdef PANDA_COMPILER_TARGET_AARCH32
157 ASSERT_TRUE(regfile->IsValid());
158 ASSERT_NE(regfile, nullptr);
159 #else
160 ASSERT_EQ(regfile, nullptr);
161 #endif
162
163 regfile = RegistersDescription::Create(GetAllocator(), Arch::AARCH64);
164 #ifdef PANDA_COMPILER_TARGET_AARCH64
165 ASSERT_TRUE(regfile->IsValid());
166 ASSERT_NE(regfile, nullptr);
167 #else
168 ASSERT_EQ(regfile, nullptr);
169 #endif
170
171 regfile = RegistersDescription::Create(GetAllocator(), Arch::X86);
172 #ifdef PANDA_COMPILER_TARGET_X86
173 // TODO! Enable when will supported
174 #else
175 ASSERT_EQ(regfile, nullptr);
176 #endif
177
178 regfile = RegistersDescription::Create(GetAllocator(), Arch::X86_64);
179 #ifdef PANDA_COMPILER_TARGET_X86_64
180 ASSERT_TRUE(regfile->IsValid());
181 ASSERT_NE(regfile, nullptr);
182 #else
183 ASSERT_EQ(regfile, nullptr);
184 #endif
185
186 regfile = RegistersDescription::Create(GetAllocator(), Arch::NONE);
187 ASSERT_EQ(regfile, nullptr);
188 }
189
TEST_F(ConstructorTest,CallingConvention)190 TEST_F(ConstructorTest, CallingConvention)
191 {
192 CallingConvention *callconv = nullptr;
193 RegistersDescription *regfile = nullptr;
194 Encoder *encoder = nullptr;
195
196 callconv = CallingConvention::Create(GetAllocator(), encoder, regfile, Arch::AARCH32);
197 #ifdef PANDA_COMPILER_TARGET_AARCH32
198 ASSERT_TRUE(callconv->IsValid());
199 ASSERT_NE(callconv, nullptr);
200 #else
201 ASSERT_EQ(callconv, nullptr);
202 #endif
203
204 callconv = CallingConvention::Create(GetAllocator(), encoder, regfile, Arch::AARCH64);
205 #ifdef PANDA_COMPILER_TARGET_AARCH64
206 ASSERT_TRUE(callconv->IsValid());
207 ASSERT_NE(callconv, nullptr);
208 #else
209 ASSERT_EQ(callconv, nullptr);
210 #endif
211
212 callconv = CallingConvention::Create(GetAllocator(), encoder, regfile, Arch::X86);
213 #ifdef PANDA_COMPILER_TARGET_X86
214 // TODO! Enable when will supported
215 #else
216 ASSERT_EQ(callconv, nullptr);
217 #endif
218
219 callconv = CallingConvention::Create(GetAllocator(), encoder, regfile, Arch::X86_64);
220 #ifdef PANDA_COMPILER_TARGET_X86_64
221 ASSERT_TRUE(callconv->IsValid());
222 ASSERT_NE(callconv, nullptr);
223 #else
224 ASSERT_EQ(callconv, nullptr);
225 #endif
226
227 callconv = CallingConvention::Create(GetAllocator(), encoder, regfile, Arch::NONE);
228 ASSERT_EQ(callconv, nullptr);
229 }
230 } // namespace panda::compiler
231