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/>. */
27 #include <gpython/gpython.h>
28 #include <gpython/objects.h>
29 #include <gpython/vectors.h>
30 #include <gpython/garbage.h>
32 gpy_vector_t
* gpy_primitives
;
33 gpy_vector_t
* gpy_namespace_vec
;
35 /* Used for stack-traces ... */
36 gpy_vector_t
* gpy_call_stack
;
38 void gpy_rr_init_primitives (void)
40 gpy_primitives
= (gpy_vector_t
*)
41 gpy_malloc( sizeof(gpy_vector_t
) );
42 gpy_vec_init( gpy_primitives
);
44 gpy_obj_integer_mod_init( gpy_primitives
);
47 void gpy_dump_current_stack_trace (void)
52 void gpy_rr_register_callable (gpy_std_callable call
, int nargs
,
55 gpy_object_t
* c
= NULL_OBJECT
;
57 gpy_callable_t
* call_
= (gpy_callable_t
*)
58 gpy_malloc (sizeof(gpy_callable_t
));
65 gpy_malloc (sizeof(gpy_object_t
));
69 gpy_hashval_t h
= gpy_dd_hash_string (ident
);
71 gpy_hash_entry_t
* f
= gpy_rr_lookup_decl (h
, gpy_namespace_vec
);
74 gpy_object_t
* prev
= f
->data
;
75 gpy_free (prev
); // fix later
80 void ** r
= gpy_dd_hash_insert (h
, c
, Gpy_Vec_Head (gpy_namespace_vec
,
83 fatal ("error registering decl <%s>!\n", ident
);
87 void gpy_rr_register_decl (char * ident
)
89 gpy_hashval_t h
= gpy_dd_hash_string (ident
);
90 gpy_hash_entry_t
* f
= gpy_rr_lookup_decl (h
, gpy_namespace_vec
);
93 gpy_object_t
* o
= (gpy_object_t
*)
94 gpy_malloc (sizeof (gpy_object_t
));
96 o
->o
.object_state
= NULL
;
98 void ** r
= gpy_dd_hash_insert (h
, o
, Gpy_Vec_Head (gpy_namespace_vec
,
101 fatal ("error registering decl <%s>!\n", ident
);
105 void gpy_rr_set_decl_val (char *ident
, gpy_object_t
*o
)
107 gpy_hashval_t h
= gpy_dd_hash_string (ident
);
108 gpy_hash_entry_t
* f
= gpy_rr_lookup_decl (h
, gpy_namespace_vec
);
112 gpy_object_t
* prev
= f
->data
;
113 gpy_rr_decr_ref_count (prev
);
118 void ** r
= gpy_dd_hash_insert (h
, o
, Gpy_Vec_Head (gpy_namespace_vec
,
121 fatal ("error registering decl <%s>!\n", ident
);
125 void gpy_rr_init_runtime (void)
128 Setup runtime namespace Init builtin's
130 gpy_rr_init_primitives ();
132 gpy_namespace_vec
= (gpy_vector_t
*)
133 gpy_malloc( sizeof(gpy_vector_t
) );
134 gpy_vec_init( gpy_namespace_vec
);
136 gpy_call_stack
= (gpy_vector_t
*)
137 gpy_malloc(sizeof(gpy_vector_t
));
138 gpy_vec_init( gpy_call_stack
);
140 gpy_hash_tab_t
* head
= (gpy_hash_tab_t
*)
141 gpy_malloc (sizeof(gpy_hash_tab_t
));
142 gpy_dd_hash_init_table (&head
);
143 gpy_vec_push( gpy_namespace_vec
, head
);
146 void gpy_rr_cleanup_final( void )
148 gpy_rr_pop_context ();
150 if( gpy_namespace_vec
->length
> 0 )
151 error( "<%i> un-free'd conexts!\n", gpy_namespace_vec
->length
);
153 gpy_vec_free (gpy_namespace_vec
);
154 gpy_vec_free (gpy_primitives
);
155 gpy_vec_free (gpy_call_stack
);
158 gpy_object_t
* gpy_rr_fold_integer (int x
)
160 gpy_object_t
* retval
= NULL_OBJECT
;
162 gpy_object_t
** args
= (gpy_object_t
**)
163 gpy_calloc(2, sizeof(gpy_object_t
*));
166 i
.type
= TYPE_INTEGER
;
167 i
.literal
.integer
= x
;
169 gpy_object_t a1
= { .T
= TYPE_OBJECT_LIT
, .o
.literal
= &i
};
170 gpy_object_t a2
= { .T
= TYPE_NULL
, .o
.literal
= NULL
};
175 gpy_typedef_t
* Int_def
= (gpy_typedef_t
*)
176 gpy_primitives
->vector
[ 0 ];
177 gpy_assert (Int_def
);
179 retval
= Int_def
->init_hook (Int_def
, args
);
182 debug("initilized integer object <%p> to <%i>!\n",
184 gpy_assert (retval
->T
== TYPE_OBJECT_STATE
);
190 * int fd: we could use bit masks to represent:
193 void gpy_rr_eval_print (int fd
, int count
, ...)
198 gpy_object_t
* it
= NULL
;
199 for( idx
= 0; idx
<count
; ++idx
)
201 it
= va_arg( vl
, gpy_object_t
* );
202 gpy_assert(it
->T
== TYPE_OBJECT_STATE
);
203 struct gpy_typedef_t
* definition
= it
->o
.object_state
->definition
;
208 (*definition
).print_hook( it
, stdout
, false );
212 (*definition
).print_hook( it
, stderr
, false );
216 fatal("invalid print file-descriptor <%i>!\n", fd
);
221 fprintf( stdout
, "\n" );
226 void gpy_rr_incr_ref_count (gpy_object_t
* x1
)
228 gpy_assert( x1
->T
== TYPE_OBJECT_STATE
);
229 gpy_object_state_t
* x
= x1
->o
.object_state
;
231 debug("incrementing ref count on <%p>:<%i> to <%i>!\n",
232 (void*) x
, x
->ref_count
, (x
->ref_count
+ 1) );
237 void gpy_rr_decr_ref_count (gpy_object_t
* x1
)
239 gpy_assert( x1
->T
== TYPE_OBJECT_STATE
);
240 gpy_object_state_t
* x
= x1
->o
.object_state
;
242 debug("decrementing ref count on <%p>:<%i> to <%i>!\n",
243 (void*) x
, x
->ref_count
, (x
->ref_count
- 1) );
247 void gpy_rr_push_context (void)
249 gpy_hash_tab_t
* head
= (gpy_hash_tab_t
*)
250 gpy_malloc (sizeof(gpy_hash_tab_t
));
251 gpy_dd_hash_init_table (&head
);
253 gpy_vec_push (gpy_namespace_vec
, head
);
256 void gpy_rr_pop_context (void)
258 gpy_hash_tab_t
* head
= Gpy_Vec_Head (gpy_namespace_vec
,
260 gpy_hash_entry_t
* array
= head
->array
;
262 unsigned int idx
= 0;
263 for( ; idx
<(head
->length
); ++idx
)
265 gpy_hash_entry_t i
= array
[idx
];
268 gpy_object_t
* o
= i
.data
;
269 gpy_rr_decr_ref_count (o
);
273 gpy_garbage_invoke_sweep (gpy_namespace_vec
);
275 /* Loop over for stragglers like returns which need pushed up a
276 context soo they can still be garbage collected....
278 straggler is something which will have a (ref_count > 0) after
279 this set of decreasing references...
283 gpy_hash_tab_t
* popd
= gpy_vec_pop (gpy_namespace_vec
);
287 void gpy_rr_finalize_block_decls (int n
, ...)
292 /* gpy_object_t is a typedef of gpy_object_state_t *
293 to keep stdarg.h happy
295 gpy_object_t
* it
= NULL
;
296 for (idx
= 0; idx
<n
; ++idx
)
298 it
= va_arg (vl
, gpy_object_t
*);
299 gpy_assert (it
->T
== TYPE_OBJECT_STATE
);
300 gpy_rr_decr_ref_count (it
);
305 gpy_object_t
* gpy_rr_fold_call (gpy_callable_t
* callables
,
309 gpy_object_t
* retval
= NULL
;
316 gpy_object_t
* gpy_rr_eval_dot_operator (gpy_object_t
* x
,
322 gpy_object_t
* gpy_rr_eval_expression (gpy_object_t
* x1
,
326 char * op_str
= NULL
;
327 gpy_object_t
* retval
= NULL
;
329 gpy_assert(x1
->T
== TYPE_OBJECT_STATE
);
330 gpy_assert(y1
->T
== TYPE_OBJECT_STATE
);
331 gpy_object_state_t
* x
= x1
->o
.object_state
;
332 gpy_object_state_t
* y
= y1
->o
.object_state
;
334 struct gpy_typedef_t
* def
= x1
->o
.object_state
->definition
;
335 struct gpy_number_prot_t
* binops
= (*def
).binary_protocol
;
336 struct gpy_number_prot_t binops_l
= (*binops
);
338 debug ("Eval expression!\n");
345 case OP_BIN_ADDITION
:
351 fatal("unhandled binary operation <%x>!\n", op
);
356 x
->definition
->print_hook( x1
, stdout
, false );
357 fprintf(stdout
, "%s", op_str
);
358 y
->definition
->print_hook( y1
, stdout
, true );
366 fprintf(stdout
, "evaluated to: ");
367 retval
->o
.object_state
->definition
->print_hook (retval
, stdout
, false);
368 fprintf(stdout
, "!\n");
374 fatal("object type <%s> has no binary protocol!\n",