• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "test/unittests/test-utils.h"
6 
7 #include "src/wasm/wasm-macro-gen.h"
8 
9 namespace v8 {
10 namespace internal {
11 namespace wasm {
12 
13 class WasmMacroGenTest : public TestWithZone {};
14 
15 #define EXPECT_SIZE(size, ...)     \
16   do {                             \
17     byte code[] = {__VA_ARGS__};   \
18     EXPECT_EQ(size, sizeof(code)); \
19   } while (false)
20 
21 
TEST_F(WasmMacroGenTest,Constants)22 TEST_F(WasmMacroGenTest, Constants) {
23   EXPECT_SIZE(2, WASM_ONE);
24   EXPECT_SIZE(2, WASM_ZERO);
25 
26   EXPECT_SIZE(2, WASM_I8(122));
27   EXPECT_SIZE(2, WASM_I8(254));
28 
29   EXPECT_SIZE(5, WASM_I32(1));
30   EXPECT_SIZE(5, WASM_I32(10000));
31   EXPECT_SIZE(5, WASM_I32(-9828934));
32 
33   EXPECT_SIZE(9, WASM_I64(1));
34   EXPECT_SIZE(9, WASM_I64(10000));
35   EXPECT_SIZE(9, WASM_I64(-9828934));
36   EXPECT_SIZE(9, WASM_I64(0x123456789abcdef0ULL));
37 
38   EXPECT_SIZE(5, WASM_F32(1.0f));
39   EXPECT_SIZE(5, WASM_F32(10000.0f));
40   EXPECT_SIZE(5, WASM_F32(-9828934.0f));
41 
42   EXPECT_SIZE(9, WASM_F64(1.5));
43   EXPECT_SIZE(9, WASM_F64(10200.0));
44   EXPECT_SIZE(9, WASM_F64(-9818934.0));
45 }
46 
47 
TEST_F(WasmMacroGenTest,Statements)48 TEST_F(WasmMacroGenTest, Statements) {
49   EXPECT_SIZE(1, WASM_NOP);
50 
51   EXPECT_SIZE(4, WASM_SET_LOCAL(0, WASM_ZERO));
52 
53   EXPECT_SIZE(4, WASM_STORE_GLOBAL(0, WASM_ZERO));
54 
55   EXPECT_SIZE(6, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, WASM_ZERO));
56 
57   EXPECT_SIZE(4, WASM_IF(WASM_ZERO, WASM_NOP));
58 
59   EXPECT_SIZE(5, WASM_IF_ELSE(WASM_ZERO, WASM_NOP, WASM_NOP));
60 
61   EXPECT_SIZE(5, WASM_SELECT(WASM_ZERO, WASM_NOP, WASM_NOP));
62 
63   EXPECT_SIZE(3, WASM_BR(0));
64   EXPECT_SIZE(5, WASM_BR_IF(0, WASM_ZERO));
65 
66   EXPECT_SIZE(3, WASM_BLOCK(1, WASM_NOP));
67   EXPECT_SIZE(4, WASM_BLOCK(2, WASM_NOP, WASM_NOP));
68   EXPECT_SIZE(5, WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_NOP));
69 
70   EXPECT_SIZE(5, WASM_INFINITE_LOOP);
71 
72   EXPECT_SIZE(3, WASM_LOOP(1, WASM_NOP));
73   EXPECT_SIZE(4, WASM_LOOP(2, WASM_NOP, WASM_NOP));
74   EXPECT_SIZE(5, WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP));
75   EXPECT_SIZE(5, WASM_LOOP(1, WASM_BR(0)));
76   EXPECT_SIZE(7, WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO)));
77 
78   EXPECT_SIZE(1, WASM_RETURN0);
79   EXPECT_SIZE(3, WASM_RETURN(WASM_ZERO));
80   EXPECT_SIZE(5, WASM_RETURN(WASM_ZERO, WASM_ZERO));
81 
82   EXPECT_SIZE(1, WASM_UNREACHABLE);
83 }
84 
85 
TEST_F(WasmMacroGenTest,MacroStatements)86 TEST_F(WasmMacroGenTest, MacroStatements) {
87   EXPECT_SIZE(8, WASM_WHILE(WASM_I8(0), WASM_NOP));
88   EXPECT_SIZE(7, WASM_INC_LOCAL(0));
89   EXPECT_SIZE(7, WASM_INC_LOCAL_BY(0, 3));
90 
91   EXPECT_SIZE(3, WASM_BREAK(0));
92   EXPECT_SIZE(3, WASM_CONTINUE(0));
93 }
94 
95 
TEST_F(WasmMacroGenTest,TableSwitch)96 TEST_F(WasmMacroGenTest, TableSwitch) {
97   EXPECT_SIZE(2, WASM_CASE(9));
98   EXPECT_SIZE(2, WASM_CASE_BR(11));
99 
100   EXPECT_SIZE(7, WASM_TABLESWITCH_OP(0, 1, WASM_CASE(7)));
101   EXPECT_SIZE(9, WASM_TABLESWITCH_OP(0, 2, WASM_CASE(7), WASM_CASE(8)));
102 
103   EXPECT_SIZE(4, WASM_TABLESWITCH_BODY(WASM_I8(88), WASM_I8(77)));
104   EXPECT_SIZE(
105       6, WASM_TABLESWITCH_BODY(WASM_I8(33), WASM_I8(44), WASM_GET_LOCAL(0)));
106 }
107 
108 
TEST_F(WasmMacroGenTest,Expressions)109 TEST_F(WasmMacroGenTest, Expressions) {
110   EXPECT_SIZE(2, WASM_GET_LOCAL(0));
111   EXPECT_SIZE(2, WASM_GET_LOCAL(1));
112   EXPECT_SIZE(2, WASM_GET_LOCAL(12));
113   EXPECT_SIZE(2, WASM_LOAD_GLOBAL(0));
114   EXPECT_SIZE(2, WASM_LOAD_GLOBAL(1));
115   EXPECT_SIZE(2, WASM_LOAD_GLOBAL(12));
116   EXPECT_SIZE(4, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO));
117   EXPECT_SIZE(4, WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO));
118   EXPECT_SIZE(4, WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO));
119 
120   EXPECT_SIZE(3, WASM_NOT(WASM_ZERO));
121 
122   EXPECT_SIZE(4, WASM_BRV(1, WASM_ZERO));
123   EXPECT_SIZE(6, WASM_BRV_IF(1, WASM_ZERO, WASM_ZERO));
124 
125   EXPECT_SIZE(4, WASM_BLOCK(1, WASM_ZERO));
126   EXPECT_SIZE(5, WASM_BLOCK(2, WASM_NOP, WASM_ZERO));
127   EXPECT_SIZE(6, WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_ZERO));
128 
129   EXPECT_SIZE(4, WASM_LOOP(1, WASM_ZERO));
130   EXPECT_SIZE(5, WASM_LOOP(2, WASM_NOP, WASM_ZERO));
131   EXPECT_SIZE(6, WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_ZERO));
132 }
133 
134 
TEST_F(WasmMacroGenTest,FunctionCalls)135 TEST_F(WasmMacroGenTest, FunctionCalls) {
136   EXPECT_SIZE(2, WASM_CALL_FUNCTION0(0));
137   EXPECT_SIZE(2, WASM_CALL_FUNCTION0(1));
138   EXPECT_SIZE(2, WASM_CALL_FUNCTION0(11));
139 
140   EXPECT_SIZE(4, WASM_CALL_FUNCTION(0, WASM_ZERO));
141   EXPECT_SIZE(6, WASM_CALL_FUNCTION(1, WASM_ZERO, WASM_ZERO));
142 
143   EXPECT_SIZE(4, WASM_CALL_INDIRECT0(0, WASM_ZERO));
144   EXPECT_SIZE(4, WASM_CALL_INDIRECT0(1, WASM_ZERO));
145   EXPECT_SIZE(4, WASM_CALL_INDIRECT0(11, WASM_ZERO));
146 
147   EXPECT_SIZE(6, WASM_CALL_INDIRECT(0, WASM_ZERO, WASM_ZERO));
148   EXPECT_SIZE(8, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_ZERO, WASM_ZERO));
149 }
150 
151 
TEST_F(WasmMacroGenTest,Int32Ops)152 TEST_F(WasmMacroGenTest, Int32Ops) {
153   EXPECT_SIZE(5, WASM_I32_ADD(WASM_ZERO, WASM_ZERO));
154   EXPECT_SIZE(5, WASM_I32_SUB(WASM_ZERO, WASM_ZERO));
155   EXPECT_SIZE(5, WASM_I32_MUL(WASM_ZERO, WASM_ZERO));
156   EXPECT_SIZE(5, WASM_I32_DIVS(WASM_ZERO, WASM_ZERO));
157   EXPECT_SIZE(5, WASM_I32_DIVU(WASM_ZERO, WASM_ZERO));
158   EXPECT_SIZE(5, WASM_I32_REMS(WASM_ZERO, WASM_ZERO));
159   EXPECT_SIZE(5, WASM_I32_REMU(WASM_ZERO, WASM_ZERO));
160   EXPECT_SIZE(5, WASM_I32_AND(WASM_ZERO, WASM_ZERO));
161   EXPECT_SIZE(5, WASM_I32_IOR(WASM_ZERO, WASM_ZERO));
162   EXPECT_SIZE(5, WASM_I32_XOR(WASM_ZERO, WASM_ZERO));
163   EXPECT_SIZE(5, WASM_I32_SHL(WASM_ZERO, WASM_ZERO));
164   EXPECT_SIZE(5, WASM_I32_SHR(WASM_ZERO, WASM_ZERO));
165   EXPECT_SIZE(5, WASM_I32_SAR(WASM_ZERO, WASM_ZERO));
166   EXPECT_SIZE(5, WASM_I32_EQ(WASM_ZERO, WASM_ZERO));
167 
168   EXPECT_SIZE(5, WASM_I32_LTS(WASM_ZERO, WASM_ZERO));
169   EXPECT_SIZE(5, WASM_I32_LES(WASM_ZERO, WASM_ZERO));
170   EXPECT_SIZE(5, WASM_I32_LTU(WASM_ZERO, WASM_ZERO));
171   EXPECT_SIZE(5, WASM_I32_LEU(WASM_ZERO, WASM_ZERO));
172 
173   EXPECT_SIZE(5, WASM_I32_GTS(WASM_ZERO, WASM_ZERO));
174   EXPECT_SIZE(5, WASM_I32_GES(WASM_ZERO, WASM_ZERO));
175   EXPECT_SIZE(5, WASM_I32_GTU(WASM_ZERO, WASM_ZERO));
176   EXPECT_SIZE(5, WASM_I32_GEU(WASM_ZERO, WASM_ZERO));
177 
178   EXPECT_SIZE(3, WASM_I32_CLZ(WASM_ZERO));
179   EXPECT_SIZE(3, WASM_I32_CTZ(WASM_ZERO));
180   EXPECT_SIZE(3, WASM_I32_POPCNT(WASM_ZERO));
181 }
182 
183 
TEST_F(WasmMacroGenTest,Int64Ops)184 TEST_F(WasmMacroGenTest, Int64Ops) {
185   EXPECT_SIZE(5, WASM_I64_ADD(WASM_ZERO, WASM_ZERO));
186   EXPECT_SIZE(5, WASM_I64_SUB(WASM_ZERO, WASM_ZERO));
187   EXPECT_SIZE(5, WASM_I64_MUL(WASM_ZERO, WASM_ZERO));
188   EXPECT_SIZE(5, WASM_I64_DIVS(WASM_ZERO, WASM_ZERO));
189   EXPECT_SIZE(5, WASM_I64_DIVU(WASM_ZERO, WASM_ZERO));
190   EXPECT_SIZE(5, WASM_I64_REMS(WASM_ZERO, WASM_ZERO));
191   EXPECT_SIZE(5, WASM_I64_REMU(WASM_ZERO, WASM_ZERO));
192   EXPECT_SIZE(5, WASM_I64_AND(WASM_ZERO, WASM_ZERO));
193   EXPECT_SIZE(5, WASM_I64_IOR(WASM_ZERO, WASM_ZERO));
194   EXPECT_SIZE(5, WASM_I64_XOR(WASM_ZERO, WASM_ZERO));
195   EXPECT_SIZE(5, WASM_I64_SHL(WASM_ZERO, WASM_ZERO));
196   EXPECT_SIZE(5, WASM_I64_SHR(WASM_ZERO, WASM_ZERO));
197   EXPECT_SIZE(5, WASM_I64_SAR(WASM_ZERO, WASM_ZERO));
198   EXPECT_SIZE(5, WASM_I64_EQ(WASM_ZERO, WASM_ZERO));
199 
200   EXPECT_SIZE(5, WASM_I64_LTS(WASM_ZERO, WASM_ZERO));
201   EXPECT_SIZE(5, WASM_I64_LES(WASM_ZERO, WASM_ZERO));
202   EXPECT_SIZE(5, WASM_I64_LTU(WASM_ZERO, WASM_ZERO));
203   EXPECT_SIZE(5, WASM_I64_LEU(WASM_ZERO, WASM_ZERO));
204 
205   EXPECT_SIZE(5, WASM_I64_GTS(WASM_ZERO, WASM_ZERO));
206   EXPECT_SIZE(5, WASM_I64_GES(WASM_ZERO, WASM_ZERO));
207   EXPECT_SIZE(5, WASM_I64_GTU(WASM_ZERO, WASM_ZERO));
208   EXPECT_SIZE(5, WASM_I64_GEU(WASM_ZERO, WASM_ZERO));
209 
210   EXPECT_SIZE(3, WASM_I64_CLZ(WASM_ZERO));
211   EXPECT_SIZE(3, WASM_I64_CTZ(WASM_ZERO));
212   EXPECT_SIZE(3, WASM_I64_POPCNT(WASM_ZERO));
213 }
214 
215 
TEST_F(WasmMacroGenTest,Float32Ops)216 TEST_F(WasmMacroGenTest, Float32Ops) {
217   EXPECT_SIZE(5, WASM_F32_ADD(WASM_ZERO, WASM_ZERO));
218   EXPECT_SIZE(5, WASM_F32_SUB(WASM_ZERO, WASM_ZERO));
219   EXPECT_SIZE(5, WASM_F32_MUL(WASM_ZERO, WASM_ZERO));
220   EXPECT_SIZE(5, WASM_F32_DIV(WASM_ZERO, WASM_ZERO));
221   EXPECT_SIZE(5, WASM_F32_MIN(WASM_ZERO, WASM_ZERO));
222   EXPECT_SIZE(5, WASM_F32_MAX(WASM_ZERO, WASM_ZERO));
223   EXPECT_SIZE(5, WASM_F32_COPYSIGN(WASM_ZERO, WASM_ZERO));
224 
225   EXPECT_SIZE(3, WASM_F32_ABS(WASM_ZERO));
226   EXPECT_SIZE(3, WASM_F32_NEG(WASM_ZERO));
227   EXPECT_SIZE(3, WASM_F32_CEIL(WASM_ZERO));
228   EXPECT_SIZE(3, WASM_F32_FLOOR(WASM_ZERO));
229   EXPECT_SIZE(3, WASM_F32_TRUNC(WASM_ZERO));
230   EXPECT_SIZE(3, WASM_F32_NEARESTINT(WASM_ZERO));
231   EXPECT_SIZE(3, WASM_F32_SQRT(WASM_ZERO));
232 
233   EXPECT_SIZE(5, WASM_F32_EQ(WASM_ZERO, WASM_ZERO));
234   EXPECT_SIZE(5, WASM_F32_LT(WASM_ZERO, WASM_ZERO));
235   EXPECT_SIZE(5, WASM_F32_LE(WASM_ZERO, WASM_ZERO));
236   EXPECT_SIZE(5, WASM_F32_GT(WASM_ZERO, WASM_ZERO));
237   EXPECT_SIZE(5, WASM_F32_GE(WASM_ZERO, WASM_ZERO));
238 }
239 
240 
TEST_F(WasmMacroGenTest,Float64Ops)241 TEST_F(WasmMacroGenTest, Float64Ops) {
242   EXPECT_SIZE(5, WASM_F64_ADD(WASM_ZERO, WASM_ZERO));
243   EXPECT_SIZE(5, WASM_F64_SUB(WASM_ZERO, WASM_ZERO));
244   EXPECT_SIZE(5, WASM_F64_MUL(WASM_ZERO, WASM_ZERO));
245   EXPECT_SIZE(5, WASM_F64_DIV(WASM_ZERO, WASM_ZERO));
246   EXPECT_SIZE(5, WASM_F64_MIN(WASM_ZERO, WASM_ZERO));
247   EXPECT_SIZE(5, WASM_F64_MAX(WASM_ZERO, WASM_ZERO));
248   EXPECT_SIZE(5, WASM_F64_COPYSIGN(WASM_ZERO, WASM_ZERO));
249 
250   EXPECT_SIZE(3, WASM_F64_ABS(WASM_ZERO));
251   EXPECT_SIZE(3, WASM_F64_NEG(WASM_ZERO));
252   EXPECT_SIZE(3, WASM_F64_CEIL(WASM_ZERO));
253   EXPECT_SIZE(3, WASM_F64_FLOOR(WASM_ZERO));
254   EXPECT_SIZE(3, WASM_F64_TRUNC(WASM_ZERO));
255   EXPECT_SIZE(3, WASM_F64_NEARESTINT(WASM_ZERO));
256   EXPECT_SIZE(3, WASM_F64_SQRT(WASM_ZERO));
257 
258   EXPECT_SIZE(5, WASM_F64_EQ(WASM_ZERO, WASM_ZERO));
259   EXPECT_SIZE(5, WASM_F64_LT(WASM_ZERO, WASM_ZERO));
260   EXPECT_SIZE(5, WASM_F64_LE(WASM_ZERO, WASM_ZERO));
261   EXPECT_SIZE(5, WASM_F64_GT(WASM_ZERO, WASM_ZERO));
262   EXPECT_SIZE(5, WASM_F64_GE(WASM_ZERO, WASM_ZERO));
263 }
264 
265 
TEST_F(WasmMacroGenTest,Conversions)266 TEST_F(WasmMacroGenTest, Conversions) {
267   EXPECT_SIZE(3, WASM_I32_SCONVERT_F32(WASM_ZERO));
268   EXPECT_SIZE(3, WASM_I32_SCONVERT_F64(WASM_ZERO));
269   EXPECT_SIZE(3, WASM_I32_UCONVERT_F32(WASM_ZERO));
270   EXPECT_SIZE(3, WASM_I32_UCONVERT_F64(WASM_ZERO));
271   EXPECT_SIZE(3, WASM_I32_CONVERT_I64(WASM_ZERO));
272   EXPECT_SIZE(3, WASM_I64_SCONVERT_F32(WASM_ZERO));
273   EXPECT_SIZE(3, WASM_I64_SCONVERT_F64(WASM_ZERO));
274   EXPECT_SIZE(3, WASM_I64_UCONVERT_F32(WASM_ZERO));
275   EXPECT_SIZE(3, WASM_I64_UCONVERT_F64(WASM_ZERO));
276   EXPECT_SIZE(3, WASM_I64_SCONVERT_I32(WASM_ZERO));
277   EXPECT_SIZE(3, WASM_I64_UCONVERT_I32(WASM_ZERO));
278   EXPECT_SIZE(3, WASM_F32_SCONVERT_I32(WASM_ZERO));
279   EXPECT_SIZE(3, WASM_F32_UCONVERT_I32(WASM_ZERO));
280   EXPECT_SIZE(3, WASM_F32_SCONVERT_I64(WASM_ZERO));
281   EXPECT_SIZE(3, WASM_F32_UCONVERT_I64(WASM_ZERO));
282   EXPECT_SIZE(3, WASM_F32_CONVERT_F64(WASM_ZERO));
283   EXPECT_SIZE(3, WASM_F32_REINTERPRET_I32(WASM_ZERO));
284   EXPECT_SIZE(3, WASM_F64_SCONVERT_I32(WASM_ZERO));
285   EXPECT_SIZE(3, WASM_F64_UCONVERT_I32(WASM_ZERO));
286   EXPECT_SIZE(3, WASM_F64_SCONVERT_I64(WASM_ZERO));
287   EXPECT_SIZE(3, WASM_F64_UCONVERT_I64(WASM_ZERO));
288   EXPECT_SIZE(3, WASM_F64_CONVERT_F32(WASM_ZERO));
289   EXPECT_SIZE(3, WASM_F64_REINTERPRET_I64(WASM_ZERO));
290 }
291 
292 static const MachineType kMemTypes[] = {
293     MachineType::Int8(),   MachineType::Uint8(),  MachineType::Int16(),
294     MachineType::Uint16(), MachineType::Int32(),  MachineType::Uint32(),
295     MachineType::Int64(),  MachineType::Uint64(), MachineType::Float32(),
296     MachineType::Float64()};
297 
TEST_F(WasmMacroGenTest,LoadsAndStores)298 TEST_F(WasmMacroGenTest, LoadsAndStores) {
299   for (size_t i = 0; i < arraysize(kMemTypes); i++) {
300     EXPECT_SIZE(4, WASM_LOAD_MEM(kMemTypes[i], WASM_ZERO));
301   }
302   for (size_t i = 0; i < arraysize(kMemTypes); i++) {
303     EXPECT_SIZE(6, WASM_STORE_MEM(kMemTypes[i], WASM_ZERO, WASM_GET_LOCAL(0)));
304   }
305 }
306 
307 
TEST_F(WasmMacroGenTest,LoadsAndStoresWithOffset)308 TEST_F(WasmMacroGenTest, LoadsAndStoresWithOffset) {
309   for (size_t i = 0; i < arraysize(kMemTypes); i++) {
310     EXPECT_SIZE(5, WASM_LOAD_MEM_OFFSET(kMemTypes[i], 11, WASM_ZERO));
311   }
312   for (size_t i = 0; i < arraysize(kMemTypes); i++) {
313     EXPECT_SIZE(7, WASM_STORE_MEM_OFFSET(kMemTypes[i], 13, WASM_ZERO,
314                                          WASM_GET_LOCAL(0)));
315   }
316 }
317 }  // namespace wasm
318 }  // namespace internal
319 }  // namespace v8
320