WasmDump.h (10527B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * 4 * Copyright 2023 Mozilla Foundation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef wasm_dump_h 20 #define wasm_dump_h 21 22 #include "js/Printer.h" 23 24 #include "wasm/WasmModuleTypes.h" 25 #include "wasm/WasmOpIter.h" 26 #include "wasm/WasmTypeDef.h" 27 #include "wasm/WasmValType.h" 28 29 namespace js { 30 namespace wasm { 31 32 extern void DumpModule(const Module& module); 33 extern void DumpModule(const Module& module, GenericPrinter& out); 34 35 extern void DumpValType(ValType type, const TypeContext* types = nullptr); 36 extern void DumpValType(ValType type, GenericPrinter& out, 37 const TypeContext* types = nullptr); 38 39 extern void DumpStorageType(StorageType type, 40 const TypeContext* types = nullptr); 41 extern void DumpStorageType(StorageType type, GenericPrinter& out, 42 const TypeContext* types = nullptr); 43 44 extern void DumpRefType(RefType type, const TypeContext* types = nullptr); 45 extern void DumpRefType(RefType type, GenericPrinter& out, 46 const TypeContext* types = nullptr); 47 48 extern void DumpHeapType(RefType type, const TypeContext* types = nullptr); 49 extern void DumpHeapType(RefType type, GenericPrinter& out, 50 const TypeContext* types = nullptr); 51 52 extern void DumpFuncType(const FuncType& funcType, 53 const TypeContext* types = nullptr); 54 extern void DumpFuncType(const FuncType& funcType, StructuredPrinter& out, 55 const TypeContext* types = nullptr); 56 57 extern void DumpStructType(const StructType& structType, 58 const TypeContext* types = nullptr); 59 extern void DumpStructType(const StructType& structType, StructuredPrinter& out, 60 const TypeContext* types = nullptr); 61 62 extern void DumpArrayType(const ArrayType& arrayType, 63 const TypeContext* types = nullptr); 64 extern void DumpArrayType(const ArrayType& arrayType, StructuredPrinter& out, 65 const TypeContext* types = nullptr); 66 67 extern void DumpTypeDef(const TypeDef& typeDef, int32_t index = -1, 68 const TypeContext* types = nullptr); 69 extern void DumpTypeDef(const TypeDef& typeDef, StructuredPrinter& out, 70 int32_t index = -1, const TypeContext* types = nullptr); 71 72 extern void DumpRecGroup(const RecGroup& recGroup, int32_t startTypeIndex = -1, 73 const TypeContext* types = nullptr); 74 extern void DumpRecGroup(const RecGroup& recGroup, StructuredPrinter& out, 75 int32_t startTypeIndex = -1, 76 const TypeContext* types = nullptr); 77 78 extern void DumpTableDesc(const TableDesc& tableDesc, 79 const CodeMetadata& codeMeta, bool includeInitExpr, 80 int32_t index = -1); 81 extern void DumpTableDesc(const TableDesc& tableDesc, 82 const CodeMetadata& codeMeta, bool includeInitExpr, 83 StructuredPrinter& out, int32_t index = -1); 84 85 extern void DumpMemoryDesc(const MemoryDesc& memDesc, int32_t index = -1); 86 extern void DumpMemoryDesc(const MemoryDesc& memDesc, StructuredPrinter& out, 87 int32_t index = -1); 88 89 extern void DumpGlobalDesc(const GlobalDesc& globalDesc, 90 const CodeMetadata& codeMeta, bool includeInitExpr, 91 int32_t index = -1); 92 extern void DumpGlobalDesc(const GlobalDesc& globalDesc, 93 const CodeMetadata& codeMeta, bool includeInitExpr, 94 StructuredPrinter& out, int32_t index = -1); 95 96 extern void DumpTagDesc(const TagDesc& tagDesc, int32_t index = -1, 97 const TypeContext* types = nullptr); 98 extern void DumpTagDesc(const TagDesc& tagDesc, StructuredPrinter& out, 99 int32_t index = -1, const TypeContext* types = nullptr); 100 101 extern void DumpInitExpr(const InitExpr& initExpr, 102 const CodeMetadata& codeMeta); 103 extern void DumpInitExpr(const InitExpr& initExpr, const CodeMetadata& codeMeta, 104 StructuredPrinter& out); 105 106 extern void DumpTypeContext(const TypeContext& typeContext); 107 extern void DumpTypeContext(const TypeContext& typeContext, 108 StructuredPrinter& out); 109 110 extern void DumpFunction(const CodeMetadata& codeMeta, 111 const CodeTailMetadata& codeTailMeta, 112 uint32_t funcIndex); 113 extern void DumpFunction(const CodeMetadata& codeMeta, 114 const CodeTailMetadata& codeTailMeta, 115 uint32_t funcIndex, StructuredPrinter& out); 116 117 extern void DumpFunctionBody(const CodeMetadata& codeMeta, uint32_t funcIndex, 118 const uint8_t* bodyStart, uint32_t bodySize); 119 extern void DumpFunctionBody(const CodeMetadata& codeMeta, uint32_t funcIndex, 120 const uint8_t* bodyStart, uint32_t bodySize, 121 StructuredPrinter& out); 122 123 struct OpDumper { 124 StructuredPrinter& out; 125 const TypeContext* types; 126 int numOps = 0; 127 explicit OpDumper(StructuredPrinter& out, const TypeContext* types = nullptr) 128 : out(out), types(types) {} 129 130 void dumpOpBegin(OpBytes op) { 131 out.brk(" ", "\n"); 132 out.put(op.toString()); 133 numOps += 1; 134 if (numOps > 1) { 135 out.expand(); 136 } 137 } 138 void dumpOpEnd() {} 139 void dumpTypeIndex(uint32_t typeIndex, bool asTypeUse = false) { 140 if (asTypeUse) { 141 out.put(" (type"); 142 } 143 out.printf(" %" PRIu32, typeIndex); 144 if (asTypeUse) { 145 out.put(")"); 146 } 147 } 148 void dumpFuncIndex(uint32_t funcIndex) { out.printf(" %" PRIu32, funcIndex); } 149 void dumpTableIndex(uint32_t tableIndex) { 150 out.printf(" %" PRIu32, tableIndex); 151 } 152 void dumpGlobalIndex(uint32_t globalIndex) { 153 out.printf(" %" PRIu32, globalIndex); 154 } 155 void dumpMemoryIndex(uint32_t memoryIndex) { 156 out.printf(" %" PRIu32, memoryIndex); 157 } 158 void dumpElemIndex(uint32_t elemIndex) { out.printf(" %" PRIu32, elemIndex); } 159 void dumpDataIndex(uint32_t dataIndex) { out.printf(" %" PRIu32, dataIndex); } 160 void dumpTagIndex(uint32_t tagIndex) { out.printf(" %" PRIu32, tagIndex); } 161 void dumpLocalIndex(uint32_t localIndex) { 162 out.printf(" %" PRIu32, localIndex); 163 } 164 void dumpBlockType(BlockType type) { 165 if (type.params().length() > 0) { 166 out.put(" (param"); 167 for (uint32_t i = 0; i < type.params().length(); i++) { 168 dumpValType(type.params()[i]); 169 } 170 out.put(")"); 171 } 172 if (type.results().length() > 0) { 173 out.put(" (result"); 174 for (uint32_t i = 0; i < type.results().length(); i++) { 175 dumpValType(type.results()[i]); 176 } 177 out.put(")"); 178 } 179 } 180 void dumpI32Const(int32_t constant) { out.printf(" %" PRId32, constant); } 181 void dumpI64Const(int64_t constant) { out.printf(" %" PRId64, constant); } 182 void dumpF32Const(float constant) { out.printf(" %f", constant); } 183 void dumpF64Const(double constant) { out.printf(" %lf", constant); } 184 void dumpV128Const(V128 constant) { 185 out.printf("i8x16 %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", 186 constant.bytes[0], constant.bytes[1], constant.bytes[2], 187 constant.bytes[3], constant.bytes[4], constant.bytes[5], 188 constant.bytes[6], constant.bytes[7], constant.bytes[8], 189 constant.bytes[9], constant.bytes[10], constant.bytes[11], 190 constant.bytes[12], constant.bytes[13], constant.bytes[14], 191 constant.bytes[15]); 192 } 193 void dumpVectorMask(V128 mask) { 194 out.printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", mask.bytes[0], 195 mask.bytes[1], mask.bytes[2], mask.bytes[3], mask.bytes[4], 196 mask.bytes[5], mask.bytes[6], mask.bytes[7], mask.bytes[8], 197 mask.bytes[9], mask.bytes[10], mask.bytes[11], mask.bytes[12], 198 mask.bytes[13], mask.bytes[14], mask.bytes[15]); 199 } 200 void dumpRefType(RefType type) { 201 out.put(" "); 202 wasm::DumpRefType(type, out, types); 203 } 204 void dumpHeapType(RefType type) { 205 out.put(" "); 206 wasm::DumpHeapType(type, out, types); 207 } 208 void dumpValType(ValType type) { 209 out.put(" "); 210 wasm::DumpValType(type, out, types); 211 } 212 void dumpTryTableCatches(const TryTableCatchVector& catches) { 213 for (uint32_t i = 0; i < catches.length(); i++) { 214 const TryTableCatch& tryCatch = catches[i]; 215 if (tryCatch.tagIndex == CatchAllIndex) { 216 if (tryCatch.captureExnRef) { 217 out.put(" (catch_all_ref "); 218 } else { 219 out.put(" (catch_all "); 220 } 221 } else { 222 if (tryCatch.captureExnRef) { 223 out.printf(" (catch_ref %d ", tryCatch.tagIndex); 224 } else { 225 out.printf(" (catch %d ", tryCatch.tagIndex); 226 } 227 } 228 dumpBlockDepth(tryCatch.labelRelativeDepth); 229 out.put(")"); 230 } 231 } 232 void dumpLinearMemoryAddress(LinearMemoryAddress<mozilla::Nothing> addr) { 233 if (addr.memoryIndex != 0) { 234 out.printf(" %d", addr.memoryIndex); 235 } 236 if (addr.offset != 0) { 237 out.printf(" offset=%" PRIu64, addr.offset); 238 } 239 if (addr.align != 0) { 240 out.printf(" align=%d", addr.align); 241 } 242 } 243 void dumpBlockDepth(uint32_t relativeDepth) { 244 out.printf(" %d", relativeDepth); 245 } 246 void dumpBlockDepths(const Uint32Vector& relativeDepths) { 247 for (uint32_t i = 0; i < relativeDepths.length(); i++) { 248 out.printf(" %d", relativeDepths[i]); 249 } 250 } 251 void dumpFieldIndex(uint32_t fieldIndex) { out.printf(" %d", fieldIndex); } 252 void dumpNumElements(uint32_t numElements) { out.printf(" %d", numElements); } 253 void dumpLaneIndex(uint32_t laneIndex) { out.printf(" %d", laneIndex); } 254 255 void startScope() { out.pushScope(); } 256 void endScope() { out.popScope(); } 257 }; 258 259 } // namespace wasm 260 } // namespace js 261 262 #endif // wasm_dump_h