OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "src/wasm/encoder.h" | 8 #include "src/wasm/encoder.h" |
9 #include "src/wasm/wasm-js.h" | 9 #include "src/wasm/wasm-js.h" |
10 #include "src/wasm/wasm-macro-gen.h" | 10 #include "src/wasm/wasm-macro-gen.h" |
11 #include "src/wasm/wasm-module.h" | 11 #include "src/wasm/wasm-module.h" |
12 #include "src/wasm/wasm-opcodes.h" | 12 #include "src/wasm/wasm-opcodes.h" |
13 | 13 |
14 #include "test/cctest/cctest.h" | 14 #include "test/cctest/cctest.h" |
15 #include "test/cctest/wasm/test-signatures.h" | 15 #include "test/cctest/wasm/test-signatures.h" |
16 | 16 |
17 using namespace v8::base; | 17 using namespace v8::base; |
18 using namespace v8::internal; | 18 using namespace v8::internal; |
19 using namespace v8::internal::compiler; | 19 using namespace v8::internal::compiler; |
20 using namespace v8::internal::wasm; | 20 using namespace v8::internal::wasm; |
21 | 21 |
22 namespace { | 22 namespace { |
23 void TestModule(WasmModuleIndex* module, int32_t expected_result) { | 23 void TestModule(Zone* zone, WasmModuleBuilder* builder, |
| 24 int32_t expected_result) { |
| 25 ZoneBuffer buffer(zone); |
| 26 WasmModuleWriter* writer = builder->Build(zone); |
| 27 writer->WriteTo(buffer); |
| 28 |
24 Isolate* isolate = CcTest::InitIsolateOnce(); | 29 Isolate* isolate = CcTest::InitIsolateOnce(); |
25 HandleScope scope(isolate); | 30 HandleScope scope(isolate); |
26 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 31 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
27 int32_t result = | 32 int32_t result = |
28 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); | 33 CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end()); |
29 CHECK_EQ(expected_result, result); | 34 CHECK_EQ(expected_result, result); |
30 } | 35 } |
31 } // namespace | 36 } // namespace |
32 | 37 |
33 TEST(Run_WasmModule_Return114) { | 38 TEST(Run_WasmModule_Return114) { |
34 static const int32_t kReturnValue = 114; | 39 static const int32_t kReturnValue = 114; |
35 TestSignatures sigs; | 40 TestSignatures sigs; |
36 v8::base::AccountingAllocator allocator; | 41 v8::base::AccountingAllocator allocator; |
37 Zone zone(&allocator); | 42 Zone zone(&allocator); |
38 | 43 |
39 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 44 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
40 uint16_t f_index = builder->AddFunction(); | 45 uint16_t f_index = builder->AddFunction(); |
41 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 46 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
42 f->SetSignature(sigs.i_v()); | 47 f->SetSignature(sigs.i_v()); |
43 f->Exported(1); | 48 f->Exported(1); |
44 byte code[] = {WASM_I8(kReturnValue)}; | 49 byte code[] = {WASM_I8(kReturnValue)}; |
45 f->EmitCode(code, sizeof(code)); | 50 f->EmitCode(code, sizeof(code)); |
46 WasmModuleWriter* writer = builder->Build(&zone); | 51 TestModule(&zone, builder, kReturnValue); |
47 TestModule(writer->WriteTo(&zone), kReturnValue); | |
48 } | 52 } |
49 | 53 |
50 TEST(Run_WasmModule_CallAdd) { | 54 TEST(Run_WasmModule_CallAdd) { |
51 v8::base::AccountingAllocator allocator; | 55 v8::base::AccountingAllocator allocator; |
52 Zone zone(&allocator); | 56 Zone zone(&allocator); |
53 TestSignatures sigs; | 57 TestSignatures sigs; |
54 | 58 |
55 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 59 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
56 | 60 |
57 uint16_t f1_index = builder->AddFunction(); | 61 uint16_t f1_index = builder->AddFunction(); |
58 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 62 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
59 f->SetSignature(sigs.i_ii()); | 63 f->SetSignature(sigs.i_ii()); |
60 uint16_t param1 = 0; | 64 uint16_t param1 = 0; |
61 uint16_t param2 = 1; | 65 uint16_t param2 = 1; |
62 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 66 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
63 f->EmitCode(code1, sizeof(code1)); | 67 f->EmitCode(code1, sizeof(code1)); |
64 | 68 |
65 uint16_t f2_index = builder->AddFunction(); | 69 uint16_t f2_index = builder->AddFunction(); |
66 f = builder->FunctionAt(f2_index); | 70 f = builder->FunctionAt(f2_index); |
67 f->SetSignature(sigs.i_v()); | 71 f->SetSignature(sigs.i_v()); |
68 | 72 |
69 f->Exported(1); | 73 f->Exported(1); |
70 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; | 74 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; |
71 f->EmitCode(code2, sizeof(code2)); | 75 f->EmitCode(code2, sizeof(code2)); |
72 WasmModuleWriter* writer = builder->Build(&zone); | 76 TestModule(&zone, builder, 99); |
73 TestModule(writer->WriteTo(&zone), 99); | |
74 } | 77 } |
75 | 78 |
76 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 79 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
77 static const byte kDataSegmentDest0 = 12; | 80 static const byte kDataSegmentDest0 = 12; |
78 v8::base::AccountingAllocator allocator; | 81 v8::base::AccountingAllocator allocator; |
79 Zone zone(&allocator); | 82 Zone zone(&allocator); |
80 TestSignatures sigs; | 83 TestSignatures sigs; |
81 | 84 |
82 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 85 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
83 uint16_t f_index = builder->AddFunction(); | 86 uint16_t f_index = builder->AddFunction(); |
84 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 87 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
85 f->SetSignature(sigs.i_v()); | 88 f->SetSignature(sigs.i_v()); |
86 | 89 |
87 f->Exported(1); | 90 f->Exported(1); |
88 byte code[] = { | 91 byte code[] = { |
89 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 92 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
90 f->EmitCode(code, sizeof(code)); | 93 f->EmitCode(code, sizeof(code)); |
91 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 94 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
92 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 95 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
93 &zone, data, sizeof(data), kDataSegmentDest0)); | 96 &zone, data, sizeof(data), kDataSegmentDest0)); |
94 WasmModuleWriter* writer = builder->Build(&zone); | 97 TestModule(&zone, builder, 0xddccbbaa); |
95 TestModule(writer->WriteTo(&zone), 0xddccbbaa); | |
96 } | 98 } |
97 | 99 |
98 TEST(Run_WasmModule_CheckMemoryIsZero) { | 100 TEST(Run_WasmModule_CheckMemoryIsZero) { |
99 static const int kCheckSize = 16 * 1024; | 101 static const int kCheckSize = 16 * 1024; |
100 v8::base::AccountingAllocator allocator; | 102 v8::base::AccountingAllocator allocator; |
101 Zone zone(&allocator); | 103 Zone zone(&allocator); |
102 TestSignatures sigs; | 104 TestSignatures sigs; |
103 | 105 |
104 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 106 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
105 uint16_t f_index = builder->AddFunction(); | 107 uint16_t f_index = builder->AddFunction(); |
106 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 108 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
107 f->SetSignature(sigs.i_v()); | 109 f->SetSignature(sigs.i_v()); |
108 | 110 |
109 uint16_t localIndex = f->AddLocal(kAstI32); | 111 uint16_t localIndex = f->AddLocal(kAstI32); |
110 f->Exported(1); | 112 f->Exported(1); |
111 byte code[] = {WASM_BLOCK( | 113 byte code[] = {WASM_BLOCK( |
112 2, | 114 2, |
113 WASM_WHILE( | 115 WASM_WHILE( |
114 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 116 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
115 WASM_IF_ELSE( | 117 WASM_IF_ELSE( |
116 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 118 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
117 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 119 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
118 WASM_I8(11))}; | 120 WASM_I8(11))}; |
119 f->EmitCode(code, sizeof(code)); | 121 f->EmitCode(code, sizeof(code)); |
120 WasmModuleWriter* writer = builder->Build(&zone); | 122 TestModule(&zone, builder, 11); |
121 TestModule(writer->WriteTo(&zone), 11); | |
122 } | 123 } |
123 | 124 |
124 TEST(Run_WasmModule_CallMain_recursive) { | 125 TEST(Run_WasmModule_CallMain_recursive) { |
125 v8::base::AccountingAllocator allocator; | 126 v8::base::AccountingAllocator allocator; |
126 Zone zone(&allocator); | 127 Zone zone(&allocator); |
127 TestSignatures sigs; | 128 TestSignatures sigs; |
128 | 129 |
129 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 130 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
130 uint16_t f_index = builder->AddFunction(); | 131 uint16_t f_index = builder->AddFunction(); |
131 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 132 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
132 f->SetSignature(sigs.i_v()); | 133 f->SetSignature(sigs.i_v()); |
133 | 134 |
134 uint16_t localIndex = f->AddLocal(kAstI32); | 135 uint16_t localIndex = f->AddLocal(kAstI32); |
135 f->Exported(1); | 136 f->Exported(1); |
136 byte code[] = {WASM_BLOCK( | 137 byte code[] = {WASM_BLOCK( |
137 2, WASM_SET_LOCAL(localIndex, | 138 2, WASM_SET_LOCAL(localIndex, |
138 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 139 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
139 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 140 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
140 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 141 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
141 WASM_INC_LOCAL(localIndex)), | 142 WASM_INC_LOCAL(localIndex)), |
142 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 143 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
143 WASM_BRV(0, WASM_I8(55))))}; | 144 WASM_BRV(0, WASM_I8(55))))}; |
144 f->EmitCode(code, sizeof(code)); | 145 f->EmitCode(code, sizeof(code)); |
145 WasmModuleWriter* writer = builder->Build(&zone); | 146 TestModule(&zone, builder, 55); |
146 TestModule(writer->WriteTo(&zone), 55); | |
147 } | 147 } |
148 | 148 |
149 TEST(Run_WasmModule_Global) { | 149 TEST(Run_WasmModule_Global) { |
150 v8::base::AccountingAllocator allocator; | 150 v8::base::AccountingAllocator allocator; |
151 Zone zone(&allocator); | 151 Zone zone(&allocator); |
152 TestSignatures sigs; | 152 TestSignatures sigs; |
153 | 153 |
154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
155 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 155 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
156 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 156 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
157 uint16_t f1_index = builder->AddFunction(); | 157 uint16_t f1_index = builder->AddFunction(); |
158 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 158 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
159 f->SetSignature(sigs.i_v()); | 159 f->SetSignature(sigs.i_v()); |
160 byte code1[] = { | 160 byte code1[] = { |
161 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 161 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
162 f->EmitCode(code1, sizeof(code1)); | 162 f->EmitCode(code1, sizeof(code1)); |
163 uint16_t f2_index = builder->AddFunction(); | 163 uint16_t f2_index = builder->AddFunction(); |
164 f = builder->FunctionAt(f2_index); | 164 f = builder->FunctionAt(f2_index); |
165 f->SetSignature(sigs.i_v()); | 165 f->SetSignature(sigs.i_v()); |
166 f->Exported(1); | 166 f->Exported(1); |
167 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 167 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
168 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 168 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
169 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 169 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
170 f->EmitCode(code2, sizeof(code2)); | 170 f->EmitCode(code2, sizeof(code2)); |
171 WasmModuleWriter* writer = builder->Build(&zone); | 171 TestModule(&zone, builder, 97); |
172 TestModule(writer->WriteTo(&zone), 97); | |
173 } | 172 } |
OLD | NEW |