summaryrefslogtreecommitdiffstatshomepage
path: root/py/emitbc.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-02-18 21:21:22 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-02-19 15:52:57 +0200
commit0f96ec826811e3cec3703141684445bea639e2bc (patch)
treec171e47ca4bee59cc396bd1326aebe98cb7ac604 /py/emitbc.c
parent46239413d033a25662700ba39a97b07737b820fc (diff)
downloadmicropython-0f96ec826811e3cec3703141684445bea639e2bc.tar.gz
micropython-0f96ec826811e3cec3703141684445bea639e2bc.zip
Bytecode uint varlen encoding: support arbitrary values.
Diffstat (limited to 'py/emitbc.c')
-rw-r--r--py/emitbc.c26
1 files changed, 13 insertions, 13 deletions
diff --git a/py/emitbc.c b/py/emitbc.c
index 269fcdeb7e..4fe7ae8cd0 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -108,19 +108,19 @@ STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
}
STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
- if (num <= 127) { // fits in 0x7f
- // fit argument in single byte
- byte* c = emit_get_cur_to_write_byte_code(emit, 1);
- c[0] = num;
- } else if (num <= 16383) { // fits in 0x3fff
- // fit argument in two bytes
- byte* c = emit_get_cur_to_write_byte_code(emit, 2);
- c[0] = (num >> 8) | 0x80;
- c[1] = num;
- } else {
- // larger numbers not implemented/supported
- assert(0);
- }
+ // We store each 7 bits in a separate byte, and that's how many bytes needed
+ byte buf[(BYTES_PER_WORD * 8 + 7) / 7];
+ byte *p = buf + sizeof(buf);
+ // We encode in little-ending order, but store in big-endian, to help decoding
+ do {
+ *--p = num & 0x7f;
+ num >>= 7;
+ } while (num != 0);
+ byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
+ while (p != buf + sizeof(buf) - 1) {
+ *c++ = *p++ | 0x80;
+ }
+ *c = *p;
}
// integers (for small ints) are stored as 24 bits, in excess