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 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 signature_tables(m->function_tables.size()), | 252 signature_tables(m->function_tables.size()), |
253 function_code(m->functions.size()) {} | 253 function_code(m->functions.size()) {} |
254 }; | 254 }; |
255 | 255 |
256 // Interface to the storage (wire bytes) of a wasm module. | 256 // Interface to the storage (wire bytes) of a wasm module. |
257 // It is illegal for anyone receiving a ModuleWireBytes to store pointers based | 257 // It is illegal for anyone receiving a ModuleWireBytes to store pointers based |
258 // on module_bytes, as this storage is only guaranteed to be alive as long as | 258 // on module_bytes, as this storage is only guaranteed to be alive as long as |
259 // this struct is alive. | 259 // this struct is alive. |
260 struct V8_EXPORT_PRIVATE ModuleWireBytes { | 260 struct V8_EXPORT_PRIVATE ModuleWireBytes { |
261 ModuleWireBytes(Vector<const byte> module_bytes) | 261 ModuleWireBytes(Vector<const byte> module_bytes) |
262 : module_bytes(module_bytes) {} | 262 : module_bytes_(module_bytes) {} |
263 ModuleWireBytes(const byte* start, const byte* end) | 263 ModuleWireBytes(const byte* start, const byte* end) |
264 : module_bytes(start, static_cast<int>(end - start)) { | 264 : module_bytes_(start, static_cast<int>(end - start)) { |
265 DCHECK_GE(kMaxInt, end - start); | 265 DCHECK_GE(kMaxInt, end - start); |
266 } | 266 } |
267 | 267 |
268 const Vector<const byte> module_bytes; | |
269 | |
270 // Get a string stored in the module bytes representing a name. | 268 // Get a string stored in the module bytes representing a name. |
271 WasmName GetName(uint32_t offset, uint32_t length) const { | 269 WasmName GetName(uint32_t offset, uint32_t length) const { |
272 if (length == 0) return {"<?>", 3}; // no name. | 270 if (length == 0) return {"<?>", 3}; // no name. |
273 CHECK(BoundsCheck(offset, length)); | 271 CHECK(BoundsCheck(offset, length)); |
274 DCHECK_GE(length, 0); | 272 DCHECK_GE(length, 0); |
275 return Vector<const char>::cast( | 273 return Vector<const char>::cast( |
276 module_bytes.SubVector(offset, offset + length)); | 274 module_bytes_.SubVector(offset, offset + length)); |
277 } | 275 } |
278 | 276 |
279 // Get a string stored in the module bytes representing a function name. | 277 // Get a string stored in the module bytes representing a function name. |
280 WasmName GetName(const WasmFunction* function) const { | 278 WasmName GetName(const WasmFunction* function) const { |
281 return GetName(function->name_offset, function->name_length); | 279 return GetName(function->name_offset, function->name_length); |
282 } | 280 } |
283 | 281 |
284 // Get a string stored in the module bytes representing a name. | 282 // Get a string stored in the module bytes representing a name. |
285 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { | 283 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { |
286 if (offset == 0 && length == 0) return {NULL, 0}; // no name. | 284 if (offset == 0 && length == 0) return {NULL, 0}; // no name. |
287 CHECK(BoundsCheck(offset, length)); | 285 CHECK(BoundsCheck(offset, length)); |
288 DCHECK_GE(length, 0); | 286 DCHECK_GE(length, 0); |
289 return Vector<const char>::cast( | 287 return Vector<const char>::cast( |
290 module_bytes.SubVector(offset, offset + length)); | 288 module_bytes_.SubVector(offset, offset + length)); |
291 } | 289 } |
292 | 290 |
293 // Get a string stored in the module bytes representing a function name. | 291 // Get a string stored in the module bytes representing a function name. |
294 WasmName GetNameOrNull(const WasmFunction* function) const { | 292 WasmName GetNameOrNull(const WasmFunction* function) const { |
295 return GetNameOrNull(function->name_offset, function->name_length); | 293 return GetNameOrNull(function->name_offset, function->name_length); |
296 } | 294 } |
297 | 295 |
298 // Checks the given offset range is contained within the module bytes. | 296 // Checks the given offset range is contained within the module bytes. |
299 bool BoundsCheck(uint32_t offset, uint32_t length) const { | 297 bool BoundsCheck(uint32_t offset, uint32_t length) const { |
300 uint32_t size = static_cast<uint32_t>(module_bytes.length()); | 298 uint32_t size = static_cast<uint32_t>(module_bytes_.length()); |
301 return offset <= size && length <= size - offset; | 299 return offset <= size && length <= size - offset; |
302 } | 300 } |
| 301 |
| 302 Vector<const byte> GetFunctionBytes(const WasmFunction* function) const { |
| 303 return module_bytes_.SubVector(function->code_start_offset, |
| 304 function->code_end_offset); |
| 305 } |
| 306 |
| 307 const byte* start() const { return module_bytes_.start(); } |
| 308 const byte* end() const { return module_bytes_.end(); } |
| 309 int length() const { return module_bytes_.length(); } |
| 310 |
| 311 private: |
| 312 const Vector<const byte> module_bytes_; |
303 }; | 313 }; |
304 | 314 |
305 // Interface provided to the decoder/graph builder which contains only | 315 // Interface provided to the decoder/graph builder which contains only |
306 // minimal information about the globals, functions, and function tables. | 316 // minimal information about the globals, functions, and function tables. |
307 struct V8_EXPORT_PRIVATE ModuleEnv { | 317 struct V8_EXPORT_PRIVATE ModuleEnv { |
308 ModuleEnv(const WasmModule* module, WasmInstance* instance) | 318 ModuleEnv(const WasmModule* module, WasmInstance* instance) |
309 : module(module), instance(instance) {} | 319 : module(module), instance(instance) {} |
310 | 320 |
311 const WasmModule* module; | 321 const WasmModule* module; |
312 WasmInstance* instance; | 322 WasmInstance* instance; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 359 |
350 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, | 360 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
351 FunctionSig* sig); | 361 FunctionSig* sig); |
352 static compiler::CallDescriptor* GetI32WasmCallDescriptor( | 362 static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
353 Zone* zone, compiler::CallDescriptor* descriptor); | 363 Zone* zone, compiler::CallDescriptor* descriptor); |
354 static compiler::CallDescriptor* GetI32WasmCallDescriptorForSimd( | 364 static compiler::CallDescriptor* GetI32WasmCallDescriptorForSimd( |
355 Zone* zone, compiler::CallDescriptor* descriptor); | 365 Zone* zone, compiler::CallDescriptor* descriptor); |
356 }; | 366 }; |
357 | 367 |
358 // A ModuleEnv together with ModuleWireBytes. | 368 // A ModuleEnv together with ModuleWireBytes. |
359 struct ModuleBytesEnv : public ModuleEnv, public ModuleWireBytes { | 369 struct ModuleBytesEnv { |
360 ModuleBytesEnv(const WasmModule* module, WasmInstance* instance, | 370 ModuleBytesEnv(const WasmModule* module, WasmInstance* instance, |
361 Vector<const byte> module_bytes) | 371 Vector<const byte> module_bytes) |
362 : ModuleEnv(module, instance), ModuleWireBytes(module_bytes) {} | 372 : module_env(module, instance), wire_bytes(module_bytes) {} |
363 ModuleBytesEnv(const WasmModule* module, WasmInstance* instance, | 373 ModuleBytesEnv(const WasmModule* module, WasmInstance* instance, |
364 const ModuleWireBytes& wire_bytes) | 374 const ModuleWireBytes& wire_bytes) |
365 : ModuleEnv(module, instance), ModuleWireBytes(wire_bytes) {} | 375 : module_env(module, instance), wire_bytes(wire_bytes) {} |
| 376 |
| 377 ModuleEnv module_env; |
| 378 ModuleWireBytes wire_bytes; |
366 }; | 379 }; |
367 | 380 |
368 // A helper for printing out the names of functions. | 381 // A helper for printing out the names of functions. |
369 struct WasmFunctionName { | 382 struct WasmFunctionName { |
370 WasmFunctionName(const WasmFunction* function, ModuleBytesEnv* module_env) | 383 WasmFunctionName(const WasmFunction* function, WasmName name) |
371 : function_(function), name_(module_env->GetNameOrNull(function)) {} | 384 : function_(function), name_(name) {} |
372 | 385 |
373 const WasmFunction* function_; | 386 const WasmFunction* function_; |
374 WasmName name_; | 387 WasmName name_; |
375 }; | 388 }; |
376 | 389 |
377 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 390 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
378 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 391 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
379 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 392 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
380 | 393 |
381 // Get the debug info associated with the given wasm object. | 394 // Get the debug info associated with the given wasm object. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); | 470 void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); |
458 void ValidateOrphanedInstance(Isolate* isolate, | 471 void ValidateOrphanedInstance(Isolate* isolate, |
459 Handle<WasmInstanceObject> instance); | 472 Handle<WasmInstanceObject> instance); |
460 | 473 |
461 } // namespace testing | 474 } // namespace testing |
462 } // namespace wasm | 475 } // namespace wasm |
463 } // namespace internal | 476 } // namespace internal |
464 } // namespace v8 | 477 } // namespace v8 |
465 | 478 |
466 #endif // V8_WASM_MODULE_H_ | 479 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |