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 #ifndef V8_WASM_MODULE_H_ | 5 #ifndef V8_WASM_MODULE_H_ |
| 6 #define V8_WASM_MODULE_H_ | 6 #define V8_WASM_MODULE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "src/api.h" | 10 #include "src/api.h" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 | 153 |
| 154 // Static representation of a wasm indirect call table. | 154 // Static representation of a wasm indirect call table. |
| 155 struct WasmIndirectFunctionTable { | 155 struct WasmIndirectFunctionTable { |
| 156 uint32_t size; // initial table size. | 156 uint32_t size; // initial table size. |
| 157 uint32_t max_size; // maximum table size. | 157 uint32_t max_size; // maximum table size. |
| 158 std::vector<uint16_t> values; // function table. | 158 std::vector<uint16_t> values; // function table. |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; | 161 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; |
| 162 | 162 |
| 163 class WasmCompiledModule; | |
| 164 | |
| 163 // Static representation of a module. | 165 // Static representation of a module. |
| 164 struct WasmModule { | 166 struct WasmModule { |
| 165 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. | 167 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
| 166 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb | 168 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
| 167 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb | 169 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb |
| 168 | 170 |
| 169 const byte* module_start; // starting address for the module bytes. | 171 const byte* module_start; // starting address for the module bytes. |
| 170 const byte* module_end; // end address for the module bytes. | 172 const byte* module_end; // end address for the module bytes. |
| 171 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 173 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
| 172 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. | 174 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 size_t size = module_end - module_start; | 233 size_t size = module_end - module_start; |
| 232 return start <= size && end <= size; | 234 return start <= size && end <= size; |
| 233 } | 235 } |
| 234 | 236 |
| 235 // Creates a new instantiation of the module in the given isolate. | 237 // Creates a new instantiation of the module in the given isolate. |
| 236 static MaybeHandle<JSObject> Instantiate(Isolate* isolate, | 238 static MaybeHandle<JSObject> Instantiate(Isolate* isolate, |
| 237 Handle<JSObject> module_object, | 239 Handle<JSObject> module_object, |
| 238 Handle<JSReceiver> ffi, | 240 Handle<JSReceiver> ffi, |
| 239 Handle<JSArrayBuffer> memory); | 241 Handle<JSArrayBuffer> memory); |
| 240 | 242 |
| 241 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, | 243 MaybeHandle<WasmCompiledModule> CompileFunctions(Isolate* isolate, |
| 242 ErrorThrower* thrower) const; | 244 ErrorThrower* thrower) const; |
| 243 | 245 |
| 244 private: | 246 private: |
| 245 DISALLOW_COPY_AND_ASSIGN(WasmModule); | 247 DISALLOW_COPY_AND_ASSIGN(WasmModule); |
| 246 }; | 248 }; |
| 247 | 249 |
| 248 // An instantiated WASM module, including memory, function table, etc. | 250 // An instantiated WASM module, including memory, function table, etc. |
| 249 struct WasmModuleInstance { | 251 struct WasmModuleInstance { |
| 250 const WasmModule* module; // static representation of the module. | 252 const WasmModule* module; // static representation of the module. |
| 251 // -- Heap allocated -------------------------------------------------------- | 253 // -- Heap allocated -------------------------------------------------------- |
| 252 Handle<JSObject> js_object; // JavaScript module object. | 254 Handle<JSObject> js_object; // JavaScript module object. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 | 342 |
| 341 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 343 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 342 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 344 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
| 343 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 345 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
| 344 | 346 |
| 345 typedef Result<const WasmModule*> ModuleResult; | 347 typedef Result<const WasmModule*> ModuleResult; |
| 346 typedef Result<WasmFunction*> FunctionResult; | 348 typedef Result<WasmFunction*> FunctionResult; |
| 347 typedef std::vector<std::pair<int, int>> FunctionOffsets; | 349 typedef std::vector<std::pair<int, int>> FunctionOffsets; |
| 348 typedef Result<FunctionOffsets> FunctionOffsetsResult; | 350 typedef Result<FunctionOffsets> FunctionOffsetsResult; |
| 349 | 351 |
| 352 class WasmCompiledModule : public FixedArray { | |
| 353 public: | |
| 354 static WasmCompiledModule* cast(Object* fixed_array) { | |
| 355 return reinterpret_cast<WasmCompiledModule*>(fixed_array); | |
| 356 } | |
| 357 static Handle<WasmCompiledModule> New(Isolate* isolate) { | |
| 358 Handle<FixedArray> ret = | |
| 359 isolate->factory()->NewFixedArray(kTotalFieldCount, TENURED); | |
| 360 ret->set(8, *isolate->factory()->NewHeapNumber(0.0, MUTABLE, TENURED)); | |
| 361 ret->set(11, *isolate->factory()->NewHeapNumber(0.0, MUTABLE, TENURED)); | |
| 362 ret->set(12, *isolate->factory()->NewHeapNumber(0.0, MUTABLE, TENURED)); | |
| 363 return handle(WasmCompiledModule::cast(*ret)); | |
| 364 } | |
| 365 | |
| 366 #define WCM_PROPERTY(ID, TYPE, NAME) \ | |
| 367 Handle<TYPE> NAME() { return handle(ptr_to_##NAME()); } \ | |
| 368 MaybeHandle<TYPE> maybe_##NAME() { \ | |
| 369 if (has_##NAME()) return NAME(); \ | |
| 370 return MaybeHandle<TYPE>(); \ | |
| 371 } \ | |
| 372 TYPE* ptr_to_##NAME() { \ | |
| 373 Object* obj = get(ID); \ | |
| 374 if (!obj->Is##TYPE()) return nullptr; \ | |
| 375 return TYPE::cast(obj); \ | |
| 376 } \ | |
| 377 void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); } \ | |
| 378 void set_ptr_to_##NAME(TYPE* value) { set(ID, value); } \ | |
| 379 bool has_##NAME() { return get(ID)->Is##TYPE(); } \ | |
| 380 void reset_##NAME(Isolate* isolate) { \ | |
| 381 set(ID, *isolate->factory()->undefined_value()); \ | |
| 382 } | |
| 383 | |
| 384 #define WCM_SMALL_NUMERIC_PROPERTY(ID, TYPE, NAME) \ | |
| 385 TYPE NAME() { return static_cast<TYPE>(Smi::cast(get(ID))->value()); } \ | |
| 386 void set_##NAME(TYPE value) { \ | |
| 387 set(ID, Smi::FromInt(static_cast<int>(value))); \ | |
| 388 } | |
| 389 | |
| 390 #define WCM_LARGE_NUMERIC_PROPERTY(ID, TYPE, NAME) \ | |
| 391 TYPE NAME() { \ | |
| 392 return static_cast<TYPE>(HeapNumber::cast(get(ID))->value()); \ | |
| 393 } \ | |
| 394 void set_##NAME(TYPE value) { \ | |
| 395 HeapNumber::cast(get(ID))->set_value(static_cast<double>(value)); \ | |
| 396 } | |
| 397 | |
| 398 #define WCM_WEAK_LINK(ID, TYPE, NAME) \ | |
| 399 WCM_PROPERTY(ID, WeakCell, weak_##NAME); \ | |
| 400 Handle<TYPE> NAME() { return handle(TYPE::cast(weak_##NAME()->value())); } | |
| 401 | |
| 402 WCM_PROPERTY(0, FixedArray, functions); | |
|
bradnelson
2016/09/13 23:54:49
Use x-macros as discussed.
Mircea Trofin
2016/09/14 16:06:28
Done.
| |
| 403 WCM_PROPERTY(1, FixedArray, import_data); | |
| 404 WCM_PROPERTY(2, FixedArray, import_map); | |
| 405 WCM_PROPERTY(3, FixedArray, exports); | |
| 406 WCM_PROPERTY(4, FixedArray, startup_function); | |
| 407 WCM_PROPERTY(5, FixedArray, indirect_function_tables); | |
| 408 WCM_PROPERTY(6, String, module_bytes); | |
| 409 WCM_PROPERTY(7, ByteArray, function_names); | |
| 410 WCM_LARGE_NUMERIC_PROPERTY(8, uint32_t, min_required_memory); | |
| 411 WCM_PROPERTY(9, FixedArray, data_segments_info); | |
| 412 WCM_PROPERTY(10, ByteArray, data_segments); | |
| 413 WCM_LARGE_NUMERIC_PROPERTY(11, uint32_t, globals_size); | |
| 414 WCM_LARGE_NUMERIC_PROPERTY(12, uint32_t, mem_size); | |
| 415 WCM_PROPERTY(13, JSArrayBuffer, mem_start); | |
| 416 WCM_SMALL_NUMERIC_PROPERTY(14, bool, export_memory); | |
| 417 WCM_SMALL_NUMERIC_PROPERTY(15, ModuleOrigin, origin); | |
| 418 WCM_WEAK_LINK(16, WasmCompiledModule, next_instance); | |
| 419 WCM_WEAK_LINK(17, WasmCompiledModule, prev_instance); | |
| 420 WCM_WEAK_LINK(18, JSObject, owning_instance); | |
| 421 WCM_WEAK_LINK(19, JSObject, module_object); | |
| 422 | |
| 423 private: | |
| 424 static const int kTotalFieldCount = 20; | |
| 425 DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); | |
| 426 }; | |
| 427 | |
| 350 // Extract a function name from the given wasm object. | 428 // Extract a function name from the given wasm object. |
| 351 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a | 429 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a |
| 352 // valid UTF-8 string. | 430 // valid UTF-8 string. |
| 353 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 431 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
| 354 uint32_t func_index); | 432 uint32_t func_index); |
| 355 | 433 |
| 356 // Extract a function name from the given wasm object. | 434 // Extract a function name from the given wasm object. |
| 357 // Returns a null handle if the function is unnamed or the name is not a valid | 435 // Returns a null handle if the function is unnamed or the name is not a valid |
| 358 // UTF-8 string. | 436 // UTF-8 string. |
| 359 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, | 437 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 int instance_count); | 503 int instance_count); |
| 426 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); | 504 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); |
| 427 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); | 505 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); |
| 428 | 506 |
| 429 } // namespace testing | 507 } // namespace testing |
| 430 } // namespace wasm | 508 } // namespace wasm |
| 431 } // namespace internal | 509 } // namespace internal |
| 432 } // namespace v8 | 510 } // namespace v8 |
| 433 | 511 |
| 434 #endif // V8_WASM_MODULE_H_ | 512 #endif // V8_WASM_MODULE_H_ |
| OLD | NEW |