aboutsummaryrefslogtreecommitdiff
path: root/libgpython/runtime/py-runtime.c
blob: 6dfcd55f001d041323ebc62727b8233b07f2b811 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* 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/vectors.h>
#include <gpython/objects.h>
#include <gpython/runtime.h>

gpy_object_t ** __GPY_GLOBL_RR_STACK;
gpy_vector_t * __GPY_GLOBL_CALL_STACK;
gpy_vector_t * __GPY_GLOBL_PRIMITIVES;
gpy_object_t ** __GPY_GLOBL_RETURN_ADDR;
gpy_object_t ** __GPY_GLOBL_RR_STACK_POINTER;

/*
  size
  current_length
  return addr
  .... symbols
*/

static
void gpy_rr_init_primitives (void)
{
  gpy_obj_integer_mod_init (__GPY_GLOBL_PRIMITIVES);
  gpy_obj_staticmethod_mod_init (__GPY_GLOBL_PRIMITIVES);
  gpy_obj_class_mod_init (__GPY_GLOBL_PRIMITIVES);
  gpy_obj_classmethod_mod_init (__GPY_GLOBL_PRIMITIVES);
}

static
void gpy_rr_init_runtime_stack (void)
{
  __GPY_GLOBL_PRIMITIVES = gpy_malloc (sizeof (gpy_vector_t));
  gpy_vec_init (__GPY_GLOBL_PRIMITIVES);
  gpy_rr_init_primitives ();

  __GPY_GLOBL_CALL_STACK = gpy_malloc (sizeof (gpy_vector_t));
  gpy_vec_init (__GPY_GLOBL_CALL_STACK);

  __GPY_GLOBL_RR_STACK = gpy_calloc (3, sizeof (gpy_object_t *));
  *__GPY_GLOBL_RR_STACK = gpy_rr_fold_integer (2);

  __GPY_GLOBL_RR_STACK_POINTER = __GPY_GLOBL_RR_STACK;
  __GPY_GLOBL_RR_STACK_POINTER++;

  *__GPY_GLOBL_RR_STACK_POINTER = gpy_rr_fold_integer (0);

  __GPY_GLOBL_RR_STACK_POINTER++;
  *__GPY_GLOBL_RR_STACK_POINTER = NULL;
  __GPY_GLOBL_RETURN_ADDR = __GPY_GLOBL_RR_STACK_POINTER;
}

/* remember to update the stack pointer's and the stack size */
void gpy_rr_extend_runtime_stack (int nslots)
{
  size_t size = sizeof (gpy_object_t *) * (3+nslots);
  __GPY_GLOBL_RR_STACK = gpy_realloc (__GPY_GLOBL_RR_STACK, size);
  
  __GPY_GLOBL_RR_STACK_POINTER = __GPY_GLOBL_RR_STACK;
  __GPY_GLOBL_RR_STACK_POINTER += 3 + nslots;
}

void gpy_rr_init_runtime (void)
{
  gpy_rr_init_runtime_stack ();
}

void gpy_rr_cleanup_final (void)
{
  /*
    Cleanup the runtime stack and all other object data
   */
  mpfr_free_cache ();
}

gpy_object_attrib_t * gpy_rr_fold_attribute (const unsigned char * identifier,
					     unsigned char * code_addr,
					     unsigned int offset)
{
  gpy_object_attrib_t * attrib = gpy_malloc (sizeof (gpy_object_attrib_t));
  attrib->identifier = identifier;
  if (code_addr)
    {
      gpy_object_t * f = gpy_rr_fold_classmethod_decl (identifier, code_addr);
      attrib->addr = f;
    }
  else
    attrib->addr = NULL;

  attrib->offset = offset;
  return attrib;
}

gpy_object_attrib_t ** gpy_rr_fold_attrib_list (int n, ...)
{
  gpy_object_attrib_t ** retval = NULL;
  if (n > 0)
    {
      /* +1 for the sentinal */
      retval = (gpy_object_attrib_t **)
	gpy_calloc (n+1, sizeof (gpy_object_attrib_t *));

      va_list ap;
      int idx;
      va_start (ap, n);
      for (idx = 0; idx < n; ++idx)
	{
	  gpy_object_attrib_t * i = va_arg (ap, gpy_object_attrib_t *);
	  retval[idx] = i;
	}
      /* sentinal */
      retval[idx] = NULL;
    }
  return retval;
}

gpy_object_t * gpy_rr_fold_class_decl (gpy_object_attrib_t ** attribs,
				       int size, const char * identifier)
{
  gpy_object_t * retval = NULL_OBJECT;

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

  gpy_literal_t A;
  A.type = TYPE_ATTRIB_L;
  A.literal.attribs = attribs;

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

  gpy_literal_t s;
  s.type = TYPE_STRING;
  s.literal.string = (char *) identifier;

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

  args[0] = &a1;
  args[1] = &a2;
  args[2] = &a3;
  args[3] = &a4;

  gpy_typedef_t * def = __gpy_class_type_node;
  retval = def->tp_new (def, args);
  gpy_free (args);

  gpy_assert (retval->T == TYPE_OBJECT_DECL);

  return retval;
}

gpy_object_t * gpy_rr_fold_staticmethod_decl (const char * identifier,
					      unsigned char * code_addr)
{
  gpy_object_t * retval = NULL_OBJECT;

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

  gpy_literal_t i;
  i.type = TYPE_STRING;
  i.literal.string = (char *)identifier;

  gpy_literal_t p;
  p.type = TYPE_ADDR;
  p.literal.addr = code_addr;

  gpy_literal_t n;
  n.type = TYPE_INTEGER;
  n.literal.integer = 0;

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

  args[0] = &a1;
  args[1] = &a2;
  args[2] = &a3;
  args[3] = &a4;

  gpy_typedef_t * def = __gpy_staticmethod_type_node;
  retval = def->tp_new (def, args);
  gpy_free (args);

  gpy_assert (retval->T == TYPE_OBJECT_DECL);

  return retval;
}

gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier,
					     unsigned char * code_addr)
{
  gpy_object_t * retval = NULL_OBJECT;

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

  gpy_literal_t s;
  s.type = TYPE_STRING;
  s.literal.string = (char *)identifier;

  gpy_literal_t p;
  p.type = TYPE_ADDR;
  p.literal.addr = code_addr;

  gpy_literal_t n;
  n.type = TYPE_INTEGER;
  n.literal.integer = 0;

  gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &s };
  gpy_object_t a2 = { .T = TYPE_OBJECT_LIT, .o.literal = &p };
  gpy_object_t a3 = { .T = TYPE_OBJECT_LIT, .o.literal = &n };
  gpy_object_t a4 = { .T = TYPE_NULL, .o.literal = NULL };

  args[0] = &a1;
  args[1] = &a2;
  args[2] = &a3;
  args[3] = &a4;

  gpy_typedef_t * def = __gpy_classmethod_type_node;
  retval = def->tp_new (def, args);
  gpy_free (args);

  gpy_assert (retval->T == TYPE_OBJECT_DECL);

  return retval;
}


gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...)
{
  gpy_object_t * retval = NULL_OBJECT;

  gpy_assert (decl->T == TYPE_OBJECT_DECL);
  gpy_typedef_t * type = decl->o.object_state->definition;
  
  /* + 1 for sentinal */
  gpy_object_t ** args = calloc (nargs + 1, sizeof (gpy_object_t *));
  va_list ap;
  int idx;
  va_start (ap, nargs);
  for (idx = 0; idx < nargs; ++idx)
    {
      args[idx] = va_arg (ap, gpy_object_t *);
    }
  args[idx] = NULL;

  if (type->tp_call)
    {
      retval = type->tp_call (decl, args);
    }
  else
    fatal ("name is not callable!\n");
    
  return retval;
}

unsigned char * gpy_rr_eval_attrib_reference (gpy_object_t * base,
					      const char * attrib)
{
  unsigned char * retval = NULL;
  gpy_typedef_t * type = base->o.object_state->definition;
  
  struct gpy_object_attrib_t ** members = type->members_defintion;
  gpy_object_state_t * objs = base->o.object_state;

  int idx, offset = -1;
  for (idx = 0; members[idx] != NULL; ++idx)
    {
      struct gpy_object_attrib_t * it = members[idx];
      if (!strcmp (attrib, it->identifier))
	{
	  offset = it->offset;
	  unsigned char * state = (unsigned char *)objs->state;
	  retval = state + offset;
	  break;
	}
    }
  gpy_assert (retval);
  return retval;
}

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

  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_typedef_t * Int_def = __gpy_integer_type_node;
  retval = Int_def->tp_new (Int_def, args);
  gpy_free(args);
  gpy_assert (retval->T == TYPE_OBJECT_STATE);

  return retval;
}

inline
void * gpy_rr_get_object_state (gpy_object_t * o)
{
  gpy_assert (o);
  return o->o.object_state->state;
}

/**
 * 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 * it = NULL;
  for (idx = 0; idx<count; ++idx)
    {
      it = va_arg (vl, gpy_object_t *);
      struct gpy_typedef_t * definition = it->o.object_state->definition;

      switch (fd)
	{
	case 1:
	  definition->tp_print (it, stdout, false);
	  break;

	case 2:
	  definition->tp_print (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_t * x1)
{
  gpy_assert (x1->T == TYPE_OBJECT_STATE);
  gpy_object_state_t * x = x1->o.object_state;

  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_t * x1)
{
  gpy_assert (x1->T == TYPE_OBJECT_STATE);
  gpy_object_state_t * x = x1->o.object_state;

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

gpy_object_t * gpy_rr_eval_expression (gpy_object_t * x1,
				       gpy_object_t * y1,
				       unsigned int 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_typedef_t * def = x1->o.object_state->definition;
  struct gpy_number_prot_t * binops = (*def).binary_protocol;
  struct gpy_number_prot_t binops_l = (*binops);

  debug ("Eval expression!\n");

  binary_op o = NULL;
  switch( op )
    {
      /* addition */
    case 1:
      o = binops_l.n_add;
      op_str = "+ ";
      break;

      /* FINISH .... */

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

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