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
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
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/>. */
20 #include "coretypes.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
41 #include "py-dot-codes.def"
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
);
65 void gpy_garbage_free_obj (gpy_symbol_obj
** 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
)
80 if( (*sym
)->op_a
.string
)
82 char *xstr
= (char*) (*sym
)->op_a
.string
;
88 gpy_garbage_free_obj( &((*sym
)->op_a
.symbol_table
) );
95 switch( (*sym
)->op_b_t
)
99 if( (*sym
)->op_b
.string
)
101 char *xstr
= (char*) (*sym
)->op_b
.string
;
107 gpy_garbage_free_obj( &((*sym
)->op_b
.symbol_table
) );
115 gpy_garbage_free_obj( &((*sym
)->next
) );
122 gpy_hashval_t
gpy_dd_hash_string (const char * s
)
124 const unsigned char *str
= (const unsigned char *) s
;
128 while ((c
= *str
++) != 0)
129 r
= r
* 67 + c
- 113;
135 gpy_dd_hash_lookup_table( gpy_hash_tab_t
* tbl
, gpy_hashval_t h
)
137 gpy_hash_entry_t
* retval
= NULL
;
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
)
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
);
170 retval
= &( entry
->data
);
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
;
192 for ( ; i
<prev_size
; ++i
)
194 gpy_hashval_t h
= prev_array
[i
].hash
;
195 void *s
= prev_array
[i
].data
;
198 gpy_dd_hash_insert( h
, s
, tbl
);
205 void gpy_dd_hash_init_table( gpy_hash_tab_t
** tbl
)
209 gpy_hash_tab_t
*tb
= *tbl
;
210 tb
->size
= 0; tb
->length
= 0;
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*) );
222 void gpy_ident_vec_push( gpy_ident_vector_t
* const v
, void * 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*) );
232 v
->vector
[ v
->length
] = s
;
237 void * gpy_ident_vec_pop( gpy_ident_vector_t
* const v
)
239 void * retval
= v
->vector
[ v
->length
-1 ];
245 void gpy_init_ctx_branch( gpy_context_branch
* const * 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
;
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
);
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
);
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
);
297 debug("decl <%s> already pushed!\n", s
);
304 tree
gpy_ctx_lookup_decl (VEC(gpy_ctx_t
,gc
) * context
, const char * s
)
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",
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
);
328 debug("found symbol <%s> in context <%p>!\n", s
, (void*)it
);
329 retval
= (tree
) (o
->data
) ;