summaryrefslogtreecommitdiffstatshomepage
path: root/py/emitbc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-09-19 08:46:01 +1000
committerDamien George <damien.p.george@gmail.com>2016-09-19 12:28:03 +1000
commitadaf0d865cd6c81fb352751566460506392ed55f (patch)
tree0a944ff133a34b0f9ee4c6411144b049fd0ebf52 /py/emitbc.c
parenta5624bf3818c573611b2b7bfc755e27de97f64e4 (diff)
downloadmicropython-adaf0d865cd6c81fb352751566460506392ed55f.tar.gz
micropython-adaf0d865cd6c81fb352751566460506392ed55f.zip
py: Combine 3 comprehension opcodes (list/dict/set) into 1.
With the previous patch combining 3 emit functions into 1, it now makes sense to also combine the corresponding VM opcodes, which is what this patch does. This eliminates 2 opcodes which simplifies the VM and reduces code size, in bytes: bare-arm:44, minimal:64, unix(NDEBUG,x86-64):272, stmhal:92, esp8266:200. Profiling (with a simple script that creates many list/dict/set comprehensions) shows no measurable change in performance.
Diffstat (limited to 'py/emitbc.c')
-rw-r--r--py/emitbc.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/py/emitbc.c b/py/emitbc.c
index 11d04c511e..8c712e1fdc 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -862,21 +862,21 @@ void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
#endif
void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
+ int t;
int n;
- byte opcode;
if (kind == SCOPE_LIST_COMP) {
- n = -1;
- opcode = MP_BC_LIST_APPEND;
- } else if (MICROPY_PY_BUILTINS_SET && kind == SCOPE_SET_COMP) {
- n = -1;
- opcode = MP_BC_SET_ADD;
- } else {
- // scope == SCOPE_DICT_COMP
- n = -2;
- opcode = MP_BC_MAP_ADD;
+ n = 0;
+ t = 0;
+ } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
+ n = 1;
+ t = 1;
+ } else if (MICROPY_PY_BUILTINS_SET) {
+ n = 0;
+ t = 2;
}
- emit_bc_pre(emit, n);
- emit_write_bytecode_byte_uint(emit, opcode, collection_stack_index);
+ emit_bc_pre(emit, -1 - n);
+ // the lower 2 bits of the opcode argument indicate the collection type
+ emit_write_bytecode_byte_uint(emit, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
}
void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {