emitbc.c 33 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
#include "emit.h"
43
#include "bc0.h"
Damien's avatar
Damien committed
44

45
46
#if !MICROPY_EMIT_CPYTHON

47
48
49
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)

50
struct _emit_t {
51
52
53
    pass_kind_t pass : 8;
    uint last_emit_was_return_value : 8;

Damien's avatar
Damien committed
54
55
56
57
    int stack_size;

    scope_t *scope;

58
59
60
    uint last_source_line_offset;
    uint last_source_line;

61
    uint max_num_labels;
Damien's avatar
Damien committed
62
63
    uint *label_offsets;

64
65
    uint code_info_offset;
    uint code_info_size;
66
67
    uint bytecode_offset;
    uint bytecode_size;
68
    byte *code_base; // stores both byte code and code info
69
70
    // Accessed as uint, so must be aligned as such
    byte dummy_data[DUMMY_DATA_SIZE];
Damien's avatar
Damien committed
71
72
};

73
STATIC void emit_bc_rot_two(emit_t *emit);
74
STATIC void emit_bc_rot_three(emit_t *emit);
75

76
emit_t *emit_bc_new(uint max_num_labels) {
77
    emit_t *emit = m_new0(emit_t, 1);
78
79
80
81
    emit->max_num_labels = max_num_labels;
    emit->label_offsets = m_new(uint, emit->max_num_labels);
    return emit;
}
82

83
void emit_bc_free(emit_t *emit) {
84
85
86
87
    m_del(uint, emit->label_offsets, emit->max_num_labels);
    m_del_obj(emit_t, emit);
}

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}

104
// all functions must go through this one to emit code info
105
STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
106
    //printf("emit %d\n", num_bytes_to_write);
107
    if (emit->pass < MP_PASS_EMIT) {
108
109
110
111
112
113
114
115
        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
116
117
}

118
STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
119
    emit->code_info_offset = (emit->code_info_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
120
121
}

122
123
124
125
126
127
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
128
129
}

130
#if MICROPY_ENABLE_SOURCE_LINE
131
STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, uint lines_to_skip) {
132
    assert(bytes_to_skip > 0 || lines_to_skip > 0);
133
    //printf("  %d %d\n", bytes_to_skip, lines_to_skip);
134
    while (bytes_to_skip > 0 || lines_to_skip > 0) {
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        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;
        }
149
150
        bytes_to_skip -= b;
        lines_to_skip -= l;
151
    }
152
}
153
#endif
154
155

// all functions must go through this one to emit byte code
156
STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien's avatar
Damien committed
157
    //printf("emit %d\n", num_bytes_to_write);
158
    if (emit->pass < MP_PASS_EMIT) {
159
        emit->bytecode_offset += num_bytes_to_write;
Damien's avatar
Damien committed
160
161
        return emit->dummy_data;
    } else {
162
163
164
        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
165
166
167
168
        return c;
    }
}

169
STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
170
    emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
171
172
}

173
174
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
175
176
177
    c[0] = b1;
}

178
179
180
181
STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) {
    emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
}

182
STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien's avatar
Damien committed
183
    assert((b2 & (~0xff)) == 0);
184
    byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien's avatar
Damien committed
185
186
187
188
    c[0] = b1;
    c[1] = b2;
}

189
// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
190
STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
191
    emit_write_bytecode_byte(emit, b1);
192
193

    // We store each 7 bits in a separate byte, and that's how many bytes needed
194
    byte buf[BYTES_FOR_INT];
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    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;
    }

209
    byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
210
211
212
213
    while (p != buf + sizeof(buf) - 1) {
        *c++ = *p++ | 0x80;
    }
    *c = *p;
Damien's avatar
Damien committed
214
215
}

216
STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) {
217
    emit_write_bytecode_byte(emit, b);
218
    emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien's avatar
Damien committed
219
220
}

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

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

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

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

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

271
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
272
273
}

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

287
288
289
290
    // 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);
291
292
    }

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

297
    // bytecode prelude: local state size and exception stack size; 16 bit uints for now
298
    {
299
        uint n_state = scope->num_locals + scope->stack_size;
300
301
302
303
304
305
        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;
        }
306
307
        emit_write_bytecode_uint(emit, n_state);
        emit_write_bytecode_uint(emit, scope->exc_stack_size);
308
309
310
    }

    // bytecode prelude: initialise closed over variables
Damien George's avatar
Damien George committed
311
312
313
314
315
316
317
318
    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);
319
    emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George's avatar
Damien George committed
320
321
322
    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) {
323
            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
324
325
326
327
        }
    }
}

328
STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George's avatar
Damien George committed
329
330
331
332
333
    // 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);
    }

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

336
    if (emit->pass == MP_PASS_CODE_SIZE) {
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        // 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
356
        emit->code_info_size = emit->code_info_offset;
357
358
        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
359

360
    } else if (emit->pass == MP_PASS_EMIT) {
361
362
        qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
        for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
363
364
            arg_names[i] = emit->scope->id_info[i].qstr;
        }
365
366
        mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
            emit->code_info_size + emit->bytecode_size,
367
368
            emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
            emit->scope->scope_flags);
Damien George's avatar
Damien George committed
369
370
371
    }
}

372
STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien's avatar
Damien committed
373
374
375
    return emit->last_emit_was_return_value;
}

376
377
STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
    emit->stack_size += delta;
Damien's avatar
Damien committed
378
379
}

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

397
STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
398
399
400
    emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
}

401
STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
402
403
404
    emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
}

405
STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
406
407
408
    emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
}

409
STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
410
    assert((int)emit->stack_size + stack_size_delta >= 0);
411
412
413
    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
414
415
416
417
    }
    emit->last_emit_was_return_value = false;
}

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

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

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

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

447
STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
448
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
449
    switch (tok) {
450
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;
        case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien's avatar
Damien committed
454
455
456
457
        default: assert(0);
    }
}

458
STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
459
    emit_bc_pre(emit, 1);
460
    emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien's avatar
Damien committed
461
462
}

463
STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
464
    emit_bc_pre(emit, 1);
465
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien's avatar
Damien committed
466
467
}

468
STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
469
    emit_bc_pre(emit, 1);
470
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien's avatar
Damien committed
471
472
}

473
STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
474
    emit_bc_pre(emit, 1);
Damien's avatar
Damien committed
475
    if (bytes) {
476
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien's avatar
Damien committed
477
    } else {
478
        emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien's avatar
Damien committed
479
480
481
    }
}

482
483
STATIC void emit_bc_load_null(emit_t *emit) {
    emit_bc_pre(emit, 1);
484
    emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
485
486
};

487
STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien's avatar
Damien committed
488
    assert(local_num >= 0);
489
    emit_bc_pre(emit, 1);
490
    switch (local_num) {
491
492
493
494
        case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
        case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
        case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
        default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien's avatar
Damien committed
495
496
497
    }
}

498
STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
499
    emit_bc_pre(emit, 1);
500
    emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien's avatar
Damien committed
501
502
}

503
STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
504
    emit_bc_pre(emit, 1);
505
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien's avatar
Damien committed
506
507
}

508
STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
509
    emit_bc_pre(emit, 1);
510
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien's avatar
Damien committed
511
512
}

513
STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
514
    emit_bc_pre(emit, 0);
515
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien's avatar
Damien committed
516
517
}

518
STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
519
    emit_bc_pre(emit, 1);
520
    emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien's avatar
Damien committed
521
522
}

523
STATIC void emit_bc_load_build_class(emit_t *emit) {
524
    emit_bc_pre(emit, 1);
525
    emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien's avatar
Damien committed
526
527
}

528
529
STATIC void emit_bc_load_subscr(emit_t *emit) {
    emit_bc_pre(emit, -1);
530
    emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
531
532
}

533
STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien's avatar
Damien committed
534
    assert(local_num >= 0);
535
    emit_bc_pre(emit, -1);
Damien's avatar
Damien committed
536
    switch (local_num) {
537
538
539
540
        case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
        case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
        case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
        default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien's avatar
Damien committed
541
542
543
    }
}

544
STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
545
    emit_bc_pre(emit, -1);
546
    emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien's avatar
Damien committed
547
548
}

549
STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
550
    emit_bc_pre(emit, -1);
551
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien's avatar
Damien committed
552
553
}

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

559
STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
560
    emit_bc_pre(emit, -2);
561
    emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien's avatar
Damien committed
562
563
}

564
STATIC void emit_bc_store_subscr(emit_t *emit) {
565
    emit_bc_pre(emit, -3);
566
    emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien's avatar
Damien committed
567
568
}

569
STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
570
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien's avatar
Damien committed
571
572
}

573
STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
574
    emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien's avatar
Damien committed
575
576
}

577
STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
578
    emit_bc_pre(emit, 0);
579
    emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien's avatar
Damien committed
580
581
}

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

587
STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
588
589
590
    emit_bc_load_null(emit);
    emit_bc_rot_two(emit);
    emit_bc_store_attr(emit, qstr);
Damien's avatar
Damien committed
591
592
}

593
STATIC void emit_bc_delete_subscr(emit_t *emit) {
594
595
596
    emit_bc_load_null(emit);
    emit_bc_rot_three(emit);
    emit_bc_store_subscr(emit);
Damien's avatar
Damien committed
597
598
}

599
STATIC void emit_bc_dup_top(emit_t *emit) {
600
    emit_bc_pre(emit, 1);
601
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien's avatar
Damien committed
602
603
}

604
STATIC void emit_bc_dup_top_two(emit_t *emit) {
605
    emit_bc_pre(emit, 2);
606
    emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien's avatar
Damien committed
607
608
}

609
STATIC void emit_bc_pop_top(emit_t *emit) {
610
    emit_bc_pre(emit, -1);
611
    emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien's avatar
Damien committed
612
613
}

614
STATIC void emit_bc_rot_two(emit_t *emit) {
615
    emit_bc_pre(emit, 0);
616
    emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien's avatar
Damien committed
617
618
}

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

624
STATIC void emit_bc_jump(emit_t *emit, uint label) {
625
    emit_bc_pre(emit, 0);
626
    emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien's avatar
Damien committed
627
628
}

629
STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
630
    emit_bc_pre(emit, -1);
631
    emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien's avatar
Damien committed
632
633
}

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

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

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

649
STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
650
    if (except_depth == 0) {
651
        emit_bc_pre(emit, 0);
652
653
654
655
656
657
658
659
        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);
660
    }
Damien's avatar
Damien committed
661
662
}

663
STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
664
    emit_bc_pre(emit, 7);
665
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien's avatar
Damien committed
666
667
}

668
STATIC void emit_bc_with_cleanup(emit_t *emit) {
669
    emit_bc_pre(emit, -7);
670
    emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien's avatar
Damien committed
671
672
}

673
STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
674
    emit_bc_pre(emit, 0);
675
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien's avatar
Damien committed
676
677
}

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

683
STATIC void emit_bc_end_finally(emit_t *emit) {
684
    emit_bc_pre(emit, -1);
685
    emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien's avatar
Damien committed
686
687
}

688
STATIC void emit_bc_get_iter(emit_t *emit) {
689
    emit_bc_pre(emit, 0);
690
    emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien's avatar
Damien committed
691
692
}

693
STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
694
    emit_bc_pre(emit, 1);
695
    emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien's avatar
Damien committed
696
697
}

698
STATIC void emit_bc_for_iter_end(emit_t *emit) {
699
    emit_bc_pre(emit, -1);
Damien's avatar
Damien committed
700
701
}

702
STATIC void emit_bc_pop_block(emit_t *emit) {
703
    emit_bc_pre(emit, 0);
704
    emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien's avatar
Damien committed
705
706
}

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

Damien George's avatar
Damien George committed
712
713
STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
    if (op == MP_UNARY_OP_NOT) {
714
        emit_bc_pre(emit, 0);
715
        emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
716
        emit_bc_pre(emit, 0);
717
        emit_write_bytecode_byte(emit, MP_BC_NOT);
718
    } else {
719
        emit_bc_pre(emit, 0);
720
        emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, op);
721
    }
Damien's avatar
Damien committed
722
723
}

Damien George's avatar
Damien George committed
724
STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
725
    bool invert = false;
Damien George's avatar
Damien George committed
726
    if (op == MP_BINARY_OP_NOT_IN) {
727
        invert = true;
Damien George's avatar
Damien George committed
728
729
        op = MP_BINARY_OP_IN;
    } else if (op == MP_BINARY_OP_IS_NOT) {
730
        invert = true;
Damien George's avatar
Damien George committed
731
        op = MP_BINARY_OP_IS;
732
    }
733
    emit_bc_pre(emit, -1);
734
    emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
735
    if (invert) {
736
        emit_bc_pre(emit, 0);
737
        emit_write_bytecode_byte(emit, MP_BC_NOT);
738
    }
Damien's avatar
Damien committed
739
740
}

741
STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
742
    assert(n_args >= 0);
743
    emit_bc_pre(emit, 1 - n_args);
744
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien's avatar
Damien committed
745
746
}

747
STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
748
    assert(n_args >= 0);
749
    emit_bc_pre(emit, 1 - n_args);
750
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien's avatar
Damien committed
751
752
}

753
STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien's avatar
Damien committed
754
    assert(list_stack_index >= 0);
755
    emit_bc_pre(emit, -1);
756
    emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien's avatar
Damien committed
757
758
}

759
STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
760
    assert(n_args >= 0);
761
    emit_bc_pre(emit, 1);
762
    emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien's avatar
Damien committed
763
764
}

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

770
STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien's avatar
Damien committed
771
    assert(map_stack_index >= 0);
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
STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
777
    assert(n_args >= 0);
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, int set_stack_index) {
Damien's avatar
Damien committed
783
    assert(set_stack_index >= 0);
784
    emit_bc_pre(emit, -1);
785
    emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien's avatar
Damien committed
786
787
}

788
STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
789
    assert(n_args >= 0);
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
}

794
STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien's avatar
Damien committed
795
    assert(n_args >= 0);
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, int n_left, int n_right) {