FAUST compiler  0.9.9.6b8
occurences.cpp
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdlib.h>
3 #include "recursivness.hh"
4 #include "occurences.hh"
5 #include "sigtype.hh"
6 #include "sigtyperules.hh"
7 #include <iostream>
8 
9 using namespace std;
10 
14 static int xVariability (int v, int r)
15 {
16  //cerr << "xVariability (" << v << ", " << r << ")" << endl;
17  //assert (v < 3); // kKonst=0, kBlock=1, kSamp=2
18  //assert(r==0 | v==2);
19  if (r>1) r=1;
20  return min(3, v + r);
21 }
22 
23 //-------------------------------------------------
24 // Occurences methods
25 //-------------------------------------------------
26 
27 Occurences::Occurences(int v, int r) : fXVariability(xVariability(v,r)) {
28  for (int i=0; i<4; i++) fOccurences[i]=0;
29  fMultiOcc = false;
30  fMaxDelay = 0;
31  fOutDelayOcc = false;
32 }
33 
34 Occurences* Occurences::incOccurences(int v, int r, int d) {
35  int ctxt = xVariability(v,r);
36  //assert (ctxt >= fXVariability);
37  fOccurences[ctxt] += 1;
38  fMultiOcc = fMultiOcc | (ctxt > fXVariability) | (fOccurences[ctxt] > 1);
39  if (d == 0) {
40  //cerr << "Occurence outside a delay " << endl;
41  fOutDelayOcc = true;
42  }
43  if (d > fMaxDelay) {
44  //cerr << "Max delay : " << fMaxDelay << " <- " << d << endl;
45  fMaxDelay = d;
46  }
47  return this;
48 }
49 
50 bool Occurences::hasMultiOccurences() const { return fMultiOcc; }
51 
53 
55 {
56  return fMaxDelay;
57 }
58 
59 //--------------------------------------------------
60 // Mark and retrieve occurences of subtrees of root
61 //--------------------------------------------------
62 
64 {
65  fRootTree = root;
66  fPropKey = tree(unique("OCCURENCES"));
67 
68  if (isList(root)) {
69  while (isList(root)) {
70  //incOcc(kSamp, 1, hd(root));
71  incOcc(nil, kSamp, 0, 0, hd(root));
72  root = tl(root);
73  }
74  //cerr << "END OF LIST IS " << *root << endl;
75  } else {
76  //incOcc(kSamp, 1, root);
77  incOcc(nil, kSamp, 0, 0, root);
78  }
79 }
80 
82 {
83  Occurences* p = getOcc(t);
84  if (p == 0) {
85  //cerr << "No Occurences info attached to : " << *t << endl;
86  //exit(1);
87  }
88  return p;
89 }
90 
91 //------------------------------------------------------------------------------
92 // Increment the occurences of t within context v,r,d and proceed recursively
93 //------------------------------------------------------------------------------
94 
95 void OccMarkup::incOcc(Tree env, int v, int r, int d, Tree t)
96 {
97  // Check if we have already visited this tree
98  Occurences* occ = getOcc(t);
99 
100  if (occ==0) {
101  // 1) We build initial occurence information
102  Type ty = getCertifiedSigType(t);
103  int v0 = ty->variability();
104  int r0 = getRecursivness(t);
105 
106  occ = new Occurences(v0,r0);
107  setOcc(t, occ);
108 
109  // We mark the subtrees of t
110  Tree c, x, y, z;
111  if (isSigFixDelay(t,x,y)) {
112  Type g2 = getCertifiedSigType(y);
113  int d2 = checkDelayInterval(g2);
114  assert(d2>=0);
115  incOcc(env, v0, r0, d2, x);
116  incOcc(env, v0, r0, 0, y);
117  } else if (isSigPrefix(t,y,x)) {
118  incOcc(env, v0, r0, 1, x);
119  incOcc(env, v0, r0, 0, y);
120  } else if (isSigSelect3(t,c,y,x,z)) {
121  // make a special case for select3 implemented with real if
122  // because the c expression will be used twice in the C++
123  // translation
124  incOcc(env, v0, r0, 0, c);
125  incOcc(env, v0, r0, 0, c);
126  incOcc(env, v0, r0, 0, x);
127  incOcc(env, v0, r0, 0, y);
128  incOcc(env, v0, r0, 0, z);
129  } else {
130  vector<Tree> br;
131  int n = getSubSignals(t, br);
132  if (n>0 && ! isSigGen(t)) {
133  for (int i=0; i<n; i++) incOcc(env, v0, r0, 0, br[i]);
134  }
135  }
136  }
137 
138  occ->incOccurences(v,r,d);
139 
140 }
141 
142 
143 
145 {
146  Tree p = t->getProperty(fPropKey);
147  if (p) {
148  return (Occurences*) tree2ptr(p);
149  } else {
150  return 0;
151  }
152 }
153 
154 
156 {
157  t->setProperty(fPropKey, tree(occ));
158 }
159 
160 
161 
168 static int position (Tree env, Tree t, int p)
169 {
170  if (isNil(env)) return 0; // was not in the environment
171  if (hd(env) == t) return p;
172  else return position (tl(env), t, p+1);
173 }
void incOcc(Tree env, int v, int r, int d, Tree t)
inc the occurence of t in context v,r
Definition: occurences.cpp:95
int getRecursivness(Tree sig)
Return the recursivness of a previously annotated signal.
Symbol * unique(const char *str)
Returns a new unique symbol of name strxxx.
Definition: symbol.hh:97
static int position(Tree env, Tree t, int p)
return the position of a signal in the current recursive environment
Definition: occurences.cpp:168
bool isSigPrefix(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:66
Occurences(int v, int r)
Definition: occurences.cpp:27
bool hasOutDelayOccurences() const
true if has occurences outside a a delay
Definition: occurences.cpp:52
Tree fRootTree
occurences computed within this tree
Definition: occurences.hh:34
Tree fPropKey
key used to store occurences property
Definition: occurences.hh:35
void mark(Tree root)
start markup of root tree with new unique key
Definition: occurences.cpp:63
const int fXVariability
Extended Variability of the expression.
Definition: occurences.hh:9
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
int fOccurences[4]
Occurences count according to Contexts.
Definition: occurences.hh:10
bool isSigFixDelay(Tree t, Tree &t0, Tree &t1)
Definition: signals.cpp:62
Tree hd(Tree l)
Definition: list.hh:133
Occurences * retrieve(Tree t)
occurences of subtree t within root tree
Definition: occurences.cpp:81
bool isSigSelect3(Tree t, Tree &selector, Tree &s1, Tree &s2, Tree &s3)
Definition: signals.cpp:119
bool hasMultiOccurences() const
true if multiple occurences or occ. in higher ctxt
Definition: occurences.cpp:50
bool isSigGen(Tree t, Tree &x)
Definition: signals.cpp:91
void setOcc(Tree t, Occurences *occ)
set Occurences property of t
Definition: occurences.cpp:155
bool isNil(Tree l)
Definition: list.hh:137
static int xVariability(int v, int r)
Extended Variability with recursiveness indication.
Definition: occurences.cpp:14
int checkDelayInterval(Type t)
Check is a type is appropriate for a delay.
Definition: sigtype.cpp:327
bool isList(Tree l)
Definition: list.hh:138
bool fMultiOcc
True when exp has multiple occ. or occ. in higher ctxt.
Definition: occurences.hh:11
void setProperty(Tree key, Tree value)
Definition: tree.hh:167
Definition: sigtype.hh:56
Type getCertifiedSigType(Tree sig)
Retrieve the type of sig and check it exists.
Occurences * incOccurences(int v, int r, int d)
inc occurences in context v,r,d
Definition: occurences.cpp:34
Tree tree(const Node &n)
Definition: tree.hh:186
void * tree2ptr(Tree t)
if t has a node of type ptr, return it otherwise error
Definition: tree.cpp:288
Occurences * getOcc(Tree t)
get Occurences property of t or null
Definition: occurences.cpp:144
int getMaxDelay() const
return the maximal delay collected
Definition: occurences.cpp:54
Tree nil
Definition: list.cpp:116
int fMaxDelay
Maximal fix delay usage.
Definition: occurences.hh:14
Tree getProperty(Tree key)
Definition: tree.hh:173
bool fOutDelayOcc
True when exp has at least one occ. outside a delay.
Definition: occurences.hh:12
API to the typing system of signals.
double min(double x, double y)
Definition: interval.hh:59
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