1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstddef>
17 #include <cstdint>
18 #include <gtest/gtest.h>
19 #include <sstream>
20 #include <type_traits>
21
22 #include "bytecode_instruction-inl.h"
23
24 namespace panda::test {
25
TEST(BytecodeInstruction,Signed)26 TEST(BytecodeInstruction, Signed)
27 {
28 {
29 // jeqz 23
30 const uint8_t bytecode[] = {0x4f, 0x17};
31 BytecodeInstruction inst(bytecode);
32 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x4f);
33 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0>()), static_cast<int8_t>(0x17));
34 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0, true>()),
35 static_cast<int8_t>(0x17));
36 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x17));
37 EXPECT_EQ(inst.GetImmCount(), 1);
38 }
39
40 {
41 // jmp -22
42 const uint8_t bytecode[] = {0x4d, 0xea};
43 BytecodeInstruction inst(bytecode);
44 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x4d);
45 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), static_cast<int8_t>(-22));
46 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0, true>()), static_cast<int8_t>(-22));
47 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(-22));
48 EXPECT_EQ(inst.GetImmCount(), 1);
49 }
50
51 {
52 // ldai 30
53 const uint8_t bytecode[] = {0x62, 0x1e, 0x00, 0x00, 0x00};
54 BytecodeInstruction inst(bytecode);
55 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x62);
56 EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM32);
57 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0>()), static_cast<int32_t>(0x1e));
58 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0, true>()), static_cast<int32_t>(0x1e));
59 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x1e));
60 EXPECT_EQ(inst.GetImmCount(), 1);
61 }
62
63 {
64 // fldai 3.14
65 const uint8_t bytecode[] = {0x63, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e, 0x09, 0x40};
66 BytecodeInstruction inst(bytecode);
67 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x63);
68 EXPECT_EQ((bit_cast<double>(inst.GetImm<BytecodeInstruction::Format::IMM64, 0, true>())), 3.14);
69 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x40091eb851eb851f));
70 EXPECT_EQ(inst.GetImmCount(), 1);
71 }
72 }
73
TEST(BytecodeInstruction,UnsignedOneImm)74 TEST(BytecodeInstruction, UnsignedOneImm)
75 {
76 {
77 // callthis2 142, v0, v2, v3
78 const uint8_t bytecode[] = {0x2f, 0x8e, 0x00, 0x02, 0x03};
79 BytecodeInstruction inst(bytecode);
80 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x2f);
81 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0>()), static_cast<int8_t>(0x8e));
82 EXPECT_NE((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0>()), static_cast<uint8_t>(0x8e));
83 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0, false>()),
84 static_cast<uint8_t>(0x8e));
85 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x8e));
86 EXPECT_EQ(inst.GetImmCount(), 1);
87 }
88
89 {
90 // neg 13
91 const uint8_t bytecode[] = {0x1f, 0x0d};
92 BytecodeInstruction inst(bytecode);
93 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x1f);
94 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), static_cast<int8_t>(0x0d));
95 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0, false>()),
96 static_cast<uint8_t>(0x0d));
97 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x0d));
98 EXPECT_EQ(inst.GetImmCount(), 1);
99 }
100
101 {
102 // tryldglobalbyname 128, id6
103 const uint8_t bytecode[] = {0x8c, 0x80, 0x00, 0x06, 0x00};
104 BytecodeInstruction inst(bytecode);
105 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x8c);
106 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16_ID16, 0>()), static_cast<int16_t>(0x80));
107 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16_ID16, 0, false>()),
108 static_cast<uint16_t>(0x80));
109 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0x80));
110 EXPECT_EQ(inst.GetImmCount(), 1);
111 }
112 }
113
TEST(BytecodeInstruction,UnsignedTwoImm)114 TEST(BytecodeInstruction, UnsignedTwoImm)
115 {
116 {
117 // stlexvar 0, 2
118 const uint8_t bytecode[] = {0x3d, 0x20};
119 BytecodeInstruction inst(bytecode);
120 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x3d);
121 EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM4_IMM4);
122 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 0>()), 0);
123 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 1>()), 2);
124 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 0, false>()), 0);
125 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 1, false>()), 2);
126 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(0));
127 EXPECT_EQ(inst.GetImmData(1), static_cast<int64_t>(2));
128 EXPECT_EQ(inst.GetImmCount(), 2);
129 }
130
131 {
132 // definefunc 2, id8, 1
133 const uint8_t bytecode[] = {0x33, 0x02, 0x08, 0x00, 0x01};
134 BytecodeInstruction inst(bytecode);
135 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x33);
136 EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM8_ID16_IMM8);
137 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 0>()), static_cast<int8_t>(2));
138 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 1>()), static_cast<int8_t>(1));
139 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 0, false>()), static_cast<int8_t>(2));
140 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 1, false>()), static_cast<int8_t>(1));
141 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(2));
142 EXPECT_EQ(inst.GetImmData(1), static_cast<int64_t>(1));
143 EXPECT_EQ(inst.GetImmCount(), 2);
144 }
145
146 {
147 // defineclasswithbuffer 2, id9, id12, 2, v3
148 const uint8_t bytecode[] = {0x35, 0x02, 0x09, 0x00, 0x0c, 0x00, 0x02, 0x00, 0x03};
149 BytecodeInstruction inst(bytecode);
150 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x35);
151 EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8);
152 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 0>()), static_cast<int8_t>(2));
153 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 1>()), static_cast<int16_t>(2));
154 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 0, false>()),
155 static_cast<uint8_t>(2));
156 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 1, false>()),
157 static_cast<uint16_t>(2));
158 EXPECT_EQ(inst.GetImmData(0), static_cast<int64_t>(2));
159 EXPECT_EQ(inst.GetImmData(1), static_cast<int64_t>(2));
160 EXPECT_EQ(inst.GetImmCount(), 2);
161 }
162 }
163
TEST(BytecodeInstruction,OutputOperator)164 TEST(BytecodeInstruction, OutputOperator)
165 {
166 {
167 const uint8_t bytecode[] = {0x4f, 0x17};
168 BytecodeInstruction inst(bytecode);
169 std::ostringstream oss;
170 oss << inst;
171 EXPECT_EQ(oss.str(), "jeqz 23");
172 }
173
174 {
175 const uint8_t bytecode[] = {0x4d, 0xea};
176 BytecodeInstruction inst(bytecode);
177 std::ostringstream oss;
178 oss << inst;
179 EXPECT_EQ(oss.str(), "jmp -22");
180 }
181
182 {
183 const uint8_t bytecode[] = {0x62, 0x1e, 0x00, 0x00, 0x00};
184 BytecodeInstruction inst(bytecode);
185 std::ostringstream oss;
186 oss << inst;
187 EXPECT_EQ(oss.str(), "ldai 30");
188 }
189
190 {
191 const uint8_t bytecode[] = {0x63, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e, 0x09, 0x40};
192 BytecodeInstruction inst(bytecode);
193 std::ostringstream oss;
194 oss << inst;
195 EXPECT_EQ(oss.str(), "fldai 3.14");
196 }
197
198 {
199 const uint8_t bytecode[] = {0x2d, 0x80, 0x00};
200 BytecodeInstruction inst(bytecode);
201 std::ostringstream oss;
202 oss << inst;
203 EXPECT_EQ(oss.str(), "callthis0 128, v0");
204 }
205 }
206
TEST(BytecodeInstruction,GetLiteralIndex)207 TEST(BytecodeInstruction, GetLiteralIndex)
208 {
209 {
210 // legal ins, and have 0 literal id
211 const uint8_t bytecode[] = {0x05};
212 BytecodeInstruction inst(bytecode);
213 EXPECT_EQ(static_cast<int>(inst.GetLiteralIndex()), -1);
214 }
215 {
216 // legal ins, and have 1 literal id in loc 0
217 const uint8_t bytecode[] = {0x06, 0x00, 0x00, 0x00};
218 BytecodeInstruction inst(bytecode);
219 EXPECT_EQ(static_cast<int>(inst.GetLiteralIndex()), 0);
220 }
221 {
222 // legal ins, and have 1 literal id in loc 1
223 const uint8_t bytecode[] = {0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
224 BytecodeInstruction inst(bytecode);
225 EXPECT_EQ(static_cast<int>(inst.GetLiteralIndex()), 1);
226 }
227 {
228 // illegal ins
229 const uint8_t bytecode[] = {0xff};
230 BytecodeInstruction inst(bytecode);
231 EXPECT_EQ(static_cast<int>(inst.GetLiteralIndex()), -1);
232 }
233 }
234
TEST(BytecodeInstruction,GetLastVReg)235 TEST(BytecodeInstruction, GetLastVReg)
236 {
237 {
238 // newobjrange 0xb, 0x3, v6
239 const uint8_t bytecode[] = {0x08, 0x0b, 0x03, 0x06};
240 BytecodeInstruction inst(bytecode);
241 EXPECT_EQ(inst.GetLastVReg().value(), 6);
242 }
243 }
244 } // namespace panda::test
245