2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 03:14:26 -04:00
|
|
|
#include <stdbool.h>
|
2013-10-04 14:53:11 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/emit.h"
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-01-04 08:55:24 -05:00
|
|
|
// wrapper around everything in this file
|
2013-10-12 11:53:13 -04:00
|
|
|
#if MICROPY_EMIT_CPYTHON
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-10-05 07:19:06 -04:00
|
|
|
struct _emit_t {
|
2013-10-04 14:53:11 -04:00
|
|
|
int pass;
|
2014-05-10 05:36:38 -04:00
|
|
|
int bytecode_offset;
|
2013-10-04 14:53:11 -04:00
|
|
|
int stack_size;
|
|
|
|
bool last_emit_was_return_value;
|
|
|
|
|
|
|
|
scope_t *scope;
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
mp_uint_t max_num_labels;
|
|
|
|
mp_uint_t *label_offsets;
|
2013-10-04 14:53:11 -04:00
|
|
|
};
|
|
|
|
|
2015-03-26 11:49:53 -04:00
|
|
|
emit_t *emit_cpython_new(void) {
|
|
|
|
emit_t *emit = m_new0(emit_t, 1);
|
|
|
|
return emit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emit_cpython_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels) {
|
2013-10-05 13:08:26 -04:00
|
|
|
emit->max_num_labels = max_num_labels;
|
2014-09-08 18:05:16 -04:00
|
|
|
emit->label_offsets = m_new(mp_uint_t, max_num_labels);
|
2015-03-26 11:49:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void emit_cpython_free(emit_t *emit) {
|
|
|
|
m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
|
|
|
|
m_del_obj(emit_t, emit);
|
2013-10-05 13:08:26 -04:00
|
|
|
}
|
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
STATIC void emit_cpy_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit->pass = pass;
|
2014-05-10 05:36:38 -04:00
|
|
|
emit->bytecode_offset = 0;
|
2013-10-04 14:53:11 -04:00
|
|
|
emit->stack_size = 0;
|
|
|
|
emit->last_emit_was_return_value = false;
|
|
|
|
emit->scope = scope;
|
2014-05-07 12:24:22 -04:00
|
|
|
if (pass < MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_end_pass(emit_t *emit) {
|
2015-03-26 11:49:53 -04:00
|
|
|
if (emit->pass == MP_PASS_SCOPE) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
// check stack is back to zero size
|
|
|
|
if (emit->stack_size != 0) {
|
|
|
|
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
return emit->last_emit_was_return_value;
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_adjust_stack_size(emit_t *emit, mp_int_t delta) {
|
2014-04-10 13:28:54 -04:00
|
|
|
emit->stack_size += delta;
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_set_source_line(emit_t *emit, mp_uint_t source_line) {
|
2014-01-18 18:24:36 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
// TODO: module-polymorphic function (read: name clash if made global)
|
2014-05-10 05:36:38 -04:00
|
|
|
static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
|
2015-03-26 11:49:53 -04:00
|
|
|
if (emit->pass == MP_PASS_SCOPE) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
emit->stack_size += stack_size_delta;
|
2013-10-05 08:37:10 -04:00
|
|
|
if (emit->stack_size > emit->scope->stack_size) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit->scope->stack_size = emit->stack_size;
|
|
|
|
}
|
|
|
|
emit->last_emit_was_return_value = false;
|
2014-05-10 05:36:38 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT && bytecode_size > 0) {
|
|
|
|
if (emit->bytecode_offset >= 1000) {
|
|
|
|
printf("%d ", emit->bytecode_offset);
|
2013-10-04 14:53:11 -04:00
|
|
|
} else {
|
2014-05-10 05:36:38 -04:00
|
|
|
printf("% 4d ", emit->bytecode_offset);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 05:36:38 -04:00
|
|
|
emit->bytecode_offset += bytecode_size;
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_label_assign(emit_t *emit, mp_uint_t l) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 0);
|
2015-03-26 11:49:53 -04:00
|
|
|
if (emit->pass == MP_PASS_SCOPE) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 08:37:10 -04:00
|
|
|
assert(l < emit->max_num_labels);
|
2014-05-07 13:57:32 -04:00
|
|
|
if (emit->pass < MP_PASS_EMIT) {
|
2013-10-05 08:37:10 -04:00
|
|
|
// assign label offset
|
|
|
|
assert(emit->label_offsets[l] == -1);
|
2014-05-10 05:36:38 -04:00
|
|
|
emit->label_offsets[l] = emit->bytecode_offset;
|
2014-05-07 12:24:22 -04:00
|
|
|
} else {
|
|
|
|
// ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
|
2014-05-10 05:36:38 -04:00
|
|
|
assert(emit->label_offsets[l] == emit->bytecode_offset);
|
|
|
|
//printf("l%d: (at %d)\n", l, emit->bytecode_offset);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_import_name(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("IMPORT_NAME %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_import_from(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("IMPORT_FROM %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_import_star(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("IMPORT_STAR\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("LOAD_CONST ");
|
|
|
|
switch (tok) {
|
2013-12-21 13:17:45 -05:00
|
|
|
case MP_TOKEN_KW_FALSE: printf("False"); break;
|
|
|
|
case MP_TOKEN_KW_NONE: printf("None"); break;
|
|
|
|
case MP_TOKEN_KW_TRUE: printf("True"); break;
|
2013-10-04 14:53:11 -04:00
|
|
|
default: printf("?=%d\n", tok); return; assert(0);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-01-29 13:58:52 -05:00
|
|
|
printf("LOAD_CONST " INT_FMT "\n", arg);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void print_quoted_str(qstr qst, bool bytes) {
|
|
|
|
const char *str = qstr_str(qst);
|
2013-12-12 10:34:40 -05:00
|
|
|
int len = strlen(str);
|
|
|
|
bool has_single_quote = false;
|
|
|
|
bool has_double_quote = false;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (str[i] == '\'') {
|
|
|
|
has_single_quote = true;
|
|
|
|
} else if (str[i] == '"') {
|
|
|
|
has_double_quote = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bytes) {
|
|
|
|
printf("b");
|
|
|
|
}
|
2014-01-25 08:51:19 -05:00
|
|
|
int quote_char = '\'';
|
2013-12-12 10:34:40 -05:00
|
|
|
if (has_single_quote && !has_double_quote) {
|
2014-01-25 08:51:19 -05:00
|
|
|
quote_char = '"';
|
2013-12-12 10:34:40 -05:00
|
|
|
}
|
2014-01-25 08:51:19 -05:00
|
|
|
printf("%c", quote_char);
|
|
|
|
for (const char *s = str, *top = str + len; s < top; s++) {
|
|
|
|
if (*s == quote_char) {
|
|
|
|
printf("\\%c", quote_char);
|
|
|
|
} else if (*s == '\\') {
|
2013-12-12 10:34:40 -05:00
|
|
|
printf("\\\\");
|
2014-01-25 08:51:19 -05:00
|
|
|
} else if (32 <= *s && *s <= 126) {
|
|
|
|
printf("%c", *s);
|
|
|
|
} else if (*s == '\n') {
|
|
|
|
printf("\\n");
|
|
|
|
// TODO add more escape codes here
|
2013-12-12 10:34:40 -05:00
|
|
|
} else {
|
2014-01-25 08:51:19 -05:00
|
|
|
printf("\\x%02x", (*s) & 0xff);
|
2013-12-12 10:34:40 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-25 08:51:19 -05:00
|
|
|
printf("%c", quote_char);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qst, bool bytes) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("LOAD_CONST ");
|
2014-09-08 18:05:16 -04:00
|
|
|
print_quoted_str(qst, bytes);
|
2013-12-12 10:34:40 -05:00
|
|
|
printf("\n");
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 10:55:54 -05:00
|
|
|
STATIC void emit_cpy_load_const_obj(emit_t *emit, void *obj) {
|
|
|
|
emit_pre(emit, 1, 3);
|
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
|
|
|
printf("LOAD_CONST ");
|
|
|
|
mp_obj_print(obj, PRINT_REPR);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 12:50:40 -04:00
|
|
|
STATIC void emit_cpy_load_null(emit_t *emit) {
|
|
|
|
// unused for cpy
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2015-01-16 07:24:49 -05:00
|
|
|
STATIC void emit_cpy_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_name(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_NAME %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_global(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_GLOBAL %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_attr(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_ATTR %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_method(emit_t *emit, qstr qst) {
|
|
|
|
emit_cpy_load_attr(emit, qst);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_load_build_class(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("LOAD_BUILD_CLASS\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:10:53 -04:00
|
|
|
STATIC void emit_cpy_load_subscr(emit_t *emit) {
|
|
|
|
emit_pre(emit, -1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-04-17 17:10:53 -04:00
|
|
|
printf("BINARY_SUBSCR\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("STORE_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("STORE_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_store_name(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("STORE_NAME %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_store_global(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("STORE_GLOBAL %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_store_attr(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -2, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("STORE_ATTR %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_store_subscr(emit_t *emit) {
|
2013-10-09 18:10:10 -04:00
|
|
|
emit_pre(emit, -3, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-09 18:10:10 -04:00
|
|
|
printf("STORE_SUBSCR\n");
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("DELETE_FAST " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("DELETE_DEREF " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_delete_name(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("DELETE_NAME %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_delete_global(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("DELETE_GLOBAL %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("DELETE_ATTR %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_delete_subscr(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -2, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("DELETE_SUBSCR\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_dup_top(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("DUP_TOP\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_dup_top_two(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 2, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("DUP_TOP_TWO\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_pop_top(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("POP_TOP\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_rot_two(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("ROT_TWO\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_rot_three(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("ROT_THREE\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_jump(emit_t *emit, mp_uint_t label) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
int dest = emit->label_offsets[label];
|
2014-05-10 05:36:38 -04:00
|
|
|
if (dest < emit->bytecode_offset) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("JUMP_ABSOLUTE " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
} else {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("JUMP_FORWARD " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 10:04:06 -05:00
|
|
|
STATIC void emit_cpy_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2015-02-28 10:04:06 -05:00
|
|
|
if (cond) {
|
|
|
|
printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]);
|
|
|
|
} else {
|
|
|
|
printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]);
|
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 10:04:06 -05:00
|
|
|
STATIC void emit_cpy_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2015-02-28 10:04:06 -05:00
|
|
|
if (cond) {
|
|
|
|
printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
|
|
|
|
} else {
|
|
|
|
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
|
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-02-01 16:56:25 -05:00
|
|
|
printf("BREAK_LOOP\n");
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
|
2014-02-01 16:56:25 -05:00
|
|
|
if (except_depth == 0) {
|
|
|
|
emit_cpy_jump(emit, label);
|
|
|
|
} else {
|
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("CONTINUE_LOOP " UINT_FMT "\n", emit->label_offsets[label]);
|
2014-02-01 16:56:25 -05:00
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_setup_with(emit_t *emit, mp_uint_t label) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 7, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("SETUP_WITH " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_with_cleanup(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -7, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("WITH_CLEANUP\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_setup_except(emit_t *emit, mp_uint_t label) {
|
2014-04-10 13:22:19 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("SETUP_EXCEPT " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_setup_finally(emit_t *emit, mp_uint_t label) {
|
2014-04-10 13:22:19 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("SETUP_FINALLY " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_end_finally(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("END_FINALLY\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_get_iter(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("GET_ITER\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_for_iter(emit_t *emit, mp_uint_t label) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("FOR_ITER " UINT_FMT "\n", emit->label_offsets[label]);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_for_iter_end(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_pop_block(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("POP_BLOCK\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_pop_except(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("POP_EXCEPT\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
|
|
|
|
case MP_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
|
|
|
|
case MP_UNARY_OP_INVERT: printf("UNARY_INVERT\n"); break;
|
|
|
|
case MP_UNARY_OP_NOT: printf("UNARY_NOT\n"); break;
|
2013-10-04 14:53:11 -04:00
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
|
|
|
|
if (op <= MP_BINARY_OP_INPLACE_POWER) {
|
2014-01-11 04:47:06 -05:00
|
|
|
// CPython uses a byte code for each binary op
|
|
|
|
emit_pre(emit, -1, 1);
|
|
|
|
} else {
|
|
|
|
// CPython uses a byte code plus an argument for compare ops
|
|
|
|
emit_pre(emit, -1, 3);
|
|
|
|
}
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
|
|
|
|
case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
|
|
|
|
case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break;
|
|
|
|
case MP_BINARY_OP_LSHIFT: printf("BINARY_LSHIFT\n"); break;
|
|
|
|
case MP_BINARY_OP_RSHIFT: printf("BINARY_RSHIFT\n"); break;
|
|
|
|
case MP_BINARY_OP_ADD: printf("BINARY_ADD\n"); break;
|
|
|
|
case MP_BINARY_OP_SUBTRACT: printf("BINARY_SUBTRACT\n"); break;
|
|
|
|
case MP_BINARY_OP_MULTIPLY: printf("BINARY_MULTIPLY\n"); break;
|
|
|
|
case MP_BINARY_OP_FLOOR_DIVIDE: printf("BINARY_FLOOR_DIVIDE\n"); break;
|
|
|
|
case MP_BINARY_OP_TRUE_DIVIDE: printf("BINARY_TRUE_DIVIDE\n"); break;
|
|
|
|
case MP_BINARY_OP_MODULO: printf("BINARY_MODULO\n"); break;
|
|
|
|
case MP_BINARY_OP_POWER: printf("BINARY_POWER\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_OR: printf("INPLACE_OR\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_XOR: printf("INPLACE_XOR\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_AND: printf("INPLACE_AND\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_LSHIFT: printf("INPLACE_LSHIFT\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_RSHIFT: printf("INPLACE_RSHIFT\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_ADD: printf("INPLACE_ADD\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT: printf("INPLACE_SUBTRACT\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_MULTIPLY: printf("INPLACE_MULTIPLY\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: printf("INPLACE_FLOOR_DIVIDE\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: printf("INPLACE_TRUE_DIVIDE\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_MODULO: printf("INPLACE_MODULO\n"); break;
|
|
|
|
case MP_BINARY_OP_INPLACE_POWER: printf("INPLACE_POWER\n"); break;
|
|
|
|
case MP_BINARY_OP_LESS: printf("COMPARE_OP <\n"); break;
|
|
|
|
case MP_BINARY_OP_MORE: printf("COMPARE_OP >\n"); break;
|
|
|
|
case MP_BINARY_OP_EQUAL: printf("COMPARE_OP ==\n"); break;
|
|
|
|
case MP_BINARY_OP_LESS_EQUAL: printf("COMPARE_OP <=\n"); break;
|
|
|
|
case MP_BINARY_OP_MORE_EQUAL: printf("COMPARE_OP >=\n"); break;
|
|
|
|
case MP_BINARY_OP_NOT_EQUAL: printf("COMPARE_OP !=\n"); break;
|
|
|
|
case MP_BINARY_OP_IN: printf("COMPARE_OP in\n"); break;
|
|
|
|
case MP_BINARY_OP_IS: printf("COMPARE_OP is\n"); break;
|
|
|
|
case MP_BINARY_OP_EXCEPTION_MATCH: printf("COMPARE_OP exception match\n"); break;
|
|
|
|
case MP_BINARY_OP_NOT_IN: printf("COMPARE_OP not in\n"); break;
|
|
|
|
case MP_BINARY_OP_IS_NOT: printf("COMPARE_OP is not\n"); break;
|
2013-10-04 14:53:11 -04:00
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_build_tuple(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1 - n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("BUILD_TUPLE " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_build_list(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1 - n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("BUILD_LIST " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_list_append(emit_t *emit, mp_uint_t list_index) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LIST_APPEND " UINT_FMT "\n", list_index);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_build_map(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("BUILD_MAP " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_store_map(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -2, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("STORE_MAP\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_map_add(emit_t *emit, mp_uint_t map_index) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -2, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("MAP_ADD " UINT_FMT "\n", map_index);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_build_set(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1 - n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("BUILD_SET " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_set_add(emit_t *emit, mp_uint_t set_index) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("SET_ADD " UINT_FMT "\n", set_index);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_build_slice(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1 - n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("BUILD_SLICE " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1 + n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("UNPACK_SEQUENCE " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1 + n_left + n_right + 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("UNPACK_EX " UINT_FMT "\n", n_left | (n_right << 8));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
|
|
|
|
mp_int_t s = 0;
|
2014-04-09 07:43:17 -04:00
|
|
|
if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
|
2013-10-04 14:53:11 -04:00
|
|
|
s += 1;
|
|
|
|
}
|
2014-04-09 07:43:17 -04:00
|
|
|
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
|
2013-10-04 14:53:11 -04:00
|
|
|
s += 1;
|
|
|
|
}
|
2014-09-08 18:05:16 -04:00
|
|
|
emit_pre(emit, -(mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - s, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-04-09 07:43:17 -04:00
|
|
|
if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
|
|
|
|
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("CALL_FUNCTION_VAR_KW");
|
|
|
|
} else {
|
|
|
|
printf("CALL_FUNCTION_VAR");
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-09 07:43:17 -04:00
|
|
|
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("CALL_FUNCTION_KW");
|
|
|
|
} else {
|
|
|
|
printf("CALL_FUNCTION");
|
|
|
|
}
|
|
|
|
}
|
2014-09-08 18:05:16 -04:00
|
|
|
printf(" " UINT_FMT ", " UINT_FMT "\n", n_positional, n_keyword);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
|
2014-04-09 07:43:17 -04:00
|
|
|
emit_cpy_call_function(emit, n_positional, n_keyword, star_flags);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_return_value(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 1);
|
|
|
|
emit->last_emit_was_return_value = true;
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("RETURN_VALUE\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_raise_varargs(emit_t *emit, mp_uint_t n_args) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -n_args, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("RAISE_VARARGS " UINT_FMT "\n", n_args);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qst) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_CONST code %s\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
// load qualified name
|
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("LOAD_CONST '");
|
|
|
|
// code just to work out the qualname (or whatever it is)
|
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
for (scope_t *s = emit->scope; s->parent != NULL; s = s->parent) {
|
|
|
|
depth += 1;
|
|
|
|
}
|
|
|
|
for (int wanted_depth = depth; wanted_depth >= 0; wanted_depth--) {
|
|
|
|
scope_t *s = emit->scope;
|
|
|
|
for (int i = 0; i < wanted_depth; i++) {
|
|
|
|
s = s->parent;
|
|
|
|
}
|
|
|
|
if (s->kind == SCOPE_FUNCTION) {
|
|
|
|
printf("%s.<locals>.", qstr_str(s->simple_name));
|
|
|
|
} else if (s->kind == SCOPE_CLASS) {
|
|
|
|
printf("%s.", qstr_str(s->simple_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("%s'\n", qstr_str(qst));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
|
2013-10-05 07:19:06 -04:00
|
|
|
load_cpy_const_code_and_name(emit, scope->simple_name);
|
2014-03-31 06:30:17 -04:00
|
|
|
emit_pre(emit, -1 - n_pos_defaults - 2 * n_kw_defaults, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("MAKE_FUNCTION " UINT_FMT "\n", (n_kw_defaults << 8) | n_pos_defaults);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
|
2014-04-20 12:50:40 -04:00
|
|
|
emit_cpy_build_tuple(emit, n_closed_over);
|
2013-10-05 07:19:06 -04:00
|
|
|
load_cpy_const_code_and_name(emit, scope->simple_name);
|
2014-03-31 06:30:17 -04:00
|
|
|
emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("MAKE_CLOSURE " UINT_FMT "\n", (n_kw_defaults << 8) | n_pos_defaults);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_yield_value(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, 0, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
|
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("YIELD_VALUE\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_cpy_yield_from(emit_t *emit) {
|
2013-10-04 14:53:11 -04:00
|
|
|
emit_pre(emit, -1, 1);
|
2014-05-07 12:24:22 -04:00
|
|
|
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
|
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2013-10-04 14:53:11 -04:00
|
|
|
printf("YIELD_FROM\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 00:17:25 -04:00
|
|
|
STATIC void emit_cpy_start_except_handler(emit_t *emit) {
|
|
|
|
emit_cpy_adjust_stack_size(emit, 3); // stack adjust for the 3 exception items
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void emit_cpy_end_except_handler(emit_t *emit) {
|
2015-05-06 11:46:21 -04:00
|
|
|
emit_cpy_adjust_stack_size(emit, -2); // stack adjust
|
2014-06-30 00:17:25 -04:00
|
|
|
}
|
|
|
|
|
2015-01-28 18:43:01 -05:00
|
|
|
STATIC void emit_cpy_load_const_verbatim_strn(emit_t *emit, const char *str, mp_uint_t len) {
|
2014-04-20 13:02:27 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2015-01-28 18:43:01 -05:00
|
|
|
printf("LOAD_CONST %.*s\n", (int)len, str);
|
2014-04-20 13:02:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_load_closure(emit_t *emit, qstr qst, mp_uint_t local_num) {
|
2014-04-20 13:02:27 -04:00
|
|
|
emit_pre(emit, 1, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("LOAD_CLOSURE " UINT_FMT " %s\n", local_num, qstr_str(qst));
|
2014-04-20 13:02:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_cpy_setup_loop(emit_t *emit, mp_uint_t label) {
|
2014-04-20 13:02:27 -04:00
|
|
|
emit_pre(emit, 0, 3);
|
2014-05-07 12:24:22 -04:00
|
|
|
if (emit->pass == MP_PASS_EMIT) {
|
2014-09-08 18:05:16 -04:00
|
|
|
printf("SETUP_LOOP " UINT_FMT "\n", emit->label_offsets[label]);
|
2014-04-20 13:02:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 13:08:26 -04:00
|
|
|
const emit_method_table_t emit_cpython_method_table = {
|
2014-08-15 11:45:41 -04:00
|
|
|
emit_cpy_set_native_type,
|
2013-10-05 07:19:06 -04:00
|
|
|
emit_cpy_start_pass,
|
|
|
|
emit_cpy_end_pass,
|
|
|
|
emit_cpy_last_emit_was_return_value,
|
2014-04-10 13:28:54 -04:00
|
|
|
emit_cpy_adjust_stack_size,
|
2014-01-18 18:24:36 -05:00
|
|
|
emit_cpy_set_source_line,
|
2013-10-05 07:19:06 -04:00
|
|
|
|
2015-03-26 10:42:40 -04:00
|
|
|
{
|
|
|
|
emit_cpy_load_fast,
|
|
|
|
emit_cpy_load_deref,
|
|
|
|
emit_cpy_load_name,
|
|
|
|
emit_cpy_load_global,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emit_cpy_store_fast,
|
|
|
|
emit_cpy_store_deref,
|
|
|
|
emit_cpy_store_name,
|
|
|
|
emit_cpy_store_global,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emit_cpy_delete_fast,
|
|
|
|
emit_cpy_delete_deref,
|
|
|
|
emit_cpy_delete_name,
|
|
|
|
emit_cpy_delete_global,
|
|
|
|
},
|
2013-10-05 09:17:09 -04:00
|
|
|
|
2013-10-05 07:19:06 -04:00
|
|
|
emit_cpy_label_assign,
|
|
|
|
emit_cpy_import_name,
|
|
|
|
emit_cpy_import_from,
|
|
|
|
emit_cpy_import_star,
|
|
|
|
emit_cpy_load_const_tok,
|
|
|
|
emit_cpy_load_const_small_int,
|
|
|
|
emit_cpy_load_const_str,
|
2015-01-13 10:55:54 -05:00
|
|
|
emit_cpy_load_const_obj,
|
2014-04-20 12:50:40 -04:00
|
|
|
emit_cpy_load_null,
|
2013-10-05 07:19:06 -04:00
|
|
|
emit_cpy_load_attr,
|
|
|
|
emit_cpy_load_method,
|
|
|
|
emit_cpy_load_build_class,
|
2014-04-17 17:10:53 -04:00
|
|
|
emit_cpy_load_subscr,
|
2013-10-05 07:19:06 -04:00
|
|
|
emit_cpy_store_attr,
|
|
|
|
emit_cpy_store_subscr,
|
|
|
|
emit_cpy_delete_attr,
|
|
|
|
emit_cpy_delete_subscr,
|
|
|
|
emit_cpy_dup_top,
|
|
|
|
emit_cpy_dup_top_two,
|
|
|
|
emit_cpy_pop_top,
|
|
|
|
emit_cpy_rot_two,
|
|
|
|
emit_cpy_rot_three,
|
|
|
|
emit_cpy_jump,
|
2015-02-28 10:04:06 -05:00
|
|
|
emit_cpy_pop_jump_if,
|
|
|
|
emit_cpy_jump_if_or_pop,
|
2013-10-05 07:19:06 -04:00
|
|
|
emit_cpy_break_loop,
|
|
|
|
emit_cpy_continue_loop,
|
|
|
|
emit_cpy_setup_with,
|
|
|
|
emit_cpy_with_cleanup,
|
|
|
|
emit_cpy_setup_except,
|
|
|
|
emit_cpy_setup_finally,
|
|
|
|
emit_cpy_end_finally,
|
|
|
|
emit_cpy_get_iter,
|
|
|
|
emit_cpy_for_iter,
|
|
|
|
emit_cpy_for_iter_end,
|
|
|
|
emit_cpy_pop_block,
|
|
|
|
emit_cpy_pop_except,
|
|
|
|
emit_cpy_unary_op,
|
|
|
|
emit_cpy_binary_op,
|
|
|
|
emit_cpy_build_tuple,
|
|
|
|
emit_cpy_build_list,
|
|
|
|
emit_cpy_list_append,
|
|
|
|
emit_cpy_build_map,
|
|
|
|
emit_cpy_store_map,
|
|
|
|
emit_cpy_map_add,
|
|
|
|
emit_cpy_build_set,
|
|
|
|
emit_cpy_set_add,
|
|
|
|
emit_cpy_build_slice,
|
|
|
|
emit_cpy_unpack_sequence,
|
|
|
|
emit_cpy_unpack_ex,
|
|
|
|
emit_cpy_make_function,
|
|
|
|
emit_cpy_make_closure,
|
|
|
|
emit_cpy_call_function,
|
|
|
|
emit_cpy_call_method,
|
|
|
|
emit_cpy_return_value,
|
|
|
|
emit_cpy_raise_varargs,
|
|
|
|
emit_cpy_yield_value,
|
|
|
|
emit_cpy_yield_from,
|
2014-04-20 13:02:27 -04:00
|
|
|
|
2014-06-30 00:17:25 -04:00
|
|
|
emit_cpy_start_except_handler,
|
|
|
|
emit_cpy_end_except_handler,
|
|
|
|
|
2014-04-20 13:02:27 -04:00
|
|
|
// emitcpy specific functions
|
2015-01-28 18:43:01 -05:00
|
|
|
emit_cpy_load_const_verbatim_strn,
|
2014-04-20 13:02:27 -04:00
|
|
|
emit_cpy_load_closure,
|
|
|
|
emit_cpy_setup_loop,
|
2013-10-05 07:19:06 -04:00
|
|
|
};
|
|
|
|
|
2013-10-12 11:53:13 -04:00
|
|
|
#endif // MICROPY_EMIT_CPYTHON
|