blob: 05c6b2ca579765af10d12fcbcce96ef182b34e5a [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"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010024#include "optimizing/optimizing_unit_test.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010025#include "utils/assembler.h"
26
27#include "optimizing/optimizing_cfi_test_expected.inc"
28
29namespace art {
30
31// Run the tests only on host.
Andreas Gampec60e1b72015-07-30 08:57:50 -070032#ifndef __ANDROID__
David Srbeckyc6b4dd82015-04-07 20:32:43 +010033
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010035 public:
36 // Enable this flag to generate the expected outputs.
37 static constexpr bool kGenerateExpected = false;
38
39 void TestImpl(InstructionSet isa, const char* isa_str,
40 const std::vector<uint8_t>& expected_asm,
41 const std::vector<uint8_t>& expected_cfi) {
42 // Setup simple context.
43 ArenaPool pool;
44 ArenaAllocator allocator(&pool);
45 CompilerOptions opts;
46 std::unique_ptr<const InstructionSetFeatures> isa_features;
47 std::string error;
48 isa_features.reset(InstructionSetFeatures::FromVariant(isa, "default", &error));
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010049 HGraph* graph = CreateGraph(&allocator);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010050 // Generate simple frame with some spills.
David Srbecky46325a02015-04-09 22:51:56 +010051 std::unique_ptr<CodeGenerator> code_gen(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010052 CodeGenerator::Create(graph, isa, *isa_features.get(), opts));
David Srbeckyc6b4dd82015-04-07 20:32:43 +010053 const int frame_size = 64;
54 int core_reg = 0;
55 int fp_reg = 0;
56 for (int i = 0; i < 2; i++) { // Two registers of each kind.
57 for (; core_reg < 32; core_reg++) {
58 if (code_gen->IsCoreCalleeSaveRegister(core_reg)) {
59 auto location = Location::RegisterLocation(core_reg);
60 code_gen->AddAllocatedRegister(location);
61 core_reg++;
62 break;
63 }
64 }
65 for (; fp_reg < 32; fp_reg++) {
66 if (code_gen->IsFloatingPointCalleeSaveRegister(fp_reg)) {
67 auto location = Location::FpuRegisterLocation(fp_reg);
68 code_gen->AddAllocatedRegister(location);
69 fp_reg++;
70 break;
71 }
72 }
73 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010074 ArenaVector<HBasicBlock*> blocks(allocator.Adapter());
Vladimir Markocf93a5c2015-06-16 11:33:24 +000075 code_gen->block_order_ = &blocks;
David Srbeckyc6b4dd82015-04-07 20:32:43 +010076 code_gen->ComputeSpillMask();
77 code_gen->SetFrameSize(frame_size);
78 code_gen->GenerateFrameEntry();
David Srbeckyc34dc932015-04-12 09:27:43 +010079 code_gen->GenerateFrameExit();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010080 // Get the outputs.
David Srbecky46325a02015-04-09 22:51:56 +010081 InternalCodeAllocator code_allocator;
82 code_gen->Finalize(&code_allocator);
83 const std::vector<uint8_t>& actual_asm = code_allocator.GetMemory();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010084 Assembler* opt_asm = code_gen->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010085 const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data());
86
87 if (kGenerateExpected) {
88 GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
89 } else {
90 EXPECT_EQ(expected_asm, actual_asm);
91 EXPECT_EQ(expected_cfi, actual_cfi);
92 }
93 }
David Srbecky46325a02015-04-09 22:51:56 +010094
95 private:
96 class InternalCodeAllocator : public CodeAllocator {
97 public:
98 InternalCodeAllocator() {}
99
100 virtual uint8_t* Allocate(size_t size) {
101 memory_.resize(size);
102 return memory_.data();
103 }
104
105 const std::vector<uint8_t>& GetMemory() { return memory_; }
106
107 private:
108 std::vector<uint8_t> memory_;
109
110 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
111 };
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100112};
113
114#define TEST_ISA(isa) \
115 TEST_F(OptimizingCFITest, isa) { \
116 std::vector<uint8_t> expected_asm(expected_asm_##isa, \
117 expected_asm_##isa + arraysize(expected_asm_##isa)); \
118 std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
119 expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
120 TestImpl(isa, #isa, expected_asm, expected_cfi); \
121 }
122
123TEST_ISA(kThumb2)
124TEST_ISA(kArm64)
125TEST_ISA(kX86)
126TEST_ISA(kX86_64)
127
Andreas Gampec60e1b72015-07-30 08:57:50 -0700128#endif // __ANDROID__
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100129
130} // namespace art