File: privatise.cpp

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (197 lines) | stat: -rw-r--r-- 5,315 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/



#include "sigtype.hh"
#include "compatibility.hh"
#include <stdio.h>

#include "sigprint.hh"
#include "sigtyperules.hh"
#include "privatise.hh"


/*****************************************************************************
						 privatise : compile a list of signals
*****************************************************************************/

static Tree makePrivatisationKey(const Tree& t);
static Tree makePrivatisationLabel(const Tree& exp);

static Tree privatisation (const Tree& k, const Tree& t);
static Tree computePrivatisation (const Tree& k, const Tree& t);
static Tree labelize(const Tree& label, const Tree& exp);


Tree privatise(const Tree& t)
{

	return privatisation(makePrivatisationKey(t), t);
}


// -- implementation -----------------

static Tree makePrivatisationKey(const Tree& t)
{
	char 	name[256];
	snprintf(name, 256, "PRIVATISE %p : ", (CTree*)t);
	return tree(unique(name));
}

static Tree makePrivatisationLabel(const Tree& t)
{
	char 	name[256];
	snprintf(name, 256, "OWNER IS %p : ", (CTree*)t);
	return tree(unique(name));
}


// -- implementation -----------------

static Tree privatisation (const Tree& k, const Tree& t)
{
	Tree v;

	if (t->arity() == 0) {
		return t;

	} else if (getProperty(t, k, v)) {
		/*	Terme deja visit. La proprit nous indique
			la version privatise ou nil si elle est identique
			au terme initial.
		*/
		return isNil(v) ? t : v;

	} else {
		/*	Calcul du terme privatis et mis  jour
			de la proprit. Nil indique que le terme
			privatis est identique  celui de depart
			(pour eviter les boucles avec les compteurs
			de references)
		*/
		v = computePrivatisation(k,t);
		if (v != t) {
			setProperty(t, k, v );
		} else {
			setProperty(t, k, nil);
		}
		return v;
	}
}

static Tree computePrivatisation(const Tree& k, const Tree& exp)
{
	Tree	tbl, size, idx, wrt, content, id, var, body;

	if ( isSigWRTbl(exp, id, tbl, idx, wrt) ) 	{
		/*	Ce qui ne peut pas tre partag, ce sont les
			tables dans lesquelles on ecrit. Pour cela
			on leur donne un label unique
		*/
		return sigWRTbl(
					id,
					labelize( makePrivatisationLabel(exp), privatisation(k, tbl) ),
					privatisation(k, idx),
					privatisation(k, wrt) );

	} else if ( isSigTable(exp, id, size, content) ) {
		/*	Rien  privatiser dans une table (car size est
			cense etre une expression entiere)
		*/
		return exp;

	} else if ( isSigGen(exp, content) ) {
		/*	On ne visite pas les contenus des tables
		*/
		printf("erreur 1 dans computePrivatisation\n");
		exit(1);

	} else if ( isRec(exp, var, body) ) {
		/*	On ne visite pas les contenus des tables
		*/
		setProperty(exp, k, nil);
		return rec(var, privatisation(k,body));

	} else {
		/*	On parcours les autres arbres en privatisant les branches
		*/
		int n = exp->arity();

		switch (n) {

			case 1 :
				return tree(
						exp->node(),
						privatisation(k, exp->branch(0)) );
			case 2 :
				return tree(
						exp->node(),
						privatisation(k, exp->branch(0)),
						privatisation(k, exp->branch(1)) );
			case 3 :
				return tree (
						exp->node(),
						privatisation(k, exp->branch(0)),
						privatisation(k, exp->branch(1)),
						privatisation(k, exp->branch(2)) );
			case 4 :
				return tree (
						exp->node(),
						privatisation(k, exp->branch(0)),
						privatisation(k, exp->branch(1)),
						privatisation(k, exp->branch(2)),
						privatisation(k, exp->branch(3)) );
		}
		printf("erreur 2 dans computePrivatisation\n");
		exit(1);
	}
	printf("situation anormale dans computePrivatisation\n");
	return exp;
}

static Tree labelize(const Tree& newid, const Tree& exp)
{
	Tree	tbl, size, idx, wrt, content, oldid;

	if ( isSigWRTbl(exp, oldid, tbl, idx, wrt) ) 	{
		/*	Ce qui ne peut pas tre partag, ce sont les
			tables dans lesquelles on ecrit. Pour cela
			on leur donne un label unique
		*/
		return sigWRTbl(newid, tbl, idx, wrt);

	} else  if ( isSigTable(exp, oldid, size, content) ) {
		/*	Rien  privatiser dans une table (car size est
			cense etre une expression entiere)
		*/
		return sigTable(newid, size, content);

	} else {

		printf("erreur labelize\n");
		exit(1);
	}

	return exp;
}