Skip to content

Commit e686c94

Browse files
committed
py/emit: Combine yield value and yield-from emit funcs into one.
Reduces code size by: bare-arm: -24 minimal x86: -72 unix x64: -200 unix nanbox: -72 stm32: -52 cc3200: -32 esp8266: -84 esp32: -24
1 parent 0a25fff commit e686c94

5 files changed

Lines changed: 19 additions & 28 deletions

File tree

py/compile.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
16991699
STATIC void compile_yield_from(compiler_t *comp) {
17001700
EMIT_ARG(get_iter, false);
17011701
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1702-
EMIT(yield_from);
1702+
EMIT_ARG(yield, MP_EMIT_YIELD_FROM);
17031703
}
17041704

17051705
#if MICROPY_PY_ASYNC_AWAIT
@@ -2621,14 +2621,14 @@ STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
26212621
}
26222622
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
26232623
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2624-
EMIT(yield_value);
2624+
EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
26252625
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
26262626
pns = (mp_parse_node_struct_t*)pns->nodes[0];
26272627
compile_node(comp, pns->nodes[0]);
26282628
compile_yield_from(comp);
26292629
} else {
26302630
compile_node(comp, pns->nodes[0]);
2631-
EMIT(yield_value);
2631+
EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
26322632
}
26332633
}
26342634

@@ -2862,7 +2862,7 @@ STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pn
28622862
// no more nested if/for; compile inner expression
28632863
compile_node(comp, pn_inner_expr);
28642864
if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
2865-
EMIT(yield_value);
2865+
EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
28662866
EMIT(pop_top);
28672867
} else {
28682868
EMIT_ARG(store_comp, comp->scope_cur->kind, 4 * for_depth + 5);

py/emit.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ typedef enum {
5959
#define MP_EMIT_IDOP_LOCAL_FAST (0)
6060
#define MP_EMIT_IDOP_LOCAL_DEREF (1)
6161

62+
// Kind for emit->yield()
63+
#define MP_EMIT_YIELD_VALUE (0)
64+
#define MP_EMIT_YIELD_FROM (1)
65+
6266
typedef struct _emit_t emit_t;
6367

6468
typedef struct _mp_emit_method_table_id_ops_t {
@@ -137,8 +141,7 @@ typedef struct _emit_method_table_t {
137141
void (*call_method)(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
138142
void (*return_value)(emit_t *emit);
139143
void (*raise_varargs)(emit_t *emit, mp_uint_t n_args);
140-
void (*yield_value)(emit_t *emit);
141-
void (*yield_from)(emit_t *emit);
144+
void (*yield)(emit_t *emit, int kind);
142145

143146
// these methods are used to control entry to/exit from an exception handler
144147
// they may or may not emit code
@@ -252,8 +255,7 @@ void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_
252255
void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
253256
void mp_emit_bc_return_value(emit_t *emit);
254257
void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args);
255-
void mp_emit_bc_yield_value(emit_t *emit);
256-
void mp_emit_bc_yield_from(emit_t *emit);
258+
void mp_emit_bc_yield(emit_t *emit, int kind);
257259
void mp_emit_bc_start_except_handler(emit_t *emit);
258260
void mp_emit_bc_end_except_handler(emit_t *emit);
259261

py/emitbc.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,11 @@ void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
931931
emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
932932
}
933933

934-
void mp_emit_bc_yield_value(emit_t *emit) {
935-
emit_bc_pre(emit, 0);
936-
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
937-
emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
938-
}
939-
940-
void mp_emit_bc_yield_from(emit_t *emit) {
941-
emit_bc_pre(emit, -1);
934+
void mp_emit_bc_yield(emit_t *emit, int kind) {
935+
MP_STATIC_ASSERT(MP_BC_YIELD_VALUE + 1 == MP_BC_YIELD_FROM);
936+
emit_bc_pre(emit, -kind);
942937
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
943-
emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
938+
emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE + kind);
944939
}
945940

946941
void mp_emit_bc_start_except_handler(emit_t *emit) {
@@ -1034,8 +1029,7 @@ const emit_method_table_t emit_bc_method_table = {
10341029
mp_emit_bc_call_method,
10351030
mp_emit_bc_return_value,
10361031
mp_emit_bc_raise_varargs,
1037-
mp_emit_bc_yield_value,
1038-
mp_emit_bc_yield_from,
1032+
mp_emit_bc_yield,
10391033

10401034
mp_emit_bc_start_except_handler,
10411035
mp_emit_bc_end_except_handler,

py/emitnative.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,16 +2170,12 @@ STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
21702170
emit_call(emit, MP_F_NATIVE_RAISE);
21712171
}
21722172

2173-
STATIC void emit_native_yield_value(emit_t *emit) {
2173+
STATIC void emit_native_yield(emit_t *emit, int kind) {
21742174
// not supported (for now)
21752175
(void)emit;
2176+
(void)kind;
21762177
mp_raise_NotImplementedError("native yield");
21772178
}
2178-
STATIC void emit_native_yield_from(emit_t *emit) {
2179-
// not supported (for now)
2180-
(void)emit;
2181-
mp_raise_NotImplementedError("native yield from");
2182-
}
21832179

21842180
STATIC void emit_native_start_except_handler(emit_t *emit) {
21852181
// This instruction follows an nlr_pop, so the stack counter is back to zero, when really
@@ -2278,8 +2274,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22782274
emit_native_call_method,
22792275
emit_native_return_value,
22802276
emit_native_raise_varargs,
2281-
emit_native_yield_value,
2282-
emit_native_yield_from,
2277+
emit_native_yield,
22832278

22842279
emit_native_start_except_handler,
22852280
emit_native_end_except_handler,

tests/micropython/viper_error.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ ViperTypeError('unary op __neg__ not implemented',)
2020
ViperTypeError('unary op __invert__ not implemented',)
2121
ViperTypeError('binary op not implemented',)
2222
NotImplementedError('native yield',)
23-
NotImplementedError('native yield from',)
23+
NotImplementedError('native yield',)
2424
NotImplementedError('conversion to object',)
2525
NotImplementedError('casting',)

0 commit comments

Comments
 (0)