some function decl stuff
[official-gcc.git] / gcc / python / py-vec.c
blob65e4440237826f3232c6e5cbb065b27cf1715d2e
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3, or (at your option) any later
6 version.
8 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #include "config.h"
18 #include "system.h"
19 #include "ansidecl.h"
20 #include "coretypes.h"
21 #include "opts.h"
22 #include "tree.h"
23 #include "gimple.h"
24 #include "toplev.h"
25 #include "debug.h"
26 #include "options.h"
27 #include "flags.h"
28 #include "convert.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
32 #include "target.h"
34 #include <gmp.h>
35 #include <mpfr.h>
37 #include "vec.h"
38 #include "hashtab.h"
40 #include "gpython.h"
41 #include "py-dot-codes.def"
42 #include "py-dot.h"
43 #include "py-vec.h"
44 #include "py-tree.h"
45 #include "py-runtime.h"
48 This is just a basic garbage collection to free all IR symbols
49 created from the parser...
51 VEC(gpy_sym,gc) * gpy_garbage_decls;
52 VEC(tree,gc) * gpy_function_decls;
53 #define threshold_alloc(x) (((x)+16)*3/2)
55 void gpy_gg_invoke_garbage( void )
57 int idx = 0; gpy_symbol_obj * it = NULL;
58 for( ; idx<VEC_iterate(gpy_sym,gpy_garbage_decls,idx,it ); ++idx )
60 gpy_garbage_free_obj( &it );
61 it = NULL;
65 void gpy_garbage_free_obj (gpy_symbol_obj ** sym)
67 if( sym )
69 debug("deleting object <%p>!\n", (void *) (*sym) );
71 if( (*sym)->identifier )
73 debug("garbage symbol identifier '%s'\n", (*sym)->identifier );
74 free( (*sym)->identifier );
77 switch( (*sym)->op_a_t )
79 case TYPE_STRING:
80 if( (*sym)->op_a.string )
82 char *xstr= (char*) (*sym)->op_a.string;
83 free( xstr );
85 break;
87 case TYPE_SYMBOL:
88 gpy_garbage_free_obj( &((*sym)->op_a.symbol_table) );
89 break;
91 default:
92 break;
95 switch( (*sym)->op_b_t )
98 case TYPE_STRING:
99 if( (*sym)->op_b.string )
101 char *xstr= (char*) (*sym)->op_b.string;
102 free( xstr );
104 break;
106 case TYPE_SYMBOL:
107 gpy_garbage_free_obj( &((*sym)->op_b.symbol_table) );
108 break;
110 default:
111 break;
114 if( (*sym)->next )
115 gpy_garbage_free_obj( &((*sym)->next) );
117 free( (*sym) );
118 (*sym) = NULL;
122 gpy_hashval_t gpy_dd_hash_string (const char * s)
124 const unsigned char *str = (const unsigned char *) s;
125 gpy_hashval_t r = 0;
126 unsigned char c;
128 while ((c = *str++) != 0)
129 r = r * 67 + c - 113;
131 return r;
134 gpy_hash_entry_t *
135 gpy_dd_hash_lookup_table( gpy_hash_tab_t * tbl, gpy_hashval_t h )
137 gpy_hash_entry_t* retval = NULL;
138 if( tbl->array )
140 gpy_hashval_t size= tbl->size, idx= (h % size);
141 gpy_hash_entry_t *array= tbl->array;
143 while( array[idx].data )
145 if( array[idx].hash == h )
146 break;
148 idx++;
149 if( idx >= size )
150 idx= 0;
152 retval= (array+idx);
154 else
155 retval= NULL;
157 return retval;
160 void ** gpy_dd_hash_insert( gpy_hashval_t h, void * obj,
161 gpy_hash_tab_t *tbl )
163 void **retval = NULL;
164 gpy_hash_entry_t *entry = NULL;
165 if( tbl->length >= tbl->size )
166 gpy_dd_hash_grow_table( tbl );
168 entry= gpy_dd_hash_lookup_table( tbl, h );
169 if( entry->data )
170 retval= &( entry->data );
171 else
173 entry->data= obj;
174 entry->hash= h;
175 tbl->length++;
177 return retval;
180 void gpy_dd_hash_grow_table( gpy_hash_tab_t * tbl )
182 unsigned int prev_size= tbl->size, size= 0, i= 0;
183 gpy_hash_entry_t *prev_array= tbl->array, *array;
185 size = threshold_alloc( prev_size );
186 array = (gpy_hash_entry_t*)
187 xcalloc( size, sizeof(gpy_hash_entry_t) );
189 tbl->length = 0; tbl->size= size;
190 tbl->array= array;
192 for ( ; i<prev_size; ++i )
194 gpy_hashval_t h= prev_array[i].hash;
195 void *s= prev_array[i].data;
197 if( s )
198 gpy_dd_hash_insert( h, s, tbl );
200 if( prev_array )
201 free( prev_array );
204 inline
205 void gpy_dd_hash_init_table( gpy_hash_tab_t ** tbl )
207 if( tbl )
209 gpy_hash_tab_t *tb= *tbl;
210 tb->size= 0; tb->length= 0;
211 tb->array= NULL;
215 void gpy_ident_vec_init( gpy_ident_vector_t * const v )
217 v->size = threshold_alloc( 0 );
218 v->vector = (void**) xcalloc( v->size, sizeof(void*) );
219 v->length = 0;
222 void gpy_ident_vec_push( gpy_ident_vector_t * const v, void * s )
224 if( s )
226 if( v->length >= v->size )
228 signed long size = threshold_alloc( v->size );
229 v->vector = (void**) xrealloc( v->vector, size*sizeof(void*) );
230 v->size = size;
232 v->vector[ v->length ] = s;
233 v->length++;
237 void * gpy_ident_vec_pop( gpy_ident_vector_t * const v )
239 void * retval = v->vector[ v->length-1 ];
240 v->length--;
241 return retval;
244 inline
245 void gpy_init_ctx_branch( gpy_context_branch * const * o )
247 if( o )
249 (*o)->decls = (gpy_hash_tab_t *)
250 xmalloc( sizeof(gpy_hash_tab_t) );
252 gpy_dd_hash_init_table( &((*o)->decls) );
254 (*o)->decl_t = VEC_alloc(gpy_ident,gc,0);
258 void gpy_init_context_tables( void )
260 gpy_context_branch *o = (gpy_context_branch *)
261 xmalloc( sizeof(gpy_context_branch) );
263 gpy_init_ctx_branch( &o );
265 VEC_safe_push( gpy_ctx_t, gc, gpy_ctx_table, o );
268 bool gpy_ctx_push_decl( tree decl, const char * s,
269 gpy_context_branch * ctx)
271 bool retval = true; gpy_hash_tab_t *t = NULL;
272 gpy_hashval_t h = 0;
274 void ** slot = NULL;
275 gpy_ident o = (gpy_ident) xmalloc( sizeof(gpy_ident_t) );
276 o->ident = xstrdup( s );
278 debug("ident <%s> at <%p>!\n", s, (void*)o );
280 t = ctx->decls;
281 debug("trying to push decl <%s>!\n", s );
283 h = gpy_dd_hash_string( o->ident );
284 slot = gpy_dd_hash_insert( h, decl, t );
285 if( !slot )
287 debug("successfully pushed DECL <%s>!\n", s);
288 VEC_safe_push( gpy_ident, gc, ctx->decl_t, o );
290 if (TREE_CODE(decl) == FUNCTION_DECL)
292 VEC_safe_push (tree,gc,gpy_function_decls,decl);
295 else
297 debug("decl <%s> already pushed!\n", s );
298 retval = false;
301 return retval;
304 tree gpy_ctx_lookup_decl (VEC(gpy_ctx_t,gc) * context, const char * s)
306 tree retval = NULL;
307 unsigned int n_ctx = VEC_length( gpy_ctx_t,context );
308 int idx = 0; gpy_context_branch * it = NULL;
310 gpy_hashval_t h = gpy_dd_hash_string( s );
311 debug( "trying to lookup <%s> : context table length = <%i>!\n",
312 s, n_ctx );
314 for( idx=(n_ctx-1); idx>=0; --idx )
316 it = VEC_index( gpy_ctx_t, context, idx );
318 gpy_hash_entry_t * o = NULL;
319 gpy_hash_tab_t * decl_table = NULL;
321 decl_table = it->decls;
323 o = gpy_dd_hash_lookup_table( decl_table, h );
324 if( o )
326 if( o->data )
328 debug("found symbol <%s> in context <%p>!\n", s, (void*)it );
329 retval = (tree) (o->data) ;
330 break;
334 return retval;