blob: 1e507a581936eada6142d24187f28275745846fd [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
Damien George08d07552014-01-29 18:58:52 +0000152static void emit_cpy_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien429d7192013-10-04 19:53:11 +0100153 emit_pre(emit, 1, 3);
154 if (emit->pass == PASS_3) {
Damien George08d07552014-01-29 18:58:52 +0000155 printf("LOAD_CONST " INT_FMT "\n", arg);
Damien429d7192013-10-04 19:53:11 +0100156 }
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
Damien Georgee24b5632014-02-01 21:56:25 +0000459static void emit_cpy_break_loop(emit_t *emit, int label, int except_depth) {
Damien429d7192013-10-04 19:53:11 +0100460 emit_pre(emit, 0, 1);
461 if (emit->pass == PASS_3) {
Damien Georgee24b5632014-02-01 21:56:25 +0000462 printf("BREAK_LOOP\n");
Damien429d7192013-10-04 19:53:11 +0100463 }
464}
465
Damien Georgee24b5632014-02-01 21:56:25 +0000466static void emit_cpy_continue_loop(emit_t *emit, int label, int except_depth) {
467 if (except_depth == 0) {
468 emit_cpy_jump(emit, label);
469 } else {
470 emit_pre(emit, 0, 3);
471 if (emit->pass == PASS_3) {
472 printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
473 }
Damien429d7192013-10-04 19:53:11 +0100474 }
475}
476
Damien415eb6f2013-10-05 12:19:06 +0100477static void emit_cpy_setup_with(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100478 emit_pre(emit, 7, 3);
479 if (emit->pass == PASS_3) {
480 printf("SETUP_WITH %d\n", emit->label_offsets[label]);
481 }
482}
483
Damien415eb6f2013-10-05 12:19:06 +0100484static void emit_cpy_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100485 emit_pre(emit, -7, 1);
486 if (emit->pass == PASS_3) {
487 printf("WITH_CLEANUP\n");
488 }
489}
490
Damien415eb6f2013-10-05 12:19:06 +0100491static void emit_cpy_setup_except(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100492 emit_pre(emit, 6, 3);
493 if (emit->pass == PASS_3) {
494 printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
495 }
496}
497
Damien415eb6f2013-10-05 12:19:06 +0100498static void emit_cpy_setup_finally(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100499 emit_pre(emit, 6, 3);
500 if (emit->pass == PASS_3) {
501 printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
502 }
503}
504
Damien415eb6f2013-10-05 12:19:06 +0100505static void emit_cpy_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100506 emit_pre(emit, -1, 1);
507 if (emit->pass == PASS_3) {
508 printf("END_FINALLY\n");
509 }
510}
511
Damien415eb6f2013-10-05 12:19:06 +0100512static void emit_cpy_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100513 emit_pre(emit, 0, 1);
514 if (emit->pass == PASS_3) {
515 printf("GET_ITER\n");
516 }
517}
518
Damien415eb6f2013-10-05 12:19:06 +0100519static void emit_cpy_for_iter(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100520 emit_pre(emit, 1, 3);
521 if (emit->pass == PASS_3) {
522 printf("FOR_ITER %d\n", emit->label_offsets[label]);
523 }
524}
525
Damien415eb6f2013-10-05 12:19:06 +0100526static void emit_cpy_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100527 emit_pre(emit, -1, 0);
528}
529
Damien415eb6f2013-10-05 12:19:06 +0100530static void emit_cpy_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100531 emit_pre(emit, 0, 1);
532 if (emit->pass == PASS_3) {
533 printf("POP_BLOCK\n");
534 }
535}
536
Damien415eb6f2013-10-05 12:19:06 +0100537static void emit_cpy_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100538 emit_pre(emit, 0, 1);
539 if (emit->pass == PASS_3) {
540 printf("POP_EXCEPT\n");
541 }
542}
543
Damien415eb6f2013-10-05 12:19:06 +0100544static void emit_cpy_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100545 emit_pre(emit, 0, 1);
546 if (emit->pass == PASS_3) {
547 switch (op) {
548 case RT_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
549 case RT_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
550 case RT_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
551 case RT_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
552 default: assert(0);
553 }
554 }
555}
556
Damien415eb6f2013-10-05 12:19:06 +0100557static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien Georgebc1d3692014-01-11 09:47:06 +0000558 if (op <= RT_BINARY_OP_INPLACE_POWER) {
559 // CPython uses a byte code for each binary op
560 emit_pre(emit, -1, 1);
561 } else {
562 // CPython uses a byte code plus an argument for compare ops
563 emit_pre(emit, -1, 3);
564 }
Damien429d7192013-10-04 19:53:11 +0100565 if (emit->pass == PASS_3) {
566 switch (op) {
567 case RT_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
568 case RT_BINARY_OP_OR: printf("BINARY_OR\n"); break;
569 case RT_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
570 case RT_BINARY_OP_AND: printf("BINARY_AND\n"); break;
571 case RT_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
572 case RT_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
573 case RT_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
574 case RT_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
575 case RT_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
576 case RT_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
577 case RT_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
578 case RT_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
579 case RT_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
580 case RT_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
581 case RT_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
582 case RT_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
583 case RT_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
584 case RT_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
585 case RT_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
586 case RT_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
587 case RT_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
588 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
589 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
590 case RT_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
591 case RT_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
Damien429d7192013-10-04 19:53:11 +0100592 case RT_COMPARE_OP_LESS: printf("COMPARE_OP <\n"); break;
593 case RT_COMPARE_OP_MORE: printf("COMPARE_OP >\n"); break;
594 case RT_COMPARE_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
595 case RT_COMPARE_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
596 case RT_COMPARE_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
597 case RT_COMPARE_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
598 case RT_COMPARE_OP_IN: printf("COMPARE_OP in\n"); break;
599 case RT_COMPARE_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
600 case RT_COMPARE_OP_IS: printf("COMPARE_OP is\n"); break;
601 case RT_COMPARE_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
602 case RT_COMPARE_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
603 default: assert(0);
604 }
605 }
606}
607
Damien415eb6f2013-10-05 12:19:06 +0100608static 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);
610 if (emit->pass == PASS_3) {
611 printf("BUILD_TUPLE %d\n", n_args);
612 }
613}
614
Damien415eb6f2013-10-05 12:19:06 +0100615static 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);
617 if (emit->pass == PASS_3) {
618 printf("BUILD_LIST %d\n", n_args);
619 }
620}
621
Damien415eb6f2013-10-05 12:19:06 +0100622static void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien429d7192013-10-04 19:53:11 +0100623 emit_pre(emit, -1, 3);
624 if (emit->pass == PASS_3) {
625 printf("LIST_APPEND %d\n", list_index);
626 }
627}
628
Damien415eb6f2013-10-05 12:19:06 +0100629static void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100630 emit_pre(emit, 1, 3);
631 if (emit->pass == PASS_3) {
632 printf("BUILD_MAP %d\n", n_args);
633 }
634}
635
Damien415eb6f2013-10-05 12:19:06 +0100636static void emit_cpy_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100637 emit_pre(emit, -2, 1);
638 if (emit->pass == PASS_3) {
639 printf("STORE_MAP\n");
640 }
641}
642
Damien415eb6f2013-10-05 12:19:06 +0100643static void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien429d7192013-10-04 19:53:11 +0100644 emit_pre(emit, -2, 3);
645 if (emit->pass == PASS_3) {
646 printf("MAP_ADD %d\n", map_index);
647 }
648}
649
Damien415eb6f2013-10-05 12:19:06 +0100650static 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);
652 if (emit->pass == PASS_3) {
653 printf("BUILD_SET %d\n", n_args);
654 }
655}
656
Damien415eb6f2013-10-05 12:19:06 +0100657static void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien429d7192013-10-04 19:53:11 +0100658 emit_pre(emit, -1, 3);
659 if (emit->pass == PASS_3) {
660 printf("SET_ADD %d\n", set_index);
661 }
662}
663
Damien415eb6f2013-10-05 12:19:06 +0100664static 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);
666 if (emit->pass == PASS_3) {
667 printf("BUILD_SLICE %d\n", n_args);
668 }
669}
670
Damien415eb6f2013-10-05 12:19:06 +0100671static 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);
673 if (emit->pass == PASS_3) {
674 printf("UNPACK_SEQUENCE %d\n", n_args);
675 }
676}
677
Damien415eb6f2013-10-05 12:19:06 +0100678static 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);
680 if (emit->pass == PASS_3) {
681 printf("UNPACK_EX %d\n", n_left | (n_right << 8));
682 }
683}
684
Damien415eb6f2013-10-05 12:19:06 +0100685static 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 +0100686 int s = 0;
687 if (have_star_arg) {
688 s += 1;
689 }
690 if (have_dbl_star_arg) {
691 s += 1;
692 }
693 emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
694 if (emit->pass == PASS_3) {
695 if (have_star_arg) {
696 if (have_dbl_star_arg) {
697 printf("CALL_FUNCTION_VAR_KW");
698 } else {
699 printf("CALL_FUNCTION_VAR");
700 }
701 } else {
702 if (have_dbl_star_arg) {
703 printf("CALL_FUNCTION_KW");
704 } else {
705 printf("CALL_FUNCTION");
706 }
707 }
708 printf(" %d, %d\n", n_positional, n_keyword);
709 }
710}
711
Damien415eb6f2013-10-05 12:19:06 +0100712static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
713 emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Damien415eb6f2013-10-05 12:19:06 +0100716static 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;
719 if (emit->pass == PASS_3) {
720 printf("RETURN_VALUE\n");
721 }
722}
723
Damien415eb6f2013-10-05 12:19:06 +0100724static void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100725 emit_pre(emit, -n_args, 3);
726 if (emit->pass == PASS_3) {
727 printf("RAISE_VARARGS %d\n", n_args);
728 }
729}
730
Damien415eb6f2013-10-05 12:19:06 +0100731static void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100732 emit_pre(emit, 1, 3);
733 if (emit->pass == PASS_3) {
734 printf("LOAD_CONST code %s\n", qstr_str(qstr));
735 }
736 // load qualified name
737 emit_pre(emit, 1, 3);
738 if (emit->pass == PASS_3) {
739 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
Damien415eb6f2013-10-05 12:19:06 +0100762static void emit_cpy_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
763 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100764 emit_pre(emit, -1 - n_default_params - 2 * n_dict_params, 3);
765 if (emit->pass == PASS_3) {
766 printf("MAKE_FUNCTION %d\n", (n_dict_params << 8) | n_default_params);
767 }
768}
769
Damien415eb6f2013-10-05 12:19:06 +0100770static void emit_cpy_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
771 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100772 emit_pre(emit, -2 - n_default_params - 2 * n_dict_params, 3);
773 if (emit->pass == PASS_3) {
774 printf("MAKE_CLOSURE %d\n", (n_dict_params << 8) | n_default_params);
775 }
776}
777
Damien415eb6f2013-10-05 12:19:06 +0100778static void emit_cpy_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100779 emit_pre(emit, 0, 1);
780 if (emit->pass == PASS_2) {
781 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
782 }
783 if (emit->pass == PASS_3) {
784 printf("YIELD_VALUE\n");
785 }
786}
787
Damien415eb6f2013-10-05 12:19:06 +0100788static void emit_cpy_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100789 emit_pre(emit, -1, 1);
790 if (emit->pass == PASS_2) {
791 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
792 }
793 if (emit->pass == PASS_3) {
794 printf("YIELD_FROM\n");
795 }
796}
797
Damien6cdd3af2013-10-05 18:08:26 +0100798const emit_method_table_t emit_cpython_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100799 emit_cpy_set_native_types,
800 emit_cpy_start_pass,
801 emit_cpy_end_pass,
802 emit_cpy_last_emit_was_return_value,
803 emit_cpy_get_stack_size,
804 emit_cpy_set_stack_size,
Damien George08335002014-01-18 23:24:36 +0000805 emit_cpy_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100806
Damien4b03e772013-10-05 14:17:09 +0100807 emit_cpy_load_id,
808 emit_cpy_store_id,
809 emit_cpy_delete_id,
810
Damien415eb6f2013-10-05 12:19:06 +0100811 emit_cpy_label_assign,
812 emit_cpy_import_name,
813 emit_cpy_import_from,
814 emit_cpy_import_star,
815 emit_cpy_load_const_tok,
816 emit_cpy_load_const_small_int,
817 emit_cpy_load_const_int,
818 emit_cpy_load_const_dec,
819 emit_cpy_load_const_id,
820 emit_cpy_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100821 emit_cpy_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100822 emit_cpy_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100823 emit_cpy_load_deref,
824 emit_cpy_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000825 emit_cpy_load_name,
826 emit_cpy_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100827 emit_cpy_load_attr,
828 emit_cpy_load_method,
829 emit_cpy_load_build_class,
830 emit_cpy_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000831 emit_cpy_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100832 emit_cpy_store_name,
833 emit_cpy_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100834 emit_cpy_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100835 emit_cpy_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100836 emit_cpy_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100837 emit_cpy_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000838 emit_cpy_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100839 emit_cpy_delete_name,
840 emit_cpy_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100841 emit_cpy_delete_attr,
842 emit_cpy_delete_subscr,
843 emit_cpy_dup_top,
844 emit_cpy_dup_top_two,
845 emit_cpy_pop_top,
846 emit_cpy_rot_two,
847 emit_cpy_rot_three,
848 emit_cpy_jump,
849 emit_cpy_pop_jump_if_true,
850 emit_cpy_pop_jump_if_false,
851 emit_cpy_jump_if_true_or_pop,
852 emit_cpy_jump_if_false_or_pop,
853 emit_cpy_setup_loop,
854 emit_cpy_break_loop,
855 emit_cpy_continue_loop,
856 emit_cpy_setup_with,
857 emit_cpy_with_cleanup,
858 emit_cpy_setup_except,
859 emit_cpy_setup_finally,
860 emit_cpy_end_finally,
861 emit_cpy_get_iter,
862 emit_cpy_for_iter,
863 emit_cpy_for_iter_end,
864 emit_cpy_pop_block,
865 emit_cpy_pop_except,
866 emit_cpy_unary_op,
867 emit_cpy_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100868 emit_cpy_build_tuple,
869 emit_cpy_build_list,
870 emit_cpy_list_append,
871 emit_cpy_build_map,
872 emit_cpy_store_map,
873 emit_cpy_map_add,
874 emit_cpy_build_set,
875 emit_cpy_set_add,
876 emit_cpy_build_slice,
877 emit_cpy_unpack_sequence,
878 emit_cpy_unpack_ex,
879 emit_cpy_make_function,
880 emit_cpy_make_closure,
881 emit_cpy_call_function,
882 emit_cpy_call_method,
883 emit_cpy_return_value,
884 emit_cpy_raise_varargs,
885 emit_cpy_yield_value,
886 emit_cpy_yield_from,
887};
888
Damien3ef4abb2013-10-12 16:53:13 +0100889#endif // MICROPY_EMIT_CPYTHON