py, compiler: Turn id_info_t.param into a set of flags.

So we can add more flags.
This commit is contained in:
Damien George 2014-04-09 14:42:51 +01:00
parent b140bff877
commit 11d8cd54c9
3 changed files with 13 additions and 9 deletions

View File

@ -2208,7 +2208,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
// get first argument to function // get first argument to function
bool found = false; bool found = false;
for (int i = 0; i < comp->scope_cur->id_info_len; i++) { for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
if (comp->scope_cur->id_info[i].param) { if (comp->scope_cur->id_info[i].flags && ID_FLAG_IS_PARAM) {
EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num); EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
found = true; found = true;
break; break;
@ -2761,8 +2761,8 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
compile_syntax_error(comp, pn, "same name used for parameter"); compile_syntax_error(comp, pn, "same name used for parameter");
return; return;
} }
id_info->param = true;
id_info->kind = ID_INFO_KIND_LOCAL; id_info->kind = ID_INFO_KIND_LOCAL;
id_info->flags |= ID_FLAG_IS_PARAM;
} }
} }
@ -3097,7 +3097,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
} }
// note: params always count for 1 local, even if they are a cell // note: params always count for 1 local, even if they are a cell
if (id->param || id->kind == ID_INFO_KIND_LOCAL) { if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
id->local_num = scope->num_locals; id->local_num = scope->num_locals;
scope->num_locals += 1; scope->num_locals += 1;
} }
@ -3119,7 +3119,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
// in Micro Python the cells come right after the fast locals // in Micro Python the cells come right after the fast locals
// parameters are not counted here, since they remain at the start // parameters are not counted here, since they remain at the start
// of the locals, even if they are cell vars // of the locals, even if they are cell vars
if (!id->param && id->kind == ID_INFO_KIND_CELL) { if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
id->local_num = scope->num_locals; id->local_num = scope->num_locals;
scope->num_locals += 1; scope->num_locals += 1;
} }
@ -3136,7 +3136,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
for (int j = 0; j < scope->id_info_len; j++) { for (int j = 0; j < scope->id_info_len; j++) {
id_info_t *id2 = &scope->id_info[j]; id_info_t *id2 = &scope->id_info[j];
if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) { if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
assert(!id2->param); // free vars should not be params assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
#if MICROPY_EMIT_CPYTHON #if MICROPY_EMIT_CPYTHON
// in CPython the frees are numbered after the cells // in CPython the frees are numbered after the cells
id2->local_num = num_cell + num_free; id2->local_num = num_cell + num_free;
@ -3154,7 +3154,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
if (num_free > 0) { if (num_free > 0) {
for (int i = 0; i < scope->id_info_len; i++) { for (int i = 0; i < scope->id_info_len; i++) {
id_info_t *id = &scope->id_info[i]; id_info_t *id = &scope->id_info[i];
if (id->param || id->kind != ID_INFO_KIND_FREE) { if (id->kind != ID_INFO_KIND_FREE || (id->flags && ID_FLAG_IS_PARAM)) {
id->local_num += num_free; id->local_num += num_free;
} }
} }

View File

@ -73,10 +73,10 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
// handled by the compiler because it adds arguments before compiling the body // handled by the compiler because it adds arguments before compiling the body
id_info_t *id_info = &scope->id_info[scope->id_info_len++]; id_info_t *id_info = &scope->id_info[scope->id_info_len++];
id_info->param = false;
id_info->kind = 0; id_info->kind = 0;
id_info->qstr = qstr; id_info->flags = 0;
id_info->local_num = 0; id_info->local_num = 0;
id_info->qstr = qstr;
*added = true; *added = true;
return id_info; return id_info;
} }

View File

@ -6,9 +6,13 @@ enum {
ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f
}; };
enum {
ID_FLAG_IS_PARAM = 0x01,
};
typedef struct _id_info_t { typedef struct _id_info_t {
uint8_t param;
uint8_t kind; uint8_t kind;
uint8_t flags;
// when it's an ID_INFO_KIND_LOCAL this is the unique number of the local // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
// whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable // whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
uint16_t local_num; uint16_t local_num;