FAUST compiler  0.9.9.6b8
sigToGraph.cpp
Go to the documentation of this file.
1 /************************************************************************
2  ************************************************************************
3  FAUST compiler
4  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 
26 #include <set>
27 #include <vector>
28 #include <iostream>
29 #include <sstream>
30 #include <string>
31 
32 #include "signals.hh"
33 #include "sigtype.hh"
34 #include "sigtyperules.hh"
35 #include "xtended.hh"
36 
37 #include "sigToGraph.hh"
38 
39 using namespace std;
40 
41 static void recdraw(Tree sig, set<Tree>& drawn, ofstream& fout );
42 static string nodeattr(Type t);
43 static string edgeattr(Type t);
44 static string sigLabel(Tree sig);
45 
46 
50 void sigToGraph (Tree L, ofstream& fout)
51 {
52  set<Tree> alreadyDrawn;
53 
54  fout << "strict digraph loopgraph {\n"
55  << " rankdir=LR; node [fontsize=10];"
56  << endl;
57  int out = 0;
58  while (isList(L)) {
59  recdraw(hd(L), alreadyDrawn, fout);
60 
61  fout << "OUTPUT_" << out << "[color=\"red2\" style=\"filled\" fillcolor=\"pink\"];" << endl;
62  fout << 'S' << hd(L) << " -> " << "OUTPUT_" << out++ << "[" << edgeattr(getCertifiedSigType(hd(L))) << "];" << endl;
63  L = tl(L);
64  }
65 
66  fout << "}" << endl;
67 }
68 
69 
70 /******************************* IMPLEMENTATION ***********************************/
71 
72 
76 static void recdraw(Tree sig, set<Tree>& drawn, ofstream& fout )
77 {
78  //cerr << ++TABBER << "ENTER REC DRAW OF " << sig << "$" << *sig << endl;
79  vector<Tree> subsig;
80  int n;
81 
82  if (drawn.count(sig) == 0) {
83  drawn.insert(sig);
84  if (isList(sig)) {
85  do {
86  recdraw(hd(sig), drawn, fout);
87  sig = tl(sig);
88  } while (isList(sig));
89  } else {
90  // draw the node
91  fout << 'S' << sig << "[label=\"" << sigLabel(sig) << "\""
92  << nodeattr(getCertifiedSigType(sig)) << "];"
93  << endl;
94 
95  // draw the subsignals
96  n = getSubSignals(sig, subsig);
97  if (n > 0) {
98  if (n==1 && isList(subsig[0])) {
99  Tree id, body;
100  assert(isRec(sig,id,body));
101  if (!isRec(sig,id,body)) {
102  }
103  // special recursion case, recreate a vector of subsignals instead of the
104  // list provided by getSubSignal
105  Tree L = subsig[0];
106  subsig.clear();
107  n = 0;
108  do {
109  subsig.push_back(hd(L));
110  L = tl(L);
111  n += 1;
112  } while (isList(L));
113  }
114 
115  for (int i=0; i<n; i++) {
116  recdraw(subsig[i], drawn, fout);
117  fout << 'S' << subsig[i] << " -> " << 'S' << sig
118  << "[" << edgeattr(getCertifiedSigType(subsig[i])) << "];"
119  << endl;
120  }
121  }
122  }
123  }
124  //cerr << --TABBER << "EXIT REC DRAW OF " << sig << endl;
125 }
126 
127 
131 static string edgeattr(Type t)
132 {
133  string s;
134 
135  // nature
136  if (t->nature()==kInt) {
137  s += " color=\"blue\"";
138  } else {
139  s += " color=\"red\"";
140  }
141 
142  // vectorability
143  if (t->vectorability()==kVect && t->variability()==kSamp) {
144  s += " style=\"bold\"";
145  }
146  return s;
147 }
148 
149 
153 static string nodeattr(Type t)
154 {
155  string s = edgeattr(t);
156 
157  // variability
158  if (t->variability()==kKonst) {
159  s += " shape=\"box\"";
160  } else if (t->variability()==kBlock) {
161  s += " shape=\"hexagon\"";
162  } else if (t->variability()==kSamp) {
163  s += " shape=\"ellipse\"";
164  }
165 
166  return s;
167 }
168 
169 
173 static const char* binopname[]= {
174  "+", "-", "*", "/", "%",
175  "<<", ">>",
176  ">", "<", ">=", "<=", "==", "!=",
177  "&", "|", "^"
178 };
179 
180 
184 static string sigLabel(Tree sig)
185 {
186  int i;
187  double r;
188  Tree x, y, z, c, type, name, file, ff, largs, id, le, sel, var, label;
189 
190  xtended* p = (xtended*) getUserData(sig);
191 
192  stringstream fout;
193 
194  if (p) { fout << p->name(); }
195  else if ( isSigInt(sig, &i) ) { fout << i; }
196  else if ( isSigReal(sig, &r) ) { fout << r; }
197  else if ( isSigWaveform(sig)) { fout << "waveform"; }
198 
199  else if ( isSigInput(sig, &i) ) { fout << "INPUT_" << i; }
200  else if ( isSigOutput(sig, &i, x) ) { fout << "OUTPUT_" << i; }
201 
202  else if ( isSigDelay1(sig, x) ) { fout << "mem"; }
203  else if ( isSigFixDelay(sig, x, y) ) { fout << "@"; }
204  else if ( isSigPrefix(sig, x, y) ) { fout << "prefix"; }
205  else if ( isSigIota(sig, x) ) { fout << "iota"; }
206  else if ( isSigBinOp(sig, &i, x, y) ) { fout << binopname[i]; }
207  else if ( isSigFFun(sig, ff, largs) ) { fout << "ffunction:" << *ff; }
208  else if ( isSigFConst(sig, type, name, file) ) { fout << *name; }
209  else if ( isSigFVar(sig, type, name, file) ) { fout << *name; }
210 
211  else if ( isSigTable(sig, id, x, y) ) { fout << "table:" << id; }
212  else if ( isSigWRTbl(sig, id, x, y, z) ) { fout << "write:" << id; }
213  else if ( isSigRDTbl(sig, x, y) ) { fout << "read"; }
214 
215 
216 
217  else if ( isSigSelect2(sig, sel, x, y) ) { fout << "select2"; }
218  else if ( isSigSelect3(sig, sel, x, y, z) ) { fout << "select3"; }
219 
220  else if ( isSigGen(sig, x) ) { fout << "generator"; }
221 
222  else if ( isProj(sig, &i, x) ) { fout << "Proj" << i; }
223  else if ( isRec(sig, var, le) ) { fout << "REC " << *var; }
224 
225  else if ( isSigIntCast(sig, x) ) { fout << "int"; }
226  else if ( isSigFloatCast(sig, x) ) { fout << "float"; }
227 #if 0
228  else if ( isSigButton(sig, label) ) { fout << "button \"" << *label << '"'; }
229  else if ( isSigCheckbox(sig, label) ) { fout << "checkbox \"" << *label << '"'; }
230  else if ( isSigVSlider(sig, label,c,x,y,z) ) { fout << "vslider \"" << *label << '"'; }
231  else if ( isSigHSlider(sig, label,c,x,y,z) ) { fout << "hslider \"" << *label << '"'; }
232  else if ( isSigNumEntry(sig, label,c,x,y,z) ) { fout << "nentry \"" << *label << '"'; }
233 
234  else if ( isSigVBargraph(sig, label,x,y,z) ) { fout << "vbargraph \"" << *label << '"'; }
235  else if ( isSigHBargraph(sig, label,x,y,z) ) { fout << "hbargraph \"" << *label << '"'; }
236 #else
237  else if ( isSigButton(sig, label) ) { fout << "button"; }
238  else if ( isSigCheckbox(sig, label) ) { fout << "checkbox"; }
239  else if ( isSigVSlider(sig, label,c,x,y,z) ) { fout << "vslider"; }
240  else if ( isSigHSlider(sig, label,c,x,y,z) ) { fout << "hslider"; }
241  else if ( isSigNumEntry(sig, label,c,x,y,z) ) { fout << "nentry"; }
242 
243  else if ( isSigVBargraph(sig, label,x,y,z) ) { fout << "vbargraph"; }
244  else if ( isSigHBargraph(sig, label,x,y,z) ) { fout << "hbargraph"; }
245 #endif
246  else if ( isSigAttach(sig, x, y) ) { fout << "attach"; }
247 
248  else {
249  cerr << "ERROR, unrecognized signal : " << *sig << endl;
250  exit(1);
251  }
252 
253  return fout.str();
254 }
bool isSigHSlider(Tree s)
Definition: signals.cpp:217
bool isSigRDTbl(Tree s, Tree &t, Tree &i)
Definition: signals.cpp:77
void sigToGraph(Tree L, ofstream &fout)
Draw a list of signals as a directed graph using graphviz's dot language.
Definition: sigToGraph.cpp:50
bool isSigFConst(Tree s)
Definition: signals.cpp:138
bool isSigPrefix(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:66
bool isSigCheckbox(Tree s)
Definition: signals.cpp:205
bool isSigFFun(Tree s, Tree &ff, Tree &largs)
Definition: signals.cpp:133
static string sigLabel(Tree sig)
return the label of a signal as a string
Definition: sigToGraph.cpp:184
bool isSigNumEntry(Tree s)
Definition: signals.cpp:257
Definition: sigtype.hh:58
bool isSigSelect2(Tree t, Tree &selector, Tree &s1, Tree &s2)
Definition: signals.cpp:116
bool isSigIota(Tree t, Tree &t0)
Definition: signals.cpp:70
Definition: sigtype.hh:54
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
bool isSigInput(Tree t, int *i)
Definition: signals.cpp:48
bool isSigFixDelay(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:62
Tree hd(Tree l)
Definition: list.hh:133
const char * name()
Definition: xtended.hh:25
bool isSigVBargraph(Tree s)
Definition: signals.cpp:285
bool isSigSelect3(Tree t, Tree &selector, Tree &s1, Tree &s2, Tree &s3)
Definition: signals.cpp:119
bool isSigVSlider(Tree s)
Definition: signals.cpp:237
static string edgeattr(Type t)
Convert a signal type into edge attributes.
Definition: sigToGraph.cpp:131
bool isSigGen(Tree t, Tree &x)
Definition: signals.cpp:91
static const char * binopname[]
translate signal binary operations into strings
Definition: sigToGraph.cpp:173
bool isSigReal(Tree t, double *r)
Definition: signals.cpp:44
bool isSigHBargraph(Tree s)
Definition: signals.cpp:279
static void recdraw(Tree sig, set< Tree > &drawn, ofstream &fout)
Draw recursively a signal.
Definition: sigToGraph.cpp:76
bool isList(Tree l)
Definition: list.hh:138
bool isSigTable(Tree t, Tree &id, Tree &n, Tree &sig)
Definition: signals.cpp:85
bool isSigBinOp(Tree s, int *op, Tree &x, Tree &y)
Definition: signals.cpp:126
bool isSigAttach(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:291
Definition: sigtype.hh:56
Type getCertifiedSigType(Tree sig)
Retrieve the type of sig and check it exists.
bool isSigWRTbl(Tree u, Tree &id, Tree &t, Tree &i, Tree &s)
Definition: signals.cpp:81
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
bool isSigWaveform(Tree s)
Definition: signals.cpp:211
bool isRec(Tree t, Tree &body)
is t a de Bruijn recursive tree
bool isSigInt(Tree t, int *i)
Definition: signals.cpp:41
bool isSigDelay1(Tree t, Tree &t0)
Definition: signals.cpp:58
bool isSigFloatCast(Tree t)
Definition: signals.cpp:187
bool isSigFVar(Tree s)
Definition: signals.cpp:144
void * getUserData(Symbol *sym)
Returns user data.
Definition: symbol.hh:100
bool isSigIntCast(Tree t)
Definition: signals.cpp:184
static string nodeattr(Type t)
Convert a signal type into node attributes.
Definition: sigToGraph.cpp:153
bool isProj(Tree t, int *i, Tree &rgroup)
Definition: signals.cpp:151
API to the typing system of signals.
Tree tl(Tree l)
Definition: list.hh:134
int getSubSignals(Tree sig, vector< Tree > &vsigs, bool visitgen=true)
Extract the sub signals of a signal expression, that is not necesseraly all the subtrees.
Definition: subsignals.cpp:12
bool isSigOutput(Tree t, int *i, Tree &t0)
Definition: signals.cpp:52
bool isSigButton(Tree s)
Definition: signals.cpp:199