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 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 enum WasmFunctionDeclBit { | 51 enum WasmFunctionDeclBit { |
52 kDeclFunctionName = 0x01, | 52 kDeclFunctionName = 0x01, |
53 kDeclFunctionImport = 0x02, | 53 kDeclFunctionImport = 0x02, |
54 kDeclFunctionLocals = 0x04, | 54 kDeclFunctionLocals = 0x04, |
55 kDeclFunctionExport = 0x08 | 55 kDeclFunctionExport = 0x08 |
56 }; | 56 }; |
57 | 57 |
58 // Constants for fixed-size elements within a module. | 58 // Constants for fixed-size elements within a module. |
59 static const size_t kDeclMemorySize = 3; | 59 static const size_t kDeclMemorySize = 3; |
60 static const size_t kDeclGlobalSize = 6; | |
61 static const size_t kDeclDataSegmentSize = 13; | 60 static const size_t kDeclDataSegmentSize = 13; |
62 | 61 |
63 // Static representation of a WASM function. | 62 // Static representation of a WASM function. |
64 struct WasmFunction { | 63 struct WasmFunction { |
65 FunctionSig* sig; // signature of the function. | 64 FunctionSig* sig; // signature of the function. |
66 uint32_t func_index; // index into the function table. | 65 uint32_t func_index; // index into the function table. |
67 uint32_t sig_index; // index into the signature table. | 66 uint32_t sig_index; // index into the signature table. |
68 uint32_t name_offset; // offset in the module bytes of the name, if any. | 67 uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 68 uint32_t name_length; // length in bytes of the name. |
69 uint32_t code_start_offset; // offset in the module bytes of code start. | 69 uint32_t code_start_offset; // offset in the module bytes of code start. |
70 uint32_t code_end_offset; // offset in the module bytes of code end. | 70 uint32_t code_end_offset; // offset in the module bytes of code end. |
71 uint16_t local_i32_count; // number of i32 local variables. | 71 uint16_t local_i32_count; // number of i32 local variables. |
72 uint16_t local_i64_count; // number of i64 local variables. | 72 uint16_t local_i64_count; // number of i64 local variables. |
73 uint16_t local_f32_count; // number of f32 local variables. | 73 uint16_t local_f32_count; // number of f32 local variables. |
74 uint16_t local_f64_count; // number of f64 local variables. | 74 uint16_t local_f64_count; // number of f64 local variables. |
75 bool exported; // true if this function is exported. | 75 bool exported; // true if this function is exported. |
76 bool external; // true if this function is externally supplied. | 76 bool external; // true if this function is externally supplied. |
77 }; | 77 }; |
78 | 78 |
79 // Static representation of an imported WASM function. | 79 // Static representation of an imported WASM function. |
80 struct WasmImport { | 80 struct WasmImport { |
81 FunctionSig* sig; // signature of the function. | 81 FunctionSig* sig; // signature of the function. |
82 uint32_t sig_index; // index into the signature table. | 82 uint32_t sig_index; // index into the signature table. |
83 uint32_t module_name_offset; // offset in module bytes of the module name. | 83 uint32_t module_name_offset; // offset in module bytes of the module name. |
| 84 uint32_t module_name_length; // length in bytes of the module name. |
84 uint32_t function_name_offset; // offset in module bytes of the import name. | 85 uint32_t function_name_offset; // offset in module bytes of the import name. |
| 86 uint32_t function_name_length; // length in bytes of the import name. |
85 }; | 87 }; |
86 | 88 |
87 // Static representation of an exported WASM function. | 89 // Static representation of an exported WASM function. |
88 struct WasmExport { | 90 struct WasmExport { |
89 uint32_t func_index; // index into the function table. | 91 uint32_t func_index; // index into the function table. |
90 uint32_t name_offset; // offset in module bytes of the name to export. | 92 uint32_t name_offset; // offset in module bytes of the name to export. |
| 93 uint32_t name_length; // length in bytes of the exported name. |
91 }; | 94 }; |
92 | 95 |
93 // Static representation of a wasm global variable. | 96 // Static representation of a wasm global variable. |
94 struct WasmGlobal { | 97 struct WasmGlobal { |
95 uint32_t name_offset; // offset in the module bytes of the name, if any. | 98 uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 99 uint32_t name_length; // length in bytes of the global name. |
96 MachineType type; // type of the global. | 100 MachineType type; // type of the global. |
97 uint32_t offset; // offset from beginning of globals area. | 101 uint32_t offset; // offset from beginning of globals area. |
98 bool exported; // true if this global is exported. | 102 bool exported; // true if this global is exported. |
99 }; | 103 }; |
100 | 104 |
101 // Static representation of a wasm data segment. | 105 // Static representation of a wasm data segment. |
102 struct WasmDataSegment { | 106 struct WasmDataSegment { |
103 uint32_t dest_addr; // destination memory address of the data. | 107 uint32_t dest_addr; // destination memory address of the data. |
104 uint32_t source_offset; // start offset in the module bytes. | 108 uint32_t source_offset; // start offset in the module bytes. |
105 uint32_t source_size; // end offset in the module bytes. | 109 uint32_t source_size; // end offset in the module bytes. |
(...skipping 21 matching lines...) Expand all Loading... |
127 std::vector<WasmGlobal> globals; // globals in this module. | 131 std::vector<WasmGlobal> globals; // globals in this module. |
128 std::vector<FunctionSig*> signatures; // signatures in this module. | 132 std::vector<FunctionSig*> signatures; // signatures in this module. |
129 std::vector<WasmFunction> functions; // functions in this module. | 133 std::vector<WasmFunction> functions; // functions in this module. |
130 std::vector<WasmDataSegment> data_segments; // data segments in this module. | 134 std::vector<WasmDataSegment> data_segments; // data segments in this module. |
131 std::vector<uint16_t> function_table; // function table. | 135 std::vector<uint16_t> function_table; // function table. |
132 std::vector<WasmImport> import_table; // import table. | 136 std::vector<WasmImport> import_table; // import table. |
133 std::vector<WasmExport> export_table; // export table. | 137 std::vector<WasmExport> export_table; // export table. |
134 | 138 |
135 WasmModule(); | 139 WasmModule(); |
136 | 140 |
137 // Get a pointer to a string stored in the module bytes representing a name. | 141 // Get a string stored in the module bytes representing a name. |
138 const char* GetName(uint32_t offset) const { | 142 WasmName GetName(uint32_t offset, uint32_t length) const { |
139 if (offset == 0) return "<?>"; // no name. | 143 if (length == 0) return {"<?>", 3}; // no name. |
140 CHECK(BoundsCheck(offset, offset + 1)); | 144 CHECK(BoundsCheck(offset, offset + length)); |
141 return reinterpret_cast<const char*>(module_start + offset); | 145 return {reinterpret_cast<const char*>(module_start + offset), length}; |
142 } | 146 } |
143 | 147 |
144 // Get a pointer to a string stored in the module bytes representing a name. | 148 // Get a string stored in the module bytes representing a name. |
145 const char* GetNameOrNull(uint32_t offset) const { | 149 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { |
146 if (offset == 0) return nullptr; // no name. | 150 if (length == 0) return {NULL, 0}; // no name. |
147 CHECK(BoundsCheck(offset, offset + 1)); | 151 CHECK(BoundsCheck(offset, offset + length)); |
148 return reinterpret_cast<const char*>(module_start + offset); | 152 return {reinterpret_cast<const char*>(module_start + offset), length}; |
149 } | 153 } |
150 | 154 |
151 // Checks the given offset range is contained within the module bytes. | 155 // Checks the given offset range is contained within the module bytes. |
152 bool BoundsCheck(uint32_t start, uint32_t end) const { | 156 bool BoundsCheck(uint32_t start, uint32_t end) const { |
153 size_t size = module_end - module_start; | 157 size_t size = module_end - module_start; |
154 return start < size && end < size; | 158 return start < size && end < size; |
155 } | 159 } |
156 | 160 |
157 // Creates a new instantiation of the module in the given isolate. | 161 // Creates a new instantiation of the module in the given isolate. |
158 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSObject> ffi, | 162 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSObject> ffi, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 267 |
264 // For testing. Decode, verify, and run the last exported function in the | 268 // For testing. Decode, verify, and run the last exported function in the |
265 // given decoded module. | 269 // given decoded module. |
266 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); | 270 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
267 | 271 |
268 } // namespace wasm | 272 } // namespace wasm |
269 } // namespace internal | 273 } // namespace internal |
270 } // namespace v8 | 274 } // namespace v8 |
271 | 275 |
272 #endif // V8_WASM_MODULE_H_ | 276 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |