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

37
38
#if !MICROPY_EMIT_CPYTHON

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

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

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

    scope_t *scope;

50
51
    mp_uint_t last_source_line_offset;
    mp_uint_t last_source_line;
52

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

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

65
emit_t *emit_bc_new(void) {
66
    emit_t *emit = m_new0(emit_t, 1);
67
68
69
70
    return emit;
}

void emit_bc_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels) {
71
    emit->max_num_labels = max_num_labels;
72
    emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
73
}
74

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

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
}

96
// all functions must go through this one to emit code info
97
STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
98
    //printf("emit %d\n", num_bytes_to_write);
99
    if (emit->pass < MP_PASS_EMIT) {
100
101
102
103
104
105
106
107
        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
108
109
}

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

114
115
116
117
118
119
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
120
121
}

122
#if MICROPY_ENABLE_SOURCE_LINE
123
STATIC void emit_write_code_info_bytes_lines(emit_t* emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) {
124
    assert(bytes_to_skip > 0 || lines_to_skip > 0);
125
    //printf("  %d %d\n", bytes_to_skip, lines_to_skip);
126
    while (bytes_to_skip > 0 || lines_to_skip > 0) {
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        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;
        }
141
142
        bytes_to_skip -= b;
        lines_to_skip -= l;
143
    }
144
}
145
#endif
146
147

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

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

165
166
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
167
168
169
    c[0] = b1;
}

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

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

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

    // We store each 7 bits in a separate byte, and that's how many bytes needed
186
    byte buf[BYTES_FOR_INT];
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    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;
    }

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

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

213
214
215
216
217
218
219
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;
}

220
// aligns the pointer so it is friendly to GC
221
222
223
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);
224
    mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
225
226
    // Verify thar c is already uint-aligned
    assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
227
    *c = (mp_uint_t)ptr;
228
229
}

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

238
239
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
240
241
}

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

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

270
271
#if MICROPY_EMIT_NATIVE
STATIC void mp_emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
272
273
274
275
    (void)emit;
    (void)op;
    (void)arg1;
    (void)arg2;
Damien George's avatar
Damien George committed
276
}
277
#endif
Damien George's avatar
Damien George committed
278

279
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George's avatar
Damien George committed
280
281
282
283
    emit->pass = pass;
    emit->stack_size = 0;
    emit->last_emit_was_return_value = false;
    emit->scope = scope;
284
285
    emit->last_source_line_offset = 0;
    emit->last_source_line = 1;
286
    if (pass < MP_PASS_EMIT) {
287
        memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George's avatar
Damien George committed
288
    }
289
    emit->bytecode_offset = 0;
290
291
    emit->code_info_offset = 0;

292
293
294
295
    // 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);
296
297
    }

298
    // write the name and source file of this function
299
    emit_write_code_info_qstr(emit, scope->simple_name);
300
    emit_write_code_info_qstr(emit, scope->source_file);
Damien George's avatar
Damien George committed
301

302
303
304
305
306
307
308
309
310
311
    // 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
312
    {
313
        mp_uint_t n_state = scope->num_locals + scope->stack_size;
314
315
316
317
318
319
        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;
        }
320
321
        emit_write_bytecode_uint(emit, n_state);
        emit_write_bytecode_uint(emit, scope->exc_stack_size);
322
323
324
    }

    // bytecode prelude: initialise closed over variables
Damien George's avatar
Damien George committed
325
326
327
    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) {
328
            assert(id->local_num < 255);
329
            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
330
331
        }
    }
332
    emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George's avatar
Damien George committed
333
334
}

335
void mp_emit_bc_end_pass(emit_t *emit) {
336
337
338
339
    if (emit->pass == MP_PASS_SCOPE) {
        return;
    }

Damien George's avatar
Damien George committed
340
341
342
343
344
    // 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);
    }

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

347
    if (emit->pass == MP_PASS_CODE_SIZE) {
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        // 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
367
        emit->code_info_size = emit->code_info_offset;
368
369
        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
370

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

379
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
380
381
382
    return emit->last_emit_was_return_value;
}

383
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
384
    emit->stack_size += delta;
Damien's avatar
Damien committed
385
386
}

387
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
388
    //printf("source: line %d -> %d  offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
389
#if MICROPY_ENABLE_SOURCE_LINE
390
    if (MP_STATE_VM(mp_optimise_value) >= 3) {
391
392
393
        // If we compile with -O3, don't store line numbers.
        return;
    }
394
    if (source_line > emit->last_source_line) {
395
396
        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;
397
        emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
398
        emit->last_source_line_offset = emit->bytecode_offset;
399
400
        emit->last_source_line = source_line;
    }
401
#endif
402
403
}

404
STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
405
406
407
    if (emit->pass == MP_PASS_SCOPE) {
        return;
    }
408
    assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
409
410
411
    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
412
413
414
415
    }
    emit->last_emit_was_return_value = false;
}

416
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
417
    emit_bc_pre(emit, 0);
418
419
420
    if (emit->pass == MP_PASS_SCOPE) {
        return;
    }
421
    assert(l < emit->max_num_labels);
422
    if (emit->pass < MP_PASS_EMIT) {
423
        // assign label offset
424
        assert(emit->label_offsets[l] == (mp_uint_t)-1);
425
        emit->label_offsets[l] = emit->bytecode_offset;
426
427
    } else {
        // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
428
429
        //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
430
431
432
    }
}

433
void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
434
    emit_bc_pre(emit, -1);
435
    emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien's avatar
Damien committed
436
437
}

438
void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
439
    emit_bc_pre(emit, 1);
440
    emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien's avatar
Damien committed
441
442
}

443
void mp_emit_bc_import_star(emit_t *emit) {
444
    emit_bc_pre(emit, -1);
445
    emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien's avatar
Damien committed
446
447
}

448
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
449
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
450
    switch (tok) {
451
452
453
        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;
454
        no_other_choice:
455
        case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
456
        default: assert(0); goto no_other_choice; // to help flow control analysis
Damien's avatar
Damien committed
457
458
459
    }
}

460
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
461
    emit_bc_pre(emit, 1);
462
463
464
465
466
    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
467
468
}

469
void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
470
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
471
    if (bytes) {
472
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien's avatar
Damien committed
473
    } else {
474
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien's avatar
Damien committed
475
476
477
    }
}

478
void mp_emit_bc_load_const_obj(emit_t *emit, void *obj) {
479
480
481
482
    emit_bc_pre(emit, 1);
    emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
}

483
void mp_emit_bc_load_null(emit_t *emit) {
484
    emit_bc_pre(emit, 1);
485
    emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
486
487
};

488
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
489
    (void)qst;
Damien's avatar
Damien committed
490
    assert(local_num >= 0);
491
    emit_bc_pre(emit, 1);
492
493
494
495
    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
496
497
498
    }
}

499
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
500
    (void)qst;
501
    emit_bc_pre(emit, 1);
502
    emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien's avatar
Damien committed
503
504
}

505
void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
506
    (void)qst;
507
    emit_bc_pre(emit, 1);
508
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
509
510
511
    if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
        emit_write_bytecode_byte(emit, 0);
    }
Damien's avatar
Damien committed
512
513
}

514
void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
515
    (void)qst;
516
    emit_bc_pre(emit, 1);
517
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
518
519
520
    if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
        emit_write_bytecode_byte(emit, 0);
    }
Damien's avatar
Damien committed
521
522
}

523
void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
524
    emit_bc_pre(emit, 0);
525
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
526
527
528
    if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
        emit_write_bytecode_byte(emit, 0);
    }
Damien's avatar
Damien committed
529
530
}

531
void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
532
    emit_bc_pre(emit, 1);
533
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien's avatar
Damien committed
534
535
}

536
void mp_emit_bc_load_build_class(emit_t *emit) {
537
    emit_bc_pre(emit, 1);
538
    emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien's avatar
Damien committed
539
540
}

541
void mp_emit_bc_load_subscr(emit_t *emit) {
542
    emit_bc_pre(emit, -1);
543
    emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
544
545
}

546
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
547
    (void)qst;
Damien's avatar
Damien committed
548
    assert(local_num >= 0);
549
    emit_bc_pre(emit, -1);
550
551
552
553
    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
554
555
556
    }
}

557
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
558
    (void)qst;
559
    emit_bc_pre(emit, -1);
560
    emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien's avatar
Damien committed
561
562
}

563
void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
564
    emit_bc_pre(emit, -1);
565
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien's avatar
Damien committed
566
567
}

568
void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
569
    emit_bc_pre(emit, -1);
570
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien's avatar
Damien committed
571
572
}

573
void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
574
    emit_bc_pre(emit, -2);
575
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
576
577
578
    if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
        emit_write_bytecode_byte(emit, 0);
    }
Damien's avatar
Damien committed
579
580
}

581
void mp_emit_bc_store_subscr(emit_t *emit) {
582
    emit_bc_pre(emit, -3);
583
    emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien's avatar
Damien committed
584
585
}

586
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
587
    (void)qst;
588
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien's avatar
Damien committed
589
590
}

591
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
592
    (void)qst;
593
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien's avatar
Damien committed
594
595
}

596
void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
597
    emit_bc_pre(emit, 0);
598
    emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien's avatar
Damien committed
599
600
}

601
void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
602
    emit_bc_pre(emit, 0);
603
    emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien's avatar
Damien committed
604
605
}

606
607
608
609
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
    mp_emit_bc_load_null(emit);
    mp_emit_bc_rot_two(emit);
    mp_emit_bc_store_attr(emit, qst);
Damien's avatar
Damien committed
610
611
}

612
613
614
615
void mp_emit_bc_delete_subscr(emit_t *emit) {
    mp_emit_bc_load_null(emit);
    mp_emit_bc_rot_three(emit);
    mp_emit_bc_store_subscr(emit);
Damien's avatar
Damien committed
616
617
}

618
void mp_emit_bc_dup_top(emit_t *emit) {
619
    emit_bc_pre(emit, 1);
620
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien's avatar
Damien committed
621
622
}

623
void mp_emit_bc_dup_top_two(emit_t *emit) {
624
    emit_bc_pre(emit, 2);
625
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien's avatar
Damien committed
626
627
}

628
void mp_emit_bc_pop_top(emit_t *emit) {
629
    emit_bc_pre(emit, -1);
630
    emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien's avatar
Damien committed
631
632
}

633
void mp_emit_bc_rot_two(emit_t *emit) {
634
    emit_bc_pre(emit, 0);
635
    emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien's avatar
Damien committed
636
637
}

638
void mp_emit_bc_rot_three(emit_t *emit) {
639
    emit_bc_pre(emit, 0);
640
    emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien's avatar
Damien committed
641
642
}

643
void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
644
    emit_bc_pre(emit, 0);
645
    emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien's avatar
Damien committed
646
647
}

648
void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
649
    emit_bc_pre(emit, -1);
650
651
652
653
654
    if (cond) {
        emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
    } else {
        emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
    }
Damien's avatar
Damien committed
655
656
}

657
void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
658
    emit_bc_pre(emit, -1);
659
660
661
662
663
    if (cond) {
        emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
    } else {
        emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
    }
Damien's avatar
Damien committed
664
665
}

666
void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
667
    if (except_depth == 0) {
668
        emit_bc_pre(emit, 0);
669
670
671
672
673
674
675
676
        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);
677
    }
Damien's avatar
Damien committed
678
679
}

680
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
681
    emit_bc_pre(emit, 7);
682
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien's avatar
Damien committed
683
684
}

685
void mp_emit_bc_with_cleanup(emit_t *emit) {
686
    emit_bc_pre(emit, -7);
687
    emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien's avatar
Damien committed
688
689
}

690
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
691
    emit_bc_pre(emit, 0);
692
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien's avatar
Damien committed
693
694
}

695
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
696
    emit_bc_pre(emit, 0);
697
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien's avatar
Damien committed
698
699
}

700
void mp_emit_bc_end_finally(emit_t *emit) {
701
    emit_bc_pre(emit, -1);
702
    emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien's avatar
Damien committed
703
704
}

705
void mp_emit_bc_get_iter(emit_t *emit) {
706
    emit_bc_pre(emit, 0);
707
    emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien's avatar
Damien committed
708
709
}

710
void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
711
    emit_bc_pre(emit, 1);
712
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien's avatar
Damien committed
713
714
}

715
void mp_emit_bc_for_iter_end(emit_t *emit) {
716
    emit_bc_pre(emit, -1);
Damien's avatar
Damien committed
717
718
}

719
void mp_emit_bc_pop_block(emit_t *emit) {
720
    emit_bc_pre(emit, 0);
721
    emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien's avatar
Damien committed
722
723
}

724
void mp_emit_bc_pop_except(emit_t *emit) {
725
    emit_bc_pre(emit, 0);
726
    emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien's avatar
Damien committed
727
728
}

729
void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien George's avatar
Damien George committed
730
    if (op == MP_UNARY_OP_NOT) {
731
        emit_bc_pre(emit, 0);
732
        emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
733
        emit_bc_pre(emit, 0);
734
        emit_write_bytecode_byte(emit, MP_BC_NOT);
735
    } else {
736
        emit_bc_pre(emit, 0);
737
        emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
738
    }
Damien's avatar
Damien committed
739
740
}

741
void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
742
    bool invert = false;
Damien George's avatar
Damien George committed
743
    if (op == MP_BINARY_OP_NOT_IN) {
744
        invert = true;
Damien George's avatar
Damien George committed
745
746
        op = MP_BINARY_OP_IN;
    } else if (op == MP_BINARY_OP_IS_NOT) {
747
        invert = true;
Damien George's avatar
Damien George committed
748
        op = MP_BINARY_OP_IS;
749
    }
750
    emit_bc_pre(emit, -1);
751
    emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
752
    if (invert) {
753
        emit_bc_pre(emit, 0);
754
        emit_write_bytecode_byte(emit, MP_BC_NOT);
755
    }
Damien's avatar
Damien committed
756
757
}

758
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
759
    emit_bc_pre(emit, 1 - n_args);
760
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien's avatar
Damien committed
761
762
}

763
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
764
    emit_bc_pre(emit, 1 - n_args);
765
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien's avatar
Damien committed
766
767
}

768
void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
769
    emit_bc_pre(emit, -1);
770
    emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien's avatar
Damien committed
771
772
}

773
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
774
    emit_bc_pre(emit, 1);
775
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien's avatar
Damien committed
776
777
}

778
void mp_emit_bc_store_map(emit_t *emit) {
779
    emit_bc_pre(emit, -2);
780
    emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien's avatar
Damien committed
781
782
}

783
void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
784
    emit_bc_pre(emit, -2);
785
    emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien's avatar
Damien committed
786
787
}

788
#if MICROPY_PY_BUILTINS_SET
789
void mp_emit_bc_build_set(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_SET, n_args);
Damien's avatar
Damien committed
792
793
}

794
void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
795
    emit_bc_pre(emit, -1);
796
    emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien's avatar
Damien committed
797
}
798
#endif
Damien's avatar
Damien committed
799

800
#if MICROPY_PY_BUILTINS_SLICE
801
void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
802
    emit_bc_pre(emit, 1 - n_args);
803
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien's avatar
Damien committed
804
}
805
#endif
Damien's avatar
Damien committed
806

807
void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
808
    emit_bc_pre(emit, -1 + n_args);