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 12 matching lines...) Expand all Loading... |
23 const size_t kMaxFunctionSize = 128 * 1024; | 23 const size_t kMaxFunctionSize = 128 * 1024; |
24 const size_t kMaxStringSize = 256; | 24 const size_t kMaxStringSize = 256; |
25 | 25 |
26 enum WasmSectionDeclCode { | 26 enum WasmSectionDeclCode { |
27 kDeclMemory = 0x00, | 27 kDeclMemory = 0x00, |
28 kDeclSignatures = 0x01, | 28 kDeclSignatures = 0x01, |
29 kDeclFunctions = 0x02, | 29 kDeclFunctions = 0x02, |
30 kDeclGlobals = 0x03, | 30 kDeclGlobals = 0x03, |
31 kDeclDataSegments = 0x04, | 31 kDeclDataSegments = 0x04, |
32 kDeclFunctionTable = 0x05, | 32 kDeclFunctionTable = 0x05, |
33 kDeclWLL = 0x11, | |
34 kDeclEnd = 0x06, | 33 kDeclEnd = 0x06, |
35 kDeclStartFunction = 0x07, | 34 kDeclStartFunction = 0x07, |
| 35 kDeclImportTable = 0x08, |
| 36 kDeclWLL = 0x11, |
36 }; | 37 }; |
37 | 38 |
38 static const int kMaxModuleSectionCode = 6; | 39 static const int kMaxModuleSectionCode = 0x11; |
39 | 40 |
40 enum WasmFunctionDeclBit { | 41 enum WasmFunctionDeclBit { |
41 kDeclFunctionName = 0x01, | 42 kDeclFunctionName = 0x01, |
42 kDeclFunctionImport = 0x02, | 43 kDeclFunctionImport = 0x02, |
43 kDeclFunctionLocals = 0x04, | 44 kDeclFunctionLocals = 0x04, |
44 kDeclFunctionExport = 0x08 | 45 kDeclFunctionExport = 0x08 |
45 }; | 46 }; |
46 | 47 |
47 // Constants for fixed-size elements within a module. | 48 // Constants for fixed-size elements within a module. |
48 static const size_t kDeclMemorySize = 3; | 49 static const size_t kDeclMemorySize = 3; |
49 static const size_t kDeclGlobalSize = 6; | 50 static const size_t kDeclGlobalSize = 6; |
50 static const size_t kDeclDataSegmentSize = 13; | 51 static const size_t kDeclDataSegmentSize = 13; |
51 | 52 |
52 // Static representation of a wasm function. | 53 // Static representation of a WASM function. |
53 struct WasmFunction { | 54 struct WasmFunction { |
54 FunctionSig* sig; // signature of the function. | 55 FunctionSig* sig; // signature of the function. |
55 uint32_t func_index; // index into the function table. | 56 uint32_t func_index; // index into the function table. |
56 uint16_t sig_index; // index into the signature table. | 57 uint16_t sig_index; // index into the signature table. |
57 uint32_t name_offset; // offset in the module bytes of the name, if any. | 58 uint32_t name_offset; // offset in the module bytes of the name, if any. |
58 uint32_t code_start_offset; // offset in the module bytes of code start. | 59 uint32_t code_start_offset; // offset in the module bytes of code start. |
59 uint32_t code_end_offset; // offset in the module bytes of code end. | 60 uint32_t code_end_offset; // offset in the module bytes of code end. |
60 uint16_t local_i32_count; // number of i32 local variables. | 61 uint16_t local_i32_count; // number of i32 local variables. |
61 uint16_t local_i64_count; // number of i64 local variables. | 62 uint16_t local_i64_count; // number of i64 local variables. |
62 uint16_t local_f32_count; // number of f32 local variables. | 63 uint16_t local_f32_count; // number of f32 local variables. |
63 uint16_t local_f64_count; // number of f64 local variables. | 64 uint16_t local_f64_count; // number of f64 local variables. |
64 bool exported; // true if this function is exported. | 65 bool exported; // true if this function is exported. |
65 bool external; // true if this function is externally supplied. | 66 bool external; // true if this function is externally supplied. |
66 }; | 67 }; |
67 | 68 |
68 struct ModuleEnv; // forward declaration of decoder interface. | 69 // Static representation of an imported WASM function. |
| 70 struct WasmImport { |
| 71 FunctionSig* sig; // signature of the function. |
| 72 uint16_t sig_index; // index into the signature table. |
| 73 uint32_t module_name_offset; // offset in module bytes of the module name. |
| 74 uint32_t function_name_offset; // offset in module bytes of the import name. |
| 75 }; |
69 | 76 |
70 // Static representation of a wasm global variable. | 77 // Static representation of a wasm global variable. |
71 struct WasmGlobal { | 78 struct WasmGlobal { |
72 uint32_t name_offset; // offset in the module bytes of the name, if any. | 79 uint32_t name_offset; // offset in the module bytes of the name, if any. |
73 MachineType type; // type of the global. | 80 MachineType type; // type of the global. |
74 uint32_t offset; // offset from beginning of globals area. | 81 uint32_t offset; // offset from beginning of globals area. |
75 bool exported; // true if this global is exported. | 82 bool exported; // true if this global is exported. |
76 }; | 83 }; |
77 | 84 |
78 // Static representation of a wasm data segment. | 85 // Static representation of a wasm data segment. |
(...skipping 16 matching lines...) Expand all Loading... |
95 uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). | 102 uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). |
96 bool mem_export; // true if the memory is exported. | 103 bool mem_export; // true if the memory is exported. |
97 bool mem_external; // true if the memory is external. | 104 bool mem_external; // true if the memory is external. |
98 int start_function_index; // start function, if any. | 105 int start_function_index; // start function, if any. |
99 | 106 |
100 std::vector<WasmGlobal>* globals; // globals in this module. | 107 std::vector<WasmGlobal>* globals; // globals in this module. |
101 std::vector<FunctionSig*>* signatures; // signatures in this module. | 108 std::vector<FunctionSig*>* signatures; // signatures in this module. |
102 std::vector<WasmFunction>* functions; // functions in this module. | 109 std::vector<WasmFunction>* functions; // functions in this module. |
103 std::vector<WasmDataSegment>* data_segments; // data segments in this module. | 110 std::vector<WasmDataSegment>* data_segments; // data segments in this module. |
104 std::vector<uint16_t>* function_table; // function table. | 111 std::vector<uint16_t>* function_table; // function table. |
| 112 std::vector<WasmImport>* import_table; // import table. |
105 | 113 |
106 WasmModule(); | 114 WasmModule(); |
107 ~WasmModule(); | 115 ~WasmModule(); |
108 | 116 |
109 // Get a pointer to a string stored in the module bytes representing a name. | 117 // Get a pointer to a string stored in the module bytes representing a name. |
110 const char* GetName(uint32_t offset) const { | 118 const char* GetName(uint32_t offset) const { |
111 if (offset == 0) return "<?>"; // no name. | 119 if (offset == 0) return "<?>"; // no name. |
112 CHECK(BoundsCheck(offset, offset + 1)); | 120 CHECK(BoundsCheck(offset, offset + 1)); |
113 return reinterpret_cast<const char*>(module_start + offset); | 121 return reinterpret_cast<const char*>(module_start + offset); |
114 } | 122 } |
(...skipping 12 matching lines...) Expand all Loading... |
127 // An instantiated WASM module, including memory, function table, etc. | 135 // An instantiated WASM module, including memory, function table, etc. |
128 struct WasmModuleInstance { | 136 struct WasmModuleInstance { |
129 WasmModule* module; // static representation of the module. | 137 WasmModule* module; // static representation of the module. |
130 // -- Heap allocated -------------------------------------------------------- | 138 // -- Heap allocated -------------------------------------------------------- |
131 Handle<JSObject> js_object; // JavaScript module object. | 139 Handle<JSObject> js_object; // JavaScript module object. |
132 Handle<Context> context; // JavaScript native context. | 140 Handle<Context> context; // JavaScript native context. |
133 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. | 141 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
134 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. | 142 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
135 Handle<FixedArray> function_table; // indirect function table. | 143 Handle<FixedArray> function_table; // indirect function table. |
136 std::vector<Handle<Code>>* function_code; // code objects for each function. | 144 std::vector<Handle<Code>>* function_code; // code objects for each function. |
| 145 std::vector<Handle<Code>>* import_code; // code objects for each import. |
137 // -- raw memory ------------------------------------------------------------ | 146 // -- raw memory ------------------------------------------------------------ |
138 byte* mem_start; // start of linear memory. | 147 byte* mem_start; // start of linear memory. |
139 size_t mem_size; // size of the linear memory. | 148 size_t mem_size; // size of the linear memory. |
140 // -- raw globals ----------------------------------------------------------- | 149 // -- raw globals ----------------------------------------------------------- |
141 byte* globals_start; // start of the globals area. | 150 byte* globals_start; // start of the globals area. |
142 size_t globals_size; // size of the globals area. | 151 size_t globals_size; // size of the globals area. |
143 | 152 |
144 explicit WasmModuleInstance(WasmModule* m) | 153 explicit WasmModuleInstance(WasmModule* m) |
145 : module(m), | 154 : module(m), |
146 function_code(nullptr), | 155 function_code(nullptr), |
(...skipping 16 matching lines...) Expand all Loading... |
163 | 172 |
164 bool IsValidGlobal(uint32_t index) { | 173 bool IsValidGlobal(uint32_t index) { |
165 return module && index < module->globals->size(); | 174 return module && index < module->globals->size(); |
166 } | 175 } |
167 bool IsValidFunction(uint32_t index) { | 176 bool IsValidFunction(uint32_t index) { |
168 return module && index < module->functions->size(); | 177 return module && index < module->functions->size(); |
169 } | 178 } |
170 bool IsValidSignature(uint32_t index) { | 179 bool IsValidSignature(uint32_t index) { |
171 return module && index < module->signatures->size(); | 180 return module && index < module->signatures->size(); |
172 } | 181 } |
| 182 bool IsValidImport(uint32_t index) { |
| 183 return module && index < module->import_table->size(); |
| 184 } |
173 MachineType GetGlobalType(uint32_t index) { | 185 MachineType GetGlobalType(uint32_t index) { |
174 DCHECK(IsValidGlobal(index)); | 186 DCHECK(IsValidGlobal(index)); |
175 return module->globals->at(index).type; | 187 return module->globals->at(index).type; |
176 } | 188 } |
177 FunctionSig* GetFunctionSignature(uint32_t index) { | 189 FunctionSig* GetFunctionSignature(uint32_t index) { |
178 DCHECK(IsValidFunction(index)); | 190 DCHECK(IsValidFunction(index)); |
179 return module->functions->at(index).sig; | 191 return module->functions->at(index).sig; |
180 } | 192 } |
| 193 FunctionSig* GetImportSignature(uint32_t index) { |
| 194 DCHECK(IsValidImport(index)); |
| 195 return module->import_table->at(index).sig; |
| 196 } |
181 FunctionSig* GetSignature(uint32_t index) { | 197 FunctionSig* GetSignature(uint32_t index) { |
182 DCHECK(IsValidSignature(index)); | 198 DCHECK(IsValidSignature(index)); |
183 return module->signatures->at(index); | 199 return module->signatures->at(index); |
184 } | 200 } |
185 size_t FunctionTableSize() { | 201 size_t FunctionTableSize() { |
186 return module && module->function_table ? module->function_table->size() | 202 return module && module->function_table ? module->function_table->size() |
187 : 0; | 203 : 0; |
188 } | 204 } |
189 | 205 |
190 Handle<Code> GetFunctionCode(uint32_t index); | 206 Handle<Code> GetFunctionCode(uint32_t index); |
| 207 Handle<Code> GetImportCode(uint32_t index); |
191 Handle<FixedArray> GetFunctionTable(); | 208 Handle<FixedArray> GetFunctionTable(); |
192 | 209 |
193 compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, FunctionSig* sig); | 210 compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, FunctionSig* sig); |
194 static compiler::CallDescriptor* GetI32WasmCallDescriptor( | 211 static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
195 Zone* zone, compiler::CallDescriptor* descriptor); | 212 Zone* zone, compiler::CallDescriptor* descriptor); |
196 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); | 213 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
197 }; | 214 }; |
198 | 215 |
199 // A helper for printing out the names of functions. | 216 // A helper for printing out the names of functions. |
200 struct WasmFunctionName { | 217 struct WasmFunctionName { |
(...skipping 17 matching lines...) Expand all Loading... |
218 | 235 |
219 // For testing. Decode, verify, and run the last exported function in the | 236 // For testing. Decode, verify, and run the last exported function in the |
220 // given decoded module. | 237 // given decoded module. |
221 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); | 238 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
222 | 239 |
223 } // namespace wasm | 240 } // namespace wasm |
224 } // namespace internal | 241 } // namespace internal |
225 } // namespace v8 | 242 } // namespace v8 |
226 | 243 |
227 #endif // V8_WASM_MODULE_H_ | 244 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |