diff --git a/py/bc0.h b/py/bc0.h index c5a3015541..b0b7d5c795 100644 --- a/py/bc0.h +++ b/py/bc0.h @@ -80,8 +80,6 @@ #define MP_BC_POP_EXCEPT (0x45) #define MP_BC_UNWIND_JUMP (0x46) // rel byte code offset, 16-bit signed, in excess; then a byte -#define MP_BC_NOT (0x47) - #define MP_BC_BUILD_TUPLE (0x50) // uint #define MP_BC_BUILD_LIST (0x51) // uint #define MP_BC_LIST_APPEND (0x52) // uint @@ -115,7 +113,7 @@ #define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // + N(64) #define MP_BC_LOAD_FAST_MULTI (0xb0) // + N(16) #define MP_BC_STORE_FAST_MULTI (0xc0) // + N(16) -#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(6) -#define MP_BC_BINARY_OP_MULTI (0xd6) // + op(36) +#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(7) +#define MP_BC_BINARY_OP_MULTI (0xd7) // + op(36) #endif // __MICROPY_INCLUDED_PY_BC0_H__ diff --git a/py/emitbc.c b/py/emitbc.c index c08501b3f8..7957a21314 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -801,15 +801,8 @@ void mp_emit_bc_pop_except(emit_t *emit) { } void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) { - if (op == MP_UNARY_OP_NOT) { - emit_bc_pre(emit, 0); - emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL); - emit_bc_pre(emit, 0); - emit_write_bytecode_byte(emit, MP_BC_NOT); - } else { - emit_bc_pre(emit, 0); - emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op); - } + emit_bc_pre(emit, 0); + emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op); } void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) { @@ -825,7 +818,7 @@ void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) { emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op); if (invert) { emit_bc_pre(emit, 0); - emit_write_bytecode_byte(emit, MP_BC_NOT); + emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT); } } diff --git a/py/emitnative.c b/py/emitnative.c index 34e0b928b5..65908070fd 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -2061,11 +2061,6 @@ STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) { vtype_kind_t vtype; emit_pre_pop_reg(emit, &vtype, REG_ARG_2); if (vtype == VTYPE_PYOBJ) { - if (op == MP_UNARY_OP_NOT) { - // we need to synthesise this operation by converting to bool first - emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1); - ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET); - } emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1); emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); } else { diff --git a/py/objbool.c b/py/objbool.c index 622005657a..4276335635 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -74,15 +74,6 @@ STATIC mp_obj_t bool_unary_op(mp_uint_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value); case MP_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value); case MP_UNARY_OP_INVERT: return MP_OBJ_NEW_SMALL_INT(~value); - - // only bool needs to implement MP_UNARY_OP_NOT - case MP_UNARY_OP_NOT: - if (value) { - return mp_const_false; - } else { - return mp_const_true; - } - default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/runtime.c b/py/runtime.c index 58b5a5dd13..c9a56f635b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -184,7 +184,10 @@ void mp_delete_global(qstr qst) { mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) { DEBUG_OP_printf("unary " UINT_FMT " %p\n", op, arg); - if (MP_OBJ_IS_SMALL_INT(arg)) { + if (op == MP_UNARY_OP_NOT) { + // "not x" is the negative of whether "x" is true per Python semantics + return mp_obj_new_bool(mp_obj_is_true(arg) == 0); + } else if (MP_OBJ_IS_SMALL_INT(arg)) { mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg); switch (op) { case MP_UNARY_OP_BOOL: diff --git a/py/runtime0.h b/py/runtime0.h index d00949f929..6417cfd034 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -45,8 +45,6 @@ typedef enum { MP_UNARY_OP_POSITIVE, MP_UNARY_OP_NEGATIVE, MP_UNARY_OP_INVERT, - // The NOT op is only implemented by bool. The emitter must synthesise NOT - // for other types by calling BOOL then inverting (eg by then calling NOT). MP_UNARY_OP_NOT, } mp_unary_op_t; diff --git a/py/showbc.c b/py/showbc.c index 9b08fa6d63..dd5959f4a9 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -399,10 +399,6 @@ const byte *mp_bytecode_print_str(const byte *ip) { printf("POP_EXCEPT"); break; - case MP_BC_NOT: - printf("NOT"); - break; - case MP_BC_BUILD_TUPLE: DECODE_UINT; printf("BUILD_TUPLE " UINT_FMT, unum); @@ -541,7 +537,7 @@ const byte *mp_bytecode_print_str(const byte *ip) { printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI); } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) { printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI); - } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) { + } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 7) { printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI); } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) { mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI; diff --git a/py/vm.c b/py/vm.c index f0a0bad8b7..5bf14eb75b 100644 --- a/py/vm.c +++ b/py/vm.c @@ -760,14 +760,6 @@ unwind_jump:; //sp -= 3; // pop 3 exception values DISPATCH(); - ENTRY(MP_BC_NOT): - if (TOP() == mp_const_true) { - SET_TOP(mp_const_false); - } else { - SET_TOP(mp_const_true); - } - DISPATCH(); - ENTRY(MP_BC_BUILD_TUPLE): { MARK_EXC_IP_SELECTIVE(); DECODE_UINT; @@ -1230,7 +1222,7 @@ yield: } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) { fastn[MP_BC_STORE_FAST_MULTI - (mp_int_t)ip[-1]] = POP(); DISPATCH(); - } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) { + } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 7) { SET_TOP(mp_unary_op(ip[-1] - MP_BC_UNARY_OP_MULTI, TOP())); DISPATCH(); } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) { diff --git a/py/vmentrytable.h b/py/vmentrytable.h index d71a8d4f6f..f3143b5d1d 100644 --- a/py/vmentrytable.h +++ b/py/vmentrytable.h @@ -76,7 +76,6 @@ static void* entry_table[256] = { [MP_BC_FOR_ITER] = &&entry_MP_BC_FOR_ITER, [MP_BC_POP_BLOCK] = &&entry_MP_BC_POP_BLOCK, [MP_BC_POP_EXCEPT] = &&entry_MP_BC_POP_EXCEPT, - [MP_BC_NOT] = &&entry_MP_BC_NOT, [MP_BC_BUILD_TUPLE] = &&entry_MP_BC_BUILD_TUPLE, [MP_BC_BUILD_LIST] = &&entry_MP_BC_BUILD_LIST, [MP_BC_LIST_APPEND] = &&entry_MP_BC_LIST_APPEND, @@ -110,7 +109,7 @@ static void* entry_table[256] = { [MP_BC_LOAD_CONST_SMALL_INT_MULTI ... MP_BC_LOAD_CONST_SMALL_INT_MULTI + 63] = &&entry_MP_BC_LOAD_CONST_SMALL_INT_MULTI, [MP_BC_LOAD_FAST_MULTI ... MP_BC_LOAD_FAST_MULTI + 15] = &&entry_MP_BC_LOAD_FAST_MULTI, [MP_BC_STORE_FAST_MULTI ... MP_BC_STORE_FAST_MULTI + 15] = &&entry_MP_BC_STORE_FAST_MULTI, - [MP_BC_UNARY_OP_MULTI ... MP_BC_UNARY_OP_MULTI + 5] = &&entry_MP_BC_UNARY_OP_MULTI, + [MP_BC_UNARY_OP_MULTI ... MP_BC_UNARY_OP_MULTI + 6] = &&entry_MP_BC_UNARY_OP_MULTI, [MP_BC_BINARY_OP_MULTI ... MP_BC_BINARY_OP_MULTI + 35] = &&entry_MP_BC_BINARY_OP_MULTI, };