1 /**
2 * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17
18 #include <type_traits>
19
20 #include <cstddef>
21 #include <cstdint>
22
23 #include "bytecode_instruction-inl.h"
24
25 namespace panda::test {
26
TEST(BytecodeInstructionV4_IMM4,Parse)27 TEST(BytecodeInstructionV4_IMM4, Parse)
28 {
29 // V4_IMM4
30 {
31 const uint8_t bytecode[] = {0x00, 0xa1, 0xff};
32 BytecodeInstruction inst(bytecode);
33 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
34 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_IMM4, 0>()), 1);
35 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V4_IMM4, 0>()), -6);
36 }
37
38 {
39 const uint8_t bytecode[] = {0x00, 0x2f, 0xff};
40 BytecodeInstruction inst(bytecode);
41 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
42 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_IMM4, 0>()), 0xf);
43 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V4_IMM4, 0>()), 0x2);
44 }
45 }
46
TEST(BytecodeInstructionIMM8,Parse)47 TEST(BytecodeInstructionIMM8, Parse)
48 {
49 // IMM8
50 {
51 const uint8_t bytecode[] = {0x00, 0xf2, 0xff};
52 BytecodeInstruction inst(bytecode);
53 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
54 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), static_cast<int8_t>(0xf2));
55 }
56
57 {
58 const uint8_t bytecode[] = {0x00, 0x21, 0xff};
59 BytecodeInstruction inst(bytecode);
60 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
61 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), 0x21);
62 }
63
64 // V8_IMM8
65 {
66 const uint8_t bytecode[] = {0x00, 0x12, 0xf2, 0xff};
67 BytecodeInstruction inst(bytecode);
68 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
69 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM8, 0>()), 0x12);
70 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM8, 0>()), static_cast<int8_t>(0xf2));
71 }
72
73 {
74 const uint8_t bytecode[] = {0x00, 0xf2, 0x12, 0xff};
75 BytecodeInstruction inst(bytecode);
76 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
77 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM8, 0>()), 0xf2);
78 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM8, 0>()), 0x12);
79 }
80 }
81
TEST(BytecodeInstructionIMM16,Parse)82 TEST(BytecodeInstructionIMM16, Parse)
83 {
84 // IMM16
85 {
86 const uint8_t bytecode[] = {0x00, 0xf2, 0x12, 0xff};
87 BytecodeInstruction inst(bytecode);
88 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
89 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16, 0>()), 0x12f2);
90 }
91
92 {
93 const uint8_t bytecode[] = {0x00, 0x12, 0xf2, 0xff};
94 BytecodeInstruction inst(bytecode);
95 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
96 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16, 0>()), static_cast<int16_t>(0xf212));
97 }
98
99 // V8_IMM16
100 {
101 const uint8_t bytecode[] = {0x00, 0x10, 0xf2, 0x12, 0xff};
102 BytecodeInstruction inst(bytecode);
103 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
104 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM16, 0>()), 0x10);
105 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM16, 0>()), 0x12f2);
106 }
107
108 {
109 const uint8_t bytecode[] = {0x00, 0xff, 0x12, 0xf2, 0xff};
110 BytecodeInstruction inst(bytecode);
111 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
112 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM16, 0>()), 0xff);
113 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM16, 0>()), static_cast<int16_t>(0xf212));
114 }
115 }
116
TEST(BytecodeInstructionIMM32,Parse)117 TEST(BytecodeInstructionIMM32, Parse)
118 {
119 // IMM32
120 {
121 const uint8_t bytecode[] = {0x00, 0x34, 0xf2, 0x12, 0x10, 0xff};
122 BytecodeInstruction inst(bytecode);
123 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
124 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0>()), 0x1012f234);
125 }
126
127 {
128 const uint8_t bytecode[] = {0x00, 0x34, 0x12, 0xf2, 0xf1, 0xff};
129 BytecodeInstruction inst(bytecode);
130 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
131 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0>()), static_cast<int32_t>(0xf1f21234));
132 }
133
134 // V8_IMM32
135 {
136 const uint8_t bytecode[] = {0x00, 0x04, 0x34, 0xf2, 0x12, 0x10, 0xff};
137 BytecodeInstruction inst(bytecode);
138 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
139 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM32, 0>()), 0x04);
140 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM32, 0>()), 0x1012f234);
141 }
142
143 {
144 const uint8_t bytecode[] = {0x00, 0xaa, 0x34, 0x12, 0xf2, 0xf1, 0xff};
145 BytecodeInstruction inst(bytecode);
146 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
147 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM32, 0>()), 0xaa);
148 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM32, 0>()), static_cast<int32_t>(0xf1f21234));
149 }
150 }
151
TEST(BytecodeInstructionIMM64,Parse)152 TEST(BytecodeInstructionIMM64, Parse)
153 {
154 // IMM64
155 {
156 const uint8_t bytecode[] = {0x00, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0x4, 0xff};
157 BytecodeInstruction inst(bytecode);
158 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
159 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM64, 0>()), 0x041012f23456789a);
160 }
161
162 {
163 const uint8_t bytecode[] = {0x00, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xab, 0xff};
164 BytecodeInstruction inst(bytecode);
165 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
166 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM64, 0>()), static_cast<int64_t>(0xab1012f23456789a));
167 }
168
169 // V8_IMM64
170 {
171 const uint8_t bytecode[] = {0x00, 0x11, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0x4, 0xff};
172 BytecodeInstruction inst(bytecode);
173 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
174 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM64, 0>()), 0x11);
175 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM64, 0>()), 0x041012f23456789a);
176 }
177
178 {
179 const uint8_t bytecode[] = {0x00, 0xab, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xab, 0xff};
180 BytecodeInstruction inst(bytecode);
181 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
182 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_IMM64, 0>()), 0xab);
183 EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::V8_IMM64, 0>()), static_cast<int64_t>(0xab1012f23456789a));
184 }
185 }
186
TEST(BytecodeInstructionV4_V4,Parse)187 TEST(BytecodeInstructionV4_V4, Parse)
188 {
189 // V4_V4
190 {
191 const uint8_t bytecode[] = {0x00, 0xba, 0xff};
192 BytecodeInstruction inst(bytecode);
193 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
194 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4, 0>()), 0xa);
195 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4, 1>()), 0xb);
196 }
197 }
198
TEST(BytecodeInstructionV8,Parse)199 TEST(BytecodeInstructionV8, Parse)
200 {
201 // V8
202 {
203 const uint8_t bytecode[] = {0x00, 0xab, 0xff};
204 BytecodeInstruction inst(bytecode);
205 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
206 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8, 0>()), 0xab);
207 }
208
209 // V8_V8
210 {
211 const uint8_t bytecode[] = {0x00, 0xab, 0xcd, 0xff};
212 BytecodeInstruction inst(bytecode);
213 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
214 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_V8, 0>()), 0xab);
215 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_V8, 1>()), 0xcd);
216 }
217 }
218
TEST(BytecodeInstructionV16,Parse)219 TEST(BytecodeInstructionV16, Parse)
220 {
221 // V16_V16
222 {
223 const uint8_t bytecode[] = {0x00, 0xcd, 0xab, 0xf1, 0xee, 0xff};
224 BytecodeInstruction inst(bytecode);
225 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
226 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V16_V16, 0>()), 0xabcd);
227 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V16_V16, 1>()), 0xeef1);
228 }
229 }
230
TEST(BytecodeInstructionID32,Parse)231 TEST(BytecodeInstructionID32, Parse)
232 {
233 // ID32
234 {
235 const uint8_t bytecode[] = {0x00, 0xf1, 0xee, 0xcd, 0xab, 0xff};
236 BytecodeInstruction inst(bytecode);
237 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
238 EXPECT_EQ((inst.GetId<BytecodeInstruction::Format::ID32, 0>()), BytecodeId(0xabcdeef1));
239 }
240 }
241
TEST(BytecodeInstructionID16,Parse)242 TEST(BytecodeInstructionID16, Parse)
243 {
244 // V4_V4_ID16
245 {
246 const uint8_t bytecode[] = {0x00, 0x21, 0xf1, 0xee, 0xcd, 0xab, 0xff};
247 BytecodeInstruction inst(bytecode);
248 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
249 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_ID16, 0>()), 0x1);
250 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_ID16, 1>()), 0x2);
251 EXPECT_EQ((inst.GetId<BytecodeInstruction::Format::V4_V4_ID16, 0>()), BytecodeId(0xeef1));
252 }
253
254 // V8_ID16
255 {
256 const uint8_t bytecode[] = {0x00, 0x12, 0xf1, 0xee, 0xcd, 0xab, 0xff};
257 BytecodeInstruction inst(bytecode);
258 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
259 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V8_ID16, 0>()), 0x12);
260 EXPECT_EQ((inst.GetId<BytecodeInstruction::Format::V8_ID16, 0>()), BytecodeId(0xeef1));
261 }
262
263 // V4_V4_V4_V4_ID16
264 {
265 const uint8_t bytecode[] = {0x00, 0x21, 0x43, 0xf1, 0xee, 0xcd, 0xab, 0xff};
266 BytecodeInstruction inst(bytecode);
267 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
268 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_V4_V4_ID16, 0x0>()), 0x1);
269 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_V4_V4_ID16, 0x1>()), 0x2);
270 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_V4_V4_ID16, 0x2>()), 0x3);
271 EXPECT_EQ((inst.GetVReg<BytecodeInstruction::Format::V4_V4_V4_V4_ID16, 0x3>()), 0x4);
272 EXPECT_EQ((inst.GetId<BytecodeInstruction::Format::V4_V4_V4_V4_ID16, 0>()), BytecodeId(0xeef1));
273 }
274 }
275
TEST(BytecodeInstruction,JumpTo)276 TEST(BytecodeInstruction, JumpTo)
277 {
278 const uint8_t bytecode[] = {0x00, 0x11, 0x22, 0x33};
279 BytecodeInstruction inst(bytecode);
280 BytecodeInstruction next = inst.JumpTo(2);
281 EXPECT_EQ(static_cast<uint8_t>(next.GetOpcode()), bytecode[2]);
282 }
283
284 // Positive tests
TEST(BytecodeInstructionSafeIMM4_P,Parse)285 TEST(BytecodeInstructionSafeIMM4_P, Parse)
286 {
287 // V4_IMM4
288 {
289 const uint8_t bytecode[] = {0x00, 0xa1, 0xff};
290 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
291 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
292 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), 1);
293 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), -6);
294 }
295
296 {
297 const uint8_t bytecode[] = {0x00, 0x2f, 0xff};
298 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
299 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
300 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), 0xf);
301 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), 0x2);
302 }
303 }
304
TEST(BytecodeInstructionSafeIMM8_P,Parse)305 TEST(BytecodeInstructionSafeIMM8_P, Parse)
306 {
307 // IMM8
308 {
309 const uint8_t bytecode[] = {0x00, 0xf2, 0xff};
310 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
311 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
312 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM8, 0>()), static_cast<int8_t>(0xf2));
313 }
314
315 {
316 const uint8_t bytecode[] = {0x00, 0x21, 0xff};
317 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
318 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
319 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM8, 0>()), 0x21);
320 }
321
322 // V8_IMM8
323 {
324 const uint8_t bytecode[] = {0x00, 0x12, 0xf2, 0xff};
325 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
326 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
327 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), 0x12);
328 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), static_cast<int8_t>(0xf2));
329 }
330
331 {
332 const uint8_t bytecode[] = {0x00, 0xf2, 0x12, 0xff};
333 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
334 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
335 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), 0xf2);
336 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), 0x12);
337 }
338 }
339
TEST(BytecodeInstructionSafeIMM16_P,Parse)340 TEST(BytecodeInstructionSafeIMM16_P, Parse)
341 {
342 // IMM16
343 {
344 const uint8_t bytecode[] = {0x00, 0xf2, 0x12, 0xff};
345 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
346 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
347 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM16, 0>()), 0x12f2);
348 }
349
350 {
351 const uint8_t bytecode[] = {0x00, 0x12, 0xf2, 0xff};
352 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
353 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
354 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM16, 0>()), static_cast<int16_t>(0xf212));
355 }
356
357 // V8_IMM16
358 {
359 const uint8_t bytecode[] = {0x00, 0x10, 0xf2, 0x12, 0xff};
360 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
361 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
362 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), 0x10);
363 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), 0x12f2);
364 }
365
366 {
367 const uint8_t bytecode[] = {0x00, 0xff, 0x12, 0xf2, 0xff};
368 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
369 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
370 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), 0xff);
371 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), static_cast<int16_t>(0xf212));
372 }
373 }
374
TEST(BytecodeInstructionSafeIMM32_P,Parse)375 TEST(BytecodeInstructionSafeIMM32_P, Parse)
376 {
377 // IMM32
378 {
379 const uint8_t bytecode[] = {0x00, 0x34, 0xf2, 0x12, 0x10, 0xff};
380 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
381 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
382 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM32, 0>()), 0x1012f234);
383 }
384
385 {
386 const uint8_t bytecode[] = {0x00, 0x34, 0x12, 0xf2, 0xf1, 0xff};
387 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
388 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
389 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM32, 0>()), static_cast<int32_t>(0xf1f21234));
390 }
391
392 // V8_IMM32
393 {
394 const uint8_t bytecode[] = {0x00, 0x04, 0x34, 0xf2, 0x12, 0x10, 0xff};
395 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
396 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
397 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), 0x04);
398 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), 0x1012f234);
399 }
400
401 {
402 const uint8_t bytecode[] = {0x00, 0xaa, 0x34, 0x12, 0xf2, 0xf1, 0xff};
403 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
404 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
405 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), 0xaa);
406 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), static_cast<int32_t>(0xf1f21234));
407 }
408 }
409
TEST(BytecodeInstructionSafeIMM64_P,Parse)410 TEST(BytecodeInstructionSafeIMM64_P, Parse)
411 {
412 // IMM64
413 {
414 const uint8_t bytecode[] = {0x00, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0x4, 0xff};
415 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
416 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
417 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM64, 0>()), 0x041012f23456789a);
418 }
419
420 {
421 const uint8_t bytecode[] = {0x00, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xab, 0xff};
422 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
423 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
424 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM64, 0>()), static_cast<int64_t>(0xab1012f23456789a));
425 }
426
427 // V8_IMM64
428 {
429 const uint8_t bytecode[] = {0x00, 0x11, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0x4, 0xff};
430 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
431 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
432 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM64, 0>()), 0x11);
433 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM64, 0>()), 0x041012f23456789a);
434 }
435
436 {
437 const uint8_t bytecode[] = {0x00, 0xab, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xab, 0xff};
438 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
439 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
440 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM64, 0>()), 0xab);
441 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM64, 0>()),
442 static_cast<int64_t>(0xab1012f23456789a));
443 }
444 }
445
TEST(BytecodeInstructionSafeV4_P,Parse)446 TEST(BytecodeInstructionSafeV4_P, Parse)
447 {
448 // V4_V4
449 {
450 const uint8_t bytecode[] = {0x00, 0xba, 0xff};
451 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
452 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
453 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4, 0>()), 0xa);
454 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4, 1>()), 0xb);
455 }
456 }
457
TEST(BytecodeInstructionSafeV8_P,Parse)458 TEST(BytecodeInstructionSafeV8_P, Parse)
459 {
460 // V8
461 {
462 const uint8_t bytecode[] = {0x00, 0xab, 0xff};
463 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
464 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
465 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8, 0>()), 0xab);
466 }
467
468 // V8_V8
469 {
470 const uint8_t bytecode[] = {0x00, 0xab, 0xcd, 0xff};
471 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
472 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
473 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_V8, 0>()), 0xab);
474 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_V8, 1>()), 0xcd);
475 }
476 }
477
TEST(BytecodeInstructionSafeV16_P,Parse)478 TEST(BytecodeInstructionSafeV16_P, Parse)
479 {
480 // V16_V16
481 {
482 const uint8_t bytecode[] = {0x00, 0xcd, 0xab, 0xf1, 0xee, 0xff};
483 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
484 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
485 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V16_V16, 0>()), 0xabcd);
486 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V16_V16, 1>()), 0xeef1);
487 }
488 }
489
TEST(BytecodeInstructionSafeID16_P,Parse)490 TEST(BytecodeInstructionSafeID16_P, Parse)
491 {
492 // ID32
493 {
494 const uint8_t bytecode[] = {0x00, 0xf1, 0xee, 0xcd, 0xab, 0xff};
495 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
496 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
497 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::ID32, 0>()), BytecodeId(0xabcdeef1));
498 }
499
500 // V4_V4_ID16
501 {
502 const uint8_t bytecode[] = {0x00, 0x21, 0xf1, 0xee, 0xcd, 0xab, 0xff};
503 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
504 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
505 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_ID16, 0>()), 0x1);
506 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_ID16, 1>()), 0x2);
507 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V4_V4_ID16, 0>()), BytecodeId(0xeef1));
508 }
509
510 // V8_ID16
511 {
512 const uint8_t bytecode[] = {0x00, 0x12, 0xf1, 0xee, 0xcd, 0xab, 0xff};
513 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
514 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
515 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_ID16, 0>()), 0x12);
516 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V8_ID16, 0>()), BytecodeId(0xeef1));
517 }
518
519 // V4_V4_V4_V4_ID16
520 {
521 const uint8_t bytecode[] = {0x00, 0x21, 0x43, 0xf1, 0xee, 0xcd, 0xab, 0xff};
522 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
523 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
524 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x0>()), 0x1);
525 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x1>()), 0x2);
526 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x2>()), 0x3);
527 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x3>()), 0x4);
528 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0>()), BytecodeId(0xeef1));
529 }
530 }
531
532 // Negative tests
533
TEST(BytecodeInstructionSafeIMM4_N,Parse)534 TEST(BytecodeInstructionSafeIMM4_N, Parse)
535 {
536 // V4_IMM4
537 {
538 const uint8_t bytecode[] = {0x00};
539 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
540 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
541 EXPECT_TRUE(inst.IsValid());
542 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), 0);
543 EXPECT_FALSE(inst.IsValid());
544 }
545
546 {
547 const uint8_t bytecode[] = {0x00};
548 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
549 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
550 EXPECT_TRUE(inst.IsValid());
551 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V4_IMM4, 0>()), 0);
552 EXPECT_FALSE(inst.IsValid());
553 }
554 }
555
TEST(BytecodeInstructionSafeIMM8_N,Parse)556 TEST(BytecodeInstructionSafeIMM8_N, Parse)
557 {
558 // IMM8
559 {
560 const uint8_t bytecode[] = {0x00};
561 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
562 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
563 EXPECT_TRUE(inst.IsValid());
564 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM8, 0>()), static_cast<int8_t>(0));
565 EXPECT_FALSE(inst.IsValid());
566 }
567
568 {
569 const uint8_t bytecode[] = {0x00};
570 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
571 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
572 EXPECT_TRUE(inst.IsValid());
573 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM8, 0>()), 0);
574 EXPECT_FALSE(inst.IsValid());
575 }
576
577 // V8_IMM8
578 {
579 const uint8_t bytecode[] = {0x00, 0x12};
580 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
581 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
582 EXPECT_TRUE(inst.IsValid());
583 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), 0x12);
584 EXPECT_TRUE(inst.IsValid());
585 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM8, 0>()), static_cast<int8_t>(0));
586 EXPECT_FALSE(inst.IsValid());
587 }
588 }
589
TEST(BytecodeInstructionSafeIMM16_N,Parse)590 TEST(BytecodeInstructionSafeIMM16_N, Parse)
591 {
592 // IMM16
593 {
594 const uint8_t bytecode[] = {0x00, 0xf2, 0xff};
595 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
596 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
597 EXPECT_TRUE(inst.IsValid());
598 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM16, 0>()), 0xf2);
599 EXPECT_FALSE(inst.IsValid());
600 }
601
602 // V8_IMM16
603 {
604 const uint8_t bytecode[] = {0x00, 0x10, 0xf2, 0xff};
605 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
606 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
607 EXPECT_TRUE(inst.IsValid());
608 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), 0x10);
609 EXPECT_TRUE(inst.IsValid());
610 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM16, 0>()), 0xf2);
611 EXPECT_FALSE(inst.IsValid());
612 }
613 }
614
TEST(BytecodeInstructionSafeIMM32_N,Parse)615 TEST(BytecodeInstructionSafeIMM32_N, Parse)
616 {
617 // IMM32
618 {
619 const uint8_t bytecode[] = {0x00, 0x34, 0xf2, 0x12, 0xff};
620 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
621 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
622 EXPECT_TRUE(inst.IsValid());
623 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM32, 0>()), 0x12f234);
624 EXPECT_FALSE(inst.IsValid());
625 }
626
627 // V8_IMM32
628 {
629 const uint8_t bytecode[] = {0x00, 0x04, 0x34, 0xf2, 0x12, 0xff};
630 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
631 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
632 EXPECT_TRUE(inst.IsValid());
633 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), 0x04);
634 EXPECT_TRUE(inst.IsValid());
635 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM32, 0>()), 0x12f234);
636 EXPECT_FALSE(inst.IsValid());
637 }
638 }
639
TEST(BytecodeInstructionSafeIMM64_N,Parse)640 TEST(BytecodeInstructionSafeIMM64_N, Parse)
641 {
642 // IMM64
643 {
644 const uint8_t bytecode[] = {0x00, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xff};
645 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 3]);
646 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
647 EXPECT_TRUE(inst.IsValid());
648 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::IMM64, 0>()), 0x12f23456789a);
649 EXPECT_FALSE(inst.IsValid());
650 }
651
652 // V8_IMM64
653 {
654 const uint8_t bytecode[] = {0x00, 0x11, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x12, 0x10, 0xff};
655 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 3]);
656 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
657 EXPECT_TRUE(inst.IsValid());
658 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_IMM64, 0>()), 0x11);
659 EXPECT_TRUE(inst.IsValid());
660 EXPECT_EQ((inst.GetImm<BytecodeInstructionSafe::Format::V8_IMM64, 0>()), 0x12f23456789a);
661 EXPECT_FALSE(inst.IsValid());
662 }
663 }
664
TEST(BytecodeInstructionSafeV4_N,Parse)665 TEST(BytecodeInstructionSafeV4_N, Parse)
666 {
667 // V4_V4
668 {
669 const uint8_t bytecode[] = {0x00};
670 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
671 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
672 EXPECT_TRUE(inst.IsValid());
673 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4, 0>()), 0);
674 EXPECT_FALSE(inst.IsValid());
675 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4, 1>()), 0);
676 EXPECT_FALSE(inst.IsValid());
677 }
678 }
679
TEST(BytecodeInstructionSafeV8_N,Parse)680 TEST(BytecodeInstructionSafeV8_N, Parse)
681 {
682 // V8
683 {
684 const uint8_t bytecode[] = {0x00};
685 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
686 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
687 EXPECT_TRUE(inst.IsValid());
688 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8, 0>()), 0);
689 EXPECT_FALSE(inst.IsValid());
690 }
691
692 // V8_V8
693 {
694 const uint8_t bytecode[] = {0x00, 0xab};
695 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
696 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
697 EXPECT_TRUE(inst.IsValid());
698 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_V8, 0>()), 0xab);
699 EXPECT_TRUE(inst.IsValid());
700 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_V8, 1>()), 0);
701 EXPECT_FALSE(inst.IsValid());
702 }
703 }
704
TEST(BytecodeInstructionSafeV16_N,Parse)705 TEST(BytecodeInstructionSafeV16_N, Parse)
706 {
707 // V16_V16
708 {
709 const uint8_t bytecode[] = {0x00, 0xcd, 0xab, 0xf1, 0xff};
710 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
711 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
712 EXPECT_TRUE(inst.IsValid());
713 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V16_V16, 0>()), 0xabcd);
714 EXPECT_TRUE(inst.IsValid());
715 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V16_V16, 1>()), 0xf1);
716 EXPECT_FALSE(inst.IsValid());
717 }
718 }
719
TEST(BytecodeInstructionSafeID32_N,Parse)720 TEST(BytecodeInstructionSafeID32_N, Parse)
721 {
722 // ID32
723 {
724 const uint8_t bytecode[] = {0x00, 0xf1, 0xee, 0xcd, 0xff};
725 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
726 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
727 EXPECT_TRUE(inst.IsValid());
728 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::ID32, 0>()), BytecodeId(0xcdeef1));
729 EXPECT_FALSE(inst.IsValid());
730 }
731 }
732
TEST(BytecodeInstructionSafeID16_N,Parse)733 TEST(BytecodeInstructionSafeID16_N, Parse)
734 {
735 // V4_V4_ID16
736 {
737 const uint8_t bytecode[] = {0x00, 0x21, 0xf1, 0xee};
738 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 2]);
739 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
740 EXPECT_TRUE(inst.IsValid());
741 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_ID16, 0>()), 0x1);
742 EXPECT_TRUE(inst.IsValid());
743 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_ID16, 1>()), 0x2);
744 EXPECT_TRUE(inst.IsValid());
745 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V4_V4_ID16, 0>()), BytecodeId(0xf1));
746 EXPECT_FALSE(inst.IsValid());
747 }
748
749 // V8_ID16
750 {
751 const uint8_t bytecode[] = {0x00, 0x12, 0xf1, 0xee};
752 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 3]);
753 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
754 EXPECT_TRUE(inst.IsValid());
755 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V8_ID16, 0>()), 0x12);
756 EXPECT_TRUE(inst.IsValid());
757 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V8_ID16, 0>()), BytecodeId(0x00));
758 EXPECT_FALSE(inst.IsValid());
759 }
760
761 // V4_V4_V4_V4_ID16
762 {
763 const uint8_t bytecode[] = {0x00, 0x21, 0x43, 0xf1, 0xee};
764 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 3]);
765 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x00);
766 EXPECT_TRUE(inst.IsValid());
767 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x0>()), 0x1);
768 EXPECT_TRUE(inst.IsValid());
769 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x1>()), 0x2);
770 EXPECT_TRUE(inst.IsValid());
771 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x2>()), 0x3);
772 EXPECT_TRUE(inst.IsValid());
773 EXPECT_EQ((inst.GetVReg<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0x3>()), 0x4);
774 EXPECT_TRUE(inst.IsValid());
775 EXPECT_EQ((inst.GetId<BytecodeInstructionSafe::Format::V4_V4_V4_V4_ID16, 0>()), BytecodeId(0x0));
776 EXPECT_FALSE(inst.IsValid());
777 }
778 }
779
TEST(BytecodeInstructionSafe,JumpTo)780 TEST(BytecodeInstructionSafe, JumpTo)
781 {
782 // Positive
783 {
784 const uint8_t bytecode[] = {0x00, 0x11, 0x22, 0x33};
785 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
786 BytecodeInstructionSafe next = inst.JumpTo(2);
787 EXPECT_EQ(static_cast<uint8_t>(next.GetOpcode()), bytecode[2]);
788 }
789 // Negative
790 {
791 const uint8_t bytecode[] = {0x00, 0x11, 0x22, 0x33};
792 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
793 BytecodeInstructionSafe next = inst.JumpTo(4);
794 EXPECT_FALSE(inst.IsValid());
795 EXPECT_FALSE(next.IsValid());
796 }
797 {
798 const uint8_t bytecode[] = {0x00, 0x11, 0x22, 0x33};
799 BytecodeInstructionSafe inst(bytecode, &bytecode[0], &bytecode[sizeof(bytecode) - 1]);
800 BytecodeInstructionSafe next = inst.JumpTo(-1);
801 EXPECT_FALSE(inst.IsValid());
802 EXPECT_FALSE(next.IsValid());
803 }
804 }
805
806 } // namespace panda::test
807