emitcpy.c 26 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "misc.h"
#include "lexer.h"
#include "machine.h"
#include "parse.h"
#include "compile.h"
#include "scope.h"
#include "runtime.h"
#include "emit.h"

17
#ifdef EMIT_ENABLE_CPY
Damien's avatar
Damien committed
18

19
struct _emit_t {
Damien's avatar
Damien committed
20
21
22
23
24
25
26
27
    int pass;
    int next_label;
    int byte_code_offset;
    int stack_size;
    bool last_emit_was_return_value;

    scope_t *scope;

28
    uint max_num_labels;
Damien's avatar
Damien committed
29
30
31
    int *label_offsets;
};

32
static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien's avatar
Damien committed
33
34
}

35
static void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien's avatar
Damien committed
36
37
38
39
40
41
    emit->pass = pass;
    emit->next_label = 1;
    emit->byte_code_offset = 0;
    emit->stack_size = 0;
    emit->last_emit_was_return_value = false;
    emit->scope = scope;
42
43
    if (pass == PASS_2) {
        memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien's avatar
Damien committed
44
45
46
    }
}

47
static void emit_cpy_end_pass(emit_t *emit) {
Damien's avatar
Damien committed
48
49
50
51
52
53
    // check stack is back to zero size
    if (emit->stack_size != 0) {
        printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
    }
}

54
static bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
55
56
57
    return emit->last_emit_was_return_value;
}

58
static int emit_cpy_get_stack_size(emit_t *emit) {
Damien's avatar
Damien committed
59
60
61
    return emit->stack_size;
}

62
static void emit_cpy_set_stack_size(emit_t *emit, int size) {
Damien's avatar
Damien committed
63
64
65
    emit->stack_size = size;
}

66
static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
Damien's avatar
Damien committed
67
    emit->stack_size += stack_size_delta;
68
    if (emit->stack_size > emit->scope->stack_size) {
Damien's avatar
Damien committed
69
70
71
72
73
74
75
76
77
78
79
80
81
        emit->scope->stack_size = emit->stack_size;
    }
    emit->last_emit_was_return_value = false;
    if (emit->pass == PASS_3 && byte_code_size > 0) {
        if (emit->byte_code_offset >= 1000) {
            printf("%d ", emit->byte_code_offset);
        } else {
            printf("% 4d ", emit->byte_code_offset);
        }
    }
    emit->byte_code_offset += byte_code_size;
}

82
static void emit_cpy_label_assign(emit_t *emit, int l) {
Damien's avatar
Damien committed
83
    emit_pre(emit, 0, 0);
84
85
86
87
88
89
90
91
92
    assert(l < emit->max_num_labels);
    if (emit->pass == PASS_2) {
        // assign label offset
        assert(emit->label_offsets[l] == -1);
        emit->label_offsets[l] = emit->byte_code_offset;
    } else if (emit->pass == PASS_3) {
        // ensure label offset has not changed from PASS_2 to PASS_3
        assert(emit->label_offsets[l] == emit->byte_code_offset);
        //printf("l%d: (at %d)\n", l, emit->byte_code_offset);
Damien's avatar
Damien committed
93
94
95
    }
}

96
static void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
97
98
99
100
101
102
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("IMPORT_NAME %s\n", qstr_str(qstr));
    }
}

103
static void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
104
105
106
107
108
109
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("IMPORT_FROM %s\n", qstr_str(qstr));
    }
}

110
static void emit_cpy_import_star(emit_t *emit) {
Damien's avatar
Damien committed
111
112
113
114
115
116
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("IMPORT_STAR\n");
    }
}

117
static void emit_cpy_load_const_tok(emit_t *emit, py_token_kind_t tok) {
Damien's avatar
Damien committed
118
119
120
121
122
123
124
125
126
127
128
129
130
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST ");
        switch (tok) {
            case PY_TOKEN_KW_FALSE: printf("False"); break;
            case PY_TOKEN_KW_NONE: printf("None"); break;
            case PY_TOKEN_KW_TRUE: printf("True"); break;
            default: printf("?=%d\n", tok); return; assert(0);
        }
        printf("\n");
    }
}

131
static void emit_cpy_load_const_small_int(emit_t *emit, int arg) {
Damien's avatar
Damien committed
132
133
134
135
136
137
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST %d\n", arg);
    }
}

138
static void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
139
140
141
142
143
144
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

145
static void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
146
147
148
149
150
151
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

152
static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
153
154
155
156
157
158
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST '%s'\n", qstr_str(qstr));
    }
}

159
160
static void emit_cpy_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes);
static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien's avatar
Damien committed
161
162
163
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST ");
164
        emit_cpy_load_const_verbatim_quoted_str(emit, qstr, bytes);
Damien's avatar
Damien committed
165
166
167
168
        printf("\n");
    }
}

169
static void emit_cpy_load_const_verbatim_start(emit_t *emit) {
Damien's avatar
Damien committed
170
171
172
173
174
175
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST ");
    }
}

176
static void emit_cpy_load_const_verbatim_int(emit_t *emit, int val) {
Damien's avatar
Damien committed
177
178
179
180
181
    if (emit->pass == PASS_3) {
        printf("%d", val);
    }
}

182
static void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
Damien's avatar
Damien committed
183
184
185
186
187
    if (emit->pass == PASS_3) {
        printf("%s", str);
    }
}

188
static void emit_cpy_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
Damien's avatar
Damien committed
189
190
191
192
193
    if (emit->pass == PASS_3) {
        printf("%.*s", len, str);
    }
}

194
static void emit_cpy_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
Damien's avatar
Damien committed
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
    // TODO strings should be escaped before we get here
    if (emit->pass == PASS_3) {
        const char *str = qstr_str(qstr);
        int len = strlen(str);
        bool has_single_quote = false;
        bool has_double_quote = false;
        for (int i = 0; i < len; i++) {
            if (str[i] == '\'') {
                has_single_quote = true;
            } else if (str[i] == '"') {
                has_double_quote = true;
            }
        }
        if (bytes) {
            printf("b");
        }
        bool quote_single = false;
        if (has_single_quote && !has_double_quote) {
            printf("\"");
        } else {
            quote_single = true;
            printf("'");
        }
        for (int i = 0; i < len; i++) {
            if (str[i] == '\n') {
                printf("\\n");
            } else if (str[i] == '\\' && str[i + 1] == '\'') {
                i += 1;
                if (quote_single) {
                    printf("\\'");
                } else {
                    printf("'");
                }
            } else if (str[i] == '\'' && quote_single) {
                printf("\\'");
            } else {
                printf("%c", str[i]);
            }
        }
        if (has_single_quote && !has_double_quote) {
            printf("\"");
        } else {
            printf("'");
        }
    }
}

242
static void emit_cpy_load_const_verbatim_end(emit_t *emit) {
Damien's avatar
Damien committed
243
244
245
246
247
    if (emit->pass == PASS_3) {
        printf("\n");
    }
}

248
static void emit_cpy_load_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
249
250
251
252
253
254
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_NAME %s\n", qstr_str(qstr));
    }
}

255
static void emit_cpy_load_global(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
256
257
258
259
260
261
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
    }
}

262
static void emit_cpy_load_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
263
264
265
266
267
268
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_FAST %s\n", qstr_str(qstr));
    }
}

269
static void emit_cpy_load_deref(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
270
271
272
273
274
275
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_DEREF %s\n", qstr_str(qstr));
    }
}

276
static void emit_cpy_load_closure(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
277
278
279
280
281
282
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CLOSURE %s\n", qstr_str(qstr));
    }
}

283
static void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
284
285
286
287
288
289
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_ATTR %s\n", qstr_str(qstr));
    }
}

290
291
static void emit_cpy_load_method(emit_t *emit, qstr qstr) {
    emit_cpy_load_attr(emit, qstr);
Damien's avatar
Damien committed
292
293
}

294
static void emit_cpy_load_build_class(emit_t *emit) {
Damien's avatar
Damien committed
295
296
297
298
299
300
    emit_pre(emit, 1, 1);
    if (emit->pass == PASS_3) {
        printf("LOAD_BUILD_CLASS\n");
    }
}

301
static void emit_cpy_store_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
302
303
304
305
306
307
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_NAME %s\n", qstr_str(qstr));
    }
}

308
static void emit_cpy_store_global(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
309
310
311
312
313
314
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_GLOBAL %s\n", qstr_str(qstr));
    }
}

315
static void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
316
317
318
319
320
321
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_FAST %s\n", qstr_str(qstr));
    }
}

322
static void emit_cpy_store_deref(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
323
324
325
326
327
328
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_DEREF %s\n", qstr_str(qstr));
    }
}

329
static void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
330
331
332
333
334
335
    emit_pre(emit, -2, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_ATTR %s\n", qstr_str(qstr));
    }
}

336
static void emit_cpy_store_locals(emit_t *emit) {
Damien's avatar
Damien committed
337
338
339
340
341
342
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("STORE_LOCALS\n");
    }
}

343
static void emit_cpy_store_subscr(emit_t *emit) {
Damien's avatar
Damien committed
344
345
346
347
348
349
    emit_pre(emit, -3, 1);
    if (emit->pass == PASS_3) {
        printf("STORE_SUBSCR\n");
    }
}

350
static void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
351
352
353
354
355
356
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_NAME %s\n", qstr_str(qstr));
    }
}

357
static void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
358
359
360
361
362
363
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
    }
}

364
static void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
365
366
367
368
369
370
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_FAST %s\n", qstr_str(qstr));
    }
}

371
static void emit_cpy_delete_deref(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
372
373
374
375
376
377
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_DEREF %s\n", qstr_str(qstr));
    }
}

378
static void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
379
380
381
382
383
384
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_ATTR %s\n", qstr_str(qstr));
    }
}

385
static void emit_cpy_delete_subscr(emit_t *emit) {
Damien's avatar
Damien committed
386
387
388
389
390
391
    emit_pre(emit, -2, 1);
    if (emit->pass == PASS_3) {
        printf("DELETE_SUBSCR\n");
    }
}

392
static void emit_cpy_dup_top(emit_t *emit) {
Damien's avatar
Damien committed
393
394
395
396
397
398
    emit_pre(emit, 1, 1);
    if (emit->pass == PASS_3) {
        printf("DUP_TOP\n");
    }
}

399
static void emit_cpy_dup_top_two(emit_t *emit) {
Damien's avatar
Damien committed
400
401
402
403
404
405
    emit_pre(emit, 2, 1);
    if (emit->pass == PASS_3) {
        printf("DUP_TOP_TWO\n");
    }
}

406
static void emit_cpy_pop_top(emit_t *emit) {
Damien's avatar
Damien committed
407
408
409
410
411
412
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("POP_TOP\n");
    }
}

413
static void emit_cpy_rot_two(emit_t *emit) {
Damien's avatar
Damien committed
414
415
416
417
418
419
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("ROT_TWO\n");
    }
}

420
static void emit_cpy_rot_three(emit_t *emit) {
Damien's avatar
Damien committed
421
422
423
424
425
426
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("ROT_THREE\n");
    }
}

427
static void emit_cpy_jump(emit_t *emit, int label) {
Damien's avatar
Damien committed
428
429
430
431
432
433
434
435
436
437
438
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        int dest = emit->label_offsets[label];
        if (dest < emit->byte_code_offset) {
            printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
        } else {
            printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
        }
    }
}

439
static void emit_cpy_pop_jump_if_true(emit_t *emit, int label) {
Damien's avatar
Damien committed
440
441
442
443
444
445
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
    }
}

446
static void emit_cpy_pop_jump_if_false(emit_t *emit, int label) {
Damien's avatar
Damien committed
447
448
449
450
451
452
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
    }
}

453
static void emit_cpy_jump_if_true_or_pop(emit_t *emit, int label) {
Damien's avatar
Damien committed
454
455
456
457
458
459
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
    }
}

460
static void emit_cpy_jump_if_false_or_pop(emit_t *emit, int label) {
Damien's avatar
Damien committed
461
462
463
464
465
466
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
    }
}

467
static void emit_cpy_setup_loop(emit_t *emit, int label) {
Damien's avatar
Damien committed
468
469
470
471
472
473
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
    }
}

474
static void emit_cpy_break_loop(emit_t *emit, int label) {
Damien's avatar
Damien committed
475
476
477
478
479
480
481
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("BREAK_LOOP\n"); // CPython doesn't have label
        //printf("BREAK_LOOP %d\n", emit->label_offsets[label]);
    }
}

482
static void emit_cpy_continue_loop(emit_t *emit, int label) {
Damien's avatar
Damien committed
483
484
485
486
487
488
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
    }
}

489
static void emit_cpy_setup_with(emit_t *emit, int label) {
Damien's avatar
Damien committed
490
491
492
493
494
495
    emit_pre(emit, 7, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_WITH %d\n", emit->label_offsets[label]);
    }
}

496
static void emit_cpy_with_cleanup(emit_t *emit) {
Damien's avatar
Damien committed
497
498
499
500
501
502
    emit_pre(emit, -7, 1);
    if (emit->pass == PASS_3) {
        printf("WITH_CLEANUP\n");
    }
}

503
static void emit_cpy_setup_except(emit_t *emit, int label) {
Damien's avatar
Damien committed
504
505
506
507
508
509
    emit_pre(emit, 6, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
    }
}

510
static void emit_cpy_setup_finally(emit_t *emit, int label) {
Damien's avatar
Damien committed
511
512
513
514
515
516
    emit_pre(emit, 6, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
    }
}

517
static void emit_cpy_end_finally(emit_t *emit) {
Damien's avatar
Damien committed
518
519
520
521
522
523
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("END_FINALLY\n");
    }
}

524
static void emit_cpy_get_iter(emit_t *emit) {
Damien's avatar
Damien committed
525
526
527
528
529
530
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("GET_ITER\n");
    }
}

531
static void emit_cpy_for_iter(emit_t *emit, int label) {
Damien's avatar
Damien committed
532
533
534
535
536
537
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("FOR_ITER %d\n", emit->label_offsets[label]);
    }
}

538
static void emit_cpy_for_iter_end(emit_t *emit) {
Damien's avatar
Damien committed
539
540
541
    emit_pre(emit, -1, 0);
}

542
static void emit_cpy_pop_block(emit_t *emit) {
Damien's avatar
Damien committed
543
544
545
546
547
548
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("POP_BLOCK\n");
    }
}

549
static void emit_cpy_pop_except(emit_t *emit) {
Damien's avatar
Damien committed
550
551
552
553
554
555
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("POP_EXCEPT\n");
    }
}

556
static void emit_cpy_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien's avatar
Damien committed
557
558
559
560
561
562
563
564
565
566
567
568
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        switch (op) {
            case RT_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
            case RT_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
            case RT_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
            case RT_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
            default: assert(0);
        }
    }
}

569
static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien's avatar
Damien committed
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
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        switch (op) {
            case RT_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
            case RT_BINARY_OP_OR: printf("BINARY_OR\n"); break;
            case RT_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
            case RT_BINARY_OP_AND: printf("BINARY_AND\n"); break;
            case RT_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
            case RT_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
            case RT_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
            case RT_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
            case RT_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
            case RT_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
            case RT_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
            case RT_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
            case RT_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
            case RT_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
            case RT_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
            case RT_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
            case RT_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
            case RT_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
            case RT_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
            case RT_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
            case RT_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
            case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
            case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
            case RT_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
            case RT_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
            default: assert(0);
        }
    }
}

603
static void emit_cpy_compare_op(emit_t *emit, rt_compare_op_t op) {
Damien's avatar
Damien committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        switch (op) {
            case RT_COMPARE_OP_LESS: printf("COMPARE_OP <\n"); break;
            case RT_COMPARE_OP_MORE: printf("COMPARE_OP >\n"); break;
            case RT_COMPARE_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
            case RT_COMPARE_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
            case RT_COMPARE_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
            case RT_COMPARE_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
            case RT_COMPARE_OP_IN: printf("COMPARE_OP in\n"); break;
            case RT_COMPARE_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
            case RT_COMPARE_OP_IS: printf("COMPARE_OP is\n"); break;
            case RT_COMPARE_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
            case RT_COMPARE_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
            default: assert(0);
        }
    }
}

623
static void emit_cpy_build_tuple(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
624
625
626
627
628
629
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_TUPLE %d\n", n_args);
    }
}

630
static void emit_cpy_build_list(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
631
632
633
634
635
636
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_LIST %d\n", n_args);
    }
}

637
static void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien's avatar
Damien committed
638
639
640
641
642
643
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("LIST_APPEND %d\n", list_index);
    }
}

644
static void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
645
646
647
648
649
650
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_MAP %d\n", n_args);
    }
}

651
static void emit_cpy_store_map(emit_t *emit) {
Damien's avatar
Damien committed
652
653
654
655
656
657
    emit_pre(emit, -2, 1);
    if (emit->pass == PASS_3) {
        printf("STORE_MAP\n");
    }
}

658
static void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien's avatar
Damien committed
659
660
661
662
663
664
    emit_pre(emit, -2, 3);
    if (emit->pass == PASS_3) {
        printf("MAP_ADD %d\n", map_index);
    }
}

665
static void emit_cpy_build_set(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
666
667
668
669
670
671
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_SET %d\n", n_args);
    }
}

672
static void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien's avatar
Damien committed
673
674
675
676
677
678
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("SET_ADD %d\n", set_index);
    }
}

679
static void emit_cpy_build_slice(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
680
681
682
683
684
685
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_SLICE %d\n", n_args);
    }
}

686
static void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
687
688
689
690
691
692
    emit_pre(emit, -1 + n_args, 3);
    if (emit->pass == PASS_3) {
        printf("UNPACK_SEQUENCE %d\n", n_args);
    }
}

693
static void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien's avatar
Damien committed
694
695
696
697
698
699
    emit_pre(emit, -1 + n_left + n_right + 1, 3);
    if (emit->pass == PASS_3) {
        printf("UNPACK_EX %d\n", n_left | (n_right << 8));
    }
}

700
static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
Damien's avatar
Damien committed
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
    int s = 0;
    if (have_star_arg) {
        s += 1;
    }
    if (have_dbl_star_arg) {
        s += 1;
    }
    emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
    if (emit->pass == PASS_3) {
        if (have_star_arg) {
            if (have_dbl_star_arg) {
                printf("CALL_FUNCTION_VAR_KW");
            } else {
                printf("CALL_FUNCTION_VAR");
            }
        } else {
            if (have_dbl_star_arg) {
                printf("CALL_FUNCTION_KW");
            } else {
                printf("CALL_FUNCTION");
            }
        }
        printf(" %d, %d\n", n_positional, n_keyword);
    }
}

727
728
static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
    emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien's avatar
Damien committed
729
730
}

731
static void emit_cpy_return_value(emit_t *emit) {
Damien's avatar
Damien committed
732
733
734
735
736
737
738
    emit_pre(emit, -1, 1);
    emit->last_emit_was_return_value = true;
    if (emit->pass == PASS_3) {
        printf("RETURN_VALUE\n");
    }
}

739
static void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
740
741
742
743
744
745
    emit_pre(emit, -n_args, 3);
    if (emit->pass == PASS_3) {
        printf("RAISE_VARARGS %d\n", n_args);
    }
}

746
static void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST code %s\n", qstr_str(qstr));
    }
    // load qualified name
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST '");
        // code just to work out the qualname (or whatever it is)
        {
            int depth = 0;
            for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
                depth += 1;
            }
            for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
                scope_t *s = emit->scope;
                for (int i = 0; i < wanted_depth; i++) {
                    s = s->parent;
                }
                if (s->kind == SCOPE_FUNCTION) {
                    printf("%s.<locals>.", qstr_str(s->simple_name));
                } else if (s->kind == SCOPE_CLASS) {
                    printf("%s.", qstr_str(s->simple_name));
                }
            }
        }
        printf("%s'\n", qstr_str(qstr));
    }
}

777
778
static void emit_cpy_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
    load_cpy_const_code_and_name(emit, scope->simple_name);
Damien's avatar
Damien committed
779
780
781
782
783
784
    emit_pre(emit, -1 - n_default_params - 2 * n_dict_params, 3);
    if (emit->pass == PASS_3) {
        printf("MAKE_FUNCTION %d\n", (n_dict_params << 8) | n_default_params);
    }
}

785
786
static void emit_cpy_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
    load_cpy_const_code_and_name(emit, scope->simple_name);
Damien's avatar
Damien committed
787
788
789
790
791
792
    emit_pre(emit, -2 - n_default_params - 2 * n_dict_params, 3);
    if (emit->pass == PASS_3) {
        printf("MAKE_CLOSURE %d\n", (n_dict_params << 8) | n_default_params);
    }
}

793
static void emit_cpy_yield_value(emit_t *emit) {
Damien's avatar
Damien committed
794
795
796
797
798
799
800
801
802
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_2) {
        emit->scope->flags |= SCOPE_FLAG_GENERATOR;
    }
    if (emit->pass == PASS_3) {
        printf("YIELD_VALUE\n");
    }
}

803
static void emit_cpy_yield_from(emit_t *emit) {
Damien's avatar
Damien committed
804
805
806
807
808
809
810
811
812
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_2) {
        emit->scope->flags |= SCOPE_FLAG_GENERATOR;
    }
    if (emit->pass == PASS_3) {
        printf("YIELD_FROM\n");
    }
}

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
static const emit_method_table_t emit_cpy_method_table = {
    emit_cpy_set_native_types,
    emit_cpy_start_pass,
    emit_cpy_end_pass,
    emit_cpy_last_emit_was_return_value,
    emit_cpy_get_stack_size,
    emit_cpy_set_stack_size,

    emit_cpy_label_assign,
    emit_cpy_import_name,
    emit_cpy_import_from,
    emit_cpy_import_star,
    emit_cpy_load_const_tok,
    emit_cpy_load_const_small_int,
    emit_cpy_load_const_int,
    emit_cpy_load_const_dec,
    emit_cpy_load_const_id,
    emit_cpy_load_const_str,
    emit_cpy_load_const_verbatim_start,
    emit_cpy_load_const_verbatim_int,
    emit_cpy_load_const_verbatim_str,
    emit_cpy_load_const_verbatim_strn,
    emit_cpy_load_const_verbatim_quoted_str,
    emit_cpy_load_const_verbatim_end,
    emit_cpy_load_fast,
    emit_cpy_load_name,
    emit_cpy_load_global,
    emit_cpy_load_deref,
    emit_cpy_load_closure,
    emit_cpy_load_attr,
    emit_cpy_load_method,
    emit_cpy_load_build_class,
    emit_cpy_store_fast,
    emit_cpy_store_name,
    emit_cpy_store_global,
    emit_cpy_store_deref,
    emit_cpy_store_attr,
    emit_cpy_store_locals,
    emit_cpy_store_subscr,
    emit_cpy_delete_fast,
    emit_cpy_delete_name,
    emit_cpy_delete_global,
    emit_cpy_delete_deref,
    emit_cpy_delete_attr,
    emit_cpy_delete_subscr,
    emit_cpy_dup_top,
    emit_cpy_dup_top_two,
    emit_cpy_pop_top,
    emit_cpy_rot_two,
    emit_cpy_rot_three,
    emit_cpy_jump,
    emit_cpy_pop_jump_if_true,
    emit_cpy_pop_jump_if_false,
    emit_cpy_jump_if_true_or_pop,
    emit_cpy_jump_if_false_or_pop,
    emit_cpy_setup_loop,
    emit_cpy_break_loop,
    emit_cpy_continue_loop,
    emit_cpy_setup_with,
    emit_cpy_with_cleanup,
    emit_cpy_setup_except,
    emit_cpy_setup_finally,
    emit_cpy_end_finally,
    emit_cpy_get_iter,
    emit_cpy_for_iter,
    emit_cpy_for_iter_end,
    emit_cpy_pop_block,
    emit_cpy_pop_except,
    emit_cpy_unary_op,
    emit_cpy_binary_op,
    emit_cpy_compare_op,
    emit_cpy_build_tuple,
    emit_cpy_build_list,
    emit_cpy_list_append,
    emit_cpy_build_map,
    emit_cpy_store_map,
    emit_cpy_map_add,
    emit_cpy_build_set,
    emit_cpy_set_add,
    emit_cpy_build_slice,
    emit_cpy_unpack_sequence,
    emit_cpy_unpack_ex,
    emit_cpy_make_function,
    emit_cpy_make_closure,
    emit_cpy_call_function,
    emit_cpy_call_method,
    emit_cpy_return_value,
    emit_cpy_raise_varargs,
    emit_cpy_yield_value,
    emit_cpy_yield_from,
};

905
void emit_cpython_new(emit_t **emit_out, const emit_method_table_t **emit_method_table_out, uint max_num_labels) {
906
    emit_t *emit = m_new(emit_t, 1);
907
908
    emit->max_num_labels = max_num_labels;
    emit->label_offsets = m_new(int, max_num_labels);
909
910
911
912
913
914

    *emit_out = emit;
    *emit_method_table_out = &emit_cpy_method_table;
}

#endif // EMIT_ENABLE_CPY