summaryrefslogtreecommitdiffstatshomepage
path: root/py/emitbc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-12-08 12:28:11 +0000
committerDamien George <damien.p.george@gmail.com>2015-12-10 22:19:48 +0000
commitbdbe8c9ae21559f6dc0b8e1ad84b7bbec2a04726 (patch)
tree3c603ca42bd10e7d5bbd037d85d6b57735d39393 /py/emitbc.c
parente242b1785f9e25b36f5d225652c679cd7cec6ec0 (diff)
downloadmicropython-bdbe8c9ae21559f6dc0b8e1ad84b7bbec2a04726.tar.gz
micropython-bdbe8c9ae21559f6dc0b8e1ad84b7bbec2a04726.zip
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.
Diffstat (limited to 'py/emitbc.c')
-rw-r--r--py/emitbc.c13
1 files changed, 3 insertions, 10 deletions
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);
}
}