blob: 8602255cc58ce60a4a5110bbc1abb3273c90e8d8 [file] [log] [blame]
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001/*
2 * Copyright (C) 2015 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#include <memory>
18#include <vector>
19
20#include "arch/instruction_set.h"
21#include "cfi_test.h"
22#include "gtest/gtest.h"
23#include "optimizing/code_generator.h"
24#include "utils/assembler.h"
25
26#include "optimizing/optimizing_cfi_test_expected.inc"
27
28namespace art {
29
30// Run the tests only on host.
31#ifndef HAVE_ANDROID_OS
32
33class OptimizingCFITest : public CFITest {
34 public:
35 // Enable this flag to generate the expected outputs.
36 static constexpr bool kGenerateExpected = false;
37
38 void TestImpl(InstructionSet isa, const char* isa_str,
39 const std::vector<uint8_t>& expected_asm,
40 const std::vector<uint8_t>& expected_cfi) {
41 // Setup simple context.
42 ArenaPool pool;
43 ArenaAllocator allocator(&pool);
44 CompilerOptions opts;
45 std::unique_ptr<const InstructionSetFeatures> isa_features;
46 std::string error;
47 isa_features.reset(InstructionSetFeatures::FromVariant(isa, "default", &error));
48 HGraph graph(&allocator);
49 // Generate simple frame with some spills.
50 auto code_gen = CodeGenerator::Create(&graph, isa, *isa_features.get(), opts);
51 const int frame_size = 64;
52 int core_reg = 0;
53 int fp_reg = 0;
54 for (int i = 0; i < 2; i++) { // Two registers of each kind.
55 for (; core_reg < 32; core_reg++) {
56 if (code_gen->IsCoreCalleeSaveRegister(core_reg)) {
57 auto location = Location::RegisterLocation(core_reg);
58 code_gen->AddAllocatedRegister(location);
59 core_reg++;
60 break;
61 }
62 }
63 for (; fp_reg < 32; fp_reg++) {
64 if (code_gen->IsFloatingPointCalleeSaveRegister(fp_reg)) {
65 auto location = Location::FpuRegisterLocation(fp_reg);
66 code_gen->AddAllocatedRegister(location);
67 fp_reg++;
68 break;
69 }
70 }
71 }
72 code_gen->ComputeSpillMask();
73 code_gen->SetFrameSize(frame_size);
74 code_gen->GenerateFrameEntry();
75 code_gen->GetInstructionVisitor()->VisitReturnVoid(new (&allocator) HReturnVoid());
76 // Get the outputs.
77 Assembler* opt_asm = code_gen->GetAssembler();
78 std::vector<uint8_t> actual_asm(opt_asm->CodeSize());
79 MemoryRegion code(&actual_asm[0], actual_asm.size());
80 opt_asm->FinalizeInstructions(code);
81 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
82
83 if (kGenerateExpected) {
84 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
85 } else {
86 EXPECT_EQ(expected_asm, actual_asm);
87 EXPECT_EQ(expected_cfi, actual_cfi);
88 }
89 }
90};
91
92#define TEST_ISA(isa) \
93 TEST_F(OptimizingCFITest, isa) { \
94 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
95 expected_asm_##isa + arraysize(expected_asm_##isa)); \
96 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
97 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
98 TestImpl(isa, #isa, expected_asm, expected_cfi); \
99 }
100
101TEST_ISA(kThumb2)
102TEST_ISA(kArm64)
103TEST_ISA(kX86)
104TEST_ISA(kX86_64)
105
106#endif // HAVE_ANDROID_OS
107
108} // namespace art