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 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "wasm/WasmDump.h"
12 using namespace js::wasm
;
16 void wasm::Dump(ValType type
) {
18 wasm::Dump(type
, out
);
21 void wasm::Dump(ValType type
, GenericPrinter
& out
) {
22 Dump(type
.storageType(), out
);
25 void wasm::Dump(StorageType type
) {
27 wasm::Dump(type
, out
);
30 void wasm::Dump(StorageType type
, GenericPrinter
& out
) {
31 const char* literal
= nullptr;
32 switch (type
.kind()) {
36 case StorageType::I16
:
39 case StorageType::I32
:
42 case StorageType::I64
:
45 case StorageType::V128
:
48 case StorageType::F32
:
51 case StorageType::F64
:
54 case StorageType::Ref
:
55 return Dump(type
.refType(), out
);
60 void wasm::Dump(RefType type
) {
62 wasm::Dump(type
, out
);
65 void wasm::Dump(RefType type
, GenericPrinter
& out
) {
66 if (type
.isNullable() && !type
.isTypeRef()) {
67 const char* literal
= nullptr;
68 switch (type
.kind()) {
73 literal
= "externref";
79 literal
= "nullfuncref";
81 case RefType::NoExtern
:
82 literal
= "nullexternref";
94 literal
= "structref";
102 case RefType::TypeRef
: {
103 MOZ_CRASH("type ref should not be possible here");
110 // Emit the full reference type with heap type
111 const char* heapType
= nullptr;
112 switch (type
.kind()) {
116 case RefType::Extern
:
122 case RefType::NoFunc
:
125 case RefType::NoExtern
:
126 heapType
= "noextern";
137 case RefType::Struct
:
146 case RefType::TypeRef
: {
147 uintptr_t typeAddress
= (uintptr_t)type
.typeDef();
148 out
.printf("(ref %s0x%" PRIxPTR
")", type
.isNullable() ? "null " : "",
153 out
.printf("(ref %s%s)", type
.isNullable() ? "null " : "", heapType
);
156 void wasm::Dump(const FuncType
& funcType
) {
157 Fprinter
fileOut(stdout
);
158 IndentedPrinter
out(fileOut
);
159 wasm::Dump(funcType
, out
);
162 void wasm::Dump(const FuncType
& funcType
, IndentedPrinter
& out
) {
163 out
.printf("(func\n");
165 IndentedPrinter::AutoIndent
innerIndent(out
);
166 for (ValType arg
: funcType
.args()) {
167 out
.printf("(param ");
171 for (ValType result
: funcType
.results()) {
172 out
.printf("(result ");
180 void wasm::Dump(const StructType
& structType
) {
181 Fprinter
fileOut(stdout
);
182 IndentedPrinter
out(fileOut
);
183 wasm::Dump(structType
, out
);
186 void wasm::Dump(const StructType
& structType
, IndentedPrinter
& out
) {
187 out
.printf("(struct\n");
189 IndentedPrinter::AutoIndent
innerIndent(out
);
190 for (const StructField
& field
: structType
.fields_
) {
191 out
.printf("(field ");
192 if (field
.isMutable
) {
195 Dump(field
.type
, out
);
196 if (field
.isMutable
) {
205 void wasm::Dump(const ArrayType
& arrayType
) {
206 Fprinter
fileOut(stdout
);
207 IndentedPrinter
out(fileOut
);
208 wasm::Dump(arrayType
, out
);
211 void wasm::Dump(const ArrayType
& arrayType
, IndentedPrinter
& out
) {
212 out
.printf("(array ");
213 if (arrayType
.isMutable_
) {
216 Dump(arrayType
.elementType_
, out
);
217 if (arrayType
.isMutable_
) {
223 void wasm::Dump(const TypeDef
& typeDef
) {
224 Fprinter
fileOut(stdout
);
225 IndentedPrinter
out(fileOut
);
226 wasm::Dump(typeDef
, out
);
229 void wasm::Dump(const TypeDef
& typeDef
, IndentedPrinter
& out
) {
230 out
.printf("(type 0x%" PRIxPTR
"\n", (uintptr_t)&typeDef
);
233 IndentedPrinter::AutoIndent
innerIndent(out
);
234 out
.printf("final=%u\n", typeDef
.isFinal() ? 1 : 0);
235 out
.printf("subtypingDepth=%u\n", typeDef
.subTypingDepth());
236 if (typeDef
.superTypeDef()) {
237 out
.printf("superType=0x%" PRIxPTR
"\n",
238 (uintptr_t)typeDef
.superTypeDef());
240 switch (typeDef
.kind()) {
241 case TypeDefKind::Func
:
242 Dump(typeDef
.funcType(), out
);
244 case TypeDefKind::Struct
:
245 Dump(typeDef
.structType(), out
);
247 case TypeDefKind::Array
:
248 Dump(typeDef
.arrayType(), out
);
250 case TypeDefKind::None
:
251 out
.printf("(none)\n");
259 void wasm::Dump(const RecGroup
& recGroup
) {
260 Fprinter
fileOut(stdout
);
261 IndentedPrinter
out(fileOut
);
262 wasm::Dump(recGroup
, out
);
265 void wasm::Dump(const RecGroup
& recGroup
, IndentedPrinter
& out
) {
266 out
.printf("(rec\n");
268 IndentedPrinter::AutoIndent
innerIndent(out
);
269 for (uint32_t typeIndex
= 0; typeIndex
< recGroup
.numTypes(); typeIndex
++) {
270 Dump(recGroup
.type(typeIndex
), out
);
276 void wasm::Dump(const TypeContext
& typeContext
) {
277 Fprinter
fileOut(stdout
);
278 IndentedPrinter
out(fileOut
);
279 wasm::Dump(typeContext
, out
);
282 void wasm::Dump(const TypeContext
& typeContext
, IndentedPrinter
& out
) {
283 out
.printf("(types\n");
285 IndentedPrinter::AutoIndent
innerIndent(out
);
286 for (const SharedRecGroup
& recGroup
: typeContext
.groups()) {
287 Dump(*recGroup
, out
);