diff options
author | Damien George <damien.p.george@gmail.com> | 2014-02-19 23:56:23 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-02-19 23:56:23 +0000 |
commit | b74501c98f9abf84f659a447077989eb3ffeb46c (patch) | |
tree | a6e536dab080a1d4714187c52912890a5e62cfb3 /py/vm.c | |
parent | 2e87c62cf62dfeb5b11022359511c31bbb35af54 (diff) | |
parent | 047cd40313e39b662650bbf6c8059ab0910e5986 (diff) | |
download | micropython-b74501c98f9abf84f659a447077989eb3ffeb46c.tar.gz micropython-b74501c98f9abf84f659a447077989eb3ffeb46c.zip |
Merge pull request #312 from pfalcon/int-varlen
Support varlen encoding for arbitrary int and uint numbers
Diffstat (limited to 'py/vm.c')
-rw-r--r-- | py/vm.c | 29 |
1 files changed, 23 insertions, 6 deletions
@@ -38,10 +38,20 @@ typedef enum { UNWIND_JUMP, } mp_unwind_reason_t; -#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0) +#define DECODE_UINT { \ + unum = 0; \ + do { \ + unum = (unum << 7) + (*ip & 0x7f); \ + } while ((*ip++ & 0x80) != 0); \ +} #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0) #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0) -#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0) +#define DECODE_QSTR { \ + qst = 0; \ + do { \ + qst = (qst << 7) + (*ip & 0x7f); \ + } while ((*ip++ & 0x80) != 0); \ +} #define PUSH(val) *++sp = (val) #define POP() (*sp--) #define TOP() (*sp) @@ -146,11 +156,18 @@ dispatch_loop: PUSH(mp_const_ellipsis); break; - case MP_BC_LOAD_CONST_SMALL_INT: - unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000; - ip += 3; - PUSH(MP_OBJ_NEW_SMALL_INT(unum)); + case MP_BC_LOAD_CONST_SMALL_INT: { + int num = 0; + if ((ip[0] & 0x40) != 0) { + // Number is negative + num--; + } + do { + num = (num << 7) | (*ip & 0x7f); + } while ((*ip++ & 0x80) != 0); + PUSH(MP_OBJ_NEW_SMALL_INT(num)); break; + } case MP_BC_LOAD_CONST_INT: DECODE_QSTR; |