emitcpy.c 25.3 KB
Newer Older
xbe's avatar
xbe committed
1
#include <stdbool.h>
Damien's avatar
Damien committed
2
3
4
5
6
7
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "misc.h"
8
#include "mpconfig.h"
9
#include "qstr.h"
Damien's avatar
Damien committed
10
11
12
#include "lexer.h"
#include "parse.h"
#include "scope.h"
13
#include "runtime0.h"
Damien's avatar
Damien committed
14
15
#include "emit.h"

16
// wrapper around everything in this file
17
#if MICROPY_EMIT_CPYTHON
Damien's avatar
Damien committed
18

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

    scope_t *scope;

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

31
32
33
34
35
36
37
emit_t *emit_cpython_new(uint max_num_labels) {
    emit_t *emit = m_new(emit_t, 1);
    emit->max_num_labels = max_num_labels;
    emit->label_offsets = m_new(int, max_num_labels);
    return emit;
}

38
STATIC void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien's avatar
Damien committed
39
40
}

41
STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien's avatar
Damien committed
42
43
44
45
46
    emit->pass = pass;
    emit->byte_code_offset = 0;
    emit->stack_size = 0;
    emit->last_emit_was_return_value = false;
    emit->scope = scope;
47
48
    if (pass == PASS_2) {
        memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien's avatar
Damien committed
49
50
51
    }
}

52
STATIC void emit_cpy_end_pass(emit_t *emit) {
Damien's avatar
Damien committed
53
54
55
56
57
58
    // 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);
    }
}

59
STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
60
61
62
    return emit->last_emit_was_return_value;
}

63
STATIC int emit_cpy_get_stack_size(emit_t *emit) {
Damien's avatar
Damien committed
64
65
66
    return emit->stack_size;
}

67
STATIC void emit_cpy_set_stack_size(emit_t *emit, int size) {
Damien's avatar
Damien committed
68
69
70
    emit->stack_size = size;
}

71
STATIC void emit_cpy_set_source_line(emit_t *emit, int source_line) {
72
73
}

74
STATIC void emit_cpy_load_id(emit_t *emit, qstr qstr) {
75
    emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qstr);
76
77
}

78
STATIC void emit_cpy_store_id(emit_t *emit, qstr qstr) {
79
    emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qstr);
80
81
}

82
STATIC void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
83
    emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qstr);
84
85
}

86
// TODO: module-polymorphic function (read: name clash if made global)
87
static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
Damien's avatar
Damien committed
88
    emit->stack_size += stack_size_delta;
89
    if (emit->stack_size > emit->scope->stack_size) {
Damien's avatar
Damien committed
90
91
92
93
94
95
96
97
98
99
100
101
102
        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;
}

103
STATIC void emit_cpy_label_assign(emit_t *emit, int l) {
Damien's avatar
Damien committed
104
    emit_pre(emit, 0, 0);
105
106
107
108
109
110
111
112
113
    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
114
115
116
    }
}

117
STATIC void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
118
119
120
121
122
123
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("IMPORT_NAME %s\n", qstr_str(qstr));
    }
}

124
STATIC void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
125
126
127
128
129
130
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("IMPORT_FROM %s\n", qstr_str(qstr));
    }
}

131
STATIC void emit_cpy_import_star(emit_t *emit) {
Damien's avatar
Damien committed
132
133
134
135
136
137
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("IMPORT_STAR\n");
    }
}

138
STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien's avatar
Damien committed
139
140
141
142
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST ");
        switch (tok) {
143
144
145
            case MP_TOKEN_KW_FALSE: printf("False"); break;
            case MP_TOKEN_KW_NONE: printf("None"); break;
            case MP_TOKEN_KW_TRUE: printf("True"); break;
Damien's avatar
Damien committed
146
147
148
149
150
151
            default: printf("?=%d\n", tok); return; assert(0);
        }
        printf("\n");
    }
}

152
STATIC void emit_cpy_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien's avatar
Damien committed
153
154
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
155
        printf("LOAD_CONST " INT_FMT "\n", arg);
Damien's avatar
Damien committed
156
157
158
    }
}

159
STATIC void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
160
161
162
163
164
165
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

166
STATIC void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
167
168
169
170
171
172
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

173
STATIC void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
174
175
176
177
178
179
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST '%s'\n", qstr_str(qstr));
    }
}

180
STATIC void print_quoted_str(qstr qstr, bool bytes) {
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    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");
    }
195
    int quote_char = '\'';
196
    if (has_single_quote && !has_double_quote) {
197
        quote_char = '"';
198
    }
199
200
201
202
203
    printf("%c", quote_char);
    for (const char *s = str, *top = str + len; s < top; s++) {
        if (*s == quote_char) {
            printf("\\%c", quote_char);
        } else if (*s == '\\') {
204
            printf("\\\\");
205
206
207
208
209
        } else if (32 <= *s && *s <= 126) {
            printf("%c", *s);
        } else if (*s == '\n') {
            printf("\\n");
        // TODO add more escape codes here
210
        } else {
211
            printf("\\x%02x", (*s) & 0xff);
212
213
        }
    }
214
    printf("%c", quote_char);
Damien's avatar
Damien committed
215
216
}

217
STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien's avatar
Damien committed
218
219
220
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_CONST ");
221
222
        print_quoted_str(qstr, bytes);
        printf("\n");
Damien's avatar
Damien committed
223
224
225
    }
}

226
STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
227
    emit_pre(emit, 1, 3);
Damien's avatar
Damien committed
228
    if (emit->pass == PASS_3) {
229
        printf("LOAD_CONST %s\n", str);
Damien's avatar
Damien committed
230
231
232
    }
}

233
STATIC void emit_cpy_load_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
234
235
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
236
        printf("LOAD_FAST %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
237
238
239
    }
}

240
STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
241
242
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
243
        printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
244
245
246
    }
}

247
STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
248
249
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
250
        printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
251
252
253
    }
}

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

261
STATIC void emit_cpy_load_global(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
262
263
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
264
        printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
Damien's avatar
Damien committed
265
266
267
    }
}

268
STATIC void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
269
270
271
272
273
274
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
        printf("LOAD_ATTR %s\n", qstr_str(qstr));
    }
}

275
STATIC void emit_cpy_load_method(emit_t *emit, qstr qstr) {
276
    emit_cpy_load_attr(emit, qstr);
Damien's avatar
Damien committed
277
278
}

279
STATIC void emit_cpy_load_build_class(emit_t *emit) {
Damien's avatar
Damien committed
280
281
282
283
284
285
    emit_pre(emit, 1, 1);
    if (emit->pass == PASS_3) {
        printf("LOAD_BUILD_CLASS\n");
    }
}

286
STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
287
288
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
289
        printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
290
291
292
    }
}

293
STATIC void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
294
295
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
296
        printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
297
298
299
    }
}

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

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

314
STATIC void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
315
316
317
318
319
320
    emit_pre(emit, -2, 3);
    if (emit->pass == PASS_3) {
        printf("STORE_ATTR %s\n", qstr_str(qstr));
    }
}

321
STATIC void emit_cpy_store_subscr(emit_t *emit) {
322
    emit_pre(emit, -3, 1);
Damien's avatar
Damien committed
323
    if (emit->pass == PASS_3) {
324
        printf("STORE_SUBSCR\n");
Damien's avatar
Damien committed
325
326
327
    }
}

328
STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
329
330
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
331
        printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
332
333
334
    }
}

335
STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
336
337
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
338
        printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
339
340
341
    }
}

342
STATIC void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
343
344
    emit_pre(emit, 0, 3);
    if (emit->pass == PASS_3) {
Damien's avatar
Damien committed
345
        printf("DELETE_NAME %s\n", qstr_str(qstr));
Damien's avatar
Damien committed
346
347
348
    }
}

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

356
STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
357
358
359
360
361
362
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("DELETE_ATTR %s\n", qstr_str(qstr));
    }
}

363
STATIC void emit_cpy_delete_subscr(emit_t *emit) {
Damien's avatar
Damien committed
364
365
366
367
368
369
    emit_pre(emit, -2, 1);
    if (emit->pass == PASS_3) {
        printf("DELETE_SUBSCR\n");
    }
}

370
STATIC void emit_cpy_dup_top(emit_t *emit) {
Damien's avatar
Damien committed
371
372
373
374
375
376
    emit_pre(emit, 1, 1);
    if (emit->pass == PASS_3) {
        printf("DUP_TOP\n");
    }
}

377
STATIC void emit_cpy_dup_top_two(emit_t *emit) {
Damien's avatar
Damien committed
378
379
380
381
382
383
    emit_pre(emit, 2, 1);
    if (emit->pass == PASS_3) {
        printf("DUP_TOP_TWO\n");
    }
}

384
STATIC void emit_cpy_pop_top(emit_t *emit) {
Damien's avatar
Damien committed
385
386
387
388
389
390
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("POP_TOP\n");
    }
}

391
STATIC void emit_cpy_rot_two(emit_t *emit) {
Damien's avatar
Damien committed
392
393
394
395
396
397
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("ROT_TWO\n");
    }
}

398
STATIC void emit_cpy_rot_three(emit_t *emit) {
Damien's avatar
Damien committed
399
400
401
402
403
404
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("ROT_THREE\n");
    }
}

405
STATIC void emit_cpy_jump(emit_t *emit, int label) {
Damien's avatar
Damien committed
406
407
408
409
410
411
412
413
414
415
416
    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]);
        }
    }
}

417
STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, int label) {
Damien's avatar
Damien committed
418
419
420
421
422
423
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
    }
}

424
STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, int label) {
Damien's avatar
Damien committed
425
426
427
428
429
430
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
    }
}

431
STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, int label) {
Damien's avatar
Damien committed
432
433
434
435
436
437
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
    }
}

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

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

452
STATIC void emit_cpy_break_loop(emit_t *emit, int label, int except_depth) {
Damien's avatar
Damien committed
453
454
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
455
        printf("BREAK_LOOP\n");
Damien's avatar
Damien committed
456
457
458
    }
}

459
STATIC void emit_cpy_continue_loop(emit_t *emit, int label, int except_depth) {
460
461
462
463
464
465
466
    if (except_depth == 0) {
        emit_cpy_jump(emit, label);
    } else {
        emit_pre(emit, 0, 3);
        if (emit->pass == PASS_3) {
            printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
        }
Damien's avatar
Damien committed
467
468
469
    }
}

470
STATIC void emit_cpy_setup_with(emit_t *emit, int label) {
Damien's avatar
Damien committed
471
472
473
474
475
476
    emit_pre(emit, 7, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_WITH %d\n", emit->label_offsets[label]);
    }
}

477
STATIC void emit_cpy_with_cleanup(emit_t *emit) {
Damien's avatar
Damien committed
478
479
480
481
482
483
    emit_pre(emit, -7, 1);
    if (emit->pass == PASS_3) {
        printf("WITH_CLEANUP\n");
    }
}

484
STATIC void emit_cpy_setup_except(emit_t *emit, int label) {
Damien's avatar
Damien committed
485
486
487
488
489
490
    emit_pre(emit, 6, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
    }
}

491
STATIC void emit_cpy_setup_finally(emit_t *emit, int label) {
Damien's avatar
Damien committed
492
493
494
495
496
497
    emit_pre(emit, 6, 3);
    if (emit->pass == PASS_3) {
        printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
    }
}

498
STATIC void emit_cpy_end_finally(emit_t *emit) {
Damien's avatar
Damien committed
499
500
501
502
503
504
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_3) {
        printf("END_FINALLY\n");
    }
}

505
STATIC void emit_cpy_get_iter(emit_t *emit) {
Damien's avatar
Damien committed
506
507
508
509
510
511
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("GET_ITER\n");
    }
}

512
STATIC void emit_cpy_for_iter(emit_t *emit, int label) {
Damien's avatar
Damien committed
513
514
515
516
517
518
    emit_pre(emit, 1, 3);
    if (emit->pass == PASS_3) {
        printf("FOR_ITER %d\n", emit->label_offsets[label]);
    }
}

519
STATIC void emit_cpy_for_iter_end(emit_t *emit) {
Damien's avatar
Damien committed
520
521
522
    emit_pre(emit, -1, 0);
}

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

530
STATIC void emit_cpy_pop_except(emit_t *emit) {
Damien's avatar
Damien committed
531
532
533
534
535
536
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        printf("POP_EXCEPT\n");
    }
}

Damien George's avatar
Damien George committed
537
STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien's avatar
Damien committed
538
539
540
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_3) {
        switch (op) {
Damien George's avatar
Damien George committed
541
542
543
544
            case MP_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
            case MP_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
            case MP_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
            case MP_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
Damien's avatar
Damien committed
545
546
547
548
549
            default: assert(0);
        }
    }
}

Damien George's avatar
Damien George committed
550
551
STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
    if (op <= MP_BINARY_OP_INPLACE_POWER) {
552
553
554
555
556
557
        // CPython uses a byte code for each binary op
        emit_pre(emit, -1, 1);
    } else {
        // CPython uses a byte code plus an argument for compare ops
        emit_pre(emit, -1, 3);
    }
Damien's avatar
Damien committed
558
559
    if (emit->pass == PASS_3) {
        switch (op) {
Damien George's avatar
Damien George committed
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
            case MP_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
            case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
            case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
            case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break;
            case MP_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
            case MP_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
            case MP_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
            case MP_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
            case MP_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
            case MP_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
            case MP_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
            case MP_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
            case MP_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
            case MP_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
            case MP_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
            case MP_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
            case MP_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
            case MP_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
            case MP_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
            case MP_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
            case MP_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
            case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
            case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
            case MP_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
            case MP_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
            case MP_BINARY_OP_LESS: printf("COMPARE_OP <\n"); break;
            case MP_BINARY_OP_MORE: printf("COMPARE_OP >\n"); break;
            case MP_BINARY_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
            case MP_BINARY_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
            case MP_BINARY_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
            case MP_BINARY_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
            case MP_BINARY_OP_IN: printf("COMPARE_OP in\n"); break;
            case MP_BINARY_OP_IS: printf("COMPARE_OP is\n"); break;
            case MP_BINARY_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
            case MP_BINARY_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
            case MP_BINARY_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
Damien's avatar
Damien committed
596
597
598
599
600
            default: assert(0);
        }
    }
}

601
STATIC void emit_cpy_build_tuple(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
602
603
604
605
606
607
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_TUPLE %d\n", n_args);
    }
}

608
STATIC void emit_cpy_build_list(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
609
610
611
612
613
614
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_LIST %d\n", n_args);
    }
}

615
STATIC void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien's avatar
Damien committed
616
617
618
619
620
621
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("LIST_APPEND %d\n", list_index);
    }
}

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

629
STATIC void emit_cpy_store_map(emit_t *emit) {
Damien's avatar
Damien committed
630
631
632
633
634
635
    emit_pre(emit, -2, 1);
    if (emit->pass == PASS_3) {
        printf("STORE_MAP\n");
    }
}

636
STATIC void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien's avatar
Damien committed
637
638
639
640
641
642
    emit_pre(emit, -2, 3);
    if (emit->pass == PASS_3) {
        printf("MAP_ADD %d\n", map_index);
    }
}

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

650
STATIC void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien's avatar
Damien committed
651
652
653
654
655
656
    emit_pre(emit, -1, 3);
    if (emit->pass == PASS_3) {
        printf("SET_ADD %d\n", set_index);
    }
}

657
STATIC void emit_cpy_build_slice(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
658
659
660
661
662
663
    emit_pre(emit, 1 - n_args, 3);
    if (emit->pass == PASS_3) {
        printf("BUILD_SLICE %d\n", n_args);
    }
}

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

671
STATIC void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien's avatar
Damien committed
672
673
674
675
676
677
    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));
    }
}

678
STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
Damien's avatar
Damien committed
679
    int s = 0;
680
    if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien's avatar
Damien committed
681
682
        s += 1;
    }
683
    if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien's avatar
Damien committed
684
685
686
687
        s += 1;
    }
    emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
    if (emit->pass == PASS_3) {
688
689
        if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
            if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien's avatar
Damien committed
690
691
692
693
694
                printf("CALL_FUNCTION_VAR_KW");
            } else {
                printf("CALL_FUNCTION_VAR");
            }
        } else {
695
            if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien's avatar
Damien committed
696
697
698
699
700
701
702
703
704
                printf("CALL_FUNCTION_KW");
            } else {
                printf("CALL_FUNCTION");
            }
        }
        printf(" %d, %d\n", n_positional, n_keyword);
    }
}

705
706
STATIC void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
    emit_cpy_call_function(emit, n_positional, n_keyword, star_flags);
Damien's avatar
Damien committed
707
708
}

709
STATIC void emit_cpy_return_value(emit_t *emit) {
Damien's avatar
Damien committed
710
711
712
713
714
715
716
    emit_pre(emit, -1, 1);
    emit->last_emit_was_return_value = true;
    if (emit->pass == PASS_3) {
        printf("RETURN_VALUE\n");
    }
}

717
STATIC void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
718
719
720
721
722
723
    emit_pre(emit, -n_args, 3);
    if (emit->pass == PASS_3) {
        printf("RAISE_VARARGS %d\n", n_args);
    }
}

724
STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
    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));
    }
}

755
STATIC void emit_cpy_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
756
    load_cpy_const_code_and_name(emit, scope->simple_name);
757
    emit_pre(emit, -1 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien's avatar
Damien committed
758
    if (emit->pass == PASS_3) {
759
        printf("MAKE_FUNCTION %d\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien's avatar
Damien committed
760
761
762
    }
}

763
STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
764
    load_cpy_const_code_and_name(emit, scope->simple_name);
765
    emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien's avatar
Damien committed
766
    if (emit->pass == PASS_3) {
767
        printf("MAKE_CLOSURE %d\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien's avatar
Damien committed
768
769
770
    }
}

771
STATIC void emit_cpy_yield_value(emit_t *emit) {
Damien's avatar
Damien committed
772
773
    emit_pre(emit, 0, 1);
    if (emit->pass == PASS_2) {
774
        emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien's avatar
Damien committed
775
776
777
778
779
780
    }
    if (emit->pass == PASS_3) {
        printf("YIELD_VALUE\n");
    }
}

781
STATIC void emit_cpy_yield_from(emit_t *emit) {
Damien's avatar
Damien committed
782
783
    emit_pre(emit, -1, 1);
    if (emit->pass == PASS_2) {
784
        emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien's avatar
Damien committed
785
786
787
788
789
790
    }
    if (emit->pass == PASS_3) {
        printf("YIELD_FROM\n");
    }
}

791
const emit_method_table_t emit_cpython_method_table = {
792
793
794
795
796
797
    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,
798
    emit_cpy_set_source_line,
799

800
801
802
803
    emit_cpy_load_id,
    emit_cpy_store_id,
    emit_cpy_delete_id,

804
805
806
807
808
809
810
811
812
813
814
815
816
817
    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_str,
    emit_cpy_load_fast,
    emit_cpy_load_deref,
    emit_cpy_load_closure,
Damien's avatar
Damien committed
818
819
    emit_cpy_load_name,
    emit_cpy_load_global,
820
821
822
823
    emit_cpy_load_attr,
    emit_cpy_load_method,
    emit_cpy_load_build_class,
    emit_cpy_store_fast,
Damien's avatar
Damien committed
824
    emit_cpy_store_deref,
825
826
827
828
829
    emit_cpy_store_name,
    emit_cpy_store_global,
    emit_cpy_store_attr,
    emit_cpy_store_subscr,
    emit_cpy_delete_fast,
Damien's avatar
Damien committed
830
    emit_cpy_delete_deref,
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
    emit_cpy_delete_name,
    emit_cpy_delete_global,
    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_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,
};

881
#endif // MICROPY_EMIT_CPYTHON