aboutsummaryrefslogtreecommitdiff
path: root/py/showbc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-10-25 16:43:46 +0100
committerDamien George <damien.p.george@gmail.com>2014-10-25 20:23:13 +0100
commit8456cc017bc2e4a9fe063f485c3f2aa410435015 (patch)
treee0c8055f6de2c7975b6b7b31e80beb622ce9b3ad /py/showbc.c
parent1084b0f9c21b093618da4494508dec9ca8467e35 (diff)
py: Compress load-int, load-fast, store-fast, unop, binop bytecodes.
There is a lot potential in compress bytecodes and make more use of the coding space. This patch introduces "multi" bytecodes which have their argument included in the bytecode (by addition). UNARY_OP and BINARY_OP now no longer take a 1 byte argument for the opcode. Rather, the opcode is included in the first byte itself. LOAD_FAST_[0,1,2] and STORE_FAST_[0,1,2] are removed in favour of their multi versions, which can take an argument between 0 and 15 inclusive. The majority of LOAD_FAST/STORE_FAST codes fit in this range and so this saves a byte for each of these. LOAD_CONST_SMALL_INT_MULTI is used to load small ints between -16 and 47 inclusive. Such ints are quite common and now only need 1 byte to store, and now have much faster decoding. In all this patch saves about 2% RAM for typically bytecode (1.8% on 64-bit test, 2.5% on pyboard test). It also reduces the binary size (because bytecodes are simplified) and doesn't harm performance.
Diffstat (limited to 'py/showbc.c')
-rw-r--r--py/showbc.c53
1 files changed, 16 insertions, 37 deletions
diff --git a/py/showbc.c b/py/showbc.c
index 13d257d30..93fe2c378 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -190,18 +190,6 @@ void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
printf("LOAD_NULL");
break;
- case MP_BC_LOAD_FAST_0:
- printf("LOAD_FAST_0");
- break;
-
- case MP_BC_LOAD_FAST_1:
- printf("LOAD_FAST_1");
- break;
-
- case MP_BC_LOAD_FAST_2:
- printf("LOAD_FAST_2");
- break;
-
case MP_BC_LOAD_FAST_N:
DECODE_UINT;
printf("LOAD_FAST_N " UINT_FMT, unum);
@@ -240,18 +228,6 @@ void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
printf("LOAD_SUBSCR");
break;
- case MP_BC_STORE_FAST_0:
- printf("STORE_FAST_0");
- break;
-
- case MP_BC_STORE_FAST_1:
- printf("STORE_FAST_1");
- break;
-
- case MP_BC_STORE_FAST_2:
- printf("STORE_FAST_2");
- break;
-
case MP_BC_STORE_FAST_N:
DECODE_UINT;
printf("STORE_FAST_N " UINT_FMT, unum);
@@ -397,16 +373,6 @@ void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
printf("NOT");
break;
- case MP_BC_UNARY_OP:
- unum = *ip++;
- printf("UNARY_OP " UINT_FMT, unum);
- break;
-
- case MP_BC_BINARY_OP:
- unum = *ip++;
- printf("BINARY_OP " UINT_FMT, unum);
- break;
-
case MP_BC_BUILD_TUPLE:
DECODE_UINT;
printf("BUILD_TUPLE " UINT_FMT, unum);
@@ -534,9 +500,22 @@ void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
break;
default:
- printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
- assert(0);
- return;
+ if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
+ printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
+ } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
+ 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 + 5) {
+ printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
+ } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 35) {
+ printf("BINARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_BINARY_OP_MULTI);
+ } else {
+ printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
+ assert(0);
+ return;
+ }
+ break;
}
printf("\n");
}