summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-09-30 12:34:05 +1000
committerDamien George <damien.p.george@gmail.com>2016-09-30 12:34:05 +1000
commit3dea8c9e926d0c1b1049ff9d5abeedfb001fc201 (patch)
treef99592adff2bf9563e214e0f985d83e3d1226a61
parent6ab2c5e6cc6359c3419a9d8ee61b4e586864d048 (diff)
downloadmicropython-3dea8c9e926d0c1b1049ff9d5abeedfb001fc201.tar.gz
micropython-3dea8c9e926d0c1b1049ff9d5abeedfb001fc201.zip
py/scope: Use lookup-table to determine a scope's simple name.
Generates slightly smaller and more efficient code.
-rw-r--r--py/compile.c2
-rw-r--r--py/emitcommon.c8
-rw-r--r--py/scope.c41
-rw-r--r--py/scope.h14
4 files changed, 33 insertions, 32 deletions
diff --git a/py/compile.c b/py/compile.c
index 3039f98b5d..2253ccd3f6 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -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
diff --git a/py/emitcommon.c b/py/emitcommon.c
index 435188f366..2925f4cafe 100644
--- a/py/emitcommon.c
+++ b/py/emitcommon.c
@@ -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;
}
diff --git a/py/scope.c b/py/scope.c
index 632e527521..d080fbcbfb 100644
--- a/py/scope.c
+++ b/py/scope.c
@@ -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;
diff --git a/py/scope.h b/py/scope.h
index fac936a729..23a863ac52 100644
--- a/py/scope.h
+++ b/py/scope.h
@@ -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;