From 3b207ba308e9828818ab9517ae912d42daf7b815 Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Thu, 20 Jul 2023 06:03:26 -0700 Subject: [PATCH] TypeGraph: add DotPrinter --- oi/type_graph/CMakeLists.txt | 1 + oi/type_graph/DotPrinter.cpp | 221 ++++++++++++++++++++++++++++++++++ oi/type_graph/DotPrinter.h | 64 ++++++++++ oi/type_graph/PassManager.cpp | 14 ++- 4 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 oi/type_graph/DotPrinter.cpp create mode 100644 oi/type_graph/DotPrinter.h diff --git a/oi/type_graph/CMakeLists.txt b/oi/type_graph/CMakeLists.txt index 38a3ca9..107b077 100644 --- a/oi/type_graph/CMakeLists.txt +++ b/oi/type_graph/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(type_graph AddChildren.cpp AddPadding.cpp AlignmentCalc.cpp + DotPrinter.cpp DrgnParser.cpp Flattener.cpp NameGen.cpp diff --git a/oi/type_graph/DotPrinter.cpp b/oi/type_graph/DotPrinter.cpp new file mode 100644 index 0000000..95e301e --- /dev/null +++ b/oi/type_graph/DotPrinter.cpp @@ -0,0 +1,221 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "DotPrinter.h" + +#include + +namespace type_graph { + +DotPrinter::DotPrinter(std::ostream& out) : out_{out} { + out_ << "digraph {" << std::endl; +} + +DotPrinter::~DotPrinter() { + out_ << "}" << std::endl; +} + +void DotPrinter::print(const Type& type) { + type.accept(*this); +} + +template +void DotPrinter::edge(const Fr* from, const To* to) { + out_ << (uintptr_t)from << " -> " << (uintptr_t)to << std::endl; +} + +void DotPrinter::visit(const Class& c) { + if (!hasPrinted_.insert(&c).second) + return; + + std::string kind; + switch (c.kind()) { + case Class::Kind::Class: + kind = "Class"; + break; + case Class::Kind::Struct: + kind = "Struct"; + break; + case Class::Kind::Union: + kind = "Union"; + break; + } + + out_ << (uintptr_t)&c << " [label=\""; + out_ << kind << ": " << c.name() << " (size: " << c.size() + << align_str(c.align()); + if (c.packed()) { + out_ << ", packed"; + } + out_ << ")" + << "\"]" << std::endl; + + for (const auto& param : c.templateParams) { + edge(&c, ¶m); + print_param(param); + } + for (const auto& parent : c.parents) { + edge(&c, &parent); + print_parent(parent); + } + for (const auto& member : c.members) { + edge(&c, &member); + print_member(member); + } + for (const auto& function : c.functions) { + edge(&c, &function); + print_function(function); + } + for (auto& child : c.children) { + edge(&c, &child); + print_child(child); + } +} + +void DotPrinter::visit(const Container& c) { + if (!hasPrinted_.insert(&c).second) + return; + + out_ << (uintptr_t)&c << " [label=\"" + << "Container: " << c.name() << " (size: " << c.size() << ")" + << "\"]" << std::endl; + + for (const auto& param : c.templateParams) { + edge(&c, ¶m); + print_param(param); + } +} + +void DotPrinter::visit(const Primitive& p) { + out_ << (uintptr_t)&p << " [label=\"" + << "Primitive: " << p.name() << "\"]" << std::endl; +} + +void DotPrinter::visit(const Enum& e) { + out_ << (uintptr_t)&e << " [label=\"" + << "Enum: " << e.name() << " (size: " << e.size() << ")" + << "\"]" << std::endl; +} + +void DotPrinter::visit(const Array& a) { + out_ << (uintptr_t)&a << " [label=\"" + << "Array: (length: " << a.len() << ")" + << "\"]" << std::endl; + edge(&a, &a.elementType()); + print(a.elementType()); +} + +void DotPrinter::visit(const Typedef& td) { + if (!hasPrinted_.insert(&td).second) + return; + + out_ << (uintptr_t)&td << " [label=\"" + << "Typedef: " << td.name() << "\"]" << std::endl; + edge(&td, &td.underlyingType()); + print(td.underlyingType()); +} + +void DotPrinter::visit(const Pointer& p) { + out_ << (uintptr_t)&p << " [label=\"" << p.name() << "\"]" << std::endl; + edge(&p, &p.pointeeType()); + print(p.pointeeType()); +} + +void DotPrinter::visit(const Dummy& d) { + out_ << (uintptr_t)&d << " [label=\"" + << "Dummy (size: " << d.size() << align_str(d.align()) << ")" + << "\"]" << std::endl; +} + +void DotPrinter::visit(const DummyAllocator& d) { + out_ << (uintptr_t)&d << " [label=\"" + << "DummyAllocator (size: " << d.size() << align_str(d.align()) << ")" + << "\"]" << std::endl; + + print(d.allocType()); +} + +void DotPrinter::print_param(const TemplateParam& param) { + out_ << (uintptr_t)¶m << " [label=\""; + out_ << "Param"; + if (param.value) { + print_value(*param.value); + } + print_qualifiers(param.qualifiers); + out_ << "\"]" << std::endl; + + if (!param.value) { + edge(¶m, param.type()); + print(*param.type()); + } +} + +void DotPrinter::print_parent(const Parent& parent) { + out_ << (uintptr_t)&parent << " [label=\"" + << "Parent (offset: " << static_cast(parent.bitOffset) / 8 << ")" + << "\"]" << std::endl; + edge(&parent, &parent.type()); + print(parent.type()); +} + +void DotPrinter::print_member(const Member& member) { + out_ << (uintptr_t)&member << " [label=\""; + out_ << "Member: " << member.name + << " (offset: " << static_cast(member.bitOffset) / 8; + out_ << align_str(member.align); + if (member.bitsize != 0) { + out_ << ", bitsize: " << member.bitsize; + } + out_ << ")" + << "\"]" << std::endl; + edge(&member, &member.type()); + print(member.type()); +} + +void DotPrinter::print_function(const Function& function) { + out_ << (uintptr_t)&function << " [label=\""; + out_ << "Function: " << function.name; + if (function.virtuality != 0) + out_ << " (virtual)"; + out_ << "\"]" << std::endl; +} + +void DotPrinter::print_child(const Type& child) { + // prefix(); + out_ << "Child" << std::endl; + print(child); +} + +void DotPrinter::print_value(const std::string& value) { + out_ << "Value: " << value; +} + +void DotPrinter::print_qualifiers(const QualifierSet& qualifiers) { + if (qualifiers.none()) { + return; + } + out_ << "Qualifiers:"; + if (qualifiers[Qualifier::Const]) { + out_ << " const"; + } +} + +std::string DotPrinter::align_str(uint64_t align) { + if (align == 0) + return ""; + return ", align: " + std::to_string(align); +} + +} // namespace type_graph diff --git a/oi/type_graph/DotPrinter.h b/oi/type_graph/DotPrinter.h new file mode 100644 index 0000000..e14b2a8 --- /dev/null +++ b/oi/type_graph/DotPrinter.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include +#include + +#include "Types.h" +#include "Visitor.h" + +namespace type_graph { + +/* + * DotPrinter + */ +class DotPrinter : public ConstVisitor { + public: + DotPrinter(std::ostream& out); + ~DotPrinter(); + + void print(const Type& type); + + void visit(const Class& c) override; + void visit(const Container& c) override; + void visit(const Primitive& p) override; + void visit(const Enum& e) override; + void visit(const Array& a) override; + void visit(const Typedef& td) override; + void visit(const Pointer& p) override; + void visit(const Dummy& d) override; + void visit(const DummyAllocator& d) override; + + private: + template + void edge(const Fr* from, const To* to); + + void print_param(const TemplateParam& param); + void print_parent(const Parent& parent); + void print_member(const Member& member); + void print_function(const Function& function); + void print_child(const Type& child); + void print_value(const std::string& value); + void print_qualifiers(const QualifierSet& qualifiers); + static std::string align_str(uint64_t align); + + std::ostream& out_; + std::unordered_set hasPrinted_; +}; + +} // namespace type_graph diff --git a/oi/type_graph/PassManager.cpp b/oi/type_graph/PassManager.cpp index 5f8f5b4..5f96358 100644 --- a/oi/type_graph/PassManager.cpp +++ b/oi/type_graph/PassManager.cpp @@ -20,6 +20,7 @@ #include #include +#include "DotPrinter.h" #include "Printer.h" #include "TypeGraph.h" @@ -42,9 +43,16 @@ void print(const TypeGraph& typeGraph) { return; std::stringstream out; - Printer printer{out, typeGraph.size()}; - for (const auto& type : typeGraph.rootTypes()) { - printer.print(type); + // Printer printer{out, typeGraph.size()}; + // for (const auto& type : typeGraph.rootTypes()) { + // printer.print(type); + // } + + { + DotPrinter dotPrinter{out}; + for (const auto& type : typeGraph.rootTypes()) { + dotPrinter.print(type); + } } // Long strings will be truncated by glog, use std::cerr instead