1 /*
2 * Copyright (C) 2023 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 "gtest/gtest.h"
18
19 #include "berberis/calling_conventions/calling_conventions_x86_64.h"
20
21 namespace berberis::x86_64 {
22
23 namespace {
24
TEST(CallingConventions_x86_64,Smoke)25 TEST(CallingConventions_x86_64, Smoke) {
26 CallingConventions conv;
27 ArgLocation loc;
28
29 loc = conv.GetNextIntArgLoc(1, 1);
30 EXPECT_EQ(loc.kind, kArgLocationInt);
31 EXPECT_EQ(loc.offset, 0u);
32
33 loc = conv.GetNextIntArgLoc(16, 16);
34 EXPECT_EQ(loc.kind, kArgLocationInt);
35 EXPECT_EQ(loc.offset, 1u);
36
37 loc = conv.GetNextIntArgLoc(8, 8);
38 EXPECT_EQ(loc.kind, kArgLocationInt);
39 EXPECT_EQ(loc.offset, 3u);
40
41 loc = conv.GetNextIntArgLoc(16, 16);
42 EXPECT_EQ(loc.kind, kArgLocationInt);
43 EXPECT_EQ(loc.offset, 4u);
44
45 loc = conv.GetNextIntArgLoc(1, 1);
46 EXPECT_EQ(loc.kind, kArgLocationStack);
47 EXPECT_EQ(loc.offset, 0u);
48
49 loc = conv.GetNextIntArgLoc(1, 1);
50 EXPECT_EQ(loc.kind, kArgLocationStack);
51 EXPECT_EQ(loc.offset, 8u);
52
53 loc = conv.GetNextFpArgLoc(8, 8);
54 EXPECT_EQ(loc.kind, kArgLocationSimd);
55 EXPECT_EQ(loc.offset, 0u);
56
57 loc = conv.GetNextFpArgLoc(8, 8);
58 EXPECT_EQ(loc.kind, kArgLocationSimd);
59 EXPECT_EQ(loc.offset, 1u);
60
61 loc = conv.GetNextFpArgLoc(8, 8);
62 EXPECT_EQ(loc.kind, kArgLocationSimd);
63 EXPECT_EQ(loc.offset, 2u);
64
65 loc = conv.GetNextFpArgLoc(8, 8);
66 EXPECT_EQ(loc.kind, kArgLocationSimd);
67 EXPECT_EQ(loc.offset, 3u);
68
69 loc = conv.GetNextFpArgLoc(8, 8);
70 EXPECT_EQ(loc.kind, kArgLocationSimd);
71 EXPECT_EQ(loc.offset, 4u);
72
73 loc = conv.GetNextFpArgLoc(8, 8);
74 EXPECT_EQ(loc.kind, kArgLocationSimd);
75 EXPECT_EQ(loc.offset, 5u);
76
77 loc = conv.GetNextFpArgLoc(8, 8);
78 EXPECT_EQ(loc.kind, kArgLocationSimd);
79 EXPECT_EQ(loc.offset, 6u);
80
81 loc = conv.GetNextFpArgLoc(8, 8);
82 EXPECT_EQ(loc.kind, kArgLocationSimd);
83 EXPECT_EQ(loc.offset, 7u);
84
85 loc = conv.GetNextFpArgLoc(8, 8);
86 EXPECT_EQ(loc.kind, kArgLocationStack);
87 EXPECT_EQ(loc.offset, 16u);
88
89 loc = conv.GetIntResLoc(1);
90 EXPECT_EQ(loc.kind, kArgLocationIntOut);
91 EXPECT_EQ(loc.offset, 0u);
92
93 loc = conv.GetFpResLoc(8);
94 EXPECT_EQ(loc.kind, kArgLocationSimd);
95 EXPECT_EQ(loc.offset, 0u);
96 }
97
TEST(CallingConventions_x86_64,LastIntRegUsed)98 TEST(CallingConventions_x86_64, LastIntRegUsed) {
99 CallingConventions conv;
100 ArgLocation loc;
101
102 // Use 5 of 6 int regs.
103 conv.GetNextIntArgLoc(4, 4);
104 conv.GetNextIntArgLoc(4, 4);
105 conv.GetNextIntArgLoc(4, 4);
106 conv.GetNextIntArgLoc(4, 4);
107 loc = conv.GetNextIntArgLoc(4, 4);
108 EXPECT_EQ(loc.kind, kArgLocationInt);
109 EXPECT_EQ(loc.offset, 4u);
110
111 // Add param that doesn't fit in the last reg.
112 loc = conv.GetNextIntArgLoc(16, 16);
113 EXPECT_EQ(loc.kind, kArgLocationStack);
114 EXPECT_EQ(loc.offset, 0u);
115
116 // Add param that fits in the last reg.
117 loc = conv.GetNextIntArgLoc(4, 4);
118 EXPECT_EQ(loc.kind, kArgLocationInt);
119 EXPECT_EQ(loc.offset, 5u);
120 }
121
122 } // namespace
123
124 } // namespace berberis::x86_64
125