Jlm
print.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
7 #include <jlm/llvm/ir/cfg.hpp>
10 #include <jlm/llvm/ir/print.hpp>
11 #include <jlm/llvm/ir/tac.hpp>
12 
13 #include <typeindex>
14 
15 #include <deque>
16 
17 namespace jlm::llvm
18 {
19 
20 /* string converters */
21 
22 static std::string
23 emit_tacs(const tacsvector_t & tacs)
24 {
25  std::string str;
26  for (const auto & tac : tacs)
27  str += ThreeAddressCode::ToAscii(*tac) + ", ";
28 
29  return "[" + str + "]";
30 }
31 
32 static std::string
34 {
35  const auto & node = *util::assertedCast<const FunctionNode>(&clg_node);
36 
37  const auto & fcttype = node.fcttype();
38 
39  /* convert result types */
40  std::string results("<");
41  for (size_t n = 0; n < fcttype.NumResults(); n++)
42  {
43  results += fcttype.ResultType(n).debug_string();
44  if (n != fcttype.NumResults() - 1)
45  results += ", ";
46  }
47  results += ">";
48 
49  /* convert operand types */
50  std::string operands("<");
51  for (size_t n = 0; n < fcttype.NumArguments(); n++)
52  {
53  operands += fcttype.ArgumentType(n).debug_string();
54  if (n != fcttype.NumArguments() - 1)
55  operands += ", ";
56  }
57  operands += ">";
58 
59  std::string cfg = node.cfg() ? ControlFlowGraph::ToAscii(*node.cfg()) : "";
60  std::string exported = isPrivateOrInternal(node.linkage()) ? "static" : "";
61 
62  return exported + results + " " + node.name() + " " + operands + "\n{\n" + cfg + "\n}\n";
63 }
64 
65 static std::string
67 {
68  const auto & node = *util::assertedCast<const DataNode>(&clg_node);
69  auto init = node.initialization();
70 
71  std::string str = node.name();
72  if (init)
73  str += " = " + emit_tacs(init->tacs());
74 
75  return str;
76 }
77 
78 std::string
80 {
81  static std::
82  unordered_map<std::type_index, std::function<std::string(const InterProceduralGraphNode &)>>
83  map({ { typeid(FunctionNode), emit_function_node },
84  { typeid(DataNode), emit_data_node } });
85 
86  std::string str;
87  for (const auto & node : clg)
88  {
89  JLM_ASSERT(map.find(typeid(node)) != map.end());
90  str += map[typeid(node)](node) + "\n";
91  }
92 
93  return str;
94 }
95 
96 /* aggregation node */
97 
98 std::string
99 to_str(const AggregationNode & n, const AnnotationMap & dm)
100 {
101  std::function<std::string(const AggregationNode &, size_t)> f =
102  [&](const AggregationNode & n, size_t depth)
103  {
104  std::string subtree(depth, '-');
105  subtree += n.debug_string();
106 
107  if (dm.Contains(n))
108  subtree += " " + dm.Lookup<AnnotationSet>(n).DebugString() + "\n";
109 
110  for (const auto & child : n)
111  subtree += f(child, depth + 1);
112 
113  return subtree;
114  };
115 
116  return f(n, 0);
117 }
118 
119 void
120 print(const AggregationNode & n, const AnnotationMap & dm, FILE * out)
121 {
122  fputs(to_str(n, dm).c_str(), out);
123  fflush(out);
124 }
125 
126 }
virtual std::string debug_string() const =0
T & Lookup(const AggregationNode &aggregationNode) const noexcept
Definition: Annotation.hpp:504
bool Contains(const AggregationNode &aggregationNode) const noexcept
Definition: Annotation.hpp:497
static std::string ToAscii(const ControlFlowGraph &controlFlowGraph)
Definition: cfg.cpp:151
static std::string ToAscii(const ThreeAddressCode &threeAddressCode)
Definition: tac.cpp:120
#define JLM_ASSERT(x)
Definition: common.hpp:16
Global memory state passed between functions.
static std::string emit_data_node(const InterProceduralGraphNode &clg_node)
Definition: print.cpp:66
static std::string emit_tacs(const tacsvector_t &tacs)
Definition: print.cpp:23
std::vector< std::unique_ptr< llvm::ThreeAddressCode > > tacsvector_t
Definition: tac.hpp:202
void print(const AggregationNode &n, const AnnotationMap &dm, FILE *out)
Definition: print.cpp:120
bool isPrivateOrInternal(const Linkage linkage)
Definition: Linkage.hpp:65
std::string to_str(const InterProceduralGraph &clg)
Definition: print.cpp:79
static std::string emit_function_node(const InterProceduralGraphNode &clg_node)
Definition: print.cpp:33
static std::vector< jlm::rvsdg::Output * > operands(const Node *node)
Definition: node.hpp:1049