OLD | NEW |
(Empty) | |
| 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; |
| 19 } |
| 20 |
| 21 namespace wasm { |
| 22 const size_t kMaxModuleSize = 1024 * 1024 * 1024; |
| 23 const size_t kMaxFunctionSize = 128 * 1024; |
| 24 const size_t kMaxStringSize = 256; |
| 25 |
| 26 enum WasmSectionDeclCode { |
| 27 kDeclMemory = 0x00, |
| 28 kDeclSignatures = 0x01, |
| 29 kDeclFunctions = 0x02, |
| 30 kDeclGlobals = 0x03, |
| 31 kDeclDataSegments = 0x04, |
| 32 kDeclFunctionTable = 0x05, |
| 33 kDeclEnd = 0x06, |
| 34 }; |
| 35 |
| 36 static const int kMaxModuleSectionCode = 6; |
| 37 |
| 38 enum WasmFunctionDeclBit { |
| 39 kDeclFunctionName = 0x01, |
| 40 kDeclFunctionImport = 0x02, |
| 41 kDeclFunctionLocals = 0x04, |
| 42 kDeclFunctionExport = 0x08 |
| 43 }; |
| 44 |
| 45 // Constants for fixed-size elements within a module. |
| 46 static const size_t kDeclMemorySize = 3; |
| 47 static const size_t kDeclGlobalSize = 6; |
| 48 static const size_t kDeclDataSegmentSize = 13; |
| 49 |
| 50 // Static representation of a wasm function. |
| 51 struct WasmFunction { |
| 52 FunctionSig* sig; // signature of the function. |
| 53 uint16_t sig_index; // index into the signature table. |
| 54 uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 55 uint32_t code_start_offset; // offset in the module bytes of code start. |
| 56 uint32_t code_end_offset; // offset in the module bytes of code end. |
| 57 uint16_t local_int32_count; // number of int32 local variables. |
| 58 uint16_t local_int64_count; // number of int64 local variables. |
| 59 uint16_t local_float32_count; // number of float32 local variables. |
| 60 uint16_t local_float64_count; // number of float64 local variables. |
| 61 bool exported; // true if this function is exported. |
| 62 bool external; // true if this function is externally supplied. |
| 63 }; |
| 64 |
| 65 struct ModuleEnv; // forward declaration of decoder interface. |
| 66 |
| 67 // Static representation of a wasm global variable. |
| 68 struct WasmGlobal { |
| 69 uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 70 MachineType type; // type of the global. |
| 71 uint32_t offset; // offset from beginning of globals area. |
| 72 bool exported; // true if this global is exported. |
| 73 }; |
| 74 |
| 75 // Static representation of a wasm data segment. |
| 76 struct WasmDataSegment { |
| 77 uint32_t dest_addr; // destination memory address of the data. |
| 78 uint32_t source_offset; // start offset in the module bytes. |
| 79 uint32_t source_size; // end offset in the module bytes. |
| 80 bool init; // true if loaded upon instantiation. |
| 81 }; |
| 82 |
| 83 // Static representation of a module. |
| 84 struct WasmModule { |
| 85 static const uint8_t kMinMemSize = 12; // Minimum memory size = 4kb |
| 86 static const uint8_t kMaxMemSize = 30; // Maximum memory size = 1gb |
| 87 |
| 88 Isolate* shared_isolate; // isolate for storing shared code. |
| 89 const byte* module_start; // starting address for the module bytes. |
| 90 const byte* module_end; // end address for the module bytes. |
| 91 uint8_t min_mem_size_log2; // minimum size of the memory (log base 2). |
| 92 uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). |
| 93 bool mem_export; // true if the memory is exported. |
| 94 bool mem_external; // true if the memory is external. |
| 95 |
| 96 std::vector<WasmGlobal>* globals; // globals in this module. |
| 97 std::vector<FunctionSig*>* signatures; // signatures in this module. |
| 98 std::vector<WasmFunction>* functions; // functions in this module. |
| 99 std::vector<WasmDataSegment>* data_segments; // data segments in this module. |
| 100 std::vector<uint16_t>* function_table; // function table. |
| 101 |
| 102 // Get a pointer to a string stored in the module bytes representing a name. |
| 103 const char* GetName(uint32_t offset) { |
| 104 CHECK(BoundsCheck(offset, offset + 1)); |
| 105 if (offset == 0) return "<?>"; // no name. |
| 106 return reinterpret_cast<const char*>(module_start + offset); |
| 107 } |
| 108 |
| 109 // Checks the given offset range is contained within the module bytes. |
| 110 bool BoundsCheck(uint32_t start, uint32_t end) { |
| 111 size_t size = module_end - module_start; |
| 112 return start < size && end < size; |
| 113 } |
| 114 |
| 115 // Creates a new instantiation of the module in the given isolate. |
| 116 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSObject> ffi, |
| 117 Handle<JSArrayBuffer> memory); |
| 118 }; |
| 119 |
| 120 // forward declaration. |
| 121 class WasmLinker; |
| 122 |
| 123 // Interface provided to the decoder/graph builder which contains only |
| 124 // minimal information about the globals, functions, and function tables. |
| 125 struct ModuleEnv { |
| 126 uintptr_t globals_area; // address of the globals area. |
| 127 uintptr_t mem_start; // address of the start of linear memory. |
| 128 uintptr_t mem_end; // address of the end of linear memory. |
| 129 |
| 130 WasmModule* module; |
| 131 WasmLinker* linker; |
| 132 std::vector<Handle<Code>>* function_code; |
| 133 Handle<FixedArray> function_table; |
| 134 Handle<JSArrayBuffer> memory; |
| 135 Handle<Context> context; |
| 136 bool asm_js; // true if the module originated from asm.js. |
| 137 |
| 138 bool IsValidGlobal(uint32_t index) { |
| 139 return module && index < module->globals->size(); |
| 140 } |
| 141 bool IsValidFunction(uint32_t index) { |
| 142 return module && index < module->functions->size(); |
| 143 } |
| 144 bool IsValidSignature(uint32_t index) { |
| 145 return module && index < module->signatures->size(); |
| 146 } |
| 147 MachineType GetGlobalType(uint32_t index) { |
| 148 DCHECK(IsValidGlobal(index)); |
| 149 return module->globals->at(index).type; |
| 150 } |
| 151 FunctionSig* GetFunctionSignature(uint32_t index) { |
| 152 DCHECK(IsValidFunction(index)); |
| 153 return module->functions->at(index).sig; |
| 154 } |
| 155 FunctionSig* GetSignature(uint32_t index) { |
| 156 DCHECK(IsValidSignature(index)); |
| 157 return module->signatures->at(index); |
| 158 } |
| 159 size_t FunctionTableSize() { |
| 160 return module ? module->function_table->size() : 0; |
| 161 } |
| 162 |
| 163 Handle<Code> GetFunctionCode(uint32_t index); |
| 164 Handle<FixedArray> GetFunctionTable(); |
| 165 |
| 166 compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, FunctionSig* sig); |
| 167 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
| 168 }; |
| 169 |
| 170 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 171 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
| 172 |
| 173 typedef Result<WasmModule*> ModuleResult; |
| 174 typedef Result<WasmFunction*> FunctionResult; |
| 175 |
| 176 // For testing. Decode, verify, and run the last exported function in the |
| 177 // given encoded module. |
| 178 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 179 const byte* module_end, bool asm_js = false); |
| 180 |
| 181 // For testing. Decode, verify, and run the last exported function in the |
| 182 // given decoded module. |
| 183 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
| 184 } // namespace wasm |
| 185 } // namespace internal |
| 186 } // namespace v8 |
| 187 |
| 188 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |