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" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 HandleScope scope(isolate); | 62 HandleScope scope(isolate); |
63 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 63 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
64 int32_t result = | 64 int32_t result = |
65 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); | 65 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); |
66 CHECK_EQ(99, result); | 66 CHECK_EQ(99, result); |
67 } | 67 } |
68 | 68 |
69 | 69 |
70 TEST(Run_WasmModule_Return114) { | 70 TEST(Run_WasmModule_Return114) { |
71 static const int32_t kReturnValue = 114; | 71 static const int32_t kReturnValue = 114; |
72 Zone zone; | 72 v8::base::AccountingAllocator allocator; |
| 73 Zone zone(&allocator); |
73 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 74 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
74 uint16_t f_index = builder->AddFunction(); | 75 uint16_t f_index = builder->AddFunction(); |
75 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 76 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
76 f->ReturnType(kAstI32); | 77 f->ReturnType(kAstI32); |
77 f->Exported(1); | 78 f->Exported(1); |
78 byte code[] = {WASM_I8(kReturnValue)}; | 79 byte code[] = {WASM_I8(kReturnValue)}; |
79 f->EmitCode(code, sizeof(code)); | 80 f->EmitCode(code, sizeof(code)); |
80 WasmModuleWriter* writer = builder->Build(&zone); | 81 WasmModuleWriter* writer = builder->Build(&zone); |
81 TestModule(writer->WriteTo(&zone), kReturnValue); | 82 TestModule(writer->WriteTo(&zone), kReturnValue); |
82 } | 83 } |
83 | 84 |
84 | 85 |
85 TEST(Run_WasmModule_CallAdd) { | 86 TEST(Run_WasmModule_CallAdd) { |
86 Zone zone; | 87 v8::base::AccountingAllocator allocator; |
| 88 Zone zone(&allocator); |
87 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 89 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
88 uint16_t f1_index = builder->AddFunction(); | 90 uint16_t f1_index = builder->AddFunction(); |
89 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 91 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
90 f->ReturnType(kAstI32); | 92 f->ReturnType(kAstI32); |
91 uint16_t param1 = f->AddParam(kAstI32); | 93 uint16_t param1 = f->AddParam(kAstI32); |
92 uint16_t param2 = f->AddParam(kAstI32); | 94 uint16_t param2 = f->AddParam(kAstI32); |
93 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 95 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
94 uint32_t local_indices1[] = {2, 4}; | 96 uint32_t local_indices1[] = {2, 4}; |
95 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); | 97 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); |
96 uint16_t f2_index = builder->AddFunction(); | 98 uint16_t f2_index = builder->AddFunction(); |
97 f = builder->FunctionAt(f2_index); | 99 f = builder->FunctionAt(f2_index); |
98 f->ReturnType(kAstI32); | 100 f->ReturnType(kAstI32); |
99 f->Exported(1); | 101 f->Exported(1); |
100 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; | 102 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; |
101 f->EmitCode(code2, sizeof(code2)); | 103 f->EmitCode(code2, sizeof(code2)); |
102 WasmModuleWriter* writer = builder->Build(&zone); | 104 WasmModuleWriter* writer = builder->Build(&zone); |
103 TestModule(writer->WriteTo(&zone), 99); | 105 TestModule(writer->WriteTo(&zone), 99); |
104 } | 106 } |
105 | 107 |
106 | 108 |
107 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 109 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
108 static const byte kDataSegmentDest0 = 12; | 110 static const byte kDataSegmentDest0 = 12; |
109 Zone zone; | 111 v8::base::AccountingAllocator allocator; |
| 112 Zone zone(&allocator); |
110 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 113 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
111 uint16_t f_index = builder->AddFunction(); | 114 uint16_t f_index = builder->AddFunction(); |
112 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 115 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
113 f->ReturnType(kAstI32); | 116 f->ReturnType(kAstI32); |
114 f->Exported(1); | 117 f->Exported(1); |
115 byte code[] = { | 118 byte code[] = { |
116 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 119 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
117 f->EmitCode(code, sizeof(code)); | 120 f->EmitCode(code, sizeof(code)); |
118 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 121 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
119 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 122 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
120 &zone, data, sizeof(data), kDataSegmentDest0)); | 123 &zone, data, sizeof(data), kDataSegmentDest0)); |
121 WasmModuleWriter* writer = builder->Build(&zone); | 124 WasmModuleWriter* writer = builder->Build(&zone); |
122 TestModule(writer->WriteTo(&zone), 0xddccbbaa); | 125 TestModule(writer->WriteTo(&zone), 0xddccbbaa); |
123 } | 126 } |
124 | 127 |
125 TEST(Run_WasmModule_CheckMemoryIsZero) { | 128 TEST(Run_WasmModule_CheckMemoryIsZero) { |
126 static const int kCheckSize = 16 * 1024; | 129 static const int kCheckSize = 16 * 1024; |
127 Zone zone; | 130 v8::base::AccountingAllocator allocator; |
| 131 Zone zone(&allocator); |
128 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 132 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
129 uint16_t f_index = builder->AddFunction(); | 133 uint16_t f_index = builder->AddFunction(); |
130 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 134 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
131 f->ReturnType(kAstI32); | 135 f->ReturnType(kAstI32); |
132 uint16_t localIndex = f->AddLocal(kAstI32); | 136 uint16_t localIndex = f->AddLocal(kAstI32); |
133 f->Exported(1); | 137 f->Exported(1); |
134 byte code[] = {WASM_BLOCK( | 138 byte code[] = {WASM_BLOCK( |
135 2, | 139 2, |
136 WASM_WHILE( | 140 WASM_WHILE( |
137 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 141 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
138 WASM_IF_ELSE( | 142 WASM_IF_ELSE( |
139 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 143 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
140 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 144 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
141 WASM_I8(11))}; | 145 WASM_I8(11))}; |
142 f->EmitCode(code, sizeof(code), nullptr, 0); | 146 f->EmitCode(code, sizeof(code), nullptr, 0); |
143 WasmModuleWriter* writer = builder->Build(&zone); | 147 WasmModuleWriter* writer = builder->Build(&zone); |
144 TestModule(writer->WriteTo(&zone), 11); | 148 TestModule(writer->WriteTo(&zone), 11); |
145 } | 149 } |
146 | 150 |
147 TEST(Run_WasmModule_CallMain_recursive) { | 151 TEST(Run_WasmModule_CallMain_recursive) { |
148 Zone zone; | 152 v8::base::AccountingAllocator allocator; |
| 153 Zone zone(&allocator); |
149 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
150 uint16_t f_index = builder->AddFunction(); | 155 uint16_t f_index = builder->AddFunction(); |
151 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 156 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
152 f->ReturnType(kAstI32); | 157 f->ReturnType(kAstI32); |
153 uint16_t localIndex = f->AddLocal(kAstI32); | 158 uint16_t localIndex = f->AddLocal(kAstI32); |
154 f->Exported(1); | 159 f->Exported(1); |
155 byte code[] = {WASM_BLOCK( | 160 byte code[] = {WASM_BLOCK( |
156 2, WASM_SET_LOCAL(localIndex, | 161 2, WASM_SET_LOCAL(localIndex, |
157 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 162 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
158 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 163 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
159 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 164 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
160 WASM_INC_LOCAL(localIndex)), | 165 WASM_INC_LOCAL(localIndex)), |
161 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 166 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
162 WASM_BRV(0, WASM_I8(55))))}; | 167 WASM_BRV(0, WASM_I8(55))))}; |
163 f->EmitCode(code, sizeof(code), nullptr, 0); | 168 f->EmitCode(code, sizeof(code), nullptr, 0); |
164 WasmModuleWriter* writer = builder->Build(&zone); | 169 WasmModuleWriter* writer = builder->Build(&zone); |
165 TestModule(writer->WriteTo(&zone), 55); | 170 TestModule(writer->WriteTo(&zone), 55); |
166 } | 171 } |
167 | 172 |
168 TEST(Run_WasmModule_Global) { | 173 TEST(Run_WasmModule_Global) { |
169 Zone zone; | 174 v8::base::AccountingAllocator allocator; |
| 175 Zone zone(&allocator); |
170 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 176 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
171 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 177 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
172 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 178 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
173 uint16_t f1_index = builder->AddFunction(); | 179 uint16_t f1_index = builder->AddFunction(); |
174 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 180 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
175 f->ReturnType(kAstI32); | 181 f->ReturnType(kAstI32); |
176 byte code1[] = { | 182 byte code1[] = { |
177 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 183 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
178 f->EmitCode(code1, sizeof(code1)); | 184 f->EmitCode(code1, sizeof(code1)); |
179 uint16_t f2_index = builder->AddFunction(); | 185 uint16_t f2_index = builder->AddFunction(); |
180 f = builder->FunctionAt(f2_index); | 186 f = builder->FunctionAt(f2_index); |
181 f->ReturnType(kAstI32); | 187 f->ReturnType(kAstI32); |
182 f->Exported(1); | 188 f->Exported(1); |
183 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 189 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
184 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 190 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
185 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; | 191 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; |
186 f->EmitCode(code2, sizeof(code2)); | 192 f->EmitCode(code2, sizeof(code2)); |
187 WasmModuleWriter* writer = builder->Build(&zone); | 193 WasmModuleWriter* writer = builder->Build(&zone); |
188 TestModule(writer->WriteTo(&zone), 97); | 194 TestModule(writer->WriteTo(&zone), 97); |
189 } | 195 } |
OLD | NEW |