emitcpy.c 27.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

xbe's avatar
xbe committed
27
#include <stdbool.h>
Damien's avatar
Damien committed
28
29
30
31
32
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

33
#include "mpconfig.h"
34
#include "misc.h"
35
#include "qstr.h"
Damien's avatar
Damien committed
36
37
#include "lexer.h"
#include "parse.h"
38
39
#include "obj.h"
#include "emitglue.h"
Damien's avatar
Damien committed
40
#include "scope.h"
41
#include "runtime0.h"
Damien's avatar
Damien committed
42
43
#include "emit.h"

44
// wrapper around everything in this file
45
#if MICROPY_EMIT_CPYTHON
Damien's avatar
Damien committed
46

47
struct _emit_t {
Damien's avatar
Damien committed
48
    int pass;
49
    int bytecode_offset;
Damien's avatar
Damien committed
50
51
52
53
54
    int stack_size;
    bool last_emit_was_return_value;

    scope_t *scope;

55
    uint max_num_labels;
Damien's avatar
Damien committed
56
57
58
    int *label_offsets;
};

59
60
61
62
63
64
65
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;
}

66
STATIC void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien's avatar
Damien committed
67
68
}

69
STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien's avatar
Damien committed
70
    emit->pass = pass;
71
    emit->bytecode_offset = 0;
Damien's avatar
Damien committed
72
73
74
    emit->stack_size = 0;
    emit->last_emit_was_return_value = false;
    emit->scope = scope;
75
    if (pass < MP_PASS_EMIT) {
76
        memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien's avatar
Damien committed
77
78
79
    }
}

80
STATIC void emit_cpy_end_pass(emit_t *emit) {
Damien's avatar
Damien committed
81
82
83
84
85
86
    // 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);
    }
}

87
STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
88
89
90
    return emit->last_emit_was_return_value;
}

91
92
STATIC void emit_cpy_adjust_stack_size(emit_t *emit, int delta) {
    emit->stack_size += delta;
Damien's avatar
Damien committed
93
94
}

95
STATIC void emit_cpy_set_source_line(emit_t *emit, int source_line) {
96
97
}

98
STATIC void emit_cpy_load_id(emit_t *emit, qstr qstr) {
99
    emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qstr);
100
101
}

102
STATIC void emit_cpy_store_id(emit_t *emit, qstr qstr) {
103
    emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qstr);
104
105
}

106
STATIC void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
107
    emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qstr);
108
109
}

110
// TODO: module-polymorphic function (read: name clash if made global)
111
static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
Damien's avatar
Damien committed
112
    emit->stack_size += stack_size_delta;
113
    if (emit->stack_size > emit->scope->stack_size) {
Damien's avatar
Damien committed
114
115
116
        emit->scope->stack_size = emit->stack_size;
    }
    emit->last_emit_was_return_value = false;
117
118
119
    if (emit->pass == MP_PASS_EMIT && bytecode_size > 0) {
        if (emit->bytecode_offset >= 1000) {
            printf("%d ", emit->bytecode_offset);
Damien's avatar
Damien committed
120
        } else {
121
            printf("% 4d ", emit->bytecode_offset);
Damien's avatar
Damien committed
122
123
        }
    }
124
    emit->bytecode_offset += bytecode_size;
Damien's avatar
Damien committed
125
126
}

127
STATIC void emit_cpy_label_assign(emit_t *emit, uint l) {
Damien's avatar
Damien committed
128
    emit_pre(emit, 0, 0);
129
    assert(l < emit->max_num_labels);
130
    if (emit->pass < MP_PASS_EMIT) {
131
132
        // assign label offset
        assert(emit->label_offsets[l] == -1);
133
        emit->label_offsets[l] = emit->bytecode_offset;
134
135
    } else {
        // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
136
137
        assert(emit->label_offsets[l] == emit->bytecode_offset);
        //printf("l%d: (at %d)\n", l, emit->bytecode_offset);
Damien's avatar
Damien committed
138
139
140
    }
}

141
STATIC void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
142
    emit_pre(emit, -1, 3);
143
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
144
145
146
147
        printf("IMPORT_NAME %s\n", qstr_str(qstr));
    }
}

148
STATIC void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
149
    emit_pre(emit, 1, 3);
150
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
151
152
153
154
        printf("IMPORT_FROM %s\n", qstr_str(qstr));
    }
}

155
STATIC void emit_cpy_import_star(emit_t *emit) {
Damien's avatar
Damien committed
156
    emit_pre(emit, -1, 1);
157
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
158
159
160
161
        printf("IMPORT_STAR\n");
    }
}

162
STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien's avatar
Damien committed
163
    emit_pre(emit, 1, 3);
164
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
165
166
        printf("LOAD_CONST ");
        switch (tok) {
167
168
169
            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
170
171
172
173
174
175
            default: printf("?=%d\n", tok); return; assert(0);
        }
        printf("\n");
    }
}

176
STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien's avatar
Damien committed
177
    emit_pre(emit, 1, 3);
178
    if (emit->pass == MP_PASS_EMIT) {
179
        printf("LOAD_CONST " INT_FMT "\n", arg);
Damien's avatar
Damien committed
180
181
182
    }
}

183
STATIC void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
184
    emit_pre(emit, 1, 3);
185
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
186
187
188
189
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

190
STATIC void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
191
    emit_pre(emit, 1, 3);
192
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
193
194
195
196
        printf("LOAD_CONST %s\n", qstr_str(qstr));
    }
}

197
STATIC void print_quoted_str(qstr qstr, bool bytes) {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    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");
    }
212
    int quote_char = '\'';
213
    if (has_single_quote && !has_double_quote) {
214
        quote_char = '"';
215
    }
216
217
218
219
220
    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 == '\\') {
221
            printf("\\\\");
222
223
224
225
226
        } else if (32 <= *s && *s <= 126) {
            printf("%c", *s);
        } else if (*s == '\n') {
            printf("\\n");
        // TODO add more escape codes here
227
        } else {
228
            printf("\\x%02x", (*s) & 0xff);
229
230
        }
    }
231
    printf("%c", quote_char);
Damien's avatar
Damien committed
232
233
}

234
STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien's avatar
Damien committed
235
    emit_pre(emit, 1, 3);
236
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
237
        printf("LOAD_CONST ");
238
239
        print_quoted_str(qstr, bytes);
        printf("\n");
Damien's avatar
Damien committed
240
241
242
    }
}

243
244
245
246
247
STATIC void emit_cpy_load_null(emit_t *emit) {
    // unused for cpy
    assert(0);
}

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

255
STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
256
    emit_pre(emit, 1, 3);
257
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
258
        printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
259
260
261
    }
}

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

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

276
STATIC void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
277
    emit_pre(emit, 0, 3);
278
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
279
280
281
282
        printf("LOAD_ATTR %s\n", qstr_str(qstr));
    }
}

283
STATIC void emit_cpy_load_method(emit_t *emit, qstr qstr) {
284
    emit_cpy_load_attr(emit, qstr);
Damien's avatar
Damien committed
285
286
}

287
STATIC void emit_cpy_load_build_class(emit_t *emit) {
Damien's avatar
Damien committed
288
    emit_pre(emit, 1, 1);
289
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
290
291
292
293
        printf("LOAD_BUILD_CLASS\n");
    }
}

294
295
STATIC void emit_cpy_load_subscr(emit_t *emit) {
    emit_pre(emit, -1, 1);
296
    if (emit->pass == MP_PASS_EMIT) {
297
298
299
300
        printf("BINARY_SUBSCR\n");
    }
}

301
STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
302
    emit_pre(emit, -1, 3);
303
    if (emit->pass == MP_PASS_EMIT) {
304
        printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
305
306
307
    }
}

308
STATIC void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
309
    emit_pre(emit, -1, 3);
310
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
311
        printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
312
313
314
    }
}

315
STATIC void emit_cpy_store_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
316
    emit_pre(emit, -1, 3);
317
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
318
        printf("STORE_NAME %s\n", qstr_str(qstr));
Damien's avatar
Damien committed
319
320
321
    }
}

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

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

336
STATIC void emit_cpy_store_subscr(emit_t *emit) {
337
    emit_pre(emit, -3, 1);
338
    if (emit->pass == MP_PASS_EMIT) {
339
        printf("STORE_SUBSCR\n");
Damien's avatar
Damien committed
340
341
342
    }
}

343
STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
344
    emit_pre(emit, 0, 3);
345
    if (emit->pass == MP_PASS_EMIT) {
346
        printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
347
348
349
    }
}

350
STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
351
    emit_pre(emit, 0, 3);
352
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
353
        printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien's avatar
Damien committed
354
355
356
    }
}

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

364
STATIC void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
365
    emit_pre(emit, 0, 3);
366
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
367
        printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
Damien's avatar
Damien committed
368
369
370
    }
}

371
STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
372
    emit_pre(emit, -1, 3);
373
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
374
375
376
377
        printf("DELETE_ATTR %s\n", qstr_str(qstr));
    }
}

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

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

392
STATIC void emit_cpy_dup_top_two(emit_t *emit) {
Damien's avatar
Damien committed
393
    emit_pre(emit, 2, 1);
394
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
395
396
397
398
        printf("DUP_TOP_TWO\n");
    }
}

399
STATIC void emit_cpy_pop_top(emit_t *emit) {
Damien's avatar
Damien committed
400
    emit_pre(emit, -1, 1);
401
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
402
403
404
405
        printf("POP_TOP\n");
    }
}

406
STATIC void emit_cpy_rot_two(emit_t *emit) {
Damien's avatar
Damien committed
407
    emit_pre(emit, 0, 1);
408
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
409
410
411
412
        printf("ROT_TWO\n");
    }
}

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

420
STATIC void emit_cpy_jump(emit_t *emit, uint label) {
Damien's avatar
Damien committed
421
    emit_pre(emit, 0, 3);
422
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
423
        int dest = emit->label_offsets[label];
424
        if (dest < emit->bytecode_offset) {
Damien's avatar
Damien committed
425
426
427
428
429
430
431
            printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
        } else {
            printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
        }
    }
}

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

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

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

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

460
STATIC void emit_cpy_break_loop(emit_t *emit, uint label, int except_depth) {
Damien's avatar
Damien committed
461
    emit_pre(emit, 0, 1);
462
    if (emit->pass == MP_PASS_EMIT) {
463
        printf("BREAK_LOOP\n");
Damien's avatar
Damien committed
464
465
466
    }
}

467
STATIC void emit_cpy_continue_loop(emit_t *emit, uint label, int except_depth) {
468
469
470
471
    if (except_depth == 0) {
        emit_cpy_jump(emit, label);
    } else {
        emit_pre(emit, 0, 3);
472
        if (emit->pass == MP_PASS_EMIT) {
473
474
            printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
        }
Damien's avatar
Damien committed
475
476
477
    }
}

478
STATIC void emit_cpy_setup_with(emit_t *emit, uint label) {
Damien's avatar
Damien committed
479
    emit_pre(emit, 7, 3);
480
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
481
482
483
484
        printf("SETUP_WITH %d\n", emit->label_offsets[label]);
    }
}

485
STATIC void emit_cpy_with_cleanup(emit_t *emit) {
Damien's avatar
Damien committed
486
    emit_pre(emit, -7, 1);
487
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
488
489
490
491
        printf("WITH_CLEANUP\n");
    }
}

492
STATIC void emit_cpy_setup_except(emit_t *emit, uint label) {
493
    emit_pre(emit, 0, 3);
494
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
495
496
497
498
        printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
    }
}

499
STATIC void emit_cpy_setup_finally(emit_t *emit, uint label) {
500
    emit_pre(emit, 0, 3);
501
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
502
503
504
505
        printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
    }
}

506
STATIC void emit_cpy_end_finally(emit_t *emit) {
Damien's avatar
Damien committed
507
    emit_pre(emit, -1, 1);
508
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
509
510
511
512
        printf("END_FINALLY\n");
    }
}

513
STATIC void emit_cpy_get_iter(emit_t *emit) {
Damien's avatar
Damien committed
514
    emit_pre(emit, 0, 1);
515
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
516
517
518
519
        printf("GET_ITER\n");
    }
}

520
STATIC void emit_cpy_for_iter(emit_t *emit, uint label) {
Damien's avatar
Damien committed
521
    emit_pre(emit, 1, 3);
522
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
523
524
525
526
        printf("FOR_ITER %d\n", emit->label_offsets[label]);
    }
}

527
STATIC void emit_cpy_for_iter_end(emit_t *emit) {
Damien's avatar
Damien committed
528
529
530
    emit_pre(emit, -1, 0);
}

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

538
STATIC void emit_cpy_pop_except(emit_t *emit) {
Damien's avatar
Damien committed
539
    emit_pre(emit, 0, 1);
540
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
541
542
543
544
        printf("POP_EXCEPT\n");
    }
}

Damien George's avatar
Damien George committed
545
STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien's avatar
Damien committed
546
    emit_pre(emit, 0, 1);
547
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
548
        switch (op) {
Damien George's avatar
Damien George committed
549
550
551
552
            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
553
554
555
556
557
            default: assert(0);
        }
    }
}

Damien George's avatar
Damien George committed
558
559
STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
    if (op <= MP_BINARY_OP_INPLACE_POWER) {
560
561
562
563
564
565
        // 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);
    }
566
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
567
        switch (op) {
Damien George's avatar
Damien George committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            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
603
604
605
606
607
            default: assert(0);
        }
    }
}

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

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

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

629
STATIC void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
630
    emit_pre(emit, 1, 3);
631
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
632
633
634
635
        printf("BUILD_MAP %d\n", n_args);
    }
}

636
STATIC void emit_cpy_store_map(emit_t *emit) {
Damien's avatar
Damien committed
637
    emit_pre(emit, -2, 1);
638
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
639
640
641
642
        printf("STORE_MAP\n");
    }
}

643
STATIC void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien's avatar
Damien committed
644
    emit_pre(emit, -2, 3);
645
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
646
647
648
649
        printf("MAP_ADD %d\n", map_index);
    }
}

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

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

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

671
STATIC void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
672
    emit_pre(emit, -1 + n_args, 3);
673
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
674
675
676
677
        printf("UNPACK_SEQUENCE %d\n", n_args);
    }
}

678
STATIC void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien's avatar
Damien committed
679
    emit_pre(emit, -1 + n_left + n_right + 1, 3);
680
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
681
682
683
684
        printf("UNPACK_EX %d\n", n_left | (n_right << 8));
    }
}

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

712
713
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
714
715
}

716
STATIC void emit_cpy_return_value(emit_t *emit) {
Damien's avatar
Damien committed
717
718
    emit_pre(emit, -1, 1);
    emit->last_emit_was_return_value = true;
719
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
720
721
722
723
        printf("RETURN_VALUE\n");
    }
}

724
STATIC void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
725
    emit_pre(emit, -n_args, 3);
726
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
727
728
729
730
        printf("RAISE_VARARGS %d\n", n_args);
    }
}

731
STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien's avatar
Damien committed
732
    emit_pre(emit, 1, 3);
733
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
734
735
736
737
        printf("LOAD_CONST code %s\n", qstr_str(qstr));
    }
    // load qualified name
    emit_pre(emit, 1, 3);
738
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
        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));
    }
}

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

770
771
STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
    emit_cpy_build_tuple(emit, n_closed_over);
772
    load_cpy_const_code_and_name(emit, scope->simple_name);
773
    emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
774
    if (emit->pass == MP_PASS_EMIT) {
775
        printf("MAKE_CLOSURE %d\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien's avatar
Damien committed
776
777
778
    }
}

779
STATIC void emit_cpy_yield_value(emit_t *emit) {
Damien's avatar
Damien committed
780
    emit_pre(emit, 0, 1);
781
782
    emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
783
784
785
786
        printf("YIELD_VALUE\n");
    }
}

787
STATIC void emit_cpy_yield_from(emit_t *emit) {
Damien's avatar
Damien committed
788
    emit_pre(emit, -1, 1);
789
790
    emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
    if (emit->pass == MP_PASS_EMIT) {
Damien's avatar
Damien committed
791
792
793
794
        printf("YIELD_FROM\n");
    }
}

795
796
797
798
799
800
801
802
STATIC void emit_cpy_start_except_handler(emit_t *emit) {
    emit_cpy_adjust_stack_size(emit, 3); // stack adjust for the 3 exception items
}

STATIC void emit_cpy_end_except_handler(emit_t *emit) {
    emit_cpy_adjust_stack_size(emit, -5); // stack adjust
}

803
804
STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
    emit_pre(emit, 1, 3);
805
    if (emit->pass == MP_PASS_EMIT) {
806
807
808
809
810
811
        printf("LOAD_CONST %s\n", str);
    }
}

STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
    emit_pre(emit, 1, 3);
812
    if (emit->pass == MP_PASS_EMIT) {
813
814
815
816
817
818
        printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
    }
}

STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) {
    emit_pre(emit, 0, 3);
819
    if (emit->pass == MP_PASS_EMIT) {
820
821
822
823
        printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
    }
}

824
const emit_method_table_t emit_cpython_method_table = {
825
826
827
828
    emit_cpy_set_native_types,
    emit_cpy_start_pass,
    emit_cpy_end_pass,
    emit_cpy_last_emit_was_return_value,
829
    emit_cpy_adjust_stack_size,
830
    emit_cpy_set_source_line,
831

832
833
834
835
    emit_cpy_load_id,
    emit_cpy_store_id,
    emit_cpy_delete_id,

836
837
838
839
840
841
842
843
844
    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_str,
845
    emit_cpy_load_null,
846
847
    emit_cpy_load_fast,
    emit_cpy_load_deref,
Damien's avatar
Damien committed
848
849
    emit_cpy_load_name,
    emit_cpy_load_global,
850
851
852
    emit_cpy_load_attr,
    emit_cpy_load_method,
    emit_cpy_load_build_class,
853
    emit_cpy_load_subscr,
854
    emit_cpy_store_fast,
Damien's avatar
Damien committed
855
    emit_cpy_store_deref,
856
857
858
859
860
    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
861
    emit_cpy_delete_deref,
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
905
906
907
908
    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_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,
909

910
911
912
    emit_cpy_start_except_handler,
    emit_cpy_end_except_handler,

913
914
915
916
    // emitcpy specific functions
    emit_cpy_load_const_verbatim_str,
    emit_cpy_load_closure,
    emit_cpy_setup_loop,
917
918
};

919
#endif // MICROPY_EMIT_CPYTHON