blob: a5d681919dcba6812ad53025207fae8ed1882ea3 [file] [log] [blame]
Dragos Sbirlea64479192013-08-01 15:38:43 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_SEA_IR_DEBUG_DOT_GEN_H_
18#define ART_COMPILER_SEA_IR_DEBUG_DOT_GEN_H_
19
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070020#include "safe_map.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070021#include "base/stringprintf.h"
22#include "file_output_stream.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070023#include "os.h"
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -070024#include "sea_ir/ir/sea.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070025#include "sea_ir/types/type_inference.h"
26
27namespace sea_ir {
28
29class DotConversionOptions {
30 public:
31 DotConversionOptions(): save_use_edges_(false) { }
32 bool WillSaveUseEdges() const {
33 return save_use_edges_;
34 }
35 private:
36 bool save_use_edges_;
37};
38
39class DotGenerationVisitor: public IRVisitor {
40 public:
41 explicit DotGenerationVisitor(const DotConversionOptions* const options,
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070042 art::SafeMap<int, const Type*>* types): graph_(), types_(types), options_(options) { }
Dragos Sbirlea64479192013-08-01 15:38:43 -070043
44 virtual void Initialize(SeaGraph* graph);
45 // Saves the ssa def->use edges corresponding to @instruction.
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070046 void ToDotSSAEdges(InstructionNode* instruction);
Dragos Sbirlea7b89bc02013-08-05 16:24:57 -070047 void ToDotSSAEdges(PhiInstructionNode* instruction);
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070048 void Visit(SeaGraph* graph) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070049 dot_text_ += "digraph seaOfNodes {\ncompound=true\n";
50 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070051 void Visit(SignatureNode* parameter);
Dragos Sbirlea64479192013-08-01 15:38:43 -070052
53 // Appends to @result a dot language formatted string representing the node and
54 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
55 // builds a complete dot graph (without prolog and epilog though).
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070056 void Visit(Region* region);
57 void Visit(InstructionNode* instruction);
58 void Visit(PhiInstructionNode* phi);
59 void Visit(UnnamedConstInstructionNode* instruction);
Dragos Sbirlea64479192013-08-01 15:38:43 -070060
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070061 void Visit(ConstInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070062 Visit(reinterpret_cast<InstructionNode*>(instruction));
63 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070064 void Visit(ReturnInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070065 Visit(reinterpret_cast<InstructionNode*>(instruction));
66 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070067 void Visit(IfNeInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070068 Visit(reinterpret_cast<InstructionNode*>(instruction));
69 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070070 void Visit(MoveResultInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070071 Visit(reinterpret_cast<InstructionNode*>(instruction));
72 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070073 void Visit(InvokeStaticInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070074 Visit(reinterpret_cast<InstructionNode*>(instruction));
75 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070076 void Visit(AddIntInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070077 Visit(reinterpret_cast<InstructionNode*>(instruction));
78 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070079 void Visit(GotoInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070080 Visit(reinterpret_cast<InstructionNode*>(instruction));
81 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070082 void Visit(IfEqzInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070083 Visit(reinterpret_cast<InstructionNode*>(instruction));
84 }
85
86 std::string GetResult() const {
87 return dot_text_;
88 }
89
90 private:
91 std::string dot_text_;
92 SeaGraph* graph_;
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070093 art::SafeMap<int, const Type*>* types_;
Dragos Sbirlea64479192013-08-01 15:38:43 -070094 const DotConversionOptions* const options_;
95};
96
97// Stores options for turning a SEA IR graph to a .dot file.
98class DotConversion {
99 public:
100 DotConversion(): options_() { }
101 // Saves to @filename the .dot representation of @graph with the options @options.
Dragos Sbirlea6547fa92013-08-05 18:33:30 -0700102 void DumpSea(SeaGraph* graph, std::string filename,
103 art::SafeMap<int, const Type*>* types) const {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -0700104 LOG(INFO) << "Starting to write SEA string to file " << filename << std::endl;
Dragos Sbirlea64479192013-08-01 15:38:43 -0700105 DotGenerationVisitor dgv = DotGenerationVisitor(&options_, types);
106 graph->Accept(&dgv);
Ian Rogers700a4022014-05-19 16:49:03 -0700107 // TODO: std::unique_ptr to close file properly. Switch to BufferedOutputStream.
Dragos Sbirlea90af14d2013-08-15 17:50:16 -0700108 art::File* file = art::OS::CreateEmptyFile(filename.c_str());
Dragos Sbirlea64479192013-08-01 15:38:43 -0700109 art::FileOutputStream fos(file);
110 std::string graph_as_string = dgv.GetResult();
111 graph_as_string += "}";
112 fos.WriteFully(graph_as_string.c_str(), graph_as_string.size());
113 LOG(INFO) << "Written SEA string to file.";
114 }
115
116 private:
117 DotConversionOptions options_;
118};
119
120} // namespace sea_ir
121#endif // ART_COMPILER_SEA_IR_DEBUG_DOT_GEN_H_