blob: 355ed105177ade63b9e935ff3d07db0522dacea6 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/emit.h"
Damien429d7192013-10-04 19:53:11 +010034
Damien Georgee67ed5d2014-01-04 13:55:24 +000035// wrapper around everything in this file
Damien3ef4abb2013-10-12 16:53:13 +010036#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +010037
Damien415eb6f2013-10-05 12:19:06 +010038struct _emit_t {
Damien429d7192013-10-04 19:53:11 +010039 int pass;
Damien George3417bc22014-05-10 10:36:38 +010040 int bytecode_offset;
Damien429d7192013-10-04 19:53:11 +010041 int stack_size;
42 bool last_emit_was_return_value;
43
44 scope_t *scope;
45
Damien George7ff996c2014-09-08 23:05:16 +010046 mp_uint_t max_num_labels;
47 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010048};
49
Damien George7ff996c2014-09-08 23:05:16 +010050emit_t *emit_cpython_new(mp_uint_t max_num_labels) {
Damien6cdd3af2013-10-05 18:08:26 +010051 emit_t *emit = m_new(emit_t, 1);
52 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010053 emit->label_offsets = m_new(mp_uint_t, max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010054 return emit;
55}
56
Damien George2ac4af62014-08-15 16:45:41 +010057STATIC void emit_cpy_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien429d7192013-10-04 19:53:11 +010058}
59
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020060STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien429d7192013-10-04 19:53:11 +010061 emit->pass = pass;
Damien George3417bc22014-05-10 10:36:38 +010062 emit->bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +010063 emit->stack_size = 0;
64 emit->last_emit_was_return_value = false;
65 emit->scope = scope;
Damien George36db6bc2014-05-07 17:24:22 +010066 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +010067 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien429d7192013-10-04 19:53:11 +010068 }
69}
70
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020071STATIC void emit_cpy_end_pass(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010072 // check stack is back to zero size
73 if (emit->stack_size != 0) {
74 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
75 }
Damien429d7192013-10-04 19:53:11 +010076}
77
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020078STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010079 return emit->last_emit_was_return_value;
80}
81
Damien George7ff996c2014-09-08 23:05:16 +010082STATIC void emit_cpy_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +000083 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +010084}
85
Damien George7ff996c2014-09-08 23:05:16 +010086STATIC void emit_cpy_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George08335002014-01-18 23:24:36 +000087}
88
Damien George7ff996c2014-09-08 23:05:16 +010089STATIC void emit_cpy_load_id(emit_t *emit, qstr qst) {
90 emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +010091}
92
Damien George7ff996c2014-09-08 23:05:16 +010093STATIC void emit_cpy_store_id(emit_t *emit, qstr qst) {
94 emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +010095}
96
Damien George7ff996c2014-09-08 23:05:16 +010097STATIC void emit_cpy_delete_id(emit_t *emit, qstr qst) {
98 emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +010099}
100
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200101// TODO: module-polymorphic function (read: name clash if made global)
Damien George3417bc22014-05-10 10:36:38 +0100102static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
Damien429d7192013-10-04 19:53:11 +0100103 emit->stack_size += stack_size_delta;
Damienb05d7072013-10-05 13:37:10 +0100104 if (emit->stack_size > emit->scope->stack_size) {
Damien429d7192013-10-04 19:53:11 +0100105 emit->scope->stack_size = emit->stack_size;
106 }
107 emit->last_emit_was_return_value = false;
Damien George3417bc22014-05-10 10:36:38 +0100108 if (emit->pass == MP_PASS_EMIT && bytecode_size > 0) {
109 if (emit->bytecode_offset >= 1000) {
110 printf("%d ", emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100111 } else {
Damien George3417bc22014-05-10 10:36:38 +0100112 printf("% 4d ", emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100113 }
114 }
Damien George3417bc22014-05-10 10:36:38 +0100115 emit->bytecode_offset += bytecode_size;
Damien429d7192013-10-04 19:53:11 +0100116}
117
Damien George7ff996c2014-09-08 23:05:16 +0100118STATIC void emit_cpy_label_assign(emit_t *emit, mp_uint_t l) {
Damien429d7192013-10-04 19:53:11 +0100119 emit_pre(emit, 0, 0);
Damienb05d7072013-10-05 13:37:10 +0100120 assert(l < emit->max_num_labels);
Damien Georgec3602e12014-05-07 18:57:32 +0100121 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100122 // assign label offset
123 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100124 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100125 } else {
126 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100127 assert(emit->label_offsets[l] == emit->bytecode_offset);
128 //printf("l%d: (at %d)\n", l, emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100129 }
130}
131
Damien George7ff996c2014-09-08 23:05:16 +0100132STATIC void emit_cpy_import_name(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100133 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100134 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100135 printf("IMPORT_NAME %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100136 }
137}
138
Damien George7ff996c2014-09-08 23:05:16 +0100139STATIC void emit_cpy_import_from(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100140 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100141 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100142 printf("IMPORT_FROM %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100143 }
144}
145
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200146STATIC void emit_cpy_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100147 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100148 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100149 printf("IMPORT_STAR\n");
150 }
151}
152
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200153STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100154 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100155 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100156 printf("LOAD_CONST ");
157 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000158 case MP_TOKEN_KW_FALSE: printf("False"); break;
159 case MP_TOKEN_KW_NONE: printf("None"); break;
160 case MP_TOKEN_KW_TRUE: printf("True"); break;
Damien429d7192013-10-04 19:53:11 +0100161 default: printf("?=%d\n", tok); return; assert(0);
162 }
163 printf("\n");
164 }
165}
166
Damien George40f3c022014-07-03 13:25:24 +0100167STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien429d7192013-10-04 19:53:11 +0100168 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100169 if (emit->pass == MP_PASS_EMIT) {
Damien George08d07552014-01-29 18:58:52 +0000170 printf("LOAD_CONST " INT_FMT "\n", arg);
Damien429d7192013-10-04 19:53:11 +0100171 }
172}
173
Damien George7ff996c2014-09-08 23:05:16 +0100174STATIC void print_quoted_str(qstr qst, bool bytes) {
175 const char *str = qstr_str(qst);
Damiena1b26932013-12-12 15:34:40 +0000176 int len = strlen(str);
177 bool has_single_quote = false;
178 bool has_double_quote = false;
179 for (int i = 0; i < len; i++) {
180 if (str[i] == '\'') {
181 has_single_quote = true;
182 } else if (str[i] == '"') {
183 has_double_quote = true;
184 }
185 }
186 if (bytes) {
187 printf("b");
188 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000189 int quote_char = '\'';
Damiena1b26932013-12-12 15:34:40 +0000190 if (has_single_quote && !has_double_quote) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000191 quote_char = '"';
Damiena1b26932013-12-12 15:34:40 +0000192 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000193 printf("%c", quote_char);
194 for (const char *s = str, *top = str + len; s < top; s++) {
195 if (*s == quote_char) {
196 printf("\\%c", quote_char);
197 } else if (*s == '\\') {
Damiena1b26932013-12-12 15:34:40 +0000198 printf("\\\\");
Damien Georgeb829b5c2014-01-25 13:51:19 +0000199 } else if (32 <= *s && *s <= 126) {
200 printf("%c", *s);
201 } else if (*s == '\n') {
202 printf("\\n");
203 // TODO add more escape codes here
Damiena1b26932013-12-12 15:34:40 +0000204 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000205 printf("\\x%02x", (*s) & 0xff);
Damiena1b26932013-12-12 15:34:40 +0000206 }
207 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000208 printf("%c", quote_char);
Damiena1b26932013-12-12 15:34:40 +0000209}
210
Damien George7ff996c2014-09-08 23:05:16 +0100211STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100212 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100213 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100214 printf("LOAD_CONST ");
Damien George7ff996c2014-09-08 23:05:16 +0100215 print_quoted_str(qst, bytes);
Damien429d7192013-10-04 19:53:11 +0100216 printf("\n");
217 }
218}
219
Damien Georgedab13852015-01-13 15:55:54 +0000220STATIC void emit_cpy_load_const_obj(emit_t *emit, void *obj) {
221 emit_pre(emit, 1, 3);
222 if (emit->pass == MP_PASS_EMIT) {
223 printf("LOAD_CONST ");
224 mp_obj_print(obj, PRINT_REPR);
225 printf("\n");
226 }
227}
228
Damien George3558f622014-04-20 17:50:40 +0100229STATIC void emit_cpy_load_null(emit_t *emit) {
230 // unused for cpy
231 assert(0);
232}
233
Damien George0abb5602015-01-16 12:24:49 +0000234STATIC void emit_cpy_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100235 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100236 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100237 printf("LOAD_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien6cdd3af2013-10-05 18:08:26 +0100238 }
239}
240
Damien George7ff996c2014-09-08 23:05:16 +0100241STATIC void emit_cpy_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100242 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100243 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100244 printf("LOAD_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100245 }
246}
247
Damien George7ff996c2014-09-08 23:05:16 +0100248STATIC void emit_cpy_load_name(emit_t *emit, qstr qst) {
Damien9ecbcff2013-12-11 00:41:43 +0000249 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100250 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100251 printf("LOAD_NAME %s\n", qstr_str(qst));
Damien9ecbcff2013-12-11 00:41:43 +0000252 }
253}
254
Damien George7ff996c2014-09-08 23:05:16 +0100255STATIC void emit_cpy_load_global(emit_t *emit, qstr qst) {
Damien9ecbcff2013-12-11 00:41:43 +0000256 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100257 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100258 printf("LOAD_GLOBAL %s\n", qstr_str(qst));
Damien9ecbcff2013-12-11 00:41:43 +0000259 }
260}
261
Damien George7ff996c2014-09-08 23:05:16 +0100262STATIC void emit_cpy_load_attr(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100263 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100264 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100265 printf("LOAD_ATTR %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100266 }
267}
268
Damien George7ff996c2014-09-08 23:05:16 +0100269STATIC void emit_cpy_load_method(emit_t *emit, qstr qst) {
270 emit_cpy_load_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100271}
272
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200273STATIC void emit_cpy_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100274 emit_pre(emit, 1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100275 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100276 printf("LOAD_BUILD_CLASS\n");
277 }
278}
279
Damien George729f7b42014-04-17 22:10:53 +0100280STATIC void emit_cpy_load_subscr(emit_t *emit) {
281 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100282 if (emit->pass == MP_PASS_EMIT) {
Damien George729f7b42014-04-17 22:10:53 +0100283 printf("BINARY_SUBSCR\n");
284 }
285}
286
Damien George7ff996c2014-09-08 23:05:16 +0100287STATIC void emit_cpy_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100288 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100289 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100290 printf("STORE_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien6cdd3af2013-10-05 18:08:26 +0100291 }
292}
293
Damien George7ff996c2014-09-08 23:05:16 +0100294STATIC void emit_cpy_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000295 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100296 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100297 printf("STORE_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien9ecbcff2013-12-11 00:41:43 +0000298 }
299}
300
Damien George7ff996c2014-09-08 23:05:16 +0100301STATIC void emit_cpy_store_name(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100302 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100303 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100304 printf("STORE_NAME %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100305 }
306}
307
Damien George7ff996c2014-09-08 23:05:16 +0100308STATIC void emit_cpy_store_global(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100309 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100310 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100311 printf("STORE_GLOBAL %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100312 }
313}
314
Damien George7ff996c2014-09-08 23:05:16 +0100315STATIC void emit_cpy_store_attr(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100316 emit_pre(emit, -2, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100317 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100318 printf("STORE_ATTR %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100319 }
320}
321
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200322STATIC void emit_cpy_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100323 emit_pre(emit, -3, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100324 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100325 printf("STORE_SUBSCR\n");
326 }
327}
328
Damien George7ff996c2014-09-08 23:05:16 +0100329STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100330 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100331 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100332 printf("DELETE_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien6cdd3af2013-10-05 18:08:26 +0100333 }
334}
335
Damien George7ff996c2014-09-08 23:05:16 +0100336STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000337 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100338 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100339 printf("DELETE_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien9ecbcff2013-12-11 00:41:43 +0000340 }
341}
342
Damien George7ff996c2014-09-08 23:05:16 +0100343STATIC void emit_cpy_delete_name(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100344 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100345 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100346 printf("DELETE_NAME %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100347 }
348}
349
Damien George7ff996c2014-09-08 23:05:16 +0100350STATIC void emit_cpy_delete_global(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100351 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100352 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100353 printf("DELETE_GLOBAL %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100354 }
355}
356
Damien George7ff996c2014-09-08 23:05:16 +0100357STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100358 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100359 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100360 printf("DELETE_ATTR %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100361 }
362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC void emit_cpy_delete_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100365 emit_pre(emit, -2, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100366 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100367 printf("DELETE_SUBSCR\n");
368 }
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC void emit_cpy_dup_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100372 emit_pre(emit, 1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100373 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100374 printf("DUP_TOP\n");
375 }
376}
377
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200378STATIC void emit_cpy_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100379 emit_pre(emit, 2, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100380 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100381 printf("DUP_TOP_TWO\n");
382 }
383}
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC void emit_cpy_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100386 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100387 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100388 printf("POP_TOP\n");
389 }
390}
391
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200392STATIC void emit_cpy_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100393 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100394 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100395 printf("ROT_TWO\n");
396 }
397}
398
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200399STATIC void emit_cpy_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100400 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100401 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100402 printf("ROT_THREE\n");
403 }
404}
405
Damien George7ff996c2014-09-08 23:05:16 +0100406STATIC void emit_cpy_jump(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100407 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100408 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100409 int dest = emit->label_offsets[label];
Damien George3417bc22014-05-10 10:36:38 +0100410 if (dest < emit->bytecode_offset) {
Damien George7ff996c2014-09-08 23:05:16 +0100411 printf("JUMP_ABSOLUTE " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100412 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100413 printf("JUMP_FORWARD " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100414 }
415 }
416}
417
Damien George7ff996c2014-09-08 23:05:16 +0100418STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100419 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100420 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100421 printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100422 }
423}
424
Damien George7ff996c2014-09-08 23:05:16 +0100425STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100426 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100427 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100428 printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100429 }
430}
431
Damien George7ff996c2014-09-08 23:05:16 +0100432STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100433 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100434 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100435 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100436 }
437}
438
Damien George7ff996c2014-09-08 23:05:16 +0100439STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100440 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100441 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100442 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100443 }
444}
445
Damien George7ff996c2014-09-08 23:05:16 +0100446STATIC void emit_cpy_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien429d7192013-10-04 19:53:11 +0100447 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100448 if (emit->pass == MP_PASS_EMIT) {
Damien Georgee24b5632014-02-01 21:56:25 +0000449 printf("BREAK_LOOP\n");
Damien429d7192013-10-04 19:53:11 +0100450 }
451}
452
Damien George7ff996c2014-09-08 23:05:16 +0100453STATIC void emit_cpy_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgee24b5632014-02-01 21:56:25 +0000454 if (except_depth == 0) {
455 emit_cpy_jump(emit, label);
456 } else {
457 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100458 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100459 printf("CONTINUE_LOOP " UINT_FMT "\n", emit->label_offsets[label]);
Damien Georgee24b5632014-02-01 21:56:25 +0000460 }
Damien429d7192013-10-04 19:53:11 +0100461 }
462}
463
Damien George7ff996c2014-09-08 23:05:16 +0100464STATIC void emit_cpy_setup_with(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100465 emit_pre(emit, 7, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100466 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100467 printf("SETUP_WITH " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100468 }
469}
470
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200471STATIC void emit_cpy_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100472 emit_pre(emit, -7, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100473 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100474 printf("WITH_CLEANUP\n");
475 }
476}
477
Damien George7ff996c2014-09-08 23:05:16 +0100478STATIC void emit_cpy_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000479 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100480 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100481 printf("SETUP_EXCEPT " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100482 }
483}
484
Damien George7ff996c2014-09-08 23:05:16 +0100485STATIC void emit_cpy_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000486 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100487 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100488 printf("SETUP_FINALLY " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100489 }
490}
491
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200492STATIC void emit_cpy_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100493 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100494 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100495 printf("END_FINALLY\n");
496 }
497}
498
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200499STATIC void emit_cpy_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100500 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100501 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100502 printf("GET_ITER\n");
503 }
504}
505
Damien George7ff996c2014-09-08 23:05:16 +0100506STATIC void emit_cpy_for_iter(emit_t *emit, mp_uint_t label) {
Damien429d7192013-10-04 19:53:11 +0100507 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100508 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100509 printf("FOR_ITER " UINT_FMT "\n", emit->label_offsets[label]);
Damien429d7192013-10-04 19:53:11 +0100510 }
511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void emit_cpy_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100514 emit_pre(emit, -1, 0);
515}
516
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200517STATIC void emit_cpy_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100518 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100519 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100520 printf("POP_BLOCK\n");
521 }
522}
523
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200524STATIC void emit_cpy_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100525 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100526 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100527 printf("POP_EXCEPT\n");
528 }
529}
530
Damien Georged17926d2014-03-30 13:35:08 +0100531STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100532 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100533 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100534 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100535 case MP_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
536 case MP_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
537 case MP_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
538 case MP_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
Damien429d7192013-10-04 19:53:11 +0100539 default: assert(0);
540 }
541 }
542}
543
Damien Georged17926d2014-03-30 13:35:08 +0100544STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
545 if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien Georgebc1d3692014-01-11 09:47:06 +0000546 // CPython uses a byte code for each binary op
547 emit_pre(emit, -1, 1);
548 } else {
549 // CPython uses a byte code plus an argument for compare ops
550 emit_pre(emit, -1, 3);
551 }
Damien George36db6bc2014-05-07 17:24:22 +0100552 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100553 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100554 case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
555 case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
556 case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break;
557 case MP_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
558 case MP_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
559 case MP_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
560 case MP_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
561 case MP_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
562 case MP_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
563 case MP_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
564 case MP_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
565 case MP_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
566 case MP_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
567 case MP_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
568 case MP_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
569 case MP_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
570 case MP_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
571 case MP_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
572 case MP_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
573 case MP_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
574 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
575 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
576 case MP_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
577 case MP_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
578 case MP_BINARY_OP_LESS: printf("COMPARE_OP <\n"); break;
579 case MP_BINARY_OP_MORE: printf("COMPARE_OP >\n"); break;
580 case MP_BINARY_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
581 case MP_BINARY_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
582 case MP_BINARY_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
583 case MP_BINARY_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
584 case MP_BINARY_OP_IN: printf("COMPARE_OP in\n"); break;
585 case MP_BINARY_OP_IS: printf("COMPARE_OP is\n"); break;
586 case MP_BINARY_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
587 case MP_BINARY_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
588 case MP_BINARY_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
Damien429d7192013-10-04 19:53:11 +0100589 default: assert(0);
590 }
591 }
592}
593
Damien George7ff996c2014-09-08 23:05:16 +0100594STATIC void emit_cpy_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100595 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100596 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100597 printf("BUILD_TUPLE " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100598 }
599}
600
Damien George7ff996c2014-09-08 23:05:16 +0100601STATIC void emit_cpy_build_list(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100602 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100603 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100604 printf("BUILD_LIST " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100605 }
606}
607
Damien George7ff996c2014-09-08 23:05:16 +0100608STATIC void emit_cpy_list_append(emit_t *emit, mp_uint_t list_index) {
Damien429d7192013-10-04 19:53:11 +0100609 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100610 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100611 printf("LIST_APPEND " UINT_FMT "\n", list_index);
Damien429d7192013-10-04 19:53:11 +0100612 }
613}
614
Damien George7ff996c2014-09-08 23:05:16 +0100615STATIC void emit_cpy_build_map(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100616 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100617 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100618 printf("BUILD_MAP " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100619 }
620}
621
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200622STATIC void emit_cpy_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100623 emit_pre(emit, -2, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100624 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100625 printf("STORE_MAP\n");
626 }
627}
628
Damien George7ff996c2014-09-08 23:05:16 +0100629STATIC void emit_cpy_map_add(emit_t *emit, mp_uint_t map_index) {
Damien429d7192013-10-04 19:53:11 +0100630 emit_pre(emit, -2, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100631 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100632 printf("MAP_ADD " UINT_FMT "\n", map_index);
Damien429d7192013-10-04 19:53:11 +0100633 }
634}
635
Damien George7ff996c2014-09-08 23:05:16 +0100636STATIC void emit_cpy_build_set(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100637 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100638 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100639 printf("BUILD_SET " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100640 }
641}
642
Damien George7ff996c2014-09-08 23:05:16 +0100643STATIC void emit_cpy_set_add(emit_t *emit, mp_uint_t set_index) {
Damien429d7192013-10-04 19:53:11 +0100644 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100645 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100646 printf("SET_ADD " UINT_FMT "\n", set_index);
Damien429d7192013-10-04 19:53:11 +0100647 }
648}
649
Damien George7ff996c2014-09-08 23:05:16 +0100650STATIC void emit_cpy_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100651 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100652 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100653 printf("BUILD_SLICE " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100654 }
655}
656
Damien George7ff996c2014-09-08 23:05:16 +0100657STATIC void emit_cpy_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100658 emit_pre(emit, -1 + n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100659 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100660 printf("UNPACK_SEQUENCE " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100661 }
662}
663
Damien George7ff996c2014-09-08 23:05:16 +0100664STATIC void emit_cpy_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien429d7192013-10-04 19:53:11 +0100665 emit_pre(emit, -1 + n_left + n_right + 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100666 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100667 printf("UNPACK_EX " UINT_FMT "\n", n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100668 }
669}
670
Damien George7ff996c2014-09-08 23:05:16 +0100671STATIC void emit_cpy_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
672 mp_int_t s = 0;
Damien George922ddd62014-04-09 12:43:17 +0100673 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +0100674 s += 1;
675 }
Damien George922ddd62014-04-09 12:43:17 +0100676 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100677 s += 1;
678 }
Damien George7ff996c2014-09-08 23:05:16 +0100679 emit_pre(emit, -(mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - s, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100680 if (emit->pass == MP_PASS_EMIT) {
Damien George922ddd62014-04-09 12:43:17 +0100681 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
682 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100683 printf("CALL_FUNCTION_VAR_KW");
684 } else {
685 printf("CALL_FUNCTION_VAR");
686 }
687 } else {
Damien George922ddd62014-04-09 12:43:17 +0100688 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100689 printf("CALL_FUNCTION_KW");
690 } else {
691 printf("CALL_FUNCTION");
692 }
693 }
Damien George7ff996c2014-09-08 23:05:16 +0100694 printf(" " UINT_FMT ", " UINT_FMT "\n", n_positional, n_keyword);
Damien429d7192013-10-04 19:53:11 +0100695 }
696}
697
Damien George7ff996c2014-09-08 23:05:16 +0100698STATIC void emit_cpy_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100699 emit_cpy_call_function(emit, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_cpy_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100703 emit_pre(emit, -1, 1);
704 emit->last_emit_was_return_value = true;
Damien George36db6bc2014-05-07 17:24:22 +0100705 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100706 printf("RETURN_VALUE\n");
707 }
708}
709
Damien George7ff996c2014-09-08 23:05:16 +0100710STATIC void emit_cpy_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien429d7192013-10-04 19:53:11 +0100711 emit_pre(emit, -n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100712 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100713 printf("RAISE_VARARGS " UINT_FMT "\n", n_args);
Damien429d7192013-10-04 19:53:11 +0100714 }
715}
716
Damien George7ff996c2014-09-08 23:05:16 +0100717STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100718 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100719 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100720 printf("LOAD_CONST code %s\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100721 }
722 // load qualified name
723 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100724 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100725 printf("LOAD_CONST '");
726 // code just to work out the qualname (or whatever it is)
727 {
728 int depth = 0;
729 for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
730 depth += 1;
731 }
732 for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
733 scope_t *s = emit->scope;
734 for (int i = 0; i < wanted_depth; i++) {
735 s = s->parent;
736 }
737 if (s->kind == SCOPE_FUNCTION) {
738 printf("%s.<locals>.", qstr_str(s->simple_name));
739 } else if (s->kind == SCOPE_CLASS) {
740 printf("%s.", qstr_str(s->simple_name));
741 }
742 }
743 }
Damien George7ff996c2014-09-08 23:05:16 +0100744 printf("%s'\n", qstr_str(qst));
Damien429d7192013-10-04 19:53:11 +0100745 }
746}
747
Damien George7ff996c2014-09-08 23:05:16 +0100748STATIC void emit_cpy_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien415eb6f2013-10-05 12:19:06 +0100749 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien George30565092014-03-31 11:30:17 +0100750 emit_pre(emit, -1 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100751 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100752 printf("MAKE_FUNCTION " UINT_FMT "\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien429d7192013-10-04 19:53:11 +0100753 }
754}
755
Damien George7ff996c2014-09-08 23:05:16 +0100756STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien George3558f622014-04-20 17:50:40 +0100757 emit_cpy_build_tuple(emit, n_closed_over);
Damien415eb6f2013-10-05 12:19:06 +0100758 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien George30565092014-03-31 11:30:17 +0100759 emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100760 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100761 printf("MAKE_CLOSURE " UINT_FMT "\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien429d7192013-10-04 19:53:11 +0100762 }
763}
764
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200765STATIC void emit_cpy_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100766 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100767 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
768 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100769 printf("YIELD_VALUE\n");
770 }
771}
772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC void emit_cpy_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100774 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100775 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
776 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100777 printf("YIELD_FROM\n");
778 }
779}
780
Damien Georgeb601d952014-06-30 05:17:25 +0100781STATIC void emit_cpy_start_except_handler(emit_t *emit) {
782 emit_cpy_adjust_stack_size(emit, 3); // stack adjust for the 3 exception items
783}
784
785STATIC void emit_cpy_end_except_handler(emit_t *emit) {
786 emit_cpy_adjust_stack_size(emit, -5); // stack adjust
787}
788
Damien George0d3cb672015-01-28 23:43:01 +0000789STATIC void emit_cpy_load_const_verbatim_strn(emit_t *emit, const char *str, mp_uint_t len) {
Damien George5f6a25f2014-04-20 18:02:27 +0100790 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100791 if (emit->pass == MP_PASS_EMIT) {
Damien George0d3cb672015-01-28 23:43:01 +0000792 printf("LOAD_CONST %.*s\n", (int)len, str);
Damien George5f6a25f2014-04-20 18:02:27 +0100793 }
794}
795
Damien George7ff996c2014-09-08 23:05:16 +0100796STATIC void emit_cpy_load_closure(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George5f6a25f2014-04-20 18:02:27 +0100797 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100798 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100799 printf("LOAD_CLOSURE " UINT_FMT " %s\n", local_num, qstr_str(qst));
Damien George5f6a25f2014-04-20 18:02:27 +0100800 }
801}
802
Damien George7ff996c2014-09-08 23:05:16 +0100803STATIC void emit_cpy_setup_loop(emit_t *emit, mp_uint_t label) {
Damien George5f6a25f2014-04-20 18:02:27 +0100804 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100805 if (emit->pass == MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100806 printf("SETUP_LOOP " UINT_FMT "\n", emit->label_offsets[label]);
Damien George5f6a25f2014-04-20 18:02:27 +0100807 }
808}
809
Damien6cdd3af2013-10-05 18:08:26 +0100810const emit_method_table_t emit_cpython_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100811 emit_cpy_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100812 emit_cpy_start_pass,
813 emit_cpy_end_pass,
814 emit_cpy_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000815 emit_cpy_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000816 emit_cpy_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100817
Damien4b03e772013-10-05 14:17:09 +0100818 emit_cpy_load_id,
819 emit_cpy_store_id,
820 emit_cpy_delete_id,
821
Damien415eb6f2013-10-05 12:19:06 +0100822 emit_cpy_label_assign,
823 emit_cpy_import_name,
824 emit_cpy_import_from,
825 emit_cpy_import_star,
826 emit_cpy_load_const_tok,
827 emit_cpy_load_const_small_int,
Damien415eb6f2013-10-05 12:19:06 +0100828 emit_cpy_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +0000829 emit_cpy_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +0100830 emit_cpy_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100831 emit_cpy_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100832 emit_cpy_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000833 emit_cpy_load_name,
834 emit_cpy_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100835 emit_cpy_load_attr,
836 emit_cpy_load_method,
837 emit_cpy_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100838 emit_cpy_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100839 emit_cpy_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000840 emit_cpy_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100841 emit_cpy_store_name,
842 emit_cpy_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100843 emit_cpy_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100844 emit_cpy_store_subscr,
845 emit_cpy_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000846 emit_cpy_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100847 emit_cpy_delete_name,
848 emit_cpy_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100849 emit_cpy_delete_attr,
850 emit_cpy_delete_subscr,
851 emit_cpy_dup_top,
852 emit_cpy_dup_top_two,
853 emit_cpy_pop_top,
854 emit_cpy_rot_two,
855 emit_cpy_rot_three,
856 emit_cpy_jump,
857 emit_cpy_pop_jump_if_true,
858 emit_cpy_pop_jump_if_false,
859 emit_cpy_jump_if_true_or_pop,
860 emit_cpy_jump_if_false_or_pop,
Damien415eb6f2013-10-05 12:19:06 +0100861 emit_cpy_break_loop,
862 emit_cpy_continue_loop,
863 emit_cpy_setup_with,
864 emit_cpy_with_cleanup,
865 emit_cpy_setup_except,
866 emit_cpy_setup_finally,
867 emit_cpy_end_finally,
868 emit_cpy_get_iter,
869 emit_cpy_for_iter,
870 emit_cpy_for_iter_end,
871 emit_cpy_pop_block,
872 emit_cpy_pop_except,
873 emit_cpy_unary_op,
874 emit_cpy_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100875 emit_cpy_build_tuple,
876 emit_cpy_build_list,
877 emit_cpy_list_append,
878 emit_cpy_build_map,
879 emit_cpy_store_map,
880 emit_cpy_map_add,
881 emit_cpy_build_set,
882 emit_cpy_set_add,
883 emit_cpy_build_slice,
884 emit_cpy_unpack_sequence,
885 emit_cpy_unpack_ex,
886 emit_cpy_make_function,
887 emit_cpy_make_closure,
888 emit_cpy_call_function,
889 emit_cpy_call_method,
890 emit_cpy_return_value,
891 emit_cpy_raise_varargs,
892 emit_cpy_yield_value,
893 emit_cpy_yield_from,
Damien George5f6a25f2014-04-20 18:02:27 +0100894
Damien Georgeb601d952014-06-30 05:17:25 +0100895 emit_cpy_start_except_handler,
896 emit_cpy_end_except_handler,
897
Damien George5f6a25f2014-04-20 18:02:27 +0100898 // emitcpy specific functions
Damien George0d3cb672015-01-28 23:43:01 +0000899 emit_cpy_load_const_verbatim_strn,
Damien George5f6a25f2014-04-20 18:02:27 +0100900 emit_cpy_load_closure,
901 emit_cpy_setup_loop,
Damien415eb6f2013-10-05 12:19:06 +0100902};
903
Damien3ef4abb2013-10-12 16:53:13 +0100904#endif // MICROPY_EMIT_CPYTHON