blob: 7b2d50fb7ea0e6ed6f4c0c44b1d593ec579a7fac [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"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
Damien429d7192013-10-04 19:53:11 +010012#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010014#include "emit.h"
15
Damien Georgee67ed5d2014-01-04 13:55:24 +000016// wrapper around everything in this file
Damien3ef4abb2013-10-12 16:53:13 +010017#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +010018
Damien415eb6f2013-10-05 12:19:06 +010019struct _emit_t {
Damien429d7192013-10-04 19:53:11 +010020 int pass;
Damien429d7192013-10-04 19:53:11 +010021 int byte_code_offset;
22 int stack_size;
23 bool last_emit_was_return_value;
24
25 scope_t *scope;
26
Damienb05d7072013-10-05 13:37:10 +010027 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010028 int *label_offsets;
29};
30
Damien6cdd3af2013-10-05 18:08:26 +010031emit_t *emit_cpython_new(uint max_num_labels) {
32 emit_t *emit = m_new(emit_t, 1);
33 emit->max_num_labels = max_num_labels;
34 emit->label_offsets = m_new(int, max_num_labels);
35 return emit;
36}
37
Damien415eb6f2013-10-05 12:19:06 +010038static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
Damien429d7192013-10-04 19:53:11 +010039}
40
Damien415eb6f2013-10-05 12:19:06 +010041static void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien429d7192013-10-04 19:53:11 +010042 emit->pass = pass;
Damien429d7192013-10-04 19:53:11 +010043 emit->byte_code_offset = 0;
44 emit->stack_size = 0;
45 emit->last_emit_was_return_value = false;
46 emit->scope = scope;
Damienb05d7072013-10-05 13:37:10 +010047 if (pass == PASS_2) {
48 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010049 }
50}
51
Damien415eb6f2013-10-05 12:19:06 +010052static void emit_cpy_end_pass(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010053 // check stack is back to zero size
54 if (emit->stack_size != 0) {
55 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
56 }
Damien429d7192013-10-04 19:53:11 +010057}
58
Damien415eb6f2013-10-05 12:19:06 +010059static bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010060 return emit->last_emit_was_return_value;
61}
62
Damien415eb6f2013-10-05 12:19:06 +010063static int emit_cpy_get_stack_size(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +010064 return emit->stack_size;
65}
66
Damien415eb6f2013-10-05 12:19:06 +010067static void emit_cpy_set_stack_size(emit_t *emit, int size) {
Damien429d7192013-10-04 19:53:11 +010068 emit->stack_size = size;
69}
70
Damien4b03e772013-10-05 14:17:09 +010071static void emit_cpy_load_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010072 emit_common_load_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010073}
74
75static void emit_cpy_store_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010076 emit_common_store_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010077}
78
79static void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
Damien6cdd3af2013-10-05 18:08:26 +010080 emit_common_delete_id(emit, &emit_cpython_method_table, emit->scope, qstr);
Damien4b03e772013-10-05 14:17:09 +010081}
82
Damien415eb6f2013-10-05 12:19:06 +010083static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
Damien429d7192013-10-04 19:53:11 +010084 emit->stack_size += stack_size_delta;
Damienb05d7072013-10-05 13:37:10 +010085 if (emit->stack_size > emit->scope->stack_size) {
Damien429d7192013-10-04 19:53:11 +010086 emit->scope->stack_size = emit->stack_size;
87 }
88 emit->last_emit_was_return_value = false;
89 if (emit->pass == PASS_3 && byte_code_size > 0) {
90 if (emit->byte_code_offset >= 1000) {
91 printf("%d ", emit->byte_code_offset);
92 } else {
93 printf("% 4d ", emit->byte_code_offset);
94 }
95 }
96 emit->byte_code_offset += byte_code_size;
97}
98
Damien415eb6f2013-10-05 12:19:06 +010099static void emit_cpy_label_assign(emit_t *emit, int l) {
Damien429d7192013-10-04 19:53:11 +0100100 emit_pre(emit, 0, 0);
Damienb05d7072013-10-05 13:37:10 +0100101 assert(l < emit->max_num_labels);
102 if (emit->pass == PASS_2) {
103 // assign label offset
104 assert(emit->label_offsets[l] == -1);
105 emit->label_offsets[l] = emit->byte_code_offset;
106 } else if (emit->pass == PASS_3) {
107 // ensure label offset has not changed from PASS_2 to PASS_3
108 assert(emit->label_offsets[l] == emit->byte_code_offset);
109 //printf("l%d: (at %d)\n", l, emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100110 }
111}
112
Damien415eb6f2013-10-05 12:19:06 +0100113static void emit_cpy_import_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100114 emit_pre(emit, -1, 3);
115 if (emit->pass == PASS_3) {
116 printf("IMPORT_NAME %s\n", qstr_str(qstr));
117 }
118}
119
Damien415eb6f2013-10-05 12:19:06 +0100120static void emit_cpy_import_from(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100121 emit_pre(emit, 1, 3);
122 if (emit->pass == PASS_3) {
123 printf("IMPORT_FROM %s\n", qstr_str(qstr));
124 }
125}
126
Damien415eb6f2013-10-05 12:19:06 +0100127static void emit_cpy_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100128 emit_pre(emit, -1, 1);
129 if (emit->pass == PASS_3) {
130 printf("IMPORT_STAR\n");
131 }
132}
133
Damiende690d12013-12-29 18:01:01 +0000134static void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100135 emit_pre(emit, 1, 3);
136 if (emit->pass == PASS_3) {
137 printf("LOAD_CONST ");
138 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000139 case MP_TOKEN_KW_FALSE: printf("False"); break;
140 case MP_TOKEN_KW_NONE: printf("None"); break;
141 case MP_TOKEN_KW_TRUE: printf("True"); break;
Damien429d7192013-10-04 19:53:11 +0100142 default: printf("?=%d\n", tok); return; assert(0);
143 }
144 printf("\n");
145 }
146}
147
Damien415eb6f2013-10-05 12:19:06 +0100148static void emit_cpy_load_const_small_int(emit_t *emit, int arg) {
Damien429d7192013-10-04 19:53:11 +0100149 emit_pre(emit, 1, 3);
150 if (emit->pass == PASS_3) {
151 printf("LOAD_CONST %d\n", arg);
152 }
153}
154
Damien415eb6f2013-10-05 12:19:06 +0100155static void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100156 emit_pre(emit, 1, 3);
157 if (emit->pass == PASS_3) {
158 printf("LOAD_CONST %s\n", qstr_str(qstr));
159 }
160}
161
Damien415eb6f2013-10-05 12:19:06 +0100162static void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100163 emit_pre(emit, 1, 3);
164 if (emit->pass == PASS_3) {
165 printf("LOAD_CONST %s\n", qstr_str(qstr));
166 }
167}
168
Damien415eb6f2013-10-05 12:19:06 +0100169static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100170 emit_pre(emit, 1, 3);
171 if (emit->pass == PASS_3) {
172 printf("LOAD_CONST '%s'\n", qstr_str(qstr));
173 }
174}
175
Damiena1b26932013-12-12 15:34:40 +0000176static void print_quoted_str(qstr qstr, bool bytes) {
177 const char *str = qstr_str(qstr);
178 int len = strlen(str);
179 bool has_single_quote = false;
180 bool has_double_quote = false;
181 for (int i = 0; i < len; i++) {
182 if (str[i] == '\'') {
183 has_single_quote = true;
184 } else if (str[i] == '"') {
185 has_double_quote = true;
186 }
187 }
188 if (bytes) {
189 printf("b");
190 }
191 bool quote_single = false;
192 if (has_single_quote && !has_double_quote) {
193 printf("\"");
194 } else {
195 quote_single = true;
196 printf("'");
197 }
198 for (int i = 0; i < len; i++) {
199 if (str[i] == '\n') {
200 printf("\\n");
201 } else if (str[i] == '\\') {
202 printf("\\\\");
203 } else if (str[i] == '\'' && quote_single) {
204 printf("\\'");
205 } else {
206 printf("%c", str[i]);
207 }
208 }
209 if (has_single_quote && !has_double_quote) {
210 printf("\"");
211 } else {
212 printf("'");
213 }
214}
215
Damien415eb6f2013-10-05 12:19:06 +0100216static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100217 emit_pre(emit, 1, 3);
218 if (emit->pass == PASS_3) {
219 printf("LOAD_CONST ");
Damiena1b26932013-12-12 15:34:40 +0000220 print_quoted_str(qstr, bytes);
Damien429d7192013-10-04 19:53:11 +0100221 printf("\n");
222 }
223}
224
Damien415eb6f2013-10-05 12:19:06 +0100225static void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000226 emit_pre(emit, 1, 3);
Damien429d7192013-10-04 19:53:11 +0100227 if (emit->pass == PASS_3) {
Damiena1b26932013-12-12 15:34:40 +0000228 printf("LOAD_CONST %s\n", str);
Damien429d7192013-10-04 19:53:11 +0100229 }
230}
231
Damien6cdd3af2013-10-05 18:08:26 +0100232static void emit_cpy_load_fast(emit_t *emit, qstr qstr, int local_num) {
233 emit_pre(emit, 1, 3);
234 if (emit->pass == PASS_3) {
235 printf("LOAD_FAST %d %s\n", local_num, qstr_str(qstr));
236 }
237}
238
Damien27fb45e2013-10-20 15:07:49 +0100239static void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100240 emit_pre(emit, 1, 3);
241 if (emit->pass == PASS_3) {
Damien27fb45e2013-10-20 15:07:49 +0100242 printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
Damien429d7192013-10-04 19:53:11 +0100243 }
244}
245
Damien27fb45e2013-10-20 15:07:49 +0100246static void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100247 emit_pre(emit, 1, 3);
248 if (emit->pass == PASS_3) {
Damien27fb45e2013-10-20 15:07:49 +0100249 printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
Damien429d7192013-10-04 19:53:11 +0100250 }
251}
252
Damien9ecbcff2013-12-11 00:41:43 +0000253static void emit_cpy_load_name(emit_t *emit, qstr qstr) {
254 emit_pre(emit, 1, 3);
255 if (emit->pass == PASS_3) {
256 printf("LOAD_NAME %s\n", qstr_str(qstr));
257 }
258}
259
260static void emit_cpy_load_global(emit_t *emit, qstr qstr) {
261 emit_pre(emit, 1, 3);
262 if (emit->pass == PASS_3) {
263 printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
264 }
265}
266
Damien415eb6f2013-10-05 12:19:06 +0100267static void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100268 emit_pre(emit, 0, 3);
269 if (emit->pass == PASS_3) {
270 printf("LOAD_ATTR %s\n", qstr_str(qstr));
271 }
272}
273
Damien415eb6f2013-10-05 12:19:06 +0100274static void emit_cpy_load_method(emit_t *emit, qstr qstr) {
275 emit_cpy_load_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100276}
277
Damien415eb6f2013-10-05 12:19:06 +0100278static void emit_cpy_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100279 emit_pre(emit, 1, 1);
280 if (emit->pass == PASS_3) {
281 printf("LOAD_BUILD_CLASS\n");
282 }
283}
284
Damien6cdd3af2013-10-05 18:08:26 +0100285static void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
286 emit_pre(emit, -1, 3);
287 if (emit->pass == PASS_3) {
288 printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
289 }
290}
291
Damien9ecbcff2013-12-11 00:41:43 +0000292static void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
293 emit_pre(emit, -1, 3);
294 if (emit->pass == PASS_3) {
295 printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
296 }
297}
298
Damien415eb6f2013-10-05 12:19:06 +0100299static void emit_cpy_store_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100300 emit_pre(emit, -1, 3);
301 if (emit->pass == PASS_3) {
302 printf("STORE_NAME %s\n", qstr_str(qstr));
303 }
304}
305
Damien415eb6f2013-10-05 12:19:06 +0100306static void emit_cpy_store_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100307 emit_pre(emit, -1, 3);
308 if (emit->pass == PASS_3) {
309 printf("STORE_GLOBAL %s\n", qstr_str(qstr));
310 }
311}
312
Damien415eb6f2013-10-05 12:19:06 +0100313static void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100314 emit_pre(emit, -2, 3);
315 if (emit->pass == PASS_3) {
316 printf("STORE_ATTR %s\n", qstr_str(qstr));
317 }
318}
319
Damien415eb6f2013-10-05 12:19:06 +0100320static void emit_cpy_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100321 emit_pre(emit, -3, 1);
322 if (emit->pass == PASS_3) {
323 printf("STORE_SUBSCR\n");
324 }
325}
326
Damiena3977762013-10-09 23:10:10 +0100327static void emit_cpy_store_locals(emit_t *emit) {
328 emit_pre(emit, -1, 1);
329 if (emit->pass == PASS_3) {
330 printf("STORE_LOCALS\n");
331 }
332}
333
Damien6cdd3af2013-10-05 18:08:26 +0100334static void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
335 emit_pre(emit, 0, 3);
336 if (emit->pass == PASS_3) {
337 printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
338 }
339}
340
Damien9ecbcff2013-12-11 00:41:43 +0000341static void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
342 emit_pre(emit, 0, 3);
343 if (emit->pass == PASS_3) {
344 printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
345 }
346}
347
Damien415eb6f2013-10-05 12:19:06 +0100348static void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100349 emit_pre(emit, 0, 3);
350 if (emit->pass == PASS_3) {
351 printf("DELETE_NAME %s\n", qstr_str(qstr));
352 }
353}
354
Damien415eb6f2013-10-05 12:19:06 +0100355static void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100356 emit_pre(emit, 0, 3);
357 if (emit->pass == PASS_3) {
358 printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
359 }
360}
361
Damien415eb6f2013-10-05 12:19:06 +0100362static void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100363 emit_pre(emit, -1, 3);
364 if (emit->pass == PASS_3) {
365 printf("DELETE_ATTR %s\n", qstr_str(qstr));
366 }
367}
368
Damien415eb6f2013-10-05 12:19:06 +0100369static void emit_cpy_delete_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100370 emit_pre(emit, -2, 1);
371 if (emit->pass == PASS_3) {
372 printf("DELETE_SUBSCR\n");
373 }
374}
375
Damien415eb6f2013-10-05 12:19:06 +0100376static void emit_cpy_dup_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100377 emit_pre(emit, 1, 1);
378 if (emit->pass == PASS_3) {
379 printf("DUP_TOP\n");
380 }
381}
382
Damien415eb6f2013-10-05 12:19:06 +0100383static void emit_cpy_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100384 emit_pre(emit, 2, 1);
385 if (emit->pass == PASS_3) {
386 printf("DUP_TOP_TWO\n");
387 }
388}
389
Damien415eb6f2013-10-05 12:19:06 +0100390static void emit_cpy_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100391 emit_pre(emit, -1, 1);
392 if (emit->pass == PASS_3) {
393 printf("POP_TOP\n");
394 }
395}
396
Damien415eb6f2013-10-05 12:19:06 +0100397static void emit_cpy_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100398 emit_pre(emit, 0, 1);
399 if (emit->pass == PASS_3) {
400 printf("ROT_TWO\n");
401 }
402}
403
Damien415eb6f2013-10-05 12:19:06 +0100404static void emit_cpy_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100405 emit_pre(emit, 0, 1);
406 if (emit->pass == PASS_3) {
407 printf("ROT_THREE\n");
408 }
409}
410
Damien415eb6f2013-10-05 12:19:06 +0100411static void emit_cpy_jump(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100412 emit_pre(emit, 0, 3);
413 if (emit->pass == PASS_3) {
414 int dest = emit->label_offsets[label];
415 if (dest < emit->byte_code_offset) {
416 printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
417 } else {
418 printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
419 }
420 }
421}
422
Damien415eb6f2013-10-05 12:19:06 +0100423static void emit_cpy_pop_jump_if_true(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100424 emit_pre(emit, -1, 3);
425 if (emit->pass == PASS_3) {
426 printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
427 }
428}
429
Damien415eb6f2013-10-05 12:19:06 +0100430static void emit_cpy_pop_jump_if_false(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100431 emit_pre(emit, -1, 3);
432 if (emit->pass == PASS_3) {
433 printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
434 }
435}
436
Damien415eb6f2013-10-05 12:19:06 +0100437static void emit_cpy_jump_if_true_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100438 emit_pre(emit, -1, 3);
439 if (emit->pass == PASS_3) {
440 printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
441 }
442}
443
Damien415eb6f2013-10-05 12:19:06 +0100444static void emit_cpy_jump_if_false_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100445 emit_pre(emit, -1, 3);
446 if (emit->pass == PASS_3) {
447 printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
448 }
449}
450
Damien415eb6f2013-10-05 12:19:06 +0100451static void emit_cpy_setup_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100452 emit_pre(emit, 0, 3);
453 if (emit->pass == PASS_3) {
454 printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
455 }
456}
457
Damien415eb6f2013-10-05 12:19:06 +0100458static void emit_cpy_break_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100459 emit_pre(emit, 0, 1);
460 if (emit->pass == PASS_3) {
461 printf("BREAK_LOOP\n"); // CPython doesn't have label
462 //printf("BREAK_LOOP %d\n", emit->label_offsets[label]);
463 }
464}
465
Damien415eb6f2013-10-05 12:19:06 +0100466static void emit_cpy_continue_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100467 emit_pre(emit, 0, 3);
468 if (emit->pass == PASS_3) {
469 printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
470 }
471}
472
Damien415eb6f2013-10-05 12:19:06 +0100473static void emit_cpy_setup_with(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100474 emit_pre(emit, 7, 3);
475 if (emit->pass == PASS_3) {
476 printf("SETUP_WITH %d\n", emit->label_offsets[label]);
477 }
478}
479
Damien415eb6f2013-10-05 12:19:06 +0100480static void emit_cpy_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100481 emit_pre(emit, -7, 1);
482 if (emit->pass == PASS_3) {
483 printf("WITH_CLEANUP\n");
484 }
485}
486
Damien415eb6f2013-10-05 12:19:06 +0100487static void emit_cpy_setup_except(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100488 emit_pre(emit, 6, 3);
489 if (emit->pass == PASS_3) {
490 printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
491 }
492}
493
Damien415eb6f2013-10-05 12:19:06 +0100494static void emit_cpy_setup_finally(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100495 emit_pre(emit, 6, 3);
496 if (emit->pass == PASS_3) {
497 printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
498 }
499}
500
Damien415eb6f2013-10-05 12:19:06 +0100501static void emit_cpy_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100502 emit_pre(emit, -1, 1);
503 if (emit->pass == PASS_3) {
504 printf("END_FINALLY\n");
505 }
506}
507
Damien415eb6f2013-10-05 12:19:06 +0100508static void emit_cpy_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100509 emit_pre(emit, 0, 1);
510 if (emit->pass == PASS_3) {
511 printf("GET_ITER\n");
512 }
513}
514
Damien415eb6f2013-10-05 12:19:06 +0100515static void emit_cpy_for_iter(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100516 emit_pre(emit, 1, 3);
517 if (emit->pass == PASS_3) {
518 printf("FOR_ITER %d\n", emit->label_offsets[label]);
519 }
520}
521
Damien415eb6f2013-10-05 12:19:06 +0100522static void emit_cpy_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100523 emit_pre(emit, -1, 0);
524}
525
Damien415eb6f2013-10-05 12:19:06 +0100526static void emit_cpy_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100527 emit_pre(emit, 0, 1);
528 if (emit->pass == PASS_3) {
529 printf("POP_BLOCK\n");
530 }
531}
532
Damien415eb6f2013-10-05 12:19:06 +0100533static void emit_cpy_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100534 emit_pre(emit, 0, 1);
535 if (emit->pass == PASS_3) {
536 printf("POP_EXCEPT\n");
537 }
538}
539
Damien415eb6f2013-10-05 12:19:06 +0100540static void emit_cpy_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100541 emit_pre(emit, 0, 1);
542 if (emit->pass == PASS_3) {
543 switch (op) {
544 case RT_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
545 case RT_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
546 case RT_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
547 case RT_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
548 default: assert(0);
549 }
550 }
551}
552
Damien415eb6f2013-10-05 12:19:06 +0100553static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100554 emit_pre(emit, -1, 1);
555 if (emit->pass == PASS_3) {
556 switch (op) {
557 case RT_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
558 case RT_BINARY_OP_OR: printf("BINARY_OR\n"); break;
559 case RT_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
560 case RT_BINARY_OP_AND: printf("BINARY_AND\n"); break;
561 case RT_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
562 case RT_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
563 case RT_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
564 case RT_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
565 case RT_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
566 case RT_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
567 case RT_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
568 case RT_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
569 case RT_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
570 case RT_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
571 case RT_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
572 case RT_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
573 case RT_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
574 case RT_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
575 case RT_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
576 case RT_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
577 case RT_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
578 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
579 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
580 case RT_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
581 case RT_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
582 default: assert(0);
583 }
584 }
585}
586
Damien415eb6f2013-10-05 12:19:06 +0100587static void emit_cpy_compare_op(emit_t *emit, rt_compare_op_t op) {
Damien429d7192013-10-04 19:53:11 +0100588 emit_pre(emit, -1, 3);
589 if (emit->pass == PASS_3) {
590 switch (op) {
591 case RT_COMPARE_OP_LESS: printf("COMPARE_OP <\n"); break;
592 case RT_COMPARE_OP_MORE: printf("COMPARE_OP >\n"); break;
593 case RT_COMPARE_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
594 case RT_COMPARE_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
595 case RT_COMPARE_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
596 case RT_COMPARE_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
597 case RT_COMPARE_OP_IN: printf("COMPARE_OP in\n"); break;
598 case RT_COMPARE_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
599 case RT_COMPARE_OP_IS: printf("COMPARE_OP is\n"); break;
600 case RT_COMPARE_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
601 case RT_COMPARE_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
602 default: assert(0);
603 }
604 }
605}
606
Damien415eb6f2013-10-05 12:19:06 +0100607static void emit_cpy_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100608 emit_pre(emit, 1 - n_args, 3);
609 if (emit->pass == PASS_3) {
610 printf("BUILD_TUPLE %d\n", n_args);
611 }
612}
613
Damien415eb6f2013-10-05 12:19:06 +0100614static void emit_cpy_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100615 emit_pre(emit, 1 - n_args, 3);
616 if (emit->pass == PASS_3) {
617 printf("BUILD_LIST %d\n", n_args);
618 }
619}
620
Damien415eb6f2013-10-05 12:19:06 +0100621static void emit_cpy_list_append(emit_t *emit, int list_index) {
Damien429d7192013-10-04 19:53:11 +0100622 emit_pre(emit, -1, 3);
623 if (emit->pass == PASS_3) {
624 printf("LIST_APPEND %d\n", list_index);
625 }
626}
627
Damien415eb6f2013-10-05 12:19:06 +0100628static void emit_cpy_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100629 emit_pre(emit, 1, 3);
630 if (emit->pass == PASS_3) {
631 printf("BUILD_MAP %d\n", n_args);
632 }
633}
634
Damien415eb6f2013-10-05 12:19:06 +0100635static void emit_cpy_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100636 emit_pre(emit, -2, 1);
637 if (emit->pass == PASS_3) {
638 printf("STORE_MAP\n");
639 }
640}
641
Damien415eb6f2013-10-05 12:19:06 +0100642static void emit_cpy_map_add(emit_t *emit, int map_index) {
Damien429d7192013-10-04 19:53:11 +0100643 emit_pre(emit, -2, 3);
644 if (emit->pass == PASS_3) {
645 printf("MAP_ADD %d\n", map_index);
646 }
647}
648
Damien415eb6f2013-10-05 12:19:06 +0100649static void emit_cpy_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100650 emit_pre(emit, 1 - n_args, 3);
651 if (emit->pass == PASS_3) {
652 printf("BUILD_SET %d\n", n_args);
653 }
654}
655
Damien415eb6f2013-10-05 12:19:06 +0100656static void emit_cpy_set_add(emit_t *emit, int set_index) {
Damien429d7192013-10-04 19:53:11 +0100657 emit_pre(emit, -1, 3);
658 if (emit->pass == PASS_3) {
659 printf("SET_ADD %d\n", set_index);
660 }
661}
662
Damien415eb6f2013-10-05 12:19:06 +0100663static void emit_cpy_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100664 emit_pre(emit, 1 - n_args, 3);
665 if (emit->pass == PASS_3) {
666 printf("BUILD_SLICE %d\n", n_args);
667 }
668}
669
Damien415eb6f2013-10-05 12:19:06 +0100670static void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100671 emit_pre(emit, -1 + n_args, 3);
672 if (emit->pass == PASS_3) {
673 printf("UNPACK_SEQUENCE %d\n", n_args);
674 }
675}
676
Damien415eb6f2013-10-05 12:19:06 +0100677static void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100678 emit_pre(emit, -1 + n_left + n_right + 1, 3);
679 if (emit->pass == PASS_3) {
680 printf("UNPACK_EX %d\n", n_left | (n_right << 8));
681 }
682}
683
Damien415eb6f2013-10-05 12:19:06 +0100684static 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 +0100685 int s = 0;
686 if (have_star_arg) {
687 s += 1;
688 }
689 if (have_dbl_star_arg) {
690 s += 1;
691 }
692 emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
693 if (emit->pass == PASS_3) {
694 if (have_star_arg) {
695 if (have_dbl_star_arg) {
696 printf("CALL_FUNCTION_VAR_KW");
697 } else {
698 printf("CALL_FUNCTION_VAR");
699 }
700 } else {
701 if (have_dbl_star_arg) {
702 printf("CALL_FUNCTION_KW");
703 } else {
704 printf("CALL_FUNCTION");
705 }
706 }
707 printf(" %d, %d\n", n_positional, n_keyword);
708 }
709}
710
Damien415eb6f2013-10-05 12:19:06 +0100711static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
712 emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +0100713}
714
Damien415eb6f2013-10-05 12:19:06 +0100715static void emit_cpy_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100716 emit_pre(emit, -1, 1);
717 emit->last_emit_was_return_value = true;
718 if (emit->pass == PASS_3) {
719 printf("RETURN_VALUE\n");
720 }
721}
722
Damien415eb6f2013-10-05 12:19:06 +0100723static void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100724 emit_pre(emit, -n_args, 3);
725 if (emit->pass == PASS_3) {
726 printf("RAISE_VARARGS %d\n", n_args);
727 }
728}
729
Damien415eb6f2013-10-05 12:19:06 +0100730static void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100731 emit_pre(emit, 1, 3);
732 if (emit->pass == PASS_3) {
733 printf("LOAD_CONST code %s\n", qstr_str(qstr));
734 }
735 // load qualified name
736 emit_pre(emit, 1, 3);
737 if (emit->pass == PASS_3) {
738 printf("LOAD_CONST '");
739 // code just to work out the qualname (or whatever it is)
740 {
741 int depth = 0;
742 for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
743 depth += 1;
744 }
745 for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
746 scope_t *s = emit->scope;
747 for (int i = 0; i < wanted_depth; i++) {
748 s = s->parent;
749 }
750 if (s->kind == SCOPE_FUNCTION) {
751 printf("%s.<locals>.", qstr_str(s->simple_name));
752 } else if (s->kind == SCOPE_CLASS) {
753 printf("%s.", qstr_str(s->simple_name));
754 }
755 }
756 }
757 printf("%s'\n", qstr_str(qstr));
758 }
759}
760
Damien415eb6f2013-10-05 12:19:06 +0100761static void emit_cpy_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
762 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100763 emit_pre(emit, -1 - n_default_params - 2 * n_dict_params, 3);
764 if (emit->pass == PASS_3) {
765 printf("MAKE_FUNCTION %d\n", (n_dict_params << 8) | n_default_params);
766 }
767}
768
Damien415eb6f2013-10-05 12:19:06 +0100769static void emit_cpy_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
770 load_cpy_const_code_and_name(emit, scope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100771 emit_pre(emit, -2 - n_default_params - 2 * n_dict_params, 3);
772 if (emit->pass == PASS_3) {
773 printf("MAKE_CLOSURE %d\n", (n_dict_params << 8) | n_default_params);
774 }
775}
776
Damien415eb6f2013-10-05 12:19:06 +0100777static void emit_cpy_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100778 emit_pre(emit, 0, 1);
779 if (emit->pass == PASS_2) {
780 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
781 }
782 if (emit->pass == PASS_3) {
783 printf("YIELD_VALUE\n");
784 }
785}
786
Damien415eb6f2013-10-05 12:19:06 +0100787static void emit_cpy_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100788 emit_pre(emit, -1, 1);
789 if (emit->pass == PASS_2) {
790 emit->scope->flags |= SCOPE_FLAG_GENERATOR;
791 }
792 if (emit->pass == PASS_3) {
793 printf("YIELD_FROM\n");
794 }
795}
796
Damien6cdd3af2013-10-05 18:08:26 +0100797const emit_method_table_t emit_cpython_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100798 emit_cpy_set_native_types,
799 emit_cpy_start_pass,
800 emit_cpy_end_pass,
801 emit_cpy_last_emit_was_return_value,
802 emit_cpy_get_stack_size,
803 emit_cpy_set_stack_size,
804
Damien4b03e772013-10-05 14:17:09 +0100805 emit_cpy_load_id,
806 emit_cpy_store_id,
807 emit_cpy_delete_id,
808
Damien415eb6f2013-10-05 12:19:06 +0100809 emit_cpy_label_assign,
810 emit_cpy_import_name,
811 emit_cpy_import_from,
812 emit_cpy_import_star,
813 emit_cpy_load_const_tok,
814 emit_cpy_load_const_small_int,
815 emit_cpy_load_const_int,
816 emit_cpy_load_const_dec,
817 emit_cpy_load_const_id,
818 emit_cpy_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100819 emit_cpy_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100820 emit_cpy_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100821 emit_cpy_load_deref,
822 emit_cpy_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000823 emit_cpy_load_name,
824 emit_cpy_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100825 emit_cpy_load_attr,
826 emit_cpy_load_method,
827 emit_cpy_load_build_class,
828 emit_cpy_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000829 emit_cpy_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100830 emit_cpy_store_name,
831 emit_cpy_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100832 emit_cpy_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100833 emit_cpy_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100834 emit_cpy_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100835 emit_cpy_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000836 emit_cpy_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100837 emit_cpy_delete_name,
838 emit_cpy_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100839 emit_cpy_delete_attr,
840 emit_cpy_delete_subscr,
841 emit_cpy_dup_top,
842 emit_cpy_dup_top_two,
843 emit_cpy_pop_top,
844 emit_cpy_rot_two,
845 emit_cpy_rot_three,
846 emit_cpy_jump,
847 emit_cpy_pop_jump_if_true,
848 emit_cpy_pop_jump_if_false,
849 emit_cpy_jump_if_true_or_pop,
850 emit_cpy_jump_if_false_or_pop,
851 emit_cpy_setup_loop,
852 emit_cpy_break_loop,
853 emit_cpy_continue_loop,
854 emit_cpy_setup_with,
855 emit_cpy_with_cleanup,
856 emit_cpy_setup_except,
857 emit_cpy_setup_finally,
858 emit_cpy_end_finally,
859 emit_cpy_get_iter,
860 emit_cpy_for_iter,
861 emit_cpy_for_iter_end,
862 emit_cpy_pop_block,
863 emit_cpy_pop_except,
864 emit_cpy_unary_op,
865 emit_cpy_binary_op,
866 emit_cpy_compare_op,
867 emit_cpy_build_tuple,
868 emit_cpy_build_list,
869 emit_cpy_list_append,
870 emit_cpy_build_map,
871 emit_cpy_store_map,
872 emit_cpy_map_add,
873 emit_cpy_build_set,
874 emit_cpy_set_add,
875 emit_cpy_build_slice,
876 emit_cpy_unpack_sequence,
877 emit_cpy_unpack_ex,
878 emit_cpy_make_function,
879 emit_cpy_make_closure,
880 emit_cpy_call_function,
881 emit_cpy_call_method,
882 emit_cpy_return_value,
883 emit_cpy_raise_varargs,
884 emit_cpy_yield_value,
885 emit_cpy_yield_from,
886};
887
Damien3ef4abb2013-10-12 16:53:13 +0100888#endif // MICROPY_EMIT_CPYTHON