1/*
2 * Copyright (C) 2017 Yusuke Suzuki <[email protected]>
3 * Copyright (C) 2018 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 static void dumpBlock(Block*, const InstructionStream&, PrintStream& out, const ICStatusMap& = ICStatusMap());
44
45 void printLocationAndOp(InstructionStream::Offset location, const char* op);
46
47 template<typename T>
48 void dumpOperand(T operand, bool isFirst = false)
49 {
50 if (!isFirst)
51 m_out.print(", ");
52 dumpValue(operand);
53 }
54
55 void dumpValue(VirtualRegister reg) { m_out.printf("%s", registerName(reg.offset()).data()); }
56 void dumpValue(BoundLabel label)
57 {
58 InstructionStream::Offset targetOffset = label.target() + m_currentLocation;
59 m_out.print(label.target(), "(->", targetOffset, ")");
60 }
61 template<typename T>
62 void dumpValue(T v) { m_out.print(v); }
63
64private:
65 BytecodeDumper(Block* block, PrintStream& out)
66 : m_block(block)
67 , m_out(out)
68 {
69 }
70
71 Block* block() const { return m_block; }
72
73 ALWAYS_INLINE VM* vm() const;
74
75 CString registerName(int r) const;
76 CString constantName(int index) const;
77
78 const Identifier& identifier(int index) const;
79
80 void dumpIdentifiers();
81 void dumpConstants();
82 void dumpExceptionHandlers();
83 void dumpSwitchJumpTables();
84 void dumpStringSwitchJumpTables();
85
86 void dumpBytecode(const InstructionStream::Ref& it, const ICStatusMap&);
87
88 Block* m_block;
89 PrintStream& m_out;
90 InstructionStream::Offset m_currentLocation { 0 };
91};
92
93}
94