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
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
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)
35 //-----------------------------------------------------------------------------
39 #include "chuck_symbol.h"
40 #include "chuck_table.h"
41 #include "chuck_temp.h"
46 //-----------------------------------------------------------------------------
47 // name: struct Temp_Temp_
49 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 // name: Temp_Label_string()
61 //-----------------------------------------------------------------------------
62 c_str
Temp_Label_string( Temp_Label s
)
71 static int labels
= 0;
73 //-----------------------------------------------------------------------------
74 // name: Temp_new_label()
76 //-----------------------------------------------------------------------------
77 Temp_Label
Temp_new_label( void )
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( )
105 //-----------------------------------------------------------------------------
106 Temp_Temp
Temp_new_temp( void )
108 Temp_Temp p
= ( Temp_Temp
) checked_malloc( sizeof ( *p
) );
112 sprintf( r
, "%d", p
->num
);
113 Temp_enter( Temp_name( ), p
, ( c_str
)r
);
122 //-----------------------------------------------------------------------------
123 // name: struct Temp_Map_
125 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
136 // name: Temp_name( )
138 //-----------------------------------------------------------------------------
139 Temp_Map
Temp_name( void )
141 static Temp_Map m
= NULL
;
142 if ( !m
) m
= Temp_empty( );
149 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
153 Temp_Map
new_map( TAB_table tab
, Temp_Map under
)
155 Temp_Map m
= (Temp_Map
)checked_malloc( sizeof( *m
) );
165 //-----------------------------------------------------------------------------
166 // name: Temp_empty( )
168 //-----------------------------------------------------------------------------
169 Temp_Map
Temp_empty( void )
171 return new_map( TAB_empty( ), NULL
);
177 //-----------------------------------------------------------------------------
178 // name: Temp_layer_map( )
180 //-----------------------------------------------------------------------------
181 Temp_Map
Temp_layer_map( Temp_Map over
, Temp_Map under
)
186 return new_map( over
->tab
, Temp_layer_map( over
->under
, under
) );
192 //-----------------------------------------------------------------------------
193 // name: Temp_enter( )
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( )
208 //-----------------------------------------------------------------------------
209 c_str
Temp_look( Temp_Map m
, Temp_Temp t
)
212 assert( m
&& m
->tab
);
213 s
= (c_str
)TAB_look( m
->tab
, t
);
215 else if ( m
->under
) return Temp_look( m
->under
, t
);
222 //-----------------------------------------------------------------------------
223 // name: Temp_new_temp_list( )
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
;
236 //-----------------------------------------------------------------------------
237 // name: Temp_new_label_list( )
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
;
250 //-----------------------------------------------------------------------------
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( )
266 //-----------------------------------------------------------------------------
267 void Temp_dump_map( FILE *out
, Temp_Map m
)
270 TAB_dump( m
->tab
,( void ( * )( void *, void* ) )showit
);
273 fprintf( out
,"---------\n" );
274 Temp_dump_map( out
,m
->under
);