FAUST compiler  0.9.9.6b8
sigtype.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 "tree.hh"
25 #include "sigtype.hh"
26 #include "property.hh"
27 
29 
30 bool SimpleType::isMaximal() const
31 {
32  return (fNature==kReal)
33  && (fVariability==kSamp)
34  && (fComputability==kExec);
35 
36 }
37 
38 
39 //------------------------------------------------------------------------------------
40 //
41 // Surcharges de l'operateur d'impression <<
42 //
43 //------------------------------------------------------------------------------------
44 
45 
46 ostream& operator<<(ostream& dst, const Type& t) { return t->print(dst);}
47 
48 ostream& operator<<(ostream& dst, const SimpleType& t) { return t.print(dst); }
49 
50 ostream& operator<<(ostream& dst, const TableType& t) { return t.print(dst); }
51 
52 ostream& operator<<(ostream& dst, const TupletType& t) { return t.print(dst); }
53 
54 
55 //------------------------------------------------------------------------------------
56 //
57 // Definition des methodes d'impression
58 //
59 //------------------------------------------------------------------------------------
60 
61 
65 ostream& SimpleType::print(ostream& dst) const
66 {
67  return dst << "NR"[nature()]
68  << "KB?S"[variability()]
69  << "CI?E"[computability()]
70  << "VS?TS"[vectorability()]
71  << "N?B"[boolean()]
72  << " " << fInterval;
73 }
74 
75 
79 ostream& TableType::print(ostream& dst) const
80 {
81  dst << "KB?S"[variability()]
82  << "CI?E"[computability()]
83  << " " << fInterval
84  << ":Table(";
85  fContent->print(dst);
86  return dst << ')';
87 }
88 
89 
94 {
95  return (fNature==kReal)
96  && (fVariability==kSamp)
97  && (fComputability==kExec);
98 }
99 
100 
101 
105 ostream& TupletType::print(ostream& dst) const
106 {
107  dst << "KB?S"[variability()]
108  << "CI?E"[computability()]
109  << " " << fInterval
110  << " : {";
111  string sep = "";
112  for (unsigned int i = 0; i < fComponents.size(); i++, sep="*") {
113  dst << sep;
114  fComponents[i]->print(dst);
115  }
116  dst << '}';
117  return dst;
118 }
119 
120 
125 {
126  for (unsigned int i = 0; i < fComponents.size(); i++) {
127  if (! fComponents[i]->isMaximal()) return false;
128  }
129  return true;
130 }
131 
132 
133 //------------------------------------------------------------------------------------
134 //
135 // Construction des types
136 // t := p, table(t), t|t, t*t
137 //
138 //------------------------------------------------------------------------------------
139 
140 // Essential predefined types
141 
144 
148 
152 
153 // more predefined types
154 
159 //Type TREC = makeSimpleType(kInt, kSamp, kInit, kVect, kNum, interval()); // kVect ou kScal ?
160 
161 // trying to accelerate type convergence
162 //Type TREC = TINT;
163 Type TREC = makeSimpleType(kInt, kSamp, kInit, kScal, kNum, interval()); // kVect ou kScal ?
164 
165 
166 Type operator| ( const Type& t1, const Type& t2)
167 {
168  SimpleType *st1, *st2;
169  TableType *tt1, *tt2;
170  TupletType *nt1, *nt2;
171 
172  if ( (st1 = isSimpleType(t1)) && (st2 = isSimpleType(t2)) ) {
173 
174  return makeSimpleType( st1->nature()|st2->nature(),
175  st1->variability()|st2->variability(),
176  st1->computability()|st2->computability(),
177  st1->vectorability()|st2->vectorability(),
178  st1->boolean()|st2->boolean(),
179  reunion(st1->getInterval(), st2->getInterval())
180  );
181 
182  } else if ( (tt1 = isTableType(t1)) && (tt2 = isTableType(t2)) ) {
183 
184  return makeTableType( tt1->content() | tt2->content() );
185 
186  } else if ( (nt1 = isTupletType(t1)) && (nt2 = isTupletType(t2)) ) {
187 
188  vector<Type> v;
189  int n = min(nt1->arity(), nt2->arity());
190  for (int i=0; i<n; i++) { v.push_back( (*nt1)[i] | (*nt2)[i]); }
191  return new TupletType( v );
192 
193  } else {
194 
195  cerr << "Error : trying to combine incompatible types, " << t1 << " and " << t2 << endl;
196  exit(1);
197  return 0;
198  }
199 }
200 
201 bool operator== ( const Type& t1, const Type& t2)
202 {
203  SimpleType *st1, *st2;
204  TableType *tt1, *tt2;
205  TupletType *nt1, *nt2;
206 
207  if (t1->variability() != t2->variability()) return false;
208  if (t1->computability() != t2->computability()) return false;
209 
210  if ( (st1 = isSimpleType(t1)) && (st2 = isSimpleType(t2)) )
211  return (st1->nature() == st2->nature())
212  && (st1->variability() == st2->variability())
213  && (st1->computability() == st2->computability())
214  && (st1->vectorability() == st2->vectorability())
215  && (st1->boolean() == st2->boolean())
216  && (st1->getInterval().lo == st2->getInterval().lo)
217  && (st1->getInterval().hi == st2->getInterval().hi)
218  && (st1->getInterval().valid == st2->getInterval().valid);
219  if ( (tt1 = isTableType(t1)) && (tt2 = isTableType(t2)) )
220  return tt1->content()== tt2->content();
221  if ( (nt1 = isTupletType(t1)) && (nt2 = isTupletType(t2)) ) {
222  int a1 = nt1->arity();
223  int a2 = nt2->arity();
224  if (a1 == a2) {
225  for (int i=0; i<a1; i++) { if ((*nt1)[i] != (*nt2)[i]) return false; }
226  return true;
227  } else {
228  return false;
229  }
230  }
231  return false;
232 }
233 
234 bool operator<= ( const Type& t1, const Type& t2)
235 {
236  return (t1|t2) == t2;
237 }
238 
239 
240 
241 Type operator* (const Type& t1, const Type& t2)
242 {
243  vector<Type> v;
244 
245  TupletType* nt1 = dynamic_cast<TupletType*>((AudioType*)t1);
246  TupletType* nt2 = dynamic_cast<TupletType*>((AudioType*)t2);
247 
248  if (nt1) {
249  for (int i=0; i<nt1->arity(); i++) {
250  v.push_back((*nt1)[i]);
251  }
252  } else {
253  v.push_back(t1);
254  }
255 
256  if (nt2) {
257  for (int i=0; i<nt2->arity(); i++) {
258  v.push_back((*nt2)[i]);
259  }
260  } else {
261  v.push_back(t2);
262  }
263  return new TupletType(v);
264 }
265 
266 
267 SimpleType* isSimpleType(AudioType* t) { return dynamic_cast<SimpleType*>(t); }
268 TableType* isTableType(AudioType* t) { return dynamic_cast<TableType*>(t); }
269 TupletType* isTupletType(AudioType* t) { return dynamic_cast<TupletType*>(t); }
270 
271 
272 
273 //--------------------------------------------------
274 // verification de type
275 
277 {
278  // verifie que t est entier
279  SimpleType* st = isSimpleType(t);
280  if (st == 0 || st->nature() > kInt) {
281  cerr << "Error : checkInt failed for type " << t << endl;
282  exit(1);
283  }
284  return t;
285 }
286 
288 {
289  // verifie que t est constant
290  if (t->variability() > kKonst) {
291  cerr << "Error : checkKonst failed for type " << t << endl;
292  exit(1);
293  }
294  return t;
295 }
296 
298 {
299  // verifie que t est connu a l'initialisation
300  if (t->computability() > kInit) {
301  cerr << "Error : checkInit failed for type " << t << endl;
302  exit(1);
303  }
304  return t;
305 }
306 
308 {
309  return checkInit(checkKonst(checkInt(t)));
310 }
311 
313 {
314  // verifie que wr est compatible avec le contenu de tbl
315  if (wr->nature() > tbl->nature()) {
316  cerr << "Error : checkWRTbl failed, the content of " << tbl << " is incompatible with " << wr << endl;
317  exit(1);
318  }
319  return tbl;
320 }
321 
328 {
329  interval i = t->getInterval();
330  if (i.valid && i.lo >= 0) {
331  return int(i.hi+0.5);
332  } else {
333  //cerr << "checkDelayInterval failed for : " << i << endl;
334  return -1;
335  }
336 }
337 
338 
339 // Donne le nom du type C correspondant �la nature d'un signal
340 string cType (Type t)
341 {
342  return (t->nature() == kInt) ? "int" : "float";
343 }
344 
345 
346 
347 /*****************************************************************************
348  *
349  * codeAudioType(Type) -> Tree
350  * Code an audio type as a tree in order to benefit of memoization
351  *
352  *****************************************************************************/
353 
354 // memoized type contruction
355 
357 
358 
359 Sym SIMPLETYPE = symbol ("SimpleType");
360 Sym TABLETYPE = symbol ("TableType");
361 Sym TUPLETTYPE = symbol ("TupletType");
362 
363 static Tree codeSimpleType(SimpleType* st);
364 static Tree codeTableType(TableType* st);
365 static Tree codeTupletType(TupletType* st);
366 
367 
375 {
376  SimpleType *st;
377  TableType *tt;
378  TupletType *nt;
379 
380  Tree r;
381 
382  if ((r=t->getCode())) return r;
383 
384  if ((st = isSimpleType(t))) {
385  r = codeSimpleType(st);
386  } else if ((tt = isTableType(t))) {
387  r = codeTableType(tt);
388  } else if ((nt = isTupletType(t))) {
389  r = codeTupletType(nt);
390  } else {
391  cerr << "ERROR in codeAudioType() : invalide pointer " << t << endl;
392  exit(1);
393  }
394 
395  r->setType(t);
396  return r;
397 
398 }
399 
400 
405 {
406  vector<Tree> elems;
407  elems.push_back(tree(st->nature()));
408  elems.push_back(tree(st->variability()));
409  elems.push_back(tree(st->computability()));
410  elems.push_back(tree(st->vectorability()));
411  elems.push_back(tree(st->boolean()));
412 
413  elems.push_back(tree(st->getInterval().valid));
414  elems.push_back(tree(st->getInterval().lo));
415  elems.push_back(tree(st->getInterval().hi));
416 
417  return CTree::make(SIMPLETYPE, elems);
418 
419 }
420 
421 AudioType* makeSimpleType(int n, int v, int c, int vec, int b, const interval& i)
422 {
423  SimpleType prototype(n,v,c,vec,b,i);
424  Tree code = codeAudioType(&prototype);
425 
426  AudioType* t;
427  if (MemoizedTypes.get(code, t)) {
428  return t;
429  } else {
431  t = new SimpleType(n,v,c,vec,b,i);
432  MemoizedTypes.set(code, t);
433  t->setCode(code);
434  return t;
435  }
436 }
437 
438 
444 {
445  return tree(TABLETYPE, codeAudioType(tt->content()));
446 }
447 
449 {
450  TableType prototype(ct);
451  Tree code = codeAudioType(&prototype);
452 
453  AudioType* tt;
454  if (MemoizedTypes.get(code, tt)) {
455  return tt;
456  } else {
458  tt = new TableType(ct);
459  MemoizedTypes.set(code, tt);
460  tt->setCode(code);
461  return tt;
462  }
463 }
464 
465 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec, int b, const interval& i)
466 {
467  TableType prototype(ct,n,v,c,vec,b,i);
468  Tree code = codeAudioType(&prototype);
469 
470  AudioType* tt;
471  if (MemoizedTypes.get(code, tt)) {
472  return tt;
473  } else {
475  tt = new TableType(ct);
476  MemoizedTypes.set(code, tt);
477  tt->setCode(code);
478  return tt;
479  }
480 }
481 
482 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec)
483 {
484  TableType prototype(ct,n,v,c,vec);
485  Tree code = codeAudioType(&prototype);
486 
487  AudioType* tt;
488  if (MemoizedTypes.get(code, tt)) {
489  return tt;
490  } else {
492  tt = new TableType(ct);
493  MemoizedTypes.set(code, tt);
494  tt->setCode(code);
495  return tt;
496  }
497 }
498 
499 
505 {
506  vector<Tree> elems;
507  for (int i=0; i<nt->arity(); i++) {
508  elems.push_back(codeAudioType((*nt)[i]));
509  }
510  return CTree::make(TUPLETTYPE, elems);
511 }
512 
513 AudioType* makeTupletType(const vector<Type>& vt)
514 {
515  TupletType prototype(vt);
516  Tree code = codeAudioType(&prototype);
517 
518  AudioType* t;
519  if (MemoizedTypes.get(code, t)) {
520  return t;
521  } else {
523  t = new TupletType(vt);
524  MemoizedTypes.set(code, t);
525  t->setCode(code);
526  return t;
527  }
528 
529 }
530 
531 AudioType* makeTupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i)
532 {
533  TupletType prototype(vt,n,v,c,vec,b,i);
534  Tree code = codeAudioType(&prototype);
535 
536  AudioType* t;
537  if (MemoizedTypes.get(code, t)) {
538  return t;
539  } else {
541  t = new TupletType(vt,n,v,c,vec,b,i);
542  MemoizedTypes.set(code, t);
543  t->setCode(code);
544  return t;
545  }
546 
547 }
Type checkInit(Type t)
verifie que t est connu a l'initialisation
Definition: sigtype.cpp:297
A tree library with hashconsing and maximal sharing capabilities.
Type checkKonst(Type t)
verifie que t est constant
Definition: sigtype.cpp:287
bool valid
true if it is a valid interval
Definition: interval.hh:38
int fVariability
how fast values change
Definition: sigtype.hh:88
virtual bool isMaximal() const
true when type is maximal (and therefore can't change depending of hypothesis)
Definition: sigtype.cpp:124
double lo
minimal value
Definition: interval.hh:39
static Tree codeSimpleType(SimpleType *st)
Code a simple audio type as a tree in order to benefit of memoization.
Definition: sigtype.cpp:404
Definition: sigtype.hh:58
int variability() const
returns how fast values change (constant, by blocks, by samples)
Definition: sigtype.hh:105
int fNature
the kind of data represented
Definition: sigtype.hh:87
static int gAllocationCount
Definition: sigtype.hh:85
Definition: sigtype.hh:58
Type TREC
Definition: sigtype.cpp:163
interval getInterval() const
returns the interval (min dn max values) of a signal
Definition: sigtype.hh:110
vector< Type > fComponents
Definition: sigtype.hh:343
The type of a tuplet of data.
Definition: sigtype.hh:340
const Type fContent
type of that data stored in the table
Definition: sigtype.hh:291
property< AudioType * > MemoizedTypes
Definition: sigtype.cpp:356
Definition: sigtype.hh:57
Type content() const
return the type of data store in the table
Definition: sigtype.hh:320
int fComputability
when are values available
Definition: sigtype.hh:89
Definition: sigtype.hh:54
int boolean() const
returns when a signal stands for a boolean value
Definition: sigtype.hh:108
Tree getCode()
returns the interval (min dn max values) of a signal
Definition: sigtype.hh:113
Type INT_TGUI
Definition: sigtype.cpp:158
Type TINT
Definition: sigtype.cpp:142
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
int arity() const
Definition: sigtype.hh:358
Tree codeAudioType(AudioType *t)
codeAudioType(Type) -> Tree Code an audio type as a tree in order to benefit of memoization The type ...
Definition: sigtype.cpp:374
Definition: sigtype.hh:57
static Tree make(const Node &n, int ar, Tree br[])
return a new tree or an existing equivalent one
void setType(void *t)
Definition: tree.hh:157
virtual bool isMaximal() const
true when type is maximal (and therefore can't change depending of hypothesis)
Definition: sigtype.cpp:93
Type operator*(const Type &t1, const Type &t2)
Definition: sigtype.cpp:241
bool operator<=(const Type &t1, const Type &t2)
Definition: sigtype.cpp:234
ostream & operator<<(ostream &dst, const Type &t)
Definition: sigtype.cpp:46
The type of a table of audio data.
Definition: sigtype.hh:288
void set(Tree t, const P &data)
Definition: property.hh:22
Type operator|(const Type &t1, const Type &t2)
Definition: sigtype.cpp:166
AudioType * makeSimpleType(int n, int v, int c, int vec, int b, const interval &i)
Definition: sigtype.cpp:421
Type TGUI01
Definition: sigtype.cpp:157
void setCode(Tree code)
returns the interval (min dn max values) of a signal
Definition: sigtype.hh:112
static Tree codeTupletType(TupletType *st)
Code a tuplet type as a tree in order to benefit of memoization.
Definition: sigtype.cpp:504
Type checkIntParam(Type t)
verifie que t est connu a l'initialisation, constant et entier
Definition: sigtype.cpp:307
Type checkWRTbl(Type tbl, Type wr)
verifie que wr est compatible avec le contenu de tbl
Definition: sigtype.cpp:312
string cType(Type t)
Definition: sigtype.cpp:340
bool operator==(const Type &t1, const Type &t2)
Definition: sigtype.cpp:201
AudioType * makeTupletType(const vector< Type > &vt)
Definition: sigtype.cpp:513
Type TINIT
Definition: sigtype.cpp:150
Definition: sigtype.hh:55
int checkDelayInterval(Type t)
Check is a type is appropriate for a delay.
Definition: sigtype.cpp:327
static Tree codeTableType(TableType *st)
Code a table type as a tree in order to benefit of memoization.
Definition: sigtype.cpp:443
The type of a simple numeric audio signal.
Definition: sigtype.hh:237
Type TKONST
Definition: sigtype.cpp:145
Type TINPUT
Definition: sigtype.cpp:155
Definition: sigtype.hh:54
bool get(Tree t, P &data)
Definition: property.hh:32
AudioType * makeTableType(const Type &ct)
Definition: sigtype.cpp:448
Definition: sigtype.hh:56
interval reunion(const interval &x, const interval &y)
Definition: interval.hh:64
interval fInterval
Minimal and maximal values the signal can take.
Definition: sigtype.hh:93
Type TBLOCK
Definition: sigtype.cpp:146
Type TGUI
Definition: sigtype.cpp:156
TableType * isTableType(AudioType *t)
Definition: sigtype.cpp:268
int nature() const
returns the kind of values (integre or floating point)
Definition: sigtype.hh:104
TupletType * isTupletType(AudioType *t)
Definition: sigtype.cpp:269
virtual ostream & print(ostream &dst) const
print a SimpleType
Definition: sigtype.cpp:65
Type TEXEC
Definition: sigtype.cpp:151
int computability() const
returns when values are available (compilation, initialisation, execution)
Definition: sigtype.hh:106
Tree tree(const Node &n)
Definition: tree.hh:186
Sym TABLETYPE
Definition: sigtype.cpp:360
Type TSAMP
Definition: sigtype.cpp:147
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
Symbol * symbol(const char *str)
Returns (and creates if new) the symbol of name str.
Definition: symbol.hh:95
virtual ostream & print(ostream &dst) const
Print the content of a tuplet of types on a stream.
Definition: sigtype.cpp:105
Type TREAL
Definition: sigtype.cpp:143
SimpleType * isSimpleType(AudioType *t)
Definition: sigtype.cpp:267
Sym SIMPLETYPE
Definition: sigtype.cpp:359
double min(double x, double y)
Definition: interval.hh:59
Definition: sigtype.hh:57
virtual ostream & print(ostream &dst) const
print a TableType
Definition: sigtype.cpp:79
double hi
maximal value
Definition: interval.hh:40
The Root class for all audio data types.
Definition: sigtype.hh:82
int vectorability() const
returns when a signal can be vectorized
Definition: sigtype.hh:107
Type checkInt(Type t)
verifie que t est entier
Definition: sigtype.cpp:276
virtual bool isMaximal() const
true when type is maximal (and therefore can't change depending of hypothesis)
Definition: sigtype.cpp:30
Type TCOMP
Definition: sigtype.cpp:149
Sym TUPLETTYPE
Definition: sigtype.cpp:361