Skip to content

Commit 5f6a25f

Browse files
committed
py: Wrap #if's around emitter functions that are used only by emitcpy.
3 emitter functions are needed only for emitcpy, and so we can #if them out when compiling with emitcpy support. Also remove unused SETUP_LOOP bytecode.
1 parent 3558f62 commit 5f6a25f

9 files changed

Lines changed: 40 additions & 77 deletions

File tree

py/bc0.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#define MP_BC_POP_JUMP_IF_FALSE (0x47) // rel byte code offset, 16-bit signed, in excess
5252
#define MP_BC_JUMP_IF_TRUE_OR_POP (0x48) // rel byte code offset, 16-bit signed, in excess
5353
#define MP_BC_JUMP_IF_FALSE_OR_POP (0x49) // rel byte code offset, 16-bit signed, in excess
54-
#define MP_BC_SETUP_LOOP (0x4a) // rel byte code offset, 16-bit unsigned
5554
#define MP_BC_SETUP_WITH (0x4d) // rel byte code offset, 16-bit unsigned
5655
#define MP_BC_WITH_CLEANUP (0x4e)
5756
#define MP_BC_SETUP_EXCEPT (0x4f) // rel byte code offset, 16-bit unsigned

py/emit.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ typedef struct _emit_method_table_t {
4141
void (*load_const_dec)(emit_t *emit, qstr qstr);
4242
void (*load_const_id)(emit_t *emit, qstr qstr);
4343
void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
44-
void (*load_const_verbatim_str)(emit_t *emit, const char *str); // only needed for emitcpy
4544
void (*load_null)(emit_t *emit);
4645
void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num);
4746
void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
48-
void (*load_closure)(emit_t *emit, qstr qstr, int local_num); // only needed for emitcpy
4947
void (*load_name)(emit_t *emit, qstr qstr);
5048
void (*load_global)(emit_t *emit, qstr qstr);
5149
void (*load_attr)(emit_t *emit, qstr qstr);
@@ -74,7 +72,6 @@ typedef struct _emit_method_table_t {
7472
void (*pop_jump_if_false)(emit_t *emit, uint label);
7573
void (*jump_if_true_or_pop)(emit_t *emit, uint label);
7674
void (*jump_if_false_or_pop)(emit_t *emit, uint label);
77-
void (*setup_loop)(emit_t *emit, uint label);
7875
void (*break_loop)(emit_t *emit, uint label, int except_depth);
7976
void (*continue_loop)(emit_t *emit, uint label, int except_depth);
8077
void (*setup_with)(emit_t *emit, uint label);
@@ -108,6 +105,14 @@ typedef struct _emit_method_table_t {
108105
void (*raise_varargs)(emit_t *emit, int n_args);
109106
void (*yield_value)(emit_t *emit);
110107
void (*yield_from)(emit_t *emit);
108+
109+
#if MICROPY_EMIT_CPYTHON
110+
// these methods are only needed for emitcpy
111+
void (*load_const_verbatim_str)(emit_t *emit, const char *str);
112+
void (*load_closure)(emit_t *emit, qstr qstr, int local_num);
113+
void (*setup_loop)(emit_t *emit, uint label);
114+
#endif
115+
111116
} emit_method_table_t;
112117

113118
void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);

py/emitbc.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "emit.h"
1717
#include "bc0.h"
1818

19+
#if !MICROPY_EMIT_CPYTHON
20+
1921
struct _emit_t {
2022
pass_kind_t pass;
2123
int stack_size;
@@ -421,11 +423,6 @@ STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
421423
}
422424
}
423425

424-
STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
425-
// not needed/supported for BC
426-
assert(0);
427-
}
428-
429426
STATIC void emit_bc_load_null(emit_t *emit) {
430427
emit_bc_pre(emit, 1);
431428
emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
@@ -447,11 +444,6 @@ STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
447444
emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
448445
}
449446

450-
STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
451-
// not needed/supported for BC
452-
assert(0);
453-
}
454-
455447
STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
456448
emit_bc_pre(emit, 1);
457449
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
@@ -598,11 +590,6 @@ STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
598590
emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
599591
}
600592

601-
STATIC void emit_bc_setup_loop(emit_t *emit, uint label) {
602-
emit_bc_pre(emit, 0);
603-
emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label);
604-
}
605-
606593
STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
607594
if (except_depth == 0) {
608595
emit_bc_jump(emit, label);
@@ -855,11 +842,9 @@ const emit_method_table_t emit_bc_method_table = {
855842
emit_bc_load_const_dec,
856843
emit_bc_load_const_id,
857844
emit_bc_load_const_str,
858-
emit_bc_load_const_verbatim_str,
859845
emit_bc_load_null,
860846
emit_bc_load_fast,
861847
emit_bc_load_deref,
862-
emit_bc_load_closure,
863848
emit_bc_load_name,
864849
emit_bc_load_global,
865850
emit_bc_load_attr,
@@ -888,7 +873,6 @@ const emit_method_table_t emit_bc_method_table = {
888873
emit_bc_pop_jump_if_false,
889874
emit_bc_jump_if_true_or_pop,
890875
emit_bc_jump_if_false_or_pop,
891-
emit_bc_setup_loop,
892876
emit_bc_unwind_jump,
893877
emit_bc_unwind_jump,
894878
emit_bc_setup_with,
@@ -923,3 +907,5 @@ const emit_method_table_t emit_bc_method_table = {
923907
emit_bc_yield_value,
924908
emit_bc_yield_from,
925909
};
910+
911+
#endif // !MICROPY_EMIT_CPYTHON

py/emitcpy.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,6 @@ STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
221221
}
222222
}
223223

224-
STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
225-
emit_pre(emit, 1, 3);
226-
if (emit->pass == PASS_3) {
227-
printf("LOAD_CONST %s\n", str);
228-
}
229-
}
230-
231224
STATIC void emit_cpy_load_null(emit_t *emit) {
232225
// unused for cpy
233226
assert(0);
@@ -247,13 +240,6 @@ STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
247240
}
248241
}
249242

250-
STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
251-
emit_pre(emit, 1, 3);
252-
if (emit->pass == PASS_3) {
253-
printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
254-
}
255-
}
256-
257243
STATIC void emit_cpy_load_name(emit_t *emit, qstr qstr) {
258244
emit_pre(emit, 1, 3);
259245
if (emit->pass == PASS_3) {
@@ -452,13 +438,6 @@ STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, uint label) {
452438
}
453439
}
454440

455-
STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) {
456-
emit_pre(emit, 0, 3);
457-
if (emit->pass == PASS_3) {
458-
printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
459-
}
460-
}
461-
462441
STATIC void emit_cpy_break_loop(emit_t *emit, uint label, int except_depth) {
463442
emit_pre(emit, 0, 1);
464443
if (emit->pass == PASS_3) {
@@ -798,6 +777,27 @@ STATIC void emit_cpy_yield_from(emit_t *emit) {
798777
}
799778
}
800779

780+
STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
781+
emit_pre(emit, 1, 3);
782+
if (emit->pass == PASS_3) {
783+
printf("LOAD_CONST %s\n", str);
784+
}
785+
}
786+
787+
STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
788+
emit_pre(emit, 1, 3);
789+
if (emit->pass == PASS_3) {
790+
printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
791+
}
792+
}
793+
794+
STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) {
795+
emit_pre(emit, 0, 3);
796+
if (emit->pass == PASS_3) {
797+
printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
798+
}
799+
}
800+
801801
const emit_method_table_t emit_cpython_method_table = {
802802
emit_cpy_set_native_types,
803803
emit_cpy_start_pass,
@@ -820,11 +820,9 @@ const emit_method_table_t emit_cpython_method_table = {
820820
emit_cpy_load_const_dec,
821821
emit_cpy_load_const_id,
822822
emit_cpy_load_const_str,
823-
emit_cpy_load_const_verbatim_str,
824823
emit_cpy_load_null,
825824
emit_cpy_load_fast,
826825
emit_cpy_load_deref,
827-
emit_cpy_load_closure,
828826
emit_cpy_load_name,
829827
emit_cpy_load_global,
830828
emit_cpy_load_attr,
@@ -853,7 +851,6 @@ const emit_method_table_t emit_cpython_method_table = {
853851
emit_cpy_pop_jump_if_false,
854852
emit_cpy_jump_if_true_or_pop,
855853
emit_cpy_jump_if_false_or_pop,
856-
emit_cpy_setup_loop,
857854
emit_cpy_break_loop,
858855
emit_cpy_continue_loop,
859856
emit_cpy_setup_with,
@@ -887,6 +884,11 @@ const emit_method_table_t emit_cpython_method_table = {
887884
emit_cpy_raise_varargs,
888885
emit_cpy_yield_value,
889886
emit_cpy_yield_from,
887+
888+
// emitcpy specific functions
889+
emit_cpy_load_const_verbatim_str,
890+
emit_cpy_load_closure,
891+
emit_cpy_setup_loop,
890892
};
891893

892894
#endif // MICROPY_EMIT_CPYTHON

py/emitnative.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,6 @@ STATIC void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
695695
}
696696
}
697697

698-
STATIC void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) {
699-
// not supported/needed for viper
700-
assert(0);
701-
}
702-
703698
STATIC void emit_native_load_null(emit_t *emit) {
704699
emit_native_pre(emit);
705700
emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
@@ -740,11 +735,6 @@ STATIC void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) {
740735
assert(0);
741736
}
742737

743-
STATIC void emit_native_load_closure(emit_t *emit, qstr qstr, int local_num) {
744-
// not implemented
745-
assert(0);
746-
}
747-
748738
STATIC void emit_native_load_name(emit_t *emit, qstr qstr) {
749739
emit_native_pre(emit);
750740
emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, mp_load_name, qstr, REG_ARG_1);
@@ -990,11 +980,6 @@ STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, uint label) {
990980
assert(0);
991981
}
992982

993-
STATIC void emit_native_setup_loop(emit_t *emit, uint label) {
994-
emit_native_pre(emit);
995-
emit_post(emit);
996-
}
997-
998983
STATIC void emit_native_break_loop(emit_t *emit, uint label, int except_depth) {
999984
emit_native_jump(emit, label); // TODO properly
1000985
}
@@ -1339,11 +1324,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
13391324
emit_native_load_const_dec,
13401325
emit_native_load_const_id,
13411326
emit_native_load_const_str,
1342-
emit_native_load_const_verbatim_str,
13431327
emit_native_load_null,
13441328
emit_native_load_fast,
13451329
emit_native_load_deref,
1346-
emit_native_load_closure,
13471330
emit_native_load_name,
13481331
emit_native_load_global,
13491332
emit_native_load_attr,
@@ -1372,7 +1355,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
13721355
emit_native_pop_jump_if_false,
13731356
emit_native_jump_if_true_or_pop,
13741357
emit_native_jump_if_false_or_pop,
1375-
emit_native_setup_loop,
13761358
emit_native_break_loop,
13771359
emit_native_continue_loop,
13781360
emit_native_setup_with,

py/emitpass1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ const emit_method_table_t emit_pass1_method_table = {
189189
(void*)emit_pass1_dummy,
190190
(void*)emit_pass1_dummy,
191191
(void*)emit_pass1_dummy,
192+
#if MICROPY_EMIT_CPYTHON
192193
(void*)emit_pass1_dummy,
193194
(void*)emit_pass1_dummy,
194195
(void*)emit_pass1_dummy,
196+
#endif
195197
};

py/showbc.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,6 @@ void mp_byte_code_print(const byte *ip, int len) {
287287
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
288288
break;
289289

290-
case MP_BC_SETUP_LOOP:
291-
DECODE_ULABEL; // loop labels are always forward
292-
printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
293-
break;
294-
295290
case MP_BC_SETUP_WITH:
296291
DECODE_ULABEL; // loop-like labels are always forward
297292
printf("SETUP_WITH " UINT_FMT, ip + unum - ip_start);

py/vm.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,6 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
481481
}
482482
DISPATCH();
483483

484-
/* we are trying to get away without using this opcode
485-
ENTRY(MP_BC_SETUP_LOOP):
486-
DECODE_UINT;
487-
// push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
488-
DISPATCH();
489-
*/
490-
491484
ENTRY(MP_BC_SETUP_WITH):
492485
obj1 = TOP();
493486
SET_TOP(mp_load_attr(obj1, MP_QSTR___exit__));

py/vmentrytable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static void* entry_table[256] = {
4545
[MP_BC_POP_JUMP_IF_FALSE] = &&entry_MP_BC_POP_JUMP_IF_FALSE,
4646
[MP_BC_JUMP_IF_TRUE_OR_POP] = &&entry_MP_BC_JUMP_IF_TRUE_OR_POP,
4747
[MP_BC_JUMP_IF_FALSE_OR_POP] = &&entry_MP_BC_JUMP_IF_FALSE_OR_POP,
48-
// [MP_BC_SETUP_LOOP] = &&entry_MP_BC_SETUP_LOOP,
4948
[MP_BC_SETUP_WITH] = &&entry_MP_BC_SETUP_WITH,
5049
[MP_BC_WITH_CLEANUP] = &&entry_MP_BC_WITH_CLEANUP,
5150
[MP_BC_UNWIND_JUMP] = &&entry_MP_BC_UNWIND_JUMP,

0 commit comments

Comments
 (0)