Skip to content

Commit 3dea8c9

Browse files
committed
py/scope: Use lookup-table to determine a scope's simple name.
Generates slightly smaller and more efficient code.
1 parent 6ab2c5e commit 3dea8c9

4 files changed

Lines changed: 33 additions & 32 deletions

File tree

py/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3288,7 +3288,7 @@ STATIC void scope_compute_things(scope_t *scope) {
32883288
// __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
32893289
continue;
32903290
}
3291-
if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3291+
if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
32923292
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
32933293
}
32943294
// params always count for 1 local, even if they are a cell

py/emitcommon.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
5050
bool added;
5151
id_info_t *id = scope_find_or_add_id(scope, qst, &added);
5252
if (added) {
53-
if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
54-
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
55-
} else {
53+
if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
5654
id->kind = ID_INFO_KIND_LOCAL;
55+
} else {
56+
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
5757
}
58-
} else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
58+
} else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
5959
// rebind as a local variable
6060
id->kind = ID_INFO_KIND_LOCAL;
6161
}

py/scope.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,26 @@
3030

3131
#if MICROPY_ENABLE_COMPILER
3232

33+
// these low numbered qstrs should fit in 8 bits
34+
STATIC const uint8_t scope_simple_name_table[] = {
35+
[SCOPE_MODULE] = MP_QSTR__lt_module_gt_,
36+
[SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_,
37+
[SCOPE_LIST_COMP] = MP_QSTR__lt_listcomp_gt_,
38+
[SCOPE_DICT_COMP] = MP_QSTR__lt_dictcomp_gt_,
39+
[SCOPE_SET_COMP] = MP_QSTR__lt_setcomp_gt_,
40+
[SCOPE_GEN_EXPR] = MP_QSTR__lt_genexpr_gt_,
41+
};
42+
3343
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
3444
scope_t *scope = m_new0(scope_t, 1);
3545
scope->kind = kind;
3646
scope->pn = pn;
3747
scope->source_file = source_file;
38-
switch (kind) {
39-
case SCOPE_MODULE:
40-
scope->simple_name = MP_QSTR__lt_module_gt_;
41-
break;
42-
case SCOPE_FUNCTION:
43-
case SCOPE_CLASS:
44-
assert(MP_PARSE_NODE_IS_STRUCT(pn));
45-
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
46-
break;
47-
case SCOPE_LAMBDA:
48-
scope->simple_name = MP_QSTR__lt_lambda_gt_;
49-
break;
50-
case SCOPE_LIST_COMP:
51-
scope->simple_name = MP_QSTR__lt_listcomp_gt_;
52-
break;
53-
case SCOPE_DICT_COMP:
54-
scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
55-
break;
56-
case SCOPE_SET_COMP:
57-
scope->simple_name = MP_QSTR__lt_setcomp_gt_;
58-
break;
59-
case SCOPE_GEN_EXPR:
60-
scope->simple_name = MP_QSTR__lt_genexpr_gt_;
61-
break;
62-
default:
63-
assert(0);
48+
if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) {
49+
assert(MP_PARSE_NODE_IS_STRUCT(pn));
50+
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
51+
} else {
52+
scope->simple_name = scope_simple_name_table[kind];
6453
}
6554
scope->raw_code = mp_emit_glue_new_raw_code();
6655
scope->emit_options = emit_options;

py/scope.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,20 @@ typedef struct _id_info_t {
5252
qstr qst;
5353
} id_info_t;
5454

55+
#define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA)
56+
5557
// scope is a "block" in Python parlance
56-
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;
58+
typedef enum {
59+
SCOPE_MODULE,
60+
SCOPE_CLASS,
61+
SCOPE_LAMBDA,
62+
SCOPE_LIST_COMP,
63+
SCOPE_DICT_COMP,
64+
SCOPE_SET_COMP,
65+
SCOPE_GEN_EXPR,
66+
SCOPE_FUNCTION,
67+
} scope_kind_t;
68+
5769
typedef struct _scope_t {
5870
scope_kind_t kind;
5971
struct _scope_t *parent;

0 commit comments

Comments
 (0)