Chromium Code Reviews| 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 "src/base/atomic-utils.h" | 5 #include "src/base/atomic-utils.h" |
| 6 #include "src/macro-assembler.h" | 6 #include "src/macro-assembler.h" |
| 7 #include "src/objects.h" | 7 #include "src/objects.h" |
| 8 #include "src/property-descriptor.h" | 8 #include "src/property-descriptor.h" |
| 9 #include "src/v8.h" | 9 #include "src/v8.h" |
| 10 | 10 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 } else { | 108 } else { |
| 109 os << "?"; | 109 os << "?"; |
| 110 } | 110 } |
| 111 return os; | 111 return os; |
| 112 } | 112 } |
| 113 | 113 |
| 114 // A helper class for compiling multiple wasm functions that offers | 114 // A helper class for compiling multiple wasm functions that offers |
| 115 // placeholder code objects for calling functions that are not yet compiled. | 115 // placeholder code objects for calling functions that are not yet compiled. |
| 116 class WasmLinker { | 116 class WasmLinker { |
| 117 public: | 117 public: |
| 118 WasmLinker(Isolate* isolate, std::vector<Handle<Code>>* functions) | 118 WasmLinker(Isolate* isolate, uint32_t function_count) |
| 119 : isolate_(isolate), | 119 : isolate_(isolate), placeholder_code_(function_count) {} |
| 120 placeholder_code_(functions->size()), | 120 |
| 121 function_code_(functions) { | 121 void InitializePlaceholders() { |
| 122 for (uint32_t i = 0; i < placeholder_code_.size(); ++i) { | 122 for (uint32_t i = 0; i < placeholder_code_.size(); ++i) { |
| 123 CreatePlaceholder(i); | 123 placeholder_code_[i] = |
| 124 CreatePlaceholder(isolate_->factory(), i, Code::WASM_FUNCTION); | |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 // Lazy, not thread safe |
| 127 Handle<Code> GetPlaceholderCode(uint32_t index) const { | 128 Handle<Code> GetPlaceholderCode(uint32_t index) { |
| 129 if (placeholder_code_[index].is_null()) { | |
| 130 placeholder_code_[index] = | |
| 131 CreatePlaceholder(isolate_->factory(), index, Code::WASM_FUNCTION); | |
| 132 } | |
| 128 return placeholder_code_[index]; | 133 return placeholder_code_[index]; |
| 129 } | 134 } |
| 130 | 135 |
| 131 void Finish(uint32_t index, Handle<Code> code) { | 136 static void LinkModuleFunctions(Isolate* isolate, |
| 132 DCHECK(index < function_code().size()); | 137 std::vector<Handle<Code>>& functions) { |
| 133 function_code()[index] = code; | 138 for (size_t i = 0; i < functions.size(); i++) { |
| 134 } | 139 Handle<Code> code = functions[i]; |
| 135 | 140 bool modified = LinkFunction(code, functions, Code::WASM_FUNCTION); |
| 136 void Link(Handle<FixedArray> function_table, | 141 if (modified) { |
| 137 const std::vector<uint16_t>& functions) { | 142 Assembler::FlushICache(isolate, code->instruction_start(), |
| 138 for (size_t i = 0; i < function_code().size(); i++) { | 143 code->instruction_size()); |
| 139 LinkFunction(function_code()[i]); | |
| 140 } | |
| 141 if (!function_table.is_null()) { | |
| 142 int table_size = static_cast<int>(functions.size()); | |
| 143 DCHECK_EQ(function_table->length(), table_size * 2); | |
| 144 for (int i = 0; i < table_size; i++) { | |
| 145 function_table->set(i + table_size, *function_code()[functions[i]]); | |
| 146 } | 144 } |
| 147 } | 145 } |
| 148 } | 146 } |
| 149 | 147 |
| 150 private: | 148 static void LinkImports(Isolate* isolate, |
| 151 std::vector<Handle<Code>>& function_code() { return *function_code_; } | 149 std::vector<Handle<Code>>& functions, |
| 150 const std::vector<Handle<Code>>& imports) { | |
| 151 for (uint32_t i = 0; i < functions.size(); ++i) { | |
| 152 Handle<Code> code = functions[i]; | |
| 153 bool modified = | |
| 154 WasmLinker::LinkFunction(code, imports, Code::WASM_TO_JS_FUNCTION); | |
| 155 if (modified) { | |
| 156 Assembler::FlushICache(isolate, code->instruction_start(), | |
| 157 code->instruction_size()); | |
| 158 } | |
| 159 } | |
| 160 } | |
| 152 | 161 |
| 153 void CreatePlaceholder(uint32_t index) { | 162 static Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index, |
|
bradnelson
2016/06/16 17:46:54
The longer term plan is to nix the need for these,
Mircea Trofin
2016/06/17 17:16:46
Yes. I plan on doing that after I have a full, rou
| |
| 154 DCHECK(index < function_code().size()); | 163 Code::Kind kind) { |
| 155 DCHECK(function_code()[index].is_null()); | |
| 156 // Create a placeholder code object and encode the corresponding index in | 164 // Create a placeholder code object and encode the corresponding index in |
| 157 // the {constant_pool_offset} field of the code object. | 165 // the {constant_pool_offset} field of the code object. |
| 158 // TODO(titzer): placeholder code objects are somewhat dangerous. | 166 // TODO(titzer): placeholder code objects are somewhat dangerous. |
| 159 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. | 167 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. |
| 160 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; | 168 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; |
| 161 Handle<Code> code = isolate_->factory()->NewCode( | 169 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), |
| 162 desc, Code::KindField::encode(Code::WASM_FUNCTION), | 170 Handle<Object>::null()); |
| 163 Handle<Object>::null()); | |
| 164 code->set_constant_pool_offset(static_cast<int>(index) + | 171 code->set_constant_pool_offset(static_cast<int>(index) + |
| 165 kPlaceholderMarker); | 172 kPlaceholderMarker); |
| 166 placeholder_code_[index] = code; | 173 return code; |
| 167 function_code()[index] = code; | |
| 168 } | 174 } |
| 169 | 175 |
| 176 private: | |
| 170 Isolate* isolate_; | 177 Isolate* isolate_; |
| 171 std::vector<Handle<Code>> placeholder_code_; | 178 std::vector<Handle<Code>> placeholder_code_; |
| 172 std::vector<Handle<Code>>* function_code_; | |
| 173 | 179 |
| 174 void LinkFunction(Handle<Code> code) { | 180 static bool LinkFunction(Handle<Code> unlinked, |
| 181 const std::vector<Handle<Code>>& to_link, | |
| 182 Code::Kind kind) { | |
| 175 bool modified = false; | 183 bool modified = false; |
| 176 int mode_mask = RelocInfo::kCodeTargetMask; | 184 int mode_mask = RelocInfo::kCodeTargetMask; |
| 177 AllowDeferredHandleDereference embedding_raw_address; | 185 AllowDeferredHandleDereference embedding_raw_address; |
| 178 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { | 186 for (RelocIterator it(*unlinked, mode_mask); !it.done(); it.next()) { |
| 179 RelocInfo::Mode mode = it.rinfo()->rmode(); | 187 RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 180 if (RelocInfo::IsCodeTarget(mode)) { | 188 if (RelocInfo::IsCodeTarget(mode)) { |
| 181 Code* target = | 189 Code* target = |
| 182 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); | 190 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); |
| 183 if (target->kind() == Code::WASM_FUNCTION && | 191 if (target->kind() == kind && |
| 184 target->constant_pool_offset() >= kPlaceholderMarker) { | 192 target->constant_pool_offset() >= kPlaceholderMarker) { |
| 185 // Patch direct calls to placeholder code objects. | 193 // Patch direct calls to placeholder code objects. |
| 186 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker; | 194 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker; |
| 187 CHECK(index < function_code().size()); | 195 CHECK(index < to_link.size()); |
| 188 Handle<Code> new_target = function_code()[index]; | 196 Handle<Code> new_target = to_link[index]; |
| 189 if (target != *new_target) { | 197 if (target != *new_target) { |
| 190 CHECK_EQ(*placeholder_code_[index], target); | |
| 191 it.rinfo()->set_target_address(new_target->instruction_start(), | 198 it.rinfo()->set_target_address(new_target->instruction_start(), |
| 192 SKIP_WRITE_BARRIER, | 199 SKIP_WRITE_BARRIER, |
| 193 SKIP_ICACHE_FLUSH); | 200 SKIP_ICACHE_FLUSH); |
| 194 modified = true; | 201 modified = true; |
| 195 } | 202 } |
| 196 } | 203 } |
| 197 } | 204 } |
| 198 } | 205 } |
| 199 if (modified) { | 206 return modified; |
| 200 Assembler::FlushICache(isolate_, code->instruction_start(), | |
| 201 code->instruction_size()); | |
| 202 } | |
| 203 } | 207 } |
| 204 }; | 208 }; |
| 205 | 209 |
| 206 namespace { | 210 namespace { |
| 207 // Internal constants for the layout of the module object. | 211 // Internal constants for the layout of the module object. |
| 208 const int kWasmModuleInternalFieldCount = 5; | 212 const int kWasmModuleInternalFieldCount = 5; |
| 209 const int kWasmModuleFunctionTable = 0; | 213 const int kWasmModuleFunctionTable = 0; |
| 210 const int kWasmModuleCodeTable = 1; | 214 const int kWasmModuleCodeTable = 1; |
| 211 const int kWasmMemArrayBuffer = 2; | 215 const int kWasmMemArrayBuffer = 2; |
| 212 const int kWasmGlobalsArrayBuffer = 3; | 216 const int kWasmGlobalsArrayBuffer = 3; |
| 213 const int kWasmFunctionNamesArray = 4; | 217 const int kWasmFunctionNamesArray = 4; |
| 214 | 218 |
| 219 uint32_t GetMinModuleMemSize(const WasmModule* module) { | |
| 220 return WasmModule::kPageSize * module->min_mem_pages; | |
| 221 } | |
| 222 | |
| 215 void LoadDataSegments(const WasmModule* module, byte* mem_addr, | 223 void LoadDataSegments(const WasmModule* module, byte* mem_addr, |
| 216 size_t mem_size) { | 224 size_t mem_size) { |
| 217 for (const WasmDataSegment& segment : module->data_segments) { | 225 for (const WasmDataSegment& segment : module->data_segments) { |
| 218 if (!segment.init) continue; | 226 if (!segment.init) continue; |
| 219 if (!segment.source_size) continue; | 227 if (!segment.source_size) continue; |
| 220 CHECK_LT(segment.dest_addr, mem_size); | 228 CHECK_LT(segment.dest_addr, mem_size); |
| 221 CHECK_LE(segment.source_size, mem_size); | 229 CHECK_LE(segment.source_size, mem_size); |
| 222 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); | 230 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); |
| 223 byte* addr = mem_addr + segment.dest_addr; | 231 byte* addr = mem_addr + segment.dest_addr; |
| 224 memcpy(addr, module->module_start + segment.source_offset, | 232 memcpy(addr, module->module_start + segment.source_offset, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 DCHECK_EQ(0, bytes[i]); | 272 DCHECK_EQ(0, bytes[i]); |
| 265 } | 273 } |
| 266 #endif | 274 #endif |
| 267 | 275 |
| 268 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 276 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 269 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); | 277 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); |
| 270 buffer->set_is_neuterable(false); | 278 buffer->set_is_neuterable(false); |
| 271 return buffer; | 279 return buffer; |
| 272 } | 280 } |
| 273 | 281 |
| 282 void RelocateInstanceCode(WasmModuleInstance* instance) { | |
| 283 for (uint32_t i = 0; i < instance->function_code.size(); ++i) { | |
| 284 Handle<Code> function = instance->function_code[i]; | |
| 285 AllowDeferredHandleDereference embedding_raw_address; | |
| 286 int mask = 1 << RelocInfo::WASM_MEMORY_REFERENCE | | |
|
bradnelson
2016/06/16 17:46:54
You know your operator precedence well :-)
FWIW, I
Mircea Trofin
2016/06/17 17:16:46
Fixed, because I prefer the readability and clarit
| |
| 287 1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE; | |
| 288 for (RelocIterator it(*function, mask); !it.done(); it.next()) { | |
| 289 it.rinfo()->update_wasm_memory_reference( | |
| 290 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module), | |
| 291 static_cast<uint32_t>(instance->mem_size)); | |
| 292 } | |
| 293 } | |
| 294 } | |
| 295 | |
| 274 // Set the memory for a module instance to be the {memory} array buffer. | 296 // Set the memory for a module instance to be the {memory} array buffer. |
| 275 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { | 297 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { |
| 276 memory->set_is_neuterable(false); | 298 memory->set_is_neuterable(false); |
| 277 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); | 299 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); |
| 278 instance->mem_size = memory->byte_length()->Number(); | 300 instance->mem_size = memory->byte_length()->Number(); |
| 279 instance->mem_buffer = memory; | 301 instance->mem_buffer = memory; |
| 302 RelocateInstanceCode(instance); | |
| 280 } | 303 } |
| 281 | 304 |
| 282 // Allocate memory for a module instance as a new JSArrayBuffer. | 305 // Allocate memory for a module instance as a new JSArrayBuffer. |
| 283 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, | 306 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, |
| 284 WasmModuleInstance* instance) { | 307 WasmModuleInstance* instance) { |
| 285 DCHECK(instance->module); | 308 DCHECK(instance->module); |
| 286 DCHECK(instance->mem_buffer.is_null()); | 309 DCHECK(instance->mem_buffer.is_null()); |
| 287 | 310 |
| 288 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { | 311 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { |
| 289 thrower->Error("Out of memory: wasm memory too large"); | 312 thrower->Error("Out of memory: wasm memory too large"); |
| 290 return false; | 313 return false; |
| 291 } | 314 } |
| 292 instance->mem_size = WasmModule::kPageSize * instance->module->min_mem_pages; | 315 instance->mem_size = GetMinModuleMemSize(instance->module); |
| 293 instance->mem_buffer = | 316 instance->mem_buffer = |
| 294 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); | 317 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); |
| 295 if (!instance->mem_start) { | 318 if (!instance->mem_start) { |
| 296 thrower->Error("Out of memory: wasm memory"); | 319 thrower->Error("Out of memory: wasm memory"); |
| 297 instance->mem_size = 0; | 320 instance->mem_size = 0; |
| 298 return false; | 321 return false; |
| 299 } | 322 } |
| 323 RelocateInstanceCode(instance); | |
| 300 return true; | 324 return true; |
| 301 } | 325 } |
| 302 | 326 |
| 303 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate, | 327 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate, |
| 304 WasmModuleInstance* instance) { | 328 WasmModuleInstance* instance) { |
| 305 uint32_t globals_size = instance->module->globals_size; | 329 uint32_t globals_size = instance->module->globals_size; |
| 306 if (globals_size > 0) { | 330 if (globals_size > 0) { |
| 307 instance->globals_buffer = | 331 instance->globals_buffer = |
| 308 NewArrayBuffer(isolate, globals_size, &instance->globals_start); | 332 NewArrayBuffer(isolate, globals_size, &instance->globals_start); |
| 309 if (!instance->globals_start) { | 333 if (!instance->globals_start) { |
| 310 // Not enough space for backing store of globals. | 334 // Not enough space for backing store of globals. |
| 311 thrower->Error("Out of memory: wasm globals"); | 335 thrower->Error("Out of memory: wasm globals"); |
| 312 return false; | 336 return false; |
| 313 } | 337 } |
| 338 | |
| 339 for (uint32_t i = 0; i < instance->function_code.size(); ++i) { | |
| 340 Handle<Code> function = instance->function_code[i]; | |
| 341 AllowDeferredHandleDereference embedding_raw_address; | |
| 342 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE; | |
| 343 for (RelocIterator it(*function, mask); !it.done(); it.next()) { | |
| 344 it.rinfo()->update_wasm_global_reference(nullptr, | |
| 345 instance->globals_start); | |
| 346 } | |
| 347 } | |
| 314 } | 348 } |
| 315 return true; | 349 return true; |
| 316 } | 350 } |
| 317 } // namespace | 351 } // namespace |
| 318 | 352 |
| 319 WasmModule::WasmModule() | 353 WasmModule::WasmModule() |
| 320 : module_start(nullptr), | 354 : module_start(nullptr), |
| 321 module_end(nullptr), | 355 module_end(nullptr), |
| 322 min_mem_pages(0), | 356 min_mem_pages(0), |
| 323 max_mem_pages(0), | 357 max_mem_pages(0), |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 PrintF("Total generated wasm code: %zu bytes\n", code_size); | 503 PrintF("Total generated wasm code: %zu bytes\n", code_size); |
| 470 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); | 504 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); |
| 471 } | 505 } |
| 472 } | 506 } |
| 473 }; | 507 }; |
| 474 | 508 |
| 475 bool CompileWrappersToImportedFunctions( | 509 bool CompileWrappersToImportedFunctions( |
| 476 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, | 510 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, |
| 477 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory, | 511 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory, |
| 478 ModuleEnv* module_env, CodeStats& code_stats) { | 512 ModuleEnv* module_env, CodeStats& code_stats) { |
| 479 uint32_t index = 0; | |
| 480 if (module->import_table.size() > 0) { | 513 if (module->import_table.size() > 0) { |
| 481 instance->import_code.reserve(module->import_table.size()); | 514 instance->import_code.reserve(module->import_table.size()); |
| 482 for (const WasmImport& import : module->import_table) { | 515 for (uint32_t index = 0; index < module->import_table.size(); ++index) { |
| 516 const WasmImport& import = module->import_table[index]; | |
| 483 WasmName module_name = module->GetNameOrNull(import.module_name_offset, | 517 WasmName module_name = module->GetNameOrNull(import.module_name_offset, |
| 484 import.module_name_length); | 518 import.module_name_length); |
| 485 WasmName function_name = module->GetNameOrNull( | 519 WasmName function_name = module->GetNameOrNull( |
| 486 import.function_name_offset, import.function_name_length); | 520 import.function_name_offset, import.function_name_length); |
| 487 MaybeHandle<JSFunction> function = LookupFunction( | 521 MaybeHandle<JSFunction> function = LookupFunction( |
| 488 *thrower, factory, ffi, index, module_name, function_name); | 522 *thrower, factory, ffi, index, module_name, function_name); |
| 489 if (function.is_null()) return false; | 523 if (function.is_null()) return false; |
| 490 | 524 |
| 491 Handle<Code> code = compiler::CompileWasmToJSWrapper( | 525 Handle<Code> code = compiler::CompileWasmToJSWrapper( |
| 492 isolate, module_env, function.ToHandleChecked(), import.sig, | 526 isolate, module_env, function.ToHandleChecked(), import.sig, |
| 493 module_name, function_name); | 527 module_name, function_name); |
| 494 instance->import_code.push_back(code); | 528 instance->import_code[index] = code; |
| 495 code_stats.Record(*code); | 529 code_stats.Record(*code); |
| 496 index++; | |
| 497 } | 530 } |
| 498 } | 531 } |
| 499 return true; | 532 return true; |
| 500 } | 533 } |
| 501 | 534 |
| 502 void InitializeParallelCompilation( | 535 void InitializeParallelCompilation( |
| 503 Isolate* isolate, const std::vector<WasmFunction>& functions, | 536 Isolate* isolate, const std::vector<WasmFunction>& functions, |
| 504 std::vector<compiler::WasmCompilationUnit*>& compilation_units, | 537 std::vector<compiler::WasmCompilationUnit*>& compilation_units, |
| 505 ModuleEnv& module_env, ErrorThrower& thrower) { | 538 ModuleEnv& module_env, ErrorThrower& thrower) { |
| 506 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) { | 539 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) { |
| 507 compilation_units[i] = new compiler::WasmCompilationUnit( | 540 compilation_units[i] = new compiler::WasmCompilationUnit( |
| 508 &thrower, isolate, &module_env, &functions[i], i); | 541 &thrower, isolate, &module_env, &functions[i], i); |
| 509 } | 542 } |
| 543 module_env.linker->InitializePlaceholders(); | |
| 510 } | 544 } |
| 511 | 545 |
| 512 uint32_t* StartCompilationTasks( | 546 uint32_t* StartCompilationTasks( |
| 513 Isolate* isolate, | 547 Isolate* isolate, |
| 514 std::vector<compiler::WasmCompilationUnit*>& compilation_units, | 548 std::vector<compiler::WasmCompilationUnit*>& compilation_units, |
| 515 std::queue<compiler::WasmCompilationUnit*>& executed_units, | 549 std::queue<compiler::WasmCompilationUnit*>& executed_units, |
| 516 const base::SmartPointer<base::Semaphore>& pending_tasks, | 550 const base::SmartPointer<base::Semaphore>& pending_tasks, |
| 517 base::Mutex& result_mutex, base::AtomicNumber<size_t>& next_unit) { | 551 base::Mutex& result_mutex, base::AtomicNumber<size_t>& next_unit) { |
| 518 const size_t num_tasks = | 552 const size_t num_tasks = |
| 519 Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks), | 553 Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 thrower, isolate, module_env, &func); | 680 thrower, isolate, module_env, &func); |
| 647 if (code.is_null()) { | 681 if (code.is_null()) { |
| 648 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), | 682 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), |
| 649 str.start()); | 683 str.start()); |
| 650 break; | 684 break; |
| 651 } | 685 } |
| 652 // Install the code into the linker table. | 686 // Install the code into the linker table. |
| 653 functions[i] = code; | 687 functions[i] = code; |
| 654 } | 688 } |
| 655 } | 689 } |
| 690 | |
| 691 void PopulateFunctionTable(WasmModuleInstance* instance) { | |
| 692 if (!instance->function_table.is_null()) { | |
| 693 int table_size = static_cast<int>(instance->module->function_table.size()); | |
| 694 DCHECK_EQ(instance->function_table->length(), table_size * 2); | |
| 695 for (int i = 0; i < table_size; i++) { | |
| 696 instance->function_table->set( | |
| 697 i + table_size, | |
| 698 *instance->function_code[instance->module->function_table[i]]); | |
| 699 } | |
| 700 } | |
| 701 } | |
| 656 } // namespace | 702 } // namespace |
| 657 | 703 |
| 658 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, | 704 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, |
| 659 std::vector<Handle<Code>>& functions) { | 705 std::vector<Handle<Code>>& functions) { |
| 660 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { | 706 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { |
| 661 Handle<Code> code = functions[i]; | 707 Handle<Code> code = functions[i]; |
| 662 DCHECK(code->deoptimization_data() == nullptr || | 708 DCHECK(code->deoptimization_data() == nullptr || |
| 663 code->deoptimization_data()->length() == 0); | 709 code->deoptimization_data()->length() == 0); |
| 664 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); | 710 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); |
| 665 if (!js_object.is_null()) { | 711 if (!js_object.is_null()) { |
| 666 deopt_data->set(0, *js_object); | 712 deopt_data->set(0, *js_object); |
| 667 } | 713 } |
| 668 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | 714 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
| 669 deopt_data->set_length(2); | 715 deopt_data->set_length(2); |
| 670 code->set_deoptimization_data(*deopt_data); | 716 code->set_deoptimization_data(*deopt_data); |
| 671 } | 717 } |
| 672 } | 718 } |
| 673 | 719 |
| 720 Handle<FixedArray> WasmModule::Compile(Isolate* isolate) const { | |
|
bradnelson
2016/06/16 17:46:54
CompileFunctions ?
Mircea Trofin
2016/06/17 17:16:46
Done.
| |
| 721 Factory* factory = isolate->factory(); | |
| 722 ErrorThrower thrower(isolate, "WasmModule::Compile()"); | |
| 723 CodeStats code_stats; | |
| 724 | |
| 725 WasmModuleInstance temp_instance_for_compilation(this); | |
|
bradnelson
2016/06/16 17:46:54
Yuck.
As we talked about, we should clean up the m
Mircea Trofin
2016/06/17 17:16:46
Yup! :)
| |
| 726 temp_instance_for_compilation.function_table = | |
| 727 BuildFunctionTable(isolate, this); | |
| 728 temp_instance_for_compilation.context = isolate->native_context(); | |
| 729 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this); | |
| 730 temp_instance_for_compilation.mem_start = nullptr; | |
| 731 temp_instance_for_compilation.globals_start = nullptr; | |
| 732 | |
| 733 WasmLinker linker(isolate, | |
| 734 static_cast<uint32_t>( | |
| 735 temp_instance_for_compilation.function_code.size())); | |
| 736 ModuleEnv module_env; | |
| 737 module_env.module = this; | |
| 738 module_env.linker = &linker; | |
| 739 module_env.instance = &temp_instance_for_compilation; | |
| 740 module_env.origin = origin; | |
| 741 | |
| 742 Handle<FixedArray> ret = | |
| 743 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | |
| 744 | |
| 745 temp_instance_for_compilation.import_code.resize(import_table.size()); | |
| 746 for (uint32_t i = 0; i < import_table.size(); ++i) { | |
| 747 temp_instance_for_compilation.import_code[i] = | |
| 748 WasmLinker::CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); | |
| 749 } | |
| 750 isolate->counters()->wasm_functions_per_module()->AddSample( | |
| 751 static_cast<int>(functions.size())); | |
| 752 if (FLAG_wasm_num_compilation_tasks != 0) { | |
| 753 CompileInParallel(isolate, this, | |
| 754 temp_instance_for_compilation.function_code, &thrower, | |
| 755 &module_env); | |
| 756 } else { | |
| 757 CompileSequentially(isolate, this, | |
| 758 temp_instance_for_compilation.function_code, &thrower, | |
| 759 &module_env); | |
| 760 } | |
| 761 if (thrower.error()) { | |
| 762 return Handle<FixedArray>::null(); | |
| 763 } | |
| 764 | |
| 765 WasmLinker::LinkModuleFunctions(isolate, | |
| 766 temp_instance_for_compilation.function_code); | |
| 767 | |
| 768 // At this point, compilation has completed. Update the code table | |
| 769 // and record sizes. | |
| 770 for (size_t i = FLAG_skip_compiling_wasm_funcs; | |
| 771 i < temp_instance_for_compilation.function_code.size(); ++i) { | |
| 772 Code* code = *temp_instance_for_compilation.function_code[i]; | |
| 773 ret->set(static_cast<int>(i), code); | |
| 774 code_stats.Record(code); | |
| 775 } | |
| 776 | |
| 777 PopulateFunctionTable(&temp_instance_for_compilation); | |
| 778 | |
| 779 return ret; | |
| 780 } | |
| 781 | |
| 674 // Instantiates a wasm module as a JSObject. | 782 // Instantiates a wasm module as a JSObject. |
| 675 // * allocates a backing store of {mem_size} bytes. | 783 // * allocates a backing store of {mem_size} bytes. |
| 676 // * installs a named property "memory" for that buffer if exported | 784 // * installs a named property "memory" for that buffer if exported |
| 677 // * installs named properties on the object for exported functions | 785 // * installs named properties on the object for exported functions |
| 678 // * compiles wasm code to machine code | 786 // * compiles wasm code to machine code |
| 679 MaybeHandle<JSObject> WasmModule::Instantiate( | 787 MaybeHandle<JSObject> WasmModule::Instantiate( |
| 680 Isolate* isolate, Handle<JSReceiver> ffi, | 788 Isolate* isolate, Handle<JSReceiver> ffi, |
| 681 Handle<JSArrayBuffer> memory) const { | 789 Handle<JSArrayBuffer> memory) const { |
| 682 HistogramTimerScope wasm_instantiate_module_time_scope( | 790 HistogramTimerScope wasm_instantiate_module_time_scope( |
| 683 isolate->counters()->wasm_instantiate_module_time()); | 791 isolate->counters()->wasm_instantiate_module_time()); |
| 684 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); | 792 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); |
| 685 Factory* factory = isolate->factory(); | 793 Factory* factory = isolate->factory(); |
| 686 | 794 |
| 687 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code | 795 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code |
| 688 // objects created for this module. | 796 // objects created for this module. |
| 689 // TODO(titzer): switch this to TRACE_EVENT | 797 // TODO(titzer): switch this to TRACE_EVENT |
| 690 CodeStats code_stats; | 798 CodeStats code_stats; |
| 691 | 799 |
| 692 //------------------------------------------------------------------------- | 800 //------------------------------------------------------------------------- |
| 693 // Allocate the instance and its JS counterpart. | 801 // Allocate the instance and its JS counterpart. |
| 694 //------------------------------------------------------------------------- | 802 //------------------------------------------------------------------------- |
| 695 Handle<Map> map = factory->NewMap( | 803 Handle<Map> map = factory->NewMap( |
| 696 JS_OBJECT_TYPE, | 804 JS_OBJECT_TYPE, |
| 697 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 805 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
| 698 WasmModuleInstance instance(this); | 806 WasmModuleInstance instance(this); |
| 699 instance.context = isolate->native_context(); | 807 instance.context = isolate->native_context(); |
| 700 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); | 808 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); |
| 701 Handle<FixedArray> code_table = | 809 |
| 702 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | 810 Handle<FixedArray> code_table = Compile(isolate); |
| 811 if (code_table.is_null()) return Handle<JSObject>::null(); | |
| 812 | |
| 703 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); | 813 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); |
| 704 | 814 |
| 815 for (uint32_t i = 0; i < functions.size(); ++i) { | |
| 816 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i))); | |
| 817 instance.function_code[i] = code; | |
| 818 } | |
| 819 | |
| 705 //------------------------------------------------------------------------- | 820 //------------------------------------------------------------------------- |
| 706 // Allocate and initialize the linear memory. | 821 // Allocate and initialize the linear memory. |
| 707 //------------------------------------------------------------------------- | 822 //------------------------------------------------------------------------- |
| 708 isolate->counters()->wasm_min_mem_pages_count()->AddSample( | 823 isolate->counters()->wasm_min_mem_pages_count()->AddSample( |
| 709 instance.module->min_mem_pages); | 824 instance.module->min_mem_pages); |
| 710 isolate->counters()->wasm_max_mem_pages_count()->AddSample( | 825 isolate->counters()->wasm_max_mem_pages_count()->AddSample( |
| 711 instance.module->max_mem_pages); | 826 instance.module->max_mem_pages); |
| 712 if (memory.is_null()) { | 827 if (memory.is_null()) { |
| 713 if (!AllocateMemory(&thrower, isolate, &instance)) { | 828 if (!AllocateMemory(&thrower, isolate, &instance)) { |
| 714 return MaybeHandle<JSObject>(); | 829 return MaybeHandle<JSObject>(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 727 return MaybeHandle<JSObject>(); | 842 return MaybeHandle<JSObject>(); |
| 728 } | 843 } |
| 729 if (!instance.globals_buffer.is_null()) { | 844 if (!instance.globals_buffer.is_null()) { |
| 730 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, | 845 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, |
| 731 *instance.globals_buffer); | 846 *instance.globals_buffer); |
| 732 } | 847 } |
| 733 | 848 |
| 734 HistogramTimerScope wasm_compile_module_time_scope( | 849 HistogramTimerScope wasm_compile_module_time_scope( |
| 735 isolate->counters()->wasm_compile_module_time()); | 850 isolate->counters()->wasm_compile_module_time()); |
| 736 | 851 |
| 737 instance.function_table = BuildFunctionTable(isolate, this); | |
| 738 WasmLinker linker(isolate, &instance.function_code); | |
| 739 ModuleEnv module_env; | 852 ModuleEnv module_env; |
| 740 module_env.module = this; | 853 module_env.module = this; |
| 741 module_env.instance = &instance; | 854 module_env.instance = &instance; |
| 742 module_env.linker = &linker; | |
| 743 module_env.origin = origin; | 855 module_env.origin = origin; |
| 744 | 856 |
| 745 //------------------------------------------------------------------------- | 857 //------------------------------------------------------------------------- |
| 746 // Compile wrappers to imported functions. | 858 // Compile wrappers to imported functions. |
| 747 //------------------------------------------------------------------------- | 859 //------------------------------------------------------------------------- |
| 748 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance, | 860 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance, |
| 749 &thrower, factory, &module_env, | 861 &thrower, factory, &module_env, |
| 750 code_stats)) { | 862 code_stats)) { |
| 751 return MaybeHandle<JSObject>(); | 863 return MaybeHandle<JSObject>(); |
| 752 } | 864 } |
| 753 //------------------------------------------------------------------------- | |
| 754 // Compile all functions in the module. | |
| 755 //------------------------------------------------------------------------- | |
| 756 { | 865 { |
| 757 isolate->counters()->wasm_functions_per_module()->AddSample( | |
| 758 static_cast<int>(functions.size())); | |
| 759 if (FLAG_wasm_num_compilation_tasks != 0) { | |
| 760 CompileInParallel(isolate, this, instance.function_code, &thrower, | |
| 761 &module_env); | |
| 762 } else { | |
| 763 // 5) The main thread finishes the compilation. | |
| 764 CompileSequentially(isolate, this, instance.function_code, &thrower, | |
| 765 &module_env); | |
| 766 } | |
| 767 if (thrower.error()) { | |
| 768 return Handle<JSObject>::null(); | |
| 769 } | |
| 770 | |
| 771 // At this point, compilation has completed. Update the code table | |
| 772 // and record sizes. | |
| 773 for (size_t i = FLAG_skip_compiling_wasm_funcs; | |
| 774 i < instance.function_code.size(); ++i) { | |
| 775 Code* code = *instance.function_code[i]; | |
| 776 code_table->set(static_cast<int>(i), code); | |
| 777 code_stats.Record(code); | |
| 778 } | |
| 779 | |
| 780 // Patch all direct call sites. | |
| 781 linker.Link(instance.function_table, this->function_table); | |
| 782 instance.js_object->SetInternalField(kWasmModuleFunctionTable, | 866 instance.js_object->SetInternalField(kWasmModuleFunctionTable, |
| 783 Smi::FromInt(0)); | 867 Smi::FromInt(0)); |
| 868 WasmLinker::LinkImports(isolate, instance.function_code, | |
| 869 instance.import_code); | |
| 784 | 870 |
| 785 SetDeoptimizationData(factory, instance.js_object, instance.function_code); | 871 SetDeoptimizationData(factory, instance.js_object, instance.function_code); |
| 786 | 872 |
| 787 //------------------------------------------------------------------------- | 873 //------------------------------------------------------------------------- |
| 788 // Create and populate the exports object. | 874 // Create and populate the exports object. |
| 789 //------------------------------------------------------------------------- | 875 //------------------------------------------------------------------------- |
| 790 if (export_table.size() > 0 || mem_export) { | 876 if (export_table.size() > 0 || mem_export) { |
| 791 Handle<JSObject> exports_object; | 877 Handle<JSObject> exports_object; |
| 792 if (origin == kWasmOrigin) { | 878 if (origin == kWasmOrigin) { |
| 793 // Create the "exports" object. | 879 // Create the "exports" object. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 907 } | 993 } |
| 908 | 994 |
| 909 int32_t retval = CompileAndRunWasmModule(isolate, result.val); | 995 int32_t retval = CompileAndRunWasmModule(isolate, result.val); |
| 910 delete result.val; | 996 delete result.val; |
| 911 return retval; | 997 return retval; |
| 912 } | 998 } |
| 913 | 999 |
| 914 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) { | 1000 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) { |
| 915 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); | 1001 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); |
| 916 WasmModuleInstance instance(module); | 1002 WasmModuleInstance instance(module); |
| 1003 Handle<FixedArray> code_table = module->Compile(isolate); | |
| 1004 | |
| 1005 if (code_table.is_null()) return -1; | |
| 1006 | |
| 1007 for (uint32_t i = 0; i < module->functions.size(); ++i) { | |
| 1008 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i))); | |
| 1009 instance.function_code[i] = code; | |
| 1010 } | |
| 917 | 1011 |
| 918 // Allocate and initialize the linear memory. | 1012 // Allocate and initialize the linear memory. |
| 919 if (!AllocateMemory(&thrower, isolate, &instance)) { | 1013 if (!AllocateMemory(&thrower, isolate, &instance)) { |
| 920 return -1; | 1014 return -1; |
| 921 } | 1015 } |
| 922 LoadDataSegments(module, instance.mem_start, instance.mem_size); | 1016 LoadDataSegments(module, instance.mem_start, instance.mem_size); |
| 923 | 1017 |
| 924 // Allocate the globals area if necessary. | 1018 // Allocate the globals area if necessary. |
| 925 if (!AllocateGlobals(&thrower, isolate, &instance)) { | 1019 if (!AllocateGlobals(&thrower, isolate, &instance)) { |
| 926 return -1; | 1020 return -1; |
| 927 } | 1021 } |
| 928 | 1022 |
| 929 // Build the function table. | |
| 930 instance.function_table = BuildFunctionTable(isolate, module); | |
| 931 | |
| 932 // Create module environment. | 1023 // Create module environment. |
| 933 WasmLinker linker(isolate, &instance.function_code); | 1024 WasmLinker linker(isolate, |
| 1025 static_cast<uint32_t>(instance.function_code.size())); | |
| 934 ModuleEnv module_env; | 1026 ModuleEnv module_env; |
| 935 module_env.module = module; | 1027 module_env.module = module; |
| 936 module_env.instance = &instance; | 1028 module_env.instance = &instance; |
| 937 module_env.linker = &linker; | 1029 module_env.linker = &linker; |
| 938 module_env.origin = module->origin; | 1030 module_env.origin = module->origin; |
| 939 | 1031 |
| 940 if (module->export_table.size() == 0) { | 1032 if (module->export_table.size() == 0) { |
| 941 thrower.Error("WASM.compileRun() failed: no exported functions"); | 1033 thrower.Error("WASM.compileRun() failed: no exported functions"); |
| 942 return -2; | 1034 return -2; |
| 943 } | 1035 } |
| 944 | 1036 |
| 945 // Compile all functions. | 1037 // Compile all functions. |
| 946 for (const WasmFunction& func : module->functions) { | 1038 for (const WasmFunction& func : module->functions) { |
| 947 // Compile the function and install it in the linker. | 1039 // Compile the function and install it in the linker. |
| 948 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction( | 1040 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction( |
| 949 &thrower, isolate, &module_env, &func); | 1041 &thrower, isolate, &module_env, &func); |
| 950 if (!code.is_null()) linker.Finish(func.func_index, code); | 1042 if (!code.is_null()) instance.function_code[func.func_index] = code; |
| 951 if (thrower.error()) return -1; | 1043 if (thrower.error()) return -1; |
| 952 } | 1044 } |
| 953 | 1045 |
| 954 linker.Link(instance.function_table, instance.module->function_table); | 1046 WasmLinker::LinkModuleFunctions(isolate, instance.function_code); |
| 955 | 1047 |
| 956 // Wrap the main code so it can be called as a JS function. | 1048 // Wrap the main code so it can be called as a JS function. |
| 957 uint32_t main_index = module->export_table.back().func_index; | 1049 uint32_t main_index = module->export_table.back().func_index; |
| 958 Handle<Code> main_code = instance.function_code[main_index]; | 1050 Handle<Code> main_code = instance.function_code[main_index]; |
| 959 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); | 1051 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); |
| 960 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); | 1052 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); |
| 961 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( | 1053 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( |
| 962 isolate, &module_env, name, main_code, module_object, main_index); | 1054 isolate, &module_env, name, main_code, module_object, main_index); |
| 963 | 1055 |
| 964 // Call the JS function. | 1056 // Call the JS function. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1014 // TODO(clemensh): Check wasm byte header once we store a copy of the bytes. | 1106 // TODO(clemensh): Check wasm byte header once we store a copy of the bytes. |
| 1015 return object->GetInternalFieldCount() == kWasmModuleInternalFieldCount && | 1107 return object->GetInternalFieldCount() == kWasmModuleInternalFieldCount && |
| 1016 object->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && | 1108 object->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && |
| 1017 object->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() && | 1109 object->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() && |
| 1018 object->GetInternalField(kWasmFunctionNamesArray)->IsByteArray(); | 1110 object->GetInternalField(kWasmFunctionNamesArray)->IsByteArray(); |
| 1019 } | 1111 } |
| 1020 | 1112 |
| 1021 } // namespace wasm | 1113 } // namespace wasm |
| 1022 } // namespace internal | 1114 } // namespace internal |
| 1023 } // namespace v8 | 1115 } // namespace v8 |
| OLD | NEW |