summaryrefslogtreecommitdiff
path: root/libgpython/runtime/py-runtime.c
blob: 301bcc387c14a950c586227b21c513149b3fb20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>

#include <gpython/gpython.h>
#include <gpython/objects.h>
#include <gpython/vectors.h>
#include <gpython/garbage.h>

gpy_vector_t * gpy_primitives;
gpy_vector_t * gpy_namespace_vec;

/* Used for stack-traces ... */
gpy_vector_t * gpy_call_stack;

void gpy_rr_init_primitives( void )
{
  gpy_primitives = (gpy_vector_t *)
    gpy_malloc( sizeof(gpy_vector_t) );
  gpy_vec_init( gpy_primitives );

  gpy_obj_integer_mod_init( gpy_primitives );
}

void gpy_dump_current_stack_trace (void)
{
  return;
}

void gpy_rr_init_runtime( void ) 
{
  /* 
     Setup runtime namespace Init builtin's
  */
  gpy_rr_init_primitives( );

  gpy_namespace_vec = (gpy_vector_t*)
    gpy_malloc( sizeof(gpy_vector_t) );
  gpy_vec_init( gpy_namespace_vec );

  gpy_call_stack = (gpy_vector_t*)
    gpy_malloc(sizeof(gpy_vector_t));
  gpy_vec_init( gpy_call_stack );

  gpy_context_t * head = (gpy_context_t *)
    gpy_malloc( sizeof(gpy_context_t) );

  head->symbols = (gpy_vector_t*)
    gpy_malloc( sizeof(gpy_vector_t) );
  gpy_vec_init( head->symbols );

  gpy_vec_push( gpy_namespace_vec, head );
}

void gpy_rr_cleanup_final( void )
{
  gpy_rr_pop_context( );

  if( gpy_namespace_vec->length > 0 )
    error( "<%i> un-free'd conexts!\n", gpy_namespace_vec->length );

  gpy_vec_free( gpy_namespace_vec );
  gpy_vec_free( gpy_primitives );
  gpy_vec_free( gpy_call_stack );
}

gpy_object_t * gpy_rr_fold_integer( int x )
{
  gpy_object_t * retval = NULL_OBJECT;

  gpy_object_state_t * o = NULL_OBJ_STATE;
  Gpy_Object_State_Init_Ctx( o, gpy_namespace_vec );

  gpy_object_t ** args = (gpy_object_t **)
    gpy_calloc(2, sizeof(gpy_object_t*));

  gpy_literal_t i;
  i.type = TYPE_INTEGER;
  i.literal.integer = x;

  gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &i };
  gpy_object_t a2 = { .T = TYPE_NULL, .o.literal = NULL };

  args[0] = &a1;
  args[1] = &a2;

  gpy_type_obj_def_t * Int_def = (gpy_type_obj_def_t *)
    gpy_primitives->vector[ 0 ];
  gpy_assert( Int_def );

  retval = Int_def->init_hook (args);
  gpy_free(args);

  debug("initilized integer object <%p> to <%i>!\n",
	(void*)retval, x );

  gpy_assert (retval->T == TYPE_OBJECT_STATE);
  Gpy_Incr_Ref (retval->o.object_state);

  return retval;
}

/**
 * int fd: we could use bit masks to represent:
 *   stdout/stderr ...
 **/
void gpy_rr_eval_print( int fd, int count, ...  )
{
  va_list vl; int idx;
  va_start( vl,count );

  /* gpy_object_t is a typedef of gpy_object_state_t *
     to keep stdarg.h happy
  */
  gpy_object_t * it = NULL;
  for( idx = 0; idx<count; ++idx )
    {
      it = va_arg( vl, gpy_object_t* );
      gpy_assert(it->T == TYPE_OBJECT_STATE);
      struct gpy_type_obj_def_t * definition = it->o.object_state->definition;

      switch( fd )
	{
	case 1:
	  (*definition).print_hook( it, stdout, false );
	  break;

	case 2:
	  (*definition).print_hook( it, stderr, false );
	  break;

	default:
	  fatal("invalid print file-descriptor <%i>!\n", fd );
	  break;
	}
    }

  fprintf( stdout, "\n" );
  va_end(vl);
}

inline
void gpy_rr_incr_ref_count( gpy_object_state_t * x )
{
  gpy_assert( x );
  debug("incrementing ref count on <%p>:<%i> to <%i>!\n",
	(void*) x, x->ref_count, (x->ref_count + 1) );
  x->ref_count++;
}

inline
void gpy_rr_decr_ref_count( gpy_object_state_t * x )
{
  gpy_assert( x );
  debug("decrementing ref count on <%p>:<%i> to <%i>!\n",
	(void*) x, x->ref_count, (x->ref_count - 1) );
  x->ref_count--;
}

void gpy_rr_push_context( void )
{
  gpy_context_t * ctx = (gpy_context_t *)
    gpy_malloc( sizeof(gpy_context_t) );
  ctx->symbols = (gpy_vector_t*)
    gpy_malloc( sizeof(gpy_vector_t) );
  gpy_vec_init( ctx->symbols );

  gpy_vec_push( gpy_namespace_vec, ctx );
}

void gpy_rr_pop_context( void )
{
  gpy_context_t * head = Gpy_Namespace_Head;
  void ** vec = head->symbols->vector;

  unsigned int idx = 0;
  for( ; idx<(head->symbols->length); ++idx )
    {
      gpy_object_state_t * i = (gpy_object_state_t *) vec[ idx ];
      Gpy_Decr_Ref( i );
    }

  gpy_garbage_invoke_sweep( gpy_namespace_vec );

  /* Loop over for stragglers like returns which need pushed up a
     context soo they can still be garbage collected....
     --
     straggler is something which will have a (ref_count > 0) after
     this set of decreasing references...
  */

  gpy_context_t * popd = gpy_vec_pop( gpy_namespace_vec );
  gpy_vec_free( popd->symbols );
  gpy_free( popd );
}

void gpy_rr_finalize_block_decls( int n, ... )
{
  va_list vl; int idx;
  va_start( vl,n );

  /* gpy_object_t is a typedef of gpy_object_state_t *
     to keep stdarg.h happy
  */
  gpy_object_t * it = NULL;
  for( idx = 0; idx<n; ++idx )
    {
      it = va_arg( vl, gpy_object_t* );
      gpy_assert(it->T == TYPE_OBJECT_STATE);
      /* no assert this macro auto inserts an assert */
      Gpy_Decr_Ref( it->o.object_state );
    }
  va_end(vl);
}

gpy_object_t * gpy_rr_fold_call(struct gpy_callable_def_t * callables, 
				const char * ident, int n_args, ... )
{
  return NULL;
}

gpy_object_t *
gpy_rr_eval_dot_operator( gpy_object_t * x, gpy_object_t * y )
{
  return NULL;
}

gpy_object_t *
gpy_rr_eval_expression (gpy_object_t * x1, gpy_object_t * y1,
			gpy_opcode_t op)
{
  char * op_str = NULL;
  gpy_object_t * retval = NULL;

  gpy_assert(x1->T == TYPE_OBJECT_STATE);
  gpy_assert(y1->T == TYPE_OBJECT_STATE);
  gpy_object_state_t * x = x1->o.object_state;
  gpy_object_state_t * y = y1->o.object_state;

  struct gpy_type_obj_def_t * def = x->definition;
  struct gpy_number_prot_t * binops = (*def).binary_protocol;
  struct gpy_number_prot_t binops_l = (*binops);

  if( binops_l.init )
    {
      binary_op o = NULL;
      switch( op )
	{
	case OP_BIN_ADDITION:
	  o = binops_l.n_add;
	  op_str = "+ ";
	  break;

	default:
	  fatal("unhandled binary operation <%x>!\n", op );
	  break;
	}

#ifdef DEBUG
      x->definition->print_hook( x1, stdout, false );
      fprintf(stdout, "%s", op_str );
      y->definition->print_hook( y1, stdout, true );
#endif
      
      retval = o(x1,y1);
      
#ifdef DEBUG
      if( retval )
	{
	  fprintf(stdout, "evaluated to: ");
	  retval->o.object_state->definition->print_hook (retval, stdout, false);
	  fprintf(stdout, "!\n");
	}
#endif
    }
  else
    {
      fatal("object type <%s> has no binary protocol!\n",
	    x->obj_t_ident );
    }
 
  return retval;
}