blob: 053e202523aa88e8176bcf9d282bbac7d696a754 [file] [log] [blame]
Dave Allison65fcc2c2014-04-28 13:45:27 -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
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010017#include <dirent.h>
Andreas Gampefd114702015-05-13 17:00:41 -070018#include <errno.h>
Andreas Gampefd114702015-05-13 17:00:41 -070019#include <string.h>
20#include <sys/types.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <fstream>
22#include <map>
Dave Allison65fcc2c2014-04-28 13:45:27 -070023
24#include "gtest/gtest.h"
Artem Serov12e097c2016-08-08 15:13:26 +010025
26#include "jni/quick/calling_convention.h"
27#include "utils/arm/jni_macro_assembler_arm_vixl.h"
28
Dave Allison65fcc2c2014-04-28 13:45:27 -070029#include "base/hex_dump.h"
David Sehr3215fff2018-04-03 17:10:12 -070030#include "base/malloc_arena_pool.h"
Dave Allison65fcc2c2014-04-28 13:45:27 -070031#include "common_runtime_test.h"
32
33namespace art {
34namespace arm {
35
36// Include results file (generated manually)
37#include "assembler_thumb_test_expected.cc.inc"
38
Bilyan Borisovbb661c02016-04-04 16:27:32 +010039#ifndef ART_TARGET_ANDROID
Dave Allison45fdb932014-06-25 12:37:10 -070040// This controls whether the results are printed to the
41// screen or compared against the expected output.
42// To generate new expected output, set this to true and
43// copy the output into the .cc.inc file in the form
44// of the other results.
45//
46// When this is false, the results are not printed to the
47// output, but are compared against the expected results
48// in the .cc.inc file.
Dave Allison65fcc2c2014-04-28 13:45:27 -070049static constexpr bool kPrintResults = false;
Dave Allisond20ddb22014-06-05 14:16:30 -070050#endif
Dave Allison65fcc2c2014-04-28 13:45:27 -070051
52void SetAndroidData() {
53 const char* data = getenv("ANDROID_DATA");
54 if (data == nullptr) {
55 setenv("ANDROID_DATA", "/tmp", 1);
56 }
57}
58
Dave Allison45fdb932014-06-25 12:37:10 -070059int CompareIgnoringSpace(const char* s1, const char* s2) {
60 while (*s1 != '\0') {
61 while (isspace(*s1)) ++s1;
62 while (isspace(*s2)) ++s2;
63 if (*s1 == '\0' || *s1 != *s2) {
64 break;
65 }
66 ++s1;
67 ++s2;
68 }
69 return *s1 - *s2;
70}
71
Vladimir Markocf93a5c2015-06-16 11:33:24 +000072void InitResults() {
73 if (test_results.empty()) {
74 setup_results();
75 }
76}
77
78std::string GetToolsDir() {
Bilyan Borisovbb661c02016-04-04 16:27:32 +010079#ifndef ART_TARGET_ANDROID
Vladimir Markocf93a5c2015-06-16 11:33:24 +000080 // This will only work on the host. There is no as, objcopy or objdump on the device.
Dave Allison65fcc2c2014-04-28 13:45:27 -070081 static std::string toolsdir;
82
Vladimir Markocf93a5c2015-06-16 11:33:24 +000083 if (toolsdir.empty()) {
Dave Allison65fcc2c2014-04-28 13:45:27 -070084 setup_results();
Vladimir Marko33bff252017-11-01 14:35:42 +000085 toolsdir = CommonRuntimeTest::GetAndroidTargetToolsDir(InstructionSet::kThumb2);
Dave Allison65fcc2c2014-04-28 13:45:27 -070086 SetAndroidData();
Dave Allison65fcc2c2014-04-28 13:45:27 -070087 }
88
Vladimir Markocf93a5c2015-06-16 11:33:24 +000089 return toolsdir;
90#else
91 return std::string();
92#endif
93}
94
95void DumpAndCheck(std::vector<uint8_t>& code, const char* testname, const char* const* results) {
Bilyan Borisovbb661c02016-04-04 16:27:32 +010096#ifndef ART_TARGET_ANDROID
Vladimir Markocf93a5c2015-06-16 11:33:24 +000097 static std::string toolsdir = GetToolsDir();
98
Dave Allison65fcc2c2014-04-28 13:45:27 -070099 ScratchFile file;
100
101 const char* filename = file.GetFilename().c_str();
102
103 std::ofstream out(filename);
104 if (out) {
105 out << ".section \".text\"\n";
106 out << ".syntax unified\n";
107 out << ".arch armv7-a\n";
108 out << ".thumb\n";
109 out << ".thumb_func\n";
110 out << ".type " << testname << ", #function\n";
111 out << ".global " << testname << "\n";
112 out << testname << ":\n";
113 out << ".fnstart\n";
114
115 for (uint32_t i = 0 ; i < code.size(); ++i) {
116 out << ".byte " << (static_cast<int>(code[i]) & 0xff) << "\n";
117 }
118 out << ".fnend\n";
119 out << ".size " << testname << ", .-" << testname << "\n";
120 }
121 out.close();
122
Andreas Gampe4470c1d2014-07-21 18:32:59 -0700123 char cmd[1024];
Dave Allison65fcc2c2014-04-28 13:45:27 -0700124
125 // Assemble the .S
David Srbecky3e52aa42015-04-12 07:45:18 +0100126 snprintf(cmd, sizeof(cmd), "%sas %s -o %s.o", toolsdir.c_str(), filename, filename);
Andreas Gampefd114702015-05-13 17:00:41 -0700127 int cmd_result = system(cmd);
128 ASSERT_EQ(cmd_result, 0) << strerror(errno);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700129
Dave Allison65fcc2c2014-04-28 13:45:27 -0700130 // Disassemble.
Rahul Chaudhry956dac22017-09-27 16:33:40 -0700131 snprintf(cmd, sizeof(cmd), "%sobjdump -D -M force-thumb --section=.text %s.o | grep '^ *[0-9a-f][0-9a-f]*:'",
David Srbecky3e52aa42015-04-12 07:45:18 +0100132 toolsdir.c_str(), filename);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700133 if (kPrintResults) {
134 // Print the results only, don't check. This is used to generate new output for inserting
Vladimir Markof5c09c32015-12-17 12:08:08 +0000135 // into the .inc file, so let's add the appropriate prefix/suffix needed in the C++ code.
136 strcat(cmd, " | sed '-es/^/ \"/' | sed '-es/$/\\\\n\",/'");
Andreas Gampefd114702015-05-13 17:00:41 -0700137 int cmd_result3 = system(cmd);
138 ASSERT_EQ(cmd_result3, 0) << strerror(errno);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700139 } else {
140 // Check the results match the appropriate results in the .inc file.
141 FILE *fp = popen(cmd, "r");
142 ASSERT_TRUE(fp != nullptr);
143
Dave Allison65fcc2c2014-04-28 13:45:27 -0700144 uint32_t lineindex = 0;
145
146 while (!feof(fp)) {
147 char testline[256];
148 char *s = fgets(testline, sizeof(testline), fp);
149 if (s == nullptr) {
150 break;
151 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000152 if (CompareIgnoringSpace(results[lineindex], testline) != 0) {
Dave Allison45fdb932014-06-25 12:37:10 -0700153 LOG(FATAL) << "Output is not as expected at line: " << lineindex
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700154 << results[lineindex] << "/" << testline << ", test name: " << testname;
Dave Allison45fdb932014-06-25 12:37:10 -0700155 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700156 ++lineindex;
157 }
158 // Check that we are at the end.
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000159 ASSERT_TRUE(results[lineindex] == nullptr);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700160 fclose(fp);
161 }
162
163 char buf[FILENAME_MAX];
164 snprintf(buf, sizeof(buf), "%s.o", filename);
165 unlink(buf);
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100166#endif // ART_TARGET_ANDROID
Dave Allison65fcc2c2014-04-28 13:45:27 -0700167}
168
Vladimir Marko0e851e22016-08-25 18:17:56 +0100169class ArmVIXLAssemblerTest : public ::testing::Test {
Artem Serov12e097c2016-08-08 15:13:26 +0100170 public:
Vladimir Marko69d310e2017-10-09 14:12:23 +0100171 ArmVIXLAssemblerTest() : pool(), allocator(&pool), assembler(&allocator) { }
Artem Serov12e097c2016-08-08 15:13:26 +0100172
David Sehr3215fff2018-04-03 17:10:12 -0700173 MallocArenaPool pool;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100174 ArenaAllocator allocator;
Roland Levillainc043d002017-07-14 16:39:16 +0100175 ArmVIXLJNIMacroAssembler assembler;
Artem Serov12e097c2016-08-08 15:13:26 +0100176};
177
Artem Serov12e097c2016-08-08 15:13:26 +0100178#define __ assembler->
179
Roland Levillainc043d002017-07-14 16:39:16 +0100180void EmitAndCheck(ArmVIXLJNIMacroAssembler* assembler, const char* testname,
Artem Serov12e097c2016-08-08 15:13:26 +0100181 const char* const* results) {
182 __ FinalizeCode();
183 size_t cs = __ CodeSize();
184 std::vector<uint8_t> managed_code(cs);
185 MemoryRegion code(&managed_code[0], managed_code.size());
186 __ FinalizeInstructions(code);
187
188 DumpAndCheck(managed_code, testname, results);
189}
190
Roland Levillainc043d002017-07-14 16:39:16 +0100191void EmitAndCheck(ArmVIXLJNIMacroAssembler* assembler, const char* testname) {
Artem Serov12e097c2016-08-08 15:13:26 +0100192 InitResults();
193 std::map<std::string, const char* const*>::iterator results = test_results.find(testname);
194 ASSERT_NE(results, test_results.end());
195
196 EmitAndCheck(assembler, testname, results->second);
197}
198
199#undef __
Roland Levillainc043d002017-07-14 16:39:16 +0100200
Artem Serov12e097c2016-08-08 15:13:26 +0100201#define __ assembler.
202
Vladimir Marko0e851e22016-08-25 18:17:56 +0100203TEST_F(ArmVIXLAssemblerTest, VixlJniHelpers) {
Roland Levillain6d729a72017-06-30 18:34:01 +0100204 // Run the test only with Baker read barriers, as the expected
205 // generated code contains a Marking Register refresh instruction.
206 TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();
207
Artem Serov12e097c2016-08-08 15:13:26 +0100208 const bool is_static = true;
209 const bool is_synchronized = false;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700210 const bool is_critical_native = false;
Artem Serov12e097c2016-08-08 15:13:26 +0100211 const char* shorty = "IIFII";
212
Artem Serov12e097c2016-08-08 15:13:26 +0100213 std::unique_ptr<JniCallingConvention> jni_conv(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100214 JniCallingConvention::Create(&allocator,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700215 is_static,
216 is_synchronized,
217 is_critical_native,
218 shorty,
Vladimir Marko33bff252017-11-01 14:35:42 +0000219 InstructionSet::kThumb2));
Artem Serov12e097c2016-08-08 15:13:26 +0100220 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100221 ManagedRuntimeCallingConvention::Create(
Vladimir Marko33bff252017-11-01 14:35:42 +0000222 &allocator, is_static, is_synchronized, shorty, InstructionSet::kThumb2));
Artem Serov12e097c2016-08-08 15:13:26 +0100223 const int frame_size(jni_conv->FrameSize());
224 ArrayRef<const ManagedRegister> callee_save_regs = jni_conv->CalleeSaveRegisters();
225
226 const ManagedRegister method_register = ArmManagedRegister::FromCoreRegister(R0);
227 const ManagedRegister scratch_register = ArmManagedRegister::FromCoreRegister(R12);
228
229 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
230 __ IncreaseFrameSize(32);
231
232 // Loads
233 __ IncreaseFrameSize(4096);
234 __ Load(method_register, FrameOffset(32), 4);
235 __ Load(method_register, FrameOffset(124), 4);
236 __ Load(method_register, FrameOffset(132), 4);
237 __ Load(method_register, FrameOffset(1020), 4);
238 __ Load(method_register, FrameOffset(1024), 4);
239 __ Load(scratch_register, FrameOffset(4092), 4);
240 __ Load(scratch_register, FrameOffset(4096), 4);
241 __ LoadRawPtrFromThread(scratch_register, ThreadOffset32(512));
Vladimir Marko0e851e22016-08-25 18:17:56 +0100242 __ LoadRef(method_register, scratch_register, MemberOffset(128), /* unpoison_reference */ false);
Artem Serov12e097c2016-08-08 15:13:26 +0100243
244 // Stores
245 __ Store(FrameOffset(32), method_register, 4);
246 __ Store(FrameOffset(124), method_register, 4);
247 __ Store(FrameOffset(132), method_register, 4);
248 __ Store(FrameOffset(1020), method_register, 4);
249 __ Store(FrameOffset(1024), method_register, 4);
250 __ Store(FrameOffset(4092), scratch_register, 4);
251 __ Store(FrameOffset(4096), scratch_register, 4);
252 __ StoreImmediateToFrame(FrameOffset(48), 0xFF, scratch_register);
253 __ StoreImmediateToFrame(FrameOffset(48), 0xFFFFFF, scratch_register);
254 __ StoreRawPtr(FrameOffset(48), scratch_register);
255 __ StoreRef(FrameOffset(48), scratch_register);
256 __ StoreSpanning(FrameOffset(48), method_register, FrameOffset(48), scratch_register);
257 __ StoreStackOffsetToThread(ThreadOffset32(512), FrameOffset(4096), scratch_register);
258 __ StoreStackPointerToThread(ThreadOffset32(512));
259
260 // Other
261 __ Call(method_register, FrameOffset(48), scratch_register);
262 __ Copy(FrameOffset(48), FrameOffset(44), scratch_register, 4);
263 __ CopyRawPtrFromThread(FrameOffset(44), ThreadOffset32(512), scratch_register);
264 __ CopyRef(FrameOffset(48), FrameOffset(44), scratch_register);
265 __ GetCurrentThread(method_register);
266 __ GetCurrentThread(FrameOffset(48), scratch_register);
267 __ Move(scratch_register, method_register, 4);
268 __ VerifyObject(scratch_register, false);
269
270 __ CreateHandleScopeEntry(scratch_register, FrameOffset(48), scratch_register, true);
271 __ CreateHandleScopeEntry(scratch_register, FrameOffset(48), scratch_register, false);
272 __ CreateHandleScopeEntry(method_register, FrameOffset(48), scratch_register, true);
273 __ CreateHandleScopeEntry(FrameOffset(48), FrameOffset(64), scratch_register, true);
274 __ CreateHandleScopeEntry(method_register, FrameOffset(0), scratch_register, true);
275 __ CreateHandleScopeEntry(method_register, FrameOffset(1025), scratch_register, true);
276 __ CreateHandleScopeEntry(scratch_register, FrameOffset(1025), scratch_register, true);
277
278 __ ExceptionPoll(scratch_register, 0);
279
Artem Serov8f840f82016-12-15 17:56:27 +0000280 // Push the target out of range of branch emitted by ExceptionPoll.
281 for (int i = 0; i < 64; i++) {
282 __ Store(FrameOffset(2047), scratch_register, 4);
283 }
284
Artem Serov12e097c2016-08-08 15:13:26 +0100285 __ DecreaseFrameSize(4096);
286 __ DecreaseFrameSize(32);
Roland Levillain0d127e12017-07-05 17:01:11 +0100287 __ RemoveFrame(frame_size, callee_save_regs, /* may_suspend */ true);
Artem Serov12e097c2016-08-08 15:13:26 +0100288
289 EmitAndCheck(&assembler, "VixlJniHelpers");
290}
291
Roland Levillainc043d002017-07-14 16:39:16 +0100292#undef __
293
294// TODO: Avoid these macros.
Artem Serov12e097c2016-08-08 15:13:26 +0100295#define R0 vixl::aarch32::r0
296#define R2 vixl::aarch32::r2
297#define R4 vixl::aarch32::r4
298#define R12 vixl::aarch32::r12
Roland Levillainc043d002017-07-14 16:39:16 +0100299
Artem Serov12e097c2016-08-08 15:13:26 +0100300#define __ assembler.asm_.
Artem Serov12e097c2016-08-08 15:13:26 +0100301
Vladimir Marko0e851e22016-08-25 18:17:56 +0100302TEST_F(ArmVIXLAssemblerTest, VixlLoadFromOffset) {
Artem Serov12e097c2016-08-08 15:13:26 +0100303 __ LoadFromOffset(kLoadWord, R2, R4, 12);
304 __ LoadFromOffset(kLoadWord, R2, R4, 0xfff);
305 __ LoadFromOffset(kLoadWord, R2, R4, 0x1000);
306 __ LoadFromOffset(kLoadWord, R2, R4, 0x1000a4);
307 __ LoadFromOffset(kLoadWord, R2, R4, 0x101000);
308 __ LoadFromOffset(kLoadWord, R4, R4, 0x101000);
309 __ LoadFromOffset(kLoadUnsignedHalfword, R2, R4, 12);
310 __ LoadFromOffset(kLoadUnsignedHalfword, R2, R4, 0xfff);
311 __ LoadFromOffset(kLoadUnsignedHalfword, R2, R4, 0x1000);
312 __ LoadFromOffset(kLoadUnsignedHalfword, R2, R4, 0x1000a4);
313 __ LoadFromOffset(kLoadUnsignedHalfword, R2, R4, 0x101000);
314 __ LoadFromOffset(kLoadUnsignedHalfword, R4, R4, 0x101000);
315 __ LoadFromOffset(kLoadWordPair, R2, R4, 12);
316 __ LoadFromOffset(kLoadWordPair, R2, R4, 0x3fc);
317 __ LoadFromOffset(kLoadWordPair, R2, R4, 0x400);
318 __ LoadFromOffset(kLoadWordPair, R2, R4, 0x400a4);
319 __ LoadFromOffset(kLoadWordPair, R2, R4, 0x40400);
320 __ LoadFromOffset(kLoadWordPair, R4, R4, 0x40400);
321
Scott Wakelingb77051e2016-11-21 19:46:00 +0000322 vixl::aarch32::UseScratchRegisterScope temps(assembler.asm_.GetVIXLAssembler());
323 temps.Exclude(R12);
Artem Serov12e097c2016-08-08 15:13:26 +0100324 __ LoadFromOffset(kLoadWord, R0, R12, 12); // 32-bit because of R12.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000325 temps.Include(R12);
Artem Serov12e097c2016-08-08 15:13:26 +0100326 __ LoadFromOffset(kLoadWord, R2, R4, 0xa4 - 0x100000);
327
328 __ LoadFromOffset(kLoadSignedByte, R2, R4, 12);
329 __ LoadFromOffset(kLoadUnsignedByte, R2, R4, 12);
330 __ LoadFromOffset(kLoadSignedHalfword, R2, R4, 12);
331
332 EmitAndCheck(&assembler, "VixlLoadFromOffset");
333}
334
Vladimir Marko0e851e22016-08-25 18:17:56 +0100335TEST_F(ArmVIXLAssemblerTest, VixlStoreToOffset) {
Artem Serov12e097c2016-08-08 15:13:26 +0100336 __ StoreToOffset(kStoreWord, R2, R4, 12);
337 __ StoreToOffset(kStoreWord, R2, R4, 0xfff);
338 __ StoreToOffset(kStoreWord, R2, R4, 0x1000);
339 __ StoreToOffset(kStoreWord, R2, R4, 0x1000a4);
340 __ StoreToOffset(kStoreWord, R2, R4, 0x101000);
341 __ StoreToOffset(kStoreWord, R4, R4, 0x101000);
342 __ StoreToOffset(kStoreHalfword, R2, R4, 12);
343 __ StoreToOffset(kStoreHalfword, R2, R4, 0xfff);
344 __ StoreToOffset(kStoreHalfword, R2, R4, 0x1000);
345 __ StoreToOffset(kStoreHalfword, R2, R4, 0x1000a4);
346 __ StoreToOffset(kStoreHalfword, R2, R4, 0x101000);
347 __ StoreToOffset(kStoreHalfword, R4, R4, 0x101000);
348 __ StoreToOffset(kStoreWordPair, R2, R4, 12);
349 __ StoreToOffset(kStoreWordPair, R2, R4, 0x3fc);
350 __ StoreToOffset(kStoreWordPair, R2, R4, 0x400);
351 __ StoreToOffset(kStoreWordPair, R2, R4, 0x400a4);
352 __ StoreToOffset(kStoreWordPair, R2, R4, 0x40400);
353 __ StoreToOffset(kStoreWordPair, R4, R4, 0x40400);
354
Scott Wakelingb77051e2016-11-21 19:46:00 +0000355 vixl::aarch32::UseScratchRegisterScope temps(assembler.asm_.GetVIXLAssembler());
356 temps.Exclude(R12);
Artem Serov12e097c2016-08-08 15:13:26 +0100357 __ StoreToOffset(kStoreWord, R0, R12, 12); // 32-bit because of R12.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000358 temps.Include(R12);
Artem Serov12e097c2016-08-08 15:13:26 +0100359 __ StoreToOffset(kStoreWord, R2, R4, 0xa4 - 0x100000);
360
361 __ StoreToOffset(kStoreByte, R2, R4, 12);
362
363 EmitAndCheck(&assembler, "VixlStoreToOffset");
364}
365
366#undef __
Dave Allison65fcc2c2014-04-28 13:45:27 -0700367} // namespace arm
368} // namespace art