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_riscv64.h"
20 
21 namespace berberis::riscv64 {
22 
23 namespace {
24 
TEST(CallingConventions_riscv64,Smoke)25 TEST(CallingConventions_riscv64, Smoke) {
26   CallingConventions conv;
27   ArgLocation loc;
28 
29   loc = conv.GetNextIntArgLoc(4, 4);
30   EXPECT_EQ(kArgLocationInt, loc.kind);
31   EXPECT_EQ(0u, loc.offset);
32 
33   loc = conv.GetNextIntArgLoc(8, 8);
34   EXPECT_EQ(kArgLocationInt, loc.kind);
35   EXPECT_EQ(1u, loc.offset);
36 
37   loc = conv.GetNextIntArgLoc(8, 8);
38   EXPECT_EQ(kArgLocationInt, loc.kind);
39   EXPECT_EQ(2u, loc.offset);
40 
41   loc = conv.GetNextIntArgLoc(8, 8);
42   EXPECT_EQ(kArgLocationInt, loc.kind);
43   EXPECT_EQ(3u, loc.offset);
44 
45   loc = conv.GetNextIntArgLoc(2, 2);
46   EXPECT_EQ(kArgLocationInt, loc.kind);
47   EXPECT_EQ(4u, loc.offset);
48 
49   loc = conv.GetNextIntArgLoc(1, 1);
50   EXPECT_EQ(kArgLocationInt, loc.kind);
51   EXPECT_EQ(5u, loc.offset);
52 
53   loc = conv.GetNextIntArgLoc(1, 1);
54   EXPECT_EQ(kArgLocationInt, loc.kind);
55   EXPECT_EQ(6u, loc.offset);
56 
57   loc = conv.GetNextIntArgLoc(2, 2);
58   EXPECT_EQ(kArgLocationInt, loc.kind);
59   EXPECT_EQ(7u, loc.offset);
60 
61   loc = conv.GetNextIntArgLoc(4, 4);
62   EXPECT_EQ(kArgLocationStack, loc.kind);
63   EXPECT_EQ(0u, loc.offset);
64 
65   loc = conv.GetNextIntArgLoc(4, 4);
66   EXPECT_EQ(kArgLocationStack, loc.kind);
67   EXPECT_EQ(8u, loc.offset);
68 
69   loc = conv.GetNextIntArgLoc(4, 4);
70   EXPECT_EQ(kArgLocationStack, loc.kind);
71   EXPECT_EQ(16u, loc.offset);
72 
73   loc = conv.GetNextIntArgLoc(2, 2);
74   EXPECT_EQ(kArgLocationStack, loc.kind);
75   EXPECT_EQ(24u, loc.offset);
76 
77   loc = conv.GetNextIntArgLoc(1, 1);
78   EXPECT_EQ(kArgLocationStack, loc.kind);
79   EXPECT_EQ(32u, loc.offset);
80 
81   loc = conv.GetNextFpArgLoc(8, 8);
82   EXPECT_EQ(kArgLocationFp, loc.kind);
83   EXPECT_EQ(0u, loc.offset);
84 
85   loc = conv.GetNextFpArgLoc(4, 4);
86   EXPECT_EQ(kArgLocationFp, loc.kind);
87   EXPECT_EQ(1u, loc.offset);
88 
89   loc = conv.GetNextFpArgLoc(4, 4);
90   EXPECT_EQ(kArgLocationFp, loc.kind);
91   EXPECT_EQ(2u, loc.offset);
92 
93   loc = conv.GetIntResLoc(1);
94   EXPECT_EQ(kArgLocationInt, loc.kind);
95   EXPECT_EQ(0u, loc.offset);
96 }
97 
TEST(CallingConventions_riscv64,FpSpilling)98 TEST(CallingConventions_riscv64, FpSpilling) {
99   CallingConventions conv;
100   ArgLocation loc;
101 
102   loc = conv.GetNextIntArgLoc(4, 4);
103   EXPECT_EQ(kArgLocationInt, loc.kind);
104   EXPECT_EQ(0U, loc.offset);
105 
106   loc = conv.GetNextIntArgLoc(8, 8);
107   EXPECT_EQ(kArgLocationInt, loc.kind);
108   EXPECT_EQ(1U, loc.offset);
109 
110   loc = conv.GetNextFpArgLoc(4, 4);
111   EXPECT_EQ(kArgLocationFp, loc.kind);
112   EXPECT_EQ(0U, loc.offset);
113 
114   loc = conv.GetNextFpArgLoc(8, 8);
115   EXPECT_EQ(kArgLocationFp, loc.kind);
116   EXPECT_EQ(1U, loc.offset);
117 
118   loc = conv.GetNextFpArgLoc(8, 8);
119   EXPECT_EQ(kArgLocationFp, loc.kind);
120   EXPECT_EQ(2U, loc.offset);
121 
122   loc = conv.GetNextFpArgLoc(8, 8);
123   EXPECT_EQ(kArgLocationFp, loc.kind);
124   EXPECT_EQ(3U, loc.offset);
125 
126   loc = conv.GetNextFpArgLoc(8, 8);
127   EXPECT_EQ(kArgLocationFp, loc.kind);
128   EXPECT_EQ(4U, loc.offset);
129 
130   loc = conv.GetNextFpArgLoc(8, 8);
131   EXPECT_EQ(kArgLocationFp, loc.kind);
132   EXPECT_EQ(5U, loc.offset);
133 
134   loc = conv.GetNextFpArgLoc(8, 8);
135   EXPECT_EQ(kArgLocationFp, loc.kind);
136   EXPECT_EQ(6U, loc.offset);
137 
138   loc = conv.GetNextFpArgLoc(8, 8);
139   EXPECT_EQ(kArgLocationFp, loc.kind);
140   EXPECT_EQ(7U, loc.offset);
141 
142   loc = conv.GetNextFpArgLoc(8, 8);
143   EXPECT_EQ(kArgLocationInt, loc.kind);
144   EXPECT_EQ(2U, loc.offset);
145 
146   loc = conv.GetNextFpArgLoc(8, 8);
147   EXPECT_EQ(kArgLocationInt, loc.kind);
148   EXPECT_EQ(3U, loc.offset);
149 
150   loc = conv.GetNextFpArgLoc(8, 8);
151   EXPECT_EQ(kArgLocationInt, loc.kind);
152   EXPECT_EQ(4U, loc.offset);
153 
154   loc = conv.GetNextFpArgLoc(8, 8);
155   EXPECT_EQ(kArgLocationInt, loc.kind);
156   EXPECT_EQ(5U, loc.offset);
157 
158   loc = conv.GetNextFpArgLoc(8, 8);
159   EXPECT_EQ(kArgLocationInt, loc.kind);
160   EXPECT_EQ(6U, loc.offset);
161 
162   loc = conv.GetNextFpArgLoc(8, 8);
163   EXPECT_EQ(kArgLocationInt, loc.kind);
164   EXPECT_EQ(7U, loc.offset);
165 
166   loc = conv.GetNextFpArgLoc(8, 8);
167   EXPECT_EQ(kArgLocationStack, loc.kind);
168   EXPECT_EQ(0U, loc.offset);
169 
170   loc = conv.GetNextIntArgLoc(8, 8);
171   EXPECT_EQ(kArgLocationStack, loc.kind);
172   EXPECT_EQ(8U, loc.offset);
173 
174   loc = conv.GetNextFpArgLoc(8, 8);
175   EXPECT_EQ(kArgLocationStack, loc.kind);
176   EXPECT_EQ(16U, loc.offset);
177 
178   loc = conv.GetNextIntArgLoc(8, 8);
179   EXPECT_EQ(kArgLocationStack, loc.kind);
180   EXPECT_EQ(24U, loc.offset);
181 
182   loc = conv.GetFpResLoc(4);
183   EXPECT_EQ(kArgLocationFp, loc.kind);
184   EXPECT_EQ(0U, loc.offset);
185 }
186 
187 }  // namespace
188 
189 }  // namespace berberis::riscv64
190