OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef WASM_RUN_UTILS_H | 5 #ifndef WASM_RUN_UTILS_H |
6 #define WASM_RUN_UTILS_H | 6 #define WASM_RUN_UTILS_H |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 class TestingModule : public ModuleEnv { | 73 class TestingModule : public ModuleEnv { |
74 public: | 74 public: |
75 TestingModule() | 75 TestingModule() |
76 : instance_(&module_), | 76 : instance_(&module_), |
77 isolate_(CcTest::InitIsolateOnce()), | 77 isolate_(CcTest::InitIsolateOnce()), |
78 global_offset(0) { | 78 global_offset(0) { |
79 module = &module_; | 79 module = &module_; |
80 instance = &instance_; | 80 instance = &instance_; |
81 instance->module = &module_; | 81 instance->module = &module_; |
82 instance->globals_start = global_data; | 82 instance->globals_start = global_data; |
83 instance->globals_size = kMaxGlobalsSize; | 83 module_.globals_size = kMaxGlobalsSize; |
84 instance->mem_start = nullptr; | 84 instance->mem_start = nullptr; |
85 instance->mem_size = 0; | 85 instance->mem_size = 0; |
86 linker = nullptr; | 86 linker = nullptr; |
87 origin = kWasmOrigin; | 87 origin = kWasmOrigin; |
88 memset(global_data, 0, sizeof(global_data)); | 88 memset(global_data, 0, sizeof(global_data)); |
89 } | 89 } |
90 | 90 |
91 ~TestingModule() { | 91 ~TestingModule() { |
92 if (instance->mem_start) { | 92 if (instance->mem_start) { |
93 free(instance->mem_start); | 93 free(instance->mem_start); |
(...skipping 11 matching lines...) Expand all Loading... |
105 } | 105 } |
106 | 106 |
107 template <typename T> | 107 template <typename T> |
108 T* AddMemoryElems(size_t count) { | 108 T* AddMemoryElems(size_t count) { |
109 AddMemory(count * sizeof(T)); | 109 AddMemory(count * sizeof(T)); |
110 return raw_mem_start<T>(); | 110 return raw_mem_start<T>(); |
111 } | 111 } |
112 | 112 |
113 template <typename T> | 113 template <typename T> |
114 T* AddGlobal(MachineType mem_type) { | 114 T* AddGlobal(MachineType mem_type) { |
115 WasmGlobal* global = AddGlobal(mem_type); | 115 const WasmGlobal* global = AddGlobal(mem_type); |
116 return reinterpret_cast<T*>(instance->globals_start + global->offset); | 116 return reinterpret_cast<T*>(instance->globals_start + global->offset); |
117 } | 117 } |
118 | 118 |
119 byte AddSignature(FunctionSig* sig) { | 119 byte AddSignature(FunctionSig* sig) { |
120 module->signatures.push_back(sig); | 120 module_.signatures.push_back(sig); |
121 size_t size = module->signatures.size(); | 121 size_t size = module->signatures.size(); |
122 CHECK(size < 127); | 122 CHECK(size < 127); |
123 return static_cast<byte>(size - 1); | 123 return static_cast<byte>(size - 1); |
124 } | 124 } |
125 | 125 |
126 template <typename T> | 126 template <typename T> |
127 T* raw_mem_start() { | 127 T* raw_mem_start() { |
128 DCHECK(instance->mem_start); | 128 DCHECK(instance->mem_start); |
129 return reinterpret_cast<T*>(instance->mem_start); | 129 return reinterpret_cast<T*>(instance->mem_start); |
130 } | 130 } |
(...skipping 29 matching lines...) Expand all Loading... |
160 byte* end = raw_mem_end<byte>(); | 160 byte* end = raw_mem_end<byte>(); |
161 v8::base::RandomNumberGenerator rng; | 161 v8::base::RandomNumberGenerator rng; |
162 rng.SetSeed(seed); | 162 rng.SetSeed(seed); |
163 rng.NextBytes(raw, end - raw); | 163 rng.NextBytes(raw, end - raw); |
164 } | 164 } |
165 | 165 |
166 uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { | 166 uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { |
167 if (module->functions.size() == 0) { | 167 if (module->functions.size() == 0) { |
168 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction | 168 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction |
169 // structs from moving. | 169 // structs from moving. |
170 module->functions.reserve(kMaxFunctions); | 170 module_.functions.reserve(kMaxFunctions); |
171 } | 171 } |
172 uint32_t index = static_cast<uint32_t>(module->functions.size()); | 172 uint32_t index = static_cast<uint32_t>(module->functions.size()); |
173 module->functions.push_back({sig, index, 0, 0, 0, 0, 0, false}); | 173 module_.functions.push_back({sig, index, 0, 0, 0, 0, 0, false}); |
174 instance->function_code.push_back(code); | 174 instance->function_code.push_back(code); |
175 DCHECK_LT(index, kMaxFunctions); // limited for testing. | 175 DCHECK_LT(index, kMaxFunctions); // limited for testing. |
176 return index; | 176 return index; |
177 } | 177 } |
178 | 178 |
179 uint32_t AddJsFunction(FunctionSig* sig, const char* source) { | 179 uint32_t AddJsFunction(FunctionSig* sig, const char* source) { |
180 Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( | 180 Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( |
181 *v8::Local<v8::Function>::Cast(CompileRun(source)))); | 181 *v8::Local<v8::Function>::Cast(CompileRun(source)))); |
182 uint32_t index = AddFunction(sig, Handle<Code>::null()); | 182 uint32_t index = AddFunction(sig, Handle<Code>::null()); |
183 WasmName module_name = ArrayVector("test"); | 183 WasmName module_name = ArrayVector("test"); |
(...skipping 17 matching lines...) Expand all Loading... |
201 void SetFunctionCode(uint32_t index, Handle<Code> code) { | 201 void SetFunctionCode(uint32_t index, Handle<Code> code) { |
202 instance->function_code[index] = code; | 202 instance->function_code[index] = code; |
203 } | 203 } |
204 | 204 |
205 void AddIndirectFunctionTable(int* functions, int table_size) { | 205 void AddIndirectFunctionTable(int* functions, int table_size) { |
206 Handle<FixedArray> fixed = | 206 Handle<FixedArray> fixed = |
207 isolate_->factory()->NewFixedArray(2 * table_size); | 207 isolate_->factory()->NewFixedArray(2 * table_size); |
208 instance->function_table = fixed; | 208 instance->function_table = fixed; |
209 DCHECK_EQ(0u, module->function_table.size()); | 209 DCHECK_EQ(0u, module->function_table.size()); |
210 for (int i = 0; i < table_size; i++) { | 210 for (int i = 0; i < table_size; i++) { |
211 module->function_table.push_back(functions[i]); | 211 module_.function_table.push_back(functions[i]); |
212 } | 212 } |
213 } | 213 } |
214 | 214 |
215 void PopulateIndirectFunctionTable() { | 215 void PopulateIndirectFunctionTable() { |
216 if (instance->function_table.is_null()) return; | 216 if (instance->function_table.is_null()) return; |
217 int table_size = static_cast<int>(module->function_table.size()); | 217 int table_size = static_cast<int>(module->function_table.size()); |
218 for (int i = 0; i < table_size; i++) { | 218 for (int i = 0; i < table_size; i++) { |
219 int function_index = module->function_table[i]; | 219 int function_index = module->function_table[i]; |
220 WasmFunction* function = &module->functions[function_index]; | 220 const WasmFunction* function = &module->functions[function_index]; |
221 instance->function_table->set(i, Smi::FromInt(function->sig_index)); | 221 instance->function_table->set(i, Smi::FromInt(function->sig_index)); |
222 instance->function_table->set(i + table_size, | 222 instance->function_table->set(i + table_size, |
223 *instance->function_code[function_index]); | 223 *instance->function_code[function_index]); |
224 } | 224 } |
225 } | 225 } |
| 226 WasmFunction* GetFunctionAt(int index) { return &module_.functions[index]; } |
226 | 227 |
227 private: | 228 private: |
228 WasmModule module_; | 229 WasmModule module_; |
229 WasmModuleInstance instance_; | 230 WasmModuleInstance instance_; |
230 Isolate* isolate_; | 231 Isolate* isolate_; |
231 uint32_t global_offset; | 232 uint32_t global_offset; |
232 V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data. | 233 V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data. |
233 | 234 |
234 WasmGlobal* AddGlobal(MachineType mem_type) { | 235 const WasmGlobal* AddGlobal(MachineType mem_type) { |
235 byte size = WasmOpcodes::MemSize(mem_type); | 236 byte size = WasmOpcodes::MemSize(mem_type); |
236 global_offset = (global_offset + size - 1) & ~(size - 1); // align | 237 global_offset = (global_offset + size - 1) & ~(size - 1); // align |
237 module->globals.push_back({0, 0, mem_type, global_offset, false}); | 238 module_.globals.push_back({0, 0, mem_type, global_offset, false}); |
238 global_offset += size; | 239 global_offset += size; |
239 // limit number of globals. | 240 // limit number of globals. |
240 CHECK_LT(global_offset, kMaxGlobalsSize); | 241 CHECK_LT(global_offset, kMaxGlobalsSize); |
241 return &module->globals.back(); | 242 return &module->globals.back(); |
242 } | 243 } |
243 }; | 244 }; |
244 | 245 |
245 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, | 246 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, |
246 FunctionSig* sig, | 247 FunctionSig* sig, |
247 SourcePositionTable* source_position_table, | 248 SourcePositionTable* source_position_table, |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 uint32_t CompileAndAdd(uint16_t sig_index = 0) { | 518 uint32_t CompileAndAdd(uint16_t sig_index = 0) { |
518 CHECK(testing_module_); | 519 CHECK(testing_module_); |
519 function()->sig_index = sig_index; | 520 function()->sig_index = sig_index; |
520 Handle<Code> code = Compile(); | 521 Handle<Code> code = Compile(); |
521 testing_module_->SetFunctionCode(function_index_, code); | 522 testing_module_->SetFunctionCode(function_index_, code); |
522 return static_cast<uint32_t>(function_index_); | 523 return static_cast<uint32_t>(function_index_); |
523 } | 524 } |
524 | 525 |
525 WasmFunction* function() { | 526 WasmFunction* function() { |
526 if (function_) return function_; | 527 if (function_) return function_; |
527 return &testing_module_->module->functions[function_index_]; | 528 return testing_module_->GetFunctionAt(function_index_); |
528 } | 529 } |
529 | 530 |
530 // Set the context, such that e.g. runtime functions can be called. | 531 // Set the context, such that e.g. runtime functions can be called. |
531 void SetModuleContext() { | 532 void SetModuleContext() { |
532 if (!testing_module_->instance->context.is_null()) { | 533 if (!testing_module_->instance->context.is_null()) { |
533 CHECK(testing_module_->instance->context.is_identical_to( | 534 CHECK(testing_module_->instance->context.is_identical_to( |
534 main_isolate()->native_context())); | 535 main_isolate()->native_context())); |
535 return; | 536 return; |
536 } | 537 } |
537 testing_module_->instance->context = main_isolate()->native_context(); | 538 testing_module_->instance->context = main_isolate()->native_context(); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 // RunWasmInterpreted_##name version will allow each test to also run in the | 664 // RunWasmInterpreted_##name version will allow each test to also run in the |
664 // interpreter. | 665 // interpreter. |
665 #define WASM_EXEC_TEST(name) \ | 666 #define WASM_EXEC_TEST(name) \ |
666 void RunWasm_##name(); \ | 667 void RunWasm_##name(); \ |
667 TEST(RunWasmCompiled_##name) { RunWasm_##name(); } \ | 668 TEST(RunWasmCompiled_##name) { RunWasm_##name(); } \ |
668 void RunWasm_##name() | 669 void RunWasm_##name() |
669 | 670 |
670 } // namespace | 671 } // namespace |
671 | 672 |
672 #endif | 673 #endif |
OLD | NEW |