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.
|
|
|
|
*/
|
|
|
|
|
2013-10-05 18:18:38 -04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/emit.h"
|
2013-10-05 18:18:38 -04:00
|
|
|
|
|
|
|
struct _emit_t {
|
|
|
|
scope_t *scope;
|
|
|
|
};
|
|
|
|
|
2014-02-04 19:51:47 -05:00
|
|
|
emit_t *emit_pass1_new(void) {
|
2013-10-05 18:18:38 -04:00
|
|
|
emit_t *emit = m_new(emit_t, 1);
|
|
|
|
return emit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emit_pass1_free(emit_t *emit) {
|
2013-12-29 14:33:23 -05:00
|
|
|
m_del_obj(emit_t, emit);
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_pass1_dummy(emit_t *emit) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)emit;
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_pass1_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
2014-05-07 12:24:22 -04:00
|
|
|
assert(pass == MP_PASS_SCOPE);
|
2013-10-05 18:18:38 -04:00
|
|
|
emit->scope = scope;
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC void emit_pass1_end_pass(emit_t *emit) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)emit;
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
|
2014-04-10 11:40:38 -04:00
|
|
|
STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)emit;
|
2014-04-10 11:40:38 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_pass1_load_id(emit_t *emit, qstr qst) {
|
2013-10-05 18:18:38 -04:00
|
|
|
// name adding/lookup
|
|
|
|
bool added;
|
2014-09-08 18:05:16 -04:00
|
|
|
id_info_t *id = scope_find_or_add_id(emit->scope, qst, &added);
|
2013-10-05 18:18:38 -04:00
|
|
|
if (added) {
|
2014-02-04 19:51:47 -05:00
|
|
|
#if MICROPY_EMIT_CPYTHON
|
2014-09-08 18:05:16 -04:00
|
|
|
if (qst == MP_QSTR_super && emit->scope->kind == SCOPE_FUNCTION) {
|
2013-10-05 18:18:38 -04:00
|
|
|
// special case, super is a global, and also counts as use of __class__
|
|
|
|
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
|
2014-02-04 19:51:47 -05:00
|
|
|
id_info_t *id2 = scope_find_local_in_parent(emit->scope, MP_QSTR___class__);
|
2013-10-05 18:18:38 -04:00
|
|
|
if (id2 != NULL) {
|
2014-02-04 19:51:47 -05:00
|
|
|
id2 = scope_find_or_add_id(emit->scope, MP_QSTR___class__, &added);
|
2013-10-05 18:18:38 -04:00
|
|
|
if (added) {
|
|
|
|
id2->kind = ID_INFO_KIND_FREE;
|
2014-02-04 19:51:47 -05:00
|
|
|
scope_close_over_in_parents(emit->scope, MP_QSTR___class__);
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 19:51:47 -05:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2014-09-08 18:05:16 -04:00
|
|
|
id_info_t *id2 = scope_find_local_in_parent(emit->scope, qst);
|
2013-10-05 18:18:38 -04:00
|
|
|
if (id2 != NULL && (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE)) {
|
|
|
|
id->kind = ID_INFO_KIND_FREE;
|
2014-09-08 18:05:16 -04:00
|
|
|
scope_close_over_in_parents(emit->scope, qst);
|
2013-10-05 18:18:38 -04:00
|
|
|
} else {
|
|
|
|
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC id_info_t *get_id_for_modification(scope_t *scope, qstr qst) {
|
2013-10-05 18:18:38 -04:00
|
|
|
// name adding/lookup
|
|
|
|
bool added;
|
2014-09-08 18:05:16 -04:00
|
|
|
id_info_t *id = scope_find_or_add_id(scope, qst, &added);
|
2013-10-05 18:18:38 -04:00
|
|
|
if (added) {
|
|
|
|
if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
|
|
|
|
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
|
|
|
|
} else {
|
|
|
|
id->kind = ID_INFO_KIND_LOCAL;
|
|
|
|
}
|
|
|
|
} else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
|
|
|
|
// rebind as a local variable
|
|
|
|
id->kind = ID_INFO_KIND_LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(id != NULL); // TODO can this ever fail?
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_pass1_store_id(emit_t *emit, qstr qst) {
|
|
|
|
get_id_for_modification(emit->scope, qst);
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 18:05:16 -04:00
|
|
|
STATIC void emit_pass1_delete_id(emit_t *emit, qstr qst) {
|
|
|
|
id_info_t *id = get_id_for_modification(emit->scope, qst);
|
2014-04-12 13:20:40 -04:00
|
|
|
// this flag is unused
|
|
|
|
//id->flags |= ID_FLAG_IS_DELETED;
|
|
|
|
(void)id; // suppress compiler warning
|
2013-10-05 18:18:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const emit_method_table_t emit_pass1_method_table = {
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
emit_pass1_start_pass,
|
|
|
|
emit_pass1_end_pass,
|
2014-04-10 11:40:38 -04:00
|
|
|
emit_pass1_last_emit_was_return_value,
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
2014-01-18 18:24:36 -05:00
|
|
|
(void*)emit_pass1_dummy,
|
2013-10-05 18:18:38 -04:00
|
|
|
|
|
|
|
emit_pass1_load_id,
|
|
|
|
emit_pass1_store_id,
|
|
|
|
emit_pass1_delete_id,
|
|
|
|
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2014-04-17 17:10:53 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
2014-04-20 12:50:40 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2015-01-13 10:55:54 -05:00
|
|
|
(void*)emit_pass1_dummy,
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2014-12-27 12:07:16 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_SET
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2014-12-27 12:07:16 -05:00
|
|
|
#endif
|
2014-12-27 12:20:41 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_SLICE
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
2014-12-27 12:20:41 -05:00
|
|
|
#endif
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2014-06-30 00:17:25 -04:00
|
|
|
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
|
2014-04-20 13:02:27 -04:00
|
|
|
#if MICROPY_EMIT_CPYTHON
|
2013-10-05 18:18:38 -04:00
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
|
|
|
(void*)emit_pass1_dummy,
|
2014-04-20 13:02:27 -04:00
|
|
|
#endif
|
2013-10-05 18:18:38 -04:00
|
|
|
};
|