initial
[prop.git] / app / test_vcg / cfg.cc
blob08132f93545b28a3642e6ca50cc284ee306c8567
1 #include <stdlib.h>
2 #include <iostream.h>
3 #include <strstream.h>
4 #include <fstream.h>
5 #include <AD/visualize/vcg.h>
7 //
8 // Visualize a CFG
9 //
11 class VisualizeCFG : public VCG
13 public:
14 VisualizeCFG() {}
16 VisualizeCFG& Start(const char * bb)
17 { begin_node(bb); label("START"); color(lightblue);
18 shape(ellipse); end_node(bb); return *this;
21 VisualizeCFG& Stop(const char * bb)
22 { begin_node(bb); label("STOP"); color(lightblue);
23 shape(ellipse); end_node(bb); return *this;
26 VisualizeCFG& Compute(const char * bb,const char * code)
27 { begin_node(bb); label(code); color(lightgreen); end_node(bb);
28 return *this;
31 VisualizeCFG& Test(const char * bb, const char * test)
32 { begin_node(bb); label(test); color(lightyellow);
33 shape(rhomb); end_node(bb); return *this;
36 VisualizeCFG& TrueBranch(const char * a, const char * b)
37 { begin_edge(a,b); label("true"); end_edge(a,b); return *this; }
39 VisualizeCFG& BackTrueBranch(const char * a, const char * b)
40 { begin_backedge(a,b); label("true"); end_edge(a,b); return *this; }
42 VisualizeCFG& FalseBranch(const char * a, const char * b)
43 { begin_edge(a,b); label("false"); end_edge(a,b); return *this; }
45 VisualizeCFG& BackFalseBranch(const char * a, const char * b)
46 { begin_backedge(a,b); label("false"); end_edge(a,b); return *this; }
48 VisualizeCFG& Jump(const char * a, const char * b)
49 { begin_edge(a,b); end_edge(a,b); return *this; }
51 VisualizeCFG& BackJump(const char * a, const char * b)
52 { begin_backedge(a,b); end_edge(a,b); return *this; }
54 VisualizeCFG& BeginRegion(const char * r, const char * l)
55 { begin_graph(r); label(l); color(lightgreen);
56 return *this;
58 VisualizeCFG& EndRegion(const char * r)
59 { end_graph(r);
60 return *this;
64 int main()
66 VisualizeCFG V;
67 ostrstream S;
68 const char * G = "A Sample Graph";
69 const char * a = "a";
70 const char * b = "b";
71 const char * c = "c";
72 const char * d1 = "d1";
73 const char * d2 = "d2";
74 const char * d3 = "d3";
75 const char * d4 = "d4";
76 const char * e = "e";
77 const char * f = "f";
78 const char * g = "g";
79 const char * h = "h";
80 const char * i = "i";
81 const char * r = "r";
83 V . begin_graph(G) . label(G) . display_edge_labels(true)
84 . layoutalgorithm(VCG::minbackward)
85 . xspace(40) . yspace(40);
87 // Create a sample CFG
88 V . Start(a)
89 . Jump(a,b)
90 . Compute(b,"x := y + z;\nq := r * s;")
91 . Jump(b,c)
92 . Test(c,"q > x")
93 . FalseBranch(c,e)
94 . BeginRegion(r,"sub region")
95 . Test(d1,"x > 0")
96 . TrueBranch(d1,d2)
97 . FalseBranch(d1,d3)
98 . Compute(d2,"x := x + 1;\ny := y / z;\nq := q[0];")
99 . Compute(d3,"x := x - 1;\ny := y / z;\nq := q[1];")
100 . Jump(d2,d4)
101 . Jump(d3,d4)
102 . Compute(d4,"y := y + x;\nx := z + 1;")
103 . EndRegion(r)
104 . TrueBranch(c,d1)
105 . Compute(e,"x := x - 2;\nz := x * x;\nq : = q/z;")
106 . Jump(d4,f)
107 . Jump(e,g)
108 . Test(f,"x != 0")
109 . TrueBranch(f,g)
110 . BackFalseBranch(f,b)
111 . Stop(g);
113 V . end_graph(G);
115 // translate it into GDL
116 V.print_GDL_on(S);
118 ofstream TMP("./cfg.vcg");
119 TMP << S.str();
120 TMP.close();
121 system("xvcg ./cfg.vcg");
123 return 0;