blob: 71861c918db2f18da878f252496dc0cb270e5b93 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <unistd.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <assert.h>
7
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien429d7192013-10-04 19:53:11 +010013#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
16
Damien Georgee67ed5d2014-01-04 13:55:24 +000017// wrapper around everything in this file
Damien3ef4abb2013-10-12 16:53:13 +010018#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +010019
Damien415eb6f2013-10-05 12:19:06 +010020struct _emit_t {
Damien429d7192013-10-04 19:53:11 +010021 int pass;
Damien429d7192013-10-04 19:53:11 +010022 int byte_code_offset;
23 int stack_size;
24 bool last_emit_was_return_value;
25
26 scope_t *scope;
27
Damienb05d7072013-10-05 13:37:10 +010028 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010029 int *label_offsets;
30};
31
Damien6cdd3af2013-10-05 18:08:26 +010032emit_t *emit_cpython_new(uint max_num_labels) {
33 emit_t *emit = m_new(emit_t, 1);
34 emit->max_num_labels = max_num_labels;
35 emit->label_offsets = m_new(int, max_num_labels);
36 return emit;
37}
38
Damien415eb6f2013-10-05 12:19:06 +010039static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien429d7192013-10-04 19:53:11 +010040}
41
Damien415eb6f2013-10-05 12:19:06 +010042static void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien429d7192013-10-04 19:53:11 +010043 emit->pass = pass;
Damien429d7192013-10-04 19:53:11 +010044 emit->byte_code_offset = 0;
45 emit->stack_size = 0;
46 emit->last_emit_was_return_value = false;
47 emit->scope = scope;
Damienb05d7072013-10-05 13:37:10 +010048 if (pass == PASS_2) {
49 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010050 }
51}
52
Damien415eb6f2013-10-05 12:19:06 +010053static void emit_cpy_end_pass(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010054 // check stack is back to zero size
55 if (emit->stack_size != 0) {
56 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
57 }
Damien429d7192013-10-04 19:53:11 +010058}
59
Damien415eb6f2013-10-05 12:19:06 +010060static bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010061 return emit->last_emit_was_return_value;
62}
63
Damien415eb6f2013-10-05 12:19:06 +010064static int emit_cpy_get_stack_size(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010065 return emit->stack_size;
66}
67
Damien415eb6f2013-10-05 12:19:06 +010068static void emit_cpy_set_stack_size(emit_t *emit, int size) {
Damien429d7192013-10-04 19:53:11 +010069 emit->stack_size = size;
70}
71
Damien George08335002014-01-18 23:24:36 +000072static void emit_cpy_set_source_line(emit_t *emit, int source_line) {
73}
74
Damien4b03e772013-10-05 14:17:09 +010075static void emit_cpy_load_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010076 emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010077}
78
79static void emit_cpy_store_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010080 emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010081}
82
83static void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010084 emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010085}
86
Damien415eb6f2013-10-05 12:19:06 +010087static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
Damien429d7192013-10-04 19:53:11 +010088 emit->stack_size += stack_size_delta;
Damienb05d7072013-10-05 13:37:10 +010089 if (emit->stack_size > emit->scope->stack_size) {
Damien429d7192013-10-04 19:53:11 +010090 emit->scope->stack_size = emit->stack_size;
91 }
92 emit->last_emit_was_return_value = false;
93 if (emit->pass == PASS_3 && byte_code_size > 0) {
94 if (emit->byte_code_offset >= 1000) {
95 printf("%d ", emit->byte_code_offset);
96 } else {
97 printf("% 4d ", emit->byte_code_offset);
98 }
99 }
100 emit->byte_code_offset += byte_code_size;
101}
102
Damien415eb6f2013-10-05 12:19:06 +0100103static void emit_cpy_label_assign(emit_t *emit, int l) {
Damien429d7192013-10-04 19:53:11 +0100104 emit_pre(emit, 0, 0);
Damienb05d7072013-10-05 13:37:10 +0100105 assert(l < emit->max_num_labels);
106 if (emit->pass == PASS_2) {
107 // assign label offset
108 assert(emit->label_offsets[l] == -1);
109 emit->label_offsets[l] = emit->byte_code_offset;
110 } else if (emit->pass == PASS_3) {
111 // ensure label offset has not changed from PASS_2 to PASS_3
112 assert(emit->label_offsets[l] == emit->byte_code_offset);
113 //printf("l%d: (at %d)\n", l, emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100114 }
115}
116
Damien415eb6f2013-10-05 12:19:06 +0100117static void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100118 emit_pre(emit, -1, 3);
119 if (emit->pass == PASS_3) {
120 printf("IMPORT_NAME %s\n", qstr_str(qstr));
121 }
122}
123
Damien415eb6f2013-10-05 12:19:06 +0100124static void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100125 emit_pre(emit, 1, 3);
126 if (emit->pass == PASS_3) {
127 printf("IMPORT_FROM %s\n", qstr_str(qstr));
128 }
129}
130
Damien415eb6f2013-10-05 12:19:06 +0100131static void emit_cpy_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100132 emit_pre(emit, -1, 1);
133 if (emit->pass == PASS_3) {
134 printf("IMPORT_STAR\n");
135 }
136}
137
Damiende690d12013-12-29 18:01:01 +0000138static void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100139 emit_pre(emit, 1, 3);
140 if (emit->pass == PASS_3) {
141 printf("LOAD_CONST ");
142 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000143 case MP_TOKEN_KW_FALSE: printf("False"); break;
144 case MP_TOKEN_KW_NONE: printf("None"); break;
145 case MP_TOKEN_KW_TRUE: printf("True"); break;
Damien429d7192013-10-04 19:53:11 +0100146 default: printf("?=%d\n", tok); return; assert(0);
147 }
148 printf("\n");
149 }
150}
151
Damien415eb6f2013-10-05 12:19:06 +0100152static void emit_cpy_load_const_small_int(emit_t *emit, int arg) {
Damien429d7192013-10-04 19:53:11 +0100153 emit_pre(emit, 1, 3);
154 if (emit->pass == PASS_3) {
155 printf("LOAD_CONST %d\n", arg);
156 }
157}
158
Damien415eb6f2013-10-05 12:19:06 +0100159static void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100160 emit_pre(emit, 1, 3);
161 if (emit->pass == PASS_3) {
162 printf("LOAD_CONST %s\n", qstr_str(qstr));
163 }
164}
165
Damien415eb6f2013-10-05 12:19:06 +0100166static void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100167 emit_pre(emit, 1, 3);
168 if (emit->pass == PASS_3) {
169 printf("LOAD_CONST %s\n", qstr_str(qstr));
170 }
171}
172
Damien415eb6f2013-10-05 12:19:06 +0100173static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100174 emit_pre(emit, 1, 3);
175 if (emit->pass == PASS_3) {
176 printf("LOAD_CONST '%s'\n", qstr_str(qstr));
177 }
178}
179
Damiena1b26932013-12-12 15:34:40 +0000180static void print_quoted_str(qstr qstr, bool bytes) {
181 const char *str = qstr_str(qstr);
182 int len = strlen(str);
183 bool has_single_quote = false;
184 bool has_double_quote = false;
185 for (int i = 0; i < len; i++) {
186 if (str[i] == '\'') {
187 has_single_quote = true;
188 } else if (str[i] == '"') {
189 has_double_quote = true;
190 }
191 }
192 if (bytes) {
193 printf("b");
194 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000195 int quote_char = '\'';
Damiena1b26932013-12-12 15:34:40 +0000196 if (has_single_quote && !has_double_quote) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000197 quote_char = '"';
Damiena1b26932013-12-12 15:34:40 +0000198 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000199 printf("%c", quote_char);
200 for (const char *s = str, *top = str + len; s < top; s++) {
201 if (*s == quote_char) {
202 printf("\\%c", quote_char);
203 } else if (*s == '\\') {
Damiena1b26932013-12-12 15:34:40 +0000204 printf("\\\\");
Damien Georgeb829b5c2014-01-25 13:51:19 +0000205 } else if (32 <= *s && *s <= 126) {
206 printf("%c", *s);
207 } else if (*s == '\n') {
208 printf("\\n");
209 // TODO add more escape codes here
Damiena1b26932013-12-12 15:34:40 +0000210 } else {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000211 printf("\\x%02x", (*s) & 0xff);
Damiena1b26932013-12-12 15:34:40 +0000212 }
213 }
Damien Georgeb829b5c2014-01-25 13:51:19 +0000214 printf("%c", quote_char);
Damiena1b26932013-12-12 15:34:40 +0000215}
216
Damien415eb6f2013-10-05 12:19:06 +0100217static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100218 emit_pre(emit, 1, 3);
219 if (emit->pass == PASS_3) {
220 printf("LOAD_CONST ");
Damiena1b26932013-12-12 15:34:40 +0000221 print_quoted_str(qstr, bytes);
Damien429d7192013-10-04 19:53:11 +0100222 printf("\n");
223 }
224}
225
Damien415eb6f2013-10-05 12:19:06 +0100226static void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000227 emit_pre(emit, 1, 3);
Damien429d7192013-10-04 19:53:11 +0100228 if (emit->pass == PASS_3) {
Damiena1b26932013-12-12 15:34:40 +0000229 printf("LOAD_CONST %s\n", str);
Damien429d7192013-10-04 19:53:11 +0100230 }
231}
232
Damien6cdd3af2013-10-05 18:08:26 +0100233static void emit_cpy_load_fast(emit_t *emit, qstr qstr, int local_num) {
234 emit_pre(emit, 1, 3);
235 if (emit->pass == PASS_3) {
236 printf("LOAD_FAST %d %s\n", local_num, qstr_str(qstr));
237 }
238}
239
Damien27fb45e2013-10-20 15:07:49 +0100240static void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100241 emit_pre(emit, 1, 3);
242 if (emit->pass == PASS_3) {
Damien27fb45e2013-10-20 15:07:49 +0100243 printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien429d7192013-10-04 19:53:11 +0100244 }
245}
246
Damien27fb45e2013-10-20 15:07:49 +0100247static void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100248 emit_pre(emit, 1, 3);
249 if (emit->pass == PASS_3) {
Damien27fb45e2013-10-20 15:07:49 +0100250 printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
Damien429d7192013-10-04 19:53:11 +0100251 }
252}
253
Damien9ecbcff2013-12-11 00:41:43 +0000254static void emit_cpy_load_name(emit_t *emit, qstr qstr) {
255 emit_pre(emit, 1, 3);
256 if (emit->pass == PASS_3) {
257 printf("LOAD_NAME %s\n", qstr_str(qstr));
258 }
259}
260
261static void emit_cpy_load_global(emit_t *emit, qstr qstr) {
262 emit_pre(emit, 1, 3);
263 if (emit->pass == PASS_3) {
264 printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
265 }
266}
267
Damien415eb6f2013-10-05 12:19:06 +0100268static void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100269 emit_pre(emit, 0, 3);
270 if (emit->pass == PASS_3) {
271 printf("LOAD_ATTR %s\n", qstr_str(qstr));
272 }
273}
274
Damien415eb6f2013-10-05 12:19:06 +0100275static void emit_cpy_load_method(emit_t *emit, qstr qstr) {
276 emit_cpy_load_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100277}
278
Damien415eb6f2013-10-05 12:19:06 +0100279static void emit_cpy_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100280 emit_pre(emit, 1, 1);
281 if (emit->pass == PASS_3) {
282 printf("LOAD_BUILD_CLASS\n");
283 }
284}
285
Damien6cdd3af2013-10-05 18:08:26 +0100286static void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
287 emit_pre(emit, -1, 3);
288 if (emit->pass == PASS_3) {
289 printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
290 }
291}
292
Damien9ecbcff2013-12-11 00:41:43 +0000293static void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
294 emit_pre(emit, -1, 3);
295 if (emit->pass == PASS_3) {
296 printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
297 }
298}
299
Damien415eb6f2013-10-05 12:19:06 +0100300static void emit_cpy_store_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100301 emit_pre(emit, -1, 3);
302 if (emit->pass == PASS_3) {
303 printf("STORE_NAME %s\n", qstr_str(qstr));
304 }
305}
306
Damien415eb6f2013-10-05 12:19:06 +0100307static void emit_cpy_store_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100308 emit_pre(emit, -1, 3);
309 if (emit->pass == PASS_3) {
310 printf("STORE_GLOBAL %s\n", qstr_str(qstr));
311 }
312}
313
Damien415eb6f2013-10-05 12:19:06 +0100314static void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100315 emit_pre(emit, -2, 3);
316 if (emit->pass == PASS_3) {
317 printf("STORE_ATTR %s\n", qstr_str(qstr));
318 }
319}
320
Damien415eb6f2013-10-05 12:19:06 +0100321static void emit_cpy_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100322 emit_pre(emit, -3, 1);
323 if (emit->pass == PASS_3) {
324 printf("STORE_SUBSCR\n");
325 }
326}
327
Damiena3977762013-10-09 23:10:10 +0100328static void emit_cpy_store_locals(emit_t *emit) {
329 emit_pre(emit, -1, 1);
330 if (emit->pass == PASS_3) {
331 printf("STORE_LOCALS\n");
332 }
333}
334
Damien6cdd3af2013-10-05 18:08:26 +0100335static void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
336 emit_pre(emit, 0, 3);
337 if (emit->pass == PASS_3) {
338 printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
339 }
340}
341
Damien9ecbcff2013-12-11 00:41:43 +0000342static void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
343 emit_pre(emit, 0, 3);
344 if (emit->pass == PASS_3) {
345 printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
346 }
347}
348
Damien415eb6f2013-10-05 12:19:06 +0100349static void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100350 emit_pre(emit, 0, 3);
351 if (emit->pass == PASS_3) {
352 printf("DELETE_NAME %s\n", qstr_str(qstr));
353 }
354}
355
Damien415eb6f2013-10-05 12:19:06 +0100356static void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100357 emit_pre(emit, 0, 3);
358 if (emit->pass == PASS_3) {
359 printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
360 }
361}
362
Damien415eb6f2013-10-05 12:19:06 +0100363static void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100364 emit_pre(emit, -1, 3);
365 if (emit->pass == PASS_3) {
366 printf("DELETE_ATTR %s\n", qstr_str(qstr));
367 }
368}
369
Damien415eb6f2013-10-05 12:19:06 +0100370static void emit_cpy_delete_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100371 emit_pre(emit, -2, 1);
372 if (emit->pass == PASS_3) {
373 printf("DELETE_SUBSCR\n");
374 }
375}
376
Damien415eb6f2013-10-05 12:19:06 +0100377static void emit_cpy_dup_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100378 emit_pre(emit, 1, 1);
379 if (emit->pass == PASS_3) {
380 printf("DUP_TOP\n");
381 }
382}
383
Damien415eb6f2013-10-05 12:19:06 +0100384static void emit_cpy_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100385 emit_pre(emit, 2, 1);
386 if (emit->pass == PASS_3) {
387 printf("DUP_TOP_TWO\n");
388 }
389}
390
Damien415eb6f2013-10-05 12:19:06 +0100391static void emit_cpy_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100392 emit_pre(emit, -1, 1);
393 if (emit->pass == PASS_3) {
394 printf("POP_TOP\n");
395 }
396}
397
Damien415eb6f2013-10-05 12:19:06 +0100398static void emit_cpy_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100399 emit_pre(emit, 0, 1);
400 if (emit->pass == PASS_3) {
401 printf("ROT_TWO\n");
402 }
403}
404
Damien415eb6f2013-10-05 12:19:06 +0100405static void emit_cpy_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100406 emit_pre(emit, 0, 1);
407 if (emit->pass == PASS_3) {
408 printf("ROT_THREE\n");
409 }
410}
411
Damien415eb6f2013-10-05 12:19:06 +0100412static void emit_cpy_jump(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100413 emit_pre(emit, 0, 3);
414 if (emit->pass == PASS_3) {
415 int dest = emit->label_offsets[label];
416 if (dest < emit->byte_code_offset) {
417 printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
418 } else {
419 printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
420 }
421 }
422}
423
Damien415eb6f2013-10-05 12:19:06 +0100424static void emit_cpy_pop_jump_if_true(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100425 emit_pre(emit, -1, 3);
426 if (emit->pass == PASS_3) {
427 printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
428 }
429}
430
Damien415eb6f2013-10-05 12:19:06 +0100431static void emit_cpy_pop_jump_if_false(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100432 emit_pre(emit, -1, 3);
433 if (emit->pass == PASS_3) {
434 printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
435 }
436}
437
Damien415eb6f2013-10-05 12:19:06 +0100438static void emit_cpy_jump_if_true_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100439 emit_pre(emit, -1, 3);
440 if (emit->pass == PASS_3) {
441 printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
442 }
443}
444
Damien415eb6f2013-10-05 12:19:06 +0100445static void emit_cpy_jump_if_false_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100446 emit_pre(emit, -1, 3);
447 if (emit->pass == PASS_3) {
448 printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
449 }
450}
451
Damien415eb6f2013-10-05 12:19:06 +0100452static void emit_cpy_setup_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100453 emit_pre(emit, 0, 3);
454 if (emit->pass == PASS_3) {
455 printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
456 }
457}
458
Damien415eb6f2013-10-05 12:19:06 +0100459static void emit_cpy_break_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100460 emit_pre(emit, 0, 1);
461 if (emit->pass == PASS_3) {
462 printf("BREAK_LOOP\n"); // CPython doesn't have label
463 //printf("BREAK_LOOP %d\n", emit->label_offsets[label]);
464 }
465}
466
Damien415eb6f2013-10-05 12:19:06 +0100467static void emit_cpy_continue_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100468 emit_pre(emit, 0, 3);
469 if (emit->pass == PASS_3) {
470 printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
471 }
472}
473
Damien415eb6f2013-10-05 12:19:06 +0100474static void emit_cpy_setup_with(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100475 emit_pre(emit, 7, 3);
476 if (emit->pass == PASS_3) {
477 printf("SETUP_WITH %d\n", emit->label_offsets[label]);
478 }
479}
480
Damien415eb6f2013-10-05 12:19:06 +0100481static void emit_cpy_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100482 emit_pre(emit, -7, 1);
483 if (emit->pass == PASS_3) {
484 printf("WITH_CLEANUP\n");
485 }
486}
487
Damien415eb6f2013-10-05 12:19:06 +0100488static void emit_cpy_setup_except(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100489 emit_pre(emit, 6, 3);
490 if (emit->pass == PASS_3) {
491 printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
492 }
493}
494
Damien415eb6f2013-10-05 12:19:06 +0100495static void emit_cpy_setup_finally(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100496 emit_pre(emit, 6, 3);
497 if (emit->pass == PASS_3) {
498 printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
499 }
500}
501
Damien415eb6f2013-10-05 12:19:06 +0100502static void emit_cpy_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100503 emit_pre(emit, -1, 1);
504 if (emit->pass == PASS_3) {
505 printf("END_FINALLY\n");
506 }
507}
508
Damien415eb6f2013-10-05 12:19:06 +0100509static void emit_cpy_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100510 emit_pre(emit, 0, 1);
511 if (emit->pass == PASS_3) {
512 printf("GET_ITER\n");
513 }
514}
515
Damien415eb6f2013-10-05 12:19:06 +0100516static void emit_cpy_for_iter(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100517 emit_pre(emit, 1, 3);
518 if (emit->pass == PASS_3) {
519 printf("FOR_ITER %d\n", emit->label_offsets[label]);
520 }
521}
522
Damien415eb6f2013-10-05 12:19:06 +0100523static void emit_cpy_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100524 emit_pre(emit, -1, 0);
525}
526
Damien415eb6f2013-10-05 12:19:06 +0100527static void emit_cpy_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100528 emit_pre(emit, 0, 1);
529 if (emit->pass == PASS_3) {
530 printf("POP_BLOCK\n");
531 }
532}
533
Damien415eb6f2013-10-05 12:19:06 +0100534static void emit_cpy_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100535 emit_pre(emit, 0, 1);
536 if (emit->pass == PASS_3) {
537 printf("POP_EXCEPT\n");
538 }
539}
540
Damien415eb6f2013-10-05 12:19:06 +0100541static void emit_cpy_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100542 emit_pre(emit, 0, 1);
543 if (emit->pass == PASS_3) {
544 switch (op) {
545 case RT_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
546 case RT_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
547 case RT_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
548 case RT_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
549 default: assert(0);
550 }
551 }
552}
553
Damien415eb6f2013-10-05 12:19:06 +0100554static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien Georgebc1d3692014-01-11 09:47:06 +0000555 if (op <= RT_BINARY_OP_INPLACE_POWER) {
556 // CPython uses a byte code for each binary op
557 emit_pre(emit, -1, 1);
558 } else {
559 // CPython uses a byte code plus an argument for compare ops
560 emit_pre(emit, -1, 3);
561 }
Damien429d7192013-10-04 19:53:11 +0100562 if (emit->pass == PASS_3) {
563 switch (op) {
564 case RT_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
565 case RT_BINARY_OP_OR: printf("BINARY_OR\n"); break;
566 case RT_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
567 case RT_BINARY_OP_AND: printf("BINARY_AND\n"); break;
568 case RT_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
569 case RT_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
570 case RT_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
571 case RT_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
572 case RT_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
573 case RT_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
574 case RT_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
575 case RT_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
576 case RT_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
577 case RT_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
578 case RT_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
579 case RT_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
580 case RT_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
581 case RT_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
582 case RT_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
583 case RT_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
584 case RT_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
585 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
586 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
587 case RT_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
588 case RT_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
Damien429d7192013-10-04 19:53:11 +0100589 case RT_COMPARE_OP_LESS: printf("COMPARE_OP <\n"); break;
590 case RT_COMPARE_OP_MORE: printf("COMPARE_OP >\n"); break;
591 case RT_COMPARE_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
592 case RT_COMPARE_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
593 case RT_COMPARE_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
594 case RT_COMPARE_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
595 case RT_COMPARE_OP_IN: printf("COMPARE_OP in\n"); break;
596 case RT_COMPARE_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
597 case RT_COMPARE_OP_IS: printf("COMPARE_OP is\n"); break;
598 case RT_COMPARE_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
599 case RT_COMPARE_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
600 default: assert(0);
601 }
602 }
603}
604
Damien415eb6f2013-10-05 12:19:06 +0100605static void emit_cpy_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100606 emit_pre(emit, 1 - n_args, 3);
607 if (emit->pass == PASS_3) {
608 printf("BUILD_TUPLE %d\n", n_args);
609 }
610}
611
Damien415eb6f2013-10-05 12:19:06 +0100612static void emit_cpy_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100613 emit_pre(emit, 1 - n_args, 3);
614 if (emit->pass == PASS_3) {
615 printf("BUILD_LIST %d\n", n_args);
616 }
617}
618
Damien415eb6f2013-10-05 12:19:06 +0100619static void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien429d7192013-10-04 19:53:11 +0100620 emit_pre(emit, -1, 3);
621 if (emit->pass == PASS_3) {
622 printf("LIST_APPEND %d\n", list_index);
623 }
624}
625
Damien415eb6f2013-10-05 12:19:06 +0100626static void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100627 emit_pre(emit, 1, 3);
628 if (emit->pass == PASS_3) {
629 printf("BUILD_MAP %d\n", n_args);
630 }
631}
632
Damien415eb6f2013-10-05 12:19:06 +0100633static void emit_cpy_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100634 emit_pre(emit, -2, 1);
635 if (emit->pass == PASS_3) {
636 printf("STORE_MAP\n");
637 }
638}
639
Damien415eb6f2013-10-05 12:19:06 +0100640static void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien429d7192013-10-04 19:53:11 +0100641 emit_pre(emit, -2, 3);
642 if (emit->pass == PASS_3) {
643 printf("MAP_ADD %d\n", map_index);
644 }
645}
646
Damien415eb6f2013-10-05 12:19:06 +0100647static void emit_cpy_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100648 emit_pre(emit, 1 - n_args, 3);
649 if (emit->pass == PASS_3) {
650 printf("BUILD_SET %d\n", n_args);
651 }
652}
653
Damien415eb6f2013-10-05 12:19:06 +0100654static void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien429d7192013-10-04 19:53:11 +0100655 emit_pre(emit, -1, 3);
656 if (emit->pass == PASS_3) {
657 printf("SET_ADD %d\n", set_index);
658 }
659}
660
Damien415eb6f2013-10-05 12:19:06 +0100661static void emit_cpy_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100662 emit_pre(emit, 1 - n_args, 3);
663 if (emit->pass == PASS_3) {
664 printf("BUILD_SLICE %d\n", n_args);
665 }
666}
667
Damien415eb6f2013-10-05 12:19:06 +0100668static void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100669 emit_pre(emit, -1 + n_args, 3);
670 if (emit->pass == PASS_3) {
671 printf("UNPACK_SEQUENCE %d\n", n_args);
672 }
673}
674
Damien415eb6f2013-10-05 12:19:06 +0100675static void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100676 emit_pre(emit, -1 + n_left + n_right + 1, 3);
677 if (emit->pass == PASS_3) {
678 printf("UNPACK_EX %d\n", n_left | (n_right << 8));
679 }
680}
681
Damien415eb6f2013-10-05 12:19:06 +0100682static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
Damien429d7192013-10-04 19:53:11 +0100683 int s = 0;
684 if (have_star_arg) {
685 s += 1;
686 }
687 if (have_dbl_star_arg) {
688 s += 1;
689 }
690 emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
691 if (emit->pass == PASS_3) {
692 if (have_star_arg) {
693 if (have_dbl_star_arg) {
694 printf("CALL_FUNCTION_VAR_KW");
695 } else {
696 printf("CALL_FUNCTION_VAR");
697 }
698 } else {
699 if (have_dbl_star_arg) {
700 printf("CALL_FUNCTION_KW");
701 } else {
702 printf("CALL_FUNCTION");
703 }
704 }
705 printf(" %d, %d\n", n_positional, n_keyword);
706 }
707}
708
Damien415eb6f2013-10-05 12:19:06 +0100709static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
710 emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +0100711}
712
Damien415eb6f2013-10-05 12:19:06 +0100713static void emit_cpy_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100714 emit_pre(emit, -1, 1);
715 emit->last_emit_was_return_value = true;
716 if (emit->pass == PASS_3) {
717 printf("RETURN_VALUE\n");
718 }
719}
720
Damien415eb6f2013-10-05 12:19:06 +0100721static void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100722 emit_pre(emit, -n_args, 3);
723 if (emit->pass == PASS_3) {
724 printf("RAISE_VARARGS %d\n", n_args);
725 }
726}
727
Damien415eb6f2013-10-05 12:19:06 +0100728static void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100729 emit_pre(emit, 1, 3);
730 if (emit->pass == PASS_3) {
731 printf("LOAD_CONST code %s\n", qstr_str(qstr));
732 }
733 // load qualified name
734 emit_pre(emit, 1, 3);
735 if (emit->pass == PASS_3) {
736 printf("LOAD_CONST '");
737 // code just to work out the qualname (or whatever it is)
738 {
739 int depth = 0;
740 for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
741 depth += 1;
742 }
743 for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
744 scope_t *s = emit->scope;
745 for (int i = 0; i < wanted_depth; i++) {
746 s = s->parent;
747 }
748 if (s->kind == SCOPE_FUNCTION) {
749 printf("%s.<locals>.", qstr_str(s->simple_name));
750 } else if (s->kind == SCOPE_CLASS) {
751 printf("%s.", qstr_str(s->simple_name));
752 }
753 }
754 }
755 printf("%s'\n", qstr_str(qstr));
756 }
757}
758
Damien415eb6f2013-10-05 12:19:06 +0100759static void emit_cpy_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
760 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100761 emit_pre(emit, -1 - n_default_params - 2 * n_dict_params, 3);
762 if (emit->pass == PASS_3) {
763 printf("MAKE_FUNCTION %d\n", (n_dict_params << 8) | n_default_params);
764 }
765}
766
Damien415eb6f2013-10-05 12:19:06 +0100767static void emit_cpy_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
768 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100769 emit_pre(emit, -2 - n_default_params - 2 * n_dict_params, 3);
770 if (emit->pass == PASS_3) {
771 printf("MAKE_CLOSURE %d\n", (n_dict_params << 8) | n_default_params);
772 }
773}
774
Damien415eb6f2013-10-05 12:19:06 +0100775static void emit_cpy_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100776 emit_pre(emit, 0, 1);
777 if (emit->pass == PASS_2) {
778 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
779 }
780 if (emit->pass == PASS_3) {
781 printf("YIELD_VALUE\n");
782 }
783}
784
Damien415eb6f2013-10-05 12:19:06 +0100785static void emit_cpy_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100786 emit_pre(emit, -1, 1);
787 if (emit->pass == PASS_2) {
788 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
789 }
790 if (emit->pass == PASS_3) {
791 printf("YIELD_FROM\n");
792 }
793}
794
Damien6cdd3af2013-10-05 18:08:26 +0100795const emit_method_table_t emit_cpython_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100796 emit_cpy_set_native_types,
797 emit_cpy_start_pass,
798 emit_cpy_end_pass,
799 emit_cpy_last_emit_was_return_value,
800 emit_cpy_get_stack_size,
801 emit_cpy_set_stack_size,
Damien George08335002014-01-18 23:24:36 +0000802 emit_cpy_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100803
Damien4b03e772013-10-05 14:17:09 +0100804 emit_cpy_load_id,
805 emit_cpy_store_id,
806 emit_cpy_delete_id,
807
Damien415eb6f2013-10-05 12:19:06 +0100808 emit_cpy_label_assign,
809 emit_cpy_import_name,
810 emit_cpy_import_from,
811 emit_cpy_import_star,
812 emit_cpy_load_const_tok,
813 emit_cpy_load_const_small_int,
814 emit_cpy_load_const_int,
815 emit_cpy_load_const_dec,
816 emit_cpy_load_const_id,
817 emit_cpy_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100818 emit_cpy_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100819 emit_cpy_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100820 emit_cpy_load_deref,
821 emit_cpy_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000822 emit_cpy_load_name,
823 emit_cpy_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100824 emit_cpy_load_attr,
825 emit_cpy_load_method,
826 emit_cpy_load_build_class,
827 emit_cpy_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000828 emit_cpy_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100829 emit_cpy_store_name,
830 emit_cpy_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100831 emit_cpy_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100832 emit_cpy_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100833 emit_cpy_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100834 emit_cpy_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000835 emit_cpy_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100836 emit_cpy_delete_name,
837 emit_cpy_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100838 emit_cpy_delete_attr,
839 emit_cpy_delete_subscr,
840 emit_cpy_dup_top,
841 emit_cpy_dup_top_two,
842 emit_cpy_pop_top,
843 emit_cpy_rot_two,
844 emit_cpy_rot_three,
845 emit_cpy_jump,
846 emit_cpy_pop_jump_if_true,
847 emit_cpy_pop_jump_if_false,
848 emit_cpy_jump_if_true_or_pop,
849 emit_cpy_jump_if_false_or_pop,
850 emit_cpy_setup_loop,
851 emit_cpy_break_loop,
852 emit_cpy_continue_loop,
853 emit_cpy_setup_with,
854 emit_cpy_with_cleanup,
855 emit_cpy_setup_except,
856 emit_cpy_setup_finally,
857 emit_cpy_end_finally,
858 emit_cpy_get_iter,
859 emit_cpy_for_iter,
860 emit_cpy_for_iter_end,
861 emit_cpy_pop_block,
862 emit_cpy_pop_except,
863 emit_cpy_unary_op,
864 emit_cpy_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100865 emit_cpy_build_tuple,
866 emit_cpy_build_list,
867 emit_cpy_list_append,
868 emit_cpy_build_map,
869 emit_cpy_store_map,
870 emit_cpy_map_add,
871 emit_cpy_build_set,
872 emit_cpy_set_add,
873 emit_cpy_build_slice,
874 emit_cpy_unpack_sequence,
875 emit_cpy_unpack_ex,
876 emit_cpy_make_function,
877 emit_cpy_make_closure,
878 emit_cpy_call_function,
879 emit_cpy_call_method,
880 emit_cpy_return_value,
881 emit_cpy_raise_varargs,
882 emit_cpy_yield_value,
883 emit_cpy_yield_from,
884};
885
Damien3ef4abb2013-10-12 16:53:13 +0100886#endif // MICROPY_EMIT_CPYTHON