py: Make UNARY_OP_NOT a first-class op, to agree with Py not semantics.
Fixes #1684 and makes "not" match Python semantics. The code is also simplified (the separate MP_BC_NOT opcode is removed) and the patch saves 68 bytes for bare-arm/ and 52 bytes for minimal/. Previously "not x" was implemented as !mp_unary_op(x, MP_UNARY_OP_BOOL), so any given object only needs to implement MP_UNARY_OP_BOOL (and the VM had a special opcode to do the ! bit). With this patch "not x" is implemented as mp_unary_op(x, MP_UNARY_OP_NOT), but this operation is caught at the start of mp_unary_op and dispatched as !mp_obj_is_true(x). mp_obj_is_true has special logic to test for truthness, and is the correct way to handle the not operation.
This commit is contained in:
parent
e242b1785f
commit
bdbe8c9ae2
6
py/bc0.h
6
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__
|
||||
|
13
py/emitbc.c
13
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
10
py/vm.c
10
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) {
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user