2013-10-05 23:18:38 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-10-05 23:18:38 +01:00
|
|
|
#include "lexer.h"
|
|
|
|
#include "parse.h"
|
2014-04-13 11:04:33 +01:00
|
|
|
#include "obj.h"
|
|
|
|
#include "emitglue.h"
|
2013-10-05 23:18:38 +01:00
|
|
|
#include "scope.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "runtime0.h"
|
2013-10-05 23:18:38 +01:00
|
|
|
#include "emit.h"
|
|
|
|
|
|
|
|
struct _emit_t {
|
|
|
|
scope_t *scope;
|
|
|
|
};
|
|
|
|
|
2014-02-05 00:51:47 +00:00
|
|
|
emit_t *emit_pass1_new(void) {
|
2013-10-05 23:18:38 +01:00
|
|
|
emit_t *emit = m_new(emit_t, 1);
|
|
|
|
return emit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emit_pass1_free(emit_t *emit) {
|
2013-12-29 19:33:23 +00:00
|
|
|
m_del_obj(emit_t, emit);
|
2013-10-05 23:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_dummy(emit_t *emit) {
|
2013-10-05 23:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
2013-10-05 23:18:38 +01:00
|
|
|
assert(pass == PASS_1);
|
|
|
|
emit->scope = scope;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_end_pass(emit_t *emit) {
|
2013-10-05 23:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 16:40:38 +01:00
|
|
|
STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_load_id(emit_t *emit, qstr qstr) {
|
2013-10-05 23:18:38 +01:00
|
|
|
// name adding/lookup
|
|
|
|
bool added;
|
|
|
|
id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
|
|
|
|
if (added) {
|
2014-02-05 00:51:47 +00:00
|
|
|
#if MICROPY_EMIT_CPYTHON
|
|
|
|
if (qstr == MP_QSTR_super && emit->scope->kind == SCOPE_FUNCTION) {
|
2013-10-05 23:18:38 +01:00
|
|
|
// special case, super is a global, and also counts as use of __class__
|
|
|
|
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
|
2014-02-05 00:51:47 +00:00
|
|
|
id_info_t *id2 = scope_find_local_in_parent(emit->scope, MP_QSTR___class__);
|
2013-10-05 23:18:38 +01:00
|
|
|
if (id2 != NULL) {
|
2014-02-05 00:51:47 +00:00
|
|
|
id2 = scope_find_or_add_id(emit->scope, MP_QSTR___class__, &added);
|
2013-10-05 23:18:38 +01:00
|
|
|
if (added) {
|
|
|
|
id2->kind = ID_INFO_KIND_FREE;
|
2014-02-05 00:51:47 +00:00
|
|
|
scope_close_over_in_parents(emit->scope, MP_QSTR___class__);
|
2013-10-05 23:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 00:51:47 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-10-05 23:18:38 +01:00
|
|
|
id_info_t *id2 = scope_find_local_in_parent(emit->scope, qstr);
|
|
|
|
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;
|
|
|
|
scope_close_over_in_parents(emit->scope, qstr);
|
|
|
|
} else {
|
|
|
|
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC id_info_t *get_id_for_modification(scope_t *scope, qstr qstr) {
|
2013-10-05 23:18:38 +01:00
|
|
|
// name adding/lookup
|
|
|
|
bool added;
|
|
|
|
id_info_t *id = scope_find_or_add_id(scope, qstr, &added);
|
|
|
|
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-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_store_id(emit_t *emit, qstr qstr) {
|
2013-10-05 23:18:38 +01:00
|
|
|
get_id_for_modification(emit->scope, qstr);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC void emit_pass1_delete_id(emit_t *emit, qstr qstr) {
|
2014-04-09 15:26:46 +01:00
|
|
|
id_info_t *id = get_id_for_modification(emit->scope, qstr);
|
2014-04-12 18:20:40 +01:00
|
|
|
// this flag is unused
|
|
|
|
//id->flags |= ID_FLAG_IS_DELETED;
|
|
|
|
(void)id; // suppress compiler warning
|
2013-10-05 23:18:38 +01: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 16:40:38 +01:00
|
|
|
emit_pass1_last_emit_was_return_value,
|
2013-10-05 23:18:38 +01:00
|
|
|
(void*)emit_pass1_dummy,
|
2014-01-18 23:24:36 +00:00
|
|
|
(void*)emit_pass1_dummy,
|
2013-10-05 23:18:38 +01: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,
|
|
|
|
(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,
|
|
|
|
(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,
|
|
|
|
};
|