| 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 "src/wasm/wasm-opcodes.h" | 8 #include "src/wasm/wasm-opcodes.h" |
| 9 #include "src/wasm/wasm-result.h" | 9 #include "src/wasm/wasm-result.h" |
| 10 | 10 |
| 11 #include "src/api.h" | 11 #include "src/api.h" |
| 12 #include "src/handles.h" | 12 #include "src/handles.h" |
| 13 | 13 |
| 14 namespace v8 { | 14 namespace v8 { |
| 15 namespace internal { | 15 namespace internal { |
| 16 | 16 |
| 17 namespace compiler { | 17 namespace compiler { |
| 18 class CallDescriptor; | 18 class CallDescriptor; |
| 19 class WasmCompilationUnit; | 19 class WasmCompilationUnit; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace wasm { | 22 namespace wasm { |
| 23 const size_t kMaxModuleSize = 1024 * 1024 * 1024; | 23 const size_t kMaxModuleSize = 1024 * 1024 * 1024; |
| 24 const size_t kMaxFunctionSize = 128 * 1024; | 24 const size_t kMaxFunctionSize = 128 * 1024; |
| 25 const size_t kMaxStringSize = 256; | 25 const size_t kMaxStringSize = 256; |
| 26 const uint32_t kWasmMagic = 0x6d736100; | 26 const uint32_t kWasmMagic = 0x6d736100; |
| 27 const uint32_t kWasmVersion = 0x0a; | 27 const uint32_t kWasmVersion = 0x0b; |
| 28 const uint8_t kWasmFunctionTypeForm = 0x40; |
| 28 | 29 |
| 29 // WebAssembly sections are named as strings in the binary format, but | 30 // WebAssembly sections are named as strings in the binary format, but |
| 30 // internally V8 uses an enum to handle them. | 31 // internally V8 uses an enum to handle them. |
| 31 // | 32 // |
| 32 // Entries have the form F(enumerator, order, string). | 33 // Entries have the form F(enumerator, string). |
| 33 #define FOR_EACH_WASM_SECTION_TYPE(F) \ | 34 #define FOR_EACH_WASM_SECTION_TYPE(F) \ |
| 34 F(Signatures, 1, "signatures") \ | 35 F(Signatures, 1, "type") \ |
| 35 F(ImportTable, 2, "import_table") \ | 36 F(ImportTable, 2, "import") \ |
| 36 F(FunctionSignatures, 3, "function_signatures") \ | 37 F(FunctionSignatures, 3, "function") \ |
| 37 F(FunctionTable, 4, "function_table") \ | 38 F(FunctionTable, 4, "table") \ |
| 38 F(Memory, 5, "memory") \ | 39 F(Memory, 5, "memory") \ |
| 39 F(ExportTable, 6, "export_table") \ | 40 F(ExportTable, 6, "export") \ |
| 40 F(StartFunction, 7, "start_function") \ | 41 F(StartFunction, 7, "start") \ |
| 41 F(FunctionBodies, 8, "function_bodies") \ | 42 F(FunctionBodies, 8, "code") \ |
| 42 F(DataSegments, 9, "data_segments") \ | 43 F(DataSegments, 9, "data") \ |
| 43 F(Names, 10, "names") \ | 44 F(Names, 10, "name") \ |
| 44 F(Globals, 0, "globals") \ | 45 F(OldFunctions, 0, "old_function") \ |
| 45 F(Functions, 0, "functions") \ | 46 F(Globals, 0, "global") \ |
| 46 F(End, 0, "end") | 47 F(End, 0, "end") |
| 47 | 48 |
| 48 // Contants for the above section types: {LEB128 length, characters...}. | 49 // Contants for the above section types: {LEB128 length, characters...}. |
| 49 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' | 50 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' |
| 50 #define WASM_SECTION_SIGNATURES \ | 51 #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' |
| 51 10, 's', 'i', 'g', 'n', 'a', 't', 'u', 'r', 'e', 's' | 52 #define WASM_SECTION_OLD_FUNCTIONS \ |
| 52 #define WASM_SECTION_FUNCTIONS 9, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', 's' | 53 12, 'o', 'l', 'd', '_', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
| 53 #define WASM_SECTION_GLOBALS 7, 'g', 'l', 'o', 'b', 'a', 'l', 's' | 54 #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' |
| 54 #define WASM_SECTION_DATA_SEGMENTS \ | 55 #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' |
| 55 13, 'd', 'a', 't', 'a', '_', 's', 'e', 'g', 'm', 'e', 'n', 't', 's' | 56 #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' |
| 56 #define WASM_SECTION_FUNCTION_TABLE \ | |
| 57 14, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', '_', 't', 'a', 'b', 'l', 'e' | |
| 58 #define WASM_SECTION_END 3, 'e', 'n', 'd' | 57 #define WASM_SECTION_END 3, 'e', 'n', 'd' |
| 59 #define WASM_SECTION_START_FUNCTION \ | 58 #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' |
| 60 14, 's', 't', 'a', 'r', 't', '_', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' | 59 #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' |
| 61 #define WASM_SECTION_IMPORT_TABLE \ | 60 #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' |
| 62 12, 'i', 'm', 'p', 'o', 'r', 't', '_', 't', 'a', 'b', 'l', 'e' | 61 #define WASM_SECTION_FUNCTION_SIGNATURES \ |
| 63 #define WASM_SECTION_EXPORT_TABLE \ | 62 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
| 64 12, 'e', 'x', 'p', 'o', 'r', 't', '_', 't', 'a', 'b', 'l', 'e' | 63 #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' |
| 65 #define WASM_SECTION_FUNCTION_SIGNATURES \ | 64 #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' |
| 66 19, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', '_', 's', 'i', 'g', 'n', 'a', \ | |
| 67 't', 'u', 'r', 'e', 's' | |
| 68 #define WASM_SECTION_FUNCTION_BODIES \ | |
| 69 15, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', '_', 'b', 'o', 'd', 'i', 'e', 's' | |
| 70 #define WASM_SECTION_NAMES 5, 'n', 'a', 'm', 'e', 's' | |
| 71 | 65 |
| 72 // Constants for the above section headers' size (LEB128 + characters). | 66 // Constants for the above section headers' size (LEB128 + characters). |
| 73 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) | 67 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) |
| 74 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)11) | 68 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) |
| 75 #define WASM_SECTION_FUNCTIONS_SIZE ((size_t)10) | 69 #define WASM_SECTION_OLD_FUNCTIONS_SIZE ((size_t)13) |
| 76 #define WASM_SECTION_GLOBALS_SIZE ((size_t)8) | 70 #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) |
| 77 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)14) | 71 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) |
| 78 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)15) | 72 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) |
| 79 #define WASM_SECTION_END_SIZE ((size_t)4) | 73 #define WASM_SECTION_END_SIZE ((size_t)4) |
| 80 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)15) | 74 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) |
| 81 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)13) | 75 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) |
| 82 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)13) | 76 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) |
| 83 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)20) | 77 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) |
| 84 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)16) | 78 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) |
| 85 #define WASM_SECTION_NAMES_SIZE ((size_t)6) | 79 #define WASM_SECTION_NAMES_SIZE ((size_t)5) |
| 86 | 80 |
| 87 struct WasmSection { | 81 struct WasmSection { |
| 88 enum class Code : uint32_t { | 82 enum class Code : uint32_t { |
| 89 #define F(enumerator, order, string) enumerator, | 83 #define F(enumerator, order, string) enumerator, |
| 90 FOR_EACH_WASM_SECTION_TYPE(F) | 84 FOR_EACH_WASM_SECTION_TYPE(F) |
| 91 #undef F | 85 #undef F |
| 92 Max | 86 Max |
| 93 }; | 87 }; |
| 94 static WasmSection::Code begin(); | 88 static WasmSection::Code begin(); |
| 95 static WasmSection::Code end(); | 89 static WasmSection::Code end(); |
| 96 static WasmSection::Code next(WasmSection::Code code); | 90 static WasmSection::Code next(WasmSection::Code code); |
| 97 static const char* getName(Code code); | 91 static const char* getName(Code code); |
| 98 static int getOrder(Code code); | 92 static int getOrder(Code code); |
| 99 static size_t getNameLength(Code code); | 93 static size_t getNameLength(Code code); |
| 100 static WasmSection::Code lookup(const byte* string, uint32_t length); | 94 static WasmSection::Code lookup(const byte* string, uint32_t length); |
| 101 }; | 95 }; |
| 102 | 96 |
| 103 enum WasmFunctionDeclBit { | 97 enum WasmFunctionDeclBit { |
| 104 kDeclFunctionName = 0x01, | 98 kDeclFunctionName = 0x01, |
| 105 kDeclFunctionImport = 0x02, | 99 kDeclFunctionImport = 0x02, |
| 106 kDeclFunctionLocals = 0x04, | 100 kDeclFunctionLocals = 0x04, |
| 107 kDeclFunctionExport = 0x08 | 101 kDeclFunctionExport = 0x08 |
| 108 }; | 102 }; |
| 109 | 103 |
| 110 // Constants for fixed-size elements within a module. | 104 // Constants for fixed-size elements within a module. |
| 111 static const size_t kDeclMemorySize = 3; | 105 static const size_t kDeclMemorySize = 3; |
| 112 static const size_t kDeclDataSegmentSize = 13; | 106 static const size_t kDeclDataSegmentSize = 13; |
| 113 | 107 |
| 108 static const uint32_t kMaxReturnCount = 1; |
| 109 |
| 114 // Static representation of a WASM function. | 110 // Static representation of a WASM function. |
| 115 struct WasmFunction { | 111 struct WasmFunction { |
| 116 FunctionSig* sig; // signature of the function. | 112 FunctionSig* sig; // signature of the function. |
| 117 uint32_t func_index; // index into the function table. | 113 uint32_t func_index; // index into the function table. |
| 118 uint32_t sig_index; // index into the signature table. | 114 uint32_t sig_index; // index into the signature table. |
| 119 uint32_t name_offset; // offset in the module bytes of the name, if any. | 115 uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 120 uint32_t name_length; // length in bytes of the name. | 116 uint32_t name_length; // length in bytes of the name. |
| 121 uint32_t code_start_offset; // offset in the module bytes of code start. | 117 uint32_t code_start_offset; // offset in the module bytes of code start. |
| 122 uint32_t code_end_offset; // offset in the module bytes of code end. | 118 uint32_t code_end_offset; // offset in the module bytes of code end. |
| 123 uint16_t local_i32_count; // number of i32 local variables. | 119 uint16_t local_i32_count; // number of i32 local variables. |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 319 |
| 324 // For testing. Decode, verify, and run the last exported function in the | 320 // For testing. Decode, verify, and run the last exported function in the |
| 325 // given decoded module. | 321 // given decoded module. |
| 326 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); | 322 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
| 327 | 323 |
| 328 } // namespace wasm | 324 } // namespace wasm |
| 329 } // namespace internal | 325 } // namespace internal |
| 330 } // namespace v8 | 326 } // namespace v8 |
| 331 | 327 |
| 332 #endif // V8_WASM_MODULE_H_ | 328 #endif // V8_WASM_MODULE_H_ |
| OLD | NEW |