• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
37 
38     {
39         // jmp -22
40         const uint8_t bytecode[] = {0x4d, 0xea};
41         BytecodeInstruction inst(bytecode);
42         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x4d);
43         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), static_cast<int8_t>(-22));
44         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0, true>()), static_cast<int8_t>(-22));
45     }
46 
47     {
48         // ldai 30
49         const uint8_t bytecode[] = {0x62, 0x1e, 0x00, 0x00, 0x00};
50         BytecodeInstruction inst(bytecode);
51         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x62);
52         EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM32);
53         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0>()), static_cast<int32_t>(0x1e));
54         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM32, 0, true>()), static_cast<int32_t>(0x1e));
55     }
56 
57     {
58         // fldai 3.14
59         const uint8_t bytecode[] = {0x63, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e, 0x09, 0x40};
60         BytecodeInstruction inst(bytecode);
61         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x63);
62         EXPECT_EQ((bit_cast<double>(inst.GetImm<BytecodeInstruction::Format::IMM64, 0, true>())), 3.14);
63     }
64 }
65 
TEST(BytecodeInstruction,UnsignedOneImm)66 TEST(BytecodeInstruction, UnsignedOneImm)
67 {
68     {
69         // callthis2 142, v0, v2, v3
70         const uint8_t bytecode[] = {0x2f, 0x8e, 0x00, 0x02, 0x03};
71         BytecodeInstruction inst(bytecode);
72         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x2f);
73         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0>()), static_cast<int8_t>(0x8e));
74         EXPECT_NE((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0>()), static_cast<uint8_t>(0x8e));
75         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_V8_V8_V8, 0, false>()),
76                    static_cast<uint8_t>(0x8e));
77     }
78 
79     {
80         // neg 13
81         const uint8_t bytecode[] = {0x1f, 0x0d};
82         BytecodeInstruction inst(bytecode);
83         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x1f);
84         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0>()), static_cast<int8_t>(0x0d));
85         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8, 0, false>()),
86                    static_cast<uint8_t>(0x0d));
87     }
88 
89     {
90         // tryldglobalbyname 128, id6
91         const uint8_t bytecode[] = {0x8c, 0x80, 0x00, 0x06, 0x00};
92         BytecodeInstruction inst(bytecode);
93         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x8c);
94         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16_ID16, 0>()), static_cast<int16_t>(0x80));
95         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM16_ID16, 0, false>()),
96                    static_cast<uint16_t>(0x80));
97     }
98 }
99 
TEST(BytecodeInstruction,UnsignedTwoImm)100 TEST(BytecodeInstruction, UnsignedTwoImm)
101 {
102     {
103         // stlexvar 0, 2
104         const uint8_t bytecode[] = {0x3d, 0x20};
105         BytecodeInstruction inst(bytecode);
106         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x3d);
107         EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM4_IMM4);
108         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 0>()), 0);
109         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 1>()), 2);
110         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 0, false>()), 0);
111         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM4_IMM4, 1, false>()), 2);
112     }
113 
114     {
115         // definefunc 2, id8, 1
116         const uint8_t bytecode[] = {0x33, 0x02, 0x08, 0x00, 0x01};
117         BytecodeInstruction inst(bytecode);
118         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x33);
119         EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM8_ID16_IMM8);
120         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 0>()), static_cast<int8_t>(2));
121         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 1>()), static_cast<int8_t>(1));
122         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 0, false>()), static_cast<int8_t>(2));
123         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_IMM8, 1, false>()), static_cast<int8_t>(1));
124     }
125 
126     {
127         // defineclasswithbuffer 2, id9, id12, 2, v3
128         const uint8_t bytecode[] = {0x35, 0x02, 0x09, 0x00, 0x0c, 0x00, 0x02, 0x00, 0x03};
129         BytecodeInstruction inst(bytecode);
130         EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x35);
131         EXPECT_EQ(inst.GetFormat(), BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8);
132         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 0>()), static_cast<int8_t>(2));
133         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 1>()), static_cast<int16_t>(2));
134         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 0, false>()),
135                    static_cast<uint8_t>(2));
136         EXPECT_EQ((inst.GetImm<BytecodeInstruction::Format::IMM8_ID16_ID16_IMM16_V8, 1, false>()),
137                    static_cast<uint16_t>(2));
138     }
139 }
140 
TEST(BytecodeInstruction,OutputOperator)141 TEST(BytecodeInstruction, OutputOperator)
142 {
143     {
144         const uint8_t bytecode[] = {0x4f, 0x17};
145         BytecodeInstruction inst(bytecode);
146         std::ostringstream oss;
147         oss << inst;
148         EXPECT_EQ(oss.str(), "jeqz 23");
149     }
150 
151     {
152         const uint8_t bytecode[] = {0x4d, 0xea};
153         BytecodeInstruction inst(bytecode);
154         std::ostringstream oss;
155         oss << inst;
156         EXPECT_EQ(oss.str(), "jmp -22");
157     }
158 
159     {
160         const uint8_t bytecode[] = {0x62, 0x1e, 0x00, 0x00, 0x00};
161         BytecodeInstruction inst(bytecode);
162         std::ostringstream oss;
163         oss << inst;
164         EXPECT_EQ(oss.str(), "ldai 30");
165     }
166 
167     {
168         const uint8_t bytecode[] = {0x63, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e, 0x09, 0x40};
169         BytecodeInstruction inst(bytecode);
170         std::ostringstream oss;
171         oss << inst;
172         EXPECT_EQ(oss.str(), "fldai 3.14");
173     }
174 
175     {
176         const uint8_t bytecode[] = {0x2d, 0x80, 0x00};
177         BytecodeInstruction inst(bytecode);
178         std::ostringstream oss;
179         oss << inst;
180         EXPECT_EQ(oss.str(), "callthis0 128, v0");
181     }
182 }
183 
184 }  // namespace panda::test
185