From c504d9aa7289eb7cb5aef6e12035c72f0f0648a7 Mon Sep 17 00:00:00 2001 From: redbrain Date: Tue, 15 Mar 2011 19:38:40 +0000 Subject: [PATCH] fixes to strings in generic usuage --- gcc/python/py-decl.c | 7 +-- gcc/python/py-runtime.c | 126 +++++++++++++++++++++++++++++++++------- gcc/python/py-runtime.h | 6 ++ gcc/python/py-stmt.c | 73 +++-------------------- gcc/python/py-types.c | 7 +++ gcc/python/py-types.h | 2 + libgpython/runtime/py-runtime.c | 14 +++-- 7 files changed, 138 insertions(+), 97 deletions(-) diff --git a/gcc/python/py-decl.c b/gcc/python/py-decl.c index 36209a26c01..8d08fbec7b9 100644 --- a/gcc/python/py-decl.c +++ b/gcc/python/py-decl.c @@ -73,12 +73,6 @@ VEC(tree,gc) * gpy_fold_primitive (const gpy_symbol_obj * const sym) VEC(tree,gc) * gpy_fold_call (tree decl, location_t l) { VEC(tree,gc) * retval = VEC_alloc(tree,gc,0); - /*gcc_assert (TREE_TYPE(decl) == FUNCTION_DECL); - - tree address = build_decl (sym->loc, VAR_DECL, create_tmp_var_name("C"), - gpy_object_type_ptr); - tree call = build_call_expr (decl, 0, NULL_TREE); */ - return retval; } @@ -122,6 +116,7 @@ VEC(tree,gc) * gpy_process_assign (gpy_symbol_obj ** op_a, gpy_symbol_obj ** op_ VEC_safe_push (tree, gc, retval, build2 (MODIFY_EXPR, gpy_object_type_ptr, decl,rhs_tree_res_decl)); VEC_safe_push (tree, gc, retval, gpy_builtin_get_incr_ref_call (decl)); + VEC_safe_push (tree, gc, retval, gpy_builtin_get_set_decl_call (decl)); VEC_safe_push (tree, gc, retval, decl); } diff --git a/gcc/python/py-runtime.c b/gcc/python/py-runtime.c index 9386ae3a8dd..8f4a8455346 100644 --- a/gcc/python/py-runtime.c +++ b/gcc/python/py-runtime.c @@ -209,28 +209,6 @@ tree gpy_builtin_get_decr_ref_call( tree x ) return ( build_call_expr( gpy_decr_ref_decl, 1, x ) ); } -tree gpy_builtin_get_set_callable_call (tree decl) -{ - tree params = NULL_TREE; - - chainon( params, tree_cons (NULL_TREE, gpy_callable_type_ptr, NULL_TREE) ); - chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) ); - - tree fntype = build_function_type( void_type_node, params ); - tree gpy_set_callables_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL, - get_identifier("gpy_rr_set_callables"), - fntype ); - tree restype = TREE_TYPE( gpy_set_callables_decl ); - tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, - restype ); - DECL_CONTEXT( resdecl ) = gpy_set_callables_decl; - DECL_RESULT( gpy_set_callables_decl ) = resdecl; - DECL_EXTERNAL( gpy_set_callables_decl ) = 1; - TREE_PUBLIC( gpy_set_callables_decl ) = 1; - - return ( build_call_expr( gpy_set_callables_decl, 1, decl ) ); -} - tree gpy_builtin_get_print_call( int n, tree * args ) { tree params = NULL_TREE; @@ -308,6 +286,110 @@ tree gpy_builtin_get_finalize_block_call( int n, tree * args ) n+1, vec ) ); } +tree gpy_builtin_get_set_decl_call (tree decl) +{ + tree params = NULL_TREE; + + chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, gpy_object_type_ptr, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE)); + + tree fntype = build_function_type (void_type_node, params); + tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, + get_identifier("gpy_rr_set_decl_val"), + fntype); + + tree restype = TREE_TYPE (gpy_rr_decl); + tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, + restype); + + DECL_CONTEXT (resdecl ) = gpy_rr_decl; + DECL_RESULT (gpy_rr_decl ) = resdecl; + DECL_EXTERNAL (gpy_rr_decl ) = 1; + TREE_PUBLIC (gpy_rr_decl ) = 1; + + const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl)); + debug ("c_ident = <%s>!\n", c_ident); + + tree str = build_string (strlen (c_ident), c_ident); + TREE_TYPE (str) = gpy_const_char_ptr; + tree ident = build_fold_addr_expr (str); + + return build_call_expr (gpy_rr_decl, 2, ident, decl); +} + +tree gpy_builtin_get_register_decl_call (tree decl) +{ + tree params = NULL_TREE; + gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); + + chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE)); + + tree fntype = build_function_type (void_type_node, params); + tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, + get_identifier("gpy_rr_register_decl"), + fntype); + + tree restype = TREE_TYPE (gpy_rr_decl); + tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, + restype); + + DECL_CONTEXT (resdecl ) = gpy_rr_decl; + DECL_RESULT (gpy_rr_decl ) = resdecl; + DECL_EXTERNAL (gpy_rr_decl ) = 1; + TREE_PUBLIC (gpy_rr_decl ) = 1; + + const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl)); + debug ("c_ident = <%s>!\n", c_ident); + + tree str = build_string (strlen (c_ident), c_ident); + TREE_TYPE (str) = gpy_const_char_ptr; + tree ident = build_fold_addr_expr (str); + + + return build_call_expr (gpy_rr_decl, 1, ident); +} + +tree gpy_builtin_get_register_callable_call (tree decl, int n) +{ + tree params = NULL_TREE; + gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); + + chainon (params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE)); + chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE)); + + tree fntype = build_function_type (void_type_node, params); + tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, + get_identifier("gpy_rr_register_callable"), + fntype); + + tree restype = TREE_TYPE (gpy_rr_decl); + tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, + restype); + + DECL_CONTEXT (resdecl ) = gpy_rr_decl; + DECL_RESULT (gpy_rr_decl ) = resdecl; + DECL_EXTERNAL (gpy_rr_decl ) = 1; + TREE_PUBLIC (gpy_rr_decl ) = 1; + + const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl)); + debug ("c_ident = <%s>!\n", c_ident); + + tree str = build_string (strlen (c_ident), c_ident); + TREE_TYPE (str) = gpy_const_char_ptr; + tree ident = build_fold_addr_expr (str); + TREE_TYPE (ident) = gpy_const_char_ptr; + + return build_call_expr (gpy_rr_decl, 3, + build_fold_addr_expr (decl), + build_int_cst (integer_type_node, n), + ident); +} + + tree gpy_builtin_get_fold_call_call (const char * ident, int n, tree *args) { diff --git a/gcc/python/py-runtime.h b/gcc/python/py-runtime.h index 3caf3064503..0be84ac2f88 100644 --- a/gcc/python/py-runtime.h +++ b/gcc/python/py-runtime.h @@ -43,4 +43,10 @@ extern tree gpy_builtin_get_eval_expression_call (tree, tree, gpy_opcode_t); extern tree gpy_builtin_get_fold_call_call (const char *, int, tree *); +extern tree gpy_builtin_get_set_decl_call (tree); + +extern tree gpy_builtin_get_register_decl_call (tree); + +extern tree gpy_builtin_get_register_callable_call (tree, int); + #endif /* __RUNTIME_H__ */ diff --git a/gcc/python/py-stmt.c b/gcc/python/py-stmt.c index c9ee0a8a1ea..100e3b8d49b 100644 --- a/gcc/python/py-stmt.c +++ b/gcc/python/py-stmt.c @@ -56,7 +56,7 @@ VEC(gpy_ctx_t,gc) * gpy_ctx_table; VEC(tree,gc) * global_decls; VEC(tree,gc) * gpy_toplev_fncs; -void gpy_process_decl( gpy_symbol_obj * sym ) +void gpy_process_decl (gpy_symbol_obj * sym) { /* Push the declaration! */ VEC_safe_push( gpy_sym, gc, gpy_decls, sym ); @@ -340,7 +340,7 @@ VEC(tree,gc) * gpy_process_functor (const gpy_symbol_obj * const functor, gpy_init_ctx_branch( &co ); VEC_safe_push( gpy_ctx_t, gc, context, co ); - while( o ) + while (o) { /* looping over the gpy_symbol_obj block of function statements and getting the respective tree's and creating the GENERIC block @@ -355,7 +355,7 @@ VEC(tree,gc) * gpy_process_functor (const gpy_symbol_obj * const functor, o = o->next; } - for( ; VEC_iterate( gpy_ident,co->decl_t, idx, it ); ++idx ) + for (; VEC_iterate (gpy_ident,co->decl_t, idx, it); ++idx) { /* get all block var_decls */ tree x = gpy_ctx_lookup_decl( context, it->ident); @@ -366,7 +366,7 @@ VEC(tree,gc) * gpy_process_functor (const gpy_symbol_obj * const functor, declare_vars = x; } - if( declare_vars != NULL_TREE ) + if (declare_vars != NULL_TREE) { tree bl = make_node(BLOCK); BLOCK_SUPERCONTEXT(bl) = retval; @@ -441,8 +441,8 @@ VEC(tree,gc) * gpy_process_class( gpy_symbol_obj * const sym, return retval; } -VEC(tree,gc) * gpy_get_tree( gpy_symbol_obj * sym, - VEC(gpy_ctx_t,gc) * context ) +VEC(tree,gc) * gpy_get_tree (gpy_symbol_obj * sym, + VEC(gpy_ctx_t,gc) * context) { VEC(tree,gc) * retval = NULL; @@ -513,7 +513,7 @@ tree gpy_main_method_decl (VEC(tree,gc) * block, gpy_context_branch * co) tree declare_vars = NULL_TREE; tree main_stmts = alloc_stmt_list( ); - append_to_statement_list (gpy_builtin_get_init_call( ), &main_stmts); + append_to_statement_list (gpy_builtin_get_init_call (), &main_stmts); tree it = NULL_TREE; for( idx = 0; VEC_iterate(tree,block,idx,it); ++idx ) @@ -623,64 +623,9 @@ void gpy_write_globals (void) } } - tree itx = NULL_TREE; - VEC(constructor_elt,gc) *array_data = VEC_alloc(constructor_elt, gc, - VEC_length(tree,gpy_function_decls) + 1); - constructor_elt* elt; - - int iax = 0; - for (idx = 0; VEC_iterate (tree,gpy_function_decls,idx,itx); ++idx) - { - tree id = DECL_NAME (itx); - gcc_assert (TREE_CODE(id) == IDENTIFIER_NODE); - debug("test = <%s>!\n", IDENTIFIER_POINTER(id)); - - tree elent = gpy_init_callable_record (id, 2, itx); - elt = VEC_quick_push(constructor_elt, array_data, NULL); - elt->index = build_int_cst (integer_type_node, iax); - elt->value = elent; - - iax++; - } - elt = VEC_quick_push(constructor_elt, array_data, NULL); - elt->index = build_int_cst (integer_type_node, iax); - elt->value = gpy_init_callable_record (NULL_TREE, 0, NULL_TREE); - - tree array_type = build_array_type (gpy_callable_type, - build_index_type (build_int_cst - (integer_type_node, - VEC_length(tree,gpy_function_decls)) + 1)); - - tree callable_var_decl = build_decl (BUILTINS_LOCATION,VAR_DECL, - get_identifier("__gpy_module_main_callables"), - array_type); - - TREE_STATIC (callable_var_decl) = 1; - TREE_USED (callable_var_decl) = 1; - TREE_READONLY (callable_var_decl) = 1; - TREE_CONSTANT (callable_var_decl) = 1; - TREE_READONLY (callable_var_decl) = 1; - - tree array = build_constructor (array_type, array_data); - debug_tree (array); - DECL_INITIAL (callable_var_decl) = array; - - rest_of_decl_compilation (callable_var_decl,1,0); - gpy_preserve_from_gc (callable_var_decl); - - VEC_safe_push (tree,gc,global_decls,callable_var_decl); - - VEC(tree,gc) * main_stmts_vec__ = VEC_alloc(tree,gc,0); - VEC_safe_push (tree, gc, main_stmts_vec__, - gpy_builtin_get_set_callable_call(callable_var_decl) ); - - for ( idx = 0; VEC_iterate(tree,main_stmts_vec,idx,itx); ++idx) - { - VEC_safe_push(tree,gc,main_stmts_vec__,itx); - } - + tree itx = NULL_TREE; /* Add in the main method decl! */ - VEC_safe_push (tree,gc,global_decls,gpy_main_method_decl (main_stmts_vec__,co)); + VEC_safe_push (tree,gc,global_decls,gpy_main_method_decl (main_stmts_vec,co)); VEC_pop( gpy_ctx_t, gpy_ctx_table ); diff --git a/gcc/python/py-types.c b/gcc/python/py-types.c index 864b4120b87..40226fc5cc4 100644 --- a/gcc/python/py-types.c +++ b/gcc/python/py-types.c @@ -331,6 +331,11 @@ void gpy_initilize_types (void) { gpy_builtin_types_vec = VEC_alloc(tree,gc,0); + tree const_char_type = build_qualified_type(unsigned_char_type_node, + TYPE_QUAL_CONST); + tree ctype = build_pointer_type(const_char_type); + gpy_preserve_from_gc(ctype); + VEC_safe_push (tree,gc,gpy_builtin_types_vec, gpy_build_py_object_type ()); @@ -342,4 +347,6 @@ void gpy_initilize_types (void) VEC_safe_push (tree,gc,gpy_builtin_types_vec, gpy_build_callable_record_type_ptr ()); + + VEC_safe_push (tree,gc,gpy_builtin_types_vec,ctype); } diff --git a/gcc/python/py-types.h b/gcc/python/py-types.h index 96bdbbac952..b5ae330cc12 100644 --- a/gcc/python/py-types.h +++ b/gcc/python/py-types.h @@ -27,6 +27,8 @@ extern VEC(tree,gc) * gpy_builtin_types_vec; #define gpy_callable_type_ptr VEC_index(tree,gpy_builtin_types_vec,3) +#define gpy_const_char_ptr VEC_index(tree,gpy_builtin_types_vec,4) + extern tree gpy_init_callable_record (tree, int, tree); extern void gpy_initilize_types (void); diff --git a/libgpython/runtime/py-runtime.c b/libgpython/runtime/py-runtime.c index c7a310776a0..9b811bf199f 100644 --- a/libgpython/runtime/py-runtime.c +++ b/libgpython/runtime/py-runtime.c @@ -73,7 +73,6 @@ void gpy_rr_register_callable (gpy_std_callable call, int nargs, { gpy_object_t * prev = f->data; gpy_free (prev); // fix later - f->data = c; } else @@ -111,11 +110,16 @@ void gpy_rr_set_decl_val (char *ident, gpy_object_t *o) if (f) { gpy_object_t * prev = f->data; - gpy_free (prev); // fix later... + gpy_rr_decr_ref_count (prev); f->data = o; } else - fatal ("decl <%s> is un-registered to a current context!\n", ident); + { + void ** r = gpy_dd_hash_insert (h, o, Gpy_Vec_Head (gpy_namespace_vec, + gpy_hash_tab_t *)); + if (r) + fatal ("error registering decl <%s>!\n", ident); + } } void gpy_rr_init_runtime (void) @@ -219,7 +223,7 @@ void gpy_rr_eval_print (int fd, int count, ...) } inline -void gpy_rr_incr_ref_count( gpy_object_t * x1 ) +void gpy_rr_incr_ref_count (gpy_object_t * x1) { gpy_assert( x1->T == TYPE_OBJECT_STATE ); gpy_object_state_t * x = x1->o.object_state; @@ -230,7 +234,7 @@ void gpy_rr_incr_ref_count( gpy_object_t * x1 ) } inline -void gpy_rr_decr_ref_count( gpy_object_t * x1 ) +void gpy_rr_decr_ref_count (gpy_object_t * x1) { gpy_assert( x1->T == TYPE_OBJECT_STATE ); gpy_object_state_t * x = x1->o.object_state; -- 2.11.4.GIT