runtime.c 37.2 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
5
6
7
8
9
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "misc.h"
#include "machine.h"
#include "runtime.h"
10
#include "bc.h"
Damien's avatar
Damien committed
11

12
13
14
15
#if 0 // print debugging info
#define DEBUG_printf(args...) printf(args)
#define DEBUG_OP_printf(args...) printf(args)
#else // don't print debugging info
16
#define DEBUG_printf(args...) (void)0
Damien's avatar
Damien committed
17
#define DEBUG_OP_printf(args...) (void)0
18
#endif
Damien's avatar
Damien committed
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

// enable/disable float support with this definition
#define PY_FLOAT (1)

typedef machine_int_t py_small_int_t;

#define IS_O(o, k) (((((py_small_int_t)(o)) & 1) == 0) && (((py_obj_base_t*)(o))->kind == (k)))
#define IS_SMALL_INT(o) (((py_small_int_t)(o)) & 1)
#define FROM_SMALL_INT(o) (((py_small_int_t)(o)) >> 1)
#define TO_SMALL_INT(o) ((py_obj_t)(((o) << 1) | 1))

#ifdef PY_FLOAT
typedef machine_float_t float_t;
#endif

typedef enum {
    O_CONST,
    O_STR,
#ifdef PY_FLOAT
    O_FLOAT,
#endif
    O_FUN_0,
    O_FUN_1,
    O_FUN_2,
    O_FUN_N,
    O_FUN_BC,
45
    O_FUN_ASM,
Damien's avatar
Damien committed
46
47
48
49
50
    O_BOUND_METH,
    O_LIST,
    O_SET,
    O_MAP,
    O_CLASS,
51
    O_OBJ,
Damien's avatar
Damien committed
52
53
54
55
56
57
58
59
60
61
62
63
64
} py_obj_kind_t;

typedef enum {
    MAP_QSTR,
    MAP_PY_OBJ,
} py_map_kind_t;

typedef struct _py_map_elem_t {
    py_obj_t key;
    py_obj_t value;
} py_map_elem_t;

typedef struct _py_map_t {
65
    py_map_kind_t kind; // TODO merge this 1-bit field into alloc or used
Damien's avatar
Damien committed
66
67
68
69
70
    machine_uint_t alloc;
    machine_uint_t used;
    py_map_elem_t *table;
} py_map_t;

71
72
73
typedef struct _py_obj_base_t py_obj_base_t;

struct _py_obj_base_t {
Damien's avatar
Damien committed
74
75
76
77
78
    py_obj_kind_t kind;
    union {
        const char *id;
        qstr u_str;
#ifdef PY_FLOAT
79
        float_t u_flt;
Damien's avatar
Damien committed
80
81
82
#endif
        struct { // for O_FUN_[012N]
            int n_args;
83
            void *fun;
Damien's avatar
Damien committed
84
85
        } u_fun;
        struct { // for O_FUN_BC
86
            int n_args;
Damien's avatar
Damien committed
87
88
89
            byte *code;
            uint len;
        } u_fun_bc;
90
91
92
93
        struct { // for O_FUN_ASM
            int n_args;
            void *fun;
        } u_fun_asm;
Damien's avatar
Damien committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        struct { // for O_BOUND_METH
            py_obj_t meth;
            py_obj_t self;
        } u_bound_meth;
        struct { // for O_LIST
            int alloc;
            int len;
            py_obj_t *items;
        } u_list;
        struct { // for O_SET
            int alloc;
            int used;
            py_obj_t *table;
        } u_set;
        py_map_t u_map; // for O_MAP
        struct { // for O_CLASS
110
            py_map_t *locals;
Damien's avatar
Damien committed
111
        } u_class;
112
113
114
115
        struct { // for O_OBJ
            py_obj_base_t *class; // points to a O_CLASS object
            py_map_t *members;
        } u_obj;
Damien's avatar
Damien committed
116
    };
117
};
Damien's avatar
Damien committed
118
119
120
121
122

py_obj_t py_const_none;
py_obj_t py_const_false;
py_obj_t py_const_true;

123
124
125
// locals and globals need to be pointers because they can be the same in outer module scope
py_map_t *map_locals;
py_map_t *map_globals;
Damien's avatar
Damien committed
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
py_map_t map_builtins;

// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
static int doubling_primes[] = {7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607};

int get_doubling_prime_greater_or_equal_to(int x) {
    for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
        if (doubling_primes[i] >= x) {
            return doubling_primes[i];
        }
    }
    // ran out of primes in the table!
    // return something sensible, at least make it odd
    return x | 1;
}

void py_map_init(py_map_t *map, py_map_kind_t kind, int n) {
    map->kind = kind;
    map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
    map->used = 0;
    map->table = m_new(py_map_elem_t, map->alloc);
    for (int i = 0; i < map->alloc; i++) {
        map->table[i].key = NULL;
        map->table[i].value = NULL;
    }
}

py_map_t *py_map_new(py_map_kind_t kind, int n) {
    py_map_t *map = m_new(py_map_t, 1);
    py_map_init(map, kind, n);
    return map;
}

int py_obj_hash(py_obj_t o_in) {
    if (IS_SMALL_INT(o_in)) {
        return FROM_SMALL_INT(o_in);
    } else if (IS_O(o_in, O_STR)) {
        return ((py_obj_base_t*)o_in)->u_str;
    } else {
        assert(0);
        return 0;
    }
}

bool py_obj_equal(py_obj_t o1, py_obj_t o2) {
    if (o1 == o2) {
        return true;
    } else if (IS_SMALL_INT(o1) && IS_SMALL_INT(o2)) {
        return false;
    } else if (IS_O(o1, O_STR) && IS_O(o2, O_STR)) {
        return ((py_obj_base_t*)o1)->u_str == ((py_obj_base_t*)o2)->u_str;
    } else {
        assert(0);
        return false;
    }
}

py_map_elem_t* py_map_lookup_helper(py_map_t *map, py_obj_t index, bool add_if_not_found) {
    bool is_map_py_obj = (map->kind == MAP_PY_OBJ);
    machine_uint_t hash;
    if (is_map_py_obj) {
        hash = py_obj_hash(index);
    } else {
        hash = (machine_uint_t)index;
    }
    uint pos = hash % map->alloc;
    for (;;) {
        py_map_elem_t *elem = &map->table[pos];
        if (elem->key == NULL) {
            // not in table
            if (add_if_not_found) {
                if (map->used + 1 >= map->alloc) {
                    // not enough room in table, rehash it
                    int old_alloc = map->alloc;
                    py_map_elem_t *old_table = map->table;
                    map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
                    map->used = 0;
                    map->table = m_new(py_map_elem_t, map->alloc);
                    for (int i = 0; i < old_alloc; i++) {
                        if (old_table[i].key != NULL) {
                            py_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value;
                        }
                    }
                    m_free(old_table);
                    // restart the search for the new element
                    pos = hash % map->alloc;
                } else {
                    map->used += 1;
                    elem->key = index;
                    return elem;
                }
            } else {
                return NULL;
            }
        } else if (elem->key == index || (is_map_py_obj && py_obj_equal(elem->key, index))) {
            // found it
            if (add_if_not_found) {
                elem->key = index;
            }
            return elem;
        } else {
            // not yet found, keep searching in this table
            pos = (pos + 1) % map->alloc;
        }
    }
}

py_map_elem_t* py_qstr_map_lookup(py_map_t *map, qstr index, bool add_if_not_found) {
    py_obj_t o = (py_obj_t)(machine_uint_t)index;
    return py_map_lookup_helper(map, o, add_if_not_found);
}

py_map_elem_t* py_map_lookup(py_obj_t o, py_obj_t index, bool add_if_not_found) {
    assert(IS_O(o, O_MAP));
    return py_map_lookup_helper(&((py_obj_base_t *)o)->u_map, index, add_if_not_found);
}

static bool fit_small_int(py_small_int_t o) {
    return true;
}

py_obj_t py_obj_new_const(const char *id) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_CONST;
    o->id = id;
    return (py_obj_t)o;
}

py_obj_t py_obj_new_str(qstr qstr) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_STR;
    o->u_str = qstr;
    return (py_obj_t)o;
}

#ifdef PY_FLOAT
py_obj_t py_obj_new_float(float_t val) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_FLOAT;
265
    o->u_flt = val;
Damien's avatar
Damien committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    return (py_obj_t)o;
}
#endif

py_obj_t list_append(py_obj_t self_in, py_obj_t arg) {
    assert(IS_O(self_in, O_LIST));
    py_obj_base_t *self = self_in;
    if (self->u_list.len >= self->u_list.alloc) {
        self->u_list.alloc *= 2;
        self->u_list.items = m_renew(py_obj_t, self->u_list.items, self->u_list.alloc);
    }
    self->u_list.items[self->u_list.len++] = arg;
    return arg;
}

static qstr q_append;
static qstr q_print;
static qstr q_len;
static qstr q___build_class__;

typedef enum {
287
    PY_CODE_NONE,
Damien's avatar
Damien committed
288
    PY_CODE_BYTE,
289
290
    PY_CODE_NATIVE,
    PY_CODE_INLINE_ASM,
Damien's avatar
Damien committed
291
292
293
294
295
296
297
298
299
300
} py_code_kind_t;

typedef struct _py_code_t {
    py_code_kind_t kind;
    int n_args;
    union {
        struct {
            byte *code;
            uint len;
        } u_byte;
301
302
303
304
        struct {
            py_fun_t fun;
        } u_native;
        struct {
305
            void *fun;
306
        } u_inline_asm;
Damien's avatar
Damien committed
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
    };
} py_code_t;

static int next_unique_code_id;
static py_code_t *unique_codes;

py_obj_t fun_list_append;

py_obj_t py_builtin_print(py_obj_t o) {
    if (IS_O(o, O_STR)) {
        // special case, print string raw
        printf("%s\n", qstr_str(((py_obj_base_t*)o)->u_str));
    } else {
        // print the object Python style
        py_obj_print(o);
        printf("\n");
    }
    return py_const_none;
}

py_obj_t py_builtin_len(py_obj_t o_in) {
    py_small_int_t len = 0;
    if (IS_O(o_in, O_LIST)) {
        py_obj_base_t *o = o_in;
        len = o->u_list.len;
    } else if (IS_O(o_in, O_MAP)) {
        py_obj_base_t *o = o_in;
        len = o->u_map.used;
    } else {
        assert(0);
    }
    return TO_SMALL_INT(len);
}

341
342
py_obj_t py_builtin___build_class__(py_obj_t o_class_fun, py_obj_t o_class_name) {
    // we differ from CPython: we set the new __locals__ object here
343
    py_map_t *old_locals = map_locals;
344
    py_map_t *class_locals = py_map_new(MAP_QSTR, 0);
345
    map_locals = class_locals;
346
347
348
349
350
351
352
353

    // call the class code
    rt_call_function_1(o_class_fun, (py_obj_t)0xdeadbeef);

    // restore old __locals__ object
    map_locals = old_locals;

    // create and return the new class
Damien's avatar
Damien committed
354
355
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_CLASS;
356
    o->u_class.locals = class_locals;
Damien's avatar
Damien committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
    return o;
}

FILE *fp_native = NULL;

void rt_init() {
    q_append = qstr_from_str_static("append");
    q_print = qstr_from_str_static("print");
    q_len = qstr_from_str_static("len");
    q___build_class__ = qstr_from_str_static("__build_class__");

    py_const_none = py_obj_new_const("None");
    py_const_false = py_obj_new_const("False");
    py_const_true = py_obj_new_const("True");

372
373
374
    // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
    map_locals = map_globals = py_map_new(MAP_QSTR, 1);
    py_qstr_map_lookup(map_globals, qstr_from_str_static("__name__"), true)->value = py_obj_new_str(qstr_from_str_static("__main__"));
Damien's avatar
Damien committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

    py_map_init(&map_builtins, MAP_QSTR, 3);
    py_qstr_map_lookup(&map_builtins, q_print, true)->value = rt_make_function_1(py_builtin_print);
    py_qstr_map_lookup(&map_builtins, q_len, true)->value = rt_make_function_1(py_builtin_len);
    py_qstr_map_lookup(&map_builtins, q___build_class__, true)->value = rt_make_function_2(py_builtin___build_class__);

    next_unique_code_id = 1;
    unique_codes = NULL;

    fun_list_append = rt_make_function_2(list_append);

    fp_native = fopen("out-native", "wb");
}

void rt_deinit() {
    if (fp_native != NULL) {
        fclose(fp_native);
    }
}

int rt_get_new_unique_code_id() {
    return next_unique_code_id++;
}

399
static void alloc_unique_codes() {
Damien's avatar
Damien committed
400
401
    if (unique_codes == NULL) {
        unique_codes = m_new(py_code_t, next_unique_code_id);
402
403
404
        for (int i = 0; i < next_unique_code_id; i++) {
            unique_codes[i].kind = PY_CODE_NONE;
        }
Damien's avatar
Damien committed
405
    }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
}

void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args) {
    alloc_unique_codes();

    assert(unique_code_id < next_unique_code_id);
    unique_codes[unique_code_id].kind = PY_CODE_BYTE;
    unique_codes[unique_code_id].n_args = n_args;
    unique_codes[unique_code_id].u_byte.code = code;
    unique_codes[unique_code_id].u_byte.len = len;

    DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d\n", unique_code_id, code, len, n_args);
}

void rt_assign_native_code(int unique_code_id, py_fun_t fun, uint len, int n_args) {
    alloc_unique_codes();

423
    assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damien's avatar
Damien committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    unique_codes[unique_code_id].kind = PY_CODE_NATIVE;
    unique_codes[unique_code_id].n_args = n_args;
    unique_codes[unique_code_id].u_native.fun = fun;

    DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
    byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
    for (int i = 0; i < 128 && i < len; i++) {
        if (i > 0 && i % 16 == 0) {
            DEBUG_printf("\n");
        }
        DEBUG_printf(" %02x", fun_data[i]);
    }
    DEBUG_printf("\n");

    if (fp_native != NULL) {
        fwrite(fun_data, len, 1, fp_native);
440
        fflush(fp_native);
Damien's avatar
Damien committed
441
442
443
    }
}

444
445
446
447
448
void rt_assign_inline_asm_code(int unique_code_id, py_fun_t fun, uint len, int n_args) {
    alloc_unique_codes();

    assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
    unique_codes[unique_code_id].kind = PY_CODE_INLINE_ASM;
Damien's avatar
Damien committed
449
    unique_codes[unique_code_id].n_args = n_args;
450
    unique_codes[unique_code_id].u_inline_asm.fun = fun;
Damien's avatar
Damien committed
451

452
453
454
455
456
457
458
459
460
461
462
463
464
    DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
    byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
    for (int i = 0; i < 128 && i < len; i++) {
        if (i > 0 && i % 16 == 0) {
            DEBUG_printf("\n");
        }
        DEBUG_printf(" %02x", fun_data[i]);
    }
    DEBUG_printf("\n");

    if (fp_native != NULL) {
        fwrite(fun_data, len, 1, fp_native);
    }
Damien's avatar
Damien committed
465
466
}

467
468
469
470
471
472
473
474
475
476
477
478
bool py_obj_is_callable(py_obj_t o_in) {
    if (IS_SMALL_INT(o_in)) {
        return false;
    } else {
        py_obj_base_t *o = o_in;
        switch (o->kind) {
            case O_FUN_0:
            case O_FUN_1:
            case O_FUN_2:
            case O_FUN_N:
            case O_FUN_BC:
            case O_FUN_ASM:
479
            // what about O_CLASS, and an O_OBJ that has a __call__ method?
480
481
482
483
484
485
486
                return true;
            default:
                return false;
        }
    }
}

Damien's avatar
Damien committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
const char *py_obj_get_type_str(py_obj_t o_in) {
    if (IS_SMALL_INT(o_in)) {
        return "int";
    } else {
        py_obj_base_t *o = o_in;
        switch (o->kind) {
            case O_CONST:
                if (o == py_const_none) {
                    return "NoneType";
                } else {
                    return "bool";
                }
            case O_STR:
                return "str";
#ifdef PY_FLOAT
            case O_FLOAT:
                return "float";
#endif
505
506
507
508
509
510
            case O_FUN_0:
            case O_FUN_1:
            case O_FUN_2:
            case O_FUN_N:
            case O_FUN_BC:
                return "function";
Damien's avatar
Damien committed
511
512
513
514
515
516
            case O_LIST:
                return "list";
            case O_SET:
                return "set";
            case O_MAP:
                return "dict";
517
518
519
520
521
522
523
            case O_OBJ:
            {
                py_map_elem_t *qn = py_qstr_map_lookup(o->u_obj.class->u_class.locals, qstr_from_str_static("__qualname__"), false);
                assert(qn != NULL);
                assert(IS_O(qn->value, O_STR));
                return qstr_str(((py_obj_base_t*)qn->value)->u_str);
            }
Damien's avatar
Damien committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
            default:
                assert(0);
                return "UnknownType";
        }
    }
}

void py_obj_print(py_obj_t o_in) {
    if (IS_SMALL_INT(o_in)) {
        printf("%d", (int)FROM_SMALL_INT(o_in));
    } else {
        py_obj_base_t *o = o_in;
        switch (o->kind) {
            case O_CONST:
                printf("%s", o->id);
                break;
            case O_STR:
                // TODO need to escape chars etc
                printf("'%s'", qstr_str(o->u_str));
                break;
#ifdef PY_FLOAT
            case O_FLOAT:
546
                printf("%f", o->u_flt);
Damien's avatar
Damien committed
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
                break;
#endif
            case O_LIST:
                printf("[");
                for (int i = 0; i < o->u_list.len; i++) {
                    if (i > 0) {
                        printf(", ");
                    }
                    py_obj_print(o->u_list.items[i]);
                }
                printf("]");
                break;
            case O_SET:
            {
                bool first = true;
                printf("{");
                for (int i = 0; i < o->u_set.alloc; i++) {
                    if (o->u_set.table[i] != NULL) {
                        if (!first) {
                            printf(", ");
                        }
                        first = false;
                        py_obj_print(o->u_set.table[i]);
                    }
                }
                printf("}");
                break;
            }
            case O_MAP:
            {
                bool first = true;
                printf("{");
                for (int i = 0; i < o->u_map.alloc; i++) {
                    if (o->u_map.table[i].key != NULL) {
                        if (!first) {
                            printf(", ");
                        }
                        first = false;
                        py_obj_print(o->u_map.table[i].key);
                        printf(": ");
                        py_obj_print(o->u_map.table[i].value);
                    }
                }
                printf("}");
                break;
            }
            default:
594
                printf("<? %d>", o->kind);
Damien's avatar
Damien committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
                assert(0);
        }
    }
}

int rt_is_true(py_obj_t arg) {
    DEBUG_OP_printf("is true %p\n", arg);
    if (IS_SMALL_INT(arg)) {
        if (FROM_SMALL_INT(arg) == 0) {
            return 0;
        } else {
            return 1;
        }
    } else if (arg == py_const_none) {
        return 0;
    } else if (arg == py_const_false) {
        return 0;
    } else if (arg == py_const_true) {
        return 1;
    } else {
        assert(0);
        return 0;
    }
}

int rt_get_int(py_obj_t arg) {
    if (IS_SMALL_INT(arg)) {
        return FROM_SMALL_INT(arg);
    } else {
        assert(0);
        return 0;
    }
}

py_obj_t rt_load_const_str(qstr qstr) {
    DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
    return py_obj_new_str(qstr);
}

py_obj_t rt_load_name(qstr qstr) {
    // logic: search locals, globals, builtins
636
    DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
637
    py_map_elem_t *elem = py_qstr_map_lookup(map_locals, qstr, false);
Damien's avatar
Damien committed
638
    if (elem == NULL) {
639
        elem = py_qstr_map_lookup(map_globals, qstr, false);
Damien's avatar
Damien committed
640
        if (elem == NULL) {
641
642
643
644
645
            elem = py_qstr_map_lookup(&map_builtins, qstr, false);
            if (elem == NULL) {
                printf("name doesn't exist: %s\n", qstr_str(qstr));
                assert(0);
            }
Damien's avatar
Damien committed
646
647
648
649
650
651
        }
    }
    return elem->value;
}

py_obj_t rt_load_global(qstr qstr) {
652
653
    // logic: search globals, builtins
    DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
654
    py_map_elem_t *elem = py_qstr_map_lookup(map_globals, qstr, false);
655
656
657
658
659
660
661
662
    if (elem == NULL) {
        elem = py_qstr_map_lookup(&map_builtins, qstr, false);
        if (elem == NULL) {
            printf("name doesn't exist: %s\n", qstr_str(qstr));
            assert(0);
        }
    }
    return elem->value;
Damien's avatar
Damien committed
663
664
665
666
667
668
669
670
671
672
673
674
675
}

py_obj_t rt_load_build_class() {
    DEBUG_OP_printf("load_build_class\n");
    py_map_elem_t *elem = py_qstr_map_lookup(&map_builtins, q___build_class__, false);
    if (elem == NULL) {
        printf("name doesn't exist: __build_class__\n");
        assert(0);
    }
    return elem->value;
}

void rt_store_name(qstr qstr, py_obj_t obj) {
676
    DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
677
    py_qstr_map_lookup(map_locals, qstr, true)->value = obj;
678
679
680
681
}

void rt_store_global(qstr qstr, py_obj_t obj) {
    DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
682
    py_qstr_map_lookup(map_globals, qstr, true)->value = obj;
Damien's avatar
Damien committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}

py_obj_t rt_unary_op(int op, py_obj_t arg) {
    assert(0);
    return py_const_none;
}

py_obj_t rt_binary_op(int op, py_obj_t lhs, py_obj_t rhs) {
    DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
    if (op == RT_BINARY_OP_SUBSCR) {
        if (IS_O(lhs, O_LIST) && IS_SMALL_INT(rhs)) {
            return ((py_obj_base_t*)lhs)->u_list.items[FROM_SMALL_INT(rhs)];
        } else {
            assert(0);
        }
    } else if (IS_SMALL_INT(lhs) && IS_SMALL_INT(rhs)) {
        py_small_int_t val;
        switch (op) {
            case RT_BINARY_OP_ADD:
            case RT_BINARY_OP_INPLACE_ADD: val = FROM_SMALL_INT(lhs) + FROM_SMALL_INT(rhs); break;
            case RT_BINARY_OP_SUBTRACT: val = FROM_SMALL_INT(lhs) - FROM_SMALL_INT(rhs); break;
            case RT_BINARY_OP_MULTIPLY: val = FROM_SMALL_INT(lhs) * FROM_SMALL_INT(rhs); break;
            case RT_BINARY_OP_FLOOR_DIVIDE: val = FROM_SMALL_INT(lhs) / FROM_SMALL_INT(rhs); break;
#ifdef PY_FLOAT
            case RT_BINARY_OP_TRUE_DIVIDE: return py_obj_new_float((float_t)FROM_SMALL_INT(lhs) / (float_t)FROM_SMALL_INT(rhs));
#endif
            default: printf("%d\n", op); assert(0); val = 0;
        }
        if (fit_small_int(val)) {
            return TO_SMALL_INT(val);
        }
    } else if (IS_O(lhs, O_STR) && IS_O(rhs, O_STR)) {
        const char *lhs_str = qstr_str(((py_obj_base_t*)lhs)->u_str);
        const char *rhs_str = qstr_str(((py_obj_base_t*)rhs)->u_str);
        char *val;
        switch (op) {
            case RT_BINARY_OP_ADD:
            case RT_BINARY_OP_INPLACE_ADD: val = m_new(char, strlen(lhs_str) + strlen(rhs_str) + 1); strcpy(val, lhs_str); strcat(val, rhs_str); break;
            default: printf("%d\n", op); assert(0); val = NULL;
        }
        return py_obj_new_str(qstr_from_str_take(val));
    }
    assert(0);
    return py_const_none;
}

py_obj_t rt_compare_op(int op, py_obj_t lhs, py_obj_t rhs) {
    DEBUG_OP_printf("compare %d %p %p\n", op, lhs, rhs);
    if (IS_SMALL_INT(lhs) && IS_SMALL_INT(rhs)) {
        int cmp;
        switch (op) {
            case RT_COMPARE_OP_LESS: cmp = FROM_SMALL_INT(lhs) < FROM_SMALL_INT(rhs); break;
            case RT_COMPARE_OP_MORE: cmp = FROM_SMALL_INT(lhs) > FROM_SMALL_INT(rhs); break;
            default: assert(0); cmp = 0;
        }
        if (cmp) {
            return py_const_true;
        } else {
            return py_const_false;
        }
    }
    assert(0);
    return py_const_none;
}

py_obj_t rt_make_function_from_id(int unique_code_id) {
749
750
    DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
    if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien's avatar
Damien committed
751
752
753
754
755
756
        // illegal code id
        return py_const_none;
    }
    py_code_t *c = &unique_codes[unique_code_id];
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    switch (c->kind) {
757
758
759
760
761
762
        case PY_CODE_BYTE:
            o->kind = O_FUN_BC;
            o->u_fun_bc.n_args = c->n_args;
            o->u_fun_bc.code = c->u_byte.code;
            o->u_fun_bc.len = c->u_byte.len;
            break;
Damien's avatar
Damien committed
763
764
765
766
767
768
769
770
771
        case PY_CODE_NATIVE:
            switch (c->n_args) {
                case 0: o->kind = O_FUN_0; break;
                case 1: o->kind = O_FUN_1; break;
                case 2: o->kind = O_FUN_2; break;
                default: assert(0);
            }
            o->u_fun.fun = c->u_native.fun;
            break;
772
773
774
775
        case PY_CODE_INLINE_ASM:
            o->kind = O_FUN_ASM;
            o->u_fun_asm.n_args = c->n_args;
            o->u_fun_asm.fun = c->u_inline_asm.fun;
Damien's avatar
Damien committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
            break;
        default:
            assert(0);
    }
    return o;
}

py_obj_t rt_make_function_0(py_fun_0_t fun) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_FUN_0;
    o->u_fun.fun = fun;
    return o;
}

py_obj_t rt_make_function_1(py_fun_1_t fun) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_FUN_1;
    o->u_fun.fun = fun;
    return o;
}

py_obj_t rt_make_function_2(py_fun_2_t fun) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_FUN_2;
    o->u_fun.fun = fun;
    return o;
}

py_obj_t rt_make_function(int n_args, py_fun_t code) {
    // assumes code is a pointer to a py_fun_t (i think this is safe...)
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_FUN_N;
    o->u_fun.n_args = n_args;
809
    o->u_fun.fun = code;
Damien's avatar
Damien committed
810
811
812
    return o;
}

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
py_obj_t rt_call_function_0(py_obj_t fun) {
    return rt_call_function_n(fun, 0, NULL);
}

py_obj_t rt_call_function_1(py_obj_t fun, py_obj_t arg) {
    return rt_call_function_n(fun, 1, &arg);
}

py_obj_t rt_call_function_2(py_obj_t fun, py_obj_t arg1, py_obj_t arg2) {
    py_obj_t args[2];
    args[1] = arg1;
    args[0] = arg2;
    return rt_call_function_n(fun, 2, args);
}

typedef machine_uint_t (*inline_asm_fun_0_t)();
typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// convert a Python object to a sensible value for inline asm
machine_uint_t rt_convert_obj_for_inline_asm(py_obj_t obj) {
    // TODO for byte_array, pass pointer to the array
    if (IS_SMALL_INT(obj)) {
        return FROM_SMALL_INT(obj);
    } else if (obj == py_const_none) {
        return 0;
    } else if (obj == py_const_false) {
        return 0;
    } else if (obj == py_const_true) {
        return 1;
    } else {
        py_obj_base_t *o = obj;
        switch (o->kind) {
            case O_STR:
                // pointer to the string (it's probably constant though!)
                return (machine_uint_t)qstr_str(o->u_str);

            case O_FLOAT:
                // convert float to int (could also pass in float registers)
                return (machine_int_t)o->u_flt;

            case O_LIST:
                // pointer to start of list (could pass length, but then could use len(x) for that)
                return (machine_uint_t)o->u_list.items;

            default:
                // just pass along a pointer to the object
                return (machine_uint_t)obj;
        }
    }
}

// convert a return value from inline asm to a sensible Python object
py_obj_t rt_convert_val_from_inline_asm(machine_uint_t val) {
    return TO_SMALL_INT(val);
}

871
872
873
// args are in reverse order in the array
py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args) {
    int n_args_fun = 0;
Damien's avatar
Damien committed
874
875
    if (IS_O(fun, O_FUN_0)) {
        py_obj_base_t *o = fun;
876
877
878
879
880
        if (n_args != 0) {
            n_args_fun = 0;
            goto bad_n_args;
        }
        DEBUG_OP_printf("calling native %p()\n", o->u_fun.fun);
Damien's avatar
Damien committed
881
        return ((py_fun_0_t)o->u_fun.fun)();
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902

    } else if (IS_O(fun, O_FUN_1)) {
        py_obj_base_t *o = fun;
        if (n_args != 1) {
            n_args_fun = 1;
            goto bad_n_args;
        }
        DEBUG_OP_printf("calling native %p(%p)\n", o->u_fun.fun, args[0]);
        return ((py_fun_1_t)o->u_fun.fun)(args[0]);

    } else if (IS_O(fun, O_FUN_2)) {
        py_obj_base_t *o = fun;
        if (n_args != 2) {
            n_args_fun = 2;
            goto bad_n_args;
        }
        DEBUG_OP_printf("calling native %p(%p, %p)\n", o->u_fun.fun, args[1], args[0]);
        return ((py_fun_2_t)o->u_fun.fun)(args[1], args[0]);

    // TODO O_FUN_N

Damien's avatar
Damien committed
903
904
    } else if (IS_O(fun, O_FUN_BC)) {
        py_obj_base_t *o = fun;
905
906
907
908
909
910
911
        if (n_args != o->u_fun_bc.n_args) {
            n_args_fun = o->u_fun_bc.n_args;
            goto bad_n_args;
        }
        DEBUG_OP_printf("calling byte code %p(n_args=%d)\n", o->u_fun_bc.code, n_args);
        return py_execute_byte_code(o->u_fun_bc.code, o->u_fun_bc.len, args, n_args);

912
913
    } else if (IS_O(fun, O_FUN_ASM)) {
        py_obj_base_t *o = fun;
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
        if (n_args != o->u_fun_asm.n_args) {
            n_args_fun = o->u_fun_asm.n_args;
            goto bad_n_args;
        }
        DEBUG_OP_printf("calling inline asm %p(n_args=%d)\n", o->u_fun_asm.fun, n_args);
        machine_uint_t ret;
        if (n_args == 0) {
            ret = ((inline_asm_fun_0_t)o->u_fun_asm.fun)();
        } else if (n_args == 1) {
            ret = ((inline_asm_fun_1_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[0]));
        } else if (n_args == 2) {
            ret = ((inline_asm_fun_2_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[1]), rt_convert_obj_for_inline_asm(args[0]));
        } else if (n_args == 3) {
            ret = ((inline_asm_fun_3_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[2]), rt_convert_obj_for_inline_asm(args[1]), rt_convert_obj_for_inline_asm(args[0]));
        } else {
            assert(0);
            ret = 0;
        }
        return rt_convert_val_from_inline_asm(ret);

934
935
    } else if (IS_O(fun, O_BOUND_METH)) {
        py_obj_base_t *o = fun;
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
        DEBUG_OP_printf("calling bound method %p(self=%p, n_args=%d)\n", o->u_bound_meth.meth, o->u_bound_meth.self, n_args);
        if (n_args == 0) {
            return rt_call_function_n(o->u_bound_meth.meth, 1, &o->u_bound_meth.self);
        } else if (n_args == 1) {
            py_obj_t args2[2];
            args2[1] = o->u_bound_meth.self;
            args2[0] = args[0];
            return rt_call_function_n(o->u_bound_meth.meth, 2, args2);
        } else {
            // TODO not implemented
            assert(0);
            return py_const_none;
            //return rt_call_function_2(o->u_bound_meth.meth, n_args + 1, o->u_bound_meth.self + args);
        }

951
952
    } else if (IS_O(fun, O_CLASS)) {
        // instantiate an instance of a class
953
954
955
956
        if (n_args != 0) {
            n_args_fun = 0;
            goto bad_n_args;
        }
957
958
959
960
961
962
        DEBUG_OP_printf("instantiate object of class %p with no args\n", fun);
        py_obj_base_t *o = m_new(py_obj_base_t, 1);
        o->kind = O_OBJ;
        o->u_obj.class = fun;
        o->u_obj.members = py_map_new(MAP_QSTR, 0);
        return o;
Damien's avatar
Damien committed
963
964

    } else {
965
        printf("fun %p %d\n", fun, ((py_obj_base_t*)fun)->kind);
Damien's avatar
Damien committed
966
967
968
969
        assert(0);
        return py_const_none;
    }

970
971
972
973
bad_n_args:
    printf("TypeError: function takes %d positional arguments but %d were given\n", n_args_fun, n_args);
    assert(0);
    return py_const_none;
Damien's avatar
Damien committed
974
975
}

976
977
978
// args contains: arg(n_args-1)  arg(n_args-2)  ...  arg(0)  self/NULL  fun
// if n_args==0 then there are only self/NULL and fun
py_obj_t rt_call_method_n(int n_args, const py_obj_t *args) {
979
    DEBUG_OP_printf("call method %p(self=%p, n_args=%d)\n", args[n_args + 1], args[n_args], n_args);
980
981
982
    return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
}

Damien's avatar
Damien committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
// items are in reverse order
py_obj_t rt_build_list(int n_args, py_obj_t *items) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_LIST;
    o->u_list.alloc = n_args;
    if (o->u_list.alloc < 4) {
        o->u_list.alloc = 4;
    }
    o->u_list.len = n_args;
    o->u_list.items = m_new(py_obj_t, o->u_list.alloc);
    for (int i = 0; i < n_args; i++) {
        o->u_list.items[i] = items[n_args - i - 1];
    }
    return o;
}

py_obj_t py_set_lookup(py_obj_t o_in, py_obj_t index, bool add_if_not_found) {
    assert(IS_O(o_in, O_SET));
    py_obj_base_t *o = o_in;
    int hash = py_obj_hash(index);
    int pos = hash % o->u_set.alloc;
    for (;;) {
        py_obj_t elem = o->u_set.table[pos];
        if (elem == NULL) {
            // not in table
            if (add_if_not_found) {
                if (o->u_set.used + 1 >= o->u_set.alloc) {
                    // not enough room in table, rehash it
                    int old_alloc = o->u_set.alloc;
                    py_obj_t *old_table = o->u_set.table;
                    o->u_set.alloc = get_doubling_prime_greater_or_equal_to(o->u_set.alloc + 1);
                    o->u_set.used = 0;
                    o->u_set.table = m_new(py_obj_t, o->u_set.alloc);
                    for (int i = 0; i < old_alloc; i++) {
                        if (old_table[i] != NULL) {
                            py_set_lookup(o, old_table[i], true);
                        }
                    }
                    m_free(old_table);
                    // restart the search for the new element
                    pos = hash % o->u_set.alloc;
                } else {
                    o->u_set.used += 1;
                    o->u_set.table[pos] = index;
                    return index;
                }
            } else {
                return NULL;
            }
        } else if (py_obj_equal(elem, index)) {
            // found it
            return elem;
        } else {
            // not yet found, keep searching in this table
            pos = (pos + 1) % o->u_set.alloc;
        }
    }
}

py_obj_t rt_build_set(int n_args, py_obj_t *items) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_SET;
    o->u_set.alloc = get_doubling_prime_greater_or_equal_to(n_args + 1);
    o->u_set.used = 0;
    o->u_set.table = m_new(py_obj_t, o->u_set.alloc);
    for (int i = 0; i < o->u_set.alloc; i++) {
        o->u_set.table[i] = NULL;
    }
    for (int i = 0; i < n_args; i++) {
        py_set_lookup(o, items[i], true);
    }
    return o;
}

py_obj_t rt_build_map(int n_args) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_MAP;
    py_map_init(&o->u_map, MAP_PY_OBJ, n_args);
    return o;
}

py_obj_t rt_store_map(py_obj_t map, py_obj_t key, py_obj_t value) {
    assert(IS_O(map, O_MAP)); // should always be
    py_map_lookup(map, key, true)->value = value;
    return map;
}

py_obj_t build_bound_method(py_obj_t self, py_obj_t meth) {
    py_obj_base_t *o = m_new(py_obj_base_t, 1);
    o->kind = O_BOUND_METH;
    o->u_bound_meth.meth = meth;
    o->u_bound_meth.self = self;
    return o;
}

py_obj_t rt_load_attr(py_obj_t base, qstr attr) {
1079
    DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
Damien's avatar
Damien committed
1080
1081
1082
1083
    if (IS_O(base, O_LIST) && attr == q_append) {
        return build_bound_method(base, fun_list_append);
    } else if (IS_O(base, O_CLASS)) {
        py_obj_base_t *o = base;
1084
        py_map_elem_t *elem = py_qstr_map_lookup(o->u_class.locals, attr, false);
Damien's avatar
Damien committed
1085
        if (elem == NULL) {
1086
            goto no_attr;
Damien's avatar
Damien committed
1087
1088
        }
        return elem->value;
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
    } else if (IS_O(base, O_OBJ)) {
        // logic: look in obj members then class locals (TODO check this against CPython)
        py_obj_base_t *o = base;
        py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.members, attr, false);
        if (elem != NULL) {
            // object member, always treated as a value
            return elem->value;
        }
        elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false);
        if (elem != NULL) {
            if (py_obj_is_callable(elem->value)) {
                // class member is callable so build a bound method
                return build_bound_method(base, elem->value);
            } else {
                // class member is a value, so just return that value
                return elem->value;
            }
        }
        goto no_attr;
Damien's avatar
Damien committed
1108
    }
1109
1110
1111
1112
1113

no_attr:
    printf("AttributeError: '%s' object has no attribute '%s'\n", py_obj_get_type_str(base), qstr_str(attr));
    assert(0);
    return py_const_none;
Damien's avatar
Damien committed
1114
1115
1116
1117
1118
1119
1120
}

void rt_load_method(py_obj_t base, qstr attr, py_obj_t *dest) {
    DEBUG_OP_printf("load method %s\n", qstr_str(attr));
    if (IS_O(base, O_LIST) && attr == q_append) {
        dest[1] = fun_list_append;
        dest[0] = base;
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
        return;
    } else if (IS_O(base, O_OBJ)) {
        // logic: look in obj members then class locals (TODO check this against CPython)
        py_obj_base_t *o = base;
        py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.members, attr, false);
        if (elem != NULL) {
            // object member, always treated as a value
            dest[1] = elem->value;
            dest[0] = NULL;
            return;
        }
        elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false);
        if (elem != NULL) {
            if (py_obj_is_callable(elem->value)) {
                // class member is callable so build a bound method
                dest[1] = elem->value;
                dest[0] = base;
                return;
            } else {
                // class member is a value, so just return that value
                dest[1] = elem->value;
                dest[0] = NULL;
                return;
            }
        }
        goto no_attr;
    }

no_attr:
    dest[1] = rt_load_attr(base, attr);
    dest[0] = NULL;
}

void rt_store_attr(py_obj_t base, qstr attr, py_obj_t val) {
    DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), val);
    if (IS_O(base, O_OBJ)) {
        // logic: look in class locals (no add) then obj members (add) (TODO check this against CPython)
        py_obj_base_t *o = base;
        py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false);
        if (elem != NULL) {
            elem->value = val;
        } else {
            elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, true)->value = val;
        }
    } else {
        printf("?AttributeError: '%s' object has no attribute '%s'\n", py_obj_get_type_str(base), qstr_str(attr));
        assert(0);
    }
}

void rt_store_subscr(py_obj_t base, py_obj_t index, py_obj_t value) {
    if (IS_O(base, O_LIST) && IS_SMALL_INT(index)) {
        // list store
        py_obj_base_t *o = base;
        int idx = FROM_SMALL_INT(index);
        if (idx < 0) {
            idx += o->u_list.len;
        }
        if (0 <= idx && idx < o->u_list.len) {
            o->u_list.items[idx] = value;
        } else {
            assert(0);
        }
    } else if (IS_O(base, O_MAP)) {
        // map store
        py_map_lookup(base, index, true)->value = value;
Damien's avatar
Damien committed
1187
    } else {
1188
        assert(0);
Damien's avatar
Damien committed
1189
1190
1191
1192
1193
1194
1195
    }
}

void *rt_fun_table[RT_F_NUMBER_OF] = {
    rt_load_const_str,
    rt_load_name,
    rt_load_global,
1196
    rt_load_build_class,
Damien's avatar
Damien committed
1197
1198
1199
    rt_load_attr,
    rt_load_method,
    rt_store_name,
1200
    rt_store_attr,
Damien's avatar
Damien committed
1201
1202
1203
1204
1205
1206
1207
1208
    rt_store_subscr,
    rt_is_true,
    rt_unary_op,
    rt_build_list,
    rt_build_map,
    rt_store_map,
    rt_build_set,
    rt_make_function_from_id,
1209
    rt_call_function_n,
1210
    rt_call_method_n,
Damien's avatar
Damien committed
1211
1212
1213
1214
1215
1216
1217
1218
1219
    rt_binary_op,
    rt_compare_op,
};

/*
void rt_f_vector(rt_fun_kind_t fun_kind) {
    (rt_f_table[fun_kind])();
}
*/