py/scope: Use lookup-table to determine a scope's simple name.

Generates slightly smaller and more efficient code.
This commit is contained in:
Damien George 2016-09-30 12:34:05 +10:00
parent 6ab2c5e6cc
commit 3dea8c9e92
4 changed files with 33 additions and 32 deletions

View File

@ -3288,7 +3288,7 @@ STATIC void scope_compute_things(scope_t *scope) {
// __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
continue;
}
if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
}
// params always count for 1 local, even if they are a cell

View File

@ -50,12 +50,12 @@ void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
bool added;
id_info_t *id = scope_find_or_add_id(scope, qst, &added);
if (added) {
if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
} else {
if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
id->kind = ID_INFO_KIND_LOCAL;
} else {
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
}
} else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
} else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
// rebind as a local variable
id->kind = ID_INFO_KIND_LOCAL;
}

View File

@ -30,37 +30,26 @@
#if MICROPY_ENABLE_COMPILER
// these low numbered qstrs should fit in 8 bits
STATIC const uint8_t scope_simple_name_table[] = {
[SCOPE_MODULE] = MP_QSTR__lt_module_gt_,
[SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_,
[SCOPE_LIST_COMP] = MP_QSTR__lt_listcomp_gt_,
[SCOPE_DICT_COMP] = MP_QSTR__lt_dictcomp_gt_,
[SCOPE_SET_COMP] = MP_QSTR__lt_setcomp_gt_,
[SCOPE_GEN_EXPR] = MP_QSTR__lt_genexpr_gt_,
};
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
scope_t *scope = m_new0(scope_t, 1);
scope->kind = kind;
scope->pn = pn;
scope->source_file = source_file;
switch (kind) {
case SCOPE_MODULE:
scope->simple_name = MP_QSTR__lt_module_gt_;
break;
case SCOPE_FUNCTION:
case SCOPE_CLASS:
assert(MP_PARSE_NODE_IS_STRUCT(pn));
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
break;
case SCOPE_LAMBDA:
scope->simple_name = MP_QSTR__lt_lambda_gt_;
break;
case SCOPE_LIST_COMP:
scope->simple_name = MP_QSTR__lt_listcomp_gt_;
break;
case SCOPE_DICT_COMP:
scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
break;
case SCOPE_SET_COMP:
scope->simple_name = MP_QSTR__lt_setcomp_gt_;
break;
case SCOPE_GEN_EXPR:
scope->simple_name = MP_QSTR__lt_genexpr_gt_;
break;
default:
assert(0);
if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) {
assert(MP_PARSE_NODE_IS_STRUCT(pn));
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
} else {
scope->simple_name = scope_simple_name_table[kind];
}
scope->raw_code = mp_emit_glue_new_raw_code();
scope->emit_options = emit_options;

View File

@ -52,8 +52,20 @@ typedef struct _id_info_t {
qstr qst;
} id_info_t;
#define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA)
// scope is a "block" in Python parlance
typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOPE_DICT_COMP, SCOPE_SET_COMP, SCOPE_GEN_EXPR, SCOPE_CLASS } scope_kind_t;
typedef enum {
SCOPE_MODULE,
SCOPE_CLASS,
SCOPE_LAMBDA,
SCOPE_LIST_COMP,
SCOPE_DICT_COMP,
SCOPE_SET_COMP,
SCOPE_GEN_EXPR,
SCOPE_FUNCTION,
} scope_kind_t;
typedef struct _scope_t {
scope_kind_t kind;
struct _scope_t *parent;