summaryrefslogtreecommitdiff
path: root/cad/src/experimental/pyrex-atoms-bonds/iguana/ighelp.c
blob: 8c5dc1cd6a75d6a269c18281b98907b8480905e7 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
/*
 * C code for the multithreaded Iguana virtual machine
 */

#include "Python.h"
#include "ighelp.h"

#define DEBUG 0

#if DEBUG
#define MARK()  \
  fprintf(stderr, __FILE__ ":%d\n", __LINE__); fflush(stderr)
#define DBGPRINTF(fmt)  \
  fprintf(stderr, __FILE__ ":%d " fmt, __LINE__); fflush(stderr)
#define DBGPRINTF1(fmt,a)  \
  fprintf(stderr, __FILE__ ":%d " fmt, __LINE__, a); fflush(stderr)
#define DBGPRINTF2(fmt,a,b)  \
  fprintf(stderr, __FILE__ ":%d " fmt, __LINE__, a, b); fflush(stderr)
#define DBGPRINTF3(fmt,a,b,c)  \
  fprintf(stderr, __FILE__ ":%d " fmt, __LINE__, a, b, c); fflush(stderr)
#else
#define MARK()
#define DBGPRINTF(fmt)
#define DBGPRINTF1(fmt,a)
#define DBGPRINTF2(fmt,a,b)
#define DBGPRINTF3(fmt,a,b,c)
#endif

static unsigned int xrand, yrand, zrand;        /* steal from whrandom.py */
static iguana_thread_object root_thread;        /* never actually runs
                                                 * code, just the anchor
                                                 * for a doubly-linked
                                                 * list */
static int task_switch_enable = 1;
PyObject *IguanaError;
static void igthread_dealloc(iguana_thread_object * thr);
static PyObject *igthread_getattr(iguana_thread_object * a, char *name);
static int igthread_setattr(iguana_thread_object * self, char *name,
                            PyObject * v);
static int igverb_lit(iguana_thread_object * self, int pc);

#define EVILRETURN NULL

PyTypeObject iguana_thread_type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "iguana_thread",
    sizeof(iguana_thread_object),
    0,
    (destructor) igthread_dealloc,      /* tp_dealloc */
    0,                          /* tp_print */
    (getattrfunc) igthread_getattr,     /* tp_getattr */
    (setattrfunc) igthread_setattr,     /* tp_setattr */
    0,                          /* tp_compare */
    0,                          /* tp_repr */
    0,                          /* tp_as_number */
    0,                          /* tp_as_sequence */
    0,                          /* tp_as_mapping */
    0,                          /* tp_hash */
};

#define IguanaThread_Check(op) ((op)->ob_type == &iguana_thread_type)

static PyObject *
new_iguana_thread(PyObject *self, PyObject *args)
{
    int stack_size = 100, i, n;
    PyObject *prog, *mem = NULL, *z;
    iguana_thread_object *thr, *prev, *next;
    if (!PyArg_ParseTuple(args, "OO|i", &prog, &mem, &stack_size))
        return NULL;
    if (mem == Py_None) {
        mem = NULL;
    }
    if (mem != NULL && mem->ob_type->tp_as_sequence == NULL) {
        PyErr_SetString(IguanaError,
                        "memory must be a subscriptable object");
        return NULL;
    }
    if (!PyList_Check(prog)) {
        PyErr_SetString(IguanaError, "Iguana programs must be lists");
        return NULL;
    }
    iguana_thread_type.ob_type = &PyType_Type;
    thr = PyObject_NEW(iguana_thread_object, &iguana_thread_type);
    thr->data_stack = malloc(stack_size * sizeof(double));
    if (thr->data_stack == NULL)
        return PyErr_NoMemory();
    thr->program_size = n = PyList_Size(prog);
    thr->program = (program_entry *) malloc(n * sizeof(program_entry));
    if (thr->program == NULL) {
        PyErr_SetString(PyExc_MemoryError, "out of memory");
        return NULL;
    }
    for (i = 0; i < n; i++) {
	int x;
	z = PyList_GetItem(prog, i);
	if (!PyInt_Check(z)) {
	    PyErr_SetString(PyExc_TypeError, "program entries should be ints");
	    return NULL;
	}
	thr->program[i].ival = x = PyInt_AsLong(z);
	if (x == (int) igverb_lit) {
	    i++;
	    z = PyList_GetItem(prog, i);
	    if (!PyFloat_Check(z)) {
		PyErr_SetString(PyExc_TypeError, "argument to LIT should be a float");
		return NULL;
	    }
	    thr->program[i].dval = PyFloat_AsDouble(z);
	}
    }

    Py_XINCREF(mem);
    thr->memory = mem;
    thr->program_counter = thr->finished = 0;
    thr->stacksize = stack_size;
    thr->dspointer = thr->rspointer = 0;
    /*
     * When we include a thread in the doubly-linked list of active
     * threads, we INCREF it. When the thread finishes, we DECREF it. 
     */
    prev = root_thread.prev;
    next = &root_thread;
    thr->next = &root_thread;
    thr->prev = prev;
    prev->next = thr;
    next->prev = thr;
    Py_INCREF(thr);
    return (PyObject *) thr;
}

static void
igthread_dealloc(iguana_thread_object * thr)
{
    Py_XDECREF(thr->memory);
    PyMem_DEL(thr->program);
    PyMem_DEL(thr->data_stack);
    PyMem_DEL(thr);
}

static char push_doc[] = "push (x)\n\
\n\
Push a float onto the data stack.";

static PyObject *
igthread_push(iguana_thread_object * self, PyObject * args)
{
    double x;
    if (!PyArg_ParseTuple(args, "d", &x))
        return NULL;
    CHECK_OVERFLOW(1);
    self->data_stack[self->dspointer] = x;
    self->dspointer++;
    Py_INCREF(Py_None);
    return Py_None;
}

static char pop_doc[] = "pop ()\n\
\n\
Pop a float from the data stack.";

static PyObject *
igthread_pop(iguana_thread_object * self, PyObject * args)
{
    if (!PyArg_ParseTuple(args, ""))
        return NULL;
    CHECK_UNDERFLOW(1);
    self->dspointer--;
    return PyFloat_FromDouble(self->data_stack[self->dspointer]);
}

static double
my_rand(void)
{
    double d;
    xrand = (171 * xrand) % 30269;
    yrand = (172 * yrand) % 30307;
    zrand = (170 * zrand) % 30323;
    d = xrand / 30269.0 + yrand / 30307.0 + zrand / 30323.0;
    while (d < 0.0)
        d += 1.0;
    while (d >= 1.0)
        d -= 1.0;
    return d;
}

/****************************************************/

PyObject *verb_dict;

#undef EVILRETURN
#define EVILRETURN -1

static int
igverb_rand(iguana_thread_object * self, int pc)
{
    CHECK_OVERFLOW(1);
    self->data_stack[self->dspointer] = my_rand();
    self->dspointer++;
    return pc;
}

static int
igverb_lit(iguana_thread_object * self, int pc)
{
    CHECK_OVERFLOW(1);
    if (pc > self->program_size - 2) {
        PyErr_SetString(IguanaError, "badly compiled program");
        return -1;
    }
    self->data_stack[self->dspointer] = self->program[pc].dval;
    self->dspointer++;
    return pc + 1;
}

static int
igverb_call(iguana_thread_object * self, int pc)
{
    if (self->rspointer > RETURN_STACK_SIZE - 1) {
        PyErr_SetString(IguanaError, "return stack overflow");
        return -1;
    }
    self->return_stack[self->rspointer] = pc + 1;
    self->rspointer++;
    return self->program[pc].ival;
}

static int
igverb_exit(iguana_thread_object * self, int pc)
{
    if (self->rspointer == 0) {
        PyErr_SetString(IguanaError, "thread finished");
        return -1;
    }
    self->rspointer--;
    return self->return_stack[self->rspointer];
}

static int
igverb_zjump(iguana_thread_object * self, int pc)
{
    CHECK_UNDERFLOW(1);
    self->dspointer--;
    if (self->data_stack[self->dspointer] == 0.0)
        return self->program[pc].ival;
    return pc + 1;
}

static int
igverb_jump(iguana_thread_object * self, int pc)
{
    return self->program[pc].ival;
}

static int
igverb_fetch(iguana_thread_object * self, int pc)
{
    int n;
    PyObject *q;
    if (self->memory == NULL) {
        PyErr_SetString(IguanaError, "fetching from non-existent memory");
        return -1;
    }
    CHECK_UNDERFLOW(1);
    n = (int) self->data_stack[self->dspointer - 1];
    q = PyObject_GetItem(self->memory, PyInt_FromLong(n));
    if (q == NULL)
        return -1;
    if (PyFloat_Check(q))
        self->data_stack[self->dspointer - 1] = PyFloat_AsDouble(q);
    else if (PyInt_Check(q))
        self->data_stack[self->dspointer - 1] = (double) PyInt_AsLong(q);
    else {
        PyErr_SetString(IguanaError,
                        "things in memory must be ints or floats");
        return -1;
    }
    return pc;
}

static int
igverb_store(iguana_thread_object * self, int pc)
{
    int n;
    double d;
    if (self->memory == NULL) {
        PyErr_SetString(IguanaError, "storing to non-existent memory");
        return -1;
    }
    CHECK_UNDERFLOW(2);
    self->dspointer -= 2;
    n = (int) self->data_stack[self->dspointer + 1];
    d = self->data_stack[self->dspointer];
    if (PyObject_SetItem(self->memory, PyInt_FromLong(n),
                         PyFloat_FromDouble(d)) == -1)
        return -1;
    return pc;
}

static int
igverb_do(iguana_thread_object * self, int pc)
{
    int index, limit;
    CHECK_UNDERFLOW(2);
    R_CHECK_OVERFLOW(2);
    self->dspointer -= 2;
    index = (int) self->data_stack[self->dspointer + 1];
    limit = (int) self->data_stack[self->dspointer];
    self->return_stack[self->rspointer] = limit;
    self->return_stack[self->rspointer + 1] = index;
    self->rspointer += 2;
    return pc;
}

static int
igverb_loop(iguana_thread_object * self, int pc)
{
    unsigned int index, limit;
    R_CHECK_UNDERFLOW(2);
    index = self->return_stack[self->rspointer - 1] + 1;
    limit = self->return_stack[self->rspointer - 2];
    if (index >= limit) {
        self->rspointer -= 2;
        return pc + 1;
    }
    self->return_stack[self->rspointer - 1] = index;
    return self->program[pc].ival;
}

static int
igverb_i(iguana_thread_object * self, int pc)
{
    unsigned int index;
    R_CHECK_UNDERFLOW(2);
    CHECK_OVERFLOW(1);
    index = self->return_stack[self->rspointer - 1];
    self->data_stack[self->dspointer] = (double) index;
    self->dspointer++;
    return pc;
}

static int
igverb_j(iguana_thread_object * self, int pc)
{
    unsigned int index;
    R_CHECK_UNDERFLOW(4);
    CHECK_OVERFLOW(1);
    index = self->return_stack[self->rspointer - 3];
    self->data_stack[self->dspointer] = (double) index;
    self->dspointer++;
    return pc;
}

static int
igverb_k(iguana_thread_object * self, int pc)
{
    unsigned int index;
    R_CHECK_UNDERFLOW(6);
    CHECK_OVERFLOW(1);
    index = self->return_stack[self->rspointer - 5];
    self->data_stack[self->dspointer] = (double) index;
    self->dspointer++;
    return pc;
}

static int
igverb_spawn(iguana_thread_object * self, int pc)
{
    iguana_thread_object *newguy;
    PyObject *args;
    int n;
    args = Py_BuildValue("(OO)", self->program, self->memory);
    newguy = (iguana_thread_object *) new_iguana_thread(NULL, args);
    if (newguy == NULL)
        return -1;
    CHECK_UNDERFLOW(1);
    self->dspointer--;
    n = (int) self->data_stack[self->dspointer];
    CHECK_UNDERFLOW(n);
    self->dspointer -= n;
    memcpy(newguy->data_stack,
           &(self->data_stack[self->dspointer]), n * sizeof(double));
    newguy->dspointer = n;
    newguy->program_counter = self->program[pc].ival;
    return pc + 1;
}

static int
igverb_atomic_on(iguana_thread_object * self, int pc)
{
    task_switch_enable = 0;
    return pc;
}

static int
igverb_atomic_off(iguana_thread_object * self, int pc)
{
    task_switch_enable = 1;
    return pc;
}

static struct predef {
    char *forth_name;
    int (*function) (iguana_thread_object * self, int pc);
} predefined[] = {
    { "rand", igverb_rand },
    { "lit", igverb_lit },
    { "call", igverb_call },
    { "exit", igverb_exit },
    { "jump", igverb_jump },
    { "zjump", igverb_zjump },
    { "store", igverb_store },
    { "fetch", igverb_fetch },
    { "do", igverb_do },
    { "loop", igverb_loop },
    { "i", igverb_i },
    { "j", igverb_j },
    { "k", igverb_k },
    { "spawn", igverb_spawn },
    { "<atomic", igverb_atomic_on },
    { "atomic>", igverb_atomic_off },
    { NULL, NULL }
};

static void
add_verbs(void)
{
    extern void add_more_verbs(PyObject *);
    struct predef *pr;
    for (pr = predefined; pr->forth_name; pr++)
        PyDict_SetItemString(verb_dict, pr->forth_name,
                             PyInt_FromLong((long) pr->function));
    add_more_verbs(verb_dict);
}

/****************************************************/

PyMethodDef iguana_thread_methods[] = {
    {"push", (PyCFunction) igthread_push, 1, push_doc},
    {"pop", (PyCFunction) igthread_pop, 1, pop_doc},
    {NULL, NULL}                /* sentinel */
};

static PyObject *
igthread_getattr(iguana_thread_object *self, char *name)
{
    if (strcmp(name, "pc") == 0)
        return PyInt_FromLong(self->program_counter);
    return Py_FindMethod(iguana_thread_methods, (PyObject *) self, name);
}

static int
igthread_setattr(iguana_thread_object *self, char *name, PyObject * v)
{
    if (v == NULL) {
        PyErr_SetString(PyExc_AttributeError,
                        "can't delete attributes of an iguana thread");
        return -1;
    }
    if (strcmp(name, "pc") == 0) {
        if (!PyInt_Check(v)) {
            PyErr_SetString(PyExc_AttributeError,
                            "program counter must be an integer");
            return -1;
        }
        self->program_counter = PyInt_AsLong(v);
        return 0;
    }
    PyErr_SetString(PyExc_AttributeError, name);
    return -1;
}

/**************************************************************/

static PyObject *
ig_active_threads(PyObject * self, PyObject * args)
{
    long n;
    iguana_thread_object *P;
    if (!PyArg_ParseTuple(args, ""))
        return NULL;
    for (n = 0, P = root_thread.next; P != &root_thread; P = P->next, n++);
    return PyInt_FromLong(n);
}

static int igverb_exit(iguana_thread_object * self, int pc);

static PyObject *
ig_threads_step(PyObject * self, PyObject * args)
{
    int num_steps = 1;
    iguana_thread_object *P, *Q, *R;
    if (!PyArg_ParseTuple(args, "|i", &num_steps))
        return NULL;
    while (num_steps--) {
        if (root_thread.next == &root_thread)
            break;
        P = root_thread.next;
        while (P != &root_thread) {
            int pc;
            igverbfunc func;
            // printf("Now serving thread %p\n", P);
            pc = P->program_counter;
            if (pc >= P->program_size) {
                PyErr_SetString(IguanaError, "program ran off the end");
                return NULL;
            }
            func = (igverbfunc) P->program[pc].ival;
            if (func == igverb_exit && P->rspointer == 0) {
                /*
                 * this thread has finished its job 
                 */
                Q = P->next;
                R = P->prev;
                R->next = Q;
                Q->prev = R;
                P->next = P->prev = NULL;
                Py_DECREF(P);
                P = Q;
                task_switch_enable = 1;
            } else {
                pc = (*func) (P, pc + 1);
                if (pc < 0) {
                    /*
                     * the function will set up its own error string 
                     */
                    return NULL;
                }
                P->program_counter = pc;
            }
            if (task_switch_enable)
                P = P->next;
        }
    }
    Py_INCREF(Py_None);
    return Py_None;
}

/*
 * List of methods defined in the module 
 */

static struct PyMethodDef ighelp_methods[] = {
    {"thread", (PyCFunction) new_iguana_thread, 1},
    {"active_threads", (PyCFunction) ig_active_threads, 1},
    {"step", (PyCFunction) ig_threads_step, 1},
    {NULL, NULL}
};

static char ighelp_doc[] = "\
This module defines a Forth-esque virtual machine called an Iguana\n\
thread. Iguana threads are small and independent so they'll be good\n\
for massive-multithreading hacks.\n\
\n\
thread(p)  ==> p is a list representing an Iguana program\n\
               the return value is a thread\n\
thread(p,n)  ==> p is a list representing an Iguana program\n\
                 n is an integer representing a stack depth\n\
                 the return value is a thread\n\
Default stack depth is 100.";

void
initighelp()
{
    PyObject *m, *d;
    time_t T;
    int i;

    /*
     * initialize doubly-linked list of active threads 
     */
    root_thread.next = root_thread.prev = &root_thread;
    /*
     * set up a random number generator 
     */
    time(&T);
    xrand = T & 255;
    yrand = (T >> 8) & 255;
    zrand = (T >> 16) & 255;
    for (i = 0; i < 5; i++) my_rand();
    /*
     * Create the module and add the functions 
     */
    m = Py_InitModule3("ighelp", ighelp_methods, ighelp_doc);
    /*
     * Add some symbolic constants to the module 
     */
    d = PyModule_GetDict(m);
    IguanaError = PyString_FromString("IguanaError");
    PyDict_SetItemString(d, "IguanaError", IguanaError);
    verb_dict = PyDict_New();
    add_verbs();
    PyDict_SetItemString(d, "verbs", verb_dict);
}

/*
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * End:
 */