FAUST compiler  0.9.9.6b8
shlysis.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 /*****************************************************************************
25 ******************************************************************************
26  Tree Sharing Analysis
27  Y. Orlarey, (c) Grame 2002
28 ------------------------------------------------------------------------------
29 The sharing analysis of tree t is the annotation of all its subtrees t'
30 with their number of occurences in t. As this annotation of t' depends of
31 a context (the tree t for which t' is a subtree) a specific property key
32 unique to each sharing analysis must be generated.
33 
34  API:
35  ----
36 
37  shprkey(t) -> k = unique sharing property key of t
38  shcount(k,t') -> n = returns the number of occurences of t' in t (where k = shprkey(t))
39  shlysis(t) -> k = annotated the subtrees of t with prop (key sharing-count)
40  (0 if t' is not a subtree of t)
41 
42  History :
43  ---------
44  2002-04-08 : First version
45  2006-03-25 : Modifications for new symbolic rec trees
46 
47 ******************************************************************************
48 *****************************************************************************/
49 
58 #include <string.h>
59 #include <stdlib.h>
60 #include <stdio.h>
61 
62 #include "shlysis.hh"
63 #include "compatibility.hh"
64 
70 {
71  char name[256];
72  snprintf(name, 256, "SHARED IN %p : ", (CTree*)t);
73  return tree(unique(name));
74 }
75 
76 
81 int shcount(Tree key, Tree t)
82 {
83  Tree c;
84  if (getProperty(t, key, c)) {
85  return c->node().getInt();
86  } else {
87  return 0;
88  }
89 }
90 
91 
92 
93 //------------------------------------------------------------------------------
94 // Create a specific property key for the sharing count of subtrees of t
95 //------------------------------------------------------------------------------
96 
97 static void annotate(Tree k, Tree t, barrier foo);
98 
99 static bool nobarrier (const Tree& t) { return false; }
100 
106 {
107  Tree k = shprkey(t);
108  annotate(k, t, foo);
109  return k;
110 }
111 
112 
118 {
119  Tree k = shprkey(t);
120  annotate(k, t, nobarrier);
121  return k;
122 }
123 
124 
129 static void annotate(Tree k, Tree t, barrier foo)
130 {
131  cerr << "Annotate " << *t << endl;
132  int c = shcount(k,t);
133  if (c==0) {
134  // First visit
135  Tree var, body;
136  if (isRec(t, var, body)) {
137  // special case for recursive trees
138  setProperty(t, k, tree(1));
139  annotate(k, body, foo);
140  return;
141  } else {
142  int n = t->arity();
143  if (n>0 && ! foo(t)) {
144  for (int i=0; i<n; i++) annotate(k, t->branch(i), foo);
145  }
146  }
147  } else {
148  //printf(" annotate %p with %d\n", (CTree*)t, c+1);
149  }
150  setProperty(t, k, tree(c+1));
151 }
Symbol * unique(const char *str)
Returns a new unique symbol of name strxxx.
Definition: symbol.hh:97
Tree shlysis(Tree t, barrier foo)
Do a sharing analysis : annotates all the subtrees of t with there occurences.
Definition: shlysis.cpp:105
Tree shprkey(Tree t)
Create a specific property key for the sharing count of subtrees of t.
Definition: shlysis.cpp:69
static bool nobarrier(const Tree &t)
Definition: shlysis.cpp:99
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
const Node & node() const
return the content of the tree
Definition: tree.hh:143
int shcount(Tree key, Tree t)
Return the value of sharing count or 0.
Definition: shlysis.cpp:81
bool(* barrier)(const Tree &t)
Definition: shlysis.hh:58
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
bool isRec(Tree t, Tree &body)
is t a de Bruijn recursive tree
Tree tree(const Node &n)
Definition: tree.hh:186
int getInt() const
Definition: node.hh:104
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145
static void annotate(Tree k, Tree t, barrier foo)
Recursively increment the occurences count of t and its subtrees.
Definition: shlysis.cpp:129
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423