Skip to content

Commit 8725f8f

Browse files
committed
py: Pass all scope flags through to runtime.
1 parent c596612 commit 8725f8f

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

py/compile.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,11 +2595,11 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
25952595
//assert(comp->scope_cur->num_dict_params == 0);
25962596
} else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
25972597
// named star
2598-
comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2598+
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
25992599
param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
26002600
} else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
26012601
// named star with annotation
2602-
comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2602+
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
26032603
pns = (mp_parse_node_struct_t*)pns->nodes[0];
26042604
param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
26052605
pn_annotation = pns->nodes[1];
@@ -2613,7 +2613,7 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
26132613
// this parameter has an annotation
26142614
pn_annotation = pns->nodes[1];
26152615
}
2616-
comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
2616+
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
26172617
} else {
26182618
// TODO anything to implement?
26192619
assert(0);
@@ -3032,19 +3032,19 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30323032
#endif
30333033
}
30343034

3035-
// compute flags
3036-
//scope->flags = 0; since we set some things in parameters
3035+
// compute scope_flags
3036+
//scope->scope_flags = 0; since we set some things in parameters
30373037
if (scope->kind != SCOPE_MODULE) {
3038-
scope->flags |= SCOPE_FLAG_NEWLOCALS;
3038+
scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
30393039
}
30403040
if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
30413041
assert(scope->parent != NULL);
3042-
scope->flags |= SCOPE_FLAG_OPTIMISED;
3042+
scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
30433043

30443044
// TODO possibly other ways it can be nested
30453045
// Note that we don't actually use this information at the moment (for CPython compat only)
30463046
if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
3047-
scope->flags |= SCOPE_FLAG_NESTED;
3047+
scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
30483048
}
30493049
}
30503050
int num_free = 0;
@@ -3055,7 +3055,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30553055
}
30563056
}
30573057
if (num_free == 0) {
3058-
scope->flags |= SCOPE_FLAG_NOFREE;
3058+
scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
30593059
}
30603060
}
30613061

py/emitbc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
242242
emit->code_base = m_new(byte, emit->code_info_size + emit->byte_code_size);
243243

244244
} else if (emit->pass == PASS_3) {
245-
rt_assign_byte_code(emit->scope->unique_code_id, emit->code_base, emit->code_info_size + emit->byte_code_size, emit->scope->num_params, emit->scope->num_locals, emit->scope->stack_size, (emit->scope->flags & SCOPE_FLAG_GENERATOR) != 0);
245+
rt_assign_byte_code(emit->scope->unique_code_id, emit->code_base, emit->code_info_size + emit->byte_code_size, emit->scope->num_params, emit->scope->num_locals, emit->scope->stack_size, emit->scope->scope_flags);
246246
}
247247
}
248248

@@ -779,15 +779,15 @@ STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
779779
STATIC void emit_bc_yield_value(emit_t *emit) {
780780
emit_pre(emit, 0);
781781
if (emit->pass == PASS_2) {
782-
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
782+
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
783783
}
784784
emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
785785
}
786786

787787
STATIC void emit_bc_yield_from(emit_t *emit) {
788788
emit_pre(emit, -1);
789789
if (emit->pass == PASS_2) {
790-
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
790+
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
791791
}
792792
emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
793793
}

py/runtime.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef enum {
4646
typedef struct _mp_code_t {
4747
struct {
4848
mp_code_kind_t kind : 8;
49-
bool is_generator : 1;
49+
uint scope_flags : 8;
5050
};
5151
struct {
5252
uint n_args : 16;
@@ -242,12 +242,12 @@ STATIC void alloc_unique_codes(void) {
242242
}
243243
}
244244

245-
void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) {
245+
void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, uint scope_flags) {
246246
alloc_unique_codes();
247247

248248
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
249249
unique_codes[unique_code_id].kind = MP_CODE_BYTE;
250-
unique_codes[unique_code_id].is_generator = is_generator;
250+
unique_codes[unique_code_id].scope_flags = scope_flags;
251251
unique_codes[unique_code_id].n_args = n_args;
252252
unique_codes[unique_code_id].n_state = n_locals + n_stack;
253253
unique_codes[unique_code_id].u_byte.code = code;
@@ -275,7 +275,7 @@ void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args)
275275

276276
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
277277
unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
278-
unique_codes[unique_code_id].is_generator = false;
278+
unique_codes[unique_code_id].scope_flags = 0;
279279
unique_codes[unique_code_id].n_args = n_args;
280280
unique_codes[unique_code_id].n_state = 0;
281281
unique_codes[unique_code_id].u_native.fun = fun;
@@ -307,7 +307,7 @@ void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_a
307307

308308
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
309309
unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
310-
unique_codes[unique_code_id].is_generator = false;
310+
unique_codes[unique_code_id].scope_flags = 0;
311311
unique_codes[unique_code_id].n_args = n_args;
312312
unique_codes[unique_code_id].n_state = 0;
313313
unique_codes[unique_code_id].u_inline_asm.fun = fun;
@@ -728,7 +728,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
728728
}
729729

730730
// check for generator functions and if so wrap in generator object
731-
if (c->is_generator) {
731+
if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
732732
fun = mp_obj_new_gen_wrap(fun);
733733
}
734734

py/runtime0.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// taken from python source, Include/code.h
2+
#define MP_SCOPE_FLAG_OPTIMISED 0x01
3+
#define MP_SCOPE_FLAG_NEWLOCALS 0x02
4+
#define MP_SCOPE_FLAG_VARARGS 0x04
5+
#define MP_SCOPE_FLAG_VARKEYWORDS 0x08
6+
#define MP_SCOPE_FLAG_NESTED 0x10
7+
#define MP_SCOPE_FLAG_GENERATOR 0x20
8+
/* The MP_SCOPE_FLAG_NOFREE flag is set if there are no free or cell variables.
9+
This information is redundant, but it allows a single flag test
10+
to determine whether there is any extra work to be done when the
11+
call frame is setup.
12+
*/
13+
#define MP_SCOPE_FLAG_NOFREE 0x40
14+
115
typedef enum {
216
RT_UNARY_OP_BOOL, // __bool__
317
RT_UNARY_OP_LEN, // __len__
@@ -83,6 +97,6 @@ extern void *const rt_fun_table[RT_F_NUMBER_OF];
8397
void rt_init(void);
8498
void rt_deinit(void);
8599
uint rt_get_unique_code_id(void);
86-
void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator);
100+
void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, uint scope_flags);
87101
void rt_assign_native_code(uint unique_code_id, void *f, uint len, int n_args);
88102
void rt_assign_inline_asm_code(uint unique_code_id, void *f, uint len, int n_args);

py/scope.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
4747
scope->id_info_len = 0;
4848
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
4949

50-
scope->flags = 0;
50+
scope->scope_flags = 0;
5151
scope->num_params = 0;
5252
/* not needed
5353
scope->num_default_params = 0;
@@ -244,7 +244,7 @@ void scope_print_info(scope_t *s) {
244244
}
245245
printf("\n");
246246
*/
247-
printf(" flags %04x\n", s->flags);
247+
printf(" flags %04x\n", s->scope_flags);
248248
printf(" argcount %d\n", s->num_params);
249249
printf(" nlocals %d\n", s->num_locals);
250250
printf(" stacksize %d\n", s->stack_size);

py/scope.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ typedef struct _id_info_t {
1717
int local_num;
1818
} id_info_t;
1919

20-
// taken from python source, Include/code.h
21-
#define SCOPE_FLAG_OPTIMISED 0x0001
22-
#define SCOPE_FLAG_NEWLOCALS 0x0002
23-
#define SCOPE_FLAG_VARARGS 0x0004
24-
#define SCOPE_FLAG_VARKEYWORDS 0x0008
25-
#define SCOPE_FLAG_NESTED 0x0010
26-
#define SCOPE_FLAG_GENERATOR 0x0020
27-
/* The SCOPE_FLAG_NOFREE flag is set if there are no free or cell variables.
28-
This information is redundant, but it allows a single flag test
29-
to determine whether there is any extra work to be done when the
30-
call frame is setup.
31-
*/
32-
#define SCOPE_FLAG_NOFREE 0x0040
33-
3420
// scope is a "block" in Python parlance
3521
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;
3622
typedef struct _scope_t {
@@ -43,7 +29,7 @@ typedef struct _scope_t {
4329
int id_info_alloc;
4430
int id_info_len;
4531
id_info_t *id_info;
46-
int flags;
32+
uint scope_flags; // see runtime0.h
4733
int num_params;
4834
/* not needed
4935
int num_default_params;

0 commit comments

Comments
 (0)