// Copyright 2013 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_COMPILER_GRAPH_VISUALIZER_H_ #define V8_COMPILER_GRAPH_VISUALIZER_H_ #include #include #include #include #include #include "src/objects/code.h" #include "src/base/optional.h" #include "src/common/globals.h" #include "src/handles/handles.h" namespace v8 { namespace internal { class OptimizedCompilationInfo; class SharedFunctionInfo; class SourcePosition; struct WasmInliningPosition; namespace wasm { struct WasmModule; class WireBytesStorage; } // namespace wasm namespace compiler { class Graph; class LiveRange; class TopLevelLiveRange; class Instruction; class InstructionBlock; class InstructionOperand; class InstructionSequence; class Node; class NodeOrigin; class NodeOriginTable; class RegisterAllocationData; class Schedule; class SourcePositionTable; class Type; class JSONEscaped { public: template explicit JSONEscaped(const T& value) { std::ostringstream s; s << value; str_ = s.str(); } explicit JSONEscaped(std::string str) : str_(std::move(str)) {} explicit JSONEscaped(const std::ostringstream& os) : str_(os.str()) {} friend std::ostream& operator<<(std::ostream& os, const JSONEscaped& e) { for (char c : e.str_) PipeCharacter(os, c); return os; } private: static std::ostream& PipeCharacter(std::ostream& os, char c) { if (c == '"') return os << "\\\""; if (c == '\\') return os << "\\\\"; if (c == '\b') return os << "\\b"; if (c == '\f') return os << "\\f"; if (c == '\n') return os << "\\n"; if (c == '\r') return os << "\\r"; if (c == '\t') return os << "\\t"; return os << c; } std::string str_; }; struct TurboJsonFile : public std::ofstream { TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode); ~TurboJsonFile() override; }; struct TurboCfgFile : public std::ofstream { explicit TurboCfgFile(Isolate* isolate = nullptr); ~TurboCfgFile() override; }; struct SourcePositionAsJSON { explicit SourcePositionAsJSON(const SourcePosition& sp) : sp(sp) {} const SourcePosition& sp; }; V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON AsJSON(const SourcePosition& sp) { return SourcePositionAsJSON(sp); } struct NodeOriginAsJSON { explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {} const NodeOrigin& no; }; V8_INLINE V8_EXPORT_PRIVATE NodeOriginAsJSON AsJSON(const NodeOrigin& no) { return NodeOriginAsJSON(no); } std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos); std::ostream& operator<<(std::ostream& out, const NodeOriginAsJSON& asJSON); // Small helper that deduplicates SharedFunctionInfos. class V8_EXPORT_PRIVATE SourceIdAssigner { public: explicit SourceIdAssigner(size_t size) { printed_.reserve(size); source_ids_.reserve(size); } int GetIdFor(Handle shared); int GetIdAt(size_t pos) const { return source_ids_[pos]; } private: std::vector> printed_; std::vector source_ids_; }; void JsonPrintAllBytecodeSources(std::ostream& os, OptimizedCompilationInfo* info); void JsonPrintBytecodeSource(std::ostream& os, int source_id, std::unique_ptr function_name, Handle bytecode_array); void JsonPrintAllSourceWithPositions(std::ostream& os, OptimizedCompilationInfo* info, Isolate* isolate); #if V8_ENABLE_WEBASSEMBLY void JsonPrintAllSourceWithPositionsWasm( std::ostream& os, const wasm::WasmModule* module, const wasm::WireBytesStorage* wire_bytes, base::Vector positions); #endif void JsonPrintFunctionSource(std::ostream& os, int source_id, std::unique_ptr function_name, Handle