emitbc.c 33.4 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
34
#include "py/emit.h"
#include "py/bc0.h"
Damien's avatar
Damien committed
35

36
37
#if !MICROPY_EMIT_CPYTHON

38
39
40
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)

41
struct _emit_t {
42
    pass_kind_t pass : 8;
43
    mp_uint_t last_emit_was_return_value : 8;
44

Damien's avatar
Damien committed
45
46
47
48
    int stack_size;

    scope_t *scope;

49
50
    mp_uint_t last_source_line_offset;
    mp_uint_t last_source_line;
51

52
53
    mp_uint_t max_num_labels;
    mp_uint_t *label_offsets;
Damien's avatar
Damien committed
54

55
56
57
58
    mp_uint_t code_info_offset;
    mp_uint_t code_info_size;
    mp_uint_t bytecode_offset;
    mp_uint_t bytecode_size;
59
    byte *code_base; // stores both byte code and code info
60
    // Accessed as mp_uint_t, so must be aligned as such
61
    byte dummy_data[DUMMY_DATA_SIZE];
Damien's avatar
Damien committed
62
63
};

64
STATIC void emit_bc_rot_two(emit_t *emit);
65
STATIC void emit_bc_rot_three(emit_t *emit);
66

67
emit_t *emit_bc_new(mp_uint_t max_num_labels) {
68
    emit_t *emit = m_new0(emit_t, 1);
69
    emit->max_num_labels = max_num_labels;
70
    emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
71
72
    return emit;
}
73

74
void emit_bc_free(emit_t *emit) {
75
    m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
76
77
78
    m_del_obj(emit_t, emit);
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
STATIC void emit_write_uint(emit_t* emit, byte*(*allocator)(emit_t*, int), mp_uint_t val) {
    // We store each 7 bits in a separate byte, and that's how many bytes needed
    byte buf[BYTES_FOR_INT];
    byte *p = buf + sizeof(buf);
    // We encode in little-ending order, but store in big-endian, to help decoding
    do {
        *--p = val & 0x7f;
        val >>= 7;
    } while (val != 0);
    byte* c = allocator(emit, buf + sizeof(buf) - p);
    while (p != buf + sizeof(buf) - 1) {
        *c++ = *p++ | 0x80;
    }
    *c = *p;
}

95
// all functions must go through this one to emit code info
96
STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
97
    //printf("emit %d\n", num_bytes_to_write);
98
    if (emit->pass < MP_PASS_EMIT) {
99
100
101
102
103
104
105
106
        emit->code_info_offset += num_bytes_to_write;
        return emit->dummy_data;
    } else {
        assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
        byte *c = emit->code_base + emit->code_info_offset;
        emit->code_info_offset += num_bytes_to_write;
        return c;
    }
Damien's avatar
Damien committed
107
108
}

109
STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
110
    emit->code_info_offset = (emit->code_info_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
111
112
}

113
114
115
116
117
118
STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
    emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
}

STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qst) {
    emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien's avatar
Damien committed
119
120
}

121
#if MICROPY_ENABLE_SOURCE_LINE
122
STATIC void emit_write_code_info_bytes_lines(emit_t* emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) {
123
    assert(bytes_to_skip > 0 || lines_to_skip > 0);
124
    //printf("  %d %d\n", bytes_to_skip, lines_to_skip);
125
    while (bytes_to_skip > 0 || lines_to_skip > 0) {
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        mp_uint_t b, l;
        if (lines_to_skip <= 6) {
            // use 0b0LLBBBBB encoding
            b = MIN(bytes_to_skip, 0x1f);
            l = MIN(lines_to_skip, 0x3);
            *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
        } else {
            // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
            b = MIN(bytes_to_skip, 0xf);
            l = MIN(lines_to_skip, 0x7ff);
            byte *ci = emit_get_cur_to_write_code_info(emit, 2);
            ci[0] = 0x80 | b | ((l >> 4) & 0x70);
            ci[1] = l;
        }
140
141
        bytes_to_skip -= b;
        lines_to_skip -= l;
142
    }
143
}
144
#endif
145
146

// all functions must go through this one to emit byte code
147
STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien's avatar
Damien committed
148
    //printf("emit %d\n", num_bytes_to_write);
149
    if (emit->pass < MP_PASS_EMIT) {
150
        emit->bytecode_offset += num_bytes_to_write;
Damien's avatar
Damien committed
151
152
        return emit->dummy_data;
    } else {
153
154
155
        assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
        byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
        emit->bytecode_offset += num_bytes_to_write;
Damien's avatar
Damien committed
156
157
158
159
        return c;
    }
}

160
STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
161
    emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
162
163
}

164
165
STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
    byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien's avatar
Damien committed
166
167
168
    c[0] = b1;
}

169
170
171
172
STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) {
    emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
}

173
STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien's avatar
Damien committed
174
    assert((b2 & (~0xff)) == 0);
175
    byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien's avatar
Damien committed
176
177
178
179
    c[0] = b1;
    c[1] = b2;
}

180
// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
181
STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
182
    emit_write_bytecode_byte(emit, b1);
183
184

    // We store each 7 bits in a separate byte, and that's how many bytes needed
185
    byte buf[BYTES_FOR_INT];
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    byte *p = buf + sizeof(buf);
    // We encode in little-ending order, but store in big-endian, to help decoding
    do {
        *--p = num & 0x7f;
        num >>= 7;
    } while (num != 0 && num != -1);
    // Make sure that highest bit we stored (mask 0x40) matches sign
    // of the number. If not, store extra byte just to encode sign
    if (num == -1 && (*p & 0x40) == 0) {
        *--p = 0x7f;
    } else if (num == 0 && (*p & 0x40) != 0) {
        *--p = 0;
    }

200
    byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
201
202
203
204
    while (p != buf + sizeof(buf) - 1) {
        *c++ = *p++ | 0x80;
    }
    *c = *p;
Damien's avatar
Damien committed
205
206
}

207
STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) {
208
    emit_write_bytecode_byte(emit, b);
209
    emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien's avatar
Damien committed
210
211
}

212
213
214
215
216
217
218
STATIC void emit_write_bytecode_prealigned_ptr(emit_t* emit, void *ptr) {
    mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
    // Verify thar c is already uint-aligned
    assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
    *c = (mp_uint_t)ptr;
}

219
// aligns the pointer so it is friendly to GC
220
221
222
STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
    emit_write_bytecode_byte(emit, b);
    emit_align_bytecode_to_machine_word(emit);
223
    mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
224
225
    // Verify thar c is already uint-aligned
    assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
226
    *c = (mp_uint_t)ptr;
227
228
}

229
/* currently unused
230
STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, mp_uint_t num1, mp_uint_t num2) {
231
232
233
    emit_write_bytecode_byte(emit, b);
    emit_write_bytecode_byte_uint(emit, num1);
    emit_write_bytecode_byte_uint(emit, num2);
234
235
236
}
*/

237
238
STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
    emit_write_bytecode_byte_uint(emit, b, qst);
Damien's avatar
Damien committed
239
240
}

Damien's avatar
Damien committed
241
// unsigned labels are relative to ip following this instruction, stored as 16 bits
242
243
STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) {
    mp_uint_t bytecode_offset;
244
    if (emit->pass < MP_PASS_EMIT) {
245
        bytecode_offset = 0;
Damien's avatar
Damien committed
246
    } else {
247
        bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien's avatar
Damien committed
248
    }
249
    byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien's avatar
Damien committed
250
    c[0] = b1;
251
252
    c[1] = bytecode_offset;
    c[2] = bytecode_offset >> 8;
Damien's avatar
Damien committed
253
254
255
}

// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
256
STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) {
257
    int bytecode_offset;
258
    if (emit->pass < MP_PASS_EMIT) {
259
        bytecode_offset = 0;
Damien's avatar
Damien committed
260
    } else {
261
        bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien's avatar
Damien committed
262
    }
263
    byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien's avatar
Damien committed
264
    c[0] = b1;
265
266
    c[1] = bytecode_offset;
    c[2] = bytecode_offset >> 8;
Damien's avatar
Damien committed
267
268
}

269
STATIC void emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien George's avatar
Damien George committed
270
271
}

272
STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George's avatar
Damien George committed
273
274
275
276
    emit->pass = pass;
    emit->stack_size = 0;
    emit->last_emit_was_return_value = false;
    emit->scope = scope;
277
278
    emit->last_source_line_offset = 0;
    emit->last_source_line = 1;
279
    if (pass < MP_PASS_EMIT) {
280
        memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George's avatar
Damien George committed
281
    }
282
    emit->bytecode_offset = 0;
283
284
    emit->code_info_offset = 0;

285
286
287
288
    // Write code info size as compressed uint.  If we are not in the final pass
    // then space for this uint is reserved in emit_bc_end_pass.
    if (pass == MP_PASS_EMIT) {
        emit_write_code_info_uint(emit, emit->code_info_size);
289
290
    }

291
    // write the name and source file of this function
292
    emit_write_code_info_qstr(emit, scope->simple_name);
293
    emit_write_code_info_qstr(emit, scope->source_file);
Damien George's avatar
Damien George committed
294

295
296
297
298
299
300
301
302
303
304
    // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
    // we store them as full word-sized objects for efficient access in mp_setup_code_state
    // this is the start of the prelude and is guaranteed to be aligned on a word boundary
    {
        for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
            emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst));
        }
    }

    // bytecode prelude: local state size and exception stack size
305
    {
306
        mp_uint_t n_state = scope->num_locals + scope->stack_size;
307
308
309
310
311
312
        if (n_state == 0) {
            // Need at least 1 entry in the state, in the case an exception is
            // propagated through this function, the exception is returned in
            // the highest slot in the state (fastn[0], see vm.c).
            n_state = 1;
        }
313
314
        emit_write_bytecode_uint(emit, n_state);
        emit_write_bytecode_uint(emit, scope->exc_stack_size);
315
316
317
    }

    // bytecode prelude: initialise closed over variables
Damien George's avatar
Damien George committed
318
319
320
321
322
323
324
325
    int num_cell = 0;
    for (int i = 0; i < scope->id_info_len; i++) {
        id_info_t *id = &scope->id_info[i];
        if (id->kind == ID_INFO_KIND_CELL) {
            num_cell += 1;
        }
    }
    assert(num_cell <= 255);
326
    emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George's avatar
Damien George committed
327
328
329
    for (int i = 0; i < scope->id_info_len; i++) {
        id_info_t *id = &scope->id_info[i];
        if (id->kind == ID_INFO_KIND_CELL) {
330
            emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
Damien George's avatar
Damien George committed
331
332
333
334
        }
    }
}

335
STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George's avatar
Damien George committed
336
337
338
339
340
    // 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);
    }

341
    *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
342

343
    if (emit->pass == MP_PASS_CODE_SIZE) {
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
        // Need to make sure we have enough room in the code-info block to write
        // the size of the code-info block.  Since the size is written as a
        // compressed uint, we don't know its size until we write it!  Thus, we
        // take the biggest possible value it could be and write that here.
        // Then there will be enough room to write the value, and any leftover
        // space will be absorbed in the alignment at the end of the code-info
        // block.
        mp_uint_t max_code_info_size =
            emit->code_info_offset  // current code-info size
            + BYTES_FOR_INT         // maximum space for compressed uint
            + BYTES_PER_WORD - 1;   // maximum space for alignment padding
        emit_write_code_info_uint(emit, max_code_info_size);

        // Align code-info so that following bytecode is aligned on a machine word.
        // We don't need to write anything here, it's just dead space between the
        // code-info block and the bytecode block that follows it.
        emit_align_code_info_to_machine_word(emit);

        // calculate size of total code-info + bytecode, in bytes
363
        emit->code_info_size = emit->code_info_offset;
364
365
        emit->bytecode_size = emit->bytecode_offset;
        emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George's avatar
Damien George committed
366

367
    } else if (emit->pass == MP_PASS_EMIT) {
368
369
        mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
            emit->code_info_size + emit->bytecode_size,
370
            emit->scope->num_pos_args, emit->scope->num_kwonly_args,
371
            emit->scope->scope_flags);
Damien George's avatar
Damien George committed
372
373
374
    }
}

375
STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
376
377
378
    return emit->last_emit_was_return_value;
}

379
STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
380
    emit->stack_size += delta;
Damien's avatar
Damien committed
381
382
}

383
STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
384
    //printf("source: line %d -> %d  offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
385
#if MICROPY_ENABLE_SOURCE_LINE
386
387
388
389
    if (mp_optimise_value >= 3) {
        // If we compile with -O3, don't store line numbers.
        return;
    }
390
    if (source_line > emit->last_source_line) {
391
392
        mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
        mp_uint_t lines_to_skip = source_line - emit->last_source_line;
393
        emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
394
        emit->last_source_line_offset = emit->bytecode_offset;
395
396
        emit->last_source_line = source_line;
    }
397
#endif
398
399
}

400
401
STATIC void emit_bc_load_id(emit_t *emit, qstr qst) {
    emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst);
402
403
}

404
405
STATIC void emit_bc_store_id(emit_t *emit, qstr qst) {
    emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst);
406
407
}

408
409
STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) {
    emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst);
410
411
}

412
413
STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
    assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
414
415
416
    emit->stack_size += stack_size_delta;
    if (emit->stack_size > emit->scope->stack_size) {
        emit->scope->stack_size = emit->stack_size;
Damien's avatar
Damien committed
417
418
419
420
    }
    emit->last_emit_was_return_value = false;
}

421
STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
422
    emit_bc_pre(emit, 0);
423
    assert(l < emit->max_num_labels);
424
    if (emit->pass < MP_PASS_EMIT) {
425
426
        // assign label offset
        assert(emit->label_offsets[l] == -1);
427
        emit->label_offsets[l] = emit->bytecode_offset;
428
429
    } else {
        // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
430
431
        //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
        assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien's avatar
Damien committed
432
433
434
    }
}

435
STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
436
    emit_bc_pre(emit, -1);
437
    emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien's avatar
Damien committed
438
439
}

440
STATIC void emit_bc_import_from(emit_t *emit, qstr qst) {
441
    emit_bc_pre(emit, 1);
442
    emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien's avatar
Damien committed
443
444
}

445
STATIC void emit_bc_import_star(emit_t *emit) {
446
    emit_bc_pre(emit, -1);
447
    emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien's avatar
Damien committed
448
449
}

450
STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
451
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
452
    switch (tok) {
453
454
455
456
        case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
        case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
        case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
        case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien's avatar
Damien committed
457
458
459
460
        default: assert(0);
    }
}

461
STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
462
    emit_bc_pre(emit, 1);
463
464
465
466
467
    if (-16 <= arg && arg <= 47) {
        emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
    } else {
        emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
    }
Damien's avatar
Damien committed
468
469
}

470
STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
471
    emit_bc_pre(emit, 1);
472
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
Damien's avatar
Damien committed
473
474
}

475
STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
476
    emit_bc_pre(emit, 1);
477
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
Damien's avatar
Damien committed
478
479
}

480
STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
481
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
482
    if (bytes) {
483
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien's avatar
Damien committed
484
    } else {
485
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien's avatar
Damien committed
486
487
488
    }
}

489
490
STATIC void emit_bc_load_null(emit_t *emit) {
    emit_bc_pre(emit, 1);
491
    emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
492
493
};

494
STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien's avatar
Damien committed
495
    assert(local_num >= 0);
496
    emit_bc_pre(emit, 1);
497
498
499
500
    if (local_num <= 15) {
        emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
    } else {
        emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien's avatar
Damien committed
501
502
503
    }
}

504
STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
505
    emit_bc_pre(emit, 1);
506
    emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien's avatar
Damien committed
507
508
}

509
STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
510
    emit_bc_pre(emit, 1);
511
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien's avatar
Damien committed
512
513
}

514
STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
515
    emit_bc_pre(emit, 1);
516
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien's avatar
Damien committed
517
518
}

519
STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
520
    emit_bc_pre(emit, 0);
521
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien's avatar
Damien committed
522
523
}

524
STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
525
    emit_bc_pre(emit, 1);
526
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien's avatar
Damien committed
527
528
}

529
STATIC void emit_bc_load_build_class(emit_t *emit) {
530
    emit_bc_pre(emit, 1);
531
    emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien's avatar
Damien committed
532
533
}

534
535
STATIC void emit_bc_load_subscr(emit_t *emit) {
    emit_bc_pre(emit, -1);
536
    emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
537
538
}

539
STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien's avatar
Damien committed
540
    assert(local_num >= 0);
541
    emit_bc_pre(emit, -1);
542
543
544
545
    if (local_num <= 15) {
        emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
    } else {
        emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien's avatar
Damien committed
546
547
548
    }
}

549
STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
550
    emit_bc_pre(emit, -1);
551
    emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien's avatar
Damien committed
552
553
}

554
STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
555
    emit_bc_pre(emit, -1);
556
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien's avatar
Damien committed
557
558
}

559
STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
560
    emit_bc_pre(emit, -1);
561
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien's avatar
Damien committed
562
563
}

564
STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
565
    emit_bc_pre(emit, -2);
566
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien's avatar
Damien committed
567
568
}

569
STATIC void emit_bc_store_subscr(emit_t *emit) {
570
    emit_bc_pre(emit, -3);
571
    emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien's avatar
Damien committed
572
573
}

574
STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
575
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien's avatar
Damien committed
576
577
}

578
STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
579
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien's avatar
Damien committed
580
581
}

582
STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
583
    emit_bc_pre(emit, 0);
584
    emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien's avatar
Damien committed
585
586
}

587
STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
588
    emit_bc_pre(emit, 0);
589
    emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien's avatar
Damien committed
590
591
}

592
STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
593
594
    emit_bc_load_null(emit);
    emit_bc_rot_two(emit);
595
    emit_bc_store_attr(emit, qst);
Damien's avatar
Damien committed
596
597
}

598
STATIC void emit_bc_delete_subscr(emit_t *emit) {
599
600
601
    emit_bc_load_null(emit);
    emit_bc_rot_three(emit);
    emit_bc_store_subscr(emit);
Damien's avatar
Damien committed
602
603
}

604
STATIC void emit_bc_dup_top(emit_t *emit) {
605
    emit_bc_pre(emit, 1);
606
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien's avatar
Damien committed
607
608
}

609
STATIC void emit_bc_dup_top_two(emit_t *emit) {
610
    emit_bc_pre(emit, 2);
611
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien's avatar
Damien committed
612
613
}

614
STATIC void emit_bc_pop_top(emit_t *emit) {
615
    emit_bc_pre(emit, -1);
616
    emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien's avatar
Damien committed
617
618
}

619
STATIC void emit_bc_rot_two(emit_t *emit) {
620
    emit_bc_pre(emit, 0);
621
    emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien's avatar
Damien committed
622
623
}

624
STATIC void emit_bc_rot_three(emit_t *emit) {
625
    emit_bc_pre(emit, 0);
626
    emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien's avatar
Damien committed
627
628
}

629
STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
630
    emit_bc_pre(emit, 0);
631
    emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien's avatar
Damien committed
632
633
}

634
STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
635
    emit_bc_pre(emit, -1);
636
    emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien's avatar
Damien committed
637
638
}

639
STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
640
    emit_bc_pre(emit, -1);
641
    emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien's avatar
Damien committed
642
643
}

644
STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
645
    emit_bc_pre(emit, -1);
646
    emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien's avatar
Damien committed
647
648
}

649
STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
650
    emit_bc_pre(emit, -1);
651
    emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien's avatar
Damien committed
652
653
}

654
STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
655
    if (except_depth == 0) {
656
        emit_bc_pre(emit, 0);
657
658
659
660
661
662
663
664
        if (label & MP_EMIT_BREAK_FROM_FOR) {
            // need to pop the iterator if we are breaking out of a for loop
            emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
        }
        emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
    } else {
        emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
        emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
665
    }
Damien's avatar
Damien committed
666
667
}

668
STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
669
    emit_bc_pre(emit, 7);
670
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien's avatar
Damien committed
671
672
}

673
STATIC void emit_bc_with_cleanup(emit_t *emit) {
674
    emit_bc_pre(emit, -7);
675
    emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien's avatar
Damien committed
676
677
}

678
STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
679
    emit_bc_pre(emit, 0);
680
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien's avatar
Damien committed
681
682
}

683
STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
684
    emit_bc_pre(emit, 0);
685
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien's avatar
Damien committed
686
687
}

688
STATIC void emit_bc_end_finally(emit_t *emit) {
689
    emit_bc_pre(emit, -1);
690
    emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien's avatar
Damien committed
691
692
}

693
STATIC void emit_bc_get_iter(emit_t *emit) {
694
    emit_bc_pre(emit, 0);
695
    emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien's avatar
Damien committed
696
697
}

698
STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
699
    emit_bc_pre(emit, 1);
700
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien's avatar
Damien committed
701
702
}

703
STATIC void emit_bc_for_iter_end(emit_t *emit) {
704
    emit_bc_pre(emit, -1);
Damien's avatar
Damien committed
705
706
}

707
STATIC void emit_bc_pop_block(emit_t *emit) {
708
    emit_bc_pre(emit, 0);
709
    emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien's avatar
Damien committed
710
711
}

712
STATIC void emit_bc_pop_except(emit_t *emit) {
713
    emit_bc_pre(emit, 0);
714
    emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien's avatar
Damien committed
715
716
}

Damien George's avatar
Damien George committed
717
718
STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
    if (op == MP_UNARY_OP_NOT) {
719
        emit_bc_pre(emit, 0);
720
        emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
721
        emit_bc_pre(emit, 0);
722
        emit_write_bytecode_byte(emit, MP_BC_NOT);
723
    } else {
724
        emit_bc_pre(emit, 0);
725
        emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
726
    }
Damien's avatar
Damien committed
727
728
}

Damien George's avatar
Damien George committed
729
STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
730
    bool invert = false;
Damien George's avatar
Damien George committed
731
    if (op == MP_BINARY_OP_NOT_IN) {
732
        invert = true;
Damien George's avatar
Damien George committed
733
734
        op = MP_BINARY_OP_IN;
    } else if (op == MP_BINARY_OP_IS_NOT) {
735
        invert = true;
Damien George's avatar
Damien George committed
736
        op = MP_BINARY_OP_IS;
737
    }
738
    emit_bc_pre(emit, -1);
739
    emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
740
    if (invert) {
741
        emit_bc_pre(emit, 0);
742
        emit_write_bytecode_byte(emit, MP_BC_NOT);
743
    }
Damien's avatar
Damien committed
744
745
}

746
STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
747
    emit_bc_pre(emit, 1 - n_args);
748
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien's avatar
Damien committed
749
750
}

751
STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
752
    emit_bc_pre(emit, 1 - n_args);
753
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien's avatar
Damien committed
754
755
}

756
STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
757
    emit_bc_pre(emit, -1);
758
    emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien's avatar
Damien committed
759
760
}

761
STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
762
    emit_bc_pre(emit, 1);
763
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien's avatar
Damien committed
764
765
}

766
STATIC void emit_bc_store_map(emit_t *emit) {
767
    emit_bc_pre(emit, -2);
768
    emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien's avatar
Damien committed
769
770
}

771
STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
772
    emit_bc_pre(emit, -2);
773
    emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien's avatar
Damien committed
774
775
}

776
#if MICROPY_PY_BUILTINS_SET
777
STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
778
    emit_bc_pre(emit, 1 - n_args);
779
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien's avatar
Damien committed
780
781
}

782
STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
783
    emit_bc_pre(emit, -1);
784
    emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien's avatar
Damien committed
785
}
786
#endif
Damien's avatar
Damien committed
787

788
#if MICROPY_PY_BUILTINS_SLICE
789
STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
790
    emit_bc_pre(emit, 1 - n_args);
791
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien's avatar
Damien committed
792
}
793
#endif
Damien's avatar
Damien committed
794

795
STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
796
    emit_bc_pre(emit, -1 + n_args);
797
    emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien's avatar
Damien committed
798
799
}

800
STATIC void emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
801
    emit_bc_pre(emit, -1 + n_left + n_right + 1);