Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_MODULE_H_ |
| 6 | #define V8_WASM_MODULE_H_ |
| 7 | |
| 8 | #include "src/wasm/wasm-opcodes.h" |
| 9 | #include "src/wasm/wasm-result.h" |
| 10 | |
| 11 | #include "src/api.h" |
| 12 | #include "src/handles.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
| 17 | namespace compiler { |
| 18 | class CallDescriptor; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 19 | class WasmCompilationUnit; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | namespace wasm { |
| 23 | const size_t kMaxModuleSize = 1024 * 1024 * 1024; |
| 24 | const size_t kMaxFunctionSize = 128 * 1024; |
| 25 | const size_t kMaxStringSize = 256; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 26 | const uint32_t kWasmMagic = 0x6d736100; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 27 | const uint32_t kWasmVersion = 0x0b; |
| 28 | const uint8_t kWasmFunctionTypeForm = 0x40; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 29 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 30 | // WebAssembly sections are named as strings in the binary format, but |
| 31 | // internally V8 uses an enum to handle them. |
| 32 | // |
| 33 | // Entries have the form F(enumerator, string). |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 34 | #define FOR_EACH_WASM_SECTION_TYPE(F) \ |
| 35 | F(Signatures, 1, "type") \ |
| 36 | F(ImportTable, 2, "import") \ |
| 37 | F(FunctionSignatures, 3, "function") \ |
| 38 | F(FunctionTable, 4, "table") \ |
| 39 | F(Memory, 5, "memory") \ |
| 40 | F(ExportTable, 6, "export") \ |
| 41 | F(StartFunction, 7, "start") \ |
| 42 | F(FunctionBodies, 8, "code") \ |
| 43 | F(DataSegments, 9, "data") \ |
| 44 | F(Names, 10, "name") \ |
| 45 | F(OldFunctions, 0, "old_function") \ |
| 46 | F(Globals, 0, "global") \ |
| 47 | F(End, 0, "end") |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 48 | |
| 49 | // Contants for the above section types: {LEB128 length, characters...}. |
| 50 | #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 51 | #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' |
| 52 | #define WASM_SECTION_OLD_FUNCTIONS \ |
| 53 | 12, 'o', 'l', 'd', '_', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
| 54 | #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' |
| 55 | #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' |
| 56 | #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 57 | #define WASM_SECTION_END 3, 'e', 'n', 'd' |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 58 | #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' |
| 59 | #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' |
| 60 | #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' |
| 61 | #define WASM_SECTION_FUNCTION_SIGNATURES \ |
| 62 | 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
| 63 | #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' |
| 64 | #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 65 | |
| 66 | // Constants for the above section headers' size (LEB128 + characters). |
| 67 | #define WASM_SECTION_MEMORY_SIZE ((size_t)7) |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 68 | #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) |
| 69 | #define WASM_SECTION_OLD_FUNCTIONS_SIZE ((size_t)13) |
| 70 | #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) |
| 71 | #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) |
| 72 | #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 73 | #define WASM_SECTION_END_SIZE ((size_t)4) |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 74 | #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) |
| 75 | #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) |
| 76 | #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) |
| 77 | #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) |
| 78 | #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) |
| 79 | #define WASM_SECTION_NAMES_SIZE ((size_t)5) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 80 | |
| 81 | struct WasmSection { |
| 82 | enum class Code : uint32_t { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 83 | #define F(enumerator, order, string) enumerator, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 84 | FOR_EACH_WASM_SECTION_TYPE(F) |
| 85 | #undef F |
| 86 | Max |
| 87 | }; |
| 88 | static WasmSection::Code begin(); |
| 89 | static WasmSection::Code end(); |
| 90 | static WasmSection::Code next(WasmSection::Code code); |
| 91 | static const char* getName(Code code); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 92 | static int getOrder(Code code); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 93 | static size_t getNameLength(Code code); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 94 | static WasmSection::Code lookup(const byte* string, uint32_t length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | enum WasmFunctionDeclBit { |
| 98 | kDeclFunctionName = 0x01, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | kDeclFunctionExport = 0x08 |
| 100 | }; |
| 101 | |
| 102 | // Constants for fixed-size elements within a module. |
| 103 | static const size_t kDeclMemorySize = 3; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 104 | static const size_t kDeclDataSegmentSize = 13; |
| 105 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 106 | static const uint32_t kMaxReturnCount = 1; |
| 107 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 108 | // Static representation of a WASM function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 109 | struct WasmFunction { |
| 110 | FunctionSig* sig; // signature of the function. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 111 | uint32_t func_index; // index into the function table. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 112 | uint32_t sig_index; // index into the signature table. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 114 | uint32_t name_length; // length in bytes of the name. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | uint32_t code_start_offset; // offset in the module bytes of code start. |
| 116 | uint32_t code_end_offset; // offset in the module bytes of code end. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | bool exported; // true if this function is exported. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 120 | // Static representation of an imported WASM function. |
| 121 | struct WasmImport { |
| 122 | FunctionSig* sig; // signature of the function. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 123 | uint32_t sig_index; // index into the signature table. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 124 | uint32_t module_name_offset; // offset in module bytes of the module name. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 125 | uint32_t module_name_length; // length in bytes of the module name. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 126 | uint32_t function_name_offset; // offset in module bytes of the import name. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 127 | uint32_t function_name_length; // length in bytes of the import name. |
| 128 | }; |
| 129 | |
| 130 | // Static representation of an exported WASM function. |
| 131 | struct WasmExport { |
| 132 | uint32_t func_index; // index into the function table. |
| 133 | uint32_t name_offset; // offset in module bytes of the name to export. |
| 134 | uint32_t name_length; // length in bytes of the exported name. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 135 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 | |
| 137 | // Static representation of a wasm global variable. |
| 138 | struct WasmGlobal { |
| 139 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 140 | uint32_t name_length; // length in bytes of the global name. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | MachineType type; // type of the global. |
| 142 | uint32_t offset; // offset from beginning of globals area. |
| 143 | bool exported; // true if this global is exported. |
| 144 | }; |
| 145 | |
| 146 | // Static representation of a wasm data segment. |
| 147 | struct WasmDataSegment { |
| 148 | uint32_t dest_addr; // destination memory address of the data. |
| 149 | uint32_t source_offset; // start offset in the module bytes. |
| 150 | uint32_t source_size; // end offset in the module bytes. |
| 151 | bool init; // true if loaded upon instantiation. |
| 152 | }; |
| 153 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 154 | enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; |
| 155 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | // Static representation of a module. |
| 157 | struct WasmModule { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 158 | static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
| 159 | static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
| 160 | static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 161 | |
| 162 | Isolate* shared_isolate; // isolate for storing shared code. |
| 163 | const byte* module_start; // starting address for the module bytes. |
| 164 | const byte* module_end; // end address for the module bytes. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 165 | uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
| 166 | uint32_t max_mem_pages; // maximum size of the memory in 64k pages. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 167 | bool mem_export; // true if the memory is exported. |
| 168 | bool mem_external; // true if the memory is external. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 169 | int start_function_index; // start function, if any. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 170 | ModuleOrigin origin; // origin of the module |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 171 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 172 | std::vector<WasmGlobal> globals; // globals in this module. |
| 173 | std::vector<FunctionSig*> signatures; // signatures in this module. |
| 174 | std::vector<WasmFunction> functions; // functions in this module. |
| 175 | std::vector<WasmDataSegment> data_segments; // data segments in this module. |
| 176 | std::vector<uint16_t> function_table; // function table. |
| 177 | std::vector<WasmImport> import_table; // import table. |
| 178 | std::vector<WasmExport> export_table; // export table. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 179 | |
| 180 | WasmModule(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 182 | // Get a string stored in the module bytes representing a name. |
| 183 | WasmName GetName(uint32_t offset, uint32_t length) const { |
| 184 | if (length == 0) return {"<?>", 3}; // no name. |
| 185 | CHECK(BoundsCheck(offset, offset + length)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 186 | DCHECK_GE(static_cast<int>(length), 0); |
| 187 | return {reinterpret_cast<const char*>(module_start + offset), |
| 188 | static_cast<int>(length)}; |
| 189 | } |
| 190 | |
| 191 | // Get a string stored in the module bytes representing a function name. |
| 192 | WasmName GetName(WasmFunction* function) const { |
| 193 | return GetName(function->name_offset, function->name_length); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Get a string stored in the module bytes representing a name. |
| 197 | WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { |
| 198 | if (length == 0) return {NULL, 0}; // no name. |
| 199 | CHECK(BoundsCheck(offset, offset + length)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 200 | DCHECK_GE(static_cast<int>(length), 0); |
| 201 | return {reinterpret_cast<const char*>(module_start + offset), |
| 202 | static_cast<int>(length)}; |
| 203 | } |
| 204 | |
| 205 | // Get a string stored in the module bytes representing a function name. |
| 206 | WasmName GetNameOrNull(WasmFunction* function) const { |
| 207 | return GetNameOrNull(function->name_offset, function->name_length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Checks the given offset range is contained within the module bytes. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 211 | bool BoundsCheck(uint32_t start, uint32_t end) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 212 | size_t size = module_end - module_start; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 213 | return start <= size && end <= size; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Creates a new instantiation of the module in the given isolate. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 217 | MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | Handle<JSArrayBuffer> memory); |
| 219 | }; |
| 220 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 221 | // An instantiated WASM module, including memory, function table, etc. |
| 222 | struct WasmModuleInstance { |
| 223 | WasmModule* module; // static representation of the module. |
| 224 | // -- Heap allocated -------------------------------------------------------- |
| 225 | Handle<JSObject> js_object; // JavaScript module object. |
| 226 | Handle<Context> context; // JavaScript native context. |
| 227 | Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
| 228 | Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
| 229 | Handle<FixedArray> function_table; // indirect function table. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 230 | std::vector<Handle<Code>> function_code; // code objects for each function. |
| 231 | std::vector<Handle<Code>> import_code; // code objects for each import. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 232 | // -- raw memory ------------------------------------------------------------ |
| 233 | byte* mem_start; // start of linear memory. |
| 234 | size_t mem_size; // size of the linear memory. |
| 235 | // -- raw globals ----------------------------------------------------------- |
| 236 | byte* globals_start; // start of the globals area. |
| 237 | size_t globals_size; // size of the globals area. |
| 238 | |
| 239 | explicit WasmModuleInstance(WasmModule* m) |
| 240 | : module(m), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 241 | mem_start(nullptr), |
| 242 | mem_size(0), |
| 243 | globals_start(nullptr), |
| 244 | globals_size(0) {} |
| 245 | }; |
| 246 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 247 | // forward declaration. |
| 248 | class WasmLinker; |
| 249 | |
| 250 | // Interface provided to the decoder/graph builder which contains only |
| 251 | // minimal information about the globals, functions, and function tables. |
| 252 | struct ModuleEnv { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | WasmModule* module; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 254 | WasmModuleInstance* instance; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 255 | WasmLinker* linker; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 256 | ModuleOrigin origin; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | |
| 258 | bool IsValidGlobal(uint32_t index) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 259 | return module && index < module->globals.size(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 260 | } |
| 261 | bool IsValidFunction(uint32_t index) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 262 | return module && index < module->functions.size(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 263 | } |
| 264 | bool IsValidSignature(uint32_t index) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 265 | return module && index < module->signatures.size(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 266 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 267 | bool IsValidImport(uint32_t index) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 268 | return module && index < module->import_table.size(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 269 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 270 | MachineType GetGlobalType(uint32_t index) { |
| 271 | DCHECK(IsValidGlobal(index)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 272 | return module->globals[index].type; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 273 | } |
| 274 | FunctionSig* GetFunctionSignature(uint32_t index) { |
| 275 | DCHECK(IsValidFunction(index)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 276 | return module->functions[index].sig; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 277 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 278 | FunctionSig* GetImportSignature(uint32_t index) { |
| 279 | DCHECK(IsValidImport(index)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 280 | return module->import_table[index].sig; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 281 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 282 | FunctionSig* GetSignature(uint32_t index) { |
| 283 | DCHECK(IsValidSignature(index)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 284 | return module->signatures[index]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 285 | } |
| 286 | size_t FunctionTableSize() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 287 | return module ? module->function_table.size() : 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 290 | bool asm_js() { return origin == kAsmJsOrigin; } |
| 291 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 292 | Handle<Code> GetFunctionCode(uint32_t index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 293 | Handle<Code> GetImportCode(uint32_t index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 294 | Handle<FixedArray> GetFunctionTable(); |
| 295 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 296 | static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
| 297 | FunctionSig* sig); |
| 298 | static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
| 299 | Zone* zone, compiler::CallDescriptor* descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 300 | compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
| 301 | }; |
| 302 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 303 | // A helper for printing out the names of functions. |
| 304 | struct WasmFunctionName { |
| 305 | const WasmFunction* function_; |
| 306 | const WasmModule* module_; |
| 307 | WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) |
| 308 | : function_(function), module_(menv ? menv->module : nullptr) {} |
| 309 | }; |
| 310 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 312 | std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 313 | std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 314 | |
| 315 | typedef Result<WasmModule*> ModuleResult; |
| 316 | typedef Result<WasmFunction*> FunctionResult; |
| 317 | |
| 318 | // For testing. Decode, verify, and run the last exported function in the |
| 319 | // given encoded module. |
| 320 | int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 321 | const byte* module_end, bool asm_js = false); |
| 322 | |
| 323 | // For testing. Decode, verify, and run the last exported function in the |
| 324 | // given decoded module. |
| 325 | int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 326 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 327 | // Extract a function name from the given wasm object. |
| 328 | // Returns undefined if the function is unnamed or the function index is |
| 329 | // invalid. |
| 330 | Handle<Object> GetWasmFunctionName(Handle<JSObject> wasm, uint32_t func_index); |
| 331 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 332 | } // namespace wasm |
| 333 | } // namespace internal |
| 334 | } // namespace v8 |
| 335 | |
| 336 | #endif // V8_WASM_MODULE_H_ |