• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 package com.android.tools.r8.utils;
5 
6 import java.util.Stack;
7 
8 /**
9  * Printer abstraction for creating flow-graphs for the C1 visualizer.
10  */
11 public class CfgPrinter {
12 
13   private final StringBuilder builder = new StringBuilder();
14   private final Stack<String> opened = new Stack<>();
15   private final int indentSpacing = 2;
16 
17   public int nextUnusedValue = 0;
18 
makeUnusedValue()19   public String makeUnusedValue() {
20     return "_" + nextUnusedValue++;
21   }
22 
resetUnusedValue()23   public void resetUnusedValue() {
24     nextUnusedValue = 0;
25   }
26 
begin(String title)27   public CfgPrinter begin(String title) {
28     print("begin_");
29     append(title).ln();
30     opened.push(title);
31     return this;
32   }
33 
end(String title)34   public CfgPrinter end(String title) {
35     String top = opened.pop();
36     assert title.equals(top);
37     print("end_");
38     append(title).ln();
39     return this;
40   }
41 
print(int i)42   public CfgPrinter print(int i) {
43     printIndent();
44     builder.append(i);
45     return this;
46   }
47 
print(String string)48   public CfgPrinter print(String string) {
49     printIndent();
50     builder.append(string);
51     return this;
52   }
53 
append(int i)54   public CfgPrinter append(int i) {
55     builder.append(i);
56     return this;
57   }
58 
append(String string)59   public CfgPrinter append(String string) {
60     builder.append(string);
61     return this;
62   }
63 
sp()64   public CfgPrinter sp() {
65     builder.append(" ");
66     return this;
67   }
68 
ln()69   public CfgPrinter ln() {
70     builder.append("\n");
71     return this;
72   }
73 
printIndent()74   private void printIndent() {
75     for (int i = 0; i < opened.size() * indentSpacing; i++) {
76       builder.append(" ");
77     }
78   }
79 
80   @Override
toString()81   public String toString() {
82     return builder.toString();
83   }
84 }
85