blob: 4ff99866a0533931971290191e820dddb626b6c3 [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
Damiend99b0522013-12-21 18:17:45 +000033#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030034#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010036#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010037#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010038#include "obj.h"
39#include "emitglue.h"
Damien429d7192013-10-04 19:53:11 +010040#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000041#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010042#include "emit.h"
43
Damien Georgee67ed5d2014-01-04 13:55:24 +000044// wrapper around everything in this file
Damien3ef4abb2013-10-12 16:53:13 +010045#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +010046
Damien415eb6f2013-10-05 12:19:06 +010047struct _emit_t {
Damien429d7192013-10-04 19:53:11 +010048 int pass;
Damien George3417bc22014-05-10 10:36:38 +010049 int bytecode_offset;
Damien429d7192013-10-04 19:53:11 +010050 int stack_size;
51 bool last_emit_was_return_value;
52
53 scope_t *scope;
54
Damienb05d7072013-10-05 13:37:10 +010055 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010056 int *label_offsets;
57};
58
Damien6cdd3af2013-10-05 18:08:26 +010059emit_t *emit_cpython_new(uint max_num_labels) {
60 emit_t *emit = m_new(emit_t, 1);
61 emit->max_num_labels = max_num_labels;
62 emit->label_offsets = m_new(int, max_num_labels);
63 return emit;
64}
65
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020066STATIC void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien429d7192013-10-04 19:53:11 +010067}
68
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020069STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien429d7192013-10-04 19:53:11 +010070 emit->pass = pass;
Damien George3417bc22014-05-10 10:36:38 +010071 emit->bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +010072 emit->stack_size = 0;
73 emit->last_emit_was_return_value = false;
74 emit->scope = scope;
Damien George36db6bc2014-05-07 17:24:22 +010075 if (pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +010076 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010077 }
78}
79
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020080STATIC void emit_cpy_end_pass(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010081 // check stack is back to zero size
82 if (emit->stack_size != 0) {
83 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
84 }
Damien429d7192013-10-04 19:53:11 +010085}
86
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020087STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010088 return emit->last_emit_was_return_value;
89}
90
Damien Georged66ae182014-04-10 17:28:54 +000091STATIC void emit_cpy_adjust_stack_size(emit_t *emit, int delta) {
92 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +010093}
94
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020095STATIC void emit_cpy_set_source_line(emit_t *emit, int source_line) {
Damien George08335002014-01-18 23:24:36 +000096}
97
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020098STATIC void emit_cpy_load_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010099 emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +0100100}
101
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200102STATIC void emit_cpy_store_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +0100103 emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +0100104}
105
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200106STATIC void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +0100107 emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +0100108}
109
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200110// TODO: module-polymorphic function (read: name clash if made global)
Damien George3417bc22014-05-10 10:36:38 +0100111static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
Damien429d7192013-10-04 19:53:11 +0100112 emit->stack_size += stack_size_delta;
Damienb05d7072013-10-05 13:37:10 +0100113 if (emit->stack_size > emit->scope->stack_size) {
Damien429d7192013-10-04 19:53:11 +0100114 emit->scope->stack_size = emit->stack_size;
115 }
116 emit->last_emit_was_return_value = false;
Damien George3417bc22014-05-10 10:36:38 +0100117 if (emit->pass == MP_PASS_EMIT && bytecode_size > 0) {
118 if (emit->bytecode_offset >= 1000) {
119 printf("%d ", emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100120 } else {
Damien George3417bc22014-05-10 10:36:38 +0100121 printf("% 4d ", emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100122 }
123 }
Damien George3417bc22014-05-10 10:36:38 +0100124 emit->bytecode_offset += bytecode_size;
Damien429d7192013-10-04 19:53:11 +0100125}
126
Damien George6f355fd2014-04-10 14:11:31 +0100127STATIC void emit_cpy_label_assign(emit_t *emit, uint l) {
Damien429d7192013-10-04 19:53:11 +0100128 emit_pre(emit, 0, 0);
Damienb05d7072013-10-05 13:37:10 +0100129 assert(l < emit->max_num_labels);
Damien Georgec3602e12014-05-07 18:57:32 +0100130 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100131 // assign label offset
132 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100133 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100134 } else {
135 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100136 assert(emit->label_offsets[l] == emit->bytecode_offset);
137 //printf("l%d: (at %d)\n", l, emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100138 }
139}
140
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200141STATIC void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100142 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100143 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100144 printf("IMPORT_NAME %s\n", qstr_str(qstr));
145 }
146}
147
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200148STATIC void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100149 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100150 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100151 printf("IMPORT_FROM %s\n", qstr_str(qstr));
152 }
153}
154
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200155STATIC void emit_cpy_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100156 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100157 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100158 printf("IMPORT_STAR\n");
159 }
160}
161
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200162STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100163 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100164 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100165 printf("LOAD_CONST ");
166 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000167 case MP_TOKEN_KW_FALSE: printf("False"); break;
168 case MP_TOKEN_KW_NONE: printf("None"); break;
169 case MP_TOKEN_KW_TRUE: printf("True"); break;
Damien429d7192013-10-04 19:53:11 +0100170 default: printf("?=%d\n", tok); return; assert(0);
171 }
172 printf("\n");
173 }
174}
175
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200176STATIC void emit_cpy_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien429d7192013-10-04 19:53:11 +0100177 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100178 if (emit->pass == MP_PASS_EMIT) {
Damien George08d07552014-01-29 18:58:52 +0000179 printf("LOAD_CONST " INT_FMT "\n", arg);
Damien429d7192013-10-04 19:53:11 +0100180 }
181}
182
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200183STATIC void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100184 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100185 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100186 printf("LOAD_CONST %s\n", qstr_str(qstr));
187 }
188}
189
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200190STATIC void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100191 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100192 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100193 printf("LOAD_CONST %s\n", qstr_str(qstr));
194 }
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void print_quoted_str(qstr qstr, bool bytes) {
Damiena1b26932013-12-12 15:34:40 +0000198 const char *str = qstr_str(qstr);
199 int len = strlen(str);
200 bool has_single_quote = false;
201 bool has_double_quote = false;
202 for (int i = 0; i < len; i++) {
203 if (str[i] == '\'') {
204 has_single_quote = true;
205 } else if (str[i] == '"') {
206 has_double_quote = true;
207 }
208 }
209 if (bytes) {
210 printf("b");
211 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000212 int quote_char = '\'';
Damiena1b26932013-12-12 15:34:40 +0000213 if (has_single_quote && !has_double_quote) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000214 quote_char = '"';
Damiena1b26932013-12-12 15:34:40 +0000215 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000216 printf("%c", quote_char);
217 for (const char *s = str, *top = str + len; s < top; s++) {
218 if (*s == quote_char) {
219 printf("\\%c", quote_char);
220 } else if (*s == '\\') {
Damiena1b26932013-12-12 15:34:40 +0000221 printf("\\\\");
Damien Georgeb829b5c2014-01-25 13:51:19 +0000222 } else if (32 <= *s && *s <= 126) {
223 printf("%c", *s);
224 } else if (*s == '\n') {
225 printf("\\n");
226 // TODO add more escape codes here
Damiena1b26932013-12-12 15:34:40 +0000227 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000228 printf("\\x%02x", (*s) & 0xff);
Damiena1b26932013-12-12 15:34:40 +0000229 }
230 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000231 printf("%c", quote_char);
Damiena1b26932013-12-12 15:34:40 +0000232}
233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200234STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100235 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100236 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100237 printf("LOAD_CONST ");
Damiena1b26932013-12-12 15:34:40 +0000238 print_quoted_str(qstr, bytes);
Damien429d7192013-10-04 19:53:11 +0100239 printf("\n");
240 }
241}
242
Damien George3558f622014-04-20 17:50:40 +0100243STATIC void emit_cpy_load_null(emit_t *emit) {
244 // unused for cpy
245 assert(0);
246}
247
Damien Georgee2835c12014-04-09 20:20:34 +0100248STATIC void emit_cpy_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100249 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100250 if (emit->pass == MP_PASS_EMIT) {
Damien6cdd3af2013-10-05 18:08:26 +0100251 printf("LOAD_FAST %d %s\n", local_num, qstr_str(qstr));
252 }
253}
254
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200255STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100256 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100257 if (emit->pass == MP_PASS_EMIT) {
Damien27fb45e2013-10-20 15:07:49 +0100258 printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien429d7192013-10-04 19:53:11 +0100259 }
260}
261
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200262STATIC void emit_cpy_load_name(emit_t *emit, qstr qstr) {
Damien9ecbcff2013-12-11 00:41:43 +0000263 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100264 if (emit->pass == MP_PASS_EMIT) {
Damien9ecbcff2013-12-11 00:41:43 +0000265 printf("LOAD_NAME %s\n", qstr_str(qstr));
266 }
267}
268
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200269STATIC void emit_cpy_load_global(emit_t *emit, qstr qstr) {
Damien9ecbcff2013-12-11 00:41:43 +0000270 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100271 if (emit->pass == MP_PASS_EMIT) {
Damien9ecbcff2013-12-11 00:41:43 +0000272 printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
273 }
274}
275
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200276STATIC void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100277 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100278 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100279 printf("LOAD_ATTR %s\n", qstr_str(qstr));
280 }
281}
282
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200283STATIC void emit_cpy_load_method(emit_t *emit, qstr qstr) {
Damien415eb6f2013-10-05 12:19:06 +0100284 emit_cpy_load_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100285}
286
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200287STATIC void emit_cpy_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100288 emit_pre(emit, 1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100289 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100290 printf("LOAD_BUILD_CLASS\n");
291 }
292}
293
Damien George729f7b42014-04-17 22:10:53 +0100294STATIC void emit_cpy_load_subscr(emit_t *emit) {
295 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100296 if (emit->pass == MP_PASS_EMIT) {
Damien George729f7b42014-04-17 22:10:53 +0100297 printf("BINARY_SUBSCR\n");
298 }
299}
300
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200301STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100302 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100303 if (emit->pass == MP_PASS_EMIT) {
Damien6cdd3af2013-10-05 18:08:26 +0100304 printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
305 }
306}
307
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200308STATIC void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000309 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100310 if (emit->pass == MP_PASS_EMIT) {
Damien9ecbcff2013-12-11 00:41:43 +0000311 printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
312 }
313}
314
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200315STATIC void emit_cpy_store_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100316 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100317 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100318 printf("STORE_NAME %s\n", qstr_str(qstr));
319 }
320}
321
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200322STATIC void emit_cpy_store_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100323 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100324 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100325 printf("STORE_GLOBAL %s\n", qstr_str(qstr));
326 }
327}
328
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200329STATIC void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100330 emit_pre(emit, -2, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100331 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100332 printf("STORE_ATTR %s\n", qstr_str(qstr));
333 }
334}
335
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200336STATIC void emit_cpy_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100337 emit_pre(emit, -3, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100338 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100339 printf("STORE_SUBSCR\n");
340 }
341}
342
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200343STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien6cdd3af2013-10-05 18:08:26 +0100344 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100345 if (emit->pass == MP_PASS_EMIT) {
Damien6cdd3af2013-10-05 18:08:26 +0100346 printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
347 }
348}
349
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200350STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000351 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100352 if (emit->pass == MP_PASS_EMIT) {
Damien9ecbcff2013-12-11 00:41:43 +0000353 printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
354 }
355}
356
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200357STATIC void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100358 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100359 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100360 printf("DELETE_NAME %s\n", qstr_str(qstr));
361 }
362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100365 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100366 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100367 printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
368 }
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100372 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100373 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100374 printf("DELETE_ATTR %s\n", qstr_str(qstr));
375 }
376}
377
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200378STATIC void emit_cpy_delete_subscr(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("DELETE_SUBSCR\n");
382 }
383}
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC void emit_cpy_dup_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("DUP_TOP\n");
389 }
390}
391
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200392STATIC void emit_cpy_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100393 emit_pre(emit, 2, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100394 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100395 printf("DUP_TOP_TWO\n");
396 }
397}
398
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200399STATIC void emit_cpy_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100400 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100401 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100402 printf("POP_TOP\n");
403 }
404}
405
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200406STATIC void emit_cpy_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100407 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100408 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100409 printf("ROT_TWO\n");
410 }
411}
412
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200413STATIC void emit_cpy_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100414 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100415 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100416 printf("ROT_THREE\n");
417 }
418}
419
Damien George6f355fd2014-04-10 14:11:31 +0100420STATIC void emit_cpy_jump(emit_t *emit, uint label) {
Damien429d7192013-10-04 19:53:11 +0100421 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100422 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100423 int dest = emit->label_offsets[label];
Damien George3417bc22014-05-10 10:36:38 +0100424 if (dest < emit->bytecode_offset) {
Damien429d7192013-10-04 19:53:11 +0100425 printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
426 } else {
427 printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
428 }
429 }
430}
431
Damien George6f355fd2014-04-10 14:11:31 +0100432STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, uint 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) {
Damien429d7192013-10-04 19:53:11 +0100435 printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
436 }
437}
438
Damien George6f355fd2014-04-10 14:11:31 +0100439STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, uint 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) {
Damien429d7192013-10-04 19:53:11 +0100442 printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
443 }
444}
445
Damien George6f355fd2014-04-10 14:11:31 +0100446STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien429d7192013-10-04 19:53:11 +0100447 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100448 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100449 printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
450 }
451}
452
Damien George6f355fd2014-04-10 14:11:31 +0100453STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien429d7192013-10-04 19:53:11 +0100454 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100455 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100456 printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
457 }
458}
459
Damien George6f355fd2014-04-10 14:11:31 +0100460STATIC void emit_cpy_break_loop(emit_t *emit, uint label, int except_depth) {
Damien429d7192013-10-04 19:53:11 +0100461 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100462 if (emit->pass == MP_PASS_EMIT) {
Damien Georgee24b5632014-02-01 21:56:25 +0000463 printf("BREAK_LOOP\n");
Damien429d7192013-10-04 19:53:11 +0100464 }
465}
466
Damien George6f355fd2014-04-10 14:11:31 +0100467STATIC void emit_cpy_continue_loop(emit_t *emit, uint label, int except_depth) {
Damien Georgee24b5632014-02-01 21:56:25 +0000468 if (except_depth == 0) {
469 emit_cpy_jump(emit, label);
470 } else {
471 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100472 if (emit->pass == MP_PASS_EMIT) {
Damien Georgee24b5632014-02-01 21:56:25 +0000473 printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
474 }
Damien429d7192013-10-04 19:53:11 +0100475 }
476}
477
Damien George6f355fd2014-04-10 14:11:31 +0100478STATIC void emit_cpy_setup_with(emit_t *emit, uint label) {
Damien429d7192013-10-04 19:53:11 +0100479 emit_pre(emit, 7, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100480 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100481 printf("SETUP_WITH %d\n", emit->label_offsets[label]);
482 }
483}
484
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200485STATIC void emit_cpy_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100486 emit_pre(emit, -7, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100487 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100488 printf("WITH_CLEANUP\n");
489 }
490}
491
Damien George6f355fd2014-04-10 14:11:31 +0100492STATIC void emit_cpy_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000493 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100494 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100495 printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
496 }
497}
498
Damien George6f355fd2014-04-10 14:11:31 +0100499STATIC void emit_cpy_setup_finally(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000500 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100501 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100502 printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
503 }
504}
505
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200506STATIC void emit_cpy_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100507 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100508 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100509 printf("END_FINALLY\n");
510 }
511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void emit_cpy_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100514 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100515 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100516 printf("GET_ITER\n");
517 }
518}
519
Damien George6f355fd2014-04-10 14:11:31 +0100520STATIC void emit_cpy_for_iter(emit_t *emit, uint label) {
Damien429d7192013-10-04 19:53:11 +0100521 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100522 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100523 printf("FOR_ITER %d\n", emit->label_offsets[label]);
524 }
525}
526
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200527STATIC void emit_cpy_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100528 emit_pre(emit, -1, 0);
529}
530
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200531STATIC void emit_cpy_pop_block(emit_t *emit) {
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 printf("POP_BLOCK\n");
535 }
536}
537
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200538STATIC void emit_cpy_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100539 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100540 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100541 printf("POP_EXCEPT\n");
542 }
543}
544
Damien Georged17926d2014-03-30 13:35:08 +0100545STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100546 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100547 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100548 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100549 case MP_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
550 case MP_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
551 case MP_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
552 case MP_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
Damien429d7192013-10-04 19:53:11 +0100553 default: assert(0);
554 }
555 }
556}
557
Damien Georged17926d2014-03-30 13:35:08 +0100558STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
559 if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien Georgebc1d3692014-01-11 09:47:06 +0000560 // CPython uses a byte code for each binary op
561 emit_pre(emit, -1, 1);
562 } else {
563 // CPython uses a byte code plus an argument for compare ops
564 emit_pre(emit, -1, 3);
565 }
Damien George36db6bc2014-05-07 17:24:22 +0100566 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100567 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100568 case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
569 case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
570 case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break;
571 case MP_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
572 case MP_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
573 case MP_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
574 case MP_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
575 case MP_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
576 case MP_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
577 case MP_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
578 case MP_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
579 case MP_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
580 case MP_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
581 case MP_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
582 case MP_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
583 case MP_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
584 case MP_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
585 case MP_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
586 case MP_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
587 case MP_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
588 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
589 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
590 case MP_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
591 case MP_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
592 case MP_BINARY_OP_LESS: printf("COMPARE_OP <\n"); break;
593 case MP_BINARY_OP_MORE: printf("COMPARE_OP >\n"); break;
594 case MP_BINARY_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
595 case MP_BINARY_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
596 case MP_BINARY_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
597 case MP_BINARY_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
598 case MP_BINARY_OP_IN: printf("COMPARE_OP in\n"); break;
599 case MP_BINARY_OP_IS: printf("COMPARE_OP is\n"); break;
600 case MP_BINARY_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
601 case MP_BINARY_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
602 case MP_BINARY_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
Damien429d7192013-10-04 19:53:11 +0100603 default: assert(0);
604 }
605 }
606}
607
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200608STATIC void emit_cpy_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100609 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100610 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100611 printf("BUILD_TUPLE %d\n", n_args);
612 }
613}
614
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200615STATIC void emit_cpy_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100616 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100617 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100618 printf("BUILD_LIST %d\n", n_args);
619 }
620}
621
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200622STATIC void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien429d7192013-10-04 19:53:11 +0100623 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100624 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100625 printf("LIST_APPEND %d\n", list_index);
626 }
627}
628
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200629STATIC void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100630 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100631 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100632 printf("BUILD_MAP %d\n", n_args);
633 }
634}
635
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200636STATIC void emit_cpy_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100637 emit_pre(emit, -2, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100638 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100639 printf("STORE_MAP\n");
640 }
641}
642
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200643STATIC void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien429d7192013-10-04 19:53:11 +0100644 emit_pre(emit, -2, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100645 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100646 printf("MAP_ADD %d\n", map_index);
647 }
648}
649
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200650STATIC void emit_cpy_build_set(emit_t *emit, int 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) {
Damien429d7192013-10-04 19:53:11 +0100653 printf("BUILD_SET %d\n", n_args);
654 }
655}
656
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200657STATIC void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien429d7192013-10-04 19:53:11 +0100658 emit_pre(emit, -1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100659 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100660 printf("SET_ADD %d\n", set_index);
661 }
662}
663
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200664STATIC void emit_cpy_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100665 emit_pre(emit, 1 - n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100666 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100667 printf("BUILD_SLICE %d\n", n_args);
668 }
669}
670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200671STATIC void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100672 emit_pre(emit, -1 + n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100673 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100674 printf("UNPACK_SEQUENCE %d\n", n_args);
675 }
676}
677
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200678STATIC void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100679 emit_pre(emit, -1 + n_left + n_right + 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100680 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100681 printf("UNPACK_EX %d\n", n_left | (n_right << 8));
682 }
683}
684
Damien George922ddd62014-04-09 12:43:17 +0100685STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
Damien429d7192013-10-04 19:53:11 +0100686 int s = 0;
Damien George922ddd62014-04-09 12:43:17 +0100687 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +0100688 s += 1;
689 }
Damien George922ddd62014-04-09 12:43:17 +0100690 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100691 s += 1;
692 }
693 emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100694 if (emit->pass == MP_PASS_EMIT) {
Damien George922ddd62014-04-09 12:43:17 +0100695 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
696 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100697 printf("CALL_FUNCTION_VAR_KW");
698 } else {
699 printf("CALL_FUNCTION_VAR");
700 }
701 } else {
Damien George922ddd62014-04-09 12:43:17 +0100702 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +0100703 printf("CALL_FUNCTION_KW");
704 } else {
705 printf("CALL_FUNCTION");
706 }
707 }
708 printf(" %d, %d\n", n_positional, n_keyword);
709 }
710}
711
Damien George922ddd62014-04-09 12:43:17 +0100712STATIC void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
713 emit_cpy_call_function(emit, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200716STATIC void emit_cpy_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100717 emit_pre(emit, -1, 1);
718 emit->last_emit_was_return_value = true;
Damien George36db6bc2014-05-07 17:24:22 +0100719 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100720 printf("RETURN_VALUE\n");
721 }
722}
723
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200724STATIC void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100725 emit_pre(emit, -n_args, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100726 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100727 printf("RAISE_VARARGS %d\n", n_args);
728 }
729}
730
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200731STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100732 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100733 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100734 printf("LOAD_CONST code %s\n", qstr_str(qstr));
735 }
736 // load qualified name
737 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100738 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100739 printf("LOAD_CONST '");
740 // code just to work out the qualname (or whatever it is)
741 {
742 int depth = 0;
743 for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
744 depth += 1;
745 }
746 for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
747 scope_t *s = emit->scope;
748 for (int i = 0; i < wanted_depth; i++) {
749 s = s->parent;
750 }
751 if (s->kind == SCOPE_FUNCTION) {
752 printf("%s.<locals>.", qstr_str(s->simple_name));
753 } else if (s->kind == SCOPE_CLASS) {
754 printf("%s.", qstr_str(s->simple_name));
755 }
756 }
757 }
758 printf("%s'\n", qstr_str(qstr));
759 }
760}
761
Damien George30565092014-03-31 11:30:17 +0100762STATIC void emit_cpy_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
Damien415eb6f2013-10-05 12:19:06 +0100763 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien George30565092014-03-31 11:30:17 +0100764 emit_pre(emit, -1 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100765 if (emit->pass == MP_PASS_EMIT) {
Damien George30565092014-03-31 11:30:17 +0100766 printf("MAKE_FUNCTION %d\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien429d7192013-10-04 19:53:11 +0100767 }
768}
769
Damien George3558f622014-04-20 17:50:40 +0100770STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
771 emit_cpy_build_tuple(emit, n_closed_over);
Damien415eb6f2013-10-05 12:19:06 +0100772 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien George30565092014-03-31 11:30:17 +0100773 emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100774 if (emit->pass == MP_PASS_EMIT) {
Damien George30565092014-03-31 11:30:17 +0100775 printf("MAKE_CLOSURE %d\n", (n_kw_defaults << 8) | n_pos_defaults);
Damien429d7192013-10-04 19:53:11 +0100776 }
777}
778
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200779STATIC void emit_cpy_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100780 emit_pre(emit, 0, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100781 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
782 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100783 printf("YIELD_VALUE\n");
784 }
785}
786
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200787STATIC void emit_cpy_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100788 emit_pre(emit, -1, 1);
Damien George36db6bc2014-05-07 17:24:22 +0100789 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
790 if (emit->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100791 printf("YIELD_FROM\n");
792 }
793}
794
Damien Georgeb601d952014-06-30 05:17:25 +0100795STATIC void emit_cpy_start_except_handler(emit_t *emit) {
796 emit_cpy_adjust_stack_size(emit, 3); // stack adjust for the 3 exception items
797}
798
799STATIC void emit_cpy_end_except_handler(emit_t *emit) {
800 emit_cpy_adjust_stack_size(emit, -5); // stack adjust
801}
802
Damien George5f6a25f2014-04-20 18:02:27 +0100803STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
804 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100805 if (emit->pass == MP_PASS_EMIT) {
Damien George5f6a25f2014-04-20 18:02:27 +0100806 printf("LOAD_CONST %s\n", str);
807 }
808}
809
810STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
811 emit_pre(emit, 1, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100812 if (emit->pass == MP_PASS_EMIT) {
Damien George5f6a25f2014-04-20 18:02:27 +0100813 printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
814 }
815}
816
817STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) {
818 emit_pre(emit, 0, 3);
Damien George36db6bc2014-05-07 17:24:22 +0100819 if (emit->pass == MP_PASS_EMIT) {
Damien George5f6a25f2014-04-20 18:02:27 +0100820 printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
821 }
822}
823
Damien6cdd3af2013-10-05 18:08:26 +0100824const emit_method_table_t emit_cpython_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100825 emit_cpy_set_native_types,
826 emit_cpy_start_pass,
827 emit_cpy_end_pass,
828 emit_cpy_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000829 emit_cpy_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000830 emit_cpy_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100831
Damien4b03e772013-10-05 14:17:09 +0100832 emit_cpy_load_id,
833 emit_cpy_store_id,
834 emit_cpy_delete_id,
835
Damien415eb6f2013-10-05 12:19:06 +0100836 emit_cpy_label_assign,
837 emit_cpy_import_name,
838 emit_cpy_import_from,
839 emit_cpy_import_star,
840 emit_cpy_load_const_tok,
841 emit_cpy_load_const_small_int,
842 emit_cpy_load_const_int,
843 emit_cpy_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100844 emit_cpy_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100845 emit_cpy_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100846 emit_cpy_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100847 emit_cpy_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000848 emit_cpy_load_name,
849 emit_cpy_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100850 emit_cpy_load_attr,
851 emit_cpy_load_method,
852 emit_cpy_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100853 emit_cpy_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100854 emit_cpy_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000855 emit_cpy_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100856 emit_cpy_store_name,
857 emit_cpy_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100858 emit_cpy_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100859 emit_cpy_store_subscr,
860 emit_cpy_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000861 emit_cpy_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100862 emit_cpy_delete_name,
863 emit_cpy_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100864 emit_cpy_delete_attr,
865 emit_cpy_delete_subscr,
866 emit_cpy_dup_top,
867 emit_cpy_dup_top_two,
868 emit_cpy_pop_top,
869 emit_cpy_rot_two,
870 emit_cpy_rot_three,
871 emit_cpy_jump,
872 emit_cpy_pop_jump_if_true,
873 emit_cpy_pop_jump_if_false,
874 emit_cpy_jump_if_true_or_pop,
875 emit_cpy_jump_if_false_or_pop,
Damien415eb6f2013-10-05 12:19:06 +0100876 emit_cpy_break_loop,
877 emit_cpy_continue_loop,
878 emit_cpy_setup_with,
879 emit_cpy_with_cleanup,
880 emit_cpy_setup_except,
881 emit_cpy_setup_finally,
882 emit_cpy_end_finally,
883 emit_cpy_get_iter,
884 emit_cpy_for_iter,
885 emit_cpy_for_iter_end,
886 emit_cpy_pop_block,
887 emit_cpy_pop_except,
888 emit_cpy_unary_op,
889 emit_cpy_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100890 emit_cpy_build_tuple,
891 emit_cpy_build_list,
892 emit_cpy_list_append,
893 emit_cpy_build_map,
894 emit_cpy_store_map,
895 emit_cpy_map_add,
896 emit_cpy_build_set,
897 emit_cpy_set_add,
898 emit_cpy_build_slice,
899 emit_cpy_unpack_sequence,
900 emit_cpy_unpack_ex,
901 emit_cpy_make_function,
902 emit_cpy_make_closure,
903 emit_cpy_call_function,
904 emit_cpy_call_method,
905 emit_cpy_return_value,
906 emit_cpy_raise_varargs,
907 emit_cpy_yield_value,
908 emit_cpy_yield_from,
Damien George5f6a25f2014-04-20 18:02:27 +0100909
Damien Georgeb601d952014-06-30 05:17:25 +0100910 emit_cpy_start_except_handler,
911 emit_cpy_end_except_handler,
912
Damien George5f6a25f2014-04-20 18:02:27 +0100913 // emitcpy specific functions
914 emit_cpy_load_const_verbatim_str,
915 emit_cpy_load_closure,
916 emit_cpy_setup_loop,
Damien415eb6f2013-10-05 12:19:06 +0100917};
918
Damien3ef4abb2013-10-12 16:53:13 +0100919#endif // MICROPY_EMIT_CPYTHON