*** empty log message ***
[chuck-blob.git] / exile / v1 / src / chuck_temp.cpp
blob4ed78750fc59e2f28131769bd1d4527eca882590
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_temp.c
27 // desc: functions to create and manipulate temporary variables which are
28 // used in the IR tree representation before it has been determined
29 // which variables are to go into registers.
31 // author: Andrew Appel (appel@cs.princeton.edu)
32 // modified: Ge Wang (gewang@cs.princeton.edu)
33 // Perry R. Cook (prc@cs.princeton.edu)
34 // date: Autumn 2002
35 //-----------------------------------------------------------------------------
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "chuck_symbol.h"
40 #include "chuck_table.h"
41 #include "chuck_temp.h"
46 //-----------------------------------------------------------------------------
47 // name: struct Temp_Temp_
48 // desc: ...
49 //-----------------------------------------------------------------------------
50 struct Temp_Temp_
52 int num;
58 //-----------------------------------------------------------------------------
59 // name: Temp_Label_string()
60 // desc: ...
61 //-----------------------------------------------------------------------------
62 c_str Temp_Label_string( Temp_Label s )
64 return S_name( s );
71 static int labels = 0;
73 //-----------------------------------------------------------------------------
74 // name: Temp_new_label()
75 // desc: ...
76 //-----------------------------------------------------------------------------
77 Temp_Label Temp_new_label( void )
79 char buf[100];
80 sprintf( buf,"L%d",labels++ );
81 return Temp_named_label( ( c_str )buf );
87 //-----------------------------------------------------------------------------
88 // name: Temp_named_label()
89 // desc: the label will be created only if it is not found.
90 //-----------------------------------------------------------------------------
91 Temp_Label Temp_named_label( c_str s )
93 return insert_symbol( s );
100 static int temps = 100;
102 //-----------------------------------------------------------------------------
103 // name: Temp_new_temp( )
104 // desc: ...
105 //-----------------------------------------------------------------------------
106 Temp_Temp Temp_new_temp( void )
108 Temp_Temp p = ( Temp_Temp ) checked_malloc( sizeof ( *p ) );
109 p->num=temps++;
111 char r[16];
112 sprintf( r, "%d", p->num );
113 Temp_enter( Temp_name( ), p, ( c_str )r );
116 return p;
122 //-----------------------------------------------------------------------------
123 // name: struct Temp_Map_
124 // desc: ...
125 //-----------------------------------------------------------------------------
126 struct Temp_Map_
128 TAB_table tab;
129 Temp_Map under;
135 //-----------------------------------------------------------------------------
136 // name: Temp_name( )
137 // desc: ...
138 //-----------------------------------------------------------------------------
139 Temp_Map Temp_name( void )
141 static Temp_Map m = NULL;
142 if ( !m ) m = Temp_empty( );
143 return m;
149 //-----------------------------------------------------------------------------
150 // name: new_map( )
151 // desc: ...
152 //-----------------------------------------------------------------------------
153 Temp_Map new_map( TAB_table tab, Temp_Map under )
155 Temp_Map m = (Temp_Map)checked_malloc( sizeof( *m ) );
156 m->tab=tab;
157 m->under=under;
159 return m;
165 //-----------------------------------------------------------------------------
166 // name: Temp_empty( )
167 // desc: ...
168 //-----------------------------------------------------------------------------
169 Temp_Map Temp_empty( void )
171 return new_map( TAB_empty( ), NULL );
177 //-----------------------------------------------------------------------------
178 // name: Temp_layer_map( )
179 // desc: ...
180 //-----------------------------------------------------------------------------
181 Temp_Map Temp_layer_map( Temp_Map over, Temp_Map under )
183 if ( over==NULL )
184 return under;
185 else
186 return new_map( over->tab, Temp_layer_map( over->under, under ) );
192 //-----------------------------------------------------------------------------
193 // name: Temp_enter( )
194 // desc: ...
195 //-----------------------------------------------------------------------------
196 void Temp_enter( Temp_Map m, Temp_Temp t, c_str s )
198 assert( m && m->tab );
199 TAB_enter( m->tab,t,s );
205 //-----------------------------------------------------------------------------
206 // name: Temp_look( )
207 // desc: ...
208 //-----------------------------------------------------------------------------
209 c_str Temp_look( Temp_Map m, Temp_Temp t )
211 c_str s;
212 assert( m && m->tab );
213 s = (c_str)TAB_look( m->tab, t );
214 if ( s ) return s;
215 else if ( m->under ) return Temp_look( m->under, t );
216 else return NULL;
222 //-----------------------------------------------------------------------------
223 // name: Temp_new_temp_list( )
224 // desc: ...
225 //-----------------------------------------------------------------------------
226 Temp_Temp_List Temp_new_temp_list( Temp_Temp h, Temp_Temp_List t )
228 Temp_Temp_List p = ( Temp_Temp_List ) checked_malloc( sizeof ( *p ) );
229 p->head=h; p->tail=t;
230 return p;
236 //-----------------------------------------------------------------------------
237 // name: Temp_new_label_list( )
238 // desc: ...
239 //-----------------------------------------------------------------------------
240 Temp_Label_List Temp_new_label_list( Temp_Label h, Temp_Label_List t )
242 Temp_Label_List p = ( Temp_Label_List ) checked_malloc( sizeof ( *p ) );
243 p->head=h; p->tail=t;
244 return p;
250 //-----------------------------------------------------------------------------
251 // name: showit( )
252 // desc: ...
253 //-----------------------------------------------------------------------------
254 static FILE *outfile;
255 void showit( Temp_Temp t, c_str r )
257 fprintf( outfile, "t%d -> %s\n", t->num, r );
263 //-----------------------------------------------------------------------------
264 // name: Temp_dump_map( )
265 // desc: ...
266 //-----------------------------------------------------------------------------
267 void Temp_dump_map( FILE *out, Temp_Map m )
269 outfile=out;
270 TAB_dump( m->tab,( void ( * )( void *, void* ) )showit );
271 if ( m->under )
273 fprintf( out,"---------\n" );
274 Temp_dump_map( out,m->under );