1/*
2 * Copyright (C) 2017 Yusuke Suzuki <[email protected]>
3 * Copyright (C) 2018-2019 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "CallLinkInfo.h"
30#include "ICStatusMap.h"
31#include "InstructionStream.h"
32#include "Label.h"
33#include "StructureStubInfo.h"
34
35namespace JSC {
36
37struct Instruction;
38
39template<class Block>
40class BytecodeDumper {
41public:
42 static void dumpBytecode(Block*, PrintStream& out, const InstructionStream::Ref& it, const ICStatusMap& = ICStatusMap());
43
44 void printLocationAndOp(InstructionStream::Offset location, const char* op);
45
46 template<typename T>
47 void dumpOperand(T operand, bool isFirst = false)
48 {
49 if (!isFirst)
50 m_out.print(", ");
51 dumpValue(operand);
52 }
53
54 void dumpValue(VirtualRegister);
55
56 template<typename Traits>
57 void dumpValue(GenericBoundLabel<Traits> label)
58 {
59 InstructionStream::Offset targetOffset = label.target() + m_currentLocation;
60 m_out.print(label.target(), "(->", targetOffset, ")");
61 }
62
63
64 template<typename T>
65 void dumpValue(T v) { m_out.print(v); }
66
67 BytecodeDumper(Block* block, PrintStream& out)
68 : m_out(out)
69 , m_block(block)
70 {
71 }
72
73 virtual ~BytecodeDumper() { }
74
75protected:
76 Block* block() const { return m_block; }
77
78 void dumpBytecode(const InstructionStream::Ref& it, const ICStatusMap&);
79
80 PrintStream& m_out;
81
82private:
83 CString registerName(int r) const;
84 virtual CString constantName(int index) const;
85
86 Block* m_block;
87 InstructionStream::Offset m_currentLocation { 0 };
88};
89
90template<class Block>
91class CodeBlockBytecodeDumper : public BytecodeDumper<Block> {
92public:
93 static void dumpBlock(Block*, const InstructionStream&, PrintStream& out, const ICStatusMap& = ICStatusMap());
94
95private:
96 using BytecodeDumper<Block>::BytecodeDumper;
97
98 ALWAYS_INLINE VM& vm() const;
99
100 const Identifier& identifier(int index) const;
101
102 void dumpIdentifiers();
103 void dumpConstants();
104 void dumpExceptionHandlers();
105 void dumpSwitchJumpTables();
106 void dumpStringSwitchJumpTables();
107};
108
109#if ENABLE(WEBASSEMBLY)
110
111namespace Wasm {
112
113class FunctionCodeBlock;
114struct ModuleInformation;
115enum Type : int8_t;
116
117class BytecodeDumper : public JSC::BytecodeDumper<FunctionCodeBlock> {
118public:
119 static void dumpBlock(FunctionCodeBlock*, const ModuleInformation&, PrintStream& out);
120
121private:
122 using JSC::BytecodeDumper<FunctionCodeBlock>::BytecodeDumper;
123
124 void dumpConstants();
125 CString constantName(int index) const override;
126 CString formatConstant(Type, uint64_t) const;
127};
128
129} // namespace Wasm
130
131#endif // ENABLE(WEBASSEMBLY)
132
133}
134